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
28 #include "bessadestr.h"
35 #include "iredges_t.h"
43 #include "bechordal_t.h"
48 #include "bestatevent.h"
50 #include "beintlive_t.h"
52 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
54 static void clear_link(ir_node *irn, void *data)
57 set_irn_link(irn, NULL);
61 * For each block build a linked list of phis that
63 * - have the current register class
64 * The list is rooted at get_irn_link(BB).
66 static void collect_phis_walker(ir_node *irn, void *data)
68 be_chordal_env_t *env = (be_chordal_env_t*)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)
91 const perm_proj_t *p = (const perm_proj_t*)a;
92 const perm_proj_t *q = (const perm_proj_t*)b;
95 return !(p->arg == q->arg);
99 * Insert Perms in all predecessors of a block containing a phi
101 static void insert_all_perms_walker(ir_node *bl, void *data)
103 be_chordal_env_t *const chordal_env = (be_chordal_env_t*)data;
104 be_lv_t *lv = be_get_irg_liveness(chordal_env->irg);
107 assert(is_Block(bl));
109 /* If the link flag is NULL, this block has no phis. */
110 if (!get_irn_link(bl))
113 /* Look at all predecessors of the phi block */
114 for (i = 0, n = get_irn_arity(bl); i < n; ++i) {
115 ir_node *phi, *perm, *insert_after, **in;
117 set *arg_set = new_set(cmp_perm_proj, chordal_env->cls->n_regs);
118 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
122 * Note that all phis in the list are in the same
123 * register class by construction.
125 for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
126 phi = (ir_node*)get_irn_link(phi)) {
127 ir_node *arg = get_irn_n(phi, i);
131 hash = hash_irn(arg);
133 pp = set_find(perm_proj_t, arg_set, &templ, sizeof(templ), hash);
136 * If a proj_perm_t entry has not been made in the argument set,
137 * create one. The only restriction is, that the phi argument
138 * may not be live in at the current block, since this argument
139 * interferes with the phi and must thus not be member of a
140 * Perm. A copy will be inserted for this argument later on.
142 if (!pp && !be_is_live_in(lv, bl, arg)) {
143 templ.pos = n_projs++;
144 set_insert(perm_proj_t, arg_set, &templ, sizeof(templ), hash);
151 * Create a new Perm with the arguments just collected
152 * above in the arg_set and insert it into the schedule.
154 in = XMALLOCN(ir_node*, n_projs);
155 foreach_set(arg_set, perm_proj_t, pp) {
156 in[pp->pos] = pp->arg;
159 perm = be_new_Perm(chordal_env->cls, pred_bl, n_projs, in);
160 be_stat_ev("phi_perm", n_projs);
162 insert_after = pred_bl;
164 insert_after = sched_prev(insert_after);
165 } while (is_cfop(insert_after));
166 sched_add_after(insert_after, perm);
169 * Make the Projs for the Perm and insert into schedule.
170 * Register allocation is copied from the former phi
171 * arguments to the projs (new phi arguments).
174 foreach_set(arg_set, perm_proj_t, pp) {
175 ir_node *proj = new_r_Proj(perm, get_irn_mode(pp->arg), pp->pos);
177 assert(arch_get_irn_register(pp->arg));
178 arch_set_irn_register(proj, arch_get_irn_register(pp->arg));
180 DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", arch_get_irn_register(pp->arg)->name, pp->arg, pp->proj));
184 * Set the phi nodes to their new arguments: The Projs of the Perm
186 for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
187 phi = (ir_node*)get_irn_link(phi)) {
190 templ.arg = get_irn_n(phi, i);
191 pp = set_find(perm_proj_t, arg_set, &templ, sizeof(templ), hash_irn(templ.arg));
193 /* If not found, it was an interfering argument */
195 set_irn_n(phi, i, pp->proj);
196 be_liveness_introduce(lv, pp->proj);
200 /* update the liveness of the Perm's operands. It might be changed. */
203 for (i = 0; i < n_projs; ++i)
204 be_liveness_update(lv, in[i]);
213 #define is_pinned(irn) (get_irn_link(irn))
214 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
215 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
218 * Adjusts the register allocation for the (new) phi-operands
219 * and insert duplicates iff necessary.
221 static void set_regs_or_place_dupls_walker(ir_node *bl, void *data)
223 be_chordal_env_t *chordal_env = (be_chordal_env_t*)data;
224 be_lv_t *lv = be_get_irg_liveness(chordal_env->irg);
227 /* Consider all phis of this block */
228 for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
229 phi = (ir_node*)get_irn_link(phi)) {
230 ir_node *phi_block = get_nodes_block(phi);
231 const arch_register_t *phi_reg = arch_get_irn_register(phi);
235 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
237 /* process all arguments of the phi */
238 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
239 ir_node *arg = get_irn_n(phi, i);
240 const arch_register_t *arg_reg;
243 arg_block = get_Block_cfgpred_block(phi_block, i);
244 arg_reg = arch_get_irn_register(arg);
246 assert(arg_reg && "Register must be set while placing perms");
248 DBG((dbg, LEVEL_1, " for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
250 if (phi_reg == arg_reg
251 || (arg_reg->type & arch_register_type_joker)
252 || (arg_reg->type & arch_register_type_virtual)) {
253 /* Phi and arg have the same register, so pin and continue */
254 pin_irn(arg, phi_block);
255 DBG((dbg, LEVEL_1, " arg has same reg: pin %+F(%s)\n", arg, arch_get_irn_register(arg)->name));
259 if (be_values_interfere(lv, 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(arg_block, arg);
271 set_irn_n(phi, i, dupl);
272 arch_set_irn_register(dupl, phi_reg);
273 schedpoint = arg_block;
275 schedpoint = sched_prev(schedpoint);
276 } while (is_cfop(schedpoint));
277 sched_add_after(schedpoint, dupl);
278 pin_irn(dupl, phi_block);
279 be_liveness_introduce(lv, dupl);
280 be_liveness_update(lv, arg);
281 DBG((dbg, LEVEL_1, " they do interfere: insert %+F(%s)\n", dupl, arch_get_irn_register(dupl)->name));
282 continue; /* with next argument */
285 DBG((dbg, LEVEL_1, " they do not interfere\n"));
286 assert(is_Proj(arg));
288 First check if there is an other phi
290 - having arg at the current pos in its arg-list
291 - having the same color as arg
293 If found, then pin the arg (for that phi)
295 if (! is_pinned(arg)) {
298 DBG((dbg, LEVEL_1, " searching for phi with same arg having args register\n"));
300 for (other_phi = (ir_node*)get_irn_link(phi_block);
302 other_phi = (ir_node*)get_irn_link(other_phi)) {
304 assert(is_Phi(other_phi) &&
305 get_nodes_block(phi) == get_nodes_block(other_phi) &&
306 "link fields are screwed up");
308 if (get_irn_n(other_phi, i) == arg && arch_get_irn_register(other_phi) == arg_reg) {
309 DBG((dbg, LEVEL_1, " found %+F(%s)\n", other_phi, arch_get_irn_register(other_phi)->name));
310 pin_irn(arg, phi_block);
316 if (is_pinned(arg)) {
318 Insert a duplicate of the original value in arguments block,
319 make it the new phi arg,
321 insert it into schedule,
324 ir_node *perm = get_Proj_pred(arg);
325 ir_node *dupl = be_new_Copy(arg_block, arg);
328 set_irn_n(phi, i, dupl);
329 arch_set_irn_register(dupl, phi_reg);
330 /* skip the Perm's Projs and insert the copies behind. */
331 for (ins = sched_next(perm); is_Proj(ins); ins = sched_next(ins)) {
333 sched_add_before(ins, dupl);
334 pin_irn(dupl, phi_block);
335 be_liveness_introduce(lv, dupl);
336 be_liveness_update(lv, arg);
337 DBG((dbg, LEVEL_1, " arg is pinned: insert %+F(%s)\n", dupl, arch_get_irn_register(dupl)->name));
340 No other phi has the same color (else arg would have been pinned),
341 so just set the register and pin
343 arch_set_irn_register(arg, phi_reg);
344 pin_irn(arg, phi_block);
345 DBG((dbg, LEVEL_1, " arg is not pinned: so pin %+F(%s)\n", arg, arch_get_irn_register(arg)->name));
351 void be_ssa_destruction(be_chordal_env_t *chordal_env)
353 ir_graph *irg = chordal_env->irg;
355 FIRM_DBG_REGISTER(dbg, "ir.be.ssadestr");
357 be_invalidate_live_sets(irg);
359 /* create a map for fast lookup of perms: block --> perm */
360 irg_walk_graph(irg, clear_link, collect_phis_walker, chordal_env);
362 DBG((dbg, LEVEL_1, "Placing perms...\n"));
363 irg_block_walk_graph(irg, insert_all_perms_walker, NULL, chordal_env);
365 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
366 dump_ir_graph(irg, "ssa_destr_perms_placed");
368 be_assure_live_chk(irg);
370 DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
371 irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
373 /* unfortunately updating doesn't work yet. */
374 be_invalidate_live_chk(irg);
376 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
377 dump_ir_graph(irg, "ssa_destr_regs_set");
380 static void ssa_destruction_check_walker(ir_node *bl, void *data)
386 for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
387 phi = (ir_node*)get_irn_link(phi)) {
388 const arch_register_t *phi_reg, *arg_reg;
390 phi_reg = arch_get_irn_register(phi);
391 /* iterate over all args of phi */
392 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
393 ir_node *arg = get_irn_n(phi, i);
394 const arch_register_req_t *req = arch_get_irn_register_req(arg);
396 if (req->type & arch_register_req_type_ignore)
399 arg_reg = arch_get_irn_register(arg);
401 if (phi_reg != arg_reg) {
402 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
406 if (! is_pinned(arg)) {
407 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
414 void be_ssa_destruction_check(be_chordal_env_t *chordal_env)
416 irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, NULL);