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