Add a skip_Id() to make the following assert happy
[libfirm] / ir / opt / boolopt.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   boolean condition/controlflow optimisations
23  * @author  Matthias Braun, Christoph Mallon
24  * @version $Id: cfopt.c 22579 2008-10-07 14:54:04Z beck $
25  */
26 #include "config.h"
27
28 #include <assert.h>
29 #include <string.h>
30
31 #include "adt/obst.h"
32 #include "ircons.h"
33 #include "irgmod.h"
34 #include "irgwalk.h"
35 #include "irprintf.h"
36 #include "irnode_t.h"
37 #include "tv.h"
38 #include "irpass.h"
39
40 typedef struct cond_pair {
41         ir_node *cmp_lo;
42         ir_node *cmp_hi;
43         pn_Cmp   pnc_lo;
44         pn_Cmp   pnc_hi;
45         ir_node *proj_lo;
46         ir_node *proj_hi;
47         tarval  *tv_lo;
48         tarval  *tv_hi;
49 } cond_pair;
50
51 static int find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const res)
52 {
53         if (is_Proj(l) && is_Proj(r)) {
54                 ir_node *const lo = get_Proj_pred(l);
55                 ir_node *const ro = get_Proj_pred(r);
56
57                 if (is_Cmp(lo) && is_Cmp(ro)) {
58                         ir_node *const lol = get_Cmp_left(lo);
59                         ir_node *const lor = get_Cmp_right(lo);
60                         ir_node *const rol = get_Cmp_left(ro);
61                         ir_node *const ror = get_Cmp_right(ro);
62
63                         if(is_Const(lor) && is_Const_null(lor) && is_Const(ror) && is_Const_null(ror) && get_Proj_proj(l) == pn_Cmp_Lg && get_Proj_proj(r) == pn_Cmp_Lg) {
64                                 ir_fprintf(stderr, "found zero zero\n");
65                         }
66
67                         /* TODO float */
68                         /* The constants shall be unequal.  Local optimisations handle the
69                          * equal case */
70                         if (lol == rol && mode_is_int(get_irn_mode(lol)) && lor != ror && is_Const(lor) && is_Const(ror)) {
71                                 tarval *const tv_l  = get_Const_tarval(lor);
72                                 tarval *const tv_r  = get_Const_tarval(ror);
73                                 pn_Cmp  const pnc_l = get_Proj_proj(l);
74                                 pn_Cmp  const pnc_r = get_Proj_proj(r);
75                                 pn_Cmp  const rel   = tarval_cmp(tv_l, tv_r);
76
77                                 assert(rel != pn_Cmp_Eq);
78
79                                 if (rel == pn_Cmp_Lt) {
80                                         res->cmp_lo  = lo;
81                                         res->cmp_hi  = ro;
82                                         res->pnc_lo  = pnc_l;
83                                         res->pnc_hi  = pnc_r;
84                                         res->proj_lo = l;
85                                         res->proj_hi = r;
86                                         res->tv_lo   = tv_l;
87                                         res->tv_hi   = tv_r;
88                                 } else {
89                                         assert(rel == pn_Cmp_Gt);
90                                         res->cmp_lo  = ro;
91                                         res->cmp_hi  = lo;
92                                         res->pnc_lo  = pnc_r;
93                                         res->pnc_hi  = pnc_l;
94                                         res->proj_lo = r;
95                                         res->proj_hi = l;
96                                         res->tv_lo   = tv_r;
97                                         res->tv_hi   = tv_l;
98                                 }
99                                 return 1;
100                         }
101                 }
102         }
103         return 0;
104 }
105
106 static ir_node *bool_and(cond_pair* const cpair)
107 {
108         ir_node *const cmp_lo  = cpair->cmp_lo;
109         ir_node *const cmp_hi  = cpair->cmp_hi;
110         pn_Cmp   const pnc_lo  = cpair->pnc_lo;
111         pn_Cmp   const pnc_hi  = cpair->pnc_hi;
112         ir_node *const proj_lo = cpair->proj_lo;
113         ir_node *const proj_hi = cpair->proj_hi;
114         tarval  *const tv_lo   = cpair->tv_lo;
115         tarval  *const tv_hi   = cpair->tv_hi;
116
117         /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
118         if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
119                         (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
120                 /* x <|<=|== lo | x ==|>=|> hi -> false */
121                 ir_node *const t = new_Const(tarval_b_false);
122                 return t;
123         } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
124                                                  (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
125                 /* x <|<=|== lo && x <|<=|!= hi -> x <|<=|== lo */
126                 return proj_lo;
127         } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
128                                                  (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
129                 /* x >=|>|!= lo || x ==|>=|> hi -> x ==|>=|> hi */
130                 return proj_hi;
131         } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
132                 if (pnc_lo == pn_Cmp_Ge && pnc_hi == pn_Cmp_Lt) {
133                         /* x >= c || x < c + 1 -> x == c */
134                         ir_node  *const block = get_nodes_block(cmp_lo);
135                         ir_node  *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Eq);
136                         return p;
137                 } else if (pnc_lo == pn_Cmp_Gt) {
138                         if (pnc_hi == pn_Cmp_Lg) {
139                                 /* x > c || x != c + 1 -> x > c + 1 */
140                                 ir_node  *const block = get_nodes_block(cmp_hi);
141                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Gt);
142                                 return p;
143                         } else if (pnc_hi == pn_Cmp_Lt) {
144                                 /* x > c || x < c + 1 -> false */
145                                 ir_node *const t = new_Const(tarval_b_false);
146                                 return t;
147                         } else if (pnc_hi == pn_Cmp_Le) {
148                                 /* x > c || x <= c + 1 -> x != c + 1 */
149                                 ir_node  *const block = get_nodes_block(cmp_hi);
150                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Eq);
151                                 return p;
152                         }
153                 } else if (pnc_lo == pn_Cmp_Lg && pnc_hi == pn_Cmp_Lt) {
154                         /* x != c || c < c + 1 -> x < c */
155                         ir_node  *const block = get_nodes_block(cmp_lo);
156                         ir_node  *const p     = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lt);
157                         return p;
158                 }
159         }
160         return NULL;
161 }
162
163 static ir_node *bool_or(cond_pair *const cpair)
164 {
165         ir_node *const cmp_lo  = cpair->cmp_lo;
166         ir_node *const cmp_hi  = cpair->cmp_hi;
167         pn_Cmp   const pnc_lo  = cpair->pnc_lo;
168         pn_Cmp   const pnc_hi  = cpair->pnc_hi;
169         ir_node *const proj_lo = cpair->proj_lo;
170         ir_node *const proj_hi = cpair->proj_hi;
171         tarval  *const tv_lo   = cpair->tv_lo;
172         tarval  *const tv_hi   = cpair->tv_hi;
173
174         /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
175         if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
176                         (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
177                 /* x >=|>|!= lo | x <|<=|!= hi -> true */
178                 ir_node *const t = new_Const(tarval_b_true);
179                 return t;
180         } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
181                                                  (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
182                 /* x <|<=|== lo || x <|<=|!= hi -> x <|<=|!= hi */
183                 return proj_hi;
184         } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
185                                                  (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
186                 /* x >=|>|!= lo || x ==|>=|> hi -> x >=|>|!= lo */
187                 return proj_lo;
188         } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
189                 if (pnc_lo == pn_Cmp_Lt && pnc_hi == pn_Cmp_Ge) {
190                         /* x < c || x >= c + 1 -> x != c */
191                         ir_node  *const block = get_nodes_block(cmp_lo);
192                         ir_node  *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lg);
193                         return p;
194                 } else if (pnc_lo == pn_Cmp_Le) {
195                         if (pnc_hi == pn_Cmp_Eq) {
196                                 /* x <= c || x == c + 1 -> x <= c + 1 */
197                                 ir_node  *const block = get_nodes_block(cmp_hi);
198                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Le);
199                                 return p;
200                         } else if (pnc_hi == pn_Cmp_Ge) {
201                                 /* x <= c || x >= c + 1 -> true */
202                                 ir_node *const t = new_Const(tarval_b_true);
203                                 return t;
204                         } else if (pnc_hi == pn_Cmp_Gt) {
205                                 /* x <= c || x > c + 1 -> x != c + 1 */
206                                 ir_node  *const block = get_nodes_block(cmp_hi);
207                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Lg);
208                                 return p;
209                         }
210                 } else if (pnc_lo == pn_Cmp_Eq && pnc_hi == pn_Cmp_Ge) {
211                         /* x == c || x >= c + 1 -> x >= c */
212                         ir_node  *const block = get_nodes_block(cmp_lo);
213                         ir_node  *const p     = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Ge);
214                         return p;
215                 }
216         }
217         return NULL;
218 }
219
220 static void bool_walk(ir_node *n, void *env)
221 {
222         (void)env;
223
224         if (get_irn_mode(n) != mode_b)
225                 return;
226
227         if (is_And(n)) {
228                 ir_node *const l = get_And_left(n);
229                 ir_node *const r = get_And_right(n);
230                 ir_node *      replacement;
231                 cond_pair      cpair;
232                 if (!find_cond_pair(l, r, &cpair))
233                         return;
234                 replacement = bool_and(&cpair);
235                 if (replacement)
236                         exchange(n, replacement);
237         } else if (is_Or(n)) {
238                 ir_node *const l = get_Or_left(n);
239                 ir_node *const r = get_Or_right(n);
240                 ir_node *      replacement;
241                 cond_pair      cpair;
242                 if (!find_cond_pair(l, r, &cpair))
243                         return;
244                 replacement = bool_or(&cpair);
245                 if (replacement)
246                         exchange(n, replacement);
247         }
248 }
249
250 /**
251  * Walker, clear Block mark and Phi list
252  */
253 static void clear_block_infos(ir_node *node, void *env)
254 {
255         (void) env;
256
257         /* we visit blocks before any other nodes (from the block) */
258         if (!is_Block(node))
259                 return;
260
261         /* clear the PHI list */
262         set_Block_phis(node, NULL);
263         set_Block_mark(node, 0);
264 }
265
266 /**
267  * Walker: collect Phi nodes and update the
268  */
269 static void collect_phis(ir_node *node, void *env)
270 {
271         (void) env;
272
273         if (is_Phi(node)) {
274                 ir_node *block = get_nodes_block(node);
275                 add_Block_phi(block, node);
276                 return;
277         }
278
279         /* Ignore control flow nodes, these will be removed. */
280         if (get_irn_pinned(node) == op_pin_state_pinned &&
281                         !is_Block(node) && !is_cfop(node)) {
282                 ir_node *block = get_nodes_block(node);
283                 set_Block_mark(block, 1);
284         }
285 }
286
287 /**
288  * If node is a Jmp in a block containing no pinned instruction
289  * and having only one predecessor, skip the block and return its
290  * cf predecessor, else the node itself.
291  */
292 static ir_node *skip_empty_block(ir_node *node)
293 {
294         ir_node      *block;
295
296         if(!is_Jmp(node))
297                 return node;
298
299         block = get_nodes_block(node);
300         if(get_Block_n_cfgpreds(block) != 1)
301                 return node;
302
303         if(get_Block_mark(block))
304                 return node;
305
306         return get_Block_cfgpred(block, 0);
307 }
308
309 static void find_cf_and_or_walker(ir_node *block, void *env)
310 {
311         int i, i2;
312         int n_cfgpreds = get_Block_n_cfgpreds(block);
313         (void) env;
314
315         if(n_cfgpreds < 2)
316                 return;
317
318         /* Find the following structure:
319          *
320          *        upper_block
321          *         /       |
322          *        /        |
323          *   lower_block   |
324          *     /  \        |
325          *   ...   \       |
326          *           block
327          */
328
329 restart:
330         for(i = 0; i < n_cfgpreds; ++i) {
331                 ir_node      *lower_block;
332                 ir_node      *lower_cf;
333                 ir_node      *cond;
334                 ir_node      *cond_selector;
335                 ir_node      *lower_pred;
336
337                 lower_cf = get_Block_cfgpred(block, i);
338                 lower_cf = skip_empty_block(lower_cf);
339                 if(!is_Proj(lower_cf))
340                         continue;
341
342                 cond = get_Proj_pred(lower_cf);
343                 if(!is_Cond(cond))
344                         continue;
345
346                 lower_block = get_nodes_block(cond);
347                 if(get_Block_n_cfgpreds(lower_block) != 1)
348                         continue;
349
350                 /* the block must not produce any side-effects */
351                 if(get_Block_mark(lower_block))
352                         continue;
353
354                 cond_selector = get_Cond_selector(cond);
355                 if(get_irn_mode(cond_selector) != mode_b)
356                         continue;
357
358                 lower_pred = get_Block_cfgpred_block(lower_block, 0);
359
360                 for(i2 = 0; i2 < n_cfgpreds; ++i2) {
361                         ir_node   *upper_block;
362                         ir_node   *upper_cf;
363                         ir_node   *upper_cond;
364                         ir_node   *upper_cond_selector;
365                         ir_node   *replacement;
366                         cond_pair  cpair;
367
368                         upper_cf    = get_Block_cfgpred(block, i2);
369                         upper_cf    = skip_empty_block(upper_cf);
370                         if(is_Bad(upper_cf))
371                                 continue;
372                         upper_block = get_nodes_block(upper_cf);
373                         if(upper_block != lower_pred)
374                                 continue;
375
376                         assert(is_Proj(upper_cf));
377                         upper_cond = get_Proj_pred(upper_cf);
378                         assert(is_Cond(upper_cond));
379                         upper_cond_selector = get_Cond_selector(upper_cond);
380                         if(get_irn_mode(upper_cond_selector) != mode_b)
381                                 continue;
382
383                         /* we have found the structure */
384                         /* TODO: check phis */
385                         if(!find_cond_pair(cond_selector, upper_cond_selector, &cpair))
386                                 continue;
387
388                         /* normalize pncs: we need the true case to jump into the
389                          * common block (ie. conjunctive normal form) */
390                         if(get_Proj_proj(lower_cf) == pn_Cond_false) {
391                                 if(cpair.proj_lo == cond_selector) {
392                                         ir_mode *mode = get_tarval_mode(cpair.tv_lo);
393                                         cpair.pnc_lo  = get_negated_pnc(cpair.pnc_lo, mode);
394                                         cpair.proj_lo = new_r_Proj(lower_block,
395                                                         get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
396                                 } else {
397                                         ir_mode *mode = get_tarval_mode(cpair.tv_hi);
398                                         assert(cpair.proj_hi == cond_selector);
399                                         cpair.pnc_hi  = get_negated_pnc(cpair.pnc_hi, mode);
400                                         cpair.proj_hi = new_r_Proj(lower_block,
401                                                         get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
402                                 }
403                         }
404                         if(get_Proj_proj(upper_cf) == pn_Cond_false) {
405                                 if(cpair.proj_lo == upper_cond_selector) {
406                                         ir_mode *mode = get_tarval_mode(cpair.tv_lo);
407                                         cpair.pnc_lo  = get_negated_pnc(cpair.pnc_lo, mode);
408                                         cpair.proj_lo = new_r_Proj(upper_block,
409                                                         get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
410                                 } else {
411                                         ir_mode *mode = get_tarval_mode(cpair.tv_hi);
412                                         assert(cpair.proj_hi == upper_cond_selector);
413                                         cpair.pnc_hi  = get_negated_pnc(cpair.pnc_hi, mode);
414                                         cpair.proj_hi = new_r_Proj(upper_block,
415                                                         get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
416                                 }
417                         }
418
419                         /* can we optimize the case? */
420                         replacement = bool_or(&cpair);
421                         if(replacement == NULL)
422                                 continue;
423
424                         /* move all nodes from lower block to upper block */
425                         exchange(lower_block, upper_block);
426
427                         set_Block_cfgpred(block, i2, new_Bad());
428
429                         /* the optimisations expected the true case to jump */
430                         if(get_Proj_proj(lower_cf) == pn_Cond_false) {
431                                 ir_node *block = get_nodes_block(replacement);
432                                 replacement    = new_rd_Not(NULL, block, replacement, mode_b);
433                         }
434                         set_Cond_selector(cond, replacement);
435
436                         ir_fprintf(stderr, "replaced (ub %+F)\n", upper_block);
437                         goto restart;
438                 }
439         }
440 }
441
442 void opt_bool(ir_graph *const irg)
443 {
444         irg_walk_graph(irg, NULL, bool_walk, NULL);
445
446         ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
447
448         irg_walk_graph(irg, clear_block_infos, collect_phis, NULL);
449
450         irg_block_walk_graph(irg, NULL, find_cf_and_or_walker, NULL);
451
452         set_irg_outs_inconsistent(irg);
453         set_irg_doms_inconsistent(irg);
454         set_irg_extblk_inconsistent(irg);
455         set_irg_loopinfo_inconsistent(irg);
456
457         ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
458 }
459
460 /* Creates an ir_graph pass for opt_bool. */
461 ir_graph_pass_t *opt_bool_pass(const char *name)
462 {
463         return def_graph_pass(name ? name : "opt_bool", opt_bool);
464 }  /* opt_bool_pass */