merge kaps
[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  * @version     $Id: bechordal.c 26750 2009-11-27 09:37:43Z bersch $
26  */
27 #include "config.h"
28
29 #include "bechordal_common.h"
30
31 #include "debug.h"
32
33 #include "iredges.h"
34 #include "bitset.h"
35
36 #include "bechordal.h"
37 #include "bechordal_t.h"
38 #include "beirgmod.h"
39 #include "beinsn_t.h"
40 #include "besched.h"
41 #include "bestatevent.h"
42 #include "benode.h"
43 #include "bemodule.h"
44 #include "belive.h"
45 #include "belive_t.h"
46 #include "fourcc.h"
47
48 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
49
50 /* Make a fourcc for border checking. */
51 #define BORDER_FOURCC   FOURCC('B', 'O', 'R', 'D')
52
53 int has_reg_class(const be_chordal_env_t *env, const ir_node *irn)
54 {
55         return arch_irn_consider_in_reg_alloc(env->cls, irn);
56 }
57
58 static inline border_t *border_add(be_chordal_env_t *env, struct list_head *head,
59                         ir_node *irn, unsigned step, unsigned pressure,
60                         unsigned is_def, unsigned is_real)
61 {
62         border_t *b;
63
64         if (!is_def) {
65                 border_t *def;
66
67                 b = OALLOC(env->obst, border_t);
68
69                 /* also allocate the def and tie it to the use. */
70                 def = OALLOCZ(env->obst, border_t);
71                 b->other_end = def;
72                 def->other_end = b;
73
74                 /*
75                  * Set the link field of the irn to the def.
76                  * This strongly relies on the fact, that the use is always
77                  * made before the def.
78                  */
79                 set_irn_link(irn, def);
80
81                 DEBUG_ONLY(b->magic = BORDER_FOURCC);
82                 DEBUG_ONLY(def->magic = BORDER_FOURCC);
83         } else {
84                 /*
85                  * If the def is encountered, the use was made and so was the
86                  * the def node (see the code above). It was placed into the
87                  * link field of the irn, so we can get it there.
88                  */
89                 b = (border_t*)get_irn_link(irn);
90
91                 DEBUG_ONLY(assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered"));
92         }
93
94         b->pressure = pressure;
95         b->is_def = is_def;
96         b->is_real = is_real;
97         b->irn = irn;
98         b->step = step;
99         list_add_tail(&b->list, head);
100         DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n", is_def ? "def" : "use", irn, step));
101
102
103         return b;
104 }
105
106 void create_borders(ir_node *block, void *env_ptr)
107 {
108 /* Convenience macro for a def */
109 #define border_def(irn, step, real) \
110         border_add(env, head, irn, step, pressure--, 1, real)
111
112 /* Convenience macro for a use */
113 #define border_use(irn, step, real) \
114         border_add(env, head, irn, step, ++pressure, 0, real)
115
116         be_chordal_env_t *env             = (be_chordal_env_t*)env_ptr;
117         bitset_t *live                    = bitset_malloc(get_irg_last_idx(env->irg));
118         ir_node *irn;
119         be_lv_t *lv                       = be_get_irg_liveness(env->irg);
120
121         int i, n;
122         size_t elm;
123         unsigned step = 0;
124         unsigned pressure = 0;
125         struct list_head *head;
126
127         bitset_clear_all(live);
128
129         /* Set up the border list in the block info */
130         head = OALLOC(env->obst, struct list_head);
131         INIT_LIST_HEAD(head);
132         assert(pmap_get(env->border_heads, block) == NULL);
133         pmap_insert(env->border_heads, block, head);
134
135         /*
136          * Make final uses of all values live out of the block.
137          * They are necessary to build up real intervals.
138          */
139         be_lv_foreach(lv, block, be_lv_state_end, i) {
140                 ir_node *irn = be_lv_get_irn(lv, block, i);
141                 if (has_reg_class(env, irn)) {
142                         DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_idx(irn)));
143                         bitset_set(live, get_irn_idx(irn));
144                         border_use(irn, step, 0);
145                 }
146         }
147         ++step;
148
149         /*
150          * Determine the last uses of a value inside the block, since they are
151          * relevant for the interval borders.
152          */
153         sched_foreach_reverse(block, irn) {
154                 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
155                 DBG((dbg, LEVEL_2, "\tlive: %B\n", live));
156
157                 if (get_irn_mode(irn) == mode_T) {
158                         const ir_edge_t *edge;
159
160                         foreach_out_edge(irn, edge) {
161                                 ir_node *proj = get_edge_src_irn(edge);
162
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, proj)) {
168                                         int nr = get_irn_idx(proj);
169
170                                         bitset_clear(live, nr);
171                                         border_def(proj, step, 1);
172                                 }
173                         }
174                 } else {
175                         /*
176                          * If the node defines some value, which can put into a
177                          * register of the current class, make a border for it.
178                          */
179                         if (has_reg_class(env, irn)) {
180                                 int nr = get_irn_idx(irn);
181
182                                 bitset_clear(live, nr);
183                                 border_def(irn, step, 1);
184                         }
185                 }
186
187                 /*
188                  * If the node is no phi node we can examine the uses.
189                  */
190                 if (!is_Phi(irn)) {
191                         for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
192                                 ir_node *op = get_irn_n(irn, i);
193
194                                 if (has_reg_class(env, op)) {
195                                         int nr = get_irn_idx(op);
196                                         const char *msg = "-";
197
198                                         if (!bitset_is_set(live, nr)) {
199                                                 border_use(op, step, 1);
200                                                 bitset_set(live, nr);
201                                                 msg = "X";
202                                         }
203
204                                         DBG((dbg, LEVEL_4, "\t\t%s pos: %d, use: %+F\n", msg, i, op));
205                                 }
206                         }
207                 }
208                 ++step;
209         }
210
211         bitset_foreach(live, elm) {
212                 ir_node *irn = get_idx_irn(env->irg, elm);
213                 if (be_is_live_in(lv, block, irn))
214                         border_def(irn, step, 0);
215         }
216
217         bitset_free(live);
218 }
219
220
221 be_insn_t *chordal_scan_insn(be_chordal_env_t *env, ir_node *irn)
222 {
223         be_insn_env_t ie;
224
225         ie.allocatable_regs = env->allocatable_regs;
226         ie.obst             = env->obst;
227         ie.cls              = env->cls;
228         return be_scan_insn(&ie, irn);
229 }
230
231 ir_node *pre_process_constraints(be_chordal_env_t *env, be_insn_t **the_insn)
232 {
233         be_insn_t *insn             = *the_insn;
234         ir_node *perm               = NULL;
235         bitset_t *out_constr        = bitset_alloca(env->cls->n_regs);
236         const ir_edge_t *edge;
237         int i;
238
239         assert(insn->has_constraints && "only do this for constrained nodes");
240
241         /*
242          * Collect all registers that occur in output constraints.
243          * This is necessary, since if the insn has one of these as an input constraint
244          * and the corresponding operand interferes with the insn, the operand must
245          * be copied.
246          */
247         for (i = 0; i < insn->use_start; ++i) {
248                 be_operand_t *op = &insn->ops[i];
249                 if (op->has_constraints)
250                         bitset_or(out_constr, op->regs);
251         }
252
253         /*
254          * Make the Perm, recompute liveness and re-scan the insn since the
255          * in operands are now the Projs of the Perm.
256          */
257         perm = insert_Perm_after(env->irg, env->cls, sched_prev(insn->irn));
258
259         /* Registers are propagated by insert_Perm_after(). Clean them here! */
260         if (perm == NULL)
261                 return NULL;
262
263         be_stat_ev("constr_perm", get_irn_arity(perm));
264         foreach_out_edge(perm, edge) {
265                 ir_node *proj = get_edge_src_irn(edge);
266                 arch_set_irn_register(proj, NULL);
267         }
268
269         /*
270          * We also have to re-build the insn since the input operands are now the Projs of
271          * the Perm. Recomputing liveness is also a good idea if a Perm is inserted, since
272          * the live sets may change.
273          */
274         obstack_free(env->obst, insn);
275         *the_insn = insn = chordal_scan_insn(env, insn->irn);
276
277         /*
278          * Copy the input constraints of the insn to the Perm as output
279          * constraints. Succeeding phases (coalescing) will need that.
280          */
281         for (i = insn->use_start; i < insn->n_ops; ++i) {
282                 be_operand_t *op = &insn->ops[i];
283                 ir_node *proj = op->carrier;
284                 /*
285                  * Note that the predecessor must not be a Proj of the Perm,
286                  * since ignore-nodes are not Perm'ed.
287                  */
288                 if (op->has_constraints &&  is_Proj(proj) && get_Proj_pred(proj) == perm) {
289                         be_set_constr_out(perm, get_Proj_proj(proj), op->req);
290                 }
291         }
292
293         return perm;
294 }
295
296 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_common);
297 void be_init_chordal_common(void)
298 {
299         FIRM_DBG_REGISTER(dbg, "firm.be.chordal_common");
300 }