Adapted to new liveness
[libfirm] / ir / be / belive.c
1 /**
2  * Interblock liveness analysis.
3  * @author Sebastian Hack
4  * @date 6.12.2004
5  */
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9
10 #include "impl.h"
11 #include "iredges_t.h"
12 #include "irgwalk.h"
13 #include "irprintf_t.h"
14 #include "irbitset.h"
15
16 #include "beutil.h"
17 #include "belive_t.h"
18 #include "besched_t.h"
19
20 #define DBG_MODULE              "firm.be.liveness"
21
22 #define LV_STD_SIZE             128
23 #define LV_USE_BINARY_SEARCH
24 #undef  LV_INTESIVE_CHECKS
25
26 int (be_lv_next_irn)(const struct _be_lv_t *lv, const ir_node *bl, unsigned flags, int i)
27 {
28         return _be_lv_next_irn(lv, bl, flags, i);
29 }
30
31 const ir_node * (be_lv_get_irn)(const struct _be_lv_t *lv, const ir_node *bl, int i)
32 {
33         return _be_lv_get_irn(lv, bl, i);
34 }
35
36 int (be_is_live_in)(const be_lv_t *lv, const ir_node *block, const ir_node *irn)
37 {
38         return _be_is_live_xxx(lv, block, irn, be_lv_state_in);
39 }
40
41 int (be_is_live_out)(const be_lv_t *lv, const ir_node *block, const ir_node *irn)
42 {
43         return _be_is_live_xxx(lv, block, irn, be_lv_state_out);
44 }
45
46 int (be_is_live_end)(const be_lv_t *lv, const ir_node *block, const ir_node *irn)
47 {
48         return _be_is_live_xxx(lv, block, irn, be_lv_state_end);
49 }
50
51
52 #ifdef LV_USE_BINARY_SEARCH
53 static INLINE unsigned _be_liveness_bsearch(struct _be_lv_info_t *arr, unsigned idx)
54 {
55         struct _be_lv_info_t *payload = arr + 1;
56
57         unsigned n   = arr[0].u.head.n_members;
58         unsigned res = 0;
59         int lo       = 0;
60         int hi       = n;
61
62         if(n == 0)
63                 return 0;
64
65 #if 0
66         if(idx < payload[0].u.node.idx)
67                 return 0;
68
69         if(idx > payload[n - 1].u.node.idx)
70                 return n - 1;
71 #endif
72
73         /* start a binary search for the requested node. */
74         while(lo < hi) {
75                 int md          = lo + ((hi - lo) >> 1);
76                 unsigned md_idx = payload[md].u.node.idx;
77
78                 if(idx > md_idx)
79                         lo = md + 1;
80                 else if(idx < md_idx)
81                         hi = md;
82                 else {
83                         res = md;
84                         assert(payload[res].u.node.idx == idx);
85                         break;
86                 }
87
88                 res = lo;
89         }
90
91 #ifdef LV_INTESIVE_CHECKS
92         {
93                 unsigned i;
94                 for(i = res; i < n; ++i)
95                         assert(payload[i].u.node.idx >= idx);
96
97                 for(i = 0; i < res; ++i)
98                         assert(payload[i].u.node.idx < idx);
99         }
100 #endif
101
102         return res;
103 }
104
105 #else
106
107 /**
108  * This function searches linearily for the node in the array.
109  */
110 static INLINE unsigned _be_liveness_bsearch(struct _be_lv_info_t *arr, unsigned idx) {
111         unsigned n  = arr[0].u.head.n_members;
112         unsigned i;
113
114         for(i = 0; i < n; ++i) {
115                 if(arr[i + 1].u.node.idx == idx)
116                         return i;
117         }
118
119         return i;
120 }
121 #endif
122
123 struct _be_lv_info_node_t *be_lv_get(const struct _be_lv_t *li, const ir_node *bl, const ir_node *irn)
124 {
125         struct _be_lv_info_t *irn_live = phase_get_irn_data(&li->ph, bl);
126
127         if(irn_live) {
128                 unsigned idx = get_irn_idx(irn);
129
130                 /* Get the position of the index in the array. */
131                 int pos = _be_liveness_bsearch(irn_live, idx);
132
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;
135
136                 /* Check, if the irn is in deed in the array. */
137                 if(res->idx == idx)
138                         return res;
139         }
140
141         return NULL;
142 }
143
144 static struct _be_lv_info_node_t *be_lv_get_or_set(struct _be_lv_t *li, ir_node *bl, ir_node *irn)
145 {
146         struct _be_lv_info_t *irn_live = phase_get_or_set_irn_data(&li->ph, bl);
147
148         unsigned idx = get_irn_idx(irn);
149
150         /* Get the position of the index in the array. */
151         unsigned pos = _be_liveness_bsearch(irn_live, idx);
152
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;
155
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;
161                 unsigned i;
162
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;
168                         irn_live = nw;
169                         phase_set_irn_data(&li->ph, irn, nw);
170                 }
171
172                 payload = &irn_live[1];
173                 for(i = n_members; i > pos; --i) {
174                         payload[i] = payload[i - 1];
175                 }
176
177                 ++irn_live[0].u.head.n_members;
178
179                 res = &payload[pos].u.node;
180                 res->idx    = idx;
181                 res->flags  = 0;
182         }
183
184 #ifdef LV_INTESIVE_CHECKS
185         {
186                 unsigned i;
187                 unsigned n = irn_live[0].u.head.n_members;
188                 unsigned last = 0;
189                 struct _be_lv_info_t *payload = &irn_live[1];
190
191                 for(i = 0; i < n; ++i) {
192                         assert(payload[i].u.node.idx >= last);
193                         last = payload[i].u.node.idx;
194                 }
195         }
196 #endif
197
198         return res;
199 }
200
201 /**
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.
204  */
205 static int be_lv_remove(struct _be_lv_t *li, ir_node *bl, ir_node *irn)
206 {
207         struct _be_lv_info_t *irn_live = phase_get_irn_data(&li->ph, bl);
208
209         if(irn_live) {
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;
215
216                 /* The node is in deed in the block's array. Let's remove it. */
217                 if(res->idx == idx) {
218                         unsigned i;
219
220                         for(i = pos + 1; i < n; ++i)
221                                 payload[i - 1] = payload[i];
222
223                         payload[n - 1].u.node.idx   = 0;
224                         payload[n - 1].u.node.flags = 0;
225
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));
228                         return 1;
229                 }
230         }
231
232         return 0;
233 }
234
235 static void register_node(be_lv_t *lv, const ir_node *irn)
236 {
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);
242                 lv->nodes = nw;
243         }
244
245         bitset_set(lv->nodes, idx);
246 }
247
248 /**
249  * Mark a node as live-in in a block.
250  */
251 static INLINE void mark_live_in(be_lv_t *lv, ir_node *block, ir_node *irn)
252 {
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);
257 }
258
259 /**
260  * Mark a node as live-out in a block.
261  */
262 static INLINE void mark_live_out(be_lv_t *lv, ir_node *block, ir_node *irn)
263 {
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);
268 }
269
270 /**
271  * Mark a node as live-end in a block.
272  */
273 static INLINE void mark_live_end(be_lv_t *lv, ir_node *block, ir_node *irn)
274 {
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);
279 }
280
281 /**
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
289  * of the block.
290  */
291 static void live_end_at_block(be_lv_t *lv, ir_node *def, ir_node *block, bitset_t *visited, int is_true_out)
292 {
293         mark_live_end(lv, block, def);
294         if(is_true_out)
295                 mark_live_out(lv, block, def);
296
297         if(!bitset_contains_irn(visited, block)) {
298                 bitset_add_irn(visited, block);
299
300                 /*
301                 * If this block is not the definition block, we have to go up
302                 * further.
303                 */
304                 if(get_nodes_block(def) != block) {
305                         int i, n;
306
307                         mark_live_in(lv, block, def);
308
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);
311                 }
312
313         }
314 }
315
316 struct _lv_walker_t {
317         be_lv_t *lv;
318         void *data;
319 };
320
321 /**
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.
327  */
328 static void liveness_for_node(ir_node *irn, void *data)
329 {
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;
334         ir_node *def_block;
335
336         /* Don't compute liveness information for non-data nodes. */
337         if(!is_data_node(irn))
338                 return;
339
340         bitset_clear_all(visited);
341         def_block = get_nodes_block(irn);
342
343         /* Go over all uses of the value */
344         foreach_out_edge(irn, edge) {
345                 ir_node *use = edge->src;
346                 ir_node *use_block;
347
348                 /*
349                 * If the usage is no data node, skip this use, since it does not
350                 * affect the liveness of the node.
351                 */
352                 if(!is_data_node(use))
353                         continue;
354
355                 /* Get the block where the usage is in. */
356                 use_block = get_nodes_block(use);
357
358                 /*
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.
362                 */
363                 if(is_Phi(use)) {
364                         ir_node *pred_block = get_Block_cfgpred_block(use_block, edge->pos);
365                         live_end_at_block(lv, irn, pred_block, visited, 0);
366                 }
367
368                 /*
369                 * Else, the value is live in at this block. Mark it and call live
370                 * out on the predecessors.
371                 */
372                 else if(def_block != use_block) {
373                         int i, n;
374
375                         mark_live_in(lv, use_block, irn);
376
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);
380                         }
381                 }
382         }
383 }
384
385 static void lv_remove_irn_walker(ir_node *bl, void *data)
386 {
387         struct _lv_walker_t *w = data;
388         be_lv_t *lv  = w->lv;
389         ir_node *irn = w->data;
390         be_lv_remove(lv, bl, irn);
391 }
392
393 static const char *lv_flags_to_str(unsigned flags)
394 {
395         static const char *states[] = {
396                 "---",
397                 "i--",
398                 "-e-",
399                 "ie-",
400                 "--o",
401                 "i-o",
402                 "-eo",
403                 "ieo"
404         };
405
406         return states[flags & 7];
407 }
408
409 static void lv_dump_block(void *context, FILE *f, const ir_node *bl)
410 {
411         if(is_Block(bl)) {
412                 be_lv_t *lv = context;
413                 struct _be_lv_info_t *info = phase_get_irn_data(&lv->ph, bl);
414
415                 fprintf(f, "liveness:\n");
416                 if(info) {
417                         unsigned n = info[0].u.head.n_members;
418                         unsigned i;
419
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));
423                         }
424                 }
425         }
426 }
427
428 static void *lv_phase_data_init(phase_t *phase, ir_node *irn, void *old)
429 {
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;
433         return info;
434 }
435
436 static void compute_liveness(be_lv_t *lv)
437 {
438         struct _lv_walker_t w;
439         w.lv   = lv;
440         w.data = bitset_malloc(get_irg_last_idx(lv->irg));
441         irg_walk_graph(lv->irg, liveness_for_node, NULL, &w);
442         bitset_free(w.data);
443 }
444
445 /* Compute the inter block liveness for a graph. */
446 be_lv_t *be_liveness(ir_graph *irg)
447 {
448         be_lv_t *lv = xmalloc(sizeof(lv[0]));
449
450         memset(lv, 0, sizeof(lv[0]));
451         FIRM_DBG_REGISTER(lv->dbg, DBG_MODULE);
452         lv->irg = irg;
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);
459
460         return lv;
461 }
462
463 void be_liveness_recompute(be_lv_t *lv)
464 {
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);
469         }
470
471         else
472                 bitset_clear_all(lv->nodes);
473
474         phase_free(&lv->ph);
475         phase_init(&lv->ph, "liveness", lv->irg, PHASE_DEFAULT_GROWTH, lv_phase_data_init);
476         compute_liveness(lv);
477 }
478
479
480 void be_liveness_free(be_lv_t *lv)
481 {
482         unregister_hook(hook_node_info, &lv->hook_info);
483         phase_free(&lv->ph);
484         bitset_free(lv->nodes);
485         free(lv);
486 }
487
488 void be_liveness_remove(be_lv_t *lv, ir_node *irn)
489 {
490         unsigned idx = get_irn_idx(irn);
491         struct _lv_walker_t w;
492
493         /*
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.
497          */
498         w.lv   = lv;
499         w.data = irn;
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);
503 }
504
505 void be_liveness_introduce(be_lv_t *lv, ir_node *irn)
506 {
507         struct _lv_walker_t w;
508         w.lv   = lv;
509         w.data = bitset_malloc(get_irg_last_idx(lv->irg));
510         liveness_for_node(irn, &w);
511         bitset_free(w.data);
512 }
513
514 void be_liveness_update(be_lv_t *lv, ir_node *irn)
515 {
516         be_liveness_remove(lv, irn);
517         be_liveness_introduce(lv, irn);
518 }
519
520 static void lv_add_missing_walker(ir_node *irn, void *data)
521 {
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);
525         }
526 }
527
528 void be_liveness_add_missing(be_lv_t *lv)
529 {
530         struct _lv_walker_t w;
531         w.lv   = lv;
532         w.data = bitset_malloc(get_irg_last_idx(lv->irg));
533         irg_walk_graph(lv->irg, lv_add_missing_walker, NULL, &w);
534         bitset_free(w.data);
535 }
536
537 static void lv_check_walker(ir_node *bl, void *data)
538 {
539         struct _lv_walker_t *w = data;
540         be_lv_t *lv    = w->lv;
541         be_lv_t *fresh = w->data;
542
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);
545
546         if(!fr && curr && curr[0].u.head.n_members > 0) {
547                 unsigned i;
548
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));
552                 }
553         }
554
555         else if(curr) {
556                 unsigned n_curr  = curr[0].u.head.n_members;
557                 unsigned n_fresh = fr[0].u.head.n_members;
558                 unsigned n = MIN(n_curr, n_fresh);
559
560                 unsigned i;
561
562                 if(n_curr != n_fresh) {
563                         ir_fprintf(stderr, "%+F: liveness set sizes differ. curr %d, correct %d\n", bl, n_curr, n_fresh);
564
565                         ir_fprintf(stderr, "current:\n");
566                         for(i = 0; i < n_curr; ++i) {
567                                 struct _be_lv_info_node_t *n = &curr[1 + i].u.node;
568                                 ir_fprintf(stderr, "%+F %u %+F %s\n", bl, i, get_idx_irn(lv->irg, n->idx), lv_flags_to_str(n->flags));
569                         }
570
571                         ir_fprintf(stderr, "correct:\n");
572                         for(i = 0; i < n_fresh; ++i) {
573                                 struct _be_lv_info_node_t *n = &fr[1 + i].u.node;
574                                 ir_fprintf(stderr, "%+F %u %+F %s\n", bl, i, get_idx_irn(lv->irg, n->idx), lv_flags_to_str(n->flags));
575                         }
576                 }
577         }
578 }
579
580 void be_liveness_check(be_lv_t *lv)
581 {
582         struct _lv_walker_t w;
583         be_lv_t *fresh = be_liveness(lv->irg);
584
585         w.lv   = lv;
586         w.data = fresh;
587         irg_block_walk_graph(lv->irg, lv_check_walker, NULL, &w);
588         be_liveness_free(fresh);
589 }
590
591
592 static void lv_dump_block_walker(ir_node *irn, void *data)
593 {
594         struct _lv_walker_t *w = data;
595         if(is_Block(irn))
596                 lv_dump_block(w->lv, w->data, irn);
597 }
598
599
600 /* Dump the liveness information for a graph. */
601 void be_liveness_dump(const be_lv_t *lv, FILE *f)
602 {
603         struct _lv_walker_t w;
604
605         w.lv   = (be_lv_t *) lv;
606         w.data = f;
607         irg_block_walk_graph(lv->irg, lv_dump_block_walker, NULL, &w);
608 }
609
610 /* Dump the liveness information for a graph. */
611 void be_liveness_dumpto(const be_lv_t *lv, const char *cls_name)
612 {
613         FILE *f;
614         char buf[128];
615         ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", lv->irg, cls_name);
616         if((f = fopen(buf, "wt")) != NULL) {
617                 be_liveness_dump(lv, f);
618                 fclose(f);
619         }
620 }
621
622 /**
623  * Walker: checks the every predecessors of a node dominate
624  * the note.
625  */
626 static void dom_check(ir_node *irn, void *data)
627 {
628         if(!is_Block(irn) && irn != get_irg_end(get_irn_irg(irn))) {
629                 int i, n;
630                 ir_node *bl = get_nodes_block(irn);
631
632                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
633                         ir_node *op     = get_irn_n(irn, i);
634                         ir_node *def_bl = get_nodes_block(op);
635                         ir_node *use_bl = bl;
636
637                         if(is_Phi(irn))
638                                 use_bl = get_Block_cfgpred_block(bl, i);
639
640                         if(!block_dominates(def_bl, use_bl)) {
641                                 ir_fprintf(stderr, "%+F in %+F must dominate %+F for user %+F\n", op, def_bl, use_bl, irn);
642                                 assert(0);
643                         }
644                 }
645         }
646 }
647
648 /* Check, if the SSA dominance property is fulfilled. */
649 void be_check_dominance(ir_graph *irg)
650 {
651         irg_walk_graph(irg, dom_check, NULL, NULL);
652 }
653
654 pset *be_liveness_transfer(const arch_env_t *arch_env, const arch_register_class_t *cls, ir_node *irn, pset *live)
655 {
656         int i, n;
657         ir_node *x;
658         FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, DBG_MODULE);
659
660         DEBUG_ONLY(
661                 DBG((dbg, LEVEL_1, "%+F\n", irn));
662                 for(x = pset_first(live); x; x = pset_next(live))
663                         DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
664         )
665
666         /* You should better break out of your loop when hitting the first phi function. */
667         assert(!is_Phi(irn) && "liveness_transfer produces invalid results for phi nodes");
668
669         if(arch_irn_consider_in_reg_alloc(arch_env, cls, irn)) {
670                 ir_node *del = pset_remove_ptr(live, irn);
671                 assert(irn == del);
672         }
673
674         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
675                 ir_node *op = get_irn_n(irn, i);
676
677                 if(arch_irn_consider_in_reg_alloc(arch_env, cls, op))
678                         pset_insert_ptr(live, op);
679         }
680
681         return live;
682 }
683
684 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)
685 {
686         int i;
687         be_lv_foreach(lv, bl, be_lv_state_end, i) {
688                 ir_node *irn = be_lv_get_irn(lv, bl, i);
689                 if(arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
690                         pset_insert_ptr(live, irn);
691         }
692
693         return live;
694 }
695
696 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 {
698         const ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
699         ir_node *irn;
700
701         be_liveness_end_of_block(lv, arch_env, cls, bl, live);
702         sched_foreach_reverse(bl, irn) {
703                 /*
704                  * If we encounter the node we want to insert the Perm after,
705                  * exit immediately, so that this node is still live
706                  */
707                 if(irn == pos)
708                         return live;
709
710                 be_liveness_transfer(arch_env, cls, irn, live);
711         }
712
713         return live;
714 }