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"
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 static inline border_t *border_add(be_chordal_env_t *env, struct list_head *head,
54 ir_node *irn, unsigned step, unsigned pressure,
55 unsigned is_def, unsigned is_real)
62 b = OALLOC(&env->obst, border_t);
64 /* also allocate the def and tie it to the use. */
65 def = OALLOCZ(&env->obst, border_t);
70 * Set the link field of the irn to the def.
71 * This strongly relies on the fact, that the use is always
72 * made before the def.
74 set_irn_link(irn, def);
76 DEBUG_ONLY(b->magic = BORDER_FOURCC;)
77 DEBUG_ONLY(def->magic = BORDER_FOURCC;)
80 * If the def is encountered, the use was made and so was the
81 * the def node (see the code above). It was placed into the
82 * link field of the irn, so we can get it there.
84 b = (border_t*)get_irn_link(irn);
86 DEBUG_ONLY(assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");)
89 b->pressure = pressure;
94 list_add_tail(&b->list, head);
95 DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n", is_def ? "def" : "use", irn, step));
101 void create_borders(ir_node *block, void *env_ptr)
103 /* Convenience macro for a def */
104 #define border_def(irn, step, real) \
105 border_add(env, head, irn, step, pressure--, 1, real)
107 /* Convenience macro for a use */
108 #define border_use(irn, step, real) \
109 border_add(env, head, irn, step, ++pressure, 0, real)
111 be_chordal_env_t *env = (be_chordal_env_t*)env_ptr;
112 bitset_t *live = bitset_malloc(get_irg_last_idx(env->irg));
113 be_lv_t *lv = be_get_irg_liveness(env->irg);
116 unsigned pressure = 0;
117 struct list_head *head;
119 bitset_clear_all(live);
121 /* Set up the border list in the block info */
122 head = OALLOC(&env->obst, struct list_head);
123 INIT_LIST_HEAD(head);
124 assert(pmap_get(struct list_head, env->border_heads, block) == NULL);
125 pmap_insert(env->border_heads, block, head);
128 * Make final uses of all values live out of the block.
129 * They are necessary to build up real intervals.
131 be_lv_foreach_cls(lv, block, be_lv_state_end, env->cls, irn) {
132 DB((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_idx(irn)));
133 bitset_set(live, get_irn_idx(irn));
134 border_use(irn, step, 0);
139 * Determine the last uses of a value inside the block, since they are
140 * relevant for the interval borders.
142 sched_foreach_reverse(block, irn) {
143 DB((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
144 DB((dbg, LEVEL_2, "\tlive: %B\n", live));
146 be_foreach_definition(irn, env->cls, def, req,
148 * If the node defines some value, which can put into a
149 * register of the current class, make a border for it.
151 unsigned idx = get_irn_idx(def);
152 bitset_clear(live, idx);
153 border_def(def, step, 1);
157 * If the node is no phi node we can examine the uses.
160 be_foreach_use(irn, env->cls, in_req_, op, op_req_,
161 unsigned idx = get_irn_idx(op);
162 const char *msg = "-";
164 if (!bitset_is_set(live, idx)) {
165 border_use(op, step, 1);
166 bitset_set(live, idx);
170 DB((dbg, LEVEL_4, "\t\t%s pos: %d, use: %+F\n", msg, i_, op));
176 bitset_foreach(live, elm) {
177 ir_node *irn = get_idx_irn(env->irg, elm);
178 if (be_is_live_in(lv, block, irn))
179 border_def(irn, step, 0);
185 ir_node *pre_process_constraints(be_chordal_env_t *env, be_insn_t **the_insn)
187 be_insn_t *insn = *the_insn;
190 * Make the Perm, recompute liveness and re-scan the insn since the
191 * in operands are now the Projs of the Perm.
193 ir_node *const irn = insn->irn;
194 ir_node *const perm = insert_Perm_before(env->irg, env->cls, irn);
196 /* Registers are propagated by insert_Perm_before(). Clean them here! */
200 stat_ev_int("constr_perm", get_irn_arity(perm));
203 * We also have to re-build the insn since the input operands are now the Projs of
204 * the Perm. Recomputing liveness is also a good idea if a Perm is inserted, since
205 * the live sets may change.
207 obstack_free(&env->obst, insn);
208 *the_insn = insn = be_scan_insn(env, irn);
210 /* Copy the input constraints of the irn to the Perm as output
211 * constraints. Succeeding phases (coalescing) will need that. */
212 for (int i = 0, n = get_irn_arity(irn); i != n; ++i) {
213 ir_node *const proj = get_irn_n(irn, i);
214 /* Note that the predecessor is not necessarily a Proj of the Perm,
215 * since ignore-nodes are not Perm'ed. */
216 if (!is_Proj(proj) || get_Proj_pred(proj) != perm)
218 /* FIXME: Only setting the constraints, when the register requirement is
219 * limited, is a hack. It will break when multiple differently constrained
220 * inputs use the same value. */
221 arch_register_req_t const *const req = arch_get_irn_register_req_in(irn, i);
222 if (!arch_register_req_is(req, limited))
224 be_set_constr_out(perm, get_Proj_proj(proj), req);
230 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_common)
231 void be_init_chordal_common(void)
233 FIRM_DBG_REGISTER(dbg, "firm.be.chordal_common");