BugFix: fixed conversion for x < lo || x >|>= hi
[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(dst_block, 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 block = get_nodes_block(cmp_lo);
207                         ir_node  *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Eq);
208                         return p;
209                 } else if (pnc_lo == pn_Cmp_Gt) {
210                         if (pnc_hi == pn_Cmp_Lg) {
211                                 /* x > c && x != c + 1 ==> x > c + 1 */
212                                 ir_node  *const block = get_nodes_block(cmp_hi);
213                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Gt);
214                                 return p;
215                         } else if (pnc_hi == pn_Cmp_Lt) {
216                                 /* x > c && x < c + 1 ==> false */
217                                 ir_node *const t = new_Const(tarval_b_false);
218                                 return t;
219                         } else if (pnc_hi == pn_Cmp_Le) {
220                                 /* x > c && x <= c + 1 ==> x != c + 1 */
221                                 ir_node  *const block = get_nodes_block(cmp_hi);
222                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Eq);
223                                 return p;
224                         }
225                 } else if (pnc_lo == pn_Cmp_Lg && pnc_hi == pn_Cmp_Lt) {
226                         /* x != c && c < c + 1 ==> x < c */
227                         ir_node  *const block = get_nodes_block(cmp_lo);
228                         ir_node  *const p     = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lt);
229                         return p;
230                 }
231         } else if ((pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Ge) &&
232                    (pnc_hi == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le) &&
233                    get_mode_arithmetic(mode) == irma_twos_complement) {
234                 /* works for two-complements only */
235                 /* x >|\= lo && x <|<= hi ==> (x - lo) <u|<=u (hi-lo) */
236                 if (pnc_lo == pn_Cmp_Gt) {
237                         /* must convert to >= */
238                         ir_mode *mode = get_tarval_mode(tv_lo);
239                         tarval *n = tarval_add(tv_lo, get_mode_one(mode));
240                         if (n != tarval_bad && tarval_cmp(n, tv_lo) == pn_Cmp_Gt) {
241                                 /* no overflow */
242                                 tv_lo = n;
243                                 pnc_lo = pn_Cmp_Ge;
244                         }
245                 }
246                 if (pnc_lo == pn_Cmp_Ge) {
247                         /* all fine */
248                         ir_node *const block = get_nodes_block(cmp_hi);
249                         ir_node *      x     = get_Cmp_left(cmp_hi);
250                         ir_mode *      mode  = get_irn_mode(x);
251                         ir_node *sub, *cmp, *c, *subc, *p;
252
253                         if (mode_is_signed(mode)) {
254                                 /* convert to unsigned */
255                                 mode = find_unsigned_mode(mode);
256                                 if (mode == NULL)
257                                         return NULL;
258                                 x     = new_r_Conv(block, x, mode);
259                                 tv_lo = tarval_convert_to(tv_lo, mode);
260                                 tv_hi = tarval_convert_to(tv_hi, mode);
261                                 if (tv_lo == tarval_bad || tv_hi == tarval_bad)
262                                         return NULL;
263                         }
264                         c    = new_Const(tv_lo);
265                         sub  = new_r_Sub(block, x, c, mode);
266                         subc = new_r_Sub(block, new_Const(tv_hi), c, mode);
267                         cmp  = new_r_Cmp(block, sub, subc);
268                         p    = new_r_Proj(block, cmp, mode_b, pnc_hi);
269                         return p;
270                 }
271         }
272         return NULL;
273 }
274
275 /**
276  * Handle (lo pnc_lo x) OR (hi pnc_hi x)
277  */
278 static ir_node *bool_or(cond_pair *const cpair, ir_node *dst_block)
279 {
280         ir_node *const cmp_lo  = cpair->cmp_lo;
281         ir_node *const cmp_hi  = cpair->cmp_hi;
282         pn_Cmp         pnc_lo  = cpair->pnc_lo;
283         pn_Cmp   const pnc_hi  = cpair->pnc_hi;
284         ir_node *const proj_lo = cpair->proj_lo;
285         ir_node *const proj_hi = cpair->proj_hi;
286         tarval  *      tv_lo   = cpair->tv_lo;
287         tarval  *      tv_hi   = cpair->tv_hi;
288         ir_mode *      mode    = cpair->lo_mode;
289
290         if (pnc_lo == pn_Cmp_Lg && pnc_hi == pn_Cmp_Lg &&
291                 tarval_is_null(tv_lo) && tarval_is_null(tv_hi) &&
292                 mode == get_tarval_mode(tv_hi)) {
293                 /* p != NULL || q != NULL ==> (p|q) != NULL) */
294                 ir_node *lol, *hil, *cmp, *c, *p;
295
296                 if (mode_is_reference(mode)) {
297                         mode = find_unsigned_mode(mode);
298                         if (! mode)
299                                 return NULL;
300                         tv_lo = tarval_convert_to(tv_lo, mode);
301                         if (tv_lo == tarval_bad)
302                                 return NULL;
303                 }
304                 if (mode_is_int(mode)) {
305                         lol   = get_Cmp_left(cmp_lo);
306                         lol   = new_r_Conv(dst_block, lol, mode);
307                         hil   = get_Cmp_left(cmp_hi);
308                         hil   = new_r_Conv(dst_block, hil, mode);
309                         p     = new_r_Or(dst_block, lol, hil, mode);
310                         c     = new_Const(tv_lo);
311                         cmp   = new_r_Cmp(dst_block, p, c);
312                         p     = new_r_Proj(dst_block, cmp, mode_b, pn_Cmp_Lg);
313                         return p;
314                 }
315         }
316
317         /* the following tests expect one common operand */
318         if (get_Cmp_left(cmp_lo) !=  get_Cmp_left(cmp_hi))
319                 return 0;
320
321         /* TODO: for now reject float modes */
322         if (! mode_is_int(mode))
323                 return 0;
324
325         /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
326         if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
327             (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
328                 /* x >=|>|!= lo | x <|<=|!= hi ==> true */
329                 ir_node *const t = new_Const(tarval_b_true);
330                 return t;
331         } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
332                    (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
333                 /* x <|<=|== lo || x <|<=|!= hi ==> x <|<=|!= hi */
334                 return proj_hi;
335         } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
336                    (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
337                 /* x >=|>|!= lo || x ==|>=|> hi ==> x >=|>|!= lo */
338                 return proj_lo;
339         } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
340                 if (pnc_lo == pn_Cmp_Lt && pnc_hi == pn_Cmp_Ge) {
341                         /* x < c || x >= c + 1 ==> x != c */
342                         ir_node  *const block = get_nodes_block(cmp_lo);
343                         ir_node  *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lg);
344                         return p;
345                 } else if (pnc_lo == pn_Cmp_Le) {
346                         if (pnc_hi == pn_Cmp_Eq) {
347                                 /* x <= c || x == c + 1 ==> x <= c + 1 */
348                                 ir_node  *const block = get_nodes_block(cmp_hi);
349                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Le);
350                                 return p;
351                         } else if (pnc_hi == pn_Cmp_Ge) {
352                                 /* x <= c || x >= c + 1 ==> true */
353                                 ir_node *const t = new_Const(tarval_b_true);
354                                 return t;
355                         } else if (pnc_hi == pn_Cmp_Gt) {
356                                 /* x <= c || x > c + 1 ==> x != c + 1 */
357                                 ir_node  *const block = get_nodes_block(cmp_hi);
358                                 ir_node  *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Lg);
359                                 return p;
360                         }
361                 } else if (pnc_lo == pn_Cmp_Eq && pnc_hi == pn_Cmp_Ge) {
362                         /* x == c || x >= c + 1 ==> x >= c */
363                         ir_node  *const block = get_nodes_block(cmp_lo);
364                         ir_node  *const p     = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Ge);
365                         return p;
366                 }
367         } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le) &&
368                    (pnc_hi == pn_Cmp_Gt || pnc_lo == pn_Cmp_Ge) &&
369                    get_mode_arithmetic(mode) == irma_twos_complement) {
370                 /* works for two-complements only */
371                 /* x <|<= lo  || x >|>= hi ==> (x - lo) >u|>=u (hi-lo) */
372                 if (pnc_lo == pn_Cmp_Le) {
373                         /* must convert to < */
374                         ir_mode *mode = get_tarval_mode(tv_lo);
375                         tarval *n = tarval_add(tv_lo, get_mode_one(mode));
376                         if (n != tarval_bad && tarval_cmp(n, tv_lo) == pn_Cmp_Gt) {
377                                 /* no overflow */
378                                 tv_lo = n;
379                                 pnc_lo = pn_Cmp_Lt;
380                         }
381                 }
382                 if (pnc_lo == pn_Cmp_Lt) {
383                         /* all fine */
384                         ir_node *const block = get_nodes_block(cmp_hi);
385                         ir_node *      x     = get_Cmp_left(cmp_hi);
386                         ir_mode *      mode  = get_irn_mode(x);
387                         ir_node *sub, *cmp, *c, *subc, *p;
388
389                         if (mode_is_signed(mode)) {
390                                 /* convert to unsigned */
391                                 mode = find_unsigned_mode(mode);
392                                 if (mode == NULL)
393                                         return NULL;
394                                 x     = new_r_Conv(block, x, mode);
395                                 tv_lo = tarval_convert_to(tv_lo, mode);
396                                 tv_hi = tarval_convert_to(tv_hi, mode);
397                                 if (tv_lo == tarval_bad || tv_hi == tarval_bad)
398                                         return NULL;
399                         }
400                         c    = new_Const(tv_lo);
401                         sub  = new_r_Sub(block, x, c, mode);
402                         subc = new_r_Sub(block, new_Const(tv_hi), c, mode);
403                         cmp  = new_r_Cmp(block, sub, subc);
404                         p    = new_r_Proj(block, cmp, mode_b, pnc_hi);
405                         return p;
406                 }
407         }
408         return NULL;
409 }
410
411 /**
412  * Walker, tries to optimize Andb and Orb nodes.
413  */
414 static void bool_walk(ir_node *n, void *ctx)
415 {
416         bool_opt_env_t *env = ctx;
417
418         if (get_irn_mode(n) != mode_b)
419                 return;
420
421         if (is_And(n)) {
422                 ir_node *const l = get_And_left(n);
423                 ir_node *const r = get_And_right(n);
424                 ir_node *      replacement;
425                 cond_pair      cpair;
426                 if (!find_cond_pair(l, r, &cpair))
427                         return;
428                 replacement = bool_and(&cpair, get_nodes_block(n));
429                 if (replacement) {
430                         exchange(n, replacement);
431                         env->changed = 1;
432                 }
433         } else if (is_Or(n)) {
434                 ir_node *const l = get_Or_left(n);
435                 ir_node *const r = get_Or_right(n);
436                 ir_node *      replacement;
437                 cond_pair      cpair;
438                 if (!find_cond_pair(l, r, &cpair))
439                         return;
440                 replacement = bool_or(&cpair, get_nodes_block(n));
441                 if (replacement) {
442                         exchange(n, replacement);
443                         env->changed = 1;
444                 }
445         }
446 }
447
448 /**
449  * Walker, clear Block marker and Phi lists.
450  */
451 static void clear_block_infos(ir_node *node, void *env)
452 {
453         (void) env;
454
455         /* we visit blocks before any other nodes (from the block) */
456         if (!is_Block(node))
457                 return;
458
459         /* clear the PHI list */
460         set_Block_phis(node, NULL);
461         set_Block_mark(node, 0);
462 }
463
464 /**
465  * Walker: collect Phi nodes and mark
466  */
467 static void collect_phis(ir_node *node, void *env)
468 {
469         (void) env;
470
471         if (is_Phi(node)) {
472                 ir_node *block = get_nodes_block(node);
473                 add_Block_phi(block, node);
474                 return;
475         }
476
477         /* Ignore control flow nodes, these will be removed. */
478         if (get_irn_pinned(node) == op_pin_state_pinned &&
479                         !is_Block(node) && !is_cfop(node)) {
480                                 /* found a pinned non-cf node, mark its block */
481                 ir_node *block = get_nodes_block(node);
482                 set_Block_mark(block, 1);
483         }
484 }
485
486 /**
487  * If node is a Jmp in a block containing no pinned instruction
488  * and having only one predecessor, skip the block and return its
489  * cf predecessor, else the node itself.
490  */
491 static ir_node *skip_empty_blocks(ir_node *node)
492 {
493         while (is_Jmp(node)) {
494                 ir_node *block = get_nodes_block(node);
495
496                 if (get_Block_n_cfgpreds(block) != 1)
497                         break;
498
499                 if (get_Block_mark(block))
500                         break;
501
502                 node = get_Block_cfgpred(block, 0);
503         }
504         return node;
505 }
506
507 /**
508  * Check if two block inputs can be fused.
509  * This can be done, if block contains no Phi node that depends on
510  * different inputs idx_i and idx_j.
511  */
512 static int can_fuse_block_inputs(const ir_node *block, int idx_i, int idx_j) {
513         const ir_node *phi;
514
515         for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
516                 if (get_Phi_pred(phi, idx_i) != get_Phi_pred(phi, idx_j))
517                         return 0;
518         }
519         return 1;
520 }
521
522 /**
523  * Remove block input with given index.
524  */
525 static void remove_block_input(ir_node *block, int idx)
526 {
527         int i, j, n = get_Block_n_cfgpreds(block) - 1;
528         ir_node *phi, **ins;
529
530         NEW_ARR_A(ir_node *, ins, n);
531
532         if (n == 1) {
533                 /* all Phis will be deleted */
534                 ir_node *next_phi;
535
536                 for (phi = get_Block_phis(block); phi != NULL; phi = next_phi) {
537                         next_phi = get_Phi_next(phi);
538                         exchange(phi, get_Phi_pred(phi, idx ^ 1));
539                 }
540                 set_Block_phis(block, NULL);
541         } else {
542                 for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
543                         for (i = j = 0; i <= n; ++i) {
544                                 if (i != idx)
545                                         ins[j++] = get_Phi_pred(phi, i);
546                         }
547                         set_irn_in(phi, n, ins);
548                 }
549         }
550         for (i = j = 0; i <= n; ++i) {
551                 if (i != idx)
552                         ins[j++] = get_Block_cfgpred(block, i);
553         }
554         set_irn_in(block, n, ins);
555 }
556
557 /**
558  * Under the preposition that we have a chain of blocks from
559  * from_block to to_block, collapse them all into to_block.
560  */
561 static void move_nodes_to_block(ir_node *jmp, ir_node *to_block) {
562         ir_node *new_jmp = NULL;
563         ir_node *block, *next_block;
564
565         for (block = get_nodes_block(jmp); block != to_block; block = next_block) {
566                 new_jmp = get_Block_cfgpred(block, 0);
567                 next_block = get_nodes_block(new_jmp);
568                 exchange(block, to_block);
569         }
570         if (new_jmp)
571                 exchange(jmp, new_jmp);
572 }
573
574 /**
575  * Block walker:
576  *
577  * if we can find the following structure,
578  *
579  *        upper_block
580  *         /       |
581  *        /        |
582  *   lower_block   |
583  *     /  \        |
584  *   ... low_idx up_idx
585  *          \      |
586  *            block
587  *
588  * try to convert it into a (x pnc_lo c_lo) || (x pnc_hi c_hi)
589  * and optimize.
590  */
591 static void find_cf_and_or_walker(ir_node *block, void *ctx)
592 {
593         int low_idx, up_idx;
594         int n_cfgpreds = get_Block_n_cfgpreds(block);
595         bool_opt_env_t *env = ctx;
596
597 restart:
598         if (n_cfgpreds < 2)
599                 return;
600
601         for (low_idx = 0; low_idx < n_cfgpreds; ++low_idx) {
602                 ir_node      *lower_block;
603                 ir_node      *lower_cf;
604                 ir_node      *cond;
605                 ir_node      *cond_selector;
606                 ir_node      *lower_pred;
607
608                 lower_cf = get_Block_cfgpred(block, low_idx);
609                 lower_cf = skip_empty_blocks(lower_cf);
610                 if (!is_Proj(lower_cf))
611                         continue;
612
613                 cond = get_Proj_pred(lower_cf);
614                 if (!is_Cond(cond))
615                         continue;
616
617                 lower_block = get_nodes_block(cond);
618                 if (get_Block_n_cfgpreds(lower_block) != 1)
619                         continue;
620
621                 /* the block must not produce any side-effects */
622                 if (get_Block_mark(lower_block))
623                         continue;
624
625                 cond_selector = get_Cond_selector(cond);
626                 if (get_irn_mode(cond_selector) != mode_b)
627                         continue;
628
629                 lower_pred = get_Block_cfgpred_block(lower_block, 0);
630
631                 for (up_idx = 0; up_idx < n_cfgpreds; ++up_idx) {
632                         ir_node   *upper_block;
633                         ir_node   *upper_cf;
634                         ir_node   *upper_cond;
635                         ir_node   *upper_cond_selector;
636                         ir_node   *replacement;
637                         cond_pair  cpair;
638
639                         upper_cf    = get_Block_cfgpred(block, up_idx);
640                         upper_cf    = skip_empty_blocks(upper_cf);
641                         if (is_Bad(upper_cf))
642                                 continue;
643                         upper_block = get_nodes_block(upper_cf);
644                         if (upper_block != lower_pred)
645                                 continue;
646                         if (!block_dominates(upper_block, block))
647                                 continue;
648
649                         assert(is_Proj(upper_cf));
650                         upper_cond = get_Proj_pred(upper_cf);
651                         assert(is_Cond(upper_cond));
652                         upper_cond_selector = get_Cond_selector(upper_cond);
653                         if (get_irn_mode(upper_cond_selector) != mode_b)
654                                 continue;
655
656                         /* we have found the structure */
657                         /* check Phis: There must be NO Phi in block that
658                            depends on the existence of low block */
659                         if (!can_fuse_block_inputs(block, low_idx, up_idx))
660                                 continue;
661
662                         /* all fine, try it */
663                         if (!find_cond_pair(cond_selector, upper_cond_selector, &cpair))
664                                 continue;
665
666                         /* normalize pncs: we need the true case to jump into the
667                          * common block (ie. conjunctive normal form) */
668                         if (get_Proj_proj(lower_cf) == pn_Cond_false) {
669                                 if (cpair.proj_lo == cond_selector) {
670                                         ir_mode *mode  = get_tarval_mode(cpair.tv_lo);
671                                         ir_node *cmp   = get_Proj_pred(cpair.proj_lo);
672                                         ir_node *block = get_nodes_block(cmp);
673                                         cpair.pnc_lo   = get_negated_pnc(cpair.pnc_lo, mode);
674                                         cpair.proj_lo  = new_r_Proj(block, cmp, mode_b, cpair.pnc_lo);
675                                 } else {
676                                         ir_mode *mode  = get_tarval_mode(cpair.tv_hi);
677                                         ir_node *cmp   = get_Proj_pred(cpair.proj_hi);
678                                         ir_node *block = get_nodes_block(cmp);
679                                         assert(cpair.proj_hi == cond_selector);
680                                         cpair.pnc_hi   = get_negated_pnc(cpair.pnc_hi, mode);
681                                         cpair.proj_hi  = new_r_Proj(block, cmp, mode_b, cpair.pnc_hi);
682                                 }
683                         }
684                         if (get_Proj_proj(upper_cf) == pn_Cond_false) {
685                                 if (cpair.proj_lo == upper_cond_selector) {
686                                         ir_mode *mode  = get_tarval_mode(cpair.tv_lo);
687                                         ir_node *cmp   = get_Proj_pred(cpair.proj_lo);
688                                         ir_node *block = get_nodes_block(cmp);
689                                         cpair.pnc_lo   = get_negated_pnc(cpair.pnc_lo, mode);
690                                         cpair.proj_lo  = new_r_Proj(block, cmp, mode_b, cpair.pnc_lo);
691                                 } else {
692                                         ir_mode *mode  = get_tarval_mode(cpair.tv_hi);
693                                         ir_node *cmp   = get_Proj_pred(cpair.proj_hi);
694                                         ir_node *block = get_nodes_block(cmp);
695                                         assert(cpair.proj_hi == upper_cond_selector);
696                                         cpair.pnc_hi   = get_negated_pnc(cpair.pnc_hi, mode);
697                                         cpair.proj_hi  = new_r_Proj(block, cmp, mode_b, cpair.pnc_hi);
698                                 }
699                         }
700
701                         /* can we optimize the case? */
702                         replacement = bool_or(&cpair, upper_block);
703                         if (replacement == NULL)
704                                 continue;
705
706                         env->changed = 1;
707
708                         DB((dbg, LEVEL_1, "boolopt: %+F: fusing (ub %+F lb %+F)\n",
709                                 current_ir_graph, upper_block, lower_block));
710
711                         /* move all expressions on the path to lower/upper block */
712                         move_nodes_to_block(get_Block_cfgpred(block, up_idx), upper_block);
713                         move_nodes_to_block(get_Block_cfgpred(block, low_idx), lower_block);
714
715                         /* move all nodes from lower block to upper block */
716                         exchange(lower_block, upper_block);
717
718                         remove_block_input(block, up_idx);
719                         --n_cfgpreds;
720
721                         /* the optimizations expected the true case to jump */
722                         if (get_Proj_proj(lower_cf) == pn_Cond_false) {
723                                 ir_node *block = get_nodes_block(replacement);
724                                 replacement    = new_rd_Not(NULL, block, replacement, mode_b);
725                         }
726                         set_Cond_selector(cond, replacement);
727
728                         goto restart;
729                 }
730         }
731 }
732
733 void opt_bool(ir_graph *const irg)
734 {
735         bool_opt_env_t env;
736
737         /* register a debug mask */
738         FIRM_DBG_REGISTER(dbg, "firm.opt.bool");
739
740         /* works better with one return block only */
741         normalize_one_return(irg);
742
743         env.changed = 0;
744
745         /* optimize simple Andb and Orb cases */
746         irg_walk_graph(irg, NULL, bool_walk, &env);
747
748         ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
749
750         /* now more complicated cases: find control flow And/Or and optimize. */
751         irg_walk_graph(irg, clear_block_infos, collect_phis, NULL);
752         irg_block_walk_graph(irg, NULL, find_cf_and_or_walker, &env);
753
754         if (env.changed) {
755                 set_irg_outs_inconsistent(irg);
756                 set_irg_doms_inconsistent(irg);
757                 set_irg_extblk_inconsistent(irg);
758                 set_irg_loopinfo_inconsistent(irg);
759         }
760
761         ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
762 }
763
764 /* Creates an ir_graph pass for opt_bool. */
765 ir_graph_pass_t *opt_bool_pass(const char *name)
766 {
767         return def_graph_pass(name ? name : "opt_bool", opt_bool);
768 }  /* opt_bool_pass */