2 * Copyright (C) 1995-2011 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 #include "irnodeset.h"
41 #define foreach_out_irn(irn, i, outirn) for (i = get_irn_n_outs(irn) - 1;\
42 i >= 0 && (outirn = get_irn_out(irn, i)); --i)
44 typedef struct walk_env_t {
45 unsigned spare_size; /**< the allowed spare size for table switches */
46 unsigned small_switch;
47 bool allow_out_of_bounds;
48 bool changed; /**< indicates whether a change was performed */
49 ir_nodeset_t processed;
52 typedef struct case_data_t {
53 const ir_switch_table_entry *entry;
57 typedef struct switch_info_t {
61 ir_node *default_block;
64 ir_node **defusers; /**< the Projs pointing to the default case */
68 * analyze enough to decide if we should lower the switch
70 static bool analyse_switch0(switch_info_t *info, ir_node *switchn)
72 const ir_switch_table *table = get_Switch_table(switchn);
73 size_t n_entries = ir_switch_table_get_n_entries(table);
74 long switch_min = LONG_MAX;
75 long switch_max = LONG_MIN;
76 unsigned num_cases = 0;
79 for (e = 0; e < n_entries; ++e) {
80 const ir_switch_table_entry *entry
81 = ir_switch_table_get_entry_const(table, e);
87 if (!tarval_is_long(entry->min) || !tarval_is_long(entry->max))
89 minval = get_tarval_long(entry->min);
90 maxval = get_tarval_long(entry->max);
91 if (minval < switch_min)
93 if (maxval > switch_max)
98 info->switchn = switchn;
99 info->switch_min = switch_min;
100 info->switch_max = switch_max;
101 info->num_cases = num_cases;
105 static int casecmp(const void *a, const void *b)
107 const case_data_t *cda = (const case_data_t*)a;
108 const case_data_t *cdb = (const case_data_t*)b;
109 const ir_switch_table_entry *ea = cda->entry;
110 const ir_switch_table_entry *eb = cdb->entry;
115 if (tarval_cmp(ea->max, eb->min) == ir_relation_less)
117 /* cases must be non overlapping, so the only remaining case is greater */
118 assert(tarval_cmp(ea->min, eb->max) == ir_relation_greater);
123 * Analyse the stuff that anayse_switch0() left out
125 static void analyse_switch1(switch_info_t *info)
127 const ir_node *switchn = info->switchn;
128 const ir_switch_table *table = get_Switch_table(switchn);
129 size_t n_entries = ir_switch_table_get_n_entries(table);
130 unsigned n_outs = get_Switch_n_outs(switchn);
131 ir_node **targets = XMALLOCNZ(ir_node*, n_outs);
132 unsigned num_cases = info->num_cases;
133 case_data_t *cases = XMALLOCN(case_data_t, num_cases);
139 foreach_out_irn(switchn, i, proj) {
140 long pn = get_Proj_proj(proj);
141 ir_node *target = get_irn_out(proj, 0);
143 assert((unsigned)pn < n_outs);
144 assert(targets[(unsigned)pn] == NULL);
145 targets[(unsigned)pn] = target;
148 for (e = 0; e < n_entries; ++e) {
149 const ir_switch_table_entry *entry
150 = ir_switch_table_get_entry_const(table, e);
154 cases[c].entry = entry;
155 cases[c].target = targets[entry->pn];
158 assert(c == num_cases);
161 * Switch should be transformed into an if cascade.
162 * So first order the cases, so we can do a binary search on them.
164 qsort(cases, num_cases, sizeof(cases[0]), casecmp);
166 info->default_block = targets[pn_Switch_default];
170 static void normalize_table(ir_node *switchn, ir_mode *new_mode,
173 ir_switch_table *table = get_Switch_table(switchn);
174 size_t n_entries = ir_switch_table_get_n_entries(table);
176 /* adapt switch_table */
177 for (e = 0; e < n_entries; ++e) {
178 ir_switch_table_entry *entry = ir_switch_table_get_entry(table, e);
179 ir_tarval *min = entry->min;
184 min = tarval_convert_to(min, new_mode);
186 min = tarval_sub(min, delta, NULL);
188 if (entry->min == entry->max) {
192 ir_tarval *max = entry->max;
193 max = tarval_convert_to(max, new_mode);
195 max = tarval_sub(max, delta, NULL);
203 * normalize switch to work on an unsigned input with the first case at 0
205 static void normalize_switch(switch_info_t *info)
207 ir_node *switchn = info->switchn;
208 ir_graph *irg = get_irn_irg(switchn);
209 ir_node *block = get_nodes_block(switchn);
210 ir_node *selector = get_Switch_selector(switchn);
211 ir_mode *mode = get_irn_mode(selector);
212 ir_tarval *delta = NULL;
213 bool change_mode = false;
215 if (mode_is_signed(mode)) {
216 mode = find_unsigned_mode(mode);
217 selector = new_r_Conv(block, selector, mode);
221 /* normalize so switch_min is at 0 */
222 if (info->switch_min != 0) {
223 dbg_info *dbgi = get_irn_dbg_info(switchn);
226 delta = new_tarval_from_long(info->switch_min, mode);
228 min_const = new_r_Const(irg, delta);
229 selector = new_rd_Sub(dbgi, block, selector, min_const, mode);
231 info->switch_max -= info->switch_min;
232 info->switch_min = 0;
235 if (delta != NULL || change_mode) {
236 set_Switch_selector(switchn, selector);
237 normalize_table(switchn, mode, delta);
242 * Create an if (selector == caseval) Cond node (and handle the special case
245 static ir_node *create_case_cond(const ir_switch_table_entry *entry,
246 dbg_info *dbgi, ir_node *block,
249 ir_graph *irg = get_irn_irg(block);
250 ir_node *minconst = new_r_Const(irg, entry->min);
253 if (entry->min == entry->max) {
254 cmp = new_rd_Cmp(dbgi, block, selector, minconst, ir_relation_equal);
256 ir_tarval *adjusted_max = tarval_sub(entry->max, entry->min, NULL);
257 ir_node *sub = new_rd_Sub(dbgi, block, selector, minconst,
258 get_tarval_mode(adjusted_max));
259 ir_node *maxconst = new_r_Const(irg, adjusted_max);
260 cmp = new_rd_Cmp(dbgi, block, sub, maxconst, ir_relation_less_equal);
263 return new_rd_Cond(dbgi, block, cmp);
267 * Creates an if cascade realizing binary search.
269 static void create_if_cascade(switch_info_t *info, ir_node *block,
270 case_data_t *curcases, unsigned numcases)
272 ir_graph *irg = get_irn_irg(block);
273 const ir_node *switchn = info->switchn;
274 dbg_info *dbgi = get_irn_dbg_info(switchn);
275 ir_node *selector = get_Switch_selector(switchn);
278 /* zero cases: "goto default;" */
279 ARR_APP1(ir_node*, info->defusers, new_r_Jmp(block));
280 } else if (numcases == 1) {
281 /*only one case: "if (sel == val) goto target else goto default;"*/
282 const ir_switch_table_entry *entry = curcases[0].entry;
283 ir_node *cond = create_case_cond(entry, dbgi, block, selector);
284 ir_node *trueproj = new_r_Proj(cond, mode_X, pn_Cond_true);
285 ir_node *falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);
287 set_Block_cfgpred(curcases[0].target, 0, trueproj);
288 ARR_APP1(ir_node*, info->defusers, falseproj);
289 } else if (numcases == 2) {
290 /* only two cases: "if (sel == val[0]) goto target[0];" */
291 const ir_switch_table_entry *entry0 = curcases[0].entry;
292 const ir_switch_table_entry *entry1 = curcases[1].entry;
293 ir_node *cond = create_case_cond(entry0, dbgi, block, selector);
294 ir_node *trueproj = new_r_Proj(cond, mode_X, pn_Cond_true);
295 ir_node *falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);
299 set_Block_cfgpred(curcases[0].target, 0, trueproj);
302 neblock = new_r_Block(irg, 1, in);
304 /* second part: "else if (sel == val[1]) goto target[1] else goto default;" */
305 cond = create_case_cond(entry1, dbgi, neblock, selector);
306 trueproj = new_r_Proj(cond, mode_X, pn_Cond_true);
307 falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);
308 set_Block_cfgpred(curcases[1].target, 0, trueproj);
309 ARR_APP1(ir_node*, info->defusers, falseproj);
311 /* recursive case: split cases in the middle */
312 unsigned midcase = numcases / 2;
313 const ir_switch_table_entry *entry = curcases[midcase].entry;
314 ir_node *val = new_r_Const(irg, entry->min);
315 ir_node *cmp = new_rd_Cmp(dbgi, block, selector, val, ir_relation_less);
316 ir_node *cond = new_rd_Cond(dbgi, block, cmp);
321 in[0] = new_r_Proj(cond, mode_X, pn_Cond_true);
322 ltblock = new_r_Block(irg, 1, in);
324 in[0] = new_r_Proj(cond, mode_X, pn_Cond_false);
325 geblock = new_r_Block(irg, 1, in);
327 create_if_cascade(info, ltblock, curcases, midcase);
328 create_if_cascade(info, geblock, curcases + midcase, numcases - midcase);
332 static void create_out_of_bounds_check(switch_info_t *info)
334 ir_node *switchn = info->switchn;
335 ir_graph *irg = get_irn_irg(switchn);
336 dbg_info *dbgi = get_irn_dbg_info(switchn);
337 ir_node *selector = get_Switch_selector(switchn);
338 ir_node *block = get_nodes_block(switchn);
339 ir_mode *cmp_mode = get_irn_mode(selector);
340 ir_node **default_preds = NEW_ARR_F(ir_node*, 0);
341 ir_node *default_block = NULL;
351 size_t n_default_preds;
353 assert(info->switch_min == 0);
355 /* check for out-of-bounds */
356 max_const = new_r_Const_long(irg, cmp_mode, info->switch_max);
357 cmp = new_rd_Cmp(dbgi, block, selector, max_const, ir_relation_less_equal);
358 oob_cond = new_rd_Cond(dbgi, block, cmp);
359 proj_true = new_r_Proj(oob_cond, mode_X, pn_Cond_true);
360 proj_false = new_r_Proj(oob_cond, mode_X, pn_Cond_false);
362 ARR_APP1(ir_node*, default_preds, proj_false);
364 /* create new block containing the switch */
366 new_block = new_r_Block(irg, 1, in);
367 set_nodes_block(switchn, new_block);
370 foreach_out_irn(switchn, i, proj) {
371 long pn = get_Proj_proj(proj);
372 if (pn == pn_Switch_default) {
373 assert(default_block == NULL);
374 default_block = get_irn_out(proj, 0);
375 ARR_APP1(ir_node*, default_preds, proj);
377 set_nodes_block(proj, new_block);
380 /* adapt default block */
381 n_default_preds = ARR_LEN(default_preds);
382 if (n_default_preds > 1) {
383 /* create new intermediate blocks so we don't have critical edges */
385 for (p = 0; p < n_default_preds; ++p) {
386 ir_node *pred = default_preds[p];
387 ir_node *split_block;
388 ir_node *block_in[1];
391 split_block = new_r_Block(irg, 1, block_in);
393 default_preds[p] = new_r_Jmp(split_block);
396 set_irn_in(default_block, n_default_preds, default_preds);
398 DEL_ARR_F(default_preds);
400 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE
401 | IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
405 * Block-Walker: searches for Switch nodes
407 static void find_switch_nodes(ir_node *block, void *ctx)
409 walk_env_t *env = (walk_env_t *)ctx;
414 bool lower_switch = false;
417 /* because we split critical blocks only blocks with 1 predecessors may
418 * contain Proj->Cond nodes */
419 if (get_Block_n_cfgpreds(block) != 1)
422 projx = get_Block_cfgpred(block, 0);
425 assert(get_irn_mode(projx) == mode_X);
427 switchn = get_Proj_pred(projx);
428 if (!is_Switch(switchn))
431 if (ir_nodeset_contains(&env->processed, switchn))
433 ir_nodeset_insert(&env->processed, switchn);
435 could_analyze = analyse_switch0(&info, switchn);
436 /* the code can't handle values which are not representable in the host */
437 if (!could_analyze) {
438 ir_fprintf(stderr, "libfirm warning: Couldn't analyse %+F (this could go wrong in the backend)\n", switchn);
443 * Here we have: num_cases and [switch_min, switch_max] interval.
444 * We do an if-cascade if there are too many spare numbers.
446 spare = (unsigned long) info.switch_max
447 - (unsigned long) info.switch_min
448 - (unsigned long) info.num_cases + 1;
449 lower_switch |= spare >= env->spare_size;
450 lower_switch |= info.num_cases <= env->small_switch;
453 /* we won't decompose the switch. But we might have to add
454 * out-of-bounds checking */
455 if (!env->allow_out_of_bounds) {
456 normalize_switch(&info);
457 create_out_of_bounds_check(&info);
462 normalize_switch(&info);
463 analyse_switch1(&info);
465 /* Now create the if cascade */
467 info.defusers = NEW_ARR_F(ir_node*, 0);
468 block = get_nodes_block(switchn);
469 create_if_cascade(&info, block, info.cases, info.num_cases);
471 /* Connect new default case users */
472 set_irn_in(info.default_block, ARR_LEN(info.defusers), info.defusers);
474 DEL_ARR_F(info.defusers);
476 clear_irg_state(get_irn_irg(block), IR_GRAPH_STATE_NO_CRITICAL_EDGES
477 | IR_GRAPH_STATE_CONSISTENT_DOMINANCE
478 | IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
481 void lower_switch(ir_graph *irg, unsigned small_switch, unsigned spare_size,
482 int allow_out_of_bounds)
486 env.spare_size = spare_size;
487 env.small_switch = small_switch;
488 env.allow_out_of_bounds = allow_out_of_bounds;
489 ir_nodeset_init(&env.processed);
491 remove_critical_cf_edges(irg);
492 assure_irg_outs(irg);
494 irg_block_walk_graph(irg, find_switch_nodes, NULL, &env);
495 ir_nodeset_destroy(&env.processed);