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