* verify.c (verify_jvm_instructions): Fix typo.
[official-gcc.git] / gcc / fold-const.c
blob7cefaeaef0e756d50dad8337df76905df9b889f1
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 GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 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 "flags.h"
48 #include "tree.h"
49 #include "rtl.h"
50 #include "expr.h"
51 #include "tm_p.h"
52 #include "toplev.h"
53 #include "ggc.h"
55 static void encode PARAMS ((HOST_WIDE_INT *,
56 unsigned HOST_WIDE_INT,
57 HOST_WIDE_INT));
58 static void decode PARAMS ((HOST_WIDE_INT *,
59 unsigned HOST_WIDE_INT *,
60 HOST_WIDE_INT *));
61 #ifndef REAL_ARITHMETIC
62 static void exact_real_inverse_1 PARAMS ((PTR));
63 #endif
64 static tree negate_expr PARAMS ((tree));
65 static tree split_tree PARAMS ((tree, enum tree_code, tree *, tree *,
66 int));
67 static tree associate_trees PARAMS ((tree, tree, enum tree_code, tree));
68 static tree int_const_binop PARAMS ((enum tree_code, tree, tree, int, int));
69 static void const_binop_1 PARAMS ((PTR));
70 static tree const_binop PARAMS ((enum tree_code, tree, tree, int));
71 static void fold_convert_1 PARAMS ((PTR));
72 static tree fold_convert PARAMS ((tree, tree));
73 static enum tree_code invert_tree_comparison PARAMS ((enum tree_code));
74 static enum tree_code swap_tree_comparison PARAMS ((enum tree_code));
75 static int truth_value_p PARAMS ((enum tree_code));
76 static int operand_equal_for_comparison_p PARAMS ((tree, tree, tree));
77 static int twoval_comparison_p PARAMS ((tree, tree *, tree *, int *));
78 static tree eval_subst PARAMS ((tree, tree, tree, tree, tree));
79 static tree omit_one_operand PARAMS ((tree, tree, tree));
80 static tree pedantic_omit_one_operand PARAMS ((tree, tree, tree));
81 static tree distribute_bit_expr PARAMS ((enum tree_code, tree, tree, tree));
82 static tree make_bit_field_ref PARAMS ((tree, tree, int, int, int));
83 static tree optimize_bit_field_compare PARAMS ((enum tree_code, tree,
84 tree, tree));
85 static tree decode_field_reference PARAMS ((tree, HOST_WIDE_INT *,
86 HOST_WIDE_INT *,
87 enum machine_mode *, int *,
88 int *, tree *, tree *));
89 static int all_ones_mask_p PARAMS ((tree, int));
90 static int simple_operand_p PARAMS ((tree));
91 static tree range_binop PARAMS ((enum tree_code, tree, tree, int,
92 tree, int));
93 static tree make_range PARAMS ((tree, int *, tree *, tree *));
94 static tree build_range_check PARAMS ((tree, tree, int, tree, tree));
95 static int merge_ranges PARAMS ((int *, tree *, tree *, int, tree, tree,
96 int, tree, tree));
97 static tree fold_range_test PARAMS ((tree));
98 static tree unextend PARAMS ((tree, int, int, tree));
99 static tree fold_truthop PARAMS ((enum tree_code, tree, tree, tree));
100 static tree optimize_minmax_comparison PARAMS ((tree));
101 static tree extract_muldiv PARAMS ((tree, tree, enum tree_code, tree));
102 static tree strip_compound_expr PARAMS ((tree, tree));
103 static int multiple_of_p PARAMS ((tree, tree, tree));
104 static tree constant_boolean_node PARAMS ((int, tree));
105 static int count_cond PARAMS ((tree, int));
106 static tree fold_binary_op_with_conditional_arg
107 PARAMS ((enum tree_code, tree, tree, tree, int));
109 #ifndef BRANCH_COST
110 #define BRANCH_COST 1
111 #endif
113 #if defined(HOST_EBCDIC)
114 /* bit 8 is significant in EBCDIC */
115 #define CHARMASK 0xff
116 #else
117 #define CHARMASK 0x7f
118 #endif
120 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
121 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
122 and SUM1. Then this yields nonzero if overflow occurred during the
123 addition.
125 Overflow occurs if A and B have the same sign, but A and SUM differ in
126 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
127 sign. */
128 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
130 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
131 We do that by representing the two-word integer in 4 words, with only
132 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
133 number. The value of the word is LOWPART + HIGHPART * BASE. */
135 #define LOWPART(x) \
136 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
137 #define HIGHPART(x) \
138 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
139 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
141 /* Unpack a two-word integer into 4 words.
142 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
143 WORDS points to the array of HOST_WIDE_INTs. */
145 static void
146 encode (words, low, hi)
147 HOST_WIDE_INT *words;
148 unsigned HOST_WIDE_INT low;
149 HOST_WIDE_INT hi;
151 words[0] = LOWPART (low);
152 words[1] = HIGHPART (low);
153 words[2] = LOWPART (hi);
154 words[3] = HIGHPART (hi);
157 /* Pack an array of 4 words into a two-word integer.
158 WORDS points to the array of words.
159 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
161 static void
162 decode (words, low, hi)
163 HOST_WIDE_INT *words;
164 unsigned HOST_WIDE_INT *low;
165 HOST_WIDE_INT *hi;
167 *low = words[0] + words[1] * BASE;
168 *hi = words[2] + words[3] * BASE;
171 /* Make the integer constant T valid for its type by setting to 0 or 1 all
172 the bits in the constant that don't belong in the type.
174 Return 1 if a signed overflow occurs, 0 otherwise. If OVERFLOW is
175 nonzero, a signed overflow has already occurred in calculating T, so
176 propagate it.
178 Make the real constant T valid for its type by calling CHECK_FLOAT_VALUE,
179 if it exists. */
182 force_fit_type (t, overflow)
183 tree t;
184 int overflow;
186 unsigned HOST_WIDE_INT low;
187 HOST_WIDE_INT high;
188 unsigned int prec;
190 if (TREE_CODE (t) == REAL_CST)
192 #ifdef CHECK_FLOAT_VALUE
193 CHECK_FLOAT_VALUE (TYPE_MODE (TREE_TYPE (t)), TREE_REAL_CST (t),
194 overflow);
195 #endif
196 return overflow;
199 else if (TREE_CODE (t) != INTEGER_CST)
200 return overflow;
202 low = TREE_INT_CST_LOW (t);
203 high = TREE_INT_CST_HIGH (t);
205 if (POINTER_TYPE_P (TREE_TYPE (t)))
206 prec = POINTER_SIZE;
207 else
208 prec = TYPE_PRECISION (TREE_TYPE (t));
210 /* First clear all bits that are beyond the type's precision. */
212 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
214 else if (prec > HOST_BITS_PER_WIDE_INT)
215 TREE_INT_CST_HIGH (t)
216 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
217 else
219 TREE_INT_CST_HIGH (t) = 0;
220 if (prec < HOST_BITS_PER_WIDE_INT)
221 TREE_INT_CST_LOW (t) &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
224 /* Unsigned types do not suffer sign extension or overflow unless they
225 are a sizetype. */
226 if (TREE_UNSIGNED (TREE_TYPE (t))
227 && ! (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
228 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
229 return overflow;
231 /* If the value's sign bit is set, extend the sign. */
232 if (prec != 2 * HOST_BITS_PER_WIDE_INT
233 && (prec > HOST_BITS_PER_WIDE_INT
234 ? 0 != (TREE_INT_CST_HIGH (t)
235 & ((HOST_WIDE_INT) 1
236 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
237 : 0 != (TREE_INT_CST_LOW (t)
238 & ((unsigned HOST_WIDE_INT) 1 << (prec - 1)))))
240 /* Value is negative:
241 set to 1 all the bits that are outside this type's precision. */
242 if (prec > HOST_BITS_PER_WIDE_INT)
243 TREE_INT_CST_HIGH (t)
244 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
245 else
247 TREE_INT_CST_HIGH (t) = -1;
248 if (prec < HOST_BITS_PER_WIDE_INT)
249 TREE_INT_CST_LOW (t) |= ((unsigned HOST_WIDE_INT) (-1) << prec);
253 /* Return nonzero if signed overflow occurred. */
254 return
255 ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
256 != 0);
259 /* Add two doubleword integers with doubleword result.
260 Each argument is given as two `HOST_WIDE_INT' pieces.
261 One argument is L1 and H1; the other, L2 and H2.
262 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
265 add_double (l1, h1, l2, h2, lv, hv)
266 unsigned HOST_WIDE_INT l1, l2;
267 HOST_WIDE_INT h1, h2;
268 unsigned HOST_WIDE_INT *lv;
269 HOST_WIDE_INT *hv;
271 unsigned HOST_WIDE_INT l;
272 HOST_WIDE_INT h;
274 l = l1 + l2;
275 h = h1 + h2 + (l < l1);
277 *lv = l;
278 *hv = h;
279 return OVERFLOW_SUM_SIGN (h1, h2, h);
282 /* Negate a doubleword integer with doubleword result.
283 Return nonzero if the operation overflows, assuming it's signed.
284 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
285 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
288 neg_double (l1, h1, lv, hv)
289 unsigned HOST_WIDE_INT l1;
290 HOST_WIDE_INT h1;
291 unsigned HOST_WIDE_INT *lv;
292 HOST_WIDE_INT *hv;
294 if (l1 == 0)
296 *lv = 0;
297 *hv = - h1;
298 return (*hv & h1) < 0;
300 else
302 *lv = -l1;
303 *hv = ~h1;
304 return 0;
308 /* Multiply two doubleword integers with doubleword result.
309 Return nonzero if the operation overflows, assuming it's signed.
310 Each argument is given as two `HOST_WIDE_INT' pieces.
311 One argument is L1 and H1; the other, L2 and H2.
312 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
315 mul_double (l1, h1, l2, h2, lv, hv)
316 unsigned HOST_WIDE_INT l1, l2;
317 HOST_WIDE_INT h1, h2;
318 unsigned HOST_WIDE_INT *lv;
319 HOST_WIDE_INT *hv;
321 HOST_WIDE_INT arg1[4];
322 HOST_WIDE_INT arg2[4];
323 HOST_WIDE_INT prod[4 * 2];
324 register unsigned HOST_WIDE_INT carry;
325 register int i, j, k;
326 unsigned HOST_WIDE_INT toplow, neglow;
327 HOST_WIDE_INT tophigh, neghigh;
329 encode (arg1, l1, h1);
330 encode (arg2, l2, h2);
332 memset ((char *) prod, 0, sizeof prod);
334 for (i = 0; i < 4; i++)
336 carry = 0;
337 for (j = 0; j < 4; j++)
339 k = i + j;
340 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
341 carry += arg1[i] * arg2[j];
342 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
343 carry += prod[k];
344 prod[k] = LOWPART (carry);
345 carry = HIGHPART (carry);
347 prod[i + 4] = carry;
350 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
352 /* Check for overflow by calculating the top half of the answer in full;
353 it should agree with the low half's sign bit. */
354 decode (prod + 4, &toplow, &tophigh);
355 if (h1 < 0)
357 neg_double (l2, h2, &neglow, &neghigh);
358 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
360 if (h2 < 0)
362 neg_double (l1, h1, &neglow, &neghigh);
363 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
365 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
368 /* Shift the doubleword integer in L1, H1 left by COUNT places
369 keeping only PREC bits of result.
370 Shift right if COUNT is negative.
371 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
372 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
374 void
375 lshift_double (l1, h1, count, prec, lv, hv, arith)
376 unsigned HOST_WIDE_INT l1;
377 HOST_WIDE_INT h1, count;
378 unsigned int prec;
379 unsigned HOST_WIDE_INT *lv;
380 HOST_WIDE_INT *hv;
381 int arith;
383 if (count < 0)
385 rshift_double (l1, h1, -count, prec, lv, hv, arith);
386 return;
389 #ifdef SHIFT_COUNT_TRUNCATED
390 if (SHIFT_COUNT_TRUNCATED)
391 count %= prec;
392 #endif
394 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
396 /* Shifting by the host word size is undefined according to the
397 ANSI standard, so we must handle this as a special case. */
398 *hv = 0;
399 *lv = 0;
401 else if (count >= HOST_BITS_PER_WIDE_INT)
403 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
404 *lv = 0;
406 else
408 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
409 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
410 *lv = l1 << count;
414 /* Shift the doubleword integer in L1, H1 right by COUNT places
415 keeping only PREC bits of result. COUNT must be positive.
416 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
417 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
419 void
420 rshift_double (l1, h1, count, prec, lv, hv, arith)
421 unsigned HOST_WIDE_INT l1;
422 HOST_WIDE_INT h1, count;
423 unsigned int prec ATTRIBUTE_UNUSED;
424 unsigned HOST_WIDE_INT *lv;
425 HOST_WIDE_INT *hv;
426 int arith;
428 unsigned HOST_WIDE_INT signmask;
430 signmask = (arith
431 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
432 : 0);
434 #ifdef SHIFT_COUNT_TRUNCATED
435 if (SHIFT_COUNT_TRUNCATED)
436 count %= prec;
437 #endif
439 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
441 /* Shifting by the host word size is undefined according to the
442 ANSI standard, so we must handle this as a special case. */
443 *hv = signmask;
444 *lv = signmask;
446 else if (count >= HOST_BITS_PER_WIDE_INT)
448 *hv = signmask;
449 *lv = ((signmask << (2 * HOST_BITS_PER_WIDE_INT - count - 1) << 1)
450 | ((unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT)));
452 else
454 *lv = ((l1 >> count)
455 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
456 *hv = ((signmask << (HOST_BITS_PER_WIDE_INT - count))
457 | ((unsigned HOST_WIDE_INT) h1 >> count));
461 /* Rotate the doubleword integer in L1, H1 left by COUNT places
462 keeping only PREC bits of result.
463 Rotate right if COUNT is negative.
464 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
466 void
467 lrotate_double (l1, h1, count, prec, lv, hv)
468 unsigned HOST_WIDE_INT l1;
469 HOST_WIDE_INT h1, count;
470 unsigned int prec;
471 unsigned HOST_WIDE_INT *lv;
472 HOST_WIDE_INT *hv;
474 unsigned HOST_WIDE_INT s1l, s2l;
475 HOST_WIDE_INT s1h, s2h;
477 count %= prec;
478 if (count < 0)
479 count += prec;
481 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
482 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
483 *lv = s1l | s2l;
484 *hv = s1h | s2h;
487 /* Rotate the doubleword integer in L1, H1 left by COUNT places
488 keeping only PREC bits of result. COUNT must be positive.
489 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
491 void
492 rrotate_double (l1, h1, count, prec, lv, hv)
493 unsigned HOST_WIDE_INT l1;
494 HOST_WIDE_INT h1, count;
495 unsigned int prec;
496 unsigned HOST_WIDE_INT *lv;
497 HOST_WIDE_INT *hv;
499 unsigned HOST_WIDE_INT s1l, s2l;
500 HOST_WIDE_INT s1h, s2h;
502 count %= prec;
503 if (count < 0)
504 count += prec;
506 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
507 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
508 *lv = s1l | s2l;
509 *hv = s1h | s2h;
512 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
513 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
514 CODE is a tree code for a kind of division, one of
515 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
516 or EXACT_DIV_EXPR
517 It controls how the quotient is rounded to a integer.
518 Return nonzero if the operation overflows.
519 UNS nonzero says do unsigned division. */
522 div_and_round_double (code, uns,
523 lnum_orig, hnum_orig, lden_orig, hden_orig,
524 lquo, hquo, lrem, hrem)
525 enum tree_code code;
526 int uns;
527 unsigned HOST_WIDE_INT lnum_orig; /* num == numerator == dividend */
528 HOST_WIDE_INT hnum_orig;
529 unsigned HOST_WIDE_INT lden_orig; /* den == denominator == divisor */
530 HOST_WIDE_INT hden_orig;
531 unsigned HOST_WIDE_INT *lquo, *lrem;
532 HOST_WIDE_INT *hquo, *hrem;
534 int quo_neg = 0;
535 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
536 HOST_WIDE_INT den[4], quo[4];
537 register int i, j;
538 unsigned HOST_WIDE_INT work;
539 unsigned HOST_WIDE_INT carry = 0;
540 unsigned HOST_WIDE_INT lnum = lnum_orig;
541 HOST_WIDE_INT hnum = hnum_orig;
542 unsigned HOST_WIDE_INT lden = lden_orig;
543 HOST_WIDE_INT hden = hden_orig;
544 int overflow = 0;
546 if (hden == 0 && lden == 0)
547 overflow = 1, lden = 1;
549 /* calculate quotient sign and convert operands to unsigned. */
550 if (!uns)
552 if (hnum < 0)
554 quo_neg = ~ quo_neg;
555 /* (minimum integer) / (-1) is the only overflow case. */
556 if (neg_double (lnum, hnum, &lnum, &hnum)
557 && ((HOST_WIDE_INT) lden & hden) == -1)
558 overflow = 1;
560 if (hden < 0)
562 quo_neg = ~ quo_neg;
563 neg_double (lden, hden, &lden, &hden);
567 if (hnum == 0 && hden == 0)
568 { /* single precision */
569 *hquo = *hrem = 0;
570 /* This unsigned division rounds toward zero. */
571 *lquo = lnum / lden;
572 goto finish_up;
575 if (hnum == 0)
576 { /* trivial case: dividend < divisor */
577 /* hden != 0 already checked. */
578 *hquo = *lquo = 0;
579 *hrem = hnum;
580 *lrem = lnum;
581 goto finish_up;
584 memset ((char *) quo, 0, sizeof quo);
586 memset ((char *) num, 0, sizeof num); /* to zero 9th element */
587 memset ((char *) den, 0, sizeof den);
589 encode (num, lnum, hnum);
590 encode (den, lden, hden);
592 /* Special code for when the divisor < BASE. */
593 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
595 /* hnum != 0 already checked. */
596 for (i = 4 - 1; i >= 0; i--)
598 work = num[i] + carry * BASE;
599 quo[i] = work / lden;
600 carry = work % lden;
603 else
605 /* Full double precision division,
606 with thanks to Don Knuth's "Seminumerical Algorithms". */
607 int num_hi_sig, den_hi_sig;
608 unsigned HOST_WIDE_INT quo_est, scale;
610 /* Find the highest non-zero divisor digit. */
611 for (i = 4 - 1;; i--)
612 if (den[i] != 0)
614 den_hi_sig = i;
615 break;
618 /* Insure that the first digit of the divisor is at least BASE/2.
619 This is required by the quotient digit estimation algorithm. */
621 scale = BASE / (den[den_hi_sig] + 1);
622 if (scale > 1)
623 { /* scale divisor and dividend */
624 carry = 0;
625 for (i = 0; i <= 4 - 1; i++)
627 work = (num[i] * scale) + carry;
628 num[i] = LOWPART (work);
629 carry = HIGHPART (work);
632 num[4] = carry;
633 carry = 0;
634 for (i = 0; i <= 4 - 1; i++)
636 work = (den[i] * scale) + carry;
637 den[i] = LOWPART (work);
638 carry = HIGHPART (work);
639 if (den[i] != 0) den_hi_sig = i;
643 num_hi_sig = 4;
645 /* Main loop */
646 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
648 /* Guess the next quotient digit, quo_est, by dividing the first
649 two remaining dividend digits by the high order quotient digit.
650 quo_est is never low and is at most 2 high. */
651 unsigned HOST_WIDE_INT tmp;
653 num_hi_sig = i + den_hi_sig + 1;
654 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
655 if (num[num_hi_sig] != den[den_hi_sig])
656 quo_est = work / den[den_hi_sig];
657 else
658 quo_est = BASE - 1;
660 /* Refine quo_est so it's usually correct, and at most one high. */
661 tmp = work - quo_est * den[den_hi_sig];
662 if (tmp < BASE
663 && (den[den_hi_sig - 1] * quo_est
664 > (tmp * BASE + num[num_hi_sig - 2])))
665 quo_est--;
667 /* Try QUO_EST as the quotient digit, by multiplying the
668 divisor by QUO_EST and subtracting from the remaining dividend.
669 Keep in mind that QUO_EST is the I - 1st digit. */
671 carry = 0;
672 for (j = 0; j <= den_hi_sig; j++)
674 work = quo_est * den[j] + carry;
675 carry = HIGHPART (work);
676 work = num[i + j] - LOWPART (work);
677 num[i + j] = LOWPART (work);
678 carry += HIGHPART (work) != 0;
681 /* If quo_est was high by one, then num[i] went negative and
682 we need to correct things. */
683 if (num[num_hi_sig] < carry)
685 quo_est--;
686 carry = 0; /* add divisor back in */
687 for (j = 0; j <= den_hi_sig; j++)
689 work = num[i + j] + den[j] + carry;
690 carry = HIGHPART (work);
691 num[i + j] = LOWPART (work);
694 num [num_hi_sig] += carry;
697 /* Store the quotient digit. */
698 quo[i] = quo_est;
702 decode (quo, lquo, hquo);
704 finish_up:
705 /* if result is negative, make it so. */
706 if (quo_neg)
707 neg_double (*lquo, *hquo, lquo, hquo);
709 /* compute trial remainder: rem = num - (quo * den) */
710 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
711 neg_double (*lrem, *hrem, lrem, hrem);
712 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
714 switch (code)
716 case TRUNC_DIV_EXPR:
717 case TRUNC_MOD_EXPR: /* round toward zero */
718 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
719 return overflow;
721 case FLOOR_DIV_EXPR:
722 case FLOOR_MOD_EXPR: /* round toward negative infinity */
723 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
725 /* quo = quo - 1; */
726 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
727 lquo, hquo);
729 else
730 return overflow;
731 break;
733 case CEIL_DIV_EXPR:
734 case CEIL_MOD_EXPR: /* round toward positive infinity */
735 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
737 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
738 lquo, hquo);
740 else
741 return overflow;
742 break;
744 case ROUND_DIV_EXPR:
745 case ROUND_MOD_EXPR: /* round to closest integer */
747 unsigned HOST_WIDE_INT labs_rem = *lrem;
748 HOST_WIDE_INT habs_rem = *hrem;
749 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
750 HOST_WIDE_INT habs_den = hden, htwice;
752 /* Get absolute values */
753 if (*hrem < 0)
754 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
755 if (hden < 0)
756 neg_double (lden, hden, &labs_den, &habs_den);
758 /* If (2 * abs (lrem) >= abs (lden)) */
759 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
760 labs_rem, habs_rem, &ltwice, &htwice);
762 if (((unsigned HOST_WIDE_INT) habs_den
763 < (unsigned HOST_WIDE_INT) htwice)
764 || (((unsigned HOST_WIDE_INT) habs_den
765 == (unsigned HOST_WIDE_INT) htwice)
766 && (labs_den < ltwice)))
768 if (*hquo < 0)
769 /* quo = quo - 1; */
770 add_double (*lquo, *hquo,
771 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
772 else
773 /* quo = quo + 1; */
774 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
775 lquo, hquo);
777 else
778 return overflow;
780 break;
782 default:
783 abort ();
786 /* compute true remainder: rem = num - (quo * den) */
787 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
788 neg_double (*lrem, *hrem, lrem, hrem);
789 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
790 return overflow;
793 #ifndef REAL_ARITHMETIC
794 /* Effectively truncate a real value to represent the nearest possible value
795 in a narrower mode. The result is actually represented in the same data
796 type as the argument, but its value is usually different.
798 A trap may occur during the FP operations and it is the responsibility
799 of the calling function to have a handler established. */
801 REAL_VALUE_TYPE
802 real_value_truncate (mode, arg)
803 enum machine_mode mode;
804 REAL_VALUE_TYPE arg;
806 return REAL_VALUE_TRUNCATE (mode, arg);
809 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
811 /* Check for infinity in an IEEE double precision number. */
814 target_isinf (x)
815 REAL_VALUE_TYPE x;
817 /* The IEEE 64-bit double format. */
818 union {
819 REAL_VALUE_TYPE d;
820 struct {
821 unsigned sign : 1;
822 unsigned exponent : 11;
823 unsigned mantissa1 : 20;
824 unsigned mantissa2 : 32;
825 } little_endian;
826 struct {
827 unsigned mantissa2 : 32;
828 unsigned mantissa1 : 20;
829 unsigned exponent : 11;
830 unsigned sign : 1;
831 } big_endian;
832 } u;
834 u.d = dconstm1;
835 if (u.big_endian.sign == 1)
837 u.d = x;
838 return (u.big_endian.exponent == 2047
839 && u.big_endian.mantissa1 == 0
840 && u.big_endian.mantissa2 == 0);
842 else
844 u.d = x;
845 return (u.little_endian.exponent == 2047
846 && u.little_endian.mantissa1 == 0
847 && u.little_endian.mantissa2 == 0);
851 /* Check whether an IEEE double precision number is a NaN. */
854 target_isnan (x)
855 REAL_VALUE_TYPE x;
857 /* The IEEE 64-bit double format. */
858 union {
859 REAL_VALUE_TYPE d;
860 struct {
861 unsigned sign : 1;
862 unsigned exponent : 11;
863 unsigned mantissa1 : 20;
864 unsigned mantissa2 : 32;
865 } little_endian;
866 struct {
867 unsigned mantissa2 : 32;
868 unsigned mantissa1 : 20;
869 unsigned exponent : 11;
870 unsigned sign : 1;
871 } big_endian;
872 } u;
874 u.d = dconstm1;
875 if (u.big_endian.sign == 1)
877 u.d = x;
878 return (u.big_endian.exponent == 2047
879 && (u.big_endian.mantissa1 != 0
880 || u.big_endian.mantissa2 != 0));
882 else
884 u.d = x;
885 return (u.little_endian.exponent == 2047
886 && (u.little_endian.mantissa1 != 0
887 || u.little_endian.mantissa2 != 0));
891 /* Check for a negative IEEE double precision number. */
894 target_negative (x)
895 REAL_VALUE_TYPE x;
897 /* The IEEE 64-bit double format. */
898 union {
899 REAL_VALUE_TYPE d;
900 struct {
901 unsigned sign : 1;
902 unsigned exponent : 11;
903 unsigned mantissa1 : 20;
904 unsigned mantissa2 : 32;
905 } little_endian;
906 struct {
907 unsigned mantissa2 : 32;
908 unsigned mantissa1 : 20;
909 unsigned exponent : 11;
910 unsigned sign : 1;
911 } big_endian;
912 } u;
914 u.d = dconstm1;
915 if (u.big_endian.sign == 1)
917 u.d = x;
918 return u.big_endian.sign;
920 else
922 u.d = x;
923 return u.little_endian.sign;
926 #else /* Target not IEEE */
928 /* Let's assume other float formats don't have infinity.
929 (This can be overridden by redefining REAL_VALUE_ISINF.) */
932 target_isinf (x)
933 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
935 return 0;
938 /* Let's assume other float formats don't have NaNs.
939 (This can be overridden by redefining REAL_VALUE_ISNAN.) */
942 target_isnan (x)
943 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
945 return 0;
948 /* Let's assume other float formats don't have minus zero.
949 (This can be overridden by redefining REAL_VALUE_NEGATIVE.) */
952 target_negative (x)
953 REAL_VALUE_TYPE x;
955 return x < 0;
957 #endif /* Target not IEEE */
959 /* Try to change R into its exact multiplicative inverse in machine mode
960 MODE. Return nonzero function value if successful. */
961 struct exact_real_inverse_args
963 REAL_VALUE_TYPE *r;
964 enum machine_mode mode;
965 int success;
968 static void
969 exact_real_inverse_1 (p)
970 PTR p;
972 struct exact_real_inverse_args *args =
973 (struct exact_real_inverse_args *) p;
975 enum machine_mode mode = args->mode;
976 REAL_VALUE_TYPE *r = args->r;
978 union
980 double d;
981 unsigned short i[4];
983 x, t, y;
984 #ifdef CHECK_FLOAT_VALUE
985 int i;
986 #endif
988 /* Set array index to the less significant bits in the unions, depending
989 on the endian-ness of the host doubles. */
990 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT \
991 || HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
992 # define K 2
993 #else
994 # define K (2 * HOST_FLOAT_WORDS_BIG_ENDIAN)
995 #endif
997 /* Domain check the argument. */
998 x.d = *r;
999 if (x.d == 0.0)
1000 goto fail;
1002 #ifdef REAL_INFINITY
1003 if (REAL_VALUE_ISINF (x.d) || REAL_VALUE_ISNAN (x.d))
1004 goto fail;
1005 #endif
1007 /* Compute the reciprocal and check for numerical exactness.
1008 It is unnecessary to check all the significand bits to determine
1009 whether X is a power of 2. If X is not, then it is impossible for
1010 the bottom half significand of both X and 1/X to be all zero bits.
1011 Hence we ignore the data structure of the top half and examine only
1012 the low order bits of the two significands. */
1013 t.d = 1.0 / x.d;
1014 if (x.i[K] != 0 || x.i[K + 1] != 0 || t.i[K] != 0 || t.i[K + 1] != 0)
1015 goto fail;
1017 /* Truncate to the required mode and range-check the result. */
1018 y.d = REAL_VALUE_TRUNCATE (mode, t.d);
1019 #ifdef CHECK_FLOAT_VALUE
1020 i = 0;
1021 if (CHECK_FLOAT_VALUE (mode, y.d, i))
1022 goto fail;
1023 #endif
1025 /* Fail if truncation changed the value. */
1026 if (y.d != t.d || y.d == 0.0)
1027 goto fail;
1029 #ifdef REAL_INFINITY
1030 if (REAL_VALUE_ISINF (y.d) || REAL_VALUE_ISNAN (y.d))
1031 goto fail;
1032 #endif
1034 /* Output the reciprocal and return success flag. */
1035 *r = y.d;
1036 args->success = 1;
1037 return;
1039 fail:
1040 args->success = 0;
1041 return;
1043 #undef K
1048 exact_real_inverse (mode, r)
1049 enum machine_mode mode;
1050 REAL_VALUE_TYPE *r;
1052 struct exact_real_inverse_args args;
1054 /* Disable if insufficient information on the data structure. */
1055 #if HOST_FLOAT_FORMAT == UNKNOWN_FLOAT_FORMAT
1056 return 0;
1057 #endif
1059 /* Usually disable if bounds checks are not reliable. */
1060 if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT) && !flag_pretend_float)
1061 return 0;
1063 args.mode = mode;
1064 args.r = r;
1066 if (do_float_handler (exact_real_inverse_1, (PTR) &args))
1067 return args.success;
1068 return 0;
1071 /* Convert C99 hexadecimal floating point string constant S. Return
1072 real value type in mode MODE. This function uses the host computer's
1073 floating point arithmetic when there is no REAL_ARITHMETIC. */
1075 REAL_VALUE_TYPE
1076 real_hex_to_f (s, mode)
1077 const char *s;
1078 enum machine_mode mode;
1080 REAL_VALUE_TYPE ip;
1081 const char *p = s;
1082 unsigned HOST_WIDE_INT low, high;
1083 int shcount, nrmcount, k;
1084 int sign, expsign, isfloat;
1085 int lost = 0;/* Nonzero low order bits shifted out and discarded. */
1086 int frexpon = 0; /* Bits after the decimal point. */
1087 int expon = 0; /* Value of exponent. */
1088 int decpt = 0; /* How many decimal points. */
1089 int gotp = 0; /* How many P's. */
1090 char c;
1092 isfloat = 0;
1093 expsign = 1;
1094 ip = 0.0;
1096 while (*p == ' ' || *p == '\t')
1097 ++p;
1099 /* Sign, if any, comes first. */
1100 sign = 1;
1101 if (*p == '-')
1103 sign = -1;
1104 ++p;
1107 /* The string is supposed to start with 0x or 0X . */
1108 if (*p == '0')
1110 ++p;
1111 if (*p == 'x' || *p == 'X')
1112 ++p;
1113 else
1114 abort ();
1116 else
1117 abort ();
1119 while (*p == '0')
1120 ++p;
1122 high = 0;
1123 low = 0;
1124 shcount = 0;
1125 while ((c = *p) != '\0')
1127 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')
1128 || (c >= 'a' && c <= 'f'))
1130 k = c & CHARMASK;
1131 if (k >= 'a' && k <= 'f')
1132 k = k - 'a' + 10;
1133 else if (k >= 'A')
1134 k = k - 'A' + 10;
1135 else
1136 k = k - '0';
1138 if ((high & 0xf0000000) == 0)
1140 high = (high << 4) + ((low >> 28) & 15);
1141 low = (low << 4) + k;
1142 shcount += 4;
1143 if (decpt)
1144 frexpon += 4;
1146 else
1148 /* Record nonzero lost bits. */
1149 lost |= k;
1150 if (! decpt)
1151 frexpon -= 4;
1153 ++p;
1155 else if (c == '.')
1157 ++decpt;
1158 ++p;
1161 else if (c == 'p' || c == 'P')
1163 ++gotp;
1164 ++p;
1165 /* Sign of exponent. */
1166 if (*p == '-')
1168 expsign = -1;
1169 ++p;
1172 /* Value of exponent.
1173 The exponent field is a decimal integer. */
1174 while (ISDIGIT (*p))
1176 k = (*p++ & CHARMASK) - '0';
1177 expon = 10 * expon + k;
1180 expon *= expsign;
1181 /* F suffix is ambiguous in the significand part
1182 so it must appear after the decimal exponent field. */
1183 if (*p == 'f' || *p == 'F')
1185 isfloat = 1;
1186 ++p;
1187 break;
1191 else if (c == 'l' || c == 'L')
1193 ++p;
1194 break;
1196 else
1197 break;
1200 /* Abort if last character read was not legitimate. */
1201 c = *p;
1202 if ((c != '\0' && c != ' ' && c != '\n' && c != '\r') || (decpt > 1))
1203 abort ();
1205 /* There must be either one decimal point or one p. */
1206 if (decpt == 0 && gotp == 0)
1207 abort ();
1209 shcount -= 4;
1210 if (high == 0 && low == 0)
1211 return dconst0;
1213 /* Normalize. */
1214 nrmcount = 0;
1215 if (high == 0)
1217 high = low;
1218 low = 0;
1219 nrmcount += 32;
1222 /* Leave a high guard bit for carry-out. */
1223 if ((high & 0x80000000) != 0)
1225 lost |= low & 1;
1226 low = (low >> 1) | (high << 31);
1227 high = high >> 1;
1228 nrmcount -= 1;
1231 if ((high & 0xffff8000) == 0)
1233 high = (high << 16) + ((low >> 16) & 0xffff);
1234 low = low << 16;
1235 nrmcount += 16;
1238 while ((high & 0xc0000000) == 0)
1240 high = (high << 1) + ((low >> 31) & 1);
1241 low = low << 1;
1242 nrmcount += 1;
1245 if (isfloat || GET_MODE_SIZE (mode) == UNITS_PER_WORD)
1247 /* Keep 24 bits precision, bits 0x7fffff80.
1248 Rounding bit is 0x40. */
1249 lost = lost | low | (high & 0x3f);
1250 low = 0;
1251 if (high & 0x40)
1253 if ((high & 0x80) || lost)
1254 high += 0x40;
1256 high &= 0xffffff80;
1258 else
1260 /* We need real.c to do long double formats, so here default
1261 to double precision. */
1262 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1263 /* IEEE double.
1264 Keep 53 bits precision, bits 0x7fffffff fffffc00.
1265 Rounding bit is low word 0x200. */
1266 lost = lost | (low & 0x1ff);
1267 if (low & 0x200)
1269 if ((low & 0x400) || lost)
1271 low = (low + 0x200) & 0xfffffc00;
1272 if (low == 0)
1273 high += 1;
1276 low &= 0xfffffc00;
1277 #else
1278 /* Assume it's a VAX with 56-bit significand,
1279 bits 0x7fffffff ffffff80. */
1280 lost = lost | (low & 0x7f);
1281 if (low & 0x40)
1283 if ((low & 0x80) || lost)
1285 low = (low + 0x40) & 0xffffff80;
1286 if (low == 0)
1287 high += 1;
1290 low &= 0xffffff80;
1291 #endif
1294 ip = (double) high;
1295 ip = REAL_VALUE_LDEXP (ip, 32) + (double) low;
1296 /* Apply shifts and exponent value as power of 2. */
1297 ip = REAL_VALUE_LDEXP (ip, expon - (nrmcount + frexpon));
1299 if (sign < 0)
1300 ip = -ip;
1301 return ip;
1304 #endif /* no REAL_ARITHMETIC */
1306 /* Given T, an expression, return the negation of T. Allow for T to be
1307 null, in which case return null. */
1309 static tree
1310 negate_expr (t)
1311 tree t;
1313 tree type;
1314 tree tem;
1316 if (t == 0)
1317 return 0;
1319 type = TREE_TYPE (t);
1320 STRIP_SIGN_NOPS (t);
1322 switch (TREE_CODE (t))
1324 case INTEGER_CST:
1325 case REAL_CST:
1326 if (! TREE_UNSIGNED (type)
1327 && 0 != (tem = fold (build1 (NEGATE_EXPR, type, t)))
1328 && ! TREE_OVERFLOW (tem))
1329 return tem;
1330 break;
1332 case NEGATE_EXPR:
1333 return convert (type, TREE_OPERAND (t, 0));
1335 case MINUS_EXPR:
1336 /* - (A - B) -> B - A */
1337 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1338 return convert (type,
1339 fold (build (MINUS_EXPR, TREE_TYPE (t),
1340 TREE_OPERAND (t, 1),
1341 TREE_OPERAND (t, 0))));
1342 break;
1344 default:
1345 break;
1348 return convert (type, build1 (NEGATE_EXPR, TREE_TYPE (t), t));
1351 /* Split a tree IN into a constant, literal and variable parts that could be
1352 combined with CODE to make IN. "constant" means an expression with
1353 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1354 commutative arithmetic operation. Store the constant part into *CONP,
1355 the literal in &LITP and return the variable part. If a part isn't
1356 present, set it to null. If the tree does not decompose in this way,
1357 return the entire tree as the variable part and the other parts as null.
1359 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1360 case, we negate an operand that was subtracted. If NEGATE_P is true, we
1361 are negating all of IN.
1363 If IN is itself a literal or constant, return it as appropriate.
1365 Note that we do not guarantee that any of the three values will be the
1366 same type as IN, but they will have the same signedness and mode. */
1368 static tree
1369 split_tree (in, code, conp, litp, negate_p)
1370 tree in;
1371 enum tree_code code;
1372 tree *conp, *litp;
1373 int negate_p;
1375 tree var = 0;
1377 *conp = 0;
1378 *litp = 0;
1380 /* Strip any conversions that don't change the machine mode or signedness. */
1381 STRIP_SIGN_NOPS (in);
1383 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1384 *litp = in;
1385 else if (TREE_CODE (in) == code
1386 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1387 /* We can associate addition and subtraction together (even
1388 though the C standard doesn't say so) for integers because
1389 the value is not affected. For reals, the value might be
1390 affected, so we can't. */
1391 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1392 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1394 tree op0 = TREE_OPERAND (in, 0);
1395 tree op1 = TREE_OPERAND (in, 1);
1396 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1397 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1399 /* First see if either of the operands is a literal, then a constant. */
1400 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1401 *litp = op0, op0 = 0;
1402 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1403 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1405 if (op0 != 0 && TREE_CONSTANT (op0))
1406 *conp = op0, op0 = 0;
1407 else if (op1 != 0 && TREE_CONSTANT (op1))
1408 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1410 /* If we haven't dealt with either operand, this is not a case we can
1411 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1412 if (op0 != 0 && op1 != 0)
1413 var = in;
1414 else if (op0 != 0)
1415 var = op0;
1416 else
1417 var = op1, neg_var_p = neg1_p;
1419 /* Now do any needed negations. */
1420 if (neg_litp_p) *litp = negate_expr (*litp);
1421 if (neg_conp_p) *conp = negate_expr (*conp);
1422 if (neg_var_p) var = negate_expr (var);
1424 else if (TREE_CONSTANT (in))
1425 *conp = in;
1426 else
1427 var = in;
1429 if (negate_p)
1431 var = negate_expr (var);
1432 *conp = negate_expr (*conp);
1433 *litp = negate_expr (*litp);
1436 return var;
1439 /* Re-associate trees split by the above function. T1 and T2 are either
1440 expressions to associate or null. Return the new expression, if any. If
1441 we build an operation, do it in TYPE and with CODE, except if CODE is a
1442 MINUS_EXPR, in which case we use PLUS_EXPR since split_tree will already
1443 have taken care of the negations. */
1445 static tree
1446 associate_trees (t1, t2, code, type)
1447 tree t1, t2;
1448 enum tree_code code;
1449 tree type;
1451 if (t1 == 0)
1452 return t2;
1453 else if (t2 == 0)
1454 return t1;
1456 if (code == MINUS_EXPR)
1457 code = PLUS_EXPR;
1459 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1460 try to fold this since we will have infinite recursion. But do
1461 deal with any NEGATE_EXPRs. */
1462 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1463 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1465 if (TREE_CODE (t1) == NEGATE_EXPR)
1466 return build (MINUS_EXPR, type, convert (type, t2),
1467 convert (type, TREE_OPERAND (t1, 0)));
1468 else if (TREE_CODE (t2) == NEGATE_EXPR)
1469 return build (MINUS_EXPR, type, convert (type, t1),
1470 convert (type, TREE_OPERAND (t2, 0)));
1471 else
1472 return build (code, type, convert (type, t1), convert (type, t2));
1475 return fold (build (code, type, convert (type, t1), convert (type, t2)));
1478 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1479 to produce a new constant.
1481 If NOTRUNC is nonzero, do not truncate the result to fit the data type.
1482 If FORSIZE is nonzero, compute overflow for unsigned types. */
1484 static tree
1485 int_const_binop (code, arg1, arg2, notrunc, forsize)
1486 enum tree_code code;
1487 register tree arg1, arg2;
1488 int notrunc, forsize;
1490 unsigned HOST_WIDE_INT int1l, int2l;
1491 HOST_WIDE_INT int1h, int2h;
1492 unsigned HOST_WIDE_INT low;
1493 HOST_WIDE_INT hi;
1494 unsigned HOST_WIDE_INT garbagel;
1495 HOST_WIDE_INT garbageh;
1496 register tree t;
1497 int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
1498 int overflow = 0;
1499 int no_overflow = 0;
1501 int1l = TREE_INT_CST_LOW (arg1);
1502 int1h = TREE_INT_CST_HIGH (arg1);
1503 int2l = TREE_INT_CST_LOW (arg2);
1504 int2h = TREE_INT_CST_HIGH (arg2);
1506 switch (code)
1508 case BIT_IOR_EXPR:
1509 low = int1l | int2l, hi = int1h | int2h;
1510 break;
1512 case BIT_XOR_EXPR:
1513 low = int1l ^ int2l, hi = int1h ^ int2h;
1514 break;
1516 case BIT_AND_EXPR:
1517 low = int1l & int2l, hi = int1h & int2h;
1518 break;
1520 case BIT_ANDTC_EXPR:
1521 low = int1l & ~int2l, hi = int1h & ~int2h;
1522 break;
1524 case RSHIFT_EXPR:
1525 int2l = -int2l;
1526 case LSHIFT_EXPR:
1527 /* It's unclear from the C standard whether shifts can overflow.
1528 The following code ignores overflow; perhaps a C standard
1529 interpretation ruling is needed. */
1530 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (TREE_TYPE (arg1)),
1531 &low, &hi, !uns);
1532 no_overflow = 1;
1533 break;
1535 case RROTATE_EXPR:
1536 int2l = - int2l;
1537 case LROTATE_EXPR:
1538 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (TREE_TYPE (arg1)),
1539 &low, &hi);
1540 break;
1542 case PLUS_EXPR:
1543 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1544 break;
1546 case MINUS_EXPR:
1547 neg_double (int2l, int2h, &low, &hi);
1548 add_double (int1l, int1h, low, hi, &low, &hi);
1549 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1550 break;
1552 case MULT_EXPR:
1553 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1554 break;
1556 case TRUNC_DIV_EXPR:
1557 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1558 case EXACT_DIV_EXPR:
1559 /* This is a shortcut for a common special case. */
1560 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1561 && ! TREE_CONSTANT_OVERFLOW (arg1)
1562 && ! TREE_CONSTANT_OVERFLOW (arg2)
1563 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1565 if (code == CEIL_DIV_EXPR)
1566 int1l += int2l - 1;
1568 low = int1l / int2l, hi = 0;
1569 break;
1572 /* ... fall through ... */
1574 case ROUND_DIV_EXPR:
1575 if (int2h == 0 && int2l == 1)
1577 low = int1l, hi = int1h;
1578 break;
1580 if (int1l == int2l && int1h == int2h
1581 && ! (int1l == 0 && int1h == 0))
1583 low = 1, hi = 0;
1584 break;
1586 overflow = div_and_round_double (code, uns,
1587 int1l, int1h, int2l, int2h,
1588 &low, &hi, &garbagel, &garbageh);
1589 break;
1591 case TRUNC_MOD_EXPR:
1592 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1593 /* This is a shortcut for a common special case. */
1594 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1595 && ! TREE_CONSTANT_OVERFLOW (arg1)
1596 && ! TREE_CONSTANT_OVERFLOW (arg2)
1597 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1599 if (code == CEIL_MOD_EXPR)
1600 int1l += int2l - 1;
1601 low = int1l % int2l, hi = 0;
1602 break;
1605 /* ... fall through ... */
1607 case ROUND_MOD_EXPR:
1608 overflow = div_and_round_double (code, uns,
1609 int1l, int1h, int2l, int2h,
1610 &garbagel, &garbageh, &low, &hi);
1611 break;
1613 case MIN_EXPR:
1614 case MAX_EXPR:
1615 if (uns)
1616 low = (((unsigned HOST_WIDE_INT) int1h
1617 < (unsigned HOST_WIDE_INT) int2h)
1618 || (((unsigned HOST_WIDE_INT) int1h
1619 == (unsigned HOST_WIDE_INT) int2h)
1620 && int1l < int2l));
1621 else
1622 low = (int1h < int2h
1623 || (int1h == int2h && int1l < int2l));
1625 if (low == (code == MIN_EXPR))
1626 low = int1l, hi = int1h;
1627 else
1628 low = int2l, hi = int2h;
1629 break;
1631 default:
1632 abort ();
1635 if (forsize && hi == 0 && low < 10000
1636 && overflow == 0 && ! TREE_OVERFLOW (arg1) && ! TREE_OVERFLOW (arg2))
1637 return size_int_type_wide (low, TREE_TYPE (arg1));
1638 else
1640 t = build_int_2 (low, hi);
1641 TREE_TYPE (t) = TREE_TYPE (arg1);
1644 TREE_OVERFLOW (t)
1645 = ((notrunc ? (!uns || forsize) && overflow
1646 : force_fit_type (t, (!uns || forsize) && overflow) && ! no_overflow)
1647 | TREE_OVERFLOW (arg1)
1648 | TREE_OVERFLOW (arg2));
1650 /* If we're doing a size calculation, unsigned arithmetic does overflow.
1651 So check if force_fit_type truncated the value. */
1652 if (forsize
1653 && ! TREE_OVERFLOW (t)
1654 && (TREE_INT_CST_HIGH (t) != hi
1655 || TREE_INT_CST_LOW (t) != low))
1656 TREE_OVERFLOW (t) = 1;
1658 TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1659 | TREE_CONSTANT_OVERFLOW (arg1)
1660 | TREE_CONSTANT_OVERFLOW (arg2));
1661 return t;
1664 /* Define input and output argument for const_binop_1. */
1665 struct cb_args
1667 enum tree_code code; /* Input: tree code for operation. */
1668 tree type; /* Input: tree type for operation. */
1669 REAL_VALUE_TYPE d1, d2; /* Input: floating point operands. */
1670 tree t; /* Output: constant for result. */
1673 /* Do the real arithmetic for const_binop while protected by a
1674 float overflow handler. */
1676 static void
1677 const_binop_1 (data)
1678 PTR data;
1680 struct cb_args *args = (struct cb_args *) data;
1681 REAL_VALUE_TYPE value;
1683 #ifdef REAL_ARITHMETIC
1684 REAL_ARITHMETIC (value, args->code, args->d1, args->d2);
1685 #else
1686 switch (args->code)
1688 case PLUS_EXPR:
1689 value = args->d1 + args->d2;
1690 break;
1692 case MINUS_EXPR:
1693 value = args->d1 - args->d2;
1694 break;
1696 case MULT_EXPR:
1697 value = args->d1 * args->d2;
1698 break;
1700 case RDIV_EXPR:
1701 #ifndef REAL_INFINITY
1702 if (args->d2 == 0)
1703 abort ();
1704 #endif
1706 value = args->d1 / args->d2;
1707 break;
1709 case MIN_EXPR:
1710 value = MIN (args->d1, args->d2);
1711 break;
1713 case MAX_EXPR:
1714 value = MAX (args->d1, args->d2);
1715 break;
1717 default:
1718 abort ();
1720 #endif /* no REAL_ARITHMETIC */
1722 args->t
1723 = build_real (args->type,
1724 real_value_truncate (TYPE_MODE (args->type), value));
1727 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1728 constant. We assume ARG1 and ARG2 have the same data type, or at least
1729 are the same kind of constant and the same machine mode.
1731 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1733 static tree
1734 const_binop (code, arg1, arg2, notrunc)
1735 enum tree_code code;
1736 register tree arg1, arg2;
1737 int notrunc;
1739 STRIP_NOPS (arg1);
1740 STRIP_NOPS (arg2);
1742 if (TREE_CODE (arg1) == INTEGER_CST)
1743 return int_const_binop (code, arg1, arg2, notrunc, 0);
1745 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1746 if (TREE_CODE (arg1) == REAL_CST)
1748 REAL_VALUE_TYPE d1;
1749 REAL_VALUE_TYPE d2;
1750 int overflow = 0;
1751 tree t;
1752 struct cb_args args;
1754 d1 = TREE_REAL_CST (arg1);
1755 d2 = TREE_REAL_CST (arg2);
1757 /* If either operand is a NaN, just return it. Otherwise, set up
1758 for floating-point trap; we return an overflow. */
1759 if (REAL_VALUE_ISNAN (d1))
1760 return arg1;
1761 else if (REAL_VALUE_ISNAN (d2))
1762 return arg2;
1764 /* Setup input for const_binop_1() */
1765 args.type = TREE_TYPE (arg1);
1766 args.d1 = d1;
1767 args.d2 = d2;
1768 args.code = code;
1770 if (do_float_handler (const_binop_1, (PTR) &args))
1771 /* Receive output from const_binop_1. */
1772 t = args.t;
1773 else
1775 /* We got an exception from const_binop_1. */
1776 t = copy_node (arg1);
1777 overflow = 1;
1780 TREE_OVERFLOW (t)
1781 = (force_fit_type (t, overflow)
1782 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1783 TREE_CONSTANT_OVERFLOW (t)
1784 = TREE_OVERFLOW (t)
1785 | TREE_CONSTANT_OVERFLOW (arg1)
1786 | TREE_CONSTANT_OVERFLOW (arg2);
1787 return t;
1789 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1790 if (TREE_CODE (arg1) == COMPLEX_CST)
1792 register tree type = TREE_TYPE (arg1);
1793 register tree r1 = TREE_REALPART (arg1);
1794 register tree i1 = TREE_IMAGPART (arg1);
1795 register tree r2 = TREE_REALPART (arg2);
1796 register tree i2 = TREE_IMAGPART (arg2);
1797 register tree t;
1799 switch (code)
1801 case PLUS_EXPR:
1802 t = build_complex (type,
1803 const_binop (PLUS_EXPR, r1, r2, notrunc),
1804 const_binop (PLUS_EXPR, i1, i2, notrunc));
1805 break;
1807 case MINUS_EXPR:
1808 t = build_complex (type,
1809 const_binop (MINUS_EXPR, r1, r2, notrunc),
1810 const_binop (MINUS_EXPR, i1, i2, notrunc));
1811 break;
1813 case MULT_EXPR:
1814 t = build_complex (type,
1815 const_binop (MINUS_EXPR,
1816 const_binop (MULT_EXPR,
1817 r1, r2, notrunc),
1818 const_binop (MULT_EXPR,
1819 i1, i2, notrunc),
1820 notrunc),
1821 const_binop (PLUS_EXPR,
1822 const_binop (MULT_EXPR,
1823 r1, i2, notrunc),
1824 const_binop (MULT_EXPR,
1825 i1, r2, notrunc),
1826 notrunc));
1827 break;
1829 case RDIV_EXPR:
1831 register tree magsquared
1832 = const_binop (PLUS_EXPR,
1833 const_binop (MULT_EXPR, r2, r2, notrunc),
1834 const_binop (MULT_EXPR, i2, i2, notrunc),
1835 notrunc);
1837 t = build_complex (type,
1838 const_binop
1839 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1840 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1841 const_binop (PLUS_EXPR,
1842 const_binop (MULT_EXPR, r1, r2,
1843 notrunc),
1844 const_binop (MULT_EXPR, i1, i2,
1845 notrunc),
1846 notrunc),
1847 magsquared, notrunc),
1848 const_binop
1849 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1850 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1851 const_binop (MINUS_EXPR,
1852 const_binop (MULT_EXPR, i1, r2,
1853 notrunc),
1854 const_binop (MULT_EXPR, r1, i2,
1855 notrunc),
1856 notrunc),
1857 magsquared, notrunc));
1859 break;
1861 default:
1862 abort ();
1864 return t;
1866 return 0;
1869 /* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
1870 bits are given by NUMBER and of the sizetype represented by KIND. */
1872 tree
1873 size_int_wide (number, kind)
1874 HOST_WIDE_INT number;
1875 enum size_type_kind kind;
1877 return size_int_type_wide (number, sizetype_tab[(int) kind]);
1880 /* Likewise, but the desired type is specified explicitly. */
1882 tree
1883 size_int_type_wide (number, type)
1884 HOST_WIDE_INT number;
1885 tree type;
1887 /* Type-size nodes already made for small sizes. */
1888 static tree size_table[2048 + 1];
1889 static int init_p = 0;
1890 tree t;
1892 if (! init_p)
1894 ggc_add_tree_root ((tree *) size_table,
1895 sizeof size_table / sizeof (tree));
1896 init_p = 1;
1899 /* If this is a positive number that fits in the table we use to hold
1900 cached entries, see if it is already in the table and put it there
1901 if not. */
1902 if (number >= 0 && number < (int) ARRAY_SIZE (size_table))
1904 if (size_table[number] != 0)
1905 for (t = size_table[number]; t != 0; t = TREE_CHAIN (t))
1906 if (TREE_TYPE (t) == type)
1907 return t;
1909 t = build_int_2 (number, 0);
1910 TREE_TYPE (t) = type;
1911 TREE_CHAIN (t) = size_table[number];
1912 size_table[number] = t;
1914 return t;
1917 t = build_int_2 (number, number < 0 ? -1 : 0);
1918 TREE_TYPE (t) = type;
1919 TREE_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (t) = force_fit_type (t, 0);
1920 return t;
1923 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1924 is a tree code. The type of the result is taken from the operands.
1925 Both must be the same type integer type and it must be a size type.
1926 If the operands are constant, so is the result. */
1928 tree
1929 size_binop (code, arg0, arg1)
1930 enum tree_code code;
1931 tree arg0, arg1;
1933 tree type = TREE_TYPE (arg0);
1935 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1936 || type != TREE_TYPE (arg1))
1937 abort ();
1939 /* Handle the special case of two integer constants faster. */
1940 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1942 /* And some specific cases even faster than that. */
1943 if (code == PLUS_EXPR && integer_zerop (arg0))
1944 return arg1;
1945 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1946 && integer_zerop (arg1))
1947 return arg0;
1948 else if (code == MULT_EXPR && integer_onep (arg0))
1949 return arg1;
1951 /* Handle general case of two integer constants. */
1952 return int_const_binop (code, arg0, arg1, 0, 1);
1955 if (arg0 == error_mark_node || arg1 == error_mark_node)
1956 return error_mark_node;
1958 return fold (build (code, type, arg0, arg1));
1961 /* Given two values, either both of sizetype or both of bitsizetype,
1962 compute the difference between the two values. Return the value
1963 in signed type corresponding to the type of the operands. */
1965 tree
1966 size_diffop (arg0, arg1)
1967 tree arg0, arg1;
1969 tree type = TREE_TYPE (arg0);
1970 tree ctype;
1972 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1973 || type != TREE_TYPE (arg1))
1974 abort ();
1976 /* If the type is already signed, just do the simple thing. */
1977 if (! TREE_UNSIGNED (type))
1978 return size_binop (MINUS_EXPR, arg0, arg1);
1980 ctype = (type == bitsizetype || type == ubitsizetype
1981 ? sbitsizetype : ssizetype);
1983 /* If either operand is not a constant, do the conversions to the signed
1984 type and subtract. The hardware will do the right thing with any
1985 overflow in the subtraction. */
1986 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1987 return size_binop (MINUS_EXPR, convert (ctype, arg0),
1988 convert (ctype, arg1));
1990 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1991 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1992 overflow) and negate (which can't either). Special-case a result
1993 of zero while we're here. */
1994 if (tree_int_cst_equal (arg0, arg1))
1995 return convert (ctype, integer_zero_node);
1996 else if (tree_int_cst_lt (arg1, arg0))
1997 return convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1998 else
1999 return size_binop (MINUS_EXPR, convert (ctype, integer_zero_node),
2000 convert (ctype, size_binop (MINUS_EXPR, arg1, arg0)));
2003 /* This structure is used to communicate arguments to fold_convert_1. */
2004 struct fc_args
2006 tree arg1; /* Input: value to convert. */
2007 tree type; /* Input: type to convert value to. */
2008 tree t; /* Ouput: result of conversion. */
2011 /* Function to convert floating-point constants, protected by floating
2012 point exception handler. */
2014 static void
2015 fold_convert_1 (data)
2016 PTR data;
2018 struct fc_args *args = (struct fc_args *) data;
2020 args->t = build_real (args->type,
2021 real_value_truncate (TYPE_MODE (args->type),
2022 TREE_REAL_CST (args->arg1)));
2025 /* Given T, a tree representing type conversion of ARG1, a constant,
2026 return a constant tree representing the result of conversion. */
2028 static tree
2029 fold_convert (t, arg1)
2030 register tree t;
2031 register tree arg1;
2033 register tree type = TREE_TYPE (t);
2034 int overflow = 0;
2036 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2038 if (TREE_CODE (arg1) == INTEGER_CST)
2040 /* If we would build a constant wider than GCC supports,
2041 leave the conversion unfolded. */
2042 if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
2043 return t;
2045 /* If we are trying to make a sizetype for a small integer, use
2046 size_int to pick up cached types to reduce duplicate nodes. */
2047 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
2048 && !TREE_CONSTANT_OVERFLOW (arg1)
2049 && compare_tree_int (arg1, 10000) < 0)
2050 return size_int_type_wide (TREE_INT_CST_LOW (arg1), type);
2052 /* Given an integer constant, make new constant with new type,
2053 appropriately sign-extended or truncated. */
2054 t = build_int_2 (TREE_INT_CST_LOW (arg1),
2055 TREE_INT_CST_HIGH (arg1));
2056 TREE_TYPE (t) = type;
2057 /* Indicate an overflow if (1) ARG1 already overflowed,
2058 or (2) force_fit_type indicates an overflow.
2059 Tell force_fit_type that an overflow has already occurred
2060 if ARG1 is a too-large unsigned value and T is signed.
2061 But don't indicate an overflow if converting a pointer. */
2062 TREE_OVERFLOW (t)
2063 = ((force_fit_type (t,
2064 (TREE_INT_CST_HIGH (arg1) < 0
2065 && (TREE_UNSIGNED (type)
2066 < TREE_UNSIGNED (TREE_TYPE (arg1)))))
2067 && ! POINTER_TYPE_P (TREE_TYPE (arg1)))
2068 || TREE_OVERFLOW (arg1));
2069 TREE_CONSTANT_OVERFLOW (t)
2070 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2072 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2073 else if (TREE_CODE (arg1) == REAL_CST)
2075 /* Don't initialize these, use assignments.
2076 Initialized local aggregates don't work on old compilers. */
2077 REAL_VALUE_TYPE x;
2078 REAL_VALUE_TYPE l;
2079 REAL_VALUE_TYPE u;
2080 tree type1 = TREE_TYPE (arg1);
2081 int no_upper_bound;
2083 x = TREE_REAL_CST (arg1);
2084 l = real_value_from_int_cst (type1, TYPE_MIN_VALUE (type));
2086 no_upper_bound = (TYPE_MAX_VALUE (type) == NULL);
2087 if (!no_upper_bound)
2088 u = real_value_from_int_cst (type1, TYPE_MAX_VALUE (type));
2090 /* See if X will be in range after truncation towards 0.
2091 To compensate for truncation, move the bounds away from 0,
2092 but reject if X exactly equals the adjusted bounds. */
2093 #ifdef REAL_ARITHMETIC
2094 REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
2095 if (!no_upper_bound)
2096 REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
2097 #else
2098 l--;
2099 if (!no_upper_bound)
2100 u++;
2101 #endif
2102 /* If X is a NaN, use zero instead and show we have an overflow.
2103 Otherwise, range check. */
2104 if (REAL_VALUE_ISNAN (x))
2105 overflow = 1, x = dconst0;
2106 else if (! (REAL_VALUES_LESS (l, x)
2107 && !no_upper_bound
2108 && REAL_VALUES_LESS (x, u)))
2109 overflow = 1;
2111 #ifndef REAL_ARITHMETIC
2113 HOST_WIDE_INT low, high;
2114 HOST_WIDE_INT half_word
2115 = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
2117 if (x < 0)
2118 x = -x;
2120 high = (HOST_WIDE_INT) (x / half_word / half_word);
2121 x -= (REAL_VALUE_TYPE) high * half_word * half_word;
2122 if (x >= (REAL_VALUE_TYPE) half_word * half_word / 2)
2124 low = x - (REAL_VALUE_TYPE) half_word * half_word / 2;
2125 low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1);
2127 else
2128 low = (HOST_WIDE_INT) x;
2129 if (TREE_REAL_CST (arg1) < 0)
2130 neg_double (low, high, &low, &high);
2131 t = build_int_2 (low, high);
2133 #else
2135 HOST_WIDE_INT low, high;
2136 REAL_VALUE_TO_INT (&low, &high, x);
2137 t = build_int_2 (low, high);
2139 #endif
2140 TREE_TYPE (t) = type;
2141 TREE_OVERFLOW (t)
2142 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
2143 TREE_CONSTANT_OVERFLOW (t)
2144 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2146 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2147 TREE_TYPE (t) = type;
2149 else if (TREE_CODE (type) == REAL_TYPE)
2151 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2152 if (TREE_CODE (arg1) == INTEGER_CST)
2153 return build_real_from_int_cst (type, arg1);
2154 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2155 if (TREE_CODE (arg1) == REAL_CST)
2157 struct fc_args args;
2159 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
2161 t = arg1;
2162 TREE_TYPE (arg1) = type;
2163 return t;
2166 /* Setup input for fold_convert_1() */
2167 args.arg1 = arg1;
2168 args.type = type;
2170 if (do_float_handler (fold_convert_1, (PTR) &args))
2172 /* Receive output from fold_convert_1() */
2173 t = args.t;
2175 else
2177 /* We got an exception from fold_convert_1() */
2178 overflow = 1;
2179 t = copy_node (arg1);
2182 TREE_OVERFLOW (t)
2183 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
2184 TREE_CONSTANT_OVERFLOW (t)
2185 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2186 return t;
2189 TREE_CONSTANT (t) = 1;
2190 return t;
2193 /* Return an expr equal to X but certainly not valid as an lvalue. */
2195 tree
2196 non_lvalue (x)
2197 tree x;
2199 tree result;
2201 /* These things are certainly not lvalues. */
2202 if (TREE_CODE (x) == NON_LVALUE_EXPR
2203 || TREE_CODE (x) == INTEGER_CST
2204 || TREE_CODE (x) == REAL_CST
2205 || TREE_CODE (x) == STRING_CST
2206 || TREE_CODE (x) == ADDR_EXPR)
2207 return x;
2209 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2210 TREE_CONSTANT (result) = TREE_CONSTANT (x);
2211 return result;
2214 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2215 Zero means allow extended lvalues. */
2217 int pedantic_lvalues;
2219 /* When pedantic, return an expr equal to X but certainly not valid as a
2220 pedantic lvalue. Otherwise, return X. */
2222 tree
2223 pedantic_non_lvalue (x)
2224 tree x;
2226 if (pedantic_lvalues)
2227 return non_lvalue (x);
2228 else
2229 return x;
2232 /* Given a tree comparison code, return the code that is the logical inverse
2233 of the given code. It is not safe to do this for floating-point
2234 comparisons, except for NE_EXPR and EQ_EXPR. */
2236 static enum tree_code
2237 invert_tree_comparison (code)
2238 enum tree_code code;
2240 switch (code)
2242 case EQ_EXPR:
2243 return NE_EXPR;
2244 case NE_EXPR:
2245 return EQ_EXPR;
2246 case GT_EXPR:
2247 return LE_EXPR;
2248 case GE_EXPR:
2249 return LT_EXPR;
2250 case LT_EXPR:
2251 return GE_EXPR;
2252 case LE_EXPR:
2253 return GT_EXPR;
2254 default:
2255 abort ();
2259 /* Similar, but return the comparison that results if the operands are
2260 swapped. This is safe for floating-point. */
2262 static enum tree_code
2263 swap_tree_comparison (code)
2264 enum tree_code code;
2266 switch (code)
2268 case EQ_EXPR:
2269 case NE_EXPR:
2270 return code;
2271 case GT_EXPR:
2272 return LT_EXPR;
2273 case GE_EXPR:
2274 return LE_EXPR;
2275 case LT_EXPR:
2276 return GT_EXPR;
2277 case LE_EXPR:
2278 return GE_EXPR;
2279 default:
2280 abort ();
2284 /* Return nonzero if CODE is a tree code that represents a truth value. */
2286 static int
2287 truth_value_p (code)
2288 enum tree_code code;
2290 return (TREE_CODE_CLASS (code) == '<'
2291 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2292 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2293 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2296 /* Return nonzero if two operands are necessarily equal.
2297 If ONLY_CONST is non-zero, only return non-zero for constants.
2298 This function tests whether the operands are indistinguishable;
2299 it does not test whether they are equal using C's == operation.
2300 The distinction is important for IEEE floating point, because
2301 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2302 (2) two NaNs may be indistinguishable, but NaN!=NaN. */
2305 operand_equal_p (arg0, arg1, only_const)
2306 tree arg0, arg1;
2307 int only_const;
2309 /* If both types don't have the same signedness, then we can't consider
2310 them equal. We must check this before the STRIP_NOPS calls
2311 because they may change the signedness of the arguments. */
2312 if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
2313 return 0;
2315 STRIP_NOPS (arg0);
2316 STRIP_NOPS (arg1);
2318 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2319 /* This is needed for conversions and for COMPONENT_REF.
2320 Might as well play it safe and always test this. */
2321 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2322 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2323 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2324 return 0;
2326 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2327 We don't care about side effects in that case because the SAVE_EXPR
2328 takes care of that for us. In all other cases, two expressions are
2329 equal if they have no side effects. If we have two identical
2330 expressions with side effects that should be treated the same due
2331 to the only side effects being identical SAVE_EXPR's, that will
2332 be detected in the recursive calls below. */
2333 if (arg0 == arg1 && ! only_const
2334 && (TREE_CODE (arg0) == SAVE_EXPR
2335 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2336 return 1;
2338 /* Next handle constant cases, those for which we can return 1 even
2339 if ONLY_CONST is set. */
2340 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2341 switch (TREE_CODE (arg0))
2343 case INTEGER_CST:
2344 return (! TREE_CONSTANT_OVERFLOW (arg0)
2345 && ! TREE_CONSTANT_OVERFLOW (arg1)
2346 && tree_int_cst_equal (arg0, arg1));
2348 case REAL_CST:
2349 return (! TREE_CONSTANT_OVERFLOW (arg0)
2350 && ! TREE_CONSTANT_OVERFLOW (arg1)
2351 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2352 TREE_REAL_CST (arg1)));
2354 case COMPLEX_CST:
2355 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2356 only_const)
2357 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2358 only_const));
2360 case STRING_CST:
2361 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2362 && ! memcmp (TREE_STRING_POINTER (arg0),
2363 TREE_STRING_POINTER (arg1),
2364 TREE_STRING_LENGTH (arg0)));
2366 case ADDR_EXPR:
2367 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2369 default:
2370 break;
2373 if (only_const)
2374 return 0;
2376 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2378 case '1':
2379 /* Two conversions are equal only if signedness and modes match. */
2380 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
2381 && (TREE_UNSIGNED (TREE_TYPE (arg0))
2382 != TREE_UNSIGNED (TREE_TYPE (arg1))))
2383 return 0;
2385 return operand_equal_p (TREE_OPERAND (arg0, 0),
2386 TREE_OPERAND (arg1, 0), 0);
2388 case '<':
2389 case '2':
2390 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)
2391 && operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1),
2393 return 1;
2395 /* For commutative ops, allow the other order. */
2396 return ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MULT_EXPR
2397 || TREE_CODE (arg0) == MIN_EXPR || TREE_CODE (arg0) == MAX_EXPR
2398 || TREE_CODE (arg0) == BIT_IOR_EXPR
2399 || TREE_CODE (arg0) == BIT_XOR_EXPR
2400 || TREE_CODE (arg0) == BIT_AND_EXPR
2401 || TREE_CODE (arg0) == NE_EXPR || TREE_CODE (arg0) == EQ_EXPR)
2402 && operand_equal_p (TREE_OPERAND (arg0, 0),
2403 TREE_OPERAND (arg1, 1), 0)
2404 && operand_equal_p (TREE_OPERAND (arg0, 1),
2405 TREE_OPERAND (arg1, 0), 0));
2407 case 'r':
2408 /* If either of the pointer (or reference) expressions we are dereferencing
2409 contain a side effect, these cannot be equal. */
2410 if (TREE_SIDE_EFFECTS (arg0)
2411 || TREE_SIDE_EFFECTS (arg1))
2412 return 0;
2414 switch (TREE_CODE (arg0))
2416 case INDIRECT_REF:
2417 return operand_equal_p (TREE_OPERAND (arg0, 0),
2418 TREE_OPERAND (arg1, 0), 0);
2420 case COMPONENT_REF:
2421 case ARRAY_REF:
2422 case ARRAY_RANGE_REF:
2423 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2424 TREE_OPERAND (arg1, 0), 0)
2425 && operand_equal_p (TREE_OPERAND (arg0, 1),
2426 TREE_OPERAND (arg1, 1), 0));
2428 case BIT_FIELD_REF:
2429 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2430 TREE_OPERAND (arg1, 0), 0)
2431 && operand_equal_p (TREE_OPERAND (arg0, 1),
2432 TREE_OPERAND (arg1, 1), 0)
2433 && operand_equal_p (TREE_OPERAND (arg0, 2),
2434 TREE_OPERAND (arg1, 2), 0));
2435 default:
2436 return 0;
2439 case 'e':
2440 if (TREE_CODE (arg0) == RTL_EXPR)
2441 return rtx_equal_p (RTL_EXPR_RTL (arg0), RTL_EXPR_RTL (arg1));
2442 return 0;
2444 default:
2445 return 0;
2449 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2450 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2452 When in doubt, return 0. */
2454 static int
2455 operand_equal_for_comparison_p (arg0, arg1, other)
2456 tree arg0, arg1;
2457 tree other;
2459 int unsignedp1, unsignedpo;
2460 tree primarg0, primarg1, primother;
2461 unsigned int correct_width;
2463 if (operand_equal_p (arg0, arg1, 0))
2464 return 1;
2466 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2467 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2468 return 0;
2470 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2471 and see if the inner values are the same. This removes any
2472 signedness comparison, which doesn't matter here. */
2473 primarg0 = arg0, primarg1 = arg1;
2474 STRIP_NOPS (primarg0);
2475 STRIP_NOPS (primarg1);
2476 if (operand_equal_p (primarg0, primarg1, 0))
2477 return 1;
2479 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2480 actual comparison operand, ARG0.
2482 First throw away any conversions to wider types
2483 already present in the operands. */
2485 primarg1 = get_narrower (arg1, &unsignedp1);
2486 primother = get_narrower (other, &unsignedpo);
2488 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2489 if (unsignedp1 == unsignedpo
2490 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2491 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2493 tree type = TREE_TYPE (arg0);
2495 /* Make sure shorter operand is extended the right way
2496 to match the longer operand. */
2497 primarg1 = convert (signed_or_unsigned_type (unsignedp1,
2498 TREE_TYPE (primarg1)),
2499 primarg1);
2501 if (operand_equal_p (arg0, convert (type, primarg1), 0))
2502 return 1;
2505 return 0;
2508 /* See if ARG is an expression that is either a comparison or is performing
2509 arithmetic on comparisons. The comparisons must only be comparing
2510 two different values, which will be stored in *CVAL1 and *CVAL2; if
2511 they are non-zero it means that some operands have already been found.
2512 No variables may be used anywhere else in the expression except in the
2513 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2514 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2516 If this is true, return 1. Otherwise, return zero. */
2518 static int
2519 twoval_comparison_p (arg, cval1, cval2, save_p)
2520 tree arg;
2521 tree *cval1, *cval2;
2522 int *save_p;
2524 enum tree_code code = TREE_CODE (arg);
2525 char class = TREE_CODE_CLASS (code);
2527 /* We can handle some of the 'e' cases here. */
2528 if (class == 'e' && code == TRUTH_NOT_EXPR)
2529 class = '1';
2530 else if (class == 'e'
2531 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2532 || code == COMPOUND_EXPR))
2533 class = '2';
2535 else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0
2536 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2538 /* If we've already found a CVAL1 or CVAL2, this expression is
2539 two complex to handle. */
2540 if (*cval1 || *cval2)
2541 return 0;
2543 class = '1';
2544 *save_p = 1;
2547 switch (class)
2549 case '1':
2550 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2552 case '2':
2553 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2554 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2555 cval1, cval2, save_p));
2557 case 'c':
2558 return 1;
2560 case 'e':
2561 if (code == COND_EXPR)
2562 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2563 cval1, cval2, save_p)
2564 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2565 cval1, cval2, save_p)
2566 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2567 cval1, cval2, save_p));
2568 return 0;
2570 case '<':
2571 /* First see if we can handle the first operand, then the second. For
2572 the second operand, we know *CVAL1 can't be zero. It must be that
2573 one side of the comparison is each of the values; test for the
2574 case where this isn't true by failing if the two operands
2575 are the same. */
2577 if (operand_equal_p (TREE_OPERAND (arg, 0),
2578 TREE_OPERAND (arg, 1), 0))
2579 return 0;
2581 if (*cval1 == 0)
2582 *cval1 = TREE_OPERAND (arg, 0);
2583 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2585 else if (*cval2 == 0)
2586 *cval2 = TREE_OPERAND (arg, 0);
2587 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2589 else
2590 return 0;
2592 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2594 else if (*cval2 == 0)
2595 *cval2 = TREE_OPERAND (arg, 1);
2596 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2598 else
2599 return 0;
2601 return 1;
2603 default:
2604 return 0;
2608 /* ARG is a tree that is known to contain just arithmetic operations and
2609 comparisons. Evaluate the operations in the tree substituting NEW0 for
2610 any occurrence of OLD0 as an operand of a comparison and likewise for
2611 NEW1 and OLD1. */
2613 static tree
2614 eval_subst (arg, old0, new0, old1, new1)
2615 tree arg;
2616 tree old0, new0, old1, new1;
2618 tree type = TREE_TYPE (arg);
2619 enum tree_code code = TREE_CODE (arg);
2620 char class = TREE_CODE_CLASS (code);
2622 /* We can handle some of the 'e' cases here. */
2623 if (class == 'e' && code == TRUTH_NOT_EXPR)
2624 class = '1';
2625 else if (class == 'e'
2626 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2627 class = '2';
2629 switch (class)
2631 case '1':
2632 return fold (build1 (code, type,
2633 eval_subst (TREE_OPERAND (arg, 0),
2634 old0, new0, old1, new1)));
2636 case '2':
2637 return fold (build (code, type,
2638 eval_subst (TREE_OPERAND (arg, 0),
2639 old0, new0, old1, new1),
2640 eval_subst (TREE_OPERAND (arg, 1),
2641 old0, new0, old1, new1)));
2643 case 'e':
2644 switch (code)
2646 case SAVE_EXPR:
2647 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2649 case COMPOUND_EXPR:
2650 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2652 case COND_EXPR:
2653 return fold (build (code, type,
2654 eval_subst (TREE_OPERAND (arg, 0),
2655 old0, new0, old1, new1),
2656 eval_subst (TREE_OPERAND (arg, 1),
2657 old0, new0, old1, new1),
2658 eval_subst (TREE_OPERAND (arg, 2),
2659 old0, new0, old1, new1)));
2660 default:
2661 break;
2663 /* fall through - ??? */
2665 case '<':
2667 tree arg0 = TREE_OPERAND (arg, 0);
2668 tree arg1 = TREE_OPERAND (arg, 1);
2670 /* We need to check both for exact equality and tree equality. The
2671 former will be true if the operand has a side-effect. In that
2672 case, we know the operand occurred exactly once. */
2674 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2675 arg0 = new0;
2676 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2677 arg0 = new1;
2679 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2680 arg1 = new0;
2681 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2682 arg1 = new1;
2684 return fold (build (code, type, arg0, arg1));
2687 default:
2688 return arg;
2692 /* Return a tree for the case when the result of an expression is RESULT
2693 converted to TYPE and OMITTED was previously an operand of the expression
2694 but is now not needed (e.g., we folded OMITTED * 0).
2696 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2697 the conversion of RESULT to TYPE. */
2699 static tree
2700 omit_one_operand (type, result, omitted)
2701 tree type, result, omitted;
2703 tree t = convert (type, result);
2705 if (TREE_SIDE_EFFECTS (omitted))
2706 return build (COMPOUND_EXPR, type, omitted, t);
2708 return non_lvalue (t);
2711 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2713 static tree
2714 pedantic_omit_one_operand (type, result, omitted)
2715 tree type, result, omitted;
2717 tree t = convert (type, result);
2719 if (TREE_SIDE_EFFECTS (omitted))
2720 return build (COMPOUND_EXPR, type, omitted, t);
2722 return pedantic_non_lvalue (t);
2725 /* Return a simplified tree node for the truth-negation of ARG. This
2726 never alters ARG itself. We assume that ARG is an operation that
2727 returns a truth value (0 or 1). */
2729 tree
2730 invert_truthvalue (arg)
2731 tree arg;
2733 tree type = TREE_TYPE (arg);
2734 enum tree_code code = TREE_CODE (arg);
2736 if (code == ERROR_MARK)
2737 return arg;
2739 /* If this is a comparison, we can simply invert it, except for
2740 floating-point non-equality comparisons, in which case we just
2741 enclose a TRUTH_NOT_EXPR around what we have. */
2743 if (TREE_CODE_CLASS (code) == '<')
2745 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
2746 && !flag_unsafe_math_optimizations
2747 && code != NE_EXPR
2748 && code != EQ_EXPR)
2749 return build1 (TRUTH_NOT_EXPR, type, arg);
2750 else
2751 return build (invert_tree_comparison (code), type,
2752 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2755 switch (code)
2757 case INTEGER_CST:
2758 return convert (type, build_int_2 (integer_zerop (arg), 0));
2760 case TRUTH_AND_EXPR:
2761 return build (TRUTH_OR_EXPR, type,
2762 invert_truthvalue (TREE_OPERAND (arg, 0)),
2763 invert_truthvalue (TREE_OPERAND (arg, 1)));
2765 case TRUTH_OR_EXPR:
2766 return build (TRUTH_AND_EXPR, type,
2767 invert_truthvalue (TREE_OPERAND (arg, 0)),
2768 invert_truthvalue (TREE_OPERAND (arg, 1)));
2770 case TRUTH_XOR_EXPR:
2771 /* Here we can invert either operand. We invert the first operand
2772 unless the second operand is a TRUTH_NOT_EXPR in which case our
2773 result is the XOR of the first operand with the inside of the
2774 negation of the second operand. */
2776 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2777 return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2778 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2779 else
2780 return build (TRUTH_XOR_EXPR, type,
2781 invert_truthvalue (TREE_OPERAND (arg, 0)),
2782 TREE_OPERAND (arg, 1));
2784 case TRUTH_ANDIF_EXPR:
2785 return build (TRUTH_ORIF_EXPR, type,
2786 invert_truthvalue (TREE_OPERAND (arg, 0)),
2787 invert_truthvalue (TREE_OPERAND (arg, 1)));
2789 case TRUTH_ORIF_EXPR:
2790 return build (TRUTH_ANDIF_EXPR, type,
2791 invert_truthvalue (TREE_OPERAND (arg, 0)),
2792 invert_truthvalue (TREE_OPERAND (arg, 1)));
2794 case TRUTH_NOT_EXPR:
2795 return TREE_OPERAND (arg, 0);
2797 case COND_EXPR:
2798 return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2799 invert_truthvalue (TREE_OPERAND (arg, 1)),
2800 invert_truthvalue (TREE_OPERAND (arg, 2)));
2802 case COMPOUND_EXPR:
2803 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2804 invert_truthvalue (TREE_OPERAND (arg, 1)));
2806 case WITH_RECORD_EXPR:
2807 return build (WITH_RECORD_EXPR, type,
2808 invert_truthvalue (TREE_OPERAND (arg, 0)),
2809 TREE_OPERAND (arg, 1));
2811 case NON_LVALUE_EXPR:
2812 return invert_truthvalue (TREE_OPERAND (arg, 0));
2814 case NOP_EXPR:
2815 case CONVERT_EXPR:
2816 case FLOAT_EXPR:
2817 return build1 (TREE_CODE (arg), type,
2818 invert_truthvalue (TREE_OPERAND (arg, 0)));
2820 case BIT_AND_EXPR:
2821 if (!integer_onep (TREE_OPERAND (arg, 1)))
2822 break;
2823 return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
2825 case SAVE_EXPR:
2826 return build1 (TRUTH_NOT_EXPR, type, arg);
2828 case CLEANUP_POINT_EXPR:
2829 return build1 (CLEANUP_POINT_EXPR, type,
2830 invert_truthvalue (TREE_OPERAND (arg, 0)));
2832 default:
2833 break;
2835 if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
2836 abort ();
2837 return build1 (TRUTH_NOT_EXPR, type, arg);
2840 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2841 operands are another bit-wise operation with a common input. If so,
2842 distribute the bit operations to save an operation and possibly two if
2843 constants are involved. For example, convert
2844 (A | B) & (A | C) into A | (B & C)
2845 Further simplification will occur if B and C are constants.
2847 If this optimization cannot be done, 0 will be returned. */
2849 static tree
2850 distribute_bit_expr (code, type, arg0, arg1)
2851 enum tree_code code;
2852 tree type;
2853 tree arg0, arg1;
2855 tree common;
2856 tree left, right;
2858 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2859 || TREE_CODE (arg0) == code
2860 || (TREE_CODE (arg0) != BIT_AND_EXPR
2861 && TREE_CODE (arg0) != BIT_IOR_EXPR))
2862 return 0;
2864 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2866 common = TREE_OPERAND (arg0, 0);
2867 left = TREE_OPERAND (arg0, 1);
2868 right = TREE_OPERAND (arg1, 1);
2870 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2872 common = TREE_OPERAND (arg0, 0);
2873 left = TREE_OPERAND (arg0, 1);
2874 right = TREE_OPERAND (arg1, 0);
2876 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2878 common = TREE_OPERAND (arg0, 1);
2879 left = TREE_OPERAND (arg0, 0);
2880 right = TREE_OPERAND (arg1, 1);
2882 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2884 common = TREE_OPERAND (arg0, 1);
2885 left = TREE_OPERAND (arg0, 0);
2886 right = TREE_OPERAND (arg1, 0);
2888 else
2889 return 0;
2891 return fold (build (TREE_CODE (arg0), type, common,
2892 fold (build (code, type, left, right))));
2895 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2896 starting at BITPOS. The field is unsigned if UNSIGNEDP is non-zero. */
2898 static tree
2899 make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
2900 tree inner;
2901 tree type;
2902 int bitsize, bitpos;
2903 int unsignedp;
2905 tree result = build (BIT_FIELD_REF, type, inner,
2906 size_int (bitsize), bitsize_int (bitpos));
2908 TREE_UNSIGNED (result) = unsignedp;
2910 return result;
2913 /* Optimize a bit-field compare.
2915 There are two cases: First is a compare against a constant and the
2916 second is a comparison of two items where the fields are at the same
2917 bit position relative to the start of a chunk (byte, halfword, word)
2918 large enough to contain it. In these cases we can avoid the shift
2919 implicit in bitfield extractions.
2921 For constants, we emit a compare of the shifted constant with the
2922 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2923 compared. For two fields at the same position, we do the ANDs with the
2924 similar mask and compare the result of the ANDs.
2926 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2927 COMPARE_TYPE is the type of the comparison, and LHS and RHS
2928 are the left and right operands of the comparison, respectively.
2930 If the optimization described above can be done, we return the resulting
2931 tree. Otherwise we return zero. */
2933 static tree
2934 optimize_bit_field_compare (code, compare_type, lhs, rhs)
2935 enum tree_code code;
2936 tree compare_type;
2937 tree lhs, rhs;
2939 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
2940 tree type = TREE_TYPE (lhs);
2941 tree signed_type, unsigned_type;
2942 int const_p = TREE_CODE (rhs) == INTEGER_CST;
2943 enum machine_mode lmode, rmode, nmode;
2944 int lunsignedp, runsignedp;
2945 int lvolatilep = 0, rvolatilep = 0;
2946 unsigned int alignment;
2947 tree linner, rinner = NULL_TREE;
2948 tree mask;
2949 tree offset;
2951 /* Get all the information about the extractions being done. If the bit size
2952 if the same as the size of the underlying object, we aren't doing an
2953 extraction at all and so can do nothing. We also don't want to
2954 do anything if the inner expression is a PLACEHOLDER_EXPR since we
2955 then will no longer be able to replace it. */
2956 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
2957 &lunsignedp, &lvolatilep, &alignment);
2958 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
2959 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
2960 return 0;
2962 if (!const_p)
2964 /* If this is not a constant, we can only do something if bit positions,
2965 sizes, and signedness are the same. */
2966 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
2967 &runsignedp, &rvolatilep, &alignment);
2969 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
2970 || lunsignedp != runsignedp || offset != 0
2971 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
2972 return 0;
2975 /* See if we can find a mode to refer to this field. We should be able to,
2976 but fail if we can't. */
2977 nmode = get_best_mode (lbitsize, lbitpos,
2978 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
2979 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
2980 TYPE_ALIGN (TREE_TYPE (rinner))),
2981 word_mode, lvolatilep || rvolatilep);
2982 if (nmode == VOIDmode)
2983 return 0;
2985 /* Set signed and unsigned types of the precision of this mode for the
2986 shifts below. */
2987 signed_type = type_for_mode (nmode, 0);
2988 unsigned_type = type_for_mode (nmode, 1);
2990 /* Compute the bit position and size for the new reference and our offset
2991 within it. If the new reference is the same size as the original, we
2992 won't optimize anything, so return zero. */
2993 nbitsize = GET_MODE_BITSIZE (nmode);
2994 nbitpos = lbitpos & ~ (nbitsize - 1);
2995 lbitpos -= nbitpos;
2996 if (nbitsize == lbitsize)
2997 return 0;
2999 if (BYTES_BIG_ENDIAN)
3000 lbitpos = nbitsize - lbitsize - lbitpos;
3002 /* Make the mask to be used against the extracted field. */
3003 mask = build_int_2 (~0, ~0);
3004 TREE_TYPE (mask) = unsigned_type;
3005 force_fit_type (mask, 0);
3006 mask = convert (unsigned_type, mask);
3007 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3008 mask = const_binop (RSHIFT_EXPR, mask,
3009 size_int (nbitsize - lbitsize - lbitpos), 0);
3011 if (! const_p)
3012 /* If not comparing with constant, just rework the comparison
3013 and return. */
3014 return build (code, compare_type,
3015 build (BIT_AND_EXPR, unsigned_type,
3016 make_bit_field_ref (linner, unsigned_type,
3017 nbitsize, nbitpos, 1),
3018 mask),
3019 build (BIT_AND_EXPR, unsigned_type,
3020 make_bit_field_ref (rinner, unsigned_type,
3021 nbitsize, nbitpos, 1),
3022 mask));
3024 /* Otherwise, we are handling the constant case. See if the constant is too
3025 big for the field. Warn and return a tree of for 0 (false) if so. We do
3026 this not only for its own sake, but to avoid having to test for this
3027 error case below. If we didn't, we might generate wrong code.
3029 For unsigned fields, the constant shifted right by the field length should
3030 be all zero. For signed fields, the high-order bits should agree with
3031 the sign bit. */
3033 if (lunsignedp)
3035 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3036 convert (unsigned_type, rhs),
3037 size_int (lbitsize), 0)))
3039 warning ("comparison is always %d due to width of bitfield",
3040 code == NE_EXPR);
3041 return convert (compare_type,
3042 (code == NE_EXPR
3043 ? integer_one_node : integer_zero_node));
3046 else
3048 tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
3049 size_int (lbitsize - 1), 0);
3050 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3052 warning ("comparison is always %d due to width of bitfield",
3053 code == NE_EXPR);
3054 return convert (compare_type,
3055 (code == NE_EXPR
3056 ? integer_one_node : integer_zero_node));
3060 /* Single-bit compares should always be against zero. */
3061 if (lbitsize == 1 && ! integer_zerop (rhs))
3063 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3064 rhs = convert (type, integer_zero_node);
3067 /* Make a new bitfield reference, shift the constant over the
3068 appropriate number of bits and mask it with the computed mask
3069 (in case this was a signed field). If we changed it, make a new one. */
3070 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
3071 if (lvolatilep)
3073 TREE_SIDE_EFFECTS (lhs) = 1;
3074 TREE_THIS_VOLATILE (lhs) = 1;
3077 rhs = fold (const_binop (BIT_AND_EXPR,
3078 const_binop (LSHIFT_EXPR,
3079 convert (unsigned_type, rhs),
3080 size_int (lbitpos), 0),
3081 mask, 0));
3083 return build (code, compare_type,
3084 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
3085 rhs);
3088 /* Subroutine for fold_truthop: decode a field reference.
3090 If EXP is a comparison reference, we return the innermost reference.
3092 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3093 set to the starting bit number.
3095 If the innermost field can be completely contained in a mode-sized
3096 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3098 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3099 otherwise it is not changed.
3101 *PUNSIGNEDP is set to the signedness of the field.
3103 *PMASK is set to the mask used. This is either contained in a
3104 BIT_AND_EXPR or derived from the width of the field.
3106 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3108 Return 0 if this is not a component reference or is one that we can't
3109 do anything with. */
3111 static tree
3112 decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
3113 pvolatilep, pmask, pand_mask)
3114 tree exp;
3115 HOST_WIDE_INT *pbitsize, *pbitpos;
3116 enum machine_mode *pmode;
3117 int *punsignedp, *pvolatilep;
3118 tree *pmask;
3119 tree *pand_mask;
3121 tree and_mask = 0;
3122 tree mask, inner, offset;
3123 tree unsigned_type;
3124 unsigned int precision;
3125 unsigned int alignment;
3127 /* All the optimizations using this function assume integer fields.
3128 There are problems with FP fields since the type_for_size call
3129 below can fail for, e.g., XFmode. */
3130 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3131 return 0;
3133 STRIP_NOPS (exp);
3135 if (TREE_CODE (exp) == BIT_AND_EXPR)
3137 and_mask = TREE_OPERAND (exp, 1);
3138 exp = TREE_OPERAND (exp, 0);
3139 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3140 if (TREE_CODE (and_mask) != INTEGER_CST)
3141 return 0;
3144 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3145 punsignedp, pvolatilep, &alignment);
3146 if ((inner == exp && and_mask == 0)
3147 || *pbitsize < 0 || offset != 0
3148 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3149 return 0;
3151 /* Compute the mask to access the bitfield. */
3152 unsigned_type = type_for_size (*pbitsize, 1);
3153 precision = TYPE_PRECISION (unsigned_type);
3155 mask = build_int_2 (~0, ~0);
3156 TREE_TYPE (mask) = unsigned_type;
3157 force_fit_type (mask, 0);
3158 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3159 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3161 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3162 if (and_mask != 0)
3163 mask = fold (build (BIT_AND_EXPR, unsigned_type,
3164 convert (unsigned_type, and_mask), mask));
3166 *pmask = mask;
3167 *pand_mask = and_mask;
3168 return inner;
3171 /* Return non-zero if MASK represents a mask of SIZE ones in the low-order
3172 bit positions. */
3174 static int
3175 all_ones_mask_p (mask, size)
3176 tree mask;
3177 int size;
3179 tree type = TREE_TYPE (mask);
3180 unsigned int precision = TYPE_PRECISION (type);
3181 tree tmask;
3183 tmask = build_int_2 (~0, ~0);
3184 TREE_TYPE (tmask) = signed_type (type);
3185 force_fit_type (tmask, 0);
3186 return
3187 tree_int_cst_equal (mask,
3188 const_binop (RSHIFT_EXPR,
3189 const_binop (LSHIFT_EXPR, tmask,
3190 size_int (precision - size),
3192 size_int (precision - size), 0));
3195 /* Subroutine for fold_truthop: determine if an operand is simple enough
3196 to be evaluated unconditionally. */
3198 static int
3199 simple_operand_p (exp)
3200 tree exp;
3202 /* Strip any conversions that don't change the machine mode. */
3203 while ((TREE_CODE (exp) == NOP_EXPR
3204 || TREE_CODE (exp) == CONVERT_EXPR)
3205 && (TYPE_MODE (TREE_TYPE (exp))
3206 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
3207 exp = TREE_OPERAND (exp, 0);
3209 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
3210 || (DECL_P (exp)
3211 && ! TREE_ADDRESSABLE (exp)
3212 && ! TREE_THIS_VOLATILE (exp)
3213 && ! DECL_NONLOCAL (exp)
3214 /* Don't regard global variables as simple. They may be
3215 allocated in ways unknown to the compiler (shared memory,
3216 #pragma weak, etc). */
3217 && ! TREE_PUBLIC (exp)
3218 && ! DECL_EXTERNAL (exp)
3219 /* Loading a static variable is unduly expensive, but global
3220 registers aren't expensive. */
3221 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3224 /* The following functions are subroutines to fold_range_test and allow it to
3225 try to change a logical combination of comparisons into a range test.
3227 For example, both
3228 X == 2 || X == 3 || X == 4 || X == 5
3230 X >= 2 && X <= 5
3231 are converted to
3232 (unsigned) (X - 2) <= 3
3234 We describe each set of comparisons as being either inside or outside
3235 a range, using a variable named like IN_P, and then describe the
3236 range with a lower and upper bound. If one of the bounds is omitted,
3237 it represents either the highest or lowest value of the type.
3239 In the comments below, we represent a range by two numbers in brackets
3240 preceded by a "+" to designate being inside that range, or a "-" to
3241 designate being outside that range, so the condition can be inverted by
3242 flipping the prefix. An omitted bound is represented by a "-". For
3243 example, "- [-, 10]" means being outside the range starting at the lowest
3244 possible value and ending at 10, in other words, being greater than 10.
3245 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3246 always false.
3248 We set up things so that the missing bounds are handled in a consistent
3249 manner so neither a missing bound nor "true" and "false" need to be
3250 handled using a special case. */
3252 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3253 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3254 and UPPER1_P are nonzero if the respective argument is an upper bound
3255 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3256 must be specified for a comparison. ARG1 will be converted to ARG0's
3257 type if both are specified. */
3259 static tree
3260 range_binop (code, type, arg0, upper0_p, arg1, upper1_p)
3261 enum tree_code code;
3262 tree type;
3263 tree arg0, arg1;
3264 int upper0_p, upper1_p;
3266 tree tem;
3267 int result;
3268 int sgn0, sgn1;
3270 /* If neither arg represents infinity, do the normal operation.
3271 Else, if not a comparison, return infinity. Else handle the special
3272 comparison rules. Note that most of the cases below won't occur, but
3273 are handled for consistency. */
3275 if (arg0 != 0 && arg1 != 0)
3277 tem = fold (build (code, type != 0 ? type : TREE_TYPE (arg0),
3278 arg0, convert (TREE_TYPE (arg0), arg1)));
3279 STRIP_NOPS (tem);
3280 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3283 if (TREE_CODE_CLASS (code) != '<')
3284 return 0;
3286 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3287 for neither. In real maths, we cannot assume open ended ranges are
3288 the same. But, this is computer arithmetic, where numbers are finite.
3289 We can therefore make the transformation of any unbounded range with
3290 the value Z, Z being greater than any representable number. This permits
3291 us to treat unbounded ranges as equal. */
3292 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3293 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3294 switch (code)
3296 case EQ_EXPR:
3297 result = sgn0 == sgn1;
3298 break;
3299 case NE_EXPR:
3300 result = sgn0 != sgn1;
3301 break;
3302 case LT_EXPR:
3303 result = sgn0 < sgn1;
3304 break;
3305 case LE_EXPR:
3306 result = sgn0 <= sgn1;
3307 break;
3308 case GT_EXPR:
3309 result = sgn0 > sgn1;
3310 break;
3311 case GE_EXPR:
3312 result = sgn0 >= sgn1;
3313 break;
3314 default:
3315 abort ();
3318 return convert (type, result ? integer_one_node : integer_zero_node);
3321 /* Given EXP, a logical expression, set the range it is testing into
3322 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3323 actually being tested. *PLOW and *PHIGH will be made of the same type
3324 as the returned expression. If EXP is not a comparison, we will most
3325 likely not be returning a useful value and range. */
3327 static tree
3328 make_range (exp, pin_p, plow, phigh)
3329 tree exp;
3330 int *pin_p;
3331 tree *plow, *phigh;
3333 enum tree_code code;
3334 tree arg0 = NULL_TREE, arg1 = NULL_TREE, type = NULL_TREE;
3335 tree orig_type = NULL_TREE;
3336 int in_p, n_in_p;
3337 tree low, high, n_low, n_high;
3339 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3340 and see if we can refine the range. Some of the cases below may not
3341 happen, but it doesn't seem worth worrying about this. We "continue"
3342 the outer loop when we've changed something; otherwise we "break"
3343 the switch, which will "break" the while. */
3345 in_p = 0, low = high = convert (TREE_TYPE (exp), integer_zero_node);
3347 while (1)
3349 code = TREE_CODE (exp);
3351 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3353 arg0 = TREE_OPERAND (exp, 0);
3354 if (TREE_CODE_CLASS (code) == '<'
3355 || TREE_CODE_CLASS (code) == '1'
3356 || TREE_CODE_CLASS (code) == '2')
3357 type = TREE_TYPE (arg0);
3358 if (TREE_CODE_CLASS (code) == '2'
3359 || TREE_CODE_CLASS (code) == '<'
3360 || (TREE_CODE_CLASS (code) == 'e'
3361 && TREE_CODE_LENGTH (code) > 1))
3362 arg1 = TREE_OPERAND (exp, 1);
3365 /* Set ORIG_TYPE as soon as TYPE is non-null so that we do not
3366 lose a cast by accident. */
3367 if (type != NULL_TREE && orig_type == NULL_TREE)
3368 orig_type = type;
3370 switch (code)
3372 case TRUTH_NOT_EXPR:
3373 in_p = ! in_p, exp = arg0;
3374 continue;
3376 case EQ_EXPR: case NE_EXPR:
3377 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3378 /* We can only do something if the range is testing for zero
3379 and if the second operand is an integer constant. Note that
3380 saying something is "in" the range we make is done by
3381 complementing IN_P since it will set in the initial case of
3382 being not equal to zero; "out" is leaving it alone. */
3383 if (low == 0 || high == 0
3384 || ! integer_zerop (low) || ! integer_zerop (high)
3385 || TREE_CODE (arg1) != INTEGER_CST)
3386 break;
3388 switch (code)
3390 case NE_EXPR: /* - [c, c] */
3391 low = high = arg1;
3392 break;
3393 case EQ_EXPR: /* + [c, c] */
3394 in_p = ! in_p, low = high = arg1;
3395 break;
3396 case GT_EXPR: /* - [-, c] */
3397 low = 0, high = arg1;
3398 break;
3399 case GE_EXPR: /* + [c, -] */
3400 in_p = ! in_p, low = arg1, high = 0;
3401 break;
3402 case LT_EXPR: /* - [c, -] */
3403 low = arg1, high = 0;
3404 break;
3405 case LE_EXPR: /* + [-, c] */
3406 in_p = ! in_p, low = 0, high = arg1;
3407 break;
3408 default:
3409 abort ();
3412 exp = arg0;
3414 /* If this is an unsigned comparison, we also know that EXP is
3415 greater than or equal to zero. We base the range tests we make
3416 on that fact, so we record it here so we can parse existing
3417 range tests. */
3418 if (TREE_UNSIGNED (type) && (low == 0 || high == 0))
3420 if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
3421 1, convert (type, integer_zero_node),
3422 NULL_TREE))
3423 break;
3425 in_p = n_in_p, low = n_low, high = n_high;
3427 /* If the high bound is missing, but we
3428 have a low bound, reverse the range so
3429 it goes from zero to the low bound minus 1. */
3430 if (high == 0 && low)
3432 in_p = ! in_p;
3433 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3434 integer_one_node, 0);
3435 low = convert (type, integer_zero_node);
3438 continue;
3440 case NEGATE_EXPR:
3441 /* (-x) IN [a,b] -> x in [-b, -a] */
3442 n_low = range_binop (MINUS_EXPR, type,
3443 convert (type, integer_zero_node), 0, high, 1);
3444 n_high = range_binop (MINUS_EXPR, type,
3445 convert (type, integer_zero_node), 0, low, 0);
3446 low = n_low, high = n_high;
3447 exp = arg0;
3448 continue;
3450 case BIT_NOT_EXPR:
3451 /* ~ X -> -X - 1 */
3452 exp = build (MINUS_EXPR, type, negate_expr (arg0),
3453 convert (type, integer_one_node));
3454 continue;
3456 case PLUS_EXPR: case MINUS_EXPR:
3457 if (TREE_CODE (arg1) != INTEGER_CST)
3458 break;
3460 /* If EXP is signed, any overflow in the computation is undefined,
3461 so we don't worry about it so long as our computations on
3462 the bounds don't overflow. For unsigned, overflow is defined
3463 and this is exactly the right thing. */
3464 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3465 type, low, 0, arg1, 0);
3466 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3467 type, high, 1, arg1, 0);
3468 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3469 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3470 break;
3472 /* Check for an unsigned range which has wrapped around the maximum
3473 value thus making n_high < n_low, and normalize it. */
3474 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3476 low = range_binop (PLUS_EXPR, type, n_high, 0,
3477 integer_one_node, 0);
3478 high = range_binop (MINUS_EXPR, type, n_low, 0,
3479 integer_one_node, 0);
3481 /* If the range is of the form +/- [ x+1, x ], we won't
3482 be able to normalize it. But then, it represents the
3483 whole range or the empty set, so make it
3484 +/- [ -, - ]. */
3485 if (tree_int_cst_equal (n_low, low)
3486 && tree_int_cst_equal (n_high, high))
3487 low = high = 0;
3488 else
3489 in_p = ! in_p;
3491 else
3492 low = n_low, high = n_high;
3494 exp = arg0;
3495 continue;
3497 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
3498 if (TYPE_PRECISION (type) > TYPE_PRECISION (orig_type))
3499 break;
3501 if (! INTEGRAL_TYPE_P (type)
3502 || (low != 0 && ! int_fits_type_p (low, type))
3503 || (high != 0 && ! int_fits_type_p (high, type)))
3504 break;
3506 n_low = low, n_high = high;
3508 if (n_low != 0)
3509 n_low = convert (type, n_low);
3511 if (n_high != 0)
3512 n_high = convert (type, n_high);
3514 /* If we're converting from an unsigned to a signed type,
3515 we will be doing the comparison as unsigned. The tests above
3516 have already verified that LOW and HIGH are both positive.
3518 So we have to make sure that the original unsigned value will
3519 be interpreted as positive. */
3520 if (TREE_UNSIGNED (type) && ! TREE_UNSIGNED (TREE_TYPE (exp)))
3522 tree equiv_type = type_for_mode (TYPE_MODE (type), 1);
3523 tree high_positive;
3525 /* A range without an upper bound is, naturally, unbounded.
3526 Since convert would have cropped a very large value, use
3527 the max value for the destination type. */
3528 high_positive
3529 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3530 : TYPE_MAX_VALUE (type);
3532 high_positive = fold (build (RSHIFT_EXPR, type,
3533 convert (type, high_positive),
3534 convert (type, integer_one_node)));
3536 /* If the low bound is specified, "and" the range with the
3537 range for which the original unsigned value will be
3538 positive. */
3539 if (low != 0)
3541 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3542 1, n_low, n_high,
3543 1, convert (type, integer_zero_node),
3544 high_positive))
3545 break;
3547 in_p = (n_in_p == in_p);
3549 else
3551 /* Otherwise, "or" the range with the range of the input
3552 that will be interpreted as negative. */
3553 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3554 0, n_low, n_high,
3555 1, convert (type, integer_zero_node),
3556 high_positive))
3557 break;
3559 in_p = (in_p != n_in_p);
3563 exp = arg0;
3564 low = n_low, high = n_high;
3565 continue;
3567 default:
3568 break;
3571 break;
3574 /* If EXP is a constant, we can evaluate whether this is true or false. */
3575 if (TREE_CODE (exp) == INTEGER_CST)
3577 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3578 exp, 0, low, 0))
3579 && integer_onep (range_binop (LE_EXPR, integer_type_node,
3580 exp, 1, high, 1)));
3581 low = high = 0;
3582 exp = 0;
3585 *pin_p = in_p, *plow = low, *phigh = high;
3586 return exp;
3589 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3590 type, TYPE, return an expression to test if EXP is in (or out of, depending
3591 on IN_P) the range. */
3593 static tree
3594 build_range_check (type, exp, in_p, low, high)
3595 tree type;
3596 tree exp;
3597 int in_p;
3598 tree low, high;
3600 tree etype = TREE_TYPE (exp);
3601 tree utype, value;
3603 if (! in_p
3604 && (0 != (value = build_range_check (type, exp, 1, low, high))))
3605 return invert_truthvalue (value);
3607 else if (low == 0 && high == 0)
3608 return convert (type, integer_one_node);
3610 else if (low == 0)
3611 return fold (build (LE_EXPR, type, exp, high));
3613 else if (high == 0)
3614 return fold (build (GE_EXPR, type, exp, low));
3616 else if (operand_equal_p (low, high, 0))
3617 return fold (build (EQ_EXPR, type, exp, low));
3619 else if (TREE_UNSIGNED (etype) && integer_zerop (low))
3620 return build_range_check (type, exp, 1, 0, high);
3622 else if (integer_zerop (low))
3624 utype = unsigned_type (etype);
3625 return build_range_check (type, convert (utype, exp), 1, 0,
3626 convert (utype, high));
3629 else if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
3630 && ! TREE_OVERFLOW (value))
3631 return build_range_check (type,
3632 fold (build (MINUS_EXPR, etype, exp, low)),
3633 1, convert (etype, integer_zero_node), value);
3634 else
3635 return 0;
3638 /* Given two ranges, see if we can merge them into one. Return 1 if we
3639 can, 0 if we can't. Set the output range into the specified parameters. */
3641 static int
3642 merge_ranges (pin_p, plow, phigh, in0_p, low0, high0, in1_p, low1, high1)
3643 int *pin_p;
3644 tree *plow, *phigh;
3645 int in0_p, in1_p;
3646 tree low0, high0, low1, high1;
3648 int no_overlap;
3649 int subset;
3650 int temp;
3651 tree tem;
3652 int in_p;
3653 tree low, high;
3654 int lowequal = ((low0 == 0 && low1 == 0)
3655 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3656 low0, 0, low1, 0)));
3657 int highequal = ((high0 == 0 && high1 == 0)
3658 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3659 high0, 1, high1, 1)));
3661 /* Make range 0 be the range that starts first, or ends last if they
3662 start at the same value. Swap them if it isn't. */
3663 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3664 low0, 0, low1, 0))
3665 || (lowequal
3666 && integer_onep (range_binop (GT_EXPR, integer_type_node,
3667 high1, 1, high0, 1))))
3669 temp = in0_p, in0_p = in1_p, in1_p = temp;
3670 tem = low0, low0 = low1, low1 = tem;
3671 tem = high0, high0 = high1, high1 = tem;
3674 /* Now flag two cases, whether the ranges are disjoint or whether the
3675 second range is totally subsumed in the first. Note that the tests
3676 below are simplified by the ones above. */
3677 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3678 high0, 1, low1, 0));
3679 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3680 high1, 1, high0, 1));
3682 /* We now have four cases, depending on whether we are including or
3683 excluding the two ranges. */
3684 if (in0_p && in1_p)
3686 /* If they don't overlap, the result is false. If the second range
3687 is a subset it is the result. Otherwise, the range is from the start
3688 of the second to the end of the first. */
3689 if (no_overlap)
3690 in_p = 0, low = high = 0;
3691 else if (subset)
3692 in_p = 1, low = low1, high = high1;
3693 else
3694 in_p = 1, low = low1, high = high0;
3697 else if (in0_p && ! in1_p)
3699 /* If they don't overlap, the result is the first range. If they are
3700 equal, the result is false. If the second range is a subset of the
3701 first, and the ranges begin at the same place, we go from just after
3702 the end of the first range to the end of the second. If the second
3703 range is not a subset of the first, or if it is a subset and both
3704 ranges end at the same place, the range starts at the start of the
3705 first range and ends just before the second range.
3706 Otherwise, we can't describe this as a single range. */
3707 if (no_overlap)
3708 in_p = 1, low = low0, high = high0;
3709 else if (lowequal && highequal)
3710 in_p = 0, low = high = 0;
3711 else if (subset && lowequal)
3713 in_p = 1, high = high0;
3714 low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
3715 integer_one_node, 0);
3717 else if (! subset || highequal)
3719 in_p = 1, low = low0;
3720 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
3721 integer_one_node, 0);
3723 else
3724 return 0;
3727 else if (! in0_p && in1_p)
3729 /* If they don't overlap, the result is the second range. If the second
3730 is a subset of the first, the result is false. Otherwise,
3731 the range starts just after the first range and ends at the
3732 end of the second. */
3733 if (no_overlap)
3734 in_p = 1, low = low1, high = high1;
3735 else if (subset || highequal)
3736 in_p = 0, low = high = 0;
3737 else
3739 in_p = 1, high = high1;
3740 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3741 integer_one_node, 0);
3745 else
3747 /* The case where we are excluding both ranges. Here the complex case
3748 is if they don't overlap. In that case, the only time we have a
3749 range is if they are adjacent. If the second is a subset of the
3750 first, the result is the first. Otherwise, the range to exclude
3751 starts at the beginning of the first range and ends at the end of the
3752 second. */
3753 if (no_overlap)
3755 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3756 range_binop (PLUS_EXPR, NULL_TREE,
3757 high0, 1,
3758 integer_one_node, 1),
3759 1, low1, 0)))
3760 in_p = 0, low = low0, high = high1;
3761 else
3762 return 0;
3764 else if (subset)
3765 in_p = 0, low = low0, high = high0;
3766 else
3767 in_p = 0, low = low0, high = high1;
3770 *pin_p = in_p, *plow = low, *phigh = high;
3771 return 1;
3774 /* EXP is some logical combination of boolean tests. See if we can
3775 merge it into some range test. Return the new tree if so. */
3777 static tree
3778 fold_range_test (exp)
3779 tree exp;
3781 int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3782 || TREE_CODE (exp) == TRUTH_OR_EXPR);
3783 int in0_p, in1_p, in_p;
3784 tree low0, low1, low, high0, high1, high;
3785 tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
3786 tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
3787 tree tem;
3789 /* If this is an OR operation, invert both sides; we will invert
3790 again at the end. */
3791 if (or_op)
3792 in0_p = ! in0_p, in1_p = ! in1_p;
3794 /* If both expressions are the same, if we can merge the ranges, and we
3795 can build the range test, return it or it inverted. If one of the
3796 ranges is always true or always false, consider it to be the same
3797 expression as the other. */
3798 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
3799 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
3800 in1_p, low1, high1)
3801 && 0 != (tem = (build_range_check (TREE_TYPE (exp),
3802 lhs != 0 ? lhs
3803 : rhs != 0 ? rhs : integer_zero_node,
3804 in_p, low, high))))
3805 return or_op ? invert_truthvalue (tem) : tem;
3807 /* On machines where the branch cost is expensive, if this is a
3808 short-circuited branch and the underlying object on both sides
3809 is the same, make a non-short-circuit operation. */
3810 else if (BRANCH_COST >= 2
3811 && lhs != 0 && rhs != 0
3812 && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3813 || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
3814 && operand_equal_p (lhs, rhs, 0))
3816 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
3817 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
3818 which cases we can't do this. */
3819 if (simple_operand_p (lhs))
3820 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3821 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3822 TREE_TYPE (exp), TREE_OPERAND (exp, 0),
3823 TREE_OPERAND (exp, 1));
3825 else if (global_bindings_p () == 0
3826 && ! contains_placeholder_p (lhs))
3828 tree common = save_expr (lhs);
3830 if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
3831 or_op ? ! in0_p : in0_p,
3832 low0, high0))
3833 && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
3834 or_op ? ! in1_p : in1_p,
3835 low1, high1))))
3836 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3837 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3838 TREE_TYPE (exp), lhs, rhs);
3842 return 0;
3845 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
3846 bit value. Arrange things so the extra bits will be set to zero if and
3847 only if C is signed-extended to its full width. If MASK is nonzero,
3848 it is an INTEGER_CST that should be AND'ed with the extra bits. */
3850 static tree
3851 unextend (c, p, unsignedp, mask)
3852 tree c;
3853 int p;
3854 int unsignedp;
3855 tree mask;
3857 tree type = TREE_TYPE (c);
3858 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
3859 tree temp;
3861 if (p == modesize || unsignedp)
3862 return c;
3864 /* We work by getting just the sign bit into the low-order bit, then
3865 into the high-order bit, then sign-extend. We then XOR that value
3866 with C. */
3867 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
3868 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
3870 /* We must use a signed type in order to get an arithmetic right shift.
3871 However, we must also avoid introducing accidental overflows, so that
3872 a subsequent call to integer_zerop will work. Hence we must
3873 do the type conversion here. At this point, the constant is either
3874 zero or one, and the conversion to a signed type can never overflow.
3875 We could get an overflow if this conversion is done anywhere else. */
3876 if (TREE_UNSIGNED (type))
3877 temp = convert (signed_type (type), temp);
3879 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
3880 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
3881 if (mask != 0)
3882 temp = const_binop (BIT_AND_EXPR, temp, convert (TREE_TYPE (c), mask), 0);
3883 /* If necessary, convert the type back to match the type of C. */
3884 if (TREE_UNSIGNED (type))
3885 temp = convert (type, temp);
3887 return convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
3890 /* Find ways of folding logical expressions of LHS and RHS:
3891 Try to merge two comparisons to the same innermost item.
3892 Look for range tests like "ch >= '0' && ch <= '9'".
3893 Look for combinations of simple terms on machines with expensive branches
3894 and evaluate the RHS unconditionally.
3896 For example, if we have p->a == 2 && p->b == 4 and we can make an
3897 object large enough to span both A and B, we can do this with a comparison
3898 against the object ANDed with the a mask.
3900 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
3901 operations to do this with one comparison.
3903 We check for both normal comparisons and the BIT_AND_EXPRs made this by
3904 function and the one above.
3906 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
3907 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
3909 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
3910 two operands.
3912 We return the simplified tree or 0 if no optimization is possible. */
3914 static tree
3915 fold_truthop (code, truth_type, lhs, rhs)
3916 enum tree_code code;
3917 tree truth_type, lhs, rhs;
3919 /* If this is the "or" of two comparisons, we can do something if
3920 the comparisons are NE_EXPR. If this is the "and", we can do something
3921 if the comparisons are EQ_EXPR. I.e.,
3922 (a->b == 2 && a->c == 4) can become (a->new == NEW).
3924 WANTED_CODE is this operation code. For single bit fields, we can
3925 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
3926 comparison for one-bit fields. */
3928 enum tree_code wanted_code;
3929 enum tree_code lcode, rcode;
3930 tree ll_arg, lr_arg, rl_arg, rr_arg;
3931 tree ll_inner, lr_inner, rl_inner, rr_inner;
3932 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
3933 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
3934 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
3935 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
3936 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
3937 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
3938 enum machine_mode lnmode, rnmode;
3939 tree ll_mask, lr_mask, rl_mask, rr_mask;
3940 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
3941 tree l_const, r_const;
3942 tree lntype, rntype, result;
3943 int first_bit, end_bit;
3944 int volatilep;
3946 /* Start by getting the comparison codes. Fail if anything is volatile.
3947 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
3948 it were surrounded with a NE_EXPR. */
3950 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
3951 return 0;
3953 lcode = TREE_CODE (lhs);
3954 rcode = TREE_CODE (rhs);
3956 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
3957 lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
3959 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
3960 rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
3962 if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
3963 return 0;
3965 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
3966 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
3968 ll_arg = TREE_OPERAND (lhs, 0);
3969 lr_arg = TREE_OPERAND (lhs, 1);
3970 rl_arg = TREE_OPERAND (rhs, 0);
3971 rr_arg = TREE_OPERAND (rhs, 1);
3973 /* If the RHS can be evaluated unconditionally and its operands are
3974 simple, it wins to evaluate the RHS unconditionally on machines
3975 with expensive branches. In this case, this isn't a comparison
3976 that can be merged. Avoid doing this if the RHS is a floating-point
3977 comparison since those can trap. */
3979 if (BRANCH_COST >= 2
3980 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
3981 && simple_operand_p (rl_arg)
3982 && simple_operand_p (rr_arg))
3983 return build (code, truth_type, lhs, rhs);
3985 /* See if the comparisons can be merged. Then get all the parameters for
3986 each side. */
3988 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
3989 || (rcode != EQ_EXPR && rcode != NE_EXPR))
3990 return 0;
3992 volatilep = 0;
3993 ll_inner = decode_field_reference (ll_arg,
3994 &ll_bitsize, &ll_bitpos, &ll_mode,
3995 &ll_unsignedp, &volatilep, &ll_mask,
3996 &ll_and_mask);
3997 lr_inner = decode_field_reference (lr_arg,
3998 &lr_bitsize, &lr_bitpos, &lr_mode,
3999 &lr_unsignedp, &volatilep, &lr_mask,
4000 &lr_and_mask);
4001 rl_inner = decode_field_reference (rl_arg,
4002 &rl_bitsize, &rl_bitpos, &rl_mode,
4003 &rl_unsignedp, &volatilep, &rl_mask,
4004 &rl_and_mask);
4005 rr_inner = decode_field_reference (rr_arg,
4006 &rr_bitsize, &rr_bitpos, &rr_mode,
4007 &rr_unsignedp, &volatilep, &rr_mask,
4008 &rr_and_mask);
4010 /* It must be true that the inner operation on the lhs of each
4011 comparison must be the same if we are to be able to do anything.
4012 Then see if we have constants. If not, the same must be true for
4013 the rhs's. */
4014 if (volatilep || ll_inner == 0 || rl_inner == 0
4015 || ! operand_equal_p (ll_inner, rl_inner, 0))
4016 return 0;
4018 if (TREE_CODE (lr_arg) == INTEGER_CST
4019 && TREE_CODE (rr_arg) == INTEGER_CST)
4020 l_const = lr_arg, r_const = rr_arg;
4021 else if (lr_inner == 0 || rr_inner == 0
4022 || ! operand_equal_p (lr_inner, rr_inner, 0))
4023 return 0;
4024 else
4025 l_const = r_const = 0;
4027 /* If either comparison code is not correct for our logical operation,
4028 fail. However, we can convert a one-bit comparison against zero into
4029 the opposite comparison against that bit being set in the field. */
4031 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
4032 if (lcode != wanted_code)
4034 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
4036 /* Make the left operand unsigned, since we are only interested
4037 in the value of one bit. Otherwise we are doing the wrong
4038 thing below. */
4039 ll_unsignedp = 1;
4040 l_const = ll_mask;
4042 else
4043 return 0;
4046 /* This is analogous to the code for l_const above. */
4047 if (rcode != wanted_code)
4049 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
4051 rl_unsignedp = 1;
4052 r_const = rl_mask;
4054 else
4055 return 0;
4058 /* See if we can find a mode that contains both fields being compared on
4059 the left. If we can't, fail. Otherwise, update all constants and masks
4060 to be relative to a field of that size. */
4061 first_bit = MIN (ll_bitpos, rl_bitpos);
4062 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4063 lnmode = get_best_mode (end_bit - first_bit, first_bit,
4064 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4065 volatilep);
4066 if (lnmode == VOIDmode)
4067 return 0;
4069 lnbitsize = GET_MODE_BITSIZE (lnmode);
4070 lnbitpos = first_bit & ~ (lnbitsize - 1);
4071 lntype = type_for_size (lnbitsize, 1);
4072 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
4074 if (BYTES_BIG_ENDIAN)
4076 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
4077 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
4080 ll_mask = const_binop (LSHIFT_EXPR, convert (lntype, ll_mask),
4081 size_int (xll_bitpos), 0);
4082 rl_mask = const_binop (LSHIFT_EXPR, convert (lntype, rl_mask),
4083 size_int (xrl_bitpos), 0);
4085 if (l_const)
4087 l_const = convert (lntype, l_const);
4088 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
4089 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
4090 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
4091 fold (build1 (BIT_NOT_EXPR,
4092 lntype, ll_mask)),
4093 0)))
4095 warning ("comparison is always %d", wanted_code == NE_EXPR);
4097 return convert (truth_type,
4098 wanted_code == NE_EXPR
4099 ? integer_one_node : integer_zero_node);
4102 if (r_const)
4104 r_const = convert (lntype, r_const);
4105 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
4106 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
4107 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4108 fold (build1 (BIT_NOT_EXPR,
4109 lntype, rl_mask)),
4110 0)))
4112 warning ("comparison is always %d", wanted_code == NE_EXPR);
4114 return convert (truth_type,
4115 wanted_code == NE_EXPR
4116 ? integer_one_node : integer_zero_node);
4120 /* If the right sides are not constant, do the same for it. Also,
4121 disallow this optimization if a size or signedness mismatch occurs
4122 between the left and right sides. */
4123 if (l_const == 0)
4125 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
4126 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4127 /* Make sure the two fields on the right
4128 correspond to the left without being swapped. */
4129 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
4130 return 0;
4132 first_bit = MIN (lr_bitpos, rr_bitpos);
4133 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4134 rnmode = get_best_mode (end_bit - first_bit, first_bit,
4135 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4136 volatilep);
4137 if (rnmode == VOIDmode)
4138 return 0;
4140 rnbitsize = GET_MODE_BITSIZE (rnmode);
4141 rnbitpos = first_bit & ~ (rnbitsize - 1);
4142 rntype = type_for_size (rnbitsize, 1);
4143 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4145 if (BYTES_BIG_ENDIAN)
4147 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4148 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4151 lr_mask = const_binop (LSHIFT_EXPR, convert (rntype, lr_mask),
4152 size_int (xlr_bitpos), 0);
4153 rr_mask = const_binop (LSHIFT_EXPR, convert (rntype, rr_mask),
4154 size_int (xrr_bitpos), 0);
4156 /* Make a mask that corresponds to both fields being compared.
4157 Do this for both items being compared. If the operands are the
4158 same size and the bits being compared are in the same position
4159 then we can do this by masking both and comparing the masked
4160 results. */
4161 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4162 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
4163 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
4165 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4166 ll_unsignedp || rl_unsignedp);
4167 if (! all_ones_mask_p (ll_mask, lnbitsize))
4168 lhs = build (BIT_AND_EXPR, lntype, lhs, ll_mask);
4170 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
4171 lr_unsignedp || rr_unsignedp);
4172 if (! all_ones_mask_p (lr_mask, rnbitsize))
4173 rhs = build (BIT_AND_EXPR, rntype, rhs, lr_mask);
4175 return build (wanted_code, truth_type, lhs, rhs);
4178 /* There is still another way we can do something: If both pairs of
4179 fields being compared are adjacent, we may be able to make a wider
4180 field containing them both.
4182 Note that we still must mask the lhs/rhs expressions. Furthermore,
4183 the mask must be shifted to account for the shift done by
4184 make_bit_field_ref. */
4185 if ((ll_bitsize + ll_bitpos == rl_bitpos
4186 && lr_bitsize + lr_bitpos == rr_bitpos)
4187 || (ll_bitpos == rl_bitpos + rl_bitsize
4188 && lr_bitpos == rr_bitpos + rr_bitsize))
4190 tree type;
4192 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
4193 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
4194 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4195 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4197 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4198 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
4199 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4200 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4202 /* Convert to the smaller type before masking out unwanted bits. */
4203 type = lntype;
4204 if (lntype != rntype)
4206 if (lnbitsize > rnbitsize)
4208 lhs = convert (rntype, lhs);
4209 ll_mask = convert (rntype, ll_mask);
4210 type = rntype;
4212 else if (lnbitsize < rnbitsize)
4214 rhs = convert (lntype, rhs);
4215 lr_mask = convert (lntype, lr_mask);
4216 type = lntype;
4220 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
4221 lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
4223 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
4224 rhs = build (BIT_AND_EXPR, type, rhs, lr_mask);
4226 return build (wanted_code, truth_type, lhs, rhs);
4229 return 0;
4232 /* Handle the case of comparisons with constants. If there is something in
4233 common between the masks, those bits of the constants must be the same.
4234 If not, the condition is always false. Test for this to avoid generating
4235 incorrect code below. */
4236 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
4237 if (! integer_zerop (result)
4238 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
4239 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
4241 if (wanted_code == NE_EXPR)
4243 warning ("`or' of unmatched not-equal tests is always 1");
4244 return convert (truth_type, integer_one_node);
4246 else
4248 warning ("`and' of mutually exclusive equal-tests is always 0");
4249 return convert (truth_type, integer_zero_node);
4253 /* Construct the expression we will return. First get the component
4254 reference we will make. Unless the mask is all ones the width of
4255 that field, perform the mask operation. Then compare with the
4256 merged constant. */
4257 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4258 ll_unsignedp || rl_unsignedp);
4260 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4261 if (! all_ones_mask_p (ll_mask, lnbitsize))
4262 result = build (BIT_AND_EXPR, lntype, result, ll_mask);
4264 return build (wanted_code, truth_type, result,
4265 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
4268 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4269 constant. */
4271 static tree
4272 optimize_minmax_comparison (t)
4273 tree t;
4275 tree type = TREE_TYPE (t);
4276 tree arg0 = TREE_OPERAND (t, 0);
4277 enum tree_code op_code;
4278 tree comp_const = TREE_OPERAND (t, 1);
4279 tree minmax_const;
4280 int consts_equal, consts_lt;
4281 tree inner;
4283 STRIP_SIGN_NOPS (arg0);
4285 op_code = TREE_CODE (arg0);
4286 minmax_const = TREE_OPERAND (arg0, 1);
4287 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
4288 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4289 inner = TREE_OPERAND (arg0, 0);
4291 /* If something does not permit us to optimize, return the original tree. */
4292 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4293 || TREE_CODE (comp_const) != INTEGER_CST
4294 || TREE_CONSTANT_OVERFLOW (comp_const)
4295 || TREE_CODE (minmax_const) != INTEGER_CST
4296 || TREE_CONSTANT_OVERFLOW (minmax_const))
4297 return t;
4299 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4300 and GT_EXPR, doing the rest with recursive calls using logical
4301 simplifications. */
4302 switch (TREE_CODE (t))
4304 case NE_EXPR: case LT_EXPR: case LE_EXPR:
4305 return
4306 invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t)));
4308 case GE_EXPR:
4309 return
4310 fold (build (TRUTH_ORIF_EXPR, type,
4311 optimize_minmax_comparison
4312 (build (EQ_EXPR, type, arg0, comp_const)),
4313 optimize_minmax_comparison
4314 (build (GT_EXPR, type, arg0, comp_const))));
4316 case EQ_EXPR:
4317 if (op_code == MAX_EXPR && consts_equal)
4318 /* MAX (X, 0) == 0 -> X <= 0 */
4319 return fold (build (LE_EXPR, type, inner, comp_const));
4321 else if (op_code == MAX_EXPR && consts_lt)
4322 /* MAX (X, 0) == 5 -> X == 5 */
4323 return fold (build (EQ_EXPR, type, inner, comp_const));
4325 else if (op_code == MAX_EXPR)
4326 /* MAX (X, 0) == -1 -> false */
4327 return omit_one_operand (type, integer_zero_node, inner);
4329 else if (consts_equal)
4330 /* MIN (X, 0) == 0 -> X >= 0 */
4331 return fold (build (GE_EXPR, type, inner, comp_const));
4333 else if (consts_lt)
4334 /* MIN (X, 0) == 5 -> false */
4335 return omit_one_operand (type, integer_zero_node, inner);
4337 else
4338 /* MIN (X, 0) == -1 -> X == -1 */
4339 return fold (build (EQ_EXPR, type, inner, comp_const));
4341 case GT_EXPR:
4342 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
4343 /* MAX (X, 0) > 0 -> X > 0
4344 MAX (X, 0) > 5 -> X > 5 */
4345 return fold (build (GT_EXPR, type, inner, comp_const));
4347 else if (op_code == MAX_EXPR)
4348 /* MAX (X, 0) > -1 -> true */
4349 return omit_one_operand (type, integer_one_node, inner);
4351 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
4352 /* MIN (X, 0) > 0 -> false
4353 MIN (X, 0) > 5 -> false */
4354 return omit_one_operand (type, integer_zero_node, inner);
4356 else
4357 /* MIN (X, 0) > -1 -> X > -1 */
4358 return fold (build (GT_EXPR, type, inner, comp_const));
4360 default:
4361 return t;
4365 /* T is an integer expression that is being multiplied, divided, or taken a
4366 modulus (CODE says which and what kind of divide or modulus) by a
4367 constant C. See if we can eliminate that operation by folding it with
4368 other operations already in T. WIDE_TYPE, if non-null, is a type that
4369 should be used for the computation if wider than our type.
4371 For example, if we are dividing (X * 8) + (Y + 16) by 4, we can return
4372 (X * 2) + (Y + 4). We must, however, be assured that either the original
4373 expression would not overflow or that overflow is undefined for the type
4374 in the language in question.
4376 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
4377 the machine has a multiply-accumulate insn or that this is part of an
4378 addressing calculation.
4380 If we return a non-null expression, it is an equivalent form of the
4381 original computation, but need not be in the original type. */
4383 static tree
4384 extract_muldiv (t, c, code, wide_type)
4385 tree t;
4386 tree c;
4387 enum tree_code code;
4388 tree wide_type;
4390 tree type = TREE_TYPE (t);
4391 enum tree_code tcode = TREE_CODE (t);
4392 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
4393 > GET_MODE_SIZE (TYPE_MODE (type)))
4394 ? wide_type : type);
4395 tree t1, t2;
4396 int same_p = tcode == code;
4397 tree op0 = NULL_TREE, op1 = NULL_TREE;
4399 /* Don't deal with constants of zero here; they confuse the code below. */
4400 if (integer_zerop (c))
4401 return NULL_TREE;
4403 if (TREE_CODE_CLASS (tcode) == '1')
4404 op0 = TREE_OPERAND (t, 0);
4406 if (TREE_CODE_CLASS (tcode) == '2')
4407 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
4409 /* Note that we need not handle conditional operations here since fold
4410 already handles those cases. So just do arithmetic here. */
4411 switch (tcode)
4413 case INTEGER_CST:
4414 /* For a constant, we can always simplify if we are a multiply
4415 or (for divide and modulus) if it is a multiple of our constant. */
4416 if (code == MULT_EXPR
4417 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
4418 return const_binop (code, convert (ctype, t), convert (ctype, c), 0);
4419 break;
4421 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
4422 /* If op0 is an expression, and is unsigned, and the type is
4423 smaller than ctype, then we cannot widen the expression. */
4424 if ((TREE_CODE_CLASS (TREE_CODE (op0)) == '<'
4425 || TREE_CODE_CLASS (TREE_CODE (op0)) == '1'
4426 || TREE_CODE_CLASS (TREE_CODE (op0)) == '2'
4427 || TREE_CODE_CLASS (TREE_CODE (op0)) == 'e')
4428 && TREE_UNSIGNED (TREE_TYPE (op0))
4429 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
4430 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
4431 && (GET_MODE_SIZE (TYPE_MODE (ctype))
4432 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
4433 break;
4435 /* Pass the constant down and see if we can make a simplification. If
4436 we can, replace this expression with the inner simplification for
4437 possible later conversion to our or some other type. */
4438 if (0 != (t1 = extract_muldiv (op0, convert (TREE_TYPE (op0), c), code,
4439 code == MULT_EXPR ? ctype : NULL_TREE)))
4440 return t1;
4441 break;
4443 case NEGATE_EXPR: case ABS_EXPR:
4444 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4445 return fold (build1 (tcode, ctype, convert (ctype, t1)));
4446 break;
4448 case MIN_EXPR: case MAX_EXPR:
4449 /* If widening the type changes the signedness, then we can't perform
4450 this optimization as that changes the result. */
4451 if (TREE_UNSIGNED (ctype) != TREE_UNSIGNED (type))
4452 break;
4454 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
4455 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
4456 && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
4458 if (tree_int_cst_sgn (c) < 0)
4459 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
4461 return fold (build (tcode, ctype, convert (ctype, t1),
4462 convert (ctype, t2)));
4464 break;
4466 case WITH_RECORD_EXPR:
4467 if ((t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code, wide_type)) != 0)
4468 return build (WITH_RECORD_EXPR, TREE_TYPE (t1), t1,
4469 TREE_OPERAND (t, 1));
4470 break;
4472 case SAVE_EXPR:
4473 /* If this has not been evaluated and the operand has no side effects,
4474 we can see if we can do something inside it and make a new one.
4475 Note that this test is overly conservative since we can do this
4476 if the only reason it had side effects is that it was another
4477 similar SAVE_EXPR, but that isn't worth bothering with. */
4478 if (SAVE_EXPR_RTL (t) == 0 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0))
4479 && 0 != (t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code,
4480 wide_type)))
4482 t1 = save_expr (t1);
4483 if (SAVE_EXPR_PERSISTENT_P (t) && TREE_CODE (t1) == SAVE_EXPR)
4484 SAVE_EXPR_PERSISTENT_P (t1) = 1;
4485 if (is_pending_size (t))
4486 put_pending_size (t1);
4487 return t1;
4489 break;
4491 case LSHIFT_EXPR: case RSHIFT_EXPR:
4492 /* If the second operand is constant, this is a multiplication
4493 or floor division, by a power of two, so we can treat it that
4494 way unless the multiplier or divisor overflows. */
4495 if (TREE_CODE (op1) == INTEGER_CST
4496 /* const_binop may not detect overflow correctly,
4497 so check for it explicitly here. */
4498 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
4499 && TREE_INT_CST_HIGH (op1) == 0
4500 && 0 != (t1 = convert (ctype,
4501 const_binop (LSHIFT_EXPR, size_one_node,
4502 op1, 0)))
4503 && ! TREE_OVERFLOW (t1))
4504 return extract_muldiv (build (tcode == LSHIFT_EXPR
4505 ? MULT_EXPR : FLOOR_DIV_EXPR,
4506 ctype, convert (ctype, op0), t1),
4507 c, code, wide_type);
4508 break;
4510 case PLUS_EXPR: case MINUS_EXPR:
4511 /* See if we can eliminate the operation on both sides. If we can, we
4512 can return a new PLUS or MINUS. If we can't, the only remaining
4513 cases where we can do anything are if the second operand is a
4514 constant. */
4515 t1 = extract_muldiv (op0, c, code, wide_type);
4516 t2 = extract_muldiv (op1, c, code, wide_type);
4517 if (t1 != 0 && t2 != 0
4518 && (code == MULT_EXPR
4519 /* If not multiplication, we can only do this if either operand
4520 is divisible by c. */
4521 || multiple_of_p (ctype, op0, c)
4522 || multiple_of_p (ctype, op1, c)))
4523 return fold (build (tcode, ctype, convert (ctype, t1),
4524 convert (ctype, t2)));
4526 /* If this was a subtraction, negate OP1 and set it to be an addition.
4527 This simplifies the logic below. */
4528 if (tcode == MINUS_EXPR)
4529 tcode = PLUS_EXPR, op1 = negate_expr (op1);
4531 if (TREE_CODE (op1) != INTEGER_CST)
4532 break;
4534 /* If either OP1 or C are negative, this optimization is not safe for
4535 some of the division and remainder types while for others we need
4536 to change the code. */
4537 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
4539 if (code == CEIL_DIV_EXPR)
4540 code = FLOOR_DIV_EXPR;
4541 else if (code == CEIL_MOD_EXPR)
4542 code = FLOOR_MOD_EXPR;
4543 else if (code == FLOOR_DIV_EXPR)
4544 code = CEIL_DIV_EXPR;
4545 else if (code == FLOOR_MOD_EXPR)
4546 code = CEIL_MOD_EXPR;
4547 else if (code != MULT_EXPR)
4548 break;
4551 /* If it's a multiply or a division/modulus operation of a multiple
4552 of our constant, do the operation and verify it doesn't overflow. */
4553 if (code == MULT_EXPR
4554 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4556 op1 = const_binop (code, convert (ctype, op1), convert (ctype, c), 0);
4557 if (op1 == 0 || TREE_OVERFLOW (op1))
4558 break;
4560 else
4561 break;
4563 /* If we have an unsigned type is not a sizetype, we cannot widen
4564 the operation since it will change the result if the original
4565 computation overflowed. */
4566 if (TREE_UNSIGNED (ctype)
4567 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
4568 && ctype != type)
4569 break;
4571 /* If we were able to eliminate our operation from the first side,
4572 apply our operation to the second side and reform the PLUS. */
4573 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
4574 return fold (build (tcode, ctype, convert (ctype, t1), op1));
4576 /* The last case is if we are a multiply. In that case, we can
4577 apply the distributive law to commute the multiply and addition
4578 if the multiplication of the constants doesn't overflow. */
4579 if (code == MULT_EXPR)
4580 return fold (build (tcode, ctype, fold (build (code, ctype,
4581 convert (ctype, op0),
4582 convert (ctype, c))),
4583 op1));
4585 break;
4587 case MULT_EXPR:
4588 /* We have a special case here if we are doing something like
4589 (C * 8) % 4 since we know that's zero. */
4590 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
4591 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
4592 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
4593 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4594 return omit_one_operand (type, integer_zero_node, op0);
4596 /* ... fall through ... */
4598 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
4599 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
4600 /* If we can extract our operation from the LHS, do so and return a
4601 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
4602 do something only if the second operand is a constant. */
4603 if (same_p
4604 && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4605 return fold (build (tcode, ctype, convert (ctype, t1),
4606 convert (ctype, op1)));
4607 else if (tcode == MULT_EXPR && code == MULT_EXPR
4608 && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
4609 return fold (build (tcode, ctype, convert (ctype, op0),
4610 convert (ctype, t1)));
4611 else if (TREE_CODE (op1) != INTEGER_CST)
4612 return 0;
4614 /* If these are the same operation types, we can associate them
4615 assuming no overflow. */
4616 if (tcode == code
4617 && 0 != (t1 = const_binop (MULT_EXPR, convert (ctype, op1),
4618 convert (ctype, c), 0))
4619 && ! TREE_OVERFLOW (t1))
4620 return fold (build (tcode, ctype, convert (ctype, op0), t1));
4622 /* If these operations "cancel" each other, we have the main
4623 optimizations of this pass, which occur when either constant is a
4624 multiple of the other, in which case we replace this with either an
4625 operation or CODE or TCODE.
4627 If we have an unsigned type that is not a sizetype, we canot do
4628 this since it will change the result if the original computation
4629 overflowed. */
4630 if ((! TREE_UNSIGNED (ctype)
4631 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
4632 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
4633 || (tcode == MULT_EXPR
4634 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
4635 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
4637 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4638 return fold (build (tcode, ctype, convert (ctype, op0),
4639 convert (ctype,
4640 const_binop (TRUNC_DIV_EXPR,
4641 op1, c, 0))));
4642 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
4643 return fold (build (code, ctype, convert (ctype, op0),
4644 convert (ctype,
4645 const_binop (TRUNC_DIV_EXPR,
4646 c, op1, 0))));
4648 break;
4650 default:
4651 break;
4654 return 0;
4657 /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
4658 S, a SAVE_EXPR, return the expression actually being evaluated. Note
4659 that we may sometimes modify the tree. */
4661 static tree
4662 strip_compound_expr (t, s)
4663 tree t;
4664 tree s;
4666 enum tree_code code = TREE_CODE (t);
4668 /* See if this is the COMPOUND_EXPR we want to eliminate. */
4669 if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
4670 && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
4671 return TREE_OPERAND (t, 1);
4673 /* See if this is a COND_EXPR or a simple arithmetic operator. We
4674 don't bother handling any other types. */
4675 else if (code == COND_EXPR)
4677 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4678 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4679 TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
4681 else if (TREE_CODE_CLASS (code) == '1')
4682 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4683 else if (TREE_CODE_CLASS (code) == '<'
4684 || TREE_CODE_CLASS (code) == '2')
4686 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4687 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4690 return t;
4693 /* Return a node which has the indicated constant VALUE (either 0 or
4694 1), and is of the indicated TYPE. */
4696 static tree
4697 constant_boolean_node (value, type)
4698 int value;
4699 tree type;
4701 if (type == integer_type_node)
4702 return value ? integer_one_node : integer_zero_node;
4703 else if (TREE_CODE (type) == BOOLEAN_TYPE)
4704 return truthvalue_conversion (value ? integer_one_node :
4705 integer_zero_node);
4706 else
4708 tree t = build_int_2 (value, 0);
4710 TREE_TYPE (t) = type;
4711 return t;
4715 /* Utility function for the following routine, to see how complex a nesting of
4716 COND_EXPRs can be. EXPR is the expression and LIMIT is a count beyond which
4717 we don't care (to avoid spending too much time on complex expressions.). */
4719 static int
4720 count_cond (expr, lim)
4721 tree expr;
4722 int lim;
4724 int ctrue, cfalse;
4726 if (TREE_CODE (expr) != COND_EXPR)
4727 return 0;
4728 else if (lim <= 0)
4729 return 0;
4731 ctrue = count_cond (TREE_OPERAND (expr, 1), lim - 1);
4732 cfalse = count_cond (TREE_OPERAND (expr, 2), lim - 1 - ctrue);
4733 return MIN (lim, 1 + ctrue + cfalse);
4736 /* Transform `a + (b ? x : y)' into `x ? (a + b) : (a + y)'.
4737 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
4738 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
4739 expression, and ARG to `a'. If COND_FIRST_P is non-zero, then the
4740 COND is the first argument to CODE; otherwise (as in the example
4741 given here), it is the second argument. TYPE is the type of the
4742 original expression. */
4744 static tree
4745 fold_binary_op_with_conditional_arg (code, type, cond, arg, cond_first_p)
4746 enum tree_code code;
4747 tree type;
4748 tree cond;
4749 tree arg;
4750 int cond_first_p;
4752 tree test, true_value, false_value;
4753 tree lhs = NULL_TREE;
4754 tree rhs = NULL_TREE;
4755 /* In the end, we'll produce a COND_EXPR. Both arms of the
4756 conditional expression will be binary operations. The left-hand
4757 side of the expression to be executed if the condition is true
4758 will be pointed to by TRUE_LHS. Similarly, the right-hand side
4759 of the expression to be executed if the condition is true will be
4760 pointed to by TRUE_RHS. FALSE_LHS and FALSE_RHS are analagous --
4761 but apply to the expression to be executed if the conditional is
4762 false. */
4763 tree *true_lhs;
4764 tree *true_rhs;
4765 tree *false_lhs;
4766 tree *false_rhs;
4767 /* These are the codes to use for the left-hand side and right-hand
4768 side of the COND_EXPR. Normally, they are the same as CODE. */
4769 enum tree_code lhs_code = code;
4770 enum tree_code rhs_code = code;
4771 /* And these are the types of the expressions. */
4772 tree lhs_type = type;
4773 tree rhs_type = type;
4775 if (cond_first_p)
4777 true_rhs = false_rhs = &arg;
4778 true_lhs = &true_value;
4779 false_lhs = &false_value;
4781 else
4783 true_lhs = false_lhs = &arg;
4784 true_rhs = &true_value;
4785 false_rhs = &false_value;
4788 if (TREE_CODE (cond) == COND_EXPR)
4790 test = TREE_OPERAND (cond, 0);
4791 true_value = TREE_OPERAND (cond, 1);
4792 false_value = TREE_OPERAND (cond, 2);
4793 /* If this operand throws an expression, then it does not make
4794 sense to try to perform a logical or arithmetic operation
4795 involving it. Instead of building `a + throw 3' for example,
4796 we simply build `a, throw 3'. */
4797 if (VOID_TYPE_P (TREE_TYPE (true_value)))
4799 lhs_code = COMPOUND_EXPR;
4800 if (!cond_first_p)
4801 lhs_type = void_type_node;
4803 if (VOID_TYPE_P (TREE_TYPE (false_value)))
4805 rhs_code = COMPOUND_EXPR;
4806 if (!cond_first_p)
4807 rhs_type = void_type_node;
4810 else
4812 tree testtype = TREE_TYPE (cond);
4813 test = cond;
4814 true_value = convert (testtype, integer_one_node);
4815 false_value = convert (testtype, integer_zero_node);
4818 /* If ARG is complex we want to make sure we only evaluate
4819 it once. Though this is only required if it is volatile, it
4820 might be more efficient even if it is not. However, if we
4821 succeed in folding one part to a constant, we do not need
4822 to make this SAVE_EXPR. Since we do this optimization
4823 primarily to see if we do end up with constant and this
4824 SAVE_EXPR interferes with later optimizations, suppressing
4825 it when we can is important.
4827 If we are not in a function, we can't make a SAVE_EXPR, so don't
4828 try to do so. Don't try to see if the result is a constant
4829 if an arm is a COND_EXPR since we get exponential behavior
4830 in that case. */
4832 if (TREE_CODE (arg) != SAVE_EXPR && ! TREE_CONSTANT (arg)
4833 && global_bindings_p () == 0
4834 && ((TREE_CODE (arg) != VAR_DECL
4835 && TREE_CODE (arg) != PARM_DECL)
4836 || TREE_SIDE_EFFECTS (arg)))
4838 if (TREE_CODE (true_value) != COND_EXPR)
4839 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4841 if (TREE_CODE (false_value) != COND_EXPR)
4842 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4844 if ((lhs == 0 || ! TREE_CONSTANT (lhs))
4845 && (rhs == 0 || !TREE_CONSTANT (rhs)))
4846 arg = save_expr (arg), lhs = rhs = 0;
4849 if (lhs == 0)
4850 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4851 if (rhs == 0)
4852 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4854 test = fold (build (COND_EXPR, type, test, lhs, rhs));
4856 if (TREE_CODE (arg) == SAVE_EXPR)
4857 return build (COMPOUND_EXPR, type,
4858 convert (void_type_node, arg),
4859 strip_compound_expr (test, arg));
4860 else
4861 return convert (type, test);
4865 /* Perform constant folding and related simplification of EXPR.
4866 The related simplifications include x*1 => x, x*0 => 0, etc.,
4867 and application of the associative law.
4868 NOP_EXPR conversions may be removed freely (as long as we
4869 are careful not to change the C type of the overall expression)
4870 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
4871 but we can constant-fold them if they have constant operands. */
4873 tree
4874 fold (expr)
4875 tree expr;
4877 register tree t = expr;
4878 tree t1 = NULL_TREE;
4879 tree tem;
4880 tree type = TREE_TYPE (expr);
4881 register tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4882 register enum tree_code code = TREE_CODE (t);
4883 register int kind = TREE_CODE_CLASS (code);
4884 int invert;
4885 /* WINS will be nonzero when the switch is done
4886 if all operands are constant. */
4887 int wins = 1;
4889 /* Don't try to process an RTL_EXPR since its operands aren't trees.
4890 Likewise for a SAVE_EXPR that's already been evaluated. */
4891 if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t) != 0))
4892 return t;
4894 /* Return right away if a constant. */
4895 if (kind == 'c')
4896 return t;
4898 #ifdef MAX_INTEGER_COMPUTATION_MODE
4899 check_max_integer_computation_mode (expr);
4900 #endif
4902 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
4904 tree subop;
4906 /* Special case for conversion ops that can have fixed point args. */
4907 arg0 = TREE_OPERAND (t, 0);
4909 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
4910 if (arg0 != 0)
4911 STRIP_SIGN_NOPS (arg0);
4913 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
4914 subop = TREE_REALPART (arg0);
4915 else
4916 subop = arg0;
4918 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
4919 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4920 && TREE_CODE (subop) != REAL_CST
4921 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4923 /* Note that TREE_CONSTANT isn't enough:
4924 static var addresses are constant but we can't
4925 do arithmetic on them. */
4926 wins = 0;
4928 else if (IS_EXPR_CODE_CLASS (kind) || kind == 'r')
4930 register int len = first_rtl_op (code);
4931 register int i;
4932 for (i = 0; i < len; i++)
4934 tree op = TREE_OPERAND (t, i);
4935 tree subop;
4937 if (op == 0)
4938 continue; /* Valid for CALL_EXPR, at least. */
4940 if (kind == '<' || code == RSHIFT_EXPR)
4942 /* Signedness matters here. Perhaps we can refine this
4943 later. */
4944 STRIP_SIGN_NOPS (op);
4946 else
4947 /* Strip any conversions that don't change the mode. */
4948 STRIP_NOPS (op);
4950 if (TREE_CODE (op) == COMPLEX_CST)
4951 subop = TREE_REALPART (op);
4952 else
4953 subop = op;
4955 if (TREE_CODE (subop) != INTEGER_CST
4956 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4957 && TREE_CODE (subop) != REAL_CST
4958 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4960 /* Note that TREE_CONSTANT isn't enough:
4961 static var addresses are constant but we can't
4962 do arithmetic on them. */
4963 wins = 0;
4965 if (i == 0)
4966 arg0 = op;
4967 else if (i == 1)
4968 arg1 = op;
4972 /* If this is a commutative operation, and ARG0 is a constant, move it
4973 to ARG1 to reduce the number of tests below. */
4974 if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
4975 || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
4976 || code == BIT_AND_EXPR)
4977 && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
4979 tem = arg0; arg0 = arg1; arg1 = tem;
4981 tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
4982 TREE_OPERAND (t, 1) = tem;
4985 /* Now WINS is set as described above,
4986 ARG0 is the first operand of EXPR,
4987 and ARG1 is the second operand (if it has more than one operand).
4989 First check for cases where an arithmetic operation is applied to a
4990 compound, conditional, or comparison operation. Push the arithmetic
4991 operation inside the compound or conditional to see if any folding
4992 can then be done. Convert comparison to conditional for this purpose.
4993 The also optimizes non-constant cases that used to be done in
4994 expand_expr.
4996 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
4997 one of the operands is a comparison and the other is a comparison, a
4998 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
4999 code below would make the expression more complex. Change it to a
5000 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
5001 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
5003 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
5004 || code == EQ_EXPR || code == NE_EXPR)
5005 && ((truth_value_p (TREE_CODE (arg0))
5006 && (truth_value_p (TREE_CODE (arg1))
5007 || (TREE_CODE (arg1) == BIT_AND_EXPR
5008 && integer_onep (TREE_OPERAND (arg1, 1)))))
5009 || (truth_value_p (TREE_CODE (arg1))
5010 && (truth_value_p (TREE_CODE (arg0))
5011 || (TREE_CODE (arg0) == BIT_AND_EXPR
5012 && integer_onep (TREE_OPERAND (arg0, 1)))))))
5014 t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
5015 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
5016 : TRUTH_XOR_EXPR,
5017 type, arg0, arg1));
5019 if (code == EQ_EXPR)
5020 t = invert_truthvalue (t);
5022 return t;
5025 if (TREE_CODE_CLASS (code) == '1')
5027 if (TREE_CODE (arg0) == COMPOUND_EXPR)
5028 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5029 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
5030 else if (TREE_CODE (arg0) == COND_EXPR)
5032 t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
5033 fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
5034 fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
5036 /* If this was a conversion, and all we did was to move into
5037 inside the COND_EXPR, bring it back out. But leave it if
5038 it is a conversion from integer to integer and the
5039 result precision is no wider than a word since such a
5040 conversion is cheap and may be optimized away by combine,
5041 while it couldn't if it were outside the COND_EXPR. Then return
5042 so we don't get into an infinite recursion loop taking the
5043 conversion out and then back in. */
5045 if ((code == NOP_EXPR || code == CONVERT_EXPR
5046 || code == NON_LVALUE_EXPR)
5047 && TREE_CODE (t) == COND_EXPR
5048 && TREE_CODE (TREE_OPERAND (t, 1)) == code
5049 && TREE_CODE (TREE_OPERAND (t, 2)) == code
5050 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
5051 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0)))
5052 && ! (INTEGRAL_TYPE_P (TREE_TYPE (t))
5053 && (INTEGRAL_TYPE_P
5054 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))))
5055 && TYPE_PRECISION (TREE_TYPE (t)) <= BITS_PER_WORD))
5056 t = build1 (code, type,
5057 build (COND_EXPR,
5058 TREE_TYPE (TREE_OPERAND
5059 (TREE_OPERAND (t, 1), 0)),
5060 TREE_OPERAND (t, 0),
5061 TREE_OPERAND (TREE_OPERAND (t, 1), 0),
5062 TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
5063 return t;
5065 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
5066 return fold (build (COND_EXPR, type, arg0,
5067 fold (build1 (code, type, integer_one_node)),
5068 fold (build1 (code, type, integer_zero_node))));
5070 else if (TREE_CODE_CLASS (code) == '2'
5071 || TREE_CODE_CLASS (code) == '<')
5073 if (TREE_CODE (arg1) == COMPOUND_EXPR)
5074 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5075 fold (build (code, type,
5076 arg0, TREE_OPERAND (arg1, 1))));
5077 else if ((TREE_CODE (arg1) == COND_EXPR
5078 || (TREE_CODE_CLASS (TREE_CODE (arg1)) == '<'
5079 && TREE_CODE_CLASS (code) != '<'))
5080 && (TREE_CODE (arg0) != COND_EXPR
5081 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
5082 && (! TREE_SIDE_EFFECTS (arg0)
5083 || (global_bindings_p () == 0
5084 && ! contains_placeholder_p (arg0))))
5085 return
5086 fold_binary_op_with_conditional_arg (code, type, arg1, arg0,
5087 /*cond_first_p=*/0);
5088 else if (TREE_CODE (arg0) == COMPOUND_EXPR)
5089 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5090 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5091 else if ((TREE_CODE (arg0) == COND_EXPR
5092 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
5093 && TREE_CODE_CLASS (code) != '<'))
5094 && (TREE_CODE (arg1) != COND_EXPR
5095 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
5096 && (! TREE_SIDE_EFFECTS (arg1)
5097 || (global_bindings_p () == 0
5098 && ! contains_placeholder_p (arg1))))
5099 return
5100 fold_binary_op_with_conditional_arg (code, type, arg0, arg1,
5101 /*cond_first_p=*/1);
5103 else if (TREE_CODE_CLASS (code) == '<'
5104 && TREE_CODE (arg0) == COMPOUND_EXPR)
5105 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5106 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5107 else if (TREE_CODE_CLASS (code) == '<'
5108 && TREE_CODE (arg1) == COMPOUND_EXPR)
5109 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5110 fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
5112 switch (code)
5114 case INTEGER_CST:
5115 case REAL_CST:
5116 case STRING_CST:
5117 case COMPLEX_CST:
5118 case CONSTRUCTOR:
5119 return t;
5121 case CONST_DECL:
5122 return fold (DECL_INITIAL (t));
5124 case NOP_EXPR:
5125 case FLOAT_EXPR:
5126 case CONVERT_EXPR:
5127 case FIX_TRUNC_EXPR:
5128 /* Other kinds of FIX are not handled properly by fold_convert. */
5130 if (TREE_TYPE (TREE_OPERAND (t, 0)) == TREE_TYPE (t))
5131 return TREE_OPERAND (t, 0);
5133 /* Handle cases of two conversions in a row. */
5134 if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
5135 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
5137 tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5138 tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
5139 tree final_type = TREE_TYPE (t);
5140 int inside_int = INTEGRAL_TYPE_P (inside_type);
5141 int inside_ptr = POINTER_TYPE_P (inside_type);
5142 int inside_float = FLOAT_TYPE_P (inside_type);
5143 unsigned int inside_prec = TYPE_PRECISION (inside_type);
5144 int inside_unsignedp = TREE_UNSIGNED (inside_type);
5145 int inter_int = INTEGRAL_TYPE_P (inter_type);
5146 int inter_ptr = POINTER_TYPE_P (inter_type);
5147 int inter_float = FLOAT_TYPE_P (inter_type);
5148 unsigned int inter_prec = TYPE_PRECISION (inter_type);
5149 int inter_unsignedp = TREE_UNSIGNED (inter_type);
5150 int final_int = INTEGRAL_TYPE_P (final_type);
5151 int final_ptr = POINTER_TYPE_P (final_type);
5152 int final_float = FLOAT_TYPE_P (final_type);
5153 unsigned int final_prec = TYPE_PRECISION (final_type);
5154 int final_unsignedp = TREE_UNSIGNED (final_type);
5156 /* In addition to the cases of two conversions in a row
5157 handled below, if we are converting something to its own
5158 type via an object of identical or wider precision, neither
5159 conversion is needed. */
5160 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (final_type)
5161 && ((inter_int && final_int) || (inter_float && final_float))
5162 && inter_prec >= final_prec)
5163 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5165 /* Likewise, if the intermediate and final types are either both
5166 float or both integer, we don't need the middle conversion if
5167 it is wider than the final type and doesn't change the signedness
5168 (for integers). Avoid this if the final type is a pointer
5169 since then we sometimes need the inner conversion. Likewise if
5170 the outer has a precision not equal to the size of its mode. */
5171 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
5172 || (inter_float && inside_float))
5173 && inter_prec >= inside_prec
5174 && (inter_float || inter_unsignedp == inside_unsignedp)
5175 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5176 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5177 && ! final_ptr)
5178 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5180 /* If we have a sign-extension of a zero-extended value, we can
5181 replace that by a single zero-extension. */
5182 if (inside_int && inter_int && final_int
5183 && inside_prec < inter_prec && inter_prec < final_prec
5184 && inside_unsignedp && !inter_unsignedp)
5185 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5187 /* Two conversions in a row are not needed unless:
5188 - some conversion is floating-point (overstrict for now), or
5189 - the intermediate type is narrower than both initial and
5190 final, or
5191 - the intermediate type and innermost type differ in signedness,
5192 and the outermost type is wider than the intermediate, or
5193 - the initial type is a pointer type and the precisions of the
5194 intermediate and final types differ, or
5195 - the final type is a pointer type and the precisions of the
5196 initial and intermediate types differ. */
5197 if (! inside_float && ! inter_float && ! final_float
5198 && (inter_prec > inside_prec || inter_prec > final_prec)
5199 && ! (inside_int && inter_int
5200 && inter_unsignedp != inside_unsignedp
5201 && inter_prec < final_prec)
5202 && ((inter_unsignedp && inter_prec > inside_prec)
5203 == (final_unsignedp && final_prec > inter_prec))
5204 && ! (inside_ptr && inter_prec != final_prec)
5205 && ! (final_ptr && inside_prec != inter_prec)
5206 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5207 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5208 && ! final_ptr)
5209 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5212 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
5213 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
5214 /* Detect assigning a bitfield. */
5215 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
5216 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
5218 /* Don't leave an assignment inside a conversion
5219 unless assigning a bitfield. */
5220 tree prev = TREE_OPERAND (t, 0);
5221 TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
5222 /* First do the assignment, then return converted constant. */
5223 t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
5224 TREE_USED (t) = 1;
5225 return t;
5227 if (!wins)
5229 TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
5230 return t;
5232 return fold_convert (t, arg0);
5234 #if 0 /* This loses on &"foo"[0]. */
5235 case ARRAY_REF:
5237 int i;
5239 /* Fold an expression like: "foo"[2] */
5240 if (TREE_CODE (arg0) == STRING_CST
5241 && TREE_CODE (arg1) == INTEGER_CST
5242 && compare_tree_int (arg1, TREE_STRING_LENGTH (arg0)) < 0)
5244 t = build_int_2 (TREE_STRING_POINTER (arg0)[TREE_INT_CST_LOW (arg))], 0);
5245 TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
5246 force_fit_type (t, 0);
5249 return t;
5250 #endif /* 0 */
5252 case COMPONENT_REF:
5253 if (TREE_CODE (arg0) == CONSTRUCTOR)
5255 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
5256 if (m)
5257 t = TREE_VALUE (m);
5259 return t;
5261 case RANGE_EXPR:
5262 TREE_CONSTANT (t) = wins;
5263 return t;
5265 case NEGATE_EXPR:
5266 if (wins)
5268 if (TREE_CODE (arg0) == INTEGER_CST)
5270 unsigned HOST_WIDE_INT low;
5271 HOST_WIDE_INT high;
5272 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5273 TREE_INT_CST_HIGH (arg0),
5274 &low, &high);
5275 t = build_int_2 (low, high);
5276 TREE_TYPE (t) = type;
5277 TREE_OVERFLOW (t)
5278 = (TREE_OVERFLOW (arg0)
5279 | force_fit_type (t, overflow && !TREE_UNSIGNED (type)));
5280 TREE_CONSTANT_OVERFLOW (t)
5281 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
5283 else if (TREE_CODE (arg0) == REAL_CST)
5284 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5286 else if (TREE_CODE (arg0) == NEGATE_EXPR)
5287 return TREE_OPERAND (arg0, 0);
5289 /* Convert - (a - b) to (b - a) for non-floating-point. */
5290 else if (TREE_CODE (arg0) == MINUS_EXPR
5291 && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
5292 return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
5293 TREE_OPERAND (arg0, 0));
5295 return t;
5297 case ABS_EXPR:
5298 if (wins)
5300 if (TREE_CODE (arg0) == INTEGER_CST)
5302 /* If the value is unsigned, then the absolute value is
5303 the same as the ordinary value. */
5304 if (TREE_UNSIGNED (type))
5305 return arg0;
5306 /* Similarly, if the value is non-negative. */
5307 else if (INT_CST_LT (integer_minus_one_node, arg0))
5308 return arg0;
5309 /* If the value is negative, then the absolute value is
5310 its negation. */
5311 else
5313 unsigned HOST_WIDE_INT low;
5314 HOST_WIDE_INT high;
5315 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5316 TREE_INT_CST_HIGH (arg0),
5317 &low, &high);
5318 t = build_int_2 (low, high);
5319 TREE_TYPE (t) = type;
5320 TREE_OVERFLOW (t)
5321 = (TREE_OVERFLOW (arg0)
5322 | force_fit_type (t, overflow));
5323 TREE_CONSTANT_OVERFLOW (t)
5324 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
5327 else if (TREE_CODE (arg0) == REAL_CST)
5329 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
5330 t = build_real (type,
5331 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5334 else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
5335 return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
5336 return t;
5338 case CONJ_EXPR:
5339 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
5340 return convert (type, arg0);
5341 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
5342 return build (COMPLEX_EXPR, type,
5343 TREE_OPERAND (arg0, 0),
5344 negate_expr (TREE_OPERAND (arg0, 1)));
5345 else if (TREE_CODE (arg0) == COMPLEX_CST)
5346 return build_complex (type, TREE_REALPART (arg0),
5347 negate_expr (TREE_IMAGPART (arg0)));
5348 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
5349 return fold (build (TREE_CODE (arg0), type,
5350 fold (build1 (CONJ_EXPR, type,
5351 TREE_OPERAND (arg0, 0))),
5352 fold (build1 (CONJ_EXPR,
5353 type, TREE_OPERAND (arg0, 1)))));
5354 else if (TREE_CODE (arg0) == CONJ_EXPR)
5355 return TREE_OPERAND (arg0, 0);
5356 return t;
5358 case BIT_NOT_EXPR:
5359 if (wins)
5361 t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
5362 ~ TREE_INT_CST_HIGH (arg0));
5363 TREE_TYPE (t) = type;
5364 force_fit_type (t, 0);
5365 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
5366 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
5368 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
5369 return TREE_OPERAND (arg0, 0);
5370 return t;
5372 case PLUS_EXPR:
5373 /* A + (-B) -> A - B */
5374 if (TREE_CODE (arg1) == NEGATE_EXPR)
5375 return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5376 /* (-A) + B -> B - A */
5377 if (TREE_CODE (arg0) == NEGATE_EXPR)
5378 return fold (build (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
5379 else if (! FLOAT_TYPE_P (type))
5381 if (integer_zerop (arg1))
5382 return non_lvalue (convert (type, arg0));
5384 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
5385 with a constant, and the two constants have no bits in common,
5386 we should treat this as a BIT_IOR_EXPR since this may produce more
5387 simplifications. */
5388 if (TREE_CODE (arg0) == BIT_AND_EXPR
5389 && TREE_CODE (arg1) == BIT_AND_EXPR
5390 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5391 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5392 && integer_zerop (const_binop (BIT_AND_EXPR,
5393 TREE_OPERAND (arg0, 1),
5394 TREE_OPERAND (arg1, 1), 0)))
5396 code = BIT_IOR_EXPR;
5397 goto bit_ior;
5400 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
5401 (plus (plus (mult) (mult)) (foo)) so that we can
5402 take advantage of the factoring cases below. */
5403 if ((TREE_CODE (arg0) == PLUS_EXPR
5404 && TREE_CODE (arg1) == MULT_EXPR)
5405 || (TREE_CODE (arg1) == PLUS_EXPR
5406 && TREE_CODE (arg0) == MULT_EXPR))
5408 tree parg0, parg1, parg, marg;
5410 if (TREE_CODE (arg0) == PLUS_EXPR)
5411 parg = arg0, marg = arg1;
5412 else
5413 parg = arg1, marg = arg0;
5414 parg0 = TREE_OPERAND (parg, 0);
5415 parg1 = TREE_OPERAND (parg, 1);
5416 STRIP_NOPS (parg0);
5417 STRIP_NOPS (parg1);
5419 if (TREE_CODE (parg0) == MULT_EXPR
5420 && TREE_CODE (parg1) != MULT_EXPR)
5421 return fold (build (PLUS_EXPR, type,
5422 fold (build (PLUS_EXPR, type, parg0, marg)),
5423 parg1));
5424 if (TREE_CODE (parg0) != MULT_EXPR
5425 && TREE_CODE (parg1) == MULT_EXPR)
5426 return fold (build (PLUS_EXPR, type,
5427 fold (build (PLUS_EXPR, type, parg1, marg)),
5428 parg0));
5431 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
5433 tree arg00, arg01, arg10, arg11;
5434 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
5436 /* (A * C) + (B * C) -> (A+B) * C.
5437 We are most concerned about the case where C is a constant,
5438 but other combinations show up during loop reduction. Since
5439 it is not difficult, try all four possibilities. */
5441 arg00 = TREE_OPERAND (arg0, 0);
5442 arg01 = TREE_OPERAND (arg0, 1);
5443 arg10 = TREE_OPERAND (arg1, 0);
5444 arg11 = TREE_OPERAND (arg1, 1);
5445 same = NULL_TREE;
5447 if (operand_equal_p (arg01, arg11, 0))
5448 same = arg01, alt0 = arg00, alt1 = arg10;
5449 else if (operand_equal_p (arg00, arg10, 0))
5450 same = arg00, alt0 = arg01, alt1 = arg11;
5451 else if (operand_equal_p (arg00, arg11, 0))
5452 same = arg00, alt0 = arg01, alt1 = arg10;
5453 else if (operand_equal_p (arg01, arg10, 0))
5454 same = arg01, alt0 = arg00, alt1 = arg11;
5456 /* No identical multiplicands; see if we can find a common
5457 power-of-two factor in non-power-of-two multiplies. This
5458 can help in multi-dimensional array access. */
5459 else if (TREE_CODE (arg01) == INTEGER_CST
5460 && TREE_CODE (arg11) == INTEGER_CST
5461 && TREE_INT_CST_HIGH (arg01) == 0
5462 && TREE_INT_CST_HIGH (arg11) == 0)
5464 HOST_WIDE_INT int01, int11, tmp;
5465 int01 = TREE_INT_CST_LOW (arg01);
5466 int11 = TREE_INT_CST_LOW (arg11);
5468 /* Move min of absolute values to int11. */
5469 if ((int01 >= 0 ? int01 : -int01)
5470 < (int11 >= 0 ? int11 : -int11))
5472 tmp = int01, int01 = int11, int11 = tmp;
5473 alt0 = arg00, arg00 = arg10, arg10 = alt0;
5474 alt0 = arg01, arg01 = arg11, arg11 = alt0;
5477 if (exact_log2 (int11) > 0 && int01 % int11 == 0)
5479 alt0 = fold (build (MULT_EXPR, type, arg00,
5480 build_int_2 (int01 / int11, 0)));
5481 alt1 = arg10;
5482 same = arg11;
5486 if (same)
5487 return fold (build (MULT_EXPR, type,
5488 fold (build (PLUS_EXPR, type, alt0, alt1)),
5489 same));
5492 /* In IEEE floating point, x+0 may not equal x. */
5493 else if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5494 || flag_unsafe_math_optimizations)
5495 && real_zerop (arg1))
5496 return non_lvalue (convert (type, arg0));
5497 /* x+(-0) equals x, even for IEEE. */
5498 else if (TREE_CODE (arg1) == REAL_CST
5499 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1)))
5500 return non_lvalue (convert (type, arg0));
5502 bit_rotate:
5503 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
5504 is a rotate of A by C1 bits. */
5505 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
5506 is a rotate of A by B bits. */
5508 register enum tree_code code0, code1;
5509 code0 = TREE_CODE (arg0);
5510 code1 = TREE_CODE (arg1);
5511 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
5512 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
5513 && operand_equal_p (TREE_OPERAND (arg0, 0),
5514 TREE_OPERAND (arg1, 0), 0)
5515 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5517 register tree tree01, tree11;
5518 register enum tree_code code01, code11;
5520 tree01 = TREE_OPERAND (arg0, 1);
5521 tree11 = TREE_OPERAND (arg1, 1);
5522 STRIP_NOPS (tree01);
5523 STRIP_NOPS (tree11);
5524 code01 = TREE_CODE (tree01);
5525 code11 = TREE_CODE (tree11);
5526 if (code01 == INTEGER_CST
5527 && code11 == INTEGER_CST
5528 && TREE_INT_CST_HIGH (tree01) == 0
5529 && TREE_INT_CST_HIGH (tree11) == 0
5530 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
5531 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
5532 return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
5533 code0 == LSHIFT_EXPR ? tree01 : tree11);
5534 else if (code11 == MINUS_EXPR)
5536 tree tree110, tree111;
5537 tree110 = TREE_OPERAND (tree11, 0);
5538 tree111 = TREE_OPERAND (tree11, 1);
5539 STRIP_NOPS (tree110);
5540 STRIP_NOPS (tree111);
5541 if (TREE_CODE (tree110) == INTEGER_CST
5542 && 0 == compare_tree_int (tree110,
5543 TYPE_PRECISION
5544 (TREE_TYPE (TREE_OPERAND
5545 (arg0, 0))))
5546 && operand_equal_p (tree01, tree111, 0))
5547 return build ((code0 == LSHIFT_EXPR
5548 ? LROTATE_EXPR
5549 : RROTATE_EXPR),
5550 type, TREE_OPERAND (arg0, 0), tree01);
5552 else if (code01 == MINUS_EXPR)
5554 tree tree010, tree011;
5555 tree010 = TREE_OPERAND (tree01, 0);
5556 tree011 = TREE_OPERAND (tree01, 1);
5557 STRIP_NOPS (tree010);
5558 STRIP_NOPS (tree011);
5559 if (TREE_CODE (tree010) == INTEGER_CST
5560 && 0 == compare_tree_int (tree010,
5561 TYPE_PRECISION
5562 (TREE_TYPE (TREE_OPERAND
5563 (arg0, 0))))
5564 && operand_equal_p (tree11, tree011, 0))
5565 return build ((code0 != LSHIFT_EXPR
5566 ? LROTATE_EXPR
5567 : RROTATE_EXPR),
5568 type, TREE_OPERAND (arg0, 0), tree11);
5573 associate:
5574 /* In most languages, can't associate operations on floats through
5575 parentheses. Rather than remember where the parentheses were, we
5576 don't associate floats at all. It shouldn't matter much. However,
5577 associating multiplications is only very slightly inaccurate, so do
5578 that if -funsafe-math-optimizations is specified. */
5580 if (! wins
5581 && (! FLOAT_TYPE_P (type)
5582 || (flag_unsafe_math_optimizations && code == MULT_EXPR)))
5584 tree var0, con0, lit0, var1, con1, lit1;
5586 /* Split both trees into variables, constants, and literals. Then
5587 associate each group together, the constants with literals,
5588 then the result with variables. This increases the chances of
5589 literals being recombined later and of generating relocatable
5590 expressions for the sum of a constant and literal. */
5591 var0 = split_tree (arg0, code, &con0, &lit0, 0);
5592 var1 = split_tree (arg1, code, &con1, &lit1, code == MINUS_EXPR);
5594 /* Only do something if we found more than two objects. Otherwise,
5595 nothing has changed and we risk infinite recursion. */
5596 if (2 < ((var0 != 0) + (var1 != 0) + (con0 != 0) + (con1 != 0)
5597 + (lit0 != 0) + (lit1 != 0)))
5599 var0 = associate_trees (var0, var1, code, type);
5600 con0 = associate_trees (con0, con1, code, type);
5601 lit0 = associate_trees (lit0, lit1, code, type);
5602 con0 = associate_trees (con0, lit0, code, type);
5603 return convert (type, associate_trees (var0, con0, code, type));
5607 binary:
5608 #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
5609 if (TREE_CODE (arg1) == REAL_CST)
5610 return t;
5611 #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
5612 if (wins)
5613 t1 = const_binop (code, arg0, arg1, 0);
5614 if (t1 != NULL_TREE)
5616 /* The return value should always have
5617 the same type as the original expression. */
5618 if (TREE_TYPE (t1) != TREE_TYPE (t))
5619 t1 = convert (TREE_TYPE (t), t1);
5621 return t1;
5623 return t;
5625 case MINUS_EXPR:
5626 /* A - (-B) -> A + B */
5627 if (TREE_CODE (arg1) == NEGATE_EXPR)
5628 return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5629 /* (-A) - CST -> (-CST) - A for floating point (what about ints ?) */
5630 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == REAL_CST)
5631 return
5632 fold (build (MINUS_EXPR, type,
5633 build_real (TREE_TYPE (arg1),
5634 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1))),
5635 TREE_OPERAND (arg0, 0)));
5637 if (! FLOAT_TYPE_P (type))
5639 if (! wins && integer_zerop (arg0))
5640 return negate_expr (convert (type, arg1));
5641 if (integer_zerop (arg1))
5642 return non_lvalue (convert (type, arg0));
5644 /* (A * C) - (B * C) -> (A-B) * C. Since we are most concerned
5645 about the case where C is a constant, just try one of the
5646 four possibilities. */
5648 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
5649 && operand_equal_p (TREE_OPERAND (arg0, 1),
5650 TREE_OPERAND (arg1, 1), 0))
5651 return fold (build (MULT_EXPR, type,
5652 fold (build (MINUS_EXPR, type,
5653 TREE_OPERAND (arg0, 0),
5654 TREE_OPERAND (arg1, 0))),
5655 TREE_OPERAND (arg0, 1)));
5658 else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5659 || flag_unsafe_math_optimizations)
5661 /* Except with IEEE floating point, 0-x equals -x. */
5662 if (! wins && real_zerop (arg0))
5663 return negate_expr (convert (type, arg1));
5664 /* Except with IEEE floating point, x-0 equals x. */
5665 if (real_zerop (arg1))
5666 return non_lvalue (convert (type, arg0));
5669 /* Fold &x - &x. This can happen from &x.foo - &x.
5670 This is unsafe for certain floats even in non-IEEE formats.
5671 In IEEE, it is unsafe because it does wrong for NaNs.
5672 Also note that operand_equal_p is always false if an operand
5673 is volatile. */
5675 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
5676 && operand_equal_p (arg0, arg1, 0))
5677 return convert (type, integer_zero_node);
5679 goto associate;
5681 case MULT_EXPR:
5682 /* (-A) * (-B) -> A * B */
5683 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
5684 return fold (build (MULT_EXPR, type, TREE_OPERAND (arg0, 0),
5685 TREE_OPERAND (arg1, 0)));
5687 if (! FLOAT_TYPE_P (type))
5689 if (integer_zerop (arg1))
5690 return omit_one_operand (type, arg1, arg0);
5691 if (integer_onep (arg1))
5692 return non_lvalue (convert (type, arg0));
5694 /* (a * (1 << b)) is (a << b) */
5695 if (TREE_CODE (arg1) == LSHIFT_EXPR
5696 && integer_onep (TREE_OPERAND (arg1, 0)))
5697 return fold (build (LSHIFT_EXPR, type, arg0,
5698 TREE_OPERAND (arg1, 1)));
5699 if (TREE_CODE (arg0) == LSHIFT_EXPR
5700 && integer_onep (TREE_OPERAND (arg0, 0)))
5701 return fold (build (LSHIFT_EXPR, type, arg1,
5702 TREE_OPERAND (arg0, 1)));
5704 if (TREE_CODE (arg1) == INTEGER_CST
5705 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5706 code, NULL_TREE)))
5707 return convert (type, tem);
5710 else
5712 /* x*0 is 0, except for IEEE floating point. */
5713 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5714 || flag_unsafe_math_optimizations)
5715 && real_zerop (arg1))
5716 return omit_one_operand (type, arg1, arg0);
5717 /* In IEEE floating point, x*1 is not equivalent to x for snans.
5718 However, ANSI says we can drop signals,
5719 so we can do this anyway. */
5720 if (real_onep (arg1))
5721 return non_lvalue (convert (type, arg0));
5722 /* x*2 is x+x */
5723 if (! wins && real_twop (arg1) && global_bindings_p () == 0
5724 && ! contains_placeholder_p (arg0))
5726 tree arg = save_expr (arg0);
5727 return build (PLUS_EXPR, type, arg, arg);
5730 goto associate;
5732 case BIT_IOR_EXPR:
5733 bit_ior:
5734 if (integer_all_onesp (arg1))
5735 return omit_one_operand (type, arg1, arg0);
5736 if (integer_zerop (arg1))
5737 return non_lvalue (convert (type, arg0));
5738 t1 = distribute_bit_expr (code, type, arg0, arg1);
5739 if (t1 != NULL_TREE)
5740 return t1;
5742 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
5744 This results in more efficient code for machines without a NAND
5745 instruction. Combine will canonicalize to the first form
5746 which will allow use of NAND instructions provided by the
5747 backend if they exist. */
5748 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5749 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5751 return fold (build1 (BIT_NOT_EXPR, type,
5752 build (BIT_AND_EXPR, type,
5753 TREE_OPERAND (arg0, 0),
5754 TREE_OPERAND (arg1, 0))));
5757 /* See if this can be simplified into a rotate first. If that
5758 is unsuccessful continue in the association code. */
5759 goto bit_rotate;
5761 case BIT_XOR_EXPR:
5762 if (integer_zerop (arg1))
5763 return non_lvalue (convert (type, arg0));
5764 if (integer_all_onesp (arg1))
5765 return fold (build1 (BIT_NOT_EXPR, type, arg0));
5767 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
5768 with a constant, and the two constants have no bits in common,
5769 we should treat this as a BIT_IOR_EXPR since this may produce more
5770 simplifications. */
5771 if (TREE_CODE (arg0) == BIT_AND_EXPR
5772 && TREE_CODE (arg1) == BIT_AND_EXPR
5773 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5774 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5775 && integer_zerop (const_binop (BIT_AND_EXPR,
5776 TREE_OPERAND (arg0, 1),
5777 TREE_OPERAND (arg1, 1), 0)))
5779 code = BIT_IOR_EXPR;
5780 goto bit_ior;
5783 /* See if this can be simplified into a rotate first. If that
5784 is unsuccessful continue in the association code. */
5785 goto bit_rotate;
5787 case BIT_AND_EXPR:
5788 bit_and:
5789 if (integer_all_onesp (arg1))
5790 return non_lvalue (convert (type, arg0));
5791 if (integer_zerop (arg1))
5792 return omit_one_operand (type, arg1, arg0);
5793 t1 = distribute_bit_expr (code, type, arg0, arg1);
5794 if (t1 != NULL_TREE)
5795 return t1;
5796 /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */
5797 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
5798 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
5800 unsigned int prec
5801 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
5803 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5804 && (~TREE_INT_CST_LOW (arg0)
5805 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
5806 return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
5808 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
5809 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5811 unsigned int prec
5812 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
5814 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5815 && (~TREE_INT_CST_LOW (arg1)
5816 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
5817 return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
5820 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
5822 This results in more efficient code for machines without a NOR
5823 instruction. Combine will canonicalize to the first form
5824 which will allow use of NOR instructions provided by the
5825 backend if they exist. */
5826 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5827 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5829 return fold (build1 (BIT_NOT_EXPR, type,
5830 build (BIT_IOR_EXPR, type,
5831 TREE_OPERAND (arg0, 0),
5832 TREE_OPERAND (arg1, 0))));
5835 goto associate;
5837 case BIT_ANDTC_EXPR:
5838 if (integer_all_onesp (arg0))
5839 return non_lvalue (convert (type, arg1));
5840 if (integer_zerop (arg0))
5841 return omit_one_operand (type, arg0, arg1);
5842 if (TREE_CODE (arg1) == INTEGER_CST)
5844 arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
5845 code = BIT_AND_EXPR;
5846 goto bit_and;
5848 goto binary;
5850 case RDIV_EXPR:
5851 /* In most cases, do nothing with a divide by zero. */
5852 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
5853 #ifndef REAL_INFINITY
5854 if (TREE_CODE (arg1) == REAL_CST && real_zerop (arg1))
5855 return t;
5856 #endif
5857 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
5859 /* (-A) / (-B) -> A / B */
5860 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
5861 return fold (build (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
5862 TREE_OPERAND (arg1, 0)));
5864 /* In IEEE floating point, x/1 is not equivalent to x for snans.
5865 However, ANSI says we can drop signals, so we can do this anyway. */
5866 if (real_onep (arg1))
5867 return non_lvalue (convert (type, arg0));
5869 /* If ARG1 is a constant, we can convert this to a multiply by the
5870 reciprocal. This does not have the same rounding properties,
5871 so only do this if -funsafe-math-optimizations. We can actually
5872 always safely do it if ARG1 is a power of two, but it's hard to
5873 tell if it is or not in a portable manner. */
5874 if (TREE_CODE (arg1) == REAL_CST)
5876 if (flag_unsafe_math_optimizations
5877 && 0 != (tem = const_binop (code, build_real (type, dconst1),
5878 arg1, 0)))
5879 return fold (build (MULT_EXPR, type, arg0, tem));
5880 /* Find the reciprocal if optimizing and the result is exact. */
5881 else if (optimize)
5883 REAL_VALUE_TYPE r;
5884 r = TREE_REAL_CST (arg1);
5885 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
5887 tem = build_real (type, r);
5888 return fold (build (MULT_EXPR, type, arg0, tem));
5892 /* Convert A/B/C to A/(B*C). */
5893 if (flag_unsafe_math_optimizations
5894 && TREE_CODE (arg0) == RDIV_EXPR)
5896 return fold (build (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
5897 build (MULT_EXPR, type, TREE_OPERAND (arg0, 1),
5898 arg1)));
5900 /* Convert A/(B/C) to (A/B)*C. */
5901 if (flag_unsafe_math_optimizations
5902 && TREE_CODE (arg1) == RDIV_EXPR)
5904 return fold (build (MULT_EXPR, type,
5905 build (RDIV_EXPR, type, arg0,
5906 TREE_OPERAND (arg1, 0)),
5907 TREE_OPERAND (arg1, 1)));
5909 goto binary;
5911 case TRUNC_DIV_EXPR:
5912 case ROUND_DIV_EXPR:
5913 case FLOOR_DIV_EXPR:
5914 case CEIL_DIV_EXPR:
5915 case EXACT_DIV_EXPR:
5916 if (integer_onep (arg1))
5917 return non_lvalue (convert (type, arg0));
5918 if (integer_zerop (arg1))
5919 return t;
5921 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
5922 operation, EXACT_DIV_EXPR.
5924 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
5925 At one time others generated faster code, it's not clear if they do
5926 after the last round to changes to the DIV code in expmed.c. */
5927 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
5928 && multiple_of_p (type, arg0, arg1))
5929 return fold (build (EXACT_DIV_EXPR, type, arg0, arg1));
5931 if (TREE_CODE (arg1) == INTEGER_CST
5932 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5933 code, NULL_TREE)))
5934 return convert (type, tem);
5936 goto binary;
5938 case CEIL_MOD_EXPR:
5939 case FLOOR_MOD_EXPR:
5940 case ROUND_MOD_EXPR:
5941 case TRUNC_MOD_EXPR:
5942 if (integer_onep (arg1))
5943 return omit_one_operand (type, integer_zero_node, arg0);
5944 if (integer_zerop (arg1))
5945 return t;
5947 if (TREE_CODE (arg1) == INTEGER_CST
5948 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5949 code, NULL_TREE)))
5950 return convert (type, tem);
5952 goto binary;
5954 case LSHIFT_EXPR:
5955 case RSHIFT_EXPR:
5956 case LROTATE_EXPR:
5957 case RROTATE_EXPR:
5958 if (integer_zerop (arg1))
5959 return non_lvalue (convert (type, arg0));
5960 /* Since negative shift count is not well-defined,
5961 don't try to compute it in the compiler. */
5962 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
5963 return t;
5964 /* Rewrite an LROTATE_EXPR by a constant into an
5965 RROTATE_EXPR by a new constant. */
5966 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
5968 TREE_SET_CODE (t, RROTATE_EXPR);
5969 code = RROTATE_EXPR;
5970 TREE_OPERAND (t, 1) = arg1
5971 = const_binop
5972 (MINUS_EXPR,
5973 convert (TREE_TYPE (arg1),
5974 build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0)),
5975 arg1, 0);
5976 if (tree_int_cst_sgn (arg1) < 0)
5977 return t;
5980 /* If we have a rotate of a bit operation with the rotate count and
5981 the second operand of the bit operation both constant,
5982 permute the two operations. */
5983 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
5984 && (TREE_CODE (arg0) == BIT_AND_EXPR
5985 || TREE_CODE (arg0) == BIT_ANDTC_EXPR
5986 || TREE_CODE (arg0) == BIT_IOR_EXPR
5987 || TREE_CODE (arg0) == BIT_XOR_EXPR)
5988 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
5989 return fold (build (TREE_CODE (arg0), type,
5990 fold (build (code, type,
5991 TREE_OPERAND (arg0, 0), arg1)),
5992 fold (build (code, type,
5993 TREE_OPERAND (arg0, 1), arg1))));
5995 /* Two consecutive rotates adding up to the width of the mode can
5996 be ignored. */
5997 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
5998 && TREE_CODE (arg0) == RROTATE_EXPR
5999 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6000 && TREE_INT_CST_HIGH (arg1) == 0
6001 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
6002 && ((TREE_INT_CST_LOW (arg1)
6003 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
6004 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
6005 return TREE_OPERAND (arg0, 0);
6007 goto binary;
6009 case MIN_EXPR:
6010 if (operand_equal_p (arg0, arg1, 0))
6011 return omit_one_operand (type, arg0, arg1);
6012 if (INTEGRAL_TYPE_P (type)
6013 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
6014 return omit_one_operand (type, arg1, arg0);
6015 goto associate;
6017 case MAX_EXPR:
6018 if (operand_equal_p (arg0, arg1, 0))
6019 return omit_one_operand (type, arg0, arg1);
6020 if (INTEGRAL_TYPE_P (type)
6021 && TYPE_MAX_VALUE (type)
6022 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
6023 return omit_one_operand (type, arg1, arg0);
6024 goto associate;
6026 case TRUTH_NOT_EXPR:
6027 /* Note that the operand of this must be an int
6028 and its values must be 0 or 1.
6029 ("true" is a fixed value perhaps depending on the language,
6030 but we don't handle values other than 1 correctly yet.) */
6031 tem = invert_truthvalue (arg0);
6032 /* Avoid infinite recursion. */
6033 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
6034 return t;
6035 return convert (type, tem);
6037 case TRUTH_ANDIF_EXPR:
6038 /* Note that the operands of this must be ints
6039 and their values must be 0 or 1.
6040 ("true" is a fixed value perhaps depending on the language.) */
6041 /* If first arg is constant zero, return it. */
6042 if (integer_zerop (arg0))
6043 return convert (type, arg0);
6044 case TRUTH_AND_EXPR:
6045 /* If either arg is constant true, drop it. */
6046 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6047 return non_lvalue (convert (type, arg1));
6048 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
6049 /* Preserve sequence points. */
6050 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
6051 return non_lvalue (convert (type, arg0));
6052 /* If second arg is constant zero, result is zero, but first arg
6053 must be evaluated. */
6054 if (integer_zerop (arg1))
6055 return omit_one_operand (type, arg1, arg0);
6056 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
6057 case will be handled here. */
6058 if (integer_zerop (arg0))
6059 return omit_one_operand (type, arg0, arg1);
6061 truth_andor:
6062 /* We only do these simplifications if we are optimizing. */
6063 if (!optimize)
6064 return t;
6066 /* Check for things like (A || B) && (A || C). We can convert this
6067 to A || (B && C). Note that either operator can be any of the four
6068 truth and/or operations and the transformation will still be
6069 valid. Also note that we only care about order for the
6070 ANDIF and ORIF operators. If B contains side effects, this
6071 might change the truth-value of A. */
6072 if (TREE_CODE (arg0) == TREE_CODE (arg1)
6073 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
6074 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
6075 || TREE_CODE (arg0) == TRUTH_AND_EXPR
6076 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
6077 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
6079 tree a00 = TREE_OPERAND (arg0, 0);
6080 tree a01 = TREE_OPERAND (arg0, 1);
6081 tree a10 = TREE_OPERAND (arg1, 0);
6082 tree a11 = TREE_OPERAND (arg1, 1);
6083 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
6084 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
6085 && (code == TRUTH_AND_EXPR
6086 || code == TRUTH_OR_EXPR));
6088 if (operand_equal_p (a00, a10, 0))
6089 return fold (build (TREE_CODE (arg0), type, a00,
6090 fold (build (code, type, a01, a11))));
6091 else if (commutative && operand_equal_p (a00, a11, 0))
6092 return fold (build (TREE_CODE (arg0), type, a00,
6093 fold (build (code, type, a01, a10))));
6094 else if (commutative && operand_equal_p (a01, a10, 0))
6095 return fold (build (TREE_CODE (arg0), type, a01,
6096 fold (build (code, type, a00, a11))));
6098 /* This case if tricky because we must either have commutative
6099 operators or else A10 must not have side-effects. */
6101 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
6102 && operand_equal_p (a01, a11, 0))
6103 return fold (build (TREE_CODE (arg0), type,
6104 fold (build (code, type, a00, a10)),
6105 a01));
6108 /* See if we can build a range comparison. */
6109 if (0 != (tem = fold_range_test (t)))
6110 return tem;
6112 /* Check for the possibility of merging component references. If our
6113 lhs is another similar operation, try to merge its rhs with our
6114 rhs. Then try to merge our lhs and rhs. */
6115 if (TREE_CODE (arg0) == code
6116 && 0 != (tem = fold_truthop (code, type,
6117 TREE_OPERAND (arg0, 1), arg1)))
6118 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6120 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
6121 return tem;
6123 return t;
6125 case TRUTH_ORIF_EXPR:
6126 /* Note that the operands of this must be ints
6127 and their values must be 0 or true.
6128 ("true" is a fixed value perhaps depending on the language.) */
6129 /* If first arg is constant true, return it. */
6130 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6131 return convert (type, arg0);
6132 case TRUTH_OR_EXPR:
6133 /* If either arg is constant zero, drop it. */
6134 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
6135 return non_lvalue (convert (type, arg1));
6136 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
6137 /* Preserve sequence points. */
6138 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
6139 return non_lvalue (convert (type, arg0));
6140 /* If second arg is constant true, result is true, but we must
6141 evaluate first arg. */
6142 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
6143 return omit_one_operand (type, arg1, arg0);
6144 /* Likewise for first arg, but note this only occurs here for
6145 TRUTH_OR_EXPR. */
6146 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6147 return omit_one_operand (type, arg0, arg1);
6148 goto truth_andor;
6150 case TRUTH_XOR_EXPR:
6151 /* If either arg is constant zero, drop it. */
6152 if (integer_zerop (arg0))
6153 return non_lvalue (convert (type, arg1));
6154 if (integer_zerop (arg1))
6155 return non_lvalue (convert (type, arg0));
6156 /* If either arg is constant true, this is a logical inversion. */
6157 if (integer_onep (arg0))
6158 return non_lvalue (convert (type, invert_truthvalue (arg1)));
6159 if (integer_onep (arg1))
6160 return non_lvalue (convert (type, invert_truthvalue (arg0)));
6161 return t;
6163 case EQ_EXPR:
6164 case NE_EXPR:
6165 case LT_EXPR:
6166 case GT_EXPR:
6167 case LE_EXPR:
6168 case GE_EXPR:
6169 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
6171 /* (-a) CMP (-b) -> b CMP a */
6172 if (TREE_CODE (arg0) == NEGATE_EXPR
6173 && TREE_CODE (arg1) == NEGATE_EXPR)
6174 return fold (build (code, type, TREE_OPERAND (arg1, 0),
6175 TREE_OPERAND (arg0, 0)));
6176 /* (-a) CMP CST -> a swap(CMP) (-CST) */
6177 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == REAL_CST)
6178 return
6179 fold (build
6180 (swap_tree_comparison (code), type,
6181 TREE_OPERAND (arg0, 0),
6182 build_real (TREE_TYPE (arg1),
6183 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1)))));
6184 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6185 /* a CMP (-0) -> a CMP 0 */
6186 if (TREE_CODE (arg1) == REAL_CST
6187 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1)))
6188 return fold (build (code, type, arg0,
6189 build_real (TREE_TYPE (arg1), dconst0)));
6192 /* If one arg is a constant integer, put it last. */
6193 if (TREE_CODE (arg0) == INTEGER_CST
6194 && TREE_CODE (arg1) != INTEGER_CST)
6196 TREE_OPERAND (t, 0) = arg1;
6197 TREE_OPERAND (t, 1) = arg0;
6198 arg0 = TREE_OPERAND (t, 0);
6199 arg1 = TREE_OPERAND (t, 1);
6200 code = swap_tree_comparison (code);
6201 TREE_SET_CODE (t, code);
6204 /* Convert foo++ == CONST into ++foo == CONST + INCR.
6205 First, see if one arg is constant; find the constant arg
6206 and the other one. */
6208 tree constop = 0, varop = NULL_TREE;
6209 int constopnum = -1;
6211 if (TREE_CONSTANT (arg1))
6212 constopnum = 1, constop = arg1, varop = arg0;
6213 if (TREE_CONSTANT (arg0))
6214 constopnum = 0, constop = arg0, varop = arg1;
6216 if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
6218 /* This optimization is invalid for ordered comparisons
6219 if CONST+INCR overflows or if foo+incr might overflow.
6220 This optimization is invalid for floating point due to rounding.
6221 For pointer types we assume overflow doesn't happen. */
6222 if (POINTER_TYPE_P (TREE_TYPE (varop))
6223 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
6224 && (code == EQ_EXPR || code == NE_EXPR)))
6226 tree newconst
6227 = fold (build (PLUS_EXPR, TREE_TYPE (varop),
6228 constop, TREE_OPERAND (varop, 1)));
6230 /* Do not overwrite the current varop to be a preincrement,
6231 create a new node so that we won't confuse our caller who
6232 might create trees and throw them away, reusing the
6233 arguments that they passed to build. This shows up in
6234 the THEN or ELSE parts of ?: being postincrements. */
6235 varop = build (PREINCREMENT_EXPR, TREE_TYPE (varop),
6236 TREE_OPERAND (varop, 0),
6237 TREE_OPERAND (varop, 1));
6239 /* If VAROP is a reference to a bitfield, we must mask
6240 the constant by the width of the field. */
6241 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6242 && DECL_BIT_FIELD(TREE_OPERAND
6243 (TREE_OPERAND (varop, 0), 1)))
6245 int size
6246 = TREE_INT_CST_LOW (DECL_SIZE
6247 (TREE_OPERAND
6248 (TREE_OPERAND (varop, 0), 1)));
6249 tree mask, unsigned_type;
6250 unsigned int precision;
6251 tree folded_compare;
6253 /* First check whether the comparison would come out
6254 always the same. If we don't do that we would
6255 change the meaning with the masking. */
6256 if (constopnum == 0)
6257 folded_compare = fold (build (code, type, constop,
6258 TREE_OPERAND (varop, 0)));
6259 else
6260 folded_compare = fold (build (code, type,
6261 TREE_OPERAND (varop, 0),
6262 constop));
6263 if (integer_zerop (folded_compare)
6264 || integer_onep (folded_compare))
6265 return omit_one_operand (type, folded_compare, varop);
6267 unsigned_type = type_for_size (size, 1);
6268 precision = TYPE_PRECISION (unsigned_type);
6269 mask = build_int_2 (~0, ~0);
6270 TREE_TYPE (mask) = unsigned_type;
6271 force_fit_type (mask, 0);
6272 mask = const_binop (RSHIFT_EXPR, mask,
6273 size_int (precision - size), 0);
6274 newconst = fold (build (BIT_AND_EXPR,
6275 TREE_TYPE (varop), newconst,
6276 convert (TREE_TYPE (varop),
6277 mask)));
6280 t = build (code, type,
6281 (constopnum == 0) ? newconst : varop,
6282 (constopnum == 1) ? newconst : varop);
6283 return t;
6286 else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
6288 if (POINTER_TYPE_P (TREE_TYPE (varop))
6289 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
6290 && (code == EQ_EXPR || code == NE_EXPR)))
6292 tree newconst
6293 = fold (build (MINUS_EXPR, TREE_TYPE (varop),
6294 constop, TREE_OPERAND (varop, 1)));
6296 /* Do not overwrite the current varop to be a predecrement,
6297 create a new node so that we won't confuse our caller who
6298 might create trees and throw them away, reusing the
6299 arguments that they passed to build. This shows up in
6300 the THEN or ELSE parts of ?: being postdecrements. */
6301 varop = build (PREDECREMENT_EXPR, TREE_TYPE (varop),
6302 TREE_OPERAND (varop, 0),
6303 TREE_OPERAND (varop, 1));
6305 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6306 && DECL_BIT_FIELD(TREE_OPERAND
6307 (TREE_OPERAND (varop, 0), 1)))
6309 int size
6310 = TREE_INT_CST_LOW (DECL_SIZE
6311 (TREE_OPERAND
6312 (TREE_OPERAND (varop, 0), 1)));
6313 tree mask, unsigned_type;
6314 unsigned int precision;
6315 tree folded_compare;
6317 if (constopnum == 0)
6318 folded_compare = fold (build (code, type, constop,
6319 TREE_OPERAND (varop, 0)));
6320 else
6321 folded_compare = fold (build (code, type,
6322 TREE_OPERAND (varop, 0),
6323 constop));
6324 if (integer_zerop (folded_compare)
6325 || integer_onep (folded_compare))
6326 return omit_one_operand (type, folded_compare, varop);
6328 unsigned_type = type_for_size (size, 1);
6329 precision = TYPE_PRECISION (unsigned_type);
6330 mask = build_int_2 (~0, ~0);
6331 TREE_TYPE (mask) = TREE_TYPE (varop);
6332 force_fit_type (mask, 0);
6333 mask = const_binop (RSHIFT_EXPR, mask,
6334 size_int (precision - size), 0);
6335 newconst = fold (build (BIT_AND_EXPR,
6336 TREE_TYPE (varop), newconst,
6337 convert (TREE_TYPE (varop),
6338 mask)));
6341 t = build (code, type,
6342 (constopnum == 0) ? newconst : varop,
6343 (constopnum == 1) ? newconst : varop);
6344 return t;
6349 /* Change X >= CST to X > (CST - 1) if CST is positive. */
6350 if (TREE_CODE (arg1) == INTEGER_CST
6351 && TREE_CODE (arg0) != INTEGER_CST
6352 && tree_int_cst_sgn (arg1) > 0)
6354 switch (TREE_CODE (t))
6356 case GE_EXPR:
6357 code = GT_EXPR;
6358 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6359 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6360 break;
6362 case LT_EXPR:
6363 code = LE_EXPR;
6364 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6365 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6366 break;
6368 default:
6369 break;
6373 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
6374 a MINUS_EXPR of a constant, we can convert it into a comparison with
6375 a revised constant as long as no overflow occurs. */
6376 if ((code == EQ_EXPR || code == NE_EXPR)
6377 && TREE_CODE (arg1) == INTEGER_CST
6378 && (TREE_CODE (arg0) == PLUS_EXPR
6379 || TREE_CODE (arg0) == MINUS_EXPR)
6380 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6381 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
6382 ? MINUS_EXPR : PLUS_EXPR,
6383 arg1, TREE_OPERAND (arg0, 1), 0))
6384 && ! TREE_CONSTANT_OVERFLOW (tem))
6385 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6387 /* Similarly for a NEGATE_EXPR. */
6388 else if ((code == EQ_EXPR || code == NE_EXPR)
6389 && TREE_CODE (arg0) == NEGATE_EXPR
6390 && TREE_CODE (arg1) == INTEGER_CST
6391 && 0 != (tem = negate_expr (arg1))
6392 && TREE_CODE (tem) == INTEGER_CST
6393 && ! TREE_CONSTANT_OVERFLOW (tem))
6394 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6396 /* If we have X - Y == 0, we can convert that to X == Y and similarly
6397 for !=. Don't do this for ordered comparisons due to overflow. */
6398 else if ((code == NE_EXPR || code == EQ_EXPR)
6399 && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
6400 return fold (build (code, type,
6401 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
6403 /* If we are widening one operand of an integer comparison,
6404 see if the other operand is similarly being widened. Perhaps we
6405 can do the comparison in the narrower type. */
6406 else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
6407 && TREE_CODE (arg0) == NOP_EXPR
6408 && (tem = get_unwidened (arg0, NULL_TREE)) != arg0
6409 && (t1 = get_unwidened (arg1, TREE_TYPE (tem))) != 0
6410 && (TREE_TYPE (t1) == TREE_TYPE (tem)
6411 || (TREE_CODE (t1) == INTEGER_CST
6412 && int_fits_type_p (t1, TREE_TYPE (tem)))))
6413 return fold (build (code, type, tem, convert (TREE_TYPE (tem), t1)));
6415 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
6416 constant, we can simplify it. */
6417 else if (TREE_CODE (arg1) == INTEGER_CST
6418 && (TREE_CODE (arg0) == MIN_EXPR
6419 || TREE_CODE (arg0) == MAX_EXPR)
6420 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
6421 return optimize_minmax_comparison (t);
6423 /* If we are comparing an ABS_EXPR with a constant, we can
6424 convert all the cases into explicit comparisons, but they may
6425 well not be faster than doing the ABS and one comparison.
6426 But ABS (X) <= C is a range comparison, which becomes a subtraction
6427 and a comparison, and is probably faster. */
6428 else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6429 && TREE_CODE (arg0) == ABS_EXPR
6430 && ! TREE_SIDE_EFFECTS (arg0)
6431 && (0 != (tem = negate_expr (arg1)))
6432 && TREE_CODE (tem) == INTEGER_CST
6433 && ! TREE_CONSTANT_OVERFLOW (tem))
6434 return fold (build (TRUTH_ANDIF_EXPR, type,
6435 build (GE_EXPR, type, TREE_OPERAND (arg0, 0), tem),
6436 build (LE_EXPR, type,
6437 TREE_OPERAND (arg0, 0), arg1)));
6439 /* If this is an EQ or NE comparison with zero and ARG0 is
6440 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
6441 two operations, but the latter can be done in one less insn
6442 on machines that have only two-operand insns or on which a
6443 constant cannot be the first operand. */
6444 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
6445 && TREE_CODE (arg0) == BIT_AND_EXPR)
6447 if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
6448 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
6449 return
6450 fold (build (code, type,
6451 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6452 build (RSHIFT_EXPR,
6453 TREE_TYPE (TREE_OPERAND (arg0, 0)),
6454 TREE_OPERAND (arg0, 1),
6455 TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
6456 convert (TREE_TYPE (arg0),
6457 integer_one_node)),
6458 arg1));
6459 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
6460 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
6461 return
6462 fold (build (code, type,
6463 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6464 build (RSHIFT_EXPR,
6465 TREE_TYPE (TREE_OPERAND (arg0, 1)),
6466 TREE_OPERAND (arg0, 0),
6467 TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
6468 convert (TREE_TYPE (arg0),
6469 integer_one_node)),
6470 arg1));
6473 /* If this is an NE or EQ comparison of zero against the result of a
6474 signed MOD operation whose second operand is a power of 2, make
6475 the MOD operation unsigned since it is simpler and equivalent. */
6476 if ((code == NE_EXPR || code == EQ_EXPR)
6477 && integer_zerop (arg1)
6478 && ! TREE_UNSIGNED (TREE_TYPE (arg0))
6479 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
6480 || TREE_CODE (arg0) == CEIL_MOD_EXPR
6481 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
6482 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
6483 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6485 tree newtype = unsigned_type (TREE_TYPE (arg0));
6486 tree newmod = build (TREE_CODE (arg0), newtype,
6487 convert (newtype, TREE_OPERAND (arg0, 0)),
6488 convert (newtype, TREE_OPERAND (arg0, 1)));
6490 return build (code, type, newmod, convert (newtype, arg1));
6493 /* If this is an NE comparison of zero with an AND of one, remove the
6494 comparison since the AND will give the correct value. */
6495 if (code == NE_EXPR && integer_zerop (arg1)
6496 && TREE_CODE (arg0) == BIT_AND_EXPR
6497 && integer_onep (TREE_OPERAND (arg0, 1)))
6498 return convert (type, arg0);
6500 /* If we have (A & C) == C where C is a power of 2, convert this into
6501 (A & C) != 0. Similarly for NE_EXPR. */
6502 if ((code == EQ_EXPR || code == NE_EXPR)
6503 && TREE_CODE (arg0) == BIT_AND_EXPR
6504 && integer_pow2p (TREE_OPERAND (arg0, 1))
6505 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
6506 return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
6507 arg0, integer_zero_node);
6509 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
6510 and similarly for >= into !=. */
6511 if ((code == LT_EXPR || code == GE_EXPR)
6512 && TREE_UNSIGNED (TREE_TYPE (arg0))
6513 && TREE_CODE (arg1) == LSHIFT_EXPR
6514 && integer_onep (TREE_OPERAND (arg1, 0)))
6515 return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
6516 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6517 TREE_OPERAND (arg1, 1)),
6518 convert (TREE_TYPE (arg0), integer_zero_node));
6520 else if ((code == LT_EXPR || code == GE_EXPR)
6521 && TREE_UNSIGNED (TREE_TYPE (arg0))
6522 && (TREE_CODE (arg1) == NOP_EXPR
6523 || TREE_CODE (arg1) == CONVERT_EXPR)
6524 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
6525 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
6526 return
6527 build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
6528 convert (TREE_TYPE (arg0),
6529 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6530 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))),
6531 convert (TREE_TYPE (arg0), integer_zero_node));
6533 /* Simplify comparison of something with itself. (For IEEE
6534 floating-point, we can only do some of these simplifications.) */
6535 if (operand_equal_p (arg0, arg1, 0))
6537 switch (code)
6539 case EQ_EXPR:
6540 case GE_EXPR:
6541 case LE_EXPR:
6542 if (! FLOAT_TYPE_P (TREE_TYPE (arg0)))
6543 return constant_boolean_node (1, type);
6544 code = EQ_EXPR;
6545 TREE_SET_CODE (t, code);
6546 break;
6548 case NE_EXPR:
6549 /* For NE, we can only do this simplification if integer. */
6550 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
6551 break;
6552 /* ... fall through ... */
6553 case GT_EXPR:
6554 case LT_EXPR:
6555 return constant_boolean_node (0, type);
6556 default:
6557 abort ();
6561 /* An unsigned comparison against 0 can be simplified. */
6562 if (integer_zerop (arg1)
6563 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6564 || POINTER_TYPE_P (TREE_TYPE (arg1)))
6565 && TREE_UNSIGNED (TREE_TYPE (arg1)))
6567 switch (TREE_CODE (t))
6569 case GT_EXPR:
6570 code = NE_EXPR;
6571 TREE_SET_CODE (t, NE_EXPR);
6572 break;
6573 case LE_EXPR:
6574 code = EQ_EXPR;
6575 TREE_SET_CODE (t, EQ_EXPR);
6576 break;
6577 case GE_EXPR:
6578 return omit_one_operand (type,
6579 convert (type, integer_one_node),
6580 arg0);
6581 case LT_EXPR:
6582 return omit_one_operand (type,
6583 convert (type, integer_zero_node),
6584 arg0);
6585 default:
6586 break;
6590 /* Comparisons with the highest or lowest possible integer of
6591 the specified size will have known values and an unsigned
6592 <= 0x7fffffff can be simplified. */
6594 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
6596 if (TREE_CODE (arg1) == INTEGER_CST
6597 && ! TREE_CONSTANT_OVERFLOW (arg1)
6598 && width <= HOST_BITS_PER_WIDE_INT
6599 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6600 || POINTER_TYPE_P (TREE_TYPE (arg1))))
6602 if (TREE_INT_CST_HIGH (arg1) == 0
6603 && (TREE_INT_CST_LOW (arg1)
6604 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1)
6605 && ! TREE_UNSIGNED (TREE_TYPE (arg1)))
6606 switch (TREE_CODE (t))
6608 case GT_EXPR:
6609 return omit_one_operand (type,
6610 convert (type, integer_zero_node),
6611 arg0);
6612 case GE_EXPR:
6613 TREE_SET_CODE (t, EQ_EXPR);
6614 break;
6616 case LE_EXPR:
6617 return omit_one_operand (type,
6618 convert (type, integer_one_node),
6619 arg0);
6620 case LT_EXPR:
6621 TREE_SET_CODE (t, NE_EXPR);
6622 break;
6624 default:
6625 break;
6628 else if (TREE_INT_CST_HIGH (arg1) == -1
6629 && (- TREE_INT_CST_LOW (arg1)
6630 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)))
6631 && ! TREE_UNSIGNED (TREE_TYPE (arg1)))
6632 switch (TREE_CODE (t))
6634 case LT_EXPR:
6635 return omit_one_operand (type,
6636 convert (type, integer_zero_node),
6637 arg0);
6638 case LE_EXPR:
6639 TREE_SET_CODE (t, EQ_EXPR);
6640 break;
6642 case GE_EXPR:
6643 return omit_one_operand (type,
6644 convert (type, integer_one_node),
6645 arg0);
6646 case GT_EXPR:
6647 TREE_SET_CODE (t, NE_EXPR);
6648 break;
6650 default:
6651 break;
6654 else if (TREE_INT_CST_HIGH (arg1) == 0
6655 && (TREE_INT_CST_LOW (arg1)
6656 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1)
6657 && TREE_UNSIGNED (TREE_TYPE (arg1))
6658 /* signed_type does not work on pointer types. */
6659 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
6661 switch (TREE_CODE (t))
6663 case LE_EXPR:
6664 return fold (build (GE_EXPR, type,
6665 convert (signed_type (TREE_TYPE (arg0)),
6666 arg0),
6667 convert (signed_type (TREE_TYPE (arg1)),
6668 integer_zero_node)));
6669 case GT_EXPR:
6670 return fold (build (LT_EXPR, type,
6671 convert (signed_type (TREE_TYPE (arg0)),
6672 arg0),
6673 convert (signed_type (TREE_TYPE (arg1)),
6674 integer_zero_node)));
6676 default:
6677 break;
6682 /* If we are comparing an expression that just has comparisons
6683 of two integer values, arithmetic expressions of those comparisons,
6684 and constants, we can simplify it. There are only three cases
6685 to check: the two values can either be equal, the first can be
6686 greater, or the second can be greater. Fold the expression for
6687 those three values. Since each value must be 0 or 1, we have
6688 eight possibilities, each of which corresponds to the constant 0
6689 or 1 or one of the six possible comparisons.
6691 This handles common cases like (a > b) == 0 but also handles
6692 expressions like ((x > y) - (y > x)) > 0, which supposedly
6693 occur in macroized code. */
6695 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
6697 tree cval1 = 0, cval2 = 0;
6698 int save_p = 0;
6700 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
6701 /* Don't handle degenerate cases here; they should already
6702 have been handled anyway. */
6703 && cval1 != 0 && cval2 != 0
6704 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
6705 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
6706 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
6707 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
6708 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
6709 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
6710 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
6712 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
6713 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
6715 /* We can't just pass T to eval_subst in case cval1 or cval2
6716 was the same as ARG1. */
6718 tree high_result
6719 = fold (build (code, type,
6720 eval_subst (arg0, cval1, maxval, cval2, minval),
6721 arg1));
6722 tree equal_result
6723 = fold (build (code, type,
6724 eval_subst (arg0, cval1, maxval, cval2, maxval),
6725 arg1));
6726 tree low_result
6727 = fold (build (code, type,
6728 eval_subst (arg0, cval1, minval, cval2, maxval),
6729 arg1));
6731 /* All three of these results should be 0 or 1. Confirm they
6732 are. Then use those values to select the proper code
6733 to use. */
6735 if ((integer_zerop (high_result)
6736 || integer_onep (high_result))
6737 && (integer_zerop (equal_result)
6738 || integer_onep (equal_result))
6739 && (integer_zerop (low_result)
6740 || integer_onep (low_result)))
6742 /* Make a 3-bit mask with the high-order bit being the
6743 value for `>', the next for '=', and the low for '<'. */
6744 switch ((integer_onep (high_result) * 4)
6745 + (integer_onep (equal_result) * 2)
6746 + integer_onep (low_result))
6748 case 0:
6749 /* Always false. */
6750 return omit_one_operand (type, integer_zero_node, arg0);
6751 case 1:
6752 code = LT_EXPR;
6753 break;
6754 case 2:
6755 code = EQ_EXPR;
6756 break;
6757 case 3:
6758 code = LE_EXPR;
6759 break;
6760 case 4:
6761 code = GT_EXPR;
6762 break;
6763 case 5:
6764 code = NE_EXPR;
6765 break;
6766 case 6:
6767 code = GE_EXPR;
6768 break;
6769 case 7:
6770 /* Always true. */
6771 return omit_one_operand (type, integer_one_node, arg0);
6774 t = build (code, type, cval1, cval2);
6775 if (save_p)
6776 return save_expr (t);
6777 else
6778 return fold (t);
6783 /* If this is a comparison of a field, we may be able to simplify it. */
6784 if ((TREE_CODE (arg0) == COMPONENT_REF
6785 || TREE_CODE (arg0) == BIT_FIELD_REF)
6786 && (code == EQ_EXPR || code == NE_EXPR)
6787 /* Handle the constant case even without -O
6788 to make sure the warnings are given. */
6789 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
6791 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
6792 return t1 ? t1 : t;
6795 /* If this is a comparison of complex values and either or both sides
6796 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
6797 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
6798 This may prevent needless evaluations. */
6799 if ((code == EQ_EXPR || code == NE_EXPR)
6800 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
6801 && (TREE_CODE (arg0) == COMPLEX_EXPR
6802 || TREE_CODE (arg1) == COMPLEX_EXPR
6803 || TREE_CODE (arg0) == COMPLEX_CST
6804 || TREE_CODE (arg1) == COMPLEX_CST))
6806 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
6807 tree real0, imag0, real1, imag1;
6809 arg0 = save_expr (arg0);
6810 arg1 = save_expr (arg1);
6811 real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
6812 imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
6813 real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
6814 imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
6816 return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
6817 : TRUTH_ORIF_EXPR),
6818 type,
6819 fold (build (code, type, real0, real1)),
6820 fold (build (code, type, imag0, imag1))));
6823 /* From here on, the only cases we handle are when the result is
6824 known to be a constant.
6826 To compute GT, swap the arguments and do LT.
6827 To compute GE, do LT and invert the result.
6828 To compute LE, swap the arguments, do LT and invert the result.
6829 To compute NE, do EQ and invert the result.
6831 Therefore, the code below must handle only EQ and LT. */
6833 if (code == LE_EXPR || code == GT_EXPR)
6835 tem = arg0, arg0 = arg1, arg1 = tem;
6836 code = swap_tree_comparison (code);
6839 /* Note that it is safe to invert for real values here because we
6840 will check below in the one case that it matters. */
6842 t1 = NULL_TREE;
6843 invert = 0;
6844 if (code == NE_EXPR || code == GE_EXPR)
6846 invert = 1;
6847 code = invert_tree_comparison (code);
6850 /* Compute a result for LT or EQ if args permit;
6851 otherwise return T. */
6852 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
6854 if (code == EQ_EXPR)
6855 t1 = build_int_2 (tree_int_cst_equal (arg0, arg1), 0);
6856 else
6857 t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
6858 ? INT_CST_LT_UNSIGNED (arg0, arg1)
6859 : INT_CST_LT (arg0, arg1)),
6863 #if 0 /* This is no longer useful, but breaks some real code. */
6864 /* Assume a nonexplicit constant cannot equal an explicit one,
6865 since such code would be undefined anyway.
6866 Exception: on sysvr4, using #pragma weak,
6867 a label can come out as 0. */
6868 else if (TREE_CODE (arg1) == INTEGER_CST
6869 && !integer_zerop (arg1)
6870 && TREE_CONSTANT (arg0)
6871 && TREE_CODE (arg0) == ADDR_EXPR
6872 && code == EQ_EXPR)
6873 t1 = build_int_2 (0, 0);
6874 #endif
6875 /* Two real constants can be compared explicitly. */
6876 else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
6878 /* If either operand is a NaN, the result is false with two
6879 exceptions: First, an NE_EXPR is true on NaNs, but that case
6880 is already handled correctly since we will be inverting the
6881 result for NE_EXPR. Second, if we had inverted a LE_EXPR
6882 or a GE_EXPR into a LT_EXPR, we must return true so that it
6883 will be inverted into false. */
6885 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
6886 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
6887 t1 = build_int_2 (invert && code == LT_EXPR, 0);
6889 else if (code == EQ_EXPR)
6890 t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
6891 TREE_REAL_CST (arg1)),
6893 else
6894 t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
6895 TREE_REAL_CST (arg1)),
6899 if (t1 == NULL_TREE)
6900 return t;
6902 if (invert)
6903 TREE_INT_CST_LOW (t1) ^= 1;
6905 TREE_TYPE (t1) = type;
6906 if (TREE_CODE (type) == BOOLEAN_TYPE)
6907 return truthvalue_conversion (t1);
6908 return t1;
6910 case COND_EXPR:
6911 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
6912 so all simple results must be passed through pedantic_non_lvalue. */
6913 if (TREE_CODE (arg0) == INTEGER_CST)
6914 return pedantic_non_lvalue
6915 (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
6916 else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
6917 return pedantic_omit_one_operand (type, arg1, arg0);
6919 /* If the second operand is zero, invert the comparison and swap
6920 the second and third operands. Likewise if the second operand
6921 is constant and the third is not or if the third operand is
6922 equivalent to the first operand of the comparison. */
6924 if (integer_zerop (arg1)
6925 || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
6926 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
6927 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
6928 TREE_OPERAND (t, 2),
6929 TREE_OPERAND (arg0, 1))))
6931 /* See if this can be inverted. If it can't, possibly because
6932 it was a floating-point inequality comparison, don't do
6933 anything. */
6934 tem = invert_truthvalue (arg0);
6936 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
6938 t = build (code, type, tem,
6939 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
6940 arg0 = tem;
6941 /* arg1 should be the first argument of the new T. */
6942 arg1 = TREE_OPERAND (t, 1);
6943 STRIP_NOPS (arg1);
6947 /* If we have A op B ? A : C, we may be able to convert this to a
6948 simpler expression, depending on the operation and the values
6949 of B and C. IEEE floating point prevents this though,
6950 because A or B might be -0.0 or a NaN. */
6952 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
6953 && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
6954 || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
6955 || flag_unsafe_math_optimizations)
6956 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
6957 arg1, TREE_OPERAND (arg0, 1)))
6959 tree arg2 = TREE_OPERAND (t, 2);
6960 enum tree_code comp_code = TREE_CODE (arg0);
6962 STRIP_NOPS (arg2);
6964 /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
6965 depending on the comparison operation. */
6966 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
6967 ? real_zerop (TREE_OPERAND (arg0, 1))
6968 : integer_zerop (TREE_OPERAND (arg0, 1)))
6969 && TREE_CODE (arg2) == NEGATE_EXPR
6970 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6971 switch (comp_code)
6973 case EQ_EXPR:
6974 return
6975 pedantic_non_lvalue
6976 (convert (type,
6977 negate_expr
6978 (convert (TREE_TYPE (TREE_OPERAND (t, 1)),
6979 arg1))));
6981 case NE_EXPR:
6982 return pedantic_non_lvalue (convert (type, arg1));
6983 case GE_EXPR:
6984 case GT_EXPR:
6985 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
6986 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
6987 return pedantic_non_lvalue
6988 (convert (type, fold (build1 (ABS_EXPR,
6989 TREE_TYPE (arg1), arg1))));
6990 case LE_EXPR:
6991 case LT_EXPR:
6992 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
6993 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
6994 return pedantic_non_lvalue
6995 (negate_expr (convert (type,
6996 fold (build1 (ABS_EXPR,
6997 TREE_TYPE (arg1),
6998 arg1)))));
6999 default:
7000 abort ();
7003 /* If this is A != 0 ? A : 0, this is simply A. For ==, it is
7004 always zero. */
7006 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
7008 if (comp_code == NE_EXPR)
7009 return pedantic_non_lvalue (convert (type, arg1));
7010 else if (comp_code == EQ_EXPR)
7011 return pedantic_non_lvalue (convert (type, integer_zero_node));
7014 /* If this is A op B ? A : B, this is either A, B, min (A, B),
7015 or max (A, B), depending on the operation. */
7017 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
7018 arg2, TREE_OPERAND (arg0, 0)))
7020 tree comp_op0 = TREE_OPERAND (arg0, 0);
7021 tree comp_op1 = TREE_OPERAND (arg0, 1);
7022 tree comp_type = TREE_TYPE (comp_op0);
7024 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
7025 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
7026 comp_type = type;
7028 switch (comp_code)
7030 case EQ_EXPR:
7031 return pedantic_non_lvalue (convert (type, arg2));
7032 case NE_EXPR:
7033 return pedantic_non_lvalue (convert (type, arg1));
7034 case LE_EXPR:
7035 case LT_EXPR:
7036 /* In C++ a ?: expression can be an lvalue, so put the
7037 operand which will be used if they are equal first
7038 so that we can convert this back to the
7039 corresponding COND_EXPR. */
7040 return pedantic_non_lvalue
7041 (convert (type, fold (build (MIN_EXPR, comp_type,
7042 (comp_code == LE_EXPR
7043 ? comp_op0 : comp_op1),
7044 (comp_code == LE_EXPR
7045 ? comp_op1 : comp_op0)))));
7046 break;
7047 case GE_EXPR:
7048 case GT_EXPR:
7049 return pedantic_non_lvalue
7050 (convert (type, fold (build (MAX_EXPR, comp_type,
7051 (comp_code == GE_EXPR
7052 ? comp_op0 : comp_op1),
7053 (comp_code == GE_EXPR
7054 ? comp_op1 : comp_op0)))));
7055 break;
7056 default:
7057 abort ();
7061 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
7062 we might still be able to simplify this. For example,
7063 if C1 is one less or one more than C2, this might have started
7064 out as a MIN or MAX and been transformed by this function.
7065 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
7067 if (INTEGRAL_TYPE_P (type)
7068 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7069 && TREE_CODE (arg2) == INTEGER_CST)
7070 switch (comp_code)
7072 case EQ_EXPR:
7073 /* We can replace A with C1 in this case. */
7074 arg1 = convert (type, TREE_OPERAND (arg0, 1));
7075 t = build (code, type, TREE_OPERAND (t, 0), arg1,
7076 TREE_OPERAND (t, 2));
7077 break;
7079 case LT_EXPR:
7080 /* If C1 is C2 + 1, this is min(A, C2). */
7081 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
7082 && operand_equal_p (TREE_OPERAND (arg0, 1),
7083 const_binop (PLUS_EXPR, arg2,
7084 integer_one_node, 0), 1))
7085 return pedantic_non_lvalue
7086 (fold (build (MIN_EXPR, type, arg1, arg2)));
7087 break;
7089 case LE_EXPR:
7090 /* If C1 is C2 - 1, this is min(A, C2). */
7091 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
7092 && operand_equal_p (TREE_OPERAND (arg0, 1),
7093 const_binop (MINUS_EXPR, arg2,
7094 integer_one_node, 0), 1))
7095 return pedantic_non_lvalue
7096 (fold (build (MIN_EXPR, type, arg1, arg2)));
7097 break;
7099 case GT_EXPR:
7100 /* If C1 is C2 - 1, this is max(A, C2). */
7101 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
7102 && operand_equal_p (TREE_OPERAND (arg0, 1),
7103 const_binop (MINUS_EXPR, arg2,
7104 integer_one_node, 0), 1))
7105 return pedantic_non_lvalue
7106 (fold (build (MAX_EXPR, type, arg1, arg2)));
7107 break;
7109 case GE_EXPR:
7110 /* If C1 is C2 + 1, this is max(A, C2). */
7111 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
7112 && operand_equal_p (TREE_OPERAND (arg0, 1),
7113 const_binop (PLUS_EXPR, arg2,
7114 integer_one_node, 0), 1))
7115 return pedantic_non_lvalue
7116 (fold (build (MAX_EXPR, type, arg1, arg2)));
7117 break;
7118 case NE_EXPR:
7119 break;
7120 default:
7121 abort ();
7125 /* If the second operand is simpler than the third, swap them
7126 since that produces better jump optimization results. */
7127 if ((TREE_CONSTANT (arg1) || DECL_P (arg1)
7128 || TREE_CODE (arg1) == SAVE_EXPR)
7129 && ! (TREE_CONSTANT (TREE_OPERAND (t, 2))
7130 || DECL_P (TREE_OPERAND (t, 2))
7131 || TREE_CODE (TREE_OPERAND (t, 2)) == SAVE_EXPR))
7133 /* See if this can be inverted. If it can't, possibly because
7134 it was a floating-point inequality comparison, don't do
7135 anything. */
7136 tem = invert_truthvalue (arg0);
7138 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
7140 t = build (code, type, tem,
7141 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
7142 arg0 = tem;
7143 /* arg1 should be the first argument of the new T. */
7144 arg1 = TREE_OPERAND (t, 1);
7145 STRIP_NOPS (arg1);
7149 /* Convert A ? 1 : 0 to simply A. */
7150 if (integer_onep (TREE_OPERAND (t, 1))
7151 && integer_zerop (TREE_OPERAND (t, 2))
7152 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
7153 call to fold will try to move the conversion inside
7154 a COND, which will recurse. In that case, the COND_EXPR
7155 is probably the best choice, so leave it alone. */
7156 && type == TREE_TYPE (arg0))
7157 return pedantic_non_lvalue (arg0);
7159 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
7160 operation is simply A & 2. */
7162 if (integer_zerop (TREE_OPERAND (t, 2))
7163 && TREE_CODE (arg0) == NE_EXPR
7164 && integer_zerop (TREE_OPERAND (arg0, 1))
7165 && integer_pow2p (arg1)
7166 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
7167 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
7168 arg1, 1))
7169 return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
7171 return t;
7173 case COMPOUND_EXPR:
7174 /* When pedantic, a compound expression can be neither an lvalue
7175 nor an integer constant expression. */
7176 if (TREE_SIDE_EFFECTS (arg0) || pedantic)
7177 return t;
7178 /* Don't let (0, 0) be null pointer constant. */
7179 if (integer_zerop (arg1))
7180 return build1 (NOP_EXPR, type, arg1);
7181 return convert (type, arg1);
7183 case COMPLEX_EXPR:
7184 if (wins)
7185 return build_complex (type, arg0, arg1);
7186 return t;
7188 case REALPART_EXPR:
7189 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7190 return t;
7191 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7192 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
7193 TREE_OPERAND (arg0, 1));
7194 else if (TREE_CODE (arg0) == COMPLEX_CST)
7195 return TREE_REALPART (arg0);
7196 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7197 return fold (build (TREE_CODE (arg0), type,
7198 fold (build1 (REALPART_EXPR, type,
7199 TREE_OPERAND (arg0, 0))),
7200 fold (build1 (REALPART_EXPR,
7201 type, TREE_OPERAND (arg0, 1)))));
7202 return t;
7204 case IMAGPART_EXPR:
7205 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7206 return convert (type, integer_zero_node);
7207 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7208 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
7209 TREE_OPERAND (arg0, 0));
7210 else if (TREE_CODE (arg0) == COMPLEX_CST)
7211 return TREE_IMAGPART (arg0);
7212 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7213 return fold (build (TREE_CODE (arg0), type,
7214 fold (build1 (IMAGPART_EXPR, type,
7215 TREE_OPERAND (arg0, 0))),
7216 fold (build1 (IMAGPART_EXPR, type,
7217 TREE_OPERAND (arg0, 1)))));
7218 return t;
7220 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
7221 appropriate. */
7222 case CLEANUP_POINT_EXPR:
7223 if (! has_cleanups (arg0))
7224 return TREE_OPERAND (t, 0);
7227 enum tree_code code0 = TREE_CODE (arg0);
7228 int kind0 = TREE_CODE_CLASS (code0);
7229 tree arg00 = TREE_OPERAND (arg0, 0);
7230 tree arg01;
7232 if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
7233 return fold (build1 (code0, type,
7234 fold (build1 (CLEANUP_POINT_EXPR,
7235 TREE_TYPE (arg00), arg00))));
7237 if (kind0 == '<' || kind0 == '2'
7238 || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
7239 || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR
7240 || code0 == TRUTH_XOR_EXPR)
7242 arg01 = TREE_OPERAND (arg0, 1);
7244 if (TREE_CONSTANT (arg00)
7245 || ((code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR)
7246 && ! has_cleanups (arg00)))
7247 return fold (build (code0, type, arg00,
7248 fold (build1 (CLEANUP_POINT_EXPR,
7249 TREE_TYPE (arg01), arg01))));
7251 if (TREE_CONSTANT (arg01))
7252 return fold (build (code0, type,
7253 fold (build1 (CLEANUP_POINT_EXPR,
7254 TREE_TYPE (arg00), arg00)),
7255 arg01));
7258 return t;
7261 case CALL_EXPR:
7262 /* Check for a built-in function. */
7263 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR
7264 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (expr, 0), 0))
7265 == FUNCTION_DECL)
7266 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (expr, 0), 0)))
7268 tree tmp = fold_builtin (expr);
7269 if (tmp)
7270 return tmp;
7272 return t;
7274 default:
7275 return t;
7276 } /* switch (code) */
7279 /* Determine if first argument is a multiple of second argument. Return 0 if
7280 it is not, or we cannot easily determined it to be.
7282 An example of the sort of thing we care about (at this point; this routine
7283 could surely be made more general, and expanded to do what the *_DIV_EXPR's
7284 fold cases do now) is discovering that
7286 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7288 is a multiple of
7290 SAVE_EXPR (J * 8)
7292 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
7294 This code also handles discovering that
7296 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7298 is a multiple of 8 so we don't have to worry about dealing with a
7299 possible remainder.
7301 Note that we *look* inside a SAVE_EXPR only to determine how it was
7302 calculated; it is not safe for fold to do much of anything else with the
7303 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
7304 at run time. For example, the latter example above *cannot* be implemented
7305 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
7306 evaluation time of the original SAVE_EXPR is not necessarily the same at
7307 the time the new expression is evaluated. The only optimization of this
7308 sort that would be valid is changing
7310 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
7312 divided by 8 to
7314 SAVE_EXPR (I) * SAVE_EXPR (J)
7316 (where the same SAVE_EXPR (J) is used in the original and the
7317 transformed version). */
7319 static int
7320 multiple_of_p (type, top, bottom)
7321 tree type;
7322 tree top;
7323 tree bottom;
7325 if (operand_equal_p (top, bottom, 0))
7326 return 1;
7328 if (TREE_CODE (type) != INTEGER_TYPE)
7329 return 0;
7331 switch (TREE_CODE (top))
7333 case MULT_EXPR:
7334 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7335 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7337 case PLUS_EXPR:
7338 case MINUS_EXPR:
7339 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7340 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7342 case LSHIFT_EXPR:
7343 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
7345 tree op1, t1;
7347 op1 = TREE_OPERAND (top, 1);
7348 /* const_binop may not detect overflow correctly,
7349 so check for it explicitly here. */
7350 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
7351 > TREE_INT_CST_LOW (op1)
7352 && TREE_INT_CST_HIGH (op1) == 0
7353 && 0 != (t1 = convert (type,
7354 const_binop (LSHIFT_EXPR, size_one_node,
7355 op1, 0)))
7356 && ! TREE_OVERFLOW (t1))
7357 return multiple_of_p (type, t1, bottom);
7359 return 0;
7361 case NOP_EXPR:
7362 /* Can't handle conversions from non-integral or wider integral type. */
7363 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
7364 || (TYPE_PRECISION (type)
7365 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
7366 return 0;
7368 /* .. fall through ... */
7370 case SAVE_EXPR:
7371 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
7373 case INTEGER_CST:
7374 if (TREE_CODE (bottom) != INTEGER_CST
7375 || (TREE_UNSIGNED (type)
7376 && (tree_int_cst_sgn (top) < 0
7377 || tree_int_cst_sgn (bottom) < 0)))
7378 return 0;
7379 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
7380 top, bottom, 0));
7382 default:
7383 return 0;
7387 /* Return true if `t' is known to be non-negative. */
7390 tree_expr_nonnegative_p (t)
7391 tree t;
7393 switch (TREE_CODE (t))
7395 case ABS_EXPR:
7396 case FFS_EXPR:
7397 return 1;
7398 case INTEGER_CST:
7399 return tree_int_cst_sgn (t) >= 0;
7400 case TRUNC_DIV_EXPR:
7401 case CEIL_DIV_EXPR:
7402 case FLOOR_DIV_EXPR:
7403 case ROUND_DIV_EXPR:
7404 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
7405 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7406 case TRUNC_MOD_EXPR:
7407 case CEIL_MOD_EXPR:
7408 case FLOOR_MOD_EXPR:
7409 case ROUND_MOD_EXPR:
7410 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
7411 case COND_EXPR:
7412 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
7413 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
7414 case COMPOUND_EXPR:
7415 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7416 case MIN_EXPR:
7417 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
7418 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7419 case MAX_EXPR:
7420 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
7421 || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7422 case MODIFY_EXPR:
7423 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7424 case BIND_EXPR:
7425 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7426 case SAVE_EXPR:
7427 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
7428 case NON_LVALUE_EXPR:
7429 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
7430 case RTL_EXPR:
7431 return rtl_expr_nonnegative_p (RTL_EXPR_RTL (t));
7433 default:
7434 if (truth_value_p (TREE_CODE (t)))
7435 /* Truth values evaluate to 0 or 1, which is nonnegative. */
7436 return 1;
7437 else
7438 /* We don't know sign of `t', so be conservative and return false. */
7439 return 0;
7443 /* Return true if `r' is known to be non-negative.
7444 Only handles constants at the moment. */
7447 rtl_expr_nonnegative_p (r)
7448 rtx r;
7450 switch (GET_CODE (r))
7452 case CONST_INT:
7453 return INTVAL (r) >= 0;
7455 case CONST_DOUBLE:
7456 if (GET_MODE (r) == VOIDmode)
7457 return CONST_DOUBLE_HIGH (r) >= 0;
7458 return 0;
7460 case SYMBOL_REF:
7461 case LABEL_REF:
7462 /* These are always nonnegative. */
7463 return 1;
7465 default:
7466 return 0;