2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Performs SSA-Destruction.
23 * @author Daniel Grund
36 #include "iredges_t.h"
44 #include "bechordal_t.h"
48 #include "besched_t.h"
49 #include "benodesets.h"
50 #include "bestatevent.h"
52 #include "beintlive_t.h"
54 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
56 #define get_chordal_arch(ce) ((ce)->birg->main_env->arch_env)
57 #define get_reg(irn) arch_get_irn_register(get_chordal_arch(chordal_env), irn)
58 #define set_reg(irn, reg) arch_set_irn_register(get_chordal_arch(chordal_env), irn, reg)
60 #define is_Perm(irn) (arch_irn_class_is(arch_env, irn, perm))
61 #define get_reg_cls(irn) (arch_get_irn_reg_class(arch_env, irn, -1))
62 #define is_curr_reg_class(irn) (get_reg_cls(p) == chordal_env->cls)
64 static void clear_link(ir_node *irn, void *data) {
66 set_irn_link(irn, NULL);
70 * For each block build a linked list of phis that
72 * - have the current register class
73 * The list is rooted at get_irn_link(BB).
75 static void collect_phis_walker(ir_node *irn, void *data) {
76 be_chordal_env_t *env = data;
77 if (is_Phi(irn) && chordal_has_class(env, irn)) {
78 ir_node *bl = get_nodes_block(irn);
79 set_irn_link(irn, get_irn_link(bl));
80 set_irn_link(bl, irn);
85 * This struct represents a Proj for a Perm.
86 * It records the argument in the Perm and the corresponding Proj of the
90 ir_node *arg; /**< The phi argument to make the Proj for. */
91 int pos; /**< The proj number the Proj will get.
92 This also denotes the position of @p arg
93 in the in array of the Perm. */
94 ir_node *proj; /**< The proj created for @p arg. */
97 static int cmp_perm_proj(const void *a, const void *b, size_t n) {
98 const perm_proj_t *p = a;
99 const perm_proj_t *q = b;
102 return !(p->arg == q->arg);
105 typedef struct insert_all_perms_env_t {
106 be_chordal_env_t *chordal_env;
108 } insert_all_perms_env_t;
111 * Insert Perms in all predecessors of a block containing a phi
113 static void insert_all_perms_walker(ir_node *bl, void *data) {
114 insert_all_perms_env_t *env = data;
115 be_chordal_env_t *chordal_env = env->chordal_env;
116 pmap *perm_map = env->perm_map;
117 ir_graph *irg = chordal_env->irg;
118 be_lv_t *lv = chordal_env->birg->lv;
121 assert(is_Block(bl));
123 /* If the link flag is NULL, this block has no phis. */
124 if(!get_irn_link(bl))
127 /* Look at all predecessors of the phi block */
128 for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
129 ir_node *phi, *perm, *insert_after, **in;
131 set *arg_set = new_set(cmp_perm_proj, chordal_env->cls->n_regs);
132 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
135 assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
138 * Note that all phis in the list are in the same
139 * register class by construction.
141 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
143 ir_node *arg = get_irn_n(phi, i);
144 unsigned hash = nodeset_hash(arg);
147 pp = set_find(arg_set, &templ, sizeof(templ), hash);
150 * If a proj_perm_t entry has not been made in the argument set,
151 * create one. The only restriction is, that the phi argument
152 * may not be live in at the current block, since this argument
153 * interferes with the phi and must thus not be member of a
154 * Perm. A copy will be inserted for this argument alter on.
156 if(!pp && !be_is_live_in(lv, bl, arg)) {
157 templ.pos = n_projs++;
158 set_insert(arg_set, &templ, sizeof(templ), hash);
165 * Create a new Perm with the arguments just collected
166 * above in the arg_set and insert it into the schedule.
168 in = xmalloc(n_projs * sizeof(in[0]));
169 for(pp = set_first(arg_set); pp; pp = set_next(arg_set))
170 in[pp->pos] = pp->arg;
172 perm = be_new_Perm(chordal_env->cls, irg, pred_bl, n_projs, in);
173 be_stat_ev("phi_perm", n_projs);
175 insert_after = sched_skip(sched_last(pred_bl), 0, sched_skip_cf_predicator, chordal_env->birg->main_env->arch_env);
176 sched_add_after(insert_after, perm);
179 * Make the Projs for the Perm and insert into schedule.
180 * Register allocation is copied from the former phi
181 * arguments to the projs (new phi arguments).
184 for(pp = set_first(arg_set); pp; pp = set_next(arg_set)) {
185 ir_node *proj = new_r_Proj(irg, pred_bl, perm, get_irn_mode(pp->arg), pp->pos);
187 assert(get_reg(pp->arg));
188 set_reg(proj, get_reg(pp->arg));
189 sched_add_after(insert_after, proj);
191 DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", get_reg(pp->arg)->name, pp->arg, pp->proj));
195 * Set the phi nodes to their new arguments: The Projs of the Perm
197 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
200 templ.arg = get_irn_n(phi, i);
201 pp = set_find(arg_set, &templ, sizeof(templ), nodeset_hash(templ.arg));
203 /* If not found, it was an interfering argument */
205 set_irn_n(phi, i, pp->proj);
206 be_liveness_introduce(lv, pp->proj);
210 /* update the liveness of the Perm's operands. It might be changed. */
213 for (i = 0; i < n_projs; ++i)
214 be_liveness_update(lv, in[i]);
218 /* register in perm map */
219 pmap_insert(perm_map, pred_bl, perm);
226 #define is_pinned(irn) (get_irn_link(irn))
227 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
228 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
231 * Adjusts the register allocation for the (new) phi-operands
232 * and insert duplicates iff necessary.
234 static void set_regs_or_place_dupls_walker(ir_node *bl, void *data) {
235 be_chordal_env_t *chordal_env = data;
236 be_lv_t *lv = chordal_env->birg->lv;
239 /* Consider all phis of this block */
240 for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
242 ir_node *arg, *phi_block, *arg_block;
243 const arch_register_t *phi_reg, *arg_reg;
244 const arch_register_class_t *cls;
246 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
248 phi_block = get_nodes_block(phi);
249 phi_reg = get_reg(phi);
250 cls = arch_get_irn_reg_class(get_chordal_arch(chordal_env), phi, -1);
252 /* process all arguments of the phi */
253 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
254 arg = get_irn_n(phi, i);
255 arg_block = get_Block_cfgpred_block(phi_block, i);
256 arg_reg = get_reg(arg);
258 assert(arg_reg && "Register must be set while placing perms");
260 DBG((dbg, LEVEL_1, " for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
262 if (values_interfere(chordal_env->birg, phi, arg)) {
264 Insert a duplicate in arguments block,
265 make it the new phi arg,
267 insert it into schedule,
270 ir_node *dupl = be_new_Copy(cls, chordal_env->irg, arg_block, arg);
272 /* this is commented out because it will fail in case of unknown float */
274 ir_mode *m_phi = get_irn_mode(phi), *m_dupl = get_irn_mode(dupl);
277 Conv signed <-> unsigned is killed on ia32
278 check for: (both int OR both float) AND equal mode sizes
280 assert(((mode_is_int(m_phi) && mode_is_int(m_dupl)) ||
281 (mode_is_float(m_phi) && mode_is_float(m_dupl))) &&
282 (get_mode_size_bits(m_phi) == get_mode_size_bits(m_dupl)));
285 set_irn_n(phi, i, dupl);
286 set_reg(dupl, phi_reg);
287 sched_add_after(sched_skip(sched_last(arg_block), 0, sched_skip_cf_predicator, chordal_env->birg->main_env->arch_env), dupl);
288 pin_irn(dupl, phi_block);
289 be_liveness_introduce(lv, dupl);
290 be_liveness_update(lv, arg);
291 DBG((dbg, LEVEL_1, " they do interfere: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
292 continue; /* with next argument */
295 if (phi_reg == arg_reg) {
296 /* Phi and arg have the same register, so pin and continue */
297 pin_irn(arg, phi_block);
298 DBG((dbg, LEVEL_1, " arg has same reg: pin %+F(%s)\n", arg, get_reg(arg)->name));
302 DBG((dbg, LEVEL_1, " they do not interfere\n"));
303 assert(is_Proj(arg));
305 First check if there is an other phi
307 - having arg at the current pos in its arg-list
308 - having the same color as arg
310 If found, then pin the arg (for that phi)
312 if (! is_pinned(arg)) {
315 DBG((dbg, LEVEL_1, " searching for phi with same arg having args register\n"));
317 for(other_phi = get_irn_link(phi_block); other_phi; other_phi = get_irn_link(other_phi)) {
319 assert(is_Phi(other_phi) &&
320 get_nodes_block(phi) == get_nodes_block(other_phi) &&
321 "link fields are screwed up");
323 if (get_irn_n(other_phi, i) == arg && get_reg(other_phi) == arg_reg) {
324 DBG((dbg, LEVEL_1, " found %+F(%s)\n", other_phi, get_reg(other_phi)->name));
325 pin_irn(arg, phi_block);
331 if (is_pinned(arg)) {
333 Insert a duplicate of the original value in arguments block,
334 make it the new phi arg,
336 insert it into schedule,
339 ir_node *perm = get_Proj_pred(arg);
340 ir_node *dupl = be_new_Copy(cls, chordal_env->irg, arg_block, arg);
343 /* this is commented out because it will fail in case of unknown float */
345 ir_mode *m_phi = get_irn_mode(phi);
346 ir_mode *m_dupl = get_irn_mode(dupl);
349 Conv signed <-> unsigned is killed on ia32
350 check for: (both int OR both float) AND equal mode sizes
352 assert(((mode_is_int(m_phi) && mode_is_int(m_dupl)) ||
353 (mode_is_float(m_phi) && mode_is_float(m_dupl))) &&
354 (get_mode_size_bits(m_phi) == get_mode_size_bits(m_dupl)));
357 set_irn_n(phi, i, dupl);
358 set_reg(dupl, phi_reg);
359 /* skip the Perm's Projs and insert the copies behind. */
360 for(ins = sched_next(perm); is_Proj(ins); ins = sched_next(ins));
361 sched_add_before(ins, dupl);
362 pin_irn(dupl, phi_block);
363 be_liveness_introduce(lv, dupl);
364 be_liveness_update(lv, arg);
365 DBG((dbg, LEVEL_1, " arg is pinned: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
368 No other phi has the same color (else arg would have been pinned),
369 so just set the register and pin
371 set_reg(arg, phi_reg);
372 pin_irn(arg, phi_block);
373 DBG((dbg, LEVEL_1, " arg is not pinned: so pin %+F(%s)\n", arg, get_reg(arg)->name));
379 void be_ssa_destruction(be_chordal_env_t *chordal_env) {
380 insert_all_perms_env_t insert_perms_env;
381 pmap *perm_map = pmap_create();
382 ir_graph *irg = chordal_env->irg;
383 be_lv_t *lv = be_assure_liveness(chordal_env->birg);
385 FIRM_DBG_REGISTER(dbg, "ir.be.ssadestr");
387 be_liveness_assure_sets(lv);
389 /* create a map for fast lookup of perms: block --> perm */
390 irg_walk_graph(irg, clear_link, collect_phis_walker, chordal_env);
392 DBG((dbg, LEVEL_1, "Placing perms...\n"));
393 insert_perms_env.chordal_env = chordal_env;
394 insert_perms_env.perm_map = perm_map;
395 irg_block_walk_graph(irg, insert_all_perms_walker, NULL, &insert_perms_env);
397 // Matze: really needed here?
398 // Sebastian: Yes. the walker function uses interference.
399 be_liveness_invalidate(lv);
401 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
402 be_dump(irg, "-ssa_destr_perms_placed", dump_ir_block_graph_sched);
404 be_liveness_assure_chk(lv);
406 DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
407 irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
409 /* TODO: unfortunaltely updating doesn't work yet. */
410 be_liveness_invalidate(lv);
412 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
413 be_dump(irg, "-ssa_destr_regs_set", dump_ir_block_graph_sched);
415 pmap_destroy(perm_map);
418 static void ssa_destruction_check_walker(ir_node *bl, void *data) {
419 be_chordal_env_t *chordal_env = data;
423 for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
424 const arch_register_t *phi_reg, *arg_reg;
426 phi_reg = get_reg(phi);
427 /* iterate over all args of phi */
428 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
429 ir_node *arg = get_irn_n(phi, i);
431 arg_reg = get_reg(arg);
433 if (phi_reg != arg_reg) {
434 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
438 if (! is_pinned(arg)) {
439 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
446 void be_ssa_destruction_check(be_chordal_env_t *chordal_env) {
447 irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, chordal_env);