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