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