Debian Codegraph @

rpython/jit/codewriter/liveness.py

raw symbols SCIP
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 # Some instructions require liveness information (the ones that can end up
6 # in generate_guard() in pyjitpl.py). This is done by putting special
7 # space operations called '-live-' in the graph. They turn into '-live-'
8 # operation in the ssarepr. Then the present module expands the arguments
9 # of the '-live-' operations to also include all values that are alive at
10 # this point (written to before, and read afterwards). You can also force
11 # extra variables to be alive by putting them as args of the '-live-'
12 # operation in the first place.
13
14 # For this to work properly, a special operation called '---' must be
15 # used to mark unreachable places (e.g. just after a 'goto').
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 # collect lives and labels
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 # helper functions for compactly encoding and decoding liveness info
121
122 # liveness is encoded as a 2 byte offset into the single string all_liveness
123 # (which is stored on the metainterp_sd)
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 # within the string of all_liveness, we encode the bitsets of which of the 256
140 # registers are live as follows: first three byte with the number of set bits
141 # for each of the categories ints, refs, floats followed by the necessary
142 # number of bytes to store them (this number of bytes is implicit), for each of
143 # the categories
144 # | len live_i | len live_r | len live_f
145 # | bytes for live_i | bytes for live_r | bytes for live_f
146
147 def encode_liveness(live):
148 live = sorted(live) # ints in range(256)
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 #@objectmodel.never_allocate # can't be enabled because of some tests that
169 # don't optimize
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 # find next bit set
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