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