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