cfg_optimization.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. """Optimizes and analyzes CFG-IR."""
  2. from collections import defaultdict
  3. import modelverse_jit.cfg_ir as cfg_ir
  4. import modelverse_jit.cfg_dominators as cfg_dominators
  5. import modelverse_jit.cfg_ssa_construction as cfg_ssa_construction
  6. import modelverse_jit.cfg_data_structures as cfg_data_structures
  7. import modelverse_kernel.primitives as primitive_functions
  8. def is_empty_block(block):
  9. """Tests if the given block contains no parameters or definitions."""
  10. return len(block.parameters) == 0 and len(block.definitions) == 0
  11. def optimize_flow(block):
  12. """Optimizes the given block's flow instruction."""
  13. changed = True
  14. while changed:
  15. changed = False
  16. # Select flow with a literal condition can be optimized to a direct jump.
  17. if (isinstance(block.flow, cfg_ir.SelectFlow)
  18. and cfg_ir.is_literal_def(block.flow.condition)):
  19. literal = cfg_ir.get_literal_def_value(block.flow.condition)
  20. block.flow = cfg_ir.JumpFlow(
  21. block.flow.if_branch if literal else block.flow.else_branch)
  22. changed = True
  23. # Jumps to blocks which contain no parameters or definitions can be replaced
  24. # by the target block's flow.
  25. if (isinstance(block.flow, cfg_ir.JumpFlow)
  26. and is_empty_block(block.flow.branch.block)
  27. and block.flow.branch.block is not block):
  28. block.flow = block.flow.branch.block.flow
  29. changed = True
  30. # Branches to blocks which contain nothing but a jump can be replaced by branches
  31. # to the jump's target.
  32. for branch in block.flow.branches():
  33. if (is_empty_block(branch.block)
  34. and branch.block is not block
  35. and isinstance(branch.block.flow, cfg_ir.JumpFlow)):
  36. new_branch = branch.block.flow.branch
  37. branch.block = new_branch.block
  38. branch.arguments = new_branch.arguments
  39. changed = True
  40. def optimize_graph_flow(entry_point):
  41. """Optimizes all flow instructions in the graph defined by the given entry point."""
  42. for block in cfg_ir.get_all_blocks(entry_point):
  43. optimize_flow(block)
  44. def merge_blocks(entry_point):
  45. """Merges blocks which have exactly one predecessor with said predecessor, if the
  46. predecessor has a jump flow instruction."""
  47. predecessor_map = cfg_ir.get_all_predecessor_blocks(entry_point)
  48. queue = set(predecessor_map.keys())
  49. queue.add(entry_point)
  50. def __do_merge(source, target):
  51. target_params = list(target.parameters)
  52. branch_args = list(source.flow.branch.arguments)
  53. for target_param, branch_arg in zip(target_params, branch_args):
  54. target.remove_parameter(target_param)
  55. target_param.redefine(branch_arg)
  56. source.append_definition(target_param)
  57. target_defs = list(target.definitions)
  58. for target_def in target_defs:
  59. target.remove_definition(target_def)
  60. source.append_definition(target_def)
  61. source.flow = target.flow
  62. for preds in predecessor_map.values():
  63. if target in preds:
  64. preds[preds.index(target)] = source
  65. # preds.remove(target)
  66. # preds.add(source)
  67. while len(queue) > 0:
  68. block = queue.pop()
  69. if isinstance(block.flow, cfg_ir.JumpFlow):
  70. next_block = block.flow.branch.block
  71. preds = predecessor_map[next_block]
  72. if (len(preds) == 1
  73. and next(iter(preds)) == block
  74. and block != next_block
  75. and next_block != entry_point):
  76. __do_merge(block, next_block)
  77. del predecessor_map[next_block]
  78. queue.add(block)
  79. if next_block in queue:
  80. queue.remove(next_block)
  81. def elide_local_checks(entry_point):
  82. """Tries to elide redundant checks on local variables."""
  83. # The plan here is to replace all check-local-exists defs by literals if
  84. # they are either dominated by an appropriate declare-local or not reachable
  85. # from a declare-local.
  86. local_checks = []
  87. local_defs = defaultdict(list)
  88. for block in cfg_ir.get_all_blocks(entry_point):
  89. for definition in block.definitions:
  90. def_value = definition.value
  91. if isinstance(def_value, cfg_ir.CheckLocalExists):
  92. local_checks.append((def_value.variable.node_id, definition))
  93. elif isinstance(def_value, cfg_ir.DeclareLocal):
  94. local_defs[def_value.variable.node_id].append(definition)
  95. dominator_tree = cfg_dominators.get_dominator_tree(entry_point)
  96. reachable_blocks = cfg_ir.get_all_reachable_blocks(entry_point)
  97. for (variable, check) in local_checks:
  98. is_reachable = False
  99. for local_def in local_defs[variable]:
  100. if dominator_tree.dominates_instruction(local_def, check):
  101. # Check is dominated by a definition. Replace it by a 'True' literal.
  102. check.redefine(cfg_ir.Literal(True))
  103. is_reachable = True
  104. break
  105. elif check.block in reachable_blocks[local_def.block]:
  106. is_reachable = True
  107. if not is_reachable:
  108. # Check cannot be reached from any definition. Replace it by a 'False' literal.
  109. check.redefine(cfg_ir.Literal(False))
  110. def eliminate_unused_definitions(entry_point):
  111. """Tries to eliminate unused definitions in the control-flow graphb defined by the
  112. given entry point."""
  113. def_dependencies = defaultdict(set)
  114. root_defs = set()
  115. # Gather dependencies.
  116. for block in cfg_ir.get_all_blocks(entry_point):
  117. for definition in block.parameters + block.definitions:
  118. all_dependencies = list(definition.get_all_dependencies())
  119. def_dependencies[definition].update(
  120. [dep for dep in all_dependencies
  121. if isinstance(dep, cfg_ir.Definition)])
  122. if len(all_dependencies) > 0 and definition.has_bidirectional_dependencies():
  123. for dep in all_dependencies:
  124. def_dependencies[dep].add(definition)
  125. if definition.has_side_effects():
  126. root_defs.add(definition)
  127. for dep in block.flow.get_dependencies():
  128. if isinstance(dep, cfg_ir.Definition):
  129. root_defs.add(dep)
  130. else:
  131. assert isinstance(dep, cfg_ir.Branch)
  132. for param, arg in zip(dep.block.parameters, dep.arguments):
  133. def_dependencies[param].add(arg)
  134. # Figure out which definitions are live.
  135. live_defs = set()
  136. def __mark_live(definition):
  137. if definition in live_defs:
  138. return
  139. live_defs.add(definition)
  140. if definition in def_dependencies:
  141. for dep in def_dependencies[definition]:
  142. __mark_live(dep)
  143. for root in root_defs:
  144. __mark_live(root)
  145. # Remove all dead definitions.
  146. dead_defs = set.difference(set(def_dependencies.keys()), live_defs)
  147. dead_phis = set()
  148. for dead_def in dead_defs:
  149. if isinstance(dead_def.value, cfg_ir.BlockParameter):
  150. dead_phis.add(dead_def)
  151. else:
  152. dead_def.block.remove_definition(dead_def)
  153. erase_parameters(entry_point, dead_phis)
  154. def eliminate_trivial_phis(entry_point):
  155. """Eliminates trivial block parameters, i.e., block parameters which are really
  156. aliases."""
  157. phi_values = defaultdict(set)
  158. all_blocks = list(cfg_ir.get_all_blocks(entry_point))
  159. for block in all_blocks:
  160. for branch in block.flow.branches():
  161. for phi, arg in zip(branch.block.parameters, branch.arguments):
  162. phi_values[phi].add(arg)
  163. replacements = []
  164. for block in all_blocks:
  165. block_parameters = list(block.parameters)
  166. for parameter_def in block_parameters:
  167. trivial_phi_val = cfg_ir.get_trivial_phi_value(
  168. parameter_def, phi_values[parameter_def])
  169. if trivial_phi_val is not None:
  170. replacements.append((parameter_def, trivial_phi_val))
  171. erase_parameters(entry_point, set([parameter_def for parameter_def, _ in replacements]))
  172. for parameter_def, trivial_phi_val in replacements:
  173. block = parameter_def.block
  174. parameter_def.redefine(trivial_phi_val)
  175. block.prepend_definition(parameter_def)
  176. def erase_parameters(entry_point, parameters_to_erase):
  177. """Erases all arguments for the given set of parameters, and then takes out the
  178. parameters themselves."""
  179. for block in cfg_ir.get_all_blocks(entry_point):
  180. for branch in block.flow.branches():
  181. new_arg_list = []
  182. for parameter, arg in zip(branch.block.parameters, branch.arguments):
  183. if parameter not in parameters_to_erase:
  184. new_arg_list.append(arg)
  185. branch.arguments = new_arg_list
  186. for parameter_def in parameters_to_erase:
  187. parameter_def.block.remove_parameter(parameter_def)
  188. def apply_cfg_intrinsic(intrinsic_function, original_definition, named_args):
  189. """Applies the given intrinsic to the given sequence of named arguments."""
  190. kwargs = dict(named_args)
  191. kwargs['original_def'] = original_definition
  192. return intrinsic_function(**kwargs)
  193. def try_redefine_as_direct_call(definition, jit, called_globals):
  194. """Tries to redefine the given indirect call definition as a direct call."""
  195. call = cfg_ir.get_def_value(definition)
  196. if not isinstance(call, cfg_ir.IndirectFunctionCall):
  197. return
  198. target = cfg_ir.get_def_value(call.target)
  199. if isinstance(target, cfg_ir.LoadPointer):
  200. loaded_ptr = cfg_ir.get_def_value(target.pointer)
  201. if isinstance(loaded_ptr, cfg_ir.ResolveGlobal):
  202. resolved_var_name = loaded_ptr.variable.name
  203. called_globals.add(loaded_ptr)
  204. # Try to resolve the callee as an intrinsic.
  205. intrinsic = jit.get_cfg_intrinsic(resolved_var_name)
  206. if intrinsic is not None:
  207. definition.redefine(
  208. cfg_ir.DirectFunctionCall(
  209. resolved_var_name, call.argument_list,
  210. cfg_ir.JIT_CFG_INTRINSIC_CALLING_CONVENTION))
  211. else:
  212. # Otherwise, build a thunk.
  213. thunk_name = jit.jit_thunk_global(resolved_var_name)
  214. calling_convention = (
  215. cfg_ir.JIT_NO_GC_CALLING_CONVENTION
  216. if jit.get_intrinsic(thunk_name) is not None
  217. else cfg_ir.JIT_CALLING_CONVENTION)
  218. definition.redefine(
  219. cfg_ir.DirectFunctionCall(
  220. thunk_name, call.argument_list, calling_convention))
  221. called_globals.add(loaded_ptr)
  222. elif isinstance(target, cfg_ir.Literal):
  223. node_id = target.literal
  224. thunk_name = jit.jit_thunk_constant_function(node_id)
  225. definition.redefine(
  226. cfg_ir.DirectFunctionCall(
  227. thunk_name, call.argument_list, cfg_ir.JIT_CALLING_CONVENTION))
  228. def get_checked_global(definition):
  229. """If the definition is a check that tests if a global does not exist, then
  230. the instruction that resolves the global is returned; otherwise None."""
  231. def_value = cfg_ir.get_def_value(definition)
  232. if not isinstance(def_value, cfg_ir.Binary):
  233. return None
  234. if def_value.operator != 'is':
  235. return None
  236. def __get_checked_global_single_dir(lhs, rhs):
  237. if (isinstance(lhs, cfg_ir.ResolveGlobal)
  238. and isinstance(rhs, cfg_ir.Literal)
  239. and rhs.literal is None):
  240. return lhs
  241. else:
  242. return None
  243. bin_lhs = cfg_ir.get_def_value(def_value.lhs)
  244. bin_rhs = cfg_ir.get_def_value(def_value.rhs)
  245. result = __get_checked_global_single_dir(bin_lhs, bin_rhs)
  246. if result is None:
  247. result = __get_checked_global_single_dir(bin_rhs, bin_lhs)
  248. return result
  249. def optimize_calls(entry_point, jit):
  250. """Converts indirect calls to direct calls in the control-flow graph defined by the
  251. given entry point."""
  252. called_globals = set()
  253. global_exists_defs = defaultdict(list)
  254. for block in cfg_ir.get_all_blocks(entry_point):
  255. for definition in block.definitions:
  256. checked_global = get_checked_global(definition)
  257. if checked_global is not None:
  258. global_exists_defs[checked_global].append(definition)
  259. else:
  260. try_redefine_as_direct_call(definition, jit, called_globals)
  261. for resolve_global in called_globals:
  262. for exists_def in global_exists_defs[resolve_global]:
  263. exists_def.redefine(cfg_ir.Literal(False))
  264. def expand_cfg_intrinsics(entry_point, jit):
  265. """Expands CFG JIT intrinsics in the control-flow graph defined by the given entry point."""
  266. for block in cfg_ir.get_all_blocks(entry_point):
  267. for definition in block.definitions:
  268. def_value = definition.value
  269. if (isinstance(def_value, cfg_ir.DirectFunctionCall)
  270. and def_value.calling_convention ==
  271. cfg_ir.JIT_CFG_INTRINSIC_CALLING_CONVENTION):
  272. intrinsic = jit.get_cfg_intrinsic(def_value.target_name)
  273. apply_cfg_intrinsic(intrinsic, definition, def_value.argument_list)
  274. def simplify_values(entry_point):
  275. """Simplifies values in the control-flow graph defined by the given entry point."""
  276. for block in cfg_ir.get_all_blocks(entry_point):
  277. for definition in block.definitions:
  278. def_val = cfg_ir.get_def_value(definition)
  279. if isinstance(def_val, cfg_ir.Read):
  280. read_node = cfg_ir.get_def_value(def_val.node)
  281. if isinstance(read_node, cfg_ir.CreateNode):
  282. definition.redefine(read_node.value)
  283. elif isinstance(def_val, cfg_ir.Binary):
  284. lhs = cfg_ir.get_def_value(def_val.lhs)
  285. rhs = cfg_ir.get_def_value(def_val.rhs)
  286. if isinstance(lhs, cfg_ir.Literal) and isinstance(rhs, cfg_ir.Literal):
  287. definition.redefine(
  288. cfg_ir.Literal(
  289. eval('%r %s %r' % (lhs.literal, def_val.operator, rhs.literal))))
  290. elif isinstance(def_val, cfg_ir.Unary):
  291. operand = cfg_ir.get_def_value(def_val.operand)
  292. if isinstance(operand, cfg_ir.Literal):
  293. definition.redefine(
  294. cfg_ir.Literal(
  295. eval('%s %r' % (def_val.operator, operand.literal))))
  296. def inline_constants(entry_point):
  297. """Replaces reads of constant nodes by the literals they contain."""
  298. for block in cfg_ir.get_all_blocks(entry_point):
  299. for definition in block.definitions:
  300. def_val = cfg_ir.get_def_value(definition)
  301. if isinstance(def_val, cfg_ir.Read):
  302. read_node = cfg_ir.get_def_value(def_val.node)
  303. if isinstance(read_node, cfg_ir.Literal):
  304. val, = yield [("RV", [read_node.literal])]
  305. definition.redefine(cfg_ir.Literal(val))
  306. def expand_indirect_definitions(entry_point):
  307. """Replaces indirect definitions by the values referred to by those definitions."""
  308. def __expand_indirect_defs(value):
  309. dependencies = value.get_dependencies()
  310. if len(dependencies) == 0:
  311. return value
  312. else:
  313. new_dependencies = []
  314. for dep in dependencies:
  315. new_dep = dep
  316. if isinstance(new_dep, cfg_ir.Definition):
  317. while isinstance(new_dep.value, cfg_ir.Definition):
  318. new_dep = new_dep.value
  319. else:
  320. new_dep = __expand_indirect_defs(new_dep)
  321. new_dependencies.append(new_dep)
  322. return value.create(new_dependencies)
  323. for block in cfg_ir.get_all_blocks(entry_point):
  324. block_definitions = list(block.definitions)
  325. for definition in block_definitions:
  326. if isinstance(definition.value, cfg_ir.Definition):
  327. block.remove_definition(definition)
  328. else:
  329. definition.redefine(
  330. __expand_indirect_defs(definition.value))
  331. block.flow = __expand_indirect_defs(block.flow)
  332. def optimize_reads(entry_point):
  333. """Tries to replace repeated reads by a single read."""
  334. cfg_ir.match_and_rewrite(
  335. entry_point,
  336. lambda _: True,
  337. lambda use_def, _: cfg_ir.is_value_def(use_def, cfg_ir.Read),
  338. lambda def_def:
  339. def_def.redefine(
  340. cfg_ir.Read(def_def.insert_before(def_def.value))),
  341. lambda use_def, def_def: use_def.redefine(def_def))
  342. def protect_from_gc(entry_point):
  343. """Protects locals in the control-flow graph defined by the given
  344. entry point from the GC."""
  345. root_node = entry_point.prepend_definition(cfg_ir.AllocateRootNode())
  346. def protect_def_from_gc(definition):
  347. """Protects the given definition from the GC."""
  348. definition.insert_after(cfg_ir.create_gc_protect(definition, root_node))
  349. def maybe_protect_def_from_gc(definition):
  350. """Protects the given definition from the GC, if its result is not None."""
  351. definition.insert_after(cfg_ir.create_conditional_gc_protect(definition, root_node))
  352. for block in cfg_ir.get_all_blocks(entry_point):
  353. for definition in block.definitions:
  354. def_value = cfg_ir.get_def_value(definition)
  355. if isinstance(def_value, cfg_ir.CreateNode):
  356. protect_def_from_gc(definition)
  357. elif (isinstance(def_value, cfg_ir.IndirectFunctionCall)
  358. or (isinstance(def_value, cfg_ir.DirectFunctionCall)
  359. and (def_value.calling_convention == cfg_ir.JIT_CALLING_CONVENTION
  360. or def_value.calling_convention == cfg_ir.JIT_NO_GC_CALLING_CONVENTION
  361. or def_value.calling_convention == cfg_ir.MACRO_IO_CALLING_CONVENTION)
  362. and def_value.has_value())):
  363. maybe_protect_def_from_gc(definition)
  364. if isinstance(block.flow, (cfg_ir.ReturnFlow, cfg_ir.ThrowFlow)):
  365. block.append_definition(cfg_ir.DeallocateRootNode(root_node))
  366. def elide_gc_protects(entry_point):
  367. """Tries to elide GC protection values."""
  368. # We don't need to protect a value from the GC if it is used for the
  369. # last time _before_ the GC has an opportunity to kick in. To simplify
  370. # things, we'll do a quick block-based analysis.
  371. def __may_cause_gc(definition):
  372. def_value = cfg_ir.get_def_value(definition)
  373. if isinstance(def_value, cfg_ir.IndirectFunctionCall):
  374. return True
  375. elif (isinstance(def_value, cfg_ir.DirectFunctionCall)
  376. and (def_value.calling_convention == cfg_ir.JIT_CALLING_CONVENTION
  377. or def_value.calling_convention == cfg_ir.MACRO_IO_CALLING_CONVENTION)):
  378. return True
  379. else:
  380. return False
  381. def __get_protected_def(def_or_value):
  382. value = cfg_ir.get_def_value(def_or_value)
  383. if cfg_ir.is_call(
  384. value, target_name=cfg_ir.GC_PROTECT_MACRO_NAME,
  385. calling_convention=cfg_ir.MACRO_POSITIONAL_CALLING_CONVENTION):
  386. _, protected_def = value.argument_list[0]
  387. return protected_def
  388. elif cfg_ir.is_call(
  389. value, target_name=cfg_ir.MAYBE_GC_PROTECT_MACRO_NAME,
  390. calling_convention=cfg_ir.MACRO_POSITIONAL_CALLING_CONVENTION):
  391. _, protected_def = value.argument_list[1]
  392. return protected_def
  393. else:
  394. return None
  395. def_blocks = {}
  396. def __register_def_or_use(definition, block):
  397. if definition in def_blocks and def_blocks[definition] != block:
  398. # Definition seems to be used across basic blocks.
  399. ineligible_defs.add(definition)
  400. def_blocks[definition] = block
  401. ineligible_defs = set()
  402. def_protections = defaultdict(list)
  403. for block in cfg_ir.get_all_blocks(entry_point):
  404. no_gc_defs = set()
  405. block_defs = set()
  406. first_gc = {}
  407. last_def_uses = {}
  408. for i, definition in enumerate(block.definitions):
  409. if isinstance(definition.value, cfg_ir.Definition):
  410. # Handling definitions of definitions is complicated and they should already have
  411. # been expanded at this point. Just mark them as ineligible.
  412. ineligible_defs.add(definition)
  413. ineligible_defs.add(definition.value)
  414. continue
  415. protected_def = __get_protected_def(definition)
  416. if protected_def is not None:
  417. # We just ran into a gc_protect/maybe_gc_protect.
  418. def_protections[protected_def].append(definition)
  419. continue
  420. block_defs.add(definition)
  421. __register_def_or_use(definition, block)
  422. for dependency in definition.get_all_dependencies():
  423. __register_def_or_use(dependency, block)
  424. last_def_uses[dependency] = i
  425. if __may_cause_gc(definition):
  426. for gc_def in no_gc_defs:
  427. first_gc[gc_def] = i
  428. no_gc_defs = set()
  429. no_gc_defs.add(definition)
  430. # Mark all branch arguments as ineligible.
  431. for branch in block.flow.branches():
  432. ineligible_defs.update(branch.arguments)
  433. for dependency in block.flow.get_dependencies():
  434. last_def_uses[dependency] = None
  435. for definition in block_defs:
  436. if definition in ineligible_defs:
  437. # Definition was already ineligible.
  438. continue
  439. # Mark `definition` as ineligible if there is a GC definition in the range of
  440. # definitions (definition, last_def_uses[definition]].
  441. if definition in first_gc:
  442. if definition in last_def_uses:
  443. last_use = last_def_uses[definition]
  444. if last_use is None or first_gc[definition] <= last_use:
  445. ineligible_defs.add(definition)
  446. # Elide all GC protections for definitions which are not in the `ineligible_defs` set.
  447. for protected, protections in def_protections.items():
  448. if protected not in ineligible_defs:
  449. for protect_def in protections:
  450. protect_def.redefine(cfg_ir.Literal(None))
  451. def optimize(entry_point, jit):
  452. """Optimizes the control-flow graph defined by the given entry point.
  453. A potentially altered entry point is returned."""
  454. optimize_graph_flow(entry_point)
  455. elide_local_checks(entry_point)
  456. optimize_graph_flow(entry_point)
  457. eliminate_trivial_phis(entry_point)
  458. entry_point = cfg_ssa_construction.construct_ssa_form(entry_point)
  459. if jit.direct_calls_allowed:
  460. optimize_calls(entry_point, jit)
  461. cfg_data_structures.optimize_data_structures(entry_point)
  462. expand_cfg_intrinsics(entry_point, jit)
  463. yield [("CALL_ARGS", [inline_constants, (entry_point,)])]
  464. optimize_reads(entry_point)
  465. simplify_values(entry_point)
  466. eliminate_unused_definitions(entry_point)
  467. optimize_graph_flow(entry_point)
  468. expand_indirect_definitions(entry_point)
  469. eliminate_unused_definitions(entry_point)
  470. merge_blocks(entry_point)
  471. protect_from_gc(entry_point)
  472. elide_gc_protects(entry_point)
  473. eliminate_unused_definitions(entry_point)
  474. raise primitive_functions.PrimitiveFinished(entry_point)