10a9a4406db1401881f5b6b243e2d8ebbc59f1bd
[libfirm] / ir / be / bechordal_common.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       Common functions for chordal register allocation.
9  * @author      Sebastian Hack
10  * @date        08.12.2004
11  */
12 #include "config.h"
13
14 #include "bechordal_common.h"
15
16 #include "debug.h"
17
18 #include "iredges.h"
19 #include "bitset.h"
20
21 #include "bechordal.h"
22 #include "bechordal_t.h"
23 #include "beirg.h"
24 #include "beirgmod.h"
25 #include "beinsn_t.h"
26 #include "besched.h"
27 #include "statev_t.h"
28 #include "benode.h"
29 #include "bemodule.h"
30 #include "belive.h"
31 #include "belive_t.h"
32 #include "fourcc.h"
33
34 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
35
36 /* Make a fourcc for border checking. */
37 #define BORDER_FOURCC   FOURCC('B', 'O', 'R', 'D')
38
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)
40 {
41         border_t *b;
42
43         if (!is_def) {
44                 border_t *def;
45
46                 b = OALLOC(&env->obst, border_t);
47
48                 /* also allocate the def and tie it to the use. */
49                 def = OALLOCZ(&env->obst, border_t);
50                 b->other_end = def;
51                 def->other_end = b;
52
53                 /*
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.
57                  */
58                 set_irn_link(irn, def);
59
60                 DEBUG_ONLY(b->magic = BORDER_FOURCC;)
61                 DEBUG_ONLY(def->magic = BORDER_FOURCC;)
62         } else {
63                 /*
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.
67                  */
68                 b = (border_t*)get_irn_link(irn);
69
70                 DEBUG_ONLY(assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");)
71         }
72
73         b->is_def = is_def;
74         b->is_real = is_real;
75         b->irn = irn;
76         b->step = step;
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));
79
80
81         return b;
82 }
83
84 void create_borders(ir_node *block, void *env_ptr)
85 {
86 /* Convenience macro for a def */
87 #define border_def(irn, step, real) \
88         border_add(env, head, irn, step, 1, real)
89
90 /* Convenience macro for a use */
91 #define border_use(irn, step, real) \
92         border_add(env, head, irn, step, 0, real)
93
94         be_chordal_env_t *const env = (be_chordal_env_t*)env_ptr;
95
96         unsigned step = 0;
97         struct list_head *head;
98
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);
104
105         ir_nodeset_t live;
106         ir_nodeset_init(&live);
107
108         be_lv_t *const lv = be_get_irg_liveness(env->irg);
109
110         /*
111          * Make final uses of all values live out of the block.
112          * They are necessary to build up real intervals.
113          */
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);
118         }
119         ++step;
120
121         /*
122          * Determine the last uses of a value inside the block, since they are
123          * relevant for the interval borders.
124          */
125         sched_foreach_reverse(block, irn) {
126                 if (is_Phi(irn))
127                         break;
128
129                 DB((dbg, LEVEL_1, "\tinsn: %+F\n", irn));
130
131                 be_foreach_definition(irn, env->cls, def, req,
132                         /*
133                          * If the node defines some value, which can put into a
134                          * register of the current class, make a border for it.
135                          */
136                         ir_nodeset_remove(&live, def);
137                         border_def(def, step, 1);
138                 );
139
140                 /* Examine the uses. */
141                 be_foreach_use(irn, env->cls, in_req_, op, op_req_,
142                         const char *msg = "-";
143
144                         if (ir_nodeset_insert(&live, op)) {
145                                 border_use(op, step, 1);
146                                 msg = "X";
147                         }
148
149                         DB((dbg, LEVEL_4, "\t\t%s pos: %d, use: %+F\n", msg, i_, op));
150                 );
151                 ++step;
152         }
153
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);
157         }
158
159         ir_nodeset_destroy(&live);
160 }
161
162 ir_node *pre_process_constraints(be_chordal_env_t *env, be_insn_t **the_insn)
163 {
164         be_insn_t *insn = *the_insn;
165
166         /*
167          * Make the Perm, recompute liveness and re-scan the insn since the
168          * in operands are now the Projs of the Perm.
169          */
170         ir_node *const irn  = insn->irn;
171         ir_node *const perm = insert_Perm_before(env->irg, env->cls, irn);
172
173         /* Registers are propagated by insert_Perm_before(). Clean them here! */
174         if (perm == NULL)
175                 return NULL;
176
177         stat_ev_int("constr_perm", get_irn_arity(perm));
178
179         /*
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.
183          */
184         obstack_free(&env->obst, insn);
185         *the_insn = insn = be_scan_insn(env, irn);
186
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)
194                         continue;
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))
200                         continue;
201                 be_set_constr_out(perm, get_Proj_proj(proj), req);
202         }
203
204         return perm;
205 }
206
207 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_common)
208 void be_init_chordal_common(void)
209 {
210         FIRM_DBG_REGISTER(dbg, "firm.be.chordal_common");
211 }