8c84b201169835c09955f440c73c6ea777c09020
[libfirm] / ir / be / bessadestrsimple.c
1 /**
2  * Author:      Daniel Grund
3  * Date:                17.01.2006
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  *
7  */
8
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #ifdef HAVE_MALLOC_H
15  #include <malloc.h>
16 #endif
17 #ifdef HAVE_ALLOCA_H
18  #include <alloca.h>
19 #endif
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <limits.h>
24
25 #include "set.h"
26 #include "pset.h"
27 #include "pmap.h"
28 #include "bitset.h"
29
30 #include "irprintf_t.h"
31 #include "irnode_t.h"
32 #include "irgraph_t.h"
33 #include "irgwalk.h"
34 #include "iredges_t.h"
35 #include "irdom_t.h"
36 #include "phiclass.h"
37
38 #include "beraextern.h"
39 #include "beabi.h"
40 #include "bearch.h"
41 #include "benode_t.h"
42 #include "beirgmod.h"
43 #include "besched_t.h"
44 #include "beutil.h"
45 #include "belive_t.h"
46 #include "beinsn_t.h"
47
48 #include "bessadestrsimple.h"
49
50 #define DBG_LEVEL 2
51
52 typedef struct _ssa_destr_env_t {
53         ir_graph                    *irg;
54         const arch_register_class_t *cls;
55         const arch_env_t            *aenv;
56         set                         *vars;
57 } ssa_destr_env_t;
58
59 #define pset_foreach(pset, irn)  for(irn=pset_first(pset); irn; irn=pset_next(pset))
60 #define set_foreach(set, e)  for(e=set_first(set); e; e=set_next(set))
61
62 static phi_classes_t *pc = NULL;
63
64 /******************************************************************************
65    __      __   _       ___   __      __
66    \ \    / /  | |     |__ \  \ \    / /
67     \ \  / /_ _| |___     ) |  \ \  / /_ _ _ __ ___
68      \ \/ / _` | / __|   / /    \ \/ / _` | '__/ __|
69       \  / (_| | \__ \  / /_     \  / (_| | |  \__ \
70        \/ \__,_|_|___/ |____|     \/ \__,_|_|  |___/
71  *****************************************************************************/
72
73 /**
74  * The link field of an irn points to the var_info struct
75  * representing the corresponding variable.
76  */
77 #define set_var_info(irn, vi)                           set_irn_link(irn, vi)
78
79 #define HASH_VAR_NR(var_nr) var_nr
80
81 static int compare_var_infos(const void *e1, const void *e2, size_t size) {
82         const be_var_info_t *v1 = e1;
83         const be_var_info_t *v2 = e2;
84
85         if (v1->var_nr == SET_REMOVED || v2->var_nr == SET_REMOVED)
86                 return 1;
87
88         return v1->var_nr != v2->var_nr;
89 }
90
91 be_var_info_t *be_var_find(set *vars, int var_nr) {
92         be_var_info_t vi;
93         vi.var_nr = var_nr;
94
95         return set_find(vars, &vi, sizeof(vi), HASH_VAR_NR(var_nr));
96 }
97
98 be_var_info_t *be_var_find_or_insert(set *vars, int var_nr) {
99         be_var_info_t vi, *found;
100         memset(&vi, 0, sizeof(vi));
101         vi.var_nr = var_nr;
102
103         found = set_insert(vars, &vi, sizeof(vi), HASH_VAR_NR(var_nr));
104
105         if (!found->values)
106                 found->values  = pset_new_ptr(1);
107
108         return found;
109 }
110
111 /**
112  * Adds a value to a variable. Sets all pointers accordingly.
113  */
114 be_var_info_t *be_var_add_value(set *vars, int var_nr, ir_node *irn) {
115         be_var_info_t *vi = be_var_find_or_insert(vars, var_nr);
116
117         /* var 2 value mapping */
118         pset_insert_ptr(vi->values, irn);
119
120         /* value 2 var mapping */
121         set_var_info(irn, vi);
122
123         return vi;
124 }
125
126 pset *be_get_var_values(set *vars, int var_nr) {
127         be_var_info_t *vi = be_var_find(vars, var_nr);
128         assert(vi && "Variable does not exist");
129         return vi->values;
130 }
131
132 static INLINE ir_node *get_first_phi(ir_node **s) {
133         int i;
134
135         for (i = ARR_LEN(s) - 1; i >= 0; --i) {
136                 if (is_Phi(s[i]))
137                         return s[i];
138         }
139
140         assert(0 && "There must be a phi in this");
141         return NULL;
142 }
143
144
145 /**
146  * Define variables (numbers) for all SSA-values.
147  * All values in a phi class get assigned the same variable name.
148  * The link field maps values to the var-name
149  */
150 static void values_to_vars(ir_node *irn, void *env) {
151         ssa_destr_env_t *sde = env;
152         int             nr, i, build_vals = 0;
153         ir_node         **vals;
154
155         if (arch_get_irn_reg_class(sde->aenv, irn, -1) == NULL)
156                 return;
157
158         vals = get_phi_class(pc, irn);
159
160         if (vals) {
161                 nr = get_irn_node_nr(get_first_phi(vals));
162         } else {
163                 /* not a phi class member, value == var */
164                 nr         = get_irn_node_nr(irn);
165                 vals       = NEW_ARR_F(ir_node *, 1);
166                 vals[0]    = irn;
167                 build_vals = 1;
168         }
169
170         /* values <--> var mapping */
171         for (i = ARR_LEN(vals) - 1; i >= 0; --i) {
172                 be_var_add_value(sde->vars, nr, vals[i]);
173         }
174
175         if (build_vals)
176                 DEL_ARR_F(vals);
177 }
178
179 /******************************************************************************
180      _____ _____              _____            _
181     / ____/ ____|  /\        |  __ \          | |
182    | (___| (___   /  \ ______| |  | | ___  ___| |_ _ __
183     \___ \\___ \ / /\ \______| |  | |/ _ \/ __| __| '__|
184     ____) |___) / ____ \     | |__| |  __/\__ \ |_| |
185    |_____/_____/_/    \_\    |_____/ \___||___/\__|_|
186
187  *****************************************************************************/
188
189 #define mark_as_done(irn, pos)                  set_irn_link(irn, INT_TO_PTR(pos+1))
190 #define has_been_done(irn, pos)                 (PTR_TO_INT(get_irn_link(irn)) > pos)
191
192 /**
193  * Insert a copy for the argument of @p start_phi found at position @p pos.
194  * Also searches a phi-loop of arbitrary length to detect and resolve
195  *   the class of phi-swap-problems. To search for a loop recursion is used.
196  *
197  * 1) Simplest case (phi with a non-phi arg):
198  *     A single copy is inserted.
199  *
200  * 2) Phi chain (phi (with phi-arg)* with non-phi arg):
201  *     Several copies are placed, each after returning from recursion.
202  *
203  * 3) Phi-loop:
204  *     On detection a loop breaker is inserted, which is a copy of the start_phi.
205  *     This copy then pretends beeing the argumnent of the last phi.
206  *     Now case 2) can be used.
207  *
208  * The values of @p start_phi and @p pos never change during recursion.
209  *
210  * @p start_phi  Phi node to process
211  * @p pos        Argument position to insert copy/copies for
212  * @p curr_phi   Phi node currently processed during recursion. Equals start_phi on initial call
213  *
214  * @return NULL  If no copy is necessary
215  *         NULL  If the phi has already been processed at this pos
216  *               Link field is used to keep track of processed positions
217  *         In all other cases the ir_node *copy which was placed is returned.
218  */
219 static ir_node *insert_copies(ssa_destr_env_t *sde, const arch_register_class_t *cls, ir_node *start_phi, int pos, ir_node *curr_phi) {
220         ir_node *arg = get_irn_n(curr_phi, pos);
221         ir_node *arg_blk = get_nodes_block(arg);
222         ir_node *pred_blk = get_Block_cfgpred_block(get_nodes_block(curr_phi), pos);
223         ir_node *curr_cpy, *last_cpy;
224
225         assert(is_Phi(start_phi) && is_Phi(curr_phi));
226
227         if (has_been_done(start_phi, pos))
228                 return NULL;
229
230         /* In case this is a 'normal' phi we insert at the
231          * end of the pred block before cf nodes */
232         last_cpy = sched_skip(pred_blk, 0, sched_skip_cf_predicator, (void *)sde->aenv);
233         last_cpy = sched_next(last_cpy);
234
235         /* If we detect a loop stop recursion. */
236         if (arg == start_phi) {
237                 ir_node *loop_breaker;
238                 if (start_phi == curr_phi) {
239                         /* Phi directly uses itself. No copy necessary */
240                         return NULL;
241                 }
242
243                 /* At least 2 phis are involved */
244                 /* Insert a loop breaking copy (an additional variable T) */
245                 loop_breaker = be_new_Copy(cls, sde->irg, pred_blk, start_phi);
246                 sched_add_before(last_cpy, loop_breaker);
247
248                 arg = loop_breaker;
249         }
250
251         /* If arg is a phi in the same block we have to continue search */
252         if (is_Phi(arg) && arg_blk == get_nodes_block(start_phi))
253                 last_cpy = insert_copies(sde, cls, start_phi, pos, arg);
254
255         /* Insert copy of argument (may be the loop-breaker) */
256         curr_cpy = be_new_Copy(cls, sde->irg, pred_blk, arg);
257         set_irn_n(curr_phi, pos, curr_cpy);
258         mark_as_done(curr_phi, pos);
259         sched_add_before(last_cpy, curr_cpy);
260         return curr_cpy;
261 }
262
263
264 /**
265  * Perform simple SSA-destruction with copies.
266  * The order of processing _must_ be
267  *  for all positions {
268  *    for all phis {
269  *      doit
270  *    }
271  *  }
272  * else the magic to keep track of processed phi-positions will fail in
273  * function 'insert_copies'
274  */
275 static void ssa_destr_simple_walker(ir_node *blk, void *env) {
276         ssa_destr_env_t *sde = env;
277         int pos, max;
278         ir_node *phi;
279         const arch_register_class_t *cls;
280
281         /* for all argument positions of the phis */
282         for (pos=0, max=get_irn_arity(blk); pos<max; ++pos) {
283
284                 /* for all phi nodes (which are scheduled first) */
285                 sched_foreach(blk, phi) {
286                         if (!is_Phi(phi))
287                                 break;
288
289                         if (arch_irn_is(sde->aenv, phi, ignore))
290                                 continue;
291
292                         cls = arch_get_irn_reg_class(sde->aenv, phi, -1);
293                         insert_copies(sde, cls, phi, pos, phi);
294                 }
295         }
296 }
297
298
299 set *be_ssa_destr_simple(ir_graph *irg, const arch_env_t *aenv) {
300         ssa_destr_env_t sde;
301
302         sde.irg = irg;
303         sde.aenv = aenv;
304         sde.vars = new_set(compare_var_infos, 16);
305
306         be_clear_links(irg);
307         irg_block_walk_graph(irg, ssa_destr_simple_walker, NULL, &sde);
308
309         /* Mapping of SSA-Values <--> Variables */
310         pc = phi_class_new_from_irg(irg, 0);
311         be_clear_links(irg);
312         irg_walk_graph(irg, values_to_vars, NULL, &sde);
313
314         return sde.vars;
315 }
316
317 void free_ssa_destr_simple(set *vars)
318 {
319   be_var_info_t *vi;
320
321   set_foreach(vars, vi)
322     del_pset(vi->values);
323
324   del_set(vars);
325   phi_class_free(pc);
326 }