4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
7 * Performs SSA-Destruction.
18 #include "iredges_t.h"
26 #include "bechordal_t.h"
30 #include "besched_t.h"
31 #include "benodesets.h"
32 #include "bestatevent.h"
34 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
36 #define get_chordal_arch(ce) ((ce)->birg->main_env->arch_env)
37 #define get_reg(irn) arch_get_irn_register(get_chordal_arch(chordal_env), irn)
38 #define set_reg(irn, reg) arch_set_irn_register(get_chordal_arch(chordal_env), irn, reg)
40 #define is_Perm(irn) (arch_irn_class_is(arch_env, irn, perm))
41 #define get_reg_cls(irn) (arch_get_irn_reg_class(arch_env, irn, -1))
42 #define is_curr_reg_class(irn) (get_reg_cls(p) == chordal_env->cls)
44 static void clear_link(ir_node *irn, void *data) {
45 set_irn_link(irn, NULL);
49 * For each block build a linked list of phis that
51 * - have the current register class
52 * The list is rooted at get_irn_link(BB).
54 static void collect_phis_walker(ir_node *irn, void *data) {
55 be_chordal_env_t *env = data;
56 if(is_Phi(irn) && chordal_has_class(env, irn)) {
57 ir_node *bl = get_nodes_block(irn);
58 set_irn_link(irn, get_irn_link(bl));
59 set_irn_link(bl, irn);
64 * This struct represents a Proj for a Perm.
65 * It records the argument in the Perm and the corresponding Proj of the
69 ir_node *arg; /**< The phi argument to make the Proj for. */
70 int pos; /**< The proj number the Proj will get.
71 This also denotes the position of @p arg
72 in the in array of the Perm. */
73 ir_node *proj; /**< The proj created for @p arg. */
76 static int cmp_perm_proj(const void *a, const void *b, size_t n) {
77 const perm_proj_t *p = a;
78 const perm_proj_t *q = b;
79 return !(p->arg == q->arg);
83 * Insert Perms in all predecessors of a block containing a phi
85 static void insert_all_perms_walker(ir_node *bl, void *data) {
86 be_chordal_env_t *chordal_env = data;
87 pmap *perm_map = chordal_env->data;
88 ir_graph *irg = chordal_env->irg;
89 be_lv_t *lv = chordal_env->birg->lv;
94 /* If the link flag is NULL, this block has no phis. */
98 /* Look at all predecessors of the phi block */
99 for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
100 ir_node *phi, *perm, *insert_after, **in;
102 set *arg_set = new_set(cmp_perm_proj, chordal_env->cls->n_regs);
103 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
106 assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
109 * Note that all phis in the list are in the same
110 * register class by construction.
112 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
114 ir_node *arg = get_irn_n(phi, i);
115 unsigned hash = nodeset_hash(arg);
118 pp = set_find(arg_set, &templ, sizeof(templ), hash);
121 * If a proj_perm_t entry has not been made in the argument set,
122 * create one. The only restriction is, that the phi argument
123 * may not be live in at the current block, since this argument
124 * interferes with the phi and must thus not be member of a
125 * Perm. A copy will be inserted for this argument alter on.
127 if(!pp && !be_is_live_in(lv, bl, arg)) {
128 templ.pos = n_projs++;
129 set_insert(arg_set, &templ, sizeof(templ), hash);
136 * Create a new Perm with the arguments just collected
137 * above in the arg_set and insert it into the schedule.
139 in = xmalloc(n_projs * sizeof(in[0]));
140 for(pp = set_first(arg_set); pp; pp = set_next(arg_set))
141 in[pp->pos] = pp->arg;
143 perm = be_new_Perm(chordal_env->cls, irg, pred_bl, n_projs, in);
144 be_stat_ev("phi_perm", n_projs);
147 insert_after = sched_skip(sched_last(pred_bl), 0, sched_skip_cf_predicator, chordal_env->birg->main_env->arch_env);
148 sched_add_after(insert_after, perm);
151 * Make the Projs for the Perm and insert into schedule.
152 * Register allocation is copied from the former phi
153 * arguments to the projs (new phi arguments).
156 for(pp = set_first(arg_set); pp; pp = set_next(arg_set)) {
157 ir_node *proj = new_r_Proj(irg, pred_bl, perm, get_irn_mode(pp->arg), pp->pos);
159 assert(get_reg(pp->arg));
160 set_reg(proj, get_reg(pp->arg));
161 sched_add_after(insert_after, proj);
163 DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", get_reg(pp->arg)->name, pp->arg, pp->proj));
167 * Set the phi nodes to their new arguments: The Projs of the Perm
169 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
172 templ.arg = get_irn_n(phi, i);
173 pp = set_find(arg_set, &templ, sizeof(templ), nodeset_hash(templ.arg));
175 /* If not found, it was an interfering argument */
177 set_irn_n(phi, i, pp->proj);
180 /* register in perm map */
181 pmap_insert(perm_map, pred_bl, perm);
188 #define is_pinned(irn) (get_irn_link(irn))
189 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
190 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
193 * Adjusts the register allocation for the (new) phi-operands
194 * and insert duplicates iff necessary.
196 static void set_regs_or_place_dupls_walker(ir_node *bl, void *data) {
197 be_chordal_env_t *chordal_env = data;
198 be_lv_t *lv = chordal_env->birg->lv;
201 /* Consider all phis of this block */
202 for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
204 ir_node *arg, *phi_block, *arg_block;
205 const arch_register_t *phi_reg, *arg_reg;
206 const arch_register_class_t *cls;
208 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
210 phi_block = get_nodes_block(phi);
211 phi_reg = get_reg(phi);
212 cls = arch_get_irn_reg_class(get_chordal_arch(chordal_env), phi, -1);
214 /* process all arguments of the phi */
215 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
216 arg = get_irn_n(phi, i);
217 arg_block = get_Block_cfgpred_block(phi_block, i);
218 arg_reg = get_reg(arg);
220 assert(arg_reg && "Register must be set while placing perms");
222 DBG((dbg, LEVEL_1, " for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
224 if(values_interfere(lv, phi, arg)) {
226 Insert a duplicate in arguments block,
227 make it the new phi arg,
229 insert it into schedule,
232 ir_node *dupl = be_new_Copy(cls, chordal_env->irg, arg_block, arg);
234 /* this is commented out because it will fail in case of unknown float */
236 ir_mode *m_phi = get_irn_mode(phi), *m_dupl = get_irn_mode(dupl);
239 Conv signed <-> unsigned is killed on ia32
240 check for: (both int OR both float) AND equal mode sizes
242 assert(((mode_is_int(m_phi) && mode_is_int(m_dupl)) ||
243 (mode_is_float(m_phi) && mode_is_float(m_dupl))) &&
244 (get_mode_size_bits(m_phi) == get_mode_size_bits(m_dupl)));
247 set_irn_n(phi, i, dupl);
248 set_reg(dupl, phi_reg);
249 sched_add_after(sched_skip(sched_last(arg_block), 0, sched_skip_cf_predicator, chordal_env->birg->main_env->arch_env), dupl);
250 pin_irn(dupl, phi_block);
251 DBG((dbg, LEVEL_1, " they do interfere: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
252 continue; /* with next argument */
255 if (phi_reg == arg_reg) {
256 /* Phi and arg have the same register, so pin and continue */
257 pin_irn(arg, phi_block);
258 DBG((dbg, LEVEL_1, " arg has same reg: pin %+F(%s)\n", arg, get_reg(arg)->name));
262 DBG((dbg, LEVEL_1, " they do not interfere\n"));
263 assert(is_Proj(arg));
265 First check if there is an other phi
267 - having arg at the current pos in its arg-list
268 - having the same color as arg
270 If found, then pin the arg (for that phi)
272 if (! is_pinned(arg)) {
275 DBG((dbg, LEVEL_1, " searching for phi with same arg having args register\n"));
277 for(other_phi = get_irn_link(phi_block); other_phi; other_phi = get_irn_link(other_phi)) {
279 assert(is_Phi(other_phi) &&
280 get_nodes_block(phi) == get_nodes_block(other_phi) &&
281 "link fields are screwed up");
283 if (get_irn_n(other_phi, i) == arg && get_reg(other_phi) == arg_reg) {
284 DBG((dbg, LEVEL_1, " found %+F(%s)\n", other_phi, get_reg(other_phi)->name));
285 pin_irn(arg, phi_block);
291 if (is_pinned(arg)) {
293 Insert a duplicate of the original value in arguments block,
294 make it the new phi arg,
296 insert it into schedule,
299 ir_node *perm = get_Proj_pred(arg);
300 ir_node *dupl = be_new_Copy(cls, chordal_env->irg, arg_block, arg);
303 /* this is commented out because it will fail in case of unknown float */
305 ir_mode *m_phi = get_irn_mode(phi);
306 ir_mode *m_dupl = get_irn_mode(dupl);
309 Conv signed <-> unsigned is killed on ia32
310 check for: (both int OR both float) AND equal mode sizes
312 assert(((mode_is_int(m_phi) && mode_is_int(m_dupl)) ||
313 (mode_is_float(m_phi) && mode_is_float(m_dupl))) &&
314 (get_mode_size_bits(m_phi) == get_mode_size_bits(m_dupl)));
317 set_irn_n(phi, i, dupl);
318 set_reg(dupl, phi_reg);
319 /* skip the Perm's Projs and insert the copies behind. */
320 for(ins = sched_next(perm); is_Proj(ins); ins = sched_next(ins));
321 sched_add_before(ins, dupl);
322 pin_irn(dupl, phi_block);
323 DBG((dbg, LEVEL_1, " arg is pinned: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
326 No other phi has the same color (else arg would have been pinned),
327 so just set the register and pin
329 set_reg(arg, phi_reg);
330 pin_irn(arg, phi_block);
331 DBG((dbg, LEVEL_1, " arg is not pinned: so pin %+F(%s)\n", arg, get_reg(arg)->name));
337 void be_ssa_destruction(be_chordal_env_t *chordal_env) {
338 pmap *perm_map = pmap_create();
339 ir_graph *irg = chordal_env->irg;
341 FIRM_DBG_REGISTER(dbg, "ir.be.ssadestr");
343 be_assure_liveness(chordal_env->birg);
345 /* create a map for fast lookup of perms: block --> perm */
346 chordal_env->data = perm_map;
347 irg_walk_graph(irg, clear_link, collect_phis_walker, chordal_env);
349 DBG((dbg, LEVEL_1, "Placing perms...\n"));
350 irg_block_walk_graph(irg, insert_all_perms_walker, NULL, chordal_env);
352 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
353 be_dump(irg, "-ssa_destr_perms_placed", dump_ir_block_graph_sched);
355 // Matze: really needed here?
356 be_invalidate_liveness(chordal_env->birg);
357 be_assure_liveness(chordal_env->birg);
359 DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
360 irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
362 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
363 be_dump(irg, "-ssa_destr_regs_set", dump_ir_block_graph_sched);
365 pmap_destroy(perm_map);
368 static void ssa_destruction_check_walker(ir_node *bl, void *data) {
369 be_chordal_env_t *chordal_env = data;
373 for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
374 const arch_register_t *phi_reg, *arg_reg;
376 phi_reg = get_reg(phi);
377 /* iterate over all args of phi */
378 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
379 ir_node *arg = get_irn_n(phi, i);
381 arg_reg = get_reg(arg);
383 if (phi_reg != arg_reg) {
384 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
388 if (! is_pinned(arg)) {
389 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
396 void be_ssa_destruction_check(be_chordal_env_t *chordal_env) {
397 irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, chordal_env);