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"
28 #include "besched_t.h"
30 static firm_dbg_module_t *dbg = NULL;
31 #define DEBUG_LVL SET_LEVEL_0
34 #define get_chordal_arch(ce) ((ce)->session_env->main_env->arch_env)
35 #define get_reg(irn) arch_get_irn_register(get_chordal_arch(chordal_env), irn, 0)
36 #define set_reg(irn, reg) arch_set_irn_register(get_chordal_arch(chordal_env), irn, 0, reg)
38 #define is_Perm(irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_perm)
39 #define get_reg_cls(irn) (arch_get_irn_reg_class(arch_env, irn, arch_pos_make_out(0)))
40 #define is_curr_reg_class(irn) (get_reg_cls(p) == chordal_env->cls)
42 static void clear_link(ir_node *irn, void *data)
44 set_irn_link(irn, NULL);
48 * Build a list of phis of a block.
50 static void collect_phis(ir_node *irn, void *data)
52 be_chordal_env_t *env = data;
54 arch_irn_has_reg_class(env->session_env->main_env->arch_env,
55 irn, arch_pos_make_out(0), env->cls)) {
57 ir_node *bl = get_nodes_block(irn);
58 set_irn_link(irn, get_irn_link(bl));
59 set_irn_link(bl, irn);
64 * Build a ring of phis for each block in the link field.
65 * @param env The chordal env.
67 static INLINE void build_phi_rings(be_chordal_env_t *env)
69 irg_walk_graph(env->session_env->irg, clear_link, collect_phis, env);
72 static int skip_cf_predicator(const ir_node *irn, void *data) {
73 be_chordal_env_t *chordal_env = data;
74 arch_env_t *ae = chordal_env->session_env->main_env->arch_env;
75 return arch_irn_classify(ae, irn) == arch_irn_class_branch;
78 static void insert_all_perms_walker(ir_node *bl, void *data)
80 be_chordal_env_t *chordal_env = data;
81 pmap *perm_map = chordal_env->data;
82 ir_graph *irg = chordal_env->session_env->irg;
83 const be_node_factory_t *fact = chordal_env->session_env->main_env->node_factory;
85 /* Dummy targets for the projs */
86 ir_node *dummy = new_rd_Unknown(irg, mode_T);
90 /* If the link flag is NULL, this block has no phis. */
91 if(get_irn_link(bl)) {
94 /* Look at all predecessors of the phi block */
95 for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
96 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
97 ir_node *phi, *perm, *insert_after;
101 pmap *arg_map = pmap_create();
103 assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
106 * Note that all phis in the list are in the same register class
109 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
110 ir_node *arg = get_irn_n(phi, i);
111 ir_node *proj = pmap_get(arg_map, arg);
114 proj = new_r_Proj(irg, pred_bl, dummy, get_irn_mode(arg), n_projs++);
115 pmap_insert(arg_map, arg, proj);
118 set_irn_n(phi, i, proj);
122 in = malloc(n_projs * sizeof(in[0]));
123 pmap_foreach(arg_map, ent)
126 perm = new_Perm(fact, chordal_env->cls, irg, pred_bl, n_projs, in);
127 insert_after = sched_skip(sched_last(pred_bl), 0, skip_cf_predicator, chordal_env);
128 sched_add_after(insert_after, perm);
129 exchange(dummy, perm);
131 /* register allocation is copied form former arguments to the projs (new arguments) */
132 pmap_foreach(arg_map, ent) {
133 DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", get_reg(ent->key)->name, ent->key, ent->value));
134 set_reg(ent->value, get_reg(ent->key));
138 pmap_destroy(arg_map);
140 /* register in perm map */
141 pmap_insert(perm_map, pred_bl, perm);
146 static void insert_all_perms(be_chordal_env_t *chordal_env) {
147 DBG((dbg, LEVEL_1, "Placing perms...\n"));
148 irg_block_walk_graph(chordal_env->session_env->irg, insert_all_perms_walker, NULL, chordal_env);
151 #define is_pinned(irn) (get_irn_link(irn))
152 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
153 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
156 * Adjusts the register allocation for the phi-operands
157 * by inserting perm nodes, if necessary.
158 * @param phi The phi node to adjust operands for
160 static void adjust_phi_arguments(be_chordal_env_t *chordal_env, ir_node *phi) {
162 ir_node *arg, *phi_block, *arg_block;
163 const be_main_session_env_t *session = chordal_env->session_env;
164 const arch_register_t *phi_reg, *arg_reg;
165 const arch_register_class_t *cls;
167 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
168 DBG((dbg, LEVEL_1, " for %+F\n", phi));
170 cls = arch_get_irn_reg_class(get_chordal_arch(chordal_env), phi, arch_pos_make_out(0));
171 phi_block = get_nodes_block(phi);
172 phi_reg = get_reg(phi);
174 /* process all arguments of the phi */
175 for(i=0, max=get_irn_arity(phi); i<max; ++i) {
178 arg = get_irn_n(phi, i);
179 assert(is_Proj(arg));
180 arg_block = get_nodes_block(arg);
181 arg_reg = get_reg(arg);
182 assert(arg_reg && "Register must be set while placing perms");
183 DBG((dbg, LEVEL_3, "%+F has register %s assigned\n", arg, arg_reg->name));
184 perm = get_Proj_pred(arg);
185 //TODO reenable this if classify is implemented. assert(is_Perm(perm));
187 DBG((dbg, LEVEL_1, " arg %+F has perm %+F\n", arg, perm));
188 /* if registers don't match ...*/
189 if (phi_reg != arg_reg) {
190 DBG((dbg, LEVEL_1, " regs don't match %d %d\n", phi_reg->name, arg_reg->name));
192 /* First check if there is another phi in the same block
193 * having arg at the same pos in its arg-list and the same color as arg */
194 if (!is_pinned(arg)) {
195 DBG((dbg, LEVEL_1, " arg is not pinned\n"));
196 ir_node *other_phi = phi;
197 for(other_phi = get_irn_link(phi_block); other_phi; other_phi = get_irn_link(other_phi)) {
200 assert(is_Phi(other_phi) && get_nodes_block(phi) == get_nodes_block(other_phi) && "link fields are screwed up");
201 if (get_irn_n(other_phi, i) == arg && get_reg(other_phi) == arg_reg) {
202 DBG((dbg, LEVEL_1, " but other phi (%+F) just pinned it\n", other_phi));
203 pin_irn(arg, phi_block);
208 /* If arg is pinned, another phi set the color of arg and pinned it.
209 * So this phi can't change the color again and a duplicate must be inserted.
211 * If arg interferes with phi, one can never set the same color for both
212 * Hence, a duplicate must be inserted */
213 if (is_pinned(arg) || nodes_interfere(chordal_env, phi, arg)) {
215 assert(get_pinning_block(arg) == phi_block && "If arg is pinned it must be due to a phi in the same block");
217 dupl = new_Copy(session->main_env->node_factory, cls, session->irg, arg_block, arg);
218 set_irn_n(phi, i, dupl);
219 set_reg(dupl, phi_reg);
220 DBG((dbg, LEVEL_1, " arg is pinned so insert dupl %+F\n", dupl));
222 /* Add dupl to schedule */
223 tmp = sched_next(perm);
224 while (is_Proj(tmp) && sched_has_next(tmp))
225 tmp = sched_next(tmp);
226 sched_add_after(tmp, dupl);
228 /* now the arg is the dupl */
231 /* Arg is not pinned. So set its color to the color of the phi.
232 * If the phi color is used by another proj of this perm
233 * one must NOT swap the colors. Proof: Critical edges removed,
234 * livein(PhiBl) = liveout(ArgBl), if all phis are processed then
235 * every color is used exactly once.
237 DBG((dbg, LEVEL_1, " arg is not pinned so just set register to %s\n", phi_reg->name));
238 set_reg(arg, phi_reg);
241 DBG((dbg, LEVEL_1, " regs match %d\n"));
244 /* Now the color of the arg (arg may be a dupl now) and the phi-result are equal.
245 * Pin it, so everyone knows and it never gets changed again.
246 * An arg never is a phi, because perms were inserted. So the link field is free */
247 DBG((dbg, LEVEL_1, " arg has correct color (now), so pin it\n"));
248 pin_irn(arg, phi_block);
252 static void set_regs_or_place_dupls_walker(ir_node *bl, void *data) {
253 be_chordal_env_t *chordal_env = data;
256 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi))
257 adjust_phi_arguments(chordal_env, phi);
260 static void set_regs_or_place_dupls(be_chordal_env_t *chordal_env)
262 DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
263 irg_block_walk_graph(chordal_env->session_env->irg,
264 set_regs_or_place_dupls_walker, NULL, chordal_env);
268 void be_ssa_destruction(be_chordal_env_t *chordal_env) {
269 pmap *perm_map = pmap_create();
270 ir_graph *irg = chordal_env->session_env->irg;
272 dbg = firm_dbg_register("ir.be.ssadestr");
273 firm_dbg_set_mask(dbg, DEBUG_LVL);
275 /* create a map for fast lookup of perms: block --> perm */
276 chordal_env->data = perm_map;
278 build_phi_rings(chordal_env);
279 insert_all_perms(chordal_env);
280 dump_ir_block_graph(irg, "-ssa_destr_perms_placed");
282 set_regs_or_place_dupls(chordal_env);
283 dump_ir_block_graph(irg, "-ssa_destr_regs_set");
285 pmap_destroy(perm_map);
288 static void ssa_destruction_check_walker(ir_node *bl, void *data)
290 be_chordal_env_t *chordal_env = data;
294 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
295 const arch_register_t *phi_reg, *arg_reg;
297 phi_reg = get_reg(phi);
298 /* iterate over all args of phi */
299 for(i=0, max=get_irn_arity(phi); i<max; ++i) {
300 ir_node *arg = get_irn_n(phi, i);
301 arg_reg = get_reg(arg);
302 if(phi_reg != arg_reg) {
303 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
306 if(!is_pinned(arg)) {
307 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
314 void be_ssa_destruction_check(be_chordal_env_t *chordal_env) {
315 irg_block_walk_graph(chordal_env->session_env->irg, ssa_destruction_check_walker, NULL, chordal_env);