becopyopt: Remove the unnecessary attribute name from struct copy_opt_t.
[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 *env, struct list_head *head,
40                         ir_node *irn, unsigned step, unsigned pressure,
41                         unsigned is_def, unsigned is_real)
42 {
43         border_t *b;
44
45         if (!is_def) {
46                 border_t *def;
47
48                 b = OALLOC(&env->obst, border_t);
49
50                 /* also allocate the def and tie it to the use. */
51                 def = OALLOCZ(&env->obst, border_t);
52                 b->other_end = def;
53                 def->other_end = b;
54
55                 /*
56                  * Set the link field of the irn to the def.
57                  * This strongly relies on the fact, that the use is always
58                  * made before the def.
59                  */
60                 set_irn_link(irn, def);
61
62                 DEBUG_ONLY(b->magic = BORDER_FOURCC;)
63                 DEBUG_ONLY(def->magic = BORDER_FOURCC;)
64         } else {
65                 /*
66                  * If the def is encountered, the use was made and so was the
67                  * the def node (see the code above). It was placed into the
68                  * link field of the irn, so we can get it there.
69                  */
70                 b = (border_t*)get_irn_link(irn);
71
72                 DEBUG_ONLY(assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");)
73         }
74
75         b->pressure = pressure;
76         b->is_def = is_def;
77         b->is_real = is_real;
78         b->irn = irn;
79         b->step = step;
80         list_add_tail(&b->list, head);
81         DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n", is_def ? "def" : "use", irn, step));
82
83
84         return b;
85 }
86
87 void create_borders(ir_node *block, void *env_ptr)
88 {
89 /* Convenience macro for a def */
90 #define border_def(irn, step, real) \
91         border_add(env, head, irn, step, pressure--, 1, real)
92
93 /* Convenience macro for a use */
94 #define border_use(irn, step, real) \
95         border_add(env, head, irn, step, ++pressure, 0, real)
96
97         be_chordal_env_t *const env = (be_chordal_env_t*)env_ptr;
98
99         unsigned step = 0;
100         unsigned pressure = 0;
101         struct list_head *head;
102
103         /* Set up the border list in the block info */
104         head = OALLOC(&env->obst, struct list_head);
105         INIT_LIST_HEAD(head);
106         assert(pmap_get(struct list_head, env->border_heads, block) == NULL);
107         pmap_insert(env->border_heads, block, head);
108
109         ir_nodeset_t live;
110         ir_nodeset_init(&live);
111
112         be_lv_t *const lv = be_get_irg_liveness(env->irg);
113
114         /*
115          * Make final uses of all values live out of the block.
116          * They are necessary to build up real intervals.
117          */
118         be_lv_foreach_cls(lv, block, be_lv_state_end, env->cls, irn) {
119                 DB((dbg, LEVEL_3, "\tMaking live: %+F\n", irn));
120                 ir_nodeset_insert(&live, irn);
121                 border_use(irn, step, 0);
122         }
123         ++step;
124
125         /*
126          * Determine the last uses of a value inside the block, since they are
127          * relevant for the interval borders.
128          */
129         sched_foreach_reverse(block, irn) {
130                 if (is_Phi(irn))
131                         break;
132
133                 DB((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
134
135                 be_foreach_definition(irn, env->cls, def, req,
136                         /*
137                          * If the node defines some value, which can put into a
138                          * register of the current class, make a border for it.
139                          */
140                         ir_nodeset_remove(&live, def);
141                         border_def(def, step, 1);
142                 );
143
144                 /* Examine the uses. */
145                 be_foreach_use(irn, env->cls, in_req_, op, op_req_,
146                         const char *msg = "-";
147
148                         if (ir_nodeset_insert(&live, op)) {
149                                 border_use(op, step, 1);
150                                 msg = "X";
151                         }
152
153                         DB((dbg, LEVEL_4, "\t\t%s pos: %d, use: %+F\n", msg, i_, op));
154                 );
155                 ++step;
156         }
157
158         foreach_ir_nodeset(&live, irn, iter) {
159                 assert(be_is_live_in(lv, block, irn) ^ (is_Phi(irn) && get_nodes_block(irn) == block));
160                 border_def(irn, step, get_nodes_block(irn) == block);
161         }
162
163         ir_nodeset_destroy(&live);
164 }
165
166 ir_node *pre_process_constraints(be_chordal_env_t *env, be_insn_t **the_insn)
167 {
168         be_insn_t *insn = *the_insn;
169
170         /*
171          * Make the Perm, recompute liveness and re-scan the insn since the
172          * in operands are now the Projs of the Perm.
173          */
174         ir_node *const irn  = insn->irn;
175         ir_node *const perm = insert_Perm_before(env->irg, env->cls, irn);
176
177         /* Registers are propagated by insert_Perm_before(). Clean them here! */
178         if (perm == NULL)
179                 return NULL;
180
181         stat_ev_int("constr_perm", get_irn_arity(perm));
182
183         /*
184          * We also have to re-build the insn since the input operands are now the Projs of
185          * the Perm. Recomputing liveness is also a good idea if a Perm is inserted, since
186          * the live sets may change.
187          */
188         obstack_free(&env->obst, insn);
189         *the_insn = insn = be_scan_insn(env, irn);
190
191         /* Copy the input constraints of the irn to the Perm as output
192          * constraints. Succeeding phases (coalescing) will need that. */
193         for (int i = 0, n = get_irn_arity(irn); i != n; ++i) {
194                 ir_node *const proj = get_irn_n(irn, i);
195                 /* Note that the predecessor is not necessarily a Proj of the Perm,
196                  * since ignore-nodes are not Perm'ed. */
197                 if (!is_Proj(proj) || get_Proj_pred(proj) != perm)
198                         continue;
199                 /* FIXME: Only setting the constraints, when the register requirement is
200                  * limited, is a hack.  It will break when multiple differently constrained
201                  * inputs use the same value. */
202                 arch_register_req_t const *const req = arch_get_irn_register_req_in(irn, i);
203                 if (!arch_register_req_is(req, limited))
204                         continue;
205                 be_set_constr_out(perm, get_Proj_proj(proj), req);
206         }
207
208         return perm;
209 }
210
211 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_common)
212 void be_init_chordal_common(void)
213 {
214         FIRM_DBG_REGISTER(dbg, "firm.be.chordal_common");
215 }