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