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