2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Simple SSA destruction.
23 * @author Daniel Grund
43 #include "irprintf_t.h"
45 #include "irgraph_t.h"
47 #include "iredges_t.h"
55 #include "besched_t.h"
60 #include "bessadestrsimple.h"
64 typedef struct _ssa_destr_env_t {
66 const arch_register_class_t *cls;
67 const arch_env_t *aenv;
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))
74 static phi_classes_t *pc = NULL;
76 /******************************************************************************
78 \ \ / / | | |__ \ \ \ / /
79 \ \ / /_ _| |___ ) | \ \ / /_ _ _ __ ___
80 \ \/ / _` | / __| / / \ \/ / _` | '__/ __|
81 \ / (_| | \__ \ / /_ \ / (_| | | \__ \
82 \/ \__,_|_|___/ |____| \/ \__,_|_| |___/
83 *****************************************************************************/
86 * The link field of an irn points to the var_info struct
87 * representing the corresponding variable.
89 #define set_var_info(irn, vi) set_irn_link(irn, vi)
91 #define HASH_VAR_NR(var_nr) var_nr
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;
97 if (v1->var_nr == SET_REMOVED || v2->var_nr == SET_REMOVED)
100 return v1->var_nr != v2->var_nr;
103 be_var_info_t *be_var_find(set *vars, int var_nr) {
107 return set_find(vars, &vi, sizeof(vi), HASH_VAR_NR(var_nr));
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));
115 found = set_insert(vars, &vi, sizeof(vi), HASH_VAR_NR(var_nr));
118 found->values = pset_new_ptr(1);
124 * Adds a value to a variable. Sets all pointers accordingly.
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);
129 /* var 2 value mapping */
130 pset_insert_ptr(vi->values, irn);
132 /* value 2 var mapping */
133 set_var_info(irn, vi);
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");
144 static INLINE ir_node *get_first_phi(ir_node **s) {
147 for (i = ARR_LEN(s) - 1; i >= 0; --i) {
152 assert(0 && "There must be a phi in this");
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
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;
167 if (arch_get_irn_reg_class(sde->aenv, irn, -1) == NULL)
170 vals = get_phi_class(pc, irn);
173 nr = get_irn_node_nr(get_first_phi(vals));
175 /* not a phi class member, value == var */
176 nr = get_irn_node_nr(irn);
177 vals = NEW_ARR_F(ir_node *, 1);
182 /* values <--> var mapping */
183 for (i = ARR_LEN(vals) - 1; i >= 0; --i) {
184 be_var_add_value(sde->vars, nr, vals[i]);
191 /******************************************************************************
193 / ____/ ____| /\ | __ \ | |
194 | (___| (___ / \ ______| | | | ___ ___| |_ _ __
195 \___ \\___ \ / /\ \______| | | |/ _ \/ __| __| '__|
196 ____) |___) / ____ \ | |__| | __/\__ \ |_| |
197 |_____/_____/_/ \_\ |_____/ \___||___/\__|_|
199 *****************************************************************************/
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)
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.
209 * 1) Simplest case (phi with a non-phi arg):
210 * A single copy is inserted.
212 * 2) Phi chain (phi (with phi-arg)* with non-phi arg):
213 * Several copies are placed, each after returning from recursion.
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.
220 * The values of @p start_phi and @p pos never change during recursion.
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
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.
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;
237 assert(is_Phi(start_phi) && is_Phi(curr_phi));
239 if (has_been_done(start_phi, pos))
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);
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 */
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);
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);
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);
277 * Perform simple SSA-destruction with copies.
278 * The order of processing _must_ be
279 * for all positions {
284 * else the magic to keep track of processed phi-positions will fail in
285 * function 'insert_copies'
287 static void ssa_destr_simple_walker(ir_node *blk, void *env) {
288 ssa_destr_env_t *sde = env;
291 const arch_register_class_t *cls;
293 /* for all argument positions of the phis */
294 for (pos=0, max=get_irn_arity(blk); pos<max; ++pos) {
296 /* for all phi nodes (which are scheduled first) */
297 sched_foreach(blk, phi) {
301 if (arch_irn_is(sde->aenv, phi, ignore))
304 cls = arch_get_irn_reg_class(sde->aenv, phi, -1);
305 insert_copies(sde, cls, phi, pos, phi);
311 set *be_ssa_destr_simple(ir_graph *irg, const arch_env_t *aenv) {
316 sde.vars = new_set(compare_var_infos, 16);
319 irg_block_walk_graph(irg, ssa_destr_simple_walker, NULL, &sde);
321 /* Mapping of SSA-Values <--> Variables */
322 pc = phi_class_new_from_irg(irg, 0);
324 irg_walk_graph(irg, values_to_vars, NULL, &sde);
329 void free_ssa_destr_simple(set *vars)
333 set_foreach(vars, vi)
334 del_pset(vi->values);
340 #endif /* NOT_PORTED */