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