Added SSA destruction. Bugfixes, small improvements.
[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 /**
38  * Adjusts the register allocation for the phi-operands
39  * by inserting perm nodes, if necessary.
40  * @param phi The phi node to adjust operands for
41  */
42 static void adjust_arguments(be_main_session_env_t *session, be_chordal_env_t *chordal_env, const ir_node *phi) {
43         int i, max;
44         ir_node *arg, *perm, *proj;
45         const arch_register_t *phi_reg, *arg_reg, *proj_reg;
46         const ir_edge_t *edge;
47         set *b2p;
48         block2perm_t find, *found;
49
50         assert(is_Phi(phi) && "Can only handle phi-destruction :)");
51
52         b2p = chordal_env->data;
53         phi_reg = get_reg(phi);
54         /* all arguments of the phi */
55         for(i=0, max=get_irn_arity(phi); i<max; ++i) {
56                 arg = get_irn_n(phi, i);
57                 arg_reg = get_reg(arg);
58                 /* if registers don't match ...*/
59                 if (phi_reg != arg_reg) {
60                         /* iff needed insert perm node */
61                         find.block = get_nodes_block(arg);
62                         find.perm = NULL;
63                         found = set_insert(b2p, &find, sizeof(find), HASH_PTR(find.block));
64                         if (!found->perm)
65                                 found->perm = insert_Perm_after(session, chordal_env->cls, sched_last(find.block));
66
67                         /* now we have the perm in the predecessor block */
68                         perm = found->perm;
69                         /* adjust assigned registers for the projs */
70                         foreach_out_edge(perm, edge) {
71                                 proj = get_edge_src_irn(edge);
72                                 proj_reg = get_reg(proj);
73                                 if (proj_reg == arg_reg)
74                                         set_reg(proj, phi_reg);
75                                 else if (proj_reg == phi_reg)
76                                         set_reg(proj, arg_reg);
77                         }
78                 }
79         }
80 }
81
82 static void checker(be_chordal_env_t *chordal_env) {
83         pmap_entry *pme;
84         int i, max;
85
86         /* iterate over all blocks */
87         pmap_foreach(chordal_env->border_heads, pme) {
88                 border_t *curr;
89                 struct list_head *head = pme->value;
90
91                 /* iterate over all ops in the block */
92                 list_for_each_entry_reverse(border_t, curr, head, list)
93                         if (curr->is_def && curr->is_real && is_Phi(curr->irn)) {
94                                 const arch_register_t *phi_reg, *arg_reg;
95                                 phi_reg = get_reg(curr->irn);
96                                 /* iterate over all args of phi */
97                                 for(i=0, max=get_irn_arity(curr->irn); i<max; ++i) {
98                                         arg_reg = get_reg(get_irn_n(curr->irn, i));
99                                         assert(phi_reg == arg_reg && "WTF? You can do it better!?");
100                                 }
101                         }
102         }
103 }
104
105 void be_ssa_destruction(be_main_session_env_t *session, be_chordal_env_t *chordal_env) {
106         pmap_entry *pme;
107         set *b2p;
108
109         b2p = new_set(set_cmp_b2p, 32);
110         chordal_env->data = b2p;
111         /* iterate over all blocks */
112         pmap_foreach(chordal_env->border_heads, pme) {
113                 border_t *curr;
114                 struct list_head *head = pme->value;
115
116                 /* iterate over all ops in the block
117                  * BETTER: phis are the first ops, so stop iteration on first non-phi-op */
118                 list_for_each_entry_reverse(border_t, curr, head, list)
119                         if (curr->is_def && curr->is_real && is_Phi(curr->irn))
120                                 adjust_arguments(session, chordal_env, curr->irn);
121         }
122         del_set(b2p);
123         checker(chordal_env);
124 }