* gcc_release: New script.
[official-gcc.git] / gcc / fold-const.c
blob1f1d63f5db20f96d0a99a551ebd87ca4bec8488e
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /*@@ This file should be rewritten to use an arbitrary precision
23 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25 @@ The routines that translate from the ap rep should
26 @@ warn if precision et. al. is lost.
27 @@ This would also make life easier when this technology is used
28 @@ for cross-compilers. */
30 /* The entry points in this file are fold, size_int_wide, size_binop
31 and force_fit_type.
33 fold takes a tree as argument and returns a simplified tree.
35 size_binop takes a tree code for an arithmetic operation
36 and two operands that are trees, and produces a tree for the
37 result, assuming the type comes from `sizetype'.
39 size_int takes an integer value, and creates a tree constant
40 with type from `sizetype'.
42 force_fit_type takes a constant and prior overflow indicator, and
43 forces the value to fit the type. It returns an overflow indicator. */
45 #include "config.h"
46 #include "system.h"
47 #include <setjmp.h>
48 #include "flags.h"
49 #include "tree.h"
50 #include "rtl.h"
51 #include "expr.h"
52 #include "tm_p.h"
53 #include "toplev.h"
54 #include "ggc.h"
56 static void encode PARAMS ((HOST_WIDE_INT *,
57 unsigned HOST_WIDE_INT,
58 HOST_WIDE_INT));
59 static void decode PARAMS ((HOST_WIDE_INT *,
60 unsigned HOST_WIDE_INT *,
61 HOST_WIDE_INT *));
62 static tree negate_expr PARAMS ((tree));
63 static tree split_tree PARAMS ((tree, enum tree_code, tree *, tree *,
64 int));
65 static tree associate_trees PARAMS ((tree, tree, enum tree_code, tree));
66 static tree int_const_binop PARAMS ((enum tree_code, tree, tree, int, int));
67 static void const_binop_1 PARAMS ((PTR));
68 static tree const_binop PARAMS ((enum tree_code, tree, tree, int));
69 static void fold_convert_1 PARAMS ((PTR));
70 static tree fold_convert PARAMS ((tree, tree));
71 static enum tree_code invert_tree_comparison PARAMS ((enum tree_code));
72 static enum tree_code swap_tree_comparison PARAMS ((enum tree_code));
73 static int truth_value_p PARAMS ((enum tree_code));
74 static int operand_equal_for_comparison_p PARAMS ((tree, tree, tree));
75 static int twoval_comparison_p PARAMS ((tree, tree *, tree *, int *));
76 static tree eval_subst PARAMS ((tree, tree, tree, tree, tree));
77 static tree omit_one_operand PARAMS ((tree, tree, tree));
78 static tree pedantic_omit_one_operand PARAMS ((tree, tree, tree));
79 static tree distribute_bit_expr PARAMS ((enum tree_code, tree, tree, tree));
80 static tree make_bit_field_ref PARAMS ((tree, tree, int, int, int));
81 static tree optimize_bit_field_compare PARAMS ((enum tree_code, tree,
82 tree, tree));
83 static tree decode_field_reference PARAMS ((tree, HOST_WIDE_INT *,
84 HOST_WIDE_INT *,
85 enum machine_mode *, int *,
86 int *, tree *, tree *));
87 static int all_ones_mask_p PARAMS ((tree, int));
88 static int simple_operand_p PARAMS ((tree));
89 static tree range_binop PARAMS ((enum tree_code, tree, tree, int,
90 tree, int));
91 static tree make_range PARAMS ((tree, int *, tree *, tree *));
92 static tree build_range_check PARAMS ((tree, tree, int, tree, tree));
93 static int merge_ranges PARAMS ((int *, tree *, tree *, int, tree, tree,
94 int, tree, tree));
95 static tree fold_range_test PARAMS ((tree));
96 static tree unextend PARAMS ((tree, int, int, tree));
97 static tree fold_truthop PARAMS ((enum tree_code, tree, tree, tree));
98 static tree optimize_minmax_comparison PARAMS ((tree));
99 static tree extract_muldiv PARAMS ((tree, tree, enum tree_code, tree));
100 static tree strip_compound_expr PARAMS ((tree, tree));
101 static int multiple_of_p PARAMS ((tree, tree, tree));
102 static tree constant_boolean_node PARAMS ((int, tree));
103 static int count_cond PARAMS ((tree, int));
104 static tree fold_binary_op_with_conditional_arg
105 PARAMS ((enum tree_code, tree, tree, tree, int));
107 #ifndef BRANCH_COST
108 #define BRANCH_COST 1
109 #endif
111 #if defined(HOST_EBCDIC)
112 /* bit 8 is significant in EBCDIC */
113 #define CHARMASK 0xff
114 #else
115 #define CHARMASK 0x7f
116 #endif
118 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
119 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
120 and SUM1. Then this yields nonzero if overflow occurred during the
121 addition.
123 Overflow occurs if A and B have the same sign, but A and SUM differ in
124 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
125 sign. */
126 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
128 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
129 We do that by representing the two-word integer in 4 words, with only
130 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
131 number. The value of the word is LOWPART + HIGHPART * BASE. */
133 #define LOWPART(x) \
134 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
135 #define HIGHPART(x) \
136 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
137 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
139 /* Unpack a two-word integer into 4 words.
140 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
141 WORDS points to the array of HOST_WIDE_INTs. */
143 static void
144 encode (words, low, hi)
145 HOST_WIDE_INT *words;
146 unsigned HOST_WIDE_INT low;
147 HOST_WIDE_INT hi;
149 words[0] = LOWPART (low);
150 words[1] = HIGHPART (low);
151 words[2] = LOWPART (hi);
152 words[3] = HIGHPART (hi);
155 /* Pack an array of 4 words into a two-word integer.
156 WORDS points to the array of words.
157 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
159 static void
160 decode (words, low, hi)
161 HOST_WIDE_INT *words;
162 unsigned HOST_WIDE_INT *low;
163 HOST_WIDE_INT *hi;
165 *low = words[0] + words[1] * BASE;
166 *hi = words[2] + words[3] * BASE;
169 /* Make the integer constant T valid for its type by setting to 0 or 1 all
170 the bits in the constant that don't belong in the type.
172 Return 1 if a signed overflow occurs, 0 otherwise. If OVERFLOW is
173 nonzero, a signed overflow has already occurred in calculating T, so
174 propagate it.
176 Make the real constant T valid for its type by calling CHECK_FLOAT_VALUE,
177 if it exists. */
180 force_fit_type (t, overflow)
181 tree t;
182 int overflow;
184 unsigned HOST_WIDE_INT low;
185 HOST_WIDE_INT high;
186 unsigned int prec;
188 if (TREE_CODE (t) == REAL_CST)
190 #ifdef CHECK_FLOAT_VALUE
191 CHECK_FLOAT_VALUE (TYPE_MODE (TREE_TYPE (t)), TREE_REAL_CST (t),
192 overflow);
193 #endif
194 return overflow;
197 else if (TREE_CODE (t) != INTEGER_CST)
198 return overflow;
200 low = TREE_INT_CST_LOW (t);
201 high = TREE_INT_CST_HIGH (t);
203 if (POINTER_TYPE_P (TREE_TYPE (t)))
204 prec = POINTER_SIZE;
205 else
206 prec = TYPE_PRECISION (TREE_TYPE (t));
208 /* First clear all bits that are beyond the type's precision. */
210 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
212 else if (prec > HOST_BITS_PER_WIDE_INT)
213 TREE_INT_CST_HIGH (t)
214 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
215 else
217 TREE_INT_CST_HIGH (t) = 0;
218 if (prec < HOST_BITS_PER_WIDE_INT)
219 TREE_INT_CST_LOW (t) &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
222 /* Unsigned types do not suffer sign extension or overflow unless they
223 are a sizetype. */
224 if (TREE_UNSIGNED (TREE_TYPE (t))
225 && ! (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
226 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
227 return overflow;
229 /* If the value's sign bit is set, extend the sign. */
230 if (prec != 2 * HOST_BITS_PER_WIDE_INT
231 && (prec > HOST_BITS_PER_WIDE_INT
232 ? 0 != (TREE_INT_CST_HIGH (t)
233 & ((HOST_WIDE_INT) 1
234 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
235 : 0 != (TREE_INT_CST_LOW (t)
236 & ((unsigned HOST_WIDE_INT) 1 << (prec - 1)))))
238 /* Value is negative:
239 set to 1 all the bits that are outside this type's precision. */
240 if (prec > HOST_BITS_PER_WIDE_INT)
241 TREE_INT_CST_HIGH (t)
242 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
243 else
245 TREE_INT_CST_HIGH (t) = -1;
246 if (prec < HOST_BITS_PER_WIDE_INT)
247 TREE_INT_CST_LOW (t) |= ((unsigned HOST_WIDE_INT) (-1) << prec);
251 /* Return nonzero if signed overflow occurred. */
252 return
253 ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
254 != 0);
257 /* Add two doubleword integers with doubleword result.
258 Each argument is given as two `HOST_WIDE_INT' pieces.
259 One argument is L1 and H1; the other, L2 and H2.
260 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
263 add_double (l1, h1, l2, h2, lv, hv)
264 unsigned HOST_WIDE_INT l1, l2;
265 HOST_WIDE_INT h1, h2;
266 unsigned HOST_WIDE_INT *lv;
267 HOST_WIDE_INT *hv;
269 unsigned HOST_WIDE_INT l;
270 HOST_WIDE_INT h;
272 l = l1 + l2;
273 h = h1 + h2 + (l < l1);
275 *lv = l;
276 *hv = h;
277 return OVERFLOW_SUM_SIGN (h1, h2, h);
280 /* Negate a doubleword integer with doubleword result.
281 Return nonzero if the operation overflows, assuming it's signed.
282 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
283 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
286 neg_double (l1, h1, lv, hv)
287 unsigned HOST_WIDE_INT l1;
288 HOST_WIDE_INT h1;
289 unsigned HOST_WIDE_INT *lv;
290 HOST_WIDE_INT *hv;
292 if (l1 == 0)
294 *lv = 0;
295 *hv = - h1;
296 return (*hv & h1) < 0;
298 else
300 *lv = -l1;
301 *hv = ~h1;
302 return 0;
306 /* Multiply two doubleword integers with doubleword result.
307 Return nonzero if the operation overflows, assuming it's signed.
308 Each argument is given as two `HOST_WIDE_INT' pieces.
309 One argument is L1 and H1; the other, L2 and H2.
310 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
313 mul_double (l1, h1, l2, h2, lv, hv)
314 unsigned HOST_WIDE_INT l1, l2;
315 HOST_WIDE_INT h1, h2;
316 unsigned HOST_WIDE_INT *lv;
317 HOST_WIDE_INT *hv;
319 HOST_WIDE_INT arg1[4];
320 HOST_WIDE_INT arg2[4];
321 HOST_WIDE_INT prod[4 * 2];
322 register unsigned HOST_WIDE_INT carry;
323 register int i, j, k;
324 unsigned HOST_WIDE_INT toplow, neglow;
325 HOST_WIDE_INT tophigh, neghigh;
327 encode (arg1, l1, h1);
328 encode (arg2, l2, h2);
330 memset ((char *) prod, 0, sizeof prod);
332 for (i = 0; i < 4; i++)
334 carry = 0;
335 for (j = 0; j < 4; j++)
337 k = i + j;
338 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
339 carry += arg1[i] * arg2[j];
340 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
341 carry += prod[k];
342 prod[k] = LOWPART (carry);
343 carry = HIGHPART (carry);
345 prod[i + 4] = carry;
348 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
350 /* Check for overflow by calculating the top half of the answer in full;
351 it should agree with the low half's sign bit. */
352 decode (prod + 4, &toplow, &tophigh);
353 if (h1 < 0)
355 neg_double (l2, h2, &neglow, &neghigh);
356 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
358 if (h2 < 0)
360 neg_double (l1, h1, &neglow, &neghigh);
361 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
363 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
366 /* Shift the doubleword integer in L1, H1 left by COUNT places
367 keeping only PREC bits of result.
368 Shift right if COUNT is negative.
369 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
370 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
372 void
373 lshift_double (l1, h1, count, prec, lv, hv, arith)
374 unsigned HOST_WIDE_INT l1;
375 HOST_WIDE_INT h1, count;
376 unsigned int prec;
377 unsigned HOST_WIDE_INT *lv;
378 HOST_WIDE_INT *hv;
379 int arith;
381 if (count < 0)
383 rshift_double (l1, h1, -count, prec, lv, hv, arith);
384 return;
387 #ifdef SHIFT_COUNT_TRUNCATED
388 if (SHIFT_COUNT_TRUNCATED)
389 count %= prec;
390 #endif
392 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
394 /* Shifting by the host word size is undefined according to the
395 ANSI standard, so we must handle this as a special case. */
396 *hv = 0;
397 *lv = 0;
399 else if (count >= HOST_BITS_PER_WIDE_INT)
401 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
402 *lv = 0;
404 else
406 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
407 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
408 *lv = l1 << count;
412 /* Shift the doubleword integer in L1, H1 right by COUNT places
413 keeping only PREC bits of result. COUNT must be positive.
414 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
415 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
417 void
418 rshift_double (l1, h1, count, prec, lv, hv, arith)
419 unsigned HOST_WIDE_INT l1;
420 HOST_WIDE_INT h1, count;
421 unsigned int prec ATTRIBUTE_UNUSED;
422 unsigned HOST_WIDE_INT *lv;
423 HOST_WIDE_INT *hv;
424 int arith;
426 unsigned HOST_WIDE_INT signmask;
428 signmask = (arith
429 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
430 : 0);
432 #ifdef SHIFT_COUNT_TRUNCATED
433 if (SHIFT_COUNT_TRUNCATED)
434 count %= prec;
435 #endif
437 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
439 /* Shifting by the host word size is undefined according to the
440 ANSI standard, so we must handle this as a special case. */
441 *hv = signmask;
442 *lv = signmask;
444 else if (count >= HOST_BITS_PER_WIDE_INT)
446 *hv = signmask;
447 *lv = ((signmask << (2 * HOST_BITS_PER_WIDE_INT - count - 1) << 1)
448 | ((unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT)));
450 else
452 *lv = ((l1 >> count)
453 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
454 *hv = ((signmask << (HOST_BITS_PER_WIDE_INT - count))
455 | ((unsigned HOST_WIDE_INT) h1 >> count));
459 /* Rotate the doubleword integer in L1, H1 left by COUNT places
460 keeping only PREC bits of result.
461 Rotate right if COUNT is negative.
462 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
464 void
465 lrotate_double (l1, h1, count, prec, lv, hv)
466 unsigned HOST_WIDE_INT l1;
467 HOST_WIDE_INT h1, count;
468 unsigned int prec;
469 unsigned HOST_WIDE_INT *lv;
470 HOST_WIDE_INT *hv;
472 unsigned HOST_WIDE_INT s1l, s2l;
473 HOST_WIDE_INT s1h, s2h;
475 count %= prec;
476 if (count < 0)
477 count += prec;
479 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
480 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
481 *lv = s1l | s2l;
482 *hv = s1h | s2h;
485 /* Rotate the doubleword integer in L1, H1 left by COUNT places
486 keeping only PREC bits of result. COUNT must be positive.
487 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
489 void
490 rrotate_double (l1, h1, count, prec, lv, hv)
491 unsigned HOST_WIDE_INT l1;
492 HOST_WIDE_INT h1, count;
493 unsigned int prec;
494 unsigned HOST_WIDE_INT *lv;
495 HOST_WIDE_INT *hv;
497 unsigned HOST_WIDE_INT s1l, s2l;
498 HOST_WIDE_INT s1h, s2h;
500 count %= prec;
501 if (count < 0)
502 count += prec;
504 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
505 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
506 *lv = s1l | s2l;
507 *hv = s1h | s2h;
510 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
511 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
512 CODE is a tree code for a kind of division, one of
513 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
514 or EXACT_DIV_EXPR
515 It controls how the quotient is rounded to a integer.
516 Return nonzero if the operation overflows.
517 UNS nonzero says do unsigned division. */
520 div_and_round_double (code, uns,
521 lnum_orig, hnum_orig, lden_orig, hden_orig,
522 lquo, hquo, lrem, hrem)
523 enum tree_code code;
524 int uns;
525 unsigned HOST_WIDE_INT lnum_orig; /* num == numerator == dividend */
526 HOST_WIDE_INT hnum_orig;
527 unsigned HOST_WIDE_INT lden_orig; /* den == denominator == divisor */
528 HOST_WIDE_INT hden_orig;
529 unsigned HOST_WIDE_INT *lquo, *lrem;
530 HOST_WIDE_INT *hquo, *hrem;
532 int quo_neg = 0;
533 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
534 HOST_WIDE_INT den[4], quo[4];
535 register int i, j;
536 unsigned HOST_WIDE_INT work;
537 unsigned HOST_WIDE_INT carry = 0;
538 unsigned HOST_WIDE_INT lnum = lnum_orig;
539 HOST_WIDE_INT hnum = hnum_orig;
540 unsigned HOST_WIDE_INT lden = lden_orig;
541 HOST_WIDE_INT hden = hden_orig;
542 int overflow = 0;
544 if (hden == 0 && lden == 0)
545 overflow = 1, lden = 1;
547 /* calculate quotient sign and convert operands to unsigned. */
548 if (!uns)
550 if (hnum < 0)
552 quo_neg = ~ quo_neg;
553 /* (minimum integer) / (-1) is the only overflow case. */
554 if (neg_double (lnum, hnum, &lnum, &hnum)
555 && ((HOST_WIDE_INT) lden & hden) == -1)
556 overflow = 1;
558 if (hden < 0)
560 quo_neg = ~ quo_neg;
561 neg_double (lden, hden, &lden, &hden);
565 if (hnum == 0 && hden == 0)
566 { /* single precision */
567 *hquo = *hrem = 0;
568 /* This unsigned division rounds toward zero. */
569 *lquo = lnum / lden;
570 goto finish_up;
573 if (hnum == 0)
574 { /* trivial case: dividend < divisor */
575 /* hden != 0 already checked. */
576 *hquo = *lquo = 0;
577 *hrem = hnum;
578 *lrem = lnum;
579 goto finish_up;
582 memset ((char *) quo, 0, sizeof quo);
584 memset ((char *) num, 0, sizeof num); /* to zero 9th element */
585 memset ((char *) den, 0, sizeof den);
587 encode (num, lnum, hnum);
588 encode (den, lden, hden);
590 /* Special code for when the divisor < BASE. */
591 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
593 /* hnum != 0 already checked. */
594 for (i = 4 - 1; i >= 0; i--)
596 work = num[i] + carry * BASE;
597 quo[i] = work / lden;
598 carry = work % lden;
601 else
603 /* Full double precision division,
604 with thanks to Don Knuth's "Seminumerical Algorithms". */
605 int num_hi_sig, den_hi_sig;
606 unsigned HOST_WIDE_INT quo_est, scale;
608 /* Find the highest non-zero divisor digit. */
609 for (i = 4 - 1;; i--)
610 if (den[i] != 0)
612 den_hi_sig = i;
613 break;
616 /* Insure that the first digit of the divisor is at least BASE/2.
617 This is required by the quotient digit estimation algorithm. */
619 scale = BASE / (den[den_hi_sig] + 1);
620 if (scale > 1)
621 { /* scale divisor and dividend */
622 carry = 0;
623 for (i = 0; i <= 4 - 1; i++)
625 work = (num[i] * scale) + carry;
626 num[i] = LOWPART (work);
627 carry = HIGHPART (work);
630 num[4] = carry;
631 carry = 0;
632 for (i = 0; i <= 4 - 1; i++)
634 work = (den[i] * scale) + carry;
635 den[i] = LOWPART (work);
636 carry = HIGHPART (work);
637 if (den[i] != 0) den_hi_sig = i;
641 num_hi_sig = 4;
643 /* Main loop */
644 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
646 /* Guess the next quotient digit, quo_est, by dividing the first
647 two remaining dividend digits by the high order quotient digit.
648 quo_est is never low and is at most 2 high. */
649 unsigned HOST_WIDE_INT tmp;
651 num_hi_sig = i + den_hi_sig + 1;
652 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
653 if (num[num_hi_sig] != den[den_hi_sig])
654 quo_est = work / den[den_hi_sig];
655 else
656 quo_est = BASE - 1;
658 /* Refine quo_est so it's usually correct, and at most one high. */
659 tmp = work - quo_est * den[den_hi_sig];
660 if (tmp < BASE
661 && (den[den_hi_sig - 1] * quo_est
662 > (tmp * BASE + num[num_hi_sig - 2])))
663 quo_est--;
665 /* Try QUO_EST as the quotient digit, by multiplying the
666 divisor by QUO_EST and subtracting from the remaining dividend.
667 Keep in mind that QUO_EST is the I - 1st digit. */
669 carry = 0;
670 for (j = 0; j <= den_hi_sig; j++)
672 work = quo_est * den[j] + carry;
673 carry = HIGHPART (work);
674 work = num[i + j] - LOWPART (work);
675 num[i + j] = LOWPART (work);
676 carry += HIGHPART (work) != 0;
679 /* If quo_est was high by one, then num[i] went negative and
680 we need to correct things. */
681 if (num[num_hi_sig] < carry)
683 quo_est--;
684 carry = 0; /* add divisor back in */
685 for (j = 0; j <= den_hi_sig; j++)
687 work = num[i + j] + den[j] + carry;
688 carry = HIGHPART (work);
689 num[i + j] = LOWPART (work);
692 num [num_hi_sig] += carry;
695 /* Store the quotient digit. */
696 quo[i] = quo_est;
700 decode (quo, lquo, hquo);
702 finish_up:
703 /* if result is negative, make it so. */
704 if (quo_neg)
705 neg_double (*lquo, *hquo, lquo, hquo);
707 /* compute trial remainder: rem = num - (quo * den) */
708 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
709 neg_double (*lrem, *hrem, lrem, hrem);
710 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
712 switch (code)
714 case TRUNC_DIV_EXPR:
715 case TRUNC_MOD_EXPR: /* round toward zero */
716 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
717 return overflow;
719 case FLOOR_DIV_EXPR:
720 case FLOOR_MOD_EXPR: /* round toward negative infinity */
721 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
723 /* quo = quo - 1; */
724 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
725 lquo, hquo);
727 else
728 return overflow;
729 break;
731 case CEIL_DIV_EXPR:
732 case CEIL_MOD_EXPR: /* round toward positive infinity */
733 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
735 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
736 lquo, hquo);
738 else
739 return overflow;
740 break;
742 case ROUND_DIV_EXPR:
743 case ROUND_MOD_EXPR: /* round to closest integer */
745 unsigned HOST_WIDE_INT labs_rem = *lrem;
746 HOST_WIDE_INT habs_rem = *hrem;
747 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
748 HOST_WIDE_INT habs_den = hden, htwice;
750 /* Get absolute values */
751 if (*hrem < 0)
752 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
753 if (hden < 0)
754 neg_double (lden, hden, &labs_den, &habs_den);
756 /* If (2 * abs (lrem) >= abs (lden)) */
757 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
758 labs_rem, habs_rem, &ltwice, &htwice);
760 if (((unsigned HOST_WIDE_INT) habs_den
761 < (unsigned HOST_WIDE_INT) htwice)
762 || (((unsigned HOST_WIDE_INT) habs_den
763 == (unsigned HOST_WIDE_INT) htwice)
764 && (labs_den < ltwice)))
766 if (*hquo < 0)
767 /* quo = quo - 1; */
768 add_double (*lquo, *hquo,
769 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
770 else
771 /* quo = quo + 1; */
772 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
773 lquo, hquo);
775 else
776 return overflow;
778 break;
780 default:
781 abort ();
784 /* compute true remainder: rem = num - (quo * den) */
785 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
786 neg_double (*lrem, *hrem, lrem, hrem);
787 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
788 return overflow;
791 #ifndef REAL_ARITHMETIC
792 /* Effectively truncate a real value to represent the nearest possible value
793 in a narrower mode. The result is actually represented in the same data
794 type as the argument, but its value is usually different.
796 A trap may occur during the FP operations and it is the responsibility
797 of the calling function to have a handler established. */
799 REAL_VALUE_TYPE
800 real_value_truncate (mode, arg)
801 enum machine_mode mode;
802 REAL_VALUE_TYPE arg;
804 return REAL_VALUE_TRUNCATE (mode, arg);
807 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
809 /* Check for infinity in an IEEE double precision number. */
812 target_isinf (x)
813 REAL_VALUE_TYPE x;
815 /* The IEEE 64-bit double format. */
816 union {
817 REAL_VALUE_TYPE d;
818 struct {
819 unsigned sign : 1;
820 unsigned exponent : 11;
821 unsigned mantissa1 : 20;
822 unsigned mantissa2;
823 } little_endian;
824 struct {
825 unsigned mantissa2;
826 unsigned mantissa1 : 20;
827 unsigned exponent : 11;
828 unsigned sign : 1;
829 } big_endian;
830 } u;
832 u.d = dconstm1;
833 if (u.big_endian.sign == 1)
835 u.d = x;
836 return (u.big_endian.exponent == 2047
837 && u.big_endian.mantissa1 == 0
838 && u.big_endian.mantissa2 == 0);
840 else
842 u.d = x;
843 return (u.little_endian.exponent == 2047
844 && u.little_endian.mantissa1 == 0
845 && u.little_endian.mantissa2 == 0);
849 /* Check whether an IEEE double precision number is a NaN. */
852 target_isnan (x)
853 REAL_VALUE_TYPE x;
855 /* The IEEE 64-bit double format. */
856 union {
857 REAL_VALUE_TYPE d;
858 struct {
859 unsigned sign : 1;
860 unsigned exponent : 11;
861 unsigned mantissa1 : 20;
862 unsigned mantissa2;
863 } little_endian;
864 struct {
865 unsigned mantissa2;
866 unsigned mantissa1 : 20;
867 unsigned exponent : 11;
868 unsigned sign : 1;
869 } big_endian;
870 } u;
872 u.d = dconstm1;
873 if (u.big_endian.sign == 1)
875 u.d = x;
876 return (u.big_endian.exponent == 2047
877 && (u.big_endian.mantissa1 != 0
878 || u.big_endian.mantissa2 != 0));
880 else
882 u.d = x;
883 return (u.little_endian.exponent == 2047
884 && (u.little_endian.mantissa1 != 0
885 || u.little_endian.mantissa2 != 0));
889 /* Check for a negative IEEE double precision number. */
892 target_negative (x)
893 REAL_VALUE_TYPE x;
895 /* The IEEE 64-bit double format. */
896 union {
897 REAL_VALUE_TYPE d;
898 struct {
899 unsigned sign : 1;
900 unsigned exponent : 11;
901 unsigned mantissa1 : 20;
902 unsigned mantissa2;
903 } little_endian;
904 struct {
905 unsigned mantissa2;
906 unsigned mantissa1 : 20;
907 unsigned exponent : 11;
908 unsigned sign : 1;
909 } big_endian;
910 } u;
912 u.d = dconstm1;
913 if (u.big_endian.sign == 1)
915 u.d = x;
916 return u.big_endian.sign;
918 else
920 u.d = x;
921 return u.little_endian.sign;
924 #else /* Target not IEEE */
926 /* Let's assume other float formats don't have infinity.
927 (This can be overridden by redefining REAL_VALUE_ISINF.) */
930 target_isinf (x)
931 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
933 return 0;
936 /* Let's assume other float formats don't have NaNs.
937 (This can be overridden by redefining REAL_VALUE_ISNAN.) */
940 target_isnan (x)
941 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
943 return 0;
946 /* Let's assume other float formats don't have minus zero.
947 (This can be overridden by redefining REAL_VALUE_NEGATIVE.) */
950 target_negative (x)
951 REAL_VALUE_TYPE x;
953 return x < 0;
955 #endif /* Target not IEEE */
957 /* Try to change R into its exact multiplicative inverse in machine mode
958 MODE. Return nonzero function value if successful. */
961 exact_real_inverse (mode, r)
962 enum machine_mode mode;
963 REAL_VALUE_TYPE *r;
965 jmp_buf float_error;
966 union
968 double d;
969 unsigned short i[4];
970 }x, t, y;
971 #ifdef CHECK_FLOAT_VALUE
972 int i;
973 #endif
975 /* Usually disable if bounds checks are not reliable. */
976 if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT) && !flag_pretend_float)
977 return 0;
979 /* Set array index to the less significant bits in the unions, depending
980 on the endian-ness of the host doubles.
981 Disable if insufficient information on the data structure. */
982 #if HOST_FLOAT_FORMAT == UNKNOWN_FLOAT_FORMAT
983 return 0;
984 #else
985 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
986 #define K 2
987 #else
988 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
989 #define K 2
990 #else
991 #define K (2 * HOST_FLOAT_WORDS_BIG_ENDIAN)
992 #endif
993 #endif
994 #endif
996 if (setjmp (float_error))
998 /* Don't do the optimization if there was an arithmetic error. */
999 fail:
1000 set_float_handler (NULL_PTR);
1001 return 0;
1003 set_float_handler (float_error);
1005 /* Domain check the argument. */
1006 x.d = *r;
1007 if (x.d == 0.0)
1008 goto fail;
1010 #ifdef REAL_INFINITY
1011 if (REAL_VALUE_ISINF (x.d) || REAL_VALUE_ISNAN (x.d))
1012 goto fail;
1013 #endif
1015 /* Compute the reciprocal and check for numerical exactness.
1016 It is unnecessary to check all the significand bits to determine
1017 whether X is a power of 2. If X is not, then it is impossible for
1018 the bottom half significand of both X and 1/X to be all zero bits.
1019 Hence we ignore the data structure of the top half and examine only
1020 the low order bits of the two significands. */
1021 t.d = 1.0 / x.d;
1022 if (x.i[K] != 0 || x.i[K + 1] != 0 || t.i[K] != 0 || t.i[K + 1] != 0)
1023 goto fail;
1025 /* Truncate to the required mode and range-check the result. */
1026 y.d = REAL_VALUE_TRUNCATE (mode, t.d);
1027 #ifdef CHECK_FLOAT_VALUE
1028 i = 0;
1029 if (CHECK_FLOAT_VALUE (mode, y.d, i))
1030 goto fail;
1031 #endif
1033 /* Fail if truncation changed the value. */
1034 if (y.d != t.d || y.d == 0.0)
1035 goto fail;
1037 #ifdef REAL_INFINITY
1038 if (REAL_VALUE_ISINF (y.d) || REAL_VALUE_ISNAN (y.d))
1039 goto fail;
1040 #endif
1042 /* Output the reciprocal and return success flag. */
1043 set_float_handler (NULL_PTR);
1044 *r = y.d;
1045 return 1;
1048 /* Convert C99 hexadecimal floating point string constant S. Return
1049 real value type in mode MODE. This function uses the host computer's
1050 floating point arithmetic when there is no REAL_ARITHMETIC. */
1052 REAL_VALUE_TYPE
1053 real_hex_to_f (s, mode)
1054 char *s;
1055 enum machine_mode mode;
1057 REAL_VALUE_TYPE ip;
1058 char *p = s;
1059 unsigned HOST_WIDE_INT low, high;
1060 int shcount, nrmcount, k;
1061 int sign, expsign, isfloat;
1062 int lost = 0;/* Nonzero low order bits shifted out and discarded. */
1063 int frexpon = 0; /* Bits after the decimal point. */
1064 int expon = 0; /* Value of exponent. */
1065 int decpt = 0; /* How many decimal points. */
1066 int gotp = 0; /* How many P's. */
1067 char c;
1069 isfloat = 0;
1070 expsign = 1;
1071 ip = 0.0;
1073 while (*p == ' ' || *p == '\t')
1074 ++p;
1076 /* Sign, if any, comes first. */
1077 sign = 1;
1078 if (*p == '-')
1080 sign = -1;
1081 ++p;
1084 /* The string is supposed to start with 0x or 0X . */
1085 if (*p == '0')
1087 ++p;
1088 if (*p == 'x' || *p == 'X')
1089 ++p;
1090 else
1091 abort ();
1093 else
1094 abort ();
1096 while (*p == '0')
1097 ++p;
1099 high = 0;
1100 low = 0;
1101 shcount = 0;
1102 while ((c = *p) != '\0')
1104 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')
1105 || (c >= 'a' && c <= 'f'))
1107 k = c & CHARMASK;
1108 if (k >= 'a' && k <= 'f')
1109 k = k - 'a' + 10;
1110 else if (k >= 'A')
1111 k = k - 'A' + 10;
1112 else
1113 k = k - '0';
1115 if ((high & 0xf0000000) == 0)
1117 high = (high << 4) + ((low >> 28) & 15);
1118 low = (low << 4) + k;
1119 shcount += 4;
1120 if (decpt)
1121 frexpon += 4;
1123 else
1125 /* Record nonzero lost bits. */
1126 lost |= k;
1127 if (! decpt)
1128 frexpon -= 4;
1130 ++p;
1132 else if (c == '.')
1134 ++decpt;
1135 ++p;
1138 else if (c == 'p' || c == 'P')
1140 ++gotp;
1141 ++p;
1142 /* Sign of exponent. */
1143 if (*p == '-')
1145 expsign = -1;
1146 ++p;
1149 /* Value of exponent.
1150 The exponent field is a decimal integer. */
1151 while (ISDIGIT (*p))
1153 k = (*p++ & CHARMASK) - '0';
1154 expon = 10 * expon + k;
1157 expon *= expsign;
1158 /* F suffix is ambiguous in the significand part
1159 so it must appear after the decimal exponent field. */
1160 if (*p == 'f' || *p == 'F')
1162 isfloat = 1;
1163 ++p;
1164 break;
1168 else if (c == 'l' || c == 'L')
1170 ++p;
1171 break;
1173 else
1174 break;
1177 /* Abort if last character read was not legitimate. */
1178 c = *p;
1179 if ((c != '\0' && c != ' ' && c != '\n' && c != '\r') || (decpt > 1))
1180 abort ();
1182 /* There must be either one decimal point or one p. */
1183 if (decpt == 0 && gotp == 0)
1184 abort ();
1186 shcount -= 4;
1187 if (high == 0 && low == 0)
1188 return dconst0;
1190 /* Normalize. */
1191 nrmcount = 0;
1192 if (high == 0)
1194 high = low;
1195 low = 0;
1196 nrmcount += 32;
1199 /* Leave a high guard bit for carry-out. */
1200 if ((high & 0x80000000) != 0)
1202 lost |= low & 1;
1203 low = (low >> 1) | (high << 31);
1204 high = high >> 1;
1205 nrmcount -= 1;
1208 if ((high & 0xffff8000) == 0)
1210 high = (high << 16) + ((low >> 16) & 0xffff);
1211 low = low << 16;
1212 nrmcount += 16;
1215 while ((high & 0xc0000000) == 0)
1217 high = (high << 1) + ((low >> 31) & 1);
1218 low = low << 1;
1219 nrmcount += 1;
1222 if (isfloat || GET_MODE_SIZE (mode) == UNITS_PER_WORD)
1224 /* Keep 24 bits precision, bits 0x7fffff80.
1225 Rounding bit is 0x40. */
1226 lost = lost | low | (high & 0x3f);
1227 low = 0;
1228 if (high & 0x40)
1230 if ((high & 0x80) || lost)
1231 high += 0x40;
1233 high &= 0xffffff80;
1235 else
1237 /* We need real.c to do long double formats, so here default
1238 to double precision. */
1239 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1240 /* IEEE double.
1241 Keep 53 bits precision, bits 0x7fffffff fffffc00.
1242 Rounding bit is low word 0x200. */
1243 lost = lost | (low & 0x1ff);
1244 if (low & 0x200)
1246 if ((low & 0x400) || lost)
1248 low = (low + 0x200) & 0xfffffc00;
1249 if (low == 0)
1250 high += 1;
1253 low &= 0xfffffc00;
1254 #else
1255 /* Assume it's a VAX with 56-bit significand,
1256 bits 0x7fffffff ffffff80. */
1257 lost = lost | (low & 0x7f);
1258 if (low & 0x40)
1260 if ((low & 0x80) || lost)
1262 low = (low + 0x40) & 0xffffff80;
1263 if (low == 0)
1264 high += 1;
1267 low &= 0xffffff80;
1268 #endif
1271 ip = (double) high;
1272 ip = REAL_VALUE_LDEXP (ip, 32) + (double) low;
1273 /* Apply shifts and exponent value as power of 2. */
1274 ip = REAL_VALUE_LDEXP (ip, expon - (nrmcount + frexpon));
1276 if (sign < 0)
1277 ip = -ip;
1278 return ip;
1281 #endif /* no REAL_ARITHMETIC */
1283 /* Given T, an expression, return the negation of T. Allow for T to be
1284 null, in which case return null. */
1286 static tree
1287 negate_expr (t)
1288 tree t;
1290 tree type;
1291 tree tem;
1293 if (t == 0)
1294 return 0;
1296 type = TREE_TYPE (t);
1297 STRIP_SIGN_NOPS (t);
1299 switch (TREE_CODE (t))
1301 case INTEGER_CST:
1302 case REAL_CST:
1303 if (! TREE_UNSIGNED (type)
1304 && 0 != (tem = fold (build1 (NEGATE_EXPR, type, t)))
1305 && ! TREE_OVERFLOW (tem))
1306 return tem;
1307 break;
1309 case NEGATE_EXPR:
1310 return convert (type, TREE_OPERAND (t, 0));
1312 case MINUS_EXPR:
1313 /* - (A - B) -> B - A */
1314 if (! FLOAT_TYPE_P (type) || flag_fast_math)
1315 return convert (type,
1316 fold (build (MINUS_EXPR, TREE_TYPE (t),
1317 TREE_OPERAND (t, 1),
1318 TREE_OPERAND (t, 0))));
1319 break;
1321 default:
1322 break;
1325 return convert (type, build1 (NEGATE_EXPR, TREE_TYPE (t), t));
1328 /* Split a tree IN into a constant, literal and variable parts that could be
1329 combined with CODE to make IN. "constant" means an expression with
1330 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1331 commutative arithmetic operation. Store the constant part into *CONP,
1332 the literal in &LITP and return the variable part. If a part isn't
1333 present, set it to null. If the tree does not decompose in this way,
1334 return the entire tree as the variable part and the other parts as null.
1336 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1337 case, we negate an operand that was subtracted. If NEGATE_P is true, we
1338 are negating all of IN.
1340 If IN is itself a literal or constant, return it as appropriate.
1342 Note that we do not guarantee that any of the three values will be the
1343 same type as IN, but they will have the same signedness and mode. */
1345 static tree
1346 split_tree (in, code, conp, litp, negate_p)
1347 tree in;
1348 enum tree_code code;
1349 tree *conp, *litp;
1350 int negate_p;
1352 tree var = 0;
1354 *conp = 0;
1355 *litp = 0;
1357 /* Strip any conversions that don't change the machine mode or signedness. */
1358 STRIP_SIGN_NOPS (in);
1360 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1361 *litp = in;
1362 else if (TREE_CODE (in) == code
1363 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1364 /* We can associate addition and subtraction together (even
1365 though the C standard doesn't say so) for integers because
1366 the value is not affected. For reals, the value might be
1367 affected, so we can't. */
1368 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1369 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1371 tree op0 = TREE_OPERAND (in, 0);
1372 tree op1 = TREE_OPERAND (in, 1);
1373 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1374 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1376 /* First see if either of the operands is a literal, then a constant. */
1377 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1378 *litp = op0, op0 = 0;
1379 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1380 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1382 if (op0 != 0 && TREE_CONSTANT (op0))
1383 *conp = op0, op0 = 0;
1384 else if (op1 != 0 && TREE_CONSTANT (op1))
1385 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1387 /* If we haven't dealt with either operand, this is not a case we can
1388 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1389 if (op0 != 0 && op1 != 0)
1390 var = in;
1391 else if (op0 != 0)
1392 var = op0;
1393 else
1394 var = op1, neg_var_p = neg1_p;
1396 /* Now do any needed negations. */
1397 if (neg_litp_p) *litp = negate_expr (*litp);
1398 if (neg_conp_p) *conp = negate_expr (*conp);
1399 if (neg_var_p) var = negate_expr (var);
1401 else if (TREE_CONSTANT (in))
1402 *conp = in;
1403 else
1404 var = in;
1406 if (negate_p)
1408 var = negate_expr (var);
1409 *conp = negate_expr (*conp);
1410 *litp = negate_expr (*litp);
1413 return var;
1416 /* Re-associate trees split by the above function. T1 and T2 are either
1417 expressions to associate or null. Return the new expression, if any. If
1418 we build an operation, do it in TYPE and with CODE, except if CODE is a
1419 MINUS_EXPR, in which case we use PLUS_EXPR since split_tree will already
1420 have taken care of the negations. */
1422 static tree
1423 associate_trees (t1, t2, code, type)
1424 tree t1, t2;
1425 enum tree_code code;
1426 tree type;
1428 if (t1 == 0)
1429 return t2;
1430 else if (t2 == 0)
1431 return t1;
1433 if (code == MINUS_EXPR)
1434 code = PLUS_EXPR;
1436 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1437 try to fold this since we will have infinite recursion. But do
1438 deal with any NEGATE_EXPRs. */
1439 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1440 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1442 if (TREE_CODE (t1) == NEGATE_EXPR)
1443 return build (MINUS_EXPR, type, convert (type, t2),
1444 convert (type, TREE_OPERAND (t1, 0)));
1445 else if (TREE_CODE (t2) == NEGATE_EXPR)
1446 return build (MINUS_EXPR, type, convert (type, t1),
1447 convert (type, TREE_OPERAND (t2, 0)));
1448 else
1449 return build (code, type, convert (type, t1), convert (type, t2));
1452 return fold (build (code, type, convert (type, t1), convert (type, t2)));
1455 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1456 to produce a new constant.
1458 If NOTRUNC is nonzero, do not truncate the result to fit the data type.
1459 If FORSIZE is nonzero, compute overflow for unsigned types. */
1461 static tree
1462 int_const_binop (code, arg1, arg2, notrunc, forsize)
1463 enum tree_code code;
1464 register tree arg1, arg2;
1465 int notrunc, forsize;
1467 unsigned HOST_WIDE_INT int1l, int2l;
1468 HOST_WIDE_INT int1h, int2h;
1469 unsigned HOST_WIDE_INT low;
1470 HOST_WIDE_INT hi;
1471 unsigned HOST_WIDE_INT garbagel;
1472 HOST_WIDE_INT garbageh;
1473 register tree t;
1474 int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
1475 int overflow = 0;
1476 int no_overflow = 0;
1478 int1l = TREE_INT_CST_LOW (arg1);
1479 int1h = TREE_INT_CST_HIGH (arg1);
1480 int2l = TREE_INT_CST_LOW (arg2);
1481 int2h = TREE_INT_CST_HIGH (arg2);
1483 switch (code)
1485 case BIT_IOR_EXPR:
1486 low = int1l | int2l, hi = int1h | int2h;
1487 break;
1489 case BIT_XOR_EXPR:
1490 low = int1l ^ int2l, hi = int1h ^ int2h;
1491 break;
1493 case BIT_AND_EXPR:
1494 low = int1l & int2l, hi = int1h & int2h;
1495 break;
1497 case BIT_ANDTC_EXPR:
1498 low = int1l & ~int2l, hi = int1h & ~int2h;
1499 break;
1501 case RSHIFT_EXPR:
1502 int2l = -int2l;
1503 case LSHIFT_EXPR:
1504 /* It's unclear from the C standard whether shifts can overflow.
1505 The following code ignores overflow; perhaps a C standard
1506 interpretation ruling is needed. */
1507 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (TREE_TYPE (arg1)),
1508 &low, &hi, !uns);
1509 no_overflow = 1;
1510 break;
1512 case RROTATE_EXPR:
1513 int2l = - int2l;
1514 case LROTATE_EXPR:
1515 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (TREE_TYPE (arg1)),
1516 &low, &hi);
1517 break;
1519 case PLUS_EXPR:
1520 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1521 break;
1523 case MINUS_EXPR:
1524 neg_double (int2l, int2h, &low, &hi);
1525 add_double (int1l, int1h, low, hi, &low, &hi);
1526 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1527 break;
1529 case MULT_EXPR:
1530 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1531 break;
1533 case TRUNC_DIV_EXPR:
1534 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1535 case EXACT_DIV_EXPR:
1536 /* This is a shortcut for a common special case. */
1537 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1538 && ! TREE_CONSTANT_OVERFLOW (arg1)
1539 && ! TREE_CONSTANT_OVERFLOW (arg2)
1540 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1542 if (code == CEIL_DIV_EXPR)
1543 int1l += int2l - 1;
1545 low = int1l / int2l, hi = 0;
1546 break;
1549 /* ... fall through ... */
1551 case ROUND_DIV_EXPR:
1552 if (int2h == 0 && int2l == 1)
1554 low = int1l, hi = int1h;
1555 break;
1557 if (int1l == int2l && int1h == int2h
1558 && ! (int1l == 0 && int1h == 0))
1560 low = 1, hi = 0;
1561 break;
1563 overflow = div_and_round_double (code, uns,
1564 int1l, int1h, int2l, int2h,
1565 &low, &hi, &garbagel, &garbageh);
1566 break;
1568 case TRUNC_MOD_EXPR:
1569 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1570 /* This is a shortcut for a common special case. */
1571 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1572 && ! TREE_CONSTANT_OVERFLOW (arg1)
1573 && ! TREE_CONSTANT_OVERFLOW (arg2)
1574 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1576 if (code == CEIL_MOD_EXPR)
1577 int1l += int2l - 1;
1578 low = int1l % int2l, hi = 0;
1579 break;
1582 /* ... fall through ... */
1584 case ROUND_MOD_EXPR:
1585 overflow = div_and_round_double (code, uns,
1586 int1l, int1h, int2l, int2h,
1587 &garbagel, &garbageh, &low, &hi);
1588 break;
1590 case MIN_EXPR:
1591 case MAX_EXPR:
1592 if (uns)
1593 low = (((unsigned HOST_WIDE_INT) int1h
1594 < (unsigned HOST_WIDE_INT) int2h)
1595 || (((unsigned HOST_WIDE_INT) int1h
1596 == (unsigned HOST_WIDE_INT) int2h)
1597 && int1l < int2l));
1598 else
1599 low = (int1h < int2h
1600 || (int1h == int2h && int1l < int2l));
1602 if (low == (code == MIN_EXPR))
1603 low = int1l, hi = int1h;
1604 else
1605 low = int2l, hi = int2h;
1606 break;
1608 default:
1609 abort ();
1612 if (forsize && hi == 0 && low < 10000
1613 && overflow == 0 && ! TREE_OVERFLOW (arg1) && ! TREE_OVERFLOW (arg2))
1614 return size_int_type_wide (low, TREE_TYPE (arg1));
1615 else
1617 t = build_int_2 (low, hi);
1618 TREE_TYPE (t) = TREE_TYPE (arg1);
1621 TREE_OVERFLOW (t)
1622 = ((notrunc ? (!uns || forsize) && overflow
1623 : force_fit_type (t, (!uns || forsize) && overflow) && ! no_overflow)
1624 | TREE_OVERFLOW (arg1)
1625 | TREE_OVERFLOW (arg2));
1627 /* If we're doing a size calculation, unsigned arithmetic does overflow.
1628 So check if force_fit_type truncated the value. */
1629 if (forsize
1630 && ! TREE_OVERFLOW (t)
1631 && (TREE_INT_CST_HIGH (t) != hi
1632 || TREE_INT_CST_LOW (t) != low))
1633 TREE_OVERFLOW (t) = 1;
1635 TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1636 | TREE_CONSTANT_OVERFLOW (arg1)
1637 | TREE_CONSTANT_OVERFLOW (arg2));
1638 return t;
1641 /* Define input and output argument for const_binop_1. */
1642 struct cb_args
1644 enum tree_code code; /* Input: tree code for operation. */
1645 tree type; /* Input: tree type for operation. */
1646 REAL_VALUE_TYPE d1, d2; /* Input: floating point operands. */
1647 tree t; /* Output: constant for result. */
1650 /* Do the real arithmetic for const_binop while protected by a
1651 float overflow handler. */
1653 static void
1654 const_binop_1 (data)
1655 PTR data;
1657 struct cb_args *args = (struct cb_args *) data;
1658 REAL_VALUE_TYPE value;
1660 #ifdef REAL_ARITHMETIC
1661 REAL_ARITHMETIC (value, args->code, args->d1, args->d2);
1662 #else
1663 switch (args->code)
1665 case PLUS_EXPR:
1666 value = args->d1 + args->d2;
1667 break;
1669 case MINUS_EXPR:
1670 value = args->d1 - args->d2;
1671 break;
1673 case MULT_EXPR:
1674 value = args->d1 * args->d2;
1675 break;
1677 case RDIV_EXPR:
1678 #ifndef REAL_INFINITY
1679 if (args->d2 == 0)
1680 abort ();
1681 #endif
1683 value = args->d1 / args->d2;
1684 break;
1686 case MIN_EXPR:
1687 value = MIN (args->d1, args->d2);
1688 break;
1690 case MAX_EXPR:
1691 value = MAX (args->d1, args->d2);
1692 break;
1694 default:
1695 abort ();
1697 #endif /* no REAL_ARITHMETIC */
1699 args->t
1700 = build_real (args->type,
1701 real_value_truncate (TYPE_MODE (args->type), value));
1704 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1705 constant. We assume ARG1 and ARG2 have the same data type, or at least
1706 are the same kind of constant and the same machine mode.
1708 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1710 static tree
1711 const_binop (code, arg1, arg2, notrunc)
1712 enum tree_code code;
1713 register tree arg1, arg2;
1714 int notrunc;
1716 STRIP_NOPS (arg1);
1717 STRIP_NOPS (arg2);
1719 if (TREE_CODE (arg1) == INTEGER_CST)
1720 return int_const_binop (code, arg1, arg2, notrunc, 0);
1722 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1723 if (TREE_CODE (arg1) == REAL_CST)
1725 REAL_VALUE_TYPE d1;
1726 REAL_VALUE_TYPE d2;
1727 int overflow = 0;
1728 tree t;
1729 struct cb_args args;
1731 d1 = TREE_REAL_CST (arg1);
1732 d2 = TREE_REAL_CST (arg2);
1734 /* If either operand is a NaN, just return it. Otherwise, set up
1735 for floating-point trap; we return an overflow. */
1736 if (REAL_VALUE_ISNAN (d1))
1737 return arg1;
1738 else if (REAL_VALUE_ISNAN (d2))
1739 return arg2;
1741 /* Setup input for const_binop_1() */
1742 args.type = TREE_TYPE (arg1);
1743 args.d1 = d1;
1744 args.d2 = d2;
1745 args.code = code;
1747 if (do_float_handler (const_binop_1, (PTR) &args))
1748 /* Receive output from const_binop_1. */
1749 t = args.t;
1750 else
1752 /* We got an exception from const_binop_1. */
1753 t = copy_node (arg1);
1754 overflow = 1;
1757 TREE_OVERFLOW (t)
1758 = (force_fit_type (t, overflow)
1759 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1760 TREE_CONSTANT_OVERFLOW (t)
1761 = TREE_OVERFLOW (t)
1762 | TREE_CONSTANT_OVERFLOW (arg1)
1763 | TREE_CONSTANT_OVERFLOW (arg2);
1764 return t;
1766 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1767 if (TREE_CODE (arg1) == COMPLEX_CST)
1769 register tree type = TREE_TYPE (arg1);
1770 register tree r1 = TREE_REALPART (arg1);
1771 register tree i1 = TREE_IMAGPART (arg1);
1772 register tree r2 = TREE_REALPART (arg2);
1773 register tree i2 = TREE_IMAGPART (arg2);
1774 register tree t;
1776 switch (code)
1778 case PLUS_EXPR:
1779 t = build_complex (type,
1780 const_binop (PLUS_EXPR, r1, r2, notrunc),
1781 const_binop (PLUS_EXPR, i1, i2, notrunc));
1782 break;
1784 case MINUS_EXPR:
1785 t = build_complex (type,
1786 const_binop (MINUS_EXPR, r1, r2, notrunc),
1787 const_binop (MINUS_EXPR, i1, i2, notrunc));
1788 break;
1790 case MULT_EXPR:
1791 t = build_complex (type,
1792 const_binop (MINUS_EXPR,
1793 const_binop (MULT_EXPR,
1794 r1, r2, notrunc),
1795 const_binop (MULT_EXPR,
1796 i1, i2, notrunc),
1797 notrunc),
1798 const_binop (PLUS_EXPR,
1799 const_binop (MULT_EXPR,
1800 r1, i2, notrunc),
1801 const_binop (MULT_EXPR,
1802 i1, r2, notrunc),
1803 notrunc));
1804 break;
1806 case RDIV_EXPR:
1808 register tree magsquared
1809 = const_binop (PLUS_EXPR,
1810 const_binop (MULT_EXPR, r2, r2, notrunc),
1811 const_binop (MULT_EXPR, i2, i2, notrunc),
1812 notrunc);
1814 t = build_complex (type,
1815 const_binop
1816 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1817 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1818 const_binop (PLUS_EXPR,
1819 const_binop (MULT_EXPR, r1, r2,
1820 notrunc),
1821 const_binop (MULT_EXPR, i1, i2,
1822 notrunc),
1823 notrunc),
1824 magsquared, notrunc),
1825 const_binop
1826 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1827 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1828 const_binop (MINUS_EXPR,
1829 const_binop (MULT_EXPR, i1, r2,
1830 notrunc),
1831 const_binop (MULT_EXPR, r1, i2,
1832 notrunc),
1833 notrunc),
1834 magsquared, notrunc));
1836 break;
1838 default:
1839 abort ();
1841 return t;
1843 return 0;
1846 /* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
1847 bits are given by NUMBER and of the sizetype represented by KIND. */
1849 tree
1850 size_int_wide (number, kind)
1851 HOST_WIDE_INT number;
1852 enum size_type_kind kind;
1854 return size_int_type_wide (number, sizetype_tab[(int) kind]);
1857 /* Likewise, but the desired type is specified explicitly. */
1859 tree
1860 size_int_type_wide (number, type)
1861 HOST_WIDE_INT number;
1862 tree type;
1864 /* Type-size nodes already made for small sizes. */
1865 static tree size_table[2048 + 1];
1866 static int init_p = 0;
1867 tree t;
1869 if (! init_p)
1871 ggc_add_tree_root ((tree *) size_table,
1872 sizeof size_table / sizeof (tree));
1873 init_p = 1;
1876 /* If this is a positive number that fits in the table we use to hold
1877 cached entries, see if it is already in the table and put it there
1878 if not. */
1879 if (number >= 0 && number < (int) ARRAY_SIZE (size_table))
1881 if (size_table[number] != 0)
1882 for (t = size_table[number]; t != 0; t = TREE_CHAIN (t))
1883 if (TREE_TYPE (t) == type)
1884 return t;
1886 t = build_int_2 (number, 0);
1887 TREE_TYPE (t) = type;
1888 TREE_CHAIN (t) = size_table[number];
1889 size_table[number] = t;
1891 return t;
1894 t = build_int_2 (number, number < 0 ? -1 : 0);
1895 TREE_TYPE (t) = type;
1896 TREE_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (t) = force_fit_type (t, 0);
1897 return t;
1900 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1901 is a tree code. The type of the result is taken from the operands.
1902 Both must be the same type integer type and it must be a size type.
1903 If the operands are constant, so is the result. */
1905 tree
1906 size_binop (code, arg0, arg1)
1907 enum tree_code code;
1908 tree arg0, arg1;
1910 tree type = TREE_TYPE (arg0);
1912 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1913 || type != TREE_TYPE (arg1))
1914 abort ();
1916 /* Handle the special case of two integer constants faster. */
1917 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1919 /* And some specific cases even faster than that. */
1920 if (code == PLUS_EXPR && integer_zerop (arg0))
1921 return arg1;
1922 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1923 && integer_zerop (arg1))
1924 return arg0;
1925 else if (code == MULT_EXPR && integer_onep (arg0))
1926 return arg1;
1928 /* Handle general case of two integer constants. */
1929 return int_const_binop (code, arg0, arg1, 0, 1);
1932 if (arg0 == error_mark_node || arg1 == error_mark_node)
1933 return error_mark_node;
1935 return fold (build (code, type, arg0, arg1));
1938 /* Given two values, either both of sizetype or both of bitsizetype,
1939 compute the difference between the two values. Return the value
1940 in signed type corresponding to the type of the operands. */
1942 tree
1943 size_diffop (arg0, arg1)
1944 tree arg0, arg1;
1946 tree type = TREE_TYPE (arg0);
1947 tree ctype;
1949 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1950 || type != TREE_TYPE (arg1))
1951 abort ();
1953 /* If the type is already signed, just do the simple thing. */
1954 if (! TREE_UNSIGNED (type))
1955 return size_binop (MINUS_EXPR, arg0, arg1);
1957 ctype = (type == bitsizetype || type == ubitsizetype
1958 ? sbitsizetype : ssizetype);
1960 /* If either operand is not a constant, do the conversions to the signed
1961 type and subtract. The hardware will do the right thing with any
1962 overflow in the subtraction. */
1963 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1964 return size_binop (MINUS_EXPR, convert (ctype, arg0),
1965 convert (ctype, arg1));
1967 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1968 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1969 overflow) and negate (which can't either). Special-case a result
1970 of zero while we're here. */
1971 if (tree_int_cst_equal (arg0, arg1))
1972 return convert (ctype, integer_zero_node);
1973 else if (tree_int_cst_lt (arg1, arg0))
1974 return convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1975 else
1976 return size_binop (MINUS_EXPR, convert (ctype, integer_zero_node),
1977 convert (ctype, size_binop (MINUS_EXPR, arg1, arg0)));
1980 /* This structure is used to communicate arguments to fold_convert_1. */
1981 struct fc_args
1983 tree arg1; /* Input: value to convert. */
1984 tree type; /* Input: type to convert value to. */
1985 tree t; /* Ouput: result of conversion. */
1988 /* Function to convert floating-point constants, protected by floating
1989 point exception handler. */
1991 static void
1992 fold_convert_1 (data)
1993 PTR data;
1995 struct fc_args *args = (struct fc_args *) data;
1997 args->t = build_real (args->type,
1998 real_value_truncate (TYPE_MODE (args->type),
1999 TREE_REAL_CST (args->arg1)));
2002 /* Given T, a tree representing type conversion of ARG1, a constant,
2003 return a constant tree representing the result of conversion. */
2005 static tree
2006 fold_convert (t, arg1)
2007 register tree t;
2008 register tree arg1;
2010 register tree type = TREE_TYPE (t);
2011 int overflow = 0;
2013 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2015 if (TREE_CODE (arg1) == INTEGER_CST)
2017 /* If we would build a constant wider than GCC supports,
2018 leave the conversion unfolded. */
2019 if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
2020 return t;
2022 /* If we are trying to make a sizetype for a small integer, use
2023 size_int to pick up cached types to reduce duplicate nodes. */
2024 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
2025 && !TREE_CONSTANT_OVERFLOW (arg1)
2026 && compare_tree_int (arg1, 10000) < 0)
2027 return size_int_type_wide (TREE_INT_CST_LOW (arg1), type);
2029 /* Given an integer constant, make new constant with new type,
2030 appropriately sign-extended or truncated. */
2031 t = build_int_2 (TREE_INT_CST_LOW (arg1),
2032 TREE_INT_CST_HIGH (arg1));
2033 TREE_TYPE (t) = type;
2034 /* Indicate an overflow if (1) ARG1 already overflowed,
2035 or (2) force_fit_type indicates an overflow.
2036 Tell force_fit_type that an overflow has already occurred
2037 if ARG1 is a too-large unsigned value and T is signed.
2038 But don't indicate an overflow if converting a pointer. */
2039 TREE_OVERFLOW (t)
2040 = ((force_fit_type (t,
2041 (TREE_INT_CST_HIGH (arg1) < 0
2042 && (TREE_UNSIGNED (type)
2043 < TREE_UNSIGNED (TREE_TYPE (arg1)))))
2044 && ! POINTER_TYPE_P (TREE_TYPE (arg1)))
2045 || TREE_OVERFLOW (arg1));
2046 TREE_CONSTANT_OVERFLOW (t)
2047 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2049 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2050 else if (TREE_CODE (arg1) == REAL_CST)
2052 /* Don't initialize these, use assignments.
2053 Initialized local aggregates don't work on old compilers. */
2054 REAL_VALUE_TYPE x;
2055 REAL_VALUE_TYPE l;
2056 REAL_VALUE_TYPE u;
2057 tree type1 = TREE_TYPE (arg1);
2058 int no_upper_bound;
2060 x = TREE_REAL_CST (arg1);
2061 l = real_value_from_int_cst (type1, TYPE_MIN_VALUE (type));
2063 no_upper_bound = (TYPE_MAX_VALUE (type) == NULL);
2064 if (!no_upper_bound)
2065 u = real_value_from_int_cst (type1, TYPE_MAX_VALUE (type));
2067 /* See if X will be in range after truncation towards 0.
2068 To compensate for truncation, move the bounds away from 0,
2069 but reject if X exactly equals the adjusted bounds. */
2070 #ifdef REAL_ARITHMETIC
2071 REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
2072 if (!no_upper_bound)
2073 REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
2074 #else
2075 l--;
2076 if (!no_upper_bound)
2077 u++;
2078 #endif
2079 /* If X is a NaN, use zero instead and show we have an overflow.
2080 Otherwise, range check. */
2081 if (REAL_VALUE_ISNAN (x))
2082 overflow = 1, x = dconst0;
2083 else if (! (REAL_VALUES_LESS (l, x)
2084 && !no_upper_bound
2085 && REAL_VALUES_LESS (x, u)))
2086 overflow = 1;
2088 #ifndef REAL_ARITHMETIC
2090 HOST_WIDE_INT low, high;
2091 HOST_WIDE_INT half_word
2092 = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
2094 if (x < 0)
2095 x = -x;
2097 high = (HOST_WIDE_INT) (x / half_word / half_word);
2098 x -= (REAL_VALUE_TYPE) high * half_word * half_word;
2099 if (x >= (REAL_VALUE_TYPE) half_word * half_word / 2)
2101 low = x - (REAL_VALUE_TYPE) half_word * half_word / 2;
2102 low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1);
2104 else
2105 low = (HOST_WIDE_INT) x;
2106 if (TREE_REAL_CST (arg1) < 0)
2107 neg_double (low, high, &low, &high);
2108 t = build_int_2 (low, high);
2110 #else
2112 HOST_WIDE_INT low, high;
2113 REAL_VALUE_TO_INT (&low, &high, x);
2114 t = build_int_2 (low, high);
2116 #endif
2117 TREE_TYPE (t) = type;
2118 TREE_OVERFLOW (t)
2119 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
2120 TREE_CONSTANT_OVERFLOW (t)
2121 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2123 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2124 TREE_TYPE (t) = type;
2126 else if (TREE_CODE (type) == REAL_TYPE)
2128 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2129 if (TREE_CODE (arg1) == INTEGER_CST)
2130 return build_real_from_int_cst (type, arg1);
2131 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2132 if (TREE_CODE (arg1) == REAL_CST)
2134 struct fc_args args;
2136 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
2138 t = arg1;
2139 TREE_TYPE (arg1) = type;
2140 return t;
2143 /* Setup input for fold_convert_1() */
2144 args.arg1 = arg1;
2145 args.type = type;
2147 if (do_float_handler (fold_convert_1, (PTR) &args))
2149 /* Receive output from fold_convert_1() */
2150 t = args.t;
2152 else
2154 /* We got an exception from fold_convert_1() */
2155 overflow = 1;
2156 t = copy_node (arg1);
2159 TREE_OVERFLOW (t)
2160 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
2161 TREE_CONSTANT_OVERFLOW (t)
2162 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2163 return t;
2166 TREE_CONSTANT (t) = 1;
2167 return t;
2170 /* Return an expr equal to X but certainly not valid as an lvalue. */
2172 tree
2173 non_lvalue (x)
2174 tree x;
2176 tree result;
2178 /* These things are certainly not lvalues. */
2179 if (TREE_CODE (x) == NON_LVALUE_EXPR
2180 || TREE_CODE (x) == INTEGER_CST
2181 || TREE_CODE (x) == REAL_CST
2182 || TREE_CODE (x) == STRING_CST
2183 || TREE_CODE (x) == ADDR_EXPR)
2184 return x;
2186 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2187 TREE_CONSTANT (result) = TREE_CONSTANT (x);
2188 return result;
2191 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2192 Zero means allow extended lvalues. */
2194 int pedantic_lvalues;
2196 /* When pedantic, return an expr equal to X but certainly not valid as a
2197 pedantic lvalue. Otherwise, return X. */
2199 tree
2200 pedantic_non_lvalue (x)
2201 tree x;
2203 if (pedantic_lvalues)
2204 return non_lvalue (x);
2205 else
2206 return x;
2209 /* Given a tree comparison code, return the code that is the logical inverse
2210 of the given code. It is not safe to do this for floating-point
2211 comparisons, except for NE_EXPR and EQ_EXPR. */
2213 static enum tree_code
2214 invert_tree_comparison (code)
2215 enum tree_code code;
2217 switch (code)
2219 case EQ_EXPR:
2220 return NE_EXPR;
2221 case NE_EXPR:
2222 return EQ_EXPR;
2223 case GT_EXPR:
2224 return LE_EXPR;
2225 case GE_EXPR:
2226 return LT_EXPR;
2227 case LT_EXPR:
2228 return GE_EXPR;
2229 case LE_EXPR:
2230 return GT_EXPR;
2231 default:
2232 abort ();
2236 /* Similar, but return the comparison that results if the operands are
2237 swapped. This is safe for floating-point. */
2239 static enum tree_code
2240 swap_tree_comparison (code)
2241 enum tree_code code;
2243 switch (code)
2245 case EQ_EXPR:
2246 case NE_EXPR:
2247 return code;
2248 case GT_EXPR:
2249 return LT_EXPR;
2250 case GE_EXPR:
2251 return LE_EXPR;
2252 case LT_EXPR:
2253 return GT_EXPR;
2254 case LE_EXPR:
2255 return GE_EXPR;
2256 default:
2257 abort ();
2261 /* Return nonzero if CODE is a tree code that represents a truth value. */
2263 static int
2264 truth_value_p (code)
2265 enum tree_code code;
2267 return (TREE_CODE_CLASS (code) == '<'
2268 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2269 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2270 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2273 /* Return nonzero if two operands are necessarily equal.
2274 If ONLY_CONST is non-zero, only return non-zero for constants.
2275 This function tests whether the operands are indistinguishable;
2276 it does not test whether they are equal using C's == operation.
2277 The distinction is important for IEEE floating point, because
2278 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2279 (2) two NaNs may be indistinguishable, but NaN!=NaN. */
2282 operand_equal_p (arg0, arg1, only_const)
2283 tree arg0, arg1;
2284 int only_const;
2286 /* If both types don't have the same signedness, then we can't consider
2287 them equal. We must check this before the STRIP_NOPS calls
2288 because they may change the signedness of the arguments. */
2289 if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
2290 return 0;
2292 STRIP_NOPS (arg0);
2293 STRIP_NOPS (arg1);
2295 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2296 /* This is needed for conversions and for COMPONENT_REF.
2297 Might as well play it safe and always test this. */
2298 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2299 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2300 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2301 return 0;
2303 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2304 We don't care about side effects in that case because the SAVE_EXPR
2305 takes care of that for us. In all other cases, two expressions are
2306 equal if they have no side effects. If we have two identical
2307 expressions with side effects that should be treated the same due
2308 to the only side effects being identical SAVE_EXPR's, that will
2309 be detected in the recursive calls below. */
2310 if (arg0 == arg1 && ! only_const
2311 && (TREE_CODE (arg0) == SAVE_EXPR
2312 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2313 return 1;
2315 /* Next handle constant cases, those for which we can return 1 even
2316 if ONLY_CONST is set. */
2317 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2318 switch (TREE_CODE (arg0))
2320 case INTEGER_CST:
2321 return (! TREE_CONSTANT_OVERFLOW (arg0)
2322 && ! TREE_CONSTANT_OVERFLOW (arg1)
2323 && tree_int_cst_equal (arg0, arg1));
2325 case REAL_CST:
2326 return (! TREE_CONSTANT_OVERFLOW (arg0)
2327 && ! TREE_CONSTANT_OVERFLOW (arg1)
2328 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2329 TREE_REAL_CST (arg1)));
2331 case COMPLEX_CST:
2332 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2333 only_const)
2334 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2335 only_const));
2337 case STRING_CST:
2338 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2339 && ! memcmp (TREE_STRING_POINTER (arg0),
2340 TREE_STRING_POINTER (arg1),
2341 TREE_STRING_LENGTH (arg0)));
2343 case ADDR_EXPR:
2344 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2346 default:
2347 break;
2350 if (only_const)
2351 return 0;
2353 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2355 case '1':
2356 /* Two conversions are equal only if signedness and modes match. */
2357 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
2358 && (TREE_UNSIGNED (TREE_TYPE (arg0))
2359 != TREE_UNSIGNED (TREE_TYPE (arg1))))
2360 return 0;
2362 return operand_equal_p (TREE_OPERAND (arg0, 0),
2363 TREE_OPERAND (arg1, 0), 0);
2365 case '<':
2366 case '2':
2367 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)
2368 && operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1),
2370 return 1;
2372 /* For commutative ops, allow the other order. */
2373 return ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MULT_EXPR
2374 || TREE_CODE (arg0) == MIN_EXPR || TREE_CODE (arg0) == MAX_EXPR
2375 || TREE_CODE (arg0) == BIT_IOR_EXPR
2376 || TREE_CODE (arg0) == BIT_XOR_EXPR
2377 || TREE_CODE (arg0) == BIT_AND_EXPR
2378 || TREE_CODE (arg0) == NE_EXPR || TREE_CODE (arg0) == EQ_EXPR)
2379 && operand_equal_p (TREE_OPERAND (arg0, 0),
2380 TREE_OPERAND (arg1, 1), 0)
2381 && operand_equal_p (TREE_OPERAND (arg0, 1),
2382 TREE_OPERAND (arg1, 0), 0));
2384 case 'r':
2385 /* If either of the pointer (or reference) expressions we are dereferencing
2386 contain a side effect, these cannot be equal. */
2387 if (TREE_SIDE_EFFECTS (arg0)
2388 || TREE_SIDE_EFFECTS (arg1))
2389 return 0;
2391 switch (TREE_CODE (arg0))
2393 case INDIRECT_REF:
2394 return operand_equal_p (TREE_OPERAND (arg0, 0),
2395 TREE_OPERAND (arg1, 0), 0);
2397 case COMPONENT_REF:
2398 case ARRAY_REF:
2399 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2400 TREE_OPERAND (arg1, 0), 0)
2401 && operand_equal_p (TREE_OPERAND (arg0, 1),
2402 TREE_OPERAND (arg1, 1), 0));
2404 case BIT_FIELD_REF:
2405 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2406 TREE_OPERAND (arg1, 0), 0)
2407 && operand_equal_p (TREE_OPERAND (arg0, 1),
2408 TREE_OPERAND (arg1, 1), 0)
2409 && operand_equal_p (TREE_OPERAND (arg0, 2),
2410 TREE_OPERAND (arg1, 2), 0));
2411 default:
2412 return 0;
2415 case 'e':
2416 if (TREE_CODE (arg0) == RTL_EXPR)
2417 return rtx_equal_p (RTL_EXPR_RTL (arg0), RTL_EXPR_RTL (arg1));
2418 return 0;
2420 default:
2421 return 0;
2425 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2426 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2428 When in doubt, return 0. */
2430 static int
2431 operand_equal_for_comparison_p (arg0, arg1, other)
2432 tree arg0, arg1;
2433 tree other;
2435 int unsignedp1, unsignedpo;
2436 tree primarg0, primarg1, primother;
2437 unsigned int correct_width;
2439 if (operand_equal_p (arg0, arg1, 0))
2440 return 1;
2442 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2443 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2444 return 0;
2446 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2447 and see if the inner values are the same. This removes any
2448 signedness comparison, which doesn't matter here. */
2449 primarg0 = arg0, primarg1 = arg1;
2450 STRIP_NOPS (primarg0);
2451 STRIP_NOPS (primarg1);
2452 if (operand_equal_p (primarg0, primarg1, 0))
2453 return 1;
2455 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2456 actual comparison operand, ARG0.
2458 First throw away any conversions to wider types
2459 already present in the operands. */
2461 primarg1 = get_narrower (arg1, &unsignedp1);
2462 primother = get_narrower (other, &unsignedpo);
2464 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2465 if (unsignedp1 == unsignedpo
2466 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2467 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2469 tree type = TREE_TYPE (arg0);
2471 /* Make sure shorter operand is extended the right way
2472 to match the longer operand. */
2473 primarg1 = convert (signed_or_unsigned_type (unsignedp1,
2474 TREE_TYPE (primarg1)),
2475 primarg1);
2477 if (operand_equal_p (arg0, convert (type, primarg1), 0))
2478 return 1;
2481 return 0;
2484 /* See if ARG is an expression that is either a comparison or is performing
2485 arithmetic on comparisons. The comparisons must only be comparing
2486 two different values, which will be stored in *CVAL1 and *CVAL2; if
2487 they are non-zero it means that some operands have already been found.
2488 No variables may be used anywhere else in the expression except in the
2489 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2490 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2492 If this is true, return 1. Otherwise, return zero. */
2494 static int
2495 twoval_comparison_p (arg, cval1, cval2, save_p)
2496 tree arg;
2497 tree *cval1, *cval2;
2498 int *save_p;
2500 enum tree_code code = TREE_CODE (arg);
2501 char class = TREE_CODE_CLASS (code);
2503 /* We can handle some of the 'e' cases here. */
2504 if (class == 'e' && code == TRUTH_NOT_EXPR)
2505 class = '1';
2506 else if (class == 'e'
2507 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2508 || code == COMPOUND_EXPR))
2509 class = '2';
2511 else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0
2512 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2514 /* If we've already found a CVAL1 or CVAL2, this expression is
2515 two complex to handle. */
2516 if (*cval1 || *cval2)
2517 return 0;
2519 class = '1';
2520 *save_p = 1;
2523 switch (class)
2525 case '1':
2526 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2528 case '2':
2529 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2530 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2531 cval1, cval2, save_p));
2533 case 'c':
2534 return 1;
2536 case 'e':
2537 if (code == COND_EXPR)
2538 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2539 cval1, cval2, save_p)
2540 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2541 cval1, cval2, save_p)
2542 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2543 cval1, cval2, save_p));
2544 return 0;
2546 case '<':
2547 /* First see if we can handle the first operand, then the second. For
2548 the second operand, we know *CVAL1 can't be zero. It must be that
2549 one side of the comparison is each of the values; test for the
2550 case where this isn't true by failing if the two operands
2551 are the same. */
2553 if (operand_equal_p (TREE_OPERAND (arg, 0),
2554 TREE_OPERAND (arg, 1), 0))
2555 return 0;
2557 if (*cval1 == 0)
2558 *cval1 = TREE_OPERAND (arg, 0);
2559 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2561 else if (*cval2 == 0)
2562 *cval2 = TREE_OPERAND (arg, 0);
2563 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2565 else
2566 return 0;
2568 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2570 else if (*cval2 == 0)
2571 *cval2 = TREE_OPERAND (arg, 1);
2572 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2574 else
2575 return 0;
2577 return 1;
2579 default:
2580 return 0;
2584 /* ARG is a tree that is known to contain just arithmetic operations and
2585 comparisons. Evaluate the operations in the tree substituting NEW0 for
2586 any occurrence of OLD0 as an operand of a comparison and likewise for
2587 NEW1 and OLD1. */
2589 static tree
2590 eval_subst (arg, old0, new0, old1, new1)
2591 tree arg;
2592 tree old0, new0, old1, new1;
2594 tree type = TREE_TYPE (arg);
2595 enum tree_code code = TREE_CODE (arg);
2596 char class = TREE_CODE_CLASS (code);
2598 /* We can handle some of the 'e' cases here. */
2599 if (class == 'e' && code == TRUTH_NOT_EXPR)
2600 class = '1';
2601 else if (class == 'e'
2602 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2603 class = '2';
2605 switch (class)
2607 case '1':
2608 return fold (build1 (code, type,
2609 eval_subst (TREE_OPERAND (arg, 0),
2610 old0, new0, old1, new1)));
2612 case '2':
2613 return fold (build (code, type,
2614 eval_subst (TREE_OPERAND (arg, 0),
2615 old0, new0, old1, new1),
2616 eval_subst (TREE_OPERAND (arg, 1),
2617 old0, new0, old1, new1)));
2619 case 'e':
2620 switch (code)
2622 case SAVE_EXPR:
2623 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2625 case COMPOUND_EXPR:
2626 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2628 case COND_EXPR:
2629 return fold (build (code, type,
2630 eval_subst (TREE_OPERAND (arg, 0),
2631 old0, new0, old1, new1),
2632 eval_subst (TREE_OPERAND (arg, 1),
2633 old0, new0, old1, new1),
2634 eval_subst (TREE_OPERAND (arg, 2),
2635 old0, new0, old1, new1)));
2636 default:
2637 break;
2639 /* fall through - ??? */
2641 case '<':
2643 tree arg0 = TREE_OPERAND (arg, 0);
2644 tree arg1 = TREE_OPERAND (arg, 1);
2646 /* We need to check both for exact equality and tree equality. The
2647 former will be true if the operand has a side-effect. In that
2648 case, we know the operand occurred exactly once. */
2650 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2651 arg0 = new0;
2652 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2653 arg0 = new1;
2655 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2656 arg1 = new0;
2657 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2658 arg1 = new1;
2660 return fold (build (code, type, arg0, arg1));
2663 default:
2664 return arg;
2668 /* Return a tree for the case when the result of an expression is RESULT
2669 converted to TYPE and OMITTED was previously an operand of the expression
2670 but is now not needed (e.g., we folded OMITTED * 0).
2672 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2673 the conversion of RESULT to TYPE. */
2675 static tree
2676 omit_one_operand (type, result, omitted)
2677 tree type, result, omitted;
2679 tree t = convert (type, result);
2681 if (TREE_SIDE_EFFECTS (omitted))
2682 return build (COMPOUND_EXPR, type, omitted, t);
2684 return non_lvalue (t);
2687 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2689 static tree
2690 pedantic_omit_one_operand (type, result, omitted)
2691 tree type, result, omitted;
2693 tree t = convert (type, result);
2695 if (TREE_SIDE_EFFECTS (omitted))
2696 return build (COMPOUND_EXPR, type, omitted, t);
2698 return pedantic_non_lvalue (t);
2701 /* Return a simplified tree node for the truth-negation of ARG. This
2702 never alters ARG itself. We assume that ARG is an operation that
2703 returns a truth value (0 or 1). */
2705 tree
2706 invert_truthvalue (arg)
2707 tree arg;
2709 tree type = TREE_TYPE (arg);
2710 enum tree_code code = TREE_CODE (arg);
2712 if (code == ERROR_MARK)
2713 return arg;
2715 /* If this is a comparison, we can simply invert it, except for
2716 floating-point non-equality comparisons, in which case we just
2717 enclose a TRUTH_NOT_EXPR around what we have. */
2719 if (TREE_CODE_CLASS (code) == '<')
2721 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
2722 && !flag_fast_math && code != NE_EXPR && code != EQ_EXPR)
2723 return build1 (TRUTH_NOT_EXPR, type, arg);
2724 else
2725 return build (invert_tree_comparison (code), type,
2726 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2729 switch (code)
2731 case INTEGER_CST:
2732 return convert (type, build_int_2 (integer_zerop (arg), 0));
2734 case TRUTH_AND_EXPR:
2735 return build (TRUTH_OR_EXPR, type,
2736 invert_truthvalue (TREE_OPERAND (arg, 0)),
2737 invert_truthvalue (TREE_OPERAND (arg, 1)));
2739 case TRUTH_OR_EXPR:
2740 return build (TRUTH_AND_EXPR, type,
2741 invert_truthvalue (TREE_OPERAND (arg, 0)),
2742 invert_truthvalue (TREE_OPERAND (arg, 1)));
2744 case TRUTH_XOR_EXPR:
2745 /* Here we can invert either operand. We invert the first operand
2746 unless the second operand is a TRUTH_NOT_EXPR in which case our
2747 result is the XOR of the first operand with the inside of the
2748 negation of the second operand. */
2750 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2751 return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2752 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2753 else
2754 return build (TRUTH_XOR_EXPR, type,
2755 invert_truthvalue (TREE_OPERAND (arg, 0)),
2756 TREE_OPERAND (arg, 1));
2758 case TRUTH_ANDIF_EXPR:
2759 return build (TRUTH_ORIF_EXPR, type,
2760 invert_truthvalue (TREE_OPERAND (arg, 0)),
2761 invert_truthvalue (TREE_OPERAND (arg, 1)));
2763 case TRUTH_ORIF_EXPR:
2764 return build (TRUTH_ANDIF_EXPR, type,
2765 invert_truthvalue (TREE_OPERAND (arg, 0)),
2766 invert_truthvalue (TREE_OPERAND (arg, 1)));
2768 case TRUTH_NOT_EXPR:
2769 return TREE_OPERAND (arg, 0);
2771 case COND_EXPR:
2772 return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2773 invert_truthvalue (TREE_OPERAND (arg, 1)),
2774 invert_truthvalue (TREE_OPERAND (arg, 2)));
2776 case COMPOUND_EXPR:
2777 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2778 invert_truthvalue (TREE_OPERAND (arg, 1)));
2780 case WITH_RECORD_EXPR:
2781 return build (WITH_RECORD_EXPR, type,
2782 invert_truthvalue (TREE_OPERAND (arg, 0)),
2783 TREE_OPERAND (arg, 1));
2785 case NON_LVALUE_EXPR:
2786 return invert_truthvalue (TREE_OPERAND (arg, 0));
2788 case NOP_EXPR:
2789 case CONVERT_EXPR:
2790 case FLOAT_EXPR:
2791 return build1 (TREE_CODE (arg), type,
2792 invert_truthvalue (TREE_OPERAND (arg, 0)));
2794 case BIT_AND_EXPR:
2795 if (!integer_onep (TREE_OPERAND (arg, 1)))
2796 break;
2797 return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
2799 case SAVE_EXPR:
2800 return build1 (TRUTH_NOT_EXPR, type, arg);
2802 case CLEANUP_POINT_EXPR:
2803 return build1 (CLEANUP_POINT_EXPR, type,
2804 invert_truthvalue (TREE_OPERAND (arg, 0)));
2806 default:
2807 break;
2809 if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
2810 abort ();
2811 return build1 (TRUTH_NOT_EXPR, type, arg);
2814 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2815 operands are another bit-wise operation with a common input. If so,
2816 distribute the bit operations to save an operation and possibly two if
2817 constants are involved. For example, convert
2818 (A | B) & (A | C) into A | (B & C)
2819 Further simplification will occur if B and C are constants.
2821 If this optimization cannot be done, 0 will be returned. */
2823 static tree
2824 distribute_bit_expr (code, type, arg0, arg1)
2825 enum tree_code code;
2826 tree type;
2827 tree arg0, arg1;
2829 tree common;
2830 tree left, right;
2832 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2833 || TREE_CODE (arg0) == code
2834 || (TREE_CODE (arg0) != BIT_AND_EXPR
2835 && TREE_CODE (arg0) != BIT_IOR_EXPR))
2836 return 0;
2838 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2840 common = TREE_OPERAND (arg0, 0);
2841 left = TREE_OPERAND (arg0, 1);
2842 right = TREE_OPERAND (arg1, 1);
2844 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2846 common = TREE_OPERAND (arg0, 0);
2847 left = TREE_OPERAND (arg0, 1);
2848 right = TREE_OPERAND (arg1, 0);
2850 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2852 common = TREE_OPERAND (arg0, 1);
2853 left = TREE_OPERAND (arg0, 0);
2854 right = TREE_OPERAND (arg1, 1);
2856 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2858 common = TREE_OPERAND (arg0, 1);
2859 left = TREE_OPERAND (arg0, 0);
2860 right = TREE_OPERAND (arg1, 0);
2862 else
2863 return 0;
2865 return fold (build (TREE_CODE (arg0), type, common,
2866 fold (build (code, type, left, right))));
2869 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2870 starting at BITPOS. The field is unsigned if UNSIGNEDP is non-zero. */
2872 static tree
2873 make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
2874 tree inner;
2875 tree type;
2876 int bitsize, bitpos;
2877 int unsignedp;
2879 tree result = build (BIT_FIELD_REF, type, inner,
2880 size_int (bitsize), bitsize_int (bitpos));
2882 TREE_UNSIGNED (result) = unsignedp;
2884 return result;
2887 /* Optimize a bit-field compare.
2889 There are two cases: First is a compare against a constant and the
2890 second is a comparison of two items where the fields are at the same
2891 bit position relative to the start of a chunk (byte, halfword, word)
2892 large enough to contain it. In these cases we can avoid the shift
2893 implicit in bitfield extractions.
2895 For constants, we emit a compare of the shifted constant with the
2896 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2897 compared. For two fields at the same position, we do the ANDs with the
2898 similar mask and compare the result of the ANDs.
2900 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2901 COMPARE_TYPE is the type of the comparison, and LHS and RHS
2902 are the left and right operands of the comparison, respectively.
2904 If the optimization described above can be done, we return the resulting
2905 tree. Otherwise we return zero. */
2907 static tree
2908 optimize_bit_field_compare (code, compare_type, lhs, rhs)
2909 enum tree_code code;
2910 tree compare_type;
2911 tree lhs, rhs;
2913 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
2914 tree type = TREE_TYPE (lhs);
2915 tree signed_type, unsigned_type;
2916 int const_p = TREE_CODE (rhs) == INTEGER_CST;
2917 enum machine_mode lmode, rmode, nmode;
2918 int lunsignedp, runsignedp;
2919 int lvolatilep = 0, rvolatilep = 0;
2920 unsigned int alignment;
2921 tree linner, rinner = NULL_TREE;
2922 tree mask;
2923 tree offset;
2925 /* Get all the information about the extractions being done. If the bit size
2926 if the same as the size of the underlying object, we aren't doing an
2927 extraction at all and so can do nothing. We also don't want to
2928 do anything if the inner expression is a PLACEHOLDER_EXPR since we
2929 then will no longer be able to replace it. */
2930 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
2931 &lunsignedp, &lvolatilep, &alignment);
2932 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
2933 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
2934 return 0;
2936 if (!const_p)
2938 /* If this is not a constant, we can only do something if bit positions,
2939 sizes, and signedness are the same. */
2940 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
2941 &runsignedp, &rvolatilep, &alignment);
2943 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
2944 || lunsignedp != runsignedp || offset != 0
2945 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
2946 return 0;
2949 /* See if we can find a mode to refer to this field. We should be able to,
2950 but fail if we can't. */
2951 nmode = get_best_mode (lbitsize, lbitpos,
2952 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
2953 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
2954 TYPE_ALIGN (TREE_TYPE (rinner))),
2955 word_mode, lvolatilep || rvolatilep);
2956 if (nmode == VOIDmode)
2957 return 0;
2959 /* Set signed and unsigned types of the precision of this mode for the
2960 shifts below. */
2961 signed_type = type_for_mode (nmode, 0);
2962 unsigned_type = type_for_mode (nmode, 1);
2964 /* Compute the bit position and size for the new reference and our offset
2965 within it. If the new reference is the same size as the original, we
2966 won't optimize anything, so return zero. */
2967 nbitsize = GET_MODE_BITSIZE (nmode);
2968 nbitpos = lbitpos & ~ (nbitsize - 1);
2969 lbitpos -= nbitpos;
2970 if (nbitsize == lbitsize)
2971 return 0;
2973 if (BYTES_BIG_ENDIAN)
2974 lbitpos = nbitsize - lbitsize - lbitpos;
2976 /* Make the mask to be used against the extracted field. */
2977 mask = build_int_2 (~0, ~0);
2978 TREE_TYPE (mask) = unsigned_type;
2979 force_fit_type (mask, 0);
2980 mask = convert (unsigned_type, mask);
2981 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
2982 mask = const_binop (RSHIFT_EXPR, mask,
2983 size_int (nbitsize - lbitsize - lbitpos), 0);
2985 if (! const_p)
2986 /* If not comparing with constant, just rework the comparison
2987 and return. */
2988 return build (code, compare_type,
2989 build (BIT_AND_EXPR, unsigned_type,
2990 make_bit_field_ref (linner, unsigned_type,
2991 nbitsize, nbitpos, 1),
2992 mask),
2993 build (BIT_AND_EXPR, unsigned_type,
2994 make_bit_field_ref (rinner, unsigned_type,
2995 nbitsize, nbitpos, 1),
2996 mask));
2998 /* Otherwise, we are handling the constant case. See if the constant is too
2999 big for the field. Warn and return a tree of for 0 (false) if so. We do
3000 this not only for its own sake, but to avoid having to test for this
3001 error case below. If we didn't, we might generate wrong code.
3003 For unsigned fields, the constant shifted right by the field length should
3004 be all zero. For signed fields, the high-order bits should agree with
3005 the sign bit. */
3007 if (lunsignedp)
3009 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3010 convert (unsigned_type, rhs),
3011 size_int (lbitsize), 0)))
3013 warning ("comparison is always %d due to width of bitfield",
3014 code == NE_EXPR);
3015 return convert (compare_type,
3016 (code == NE_EXPR
3017 ? integer_one_node : integer_zero_node));
3020 else
3022 tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
3023 size_int (lbitsize - 1), 0);
3024 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3026 warning ("comparison is always %d due to width of bitfield",
3027 code == NE_EXPR);
3028 return convert (compare_type,
3029 (code == NE_EXPR
3030 ? integer_one_node : integer_zero_node));
3034 /* Single-bit compares should always be against zero. */
3035 if (lbitsize == 1 && ! integer_zerop (rhs))
3037 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3038 rhs = convert (type, integer_zero_node);
3041 /* Make a new bitfield reference, shift the constant over the
3042 appropriate number of bits and mask it with the computed mask
3043 (in case this was a signed field). If we changed it, make a new one. */
3044 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
3045 if (lvolatilep)
3047 TREE_SIDE_EFFECTS (lhs) = 1;
3048 TREE_THIS_VOLATILE (lhs) = 1;
3051 rhs = fold (const_binop (BIT_AND_EXPR,
3052 const_binop (LSHIFT_EXPR,
3053 convert (unsigned_type, rhs),
3054 size_int (lbitpos), 0),
3055 mask, 0));
3057 return build (code, compare_type,
3058 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
3059 rhs);
3062 /* Subroutine for fold_truthop: decode a field reference.
3064 If EXP is a comparison reference, we return the innermost reference.
3066 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3067 set to the starting bit number.
3069 If the innermost field can be completely contained in a mode-sized
3070 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3072 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3073 otherwise it is not changed.
3075 *PUNSIGNEDP is set to the signedness of the field.
3077 *PMASK is set to the mask used. This is either contained in a
3078 BIT_AND_EXPR or derived from the width of the field.
3080 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3082 Return 0 if this is not a component reference or is one that we can't
3083 do anything with. */
3085 static tree
3086 decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
3087 pvolatilep, pmask, pand_mask)
3088 tree exp;
3089 HOST_WIDE_INT *pbitsize, *pbitpos;
3090 enum machine_mode *pmode;
3091 int *punsignedp, *pvolatilep;
3092 tree *pmask;
3093 tree *pand_mask;
3095 tree and_mask = 0;
3096 tree mask, inner, offset;
3097 tree unsigned_type;
3098 unsigned int precision;
3099 unsigned int alignment;
3101 /* All the optimizations using this function assume integer fields.
3102 There are problems with FP fields since the type_for_size call
3103 below can fail for, e.g., XFmode. */
3104 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3105 return 0;
3107 STRIP_NOPS (exp);
3109 if (TREE_CODE (exp) == BIT_AND_EXPR)
3111 and_mask = TREE_OPERAND (exp, 1);
3112 exp = TREE_OPERAND (exp, 0);
3113 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3114 if (TREE_CODE (and_mask) != INTEGER_CST)
3115 return 0;
3118 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3119 punsignedp, pvolatilep, &alignment);
3120 if ((inner == exp && and_mask == 0)
3121 || *pbitsize < 0 || offset != 0
3122 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3123 return 0;
3125 /* Compute the mask to access the bitfield. */
3126 unsigned_type = type_for_size (*pbitsize, 1);
3127 precision = TYPE_PRECISION (unsigned_type);
3129 mask = build_int_2 (~0, ~0);
3130 TREE_TYPE (mask) = unsigned_type;
3131 force_fit_type (mask, 0);
3132 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3133 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3135 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3136 if (and_mask != 0)
3137 mask = fold (build (BIT_AND_EXPR, unsigned_type,
3138 convert (unsigned_type, and_mask), mask));
3140 *pmask = mask;
3141 *pand_mask = and_mask;
3142 return inner;
3145 /* Return non-zero if MASK represents a mask of SIZE ones in the low-order
3146 bit positions. */
3148 static int
3149 all_ones_mask_p (mask, size)
3150 tree mask;
3151 int size;
3153 tree type = TREE_TYPE (mask);
3154 unsigned int precision = TYPE_PRECISION (type);
3155 tree tmask;
3157 tmask = build_int_2 (~0, ~0);
3158 TREE_TYPE (tmask) = signed_type (type);
3159 force_fit_type (tmask, 0);
3160 return
3161 tree_int_cst_equal (mask,
3162 const_binop (RSHIFT_EXPR,
3163 const_binop (LSHIFT_EXPR, tmask,
3164 size_int (precision - size),
3166 size_int (precision - size), 0));
3169 /* Subroutine for fold_truthop: determine if an operand is simple enough
3170 to be evaluated unconditionally. */
3172 static int
3173 simple_operand_p (exp)
3174 tree exp;
3176 /* Strip any conversions that don't change the machine mode. */
3177 while ((TREE_CODE (exp) == NOP_EXPR
3178 || TREE_CODE (exp) == CONVERT_EXPR)
3179 && (TYPE_MODE (TREE_TYPE (exp))
3180 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
3181 exp = TREE_OPERAND (exp, 0);
3183 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
3184 || (DECL_P (exp)
3185 && ! TREE_ADDRESSABLE (exp)
3186 && ! TREE_THIS_VOLATILE (exp)
3187 && ! DECL_NONLOCAL (exp)
3188 /* Don't regard global variables as simple. They may be
3189 allocated in ways unknown to the compiler (shared memory,
3190 #pragma weak, etc). */
3191 && ! TREE_PUBLIC (exp)
3192 && ! DECL_EXTERNAL (exp)
3193 /* Loading a static variable is unduly expensive, but global
3194 registers aren't expensive. */
3195 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3198 /* The following functions are subroutines to fold_range_test and allow it to
3199 try to change a logical combination of comparisons into a range test.
3201 For example, both
3202 X == 2 || X == 3 || X == 4 || X == 5
3204 X >= 2 && X <= 5
3205 are converted to
3206 (unsigned) (X - 2) <= 3
3208 We describe each set of comparisons as being either inside or outside
3209 a range, using a variable named like IN_P, and then describe the
3210 range with a lower and upper bound. If one of the bounds is omitted,
3211 it represents either the highest or lowest value of the type.
3213 In the comments below, we represent a range by two numbers in brackets
3214 preceded by a "+" to designate being inside that range, or a "-" to
3215 designate being outside that range, so the condition can be inverted by
3216 flipping the prefix. An omitted bound is represented by a "-". For
3217 example, "- [-, 10]" means being outside the range starting at the lowest
3218 possible value and ending at 10, in other words, being greater than 10.
3219 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3220 always false.
3222 We set up things so that the missing bounds are handled in a consistent
3223 manner so neither a missing bound nor "true" and "false" need to be
3224 handled using a special case. */
3226 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3227 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3228 and UPPER1_P are nonzero if the respective argument is an upper bound
3229 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3230 must be specified for a comparison. ARG1 will be converted to ARG0's
3231 type if both are specified. */
3233 static tree
3234 range_binop (code, type, arg0, upper0_p, arg1, upper1_p)
3235 enum tree_code code;
3236 tree type;
3237 tree arg0, arg1;
3238 int upper0_p, upper1_p;
3240 tree tem;
3241 int result;
3242 int sgn0, sgn1;
3244 /* If neither arg represents infinity, do the normal operation.
3245 Else, if not a comparison, return infinity. Else handle the special
3246 comparison rules. Note that most of the cases below won't occur, but
3247 are handled for consistency. */
3249 if (arg0 != 0 && arg1 != 0)
3251 tem = fold (build (code, type != 0 ? type : TREE_TYPE (arg0),
3252 arg0, convert (TREE_TYPE (arg0), arg1)));
3253 STRIP_NOPS (tem);
3254 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3257 if (TREE_CODE_CLASS (code) != '<')
3258 return 0;
3260 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3261 for neither. In real maths, we cannot assume open ended ranges are
3262 the same. But, this is computer arithmetic, where numbers are finite.
3263 We can therefore make the transformation of any unbounded range with
3264 the value Z, Z being greater than any representable number. This permits
3265 us to treat unbounded ranges as equal. */
3266 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3267 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3268 switch (code)
3270 case EQ_EXPR:
3271 result = sgn0 == sgn1;
3272 break;
3273 case NE_EXPR:
3274 result = sgn0 != sgn1;
3275 break;
3276 case LT_EXPR:
3277 result = sgn0 < sgn1;
3278 break;
3279 case LE_EXPR:
3280 result = sgn0 <= sgn1;
3281 break;
3282 case GT_EXPR:
3283 result = sgn0 > sgn1;
3284 break;
3285 case GE_EXPR:
3286 result = sgn0 >= sgn1;
3287 break;
3288 default:
3289 abort ();
3292 return convert (type, result ? integer_one_node : integer_zero_node);
3295 /* Given EXP, a logical expression, set the range it is testing into
3296 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3297 actually being tested. *PLOW and *PHIGH will be made of the same type
3298 as the returned expression. If EXP is not a comparison, we will most
3299 likely not be returning a useful value and range. */
3301 static tree
3302 make_range (exp, pin_p, plow, phigh)
3303 tree exp;
3304 int *pin_p;
3305 tree *plow, *phigh;
3307 enum tree_code code;
3308 tree arg0 = NULL_TREE, arg1 = NULL_TREE, type = NULL_TREE;
3309 tree orig_type = NULL_TREE;
3310 int in_p, n_in_p;
3311 tree low, high, n_low, n_high;
3313 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3314 and see if we can refine the range. Some of the cases below may not
3315 happen, but it doesn't seem worth worrying about this. We "continue"
3316 the outer loop when we've changed something; otherwise we "break"
3317 the switch, which will "break" the while. */
3319 in_p = 0, low = high = convert (TREE_TYPE (exp), integer_zero_node);
3321 while (1)
3323 code = TREE_CODE (exp);
3325 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3327 arg0 = TREE_OPERAND (exp, 0);
3328 if (TREE_CODE_CLASS (code) == '<'
3329 || TREE_CODE_CLASS (code) == '1'
3330 || TREE_CODE_CLASS (code) == '2')
3331 type = TREE_TYPE (arg0);
3332 if (TREE_CODE_CLASS (code) == '2'
3333 || TREE_CODE_CLASS (code) == '<'
3334 || (TREE_CODE_CLASS (code) == 'e'
3335 && TREE_CODE_LENGTH (code) > 1))
3336 arg1 = TREE_OPERAND (exp, 1);
3339 /* Set ORIG_TYPE as soon as TYPE is non-null so that we do not
3340 lose a cast by accident. */
3341 if (type != NULL_TREE && orig_type == NULL_TREE)
3342 orig_type = type;
3344 switch (code)
3346 case TRUTH_NOT_EXPR:
3347 in_p = ! in_p, exp = arg0;
3348 continue;
3350 case EQ_EXPR: case NE_EXPR:
3351 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3352 /* We can only do something if the range is testing for zero
3353 and if the second operand is an integer constant. Note that
3354 saying something is "in" the range we make is done by
3355 complementing IN_P since it will set in the initial case of
3356 being not equal to zero; "out" is leaving it alone. */
3357 if (low == 0 || high == 0
3358 || ! integer_zerop (low) || ! integer_zerop (high)
3359 || TREE_CODE (arg1) != INTEGER_CST)
3360 break;
3362 switch (code)
3364 case NE_EXPR: /* - [c, c] */
3365 low = high = arg1;
3366 break;
3367 case EQ_EXPR: /* + [c, c] */
3368 in_p = ! in_p, low = high = arg1;
3369 break;
3370 case GT_EXPR: /* - [-, c] */
3371 low = 0, high = arg1;
3372 break;
3373 case GE_EXPR: /* + [c, -] */
3374 in_p = ! in_p, low = arg1, high = 0;
3375 break;
3376 case LT_EXPR: /* - [c, -] */
3377 low = arg1, high = 0;
3378 break;
3379 case LE_EXPR: /* + [-, c] */
3380 in_p = ! in_p, low = 0, high = arg1;
3381 break;
3382 default:
3383 abort ();
3386 exp = arg0;
3388 /* If this is an unsigned comparison, we also know that EXP is
3389 greater than or equal to zero. We base the range tests we make
3390 on that fact, so we record it here so we can parse existing
3391 range tests. */
3392 if (TREE_UNSIGNED (type) && (low == 0 || high == 0))
3394 if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
3395 1, convert (type, integer_zero_node),
3396 NULL_TREE))
3397 break;
3399 in_p = n_in_p, low = n_low, high = n_high;
3401 /* If the high bound is missing, but we
3402 have a low bound, reverse the range so
3403 it goes from zero to the low bound minus 1. */
3404 if (high == 0 && low)
3406 in_p = ! in_p;
3407 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3408 integer_one_node, 0);
3409 low = convert (type, integer_zero_node);
3412 continue;
3414 case NEGATE_EXPR:
3415 /* (-x) IN [a,b] -> x in [-b, -a] */
3416 n_low = range_binop (MINUS_EXPR, type,
3417 convert (type, integer_zero_node), 0, high, 1);
3418 n_high = range_binop (MINUS_EXPR, type,
3419 convert (type, integer_zero_node), 0, low, 0);
3420 low = n_low, high = n_high;
3421 exp = arg0;
3422 continue;
3424 case BIT_NOT_EXPR:
3425 /* ~ X -> -X - 1 */
3426 exp = build (MINUS_EXPR, type, negate_expr (arg0),
3427 convert (type, integer_one_node));
3428 continue;
3430 case PLUS_EXPR: case MINUS_EXPR:
3431 if (TREE_CODE (arg1) != INTEGER_CST)
3432 break;
3434 /* If EXP is signed, any overflow in the computation is undefined,
3435 so we don't worry about it so long as our computations on
3436 the bounds don't overflow. For unsigned, overflow is defined
3437 and this is exactly the right thing. */
3438 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3439 type, low, 0, arg1, 0);
3440 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3441 type, high, 1, arg1, 0);
3442 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3443 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3444 break;
3446 /* Check for an unsigned range which has wrapped around the maximum
3447 value thus making n_high < n_low, and normalize it. */
3448 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3450 low = range_binop (PLUS_EXPR, type, n_high, 0,
3451 integer_one_node, 0);
3452 high = range_binop (MINUS_EXPR, type, n_low, 0,
3453 integer_one_node, 0);
3455 /* If the range is of the form +/- [ x+1, x ], we won't
3456 be able to normalize it. But then, it represents the
3457 whole range or the empty set, so make it
3458 +/- [ -, - ]. */
3459 if (tree_int_cst_equal (n_low, low)
3460 && tree_int_cst_equal (n_high, high))
3461 low = high = 0;
3462 else
3463 in_p = ! in_p;
3465 else
3466 low = n_low, high = n_high;
3468 exp = arg0;
3469 continue;
3471 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
3472 if (TYPE_PRECISION (type) > TYPE_PRECISION (orig_type))
3473 break;
3475 if (! INTEGRAL_TYPE_P (type)
3476 || (low != 0 && ! int_fits_type_p (low, type))
3477 || (high != 0 && ! int_fits_type_p (high, type)))
3478 break;
3480 n_low = low, n_high = high;
3482 if (n_low != 0)
3483 n_low = convert (type, n_low);
3485 if (n_high != 0)
3486 n_high = convert (type, n_high);
3488 /* If we're converting from an unsigned to a signed type,
3489 we will be doing the comparison as unsigned. The tests above
3490 have already verified that LOW and HIGH are both positive.
3492 So we have to make sure that the original unsigned value will
3493 be interpreted as positive. */
3494 if (TREE_UNSIGNED (type) && ! TREE_UNSIGNED (TREE_TYPE (exp)))
3496 tree equiv_type = type_for_mode (TYPE_MODE (type), 1);
3497 tree high_positive;
3499 /* A range without an upper bound is, naturally, unbounded.
3500 Since convert would have cropped a very large value, use
3501 the max value for the destination type. */
3502 high_positive
3503 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3504 : TYPE_MAX_VALUE (type);
3506 high_positive = fold (build (RSHIFT_EXPR, type,
3507 convert (type, high_positive),
3508 convert (type, integer_one_node)));
3510 /* If the low bound is specified, "and" the range with the
3511 range for which the original unsigned value will be
3512 positive. */
3513 if (low != 0)
3515 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3516 1, n_low, n_high,
3517 1, convert (type, integer_zero_node),
3518 high_positive))
3519 break;
3521 in_p = (n_in_p == in_p);
3523 else
3525 /* Otherwise, "or" the range with the range of the input
3526 that will be interpreted as negative. */
3527 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3528 0, n_low, n_high,
3529 1, convert (type, integer_zero_node),
3530 high_positive))
3531 break;
3533 in_p = (in_p != n_in_p);
3537 exp = arg0;
3538 low = n_low, high = n_high;
3539 continue;
3541 default:
3542 break;
3545 break;
3548 /* If EXP is a constant, we can evaluate whether this is true or false. */
3549 if (TREE_CODE (exp) == INTEGER_CST)
3551 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3552 exp, 0, low, 0))
3553 && integer_onep (range_binop (LE_EXPR, integer_type_node,
3554 exp, 1, high, 1)));
3555 low = high = 0;
3556 exp = 0;
3559 *pin_p = in_p, *plow = low, *phigh = high;
3560 return exp;
3563 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3564 type, TYPE, return an expression to test if EXP is in (or out of, depending
3565 on IN_P) the range. */
3567 static tree
3568 build_range_check (type, exp, in_p, low, high)
3569 tree type;
3570 tree exp;
3571 int in_p;
3572 tree low, high;
3574 tree etype = TREE_TYPE (exp);
3575 tree utype, value;
3577 if (! in_p
3578 && (0 != (value = build_range_check (type, exp, 1, low, high))))
3579 return invert_truthvalue (value);
3581 else if (low == 0 && high == 0)
3582 return convert (type, integer_one_node);
3584 else if (low == 0)
3585 return fold (build (LE_EXPR, type, exp, high));
3587 else if (high == 0)
3588 return fold (build (GE_EXPR, type, exp, low));
3590 else if (operand_equal_p (low, high, 0))
3591 return fold (build (EQ_EXPR, type, exp, low));
3593 else if (TREE_UNSIGNED (etype) && integer_zerop (low))
3594 return build_range_check (type, exp, 1, 0, high);
3596 else if (integer_zerop (low))
3598 utype = unsigned_type (etype);
3599 return build_range_check (type, convert (utype, exp), 1, 0,
3600 convert (utype, high));
3603 else if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
3604 && ! TREE_OVERFLOW (value))
3605 return build_range_check (type,
3606 fold (build (MINUS_EXPR, etype, exp, low)),
3607 1, convert (etype, integer_zero_node), value);
3608 else
3609 return 0;
3612 /* Given two ranges, see if we can merge them into one. Return 1 if we
3613 can, 0 if we can't. Set the output range into the specified parameters. */
3615 static int
3616 merge_ranges (pin_p, plow, phigh, in0_p, low0, high0, in1_p, low1, high1)
3617 int *pin_p;
3618 tree *plow, *phigh;
3619 int in0_p, in1_p;
3620 tree low0, high0, low1, high1;
3622 int no_overlap;
3623 int subset;
3624 int temp;
3625 tree tem;
3626 int in_p;
3627 tree low, high;
3628 int lowequal = ((low0 == 0 && low1 == 0)
3629 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3630 low0, 0, low1, 0)));
3631 int highequal = ((high0 == 0 && high1 == 0)
3632 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3633 high0, 1, high1, 1)));
3635 /* Make range 0 be the range that starts first, or ends last if they
3636 start at the same value. Swap them if it isn't. */
3637 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3638 low0, 0, low1, 0))
3639 || (lowequal
3640 && integer_onep (range_binop (GT_EXPR, integer_type_node,
3641 high1, 1, high0, 1))))
3643 temp = in0_p, in0_p = in1_p, in1_p = temp;
3644 tem = low0, low0 = low1, low1 = tem;
3645 tem = high0, high0 = high1, high1 = tem;
3648 /* Now flag two cases, whether the ranges are disjoint or whether the
3649 second range is totally subsumed in the first. Note that the tests
3650 below are simplified by the ones above. */
3651 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3652 high0, 1, low1, 0));
3653 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3654 high1, 1, high0, 1));
3656 /* We now have four cases, depending on whether we are including or
3657 excluding the two ranges. */
3658 if (in0_p && in1_p)
3660 /* If they don't overlap, the result is false. If the second range
3661 is a subset it is the result. Otherwise, the range is from the start
3662 of the second to the end of the first. */
3663 if (no_overlap)
3664 in_p = 0, low = high = 0;
3665 else if (subset)
3666 in_p = 1, low = low1, high = high1;
3667 else
3668 in_p = 1, low = low1, high = high0;
3671 else if (in0_p && ! in1_p)
3673 /* If they don't overlap, the result is the first range. If they are
3674 equal, the result is false. If the second range is a subset of the
3675 first, and the ranges begin at the same place, we go from just after
3676 the end of the first range to the end of the second. If the second
3677 range is not a subset of the first, or if it is a subset and both
3678 ranges end at the same place, the range starts at the start of the
3679 first range and ends just before the second range.
3680 Otherwise, we can't describe this as a single range. */
3681 if (no_overlap)
3682 in_p = 1, low = low0, high = high0;
3683 else if (lowequal && highequal)
3684 in_p = 0, low = high = 0;
3685 else if (subset && lowequal)
3687 in_p = 1, high = high0;
3688 low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
3689 integer_one_node, 0);
3691 else if (! subset || highequal)
3693 in_p = 1, low = low0;
3694 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
3695 integer_one_node, 0);
3697 else
3698 return 0;
3701 else if (! in0_p && in1_p)
3703 /* If they don't overlap, the result is the second range. If the second
3704 is a subset of the first, the result is false. Otherwise,
3705 the range starts just after the first range and ends at the
3706 end of the second. */
3707 if (no_overlap)
3708 in_p = 1, low = low1, high = high1;
3709 else if (subset || highequal)
3710 in_p = 0, low = high = 0;
3711 else
3713 in_p = 1, high = high1;
3714 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3715 integer_one_node, 0);
3719 else
3721 /* The case where we are excluding both ranges. Here the complex case
3722 is if they don't overlap. In that case, the only time we have a
3723 range is if they are adjacent. If the second is a subset of the
3724 first, the result is the first. Otherwise, the range to exclude
3725 starts at the beginning of the first range and ends at the end of the
3726 second. */
3727 if (no_overlap)
3729 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3730 range_binop (PLUS_EXPR, NULL_TREE,
3731 high0, 1,
3732 integer_one_node, 1),
3733 1, low1, 0)))
3734 in_p = 0, low = low0, high = high1;
3735 else
3736 return 0;
3738 else if (subset)
3739 in_p = 0, low = low0, high = high0;
3740 else
3741 in_p = 0, low = low0, high = high1;
3744 *pin_p = in_p, *plow = low, *phigh = high;
3745 return 1;
3748 /* EXP is some logical combination of boolean tests. See if we can
3749 merge it into some range test. Return the new tree if so. */
3751 static tree
3752 fold_range_test (exp)
3753 tree exp;
3755 int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3756 || TREE_CODE (exp) == TRUTH_OR_EXPR);
3757 int in0_p, in1_p, in_p;
3758 tree low0, low1, low, high0, high1, high;
3759 tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
3760 tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
3761 tree tem;
3763 /* If this is an OR operation, invert both sides; we will invert
3764 again at the end. */
3765 if (or_op)
3766 in0_p = ! in0_p, in1_p = ! in1_p;
3768 /* If both expressions are the same, if we can merge the ranges, and we
3769 can build the range test, return it or it inverted. If one of the
3770 ranges is always true or always false, consider it to be the same
3771 expression as the other. */
3772 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
3773 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
3774 in1_p, low1, high1)
3775 && 0 != (tem = (build_range_check (TREE_TYPE (exp),
3776 lhs != 0 ? lhs
3777 : rhs != 0 ? rhs : integer_zero_node,
3778 in_p, low, high))))
3779 return or_op ? invert_truthvalue (tem) : tem;
3781 /* On machines where the branch cost is expensive, if this is a
3782 short-circuited branch and the underlying object on both sides
3783 is the same, make a non-short-circuit operation. */
3784 else if (BRANCH_COST >= 2
3785 && lhs != 0 && rhs != 0
3786 && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3787 || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
3788 && operand_equal_p (lhs, rhs, 0))
3790 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
3791 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
3792 which cases we can't do this. */
3793 if (simple_operand_p (lhs))
3794 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3795 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3796 TREE_TYPE (exp), TREE_OPERAND (exp, 0),
3797 TREE_OPERAND (exp, 1));
3799 else if (global_bindings_p () == 0
3800 && ! contains_placeholder_p (lhs))
3802 tree common = save_expr (lhs);
3804 if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
3805 or_op ? ! in0_p : in0_p,
3806 low0, high0))
3807 && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
3808 or_op ? ! in1_p : in1_p,
3809 low1, high1))))
3810 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3811 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3812 TREE_TYPE (exp), lhs, rhs);
3816 return 0;
3819 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
3820 bit value. Arrange things so the extra bits will be set to zero if and
3821 only if C is signed-extended to its full width. If MASK is nonzero,
3822 it is an INTEGER_CST that should be AND'ed with the extra bits. */
3824 static tree
3825 unextend (c, p, unsignedp, mask)
3826 tree c;
3827 int p;
3828 int unsignedp;
3829 tree mask;
3831 tree type = TREE_TYPE (c);
3832 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
3833 tree temp;
3835 if (p == modesize || unsignedp)
3836 return c;
3838 /* We work by getting just the sign bit into the low-order bit, then
3839 into the high-order bit, then sign-extend. We then XOR that value
3840 with C. */
3841 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
3842 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
3844 /* We must use a signed type in order to get an arithmetic right shift.
3845 However, we must also avoid introducing accidental overflows, so that
3846 a subsequent call to integer_zerop will work. Hence we must
3847 do the type conversion here. At this point, the constant is either
3848 zero or one, and the conversion to a signed type can never overflow.
3849 We could get an overflow if this conversion is done anywhere else. */
3850 if (TREE_UNSIGNED (type))
3851 temp = convert (signed_type (type), temp);
3853 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
3854 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
3855 if (mask != 0)
3856 temp = const_binop (BIT_AND_EXPR, temp, convert (TREE_TYPE (c), mask), 0);
3857 /* If necessary, convert the type back to match the type of C. */
3858 if (TREE_UNSIGNED (type))
3859 temp = convert (type, temp);
3861 return convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
3864 /* Find ways of folding logical expressions of LHS and RHS:
3865 Try to merge two comparisons to the same innermost item.
3866 Look for range tests like "ch >= '0' && ch <= '9'".
3867 Look for combinations of simple terms on machines with expensive branches
3868 and evaluate the RHS unconditionally.
3870 For example, if we have p->a == 2 && p->b == 4 and we can make an
3871 object large enough to span both A and B, we can do this with a comparison
3872 against the object ANDed with the a mask.
3874 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
3875 operations to do this with one comparison.
3877 We check for both normal comparisons and the BIT_AND_EXPRs made this by
3878 function and the one above.
3880 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
3881 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
3883 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
3884 two operands.
3886 We return the simplified tree or 0 if no optimization is possible. */
3888 static tree
3889 fold_truthop (code, truth_type, lhs, rhs)
3890 enum tree_code code;
3891 tree truth_type, lhs, rhs;
3893 /* If this is the "or" of two comparisons, we can do something if
3894 the comparisons are NE_EXPR. If this is the "and", we can do something
3895 if the comparisons are EQ_EXPR. I.e.,
3896 (a->b == 2 && a->c == 4) can become (a->new == NEW).
3898 WANTED_CODE is this operation code. For single bit fields, we can
3899 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
3900 comparison for one-bit fields. */
3902 enum tree_code wanted_code;
3903 enum tree_code lcode, rcode;
3904 tree ll_arg, lr_arg, rl_arg, rr_arg;
3905 tree ll_inner, lr_inner, rl_inner, rr_inner;
3906 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
3907 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
3908 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
3909 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
3910 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
3911 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
3912 enum machine_mode lnmode, rnmode;
3913 tree ll_mask, lr_mask, rl_mask, rr_mask;
3914 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
3915 tree l_const, r_const;
3916 tree lntype, rntype, result;
3917 int first_bit, end_bit;
3918 int volatilep;
3920 /* Start by getting the comparison codes. Fail if anything is volatile.
3921 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
3922 it were surrounded with a NE_EXPR. */
3924 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
3925 return 0;
3927 lcode = TREE_CODE (lhs);
3928 rcode = TREE_CODE (rhs);
3930 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
3931 lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
3933 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
3934 rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
3936 if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
3937 return 0;
3939 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
3940 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
3942 ll_arg = TREE_OPERAND (lhs, 0);
3943 lr_arg = TREE_OPERAND (lhs, 1);
3944 rl_arg = TREE_OPERAND (rhs, 0);
3945 rr_arg = TREE_OPERAND (rhs, 1);
3947 /* If the RHS can be evaluated unconditionally and its operands are
3948 simple, it wins to evaluate the RHS unconditionally on machines
3949 with expensive branches. In this case, this isn't a comparison
3950 that can be merged. Avoid doing this if the RHS is a floating-point
3951 comparison since those can trap. */
3953 if (BRANCH_COST >= 2
3954 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
3955 && simple_operand_p (rl_arg)
3956 && simple_operand_p (rr_arg))
3957 return build (code, truth_type, lhs, rhs);
3959 /* See if the comparisons can be merged. Then get all the parameters for
3960 each side. */
3962 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
3963 || (rcode != EQ_EXPR && rcode != NE_EXPR))
3964 return 0;
3966 volatilep = 0;
3967 ll_inner = decode_field_reference (ll_arg,
3968 &ll_bitsize, &ll_bitpos, &ll_mode,
3969 &ll_unsignedp, &volatilep, &ll_mask,
3970 &ll_and_mask);
3971 lr_inner = decode_field_reference (lr_arg,
3972 &lr_bitsize, &lr_bitpos, &lr_mode,
3973 &lr_unsignedp, &volatilep, &lr_mask,
3974 &lr_and_mask);
3975 rl_inner = decode_field_reference (rl_arg,
3976 &rl_bitsize, &rl_bitpos, &rl_mode,
3977 &rl_unsignedp, &volatilep, &rl_mask,
3978 &rl_and_mask);
3979 rr_inner = decode_field_reference (rr_arg,
3980 &rr_bitsize, &rr_bitpos, &rr_mode,
3981 &rr_unsignedp, &volatilep, &rr_mask,
3982 &rr_and_mask);
3984 /* It must be true that the inner operation on the lhs of each
3985 comparison must be the same if we are to be able to do anything.
3986 Then see if we have constants. If not, the same must be true for
3987 the rhs's. */
3988 if (volatilep || ll_inner == 0 || rl_inner == 0
3989 || ! operand_equal_p (ll_inner, rl_inner, 0))
3990 return 0;
3992 if (TREE_CODE (lr_arg) == INTEGER_CST
3993 && TREE_CODE (rr_arg) == INTEGER_CST)
3994 l_const = lr_arg, r_const = rr_arg;
3995 else if (lr_inner == 0 || rr_inner == 0
3996 || ! operand_equal_p (lr_inner, rr_inner, 0))
3997 return 0;
3998 else
3999 l_const = r_const = 0;
4001 /* If either comparison code is not correct for our logical operation,
4002 fail. However, we can convert a one-bit comparison against zero into
4003 the opposite comparison against that bit being set in the field. */
4005 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
4006 if (lcode != wanted_code)
4008 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
4010 /* Make the left operand unsigned, since we are only interested
4011 in the value of one bit. Otherwise we are doing the wrong
4012 thing below. */
4013 ll_unsignedp = 1;
4014 l_const = ll_mask;
4016 else
4017 return 0;
4020 /* This is analogous to the code for l_const above. */
4021 if (rcode != wanted_code)
4023 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
4025 rl_unsignedp = 1;
4026 r_const = rl_mask;
4028 else
4029 return 0;
4032 /* See if we can find a mode that contains both fields being compared on
4033 the left. If we can't, fail. Otherwise, update all constants and masks
4034 to be relative to a field of that size. */
4035 first_bit = MIN (ll_bitpos, rl_bitpos);
4036 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4037 lnmode = get_best_mode (end_bit - first_bit, first_bit,
4038 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4039 volatilep);
4040 if (lnmode == VOIDmode)
4041 return 0;
4043 lnbitsize = GET_MODE_BITSIZE (lnmode);
4044 lnbitpos = first_bit & ~ (lnbitsize - 1);
4045 lntype = type_for_size (lnbitsize, 1);
4046 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
4048 if (BYTES_BIG_ENDIAN)
4050 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
4051 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
4054 ll_mask = const_binop (LSHIFT_EXPR, convert (lntype, ll_mask),
4055 size_int (xll_bitpos), 0);
4056 rl_mask = const_binop (LSHIFT_EXPR, convert (lntype, rl_mask),
4057 size_int (xrl_bitpos), 0);
4059 if (l_const)
4061 l_const = convert (lntype, l_const);
4062 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
4063 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
4064 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
4065 fold (build1 (BIT_NOT_EXPR,
4066 lntype, ll_mask)),
4067 0)))
4069 warning ("comparison is always %d", wanted_code == NE_EXPR);
4071 return convert (truth_type,
4072 wanted_code == NE_EXPR
4073 ? integer_one_node : integer_zero_node);
4076 if (r_const)
4078 r_const = convert (lntype, r_const);
4079 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
4080 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
4081 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4082 fold (build1 (BIT_NOT_EXPR,
4083 lntype, rl_mask)),
4084 0)))
4086 warning ("comparison is always %d", wanted_code == NE_EXPR);
4088 return convert (truth_type,
4089 wanted_code == NE_EXPR
4090 ? integer_one_node : integer_zero_node);
4094 /* If the right sides are not constant, do the same for it. Also,
4095 disallow this optimization if a size or signedness mismatch occurs
4096 between the left and right sides. */
4097 if (l_const == 0)
4099 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
4100 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4101 /* Make sure the two fields on the right
4102 correspond to the left without being swapped. */
4103 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
4104 return 0;
4106 first_bit = MIN (lr_bitpos, rr_bitpos);
4107 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4108 rnmode = get_best_mode (end_bit - first_bit, first_bit,
4109 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4110 volatilep);
4111 if (rnmode == VOIDmode)
4112 return 0;
4114 rnbitsize = GET_MODE_BITSIZE (rnmode);
4115 rnbitpos = first_bit & ~ (rnbitsize - 1);
4116 rntype = type_for_size (rnbitsize, 1);
4117 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4119 if (BYTES_BIG_ENDIAN)
4121 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4122 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4125 lr_mask = const_binop (LSHIFT_EXPR, convert (rntype, lr_mask),
4126 size_int (xlr_bitpos), 0);
4127 rr_mask = const_binop (LSHIFT_EXPR, convert (rntype, rr_mask),
4128 size_int (xrr_bitpos), 0);
4130 /* Make a mask that corresponds to both fields being compared.
4131 Do this for both items being compared. If the operands are the
4132 same size and the bits being compared are in the same position
4133 then we can do this by masking both and comparing the masked
4134 results. */
4135 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4136 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
4137 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
4139 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4140 ll_unsignedp || rl_unsignedp);
4141 if (! all_ones_mask_p (ll_mask, lnbitsize))
4142 lhs = build (BIT_AND_EXPR, lntype, lhs, ll_mask);
4144 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
4145 lr_unsignedp || rr_unsignedp);
4146 if (! all_ones_mask_p (lr_mask, rnbitsize))
4147 rhs = build (BIT_AND_EXPR, rntype, rhs, lr_mask);
4149 return build (wanted_code, truth_type, lhs, rhs);
4152 /* There is still another way we can do something: If both pairs of
4153 fields being compared are adjacent, we may be able to make a wider
4154 field containing them both.
4156 Note that we still must mask the lhs/rhs expressions. Furthermore,
4157 the mask must be shifted to account for the shift done by
4158 make_bit_field_ref. */
4159 if ((ll_bitsize + ll_bitpos == rl_bitpos
4160 && lr_bitsize + lr_bitpos == rr_bitpos)
4161 || (ll_bitpos == rl_bitpos + rl_bitsize
4162 && lr_bitpos == rr_bitpos + rr_bitsize))
4164 tree type;
4166 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
4167 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
4168 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4169 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4171 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4172 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
4173 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4174 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4176 /* Convert to the smaller type before masking out unwanted bits. */
4177 type = lntype;
4178 if (lntype != rntype)
4180 if (lnbitsize > rnbitsize)
4182 lhs = convert (rntype, lhs);
4183 ll_mask = convert (rntype, ll_mask);
4184 type = rntype;
4186 else if (lnbitsize < rnbitsize)
4188 rhs = convert (lntype, rhs);
4189 lr_mask = convert (lntype, lr_mask);
4190 type = lntype;
4194 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
4195 lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
4197 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
4198 rhs = build (BIT_AND_EXPR, type, rhs, lr_mask);
4200 return build (wanted_code, truth_type, lhs, rhs);
4203 return 0;
4206 /* Handle the case of comparisons with constants. If there is something in
4207 common between the masks, those bits of the constants must be the same.
4208 If not, the condition is always false. Test for this to avoid generating
4209 incorrect code below. */
4210 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
4211 if (! integer_zerop (result)
4212 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
4213 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
4215 if (wanted_code == NE_EXPR)
4217 warning ("`or' of unmatched not-equal tests is always 1");
4218 return convert (truth_type, integer_one_node);
4220 else
4222 warning ("`and' of mutually exclusive equal-tests is always 0");
4223 return convert (truth_type, integer_zero_node);
4227 /* Construct the expression we will return. First get the component
4228 reference we will make. Unless the mask is all ones the width of
4229 that field, perform the mask operation. Then compare with the
4230 merged constant. */
4231 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4232 ll_unsignedp || rl_unsignedp);
4234 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4235 if (! all_ones_mask_p (ll_mask, lnbitsize))
4236 result = build (BIT_AND_EXPR, lntype, result, ll_mask);
4238 return build (wanted_code, truth_type, result,
4239 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
4242 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4243 constant. */
4245 static tree
4246 optimize_minmax_comparison (t)
4247 tree t;
4249 tree type = TREE_TYPE (t);
4250 tree arg0 = TREE_OPERAND (t, 0);
4251 enum tree_code op_code;
4252 tree comp_const = TREE_OPERAND (t, 1);
4253 tree minmax_const;
4254 int consts_equal, consts_lt;
4255 tree inner;
4257 STRIP_SIGN_NOPS (arg0);
4259 op_code = TREE_CODE (arg0);
4260 minmax_const = TREE_OPERAND (arg0, 1);
4261 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
4262 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4263 inner = TREE_OPERAND (arg0, 0);
4265 /* If something does not permit us to optimize, return the original tree. */
4266 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4267 || TREE_CODE (comp_const) != INTEGER_CST
4268 || TREE_CONSTANT_OVERFLOW (comp_const)
4269 || TREE_CODE (minmax_const) != INTEGER_CST
4270 || TREE_CONSTANT_OVERFLOW (minmax_const))
4271 return t;
4273 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4274 and GT_EXPR, doing the rest with recursive calls using logical
4275 simplifications. */
4276 switch (TREE_CODE (t))
4278 case NE_EXPR: case LT_EXPR: case LE_EXPR:
4279 return
4280 invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t)));
4282 case GE_EXPR:
4283 return
4284 fold (build (TRUTH_ORIF_EXPR, type,
4285 optimize_minmax_comparison
4286 (build (EQ_EXPR, type, arg0, comp_const)),
4287 optimize_minmax_comparison
4288 (build (GT_EXPR, type, arg0, comp_const))));
4290 case EQ_EXPR:
4291 if (op_code == MAX_EXPR && consts_equal)
4292 /* MAX (X, 0) == 0 -> X <= 0 */
4293 return fold (build (LE_EXPR, type, inner, comp_const));
4295 else if (op_code == MAX_EXPR && consts_lt)
4296 /* MAX (X, 0) == 5 -> X == 5 */
4297 return fold (build (EQ_EXPR, type, inner, comp_const));
4299 else if (op_code == MAX_EXPR)
4300 /* MAX (X, 0) == -1 -> false */
4301 return omit_one_operand (type, integer_zero_node, inner);
4303 else if (consts_equal)
4304 /* MIN (X, 0) == 0 -> X >= 0 */
4305 return fold (build (GE_EXPR, type, inner, comp_const));
4307 else if (consts_lt)
4308 /* MIN (X, 0) == 5 -> false */
4309 return omit_one_operand (type, integer_zero_node, inner);
4311 else
4312 /* MIN (X, 0) == -1 -> X == -1 */
4313 return fold (build (EQ_EXPR, type, inner, comp_const));
4315 case GT_EXPR:
4316 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
4317 /* MAX (X, 0) > 0 -> X > 0
4318 MAX (X, 0) > 5 -> X > 5 */
4319 return fold (build (GT_EXPR, type, inner, comp_const));
4321 else if (op_code == MAX_EXPR)
4322 /* MAX (X, 0) > -1 -> true */
4323 return omit_one_operand (type, integer_one_node, inner);
4325 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
4326 /* MIN (X, 0) > 0 -> false
4327 MIN (X, 0) > 5 -> false */
4328 return omit_one_operand (type, integer_zero_node, inner);
4330 else
4331 /* MIN (X, 0) > -1 -> X > -1 */
4332 return fold (build (GT_EXPR, type, inner, comp_const));
4334 default:
4335 return t;
4339 /* T is an integer expression that is being multiplied, divided, or taken a
4340 modulus (CODE says which and what kind of divide or modulus) by a
4341 constant C. See if we can eliminate that operation by folding it with
4342 other operations already in T. WIDE_TYPE, if non-null, is a type that
4343 should be used for the computation if wider than our type.
4345 For example, if we are dividing (X * 8) + (Y + 16) by 4, we can return
4346 (X * 2) + (Y + 4). We must, however, be assured that either the original
4347 expression would not overflow or that overflow is undefined for the type
4348 in the language in question.
4350 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
4351 the machine has a multiply-accumulate insn or that this is part of an
4352 addressing calculation.
4354 If we return a non-null expression, it is an equivalent form of the
4355 original computation, but need not be in the original type. */
4357 static tree
4358 extract_muldiv (t, c, code, wide_type)
4359 tree t;
4360 tree c;
4361 enum tree_code code;
4362 tree wide_type;
4364 tree type = TREE_TYPE (t);
4365 enum tree_code tcode = TREE_CODE (t);
4366 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
4367 > GET_MODE_SIZE (TYPE_MODE (type)))
4368 ? wide_type : type);
4369 tree t1, t2;
4370 int same_p = tcode == code;
4371 tree op0 = NULL_TREE, op1 = NULL_TREE;
4373 /* Don't deal with constants of zero here; they confuse the code below. */
4374 if (integer_zerop (c))
4375 return NULL_TREE;
4377 if (TREE_CODE_CLASS (tcode) == '1')
4378 op0 = TREE_OPERAND (t, 0);
4380 if (TREE_CODE_CLASS (tcode) == '2')
4381 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
4383 /* Note that we need not handle conditional operations here since fold
4384 already handles those cases. So just do arithmetic here. */
4385 switch (tcode)
4387 case INTEGER_CST:
4388 /* For a constant, we can always simplify if we are a multiply
4389 or (for divide and modulus) if it is a multiple of our constant. */
4390 if (code == MULT_EXPR
4391 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
4392 return const_binop (code, convert (ctype, t), convert (ctype, c), 0);
4393 break;
4395 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
4396 /* If op0 is an expression, and is unsigned, and the type is
4397 smaller than ctype, then we cannot widen the expression. */
4398 if ((TREE_CODE_CLASS (TREE_CODE (op0)) == '<'
4399 || TREE_CODE_CLASS (TREE_CODE (op0)) == '1'
4400 || TREE_CODE_CLASS (TREE_CODE (op0)) == '2'
4401 || TREE_CODE_CLASS (TREE_CODE (op0)) == 'e')
4402 && TREE_UNSIGNED (TREE_TYPE (op0))
4403 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
4404 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
4405 && (GET_MODE_SIZE (TYPE_MODE (ctype))
4406 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
4407 break;
4409 /* Pass the constant down and see if we can make a simplification. If
4410 we can, replace this expression with the inner simplification for
4411 possible later conversion to our or some other type. */
4412 if (0 != (t1 = extract_muldiv (op0, convert (TREE_TYPE (op0), c), code,
4413 code == MULT_EXPR ? ctype : NULL_TREE)))
4414 return t1;
4415 break;
4417 case NEGATE_EXPR: case ABS_EXPR:
4418 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4419 return fold (build1 (tcode, ctype, convert (ctype, t1)));
4420 break;
4422 case MIN_EXPR: case MAX_EXPR:
4423 /* If widening the type changes the signedness, then we can't perform
4424 this optimization as that changes the result. */
4425 if (TREE_UNSIGNED (ctype) != TREE_UNSIGNED (type))
4426 break;
4428 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
4429 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
4430 && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
4432 if (tree_int_cst_sgn (c) < 0)
4433 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
4435 return fold (build (tcode, ctype, convert (ctype, t1),
4436 convert (ctype, t2)));
4438 break;
4440 case WITH_RECORD_EXPR:
4441 if ((t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code, wide_type)) != 0)
4442 return build (WITH_RECORD_EXPR, TREE_TYPE (t1), t1,
4443 TREE_OPERAND (t, 1));
4444 break;
4446 case SAVE_EXPR:
4447 /* If this has not been evaluated and the operand has no side effects,
4448 we can see if we can do something inside it and make a new one.
4449 Note that this test is overly conservative since we can do this
4450 if the only reason it had side effects is that it was another
4451 similar SAVE_EXPR, but that isn't worth bothering with. */
4452 if (SAVE_EXPR_RTL (t) == 0 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0))
4453 && 0 != (t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code,
4454 wide_type)))
4455 return save_expr (t1);
4456 break;
4458 case LSHIFT_EXPR: case RSHIFT_EXPR:
4459 /* If the second operand is constant, this is a multiplication
4460 or floor division, by a power of two, so we can treat it that
4461 way unless the multiplier or divisor overflows. */
4462 if (TREE_CODE (op1) == INTEGER_CST
4463 /* const_binop may not detect overflow correctly,
4464 so check for it explicitly here. */
4465 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
4466 && TREE_INT_CST_HIGH (op1) == 0
4467 && 0 != (t1 = convert (ctype,
4468 const_binop (LSHIFT_EXPR, size_one_node,
4469 op1, 0)))
4470 && ! TREE_OVERFLOW (t1))
4471 return extract_muldiv (build (tcode == LSHIFT_EXPR
4472 ? MULT_EXPR : FLOOR_DIV_EXPR,
4473 ctype, convert (ctype, op0), t1),
4474 c, code, wide_type);
4475 break;
4477 case PLUS_EXPR: case MINUS_EXPR:
4478 /* See if we can eliminate the operation on both sides. If we can, we
4479 can return a new PLUS or MINUS. If we can't, the only remaining
4480 cases where we can do anything are if the second operand is a
4481 constant. */
4482 t1 = extract_muldiv (op0, c, code, wide_type);
4483 t2 = extract_muldiv (op1, c, code, wide_type);
4484 if (t1 != 0 && t2 != 0
4485 && (code == MULT_EXPR
4486 /* If not multiplication, we can only do this if either operand
4487 is divisible by c. */
4488 || multiple_of_p (ctype, op0, c)
4489 || multiple_of_p (ctype, op1, c)))
4490 return fold (build (tcode, ctype, convert (ctype, t1),
4491 convert (ctype, t2)));
4493 /* If this was a subtraction, negate OP1 and set it to be an addition.
4494 This simplifies the logic below. */
4495 if (tcode == MINUS_EXPR)
4496 tcode = PLUS_EXPR, op1 = negate_expr (op1);
4498 if (TREE_CODE (op1) != INTEGER_CST)
4499 break;
4501 /* If either OP1 or C are negative, this optimization is not safe for
4502 some of the division and remainder types while for others we need
4503 to change the code. */
4504 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
4506 if (code == CEIL_DIV_EXPR)
4507 code = FLOOR_DIV_EXPR;
4508 else if (code == CEIL_MOD_EXPR)
4509 code = FLOOR_MOD_EXPR;
4510 else if (code == FLOOR_DIV_EXPR)
4511 code = CEIL_DIV_EXPR;
4512 else if (code == FLOOR_MOD_EXPR)
4513 code = CEIL_MOD_EXPR;
4514 else if (code != MULT_EXPR)
4515 break;
4518 /* If it's a multiply or a division/modulus operation of a multiple
4519 of our constant, do the operation and verify it doesn't overflow. */
4520 if (code == MULT_EXPR
4521 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4523 op1 = const_binop (code, convert (ctype, op1), convert (ctype, c), 0);
4524 if (op1 == 0 || TREE_OVERFLOW (op1))
4525 break;
4527 else
4528 break;
4530 /* If we have an unsigned type is not a sizetype, we cannot widen
4531 the operation since it will change the result if the original
4532 computation overflowed. */
4533 if (TREE_UNSIGNED (ctype)
4534 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
4535 && ctype != type)
4536 break;
4538 /* If we were able to eliminate our operation from the first side,
4539 apply our operation to the second side and reform the PLUS. */
4540 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
4541 return fold (build (tcode, ctype, convert (ctype, t1), op1));
4543 /* The last case is if we are a multiply. In that case, we can
4544 apply the distributive law to commute the multiply and addition
4545 if the multiplication of the constants doesn't overflow. */
4546 if (code == MULT_EXPR)
4547 return fold (build (tcode, ctype, fold (build (code, ctype,
4548 convert (ctype, op0),
4549 convert (ctype, c))),
4550 op1));
4552 break;
4554 case MULT_EXPR:
4555 /* We have a special case here if we are doing something like
4556 (C * 8) % 4 since we know that's zero. */
4557 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
4558 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
4559 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
4560 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4561 return omit_one_operand (type, integer_zero_node, op0);
4563 /* ... fall through ... */
4565 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
4566 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
4567 /* If we can extract our operation from the LHS, do so and return a
4568 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
4569 do something only if the second operand is a constant. */
4570 if (same_p
4571 && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4572 return fold (build (tcode, ctype, convert (ctype, t1),
4573 convert (ctype, op1)));
4574 else if (tcode == MULT_EXPR && code == MULT_EXPR
4575 && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
4576 return fold (build (tcode, ctype, convert (ctype, op0),
4577 convert (ctype, t1)));
4578 else if (TREE_CODE (op1) != INTEGER_CST)
4579 return 0;
4581 /* If these are the same operation types, we can associate them
4582 assuming no overflow. */
4583 if (tcode == code
4584 && 0 != (t1 = const_binop (MULT_EXPR, convert (ctype, op1),
4585 convert (ctype, c), 0))
4586 && ! TREE_OVERFLOW (t1))
4587 return fold (build (tcode, ctype, convert (ctype, op0), t1));
4589 /* If these operations "cancel" each other, we have the main
4590 optimizations of this pass, which occur when either constant is a
4591 multiple of the other, in which case we replace this with either an
4592 operation or CODE or TCODE.
4594 If we have an unsigned type that is not a sizetype, we canot do
4595 this since it will change the result if the original computation
4596 overflowed. */
4597 if ((! TREE_UNSIGNED (ctype)
4598 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
4599 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
4600 || (tcode == MULT_EXPR
4601 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
4602 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
4604 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4605 return fold (build (tcode, ctype, convert (ctype, op0),
4606 convert (ctype,
4607 const_binop (TRUNC_DIV_EXPR,
4608 op1, c, 0))));
4609 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
4610 return fold (build (code, ctype, convert (ctype, op0),
4611 convert (ctype,
4612 const_binop (TRUNC_DIV_EXPR,
4613 c, op1, 0))));
4615 break;
4617 default:
4618 break;
4621 return 0;
4624 /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
4625 S, a SAVE_EXPR, return the expression actually being evaluated. Note
4626 that we may sometimes modify the tree. */
4628 static tree
4629 strip_compound_expr (t, s)
4630 tree t;
4631 tree s;
4633 enum tree_code code = TREE_CODE (t);
4635 /* See if this is the COMPOUND_EXPR we want to eliminate. */
4636 if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
4637 && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
4638 return TREE_OPERAND (t, 1);
4640 /* See if this is a COND_EXPR or a simple arithmetic operator. We
4641 don't bother handling any other types. */
4642 else if (code == COND_EXPR)
4644 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4645 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4646 TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
4648 else if (TREE_CODE_CLASS (code) == '1')
4649 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4650 else if (TREE_CODE_CLASS (code) == '<'
4651 || TREE_CODE_CLASS (code) == '2')
4653 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4654 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4657 return t;
4660 /* Return a node which has the indicated constant VALUE (either 0 or
4661 1), and is of the indicated TYPE. */
4663 static tree
4664 constant_boolean_node (value, type)
4665 int value;
4666 tree type;
4668 if (type == integer_type_node)
4669 return value ? integer_one_node : integer_zero_node;
4670 else if (TREE_CODE (type) == BOOLEAN_TYPE)
4671 return truthvalue_conversion (value ? integer_one_node :
4672 integer_zero_node);
4673 else
4675 tree t = build_int_2 (value, 0);
4677 TREE_TYPE (t) = type;
4678 return t;
4682 /* Utility function for the following routine, to see how complex a nesting of
4683 COND_EXPRs can be. EXPR is the expression and LIMIT is a count beyond which
4684 we don't care (to avoid spending too much time on complex expressions.). */
4686 static int
4687 count_cond (expr, lim)
4688 tree expr;
4689 int lim;
4691 int ctrue, cfalse;
4693 if (TREE_CODE (expr) != COND_EXPR)
4694 return 0;
4695 else if (lim <= 0)
4696 return 0;
4698 ctrue = count_cond (TREE_OPERAND (expr, 1), lim - 1);
4699 cfalse = count_cond (TREE_OPERAND (expr, 2), lim - 1 - ctrue);
4700 return MIN (lim, 1 + ctrue + cfalse);
4703 /* Transform `a + (b ? x : y)' into `x ? (a + b) : (a + y)'.
4704 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
4705 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
4706 expression, and ARG to `a'. If COND_FIRST_P is non-zero, then the
4707 COND is the first argument to CODE; otherwise (as in the example
4708 given here), it is the second argument. TYPE is the type of the
4709 original expression. */
4711 static tree
4712 fold_binary_op_with_conditional_arg (code, type, cond, arg, cond_first_p)
4713 enum tree_code code;
4714 tree type;
4715 tree cond;
4716 tree arg;
4717 int cond_first_p;
4719 tree test, true_value, false_value;
4720 tree lhs = NULL_TREE;
4721 tree rhs = NULL_TREE;
4722 /* In the end, we'll produce a COND_EXPR. Both arms of the
4723 conditional expression will be binary operations. The left-hand
4724 side of the expression to be executed if the condition is true
4725 will be pointed to by TRUE_LHS. Similarly, the right-hand side
4726 of the expression to be executed if the condition is true will be
4727 pointed to by TRUE_RHS. FALSE_LHS and FALSE_RHS are analagous --
4728 but apply to the expression to be executed if the conditional is
4729 false. */
4730 tree *true_lhs;
4731 tree *true_rhs;
4732 tree *false_lhs;
4733 tree *false_rhs;
4734 /* These are the codes to use for the left-hand side and right-hand
4735 side of the COND_EXPR. Normally, they are the same as CODE. */
4736 enum tree_code lhs_code = code;
4737 enum tree_code rhs_code = code;
4738 /* And these are the types of the expressions. */
4739 tree lhs_type = type;
4740 tree rhs_type = type;
4742 if (cond_first_p)
4744 true_rhs = false_rhs = &arg;
4745 true_lhs = &true_value;
4746 false_lhs = &false_value;
4748 else
4750 true_lhs = false_lhs = &arg;
4751 true_rhs = &true_value;
4752 false_rhs = &false_value;
4755 if (TREE_CODE (cond) == COND_EXPR)
4757 test = TREE_OPERAND (cond, 0);
4758 true_value = TREE_OPERAND (cond, 1);
4759 false_value = TREE_OPERAND (cond, 2);
4760 /* If this operand throws an expression, then it does not make
4761 sense to try to perform a logical or arithmetic operation
4762 involving it. Instead of building `a + throw 3' for example,
4763 we simply build `a, throw 3'. */
4764 if (VOID_TYPE_P (TREE_TYPE (true_value)))
4766 lhs_code = COMPOUND_EXPR;
4767 if (!cond_first_p)
4768 lhs_type = void_type_node;
4770 if (VOID_TYPE_P (TREE_TYPE (false_value)))
4772 rhs_code = COMPOUND_EXPR;
4773 if (!cond_first_p)
4774 rhs_type = void_type_node;
4777 else
4779 tree testtype = TREE_TYPE (cond);
4780 test = cond;
4781 true_value = convert (testtype, integer_one_node);
4782 false_value = convert (testtype, integer_zero_node);
4785 /* If ARG is complex we want to make sure we only evaluate
4786 it once. Though this is only required if it is volatile, it
4787 might be more efficient even if it is not. However, if we
4788 succeed in folding one part to a constant, we do not need
4789 to make this SAVE_EXPR. Since we do this optimization
4790 primarily to see if we do end up with constant and this
4791 SAVE_EXPR interferes with later optimizations, suppressing
4792 it when we can is important.
4794 If we are not in a function, we can't make a SAVE_EXPR, so don't
4795 try to do so. Don't try to see if the result is a constant
4796 if an arm is a COND_EXPR since we get exponential behavior
4797 in that case. */
4799 if (TREE_CODE (arg) != SAVE_EXPR && ! TREE_CONSTANT (arg)
4800 && global_bindings_p () == 0
4801 && ((TREE_CODE (arg) != VAR_DECL
4802 && TREE_CODE (arg) != PARM_DECL)
4803 || TREE_SIDE_EFFECTS (arg)))
4805 if (TREE_CODE (true_value) != COND_EXPR)
4806 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4808 if (TREE_CODE (false_value) != COND_EXPR)
4809 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4811 if ((lhs == 0 || ! TREE_CONSTANT (lhs))
4812 && (rhs == 0 || !TREE_CONSTANT (rhs)))
4813 arg = save_expr (arg), lhs = rhs = 0;
4816 if (lhs == 0)
4817 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4818 if (rhs == 0)
4819 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4821 test = fold (build (COND_EXPR, type, test, lhs, rhs));
4823 if (TREE_CODE (arg) == SAVE_EXPR)
4824 return build (COMPOUND_EXPR, type,
4825 convert (void_type_node, arg),
4826 strip_compound_expr (test, arg));
4827 else
4828 return convert (type, test);
4832 /* Perform constant folding and related simplification of EXPR.
4833 The related simplifications include x*1 => x, x*0 => 0, etc.,
4834 and application of the associative law.
4835 NOP_EXPR conversions may be removed freely (as long as we
4836 are careful not to change the C type of the overall expression)
4837 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
4838 but we can constant-fold them if they have constant operands. */
4840 tree
4841 fold (expr)
4842 tree expr;
4844 register tree t = expr;
4845 tree t1 = NULL_TREE;
4846 tree tem;
4847 tree type = TREE_TYPE (expr);
4848 register tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4849 register enum tree_code code = TREE_CODE (t);
4850 register int kind = TREE_CODE_CLASS (code);
4851 int invert;
4852 /* WINS will be nonzero when the switch is done
4853 if all operands are constant. */
4854 int wins = 1;
4856 /* Don't try to process an RTL_EXPR since its operands aren't trees.
4857 Likewise for a SAVE_EXPR that's already been evaluated. */
4858 if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t)) != 0)
4859 return t;
4861 /* Return right away if a constant. */
4862 if (kind == 'c')
4863 return t;
4865 #ifdef MAX_INTEGER_COMPUTATION_MODE
4866 check_max_integer_computation_mode (expr);
4867 #endif
4869 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
4871 tree subop;
4873 /* Special case for conversion ops that can have fixed point args. */
4874 arg0 = TREE_OPERAND (t, 0);
4876 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
4877 if (arg0 != 0)
4878 STRIP_SIGN_NOPS (arg0);
4880 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
4881 subop = TREE_REALPART (arg0);
4882 else
4883 subop = arg0;
4885 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
4886 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4887 && TREE_CODE (subop) != REAL_CST
4888 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4890 /* Note that TREE_CONSTANT isn't enough:
4891 static var addresses are constant but we can't
4892 do arithmetic on them. */
4893 wins = 0;
4895 else if (IS_EXPR_CODE_CLASS (kind) || kind == 'r')
4897 register int len = TREE_CODE_LENGTH (code);
4898 register int i;
4899 for (i = 0; i < len; i++)
4901 tree op = TREE_OPERAND (t, i);
4902 tree subop;
4904 if (op == 0)
4905 continue; /* Valid for CALL_EXPR, at least. */
4907 if (kind == '<' || code == RSHIFT_EXPR)
4909 /* Signedness matters here. Perhaps we can refine this
4910 later. */
4911 STRIP_SIGN_NOPS (op);
4913 else
4914 /* Strip any conversions that don't change the mode. */
4915 STRIP_NOPS (op);
4917 if (TREE_CODE (op) == COMPLEX_CST)
4918 subop = TREE_REALPART (op);
4919 else
4920 subop = op;
4922 if (TREE_CODE (subop) != INTEGER_CST
4923 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4924 && TREE_CODE (subop) != REAL_CST
4925 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4927 /* Note that TREE_CONSTANT isn't enough:
4928 static var addresses are constant but we can't
4929 do arithmetic on them. */
4930 wins = 0;
4932 if (i == 0)
4933 arg0 = op;
4934 else if (i == 1)
4935 arg1 = op;
4939 /* If this is a commutative operation, and ARG0 is a constant, move it
4940 to ARG1 to reduce the number of tests below. */
4941 if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
4942 || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
4943 || code == BIT_AND_EXPR)
4944 && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
4946 tem = arg0; arg0 = arg1; arg1 = tem;
4948 tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
4949 TREE_OPERAND (t, 1) = tem;
4952 /* Now WINS is set as described above,
4953 ARG0 is the first operand of EXPR,
4954 and ARG1 is the second operand (if it has more than one operand).
4956 First check for cases where an arithmetic operation is applied to a
4957 compound, conditional, or comparison operation. Push the arithmetic
4958 operation inside the compound or conditional to see if any folding
4959 can then be done. Convert comparison to conditional for this purpose.
4960 The also optimizes non-constant cases that used to be done in
4961 expand_expr.
4963 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
4964 one of the operands is a comparison and the other is a comparison, a
4965 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
4966 code below would make the expression more complex. Change it to a
4967 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
4968 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
4970 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
4971 || code == EQ_EXPR || code == NE_EXPR)
4972 && ((truth_value_p (TREE_CODE (arg0))
4973 && (truth_value_p (TREE_CODE (arg1))
4974 || (TREE_CODE (arg1) == BIT_AND_EXPR
4975 && integer_onep (TREE_OPERAND (arg1, 1)))))
4976 || (truth_value_p (TREE_CODE (arg1))
4977 && (truth_value_p (TREE_CODE (arg0))
4978 || (TREE_CODE (arg0) == BIT_AND_EXPR
4979 && integer_onep (TREE_OPERAND (arg0, 1)))))))
4981 t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
4982 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
4983 : TRUTH_XOR_EXPR,
4984 type, arg0, arg1));
4986 if (code == EQ_EXPR)
4987 t = invert_truthvalue (t);
4989 return t;
4992 if (TREE_CODE_CLASS (code) == '1')
4994 if (TREE_CODE (arg0) == COMPOUND_EXPR)
4995 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
4996 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
4997 else if (TREE_CODE (arg0) == COND_EXPR)
4999 t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
5000 fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
5001 fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
5003 /* If this was a conversion, and all we did was to move into
5004 inside the COND_EXPR, bring it back out. But leave it if
5005 it is a conversion from integer to integer and the
5006 result precision is no wider than a word since such a
5007 conversion is cheap and may be optimized away by combine,
5008 while it couldn't if it were outside the COND_EXPR. Then return
5009 so we don't get into an infinite recursion loop taking the
5010 conversion out and then back in. */
5012 if ((code == NOP_EXPR || code == CONVERT_EXPR
5013 || code == NON_LVALUE_EXPR)
5014 && TREE_CODE (t) == COND_EXPR
5015 && TREE_CODE (TREE_OPERAND (t, 1)) == code
5016 && TREE_CODE (TREE_OPERAND (t, 2)) == code
5017 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
5018 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0)))
5019 && ! (INTEGRAL_TYPE_P (TREE_TYPE (t))
5020 && (INTEGRAL_TYPE_P
5021 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))))
5022 && TYPE_PRECISION (TREE_TYPE (t)) <= BITS_PER_WORD))
5023 t = build1 (code, type,
5024 build (COND_EXPR,
5025 TREE_TYPE (TREE_OPERAND
5026 (TREE_OPERAND (t, 1), 0)),
5027 TREE_OPERAND (t, 0),
5028 TREE_OPERAND (TREE_OPERAND (t, 1), 0),
5029 TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
5030 return t;
5032 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
5033 return fold (build (COND_EXPR, type, arg0,
5034 fold (build1 (code, type, integer_one_node)),
5035 fold (build1 (code, type, integer_zero_node))));
5037 else if (TREE_CODE_CLASS (code) == '2'
5038 || TREE_CODE_CLASS (code) == '<')
5040 if (TREE_CODE (arg1) == COMPOUND_EXPR)
5041 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5042 fold (build (code, type,
5043 arg0, TREE_OPERAND (arg1, 1))));
5044 else if ((TREE_CODE (arg1) == COND_EXPR
5045 || (TREE_CODE_CLASS (TREE_CODE (arg1)) == '<'
5046 && TREE_CODE_CLASS (code) != '<'))
5047 && (TREE_CODE (arg0) != COND_EXPR
5048 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
5049 && (! TREE_SIDE_EFFECTS (arg0)
5050 || (global_bindings_p () == 0
5051 && ! contains_placeholder_p (arg0))))
5052 return
5053 fold_binary_op_with_conditional_arg (code, type, arg1, arg0,
5054 /*cond_first_p=*/0);
5055 else if (TREE_CODE (arg0) == COMPOUND_EXPR)
5056 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5057 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5058 else if ((TREE_CODE (arg0) == COND_EXPR
5059 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
5060 && TREE_CODE_CLASS (code) != '<'))
5061 && (TREE_CODE (arg1) != COND_EXPR
5062 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
5063 && (! TREE_SIDE_EFFECTS (arg1)
5064 || (global_bindings_p () == 0
5065 && ! contains_placeholder_p (arg1))))
5066 return
5067 fold_binary_op_with_conditional_arg (code, type, arg0, arg1,
5068 /*cond_first_p=*/1);
5070 else if (TREE_CODE_CLASS (code) == '<'
5071 && TREE_CODE (arg0) == COMPOUND_EXPR)
5072 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5073 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5074 else if (TREE_CODE_CLASS (code) == '<'
5075 && TREE_CODE (arg1) == COMPOUND_EXPR)
5076 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5077 fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
5079 switch (code)
5081 case INTEGER_CST:
5082 case REAL_CST:
5083 case STRING_CST:
5084 case COMPLEX_CST:
5085 case CONSTRUCTOR:
5086 return t;
5088 case CONST_DECL:
5089 return fold (DECL_INITIAL (t));
5091 case NOP_EXPR:
5092 case FLOAT_EXPR:
5093 case CONVERT_EXPR:
5094 case FIX_TRUNC_EXPR:
5095 /* Other kinds of FIX are not handled properly by fold_convert. */
5097 if (TREE_TYPE (TREE_OPERAND (t, 0)) == TREE_TYPE (t))
5098 return TREE_OPERAND (t, 0);
5100 /* Handle cases of two conversions in a row. */
5101 if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
5102 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
5104 tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5105 tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
5106 tree final_type = TREE_TYPE (t);
5107 int inside_int = INTEGRAL_TYPE_P (inside_type);
5108 int inside_ptr = POINTER_TYPE_P (inside_type);
5109 int inside_float = FLOAT_TYPE_P (inside_type);
5110 unsigned int inside_prec = TYPE_PRECISION (inside_type);
5111 int inside_unsignedp = TREE_UNSIGNED (inside_type);
5112 int inter_int = INTEGRAL_TYPE_P (inter_type);
5113 int inter_ptr = POINTER_TYPE_P (inter_type);
5114 int inter_float = FLOAT_TYPE_P (inter_type);
5115 unsigned int inter_prec = TYPE_PRECISION (inter_type);
5116 int inter_unsignedp = TREE_UNSIGNED (inter_type);
5117 int final_int = INTEGRAL_TYPE_P (final_type);
5118 int final_ptr = POINTER_TYPE_P (final_type);
5119 int final_float = FLOAT_TYPE_P (final_type);
5120 unsigned int final_prec = TYPE_PRECISION (final_type);
5121 int final_unsignedp = TREE_UNSIGNED (final_type);
5123 /* In addition to the cases of two conversions in a row
5124 handled below, if we are converting something to its own
5125 type via an object of identical or wider precision, neither
5126 conversion is needed. */
5127 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (final_type)
5128 && ((inter_int && final_int) || (inter_float && final_float))
5129 && inter_prec >= final_prec)
5130 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5132 /* Likewise, if the intermediate and final types are either both
5133 float or both integer, we don't need the middle conversion if
5134 it is wider than the final type and doesn't change the signedness
5135 (for integers). Avoid this if the final type is a pointer
5136 since then we sometimes need the inner conversion. Likewise if
5137 the outer has a precision not equal to the size of its mode. */
5138 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
5139 || (inter_float && inside_float))
5140 && inter_prec >= inside_prec
5141 && (inter_float || inter_unsignedp == inside_unsignedp)
5142 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5143 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5144 && ! final_ptr)
5145 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5147 /* If we have a sign-extension of a zero-extended value, we can
5148 replace that by a single zero-extension. */
5149 if (inside_int && inter_int && final_int
5150 && inside_prec < inter_prec && inter_prec < final_prec
5151 && inside_unsignedp && !inter_unsignedp)
5152 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5154 /* Two conversions in a row are not needed unless:
5155 - some conversion is floating-point (overstrict for now), or
5156 - the intermediate type is narrower than both initial and
5157 final, or
5158 - the intermediate type and innermost type differ in signedness,
5159 and the outermost type is wider than the intermediate, or
5160 - the initial type is a pointer type and the precisions of the
5161 intermediate and final types differ, or
5162 - the final type is a pointer type and the precisions of the
5163 initial and intermediate types differ. */
5164 if (! inside_float && ! inter_float && ! final_float
5165 && (inter_prec > inside_prec || inter_prec > final_prec)
5166 && ! (inside_int && inter_int
5167 && inter_unsignedp != inside_unsignedp
5168 && inter_prec < final_prec)
5169 && ((inter_unsignedp && inter_prec > inside_prec)
5170 == (final_unsignedp && final_prec > inter_prec))
5171 && ! (inside_ptr && inter_prec != final_prec)
5172 && ! (final_ptr && inside_prec != inter_prec)
5173 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5174 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5175 && ! final_ptr)
5176 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5179 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
5180 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
5181 /* Detect assigning a bitfield. */
5182 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
5183 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
5185 /* Don't leave an assignment inside a conversion
5186 unless assigning a bitfield. */
5187 tree prev = TREE_OPERAND (t, 0);
5188 TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
5189 /* First do the assignment, then return converted constant. */
5190 t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
5191 TREE_USED (t) = 1;
5192 return t;
5194 if (!wins)
5196 TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
5197 return t;
5199 return fold_convert (t, arg0);
5201 #if 0 /* This loses on &"foo"[0]. */
5202 case ARRAY_REF:
5204 int i;
5206 /* Fold an expression like: "foo"[2] */
5207 if (TREE_CODE (arg0) == STRING_CST
5208 && TREE_CODE (arg1) == INTEGER_CST
5209 && compare_tree_int (arg1, TREE_STRING_LENGTH (arg0)) < 0)
5211 t = build_int_2 (TREE_STRING_POINTER (arg0)[TREE_INT_CST_LOW (arg))], 0);
5212 TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
5213 force_fit_type (t, 0);
5216 return t;
5217 #endif /* 0 */
5219 case COMPONENT_REF:
5220 if (TREE_CODE (arg0) == CONSTRUCTOR)
5222 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
5223 if (m)
5224 t = TREE_VALUE (m);
5226 return t;
5228 case RANGE_EXPR:
5229 TREE_CONSTANT (t) = wins;
5230 return t;
5232 case NEGATE_EXPR:
5233 if (wins)
5235 if (TREE_CODE (arg0) == INTEGER_CST)
5237 unsigned HOST_WIDE_INT low;
5238 HOST_WIDE_INT high;
5239 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5240 TREE_INT_CST_HIGH (arg0),
5241 &low, &high);
5242 t = build_int_2 (low, high);
5243 TREE_TYPE (t) = type;
5244 TREE_OVERFLOW (t)
5245 = (TREE_OVERFLOW (arg0)
5246 | force_fit_type (t, overflow && !TREE_UNSIGNED (type)));
5247 TREE_CONSTANT_OVERFLOW (t)
5248 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
5250 else if (TREE_CODE (arg0) == REAL_CST)
5251 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5253 else if (TREE_CODE (arg0) == NEGATE_EXPR)
5254 return TREE_OPERAND (arg0, 0);
5256 /* Convert - (a - b) to (b - a) for non-floating-point. */
5257 else if (TREE_CODE (arg0) == MINUS_EXPR
5258 && (! FLOAT_TYPE_P (type) || flag_fast_math))
5259 return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
5260 TREE_OPERAND (arg0, 0));
5262 return t;
5264 case ABS_EXPR:
5265 if (wins)
5267 if (TREE_CODE (arg0) == INTEGER_CST)
5269 /* If the value is unsigned, then the absolute value is
5270 the same as the ordinary value. */
5271 if (TREE_UNSIGNED (type))
5272 return arg0;
5273 /* Similarly, if the value is non-negative. */
5274 else if (INT_CST_LT (integer_minus_one_node, arg0))
5275 return arg0;
5276 /* If the value is negative, then the absolute value is
5277 its negation. */
5278 else
5280 unsigned HOST_WIDE_INT low;
5281 HOST_WIDE_INT high;
5282 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5283 TREE_INT_CST_HIGH (arg0),
5284 &low, &high);
5285 t = build_int_2 (low, high);
5286 TREE_TYPE (t) = type;
5287 TREE_OVERFLOW (t)
5288 = (TREE_OVERFLOW (arg0)
5289 | force_fit_type (t, overflow));
5290 TREE_CONSTANT_OVERFLOW (t)
5291 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
5294 else if (TREE_CODE (arg0) == REAL_CST)
5296 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
5297 t = build_real (type,
5298 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5301 else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
5302 return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
5303 return t;
5305 case CONJ_EXPR:
5306 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
5307 return convert (type, arg0);
5308 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
5309 return build (COMPLEX_EXPR, type,
5310 TREE_OPERAND (arg0, 0),
5311 negate_expr (TREE_OPERAND (arg0, 1)));
5312 else if (TREE_CODE (arg0) == COMPLEX_CST)
5313 return build_complex (type, TREE_REALPART (arg0),
5314 negate_expr (TREE_IMAGPART (arg0)));
5315 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
5316 return fold (build (TREE_CODE (arg0), type,
5317 fold (build1 (CONJ_EXPR, type,
5318 TREE_OPERAND (arg0, 0))),
5319 fold (build1 (CONJ_EXPR,
5320 type, TREE_OPERAND (arg0, 1)))));
5321 else if (TREE_CODE (arg0) == CONJ_EXPR)
5322 return TREE_OPERAND (arg0, 0);
5323 return t;
5325 case BIT_NOT_EXPR:
5326 if (wins)
5328 t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
5329 ~ TREE_INT_CST_HIGH (arg0));
5330 TREE_TYPE (t) = type;
5331 force_fit_type (t, 0);
5332 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
5333 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
5335 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
5336 return TREE_OPERAND (arg0, 0);
5337 return t;
5339 case PLUS_EXPR:
5340 /* A + (-B) -> A - B */
5341 if (TREE_CODE (arg1) == NEGATE_EXPR)
5342 return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5343 /* (-A) + B -> B - A */
5344 if (TREE_CODE (arg0) == NEGATE_EXPR)
5345 return fold (build (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
5346 else if (! FLOAT_TYPE_P (type))
5348 if (integer_zerop (arg1))
5349 return non_lvalue (convert (type, arg0));
5351 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
5352 with a constant, and the two constants have no bits in common,
5353 we should treat this as a BIT_IOR_EXPR since this may produce more
5354 simplifications. */
5355 if (TREE_CODE (arg0) == BIT_AND_EXPR
5356 && TREE_CODE (arg1) == BIT_AND_EXPR
5357 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5358 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5359 && integer_zerop (const_binop (BIT_AND_EXPR,
5360 TREE_OPERAND (arg0, 1),
5361 TREE_OPERAND (arg1, 1), 0)))
5363 code = BIT_IOR_EXPR;
5364 goto bit_ior;
5367 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
5368 (plus (plus (mult) (mult)) (foo)) so that we can
5369 take advantage of the factoring cases below. */
5370 if ((TREE_CODE (arg0) == PLUS_EXPR
5371 && TREE_CODE (arg1) == MULT_EXPR)
5372 || (TREE_CODE (arg1) == PLUS_EXPR
5373 && TREE_CODE (arg0) == MULT_EXPR))
5375 tree parg0, parg1, parg, marg;
5377 if (TREE_CODE (arg0) == PLUS_EXPR)
5378 parg = arg0, marg = arg1;
5379 else
5380 parg = arg1, marg = arg0;
5381 parg0 = TREE_OPERAND (parg, 0);
5382 parg1 = TREE_OPERAND (parg, 1);
5383 STRIP_NOPS (parg0);
5384 STRIP_NOPS (parg1);
5386 if (TREE_CODE (parg0) == MULT_EXPR
5387 && TREE_CODE (parg1) != MULT_EXPR)
5388 return fold (build (PLUS_EXPR, type,
5389 fold (build (PLUS_EXPR, type, parg0, marg)),
5390 parg1));
5391 if (TREE_CODE (parg0) != MULT_EXPR
5392 && TREE_CODE (parg1) == MULT_EXPR)
5393 return fold (build (PLUS_EXPR, type,
5394 fold (build (PLUS_EXPR, type, parg1, marg)),
5395 parg0));
5398 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
5400 tree arg00, arg01, arg10, arg11;
5401 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
5403 /* (A * C) + (B * C) -> (A+B) * C.
5404 We are most concerned about the case where C is a constant,
5405 but other combinations show up during loop reduction. Since
5406 it is not difficult, try all four possibilities. */
5408 arg00 = TREE_OPERAND (arg0, 0);
5409 arg01 = TREE_OPERAND (arg0, 1);
5410 arg10 = TREE_OPERAND (arg1, 0);
5411 arg11 = TREE_OPERAND (arg1, 1);
5412 same = NULL_TREE;
5414 if (operand_equal_p (arg01, arg11, 0))
5415 same = arg01, alt0 = arg00, alt1 = arg10;
5416 else if (operand_equal_p (arg00, arg10, 0))
5417 same = arg00, alt0 = arg01, alt1 = arg11;
5418 else if (operand_equal_p (arg00, arg11, 0))
5419 same = arg00, alt0 = arg01, alt1 = arg10;
5420 else if (operand_equal_p (arg01, arg10, 0))
5421 same = arg01, alt0 = arg00, alt1 = arg11;
5423 /* No identical multiplicands; see if we can find a common
5424 power-of-two factor in non-power-of-two multiplies. This
5425 can help in multi-dimensional array access. */
5426 else if (TREE_CODE (arg01) == INTEGER_CST
5427 && TREE_CODE (arg11) == INTEGER_CST
5428 && TREE_INT_CST_HIGH (arg01) == 0
5429 && TREE_INT_CST_HIGH (arg11) == 0)
5431 HOST_WIDE_INT int01, int11, tmp;
5432 int01 = TREE_INT_CST_LOW (arg01);
5433 int11 = TREE_INT_CST_LOW (arg11);
5435 /* Move min of absolute values to int11. */
5436 if ((int01 >= 0 ? int01 : -int01)
5437 < (int11 >= 0 ? int11 : -int11))
5439 tmp = int01, int01 = int11, int11 = tmp;
5440 alt0 = arg00, arg00 = arg10, arg10 = alt0;
5441 alt0 = arg01, arg01 = arg11, arg11 = alt0;
5444 if (exact_log2 (int11) > 0 && int01 % int11 == 0)
5446 alt0 = fold (build (MULT_EXPR, type, arg00,
5447 build_int_2 (int01 / int11, 0)));
5448 alt1 = arg10;
5449 same = arg11;
5453 if (same)
5454 return fold (build (MULT_EXPR, type,
5455 fold (build (PLUS_EXPR, type, alt0, alt1)),
5456 same));
5459 /* In IEEE floating point, x+0 may not equal x. */
5460 else if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5461 || flag_fast_math)
5462 && real_zerop (arg1))
5463 return non_lvalue (convert (type, arg0));
5464 /* x+(-0) equals x, even for IEEE. */
5465 else if (TREE_CODE (arg1) == REAL_CST
5466 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1)))
5467 return non_lvalue (convert (type, arg0));
5469 bit_rotate:
5470 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
5471 is a rotate of A by C1 bits. */
5472 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
5473 is a rotate of A by B bits. */
5475 register enum tree_code code0, code1;
5476 code0 = TREE_CODE (arg0);
5477 code1 = TREE_CODE (arg1);
5478 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
5479 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
5480 && operand_equal_p (TREE_OPERAND (arg0, 0),
5481 TREE_OPERAND (arg1, 0), 0)
5482 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5484 register tree tree01, tree11;
5485 register enum tree_code code01, code11;
5487 tree01 = TREE_OPERAND (arg0, 1);
5488 tree11 = TREE_OPERAND (arg1, 1);
5489 STRIP_NOPS (tree01);
5490 STRIP_NOPS (tree11);
5491 code01 = TREE_CODE (tree01);
5492 code11 = TREE_CODE (tree11);
5493 if (code01 == INTEGER_CST
5494 && code11 == INTEGER_CST
5495 && TREE_INT_CST_HIGH (tree01) == 0
5496 && TREE_INT_CST_HIGH (tree11) == 0
5497 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
5498 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
5499 return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
5500 code0 == LSHIFT_EXPR ? tree01 : tree11);
5501 else if (code11 == MINUS_EXPR)
5503 tree tree110, tree111;
5504 tree110 = TREE_OPERAND (tree11, 0);
5505 tree111 = TREE_OPERAND (tree11, 1);
5506 STRIP_NOPS (tree110);
5507 STRIP_NOPS (tree111);
5508 if (TREE_CODE (tree110) == INTEGER_CST
5509 && 0 == compare_tree_int (tree110,
5510 TYPE_PRECISION
5511 (TREE_TYPE (TREE_OPERAND
5512 (arg0, 0))))
5513 && operand_equal_p (tree01, tree111, 0))
5514 return build ((code0 == LSHIFT_EXPR
5515 ? LROTATE_EXPR
5516 : RROTATE_EXPR),
5517 type, TREE_OPERAND (arg0, 0), tree01);
5519 else if (code01 == MINUS_EXPR)
5521 tree tree010, tree011;
5522 tree010 = TREE_OPERAND (tree01, 0);
5523 tree011 = TREE_OPERAND (tree01, 1);
5524 STRIP_NOPS (tree010);
5525 STRIP_NOPS (tree011);
5526 if (TREE_CODE (tree010) == INTEGER_CST
5527 && 0 == compare_tree_int (tree010,
5528 TYPE_PRECISION
5529 (TREE_TYPE (TREE_OPERAND
5530 (arg0, 0))))
5531 && operand_equal_p (tree11, tree011, 0))
5532 return build ((code0 != LSHIFT_EXPR
5533 ? LROTATE_EXPR
5534 : RROTATE_EXPR),
5535 type, TREE_OPERAND (arg0, 0), tree11);
5540 associate:
5541 /* In most languages, can't associate operations on floats through
5542 parentheses. Rather than remember where the parentheses were, we
5543 don't associate floats at all. It shouldn't matter much. However,
5544 associating multiplications is only very slightly inaccurate, so do
5545 that if -ffast-math is specified. */
5547 if (! wins
5548 && (! FLOAT_TYPE_P (type)
5549 || (flag_fast_math && code != MULT_EXPR)))
5551 tree var0, con0, lit0, var1, con1, lit1;
5553 /* Split both trees into variables, constants, and literals. Then
5554 associate each group together, the constants with literals,
5555 then the result with variables. This increases the chances of
5556 literals being recombined later and of generating relocatable
5557 expressions for the sum of a constant and literal. */
5558 var0 = split_tree (arg0, code, &con0, &lit0, 0);
5559 var1 = split_tree (arg1, code, &con1, &lit1, code == MINUS_EXPR);
5561 /* Only do something if we found more than two objects. Otherwise,
5562 nothing has changed and we risk infinite recursion. */
5563 if (2 < ((var0 != 0) + (var1 != 0) + (con0 != 0) + (con1 != 0)
5564 + (lit0 != 0) + (lit1 != 0)))
5566 var0 = associate_trees (var0, var1, code, type);
5567 con0 = associate_trees (con0, con1, code, type);
5568 lit0 = associate_trees (lit0, lit1, code, type);
5569 con0 = associate_trees (con0, lit0, code, type);
5570 return convert (type, associate_trees (var0, con0, code, type));
5574 binary:
5575 #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
5576 if (TREE_CODE (arg1) == REAL_CST)
5577 return t;
5578 #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
5579 if (wins)
5580 t1 = const_binop (code, arg0, arg1, 0);
5581 if (t1 != NULL_TREE)
5583 /* The return value should always have
5584 the same type as the original expression. */
5585 if (TREE_TYPE (t1) != TREE_TYPE (t))
5586 t1 = convert (TREE_TYPE (t), t1);
5588 return t1;
5590 return t;
5592 case MINUS_EXPR:
5593 /* A - (-B) -> A + B */
5594 if (TREE_CODE (arg1) == NEGATE_EXPR)
5595 return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5596 /* (-A) - CST -> (-CST) - A for floating point (what about ints ?) */
5597 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == REAL_CST)
5598 return
5599 fold (build (MINUS_EXPR, type,
5600 build_real (TREE_TYPE (arg1),
5601 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1))),
5602 TREE_OPERAND (arg0, 0)));
5604 if (! FLOAT_TYPE_P (type))
5606 if (! wins && integer_zerop (arg0))
5607 return negate_expr (convert (type, arg1));
5608 if (integer_zerop (arg1))
5609 return non_lvalue (convert (type, arg0));
5611 /* (A * C) - (B * C) -> (A-B) * C. Since we are most concerned
5612 about the case where C is a constant, just try one of the
5613 four possibilities. */
5615 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
5616 && operand_equal_p (TREE_OPERAND (arg0, 1),
5617 TREE_OPERAND (arg1, 1), 0))
5618 return fold (build (MULT_EXPR, type,
5619 fold (build (MINUS_EXPR, type,
5620 TREE_OPERAND (arg0, 0),
5621 TREE_OPERAND (arg1, 0))),
5622 TREE_OPERAND (arg0, 1)));
5625 else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5626 || flag_fast_math)
5628 /* Except with IEEE floating point, 0-x equals -x. */
5629 if (! wins && real_zerop (arg0))
5630 return negate_expr (convert (type, arg1));
5631 /* Except with IEEE floating point, x-0 equals x. */
5632 if (real_zerop (arg1))
5633 return non_lvalue (convert (type, arg0));
5636 /* Fold &x - &x. This can happen from &x.foo - &x.
5637 This is unsafe for certain floats even in non-IEEE formats.
5638 In IEEE, it is unsafe because it does wrong for NaNs.
5639 Also note that operand_equal_p is always false if an operand
5640 is volatile. */
5642 if ((! FLOAT_TYPE_P (type) || flag_fast_math)
5643 && operand_equal_p (arg0, arg1, 0))
5644 return convert (type, integer_zero_node);
5646 goto associate;
5648 case MULT_EXPR:
5649 /* (-A) * (-B) -> A * B */
5650 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
5651 return fold (build (MULT_EXPR, type, TREE_OPERAND (arg0, 0),
5652 TREE_OPERAND (arg1, 0)));
5654 if (! FLOAT_TYPE_P (type))
5656 if (integer_zerop (arg1))
5657 return omit_one_operand (type, arg1, arg0);
5658 if (integer_onep (arg1))
5659 return non_lvalue (convert (type, arg0));
5661 /* (a * (1 << b)) is (a << b) */
5662 if (TREE_CODE (arg1) == LSHIFT_EXPR
5663 && integer_onep (TREE_OPERAND (arg1, 0)))
5664 return fold (build (LSHIFT_EXPR, type, arg0,
5665 TREE_OPERAND (arg1, 1)));
5666 if (TREE_CODE (arg0) == LSHIFT_EXPR
5667 && integer_onep (TREE_OPERAND (arg0, 0)))
5668 return fold (build (LSHIFT_EXPR, type, arg1,
5669 TREE_OPERAND (arg0, 1)));
5671 if (TREE_CODE (arg1) == INTEGER_CST
5672 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5673 code, NULL_TREE)))
5674 return convert (type, tem);
5677 else
5679 /* x*0 is 0, except for IEEE floating point. */
5680 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5681 || flag_fast_math)
5682 && real_zerop (arg1))
5683 return omit_one_operand (type, arg1, arg0);
5684 /* In IEEE floating point, x*1 is not equivalent to x for snans.
5685 However, ANSI says we can drop signals,
5686 so we can do this anyway. */
5687 if (real_onep (arg1))
5688 return non_lvalue (convert (type, arg0));
5689 /* x*2 is x+x */
5690 if (! wins && real_twop (arg1) && global_bindings_p () == 0
5691 && ! contains_placeholder_p (arg0))
5693 tree arg = save_expr (arg0);
5694 return build (PLUS_EXPR, type, arg, arg);
5697 goto associate;
5699 case BIT_IOR_EXPR:
5700 bit_ior:
5701 if (integer_all_onesp (arg1))
5702 return omit_one_operand (type, arg1, arg0);
5703 if (integer_zerop (arg1))
5704 return non_lvalue (convert (type, arg0));
5705 t1 = distribute_bit_expr (code, type, arg0, arg1);
5706 if (t1 != NULL_TREE)
5707 return t1;
5709 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
5711 This results in more efficient code for machines without a NAND
5712 instruction. Combine will canonicalize to the first form
5713 which will allow use of NAND instructions provided by the
5714 backend if they exist. */
5715 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5716 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5718 return fold (build1 (BIT_NOT_EXPR, type,
5719 build (BIT_AND_EXPR, type,
5720 TREE_OPERAND (arg0, 0),
5721 TREE_OPERAND (arg1, 0))));
5724 /* See if this can be simplified into a rotate first. If that
5725 is unsuccessful continue in the association code. */
5726 goto bit_rotate;
5728 case BIT_XOR_EXPR:
5729 if (integer_zerop (arg1))
5730 return non_lvalue (convert (type, arg0));
5731 if (integer_all_onesp (arg1))
5732 return fold (build1 (BIT_NOT_EXPR, type, arg0));
5734 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
5735 with a constant, and the two constants have no bits in common,
5736 we should treat this as a BIT_IOR_EXPR since this may produce more
5737 simplifications. */
5738 if (TREE_CODE (arg0) == BIT_AND_EXPR
5739 && TREE_CODE (arg1) == BIT_AND_EXPR
5740 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5741 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5742 && integer_zerop (const_binop (BIT_AND_EXPR,
5743 TREE_OPERAND (arg0, 1),
5744 TREE_OPERAND (arg1, 1), 0)))
5746 code = BIT_IOR_EXPR;
5747 goto bit_ior;
5750 /* See if this can be simplified into a rotate first. If that
5751 is unsuccessful continue in the association code. */
5752 goto bit_rotate;
5754 case BIT_AND_EXPR:
5755 bit_and:
5756 if (integer_all_onesp (arg1))
5757 return non_lvalue (convert (type, arg0));
5758 if (integer_zerop (arg1))
5759 return omit_one_operand (type, arg1, arg0);
5760 t1 = distribute_bit_expr (code, type, arg0, arg1);
5761 if (t1 != NULL_TREE)
5762 return t1;
5763 /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */
5764 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
5765 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
5767 unsigned int prec
5768 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
5770 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5771 && (~TREE_INT_CST_LOW (arg0)
5772 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
5773 return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
5775 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
5776 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5778 unsigned int prec
5779 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
5781 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5782 && (~TREE_INT_CST_LOW (arg1)
5783 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
5784 return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
5787 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
5789 This results in more efficient code for machines without a NOR
5790 instruction. Combine will canonicalize to the first form
5791 which will allow use of NOR instructions provided by the
5792 backend if they exist. */
5793 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5794 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5796 return fold (build1 (BIT_NOT_EXPR, type,
5797 build (BIT_IOR_EXPR, type,
5798 TREE_OPERAND (arg0, 0),
5799 TREE_OPERAND (arg1, 0))));
5802 goto associate;
5804 case BIT_ANDTC_EXPR:
5805 if (integer_all_onesp (arg0))
5806 return non_lvalue (convert (type, arg1));
5807 if (integer_zerop (arg0))
5808 return omit_one_operand (type, arg0, arg1);
5809 if (TREE_CODE (arg1) == INTEGER_CST)
5811 arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
5812 code = BIT_AND_EXPR;
5813 goto bit_and;
5815 goto binary;
5817 case RDIV_EXPR:
5818 /* In most cases, do nothing with a divide by zero. */
5819 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
5820 #ifndef REAL_INFINITY
5821 if (TREE_CODE (arg1) == REAL_CST && real_zerop (arg1))
5822 return t;
5823 #endif
5824 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
5826 /* (-A) / (-B) -> A / B */
5827 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
5828 return fold (build (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
5829 TREE_OPERAND (arg1, 0)));
5831 /* In IEEE floating point, x/1 is not equivalent to x for snans.
5832 However, ANSI says we can drop signals, so we can do this anyway. */
5833 if (real_onep (arg1))
5834 return non_lvalue (convert (type, arg0));
5836 /* If ARG1 is a constant, we can convert this to a multiply by the
5837 reciprocal. This does not have the same rounding properties,
5838 so only do this if -ffast-math. We can actually always safely
5839 do it if ARG1 is a power of two, but it's hard to tell if it is
5840 or not in a portable manner. */
5841 if (TREE_CODE (arg1) == REAL_CST)
5843 if (flag_fast_math
5844 && 0 != (tem = const_binop (code, build_real (type, dconst1),
5845 arg1, 0)))
5846 return fold (build (MULT_EXPR, type, arg0, tem));
5847 /* Find the reciprocal if optimizing and the result is exact. */
5848 else if (optimize)
5850 REAL_VALUE_TYPE r;
5851 r = TREE_REAL_CST (arg1);
5852 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
5854 tem = build_real (type, r);
5855 return fold (build (MULT_EXPR, type, arg0, tem));
5859 goto binary;
5861 case TRUNC_DIV_EXPR:
5862 case ROUND_DIV_EXPR:
5863 case FLOOR_DIV_EXPR:
5864 case CEIL_DIV_EXPR:
5865 case EXACT_DIV_EXPR:
5866 if (integer_onep (arg1))
5867 return non_lvalue (convert (type, arg0));
5868 if (integer_zerop (arg1))
5869 return t;
5871 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
5872 operation, EXACT_DIV_EXPR.
5874 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
5875 At one time others generated faster code, it's not clear if they do
5876 after the last round to changes to the DIV code in expmed.c. */
5877 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
5878 && multiple_of_p (type, arg0, arg1))
5879 return fold (build (EXACT_DIV_EXPR, type, arg0, arg1));
5881 if (TREE_CODE (arg1) == INTEGER_CST
5882 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5883 code, NULL_TREE)))
5884 return convert (type, tem);
5886 goto binary;
5888 case CEIL_MOD_EXPR:
5889 case FLOOR_MOD_EXPR:
5890 case ROUND_MOD_EXPR:
5891 case TRUNC_MOD_EXPR:
5892 if (integer_onep (arg1))
5893 return omit_one_operand (type, integer_zero_node, arg0);
5894 if (integer_zerop (arg1))
5895 return t;
5897 if (TREE_CODE (arg1) == INTEGER_CST
5898 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5899 code, NULL_TREE)))
5900 return convert (type, tem);
5902 goto binary;
5904 case LSHIFT_EXPR:
5905 case RSHIFT_EXPR:
5906 case LROTATE_EXPR:
5907 case RROTATE_EXPR:
5908 if (integer_zerop (arg1))
5909 return non_lvalue (convert (type, arg0));
5910 /* Since negative shift count is not well-defined,
5911 don't try to compute it in the compiler. */
5912 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
5913 return t;
5914 /* Rewrite an LROTATE_EXPR by a constant into an
5915 RROTATE_EXPR by a new constant. */
5916 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
5918 TREE_SET_CODE (t, RROTATE_EXPR);
5919 code = RROTATE_EXPR;
5920 TREE_OPERAND (t, 1) = arg1
5921 = const_binop
5922 (MINUS_EXPR,
5923 convert (TREE_TYPE (arg1),
5924 build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0)),
5925 arg1, 0);
5926 if (tree_int_cst_sgn (arg1) < 0)
5927 return t;
5930 /* If we have a rotate of a bit operation with the rotate count and
5931 the second operand of the bit operation both constant,
5932 permute the two operations. */
5933 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
5934 && (TREE_CODE (arg0) == BIT_AND_EXPR
5935 || TREE_CODE (arg0) == BIT_ANDTC_EXPR
5936 || TREE_CODE (arg0) == BIT_IOR_EXPR
5937 || TREE_CODE (arg0) == BIT_XOR_EXPR)
5938 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
5939 return fold (build (TREE_CODE (arg0), type,
5940 fold (build (code, type,
5941 TREE_OPERAND (arg0, 0), arg1)),
5942 fold (build (code, type,
5943 TREE_OPERAND (arg0, 1), arg1))));
5945 /* Two consecutive rotates adding up to the width of the mode can
5946 be ignored. */
5947 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
5948 && TREE_CODE (arg0) == RROTATE_EXPR
5949 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5950 && TREE_INT_CST_HIGH (arg1) == 0
5951 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
5952 && ((TREE_INT_CST_LOW (arg1)
5953 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
5954 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
5955 return TREE_OPERAND (arg0, 0);
5957 goto binary;
5959 case MIN_EXPR:
5960 if (operand_equal_p (arg0, arg1, 0))
5961 return omit_one_operand (type, arg0, arg1);
5962 if (INTEGRAL_TYPE_P (type)
5963 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
5964 return omit_one_operand (type, arg1, arg0);
5965 goto associate;
5967 case MAX_EXPR:
5968 if (operand_equal_p (arg0, arg1, 0))
5969 return omit_one_operand (type, arg0, arg1);
5970 if (INTEGRAL_TYPE_P (type)
5971 && TYPE_MAX_VALUE (type)
5972 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
5973 return omit_one_operand (type, arg1, arg0);
5974 goto associate;
5976 case TRUTH_NOT_EXPR:
5977 /* Note that the operand of this must be an int
5978 and its values must be 0 or 1.
5979 ("true" is a fixed value perhaps depending on the language,
5980 but we don't handle values other than 1 correctly yet.) */
5981 tem = invert_truthvalue (arg0);
5982 /* Avoid infinite recursion. */
5983 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
5984 return t;
5985 return convert (type, tem);
5987 case TRUTH_ANDIF_EXPR:
5988 /* Note that the operands of this must be ints
5989 and their values must be 0 or 1.
5990 ("true" is a fixed value perhaps depending on the language.) */
5991 /* If first arg is constant zero, return it. */
5992 if (integer_zerop (arg0))
5993 return convert (type, arg0);
5994 case TRUTH_AND_EXPR:
5995 /* If either arg is constant true, drop it. */
5996 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
5997 return non_lvalue (convert (type, arg1));
5998 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
5999 /* Preserve sequence points. */
6000 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
6001 return non_lvalue (convert (type, arg0));
6002 /* If second arg is constant zero, result is zero, but first arg
6003 must be evaluated. */
6004 if (integer_zerop (arg1))
6005 return omit_one_operand (type, arg1, arg0);
6006 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
6007 case will be handled here. */
6008 if (integer_zerop (arg0))
6009 return omit_one_operand (type, arg0, arg1);
6011 truth_andor:
6012 /* We only do these simplifications if we are optimizing. */
6013 if (!optimize)
6014 return t;
6016 /* Check for things like (A || B) && (A || C). We can convert this
6017 to A || (B && C). Note that either operator can be any of the four
6018 truth and/or operations and the transformation will still be
6019 valid. Also note that we only care about order for the
6020 ANDIF and ORIF operators. If B contains side effects, this
6021 might change the truth-value of A. */
6022 if (TREE_CODE (arg0) == TREE_CODE (arg1)
6023 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
6024 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
6025 || TREE_CODE (arg0) == TRUTH_AND_EXPR
6026 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
6027 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
6029 tree a00 = TREE_OPERAND (arg0, 0);
6030 tree a01 = TREE_OPERAND (arg0, 1);
6031 tree a10 = TREE_OPERAND (arg1, 0);
6032 tree a11 = TREE_OPERAND (arg1, 1);
6033 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
6034 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
6035 && (code == TRUTH_AND_EXPR
6036 || code == TRUTH_OR_EXPR));
6038 if (operand_equal_p (a00, a10, 0))
6039 return fold (build (TREE_CODE (arg0), type, a00,
6040 fold (build (code, type, a01, a11))));
6041 else if (commutative && operand_equal_p (a00, a11, 0))
6042 return fold (build (TREE_CODE (arg0), type, a00,
6043 fold (build (code, type, a01, a10))));
6044 else if (commutative && operand_equal_p (a01, a10, 0))
6045 return fold (build (TREE_CODE (arg0), type, a01,
6046 fold (build (code, type, a00, a11))));
6048 /* This case if tricky because we must either have commutative
6049 operators or else A10 must not have side-effects. */
6051 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
6052 && operand_equal_p (a01, a11, 0))
6053 return fold (build (TREE_CODE (arg0), type,
6054 fold (build (code, type, a00, a10)),
6055 a01));
6058 /* See if we can build a range comparison. */
6059 if (0 != (tem = fold_range_test (t)))
6060 return tem;
6062 /* Check for the possibility of merging component references. If our
6063 lhs is another similar operation, try to merge its rhs with our
6064 rhs. Then try to merge our lhs and rhs. */
6065 if (TREE_CODE (arg0) == code
6066 && 0 != (tem = fold_truthop (code, type,
6067 TREE_OPERAND (arg0, 1), arg1)))
6068 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6070 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
6071 return tem;
6073 return t;
6075 case TRUTH_ORIF_EXPR:
6076 /* Note that the operands of this must be ints
6077 and their values must be 0 or true.
6078 ("true" is a fixed value perhaps depending on the language.) */
6079 /* If first arg is constant true, return it. */
6080 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6081 return convert (type, arg0);
6082 case TRUTH_OR_EXPR:
6083 /* If either arg is constant zero, drop it. */
6084 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
6085 return non_lvalue (convert (type, arg1));
6086 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
6087 /* Preserve sequence points. */
6088 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
6089 return non_lvalue (convert (type, arg0));
6090 /* If second arg is constant true, result is true, but we must
6091 evaluate first arg. */
6092 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
6093 return omit_one_operand (type, arg1, arg0);
6094 /* Likewise for first arg, but note this only occurs here for
6095 TRUTH_OR_EXPR. */
6096 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6097 return omit_one_operand (type, arg0, arg1);
6098 goto truth_andor;
6100 case TRUTH_XOR_EXPR:
6101 /* If either arg is constant zero, drop it. */
6102 if (integer_zerop (arg0))
6103 return non_lvalue (convert (type, arg1));
6104 if (integer_zerop (arg1))
6105 return non_lvalue (convert (type, arg0));
6106 /* If either arg is constant true, this is a logical inversion. */
6107 if (integer_onep (arg0))
6108 return non_lvalue (convert (type, invert_truthvalue (arg1)));
6109 if (integer_onep (arg1))
6110 return non_lvalue (convert (type, invert_truthvalue (arg0)));
6111 return t;
6113 case EQ_EXPR:
6114 case NE_EXPR:
6115 case LT_EXPR:
6116 case GT_EXPR:
6117 case LE_EXPR:
6118 case GE_EXPR:
6119 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
6121 /* (-a) CMP (-b) -> b CMP a */
6122 if (TREE_CODE (arg0) == NEGATE_EXPR
6123 && TREE_CODE (arg1) == NEGATE_EXPR)
6124 return fold (build (code, type, TREE_OPERAND (arg1, 0),
6125 TREE_OPERAND (arg0, 0)));
6126 /* (-a) CMP CST -> a swap(CMP) (-CST) */
6127 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == REAL_CST)
6128 return
6129 fold (build
6130 (swap_tree_comparison (code), type,
6131 TREE_OPERAND (arg0, 0),
6132 build_real (TREE_TYPE (arg1),
6133 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1)))));
6134 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6135 /* a CMP (-0) -> a CMP 0 */
6136 if (TREE_CODE (arg1) == REAL_CST
6137 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1)))
6138 return fold (build (code, type, arg0,
6139 build_real (TREE_TYPE (arg1), dconst0)));
6142 /* If one arg is a constant integer, put it last. */
6143 if (TREE_CODE (arg0) == INTEGER_CST
6144 && TREE_CODE (arg1) != INTEGER_CST)
6146 TREE_OPERAND (t, 0) = arg1;
6147 TREE_OPERAND (t, 1) = arg0;
6148 arg0 = TREE_OPERAND (t, 0);
6149 arg1 = TREE_OPERAND (t, 1);
6150 code = swap_tree_comparison (code);
6151 TREE_SET_CODE (t, code);
6154 /* Convert foo++ == CONST into ++foo == CONST + INCR.
6155 First, see if one arg is constant; find the constant arg
6156 and the other one. */
6158 tree constop = 0, varop = NULL_TREE;
6159 int constopnum = -1;
6161 if (TREE_CONSTANT (arg1))
6162 constopnum = 1, constop = arg1, varop = arg0;
6163 if (TREE_CONSTANT (arg0))
6164 constopnum = 0, constop = arg0, varop = arg1;
6166 if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
6168 /* This optimization is invalid for ordered comparisons
6169 if CONST+INCR overflows or if foo+incr might overflow.
6170 This optimization is invalid for floating point due to rounding.
6171 For pointer types we assume overflow doesn't happen. */
6172 if (POINTER_TYPE_P (TREE_TYPE (varop))
6173 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
6174 && (code == EQ_EXPR || code == NE_EXPR)))
6176 tree newconst
6177 = fold (build (PLUS_EXPR, TREE_TYPE (varop),
6178 constop, TREE_OPERAND (varop, 1)));
6180 /* Do not overwrite the current varop to be a preincrement,
6181 create a new node so that we won't confuse our caller who
6182 might create trees and throw them away, reusing the
6183 arguments that they passed to build. This shows up in
6184 the THEN or ELSE parts of ?: being postincrements. */
6185 varop = build (PREINCREMENT_EXPR, TREE_TYPE (varop),
6186 TREE_OPERAND (varop, 0),
6187 TREE_OPERAND (varop, 1));
6189 /* If VAROP is a reference to a bitfield, we must mask
6190 the constant by the width of the field. */
6191 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6192 && DECL_BIT_FIELD(TREE_OPERAND
6193 (TREE_OPERAND (varop, 0), 1)))
6195 int size
6196 = TREE_INT_CST_LOW (DECL_SIZE
6197 (TREE_OPERAND
6198 (TREE_OPERAND (varop, 0), 1)));
6199 tree mask, unsigned_type;
6200 unsigned int precision;
6201 tree folded_compare;
6203 /* First check whether the comparison would come out
6204 always the same. If we don't do that we would
6205 change the meaning with the masking. */
6206 if (constopnum == 0)
6207 folded_compare = fold (build (code, type, constop,
6208 TREE_OPERAND (varop, 0)));
6209 else
6210 folded_compare = fold (build (code, type,
6211 TREE_OPERAND (varop, 0),
6212 constop));
6213 if (integer_zerop (folded_compare)
6214 || integer_onep (folded_compare))
6215 return omit_one_operand (type, folded_compare, varop);
6217 unsigned_type = type_for_size (size, 1);
6218 precision = TYPE_PRECISION (unsigned_type);
6219 mask = build_int_2 (~0, ~0);
6220 TREE_TYPE (mask) = unsigned_type;
6221 force_fit_type (mask, 0);
6222 mask = const_binop (RSHIFT_EXPR, mask,
6223 size_int (precision - size), 0);
6224 newconst = fold (build (BIT_AND_EXPR,
6225 TREE_TYPE (varop), newconst,
6226 convert (TREE_TYPE (varop),
6227 mask)));
6230 t = build (code, type,
6231 (constopnum == 0) ? newconst : varop,
6232 (constopnum == 1) ? newconst : varop);
6233 return t;
6236 else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
6238 if (POINTER_TYPE_P (TREE_TYPE (varop))
6239 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
6240 && (code == EQ_EXPR || code == NE_EXPR)))
6242 tree newconst
6243 = fold (build (MINUS_EXPR, TREE_TYPE (varop),
6244 constop, TREE_OPERAND (varop, 1)));
6246 /* Do not overwrite the current varop to be a predecrement,
6247 create a new node so that we won't confuse our caller who
6248 might create trees and throw them away, reusing the
6249 arguments that they passed to build. This shows up in
6250 the THEN or ELSE parts of ?: being postdecrements. */
6251 varop = build (PREDECREMENT_EXPR, TREE_TYPE (varop),
6252 TREE_OPERAND (varop, 0),
6253 TREE_OPERAND (varop, 1));
6255 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6256 && DECL_BIT_FIELD(TREE_OPERAND
6257 (TREE_OPERAND (varop, 0), 1)))
6259 int size
6260 = TREE_INT_CST_LOW (DECL_SIZE
6261 (TREE_OPERAND
6262 (TREE_OPERAND (varop, 0), 1)));
6263 tree mask, unsigned_type;
6264 unsigned int precision;
6265 tree folded_compare;
6267 if (constopnum == 0)
6268 folded_compare = fold (build (code, type, constop,
6269 TREE_OPERAND (varop, 0)));
6270 else
6271 folded_compare = fold (build (code, type,
6272 TREE_OPERAND (varop, 0),
6273 constop));
6274 if (integer_zerop (folded_compare)
6275 || integer_onep (folded_compare))
6276 return omit_one_operand (type, folded_compare, varop);
6278 unsigned_type = type_for_size (size, 1);
6279 precision = TYPE_PRECISION (unsigned_type);
6280 mask = build_int_2 (~0, ~0);
6281 TREE_TYPE (mask) = TREE_TYPE (varop);
6282 force_fit_type (mask, 0);
6283 mask = const_binop (RSHIFT_EXPR, mask,
6284 size_int (precision - size), 0);
6285 newconst = fold (build (BIT_AND_EXPR,
6286 TREE_TYPE (varop), newconst,
6287 convert (TREE_TYPE (varop),
6288 mask)));
6291 t = build (code, type,
6292 (constopnum == 0) ? newconst : varop,
6293 (constopnum == 1) ? newconst : varop);
6294 return t;
6299 /* Change X >= CST to X > (CST - 1) if CST is positive. */
6300 if (TREE_CODE (arg1) == INTEGER_CST
6301 && TREE_CODE (arg0) != INTEGER_CST
6302 && tree_int_cst_sgn (arg1) > 0)
6304 switch (TREE_CODE (t))
6306 case GE_EXPR:
6307 code = GT_EXPR;
6308 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6309 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6310 break;
6312 case LT_EXPR:
6313 code = LE_EXPR;
6314 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6315 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6316 break;
6318 default:
6319 break;
6323 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
6324 a MINUS_EXPR of a constant, we can convert it into a comparison with
6325 a revised constant as long as no overflow occurs. */
6326 if ((code == EQ_EXPR || code == NE_EXPR)
6327 && TREE_CODE (arg1) == INTEGER_CST
6328 && (TREE_CODE (arg0) == PLUS_EXPR
6329 || TREE_CODE (arg0) == MINUS_EXPR)
6330 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6331 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
6332 ? MINUS_EXPR : PLUS_EXPR,
6333 arg1, TREE_OPERAND (arg0, 1), 0))
6334 && ! TREE_CONSTANT_OVERFLOW (tem))
6335 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6337 /* Similarly for a NEGATE_EXPR. */
6338 else if ((code == EQ_EXPR || code == NE_EXPR)
6339 && TREE_CODE (arg0) == NEGATE_EXPR
6340 && TREE_CODE (arg1) == INTEGER_CST
6341 && 0 != (tem = negate_expr (arg1))
6342 && TREE_CODE (tem) == INTEGER_CST
6343 && ! TREE_CONSTANT_OVERFLOW (tem))
6344 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6346 /* If we have X - Y == 0, we can convert that to X == Y and similarly
6347 for !=. Don't do this for ordered comparisons due to overflow. */
6348 else if ((code == NE_EXPR || code == EQ_EXPR)
6349 && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
6350 return fold (build (code, type,
6351 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
6353 /* If we are widening one operand of an integer comparison,
6354 see if the other operand is similarly being widened. Perhaps we
6355 can do the comparison in the narrower type. */
6356 else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
6357 && TREE_CODE (arg0) == NOP_EXPR
6358 && (tem = get_unwidened (arg0, NULL_TREE)) != arg0
6359 && (t1 = get_unwidened (arg1, TREE_TYPE (tem))) != 0
6360 && (TREE_TYPE (t1) == TREE_TYPE (tem)
6361 || (TREE_CODE (t1) == INTEGER_CST
6362 && int_fits_type_p (t1, TREE_TYPE (tem)))))
6363 return fold (build (code, type, tem, convert (TREE_TYPE (tem), t1)));
6365 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
6366 constant, we can simplify it. */
6367 else if (TREE_CODE (arg1) == INTEGER_CST
6368 && (TREE_CODE (arg0) == MIN_EXPR
6369 || TREE_CODE (arg0) == MAX_EXPR)
6370 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
6371 return optimize_minmax_comparison (t);
6373 /* If we are comparing an ABS_EXPR with a constant, we can
6374 convert all the cases into explicit comparisons, but they may
6375 well not be faster than doing the ABS and one comparison.
6376 But ABS (X) <= C is a range comparison, which becomes a subtraction
6377 and a comparison, and is probably faster. */
6378 else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6379 && TREE_CODE (arg0) == ABS_EXPR
6380 && ! TREE_SIDE_EFFECTS (arg0)
6381 && (0 != (tem = negate_expr (arg1)))
6382 && TREE_CODE (tem) == INTEGER_CST
6383 && ! TREE_CONSTANT_OVERFLOW (tem))
6384 return fold (build (TRUTH_ANDIF_EXPR, type,
6385 build (GE_EXPR, type, TREE_OPERAND (arg0, 0), tem),
6386 build (LE_EXPR, type,
6387 TREE_OPERAND (arg0, 0), arg1)));
6389 /* If this is an EQ or NE comparison with zero and ARG0 is
6390 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
6391 two operations, but the latter can be done in one less insn
6392 on machines that have only two-operand insns or on which a
6393 constant cannot be the first operand. */
6394 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
6395 && TREE_CODE (arg0) == BIT_AND_EXPR)
6397 if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
6398 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
6399 return
6400 fold (build (code, type,
6401 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6402 build (RSHIFT_EXPR,
6403 TREE_TYPE (TREE_OPERAND (arg0, 0)),
6404 TREE_OPERAND (arg0, 1),
6405 TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
6406 convert (TREE_TYPE (arg0),
6407 integer_one_node)),
6408 arg1));
6409 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
6410 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
6411 return
6412 fold (build (code, type,
6413 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6414 build (RSHIFT_EXPR,
6415 TREE_TYPE (TREE_OPERAND (arg0, 1)),
6416 TREE_OPERAND (arg0, 0),
6417 TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
6418 convert (TREE_TYPE (arg0),
6419 integer_one_node)),
6420 arg1));
6423 /* If this is an NE or EQ comparison of zero against the result of a
6424 signed MOD operation whose second operand is a power of 2, make
6425 the MOD operation unsigned since it is simpler and equivalent. */
6426 if ((code == NE_EXPR || code == EQ_EXPR)
6427 && integer_zerop (arg1)
6428 && ! TREE_UNSIGNED (TREE_TYPE (arg0))
6429 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
6430 || TREE_CODE (arg0) == CEIL_MOD_EXPR
6431 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
6432 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
6433 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6435 tree newtype = unsigned_type (TREE_TYPE (arg0));
6436 tree newmod = build (TREE_CODE (arg0), newtype,
6437 convert (newtype, TREE_OPERAND (arg0, 0)),
6438 convert (newtype, TREE_OPERAND (arg0, 1)));
6440 return build (code, type, newmod, convert (newtype, arg1));
6443 /* If this is an NE comparison of zero with an AND of one, remove the
6444 comparison since the AND will give the correct value. */
6445 if (code == NE_EXPR && integer_zerop (arg1)
6446 && TREE_CODE (arg0) == BIT_AND_EXPR
6447 && integer_onep (TREE_OPERAND (arg0, 1)))
6448 return convert (type, arg0);
6450 /* If we have (A & C) == C where C is a power of 2, convert this into
6451 (A & C) != 0. Similarly for NE_EXPR. */
6452 if ((code == EQ_EXPR || code == NE_EXPR)
6453 && TREE_CODE (arg0) == BIT_AND_EXPR
6454 && integer_pow2p (TREE_OPERAND (arg0, 1))
6455 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
6456 return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
6457 arg0, integer_zero_node);
6459 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
6460 and similarly for >= into !=. */
6461 if ((code == LT_EXPR || code == GE_EXPR)
6462 && TREE_UNSIGNED (TREE_TYPE (arg0))
6463 && TREE_CODE (arg1) == LSHIFT_EXPR
6464 && integer_onep (TREE_OPERAND (arg1, 0)))
6465 return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
6466 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6467 TREE_OPERAND (arg1, 1)),
6468 convert (TREE_TYPE (arg0), integer_zero_node));
6470 else if ((code == LT_EXPR || code == GE_EXPR)
6471 && TREE_UNSIGNED (TREE_TYPE (arg0))
6472 && (TREE_CODE (arg1) == NOP_EXPR
6473 || TREE_CODE (arg1) == CONVERT_EXPR)
6474 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
6475 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
6476 return
6477 build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
6478 convert (TREE_TYPE (arg0),
6479 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6480 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))),
6481 convert (TREE_TYPE (arg0), integer_zero_node));
6483 /* Simplify comparison of something with itself. (For IEEE
6484 floating-point, we can only do some of these simplifications.) */
6485 if (operand_equal_p (arg0, arg1, 0))
6487 switch (code)
6489 case EQ_EXPR:
6490 case GE_EXPR:
6491 case LE_EXPR:
6492 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6493 return constant_boolean_node (1, type);
6494 code = EQ_EXPR;
6495 TREE_SET_CODE (t, code);
6496 break;
6498 case NE_EXPR:
6499 /* For NE, we can only do this simplification if integer. */
6500 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6501 break;
6502 /* ... fall through ... */
6503 case GT_EXPR:
6504 case LT_EXPR:
6505 return constant_boolean_node (0, type);
6506 default:
6507 abort ();
6511 /* An unsigned comparison against 0 can be simplified. */
6512 if (integer_zerop (arg1)
6513 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6514 || POINTER_TYPE_P (TREE_TYPE (arg1)))
6515 && TREE_UNSIGNED (TREE_TYPE (arg1)))
6517 switch (TREE_CODE (t))
6519 case GT_EXPR:
6520 code = NE_EXPR;
6521 TREE_SET_CODE (t, NE_EXPR);
6522 break;
6523 case LE_EXPR:
6524 code = EQ_EXPR;
6525 TREE_SET_CODE (t, EQ_EXPR);
6526 break;
6527 case GE_EXPR:
6528 return omit_one_operand (type,
6529 convert (type, integer_one_node),
6530 arg0);
6531 case LT_EXPR:
6532 return omit_one_operand (type,
6533 convert (type, integer_zero_node),
6534 arg0);
6535 default:
6536 break;
6540 /* Comparisons with the highest or lowest possible integer of
6541 the specified size will have known values and an unsigned
6542 <= 0x7fffffff can be simplified. */
6544 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
6546 if (TREE_CODE (arg1) == INTEGER_CST
6547 && ! TREE_CONSTANT_OVERFLOW (arg1)
6548 && width <= HOST_BITS_PER_WIDE_INT
6549 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6550 || POINTER_TYPE_P (TREE_TYPE (arg1))))
6552 if (TREE_INT_CST_HIGH (arg1) == 0
6553 && (TREE_INT_CST_LOW (arg1)
6554 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1)
6555 && ! TREE_UNSIGNED (TREE_TYPE (arg1)))
6556 switch (TREE_CODE (t))
6558 case GT_EXPR:
6559 return omit_one_operand (type,
6560 convert (type, integer_zero_node),
6561 arg0);
6562 case GE_EXPR:
6563 TREE_SET_CODE (t, EQ_EXPR);
6564 break;
6566 case LE_EXPR:
6567 return omit_one_operand (type,
6568 convert (type, integer_one_node),
6569 arg0);
6570 case LT_EXPR:
6571 TREE_SET_CODE (t, NE_EXPR);
6572 break;
6574 default:
6575 break;
6578 else if (TREE_INT_CST_HIGH (arg1) == -1
6579 && (- TREE_INT_CST_LOW (arg1)
6580 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)))
6581 && ! TREE_UNSIGNED (TREE_TYPE (arg1)))
6582 switch (TREE_CODE (t))
6584 case LT_EXPR:
6585 return omit_one_operand (type,
6586 convert (type, integer_zero_node),
6587 arg0);
6588 case LE_EXPR:
6589 TREE_SET_CODE (t, EQ_EXPR);
6590 break;
6592 case GE_EXPR:
6593 return omit_one_operand (type,
6594 convert (type, integer_one_node),
6595 arg0);
6596 case GT_EXPR:
6597 TREE_SET_CODE (t, NE_EXPR);
6598 break;
6600 default:
6601 break;
6604 else if (TREE_INT_CST_HIGH (arg1) == 0
6605 && (TREE_INT_CST_LOW (arg1)
6606 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1)
6607 && TREE_UNSIGNED (TREE_TYPE (arg1))
6608 /* signed_type does not work on pointer types. */
6609 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6611 switch (TREE_CODE (t))
6613 case LE_EXPR:
6614 return fold (build (GE_EXPR, type,
6615 convert (signed_type (TREE_TYPE (arg0)),
6616 arg0),
6617 convert (signed_type (TREE_TYPE (arg1)),
6618 integer_zero_node)));
6619 case GT_EXPR:
6620 return fold (build (LT_EXPR, type,
6621 convert (signed_type (TREE_TYPE (arg0)),
6622 arg0),
6623 convert (signed_type (TREE_TYPE (arg1)),
6624 integer_zero_node)));
6626 default:
6627 break;
6632 /* If we are comparing an expression that just has comparisons
6633 of two integer values, arithmetic expressions of those comparisons,
6634 and constants, we can simplify it. There are only three cases
6635 to check: the two values can either be equal, the first can be
6636 greater, or the second can be greater. Fold the expression for
6637 those three values. Since each value must be 0 or 1, we have
6638 eight possibilities, each of which corresponds to the constant 0
6639 or 1 or one of the six possible comparisons.
6641 This handles common cases like (a > b) == 0 but also handles
6642 expressions like ((x > y) - (y > x)) > 0, which supposedly
6643 occur in macroized code. */
6645 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
6647 tree cval1 = 0, cval2 = 0;
6648 int save_p = 0;
6650 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
6651 /* Don't handle degenerate cases here; they should already
6652 have been handled anyway. */
6653 && cval1 != 0 && cval2 != 0
6654 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
6655 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
6656 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
6657 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
6658 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
6659 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
6660 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
6662 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
6663 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
6665 /* We can't just pass T to eval_subst in case cval1 or cval2
6666 was the same as ARG1. */
6668 tree high_result
6669 = fold (build (code, type,
6670 eval_subst (arg0, cval1, maxval, cval2, minval),
6671 arg1));
6672 tree equal_result
6673 = fold (build (code, type,
6674 eval_subst (arg0, cval1, maxval, cval2, maxval),
6675 arg1));
6676 tree low_result
6677 = fold (build (code, type,
6678 eval_subst (arg0, cval1, minval, cval2, maxval),
6679 arg1));
6681 /* All three of these results should be 0 or 1. Confirm they
6682 are. Then use those values to select the proper code
6683 to use. */
6685 if ((integer_zerop (high_result)
6686 || integer_onep (high_result))
6687 && (integer_zerop (equal_result)
6688 || integer_onep (equal_result))
6689 && (integer_zerop (low_result)
6690 || integer_onep (low_result)))
6692 /* Make a 3-bit mask with the high-order bit being the
6693 value for `>', the next for '=', and the low for '<'. */
6694 switch ((integer_onep (high_result) * 4)
6695 + (integer_onep (equal_result) * 2)
6696 + integer_onep (low_result))
6698 case 0:
6699 /* Always false. */
6700 return omit_one_operand (type, integer_zero_node, arg0);
6701 case 1:
6702 code = LT_EXPR;
6703 break;
6704 case 2:
6705 code = EQ_EXPR;
6706 break;
6707 case 3:
6708 code = LE_EXPR;
6709 break;
6710 case 4:
6711 code = GT_EXPR;
6712 break;
6713 case 5:
6714 code = NE_EXPR;
6715 break;
6716 case 6:
6717 code = GE_EXPR;
6718 break;
6719 case 7:
6720 /* Always true. */
6721 return omit_one_operand (type, integer_one_node, arg0);
6724 t = build (code, type, cval1, cval2);
6725 if (save_p)
6726 return save_expr (t);
6727 else
6728 return fold (t);
6733 /* If this is a comparison of a field, we may be able to simplify it. */
6734 if ((TREE_CODE (arg0) == COMPONENT_REF
6735 || TREE_CODE (arg0) == BIT_FIELD_REF)
6736 && (code == EQ_EXPR || code == NE_EXPR)
6737 /* Handle the constant case even without -O
6738 to make sure the warnings are given. */
6739 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
6741 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
6742 return t1 ? t1 : t;
6745 /* If this is a comparison of complex values and either or both sides
6746 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
6747 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
6748 This may prevent needless evaluations. */
6749 if ((code == EQ_EXPR || code == NE_EXPR)
6750 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
6751 && (TREE_CODE (arg0) == COMPLEX_EXPR
6752 || TREE_CODE (arg1) == COMPLEX_EXPR
6753 || TREE_CODE (arg0) == COMPLEX_CST
6754 || TREE_CODE (arg1) == COMPLEX_CST))
6756 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
6757 tree real0, imag0, real1, imag1;
6759 arg0 = save_expr (arg0);
6760 arg1 = save_expr (arg1);
6761 real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
6762 imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
6763 real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
6764 imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
6766 return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
6767 : TRUTH_ORIF_EXPR),
6768 type,
6769 fold (build (code, type, real0, real1)),
6770 fold (build (code, type, imag0, imag1))));
6773 /* From here on, the only cases we handle are when the result is
6774 known to be a constant.
6776 To compute GT, swap the arguments and do LT.
6777 To compute GE, do LT and invert the result.
6778 To compute LE, swap the arguments, do LT and invert the result.
6779 To compute NE, do EQ and invert the result.
6781 Therefore, the code below must handle only EQ and LT. */
6783 if (code == LE_EXPR || code == GT_EXPR)
6785 tem = arg0, arg0 = arg1, arg1 = tem;
6786 code = swap_tree_comparison (code);
6789 /* Note that it is safe to invert for real values here because we
6790 will check below in the one case that it matters. */
6792 t1 = NULL_TREE;
6793 invert = 0;
6794 if (code == NE_EXPR || code == GE_EXPR)
6796 invert = 1;
6797 code = invert_tree_comparison (code);
6800 /* Compute a result for LT or EQ if args permit;
6801 otherwise return T. */
6802 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
6804 if (code == EQ_EXPR)
6805 t1 = build_int_2 (tree_int_cst_equal (arg0, arg1), 0);
6806 else
6807 t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
6808 ? INT_CST_LT_UNSIGNED (arg0, arg1)
6809 : INT_CST_LT (arg0, arg1)),
6813 #if 0 /* This is no longer useful, but breaks some real code. */
6814 /* Assume a nonexplicit constant cannot equal an explicit one,
6815 since such code would be undefined anyway.
6816 Exception: on sysvr4, using #pragma weak,
6817 a label can come out as 0. */
6818 else if (TREE_CODE (arg1) == INTEGER_CST
6819 && !integer_zerop (arg1)
6820 && TREE_CONSTANT (arg0)
6821 && TREE_CODE (arg0) == ADDR_EXPR
6822 && code == EQ_EXPR)
6823 t1 = build_int_2 (0, 0);
6824 #endif
6825 /* Two real constants can be compared explicitly. */
6826 else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
6828 /* If either operand is a NaN, the result is false with two
6829 exceptions: First, an NE_EXPR is true on NaNs, but that case
6830 is already handled correctly since we will be inverting the
6831 result for NE_EXPR. Second, if we had inverted a LE_EXPR
6832 or a GE_EXPR into a LT_EXPR, we must return true so that it
6833 will be inverted into false. */
6835 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
6836 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
6837 t1 = build_int_2 (invert && code == LT_EXPR, 0);
6839 else if (code == EQ_EXPR)
6840 t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
6841 TREE_REAL_CST (arg1)),
6843 else
6844 t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
6845 TREE_REAL_CST (arg1)),
6849 if (t1 == NULL_TREE)
6850 return t;
6852 if (invert)
6853 TREE_INT_CST_LOW (t1) ^= 1;
6855 TREE_TYPE (t1) = type;
6856 if (TREE_CODE (type) == BOOLEAN_TYPE)
6857 return truthvalue_conversion (t1);
6858 return t1;
6860 case COND_EXPR:
6861 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
6862 so all simple results must be passed through pedantic_non_lvalue. */
6863 if (TREE_CODE (arg0) == INTEGER_CST)
6864 return pedantic_non_lvalue
6865 (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
6866 else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
6867 return pedantic_omit_one_operand (type, arg1, arg0);
6869 /* If the second operand is zero, invert the comparison and swap
6870 the second and third operands. Likewise if the second operand
6871 is constant and the third is not or if the third operand is
6872 equivalent to the first operand of the comparison. */
6874 if (integer_zerop (arg1)
6875 || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
6876 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
6877 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
6878 TREE_OPERAND (t, 2),
6879 TREE_OPERAND (arg0, 1))))
6881 /* See if this can be inverted. If it can't, possibly because
6882 it was a floating-point inequality comparison, don't do
6883 anything. */
6884 tem = invert_truthvalue (arg0);
6886 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
6888 t = build (code, type, tem,
6889 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
6890 arg0 = tem;
6891 /* arg1 should be the first argument of the new T. */
6892 arg1 = TREE_OPERAND (t, 1);
6893 STRIP_NOPS (arg1);
6897 /* If we have A op B ? A : C, we may be able to convert this to a
6898 simpler expression, depending on the operation and the values
6899 of B and C. IEEE floating point prevents this though,
6900 because A or B might be -0.0 or a NaN. */
6902 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
6903 && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
6904 || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
6905 || flag_fast_math)
6906 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
6907 arg1, TREE_OPERAND (arg0, 1)))
6909 tree arg2 = TREE_OPERAND (t, 2);
6910 enum tree_code comp_code = TREE_CODE (arg0);
6912 STRIP_NOPS (arg2);
6914 /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
6915 depending on the comparison operation. */
6916 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
6917 ? real_zerop (TREE_OPERAND (arg0, 1))
6918 : integer_zerop (TREE_OPERAND (arg0, 1)))
6919 && TREE_CODE (arg2) == NEGATE_EXPR
6920 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6921 switch (comp_code)
6923 case EQ_EXPR:
6924 return
6925 pedantic_non_lvalue
6926 (convert (type,
6927 negate_expr
6928 (convert (TREE_TYPE (TREE_OPERAND (t, 1)),
6929 arg1))));
6931 case NE_EXPR:
6932 return pedantic_non_lvalue (convert (type, arg1));
6933 case GE_EXPR:
6934 case GT_EXPR:
6935 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
6936 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
6937 return pedantic_non_lvalue
6938 (convert (type, fold (build1 (ABS_EXPR,
6939 TREE_TYPE (arg1), arg1))));
6940 case LE_EXPR:
6941 case LT_EXPR:
6942 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
6943 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
6944 return pedantic_non_lvalue
6945 (negate_expr (convert (type,
6946 fold (build1 (ABS_EXPR,
6947 TREE_TYPE (arg1),
6948 arg1)))));
6949 default:
6950 abort ();
6953 /* If this is A != 0 ? A : 0, this is simply A. For ==, it is
6954 always zero. */
6956 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
6958 if (comp_code == NE_EXPR)
6959 return pedantic_non_lvalue (convert (type, arg1));
6960 else if (comp_code == EQ_EXPR)
6961 return pedantic_non_lvalue (convert (type, integer_zero_node));
6964 /* If this is A op B ? A : B, this is either A, B, min (A, B),
6965 or max (A, B), depending on the operation. */
6967 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
6968 arg2, TREE_OPERAND (arg0, 0)))
6970 tree comp_op0 = TREE_OPERAND (arg0, 0);
6971 tree comp_op1 = TREE_OPERAND (arg0, 1);
6972 tree comp_type = TREE_TYPE (comp_op0);
6974 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
6975 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
6976 comp_type = type;
6978 switch (comp_code)
6980 case EQ_EXPR:
6981 return pedantic_non_lvalue (convert (type, arg2));
6982 case NE_EXPR:
6983 return pedantic_non_lvalue (convert (type, arg1));
6984 case LE_EXPR:
6985 case LT_EXPR:
6986 /* In C++ a ?: expression can be an lvalue, so put the
6987 operand which will be used if they are equal first
6988 so that we can convert this back to the
6989 corresponding COND_EXPR. */
6990 return pedantic_non_lvalue
6991 (convert (type, fold (build (MIN_EXPR, comp_type,
6992 (comp_code == LE_EXPR
6993 ? comp_op0 : comp_op1),
6994 (comp_code == LE_EXPR
6995 ? comp_op1 : comp_op0)))));
6996 break;
6997 case GE_EXPR:
6998 case GT_EXPR:
6999 return pedantic_non_lvalue
7000 (convert (type, fold (build (MAX_EXPR, comp_type,
7001 (comp_code == GE_EXPR
7002 ? comp_op0 : comp_op1),
7003 (comp_code == GE_EXPR
7004 ? comp_op1 : comp_op0)))));
7005 break;
7006 default:
7007 abort ();
7011 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
7012 we might still be able to simplify this. For example,
7013 if C1 is one less or one more than C2, this might have started
7014 out as a MIN or MAX and been transformed by this function.
7015 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
7017 if (INTEGRAL_TYPE_P (type)
7018 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7019 && TREE_CODE (arg2) == INTEGER_CST)
7020 switch (comp_code)
7022 case EQ_EXPR:
7023 /* We can replace A with C1 in this case. */
7024 arg1 = convert (type, TREE_OPERAND (arg0, 1));
7025 t = build (code, type, TREE_OPERAND (t, 0), arg1,
7026 TREE_OPERAND (t, 2));
7027 break;
7029 case LT_EXPR:
7030 /* If C1 is C2 + 1, this is min(A, C2). */
7031 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
7032 && operand_equal_p (TREE_OPERAND (arg0, 1),
7033 const_binop (PLUS_EXPR, arg2,
7034 integer_one_node, 0), 1))
7035 return pedantic_non_lvalue
7036 (fold (build (MIN_EXPR, type, arg1, arg2)));
7037 break;
7039 case LE_EXPR:
7040 /* If C1 is C2 - 1, this is min(A, C2). */
7041 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
7042 && operand_equal_p (TREE_OPERAND (arg0, 1),
7043 const_binop (MINUS_EXPR, arg2,
7044 integer_one_node, 0), 1))
7045 return pedantic_non_lvalue
7046 (fold (build (MIN_EXPR, type, arg1, arg2)));
7047 break;
7049 case GT_EXPR:
7050 /* If C1 is C2 - 1, this is max(A, C2). */
7051 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
7052 && operand_equal_p (TREE_OPERAND (arg0, 1),
7053 const_binop (MINUS_EXPR, arg2,
7054 integer_one_node, 0), 1))
7055 return pedantic_non_lvalue
7056 (fold (build (MAX_EXPR, type, arg1, arg2)));
7057 break;
7059 case GE_EXPR:
7060 /* If C1 is C2 + 1, this is max(A, C2). */
7061 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
7062 && operand_equal_p (TREE_OPERAND (arg0, 1),
7063 const_binop (PLUS_EXPR, arg2,
7064 integer_one_node, 0), 1))
7065 return pedantic_non_lvalue
7066 (fold (build (MAX_EXPR, type, arg1, arg2)));
7067 break;
7068 case NE_EXPR:
7069 break;
7070 default:
7071 abort ();
7075 /* If the second operand is simpler than the third, swap them
7076 since that produces better jump optimization results. */
7077 if ((TREE_CONSTANT (arg1) || DECL_P (arg1)
7078 || TREE_CODE (arg1) == SAVE_EXPR)
7079 && ! (TREE_CONSTANT (TREE_OPERAND (t, 2))
7080 || DECL_P (TREE_OPERAND (t, 2))
7081 || TREE_CODE (TREE_OPERAND (t, 2)) == SAVE_EXPR))
7083 /* See if this can be inverted. If it can't, possibly because
7084 it was a floating-point inequality comparison, don't do
7085 anything. */
7086 tem = invert_truthvalue (arg0);
7088 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
7090 t = build (code, type, tem,
7091 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
7092 arg0 = tem;
7093 /* arg1 should be the first argument of the new T. */
7094 arg1 = TREE_OPERAND (t, 1);
7095 STRIP_NOPS (arg1);
7099 /* Convert A ? 1 : 0 to simply A. */
7100 if (integer_onep (TREE_OPERAND (t, 1))
7101 && integer_zerop (TREE_OPERAND (t, 2))
7102 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
7103 call to fold will try to move the conversion inside
7104 a COND, which will recurse. In that case, the COND_EXPR
7105 is probably the best choice, so leave it alone. */
7106 && type == TREE_TYPE (arg0))
7107 return pedantic_non_lvalue (arg0);
7109 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
7110 operation is simply A & 2. */
7112 if (integer_zerop (TREE_OPERAND (t, 2))
7113 && TREE_CODE (arg0) == NE_EXPR
7114 && integer_zerop (TREE_OPERAND (arg0, 1))
7115 && integer_pow2p (arg1)
7116 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
7117 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
7118 arg1, 1))
7119 return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
7121 return t;
7123 case COMPOUND_EXPR:
7124 /* When pedantic, a compound expression can be neither an lvalue
7125 nor an integer constant expression. */
7126 if (TREE_SIDE_EFFECTS (arg0) || pedantic)
7127 return t;
7128 /* Don't let (0, 0) be null pointer constant. */
7129 if (integer_zerop (arg1))
7130 return build1 (NOP_EXPR, type, arg1);
7131 return convert (type, arg1);
7133 case COMPLEX_EXPR:
7134 if (wins)
7135 return build_complex (type, arg0, arg1);
7136 return t;
7138 case REALPART_EXPR:
7139 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7140 return t;
7141 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7142 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
7143 TREE_OPERAND (arg0, 1));
7144 else if (TREE_CODE (arg0) == COMPLEX_CST)
7145 return TREE_REALPART (arg0);
7146 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7147 return fold (build (TREE_CODE (arg0), type,
7148 fold (build1 (REALPART_EXPR, type,
7149 TREE_OPERAND (arg0, 0))),
7150 fold (build1 (REALPART_EXPR,
7151 type, TREE_OPERAND (arg0, 1)))));
7152 return t;
7154 case IMAGPART_EXPR:
7155 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7156 return convert (type, integer_zero_node);
7157 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7158 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
7159 TREE_OPERAND (arg0, 0));
7160 else if (TREE_CODE (arg0) == COMPLEX_CST)
7161 return TREE_IMAGPART (arg0);
7162 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7163 return fold (build (TREE_CODE (arg0), type,
7164 fold (build1 (IMAGPART_EXPR, type,
7165 TREE_OPERAND (arg0, 0))),
7166 fold (build1 (IMAGPART_EXPR, type,
7167 TREE_OPERAND (arg0, 1)))));
7168 return t;
7170 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
7171 appropriate. */
7172 case CLEANUP_POINT_EXPR:
7173 if (! has_cleanups (arg0))
7174 return TREE_OPERAND (t, 0);
7177 enum tree_code code0 = TREE_CODE (arg0);
7178 int kind0 = TREE_CODE_CLASS (code0);
7179 tree arg00 = TREE_OPERAND (arg0, 0);
7180 tree arg01;
7182 if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
7183 return fold (build1 (code0, type,
7184 fold (build1 (CLEANUP_POINT_EXPR,
7185 TREE_TYPE (arg00), arg00))));
7187 if (kind0 == '<' || kind0 == '2'
7188 || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
7189 || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR
7190 || code0 == TRUTH_XOR_EXPR)
7192 arg01 = TREE_OPERAND (arg0, 1);
7194 if (TREE_CONSTANT (arg00)
7195 || ((code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR)
7196 && ! has_cleanups (arg00)))
7197 return fold (build (code0, type, arg00,
7198 fold (build1 (CLEANUP_POINT_EXPR,
7199 TREE_TYPE (arg01), arg01))));
7201 if (TREE_CONSTANT (arg01))
7202 return fold (build (code0, type,
7203 fold (build1 (CLEANUP_POINT_EXPR,
7204 TREE_TYPE (arg00), arg00)),
7205 arg01));
7208 return t;
7211 case CALL_EXPR:
7212 /* Check for a built-in function. */
7213 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR
7214 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (expr, 0), 0))
7215 == FUNCTION_DECL)
7216 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (expr, 0), 0)))
7218 tree tmp = fold_builtin (expr);
7219 if (tmp)
7220 return tmp;
7222 return t;
7224 default:
7225 return t;
7226 } /* switch (code) */
7229 /* Determine if first argument is a multiple of second argument. Return 0 if
7230 it is not, or we cannot easily determined it to be.
7232 An example of the sort of thing we care about (at this point; this routine
7233 could surely be made more general, and expanded to do what the *_DIV_EXPR's
7234 fold cases do now) is discovering that
7236 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7238 is a multiple of
7240 SAVE_EXPR (J * 8)
7242 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
7244 This code also handles discovering that
7246 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7248 is a multiple of 8 so we don't have to worry about dealing with a
7249 possible remainder.
7251 Note that we *look* inside a SAVE_EXPR only to determine how it was
7252 calculated; it is not safe for fold to do much of anything else with the
7253 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
7254 at run time. For example, the latter example above *cannot* be implemented
7255 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
7256 evaluation time of the original SAVE_EXPR is not necessarily the same at
7257 the time the new expression is evaluated. The only optimization of this
7258 sort that would be valid is changing
7260 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
7262 divided by 8 to
7264 SAVE_EXPR (I) * SAVE_EXPR (J)
7266 (where the same SAVE_EXPR (J) is used in the original and the
7267 transformed version). */
7269 static int
7270 multiple_of_p (type, top, bottom)
7271 tree type;
7272 tree top;
7273 tree bottom;
7275 if (operand_equal_p (top, bottom, 0))
7276 return 1;
7278 if (TREE_CODE (type) != INTEGER_TYPE)
7279 return 0;
7281 switch (TREE_CODE (top))
7283 case MULT_EXPR:
7284 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7285 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7287 case PLUS_EXPR:
7288 case MINUS_EXPR:
7289 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7290 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7292 case LSHIFT_EXPR:
7293 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
7295 tree op1, t1;
7297 op1 = TREE_OPERAND (top, 1);
7298 /* const_binop may not detect overflow correctly,
7299 so check for it explicitly here. */
7300 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
7301 > TREE_INT_CST_LOW (op1)
7302 && TREE_INT_CST_HIGH (op1) == 0
7303 && 0 != (t1 = convert (type,
7304 const_binop (LSHIFT_EXPR, size_one_node,
7305 op1, 0)))
7306 && ! TREE_OVERFLOW (t1))
7307 return multiple_of_p (type, t1, bottom);
7309 return 0;
7311 case NOP_EXPR:
7312 /* Can't handle conversions from non-integral or wider integral type. */
7313 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
7314 || (TYPE_PRECISION (type)
7315 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
7316 return 0;
7318 /* .. fall through ... */
7320 case SAVE_EXPR:
7321 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
7323 case INTEGER_CST:
7324 if (TREE_CODE (bottom) != INTEGER_CST
7325 || (TREE_UNSIGNED (type)
7326 && (tree_int_cst_sgn (top) < 0
7327 || tree_int_cst_sgn (bottom) < 0)))
7328 return 0;
7329 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
7330 top, bottom, 0));
7332 default:
7333 return 0;
7337 /* Return true if `t' is known to be non-negative. */
7340 tree_expr_nonnegative_p (t)
7341 tree t;
7343 switch (TREE_CODE (t))
7345 case INTEGER_CST:
7346 return tree_int_cst_sgn (t) >= 0;
7347 case COND_EXPR:
7348 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
7349 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
7350 case BIND_EXPR:
7351 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7352 case RTL_EXPR:
7353 return rtl_expr_nonnegative_p (RTL_EXPR_RTL (t));
7355 default:
7356 if (truth_value_p (TREE_CODE (t)))
7357 /* Truth values evaluate to 0 or 1, which is nonnegative. */
7358 return 1;
7359 else
7360 /* We don't know sign of `t', so be conservative and return false. */
7361 return 0;
7365 /* Return true if `r' is known to be non-negative.
7366 Only handles constants at the moment. */
7369 rtl_expr_nonnegative_p (r)
7370 rtx r;
7372 switch (GET_CODE (r))
7374 case CONST_INT:
7375 return INTVAL (r) >= 0;
7377 case CONST_DOUBLE:
7378 if (GET_MODE (r) == VOIDmode)
7379 return CONST_DOUBLE_HIGH (r) >= 0;
7380 return 0;
7382 case SYMBOL_REF:
7383 case LABEL_REF:
7384 /* These are always nonnegative. */
7385 return 1;
7387 default:
7388 return 0;