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
34 #include "iredges_t.h"
42 #include "bechordal_t.h"
46 #include "besched_t.h"
47 #include "bestatevent.h"
49 #include "beintlive_t.h"
51 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
53 #define get_reg(irn) arch_get_irn_register(irn)
54 #define set_reg(irn, reg) arch_set_irn_register(irn, reg)
56 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) {
68 be_chordal_env_t *env = data;
69 if (is_Phi(irn) && chordal_has_class(env, irn)) {
70 ir_node *bl = get_nodes_block(irn);
71 set_irn_link(irn, get_irn_link(bl));
72 set_irn_link(bl, irn);
77 * This struct represents a Proj for a Perm.
78 * It records the argument in the Perm and the corresponding Proj of the
82 ir_node *arg; /**< The phi argument to make the Proj for. */
83 int pos; /**< The proj number the Proj will get.
84 This also denotes the position of @p arg
85 in the in array of the Perm. */
86 ir_node *proj; /**< The proj created for @p arg. */
89 static int cmp_perm_proj(const void *a, const void *b, size_t n) {
90 const perm_proj_t *p = a;
91 const perm_proj_t *q = b;
94 return !(p->arg == q->arg);
97 typedef struct insert_all_perms_env_t {
98 be_chordal_env_t *chordal_env;
100 } insert_all_perms_env_t;
103 * Insert Perms in all predecessors of a block containing a phi
105 static void insert_all_perms_walker(ir_node *bl, void *data) {
106 insert_all_perms_env_t *env = data;
107 be_chordal_env_t *chordal_env = env->chordal_env;
108 pmap *perm_map = env->perm_map;
109 be_lv_t *lv = chordal_env->birg->lv;
112 assert(is_Block(bl));
114 /* If the link flag is NULL, this block has no phis. */
115 if(!get_irn_link(bl))
118 /* Look at all predecessors of the phi block */
119 for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
120 ir_node *phi, *perm, *insert_after, **in;
122 set *arg_set = new_set(cmp_perm_proj, chordal_env->cls->n_regs);
123 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
126 assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
129 * Note that all phis in the list are in the same
130 * register class by construction.
132 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
133 ir_node *arg = get_irn_n(phi, i);
134 const arch_register_req_t *req = arch_get_register_req_out(arg);
138 if (req->type & arch_register_req_type_ignore)
141 hash = hash_irn(arg);
143 pp = 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 alter 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 for(pp = set_first(arg_set); pp; pp = set_next(arg_set))
166 in[pp->pos] = pp->arg;
168 perm = be_new_Perm(chordal_env->cls, pred_bl, n_projs, in);
169 be_stat_ev("phi_perm", n_projs);
171 insert_after = sched_skip(sched_last(pred_bl), 0, sched_skip_cf_predicator, NULL);
172 sched_add_after(insert_after, perm);
175 * Make the Projs for the Perm and insert into schedule.
176 * Register allocation is copied from the former phi
177 * arguments to the projs (new phi arguments).
180 for(pp = set_first(arg_set); pp; pp = set_next(arg_set)) {
181 ir_node *proj = new_r_Proj(pred_bl, perm, get_irn_mode(pp->arg), pp->pos);
183 assert(get_reg(pp->arg));
184 set_reg(proj, get_reg(pp->arg));
186 DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", get_reg(pp->arg)->name, pp->arg, pp->proj));
190 * Set the phi nodes to their new arguments: The Projs of the Perm
192 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
195 templ.arg = get_irn_n(phi, i);
196 pp = set_find(arg_set, &templ, sizeof(templ), hash_irn(templ.arg));
198 /* If not found, it was an interfering argument */
200 set_irn_n(phi, i, pp->proj);
201 be_liveness_introduce(lv, pp->proj);
205 /* update the liveness of the Perm's operands. It might be changed. */
208 for (i = 0; i < n_projs; ++i)
209 be_liveness_update(lv, in[i]);
213 /* register in perm map */
214 pmap_insert(perm_map, pred_bl, perm);
221 #define is_pinned(irn) (get_irn_link(irn))
222 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
223 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
226 * Adjusts the register allocation for the (new) phi-operands
227 * and insert duplicates iff necessary.
229 static void set_regs_or_place_dupls_walker(ir_node *bl, void *data) {
230 be_chordal_env_t *chordal_env = data;
231 be_lv_t *lv = chordal_env->birg->lv;
234 /* Consider all phis of this block */
235 for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
236 ir_node *phi_block = get_nodes_block(phi);
237 const arch_register_t *phi_reg = get_reg(phi);
238 const arch_register_class_t *cls = phi_reg->reg_class;
242 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
244 /* process all arguments of the phi */
245 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
246 ir_node *arg = get_irn_n(phi, i);
247 const arch_register_req_t *req = arch_get_register_req_out(arg);
248 const arch_register_t *arg_reg;
251 if (req->type & arch_register_req_type_ignore)
254 arg_block = get_Block_cfgpred_block(phi_block, i);
255 arg_reg = get_reg(arg);
257 assert(arg_reg && "Register must be set while placing perms");
259 DBG((dbg, LEVEL_1, " for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
261 if (values_interfere(chordal_env->birg, phi, arg)) {
263 Insert a duplicate in arguments block,
264 make it the new phi arg,
266 insert it into schedule,
269 ir_node *dupl = be_new_Copy(cls, arg_block, arg);
271 /* this is commented out because it will fail in case of unknown float */
273 ir_mode *m_phi = get_irn_mode(phi), *m_dupl = get_irn_mode(dupl);
276 Conv signed <-> unsigned is killed on ia32
277 check for: (both int OR both float) AND equal mode sizes
279 assert(((mode_is_int(m_phi) && mode_is_int(m_dupl)) ||
280 (mode_is_float(m_phi) && mode_is_float(m_dupl))) &&
281 (get_mode_size_bits(m_phi) == get_mode_size_bits(m_dupl)));
284 set_irn_n(phi, i, dupl);
285 set_reg(dupl, phi_reg);
286 sched_add_after(sched_skip(sched_last(arg_block), 0, sched_skip_cf_predicator, NULL), dupl);
287 pin_irn(dupl, phi_block);
288 be_liveness_introduce(lv, dupl);
289 be_liveness_update(lv, arg);
290 DBG((dbg, LEVEL_1, " they do interfere: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
291 continue; /* with next argument */
294 if (phi_reg == arg_reg) {
295 /* Phi and arg have the same register, so pin and continue */
296 pin_irn(arg, phi_block);
297 DBG((dbg, LEVEL_1, " arg has same reg: pin %+F(%s)\n", arg, get_reg(arg)->name));
301 DBG((dbg, LEVEL_1, " they do not interfere\n"));
302 assert(is_Proj(arg));
304 First check if there is an other phi
306 - having arg at the current pos in its arg-list
307 - having the same color as arg
309 If found, then pin the arg (for that phi)
311 if (! is_pinned(arg)) {
314 DBG((dbg, LEVEL_1, " searching for phi with same arg having args register\n"));
316 for(other_phi = get_irn_link(phi_block); other_phi; other_phi = 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 && get_reg(other_phi) == arg_reg) {
323 DBG((dbg, LEVEL_1, " found %+F(%s)\n", other_phi, get_reg(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 /* this is commented out because it will fail in case of unknown float */
344 ir_mode *m_phi = get_irn_mode(phi);
345 ir_mode *m_dupl = get_irn_mode(dupl);
348 Conv signed <-> unsigned is killed on ia32
349 check for: (both int OR both float) AND equal mode sizes
351 assert(((mode_is_int(m_phi) && mode_is_int(m_dupl)) ||
352 (mode_is_float(m_phi) && mode_is_float(m_dupl))) &&
353 (get_mode_size_bits(m_phi) == get_mode_size_bits(m_dupl)));
356 set_irn_n(phi, i, dupl);
357 set_reg(dupl, phi_reg);
358 /* skip the Perm's Projs and insert the copies behind. */
359 for(ins = sched_next(perm); is_Proj(ins); ins = sched_next(ins));
360 sched_add_before(ins, dupl);
361 pin_irn(dupl, phi_block);
362 be_liveness_introduce(lv, dupl);
363 be_liveness_update(lv, arg);
364 DBG((dbg, LEVEL_1, " arg is pinned: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
367 No other phi has the same color (else arg would have been pinned),
368 so just set the register and pin
370 set_reg(arg, phi_reg);
371 pin_irn(arg, phi_block);
372 DBG((dbg, LEVEL_1, " arg is not pinned: so pin %+F(%s)\n", arg, get_reg(arg)->name));
378 void be_ssa_destruction(be_chordal_env_t *chordal_env) {
379 insert_all_perms_env_t insert_perms_env;
380 pmap *perm_map = pmap_create();
381 ir_graph *irg = chordal_env->irg;
382 be_lv_t *lv = be_assure_liveness(chordal_env->birg);
384 FIRM_DBG_REGISTER(dbg, "ir.be.ssadestr");
386 be_liveness_invalidate(lv);
388 /* create a map for fast lookup of perms: block --> perm */
389 irg_walk_graph(irg, clear_link, collect_phis_walker, chordal_env);
391 DBG((dbg, LEVEL_1, "Placing perms...\n"));
392 insert_perms_env.chordal_env = chordal_env;
393 insert_perms_env.perm_map = perm_map;
394 irg_block_walk_graph(irg, insert_all_perms_walker, NULL, &insert_perms_env);
396 // Matze: really needed here?
397 // Sebastian: Yes. the walker function uses interference.
398 be_liveness_invalidate(lv);
400 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
401 be_dump(irg, "-ssa_destr_perms_placed", dump_ir_block_graph_sched);
403 be_liveness_assure_chk(lv);
405 DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
406 irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
408 /* TODO: unfortunaltely updating doesn't work yet. */
409 be_liveness_invalidate(lv);
411 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
412 be_dump(irg, "-ssa_destr_regs_set", dump_ir_block_graph_sched);
414 pmap_destroy(perm_map);
417 static void ssa_destruction_check_walker(ir_node *bl, void *data) {
422 for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
423 const arch_register_t *phi_reg, *arg_reg;
425 phi_reg = get_reg(phi);
426 /* iterate over all args of phi */
427 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
428 ir_node *arg = get_irn_n(phi, i);
429 const arch_register_req_t *req = arch_get_register_req_out(arg);
431 if (req->type & arch_register_req_type_ignore)
434 arg_reg = get_reg(arg);
436 if (phi_reg != arg_reg) {
437 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
441 if (! is_pinned(arg)) {
442 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
449 void be_ssa_destruction_check(be_chordal_env_t *chordal_env) {
450 irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, NULL);