| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
import py |
| 4 |
|
| 5 |
from rpython.jit.metainterp import compile |
| 6 |
from rpython.jit.metainterp.optimizeopt.util import make_dispatcher_method |
| 7 |
from rpython.jit.metainterp.resoperation import (rop, GuardResOp, ResOperation) |
| 8 |
from rpython.jit.codewriter.effectinfo import EffectInfo |
| 9 |
from rpython.jit.metainterp.history import (ConstPtr, ConstInt,Const, |
| 10 |
AbstractValue, AbstractFailDescr) |
| 11 |
from rpython.rtyper.lltypesystem import llmemory |
| 12 |
from rpython.rlib.unroll import unrolling_iterable |
| 13 |
from rpython.rlib.objectmodel import we_are_translated |
| 14 |
|
| 15 |
MODIFY_COMPLEX_OBJ = [ (rop.SETARRAYITEM_GC, 0, 1) |
| 16 |
, (rop.SETARRAYITEM_RAW, 0, 1) |
| 17 |
, (rop.RAW_STORE, 0, 1) |
| 18 |
, (rop.VEC_STORE, 0, 1) |
| 19 |
, (rop.SETINTERIORFIELD_GC, 0, -1) |
| 20 |
, (rop.SETINTERIORFIELD_RAW, 0, -1) |
| 21 |
, (rop.SETFIELD_GC, 0, -1) |
| 22 |
, (rop.SETFIELD_RAW, 0, -1) |
| 23 |
, (rop.ZERO_ARRAY, 0, -1) |
| 24 |
, (rop.STRSETITEM, 0, -1) |
| 25 |
, (rop.UNICODESETITEM, 0, -1) |
| 26 |
] |
| 27 |
|
| 28 |
UNROLLED_MODIFY_COMPLEX_OBJ = unrolling_iterable(MODIFY_COMPLEX_OBJ) |
| 29 |
|
| 30 |
LOAD_COMPLEX_OBJ = [ (rop.GETARRAYITEM_GC_I, 0, 1) |
| 31 |
, (rop.GETARRAYITEM_GC_F, 0, 1) |
| 32 |
, (rop.GETARRAYITEM_GC_R, 0, 1) |
| 33 |
, (rop.GETARRAYITEM_RAW_I, 0, 1) |
| 34 |
, (rop.GETARRAYITEM_RAW_F, 0, 1) |
| 35 |
, (rop.RAW_LOAD_I, 0, 1) |
| 36 |
, (rop.RAW_LOAD_F, 0, 1) |
| 37 |
, (rop.VEC_LOAD_I, 0, 1) |
| 38 |
, (rop.VEC_LOAD_F, 0, 1) |
| 39 |
, (rop.GETINTERIORFIELD_GC_I, 0, 1) |
| 40 |
, (rop.GETINTERIORFIELD_GC_F, 0, 1) |
| 41 |
, (rop.GETINTERIORFIELD_GC_R, 0, 1) |
| 42 |
, (rop.GETFIELD_GC_I, 0, -1) |
| 43 |
, (rop.GETFIELD_GC_F, 0, -1) |
| 44 |
, (rop.GETFIELD_GC_R, 0, -1) |
| 45 |
, (rop.GETFIELD_RAW_I, 0, -1) |
| 46 |
, (rop.GETFIELD_RAW_F, 0, -1) |
| 47 |
, (rop.GETFIELD_RAW_R, 0, -1) |
| 48 |
] |
| 49 |
|
| 50 |
UNROLLED_LOAD_COMPLEX_OBJ = unrolling_iterable(LOAD_COMPLEX_OBJ) |
| 51 |
|
| 52 |
class Path(object): |
| 53 |
def __init__(self,path): |
| 54 |
self.path = path |
| 55 |
|
| 56 |
def second(self): |
| 57 |
if len(self.path) <= 1: |
| 58 |
return None |
| 59 |
return self.path[1] |
| 60 |
|
| 61 |
def last_but_one(self): |
| 62 |
if len(self.path) < 2: |
| 63 |
return None |
| 64 |
return self.path[-2] |
| 65 |
|
| 66 |
def last(self): |
| 67 |
if len(self.path) < 1: |
| 68 |
return None |
| 69 |
return self.path[-1] |
| 70 |
|
| 71 |
def first(self): |
| 72 |
return self.path[0] |
| 73 |
|
| 74 |
def is_always_pure(self, exclude_first=False, exclude_last=False): |
| 75 |
last = len(self.path)-1 |
| 76 |
count = len(self.path) |
| 77 |
i = 0 |
| 78 |
if exclude_first: |
| 79 |
i += 1 |
| 80 |
if exclude_last: |
| 81 |
count -= 1 |
| 82 |
while i < count: |
| 83 |
node = self.path[i] |
| 84 |
if node.is_imaginary(): |
| 85 |
i += 1 |
| 86 |
continue |
| 87 |
op = node.getoperation() |
| 88 |
if rop.is_guard(op.opnum): |
| 89 |
descr = op.getdescr() |
| 90 |
if not descr: |
| 91 |
return False |
| 92 |
assert isinstance(descr, AbstractFailDescr) |
| 93 |
if not descr.exits_early(): |
| 94 |
return False |
| 95 |
elif not rop.is_always_pure(op.opnum): |
| 96 |
return False |
| 97 |
i += 1 |
| 98 |
return True |
| 99 |
|
| 100 |
def set_schedule_priority(self, p): |
| 101 |
for node in self.path: |
| 102 |
node.setpriority(p) |
| 103 |
|
| 104 |
def walk(self, node): |
| 105 |
self.path.append(node) |
| 106 |
|
| 107 |
def cut_off_at(self, index): |
| 108 |
self.path = self.path[:index] |
| 109 |
|
| 110 |
def check_acyclic(self): |
| 111 |
"""NOT_RPYTHON""" |
| 112 |
seen = set() |
| 113 |
for segment in self.path: |
| 114 |
if segment in seen: |
| 115 |
print("path:") |
| 116 |
for segment in self.path: |
| 117 |
print(" ->", segment) |
| 118 |
print("") |
| 119 |
assert 0, "segment %s was already seen. this makes the path cyclic!" % segment |
| 120 |
else: |
| 121 |
seen.add(segment) |
| 122 |
return True |
| 123 |
|
| 124 |
def clone(self): |
| 125 |
return Path(self.path[:]) |
| 126 |
|
| 127 |
def as_str(self): |
| 128 |
""" NOT_RPYTHON """ |
| 129 |
return ' -> '.join([str(p) for p in self.path]) |
| 130 |
|
| 131 |
class Node(object): |
| 132 |
def __init__(self, op, opidx): |
| 133 |
self.op = op |
| 134 |
self.opidx = opidx |
| 135 |
self.adjacent_list = [] |
| 136 |
self.adjacent_list_back = [] |
| 137 |
self.memory_ref = None |
| 138 |
self.pack = None |
| 139 |
self.pack_position = -1 |
| 140 |
self.emitted = False |
| 141 |
self.schedule_position = -1 |
| 142 |
self.priority = 0 |
| 143 |
self._stack = False |
| 144 |
self.delayed = None |
| 145 |
|
| 146 |
def is_imaginary(self): |
| 147 |
return False |
| 148 |
|
| 149 |
def getoperation(self): |
| 150 |
return self.op |
| 151 |
|
| 152 |
def getindex(self): |
| 153 |
return self.opidx |
| 154 |
|
| 155 |
def getopnum(self): |
| 156 |
return self.op.getopnum() |
| 157 |
|
| 158 |
def getopname(self): |
| 159 |
return self.op.getopname() |
| 160 |
|
| 161 |
def setpriority(self, value): |
| 162 |
self.priority = value |
| 163 |
|
| 164 |
def can_be_relaxed(self): |
| 165 |
return self.op.getopnum() in (rop.GUARD_TRUE, rop.GUARD_FALSE) |
| 166 |
|
| 167 |
def is_pure(self): |
| 168 |
return rop.is_always_pure(self.op.getopnum()) |
| 169 |
|
| 170 |
def edge_to(self, to, arg=None, failarg=False, label=None): |
| 171 |
if self is to: |
| 172 |
return |
| 173 |
dep = self.depends_on(to) |
| 174 |
if not dep: |
| 175 |
|
| 176 |
dep = Dependency(self, to, arg, failarg) |
| 177 |
self.adjacent_list.append(dep) |
| 178 |
dep_back = Dependency(to, self, arg, failarg) |
| 179 |
dep.backward = dep_back |
| 180 |
to.adjacent_list_back.append(dep_back) |
| 181 |
if not we_are_translated(): |
| 182 |
if label is None: |
| 183 |
label = '' |
| 184 |
dep.label = label |
| 185 |
else: |
| 186 |
if not dep.because_of(arg): |
| 187 |
dep.add_dependency(self,to,arg) |
| 188 |
|
| 189 |
|
| 190 |
if not (dep.is_failarg() and failarg): |
| 191 |
dep.set_failarg(False) |
| 192 |
if not we_are_translated() and label is not None: |
| 193 |
_label = getattr(dep, 'label', '') |
| 194 |
dep.label = _label + ", " + label |
| 195 |
return dep |
| 196 |
|
| 197 |
def clear_dependencies(self): |
| 198 |
self.adjacent_list = [] |
| 199 |
self.adjacent_list_back = [] |
| 200 |
|
| 201 |
def exits_early(self): |
| 202 |
if self.op.is_guard(): |
| 203 |
descr = self.op.getdescr() |
| 204 |
return descr.exits_early() |
| 205 |
return False |
| 206 |
|
| 207 |
def loads_from_complex_object(self): |
| 208 |
return rop._ALWAYS_PURE_LAST <= self.op.getopnum() < rop._MALLOC_FIRST |
| 209 |
|
| 210 |
def modifies_complex_object(self): |
| 211 |
return rop.SETARRAYITEM_GC <= self.op.getopnum() <= rop.UNICODESETITEM |
| 212 |
|
| 213 |
def side_effect_arguments(self): |
| 214 |
|
| 215 |
|
| 216 |
args = [] |
| 217 |
op = self.op |
| 218 |
if self.modifies_complex_object(): |
| 219 |
for opnum, i, j in UNROLLED_MODIFY_COMPLEX_OBJ: |
| 220 |
if op.getopnum() == opnum: |
| 221 |
op_args = op.getarglist() |
| 222 |
if j == -1: |
| 223 |
args.append((op.getarg(i), None, True)) |
| 224 |
for j in range(i+1,len(op_args)): |
| 225 |
args.append((op.getarg(j), None, False)) |
| 226 |
else: |
| 227 |
args.append((op.getarg(i), op.getarg(j), True)) |
| 228 |
for x in range(j+1,len(op_args)): |
| 229 |
args.append((op.getarg(x), None, False)) |
| 230 |
return args |
| 231 |
|
| 232 |
|
| 233 |
for arg in op.getarglist(): |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
if arg.is_constant() or arg.type == 'f': |
| 238 |
args.append((arg, None, False)) |
| 239 |
else: |
| 240 |
args.append((arg, None, True)) |
| 241 |
return args |
| 242 |
|
| 243 |
def provides_count(self): |
| 244 |
return len(self.adjacent_list) |
| 245 |
|
| 246 |
def provides(self): |
| 247 |
return self.adjacent_list |
| 248 |
|
| 249 |
def depends_count(self): |
| 250 |
return len(self.adjacent_list_back) |
| 251 |
|
| 252 |
def depends(self): |
| 253 |
return self.adjacent_list_back |
| 254 |
|
| 255 |
def depends_on(self, to): |
| 256 |
""" Does there exist a dependency from the instruction to another? |
| 257 |
Returns None if there is no dependency or the Dependency object in |
| 258 |
any other case. |
| 259 |
""" |
| 260 |
for edge in self.adjacent_list: |
| 261 |
if edge.to is to: |
| 262 |
return edge |
| 263 |
return None |
| 264 |
|
| 265 |
def dependencies(self): |
| 266 |
return self.adjacent_list[:] + self.adjacent_list_back[:] |
| 267 |
|
| 268 |
def is_after(self, other): |
| 269 |
return self.opidx > other.opidx |
| 270 |
|
| 271 |
def is_before(self, other): |
| 272 |
return self.opidx < other.opidx |
| 273 |
|
| 274 |
def independent(self, other): |
| 275 |
""" An instruction depends on another if there is a path from |
| 276 |
self to other. """ |
| 277 |
if self == other: |
| 278 |
return True |
| 279 |
|
| 280 |
worklist = [self] |
| 281 |
while len(worklist) > 0: |
| 282 |
node = worklist.pop() |
| 283 |
for dep in node.provides(): |
| 284 |
if dep.to.is_after(other): |
| 285 |
continue |
| 286 |
if dep.points_to(other): |
| 287 |
|
| 288 |
return False |
| 289 |
worklist.append(dep.to) |
| 290 |
|
| 291 |
worklist = [self] |
| 292 |
while len(worklist) > 0: |
| 293 |
node = worklist.pop() |
| 294 |
for dep in node.depends(): |
| 295 |
if dep.to.is_before(other): |
| 296 |
continue |
| 297 |
if dep.points_to(other): |
| 298 |
|
| 299 |
return False |
| 300 |
worklist.append(dep.to) |
| 301 |
return True |
| 302 |
|
| 303 |
def iterate_paths(self, to, backwards=False, path_max_len=-1, blacklist=False): |
| 304 |
""" Yield all nodes from self leading to 'to'. |
| 305 |
|
| 306 |
backwards: Determines the iteration direction. |
| 307 |
blacklist: Marks nodes that have already been visited. |
| 308 |
It comes in handy if a property must hold for every path. |
| 309 |
Not *every* possible instance must be iterated, but trees |
| 310 |
that have already been visited can be ignored after the |
| 311 |
first visit. |
| 312 |
""" |
| 313 |
if self is to: |
| 314 |
return |
| 315 |
blacklist_visit = {} |
| 316 |
path = Path([self]) |
| 317 |
worklist = [(0, self, 1)] |
| 318 |
while len(worklist) > 0: |
| 319 |
index,node,pathlen = worklist.pop() |
| 320 |
if backwards: |
| 321 |
iterdir = node.depends() |
| 322 |
else: |
| 323 |
iterdir = node.provides() |
| 324 |
if index >= len(iterdir): |
| 325 |
if to is None and index == 0: |
| 326 |
yield Path(path.path[:]) |
| 327 |
if blacklist: |
| 328 |
blacklist_visit[node] = None |
| 329 |
continue |
| 330 |
else: |
| 331 |
next_dep = iterdir[index] |
| 332 |
next_node = next_dep.to |
| 333 |
index += 1 |
| 334 |
if index < len(iterdir): |
| 335 |
worklist.append((index, node, pathlen)) |
| 336 |
else: |
| 337 |
blacklist_visit[node] = None |
| 338 |
path.cut_off_at(pathlen) |
| 339 |
path.walk(next_node) |
| 340 |
if blacklist and next_node in blacklist_visit: |
| 341 |
yield Path(path.path[:]) |
| 342 |
continue |
| 343 |
pathlen += 1 |
| 344 |
|
| 345 |
if next_node is to or \ |
| 346 |
(path_max_len > 0 and pathlen >= path_max_len): |
| 347 |
yield Path(path.path[:]) |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
else: |
| 352 |
worklist.append((0, next_node, pathlen)) |
| 353 |
|
| 354 |
def remove_edge_to(self, node): |
| 355 |
i = 0 |
| 356 |
while i < len(self.adjacent_list): |
| 357 |
dep = self.adjacent_list[i] |
| 358 |
if dep.to is node: |
| 359 |
del self.adjacent_list[i] |
| 360 |
break |
| 361 |
i += 1 |
| 362 |
i = 0 |
| 363 |
while i < len(node.adjacent_list_back): |
| 364 |
dep = node.adjacent_list_back[i] |
| 365 |
if dep.to is self: |
| 366 |
del node.adjacent_list_back[i] |
| 367 |
break |
| 368 |
i += 1 |
| 369 |
|
| 370 |
def getedge_to(self, other): |
| 371 |
for dep in self.adjacent_list: |
| 372 |
if dep.to == other: |
| 373 |
return dep |
| 374 |
return None |
| 375 |
|
| 376 |
def __repr__(self): |
| 377 |
pack = '' |
| 378 |
if self.pack: |
| 379 |
pack = "p: %d" % self.pack.numops() |
| 380 |
return "Node(%s,%s i: %d)" % (self.op, pack, self.opidx) |
| 381 |
|
| 382 |
def getdotlabel(self): |
| 383 |
""" NOT_RPTYHON """ |
| 384 |
op_str = str(self.op) |
| 385 |
if self.op.is_guard(): |
| 386 |
args_str = [] |
| 387 |
for arg in self.op.getfailargs(): |
| 388 |
name = 'None' |
| 389 |
if arg: |
| 390 |
name = arg.repr_short(arg._repr_memo) |
| 391 |
args_str.append(name) |
| 392 |
op_str += " " + ','.join(args_str) |
| 393 |
return "[%d] %s" % (self.opidx, op_str) |
| 394 |
|
| 395 |
class ImaginaryNode(Node): |
| 396 |
_index = 987654321 |
| 397 |
def __init__(self, label): |
| 398 |
index = -1 |
| 399 |
if not we_are_translated(): |
| 400 |
self.dotlabel = label |
| 401 |
index = ImaginaryNode._index |
| 402 |
ImaginaryNode._index += 1 |
| 403 |
Node.__init__(self, None, index) |
| 404 |
|
| 405 |
def is_imaginary(self): |
| 406 |
return True |
| 407 |
|
| 408 |
def getdotlabel(self): |
| 409 |
""" NOT_RPTYHON """ |
| 410 |
return self.dotlabel |
| 411 |
|
| 412 |
class Dependency(object): |
| 413 |
def __init__(self, at, to, arg, failarg=False): |
| 414 |
assert at != to |
| 415 |
self.args = [] |
| 416 |
if arg is not None: |
| 417 |
self.add_dependency(at, to, arg) |
| 418 |
self.at = at |
| 419 |
self.to = to |
| 420 |
self.failarg = failarg |
| 421 |
self.backward = None |
| 422 |
|
| 423 |
def because_of(self, var): |
| 424 |
for arg in self.args: |
| 425 |
if arg[1] == var: |
| 426 |
return True |
| 427 |
return False |
| 428 |
|
| 429 |
def target_node(self): |
| 430 |
return self.to |
| 431 |
|
| 432 |
def origin_node(self): |
| 433 |
return self.at |
| 434 |
|
| 435 |
def to_index(self): |
| 436 |
return self.to.getindex() |
| 437 |
def at_index(self): |
| 438 |
return self.at.getindex() |
| 439 |
|
| 440 |
def points_after_to(self, to): |
| 441 |
return self.to.opidx < to.opidx |
| 442 |
def points_above_at(self, at): |
| 443 |
return self.at.opidx < at.opidx |
| 444 |
def i_points_above_at(self, idx): |
| 445 |
return self.at.opidx < idx |
| 446 |
|
| 447 |
def points_to(self, to): |
| 448 |
return self.to == to |
| 449 |
def points_at(self, at): |
| 450 |
return self.at == at |
| 451 |
|
| 452 |
def add_dependency(self, at, to, arg): |
| 453 |
self.args.append((at,arg)) |
| 454 |
|
| 455 |
def set_failarg(self, value): |
| 456 |
self.failarg = value |
| 457 |
if self.backward: |
| 458 |
self.backward.failarg = value |
| 459 |
|
| 460 |
def is_failarg(self): |
| 461 |
return self.failarg |
| 462 |
|
| 463 |
def reverse_direction(self, ref): |
| 464 |
""" if the parameter index is the same as idx_to then |
| 465 |
this edge is in reverse direction. |
| 466 |
""" |
| 467 |
return self.to == ref |
| 468 |
|
| 469 |
def __repr__(self): |
| 470 |
return 'Dep(T[%d] -> T[%d], arg: %s)' \ |
| 471 |
% (self.at.opidx, self.to.opidx, self.args) |
| 472 |
|
| 473 |
class DefTracker(object): |
| 474 |
def __init__(self, graph): |
| 475 |
self.graph = graph |
| 476 |
self.defs = {} |
| 477 |
self.non_pure = [] |
| 478 |
|
| 479 |
def add_non_pure(self, node): |
| 480 |
self.non_pure.append(node) |
| 481 |
|
| 482 |
def define(self, arg, node, argcell=None): |
| 483 |
if isinstance(arg, Const): |
| 484 |
return |
| 485 |
if arg in self.defs: |
| 486 |
self.defs[arg].append((node,argcell)) |
| 487 |
else: |
| 488 |
self.defs[arg] = [(node,argcell)] |
| 489 |
|
| 490 |
def redefinitions(self, arg): |
| 491 |
for _def in self.defs[arg]: |
| 492 |
yield _def[0] |
| 493 |
|
| 494 |
def is_defined(self, arg): |
| 495 |
return arg in self.defs |
| 496 |
|
| 497 |
def definition(self, arg, node=None, argcell=None): |
| 498 |
if arg.is_constant(): |
| 499 |
return None |
| 500 |
def_chain = self.defs.get(arg,None) |
| 501 |
if not def_chain: |
| 502 |
return None |
| 503 |
if not argcell: |
| 504 |
return def_chain[-1][0] |
| 505 |
else: |
| 506 |
assert node is not None |
| 507 |
i = len(def_chain)-1 |
| 508 |
try: |
| 509 |
mref = node.memory_ref |
| 510 |
while i >= 0: |
| 511 |
def_node = def_chain[i][0] |
| 512 |
oref = def_node.memory_ref |
| 513 |
if oref is not None and mref.alias(oref): |
| 514 |
return def_node |
| 515 |
elif oref is None: |
| 516 |
return def_node |
| 517 |
i -= 1 |
| 518 |
return None |
| 519 |
except KeyError: |
| 520 |
|
| 521 |
|
| 522 |
pass |
| 523 |
return def_chain[-1][0] |
| 524 |
|
| 525 |
def depends_on_arg(self, arg, to, argcell=None): |
| 526 |
try: |
| 527 |
at = self.definition(arg, to, argcell) |
| 528 |
if at is None: |
| 529 |
return |
| 530 |
at.edge_to(to, arg) |
| 531 |
except KeyError: |
| 532 |
if not we_are_translated(): |
| 533 |
if not isinstance(arg, Const): |
| 534 |
assert False, "arg %s must be defined" % arg |
| 535 |
|
| 536 |
|
| 537 |
class DependencyGraph(object): |
| 538 |
""" A graph that represents one of the following dependencies: |
| 539 |
* True dependency |
| 540 |
* Anti dependency (not present in SSA traces) |
| 541 |
* Ouput dependency (not present in SSA traces) |
| 542 |
Traces in RPython are not in SSA form when it comes to complex |
| 543 |
object modification such as array or object side effects. |
| 544 |
Representation is an adjacent list. The number of edges between the |
| 545 |
vertices is expected to be small. |
| 546 |
Note that adjacent lists order their dependencies. They are ordered |
| 547 |
by the target instruction they point to if the instruction is |
| 548 |
a dependency. |
| 549 |
|
| 550 |
memory_refs: a dict that contains indices of memory references |
| 551 |
(load,store,getarrayitem,...). If none provided, the construction |
| 552 |
is conservative. It will never dismiss dependencies of two |
| 553 |
modifications of one array even if the indices can never point to |
| 554 |
the same element. |
| 555 |
""" |
| 556 |
def __init__(self, loop): |
| 557 |
self.loop = loop |
| 558 |
label = loop.prefix_label or loop.label |
| 559 |
self.label = Node(label, 0) |
| 560 |
self.nodes = [ Node(op,0) for op in loop.operations if not rop.is_jit_debug(op.opnum) ] |
| 561 |
for i,node in enumerate(self.nodes): |
| 562 |
node.opidx = i+1 |
| 563 |
self.inodes = [] |
| 564 |
self.jump = Node(loop.jump, len(self.nodes)+1) |
| 565 |
self.invariant_vars = {} |
| 566 |
self.update_invariant_vars(label) |
| 567 |
self.memory_refs = {} |
| 568 |
self.schedulable_nodes = [] |
| 569 |
self.index_vars = {} |
| 570 |
self.comparison_vars = {} |
| 571 |
self.guards = [] |
| 572 |
self.build_dependencies() |
| 573 |
|
| 574 |
def getnode(self, i): |
| 575 |
return self.nodes[i] |
| 576 |
|
| 577 |
def imaginary_node(self, label): |
| 578 |
node = ImaginaryNode(label) |
| 579 |
self.inodes.append(node) |
| 580 |
return node |
| 581 |
|
| 582 |
def update_invariant_vars(self, label_op=None): |
| 583 |
if not label_op: |
| 584 |
label_op = self.label.getoperation() |
| 585 |
jump_op = self.jump.getoperation() |
| 586 |
assert label_op.numargs() == jump_op.numargs() |
| 587 |
for i in range(label_op.numargs()): |
| 588 |
label_box = label_op.getarg(i) |
| 589 |
jump_box = jump_op.getarg(i) |
| 590 |
if label_box == jump_box: |
| 591 |
self.invariant_vars[label_box] = None |
| 592 |
|
| 593 |
def box_is_invariant(self, box): |
| 594 |
return box in self.invariant_vars |
| 595 |
|
| 596 |
def build_dependencies(self): |
| 597 |
""" This is basically building the definition-use chain and saving this |
| 598 |
information in a graph structure. This is the same as calculating |
| 599 |
the reaching definitions and the 'looking back' whenever it is used. |
| 600 |
|
| 601 |
Write After Read, Write After Write dependencies are not possible, |
| 602 |
the operations are in SSA form |
| 603 |
""" |
| 604 |
tracker = DefTracker(self) |
| 605 |
|
| 606 |
label_pos = 0 |
| 607 |
jump_pos = len(self.nodes)-1 |
| 608 |
intformod = IntegralForwardModification(self.memory_refs, self.index_vars, |
| 609 |
self.comparison_vars, self.invariant_vars) |
| 610 |
|
| 611 |
for i,node in enumerate(self.nodes): |
| 612 |
op = node.op |
| 613 |
if rop.is_always_pure(op.opnum): |
| 614 |
node.setpriority(1) |
| 615 |
if rop.is_guard(op.opnum): |
| 616 |
node.setpriority(2) |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
intformod.inspect_operation(op,node) |
| 621 |
|
| 622 |
if op.type != 'v': |
| 623 |
|
| 624 |
tracker.define(op, node) |
| 625 |
|
| 626 |
if rop.is_always_pure(op.opnum) or rop.is_final(op.opnum): |
| 627 |
|
| 628 |
for arg in op.getarglist(): |
| 629 |
tracker.depends_on_arg(arg, node) |
| 630 |
elif rop.is_guard(op.opnum): |
| 631 |
if node.exits_early(): |
| 632 |
pass |
| 633 |
else: |
| 634 |
|
| 635 |
if len(self.guards) > 0: |
| 636 |
last_guard = self.guards[-1] |
| 637 |
last_guard.edge_to(node, failarg=True, label="guardorder") |
| 638 |
for nonpure in tracker.non_pure: |
| 639 |
nonpure.edge_to(node, failarg=True, label="nonpure") |
| 640 |
tracker.non_pure = [] |
| 641 |
self.guards.append(node) |
| 642 |
self.build_guard_dependencies(node, tracker) |
| 643 |
else: |
| 644 |
self.build_non_pure_dependencies(node, tracker) |
| 645 |
|
| 646 |
def guard_argument_protection(self, guard_node, tracker): |
| 647 |
""" the parameters the guard protects are an indicator for |
| 648 |
dependencies. Consider the example: |
| 649 |
i3 = ptr_eq(p1,p2) |
| 650 |
guard_true(i3) [...] |
| 651 |
|
| 652 |
guard_true|false are exceptions because they do not directly |
| 653 |
protect the arguments, but a comparison function does. |
| 654 |
""" |
| 655 |
guard_op = guard_node.getoperation() |
| 656 |
guard_opnum = guard_op.getopnum() |
| 657 |
for arg in guard_op.getarglist(): |
| 658 |
if not arg.is_constant() and arg.type not in ('i','f'): |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
tracker.define(arg, guard_node) |
| 665 |
if guard_opnum == rop.GUARD_NOT_FORCED_2: |
| 666 |
|
| 667 |
guard_node.setpriority(-10) |
| 668 |
elif guard_opnum in (rop.GUARD_OVERFLOW, rop.GUARD_NO_OVERFLOW): |
| 669 |
|
| 670 |
guard_node.setpriority(100) |
| 671 |
i = guard_node.getindex()-1 |
| 672 |
while i >= 0: |
| 673 |
node = self.nodes[i] |
| 674 |
op = node.getoperation() |
| 675 |
if op.is_ovf(): |
| 676 |
break |
| 677 |
i -= 1 |
| 678 |
else: |
| 679 |
raise AssertionError("(no)overflow: no overflowing op present") |
| 680 |
node.edge_to(guard_node, None, label='overflow') |
| 681 |
elif guard_opnum in (rop.GUARD_NO_EXCEPTION, rop.GUARD_EXCEPTION, rop.GUARD_NOT_FORCED): |
| 682 |
|
| 683 |
guard_node.setpriority(100) |
| 684 |
i = guard_node.getindex() - 1 |
| 685 |
while i >= 0: |
| 686 |
node = self.nodes[i] |
| 687 |
op = node.getoperation() |
| 688 |
if op.can_raise(): |
| 689 |
node.edge_to(guard_node, None, label='exception/notforced') |
| 690 |
break |
| 691 |
if op.is_guard(): |
| 692 |
node.edge_to(guard_node, None, label='exception/notforced') |
| 693 |
break |
| 694 |
i -= 1 |
| 695 |
else: |
| 696 |
raise AssertionError("(no)exception/not_forced: not op raises for them") |
| 697 |
else: |
| 698 |
pass |
| 699 |
|
| 700 |
def guard_exit_dependence(self, guard_node, var, tracker): |
| 701 |
def_node = tracker.definition(var) |
| 702 |
if def_node is None: |
| 703 |
return |
| 704 |
for dep in def_node.provides(): |
| 705 |
if guard_node.is_before(dep.to) and dep.because_of(var): |
| 706 |
guard_node.edge_to(dep.to, var, label='guard_exit('+str(var)+')') |
| 707 |
|
| 708 |
def build_guard_dependencies(self, guard_node, tracker): |
| 709 |
guard_op = guard_node.op |
| 710 |
if guard_op.getopnum() >= rop.GUARD_FUTURE_CONDITION: |
| 711 |
|
| 712 |
return |
| 713 |
|
| 714 |
for arg in guard_op.getarglist(): |
| 715 |
tracker.depends_on_arg(arg, guard_node) |
| 716 |
|
| 717 |
self.guard_argument_protection(guard_node, tracker) |
| 718 |
|
| 719 |
descr = guard_op.getdescr() |
| 720 |
if descr.exits_early(): |
| 721 |
return |
| 722 |
|
| 723 |
if guard_op.getfailargs(): |
| 724 |
for i,arg in enumerate(guard_op.getfailargs()): |
| 725 |
if arg is None: |
| 726 |
continue |
| 727 |
if not tracker.is_defined(arg): |
| 728 |
continue |
| 729 |
try: |
| 730 |
for at in tracker.redefinitions(arg): |
| 731 |
|
| 732 |
if at.is_before(guard_node): |
| 733 |
at.edge_to(guard_node, arg, failarg=True, label="fail") |
| 734 |
except KeyError: |
| 735 |
assert False |
| 736 |
|
| 737 |
def build_non_pure_dependencies(self, node, tracker): |
| 738 |
op = node.op |
| 739 |
if node.loads_from_complex_object(): |
| 740 |
|
| 741 |
|
| 742 |
for opnum, i, j in UNROLLED_LOAD_COMPLEX_OBJ: |
| 743 |
if opnum == op.getopnum(): |
| 744 |
cobj = op.getarg(i) |
| 745 |
if j != -1: |
| 746 |
index_var = op.getarg(j) |
| 747 |
tracker.depends_on_arg(cobj, node, index_var) |
| 748 |
tracker.depends_on_arg(index_var, node) |
| 749 |
else: |
| 750 |
tracker.depends_on_arg(cobj, node) |
| 751 |
break |
| 752 |
else: |
| 753 |
for arg, argcell, destroyed in node.side_effect_arguments(): |
| 754 |
if argcell is not None: |
| 755 |
|
| 756 |
tracker.depends_on_arg(arg, node, argcell) |
| 757 |
tracker.depends_on_arg(argcell, node) |
| 758 |
else: |
| 759 |
if destroyed: |
| 760 |
|
| 761 |
|
| 762 |
try: |
| 763 |
|
| 764 |
|
| 765 |
def_node = tracker.definition(arg) |
| 766 |
if def_node: |
| 767 |
for dep in def_node.provides(): |
| 768 |
if dep.to != node: |
| 769 |
dep.to.edge_to(node, argcell, label='war') |
| 770 |
def_node.edge_to(node, argcell) |
| 771 |
except KeyError: |
| 772 |
pass |
| 773 |
else: |
| 774 |
|
| 775 |
tracker.depends_on_arg(arg, node) |
| 776 |
if destroyed: |
| 777 |
tracker.define(arg, node, argcell=argcell) |
| 778 |
|
| 779 |
|
| 780 |
if len(self.guards) > 0: |
| 781 |
last_guard = self.guards[-1] |
| 782 |
last_guard.edge_to(node, label="sideeffect") |
| 783 |
|
| 784 |
tracker.add_non_pure(node) |
| 785 |
|
| 786 |
def cycles(self): |
| 787 |
""" NOT_RPYTHON """ |
| 788 |
stack = [] |
| 789 |
for node in self.nodes: |
| 790 |
node._stack = False |
| 791 |
|
| 792 |
label = self.nodes[0] |
| 793 |
if _first_cycle(stack, label): |
| 794 |
return stack |
| 795 |
return None |
| 796 |
|
| 797 |
def __repr__(self): |
| 798 |
graph = "graph([\n" |
| 799 |
for node in self.nodes: |
| 800 |
graph += " " + str(node.opidx) + ": " |
| 801 |
for dep in node.provides(): |
| 802 |
graph += "=>" + str(dep.to.opidx) + "," |
| 803 |
graph += " | " |
| 804 |
for dep in node.depends(): |
| 805 |
graph += "<=" + str(dep.to.opidx) + "," |
| 806 |
graph += "\n" |
| 807 |
return graph + " ])" |
| 808 |
|
| 809 |
def view(self): |
| 810 |
""" NOT_RPYTHON """ |
| 811 |
from rpython.translator.tool.graphpage import GraphPage |
| 812 |
page = GraphPage() |
| 813 |
page.source = self.as_dot() |
| 814 |
page.links = [] |
| 815 |
page.display() |
| 816 |
|
| 817 |
def as_dot(self): |
| 818 |
""" NOT_RPTYHON """ |
| 819 |
if not we_are_translated(): |
| 820 |
dot = "digraph dep_graph {\n" |
| 821 |
for node in self.nodes + self.inodes: |
| 822 |
dot += " n%d [label=\"%s\"];\n" % (node.getindex(),node.getdotlabel()) |
| 823 |
dot += "\n" |
| 824 |
for node in self.nodes + self.inodes: |
| 825 |
for dep in node.provides(): |
| 826 |
label = '' |
| 827 |
if getattr(dep, 'label', None): |
| 828 |
label = '[label="%s"]' % dep.label |
| 829 |
dot += " n%d -> n%d %s;\n" % (node.getindex(),dep.to_index(),label) |
| 830 |
dot += "\n}\n" |
| 831 |
return dot |
| 832 |
raise NotImplementedError("dot only for debug purpose") |
| 833 |
|
| 834 |
def _first_cycle(stack, node): |
| 835 |
node._stack = True |
| 836 |
stack.append(node) |
| 837 |
|
| 838 |
for dep in node.provides(): |
| 839 |
succ = dep.to |
| 840 |
if succ._stack: |
| 841 |
|
| 842 |
while stack[0] is not succ: |
| 843 |
del stack[0] |
| 844 |
return True |
| 845 |
else: |
| 846 |
return _first_cycle(stack, succ) |
| 847 |
|
| 848 |
return False |
| 849 |
|
| 850 |
def _strongly_connect(index, stack, cycles, node): |
| 851 |
""" currently unused """ |
| 852 |
node._scc_index = index |
| 853 |
node._scc_lowlink = index |
| 854 |
index += 1 |
| 855 |
stack.append(node) |
| 856 |
node._scc_stack = True |
| 857 |
|
| 858 |
for dep in node.provides(): |
| 859 |
succ = dep.to |
| 860 |
if succ._scc_index == -1: |
| 861 |
index = _strongly_connect(index, stack, cycles, succ) |
| 862 |
node._scc_lowlink = min(node._scc_lowlink, succ._scc_lowlink) |
| 863 |
elif succ._scc_stack: |
| 864 |
node._scc_lowlink = min(node._scc_lowlink, succ._scc_index) |
| 865 |
|
| 866 |
if node._scc_lowlink == node._scc_index: |
| 867 |
cycle = [] |
| 868 |
while True: |
| 869 |
w = stack.pop() |
| 870 |
w._scc_stack = False |
| 871 |
cycle.append(w) |
| 872 |
if w is node: |
| 873 |
break |
| 874 |
cycles.append(cycle) |
| 875 |
return index |
| 876 |
|
| 877 |
class IntegralForwardModification(object): |
| 878 |
""" Calculates integral modifications on integer boxes. """ |
| 879 |
def __init__(self, memory_refs, index_vars, comparison_vars, invariant_vars): |
| 880 |
self.index_vars = index_vars |
| 881 |
self.comparison_vars = comparison_vars |
| 882 |
self.memory_refs = memory_refs |
| 883 |
self.invariant_vars = invariant_vars |
| 884 |
|
| 885 |
def is_const_integral(self, box): |
| 886 |
if isinstance(box, ConstInt): |
| 887 |
return True |
| 888 |
return False |
| 889 |
|
| 890 |
def get_or_create(self, arg): |
| 891 |
var = self.index_vars.get(arg, None) |
| 892 |
if not var: |
| 893 |
var = self.index_vars[arg] = IndexVar(arg) |
| 894 |
return var |
| 895 |
|
| 896 |
additive_func_source = """ |
| 897 |
def operation_{name}(self, op, node): |
| 898 |
box_r = op |
| 899 |
box_a0 = op.getarg(0) |
| 900 |
box_a1 = op.getarg(1) |
| 901 |
if self.is_const_integral(box_a0) and self.is_const_integral(box_a1): |
| 902 |
idx_ref = IndexVar(box_r) |
| 903 |
idx_ref.constant = box_a0.getint() {op} box_a1.getint() |
| 904 |
self.index_vars[box_r] = idx_ref |
| 905 |
elif self.is_const_integral(box_a0): |
| 906 |
idx_ref = self.get_or_create(box_a1) |
| 907 |
idx_ref = idx_ref.clone() |
| 908 |
idx_ref.constant {op}= box_a0.getint() |
| 909 |
self.index_vars[box_r] = idx_ref |
| 910 |
elif self.is_const_integral(box_a1): |
| 911 |
idx_ref = self.get_or_create(box_a0) |
| 912 |
idx_ref = idx_ref.clone() |
| 913 |
idx_ref.constant {op}= box_a1.getint() |
| 914 |
self.index_vars[box_r] = idx_ref |
| 915 |
""" |
| 916 |
exec(py.code.Source(additive_func_source |
| 917 |
.format(name='INT_ADD', op='+')).compile()) |
| 918 |
exec(py.code.Source(additive_func_source |
| 919 |
.format(name='INT_SUB', op='-')).compile()) |
| 920 |
del additive_func_source |
| 921 |
|
| 922 |
multiplicative_func_source = """ |
| 923 |
def operation_{name}(self, op, node): |
| 924 |
box_r = op |
| 925 |
if not box_r: |
| 926 |
return |
| 927 |
box_a0 = op.getarg(0) |
| 928 |
box_a1 = op.getarg(1) |
| 929 |
if self.is_const_integral(box_a0) and self.is_const_integral(box_a1): |
| 930 |
idx_ref = IndexVar(box_r) |
| 931 |
idx_ref.constant = box_a0.getint() {cop} box_a1.getint() |
| 932 |
self.index_vars[box_r] = idx_ref |
| 933 |
elif self.is_const_integral(box_a0): |
| 934 |
idx_ref = self.get_or_create(box_a1) |
| 935 |
idx_ref = idx_ref.clone() |
| 936 |
idx_ref.coefficient_{tgt} *= box_a0.getint() |
| 937 |
idx_ref.constant {cop}= box_a0.getint() |
| 938 |
self.index_vars[box_r] = idx_ref |
| 939 |
elif self.is_const_integral(box_a1): |
| 940 |
idx_ref = self.get_or_create(box_a0) |
| 941 |
idx_ref = idx_ref.clone() |
| 942 |
idx_ref.coefficient_{tgt} {op}= box_a1.getint() |
| 943 |
idx_ref.constant {cop}= box_a1.getint() |
| 944 |
self.index_vars[box_r] = idx_ref |
| 945 |
""" |
| 946 |
exec(py.code.Source(multiplicative_func_source |
| 947 |
.format(name='INT_MUL', op='*', tgt='mul', cop='*')).compile()) |
| 948 |
del multiplicative_func_source |
| 949 |
|
| 950 |
array_access_source = """ |
| 951 |
def operation_{name}(self, op, node): |
| 952 |
descr = op.getdescr() |
| 953 |
idx_ref = self.get_or_create(op.getarg(1)) |
| 954 |
if descr and descr.is_array_of_primitives(): |
| 955 |
node.memory_ref = MemoryRef(op, idx_ref, {raw_access}) |
| 956 |
self.memory_refs[node] = node.memory_ref |
| 957 |
""" |
| 958 |
exec(py.code.Source(array_access_source |
| 959 |
.format(name='RAW_LOAD_I',raw_access=True)).compile()) |
| 960 |
exec(py.code.Source(array_access_source |
| 961 |
.format(name='RAW_LOAD_F',raw_access=True)).compile()) |
| 962 |
exec(py.code.Source(array_access_source |
| 963 |
.format(name='RAW_STORE',raw_access=True)).compile()) |
| 964 |
exec(py.code.Source(array_access_source |
| 965 |
.format(name='GETARRAYITEM_RAW_I',raw_access=False)).compile()) |
| 966 |
exec(py.code.Source(array_access_source |
| 967 |
.format(name='GETARRAYITEM_RAW_F',raw_access=False)).compile()) |
| 968 |
exec(py.code.Source(array_access_source |
| 969 |
.format(name='SETARRAYITEM_RAW',raw_access=False)).compile()) |
| 970 |
exec(py.code.Source(array_access_source |
| 971 |
.format(name='GETARRAYITEM_GC_I',raw_access=False)).compile()) |
| 972 |
exec(py.code.Source(array_access_source |
| 973 |
.format(name='GETARRAYITEM_GC_F',raw_access=False)).compile()) |
| 974 |
exec(py.code.Source(array_access_source |
| 975 |
.format(name='SETARRAYITEM_GC',raw_access=False)).compile()) |
| 976 |
del array_access_source |
| 977 |
integral_dispatch_opt = make_dispatcher_method(IntegralForwardModification, 'operation_') |
| 978 |
IntegralForwardModification.inspect_operation = integral_dispatch_opt |
| 979 |
del integral_dispatch_opt |
| 980 |
|
| 981 |
class IndexVar(AbstractValue): |
| 982 |
""" IndexVar is an AbstractValue only to ensure that a box can be assigned |
| 983 |
to the same variable as an index var. |
| 984 |
""" |
| 985 |
def __init__(self, var, coeff_mul=1, coeff_div=1, constant=0): |
| 986 |
self.var = var |
| 987 |
self.coefficient_mul = coeff_mul |
| 988 |
self.coefficient_div = coeff_div |
| 989 |
self.constant = constant |
| 990 |
|
| 991 |
self.next_nonconst = None |
| 992 |
self.current_end = None |
| 993 |
|
| 994 |
def calculated_by(self, op): |
| 995 |
|
| 996 |
|
| 997 |
if op.getopnum() == rop.INT_ADD: |
| 998 |
a0 = op.getarg(0) |
| 999 |
a1 = op.getarg(1) |
| 1000 |
if a0 is self.var and a1.is_constant() and a1.getint() == self.constant: |
| 1001 |
return True |
| 1002 |
if a1 is self.var and a0.is_constant() and a0.getint() == self.constant: |
| 1003 |
return True |
| 1004 |
if op.getopnum() == rop.INT_SUB: |
| 1005 |
a0 = op.getarg(0) |
| 1006 |
a1 = op.getarg(1) |
| 1007 |
if a0 is self.var and a1.is_constant() and a1.getint() == self.constant: |
| 1008 |
return True |
| 1009 |
return False |
| 1010 |
|
| 1011 |
def stride_const(self): |
| 1012 |
return self.next_nonconst is None |
| 1013 |
|
| 1014 |
def add_const(self, number): |
| 1015 |
if self.current_end is None: |
| 1016 |
self.constant += number |
| 1017 |
else: |
| 1018 |
self.current_end.constant += number |
| 1019 |
|
| 1020 |
def set_next_nonconst_mod(self, idxvar): |
| 1021 |
if self.current_end is None: |
| 1022 |
self.next_nonconst = idxvar |
| 1023 |
else: |
| 1024 |
self.current_end.next_nonconst = idxvar |
| 1025 |
self.current_end = idxvar |
| 1026 |
|
| 1027 |
def getvariable(self): |
| 1028 |
return self.var |
| 1029 |
|
| 1030 |
def is_identity(self): |
| 1031 |
return self.coefficient_mul == 1 and \ |
| 1032 |
self.coefficient_div == 1 and \ |
| 1033 |
self.constant == 0 |
| 1034 |
|
| 1035 |
def clone(self): |
| 1036 |
c = IndexVar(self.var) |
| 1037 |
c.coefficient_mul = self.coefficient_mul |
| 1038 |
c.coefficient_div = self.coefficient_div |
| 1039 |
c.constant = self.constant |
| 1040 |
return c |
| 1041 |
|
| 1042 |
def same_variable(self, other): |
| 1043 |
assert isinstance(other, IndexVar) |
| 1044 |
return other.var == self.var |
| 1045 |
|
| 1046 |
def same_mulfactor(self, other): |
| 1047 |
coeff = self.coefficient_mul == other.coefficient_mul |
| 1048 |
coeff = coeff and (self.coefficient_div == other.coefficient_div) |
| 1049 |
if not coeff: |
| 1050 |
|
| 1051 |
selfmod = self.coefficient_mul % self.coefficient_div |
| 1052 |
othermod = other.coefficient_mul % other.coefficient_div |
| 1053 |
if selfmod == 0 and othermod == 0: |
| 1054 |
|
| 1055 |
selfdiv = self.coefficient_mul // self.coefficient_div |
| 1056 |
otherdiv = other.coefficient_mul // other.coefficient_div |
| 1057 |
coeff = selfdiv == otherdiv |
| 1058 |
return coeff |
| 1059 |
|
| 1060 |
def constant_diff(self, other): |
| 1061 |
""" calculates the difference as a second parameter """ |
| 1062 |
assert isinstance(other, IndexVar) |
| 1063 |
return self.constant - other.constant |
| 1064 |
|
| 1065 |
def get_operations(self): |
| 1066 |
var = self.var |
| 1067 |
tolist = [] |
| 1068 |
if self.coefficient_mul != 1: |
| 1069 |
args = [var, ConstInt(self.coefficient_mul)] |
| 1070 |
var = ResOperation(rop.INT_MUL, args) |
| 1071 |
tolist.append(var) |
| 1072 |
if self.coefficient_div != 1: |
| 1073 |
assert 0 |
| 1074 |
|
| 1075 |
if self.constant > 0: |
| 1076 |
args = [var, ConstInt(self.constant)] |
| 1077 |
var = ResOperation(rop.INT_ADD, args) |
| 1078 |
tolist.append(var) |
| 1079 |
if self.constant < 0: |
| 1080 |
args = [var, ConstInt(-self.constant)] |
| 1081 |
var = ResOperation(rop.INT_SUB, args) |
| 1082 |
tolist.append(var) |
| 1083 |
return tolist |
| 1084 |
|
| 1085 |
def emit_operations(self, opt, result_box=None): |
| 1086 |
var = self.var |
| 1087 |
if self.is_identity(): |
| 1088 |
return var |
| 1089 |
last = None |
| 1090 |
for op in self.get_operations(): |
| 1091 |
opt.emit_operation(op) |
| 1092 |
last = op |
| 1093 |
return last |
| 1094 |
|
| 1095 |
def compare(self, other): |
| 1096 |
""" Returns if the two are compareable as a first result |
| 1097 |
and a number (-1,0,1) of the ordering |
| 1098 |
""" |
| 1099 |
coeff = self.coefficient_mul == other.coefficient_mul |
| 1100 |
coeff = coeff and (self.coefficient_div == other.coefficient_div) |
| 1101 |
if not coeff: |
| 1102 |
|
| 1103 |
selfmod = self.coefficient_mul % self.coefficient_div |
| 1104 |
othermod = other.coefficient_mul % other.coefficient_div |
| 1105 |
if selfmod == 0 and othermod == 0: |
| 1106 |
|
| 1107 |
selfdiv = self.coefficient_mul // self.coefficient_div |
| 1108 |
otherdiv = other.coefficient_mul // other.coefficient_div |
| 1109 |
coeff = selfdiv == otherdiv |
| 1110 |
|
| 1111 |
if not coeff: |
| 1112 |
return False, 0 |
| 1113 |
|
| 1114 |
c = (self.constant - other.constant) |
| 1115 |
svar = self.var |
| 1116 |
ovar = other.var |
| 1117 |
if isinstance(svar, ConstInt) and isinstance(ovar, ConstInt): |
| 1118 |
return True, (svar.getint() - ovar.getint()) |
| 1119 |
if svar.same_box(ovar): |
| 1120 |
return True, c |
| 1121 |
return False, 0 |
| 1122 |
|
| 1123 |
def __eq__(self, other): |
| 1124 |
if not self.same_variable(other): |
| 1125 |
return False |
| 1126 |
if not self.same_mulfactor(other): |
| 1127 |
return False |
| 1128 |
return self.constant_diff(other) == 0 |
| 1129 |
|
| 1130 |
def __ne__(self, other): |
| 1131 |
return not self.__eq__(other) |
| 1132 |
|
| 1133 |
def __repr__(self): |
| 1134 |
if self.is_identity(): |
| 1135 |
return 'idx(%s)' % (self.var,) |
| 1136 |
|
| 1137 |
return 'idx(%s*(%s/%s)+%s)' % (self.var, self.coefficient_mul, |
| 1138 |
self.coefficient_div, self.constant) |
| 1139 |
|
| 1140 |
class MemoryRef(object): |
| 1141 |
""" a memory reference to an array object. IntegralForwardModification is able |
| 1142 |
to propagate changes to this object if applied in backwards direction. |
| 1143 |
Example: |
| 1144 |
|
| 1145 |
i1 = int_add(i0,1) |
| 1146 |
i2 = int_mul(i1,2) |
| 1147 |
setarrayitem_gc(p0, i2, 1, ...) |
| 1148 |
|
| 1149 |
will result in the linear combination i0 * (2/1) + 2 |
| 1150 |
""" |
| 1151 |
def __init__(self, op, index_var, raw_access=False): |
| 1152 |
assert op.getdescr() is not None |
| 1153 |
self.array = op.getarg(0) |
| 1154 |
self.descr = op.getdescr() |
| 1155 |
self.index_var = index_var |
| 1156 |
self.raw_access = raw_access |
| 1157 |
|
| 1158 |
def is_adjacent_to(self, other): |
| 1159 |
""" this is a symmetric relation """ |
| 1160 |
if not self.same_array(other): |
| 1161 |
return False |
| 1162 |
if not self.index_var.same_variable(other.index_var): |
| 1163 |
return False |
| 1164 |
if not self.index_var.same_mulfactor(other.index_var): |
| 1165 |
return False |
| 1166 |
stride = self.stride() |
| 1167 |
return abs(self.index_var.constant_diff(other.index_var)) - stride == 0 |
| 1168 |
|
| 1169 |
def is_adjacent_after(self, other): |
| 1170 |
""" the asymetric relation to is_adjacent_to """ |
| 1171 |
if not self.same_array(other): |
| 1172 |
return False |
| 1173 |
if not self.index_var.same_variable(other.index_var): |
| 1174 |
return False |
| 1175 |
if not self.index_var.same_mulfactor(other.index_var): |
| 1176 |
return False |
| 1177 |
stride = self.stride() |
| 1178 |
return other.index_var.constant_diff(self.index_var) == stride |
| 1179 |
|
| 1180 |
def alias(self, other): |
| 1181 |
""" is this reference an alias to other? |
| 1182 |
they can alias iff self.origin != other.origin, or their |
| 1183 |
linear combination point to the same element. |
| 1184 |
""" |
| 1185 |
assert other is not None |
| 1186 |
if not self.same_array(other): |
| 1187 |
return False |
| 1188 |
svar = self.index_var |
| 1189 |
ovar = other.index_var |
| 1190 |
if not svar.same_variable(ovar): |
| 1191 |
return True |
| 1192 |
if not svar.same_mulfactor(ovar): |
| 1193 |
return True |
| 1194 |
return abs(svar.constant_diff(ovar)) < self.stride() |
| 1195 |
|
| 1196 |
def same_array(self, other): |
| 1197 |
return self.array is other.array and self.descr == other.descr |
| 1198 |
|
| 1199 |
def __eq__(self, other): |
| 1200 |
""" NOT_RPYTHON """ |
| 1201 |
if not self.same_array(other): |
| 1202 |
return False |
| 1203 |
if not self.index_var.same_variable(other.index_var): |
| 1204 |
return False |
| 1205 |
if not self.index_var.same_mulfactor(other.index_var): |
| 1206 |
return False |
| 1207 |
stride = self.stride() |
| 1208 |
return other.index_var.constant_diff(self.index_var) == 0 |
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
def stride(self): |
| 1214 |
""" the stride in bytes """ |
| 1215 |
if not self.raw_access: |
| 1216 |
return 1 |
| 1217 |
return self.descr.get_item_size_in_bytes() |
| 1218 |
|
| 1219 |
def __repr__(self): |
| 1220 |
return 'MemRef(%s,%s)' % (self.array, self.index_var) |