2 * Copyright (C) 1995-2008 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
29 #include "bessadestr.h"
36 #include "iredges_t.h"
44 #include "bechordal_t.h"
49 #include "bestatevent.h"
51 #include "beintlive_t.h"
53 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
55 static void clear_link(ir_node *irn, void *data)
58 set_irn_link(irn, NULL);
62 * For each block build a linked list of phis that
64 * - have the current register class
65 * The list is rooted at get_irn_link(BB).
67 static void collect_phis_walker(ir_node *irn, void *data)
69 be_chordal_env_t *env = (be_chordal_env_t*)data;
70 if (is_Phi(irn) && chordal_has_class(env, irn)) {
71 ir_node *bl = get_nodes_block(irn);
72 set_irn_link(irn, get_irn_link(bl));
73 set_irn_link(bl, irn);
78 * This struct represents a Proj for a Perm.
79 * It records the argument in the Perm and the corresponding Proj of the
83 ir_node *arg; /**< The phi argument to make the Proj for. */
84 int pos; /**< The proj number the Proj will get.
85 This also denotes the position of @p arg
86 in the in array of the Perm. */
87 ir_node *proj; /**< The proj created for @p arg. */
90 static int cmp_perm_proj(const void *a, const void *b, size_t n)
92 const perm_proj_t *p = (const perm_proj_t*)a;
93 const perm_proj_t *q = (const perm_proj_t*)b;
96 return !(p->arg == q->arg);
99 typedef struct insert_all_perms_env_t {
100 be_chordal_env_t *chordal_env;
102 } insert_all_perms_env_t;
105 * Insert Perms in all predecessors of a block containing a phi
107 static void insert_all_perms_walker(ir_node *bl, void *data)
109 insert_all_perms_env_t *env = (insert_all_perms_env_t*)data;
110 be_chordal_env_t *chordal_env = env->chordal_env;
111 pmap *perm_map = env->perm_map;
112 be_lv_t *lv = be_get_irg_liveness(chordal_env->irg);
115 assert(is_Block(bl));
117 /* If the link flag is NULL, this block has no phis. */
118 if (!get_irn_link(bl))
121 /* Look at all predecessors of the phi block */
122 for (i = 0, n = get_irn_arity(bl); i < n; ++i) {
123 ir_node *phi, *perm, *insert_after, **in;
125 set *arg_set = new_set(cmp_perm_proj, chordal_env->cls->n_regs);
126 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
129 assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
132 * Note that all phis in the list are in the same
133 * register class by construction.
135 for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
136 phi = (ir_node*)get_irn_link(phi)) {
137 ir_node *arg = get_irn_n(phi, i);
141 hash = hash_irn(arg);
143 pp = (perm_proj_t*)set_find(arg_set, &templ, sizeof(templ), hash);
146 * If a proj_perm_t entry has not been made in the argument set,
147 * create one. The only restriction is, that the phi argument
148 * may not be live in at the current block, since this argument
149 * interferes with the phi and must thus not be member of a
150 * Perm. A copy will be inserted for this argument later on.
152 if (!pp && !be_is_live_in(lv, bl, arg)) {
153 templ.pos = n_projs++;
154 set_insert(arg_set, &templ, sizeof(templ), hash);
161 * Create a new Perm with the arguments just collected
162 * above in the arg_set and insert it into the schedule.
164 in = XMALLOCN(ir_node*, n_projs);
165 foreach_set(arg_set, perm_proj_t*, pp) {
166 in[pp->pos] = pp->arg;
169 perm = be_new_Perm(chordal_env->cls, pred_bl, n_projs, in);
170 be_stat_ev("phi_perm", n_projs);
172 insert_after = pred_bl;
174 insert_after = sched_prev(insert_after);
175 } while (is_cfop(insert_after));
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 foreach_set(arg_set, perm_proj_t*, pp) {
185 ir_node *proj = new_r_Proj(perm, get_irn_mode(pp->arg), pp->pos);
187 assert(arch_get_irn_register(pp->arg));
188 arch_set_irn_register(proj, arch_get_irn_register(pp->arg));
190 DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", arch_get_irn_register(pp->arg)->name, pp->arg, pp->proj));
194 * Set the phi nodes to their new arguments: The Projs of the Perm
196 for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
197 phi = (ir_node*)get_irn_link(phi)) {
200 templ.arg = get_irn_n(phi, i);
201 pp = (perm_proj_t*)set_find(arg_set, &templ, sizeof(templ), hash_irn(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)
236 be_chordal_env_t *chordal_env = (be_chordal_env_t*)data;
237 be_lv_t *lv = be_get_irg_liveness(chordal_env->irg);
240 /* Consider all phis of this block */
241 for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
242 phi = (ir_node*)get_irn_link(phi)) {
243 ir_node *phi_block = get_nodes_block(phi);
244 const arch_register_t *phi_reg = arch_get_irn_register(phi);
245 const arch_register_class_t *cls = phi_reg->reg_class;
249 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
251 /* process all arguments of the phi */
252 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
253 ir_node *arg = get_irn_n(phi, i);
254 const arch_register_t *arg_reg;
257 arg_block = get_Block_cfgpred_block(phi_block, i);
258 arg_reg = arch_get_irn_register(arg);
260 assert(arg_reg && "Register must be set while placing perms");
262 DBG((dbg, LEVEL_1, " for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
264 if (phi_reg == arg_reg
265 || (arg_reg->type & arch_register_type_joker)
266 || (arg_reg->type & arch_register_type_virtual)) {
267 /* Phi and arg have the same register, so pin and continue */
268 pin_irn(arg, phi_block);
269 DBG((dbg, LEVEL_1, " arg has same reg: pin %+F(%s)\n", arg, arch_get_irn_register(arg)->name));
273 if (be_values_interfere(lv, phi, arg)) {
277 Insert a duplicate in arguments block,
278 make it the new phi arg,
280 insert it into schedule,
283 ir_node *dupl = be_new_Copy(cls, arg_block, arg);
285 set_irn_n(phi, i, dupl);
286 arch_set_irn_register(dupl, phi_reg);
287 schedpoint = arg_block;
289 schedpoint = sched_prev(schedpoint);
290 } while (is_cfop(schedpoint));
291 sched_add_after(schedpoint, dupl);
292 pin_irn(dupl, phi_block);
293 be_liveness_introduce(lv, dupl);
294 be_liveness_update(lv, arg);
295 DBG((dbg, LEVEL_1, " they do interfere: insert %+F(%s)\n", dupl, arch_get_irn_register(dupl)->name));
296 continue; /* with next argument */
299 DBG((dbg, LEVEL_1, " they do not interfere\n"));
300 assert(is_Proj(arg));
302 First check if there is an other phi
304 - having arg at the current pos in its arg-list
305 - having the same color as arg
307 If found, then pin the arg (for that phi)
309 if (! is_pinned(arg)) {
312 DBG((dbg, LEVEL_1, " searching for phi with same arg having args register\n"));
314 for (other_phi = (ir_node*)get_irn_link(phi_block);
316 other_phi = (ir_node*)get_irn_link(other_phi)) {
318 assert(is_Phi(other_phi) &&
319 get_nodes_block(phi) == get_nodes_block(other_phi) &&
320 "link fields are screwed up");
322 if (get_irn_n(other_phi, i) == arg && arch_get_irn_register(other_phi) == arg_reg) {
323 DBG((dbg, LEVEL_1, " found %+F(%s)\n", other_phi, arch_get_irn_register(other_phi)->name));
324 pin_irn(arg, phi_block);
330 if (is_pinned(arg)) {
332 Insert a duplicate of the original value in arguments block,
333 make it the new phi arg,
335 insert it into schedule,
338 ir_node *perm = get_Proj_pred(arg);
339 ir_node *dupl = be_new_Copy(cls, arg_block, arg);
342 set_irn_n(phi, i, dupl);
343 arch_set_irn_register(dupl, phi_reg);
344 /* skip the Perm's Projs and insert the copies behind. */
345 for (ins = sched_next(perm); is_Proj(ins); ins = sched_next(ins)) {
347 sched_add_before(ins, dupl);
348 pin_irn(dupl, phi_block);
349 be_liveness_introduce(lv, dupl);
350 be_liveness_update(lv, arg);
351 DBG((dbg, LEVEL_1, " arg is pinned: insert %+F(%s)\n", dupl, arch_get_irn_register(dupl)->name));
354 No other phi has the same color (else arg would have been pinned),
355 so just set the register and pin
357 arch_set_irn_register(arg, phi_reg);
358 pin_irn(arg, phi_block);
359 DBG((dbg, LEVEL_1, " arg is not pinned: so pin %+F(%s)\n", arg, arch_get_irn_register(arg)->name));
365 void be_ssa_destruction(be_chordal_env_t *chordal_env)
367 insert_all_perms_env_t insert_perms_env;
368 pmap *perm_map = pmap_create();
369 ir_graph *irg = chordal_env->irg;
370 be_lv_t *lv = be_assure_liveness(irg);
372 FIRM_DBG_REGISTER(dbg, "ir.be.ssadestr");
374 be_liveness_invalidate(lv);
376 /* create a map for fast lookup of perms: block --> perm */
377 irg_walk_graph(irg, clear_link, collect_phis_walker, chordal_env);
379 DBG((dbg, LEVEL_1, "Placing perms...\n"));
380 insert_perms_env.chordal_env = chordal_env;
381 insert_perms_env.perm_map = perm_map;
382 irg_block_walk_graph(irg, insert_all_perms_walker, NULL, &insert_perms_env);
384 // Matze: really needed here?
385 // Sebastian: Yes. the walker function uses interference.
386 be_liveness_invalidate(lv);
388 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
389 dump_ir_graph(irg, "ssa_destr_perms_placed");
391 be_liveness_assure_chk(lv);
393 DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
394 irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
396 /* TODO: unfortunately updating doesn't work yet. */
397 be_liveness_invalidate(lv);
399 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
400 dump_ir_graph(irg, "ssa_destr_regs_set");
402 pmap_destroy(perm_map);
405 static void ssa_destruction_check_walker(ir_node *bl, void *data)
411 for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
412 phi = (ir_node*)get_irn_link(phi)) {
413 const arch_register_t *phi_reg, *arg_reg;
415 phi_reg = arch_get_irn_register(phi);
416 /* iterate over all args of phi */
417 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
418 ir_node *arg = get_irn_n(phi, i);
419 const arch_register_req_t *req = arch_get_irn_register_req(arg);
421 if (req->type & arch_register_req_type_ignore)
424 arg_reg = arch_get_irn_register(arg);
426 if (phi_reg != arg_reg) {
427 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
431 if (! is_pinned(arg)) {
432 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
439 void be_ssa_destruction_check(be_chordal_env_t *chordal_env)
441 irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, NULL);