slightly optimize liveness code
[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  */
26 #include "config.h"
27
28 /* statev is expensive here, only enable when needed */
29 #define DISABLE_STATEV
30
31 #include "iredges_t.h"
32 #include "irgwalk.h"
33 #include "irprintf.h"
34 #include "irdump_t.h"
35 #include "irnodeset.h"
36
37 #include "absgraph.h"
38 #include "statev_t.h"
39 #include "be_t.h"
40 #include "bearch.h"
41 #include "beutil.h"
42 #include "belive_t.h"
43 #include "besched.h"
44 #include "bemodule.h"
45 #include "bedump.h"
46
47 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
48
49 #define LV_STD_SIZE             64
50
51 /**
52  * Filter out some nodes for which we never need liveness.
53  *
54  * @param irn  the node t check
55  * @return 0 if no liveness info is needed, 1 else
56  */
57 static inline int is_liveness_node(const ir_node *irn)
58 {
59         switch (get_irn_opcode(irn)) {
60         case iro_Block:
61         case iro_Bad:
62         case iro_End:
63         case iro_Anchor:
64         case iro_NoMem:
65                 return 0;
66         default:
67                 return 1;
68         }
69 }
70
71 int (be_is_live_in)(const be_lv_t *lv, const ir_node *block, const ir_node *irn)
72 {
73         return _be_is_live_xxx(lv, block, irn, be_lv_state_in);
74 }
75
76 int (be_is_live_out)(const be_lv_t *lv, const ir_node *block, const ir_node *irn)
77 {
78         return _be_is_live_xxx(lv, block, irn, be_lv_state_out);
79 }
80
81 int (be_is_live_end)(const be_lv_t *lv, const ir_node *block, const ir_node *irn)
82 {
83         return _be_is_live_xxx(lv, block, irn, be_lv_state_end);
84 }
85
86 static inline unsigned _be_liveness_bsearch(be_lv_info_t *arr, const ir_node *node)
87 {
88         be_lv_info_t *payload = arr + 1;
89
90         unsigned n   = arr[0].head.n_members;
91         unsigned res = 0;
92         int lo       = 0;
93         int hi       = n;
94
95         if (n == 0)
96                 return 0;
97
98         do {
99                 int md           = lo + ((hi - lo) >> 1);
100                 ir_node *md_node = payload[md].node.node;
101
102                 if (node > md_node)
103                         lo = md + 1;
104                 else if (node < md_node)
105                         hi = md;
106                 else {
107                         res = md;
108                         break;
109                 }
110
111                 res = lo;
112         } while (lo < hi);
113
114         return res;
115 }
116
117 be_lv_info_node_t *be_lv_get(const be_lv_t *li, const ir_node *bl,
118                              const ir_node *irn)
119 {
120         be_lv_info_t *irn_live;
121         be_lv_info_node_t *res = NULL;
122
123         stat_ev_tim_push();
124         irn_live = ir_nodehashmap_get(be_lv_info_t, &li->map, bl);
125         if (irn_live != NULL) {
126                 /* Get the position of the index in the array. */
127                 int pos = _be_liveness_bsearch(irn_live, irn);
128
129                 /* Get the record in question. 1 must be added, since the first record contains information about the array and must be skipped. */
130                 be_lv_info_node_t *rec = &irn_live[pos + 1].node;
131
132                 /* Check, if the irn is in deed in the array. */
133                 if (rec->node == irn)
134                         res = rec;
135         }
136         stat_ev_tim_pop("be_lv_get");
137
138         return res;
139 }
140
141 static be_lv_info_node_t *be_lv_get_or_set(be_lv_t *li, ir_node *bl,
142                                            ir_node *irn)
143 {
144         be_lv_info_t *irn_live = ir_nodehashmap_get(be_lv_info_t, &li->map, bl);
145         if (irn_live == NULL) {
146                 irn_live = OALLOCNZ(&li->obst, be_lv_info_t, LV_STD_SIZE);
147                 irn_live[0].head.n_size = LV_STD_SIZE-1;
148                 ir_nodehashmap_insert(&li->map, bl, irn_live);
149         }
150
151         /* Get the position of the index in the array. */
152         unsigned pos = _be_liveness_bsearch(irn_live, irn);
153
154         /* Get the record in question. 1 must be added, since the first record contains information about the array and must be skipped. */
155         be_lv_info_node_t *res = &irn_live[pos + 1].node;
156
157         /* Check, if the irn is in deed in the array. */
158         if (res->node != irn) {
159                 be_lv_info_t *payload;
160                 unsigned n_members = irn_live[0].head.n_members;
161                 unsigned n_size    = irn_live[0].head.n_size;
162                 unsigned i;
163
164                 if (n_members + 1 >= n_size) {
165                         /* double the array size. Remember that the first entry is
166                          * metadata about the array and not a real array element */
167                         unsigned old_size_bytes  = (n_size + 1) * sizeof(irn_live[0]);
168                         unsigned new_size        = (2 * n_size) + 1;
169                         size_t   new_size_bytes  = new_size * sizeof(irn_live[0]);
170                         be_lv_info_t *nw = OALLOCN(&li->obst, be_lv_info_t, new_size);
171                         memcpy(nw, irn_live, old_size_bytes);
172                         memset(((char*) nw) + old_size_bytes, 0,
173                                new_size_bytes - old_size_bytes);
174                         nw[0].head.n_size = new_size - 1;
175                         irn_live = nw;
176                         ir_nodehashmap_insert(&li->map, bl, nw);
177                 }
178
179                 payload = &irn_live[1];
180                 for (i = n_members; i > pos; --i) {
181                         payload[i] = payload[i - 1];
182                 }
183
184                 ++irn_live[0].head.n_members;
185
186                 res        = &payload[pos].node;
187                 res->node  = irn;
188                 res->flags = 0;
189         }
190
191         return res;
192 }
193
194 /**
195  * Removes a node from the list of live variables of a block.
196  * @return 1 if the node was live at that block, 0 if not.
197  */
198 static int be_lv_remove(be_lv_t *li, const ir_node *bl,
199                         const ir_node *irn)
200 {
201         be_lv_info_t *irn_live = ir_nodehashmap_get(be_lv_info_t, &li->map, bl);
202
203         if (irn_live != NULL) {
204                 unsigned n   = irn_live[0].head.n_members;
205                 unsigned pos = _be_liveness_bsearch(irn_live, irn);
206                 be_lv_info_t *payload  = irn_live + 1;
207                 be_lv_info_node_t *res = &payload[pos].node;
208
209                 /* The node is in deed in the block's array. Let's remove it. */
210                 if (res->node == irn) {
211                         unsigned i;
212
213                         for (i = pos + 1; i < n; ++i)
214                                 payload[i - 1] = payload[i];
215
216                         payload[n - 1].node.node  = NULL;
217                         payload[n - 1].node.flags = 0;
218
219                         --irn_live[0].head.n_members;
220                         DBG((dbg, LEVEL_3, "\tdeleting %+F from %+F at pos %d\n", irn, bl, pos));
221                         return 1;
222                 }
223         }
224
225         return 0;
226 }
227
228 /**
229  * Mark a node as live-in in a block.
230  */
231 static inline void mark_live_in(be_lv_t *lv, ir_node *block, ir_node *irn)
232 {
233         be_lv_info_node_t *n = be_lv_get_or_set(lv, block, irn);
234         DBG((dbg, LEVEL_2, "marking %+F live in at %+F\n", irn, block));
235         n->flags |= be_lv_state_in;
236 }
237
238 /**
239  * Mark a node as live-out in a block.
240  */
241 static inline void mark_live_out(be_lv_t *lv, ir_node *block, ir_node *irn)
242 {
243         be_lv_info_node_t *n = be_lv_get_or_set(lv, block, irn);
244         DBG((dbg, LEVEL_2, "marking %+F live out at %+F\n", irn, block));
245         n->flags |= be_lv_state_out | be_lv_state_end;
246 }
247
248 /**
249  * Mark a node as live-end in a block.
250  */
251 static inline void mark_live_end(be_lv_t *lv, ir_node *block, ir_node *irn)
252 {
253         be_lv_info_node_t *n = be_lv_get_or_set(lv, block, irn);
254         DBG((dbg, LEVEL_2, "marking %+F live end at %+F\n", irn, block));
255         n->flags |= be_lv_state_end;
256 }
257
258 static struct {
259         be_lv_t  *lv;         /**< The liveness object. */
260         ir_node  *def;        /**< The node (value). */
261         ir_node  *def_block;  /**< The block of def. */
262         bitset_t *visited;    /**< A set were all visited blocks are recorded. */
263 } re;
264
265 /**
266  * Mark a node (value) live out at a certain block. Do this also
267  * transitively, i.e. if the block is not the block of the value's
268  * definition, all predecessors are also marked live.
269  * @param block The block to mark the value live out of.
270  * @param is_true_out Is the node real out there or only live at the end
271  * of the block.
272  */
273 static void live_end_at_block(ir_node *block, int is_true_out)
274 {
275         be_lv_t *lv  = re.lv;
276         ir_node *def = re.def;
277         bitset_t *visited;
278
279         mark_live_end(lv, block, def);
280         if (is_true_out)
281                 mark_live_out(lv, block, def);
282
283         visited = re.visited;
284         if (!bitset_is_set(visited, get_irn_idx(block))) {
285                 bitset_set(visited, get_irn_idx(block));
286
287                 /*
288                  * If this block is not the definition block, we have to go up
289                  * further.
290                  */
291                 if (re.def_block != block) {
292                         int i;
293
294                         mark_live_in(lv, block, def);
295
296                         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i)
297                                 live_end_at_block(get_Block_cfgpred_block(block, i), 1);
298                 }
299         }
300 }
301
302 typedef struct lv_remove_walker_t {
303         be_lv_t       *lv;
304         const ir_node *irn;
305 } lv_remove_walker_t;
306
307
308 /**
309  * Liveness analysis for a value.
310  * Compute the set of all blocks a value is live in.
311  * @param irn     The node (value).
312  */
313 static void liveness_for_node(ir_node *irn)
314 {
315         ir_node *def_block;
316
317         bitset_clear_all(re.visited);
318         def_block = get_nodes_block(irn);
319
320         re.def       = irn;
321         re.def_block = def_block;
322
323         /* Go over all uses of the value */
324         foreach_out_edge(irn, edge) {
325                 ir_node *use = edge->src;
326                 ir_node *use_block;
327
328                 DBG((dbg, LEVEL_4, "%+F: use at %+F, pos %d in %+F\n", irn, use, edge->pos, get_block(use)));
329                 assert(get_irn_n(use, edge->pos) == irn);
330
331                 /*
332                  * If the usage is no data node, skip this use, since it does not
333                  * affect the liveness of the node.
334                  */
335                 if (!is_liveness_node(use))
336                         continue;
337
338                 /* Get the block where the usage is in. */
339                 use_block = get_nodes_block(use);
340
341                 /*
342                  * If the use is a phi function, determine the corresponding block
343                  * through which the value reaches the phi function and mark the
344                  * value as live out of that block.
345                  */
346                 if (is_Phi(use)) {
347                         ir_node *pred_block = get_Block_cfgpred_block(use_block, edge->pos);
348                         live_end_at_block(pred_block, 0);
349                 }
350
351                 /*
352                  * Else, the value is live in at this block. Mark it and call live
353                  * out on the predecessors.
354                  */
355                 else if (def_block != use_block) {
356                         int i;
357
358                         mark_live_in(re.lv, use_block, irn);
359
360                         for (i = get_Block_n_cfgpreds(use_block) - 1; i >= 0; --i) {
361                                 ir_node *pred_block = get_Block_cfgpred_block(use_block, i);
362                                 live_end_at_block(pred_block, 1);
363                         }
364                 }
365         }
366 }
367
368 static void lv_remove_irn_walker(ir_node *bl, void *data)
369 {
370         lv_remove_walker_t *w = (lv_remove_walker_t*)data;
371         be_lv_remove(w->lv, bl, w->irn);
372 }
373
374 /**
375  * Walker, collect all nodes for which we want calculate liveness info
376  * on an obstack.
377  */
378 static void collect_liveness_nodes(ir_node *irn, void *data)
379 {
380         ir_node **nodes = (ir_node**)data;
381         if (is_liveness_node(irn))
382                 nodes[get_irn_idx(irn)] = irn;
383 }
384
385 void be_liveness_compute_sets(be_lv_t *lv)
386 {
387         ir_node **nodes;
388         int       i;
389         int       n;
390
391         if (lv->sets_valid)
392                 return;
393
394         be_timer_push(T_LIVE);
395         ir_nodehashmap_init(&lv->map);
396         obstack_init(&lv->obst);
397
398         n = get_irg_last_idx(lv->irg);
399         nodes = NEW_ARR_F(ir_node *, n);
400         memset(nodes, 0, sizeof(nodes[0]) * n);
401
402         /* inserting the variables sorted by their ID is probably
403          * more efficient since the binary sorted set insertion
404          * will not need to move around the data. */
405         irg_walk_graph(lv->irg, NULL, collect_liveness_nodes, nodes);
406
407         re.lv      = lv;
408         re.visited = bitset_malloc(n);
409
410         for (i = 0; i < n; ++i) {
411                 if (nodes[i] != NULL)
412                         liveness_for_node(nodes[i]);
413         }
414
415         DEL_ARR_F(nodes);
416         free(re.visited);
417         register_hook(hook_node_info, &lv->hook_info);
418
419         be_timer_pop(T_LIVE);
420
421         lv->sets_valid = true;
422 }
423
424 void be_liveness_compute_chk(be_lv_t *lv)
425 {
426         if (lv->lvc != NULL)
427                 return;
428         lv->lvc = lv_chk_new(lv->irg);
429 }
430
431 void be_liveness_invalidate_sets(be_lv_t *lv)
432 {
433         if (!lv->sets_valid)
434                 return;
435         unregister_hook(hook_node_info, &lv->hook_info);
436         obstack_free(&lv->obst, NULL);
437         ir_nodehashmap_destroy(&lv->map);
438         lv->sets_valid = false;
439 }
440
441 void be_liveness_invalidate_chk(be_lv_t *lv)
442 {
443         be_liveness_invalidate_sets(lv);
444
445         if (lv->lvc == NULL)
446                 return;
447         lv_chk_free(lv->lvc);
448         lv->lvc = NULL;
449 }
450
451 be_lv_t *be_liveness_new(ir_graph *irg)
452 {
453         be_lv_t *lv = XMALLOCZ(be_lv_t);
454
455         lv->irg = irg;
456         lv->hook_info.context = lv;
457         lv->hook_info.hook._hook_node_info = be_dump_liveness_block;
458
459         return lv;
460 }
461
462 void be_liveness_free(be_lv_t *lv)
463 {
464         be_liveness_invalidate_sets(lv);
465         be_liveness_invalidate_chk(lv);
466
467         xfree(lv);
468 }
469
470 void be_liveness_remove(be_lv_t *lv, const ir_node *irn)
471 {
472         if (lv->sets_valid) {
473                 lv_remove_walker_t w;
474
475                 /*
476                  * Removes a single irn from the liveness information.
477                  * Since an irn can only be live at blocks dominated by the block of its
478                  * definition, we only have to process that dominance subtree.
479                  */
480                 w.lv  = lv;
481                 w.irn = irn;
482                 dom_tree_walk(get_nodes_block(irn), lv_remove_irn_walker, NULL, &w);
483         }
484 }
485
486 void be_liveness_introduce(be_lv_t *lv, ir_node *irn)
487 {
488         /* Don't compute liveness information for non-data nodes. */
489         if (lv->sets_valid && is_liveness_node(irn)) {
490                 re.lv      = lv;
491                 re.visited = bitset_malloc(get_irg_last_idx(lv->irg));
492                 liveness_for_node(irn);
493                 bitset_free(re.visited);
494         }
495 }
496
497 void be_liveness_update(be_lv_t *lv, ir_node *irn)
498 {
499         be_liveness_remove(lv, irn);
500         be_liveness_introduce(lv, irn);
501 }
502
503 void be_liveness_transfer(const arch_register_class_t *cls,
504                           ir_node *node, ir_nodeset_t *nodeset)
505 {
506         /* You should better break out of your loop when hitting the first phi
507          * function. */
508         assert(!is_Phi(node) && "liveness_transfer produces invalid results for phi nodes");
509
510         be_foreach_definition(node, cls, value,
511                 ir_nodeset_remove(nodeset, value);
512         );
513
514         int arity = get_irn_arity(node);
515         for (int i = 0; i < arity; ++i) {
516                 const arch_register_req_t *in_req = arch_get_irn_register_req_in(node, i);
517                 if (in_req->cls != cls)
518                         continue;
519                 ir_node                   *op     = get_irn_n(node, i);
520                 const arch_register_req_t *op_req = arch_get_irn_register_req(op);
521                 if (op_req->type & arch_register_req_type_ignore)
522                         continue;
523                 ir_nodeset_insert(nodeset, op);
524         }
525 }
526
527
528
529 void be_liveness_end_of_block(const be_lv_t *lv,
530                               const arch_register_class_t *cls,
531                               const ir_node *block, ir_nodeset_t *live)
532 {
533         assert(lv->sets_valid && "live sets must be computed");
534         be_lv_foreach(lv, block, be_lv_state_end, node) {
535                 if (!arch_irn_consider_in_reg_alloc(cls, node))
536                         continue;
537
538                 ir_nodeset_insert(live, node);
539         }
540 }
541
542
543
544 void be_liveness_nodes_live_before(be_lv_t const *const lv, arch_register_class_t const *const cls, ir_node const *const pos, ir_nodeset_t *const live)
545 {
546         ir_node const *const bl = get_nodes_block(pos);
547         be_liveness_end_of_block(lv, cls, bl, live);
548         sched_foreach_reverse(bl, irn) {
549                 be_liveness_transfer(cls, irn, live);
550                 if (irn == pos)
551                         return;
552         }
553 }
554
555 static void collect_node(ir_node *irn, void *data)
556 {
557         struct obstack *obst = (struct obstack*)data;
558         obstack_ptr_grow(obst, irn);
559 }
560
561 static void be_live_chk_compare(be_lv_t *lv, lv_chk_t *lvc)
562 {
563         ir_graph *irg    = lv->irg;
564
565         struct obstack obst;
566         ir_node **nodes;
567         ir_node **blocks;
568         int i, j;
569
570         obstack_init(&obst);
571
572         irg_block_walk_graph(irg, collect_node, NULL, &obst);
573         obstack_ptr_grow(&obst, NULL);
574         blocks = (ir_node**)obstack_finish(&obst);
575
576         irg_walk_graph(irg, collect_node, NULL, &obst);
577         obstack_ptr_grow(&obst, NULL);
578         nodes = (ir_node**)obstack_finish(&obst);
579
580         stat_ev_ctx_push("be_lv_chk_compare");
581         for (j = 0; nodes[j]; ++j) {
582                 ir_node *irn = nodes[j];
583                 if (is_Block(irn))
584                         continue;
585
586                 for (i = 0; blocks[i]; ++i) {
587                         ir_node *bl = blocks[i];
588                         int lvr_in  = be_is_live_in (lv, bl, irn);
589                         int lvr_out = be_is_live_out(lv, bl, irn);
590                         int lvr_end = be_is_live_end(lv, bl, irn);
591
592                         int lvc_in  = lv_chk_bl_in (lvc, bl, irn);
593                         int lvc_out = lv_chk_bl_out(lvc, bl, irn);
594                         int lvc_end = lv_chk_bl_end(lvc, bl, irn);
595
596                         if (lvr_in - lvc_in != 0)
597                                 ir_fprintf(stderr, "live in  info for %+F at %+F differs: nml: %d, chk: %d\n", irn, bl, lvr_in, lvc_in);
598
599                         if (lvr_end - lvc_end != 0)
600                                 ir_fprintf(stderr, "live end info for %+F at %+F differs: nml: %d, chk: %d\n", irn, bl, lvr_end, lvc_end);
601
602                         if (lvr_out - lvc_out != 0)
603                                 ir_fprintf(stderr, "live out info for %+F at %+F differs: nml: %d, chk: %d\n", irn, bl, lvr_out, lvc_out);
604                 }
605         }
606         stat_ev_ctx_pop("be_lv_chk_compare");
607
608         obstack_free(&obst, NULL);
609 }
610
611 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_live)
612 void be_init_live(void)
613 {
614         (void)be_live_chk_compare;
615         FIRM_DBG_REGISTER(dbg, "firm.be.liveness");
616 }