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