Debian Codegraph @

rpython/jit/metainterp/optimizeopt/guard.py

raw symbols SCIP
1 """
2 NOTE this strengthing optimization is only used in the vecopt.
3 It needs also the information about integral modifications
4 gathered with IntegralForwardModification
5 """
6
7 from rpython.jit.metainterp.optimizeopt.renamer import Renamer
8 from rpython.jit.metainterp.optimizeopt.dependency import (DependencyGraph,
9 MemoryRef, Node, IndexVar)
10 from rpython.jit.metainterp.resoperation import (rop, ResOperation, GuardResOp)
11 from rpython.jit.metainterp.history import (ConstInt, ConstFloat, Const,
12 AbstractFailDescr)
13 from rpython.jit.metainterp.compile import ResumeGuardDescr, CompileLoopVersionDescr
14 from rpython.rlib.objectmodel import we_are_translated
15
16 class Guard(object):
17 """ An object wrapper around a guard. Helps to determine
18 if one guard implies another
19 """
20 _attrs_ = ('index', 'op', 'cmp_op', 'rhs', 'lhs')
21
22 def __init__(self, index, op, cmp_op, index_vars):
23 self.index = index
24 self.op = op
25 self.cmp_op = cmp_op
26 lhs = cmp_op.getarg(0)
27 self.lhs = index_vars.get(lhs, None)
28 if self.lhs is None:
29 self.lhs = IndexVar(lhs)
30 #
31 rhs = cmp_op.getarg(1)
32 self.rhs = index_vars.get(rhs, None)
33 if self.rhs is None:
34 self.rhs = IndexVar(rhs)
35
36 def setindex(self, index):
37 self.index = index
38
39 def setoperation(self, op):
40 self.op = op
41
42 def setcmp(self, c):
43 self.cmp_op = c
44
45 def getleftkey(self):
46 return self.lhs.getvariable()
47
48 def getrightkey(self):
49 return self.rhs.getvariable()
50
51 def implies(self, guard, opt=None):
52 if self.op.getopnum() != guard.op.getopnum():
53 return False
54
55 if self.getleftkey() is guard.getleftkey():
56 # same operation
57 valid, lc = self.lhs.compare(guard.lhs)
58 if not valid: return False
59 valid, rc = self.rhs.compare(guard.rhs)
60 if not valid: return False
61 opnum = self.get_compare_opnum()
62 if opnum == -1:
63 return False
64 # x < y = -1,-2,...
65 # x == y = 0
66 # x > y = 1,2,...
67 if opnum in (rop.INT_LE, rop.INT_LT):
68 return (lc >= 0 and rc <= 0)
69 if opnum in (rop.INT_GE, rop.INT_GT):
70 return (lc <= 0 and rc >= 0)
71 return False
72
73 def transitive_imply(self, other, opt, loop):
74 if self.op.getopnum() != other.op.getopnum():
75 # stronger restriction, intermixing e.g. <= and < would be possible
76 return None
77 if self.getleftkey() is not other.getleftkey():
78 return None
79 if not self.rhs.is_identity():
80 # stronger restriction
81 return None
82 # this is a valid transitive guard that eliminates the loop guard
83 opnum = self.transitive_cmpop(self.cmp_op.getopnum())
84 box_rhs = self.emit_varops(opt, self.rhs, self.cmp_op.getarg(1))
85 other_rhs = self.emit_varops(opt, other.rhs, other.cmp_op.getarg(1))
86 compare = ResOperation(opnum, [box_rhs, other_rhs])
87 opt.emit_operation(compare)
88 # guard
89 descr = CompileLoopVersionDescr()
90 descr.copy_all_attributes_from(self.op.getdescr())
91 descr.rd_vector_info = None # do not copy the accum list
92 assert isinstance(descr, AbstractFailDescr)
93 guard = ResOperation(self.op.getopnum(), [compare], descr=descr)
94 guard.setfailargs(loop.label.getarglist_copy())
95 opt.emit_operation(guard)
96
97 return guard
98
99 def transitive_cmpop(self, opnum):
100 if opnum == rop.INT_LT:
101 return rop.INT_LE
102 if opnum == rop.INT_GT:
103 return rop.INT_GE
104 return opnum
105
106 def get_compare_opnum(self):
107 opnum = self.op.getopnum()
108 if opnum == rop.GUARD_TRUE:
109 return self.cmp_op.getopnum()
110 else:
111 return self.cmp_op.boolinverse
112
113 def inhert_attributes(self, other):
114 myop = self.op
115 otherop = other.op
116 assert isinstance(otherop, GuardResOp)
117 assert isinstance(myop, GuardResOp)
118 self.index = other.index
119
120 descr = myop.getdescr()
121 descr.copy_all_attributes_from(other.op.getdescr())
122 # TODO myop.rd_frame_info_list = otherop.rd_frame_info_list
123 myop.setfailargs(otherop.getfailargs()[:])
124 # TODO myop.rd_snapshot = otherop.rd_snapshot
125
126 def emit_varops(self, opt, var, old_arg):
127 assert isinstance(var, IndexVar)
128 if var.is_identity():
129 return var.var
130 box = var.emit_operations(opt)
131 opt.renamer.start_renaming(old_arg, box)
132 return box
133
134 def emit_operations(self, opt):
135 # create trace instructions for the index
136 lhs = self.emit_varops(opt, self.lhs, self.cmp_op.getarg(0))
137 rhs = self.emit_varops(opt, self.rhs, self.cmp_op.getarg(1))
138 opnum = self.cmp_op.getopnum()
139 cmp_op = ResOperation(opnum, [lhs, rhs])
140 opt.emit_operation(cmp_op)
141 # emit that actual guard
142 guard = ResOperation(self.op.getopnum(), [cmp_op], self.op.getdescr())
143 guard.setfailargs(self.op.getfailargs()[:])
144 opt.emit_operation(guard)
145 self.setindex(opt.operation_position()-1)
146 self.setoperation(guard)
147 self.setcmp(cmp_op)
148
149 def set_to_none(self, info, loop):
150 operations = loop.operations
151 assert operations[self.index] is self.op
152 operations[self.index] = None
153 descr = self.op.getdescr()
154 if operations[self.index-1] is self.cmp_op:
155 operations[self.index-1] = None
156
157 @staticmethod
158 def of(boolarg, operations, index, index_vars):
159 guard_op = operations[index]
160 cmp_op = guard_op.getarg(0)
161 if not (rop.INT_LT <= cmp_op.getopnum() <= rop.INT_GE):
162 return None
163 return Guard(index, guard_op, cmp_op, index_vars)
164
165 class GuardStrengthenOpt(object):
166 """ Note that this optimization is only used in the vector optimizer (yet) """
167 def __init__(self, index_vars):
168 self.index_vars = index_vars
169 self._newoperations = []
170 self.strength_reduced = 0 # how many guards could be removed?
171 self.strongest_guards = {}
172 self.guards = {}
173 self.delayed = {}
174
175 def collect_guard_information(self, loop):
176 operations = loop.operations
177 last_guard = None
178 for i,op in enumerate(operations):
179 op = operations[i]
180 if not op.is_guard():
181 continue
182 if op.getopnum() in (rop.GUARD_TRUE, rop.GUARD_FALSE):
183 guard = Guard.of(op.getarg(0), operations, i, self.index_vars)
184 if guard is None:
185 continue
186 self.record_guard(guard.getleftkey(), guard)
187 self.record_guard(guard.getrightkey(), guard)
188
189 def record_guard(self, key, guard):
190 if key is None:
191 return
192 # the operations are processed from 1..n (forward),
193 # thus if the key is not present (1), the guard is saved
194 # (2) guard(s) with this key is/are already present,
195 # thus each of is seen as possible candidate to strengthen
196 # or imply the current. in both cases the current guard is
197 # not emitted and the original is replaced with the current
198 others = self.strongest_guards.setdefault(key, [])
199 if len(others) > 0: # (2)
200 replaced = False
201 for i,other in enumerate(others):
202 assert guard is not other
203 if guard.implies(other, self):
204 # strengthend
205 others[i] = guard
206 self.guards[guard.index] = None # mark as 'do not emit'
207 guard.inhert_attributes(other)
208 self.guards[other.index] = guard
209 replaced = True
210 continue
211 elif other.implies(guard, self):
212 # implied
213 self.guards[guard.index] = None # mark as 'do not emit'
214 replaced = True
215 continue
216 if not replaced:
217 others.append(guard)
218 else: # (2)
219 others.append(guard)
220
221 def eliminate_guards(self, loop):
222 self.renamer = Renamer()
223 for i,op in enumerate(loop.operations):
224 op = loop.operations[i]
225 if op.is_guard():
226 if i in self.guards:
227 # either a stronger guard has been saved
228 # or it should not be emitted
229 guard = self.guards[i]
230 # this guard is implied or marked as not emitted (= None)
231 self.strength_reduced += 1
232 if guard is None:
233 continue
234 guard.emit_operations(self)
235 continue
236 else:
237 self.emit_operation(op)
238 continue
239 if not op.returns_void():
240 index_var = self.index_vars.get(op, None)
241 if index_var:
242 if not index_var.is_identity():
243 var = index_var.emit_operations(self, op)
244 self.renamer.start_renaming(op, var)
245 continue
246 self.emit_operation(op)
247 self.renamer.rename(loop.jump)
248 #
249 loop.operations = self._newoperations[:]
250
251 def propagate_all_forward(self, info, loop, user_code=False):
252 """ strengthens the guards that protect an integral value """
253 # the guards are ordered. guards[i] is before guards[j] iff i < j
254 self.collect_guard_information(loop)
255 self.eliminate_guards(loop)
256 #
257 assert len(info.versions) == 1
258 version = info.versions[0]
259
260 for i,op in enumerate(loop.operations):
261 if not op.is_guard():
262 continue
263 descr = op.getdescr()
264 if descr and descr.loop_version():
265 assert isinstance(descr, AbstractFailDescr)
266 info.track(op, descr, version)
267
268 if user_code:
269 self.eliminate_array_bound_checks(info, loop)
270
271
272 def emit_operation(self, op):
273 self.renamer.rename(op)
274 self._newoperations.append(op)
275
276 def operation_position(self):
277 return len(self._newoperations)
278
279 def eliminate_array_bound_checks(self, info, loop):
280 info.mark()
281 version = None
282 self._newoperations = []
283 for key, guards in self.strongest_guards.items():
284 if len(guards) <= 1:
285 continue
286 # there is more than one guard for that key,
287 # that is why we could imply the guards 2..n
288 # iff we add invariant guards
289 one = guards[0]
290 for other in guards[1:]:
291 transitive_guard = one.transitive_imply(other, self, loop)
292 if transitive_guard:
293 if version is None:
294 version = info.snapshot(loop)
295 info.remove(other.op.getdescr())
296 other.set_to_none(info, loop)
297 descr = transitive_guard.getdescr()
298 assert isinstance(descr, AbstractFailDescr)
299 info.track(transitive_guard, descr, version)
300 info.clear()
301
302 loop.prefix = self._newoperations + loop.prefix
303 loop.operations = [op for op in loop.operations if op]