bemodule: Remove (hopefully really last) remnants of the STA backend.
[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
33 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
34
35 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)
36 {
37         border_t *const b = OALLOC(&env->obst, border_t);
38         b->is_def  = is_def;
39         b->is_real = is_real;
40         b->irn     = irn;
41         b->step    = step;
42         list_add_tail(&b->list, head);
43         DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n", is_def ? "def" : "use", irn, step));
44
45         return b;
46 }
47
48 void create_borders(ir_node *block, void *env_ptr)
49 {
50 /* Convenience macro for a def */
51 #define border_def(irn, step, real) \
52         border_add(env, head, irn, step, 1, real)
53
54 /* Convenience macro for a use */
55 #define border_use(irn, step, real) \
56         border_add(env, head, irn, step, 0, real)
57
58         be_chordal_env_t *const env = (be_chordal_env_t*)env_ptr;
59
60         unsigned step = 0;
61         struct list_head *head;
62
63         /* Set up the border list in the block info */
64         head = OALLOC(&env->obst, struct list_head);
65         INIT_LIST_HEAD(head);
66         assert(pmap_get(struct list_head, env->border_heads, block) == NULL);
67         pmap_insert(env->border_heads, block, head);
68
69         ir_nodeset_t live;
70         ir_nodeset_init(&live);
71
72         be_lv_t *const lv = be_get_irg_liveness(env->irg);
73
74         /*
75          * Make final uses of all values live out of the block.
76          * They are necessary to build up real intervals.
77          */
78         be_lv_foreach_cls(lv, block, be_lv_state_end, env->cls, irn) {
79                 DB((dbg, LEVEL_3, "\tMaking live: %+F\n", irn));
80                 ir_nodeset_insert(&live, irn);
81                 border_use(irn, step, 0);
82         }
83         ++step;
84
85         /*
86          * Determine the last uses of a value inside the block, since they are
87          * relevant for the interval borders.
88          */
89         sched_foreach_reverse(block, irn) {
90                 DB((dbg, LEVEL_1, "\tinsn: %+F\n", irn));
91
92                 be_foreach_definition(irn, env->cls, def, req,
93                         /*
94                          * If the node defines some value, which can put into a
95                          * register of the current class, make a border for it.
96                          */
97                         ir_nodeset_remove(&live, def);
98                         border_def(def, step, 1);
99                 );
100
101                 /* If the node is no phi node we can examine the uses. */
102                 if (!is_Phi(irn)) {
103                         be_foreach_use(irn, env->cls, in_req_, op, op_req_,
104                                 const char *msg = "-";
105
106                                 if (ir_nodeset_insert(&live, op)) {
107                                         border_use(op, step, 1);
108                                         msg = "X";
109                                 }
110
111                                 DB((dbg, LEVEL_4, "\t\t%s pos: %d, use: %+F\n", msg, i_, op));
112                         );
113                 }
114                 ++step;
115         }
116
117         /* Process live-ins last. In particular all Phis are handled before, so when
118          * iterating the borders the live-ins come before all Phis ("live-start"). */
119         foreach_ir_nodeset(&live, irn, iter) {
120                 assert(be_is_live_in(lv, block, irn));
121                 border_def(irn, step, 0);
122         }
123
124         ir_nodeset_destroy(&live);
125 }
126
127 ir_node *pre_process_constraints(be_chordal_env_t *env, be_insn_t **the_insn)
128 {
129         be_insn_t *insn = *the_insn;
130
131         /*
132          * Make the Perm, recompute liveness and re-scan the insn since the
133          * in operands are now the Projs of the Perm.
134          */
135         ir_node *const irn  = insn->irn;
136         ir_node *const perm = insert_Perm_before(env->irg, env->cls, irn);
137
138         /* Registers are propagated by insert_Perm_before(). Clean them here! */
139         if (perm == NULL)
140                 return NULL;
141
142         stat_ev_int("constr_perm", get_irn_arity(perm));
143
144         /*
145          * We also have to re-build the insn since the input operands are now the Projs of
146          * the Perm. Recomputing liveness is also a good idea if a Perm is inserted, since
147          * the live sets may change.
148          */
149         obstack_free(&env->obst, insn);
150         *the_insn = insn = be_scan_insn(env, irn);
151
152         /* Copy the input constraints of the irn to the Perm as output
153          * constraints. Succeeding phases (coalescing) will need that. */
154         for (int i = 0, n = get_irn_arity(irn); i != n; ++i) {
155                 ir_node *const proj = get_irn_n(irn, i);
156                 /* Note that the predecessor is not necessarily a Proj of the Perm,
157                  * since ignore-nodes are not Perm'ed. */
158                 if (!is_Proj(proj) || get_Proj_pred(proj) != perm)
159                         continue;
160                 /* FIXME: Only setting the constraints, when the register requirement is
161                  * limited, is a hack.  It will break when multiple differently constrained
162                  * inputs use the same value. */
163                 arch_register_req_t const *const req = arch_get_irn_register_req_in(irn, i);
164                 if (!arch_register_req_is(req, limited))
165                         continue;
166                 be_set_constr_out(perm, get_Proj_proj(proj), req);
167         }
168
169         return perm;
170 }
171
172 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_common)
173 void be_init_chordal_common(void)
174 {
175         FIRM_DBG_REGISTER(dbg, "firm.be.chordal_common");
176 }