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