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