remove unused be_FrameLoad and be_FrameStore
[libfirm] / ir / be / bessaconstr.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       SSA construction for a set of nodes
23  * @author      Sebastian Hack, Daniel Grund, Matthias Braun, Christian Wuerdig
24  * @date        04.05.2005
25  * @version     $Id$
26  *
27  * The problem: Given a value and a set of "copies" that are known to
28  * represent the same abstract value, rewire all usages of the original value
29  * to their closest copy while introducing phis as necessary.
30  *
31  * Algorithm: Mark all blocks in the iterated dominance frontiers of the value
32  * and it's copies. Link the copies ordered by dominance to the blocks.  The
33  * we search for each use all all definitions in the current block, if none is
34  * found, then we search one in the immediate dominator. If we are in a block
35  * of the dominance frontier, create a phi and search do the same search for
36  * the phi arguments.
37  *
38  * A copy in this context means, that you want to introduce several new
39  * abstract values (in Firm: nodes) for which you know, that they
40  * represent the same concrete value. This is the case if you
41  * - copy
42  * - spill and reload
43  * - re-materialize
44  * a value.
45  *
46  * This function reroutes all uses of the original value to the copies in the
47  * corresponding dominance subtrees and creates Phi functions where necessary.
48  */
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52
53 #include "bessaconstr.h"
54 #include "bemodule.h"
55 #include "besched_t.h"
56 #include "beintlive_t.h"
57 #include "beirg_t.h"
58
59 #include "debug.h"
60 #include "error.h"
61 #include "pdeq.h"
62 #include "array.h"
63 #include "irdom.h"
64
65 #include "ircons.h"
66 #include "iredges_t.h"
67
68 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
69
70 /**
71  * Calculates the iterated dominance frontier of a set of blocks. Marks the
72  * blocks as visited. Sets the link fields of the blocks in the dominance
73  * frontier to the block itself.
74  */
75 static
76 void mark_iterated_dominance_frontiers(const be_dom_front_info_t *domfronts,
77                                        waitq *worklist)
78 {
79         DBG((dbg, LEVEL_3, "Dominance Frontier:"));
80         while(!pdeq_empty(worklist)) {
81                 int i;
82                 ir_node *block = waitq_get(worklist);
83                 ir_node **domfront = be_get_dominance_frontier(domfronts, block);
84                 int domfront_len = ARR_LEN(domfront);
85
86                 for (i = 0; i < domfront_len; ++i) {
87                         ir_node *y = domfront[i];
88                         if(Block_block_visited(y))
89                                 continue;
90
91                         if(!irn_visited(y)) {
92                                 set_irn_link(y, NULL);
93                                 waitq_put(worklist, y);
94                         }
95                         DBG((dbg, LEVEL_3, " %+F", y));
96                         mark_Block_block_visited(y);
97                 }
98         }
99         DBG((dbg, LEVEL_3, "\n"));
100 }
101
102 static
103 ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
104                                  ir_node *block);
105
106 static
107 ir_node *create_phi(be_ssa_construction_env_t *env, ir_node *block,
108                     ir_node *link_with)
109 {
110         int i, n_preds = get_Block_n_cfgpreds(block);
111         ir_graph *irg = get_irn_irg(block);
112         ir_node *phi;
113         ir_node **ins = alloca(n_preds * sizeof(ins[0]));
114
115         assert(n_preds > 1);
116
117         for(i = 0; i < n_preds; ++i) {
118                 ins[i] = new_r_Unknown(irg, env->mode);
119         }
120         phi = new_r_Phi(irg, block, n_preds, ins, env->mode);
121         if(env->new_phis != NULL) {
122                 ARR_APP1(ir_node*, env->new_phis, phi);
123         }
124
125         if(env->mode != mode_M) {
126                 sched_add_after(block, phi);
127         }
128
129         DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, block));
130         set_irn_link(link_with, phi);
131         mark_irn_visited(block);
132
133         for(i = 0; i < n_preds; ++i) {
134                 ir_node *pred_block = get_Block_cfgpred_block(block, i);
135                 ir_node *pred_def   = search_def_end_of_block(env, pred_block);
136
137                 set_irn_n(phi, i, pred_def);
138         }
139
140         return phi;
141 }
142
143 static
144 ir_node *get_def_at_idom(be_ssa_construction_env_t *env, ir_node *block)
145 {
146         ir_node *dom = get_Block_idom(block);
147         assert(dom != NULL);
148         return search_def_end_of_block(env, dom);
149 }
150
151 static
152 ir_node *search_def_end_of_block(be_ssa_construction_env_t *env, ir_node *block)
153 {
154         if(irn_visited(block)) {
155                 assert(get_irn_link(block) != NULL);
156                 return get_irn_link(block);
157         } else if(Block_block_visited(block)) {
158                 return create_phi(env, block, block);
159         } else {
160                 ir_node *def = get_def_at_idom(env, block);
161                 mark_irn_visited(block);
162                 set_irn_link(block, def);
163
164                 return def;
165         }
166 }
167
168 static
169 ir_node *search_def(be_ssa_construction_env_t *env, ir_node *at)
170 {
171         ir_node *block = get_nodes_block(at);
172         ir_node *node;
173         ir_node *def;
174
175         DBG((dbg, LEVEL_3, "\t...searching def at %+F\n", at));
176
177         /* no defs in the current block we can do the normal searching */
178         if(!irn_visited(block) && !Block_block_visited(block)) {
179                 DBG((dbg, LEVEL_3, "\t...continue at idom\n"));
180                 return get_def_at_idom(env, block);
181         }
182
183         /* there are defs in the current block, walk the linked list to find
184            the one immediately dominating us
185          */
186         node = block;
187         def  = get_irn_link(node);
188         while(def != NULL) {
189                 if(!value_dominates(at, def)) {
190                         DBG((dbg, LEVEL_3, "\t...found dominating def %+F\n", def));
191                         return def;
192                 }
193
194                 node = def;
195                 def  = get_irn_link(node);
196         }
197
198         /* block in dominance frontier? create a phi then */
199         if(Block_block_visited(block)) {
200                 DBG((dbg, LEVEL_3, "\t...create phi at block %+F\n", block));
201                 assert(!is_Phi(node));
202                 return create_phi(env, block, node);
203         }
204
205         DBG((dbg, LEVEL_3, "\t...continue at idom (after checking block)\n"));
206         return get_def_at_idom(env, block);
207 }
208
209 /**
210  * Adds a definition into the link field of the block. The definitions are
211  * sorted by dominance. A non-visited block means no definition has been
212  * inserted yet.
213  */
214 static
215 void introduce_def_at_block(ir_node *block, ir_node *def)
216 {
217         if(irn_visited(block)) {
218                 ir_node *node = block;
219                 ir_node *current_def;
220
221                 while(1) {
222                         current_def = get_irn_link(node);
223                         if(current_def == def) {
224                                 /* already in block */
225                                 return;
226                         }
227                         if(current_def == NULL)
228                                 break;
229                         if(value_dominates(current_def, def))
230                                 break;
231                         node = current_def;
232                 }
233
234                 set_irn_link(node, def);
235                 set_irn_link(def, current_def);
236         } else {
237                 set_irn_link(block, def);
238                 set_irn_link(def, NULL);
239                 mark_irn_visited(block);
240         }
241 }
242
243 void be_ssa_construction_init(be_ssa_construction_env_t *env, be_irg_t *birg)
244 {
245         ir_graph *irg = be_get_birg_irg(birg);
246         memset(env, 0, sizeof(env[0]));
247
248         be_assure_dom_front(birg);
249
250         env->irg       = irg;
251         env->domfronts = be_get_birg_dom_front(birg);
252         env->new_phis  = NEW_ARR_F(ir_node*, 0);
253         env->worklist  = new_waitq();
254
255         set_using_visited(irg);
256         set_using_block_visited(irg);
257         set_using_irn_link(irg);
258
259         /* we use the visited flag to indicate blocks in the dominance frontier
260          * and blocks that already have the relevant value at the end calculated */
261         inc_irg_visited(irg);
262         /* We use the block visited flag to indicate blocks in the dominance
263          * froniter of some values (and this potentially needing phis) */
264         inc_irg_block_visited(irg);
265 }
266
267 void be_ssa_construction_destroy(be_ssa_construction_env_t *env)
268 {
269         del_waitq(env->worklist);
270         DEL_ARR_F(env->new_phis);
271
272         clear_using_visited(env->irg);
273         clear_using_block_visited(env->irg);
274         clear_using_irn_link(env->irg);
275 }
276
277 void be_ssa_construction_add_copy(be_ssa_construction_env_t *env,
278                                   ir_node *copy)
279 {
280         ir_node *block;
281
282         assert(env->iterated_domfront_calculated == 0);
283
284         if(env->mode == NULL) {
285                 env->mode = get_irn_mode(copy);
286         } else {
287                 assert(env->mode == get_irn_mode(copy));
288         }
289
290         block = get_nodes_block(copy);
291
292         if(!irn_visited(block)) {
293                 waitq_put(env->worklist, block);
294         }
295         introduce_def_at_block(block, copy);
296 }
297
298 void be_ssa_construction_add_copies(be_ssa_construction_env_t *env,
299                                     ir_node **copies, size_t copies_len)
300 {
301         size_t i;
302
303         assert(env->iterated_domfront_calculated == 0);
304
305         if(env->mode == NULL) {
306                 env->mode = get_irn_mode(copies[0]);
307         }
308
309         for(i = 0; i < copies_len; ++i) {
310                 ir_node *copy = copies[i];
311                 ir_node *block = get_nodes_block(copy);
312
313                 assert(env->mode == get_irn_mode(copy));
314                 if(!irn_visited(block)) {
315                         waitq_put(env->worklist, block);
316                 }
317                 introduce_def_at_block(block, copy);
318         }
319 }
320
321 void be_ssa_construction_set_ignore_uses(be_ssa_construction_env_t *env,
322                                          const ir_nodeset_t *ignore_uses)
323 {
324         env->ignore_uses = ignore_uses;
325 }
326
327 ir_node **be_ssa_construction_get_new_phis(be_ssa_construction_env_t *env)
328 {
329         return env->new_phis;
330 }
331
332 void be_ssa_construction_fix_users(be_ssa_construction_env_t *env,
333                                    ir_node *value)
334 {
335         const ir_edge_t *edge, *next;
336
337         if(!env->iterated_domfront_calculated) {
338                 mark_iterated_dominance_frontiers(env->domfronts, env->worklist);
339                 env->iterated_domfront_calculated = 1;
340         }
341
342         /*
343          * Search the valid def for each use and set it.
344          */
345         foreach_out_edge_safe(value, edge, next) {
346                 ir_node *use = get_edge_src_irn(edge);
347                 ir_node *at  = use;
348                 int pos      = get_edge_src_pos(edge);
349                 ir_node *def;
350
351                 if(env->ignore_uses != NULL     &&
352                    ir_nodeset_contains(env->ignore_uses, use))
353                         continue;
354                 if(is_Anchor(use))
355                         continue;
356
357                 if(is_Phi(use)) {
358                         ir_node *block = get_nodes_block(use);
359                         ir_node *predblock = get_Block_cfgpred_block(block, pos);
360                         at = sched_last(predblock);
361                 }
362
363                 def = search_def(env, at);
364
365                 if(def == NULL) {
366                         panic("no definition found for %+F at position %d\n", use, pos);
367                 }
368
369                 DBG((dbg, LEVEL_2, "\t%+F(%d) -> %+F\n", use, pos, def));
370                 set_irn_n(use, pos, def);
371         }
372 }
373
374 void be_ssa_construction_fix_users_array(be_ssa_construction_env_t *env,
375                                          ir_node **nodes, size_t nodes_len)
376 {
377         size_t i;
378
379         for(i = 0; i < nodes_len; ++i) {
380                 ir_node *node = nodes[i];
381                 be_ssa_construction_fix_users(env, node);
382         }
383 }
384
385 void be_ssa_construction_update_liveness_phis(be_ssa_construction_env_t *env,
386                                               be_lv_t *lv)
387 {
388         int i, n;
389
390         n = ARR_LEN(env->new_phis);
391         for(i = 0; i < n; ++i) {
392                 ir_node *phi = env->new_phis[i];
393                 be_liveness_introduce(lv, phi);
394         }
395 }
396
397 void be_init_ssaconstr(void)
398 {
399         FIRM_DBG_REGISTER(dbg, "firm.be.ssaconstr");
400 }
401
402 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ssaconstr);