2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Common functions for chordal register allocation.
23 * @author Sebastian Hack
25 * @version $Id: bechordal.c 26750 2009-11-27 09:37:43Z bersch $
29 #include "bechordal_common.h"
36 #include "bechordal.h"
37 #include "bechordal_t.h"
41 #include "bestatevent.h"
48 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
50 /* Make a fourcc for border checking. */
51 #define BORDER_FOURCC FOURCC('B', 'O', 'R', 'D')
53 int has_reg_class(const be_chordal_env_t *env, const ir_node *irn)
55 return arch_irn_consider_in_reg_alloc(env->cls, irn);
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)
67 b = OALLOC(env->obst, border_t);
69 /* also allocate the def and tie it to the use. */
70 def = OALLOCZ(env->obst, border_t);
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.
79 set_irn_link(irn, def);
81 DEBUG_ONLY(b->magic = BORDER_FOURCC);
82 DEBUG_ONLY(def->magic = BORDER_FOURCC);
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.
89 b = (border_t*)get_irn_link(irn);
91 DEBUG_ONLY(assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered"));
94 b->pressure = pressure;
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));
106 void create_borders(ir_node *block, void *env_ptr)
108 /* Convenience macro for a def */
109 #define border_def(irn, step, real) \
110 border_add(env, head, irn, step, pressure--, 1, real)
112 /* Convenience macro for a use */
113 #define border_use(irn, step, real) \
114 border_add(env, head, irn, step, ++pressure, 0, real)
116 be_chordal_env_t *env = (be_chordal_env_t*)env_ptr;
117 bitset_t *live = bitset_malloc(get_irg_last_idx(env->irg));
119 be_lv_t *lv = be_get_irg_liveness(env->irg);
124 unsigned pressure = 0;
125 struct list_head *head;
127 bitset_clear_all(live);
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);
136 * Make final uses of all values live out of the block.
137 * They are necessary to build up real intervals.
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);
150 * Determine the last uses of a value inside the block, since they are
151 * relevant for the interval borders.
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));
157 if (get_irn_mode(irn) == mode_T) {
158 const ir_edge_t *edge;
160 foreach_out_edge(irn, edge) {
161 ir_node *proj = get_edge_src_irn(edge);
164 * If the node defines some value, which can put into a
165 * register of the current class, make a border for it.
167 if (has_reg_class(env, proj)) {
168 int nr = get_irn_idx(proj);
170 bitset_clear(live, nr);
171 border_def(proj, step, 1);
176 * If the node defines some value, which can put into a
177 * register of the current class, make a border for it.
179 if (has_reg_class(env, irn)) {
180 int nr = get_irn_idx(irn);
182 bitset_clear(live, nr);
183 border_def(irn, step, 1);
188 * If the node is no phi node we can examine the uses.
191 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
192 ir_node *op = get_irn_n(irn, i);
194 if (has_reg_class(env, op)) {
195 int nr = get_irn_idx(op);
196 const char *msg = "-";
198 if (!bitset_is_set(live, nr)) {
199 border_use(op, step, 1);
200 bitset_set(live, nr);
204 DBG((dbg, LEVEL_4, "\t\t%s pos: %d, use: %+F\n", msg, i, op));
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);
221 be_insn_t *chordal_scan_insn(be_chordal_env_t *env, ir_node *irn)
225 ie.allocatable_regs = env->allocatable_regs;
228 return be_scan_insn(&ie, irn);
231 ir_node *pre_process_constraints(be_chordal_env_t *env, be_insn_t **the_insn)
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;
239 assert(insn->has_constraints && "only do this for constrained nodes");
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
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);
254 * Make the Perm, recompute liveness and re-scan the insn since the
255 * in operands are now the Projs of the Perm.
257 perm = insert_Perm_after(env->irg, env->cls, sched_prev(insn->irn));
259 /* Registers are propagated by insert_Perm_after(). Clean them here! */
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);
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.
274 obstack_free(env->obst, insn);
275 *the_insn = insn = chordal_scan_insn(env, insn->irn);
278 * Copy the input constraints of the insn to the Perm as output
279 * constraints. Succeeding phases (coalescing) will need that.
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;
285 * Note that the predecessor must not be a Proj of the Perm,
286 * since ignore-nodes are not Perm'ed.
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);
296 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_common);
297 void be_init_chordal_common(void)
299 FIRM_DBG_REGISTER(dbg, "firm.be.chordal_common");