Fixed some obviously wrong conditions.
[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/control flow optimizations
23  * @author  Matthias Braun, Christoph Mallon, Michael Beck
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 "../adt/array_t.h"
33 #include "iroptimize.h"
34 #include "ircons.h"
35 #include "irgmod.h"
36 #include "irgwalk.h"
37 #include "irprintf.h"
38 #include "irnode_t.h"
39 #include "tv.h"
40 #include "irpass.h"
41 #include "debug.h"
42
43 /** Describes a pair of relative conditions lo < hi, lo pnc_lo x, hi pnc_hi x */
44 typedef struct cond_pair {
45         ir_node *cmp_lo;  /**< The lo compare node. */
46         ir_node *cmp_hi;  /**< The hi compare node. */
47         pn_Cmp   pnc_lo;  /**< The lo relation node. */
48         pn_Cmp   pnc_hi;  /**< The hi relation node. */
49         ir_node *proj_lo; /**< The mode_b result proj of cmp_lo. */
50         ir_node *proj_hi; /**< The mode_b result proj of cmp_hi. */
51         tarval  *tv_lo;   /**< The tarval of cmp_lo node. */
52         tarval  *tv_hi;   /**< The tarval of cmp_hi node. */
53         ir_mode *lo_mode; /**< The mode of the cmp_lo operands. */
54 } cond_pair;
55
56 /** Environment for all walker in boolopt. */
57 typedef struct {
58         int changed;  /**< Set if the graph was changed. */
59 } bool_opt_env_t;
60
61 DEBUG_ONLY(static firm_dbg_module_t *dbg);
62
63 /**
64  * Check if tho given nodes, l and r, represent two compares with
65  * ... . If yes, return non-zero and fill the res struct.
66  */
67 static int find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const res)
68 {
69         if (is_Proj(l) && is_Proj(r)) {
70                 ir_node *const lo = get_Proj_pred(l);
71                 ir_node *const ro = get_Proj_pred(r);
72
73                 if (is_Cmp(lo) && is_Cmp(ro)) {
74                         ir_node *const lol   = get_Cmp_left(lo);
75                         ir_node *const lor   = get_Cmp_right(lo);
76                         ir_node *const rol   = get_Cmp_left(ro);
77                         ir_node *const ror   = get_Cmp_right(ro);
78                         pn_Cmp   const pnc_l = get_Proj_proj(l);
79                         pn_Cmp   const pnc_r = get_Proj_proj(r);
80
81                         if (is_Const(lor) && is_Const_null(lor) &&
82                             is_Const(ror) && is_Const_null(ror) &&
83                             pnc_l == pnc_r &&
84                             (pnc_l == pn_Cmp_Lg || pnc_l == pn_Cmp_Eq)) {
85                                 /* lo == (lol !=|== NULL) && ro == (rol !=|== NULL) */
86                                 DB((dbg, LEVEL_1, "found <null null>\n"));
87
88                                 res->cmp_lo  = lo;
89                                 res->cmp_hi  = ro;
90                                 res->pnc_lo  = pnc_l;
91                                 res->pnc_hi  = pnc_l;
92                                 res->proj_lo = l;
93                                 res->proj_hi = r;
94                                 res->tv_lo   = get_Const_tarval(lor);
95                                 res->tv_hi   = get_Const_tarval(ror);
96                                 res->lo_mode = get_irn_mode(lor);
97
98                                 return 1;
99                         }
100
101                         if (lol == rol && lor != ror && is_Const(lor) && is_Const(ror)) {
102                                 /* lo == (x CMP c_l), ro == (x cmp c_r) */
103                                 tarval *const tv_l  = get_Const_tarval(lor);
104                                 tarval *const tv_r  = get_Const_tarval(ror);
105                                 pn_Cmp  const rel   = tarval_cmp(tv_l, tv_r);
106
107                                 res->lo_mode = get_irn_mode(lol);
108
109                                 if (rel == pn_Cmp_Lt) {
110                                         /* c_l < c_r */
111                                         res->cmp_lo  = lo;
112                                         res->cmp_hi  = ro;
113                                         res->pnc_lo  = pnc_l;
114                                         res->pnc_hi  = pnc_r;
115                                         res->proj_lo = l;
116                                         res->proj_hi = r;
117                                         res->tv_lo   = tv_l;
118                                         res->tv_hi   = tv_r;
119                                 } else if (rel == pn_Cmp_Gt) {
120                                         /* c_l > c_r */
121                                         res->cmp_lo  = ro;
122                                         res->cmp_hi  = lo;
123                                         res->pnc_lo  = pnc_r;
124                                         res->pnc_hi  = pnc_l;
125                                         res->proj_lo = r;
126                                         res->proj_hi = l;
127                                         res->tv_lo   = tv_r;
128                                         res->tv_hi   = tv_l;
129                                 } else {
130                                         /* The constants shall be unequal but comparable.
131                                          * Local optimizations handle the equal case. */
132                                         return 0;
133                                 }
134                                 return 1;
135                         }
136                 }
137         }
138         return 0;
139 }
140
141 /**
142  * Handle (lo pnc_lo x) AND (hi pnc_hi x)
143  */
144 static ir_node *bool_and(cond_pair* const cpair)
145 {
146         ir_node *const cmp_lo  = cpair->cmp_lo;
147         ir_node *const cmp_hi  = cpair->cmp_hi;
148         pn_Cmp         pnc_lo  = cpair->pnc_lo;
149         pn_Cmp   const pnc_hi  = cpair->pnc_hi;
150         ir_node *const proj_lo = cpair->proj_lo;
151         ir_node *const proj_hi = cpair->proj_hi;
152         tarval  *      tv_lo   = cpair->tv_lo;
153         tarval  *      tv_hi   = cpair->tv_hi;
154         ir_mode *      mode    = cpair->lo_mode;
155
156         if (pnc_lo == pn_Cmp_Eq && pnc_hi == pn_Cmp_Eq &&
157             tarval_is_null(tv_lo) && tarval_is_null(tv_hi) &&
158             mode == get_tarval_mode(tv_hi)) {
159                 /* p == NULL && q == NULL ==> (p&q) == NULL) */
160                 ir_node *block, *lol, *hil, *cmp, *c, *p;
161
162                 if (mode_is_reference(mode)) {
163                         mode = find_unsigned_mode(mode);
164                         if (! mode)
165                                 return NULL;
166                         tv_lo = tarval_convert_to(tv_lo, mode);
167                         if (tv_lo == tarval_bad)
168                                 return NULL;
169                 }
170                 if (mode_is_int(mode)) {
171                         block = get_nodes_block(cmp_lo);
172                         lol   = get_Cmp_left(cmp_lo);
173                         lol   = new_r_Conv(block, lol, mode);
174                         hil   = get_Cmp_left(cmp_hi);
175                         hil   = new_r_Conv(block, hil, mode);
176                         p     = new_r_And(block, lol, hil, mode);
177                         c     = new_Const(tv_lo);
178                         cmp   = new_r_Cmp(block, p, c);
179                         p     = new_r_Proj(block, cmp, mode_b, pn_Cmp_Eq);
180                         return p;
181                 }
182         }
183
184         /* TODO: for now reject float modes */
185         if (! mode_is_int(mode))
186                 return 0;
187
188         /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
189         if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
190             (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
191                 /* x <|<=|== lo && x ==|>=|> hi ==> false */
192                 ir_node *const t = new_Const(tarval_b_false);
193                 return t;
194         } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
195                    (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
196                 /* x <|<=|== lo && x <|<=|!= hi ==> x <|<=|== lo */
197                 return proj_lo;
198         } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
199                    (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
200                 /* x >=|>|!= lo && x ==|>=|> hi ==> x ==|>=|> hi */
201                 return proj_hi;
202         } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
203                 if (pnc_lo == pn_Cmp_Ge && pnc_hi == pn_Cmp_Lt) {
204                         /* x >= c && x < c + 1 ==> x == c */
205                         ir_node  *const block = get_nodes_block(cmp_lo);
206                         ir_node  *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Eq);
207                         return p;
208                 } else if (pnc_lo == pn_Cmp_Gt) {
209                         if (pnc_hi == pn_Cmp_Lg) {
210                                 /* x > c && x != c + 1 ==> x > c + 1 */
211                                 ir_node  *const block = get_nodes_block(cmp_hi);
212                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Gt);
213                                 return p;
214                         } else if (pnc_hi == pn_Cmp_Lt) {
215                                 /* x > c && x < c + 1 ==> false */
216                                 ir_node *const t = new_Const(tarval_b_false);
217                                 return t;
218                         } else if (pnc_hi == pn_Cmp_Le) {
219                                 /* x > c && x <= c + 1 ==> x != c + 1 */
220                                 ir_node  *const block = get_nodes_block(cmp_hi);
221                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Eq);
222                                 return p;
223                         }
224                 } else if (pnc_lo == pn_Cmp_Lg && pnc_hi == pn_Cmp_Lt) {
225                         /* x != c && c < c + 1 ==> x < c */
226                         ir_node  *const block = get_nodes_block(cmp_lo);
227                         ir_node  *const p     = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lt);
228                         return p;
229                 }
230         } else if ((pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Ge) &&
231                    (pnc_hi == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le) &&
232                    get_mode_arithmetic(mode) == irma_twos_complement) {
233                 /* works for two-complements only */
234                 /* x >|\= lo && x <|<= hi ==> (x - lo) <u|<=u (hi-lo) */
235                 if (pnc_lo == pn_Cmp_Gt) {
236                         /* must convert to >= */
237                         ir_mode *mode = get_tarval_mode(tv_lo);
238                         tarval *n = tarval_add(tv_lo, get_mode_one(mode));
239                         if (n != tarval_bad && tarval_cmp(n, tv_lo) == pn_Cmp_Gt) {
240                                 /* no overflow */
241                                 tv_lo = n;
242                                 pnc_lo = pn_Cmp_Ge;
243                         }
244                 }
245                 if (pnc_lo == pn_Cmp_Ge) {
246                         /* all fine */
247                         ir_node *const block = get_nodes_block(cmp_hi);
248                         ir_node *      x     = get_Cmp_left(cmp_hi);
249                         ir_mode *      mode  = get_irn_mode(x);
250                         ir_node *sub, *cmp, *c, *subc, *p;
251
252                         if (mode_is_signed(mode)) {
253                                 /* convert to unsigned */
254                                 mode = find_unsigned_mode(mode);
255                                 if (mode == NULL)
256                                         return NULL;
257                                 x     = new_r_Conv(block, x, mode);
258                                 tv_lo = tarval_convert_to(tv_lo, mode);
259                                 tv_hi = tarval_convert_to(tv_hi, mode);
260                                 if (tv_lo == tarval_bad || tv_hi == tarval_bad)
261                                         return NULL;
262                         }
263                         c    = new_Const(tv_lo);
264                         sub  = new_r_Sub(block, x, c, mode);
265                         subc = new_r_Sub(block, new_Const(tv_hi), c, mode);
266                         cmp  = new_r_Cmp(block, sub, subc);
267                         p    = new_r_Proj(block, cmp, mode_b, pnc_hi);
268                         return p;
269                 }
270         }
271         return NULL;
272 }
273
274 /**
275  * Handle (lo pnc_lo x) OR (hi pnc_hi x)
276  */
277 static ir_node *bool_or(cond_pair *const cpair)
278 {
279         ir_node *const cmp_lo  = cpair->cmp_lo;
280         ir_node *const cmp_hi  = cpair->cmp_hi;
281         pn_Cmp         pnc_lo  = cpair->pnc_lo;
282         pn_Cmp   const pnc_hi  = cpair->pnc_hi;
283         ir_node *const proj_lo = cpair->proj_lo;
284         ir_node *const proj_hi = cpair->proj_hi;
285         tarval  *      tv_lo   = cpair->tv_lo;
286         tarval  *      tv_hi   = cpair->tv_hi;
287         ir_mode *      mode    = cpair->lo_mode;
288
289         if (pnc_lo == pn_Cmp_Lg && pnc_hi == pn_Cmp_Lg &&
290                 tarval_is_null(tv_lo) && tarval_is_null(tv_hi) &&
291                 mode == get_tarval_mode(tv_hi)) {
292                 /* p != NULL || q != NULL ==> (p|q) != NULL) */
293                 ir_node *block, *lol, *hil, *cmp, *c, *p;
294
295                 if (mode_is_reference(mode)) {
296                         mode = find_unsigned_mode(mode);
297                         if (! mode)
298                                 return NULL;
299                         tv_lo = tarval_convert_to(tv_lo, mode);
300                         if (tv_lo == tarval_bad)
301                                 return NULL;
302                 }
303                 if (mode_is_int(mode)) {
304                         block = get_nodes_block(cmp_lo);
305                         lol   = get_Cmp_left(cmp_lo);
306                         lol   = new_r_Conv(block, lol, mode);
307                         hil   = get_Cmp_left(cmp_hi);
308                         hil   = new_r_Conv(block, hil, mode);
309                         p     = new_r_Or(block, lol, hil, mode);
310                         c     = new_Const(tv_lo);
311                         cmp   = new_r_Cmp(block, p, c);
312                         p     = new_r_Proj(block, cmp, mode_b, pn_Cmp_Lg);
313                         return p;
314                 }
315         }
316
317         /* TODO: for now reject float modes */
318         if (! mode_is_int(mode))
319                 return 0;
320
321         /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
322         if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
323             (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
324                 /* x >=|>|!= lo | x <|<=|!= hi ==> true */
325                 ir_node *const t = new_Const(tarval_b_true);
326                 return t;
327         } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
328                    (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
329                 /* x <|<=|== lo || x <|<=|!= hi ==> x <|<=|!= hi */
330                 return proj_hi;
331         } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
332                    (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
333                 /* x >=|>|!= lo || x ==|>=|> hi ==> x >=|>|!= lo */
334                 return proj_lo;
335         } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
336                 if (pnc_lo == pn_Cmp_Lt && pnc_hi == pn_Cmp_Ge) {
337                         /* x < c || x >= c + 1 ==> x != c */
338                         ir_node  *const block = get_nodes_block(cmp_lo);
339                         ir_node  *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lg);
340                         return p;
341                 } else if (pnc_lo == pn_Cmp_Le) {
342                         if (pnc_hi == pn_Cmp_Eq) {
343                                 /* x <= c || x == c + 1 ==> x <= c + 1 */
344                                 ir_node  *const block = get_nodes_block(cmp_hi);
345                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Le);
346                                 return p;
347                         } else if (pnc_hi == pn_Cmp_Ge) {
348                                 /* x <= c || x >= c + 1 ==> true */
349                                 ir_node *const t = new_Const(tarval_b_true);
350                                 return t;
351                         } else if (pnc_hi == pn_Cmp_Gt) {
352                                 /* x <= c || x > c + 1 ==> x != c + 1 */
353                                 ir_node  *const block = get_nodes_block(cmp_hi);
354                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Lg);
355                                 return p;
356                         }
357                 } else if (pnc_lo == pn_Cmp_Eq && pnc_hi == pn_Cmp_Ge) {
358                         /* x == c || x >= c + 1 ==> x >= c */
359                         ir_node  *const block = get_nodes_block(cmp_lo);
360                         ir_node  *const p     = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Ge);
361                         return p;
362                 }
363         } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le) &&
364                    (pnc_hi == pn_Cmp_Gt || pnc_lo == pn_Cmp_Ge) &&
365                    get_mode_arithmetic(mode) == irma_twos_complement) {
366                 /* works for two-complements only */
367                 /* x <|\= lo  || x >|>= hi ==> (x - lo) >u|>=u (hi-lo) */
368                 if (pnc_lo == pn_Cmp_Lt) {
369                         /* must convert to <= */
370                         ir_mode *mode = get_tarval_mode(tv_lo);
371                         tarval *n = tarval_sub(tv_lo, get_mode_one(mode), NULL);
372                         if (n != tarval_bad && tarval_cmp(n, tv_lo) == pn_Cmp_Lt) {
373                                 /* no overflow */
374                                 tv_lo = n;
375                                 pnc_lo = pn_Cmp_Le;
376                         }
377                 }
378                 if (pnc_lo == pn_Cmp_Le) {
379                         /* all fine */
380                         ir_node *const block = get_nodes_block(cmp_hi);
381                         ir_node *      x     = get_Cmp_left(cmp_hi);
382                         ir_mode *      mode  = get_irn_mode(x);
383                         ir_node *sub, *cmp, *c, *subc, *p;
384
385                         if (mode_is_signed(mode)) {
386                                 /* convert to unsigned */
387                                 mode = find_unsigned_mode(mode);
388                                 if (mode == NULL)
389                                         return NULL;
390                                 x     = new_r_Conv(block, x, mode);
391                                 tv_lo = tarval_convert_to(tv_lo, mode);
392                                 tv_hi = tarval_convert_to(tv_hi, mode);
393                                 if (tv_lo == tarval_bad || tv_hi == tarval_bad)
394                                         return NULL;
395                         }
396                         c    = new_Const(tv_lo);
397                         sub  = new_r_Sub(block, x, c, mode);
398                         subc = new_r_Sub(block, new_Const(tv_hi), c, mode);
399                         cmp  = new_r_Cmp(block, sub, subc);
400                         p    = new_r_Proj(block, cmp, mode_b, pnc_hi);
401                         return p;
402                 }
403         }
404         return NULL;
405 }
406
407 /**
408  * Walker, tries to optimize Andb and Orb nodes.
409  */
410 static void bool_walk(ir_node *n, void *ctx)
411 {
412         bool_opt_env_t *env = ctx;
413
414         if (get_irn_mode(n) != mode_b)
415                 return;
416
417         if (is_And(n)) {
418                 ir_node *const l = get_And_left(n);
419                 ir_node *const r = get_And_right(n);
420                 ir_node *      replacement;
421                 cond_pair      cpair;
422                 if (!find_cond_pair(l, r, &cpair))
423                         return;
424                 replacement = bool_and(&cpair);
425                 if (replacement) {
426                         exchange(n, replacement);
427                         env->changed = 1;
428                 }
429         } else if (is_Or(n)) {
430                 ir_node *const l = get_Or_left(n);
431                 ir_node *const r = get_Or_right(n);
432                 ir_node *      replacement;
433                 cond_pair      cpair;
434                 if (!find_cond_pair(l, r, &cpair))
435                         return;
436                 replacement = bool_or(&cpair);
437                 if (replacement) {
438                         exchange(n, replacement);
439                         env->changed = 1;
440                 }
441         }
442 }
443
444 /**
445  * Walker, clear Block marker and Phi lists.
446  */
447 static void clear_block_infos(ir_node *node, void *env)
448 {
449         (void) env;
450
451         /* we visit blocks before any other nodes (from the block) */
452         if (!is_Block(node))
453                 return;
454
455         /* clear the PHI list */
456         set_Block_phis(node, NULL);
457         set_Block_mark(node, 0);
458 }
459
460 /**
461  * Walker: collect Phi nodes and mark
462  */
463 static void collect_phis(ir_node *node, void *env)
464 {
465         (void) env;
466
467         if (is_Phi(node)) {
468                 ir_node *block = get_nodes_block(node);
469                 add_Block_phi(block, node);
470                 return;
471         }
472
473         /* Ignore control flow nodes, these will be removed. */
474         if (get_irn_pinned(node) == op_pin_state_pinned &&
475                         !is_Block(node) && !is_cfop(node)) {
476                                 /* found a pinned non-cf node, mark its block */
477                 ir_node *block = get_nodes_block(node);
478                 set_Block_mark(block, 1);
479         }
480 }
481
482 /**
483  * If node is a Jmp in a block containing no pinned instruction
484  * and having only one predecessor, skip the block and return its
485  * cf predecessor, else the node itself.
486  */
487 static ir_node *skip_empty_blocks(ir_node *node)
488 {
489         while (is_Jmp(node)) {
490                 ir_node *block = get_nodes_block(node);
491
492                 if (get_Block_n_cfgpreds(block) != 1)
493                         break;
494
495                 if (get_Block_mark(block))
496                         break;
497
498                 node = get_Block_cfgpred(block, 0);
499         }
500         return node;
501 }
502
503 /**
504  * Check if two block inputs can be fused.
505  * This can be done, if block contains no Phi node that depends on
506  * different inputs idx_i and idx_j.
507  */
508 static int can_fuse_block_inputs(const ir_node *block, int idx_i, int idx_j) {
509         const ir_node *phi;
510
511         for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
512                 if (get_Phi_pred(phi, idx_i) != get_Phi_pred(phi, idx_j))
513                         return 0;
514         }
515         return 1;
516 }
517
518 /**
519  * Remove block input with given index.
520  */
521 static void remove_block_input(ir_node *block, int idx)
522 {
523         int i, j, n = get_Block_n_cfgpreds(block) - 1;
524         ir_node *phi, **ins;
525
526         NEW_ARR_A(ir_node *, ins, n);
527
528         if (n == 1) {
529                 /* all Phis will be deleted */
530                 ir_node *next_phi;
531
532                 for (phi = get_Block_phis(block); phi != NULL; phi = next_phi) {
533                         next_phi = get_Phi_next(phi);
534                         exchange(phi, get_Phi_pred(phi, idx ^ 1));
535                 }
536                 set_Block_phis(block, NULL);
537         } else {
538                 for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
539                         for (i = j = 0; i <= n; ++i) {
540                                 if (i != idx)
541                                         ins[j++] = get_Phi_pred(phi, i);
542                         }
543                         set_irn_in(phi, n, ins);
544                 }
545         }
546         for (i = j = 0; i <= n; ++i) {
547                 if (i != idx)
548                         ins[j++] = get_Block_cfgpred(block, i);
549         }
550         set_irn_in(block, n, ins);
551 }
552
553 /**
554  * Under the preposition that we have a chain of blocks from
555  * from_block to to_block, collapse them all into to_block.
556  */
557 static void move_nodes_to_block(ir_node *jmp, ir_node *to_block) {
558         ir_node *new_jmp = NULL;
559         ir_node *block, *next_block;
560
561         for (block = get_nodes_block(jmp); block != to_block; block = next_block) {
562                 new_jmp = get_Block_cfgpred(block, 0);
563                 next_block = get_nodes_block(new_jmp);
564                 exchange(block, to_block);
565         }
566         if (new_jmp)
567                 exchange(jmp, new_jmp);
568 }
569
570 /**
571  * Block walker:
572  *
573  * if we can find the following structure,
574  *
575  *        upper_block
576  *         /       |
577  *        /        |
578  *   lower_block   |
579  *     /  \        |
580  *   ... low_idx up_idx
581  *          \      |
582  *            block
583  *
584  * try to convert it into a (x pnc_lo c_lo) || (x pnc_hi c_hi)
585  * and optimize.
586  */
587 static void find_cf_and_or_walker(ir_node *block, void *ctx)
588 {
589         int low_idx, up_idx;
590         int n_cfgpreds = get_Block_n_cfgpreds(block);
591         bool_opt_env_t *env = ctx;
592
593 restart:
594         if (n_cfgpreds < 2)
595                 return;
596
597         for (low_idx = 0; low_idx < n_cfgpreds; ++low_idx) {
598                 ir_node      *lower_block;
599                 ir_node      *lower_cf;
600                 ir_node      *cond;
601                 ir_node      *cond_selector;
602                 ir_node      *lower_pred;
603
604                 lower_cf = get_Block_cfgpred(block, low_idx);
605                 lower_cf = skip_empty_blocks(lower_cf);
606                 if (!is_Proj(lower_cf))
607                         continue;
608
609                 cond = get_Proj_pred(lower_cf);
610                 if (!is_Cond(cond))
611                         continue;
612
613                 lower_block = get_nodes_block(cond);
614                 if (get_Block_n_cfgpreds(lower_block) != 1)
615                         continue;
616
617                 /* the block must not produce any side-effects */
618                 if (get_Block_mark(lower_block))
619                         continue;
620
621                 cond_selector = get_Cond_selector(cond);
622                 if (get_irn_mode(cond_selector) != mode_b)
623                         continue;
624
625                 lower_pred = get_Block_cfgpred_block(lower_block, 0);
626
627                 for (up_idx = 0; up_idx < n_cfgpreds; ++up_idx) {
628                         ir_node   *upper_block;
629                         ir_node   *upper_cf;
630                         ir_node   *upper_cond;
631                         ir_node   *upper_cond_selector;
632                         ir_node   *replacement;
633                         cond_pair  cpair;
634
635                         upper_cf    = get_Block_cfgpred(block, up_idx);
636                         upper_cf    = skip_empty_blocks(upper_cf);
637                         if (is_Bad(upper_cf))
638                                 continue;
639                         upper_block = get_nodes_block(upper_cf);
640                         if (upper_block != lower_pred)
641                                 continue;
642
643                         assert(is_Proj(upper_cf));
644                         upper_cond = get_Proj_pred(upper_cf);
645                         assert(is_Cond(upper_cond));
646                         upper_cond_selector = get_Cond_selector(upper_cond);
647                         if (get_irn_mode(upper_cond_selector) != mode_b)
648                                 continue;
649
650                         /* we have found the structure */
651                         /* check Phis: There must be NO Phi in block that
652                            depends on the existence of low block */
653                         if (!can_fuse_block_inputs(block, low_idx, up_idx))
654                                 continue;
655
656                         /* all fine, try it */
657                         if (!find_cond_pair(cond_selector, upper_cond_selector, &cpair))
658                                 continue;
659
660                         /* normalize pncs: we need the true case to jump into the
661                          * common block (ie. conjunctive normal form) */
662                         if (get_Proj_proj(lower_cf) == pn_Cond_false) {
663                                 if (cpair.proj_lo == cond_selector) {
664                                         ir_mode *mode = get_tarval_mode(cpair.tv_lo);
665                                         cpair.pnc_lo  = get_negated_pnc(cpair.pnc_lo, mode);
666                                         cpair.proj_lo = new_r_Proj(lower_block,
667                                                         get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
668                                 } else {
669                                         ir_mode *mode = get_tarval_mode(cpair.tv_hi);
670                                         assert(cpair.proj_hi == cond_selector);
671                                         cpair.pnc_hi  = get_negated_pnc(cpair.pnc_hi, mode);
672                                         cpair.proj_hi = new_r_Proj(lower_block,
673                                                         get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
674                                 }
675                         }
676                         if (get_Proj_proj(upper_cf) == pn_Cond_false) {
677                                 if (cpair.proj_lo == upper_cond_selector) {
678                                         ir_mode *mode = get_tarval_mode(cpair.tv_lo);
679                                         cpair.pnc_lo  = get_negated_pnc(cpair.pnc_lo, mode);
680                                         cpair.proj_lo = new_r_Proj(upper_block,
681                                                         get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
682                                 } else {
683                                         ir_mode *mode = get_tarval_mode(cpair.tv_hi);
684                                         assert(cpair.proj_hi == upper_cond_selector);
685                                         cpair.pnc_hi  = get_negated_pnc(cpair.pnc_hi, mode);
686                                         cpair.proj_hi = new_r_Proj(upper_block,
687                                                         get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
688                                 }
689                         }
690
691                         /* can we optimize the case? */
692                         replacement = bool_or(&cpair);
693                         if (replacement == NULL)
694                                 continue;
695
696                         env->changed = 1;
697
698                         /* move all expressions on the path to lower/upper block */
699                         move_nodes_to_block(get_Block_cfgpred(block, up_idx), upper_block);
700                         move_nodes_to_block(get_Block_cfgpred(block, low_idx), lower_block);
701
702                         /* move all nodes from lower block to upper block */
703                         exchange(lower_block, upper_block);
704
705                         remove_block_input(block, up_idx);
706                         --n_cfgpreds;
707
708                         /* the optimizations expected the true case to jump */
709                         if (get_Proj_proj(lower_cf) == pn_Cond_false) {
710                                 ir_node *block = get_nodes_block(replacement);
711                                 replacement    = new_rd_Not(NULL, block, replacement, mode_b);
712                         }
713                         set_Cond_selector(cond, replacement);
714
715                         DB((dbg, LEVEL_1, "%+F: replaced (ub %+F)\n", current_ir_graph, upper_block));
716                         goto restart;
717                 }
718         }
719 }
720
721 void opt_bool(ir_graph *const irg)
722 {
723         bool_opt_env_t env;
724
725         /* register a debug mask */
726         FIRM_DBG_REGISTER(dbg, "firm.opt.bool");
727
728         /* works better with one return block only */
729         normalize_one_return(irg);
730
731         env.changed = 0;
732
733         /* optimize simple Andb and Orb cases */
734         irg_walk_graph(irg, NULL, bool_walk, &env);
735
736         ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
737
738         /* now more complicated cases: find control flow And/Or and optimize. */
739         irg_walk_graph(irg, clear_block_infos, collect_phis, NULL);
740         irg_block_walk_graph(irg, NULL, find_cf_and_or_walker, &env);
741
742         if (env.changed) {
743                 set_irg_outs_inconsistent(irg);
744                 set_irg_doms_inconsistent(irg);
745                 set_irg_extblk_inconsistent(irg);
746                 set_irg_loopinfo_inconsistent(irg);
747         }
748
749         ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
750 }
751
752 /* Creates an ir_graph pass for opt_bool. */
753 ir_graph_pass_t *opt_bool_pass(const char *name)
754 {
755         return def_graph_pass(name ? name : "opt_bool", opt_bool);
756 }  /* opt_bool_pass */