Debian Codegraph @

rpython/translator/backendopt/merge_if_blocks.py

raw symbols SCIP
1 from rpython.flowspace.model import Constant, Variable, mkentrymap
2 from rpython.tool.ansi_print import AnsiLogger
3
4 log = AnsiLogger("backendopt")
5
6
7 def is_chain_block(block, first=False):
8 if len(block.operations) == 0:
9 return False
10 if len(block.operations) > 1 and not first:
11 return False
12 op = block.operations[-1]
13 if (op.opname not in ('int_eq', 'uint_eq', 'char_eq', 'unichar_eq')
14 # note: 'llong_eq', 'ullong_eq' have been removed, as it's not
15 # strictly C-compliant to do a switch() on a long long. It also
16 # crashes the JIT, and it's very very rare anyway.
17 or op.result != block.exitswitch):
18 return False
19 if isinstance(op.args[0], Variable) and isinstance(op.args[1], Variable):
20 return False
21 if isinstance(op.args[0], Constant) and isinstance(op.args[1], Constant):
22 return False
23 # check that the constant is hashable (ie not a symbolic)
24 try:
25 if isinstance(op.args[0], Constant):
26 hash(op.args[0].value)
27 else:
28 hash(op.args[1].value)
29 except TypeError:
30 return False
31 return True
32
33 def merge_chain(chain, checkvar, varmap, graph):
34 def get_new_arg(var_or_const):
35 if isinstance(var_or_const, Constant):
36 return var_or_const
37 return varmap[var_or_const]
38 firstblock, case = chain[0]
39 firstblock.operations = firstblock.operations[:-1]
40 firstblock.exitswitch = checkvar
41 values = {}
42 links = []
43 default = chain[-1][0].exits[0]
44 default.exitcase = "default"
45 default.llexitcase = None
46 default.args = [get_new_arg(arg) for arg in default.args]
47 for block, case in chain:
48 if case.value in values:
49 # - ignore silently: it occurs in platform-dependent
50 # chains of tests, for example
51 #log.WARNING("unreachable code with value %r in graph %s" % (
52 # case.value, graph))
53 continue
54 values[case.value] = True
55 link = block.exits[1]
56 links.append(link)
57 link.exitcase = case.value
58 link.llexitcase = case.value
59 link.args = [get_new_arg(arg) for arg in link.args]
60 links.append(default)
61 firstblock.recloseblock(*links)
62
63 def merge_if_blocks_once(graph):
64 """Convert consecutive blocks that all compare a variable (of Primitive type)
65 with a constant into one block with multiple exits. The backends can in
66 turn output this block as a switch statement.
67 """
68 candidates = [block for block in graph.iterblocks()
69 if is_chain_block(block, first=True)]
70 entrymap = mkentrymap(graph)
71 for firstblock in candidates:
72 chain = []
73 checkvars = []
74 varmap = {} # {var in a block in the chain: var in the first block}
75 for var in firstblock.exits[0].args:
76 varmap[var] = var
77 for var in firstblock.exits[1].args:
78 varmap[var] = var
79 def add_to_varmap(var, newvar):
80 if isinstance(var, Variable):
81 varmap[newvar] = varmap[var]
82 else:
83 varmap[newvar] = var
84 current = firstblock
85 while 1:
86 # check whether the chain can be extended with the block that follows the
87 # False link
88 checkvar = [var for var in current.operations[-1].args
89 if isinstance(var, Variable)][0]
90 resvar = current.operations[-1].result
91 case = [var for var in current.operations[-1].args
92 if isinstance(var, Constant)][0]
93 checkvars.append(checkvar)
94 falseexit = current.exits[0]
95 assert not falseexit.exitcase
96 trueexit = current.exits[1]
97 targetblock = falseexit.target
98 # if the result of the check is also passed through the link, we
99 # cannot construct the chain
100 if resvar in falseexit.args or resvar in trueexit.args:
101 break
102 chain.append((current, case))
103 if len(entrymap[targetblock]) != 1:
104 break
105 if checkvar not in falseexit.args:
106 break
107 newcheckvar = targetblock.inputargs[falseexit.args.index(checkvar)]
108 if not is_chain_block(targetblock):
109 break
110 if newcheckvar not in targetblock.operations[0].args:
111 break
112 for i, var in enumerate(trueexit.args):
113 add_to_varmap(var, trueexit.target.inputargs[i])
114 for i, var in enumerate(falseexit.args):
115 add_to_varmap(var, falseexit.target.inputargs[i])
116 current = targetblock
117 if len(chain) > 1:
118 break
119 else:
120 return False
121 merge_chain(chain, checkvars[0], varmap, graph)
122 return True
123
124 def merge_if_blocks(graph, verbose=True):
125 merge = False
126 while merge_if_blocks_once(graph):
127 merge = True
128 if merge:
129 if verbose:
130 log("merging blocks in %s" % (graph.name, ))
131 else:
132 log.dot()