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];
171 static void normalize_table(ir_node *switchn, ir_mode *new_mode,
174 ir_switch_table *table = get_Switch_table(switchn);
175 size_t n_entries = ir_switch_table_get_n_entries(table);
177 /* adapt switch_table */
178 for (e = 0; e < n_entries; ++e) {
179 ir_switch_table_entry *entry = ir_switch_table_get_entry(table, e);
180 ir_tarval *min = entry->min;
185 min = tarval_convert_to(min, new_mode);
187 min = tarval_sub(min, delta, NULL);
189 if (entry->min == entry->max) {
193 ir_tarval *max = entry->max;
194 max = tarval_convert_to(max, new_mode);
196 max = tarval_sub(max, delta, NULL);
204 * normalize switch to work on an unsigned input with the first case at 0
206 static void normalize_switch(switch_info_t *info)
208 ir_node *switchn = info->switchn;
209 ir_graph *irg = get_irn_irg(switchn);
210 ir_node *block = get_nodes_block(switchn);
211 ir_node *selector = get_Switch_selector(switchn);
212 ir_mode *mode = get_irn_mode(selector);
213 ir_tarval *delta = NULL;
214 bool change_mode = false;
216 if (mode_is_signed(mode)) {
217 mode = find_unsigned_mode(mode);
218 selector = new_r_Conv(block, selector, mode);
222 /* normalize so switch_min is at 0 */
223 if (info->switch_min != 0) {
224 dbg_info *dbgi = get_irn_dbg_info(switchn);
227 delta = new_tarval_from_long(info->switch_min, mode);
229 min_const = new_r_Const(irg, delta);
230 selector = new_rd_Sub(dbgi, block, selector, min_const, mode);
232 info->switch_max -= info->switch_min;
233 info->switch_min = 0;
236 if (delta != NULL || change_mode) {
237 set_Switch_selector(switchn, selector);
238 normalize_table(switchn, mode, delta);
243 * Create an if (selector == caseval) Cond node (and handle the special case
246 static ir_node *create_case_cond(const ir_switch_table_entry *entry,
247 dbg_info *dbgi, ir_node *block,
250 ir_graph *irg = get_irn_irg(block);
251 ir_node *minconst = new_r_Const(irg, entry->min);
254 if (entry->min == entry->max) {
255 cmp = new_rd_Cmp(dbgi, block, selector, minconst, ir_relation_equal);
257 ir_tarval *adjusted_max = tarval_sub(entry->max, entry->min, NULL);
258 ir_node *sub = new_rd_Sub(dbgi, block, selector, minconst,
259 get_tarval_mode(adjusted_max));
260 ir_node *maxconst = new_r_Const(irg, adjusted_max);
261 cmp = new_rd_Cmp(dbgi, block, sub, maxconst, ir_relation_less_equal);
264 return new_rd_Cond(dbgi, block, cmp);
268 * Creates an if cascade realizing binary search.
270 static void create_if_cascade(switch_info_t *info, ir_node *block,
271 case_data_t *curcases, unsigned numcases)
273 ir_graph *irg = get_irn_irg(block);
274 const ir_node *switchn = info->switchn;
275 dbg_info *dbgi = get_irn_dbg_info(switchn);
276 ir_node *selector = get_Switch_selector(switchn);
279 /* zero cases: "goto default;" */
280 ARR_APP1(ir_node*, info->defusers, new_r_Jmp(block));
281 } else if (numcases == 1) {
282 /*only one case: "if (sel == val) goto target else goto default;"*/
283 const ir_switch_table_entry *entry = curcases[0].entry;
284 ir_node *cond = create_case_cond(entry, dbgi, block, selector);
285 ir_node *trueproj = new_r_Proj(cond, mode_X, pn_Cond_true);
286 ir_node *falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);
288 set_Block_cfgpred(curcases[0].target, 0, trueproj);
289 ARR_APP1(ir_node*, info->defusers, falseproj);
290 } else if (numcases == 2) {
291 /* only two cases: "if (sel == val[0]) goto target[0];" */
292 const ir_switch_table_entry *entry0 = curcases[0].entry;
293 const ir_switch_table_entry *entry1 = curcases[1].entry;
294 ir_node *cond = create_case_cond(entry0, dbgi, block, selector);
295 ir_node *trueproj = new_r_Proj(cond, mode_X, pn_Cond_true);
296 ir_node *falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);
300 set_Block_cfgpred(curcases[0].target, 0, trueproj);
303 neblock = new_r_Block(irg, 1, in);
305 /* second part: "else if (sel == val[1]) goto target[1] else goto default;" */
306 cond = create_case_cond(entry1, dbgi, neblock, selector);
307 trueproj = new_r_Proj(cond, mode_X, pn_Cond_true);
308 falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);
309 set_Block_cfgpred(curcases[1].target, 0, trueproj);
310 ARR_APP1(ir_node*, info->defusers, falseproj);
312 /* recursive case: split cases in the middle */
313 unsigned midcase = numcases / 2;
314 const ir_switch_table_entry *entry = curcases[midcase].entry;
315 ir_node *val = new_r_Const(irg, entry->min);
316 ir_node *cmp = new_rd_Cmp(dbgi, block, selector, val, ir_relation_less);
317 ir_node *cond = new_rd_Cond(dbgi, block, cmp);
322 in[0] = new_r_Proj(cond, mode_X, pn_Cond_true);
323 ltblock = new_r_Block(irg, 1, in);
325 in[0] = new_r_Proj(cond, mode_X, pn_Cond_false);
326 geblock = new_r_Block(irg, 1, in);
328 create_if_cascade(info, ltblock, curcases, midcase);
329 create_if_cascade(info, geblock, curcases + midcase, numcases - midcase);
333 static void create_out_of_bounds_check(switch_info_t *info)
335 ir_node *switchn = info->switchn;
336 ir_graph *irg = get_irn_irg(switchn);
337 dbg_info *dbgi = get_irn_dbg_info(switchn);
338 ir_node *selector = get_Switch_selector(switchn);
339 ir_node *block = get_nodes_block(switchn);
340 ir_mode *cmp_mode = get_irn_mode(selector);
341 ir_node **default_preds = NEW_ARR_F(ir_node*, 0);
342 ir_node *default_block = NULL;
352 size_t n_default_preds;
354 assert(info->switch_min == 0);
356 /* check for out-of-bounds */
357 max_const = new_r_Const_long(irg, cmp_mode, info->switch_max);
358 cmp = new_rd_Cmp(dbgi, block, selector, max_const, ir_relation_less_equal);
359 oob_cond = new_rd_Cond(dbgi, block, cmp);
360 proj_true = new_r_Proj(oob_cond, mode_X, pn_Cond_true);
361 proj_false = new_r_Proj(oob_cond, mode_X, pn_Cond_false);
363 ARR_APP1(ir_node*, default_preds, proj_false);
365 /* create new block containing the switch */
367 new_block = new_r_Block(irg, 1, in);
368 set_nodes_block(switchn, new_block);
371 foreach_out_irn(switchn, i, proj) {
372 long pn = get_Proj_proj(proj);
373 if (pn == pn_Switch_default) {
374 assert(default_block == NULL);
375 default_block = get_irn_out(proj, 0);
376 ARR_APP1(ir_node*, default_preds, proj);
378 set_nodes_block(proj, new_block);
381 /* adapt default block */
382 n_default_preds = ARR_LEN(default_preds);
383 if (n_default_preds > 1) {
384 /* create new intermediate blocks so we don't have critical edges */
386 for (p = 0; p < n_default_preds; ++p) {
387 ir_node *pred = default_preds[p];
388 ir_node *split_block;
389 ir_node *block_in[1];
392 split_block = new_r_Block(irg, 1, block_in);
394 default_preds[p] = new_r_Jmp(split_block);
397 set_irn_in(default_block, n_default_preds, default_preds);
399 DEL_ARR_F(default_preds);
401 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE
402 | IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
406 * Block-Walker: searches for Switch nodes
408 static void find_switch_nodes(ir_node *block, void *ctx)
410 walk_env_t *env = (walk_env_t *)ctx;
415 bool lower_switch = false;
418 /* because we split critical blocks only blocks with 1 predecessors may
419 * contain Proj->Cond nodes */
420 if (get_Block_n_cfgpreds(block) != 1)
423 projx = get_Block_cfgpred(block, 0);
426 assert(get_irn_mode(projx) == mode_X);
428 switchn = get_Proj_pred(projx);
429 if (!is_Switch(switchn))
432 if (ir_nodeset_contains(&env->processed, switchn))
434 ir_nodeset_insert(&env->processed, switchn);
436 could_analyze = analyse_switch0(&info, switchn);
437 /* the code can't handle values which are not representable in the host */
438 if (!could_analyze) {
439 ir_fprintf(stderr, "libfirm warning: Couldn't analyse %+F (this could go wrong in the backend)\n", switchn);
444 * Here we have: num_cases and [switch_min, switch_max] interval.
445 * We do an if-cascade if there are too many spare numbers.
447 spare = (unsigned long) info.switch_max
448 - (unsigned long) info.switch_min
449 - (unsigned long) info.num_cases + 1;
450 lower_switch |= spare >= env->spare_size;
451 lower_switch |= info.num_cases <= env->small_switch;
454 /* we won't decompose the switch. But we might have to add
455 * out-of-bounds checking */
456 if (!env->allow_out_of_bounds) {
457 normalize_switch(&info);
458 create_out_of_bounds_check(&info);
463 normalize_switch(&info);
464 analyse_switch1(&info);
466 /* Now create the if cascade */
468 info.defusers = NEW_ARR_F(ir_node*, 0);
469 block = get_nodes_block(switchn);
470 create_if_cascade(&info, block, info.cases, info.num_cases);
472 /* Connect new default case users */
473 set_irn_in(info.default_block, ARR_LEN(info.defusers), info.defusers);
475 DEL_ARR_F(info.defusers);
477 clear_irg_state(get_irn_irg(block), IR_GRAPH_STATE_NO_CRITICAL_EDGES
478 | IR_GRAPH_STATE_CONSISTENT_DOMINANCE
479 | IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
482 void lower_switch(ir_graph *irg, unsigned small_switch, unsigned spare_size,
483 int allow_out_of_bounds)
487 env.spare_size = spare_size;
488 env.small_switch = small_switch;
489 env.allow_out_of_bounds = allow_out_of_bounds;
490 ir_nodeset_init(&env.processed);
492 remove_critical_cf_edges(irg);
493 assure_irg_outs(irg);
495 irg_block_walk_graph(irg, find_switch_nodes, NULL, &env);
496 ir_nodeset_destroy(&env.processed);