Initial Version
[libfirm] / ir / be / bechordal_common.c
1 /*
2  * bechordal_common.c
3  *
4  *  Created on: Nov 11, 2009
5  *      Author: bersch
6  */
7
8 #include "config.h"
9 #include "debug.h"
10
11 #include "iredges.h"
12 #include "bitset.h"
13
14 #include "bechordal.h"
15 #include "bechordal_t.h"
16 #include "beirgmod.h"
17 #include "beinsn_t.h"
18 #include "besched.h"
19 #include "bestatevent.h"
20 #include "benode.h"
21 #include "bemodule.h"
22 #include "belive.h"
23 #include "belive_t.h"
24 #include "fourcc.h"
25
26 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
27
28 /* Make a fourcc for border checking. */
29 #define BORDER_FOURCC                           FOURCC('B', 'O', 'R', 'D')
30
31
32 /**
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.
37  */
38 inline int has_reg_class(const be_chordal_env_t *env, const ir_node *irn)
39 {
40         return arch_irn_consider_in_reg_alloc(env->cls, irn);
41 }
42
43 /**
44  * Add an interval border to the list of a block's list
45  * of interval border.
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.
54  */
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)
58 {
59         border_t *b;
60
61         if (!is_def) {
62                 border_t *def;
63
64                 b = OALLOC(env->obst, border_t);
65
66                 /* also allocate the def and tie it to the use. */
67                 def = OALLOCZ(env->obst, border_t);
68                 b->other_end = def;
69                 def->other_end = b;
70
71                 /*
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.
75                  */
76                 set_irn_link(irn, def);
77
78                 DEBUG_ONLY(b->magic = BORDER_FOURCC);
79                 DEBUG_ONLY(def->magic = BORDER_FOURCC);
80         } else {
81                 /*
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.
85                  */
86                 b = get_irn_link(irn);
87
88                 DEBUG_ONLY(assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered"));
89         }
90
91         b->pressure = pressure;
92         b->is_def = is_def;
93         b->is_real = is_real;
94         b->irn = irn;
95         b->step = step;
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));
98
99
100         return b;
101 }
102
103 /**
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.
108  */
109 void pressure(ir_node *block, void *env_ptr)
110 {
111 /* Convenience macro for a def */
112 #define border_def(irn, step, real) \
113         border_add(env, head, irn, step, pressure--, 1, real)
114
115 /* Convenience macro for a use */
116 #define border_use(irn, step, real) \
117         border_add(env, head, irn, step, ++pressure, 0, real)
118
119         be_chordal_env_t *env             = env_ptr;
120         bitset_t *live                    = bitset_malloc(get_irg_last_idx(env->irg));
121         ir_node *irn;
122         be_lv_t *lv                       = env->birg->lv;
123
124         int i, n;
125         bitset_pos_t elm;
126         unsigned step = 0;
127         unsigned pressure = 0;
128         struct list_head *head;
129
130         bitset_clear_all(live);
131
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);
137
138         /*
139          * Make final uses of all values live out of the block.
140          * They are necessary to build up real intervals.
141          */
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);
148                 }
149         }
150         ++step;
151
152         /*
153          * Determine the last uses of a value inside the block, since they are
154          * relevant for the interval borders.
155          */
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));
159
160                 if (get_irn_mode(irn) == mode_T) {
161                         const ir_edge_t *edge;
162
163                         foreach_out_edge(irn, edge) {
164                                 ir_node *proj = get_edge_src_irn(edge);
165
166                                 /*
167                                  * If the node defines some value, which can put into a
168                                  * register of the current class, make a border for it.
169                                  */
170                                 if (has_reg_class(env, proj)) {
171                                         int nr = get_irn_idx(proj);
172
173                                         bitset_clear(live, nr);
174                                         border_def(proj, step, 1);
175                                 }
176                         }
177                 } else {
178                         /*
179                          * If the node defines some value, which can put into a
180                          * register of the current class, make a border for it.
181                          */
182                         if (has_reg_class(env, irn)) {
183                                 int nr = get_irn_idx(irn);
184
185                                 bitset_clear(live, nr);
186                                 border_def(irn, step, 1);
187                         }
188                 }
189
190                 /*
191                  * If the node is no phi node we can examine the uses.
192                  */
193                 if (!is_Phi(irn)) {
194                         for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
195                                 ir_node *op = get_irn_n(irn, i);
196
197                                 if (has_reg_class(env, op)) {
198                                         int nr = get_irn_idx(op);
199                                         const char *msg = "-";
200
201                                         if (!bitset_is_set(live, nr)) {
202                                                 border_use(op, step, 1);
203                                                 bitset_set(live, nr);
204                                                 msg = "X";
205                                         }
206
207                                         DBG((dbg, LEVEL_4, "\t\t%s pos: %d, use: %+F\n", msg, i, op));
208                                 }
209                         }
210                 }
211                 ++step;
212         }
213
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);
218         }
219
220         bitset_free(live);
221 }
222
223
224 be_insn_t *chordal_scan_insn(be_chordal_env_t *env, ir_node *irn)
225 {
226         be_insn_env_t ie;
227
228         ie.ignore_colors = env->ignore_colors;
229         ie.obst          = env->obst;
230         ie.cls           = env->cls;
231         return be_scan_insn(&ie, irn);
232 }
233
234 ir_node *pre_process_constraints(be_chordal_env_t *env, be_insn_t **the_insn)
235 {
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;
240         int i;
241
242         assert(insn->has_constraints && "only do this for constrained nodes");
243
244         /*
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
248          * be copied.
249          */
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);
254         }
255
256         /*
257          * Make the Perm, recompute liveness and re-scan the insn since the
258          * in operands are now the Projs of the Perm.
259          */
260         perm = insert_Perm_after(env->birg, env->cls, sched_prev(insn->irn));
261
262         /* Registers are propagated by insert_Perm_after(). Clean them here! */
263         if (perm == NULL)
264                 return NULL;
265
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);
270         }
271
272         /*
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.
276          */
277         obstack_free(env->obst, insn);
278         *the_insn = insn = chordal_scan_insn(env, insn->irn);
279
280         /*
281          * Copy the input constraints of the insn to the Perm as output
282          * constraints. Succeeding phases (coalescing) will need that.
283          */
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;
287                 /*
288                  * Note that the predecessor must not be a Proj of the Perm,
289                  * since ignore-nodes are not Perm'ed.
290                  */
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);
293                 }
294         }
295
296         return perm;
297 }
298
299 void be_init_chordal_common(void)
300 {
301         FIRM_DBG_REGISTER(dbg, "firm.be.chordal_common");
302 }
303
304 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_common);