be: Change insert_Perm_after() to insert_Perm_before().
[libfirm] / ir / be / bechordal_common.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Common functions for chordal register allocation.
23  * @author      Sebastian Hack
24  * @date        08.12.2004
25  */
26 #include "config.h"
27
28 #include "bechordal_common.h"
29
30 #include "debug.h"
31
32 #include "iredges.h"
33 #include "bitset.h"
34
35 #include "bechordal.h"
36 #include "bechordal_t.h"
37 #include "beirgmod.h"
38 #include "beinsn_t.h"
39 #include "besched.h"
40 #include "statev_t.h"
41 #include "benode.h"
42 #include "bemodule.h"
43 #include "belive.h"
44 #include "belive_t.h"
45 #include "fourcc.h"
46
47 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
48
49 /* Make a fourcc for border checking. */
50 #define BORDER_FOURCC   FOURCC('B', 'O', 'R', 'D')
51
52 static inline border_t *border_add(be_chordal_env_t *env, struct list_head *head,
53                         ir_node *irn, unsigned step, unsigned pressure,
54                         unsigned is_def, unsigned is_real)
55 {
56         border_t *b;
57
58         if (!is_def) {
59                 border_t *def;
60
61                 b = OALLOC(env->obst, border_t);
62
63                 /* also allocate the def and tie it to the use. */
64                 def = OALLOCZ(env->obst, border_t);
65                 b->other_end = def;
66                 def->other_end = b;
67
68                 /*
69                  * Set the link field of the irn to the def.
70                  * This strongly relies on the fact, that the use is always
71                  * made before the def.
72                  */
73                 set_irn_link(irn, def);
74
75                 DEBUG_ONLY(b->magic = BORDER_FOURCC;)
76                 DEBUG_ONLY(def->magic = BORDER_FOURCC;)
77         } else {
78                 /*
79                  * If the def is encountered, the use was made and so was the
80                  * the def node (see the code above). It was placed into the
81                  * link field of the irn, so we can get it there.
82                  */
83                 b = (border_t*)get_irn_link(irn);
84
85                 DEBUG_ONLY(assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");)
86         }
87
88         b->pressure = pressure;
89         b->is_def = is_def;
90         b->is_real = is_real;
91         b->irn = irn;
92         b->step = step;
93         list_add_tail(&b->list, head);
94         DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n", is_def ? "def" : "use", irn, step));
95
96
97         return b;
98 }
99
100 void create_borders(ir_node *block, void *env_ptr)
101 {
102 /* Convenience macro for a def */
103 #define border_def(irn, step, real) \
104         border_add(env, head, irn, step, pressure--, 1, real)
105
106 /* Convenience macro for a use */
107 #define border_use(irn, step, real) \
108         border_add(env, head, irn, step, ++pressure, 0, real)
109
110         be_chordal_env_t *env  = (be_chordal_env_t*)env_ptr;
111         bitset_t         *live = bitset_malloc(get_irg_last_idx(env->irg));
112         be_lv_t          *lv   = be_get_irg_liveness(env->irg);
113
114         unsigned step = 0;
115         unsigned pressure = 0;
116         struct list_head *head;
117
118         bitset_clear_all(live);
119
120         /* Set up the border list in the block info */
121         head = OALLOC(env->obst, struct list_head);
122         INIT_LIST_HEAD(head);
123         assert(pmap_get(struct list_head, env->border_heads, block) == NULL);
124         pmap_insert(env->border_heads, block, head);
125
126         /*
127          * Make final uses of all values live out of the block.
128          * They are necessary to build up real intervals.
129          */
130         be_lv_foreach(lv, block, be_lv_state_end, irn) {
131                 if (has_reg_class(env, irn)) {
132                         DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_idx(irn)));
133                         bitset_set(live, get_irn_idx(irn));
134                         border_use(irn, step, 0);
135                 }
136         }
137         ++step;
138
139         /*
140          * Determine the last uses of a value inside the block, since they are
141          * relevant for the interval borders.
142          */
143         sched_foreach_reverse(block, irn) {
144                 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
145                 DBG((dbg, LEVEL_2, "\tlive: %B\n", live));
146
147                 if (get_irn_mode(irn) == mode_T) {
148                         foreach_out_edge(irn, edge) {
149                                 ir_node *proj = get_edge_src_irn(edge);
150
151                                 /*
152                                  * If the node defines some value, which can put into a
153                                  * register of the current class, make a border for it.
154                                  */
155                                 if (has_reg_class(env, proj)) {
156                                         int nr = get_irn_idx(proj);
157
158                                         bitset_clear(live, nr);
159                                         border_def(proj, step, 1);
160                                 }
161                         }
162                 } else {
163                         /*
164                          * If the node defines some value, which can put into a
165                          * register of the current class, make a border for it.
166                          */
167                         if (has_reg_class(env, irn)) {
168                                 int nr = get_irn_idx(irn);
169
170                                 bitset_clear(live, nr);
171                                 border_def(irn, step, 1);
172                         }
173                 }
174
175                 /*
176                  * If the node is no phi node we can examine the uses.
177                  */
178                 if (!is_Phi(irn)) {
179                         for (int i = 0, n = get_irn_arity(irn); i < n; ++i) {
180                                 ir_node *op = get_irn_n(irn, i);
181
182                                 if (has_reg_class(env, op)) {
183                                         int nr = get_irn_idx(op);
184                                         const char *msg = "-";
185
186                                         if (!bitset_is_set(live, nr)) {
187                                                 border_use(op, step, 1);
188                                                 bitset_set(live, nr);
189                                                 msg = "X";
190                                         }
191
192                                         DBG((dbg, LEVEL_4, "\t\t%s pos: %d, use: %+F\n", msg, i, op));
193                                 }
194                         }
195                 }
196                 ++step;
197         }
198
199         bitset_foreach(live, elm) {
200                 ir_node *irn = get_idx_irn(env->irg, elm);
201                 if (be_is_live_in(lv, block, irn))
202                         border_def(irn, step, 0);
203         }
204
205         bitset_free(live);
206 }
207
208 ir_node *pre_process_constraints(be_chordal_env_t *env, be_insn_t **the_insn)
209 {
210         be_insn_t *insn       = *the_insn;
211         ir_node   *perm       = NULL;
212         bitset_t  *out_constr = bitset_alloca(env->cls->n_regs);
213         int        i;
214
215         assert(insn->has_constraints && "only do this for constrained nodes");
216
217         /*
218          * Collect all registers that occur in output constraints.
219          * This is necessary, since if the insn has one of these as an input constraint
220          * and the corresponding operand interferes with the insn, the operand must
221          * be copied.
222          */
223         for (i = 0; i < insn->use_start; ++i) {
224                 be_operand_t *op = &insn->ops[i];
225                 if (op->has_constraints)
226                         bitset_or(out_constr, op->regs);
227         }
228
229         /*
230          * Make the Perm, recompute liveness and re-scan the insn since the
231          * in operands are now the Projs of the Perm.
232          */
233         perm = insert_Perm_before(env->irg, env->cls, insn->irn);
234
235         /* Registers are propagated by insert_Perm_before(). Clean them here! */
236         if (perm == NULL)
237                 return NULL;
238
239         stat_ev_int("constr_perm", get_irn_arity(perm));
240         foreach_out_edge(perm, edge) {
241                 ir_node *proj = get_edge_src_irn(edge);
242                 arch_set_irn_register(proj, NULL);
243         }
244
245         /*
246          * We also have to re-build the insn since the input operands are now the Projs of
247          * the Perm. Recomputing liveness is also a good idea if a Perm is inserted, since
248          * the live sets may change.
249          */
250         obstack_free(env->obst, insn);
251         *the_insn = insn = be_scan_insn(env, insn->irn);
252
253         /*
254          * Copy the input constraints of the insn to the Perm as output
255          * constraints. Succeeding phases (coalescing) will need that.
256          */
257         for (i = insn->use_start; i < insn->n_ops; ++i) {
258                 be_operand_t *op = &insn->ops[i];
259                 ir_node *proj = op->carrier;
260                 /*
261                  * Note that the predecessor must not be a Proj of the Perm,
262                  * since ignore-nodes are not Perm'ed.
263                  */
264                 if (op->has_constraints &&  is_Proj(proj) && get_Proj_pred(proj) == perm) {
265                         be_set_constr_out(perm, get_Proj_proj(proj), op->req);
266                 }
267         }
268
269         return perm;
270 }
271
272 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_common)
273 void be_init_chordal_common(void)
274 {
275         FIRM_DBG_REGISTER(dbg, "firm.be.chordal_common");
276 }