2db00ee92eff21360b78d9f46f8b4667746f231d
[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 #include "config.h"
28
29 #include <limits.h>
30
31 #include "array_t.h"
32 #include "ircons.h"
33 #include "irgopt.h"
34 #include "irgwalk.h"
35 #include "irnode_t.h"
36 #include "irouts.h"
37 #include "irpass_t.h"
38 #include "lowering.h"
39
40 #define foreach_out_irn(irn, i, outirn) for (i = get_irn_n_outs(irn) - 1;\
41         i >= 0 && (outirn = get_irn_out(irn, i)); --i)
42
43 typedef struct walk_env {
44         unsigned         spare_size;        /**< the allowed spare size for table switches */
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_default_proj(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         const case_data_t *cda = a;
103         const case_data_t *cdb = b;
104
105         /*
106          * Enforce unsigned sorting. Signed comparison will behave differently for
107          * 32-bit values, depending on sizeof(long). This will make the resulting
108          * array deterministic.
109          */
110         return ((unsigned long)cda->value > (unsigned long)cdb->value) -
111                ((unsigned long)cda->value < (unsigned long)cdb->value);
112 }
113
114 /**
115  * Creates an if cascade realizing binary search.
116  */
117 static void create_if_cascade(ifcas_env_t *env, ir_node *curblock,
118                               case_data_t *curcases, int numcases)
119 {
120     ir_mode *cmp_mode;
121     ir_node *cmp_sel;
122     ir_node *sel_block;
123
124     /* Get the mode and sel node for the comparison. */
125     cmp_mode  = get_irn_mode(env->sel);
126     cmp_sel   = env->sel;
127     sel_block = get_nodes_block(cmp_sel);
128
129     /*
130      * Make sure that an unsigned comparison is used, by converting the sel
131      * node to an unsigned mode and using that mode for the constants, too.
132      * This is important, because the qsort applied to the case labels uses
133      * an unsigned comparison and both comparison methods have to match.
134      */
135     if (mode_is_signed(cmp_mode))
136     {
137         cmp_mode = find_unsigned_mode(cmp_mode);
138         cmp_sel  = new_r_Conv(sel_block, cmp_sel, cmp_mode);
139     }
140
141         assert(numcases >= 0);
142
143         set_cur_block(curblock);
144
145         if (numcases == 0) {
146                 /* zero cases: "goto default;" */
147                 env->defusers[env->defindex++] = new_Jmp();
148         } else if (numcases == 1) {
149                 /* only one case: "if (sel == val) goto target else goto default;" */
150                 ir_node *val  = new_Const_long(cmp_mode, curcases[0].value);
151                 ir_node *cmp  = new_Cmp(cmp_sel, val);
152                 ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Eq);
153                 ir_node *cond = new_Cond(proj);
154                 set_Block_cfgpred(curcases[0].target, 0, new_Proj(cond, mode_X, pn_Cond_true));
155                 env->defusers[env->defindex++] = new_Proj(cond, mode_X, pn_Cond_false);
156         } else if (numcases == 2) {
157                 /* only two cases: "if (sel == val[0]) goto target[0];" */
158                 ir_node *val  = new_Const_long(cmp_mode, curcases[0].value);
159                 ir_node *cmp  = new_Cmp(cmp_sel, val);
160                 ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Eq);
161                 ir_node *cond = new_Cond(proj);
162                 ir_node *in[1];
163                 ir_node *neblock;
164
165                 set_Block_cfgpred(curcases[0].target, 0, new_Proj(cond, mode_X, pn_Cond_true));
166                 in[0] = new_Proj(cond, mode_X, pn_Cond_false);
167                 neblock = new_Block(1, in);
168                 set_cur_block(neblock);
169
170                 /* second part: "else if (sel == val[1]) goto target[1] else goto default;" */
171                 val  = new_Const_long(cmp_mode, curcases[1].value);
172                 cmp  = new_Cmp(cmp_sel, val);
173                 proj = new_Proj(cmp, mode_b, pn_Cmp_Eq);
174                 cond = new_Cond(proj);
175                 set_Block_cfgpred(curcases[1].target, 0, new_Proj(cond, mode_X, pn_Cond_true));
176                 env->defusers[env->defindex++] = new_Proj(cond, mode_X, pn_Cond_false);
177         } else {
178                 /* recursive case: split cases in the middle */
179                 int midcase = numcases / 2;
180                 ir_node *val  = new_Const_long(cmp_mode, curcases[midcase].value);
181                 ir_node *cmp  = new_Cmp(cmp_sel, val);
182                 ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Lt);
183                 ir_node *cond = new_Cond(proj);
184                 ir_node *in[1];
185                 ir_node *ltblock;
186                 ir_node *geblock;
187
188                 in[0] = new_Proj(cond, mode_X, pn_Cond_true);
189                 ltblock = new_Block(1, in);
190
191                 set_cur_block(curblock);
192                 in[0] = new_Proj(cond, mode_X, pn_Cond_false);
193                 geblock = new_Block(1, in);
194                 set_cur_block(geblock);
195
196                 create_if_cascade(env, ltblock, curcases, midcase);
197                 create_if_cascade(env, geblock, curcases + midcase, numcases - midcase);
198         }
199 }
200
201 /**
202  * Block-Walker: searches for Cond nodes with a non-boolean mode
203  */
204 static void find_cond_nodes(ir_node *block, void *ctx)
205 {
206         walk_env_t  *env = ctx;
207         ir_node     *projx;
208         ir_node     *cond;
209         ir_node     *sel;
210         ir_mode     *sel_mode;
211         long         default_pn;
212         int          i, j = 0, numcases;
213         ir_node     *proj;
214         case_data_t *cases;
215         ir_node     *condblock;
216         ir_node     *defblock = NULL;
217         ifcas_env_t  ifcas_env;
218
219         if (get_Block_n_cfgpreds(block) != 1)
220                 return;
221
222         projx = get_Block_cfgpred(block, 0);
223         if (!is_Proj(projx))
224                 return;
225         assert(get_irn_mode(projx) == mode_X);
226
227         cond = get_Proj_pred(projx);
228         if (!is_Cond(cond))
229                 return;
230
231         sel      = get_Cond_selector(cond);
232         sel_mode = get_irn_mode(sel);
233
234         if (sel_mode == mode_b)    /* not a switch? */
235                 return;
236
237         if (should_do_table_switch(cond, env->spare_size))
238                 return;
239
240         /*
241          * Switch should be transformed into an if cascade.
242          * So first order the cases, so we can do a binary search on them.
243          */
244         env->changed = 1;
245
246         numcases = get_irn_n_outs(cond) - 1;      // does not contain default case
247         NEW_ARR_A(case_data_t, cases, numcases);
248
249         default_pn = get_Cond_default_proj(cond);
250         ifcas_env.sel = sel;
251         ifcas_env.defindex = 0;
252         NEW_ARR_A(ir_node*, ifcas_env.defusers, numcases);
253
254         foreach_out_irn(cond, i, proj) {
255                 long pn = get_Proj_proj(proj);
256                 ir_node *target = get_irn_out(proj, 0);
257                 assert(get_Block_n_cfgpreds(target) == 1 && "Encountered critical edge in switch");
258
259                 if (pn == default_pn) {
260                         defblock = target;
261                         continue;
262                 }
263
264                 cases[j].value  = pn;
265                 cases[j].target = target;
266                 j++;
267         }
268
269         assert(defblock != NULL && "Switch without default proj");
270         qsort(cases, numcases, sizeof(*cases), casecmp);
271
272         /* Now create the if cascade */
273         condblock = get_nodes_block(cond);
274         create_if_cascade(&ifcas_env, condblock, cases, numcases);
275
276         /* Connect new default case users */
277         set_irn_in(defblock, ifcas_env.defindex, ifcas_env.defusers);
278 }
279
280 /**
281  * Lowers all Switches (Cond nodes with non-boolean mode) depending on spare_size.
282  * They will either remain the same or be converted into if-cascades.
283  *
284  * @param irg        The ir graph to be lowered.
285  * @param spare_size Allowed spare size for table switches in machine words.
286  *                   (Default in edgfe: 128)
287  */
288 void lower_switch(ir_graph *irg, unsigned spare_size)
289 {
290         walk_env_t env;
291         ir_graph *rem = current_ir_graph;
292
293         current_ir_graph = irg;
294
295         env.changed    = 0;
296         env.spare_size = spare_size;
297
298         remove_critical_cf_edges(irg);
299         assure_irg_outs(irg);
300
301         irg_block_walk_graph(irg, find_cond_nodes, NULL, &env);
302
303         if (env.changed) {
304                 /* control flow changed */
305                 set_irg_outs_inconsistent(irg);
306                 set_irg_doms_inconsistent(irg);
307                 set_irg_extblk_inconsistent(irg);
308                 set_irg_loopinfo_inconsistent(irg);
309         }
310         current_ir_graph = rem;
311 }
312
313 struct pass_t {
314         ir_graph_pass_t pass;
315         unsigned        spare_size;
316 };
317
318 /**
319  * Wrapper for running lower_switch() as a pass.
320  */
321 static int pass_wrapper(ir_graph *irg, void *context)
322 {
323         struct pass_t *pass = context;
324
325         lower_switch(irg, pass->spare_size);
326         return 0;
327 }
328
329 /* creates a pass for lower_switch */
330 ir_graph_pass_t *lower_switch_pass(const char *name, unsigned spare_size)
331 {
332         struct pass_t *pass = XMALLOCZ(struct pass_t);
333
334         pass->spare_size = spare_size;
335         return def_graph_pass_constructor(
336                 &pass->pass, name ? name : "lower_switch", pass_wrapper);
337 }