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 *const env, struct list_head *const head, ir_node *const irn, unsigned const step, unsigned const is_def, unsigned const is_real)
46 b = OALLOC(&env->obst, border_t);
48 /* also allocate the def and tie it to the use. */
49 def = OALLOCZ(&env->obst, border_t);
54 * Set the link field of the irn to the def.
55 * This strongly relies on the fact, that the use is always
56 * made before the def.
58 set_irn_link(irn, def);
60 DEBUG_ONLY(b->magic = BORDER_FOURCC;)
61 DEBUG_ONLY(def->magic = BORDER_FOURCC;)
64 * If the def is encountered, the use was made and so was the
65 * the def node (see the code above). It was placed into the
66 * link field of the irn, so we can get it there.
68 b = (border_t*)get_irn_link(irn);
70 DEBUG_ONLY(assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");)
77 list_add_tail(&b->list, head);
78 DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n", is_def ? "def" : "use", irn, step));
84 void create_borders(ir_node *block, void *env_ptr)
86 /* Convenience macro for a def */
87 #define border_def(irn, step, real) \
88 border_add(env, head, irn, step, 1, real)
90 /* Convenience macro for a use */
91 #define border_use(irn, step, real) \
92 border_add(env, head, irn, step, 0, real)
94 be_chordal_env_t *const env = (be_chordal_env_t*)env_ptr;
97 struct list_head *head;
99 /* Set up the border list in the block info */
100 head = OALLOC(&env->obst, struct list_head);
101 INIT_LIST_HEAD(head);
102 assert(pmap_get(struct list_head, env->border_heads, block) == NULL);
103 pmap_insert(env->border_heads, block, head);
106 ir_nodeset_init(&live);
108 be_lv_t *const lv = be_get_irg_liveness(env->irg);
111 * Make final uses of all values live out of the block.
112 * They are necessary to build up real intervals.
114 be_lv_foreach_cls(lv, block, be_lv_state_end, env->cls, irn) {
115 DB((dbg, LEVEL_3, "\tMaking live: %+F\n", irn));
116 ir_nodeset_insert(&live, irn);
117 border_use(irn, step, 0);
122 * Determine the last uses of a value inside the block, since they are
123 * relevant for the interval borders.
125 sched_foreach_reverse(block, irn) {
129 DB((dbg, LEVEL_1, "\tinsn: %+F\n", irn));
131 be_foreach_definition(irn, env->cls, def, req,
133 * If the node defines some value, which can put into a
134 * register of the current class, make a border for it.
136 ir_nodeset_remove(&live, def);
137 border_def(def, step, 1);
140 /* Examine the uses. */
141 be_foreach_use(irn, env->cls, in_req_, op, op_req_,
142 const char *msg = "-";
144 if (ir_nodeset_insert(&live, op)) {
145 border_use(op, step, 1);
149 DB((dbg, LEVEL_4, "\t\t%s pos: %d, use: %+F\n", msg, i_, op));
154 foreach_ir_nodeset(&live, irn, iter) {
155 assert(be_is_live_in(lv, block, irn) ^ (is_Phi(irn) && get_nodes_block(irn) == block));
156 border_def(irn, step, get_nodes_block(irn) == block);
159 ir_nodeset_destroy(&live);
162 ir_node *pre_process_constraints(be_chordal_env_t *env, be_insn_t **the_insn)
164 be_insn_t *insn = *the_insn;
167 * Make the Perm, recompute liveness and re-scan the insn since the
168 * in operands are now the Projs of the Perm.
170 ir_node *const irn = insn->irn;
171 ir_node *const perm = insert_Perm_before(env->irg, env->cls, irn);
173 /* Registers are propagated by insert_Perm_before(). Clean them here! */
177 stat_ev_int("constr_perm", get_irn_arity(perm));
180 * We also have to re-build the insn since the input operands are now the Projs of
181 * the Perm. Recomputing liveness is also a good idea if a Perm is inserted, since
182 * the live sets may change.
184 obstack_free(&env->obst, insn);
185 *the_insn = insn = be_scan_insn(env, irn);
187 /* Copy the input constraints of the irn to the Perm as output
188 * constraints. Succeeding phases (coalescing) will need that. */
189 for (int i = 0, n = get_irn_arity(irn); i != n; ++i) {
190 ir_node *const proj = get_irn_n(irn, i);
191 /* Note that the predecessor is not necessarily a Proj of the Perm,
192 * since ignore-nodes are not Perm'ed. */
193 if (!is_Proj(proj) || get_Proj_pred(proj) != perm)
195 /* FIXME: Only setting the constraints, when the register requirement is
196 * limited, is a hack. It will break when multiple differently constrained
197 * inputs use the same value. */
198 arch_register_req_t const *const req = arch_get_irn_register_req_in(irn, i);
199 if (!arch_register_req_is(req, limited))
201 be_set_constr_out(perm, get_Proj_proj(proj), req);
207 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_common)
208 void be_init_chordal_common(void)
210 FIRM_DBG_REGISTER(dbg, "firm.be.chordal_common");