improve value_not_zero
[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  */
25 #include "config.h"
26
27 #undef DEBUG_CONFIRM
28
29 #include "tv_t.h"
30 #include "irnode_t.h"
31 #include "iropt_t.h"
32 #include "iropt_dbg.h"
33 #include "iroptimize.h"
34 #include "irflag_t.h"
35 #include "irprintf.h"
36
37 enum range_tags {
38         MIN_INCLUDED = 0x00,  /**< [min, ... */
39         MAX_INCLUDED = 0x00,  /**< ..., max] */
40         MIN_EXCLUDED = 0x01,  /**< (min, ... */
41         MAX_EXCLUDED = 0x02   /**< ..., max) */
42 };
43
44 /**
45  * An interval. We could use
46  * intervals that ALWAYS include its borders, even for
47  * floating point, as the precision is limited.
48  * However, as our tarval module did not support
49  * such kind of operation, we use border flags allowing
50  * all intervals.
51  */
52 typedef struct interval_t {
53         ir_tarval     *min;   /**< lowest border */
54         ir_tarval     *max;   /**< highest border */
55         unsigned char flags;  /**< border flags */
56 } interval_t;
57
58 #ifdef DEBUG_CONFIRM
59
60 #define compare_iv(l_iv, r_iv, relation)    compare_iv_dbg(l_iv, r_iv, relation)
61
62 /* forward */
63 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation);
64
65 /* triangle */
66 #define DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, v) \
67   ir_printf("In %e:\na %= %n && b %= %n  ==>  a %= b == %s\n", \
68     get_irg_entity(current_ir_graph), \
69     l_relation, l_bound, r_relation, r_bound, relation, v)
70
71 /* right side */
72 #define DBG_OUT_R(r_relation, r_bound, left, relation, right, v) \
73   ir_printf("In %e:\na %= %n ==>  %n %= %n == %s\n", \
74     get_irg_entity(current_ir_graph), \
75     r_relation, r_bound, left, relation, right, v)
76
77 /* left side */
78 #define DBG_OUT_L(l_relation, l_bound, left, relation, right, v) \
79   ir_printf("In %e:\na %= %n ==>  %n %= %n == %s\n", \
80     get_irg_entity(current_ir_graph), \
81     l_relation, l_bound, left, relation, right, v)
82
83 #else
84
85 #define DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, v)  (void)0
86 #define DBG_OUT_R(r_relation, r_bound, left, relation, right, v)  (void)0
87 #define DBG_OUT_L(l_relation, l_bound, left, relation, right, v)  (void)0
88
89 #endif /* DEBUG_CONFIRM */
90
91 /*
92  * Check, if the value of a node is != 0.
93  *
94  * This is a often needed case, so we handle here Confirm
95  * nodes too.
96  */
97 int value_not_zero(const ir_node *n, const ir_node **confirm)
98 {
99 #define RET_ON(x)  if (x) { *confirm = n; return 1; } break
100
101         ir_tarval *tv;
102         ir_mode *mode = get_irn_mode(n);
103         ir_relation relation;
104
105         *confirm = NULL;
106
107         /* there might be several Confirms one after other that form an interval */
108         for (;;) {
109                 if (is_Minus(n)) {
110                         /* we can safely skip Minus when checking for != 0 */
111                         n = get_unop_op(n);
112                         continue;
113                 }
114                 if (! is_Confirm(n))
115                         break;
116
117                 /*
118                  * Note: A Confirm is never after a Const. So,
119                  * we simply can check the bound for being a Const
120                  * without the fear that is might be hidden by a further Confirm.
121                  */
122                 tv = value_of(get_Confirm_bound(n));
123                 if (tv == tarval_bad) {
124                         n = get_Confirm_value(n);
125                         continue;
126                 }
127
128                 relation = tarval_cmp(tv, get_mode_null(mode));
129
130                 /*
131                  * Beware: C might by a NaN. It is not clear, what we should do
132                  * than. Of course a NaN is != 0, but we might use this function
133                  * to remove up Exceptions, and NaN's might generate Exception.
134                  * So, we do NOT handle NaNs here for safety.
135                  *
136                  * Note that only the C != 0 case need additional checking.
137                  */
138                 switch (get_Confirm_relation(n)) {
139                 case ir_relation_equal: /* n == C /\ C != 0 ==> n != 0 */
140                         RET_ON(relation != ir_relation_equal && relation != ir_relation_unordered);
141                 case ir_relation_less_greater: /* n != C /\ C == 0 ==> n != 0 */
142                         RET_ON(relation == ir_relation_equal);
143                 case ir_relation_less: /* n <  C /\ C <= 0 ==> n != 0 */
144                         RET_ON(relation == ir_relation_less || relation == ir_relation_equal);
145                 case ir_relation_less_equal: /* n <= C /\ C <  0 ==> n != 0 */
146                         RET_ON(relation == ir_relation_less);
147                 case ir_relation_greater_equal: /* n >= C /\ C >  0 ==> n != 0 */
148                         RET_ON(relation == ir_relation_greater);
149                 case ir_relation_greater: /* n >  C /\ C >= 0 ==> n != 0 */
150                         RET_ON(relation == ir_relation_greater || relation == ir_relation_equal);
151                 default:
152                         break;
153                 }
154                 n = get_Confirm_value(n);
155         }
156         /* global entities are never NULL */
157         if (is_SymConst_addr_ent(n))
158                 return true;
159
160         tv = value_of(n);
161         if (tv == tarval_bad)
162                 return false;
163
164         relation = tarval_cmp(tv, get_mode_null(mode));
165
166         /* again, need check for NaN */
167         return (relation != ir_relation_equal) && (relation != ir_relation_unordered);
168
169 #undef RET_ON
170 }  /* value_not_zero */
171
172 /*
173  * Check, if the value of a node cannot represent a NULL pointer.
174  *
175  * - Casts are skipped, Sels are skipped
176  * - A SymConst(entity) is NEVER a NULL pointer
177  * - Confirms are evaluated
178  */
179 int value_not_null(const ir_node *n, const ir_node **confirm)
180 {
181         ir_tarval *tv;
182
183         *confirm = NULL;
184         n  = skip_Cast_const(n);
185
186         tv = value_of(n);
187         if (tarval_is_constant(tv) && ! tarval_is_null(tv))
188                 return 1;
189
190         assert(mode_is_reference(get_irn_mode(n)));
191         /* skip all Sel nodes and Cast's */
192         while (is_Sel(n)) {
193                 n = skip_Cast(get_Sel_ptr(n));
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_SymConst_addr_ent(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 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 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
645         if (is_Confirm(right)) {
646                 /* we want the Confirm on the left side */
647                 ir_node *t = right;
648                 right = left;
649                 left  = t;
650
651                 relation = get_inversed_relation(relation);
652         } else if (! is_Confirm(left)) {
653                 /* nothing more found */
654                 tv = tarval_bad;
655                 goto check_null_case;
656         }
657
658         /* ok, here at least left is a Confirm, right might be */
659         l_bound    = get_Confirm_bound(left);
660         l_relation = get_Confirm_relation(left);
661
662         if (is_Confirm(right)) {
663                 /*
664                  * both sides are Confirm's. Check some rare cases first.
665                  */
666                 ir_node    *r_bound    = get_Confirm_bound(right);
667                 ir_relation r_relation = get_Confirm_relation(right);
668
669                 /*
670                  * some check can be made WITHOUT constant bounds
671                  */
672                 if (r_bound == l_bound) {
673                         if (is_transitive(l_relation)) {
674                                 ir_relation r_inc_relation = get_inversed_relation(r_relation);
675
676                                 /*
677                                  * triangle inequality:
678                                  *
679                                  * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
680                                  *
681                                  * We handle correctly cases with some <=/>= here
682                                  */
683                                 if ((l_relation & ~ir_relation_equal) == (r_inc_relation & ~ir_relation_equal)) {
684                                         res_relation = (l_relation & ~ir_relation_equal) | (l_relation & r_inc_relation & ir_relation_equal);
685
686                                         if ((relation == res_relation) || ((relation & ~ir_relation_equal) == res_relation)) {
687                                                 DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, "true");
688                                                 DBG_EVAL_CONFIRM(cmp);
689                                                 return tarval_b_true;
690                                         } else {
691                                                 ir_relation neg_relation = get_negated_relation(relation);
692
693                                                 if ((neg_relation == res_relation) || ((neg_relation & ~ir_relation_equal) == res_relation)) {
694                                                         DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, "false");
695                                                         DBG_EVAL_CONFIRM(cmp);
696                                                         return tarval_b_false;
697                                                 }
698                                         }
699                                 }
700                         }
701                 }
702
703                 /*
704                  * Here, we check only the right Confirm, as the left Confirms are
705                  * checked later anyway.
706                  */
707                 if (left == r_bound) {
708                         /*
709                          * l == bound(r) AND relation(r) == relation:
710                          *
711                          * We know that a CMP b and check for that
712                          */
713                         if ((r_relation == relation) || (r_relation == (relation & ~ir_relation_equal))) {
714                                 DBG_OUT_R(r_relation, r_bound, left, relation, right, "true");
715                                 DBG_EVAL_CONFIRM(cmp);
716                                 return tarval_b_true;
717                         }
718                         /*
719                          * l == bound(r) AND relation(r) != relation:
720                          *
721                          * We know that a CMP b and check for a ~CMP b
722                          */
723                         else {
724                                 neg_relation = get_negated_relation(relation);
725
726                                 if ((r_relation == neg_relation) || (r_relation == (neg_relation & ~ir_relation_equal))) {
727                                         DBG_OUT_R(r_relation, r_bound, left, relation, right, "false");
728                                         DBG_EVAL_CONFIRM(cmp);
729                                         return tarval_b_false;
730                                 }
731                         }
732                 }
733
734                 /* now, try interval magic */
735                 tv = compare_iv(
736                         get_interval(&l_iv, l_bound, l_relation),
737                         get_interval(&r_iv, r_bound, r_relation),
738                         relation);
739
740                 if (tv != tarval_bad) {
741                         DBG_EVAL_CONFIRM(cmp);
742                         return tv;
743                 }
744         }
745
746         /* from Here, check only left Confirm */
747
748         /*
749          * some checks can be made WITHOUT constant bounds
750          */
751         if (right == l_bound) {
752                 /*
753                  * r == bound(l) AND relation(l) == relation:
754                  *
755                  * We know that a CMP b and check for that
756                  */
757                 if ((l_relation == relation) || (l_relation == (relation & ~ir_relation_equal))) {
758                         DBG_OUT_L(l_relation, l_bound, left, relation, right, "true");
759                         DBG_EVAL_CONFIRM(cmp);
760                         return tarval_b_true;
761                 }
762                 /*
763                  * r == bound(l) AND relation(l) is Not(relation):
764                  *
765                  * We know that a CMP b and check for a ~CMP b
766                  */
767                 else {
768                         neg_relation = get_negated_relation(relation);
769
770                         if ((l_relation == neg_relation) || (l_relation == (neg_relation & ~ir_relation_equal))) {
771                                 DBG_OUT_L(l_relation, l_bound, left, relation, right, "false");
772                                 DBG_EVAL_CONFIRM(cmp);
773                                 return tarval_b_false;
774                         }
775                 }
776         }
777
778         /* now, only right == Const can help */
779         tv = value_of(right);
780
781         if (tv != tarval_bad) {
782                 tv = compare_iv(
783                         get_interval(&l_iv, l_bound, l_relation),
784                         get_interval_from_tv(&r_iv, tv),
785                         relation);
786         } else {
787 check_null_case:
788                 /* check some other cases */
789                 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) &&
790                         is_Const(right) && is_Const_null(right)) {
791                         /* for == 0 or != 0 we have some special tools */
792                         ir_mode       *mode = get_irn_mode(left);
793                         const ir_node *dummy;
794                         if (mode_is_reference(mode)) {
795                                 if (value_not_null(left, &dummy)) {
796                                         tv = relation == ir_relation_equal ? tarval_b_false : tarval_b_true;
797                                 }
798                         } else {
799                                 if (value_not_zero(left, &dummy)) {
800                                         tv = relation == ir_relation_equal ? tarval_b_false : tarval_b_true;
801                                 }
802                         }
803                 }
804         }
805
806         if (tv != tarval_bad)
807                 DBG_EVAL_CONFIRM(cmp);
808
809         return tv;
810 }  /* computed_value_Cmp_Confirm */
811
812 #ifdef DEBUG_CONFIRM
813 /**
814  * For debugging. Prints an interval into a string.
815  *
816  * @param buf   address of a string buffer
817  * @param len   length of the string buffer
818  * @param iv    the interval
819  */
820 static int iv_snprintf(char *buf, size_t len, const interval_t *iv)
821 {
822         char smin[64], smax[64];
823
824         if (iv) {
825                 tarval_snprintf(smin, sizeof(smin), iv->min);
826
827                 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
828                         tarval_snprintf(smax, sizeof(smax), iv->max);
829
830                         return snprintf(buf, len, "%c%s, %s%c",
831                                 iv->flags & MIN_EXCLUDED ? '(' : '[',
832                                 smin, smax,
833                                 iv->flags & MAX_EXCLUDED ? ')' : ']'
834                                 );
835                 } else
836                         return snprintf(buf, len, "%s", smin);
837         }
838         return snprintf(buf, len, "<UNKNOWN>");
839 }  /* iv_snprintf */
840
841 /**
842  * For debugging. Prints an interval compare.
843  *
844  * @param l_iv      the left interval
845  * @param r_iv      the right interval
846  * @param relation  the compare relation
847  */
848 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
849 {
850         char sl[128], sr[128];
851
852         iv_snprintf(sl, sizeof(sl), l_iv);
853         iv_snprintf(sr, sizeof(sr), r_iv);
854
855         ir_printf("%s %= %s", sl, relation, sr);
856 }  /* print_iv_cmp */
857
858 /**
859  * For debugging. call *compare_iv() and prints inputs and result.
860  *
861  * @param l_iv     the left interval
862  * @param r_iv     the right interval
863  * @param relation the compare relation
864  */
865 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
866 {
867         tarval *tv = (compare_iv)(l_iv, r_iv, relation);
868
869         if (tv == tarval_bad)
870         return tv;
871
872         ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
873         print_iv_cmp(l_iv, r_iv, relation);
874         ir_printf(" = %T\n", tv);
875         return tv;
876 }  /* compare_iv_dbg */
877
878 #endif /* DEBUG_CONFIRM */