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
39 #define foreach_out_irn(irn, i, outirn) for(i = get_irn_n_outs(irn) - 1;\
40 i >= 0 && (outirn = get_irn_out(irn, i)); --i)
42 typedef struct walk_env {
43 unsigned spare_size; /**< the allowed spare size for table switches */
44 int changed; /**< indicates whether a change was performed */
47 typedef struct case_data {
52 typedef struct ifcas_env {
55 ir_node **defusers; /**< the Projs pointing to the default case */
59 * Evaluate a switch and decide whether we should build a table switch.
61 * @param cond The Cond node representing the switch.
62 * @param spare_size Allowed spare size for table switches in machine words.
63 * (Default in edgfe: 128)
65 static int should_do_table_switch(ir_node *cond, unsigned spare_size)
70 long switch_min = LONG_MAX, switch_max = LONG_MIN;
71 unsigned long spare, num_cases = 0;
73 /* TODO: Minimum size for jump table? */
74 if(get_irn_n_outs(cond) <= 4)
77 default_pn = get_Cond_defaultProj(cond);
79 foreach_out_irn(cond, i, proj) {
80 long pn = get_Proj_proj(proj);
92 * Here we have: num_cases and [switch_min, switch_max] interval.
93 * We do an if-cascade if there are too many spare numbers.
95 spare = (unsigned long) switch_max - (unsigned long) switch_min - num_cases + 1;
96 return spare < spare_size;
99 static int casecmp(const void *a, const void *b)
101 return ((case_data_t *) a)->value - ((case_data_t *) b)->value;
105 * Creates an if cascade realizing binary search.
107 static void create_if_cascade(ifcas_env_t *env, ir_node *curblock,
108 case_data_t *curcases, int numcases)
110 set_cur_block(curblock);
114 /* only one case: "if(sel == val) goto target else goto default;" */
115 ir_node *val = new_Const_long(get_irn_mode(env->sel), curcases[0].value);
116 ir_node *cmp = new_Cmp(env->sel, val);
117 ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Eq);
118 ir_node *cond = new_Cond(proj);
119 set_Block_cfgpred(curcases[0].target, 0, new_Proj(cond, mode_X, pn_Cond_true));
120 env->defusers[env->defindex++] = new_Proj(cond, mode_X, pn_Cond_false);
121 } else if(numcases == 2) {
122 /* only two cases: "if(sel == val[0]) goto target[0];" */
123 ir_node *val = new_Const_long(get_irn_mode(env->sel), curcases[0].value);
124 ir_node *cmp = new_Cmp(env->sel, val);
125 ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Eq);
126 ir_node *cond = new_Cond(proj);
130 set_Block_cfgpred(curcases[0].target, 0, new_Proj(cond, mode_X, pn_Cond_true));
131 in[0] = new_Proj(cond, mode_X, pn_Cond_false);
132 neblock = new_Block(1, in);
134 /* second part: "else if(sel == val[1]) goto target[1] else goto default;" */
135 val = new_Const_long(get_irn_mode(env->sel), curcases[1].value);
136 cmp = new_Cmp(env->sel, val);
137 proj = new_Proj(cmp, mode_b, pn_Cmp_Eq);
138 cond = new_Cond(proj);
139 set_Block_cfgpred(curcases[1].target, 0, new_Proj(cond, mode_X, pn_Cond_true));
140 env->defusers[env->defindex++] = new_Proj(cond, mode_X, pn_Cond_false);
142 /* recursive case: split cases in the middle */
143 int midcase = numcases / 2;
144 ir_node *val = new_Const_long(get_irn_mode(env->sel), curcases[midcase].value);
145 ir_node *cmp = new_Cmp(env->sel, val);
146 ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Lt);
147 ir_node *cond = new_Cond(proj);
152 in[0] = new_Proj(cond, mode_X, pn_Cond_true);
153 ltblock = new_Block(1, in);
155 set_cur_block(curblock);
156 in[0] = new_Proj(cond, mode_X, pn_Cond_false);
157 geblock = new_Block(1, in);
159 create_if_cascade(env, ltblock, curcases, midcase);
160 create_if_cascade(env, geblock, curcases + midcase, numcases - midcase);
165 * Block-Walker: searches for Cond nodes with a non-boolean mode
167 static void find_cond_nodes(ir_node *block, void *ctx)
169 walk_env_t *env = ctx;
175 int i, j = 0, numcases;
179 ir_node *defblock = NULL;
180 ifcas_env_t ifcas_env;
182 if(get_Block_n_cfgpreds(block) != 1)
185 projx = get_Block_cfgpred(block, 0);
188 assert(get_irn_mode(projx) == mode_X);
190 cond = get_Proj_pred(projx);
194 sel = get_Cond_selector(cond);
195 sel_mode = get_irn_mode(sel);
197 if(sel_mode == mode_b) /* not a switch? */
200 if(should_do_table_switch(cond, env->spare_size))
204 * Switch should be transformed into an if cascade.
205 * So first order the cases, so we can do a binary search on them.
209 numcases = get_irn_n_outs(cond) - 1; // does not contain default case
210 NEW_ARR_A(case_data_t, cases, numcases);
212 default_pn = get_Cond_defaultProj(cond);
214 ifcas_env.defindex = 0;
215 NEW_ARR_A(ir_node*, ifcas_env.defusers, numcases);
217 foreach_out_irn(cond, i, proj) {
218 long pn = get_Proj_proj(proj);
219 ir_node *target = get_irn_out(proj, 0);
220 assert(get_Block_n_cfgpreds(target) == 1 && "Encountered critical edge in switch");
229 cases[j].target = target;
233 assert(defblock != NULL && "Switch without default proj");
234 qsort(cases, numcases, sizeof(*cases), casecmp);
236 /* Now create the if cascade */
237 condblock = get_nodes_block(cond);
238 create_if_cascade(&ifcas_env, condblock, cases, numcases);
240 /* Connect new default case users */
241 set_irn_in(defblock, ifcas_env.defindex, ifcas_env.defusers);
245 * Lowers all Switches (Cond nodes with non-boolean mode) depending on spare_size.
246 * They will either remain the same or be converted into if-cascades.
248 * @param irg The ir graph to be lowered.
249 * @param spare_size Allowed spare size for table switches in machine words.
250 * (Default in edgfe: 128)
252 void lower_switch(ir_graph *irg, unsigned spare_size)
255 ir_graph *rem = current_ir_graph;
257 current_ir_graph = irg;
260 env.spare_size = spare_size;
262 remove_critical_cf_edges(irg);
263 assure_irg_outs(irg);
265 irg_block_walk_graph(irg, find_cond_nodes, NULL, &env);
268 /* control flow changed */
269 set_irg_outs_inconsistent(irg);
270 set_irg_doms_inconsistent(irg);
271 set_irg_extblk_inconsistent(irg);
272 set_irg_loopinfo_inconsistent(irg);
275 current_ir_graph = rem;