rework ASM node, it always has a memory input now
[libfirm] / ir / be / beloopana.c
1 /*
2  * Copyright (C) 1995-2011 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       Compute register pressure in loops.
23  * @author      Christian Wuerdig
24  * @date        19.02.2007
25  */
26 #include "config.h"
27
28 #include "set.h"
29 #include "pset.h"
30 #include "irnode.h"
31 #include "util.h"
32 #include "irloop_t.h"
33 #include "error.h"
34 #include "debug.h"
35
36 #include "bearch.h"
37 #include "belive.h"
38 #include "besched.h"
39 #include "beloopana.h"
40 #include "bemodule.h"
41
42 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
43
44 #define HASH_LOOP_INFO(info) (hash_ptr((info)->loop) ^ hash_ptr((info)->cls))
45
46 typedef struct be_loop_info_t {
47         ir_loop                     *loop;
48         const arch_register_class_t *cls;
49         unsigned                    max_pressure;
50 } be_loop_info_t;
51
52 struct be_loopana_t {
53         set      *data;
54         ir_graph *irg;
55 };
56
57 static int cmp_loop_info(const void *a, const void *b, size_t size)
58 {
59         const be_loop_info_t *i1 = (const be_loop_info_t*)a;
60         const be_loop_info_t *i2 = (const be_loop_info_t*)b;
61         (void) size;
62
63         return ! (i1->loop == i2->loop && i1->cls == i2->cls);
64 }
65
66 /**
67  * Compute the highest register pressure in a block.
68  * @param irg       The graph.
69  * @param block     The block to compute pressure for.
70  * @param cls       The register class to compute pressure for.
71  * @return The highest register pressure in the given block.
72  */
73 static unsigned be_compute_block_pressure(const ir_graph *irg,
74                                           ir_node *block,
75                                           const arch_register_class_t *cls)
76 {
77         be_lv_t     *lv = be_get_irg_liveness(irg);
78         ir_nodeset_t live_nodes;
79         size_t       max_live;
80
81         DBG((dbg, LEVEL_1, "Processing Block %+F\n", block));
82
83         /* determine largest pressure with this block */
84         ir_nodeset_init(&live_nodes);
85         be_liveness_end_of_block(lv, cls, block, &live_nodes);
86         max_live   = ir_nodeset_size(&live_nodes);
87
88         sched_foreach_reverse(block, irn) {
89                 size_t cnt;
90
91                 if (is_Phi(irn))
92                         break;
93
94                 be_liveness_transfer(cls, irn, &live_nodes);
95                 cnt      = ir_nodeset_size(&live_nodes);
96                 max_live = MAX(cnt, max_live);
97         }
98
99         DBG((dbg, LEVEL_1, "Finished with Block %+F (%s %zu)\n", block, cls->name, max_live));
100
101         ir_nodeset_destroy(&live_nodes);
102         return max_live;
103 }
104
105 /**
106  * Compute the highest register pressure in a loop and its sub-loops.
107  * @param loop_ana  The loop ana object.
108  * @param loop      The loop to compute pressure for.
109  * @param cls       The register class to compute pressure for.
110  * @return The highest register pressure in the given loop.
111  */
112 static unsigned be_compute_loop_pressure(be_loopana_t *loop_ana, ir_loop *loop,
113                                          const arch_register_class_t *cls)
114 {
115         size_t         i, max;
116         unsigned       pressure;
117         be_loop_info_t *entry, key;
118
119         DBG((dbg, LEVEL_1, "\nProcessing Loop %ld\n", loop->loop_nr));
120         assert(get_loop_n_elements(loop) > 0);
121         pressure = 0;
122
123         /* determine maximal pressure in all loop elements */
124         for (i = 0, max = get_loop_n_elements(loop); i < max; ++i) {
125                 unsigned     son_pressure;
126                 loop_element elem = get_loop_element(loop, i);
127
128                 if (*elem.kind == k_ir_node)
129                         son_pressure = be_compute_block_pressure(loop_ana->irg, elem.node, cls);
130                 else {
131                         assert(*elem.kind == k_ir_loop);
132                         son_pressure = be_compute_loop_pressure(loop_ana, elem.son, cls);
133                 }
134
135                 pressure = MAX(pressure, son_pressure);
136         }
137         DBG((dbg, LEVEL_1, "Done with loop %ld, pressure %u for class %s\n", loop->loop_nr, pressure, cls->name));
138
139         /* update info in set */
140         key.loop            = loop;
141         key.cls             = cls;
142         key.max_pressure    = 0;
143         entry               = set_insert(be_loop_info_t, loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
144         entry->max_pressure = MAX(entry->max_pressure, pressure);
145
146         return pressure;
147 }
148
149 /**
150  * Compute the register pressure for a class of all loops in a graph
151  * @param irg   The graph
152  * @param cls   The register class to compute the pressure for
153  * @return The loop analysis object.
154  */
155 be_loopana_t *be_new_loop_pressure_cls(ir_graph *irg,
156                                        const arch_register_class_t *cls)
157 {
158         be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
159
160         loop_ana->data = new_set(cmp_loop_info, 16);
161         loop_ana->irg  = irg;
162
163         DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
164         DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
165         DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
166
167         assure_loopinfo(irg);
168
169         be_compute_loop_pressure(loop_ana, get_irg_loop(irg), cls);
170
171         return loop_ana;
172 }
173
174 /**
175  * Compute the register pressure for all classes of all loops in the irg.
176  * @param irg  The graph
177  * @return The loop analysis object.
178  */
179 be_loopana_t *be_new_loop_pressure(ir_graph *irg,
180                                    const arch_register_class_t *cls)
181 {
182         be_loopana_t     *loop_ana = XMALLOC(be_loopana_t);
183         ir_loop          *irg_loop = get_irg_loop(irg);
184         const arch_env_t *arch_env = be_get_irg_arch_env(irg);
185         int               i;
186
187         loop_ana->data = new_set(cmp_loop_info, 16);
188         loop_ana->irg  = irg;
189
190         assure_loopinfo(irg);
191
192         if (cls != NULL) {
193                 be_compute_loop_pressure(loop_ana, irg_loop, cls);
194         } else {
195                 for (i = arch_env->n_register_classes - 1; i >= 0; --i) {
196                         const arch_register_class_t *cls = &arch_env->register_classes[i];
197                         DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
198                         DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
199                         DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
200                         be_compute_loop_pressure(loop_ana, irg_loop, cls);
201                 }
202         }
203
204         return loop_ana;
205 }
206
207 /**
208  * Returns the computed register pressure for the given class and loop.
209  * @return The pressure or INT_MAX if not found
210  */
211 unsigned be_get_loop_pressure(be_loopana_t *loop_ana, const arch_register_class_t *cls, ir_loop *loop)
212 {
213         unsigned pressure = INT_MAX;
214         be_loop_info_t *entry, key;
215
216         assert(cls && loop);
217
218         key.loop = loop;
219         key.cls  = cls;
220         entry    = set_find(be_loop_info_t, loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
221
222         if (entry)
223                 pressure = entry->max_pressure;
224         else
225                 panic("Pressure not computed for given class and loop object.");
226
227         return pressure;
228 }
229
230 /**
231  * Frees the loop analysis object.
232  */
233 void be_free_loop_pressure(be_loopana_t *loop_ana)
234 {
235         del_set(loop_ana->data);
236         xfree(loop_ana);
237 }
238
239 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana)
240 void be_init_loopana(void)
241 {
242         FIRM_DBG_REGISTER(dbg, "firm.be.loopana");
243 }