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