beinsn: Do not store, whether insn operands are constrained.
[libfirm] / ir / be / bechordal_common.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Common functions for chordal register allocation.
23  * @author      Sebastian Hack
24  * @date        08.12.2004
25  */
26 #include "config.h"
27
28 #include "bechordal_common.h"
29
30 #include "debug.h"
31
32 #include "iredges.h"
33 #include "bitset.h"
34
35 #include "bechordal.h"
36 #include "bechordal_t.h"
37 #include "beirg.h"
38 #include "beirgmod.h"
39 #include "beinsn_t.h"
40 #include "besched.h"
41 #include "statev_t.h"
42 #include "benode.h"
43 #include "bemodule.h"
44 #include "belive.h"
45 #include "belive_t.h"
46 #include "fourcc.h"
47
48 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
49
50 /* Make a fourcc for border checking. */
51 #define BORDER_FOURCC   FOURCC('B', 'O', 'R', 'D')
52
53 static inline border_t *border_add(be_chordal_env_t *env, struct list_head *head,
54                         ir_node *irn, unsigned step, unsigned pressure,
55                         unsigned is_def, unsigned is_real)
56 {
57         border_t *b;
58
59         if (!is_def) {
60                 border_t *def;
61
62                 b = OALLOC(env->obst, border_t);
63
64                 /* also allocate the def and tie it to the use. */
65                 def = OALLOCZ(env->obst, border_t);
66                 b->other_end = def;
67                 def->other_end = b;
68
69                 /*
70                  * Set the link field of the irn to the def.
71                  * This strongly relies on the fact, that the use is always
72                  * made before the def.
73                  */
74                 set_irn_link(irn, def);
75
76                 DEBUG_ONLY(b->magic = BORDER_FOURCC;)
77                 DEBUG_ONLY(def->magic = BORDER_FOURCC;)
78         } else {
79                 /*
80                  * If the def is encountered, the use was made and so was the
81                  * the def node (see the code above). It was placed into the
82                  * link field of the irn, so we can get it there.
83                  */
84                 b = (border_t*)get_irn_link(irn);
85
86                 DEBUG_ONLY(assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");)
87         }
88
89         b->pressure = pressure;
90         b->is_def = is_def;
91         b->is_real = is_real;
92         b->irn = irn;
93         b->step = step;
94         list_add_tail(&b->list, head);
95         DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n", is_def ? "def" : "use", irn, step));
96
97
98         return b;
99 }
100
101 void create_borders(ir_node *block, void *env_ptr)
102 {
103 /* Convenience macro for a def */
104 #define border_def(irn, step, real) \
105         border_add(env, head, irn, step, pressure--, 1, real)
106
107 /* Convenience macro for a use */
108 #define border_use(irn, step, real) \
109         border_add(env, head, irn, step, ++pressure, 0, real)
110
111         be_chordal_env_t *env  = (be_chordal_env_t*)env_ptr;
112         bitset_t         *live = bitset_malloc(get_irg_last_idx(env->irg));
113         be_lv_t          *lv   = be_get_irg_liveness(env->irg);
114
115         unsigned step = 0;
116         unsigned pressure = 0;
117         struct list_head *head;
118
119         bitset_clear_all(live);
120
121         /* Set up the border list in the block info */
122         head = OALLOC(env->obst, struct list_head);
123         INIT_LIST_HEAD(head);
124         assert(pmap_get(struct list_head, env->border_heads, block) == NULL);
125         pmap_insert(env->border_heads, block, head);
126
127         /*
128          * Make final uses of all values live out of the block.
129          * They are necessary to build up real intervals.
130          */
131         be_lv_foreach(lv, block, be_lv_state_end, irn) {
132                 if (arch_irn_consider_in_reg_alloc(env->cls, irn)) {
133                         DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_idx(irn)));
134                         bitset_set(live, get_irn_idx(irn));
135                         border_use(irn, step, 0);
136                 }
137         }
138         ++step;
139
140         /*
141          * Determine the last uses of a value inside the block, since they are
142          * relevant for the interval borders.
143          */
144         sched_foreach_reverse(block, irn) {
145                 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
146                 DBG((dbg, LEVEL_2, "\tlive: %B\n", live));
147
148                 if (get_irn_mode(irn) == mode_T) {
149                         foreach_out_edge(irn, edge) {
150                                 ir_node *proj = get_edge_src_irn(edge);
151
152                                 /*
153                                  * If the node defines some value, which can put into a
154                                  * register of the current class, make a border for it.
155                                  */
156                                 if (arch_irn_consider_in_reg_alloc(env->cls, proj)) {
157                                         int nr = get_irn_idx(proj);
158
159                                         bitset_clear(live, nr);
160                                         border_def(proj, step, 1);
161                                 }
162                         }
163                 } else {
164                         /*
165                          * If the node defines some value, which can put into a
166                          * register of the current class, make a border for it.
167                          */
168                         if (arch_irn_consider_in_reg_alloc(env->cls, irn)) {
169                                 int nr = get_irn_idx(irn);
170
171                                 bitset_clear(live, nr);
172                                 border_def(irn, step, 1);
173                         }
174                 }
175
176                 /*
177                  * If the node is no phi node we can examine the uses.
178                  */
179                 if (!is_Phi(irn)) {
180                         for (int i = 0, n = get_irn_arity(irn); i < n; ++i) {
181                                 ir_node *op = get_irn_n(irn, i);
182
183                                 if (arch_irn_consider_in_reg_alloc(env->cls, op)) {
184                                         int nr = get_irn_idx(op);
185                                         const char *msg = "-";
186
187                                         if (!bitset_is_set(live, nr)) {
188                                                 border_use(op, step, 1);
189                                                 bitset_set(live, nr);
190                                                 msg = "X";
191                                         }
192
193                                         DBG((dbg, LEVEL_4, "\t\t%s pos: %d, use: %+F\n", msg, i, op));
194                                 }
195                         }
196                 }
197                 ++step;
198         }
199
200         bitset_foreach(live, elm) {
201                 ir_node *irn = get_idx_irn(env->irg, elm);
202                 if (be_is_live_in(lv, block, irn))
203                         border_def(irn, step, 0);
204         }
205
206         bitset_free(live);
207 }
208
209 ir_node *pre_process_constraints(be_chordal_env_t *env, be_insn_t **the_insn)
210 {
211         be_insn_t *insn = *the_insn;
212         assert(insn->has_constraints && "only do this for constrained nodes");
213
214         /*
215          * Make the Perm, recompute liveness and re-scan the insn since the
216          * in operands are now the Projs of the Perm.
217          */
218         ir_node *const perm = insert_Perm_before(env->irg, env->cls, insn->irn);
219
220         /* Registers are propagated by insert_Perm_before(). Clean them here! */
221         if (perm == NULL)
222                 return NULL;
223
224         stat_ev_int("constr_perm", get_irn_arity(perm));
225
226         /*
227          * We also have to re-build the insn since the input operands are now the Projs of
228          * the Perm. Recomputing liveness is also a good idea if a Perm is inserted, since
229          * the live sets may change.
230          */
231         obstack_free(env->obst, insn);
232         *the_insn = insn = be_scan_insn(env, insn->irn);
233
234         /*
235          * Copy the input constraints of the insn to the Perm as output
236          * constraints. Succeeding phases (coalescing) will need that.
237          */
238         for (int i = insn->use_start; i < insn->n_ops; ++i) {
239                 be_operand_t *op = &insn->ops[i];
240                 ir_node *proj = op->carrier;
241                 /* Note that the predecessor is not necessarily a Proj of the Perm,
242                  * since ignore-nodes are not Perm'ed. */
243                 /* FIXME: Only setting the constraints, when the register requirement is
244                  * limited, is a hack.  It will break when multiple differently constrained
245                  * inputs use the same value. */
246                 if (arch_register_req_is(op->req, limited) && is_Proj(proj) && get_Proj_pred(proj) == perm) {
247                         be_set_constr_out(perm, get_Proj_proj(proj), op->req);
248                 }
249         }
250
251         return perm;
252 }
253
254 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_common)
255 void be_init_chordal_common(void)
256 {
257         FIRM_DBG_REGISTER(dbg, "firm.be.chordal_common");
258 }