remove $Id$, it doesn't work with git anyway
[libfirm] / ir / lower / lower_switch.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Lowering of Switches if necessary or advantageous.
23  * @author  Moritz Kroll
24  */
25 #include "config.h"
26
27 #include <limits.h>
28 #include <stdbool.h>
29
30 #include "array_t.h"
31 #include "ircons.h"
32 #include "irgopt.h"
33 #include "irgwalk.h"
34 #include "irnode_t.h"
35 #include "irouts.h"
36 #include "irpass_t.h"
37 #include "lowering.h"
38 #include "error.h"
39 #include "irnodeset.h"
40
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)
43
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;
50 } walk_env_t;
51
52 typedef struct case_data_t {
53         const ir_switch_table_entry *entry;
54         ir_node                     *target;
55 } case_data_t;
56
57 typedef struct switch_info_t {
58         ir_node     *switchn;
59         long         switch_min;
60         long         switch_max;
61         ir_node     *default_block;
62         unsigned     num_cases;
63         case_data_t *cases;
64         ir_node    **defusers;    /**< the Projs pointing to the default case */
65 } switch_info_t;
66
67 /**
68  * analyze enough to decide if we should lower the switch
69  */
70 static bool analyse_switch0(switch_info_t *info, ir_node *switchn)
71 {
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;
77         size_t                 e;
78
79         for (e = 0; e < n_entries; ++e) {
80                 const ir_switch_table_entry *entry
81                         = ir_switch_table_get_entry_const(table, e);
82                 long minval;
83                 long maxval;
84                 if (entry->pn == 0)
85                         continue;
86
87                 if (!tarval_is_long(entry->min) || !tarval_is_long(entry->max))
88                         return false;
89                 minval = get_tarval_long(entry->min);
90                 maxval = get_tarval_long(entry->max);
91                 if (minval < switch_min)
92                         switch_min = minval;
93                 if (maxval > switch_max)
94                         switch_max = maxval;
95                 ++num_cases;
96         }
97
98         info->switchn    = switchn;
99         info->switch_min = switch_min;
100         info->switch_max = switch_max;
101         info->num_cases  = num_cases;
102         return true;
103 }
104
105 static int casecmp(const void *a, const void *b)
106 {
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;
111
112         if (ea == eb)
113                 return 0;
114
115         if (tarval_cmp(ea->max, eb->min) == ir_relation_less)
116                 return -1;
117         /* cases must be non overlapping, so the only remaining case is greater */
118         assert(tarval_cmp(ea->min, eb->max) == ir_relation_greater);
119         return 1;
120 }
121
122 /**
123  * Analyse the stuff that anayse_switch0() left out
124  */
125 static void analyse_switch1(switch_info_t *info)
126 {
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);
134         unsigned               c         = 0;
135         size_t                 e;
136         int                    i;
137         ir_node               *proj;
138
139         foreach_out_irn(switchn, i, proj) {
140                 long     pn     = get_Proj_proj(proj);
141                 ir_node *target = get_irn_out(proj, 0);
142
143                 assert((unsigned)pn < n_outs);
144                 assert(targets[(unsigned)pn] == NULL);
145                 targets[(unsigned)pn] = target;
146         }
147
148         for (e = 0; e < n_entries; ++e) {
149                 const ir_switch_table_entry *entry
150                         = ir_switch_table_get_entry_const(table, e);
151                 if (entry->pn == 0)
152                         continue;
153
154                 cases[c].entry  = entry;
155                 cases[c].target = targets[entry->pn];
156                 ++c;
157         }
158         assert(c == num_cases);
159
160         /*
161          * Switch should be transformed into an if cascade.
162          * So first order the cases, so we can do a binary search on them.
163          */
164         qsort(cases, num_cases, sizeof(cases[0]), casecmp);
165
166         info->default_block = targets[pn_Switch_default];
167         info->cases         = cases;
168 }
169
170 static void normalize_table(ir_node *switchn, ir_mode *new_mode,
171                             ir_tarval *delta)
172 {
173         ir_switch_table *table     = get_Switch_table(switchn);
174         size_t           n_entries = ir_switch_table_get_n_entries(table);
175         size_t           e;
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;
180
181                 if (entry->pn == 0)
182                         continue;
183
184                 min = tarval_convert_to(min, new_mode);
185                 if (delta != NULL)
186                         min = tarval_sub(min, delta, NULL);
187
188                 if (entry->min == entry->max) {
189                         entry->min = min;
190                         entry->max = min;
191                 } else {
192                         ir_tarval *max = entry->max;
193                         max = tarval_convert_to(max, new_mode);
194                         if (delta != NULL)
195                                 max = tarval_sub(max, delta, NULL);
196                         entry->min = min;
197                         entry->max = max;
198                 }
199         }
200 }
201
202 /**
203  * normalize switch to work on an unsigned input with the first case at 0
204  */
205 static void normalize_switch(switch_info_t *info)
206 {
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;
214
215         if (mode_is_signed(mode)) {
216                 mode        = find_unsigned_mode(mode);
217                 selector    = new_r_Conv(block, selector, mode);
218                 change_mode = true;
219         }
220
221         /* normalize so switch_min is at 0 */
222         if (info->switch_min != 0) {
223                 dbg_info *dbgi = get_irn_dbg_info(switchn);
224                 ir_node  *min_const;
225
226                 delta = new_tarval_from_long(info->switch_min, mode);
227
228                 min_const = new_r_Const(irg, delta);
229                 selector  = new_rd_Sub(dbgi, block, selector, min_const, mode);
230
231                 info->switch_max -= info->switch_min;
232                 info->switch_min  = 0;
233         }
234
235         if (delta != NULL || change_mode) {
236                 set_Switch_selector(switchn, selector);
237                 normalize_table(switchn, mode, delta);
238         }
239 }
240
241 /**
242  * Create an if (selector == caseval) Cond node (and handle the special case
243  * of ranged cases)
244  */
245 static ir_node *create_case_cond(const ir_switch_table_entry *entry,
246                                  dbg_info *dbgi, ir_node *block,
247                                  ir_node *selector)
248 {
249         ir_graph *irg      = get_irn_irg(block);
250         ir_node  *minconst = new_r_Const(irg, entry->min);
251         ir_node  *cmp;
252
253         if (entry->min == entry->max) {
254                 cmp = new_rd_Cmp(dbgi, block, selector, minconst, ir_relation_equal);
255         } else {
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);
261         }
262
263         return new_rd_Cond(dbgi, block, cmp);
264 }
265
266 /**
267  * Creates an if cascade realizing binary search.
268  */
269 static void create_if_cascade(switch_info_t *info, ir_node *block,
270                               case_data_t *curcases, unsigned numcases)
271 {
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);
276
277         if (numcases == 0) {
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);
286
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);
296                 ir_node *in[1];
297                 ir_node *neblock;
298
299                 set_Block_cfgpred(curcases[0].target, 0, trueproj);
300
301                 in[0] = falseproj;
302                 neblock = new_r_Block(irg, 1, in);
303
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);
310         } else {
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);
317                 ir_node *in[1];
318                 ir_node *ltblock;
319                 ir_node *geblock;
320
321                 in[0]   = new_r_Proj(cond, mode_X, pn_Cond_true);
322                 ltblock = new_r_Block(irg, 1, in);
323
324                 in[0]   = new_r_Proj(cond, mode_X, pn_Cond_false);
325                 geblock = new_r_Block(irg, 1, in);
326
327                 create_if_cascade(info, ltblock, curcases, midcase);
328                 create_if_cascade(info, geblock, curcases + midcase, numcases - midcase);
329         }
330 }
331
332 static void create_out_of_bounds_check(switch_info_t *info)
333 {
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;
342         ir_node    *max_const;
343         ir_node    *proj_true;
344         ir_node    *proj_false;
345         ir_node    *cmp;
346         ir_node    *oob_cond;
347         ir_node    *in[1];
348         ir_node    *new_block;
349         int         i;
350         ir_node    *proj;
351         size_t      n_default_preds;
352
353         assert(info->switch_min == 0);
354
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);
361
362         ARR_APP1(ir_node*, default_preds, proj_false);
363
364         /* create new block containing the switch */
365         in[0] = proj_true;
366         new_block = new_r_Block(irg, 1, in);
367         set_nodes_block(switchn, new_block);
368
369         /* adjust projs */
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);
376                 }
377                 set_nodes_block(proj, new_block);
378         }
379
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 */
384                 size_t p;
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];
389
390                         block_in[0] = pred;
391                         split_block = new_r_Block(irg, 1, block_in);
392
393                         default_preds[p] = new_r_Jmp(split_block);
394                 }
395         }
396         set_irn_in(default_block, n_default_preds, default_preds);
397
398         DEL_ARR_F(default_preds);
399
400         clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE
401                            | IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
402 }
403
404 /**
405  * Block-Walker: searches for Switch nodes
406  */
407 static void find_switch_nodes(ir_node *block, void *ctx)
408 {
409         walk_env_t   *env = (walk_env_t *)ctx;
410         ir_node      *projx;
411         ir_node      *switchn;
412         switch_info_t info;
413         unsigned long spare;
414         bool          lower_switch = false;
415         bool          could_analyze;
416
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)
420                 return;
421
422         projx = get_Block_cfgpred(block, 0);
423         if (!is_Proj(projx))
424                 return;
425         assert(get_irn_mode(projx) == mode_X);
426
427         switchn = get_Proj_pred(projx);
428         if (!is_Switch(switchn))
429                 return;
430
431         if (ir_nodeset_contains(&env->processed, switchn))
432                 return;
433         ir_nodeset_insert(&env->processed, switchn);
434
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);
439                 return;
440         }
441
442         /*
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.
445          */
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;
451
452         if (!lower_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);
458                 }
459                 return;
460         }
461
462         normalize_switch(&info);
463         analyse_switch1(&info);
464
465         /* Now create the if cascade */
466         env->changed   = true;
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);
470
471         /* Connect new default case users */
472         set_irn_in(info.default_block, ARR_LEN(info.defusers), info.defusers);
473
474         DEL_ARR_F(info.defusers);
475         xfree(info.cases);
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);
479 }
480
481 void lower_switch(ir_graph *irg, unsigned small_switch, unsigned spare_size,
482                   int allow_out_of_bounds)
483 {
484         walk_env_t env;
485         env.changed             = false;
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);
490
491         remove_critical_cf_edges(irg);
492         assure_irg_outs(irg);
493
494         irg_block_walk_graph(irg, find_switch_nodes, NULL, &env);
495         ir_nodeset_destroy(&env.processed);
496 }