Remove the unused parameter const arch_env_t *arch_env from be_liveness_transfer().
[libfirm] / ir / be / belive.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Interblock liveness analysis.
23  * @author      Sebastian Hack
24  * @date        06.12.2004
25  * @version     $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 /* statev is expensive here, only enable when needed */
32 #define DISABLE_STATEV
33
34 #include "impl.h"
35 #include "iredges_t.h"
36 #include "irgwalk.h"
37 #include "irprintf_t.h"
38 #include "irbitset.h"
39 #include "irdump_t.h"
40 #include "irnodeset.h"
41
42 #include "dfs_t.h"
43 #include "absgraph.h"
44 #include "statev.h"
45
46 #include "beutil.h"
47 #include "belive_t.h"
48 #include "beirg_t.h"
49 #include "besched_t.h"
50 #include "bemodule.h"
51
52 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
53
54 /* see comment in compute_liveness() */
55 #define LV_COMPUTE_SORTED
56 #define LV_STD_SIZE             64
57 #define LV_USE_BINARY_SEARCH
58 #undef  LV_INTESIVE_CHECKS
59
60 void be_live_chk_compare(be_lv_t *lv, lv_chk_t *lvc);
61
62 static INLINE int is_liveness_node(const ir_node *irn)
63 {
64         switch(get_irn_opcode(irn)) {
65         case iro_Block:
66         case iro_Bad:
67         case iro_End:
68         case iro_Anchor:
69         case iro_NoMem:
70                 return 0;
71         default:;
72         }
73
74         return 1;
75 }
76
77 int (be_lv_next_irn)(const struct _be_lv_t *lv, const ir_node *bl, unsigned flags, int i)
78 {
79         return _be_lv_next_irn(lv, bl, flags, i);
80 }
81
82 const ir_node * (be_lv_get_irn)(const struct _be_lv_t *lv, const ir_node *bl, int i)
83 {
84         return _be_lv_get_irn(lv, bl, i);
85 }
86
87 int (be_is_live_in)(const be_lv_t *lv, const ir_node *block, const ir_node *irn)
88 {
89         return _be_is_live_xxx(lv, block, irn, be_lv_state_in);
90 }
91
92 int (be_is_live_out)(const be_lv_t *lv, const ir_node *block, const ir_node *irn)
93 {
94         return _be_is_live_xxx(lv, block, irn, be_lv_state_out);
95 }
96
97 int (be_is_live_end)(const be_lv_t *lv, const ir_node *block, const ir_node *irn)
98 {
99         return _be_is_live_xxx(lv, block, irn, be_lv_state_end);
100 }
101
102
103 #ifdef LV_USE_BINARY_SEARCH
104 static INLINE unsigned _be_liveness_bsearch(struct _be_lv_info_t *arr, unsigned idx)
105 {
106         struct _be_lv_info_t *payload = arr + 1;
107
108         unsigned n   = arr[0].u.head.n_members;
109         unsigned res = 0;
110         int lo       = 0;
111         int hi       = n;
112
113         if(n == 0)
114                 return 0;
115
116         while(lo < hi) {
117                 int md          = lo + ((hi - lo) >> 1);
118                 unsigned md_idx = payload[md].u.node.idx;
119
120                 if(idx > md_idx)
121                         lo = md + 1;
122                 else if(idx < md_idx)
123                         hi = md;
124                 else {
125                         res = md;
126                         assert(payload[res].u.node.idx == idx);
127                         break;
128                 }
129
130                 res = lo;
131         }
132
133 #ifdef LV_INTESIVE_CHECKS
134         {
135                 unsigned i;
136                 for(i = res; i < n; ++i)
137                         assert(payload[i].u.node.idx >= idx);
138
139                 for(i = 0; i < res; ++i)
140                         assert(payload[i].u.node.idx < idx);
141         }
142 #endif
143
144         return res;
145 }
146
147 #else
148
149 /**
150  * This function searches linearly for the node in the array.
151  */
152 static INLINE unsigned _be_liveness_bsearch(struct _be_lv_info_t *arr, unsigned idx) {
153         unsigned n  = arr[0].u.head.n_members;
154         unsigned i;
155
156         for(i = 0; i < n; ++i) {
157                 if(arr[i + 1].u.node.idx == idx)
158                         return i;
159         }
160
161         return i;
162 }
163 #endif
164
165 struct _be_lv_info_node_t *be_lv_get(const struct _be_lv_t *li, const ir_node *bl, const ir_node *irn)
166 {
167         struct _be_lv_info_t *irn_live;
168         struct _be_lv_info_node_t *res = NULL;
169
170         stat_ev_tim_push();
171         irn_live = phase_get_irn_data(&li->ph, bl);
172         if(irn_live) {
173                 unsigned idx = get_irn_idx(irn);
174
175                 /* Get the position of the index in the array. */
176                 int pos = _be_liveness_bsearch(irn_live, idx);
177
178                 /* Get the record in question. 1 must be added, since the first record contains information about the array and must be skipped. */
179                 struct _be_lv_info_node_t *rec = &irn_live[pos + 1].u.node;
180
181                 /* Check, if the irn is in deed in the array. */
182                 if(rec->idx == idx)
183                         res = rec;
184         }
185         stat_ev_tim_pop("be_lv_get");
186
187         return res;
188 }
189
190 static struct _be_lv_info_node_t *be_lv_get_or_set(struct _be_lv_t *li, ir_node *bl, ir_node *irn)
191 {
192         struct _be_lv_info_t *irn_live = phase_get_or_set_irn_data(&li->ph, bl);
193
194         unsigned idx = get_irn_idx(irn);
195
196         /* Get the position of the index in the array. */
197         unsigned pos = _be_liveness_bsearch(irn_live, idx);
198
199         /* Get the record in question. 1 must be added, since the first record contains information about the array and must be skipped. */
200         struct _be_lv_info_node_t *res = &irn_live[pos + 1].u.node;
201
202         /* Check, if the irn is in deed in the array. */
203         if(res->idx != idx) {
204                 struct _be_lv_info_t *payload;
205                 unsigned n_members = irn_live[0].u.head.n_members;
206                 unsigned n_size    = irn_live[0].u.head.n_size;
207                 unsigned i;
208
209                 if(n_members + 1 >= n_size) {
210                         /* double the array size. Remember that the first entry is
211                          * metadata about the array and not a real array element */
212                         unsigned old_size_bytes  = (n_size + 1) * sizeof(irn_live[0]);
213                         unsigned new_size        = (2 * n_size) + 1;
214                         size_t   new_size_bytes  = new_size * sizeof(irn_live[0]);
215                         struct _be_lv_info_t *nw = phase_alloc(&li->ph, new_size_bytes);
216                         memcpy(nw, irn_live, old_size_bytes);
217                         memset(((char*) nw) + old_size_bytes, 0,
218                                new_size_bytes - old_size_bytes);
219                         nw[0].u.head.n_size = new_size - 1;
220                         irn_live = nw;
221                         phase_set_irn_data(&li->ph, bl, nw);
222                 }
223
224                 payload = &irn_live[1];
225                 for(i = n_members; i > pos; --i) {
226                         payload[i] = payload[i - 1];
227                 }
228
229                 ++irn_live[0].u.head.n_members;
230
231                 res = &payload[pos].u.node;
232                 res->idx    = idx;
233                 res->flags  = 0;
234         }
235
236 #ifdef LV_INTESIVE_CHECKS
237         {
238                 unsigned i;
239                 unsigned n = irn_live[0].u.head.n_members;
240                 unsigned last = 0;
241                 struct _be_lv_info_t *payload = &irn_live[1];
242
243                 for(i = 0; i < n; ++i) {
244                         assert(payload[i].u.node.idx >= last);
245                         last = payload[i].u.node.idx;
246                 }
247         }
248 #endif
249
250         return res;
251 }
252
253 /**
254  * Removes a node from the list of live variables of a block.
255  * @return 1 if the node was live at that block, 0 if not.
256  */
257 static int be_lv_remove(struct _be_lv_t *li, const ir_node *bl,
258                         const ir_node *irn)
259 {
260         struct _be_lv_info_t *irn_live = phase_get_irn_data(&li->ph, bl);
261
262         if(irn_live) {
263                 unsigned n   = irn_live[0].u.head.n_members;
264                 unsigned idx = get_irn_idx(irn);
265                 unsigned pos = _be_liveness_bsearch(irn_live, idx);
266                 struct _be_lv_info_t *payload  = irn_live + 1;
267                 struct _be_lv_info_node_t *res = &payload[pos].u.node;
268
269                 /* The node is in deed in the block's array. Let's remove it. */
270                 if(res->idx == idx) {
271                         unsigned i;
272
273                         for(i = pos + 1; i < n; ++i)
274                                 payload[i - 1] = payload[i];
275
276                         payload[n - 1].u.node.idx   = 0;
277                         payload[n - 1].u.node.flags = 0;
278
279                         --irn_live[0].u.head.n_members;
280                         DBG((dbg, LEVEL_3, "\tdeleting %+F from %+F at pos %d\n", irn, bl, pos));
281                         return 1;
282                 }
283         }
284
285         return 0;
286 }
287
288 static void register_node(be_lv_t *lv, const ir_node *irn)
289 {
290         unsigned idx = get_irn_idx(irn);
291         if(idx >= bitset_size(lv->nodes)) {
292                 bitset_t *nw = bitset_malloc(2 * idx);
293                 bitset_copy(nw, lv->nodes);
294                 bitset_free(lv->nodes);
295                 lv->nodes = nw;
296         }
297
298         bitset_set(lv->nodes, idx);
299 }
300
301 /**
302  * Mark a node as live-in in a block.
303  */
304 static INLINE void mark_live_in(be_lv_t *lv, ir_node *block, ir_node *irn)
305 {
306         struct _be_lv_info_node_t *n = be_lv_get_or_set(lv, block, irn);
307         DBG((dbg, LEVEL_2, "marking %+F live in at %+F\n", irn, block));
308         n->flags |= be_lv_state_in;
309         register_node(lv, irn);
310 }
311
312 /**
313  * Mark a node as live-out in a block.
314  */
315 static INLINE void mark_live_out(be_lv_t *lv, ir_node *block, ir_node *irn)
316 {
317         struct _be_lv_info_node_t *n = be_lv_get_or_set(lv, block, irn);
318         DBG((dbg, LEVEL_2, "marking %+F live out at %+F\n", irn, block));
319         n->flags |= be_lv_state_out | be_lv_state_end;
320         register_node(lv, irn);
321 }
322
323 /**
324  * Mark a node as live-end in a block.
325  */
326 static INLINE void mark_live_end(be_lv_t *lv, ir_node *block, ir_node *irn)
327 {
328         struct _be_lv_info_node_t *n = be_lv_get_or_set(lv, block, irn);
329         DBG((dbg, LEVEL_2, "marking %+F live end at %+F\n", irn, block));
330         n->flags |= be_lv_state_end;
331         register_node(lv, irn);
332 }
333
334 /**
335  * Mark a node (value) live out at a certain block. Do this also
336  * transitively, i.e. if the block is not the block of the value's
337  * definition, all predecessors are also marked live.
338  * @param def The node (value).
339  * @param block The block to mark the value live out of.
340  * @param visited A set were all visited blocks are recorded.
341  * @param is_true_out Is the node real out there or only live at the end
342  * of the block.
343  */
344 static void live_end_at_block(be_lv_t *lv, ir_node *def, ir_node *block, bitset_t *visited, int is_true_out)
345 {
346         mark_live_end(lv, block, def);
347         if(is_true_out)
348                 mark_live_out(lv, block, def);
349
350         if(!bitset_contains_irn(visited, block)) {
351                 bitset_add_irn(visited, block);
352
353                 /*
354                 * If this block is not the definition block, we have to go up
355                 * further.
356                 */
357                 if(get_nodes_block(def) != block) {
358                         int i, n;
359
360                         mark_live_in(lv, block, def);
361
362                         for(i = 0, n = get_Block_n_cfgpreds(block); i < n; ++i)
363                                 live_end_at_block(lv, def, get_Block_cfgpred_block(block, i), visited, 1);
364                 }
365
366         }
367 }
368
369 struct _lv_walker_t {
370         be_lv_t *lv;
371         void *data;
372 };
373
374 typedef struct lv_remove_walker_t {
375         be_lv_t       *lv;
376         const ir_node *irn;
377 } lv_remove_walker_t;
378
379
380 /**
381  * Liveness analysis for a value.
382  * This functions is meant to be called by a firm walker, to compute the
383  * set of all blocks a value is live in.
384  * @param irn The node (value).
385  * @param env Ignored.
386  */
387 static void liveness_for_node(ir_node *irn, void *data)
388 {
389         struct _lv_walker_t *walker = data;
390         be_lv_t *lv       = walker->lv;
391         bitset_t *visited = walker->data;
392         const ir_edge_t *edge;
393         ir_node *def_block;
394
395         /* Don't compute liveness information for non-data nodes. */
396         if(!is_liveness_node(irn))
397                 return;
398
399         bitset_clear_all(visited);
400         def_block = get_nodes_block(irn);
401
402         /* Go over all uses of the value */
403         foreach_out_edge(irn, edge) {
404                 ir_node *use = edge->src;
405                 ir_node *use_block;
406
407                 DBG((dbg, LEVEL_4, "%+F: use at %+F, pos %d in %+F\n", irn, use, edge->pos, get_block(use)));
408                 assert(get_irn_n(use, edge->pos) == irn);
409
410                 /*
411                  * If the usage is no data node, skip this use, since it does not
412                  * affect the liveness of the node.
413                  */
414                 if(!is_liveness_node(use))
415                         continue;
416
417                 /* Get the block where the usage is in. */
418                 use_block = get_nodes_block(use);
419
420                 /*
421                  * If the use is a phi function, determine the corresponding block
422                  * through which the value reaches the phi function and mark the
423                  * value as live out of that block.
424                  */
425                 if(is_Phi(use)) {
426                         ir_node *pred_block = get_Block_cfgpred_block(use_block, edge->pos);
427                         live_end_at_block(lv, irn, pred_block, visited, 0);
428                 }
429
430                 /*
431                  * Else, the value is live in at this block. Mark it and call live
432                  * out on the predecessors.
433                  */
434                 else if(def_block != use_block) {
435                         int i, n;
436
437                         mark_live_in(lv, use_block, irn);
438
439                         for(i = 0, n = get_Block_n_cfgpreds(use_block); i < n; ++i) {
440                                 ir_node *pred_block = get_Block_cfgpred_block(use_block, i);
441                                 live_end_at_block(lv, irn, pred_block, visited, 1);
442                         }
443                 }
444         }
445 }
446
447 static void lv_remove_irn_walker(ir_node *bl, void *data)
448 {
449         lv_remove_walker_t *w = data;
450         be_lv_remove(w->lv, bl, w->irn);
451 }
452
453 static const char *lv_flags_to_str(unsigned flags)
454 {
455         static const char *states[] = {
456                 "---",
457                 "i--",
458                 "-e-",
459                 "ie-",
460                 "--o",
461                 "i-o",
462                 "-eo",
463                 "ieo"
464         };
465
466         return states[flags & 7];
467 }
468
469 static void lv_dump_block(void *context, FILE *f, const ir_node *bl)
470 {
471         if(is_Block(bl)) {
472                 be_lv_t *lv = context;
473                 struct _be_lv_info_t *info = phase_get_irn_data(&lv->ph, bl);
474
475                 fprintf(f, "liveness:\n");
476                 if(info) {
477                         unsigned n = info[0].u.head.n_members;
478                         unsigned i;
479
480                         for(i = 0; i < n; ++i) {
481                                 struct _be_lv_info_node_t *n = &info[i+1].u.node;
482                                 ir_fprintf(f, "%s %+F\n", lv_flags_to_str(n->flags), get_idx_irn(lv->irg, n->idx));
483                         }
484                 }
485         }
486 }
487
488 static void *lv_phase_data_init(ir_phase *phase, const ir_node *irn, void *old)
489 {
490         struct _be_lv_info_t *info = phase_alloc(phase, LV_STD_SIZE * sizeof(info[0]));
491         (void) irn;
492         (void) old;
493
494         memset(info, 0, LV_STD_SIZE * sizeof(info[0]));
495         info[0].u.head.n_size = LV_STD_SIZE - 1;
496         return info;
497 }
498
499 static void collect_nodes(ir_node *irn, void *data)
500 {
501         struct obstack *obst = data;
502         if (is_liveness_node(irn))
503                 obstack_ptr_grow(obst, irn);
504 }
505
506 static int node_idx_cmp(const void *a, const void *b)
507 {
508         const ir_node *p = *(ir_node **) a;
509         const ir_node *q = *(ir_node **) b;
510         int ia = get_irn_idx(p);
511         int ib = get_irn_idx(q);
512         return ia - ib;
513 }
514
515 static void compute_liveness(be_lv_t *lv)
516 {
517         struct obstack obst;
518         struct _lv_walker_t w;
519         ir_node **nodes;
520         int i, n;
521
522         stat_ev_tim_push();
523         obstack_init(&obst);
524         irg_walk_graph(lv->irg, collect_nodes, NULL, &obst);
525         n      = obstack_object_size(&obst) / sizeof(nodes[0]);
526         nodes  = obstack_finish(&obst);
527
528         /*
529          * inserting the variables sorted by their ID is probably
530          * more efficient since the binary sorted set insertion
531          * will not need to move around the data.
532          * However, if sorting the variables a priori pays off
533          * needs to be checked, hence the define.
534          */
535 #ifdef LV_COMPUTE_SORTED
536         qsort(nodes, n, sizeof(nodes[0]), node_idx_cmp);
537 #endif
538
539         w.lv   = lv;
540         w.data = bitset_obstack_alloc(&obst, get_irg_last_idx(lv->irg));
541
542         for (i = 0; i < n; ++i)
543                 liveness_for_node(nodes[i], &w);
544
545         obstack_free(&obst, NULL);
546         register_hook(hook_node_info, &lv->hook_info);
547         stat_ev_tim_pop("be_lv_sets_cons");
548 }
549
550 void be_liveness_assure_sets(be_lv_t *lv)
551 {
552         if (!lv->nodes) {
553                 BE_TIMER_PUSH(t_live);
554
555                 lv->nodes = bitset_malloc(2 * get_irg_last_idx(lv->irg));
556                 phase_init(&lv->ph, "liveness", lv->irg, PHASE_DEFAULT_GROWTH, lv_phase_data_init, NULL);
557                 compute_liveness(lv);
558                 /* be_live_chk_compare(lv, lv->lvc); */
559
560                 BE_TIMER_POP(t_live);
561         }
562 }
563
564 void be_liveness_assure_chk(be_lv_t *lv)
565 {
566 #ifndef USE_LIVE_CHK
567         BE_TIMER_PUSH(t_verify);
568         be_liveness_assure_sets(lv);
569         BE_TIMER_POP(t_verify);
570 #else
571         (void) lv;
572 #endif
573 }
574
575 void be_liveness_invalidate(be_lv_t *lv)
576 {
577         if (lv && lv->nodes) {
578                 unregister_hook(hook_node_info, &lv->hook_info);
579                 phase_free(&lv->ph);
580                 bitset_free(lv->nodes);
581                 lv->nodes = NULL;
582         }
583 }
584
585 /* Compute the inter block liveness for a graph. */
586 be_lv_t *be_liveness(const be_irg_t *birg)
587 {
588         be_lv_t *lv = XMALLOCZ(be_lv_t);
589
590         lv->irg  = be_get_birg_irg(birg);
591         lv->birg = birg;
592 #ifdef USE_LIVE_CHK
593         lv->dfs  = dfs_new(&absgraph_irg_cfg_succ, lv->irg);
594         lv->lvc  = lv_chk_new(lv->irg, lv->dfs);
595 #endif
596         lv->hook_info.context = lv;
597         lv->hook_info.hook._hook_node_info = lv_dump_block;
598
599         return lv;
600 }
601
602 void be_liveness_recompute(be_lv_t *lv)
603 {
604         unsigned last_idx;
605
606         BE_TIMER_PUSH(t_live);
607         last_idx = get_irg_last_idx(lv->irg);
608         if(last_idx >= bitset_size(lv->nodes)) {
609                 bitset_free(lv->nodes);
610                 lv->nodes = bitset_malloc(last_idx * 2);
611         } else
612                 bitset_clear_all(lv->nodes);
613
614         phase_free(&lv->ph);
615         phase_init(&lv->ph, "liveness", lv->irg, PHASE_DEFAULT_GROWTH, lv_phase_data_init, NULL);
616         compute_liveness(lv);
617
618         BE_TIMER_POP(t_live);
619 }
620
621
622 void be_liveness_free(be_lv_t *lv)
623 {
624         be_liveness_invalidate(lv);
625 #ifdef USE_LIVE_CHK
626         lv_chk_free(lv->lvc);
627         dfs_free(lv->dfs);
628 #endif
629         xfree(lv);
630 }
631
632 void be_liveness_remove(be_lv_t *lv, const ir_node *irn)
633 {
634         if (lv->nodes) {
635                 unsigned idx = get_irn_idx(irn);
636                 lv_remove_walker_t w;
637
638                 /*
639                  * Removes a single irn from the liveness information.
640                  * Since an irn can only be live at blocks dominated by the block of its
641                  * definition, we only have to process that dominance subtree.
642                  */
643                 w.lv  = lv;
644                 w.irn = irn;
645                 dom_tree_walk(get_nodes_block(irn), lv_remove_irn_walker, NULL, &w);
646                 if(idx < bitset_size(lv->nodes))
647                         bitset_clear(lv->nodes, idx);
648         }
649 }
650
651 void be_liveness_introduce(be_lv_t *lv, ir_node *irn)
652 {
653         if (lv->nodes) {
654                 struct _lv_walker_t w;
655                 w.lv   = lv;
656                 w.data = bitset_malloc(get_irg_last_idx(lv->irg));
657                 liveness_for_node(irn, &w);
658                 bitset_free(w.data);
659         }
660 }
661
662 void be_liveness_update(be_lv_t *lv, ir_node *irn)
663 {
664         be_liveness_remove(lv, irn);
665         be_liveness_introduce(lv, irn);
666 }
667
668 static void lv_check_walker(ir_node *bl, void *data)
669 {
670         struct _lv_walker_t *w = data;
671         be_lv_t *lv    = w->lv;
672         be_lv_t *fresh = w->data;
673
674         struct _be_lv_info_t *curr = phase_get_irn_data(&lv->ph, bl);
675         struct _be_lv_info_t *fr   = phase_get_irn_data(&fresh->ph, bl);
676
677         if(!fr && curr && curr[0].u.head.n_members > 0) {
678                 unsigned i;
679
680                 ir_fprintf(stderr, "%+F liveness should be empty but current liveness contains:\n", bl);
681                 for(i = 0; i < curr[0].u.head.n_members; ++i) {
682                         ir_fprintf(stderr, "\t%+F\n", get_idx_irn(lv->irg, curr[1 + i].u.node.idx));
683                 }
684         }
685
686         else if(curr) {
687                 unsigned n_curr  = curr[0].u.head.n_members;
688                 unsigned n_fresh = fr[0].u.head.n_members;
689
690                 unsigned i;
691
692                 if(n_curr != n_fresh) {
693                         ir_fprintf(stderr, "%+F: liveness set sizes differ. curr %d, correct %d\n", bl, n_curr, n_fresh);
694
695                         ir_fprintf(stderr, "current:\n");
696                         for(i = 0; i < n_curr; ++i) {
697                                 struct _be_lv_info_node_t *n = &curr[1 + i].u.node;
698                                 ir_fprintf(stderr, "%+F %u %+F %s\n", bl, i, get_idx_irn(lv->irg, n->idx), lv_flags_to_str(n->flags));
699                         }
700
701                         ir_fprintf(stderr, "correct:\n");
702                         for(i = 0; i < n_fresh; ++i) {
703                                 struct _be_lv_info_node_t *n = &fr[1 + i].u.node;
704                                 ir_fprintf(stderr, "%+F %u %+F %s\n", bl, i, get_idx_irn(lv->irg, n->idx), lv_flags_to_str(n->flags));
705                         }
706                 }
707         }
708 }
709
710 void be_liveness_check(be_lv_t *lv)
711 {
712         struct _lv_walker_t w;
713         be_lv_t *fresh = be_liveness(lv->birg);
714
715         w.lv   = lv;
716         w.data = fresh;
717         irg_block_walk_graph(lv->irg, lv_check_walker, NULL, &w);
718         be_liveness_free(fresh);
719 }
720
721
722 static void lv_dump_block_walker(ir_node *irn, void *data)
723 {
724         struct _lv_walker_t *w = data;
725         if(is_Block(irn))
726                 lv_dump_block(w->lv, w->data, irn);
727 }
728
729
730 /* Dump the liveness information for a graph. */
731 void be_liveness_dump(const be_lv_t *lv, FILE *f)
732 {
733         struct _lv_walker_t w;
734
735         w.lv   = (be_lv_t *) lv;
736         w.data = f;
737         irg_block_walk_graph(lv->irg, lv_dump_block_walker, NULL, &w);
738 }
739
740 /* Dump the liveness information for a graph. */
741 void be_liveness_dumpto(const be_lv_t *lv, const char *cls_name)
742 {
743         FILE *f;
744         char buf[128];
745         ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", lv->irg, cls_name);
746         if((f = fopen(buf, "wt")) != NULL) {
747                 be_liveness_dump(lv, f);
748                 fclose(f);
749         }
750 }
751
752 /**
753  * Walker: checks the every predecessors of a node dominate
754  * the note.
755  */
756 static void dom_check(ir_node *irn, void *data)
757 {
758         int *problem_found = data;
759
760         if(!is_Block(irn) && irn != get_irg_end(get_irn_irg(irn))) {
761                 int i, n;
762                 ir_node *bl = get_nodes_block(irn);
763
764                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
765                         ir_node *op     = get_irn_n(irn, i);
766                         ir_node *def_bl = get_nodes_block(op);
767                         ir_node *use_bl = bl;
768
769                         if(is_Phi(irn))
770                                 use_bl = get_Block_cfgpred_block(bl, i);
771
772                         if(get_irn_opcode(use_bl) != iro_Bad
773                              && get_irn_opcode(def_bl) != iro_Bad
774                              && !block_dominates(def_bl, use_bl)) {
775                                 ir_fprintf(stderr, "Verify warning: %+F in %+F must dominate %+F for user %+F (%s)\n", op, def_bl, use_bl, irn, get_irg_dump_name(get_irn_irg(op)));
776                                 *problem_found = 1;
777                         }
778                 }
779         }
780 }
781
782 /* Check, if the SSA dominance property is fulfilled. */
783 int be_check_dominance(ir_graph *irg)
784 {
785         int problem_found = 0;
786
787         assure_doms(irg);
788         irg_walk_graph(irg, dom_check, NULL, &problem_found);
789
790         return !problem_found;
791 }
792
793 void be_liveness_transfer(const arch_register_class_t *cls,
794                           ir_node *node, ir_nodeset_t *nodeset)
795 {
796         int i, arity;
797
798         /* You should better break out of your loop when hitting the first phi
799          * function. */
800         assert(!is_Phi(node) && "liveness_transfer produces invalid results for phi nodes");
801
802         if (get_irn_mode(node) == mode_T) {
803                 const ir_edge_t *edge;
804
805                 foreach_out_edge(node, edge) {
806                         ir_node *proj = get_edge_src_irn(edge);
807
808                         if (arch_irn_consider_in_reg_alloc(cls, proj)) {
809                                 ir_nodeset_remove(nodeset, proj);
810                         }
811                 }
812         } else if (arch_irn_consider_in_reg_alloc(cls, node)) {
813                 ir_nodeset_remove(nodeset, node);
814         }
815
816         arity = get_irn_arity(node);
817         for (i = 0; i < arity; ++i) {
818                 ir_node *op = get_irn_n(node, i);
819
820                 if (arch_irn_consider_in_reg_alloc(cls, op))
821                         ir_nodeset_insert(nodeset, op);
822         }
823 }
824
825
826
827 void be_liveness_end_of_block(const be_lv_t *lv, const arch_env_t *arch_env,
828                               const arch_register_class_t *cls,
829                               const ir_node *block, ir_nodeset_t *live)
830 {
831         int i;
832         (void)arch_env; // TODO remove parameter
833
834         assert(lv->nodes && "live sets must be computed");
835         be_lv_foreach(lv, block, be_lv_state_end, i) {
836                 ir_node *node = be_lv_get_irn(lv, block, i);
837                 if (!arch_irn_consider_in_reg_alloc(cls, node))
838                         continue;
839
840                 ir_nodeset_insert(live, node);
841         }
842 }
843
844
845
846 void be_liveness_nodes_live_at(const be_lv_t *lv, const arch_env_t *arch_env,
847                                const arch_register_class_t *cls,
848                                const ir_node *pos, ir_nodeset_t *live)
849 {
850         const ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
851         ir_node *irn;
852
853         be_liveness_end_of_block(lv, arch_env, cls, bl, live);
854         sched_foreach_reverse(bl, irn) {
855                 /*
856                  * If we encounter the node we want to insert the Perm after,
857                  * exit immediately, so that this node is still live
858                  */
859                 if(irn == pos)
860                         return;
861
862                 be_liveness_transfer(cls, irn, live);
863         }
864 }
865
866 void be_liveness_nodes_live_at_input(const be_lv_t *lv,
867                                      const arch_env_t *arch_env,
868                                      const arch_register_class_t *cls,
869                                      const ir_node *pos, ir_nodeset_t *live)
870 {
871         const ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
872         ir_node *irn;
873
874         assert(lv->nodes && "live sets must be computed");
875         be_liveness_end_of_block(lv, arch_env, cls, bl, live);
876         sched_foreach_reverse(bl, irn) {
877                 be_liveness_transfer(cls, irn, live);
878                 if(irn == pos)
879                         return;
880         }
881 }
882
883 static void collect_node(ir_node *irn, void *data)
884 {
885         struct obstack *obst = data;
886         obstack_ptr_grow(obst, irn);
887 }
888
889 void be_live_chk_compare(be_lv_t *lv, lv_chk_t *lvc)
890 {
891         ir_graph *irg    = lv->irg;
892
893         struct obstack obst;
894         ir_node **nodes;
895         ir_node **blocks;
896         int i, j;
897
898         obstack_init(&obst);
899
900         irg_block_walk_graph(irg, collect_node, NULL, &obst);
901         obstack_ptr_grow(&obst, NULL);
902         blocks = obstack_finish(&obst);
903
904         irg_walk_graph(irg, collect_node, NULL, &obst);
905         obstack_ptr_grow(&obst, NULL);
906         nodes = obstack_finish(&obst);
907
908         stat_ev_ctx_push("be_lv_chk_compare");
909         for (j = 0; nodes[j]; ++j) {
910                 ir_node *irn = nodes[j];
911                 for (i = 0; blocks[i]; ++i) {
912                         ir_node *bl = blocks[i];
913
914                         if (!is_Block(irn)) {
915                                 int lvr_in  = be_is_live_in (lv, bl, irn);
916                                 int lvr_out = be_is_live_out(lv, bl, irn);
917                                 int lvr_end = be_is_live_end(lv, bl, irn);
918
919                                 int lvc_in  = lv_chk_bl_in (lvc, bl, irn);
920                                 int lvc_out = lv_chk_bl_out(lvc, bl, irn);
921                                 int lvc_end = lv_chk_bl_end(lvc, bl, irn);
922
923                                 if (lvr_in - lvc_in != 0)
924                                         ir_fprintf(stderr, "live in  info for %+F at %+F differs: nml: %d, chk: %d\n", irn, bl, lvr_in, lvc_in);
925
926                                 if (lvr_end - lvc_end != 0)
927                                         ir_fprintf(stderr, "live end info for %+F at %+F differs: nml: %d, chk: %d\n", irn, bl, lvr_end, lvc_end);
928
929                                 if (lvr_out - lvc_out != 0)
930                                         ir_fprintf(stderr, "live out info for %+F at %+F differs: nml: %d, chk: %d\n", irn, bl, lvr_out, lvc_out);
931                         }
932                 }
933         }
934         stat_ev_ctx_pop("be_lv_chk_compare");
935
936         obstack_free(&obst, NULL);
937 }
938
939 void be_init_live(void)
940 {
941         FIRM_DBG_REGISTER(dbg, "firm.be.liveness");
942 }
943
944 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_live);