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