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