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
*, struct instruction
*, pseudo_t
))
19 FOR_EACH_PTR(phi_node
->phi_list
, phi
) {
20 struct instruction
*def
;
26 if (def
->opcode
== OP_PHI
) {
27 phi_defines(def
, target
, defines
);
30 defines(def
->bb
, phi
->def
, target
);
31 } END_FOR_EACH_PTR(phi
);
34 static void asm_liveness(struct basic_block
*bb
, struct instruction
*insn
,
35 void (*def
)(struct basic_block
*, struct instruction
*, pseudo_t
),
36 void (*use
)(struct basic_block
*, struct instruction
*, pseudo_t
))
38 struct asm_constraint
*entry
;
40 FOR_EACH_PTR(insn
->asm_rules
->inputs
, entry
) {
41 use(bb
, insn
, entry
->pseudo
);
42 } END_FOR_EACH_PTR(entry
);
44 FOR_EACH_PTR(insn
->asm_rules
->outputs
, entry
) {
45 def(bb
, insn
, entry
->pseudo
);
46 } END_FOR_EACH_PTR(entry
);
49 static void track_instruction_usage(struct basic_block
*bb
, struct instruction
*insn
,
50 void (*def
)(struct basic_block
*, struct instruction
*, pseudo_t
),
51 void (*use
)(struct basic_block
*, struct instruction
*, pseudo_t
))
55 #define USES(x) use(bb, insn, insn->x)
56 #define DEFINES(x) def(bb, insn, insn->x)
58 switch (insn
->opcode
) {
63 case OP_BR
: case OP_SWITCH
:
72 case OP_BINARY
... OP_BINARY_END
:
73 case OP_BINCMP
... OP_BINCMP_END
:
74 USES(src1
); USES(src2
); DEFINES(target
);
78 case OP_NOT
: case OP_NEG
:
79 USES(src1
); DEFINES(target
);
83 USES(src1
); USES(src2
); USES(src3
); DEFINES(target
);
88 USES(src
); DEFINES(target
);
92 USES(src
); USES(target
);
100 USES(symbol
); DEFINES(target
);
105 /* Phi-nodes are "backwards" nodes. Their def doesn't matter */
106 phi_defines(insn
, insn
->target
, def
);
111 * We don't care about the phi-source define, they get set
112 * up and expanded by the OP_PHI
121 USES(src
); DEFINES(target
);
126 if (insn
->target
!= VOID
)
128 FOR_EACH_PTR(insn
->arguments
, pseudo
) {
129 use(bb
, insn
, pseudo
);
130 } END_FOR_EACH_PTR(pseudo
);
134 USES(base
); DEFINES(target
);
138 asm_liveness(bb
, insn
, def
, use
);
142 USES(src1
); USES(src2
); USES(src3
);
151 case OP_GET_ELEMENT_PTR
:
162 int pseudo_in_list(struct pseudo_list
*list
, pseudo_t pseudo
)
165 FOR_EACH_PTR(list
,old
) {
168 } END_FOR_EACH_PTR(old
);
172 static int liveness_changed
;
174 static void add_pseudo_exclusive(struct pseudo_list
**list
, pseudo_t pseudo
)
176 if (!pseudo_in_list(*list
, pseudo
)) {
177 liveness_changed
= 1;
178 add_pseudo(list
, pseudo
);
182 static inline int trackable_pseudo(pseudo_t pseudo
)
184 return pseudo
&& (pseudo
->type
== PSEUDO_REG
|| pseudo
->type
== PSEUDO_ARG
);
187 static void insn_uses(struct basic_block
*bb
, struct instruction
*insn
, pseudo_t pseudo
)
189 if (trackable_pseudo(pseudo
)) {
190 struct instruction
*def
= pseudo
->def
;
191 if (pseudo
->type
!= PSEUDO_REG
|| def
->bb
!= bb
|| def
->opcode
== OP_PHI
)
192 add_pseudo_exclusive(&bb
->needs
, pseudo
);
196 static void insn_defines(struct basic_block
*bb
, struct instruction
*insn
, pseudo_t pseudo
)
198 assert(trackable_pseudo(pseudo
));
199 add_pseudo(&bb
->defines
, pseudo
);
202 static void track_bb_liveness(struct basic_block
*bb
)
206 FOR_EACH_PTR(bb
->needs
, needs
) {
207 struct basic_block
*parent
;
208 FOR_EACH_PTR(bb
->parents
, parent
) {
209 if (!pseudo_in_list(parent
->defines
, needs
)) {
210 add_pseudo_exclusive(&parent
->needs
, needs
);
212 } END_FOR_EACH_PTR(parent
);
213 } END_FOR_EACH_PTR(needs
);
217 * We need to clear the liveness information if we
218 * are going to re-run it.
220 void clear_liveness(struct entrypoint
*ep
)
222 struct basic_block
*bb
;
224 FOR_EACH_PTR(ep
->bbs
, bb
) {
225 free_ptr_list(&bb
->needs
);
226 free_ptr_list(&bb
->defines
);
227 } END_FOR_EACH_PTR(bb
);
231 * Track inter-bb pseudo liveness. The intra-bb case
232 * is purely local information.
234 void track_pseudo_liveness(struct entrypoint
*ep
)
236 struct basic_block
*bb
;
238 /* Add all the bb pseudo usage */
239 FOR_EACH_PTR(ep
->bbs
, bb
) {
240 struct instruction
*insn
;
241 FOR_EACH_PTR(bb
->insns
, insn
) {
244 assert(insn
->bb
== bb
);
245 track_instruction_usage(bb
, insn
, insn_defines
, insn_uses
);
246 } END_FOR_EACH_PTR(insn
);
247 } END_FOR_EACH_PTR(bb
);
249 /* Calculate liveness.. */
251 liveness_changed
= 0;
252 FOR_EACH_PTR_REVERSE(ep
->bbs
, bb
) {
253 track_bb_liveness(bb
);
254 } END_FOR_EACH_PTR_REVERSE(bb
);
255 } while (liveness_changed
);
257 /* Remove the pseudos from the "defines" list that are used internally */
258 FOR_EACH_PTR(ep
->bbs
, bb
) {
260 FOR_EACH_PTR(bb
->defines
, def
) {
261 struct basic_block
*child
;
262 FOR_EACH_PTR(bb
->children
, child
) {
263 if (pseudo_in_list(child
->needs
, def
))
265 } END_FOR_EACH_PTR(child
);
266 DELETE_CURRENT_PTR(def
);
269 } END_FOR_EACH_PTR(def
);
270 PACK_PTR_LIST(&bb
->defines
);
271 } END_FOR_EACH_PTR(bb
);
274 static void merge_pseudo_list(struct pseudo_list
*src
, struct pseudo_list
**dest
)
277 FOR_EACH_PTR(src
, pseudo
) {
278 add_pseudo_exclusive(dest
, pseudo
);
279 } END_FOR_EACH_PTR(pseudo
);
282 void track_phi_uses(struct instruction
*insn
)
285 FOR_EACH_PTR(insn
->phi_list
, phi
) {
286 struct instruction
*def
;
287 if (phi
== VOID
|| !phi
->def
)
290 assert(def
->opcode
== OP_PHISOURCE
);
291 add_ptr_list(&def
->phi_users
, insn
);
292 } END_FOR_EACH_PTR(phi
);
295 static void track_bb_phi_uses(struct basic_block
*bb
)
297 struct instruction
*insn
;
298 FOR_EACH_PTR(bb
->insns
, insn
) {
299 if (insn
->bb
&& insn
->opcode
== OP_PHI
)
300 track_phi_uses(insn
);
301 } END_FOR_EACH_PTR(insn
);
304 static struct pseudo_list
**live_list
;
305 static struct pseudo_list
*dead_list
;
307 static void death_def(struct basic_block
*bb
, struct instruction
*insn
, pseudo_t pseudo
)
311 static void death_use(struct basic_block
*bb
, struct instruction
*insn
, pseudo_t pseudo
)
313 if (trackable_pseudo(pseudo
) && !pseudo_in_list(*live_list
, pseudo
)) {
314 add_pseudo(&dead_list
, pseudo
);
315 add_pseudo(live_list
, pseudo
);
319 static void track_pseudo_death_bb(struct basic_block
*bb
)
321 struct pseudo_list
*live
= NULL
;
322 struct basic_block
*child
;
323 struct instruction
*insn
;
325 FOR_EACH_PTR(bb
->children
, child
) {
326 merge_pseudo_list(child
->needs
, &live
);
327 } END_FOR_EACH_PTR(child
);
330 FOR_EACH_PTR_REVERSE(bb
->insns
, insn
) {
335 track_instruction_usage(bb
, insn
, death_def
, death_use
);
338 FOR_EACH_PTR(dead_list
, dead
) {
339 struct instruction
*deathnote
= __alloc_instruction(0);
341 deathnote
->opcode
= OP_DEATHNOTE
;
342 deathnote
->target
= dead
;
343 INSERT_CURRENT(deathnote
, insn
);
344 } END_FOR_EACH_PTR(dead
);
345 free_ptr_list(&dead_list
);
347 } END_FOR_EACH_PTR_REVERSE(insn
);
348 free_ptr_list(&live
);
351 void track_pseudo_death(struct entrypoint
*ep
)
353 struct basic_block
*bb
;
355 FOR_EACH_PTR(ep
->bbs
, bb
) {
356 track_bb_phi_uses(bb
);
357 } END_FOR_EACH_PTR(bb
);
359 FOR_EACH_PTR(ep
->bbs
, bb
) {
360 track_pseudo_death_bb(bb
);
361 } END_FOR_EACH_PTR(bb
);