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