Fixed switch lowering
[libfirm] / ir / lower / lower_switch.c
1 /*
2  * Copyright (C) 1995-2008 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  * @version $Id$
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <limits.h>
32
33 #include "ircons.h"
34 #include "irgopt.h"
35 #include "irgwalk.h"
36 #include "irnode_t.h"
37 #include "irouts.h"
38
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)
41
42 typedef struct walk_env {
43         unsigned         spare_size;        /**< the allowed spare size for table switches */
44         struct obstack   obst;              /**< the obstack where data is allocated on */
45         int              changed;           /**< indicates whether a change was performed */
46 } walk_env_t;
47
48 typedef struct case_data {
49         long     value;
50         ir_node *target;
51 } case_data_t;
52
53 typedef struct ifcas_env {
54         ir_node  *sel;
55         int       defindex;
56         ir_node **defusers;                 /**< the Projs pointing to the default case */
57 } ifcas_env_t;
58
59 /**
60  * Evaluate a switch and decide whether we should build a table switch.
61  *
62  * @param cond       The Cond node representing the switch.
63  * @param spare_size Allowed spare size for table switches in machine words.
64  *                   (Default in edgfe: 128)
65  */
66 static int should_do_table_switch(ir_node *cond, unsigned spare_size)
67 {
68         long     default_pn;
69         int      i;
70         ir_node *proj;
71         long switch_min = LONG_MAX, switch_max = LONG_MIN;
72         unsigned long spare, num_cases = 0;
73
74         /* TODO: Minimum size for jump table? */
75         if(get_irn_n_outs(cond) <= 4)
76                 return 0;
77
78         default_pn = get_Cond_defaultProj(cond);
79
80         foreach_out_irn(cond, i, proj) {
81                 long pn = get_Proj_proj(proj);
82                 if(pn == default_pn)
83                         continue;
84
85                 if(pn < switch_min)
86                         switch_min = pn;
87                 if(pn > switch_max)
88                         switch_max = pn;
89                 num_cases++;
90         }
91
92         /*
93          * Here we have: num_cases and [switch_min, switch_max] interval.
94          * We do an if-cascade if there are too many spare numbers.
95          */
96         spare = (unsigned long) switch_max - (unsigned long) switch_min - num_cases + 1;
97         return spare < spare_size;
98 }
99
100 static int casecmp(const void *a, const void *b)
101 {
102         return ((case_data_t *) a)->value - ((case_data_t *) b)->value;
103 }
104
105 /**
106  * Creates an if cascade realizing binary search.
107  */
108 static void create_if_cascade(ifcas_env_t *env, ir_node *curblock,
109                               case_data_t *curcases, int numcases)
110 {
111         set_cur_block(curblock);
112
113         if(numcases == 1)
114         {
115                 /* only one case: "if(sel == val) goto target else goto default;" */
116                 ir_node *val  = new_Const_long(get_irn_mode(env->sel), curcases[0].value);
117                 ir_node *cmp  = new_Cmp(env->sel, val);
118                 ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Eq);
119                 ir_node *cond = new_Cond(proj);
120                 set_Block_cfgpred(curcases[0].target, 0, new_Proj(cond, mode_X, pn_Cond_true));
121                 env->defusers[env->defindex++] = new_Proj(cond, mode_X, pn_Cond_false);
122         } else if(numcases == 2) {
123                 /* only two cases: "if(sel == val[0]) goto target[0];" */
124                 ir_node *val  = new_Const_long(get_irn_mode(env->sel), curcases[0].value);
125                 ir_node *cmp  = new_Cmp(env->sel, val);
126                 ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Eq);
127                 ir_node *cond = new_Cond(proj);
128                 ir_node *in[1];
129                 ir_node *neblock;
130
131                 set_Block_cfgpred(curcases[0].target, 0, new_Proj(cond, mode_X, pn_Cond_true));
132                 in[0] = new_Proj(cond, mode_X, pn_Cond_false);
133                 neblock = new_Block(1, in);
134
135                 /* second part: "else if(sel == val[1]) goto target[1] else goto default;" */
136                 val  = new_Const_long(get_irn_mode(env->sel), curcases[1].value);
137                 cmp  = new_Cmp(env->sel, val);
138                 proj = new_Proj(cmp, mode_b, pn_Cmp_Eq);
139                 cond = new_Cond(proj);
140                 set_Block_cfgpred(curcases[1].target, 0, new_Proj(cond, mode_X, pn_Cond_true));
141                 env->defusers[env->defindex++] = new_Proj(cond, mode_X, pn_Cond_false);
142         } else {
143                 /* recursive case: split cases in the middle */
144                 int midcase = numcases / 2;
145                 ir_node *val  = new_Const_long(get_irn_mode(env->sel), curcases[midcase].value);
146                 ir_node *cmp  = new_Cmp(env->sel, val);
147                 ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Lt);
148                 ir_node *cond = new_Cond(proj);
149                 ir_node *in[1];
150                 ir_node *ltblock;
151                 ir_node *geblock;
152
153                 in[0] = new_Proj(cond, mode_X, pn_Cond_true);
154                 ltblock = new_Block(1, in);
155
156                 set_cur_block(curblock);
157                 in[0] = new_Proj(cond, mode_X, pn_Cond_false);
158                 geblock = new_Block(1, in);
159
160                 create_if_cascade(env, ltblock, curcases, midcase);
161                 create_if_cascade(env, geblock, curcases + midcase, numcases - midcase);
162         }
163 }
164
165 /**
166  * Block-Walker: searches for Cond nodes with a non-boolean mode
167  */
168 static void find_cond_nodes(ir_node *block, void *ctx)
169 {
170         walk_env_t  *env = ctx;
171         ir_node     *projx;
172         ir_node     *cond;
173         ir_node     *sel;
174         ir_mode     *sel_mode;
175         long         default_pn;
176         int          i, j = 0, numcases;
177         ir_node     *proj;
178         case_data_t *cases;
179         ir_node     *condblock;
180         ir_node     *defblock = NULL;
181         ifcas_env_t  ifcas_env;
182
183         if(get_Block_n_cfgpreds(block) != 1)
184                 return;
185
186         projx = get_Block_cfgpred(block, 0);
187         if(!is_Proj(projx))
188                 return;
189         assert(get_irn_mode(projx) == mode_X);
190
191         cond = get_Proj_pred(projx);
192         if(!is_Cond(cond))
193                 return;
194
195         sel      = get_Cond_selector(cond);
196         sel_mode = get_irn_mode(sel);
197
198         if(sel_mode == mode_b)    /* not a switch? */
199                 return;
200
201         if(should_do_table_switch(cond, env->spare_size))
202                 return;
203
204         /*
205          * Switch should be transformed into an if cascade.
206          * So first order the cases, so we can do a binary search on them.
207          */
208
209         numcases = get_irn_n_outs(cond) - 1;      // does not contain default case
210         cases    = obstack_alloc(&env->obst, numcases * sizeof(*cases));
211
212         default_pn = get_Cond_defaultProj(cond);
213         ifcas_env.sel = sel;
214         ifcas_env.defindex = 0;
215         NEW_ARR_A(ir_node*, ifcas_env.defusers, numcases);
216
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");
221
222                 if(pn == default_pn)
223                 {
224                         defblock = target;
225                         continue;
226                 }
227
228                 cases[j].value  = pn;
229                 cases[j].target = target;
230                 j++;
231         }
232
233         assert(defblock != NULL && "Switch without default proj");
234         qsort(cases, numcases, sizeof(*cases), casecmp);
235
236         /* Now create the if cascade */
237         condblock = get_nodes_block(cond);
238         create_if_cascade(&ifcas_env, condblock, cases, numcases);
239
240         /* Connect new default case users */
241         set_irn_in(defblock, ifcas_env.defindex, ifcas_env.defusers);
242
243         obstack_free(&env->obst, cases);
244 }
245
246 /**
247  * Lowers all Switches (Cond nodes with non-boolean mode) depending on spare_size.
248  * They will either remain the same or be converted into if-cascades.
249  *
250  * @param irg        The ir graph to be lowered.
251  * @param spare_size Allowed spare size for table switches in machine words.
252  *                   (Default in edgfe: 128)
253  */
254 void lower_Switch(ir_graph *irg, unsigned spare_size)
255 {
256         walk_env_t env;
257         ir_graph *rem = current_ir_graph;
258
259         current_ir_graph = irg;
260
261         obstack_init(&env.obst);
262         env.spare_size = spare_size;
263
264         remove_critical_cf_edges(irg);
265         assure_irg_outs(irg);
266
267         irg_block_walk_graph(irg, find_cond_nodes, NULL, &env);
268
269         if(env.changed) {
270                 /* control flow changed */
271                 set_irg_outs_inconsistent(irg);
272                 set_irg_doms_inconsistent(irg);
273                 set_irg_extblk_inconsistent(irg);
274                 set_irg_loopinfo_inconsistent(irg);
275         }
276
277         obstack_free(&env.obst, NULL);
278         current_ir_graph = rem;
279 }