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 changed; /**< indicates whether a change was performed */
48 ir_nodeset_t processed;
51 typedef struct case_data_t {
52 const ir_switch_table_entry *entry;
56 typedef struct switch_info_t {
58 ir_tarval *switch_min;
59 ir_tarval *switch_max;
60 ir_node *default_block;
63 ir_node **defusers; /**< the Projs pointing to the default case */
67 * analyze enough to decide if we should lower the switch
69 static void analyse_switch0(switch_info_t *info, ir_node *switchn)
71 const ir_switch_table *table = get_Switch_table(switchn);
72 size_t n_entries = ir_switch_table_get_n_entries(table);
73 ir_mode *mode = get_irn_mode(get_Switch_selector(switchn));
74 ir_tarval *switch_min = get_mode_max(mode);
75 ir_tarval *switch_max = get_mode_min(mode);
76 unsigned num_cases = 0;
78 for (size_t e = 0; e < n_entries; ++e) {
79 const ir_switch_table_entry *entry
80 = ir_switch_table_get_entry_const(table, e);
84 if (tarval_cmp(entry->min, switch_min) == ir_relation_less)
85 switch_min = entry->min;
86 if (tarval_cmp(entry->max, switch_max) == ir_relation_greater)
87 switch_max = entry->max;
92 info->switchn = switchn;
93 info->switch_min = switch_min;
94 info->switch_max = switch_max;
95 info->num_cases = num_cases;
98 static int casecmp(const void *a, const void *b)
100 const case_data_t *cda = (const case_data_t*)a;
101 const case_data_t *cdb = (const case_data_t*)b;
102 const ir_switch_table_entry *ea = cda->entry;
103 const ir_switch_table_entry *eb = cdb->entry;
108 if (tarval_cmp(ea->max, eb->min) == ir_relation_less)
110 /* cases must be non overlapping, so the only remaining case is greater */
111 assert(tarval_cmp(ea->min, eb->max) == ir_relation_greater);
116 * Analyse the stuff that anayse_switch0() left out
118 static void analyse_switch1(switch_info_t *info)
120 const ir_node *switchn = info->switchn;
121 const ir_switch_table *table = get_Switch_table(switchn);
122 size_t n_entries = ir_switch_table_get_n_entries(table);
123 unsigned n_outs = get_Switch_n_outs(switchn);
124 ir_node **targets = XMALLOCNZ(ir_node*, n_outs);
125 unsigned num_cases = info->num_cases;
126 case_data_t *cases = XMALLOCN(case_data_t, num_cases);
132 foreach_out_irn(switchn, i, proj) {
133 long pn = get_Proj_proj(proj);
134 ir_node *target = get_irn_out(proj, 0);
136 assert((unsigned)pn < n_outs);
137 assert(targets[(unsigned)pn] == NULL);
138 targets[(unsigned)pn] = target;
141 for (e = 0; e < n_entries; ++e) {
142 const ir_switch_table_entry *entry
143 = ir_switch_table_get_entry_const(table, e);
147 cases[c].entry = entry;
148 cases[c].target = targets[entry->pn];
151 assert(c == num_cases);
154 * Switch should be transformed into an if cascade.
155 * So first order the cases, so we can do a binary search on them.
157 qsort(cases, num_cases, sizeof(cases[0]), casecmp);
159 info->default_block = targets[pn_Switch_default];
164 static void normalize_table(ir_node *switchn, ir_mode *new_mode,
167 ir_switch_table *table = get_Switch_table(switchn);
168 size_t n_entries = ir_switch_table_get_n_entries(table);
170 /* adapt switch_table */
171 for (e = 0; e < n_entries; ++e) {
172 ir_switch_table_entry *entry = ir_switch_table_get_entry(table, e);
173 ir_tarval *min = entry->min;
178 min = tarval_convert_to(min, new_mode);
180 min = tarval_sub(min, delta, NULL);
182 if (entry->min == entry->max) {
186 ir_tarval *max = entry->max;
187 max = tarval_convert_to(max, new_mode);
189 max = tarval_sub(max, delta, NULL);
197 * normalize switch to work on an unsigned input with the first case at 0
199 static void normalize_switch(switch_info_t *info)
201 ir_node *switchn = info->switchn;
202 ir_graph *irg = get_irn_irg(switchn);
203 ir_node *block = get_nodes_block(switchn);
204 ir_node *selector = get_Switch_selector(switchn);
205 ir_mode *switch_mode = get_irn_mode(selector);
206 ir_mode *mode = switch_mode;
207 ir_tarval *delta = NULL;
208 bool needs_normalize = false;
210 ir_tarval *min = info->switch_min;
211 if (mode_is_signed(mode)) {
212 mode = find_unsigned_mode(mode);
213 selector = new_r_Conv(block, selector, mode);
214 min = tarval_convert_to(min, mode);
215 needs_normalize = true;
216 info->switch_max = tarval_convert_to(info->switch_max, mode);
219 /* normalize so switch_min is at 0 */
220 if (min != get_mode_null(mode)) {
221 ir_node *min_const = new_r_Const(irg, min);
222 dbg_info *dbgi = get_irn_dbg_info(switchn);
223 selector = new_rd_Sub(dbgi, block, selector, min_const, mode);
225 info->switch_max = tarval_sub(info->switch_max, min, mode);
226 info->switch_min = get_mode_null(mode);
229 needs_normalize = true;
232 if (needs_normalize) {
233 set_Switch_selector(switchn, selector);
234 normalize_table(switchn, mode, delta);
239 * Create an if (selector == caseval) Cond node (and handle the special case
242 static ir_node *create_case_cond(const ir_switch_table_entry *entry,
243 dbg_info *dbgi, ir_node *block,
246 ir_graph *irg = get_irn_irg(block);
247 ir_node *minconst = new_r_Const(irg, entry->min);
250 if (entry->min == entry->max) {
251 cmp = new_rd_Cmp(dbgi, block, selector, minconst, ir_relation_equal);
253 ir_tarval *adjusted_max = tarval_sub(entry->max, entry->min, NULL);
254 ir_node *sub = new_rd_Sub(dbgi, block, selector, minconst,
255 get_tarval_mode(adjusted_max));
256 ir_node *maxconst = new_r_Const(irg, adjusted_max);
257 cmp = new_rd_Cmp(dbgi, block, sub, maxconst, ir_relation_less_equal);
260 return new_rd_Cond(dbgi, block, cmp);
264 * Creates an if cascade realizing binary search.
266 static void create_if_cascade(switch_info_t *info, ir_node *block,
267 case_data_t *curcases, unsigned numcases)
269 ir_graph *irg = get_irn_irg(block);
270 const ir_node *switchn = info->switchn;
271 dbg_info *dbgi = get_irn_dbg_info(switchn);
272 ir_node *selector = get_Switch_selector(switchn);
275 /* zero cases: "goto default;" */
276 ARR_APP1(ir_node*, info->defusers, new_r_Jmp(block));
277 } else if (numcases == 1) {
278 /*only one case: "if (sel == val) goto target else goto default;"*/
279 const ir_switch_table_entry *entry = curcases[0].entry;
280 ir_node *cond = create_case_cond(entry, dbgi, block, selector);
281 ir_node *trueproj = new_r_Proj(cond, mode_X, pn_Cond_true);
282 ir_node *falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);
284 set_Block_cfgpred(curcases[0].target, 0, trueproj);
285 ARR_APP1(ir_node*, info->defusers, falseproj);
286 } else if (numcases == 2) {
287 /* only two cases: "if (sel == val[0]) goto target[0];" */
288 const ir_switch_table_entry *entry0 = curcases[0].entry;
289 const ir_switch_table_entry *entry1 = curcases[1].entry;
290 ir_node *cond = create_case_cond(entry0, dbgi, block, selector);
291 ir_node *trueproj = new_r_Proj(cond, mode_X, pn_Cond_true);
292 ir_node *falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);
296 set_Block_cfgpred(curcases[0].target, 0, trueproj);
299 neblock = new_r_Block(irg, 1, in);
301 /* second part: "else if (sel == val[1]) goto target[1] else goto default;" */
302 cond = create_case_cond(entry1, dbgi, neblock, selector);
303 trueproj = new_r_Proj(cond, mode_X, pn_Cond_true);
304 falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);
305 set_Block_cfgpred(curcases[1].target, 0, trueproj);
306 ARR_APP1(ir_node*, info->defusers, falseproj);
308 /* recursive case: split cases in the middle */
309 unsigned midcase = numcases / 2;
310 const ir_switch_table_entry *entry = curcases[midcase].entry;
311 ir_node *val = new_r_Const(irg, entry->min);
312 ir_node *cmp = new_rd_Cmp(dbgi, block, selector, val, ir_relation_less);
313 ir_node *cond = new_rd_Cond(dbgi, block, cmp);
318 in[0] = new_r_Proj(cond, mode_X, pn_Cond_true);
319 ltblock = new_r_Block(irg, 1, in);
321 in[0] = new_r_Proj(cond, mode_X, pn_Cond_false);
322 geblock = new_r_Block(irg, 1, in);
324 create_if_cascade(info, ltblock, curcases, midcase);
325 create_if_cascade(info, geblock, curcases + midcase, numcases - midcase);
329 static void create_out_of_bounds_check(switch_info_t *info)
331 ir_node *switchn = info->switchn;
332 ir_graph *irg = get_irn_irg(switchn);
333 dbg_info *dbgi = get_irn_dbg_info(switchn);
334 ir_node *selector = get_Switch_selector(switchn);
335 ir_node *block = get_nodes_block(switchn);
336 ir_node **default_preds = NEW_ARR_F(ir_node*, 0);
337 ir_node *default_block = NULL;
347 size_t n_default_preds;
349 assert(tarval_is_null(info->switch_min));
351 /* check for out-of-bounds */
352 max_const = new_r_Const(irg, info->switch_max);
353 cmp = new_rd_Cmp(dbgi, block, selector, max_const, ir_relation_less_equal);
354 oob_cond = new_rd_Cond(dbgi, block, cmp);
355 proj_true = new_r_Proj(oob_cond, mode_X, pn_Cond_true);
356 proj_false = new_r_Proj(oob_cond, mode_X, pn_Cond_false);
358 ARR_APP1(ir_node*, default_preds, proj_false);
360 /* create new block containing the switch */
362 new_block = new_r_Block(irg, 1, in);
363 set_nodes_block(switchn, new_block);
366 foreach_out_irn(switchn, i, proj) {
367 long pn = get_Proj_proj(proj);
368 if (pn == pn_Switch_default) {
369 assert(default_block == NULL);
370 default_block = get_irn_out(proj, 0);
371 ARR_APP1(ir_node*, default_preds, proj);
373 set_nodes_block(proj, new_block);
376 /* adapt default block */
377 n_default_preds = ARR_LEN(default_preds);
378 if (n_default_preds > 1) {
379 /* create new intermediate blocks so we don't have critical edges */
381 for (p = 0; p < n_default_preds; ++p) {
382 ir_node *pred = default_preds[p];
383 ir_node *split_block;
384 ir_node *block_in[1];
387 split_block = new_r_Block(irg, 1, block_in);
389 default_preds[p] = new_r_Jmp(split_block);
392 set_irn_in(default_block, n_default_preds, default_preds);
394 DEL_ARR_F(default_preds);
396 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
400 * Block-Walker: searches for Switch nodes
402 static void find_switch_nodes(ir_node *block, void *ctx)
404 walk_env_t *env = (walk_env_t *)ctx;
409 /* because we split critical blocks only blocks with 1 predecessors may
410 * contain Proj->Cond nodes */
411 if (get_Block_n_cfgpreds(block) != 1)
414 projx = get_Block_cfgpred(block, 0);
417 assert(get_irn_mode(projx) == mode_X);
419 switchn = get_Proj_pred(projx);
420 if (!is_Switch(switchn))
423 if (ir_nodeset_contains(&env->processed, switchn))
425 ir_nodeset_insert(&env->processed, switchn);
427 analyse_switch0(&info, switchn);
430 * Here we have: num_cases and [switch_min, switch_max] interval.
431 * We do an if-cascade if there are too many spare numbers.
433 ir_mode *mode = get_irn_mode(get_Switch_selector(switchn));
434 ir_tarval *spare = tarval_sub(info.switch_max, info.switch_min, mode);
435 mode = find_unsigned_mode(mode);
436 spare = tarval_convert_to(spare, mode);
437 ir_tarval *num_cases_minus_one
438 = new_tarval_from_long(info.num_cases-1, mode);
439 spare = tarval_sub(spare, num_cases_minus_one, mode);
440 ir_tarval *spare_size = new_tarval_from_long(env->spare_size, mode);
441 bool lower_switch = (info.num_cases <= env->small_switch
442 || (tarval_cmp(spare, spare_size) & ir_relation_greater_equal));
445 /* we won't decompose the switch. But we must add an out-of-bounds
447 normalize_switch(&info);
448 create_out_of_bounds_check(&info);
452 normalize_switch(&info);
453 analyse_switch1(&info);
455 /* Now create the if cascade */
457 info.defusers = NEW_ARR_F(ir_node*, 0);
458 block = get_nodes_block(switchn);
459 create_if_cascade(&info, block, info.cases, info.num_cases);
461 /* Connect new default case users */
462 set_irn_in(info.default_block, ARR_LEN(info.defusers), info.defusers);
464 DEL_ARR_F(info.defusers);
466 clear_irg_properties(get_irn_irg(block), IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES
467 | IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
470 void lower_switch(ir_graph *irg, unsigned small_switch, unsigned spare_size)
474 env.spare_size = spare_size;
475 env.small_switch = small_switch;
476 ir_nodeset_init(&env.processed);
478 remove_critical_cf_edges(irg);
479 assure_irg_outs(irg);
481 irg_block_walk_graph(irg, find_switch_nodes, NULL, &env);
482 ir_nodeset_destroy(&env.processed);