refactored changes of yesterday
[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 "irgwalk.h"
20 #include "irgmod.h"
21 #include "irdump.h"
22 #include "irprintf.h"
23
24 #include "be_t.h"
25 #include "beutil.h"
26 #include "bechordal_t.h"
27 #include "bearch.h"
28 #include "belive_t.h"
29 #include "benode_t.h"
30 #include "besched_t.h"
31
32 static firm_dbg_module_t *dbg = NULL;
33 #define 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         set_irn_link(irn, NULL);
45 }
46
47 /**
48  * For each block build a linked list of phis that
49  *  - are in that block
50  *  - have the current register class
51  * The list is rooted at get_irn_link(BB).
52  */
53 static void collect_phis_walker(ir_node *irn, void *data) {
54         be_chordal_env_t *env = data;
55         if(is_Phi(irn) && chordal_has_class(env, irn)) {
56                 ir_node *bl = get_nodes_block(irn);
57                 set_irn_link(irn, get_irn_link(bl));
58                 set_irn_link(bl, irn);
59         }
60 }
61
62 /**
63  * This struct represents a Proj for a Perm.
64  * It records the argument in the Perm and the corresponding Proj of the
65  * Perm.
66  */
67 typedef struct {
68         ir_node *arg;  /**< The phi argument to make the Proj for. */
69         int pos;       /**< The proj number the Proj will get.
70                                                                          This also denotes the position of @p arg
71                                                                          in the in array of the Perm. */
72         ir_node *proj; /**< The proj created for @p arg. */
73 } perm_proj_t;
74
75 static int cmp_perm_proj(const void *a, const void *b, size_t n) {
76         const perm_proj_t *p = a;
77         const perm_proj_t *q = b;
78         return !(p->arg == q->arg);
79 }
80
81 /**
82  * Insert Perms in all predecessors of a block containing a phi
83  */
84 static void insert_all_perms_walker(ir_node *bl, void *data) {
85         be_chordal_env_t *chordal_env = data;
86         pmap *perm_map = chordal_env->data;
87         ir_graph *irg = chordal_env->irg;
88         const be_node_factory_t *fact = chordal_env->main_env->node_factory;
89         int i, n;
90
91         assert(is_Block(bl));
92
93         /* If the link flag is NULL, this block has no phis. */
94         if(!get_irn_link(bl))
95                 return;
96
97         /* Look at all predecessors of the phi block */
98         for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
99                 ir_node *phi, *perm, *insert_after, **in;
100                 perm_proj_t *pp;
101                 set *arg_set     = new_set(cmp_perm_proj, chordal_env->cls->n_regs);
102                 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
103                 int n_projs      = 0;
104
105                 assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
106
107                 /*
108                  * Note that all phis in the list are in the same
109                  * register class by construction.
110                  */
111                 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
112                         perm_proj_t templ;
113                         ir_node *arg     = get_irn_n(phi, i);
114                         unsigned hash    = HASH_PTR(arg);
115
116                         templ.arg  = arg;
117                         pp         = set_find(arg_set, &templ, sizeof(templ), hash);
118
119                         /*
120                          * If a proj_perm_t entry has not been made in the argument set,
121                          * create one. The only restriction is, that the phi argument
122                          * may not be live in at the current block, since this argument
123                          * interferes with the phi and must thus not be member of a
124                          * Perm. A copy will be inserted for this argument alter on.
125                          */
126                         if(!pp && !is_live_in(bl, arg)) {
127                                 templ.pos = n_projs++;
128                                 set_insert(arg_set, &templ, sizeof(templ), hash);
129                         }
130                 }
131
132
133                 if (n_projs) {
134                         /*
135                          * Create a new Perm with the arguments just collected
136                          * above in the arg_set and insert it into the schedule.
137                          */
138                         in = malloc(n_projs * sizeof(in[0]));
139                         for(pp = set_first(arg_set); pp; pp = set_next(arg_set))
140                                 in[pp->pos] = pp->arg;
141
142                         perm = new_Perm(fact, chordal_env->cls, irg, pred_bl, n_projs, in);
143                         free(in);
144                         insert_after = sched_skip(sched_last(pred_bl), 0, sched_skip_cf_predicator, chordal_env->main_env->arch_env);
145                         sched_add_after(insert_after, perm);
146
147                         /*
148                          * Make the Projs for the Perm and insert into schedule.
149                          * Register allocation is copied from the former phi
150                          * arguments to the projs (new phi arguments).
151                          */
152                         insert_after = perm;
153                         for(pp = set_first(arg_set); pp; pp = set_next(arg_set)) {
154                                 ir_node *proj = new_r_Proj(irg, pred_bl, perm, get_irn_mode(pp->arg), pp->pos);
155                                 pp->proj = proj;
156                                 assert(get_reg(pp->arg));
157                                 set_reg(proj, get_reg(pp->arg));
158                                 sched_add_after(insert_after, proj);
159                                 insert_after = proj;
160                                 DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", get_reg(pp->arg)->name, pp->arg, pp->proj));
161                         }
162
163                         /*
164                          * Set the phi nodes to their new arguments: The Projs of the Perm
165                          */
166                         for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
167                                 perm_proj_t templ;
168
169                                 templ.arg = get_irn_n(phi, i);
170                                 pp        = set_find(arg_set, &templ, sizeof(templ), HASH_PTR(templ.arg));
171
172                                 /* If not found, it was an interfering argument */
173                                 if (pp)
174                                         set_irn_n(phi, i, pp->proj);
175                         }
176
177                         /* register in perm map */
178                         pmap_insert(perm_map, pred_bl, perm);
179                 }
180
181                 del_set(arg_set);
182         }
183 }
184
185 #define is_pinned(irn) (get_irn_link(irn))
186 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
187 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
188
189 /**
190  * Adjusts the register allocation for the (new) phi-operands
191  * and insert duplicates iff necessary.
192  */
193 static void     set_regs_or_place_dupls_walker(ir_node *bl, void *data) {
194         be_chordal_env_t *chordal_env = data;
195         ir_node *phi;
196
197         /* Consider all phis of this block */
198         for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
199                 int i, max;
200                 ir_node *arg, *phi_block, *arg_block;
201                 const arch_register_t *phi_reg, *arg_reg;
202                 const arch_register_class_t *cls;
203
204                 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
205
206                 phi_block = get_nodes_block(phi);
207                 phi_reg = get_reg(phi);
208                 cls = arch_get_irn_reg_class(get_chordal_arch(chordal_env), phi, -1);
209
210                 /* process all arguments of the phi */
211                 for(i=0, max=get_irn_arity(phi); i<max; ++i) {
212                         arg = get_irn_n(phi, i);
213                         arg_block = get_Block_cfgpred_block(phi_block, i);
214                         arg_reg = get_reg(arg);
215                         assert(arg_reg && "Register must be set while placing perms");
216
217                         DBG((dbg, LEVEL_1, "  for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
218
219                         if(nodes_interfere(chordal_env, phi, arg)) {
220                                 /* Insert a duplicate in arguments block,
221                                  * make it the new phi arg,
222                                  * set its register,
223                                  * insert it into schedule,
224                                  * pin it
225                                  */
226                                 ir_node *dupl = new_Copy(chordal_env->main_env->node_factory, cls, chordal_env->irg, arg_block, arg);
227                                 assert(get_irn_mode(phi) == get_irn_mode(dupl));
228                                 set_irn_n(phi, i, dupl);
229                                 set_reg(dupl, phi_reg);
230                                 sched_add_after(sched_skip(sched_last(arg_block), 0, sched_skip_cf_predicator, chordal_env->main_env->arch_env), dupl);
231                                 pin_irn(dupl, phi_block);
232                                 DBG((dbg, LEVEL_1, "    they do interfere: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
233                                 continue; /* with next argument */
234                         }
235
236                         if (phi_reg == arg_reg) {
237                                 /* Phi and arg have the same register,
238                                  * so pin and continue
239                                  */
240                                 pin_irn(arg, phi_block);
241                                 DBG((dbg, LEVEL_1, "      arg has same reg: pin %+F(%s)\n", arg, get_reg(arg)->name));
242                                 continue;
243                         }
244
245                         DBG((dbg, LEVEL_1, "    they do not interfere\n"));
246                         assert(is_Proj(arg));
247                         /*
248                          * First check if there is an other phi
249                          * - in the same block
250                          * - having arg at the current pos in its arg-list
251                          * - having the same color as arg
252                          *
253                          * If found, then pin the arg (for that phi)
254                          */
255                         if (!is_pinned(arg)) {
256                                 ir_node *other_phi;
257                                 DBG((dbg, LEVEL_1, "      searching for phi with same arg having args register\n"));
258                                 for(other_phi = get_irn_link(phi_block); other_phi; other_phi = get_irn_link(other_phi)) {
259                                         assert(is_Phi(other_phi) && get_nodes_block(phi) == get_nodes_block(other_phi) && "link fields are screwed up");
260                                         if (get_irn_n(other_phi, i) == arg && get_reg(other_phi) == arg_reg) {
261                                                 DBG((dbg, LEVEL_1, "        found %+F(%s)\n", other_phi, get_reg(other_phi)->name));
262                                                 pin_irn(arg, phi_block);
263                                                 break;
264                                         }
265                                 }
266                         }
267
268                         if (is_pinned(arg)) {
269                                 /* Insert a duplicate of the original value in arguments block,
270                                  * make it the new phi arg,
271                                  * set its register,
272                                  * insert it into schedule,
273                                  * pin it
274                                  */
275                                 ir_node *perm = get_Proj_pred(arg);
276                                 ir_node *orig_val = get_irn_n(perm, get_Proj_proj(arg));
277                                 ir_node *dupl = new_Copy(chordal_env->main_env->node_factory, cls, chordal_env->irg, arg_block, orig_val);
278                                 assert(get_irn_mode(phi) == get_irn_mode(dupl));
279                                 set_irn_n(phi, i, dupl);
280                                 set_reg(dupl, phi_reg);
281                                 sched_add_before(perm, dupl);
282                                 pin_irn(dupl, phi_block);
283                                 DBG((dbg, LEVEL_1, "      arg is pinned: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
284                         } else {
285                                 /* No other phi has the same color (else arg would have been pinned),
286                                  * so just set the register and pin
287                                  */
288                                 set_reg(arg, phi_reg);
289                                 pin_irn(arg, phi_block);
290                                 DBG((dbg, LEVEL_1, "      arg is not pinned: so pin %+F(%s)\n", arg, get_reg(arg)->name));
291                         }
292                 }
293         }
294 }
295
296 void be_ssa_destruction(be_chordal_env_t *chordal_env) {
297         pmap *perm_map = pmap_create();
298         ir_graph *irg = chordal_env->irg;
299
300         dbg = firm_dbg_register("ir.be.ssadestr");
301
302         /* create a map for fast lookup of perms: block --> perm */
303         chordal_env->data = perm_map;
304         irg_walk_graph(irg, clear_link, collect_phis_walker, chordal_env);
305
306         DBG((dbg, LEVEL_1, "Placing perms...\n"));
307         irg_block_walk_graph(irg, insert_all_perms_walker, NULL, chordal_env);
308 #ifdef DUMP_GRAPHS
309         dump_ir_block_graph_sched(irg, "-ssa_destr_perms_placed");
310 #endif
311
312         DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
313         irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
314 #ifdef DUMP_GRAPHS
315         dump_ir_block_graph_sched(irg, "-ssa_destr_regs_set");
316 #endif
317
318         pmap_destroy(perm_map);
319 }
320
321 static void ssa_destruction_check_walker(ir_node *bl, void *data) {
322         be_chordal_env_t *chordal_env = data;
323         ir_node *phi;
324         int i, max;
325
326         for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
327                 const arch_register_t *phi_reg, *arg_reg;
328
329                 phi_reg = get_reg(phi);
330                 /* iterate over all args of phi */
331                 for(i=0, max=get_irn_arity(phi); i<max; ++i) {
332                         ir_node *arg = get_irn_n(phi, i);
333                         arg_reg = get_reg(arg);
334                         if(phi_reg != arg_reg) {
335                                 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
336                                 assert(0);
337                         }
338                         if(!is_pinned(arg)) {
339                                 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
340                                 assert(0);
341                         }
342                 }
343         }
344 }
345
346 void be_ssa_destruction_check(be_chordal_env_t *chordal_env) {
347         irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, chordal_env);
348 }