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