2 * Copyright (C) 1995-2008 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
40 #include "irprintf_t.h"
42 #include "irgraph_t.h"
44 #include "iredges_t.h"
52 #include "besched_t.h"
57 #include "bessadestrsimple.h"
61 typedef struct _ssa_destr_env_t {
63 const arch_register_class_t *cls;
67 #define pset_foreach(pset, irn) for(irn=pset_first(pset); irn; irn=pset_next(pset))
68 #define set_foreach(set, e) for(e=set_first(set); e; e=set_next(set))
70 static phi_classes_t *pc = NULL;
72 /******************************************************************************
74 \ \ / / | | |__ \ \ \ / /
75 \ \ / /_ _| |___ ) | \ \ / /_ _ _ __ ___
76 \ \/ / _` | / __| / / \ \/ / _` | '__/ __|
77 \ / (_| | \__ \ / /_ \ / (_| | | \__ \
78 \/ \__,_|_|___/ |____| \/ \__,_|_| |___/
79 *****************************************************************************/
82 * The link field of an irn points to the var_info struct
83 * representing the corresponding variable.
85 #define set_var_info(irn, vi) set_irn_link(irn, vi)
87 #define HASH_VAR_NR(var_nr) var_nr
89 static int compare_var_infos(const void *e1, const void *e2, size_t size) {
90 const be_var_info_t *v1 = e1;
91 const be_var_info_t *v2 = e2;
93 if (v1->var_nr == SET_REMOVED || v2->var_nr == SET_REMOVED)
96 return v1->var_nr != v2->var_nr;
99 be_var_info_t *be_var_find(set *vars, int var_nr) {
103 return set_find(vars, &vi, sizeof(vi), HASH_VAR_NR(var_nr));
106 be_var_info_t *be_var_find_or_insert(set *vars, int var_nr) {
107 be_var_info_t vi, *found;
108 memset(&vi, 0, sizeof(vi));
111 found = set_insert(vars, &vi, sizeof(vi), HASH_VAR_NR(var_nr));
114 found->values = pset_new_ptr(1);
120 * Adds a value to a variable. Sets all pointers accordingly.
122 be_var_info_t *be_var_add_value(set *vars, int var_nr, ir_node *irn) {
123 be_var_info_t *vi = be_var_find_or_insert(vars, var_nr);
125 /* var 2 value mapping */
126 pset_insert_ptr(vi->values, irn);
128 /* value 2 var mapping */
129 set_var_info(irn, vi);
134 pset *be_get_var_values(set *vars, int var_nr) {
135 be_var_info_t *vi = be_var_find(vars, var_nr);
136 assert(vi && "Variable does not exist");
140 static INLINE ir_node *get_first_phi(ir_node **s) {
143 for (i = ARR_LEN(s) - 1; i >= 0; --i) {
148 assert(0 && "There must be a phi in this");
154 * Define variables (numbers) for all SSA-values.
155 * All values in a phi class get assigned the same variable name.
156 * The link field maps values to the var-name
158 static void values_to_vars(ir_node *irn, void *env) {
159 ssa_destr_env_t *sde = env;
160 int nr, i, build_vals = 0;
163 if (arch_get_irn_reg_class(irn, -1) == NULL)
166 vals = get_phi_class(pc, irn);
169 nr = get_irn_node_nr(get_first_phi(vals));
171 /* not a phi class member, value == var */
172 nr = get_irn_node_nr(irn);
173 vals = NEW_ARR_F(ir_node *, 1);
178 /* values <--> var mapping */
179 for (i = ARR_LEN(vals) - 1; i >= 0; --i) {
180 be_var_add_value(sde->vars, nr, vals[i]);
187 /******************************************************************************
189 / ____/ ____| /\ | __ \ | |
190 | (___| (___ / \ ______| | | | ___ ___| |_ _ __
191 \___ \\___ \ / /\ \______| | | |/ _ \/ __| __| '__|
192 ____) |___) / ____ \ | |__| | __/\__ \ |_| |
193 |_____/_____/_/ \_\ |_____/ \___||___/\__|_|
195 *****************************************************************************/
197 #define mark_as_done(irn, pos) set_irn_link(irn, INT_TO_PTR(pos+1))
198 #define has_been_done(irn, pos) (PTR_TO_INT(get_irn_link(irn)) > pos)
201 * Insert a copy for the argument of @p start_phi found at position @p pos.
202 * Also searches a phi-loop of arbitrary length to detect and resolve
203 * the class of phi-swap-problems. To search for a loop recursion is used.
205 * 1) Simplest case (phi with a non-phi arg):
206 * A single copy is inserted.
208 * 2) Phi chain (phi (with phi-arg)* with non-phi arg):
209 * Several copies are placed, each after returning from recursion.
212 * On detection a loop breaker is inserted, which is a copy of the start_phi.
213 * This copy then pretends beeing the argumnent of the last phi.
214 * Now case 2) can be used.
216 * The values of @p start_phi and @p pos never change during recursion.
218 * @p start_phi Phi node to process
219 * @p pos Argument position to insert copy/copies for
220 * @p curr_phi Phi node currently processed during recursion. Equals start_phi on initial call
222 * @return NULL If no copy is necessary
223 * NULL If the phi has already been processed at this pos
224 * Link field is used to keep track of processed positions
225 * In all other cases the ir_node *copy which was placed is returned.
227 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) {
228 ir_node *arg = get_irn_n(curr_phi, pos);
229 ir_node *arg_blk = get_nodes_block(arg);
230 ir_node *pred_blk = get_Block_cfgpred_block(get_nodes_block(curr_phi), pos);
231 ir_node *curr_cpy, *last_cpy;
233 assert(is_Phi(start_phi) && is_Phi(curr_phi));
235 if (has_been_done(start_phi, pos))
238 /* In case this is a 'normal' phi we insert at the
239 * end of the pred block before cf nodes */
240 last_cpy = sched_skip(pred_blk, 0, sched_skip_cf_predicator, NULL);
241 last_cpy = sched_next(last_cpy);
243 /* If we detect a loop stop recursion. */
244 if (arg == start_phi) {
245 ir_node *loop_breaker;
246 if (start_phi == curr_phi) {
247 /* Phi directly uses itself. No copy necessary */
251 /* At least 2 phis are involved */
252 /* Insert a loop breaking copy (an additional variable T) */
253 loop_breaker = be_new_Copy(cls, sde->irg, pred_blk, start_phi);
254 sched_add_before(last_cpy, loop_breaker);
259 /* If arg is a phi in the same block we have to continue search */
260 if (is_Phi(arg) && arg_blk == get_nodes_block(start_phi))
261 last_cpy = insert_copies(sde, cls, start_phi, pos, arg);
263 /* Insert copy of argument (may be the loop-breaker) */
264 curr_cpy = be_new_Copy(cls, sde->irg, pred_blk, arg);
265 set_irn_n(curr_phi, pos, curr_cpy);
266 mark_as_done(curr_phi, pos);
267 sched_add_before(last_cpy, curr_cpy);
273 * Perform simple SSA-destruction with copies.
274 * The order of processing _must_ be
275 * for all positions {
280 * else the magic to keep track of processed phi-positions will fail in
281 * function 'insert_copies'
283 static void ssa_destr_simple_walker(ir_node *blk, void *env) {
284 ssa_destr_env_t *sde = env;
287 const arch_register_class_t *cls;
289 /* for all argument positions of the phis */
290 for (pos=0, max=get_irn_arity(blk); pos<max; ++pos) {
292 /* for all phi nodes (which are scheduled first) */
293 sched_foreach(blk, phi) {
297 if (arch_irn_is(phi, ignore))
300 cls = arch_get_irn_reg_class(phi, -1);
301 insert_copies(sde, cls, phi, pos, phi);
307 set *be_ssa_destr_simple(ir_graph *irg)
312 sde.vars = new_set(compare_var_infos, 16);
315 irg_block_walk_graph(irg, ssa_destr_simple_walker, NULL, &sde);
317 /* Mapping of SSA-Values <--> Variables */
318 pc = phi_class_new_from_irg(irg, 0);
320 irg_walk_graph(irg, values_to_vars, NULL, &sde);
325 void free_ssa_destr_simple(set *vars)
329 set_foreach(vars, vi)
330 del_pset(vi->values);
336 #endif /* NOT_PORTED */