Debian Codegraph @

rpython/jit/metainterp/optimizeopt/pure.py

raw symbols SCIP
1 from rpython.jit.metainterp.optimizeopt.optimizer import (
2 Optimization, OptimizationResult, REMOVED)
3 from rpython.jit.metainterp.resoperation import rop, OpHelpers, AbstractResOp,\
4 ResOperation, opclasses
5 from rpython.jit.metainterp.optimizeopt.util import (
6 make_dispatcher_method, have_dispatcher_method, get_box_replacement)
7 from rpython.jit.metainterp.optimizeopt.shortpreamble import PreambleOp
8 from rpython.jit.metainterp.optimize import SpeculativeError
9
10
11 class DefaultOptimizationResult(OptimizationResult):
12 def __init__(self, opt, op, save, nextop):
13 OptimizationResult.__init__(self, opt, op)
14 self.save = save
15 self.nextop = nextop
16
17 def callback(self):
18 self._callback(self.op, self.save, self.nextop)
19
20 def _callback(self, op, save, nextop):
21 if rop.returns_bool_result(op.opnum):
22 self.opt.getintbound(op).make_bool()
23 if save:
24 recentops = self.opt.getrecentops(op.getopnum())
25 recentops.add(op)
26 if nextop:
27 self.opt.emit_extra(nextop)
28
29
30 class CallPureOptimizationResult(OptimizationResult):
31 def callback(self):
32 self.opt.call_pure_positions.append(
33 len(self.opt.optimizer._newoperations) - 1)
34
35
36 class RecentPureOps(object):
37 def __init__(self, limit=16):
38 self.lst = [None] * limit
39 self.next_index = 0
40
41 def add(self, op):
42 assert isinstance(op, AbstractResOp)
43 next_index = self.next_index
44 new = next_index + 1
45 if new == len(self.lst):
46 new = 0
47 self.next_index = new
48 self.lst[next_index] = op
49
50 def force_preamble_op(self, opt, op, i):
51 if not isinstance(op, PreambleOp):
52 return op
53 op = opt.force_op_from_preamble(op)
54 self.lst[i] = op
55 return op
56
57 def lookup1(self, opt, box0, descr):
58 for i in range(len(self.lst)):
59 op = self.lst[i]
60 if op is None:
61 break
62 if box0.same_box(get_box_replacement(op.getarg(0))) and op.getdescr() is descr:
63 op = self.force_preamble_op(opt, op, i)
64 return op
65 return None
66
67 def lookup2(self, opt, box0, box1, descr, commutative=False):
68 for i in range(len(self.lst)):
69 op = self.lst[i]
70 if op is None:
71 break
72 oparg0 = get_box_replacement(op.getarg(0))
73 oparg1 = get_box_replacement(op.getarg(1))
74 if ((box0.same_box(oparg0) and box1.same_box(oparg1)) or
75 (commutative and box1.same_box(oparg0) and box0.same_box(oparg1))):
76 if op.getdescr() is descr:
77 op = self.force_preamble_op(opt, op, i)
78 return op
79 return None
80
81 def lookup(self, optimizer, op, commutative=False):
82 # must not do get_box_replacement, for the benefit of _can_reuse_oldop
83 numargs = op.numargs()
84 if numargs == 1:
85 return self.lookup1(optimizer,
86 get_box_replacement(op.getarg(0)),
87 op.getdescr())
88 elif numargs == 2:
89 return self.lookup2(optimizer,
90 get_box_replacement(op.getarg(0)),
91 get_box_replacement(op.getarg(1)),
92 op.getdescr(),
93 commutative=commutative)
94 else:
95 assert False
96
97
98 class OptPure(Optimization):
99 def __init__(self):
100 self.postponed_op = None
101 self._pure_operations = [None] * (rop._ALWAYS_PURE_LAST -
102 rop._ALWAYS_PURE_FIRST)
103 self.call_pure_positions = []
104 self.extra_call_pure = []
105 self.known_result_call_pure = []
106
107 def propagate_forward(self, op):
108 return dispatch_opt(self, op)
109
110 def optimize_default(self, op):
111 canfold = rop.is_always_pure(op.opnum)
112 ovf = False
113 if rop.is_ovf(op.opnum):
114 self.postponed_op = op
115 return
116 if self.postponed_op:
117 nextop = op
118 op = self.postponed_op
119 self.postponed_op = None
120 canfold = nextop.getopnum() == rop.GUARD_NO_OVERFLOW
121 ovf = True
122 else:
123 nextop = None
124
125 save = False
126 if canfold:
127 for i in range(op.numargs()):
128 if self.get_constant_box(op.getarg(i)) is None:
129 break
130 else:
131 # all constant arguments: constant-fold away
132 resbox = self.optimizer.constant_fold(op)
133 # note that INT_xxx_OVF is not done from here, and the
134 # overflows in the INT_xxx operations are ignored
135 self.optimizer.make_constant(op, resbox)
136 return
137
138 # did we do the exact same operation already?
139 recentops = self.getrecentops(op.getopnum(), create=False)
140 save = True
141 if recentops is not None:
142 opnum = op.opnum
143 commutative = (opnum == rop.INT_ADD or
144 opnum == rop.INT_ADD_OVF or
145 opnum == rop.INT_MUL or
146 opnum == rop.INT_MUL_OVF or
147 opnum == rop.INT_AND or
148 opnum == rop.INT_OR or
149 opnum == rop.INT_XOR)
150 oldop = recentops.lookup(self.optimizer, op, commutative=commutative)
151 if oldop is not None and self._can_reuse_oldop(
152 recentops, oldop, op, ovf):
153 self.optimizer.make_equal_to(op, get_box_replacement(oldop))
154 return
155
156 # otherwise, the operation remains
157 if nextop is None and not save and not rop.returns_bool_result(op.getopnum()):
158 # for this case DefaultOptimizationResult would do nothing
159 return self.emit(op)
160 return self.emit_result(DefaultOptimizationResult(self, op, save, nextop))
161
162 def _can_reuse_oldop(self, recentops, oldop, op, ovf):
163 if ovf:
164 # careful! this is an ovf operation. so we can only
165 # re-use the result of a prior ovf operation, but not a
166 # regular int_... op, because that might have
167 # overflowed (the other direction is fine). therefore
168 # we need to check that the previous op and the current
169 # op have the same opnum.
170 return isinstance(oldop, AbstractResOp) and oldop.opnum == op.opnum
171 return True
172
173 def getrecentops(self, opnum, create=True):
174 if rop._OVF_FIRST <= opnum <= rop._OVF_LAST:
175 opnum = opnum - rop._OVF_FIRST
176 else:
177 opnum = opnum - rop._ALWAYS_PURE_FIRST
178 assert 0 <= opnum < len(self._pure_operations)
179 recentops = self._pure_operations[opnum]
180 if recentops is None and create:
181 length = self.optimizer.jitdriver_sd.warmstate.pureop_historylength
182 self._pure_operations[opnum] = recentops = RecentPureOps(length)
183 return recentops
184
185 def optimize_call_pure(self, op, start_index=0):
186 # Step 1: check if all arguments are constant
187 for i in range(start_index, op.numargs()):
188 self.optimizer.force_box(op.getarg(i))
189 # XXX hack to ensure that virtuals that are
190 # constant are presented that way
191 result = self._can_optimize_call_pure(op, start_index=start_index)
192 if result is not None:
193 # this removes a CALL_PURE with all constant arguments.
194 self.make_constant(op, result)
195 self.last_emitted_operation = REMOVED
196 return
197
198 # Step 2: check if all arguments are the same as a previous
199 # CALL_PURE.
200 for pos in self.call_pure_positions:
201 old_op = self.optimizer._newoperations[pos]
202 if self.optimize_call_pure_old(op, old_op, start_index):
203 return
204 if self.extra_call_pure:
205 for i, old_op in enumerate(self.extra_call_pure):
206 if self.optimize_call_pure_old(op, old_op, start_index):
207 if isinstance(old_op, PreambleOp):
208 old_op = self.optimizer.force_op_from_preamble(old_op)
209 self.extra_call_pure[i] = old_op
210 return
211 if self.known_result_call_pure:
212 # needs different logic, since the result does not come from a real
213 # call at all
214 for i, known_result_op in enumerate(self.known_result_call_pure):
215 if op.getdescr() is not known_result_op.getdescr():
216 continue
217 if self._same_args(known_result_op, op, 1, start_index):
218 self.make_equal_to(op, known_result_op.getarg(0))
219 self.last_emitted_operation = REMOVED
220 return
221
222 # replace CALL_PURE with just CALL (but keep COND_CALL_VALUE)
223 if start_index == 0:
224 opnum = OpHelpers.call_for_descr(op.getdescr())
225 newop = self.optimizer.replace_op_with(op, opnum)
226 else:
227 newop = op
228 return self.emit_result(CallPureOptimizationResult(self, newop))
229
230 def optimize_CALL_PURE_I(self, op):
231 return self.optimize_call_pure(op)
232 optimize_CALL_PURE_R = optimize_CALL_PURE_I
233 optimize_CALL_PURE_F = optimize_CALL_PURE_I
234 optimize_CALL_PURE_N = optimize_CALL_PURE_I
235
236 def optimize_COND_CALL_VALUE_I(self, op):
237 return self.optimize_call_pure(op, start_index=1)
238 optimize_COND_CALL_VALUE_R = optimize_COND_CALL_VALUE_I
239
240 def _same_args(self, op1, op2, start_index1, start_index2):
241 j = start_index2
242 for i in range(start_index1, op1.numargs()):
243 box = get_box_replacement(op1.getarg(i))
244 if not get_box_replacement(op2.getarg(j)).same_box(box):
245 return False
246 j += 1
247 return True
248
249 def optimize_call_pure_old(self, op, old_op, start_index):
250 if op.getdescr() is not old_op.getdescr():
251 return False
252 # this will match a call_pure and a cond_call_value with
253 # the same function and arguments
254 old_start_index = OpHelpers.is_cond_call_value(old_op.opnum)
255 if self._same_args(old_op, op, old_start_index, start_index):
256 # all identical
257 # this removes a CALL_PURE that has the same (non-constant)
258 # arguments as a previous CALL_PURE.
259 if isinstance(old_op, PreambleOp):
260 # xxx obscure, it's dealt with in the caller
261 old_op = old_op.op
262 self.make_equal_to(op, old_op)
263 self.last_emitted_operation = REMOVED
264 return True
265 return False
266
267 def optimize_GUARD_NO_EXCEPTION(self, op):
268 if self.last_emitted_operation is REMOVED:
269 # it was a CALL_PURE that was killed; so we also kill the
270 # following GUARD_NO_EXCEPTION
271 return
272 return self.emit(op)
273
274 def optimize_RECORD_KNOWN_RESULT(self, op):
275 # remove the op, but keep the into about the result of the pure call
276 self.known_result_call_pure.append(op)
277
278 def flush(self):
279 assert self.postponed_op is None
280
281 def setup(self):
282 self.optimizer.optpure = self
283
284 def pure(self, opnum, op):
285 recentops = self.getrecentops(opnum)
286 recentops.add(op)
287
288 def pure_from_args(self, opnum, args, op, descr=None):
289 newop = ResOperation(opnum,
290 [get_box_replacement(arg) for arg in args],
291 descr=descr)
292 newop.set_forwarded(op)
293 self.pure(opnum, newop)
294
295 def pure_from_args2(self, opnum, arg0, arg1, op):
296 cls = opclasses[opnum]
297 newop = cls()
298 newop.setarg(0, get_box_replacement(arg0))
299 newop.setarg(1, get_box_replacement(arg1))
300 newop.set_forwarded(op)
301 self.pure(opnum, newop)
302
303 def pure_from_args1(self, opnum, arg0, op):
304 cls = opclasses[opnum]
305 newop = cls()
306 newop.setarg(0, get_box_replacement(arg0))
307 newop.set_forwarded(op)
308 self.pure(opnum, newop)
309
310 def get_pure_result(self, op):
311 recentops = self.getrecentops(op.getopnum(), create=False)
312 if not recentops:
313 return None
314 return get_box_replacement(recentops.lookup(self.optimizer, op))
315
316 def produce_potential_short_preamble_ops(self, sb):
317 ops = self.optimizer._newoperations
318 for i, op in enumerate(ops):
319 if rop.is_always_pure(op.opnum):
320 sb.add_pure_op(op)
321 if rop.is_ovf(op.opnum) and ops[i + 1].getopnum() == rop.GUARD_NO_OVERFLOW:
322 sb.add_pure_op(op)
323 for i in self.call_pure_positions:
324 op = ops[i]
325 # don't move call_pure_with_exception in the short preamble...
326 # issue #2015
327
328 # Also, don't move cond_call_value in the short preamble.
329 # The issue there is that it's usually pointless to try to
330 # because the 'value' argument is typically not a loop
331 # invariant, and would really need to be in order to end up
332 # in the short preamble. Maybe the code works anyway in the
333 # other rare case, but better safe than sorry and don't try.
334 effectinfo = op.getdescr().get_extra_info()
335 if not effectinfo.check_can_raise(ignore_memoryerror=True):
336 assert rop.is_call(op.opnum)
337 if not OpHelpers.is_cond_call_value(op.opnum):
338 sb.add_pure_op(op)
339
340 dispatch_opt = make_dispatcher_method(OptPure, 'optimize_',
341 default=OptPure.optimize_default)
342 OptPure.propagate_postprocess = make_dispatcher_method(OptPure, 'postprocess_')
343 OptPure.have_postprocess_op = have_dispatcher_method(OptPure, 'postprocess_')