Revert "Let the block walker enter endless loops only at kept blocks, not Phis."
[libfirm] / ir / opt / opt_confirms.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   Optimizations regarding Confirm nodes.
23  * @author  Michael Beck
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #undef DEBUG_CONFIRM
29
30 #include "tv_t.h"
31 #include "irnode_t.h"
32 #include "iropt_t.h"
33 #include "iropt_dbg.h"
34 #include "iroptimize.h"
35 #include "irflag_t.h"
36 #include "irprintf.h"
37
38 enum range_tags {
39         MIN_INCLUDED = 0x00,  /**< [min, ... */
40         MAX_INCLUDED = 0x00,  /**< ..., max] */
41         MIN_EXCLUDED = 0x01,  /**< (min, ... */
42         MAX_EXCLUDED = 0x02   /**< ..., max) */
43 };
44
45 /**
46  * An interval. We could use
47  * intervals that ALWAYS include its borders, even for
48  * floating point, as the precision is limited.
49  * However, as our tarval module did not support
50  * such kind of operation, we use border flags allowing
51  * all intervals.
52  */
53 typedef struct interval_t {
54         ir_tarval     *min;   /**< lowest border */
55         ir_tarval     *max;   /**< highest border */
56         unsigned char flags;  /**< border flags */
57 } interval_t;
58
59 #ifdef DEBUG_CONFIRM
60
61 #define compare_iv(l_iv, r_iv, relation)    compare_iv_dbg(l_iv, r_iv, relation)
62
63 /* forward */
64 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation);
65
66 /* triangle */
67 #define DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, v) \
68   ir_printf("In %e:\na %= %n && b %= %n  ==>  a %= b == %s\n", \
69     get_irg_entity(current_ir_graph), \
70     l_relation, l_bound, r_relation, r_bound, relation, v);
71
72 /* right side */
73 #define DBG_OUT_R(r_relation, r_bound, left, relation, right, v) \
74   ir_printf("In %e:\na %= %n ==>  %n %= %n == %s\n", \
75     get_irg_entity(current_ir_graph), \
76     r_relation, r_bound, left, relation, right, v);
77
78 /* left side */
79 #define DBG_OUT_L(l_relation, l_bound, left, relation, right, v) \
80   ir_printf("In %e:\na %= %n ==>  %n %= %n == %s\n", \
81     get_irg_entity(current_ir_graph), \
82     l_relation, l_bound, left, relation, right, v);
83
84 #else
85
86 #define DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, v)
87 #define DBG_OUT_R(r_relation, r_bound, left, relation, right, v)
88 #define DBG_OUT_L(l_relation, l_bound, left, relation, right, v)
89
90 #endif /* DEBUG_CONFIRM */
91
92 /*
93  * Check, if the value of a node is != 0.
94  *
95  * This is a often needed case, so we handle here Confirm
96  * nodes too.
97  */
98 FIRM_API int value_not_zero(const ir_node *n, ir_node_cnst_ptr *confirm)
99 {
100 #define RET_ON(x)  if (x) { *confirm = n; return 1; }; break
101
102         ir_tarval *tv;
103         ir_mode *mode = get_irn_mode(n);
104         ir_relation relation;
105
106         *confirm = NULL;
107
108         /* there might be several Confirms one after other that form an interval */
109         for (;;) {
110                 if (is_Minus(n)) {
111                         /* we can safely skip Minus when checking for != 0 */
112                         n = get_unop_op(n);
113                         continue;
114                 }
115                 if (! is_Confirm(n))
116                         break;
117
118                 /*
119                  * Note: A Confirm is never after a Const. So,
120                  * we simply can check the bound for being a Const
121                  * without the fear that is might be hidden by a further Confirm.
122                  */
123                 tv = value_of(get_Confirm_bound(n));
124                 if (tv == tarval_bad)
125                         return 0;
126
127                 relation = tarval_cmp(tv, get_mode_null(mode));
128
129                 /*
130                  * Beware: C might by a NaN. It is not clear, what we should do
131                  * than. Of course a NaN is != 0, but we might use this function
132                  * to remove up Exceptions, and NaN's might generate Exception.
133                  * So, we do NOT handle NaNs here for safety.
134                  *
135                  * Note that only the C != 0 case need additional checking.
136                  */
137                 switch (get_Confirm_relation(n)) {
138                 case ir_relation_equal: /* n == C /\ C != 0 ==> n != 0 */
139                         RET_ON(relation != ir_relation_equal && relation != ir_relation_unordered);
140                 case ir_relation_less_greater: /* n != C /\ C == 0 ==> n != 0 */
141                         RET_ON(relation == ir_relation_equal);
142                 case ir_relation_less: /* n <  C /\ C <= 0 ==> n != 0 */
143                         RET_ON(relation == ir_relation_less || relation == ir_relation_equal);
144                 case ir_relation_less_equal: /* n <= C /\ C <  0 ==> n != 0 */
145                         RET_ON(relation == ir_relation_less);
146                 case ir_relation_greater_equal: /* n >= C /\ C >  0 ==> n != 0 */
147                         RET_ON(relation == ir_relation_greater);
148                 case ir_relation_greater: /* n >  C /\ C >= 0 ==> n != 0 */
149                         RET_ON(relation == ir_relation_greater || relation == ir_relation_equal);
150                 default:
151                         break;
152                 }
153                 n = get_Confirm_value(n);
154         }
155         tv = value_of(n);
156
157         if (tv == tarval_bad)
158                 return 0;
159
160         relation = tarval_cmp(tv, get_mode_null(mode));
161
162         /* again, need check for NaN */
163         return (relation != ir_relation_equal) && (relation != ir_relation_unordered);
164
165 #undef RET_ON
166 }  /* value_not_zero */
167
168 /*
169  * Check, if the value of a node cannot represent a NULL pointer.
170  *
171  * - Casts are skipped
172  * - If sel_based_null_check_elim is enabled, all
173  *   Sel nodes can be skipped.
174  * - A SymConst(entity) is NEVER a NULL pointer
175  * - Confirms are evaluated
176  */
177 FIRM_API int value_not_null(const ir_node *n, ir_node_cnst_ptr *confirm)
178 {
179         ir_tarval *tv;
180
181         *confirm = NULL;
182         n  = skip_Cast_const(n);
183
184         tv = value_of(n);
185         if (tarval_is_constant(tv) && ! tarval_is_null(tv))
186                 return 1;
187
188         assert(mode_is_reference(get_irn_mode(n)));
189         if (get_opt_sel_based_null_check_elim()) {
190                 /* skip all Sel nodes and Cast's */
191                 while (is_Sel(n)) {
192                         n = skip_Cast(get_Sel_ptr(n));
193                 }
194         }
195         while (1) {
196                 if (is_Cast(n)) { n = get_Cast_op(n); continue; }
197                 if (is_Proj(n)) { n = get_Proj_pred(n); continue; }
198                 break;
199         }
200
201         if (is_Global(n)) {
202                 /* global references are never NULL */
203                 return 1;
204         } else if (n == get_irg_frame(get_irn_irg(n))) {
205                 /* local references are never NULL */
206                 return 1;
207         } else if (is_Alloc(n)) {
208                 /* alloc never returns NULL (it throws an exception instead) */
209                 return 1;
210         } else {
211                 /* check for more Confirms */
212                 for (; is_Confirm(n); n = skip_Cast(get_Confirm_value(n))) {
213                         if (get_Confirm_relation(n) == ir_relation_less_greater) {
214                                 ir_node   *bound = get_Confirm_bound(n);
215                                 ir_tarval *tv    = value_of(bound);
216
217                                 if (tarval_is_null(tv)) {
218                                         *confirm = n;
219                                         return 1;
220                                 }
221                         }
222                 }
223         }
224         return 0;
225 }  /* value_not_null */
226
227 #ifdef __cplusplus
228 extern "C++" {
229         static inline ir_value_classify_sign operator *(ir_value_classify_sign sign, int mul) {
230         return (ir_value_classify_sign) (sign*mul);
231         }
232 }
233 #endif
234
235 /*
236  * Check, if the value of a node can be confirmed >= 0 or <= 0,
237  * If the mode of the value did not honor signed zeros, else
238  * check for >= 0 or < 0.
239  */
240 FIRM_API ir_value_classify_sign classify_value_sign(ir_node *n)
241 {
242         ir_tarval *tv, *c;
243         ir_mode *mode;
244         ir_relation cmp, ncmp;
245         int negate = 1;
246
247         for (;;) {
248                 unsigned code = get_irn_opcode(n);
249
250                 switch (code) {
251                 case iro_Minus:
252                         negate *= -1;
253                         n = get_Minus_op(n);
254                         continue;
255                 case iro_Confirm:
256                         break;
257                 default:
258                         return value_classified_unknown;
259                 }
260                 break;
261         }
262         if (!is_Confirm(n))
263                 return value_classified_unknown;
264
265         tv  = value_of(get_Confirm_bound(n));
266         if (tv == tarval_bad)
267                 return value_classified_unknown;
268
269         mode = get_irn_mode(n);
270
271         /*
272          * We can handle only >=, >, <, <= cases.
273          * We could handle == too, but this will be optimized into
274          * a constant either.
275          *
276          * Note that for integer modes we have a slightly better
277          * optimization possibilities, so we handle this
278          * different.
279          */
280         cmp = get_Confirm_relation(n);
281
282         switch (cmp) {
283         case ir_relation_less:
284                 /*
285                  * must be x < c <= 1 to be useful if integer mode and -0 = 0
286                  *         x < c <= 0 to be useful else
287                  */
288         case ir_relation_less_equal:
289                 /*
290                  * must be x <= c < 1 to be useful if integer mode and -0 = 0
291                  *         x <= c < 0 to be useful else
292                  */
293                 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
294                         get_mode_one(mode) : get_mode_null(mode);
295
296                 ncmp = tarval_cmp(tv, c);
297                 if (ncmp == ir_relation_equal)
298                         ncmp = ir_relation_less_equal;
299
300                 if (cmp != (ncmp ^ ir_relation_equal))
301                         return value_classified_unknown;
302
303                 /* yep, negative */
304                 return value_classified_negative * negate;
305
306         case ir_relation_greater_equal:
307                 /*
308                  * must be x >= c > -1 to be useful if integer mode
309                  *         x >= c >= 0 to be useful else
310                  */
311         case ir_relation_greater:
312                 /*
313                  * must be x > c >= -1 to be useful if integer mode
314                  *         x > c >= 0 to be useful else
315                  */
316                 if (mode_is_int(mode)) {
317                         c = get_mode_minus_one(mode);
318
319                         ncmp = tarval_cmp(tv, c);
320                         if (ncmp == ir_relation_equal)
321                                 ncmp = ir_relation_greater_equal;
322
323                         if (cmp != (ncmp ^ ir_relation_equal))
324                                 return value_classified_unknown;
325                 } else {
326                         c = get_mode_minus_one(mode);
327
328                         ncmp = tarval_cmp(tv, c);
329
330                         if (ncmp != ir_relation_equal && ncmp != ir_relation_greater)
331                                 return value_classified_unknown;
332                 }
333
334                 /* yep, positive */
335                 return value_classified_positive * negate;
336
337         default:
338                 return value_classified_unknown;
339         }
340 }  /* classify_value_sign */
341
342 /**
343  * construct an interval from a value
344  *
345  * @return the filled interval or NULL if no interval
346  *         can be created (happens only on floating point
347  */
348 static interval_t *get_interval_from_tv(interval_t *iv, ir_tarval *tv)
349 {
350         ir_mode *mode = get_tarval_mode(tv);
351
352         if (tv == tarval_bad) {
353                 if (mode_is_float(mode)) {
354                         /* NaN could be included which we cannot handle */
355                         iv->min   = tarval_bad;
356                         iv->max   = tarval_bad;
357                         iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
358                         return NULL;
359                 } else {
360                         /* [-oo, +oo] */
361                         iv->min   = get_mode_min(mode);
362                         iv->max   = get_mode_max(mode);
363                         iv->flags = MIN_INCLUDED | MAX_INCLUDED;
364                         return iv;
365                 }
366         }
367
368         if (mode_is_float(mode)) {
369                 if (tv == get_mode_NAN(mode)) {
370                         /* arg, we cannot handle NaN's. */
371                         iv->min   = tarval_bad;
372                         iv->max   = tarval_bad;
373                         iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
374                         return NULL;
375                 }
376         }
377
378         /* [tv, tv] */
379         iv->min   = tv;
380         iv->max   = tv;
381         iv->flags = MIN_INCLUDED | MAX_INCLUDED;
382
383         return iv;
384 }  /* get_interval_from_tv */
385
386 /**
387  * construct an interval from a Confirm
388  *
389  * @param iv       an empty interval, will be filled
390  * @param bound    the bound value
391  * @param relation the Confirm compare relation
392  *
393  * @return the filled interval or NULL if no interval
394  *         can be created (happens only on floating point
395  */
396 static interval_t *get_interval(interval_t *iv, ir_node *bound, ir_relation relation)
397 {
398         ir_mode   *mode = get_irn_mode(bound);
399         ir_tarval *tv   = value_of(bound);
400
401         if (tv == tarval_bad) {
402                 /* There is nothing we could do here. For integer
403                  * modes we could return [-oo, +oo], but there is
404                  * nothing we could deduct from such an interval.
405                  * So, speed things up and return unknown.
406                  */
407                 iv->min   = tarval_bad;
408                 iv->max   = tarval_bad;
409                 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
410                 return NULL;
411         }
412
413         if (mode_is_float(mode)) {
414                 if (tv == get_mode_NAN(mode)) {
415                         /* arg, we cannot handle NaN's. */
416                         iv->min   = tarval_bad;
417                         iv->max   = tarval_bad;
418                         iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
419
420                         return NULL;
421                 }
422         }
423
424         /* check which side is known */
425         switch (relation) {
426         case ir_relation_equal:
427                 /* [tv, tv] */
428                 iv->min   =
429                         iv->max   = tv;
430                 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
431                 break;
432
433         case ir_relation_less_equal:
434                 /* [-oo, tv] */
435                 iv->min   = get_mode_min(mode);
436                 iv->max   = tv;
437                 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
438                 break;
439
440         case ir_relation_less:
441                 /* [-oo, tv) */
442                 iv->min   = get_mode_min(mode);
443                 iv->max   = tv;
444                 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
445                 break;
446
447         case ir_relation_greater:
448                 /* (tv, +oo] */
449                 iv->min   = tv;
450                 iv->max   = get_mode_max(mode);
451                 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
452                 break;
453
454         case ir_relation_greater_equal:
455                 /* [tv, +oo] */
456                 iv->min   = tv;
457                 iv->max   = get_mode_max(mode);
458                 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
459                 break;
460
461         case ir_relation_less_equal_greater:
462                 /*
463                  * Ordered means, that at least neither
464                  * our bound nor our value ara NaN's
465                  */
466                 /* [-oo, +oo] */
467                 iv->min   = get_mode_min(mode);
468                 iv->max   = get_mode_max(mode);
469                 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
470                 break;
471
472         default:
473                 /*
474                  * We do not handle UNORDERED, as a NaN
475                  * could be included in the interval.
476                  */
477                 iv->min   = tarval_bad;
478                 iv->max   = tarval_bad;
479                 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
480                 return NULL;
481         }
482
483         if (iv->min != tarval_bad && iv->max != tarval_bad)
484                 return iv;
485         return NULL;
486 }  /* get_interval */
487
488 /**
489  * Try to evaluate l_iv relation r_iv.
490  *
491  * @param l_iv      the left interval
492  * @param r_iv      the right interval
493  * @param relation  the compare relation
494  *
495  * @return
496  *   tarval_b_true or tarval_b_false it it can be evaluated,
497  *   tarval_bad else
498  */
499 static ir_tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
500 {
501         ir_relation res;
502         unsigned    flags;
503         ir_tarval  *tv_true = tarval_b_true, *tv_false = tarval_b_false;
504
505         /* if one interval contains NaNs, we cannot evaluate anything */
506         if (! l_iv || ! r_iv)
507                 return tarval_bad;
508
509         /* we can only check ordered relations */
510         if (relation & ir_relation_unordered) {
511                 ir_tarval *t;
512
513                 relation = get_negated_relation(relation);
514                 t        = tv_true;
515                 tv_true  = tv_false;
516                 tv_false = t;
517         }
518
519         /* if we have > or >=, we do the inverse to save some cases */
520         if (relation == ir_relation_greater_equal || relation == ir_relation_greater) {
521                 const interval_t *t;
522
523                 relation = get_inversed_relation(relation);
524                 t        = l_iv;
525                 l_iv     = r_iv;
526                 r_iv     = t;
527         }
528
529         /* now, only the following cases remains */
530         switch (relation) {
531         case ir_relation_equal:
532                 /* two intervals can be compared for equality only if they are a single value */
533                 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
534                         return tarval_cmp(l_iv->min, r_iv->min) == ir_relation_equal ? tv_true : tv_false;
535
536                 /* if both intervals do not intersect, it is never equal */
537                 res = tarval_cmp(l_iv->max, r_iv->min);
538
539                 /* b < c ==> [a,b] != [c,d] */
540                 if (res == ir_relation_less)
541                         return tv_false;
542
543                 /* b <= c ==> [a,b) != [c,d]  AND [a,b] != (c,d] */
544                 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
545                         && (res == ir_relation_equal))
546                         return tv_false;
547
548                 res = tarval_cmp(r_iv->max, l_iv->min);
549
550                 /* d < a ==> [c,d] != [a,b] */
551                 if (res == ir_relation_less)
552                         return tv_false;
553
554                 /* d <= a ==> [c,d) != [a,b]  AND [c,d] != (a,b] */
555                 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
556                         && (res == ir_relation_equal))
557                         return tv_false;
558                 break;
559
560         case ir_relation_less_greater:
561                 /* two intervals can be compared for not equality only if they are a single value */
562                 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
563                         return tarval_cmp(l_iv->min, r_iv->min) != ir_relation_equal ? tv_true : tv_false;
564                 break;
565
566         case ir_relation_less:
567                 res = tarval_cmp(l_iv->max, r_iv->min);
568
569                 /* [a, b] < [c, d]  <==> b < c */
570                 if (res == ir_relation_less)
571                         return tv_true;
572
573                 /* if one border is excluded, b <= c is enough */
574                 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
575                         res == ir_relation_equal)
576                         return tv_true;
577
578                 /* [a, b] >= [c, d] <==> a > d */
579                 res = tarval_cmp(l_iv->min, r_iv->max);
580                 if (res == ir_relation_greater)
581                         return tv_false;
582
583                 /* if one border is excluded, a >= d is enough */
584                 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
585                         res == ir_relation_equal)
586                         return tv_false;
587                 break;
588
589         case ir_relation_less_equal:
590                 /* [a, b) <= [c, d] or [a, b] <= (c, d]  <==> b <= c */
591                 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
592                 if (flags) {
593                         res = tarval_cmp(l_iv->max, r_iv->min);
594
595                         if (res == ir_relation_less || res == ir_relation_equal)
596                                 return tv_true;
597                 }
598
599                 res = tarval_cmp(l_iv->min, r_iv->max);
600
601                 /* [a, b] > [c, d] <==> a > d */
602                 if (res == ir_relation_greater)
603                         return tv_false;
604
605                 /* if one border is excluded, a >= d is enough */
606                 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
607                         res == ir_relation_equal)
608                         return tv_false;
609                 break;
610
611         case ir_relation_less_equal_greater:
612                 /* Hmm. if both are intervals, we can find an order */
613                 return tv_true;
614
615         default:
616                 return tarval_bad;
617         }
618         return tarval_bad;
619 }  /* compare_iv */
620
621 /**
622  * Returns non-zero, if a given relation is transitive.
623  */
624 static int is_transitive(ir_relation relation)
625 {
626         return (ir_relation_false < relation && relation < ir_relation_less_greater);
627 }  /* is_transitive */
628
629 /**
630  * Return the value of a Cmp if one or both predecessors
631  * are Confirm nodes.
632  *
633  * @param cmp      the Cmp node
634  * @param left     the left operand of the Cmp
635  * @param right    the right operand of the Cmp
636  * @param relation the compare relation
637  */
638 FIRM_API ir_tarval *computed_value_Cmp_Confirm(const ir_node *cmp, ir_node *left, ir_node *right, ir_relation relation)
639 {
640         ir_node    *l_bound;
641         ir_relation l_relation, res_relation, neg_relation;
642         interval_t  l_iv, r_iv;
643         ir_tarval  *tv;
644         ir_mode    *mode;
645
646         if (is_Confirm(right)) {
647                 /* we want the Confirm on the left side */
648                 ir_node *t = right;
649                 right = left;
650                 left  = t;
651
652                 relation = get_inversed_relation(relation);
653         } else if (! is_Confirm(left)) {
654                 /* nothing more found */
655                 tv = tarval_bad;
656                 goto check_null_case;
657         }
658
659         /* ok, here at least left is a Confirm, right might be */
660         l_bound    = get_Confirm_bound(left);
661         l_relation = get_Confirm_relation(left);
662
663         if (is_Confirm(right)) {
664                 /*
665                  * both sides are Confirm's. Check some rare cases first.
666                  */
667                 ir_node    *r_bound    = get_Confirm_bound(right);
668                 ir_relation r_relation = get_Confirm_relation(right);
669
670                 /*
671                  * some check can be made WITHOUT constant bounds
672                  */
673                 if (r_bound == l_bound) {
674                         if (is_transitive(l_relation)) {
675                                 ir_relation r_inc_relation = get_inversed_relation(r_relation);
676
677                                 /*
678                                  * triangle inequality:
679                                  *
680                                  * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
681                                  *
682                                  * We handle correctly cases with some <=/>= here
683                                  */
684                                 if ((l_relation & ~ir_relation_equal) == (r_inc_relation & ~ir_relation_equal)) {
685                                         res_relation = (l_relation & ~ir_relation_equal) | (l_relation & r_inc_relation & ir_relation_equal);
686
687                                         if ((relation == res_relation) || ((relation & ~ir_relation_equal) == res_relation)) {
688                                                 DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, "true");
689                                                 DBG_EVAL_CONFIRM(cmp);
690                                                 return tarval_b_true;
691                                         } else {
692                                                 ir_relation neg_relation = get_negated_relation(relation);
693
694                                                 if ((neg_relation == res_relation) || ((neg_relation & ~ir_relation_equal) == res_relation)) {
695                                                         DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, "false");
696                                                         DBG_EVAL_CONFIRM(cmp);
697                                                         return tarval_b_false;
698                                                 }
699                                         }
700                                 }
701                         }
702                 }
703
704                 /*
705                  * Here, we check only the right Confirm, as the left Confirms are
706                  * checked later anyway.
707                  */
708                 if (left == r_bound) {
709                         /*
710                          * l == bound(r) AND relation(r) == relation:
711                          *
712                          * We know that a CMP b and check for that
713                          */
714                         if ((r_relation == relation) || (r_relation == (relation & ~ir_relation_equal))) {
715                                 DBG_OUT_R(r_relation, r_bound, left, relation, right, "true");
716                                 DBG_EVAL_CONFIRM(cmp);
717                                 return tarval_b_true;
718                         }
719                         /*
720                          * l == bound(r) AND relation(r) != relation:
721                          *
722                          * We know that a CMP b and check for a ~CMP b
723                          */
724                         else {
725                                 mode    = get_irn_mode(left);
726                                 neg_relation = get_negated_relation(relation);
727
728                                 if ((r_relation == neg_relation) || (r_relation == (neg_relation & ~ir_relation_equal))) {
729                                         DBG_OUT_R(r_relation, r_bound, left, relation, right, "false");
730                                         DBG_EVAL_CONFIRM(cmp);
731                                         return tarval_b_false;
732                                 }
733                         }
734                 }
735
736                 /* now, try interval magic */
737                 tv = compare_iv(
738                         get_interval(&l_iv, l_bound, l_relation),
739                         get_interval(&r_iv, r_bound, r_relation),
740                         relation);
741
742                 if (tv != tarval_bad) {
743                         DBG_EVAL_CONFIRM(cmp);
744                         return tv;
745                 }
746         }
747
748         /* from Here, check only left Confirm */
749
750         /*
751          * some checks can be made WITHOUT constant bounds
752          */
753         if (right == l_bound) {
754                 /*
755                  * r == bound(l) AND relation(l) == relation:
756                  *
757                  * We know that a CMP b and check for that
758                  */
759                 if ((l_relation == relation) || (l_relation == (relation & ~ir_relation_equal))) {
760                         DBG_OUT_L(l_relation, l_bound, left, relation, right, "true");
761                         DBG_EVAL_CONFIRM(cmp);
762                         return tarval_b_true;
763                 }
764                 /*
765                  * r == bound(l) AND relation(l) is Not(relation):
766                  *
767                  * We know that a CMP b and check for a ~CMP b
768                  */
769                 else {
770                         mode = get_irn_mode(left);
771                         neg_relation = get_negated_relation(relation);
772
773                         if ((l_relation == neg_relation) || (l_relation == (neg_relation & ~ir_relation_equal))) {
774                                 DBG_OUT_L(l_relation, l_bound, left, relation, right, "false");
775                                 DBG_EVAL_CONFIRM(cmp);
776                                 return tarval_b_false;
777                         }
778                 }
779         }
780
781         /* now, only right == Const can help */
782         tv = value_of(right);
783
784         if (tv != tarval_bad) {
785                 tv = compare_iv(
786                         get_interval(&l_iv, l_bound, l_relation),
787                         get_interval_from_tv(&r_iv, tv),
788                         relation);
789         } else {
790 check_null_case:
791                 /* check some other cases */
792                 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) &&
793                         is_Const(right) && is_Const_null(right)) {
794                         /* for == 0 or != 0 we have some special tools */
795                         ir_mode       *mode = get_irn_mode(left);
796                         const ir_node *dummy;
797                         if (mode_is_reference(mode)) {
798                                 if (value_not_null(left, &dummy)) {
799                                         tv = relation == ir_relation_equal ? tarval_b_false : tarval_b_true;
800                                 }
801                         } else {
802                                 if (value_not_zero(left, &dummy)) {
803                                         tv = relation == ir_relation_equal ? tarval_b_false : tarval_b_true;
804                                 }
805                         }
806                 }
807         }
808
809         if (tv != tarval_bad)
810                 DBG_EVAL_CONFIRM(cmp);
811
812         return tv;
813 }  /* computed_value_Cmp_Confirm */
814
815 #ifdef DEBUG_CONFIRM
816 /**
817  * For debugging. Prints an interval into a string.
818  *
819  * @param buf   address of a string buffer
820  * @param len   length of the string buffer
821  * @param iv    the interval
822  */
823 static int iv_snprintf(char *buf, size_t len, const interval_t *iv)
824 {
825         char smin[64], smax[64];
826
827         if (iv) {
828                 tarval_snprintf(smin, sizeof(smin), iv->min);
829
830                 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
831                         tarval_snprintf(smax, sizeof(smax), iv->max);
832
833                         return snprintf(buf, len, "%c%s, %s%c",
834                                 iv->flags & MIN_EXCLUDED ? '(' : '[',
835                                 smin, smax,
836                                 iv->flags & MAX_EXCLUDED ? ')' : ']'
837                                 );
838                 } else
839                         return snprintf(buf, len, "%s", smin);
840         }
841         return snprintf(buf, len, "<UNKNOWN>");
842 }  /* iv_snprintf */
843
844 /**
845  * For debugging. Prints an interval compare.
846  *
847  * @param l_iv      the left interval
848  * @param r_iv      the right interval
849  * @param relation  the compare relation
850  */
851 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
852 {
853         char sl[128], sr[128];
854
855         iv_snprintf(sl, sizeof(sl), l_iv);
856         iv_snprintf(sr, sizeof(sr), r_iv);
857
858         ir_printf("%s %= %s", sl, relation, sr);
859 }  /* print_iv_cmp */
860
861 /**
862  * For debugging. call *compare_iv() and prints inputs and result.
863  *
864  * @param l_iv     the left interval
865  * @param r_iv     the right interval
866  * @param relation the compare relation
867  */
868 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
869 {
870         tarval *tv = (compare_iv)(l_iv, r_iv, relation);
871
872         if (tv == tarval_bad)
873         return tv;
874
875         ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
876         print_iv_cmp(l_iv, r_iv, relation);
877         ir_printf(" = %T\n", tv);
878         return tv;
879 }  /* compare_iv_dbg */
880
881 #endif /* DEBUG_CONFIRM */