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