Daily bump.
[official-gcc.git] / gcc / fold-const.c
bloba7e1e284086996e8aa814805c1daf3eaf8074869
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, 2002 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"
54 #include "hashtab.h"
56 static void encode PARAMS ((HOST_WIDE_INT *,
57 unsigned HOST_WIDE_INT,
58 HOST_WIDE_INT));
59 static void decode PARAMS ((HOST_WIDE_INT *,
60 unsigned HOST_WIDE_INT *,
61 HOST_WIDE_INT *));
62 #ifndef REAL_ARITHMETIC
63 static void exact_real_inverse_1 PARAMS ((PTR));
64 #endif
65 static tree negate_expr PARAMS ((tree));
66 static tree split_tree PARAMS ((tree, enum tree_code, tree *, tree *,
67 tree *, int));
68 static tree associate_trees PARAMS ((tree, tree, enum tree_code, tree));
69 static tree int_const_binop PARAMS ((enum tree_code, tree, tree, int));
70 static void const_binop_1 PARAMS ((PTR));
71 static tree const_binop PARAMS ((enum tree_code, tree, tree, int));
72 static hashval_t size_htab_hash PARAMS ((const void *));
73 static int size_htab_eq PARAMS ((const void *, const void *));
74 static void fold_convert_1 PARAMS ((PTR));
75 static tree fold_convert PARAMS ((tree, tree));
76 static enum tree_code invert_tree_comparison PARAMS ((enum tree_code));
77 static enum tree_code swap_tree_comparison PARAMS ((enum tree_code));
78 static int truth_value_p PARAMS ((enum tree_code));
79 static int operand_equal_for_comparison_p PARAMS ((tree, tree, tree));
80 static int twoval_comparison_p PARAMS ((tree, tree *, tree *, int *));
81 static tree eval_subst PARAMS ((tree, tree, tree, tree, tree));
82 static tree omit_one_operand PARAMS ((tree, tree, tree));
83 static tree pedantic_omit_one_operand PARAMS ((tree, tree, tree));
84 static tree distribute_bit_expr PARAMS ((enum tree_code, tree, tree, tree));
85 static tree make_bit_field_ref PARAMS ((tree, tree, int, int, int));
86 static tree optimize_bit_field_compare PARAMS ((enum tree_code, tree,
87 tree, tree));
88 static tree decode_field_reference PARAMS ((tree, HOST_WIDE_INT *,
89 HOST_WIDE_INT *,
90 enum machine_mode *, int *,
91 int *, tree *, tree *));
92 static int all_ones_mask_p PARAMS ((tree, int));
93 static int simple_operand_p PARAMS ((tree));
94 static tree range_binop PARAMS ((enum tree_code, tree, tree, int,
95 tree, int));
96 static tree make_range PARAMS ((tree, int *, tree *, tree *));
97 static tree build_range_check PARAMS ((tree, tree, int, tree, tree));
98 static int merge_ranges PARAMS ((int *, tree *, tree *, int, tree, tree,
99 int, tree, tree));
100 static tree fold_range_test PARAMS ((tree));
101 static tree unextend PARAMS ((tree, int, int, tree));
102 static tree fold_truthop PARAMS ((enum tree_code, tree, tree, tree));
103 static tree optimize_minmax_comparison PARAMS ((tree));
104 static tree extract_muldiv PARAMS ((tree, tree, enum tree_code, tree));
105 static tree strip_compound_expr PARAMS ((tree, tree));
106 static int multiple_of_p PARAMS ((tree, tree, tree));
107 static tree constant_boolean_node PARAMS ((int, tree));
108 static int count_cond PARAMS ((tree, int));
109 static tree fold_binary_op_with_conditional_arg
110 PARAMS ((enum tree_code, tree, tree, tree, int));
112 #if defined(HOST_EBCDIC)
113 /* bit 8 is significant in EBCDIC */
114 #define CHARMASK 0xff
115 #else
116 #define CHARMASK 0x7f
117 #endif
119 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
120 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
121 and SUM1. Then this yields nonzero if overflow occurred during the
122 addition.
124 Overflow occurs if A and B have the same sign, but A and SUM differ in
125 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
126 sign. */
127 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
129 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
130 We do that by representing the two-word integer in 4 words, with only
131 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
132 number. The value of the word is LOWPART + HIGHPART * BASE. */
134 #define LOWPART(x) \
135 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
136 #define HIGHPART(x) \
137 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
138 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
140 /* Unpack a two-word integer into 4 words.
141 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
142 WORDS points to the array of HOST_WIDE_INTs. */
144 static void
145 encode (words, low, hi)
146 HOST_WIDE_INT *words;
147 unsigned HOST_WIDE_INT low;
148 HOST_WIDE_INT hi;
150 words[0] = LOWPART (low);
151 words[1] = HIGHPART (low);
152 words[2] = LOWPART (hi);
153 words[3] = HIGHPART (hi);
156 /* Pack an array of 4 words into a two-word integer.
157 WORDS points to the array of words.
158 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
160 static void
161 decode (words, low, hi)
162 HOST_WIDE_INT *words;
163 unsigned HOST_WIDE_INT *low;
164 HOST_WIDE_INT *hi;
166 *low = words[0] + words[1] * BASE;
167 *hi = words[2] + words[3] * BASE;
170 /* Make the integer constant T valid for its type by setting to 0 or 1 all
171 the bits in the constant that don't belong in the type.
173 Return 1 if a signed overflow occurs, 0 otherwise. If OVERFLOW is
174 nonzero, a signed overflow has already occurred in calculating T, so
175 propagate it.
177 Make the real constant T valid for its type by calling CHECK_FLOAT_VALUE,
178 if it exists. */
181 force_fit_type (t, overflow)
182 tree t;
183 int overflow;
185 unsigned HOST_WIDE_INT low;
186 HOST_WIDE_INT high;
187 unsigned int prec;
189 if (TREE_CODE (t) == REAL_CST)
191 #ifdef CHECK_FLOAT_VALUE
192 CHECK_FLOAT_VALUE (TYPE_MODE (TREE_TYPE (t)), TREE_REAL_CST (t),
193 overflow);
194 #endif
195 return overflow;
198 else if (TREE_CODE (t) != INTEGER_CST)
199 return overflow;
201 low = TREE_INT_CST_LOW (t);
202 high = TREE_INT_CST_HIGH (t);
204 if (POINTER_TYPE_P (TREE_TYPE (t)))
205 prec = POINTER_SIZE;
206 else
207 prec = TYPE_PRECISION (TREE_TYPE (t));
209 /* First clear all bits that are beyond the type's precision. */
211 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
213 else if (prec > HOST_BITS_PER_WIDE_INT)
214 TREE_INT_CST_HIGH (t)
215 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
216 else
218 TREE_INT_CST_HIGH (t) = 0;
219 if (prec < HOST_BITS_PER_WIDE_INT)
220 TREE_INT_CST_LOW (t) &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
223 /* Unsigned types do not suffer sign extension or overflow unless they
224 are a sizetype. */
225 if (TREE_UNSIGNED (TREE_TYPE (t))
226 && ! (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
227 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
228 return overflow;
230 /* If the value's sign bit is set, extend the sign. */
231 if (prec != 2 * HOST_BITS_PER_WIDE_INT
232 && (prec > HOST_BITS_PER_WIDE_INT
233 ? 0 != (TREE_INT_CST_HIGH (t)
234 & ((HOST_WIDE_INT) 1
235 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
236 : 0 != (TREE_INT_CST_LOW (t)
237 & ((unsigned HOST_WIDE_INT) 1 << (prec - 1)))))
239 /* Value is negative:
240 set to 1 all the bits that are outside this type's precision. */
241 if (prec > HOST_BITS_PER_WIDE_INT)
242 TREE_INT_CST_HIGH (t)
243 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
244 else
246 TREE_INT_CST_HIGH (t) = -1;
247 if (prec < HOST_BITS_PER_WIDE_INT)
248 TREE_INT_CST_LOW (t) |= ((unsigned HOST_WIDE_INT) (-1) << prec);
252 /* Return nonzero if signed overflow occurred. */
253 return
254 ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
255 != 0);
258 /* Add two doubleword integers with doubleword result.
259 Each argument is given as two `HOST_WIDE_INT' pieces.
260 One argument is L1 and H1; the other, L2 and H2.
261 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
264 add_double (l1, h1, l2, h2, lv, hv)
265 unsigned HOST_WIDE_INT l1, l2;
266 HOST_WIDE_INT h1, h2;
267 unsigned HOST_WIDE_INT *lv;
268 HOST_WIDE_INT *hv;
270 unsigned HOST_WIDE_INT l;
271 HOST_WIDE_INT h;
273 l = l1 + l2;
274 h = h1 + h2 + (l < l1);
276 *lv = l;
277 *hv = h;
278 return OVERFLOW_SUM_SIGN (h1, h2, h);
281 /* Negate a doubleword integer with doubleword result.
282 Return nonzero if the operation overflows, assuming it's signed.
283 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
284 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
287 neg_double (l1, h1, lv, hv)
288 unsigned HOST_WIDE_INT l1;
289 HOST_WIDE_INT h1;
290 unsigned HOST_WIDE_INT *lv;
291 HOST_WIDE_INT *hv;
293 if (l1 == 0)
295 *lv = 0;
296 *hv = - h1;
297 return (*hv & h1) < 0;
299 else
301 *lv = -l1;
302 *hv = ~h1;
303 return 0;
307 /* Multiply two doubleword integers with doubleword result.
308 Return nonzero if the operation overflows, assuming it's signed.
309 Each argument is given as two `HOST_WIDE_INT' pieces.
310 One argument is L1 and H1; the other, L2 and H2.
311 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
314 mul_double (l1, h1, l2, h2, lv, hv)
315 unsigned HOST_WIDE_INT l1, l2;
316 HOST_WIDE_INT h1, h2;
317 unsigned HOST_WIDE_INT *lv;
318 HOST_WIDE_INT *hv;
320 HOST_WIDE_INT arg1[4];
321 HOST_WIDE_INT arg2[4];
322 HOST_WIDE_INT prod[4 * 2];
323 unsigned HOST_WIDE_INT carry;
324 int i, j, k;
325 unsigned HOST_WIDE_INT toplow, neglow;
326 HOST_WIDE_INT tophigh, neghigh;
328 encode (arg1, l1, h1);
329 encode (arg2, l2, h2);
331 memset ((char *) prod, 0, sizeof prod);
333 for (i = 0; i < 4; i++)
335 carry = 0;
336 for (j = 0; j < 4; j++)
338 k = i + j;
339 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
340 carry += arg1[i] * arg2[j];
341 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
342 carry += prod[k];
343 prod[k] = LOWPART (carry);
344 carry = HIGHPART (carry);
346 prod[i + 4] = carry;
349 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
351 /* Check for overflow by calculating the top half of the answer in full;
352 it should agree with the low half's sign bit. */
353 decode (prod + 4, &toplow, &tophigh);
354 if (h1 < 0)
356 neg_double (l2, h2, &neglow, &neghigh);
357 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
359 if (h2 < 0)
361 neg_double (l1, h1, &neglow, &neghigh);
362 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
364 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
367 /* Shift the doubleword integer in L1, H1 left by COUNT places
368 keeping only PREC bits of result.
369 Shift right if COUNT is negative.
370 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
371 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
373 void
374 lshift_double (l1, h1, count, prec, lv, hv, arith)
375 unsigned HOST_WIDE_INT l1;
376 HOST_WIDE_INT h1, count;
377 unsigned int prec;
378 unsigned HOST_WIDE_INT *lv;
379 HOST_WIDE_INT *hv;
380 int arith;
382 unsigned HOST_WIDE_INT signmask;
384 if (count < 0)
386 rshift_double (l1, h1, -count, prec, lv, hv, arith);
387 return;
390 #ifdef SHIFT_COUNT_TRUNCATED
391 if (SHIFT_COUNT_TRUNCATED)
392 count %= prec;
393 #endif
395 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
397 /* Shifting by the host word size is undefined according to the
398 ANSI standard, so we must handle this as a special case. */
399 *hv = 0;
400 *lv = 0;
402 else if (count >= HOST_BITS_PER_WIDE_INT)
404 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
405 *lv = 0;
407 else
409 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
410 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
411 *lv = l1 << count;
414 /* Sign extend all bits that are beyond the precision. */
416 signmask = -((prec > HOST_BITS_PER_WIDE_INT
417 ? (*hv >> (prec - HOST_BITS_PER_WIDE_INT - 1))
418 : (*lv >> (prec - 1))) & 1);
420 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
422 else if (prec >= HOST_BITS_PER_WIDE_INT)
424 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
425 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
427 else
429 *hv = signmask;
430 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
431 *lv |= signmask << prec;
435 /* Shift the doubleword integer in L1, H1 right by COUNT places
436 keeping only PREC bits of result. COUNT must be positive.
437 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
438 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
440 void
441 rshift_double (l1, h1, count, prec, lv, hv, arith)
442 unsigned HOST_WIDE_INT l1;
443 HOST_WIDE_INT h1, count;
444 unsigned int prec;
445 unsigned HOST_WIDE_INT *lv;
446 HOST_WIDE_INT *hv;
447 int arith;
449 unsigned HOST_WIDE_INT signmask;
451 signmask = (arith
452 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
453 : 0);
455 #ifdef SHIFT_COUNT_TRUNCATED
456 if (SHIFT_COUNT_TRUNCATED)
457 count %= prec;
458 #endif
460 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
462 /* Shifting by the host word size is undefined according to the
463 ANSI standard, so we must handle this as a special case. */
464 *hv = 0;
465 *lv = 0;
467 else if (count >= HOST_BITS_PER_WIDE_INT)
469 *hv = 0;
470 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
472 else
474 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
475 *lv = ((l1 >> count)
476 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
479 /* Zero / sign extend all bits that are beyond the precision. */
481 if (count >= (HOST_WIDE_INT)prec)
483 *hv = signmask;
484 *lv = signmask;
486 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
488 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
490 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
491 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
493 else
495 *hv = signmask;
496 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
497 *lv |= signmask << (prec - count);
501 /* Rotate the doubleword integer in L1, H1 left by COUNT places
502 keeping only PREC bits of result.
503 Rotate right if COUNT is negative.
504 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
506 void
507 lrotate_double (l1, h1, count, prec, lv, hv)
508 unsigned HOST_WIDE_INT l1;
509 HOST_WIDE_INT h1, count;
510 unsigned int prec;
511 unsigned HOST_WIDE_INT *lv;
512 HOST_WIDE_INT *hv;
514 unsigned HOST_WIDE_INT s1l, s2l;
515 HOST_WIDE_INT s1h, s2h;
517 count %= prec;
518 if (count < 0)
519 count += prec;
521 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
522 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
523 *lv = s1l | s2l;
524 *hv = s1h | s2h;
527 /* Rotate the doubleword integer in L1, H1 left by COUNT places
528 keeping only PREC bits of result. COUNT must be positive.
529 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
531 void
532 rrotate_double (l1, h1, count, prec, lv, hv)
533 unsigned HOST_WIDE_INT l1;
534 HOST_WIDE_INT h1, count;
535 unsigned int prec;
536 unsigned HOST_WIDE_INT *lv;
537 HOST_WIDE_INT *hv;
539 unsigned HOST_WIDE_INT s1l, s2l;
540 HOST_WIDE_INT s1h, s2h;
542 count %= prec;
543 if (count < 0)
544 count += prec;
546 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
547 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
548 *lv = s1l | s2l;
549 *hv = s1h | s2h;
552 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
553 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
554 CODE is a tree code for a kind of division, one of
555 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
556 or EXACT_DIV_EXPR
557 It controls how the quotient is rounded to an integer.
558 Return nonzero if the operation overflows.
559 UNS nonzero says do unsigned division. */
562 div_and_round_double (code, uns,
563 lnum_orig, hnum_orig, lden_orig, hden_orig,
564 lquo, hquo, lrem, hrem)
565 enum tree_code code;
566 int uns;
567 unsigned HOST_WIDE_INT lnum_orig; /* num == numerator == dividend */
568 HOST_WIDE_INT hnum_orig;
569 unsigned HOST_WIDE_INT lden_orig; /* den == denominator == divisor */
570 HOST_WIDE_INT hden_orig;
571 unsigned HOST_WIDE_INT *lquo, *lrem;
572 HOST_WIDE_INT *hquo, *hrem;
574 int quo_neg = 0;
575 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
576 HOST_WIDE_INT den[4], quo[4];
577 int i, j;
578 unsigned HOST_WIDE_INT work;
579 unsigned HOST_WIDE_INT carry = 0;
580 unsigned HOST_WIDE_INT lnum = lnum_orig;
581 HOST_WIDE_INT hnum = hnum_orig;
582 unsigned HOST_WIDE_INT lden = lden_orig;
583 HOST_WIDE_INT hden = hden_orig;
584 int overflow = 0;
586 if (hden == 0 && lden == 0)
587 overflow = 1, lden = 1;
589 /* calculate quotient sign and convert operands to unsigned. */
590 if (!uns)
592 if (hnum < 0)
594 quo_neg = ~ quo_neg;
595 /* (minimum integer) / (-1) is the only overflow case. */
596 if (neg_double (lnum, hnum, &lnum, &hnum)
597 && ((HOST_WIDE_INT) lden & hden) == -1)
598 overflow = 1;
600 if (hden < 0)
602 quo_neg = ~ quo_neg;
603 neg_double (lden, hden, &lden, &hden);
607 if (hnum == 0 && hden == 0)
608 { /* single precision */
609 *hquo = *hrem = 0;
610 /* This unsigned division rounds toward zero. */
611 *lquo = lnum / lden;
612 goto finish_up;
615 if (hnum == 0)
616 { /* trivial case: dividend < divisor */
617 /* hden != 0 already checked. */
618 *hquo = *lquo = 0;
619 *hrem = hnum;
620 *lrem = lnum;
621 goto finish_up;
624 memset ((char *) quo, 0, sizeof quo);
626 memset ((char *) num, 0, sizeof num); /* to zero 9th element */
627 memset ((char *) den, 0, sizeof den);
629 encode (num, lnum, hnum);
630 encode (den, lden, hden);
632 /* Special code for when the divisor < BASE. */
633 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
635 /* hnum != 0 already checked. */
636 for (i = 4 - 1; i >= 0; i--)
638 work = num[i] + carry * BASE;
639 quo[i] = work / lden;
640 carry = work % lden;
643 else
645 /* Full double precision division,
646 with thanks to Don Knuth's "Seminumerical Algorithms". */
647 int num_hi_sig, den_hi_sig;
648 unsigned HOST_WIDE_INT quo_est, scale;
650 /* Find the highest non-zero divisor digit. */
651 for (i = 4 - 1;; i--)
652 if (den[i] != 0)
654 den_hi_sig = i;
655 break;
658 /* Insure that the first digit of the divisor is at least BASE/2.
659 This is required by the quotient digit estimation algorithm. */
661 scale = BASE / (den[den_hi_sig] + 1);
662 if (scale > 1)
663 { /* scale divisor and dividend */
664 carry = 0;
665 for (i = 0; i <= 4 - 1; i++)
667 work = (num[i] * scale) + carry;
668 num[i] = LOWPART (work);
669 carry = HIGHPART (work);
672 num[4] = carry;
673 carry = 0;
674 for (i = 0; i <= 4 - 1; i++)
676 work = (den[i] * scale) + carry;
677 den[i] = LOWPART (work);
678 carry = HIGHPART (work);
679 if (den[i] != 0) den_hi_sig = i;
683 num_hi_sig = 4;
685 /* Main loop */
686 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
688 /* Guess the next quotient digit, quo_est, by dividing the first
689 two remaining dividend digits by the high order quotient digit.
690 quo_est is never low and is at most 2 high. */
691 unsigned HOST_WIDE_INT tmp;
693 num_hi_sig = i + den_hi_sig + 1;
694 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
695 if (num[num_hi_sig] != den[den_hi_sig])
696 quo_est = work / den[den_hi_sig];
697 else
698 quo_est = BASE - 1;
700 /* Refine quo_est so it's usually correct, and at most one high. */
701 tmp = work - quo_est * den[den_hi_sig];
702 if (tmp < BASE
703 && (den[den_hi_sig - 1] * quo_est
704 > (tmp * BASE + num[num_hi_sig - 2])))
705 quo_est--;
707 /* Try QUO_EST as the quotient digit, by multiplying the
708 divisor by QUO_EST and subtracting from the remaining dividend.
709 Keep in mind that QUO_EST is the I - 1st digit. */
711 carry = 0;
712 for (j = 0; j <= den_hi_sig; j++)
714 work = quo_est * den[j] + carry;
715 carry = HIGHPART (work);
716 work = num[i + j] - LOWPART (work);
717 num[i + j] = LOWPART (work);
718 carry += HIGHPART (work) != 0;
721 /* If quo_est was high by one, then num[i] went negative and
722 we need to correct things. */
723 if (num[num_hi_sig] < carry)
725 quo_est--;
726 carry = 0; /* add divisor back in */
727 for (j = 0; j <= den_hi_sig; j++)
729 work = num[i + j] + den[j] + carry;
730 carry = HIGHPART (work);
731 num[i + j] = LOWPART (work);
734 num [num_hi_sig] += carry;
737 /* Store the quotient digit. */
738 quo[i] = quo_est;
742 decode (quo, lquo, hquo);
744 finish_up:
745 /* if result is negative, make it so. */
746 if (quo_neg)
747 neg_double (*lquo, *hquo, lquo, hquo);
749 /* compute trial remainder: rem = num - (quo * den) */
750 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
751 neg_double (*lrem, *hrem, lrem, hrem);
752 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
754 switch (code)
756 case TRUNC_DIV_EXPR:
757 case TRUNC_MOD_EXPR: /* round toward zero */
758 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
759 return overflow;
761 case FLOOR_DIV_EXPR:
762 case FLOOR_MOD_EXPR: /* round toward negative infinity */
763 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
765 /* quo = quo - 1; */
766 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
767 lquo, hquo);
769 else
770 return overflow;
771 break;
773 case CEIL_DIV_EXPR:
774 case CEIL_MOD_EXPR: /* round toward positive infinity */
775 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
777 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
778 lquo, hquo);
780 else
781 return overflow;
782 break;
784 case ROUND_DIV_EXPR:
785 case ROUND_MOD_EXPR: /* round to closest integer */
787 unsigned HOST_WIDE_INT labs_rem = *lrem;
788 HOST_WIDE_INT habs_rem = *hrem;
789 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
790 HOST_WIDE_INT habs_den = hden, htwice;
792 /* Get absolute values */
793 if (*hrem < 0)
794 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
795 if (hden < 0)
796 neg_double (lden, hden, &labs_den, &habs_den);
798 /* If (2 * abs (lrem) >= abs (lden)) */
799 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
800 labs_rem, habs_rem, &ltwice, &htwice);
802 if (((unsigned HOST_WIDE_INT) habs_den
803 < (unsigned HOST_WIDE_INT) htwice)
804 || (((unsigned HOST_WIDE_INT) habs_den
805 == (unsigned HOST_WIDE_INT) htwice)
806 && (labs_den < ltwice)))
808 if (*hquo < 0)
809 /* quo = quo - 1; */
810 add_double (*lquo, *hquo,
811 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
812 else
813 /* quo = quo + 1; */
814 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
815 lquo, hquo);
817 else
818 return overflow;
820 break;
822 default:
823 abort ();
826 /* compute true remainder: rem = num - (quo * den) */
827 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
828 neg_double (*lrem, *hrem, lrem, hrem);
829 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
830 return overflow;
833 #ifndef REAL_ARITHMETIC
834 /* Effectively truncate a real value to represent the nearest possible value
835 in a narrower mode. The result is actually represented in the same data
836 type as the argument, but its value is usually different.
838 A trap may occur during the FP operations and it is the responsibility
839 of the calling function to have a handler established. */
841 REAL_VALUE_TYPE
842 real_value_truncate (mode, arg)
843 enum machine_mode mode;
844 REAL_VALUE_TYPE arg;
846 return REAL_VALUE_TRUNCATE (mode, arg);
849 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
851 /* Check for infinity in an IEEE double precision number. */
854 target_isinf (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 whether an IEEE double precision number is a NaN. */
894 target_isnan (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.exponent == 2047
919 && (u.big_endian.mantissa1 != 0
920 || u.big_endian.mantissa2 != 0));
922 else
924 u.d = x;
925 return (u.little_endian.exponent == 2047
926 && (u.little_endian.mantissa1 != 0
927 || u.little_endian.mantissa2 != 0));
931 /* Check for a negative IEEE double precision number. */
934 target_negative (x)
935 REAL_VALUE_TYPE x;
937 /* The IEEE 64-bit double format. */
938 union {
939 REAL_VALUE_TYPE d;
940 struct {
941 unsigned sign : 1;
942 unsigned exponent : 11;
943 unsigned mantissa1 : 20;
944 unsigned mantissa2 : 32;
945 } little_endian;
946 struct {
947 unsigned mantissa2 : 32;
948 unsigned mantissa1 : 20;
949 unsigned exponent : 11;
950 unsigned sign : 1;
951 } big_endian;
952 } u;
954 u.d = dconstm1;
955 if (u.big_endian.sign == 1)
957 u.d = x;
958 return u.big_endian.sign;
960 else
962 u.d = x;
963 return u.little_endian.sign;
966 #else /* Target not IEEE */
968 /* Let's assume other float formats don't have infinity.
969 (This can be overridden by redefining REAL_VALUE_ISINF.) */
972 target_isinf (x)
973 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
975 return 0;
978 /* Let's assume other float formats don't have NaNs.
979 (This can be overridden by redefining REAL_VALUE_ISNAN.) */
982 target_isnan (x)
983 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
985 return 0;
988 /* Let's assume other float formats don't have minus zero.
989 (This can be overridden by redefining REAL_VALUE_NEGATIVE.) */
992 target_negative (x)
993 REAL_VALUE_TYPE x;
995 return x < 0;
997 #endif /* Target not IEEE */
999 /* Try to change R into its exact multiplicative inverse in machine mode
1000 MODE. Return nonzero function value if successful. */
1001 struct exact_real_inverse_args
1003 REAL_VALUE_TYPE *r;
1004 enum machine_mode mode;
1005 int success;
1008 static void
1009 exact_real_inverse_1 (p)
1010 PTR p;
1012 struct exact_real_inverse_args *args =
1013 (struct exact_real_inverse_args *) p;
1015 enum machine_mode mode = args->mode;
1016 REAL_VALUE_TYPE *r = args->r;
1018 union
1020 double d;
1021 unsigned short i[4];
1023 x, t, y;
1024 #ifdef CHECK_FLOAT_VALUE
1025 int i;
1026 #endif
1028 /* Set array index to the less significant bits in the unions, depending
1029 on the endian-ness of the host doubles. */
1030 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT \
1031 || HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
1032 # define K 2
1033 #else
1034 # define K (2 * HOST_FLOAT_WORDS_BIG_ENDIAN)
1035 #endif
1037 /* Domain check the argument. */
1038 x.d = *r;
1039 if (x.d == 0.0)
1040 goto fail;
1042 #ifdef REAL_INFINITY
1043 if (REAL_VALUE_ISINF (x.d) || REAL_VALUE_ISNAN (x.d))
1044 goto fail;
1045 #endif
1047 /* Compute the reciprocal and check for numerical exactness.
1048 It is unnecessary to check all the significand bits to determine
1049 whether X is a power of 2. If X is not, then it is impossible for
1050 the bottom half significand of both X and 1/X to be all zero bits.
1051 Hence we ignore the data structure of the top half and examine only
1052 the low order bits of the two significands. */
1053 t.d = 1.0 / x.d;
1054 if (x.i[K] != 0 || x.i[K + 1] != 0 || t.i[K] != 0 || t.i[K + 1] != 0)
1055 goto fail;
1057 /* Truncate to the required mode and range-check the result. */
1058 y.d = REAL_VALUE_TRUNCATE (mode, t.d);
1059 #ifdef CHECK_FLOAT_VALUE
1060 i = 0;
1061 if (CHECK_FLOAT_VALUE (mode, y.d, i))
1062 goto fail;
1063 #endif
1065 /* Fail if truncation changed the value. */
1066 if (y.d != t.d || y.d == 0.0)
1067 goto fail;
1069 #ifdef REAL_INFINITY
1070 if (REAL_VALUE_ISINF (y.d) || REAL_VALUE_ISNAN (y.d))
1071 goto fail;
1072 #endif
1074 /* Output the reciprocal and return success flag. */
1075 *r = y.d;
1076 args->success = 1;
1077 return;
1079 fail:
1080 args->success = 0;
1081 return;
1083 #undef K
1088 exact_real_inverse (mode, r)
1089 enum machine_mode mode;
1090 REAL_VALUE_TYPE *r;
1092 struct exact_real_inverse_args args;
1094 /* Disable if insufficient information on the data structure. */
1095 #if HOST_FLOAT_FORMAT == UNKNOWN_FLOAT_FORMAT
1096 return 0;
1097 #endif
1099 /* Usually disable if bounds checks are not reliable. */
1100 if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT) && !flag_pretend_float)
1101 return 0;
1103 args.mode = mode;
1104 args.r = r;
1106 if (do_float_handler (exact_real_inverse_1, (PTR) &args))
1107 return args.success;
1108 return 0;
1111 /* Convert C99 hexadecimal floating point string constant S. Return
1112 real value type in mode MODE. This function uses the host computer's
1113 floating point arithmetic when there is no REAL_ARITHMETIC. */
1115 REAL_VALUE_TYPE
1116 real_hex_to_f (s, mode)
1117 const char *s;
1118 enum machine_mode mode;
1120 REAL_VALUE_TYPE ip;
1121 const char *p = s;
1122 unsigned HOST_WIDE_INT low, high;
1123 int shcount, nrmcount, k;
1124 int sign, expsign, isfloat;
1125 int lost = 0;/* Nonzero low order bits shifted out and discarded. */
1126 int frexpon = 0; /* Bits after the decimal point. */
1127 int expon = 0; /* Value of exponent. */
1128 int decpt = 0; /* How many decimal points. */
1129 int gotp = 0; /* How many P's. */
1130 char c;
1132 isfloat = 0;
1133 expsign = 1;
1134 ip = 0.0;
1136 while (*p == ' ' || *p == '\t')
1137 ++p;
1139 /* Sign, if any, comes first. */
1140 sign = 1;
1141 if (*p == '-')
1143 sign = -1;
1144 ++p;
1147 /* The string is supposed to start with 0x or 0X . */
1148 if (*p == '0')
1150 ++p;
1151 if (*p == 'x' || *p == 'X')
1152 ++p;
1153 else
1154 abort ();
1156 else
1157 abort ();
1159 while (*p == '0')
1160 ++p;
1162 high = 0;
1163 low = 0;
1164 shcount = 0;
1165 while ((c = *p) != '\0')
1167 if (ISXDIGIT (c))
1169 k = hex_value (c & CHARMASK);
1171 if ((high & 0xf0000000) == 0)
1173 high = (high << 4) + ((low >> 28) & 15);
1174 low = (low << 4) + k;
1175 shcount += 4;
1176 if (decpt)
1177 frexpon += 4;
1179 else
1181 /* Record nonzero lost bits. */
1182 lost |= k;
1183 if (! decpt)
1184 frexpon -= 4;
1186 ++p;
1188 else if (c == '.')
1190 ++decpt;
1191 ++p;
1194 else if (c == 'p' || c == 'P')
1196 ++gotp;
1197 ++p;
1198 /* Sign of exponent. */
1199 if (*p == '-')
1201 expsign = -1;
1202 ++p;
1205 /* Value of exponent.
1206 The exponent field is a decimal integer. */
1207 while (ISDIGIT (*p))
1209 k = (*p++ & CHARMASK) - '0';
1210 expon = 10 * expon + k;
1213 expon *= expsign;
1214 /* F suffix is ambiguous in the significand part
1215 so it must appear after the decimal exponent field. */
1216 if (*p == 'f' || *p == 'F')
1218 isfloat = 1;
1219 ++p;
1220 break;
1224 else if (c == 'l' || c == 'L')
1226 ++p;
1227 break;
1229 else
1230 break;
1233 /* Abort if last character read was not legitimate. */
1234 c = *p;
1235 if ((c != '\0' && c != ' ' && c != '\n' && c != '\r') || (decpt > 1))
1236 abort ();
1238 /* There must be either one decimal point or one p. */
1239 if (decpt == 0 && gotp == 0)
1240 abort ();
1242 shcount -= 4;
1243 if (high == 0 && low == 0)
1244 return dconst0;
1246 /* Normalize. */
1247 nrmcount = 0;
1248 if (high == 0)
1250 high = low;
1251 low = 0;
1252 nrmcount += 32;
1255 /* Leave a high guard bit for carry-out. */
1256 if ((high & 0x80000000) != 0)
1258 lost |= low & 1;
1259 low = (low >> 1) | (high << 31);
1260 high = high >> 1;
1261 nrmcount -= 1;
1264 if ((high & 0xffff8000) == 0)
1266 high = (high << 16) + ((low >> 16) & 0xffff);
1267 low = low << 16;
1268 nrmcount += 16;
1271 while ((high & 0xc0000000) == 0)
1273 high = (high << 1) + ((low >> 31) & 1);
1274 low = low << 1;
1275 nrmcount += 1;
1278 if (isfloat || GET_MODE_SIZE (mode) == UNITS_PER_WORD)
1280 /* Keep 24 bits precision, bits 0x7fffff80.
1281 Rounding bit is 0x40. */
1282 lost = lost | low | (high & 0x3f);
1283 low = 0;
1284 if (high & 0x40)
1286 if ((high & 0x80) || lost)
1287 high += 0x40;
1289 high &= 0xffffff80;
1291 else
1293 /* We need real.c to do long double formats, so here default
1294 to double precision. */
1295 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1296 /* IEEE double.
1297 Keep 53 bits precision, bits 0x7fffffff fffffc00.
1298 Rounding bit is low word 0x200. */
1299 lost = lost | (low & 0x1ff);
1300 if (low & 0x200)
1302 if ((low & 0x400) || lost)
1304 low = (low + 0x200) & 0xfffffc00;
1305 if (low == 0)
1306 high += 1;
1309 low &= 0xfffffc00;
1310 #else
1311 /* Assume it's a VAX with 56-bit significand,
1312 bits 0x7fffffff ffffff80. */
1313 lost = lost | (low & 0x7f);
1314 if (low & 0x40)
1316 if ((low & 0x80) || lost)
1318 low = (low + 0x40) & 0xffffff80;
1319 if (low == 0)
1320 high += 1;
1323 low &= 0xffffff80;
1324 #endif
1327 ip = (double) high;
1328 ip = REAL_VALUE_LDEXP (ip, 32) + (double) low;
1329 /* Apply shifts and exponent value as power of 2. */
1330 ip = REAL_VALUE_LDEXP (ip, expon - (nrmcount + frexpon));
1332 if (sign < 0)
1333 ip = -ip;
1334 return ip;
1337 #endif /* no REAL_ARITHMETIC */
1339 /* Given T, an expression, return the negation of T. Allow for T to be
1340 null, in which case return null. */
1342 static tree
1343 negate_expr (t)
1344 tree t;
1346 tree type;
1347 tree tem;
1349 if (t == 0)
1350 return 0;
1352 type = TREE_TYPE (t);
1353 STRIP_SIGN_NOPS (t);
1355 switch (TREE_CODE (t))
1357 case INTEGER_CST:
1358 case REAL_CST:
1359 if (! TREE_UNSIGNED (type)
1360 && 0 != (tem = fold (build1 (NEGATE_EXPR, type, t)))
1361 && ! TREE_OVERFLOW (tem))
1362 return tem;
1363 break;
1365 case NEGATE_EXPR:
1366 return convert (type, TREE_OPERAND (t, 0));
1368 case MINUS_EXPR:
1369 /* - (A - B) -> B - A */
1370 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1371 return convert (type,
1372 fold (build (MINUS_EXPR, TREE_TYPE (t),
1373 TREE_OPERAND (t, 1),
1374 TREE_OPERAND (t, 0))));
1375 break;
1377 default:
1378 break;
1381 return convert (type, fold (build1 (NEGATE_EXPR, TREE_TYPE (t), t)));
1384 /* Split a tree IN into a constant, literal and variable parts that could be
1385 combined with CODE to make IN. "constant" means an expression with
1386 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1387 commutative arithmetic operation. Store the constant part into *CONP,
1388 the literal in *LITP and return the variable part. If a part isn't
1389 present, set it to null. If the tree does not decompose in this way,
1390 return the entire tree as the variable part and the other parts as null.
1392 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1393 case, we negate an operand that was subtracted. Except if it is a
1394 literal for which we use *MINUS_LITP instead.
1396 If NEGATE_P is true, we are negating all of IN, again except a literal
1397 for which we use *MINUS_LITP instead.
1399 If IN is itself a literal or constant, return it as appropriate.
1401 Note that we do not guarantee that any of the three values will be the
1402 same type as IN, but they will have the same signedness and mode. */
1404 static tree
1405 split_tree (in, code, conp, litp, minus_litp, negate_p)
1406 tree in;
1407 enum tree_code code;
1408 tree *conp, *litp, *minus_litp;
1409 int negate_p;
1411 tree var = 0;
1413 *conp = 0;
1414 *litp = 0;
1415 *minus_litp = 0;
1417 /* Strip any conversions that don't change the machine mode or signedness. */
1418 STRIP_SIGN_NOPS (in);
1420 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1421 *litp = in;
1422 else if (TREE_CODE (in) == code
1423 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1424 /* We can associate addition and subtraction together (even
1425 though the C standard doesn't say so) for integers because
1426 the value is not affected. For reals, the value might be
1427 affected, so we can't. */
1428 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1429 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1431 tree op0 = TREE_OPERAND (in, 0);
1432 tree op1 = TREE_OPERAND (in, 1);
1433 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1434 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1436 /* First see if either of the operands is a literal, then a constant. */
1437 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1438 *litp = op0, op0 = 0;
1439 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1440 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1442 if (op0 != 0 && TREE_CONSTANT (op0))
1443 *conp = op0, op0 = 0;
1444 else if (op1 != 0 && TREE_CONSTANT (op1))
1445 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1447 /* If we haven't dealt with either operand, this is not a case we can
1448 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1449 if (op0 != 0 && op1 != 0)
1450 var = in;
1451 else if (op0 != 0)
1452 var = op0;
1453 else
1454 var = op1, neg_var_p = neg1_p;
1456 /* Now do any needed negations. */
1457 if (neg_litp_p)
1458 *minus_litp = *litp, *litp = 0;
1459 if (neg_conp_p)
1460 *conp = negate_expr (*conp);
1461 if (neg_var_p)
1462 var = negate_expr (var);
1464 else if (TREE_CONSTANT (in))
1465 *conp = in;
1466 else
1467 var = in;
1469 if (negate_p)
1471 if (*litp)
1472 *minus_litp = *litp, *litp = 0;
1473 else if (*minus_litp)
1474 *litp = *minus_litp, *minus_litp = 0;
1475 *conp = negate_expr (*conp);
1476 var = negate_expr (var);
1479 return var;
1482 /* Re-associate trees split by the above function. T1 and T2 are either
1483 expressions to associate or null. Return the new expression, if any. If
1484 we build an operation, do it in TYPE and with CODE. */
1486 static tree
1487 associate_trees (t1, t2, code, type)
1488 tree t1, t2;
1489 enum tree_code code;
1490 tree type;
1492 if (t1 == 0)
1493 return t2;
1494 else if (t2 == 0)
1495 return t1;
1497 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1498 try to fold this since we will have infinite recursion. But do
1499 deal with any NEGATE_EXPRs. */
1500 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1501 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1503 if (TREE_CODE (t1) == NEGATE_EXPR)
1504 return build (MINUS_EXPR, type, convert (type, t2),
1505 convert (type, TREE_OPERAND (t1, 0)));
1506 else if (TREE_CODE (t2) == NEGATE_EXPR)
1507 return build (MINUS_EXPR, type, convert (type, t1),
1508 convert (type, TREE_OPERAND (t2, 0)));
1509 else
1510 return build (code, type, convert (type, t1), convert (type, t2));
1513 return fold (build (code, type, convert (type, t1), convert (type, t2)));
1516 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1517 to produce a new constant.
1519 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1521 static tree
1522 int_const_binop (code, arg1, arg2, notrunc)
1523 enum tree_code code;
1524 tree arg1, arg2;
1525 int notrunc;
1527 unsigned HOST_WIDE_INT int1l, int2l;
1528 HOST_WIDE_INT int1h, int2h;
1529 unsigned HOST_WIDE_INT low;
1530 HOST_WIDE_INT hi;
1531 unsigned HOST_WIDE_INT garbagel;
1532 HOST_WIDE_INT garbageh;
1533 tree t;
1534 tree type = TREE_TYPE (arg1);
1535 int uns = TREE_UNSIGNED (type);
1536 int is_sizetype
1537 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1538 int overflow = 0;
1539 int no_overflow = 0;
1541 int1l = TREE_INT_CST_LOW (arg1);
1542 int1h = TREE_INT_CST_HIGH (arg1);
1543 int2l = TREE_INT_CST_LOW (arg2);
1544 int2h = TREE_INT_CST_HIGH (arg2);
1546 switch (code)
1548 case BIT_IOR_EXPR:
1549 low = int1l | int2l, hi = int1h | int2h;
1550 break;
1552 case BIT_XOR_EXPR:
1553 low = int1l ^ int2l, hi = int1h ^ int2h;
1554 break;
1556 case BIT_AND_EXPR:
1557 low = int1l & int2l, hi = int1h & int2h;
1558 break;
1560 case BIT_ANDTC_EXPR:
1561 low = int1l & ~int2l, hi = int1h & ~int2h;
1562 break;
1564 case RSHIFT_EXPR:
1565 int2l = -int2l;
1566 case LSHIFT_EXPR:
1567 /* It's unclear from the C standard whether shifts can overflow.
1568 The following code ignores overflow; perhaps a C standard
1569 interpretation ruling is needed. */
1570 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1571 &low, &hi, !uns);
1572 no_overflow = 1;
1573 break;
1575 case RROTATE_EXPR:
1576 int2l = - int2l;
1577 case LROTATE_EXPR:
1578 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1579 &low, &hi);
1580 break;
1582 case PLUS_EXPR:
1583 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1584 break;
1586 case MINUS_EXPR:
1587 neg_double (int2l, int2h, &low, &hi);
1588 add_double (int1l, int1h, low, hi, &low, &hi);
1589 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1590 break;
1592 case MULT_EXPR:
1593 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1594 break;
1596 case TRUNC_DIV_EXPR:
1597 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1598 case EXACT_DIV_EXPR:
1599 /* This is a shortcut for a common special case. */
1600 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1601 && ! TREE_CONSTANT_OVERFLOW (arg1)
1602 && ! TREE_CONSTANT_OVERFLOW (arg2)
1603 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1605 if (code == CEIL_DIV_EXPR)
1606 int1l += int2l - 1;
1608 low = int1l / int2l, hi = 0;
1609 break;
1612 /* ... fall through ... */
1614 case ROUND_DIV_EXPR:
1615 if (int2h == 0 && int2l == 1)
1617 low = int1l, hi = int1h;
1618 break;
1620 if (int1l == int2l && int1h == int2h
1621 && ! (int1l == 0 && int1h == 0))
1623 low = 1, hi = 0;
1624 break;
1626 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1627 &low, &hi, &garbagel, &garbageh);
1628 break;
1630 case TRUNC_MOD_EXPR:
1631 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1632 /* This is a shortcut for a common special case. */
1633 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1634 && ! TREE_CONSTANT_OVERFLOW (arg1)
1635 && ! TREE_CONSTANT_OVERFLOW (arg2)
1636 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1638 if (code == CEIL_MOD_EXPR)
1639 int1l += int2l - 1;
1640 low = int1l % int2l, hi = 0;
1641 break;
1644 /* ... fall through ... */
1646 case ROUND_MOD_EXPR:
1647 overflow = div_and_round_double (code, uns,
1648 int1l, int1h, int2l, int2h,
1649 &garbagel, &garbageh, &low, &hi);
1650 break;
1652 case MIN_EXPR:
1653 case MAX_EXPR:
1654 if (uns)
1655 low = (((unsigned HOST_WIDE_INT) int1h
1656 < (unsigned HOST_WIDE_INT) int2h)
1657 || (((unsigned HOST_WIDE_INT) int1h
1658 == (unsigned HOST_WIDE_INT) int2h)
1659 && int1l < int2l));
1660 else
1661 low = (int1h < int2h
1662 || (int1h == int2h && int1l < int2l));
1664 if (low == (code == MIN_EXPR))
1665 low = int1l, hi = int1h;
1666 else
1667 low = int2l, hi = int2h;
1668 break;
1670 default:
1671 abort ();
1674 /* If this is for a sizetype, can be represented as one (signed)
1675 HOST_WIDE_INT word, and doesn't overflow, use size_int since it caches
1676 constants. */
1677 if (is_sizetype
1678 && ((hi == 0 && (HOST_WIDE_INT) low >= 0)
1679 || (hi == -1 && (HOST_WIDE_INT) low < 0))
1680 && overflow == 0 && ! TREE_OVERFLOW (arg1) && ! TREE_OVERFLOW (arg2))
1681 return size_int_type_wide (low, type);
1682 else
1684 t = build_int_2 (low, hi);
1685 TREE_TYPE (t) = TREE_TYPE (arg1);
1688 TREE_OVERFLOW (t)
1689 = ((notrunc
1690 ? (!uns || is_sizetype) && overflow
1691 : (force_fit_type (t, (!uns || is_sizetype) && overflow)
1692 && ! no_overflow))
1693 | TREE_OVERFLOW (arg1)
1694 | TREE_OVERFLOW (arg2));
1696 /* If we're doing a size calculation, unsigned arithmetic does overflow.
1697 So check if force_fit_type truncated the value. */
1698 if (is_sizetype
1699 && ! TREE_OVERFLOW (t)
1700 && (TREE_INT_CST_HIGH (t) != hi
1701 || TREE_INT_CST_LOW (t) != low))
1702 TREE_OVERFLOW (t) = 1;
1704 TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1705 | TREE_CONSTANT_OVERFLOW (arg1)
1706 | TREE_CONSTANT_OVERFLOW (arg2));
1707 return t;
1710 /* Define input and output argument for const_binop_1. */
1711 struct cb_args
1713 enum tree_code code; /* Input: tree code for operation. */
1714 tree type; /* Input: tree type for operation. */
1715 REAL_VALUE_TYPE d1, d2; /* Input: floating point operands. */
1716 tree t; /* Output: constant for result. */
1719 /* Do the real arithmetic for const_binop while protected by a
1720 float overflow handler. */
1722 static void
1723 const_binop_1 (data)
1724 PTR data;
1726 struct cb_args *args = (struct cb_args *) data;
1727 REAL_VALUE_TYPE value;
1729 #ifdef REAL_ARITHMETIC
1730 REAL_ARITHMETIC (value, args->code, args->d1, args->d2);
1731 #else
1732 switch (args->code)
1734 case PLUS_EXPR:
1735 value = args->d1 + args->d2;
1736 break;
1738 case MINUS_EXPR:
1739 value = args->d1 - args->d2;
1740 break;
1742 case MULT_EXPR:
1743 value = args->d1 * args->d2;
1744 break;
1746 case RDIV_EXPR:
1747 #ifndef REAL_INFINITY
1748 if (args->d2 == 0)
1749 abort ();
1750 #endif
1752 value = args->d1 / args->d2;
1753 break;
1755 case MIN_EXPR:
1756 value = MIN (args->d1, args->d2);
1757 break;
1759 case MAX_EXPR:
1760 value = MAX (args->d1, args->d2);
1761 break;
1763 default:
1764 abort ();
1766 #endif /* no REAL_ARITHMETIC */
1768 args->t
1769 = build_real (args->type,
1770 real_value_truncate (TYPE_MODE (args->type), value));
1773 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1774 constant. We assume ARG1 and ARG2 have the same data type, or at least
1775 are the same kind of constant and the same machine mode.
1777 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1779 static tree
1780 const_binop (code, arg1, arg2, notrunc)
1781 enum tree_code code;
1782 tree arg1, arg2;
1783 int notrunc;
1785 STRIP_NOPS (arg1);
1786 STRIP_NOPS (arg2);
1788 if (TREE_CODE (arg1) == INTEGER_CST)
1789 return int_const_binop (code, arg1, arg2, notrunc);
1791 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1792 if (TREE_CODE (arg1) == REAL_CST)
1794 REAL_VALUE_TYPE d1;
1795 REAL_VALUE_TYPE d2;
1796 int overflow = 0;
1797 tree t;
1798 struct cb_args args;
1800 d1 = TREE_REAL_CST (arg1);
1801 d2 = TREE_REAL_CST (arg2);
1803 /* If either operand is a NaN, just return it. Otherwise, set up
1804 for floating-point trap; we return an overflow. */
1805 if (REAL_VALUE_ISNAN (d1))
1806 return arg1;
1807 else if (REAL_VALUE_ISNAN (d2))
1808 return arg2;
1810 /* Setup input for const_binop_1() */
1811 args.type = TREE_TYPE (arg1);
1812 args.d1 = d1;
1813 args.d2 = d2;
1814 args.code = code;
1816 if (do_float_handler (const_binop_1, (PTR) &args))
1817 /* Receive output from const_binop_1. */
1818 t = args.t;
1819 else
1821 /* We got an exception from const_binop_1. */
1822 t = copy_node (arg1);
1823 overflow = 1;
1826 TREE_OVERFLOW (t)
1827 = (force_fit_type (t, overflow)
1828 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1829 TREE_CONSTANT_OVERFLOW (t)
1830 = TREE_OVERFLOW (t)
1831 | TREE_CONSTANT_OVERFLOW (arg1)
1832 | TREE_CONSTANT_OVERFLOW (arg2);
1833 return t;
1835 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1836 if (TREE_CODE (arg1) == COMPLEX_CST)
1838 tree type = TREE_TYPE (arg1);
1839 tree r1 = TREE_REALPART (arg1);
1840 tree i1 = TREE_IMAGPART (arg1);
1841 tree r2 = TREE_REALPART (arg2);
1842 tree i2 = TREE_IMAGPART (arg2);
1843 tree t;
1845 switch (code)
1847 case PLUS_EXPR:
1848 t = build_complex (type,
1849 const_binop (PLUS_EXPR, r1, r2, notrunc),
1850 const_binop (PLUS_EXPR, i1, i2, notrunc));
1851 break;
1853 case MINUS_EXPR:
1854 t = build_complex (type,
1855 const_binop (MINUS_EXPR, r1, r2, notrunc),
1856 const_binop (MINUS_EXPR, i1, i2, notrunc));
1857 break;
1859 case MULT_EXPR:
1860 t = build_complex (type,
1861 const_binop (MINUS_EXPR,
1862 const_binop (MULT_EXPR,
1863 r1, r2, notrunc),
1864 const_binop (MULT_EXPR,
1865 i1, i2, notrunc),
1866 notrunc),
1867 const_binop (PLUS_EXPR,
1868 const_binop (MULT_EXPR,
1869 r1, i2, notrunc),
1870 const_binop (MULT_EXPR,
1871 i1, r2, notrunc),
1872 notrunc));
1873 break;
1875 case RDIV_EXPR:
1877 tree magsquared
1878 = const_binop (PLUS_EXPR,
1879 const_binop (MULT_EXPR, r2, r2, notrunc),
1880 const_binop (MULT_EXPR, i2, i2, notrunc),
1881 notrunc);
1883 t = build_complex (type,
1884 const_binop
1885 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1886 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1887 const_binop (PLUS_EXPR,
1888 const_binop (MULT_EXPR, r1, r2,
1889 notrunc),
1890 const_binop (MULT_EXPR, i1, i2,
1891 notrunc),
1892 notrunc),
1893 magsquared, notrunc),
1894 const_binop
1895 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1896 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1897 const_binop (MINUS_EXPR,
1898 const_binop (MULT_EXPR, i1, r2,
1899 notrunc),
1900 const_binop (MULT_EXPR, r1, i2,
1901 notrunc),
1902 notrunc),
1903 magsquared, notrunc));
1905 break;
1907 default:
1908 abort ();
1910 return t;
1912 return 0;
1915 /* These are the hash table functions for the hash table of INTEGER_CST
1916 nodes of a sizetype. */
1918 /* Return the hash code code X, an INTEGER_CST. */
1920 static hashval_t
1921 size_htab_hash (x)
1922 const void *x;
1924 tree t = (tree) x;
1926 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1927 ^ (hashval_t) ((long) TREE_TYPE (t) >> 3)
1928 ^ (TREE_OVERFLOW (t) << 20));
1931 /* Return non-zero if the value represented by *X (an INTEGER_CST tree node)
1932 is the same as that given by *Y, which is the same. */
1934 static int
1935 size_htab_eq (x, y)
1936 const void *x;
1937 const void *y;
1939 tree xt = (tree) x;
1940 tree yt = (tree) y;
1942 return (TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1943 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt)
1944 && TREE_TYPE (xt) == TREE_TYPE (yt)
1945 && TREE_OVERFLOW (xt) == TREE_OVERFLOW (yt));
1948 /* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
1949 bits are given by NUMBER and of the sizetype represented by KIND. */
1951 tree
1952 size_int_wide (number, kind)
1953 HOST_WIDE_INT number;
1954 enum size_type_kind kind;
1956 return size_int_type_wide (number, sizetype_tab[(int) kind]);
1959 /* Likewise, but the desired type is specified explicitly. */
1961 tree
1962 size_int_type_wide (number, type)
1963 HOST_WIDE_INT number;
1964 tree type;
1966 static htab_t size_htab = 0;
1967 static tree new_const = 0;
1968 PTR *slot;
1970 if (size_htab == 0)
1972 size_htab = htab_create (1024, size_htab_hash, size_htab_eq, NULL);
1973 ggc_add_deletable_htab (size_htab, NULL, NULL);
1974 new_const = make_node (INTEGER_CST);
1975 ggc_add_tree_root (&new_const, 1);
1978 /* Adjust NEW_CONST to be the constant we want. If it's already in the
1979 hash table, we return the value from the hash table. Otherwise, we
1980 place that in the hash table and make a new node for the next time. */
1981 TREE_INT_CST_LOW (new_const) = number;
1982 TREE_INT_CST_HIGH (new_const) = number < 0 ? -1 : 0;
1983 TREE_TYPE (new_const) = type;
1984 TREE_OVERFLOW (new_const) = TREE_CONSTANT_OVERFLOW (new_const)
1985 = force_fit_type (new_const, 0);
1987 slot = htab_find_slot (size_htab, new_const, INSERT);
1988 if (*slot == 0)
1990 tree t = new_const;
1992 *slot = (PTR) new_const;
1993 new_const = make_node (INTEGER_CST);
1994 return t;
1996 else
1997 return (tree) *slot;
2000 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2001 is a tree code. The type of the result is taken from the operands.
2002 Both must be the same type integer type and it must be a size type.
2003 If the operands are constant, so is the result. */
2005 tree
2006 size_binop (code, arg0, arg1)
2007 enum tree_code code;
2008 tree arg0, arg1;
2010 tree type = TREE_TYPE (arg0);
2012 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
2013 || type != TREE_TYPE (arg1))
2014 abort ();
2016 /* Handle the special case of two integer constants faster. */
2017 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2019 /* And some specific cases even faster than that. */
2020 if (code == PLUS_EXPR && integer_zerop (arg0))
2021 return arg1;
2022 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
2023 && integer_zerop (arg1))
2024 return arg0;
2025 else if (code == MULT_EXPR && integer_onep (arg0))
2026 return arg1;
2028 /* Handle general case of two integer constants. */
2029 return int_const_binop (code, arg0, arg1, 0);
2032 if (arg0 == error_mark_node || arg1 == error_mark_node)
2033 return error_mark_node;
2035 return fold (build (code, type, arg0, arg1));
2038 /* Given two values, either both of sizetype or both of bitsizetype,
2039 compute the difference between the two values. Return the value
2040 in signed type corresponding to the type of the operands. */
2042 tree
2043 size_diffop (arg0, arg1)
2044 tree arg0, arg1;
2046 tree type = TREE_TYPE (arg0);
2047 tree ctype;
2049 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
2050 || type != TREE_TYPE (arg1))
2051 abort ();
2053 /* If the type is already signed, just do the simple thing. */
2054 if (! TREE_UNSIGNED (type))
2055 return size_binop (MINUS_EXPR, arg0, arg1);
2057 ctype = (type == bitsizetype || type == ubitsizetype
2058 ? sbitsizetype : ssizetype);
2060 /* If either operand is not a constant, do the conversions to the signed
2061 type and subtract. The hardware will do the right thing with any
2062 overflow in the subtraction. */
2063 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2064 return size_binop (MINUS_EXPR, convert (ctype, arg0),
2065 convert (ctype, arg1));
2067 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2068 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2069 overflow) and negate (which can't either). Special-case a result
2070 of zero while we're here. */
2071 if (tree_int_cst_equal (arg0, arg1))
2072 return convert (ctype, integer_zero_node);
2073 else if (tree_int_cst_lt (arg1, arg0))
2074 return convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2075 else
2076 return size_binop (MINUS_EXPR, convert (ctype, integer_zero_node),
2077 convert (ctype, size_binop (MINUS_EXPR, arg1, arg0)));
2080 /* This structure is used to communicate arguments to fold_convert_1. */
2081 struct fc_args
2083 tree arg1; /* Input: value to convert. */
2084 tree type; /* Input: type to convert value to. */
2085 tree t; /* Output: result of conversion. */
2088 /* Function to convert floating-point constants, protected by floating
2089 point exception handler. */
2091 static void
2092 fold_convert_1 (data)
2093 PTR data;
2095 struct fc_args *args = (struct fc_args *) data;
2097 args->t = build_real (args->type,
2098 real_value_truncate (TYPE_MODE (args->type),
2099 TREE_REAL_CST (args->arg1)));
2102 /* Given T, a tree representing type conversion of ARG1, a constant,
2103 return a constant tree representing the result of conversion. */
2105 static tree
2106 fold_convert (t, arg1)
2107 tree t;
2108 tree arg1;
2110 tree type = TREE_TYPE (t);
2111 int overflow = 0;
2113 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2115 if (TREE_CODE (arg1) == INTEGER_CST)
2117 /* If we would build a constant wider than GCC supports,
2118 leave the conversion unfolded. */
2119 if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
2120 return t;
2122 /* If we are trying to make a sizetype for a small integer, use
2123 size_int to pick up cached types to reduce duplicate nodes. */
2124 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
2125 && !TREE_CONSTANT_OVERFLOW (arg1)
2126 && compare_tree_int (arg1, 10000) < 0)
2127 return size_int_type_wide (TREE_INT_CST_LOW (arg1), type);
2129 /* Given an integer constant, make new constant with new type,
2130 appropriately sign-extended or truncated. */
2131 t = build_int_2 (TREE_INT_CST_LOW (arg1),
2132 TREE_INT_CST_HIGH (arg1));
2133 TREE_TYPE (t) = type;
2134 /* Indicate an overflow if (1) ARG1 already overflowed,
2135 or (2) force_fit_type indicates an overflow.
2136 Tell force_fit_type that an overflow has already occurred
2137 if ARG1 is a too-large unsigned value and T is signed.
2138 But don't indicate an overflow if converting a pointer. */
2139 TREE_OVERFLOW (t)
2140 = ((force_fit_type (t,
2141 (TREE_INT_CST_HIGH (arg1) < 0
2142 && (TREE_UNSIGNED (type)
2143 < TREE_UNSIGNED (TREE_TYPE (arg1)))))
2144 && ! POINTER_TYPE_P (TREE_TYPE (arg1)))
2145 || TREE_OVERFLOW (arg1));
2146 TREE_CONSTANT_OVERFLOW (t)
2147 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2149 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2150 else if (TREE_CODE (arg1) == REAL_CST)
2152 /* Don't initialize these, use assignments.
2153 Initialized local aggregates don't work on old compilers. */
2154 REAL_VALUE_TYPE x;
2155 REAL_VALUE_TYPE l;
2156 REAL_VALUE_TYPE u;
2157 tree type1 = TREE_TYPE (arg1);
2158 int no_upper_bound;
2160 x = TREE_REAL_CST (arg1);
2161 l = real_value_from_int_cst (type1, TYPE_MIN_VALUE (type));
2163 no_upper_bound = (TYPE_MAX_VALUE (type) == NULL);
2164 if (!no_upper_bound)
2165 u = real_value_from_int_cst (type1, TYPE_MAX_VALUE (type));
2167 /* See if X will be in range after truncation towards 0.
2168 To compensate for truncation, move the bounds away from 0,
2169 but reject if X exactly equals the adjusted bounds. */
2170 #ifdef REAL_ARITHMETIC
2171 REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
2172 if (!no_upper_bound)
2173 REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
2174 #else
2175 l--;
2176 if (!no_upper_bound)
2177 u++;
2178 #endif
2179 /* If X is a NaN, use zero instead and show we have an overflow.
2180 Otherwise, range check. */
2181 if (REAL_VALUE_ISNAN (x))
2182 overflow = 1, x = dconst0;
2183 else if (! (REAL_VALUES_LESS (l, x)
2184 && !no_upper_bound
2185 && REAL_VALUES_LESS (x, u)))
2186 overflow = 1;
2188 #ifndef REAL_ARITHMETIC
2190 HOST_WIDE_INT low, high;
2191 HOST_WIDE_INT half_word
2192 = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
2194 if (x < 0)
2195 x = -x;
2197 high = (HOST_WIDE_INT) (x / half_word / half_word);
2198 x -= (REAL_VALUE_TYPE) high * half_word * half_word;
2199 if (x >= (REAL_VALUE_TYPE) half_word * half_word / 2)
2201 low = x - (REAL_VALUE_TYPE) half_word * half_word / 2;
2202 low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1);
2204 else
2205 low = (HOST_WIDE_INT) x;
2206 if (TREE_REAL_CST (arg1) < 0)
2207 neg_double (low, high, &low, &high);
2208 t = build_int_2 (low, high);
2210 #else
2212 HOST_WIDE_INT low, high;
2213 REAL_VALUE_TO_INT (&low, &high, x);
2214 t = build_int_2 (low, high);
2216 #endif
2217 TREE_TYPE (t) = type;
2218 TREE_OVERFLOW (t)
2219 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
2220 TREE_CONSTANT_OVERFLOW (t)
2221 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2223 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2224 TREE_TYPE (t) = type;
2226 else if (TREE_CODE (type) == REAL_TYPE)
2228 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2229 if (TREE_CODE (arg1) == INTEGER_CST)
2230 return build_real_from_int_cst (type, arg1);
2231 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2232 if (TREE_CODE (arg1) == REAL_CST)
2234 struct fc_args args;
2236 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
2238 t = arg1;
2239 TREE_TYPE (arg1) = type;
2240 return t;
2243 /* Setup input for fold_convert_1() */
2244 args.arg1 = arg1;
2245 args.type = type;
2247 if (do_float_handler (fold_convert_1, (PTR) &args))
2249 /* Receive output from fold_convert_1() */
2250 t = args.t;
2252 else
2254 /* We got an exception from fold_convert_1() */
2255 overflow = 1;
2256 t = copy_node (arg1);
2259 TREE_OVERFLOW (t)
2260 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
2261 TREE_CONSTANT_OVERFLOW (t)
2262 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2263 return t;
2266 TREE_CONSTANT (t) = 1;
2267 return t;
2270 /* Return an expr equal to X but certainly not valid as an lvalue. */
2272 tree
2273 non_lvalue (x)
2274 tree x;
2276 tree result;
2278 /* These things are certainly not lvalues. */
2279 if (TREE_CODE (x) == NON_LVALUE_EXPR
2280 || TREE_CODE (x) == INTEGER_CST
2281 || TREE_CODE (x) == REAL_CST
2282 || TREE_CODE (x) == STRING_CST
2283 || TREE_CODE (x) == ADDR_EXPR)
2284 return x;
2286 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2287 TREE_CONSTANT (result) = TREE_CONSTANT (x);
2288 return result;
2291 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2292 Zero means allow extended lvalues. */
2294 int pedantic_lvalues;
2296 /* When pedantic, return an expr equal to X but certainly not valid as a
2297 pedantic lvalue. Otherwise, return X. */
2299 tree
2300 pedantic_non_lvalue (x)
2301 tree x;
2303 if (pedantic_lvalues)
2304 return non_lvalue (x);
2305 else
2306 return x;
2309 /* Given a tree comparison code, return the code that is the logical inverse
2310 of the given code. It is not safe to do this for floating-point
2311 comparisons, except for NE_EXPR and EQ_EXPR. */
2313 static enum tree_code
2314 invert_tree_comparison (code)
2315 enum tree_code code;
2317 switch (code)
2319 case EQ_EXPR:
2320 return NE_EXPR;
2321 case NE_EXPR:
2322 return EQ_EXPR;
2323 case GT_EXPR:
2324 return LE_EXPR;
2325 case GE_EXPR:
2326 return LT_EXPR;
2327 case LT_EXPR:
2328 return GE_EXPR;
2329 case LE_EXPR:
2330 return GT_EXPR;
2331 default:
2332 abort ();
2336 /* Similar, but return the comparison that results if the operands are
2337 swapped. This is safe for floating-point. */
2339 static enum tree_code
2340 swap_tree_comparison (code)
2341 enum tree_code code;
2343 switch (code)
2345 case EQ_EXPR:
2346 case NE_EXPR:
2347 return code;
2348 case GT_EXPR:
2349 return LT_EXPR;
2350 case GE_EXPR:
2351 return LE_EXPR;
2352 case LT_EXPR:
2353 return GT_EXPR;
2354 case LE_EXPR:
2355 return GE_EXPR;
2356 default:
2357 abort ();
2361 /* Return nonzero if CODE is a tree code that represents a truth value. */
2363 static int
2364 truth_value_p (code)
2365 enum tree_code code;
2367 return (TREE_CODE_CLASS (code) == '<'
2368 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2369 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2370 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2373 /* Return nonzero if two operands are necessarily equal.
2374 If ONLY_CONST is non-zero, only return non-zero for constants.
2375 This function tests whether the operands are indistinguishable;
2376 it does not test whether they are equal using C's == operation.
2377 The distinction is important for IEEE floating point, because
2378 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2379 (2) two NaNs may be indistinguishable, but NaN!=NaN. */
2382 operand_equal_p (arg0, arg1, only_const)
2383 tree arg0, arg1;
2384 int only_const;
2386 /* If both types don't have the same signedness, then we can't consider
2387 them equal. We must check this before the STRIP_NOPS calls
2388 because they may change the signedness of the arguments. */
2389 if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
2390 return 0;
2392 STRIP_NOPS (arg0);
2393 STRIP_NOPS (arg1);
2395 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2396 /* This is needed for conversions and for COMPONENT_REF.
2397 Might as well play it safe and always test this. */
2398 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2399 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2400 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2401 return 0;
2403 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2404 We don't care about side effects in that case because the SAVE_EXPR
2405 takes care of that for us. In all other cases, two expressions are
2406 equal if they have no side effects. If we have two identical
2407 expressions with side effects that should be treated the same due
2408 to the only side effects being identical SAVE_EXPR's, that will
2409 be detected in the recursive calls below. */
2410 if (arg0 == arg1 && ! only_const
2411 && (TREE_CODE (arg0) == SAVE_EXPR
2412 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2413 return 1;
2415 /* Next handle constant cases, those for which we can return 1 even
2416 if ONLY_CONST is set. */
2417 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2418 switch (TREE_CODE (arg0))
2420 case INTEGER_CST:
2421 return (! TREE_CONSTANT_OVERFLOW (arg0)
2422 && ! TREE_CONSTANT_OVERFLOW (arg1)
2423 && tree_int_cst_equal (arg0, arg1));
2425 case REAL_CST:
2426 return (! TREE_CONSTANT_OVERFLOW (arg0)
2427 && ! TREE_CONSTANT_OVERFLOW (arg1)
2428 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2429 TREE_REAL_CST (arg1)));
2431 case VECTOR_CST:
2433 tree v1, v2;
2435 if (TREE_CONSTANT_OVERFLOW (arg0)
2436 || TREE_CONSTANT_OVERFLOW (arg1))
2437 return 0;
2439 v1 = TREE_VECTOR_CST_ELTS (arg0);
2440 v2 = TREE_VECTOR_CST_ELTS (arg1);
2441 while (v1 && v2)
2443 if (!operand_equal_p (v1, v2, only_const))
2444 return 0;
2445 v1 = TREE_CHAIN (v1);
2446 v2 = TREE_CHAIN (v2);
2449 return 1;
2452 case COMPLEX_CST:
2453 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2454 only_const)
2455 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2456 only_const));
2458 case STRING_CST:
2459 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2460 && ! memcmp (TREE_STRING_POINTER (arg0),
2461 TREE_STRING_POINTER (arg1),
2462 TREE_STRING_LENGTH (arg0)));
2464 case ADDR_EXPR:
2465 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2467 default:
2468 break;
2471 if (only_const)
2472 return 0;
2474 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2476 case '1':
2477 /* Two conversions are equal only if signedness and modes match. */
2478 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
2479 && (TREE_UNSIGNED (TREE_TYPE (arg0))
2480 != TREE_UNSIGNED (TREE_TYPE (arg1))))
2481 return 0;
2483 return operand_equal_p (TREE_OPERAND (arg0, 0),
2484 TREE_OPERAND (arg1, 0), 0);
2486 case '<':
2487 case '2':
2488 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)
2489 && operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1),
2491 return 1;
2493 /* For commutative ops, allow the other order. */
2494 return ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MULT_EXPR
2495 || TREE_CODE (arg0) == MIN_EXPR || TREE_CODE (arg0) == MAX_EXPR
2496 || TREE_CODE (arg0) == BIT_IOR_EXPR
2497 || TREE_CODE (arg0) == BIT_XOR_EXPR
2498 || TREE_CODE (arg0) == BIT_AND_EXPR
2499 || TREE_CODE (arg0) == NE_EXPR || TREE_CODE (arg0) == EQ_EXPR)
2500 && operand_equal_p (TREE_OPERAND (arg0, 0),
2501 TREE_OPERAND (arg1, 1), 0)
2502 && operand_equal_p (TREE_OPERAND (arg0, 1),
2503 TREE_OPERAND (arg1, 0), 0));
2505 case 'r':
2506 /* If either of the pointer (or reference) expressions we are dereferencing
2507 contain a side effect, these cannot be equal. */
2508 if (TREE_SIDE_EFFECTS (arg0)
2509 || TREE_SIDE_EFFECTS (arg1))
2510 return 0;
2512 switch (TREE_CODE (arg0))
2514 case INDIRECT_REF:
2515 return operand_equal_p (TREE_OPERAND (arg0, 0),
2516 TREE_OPERAND (arg1, 0), 0);
2518 case COMPONENT_REF:
2519 case ARRAY_REF:
2520 case ARRAY_RANGE_REF:
2521 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2522 TREE_OPERAND (arg1, 0), 0)
2523 && operand_equal_p (TREE_OPERAND (arg0, 1),
2524 TREE_OPERAND (arg1, 1), 0));
2526 case BIT_FIELD_REF:
2527 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2528 TREE_OPERAND (arg1, 0), 0)
2529 && operand_equal_p (TREE_OPERAND (arg0, 1),
2530 TREE_OPERAND (arg1, 1), 0)
2531 && operand_equal_p (TREE_OPERAND (arg0, 2),
2532 TREE_OPERAND (arg1, 2), 0));
2533 default:
2534 return 0;
2537 case 'e':
2538 if (TREE_CODE (arg0) == RTL_EXPR)
2539 return rtx_equal_p (RTL_EXPR_RTL (arg0), RTL_EXPR_RTL (arg1));
2540 return 0;
2542 default:
2543 return 0;
2547 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2548 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2550 When in doubt, return 0. */
2552 static int
2553 operand_equal_for_comparison_p (arg0, arg1, other)
2554 tree arg0, arg1;
2555 tree other;
2557 int unsignedp1, unsignedpo;
2558 tree primarg0, primarg1, primother;
2559 unsigned int correct_width;
2561 if (operand_equal_p (arg0, arg1, 0))
2562 return 1;
2564 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2565 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2566 return 0;
2568 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2569 and see if the inner values are the same. This removes any
2570 signedness comparison, which doesn't matter here. */
2571 primarg0 = arg0, primarg1 = arg1;
2572 STRIP_NOPS (primarg0);
2573 STRIP_NOPS (primarg1);
2574 if (operand_equal_p (primarg0, primarg1, 0))
2575 return 1;
2577 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2578 actual comparison operand, ARG0.
2580 First throw away any conversions to wider types
2581 already present in the operands. */
2583 primarg1 = get_narrower (arg1, &unsignedp1);
2584 primother = get_narrower (other, &unsignedpo);
2586 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2587 if (unsignedp1 == unsignedpo
2588 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2589 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2591 tree type = TREE_TYPE (arg0);
2593 /* Make sure shorter operand is extended the right way
2594 to match the longer operand. */
2595 primarg1 = convert (signed_or_unsigned_type (unsignedp1,
2596 TREE_TYPE (primarg1)),
2597 primarg1);
2599 if (operand_equal_p (arg0, convert (type, primarg1), 0))
2600 return 1;
2603 return 0;
2606 /* See if ARG is an expression that is either a comparison or is performing
2607 arithmetic on comparisons. The comparisons must only be comparing
2608 two different values, which will be stored in *CVAL1 and *CVAL2; if
2609 they are non-zero it means that some operands have already been found.
2610 No variables may be used anywhere else in the expression except in the
2611 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2612 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2614 If this is true, return 1. Otherwise, return zero. */
2616 static int
2617 twoval_comparison_p (arg, cval1, cval2, save_p)
2618 tree arg;
2619 tree *cval1, *cval2;
2620 int *save_p;
2622 enum tree_code code = TREE_CODE (arg);
2623 char class = TREE_CODE_CLASS (code);
2625 /* We can handle some of the 'e' cases here. */
2626 if (class == 'e' && code == TRUTH_NOT_EXPR)
2627 class = '1';
2628 else if (class == 'e'
2629 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2630 || code == COMPOUND_EXPR))
2631 class = '2';
2633 else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0
2634 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2636 /* If we've already found a CVAL1 or CVAL2, this expression is
2637 two complex to handle. */
2638 if (*cval1 || *cval2)
2639 return 0;
2641 class = '1';
2642 *save_p = 1;
2645 switch (class)
2647 case '1':
2648 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2650 case '2':
2651 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2652 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2653 cval1, cval2, save_p));
2655 case 'c':
2656 return 1;
2658 case 'e':
2659 if (code == COND_EXPR)
2660 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2661 cval1, cval2, save_p)
2662 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2663 cval1, cval2, save_p)
2664 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2665 cval1, cval2, save_p));
2666 return 0;
2668 case '<':
2669 /* First see if we can handle the first operand, then the second. For
2670 the second operand, we know *CVAL1 can't be zero. It must be that
2671 one side of the comparison is each of the values; test for the
2672 case where this isn't true by failing if the two operands
2673 are the same. */
2675 if (operand_equal_p (TREE_OPERAND (arg, 0),
2676 TREE_OPERAND (arg, 1), 0))
2677 return 0;
2679 if (*cval1 == 0)
2680 *cval1 = TREE_OPERAND (arg, 0);
2681 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2683 else if (*cval2 == 0)
2684 *cval2 = TREE_OPERAND (arg, 0);
2685 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2687 else
2688 return 0;
2690 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2692 else if (*cval2 == 0)
2693 *cval2 = TREE_OPERAND (arg, 1);
2694 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2696 else
2697 return 0;
2699 return 1;
2701 default:
2702 return 0;
2706 /* ARG is a tree that is known to contain just arithmetic operations and
2707 comparisons. Evaluate the operations in the tree substituting NEW0 for
2708 any occurrence of OLD0 as an operand of a comparison and likewise for
2709 NEW1 and OLD1. */
2711 static tree
2712 eval_subst (arg, old0, new0, old1, new1)
2713 tree arg;
2714 tree old0, new0, old1, new1;
2716 tree type = TREE_TYPE (arg);
2717 enum tree_code code = TREE_CODE (arg);
2718 char class = TREE_CODE_CLASS (code);
2720 /* We can handle some of the 'e' cases here. */
2721 if (class == 'e' && code == TRUTH_NOT_EXPR)
2722 class = '1';
2723 else if (class == 'e'
2724 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2725 class = '2';
2727 switch (class)
2729 case '1':
2730 return fold (build1 (code, type,
2731 eval_subst (TREE_OPERAND (arg, 0),
2732 old0, new0, old1, new1)));
2734 case '2':
2735 return fold (build (code, type,
2736 eval_subst (TREE_OPERAND (arg, 0),
2737 old0, new0, old1, new1),
2738 eval_subst (TREE_OPERAND (arg, 1),
2739 old0, new0, old1, new1)));
2741 case 'e':
2742 switch (code)
2744 case SAVE_EXPR:
2745 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2747 case COMPOUND_EXPR:
2748 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2750 case COND_EXPR:
2751 return fold (build (code, type,
2752 eval_subst (TREE_OPERAND (arg, 0),
2753 old0, new0, old1, new1),
2754 eval_subst (TREE_OPERAND (arg, 1),
2755 old0, new0, old1, new1),
2756 eval_subst (TREE_OPERAND (arg, 2),
2757 old0, new0, old1, new1)));
2758 default:
2759 break;
2761 /* fall through - ??? */
2763 case '<':
2765 tree arg0 = TREE_OPERAND (arg, 0);
2766 tree arg1 = TREE_OPERAND (arg, 1);
2768 /* We need to check both for exact equality and tree equality. The
2769 former will be true if the operand has a side-effect. In that
2770 case, we know the operand occurred exactly once. */
2772 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2773 arg0 = new0;
2774 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2775 arg0 = new1;
2777 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2778 arg1 = new0;
2779 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2780 arg1 = new1;
2782 return fold (build (code, type, arg0, arg1));
2785 default:
2786 return arg;
2790 /* Return a tree for the case when the result of an expression is RESULT
2791 converted to TYPE and OMITTED was previously an operand of the expression
2792 but is now not needed (e.g., we folded OMITTED * 0).
2794 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2795 the conversion of RESULT to TYPE. */
2797 static tree
2798 omit_one_operand (type, result, omitted)
2799 tree type, result, omitted;
2801 tree t = convert (type, result);
2803 if (TREE_SIDE_EFFECTS (omitted))
2804 return build (COMPOUND_EXPR, type, omitted, t);
2806 return non_lvalue (t);
2809 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2811 static tree
2812 pedantic_omit_one_operand (type, result, omitted)
2813 tree type, result, omitted;
2815 tree t = convert (type, result);
2817 if (TREE_SIDE_EFFECTS (omitted))
2818 return build (COMPOUND_EXPR, type, omitted, t);
2820 return pedantic_non_lvalue (t);
2823 /* Return a simplified tree node for the truth-negation of ARG. This
2824 never alters ARG itself. We assume that ARG is an operation that
2825 returns a truth value (0 or 1). */
2827 tree
2828 invert_truthvalue (arg)
2829 tree arg;
2831 tree type = TREE_TYPE (arg);
2832 enum tree_code code = TREE_CODE (arg);
2834 if (code == ERROR_MARK)
2835 return arg;
2837 /* If this is a comparison, we can simply invert it, except for
2838 floating-point non-equality comparisons, in which case we just
2839 enclose a TRUTH_NOT_EXPR around what we have. */
2841 if (TREE_CODE_CLASS (code) == '<')
2843 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
2844 && !flag_unsafe_math_optimizations
2845 && code != NE_EXPR
2846 && code != EQ_EXPR)
2847 return build1 (TRUTH_NOT_EXPR, type, arg);
2848 else
2849 return build (invert_tree_comparison (code), type,
2850 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2853 switch (code)
2855 case INTEGER_CST:
2856 return convert (type, build_int_2 (integer_zerop (arg), 0));
2858 case TRUTH_AND_EXPR:
2859 return build (TRUTH_OR_EXPR, type,
2860 invert_truthvalue (TREE_OPERAND (arg, 0)),
2861 invert_truthvalue (TREE_OPERAND (arg, 1)));
2863 case TRUTH_OR_EXPR:
2864 return build (TRUTH_AND_EXPR, type,
2865 invert_truthvalue (TREE_OPERAND (arg, 0)),
2866 invert_truthvalue (TREE_OPERAND (arg, 1)));
2868 case TRUTH_XOR_EXPR:
2869 /* Here we can invert either operand. We invert the first operand
2870 unless the second operand is a TRUTH_NOT_EXPR in which case our
2871 result is the XOR of the first operand with the inside of the
2872 negation of the second operand. */
2874 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2875 return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2876 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2877 else
2878 return build (TRUTH_XOR_EXPR, type,
2879 invert_truthvalue (TREE_OPERAND (arg, 0)),
2880 TREE_OPERAND (arg, 1));
2882 case TRUTH_ANDIF_EXPR:
2883 return build (TRUTH_ORIF_EXPR, type,
2884 invert_truthvalue (TREE_OPERAND (arg, 0)),
2885 invert_truthvalue (TREE_OPERAND (arg, 1)));
2887 case TRUTH_ORIF_EXPR:
2888 return build (TRUTH_ANDIF_EXPR, type,
2889 invert_truthvalue (TREE_OPERAND (arg, 0)),
2890 invert_truthvalue (TREE_OPERAND (arg, 1)));
2892 case TRUTH_NOT_EXPR:
2893 return TREE_OPERAND (arg, 0);
2895 case COND_EXPR:
2896 return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2897 invert_truthvalue (TREE_OPERAND (arg, 1)),
2898 invert_truthvalue (TREE_OPERAND (arg, 2)));
2900 case COMPOUND_EXPR:
2901 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2902 invert_truthvalue (TREE_OPERAND (arg, 1)));
2904 case WITH_RECORD_EXPR:
2905 return build (WITH_RECORD_EXPR, type,
2906 invert_truthvalue (TREE_OPERAND (arg, 0)),
2907 TREE_OPERAND (arg, 1));
2909 case NON_LVALUE_EXPR:
2910 return invert_truthvalue (TREE_OPERAND (arg, 0));
2912 case NOP_EXPR:
2913 case CONVERT_EXPR:
2914 case FLOAT_EXPR:
2915 return build1 (TREE_CODE (arg), type,
2916 invert_truthvalue (TREE_OPERAND (arg, 0)));
2918 case BIT_AND_EXPR:
2919 if (!integer_onep (TREE_OPERAND (arg, 1)))
2920 break;
2921 return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
2923 case SAVE_EXPR:
2924 return build1 (TRUTH_NOT_EXPR, type, arg);
2926 case CLEANUP_POINT_EXPR:
2927 return build1 (CLEANUP_POINT_EXPR, type,
2928 invert_truthvalue (TREE_OPERAND (arg, 0)));
2930 default:
2931 break;
2933 if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
2934 abort ();
2935 return build1 (TRUTH_NOT_EXPR, type, arg);
2938 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2939 operands are another bit-wise operation with a common input. If so,
2940 distribute the bit operations to save an operation and possibly two if
2941 constants are involved. For example, convert
2942 (A | B) & (A | C) into A | (B & C)
2943 Further simplification will occur if B and C are constants.
2945 If this optimization cannot be done, 0 will be returned. */
2947 static tree
2948 distribute_bit_expr (code, type, arg0, arg1)
2949 enum tree_code code;
2950 tree type;
2951 tree arg0, arg1;
2953 tree common;
2954 tree left, right;
2956 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2957 || TREE_CODE (arg0) == code
2958 || (TREE_CODE (arg0) != BIT_AND_EXPR
2959 && TREE_CODE (arg0) != BIT_IOR_EXPR))
2960 return 0;
2962 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2964 common = TREE_OPERAND (arg0, 0);
2965 left = TREE_OPERAND (arg0, 1);
2966 right = TREE_OPERAND (arg1, 1);
2968 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2970 common = TREE_OPERAND (arg0, 0);
2971 left = TREE_OPERAND (arg0, 1);
2972 right = TREE_OPERAND (arg1, 0);
2974 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2976 common = TREE_OPERAND (arg0, 1);
2977 left = TREE_OPERAND (arg0, 0);
2978 right = TREE_OPERAND (arg1, 1);
2980 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2982 common = TREE_OPERAND (arg0, 1);
2983 left = TREE_OPERAND (arg0, 0);
2984 right = TREE_OPERAND (arg1, 0);
2986 else
2987 return 0;
2989 return fold (build (TREE_CODE (arg0), type, common,
2990 fold (build (code, type, left, right))));
2993 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2994 starting at BITPOS. The field is unsigned if UNSIGNEDP is non-zero. */
2996 static tree
2997 make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
2998 tree inner;
2999 tree type;
3000 int bitsize, bitpos;
3001 int unsignedp;
3003 tree result = build (BIT_FIELD_REF, type, inner,
3004 size_int (bitsize), bitsize_int (bitpos));
3006 TREE_UNSIGNED (result) = unsignedp;
3008 return result;
3011 /* Optimize a bit-field compare.
3013 There are two cases: First is a compare against a constant and the
3014 second is a comparison of two items where the fields are at the same
3015 bit position relative to the start of a chunk (byte, halfword, word)
3016 large enough to contain it. In these cases we can avoid the shift
3017 implicit in bitfield extractions.
3019 For constants, we emit a compare of the shifted constant with the
3020 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3021 compared. For two fields at the same position, we do the ANDs with the
3022 similar mask and compare the result of the ANDs.
3024 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3025 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3026 are the left and right operands of the comparison, respectively.
3028 If the optimization described above can be done, we return the resulting
3029 tree. Otherwise we return zero. */
3031 static tree
3032 optimize_bit_field_compare (code, compare_type, lhs, rhs)
3033 enum tree_code code;
3034 tree compare_type;
3035 tree lhs, rhs;
3037 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3038 tree type = TREE_TYPE (lhs);
3039 tree signed_type, unsigned_type;
3040 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3041 enum machine_mode lmode, rmode, nmode;
3042 int lunsignedp, runsignedp;
3043 int lvolatilep = 0, rvolatilep = 0;
3044 tree linner, rinner = NULL_TREE;
3045 tree mask;
3046 tree offset;
3048 /* Get all the information about the extractions being done. If the bit size
3049 if the same as the size of the underlying object, we aren't doing an
3050 extraction at all and so can do nothing. We also don't want to
3051 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3052 then will no longer be able to replace it. */
3053 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3054 &lunsignedp, &lvolatilep);
3055 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3056 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3057 return 0;
3059 if (!const_p)
3061 /* If this is not a constant, we can only do something if bit positions,
3062 sizes, and signedness are the same. */
3063 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3064 &runsignedp, &rvolatilep);
3066 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3067 || lunsignedp != runsignedp || offset != 0
3068 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3069 return 0;
3072 /* See if we can find a mode to refer to this field. We should be able to,
3073 but fail if we can't. */
3074 nmode = get_best_mode (lbitsize, lbitpos,
3075 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3076 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3077 TYPE_ALIGN (TREE_TYPE (rinner))),
3078 word_mode, lvolatilep || rvolatilep);
3079 if (nmode == VOIDmode)
3080 return 0;
3082 /* Set signed and unsigned types of the precision of this mode for the
3083 shifts below. */
3084 signed_type = type_for_mode (nmode, 0);
3085 unsigned_type = type_for_mode (nmode, 1);
3087 /* Compute the bit position and size for the new reference and our offset
3088 within it. If the new reference is the same size as the original, we
3089 won't optimize anything, so return zero. */
3090 nbitsize = GET_MODE_BITSIZE (nmode);
3091 nbitpos = lbitpos & ~ (nbitsize - 1);
3092 lbitpos -= nbitpos;
3093 if (nbitsize == lbitsize)
3094 return 0;
3096 if (BYTES_BIG_ENDIAN)
3097 lbitpos = nbitsize - lbitsize - lbitpos;
3099 /* Make the mask to be used against the extracted field. */
3100 mask = build_int_2 (~0, ~0);
3101 TREE_TYPE (mask) = unsigned_type;
3102 force_fit_type (mask, 0);
3103 mask = convert (unsigned_type, mask);
3104 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3105 mask = const_binop (RSHIFT_EXPR, mask,
3106 size_int (nbitsize - lbitsize - lbitpos), 0);
3108 if (! const_p)
3109 /* If not comparing with constant, just rework the comparison
3110 and return. */
3111 return build (code, compare_type,
3112 build (BIT_AND_EXPR, unsigned_type,
3113 make_bit_field_ref (linner, unsigned_type,
3114 nbitsize, nbitpos, 1),
3115 mask),
3116 build (BIT_AND_EXPR, unsigned_type,
3117 make_bit_field_ref (rinner, unsigned_type,
3118 nbitsize, nbitpos, 1),
3119 mask));
3121 /* Otherwise, we are handling the constant case. See if the constant is too
3122 big for the field. Warn and return a tree of for 0 (false) if so. We do
3123 this not only for its own sake, but to avoid having to test for this
3124 error case below. If we didn't, we might generate wrong code.
3126 For unsigned fields, the constant shifted right by the field length should
3127 be all zero. For signed fields, the high-order bits should agree with
3128 the sign bit. */
3130 if (lunsignedp)
3132 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3133 convert (unsigned_type, rhs),
3134 size_int (lbitsize), 0)))
3136 warning ("comparison is always %d due to width of bit-field",
3137 code == NE_EXPR);
3138 return convert (compare_type,
3139 (code == NE_EXPR
3140 ? integer_one_node : integer_zero_node));
3143 else
3145 tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
3146 size_int (lbitsize - 1), 0);
3147 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3149 warning ("comparison is always %d due to width of bit-field",
3150 code == NE_EXPR);
3151 return convert (compare_type,
3152 (code == NE_EXPR
3153 ? integer_one_node : integer_zero_node));
3157 /* Single-bit compares should always be against zero. */
3158 if (lbitsize == 1 && ! integer_zerop (rhs))
3160 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3161 rhs = convert (type, integer_zero_node);
3164 /* Make a new bitfield reference, shift the constant over the
3165 appropriate number of bits and mask it with the computed mask
3166 (in case this was a signed field). If we changed it, make a new one. */
3167 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
3168 if (lvolatilep)
3170 TREE_SIDE_EFFECTS (lhs) = 1;
3171 TREE_THIS_VOLATILE (lhs) = 1;
3174 rhs = fold (const_binop (BIT_AND_EXPR,
3175 const_binop (LSHIFT_EXPR,
3176 convert (unsigned_type, rhs),
3177 size_int (lbitpos), 0),
3178 mask, 0));
3180 return build (code, compare_type,
3181 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
3182 rhs);
3185 /* Subroutine for fold_truthop: decode a field reference.
3187 If EXP is a comparison reference, we return the innermost reference.
3189 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3190 set to the starting bit number.
3192 If the innermost field can be completely contained in a mode-sized
3193 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3195 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3196 otherwise it is not changed.
3198 *PUNSIGNEDP is set to the signedness of the field.
3200 *PMASK is set to the mask used. This is either contained in a
3201 BIT_AND_EXPR or derived from the width of the field.
3203 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3205 Return 0 if this is not a component reference or is one that we can't
3206 do anything with. */
3208 static tree
3209 decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
3210 pvolatilep, pmask, pand_mask)
3211 tree exp;
3212 HOST_WIDE_INT *pbitsize, *pbitpos;
3213 enum machine_mode *pmode;
3214 int *punsignedp, *pvolatilep;
3215 tree *pmask;
3216 tree *pand_mask;
3218 tree and_mask = 0;
3219 tree mask, inner, offset;
3220 tree unsigned_type;
3221 unsigned int precision;
3223 /* All the optimizations using this function assume integer fields.
3224 There are problems with FP fields since the type_for_size call
3225 below can fail for, e.g., XFmode. */
3226 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3227 return 0;
3229 STRIP_NOPS (exp);
3231 if (TREE_CODE (exp) == BIT_AND_EXPR)
3233 and_mask = TREE_OPERAND (exp, 1);
3234 exp = TREE_OPERAND (exp, 0);
3235 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3236 if (TREE_CODE (and_mask) != INTEGER_CST)
3237 return 0;
3240 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3241 punsignedp, pvolatilep);
3242 if ((inner == exp && and_mask == 0)
3243 || *pbitsize < 0 || offset != 0
3244 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3245 return 0;
3247 /* Compute the mask to access the bitfield. */
3248 unsigned_type = type_for_size (*pbitsize, 1);
3249 precision = TYPE_PRECISION (unsigned_type);
3251 mask = build_int_2 (~0, ~0);
3252 TREE_TYPE (mask) = unsigned_type;
3253 force_fit_type (mask, 0);
3254 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3255 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3257 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3258 if (and_mask != 0)
3259 mask = fold (build (BIT_AND_EXPR, unsigned_type,
3260 convert (unsigned_type, and_mask), mask));
3262 *pmask = mask;
3263 *pand_mask = and_mask;
3264 return inner;
3267 /* Return non-zero if MASK represents a mask of SIZE ones in the low-order
3268 bit positions. */
3270 static int
3271 all_ones_mask_p (mask, size)
3272 tree mask;
3273 int size;
3275 tree type = TREE_TYPE (mask);
3276 unsigned int precision = TYPE_PRECISION (type);
3277 tree tmask;
3279 tmask = build_int_2 (~0, ~0);
3280 TREE_TYPE (tmask) = signed_type (type);
3281 force_fit_type (tmask, 0);
3282 return
3283 tree_int_cst_equal (mask,
3284 const_binop (RSHIFT_EXPR,
3285 const_binop (LSHIFT_EXPR, tmask,
3286 size_int (precision - size),
3288 size_int (precision - size), 0));
3291 /* Subroutine for fold_truthop: determine if an operand is simple enough
3292 to be evaluated unconditionally. */
3294 static int
3295 simple_operand_p (exp)
3296 tree exp;
3298 /* Strip any conversions that don't change the machine mode. */
3299 while ((TREE_CODE (exp) == NOP_EXPR
3300 || TREE_CODE (exp) == CONVERT_EXPR)
3301 && (TYPE_MODE (TREE_TYPE (exp))
3302 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
3303 exp = TREE_OPERAND (exp, 0);
3305 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
3306 || (DECL_P (exp)
3307 && ! TREE_ADDRESSABLE (exp)
3308 && ! TREE_THIS_VOLATILE (exp)
3309 && ! DECL_NONLOCAL (exp)
3310 /* Don't regard global variables as simple. They may be
3311 allocated in ways unknown to the compiler (shared memory,
3312 #pragma weak, etc). */
3313 && ! TREE_PUBLIC (exp)
3314 && ! DECL_EXTERNAL (exp)
3315 /* Loading a static variable is unduly expensive, but global
3316 registers aren't expensive. */
3317 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3320 /* The following functions are subroutines to fold_range_test and allow it to
3321 try to change a logical combination of comparisons into a range test.
3323 For example, both
3324 X == 2 || X == 3 || X == 4 || X == 5
3326 X >= 2 && X <= 5
3327 are converted to
3328 (unsigned) (X - 2) <= 3
3330 We describe each set of comparisons as being either inside or outside
3331 a range, using a variable named like IN_P, and then describe the
3332 range with a lower and upper bound. If one of the bounds is omitted,
3333 it represents either the highest or lowest value of the type.
3335 In the comments below, we represent a range by two numbers in brackets
3336 preceded by a "+" to designate being inside that range, or a "-" to
3337 designate being outside that range, so the condition can be inverted by
3338 flipping the prefix. An omitted bound is represented by a "-". For
3339 example, "- [-, 10]" means being outside the range starting at the lowest
3340 possible value and ending at 10, in other words, being greater than 10.
3341 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3342 always false.
3344 We set up things so that the missing bounds are handled in a consistent
3345 manner so neither a missing bound nor "true" and "false" need to be
3346 handled using a special case. */
3348 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3349 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3350 and UPPER1_P are nonzero if the respective argument is an upper bound
3351 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3352 must be specified for a comparison. ARG1 will be converted to ARG0's
3353 type if both are specified. */
3355 static tree
3356 range_binop (code, type, arg0, upper0_p, arg1, upper1_p)
3357 enum tree_code code;
3358 tree type;
3359 tree arg0, arg1;
3360 int upper0_p, upper1_p;
3362 tree tem;
3363 int result;
3364 int sgn0, sgn1;
3366 /* If neither arg represents infinity, do the normal operation.
3367 Else, if not a comparison, return infinity. Else handle the special
3368 comparison rules. Note that most of the cases below won't occur, but
3369 are handled for consistency. */
3371 if (arg0 != 0 && arg1 != 0)
3373 tem = fold (build (code, type != 0 ? type : TREE_TYPE (arg0),
3374 arg0, convert (TREE_TYPE (arg0), arg1)));
3375 STRIP_NOPS (tem);
3376 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3379 if (TREE_CODE_CLASS (code) != '<')
3380 return 0;
3382 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3383 for neither. In real maths, we cannot assume open ended ranges are
3384 the same. But, this is computer arithmetic, where numbers are finite.
3385 We can therefore make the transformation of any unbounded range with
3386 the value Z, Z being greater than any representable number. This permits
3387 us to treat unbounded ranges as equal. */
3388 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3389 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3390 switch (code)
3392 case EQ_EXPR:
3393 result = sgn0 == sgn1;
3394 break;
3395 case NE_EXPR:
3396 result = sgn0 != sgn1;
3397 break;
3398 case LT_EXPR:
3399 result = sgn0 < sgn1;
3400 break;
3401 case LE_EXPR:
3402 result = sgn0 <= sgn1;
3403 break;
3404 case GT_EXPR:
3405 result = sgn0 > sgn1;
3406 break;
3407 case GE_EXPR:
3408 result = sgn0 >= sgn1;
3409 break;
3410 default:
3411 abort ();
3414 return convert (type, result ? integer_one_node : integer_zero_node);
3417 /* Given EXP, a logical expression, set the range it is testing into
3418 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3419 actually being tested. *PLOW and *PHIGH will be made of the same type
3420 as the returned expression. If EXP is not a comparison, we will most
3421 likely not be returning a useful value and range. */
3423 static tree
3424 make_range (exp, pin_p, plow, phigh)
3425 tree exp;
3426 int *pin_p;
3427 tree *plow, *phigh;
3429 enum tree_code code;
3430 tree arg0 = NULL_TREE, arg1 = NULL_TREE, type = NULL_TREE;
3431 tree orig_type = NULL_TREE;
3432 int in_p, n_in_p;
3433 tree low, high, n_low, n_high;
3435 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3436 and see if we can refine the range. Some of the cases below may not
3437 happen, but it doesn't seem worth worrying about this. We "continue"
3438 the outer loop when we've changed something; otherwise we "break"
3439 the switch, which will "break" the while. */
3441 in_p = 0, low = high = convert (TREE_TYPE (exp), integer_zero_node);
3443 while (1)
3445 code = TREE_CODE (exp);
3447 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3449 arg0 = TREE_OPERAND (exp, 0);
3450 if (TREE_CODE_CLASS (code) == '<'
3451 || TREE_CODE_CLASS (code) == '1'
3452 || TREE_CODE_CLASS (code) == '2')
3453 type = TREE_TYPE (arg0);
3454 if (TREE_CODE_CLASS (code) == '2'
3455 || TREE_CODE_CLASS (code) == '<'
3456 || (TREE_CODE_CLASS (code) == 'e'
3457 && TREE_CODE_LENGTH (code) > 1))
3458 arg1 = TREE_OPERAND (exp, 1);
3461 /* Set ORIG_TYPE as soon as TYPE is non-null so that we do not
3462 lose a cast by accident. */
3463 if (type != NULL_TREE && orig_type == NULL_TREE)
3464 orig_type = type;
3466 switch (code)
3468 case TRUTH_NOT_EXPR:
3469 in_p = ! in_p, exp = arg0;
3470 continue;
3472 case EQ_EXPR: case NE_EXPR:
3473 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3474 /* We can only do something if the range is testing for zero
3475 and if the second operand is an integer constant. Note that
3476 saying something is "in" the range we make is done by
3477 complementing IN_P since it will set in the initial case of
3478 being not equal to zero; "out" is leaving it alone. */
3479 if (low == 0 || high == 0
3480 || ! integer_zerop (low) || ! integer_zerop (high)
3481 || TREE_CODE (arg1) != INTEGER_CST)
3482 break;
3484 switch (code)
3486 case NE_EXPR: /* - [c, c] */
3487 low = high = arg1;
3488 break;
3489 case EQ_EXPR: /* + [c, c] */
3490 in_p = ! in_p, low = high = arg1;
3491 break;
3492 case GT_EXPR: /* - [-, c] */
3493 low = 0, high = arg1;
3494 break;
3495 case GE_EXPR: /* + [c, -] */
3496 in_p = ! in_p, low = arg1, high = 0;
3497 break;
3498 case LT_EXPR: /* - [c, -] */
3499 low = arg1, high = 0;
3500 break;
3501 case LE_EXPR: /* + [-, c] */
3502 in_p = ! in_p, low = 0, high = arg1;
3503 break;
3504 default:
3505 abort ();
3508 exp = arg0;
3510 /* If this is an unsigned comparison, we also know that EXP is
3511 greater than or equal to zero. We base the range tests we make
3512 on that fact, so we record it here so we can parse existing
3513 range tests. */
3514 if (TREE_UNSIGNED (type) && (low == 0 || high == 0))
3516 if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
3517 1, convert (type, integer_zero_node),
3518 NULL_TREE))
3519 break;
3521 in_p = n_in_p, low = n_low, high = n_high;
3523 /* If the high bound is missing, but we
3524 have a low bound, reverse the range so
3525 it goes from zero to the low bound minus 1. */
3526 if (high == 0 && low)
3528 in_p = ! in_p;
3529 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3530 integer_one_node, 0);
3531 low = convert (type, integer_zero_node);
3534 continue;
3536 case NEGATE_EXPR:
3537 /* (-x) IN [a,b] -> x in [-b, -a] */
3538 n_low = range_binop (MINUS_EXPR, type,
3539 convert (type, integer_zero_node), 0, high, 1);
3540 n_high = range_binop (MINUS_EXPR, type,
3541 convert (type, integer_zero_node), 0, low, 0);
3542 low = n_low, high = n_high;
3543 exp = arg0;
3544 continue;
3546 case BIT_NOT_EXPR:
3547 /* ~ X -> -X - 1 */
3548 exp = build (MINUS_EXPR, type, negate_expr (arg0),
3549 convert (type, integer_one_node));
3550 continue;
3552 case PLUS_EXPR: case MINUS_EXPR:
3553 if (TREE_CODE (arg1) != INTEGER_CST)
3554 break;
3556 /* If EXP is signed, any overflow in the computation is undefined,
3557 so we don't worry about it so long as our computations on
3558 the bounds don't overflow. For unsigned, overflow is defined
3559 and this is exactly the right thing. */
3560 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3561 type, low, 0, arg1, 0);
3562 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3563 type, high, 1, arg1, 0);
3564 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3565 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3566 break;
3568 /* Check for an unsigned range which has wrapped around the maximum
3569 value thus making n_high < n_low, and normalize it. */
3570 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3572 low = range_binop (PLUS_EXPR, type, n_high, 0,
3573 integer_one_node, 0);
3574 high = range_binop (MINUS_EXPR, type, n_low, 0,
3575 integer_one_node, 0);
3577 /* If the range is of the form +/- [ x+1, x ], we won't
3578 be able to normalize it. But then, it represents the
3579 whole range or the empty set, so make it
3580 +/- [ -, - ]. */
3581 if (tree_int_cst_equal (n_low, low)
3582 && tree_int_cst_equal (n_high, high))
3583 low = high = 0;
3584 else
3585 in_p = ! in_p;
3587 else
3588 low = n_low, high = n_high;
3590 exp = arg0;
3591 continue;
3593 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
3594 if (TYPE_PRECISION (type) > TYPE_PRECISION (orig_type))
3595 break;
3597 if (! INTEGRAL_TYPE_P (type)
3598 || (low != 0 && ! int_fits_type_p (low, type))
3599 || (high != 0 && ! int_fits_type_p (high, type)))
3600 break;
3602 n_low = low, n_high = high;
3604 if (n_low != 0)
3605 n_low = convert (type, n_low);
3607 if (n_high != 0)
3608 n_high = convert (type, n_high);
3610 /* If we're converting from an unsigned to a signed type,
3611 we will be doing the comparison as unsigned. The tests above
3612 have already verified that LOW and HIGH are both positive.
3614 So we have to make sure that the original unsigned value will
3615 be interpreted as positive. */
3616 if (TREE_UNSIGNED (type) && ! TREE_UNSIGNED (TREE_TYPE (exp)))
3618 tree equiv_type = type_for_mode (TYPE_MODE (type), 1);
3619 tree high_positive;
3621 /* A range without an upper bound is, naturally, unbounded.
3622 Since convert would have cropped a very large value, use
3623 the max value for the destination type. */
3624 high_positive
3625 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3626 : TYPE_MAX_VALUE (type);
3628 high_positive = fold (build (RSHIFT_EXPR, type,
3629 convert (type, high_positive),
3630 convert (type, integer_one_node)));
3632 /* If the low bound is specified, "and" the range with the
3633 range for which the original unsigned value will be
3634 positive. */
3635 if (low != 0)
3637 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3638 1, n_low, n_high,
3639 1, convert (type, integer_zero_node),
3640 high_positive))
3641 break;
3643 in_p = (n_in_p == in_p);
3645 else
3647 /* Otherwise, "or" the range with the range of the input
3648 that will be interpreted as negative. */
3649 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3650 0, n_low, n_high,
3651 1, convert (type, integer_zero_node),
3652 high_positive))
3653 break;
3655 in_p = (in_p != n_in_p);
3659 exp = arg0;
3660 low = n_low, high = n_high;
3661 continue;
3663 default:
3664 break;
3667 break;
3670 /* If EXP is a constant, we can evaluate whether this is true or false. */
3671 if (TREE_CODE (exp) == INTEGER_CST)
3673 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3674 exp, 0, low, 0))
3675 && integer_onep (range_binop (LE_EXPR, integer_type_node,
3676 exp, 1, high, 1)));
3677 low = high = 0;
3678 exp = 0;
3681 *pin_p = in_p, *plow = low, *phigh = high;
3682 return exp;
3685 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3686 type, TYPE, return an expression to test if EXP is in (or out of, depending
3687 on IN_P) the range. */
3689 static tree
3690 build_range_check (type, exp, in_p, low, high)
3691 tree type;
3692 tree exp;
3693 int in_p;
3694 tree low, high;
3696 tree etype = TREE_TYPE (exp);
3697 tree utype, value;
3699 if (! in_p
3700 && (0 != (value = build_range_check (type, exp, 1, low, high))))
3701 return invert_truthvalue (value);
3703 else if (low == 0 && high == 0)
3704 return convert (type, integer_one_node);
3706 else if (low == 0)
3707 return fold (build (LE_EXPR, type, exp, high));
3709 else if (high == 0)
3710 return fold (build (GE_EXPR, type, exp, low));
3712 else if (operand_equal_p (low, high, 0))
3713 return fold (build (EQ_EXPR, type, exp, low));
3715 else if (TREE_UNSIGNED (etype) && integer_zerop (low))
3716 return build_range_check (type, exp, 1, 0, high);
3718 else if (integer_zerop (low))
3720 utype = unsigned_type (etype);
3721 return build_range_check (type, convert (utype, exp), 1, 0,
3722 convert (utype, high));
3725 else if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
3726 && ! TREE_OVERFLOW (value))
3727 return build_range_check (type,
3728 fold (build (MINUS_EXPR, etype, exp, low)),
3729 1, convert (etype, integer_zero_node), value);
3730 else
3731 return 0;
3734 /* Given two ranges, see if we can merge them into one. Return 1 if we
3735 can, 0 if we can't. Set the output range into the specified parameters. */
3737 static int
3738 merge_ranges (pin_p, plow, phigh, in0_p, low0, high0, in1_p, low1, high1)
3739 int *pin_p;
3740 tree *plow, *phigh;
3741 int in0_p, in1_p;
3742 tree low0, high0, low1, high1;
3744 int no_overlap;
3745 int subset;
3746 int temp;
3747 tree tem;
3748 int in_p;
3749 tree low, high;
3750 int lowequal = ((low0 == 0 && low1 == 0)
3751 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3752 low0, 0, low1, 0)));
3753 int highequal = ((high0 == 0 && high1 == 0)
3754 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3755 high0, 1, high1, 1)));
3757 /* Make range 0 be the range that starts first, or ends last if they
3758 start at the same value. Swap them if it isn't. */
3759 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3760 low0, 0, low1, 0))
3761 || (lowequal
3762 && integer_onep (range_binop (GT_EXPR, integer_type_node,
3763 high1, 1, high0, 1))))
3765 temp = in0_p, in0_p = in1_p, in1_p = temp;
3766 tem = low0, low0 = low1, low1 = tem;
3767 tem = high0, high0 = high1, high1 = tem;
3770 /* Now flag two cases, whether the ranges are disjoint or whether the
3771 second range is totally subsumed in the first. Note that the tests
3772 below are simplified by the ones above. */
3773 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3774 high0, 1, low1, 0));
3775 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3776 high1, 1, high0, 1));
3778 /* We now have four cases, depending on whether we are including or
3779 excluding the two ranges. */
3780 if (in0_p && in1_p)
3782 /* If they don't overlap, the result is false. If the second range
3783 is a subset it is the result. Otherwise, the range is from the start
3784 of the second to the end of the first. */
3785 if (no_overlap)
3786 in_p = 0, low = high = 0;
3787 else if (subset)
3788 in_p = 1, low = low1, high = high1;
3789 else
3790 in_p = 1, low = low1, high = high0;
3793 else if (in0_p && ! in1_p)
3795 /* If they don't overlap, the result is the first range. If they are
3796 equal, the result is false. If the second range is a subset of the
3797 first, and the ranges begin at the same place, we go from just after
3798 the end of the first range to the end of the second. If the second
3799 range is not a subset of the first, or if it is a subset and both
3800 ranges end at the same place, the range starts at the start of the
3801 first range and ends just before the second range.
3802 Otherwise, we can't describe this as a single range. */
3803 if (no_overlap)
3804 in_p = 1, low = low0, high = high0;
3805 else if (lowequal && highequal)
3806 in_p = 0, low = high = 0;
3807 else if (subset && lowequal)
3809 in_p = 1, high = high0;
3810 low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
3811 integer_one_node, 0);
3813 else if (! subset || highequal)
3815 in_p = 1, low = low0;
3816 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
3817 integer_one_node, 0);
3819 else
3820 return 0;
3823 else if (! in0_p && in1_p)
3825 /* If they don't overlap, the result is the second range. If the second
3826 is a subset of the first, the result is false. Otherwise,
3827 the range starts just after the first range and ends at the
3828 end of the second. */
3829 if (no_overlap)
3830 in_p = 1, low = low1, high = high1;
3831 else if (subset || highequal)
3832 in_p = 0, low = high = 0;
3833 else
3835 in_p = 1, high = high1;
3836 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3837 integer_one_node, 0);
3841 else
3843 /* The case where we are excluding both ranges. Here the complex case
3844 is if they don't overlap. In that case, the only time we have a
3845 range is if they are adjacent. If the second is a subset of the
3846 first, the result is the first. Otherwise, the range to exclude
3847 starts at the beginning of the first range and ends at the end of the
3848 second. */
3849 if (no_overlap)
3851 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3852 range_binop (PLUS_EXPR, NULL_TREE,
3853 high0, 1,
3854 integer_one_node, 1),
3855 1, low1, 0)))
3856 in_p = 0, low = low0, high = high1;
3857 else
3858 return 0;
3860 else if (subset)
3861 in_p = 0, low = low0, high = high0;
3862 else
3863 in_p = 0, low = low0, high = high1;
3866 *pin_p = in_p, *plow = low, *phigh = high;
3867 return 1;
3870 /* EXP is some logical combination of boolean tests. See if we can
3871 merge it into some range test. Return the new tree if so. */
3873 static tree
3874 fold_range_test (exp)
3875 tree exp;
3877 int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3878 || TREE_CODE (exp) == TRUTH_OR_EXPR);
3879 int in0_p, in1_p, in_p;
3880 tree low0, low1, low, high0, high1, high;
3881 tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
3882 tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
3883 tree tem;
3885 /* If this is an OR operation, invert both sides; we will invert
3886 again at the end. */
3887 if (or_op)
3888 in0_p = ! in0_p, in1_p = ! in1_p;
3890 /* If both expressions are the same, if we can merge the ranges, and we
3891 can build the range test, return it or it inverted. If one of the
3892 ranges is always true or always false, consider it to be the same
3893 expression as the other. */
3894 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
3895 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
3896 in1_p, low1, high1)
3897 && 0 != (tem = (build_range_check (TREE_TYPE (exp),
3898 lhs != 0 ? lhs
3899 : rhs != 0 ? rhs : integer_zero_node,
3900 in_p, low, high))))
3901 return or_op ? invert_truthvalue (tem) : tem;
3903 /* On machines where the branch cost is expensive, if this is a
3904 short-circuited branch and the underlying object on both sides
3905 is the same, make a non-short-circuit operation. */
3906 else if (BRANCH_COST >= 2
3907 && lhs != 0 && rhs != 0
3908 && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3909 || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
3910 && operand_equal_p (lhs, rhs, 0))
3912 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
3913 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
3914 which cases we can't do this. */
3915 if (simple_operand_p (lhs))
3916 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3917 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3918 TREE_TYPE (exp), TREE_OPERAND (exp, 0),
3919 TREE_OPERAND (exp, 1));
3921 else if (global_bindings_p () == 0
3922 && ! contains_placeholder_p (lhs))
3924 tree common = save_expr (lhs);
3926 if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
3927 or_op ? ! in0_p : in0_p,
3928 low0, high0))
3929 && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
3930 or_op ? ! in1_p : in1_p,
3931 low1, high1))))
3932 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3933 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3934 TREE_TYPE (exp), lhs, rhs);
3938 return 0;
3941 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
3942 bit value. Arrange things so the extra bits will be set to zero if and
3943 only if C is signed-extended to its full width. If MASK is nonzero,
3944 it is an INTEGER_CST that should be AND'ed with the extra bits. */
3946 static tree
3947 unextend (c, p, unsignedp, mask)
3948 tree c;
3949 int p;
3950 int unsignedp;
3951 tree mask;
3953 tree type = TREE_TYPE (c);
3954 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
3955 tree temp;
3957 if (p == modesize || unsignedp)
3958 return c;
3960 /* We work by getting just the sign bit into the low-order bit, then
3961 into the high-order bit, then sign-extend. We then XOR that value
3962 with C. */
3963 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
3964 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
3966 /* We must use a signed type in order to get an arithmetic right shift.
3967 However, we must also avoid introducing accidental overflows, so that
3968 a subsequent call to integer_zerop will work. Hence we must
3969 do the type conversion here. At this point, the constant is either
3970 zero or one, and the conversion to a signed type can never overflow.
3971 We could get an overflow if this conversion is done anywhere else. */
3972 if (TREE_UNSIGNED (type))
3973 temp = convert (signed_type (type), temp);
3975 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
3976 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
3977 if (mask != 0)
3978 temp = const_binop (BIT_AND_EXPR, temp, convert (TREE_TYPE (c), mask), 0);
3979 /* If necessary, convert the type back to match the type of C. */
3980 if (TREE_UNSIGNED (type))
3981 temp = convert (type, temp);
3983 return convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
3986 /* Find ways of folding logical expressions of LHS and RHS:
3987 Try to merge two comparisons to the same innermost item.
3988 Look for range tests like "ch >= '0' && ch <= '9'".
3989 Look for combinations of simple terms on machines with expensive branches
3990 and evaluate the RHS unconditionally.
3992 For example, if we have p->a == 2 && p->b == 4 and we can make an
3993 object large enough to span both A and B, we can do this with a comparison
3994 against the object ANDed with the a mask.
3996 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
3997 operations to do this with one comparison.
3999 We check for both normal comparisons and the BIT_AND_EXPRs made this by
4000 function and the one above.
4002 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
4003 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
4005 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
4006 two operands.
4008 We return the simplified tree or 0 if no optimization is possible. */
4010 static tree
4011 fold_truthop (code, truth_type, lhs, rhs)
4012 enum tree_code code;
4013 tree truth_type, lhs, rhs;
4015 /* If this is the "or" of two comparisons, we can do something if
4016 the comparisons are NE_EXPR. If this is the "and", we can do something
4017 if the comparisons are EQ_EXPR. I.e.,
4018 (a->b == 2 && a->c == 4) can become (a->new == NEW).
4020 WANTED_CODE is this operation code. For single bit fields, we can
4021 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
4022 comparison for one-bit fields. */
4024 enum tree_code wanted_code;
4025 enum tree_code lcode, rcode;
4026 tree ll_arg, lr_arg, rl_arg, rr_arg;
4027 tree ll_inner, lr_inner, rl_inner, rr_inner;
4028 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
4029 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
4030 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
4031 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
4032 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
4033 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
4034 enum machine_mode lnmode, rnmode;
4035 tree ll_mask, lr_mask, rl_mask, rr_mask;
4036 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
4037 tree l_const, r_const;
4038 tree lntype, rntype, result;
4039 int first_bit, end_bit;
4040 int volatilep;
4042 /* Start by getting the comparison codes. Fail if anything is volatile.
4043 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
4044 it were surrounded with a NE_EXPR. */
4046 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
4047 return 0;
4049 lcode = TREE_CODE (lhs);
4050 rcode = TREE_CODE (rhs);
4052 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
4053 lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
4055 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
4056 rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
4058 if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
4059 return 0;
4061 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
4062 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
4064 ll_arg = TREE_OPERAND (lhs, 0);
4065 lr_arg = TREE_OPERAND (lhs, 1);
4066 rl_arg = TREE_OPERAND (rhs, 0);
4067 rr_arg = TREE_OPERAND (rhs, 1);
4069 /* If the RHS can be evaluated unconditionally and its operands are
4070 simple, it wins to evaluate the RHS unconditionally on machines
4071 with expensive branches. In this case, this isn't a comparison
4072 that can be merged. Avoid doing this if the RHS is a floating-point
4073 comparison since those can trap. */
4075 if (BRANCH_COST >= 2
4076 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
4077 && simple_operand_p (rl_arg)
4078 && simple_operand_p (rr_arg))
4079 return build (code, truth_type, lhs, rhs);
4081 /* See if the comparisons can be merged. Then get all the parameters for
4082 each side. */
4084 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
4085 || (rcode != EQ_EXPR && rcode != NE_EXPR))
4086 return 0;
4088 volatilep = 0;
4089 ll_inner = decode_field_reference (ll_arg,
4090 &ll_bitsize, &ll_bitpos, &ll_mode,
4091 &ll_unsignedp, &volatilep, &ll_mask,
4092 &ll_and_mask);
4093 lr_inner = decode_field_reference (lr_arg,
4094 &lr_bitsize, &lr_bitpos, &lr_mode,
4095 &lr_unsignedp, &volatilep, &lr_mask,
4096 &lr_and_mask);
4097 rl_inner = decode_field_reference (rl_arg,
4098 &rl_bitsize, &rl_bitpos, &rl_mode,
4099 &rl_unsignedp, &volatilep, &rl_mask,
4100 &rl_and_mask);
4101 rr_inner = decode_field_reference (rr_arg,
4102 &rr_bitsize, &rr_bitpos, &rr_mode,
4103 &rr_unsignedp, &volatilep, &rr_mask,
4104 &rr_and_mask);
4106 /* It must be true that the inner operation on the lhs of each
4107 comparison must be the same if we are to be able to do anything.
4108 Then see if we have constants. If not, the same must be true for
4109 the rhs's. */
4110 if (volatilep || ll_inner == 0 || rl_inner == 0
4111 || ! operand_equal_p (ll_inner, rl_inner, 0))
4112 return 0;
4114 if (TREE_CODE (lr_arg) == INTEGER_CST
4115 && TREE_CODE (rr_arg) == INTEGER_CST)
4116 l_const = lr_arg, r_const = rr_arg;
4117 else if (lr_inner == 0 || rr_inner == 0
4118 || ! operand_equal_p (lr_inner, rr_inner, 0))
4119 return 0;
4120 else
4121 l_const = r_const = 0;
4123 /* If either comparison code is not correct for our logical operation,
4124 fail. However, we can convert a one-bit comparison against zero into
4125 the opposite comparison against that bit being set in the field. */
4127 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
4128 if (lcode != wanted_code)
4130 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
4132 /* Make the left operand unsigned, since we are only interested
4133 in the value of one bit. Otherwise we are doing the wrong
4134 thing below. */
4135 ll_unsignedp = 1;
4136 l_const = ll_mask;
4138 else
4139 return 0;
4142 /* This is analogous to the code for l_const above. */
4143 if (rcode != wanted_code)
4145 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
4147 rl_unsignedp = 1;
4148 r_const = rl_mask;
4150 else
4151 return 0;
4154 /* See if we can find a mode that contains both fields being compared on
4155 the left. If we can't, fail. Otherwise, update all constants and masks
4156 to be relative to a field of that size. */
4157 first_bit = MIN (ll_bitpos, rl_bitpos);
4158 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4159 lnmode = get_best_mode (end_bit - first_bit, first_bit,
4160 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4161 volatilep);
4162 if (lnmode == VOIDmode)
4163 return 0;
4165 lnbitsize = GET_MODE_BITSIZE (lnmode);
4166 lnbitpos = first_bit & ~ (lnbitsize - 1);
4167 lntype = type_for_size (lnbitsize, 1);
4168 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
4170 if (BYTES_BIG_ENDIAN)
4172 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
4173 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
4176 ll_mask = const_binop (LSHIFT_EXPR, convert (lntype, ll_mask),
4177 size_int (xll_bitpos), 0);
4178 rl_mask = const_binop (LSHIFT_EXPR, convert (lntype, rl_mask),
4179 size_int (xrl_bitpos), 0);
4181 if (l_const)
4183 l_const = convert (lntype, l_const);
4184 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
4185 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
4186 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
4187 fold (build1 (BIT_NOT_EXPR,
4188 lntype, ll_mask)),
4189 0)))
4191 warning ("comparison is always %d", wanted_code == NE_EXPR);
4193 return convert (truth_type,
4194 wanted_code == NE_EXPR
4195 ? integer_one_node : integer_zero_node);
4198 if (r_const)
4200 r_const = convert (lntype, r_const);
4201 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
4202 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
4203 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4204 fold (build1 (BIT_NOT_EXPR,
4205 lntype, rl_mask)),
4206 0)))
4208 warning ("comparison is always %d", wanted_code == NE_EXPR);
4210 return convert (truth_type,
4211 wanted_code == NE_EXPR
4212 ? integer_one_node : integer_zero_node);
4216 /* If the right sides are not constant, do the same for it. Also,
4217 disallow this optimization if a size or signedness mismatch occurs
4218 between the left and right sides. */
4219 if (l_const == 0)
4221 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
4222 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4223 /* Make sure the two fields on the right
4224 correspond to the left without being swapped. */
4225 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
4226 return 0;
4228 first_bit = MIN (lr_bitpos, rr_bitpos);
4229 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4230 rnmode = get_best_mode (end_bit - first_bit, first_bit,
4231 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4232 volatilep);
4233 if (rnmode == VOIDmode)
4234 return 0;
4236 rnbitsize = GET_MODE_BITSIZE (rnmode);
4237 rnbitpos = first_bit & ~ (rnbitsize - 1);
4238 rntype = type_for_size (rnbitsize, 1);
4239 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4241 if (BYTES_BIG_ENDIAN)
4243 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4244 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4247 lr_mask = const_binop (LSHIFT_EXPR, convert (rntype, lr_mask),
4248 size_int (xlr_bitpos), 0);
4249 rr_mask = const_binop (LSHIFT_EXPR, convert (rntype, rr_mask),
4250 size_int (xrr_bitpos), 0);
4252 /* Make a mask that corresponds to both fields being compared.
4253 Do this for both items being compared. If the operands are the
4254 same size and the bits being compared are in the same position
4255 then we can do this by masking both and comparing the masked
4256 results. */
4257 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4258 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
4259 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
4261 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4262 ll_unsignedp || rl_unsignedp);
4263 if (! all_ones_mask_p (ll_mask, lnbitsize))
4264 lhs = build (BIT_AND_EXPR, lntype, lhs, ll_mask);
4266 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
4267 lr_unsignedp || rr_unsignedp);
4268 if (! all_ones_mask_p (lr_mask, rnbitsize))
4269 rhs = build (BIT_AND_EXPR, rntype, rhs, lr_mask);
4271 return build (wanted_code, truth_type, lhs, rhs);
4274 /* There is still another way we can do something: If both pairs of
4275 fields being compared are adjacent, we may be able to make a wider
4276 field containing them both.
4278 Note that we still must mask the lhs/rhs expressions. Furthermore,
4279 the mask must be shifted to account for the shift done by
4280 make_bit_field_ref. */
4281 if ((ll_bitsize + ll_bitpos == rl_bitpos
4282 && lr_bitsize + lr_bitpos == rr_bitpos)
4283 || (ll_bitpos == rl_bitpos + rl_bitsize
4284 && lr_bitpos == rr_bitpos + rr_bitsize))
4286 tree type;
4288 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
4289 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
4290 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4291 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4293 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4294 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
4295 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4296 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4298 /* Convert to the smaller type before masking out unwanted bits. */
4299 type = lntype;
4300 if (lntype != rntype)
4302 if (lnbitsize > rnbitsize)
4304 lhs = convert (rntype, lhs);
4305 ll_mask = convert (rntype, ll_mask);
4306 type = rntype;
4308 else if (lnbitsize < rnbitsize)
4310 rhs = convert (lntype, rhs);
4311 lr_mask = convert (lntype, lr_mask);
4312 type = lntype;
4316 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
4317 lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
4319 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
4320 rhs = build (BIT_AND_EXPR, type, rhs, lr_mask);
4322 return build (wanted_code, truth_type, lhs, rhs);
4325 return 0;
4328 /* Handle the case of comparisons with constants. If there is something in
4329 common between the masks, those bits of the constants must be the same.
4330 If not, the condition is always false. Test for this to avoid generating
4331 incorrect code below. */
4332 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
4333 if (! integer_zerop (result)
4334 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
4335 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
4337 if (wanted_code == NE_EXPR)
4339 warning ("`or' of unmatched not-equal tests is always 1");
4340 return convert (truth_type, integer_one_node);
4342 else
4344 warning ("`and' of mutually exclusive equal-tests is always 0");
4345 return convert (truth_type, integer_zero_node);
4349 /* Construct the expression we will return. First get the component
4350 reference we will make. Unless the mask is all ones the width of
4351 that field, perform the mask operation. Then compare with the
4352 merged constant. */
4353 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4354 ll_unsignedp || rl_unsignedp);
4356 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4357 if (! all_ones_mask_p (ll_mask, lnbitsize))
4358 result = build (BIT_AND_EXPR, lntype, result, ll_mask);
4360 return build (wanted_code, truth_type, result,
4361 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
4364 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4365 constant. */
4367 static tree
4368 optimize_minmax_comparison (t)
4369 tree t;
4371 tree type = TREE_TYPE (t);
4372 tree arg0 = TREE_OPERAND (t, 0);
4373 enum tree_code op_code;
4374 tree comp_const = TREE_OPERAND (t, 1);
4375 tree minmax_const;
4376 int consts_equal, consts_lt;
4377 tree inner;
4379 STRIP_SIGN_NOPS (arg0);
4381 op_code = TREE_CODE (arg0);
4382 minmax_const = TREE_OPERAND (arg0, 1);
4383 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
4384 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4385 inner = TREE_OPERAND (arg0, 0);
4387 /* If something does not permit us to optimize, return the original tree. */
4388 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4389 || TREE_CODE (comp_const) != INTEGER_CST
4390 || TREE_CONSTANT_OVERFLOW (comp_const)
4391 || TREE_CODE (minmax_const) != INTEGER_CST
4392 || TREE_CONSTANT_OVERFLOW (minmax_const))
4393 return t;
4395 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4396 and GT_EXPR, doing the rest with recursive calls using logical
4397 simplifications. */
4398 switch (TREE_CODE (t))
4400 case NE_EXPR: case LT_EXPR: case LE_EXPR:
4401 return
4402 invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t)));
4404 case GE_EXPR:
4405 return
4406 fold (build (TRUTH_ORIF_EXPR, type,
4407 optimize_minmax_comparison
4408 (build (EQ_EXPR, type, arg0, comp_const)),
4409 optimize_minmax_comparison
4410 (build (GT_EXPR, type, arg0, comp_const))));
4412 case EQ_EXPR:
4413 if (op_code == MAX_EXPR && consts_equal)
4414 /* MAX (X, 0) == 0 -> X <= 0 */
4415 return fold (build (LE_EXPR, type, inner, comp_const));
4417 else if (op_code == MAX_EXPR && consts_lt)
4418 /* MAX (X, 0) == 5 -> X == 5 */
4419 return fold (build (EQ_EXPR, type, inner, comp_const));
4421 else if (op_code == MAX_EXPR)
4422 /* MAX (X, 0) == -1 -> false */
4423 return omit_one_operand (type, integer_zero_node, inner);
4425 else if (consts_equal)
4426 /* MIN (X, 0) == 0 -> X >= 0 */
4427 return fold (build (GE_EXPR, type, inner, comp_const));
4429 else if (consts_lt)
4430 /* MIN (X, 0) == 5 -> false */
4431 return omit_one_operand (type, integer_zero_node, inner);
4433 else
4434 /* MIN (X, 0) == -1 -> X == -1 */
4435 return fold (build (EQ_EXPR, type, inner, comp_const));
4437 case GT_EXPR:
4438 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
4439 /* MAX (X, 0) > 0 -> X > 0
4440 MAX (X, 0) > 5 -> X > 5 */
4441 return fold (build (GT_EXPR, type, inner, comp_const));
4443 else if (op_code == MAX_EXPR)
4444 /* MAX (X, 0) > -1 -> true */
4445 return omit_one_operand (type, integer_one_node, inner);
4447 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
4448 /* MIN (X, 0) > 0 -> false
4449 MIN (X, 0) > 5 -> false */
4450 return omit_one_operand (type, integer_zero_node, inner);
4452 else
4453 /* MIN (X, 0) > -1 -> X > -1 */
4454 return fold (build (GT_EXPR, type, inner, comp_const));
4456 default:
4457 return t;
4461 /* T is an integer expression that is being multiplied, divided, or taken a
4462 modulus (CODE says which and what kind of divide or modulus) by a
4463 constant C. See if we can eliminate that operation by folding it with
4464 other operations already in T. WIDE_TYPE, if non-null, is a type that
4465 should be used for the computation if wider than our type.
4467 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
4468 (X * 2) + (Y * 4). We must, however, be assured that either the original
4469 expression would not overflow or that overflow is undefined for the type
4470 in the language in question.
4472 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
4473 the machine has a multiply-accumulate insn or that this is part of an
4474 addressing calculation.
4476 If we return a non-null expression, it is an equivalent form of the
4477 original computation, but need not be in the original type. */
4479 static tree
4480 extract_muldiv (t, c, code, wide_type)
4481 tree t;
4482 tree c;
4483 enum tree_code code;
4484 tree wide_type;
4486 tree type = TREE_TYPE (t);
4487 enum tree_code tcode = TREE_CODE (t);
4488 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
4489 > GET_MODE_SIZE (TYPE_MODE (type)))
4490 ? wide_type : type);
4491 tree t1, t2;
4492 int same_p = tcode == code;
4493 tree op0 = NULL_TREE, op1 = NULL_TREE;
4495 /* Don't deal with constants of zero here; they confuse the code below. */
4496 if (integer_zerop (c))
4497 return NULL_TREE;
4499 if (TREE_CODE_CLASS (tcode) == '1')
4500 op0 = TREE_OPERAND (t, 0);
4502 if (TREE_CODE_CLASS (tcode) == '2')
4503 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
4505 /* Note that we need not handle conditional operations here since fold
4506 already handles those cases. So just do arithmetic here. */
4507 switch (tcode)
4509 case INTEGER_CST:
4510 /* For a constant, we can always simplify if we are a multiply
4511 or (for divide and modulus) if it is a multiple of our constant. */
4512 if (code == MULT_EXPR
4513 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
4514 return const_binop (code, convert (ctype, t), convert (ctype, c), 0);
4515 break;
4517 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
4518 /* If op0 is an expression... */
4519 if ((TREE_CODE_CLASS (TREE_CODE (op0)) == '<'
4520 || TREE_CODE_CLASS (TREE_CODE (op0)) == '1'
4521 || TREE_CODE_CLASS (TREE_CODE (op0)) == '2'
4522 || TREE_CODE_CLASS (TREE_CODE (op0)) == 'e')
4523 /* ...and is unsigned, and its type is smaller than ctype,
4524 then we cannot pass through this widening. */
4525 && ((TREE_UNSIGNED (TREE_TYPE (op0))
4526 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
4527 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
4528 && (GET_MODE_SIZE (TYPE_MODE (ctype))
4529 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
4530 /* ...and its type is larger than ctype,
4531 then we cannot pass through this truncation. */
4532 || (GET_MODE_SIZE (TYPE_MODE (ctype))
4533 < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))))
4534 break;
4536 /* Pass the constant down and see if we can make a simplification. If
4537 we can, replace this expression with the inner simplification for
4538 possible later conversion to our or some other type. */
4539 if (0 != (t1 = extract_muldiv (op0, convert (TREE_TYPE (op0), c), code,
4540 code == MULT_EXPR ? ctype : NULL_TREE)))
4541 return t1;
4542 break;
4544 case NEGATE_EXPR: case ABS_EXPR:
4545 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4546 return fold (build1 (tcode, ctype, convert (ctype, t1)));
4547 break;
4549 case MIN_EXPR: case MAX_EXPR:
4550 /* If widening the type changes the signedness, then we can't perform
4551 this optimization as that changes the result. */
4552 if (TREE_UNSIGNED (ctype) != TREE_UNSIGNED (type))
4553 break;
4555 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
4556 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
4557 && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
4559 if (tree_int_cst_sgn (c) < 0)
4560 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
4562 return fold (build (tcode, ctype, convert (ctype, t1),
4563 convert (ctype, t2)));
4565 break;
4567 case WITH_RECORD_EXPR:
4568 if ((t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code, wide_type)) != 0)
4569 return build (WITH_RECORD_EXPR, TREE_TYPE (t1), t1,
4570 TREE_OPERAND (t, 1));
4571 break;
4573 case SAVE_EXPR:
4574 /* If this has not been evaluated and the operand has no side effects,
4575 we can see if we can do something inside it and make a new one.
4576 Note that this test is overly conservative since we can do this
4577 if the only reason it had side effects is that it was another
4578 similar SAVE_EXPR, but that isn't worth bothering with. */
4579 if (SAVE_EXPR_RTL (t) == 0 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0))
4580 && 0 != (t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code,
4581 wide_type)))
4583 t1 = save_expr (t1);
4584 if (SAVE_EXPR_PERSISTENT_P (t) && TREE_CODE (t1) == SAVE_EXPR)
4585 SAVE_EXPR_PERSISTENT_P (t1) = 1;
4586 if (is_pending_size (t))
4587 put_pending_size (t1);
4588 return t1;
4590 break;
4592 case LSHIFT_EXPR: case RSHIFT_EXPR:
4593 /* If the second operand is constant, this is a multiplication
4594 or floor division, by a power of two, so we can treat it that
4595 way unless the multiplier or divisor overflows. */
4596 if (TREE_CODE (op1) == INTEGER_CST
4597 /* const_binop may not detect overflow correctly,
4598 so check for it explicitly here. */
4599 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
4600 && TREE_INT_CST_HIGH (op1) == 0
4601 && 0 != (t1 = convert (ctype,
4602 const_binop (LSHIFT_EXPR, size_one_node,
4603 op1, 0)))
4604 && ! TREE_OVERFLOW (t1))
4605 return extract_muldiv (build (tcode == LSHIFT_EXPR
4606 ? MULT_EXPR : FLOOR_DIV_EXPR,
4607 ctype, convert (ctype, op0), t1),
4608 c, code, wide_type);
4609 break;
4611 case PLUS_EXPR: case MINUS_EXPR:
4612 /* See if we can eliminate the operation on both sides. If we can, we
4613 can return a new PLUS or MINUS. If we can't, the only remaining
4614 cases where we can do anything are if the second operand is a
4615 constant. */
4616 t1 = extract_muldiv (op0, c, code, wide_type);
4617 t2 = extract_muldiv (op1, c, code, wide_type);
4618 if (t1 != 0 && t2 != 0
4619 && (code == MULT_EXPR
4620 /* If not multiplication, we can only do this if either operand
4621 is divisible by c. */
4622 || multiple_of_p (ctype, op0, c)
4623 || multiple_of_p (ctype, op1, c)))
4624 return fold (build (tcode, ctype, convert (ctype, t1),
4625 convert (ctype, t2)));
4627 /* If this was a subtraction, negate OP1 and set it to be an addition.
4628 This simplifies the logic below. */
4629 if (tcode == MINUS_EXPR)
4630 tcode = PLUS_EXPR, op1 = negate_expr (op1);
4632 if (TREE_CODE (op1) != INTEGER_CST)
4633 break;
4635 /* If either OP1 or C are negative, this optimization is not safe for
4636 some of the division and remainder types while for others we need
4637 to change the code. */
4638 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
4640 if (code == CEIL_DIV_EXPR)
4641 code = FLOOR_DIV_EXPR;
4642 else if (code == FLOOR_DIV_EXPR)
4643 code = CEIL_DIV_EXPR;
4644 else if (code != MULT_EXPR
4645 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
4646 break;
4649 /* If it's a multiply or a division/modulus operation of a multiple
4650 of our constant, do the operation and verify it doesn't overflow. */
4651 if (code == MULT_EXPR
4652 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4654 op1 = const_binop (code, convert (ctype, op1), convert (ctype, c), 0);
4655 if (op1 == 0 || TREE_OVERFLOW (op1))
4656 break;
4658 else
4659 break;
4661 /* If we have an unsigned type is not a sizetype, we cannot widen
4662 the operation since it will change the result if the original
4663 computation overflowed. */
4664 if (TREE_UNSIGNED (ctype)
4665 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
4666 && ctype != type)
4667 break;
4669 /* If we were able to eliminate our operation from the first side,
4670 apply our operation to the second side and reform the PLUS. */
4671 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
4672 return fold (build (tcode, ctype, convert (ctype, t1), op1));
4674 /* The last case is if we are a multiply. In that case, we can
4675 apply the distributive law to commute the multiply and addition
4676 if the multiplication of the constants doesn't overflow. */
4677 if (code == MULT_EXPR)
4678 return fold (build (tcode, ctype, fold (build (code, ctype,
4679 convert (ctype, op0),
4680 convert (ctype, c))),
4681 op1));
4683 break;
4685 case MULT_EXPR:
4686 /* We have a special case here if we are doing something like
4687 (C * 8) % 4 since we know that's zero. */
4688 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
4689 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
4690 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
4691 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4692 return omit_one_operand (type, integer_zero_node, op0);
4694 /* ... fall through ... */
4696 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
4697 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
4698 /* If we can extract our operation from the LHS, do so and return a
4699 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
4700 do something only if the second operand is a constant. */
4701 if (same_p
4702 && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4703 return fold (build (tcode, ctype, convert (ctype, t1),
4704 convert (ctype, op1)));
4705 else if (tcode == MULT_EXPR && code == MULT_EXPR
4706 && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
4707 return fold (build (tcode, ctype, convert (ctype, op0),
4708 convert (ctype, t1)));
4709 else if (TREE_CODE (op1) != INTEGER_CST)
4710 return 0;
4712 /* If these are the same operation types, we can associate them
4713 assuming no overflow. */
4714 if (tcode == code
4715 && 0 != (t1 = const_binop (MULT_EXPR, convert (ctype, op1),
4716 convert (ctype, c), 0))
4717 && ! TREE_OVERFLOW (t1))
4718 return fold (build (tcode, ctype, convert (ctype, op0), t1));
4720 /* If these operations "cancel" each other, we have the main
4721 optimizations of this pass, which occur when either constant is a
4722 multiple of the other, in which case we replace this with either an
4723 operation or CODE or TCODE.
4725 If we have an unsigned type that is not a sizetype, we cannot do
4726 this since it will change the result if the original computation
4727 overflowed. */
4728 if ((! TREE_UNSIGNED (ctype)
4729 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
4730 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
4731 || (tcode == MULT_EXPR
4732 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
4733 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
4735 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4736 return fold (build (tcode, ctype, convert (ctype, op0),
4737 convert (ctype,
4738 const_binop (TRUNC_DIV_EXPR,
4739 op1, c, 0))));
4740 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
4741 return fold (build (code, ctype, convert (ctype, op0),
4742 convert (ctype,
4743 const_binop (TRUNC_DIV_EXPR,
4744 c, op1, 0))));
4746 break;
4748 default:
4749 break;
4752 return 0;
4755 /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
4756 S, a SAVE_EXPR, return the expression actually being evaluated. Note
4757 that we may sometimes modify the tree. */
4759 static tree
4760 strip_compound_expr (t, s)
4761 tree t;
4762 tree s;
4764 enum tree_code code = TREE_CODE (t);
4766 /* See if this is the COMPOUND_EXPR we want to eliminate. */
4767 if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
4768 && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
4769 return TREE_OPERAND (t, 1);
4771 /* See if this is a COND_EXPR or a simple arithmetic operator. We
4772 don't bother handling any other types. */
4773 else if (code == COND_EXPR)
4775 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4776 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4777 TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
4779 else if (TREE_CODE_CLASS (code) == '1')
4780 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4781 else if (TREE_CODE_CLASS (code) == '<'
4782 || TREE_CODE_CLASS (code) == '2')
4784 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4785 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4788 return t;
4791 /* Return a node which has the indicated constant VALUE (either 0 or
4792 1), and is of the indicated TYPE. */
4794 static tree
4795 constant_boolean_node (value, type)
4796 int value;
4797 tree type;
4799 if (type == integer_type_node)
4800 return value ? integer_one_node : integer_zero_node;
4801 else if (TREE_CODE (type) == BOOLEAN_TYPE)
4802 return truthvalue_conversion (value ? integer_one_node :
4803 integer_zero_node);
4804 else
4806 tree t = build_int_2 (value, 0);
4808 TREE_TYPE (t) = type;
4809 return t;
4813 /* Utility function for the following routine, to see how complex a nesting of
4814 COND_EXPRs can be. EXPR is the expression and LIMIT is a count beyond which
4815 we don't care (to avoid spending too much time on complex expressions.). */
4817 static int
4818 count_cond (expr, lim)
4819 tree expr;
4820 int lim;
4822 int ctrue, cfalse;
4824 if (TREE_CODE (expr) != COND_EXPR)
4825 return 0;
4826 else if (lim <= 0)
4827 return 0;
4829 ctrue = count_cond (TREE_OPERAND (expr, 1), lim - 1);
4830 cfalse = count_cond (TREE_OPERAND (expr, 2), lim - 1 - ctrue);
4831 return MIN (lim, 1 + ctrue + cfalse);
4834 /* Transform `a + (b ? x : y)' into `x ? (a + b) : (a + y)'.
4835 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
4836 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
4837 expression, and ARG to `a'. If COND_FIRST_P is non-zero, then the
4838 COND is the first argument to CODE; otherwise (as in the example
4839 given here), it is the second argument. TYPE is the type of the
4840 original expression. */
4842 static tree
4843 fold_binary_op_with_conditional_arg (code, type, cond, arg, cond_first_p)
4844 enum tree_code code;
4845 tree type;
4846 tree cond;
4847 tree arg;
4848 int cond_first_p;
4850 tree test, true_value, false_value;
4851 tree lhs = NULL_TREE;
4852 tree rhs = NULL_TREE;
4853 /* In the end, we'll produce a COND_EXPR. Both arms of the
4854 conditional expression will be binary operations. The left-hand
4855 side of the expression to be executed if the condition is true
4856 will be pointed to by TRUE_LHS. Similarly, the right-hand side
4857 of the expression to be executed if the condition is true will be
4858 pointed to by TRUE_RHS. FALSE_LHS and FALSE_RHS are analogous --
4859 but apply to the expression to be executed if the conditional is
4860 false. */
4861 tree *true_lhs;
4862 tree *true_rhs;
4863 tree *false_lhs;
4864 tree *false_rhs;
4865 /* These are the codes to use for the left-hand side and right-hand
4866 side of the COND_EXPR. Normally, they are the same as CODE. */
4867 enum tree_code lhs_code = code;
4868 enum tree_code rhs_code = code;
4869 /* And these are the types of the expressions. */
4870 tree lhs_type = type;
4871 tree rhs_type = type;
4873 if (cond_first_p)
4875 true_rhs = false_rhs = &arg;
4876 true_lhs = &true_value;
4877 false_lhs = &false_value;
4879 else
4881 true_lhs = false_lhs = &arg;
4882 true_rhs = &true_value;
4883 false_rhs = &false_value;
4886 if (TREE_CODE (cond) == COND_EXPR)
4888 test = TREE_OPERAND (cond, 0);
4889 true_value = TREE_OPERAND (cond, 1);
4890 false_value = TREE_OPERAND (cond, 2);
4891 /* If this operand throws an expression, then it does not make
4892 sense to try to perform a logical or arithmetic operation
4893 involving it. Instead of building `a + throw 3' for example,
4894 we simply build `a, throw 3'. */
4895 if (VOID_TYPE_P (TREE_TYPE (true_value)))
4897 lhs_code = COMPOUND_EXPR;
4898 if (!cond_first_p)
4899 lhs_type = void_type_node;
4901 if (VOID_TYPE_P (TREE_TYPE (false_value)))
4903 rhs_code = COMPOUND_EXPR;
4904 if (!cond_first_p)
4905 rhs_type = void_type_node;
4908 else
4910 tree testtype = TREE_TYPE (cond);
4911 test = cond;
4912 true_value = convert (testtype, integer_one_node);
4913 false_value = convert (testtype, integer_zero_node);
4916 /* If ARG is complex we want to make sure we only evaluate
4917 it once. Though this is only required if it is volatile, it
4918 might be more efficient even if it is not. However, if we
4919 succeed in folding one part to a constant, we do not need
4920 to make this SAVE_EXPR. Since we do this optimization
4921 primarily to see if we do end up with constant and this
4922 SAVE_EXPR interferes with later optimizations, suppressing
4923 it when we can is important.
4925 If we are not in a function, we can't make a SAVE_EXPR, so don't
4926 try to do so. Don't try to see if the result is a constant
4927 if an arm is a COND_EXPR since we get exponential behavior
4928 in that case. */
4930 if (TREE_CODE (arg) != SAVE_EXPR && ! TREE_CONSTANT (arg)
4931 && global_bindings_p () == 0
4932 && ((TREE_CODE (arg) != VAR_DECL
4933 && TREE_CODE (arg) != PARM_DECL)
4934 || TREE_SIDE_EFFECTS (arg)))
4936 if (TREE_CODE (true_value) != COND_EXPR)
4937 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4939 if (TREE_CODE (false_value) != COND_EXPR)
4940 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4942 if ((lhs == 0 || ! TREE_CONSTANT (lhs))
4943 && (rhs == 0 || !TREE_CONSTANT (rhs)))
4944 arg = save_expr (arg), lhs = rhs = 0;
4947 if (lhs == 0)
4948 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4949 if (rhs == 0)
4950 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4952 test = fold (build (COND_EXPR, type, test, lhs, rhs));
4954 if (TREE_CODE (arg) == SAVE_EXPR)
4955 return build (COMPOUND_EXPR, type,
4956 convert (void_type_node, arg),
4957 strip_compound_expr (test, arg));
4958 else
4959 return convert (type, test);
4963 /* Perform constant folding and related simplification of EXPR.
4964 The related simplifications include x*1 => x, x*0 => 0, etc.,
4965 and application of the associative law.
4966 NOP_EXPR conversions may be removed freely (as long as we
4967 are careful not to change the C type of the overall expression)
4968 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
4969 but we can constant-fold them if they have constant operands. */
4971 tree
4972 fold (expr)
4973 tree expr;
4975 tree t = expr;
4976 tree t1 = NULL_TREE;
4977 tree tem;
4978 tree type = TREE_TYPE (expr);
4979 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4980 enum tree_code code = TREE_CODE (t);
4981 int kind = TREE_CODE_CLASS (code);
4982 int invert;
4983 /* WINS will be nonzero when the switch is done
4984 if all operands are constant. */
4985 int wins = 1;
4987 /* Don't try to process an RTL_EXPR since its operands aren't trees.
4988 Likewise for a SAVE_EXPR that's already been evaluated. */
4989 if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t) != 0))
4990 return t;
4992 /* Return right away if a constant. */
4993 if (kind == 'c')
4994 return t;
4996 #ifdef MAX_INTEGER_COMPUTATION_MODE
4997 check_max_integer_computation_mode (expr);
4998 #endif
5000 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
5002 tree subop;
5004 /* Special case for conversion ops that can have fixed point args. */
5005 arg0 = TREE_OPERAND (t, 0);
5007 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
5008 if (arg0 != 0)
5009 STRIP_SIGN_NOPS (arg0);
5011 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
5012 subop = TREE_REALPART (arg0);
5013 else
5014 subop = arg0;
5016 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
5017 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
5018 && TREE_CODE (subop) != REAL_CST
5019 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
5021 /* Note that TREE_CONSTANT isn't enough:
5022 static var addresses are constant but we can't
5023 do arithmetic on them. */
5024 wins = 0;
5026 else if (IS_EXPR_CODE_CLASS (kind) || kind == 'r')
5028 int len = first_rtl_op (code);
5029 int i;
5030 for (i = 0; i < len; i++)
5032 tree op = TREE_OPERAND (t, i);
5033 tree subop;
5035 if (op == 0)
5036 continue; /* Valid for CALL_EXPR, at least. */
5038 if (kind == '<' || code == RSHIFT_EXPR)
5040 /* Signedness matters here. Perhaps we can refine this
5041 later. */
5042 STRIP_SIGN_NOPS (op);
5044 else
5045 /* Strip any conversions that don't change the mode. */
5046 STRIP_NOPS (op);
5048 if (TREE_CODE (op) == COMPLEX_CST)
5049 subop = TREE_REALPART (op);
5050 else
5051 subop = op;
5053 if (TREE_CODE (subop) != INTEGER_CST
5054 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
5055 && TREE_CODE (subop) != REAL_CST
5056 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
5058 /* Note that TREE_CONSTANT isn't enough:
5059 static var addresses are constant but we can't
5060 do arithmetic on them. */
5061 wins = 0;
5063 if (i == 0)
5064 arg0 = op;
5065 else if (i == 1)
5066 arg1 = op;
5070 /* If this is a commutative operation, and ARG0 is a constant, move it
5071 to ARG1 to reduce the number of tests below. */
5072 if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
5073 || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
5074 || code == BIT_AND_EXPR)
5075 && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
5077 tem = arg0; arg0 = arg1; arg1 = tem;
5079 tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
5080 TREE_OPERAND (t, 1) = tem;
5083 /* Now WINS is set as described above,
5084 ARG0 is the first operand of EXPR,
5085 and ARG1 is the second operand (if it has more than one operand).
5087 First check for cases where an arithmetic operation is applied to a
5088 compound, conditional, or comparison operation. Push the arithmetic
5089 operation inside the compound or conditional to see if any folding
5090 can then be done. Convert comparison to conditional for this purpose.
5091 The also optimizes non-constant cases that used to be done in
5092 expand_expr.
5094 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
5095 one of the operands is a comparison and the other is a comparison, a
5096 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
5097 code below would make the expression more complex. Change it to a
5098 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
5099 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
5101 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
5102 || code == EQ_EXPR || code == NE_EXPR)
5103 && ((truth_value_p (TREE_CODE (arg0))
5104 && (truth_value_p (TREE_CODE (arg1))
5105 || (TREE_CODE (arg1) == BIT_AND_EXPR
5106 && integer_onep (TREE_OPERAND (arg1, 1)))))
5107 || (truth_value_p (TREE_CODE (arg1))
5108 && (truth_value_p (TREE_CODE (arg0))
5109 || (TREE_CODE (arg0) == BIT_AND_EXPR
5110 && integer_onep (TREE_OPERAND (arg0, 1)))))))
5112 t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
5113 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
5114 : TRUTH_XOR_EXPR,
5115 type, arg0, arg1));
5117 if (code == EQ_EXPR)
5118 t = invert_truthvalue (t);
5120 return t;
5123 if (TREE_CODE_CLASS (code) == '1')
5125 if (TREE_CODE (arg0) == COMPOUND_EXPR)
5126 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5127 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
5128 else if (TREE_CODE (arg0) == COND_EXPR)
5130 t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
5131 fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
5132 fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
5134 /* If this was a conversion, and all we did was to move into
5135 inside the COND_EXPR, bring it back out. But leave it if
5136 it is a conversion from integer to integer and the
5137 result precision is no wider than a word since such a
5138 conversion is cheap and may be optimized away by combine,
5139 while it couldn't if it were outside the COND_EXPR. Then return
5140 so we don't get into an infinite recursion loop taking the
5141 conversion out and then back in. */
5143 if ((code == NOP_EXPR || code == CONVERT_EXPR
5144 || code == NON_LVALUE_EXPR)
5145 && TREE_CODE (t) == COND_EXPR
5146 && TREE_CODE (TREE_OPERAND (t, 1)) == code
5147 && TREE_CODE (TREE_OPERAND (t, 2)) == code
5148 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
5149 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0)))
5150 && ! (INTEGRAL_TYPE_P (TREE_TYPE (t))
5151 && (INTEGRAL_TYPE_P
5152 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))))
5153 && TYPE_PRECISION (TREE_TYPE (t)) <= BITS_PER_WORD))
5154 t = build1 (code, type,
5155 build (COND_EXPR,
5156 TREE_TYPE (TREE_OPERAND
5157 (TREE_OPERAND (t, 1), 0)),
5158 TREE_OPERAND (t, 0),
5159 TREE_OPERAND (TREE_OPERAND (t, 1), 0),
5160 TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
5161 return t;
5163 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
5164 return fold (build (COND_EXPR, type, arg0,
5165 fold (build1 (code, type, integer_one_node)),
5166 fold (build1 (code, type, integer_zero_node))));
5168 else if (TREE_CODE_CLASS (code) == '2'
5169 || TREE_CODE_CLASS (code) == '<')
5171 if (TREE_CODE (arg1) == COMPOUND_EXPR)
5172 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5173 fold (build (code, type,
5174 arg0, TREE_OPERAND (arg1, 1))));
5175 else if ((TREE_CODE (arg1) == COND_EXPR
5176 || (TREE_CODE_CLASS (TREE_CODE (arg1)) == '<'
5177 && TREE_CODE_CLASS (code) != '<'))
5178 && (TREE_CODE (arg0) != COND_EXPR
5179 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
5180 && (! TREE_SIDE_EFFECTS (arg0)
5181 || (global_bindings_p () == 0
5182 && ! contains_placeholder_p (arg0))))
5183 return
5184 fold_binary_op_with_conditional_arg (code, type, arg1, arg0,
5185 /*cond_first_p=*/0);
5186 else if (TREE_CODE (arg0) == COMPOUND_EXPR)
5187 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5188 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5189 else if ((TREE_CODE (arg0) == COND_EXPR
5190 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
5191 && TREE_CODE_CLASS (code) != '<'))
5192 && (TREE_CODE (arg1) != COND_EXPR
5193 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
5194 && (! TREE_SIDE_EFFECTS (arg1)
5195 || (global_bindings_p () == 0
5196 && ! contains_placeholder_p (arg1))))
5197 return
5198 fold_binary_op_with_conditional_arg (code, type, arg0, arg1,
5199 /*cond_first_p=*/1);
5201 else if (TREE_CODE_CLASS (code) == '<'
5202 && TREE_CODE (arg0) == COMPOUND_EXPR)
5203 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5204 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5205 else if (TREE_CODE_CLASS (code) == '<'
5206 && TREE_CODE (arg1) == COMPOUND_EXPR)
5207 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5208 fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
5210 switch (code)
5212 case INTEGER_CST:
5213 case REAL_CST:
5214 case VECTOR_CST:
5215 case STRING_CST:
5216 case COMPLEX_CST:
5217 case CONSTRUCTOR:
5218 return t;
5220 case CONST_DECL:
5221 return fold (DECL_INITIAL (t));
5223 case NOP_EXPR:
5224 case FLOAT_EXPR:
5225 case CONVERT_EXPR:
5226 case FIX_TRUNC_EXPR:
5227 /* Other kinds of FIX are not handled properly by fold_convert. */
5229 if (TREE_TYPE (TREE_OPERAND (t, 0)) == TREE_TYPE (t))
5230 return TREE_OPERAND (t, 0);
5232 /* Handle cases of two conversions in a row. */
5233 if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
5234 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
5236 tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5237 tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
5238 tree final_type = TREE_TYPE (t);
5239 int inside_int = INTEGRAL_TYPE_P (inside_type);
5240 int inside_ptr = POINTER_TYPE_P (inside_type);
5241 int inside_float = FLOAT_TYPE_P (inside_type);
5242 unsigned int inside_prec = TYPE_PRECISION (inside_type);
5243 int inside_unsignedp = TREE_UNSIGNED (inside_type);
5244 int inter_int = INTEGRAL_TYPE_P (inter_type);
5245 int inter_ptr = POINTER_TYPE_P (inter_type);
5246 int inter_float = FLOAT_TYPE_P (inter_type);
5247 unsigned int inter_prec = TYPE_PRECISION (inter_type);
5248 int inter_unsignedp = TREE_UNSIGNED (inter_type);
5249 int final_int = INTEGRAL_TYPE_P (final_type);
5250 int final_ptr = POINTER_TYPE_P (final_type);
5251 int final_float = FLOAT_TYPE_P (final_type);
5252 unsigned int final_prec = TYPE_PRECISION (final_type);
5253 int final_unsignedp = TREE_UNSIGNED (final_type);
5255 /* In addition to the cases of two conversions in a row
5256 handled below, if we are converting something to its own
5257 type via an object of identical or wider precision, neither
5258 conversion is needed. */
5259 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (final_type)
5260 && ((inter_int && final_int) || (inter_float && final_float))
5261 && inter_prec >= final_prec)
5262 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5264 /* Likewise, if the intermediate and final types are either both
5265 float or both integer, we don't need the middle conversion if
5266 it is wider than the final type and doesn't change the signedness
5267 (for integers). Avoid this if the final type is a pointer
5268 since then we sometimes need the inner conversion. Likewise if
5269 the outer has a precision not equal to the size of its mode. */
5270 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
5271 || (inter_float && inside_float))
5272 && inter_prec >= inside_prec
5273 && (inter_float || inter_unsignedp == inside_unsignedp)
5274 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5275 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5276 && ! final_ptr)
5277 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5279 /* If we have a sign-extension of a zero-extended value, we can
5280 replace that by a single zero-extension. */
5281 if (inside_int && inter_int && final_int
5282 && inside_prec < inter_prec && inter_prec < final_prec
5283 && inside_unsignedp && !inter_unsignedp)
5284 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5286 /* Two conversions in a row are not needed unless:
5287 - some conversion is floating-point (overstrict for now), or
5288 - the intermediate type is narrower than both initial and
5289 final, or
5290 - the intermediate type and innermost type differ in signedness,
5291 and the outermost type is wider than the intermediate, or
5292 - the initial type is a pointer type and the precisions of the
5293 intermediate and final types differ, or
5294 - the final type is a pointer type and the precisions of the
5295 initial and intermediate types differ. */
5296 if (! inside_float && ! inter_float && ! final_float
5297 && (inter_prec > inside_prec || inter_prec > final_prec)
5298 && ! (inside_int && inter_int
5299 && inter_unsignedp != inside_unsignedp
5300 && inter_prec < final_prec)
5301 && ((inter_unsignedp && inter_prec > inside_prec)
5302 == (final_unsignedp && final_prec > inter_prec))
5303 && ! (inside_ptr && inter_prec != final_prec)
5304 && ! (final_ptr && inside_prec != inter_prec)
5305 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5306 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5307 && ! final_ptr)
5308 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5311 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
5312 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
5313 /* Detect assigning a bitfield. */
5314 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
5315 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
5317 /* Don't leave an assignment inside a conversion
5318 unless assigning a bitfield. */
5319 tree prev = TREE_OPERAND (t, 0);
5320 TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
5321 /* First do the assignment, then return converted constant. */
5322 t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
5323 TREE_USED (t) = 1;
5324 return t;
5326 if (!wins)
5328 TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
5329 return t;
5331 return fold_convert (t, arg0);
5333 case VIEW_CONVERT_EXPR:
5334 if (TREE_CODE (TREE_OPERAND (t, 0)) == VIEW_CONVERT_EXPR)
5335 return build1 (VIEW_CONVERT_EXPR, type,
5336 TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5337 return t;
5339 #if 0 /* This loses on &"foo"[0]. */
5340 case ARRAY_REF:
5342 int i;
5344 /* Fold an expression like: "foo"[2] */
5345 if (TREE_CODE (arg0) == STRING_CST
5346 && TREE_CODE (arg1) == INTEGER_CST
5347 && compare_tree_int (arg1, TREE_STRING_LENGTH (arg0)) < 0)
5349 t = build_int_2 (TREE_STRING_POINTER (arg0)[TREE_INT_CST_LOW (arg))], 0);
5350 TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
5351 force_fit_type (t, 0);
5354 return t;
5355 #endif /* 0 */
5357 case COMPONENT_REF:
5358 if (TREE_CODE (arg0) == CONSTRUCTOR)
5360 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
5361 if (m)
5362 t = TREE_VALUE (m);
5364 return t;
5366 case RANGE_EXPR:
5367 TREE_CONSTANT (t) = wins;
5368 return t;
5370 case NEGATE_EXPR:
5371 if (wins)
5373 if (TREE_CODE (arg0) == INTEGER_CST)
5375 unsigned HOST_WIDE_INT low;
5376 HOST_WIDE_INT high;
5377 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5378 TREE_INT_CST_HIGH (arg0),
5379 &low, &high);
5380 t = build_int_2 (low, high);
5381 TREE_TYPE (t) = type;
5382 TREE_OVERFLOW (t)
5383 = (TREE_OVERFLOW (arg0)
5384 | force_fit_type (t, overflow && !TREE_UNSIGNED (type)));
5385 TREE_CONSTANT_OVERFLOW (t)
5386 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
5388 else if (TREE_CODE (arg0) == REAL_CST)
5389 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5391 else if (TREE_CODE (arg0) == NEGATE_EXPR)
5392 return TREE_OPERAND (arg0, 0);
5394 /* Convert - (a - b) to (b - a) for non-floating-point. */
5395 else if (TREE_CODE (arg0) == MINUS_EXPR
5396 && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
5397 return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
5398 TREE_OPERAND (arg0, 0));
5400 return t;
5402 case ABS_EXPR:
5403 if (wins)
5405 if (TREE_CODE (arg0) == INTEGER_CST)
5407 /* If the value is unsigned, then the absolute value is
5408 the same as the ordinary value. */
5409 if (TREE_UNSIGNED (type))
5410 return arg0;
5411 /* Similarly, if the value is non-negative. */
5412 else if (INT_CST_LT (integer_minus_one_node, arg0))
5413 return arg0;
5414 /* If the value is negative, then the absolute value is
5415 its negation. */
5416 else
5418 unsigned HOST_WIDE_INT low;
5419 HOST_WIDE_INT high;
5420 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5421 TREE_INT_CST_HIGH (arg0),
5422 &low, &high);
5423 t = build_int_2 (low, high);
5424 TREE_TYPE (t) = type;
5425 TREE_OVERFLOW (t)
5426 = (TREE_OVERFLOW (arg0)
5427 | force_fit_type (t, overflow));
5428 TREE_CONSTANT_OVERFLOW (t)
5429 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
5432 else if (TREE_CODE (arg0) == REAL_CST)
5434 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
5435 t = build_real (type,
5436 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5439 else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
5440 return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
5441 return t;
5443 case CONJ_EXPR:
5444 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
5445 return convert (type, arg0);
5446 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
5447 return build (COMPLEX_EXPR, type,
5448 TREE_OPERAND (arg0, 0),
5449 negate_expr (TREE_OPERAND (arg0, 1)));
5450 else if (TREE_CODE (arg0) == COMPLEX_CST)
5451 return build_complex (type, TREE_REALPART (arg0),
5452 negate_expr (TREE_IMAGPART (arg0)));
5453 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
5454 return fold (build (TREE_CODE (arg0), type,
5455 fold (build1 (CONJ_EXPR, type,
5456 TREE_OPERAND (arg0, 0))),
5457 fold (build1 (CONJ_EXPR,
5458 type, TREE_OPERAND (arg0, 1)))));
5459 else if (TREE_CODE (arg0) == CONJ_EXPR)
5460 return TREE_OPERAND (arg0, 0);
5461 return t;
5463 case BIT_NOT_EXPR:
5464 if (wins)
5466 t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
5467 ~ TREE_INT_CST_HIGH (arg0));
5468 TREE_TYPE (t) = type;
5469 force_fit_type (t, 0);
5470 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
5471 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
5473 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
5474 return TREE_OPERAND (arg0, 0);
5475 return t;
5477 case PLUS_EXPR:
5478 /* A + (-B) -> A - B */
5479 if (TREE_CODE (arg1) == NEGATE_EXPR)
5480 return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5481 /* (-A) + B -> B - A */
5482 if (TREE_CODE (arg0) == NEGATE_EXPR)
5483 return fold (build (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
5484 else if (! FLOAT_TYPE_P (type))
5486 if (integer_zerop (arg1))
5487 return non_lvalue (convert (type, arg0));
5489 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
5490 with a constant, and the two constants have no bits in common,
5491 we should treat this as a BIT_IOR_EXPR since this may produce more
5492 simplifications. */
5493 if (TREE_CODE (arg0) == BIT_AND_EXPR
5494 && TREE_CODE (arg1) == BIT_AND_EXPR
5495 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5496 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5497 && integer_zerop (const_binop (BIT_AND_EXPR,
5498 TREE_OPERAND (arg0, 1),
5499 TREE_OPERAND (arg1, 1), 0)))
5501 code = BIT_IOR_EXPR;
5502 goto bit_ior;
5505 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
5506 (plus (plus (mult) (mult)) (foo)) so that we can
5507 take advantage of the factoring cases below. */
5508 if ((TREE_CODE (arg0) == PLUS_EXPR
5509 && TREE_CODE (arg1) == MULT_EXPR)
5510 || (TREE_CODE (arg1) == PLUS_EXPR
5511 && TREE_CODE (arg0) == MULT_EXPR))
5513 tree parg0, parg1, parg, marg;
5515 if (TREE_CODE (arg0) == PLUS_EXPR)
5516 parg = arg0, marg = arg1;
5517 else
5518 parg = arg1, marg = arg0;
5519 parg0 = TREE_OPERAND (parg, 0);
5520 parg1 = TREE_OPERAND (parg, 1);
5521 STRIP_NOPS (parg0);
5522 STRIP_NOPS (parg1);
5524 if (TREE_CODE (parg0) == MULT_EXPR
5525 && TREE_CODE (parg1) != MULT_EXPR)
5526 return fold (build (PLUS_EXPR, type,
5527 fold (build (PLUS_EXPR, type, parg0, marg)),
5528 parg1));
5529 if (TREE_CODE (parg0) != MULT_EXPR
5530 && TREE_CODE (parg1) == MULT_EXPR)
5531 return fold (build (PLUS_EXPR, type,
5532 fold (build (PLUS_EXPR, type, parg1, marg)),
5533 parg0));
5536 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
5538 tree arg00, arg01, arg10, arg11;
5539 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
5541 /* (A * C) + (B * C) -> (A+B) * C.
5542 We are most concerned about the case where C is a constant,
5543 but other combinations show up during loop reduction. Since
5544 it is not difficult, try all four possibilities. */
5546 arg00 = TREE_OPERAND (arg0, 0);
5547 arg01 = TREE_OPERAND (arg0, 1);
5548 arg10 = TREE_OPERAND (arg1, 0);
5549 arg11 = TREE_OPERAND (arg1, 1);
5550 same = NULL_TREE;
5552 if (operand_equal_p (arg01, arg11, 0))
5553 same = arg01, alt0 = arg00, alt1 = arg10;
5554 else if (operand_equal_p (arg00, arg10, 0))
5555 same = arg00, alt0 = arg01, alt1 = arg11;
5556 else if (operand_equal_p (arg00, arg11, 0))
5557 same = arg00, alt0 = arg01, alt1 = arg10;
5558 else if (operand_equal_p (arg01, arg10, 0))
5559 same = arg01, alt0 = arg00, alt1 = arg11;
5561 /* No identical multiplicands; see if we can find a common
5562 power-of-two factor in non-power-of-two multiplies. This
5563 can help in multi-dimensional array access. */
5564 else if (TREE_CODE (arg01) == INTEGER_CST
5565 && TREE_CODE (arg11) == INTEGER_CST
5566 && TREE_INT_CST_HIGH (arg01) == 0
5567 && TREE_INT_CST_HIGH (arg11) == 0)
5569 HOST_WIDE_INT int01, int11, tmp;
5570 int01 = TREE_INT_CST_LOW (arg01);
5571 int11 = TREE_INT_CST_LOW (arg11);
5573 /* Move min of absolute values to int11. */
5574 if ((int01 >= 0 ? int01 : -int01)
5575 < (int11 >= 0 ? int11 : -int11))
5577 tmp = int01, int01 = int11, int11 = tmp;
5578 alt0 = arg00, arg00 = arg10, arg10 = alt0;
5579 alt0 = arg01, arg01 = arg11, arg11 = alt0;
5582 if (exact_log2 (int11) > 0 && int01 % int11 == 0)
5584 alt0 = fold (build (MULT_EXPR, type, arg00,
5585 build_int_2 (int01 / int11, 0)));
5586 alt1 = arg10;
5587 same = arg11;
5591 if (same)
5592 return fold (build (MULT_EXPR, type,
5593 fold (build (PLUS_EXPR, type, alt0, alt1)),
5594 same));
5597 /* In IEEE floating point, x+0 may not equal x. */
5598 else if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5599 || flag_unsafe_math_optimizations)
5600 && real_zerop (arg1))
5601 return non_lvalue (convert (type, arg0));
5602 /* x+(-0) equals x, even for IEEE. */
5603 else if (TREE_CODE (arg1) == REAL_CST
5604 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1)))
5605 return non_lvalue (convert (type, arg0));
5607 bit_rotate:
5608 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
5609 is a rotate of A by C1 bits. */
5610 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
5611 is a rotate of A by B bits. */
5613 enum tree_code code0, code1;
5614 code0 = TREE_CODE (arg0);
5615 code1 = TREE_CODE (arg1);
5616 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
5617 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
5618 && operand_equal_p (TREE_OPERAND (arg0, 0),
5619 TREE_OPERAND (arg1, 0), 0)
5620 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5622 tree tree01, tree11;
5623 enum tree_code code01, code11;
5625 tree01 = TREE_OPERAND (arg0, 1);
5626 tree11 = TREE_OPERAND (arg1, 1);
5627 STRIP_NOPS (tree01);
5628 STRIP_NOPS (tree11);
5629 code01 = TREE_CODE (tree01);
5630 code11 = TREE_CODE (tree11);
5631 if (code01 == INTEGER_CST
5632 && code11 == INTEGER_CST
5633 && TREE_INT_CST_HIGH (tree01) == 0
5634 && TREE_INT_CST_HIGH (tree11) == 0
5635 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
5636 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
5637 return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
5638 code0 == LSHIFT_EXPR ? tree01 : tree11);
5639 else if (code11 == MINUS_EXPR)
5641 tree tree110, tree111;
5642 tree110 = TREE_OPERAND (tree11, 0);
5643 tree111 = TREE_OPERAND (tree11, 1);
5644 STRIP_NOPS (tree110);
5645 STRIP_NOPS (tree111);
5646 if (TREE_CODE (tree110) == INTEGER_CST
5647 && 0 == compare_tree_int (tree110,
5648 TYPE_PRECISION
5649 (TREE_TYPE (TREE_OPERAND
5650 (arg0, 0))))
5651 && operand_equal_p (tree01, tree111, 0))
5652 return build ((code0 == LSHIFT_EXPR
5653 ? LROTATE_EXPR
5654 : RROTATE_EXPR),
5655 type, TREE_OPERAND (arg0, 0), tree01);
5657 else if (code01 == MINUS_EXPR)
5659 tree tree010, tree011;
5660 tree010 = TREE_OPERAND (tree01, 0);
5661 tree011 = TREE_OPERAND (tree01, 1);
5662 STRIP_NOPS (tree010);
5663 STRIP_NOPS (tree011);
5664 if (TREE_CODE (tree010) == INTEGER_CST
5665 && 0 == compare_tree_int (tree010,
5666 TYPE_PRECISION
5667 (TREE_TYPE (TREE_OPERAND
5668 (arg0, 0))))
5669 && operand_equal_p (tree11, tree011, 0))
5670 return build ((code0 != LSHIFT_EXPR
5671 ? LROTATE_EXPR
5672 : RROTATE_EXPR),
5673 type, TREE_OPERAND (arg0, 0), tree11);
5678 associate:
5679 /* In most languages, can't associate operations on floats through
5680 parentheses. Rather than remember where the parentheses were, we
5681 don't associate floats at all. It shouldn't matter much. However,
5682 associating multiplications is only very slightly inaccurate, so do
5683 that if -funsafe-math-optimizations is specified. */
5685 if (! wins
5686 && (! FLOAT_TYPE_P (type)
5687 || (flag_unsafe_math_optimizations && code == MULT_EXPR)))
5689 tree var0, con0, lit0, minus_lit0;
5690 tree var1, con1, lit1, minus_lit1;
5692 /* Split both trees into variables, constants, and literals. Then
5693 associate each group together, the constants with literals,
5694 then the result with variables. This increases the chances of
5695 literals being recombined later and of generating relocatable
5696 expressions for the sum of a constant and literal. */
5697 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
5698 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
5699 code == MINUS_EXPR);
5701 /* Only do something if we found more than two objects. Otherwise,
5702 nothing has changed and we risk infinite recursion. */
5703 if (2 < ((var0 != 0) + (var1 != 0)
5704 + (con0 != 0) + (con1 != 0)
5705 + (lit0 != 0) + (lit1 != 0)
5706 + (minus_lit0 != 0) + (minus_lit1 != 0)))
5708 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
5709 if (code == MINUS_EXPR)
5710 code = PLUS_EXPR;
5712 var0 = associate_trees (var0, var1, code, type);
5713 con0 = associate_trees (con0, con1, code, type);
5714 lit0 = associate_trees (lit0, lit1, code, type);
5715 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
5717 /* Preserve the MINUS_EXPR if the negative part of the literal is
5718 greater than the positive part. Otherwise, the multiplicative
5719 folding code (i.e extract_muldiv) may be fooled in case
5720 unsigned constants are substracted, like in the following
5721 example: ((X*2 + 4) - 8U)/2. */
5722 if (minus_lit0 && lit0)
5724 if (tree_int_cst_lt (lit0, minus_lit0))
5726 minus_lit0 = associate_trees (minus_lit0, lit0,
5727 MINUS_EXPR, type);
5728 lit0 = 0;
5730 else
5732 lit0 = associate_trees (lit0, minus_lit0,
5733 MINUS_EXPR, type);
5734 minus_lit0 = 0;
5737 if (minus_lit0)
5739 if (con0 == 0)
5740 return convert (type, associate_trees (var0, minus_lit0,
5741 MINUS_EXPR, type));
5742 else
5744 con0 = associate_trees (con0, minus_lit0,
5745 MINUS_EXPR, type);
5746 return convert (type, associate_trees (var0, con0,
5747 PLUS_EXPR, type));
5751 con0 = associate_trees (con0, lit0, code, type);
5752 return convert (type, associate_trees (var0, con0, code, type));
5756 binary:
5757 #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
5758 if (TREE_CODE (arg1) == REAL_CST)
5759 return t;
5760 #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
5761 if (wins)
5762 t1 = const_binop (code, arg0, arg1, 0);
5763 if (t1 != NULL_TREE)
5765 /* The return value should always have
5766 the same type as the original expression. */
5767 if (TREE_TYPE (t1) != TREE_TYPE (t))
5768 t1 = convert (TREE_TYPE (t), t1);
5770 return t1;
5772 return t;
5774 case MINUS_EXPR:
5775 /* A - (-B) -> A + B */
5776 if (TREE_CODE (arg1) == NEGATE_EXPR)
5777 return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5778 /* (-A) - CST -> (-CST) - A for floating point (what about ints ?) */
5779 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == REAL_CST)
5780 return
5781 fold (build (MINUS_EXPR, type,
5782 build_real (TREE_TYPE (arg1),
5783 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1))),
5784 TREE_OPERAND (arg0, 0)));
5786 if (! FLOAT_TYPE_P (type))
5788 if (! wins && integer_zerop (arg0))
5789 return negate_expr (convert (type, arg1));
5790 if (integer_zerop (arg1))
5791 return non_lvalue (convert (type, arg0));
5793 /* (A * C) - (B * C) -> (A-B) * C. Since we are most concerned
5794 about the case where C is a constant, just try one of the
5795 four possibilities. */
5797 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
5798 && operand_equal_p (TREE_OPERAND (arg0, 1),
5799 TREE_OPERAND (arg1, 1), 0))
5800 return fold (build (MULT_EXPR, type,
5801 fold (build (MINUS_EXPR, type,
5802 TREE_OPERAND (arg0, 0),
5803 TREE_OPERAND (arg1, 0))),
5804 TREE_OPERAND (arg0, 1)));
5807 else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5808 || flag_unsafe_math_optimizations)
5810 /* Except with IEEE floating point, 0-x equals -x. */
5811 if (! wins && real_zerop (arg0))
5812 return negate_expr (convert (type, arg1));
5813 /* Except with IEEE floating point, x-0 equals x. */
5814 if (real_zerop (arg1))
5815 return non_lvalue (convert (type, arg0));
5818 /* Fold &x - &x. This can happen from &x.foo - &x.
5819 This is unsafe for certain floats even in non-IEEE formats.
5820 In IEEE, it is unsafe because it does wrong for NaNs.
5821 Also note that operand_equal_p is always false if an operand
5822 is volatile. */
5824 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
5825 && operand_equal_p (arg0, arg1, 0))
5826 return convert (type, integer_zero_node);
5828 goto associate;
5830 case MULT_EXPR:
5831 /* (-A) * (-B) -> A * B */
5832 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
5833 return fold (build (MULT_EXPR, type, TREE_OPERAND (arg0, 0),
5834 TREE_OPERAND (arg1, 0)));
5836 if (! FLOAT_TYPE_P (type))
5838 if (integer_zerop (arg1))
5839 return omit_one_operand (type, arg1, arg0);
5840 if (integer_onep (arg1))
5841 return non_lvalue (convert (type, arg0));
5843 /* (a * (1 << b)) is (a << b) */
5844 if (TREE_CODE (arg1) == LSHIFT_EXPR
5845 && integer_onep (TREE_OPERAND (arg1, 0)))
5846 return fold (build (LSHIFT_EXPR, type, arg0,
5847 TREE_OPERAND (arg1, 1)));
5848 if (TREE_CODE (arg0) == LSHIFT_EXPR
5849 && integer_onep (TREE_OPERAND (arg0, 0)))
5850 return fold (build (LSHIFT_EXPR, type, arg1,
5851 TREE_OPERAND (arg0, 1)));
5853 if (TREE_CODE (arg1) == INTEGER_CST
5854 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5855 code, NULL_TREE)))
5856 return convert (type, tem);
5859 else
5861 /* x*0 is 0, except for IEEE floating point. */
5862 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5863 || flag_unsafe_math_optimizations)
5864 && real_zerop (arg1))
5865 return omit_one_operand (type, arg1, arg0);
5866 /* In IEEE floating point, x*1 is not equivalent to x for snans.
5867 However, ANSI says we can drop signals,
5868 so we can do this anyway. */
5869 if (real_onep (arg1))
5870 return non_lvalue (convert (type, arg0));
5871 /* x*2 is x+x */
5872 if (! wins && real_twop (arg1) && global_bindings_p () == 0
5873 && ! contains_placeholder_p (arg0))
5875 tree arg = save_expr (arg0);
5876 return build (PLUS_EXPR, type, arg, arg);
5879 goto associate;
5881 case BIT_IOR_EXPR:
5882 bit_ior:
5883 if (integer_all_onesp (arg1))
5884 return omit_one_operand (type, arg1, arg0);
5885 if (integer_zerop (arg1))
5886 return non_lvalue (convert (type, arg0));
5887 t1 = distribute_bit_expr (code, type, arg0, arg1);
5888 if (t1 != NULL_TREE)
5889 return t1;
5891 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
5893 This results in more efficient code for machines without a NAND
5894 instruction. Combine will canonicalize to the first form
5895 which will allow use of NAND instructions provided by the
5896 backend if they exist. */
5897 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5898 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5900 return fold (build1 (BIT_NOT_EXPR, type,
5901 build (BIT_AND_EXPR, type,
5902 TREE_OPERAND (arg0, 0),
5903 TREE_OPERAND (arg1, 0))));
5906 /* See if this can be simplified into a rotate first. If that
5907 is unsuccessful continue in the association code. */
5908 goto bit_rotate;
5910 case BIT_XOR_EXPR:
5911 if (integer_zerop (arg1))
5912 return non_lvalue (convert (type, arg0));
5913 if (integer_all_onesp (arg1))
5914 return fold (build1 (BIT_NOT_EXPR, type, arg0));
5916 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
5917 with a constant, and the two constants have no bits in common,
5918 we should treat this as a BIT_IOR_EXPR since this may produce more
5919 simplifications. */
5920 if (TREE_CODE (arg0) == BIT_AND_EXPR
5921 && TREE_CODE (arg1) == BIT_AND_EXPR
5922 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5923 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5924 && integer_zerop (const_binop (BIT_AND_EXPR,
5925 TREE_OPERAND (arg0, 1),
5926 TREE_OPERAND (arg1, 1), 0)))
5928 code = BIT_IOR_EXPR;
5929 goto bit_ior;
5932 /* See if this can be simplified into a rotate first. If that
5933 is unsuccessful continue in the association code. */
5934 goto bit_rotate;
5936 case BIT_AND_EXPR:
5937 bit_and:
5938 if (integer_all_onesp (arg1))
5939 return non_lvalue (convert (type, arg0));
5940 if (integer_zerop (arg1))
5941 return omit_one_operand (type, arg1, arg0);
5942 t1 = distribute_bit_expr (code, type, arg0, arg1);
5943 if (t1 != NULL_TREE)
5944 return t1;
5945 /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */
5946 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
5947 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
5949 unsigned int prec
5950 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
5952 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5953 && (~TREE_INT_CST_LOW (arg0)
5954 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
5955 return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
5957 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
5958 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5960 unsigned int prec
5961 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
5963 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5964 && (~TREE_INT_CST_LOW (arg1)
5965 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
5966 return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
5969 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
5971 This results in more efficient code for machines without a NOR
5972 instruction. Combine will canonicalize to the first form
5973 which will allow use of NOR instructions provided by the
5974 backend if they exist. */
5975 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5976 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5978 return fold (build1 (BIT_NOT_EXPR, type,
5979 build (BIT_IOR_EXPR, type,
5980 TREE_OPERAND (arg0, 0),
5981 TREE_OPERAND (arg1, 0))));
5984 goto associate;
5986 case BIT_ANDTC_EXPR:
5987 if (integer_all_onesp (arg0))
5988 return non_lvalue (convert (type, arg1));
5989 if (integer_zerop (arg0))
5990 return omit_one_operand (type, arg0, arg1);
5991 if (TREE_CODE (arg1) == INTEGER_CST)
5993 arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
5994 code = BIT_AND_EXPR;
5995 goto bit_and;
5997 goto binary;
5999 case RDIV_EXPR:
6000 /* In most cases, do nothing with a divide by zero. */
6001 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
6002 #ifndef REAL_INFINITY
6003 if (TREE_CODE (arg1) == REAL_CST && real_zerop (arg1))
6004 return t;
6005 #endif
6006 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
6008 /* (-A) / (-B) -> A / B */
6009 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
6010 return fold (build (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
6011 TREE_OPERAND (arg1, 0)));
6013 /* In IEEE floating point, x/1 is not equivalent to x for snans.
6014 However, ANSI says we can drop signals, so we can do this anyway. */
6015 if (real_onep (arg1))
6016 return non_lvalue (convert (type, arg0));
6018 /* If ARG1 is a constant, we can convert this to a multiply by the
6019 reciprocal. This does not have the same rounding properties,
6020 so only do this if -funsafe-math-optimizations. We can actually
6021 always safely do it if ARG1 is a power of two, but it's hard to
6022 tell if it is or not in a portable manner. */
6023 if (TREE_CODE (arg1) == REAL_CST)
6025 if (flag_unsafe_math_optimizations
6026 && 0 != (tem = const_binop (code, build_real (type, dconst1),
6027 arg1, 0)))
6028 return fold (build (MULT_EXPR, type, arg0, tem));
6029 /* Find the reciprocal if optimizing and the result is exact. */
6030 else if (optimize)
6032 REAL_VALUE_TYPE r;
6033 r = TREE_REAL_CST (arg1);
6034 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
6036 tem = build_real (type, r);
6037 return fold (build (MULT_EXPR, type, arg0, tem));
6041 /* Convert A/B/C to A/(B*C). */
6042 if (flag_unsafe_math_optimizations
6043 && TREE_CODE (arg0) == RDIV_EXPR)
6045 return fold (build (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
6046 build (MULT_EXPR, type, TREE_OPERAND (arg0, 1),
6047 arg1)));
6049 /* Convert A/(B/C) to (A/B)*C. */
6050 if (flag_unsafe_math_optimizations
6051 && TREE_CODE (arg1) == RDIV_EXPR)
6053 return fold (build (MULT_EXPR, type,
6054 build (RDIV_EXPR, type, arg0,
6055 TREE_OPERAND (arg1, 0)),
6056 TREE_OPERAND (arg1, 1)));
6058 goto binary;
6060 case TRUNC_DIV_EXPR:
6061 case ROUND_DIV_EXPR:
6062 case FLOOR_DIV_EXPR:
6063 case CEIL_DIV_EXPR:
6064 case EXACT_DIV_EXPR:
6065 if (integer_onep (arg1))
6066 return non_lvalue (convert (type, arg0));
6067 if (integer_zerop (arg1))
6068 return t;
6070 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
6071 operation, EXACT_DIV_EXPR.
6073 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
6074 At one time others generated faster code, it's not clear if they do
6075 after the last round to changes to the DIV code in expmed.c. */
6076 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
6077 && multiple_of_p (type, arg0, arg1))
6078 return fold (build (EXACT_DIV_EXPR, type, arg0, arg1));
6080 if (TREE_CODE (arg1) == INTEGER_CST
6081 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
6082 code, NULL_TREE)))
6083 return convert (type, tem);
6085 goto binary;
6087 case CEIL_MOD_EXPR:
6088 case FLOOR_MOD_EXPR:
6089 case ROUND_MOD_EXPR:
6090 case TRUNC_MOD_EXPR:
6091 if (integer_onep (arg1))
6092 return omit_one_operand (type, integer_zero_node, arg0);
6093 if (integer_zerop (arg1))
6094 return t;
6096 if (TREE_CODE (arg1) == INTEGER_CST
6097 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
6098 code, NULL_TREE)))
6099 return convert (type, tem);
6101 goto binary;
6103 case LSHIFT_EXPR:
6104 case RSHIFT_EXPR:
6105 case LROTATE_EXPR:
6106 case RROTATE_EXPR:
6107 if (integer_zerop (arg1))
6108 return non_lvalue (convert (type, arg0));
6109 /* Since negative shift count is not well-defined,
6110 don't try to compute it in the compiler. */
6111 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
6112 return t;
6113 /* Rewrite an LROTATE_EXPR by a constant into an
6114 RROTATE_EXPR by a new constant. */
6115 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
6117 TREE_SET_CODE (t, RROTATE_EXPR);
6118 code = RROTATE_EXPR;
6119 TREE_OPERAND (t, 1) = arg1
6120 = const_binop
6121 (MINUS_EXPR,
6122 convert (TREE_TYPE (arg1),
6123 build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0)),
6124 arg1, 0);
6125 if (tree_int_cst_sgn (arg1) < 0)
6126 return t;
6129 /* If we have a rotate of a bit operation with the rotate count and
6130 the second operand of the bit operation both constant,
6131 permute the two operations. */
6132 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6133 && (TREE_CODE (arg0) == BIT_AND_EXPR
6134 || TREE_CODE (arg0) == BIT_ANDTC_EXPR
6135 || TREE_CODE (arg0) == BIT_IOR_EXPR
6136 || TREE_CODE (arg0) == BIT_XOR_EXPR)
6137 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
6138 return fold (build (TREE_CODE (arg0), type,
6139 fold (build (code, type,
6140 TREE_OPERAND (arg0, 0), arg1)),
6141 fold (build (code, type,
6142 TREE_OPERAND (arg0, 1), arg1))));
6144 /* Two consecutive rotates adding up to the width of the mode can
6145 be ignored. */
6146 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6147 && TREE_CODE (arg0) == RROTATE_EXPR
6148 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6149 && TREE_INT_CST_HIGH (arg1) == 0
6150 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
6151 && ((TREE_INT_CST_LOW (arg1)
6152 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
6153 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
6154 return TREE_OPERAND (arg0, 0);
6156 goto binary;
6158 case MIN_EXPR:
6159 if (operand_equal_p (arg0, arg1, 0))
6160 return omit_one_operand (type, arg0, arg1);
6161 if (INTEGRAL_TYPE_P (type)
6162 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
6163 return omit_one_operand (type, arg1, arg0);
6164 goto associate;
6166 case MAX_EXPR:
6167 if (operand_equal_p (arg0, arg1, 0))
6168 return omit_one_operand (type, arg0, arg1);
6169 if (INTEGRAL_TYPE_P (type)
6170 && TYPE_MAX_VALUE (type)
6171 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
6172 return omit_one_operand (type, arg1, arg0);
6173 goto associate;
6175 case TRUTH_NOT_EXPR:
6176 /* Note that the operand of this must be an int
6177 and its values must be 0 or 1.
6178 ("true" is a fixed value perhaps depending on the language,
6179 but we don't handle values other than 1 correctly yet.) */
6180 tem = invert_truthvalue (arg0);
6181 /* Avoid infinite recursion. */
6182 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
6183 return t;
6184 return convert (type, tem);
6186 case TRUTH_ANDIF_EXPR:
6187 /* Note that the operands of this must be ints
6188 and their values must be 0 or 1.
6189 ("true" is a fixed value perhaps depending on the language.) */
6190 /* If first arg is constant zero, return it. */
6191 if (integer_zerop (arg0))
6192 return convert (type, arg0);
6193 case TRUTH_AND_EXPR:
6194 /* If either arg is constant true, drop it. */
6195 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6196 return non_lvalue (convert (type, arg1));
6197 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
6198 /* Preserve sequence points. */
6199 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
6200 return non_lvalue (convert (type, arg0));
6201 /* If second arg is constant zero, result is zero, but first arg
6202 must be evaluated. */
6203 if (integer_zerop (arg1))
6204 return omit_one_operand (type, arg1, arg0);
6205 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
6206 case will be handled here. */
6207 if (integer_zerop (arg0))
6208 return omit_one_operand (type, arg0, arg1);
6210 truth_andor:
6211 /* We only do these simplifications if we are optimizing. */
6212 if (!optimize)
6213 return t;
6215 /* Check for things like (A || B) && (A || C). We can convert this
6216 to A || (B && C). Note that either operator can be any of the four
6217 truth and/or operations and the transformation will still be
6218 valid. Also note that we only care about order for the
6219 ANDIF and ORIF operators. If B contains side effects, this
6220 might change the truth-value of A. */
6221 if (TREE_CODE (arg0) == TREE_CODE (arg1)
6222 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
6223 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
6224 || TREE_CODE (arg0) == TRUTH_AND_EXPR
6225 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
6226 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
6228 tree a00 = TREE_OPERAND (arg0, 0);
6229 tree a01 = TREE_OPERAND (arg0, 1);
6230 tree a10 = TREE_OPERAND (arg1, 0);
6231 tree a11 = TREE_OPERAND (arg1, 1);
6232 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
6233 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
6234 && (code == TRUTH_AND_EXPR
6235 || code == TRUTH_OR_EXPR));
6237 if (operand_equal_p (a00, a10, 0))
6238 return fold (build (TREE_CODE (arg0), type, a00,
6239 fold (build (code, type, a01, a11))));
6240 else if (commutative && operand_equal_p (a00, a11, 0))
6241 return fold (build (TREE_CODE (arg0), type, a00,
6242 fold (build (code, type, a01, a10))));
6243 else if (commutative && operand_equal_p (a01, a10, 0))
6244 return fold (build (TREE_CODE (arg0), type, a01,
6245 fold (build (code, type, a00, a11))));
6247 /* This case if tricky because we must either have commutative
6248 operators or else A10 must not have side-effects. */
6250 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
6251 && operand_equal_p (a01, a11, 0))
6252 return fold (build (TREE_CODE (arg0), type,
6253 fold (build (code, type, a00, a10)),
6254 a01));
6257 /* See if we can build a range comparison. */
6258 if (0 != (tem = fold_range_test (t)))
6259 return tem;
6261 /* Check for the possibility of merging component references. If our
6262 lhs is another similar operation, try to merge its rhs with our
6263 rhs. Then try to merge our lhs and rhs. */
6264 if (TREE_CODE (arg0) == code
6265 && 0 != (tem = fold_truthop (code, type,
6266 TREE_OPERAND (arg0, 1), arg1)))
6267 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6269 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
6270 return tem;
6272 return t;
6274 case TRUTH_ORIF_EXPR:
6275 /* Note that the operands of this must be ints
6276 and their values must be 0 or true.
6277 ("true" is a fixed value perhaps depending on the language.) */
6278 /* If first arg is constant true, return it. */
6279 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6280 return convert (type, arg0);
6281 case TRUTH_OR_EXPR:
6282 /* If either arg is constant zero, drop it. */
6283 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
6284 return non_lvalue (convert (type, arg1));
6285 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
6286 /* Preserve sequence points. */
6287 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
6288 return non_lvalue (convert (type, arg0));
6289 /* If second arg is constant true, result is true, but we must
6290 evaluate first arg. */
6291 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
6292 return omit_one_operand (type, arg1, arg0);
6293 /* Likewise for first arg, but note this only occurs here for
6294 TRUTH_OR_EXPR. */
6295 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6296 return omit_one_operand (type, arg0, arg1);
6297 goto truth_andor;
6299 case TRUTH_XOR_EXPR:
6300 /* If either arg is constant zero, drop it. */
6301 if (integer_zerop (arg0))
6302 return non_lvalue (convert (type, arg1));
6303 if (integer_zerop (arg1))
6304 return non_lvalue (convert (type, arg0));
6305 /* If either arg is constant true, this is a logical inversion. */
6306 if (integer_onep (arg0))
6307 return non_lvalue (convert (type, invert_truthvalue (arg1)));
6308 if (integer_onep (arg1))
6309 return non_lvalue (convert (type, invert_truthvalue (arg0)));
6310 return t;
6312 case EQ_EXPR:
6313 case NE_EXPR:
6314 case LT_EXPR:
6315 case GT_EXPR:
6316 case LE_EXPR:
6317 case GE_EXPR:
6318 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
6320 /* (-a) CMP (-b) -> b CMP a */
6321 if (TREE_CODE (arg0) == NEGATE_EXPR
6322 && TREE_CODE (arg1) == NEGATE_EXPR)
6323 return fold (build (code, type, TREE_OPERAND (arg1, 0),
6324 TREE_OPERAND (arg0, 0)));
6325 /* (-a) CMP CST -> a swap(CMP) (-CST) */
6326 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == REAL_CST)
6327 return
6328 fold (build
6329 (swap_tree_comparison (code), type,
6330 TREE_OPERAND (arg0, 0),
6331 build_real (TREE_TYPE (arg1),
6332 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1)))));
6333 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6334 /* a CMP (-0) -> a CMP 0 */
6335 if (TREE_CODE (arg1) == REAL_CST
6336 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1)))
6337 return fold (build (code, type, arg0,
6338 build_real (TREE_TYPE (arg1), dconst0)));
6341 /* If one arg is a constant integer, put it last. */
6342 if (TREE_CODE (arg0) == INTEGER_CST
6343 && TREE_CODE (arg1) != INTEGER_CST)
6345 TREE_OPERAND (t, 0) = arg1;
6346 TREE_OPERAND (t, 1) = arg0;
6347 arg0 = TREE_OPERAND (t, 0);
6348 arg1 = TREE_OPERAND (t, 1);
6349 code = swap_tree_comparison (code);
6350 TREE_SET_CODE (t, code);
6353 /* Convert foo++ == CONST into ++foo == CONST + INCR.
6354 First, see if one arg is constant; find the constant arg
6355 and the other one. */
6357 tree constop = 0, varop = NULL_TREE;
6358 int constopnum = -1;
6360 if (TREE_CONSTANT (arg1))
6361 constopnum = 1, constop = arg1, varop = arg0;
6362 if (TREE_CONSTANT (arg0))
6363 constopnum = 0, constop = arg0, varop = arg1;
6365 if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
6367 /* This optimization is invalid for ordered comparisons
6368 if CONST+INCR overflows or if foo+incr might overflow.
6369 This optimization is invalid for floating point due to rounding.
6370 For pointer types we assume overflow doesn't happen. */
6371 if (POINTER_TYPE_P (TREE_TYPE (varop))
6372 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
6373 && (code == EQ_EXPR || code == NE_EXPR)))
6375 tree newconst
6376 = fold (build (PLUS_EXPR, TREE_TYPE (varop),
6377 constop, TREE_OPERAND (varop, 1)));
6379 /* Do not overwrite the current varop to be a preincrement,
6380 create a new node so that we won't confuse our caller who
6381 might create trees and throw them away, reusing the
6382 arguments that they passed to build. This shows up in
6383 the THEN or ELSE parts of ?: being postincrements. */
6384 varop = build (PREINCREMENT_EXPR, TREE_TYPE (varop),
6385 TREE_OPERAND (varop, 0),
6386 TREE_OPERAND (varop, 1));
6388 /* If VAROP is a reference to a bitfield, we must mask
6389 the constant by the width of the field. */
6390 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6391 && DECL_BIT_FIELD(TREE_OPERAND
6392 (TREE_OPERAND (varop, 0), 1)))
6394 int size
6395 = TREE_INT_CST_LOW (DECL_SIZE
6396 (TREE_OPERAND
6397 (TREE_OPERAND (varop, 0), 1)));
6398 tree mask, unsigned_type;
6399 unsigned int precision;
6400 tree folded_compare;
6402 /* First check whether the comparison would come out
6403 always the same. If we don't do that we would
6404 change the meaning with the masking. */
6405 if (constopnum == 0)
6406 folded_compare = fold (build (code, type, constop,
6407 TREE_OPERAND (varop, 0)));
6408 else
6409 folded_compare = fold (build (code, type,
6410 TREE_OPERAND (varop, 0),
6411 constop));
6412 if (integer_zerop (folded_compare)
6413 || integer_onep (folded_compare))
6414 return omit_one_operand (type, folded_compare, varop);
6416 unsigned_type = type_for_size (size, 1);
6417 precision = TYPE_PRECISION (unsigned_type);
6418 mask = build_int_2 (~0, ~0);
6419 TREE_TYPE (mask) = unsigned_type;
6420 force_fit_type (mask, 0);
6421 mask = const_binop (RSHIFT_EXPR, mask,
6422 size_int (precision - size), 0);
6423 newconst = fold (build (BIT_AND_EXPR,
6424 TREE_TYPE (varop), newconst,
6425 convert (TREE_TYPE (varop),
6426 mask)));
6429 t = build (code, type,
6430 (constopnum == 0) ? newconst : varop,
6431 (constopnum == 1) ? newconst : varop);
6432 return t;
6435 else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
6437 if (POINTER_TYPE_P (TREE_TYPE (varop))
6438 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
6439 && (code == EQ_EXPR || code == NE_EXPR)))
6441 tree newconst
6442 = fold (build (MINUS_EXPR, TREE_TYPE (varop),
6443 constop, TREE_OPERAND (varop, 1)));
6445 /* Do not overwrite the current varop to be a predecrement,
6446 create a new node so that we won't confuse our caller who
6447 might create trees and throw them away, reusing the
6448 arguments that they passed to build. This shows up in
6449 the THEN or ELSE parts of ?: being postdecrements. */
6450 varop = build (PREDECREMENT_EXPR, TREE_TYPE (varop),
6451 TREE_OPERAND (varop, 0),
6452 TREE_OPERAND (varop, 1));
6454 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6455 && DECL_BIT_FIELD(TREE_OPERAND
6456 (TREE_OPERAND (varop, 0), 1)))
6458 int size
6459 = TREE_INT_CST_LOW (DECL_SIZE
6460 (TREE_OPERAND
6461 (TREE_OPERAND (varop, 0), 1)));
6462 tree mask, unsigned_type;
6463 unsigned int precision;
6464 tree folded_compare;
6466 if (constopnum == 0)
6467 folded_compare = fold (build (code, type, constop,
6468 TREE_OPERAND (varop, 0)));
6469 else
6470 folded_compare = fold (build (code, type,
6471 TREE_OPERAND (varop, 0),
6472 constop));
6473 if (integer_zerop (folded_compare)
6474 || integer_onep (folded_compare))
6475 return omit_one_operand (type, folded_compare, varop);
6477 unsigned_type = type_for_size (size, 1);
6478 precision = TYPE_PRECISION (unsigned_type);
6479 mask = build_int_2 (~0, ~0);
6480 TREE_TYPE (mask) = TREE_TYPE (varop);
6481 force_fit_type (mask, 0);
6482 mask = const_binop (RSHIFT_EXPR, mask,
6483 size_int (precision - size), 0);
6484 newconst = fold (build (BIT_AND_EXPR,
6485 TREE_TYPE (varop), newconst,
6486 convert (TREE_TYPE (varop),
6487 mask)));
6490 t = build (code, type,
6491 (constopnum == 0) ? newconst : varop,
6492 (constopnum == 1) ? newconst : varop);
6493 return t;
6498 /* Comparisons with the highest or lowest possible integer of
6499 the specified size will have known values and an unsigned
6500 <= 0x7fffffff can be simplified. */
6502 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
6504 if (TREE_CODE (arg1) == INTEGER_CST
6505 && ! TREE_CONSTANT_OVERFLOW (arg1)
6506 && width <= HOST_BITS_PER_WIDE_INT
6507 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6508 || POINTER_TYPE_P (TREE_TYPE (arg1))))
6510 if (TREE_INT_CST_HIGH (arg1) == 0
6511 && (TREE_INT_CST_LOW (arg1)
6512 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1)
6513 && ! TREE_UNSIGNED (TREE_TYPE (arg1)))
6514 switch (TREE_CODE (t))
6516 case GT_EXPR:
6517 return omit_one_operand (type,
6518 convert (type, integer_zero_node),
6519 arg0);
6520 case GE_EXPR:
6521 TREE_SET_CODE (t, EQ_EXPR);
6522 break;
6524 case LE_EXPR:
6525 return omit_one_operand (type,
6526 convert (type, integer_one_node),
6527 arg0);
6528 case LT_EXPR:
6529 TREE_SET_CODE (t, NE_EXPR);
6530 break;
6532 default:
6533 break;
6536 else if (TREE_INT_CST_HIGH (arg1) == -1
6537 && (TREE_INT_CST_LOW (arg1)
6538 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)))
6539 && ! TREE_UNSIGNED (TREE_TYPE (arg1)))
6540 switch (TREE_CODE (t))
6542 case LT_EXPR:
6543 return omit_one_operand (type,
6544 convert (type, integer_zero_node),
6545 arg0);
6546 case LE_EXPR:
6547 TREE_SET_CODE (t, EQ_EXPR);
6548 break;
6550 case GE_EXPR:
6551 return omit_one_operand (type,
6552 convert (type, integer_one_node),
6553 arg0);
6554 case GT_EXPR:
6555 TREE_SET_CODE (t, NE_EXPR);
6556 break;
6558 default:
6559 break;
6562 else if (TREE_INT_CST_HIGH (arg1) == 0
6563 && (TREE_INT_CST_LOW (arg1)
6564 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1)
6565 && TREE_UNSIGNED (TREE_TYPE (arg1))
6566 /* signed_type does not work on pointer types. */
6567 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
6568 switch (TREE_CODE (t))
6570 case LE_EXPR:
6571 return fold (build (GE_EXPR, type,
6572 convert (signed_type (TREE_TYPE (arg0)),
6573 arg0),
6574 convert (signed_type (TREE_TYPE (arg1)),
6575 integer_zero_node)));
6576 case GT_EXPR:
6577 return fold (build (LT_EXPR, type,
6578 convert (signed_type (TREE_TYPE (arg0)),
6579 arg0),
6580 convert (signed_type (TREE_TYPE (arg1)),
6581 integer_zero_node)));
6583 default:
6584 break;
6587 else if (TREE_INT_CST_HIGH (arg1) == 0
6588 && (TREE_INT_CST_LOW (arg1)
6589 == ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1)
6590 && TREE_UNSIGNED (TREE_TYPE (arg1)))
6591 switch (TREE_CODE (t))
6593 case GT_EXPR:
6594 return omit_one_operand (type,
6595 convert (type, integer_zero_node),
6596 arg0);
6597 case GE_EXPR:
6598 TREE_SET_CODE (t, EQ_EXPR);
6599 break;
6601 case LE_EXPR:
6602 return omit_one_operand (type,
6603 convert (type, integer_one_node),
6604 arg0);
6605 case LT_EXPR:
6606 TREE_SET_CODE (t, NE_EXPR);
6607 break;
6609 default:
6610 break;
6615 /* Change X >= CST to X > (CST - 1) and X < CST to X <= (CST - 1)
6616 if CST is positive. */
6617 if (TREE_CODE (arg1) == INTEGER_CST
6618 && TREE_CODE (arg0) != INTEGER_CST
6619 && tree_int_cst_sgn (arg1) > 0)
6621 switch (TREE_CODE (t))
6623 case GE_EXPR:
6624 code = GT_EXPR;
6625 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6626 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6627 break;
6629 case LT_EXPR:
6630 code = LE_EXPR;
6631 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6632 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6633 break;
6635 default:
6636 break;
6640 /* An unsigned comparison against 0 can be simplified. */
6641 if (integer_zerop (arg1)
6642 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6643 || POINTER_TYPE_P (TREE_TYPE (arg1)))
6644 && TREE_UNSIGNED (TREE_TYPE (arg1)))
6646 switch (TREE_CODE (t))
6648 case GT_EXPR:
6649 code = NE_EXPR;
6650 TREE_SET_CODE (t, NE_EXPR);
6651 break;
6652 case LE_EXPR:
6653 code = EQ_EXPR;
6654 TREE_SET_CODE (t, EQ_EXPR);
6655 break;
6656 case GE_EXPR:
6657 return omit_one_operand (type,
6658 convert (type, integer_one_node),
6659 arg0);
6660 case LT_EXPR:
6661 return omit_one_operand (type,
6662 convert (type, integer_zero_node),
6663 arg0);
6664 default:
6665 break;
6669 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
6670 a MINUS_EXPR of a constant, we can convert it into a comparison with
6671 a revised constant as long as no overflow occurs. */
6672 if ((code == EQ_EXPR || code == NE_EXPR)
6673 && TREE_CODE (arg1) == INTEGER_CST
6674 && (TREE_CODE (arg0) == PLUS_EXPR
6675 || TREE_CODE (arg0) == MINUS_EXPR)
6676 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6677 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
6678 ? MINUS_EXPR : PLUS_EXPR,
6679 arg1, TREE_OPERAND (arg0, 1), 0))
6680 && ! TREE_CONSTANT_OVERFLOW (tem))
6681 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6683 /* Similarly for a NEGATE_EXPR. */
6684 else if ((code == EQ_EXPR || code == NE_EXPR)
6685 && TREE_CODE (arg0) == NEGATE_EXPR
6686 && TREE_CODE (arg1) == INTEGER_CST
6687 && 0 != (tem = negate_expr (arg1))
6688 && TREE_CODE (tem) == INTEGER_CST
6689 && ! TREE_CONSTANT_OVERFLOW (tem))
6690 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6692 /* If we have X - Y == 0, we can convert that to X == Y and similarly
6693 for !=. Don't do this for ordered comparisons due to overflow. */
6694 else if ((code == NE_EXPR || code == EQ_EXPR)
6695 && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
6696 return fold (build (code, type,
6697 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
6699 /* If we are widening one operand of an integer comparison,
6700 see if the other operand is similarly being widened. Perhaps we
6701 can do the comparison in the narrower type. */
6702 else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
6703 && TREE_CODE (arg0) == NOP_EXPR
6704 && (tem = get_unwidened (arg0, NULL_TREE)) != arg0
6705 && (t1 = get_unwidened (arg1, TREE_TYPE (tem))) != 0
6706 && (TREE_TYPE (t1) == TREE_TYPE (tem)
6707 || (TREE_CODE (t1) == INTEGER_CST
6708 && int_fits_type_p (t1, TREE_TYPE (tem)))))
6709 return fold (build (code, type, tem, convert (TREE_TYPE (tem), t1)));
6711 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
6712 constant, we can simplify it. */
6713 else if (TREE_CODE (arg1) == INTEGER_CST
6714 && (TREE_CODE (arg0) == MIN_EXPR
6715 || TREE_CODE (arg0) == MAX_EXPR)
6716 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
6717 return optimize_minmax_comparison (t);
6719 /* If we are comparing an ABS_EXPR with a constant, we can
6720 convert all the cases into explicit comparisons, but they may
6721 well not be faster than doing the ABS and one comparison.
6722 But ABS (X) <= C is a range comparison, which becomes a subtraction
6723 and a comparison, and is probably faster. */
6724 else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6725 && TREE_CODE (arg0) == ABS_EXPR
6726 && ! TREE_SIDE_EFFECTS (arg0)
6727 && (0 != (tem = negate_expr (arg1)))
6728 && TREE_CODE (tem) == INTEGER_CST
6729 && ! TREE_CONSTANT_OVERFLOW (tem))
6730 return fold (build (TRUTH_ANDIF_EXPR, type,
6731 build (GE_EXPR, type, TREE_OPERAND (arg0, 0), tem),
6732 build (LE_EXPR, type,
6733 TREE_OPERAND (arg0, 0), arg1)));
6735 /* If this is an EQ or NE comparison with zero and ARG0 is
6736 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
6737 two operations, but the latter can be done in one less insn
6738 on machines that have only two-operand insns or on which a
6739 constant cannot be the first operand. */
6740 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
6741 && TREE_CODE (arg0) == BIT_AND_EXPR)
6743 if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
6744 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
6745 return
6746 fold (build (code, type,
6747 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6748 build (RSHIFT_EXPR,
6749 TREE_TYPE (TREE_OPERAND (arg0, 0)),
6750 TREE_OPERAND (arg0, 1),
6751 TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
6752 convert (TREE_TYPE (arg0),
6753 integer_one_node)),
6754 arg1));
6755 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
6756 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
6757 return
6758 fold (build (code, type,
6759 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6760 build (RSHIFT_EXPR,
6761 TREE_TYPE (TREE_OPERAND (arg0, 1)),
6762 TREE_OPERAND (arg0, 0),
6763 TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
6764 convert (TREE_TYPE (arg0),
6765 integer_one_node)),
6766 arg1));
6769 /* If this is an NE or EQ comparison of zero against the result of a
6770 signed MOD operation whose second operand is a power of 2, make
6771 the MOD operation unsigned since it is simpler and equivalent. */
6772 if ((code == NE_EXPR || code == EQ_EXPR)
6773 && integer_zerop (arg1)
6774 && ! TREE_UNSIGNED (TREE_TYPE (arg0))
6775 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
6776 || TREE_CODE (arg0) == CEIL_MOD_EXPR
6777 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
6778 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
6779 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6781 tree newtype = unsigned_type (TREE_TYPE (arg0));
6782 tree newmod = build (TREE_CODE (arg0), newtype,
6783 convert (newtype, TREE_OPERAND (arg0, 0)),
6784 convert (newtype, TREE_OPERAND (arg0, 1)));
6786 return build (code, type, newmod, convert (newtype, arg1));
6789 /* If this is an NE comparison of zero with an AND of one, remove the
6790 comparison since the AND will give the correct value. */
6791 if (code == NE_EXPR && integer_zerop (arg1)
6792 && TREE_CODE (arg0) == BIT_AND_EXPR
6793 && integer_onep (TREE_OPERAND (arg0, 1)))
6794 return convert (type, arg0);
6796 /* If we have (A & C) == C where C is a power of 2, convert this into
6797 (A & C) != 0. Similarly for NE_EXPR. */
6798 if ((code == EQ_EXPR || code == NE_EXPR)
6799 && TREE_CODE (arg0) == BIT_AND_EXPR
6800 && integer_pow2p (TREE_OPERAND (arg0, 1))
6801 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
6802 return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
6803 arg0, integer_zero_node);
6805 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
6806 and similarly for >= into !=. */
6807 if ((code == LT_EXPR || code == GE_EXPR)
6808 && TREE_UNSIGNED (TREE_TYPE (arg0))
6809 && TREE_CODE (arg1) == LSHIFT_EXPR
6810 && integer_onep (TREE_OPERAND (arg1, 0)))
6811 return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
6812 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6813 TREE_OPERAND (arg1, 1)),
6814 convert (TREE_TYPE (arg0), integer_zero_node));
6816 else if ((code == LT_EXPR || code == GE_EXPR)
6817 && TREE_UNSIGNED (TREE_TYPE (arg0))
6818 && (TREE_CODE (arg1) == NOP_EXPR
6819 || TREE_CODE (arg1) == CONVERT_EXPR)
6820 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
6821 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
6822 return
6823 build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
6824 convert (TREE_TYPE (arg0),
6825 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6826 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))),
6827 convert (TREE_TYPE (arg0), integer_zero_node));
6829 /* Simplify comparison of something with itself. (For IEEE
6830 floating-point, we can only do some of these simplifications.) */
6831 if (operand_equal_p (arg0, arg1, 0))
6833 switch (code)
6835 case EQ_EXPR:
6836 case GE_EXPR:
6837 case LE_EXPR:
6838 if (! FLOAT_TYPE_P (TREE_TYPE (arg0)))
6839 return constant_boolean_node (1, type);
6840 code = EQ_EXPR;
6841 TREE_SET_CODE (t, code);
6842 break;
6844 case NE_EXPR:
6845 /* For NE, we can only do this simplification if integer. */
6846 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
6847 break;
6848 /* ... fall through ... */
6849 case GT_EXPR:
6850 case LT_EXPR:
6851 return constant_boolean_node (0, type);
6852 default:
6853 abort ();
6857 /* If we are comparing an expression that just has comparisons
6858 of two integer values, arithmetic expressions of those comparisons,
6859 and constants, we can simplify it. There are only three cases
6860 to check: the two values can either be equal, the first can be
6861 greater, or the second can be greater. Fold the expression for
6862 those three values. Since each value must be 0 or 1, we have
6863 eight possibilities, each of which corresponds to the constant 0
6864 or 1 or one of the six possible comparisons.
6866 This handles common cases like (a > b) == 0 but also handles
6867 expressions like ((x > y) - (y > x)) > 0, which supposedly
6868 occur in macroized code. */
6870 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
6872 tree cval1 = 0, cval2 = 0;
6873 int save_p = 0;
6875 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
6876 /* Don't handle degenerate cases here; they should already
6877 have been handled anyway. */
6878 && cval1 != 0 && cval2 != 0
6879 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
6880 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
6881 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
6882 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
6883 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
6884 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
6885 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
6887 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
6888 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
6890 /* We can't just pass T to eval_subst in case cval1 or cval2
6891 was the same as ARG1. */
6893 tree high_result
6894 = fold (build (code, type,
6895 eval_subst (arg0, cval1, maxval, cval2, minval),
6896 arg1));
6897 tree equal_result
6898 = fold (build (code, type,
6899 eval_subst (arg0, cval1, maxval, cval2, maxval),
6900 arg1));
6901 tree low_result
6902 = fold (build (code, type,
6903 eval_subst (arg0, cval1, minval, cval2, maxval),
6904 arg1));
6906 /* All three of these results should be 0 or 1. Confirm they
6907 are. Then use those values to select the proper code
6908 to use. */
6910 if ((integer_zerop (high_result)
6911 || integer_onep (high_result))
6912 && (integer_zerop (equal_result)
6913 || integer_onep (equal_result))
6914 && (integer_zerop (low_result)
6915 || integer_onep (low_result)))
6917 /* Make a 3-bit mask with the high-order bit being the
6918 value for `>', the next for '=', and the low for '<'. */
6919 switch ((integer_onep (high_result) * 4)
6920 + (integer_onep (equal_result) * 2)
6921 + integer_onep (low_result))
6923 case 0:
6924 /* Always false. */
6925 return omit_one_operand (type, integer_zero_node, arg0);
6926 case 1:
6927 code = LT_EXPR;
6928 break;
6929 case 2:
6930 code = EQ_EXPR;
6931 break;
6932 case 3:
6933 code = LE_EXPR;
6934 break;
6935 case 4:
6936 code = GT_EXPR;
6937 break;
6938 case 5:
6939 code = NE_EXPR;
6940 break;
6941 case 6:
6942 code = GE_EXPR;
6943 break;
6944 case 7:
6945 /* Always true. */
6946 return omit_one_operand (type, integer_one_node, arg0);
6949 t = build (code, type, cval1, cval2);
6950 if (save_p)
6951 return save_expr (t);
6952 else
6953 return fold (t);
6958 /* If this is a comparison of a field, we may be able to simplify it. */
6959 if ((TREE_CODE (arg0) == COMPONENT_REF
6960 || TREE_CODE (arg0) == BIT_FIELD_REF)
6961 && (code == EQ_EXPR || code == NE_EXPR)
6962 /* Handle the constant case even without -O
6963 to make sure the warnings are given. */
6964 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
6966 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
6967 return t1 ? t1 : t;
6970 /* If this is a comparison of complex values and either or both sides
6971 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
6972 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
6973 This may prevent needless evaluations. */
6974 if ((code == EQ_EXPR || code == NE_EXPR)
6975 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
6976 && (TREE_CODE (arg0) == COMPLEX_EXPR
6977 || TREE_CODE (arg1) == COMPLEX_EXPR
6978 || TREE_CODE (arg0) == COMPLEX_CST
6979 || TREE_CODE (arg1) == COMPLEX_CST))
6981 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
6982 tree real0, imag0, real1, imag1;
6984 arg0 = save_expr (arg0);
6985 arg1 = save_expr (arg1);
6986 real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
6987 imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
6988 real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
6989 imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
6991 return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
6992 : TRUTH_ORIF_EXPR),
6993 type,
6994 fold (build (code, type, real0, real1)),
6995 fold (build (code, type, imag0, imag1))));
6998 /* Optimize comparisons of strlen vs zero to a compare of the
6999 first character of the string vs zero. To wit,
7000 strlen(ptr) == 0 => *ptr == 0
7001 strlen(ptr) != 0 => *ptr != 0
7002 Other cases should reduce to one of these two (or a constant)
7003 due to the return value of strlen being unsigned. */
7004 if ((code == EQ_EXPR || code == NE_EXPR)
7005 && integer_zerop (arg1)
7006 && TREE_CODE (arg0) == CALL_EXPR
7007 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR)
7009 tree fndecl = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7010 tree arglist;
7012 if (TREE_CODE (fndecl) == FUNCTION_DECL
7013 && DECL_BUILT_IN (fndecl)
7014 && DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD
7015 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
7016 && (arglist = TREE_OPERAND (arg0, 1))
7017 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
7018 && ! TREE_CHAIN (arglist))
7019 return fold (build (code, type,
7020 build1 (INDIRECT_REF, char_type_node,
7021 TREE_VALUE(arglist)),
7022 integer_zero_node));
7025 /* From here on, the only cases we handle are when the result is
7026 known to be a constant.
7028 To compute GT, swap the arguments and do LT.
7029 To compute GE, do LT and invert the result.
7030 To compute LE, swap the arguments, do LT and invert the result.
7031 To compute NE, do EQ and invert the result.
7033 Therefore, the code below must handle only EQ and LT. */
7035 if (code == LE_EXPR || code == GT_EXPR)
7037 tem = arg0, arg0 = arg1, arg1 = tem;
7038 code = swap_tree_comparison (code);
7041 /* Note that it is safe to invert for real values here because we
7042 will check below in the one case that it matters. */
7044 t1 = NULL_TREE;
7045 invert = 0;
7046 if (code == NE_EXPR || code == GE_EXPR)
7048 invert = 1;
7049 code = invert_tree_comparison (code);
7052 /* Compute a result for LT or EQ if args permit;
7053 otherwise return T. */
7054 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
7056 if (code == EQ_EXPR)
7057 t1 = build_int_2 (tree_int_cst_equal (arg0, arg1), 0);
7058 else
7059 t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
7060 ? INT_CST_LT_UNSIGNED (arg0, arg1)
7061 : INT_CST_LT (arg0, arg1)),
7065 #if 0 /* This is no longer useful, but breaks some real code. */
7066 /* Assume a nonexplicit constant cannot equal an explicit one,
7067 since such code would be undefined anyway.
7068 Exception: on sysvr4, using #pragma weak,
7069 a label can come out as 0. */
7070 else if (TREE_CODE (arg1) == INTEGER_CST
7071 && !integer_zerop (arg1)
7072 && TREE_CONSTANT (arg0)
7073 && TREE_CODE (arg0) == ADDR_EXPR
7074 && code == EQ_EXPR)
7075 t1 = build_int_2 (0, 0);
7076 #endif
7077 /* Two real constants can be compared explicitly. */
7078 else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
7080 /* If either operand is a NaN, the result is false with two
7081 exceptions: First, an NE_EXPR is true on NaNs, but that case
7082 is already handled correctly since we will be inverting the
7083 result for NE_EXPR. Second, if we had inverted a LE_EXPR
7084 or a GE_EXPR into a LT_EXPR, we must return true so that it
7085 will be inverted into false. */
7087 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
7088 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
7089 t1 = build_int_2 (invert && code == LT_EXPR, 0);
7091 else if (code == EQ_EXPR)
7092 t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
7093 TREE_REAL_CST (arg1)),
7095 else
7096 t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
7097 TREE_REAL_CST (arg1)),
7101 if (t1 == NULL_TREE)
7102 return t;
7104 if (invert)
7105 TREE_INT_CST_LOW (t1) ^= 1;
7107 TREE_TYPE (t1) = type;
7108 if (TREE_CODE (type) == BOOLEAN_TYPE)
7109 return truthvalue_conversion (t1);
7110 return t1;
7112 case COND_EXPR:
7113 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
7114 so all simple results must be passed through pedantic_non_lvalue. */
7115 if (TREE_CODE (arg0) == INTEGER_CST)
7116 return pedantic_non_lvalue
7117 (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
7118 else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
7119 return pedantic_omit_one_operand (type, arg1, arg0);
7121 /* If the second operand is zero, invert the comparison and swap
7122 the second and third operands. Likewise if the second operand
7123 is constant and the third is not or if the third operand is
7124 equivalent to the first operand of the comparison. */
7126 if (integer_zerop (arg1)
7127 || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
7128 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
7129 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
7130 TREE_OPERAND (t, 2),
7131 TREE_OPERAND (arg0, 1))))
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 /* If we have A op B ? A : C, we may be able to convert this to a
7150 simpler expression, depending on the operation and the values
7151 of B and C. IEEE floating point prevents this though,
7152 because A or B might be -0.0 or a NaN. */
7154 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
7155 && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
7156 || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
7157 || flag_unsafe_math_optimizations)
7158 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
7159 arg1, TREE_OPERAND (arg0, 1)))
7161 tree arg2 = TREE_OPERAND (t, 2);
7162 enum tree_code comp_code = TREE_CODE (arg0);
7164 STRIP_NOPS (arg2);
7166 /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or -abs (A),
7167 depending on the comparison operation. */
7168 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
7169 ? real_zerop (TREE_OPERAND (arg0, 1))
7170 : integer_zerop (TREE_OPERAND (arg0, 1)))
7171 && TREE_CODE (arg2) == NEGATE_EXPR
7172 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
7173 switch (comp_code)
7175 case EQ_EXPR:
7176 return
7177 pedantic_non_lvalue
7178 (convert (type,
7179 negate_expr
7180 (convert (TREE_TYPE (TREE_OPERAND (t, 1)),
7181 arg1))));
7183 case NE_EXPR:
7184 return pedantic_non_lvalue (convert (type, arg1));
7185 case GE_EXPR:
7186 case GT_EXPR:
7187 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
7188 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
7189 return pedantic_non_lvalue
7190 (convert (type, fold (build1 (ABS_EXPR,
7191 TREE_TYPE (arg1), arg1))));
7192 case LE_EXPR:
7193 case LT_EXPR:
7194 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
7195 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
7196 return pedantic_non_lvalue
7197 (negate_expr (convert (type,
7198 fold (build1 (ABS_EXPR,
7199 TREE_TYPE (arg1),
7200 arg1)))));
7201 default:
7202 abort ();
7205 /* If this is A != 0 ? A : 0, this is simply A. For ==, it is
7206 always zero. */
7208 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
7210 if (comp_code == NE_EXPR)
7211 return pedantic_non_lvalue (convert (type, arg1));
7212 else if (comp_code == EQ_EXPR)
7213 return pedantic_non_lvalue (convert (type, integer_zero_node));
7216 /* If this is A op B ? A : B, this is either A, B, min (A, B),
7217 or max (A, B), depending on the operation. */
7219 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
7220 arg2, TREE_OPERAND (arg0, 0)))
7222 tree comp_op0 = TREE_OPERAND (arg0, 0);
7223 tree comp_op1 = TREE_OPERAND (arg0, 1);
7224 tree comp_type = TREE_TYPE (comp_op0);
7226 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
7227 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
7228 comp_type = type;
7230 switch (comp_code)
7232 case EQ_EXPR:
7233 return pedantic_non_lvalue (convert (type, arg2));
7234 case NE_EXPR:
7235 return pedantic_non_lvalue (convert (type, arg1));
7236 case LE_EXPR:
7237 case LT_EXPR:
7238 /* In C++ a ?: expression can be an lvalue, so put the
7239 operand which will be used if they are equal first
7240 so that we can convert this back to the
7241 corresponding COND_EXPR. */
7242 return pedantic_non_lvalue
7243 (convert (type, fold (build (MIN_EXPR, comp_type,
7244 (comp_code == LE_EXPR
7245 ? comp_op0 : comp_op1),
7246 (comp_code == LE_EXPR
7247 ? comp_op1 : comp_op0)))));
7248 break;
7249 case GE_EXPR:
7250 case GT_EXPR:
7251 return pedantic_non_lvalue
7252 (convert (type, fold (build (MAX_EXPR, comp_type,
7253 (comp_code == GE_EXPR
7254 ? comp_op0 : comp_op1),
7255 (comp_code == GE_EXPR
7256 ? comp_op1 : comp_op0)))));
7257 break;
7258 default:
7259 abort ();
7263 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
7264 we might still be able to simplify this. For example,
7265 if C1 is one less or one more than C2, this might have started
7266 out as a MIN or MAX and been transformed by this function.
7267 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
7269 if (INTEGRAL_TYPE_P (type)
7270 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7271 && TREE_CODE (arg2) == INTEGER_CST)
7272 switch (comp_code)
7274 case EQ_EXPR:
7275 /* We can replace A with C1 in this case. */
7276 arg1 = convert (type, TREE_OPERAND (arg0, 1));
7277 t = build (code, type, TREE_OPERAND (t, 0), arg1,
7278 TREE_OPERAND (t, 2));
7279 break;
7281 case LT_EXPR:
7282 /* If C1 is C2 + 1, this is min(A, C2). */
7283 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
7284 && operand_equal_p (TREE_OPERAND (arg0, 1),
7285 const_binop (PLUS_EXPR, arg2,
7286 integer_one_node, 0), 1))
7287 return pedantic_non_lvalue
7288 (fold (build (MIN_EXPR, type, arg1, arg2)));
7289 break;
7291 case LE_EXPR:
7292 /* If C1 is C2 - 1, this is min(A, C2). */
7293 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
7294 && operand_equal_p (TREE_OPERAND (arg0, 1),
7295 const_binop (MINUS_EXPR, arg2,
7296 integer_one_node, 0), 1))
7297 return pedantic_non_lvalue
7298 (fold (build (MIN_EXPR, type, arg1, arg2)));
7299 break;
7301 case GT_EXPR:
7302 /* If C1 is C2 - 1, this is max(A, C2). */
7303 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
7304 && operand_equal_p (TREE_OPERAND (arg0, 1),
7305 const_binop (MINUS_EXPR, arg2,
7306 integer_one_node, 0), 1))
7307 return pedantic_non_lvalue
7308 (fold (build (MAX_EXPR, type, arg1, arg2)));
7309 break;
7311 case GE_EXPR:
7312 /* If C1 is C2 + 1, this is max(A, C2). */
7313 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
7314 && operand_equal_p (TREE_OPERAND (arg0, 1),
7315 const_binop (PLUS_EXPR, arg2,
7316 integer_one_node, 0), 1))
7317 return pedantic_non_lvalue
7318 (fold (build (MAX_EXPR, type, arg1, arg2)));
7319 break;
7320 case NE_EXPR:
7321 break;
7322 default:
7323 abort ();
7327 /* If the second operand is simpler than the third, swap them
7328 since that produces better jump optimization results. */
7329 if ((TREE_CONSTANT (arg1) || DECL_P (arg1)
7330 || TREE_CODE (arg1) == SAVE_EXPR)
7331 && ! (TREE_CONSTANT (TREE_OPERAND (t, 2))
7332 || DECL_P (TREE_OPERAND (t, 2))
7333 || TREE_CODE (TREE_OPERAND (t, 2)) == SAVE_EXPR))
7335 /* See if this can be inverted. If it can't, possibly because
7336 it was a floating-point inequality comparison, don't do
7337 anything. */
7338 tem = invert_truthvalue (arg0);
7340 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
7342 t = build (code, type, tem,
7343 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
7344 arg0 = tem;
7345 /* arg1 should be the first argument of the new T. */
7346 arg1 = TREE_OPERAND (t, 1);
7347 STRIP_NOPS (arg1);
7351 /* Convert A ? 1 : 0 to simply A. */
7352 if (integer_onep (TREE_OPERAND (t, 1))
7353 && integer_zerop (TREE_OPERAND (t, 2))
7354 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
7355 call to fold will try to move the conversion inside
7356 a COND, which will recurse. In that case, the COND_EXPR
7357 is probably the best choice, so leave it alone. */
7358 && type == TREE_TYPE (arg0))
7359 return pedantic_non_lvalue (arg0);
7361 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
7362 operation is simply A & 2. */
7364 if (integer_zerop (TREE_OPERAND (t, 2))
7365 && TREE_CODE (arg0) == NE_EXPR
7366 && integer_zerop (TREE_OPERAND (arg0, 1))
7367 && integer_pow2p (arg1)
7368 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
7369 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
7370 arg1, 1))
7371 return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
7373 return t;
7375 case COMPOUND_EXPR:
7376 /* When pedantic, a compound expression can be neither an lvalue
7377 nor an integer constant expression. */
7378 if (TREE_SIDE_EFFECTS (arg0) || pedantic)
7379 return t;
7380 /* Don't let (0, 0) be null pointer constant. */
7381 if (integer_zerop (arg1))
7382 return build1 (NOP_EXPR, type, arg1);
7383 return convert (type, arg1);
7385 case COMPLEX_EXPR:
7386 if (wins)
7387 return build_complex (type, arg0, arg1);
7388 return t;
7390 case REALPART_EXPR:
7391 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7392 return t;
7393 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7394 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
7395 TREE_OPERAND (arg0, 1));
7396 else if (TREE_CODE (arg0) == COMPLEX_CST)
7397 return TREE_REALPART (arg0);
7398 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7399 return fold (build (TREE_CODE (arg0), type,
7400 fold (build1 (REALPART_EXPR, type,
7401 TREE_OPERAND (arg0, 0))),
7402 fold (build1 (REALPART_EXPR,
7403 type, TREE_OPERAND (arg0, 1)))));
7404 return t;
7406 case IMAGPART_EXPR:
7407 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7408 return convert (type, integer_zero_node);
7409 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7410 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
7411 TREE_OPERAND (arg0, 0));
7412 else if (TREE_CODE (arg0) == COMPLEX_CST)
7413 return TREE_IMAGPART (arg0);
7414 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7415 return fold (build (TREE_CODE (arg0), type,
7416 fold (build1 (IMAGPART_EXPR, type,
7417 TREE_OPERAND (arg0, 0))),
7418 fold (build1 (IMAGPART_EXPR, type,
7419 TREE_OPERAND (arg0, 1)))));
7420 return t;
7422 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
7423 appropriate. */
7424 case CLEANUP_POINT_EXPR:
7425 if (! has_cleanups (arg0))
7426 return TREE_OPERAND (t, 0);
7429 enum tree_code code0 = TREE_CODE (arg0);
7430 int kind0 = TREE_CODE_CLASS (code0);
7431 tree arg00 = TREE_OPERAND (arg0, 0);
7432 tree arg01;
7434 if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
7435 return fold (build1 (code0, type,
7436 fold (build1 (CLEANUP_POINT_EXPR,
7437 TREE_TYPE (arg00), arg00))));
7439 if (kind0 == '<' || kind0 == '2'
7440 || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
7441 || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR
7442 || code0 == TRUTH_XOR_EXPR)
7444 arg01 = TREE_OPERAND (arg0, 1);
7446 if (TREE_CONSTANT (arg00)
7447 || ((code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR)
7448 && ! has_cleanups (arg00)))
7449 return fold (build (code0, type, arg00,
7450 fold (build1 (CLEANUP_POINT_EXPR,
7451 TREE_TYPE (arg01), arg01))));
7453 if (TREE_CONSTANT (arg01))
7454 return fold (build (code0, type,
7455 fold (build1 (CLEANUP_POINT_EXPR,
7456 TREE_TYPE (arg00), arg00)),
7457 arg01));
7460 return t;
7463 case CALL_EXPR:
7464 /* Check for a built-in function. */
7465 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR
7466 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (expr, 0), 0))
7467 == FUNCTION_DECL)
7468 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (expr, 0), 0)))
7470 tree tmp = fold_builtin (expr);
7471 if (tmp)
7472 return tmp;
7474 return t;
7476 default:
7477 return t;
7478 } /* switch (code) */
7481 /* Determine if first argument is a multiple of second argument. Return 0 if
7482 it is not, or we cannot easily determined it to be.
7484 An example of the sort of thing we care about (at this point; this routine
7485 could surely be made more general, and expanded to do what the *_DIV_EXPR's
7486 fold cases do now) is discovering that
7488 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7490 is a multiple of
7492 SAVE_EXPR (J * 8)
7494 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
7496 This code also handles discovering that
7498 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7500 is a multiple of 8 so we don't have to worry about dealing with a
7501 possible remainder.
7503 Note that we *look* inside a SAVE_EXPR only to determine how it was
7504 calculated; it is not safe for fold to do much of anything else with the
7505 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
7506 at run time. For example, the latter example above *cannot* be implemented
7507 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
7508 evaluation time of the original SAVE_EXPR is not necessarily the same at
7509 the time the new expression is evaluated. The only optimization of this
7510 sort that would be valid is changing
7512 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
7514 divided by 8 to
7516 SAVE_EXPR (I) * SAVE_EXPR (J)
7518 (where the same SAVE_EXPR (J) is used in the original and the
7519 transformed version). */
7521 static int
7522 multiple_of_p (type, top, bottom)
7523 tree type;
7524 tree top;
7525 tree bottom;
7527 if (operand_equal_p (top, bottom, 0))
7528 return 1;
7530 if (TREE_CODE (type) != INTEGER_TYPE)
7531 return 0;
7533 switch (TREE_CODE (top))
7535 case MULT_EXPR:
7536 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7537 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7539 case PLUS_EXPR:
7540 case MINUS_EXPR:
7541 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7542 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7544 case LSHIFT_EXPR:
7545 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
7547 tree op1, t1;
7549 op1 = TREE_OPERAND (top, 1);
7550 /* const_binop may not detect overflow correctly,
7551 so check for it explicitly here. */
7552 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
7553 > TREE_INT_CST_LOW (op1)
7554 && TREE_INT_CST_HIGH (op1) == 0
7555 && 0 != (t1 = convert (type,
7556 const_binop (LSHIFT_EXPR, size_one_node,
7557 op1, 0)))
7558 && ! TREE_OVERFLOW (t1))
7559 return multiple_of_p (type, t1, bottom);
7561 return 0;
7563 case NOP_EXPR:
7564 /* Can't handle conversions from non-integral or wider integral type. */
7565 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
7566 || (TYPE_PRECISION (type)
7567 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
7568 return 0;
7570 /* .. fall through ... */
7572 case SAVE_EXPR:
7573 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
7575 case INTEGER_CST:
7576 if (TREE_CODE (bottom) != INTEGER_CST
7577 || (TREE_UNSIGNED (type)
7578 && (tree_int_cst_sgn (top) < 0
7579 || tree_int_cst_sgn (bottom) < 0)))
7580 return 0;
7581 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
7582 top, bottom, 0));
7584 default:
7585 return 0;
7589 /* Return true if `t' is known to be non-negative. */
7592 tree_expr_nonnegative_p (t)
7593 tree t;
7595 switch (TREE_CODE (t))
7597 case ABS_EXPR:
7598 case FFS_EXPR:
7599 return 1;
7600 case INTEGER_CST:
7601 return tree_int_cst_sgn (t) >= 0;
7602 case TRUNC_DIV_EXPR:
7603 case CEIL_DIV_EXPR:
7604 case FLOOR_DIV_EXPR:
7605 case ROUND_DIV_EXPR:
7606 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
7607 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7608 case TRUNC_MOD_EXPR:
7609 case CEIL_MOD_EXPR:
7610 case FLOOR_MOD_EXPR:
7611 case ROUND_MOD_EXPR:
7612 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
7613 case COND_EXPR:
7614 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
7615 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
7616 case COMPOUND_EXPR:
7617 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7618 case MIN_EXPR:
7619 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
7620 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7621 case MAX_EXPR:
7622 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
7623 || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7624 case MODIFY_EXPR:
7625 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7626 case BIND_EXPR:
7627 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7628 case SAVE_EXPR:
7629 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
7630 case NON_LVALUE_EXPR:
7631 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
7632 case RTL_EXPR:
7633 return rtl_expr_nonnegative_p (RTL_EXPR_RTL (t));
7635 default:
7636 if (truth_value_p (TREE_CODE (t)))
7637 /* Truth values evaluate to 0 or 1, which is nonnegative. */
7638 return 1;
7639 else
7640 /* We don't know sign of `t', so be conservative and return false. */
7641 return 0;
7645 /* Return true if `r' is known to be non-negative.
7646 Only handles constants at the moment. */
7649 rtl_expr_nonnegative_p (r)
7650 rtx r;
7652 switch (GET_CODE (r))
7654 case CONST_INT:
7655 return INTVAL (r) >= 0;
7657 case CONST_DOUBLE:
7658 if (GET_MODE (r) == VOIDmode)
7659 return CONST_DOUBLE_HIGH (r) >= 0;
7660 return 0;
7662 case CONST_VECTOR:
7664 int units, i;
7665 rtx elt;
7667 units = CONST_VECTOR_NUNITS (r);
7669 for (i = 0; i < units; ++i)
7671 elt = CONST_VECTOR_ELT (r, i);
7672 if (!rtl_expr_nonnegative_p (elt))
7673 return 0;
7676 return 1;
7679 case SYMBOL_REF:
7680 case LABEL_REF:
7681 /* These are always nonnegative. */
7682 return 1;
7684 default:
7685 return 0;