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