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