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