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