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
28 #include "bechordal_common.h"
35 #include "bechordal.h"
36 #include "bechordal_t.h"
40 #include "bestatevent.h"
47 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
49 /* Make a fourcc for border checking. */
50 #define BORDER_FOURCC FOURCC('B', 'O', 'R', 'D')
52 int has_reg_class(const be_chordal_env_t *env, const ir_node *irn)
54 return arch_irn_consider_in_reg_alloc(env->cls, irn);
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)
66 b = OALLOC(env->obst, border_t);
68 /* also allocate the def and tie it to the use. */
69 def = OALLOCZ(env->obst, border_t);
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.
78 set_irn_link(irn, def);
80 DEBUG_ONLY(b->magic = BORDER_FOURCC;)
81 DEBUG_ONLY(def->magic = BORDER_FOURCC;)
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.
88 b = (border_t*)get_irn_link(irn);
90 DEBUG_ONLY(assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");)
93 b->pressure = pressure;
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));
105 void create_borders(ir_node *block, void *env_ptr)
107 /* Convenience macro for a def */
108 #define border_def(irn, step, real) \
109 border_add(env, head, irn, step, pressure--, 1, real)
111 /* Convenience macro for a use */
112 #define border_use(irn, step, real) \
113 border_add(env, head, irn, step, ++pressure, 0, real)
115 be_chordal_env_t *env = (be_chordal_env_t*)env_ptr;
116 bitset_t *live = bitset_malloc(get_irg_last_idx(env->irg));
117 be_lv_t *lv = be_get_irg_liveness(env->irg);
120 unsigned pressure = 0;
121 struct list_head *head;
123 bitset_clear_all(live);
125 /* Set up the border list in the block info */
126 head = OALLOC(env->obst, struct list_head);
127 INIT_LIST_HEAD(head);
128 assert(pmap_get(struct list_head, env->border_heads, block) == NULL);
129 pmap_insert(env->border_heads, block, head);
132 * Make final uses of all values live out of the block.
133 * They are necessary to build up real intervals.
135 be_lv_foreach(lv, block, be_lv_state_end, irn) {
136 if (has_reg_class(env, irn)) {
137 DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_idx(irn)));
138 bitset_set(live, get_irn_idx(irn));
139 border_use(irn, step, 0);
145 * Determine the last uses of a value inside the block, since they are
146 * relevant for the interval borders.
148 sched_foreach_reverse(block, irn) {
149 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
150 DBG((dbg, LEVEL_2, "\tlive: %B\n", live));
152 if (get_irn_mode(irn) == mode_T) {
153 foreach_out_edge(irn, edge) {
154 ir_node *proj = get_edge_src_irn(edge);
157 * If the node defines some value, which can put into a
158 * register of the current class, make a border for it.
160 if (has_reg_class(env, proj)) {
161 int nr = get_irn_idx(proj);
163 bitset_clear(live, nr);
164 border_def(proj, step, 1);
169 * If the node defines some value, which can put into a
170 * register of the current class, make a border for it.
172 if (has_reg_class(env, irn)) {
173 int nr = get_irn_idx(irn);
175 bitset_clear(live, nr);
176 border_def(irn, step, 1);
181 * If the node is no phi node we can examine the uses.
184 for (int i = 0, n = get_irn_arity(irn); i < n; ++i) {
185 ir_node *op = get_irn_n(irn, i);
187 if (has_reg_class(env, op)) {
188 int nr = get_irn_idx(op);
189 const char *msg = "-";
191 if (!bitset_is_set(live, nr)) {
192 border_use(op, step, 1);
193 bitset_set(live, nr);
197 DBG((dbg, LEVEL_4, "\t\t%s pos: %d, use: %+F\n", msg, i, op));
204 bitset_foreach(live, elm) {
205 ir_node *irn = get_idx_irn(env->irg, elm);
206 if (be_is_live_in(lv, block, irn))
207 border_def(irn, step, 0);
214 be_insn_t *chordal_scan_insn(be_chordal_env_t *env, ir_node *irn)
218 ie.allocatable_regs = env->allocatable_regs;
221 return be_scan_insn(&ie, irn);
224 ir_node *pre_process_constraints(be_chordal_env_t *env, be_insn_t **the_insn)
226 be_insn_t *insn = *the_insn;
227 ir_node *perm = NULL;
228 bitset_t *out_constr = bitset_alloca(env->cls->n_regs);
231 assert(insn->has_constraints && "only do this for constrained nodes");
234 * Collect all registers that occur in output constraints.
235 * This is necessary, since if the insn has one of these as an input constraint
236 * and the corresponding operand interferes with the insn, the operand must
239 for (i = 0; i < insn->use_start; ++i) {
240 be_operand_t *op = &insn->ops[i];
241 if (op->has_constraints)
242 bitset_or(out_constr, op->regs);
246 * Make the Perm, recompute liveness and re-scan the insn since the
247 * in operands are now the Projs of the Perm.
249 perm = insert_Perm_after(env->irg, env->cls, sched_prev(insn->irn));
251 /* Registers are propagated by insert_Perm_after(). Clean them here! */
255 be_stat_ev("constr_perm", get_irn_arity(perm));
256 foreach_out_edge(perm, edge) {
257 ir_node *proj = get_edge_src_irn(edge);
258 arch_set_irn_register(proj, NULL);
262 * We also have to re-build the insn since the input operands are now the Projs of
263 * the Perm. Recomputing liveness is also a good idea if a Perm is inserted, since
264 * the live sets may change.
266 obstack_free(env->obst, insn);
267 *the_insn = insn = chordal_scan_insn(env, insn->irn);
270 * Copy the input constraints of the insn to the Perm as output
271 * constraints. Succeeding phases (coalescing) will need that.
273 for (i = insn->use_start; i < insn->n_ops; ++i) {
274 be_operand_t *op = &insn->ops[i];
275 ir_node *proj = op->carrier;
277 * Note that the predecessor must not be a Proj of the Perm,
278 * since ignore-nodes are not Perm'ed.
280 if (op->has_constraints && is_Proj(proj) && get_Proj_pred(proj) == perm) {
281 be_set_constr_out(perm, get_Proj_proj(proj), op->req);
288 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_common)
289 void be_init_chordal_common(void)
291 FIRM_DBG_REGISTER(dbg, "firm.be.chordal_common");