4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
7 * Performs SSA-Destruction.
14 #include "iredges_t.h"
16 #include "bechordal_t.h"
19 #include "besched_t.h"
21 #define get_reg(irn) arch_get_irn_register(chordal_env->arch_env, irn, 0)
22 #define set_reg(irn, reg) arch_set_irn_register(chordal_env->arch_env, irn, 0, reg)
25 * Maps blocks to perm nodes inserted during phi destruction.
27 typedef struct _block2perm_t {
28 ir_node *block, *perm;
31 static int set_cmp_b2p(const void *x, const void *y, size_t size) {
32 const block2perm_t *b1 = x;
33 const block2perm_t *b2 = y;
34 return b1->block != b2->block;
37 #define is_Branch(irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_branch)
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 ir_node *get_perm(be_main_session_env_t *session, be_chordal_env_t *chordal_env, ir_node *block) {
43 block2perm_t find, *found;
45 set *b2p = chordal_env->data;
46 const arch_env_t *arch_env = chordal_env->arch_env;
48 /* iff needed insert perm node */
50 /* .if the perm is in the pset return it */
53 found = set_insert(b2p, &find, sizeof(find), HASH_PTR(find.block));
57 /* .else look for a perm of right register class in the schedule */
58 p = sched_last(find.block);
59 while (!is_Block(p) && (is_Branch(p) || (is_Perm(p) && !is_curr_reg_class(p))))
62 /* if we haven't found a perm of the right register class create a new one */
63 if (! (is_Perm(p) && is_curr_reg_class(p)))
64 p = insert_Perm_after(session, chordal_env->cls, p);
66 /* insert perm into pset */
72 * Adjusts the register allocation for the phi-operands
73 * by inserting perm nodes, if necessary.
74 * @param phi The phi node to adjust operands for
76 static void adjust_arguments(be_main_session_env_t *session, be_chordal_env_t *chordal_env, const ir_node *phi) {
78 ir_node *arg, *perm, *proj;
79 const arch_register_t *phi_reg, *arg_reg, *proj_reg;
80 const ir_edge_t *edge;
82 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
84 phi_reg = get_reg(phi);
85 /* all arguments of the phi */
86 for(i=0, max=get_irn_arity(phi); i<max; ++i) {
87 arg = get_irn_n(phi, i);
88 arg_reg = get_reg(arg);
89 /* if registers don't match ...*/
90 if (phi_reg != arg_reg) {
91 perm = get_perm(session, chordal_env, get_nodes_block(arg));
92 /* adjust assigned registers for the projs */
93 foreach_out_edge(perm, edge) {
94 proj = get_edge_src_irn(edge);
95 proj_reg = get_reg(proj);
96 if (proj_reg == arg_reg)
97 set_reg(proj, phi_reg);
98 else if (proj_reg == phi_reg)
99 set_reg(proj, arg_reg);
105 static void checker(be_chordal_env_t *chordal_env) {
109 /* iterate over all blocks */
110 pmap_foreach(chordal_env->border_heads, pme) {
112 struct list_head *head = pme->value;
114 /* iterate over the first ops in the block */
115 list_for_each_entry_reverse(border_t, curr, head, list)
116 if (curr->is_def && curr->is_real && is_Phi(curr->irn)) {
117 const arch_register_t *phi_reg, *arg_reg;
118 if (!is_Phi(curr->irn))
121 phi_reg = get_reg(curr->irn);
122 /* iterate over all args of phi */
123 for(i=0, max=get_irn_arity(curr->irn); i<max; ++i) {
124 arg_reg = get_reg(get_irn_n(curr->irn, i));
125 assert(phi_reg == arg_reg && "WTF? You can do it better!?");
131 void be_ssa_destruction(be_main_session_env_t *session, be_chordal_env_t *chordal_env) {
135 b2p = new_set(set_cmp_b2p, 32);
136 chordal_env->data = b2p;
137 /* iterate over all blocks */
138 pmap_foreach(chordal_env->border_heads, pme) {
140 struct list_head *head = pme->value;
142 /* iterate over the first ops in the block until a non-phi is reached */
143 list_for_each_entry_reverse(border_t, curr, head, list)
144 if (curr->is_def && curr->is_real) {
145 if (!is_Phi(curr->irn))
147 adjust_arguments(session, chordal_env, curr->irn);
151 checker(chordal_env);