summary refs log tree commit diff stats
path: root/compiler/optimizer.nim
blob: 744c82ab5097ccf3a22005d7ab2ffa43e39da2f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#
#
#           The Nim Compiler
#        (c) Copyright 2020 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## Optimizer:
## - elide 'wasMoved(x); destroy(x)' pairs
## - recognize "all paths lead to 'wasMoved(x)'"

import
  ast, renderer, idents, intsets

from trees import exprStructuralEquivalent

const
  nfMarkForDeletion = nfNone # faster than a lookup table

type
  BasicBlock = object
    wasMovedLocs: seq[PNode]
    kind: TNodeKind
    hasReturn, hasBreak: bool
    label: PSym # can be nil
    parent: ptr BasicBlock

  Con = object
    somethingTodo: bool
    inFinally: int

proc nestedBlock(parent: var BasicBlock; kind: TNodeKind): BasicBlock =
  BasicBlock(wasMovedLocs: @[], kind: kind, hasReturn: false, hasBreak: false,
    label: nil, parent: addr(parent))

proc breakStmt(b: var BasicBlock; n: PNode) =
  var it = addr(b)
  while it != nil:
    it.wasMovedLocs.setLen 0
    it.hasBreak = true

    if n.kind == nkSym:
      if it.label == n.sym: break
    else:
      # unnamed break leaves the block is nkWhileStmt or the like:
      if it.kind in {nkWhileStmt, nkBlockStmt, nkBlockExpr}: break

    it = it.parent

proc returnStmt(b: var BasicBlock) =
  b.hasReturn = true
  var it = addr(b)
  while it != nil:
    it.wasMovedLocs.setLen 0
    it = it.parent

proc mergeBasicBlockInfo(parent: var BasicBlock; this: BasicBlock) {.inline.} =
  if this.hasReturn:
    parent.wasMovedLocs.setLen 0
    parent.hasReturn = true

proc wasMovedTarget(matches: var IntSet; branch: seq[PNode]; moveTarget: PNode): bool =
  result = false
  for i in 0..<branch.len:
    if exprStructuralEquivalent(branch[i][1].skipAddr, moveTarget,
                                strictSymEquality = true):
      result = true
      matches.incl i

proc intersect(summary: var seq[PNode]; branch: seq[PNode]) =
  # keep all 'wasMoved(x)' calls in summary that are also in 'branch':
  var i = 0
  var matches = initIntSet()
  while i < summary.len:
    if wasMovedTarget(matches, branch, summary[i][1].skipAddr):
      inc i
    else:
      summary.del i
  for m in matches:
    summary.add branch[m]


proc invalidateWasMoved(c: var BasicBlock; x: PNode) =
  var i = 0
  while i < c.wasMovedLocs.len:
    if exprStructuralEquivalent(c.wasMovedLocs[i][1].skipAddr, x,
                                strictSymEquality = true):
      c.wasMovedLocs.del i
    else:
      inc i

proc wasMovedDestroyPair(c: var Con; b: var BasicBlock; d: PNode) =
  var i = 0
  while i < b.wasMovedLocs.len:
    if exprStructuralEquivalent(b.wasMovedLocs[i][1].skipAddr, d[1].skipAddr,
                                strictSymEquality = true):
      b.wasMovedLocs[i].flags.incl nfMarkForDeletion
      c.somethingTodo = true
      d.flags.incl nfMarkForDeletion
      b.wasMovedLocs.del i
    else:
      inc i

proc analyse(c: var Con; b: var BasicBlock; n: PNode) =
  case n.kind
  of nkCallKinds:
    var special = false
    var reverse = false
    if n[0].kind == nkSym:
      let s = n[0].sym
      if s.magic == mWasMoved:
        b.wasMovedLocs.add n
        special = true
      elif s.name.s == "=destroy":
        if c.inFinally > 0 and (b.hasReturn or b.hasBreak):
          discard "cannot optimize away the destructor"
        else:
          c.wasMovedDestroyPair b, n
        special = true
      elif s.name.s == "=sink":
        reverse = true

    if not special:
      if not reverse:
        for i in 0 ..< n.len:
          analyse(c, b, n[i])
      else:
        #[ Test tmatrix.test3:
        Prevent this from being elided. We should probably
        find a better solution...

            `=sink`(b, - (
              let blitTmp = b;
              wasMoved(b);
              blitTmp + a)
            `=destroy`(b)

        ]#
        for i in countdown(n.len-1, 0):
          analyse(c, b, n[i])
      if canRaise(n[0]): returnStmt(b)

  of nkSym:
    # any usage of the location before destruction implies we
    # cannot elide the 'wasMoved(x)':
    b.invalidateWasMoved n

  of nkNone..pred(nkSym), succ(nkSym)..nkNilLit, nkTypeSection, nkProcDef, nkConverterDef,
      nkMethodDef, nkIteratorDef, nkMacroDef, nkTemplateDef, nkLambda, nkDo,
      nkFuncDef, nkConstSection, nkConstDef, nkIncludeStmt, nkImportStmt,
      nkExportStmt, nkPragma, nkCommentStmt, nkBreakState,
      nkTypeOfExpr, nkMixinStmt, nkBindStmt:
    discard "do not follow the construct"

  of nkAsgn, nkFastAsgn:
    # reverse order, see remark for `=sink`:
    analyse(c, b, n[1])
    analyse(c, b, n[0])

  of nkIfStmt, nkIfExpr:
    let isExhaustive = n[^1].kind in {nkElse, nkElseExpr}
    var wasMovedSet: seq[PNode] = @[]

    for i in 0 ..< n.len:
      var branch = nestedBlock(b, n[i].kind)

      analyse(c, branch, n[i])
      mergeBasicBlockInfo(b, branch)
      if isExhaustive:
        if i == 0:
          wasMovedSet = move(branch.wasMovedLocs)
        else:
          wasMovedSet.intersect(branch.wasMovedLocs)
    for i in 0..<wasMovedSet.len:
      b.wasMovedLocs.add wasMovedSet[i]

  of nkCaseStmt:
    let isExhaustive = skipTypes(n[0].typ,
      abstractVarRange-{tyTypeDesc}).kind notin {tyFloat..tyFloat128, tyString} or
      n[^1].kind == nkElse

    analyse(c, b, n[0])

    var wasMovedSet: seq[PNode] = @[]

    for i in 1 ..< n.len:
      var branch = nestedBlock(b, n[i].kind)

      analyse(c, branch, n[i])
      mergeBasicBlockInfo(b, branch)
      if isExhaustive:
        if i == 1:
          wasMovedSet = move(branch.wasMovedLocs)
        else:
          wasMovedSet.intersect(branch.wasMovedLocs)
    for i in 0..<wasMovedSet.len:
      b.wasMovedLocs.add wasMovedSet[i]

  of nkTryStmt:
    for i in 0 ..< n.len:
      var tryBody = nestedBlock(b, nkTryStmt)

      analyse(c, tryBody, n[i])
      mergeBasicBlockInfo(b, tryBody)

  of nkWhileStmt:
    analyse(c, b, n[0])
    var loopBody = nestedBlock(b, nkWhileStmt)
    analyse(c, loopBody, n[1])
    mergeBasicBlockInfo(b, loopBody)

  of nkBlockStmt, nkBlockExpr:
    var blockBody = nestedBlock(b, n.kind)
    if n[0].kind == nkSym:
      blockBody.label = n[0].sym
    analyse(c, blockBody, n[1])
    mergeBasicBlockInfo(b, blockBody)

  of nkBreakStmt:
    breakStmt(b, n[0])

  of nkReturnStmt, nkRaiseStmt:
    for child in n: analyse(c, b, child)
    returnStmt(b)

  of nkFinally:
    inc c.inFinally
    for child in n: analyse(c, b, child)
    dec c.inFinally

  else:
    for child in n: analyse(c, b, child)

proc opt(c: Con; n, parent: PNode; parentPos: int) =
  template recurse() =
    let x = shallowCopy(n)
    for i in 0 ..< n.len:
      opt(c, n[i], x, i)
    parent[parentPos] = x

  case n.kind
  of nkCallKinds:
    if nfMarkForDeletion in n.flags:
      parent[parentPos] = newNodeI(nkEmpty, n.info)
    else:
      recurse()

  of nkNone..nkNilLit, nkTypeSection, nkProcDef, nkConverterDef,
      nkMethodDef, nkIteratorDef, nkMacroDef, nkTemplateDef, nkLambda, nkDo,
      nkFuncDef, nkConstSection, nkConstDef, nkIncludeStmt, nkImportStmt,
      nkExportStmt, nkPragma, nkCommentStmt, nkBreakState, nkTypeOfExpr,
      nkMixinStmt, nkBindStmt:
    parent[parentPos] = n

  else:
    recurse()


proc optimize*(n: PNode): PNode =
  # optimize away simple 'wasMoved(x); destroy(x)' pairs.
  #[ Unfortunately this optimization is only really safe when no exceptions
     are possible, see for example:

  proc main(inp: string; cond: bool) =
    if cond:
      try:
        var s = ["hi", inp & "more"]
        for i in 0..4:
          use s
        consume(s)
        wasMoved(s)
      finally:
        destroy(s)

    Now assume 'use' raises, then we shouldn't do the 'wasMoved(s)'
  ]#
  var c: Con
  var b: BasicBlock
  analyse(c, b, n)
  if c.somethingTodo:
    result = shallowCopy(n)
    for i in 0 ..< n.safeLen:
      opt(c, n[i], result, i)
  else:
    result = n
an class="p">, metavar='OUTFILE', help="Makes ranger act like a directory chooser. When ranger quits" ", it will write the name of the last visited directory to OUTFILE") parser.add_option('--selectfile', type='string', metavar='filepath', help="Open ranger with supplied file selected.") parser.add_option('--show-only-dirs', action='store_true', help="Show only directories, no files or links") parser.add_option('--list-unused-keys', action='store_true', help="List common keys which are not bound to any action.") parser.add_option('--list-tagged-files', type='string', default=None, metavar='tag', help="List all files which are tagged with the given tag, default: *") parser.add_option('--profile', action='store_true', help="Print statistics of CPU usage on exit.") parser.add_option('--cmd', action='append', type='string', metavar='COMMAND', help="Execute COMMAND after the configuration has been read. " "Use this option multiple times to run multiple commands.") args, positional = parser.parse_args() args.paths = positional def path_init(option): argval = args.__dict__[option] try: path = os.path.abspath(argval) except OSError as ex: sys.stderr.write( '--{0} is not accessible: {1}\n{2}\n'.format(option, argval, str(ex))) sys.exit(1) if os.path.exists(path) and not os.access(path, os.W_OK): sys.stderr.write('--{0} is not writable: {1}\n'.format(option, path)) sys.exit(1) return path if args.clean: from tempfile import mkdtemp args.cachedir = mkdtemp(suffix='.ranger-cache') args.confdir = None args.datadir = None @atexit.register def cleanup_cachedir(): # pylint: disable=unused-variable try: shutil.rmtree(args.cachedir) except Exception as ex: # pylint: disable=broad-except sys.stderr.write( "Error during the temporary cache directory cleanup:\n" "{}\n".format(ex) ) else: args.cachedir = path_init('cachedir') args.confdir = path_init('confdir') args.datadir = path_init('datadir') if args.choosefile: args.choosefile = path_init('choosefile') if args.choosefiles: args.choosefiles = path_init('choosefiles') if args.choosedir: args.choosedir = path_init('choosedir') return args COMMANDS_EXCLUDE = ['settings', 'notify'] def load_settings( # pylint: disable=too-many-locals,too-many-branches,too-many-statements fm, clean): from ranger.core.actions import Actions import ranger.core.shared import ranger.api.commands from ranger.config import commands as commands_default # Load default commands fm.commands = ranger.api.commands.CommandContainer() include = [name for name in dir(Actions) if name not in COMMANDS_EXCLUDE] fm.commands.load_commands_from_object(fm, include) fm.commands.load_commands_from_module(commands_default) if not clean: system_confdir = os.path.join(os.sep, 'etc', 'ranger') if os.path.exists(system_confdir): sys.path.append(system_confdir) allow_access_to_confdir(ranger.args.confdir, True) # Load custom commands def import_file(name, path): # From https://stackoverflow.com/a/67692 # pragma pylint: disable=no-name-in-module,import-error,no-member, deprecated-method if sys.version_info >= (3, 5): import importlib.util as util spec = util.spec_from_file_location(name, path) module = util.module_from_spec(spec) spec.loader.exec_module(module) elif (3, 3) <= sys.version_info < (3, 5): from importlib.machinery import SourceFileLoader module = SourceFileLoader(name, path).load_module() else: import imp module = imp.load_source(name, path) # pragma pylint: enable=no-name-in-module,import-error,no-member return module def load_custom_commands(*paths): old_bytecode_setting = sys.dont_write_bytecode sys.dont_write_bytecode = True for custom_comm_path in paths: if os.path.exists(custom_comm_path): try: commands_custom = import_file('commands', custom_comm_path) fm.commands.load_commands_from_module(commands_custom) except ImportError as ex: LOG.debug("Failed to import custom commands from '%s'", custom_comm_path) LOG.exception(ex) else: LOG.debug("Loaded custom commands from '%s'", custom_comm_path) sys.dont_write_bytecode = old_bytecode_setting system_comm_path = os.path.join(system_confdir, 'commands.py') custom_comm_path = fm.confpath('commands.py') load_custom_commands(system_comm_path, custom_comm_path) # XXX Load plugins (experimental) plugindir = fm.confpath('plugins') try: plugin_files = os.listdir(plugindir) except OSError: LOG.debug('Unable to access plugin directory: %s', plugindir) else: plugins = [] for path in plugin_files: if not path.startswith('_'): if path.endswith('.py'): # remove trailing '.py' plugins.append(path[:-3]) elif os.path.isdir(os.path.join(plugindir, path)): plugins.append(path) if not os.path.exists(fm.confpath('plugins', '__init__.py')): LOG.debug("Creating missing '__init__.py' file in plugin folder") fobj = open(fm.confpath('plugins', '__init__.py'), 'w') fobj.close() ranger.fm = fm for plugin in sorted(plugins): try: try: # importlib does not exist before python2.7. It's # required for loading commands from plugins, so you # can't use that feature in python2.6. import importlib except ImportError: module = __import__('plugins', fromlist=[plugin]) else: module = importlib.import_module('plugins.' + plugin) fm.commands.load_commands_from_module(module) LOG.debug("Loaded plugin '%s'", plugin) except Exception as ex: # pylint: disable=broad-except ex_msg = "Error while loading plugin '{0}'".format(plugin) LOG.error(ex_msg) LOG.exception(ex) fm.notify(ex_msg, bad=True) ranger.fm = None allow_access_to_confdir(ranger.args.confdir, False) # Load rc.conf custom_conf = fm.confpath('rc.conf') system_conf = os.path.join(system_confdir, 'rc.conf') default_conf = fm.relpath('config', 'rc.conf') custom_conf_is_readable = os.access(custom_conf, os.R_OK) system_conf_is_readable = os.access(system_conf, os.R_OK) if (os.environ.get('RANGER_LOAD_DEFAULT_RC', 'TRUE').upper() != 'FALSE' or not (custom_conf_is_readable or system_conf_is_readable)): fm.source(default_conf) if system_conf_is_readable: fm.source(system_conf) if custom_conf_is_readable: fm.source(custom_conf) else: fm.source(fm.relpath('config', 'rc.conf')) def allow_access_to_confdir(confdir, allow): from errno import EEXIST if allow: try: os.makedirs(confdir) except OSError as err: if err.errno != EEXIST: # EEXIST means it already exists print("This configuration directory could not be created:") print(confdir) print("To run ranger without the need for configuration") print("files, use the --clean option.") raise SystemExit else: LOG.debug("Created config directory '%s'", confdir) if confdir not in sys.path: sys.path[0:0] = [confdir] else: if sys.path[0] == confdir: del sys.path[0]