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