a3fbac010db7306f6668fdc2e04ec18127faac2f
[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 "irgwalk.h"
35 #include "irnode_t.h"
36 #include "irouts.h"
37
38 #define foreach_out_irn(irn, i, outirn) for(i = get_irn_n_outs(irn) - 1;\
39         i >= 0 && (outirn = get_irn_out(irn, i)); --i)
40
41 typedef struct walk_env {
42         unsigned         spare_size;            /**< the allowed spare size for table switches */
43         struct obstack   obst;              /**< the obstack where data is allocated on */
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 /**
53  * Add the new predecessor x to node node, which is either a Block or a Phi
54  */
55 static void add_pred(ir_node* node, ir_node* x)
56 {
57         ir_node** ins;
58         int n;
59         int i;
60
61         assert(is_Block(node) || is_Phi(node));
62
63         n = get_irn_arity(node);
64         NEW_ARR_A(ir_node*, ins, n + 1);
65         for (i = 0; i < n; i++)
66                 ins[i] = get_irn_n(node, i);
67         ins[n] = x;
68         set_irn_in(node, n + 1, ins);
69 }
70
71 /**
72  * Remove the predecessor x from node node one time, which is either a Block or a Phi
73  */
74 static void remove_pred(ir_node* node, ir_node* x)
75 {
76         ir_node** ins;
77         int n;
78         int i, j;
79
80         assert(is_Block(node) || is_Phi(node));
81
82         n = get_irn_arity(node);
83         NEW_ARR_A(ir_node*, ins, n - 1);
84         for (i = 0, j = -1; i < n - 1; i++)
85         {
86                 ins[++j] = get_irn_n(node, i);
87                 if(ins[i] == x) j--;
88         }
89         assert(i == j + 1 && "x is not a pred of node");
90         set_irn_in(node, n - 1, ins);
91 }
92
93 /**
94  * Evaluate a switch and decide whether we should build a table switch.
95  *
96  * @param cond       The Cond node representing the switch.
97  * @param spare_size Allowed spare size for table switches in machine words.
98  *                   (Default in edgfe: 128)
99  */
100 static int should_do_table_switch(ir_node *cond, unsigned spare_size)
101 {
102         long     default_pn;
103         int      i;
104         ir_node *proj;
105         long switch_min = LONG_MAX, switch_max = LONG_MIN;
106         unsigned long spare, num_cases = 0;
107
108         /* TODO: Minimum size for jump table? */
109         if(get_irn_n_outs(cond) <= 4)
110                 return 0;
111
112         default_pn = get_Cond_defaultProj(cond);
113
114         foreach_out_irn(cond, i, proj) {
115                 long pn = get_Proj_proj(proj);
116                 if(pn == default_pn)
117                         continue;
118
119                 if(pn < switch_min)
120                         switch_min = pn;
121                 if(pn > switch_max)
122                         switch_max = pn;
123                 num_cases++;
124         }
125
126         /*
127          * Here we have: num_cases and [switch_min, switch_max] interval.
128          * We do an if-cascade if there are too many spare numbers.
129          */
130         spare = (unsigned long) switch_max - (unsigned long) switch_min - num_cases + 1;
131         return spare < spare_size;
132 }
133
134 static int casecmp(const void *a, const void *b)
135 {
136         return ((case_data_t *) a)->value - ((case_data_t *) b)->value;
137 }
138
139 /**
140  * Creates an if cascade realizing binary search.
141  */
142 static void create_if_cascade(ir_node *sel, ir_node *curblock, ir_node *defblock,
143                                                           case_data_t *curcases, int numcases)
144 {
145         set_cur_block(curblock);
146
147         if(numcases == 1)
148         {
149                 /* only one case: "if(sel == val) goto target else goto default;" */
150                 ir_node *val  = new_Const_long(get_irn_mode(sel), curcases[0].value);
151                 ir_node *cmp  = new_Cmp(sel, val);
152                 ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Eq);
153                 ir_node *cond = new_Cond(proj);
154                 add_pred(curcases[0].target, new_Proj(cond, mode_X, pn_Cond_true));
155                 add_pred(defblock,           new_Proj(cond, mode_X, pn_Cond_false));
156                 return;
157         } else if(numcases == 2) {
158                 /* only two cases: "if(sel == val[0]) goto target[0];" */
159                 ir_node *val  = new_Const_long(get_irn_mode(sel), curcases[0].value);
160                 ir_node *cmp  = new_Cmp(sel, val);
161                 ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Eq);
162                 ir_node *cond = new_Cond(proj);
163                 ir_node *in[1];
164                 ir_node *neblock;
165
166                 add_pred(curcases[0].target, new_Proj(cond, mode_X, pn_Cond_true));
167                 in[0] = new_Proj(cond, mode_X, pn_Cond_false);
168                 neblock = new_Block(1, in);
169
170                 /* second part: "else if(sel == val[1]) goto target[1] else goto default;" */
171                 val  = new_Const_long(get_irn_mode(sel), curcases[1].value);
172                 cmp  = new_Cmp(sel, val);
173                 proj = new_Proj(cmp, mode_b, pn_Cmp_Eq);
174                 cond = new_Cond(proj);
175                 add_pred(curcases[1].target, new_Proj(cond, mode_X, pn_Cond_true));
176                 add_pred(defblock,           new_Proj(cond, mode_X, pn_Cond_false));
177                 return;
178         } else {
179                 /* recursive case: split cases in the middle */
180                 int midcase = numcases / 2;
181                 ir_node *val  = new_Const_long(get_irn_mode(sel), curcases[midcase].value);
182                 ir_node *cmp  = new_Cmp(sel, val);
183                 ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Lt);
184                 ir_node *cond = new_Cond(proj);
185                 ir_node *in[1];
186                 ir_node *ltblock;
187                 ir_node *geblock;
188
189                 in[0] = new_Proj(cond, mode_X, pn_Cond_true);
190                 ltblock = new_Block(1, in);
191
192                 set_cur_block(curblock);
193                 in[0] = new_Proj(cond, mode_X, pn_Cond_false);
194                 geblock = new_Block(1, in);
195
196                 create_if_cascade(sel, ltblock, defblock, curcases, midcase);
197                 create_if_cascade(sel, geblock, defblock, 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
218         if(get_Block_n_cfgpreds(block) != 1)
219                 return;
220
221         projx = get_Block_cfgpred(block, 0);
222         if(!is_Proj(projx))
223                 return;
224         assert(get_irn_mode(projx) == mode_X);
225
226         cond = get_Proj_pred(projx);
227         if(!is_Cond(cond))
228                 return;
229
230         sel      = get_Cond_selector(cond);
231         sel_mode = get_irn_mode(sel);
232
233         if(sel_mode == mode_b)    /* not a switch? */
234                 return;
235
236         if(should_do_table_switch(cond, env->spare_size))
237                 return;
238
239         /*
240          * Switch should be transformed into an if cascade.
241          * So first order the cases, so we can do a binary search on them.
242          */
243
244         numcases = get_irn_n_outs(cond) - 1;      // does not contain default case
245         cases    = obstack_alloc(&env->obst, numcases * sizeof(*cases));
246
247         default_pn = get_Cond_defaultProj(cond);
248
249         foreach_out_irn(cond, i, proj) {
250                 long pn = get_Proj_proj(proj);
251                 if(pn == default_pn)
252                 {
253                         defblock = get_irn_out(proj, 0);
254                         remove_pred(defblock, proj);
255                         continue;
256                 }
257
258                 cases[j].value  = pn;
259                 cases[j].target = get_irn_out(proj, 0);
260                 remove_pred(cases[j].target, proj);
261                 j++;
262         }
263         assert(defblock != NULL && "Switch without default proj");
264         qsort(cases, numcases, sizeof(*cases), casecmp);
265
266         /* Now create the if cascade */
267         condblock = get_nodes_block(cond);
268         create_if_cascade(sel, condblock, defblock, cases, numcases);
269
270         obstack_free(&env->obst, cases);
271 }
272
273 /**
274  * Lowers all Switches (Cond nodes with non-boolean mode) depending on spare_size.
275  * They will either remain the same or be converted into if-cascades.
276  *
277  * @param irg        The ir graph to be lowered.
278  * @param spare_size Allowed spare size for table switches in machine words.
279  *                   (Default in edgfe: 128)
280  */
281 void lower_Switch(ir_graph *irg, unsigned spare_size)
282 {
283         walk_env_t env;
284         ir_graph *rem = current_ir_graph;
285
286         current_ir_graph = irg;
287
288         obstack_init(&env.obst);
289         env.spare_size = spare_size;
290
291         assure_irg_outs(irg);
292
293         irg_block_walk_graph(irg, find_cond_nodes, NULL, &env);
294
295         if(env.changed) {
296                 /* control flow changed */
297                 set_irg_outs_inconsistent(irg);
298                 set_irg_doms_inconsistent(irg);
299                 set_irg_extblk_inconsistent(irg);
300                 set_irg_loopinfo_inconsistent(irg);
301         }
302
303         obstack_free(&env.obst, NULL);
304         current_ir_graph = rem;
305 }