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