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