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