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