4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
7 * Performs SSA-Destruction.
18 #include "iredges_t.h"
25 #include "bechordal_t.h"
29 #include "besched_t.h"
31 static firm_dbg_module_t *dbg = NULL;
32 #define DEBUG_LVL SET_LEVEL_0
35 #define get_chordal_arch(ce) ((ce)->session_env->main_env->arch_env)
36 #define get_reg(irn) arch_get_irn_register(get_chordal_arch(chordal_env), irn, 0)
37 #define set_reg(irn, reg) arch_set_irn_register(get_chordal_arch(chordal_env), irn, 0, reg)
39 #define is_Perm(irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_perm)
40 #define get_reg_cls(irn) (arch_get_irn_reg_class(arch_env, irn, arch_pos_make_out(0)))
41 #define is_curr_reg_class(irn) (get_reg_cls(p) == chordal_env->cls)
43 static void clear_link(ir_node *irn, void *data)
45 set_irn_link(irn, NULL);
49 * Build a list of phis of a block.
51 static void collect_phis(ir_node *irn, void *data)
53 be_chordal_env_t *env = data;
55 arch_irn_has_reg_class(env->session_env->main_env->arch_env,
56 irn, arch_pos_make_out(0), env->cls)) {
58 ir_node *bl = get_nodes_block(irn);
59 set_irn_link(irn, get_irn_link(bl));
60 set_irn_link(bl, irn);
65 * Build a ring of phis for each block in the link field.
66 * @param env The chordal env.
68 static INLINE void build_phi_rings(be_chordal_env_t *env)
70 irg_walk_graph(env->session_env->irg, clear_link, collect_phis, env);
73 static int skip_cf_predicator(const ir_node *irn, void *data) {
74 be_chordal_env_t *chordal_env = data;
75 arch_env_t *ae = chordal_env->session_env->main_env->arch_env;
76 return arch_irn_classify(ae, irn) == arch_irn_class_branch;
79 static void insert_all_perms_walker(ir_node *bl, void *data)
81 be_chordal_env_t *chordal_env = data;
82 pmap *perm_map = chordal_env->data;
83 ir_graph *irg = chordal_env->session_env->irg;
84 const be_node_factory_t *fact = chordal_env->session_env->main_env->node_factory;
86 /* Dummy targets for the projs */
87 ir_node *dummy = new_rd_Unknown(irg, mode_T);
91 /* If the link flag is NULL, this block has no phis. */
92 if(get_irn_link(bl)) {
95 /* Look at all predecessors of the phi block */
96 for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
97 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
98 ir_node *phi, *perm, *insert_after;
102 pmap *arg_map = pmap_create();
104 assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
107 * Note that all phis in the list are in the same register class
110 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
111 ir_node *arg = get_irn_n(phi, i);
112 ir_node *proj = pmap_get(arg_map, arg);
114 if(!proj && !is_live_in(bl, arg)) {
115 proj = new_r_Proj(irg, pred_bl, dummy, get_irn_mode(arg), n_projs++);
116 pmap_insert(arg_map, arg, proj);
120 assert(get_irn_mode(phi) == get_irn_mode(proj));
121 set_irn_n(phi, i, proj);
125 in = malloc(n_projs * sizeof(in[0]));
126 pmap_foreach(arg_map, ent) {
127 int proj_nr = get_Proj_proj(ent->value);
128 in[proj_nr] = ent->key;
131 perm = new_Perm(fact, chordal_env->cls, irg, pred_bl, n_projs, in);
132 insert_after = sched_skip(sched_last(pred_bl), 0, skip_cf_predicator, chordal_env);
133 sched_add_after(insert_after, perm);
134 exchange(dummy, perm);
136 /* register allocation is copied form former arguments to the projs (new arguments) */
137 pmap_foreach(arg_map, ent) {
138 DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", get_reg(ent->key)->name, ent->key, ent->value));
139 set_reg(ent->value, get_reg(ent->key));
143 pmap_destroy(arg_map);
145 /* register in perm map */
146 pmap_insert(perm_map, pred_bl, perm);
151 static void insert_all_perms(be_chordal_env_t *chordal_env) {
152 DBG((dbg, LEVEL_1, "Placing perms...\n"));
153 irg_block_walk_graph(chordal_env->session_env->irg, insert_all_perms_walker, NULL, chordal_env);
156 #define is_pinned(irn) (get_irn_link(irn))
157 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
158 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
162 * Adjusts the register allocation for the phi-operands
163 * by inserting perm nodes, if necessary.
164 * @param phi The phi node to adjust operands for
166 static void adjust_phi_arguments(be_chordal_env_t *chordal_env, ir_node *phi) {
168 ir_node *arg, *phi_block, *arg_block;
169 const be_main_session_env_t *session = chordal_env->session_env;
170 const arch_register_t *phi_reg, *arg_reg;
171 const arch_register_class_t *cls;
173 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
175 phi_block = get_nodes_block(phi);
176 phi_reg = get_reg(phi);
177 cls = arch_get_irn_reg_class(get_chordal_arch(chordal_env), phi, arch_pos_make_out(0));
179 /* process all arguments of the phi */
180 for(i=0, max=get_irn_arity(phi); i<max; ++i) {
181 arg = get_irn_n(phi, i);
182 arg_block = get_Block_cfgpred_block(phi_block, i);
183 arg_reg = get_reg(arg);
184 assert(arg_reg && "Register must be set while placing perms");
186 DBG((dbg, LEVEL_1, " for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
188 if(nodes_interfere(chordal_env, phi, arg)) {
189 /* Insert a duplicate in arguments block,
190 * make it the new phi arg,
192 * insert it into schedule,
195 ir_node *dupl = new_Copy(session->main_env->node_factory, cls, session->irg, arg_block, arg);
196 assert(get_irn_mode(phi) == get_irn_mode(dupl));
197 set_irn_n(phi, i, dupl);
198 set_reg(dupl, phi_reg);
199 sched_add_after(sched_skip(sched_last(arg_block), 0, skip_cf_predicator, chordal_env), dupl);
200 pin_irn(dupl, phi_block);
201 DBG((dbg, LEVEL_1, " they do interfere: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
204 * First check if there is a phi
205 * - in the same block
206 * - having arg at the current pos in its arg-list
207 * - having the same color as arg
209 * If found, then pin the arg
211 DBG((dbg, LEVEL_1, " they do not interfere\n"));
212 assert(is_Proj(arg));
213 if (!is_pinned(arg)) {
215 DBG((dbg, LEVEL_1, " searching for phi with same arg having args register\n"));
216 for(other_phi = get_irn_link(phi_block); other_phi; other_phi = get_irn_link(other_phi)) {
217 assert(is_Phi(other_phi) && get_nodes_block(phi) == get_nodes_block(other_phi) && "link fields are screwed up");
218 if (get_irn_n(other_phi, i) == arg && get_reg(other_phi) == arg_reg) {
219 DBG((dbg, LEVEL_1, " found %+F(%s)\n", other_phi, get_reg(other_phi)->name));
220 pin_irn(arg, phi_block);
225 if (is_pinned(arg)) {
226 /* Insert a duplicate of the original value in arguments block,
227 * make it the new phi arg,
229 * insert it into schedule,
232 ir_node *perm = get_Proj_pred(arg);
233 ir_node *orig_val = get_irn_n(perm, get_Proj_proj(arg));
234 ir_node *dupl = new_Copy(session->main_env->node_factory, cls, session->irg, arg_block, orig_val);
235 assert(get_irn_mode(phi) == get_irn_mode(dupl));
236 set_irn_n(phi, i, dupl);
237 set_reg(dupl, phi_reg);
238 sched_add_before(perm, dupl);
239 pin_irn(dupl, phi_block);
240 DBG((dbg, LEVEL_1, " arg is pinned: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
242 /* No other phi has the same color (else arg would be pinned),
243 * so just set the register and pin
245 set_reg(arg, phi_reg);
246 pin_irn(arg, phi_block);
247 DBG((dbg, LEVEL_1, " arg is not pinned: so pin %+F(%s)\n", arg, get_reg(arg)->name));
253 static void set_regs_or_place_dupls_walker(ir_node *bl, void *data) {
254 be_chordal_env_t *chordal_env = data;
257 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi))
258 adjust_phi_arguments(chordal_env, phi);
261 static void set_regs_or_place_dupls(be_chordal_env_t *chordal_env)
263 DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
264 irg_block_walk_graph(chordal_env->session_env->irg,
265 set_regs_or_place_dupls_walker, NULL, chordal_env);
269 void be_ssa_destruction(be_chordal_env_t *chordal_env) {
270 pmap *perm_map = pmap_create();
271 ir_graph *irg = chordal_env->session_env->irg;
273 dbg = firm_dbg_register("ir.be.ssadestr");
274 firm_dbg_set_mask(dbg, DEBUG_LVL);
276 /* create a map for fast lookup of perms: block --> perm */
277 chordal_env->data = perm_map;
279 build_phi_rings(chordal_env);
280 insert_all_perms(chordal_env);
282 dump_ir_block_graph_sched(irg, "-ssa_destr_perms_placed");
284 set_regs_or_place_dupls(chordal_env);
286 dump_ir_block_graph_sched(irg, "-ssa_destr_regs_set");
289 pmap_destroy(perm_map);
292 static void ssa_destruction_check_walker(ir_node *bl, void *data)
294 be_chordal_env_t *chordal_env = data;
298 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
299 const arch_register_t *phi_reg, *arg_reg;
301 phi_reg = get_reg(phi);
302 /* iterate over all args of phi */
303 for(i=0, max=get_irn_arity(phi); i<max; ++i) {
304 ir_node *arg = get_irn_n(phi, i);
305 arg_reg = get_reg(arg);
306 if(phi_reg != arg_reg) {
307 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
310 if(!is_pinned(arg)) {
311 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
318 void be_ssa_destruction_check(be_chordal_env_t *chordal_env) {
319 irg_block_walk_graph(chordal_env->session_env->irg, ssa_destruction_check_walker, NULL, chordal_env);