| 1 |
from rpython.jit.codewriter.flatten import Register, ListOfKind, Label, TLabel |
| 2 |
from rpython.jit.codewriter.jitcode import SwitchDictDescr |
| 3 |
from rpython.rlib import objectmodel |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
def compute_liveness(ssarepr): |
| 20 |
label2alive = {} |
| 21 |
while _compute_liveness_must_continue(ssarepr, label2alive): |
| 22 |
pass |
| 23 |
remove_repeated_live(ssarepr) |
| 24 |
|
| 25 |
def _compute_liveness_must_continue(ssarepr, label2alive): |
| 26 |
alive = set() |
| 27 |
must_continue = False |
| 28 |
|
| 29 |
def follow_label(lbl): |
| 30 |
alive_at_point = label2alive.get(lbl.name, ()) |
| 31 |
alive.update(alive_at_point) |
| 32 |
|
| 33 |
for i in range(len(ssarepr.insns)-1, -1, -1): |
| 34 |
insn = ssarepr.insns[i] |
| 35 |
|
| 36 |
if isinstance(insn[0], Label): |
| 37 |
alive_at_point = label2alive.setdefault(insn[0].name, set()) |
| 38 |
prevlength = len(alive_at_point) |
| 39 |
alive_at_point.update(alive) |
| 40 |
if prevlength != len(alive_at_point): |
| 41 |
must_continue = True |
| 42 |
continue |
| 43 |
|
| 44 |
if insn[0] == '-live-': |
| 45 |
labels = [] |
| 46 |
for x in insn[1:]: |
| 47 |
if isinstance(x, Register): |
| 48 |
alive.add(x) |
| 49 |
elif isinstance(x, TLabel): |
| 50 |
follow_label(x) |
| 51 |
labels.append(x) |
| 52 |
ssarepr.insns[i] = insn[:1] + tuple(alive) + tuple(labels) |
| 53 |
continue |
| 54 |
|
| 55 |
if insn[0] == '---': |
| 56 |
alive = set() |
| 57 |
continue |
| 58 |
|
| 59 |
args = insn[1:] |
| 60 |
|
| 61 |
if len(args) >= 2 and args[-2] == '->': |
| 62 |
reg = args[-1] |
| 63 |
assert isinstance(reg, Register) |
| 64 |
alive.discard(reg) |
| 65 |
args = args[:-2] |
| 66 |
|
| 67 |
for x in args: |
| 68 |
if isinstance(x, Register): |
| 69 |
alive.add(x) |
| 70 |
elif isinstance(x, ListOfKind): |
| 71 |
for y in x: |
| 72 |
if isinstance(y, Register): |
| 73 |
alive.add(y) |
| 74 |
elif isinstance(x, TLabel): |
| 75 |
follow_label(x) |
| 76 |
elif isinstance(x, SwitchDictDescr): |
| 77 |
for key, label in x._labels: |
| 78 |
follow_label(label) |
| 79 |
|
| 80 |
return must_continue |
| 81 |
|
| 82 |
def remove_repeated_live(ssarepr): |
| 83 |
last_i_pos = None |
| 84 |
i = 0 |
| 85 |
res = [] |
| 86 |
while i < len(ssarepr.insns): |
| 87 |
insn = ssarepr.insns[i] |
| 88 |
if insn[0] != '-live-': |
| 89 |
res.append(insn) |
| 90 |
i += 1 |
| 91 |
continue |
| 92 |
last_i_pos = i |
| 93 |
i += 1 |
| 94 |
labels = [] |
| 95 |
lives = [insn] |
| 96 |
|
| 97 |
while i < len(ssarepr.insns): |
| 98 |
next = ssarepr.insns[i] |
| 99 |
if next[0] == '-live-': |
| 100 |
lives.append(next) |
| 101 |
i += 1 |
| 102 |
elif isinstance(next[0], Label): |
| 103 |
labels.append(next) |
| 104 |
i += 1 |
| 105 |
else: |
| 106 |
break |
| 107 |
if len(lives) == 1: |
| 108 |
res.extend(labels) |
| 109 |
res.append(lives[0]) |
| 110 |
continue |
| 111 |
liveset = set() |
| 112 |
for live in lives: |
| 113 |
liveset.update(live[1:]) |
| 114 |
res.extend(labels) |
| 115 |
res.append(('-live-', ) + tuple(sorted(liveset))) |
| 116 |
ssarepr.insns = res |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
OFFSET_SIZE = 2 |
| 126 |
|
| 127 |
def encode_offset(pos, code): |
| 128 |
assert OFFSET_SIZE == 2 |
| 129 |
code.append(chr(pos & 0xff)) |
| 130 |
code.append(chr((pos >> 8) & 0xff)) |
| 131 |
assert (pos >> 16) == 0 |
| 132 |
|
| 133 |
def decode_offset(jitcode, pc): |
| 134 |
assert OFFSET_SIZE == 2 |
| 135 |
return (ord(jitcode[pc]) | |
| 136 |
(ord(jitcode[pc + 1]) << 8)) |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
def encode_liveness(live): |
| 148 |
live = sorted(live) |
| 149 |
liveness = [] |
| 150 |
offset = 0 |
| 151 |
char = 0 |
| 152 |
i = 0 |
| 153 |
while i < len(live): |
| 154 |
x = ord(live[i]) |
| 155 |
x -= offset |
| 156 |
if x >= 8: |
| 157 |
liveness.append(chr(char)) |
| 158 |
char = 0 |
| 159 |
offset += 8 |
| 160 |
continue |
| 161 |
char |= 1 << x |
| 162 |
assert 0 <= char < 256 |
| 163 |
i += 1 |
| 164 |
if char: |
| 165 |
liveness.append(chr(char)) |
| 166 |
return "".join(liveness) |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
class LivenessIterator(object): |
| 171 |
@objectmodel.always_inline |
| 172 |
def __init__(self, offset, length, all_liveness): |
| 173 |
self.all_liveness = all_liveness |
| 174 |
self.offset = offset |
| 175 |
assert length |
| 176 |
self.length = length |
| 177 |
self.curr_byte = 0 |
| 178 |
self.count = 0 |
| 179 |
|
| 180 |
@objectmodel.always_inline |
| 181 |
def __iter__(self): |
| 182 |
return self |
| 183 |
|
| 184 |
@objectmodel.always_inline |
| 185 |
def next(self): |
| 186 |
if not self.length: |
| 187 |
raise StopIteration |
| 188 |
self.length -= 1 |
| 189 |
count = self.count |
| 190 |
all_liveness = self.all_liveness |
| 191 |
curr_byte = self.curr_byte |
| 192 |
|
| 193 |
while 1: |
| 194 |
if (count & 7) == 0: |
| 195 |
curr_byte = self.curr_byte = ord(all_liveness[self.offset]) |
| 196 |
self.offset += 1 |
| 197 |
if (curr_byte >> (count & 7)) & 1: |
| 198 |
self.count = count + 1 |
| 199 |
return count |
| 200 |
count += 1 |
| 201 |
|