Added clique walker
[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
15 #include "beutil.h"
16 #include "belive_t.h"
17
18 FIRM_IMPL2(is_live_in, int, const ir_node *, const ir_node *)
19 FIRM_IMPL2(is_live_out, int, const ir_node *, const ir_node *)
20 FIRM_IMPL2(is_live_end, int, const ir_node *, const ir_node *)
21
22 /** The offset of the liveness information in a firm node. */
23 size_t live_irg_data_offset = 0;
24
25 void be_liveness_init(void)
26 {
27   live_irg_data_offset = register_additional_graph_data(sizeof(irn_live_t));
28 }
29
30 static INLINE void mark_live_in(ir_node *block, const ir_node *irn)
31 {
32   _get_or_set_live(block, irn, live_state_in);
33 }
34
35 static INLINE void mark_live_out(ir_node *block, const ir_node *irn)
36 {
37   _get_or_set_live(block, irn, live_state_out | live_state_end);
38 }
39
40 static INLINE void mark_live_end(ir_node *block, const ir_node *irn)
41 {
42   _get_or_set_live(block, irn, live_state_end);
43 }
44
45 /**
46  * Mark a node (value) live out at a certain block. Do this also
47  * transitively, i.e. if the block is not the block of the value's
48  * definition, all predecessors are also marked live.
49  * @param def The node (value).
50  * @param block The block to mark the value live out of.
51  * @param visited A set were all visited blocks are recorded.
52  * @param is_true_out Is the node real out there or only live at the end
53  * of the block.
54  */
55 static void live_end_at_block(ir_node *def, ir_node *block,
56     pset *visited, int is_true_out)
57 {
58   mark_live_end(block, def);
59   if(is_true_out)
60     mark_live_out(block, def);
61
62   if(!pset_find_ptr(visited, block)) {
63
64     pset_insert_ptr(visited, block);
65
66     /*
67      * If this block is not the definition block, we have to go up
68      * further.
69      */
70     if(get_nodes_block(def) != block) {
71       int i, n;
72
73       mark_live_in(block, def);
74
75       for(i = 0, n = get_irn_arity(block); i < n; ++i)
76         live_end_at_block(def, get_Block_cfgpred_block(block, i), visited, 1);
77     }
78
79   }
80 }
81
82 /**
83  * Liveness analysis for a value.
84  * This functions is meant to be called by a firm walker, to compute the
85  * set of all blocks a value is live in.
86  * @param irn The node (value).
87  * @param env Ignored.
88  */
89 static void liveness_for_node(ir_node *irn, void *env)
90 {
91   const ir_edge_t *edge;
92   ir_node *def_block;
93   pset *visited;
94
95   /* Don't compute liveness information for non-data nodes. */
96   if(!is_data_node(irn))
97     return;
98
99   visited = pset_new_ptr(512);
100   def_block = get_nodes_block(irn);
101
102   /* Go over all uses of the value */
103   foreach_out_edge(irn, edge) {
104     ir_node *use = edge->src;
105     ir_node *use_block;
106
107     /*
108      * If the usage is no data node, skip this use, since it does not
109      * affect the liveness of the node.
110      */
111     if(!is_data_node(use))
112       continue;
113
114     /* Get the block where the usage is in. */
115     use_block = get_nodes_block(use);
116
117     /*
118      * If the use is a phi function, determine the corresponding block
119      * through which the value reaches the phi function and mark the
120      * value as live out of that block.
121      */
122     if(is_Phi(use)) {
123                         ir_node *pred_block = get_Block_cfgpred_block(use_block, edge->pos);
124                         live_end_at_block(irn, pred_block, visited, 0);
125     }
126
127     /*
128      * Else, the value is live in at this block. Mark it and call live
129      * out on the predecessors.
130      */
131     else if(def_block != use_block) {
132       int i, n;
133
134       mark_live_in(use_block, irn);
135
136       for(i = 0, n = get_irn_arity(use_block); i < n; ++i) {
137         ir_node *pred_block = get_nodes_block(get_irn_n(use_block, i));
138         live_end_at_block(irn, pred_block, visited, 1);
139       }
140     }
141   }
142
143   del_pset(visited);
144 }
145
146 static int cmp_irn_live(const void *a, const void *b, size_t size)
147 {
148   const irn_live_t *p = a;
149   const irn_live_t *q = b;
150
151   return !(p->block == q->block && p->irn == q->irn);
152 }
153
154 static int (*old_dump_block_func)(ir_node *self, FILE *F, dump_reason_t reason) = NULL;
155
156 static int dump_block_func(ir_node *self, FILE *F, dump_reason_t reason)
157 {
158         switch(reason) {
159         case dump_node_opcode_txt:
160                 fprintf(F, get_irn_opname(self));
161                 break;
162         case dump_node_mode_txt:
163                 fprintf(F, get_irn_modename(self));
164                 break;
165         case dump_node_nodeattr_txt:
166                 break;
167         case dump_node_info_txt:
168                 if(!get_irg_live_info(get_irn_irg(self))->live)
169                         return 0;
170
171                 fprintf(F, "liveness information:\n");
172                 {
173                         irn_live_t *li;
174                         live_foreach(self, li) {
175                                 ir_fprintf(F, "%+F", li->irn);
176                                 if(live_is_in(li))
177                                         fprintf(F, " in");
178                                 if(live_is_end(li))
179                                         fprintf(F, " end");
180                                 if(live_is_out(li))
181                                         fprintf(F, " out");
182
183                                 fprintf(F, "\n");
184                         }
185                 }
186         }
187
188         return 0;
189 }
190
191 void be_liveness(ir_graph *irg)
192 {
193   irg_live_info_t *live_info = get_irg_live_info(irg);
194   if(live_info->live)
195     del_set(live_info->live);
196
197   live_info->live = new_set(cmp_irn_live, 8192);
198   irg_walk_graph(irg, liveness_for_node, NULL, NULL);
199
200   old_dump_block_func     = op_Block->ops.dump_node;
201   op_Block->ops.dump_node = dump_block_func;
202 }
203
204 static void dump_liveness_walker(ir_node *bl, void *data)
205 {
206         FILE *f = data;
207         const irn_live_t *li;
208         char buf[64];
209
210         ir_fprintf(f, "%+F\n", bl);
211         live_foreach(bl, li) {
212                 strcpy(buf, "");
213
214                 if(live_is_in(li))
215                         strcat(buf, "in ");
216
217                 if(live_is_end(li))
218                         strcat(buf, "end ");
219
220                 if(live_is_out(li))
221                         strcat(buf, "out ");
222
223                 ir_fprintf(f, "\t%+20F %s\n", li->irn, buf);
224         }
225 }
226
227 void be_liveness_dump(ir_graph *irg, FILE *f)
228 {
229         irg_block_walk_graph(irg, dump_liveness_walker, NULL, f);
230 }
231
232 void be_liveness_dumpto(ir_graph *irg, const char *cls_name)
233 {
234         FILE *f;
235         char buf[128];
236         ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", irg, cls_name);
237         if((f = fopen(buf, "wt")) != NULL) {
238                 be_liveness_dump(irg, f);
239                 fclose(f);
240         }
241 }
242
243 static void dom_check(ir_node *irn, void *data)
244 {
245         if(!is_Block(irn) && irn != get_irg_end(get_irn_irg(irn))) {
246                 int i, n;
247                 ir_node *bl = get_nodes_block(irn);
248
249                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
250                         ir_node *op     = get_irn_n(irn, i);
251                         ir_node *def_bl = get_nodes_block(op);
252                         ir_node *use_bl = bl;
253
254                         if(is_Phi(irn))
255                                 use_bl = get_Block_cfgpred_block(bl, i);
256
257                         if(!block_dominates(def_bl, use_bl)) {
258                                 ir_fprintf(stderr, "%+F in %+F must dominate %+F for user %+F\n", op, def_bl, use_bl, irn);
259                                 assert(0);
260                         }
261                 }
262         }
263 }
264
265 void be_check_dominance(ir_graph *irg)
266 {
267         irg_walk_graph(irg, dom_check, NULL, NULL);
268 }