renamed ent_visibility to visibility
[libfirm] / ir / be / bessadestr.c
1 /**
2  * Author:      Daniel Grund
3  * Date:                25.05.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  *
7  * Performs SSA-Destruction.
8  */
9
10 #include "debug.h"
11 #include "set.h"
12 #include "pmap.h"
13 #include "irnode.h"
14 #include "iredges_t.h"
15 #include "be_t.h"
16 #include "bechordal_t.h"
17 #include "bearch.h"
18 #include "benode_t.h"
19 #include "besched_t.h"
20
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)
23
24 /**
25  * Maps blocks to perm nodes inserted during phi destruction.
26  */
27 typedef struct _block2perm_t {
28         ir_node *block, *perm;
29 } block2perm_t;
30
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;
35 }
36
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)
41
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;
44         ir_node *p;
45         set *b2p = chordal_env->data;
46         const arch_env_t *arch_env = chordal_env->arch_env;
47
48         /* iff needed insert perm node */
49
50         /* .if the perm is in the pset return it */
51         find.block = block;
52         find.perm = NULL;
53         found = set_insert(b2p, &find, sizeof(find), HASH_PTR(find.block));
54         if (found->perm)
55                 return found->perm;
56
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))))
60                 p = sched_prev(p);
61
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);
65
66         /* insert perm into pset */
67         found->perm = p;
68         return p;
69 }
70
71 /**
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
75  */
76 static void adjust_arguments(be_main_session_env_t *session, be_chordal_env_t *chordal_env, const ir_node *phi) {
77         int i, max;
78         ir_node *arg, *perm, *proj;
79         const arch_register_t *phi_reg, *arg_reg, *proj_reg;
80         const ir_edge_t *edge;
81
82         assert(is_Phi(phi) && "Can only handle phi-destruction :)");
83
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);
100                         }
101                 }
102         }
103 }
104
105 static void checker(be_chordal_env_t *chordal_env) {
106         pmap_entry *pme;
107         int i, max;
108
109         /* iterate over all blocks */
110         pmap_foreach(chordal_env->border_heads, pme) {
111                 border_t *curr;
112                 struct list_head *head = pme->value;
113
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))
119                                         break;
120
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!?");
126                                 }
127                         }
128         }
129 }
130
131 void be_ssa_destruction(be_main_session_env_t *session, be_chordal_env_t *chordal_env) {
132         pmap_entry *pme;
133         set *b2p;
134
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) {
139                 border_t *curr;
140                 struct list_head *head = pme->value;
141
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))
146                                         break;
147                                 adjust_arguments(session, chordal_env, curr->irn);
148                         }
149         }
150         del_set(b2p);
151         checker(chordal_env);
152 }