4 * Created on: Nov 11, 2009
14 #include "bechordal.h"
15 #include "bechordal_t.h"
19 #include "bestatevent.h"
26 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
28 /* Make a fourcc for border checking. */
29 #define BORDER_FOURCC FOURCC('B', 'O', 'R', 'D')
33 * Check, if an irn is of the register class currently under processing.
34 * @param env The chordal environment.
35 * @param irn The node.
36 * @return 1, if the node is of that register class, 0 if not.
38 inline int has_reg_class(const be_chordal_env_t *env, const ir_node *irn)
40 return arch_irn_consider_in_reg_alloc(env->cls, irn);
44 * Add an interval border to the list of a block's list
46 * @note You always have to create the use before the def.
47 * @param env The environment.
48 * @param head The list head to enqueue the borders.
49 * @param irn The node (value) the border belongs to.
50 * @param pressure The pressure at this point in time.
51 * @param step A time step for the border.
52 * @param is_def Is the border a use or a def.
53 * @return The created border.
55 static inline border_t *border_add(be_chordal_env_t *env, struct list_head *head,
56 ir_node *irn, unsigned step, unsigned pressure,
57 unsigned is_def, unsigned is_real)
64 b = OALLOC(env->obst, border_t);
66 /* also allocate the def and tie it to the use. */
67 def = OALLOCZ(env->obst, border_t);
72 * Set the link field of the irn to the def.
73 * This strongly relies on the fact, that the use is always
74 * made before the def.
76 set_irn_link(irn, def);
78 DEBUG_ONLY(b->magic = BORDER_FOURCC);
79 DEBUG_ONLY(def->magic = BORDER_FOURCC);
82 * If the def is encountered, the use was made and so was the
83 * the def node (see the code above). It was placed into the
84 * link field of the irn, so we can get it there.
86 b = get_irn_link(irn);
88 DEBUG_ONLY(assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered"));
91 b->pressure = pressure;
96 list_add_tail(&b->list, head);
97 DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n", is_def ? "def" : "use", irn, step));
104 * Annotate the register pressure to the nodes and compute
105 * the liveness intervals.
106 * @param block The block to do it for.
107 * @param env_ptr The environment.
109 void pressure(ir_node *block, void *env_ptr)
111 /* Convenience macro for a def */
112 #define border_def(irn, step, real) \
113 border_add(env, head, irn, step, pressure--, 1, real)
115 /* Convenience macro for a use */
116 #define border_use(irn, step, real) \
117 border_add(env, head, irn, step, ++pressure, 0, real)
119 be_chordal_env_t *env = env_ptr;
120 bitset_t *live = bitset_malloc(get_irg_last_idx(env->irg));
122 be_lv_t *lv = env->birg->lv;
127 unsigned pressure = 0;
128 struct list_head *head;
130 bitset_clear_all(live);
132 /* Set up the border list in the block info */
133 head = OALLOC(env->obst, struct list_head);
134 INIT_LIST_HEAD(head);
135 assert(pmap_get(env->border_heads, block) == NULL);
136 pmap_insert(env->border_heads, block, head);
139 * Make final uses of all values live out of the block.
140 * They are necessary to build up real intervals.
142 be_lv_foreach(lv, block, be_lv_state_end, i) {
143 ir_node *irn = be_lv_get_irn(lv, block, i);
144 if (has_reg_class(env, irn)) {
145 DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_idx(irn)));
146 bitset_set(live, get_irn_idx(irn));
147 border_use(irn, step, 0);
153 * Determine the last uses of a value inside the block, since they are
154 * relevant for the interval borders.
156 sched_foreach_reverse(block, irn) {
157 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
158 DBG((dbg, LEVEL_2, "\tlive: %B\n", live));
160 if (get_irn_mode(irn) == mode_T) {
161 const ir_edge_t *edge;
163 foreach_out_edge(irn, edge) {
164 ir_node *proj = get_edge_src_irn(edge);
167 * If the node defines some value, which can put into a
168 * register of the current class, make a border for it.
170 if (has_reg_class(env, proj)) {
171 int nr = get_irn_idx(proj);
173 bitset_clear(live, nr);
174 border_def(proj, step, 1);
179 * If the node defines some value, which can put into a
180 * register of the current class, make a border for it.
182 if (has_reg_class(env, irn)) {
183 int nr = get_irn_idx(irn);
185 bitset_clear(live, nr);
186 border_def(irn, step, 1);
191 * If the node is no phi node we can examine the uses.
194 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
195 ir_node *op = get_irn_n(irn, i);
197 if (has_reg_class(env, op)) {
198 int nr = get_irn_idx(op);
199 const char *msg = "-";
201 if (!bitset_is_set(live, nr)) {
202 border_use(op, step, 1);
203 bitset_set(live, nr);
207 DBG((dbg, LEVEL_4, "\t\t%s pos: %d, use: %+F\n", msg, i, op));
214 bitset_foreach(live, elm) {
215 ir_node *irn = get_idx_irn(env->irg, elm);
216 if (be_is_live_in(lv, block, irn))
217 border_def(irn, step, 0);
224 be_insn_t *chordal_scan_insn(be_chordal_env_t *env, ir_node *irn)
228 ie.ignore_colors = env->ignore_colors;
231 return be_scan_insn(&ie, irn);
234 ir_node *pre_process_constraints(be_chordal_env_t *env, be_insn_t **the_insn)
236 be_insn_t *insn = *the_insn;
237 ir_node *perm = NULL;
238 bitset_t *out_constr = bitset_alloca(env->cls->n_regs);
239 const ir_edge_t *edge;
242 assert(insn->has_constraints && "only do this for constrained nodes");
245 * Collect all registers that occur in output constraints.
246 * This is necessary, since if the insn has one of these as an input constraint
247 * and the corresponding operand interferes with the insn, the operand must
250 for (i = 0; i < insn->use_start; ++i) {
251 be_operand_t *op = &insn->ops[i];
252 if (op->has_constraints)
253 bitset_or(out_constr, op->regs);
257 * Make the Perm, recompute liveness and re-scan the insn since the
258 * in operands are now the Projs of the Perm.
260 perm = insert_Perm_after(env->birg, env->cls, sched_prev(insn->irn));
262 /* Registers are propagated by insert_Perm_after(). Clean them here! */
266 be_stat_ev("constr_perm", get_irn_arity(perm));
267 foreach_out_edge(perm, edge) {
268 ir_node *proj = get_edge_src_irn(edge);
269 arch_set_irn_register(proj, NULL);
273 * We also have to re-build the insn since the input operands are now the Projs of
274 * the Perm. Recomputing liveness is also a good idea if a Perm is inserted, since
275 * the live sets may change.
277 obstack_free(env->obst, insn);
278 *the_insn = insn = chordal_scan_insn(env, insn->irn);
281 * Copy the input constraints of the insn to the Perm as output
282 * constraints. Succeeding phases (coalescing) will need that.
284 for (i = insn->use_start; i < insn->n_ops; ++i) {
285 be_operand_t *op = &insn->ops[i];
286 ir_node *proj = op->carrier;
288 * Note that the predecessor must not be a Proj of the Perm,
289 * since ignore-nodes are not Perm'ed.
291 if (op->has_constraints && is_Proj(proj) && get_Proj_pred(proj) == perm) {
292 be_set_constr_out(perm, get_Proj_proj(proj), op->req);
299 void be_init_chordal_common(void)
301 FIRM_DBG_REGISTER(dbg, "firm.be.chordal_common");
304 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_common);