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