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