*** empty log message ***
[libfirm] / ir / be / bessadestr.c
1 /**
2  * Author:      Daniel Grund
3  * Date:                25.05.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  *
7  * Performs SSA-Destruction.
8  */
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include "debug.h"
14 #include "set.h"
15 #include "pmap.h"
16 #include "irnode_t.h"
17 #include "ircons_t.h"
18 #include "iredges_t.h"
19 #include "irgmod.h"
20 #include "irdump.h"
21 #include "irprintf.h"
22
23 #include "be_t.h"
24 #include "beutil.h"
25 #include "bechordal_t.h"
26 #include "bearch.h"
27 #include "belive_t.h"
28 #include "benode_t.h"
29 #include "besched_t.h"
30
31 static firm_dbg_module_t *dbg = NULL;
32 #define DEBUG_LVL SET_LEVEL_0
33 #undef DUMP_GRAPHS
34
35 #define get_chordal_arch(ce) ((ce)->main_env->arch_env)
36 #define get_reg(irn) arch_get_irn_register(get_chordal_arch(chordal_env), irn)
37 #define set_reg(irn, reg) arch_set_irn_register(get_chordal_arch(chordal_env), irn, reg)
38
39 #define is_Perm(irn)            (arch_irn_classify(arch_env, irn) == arch_irn_class_perm)
40 #define get_reg_cls(irn)        (arch_get_irn_reg_class(arch_env, irn, -1))
41 #define is_curr_reg_class(irn)  (get_reg_cls(p) == chordal_env->cls)
42
43 static void clear_link(ir_node *irn, void *data)
44 {
45   set_irn_link(irn, NULL);
46 }
47
48 /**
49  * Build a list of phis of a block.
50  */
51 static void collect_phis(ir_node *irn, void *data)
52 {
53   be_chordal_env_t *env = data;
54   if(is_Phi(irn) && chordal_has_class(env, irn)) {
55     ir_node *bl = get_nodes_block(irn);
56     set_irn_link(irn, get_irn_link(bl));
57     set_irn_link(bl, irn);
58   }
59 }
60
61 /**
62  * Build a ring of phis for each block in the link field.
63  * @param env The chordal env.
64  */
65 static INLINE void build_phi_rings(be_chordal_env_t *env)
66 {
67   irg_walk_graph(env->irg, clear_link, collect_phis, env);
68 }
69
70 static void insert_all_perms_walker(ir_node *bl, void *data)
71 {
72   be_chordal_env_t *chordal_env = data;
73   pmap *perm_map = chordal_env->data;
74   ir_graph *irg = chordal_env->irg;
75   const be_node_factory_t *fact = chordal_env->main_env->node_factory;
76
77   /* Dummy targets for the projs */
78   ir_node *dummy = new_rd_Unknown(irg, mode_T);
79
80   assert(is_Block(bl));
81
82   /* If the link flag is NULL, this block has no phis. */
83   if(get_irn_link(bl)) {
84     int i, n;
85
86     /* Look at all predecessors of the phi block */
87     for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
88       ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
89       ir_node *phi, *perm, *insert_after;
90       ir_node **in;
91       int n_projs = 0;
92       pmap_entry *ent;
93       pmap *arg_map = pmap_create();
94
95       assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
96
97       /*
98        * Note that all phis in the list are in the same register class
99        * by construction.
100        */
101       for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
102         ir_node *arg = get_irn_n(phi, i);
103         ir_node *proj = pmap_get(arg_map, arg);
104
105         if(!proj && !is_live_in(bl, arg)) {
106                   proj = new_r_Proj(irg, pred_bl, dummy, get_irn_mode(arg), n_projs++);
107                   pmap_insert(arg_map, arg, proj);
108         }
109
110                 if (proj) {
111                         assert(get_irn_mode(phi) == get_irn_mode(proj));
112                 set_irn_n(phi, i, proj);
113                 }
114       }
115
116       in = malloc(n_projs * sizeof(in[0]));
117       pmap_foreach(arg_map, ent) {
118         int proj_nr = get_Proj_proj(ent->value);
119         in[proj_nr] = ent->key;
120       }
121
122       perm = new_Perm(fact, chordal_env->cls, irg, pred_bl, n_projs, in);
123       insert_after = sched_skip(sched_last(pred_bl), 0, sched_skip_cf_predicator, chordal_env->main_env->arch_env);
124       sched_add_after(insert_after, perm);
125       exchange(dummy, perm);
126
127       /* register allocation is copied form former arguments to the projs (new arguments) */
128       pmap_foreach(arg_map, ent) {
129         DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", get_reg(ent->key)->name, ent->key, ent->value));
130         set_reg(ent->value, get_reg(ent->key));
131       }
132
133       free(in);
134       pmap_destroy(arg_map);
135
136       /* register in perm map */
137       pmap_insert(perm_map, pred_bl, perm);
138     }
139   }
140 }
141
142 static void insert_all_perms(be_chordal_env_t *chordal_env) {
143         DBG((dbg, LEVEL_1, "Placing perms...\n"));
144         irg_block_walk_graph(chordal_env->irg, insert_all_perms_walker, NULL, chordal_env);
145 }
146
147 #define is_pinned(irn) (get_irn_link(irn))
148 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
149 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
150
151
152 /**
153  * Adjusts the register allocation for the phi-operands
154  * by inserting perm nodes, if necessary.
155  * @param phi The phi node to adjust operands for
156  */
157 static void adjust_phi_arguments(be_chordal_env_t *chordal_env, ir_node *phi) {
158         int i, max;
159         ir_node *arg, *phi_block, *arg_block;
160         const arch_register_t *phi_reg, *arg_reg;
161         const arch_register_class_t *cls;
162
163         assert(is_Phi(phi) && "Can only handle phi-destruction :)");
164
165         phi_block = get_nodes_block(phi);
166         phi_reg = get_reg(phi);
167         cls = arch_get_irn_reg_class(get_chordal_arch(chordal_env), phi, -1);
168
169         /* process all arguments of the phi */
170         for(i=0, max=get_irn_arity(phi); i<max; ++i) {
171                 arg = get_irn_n(phi, i);
172                 arg_block = get_Block_cfgpred_block(phi_block, i);
173                 arg_reg = get_reg(arg);
174                 assert(arg_reg && "Register must be set while placing perms");
175
176                 DBG((dbg, LEVEL_1, "  for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
177
178                 if(nodes_interfere(chordal_env, phi, arg)) {
179                         /* Insert a duplicate in arguments block,
180                          * make it the new phi arg,
181                          * set its register,
182                          * insert it into schedule,
183                          * pin it
184                          */
185                         ir_node *dupl = new_Copy(chordal_env->main_env->node_factory, cls, chordal_env->irg, arg_block, arg);
186                         assert(get_irn_mode(phi) == get_irn_mode(dupl));
187                         set_irn_n(phi, i, dupl);
188                         set_reg(dupl, phi_reg);
189                         sched_add_after(sched_skip(sched_last(arg_block), 0, sched_skip_cf_predicator, chordal_env->main_env->arch_env), dupl);
190                         pin_irn(dupl, phi_block);
191                         DBG((dbg, LEVEL_1, "    they do interfere: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
192                 } else {
193                         /*
194                          * First check if there is a phi
195                          * - in the same block
196                          * - having arg at the current pos in its arg-list
197                          * - having the same color as arg
198                          *
199                          * If found, then pin the arg
200                          */
201                         DBG((dbg, LEVEL_1, "    they do not interfere\n"));
202                         assert(is_Proj(arg));
203                         if (!is_pinned(arg)) {
204                                 ir_node *other_phi;
205                                 DBG((dbg, LEVEL_1, "      searching for phi with same arg having args register\n"));
206                                 for(other_phi = get_irn_link(phi_block); other_phi; other_phi = get_irn_link(other_phi)) {
207                                         assert(is_Phi(other_phi) && get_nodes_block(phi) == get_nodes_block(other_phi) && "link fields are screwed up");
208                                         if (get_irn_n(other_phi, i) == arg && get_reg(other_phi) == arg_reg) {
209                                                 DBG((dbg, LEVEL_1, "        found %+F(%s)\n", other_phi, get_reg(other_phi)->name));
210                                                 pin_irn(arg, phi_block);
211                                         }
212                                 }
213                         }
214
215                         if (is_pinned(arg)) {
216                                 /* Insert a duplicate of the original value in arguments block,
217                                  * make it the new phi arg,
218                                  * set its register,
219                                  * insert it into schedule,
220                                  * pin it
221                                  */
222                                 ir_node *perm = get_Proj_pred(arg);
223                                 ir_node *orig_val = get_irn_n(perm, get_Proj_proj(arg));
224                                 ir_node *dupl = new_Copy(chordal_env->main_env->node_factory, cls, chordal_env->irg, arg_block, orig_val);
225                                 assert(get_irn_mode(phi) == get_irn_mode(dupl));
226                                 set_irn_n(phi, i, dupl);
227                                 set_reg(dupl, phi_reg);
228                                 sched_add_before(perm, dupl);
229                                 pin_irn(dupl, phi_block);
230                                 DBG((dbg, LEVEL_1, "      arg is pinned: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
231                         } else {
232                                 /* No other phi has the same color (else arg would be pinned),
233                                  * so just set the register and pin
234                                  */
235                                 set_reg(arg, phi_reg);
236                                 pin_irn(arg, phi_block);
237                                 DBG((dbg, LEVEL_1, "      arg is not pinned: so pin %+F(%s)\n", arg, get_reg(arg)->name));
238                         }
239                 }
240         }
241 }
242
243 static void     set_regs_or_place_dupls_walker(ir_node *bl, void *data) {
244         be_chordal_env_t *chordal_env = data;
245         ir_node *phi;
246
247         for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi))
248                 adjust_phi_arguments(chordal_env, phi);
249 }
250
251 static void     set_regs_or_place_dupls(be_chordal_env_t *chordal_env)
252 {
253         DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
254         irg_block_walk_graph(chordal_env->irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
255 }
256
257
258 void be_ssa_destruction(be_chordal_env_t *chordal_env) {
259         pmap *perm_map = pmap_create();
260         ir_graph *irg = chordal_env->irg;
261
262         dbg = firm_dbg_register("ir.be.ssadestr");
263         firm_dbg_set_mask(dbg, DEBUG_LVL);
264
265         /* create a map for fast lookup of perms: block --> perm */
266         chordal_env->data = perm_map;
267
268         build_phi_rings(chordal_env);
269         insert_all_perms(chordal_env);
270 #ifdef DUMP_GRAPHS
271         dump_ir_block_graph_sched(irg, "-ssa_destr_perms_placed");
272 #endif
273         set_regs_or_place_dupls(chordal_env);
274 #ifdef DUMP_GRAPHS
275         dump_ir_block_graph_sched(irg, "-ssa_destr_regs_set");
276 #endif
277
278         pmap_destroy(perm_map);
279 }
280
281 static void ssa_destruction_check_walker(ir_node *bl, void *data)
282 {
283         be_chordal_env_t *chordal_env = data;
284         ir_node *phi;
285         int i, max;
286
287         for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
288                 const arch_register_t *phi_reg, *arg_reg;
289
290                 phi_reg = get_reg(phi);
291                 /* iterate over all args of phi */
292                 for(i=0, max=get_irn_arity(phi); i<max; ++i) {
293                         ir_node *arg = get_irn_n(phi, i);
294                         arg_reg = get_reg(arg);
295                         if(phi_reg != arg_reg) {
296                                 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
297                                 assert(0);
298                         }
299                         if(!is_pinned(arg)) {
300                                 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
301                                 assert(0);
302                         }
303                 }
304         }
305 }
306
307 void be_ssa_destruction_check(be_chordal_env_t *chordal_env) {
308         irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, chordal_env);
309 }