2 * Register - track pseudo usage, maybe eventually try to do register
5 * Copyright (C) 2004 Linus Torvalds
11 #include "expression.h"
12 #include "linearize.h"
15 static void phi_defines(struct instruction
* phi_node
, pseudo_t target
,
16 void (*defines
)(struct basic_block
*, pseudo_t
))
19 FOR_EACH_PTR(phi_node
->phi_list
, phi
) {
20 struct instruction
*def
;
26 defines(def
->bb
, target
);
27 } END_FOR_EACH_PTR(phi
);
30 static void asm_liveness(struct basic_block
*bb
, struct instruction
*insn
,
31 void (*def
)(struct basic_block
*, pseudo_t
),
32 void (*use
)(struct basic_block
*, pseudo_t
))
34 struct asm_constraint
*entry
;
36 FOR_EACH_PTR(insn
->asm_rules
->inputs
, entry
) {
37 use(bb
, entry
->pseudo
);
38 } END_FOR_EACH_PTR(entry
);
40 FOR_EACH_PTR(insn
->asm_rules
->outputs
, entry
) {
41 def(bb
, entry
->pseudo
);
42 } END_FOR_EACH_PTR(entry
);
45 static void track_instruction_usage(struct basic_block
*bb
, struct instruction
*insn
,
46 void (*def
)(struct basic_block
*, pseudo_t
),
47 void (*use
)(struct basic_block
*, pseudo_t
))
51 #define USES(x) use(bb, insn->x)
52 #define DEFINES(x) def(bb, insn->x)
54 switch (insn
->opcode
) {
59 case OP_BR
: case OP_SWITCH
:
68 case OP_BINARY
... OP_BINARY_END
:
69 case OP_BINCMP
... OP_BINCMP_END
:
70 USES(src1
); USES(src2
); DEFINES(target
);
74 case OP_NOT
: case OP_NEG
:
75 USES(src1
); DEFINES(target
);
79 USES(src1
); USES(src2
); USES(src3
); DEFINES(target
);
84 USES(src
); DEFINES(target
);
88 USES(src
); USES(target
);
96 USES(symbol
); DEFINES(target
);
101 /* Phi-nodes are "backwards" nodes. Their def doesn't matter */
102 phi_defines(insn
, insn
->target
, def
);
107 * We don't care about the phi-source define, they get set
108 * up and expanded by the OP_PHI
117 USES(src
); DEFINES(target
);
122 if (insn
->target
!= VOID
)
124 FOR_EACH_PTR(insn
->arguments
, pseudo
) {
126 } END_FOR_EACH_PTR(pseudo
);
130 USES(base
); DEFINES(target
);
134 asm_liveness(bb
, insn
, def
, use
);
138 USES(src1
); USES(src2
); USES(src3
);
147 case OP_GET_ELEMENT_PTR
:
158 int pseudo_in_list(struct pseudo_list
*list
, pseudo_t pseudo
)
161 FOR_EACH_PTR(list
,old
) {
164 } END_FOR_EACH_PTR(old
);
168 static int liveness_changed
;
170 static void add_pseudo_exclusive(struct pseudo_list
**list
, pseudo_t pseudo
)
172 if (!pseudo_in_list(*list
, pseudo
)) {
173 liveness_changed
= 1;
174 add_pseudo(list
, pseudo
);
178 static inline int trackable_pseudo(pseudo_t pseudo
)
180 return pseudo
&& (pseudo
->type
== PSEUDO_REG
|| pseudo
->type
== PSEUDO_ARG
);
183 static void insn_uses(struct basic_block
*bb
, pseudo_t pseudo
)
185 if (trackable_pseudo(pseudo
)) {
186 struct instruction
*def
= pseudo
->def
;
187 if (pseudo
->type
!= PSEUDO_REG
|| def
->bb
!= bb
|| def
->opcode
== OP_PHI
)
188 add_pseudo_exclusive(&bb
->needs
, pseudo
);
192 static void insn_defines(struct basic_block
*bb
, pseudo_t pseudo
)
194 assert(trackable_pseudo(pseudo
));
195 add_pseudo(&bb
->defines
, pseudo
);
198 static void track_bb_liveness(struct basic_block
*bb
)
202 FOR_EACH_PTR(bb
->needs
, needs
) {
203 struct basic_block
*parent
;
204 FOR_EACH_PTR(bb
->parents
, parent
) {
205 if (!pseudo_in_list(parent
->defines
, needs
)) {
206 add_pseudo_exclusive(&parent
->needs
, needs
);
208 } END_FOR_EACH_PTR(parent
);
209 } END_FOR_EACH_PTR(needs
);
213 * We need to clear the liveness information if we
214 * are going to re-run it.
216 void clear_liveness(struct entrypoint
*ep
)
218 struct basic_block
*bb
;
220 FOR_EACH_PTR(ep
->bbs
, bb
) {
221 free_ptr_list(&bb
->needs
);
222 free_ptr_list(&bb
->defines
);
223 } END_FOR_EACH_PTR(bb
);
227 * Track inter-bb pseudo liveness. The intra-bb case
228 * is purely local information.
230 void track_pseudo_liveness(struct entrypoint
*ep
)
232 struct basic_block
*bb
;
234 /* Add all the bb pseudo usage */
235 FOR_EACH_PTR(ep
->bbs
, bb
) {
236 struct instruction
*insn
;
237 FOR_EACH_PTR(bb
->insns
, insn
) {
240 assert(insn
->bb
== bb
);
241 track_instruction_usage(bb
, insn
, insn_defines
, insn_uses
);
242 } END_FOR_EACH_PTR(insn
);
243 } END_FOR_EACH_PTR(bb
);
245 /* Calculate liveness.. */
247 liveness_changed
= 0;
248 FOR_EACH_PTR_REVERSE(ep
->bbs
, bb
) {
249 track_bb_liveness(bb
);
250 } END_FOR_EACH_PTR_REVERSE(bb
);
251 } while (liveness_changed
);
253 /* Remove the pseudos from the "defines" list that are used internally */
254 FOR_EACH_PTR(ep
->bbs
, bb
) {
256 FOR_EACH_PTR(bb
->defines
, def
) {
257 struct basic_block
*child
;
258 FOR_EACH_PTR(bb
->children
, child
) {
259 if (pseudo_in_list(child
->needs
, def
))
261 } END_FOR_EACH_PTR(child
);
262 DELETE_CURRENT_PTR(def
);
265 } END_FOR_EACH_PTR(def
);
266 PACK_PTR_LIST(&bb
->defines
);
267 } END_FOR_EACH_PTR(bb
);
270 static void merge_pseudo_list(struct pseudo_list
*src
, struct pseudo_list
**dest
)
273 FOR_EACH_PTR(src
, pseudo
) {
274 add_pseudo_exclusive(dest
, pseudo
);
275 } END_FOR_EACH_PTR(pseudo
);
278 void track_phi_uses(struct instruction
*insn
)
281 FOR_EACH_PTR(insn
->phi_list
, phi
) {
282 struct instruction
*def
;
283 if (phi
== VOID
|| !phi
->def
)
286 assert(def
->opcode
== OP_PHISOURCE
);
287 add_ptr_list(&def
->phi_users
, insn
);
288 } END_FOR_EACH_PTR(phi
);
291 static void track_bb_phi_uses(struct basic_block
*bb
)
293 struct instruction
*insn
;
294 FOR_EACH_PTR(bb
->insns
, insn
) {
295 if (insn
->bb
&& insn
->opcode
== OP_PHI
)
296 track_phi_uses(insn
);
297 } END_FOR_EACH_PTR(insn
);
300 static struct pseudo_list
**live_list
;
301 static struct pseudo_list
*dead_list
;
303 static void death_def(struct basic_block
*bb
, pseudo_t pseudo
)
307 static void death_use(struct basic_block
*bb
, pseudo_t pseudo
)
309 if (trackable_pseudo(pseudo
) && !pseudo_in_list(*live_list
, pseudo
)) {
310 add_pseudo(&dead_list
, pseudo
);
311 add_pseudo(live_list
, pseudo
);
315 static void track_pseudo_death_bb(struct basic_block
*bb
)
317 struct pseudo_list
*live
= NULL
;
318 struct basic_block
*child
;
319 struct instruction
*insn
;
321 FOR_EACH_PTR(bb
->children
, child
) {
322 merge_pseudo_list(child
->needs
, &live
);
323 } END_FOR_EACH_PTR(child
);
326 FOR_EACH_PTR_REVERSE(bb
->insns
, insn
) {
331 track_instruction_usage(bb
, insn
, death_def
, death_use
);
334 FOR_EACH_PTR(dead_list
, dead
) {
335 struct instruction
*deathnote
= __alloc_instruction(0);
337 deathnote
->opcode
= OP_DEATHNOTE
;
338 deathnote
->target
= dead
;
339 INSERT_CURRENT(deathnote
, insn
);
340 } END_FOR_EACH_PTR(dead
);
341 free_ptr_list(&dead_list
);
343 } END_FOR_EACH_PTR_REVERSE(insn
);
344 free_ptr_list(&live
);
347 void track_pseudo_death(struct entrypoint
*ep
)
349 struct basic_block
*bb
;
351 FOR_EACH_PTR(ep
->bbs
, bb
) {
352 track_bb_phi_uses(bb
);
353 } END_FOR_EACH_PTR(bb
);
355 FOR_EACH_PTR(ep
->bbs
, bb
) {
356 track_pseudo_death_bb(bb
);
357 } END_FOR_EACH_PTR(bb
);