2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Performs SSA-Destruction.
14 #include "bessadestr.h"
21 #include "iredges_t.h"
29 #include "bechordal_t.h"
36 #include "beintlive_t.h"
37 #include "bespillutil.h"
39 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
41 static void clear_link(ir_node *irn, void *data)
44 set_irn_link(irn, NULL);
48 * For each block build a linked list of phis that
50 * - have the current register class
51 * The list is rooted at get_irn_link(BB).
53 static void collect_phis_walker(ir_node *irn, void *data)
55 be_chordal_env_t *env = (be_chordal_env_t*)data;
56 if (is_Phi(irn) && arch_irn_consider_in_reg_alloc(env->cls, 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)
78 const perm_proj_t *p = (const perm_proj_t*)a;
79 const perm_proj_t *q = (const perm_proj_t*)b;
82 return !(p->arg == q->arg);
86 * Insert Perms in all predecessors of a block containing a phi
88 static void insert_all_perms_walker(ir_node *bl, void *data)
90 be_chordal_env_t *const chordal_env = (be_chordal_env_t*)data;
91 be_lv_t *lv = be_get_irg_liveness(chordal_env->irg);
96 /* If the link flag is NULL, this block has no phis. */
97 if (!get_irn_link(bl))
100 /* Look at all predecessors of the phi block */
101 for (i = 0, n = get_irn_arity(bl); i < n; ++i) {
102 ir_node *phi, *perm, **in;
103 set *arg_set = new_set(cmp_perm_proj, chordal_env->cls->n_regs);
104 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
108 * Note that all phis in the list are in the same
109 * register class by construction.
111 for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
112 phi = (ir_node*)get_irn_link(phi)) {
113 ir_node *arg = get_irn_n(phi, i);
117 hash = hash_irn(arg);
119 perm_proj_t *const pp = set_find(perm_proj_t, arg_set, &templ, sizeof(templ), hash);
122 * If a proj_perm_t entry has not been made in the argument set,
123 * create one. The only restriction is, that the phi argument
124 * may not be live in at the current block, since this argument
125 * interferes with the phi and must thus not be member of a
126 * Perm. A copy will be inserted for this argument later on.
128 if (!pp && !be_is_live_in(lv, bl, arg)) {
129 templ.pos = n_projs++;
130 (void)set_insert(perm_proj_t, arg_set, &templ, sizeof(templ), hash);
137 * Create a new Perm with the arguments just collected
138 * above in the arg_set and insert it into the schedule.
140 in = XMALLOCN(ir_node*, n_projs);
141 foreach_set(arg_set, perm_proj_t, pp) {
142 in[pp->pos] = pp->arg;
145 perm = be_new_Perm(chordal_env->cls, pred_bl, n_projs, in);
146 stat_ev_int("phi_perm", n_projs);
148 ir_node *const schedpoint = be_get_end_of_block_insertion_point(pred_bl);
149 sched_add_before(schedpoint, perm);
152 * Make the Projs for the Perm and insert into schedule.
153 * Register allocation is copied from the former phi
154 * arguments to the projs (new phi arguments).
156 foreach_set(arg_set, perm_proj_t, pp) {
157 ir_node *proj = new_r_Proj(perm, get_irn_mode(pp->arg), pp->pos);
159 assert(arch_get_irn_register(pp->arg));
160 arch_set_irn_register(proj, arch_get_irn_register(pp->arg));
161 DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", arch_get_irn_register(pp->arg)->name, pp->arg, pp->proj));
165 * Set the phi nodes to their new arguments: The Projs of the Perm
167 for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
168 phi = (ir_node*)get_irn_link(phi)) {
171 templ.arg = get_irn_n(phi, i);
172 perm_proj_t *const pp = set_find(perm_proj_t, arg_set, &templ, sizeof(templ), hash_irn(templ.arg));
174 /* If not found, it was an interfering argument */
176 set_irn_n(phi, i, pp->proj);
177 be_liveness_introduce(lv, pp->proj);
181 /* update the liveness of the Perm's operands. It might be changed. */
184 for (i = 0; i < n_projs; ++i)
185 be_liveness_update(lv, in[i]);
194 #define is_pinned(irn) (get_irn_link(irn))
195 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
196 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
199 * Adjusts the register allocation for the (new) phi-operands
200 * and insert duplicates iff necessary.
202 static void set_regs_or_place_dupls_walker(ir_node *bl, void *data)
204 be_chordal_env_t *chordal_env = (be_chordal_env_t*)data;
205 be_lv_t *lv = be_get_irg_liveness(chordal_env->irg);
208 /* Consider all phis of this block */
209 for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
210 phi = (ir_node*)get_irn_link(phi)) {
211 ir_node *phi_block = get_nodes_block(phi);
212 const arch_register_t *phi_reg = arch_get_irn_register(phi);
216 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
218 /* process all arguments of the phi */
219 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
220 ir_node *arg = get_irn_n(phi, i);
221 const arch_register_t *arg_reg;
224 arg_block = get_Block_cfgpred_block(phi_block, i);
225 arg_reg = arch_get_irn_register(arg);
227 assert(arg_reg && "Register must be set while placing perms");
229 DBG((dbg, LEVEL_1, " for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
231 if (phi_reg == arg_reg
232 || (arg_reg->type & arch_register_type_virtual)) {
233 /* Phi and arg have the same register, so pin and continue */
234 pin_irn(arg, phi_block);
235 DBG((dbg, LEVEL_1, " arg has same reg: pin %+F(%s)\n", arg, arch_get_irn_register(arg)->name));
239 if (be_values_interfere(lv, phi, arg)) {
241 Insert a duplicate in arguments block,
242 make it the new phi arg,
244 insert it into schedule,
247 ir_node *dupl = be_new_Copy(arg_block, arg);
249 set_irn_n(phi, i, dupl);
250 arch_set_irn_register(dupl, phi_reg);
251 ir_node *const schedpoint = be_get_end_of_block_insertion_point(arg_block);
252 sched_add_before(schedpoint, dupl);
253 pin_irn(dupl, phi_block);
254 be_liveness_introduce(lv, dupl);
255 be_liveness_update(lv, arg);
256 DBG((dbg, LEVEL_1, " they do interfere: insert %+F(%s)\n", dupl, arch_get_irn_register(dupl)->name));
257 continue; /* with next argument */
260 DBG((dbg, LEVEL_1, " they do not interfere\n"));
261 assert(is_Proj(arg));
263 First check if there is an other phi
265 - having arg at the current pos in its arg-list
266 - having the same color as arg
268 If found, then pin the arg (for that phi)
270 if (! is_pinned(arg)) {
273 DBG((dbg, LEVEL_1, " searching for phi with same arg having args register\n"));
275 for (other_phi = (ir_node*)get_irn_link(phi_block);
277 other_phi = (ir_node*)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 && arch_get_irn_register(other_phi) == arg_reg) {
284 DBG((dbg, LEVEL_1, " found %+F(%s)\n", other_phi, arch_get_irn_register(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(arg_block, arg);
302 set_irn_n(phi, i, dupl);
303 arch_set_irn_register(dupl, phi_reg);
304 sched_add_after(perm, dupl);
305 pin_irn(dupl, phi_block);
306 be_liveness_introduce(lv, dupl);
307 be_liveness_update(lv, arg);
308 DBG((dbg, LEVEL_1, " arg is pinned: insert %+F(%s)\n", dupl, arch_get_irn_register(dupl)->name));
311 No other phi has the same color (else arg would have been pinned),
312 so just set the register and pin
314 arch_set_irn_register(arg, phi_reg);
315 pin_irn(arg, phi_block);
316 DBG((dbg, LEVEL_1, " arg is not pinned: so pin %+F(%s)\n", arg, arch_get_irn_register(arg)->name));
322 void be_ssa_destruction(be_chordal_env_t *chordal_env)
324 ir_graph *irg = chordal_env->irg;
326 FIRM_DBG_REGISTER(dbg, "ir.be.ssadestr");
328 be_invalidate_live_sets(irg);
330 /* create a map for fast lookup of perms: block --> perm */
331 irg_walk_graph(irg, clear_link, collect_phis_walker, chordal_env);
333 DBG((dbg, LEVEL_1, "Placing perms...\n"));
334 irg_block_walk_graph(irg, insert_all_perms_walker, NULL, chordal_env);
336 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
337 dump_ir_graph(irg, "ssa_destr_perms_placed");
339 be_assure_live_chk(irg);
341 DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
342 irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
344 /* unfortunately updating doesn't work yet. */
345 be_invalidate_live_chk(irg);
347 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
348 dump_ir_graph(irg, "ssa_destr_regs_set");
351 static void ssa_destruction_check_walker(ir_node *bl, void *data)
357 for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
358 phi = (ir_node*)get_irn_link(phi)) {
359 const arch_register_t *phi_reg, *arg_reg;
361 phi_reg = arch_get_irn_register(phi);
362 /* iterate over all args of phi */
363 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
364 ir_node *arg = get_irn_n(phi, i);
365 const arch_register_req_t *req = arch_get_irn_register_req(arg);
366 if (arch_register_req_is(req, ignore))
369 arg_reg = arch_get_irn_register(arg);
371 if (phi_reg != arg_reg) {
372 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
376 if (! is_pinned(arg)) {
377 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
384 void be_ssa_destruction_check(be_chordal_env_t *chordal_env)
386 irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, NULL);