cfg_ir.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. """Defines control flow graph IR data structures."""
  2. from collections import defaultdict
  3. # Let's just agree to disagree on map vs list comprehensions, pylint.
  4. # pylint: disable=I0011,W0141
  5. class SharedCounter(object):
  6. """Defines a shared counter."""
  7. def __init__(self):
  8. self.index = 0
  9. def next_value(self):
  10. """Gets the next value for this counter."""
  11. result = self.index
  12. self.index += 1
  13. return result
  14. class BasicBlock(object):
  15. """Represents a basic block."""
  16. def __init__(self, counter):
  17. self.parameters = []
  18. self.definitions = []
  19. self.counter = counter
  20. self.index = counter.next_value()
  21. self.definition_counter = SharedCounter()
  22. self.flow = UnreachableFlow()
  23. def append_parameter(self, parameter):
  24. """Appends a parameter to this basic block."""
  25. result = self.create_definition(parameter)
  26. self.parameters.append(result)
  27. if len(self.definitions) > 0:
  28. self.renumber_definitions()
  29. return result
  30. def remove_parameter(self, parameter):
  31. """Removes the given parameter definition from this basic block."""
  32. return self.parameters.remove(parameter)
  33. def prepend_definition(self, value):
  34. """Defines the given value in this basic block."""
  35. result = self.create_definition(value)
  36. self.definitions.insert(0, result)
  37. self.renumber_definitions()
  38. return result
  39. def insert_definition_before(self, anchor, value):
  40. """Inserts the second definition or value before the first definition."""
  41. index = None
  42. for i, definition in enumerate(self.definitions):
  43. if definition.definition_index == anchor.definition_index:
  44. index = i
  45. if index is None:
  46. raise ValueError(
  47. 'Cannot insert a definition because the anchor '
  48. 'is not defined in this block.')
  49. result = self.create_definition(value)
  50. self.definitions.insert(index, result)
  51. self.renumber_definitions()
  52. return result
  53. def append_definition(self, value):
  54. """Defines the given value in this basic block."""
  55. result = self.create_definition(value)
  56. self.definitions.append(result)
  57. return result
  58. def create_definition(self, value=None):
  59. """Creates a definition, but does not assign it to this block yet."""
  60. if isinstance(value, Definition):
  61. value.block = self
  62. value.renumber(self.definition_counter.next_value())
  63. return value
  64. else:
  65. assert isinstance(value, Value) or value is None
  66. return Definition(
  67. self.counter.next_value(),
  68. self,
  69. self.definition_counter.next_value(),
  70. value)
  71. def remove_definition(self, definition):
  72. """Removes the given definition from this basic block."""
  73. return self.definitions.remove(definition)
  74. def renumber_definitions(self):
  75. """Re-numbers all definitions in this basic block."""
  76. self.definition_counter = SharedCounter()
  77. for definition in self.parameters:
  78. definition.renumber(self.definition_counter.next_value())
  79. for definition in self.definitions:
  80. definition.renumber(self.definition_counter.next_value())
  81. def __str__(self):
  82. prefix = '!%d(%s):' % (self.index, ', '.join(map(str, self.parameters)))
  83. return '\n'.join(
  84. [prefix] +
  85. [' ' * 4 + str(item) for item in self.definitions + [self.flow]])
  86. class Definition(object):
  87. """Maps a value to a variable."""
  88. def __init__(self, index, block, definition_index, value):
  89. self.index = index
  90. self.block = block
  91. self.definition_index = definition_index
  92. self.value = value
  93. if value is not None:
  94. assert isinstance(value, Value) or isinstance(value, Definition)
  95. def redefine(self, new_value):
  96. """Tweaks this definition to take on the given new value."""
  97. self.value = new_value
  98. if new_value is not None:
  99. assert isinstance(new_value, Value) or isinstance(new_value, Definition)
  100. def renumber(self, new_definition_index):
  101. """Updates this definition's index in the block that defines it."""
  102. self.definition_index = new_definition_index
  103. def get_all_dependencies(self):
  104. """Gets all definitions and instructions on which this definition depends,
  105. along with any dependencies of instruction dependencies."""
  106. if isinstance(self.value, Definition):
  107. return [self.value]
  108. else:
  109. return self.value.get_all_dependencies()
  110. def has_side_effects(self):
  111. """Tests if this definition produces any side-effects."""
  112. return self.value.has_side_effects()
  113. def has_value(self):
  114. """Tells if this definition produces a result that is not None."""
  115. return self.value.has_value()
  116. def insert_before(self, value):
  117. """Inserts the given value or definition before this definition."""
  118. return self.block.insert_definition_before(self, value)
  119. def ref_str(self):
  120. """Gets a string that represents a reference to this definition."""
  121. return '$%d' % self.index
  122. def __str__(self):
  123. return '$%d = %s' % (self.index, self.value.ref_str())
  124. class Instruction(object):
  125. """Represents an instruction."""
  126. def get_dependencies(self):
  127. """Gets all definitions and instructions on which this instruction depends."""
  128. raise NotImplementedError()
  129. def get_all_dependencies(self):
  130. """Gets all definitions and instructions on which this instruction depends,
  131. along with any dependencies of instruction dependencies."""
  132. results = list(self.get_dependencies())
  133. for item in results:
  134. if not isinstance(item, Definition):
  135. results.extend(item.get_all_dependencies())
  136. return results
  137. class Branch(Instruction):
  138. """Represents a branch from one basic block to another."""
  139. def __init__(self, block, arguments=None):
  140. self.block = block
  141. assert isinstance(block, BasicBlock)
  142. if arguments is None:
  143. arguments = []
  144. self.arguments = arguments
  145. assert all([isinstance(arg, Definition) for arg in arguments])
  146. def get_dependencies(self):
  147. """Gets all definitions and instructions on which this instruction depends."""
  148. return self.arguments
  149. def __str__(self):
  150. return '!%d(%s)' % (self.block.index, ', '.join([arg.ref_str() for arg in self.arguments]))
  151. class FlowInstruction(Instruction):
  152. """Represents a control flow instruction which terminates a basic block."""
  153. def branches(self):
  154. """Gets a list of basic blocks targeted by this flow instruction."""
  155. raise NotImplementedError()
  156. def has_side_effects(self):
  157. """Tells if this instruction has side-effects."""
  158. # All flow-instructions have side-effects!
  159. return True
  160. class JumpFlow(FlowInstruction):
  161. """Represents a control flow instruction which jumps directly to a basic block."""
  162. def __init__(self, branch):
  163. FlowInstruction.__init__(self)
  164. self.branch = branch
  165. assert isinstance(branch, Branch)
  166. def get_dependencies(self):
  167. """Gets all definitions and instructions on which this instruction depends."""
  168. return self.branches()
  169. def branches(self):
  170. """Gets a list of basic blocks targeted by this flow instruction."""
  171. return [self.branch]
  172. def __str__(self):
  173. return 'jump %s' % self.branch
  174. class SelectFlow(FlowInstruction):
  175. """Represents a control flow instruction which jumps to one of two basic blocks depending
  176. on whether a condition is truthy or not."""
  177. def __init__(self, condition, if_branch, else_branch):
  178. FlowInstruction.__init__(self)
  179. self.condition = condition
  180. assert isinstance(condition, Definition)
  181. self.if_branch = if_branch
  182. assert isinstance(if_branch, Branch)
  183. self.else_branch = else_branch
  184. assert isinstance(else_branch, Branch)
  185. def get_dependencies(self):
  186. """Gets all definitions and instructions on which this instruction depends."""
  187. return [self.condition] + self.branches()
  188. def branches(self):
  189. """Gets a list of basic blocks targeted by this flow instruction."""
  190. return [self.if_branch, self.else_branch]
  191. def __str__(self):
  192. return 'select %s, %s, %s' % (self.condition.ref_str(), self.if_branch, self.else_branch)
  193. class ReturnFlow(FlowInstruction):
  194. """Represents a control flow instruction which terminates the execution of the current
  195. function and returns a value."""
  196. def __init__(self, value):
  197. FlowInstruction.__init__(self)
  198. self.value = value
  199. assert isinstance(value, Definition)
  200. def get_dependencies(self):
  201. """Gets all definitions and instructions on which this instruction depends."""
  202. return [self.value]
  203. def branches(self):
  204. """Gets a list of basic blocks targeted by this flow instruction."""
  205. return []
  206. def __str__(self):
  207. return 'return %s' % self.value.ref_str()
  208. class ThrowFlow(FlowInstruction):
  209. """Represents a control flow instruction which throws an exception."""
  210. def __init__(self, exception):
  211. FlowInstruction.__init__(self)
  212. self.exception = exception
  213. assert isinstance(exception, Definition)
  214. def get_dependencies(self):
  215. """Gets all definitions and instructions on which this instruction depends."""
  216. return [self.exception]
  217. def branches(self):
  218. """Gets a list of basic blocks targeted by this flow instruction."""
  219. return []
  220. def __str__(self):
  221. return 'throw %s' % self.exception.ref_str()
  222. class UnreachableFlow(FlowInstruction):
  223. """Represents a control flow instruction which is unreachable."""
  224. def get_dependencies(self):
  225. """Gets all definitions and instructions on which this instruction depends."""
  226. return []
  227. def branches(self):
  228. """Gets a list of basic blocks targeted by this flow instruction."""
  229. return []
  230. def __str__(self):
  231. return 'unreachable'
  232. class Value(Instruction):
  233. """A value: an instruction that produces some result."""
  234. def get_dependencies(self):
  235. """Gets all definitions and instructions on which this instruction depends."""
  236. raise NotImplementedError()
  237. def has_value(self):
  238. """Tells if this value produces a result that is not None."""
  239. return True
  240. def has_side_effects(self):
  241. """Tells if this instruction has side-effects."""
  242. return False
  243. def ref_str(self):
  244. """Gets a string that represents this value."""
  245. return str(self)
  246. class BlockParameter(Value):
  247. """A basic block parameter."""
  248. def get_dependencies(self):
  249. """Gets all definitions and instructions on which this instruction depends."""
  250. return []
  251. def __str__(self):
  252. return 'block-parameter'
  253. class FunctionParameter(Value):
  254. """A function parameter."""
  255. def __init__(self, name):
  256. Value.__init__(self)
  257. self.name = name
  258. def get_dependencies(self):
  259. """Gets all definitions and instructions on which this instruction depends."""
  260. return []
  261. def __str__(self):
  262. return 'func-parameter %s' % self.name
  263. class Literal(Value):
  264. """A literal value."""
  265. def __init__(self, literal):
  266. Value.__init__(self)
  267. self.literal = literal
  268. def get_dependencies(self):
  269. """Gets all definitions and instructions on which this instruction depends."""
  270. return []
  271. def has_value(self):
  272. """Tells if this value produces a result that is not None."""
  273. return self.literal is not None
  274. def __str__(self):
  275. return 'literal %r' % self.literal
  276. class IndirectFunctionCall(Value):
  277. """A value that is the result of an indirect function call."""
  278. def __init__(self, target, argument_list):
  279. Value.__init__(self)
  280. assert isinstance(target, Definition)
  281. self.target = target
  282. assert all([isinstance(val, Definition) for _, val in argument_list])
  283. self.argument_list = argument_list
  284. def has_side_effects(self):
  285. """Tells if this instruction has side-effects."""
  286. return True
  287. def get_dependencies(self):
  288. """Gets all definitions and instructions on which this instruction depends."""
  289. return [self.target] + [val for _, val in self.argument_list]
  290. def __str__(self):
  291. return 'indirect-call %s(%s)' % (
  292. self.target.ref_str(),
  293. ', '.join(['%s=%s' % (key, val.ref_str()) for key, val in self.argument_list]))
  294. SIMPLE_POSITIONAL_CALLING_CONVENTION = 'simple-positional'
  295. """The calling convention for functions that use 'return' statements to return.
  296. Arguments are matched to parameters based on position."""
  297. JIT_CALLING_CONVENTION = 'jit'
  298. """The calling convention for jitted functions."""
  299. class DirectFunctionCall(Value):
  300. """A value that is the result of a direct function call."""
  301. def __init__(self, target_name, argument_list, calling_convention=JIT_CALLING_CONVENTION):
  302. Value.__init__(self)
  303. self.target_name = target_name
  304. assert all([isinstance(val, Definition) for _, val in argument_list])
  305. self.argument_list = argument_list
  306. self.calling_convention = calling_convention
  307. def has_side_effects(self):
  308. """Tells if this instruction has side-effects."""
  309. return True
  310. def get_dependencies(self):
  311. """Gets all definitions and instructions on which this instruction depends."""
  312. return [val for _, val in self.argument_list]
  313. def __str__(self):
  314. return 'direct-call %r %s(%s)' % (
  315. self.calling_convention,
  316. self.target_name,
  317. ', '.join(['%s=%s' % (key, val.ref_str()) for key, val in self.argument_list]))
  318. class AllocateRootNode(Value):
  319. """A value that produces a new root node. Typically used in function prologs."""
  320. def __init__(self):
  321. Value.__init__(self)
  322. def get_dependencies(self):
  323. """Gets all definitions and instructions on which this instruction depends."""
  324. return []
  325. def __str__(self):
  326. return 'alloc-root-node'
  327. class DeallocateRootNode(Value):
  328. """A value that deallocates a root node. Typically used in function epilogs."""
  329. def __init__(self, root_node):
  330. Value.__init__(self)
  331. assert isinstance(root_node, Definition)
  332. self.root_node = root_node
  333. def get_dependencies(self):
  334. """Gets all definitions and instructions on which this instruction depends."""
  335. return [self.root_node]
  336. def has_value(self):
  337. """Tells if this value produces a result that is not None."""
  338. return False
  339. def has_side_effects(self):
  340. """Tells if this instruction has side-effects."""
  341. return True
  342. def __str__(self):
  343. return 'free-root-node %s' % self.root_node.ref_str()
  344. class DeclareLocal(Value):
  345. """A value that declares a local variable."""
  346. def __init__(self, variable, root_node):
  347. Value.__init__(self)
  348. self.variable = variable
  349. self.root_node = root_node
  350. def get_dependencies(self):
  351. """Gets all definitions and instructions on which this instruction depends."""
  352. return [self.root_node]
  353. def has_value(self):
  354. """Tells if this value produces a result that is not None."""
  355. return False
  356. def has_side_effects(self):
  357. """Tells if this instruction has side-effects."""
  358. return True
  359. def __str__(self):
  360. return 'declare-local %s, %s' % (self.variable, self.root_node.ref_str())
  361. class DeclareGlobal(Value):
  362. """A value that declares a global variable."""
  363. def __init__(self, variable):
  364. Value.__init__(self)
  365. self.variable = variable
  366. def get_dependencies(self):
  367. """Gets all definitions and instructions on which this instruction depends."""
  368. return []
  369. def has_value(self):
  370. """Tells if this value produces a result that is not None."""
  371. return False
  372. def has_side_effects(self):
  373. """Tells if this instruction has side-effects."""
  374. return True
  375. def __str__(self):
  376. return 'declare-global %s' % self.variable.name
  377. class CheckLocalExists(Value):
  378. """A value that checks if a local value has been defined (yet)."""
  379. def __init__(self, variable):
  380. Value.__init__(self)
  381. self.variable = variable
  382. def get_dependencies(self):
  383. """Gets all definitions and instructions on which this instruction depends."""
  384. return []
  385. def __str__(self):
  386. return 'check-local-exists %s' % self.variable
  387. class ResolveLocal(Value):
  388. """A value that resolves a local as a pointer."""
  389. def __init__(self, variable):
  390. Value.__init__(self)
  391. self.variable = variable
  392. def get_dependencies(self):
  393. """Gets all definitions and instructions on which this instruction depends."""
  394. return []
  395. def __str__(self):
  396. return 'resolve-local %s' % self.variable
  397. class ResolveGlobal(Value):
  398. """A value that resolves a global as a pointer."""
  399. def __init__(self, variable):
  400. Value.__init__(self)
  401. self.variable = variable
  402. def get_dependencies(self):
  403. """Gets all definitions and instructions on which this instruction depends."""
  404. return []
  405. def __str__(self):
  406. return 'resolve-global %s' % self.variable.name
  407. class LoadPointer(Value):
  408. """A value that loads the value assigned to a pointer."""
  409. def __init__(self, pointer):
  410. Value.__init__(self)
  411. self.pointer = pointer
  412. assert isinstance(pointer, Definition)
  413. def get_dependencies(self):
  414. """Gets all definitions and instructions on which this instruction depends."""
  415. return [self.pointer]
  416. def __str__(self):
  417. return 'load %s' % self.pointer.ref_str()
  418. class StoreAtPointer(Value):
  419. """A value that assigns a value to a pointer."""
  420. def __init__(self, pointer, value):
  421. Value.__init__(self)
  422. self.pointer = pointer
  423. assert isinstance(pointer, Definition)
  424. self.value = value
  425. assert isinstance(value, Definition)
  426. def get_dependencies(self):
  427. """Gets all definitions and instructions on which this instruction depends."""
  428. return [self.pointer, self.value]
  429. def has_value(self):
  430. """Tells if this value produces a result that is not None."""
  431. return False
  432. def has_side_effects(self):
  433. """Tells if this instruction has side-effects."""
  434. return True
  435. def __str__(self):
  436. return 'store %s, %s' % (self.pointer.ref_str(), self.value.ref_str())
  437. class Read(Value):
  438. """A value that reads the value stored in a node."""
  439. def __init__(self, node):
  440. Value.__init__(self)
  441. self.node = node
  442. assert isinstance(node, Definition)
  443. def get_dependencies(self):
  444. """Gets all definitions and instructions on which this instruction depends."""
  445. return [self.node]
  446. def __str__(self):
  447. return 'read %s' % (self.node.ref_str())
  448. class CreateNode(Value):
  449. """A value that creates a new node."""
  450. def __init__(self, value):
  451. Value.__init__(self)
  452. self.value = value
  453. assert isinstance(value, Definition)
  454. def get_dependencies(self):
  455. """Gets all definitions and instructions on which this instruction depends."""
  456. return [self.value]
  457. def __str__(self):
  458. return 'create-node %s' % (self.value.ref_str())
  459. class Input(Value):
  460. """A value that pops a node from the input queue."""
  461. def get_dependencies(self):
  462. """Gets all definitions and instructions on which this instruction depends."""
  463. return []
  464. def has_side_effects(self):
  465. """Tells if this instruction has side-effects."""
  466. return True
  467. def __str__(self):
  468. return 'input'
  469. class Output(Value):
  470. """A value that pushes a node onto the output queue."""
  471. def __init__(self, value):
  472. Value.__init__(self)
  473. self.value = value
  474. assert isinstance(value, Definition)
  475. def get_dependencies(self):
  476. """Gets all definitions and instructions on which this instruction depends."""
  477. return [self.value]
  478. def has_value(self):
  479. """Tells if this value produces a result that is not None."""
  480. return False
  481. def has_side_effects(self):
  482. """Tells if this instruction has side-effects."""
  483. return True
  484. def __str__(self):
  485. return 'output %s' % self.value.ref_str()
  486. class Binary(Value):
  487. """A value that applies a binary operator to two other values."""
  488. def __init__(self, lhs, operator, rhs):
  489. Value.__init__(self)
  490. self.lhs = lhs
  491. assert isinstance(lhs, Definition)
  492. self.operator = operator
  493. self.rhs = rhs
  494. assert isinstance(rhs, Definition)
  495. def get_dependencies(self):
  496. """Gets all definitions and instructions on which this instruction depends."""
  497. return [self.lhs, self.rhs]
  498. def __str__(self):
  499. return 'binary %s, %r, %s' % (self.lhs.ref_str(), self.operator, self.rhs.ref_str())
  500. def create_jump(block, arguments=None):
  501. """Creates a jump to the given block with the given argument list."""
  502. return JumpFlow(Branch(block, arguments))
  503. def get_def_value(def_or_value):
  504. """Returns the given value, or the underlying value of the given definition, whichever is
  505. appropriate."""
  506. if isinstance(def_or_value, Definition):
  507. return get_def_value(def_or_value.value)
  508. else:
  509. return def_or_value
  510. def apply_to_value(function, def_or_value):
  511. """Applies the given function to the specified value, or the underlying value of the
  512. given definition."""
  513. return function(get_def_value(def_or_value))
  514. def is_literal(value):
  515. """Tests if the given value is a literal."""
  516. return isinstance(value, Literal)
  517. def is_literal_def(def_or_value):
  518. """Tests if the given value is a literal or a definition with an underlying literal."""
  519. return apply_to_value(is_literal, def_or_value)
  520. def is_value_def(def_or_value, class_or_type_or_tuple=Value):
  521. """Tests if the given definition or value is a value of the given type."""
  522. return isinstance(get_def_value(def_or_value), class_or_type_or_tuple)
  523. def get_def_variable(def_or_value):
  524. """Gets the 'variable' attribute of the given value, or the underlying value of the given
  525. definition, whichever is appropriate."""
  526. return get_def_value(def_or_value).variable
  527. def get_literal_value(value):
  528. """Gets the value of the given literal value."""
  529. return value.literal
  530. def get_literal_def_value(def_or_value):
  531. """Gets the value of the given literal value or definition with an underlying literal."""
  532. return apply_to_value(get_literal_value, def_or_value)
  533. def get_all_predecessor_blocks(entry_point):
  534. """Creates a mapping of blocks to their direct predecessors for every block in the control-flow
  535. graph defined by the given entry point."""
  536. results = defaultdict(set)
  537. processed = set()
  538. def __find_predecessors_step(block):
  539. if block in processed:
  540. return
  541. processed.add(block)
  542. for branch in block.flow.branches():
  543. target_block = branch.block
  544. results[target_block].add(block)
  545. __find_predecessors_step(target_block)
  546. __find_predecessors_step(entry_point)
  547. return results
  548. def get_directly_reachable_blocks(block):
  549. """Gets the set of all blocks that can be reached by taking a single branch from the
  550. given block."""
  551. return [branch.block for branch in block.flow.branches()]
  552. def get_reachable_blocks(entry_point):
  553. """Constructs the set of all reachable vertices from the given block."""
  554. # This is a simple O(n^2) algorithm. Maybe a faster algorithm is more appropriate here.
  555. def __add_block_children(block, results):
  556. for child in get_directly_reachable_blocks(block):
  557. if child not in results:
  558. results.add(child)
  559. __add_block_children(child, results)
  560. return results
  561. return __add_block_children(entry_point, set())
  562. def get_all_reachable_blocks(entry_point):
  563. """Constructs the set of all reachable vertices, for every block that is
  564. reachable from the given entry point."""
  565. # This is a simple O(n^3) algorithm. Maybe a faster algorithm is more appropriate here.
  566. results = {}
  567. all_blocks = get_reachable_blocks(entry_point)
  568. results[entry_point] = all_blocks
  569. for block in all_blocks:
  570. if block not in results:
  571. results[block] = get_reachable_blocks(block)
  572. return results
  573. def get_all_blocks(entry_point):
  574. """Gets all basic blocks in the control-flow graph defined by the given entry point."""
  575. yield entry_point
  576. for block in get_reachable_blocks(entry_point):
  577. yield block