2 * Interblock liveness analysis.
3 * @author Sebastian Hack
11 #include "iredges_t.h"
13 #include "irprintf_t.h"
18 #include "besched_t.h"
20 #define DBG_MODULE "firm.be.liveness"
22 #define LV_STD_SIZE 128
23 #define LV_USE_BINARY_SEARCH
24 #undef LV_INTESIVE_CHECKS
26 int (be_lv_next_irn)(const struct _be_lv_t *lv, const ir_node *bl, unsigned flags, int i)
28 return _be_lv_next_irn(lv, bl, flags, i);
31 const ir_node * (be_lv_get_irn)(const struct _be_lv_t *lv, const ir_node *bl, int i)
33 return _be_lv_get_irn(lv, bl, i);
36 int (be_is_live_in)(const be_lv_t *lv, const ir_node *block, const ir_node *irn)
38 return _be_is_live_xxx(lv, block, irn, be_lv_state_in);
41 int (be_is_live_out)(const be_lv_t *lv, const ir_node *block, const ir_node *irn)
43 return _be_is_live_xxx(lv, block, irn, be_lv_state_out);
46 int (be_is_live_end)(const be_lv_t *lv, const ir_node *block, const ir_node *irn)
48 return _be_is_live_xxx(lv, block, irn, be_lv_state_end);
52 #ifdef LV_USE_BINARY_SEARCH
53 static INLINE unsigned _be_liveness_bsearch(struct _be_lv_info_t *arr, unsigned idx)
55 struct _be_lv_info_t *payload = arr + 1;
57 unsigned n = arr[0].u.head.n_members;
66 if(idx < payload[0].u.node.idx)
69 if(idx > payload[n - 1].u.node.idx)
73 /* start a binary search for the requested node. */
75 int md = lo + ((hi - lo) >> 1);
76 unsigned md_idx = payload[md].u.node.idx;
84 assert(payload[res].u.node.idx == idx);
91 #ifdef LV_INTESIVE_CHECKS
94 for(i = res; i < n; ++i)
95 assert(payload[i].u.node.idx >= idx);
97 for(i = 0; i < res; ++i)
98 assert(payload[i].u.node.idx < idx);
108 * This function searches linearily for the node in the array.
110 static INLINE unsigned _be_liveness_bsearch(struct _be_lv_info_t *arr, unsigned idx) {
111 unsigned n = arr[0].u.head.n_members;
114 for(i = 0; i < n; ++i) {
115 if(arr[i + 1].u.node.idx == idx)
123 struct _be_lv_info_node_t *be_lv_get(const struct _be_lv_t *li, const ir_node *bl, const ir_node *irn)
125 struct _be_lv_info_t *irn_live = phase_get_irn_data(&li->ph, bl);
128 unsigned idx = get_irn_idx(irn);
130 /* Get the position of the index in the array. */
131 int pos = _be_liveness_bsearch(irn_live, idx);
133 /* Get the record in question. 1 must be added, since the first record contains information about the array and must be skipped. */
134 struct _be_lv_info_node_t *res = &irn_live[pos + 1].u.node;
136 /* Check, if the irn is in deed in the array. */
144 static struct _be_lv_info_node_t *be_lv_get_or_set(struct _be_lv_t *li, ir_node *bl, ir_node *irn)
146 struct _be_lv_info_t *irn_live = phase_get_or_set_irn_data(&li->ph, bl);
148 unsigned idx = get_irn_idx(irn);
150 /* Get the position of the index in the array. */
151 unsigned pos = _be_liveness_bsearch(irn_live, idx);
153 /* Get the record in question. 1 must be added, since the first record contains information about the array and must be skipped. */
154 struct _be_lv_info_node_t *res = &irn_live[pos + 1].u.node;
156 /* Check, if the irn is in deed in the array. */
157 if(res->idx != idx) {
158 struct _be_lv_info_t *payload;
159 unsigned n_members = irn_live[0].u.head.n_members;
160 unsigned n_size = irn_live[0].u.head.n_size;
163 if(n_members == n_size - 1) {
164 unsigned new_size = 2 * n_size * sizeof(irn_live[0]);
165 struct _be_lv_info_t *nw = phase_alloc(&li->ph, new_size);
166 memcpy(nw, irn_live, new_size);
167 nw[0].u.head.n_size = new_size;
169 phase_set_irn_data(&li->ph, irn, nw);
172 payload = &irn_live[1];
173 for(i = n_members; i > pos; --i) {
174 payload[i] = payload[i - 1];
177 ++irn_live[0].u.head.n_members;
179 res = &payload[pos].u.node;
184 #ifdef LV_INTESIVE_CHECKS
187 unsigned n = irn_live[0].u.head.n_members;
189 struct _be_lv_info_t *payload = &irn_live[1];
191 for(i = 0; i < n; ++i) {
192 assert(payload[i].u.node.idx >= last);
193 last = payload[i].u.node.idx;
202 * Removes a node from the list of live variables of a block.
203 * @return 1 if the node was live at that block, 0 if not.
205 static int be_lv_remove(struct _be_lv_t *li, ir_node *bl, ir_node *irn)
207 struct _be_lv_info_t *irn_live = phase_get_irn_data(&li->ph, bl);
210 unsigned n = irn_live[0].u.head.n_members;
211 unsigned idx = get_irn_idx(irn);
212 unsigned pos = _be_liveness_bsearch(irn_live, idx);
213 struct _be_lv_info_t *payload = irn_live + 1;
214 struct _be_lv_info_node_t *res = &payload[pos].u.node;
216 /* The node is in deed in the block's array. Let's remove it. */
217 if(res->idx == idx) {
220 for(i = pos + 1; i < n; ++i)
221 payload[i - 1] = payload[i];
223 payload[n - 1].u.node.idx = 0;
224 payload[n - 1].u.node.flags = 0;
226 --irn_live[0].u.head.n_members;
227 DBG((li->dbg, LEVEL_3, "\tdeleting %+F from %+F at pos %d\n", irn, bl, pos));
235 static void register_node(be_lv_t *lv, const ir_node *irn)
237 unsigned idx = get_irn_idx(irn);
238 if(idx >= bitset_size(lv->nodes)) {
239 bitset_t *nw = bitset_malloc(2 * idx);
240 bitset_copy(nw, lv->nodes);
241 bitset_free(lv->nodes);
245 bitset_set(lv->nodes, idx);
249 * Mark a node as live-in in a block.
251 static INLINE void mark_live_in(be_lv_t *lv, ir_node *block, ir_node *irn)
253 struct _be_lv_info_node_t *n = be_lv_get_or_set(lv, block, irn);
254 DBG((lv->dbg, LEVEL_2, "marking %+F live in at %+F\n", irn, block));
255 n->flags |= be_lv_state_in;
256 register_node(lv, irn);
260 * Mark a node as live-out in a block.
262 static INLINE void mark_live_out(be_lv_t *lv, ir_node *block, ir_node *irn)
264 struct _be_lv_info_node_t *n = be_lv_get_or_set(lv, block, irn);
265 DBG((lv->dbg, LEVEL_2, "marking %+F live out at %+F\n", irn, block));
266 n->flags |= be_lv_state_out | be_lv_state_end;
267 register_node(lv, irn);
271 * Mark a node as live-end in a block.
273 static INLINE void mark_live_end(be_lv_t *lv, ir_node *block, ir_node *irn)
275 struct _be_lv_info_node_t *n = be_lv_get_or_set(lv, block, irn);
276 DBG((lv->dbg, LEVEL_2, "marking %+F live end at %+F\n", irn, block));
277 n->flags |= be_lv_state_end;
278 register_node(lv, irn);
282 * Mark a node (value) live out at a certain block. Do this also
283 * transitively, i.e. if the block is not the block of the value's
284 * definition, all predecessors are also marked live.
285 * @param def The node (value).
286 * @param block The block to mark the value live out of.
287 * @param visited A set were all visited blocks are recorded.
288 * @param is_true_out Is the node real out there or only live at the end
291 static void live_end_at_block(be_lv_t *lv, ir_node *def, ir_node *block, bitset_t *visited, int is_true_out)
293 mark_live_end(lv, block, def);
295 mark_live_out(lv, block, def);
297 if(!bitset_contains_irn(visited, block)) {
298 bitset_add_irn(visited, block);
301 * If this block is not the definition block, we have to go up
304 if(get_nodes_block(def) != block) {
307 mark_live_in(lv, block, def);
309 for(i = 0, n = get_Block_n_cfgpreds(block); i < n; ++i)
310 live_end_at_block(lv, def, get_Block_cfgpred_block(block, i), visited, 1);
316 struct _lv_walker_t {
322 * Liveness analysis for a value.
323 * This functions is meant to be called by a firm walker, to compute the
324 * set of all blocks a value is live in.
325 * @param irn The node (value).
326 * @param env Ignored.
328 static void liveness_for_node(ir_node *irn, void *data)
330 struct _lv_walker_t *walker = data;
331 be_lv_t *lv = walker->lv;
332 bitset_t *visited = walker->data;
333 const ir_edge_t *edge;
336 /* Don't compute liveness information for non-data nodes. */
337 if(!is_data_node(irn))
340 bitset_clear_all(visited);
341 def_block = get_nodes_block(irn);
343 /* Go over all uses of the value */
344 foreach_out_edge(irn, edge) {
345 ir_node *use = edge->src;
349 * If the usage is no data node, skip this use, since it does not
350 * affect the liveness of the node.
352 if(!is_data_node(use))
355 /* Get the block where the usage is in. */
356 use_block = get_nodes_block(use);
359 * If the use is a phi function, determine the corresponding block
360 * through which the value reaches the phi function and mark the
361 * value as live out of that block.
364 ir_node *pred_block = get_Block_cfgpred_block(use_block, edge->pos);
365 live_end_at_block(lv, irn, pred_block, visited, 0);
369 * Else, the value is live in at this block. Mark it and call live
370 * out on the predecessors.
372 else if(def_block != use_block) {
375 mark_live_in(lv, use_block, irn);
377 for(i = 0, n = get_Block_n_cfgpreds(use_block); i < n; ++i) {
378 ir_node *pred_block = get_Block_cfgpred_block(use_block, i);
379 live_end_at_block(lv, irn, pred_block, visited, 1);
385 static void lv_remove_irn_walker(ir_node *bl, void *data)
387 struct _lv_walker_t *w = data;
389 ir_node *irn = w->data;
390 be_lv_remove(lv, bl, irn);
393 static const char *lv_flags_to_str(unsigned flags)
395 static const char *states[] = {
406 return states[flags & 7];
409 static void lv_dump_block(void *context, FILE *f, const ir_node *bl)
412 be_lv_t *lv = context;
413 struct _be_lv_info_t *info = phase_get_irn_data(&lv->ph, bl);
415 fprintf(f, "liveness:\n");
417 unsigned n = info[0].u.head.n_members;
420 for(i = 0; i < n; ++i) {
421 struct _be_lv_info_node_t *n = &info[i+1].u.node;
422 ir_fprintf(f, "%s %+F\n", lv_flags_to_str(n->flags), get_idx_irn(lv->irg, n->idx));
428 static void *lv_phase_data_init(phase_t *phase, ir_node *irn, void *old)
430 struct _be_lv_info_t *info = phase_alloc(phase, LV_STD_SIZE * sizeof(info[0]));
431 memset(info, 0, LV_STD_SIZE * sizeof(info[0]));
432 info[0].u.head.n_size = LV_STD_SIZE - 1;
436 static void compute_liveness(be_lv_t *lv)
438 struct _lv_walker_t w;
440 w.data = bitset_malloc(get_irg_last_idx(lv->irg));
441 irg_walk_graph(lv->irg, liveness_for_node, NULL, &w);
445 /* Compute the inter block liveness for a graph. */
446 be_lv_t *be_liveness(ir_graph *irg)
448 be_lv_t *lv = xmalloc(sizeof(lv[0]));
450 memset(lv, 0, sizeof(lv[0]));
451 FIRM_DBG_REGISTER(lv->dbg, DBG_MODULE);
453 lv->nodes = bitset_malloc(2 * get_irg_last_idx(irg));
454 lv->hook_info.context = lv;
455 lv->hook_info.hook._hook_node_info = lv_dump_block;
456 register_hook(hook_node_info, &lv->hook_info);
457 phase_init(&lv->ph, "liveness", irg, PHASE_DEFAULT_GROWTH, lv_phase_data_init);
458 compute_liveness(lv);
463 void be_liveness_recompute(be_lv_t *lv)
465 unsigned last_idx = get_irg_last_idx(lv->irg);
466 if(last_idx >= bitset_size(lv->nodes)) {
467 bitset_free(lv->nodes);
468 lv->nodes = bitset_malloc(last_idx * 2);
472 bitset_clear_all(lv->nodes);
475 phase_init(&lv->ph, "liveness", lv->irg, PHASE_DEFAULT_GROWTH, lv_phase_data_init);
476 compute_liveness(lv);
480 void be_liveness_free(be_lv_t *lv)
482 unregister_hook(hook_node_info, &lv->hook_info);
484 bitset_free(lv->nodes);
488 void be_liveness_remove(be_lv_t *lv, ir_node *irn)
490 unsigned idx = get_irn_idx(irn);
491 struct _lv_walker_t w;
494 * Removes a single irn from the liveness information.
495 * Since an irn can only be live at blocks dominated by the block of its
496 * definition, we only have to process that dominance subtree.
500 dom_tree_walk(get_nodes_block(irn), lv_remove_irn_walker, NULL, &w);
501 if(idx <= bitset_size(lv->nodes))
502 bitset_clear(lv->nodes, idx);
505 void be_liveness_introduce(be_lv_t *lv, ir_node *irn)
507 struct _lv_walker_t w;
509 w.data = bitset_malloc(get_irg_last_idx(lv->irg));
510 liveness_for_node(irn, &w);
514 void be_liveness_update(be_lv_t *lv, ir_node *irn)
516 be_liveness_remove(lv, irn);
517 be_liveness_introduce(lv, irn);
520 static void lv_add_missing_walker(ir_node *irn, void *data)
522 struct _lv_walker_t *w = data;
523 if(!is_Block(irn) && !bitset_contains_irn(w->lv->nodes, irn)) {
524 liveness_for_node(irn, w);
528 void be_liveness_add_missing(be_lv_t *lv)
530 struct _lv_walker_t w;
532 w.data = bitset_malloc(get_irg_last_idx(lv->irg));
533 irg_walk_graph(lv->irg, lv_add_missing_walker, NULL, &w);
537 static void lv_check_walker(ir_node *bl, void *data)
539 struct _lv_walker_t *w = data;
541 be_lv_t *fresh = w->data;
543 struct _be_lv_info_t *curr = phase_get_irn_data(&lv->ph, bl);
544 struct _be_lv_info_t *fr = phase_get_irn_data(&fresh->ph, bl);
546 if(!fr && curr && curr[0].u.head.n_members > 0) {
549 ir_fprintf(stderr, "%+F liveness should be empty but current liveness contains:\n", bl);
550 for(i = 0; i < curr[0].u.head.n_members; ++i) {
551 ir_fprintf(stderr, "\t%+F\n", get_idx_irn(lv->irg, curr[1 + i].u.node.idx));
556 unsigned n_curr = curr[0].u.head.n_members;
557 unsigned n_fresh = fr[0].u.head.n_members;
561 if(n_curr != n_fresh) {
562 ir_fprintf(stderr, "%+F: liveness set sizes differ. curr %d, correct %d\n", bl, n_curr, n_fresh);
564 ir_fprintf(stderr, "current:\n");
565 for(i = 0; i < n_curr; ++i) {
566 struct _be_lv_info_node_t *n = &curr[1 + i].u.node;
567 ir_fprintf(stderr, "%+F %u %+F %s\n", bl, i, get_idx_irn(lv->irg, n->idx), lv_flags_to_str(n->flags));
570 ir_fprintf(stderr, "correct:\n");
571 for(i = 0; i < n_fresh; ++i) {
572 struct _be_lv_info_node_t *n = &fr[1 + i].u.node;
573 ir_fprintf(stderr, "%+F %u %+F %s\n", bl, i, get_idx_irn(lv->irg, n->idx), lv_flags_to_str(n->flags));
579 void be_liveness_check(be_lv_t *lv)
581 struct _lv_walker_t w;
582 be_lv_t *fresh = be_liveness(lv->irg);
586 irg_block_walk_graph(lv->irg, lv_check_walker, NULL, &w);
587 be_liveness_free(fresh);
591 static void lv_dump_block_walker(ir_node *irn, void *data)
593 struct _lv_walker_t *w = data;
595 lv_dump_block(w->lv, w->data, irn);
599 /* Dump the liveness information for a graph. */
600 void be_liveness_dump(const be_lv_t *lv, FILE *f)
602 struct _lv_walker_t w;
604 w.lv = (be_lv_t *) lv;
606 irg_block_walk_graph(lv->irg, lv_dump_block_walker, NULL, &w);
609 /* Dump the liveness information for a graph. */
610 void be_liveness_dumpto(const be_lv_t *lv, const char *cls_name)
614 ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", lv->irg, cls_name);
615 if((f = fopen(buf, "wt")) != NULL) {
616 be_liveness_dump(lv, f);
622 * Walker: checks the every predecessors of a node dominate
625 static void dom_check(ir_node *irn, void *data)
627 if(!is_Block(irn) && irn != get_irg_end(get_irn_irg(irn))) {
629 ir_node *bl = get_nodes_block(irn);
631 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
632 ir_node *op = get_irn_n(irn, i);
633 ir_node *def_bl = get_nodes_block(op);
634 ir_node *use_bl = bl;
637 use_bl = get_Block_cfgpred_block(bl, i);
639 if(!block_dominates(def_bl, use_bl)) {
640 ir_fprintf(stderr, "%+F in %+F must dominate %+F for user %+F\n", op, def_bl, use_bl, irn);
647 /* Check, if the SSA dominance property is fulfilled. */
648 void be_check_dominance(ir_graph *irg)
650 irg_walk_graph(irg, dom_check, NULL, NULL);
653 pset *be_liveness_transfer(const arch_env_t *arch_env, const arch_register_class_t *cls, ir_node *irn, pset *live)
657 FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, DBG_MODULE);
660 DBG((dbg, LEVEL_1, "%+F\n", irn));
661 for(x = pset_first(live); x; x = pset_next(live))
662 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
665 /* You should better break out of your loop when hitting the first phi function. */
666 assert(!is_Phi(irn) && "liveness_transfer produces invalid results for phi nodes");
668 if(arch_irn_consider_in_reg_alloc(arch_env, cls, irn)) {
669 ir_node *del = pset_remove_ptr(live, irn);
673 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
674 ir_node *op = get_irn_n(irn, i);
676 if(arch_irn_consider_in_reg_alloc(arch_env, cls, op))
677 pset_insert_ptr(live, op);
683 pset *be_liveness_end_of_block(const be_lv_t *lv, const arch_env_t *arch_env, const arch_register_class_t *cls, const ir_node *bl, pset *live)
686 be_lv_foreach(lv, bl, be_lv_state_end, i) {
687 ir_node *irn = be_lv_get_irn(lv, bl, i);
688 if(arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
689 pset_insert_ptr(live, irn);
695 pset *be_liveness_nodes_live_at(const be_lv_t *lv, const arch_env_t *arch_env, const arch_register_class_t *cls, const ir_node *pos, pset *live)
697 const ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
700 be_liveness_end_of_block(lv, arch_env, cls, bl, live);
701 sched_foreach_reverse(bl, irn) {
703 * If we encounter the node we want to insert the Perm after,
704 * exit immediately, so that this node is still live
709 be_liveness_transfer(arch_env, cls, irn, live);