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