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