2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Common functions for chordal register allocation.
9 * @author Sebastian Hack
14 #include "bechordal_common.h"
21 #include "bechordal.h"
22 #include "bechordal_t.h"
34 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
36 /* Make a fourcc for border checking. */
37 #define BORDER_FOURCC FOURCC('B', 'O', 'R', 'D')
39 static inline border_t *border_add(be_chordal_env_t *env, struct list_head *head,
40 ir_node *irn, unsigned step, unsigned pressure,
41 unsigned is_def, unsigned is_real)
48 b = OALLOC(&env->obst, border_t);
50 /* also allocate the def and tie it to the use. */
51 def = OALLOCZ(&env->obst, border_t);
56 * Set the link field of the irn to the def.
57 * This strongly relies on the fact, that the use is always
58 * made before the def.
60 set_irn_link(irn, def);
62 DEBUG_ONLY(b->magic = BORDER_FOURCC;)
63 DEBUG_ONLY(def->magic = BORDER_FOURCC;)
66 * If the def is encountered, the use was made and so was the
67 * the def node (see the code above). It was placed into the
68 * link field of the irn, so we can get it there.
70 b = (border_t*)get_irn_link(irn);
72 DEBUG_ONLY(assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");)
75 b->pressure = pressure;
80 list_add_tail(&b->list, head);
81 DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n", is_def ? "def" : "use", irn, step));
87 void create_borders(ir_node *block, void *env_ptr)
89 /* Convenience macro for a def */
90 #define border_def(irn, step, real) \
91 border_add(env, head, irn, step, pressure--, 1, real)
93 /* Convenience macro for a use */
94 #define border_use(irn, step, real) \
95 border_add(env, head, irn, step, ++pressure, 0, real)
97 be_chordal_env_t *env = (be_chordal_env_t*)env_ptr;
98 bitset_t *live = bitset_malloc(get_irg_last_idx(env->irg));
99 be_lv_t *lv = be_get_irg_liveness(env->irg);
102 unsigned pressure = 0;
103 struct list_head *head;
105 bitset_clear_all(live);
107 /* Set up the border list in the block info */
108 head = OALLOC(&env->obst, struct list_head);
109 INIT_LIST_HEAD(head);
110 assert(pmap_get(struct list_head, env->border_heads, block) == NULL);
111 pmap_insert(env->border_heads, block, head);
114 * Make final uses of all values live out of the block.
115 * They are necessary to build up real intervals.
117 be_lv_foreach_cls(lv, block, be_lv_state_end, env->cls, irn) {
118 DB((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_idx(irn)));
119 bitset_set(live, get_irn_idx(irn));
120 border_use(irn, step, 0);
125 * Determine the last uses of a value inside the block, since they are
126 * relevant for the interval borders.
128 sched_foreach_reverse(block, irn) {
129 DB((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
130 DB((dbg, LEVEL_2, "\tlive: %B\n", live));
132 be_foreach_definition(irn, env->cls, def, req,
134 * If the node defines some value, which can put into a
135 * register of the current class, make a border for it.
137 unsigned idx = get_irn_idx(def);
138 bitset_clear(live, idx);
139 border_def(def, step, 1);
143 * If the node is no phi node we can examine the uses.
146 be_foreach_use(irn, env->cls, in_req_, op, op_req_,
147 unsigned idx = get_irn_idx(op);
148 const char *msg = "-";
150 if (!bitset_is_set(live, idx)) {
151 border_use(op, step, 1);
152 bitset_set(live, idx);
156 DB((dbg, LEVEL_4, "\t\t%s pos: %d, use: %+F\n", msg, i_, op));
162 bitset_foreach(live, elm) {
163 ir_node *irn = get_idx_irn(env->irg, elm);
164 if (be_is_live_in(lv, block, irn))
165 border_def(irn, step, 0);
171 ir_node *pre_process_constraints(be_chordal_env_t *env, be_insn_t **the_insn)
173 be_insn_t *insn = *the_insn;
176 * Make the Perm, recompute liveness and re-scan the insn since the
177 * in operands are now the Projs of the Perm.
179 ir_node *const irn = insn->irn;
180 ir_node *const perm = insert_Perm_before(env->irg, env->cls, irn);
182 /* Registers are propagated by insert_Perm_before(). Clean them here! */
186 stat_ev_int("constr_perm", get_irn_arity(perm));
189 * We also have to re-build the insn since the input operands are now the Projs of
190 * the Perm. Recomputing liveness is also a good idea if a Perm is inserted, since
191 * the live sets may change.
193 obstack_free(&env->obst, insn);
194 *the_insn = insn = be_scan_insn(env, irn);
196 /* Copy the input constraints of the irn to the Perm as output
197 * constraints. Succeeding phases (coalescing) will need that. */
198 for (int i = 0, n = get_irn_arity(irn); i != n; ++i) {
199 ir_node *const proj = get_irn_n(irn, i);
200 /* Note that the predecessor is not necessarily a Proj of the Perm,
201 * since ignore-nodes are not Perm'ed. */
202 if (!is_Proj(proj) || get_Proj_pred(proj) != perm)
204 /* FIXME: Only setting the constraints, when the register requirement is
205 * limited, is a hack. It will break when multiple differently constrained
206 * inputs use the same value. */
207 arch_register_req_t const *const req = arch_get_irn_register_req_in(irn, i);
208 if (!arch_register_req_is(req, limited))
210 be_set_constr_out(perm, get_Proj_proj(proj), req);
216 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_common)
217 void be_init_chordal_common(void)
219 FIRM_DBG_REGISTER(dbg, "firm.be.chordal_common");