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 Lowering of Switches if necessary or advantageous.
23 * @author Moritz Kroll
36 #define foreach_out_irn(irn, i, outirn) for(i = get_irn_n_outs(irn) - 1;\
37 i >= 0 && (outirn = get_irn_out(irn, i)); --i)
39 typedef struct walk_env {
40 unsigned spare_size; /**< the allowed spare size for table switches */
41 struct obstack obst; /**< the obstack where data is allocated on */
42 int changed; /**< indicates whether a change was performed */
45 typedef struct case_data {
51 * Add the new predecessor x to node node, which is either a Block or a Phi
53 static void add_pred(ir_node* node, ir_node* x)
59 assert(is_Block(node) || is_Phi(node));
61 n = get_irn_arity(node);
62 NEW_ARR_A(ir_node*, ins, n + 1);
63 for (i = 0; i < n; i++)
64 ins[i] = get_irn_n(node, i);
66 set_irn_in(node, n + 1, ins);
70 * Remove the predecessor x from node node one time, which is either a Block or a Phi
72 static void remove_pred(ir_node* node, ir_node* x)
78 assert(is_Block(node) || is_Phi(node));
80 n = get_irn_arity(node);
81 NEW_ARR_A(ir_node*, ins, n - 1);
82 for (i = 0, j = -1; i < n - 1; i++)
84 ins[++j] = get_irn_n(node, i);
87 assert(i == j + 1 && "x is not a pred of node");
88 set_irn_in(node, n - 1, ins);
92 * Evaluate a switch and decide whether we should build a table switch.
94 * @param cond The Cond node representing the switch.
95 * @param spare_size Allowed spare size for table switches in machine words.
96 * (Default in edgfe: 128)
98 static int should_do_table_switch(ir_node *cond, unsigned spare_size)
103 long switch_min = LONG_MAX, switch_max = LONG_MIN;
104 unsigned long spare, num_cases = 0;
106 /* TODO: Minimum size for jump table? */
107 if(get_irn_n_outs(cond) <= 4)
110 default_pn = get_Cond_defaultProj(cond);
112 foreach_out_irn(cond, i, proj) {
113 long pn = get_Proj_proj(proj);
125 * Here we have: num_cases and [switch_min, switch_max] interval.
126 * We do an if-cascade if there are too many spare numbers.
128 spare = (unsigned long) switch_max - (unsigned long) switch_min - num_cases + 1;
129 return spare < spare_size;
132 static int casecmp(const void *a, const void *b)
134 return ((case_data_t *) a)->value - ((case_data_t *) b)->value;
138 * Creates an if cascade realizing binary search.
140 static void create_if_cascade(ir_node *sel, ir_node *curblock, ir_node *defblock,
141 case_data_t *curcases, int numcases)
143 set_cur_block(curblock);
147 /* only one case: "if(sel == val) goto target else goto default;" */
148 ir_node *val = new_Const_long(get_irn_mode(sel), curcases[0].value);
149 ir_node *cmp = new_Cmp(sel, val);
150 ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Eq);
151 ir_node *cond = new_Cond(proj);
152 add_pred(curcases[0].target, new_Proj(cond, mode_X, pn_Cond_true));
153 add_pred(defblock, new_Proj(cond, mode_X, pn_Cond_false));
155 } else if(numcases == 2) {
156 /* only two cases: "if(sel == val[0]) goto target[0];" */
157 ir_node *val = new_Const_long(get_irn_mode(sel), curcases[0].value);
158 ir_node *cmp = new_Cmp(sel, val);
159 ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Eq);
160 ir_node *cond = new_Cond(proj);
164 add_pred(curcases[0].target, new_Proj(cond, mode_X, pn_Cond_true));
165 in[0] = new_Proj(cond, mode_X, pn_Cond_false);
166 neblock = new_Block(1, in);
168 /* second part: "else if(sel == val[1]) goto target[1] else goto default;" */
169 val = new_Const_long(get_irn_mode(sel), curcases[1].value);
170 cmp = new_Cmp(sel, val);
171 proj = new_Proj(cmp, mode_b, pn_Cmp_Eq);
172 cond = new_Cond(proj);
173 add_pred(curcases[1].target, new_Proj(cond, mode_X, pn_Cond_true));
174 add_pred(defblock, new_Proj(cond, mode_X, pn_Cond_false));
177 /* recursive case: split cases in the middle */
178 int midcase = numcases / 2;
179 ir_node *val = new_Const_long(get_irn_mode(sel), curcases[midcase].value);
180 ir_node *cmp = new_Cmp(sel, val);
181 ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Lt);
182 ir_node *cond = new_Cond(proj);
187 in[0] = new_Proj(cond, mode_X, pn_Cond_true);
188 ltblock = new_Block(1, in);
190 set_cur_block(curblock);
191 in[0] = new_Proj(cond, mode_X, pn_Cond_false);
192 geblock = new_Block(1, in);
194 create_if_cascade(sel, ltblock, defblock, curcases, midcase);
195 create_if_cascade(sel, geblock, defblock, curcases + midcase, numcases - midcase);
200 * Block-Walker: searches for Cond nodes with a non-boolean mode
202 static void find_cond_nodes(ir_node *block, void *ctx)
204 walk_env_t *env = ctx;
210 int i, j = 0, numcases;
214 ir_node *defblock = NULL;
216 if(get_Block_n_cfgpreds(block) != 1)
219 projx = get_Block_cfgpred(block, 0);
222 assert(get_irn_mode(projx) == mode_X);
224 cond = get_Proj_pred(projx);
228 sel = get_Cond_selector(cond);
229 sel_mode = get_irn_mode(sel);
231 if(sel_mode == mode_b) /* not a switch? */
234 if(should_do_table_switch(cond, env->spare_size))
238 * Switch should be transformed into an if cascade.
239 * So first order the cases, so we can do a binary search on them.
242 numcases = get_irn_n_outs(cond) - 1; // does not contain default case
243 cases = obstack_alloc(&env->obst, numcases * sizeof(*cases));
245 default_pn = get_Cond_defaultProj(cond);
247 foreach_out_irn(cond, i, proj) {
248 long pn = get_Proj_proj(proj);
251 defblock = get_irn_out(proj, 0);
252 remove_pred(defblock, proj);
257 cases[j].target = get_irn_out(proj, 0);
258 remove_pred(cases[j].target, proj);
261 assert(defblock != NULL && "Switch without default proj");
262 qsort(cases, numcases, sizeof(*cases), casecmp);
264 /* Now create the if cascade */
265 condblock = get_nodes_block(cond);
266 create_if_cascade(sel, condblock, defblock, cases, numcases);
268 obstack_free(&env->obst, cases);
272 * Lowers all Switches (Cond nodes with non-boolean mode) depending on spare_size.
273 * They will either remain the same or be converted into if-cascades.
275 * @param irg The ir graph to be lowered.
276 * @param spare_size Allowed spare size for table switches in machine words.
277 * (Default in edgfe: 128)
279 void lower_Switch(ir_graph *irg, unsigned spare_size)
282 ir_graph *rem = current_ir_graph;
284 current_ir_graph = irg;
286 obstack_init(&env.obst);
287 env.spare_size = spare_size;
289 assure_irg_outs(irg);
291 irg_block_walk_graph(irg, find_cond_nodes, NULL, &env);
294 /* control flow changed */
295 set_irg_outs_inconsistent(irg);
296 set_irg_doms_inconsistent(irg);
297 set_irg_extblk_inconsistent(irg);
298 set_irg_loopinfo_inconsistent(irg);
301 obstack_free(&env.obst, NULL);
302 current_ir_graph = rem;