* gnu/regexp/CharIndexedReader.java: Removed.
[official-gcc.git] / gcc / fold-const.c
blobed54ee93bb615b138227800ab8063ecbd8031438
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, 1999,
3 2000, 2001, 2002, 2003, 2004 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 "coretypes.h"
48 #include "tm.h"
49 #include "flags.h"
50 #include "tree.h"
51 #include "real.h"
52 #include "rtl.h"
53 #include "expr.h"
54 #include "tm_p.h"
55 #include "toplev.h"
56 #include "ggc.h"
57 #include "hashtab.h"
58 #include "langhooks.h"
59 #include "md5.h"
61 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
62 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
63 static bool negate_mathfn_p (enum built_in_function);
64 static bool negate_expr_p (tree);
65 static tree negate_expr (tree);
66 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
67 static tree associate_trees (tree, tree, enum tree_code, tree);
68 static tree const_binop (enum tree_code, tree, tree, int);
69 static hashval_t size_htab_hash (const void *);
70 static int size_htab_eq (const void *, const void *);
71 static tree fold_convert_const (enum tree_code, tree, tree);
72 static enum tree_code invert_tree_comparison (enum tree_code);
73 static enum tree_code swap_tree_comparison (enum tree_code);
74 static int comparison_to_compcode (enum tree_code);
75 static enum tree_code compcode_to_comparison (int);
76 static int truth_value_p (enum tree_code);
77 static int operand_equal_for_comparison_p (tree, tree, tree);
78 static int twoval_comparison_p (tree, tree *, tree *, int *);
79 static tree eval_subst (tree, tree, tree, tree, tree);
80 static tree pedantic_omit_one_operand (tree, tree, tree);
81 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
82 static tree make_bit_field_ref (tree, tree, int, int, int);
83 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
84 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
85 enum machine_mode *, int *, int *,
86 tree *, tree *);
87 static int all_ones_mask_p (tree, int);
88 static tree sign_bit_p (tree, tree);
89 static int simple_operand_p (tree);
90 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
91 static tree make_range (tree, int *, tree *, tree *);
92 static tree build_range_check (tree, tree, int, tree, tree);
93 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
94 tree);
95 static tree fold_range_test (tree);
96 static tree unextend (tree, int, int, tree);
97 static tree fold_truthop (enum tree_code, tree, tree, tree);
98 static tree optimize_minmax_comparison (tree);
99 static tree extract_muldiv (tree, tree, enum tree_code, tree);
100 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree);
101 static tree strip_compound_expr (tree, tree);
102 static int multiple_of_p (tree, tree, tree);
103 static tree constant_boolean_node (int, tree);
104 static int count_cond (tree, int);
105 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree, tree,
106 tree, int);
107 static bool fold_real_zero_addition_p (tree, tree, int);
108 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
109 tree, tree, tree);
110 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
111 static tree fold_div_compare (enum tree_code, tree, tree, tree);
112 static bool reorder_operands_p (tree, tree);
113 static bool tree_swap_operands_p (tree, tree, bool);
115 static tree fold_negate_const (tree, tree);
116 static tree fold_abs_const (tree, tree);
117 static tree fold_relational_const (enum tree_code, tree, tree, tree);
118 static tree fold_relational_hi_lo (enum tree_code *, const tree, tree *, tree *);
120 /* The following constants represent a bit based encoding of GCC's
121 comparison operators. This encoding simplifies transformations
122 on relational comparison operators, such as AND and OR. */
123 #define COMPCODE_FALSE 0
124 #define COMPCODE_LT 1
125 #define COMPCODE_EQ 2
126 #define COMPCODE_LE 3
127 #define COMPCODE_GT 4
128 #define COMPCODE_NE 5
129 #define COMPCODE_GE 6
130 #define COMPCODE_TRUE 7
132 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
133 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
134 and SUM1. Then this yields nonzero if overflow occurred during the
135 addition.
137 Overflow occurs if A and B have the same sign, but A and SUM differ in
138 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
139 sign. */
140 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
142 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
143 We do that by representing the two-word integer in 4 words, with only
144 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
145 number. The value of the word is LOWPART + HIGHPART * BASE. */
147 #define LOWPART(x) \
148 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
149 #define HIGHPART(x) \
150 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
151 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
153 /* Unpack a two-word integer into 4 words.
154 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
155 WORDS points to the array of HOST_WIDE_INTs. */
157 static void
158 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
160 words[0] = LOWPART (low);
161 words[1] = HIGHPART (low);
162 words[2] = LOWPART (hi);
163 words[3] = HIGHPART (hi);
166 /* Pack an array of 4 words into a two-word integer.
167 WORDS points to the array of words.
168 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
170 static void
171 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
172 HOST_WIDE_INT *hi)
174 *low = words[0] + words[1] * BASE;
175 *hi = words[2] + words[3] * BASE;
178 /* Make the integer constant T valid for its type by setting to 0 or 1 all
179 the bits in the constant that don't belong in the type.
181 Return 1 if a signed overflow occurs, 0 otherwise. If OVERFLOW is
182 nonzero, a signed overflow has already occurred in calculating T, so
183 propagate it. */
186 force_fit_type (tree t, int overflow)
188 unsigned HOST_WIDE_INT low;
189 HOST_WIDE_INT high;
190 unsigned int prec;
192 if (TREE_CODE (t) == REAL_CST)
194 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
195 Consider doing it via real_convert now. */
196 return overflow;
199 else if (TREE_CODE (t) != INTEGER_CST)
200 return overflow;
202 low = TREE_INT_CST_LOW (t);
203 high = TREE_INT_CST_HIGH (t);
205 if (POINTER_TYPE_P (TREE_TYPE (t))
206 || TREE_CODE (TREE_TYPE (t)) == OFFSET_TYPE)
207 prec = POINTER_SIZE;
208 else
209 prec = TYPE_PRECISION (TREE_TYPE (t));
211 /* First clear all bits that are beyond the type's precision. */
213 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
215 else if (prec > HOST_BITS_PER_WIDE_INT)
216 TREE_INT_CST_HIGH (t)
217 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
218 else
220 TREE_INT_CST_HIGH (t) = 0;
221 if (prec < HOST_BITS_PER_WIDE_INT)
222 TREE_INT_CST_LOW (t) &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
225 /* Unsigned types do not suffer sign extension or overflow unless they
226 are a sizetype. */
227 if (TYPE_UNSIGNED (TREE_TYPE (t))
228 && ! (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
229 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
230 return overflow;
232 /* If the value's sign bit is set, extend the sign. */
233 if (prec != 2 * HOST_BITS_PER_WIDE_INT
234 && (prec > HOST_BITS_PER_WIDE_INT
235 ? 0 != (TREE_INT_CST_HIGH (t)
236 & ((HOST_WIDE_INT) 1
237 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
238 : 0 != (TREE_INT_CST_LOW (t)
239 & ((unsigned HOST_WIDE_INT) 1 << (prec - 1)))))
241 /* Value is negative:
242 set to 1 all the bits that are outside this type's precision. */
243 if (prec > HOST_BITS_PER_WIDE_INT)
244 TREE_INT_CST_HIGH (t)
245 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
246 else
248 TREE_INT_CST_HIGH (t) = -1;
249 if (prec < HOST_BITS_PER_WIDE_INT)
250 TREE_INT_CST_LOW (t) |= ((unsigned HOST_WIDE_INT) (-1) << prec);
254 /* Return nonzero if signed overflow occurred. */
255 return
256 ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
257 != 0);
260 /* Add two doubleword integers with doubleword result.
261 Each argument is given as two `HOST_WIDE_INT' pieces.
262 One argument is L1 and H1; the other, L2 and H2.
263 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
266 add_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
267 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
268 unsigned HOST_WIDE_INT *lv, 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 (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
288 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
290 if (l1 == 0)
292 *lv = 0;
293 *hv = - h1;
294 return (*hv & h1) < 0;
296 else
298 *lv = -l1;
299 *hv = ~h1;
300 return 0;
304 /* Multiply two doubleword integers with doubleword result.
305 Return nonzero if the operation overflows, assuming it's signed.
306 Each argument is given as two `HOST_WIDE_INT' pieces.
307 One argument is L1 and H1; the other, L2 and H2.
308 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
311 mul_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
312 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
313 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
315 HOST_WIDE_INT arg1[4];
316 HOST_WIDE_INT arg2[4];
317 HOST_WIDE_INT prod[4 * 2];
318 unsigned HOST_WIDE_INT carry;
319 int i, j, k;
320 unsigned HOST_WIDE_INT toplow, neglow;
321 HOST_WIDE_INT tophigh, neghigh;
323 encode (arg1, l1, h1);
324 encode (arg2, l2, h2);
326 memset (prod, 0, sizeof prod);
328 for (i = 0; i < 4; i++)
330 carry = 0;
331 for (j = 0; j < 4; j++)
333 k = i + j;
334 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
335 carry += arg1[i] * arg2[j];
336 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
337 carry += prod[k];
338 prod[k] = LOWPART (carry);
339 carry = HIGHPART (carry);
341 prod[i + 4] = carry;
344 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
346 /* Check for overflow by calculating the top half of the answer in full;
347 it should agree with the low half's sign bit. */
348 decode (prod + 4, &toplow, &tophigh);
349 if (h1 < 0)
351 neg_double (l2, h2, &neglow, &neghigh);
352 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
354 if (h2 < 0)
356 neg_double (l1, h1, &neglow, &neghigh);
357 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
359 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
362 /* Shift the doubleword integer in L1, H1 left by COUNT places
363 keeping only PREC bits of result.
364 Shift right if COUNT is negative.
365 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
366 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
368 void
369 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
370 HOST_WIDE_INT count, unsigned int prec,
371 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
373 unsigned HOST_WIDE_INT signmask;
375 if (count < 0)
377 rshift_double (l1, h1, -count, prec, lv, hv, arith);
378 return;
381 if (SHIFT_COUNT_TRUNCATED)
382 count %= prec;
384 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
386 /* Shifting by the host word size is undefined according to the
387 ANSI standard, so we must handle this as a special case. */
388 *hv = 0;
389 *lv = 0;
391 else if (count >= HOST_BITS_PER_WIDE_INT)
393 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
394 *lv = 0;
396 else
398 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
399 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
400 *lv = l1 << count;
403 /* Sign extend all bits that are beyond the precision. */
405 signmask = -((prec > HOST_BITS_PER_WIDE_INT
406 ? ((unsigned HOST_WIDE_INT) *hv
407 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
408 : (*lv >> (prec - 1))) & 1);
410 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
412 else if (prec >= HOST_BITS_PER_WIDE_INT)
414 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
415 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
417 else
419 *hv = signmask;
420 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
421 *lv |= signmask << prec;
425 /* Shift the doubleword integer in L1, H1 right by COUNT places
426 keeping only PREC bits of result. COUNT must be positive.
427 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
428 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
430 void
431 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
432 HOST_WIDE_INT count, unsigned int prec,
433 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
434 int arith)
436 unsigned HOST_WIDE_INT signmask;
438 signmask = (arith
439 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
440 : 0);
442 if (SHIFT_COUNT_TRUNCATED)
443 count %= prec;
445 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
447 /* Shifting by the host word size is undefined according to the
448 ANSI standard, so we must handle this as a special case. */
449 *hv = 0;
450 *lv = 0;
452 else if (count >= HOST_BITS_PER_WIDE_INT)
454 *hv = 0;
455 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
457 else
459 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
460 *lv = ((l1 >> count)
461 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
464 /* Zero / sign extend all bits that are beyond the precision. */
466 if (count >= (HOST_WIDE_INT)prec)
468 *hv = signmask;
469 *lv = signmask;
471 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
473 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
475 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
476 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
478 else
480 *hv = signmask;
481 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
482 *lv |= signmask << (prec - count);
486 /* Rotate the doubleword integer in L1, H1 left by COUNT places
487 keeping only PREC bits of result.
488 Rotate right if COUNT is negative.
489 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
491 void
492 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
493 HOST_WIDE_INT count, unsigned int prec,
494 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
496 unsigned HOST_WIDE_INT s1l, s2l;
497 HOST_WIDE_INT s1h, s2h;
499 count %= prec;
500 if (count < 0)
501 count += prec;
503 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
504 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
505 *lv = s1l | s2l;
506 *hv = s1h | s2h;
509 /* Rotate the doubleword integer in L1, H1 left by COUNT places
510 keeping only PREC bits of result. COUNT must be positive.
511 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
513 void
514 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
515 HOST_WIDE_INT count, unsigned int prec,
516 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
518 unsigned HOST_WIDE_INT s1l, s2l;
519 HOST_WIDE_INT s1h, s2h;
521 count %= prec;
522 if (count < 0)
523 count += prec;
525 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
526 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
527 *lv = s1l | s2l;
528 *hv = s1h | s2h;
531 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
532 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
533 CODE is a tree code for a kind of division, one of
534 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
535 or EXACT_DIV_EXPR
536 It controls how the quotient is rounded to an integer.
537 Return nonzero if the operation overflows.
538 UNS nonzero says do unsigned division. */
541 div_and_round_double (enum tree_code code, int uns,
542 unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
543 HOST_WIDE_INT hnum_orig,
544 unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
545 HOST_WIDE_INT hden_orig,
546 unsigned HOST_WIDE_INT *lquo,
547 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
548 HOST_WIDE_INT *hrem)
550 int quo_neg = 0;
551 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
552 HOST_WIDE_INT den[4], quo[4];
553 int i, j;
554 unsigned HOST_WIDE_INT work;
555 unsigned HOST_WIDE_INT carry = 0;
556 unsigned HOST_WIDE_INT lnum = lnum_orig;
557 HOST_WIDE_INT hnum = hnum_orig;
558 unsigned HOST_WIDE_INT lden = lden_orig;
559 HOST_WIDE_INT hden = hden_orig;
560 int overflow = 0;
562 if (hden == 0 && lden == 0)
563 overflow = 1, lden = 1;
565 /* Calculate quotient sign and convert operands to unsigned. */
566 if (!uns)
568 if (hnum < 0)
570 quo_neg = ~ quo_neg;
571 /* (minimum integer) / (-1) is the only overflow case. */
572 if (neg_double (lnum, hnum, &lnum, &hnum)
573 && ((HOST_WIDE_INT) lden & hden) == -1)
574 overflow = 1;
576 if (hden < 0)
578 quo_neg = ~ quo_neg;
579 neg_double (lden, hden, &lden, &hden);
583 if (hnum == 0 && hden == 0)
584 { /* single precision */
585 *hquo = *hrem = 0;
586 /* This unsigned division rounds toward zero. */
587 *lquo = lnum / lden;
588 goto finish_up;
591 if (hnum == 0)
592 { /* trivial case: dividend < divisor */
593 /* hden != 0 already checked. */
594 *hquo = *lquo = 0;
595 *hrem = hnum;
596 *lrem = lnum;
597 goto finish_up;
600 memset (quo, 0, sizeof quo);
602 memset (num, 0, sizeof num); /* to zero 9th element */
603 memset (den, 0, sizeof den);
605 encode (num, lnum, hnum);
606 encode (den, lden, hden);
608 /* Special code for when the divisor < BASE. */
609 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
611 /* hnum != 0 already checked. */
612 for (i = 4 - 1; i >= 0; i--)
614 work = num[i] + carry * BASE;
615 quo[i] = work / lden;
616 carry = work % lden;
619 else
621 /* Full double precision division,
622 with thanks to Don Knuth's "Seminumerical Algorithms". */
623 int num_hi_sig, den_hi_sig;
624 unsigned HOST_WIDE_INT quo_est, scale;
626 /* Find the highest nonzero divisor digit. */
627 for (i = 4 - 1;; i--)
628 if (den[i] != 0)
630 den_hi_sig = i;
631 break;
634 /* Insure that the first digit of the divisor is at least BASE/2.
635 This is required by the quotient digit estimation algorithm. */
637 scale = BASE / (den[den_hi_sig] + 1);
638 if (scale > 1)
639 { /* scale divisor and dividend */
640 carry = 0;
641 for (i = 0; i <= 4 - 1; i++)
643 work = (num[i] * scale) + carry;
644 num[i] = LOWPART (work);
645 carry = HIGHPART (work);
648 num[4] = carry;
649 carry = 0;
650 for (i = 0; i <= 4 - 1; i++)
652 work = (den[i] * scale) + carry;
653 den[i] = LOWPART (work);
654 carry = HIGHPART (work);
655 if (den[i] != 0) den_hi_sig = i;
659 num_hi_sig = 4;
661 /* Main loop */
662 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
664 /* Guess the next quotient digit, quo_est, by dividing the first
665 two remaining dividend digits by the high order quotient digit.
666 quo_est is never low and is at most 2 high. */
667 unsigned HOST_WIDE_INT tmp;
669 num_hi_sig = i + den_hi_sig + 1;
670 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
671 if (num[num_hi_sig] != den[den_hi_sig])
672 quo_est = work / den[den_hi_sig];
673 else
674 quo_est = BASE - 1;
676 /* Refine quo_est so it's usually correct, and at most one high. */
677 tmp = work - quo_est * den[den_hi_sig];
678 if (tmp < BASE
679 && (den[den_hi_sig - 1] * quo_est
680 > (tmp * BASE + num[num_hi_sig - 2])))
681 quo_est--;
683 /* Try QUO_EST as the quotient digit, by multiplying the
684 divisor by QUO_EST and subtracting from the remaining dividend.
685 Keep in mind that QUO_EST is the I - 1st digit. */
687 carry = 0;
688 for (j = 0; j <= den_hi_sig; j++)
690 work = quo_est * den[j] + carry;
691 carry = HIGHPART (work);
692 work = num[i + j] - LOWPART (work);
693 num[i + j] = LOWPART (work);
694 carry += HIGHPART (work) != 0;
697 /* If quo_est was high by one, then num[i] went negative and
698 we need to correct things. */
699 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
701 quo_est--;
702 carry = 0; /* add divisor back in */
703 for (j = 0; j <= den_hi_sig; j++)
705 work = num[i + j] + den[j] + carry;
706 carry = HIGHPART (work);
707 num[i + j] = LOWPART (work);
710 num [num_hi_sig] += carry;
713 /* Store the quotient digit. */
714 quo[i] = quo_est;
718 decode (quo, lquo, hquo);
720 finish_up:
721 /* If result is negative, make it so. */
722 if (quo_neg)
723 neg_double (*lquo, *hquo, lquo, hquo);
725 /* Compute trial remainder: rem = num - (quo * den) */
726 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
727 neg_double (*lrem, *hrem, lrem, hrem);
728 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
730 switch (code)
732 case TRUNC_DIV_EXPR:
733 case TRUNC_MOD_EXPR: /* round toward zero */
734 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
735 return overflow;
737 case FLOOR_DIV_EXPR:
738 case FLOOR_MOD_EXPR: /* round toward negative infinity */
739 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
741 /* quo = quo - 1; */
742 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
743 lquo, hquo);
745 else
746 return overflow;
747 break;
749 case CEIL_DIV_EXPR:
750 case CEIL_MOD_EXPR: /* round toward positive infinity */
751 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
753 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
754 lquo, hquo);
756 else
757 return overflow;
758 break;
760 case ROUND_DIV_EXPR:
761 case ROUND_MOD_EXPR: /* round to closest integer */
763 unsigned HOST_WIDE_INT labs_rem = *lrem;
764 HOST_WIDE_INT habs_rem = *hrem;
765 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
766 HOST_WIDE_INT habs_den = hden, htwice;
768 /* Get absolute values. */
769 if (*hrem < 0)
770 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
771 if (hden < 0)
772 neg_double (lden, hden, &labs_den, &habs_den);
774 /* If (2 * abs (lrem) >= abs (lden)) */
775 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
776 labs_rem, habs_rem, &ltwice, &htwice);
778 if (((unsigned HOST_WIDE_INT) habs_den
779 < (unsigned HOST_WIDE_INT) htwice)
780 || (((unsigned HOST_WIDE_INT) habs_den
781 == (unsigned HOST_WIDE_INT) htwice)
782 && (labs_den < ltwice)))
784 if (*hquo < 0)
785 /* quo = quo - 1; */
786 add_double (*lquo, *hquo,
787 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
788 else
789 /* quo = quo + 1; */
790 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
791 lquo, hquo);
793 else
794 return overflow;
796 break;
798 default:
799 abort ();
802 /* Compute true remainder: rem = num - (quo * den) */
803 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
804 neg_double (*lrem, *hrem, lrem, hrem);
805 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
806 return overflow;
809 /* Return true if built-in mathematical function specified by CODE
810 preserves the sign of it argument, i.e. -f(x) == f(-x). */
812 static bool
813 negate_mathfn_p (enum built_in_function code)
815 switch (code)
817 case BUILT_IN_ASIN:
818 case BUILT_IN_ASINF:
819 case BUILT_IN_ASINL:
820 case BUILT_IN_ATAN:
821 case BUILT_IN_ATANF:
822 case BUILT_IN_ATANL:
823 case BUILT_IN_SIN:
824 case BUILT_IN_SINF:
825 case BUILT_IN_SINL:
826 case BUILT_IN_TAN:
827 case BUILT_IN_TANF:
828 case BUILT_IN_TANL:
829 return true;
831 default:
832 break;
834 return false;
837 /* Determine whether an expression T can be cheaply negated using
838 the function negate_expr. */
840 static bool
841 negate_expr_p (tree t)
843 unsigned HOST_WIDE_INT val;
844 unsigned int prec;
845 tree type;
847 if (t == 0)
848 return false;
850 type = TREE_TYPE (t);
852 STRIP_SIGN_NOPS (t);
853 switch (TREE_CODE (t))
855 case INTEGER_CST:
856 if (TYPE_UNSIGNED (type) || ! flag_trapv)
857 return true;
859 /* Check that -CST will not overflow type. */
860 prec = TYPE_PRECISION (type);
861 if (prec > HOST_BITS_PER_WIDE_INT)
863 if (TREE_INT_CST_LOW (t) != 0)
864 return true;
865 prec -= HOST_BITS_PER_WIDE_INT;
866 val = TREE_INT_CST_HIGH (t);
868 else
869 val = TREE_INT_CST_LOW (t);
870 if (prec < HOST_BITS_PER_WIDE_INT)
871 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
872 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
874 case REAL_CST:
875 case NEGATE_EXPR:
876 return true;
878 case COMPLEX_CST:
879 return negate_expr_p (TREE_REALPART (t))
880 && negate_expr_p (TREE_IMAGPART (t));
882 case PLUS_EXPR:
883 if (FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
884 return false;
885 /* -(A + B) -> (-B) - A. */
886 if (negate_expr_p (TREE_OPERAND (t, 1))
887 && reorder_operands_p (TREE_OPERAND (t, 0),
888 TREE_OPERAND (t, 1)))
889 return true;
890 /* -(A + B) -> (-A) - B. */
891 return negate_expr_p (TREE_OPERAND (t, 0));
893 case MINUS_EXPR:
894 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
895 return (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
896 && reorder_operands_p (TREE_OPERAND (t, 0),
897 TREE_OPERAND (t, 1));
899 case MULT_EXPR:
900 if (TYPE_UNSIGNED (TREE_TYPE (t)))
901 break;
903 /* Fall through. */
905 case RDIV_EXPR:
906 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
907 return negate_expr_p (TREE_OPERAND (t, 1))
908 || negate_expr_p (TREE_OPERAND (t, 0));
909 break;
911 case NOP_EXPR:
912 /* Negate -((double)float) as (double)(-float). */
913 if (TREE_CODE (type) == REAL_TYPE)
915 tree tem = strip_float_extensions (t);
916 if (tem != t)
917 return negate_expr_p (tem);
919 break;
921 case CALL_EXPR:
922 /* Negate -f(x) as f(-x). */
923 if (negate_mathfn_p (builtin_mathfn_code (t)))
924 return negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1)));
925 break;
927 case RSHIFT_EXPR:
928 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
929 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
931 tree op1 = TREE_OPERAND (t, 1);
932 if (TREE_INT_CST_HIGH (op1) == 0
933 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
934 == TREE_INT_CST_LOW (op1))
935 return true;
937 break;
939 default:
940 break;
942 return false;
945 /* Given T, an expression, return the negation of T. Allow for T to be
946 null, in which case return null. */
948 static tree
949 negate_expr (tree t)
951 tree type;
952 tree tem;
954 if (t == 0)
955 return 0;
957 type = TREE_TYPE (t);
958 STRIP_SIGN_NOPS (t);
960 switch (TREE_CODE (t))
962 case INTEGER_CST:
963 tem = fold_negate_const (t, type);
964 if (! TREE_OVERFLOW (tem)
965 || TYPE_UNSIGNED (type)
966 || ! flag_trapv)
967 return tem;
968 break;
970 case REAL_CST:
971 tem = fold_negate_const (t, type);
972 /* Two's complement FP formats, such as c4x, may overflow. */
973 if (! TREE_OVERFLOW (tem) || ! flag_trapping_math)
974 return fold_convert (type, tem);
975 break;
977 case COMPLEX_CST:
979 tree rpart = negate_expr (TREE_REALPART (t));
980 tree ipart = negate_expr (TREE_IMAGPART (t));
982 if ((TREE_CODE (rpart) == REAL_CST
983 && TREE_CODE (ipart) == REAL_CST)
984 || (TREE_CODE (rpart) == INTEGER_CST
985 && TREE_CODE (ipart) == INTEGER_CST))
986 return build_complex (type, rpart, ipart);
988 break;
990 case NEGATE_EXPR:
991 return fold_convert (type, TREE_OPERAND (t, 0));
993 case PLUS_EXPR:
994 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
996 /* -(A + B) -> (-B) - A. */
997 if (negate_expr_p (TREE_OPERAND (t, 1))
998 && reorder_operands_p (TREE_OPERAND (t, 0),
999 TREE_OPERAND (t, 1)))
1000 return fold_convert (type,
1001 fold (build (MINUS_EXPR, TREE_TYPE (t),
1002 negate_expr (TREE_OPERAND (t, 1)),
1003 TREE_OPERAND (t, 0))));
1004 /* -(A + B) -> (-A) - B. */
1005 if (negate_expr_p (TREE_OPERAND (t, 0)))
1006 return fold_convert (type,
1007 fold (build (MINUS_EXPR, TREE_TYPE (t),
1008 negate_expr (TREE_OPERAND (t, 0)),
1009 TREE_OPERAND (t, 1))));
1011 break;
1013 case MINUS_EXPR:
1014 /* - (A - B) -> B - A */
1015 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1016 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1017 return fold_convert (type,
1018 fold (build (MINUS_EXPR, TREE_TYPE (t),
1019 TREE_OPERAND (t, 1),
1020 TREE_OPERAND (t, 0))));
1021 break;
1023 case MULT_EXPR:
1024 if (TYPE_UNSIGNED (TREE_TYPE (t)))
1025 break;
1027 /* Fall through. */
1029 case RDIV_EXPR:
1030 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1032 tem = TREE_OPERAND (t, 1);
1033 if (negate_expr_p (tem))
1034 return fold_convert (type,
1035 fold (build (TREE_CODE (t), TREE_TYPE (t),
1036 TREE_OPERAND (t, 0),
1037 negate_expr (tem))));
1038 tem = TREE_OPERAND (t, 0);
1039 if (negate_expr_p (tem))
1040 return fold_convert (type,
1041 fold (build (TREE_CODE (t), TREE_TYPE (t),
1042 negate_expr (tem),
1043 TREE_OPERAND (t, 1))));
1045 break;
1047 case NOP_EXPR:
1048 /* Convert -((double)float) into (double)(-float). */
1049 if (TREE_CODE (type) == REAL_TYPE)
1051 tem = strip_float_extensions (t);
1052 if (tem != t && negate_expr_p (tem))
1053 return fold_convert (type, negate_expr (tem));
1055 break;
1057 case CALL_EXPR:
1058 /* Negate -f(x) as f(-x). */
1059 if (negate_mathfn_p (builtin_mathfn_code (t))
1060 && negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1))))
1062 tree fndecl, arg, arglist;
1064 fndecl = get_callee_fndecl (t);
1065 arg = negate_expr (TREE_VALUE (TREE_OPERAND (t, 1)));
1066 arglist = build_tree_list (NULL_TREE, arg);
1067 return build_function_call_expr (fndecl, arglist);
1069 break;
1071 case RSHIFT_EXPR:
1072 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1073 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1075 tree op1 = TREE_OPERAND (t, 1);
1076 if (TREE_INT_CST_HIGH (op1) == 0
1077 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1078 == TREE_INT_CST_LOW (op1))
1080 tree ntype = TYPE_UNSIGNED (type)
1081 ? lang_hooks.types.signed_type (type)
1082 : lang_hooks.types.unsigned_type (type);
1083 tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1084 temp = fold (build2 (RSHIFT_EXPR, ntype, temp, op1));
1085 return fold_convert (type, temp);
1088 break;
1090 default:
1091 break;
1094 tem = fold (build1 (NEGATE_EXPR, TREE_TYPE (t), t));
1095 return fold_convert (type, tem);
1098 /* Split a tree IN into a constant, literal and variable parts that could be
1099 combined with CODE to make IN. "constant" means an expression with
1100 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1101 commutative arithmetic operation. Store the constant part into *CONP,
1102 the literal in *LITP and return the variable part. If a part isn't
1103 present, set it to null. If the tree does not decompose in this way,
1104 return the entire tree as the variable part and the other parts as null.
1106 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1107 case, we negate an operand that was subtracted. Except if it is a
1108 literal for which we use *MINUS_LITP instead.
1110 If NEGATE_P is true, we are negating all of IN, again except a literal
1111 for which we use *MINUS_LITP instead.
1113 If IN is itself a literal or constant, return it as appropriate.
1115 Note that we do not guarantee that any of the three values will be the
1116 same type as IN, but they will have the same signedness and mode. */
1118 static tree
1119 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1120 tree *minus_litp, int negate_p)
1122 tree var = 0;
1124 *conp = 0;
1125 *litp = 0;
1126 *minus_litp = 0;
1128 /* Strip any conversions that don't change the machine mode or signedness. */
1129 STRIP_SIGN_NOPS (in);
1131 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1132 *litp = in;
1133 else if (TREE_CODE (in) == code
1134 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1135 /* We can associate addition and subtraction together (even
1136 though the C standard doesn't say so) for integers because
1137 the value is not affected. For reals, the value might be
1138 affected, so we can't. */
1139 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1140 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1142 tree op0 = TREE_OPERAND (in, 0);
1143 tree op1 = TREE_OPERAND (in, 1);
1144 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1145 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1147 /* First see if either of the operands is a literal, then a constant. */
1148 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1149 *litp = op0, op0 = 0;
1150 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1151 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1153 if (op0 != 0 && TREE_CONSTANT (op0))
1154 *conp = op0, op0 = 0;
1155 else if (op1 != 0 && TREE_CONSTANT (op1))
1156 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1158 /* If we haven't dealt with either operand, this is not a case we can
1159 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1160 if (op0 != 0 && op1 != 0)
1161 var = in;
1162 else if (op0 != 0)
1163 var = op0;
1164 else
1165 var = op1, neg_var_p = neg1_p;
1167 /* Now do any needed negations. */
1168 if (neg_litp_p)
1169 *minus_litp = *litp, *litp = 0;
1170 if (neg_conp_p)
1171 *conp = negate_expr (*conp);
1172 if (neg_var_p)
1173 var = negate_expr (var);
1175 else if (TREE_CONSTANT (in))
1176 *conp = in;
1177 else
1178 var = in;
1180 if (negate_p)
1182 if (*litp)
1183 *minus_litp = *litp, *litp = 0;
1184 else if (*minus_litp)
1185 *litp = *minus_litp, *minus_litp = 0;
1186 *conp = negate_expr (*conp);
1187 var = negate_expr (var);
1190 return var;
1193 /* Re-associate trees split by the above function. T1 and T2 are either
1194 expressions to associate or null. Return the new expression, if any. If
1195 we build an operation, do it in TYPE and with CODE. */
1197 static tree
1198 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1200 if (t1 == 0)
1201 return t2;
1202 else if (t2 == 0)
1203 return t1;
1205 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1206 try to fold this since we will have infinite recursion. But do
1207 deal with any NEGATE_EXPRs. */
1208 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1209 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1211 if (code == PLUS_EXPR)
1213 if (TREE_CODE (t1) == NEGATE_EXPR)
1214 return build (MINUS_EXPR, type, fold_convert (type, t2),
1215 fold_convert (type, TREE_OPERAND (t1, 0)));
1216 else if (TREE_CODE (t2) == NEGATE_EXPR)
1217 return build (MINUS_EXPR, type, fold_convert (type, t1),
1218 fold_convert (type, TREE_OPERAND (t2, 0)));
1220 return build (code, type, fold_convert (type, t1),
1221 fold_convert (type, t2));
1224 return fold (build (code, type, fold_convert (type, t1),
1225 fold_convert (type, t2)));
1228 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1229 to produce a new constant.
1231 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1233 tree
1234 int_const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1236 unsigned HOST_WIDE_INT int1l, int2l;
1237 HOST_WIDE_INT int1h, int2h;
1238 unsigned HOST_WIDE_INT low;
1239 HOST_WIDE_INT hi;
1240 unsigned HOST_WIDE_INT garbagel;
1241 HOST_WIDE_INT garbageh;
1242 tree t;
1243 tree type = TREE_TYPE (arg1);
1244 int uns = TYPE_UNSIGNED (type);
1245 int is_sizetype
1246 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1247 int overflow = 0;
1248 int no_overflow = 0;
1250 int1l = TREE_INT_CST_LOW (arg1);
1251 int1h = TREE_INT_CST_HIGH (arg1);
1252 int2l = TREE_INT_CST_LOW (arg2);
1253 int2h = TREE_INT_CST_HIGH (arg2);
1255 switch (code)
1257 case BIT_IOR_EXPR:
1258 low = int1l | int2l, hi = int1h | int2h;
1259 break;
1261 case BIT_XOR_EXPR:
1262 low = int1l ^ int2l, hi = int1h ^ int2h;
1263 break;
1265 case BIT_AND_EXPR:
1266 low = int1l & int2l, hi = int1h & int2h;
1267 break;
1269 case RSHIFT_EXPR:
1270 int2l = -int2l;
1271 case LSHIFT_EXPR:
1272 /* It's unclear from the C standard whether shifts can overflow.
1273 The following code ignores overflow; perhaps a C standard
1274 interpretation ruling is needed. */
1275 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1276 &low, &hi, !uns);
1277 no_overflow = 1;
1278 break;
1280 case RROTATE_EXPR:
1281 int2l = - int2l;
1282 case LROTATE_EXPR:
1283 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1284 &low, &hi);
1285 break;
1287 case PLUS_EXPR:
1288 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1289 break;
1291 case MINUS_EXPR:
1292 neg_double (int2l, int2h, &low, &hi);
1293 add_double (int1l, int1h, low, hi, &low, &hi);
1294 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1295 break;
1297 case MULT_EXPR:
1298 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1299 break;
1301 case TRUNC_DIV_EXPR:
1302 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1303 case EXACT_DIV_EXPR:
1304 /* This is a shortcut for a common special case. */
1305 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1306 && ! TREE_CONSTANT_OVERFLOW (arg1)
1307 && ! TREE_CONSTANT_OVERFLOW (arg2)
1308 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1310 if (code == CEIL_DIV_EXPR)
1311 int1l += int2l - 1;
1313 low = int1l / int2l, hi = 0;
1314 break;
1317 /* ... fall through ... */
1319 case ROUND_DIV_EXPR:
1320 if (int2h == 0 && int2l == 1)
1322 low = int1l, hi = int1h;
1323 break;
1325 if (int1l == int2l && int1h == int2h
1326 && ! (int1l == 0 && int1h == 0))
1328 low = 1, hi = 0;
1329 break;
1331 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1332 &low, &hi, &garbagel, &garbageh);
1333 break;
1335 case TRUNC_MOD_EXPR:
1336 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1337 /* This is a shortcut for a common special case. */
1338 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1339 && ! TREE_CONSTANT_OVERFLOW (arg1)
1340 && ! TREE_CONSTANT_OVERFLOW (arg2)
1341 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1343 if (code == CEIL_MOD_EXPR)
1344 int1l += int2l - 1;
1345 low = int1l % int2l, hi = 0;
1346 break;
1349 /* ... fall through ... */
1351 case ROUND_MOD_EXPR:
1352 overflow = div_and_round_double (code, uns,
1353 int1l, int1h, int2l, int2h,
1354 &garbagel, &garbageh, &low, &hi);
1355 break;
1357 case MIN_EXPR:
1358 case MAX_EXPR:
1359 if (uns)
1360 low = (((unsigned HOST_WIDE_INT) int1h
1361 < (unsigned HOST_WIDE_INT) int2h)
1362 || (((unsigned HOST_WIDE_INT) int1h
1363 == (unsigned HOST_WIDE_INT) int2h)
1364 && int1l < int2l));
1365 else
1366 low = (int1h < int2h
1367 || (int1h == int2h && int1l < int2l));
1369 if (low == (code == MIN_EXPR))
1370 low = int1l, hi = int1h;
1371 else
1372 low = int2l, hi = int2h;
1373 break;
1375 default:
1376 abort ();
1379 /* If this is for a sizetype, can be represented as one (signed)
1380 HOST_WIDE_INT word, and doesn't overflow, use size_int since it caches
1381 constants. */
1382 if (is_sizetype
1383 && ((hi == 0 && (HOST_WIDE_INT) low >= 0)
1384 || (hi == -1 && (HOST_WIDE_INT) low < 0))
1385 && overflow == 0 && ! TREE_OVERFLOW (arg1) && ! TREE_OVERFLOW (arg2))
1386 return size_int_type_wide (low, type);
1387 else
1389 t = build_int_2 (low, hi);
1390 TREE_TYPE (t) = TREE_TYPE (arg1);
1393 TREE_OVERFLOW (t)
1394 = ((notrunc
1395 ? (!uns || is_sizetype) && overflow
1396 : (force_fit_type (t, (!uns || is_sizetype) && overflow)
1397 && ! no_overflow))
1398 | TREE_OVERFLOW (arg1)
1399 | TREE_OVERFLOW (arg2));
1401 /* If we're doing a size calculation, unsigned arithmetic does overflow.
1402 So check if force_fit_type truncated the value. */
1403 if (is_sizetype
1404 && ! TREE_OVERFLOW (t)
1405 && (TREE_INT_CST_HIGH (t) != hi
1406 || TREE_INT_CST_LOW (t) != low))
1407 TREE_OVERFLOW (t) = 1;
1409 TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1410 | TREE_CONSTANT_OVERFLOW (arg1)
1411 | TREE_CONSTANT_OVERFLOW (arg2));
1412 return t;
1415 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1416 constant. We assume ARG1 and ARG2 have the same data type, or at least
1417 are the same kind of constant and the same machine mode.
1419 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1421 static tree
1422 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1424 STRIP_NOPS (arg1);
1425 STRIP_NOPS (arg2);
1427 if (TREE_CODE (arg1) == INTEGER_CST)
1428 return int_const_binop (code, arg1, arg2, notrunc);
1430 if (TREE_CODE (arg1) == REAL_CST)
1432 enum machine_mode mode;
1433 REAL_VALUE_TYPE d1;
1434 REAL_VALUE_TYPE d2;
1435 REAL_VALUE_TYPE value;
1436 tree t, type;
1438 d1 = TREE_REAL_CST (arg1);
1439 d2 = TREE_REAL_CST (arg2);
1441 type = TREE_TYPE (arg1);
1442 mode = TYPE_MODE (type);
1444 /* Don't perform operation if we honor signaling NaNs and
1445 either operand is a NaN. */
1446 if (HONOR_SNANS (mode)
1447 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1448 return NULL_TREE;
1450 /* Don't perform operation if it would raise a division
1451 by zero exception. */
1452 if (code == RDIV_EXPR
1453 && REAL_VALUES_EQUAL (d2, dconst0)
1454 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1455 return NULL_TREE;
1457 /* If either operand is a NaN, just return it. Otherwise, set up
1458 for floating-point trap; we return an overflow. */
1459 if (REAL_VALUE_ISNAN (d1))
1460 return arg1;
1461 else if (REAL_VALUE_ISNAN (d2))
1462 return arg2;
1464 REAL_ARITHMETIC (value, code, d1, d2);
1466 t = build_real (type, real_value_truncate (mode, value));
1468 TREE_OVERFLOW (t)
1469 = (force_fit_type (t, 0)
1470 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1471 TREE_CONSTANT_OVERFLOW (t)
1472 = TREE_OVERFLOW (t)
1473 | TREE_CONSTANT_OVERFLOW (arg1)
1474 | TREE_CONSTANT_OVERFLOW (arg2);
1475 return t;
1477 if (TREE_CODE (arg1) == COMPLEX_CST)
1479 tree type = TREE_TYPE (arg1);
1480 tree r1 = TREE_REALPART (arg1);
1481 tree i1 = TREE_IMAGPART (arg1);
1482 tree r2 = TREE_REALPART (arg2);
1483 tree i2 = TREE_IMAGPART (arg2);
1484 tree t;
1486 switch (code)
1488 case PLUS_EXPR:
1489 t = build_complex (type,
1490 const_binop (PLUS_EXPR, r1, r2, notrunc),
1491 const_binop (PLUS_EXPR, i1, i2, notrunc));
1492 break;
1494 case MINUS_EXPR:
1495 t = build_complex (type,
1496 const_binop (MINUS_EXPR, r1, r2, notrunc),
1497 const_binop (MINUS_EXPR, i1, i2, notrunc));
1498 break;
1500 case MULT_EXPR:
1501 t = build_complex (type,
1502 const_binop (MINUS_EXPR,
1503 const_binop (MULT_EXPR,
1504 r1, r2, notrunc),
1505 const_binop (MULT_EXPR,
1506 i1, i2, notrunc),
1507 notrunc),
1508 const_binop (PLUS_EXPR,
1509 const_binop (MULT_EXPR,
1510 r1, i2, notrunc),
1511 const_binop (MULT_EXPR,
1512 i1, r2, notrunc),
1513 notrunc));
1514 break;
1516 case RDIV_EXPR:
1518 tree magsquared
1519 = const_binop (PLUS_EXPR,
1520 const_binop (MULT_EXPR, r2, r2, notrunc),
1521 const_binop (MULT_EXPR, i2, i2, notrunc),
1522 notrunc);
1524 t = build_complex (type,
1525 const_binop
1526 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1527 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1528 const_binop (PLUS_EXPR,
1529 const_binop (MULT_EXPR, r1, r2,
1530 notrunc),
1531 const_binop (MULT_EXPR, i1, i2,
1532 notrunc),
1533 notrunc),
1534 magsquared, notrunc),
1535 const_binop
1536 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1537 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1538 const_binop (MINUS_EXPR,
1539 const_binop (MULT_EXPR, i1, r2,
1540 notrunc),
1541 const_binop (MULT_EXPR, r1, i2,
1542 notrunc),
1543 notrunc),
1544 magsquared, notrunc));
1546 break;
1548 default:
1549 abort ();
1551 return t;
1553 return 0;
1556 /* These are the hash table functions for the hash table of INTEGER_CST
1557 nodes of a sizetype. */
1559 /* Return the hash code code X, an INTEGER_CST. */
1561 static hashval_t
1562 size_htab_hash (const void *x)
1564 tree t = (tree) x;
1566 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1567 ^ htab_hash_pointer (TREE_TYPE (t))
1568 ^ (TREE_OVERFLOW (t) << 20));
1571 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1572 is the same as that given by *Y, which is the same. */
1574 static int
1575 size_htab_eq (const void *x, const void *y)
1577 tree xt = (tree) x;
1578 tree yt = (tree) y;
1580 return (TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1581 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt)
1582 && TREE_TYPE (xt) == TREE_TYPE (yt)
1583 && TREE_OVERFLOW (xt) == TREE_OVERFLOW (yt));
1586 /* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
1587 bits are given by NUMBER and of the sizetype represented by KIND. */
1589 tree
1590 size_int_wide (HOST_WIDE_INT number, enum size_type_kind kind)
1592 return size_int_type_wide (number, sizetype_tab[(int) kind]);
1595 /* Likewise, but the desired type is specified explicitly. */
1597 static GTY (()) tree new_const;
1598 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
1599 htab_t size_htab;
1601 tree
1602 size_int_type_wide (HOST_WIDE_INT number, tree type)
1604 void **slot;
1606 if (size_htab == 0)
1608 size_htab = htab_create_ggc (1024, size_htab_hash, size_htab_eq, NULL);
1609 new_const = make_node (INTEGER_CST);
1612 /* Adjust NEW_CONST to be the constant we want. If it's already in the
1613 hash table, we return the value from the hash table. Otherwise, we
1614 place that in the hash table and make a new node for the next time. */
1615 TREE_INT_CST_LOW (new_const) = number;
1616 TREE_INT_CST_HIGH (new_const) = number < 0 ? -1 : 0;
1617 TREE_TYPE (new_const) = type;
1618 TREE_OVERFLOW (new_const) = TREE_CONSTANT_OVERFLOW (new_const)
1619 = force_fit_type (new_const, 0);
1621 slot = htab_find_slot (size_htab, new_const, INSERT);
1622 if (*slot == 0)
1624 tree t = new_const;
1626 *slot = new_const;
1627 new_const = make_node (INTEGER_CST);
1628 return t;
1630 else
1631 return (tree) *slot;
1634 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1635 is a tree code. The type of the result is taken from the operands.
1636 Both must be the same type integer type and it must be a size type.
1637 If the operands are constant, so is the result. */
1639 tree
1640 size_binop (enum tree_code code, tree arg0, tree arg1)
1642 tree type = TREE_TYPE (arg0);
1644 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1645 || type != TREE_TYPE (arg1))
1646 abort ();
1648 /* Handle the special case of two integer constants faster. */
1649 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1651 /* And some specific cases even faster than that. */
1652 if (code == PLUS_EXPR && integer_zerop (arg0))
1653 return arg1;
1654 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1655 && integer_zerop (arg1))
1656 return arg0;
1657 else if (code == MULT_EXPR && integer_onep (arg0))
1658 return arg1;
1660 /* Handle general case of two integer constants. */
1661 return int_const_binop (code, arg0, arg1, 0);
1664 if (arg0 == error_mark_node || arg1 == error_mark_node)
1665 return error_mark_node;
1667 return fold (build (code, type, arg0, arg1));
1670 /* Given two values, either both of sizetype or both of bitsizetype,
1671 compute the difference between the two values. Return the value
1672 in signed type corresponding to the type of the operands. */
1674 tree
1675 size_diffop (tree arg0, tree arg1)
1677 tree type = TREE_TYPE (arg0);
1678 tree ctype;
1680 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1681 || type != TREE_TYPE (arg1))
1682 abort ();
1684 /* If the type is already signed, just do the simple thing. */
1685 if (!TYPE_UNSIGNED (type))
1686 return size_binop (MINUS_EXPR, arg0, arg1);
1688 ctype = (type == bitsizetype || type == ubitsizetype
1689 ? sbitsizetype : ssizetype);
1691 /* If either operand is not a constant, do the conversions to the signed
1692 type and subtract. The hardware will do the right thing with any
1693 overflow in the subtraction. */
1694 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1695 return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
1696 fold_convert (ctype, arg1));
1698 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1699 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1700 overflow) and negate (which can't either). Special-case a result
1701 of zero while we're here. */
1702 if (tree_int_cst_equal (arg0, arg1))
1703 return fold_convert (ctype, integer_zero_node);
1704 else if (tree_int_cst_lt (arg1, arg0))
1705 return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1706 else
1707 return size_binop (MINUS_EXPR, fold_convert (ctype, integer_zero_node),
1708 fold_convert (ctype, size_binop (MINUS_EXPR,
1709 arg1, arg0)));
1713 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1714 type TYPE. If no simplification can be done return NULL_TREE. */
1716 static tree
1717 fold_convert_const (enum tree_code code, tree type, tree arg1)
1719 int overflow = 0;
1720 tree t;
1722 if (TREE_TYPE (arg1) == type)
1723 return arg1;
1725 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
1727 if (TREE_CODE (arg1) == INTEGER_CST)
1729 /* If we would build a constant wider than GCC supports,
1730 leave the conversion unfolded. */
1731 if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
1732 return NULL_TREE;
1734 /* If we are trying to make a sizetype for a small integer, use
1735 size_int to pick up cached types to reduce duplicate nodes. */
1736 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
1737 && !TREE_CONSTANT_OVERFLOW (arg1)
1738 && compare_tree_int (arg1, 10000) < 0)
1739 return size_int_type_wide (TREE_INT_CST_LOW (arg1), type);
1741 /* Given an integer constant, make new constant with new type,
1742 appropriately sign-extended or truncated. */
1743 t = build_int_2 (TREE_INT_CST_LOW (arg1),
1744 TREE_INT_CST_HIGH (arg1));
1745 TREE_TYPE (t) = type;
1746 /* Indicate an overflow if (1) ARG1 already overflowed,
1747 or (2) force_fit_type indicates an overflow.
1748 Tell force_fit_type that an overflow has already occurred
1749 if ARG1 is a too-large unsigned value and T is signed.
1750 But don't indicate an overflow if converting a pointer. */
1751 TREE_OVERFLOW (t)
1752 = ((force_fit_type (t,
1753 (TREE_INT_CST_HIGH (arg1) < 0
1754 && (TYPE_UNSIGNED (type)
1755 < TYPE_UNSIGNED (TREE_TYPE (arg1)))))
1756 && ! POINTER_TYPE_P (TREE_TYPE (arg1)))
1757 || TREE_OVERFLOW (arg1));
1758 TREE_CONSTANT_OVERFLOW (t)
1759 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1760 return t;
1762 else if (TREE_CODE (arg1) == REAL_CST)
1764 /* The following code implements the floating point to integer
1765 conversion rules required by the Java Language Specification,
1766 that IEEE NaNs are mapped to zero and values that overflow
1767 the target precision saturate, i.e. values greater than
1768 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1769 are mapped to INT_MIN. These semantics are allowed by the
1770 C and C++ standards that simply state that the behavior of
1771 FP-to-integer conversion is unspecified upon overflow. */
1773 HOST_WIDE_INT high, low;
1775 REAL_VALUE_TYPE r;
1776 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1778 switch (code)
1780 case FIX_TRUNC_EXPR:
1781 real_trunc (&r, VOIDmode, &x);
1782 break;
1784 case FIX_CEIL_EXPR:
1785 real_ceil (&r, VOIDmode, &x);
1786 break;
1788 case FIX_FLOOR_EXPR:
1789 real_floor (&r, VOIDmode, &x);
1790 break;
1792 case FIX_ROUND_EXPR:
1793 real_round (&r, VOIDmode, &x);
1794 break;
1796 default:
1797 abort ();
1800 /* If R is NaN, return zero and show we have an overflow. */
1801 if (REAL_VALUE_ISNAN (r))
1803 overflow = 1;
1804 high = 0;
1805 low = 0;
1808 /* See if R is less than the lower bound or greater than the
1809 upper bound. */
1811 if (! overflow)
1813 tree lt = TYPE_MIN_VALUE (type);
1814 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1815 if (REAL_VALUES_LESS (r, l))
1817 overflow = 1;
1818 high = TREE_INT_CST_HIGH (lt);
1819 low = TREE_INT_CST_LOW (lt);
1823 if (! overflow)
1825 tree ut = TYPE_MAX_VALUE (type);
1826 if (ut)
1828 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1829 if (REAL_VALUES_LESS (u, r))
1831 overflow = 1;
1832 high = TREE_INT_CST_HIGH (ut);
1833 low = TREE_INT_CST_LOW (ut);
1838 if (! overflow)
1839 REAL_VALUE_TO_INT (&low, &high, r);
1841 t = build_int_2 (low, high);
1842 TREE_TYPE (t) = type;
1843 TREE_OVERFLOW (t)
1844 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
1845 TREE_CONSTANT_OVERFLOW (t)
1846 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1847 return t;
1850 else if (TREE_CODE (type) == REAL_TYPE)
1852 if (TREE_CODE (arg1) == INTEGER_CST)
1853 return build_real_from_int_cst (type, arg1);
1854 if (TREE_CODE (arg1) == REAL_CST)
1856 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
1858 /* We make a copy of ARG1 so that we don't modify an
1859 existing constant tree. */
1860 t = copy_node (arg1);
1861 TREE_TYPE (t) = type;
1862 return t;
1865 t = build_real (type,
1866 real_value_truncate (TYPE_MODE (type),
1867 TREE_REAL_CST (arg1)));
1869 TREE_OVERFLOW (t)
1870 = TREE_OVERFLOW (arg1) | force_fit_type (t, 0);
1871 TREE_CONSTANT_OVERFLOW (t)
1872 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1873 return t;
1876 return NULL_TREE;
1879 /* Convert expression ARG to type TYPE. Used by the middle-end for
1880 simple conversions in preference to calling the front-end's convert. */
1882 tree
1883 fold_convert (tree type, tree arg)
1885 tree orig = TREE_TYPE (arg);
1886 tree tem;
1888 if (type == orig)
1889 return arg;
1891 if (TREE_CODE (arg) == ERROR_MARK
1892 || TREE_CODE (type) == ERROR_MARK
1893 || TREE_CODE (orig) == ERROR_MARK)
1894 return error_mark_node;
1896 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1897 return fold (build1 (NOP_EXPR, type, arg));
1899 if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1901 if (TREE_CODE (arg) == INTEGER_CST)
1903 tem = fold_convert_const (NOP_EXPR, type, arg);
1904 if (tem != NULL_TREE)
1905 return tem;
1907 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig))
1908 return fold (build1 (NOP_EXPR, type, arg));
1909 if (TREE_CODE (orig) == COMPLEX_TYPE)
1911 tem = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1912 return fold_convert (type, tem);
1914 if (TREE_CODE (orig) == VECTOR_TYPE
1915 && GET_MODE_SIZE (TYPE_MODE (type))
1916 == GET_MODE_SIZE (TYPE_MODE (orig)))
1917 return fold (build1 (NOP_EXPR, type, arg));
1919 else if (TREE_CODE (type) == REAL_TYPE)
1921 if (TREE_CODE (arg) == INTEGER_CST)
1923 tem = fold_convert_const (FLOAT_EXPR, type, arg);
1924 if (tem != NULL_TREE)
1925 return tem;
1927 else if (TREE_CODE (arg) == REAL_CST)
1929 tem = fold_convert_const (NOP_EXPR, type, arg);
1930 if (tem != NULL_TREE)
1931 return tem;
1934 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig))
1935 return fold (build1 (FLOAT_EXPR, type, arg));
1936 if (TREE_CODE (orig) == REAL_TYPE)
1937 return fold (build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
1938 type, arg));
1939 if (TREE_CODE (orig) == COMPLEX_TYPE)
1941 tem = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1942 return fold_convert (type, tem);
1945 else if (TREE_CODE (type) == COMPLEX_TYPE)
1947 if (INTEGRAL_TYPE_P (orig)
1948 || POINTER_TYPE_P (orig)
1949 || TREE_CODE (orig) == REAL_TYPE)
1950 return build (COMPLEX_EXPR, type,
1951 fold_convert (TREE_TYPE (type), arg),
1952 fold_convert (TREE_TYPE (type), integer_zero_node));
1953 if (TREE_CODE (orig) == COMPLEX_TYPE)
1955 tree rpart, ipart;
1957 if (TREE_CODE (arg) == COMPLEX_EXPR)
1959 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
1960 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
1961 return fold (build (COMPLEX_EXPR, type, rpart, ipart));
1964 arg = save_expr (arg);
1965 rpart = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1966 ipart = fold (build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg));
1967 rpart = fold_convert (TREE_TYPE (type), rpart);
1968 ipart = fold_convert (TREE_TYPE (type), ipart);
1969 return fold (build (COMPLEX_EXPR, type, rpart, ipart));
1972 else if (TREE_CODE (type) == VECTOR_TYPE)
1974 if ((INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig))
1975 && GET_MODE_SIZE (TYPE_MODE (type))
1976 == GET_MODE_SIZE (TYPE_MODE (orig)))
1977 return fold (build1 (NOP_EXPR, type, arg));
1978 if (TREE_CODE (orig) == VECTOR_TYPE
1979 && GET_MODE_SIZE (TYPE_MODE (type))
1980 == GET_MODE_SIZE (TYPE_MODE (orig)))
1981 return fold (build1 (NOP_EXPR, type, arg));
1983 else if (VOID_TYPE_P (type))
1984 return fold (build1 (CONVERT_EXPR, type, arg));
1985 abort ();
1988 /* Return an expr equal to X but certainly not valid as an lvalue. */
1990 tree
1991 non_lvalue (tree x)
1993 /* These things are certainly not lvalues. */
1994 if (TREE_CODE (x) == NON_LVALUE_EXPR
1995 || TREE_CODE (x) == INTEGER_CST
1996 || TREE_CODE (x) == REAL_CST
1997 || TREE_CODE (x) == STRING_CST
1998 || TREE_CODE (x) == ADDR_EXPR)
1999 return x;
2001 return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2004 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2005 Zero means allow extended lvalues. */
2007 int pedantic_lvalues;
2009 /* When pedantic, return an expr equal to X but certainly not valid as a
2010 pedantic lvalue. Otherwise, return X. */
2012 tree
2013 pedantic_non_lvalue (tree x)
2015 if (pedantic_lvalues)
2016 return non_lvalue (x);
2017 else
2018 return x;
2021 /* Given a tree comparison code, return the code that is the logical inverse
2022 of the given code. It is not safe to do this for floating-point
2023 comparisons, except for NE_EXPR and EQ_EXPR. */
2025 static enum tree_code
2026 invert_tree_comparison (enum tree_code code)
2028 switch (code)
2030 case EQ_EXPR:
2031 return NE_EXPR;
2032 case NE_EXPR:
2033 return EQ_EXPR;
2034 case GT_EXPR:
2035 return LE_EXPR;
2036 case GE_EXPR:
2037 return LT_EXPR;
2038 case LT_EXPR:
2039 return GE_EXPR;
2040 case LE_EXPR:
2041 return GT_EXPR;
2042 default:
2043 abort ();
2047 /* Similar, but return the comparison that results if the operands are
2048 swapped. This is safe for floating-point. */
2050 static enum tree_code
2051 swap_tree_comparison (enum tree_code code)
2053 switch (code)
2055 case EQ_EXPR:
2056 case NE_EXPR:
2057 return code;
2058 case GT_EXPR:
2059 return LT_EXPR;
2060 case GE_EXPR:
2061 return LE_EXPR;
2062 case LT_EXPR:
2063 return GT_EXPR;
2064 case LE_EXPR:
2065 return GE_EXPR;
2066 default:
2067 abort ();
2072 /* Convert a comparison tree code from an enum tree_code representation
2073 into a compcode bit-based encoding. This function is the inverse of
2074 compcode_to_comparison. */
2076 static int
2077 comparison_to_compcode (enum tree_code code)
2079 switch (code)
2081 case LT_EXPR:
2082 return COMPCODE_LT;
2083 case EQ_EXPR:
2084 return COMPCODE_EQ;
2085 case LE_EXPR:
2086 return COMPCODE_LE;
2087 case GT_EXPR:
2088 return COMPCODE_GT;
2089 case NE_EXPR:
2090 return COMPCODE_NE;
2091 case GE_EXPR:
2092 return COMPCODE_GE;
2093 default:
2094 abort ();
2098 /* Convert a compcode bit-based encoding of a comparison operator back
2099 to GCC's enum tree_code representation. This function is the
2100 inverse of comparison_to_compcode. */
2102 static enum tree_code
2103 compcode_to_comparison (int code)
2105 switch (code)
2107 case COMPCODE_LT:
2108 return LT_EXPR;
2109 case COMPCODE_EQ:
2110 return EQ_EXPR;
2111 case COMPCODE_LE:
2112 return LE_EXPR;
2113 case COMPCODE_GT:
2114 return GT_EXPR;
2115 case COMPCODE_NE:
2116 return NE_EXPR;
2117 case COMPCODE_GE:
2118 return GE_EXPR;
2119 default:
2120 abort ();
2124 /* Return nonzero if CODE is a tree code that represents a truth value. */
2126 static int
2127 truth_value_p (enum tree_code code)
2129 return (TREE_CODE_CLASS (code) == '<'
2130 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2131 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2132 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2135 /* Return nonzero if two operands (typically of the same tree node)
2136 are necessarily equal. If either argument has side-effects this
2137 function returns zero. FLAGS modifies behaviour as follows:
2139 If OEP_ONLY_CONST is set, only return nonzero for constants.
2140 This function tests whether the operands are indistinguishable;
2141 it does not test whether they are equal using C's == operation.
2142 The distinction is important for IEEE floating point, because
2143 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2144 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2146 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2147 even though it may hold multiple values during a function.
2148 This is because a GCC tree node guarantees that nothing else is
2149 executed between the evaluation of its "operands" (which may often
2150 be evaluated in arbitrary order). Hence if the operands themselves
2151 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2152 same value in each operand/subexpression. Hence a zero value for
2153 ONLY_CONST assumes isochronic (or instantaneous) tree equivalence.
2154 If comparing arbitrary expression trees, such as from different
2155 statements, ONLY_CONST must usually be nonzero.
2157 If OEP_PURE_SAME is set, then pure functions with identical arguments
2158 are considered the same. It is used when the caller has other ways
2159 to ensure that global memory is unchanged in between. */
2162 operand_equal_p (tree arg0, tree arg1, unsigned int flags)
2164 /* If either is ERROR_MARK, they aren't equal. */
2165 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
2166 return 0;
2168 /* If both types don't have the same signedness, then we can't consider
2169 them equal. We must check this before the STRIP_NOPS calls
2170 because they may change the signedness of the arguments. */
2171 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2172 return 0;
2174 STRIP_NOPS (arg0);
2175 STRIP_NOPS (arg1);
2177 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2178 /* This is needed for conversions and for COMPONENT_REF.
2179 Might as well play it safe and always test this. */
2180 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2181 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2182 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2183 return 0;
2185 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2186 We don't care about side effects in that case because the SAVE_EXPR
2187 takes care of that for us. In all other cases, two expressions are
2188 equal if they have no side effects. If we have two identical
2189 expressions with side effects that should be treated the same due
2190 to the only side effects being identical SAVE_EXPR's, that will
2191 be detected in the recursive calls below. */
2192 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2193 && (TREE_CODE (arg0) == SAVE_EXPR
2194 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2195 return 1;
2197 /* Next handle constant cases, those for which we can return 1 even
2198 if ONLY_CONST is set. */
2199 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2200 switch (TREE_CODE (arg0))
2202 case INTEGER_CST:
2203 return (! TREE_CONSTANT_OVERFLOW (arg0)
2204 && ! TREE_CONSTANT_OVERFLOW (arg1)
2205 && tree_int_cst_equal (arg0, arg1));
2207 case REAL_CST:
2208 return (! TREE_CONSTANT_OVERFLOW (arg0)
2209 && ! TREE_CONSTANT_OVERFLOW (arg1)
2210 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2211 TREE_REAL_CST (arg1)));
2213 case VECTOR_CST:
2215 tree v1, v2;
2217 if (TREE_CONSTANT_OVERFLOW (arg0)
2218 || TREE_CONSTANT_OVERFLOW (arg1))
2219 return 0;
2221 v1 = TREE_VECTOR_CST_ELTS (arg0);
2222 v2 = TREE_VECTOR_CST_ELTS (arg1);
2223 while (v1 && v2)
2225 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2226 flags))
2227 return 0;
2228 v1 = TREE_CHAIN (v1);
2229 v2 = TREE_CHAIN (v2);
2232 return 1;
2235 case COMPLEX_CST:
2236 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2237 flags)
2238 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2239 flags));
2241 case STRING_CST:
2242 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2243 && ! memcmp (TREE_STRING_POINTER (arg0),
2244 TREE_STRING_POINTER (arg1),
2245 TREE_STRING_LENGTH (arg0)));
2247 case ADDR_EXPR:
2248 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2250 default:
2251 break;
2254 if (flags & OEP_ONLY_CONST)
2255 return 0;
2257 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2259 case '1':
2260 /* Two conversions are equal only if signedness and modes match. */
2261 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
2262 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
2263 != TYPE_UNSIGNED (TREE_TYPE (arg1))))
2264 return 0;
2266 return operand_equal_p (TREE_OPERAND (arg0, 0),
2267 TREE_OPERAND (arg1, 0), flags);
2269 case '<':
2270 case '2':
2271 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)
2272 && operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1),
2274 return 1;
2276 /* For commutative ops, allow the other order. */
2277 return (commutative_tree_code (TREE_CODE (arg0))
2278 && operand_equal_p (TREE_OPERAND (arg0, 0),
2279 TREE_OPERAND (arg1, 1), flags)
2280 && operand_equal_p (TREE_OPERAND (arg0, 1),
2281 TREE_OPERAND (arg1, 0), flags));
2283 case 'r':
2284 /* If either of the pointer (or reference) expressions we are
2285 dereferencing contain a side effect, these cannot be equal. */
2286 if (TREE_SIDE_EFFECTS (arg0)
2287 || TREE_SIDE_EFFECTS (arg1))
2288 return 0;
2290 switch (TREE_CODE (arg0))
2292 case INDIRECT_REF:
2293 return operand_equal_p (TREE_OPERAND (arg0, 0),
2294 TREE_OPERAND (arg1, 0), flags);
2296 case COMPONENT_REF:
2297 case ARRAY_REF:
2298 case ARRAY_RANGE_REF:
2299 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2300 TREE_OPERAND (arg1, 0), flags)
2301 && operand_equal_p (TREE_OPERAND (arg0, 1),
2302 TREE_OPERAND (arg1, 1), flags));
2304 case BIT_FIELD_REF:
2305 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2306 TREE_OPERAND (arg1, 0), flags)
2307 && operand_equal_p (TREE_OPERAND (arg0, 1),
2308 TREE_OPERAND (arg1, 1), flags)
2309 && operand_equal_p (TREE_OPERAND (arg0, 2),
2310 TREE_OPERAND (arg1, 2), flags));
2311 default:
2312 return 0;
2315 case 'e':
2316 switch (TREE_CODE (arg0))
2318 case ADDR_EXPR:
2319 case TRUTH_NOT_EXPR:
2320 return operand_equal_p (TREE_OPERAND (arg0, 0),
2321 TREE_OPERAND (arg1, 0), flags);
2323 case RTL_EXPR:
2324 return rtx_equal_p (RTL_EXPR_RTL (arg0), RTL_EXPR_RTL (arg1));
2326 case CALL_EXPR:
2327 /* If the CALL_EXPRs call different functions, then they
2328 clearly can not be equal. */
2329 if (! operand_equal_p (TREE_OPERAND (arg0, 0),
2330 TREE_OPERAND (arg1, 0), flags))
2331 return 0;
2334 unsigned int cef = call_expr_flags (arg0);
2335 if (flags & OEP_PURE_SAME)
2336 cef &= ECF_CONST | ECF_PURE;
2337 else
2338 cef &= ECF_CONST;
2339 if (!cef)
2340 return 0;
2343 /* Now see if all the arguments are the same. operand_equal_p
2344 does not handle TREE_LIST, so we walk the operands here
2345 feeding them to operand_equal_p. */
2346 arg0 = TREE_OPERAND (arg0, 1);
2347 arg1 = TREE_OPERAND (arg1, 1);
2348 while (arg0 && arg1)
2350 if (! operand_equal_p (TREE_VALUE (arg0), TREE_VALUE (arg1),
2351 flags))
2352 return 0;
2354 arg0 = TREE_CHAIN (arg0);
2355 arg1 = TREE_CHAIN (arg1);
2358 /* If we get here and both argument lists are exhausted
2359 then the CALL_EXPRs are equal. */
2360 return ! (arg0 || arg1);
2362 default:
2363 return 0;
2366 case 'd':
2367 /* Consider __builtin_sqrt equal to sqrt. */
2368 return (TREE_CODE (arg0) == FUNCTION_DECL
2369 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2370 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2371 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2373 default:
2374 return 0;
2378 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2379 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2381 When in doubt, return 0. */
2383 static int
2384 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2386 int unsignedp1, unsignedpo;
2387 tree primarg0, primarg1, primother;
2388 unsigned int correct_width;
2390 if (operand_equal_p (arg0, arg1, 0))
2391 return 1;
2393 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2394 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2395 return 0;
2397 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2398 and see if the inner values are the same. This removes any
2399 signedness comparison, which doesn't matter here. */
2400 primarg0 = arg0, primarg1 = arg1;
2401 STRIP_NOPS (primarg0);
2402 STRIP_NOPS (primarg1);
2403 if (operand_equal_p (primarg0, primarg1, 0))
2404 return 1;
2406 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2407 actual comparison operand, ARG0.
2409 First throw away any conversions to wider types
2410 already present in the operands. */
2412 primarg1 = get_narrower (arg1, &unsignedp1);
2413 primother = get_narrower (other, &unsignedpo);
2415 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2416 if (unsignedp1 == unsignedpo
2417 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2418 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2420 tree type = TREE_TYPE (arg0);
2422 /* Make sure shorter operand is extended the right way
2423 to match the longer operand. */
2424 primarg1 = fold_convert (lang_hooks.types.signed_or_unsigned_type
2425 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2427 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2428 return 1;
2431 return 0;
2434 /* See if ARG is an expression that is either a comparison or is performing
2435 arithmetic on comparisons. The comparisons must only be comparing
2436 two different values, which will be stored in *CVAL1 and *CVAL2; if
2437 they are nonzero it means that some operands have already been found.
2438 No variables may be used anywhere else in the expression except in the
2439 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2440 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2442 If this is true, return 1. Otherwise, return zero. */
2444 static int
2445 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2447 enum tree_code code = TREE_CODE (arg);
2448 char class = TREE_CODE_CLASS (code);
2450 /* We can handle some of the 'e' cases here. */
2451 if (class == 'e' && code == TRUTH_NOT_EXPR)
2452 class = '1';
2453 else if (class == 'e'
2454 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2455 || code == COMPOUND_EXPR))
2456 class = '2';
2458 else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0
2459 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2461 /* If we've already found a CVAL1 or CVAL2, this expression is
2462 two complex to handle. */
2463 if (*cval1 || *cval2)
2464 return 0;
2466 class = '1';
2467 *save_p = 1;
2470 switch (class)
2472 case '1':
2473 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2475 case '2':
2476 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2477 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2478 cval1, cval2, save_p));
2480 case 'c':
2481 return 1;
2483 case 'e':
2484 if (code == COND_EXPR)
2485 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2486 cval1, cval2, save_p)
2487 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2488 cval1, cval2, save_p)
2489 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2490 cval1, cval2, save_p));
2491 return 0;
2493 case '<':
2494 /* First see if we can handle the first operand, then the second. For
2495 the second operand, we know *CVAL1 can't be zero. It must be that
2496 one side of the comparison is each of the values; test for the
2497 case where this isn't true by failing if the two operands
2498 are the same. */
2500 if (operand_equal_p (TREE_OPERAND (arg, 0),
2501 TREE_OPERAND (arg, 1), 0))
2502 return 0;
2504 if (*cval1 == 0)
2505 *cval1 = TREE_OPERAND (arg, 0);
2506 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2508 else if (*cval2 == 0)
2509 *cval2 = TREE_OPERAND (arg, 0);
2510 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2512 else
2513 return 0;
2515 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2517 else if (*cval2 == 0)
2518 *cval2 = TREE_OPERAND (arg, 1);
2519 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2521 else
2522 return 0;
2524 return 1;
2526 default:
2527 return 0;
2531 /* ARG is a tree that is known to contain just arithmetic operations and
2532 comparisons. Evaluate the operations in the tree substituting NEW0 for
2533 any occurrence of OLD0 as an operand of a comparison and likewise for
2534 NEW1 and OLD1. */
2536 static tree
2537 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
2539 tree type = TREE_TYPE (arg);
2540 enum tree_code code = TREE_CODE (arg);
2541 char class = TREE_CODE_CLASS (code);
2543 /* We can handle some of the 'e' cases here. */
2544 if (class == 'e' && code == TRUTH_NOT_EXPR)
2545 class = '1';
2546 else if (class == 'e'
2547 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2548 class = '2';
2550 switch (class)
2552 case '1':
2553 return fold (build1 (code, type,
2554 eval_subst (TREE_OPERAND (arg, 0),
2555 old0, new0, old1, new1)));
2557 case '2':
2558 return fold (build (code, type,
2559 eval_subst (TREE_OPERAND (arg, 0),
2560 old0, new0, old1, new1),
2561 eval_subst (TREE_OPERAND (arg, 1),
2562 old0, new0, old1, new1)));
2564 case 'e':
2565 switch (code)
2567 case SAVE_EXPR:
2568 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2570 case COMPOUND_EXPR:
2571 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2573 case COND_EXPR:
2574 return fold (build (code, type,
2575 eval_subst (TREE_OPERAND (arg, 0),
2576 old0, new0, old1, new1),
2577 eval_subst (TREE_OPERAND (arg, 1),
2578 old0, new0, old1, new1),
2579 eval_subst (TREE_OPERAND (arg, 2),
2580 old0, new0, old1, new1)));
2581 default:
2582 break;
2584 /* Fall through - ??? */
2586 case '<':
2588 tree arg0 = TREE_OPERAND (arg, 0);
2589 tree arg1 = TREE_OPERAND (arg, 1);
2591 /* We need to check both for exact equality and tree equality. The
2592 former will be true if the operand has a side-effect. In that
2593 case, we know the operand occurred exactly once. */
2595 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2596 arg0 = new0;
2597 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2598 arg0 = new1;
2600 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2601 arg1 = new0;
2602 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2603 arg1 = new1;
2605 return fold (build (code, type, arg0, arg1));
2608 default:
2609 return arg;
2613 /* Return a tree for the case when the result of an expression is RESULT
2614 converted to TYPE and OMITTED was previously an operand of the expression
2615 but is now not needed (e.g., we folded OMITTED * 0).
2617 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2618 the conversion of RESULT to TYPE. */
2620 tree
2621 omit_one_operand (tree type, tree result, tree omitted)
2623 tree t = fold_convert (type, result);
2625 if (TREE_SIDE_EFFECTS (omitted))
2626 return build (COMPOUND_EXPR, type, omitted, t);
2628 return non_lvalue (t);
2631 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2633 static tree
2634 pedantic_omit_one_operand (tree type, tree result, tree omitted)
2636 tree t = fold_convert (type, result);
2638 if (TREE_SIDE_EFFECTS (omitted))
2639 return build (COMPOUND_EXPR, type, omitted, t);
2641 return pedantic_non_lvalue (t);
2644 /* Return a simplified tree node for the truth-negation of ARG. This
2645 never alters ARG itself. We assume that ARG is an operation that
2646 returns a truth value (0 or 1). */
2648 tree
2649 invert_truthvalue (tree arg)
2651 tree type = TREE_TYPE (arg);
2652 enum tree_code code = TREE_CODE (arg);
2654 if (code == ERROR_MARK)
2655 return arg;
2657 /* If this is a comparison, we can simply invert it, except for
2658 floating-point non-equality comparisons, in which case we just
2659 enclose a TRUTH_NOT_EXPR around what we have. */
2661 if (TREE_CODE_CLASS (code) == '<')
2663 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
2664 && !flag_unsafe_math_optimizations
2665 && code != NE_EXPR
2666 && code != EQ_EXPR)
2667 return build1 (TRUTH_NOT_EXPR, type, arg);
2668 else if (code == UNORDERED_EXPR
2669 || code == ORDERED_EXPR
2670 || code == UNEQ_EXPR
2671 || code == UNLT_EXPR
2672 || code == UNLE_EXPR
2673 || code == UNGT_EXPR
2674 || code == UNGE_EXPR)
2675 return build1 (TRUTH_NOT_EXPR, type, arg);
2676 else
2677 return build (invert_tree_comparison (code), type,
2678 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2681 switch (code)
2683 case INTEGER_CST:
2684 return fold_convert (type, build_int_2 (integer_zerop (arg), 0));
2686 case TRUTH_AND_EXPR:
2687 return build (TRUTH_OR_EXPR, type,
2688 invert_truthvalue (TREE_OPERAND (arg, 0)),
2689 invert_truthvalue (TREE_OPERAND (arg, 1)));
2691 case TRUTH_OR_EXPR:
2692 return build (TRUTH_AND_EXPR, type,
2693 invert_truthvalue (TREE_OPERAND (arg, 0)),
2694 invert_truthvalue (TREE_OPERAND (arg, 1)));
2696 case TRUTH_XOR_EXPR:
2697 /* Here we can invert either operand. We invert the first operand
2698 unless the second operand is a TRUTH_NOT_EXPR in which case our
2699 result is the XOR of the first operand with the inside of the
2700 negation of the second operand. */
2702 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2703 return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2704 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2705 else
2706 return build (TRUTH_XOR_EXPR, type,
2707 invert_truthvalue (TREE_OPERAND (arg, 0)),
2708 TREE_OPERAND (arg, 1));
2710 case TRUTH_ANDIF_EXPR:
2711 return build (TRUTH_ORIF_EXPR, type,
2712 invert_truthvalue (TREE_OPERAND (arg, 0)),
2713 invert_truthvalue (TREE_OPERAND (arg, 1)));
2715 case TRUTH_ORIF_EXPR:
2716 return build (TRUTH_ANDIF_EXPR, type,
2717 invert_truthvalue (TREE_OPERAND (arg, 0)),
2718 invert_truthvalue (TREE_OPERAND (arg, 1)));
2720 case TRUTH_NOT_EXPR:
2721 return TREE_OPERAND (arg, 0);
2723 case COND_EXPR:
2724 return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2725 invert_truthvalue (TREE_OPERAND (arg, 1)),
2726 invert_truthvalue (TREE_OPERAND (arg, 2)));
2728 case COMPOUND_EXPR:
2729 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2730 invert_truthvalue (TREE_OPERAND (arg, 1)));
2732 case NON_LVALUE_EXPR:
2733 return invert_truthvalue (TREE_OPERAND (arg, 0));
2735 case NOP_EXPR:
2736 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2737 break;
2739 case CONVERT_EXPR:
2740 case FLOAT_EXPR:
2741 return build1 (TREE_CODE (arg), type,
2742 invert_truthvalue (TREE_OPERAND (arg, 0)));
2744 case BIT_AND_EXPR:
2745 if (!integer_onep (TREE_OPERAND (arg, 1)))
2746 break;
2747 return build (EQ_EXPR, type, arg,
2748 fold_convert (type, integer_zero_node));
2750 case SAVE_EXPR:
2751 return build1 (TRUTH_NOT_EXPR, type, arg);
2753 case CLEANUP_POINT_EXPR:
2754 return build1 (CLEANUP_POINT_EXPR, type,
2755 invert_truthvalue (TREE_OPERAND (arg, 0)));
2757 default:
2758 break;
2760 if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
2761 abort ();
2762 return build1 (TRUTH_NOT_EXPR, type, arg);
2765 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2766 operands are another bit-wise operation with a common input. If so,
2767 distribute the bit operations to save an operation and possibly two if
2768 constants are involved. For example, convert
2769 (A | B) & (A | C) into A | (B & C)
2770 Further simplification will occur if B and C are constants.
2772 If this optimization cannot be done, 0 will be returned. */
2774 static tree
2775 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
2777 tree common;
2778 tree left, right;
2780 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2781 || TREE_CODE (arg0) == code
2782 || (TREE_CODE (arg0) != BIT_AND_EXPR
2783 && TREE_CODE (arg0) != BIT_IOR_EXPR))
2784 return 0;
2786 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2788 common = TREE_OPERAND (arg0, 0);
2789 left = TREE_OPERAND (arg0, 1);
2790 right = TREE_OPERAND (arg1, 1);
2792 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2794 common = TREE_OPERAND (arg0, 0);
2795 left = TREE_OPERAND (arg0, 1);
2796 right = TREE_OPERAND (arg1, 0);
2798 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2800 common = TREE_OPERAND (arg0, 1);
2801 left = TREE_OPERAND (arg0, 0);
2802 right = TREE_OPERAND (arg1, 1);
2804 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2806 common = TREE_OPERAND (arg0, 1);
2807 left = TREE_OPERAND (arg0, 0);
2808 right = TREE_OPERAND (arg1, 0);
2810 else
2811 return 0;
2813 return fold (build (TREE_CODE (arg0), type, common,
2814 fold (build (code, type, left, right))));
2817 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2818 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
2820 static tree
2821 make_bit_field_ref (tree inner, tree type, int bitsize, int bitpos,
2822 int unsignedp)
2824 tree result = build (BIT_FIELD_REF, type, inner,
2825 size_int (bitsize), bitsize_int (bitpos));
2827 BIT_FIELD_REF_UNSIGNED (result) = unsignedp;
2829 return result;
2832 /* Optimize a bit-field compare.
2834 There are two cases: First is a compare against a constant and the
2835 second is a comparison of two items where the fields are at the same
2836 bit position relative to the start of a chunk (byte, halfword, word)
2837 large enough to contain it. In these cases we can avoid the shift
2838 implicit in bitfield extractions.
2840 For constants, we emit a compare of the shifted constant with the
2841 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2842 compared. For two fields at the same position, we do the ANDs with the
2843 similar mask and compare the result of the ANDs.
2845 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2846 COMPARE_TYPE is the type of the comparison, and LHS and RHS
2847 are the left and right operands of the comparison, respectively.
2849 If the optimization described above can be done, we return the resulting
2850 tree. Otherwise we return zero. */
2852 static tree
2853 optimize_bit_field_compare (enum tree_code code, tree compare_type,
2854 tree lhs, tree rhs)
2856 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
2857 tree type = TREE_TYPE (lhs);
2858 tree signed_type, unsigned_type;
2859 int const_p = TREE_CODE (rhs) == INTEGER_CST;
2860 enum machine_mode lmode, rmode, nmode;
2861 int lunsignedp, runsignedp;
2862 int lvolatilep = 0, rvolatilep = 0;
2863 tree linner, rinner = NULL_TREE;
2864 tree mask;
2865 tree offset;
2867 /* Get all the information about the extractions being done. If the bit size
2868 if the same as the size of the underlying object, we aren't doing an
2869 extraction at all and so can do nothing. We also don't want to
2870 do anything if the inner expression is a PLACEHOLDER_EXPR since we
2871 then will no longer be able to replace it. */
2872 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
2873 &lunsignedp, &lvolatilep);
2874 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
2875 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
2876 return 0;
2878 if (!const_p)
2880 /* If this is not a constant, we can only do something if bit positions,
2881 sizes, and signedness are the same. */
2882 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
2883 &runsignedp, &rvolatilep);
2885 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
2886 || lunsignedp != runsignedp || offset != 0
2887 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
2888 return 0;
2891 /* See if we can find a mode to refer to this field. We should be able to,
2892 but fail if we can't. */
2893 nmode = get_best_mode (lbitsize, lbitpos,
2894 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
2895 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
2896 TYPE_ALIGN (TREE_TYPE (rinner))),
2897 word_mode, lvolatilep || rvolatilep);
2898 if (nmode == VOIDmode)
2899 return 0;
2901 /* Set signed and unsigned types of the precision of this mode for the
2902 shifts below. */
2903 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
2904 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
2906 /* Compute the bit position and size for the new reference and our offset
2907 within it. If the new reference is the same size as the original, we
2908 won't optimize anything, so return zero. */
2909 nbitsize = GET_MODE_BITSIZE (nmode);
2910 nbitpos = lbitpos & ~ (nbitsize - 1);
2911 lbitpos -= nbitpos;
2912 if (nbitsize == lbitsize)
2913 return 0;
2915 if (BYTES_BIG_ENDIAN)
2916 lbitpos = nbitsize - lbitsize - lbitpos;
2918 /* Make the mask to be used against the extracted field. */
2919 mask = build_int_2 (~0, ~0);
2920 TREE_TYPE (mask) = unsigned_type;
2921 force_fit_type (mask, 0);
2922 mask = fold_convert (unsigned_type, mask);
2923 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
2924 mask = const_binop (RSHIFT_EXPR, mask,
2925 size_int (nbitsize - lbitsize - lbitpos), 0);
2927 if (! const_p)
2928 /* If not comparing with constant, just rework the comparison
2929 and return. */
2930 return build (code, compare_type,
2931 build (BIT_AND_EXPR, unsigned_type,
2932 make_bit_field_ref (linner, unsigned_type,
2933 nbitsize, nbitpos, 1),
2934 mask),
2935 build (BIT_AND_EXPR, unsigned_type,
2936 make_bit_field_ref (rinner, unsigned_type,
2937 nbitsize, nbitpos, 1),
2938 mask));
2940 /* Otherwise, we are handling the constant case. See if the constant is too
2941 big for the field. Warn and return a tree of for 0 (false) if so. We do
2942 this not only for its own sake, but to avoid having to test for this
2943 error case below. If we didn't, we might generate wrong code.
2945 For unsigned fields, the constant shifted right by the field length should
2946 be all zero. For signed fields, the high-order bits should agree with
2947 the sign bit. */
2949 if (lunsignedp)
2951 if (! integer_zerop (const_binop (RSHIFT_EXPR,
2952 fold_convert (unsigned_type, rhs),
2953 size_int (lbitsize), 0)))
2955 warning ("comparison is always %d due to width of bit-field",
2956 code == NE_EXPR);
2957 return fold_convert (compare_type,
2958 (code == NE_EXPR
2959 ? integer_one_node : integer_zero_node));
2962 else
2964 tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
2965 size_int (lbitsize - 1), 0);
2966 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
2968 warning ("comparison is always %d due to width of bit-field",
2969 code == NE_EXPR);
2970 return fold_convert (compare_type,
2971 (code == NE_EXPR
2972 ? integer_one_node : integer_zero_node));
2976 /* Single-bit compares should always be against zero. */
2977 if (lbitsize == 1 && ! integer_zerop (rhs))
2979 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
2980 rhs = fold_convert (type, integer_zero_node);
2983 /* Make a new bitfield reference, shift the constant over the
2984 appropriate number of bits and mask it with the computed mask
2985 (in case this was a signed field). If we changed it, make a new one. */
2986 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
2987 if (lvolatilep)
2989 TREE_SIDE_EFFECTS (lhs) = 1;
2990 TREE_THIS_VOLATILE (lhs) = 1;
2993 rhs = fold (const_binop (BIT_AND_EXPR,
2994 const_binop (LSHIFT_EXPR,
2995 fold_convert (unsigned_type, rhs),
2996 size_int (lbitpos), 0),
2997 mask, 0));
2999 return build (code, compare_type,
3000 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
3001 rhs);
3004 /* Subroutine for fold_truthop: decode a field reference.
3006 If EXP is a comparison reference, we return the innermost reference.
3008 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3009 set to the starting bit number.
3011 If the innermost field can be completely contained in a mode-sized
3012 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3014 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3015 otherwise it is not changed.
3017 *PUNSIGNEDP is set to the signedness of the field.
3019 *PMASK is set to the mask used. This is either contained in a
3020 BIT_AND_EXPR or derived from the width of the field.
3022 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3024 Return 0 if this is not a component reference or is one that we can't
3025 do anything with. */
3027 static tree
3028 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3029 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3030 int *punsignedp, int *pvolatilep,
3031 tree *pmask, tree *pand_mask)
3033 tree outer_type = 0;
3034 tree and_mask = 0;
3035 tree mask, inner, offset;
3036 tree unsigned_type;
3037 unsigned int precision;
3039 /* All the optimizations using this function assume integer fields.
3040 There are problems with FP fields since the type_for_size call
3041 below can fail for, e.g., XFmode. */
3042 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3043 return 0;
3045 /* We are interested in the bare arrangement of bits, so strip everything
3046 that doesn't affect the machine mode. However, record the type of the
3047 outermost expression if it may matter below. */
3048 if (TREE_CODE (exp) == NOP_EXPR
3049 || TREE_CODE (exp) == CONVERT_EXPR
3050 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3051 outer_type = TREE_TYPE (exp);
3052 STRIP_NOPS (exp);
3054 if (TREE_CODE (exp) == BIT_AND_EXPR)
3056 and_mask = TREE_OPERAND (exp, 1);
3057 exp = TREE_OPERAND (exp, 0);
3058 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3059 if (TREE_CODE (and_mask) != INTEGER_CST)
3060 return 0;
3063 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3064 punsignedp, pvolatilep);
3065 if ((inner == exp && and_mask == 0)
3066 || *pbitsize < 0 || offset != 0
3067 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3068 return 0;
3070 /* If the number of bits in the reference is the same as the bitsize of
3071 the outer type, then the outer type gives the signedness. Otherwise
3072 (in case of a small bitfield) the signedness is unchanged. */
3073 if (outer_type && *pbitsize == tree_low_cst (TYPE_SIZE (outer_type), 1))
3074 *punsignedp = TYPE_UNSIGNED (outer_type);
3076 /* Compute the mask to access the bitfield. */
3077 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3078 precision = TYPE_PRECISION (unsigned_type);
3080 mask = build_int_2 (~0, ~0);
3081 TREE_TYPE (mask) = unsigned_type;
3082 force_fit_type (mask, 0);
3083 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3084 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3086 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3087 if (and_mask != 0)
3088 mask = fold (build (BIT_AND_EXPR, unsigned_type,
3089 fold_convert (unsigned_type, and_mask), mask));
3091 *pmask = mask;
3092 *pand_mask = and_mask;
3093 return inner;
3096 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3097 bit positions. */
3099 static int
3100 all_ones_mask_p (tree mask, int size)
3102 tree type = TREE_TYPE (mask);
3103 unsigned int precision = TYPE_PRECISION (type);
3104 tree tmask;
3106 tmask = build_int_2 (~0, ~0);
3107 TREE_TYPE (tmask) = lang_hooks.types.signed_type (type);
3108 force_fit_type (tmask, 0);
3109 return
3110 tree_int_cst_equal (mask,
3111 const_binop (RSHIFT_EXPR,
3112 const_binop (LSHIFT_EXPR, tmask,
3113 size_int (precision - size),
3115 size_int (precision - size), 0));
3118 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3119 represents the sign bit of EXP's type. If EXP represents a sign
3120 or zero extension, also test VAL against the unextended type.
3121 The return value is the (sub)expression whose sign bit is VAL,
3122 or NULL_TREE otherwise. */
3124 static tree
3125 sign_bit_p (tree exp, tree val)
3127 unsigned HOST_WIDE_INT mask_lo, lo;
3128 HOST_WIDE_INT mask_hi, hi;
3129 int width;
3130 tree t;
3132 /* Tree EXP must have an integral type. */
3133 t = TREE_TYPE (exp);
3134 if (! INTEGRAL_TYPE_P (t))
3135 return NULL_TREE;
3137 /* Tree VAL must be an integer constant. */
3138 if (TREE_CODE (val) != INTEGER_CST
3139 || TREE_CONSTANT_OVERFLOW (val))
3140 return NULL_TREE;
3142 width = TYPE_PRECISION (t);
3143 if (width > HOST_BITS_PER_WIDE_INT)
3145 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3146 lo = 0;
3148 mask_hi = ((unsigned HOST_WIDE_INT) -1
3149 >> (2 * HOST_BITS_PER_WIDE_INT - width));
3150 mask_lo = -1;
3152 else
3154 hi = 0;
3155 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3157 mask_hi = 0;
3158 mask_lo = ((unsigned HOST_WIDE_INT) -1
3159 >> (HOST_BITS_PER_WIDE_INT - width));
3162 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3163 treat VAL as if it were unsigned. */
3164 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3165 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3166 return exp;
3168 /* Handle extension from a narrower type. */
3169 if (TREE_CODE (exp) == NOP_EXPR
3170 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3171 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3173 return NULL_TREE;
3176 /* Subroutine for fold_truthop: determine if an operand is simple enough
3177 to be evaluated unconditionally. */
3179 static int
3180 simple_operand_p (tree exp)
3182 /* Strip any conversions that don't change the machine mode. */
3183 while ((TREE_CODE (exp) == NOP_EXPR
3184 || TREE_CODE (exp) == CONVERT_EXPR)
3185 && (TYPE_MODE (TREE_TYPE (exp))
3186 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
3187 exp = TREE_OPERAND (exp, 0);
3189 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
3190 || (DECL_P (exp)
3191 && ! TREE_ADDRESSABLE (exp)
3192 && ! TREE_THIS_VOLATILE (exp)
3193 && ! DECL_NONLOCAL (exp)
3194 /* Don't regard global variables as simple. They may be
3195 allocated in ways unknown to the compiler (shared memory,
3196 #pragma weak, etc). */
3197 && ! TREE_PUBLIC (exp)
3198 && ! DECL_EXTERNAL (exp)
3199 /* Loading a static variable is unduly expensive, but global
3200 registers aren't expensive. */
3201 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3204 /* The following functions are subroutines to fold_range_test and allow it to
3205 try to change a logical combination of comparisons into a range test.
3207 For example, both
3208 X == 2 || X == 3 || X == 4 || X == 5
3210 X >= 2 && X <= 5
3211 are converted to
3212 (unsigned) (X - 2) <= 3
3214 We describe each set of comparisons as being either inside or outside
3215 a range, using a variable named like IN_P, and then describe the
3216 range with a lower and upper bound. If one of the bounds is omitted,
3217 it represents either the highest or lowest value of the type.
3219 In the comments below, we represent a range by two numbers in brackets
3220 preceded by a "+" to designate being inside that range, or a "-" to
3221 designate being outside that range, so the condition can be inverted by
3222 flipping the prefix. An omitted bound is represented by a "-". For
3223 example, "- [-, 10]" means being outside the range starting at the lowest
3224 possible value and ending at 10, in other words, being greater than 10.
3225 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3226 always false.
3228 We set up things so that the missing bounds are handled in a consistent
3229 manner so neither a missing bound nor "true" and "false" need to be
3230 handled using a special case. */
3232 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3233 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3234 and UPPER1_P are nonzero if the respective argument is an upper bound
3235 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3236 must be specified for a comparison. ARG1 will be converted to ARG0's
3237 type if both are specified. */
3239 static tree
3240 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3241 tree arg1, int upper1_p)
3243 tree tem;
3244 int result;
3245 int sgn0, sgn1;
3247 /* If neither arg represents infinity, do the normal operation.
3248 Else, if not a comparison, return infinity. Else handle the special
3249 comparison rules. Note that most of the cases below won't occur, but
3250 are handled for consistency. */
3252 if (arg0 != 0 && arg1 != 0)
3254 tem = fold (build (code, type != 0 ? type : TREE_TYPE (arg0),
3255 arg0, fold_convert (TREE_TYPE (arg0), arg1)));
3256 STRIP_NOPS (tem);
3257 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3260 if (TREE_CODE_CLASS (code) != '<')
3261 return 0;
3263 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3264 for neither. In real maths, we cannot assume open ended ranges are
3265 the same. But, this is computer arithmetic, where numbers are finite.
3266 We can therefore make the transformation of any unbounded range with
3267 the value Z, Z being greater than any representable number. This permits
3268 us to treat unbounded ranges as equal. */
3269 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3270 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3271 switch (code)
3273 case EQ_EXPR:
3274 result = sgn0 == sgn1;
3275 break;
3276 case NE_EXPR:
3277 result = sgn0 != sgn1;
3278 break;
3279 case LT_EXPR:
3280 result = sgn0 < sgn1;
3281 break;
3282 case LE_EXPR:
3283 result = sgn0 <= sgn1;
3284 break;
3285 case GT_EXPR:
3286 result = sgn0 > sgn1;
3287 break;
3288 case GE_EXPR:
3289 result = sgn0 >= sgn1;
3290 break;
3291 default:
3292 abort ();
3295 return fold_convert (type, result ? integer_one_node : integer_zero_node);
3298 /* Given EXP, a logical expression, set the range it is testing into
3299 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3300 actually being tested. *PLOW and *PHIGH will be made of the same type
3301 as the returned expression. If EXP is not a comparison, we will most
3302 likely not be returning a useful value and range. */
3304 static tree
3305 make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
3307 enum tree_code code;
3308 tree arg0 = NULL_TREE, arg1 = NULL_TREE, type = NULL_TREE;
3309 tree orig_type = NULL_TREE;
3310 int in_p, n_in_p;
3311 tree low, high, n_low, n_high;
3313 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3314 and see if we can refine the range. Some of the cases below may not
3315 happen, but it doesn't seem worth worrying about this. We "continue"
3316 the outer loop when we've changed something; otherwise we "break"
3317 the switch, which will "break" the while. */
3319 in_p = 0;
3320 low = high = fold_convert (TREE_TYPE (exp), integer_zero_node);
3322 while (1)
3324 code = TREE_CODE (exp);
3326 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3328 if (first_rtl_op (code) > 0)
3329 arg0 = TREE_OPERAND (exp, 0);
3330 if (TREE_CODE_CLASS (code) == '<'
3331 || TREE_CODE_CLASS (code) == '1'
3332 || TREE_CODE_CLASS (code) == '2')
3333 type = TREE_TYPE (arg0);
3334 if (TREE_CODE_CLASS (code) == '2'
3335 || TREE_CODE_CLASS (code) == '<'
3336 || (TREE_CODE_CLASS (code) == 'e'
3337 && TREE_CODE_LENGTH (code) > 1))
3338 arg1 = TREE_OPERAND (exp, 1);
3341 /* Set ORIG_TYPE as soon as TYPE is non-null so that we do not
3342 lose a cast by accident. */
3343 if (type != NULL_TREE && orig_type == NULL_TREE)
3344 orig_type = type;
3346 switch (code)
3348 case TRUTH_NOT_EXPR:
3349 in_p = ! in_p, exp = arg0;
3350 continue;
3352 case EQ_EXPR: case NE_EXPR:
3353 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3354 /* We can only do something if the range is testing for zero
3355 and if the second operand is an integer constant. Note that
3356 saying something is "in" the range we make is done by
3357 complementing IN_P since it will set in the initial case of
3358 being not equal to zero; "out" is leaving it alone. */
3359 if (low == 0 || high == 0
3360 || ! integer_zerop (low) || ! integer_zerop (high)
3361 || TREE_CODE (arg1) != INTEGER_CST)
3362 break;
3364 switch (code)
3366 case NE_EXPR: /* - [c, c] */
3367 low = high = arg1;
3368 break;
3369 case EQ_EXPR: /* + [c, c] */
3370 in_p = ! in_p, low = high = arg1;
3371 break;
3372 case GT_EXPR: /* - [-, c] */
3373 low = 0, high = arg1;
3374 break;
3375 case GE_EXPR: /* + [c, -] */
3376 in_p = ! in_p, low = arg1, high = 0;
3377 break;
3378 case LT_EXPR: /* - [c, -] */
3379 low = arg1, high = 0;
3380 break;
3381 case LE_EXPR: /* + [-, c] */
3382 in_p = ! in_p, low = 0, high = arg1;
3383 break;
3384 default:
3385 abort ();
3388 exp = arg0;
3390 /* If this is an unsigned comparison, we also know that EXP is
3391 greater than or equal to zero. We base the range tests we make
3392 on that fact, so we record it here so we can parse existing
3393 range tests. */
3394 if (TYPE_UNSIGNED (type) && (low == 0 || high == 0))
3396 if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
3397 1, fold_convert (type, integer_zero_node),
3398 NULL_TREE))
3399 break;
3401 in_p = n_in_p, low = n_low, high = n_high;
3403 /* If the high bound is missing, but we have a nonzero low
3404 bound, reverse the range so it goes from zero to the low bound
3405 minus 1. */
3406 if (high == 0 && low && ! integer_zerop (low))
3408 in_p = ! in_p;
3409 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3410 integer_one_node, 0);
3411 low = fold_convert (type, integer_zero_node);
3414 continue;
3416 case NEGATE_EXPR:
3417 /* (-x) IN [a,b] -> x in [-b, -a] */
3418 n_low = range_binop (MINUS_EXPR, type,
3419 fold_convert (type, integer_zero_node),
3420 0, high, 1);
3421 n_high = range_binop (MINUS_EXPR, type,
3422 fold_convert (type, integer_zero_node),
3423 0, low, 0);
3424 low = n_low, high = n_high;
3425 exp = arg0;
3426 continue;
3428 case BIT_NOT_EXPR:
3429 /* ~ X -> -X - 1 */
3430 exp = build (MINUS_EXPR, type, negate_expr (arg0),
3431 fold_convert (type, integer_one_node));
3432 continue;
3434 case PLUS_EXPR: case MINUS_EXPR:
3435 if (TREE_CODE (arg1) != INTEGER_CST)
3436 break;
3438 /* If EXP is signed, any overflow in the computation is undefined,
3439 so we don't worry about it so long as our computations on
3440 the bounds don't overflow. For unsigned, overflow is defined
3441 and this is exactly the right thing. */
3442 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3443 type, low, 0, arg1, 0);
3444 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3445 type, high, 1, arg1, 0);
3446 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3447 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3448 break;
3450 /* Check for an unsigned range which has wrapped around the maximum
3451 value thus making n_high < n_low, and normalize it. */
3452 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3454 low = range_binop (PLUS_EXPR, type, n_high, 0,
3455 integer_one_node, 0);
3456 high = range_binop (MINUS_EXPR, type, n_low, 0,
3457 integer_one_node, 0);
3459 /* If the range is of the form +/- [ x+1, x ], we won't
3460 be able to normalize it. But then, it represents the
3461 whole range or the empty set, so make it
3462 +/- [ -, - ]. */
3463 if (tree_int_cst_equal (n_low, low)
3464 && tree_int_cst_equal (n_high, high))
3465 low = high = 0;
3466 else
3467 in_p = ! in_p;
3469 else
3470 low = n_low, high = n_high;
3472 exp = arg0;
3473 continue;
3475 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
3476 if (TYPE_PRECISION (type) > TYPE_PRECISION (orig_type))
3477 break;
3479 if (! INTEGRAL_TYPE_P (type)
3480 || (low != 0 && ! int_fits_type_p (low, type))
3481 || (high != 0 && ! int_fits_type_p (high, type)))
3482 break;
3484 n_low = low, n_high = high;
3486 if (n_low != 0)
3487 n_low = fold_convert (type, n_low);
3489 if (n_high != 0)
3490 n_high = fold_convert (type, n_high);
3492 /* If we're converting from an unsigned to a signed type,
3493 we will be doing the comparison as unsigned. The tests above
3494 have already verified that LOW and HIGH are both positive.
3496 So we have to make sure that the original unsigned value will
3497 be interpreted as positive. */
3498 if (TYPE_UNSIGNED (type) && ! TYPE_UNSIGNED (TREE_TYPE (exp)))
3500 tree equiv_type = lang_hooks.types.type_for_mode
3501 (TYPE_MODE (type), 1);
3502 tree high_positive;
3504 /* A range without an upper bound is, naturally, unbounded.
3505 Since convert would have cropped a very large value, use
3506 the max value for the destination type. */
3507 high_positive
3508 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3509 : TYPE_MAX_VALUE (type);
3511 if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (exp)))
3512 high_positive = fold (build (RSHIFT_EXPR, type,
3513 fold_convert (type,
3514 high_positive),
3515 fold_convert (type,
3516 integer_one_node)));
3518 /* If the low bound is specified, "and" the range with the
3519 range for which the original unsigned value will be
3520 positive. */
3521 if (low != 0)
3523 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3524 1, n_low, n_high, 1,
3525 fold_convert (type, integer_zero_node),
3526 high_positive))
3527 break;
3529 in_p = (n_in_p == in_p);
3531 else
3533 /* Otherwise, "or" the range with the range of the input
3534 that will be interpreted as negative. */
3535 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3536 0, n_low, n_high, 1,
3537 fold_convert (type, integer_zero_node),
3538 high_positive))
3539 break;
3541 in_p = (in_p != n_in_p);
3545 exp = arg0;
3546 low = n_low, high = n_high;
3547 continue;
3549 default:
3550 break;
3553 break;
3556 /* If EXP is a constant, we can evaluate whether this is true or false. */
3557 if (TREE_CODE (exp) == INTEGER_CST)
3559 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3560 exp, 0, low, 0))
3561 && integer_onep (range_binop (LE_EXPR, integer_type_node,
3562 exp, 1, high, 1)));
3563 low = high = 0;
3564 exp = 0;
3567 *pin_p = in_p, *plow = low, *phigh = high;
3568 return exp;
3571 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3572 type, TYPE, return an expression to test if EXP is in (or out of, depending
3573 on IN_P) the range. */
3575 static tree
3576 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
3578 tree etype = TREE_TYPE (exp);
3579 tree value;
3581 if (! in_p
3582 && (0 != (value = build_range_check (type, exp, 1, low, high))))
3583 return invert_truthvalue (value);
3585 if (low == 0 && high == 0)
3586 return fold_convert (type, integer_one_node);
3588 if (low == 0)
3589 return fold (build (LE_EXPR, type, exp, high));
3591 if (high == 0)
3592 return fold (build (GE_EXPR, type, exp, low));
3594 if (operand_equal_p (low, high, 0))
3595 return fold (build (EQ_EXPR, type, exp, low));
3597 if (integer_zerop (low))
3599 if (! TYPE_UNSIGNED (etype))
3601 etype = lang_hooks.types.unsigned_type (etype);
3602 high = fold_convert (etype, high);
3603 exp = fold_convert (etype, exp);
3605 return build_range_check (type, exp, 1, 0, high);
3608 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
3609 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
3611 unsigned HOST_WIDE_INT lo;
3612 HOST_WIDE_INT hi;
3613 int prec;
3615 prec = TYPE_PRECISION (etype);
3616 if (prec <= HOST_BITS_PER_WIDE_INT)
3618 hi = 0;
3619 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
3621 else
3623 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
3624 lo = (unsigned HOST_WIDE_INT) -1;
3627 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
3629 if (TYPE_UNSIGNED (etype))
3631 etype = lang_hooks.types.signed_type (etype);
3632 exp = fold_convert (etype, exp);
3634 return fold (build (GT_EXPR, type, exp,
3635 fold_convert (etype, integer_zero_node)));
3639 if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
3640 && ! TREE_OVERFLOW (value))
3641 return build_range_check (type,
3642 fold (build (MINUS_EXPR, etype, exp, low)),
3643 1, fold_convert (etype, integer_zero_node),
3644 value);
3646 return 0;
3649 /* Given two ranges, see if we can merge them into one. Return 1 if we
3650 can, 0 if we can't. Set the output range into the specified parameters. */
3652 static int
3653 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
3654 tree high0, int in1_p, tree low1, tree high1)
3656 int no_overlap;
3657 int subset;
3658 int temp;
3659 tree tem;
3660 int in_p;
3661 tree low, high;
3662 int lowequal = ((low0 == 0 && low1 == 0)
3663 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3664 low0, 0, low1, 0)));
3665 int highequal = ((high0 == 0 && high1 == 0)
3666 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3667 high0, 1, high1, 1)));
3669 /* Make range 0 be the range that starts first, or ends last if they
3670 start at the same value. Swap them if it isn't. */
3671 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3672 low0, 0, low1, 0))
3673 || (lowequal
3674 && integer_onep (range_binop (GT_EXPR, integer_type_node,
3675 high1, 1, high0, 1))))
3677 temp = in0_p, in0_p = in1_p, in1_p = temp;
3678 tem = low0, low0 = low1, low1 = tem;
3679 tem = high0, high0 = high1, high1 = tem;
3682 /* Now flag two cases, whether the ranges are disjoint or whether the
3683 second range is totally subsumed in the first. Note that the tests
3684 below are simplified by the ones above. */
3685 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3686 high0, 1, low1, 0));
3687 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3688 high1, 1, high0, 1));
3690 /* We now have four cases, depending on whether we are including or
3691 excluding the two ranges. */
3692 if (in0_p && in1_p)
3694 /* If they don't overlap, the result is false. If the second range
3695 is a subset it is the result. Otherwise, the range is from the start
3696 of the second to the end of the first. */
3697 if (no_overlap)
3698 in_p = 0, low = high = 0;
3699 else if (subset)
3700 in_p = 1, low = low1, high = high1;
3701 else
3702 in_p = 1, low = low1, high = high0;
3705 else if (in0_p && ! in1_p)
3707 /* If they don't overlap, the result is the first range. If they are
3708 equal, the result is false. If the second range is a subset of the
3709 first, and the ranges begin at the same place, we go from just after
3710 the end of the first range to the end of the second. If the second
3711 range is not a subset of the first, or if it is a subset and both
3712 ranges end at the same place, the range starts at the start of the
3713 first range and ends just before the second range.
3714 Otherwise, we can't describe this as a single range. */
3715 if (no_overlap)
3716 in_p = 1, low = low0, high = high0;
3717 else if (lowequal && highequal)
3718 in_p = 0, low = high = 0;
3719 else if (subset && lowequal)
3721 in_p = 1, high = high0;
3722 low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
3723 integer_one_node, 0);
3725 else if (! subset || highequal)
3727 in_p = 1, low = low0;
3728 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
3729 integer_one_node, 0);
3731 else
3732 return 0;
3735 else if (! in0_p && in1_p)
3737 /* If they don't overlap, the result is the second range. If the second
3738 is a subset of the first, the result is false. Otherwise,
3739 the range starts just after the first range and ends at the
3740 end of the second. */
3741 if (no_overlap)
3742 in_p = 1, low = low1, high = high1;
3743 else if (subset || highequal)
3744 in_p = 0, low = high = 0;
3745 else
3747 in_p = 1, high = high1;
3748 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3749 integer_one_node, 0);
3753 else
3755 /* The case where we are excluding both ranges. Here the complex case
3756 is if they don't overlap. In that case, the only time we have a
3757 range is if they are adjacent. If the second is a subset of the
3758 first, the result is the first. Otherwise, the range to exclude
3759 starts at the beginning of the first range and ends at the end of the
3760 second. */
3761 if (no_overlap)
3763 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3764 range_binop (PLUS_EXPR, NULL_TREE,
3765 high0, 1,
3766 integer_one_node, 1),
3767 1, low1, 0)))
3768 in_p = 0, low = low0, high = high1;
3769 else
3770 return 0;
3772 else if (subset)
3773 in_p = 0, low = low0, high = high0;
3774 else
3775 in_p = 0, low = low0, high = high1;
3778 *pin_p = in_p, *plow = low, *phigh = high;
3779 return 1;
3782 #ifndef RANGE_TEST_NON_SHORT_CIRCUIT
3783 #define RANGE_TEST_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
3784 #endif
3786 /* EXP is some logical combination of boolean tests. See if we can
3787 merge it into some range test. Return the new tree if so. */
3789 static tree
3790 fold_range_test (tree exp)
3792 int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3793 || TREE_CODE (exp) == TRUTH_OR_EXPR);
3794 int in0_p, in1_p, in_p;
3795 tree low0, low1, low, high0, high1, high;
3796 tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
3797 tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
3798 tree tem;
3800 /* If this is an OR operation, invert both sides; we will invert
3801 again at the end. */
3802 if (or_op)
3803 in0_p = ! in0_p, in1_p = ! in1_p;
3805 /* If both expressions are the same, if we can merge the ranges, and we
3806 can build the range test, return it or it inverted. If one of the
3807 ranges is always true or always false, consider it to be the same
3808 expression as the other. */
3809 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
3810 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
3811 in1_p, low1, high1)
3812 && 0 != (tem = (build_range_check (TREE_TYPE (exp),
3813 lhs != 0 ? lhs
3814 : rhs != 0 ? rhs : integer_zero_node,
3815 in_p, low, high))))
3816 return or_op ? invert_truthvalue (tem) : tem;
3818 /* On machines where the branch cost is expensive, if this is a
3819 short-circuited branch and the underlying object on both sides
3820 is the same, make a non-short-circuit operation. */
3821 else if (RANGE_TEST_NON_SHORT_CIRCUIT
3822 && lhs != 0 && rhs != 0
3823 && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3824 || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
3825 && operand_equal_p (lhs, rhs, 0))
3827 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
3828 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
3829 which cases we can't do this. */
3830 if (simple_operand_p (lhs))
3831 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3832 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3833 TREE_TYPE (exp), TREE_OPERAND (exp, 0),
3834 TREE_OPERAND (exp, 1));
3836 else if (lang_hooks.decls.global_bindings_p () == 0
3837 && ! CONTAINS_PLACEHOLDER_P (lhs))
3839 tree common = save_expr (lhs);
3841 if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
3842 or_op ? ! in0_p : in0_p,
3843 low0, high0))
3844 && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
3845 or_op ? ! in1_p : in1_p,
3846 low1, high1))))
3847 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3848 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3849 TREE_TYPE (exp), lhs, rhs);
3853 return 0;
3856 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
3857 bit value. Arrange things so the extra bits will be set to zero if and
3858 only if C is signed-extended to its full width. If MASK is nonzero,
3859 it is an INTEGER_CST that should be AND'ed with the extra bits. */
3861 static tree
3862 unextend (tree c, int p, int unsignedp, tree mask)
3864 tree type = TREE_TYPE (c);
3865 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
3866 tree temp;
3868 if (p == modesize || unsignedp)
3869 return c;
3871 /* We work by getting just the sign bit into the low-order bit, then
3872 into the high-order bit, then sign-extend. We then XOR that value
3873 with C. */
3874 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
3875 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
3877 /* We must use a signed type in order to get an arithmetic right shift.
3878 However, we must also avoid introducing accidental overflows, so that
3879 a subsequent call to integer_zerop will work. Hence we must
3880 do the type conversion here. At this point, the constant is either
3881 zero or one, and the conversion to a signed type can never overflow.
3882 We could get an overflow if this conversion is done anywhere else. */
3883 if (TYPE_UNSIGNED (type))
3884 temp = fold_convert (lang_hooks.types.signed_type (type), temp);
3886 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
3887 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
3888 if (mask != 0)
3889 temp = const_binop (BIT_AND_EXPR, temp,
3890 fold_convert (TREE_TYPE (c), mask), 0);
3891 /* If necessary, convert the type back to match the type of C. */
3892 if (TYPE_UNSIGNED (type))
3893 temp = fold_convert (type, temp);
3895 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
3898 /* Find ways of folding logical expressions of LHS and RHS:
3899 Try to merge two comparisons to the same innermost item.
3900 Look for range tests like "ch >= '0' && ch <= '9'".
3901 Look for combinations of simple terms on machines with expensive branches
3902 and evaluate the RHS unconditionally.
3904 For example, if we have p->a == 2 && p->b == 4 and we can make an
3905 object large enough to span both A and B, we can do this with a comparison
3906 against the object ANDed with the a mask.
3908 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
3909 operations to do this with one comparison.
3911 We check for both normal comparisons and the BIT_AND_EXPRs made this by
3912 function and the one above.
3914 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
3915 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
3917 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
3918 two operands.
3920 We return the simplified tree or 0 if no optimization is possible. */
3922 static tree
3923 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
3925 /* If this is the "or" of two comparisons, we can do something if
3926 the comparisons are NE_EXPR. If this is the "and", we can do something
3927 if the comparisons are EQ_EXPR. I.e.,
3928 (a->b == 2 && a->c == 4) can become (a->new == NEW).
3930 WANTED_CODE is this operation code. For single bit fields, we can
3931 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
3932 comparison for one-bit fields. */
3934 enum tree_code wanted_code;
3935 enum tree_code lcode, rcode;
3936 tree ll_arg, lr_arg, rl_arg, rr_arg;
3937 tree ll_inner, lr_inner, rl_inner, rr_inner;
3938 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
3939 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
3940 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
3941 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
3942 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
3943 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
3944 enum machine_mode lnmode, rnmode;
3945 tree ll_mask, lr_mask, rl_mask, rr_mask;
3946 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
3947 tree l_const, r_const;
3948 tree lntype, rntype, result;
3949 int first_bit, end_bit;
3950 int volatilep;
3952 /* Start by getting the comparison codes. Fail if anything is volatile.
3953 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
3954 it were surrounded with a NE_EXPR. */
3956 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
3957 return 0;
3959 lcode = TREE_CODE (lhs);
3960 rcode = TREE_CODE (rhs);
3962 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
3963 lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
3965 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
3966 rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
3968 if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
3969 return 0;
3971 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
3972 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
3974 ll_arg = TREE_OPERAND (lhs, 0);
3975 lr_arg = TREE_OPERAND (lhs, 1);
3976 rl_arg = TREE_OPERAND (rhs, 0);
3977 rr_arg = TREE_OPERAND (rhs, 1);
3979 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
3980 if (simple_operand_p (ll_arg)
3981 && simple_operand_p (lr_arg)
3982 && !FLOAT_TYPE_P (TREE_TYPE (ll_arg)))
3984 int compcode;
3986 if (operand_equal_p (ll_arg, rl_arg, 0)
3987 && operand_equal_p (lr_arg, rr_arg, 0))
3989 int lcompcode, rcompcode;
3991 lcompcode = comparison_to_compcode (lcode);
3992 rcompcode = comparison_to_compcode (rcode);
3993 compcode = (code == TRUTH_AND_EXPR)
3994 ? lcompcode & rcompcode
3995 : lcompcode | rcompcode;
3997 else if (operand_equal_p (ll_arg, rr_arg, 0)
3998 && operand_equal_p (lr_arg, rl_arg, 0))
4000 int lcompcode, rcompcode;
4002 rcode = swap_tree_comparison (rcode);
4003 lcompcode = comparison_to_compcode (lcode);
4004 rcompcode = comparison_to_compcode (rcode);
4005 compcode = (code == TRUTH_AND_EXPR)
4006 ? lcompcode & rcompcode
4007 : lcompcode | rcompcode;
4009 else
4010 compcode = -1;
4012 if (compcode == COMPCODE_TRUE)
4013 return fold_convert (truth_type, integer_one_node);
4014 else if (compcode == COMPCODE_FALSE)
4015 return fold_convert (truth_type, integer_zero_node);
4016 else if (compcode != -1)
4017 return build (compcode_to_comparison (compcode),
4018 truth_type, ll_arg, lr_arg);
4021 /* If the RHS can be evaluated unconditionally and its operands are
4022 simple, it wins to evaluate the RHS unconditionally on machines
4023 with expensive branches. In this case, this isn't a comparison
4024 that can be merged. Avoid doing this if the RHS is a floating-point
4025 comparison since those can trap. */
4027 if (BRANCH_COST >= 2
4028 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
4029 && simple_operand_p (rl_arg)
4030 && simple_operand_p (rr_arg))
4032 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
4033 if (code == TRUTH_OR_EXPR
4034 && lcode == NE_EXPR && integer_zerop (lr_arg)
4035 && rcode == NE_EXPR && integer_zerop (rr_arg)
4036 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4037 return build (NE_EXPR, truth_type,
4038 build (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4039 ll_arg, rl_arg),
4040 integer_zero_node);
4042 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
4043 if (code == TRUTH_AND_EXPR
4044 && lcode == EQ_EXPR && integer_zerop (lr_arg)
4045 && rcode == EQ_EXPR && integer_zerop (rr_arg)
4046 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4047 return build (EQ_EXPR, truth_type,
4048 build (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4049 ll_arg, rl_arg),
4050 integer_zero_node);
4052 return build (code, truth_type, lhs, rhs);
4055 /* See if the comparisons can be merged. Then get all the parameters for
4056 each side. */
4058 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
4059 || (rcode != EQ_EXPR && rcode != NE_EXPR))
4060 return 0;
4062 volatilep = 0;
4063 ll_inner = decode_field_reference (ll_arg,
4064 &ll_bitsize, &ll_bitpos, &ll_mode,
4065 &ll_unsignedp, &volatilep, &ll_mask,
4066 &ll_and_mask);
4067 lr_inner = decode_field_reference (lr_arg,
4068 &lr_bitsize, &lr_bitpos, &lr_mode,
4069 &lr_unsignedp, &volatilep, &lr_mask,
4070 &lr_and_mask);
4071 rl_inner = decode_field_reference (rl_arg,
4072 &rl_bitsize, &rl_bitpos, &rl_mode,
4073 &rl_unsignedp, &volatilep, &rl_mask,
4074 &rl_and_mask);
4075 rr_inner = decode_field_reference (rr_arg,
4076 &rr_bitsize, &rr_bitpos, &rr_mode,
4077 &rr_unsignedp, &volatilep, &rr_mask,
4078 &rr_and_mask);
4080 /* It must be true that the inner operation on the lhs of each
4081 comparison must be the same if we are to be able to do anything.
4082 Then see if we have constants. If not, the same must be true for
4083 the rhs's. */
4084 if (volatilep || ll_inner == 0 || rl_inner == 0
4085 || ! operand_equal_p (ll_inner, rl_inner, 0))
4086 return 0;
4088 if (TREE_CODE (lr_arg) == INTEGER_CST
4089 && TREE_CODE (rr_arg) == INTEGER_CST)
4090 l_const = lr_arg, r_const = rr_arg;
4091 else if (lr_inner == 0 || rr_inner == 0
4092 || ! operand_equal_p (lr_inner, rr_inner, 0))
4093 return 0;
4094 else
4095 l_const = r_const = 0;
4097 /* If either comparison code is not correct for our logical operation,
4098 fail. However, we can convert a one-bit comparison against zero into
4099 the opposite comparison against that bit being set in the field. */
4101 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
4102 if (lcode != wanted_code)
4104 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
4106 /* Make the left operand unsigned, since we are only interested
4107 in the value of one bit. Otherwise we are doing the wrong
4108 thing below. */
4109 ll_unsignedp = 1;
4110 l_const = ll_mask;
4112 else
4113 return 0;
4116 /* This is analogous to the code for l_const above. */
4117 if (rcode != wanted_code)
4119 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
4121 rl_unsignedp = 1;
4122 r_const = rl_mask;
4124 else
4125 return 0;
4128 /* After this point all optimizations will generate bit-field
4129 references, which we might not want. */
4130 if (! lang_hooks.can_use_bit_fields_p ())
4131 return 0;
4133 /* See if we can find a mode that contains both fields being compared on
4134 the left. If we can't, fail. Otherwise, update all constants and masks
4135 to be relative to a field of that size. */
4136 first_bit = MIN (ll_bitpos, rl_bitpos);
4137 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4138 lnmode = get_best_mode (end_bit - first_bit, first_bit,
4139 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4140 volatilep);
4141 if (lnmode == VOIDmode)
4142 return 0;
4144 lnbitsize = GET_MODE_BITSIZE (lnmode);
4145 lnbitpos = first_bit & ~ (lnbitsize - 1);
4146 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
4147 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
4149 if (BYTES_BIG_ENDIAN)
4151 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
4152 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
4155 ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
4156 size_int (xll_bitpos), 0);
4157 rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
4158 size_int (xrl_bitpos), 0);
4160 if (l_const)
4162 l_const = fold_convert (lntype, l_const);
4163 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
4164 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
4165 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
4166 fold (build1 (BIT_NOT_EXPR,
4167 lntype, ll_mask)),
4168 0)))
4170 warning ("comparison is always %d", wanted_code == NE_EXPR);
4172 return fold_convert (truth_type,
4173 wanted_code == NE_EXPR
4174 ? integer_one_node : integer_zero_node);
4177 if (r_const)
4179 r_const = fold_convert (lntype, r_const);
4180 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
4181 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
4182 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4183 fold (build1 (BIT_NOT_EXPR,
4184 lntype, rl_mask)),
4185 0)))
4187 warning ("comparison is always %d", wanted_code == NE_EXPR);
4189 return fold_convert (truth_type,
4190 wanted_code == NE_EXPR
4191 ? integer_one_node : integer_zero_node);
4195 /* If the right sides are not constant, do the same for it. Also,
4196 disallow this optimization if a size or signedness mismatch occurs
4197 between the left and right sides. */
4198 if (l_const == 0)
4200 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
4201 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4202 /* Make sure the two fields on the right
4203 correspond to the left without being swapped. */
4204 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
4205 return 0;
4207 first_bit = MIN (lr_bitpos, rr_bitpos);
4208 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4209 rnmode = get_best_mode (end_bit - first_bit, first_bit,
4210 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4211 volatilep);
4212 if (rnmode == VOIDmode)
4213 return 0;
4215 rnbitsize = GET_MODE_BITSIZE (rnmode);
4216 rnbitpos = first_bit & ~ (rnbitsize - 1);
4217 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
4218 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4220 if (BYTES_BIG_ENDIAN)
4222 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4223 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4226 lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
4227 size_int (xlr_bitpos), 0);
4228 rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
4229 size_int (xrr_bitpos), 0);
4231 /* Make a mask that corresponds to both fields being compared.
4232 Do this for both items being compared. If the operands are the
4233 same size and the bits being compared are in the same position
4234 then we can do this by masking both and comparing the masked
4235 results. */
4236 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4237 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
4238 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
4240 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4241 ll_unsignedp || rl_unsignedp);
4242 if (! all_ones_mask_p (ll_mask, lnbitsize))
4243 lhs = build (BIT_AND_EXPR, lntype, lhs, ll_mask);
4245 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
4246 lr_unsignedp || rr_unsignedp);
4247 if (! all_ones_mask_p (lr_mask, rnbitsize))
4248 rhs = build (BIT_AND_EXPR, rntype, rhs, lr_mask);
4250 return build (wanted_code, truth_type, lhs, rhs);
4253 /* There is still another way we can do something: If both pairs of
4254 fields being compared are adjacent, we may be able to make a wider
4255 field containing them both.
4257 Note that we still must mask the lhs/rhs expressions. Furthermore,
4258 the mask must be shifted to account for the shift done by
4259 make_bit_field_ref. */
4260 if ((ll_bitsize + ll_bitpos == rl_bitpos
4261 && lr_bitsize + lr_bitpos == rr_bitpos)
4262 || (ll_bitpos == rl_bitpos + rl_bitsize
4263 && lr_bitpos == rr_bitpos + rr_bitsize))
4265 tree type;
4267 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
4268 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
4269 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4270 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4272 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4273 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
4274 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4275 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4277 /* Convert to the smaller type before masking out unwanted bits. */
4278 type = lntype;
4279 if (lntype != rntype)
4281 if (lnbitsize > rnbitsize)
4283 lhs = fold_convert (rntype, lhs);
4284 ll_mask = fold_convert (rntype, ll_mask);
4285 type = rntype;
4287 else if (lnbitsize < rnbitsize)
4289 rhs = fold_convert (lntype, rhs);
4290 lr_mask = fold_convert (lntype, lr_mask);
4291 type = lntype;
4295 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
4296 lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
4298 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
4299 rhs = build (BIT_AND_EXPR, type, rhs, lr_mask);
4301 return build (wanted_code, truth_type, lhs, rhs);
4304 return 0;
4307 /* Handle the case of comparisons with constants. If there is something in
4308 common between the masks, those bits of the constants must be the same.
4309 If not, the condition is always false. Test for this to avoid generating
4310 incorrect code below. */
4311 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
4312 if (! integer_zerop (result)
4313 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
4314 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
4316 if (wanted_code == NE_EXPR)
4318 warning ("`or' of unmatched not-equal tests is always 1");
4319 return fold_convert (truth_type, integer_one_node);
4321 else
4323 warning ("`and' of mutually exclusive equal-tests is always 0");
4324 return fold_convert (truth_type, integer_zero_node);
4328 /* Construct the expression we will return. First get the component
4329 reference we will make. Unless the mask is all ones the width of
4330 that field, perform the mask operation. Then compare with the
4331 merged constant. */
4332 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4333 ll_unsignedp || rl_unsignedp);
4335 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4336 if (! all_ones_mask_p (ll_mask, lnbitsize))
4337 result = build (BIT_AND_EXPR, lntype, result, ll_mask);
4339 return build (wanted_code, truth_type, result,
4340 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
4343 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4344 constant. */
4346 static tree
4347 optimize_minmax_comparison (tree t)
4349 tree type = TREE_TYPE (t);
4350 tree arg0 = TREE_OPERAND (t, 0);
4351 enum tree_code op_code;
4352 tree comp_const = TREE_OPERAND (t, 1);
4353 tree minmax_const;
4354 int consts_equal, consts_lt;
4355 tree inner;
4357 STRIP_SIGN_NOPS (arg0);
4359 op_code = TREE_CODE (arg0);
4360 minmax_const = TREE_OPERAND (arg0, 1);
4361 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
4362 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4363 inner = TREE_OPERAND (arg0, 0);
4365 /* If something does not permit us to optimize, return the original tree. */
4366 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4367 || TREE_CODE (comp_const) != INTEGER_CST
4368 || TREE_CONSTANT_OVERFLOW (comp_const)
4369 || TREE_CODE (minmax_const) != INTEGER_CST
4370 || TREE_CONSTANT_OVERFLOW (minmax_const))
4371 return t;
4373 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4374 and GT_EXPR, doing the rest with recursive calls using logical
4375 simplifications. */
4376 switch (TREE_CODE (t))
4378 case NE_EXPR: case LT_EXPR: case LE_EXPR:
4379 return
4380 invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t)));
4382 case GE_EXPR:
4383 return
4384 fold (build (TRUTH_ORIF_EXPR, type,
4385 optimize_minmax_comparison
4386 (build (EQ_EXPR, type, arg0, comp_const)),
4387 optimize_minmax_comparison
4388 (build (GT_EXPR, type, arg0, comp_const))));
4390 case EQ_EXPR:
4391 if (op_code == MAX_EXPR && consts_equal)
4392 /* MAX (X, 0) == 0 -> X <= 0 */
4393 return fold (build (LE_EXPR, type, inner, comp_const));
4395 else if (op_code == MAX_EXPR && consts_lt)
4396 /* MAX (X, 0) == 5 -> X == 5 */
4397 return fold (build (EQ_EXPR, type, inner, comp_const));
4399 else if (op_code == MAX_EXPR)
4400 /* MAX (X, 0) == -1 -> false */
4401 return omit_one_operand (type, integer_zero_node, inner);
4403 else if (consts_equal)
4404 /* MIN (X, 0) == 0 -> X >= 0 */
4405 return fold (build (GE_EXPR, type, inner, comp_const));
4407 else if (consts_lt)
4408 /* MIN (X, 0) == 5 -> false */
4409 return omit_one_operand (type, integer_zero_node, inner);
4411 else
4412 /* MIN (X, 0) == -1 -> X == -1 */
4413 return fold (build (EQ_EXPR, type, inner, comp_const));
4415 case GT_EXPR:
4416 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
4417 /* MAX (X, 0) > 0 -> X > 0
4418 MAX (X, 0) > 5 -> X > 5 */
4419 return fold (build (GT_EXPR, type, inner, comp_const));
4421 else if (op_code == MAX_EXPR)
4422 /* MAX (X, 0) > -1 -> true */
4423 return omit_one_operand (type, integer_one_node, inner);
4425 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
4426 /* MIN (X, 0) > 0 -> false
4427 MIN (X, 0) > 5 -> false */
4428 return omit_one_operand (type, integer_zero_node, inner);
4430 else
4431 /* MIN (X, 0) > -1 -> X > -1 */
4432 return fold (build (GT_EXPR, type, inner, comp_const));
4434 default:
4435 return t;
4439 /* T is an integer expression that is being multiplied, divided, or taken a
4440 modulus (CODE says which and what kind of divide or modulus) by a
4441 constant C. See if we can eliminate that operation by folding it with
4442 other operations already in T. WIDE_TYPE, if non-null, is a type that
4443 should be used for the computation if wider than our type.
4445 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
4446 (X * 2) + (Y * 4). We must, however, be assured that either the original
4447 expression would not overflow or that overflow is undefined for the type
4448 in the language in question.
4450 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
4451 the machine has a multiply-accumulate insn or that this is part of an
4452 addressing calculation.
4454 If we return a non-null expression, it is an equivalent form of the
4455 original computation, but need not be in the original type. */
4457 static tree
4458 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
4460 /* To avoid exponential search depth, refuse to allow recursion past
4461 three levels. Beyond that (1) it's highly unlikely that we'll find
4462 something interesting and (2) we've probably processed it before
4463 when we built the inner expression. */
4465 static int depth;
4466 tree ret;
4468 if (depth > 3)
4469 return NULL;
4471 depth++;
4472 ret = extract_muldiv_1 (t, c, code, wide_type);
4473 depth--;
4475 return ret;
4478 static tree
4479 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
4481 tree type = TREE_TYPE (t);
4482 enum tree_code tcode = TREE_CODE (t);
4483 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
4484 > GET_MODE_SIZE (TYPE_MODE (type)))
4485 ? wide_type : type);
4486 tree t1, t2;
4487 int same_p = tcode == code;
4488 tree op0 = NULL_TREE, op1 = NULL_TREE;
4490 /* Don't deal with constants of zero here; they confuse the code below. */
4491 if (integer_zerop (c))
4492 return NULL_TREE;
4494 if (TREE_CODE_CLASS (tcode) == '1')
4495 op0 = TREE_OPERAND (t, 0);
4497 if (TREE_CODE_CLASS (tcode) == '2')
4498 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
4500 /* Note that we need not handle conditional operations here since fold
4501 already handles those cases. So just do arithmetic here. */
4502 switch (tcode)
4504 case INTEGER_CST:
4505 /* For a constant, we can always simplify if we are a multiply
4506 or (for divide and modulus) if it is a multiple of our constant. */
4507 if (code == MULT_EXPR
4508 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
4509 return const_binop (code, fold_convert (ctype, t),
4510 fold_convert (ctype, c), 0);
4511 break;
4513 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
4514 /* If op0 is an expression ... */
4515 if ((TREE_CODE_CLASS (TREE_CODE (op0)) == '<'
4516 || TREE_CODE_CLASS (TREE_CODE (op0)) == '1'
4517 || TREE_CODE_CLASS (TREE_CODE (op0)) == '2'
4518 || TREE_CODE_CLASS (TREE_CODE (op0)) == 'e')
4519 /* ... and is unsigned, and its type is smaller than ctype,
4520 then we cannot pass through as widening. */
4521 && ((TYPE_UNSIGNED (TREE_TYPE (op0))
4522 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
4523 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
4524 && (GET_MODE_SIZE (TYPE_MODE (ctype))
4525 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
4526 /* ... or its type is larger than ctype,
4527 then we cannot pass through this truncation. */
4528 || (GET_MODE_SIZE (TYPE_MODE (ctype))
4529 < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
4530 /* ... or signedness changes for division or modulus,
4531 then we cannot pass through this conversion. */
4532 || (code != MULT_EXPR
4533 && (TYPE_UNSIGNED (ctype)
4534 != TYPE_UNSIGNED (TREE_TYPE (op0))))))
4535 break;
4537 /* Pass the constant down and see if we can make a simplification. If
4538 we can, replace this expression with the inner simplification for
4539 possible later conversion to our or some other type. */
4540 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
4541 && TREE_CODE (t2) == INTEGER_CST
4542 && ! TREE_CONSTANT_OVERFLOW (t2)
4543 && (0 != (t1 = extract_muldiv (op0, t2, code,
4544 code == MULT_EXPR
4545 ? ctype : NULL_TREE))))
4546 return t1;
4547 break;
4549 case NEGATE_EXPR: case ABS_EXPR:
4550 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4551 return fold (build1 (tcode, ctype, fold_convert (ctype, t1)));
4552 break;
4554 case MIN_EXPR: case MAX_EXPR:
4555 /* If widening the type changes the signedness, then we can't perform
4556 this optimization as that changes the result. */
4557 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
4558 break;
4560 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
4561 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
4562 && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
4564 if (tree_int_cst_sgn (c) < 0)
4565 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
4567 return fold (build (tcode, ctype, fold_convert (ctype, t1),
4568 fold_convert (ctype, t2)));
4570 break;
4572 case LSHIFT_EXPR: case RSHIFT_EXPR:
4573 /* If the second operand is constant, this is a multiplication
4574 or floor division, by a power of two, so we can treat it that
4575 way unless the multiplier or divisor overflows. */
4576 if (TREE_CODE (op1) == INTEGER_CST
4577 /* const_binop may not detect overflow correctly,
4578 so check for it explicitly here. */
4579 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
4580 && TREE_INT_CST_HIGH (op1) == 0
4581 && 0 != (t1 = fold_convert (ctype,
4582 const_binop (LSHIFT_EXPR,
4583 size_one_node,
4584 op1, 0)))
4585 && ! TREE_OVERFLOW (t1))
4586 return extract_muldiv (build (tcode == LSHIFT_EXPR
4587 ? MULT_EXPR : FLOOR_DIV_EXPR,
4588 ctype, fold_convert (ctype, op0), t1),
4589 c, code, wide_type);
4590 break;
4592 case PLUS_EXPR: case MINUS_EXPR:
4593 /* See if we can eliminate the operation on both sides. If we can, we
4594 can return a new PLUS or MINUS. If we can't, the only remaining
4595 cases where we can do anything are if the second operand is a
4596 constant. */
4597 t1 = extract_muldiv (op0, c, code, wide_type);
4598 t2 = extract_muldiv (op1, c, code, wide_type);
4599 if (t1 != 0 && t2 != 0
4600 && (code == MULT_EXPR
4601 /* If not multiplication, we can only do this if both operands
4602 are divisible by c. */
4603 || (multiple_of_p (ctype, op0, c)
4604 && multiple_of_p (ctype, op1, c))))
4605 return fold (build (tcode, ctype, fold_convert (ctype, t1),
4606 fold_convert (ctype, t2)));
4608 /* If this was a subtraction, negate OP1 and set it to be an addition.
4609 This simplifies the logic below. */
4610 if (tcode == MINUS_EXPR)
4611 tcode = PLUS_EXPR, op1 = negate_expr (op1);
4613 if (TREE_CODE (op1) != INTEGER_CST)
4614 break;
4616 /* If either OP1 or C are negative, this optimization is not safe for
4617 some of the division and remainder types while for others we need
4618 to change the code. */
4619 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
4621 if (code == CEIL_DIV_EXPR)
4622 code = FLOOR_DIV_EXPR;
4623 else if (code == FLOOR_DIV_EXPR)
4624 code = CEIL_DIV_EXPR;
4625 else if (code != MULT_EXPR
4626 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
4627 break;
4630 /* If it's a multiply or a division/modulus operation of a multiple
4631 of our constant, do the operation and verify it doesn't overflow. */
4632 if (code == MULT_EXPR
4633 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4635 op1 = const_binop (code, fold_convert (ctype, op1),
4636 fold_convert (ctype, c), 0);
4637 /* We allow the constant to overflow with wrapping semantics. */
4638 if (op1 == 0
4639 || (TREE_OVERFLOW (op1) && ! flag_wrapv))
4640 break;
4642 else
4643 break;
4645 /* If we have an unsigned type is not a sizetype, we cannot widen
4646 the operation since it will change the result if the original
4647 computation overflowed. */
4648 if (TYPE_UNSIGNED (ctype)
4649 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
4650 && ctype != type)
4651 break;
4653 /* If we were able to eliminate our operation from the first side,
4654 apply our operation to the second side and reform the PLUS. */
4655 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
4656 return fold (build (tcode, ctype, fold_convert (ctype, t1), op1));
4658 /* The last case is if we are a multiply. In that case, we can
4659 apply the distributive law to commute the multiply and addition
4660 if the multiplication of the constants doesn't overflow. */
4661 if (code == MULT_EXPR)
4662 return fold (build (tcode, ctype,
4663 fold (build (code, ctype,
4664 fold_convert (ctype, op0),
4665 fold_convert (ctype, c))),
4666 op1));
4668 break;
4670 case MULT_EXPR:
4671 /* We have a special case here if we are doing something like
4672 (C * 8) % 4 since we know that's zero. */
4673 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
4674 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
4675 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
4676 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4677 return omit_one_operand (type, integer_zero_node, op0);
4679 /* ... fall through ... */
4681 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
4682 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
4683 /* If we can extract our operation from the LHS, do so and return a
4684 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
4685 do something only if the second operand is a constant. */
4686 if (same_p
4687 && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4688 return fold (build (tcode, ctype, fold_convert (ctype, t1),
4689 fold_convert (ctype, op1)));
4690 else if (tcode == MULT_EXPR && code == MULT_EXPR
4691 && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
4692 return fold (build (tcode, ctype, fold_convert (ctype, op0),
4693 fold_convert (ctype, t1)));
4694 else if (TREE_CODE (op1) != INTEGER_CST)
4695 return 0;
4697 /* If these are the same operation types, we can associate them
4698 assuming no overflow. */
4699 if (tcode == code
4700 && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
4701 fold_convert (ctype, c), 0))
4702 && ! TREE_OVERFLOW (t1))
4703 return fold (build (tcode, ctype, fold_convert (ctype, op0), t1));
4705 /* If these operations "cancel" each other, we have the main
4706 optimizations of this pass, which occur when either constant is a
4707 multiple of the other, in which case we replace this with either an
4708 operation or CODE or TCODE.
4710 If we have an unsigned type that is not a sizetype, we cannot do
4711 this since it will change the result if the original computation
4712 overflowed. */
4713 if ((! TYPE_UNSIGNED (ctype)
4714 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
4715 && ! flag_wrapv
4716 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
4717 || (tcode == MULT_EXPR
4718 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
4719 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
4721 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4722 return fold (build (tcode, ctype, fold_convert (ctype, op0),
4723 fold_convert (ctype,
4724 const_binop (TRUNC_DIV_EXPR,
4725 op1, c, 0))));
4726 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
4727 return fold (build (code, ctype, fold_convert (ctype, op0),
4728 fold_convert (ctype,
4729 const_binop (TRUNC_DIV_EXPR,
4730 c, op1, 0))));
4732 break;
4734 default:
4735 break;
4738 return 0;
4741 /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
4742 S, a SAVE_EXPR, return the expression actually being evaluated. Note
4743 that we may sometimes modify the tree. */
4745 static tree
4746 strip_compound_expr (tree t, tree s)
4748 enum tree_code code = TREE_CODE (t);
4750 /* See if this is the COMPOUND_EXPR we want to eliminate. */
4751 if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
4752 && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
4753 return TREE_OPERAND (t, 1);
4755 /* See if this is a COND_EXPR or a simple arithmetic operator. We
4756 don't bother handling any other types. */
4757 else if (code == COND_EXPR)
4759 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4760 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4761 TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
4763 else if (TREE_CODE_CLASS (code) == '1')
4764 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4765 else if (TREE_CODE_CLASS (code) == '<'
4766 || TREE_CODE_CLASS (code) == '2')
4768 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4769 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4772 return t;
4775 /* Return a node which has the indicated constant VALUE (either 0 or
4776 1), and is of the indicated TYPE. */
4778 static tree
4779 constant_boolean_node (int value, tree type)
4781 if (type == integer_type_node)
4782 return value ? integer_one_node : integer_zero_node;
4783 else if (TREE_CODE (type) == BOOLEAN_TYPE)
4784 return lang_hooks.truthvalue_conversion (value ? integer_one_node
4785 : integer_zero_node);
4786 else
4788 tree t = build_int_2 (value, 0);
4790 TREE_TYPE (t) = type;
4791 return t;
4795 /* Utility function for the following routine, to see how complex a nesting of
4796 COND_EXPRs can be. EXPR is the expression and LIMIT is a count beyond which
4797 we don't care (to avoid spending too much time on complex expressions.). */
4799 static int
4800 count_cond (tree expr, int lim)
4802 int ctrue, cfalse;
4804 if (TREE_CODE (expr) != COND_EXPR)
4805 return 0;
4806 else if (lim <= 0)
4807 return 0;
4809 ctrue = count_cond (TREE_OPERAND (expr, 1), lim - 1);
4810 cfalse = count_cond (TREE_OPERAND (expr, 2), lim - 1 - ctrue);
4811 return MIN (lim, 1 + ctrue + cfalse);
4814 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
4815 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
4816 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
4817 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
4818 COND is the first argument to CODE; otherwise (as in the example
4819 given here), it is the second argument. TYPE is the type of the
4820 original expression. Return NULL_TREE if no simplification is
4821 possible. */
4823 static tree
4824 fold_binary_op_with_conditional_arg (enum tree_code code, tree type,
4825 tree cond, tree arg, int cond_first_p)
4827 tree test, true_value, false_value;
4828 tree lhs = NULL_TREE;
4829 tree rhs = NULL_TREE;
4830 /* In the end, we'll produce a COND_EXPR. Both arms of the
4831 conditional expression will be binary operations. The left-hand
4832 side of the expression to be executed if the condition is true
4833 will be pointed to by TRUE_LHS. Similarly, the right-hand side
4834 of the expression to be executed if the condition is true will be
4835 pointed to by TRUE_RHS. FALSE_LHS and FALSE_RHS are analogous --
4836 but apply to the expression to be executed if the conditional is
4837 false. */
4838 tree *true_lhs;
4839 tree *true_rhs;
4840 tree *false_lhs;
4841 tree *false_rhs;
4842 /* These are the codes to use for the left-hand side and right-hand
4843 side of the COND_EXPR. Normally, they are the same as CODE. */
4844 enum tree_code lhs_code = code;
4845 enum tree_code rhs_code = code;
4846 /* And these are the types of the expressions. */
4847 tree lhs_type = type;
4848 tree rhs_type = type;
4849 int save = 0;
4851 if (TREE_CODE (cond) != COND_EXPR
4852 && TREE_CODE_CLASS (code) == '<')
4853 return NULL_TREE;
4855 if (TREE_CODE (arg) == COND_EXPR
4856 && count_cond (cond, 25) + count_cond (arg, 25) > 25)
4857 return NULL_TREE;
4859 if (TREE_SIDE_EFFECTS (arg)
4860 && (lang_hooks.decls.global_bindings_p () != 0
4861 || CONTAINS_PLACEHOLDER_P (arg)))
4862 return NULL_TREE;
4864 if (cond_first_p)
4866 true_rhs = false_rhs = &arg;
4867 true_lhs = &true_value;
4868 false_lhs = &false_value;
4870 else
4872 true_lhs = false_lhs = &arg;
4873 true_rhs = &true_value;
4874 false_rhs = &false_value;
4877 if (TREE_CODE (cond) == COND_EXPR)
4879 test = TREE_OPERAND (cond, 0);
4880 true_value = TREE_OPERAND (cond, 1);
4881 false_value = TREE_OPERAND (cond, 2);
4882 /* If this operand throws an expression, then it does not make
4883 sense to try to perform a logical or arithmetic operation
4884 involving it. Instead of building `a + throw 3' for example,
4885 we simply build `a, throw 3'. */
4886 if (VOID_TYPE_P (TREE_TYPE (true_value)))
4888 if (! cond_first_p)
4890 lhs_code = COMPOUND_EXPR;
4891 lhs_type = void_type_node;
4893 else
4894 lhs = true_value;
4896 if (VOID_TYPE_P (TREE_TYPE (false_value)))
4898 if (! cond_first_p)
4900 rhs_code = COMPOUND_EXPR;
4901 rhs_type = void_type_node;
4903 else
4904 rhs = false_value;
4907 else
4909 tree testtype = TREE_TYPE (cond);
4910 test = cond;
4911 true_value = fold_convert (testtype, integer_one_node);
4912 false_value = fold_convert (testtype, integer_zero_node);
4915 /* If ARG is complex we want to make sure we only evaluate it once. Though
4916 this is only required if it is volatile, it might be more efficient even
4917 if it is not. However, if we succeed in folding one part to a constant,
4918 we do not need to make this SAVE_EXPR. Since we do this optimization
4919 primarily to see if we do end up with constant and this SAVE_EXPR
4920 interferes with later optimizations, suppressing it when we can is
4921 important.
4923 If we are not in a function, we can't make a SAVE_EXPR, so don't try to
4924 do so. Don't try to see if the result is a constant if an arm is a
4925 COND_EXPR since we get exponential behavior in that case. */
4927 if (saved_expr_p (arg))
4928 save = 1;
4929 else if (lhs == 0 && rhs == 0
4930 && !TREE_CONSTANT (arg)
4931 && lang_hooks.decls.global_bindings_p () == 0
4932 && ((TREE_CODE (arg) != VAR_DECL && TREE_CODE (arg) != PARM_DECL)
4933 || TREE_SIDE_EFFECTS (arg)))
4935 if (TREE_CODE (true_value) != COND_EXPR)
4936 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4938 if (TREE_CODE (false_value) != COND_EXPR)
4939 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4941 if ((lhs == 0 || ! TREE_CONSTANT (lhs))
4942 && (rhs == 0 || !TREE_CONSTANT (rhs)))
4944 arg = save_expr (arg);
4945 lhs = rhs = 0;
4946 save = saved_expr_p (arg);
4950 if (lhs == 0)
4951 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4952 if (rhs == 0)
4953 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4955 test = fold (build (COND_EXPR, type, test, lhs, rhs));
4957 /* If ARG involves a SAVE_EXPR, we need to ensure it is evaluated
4958 ahead of the COND_EXPR we made. Otherwise we would have it only
4959 evaluated in one branch, with the other branch using the result
4960 but missing the evaluation code. Beware that the save_expr call
4961 above might not return a SAVE_EXPR, so testing the TREE_CODE
4962 of ARG is not enough to decide here.  */
4963 if (save)
4964 return build (COMPOUND_EXPR, type,
4965 fold_convert (void_type_node, arg),
4966 strip_compound_expr (test, arg));
4967 else
4968 return fold_convert (type, test);
4972 /* Subroutine of fold() that checks for the addition of +/- 0.0.
4974 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
4975 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
4976 ADDEND is the same as X.
4978 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
4979 and finite. The problematic cases are when X is zero, and its mode
4980 has signed zeros. In the case of rounding towards -infinity,
4981 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
4982 modes, X + 0 is not the same as X because -0 + 0 is 0. */
4984 static bool
4985 fold_real_zero_addition_p (tree type, tree addend, int negate)
4987 if (!real_zerop (addend))
4988 return false;
4990 /* Don't allow the fold with -fsignaling-nans. */
4991 if (HONOR_SNANS (TYPE_MODE (type)))
4992 return false;
4994 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
4995 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
4996 return true;
4998 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
4999 if (TREE_CODE (addend) == REAL_CST
5000 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
5001 negate = !negate;
5003 /* The mode has signed zeros, and we have to honor their sign.
5004 In this situation, there is only one case we can return true for.
5005 X - 0 is the same as X unless rounding towards -infinity is
5006 supported. */
5007 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
5010 /* Subroutine of fold() that checks comparisons of built-in math
5011 functions against real constants.
5013 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
5014 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
5015 is the type of the result and ARG0 and ARG1 are the operands of the
5016 comparison. ARG1 must be a TREE_REAL_CST.
5018 The function returns the constant folded tree if a simplification
5019 can be made, and NULL_TREE otherwise. */
5021 static tree
5022 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
5023 tree type, tree arg0, tree arg1)
5025 REAL_VALUE_TYPE c;
5027 if (BUILTIN_SQRT_P (fcode))
5029 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
5030 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
5032 c = TREE_REAL_CST (arg1);
5033 if (REAL_VALUE_NEGATIVE (c))
5035 /* sqrt(x) < y is always false, if y is negative. */
5036 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
5037 return omit_one_operand (type,
5038 fold_convert (type, integer_zero_node),
5039 arg);
5041 /* sqrt(x) > y is always true, if y is negative and we
5042 don't care about NaNs, i.e. negative values of x. */
5043 if (code == NE_EXPR || !HONOR_NANS (mode))
5044 return omit_one_operand (type,
5045 fold_convert (type, integer_one_node),
5046 arg);
5048 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
5049 return fold (build (GE_EXPR, type, arg,
5050 build_real (TREE_TYPE (arg), dconst0)));
5052 else if (code == GT_EXPR || code == GE_EXPR)
5054 REAL_VALUE_TYPE c2;
5056 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5057 real_convert (&c2, mode, &c2);
5059 if (REAL_VALUE_ISINF (c2))
5061 /* sqrt(x) > y is x == +Inf, when y is very large. */
5062 if (HONOR_INFINITIES (mode))
5063 return fold (build (EQ_EXPR, type, arg,
5064 build_real (TREE_TYPE (arg), c2)));
5066 /* sqrt(x) > y is always false, when y is very large
5067 and we don't care about infinities. */
5068 return omit_one_operand (type,
5069 fold_convert (type, integer_zero_node),
5070 arg);
5073 /* sqrt(x) > c is the same as x > c*c. */
5074 return fold (build (code, type, arg,
5075 build_real (TREE_TYPE (arg), c2)));
5077 else if (code == LT_EXPR || code == LE_EXPR)
5079 REAL_VALUE_TYPE c2;
5081 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5082 real_convert (&c2, mode, &c2);
5084 if (REAL_VALUE_ISINF (c2))
5086 /* sqrt(x) < y is always true, when y is a very large
5087 value and we don't care about NaNs or Infinities. */
5088 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
5089 return omit_one_operand (type,
5090 fold_convert (type, integer_one_node),
5091 arg);
5093 /* sqrt(x) < y is x != +Inf when y is very large and we
5094 don't care about NaNs. */
5095 if (! HONOR_NANS (mode))
5096 return fold (build (NE_EXPR, type, arg,
5097 build_real (TREE_TYPE (arg), c2)));
5099 /* sqrt(x) < y is x >= 0 when y is very large and we
5100 don't care about Infinities. */
5101 if (! HONOR_INFINITIES (mode))
5102 return fold (build (GE_EXPR, type, arg,
5103 build_real (TREE_TYPE (arg), dconst0)));
5105 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
5106 if (lang_hooks.decls.global_bindings_p () != 0
5107 || CONTAINS_PLACEHOLDER_P (arg))
5108 return NULL_TREE;
5110 arg = save_expr (arg);
5111 return fold (build (TRUTH_ANDIF_EXPR, type,
5112 fold (build (GE_EXPR, type, arg,
5113 build_real (TREE_TYPE (arg),
5114 dconst0))),
5115 fold (build (NE_EXPR, type, arg,
5116 build_real (TREE_TYPE (arg),
5117 c2)))));
5120 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
5121 if (! HONOR_NANS (mode))
5122 return fold (build (code, type, arg,
5123 build_real (TREE_TYPE (arg), c2)));
5125 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
5126 if (lang_hooks.decls.global_bindings_p () == 0
5127 && ! CONTAINS_PLACEHOLDER_P (arg))
5129 arg = save_expr (arg);
5130 return fold (build (TRUTH_ANDIF_EXPR, type,
5131 fold (build (GE_EXPR, type, arg,
5132 build_real (TREE_TYPE (arg),
5133 dconst0))),
5134 fold (build (code, type, arg,
5135 build_real (TREE_TYPE (arg),
5136 c2)))));
5141 return NULL_TREE;
5144 /* Subroutine of fold() that optimizes comparisons against Infinities,
5145 either +Inf or -Inf.
5147 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5148 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
5149 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
5151 The function returns the constant folded tree if a simplification
5152 can be made, and NULL_TREE otherwise. */
5154 static tree
5155 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5157 enum machine_mode mode;
5158 REAL_VALUE_TYPE max;
5159 tree temp;
5160 bool neg;
5162 mode = TYPE_MODE (TREE_TYPE (arg0));
5164 /* For negative infinity swap the sense of the comparison. */
5165 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
5166 if (neg)
5167 code = swap_tree_comparison (code);
5169 switch (code)
5171 case GT_EXPR:
5172 /* x > +Inf is always false, if with ignore sNANs. */
5173 if (HONOR_SNANS (mode))
5174 return NULL_TREE;
5175 return omit_one_operand (type,
5176 fold_convert (type, integer_zero_node),
5177 arg0);
5179 case LE_EXPR:
5180 /* x <= +Inf is always true, if we don't case about NaNs. */
5181 if (! HONOR_NANS (mode))
5182 return omit_one_operand (type,
5183 fold_convert (type, integer_one_node),
5184 arg0);
5186 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
5187 if (lang_hooks.decls.global_bindings_p () == 0
5188 && ! CONTAINS_PLACEHOLDER_P (arg0))
5190 arg0 = save_expr (arg0);
5191 return fold (build (EQ_EXPR, type, arg0, arg0));
5193 break;
5195 case EQ_EXPR:
5196 case GE_EXPR:
5197 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
5198 real_maxval (&max, neg, mode);
5199 return fold (build (neg ? LT_EXPR : GT_EXPR, type,
5200 arg0, build_real (TREE_TYPE (arg0), max)));
5202 case LT_EXPR:
5203 /* x < +Inf is always equal to x <= DBL_MAX. */
5204 real_maxval (&max, neg, mode);
5205 return fold (build (neg ? GE_EXPR : LE_EXPR, type,
5206 arg0, build_real (TREE_TYPE (arg0), max)));
5208 case NE_EXPR:
5209 /* x != +Inf is always equal to !(x > DBL_MAX). */
5210 real_maxval (&max, neg, mode);
5211 if (! HONOR_NANS (mode))
5212 return fold (build (neg ? GE_EXPR : LE_EXPR, type,
5213 arg0, build_real (TREE_TYPE (arg0), max)));
5214 temp = fold (build (neg ? LT_EXPR : GT_EXPR, type,
5215 arg0, build_real (TREE_TYPE (arg0), max)));
5216 return fold (build1 (TRUTH_NOT_EXPR, type, temp));
5218 default:
5219 break;
5222 return NULL_TREE;
5225 /* Subroutine of fold() that optimizes comparisons of a division by
5226 a non-zero integer constant against an integer constant, i.e.
5227 X/C1 op C2.
5229 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5230 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
5231 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
5233 The function returns the constant folded tree if a simplification
5234 can be made, and NULL_TREE otherwise. */
5236 static tree
5237 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5239 tree prod, tmp, hi, lo;
5240 tree arg00 = TREE_OPERAND (arg0, 0);
5241 tree arg01 = TREE_OPERAND (arg0, 1);
5242 unsigned HOST_WIDE_INT lpart;
5243 HOST_WIDE_INT hpart;
5244 int overflow;
5246 /* We have to do this the hard way to detect unsigned overflow.
5247 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
5248 overflow = mul_double (TREE_INT_CST_LOW (arg01),
5249 TREE_INT_CST_HIGH (arg01),
5250 TREE_INT_CST_LOW (arg1),
5251 TREE_INT_CST_HIGH (arg1), &lpart, &hpart);
5252 prod = build_int_2 (lpart, hpart);
5253 TREE_TYPE (prod) = TREE_TYPE (arg00);
5254 TREE_OVERFLOW (prod) = force_fit_type (prod, overflow)
5255 || TREE_INT_CST_HIGH (prod) != hpart
5256 || TREE_INT_CST_LOW (prod) != lpart;
5257 TREE_CONSTANT_OVERFLOW (prod) = TREE_OVERFLOW (prod);
5259 if (TYPE_UNSIGNED (TREE_TYPE (arg0)))
5261 tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5262 lo = prod;
5264 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
5265 overflow = add_double (TREE_INT_CST_LOW (prod),
5266 TREE_INT_CST_HIGH (prod),
5267 TREE_INT_CST_LOW (tmp),
5268 TREE_INT_CST_HIGH (tmp),
5269 &lpart, &hpart);
5270 hi = build_int_2 (lpart, hpart);
5271 TREE_TYPE (hi) = TREE_TYPE (arg00);
5272 TREE_OVERFLOW (hi) = force_fit_type (hi, overflow)
5273 || TREE_INT_CST_HIGH (hi) != hpart
5274 || TREE_INT_CST_LOW (hi) != lpart
5275 || TREE_OVERFLOW (prod);
5276 TREE_CONSTANT_OVERFLOW (hi) = TREE_OVERFLOW (hi);
5278 else if (tree_int_cst_sgn (arg01) >= 0)
5280 tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5281 switch (tree_int_cst_sgn (arg1))
5283 case -1:
5284 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5285 hi = prod;
5286 break;
5288 case 0:
5289 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
5290 hi = tmp;
5291 break;
5293 case 1:
5294 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5295 lo = prod;
5296 break;
5298 default:
5299 abort ();
5302 else
5304 tmp = int_const_binop (PLUS_EXPR, arg01, integer_one_node, 0);
5305 switch (tree_int_cst_sgn (arg1))
5307 case -1:
5308 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5309 lo = prod;
5310 break;
5312 case 0:
5313 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
5314 lo = tmp;
5315 break;
5317 case 1:
5318 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5319 hi = prod;
5320 break;
5322 default:
5323 abort ();
5327 switch (code)
5329 case EQ_EXPR:
5330 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5331 return omit_one_operand (type, integer_zero_node, arg00);
5332 if (TREE_OVERFLOW (hi))
5333 return fold (build2 (GE_EXPR, type, arg00, lo));
5334 if (TREE_OVERFLOW (lo))
5335 return fold (build2 (LE_EXPR, type, arg00, hi));
5336 return build_range_check (type, arg00, 1, lo, hi);
5338 case NE_EXPR:
5339 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5340 return omit_one_operand (type, integer_one_node, arg00);
5341 if (TREE_OVERFLOW (hi))
5342 return fold (build2 (LT_EXPR, type, arg00, lo));
5343 if (TREE_OVERFLOW (lo))
5344 return fold (build2 (GT_EXPR, type, arg00, hi));
5345 return build_range_check (type, arg00, 0, lo, hi);
5347 case LT_EXPR:
5348 if (TREE_OVERFLOW (lo))
5349 return omit_one_operand (type, integer_zero_node, arg00);
5350 return fold (build2 (LT_EXPR, type, arg00, lo));
5352 case LE_EXPR:
5353 if (TREE_OVERFLOW (hi))
5354 return omit_one_operand (type, integer_one_node, arg00);
5355 return fold (build2 (LE_EXPR, type, arg00, hi));
5357 case GT_EXPR:
5358 if (TREE_OVERFLOW (hi))
5359 return omit_one_operand (type, integer_zero_node, arg00);
5360 return fold (build2 (GT_EXPR, type, arg00, hi));
5362 case GE_EXPR:
5363 if (TREE_OVERFLOW (lo))
5364 return omit_one_operand (type, integer_one_node, arg00);
5365 return fold (build2 (GE_EXPR, type, arg00, lo));
5367 default:
5368 break;
5371 return NULL_TREE;
5375 /* If CODE with arguments ARG0 and ARG1 represents a single bit
5376 equality/inequality test, then return a simplified form of
5377 the test using shifts and logical operations. Otherwise return
5378 NULL. TYPE is the desired result type. */
5380 tree
5381 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
5382 tree result_type)
5384 /* If this is a TRUTH_NOT_EXPR, it may have a single bit test inside
5385 operand 0. */
5386 if (code == TRUTH_NOT_EXPR)
5388 code = TREE_CODE (arg0);
5389 if (code != NE_EXPR && code != EQ_EXPR)
5390 return NULL_TREE;
5392 /* Extract the arguments of the EQ/NE. */
5393 arg1 = TREE_OPERAND (arg0, 1);
5394 arg0 = TREE_OPERAND (arg0, 0);
5396 /* This requires us to invert the code. */
5397 code = (code == EQ_EXPR ? NE_EXPR : EQ_EXPR);
5400 /* If this is testing a single bit, we can optimize the test. */
5401 if ((code == NE_EXPR || code == EQ_EXPR)
5402 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
5403 && integer_pow2p (TREE_OPERAND (arg0, 1)))
5405 tree inner = TREE_OPERAND (arg0, 0);
5406 tree type = TREE_TYPE (arg0);
5407 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
5408 enum machine_mode operand_mode = TYPE_MODE (type);
5409 int ops_unsigned;
5410 tree signed_type, unsigned_type, intermediate_type;
5411 tree arg00;
5413 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5414 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5415 arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
5416 if (arg00 != NULL_TREE)
5418 tree stype = lang_hooks.types.signed_type (TREE_TYPE (arg00));
5419 return fold (build (code == EQ_EXPR ? GE_EXPR : LT_EXPR, result_type,
5420 fold_convert (stype, arg00),
5421 fold_convert (stype, integer_zero_node)));
5424 /* At this point, we know that arg0 is not testing the sign bit. */
5425 if (TYPE_PRECISION (type) - 1 == bitnum)
5426 abort ();
5428 /* Otherwise we have (A & C) != 0 where C is a single bit,
5429 convert that into ((A >> C2) & 1). Where C2 = log2(C).
5430 Similarly for (A & C) == 0. */
5432 /* If INNER is a right shift of a constant and it plus BITNUM does
5433 not overflow, adjust BITNUM and INNER. */
5434 if (TREE_CODE (inner) == RSHIFT_EXPR
5435 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
5436 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
5437 && bitnum < TYPE_PRECISION (type)
5438 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
5439 bitnum - TYPE_PRECISION (type)))
5441 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
5442 inner = TREE_OPERAND (inner, 0);
5445 /* If we are going to be able to omit the AND below, we must do our
5446 operations as unsigned. If we must use the AND, we have a choice.
5447 Normally unsigned is faster, but for some machines signed is. */
5448 #ifdef LOAD_EXTEND_OP
5449 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND ? 0 : 1);
5450 #else
5451 ops_unsigned = 1;
5452 #endif
5454 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
5455 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
5456 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
5457 inner = fold_convert (intermediate_type, inner);
5459 if (bitnum != 0)
5460 inner = build (RSHIFT_EXPR, intermediate_type,
5461 inner, size_int (bitnum));
5463 if (code == EQ_EXPR)
5464 inner = build (BIT_XOR_EXPR, intermediate_type,
5465 inner, integer_one_node);
5467 /* Put the AND last so it can combine with more things. */
5468 inner = build (BIT_AND_EXPR, intermediate_type,
5469 inner, integer_one_node);
5471 /* Make sure to return the proper type. */
5472 inner = fold_convert (result_type, inner);
5474 return inner;
5476 return NULL_TREE;
5479 /* Check whether we are allowed to reorder operands arg0 and arg1,
5480 such that the evaluation of arg1 occurs before arg0. */
5482 static bool
5483 reorder_operands_p (tree arg0, tree arg1)
5485 if (! flag_evaluation_order)
5486 return true;
5487 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
5488 return true;
5489 return ! TREE_SIDE_EFFECTS (arg0)
5490 && ! TREE_SIDE_EFFECTS (arg1);
5493 /* Test whether it is preferable two swap two operands, ARG0 and
5494 ARG1, for example because ARG0 is an integer constant and ARG1
5495 isn't. If REORDER is true, only recommend swapping if we can
5496 evaluate the operands in reverse order. */
5498 static bool
5499 tree_swap_operands_p (tree arg0, tree arg1, bool reorder)
5501 STRIP_SIGN_NOPS (arg0);
5502 STRIP_SIGN_NOPS (arg1);
5504 if (TREE_CODE (arg1) == INTEGER_CST)
5505 return 0;
5506 if (TREE_CODE (arg0) == INTEGER_CST)
5507 return 1;
5509 if (TREE_CODE (arg1) == REAL_CST)
5510 return 0;
5511 if (TREE_CODE (arg0) == REAL_CST)
5512 return 1;
5514 if (TREE_CODE (arg1) == COMPLEX_CST)
5515 return 0;
5516 if (TREE_CODE (arg0) == COMPLEX_CST)
5517 return 1;
5519 if (TREE_CONSTANT (arg1))
5520 return 0;
5521 if (TREE_CONSTANT (arg0))
5522 return 1;
5524 if (optimize_size)
5525 return 0;
5527 if (reorder && flag_evaluation_order
5528 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
5529 return 0;
5531 if (DECL_P (arg1))
5532 return 0;
5533 if (DECL_P (arg0))
5534 return 1;
5536 if (reorder && flag_evaluation_order
5537 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
5538 return 0;
5540 if (DECL_P (arg1))
5541 return 0;
5542 if (DECL_P (arg0))
5543 return 1;
5545 return 0;
5548 /* Perform constant folding and related simplification of EXPR.
5549 The related simplifications include x*1 => x, x*0 => 0, etc.,
5550 and application of the associative law.
5551 NOP_EXPR conversions may be removed freely (as long as we
5552 are careful not to change the type of the overall expression).
5553 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
5554 but we can constant-fold them if they have constant operands. */
5556 #ifdef ENABLE_FOLD_CHECKING
5557 # define fold(x) fold_1 (x)
5558 static tree fold_1 (tree);
5559 static
5560 #endif
5561 tree
5562 fold (tree expr)
5564 const tree t = expr;
5565 const tree type = TREE_TYPE (expr);
5566 tree t1 = NULL_TREE;
5567 tree tem;
5568 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
5569 enum tree_code code = TREE_CODE (t);
5570 int kind = TREE_CODE_CLASS (code);
5572 /* WINS will be nonzero when the switch is done
5573 if all operands are constant. */
5574 int wins = 1;
5576 /* Don't try to process an RTL_EXPR since its operands aren't trees.
5577 Likewise for a SAVE_EXPR that's already been evaluated. */
5578 if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t) != 0))
5579 return t;
5581 /* Return right away if a constant. */
5582 if (kind == 'c')
5583 return t;
5585 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
5587 tree subop;
5589 /* Special case for conversion ops that can have fixed point args. */
5590 arg0 = TREE_OPERAND (t, 0);
5592 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
5593 if (arg0 != 0)
5594 STRIP_SIGN_NOPS (arg0);
5596 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
5597 subop = TREE_REALPART (arg0);
5598 else
5599 subop = arg0;
5601 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
5602 && TREE_CODE (subop) != REAL_CST)
5603 /* Note that TREE_CONSTANT isn't enough:
5604 static var addresses are constant but we can't
5605 do arithmetic on them. */
5606 wins = 0;
5608 else if (IS_EXPR_CODE_CLASS (kind))
5610 int len = first_rtl_op (code);
5611 int i;
5612 for (i = 0; i < len; i++)
5614 tree op = TREE_OPERAND (t, i);
5615 tree subop;
5617 if (op == 0)
5618 continue; /* Valid for CALL_EXPR, at least. */
5620 /* Strip any conversions that don't change the mode. This is
5621 safe for every expression, except for a comparison expression
5622 because its signedness is derived from its operands. So, in
5623 the latter case, only strip conversions that don't change the
5624 signedness.
5626 Note that this is done as an internal manipulation within the
5627 constant folder, in order to find the simplest representation
5628 of the arguments so that their form can be studied. In any
5629 cases, the appropriate type conversions should be put back in
5630 the tree that will get out of the constant folder. */
5631 if (kind == '<')
5632 STRIP_SIGN_NOPS (op);
5633 else
5634 STRIP_NOPS (op);
5636 if (TREE_CODE (op) == COMPLEX_CST)
5637 subop = TREE_REALPART (op);
5638 else
5639 subop = op;
5641 if (TREE_CODE (subop) != INTEGER_CST
5642 && TREE_CODE (subop) != REAL_CST)
5643 /* Note that TREE_CONSTANT isn't enough:
5644 static var addresses are constant but we can't
5645 do arithmetic on them. */
5646 wins = 0;
5648 if (i == 0)
5649 arg0 = op;
5650 else if (i == 1)
5651 arg1 = op;
5655 /* If this is a commutative operation, and ARG0 is a constant, move it
5656 to ARG1 to reduce the number of tests below. */
5657 if (commutative_tree_code (code)
5658 && tree_swap_operands_p (arg0, arg1, true))
5659 return fold (build (code, type, TREE_OPERAND (t, 1),
5660 TREE_OPERAND (t, 0)));
5662 /* Now WINS is set as described above,
5663 ARG0 is the first operand of EXPR,
5664 and ARG1 is the second operand (if it has more than one operand).
5666 First check for cases where an arithmetic operation is applied to a
5667 compound, conditional, or comparison operation. Push the arithmetic
5668 operation inside the compound or conditional to see if any folding
5669 can then be done. Convert comparison to conditional for this purpose.
5670 The also optimizes non-constant cases that used to be done in
5671 expand_expr.
5673 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
5674 one of the operands is a comparison and the other is a comparison, a
5675 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
5676 code below would make the expression more complex. Change it to a
5677 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
5678 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
5680 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
5681 || code == EQ_EXPR || code == NE_EXPR)
5682 && ((truth_value_p (TREE_CODE (arg0))
5683 && (truth_value_p (TREE_CODE (arg1))
5684 || (TREE_CODE (arg1) == BIT_AND_EXPR
5685 && integer_onep (TREE_OPERAND (arg1, 1)))))
5686 || (truth_value_p (TREE_CODE (arg1))
5687 && (truth_value_p (TREE_CODE (arg0))
5688 || (TREE_CODE (arg0) == BIT_AND_EXPR
5689 && integer_onep (TREE_OPERAND (arg0, 1)))))))
5691 tem = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
5692 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
5693 : TRUTH_XOR_EXPR,
5694 type, fold_convert (boolean_type_node, arg0),
5695 fold_convert (boolean_type_node, arg1)));
5697 if (code == EQ_EXPR)
5698 tem = invert_truthvalue (tem);
5700 return tem;
5703 if (TREE_CODE_CLASS (code) == '1')
5705 if (TREE_CODE (arg0) == COMPOUND_EXPR)
5706 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5707 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
5708 else if (TREE_CODE (arg0) == COND_EXPR)
5710 tree arg01 = TREE_OPERAND (arg0, 1);
5711 tree arg02 = TREE_OPERAND (arg0, 2);
5712 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
5713 arg01 = fold (build1 (code, type, arg01));
5714 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
5715 arg02 = fold (build1 (code, type, arg02));
5716 tem = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
5717 arg01, arg02));
5719 /* If this was a conversion, and all we did was to move into
5720 inside the COND_EXPR, bring it back out. But leave it if
5721 it is a conversion from integer to integer and the
5722 result precision is no wider than a word since such a
5723 conversion is cheap and may be optimized away by combine,
5724 while it couldn't if it were outside the COND_EXPR. Then return
5725 so we don't get into an infinite recursion loop taking the
5726 conversion out and then back in. */
5728 if ((code == NOP_EXPR || code == CONVERT_EXPR
5729 || code == NON_LVALUE_EXPR)
5730 && TREE_CODE (tem) == COND_EXPR
5731 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
5732 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
5733 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
5734 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
5735 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
5736 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
5737 && ! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
5738 && (INTEGRAL_TYPE_P
5739 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
5740 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD))
5741 tem = build1 (code, type,
5742 build (COND_EXPR,
5743 TREE_TYPE (TREE_OPERAND
5744 (TREE_OPERAND (tem, 1), 0)),
5745 TREE_OPERAND (tem, 0),
5746 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
5747 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
5748 return tem;
5750 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
5752 if (TREE_CODE (type) == BOOLEAN_TYPE)
5754 arg0 = copy_node (arg0);
5755 TREE_TYPE (arg0) = type;
5756 return arg0;
5758 else if (TREE_CODE (type) != INTEGER_TYPE)
5759 return fold (build (COND_EXPR, type, arg0,
5760 fold (build1 (code, type, integer_one_node)),
5761 fold (build1 (code, type, integer_zero_node))));
5764 else if (TREE_CODE_CLASS (code) == '<'
5765 && TREE_CODE (arg0) == COMPOUND_EXPR)
5766 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5767 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5768 else if (TREE_CODE_CLASS (code) == '<'
5769 && TREE_CODE (arg1) == COMPOUND_EXPR)
5770 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5771 fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
5772 else if (TREE_CODE_CLASS (code) == '2'
5773 || TREE_CODE_CLASS (code) == '<')
5775 if (TREE_CODE (arg0) == COMPOUND_EXPR)
5776 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5777 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5778 if (TREE_CODE (arg1) == COMPOUND_EXPR
5779 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
5780 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5781 fold (build (code, type,
5782 arg0, TREE_OPERAND (arg1, 1))));
5784 if (TREE_CODE (arg0) == COND_EXPR
5785 || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
5787 tem = fold_binary_op_with_conditional_arg (code, type, arg0, arg1,
5788 /*cond_first_p=*/1);
5789 if (tem != NULL_TREE)
5790 return tem;
5793 if (TREE_CODE (arg1) == COND_EXPR
5794 || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<')
5796 tem = fold_binary_op_with_conditional_arg (code, type, arg1, arg0,
5797 /*cond_first_p=*/0);
5798 if (tem != NULL_TREE)
5799 return tem;
5803 switch (code)
5805 case CONST_DECL:
5806 return fold (DECL_INITIAL (t));
5808 case NOP_EXPR:
5809 case FLOAT_EXPR:
5810 case CONVERT_EXPR:
5811 case FIX_TRUNC_EXPR:
5812 case FIX_CEIL_EXPR:
5813 case FIX_FLOOR_EXPR:
5814 case FIX_ROUND_EXPR:
5815 if (TREE_TYPE (TREE_OPERAND (t, 0)) == type)
5816 return TREE_OPERAND (t, 0);
5818 /* Handle cases of two conversions in a row. */
5819 if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
5820 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
5822 tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5823 tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
5824 int inside_int = INTEGRAL_TYPE_P (inside_type);
5825 int inside_ptr = POINTER_TYPE_P (inside_type);
5826 int inside_float = FLOAT_TYPE_P (inside_type);
5827 unsigned int inside_prec = TYPE_PRECISION (inside_type);
5828 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
5829 int inter_int = INTEGRAL_TYPE_P (inter_type);
5830 int inter_ptr = POINTER_TYPE_P (inter_type);
5831 int inter_float = FLOAT_TYPE_P (inter_type);
5832 unsigned int inter_prec = TYPE_PRECISION (inter_type);
5833 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
5834 int final_int = INTEGRAL_TYPE_P (type);
5835 int final_ptr = POINTER_TYPE_P (type);
5836 int final_float = FLOAT_TYPE_P (type);
5837 unsigned int final_prec = TYPE_PRECISION (type);
5838 int final_unsignedp = TYPE_UNSIGNED (type);
5840 /* In addition to the cases of two conversions in a row
5841 handled below, if we are converting something to its own
5842 type via an object of identical or wider precision, neither
5843 conversion is needed. */
5844 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
5845 && ((inter_int && final_int) || (inter_float && final_float))
5846 && inter_prec >= final_prec)
5847 return fold (build1 (code, type,
5848 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5850 /* Likewise, if the intermediate and final types are either both
5851 float or both integer, we don't need the middle conversion if
5852 it is wider than the final type and doesn't change the signedness
5853 (for integers). Avoid this if the final type is a pointer
5854 since then we sometimes need the inner conversion. Likewise if
5855 the outer has a precision not equal to the size of its mode. */
5856 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
5857 || (inter_float && inside_float))
5858 && inter_prec >= inside_prec
5859 && (inter_float || inter_unsignedp == inside_unsignedp)
5860 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
5861 && TYPE_MODE (type) == TYPE_MODE (inter_type))
5862 && ! final_ptr)
5863 return fold (build1 (code, type,
5864 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5866 /* If we have a sign-extension of a zero-extended value, we can
5867 replace that by a single zero-extension. */
5868 if (inside_int && inter_int && final_int
5869 && inside_prec < inter_prec && inter_prec < final_prec
5870 && inside_unsignedp && !inter_unsignedp)
5871 return fold (build1 (code, type,
5872 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5874 /* Two conversions in a row are not needed unless:
5875 - some conversion is floating-point (overstrict for now), or
5876 - the intermediate type is narrower than both initial and
5877 final, or
5878 - the intermediate type and innermost type differ in signedness,
5879 and the outermost type is wider than the intermediate, or
5880 - the initial type is a pointer type and the precisions of the
5881 intermediate and final types differ, or
5882 - the final type is a pointer type and the precisions of the
5883 initial and intermediate types differ. */
5884 if (! inside_float && ! inter_float && ! final_float
5885 && (inter_prec > inside_prec || inter_prec > final_prec)
5886 && ! (inside_int && inter_int
5887 && inter_unsignedp != inside_unsignedp
5888 && inter_prec < final_prec)
5889 && ((inter_unsignedp && inter_prec > inside_prec)
5890 == (final_unsignedp && final_prec > inter_prec))
5891 && ! (inside_ptr && inter_prec != final_prec)
5892 && ! (final_ptr && inside_prec != inter_prec)
5893 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
5894 && TYPE_MODE (type) == TYPE_MODE (inter_type))
5895 && ! final_ptr)
5896 return fold (build1 (code, type,
5897 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5900 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
5901 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
5902 /* Detect assigning a bitfield. */
5903 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
5904 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
5906 /* Don't leave an assignment inside a conversion
5907 unless assigning a bitfield. */
5908 tree prev = TREE_OPERAND (t, 0);
5909 tem = copy_node (t);
5910 TREE_OPERAND (tem, 0) = TREE_OPERAND (prev, 1);
5911 /* First do the assignment, then return converted constant. */
5912 tem = build (COMPOUND_EXPR, TREE_TYPE (tem), prev, fold (tem));
5913 TREE_NO_WARNING (tem) = 1;
5914 TREE_USED (tem) = 1;
5915 return tem;
5918 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
5919 constants (if x has signed type, the sign bit cannot be set
5920 in c). This folds extension into the BIT_AND_EXPR. */
5921 if (INTEGRAL_TYPE_P (type)
5922 && TREE_CODE (type) != BOOLEAN_TYPE
5923 && TREE_CODE (TREE_OPERAND (t, 0)) == BIT_AND_EXPR
5924 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST)
5926 tree and = TREE_OPERAND (t, 0);
5927 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
5928 int change = 0;
5930 if (TYPE_UNSIGNED (TREE_TYPE (and))
5931 || (TYPE_PRECISION (type)
5932 <= TYPE_PRECISION (TREE_TYPE (and))))
5933 change = 1;
5934 else if (TYPE_PRECISION (TREE_TYPE (and1))
5935 <= HOST_BITS_PER_WIDE_INT
5936 && host_integerp (and1, 1))
5938 unsigned HOST_WIDE_INT cst;
5940 cst = tree_low_cst (and1, 1);
5941 cst &= (HOST_WIDE_INT) -1
5942 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
5943 change = (cst == 0);
5944 #ifdef LOAD_EXTEND_OP
5945 if (change
5946 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
5947 == ZERO_EXTEND))
5949 tree uns = lang_hooks.types.unsigned_type (TREE_TYPE (and0));
5950 and0 = fold_convert (uns, and0);
5951 and1 = fold_convert (uns, and1);
5953 #endif
5955 if (change)
5956 return fold (build (BIT_AND_EXPR, type,
5957 fold_convert (type, and0),
5958 fold_convert (type, and1)));
5961 /* Convert (T1)((T2)X op Y) into (T1)X op Y, for pointer types T1 and
5962 T2 being pointers to types of the same size. */
5963 if (POINTER_TYPE_P (TREE_TYPE (t))
5964 && TREE_CODE_CLASS (TREE_CODE (arg0)) == '2'
5965 && TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
5966 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5968 tree arg00 = TREE_OPERAND (arg0, 0);
5969 tree t0 = TREE_TYPE (t);
5970 tree t1 = TREE_TYPE (arg00);
5971 tree tt0 = TREE_TYPE (t0);
5972 tree tt1 = TREE_TYPE (t1);
5973 tree s0 = TYPE_SIZE (tt0);
5974 tree s1 = TYPE_SIZE (tt1);
5976 if (s0 && s1 && operand_equal_p (s0, s1, OEP_ONLY_CONST))
5977 return build (TREE_CODE (arg0), t0, convert (t0, arg00),
5978 TREE_OPERAND (arg0, 1));
5981 tem = fold_convert_const (code, type, arg0);
5982 return tem ? tem : t;
5984 case VIEW_CONVERT_EXPR:
5985 if (TREE_CODE (TREE_OPERAND (t, 0)) == VIEW_CONVERT_EXPR)
5986 return build1 (VIEW_CONVERT_EXPR, type,
5987 TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5988 return t;
5990 case COMPONENT_REF:
5991 if (TREE_CODE (arg0) == CONSTRUCTOR
5992 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
5994 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
5995 if (m)
5996 return TREE_VALUE (m);
5998 return t;
6000 case RANGE_EXPR:
6001 if (TREE_CONSTANT (t) != wins)
6003 tem = copy_node (t);
6004 TREE_CONSTANT (tem) = wins;
6005 TREE_INVARIANT (tem) = wins;
6006 return tem;
6008 return t;
6010 case NEGATE_EXPR:
6011 if (negate_expr_p (arg0))
6012 return fold_convert (type, negate_expr (arg0));
6013 return t;
6015 case ABS_EXPR:
6016 if (wins
6017 && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
6018 return fold_abs_const (arg0, type);
6019 else if (TREE_CODE (arg0) == NEGATE_EXPR)
6020 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0)));
6021 /* Convert fabs((double)float) into (double)fabsf(float). */
6022 else if (TREE_CODE (arg0) == NOP_EXPR
6023 && TREE_CODE (type) == REAL_TYPE)
6025 tree targ0 = strip_float_extensions (arg0);
6026 if (targ0 != arg0)
6027 return fold_convert (type, fold (build1 (ABS_EXPR,
6028 TREE_TYPE (targ0),
6029 targ0)));
6031 else if (tree_expr_nonnegative_p (arg0))
6032 return arg0;
6033 return t;
6035 case CONJ_EXPR:
6036 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
6037 return fold_convert (type, arg0);
6038 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
6039 return build (COMPLEX_EXPR, type,
6040 TREE_OPERAND (arg0, 0),
6041 negate_expr (TREE_OPERAND (arg0, 1)));
6042 else if (TREE_CODE (arg0) == COMPLEX_CST)
6043 return build_complex (type, TREE_REALPART (arg0),
6044 negate_expr (TREE_IMAGPART (arg0)));
6045 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
6046 return fold (build (TREE_CODE (arg0), type,
6047 fold (build1 (CONJ_EXPR, type,
6048 TREE_OPERAND (arg0, 0))),
6049 fold (build1 (CONJ_EXPR,
6050 type, TREE_OPERAND (arg0, 1)))));
6051 else if (TREE_CODE (arg0) == CONJ_EXPR)
6052 return TREE_OPERAND (arg0, 0);
6053 return t;
6055 case BIT_NOT_EXPR:
6056 if (wins)
6058 tem = build_int_2 (~ TREE_INT_CST_LOW (arg0),
6059 ~ TREE_INT_CST_HIGH (arg0));
6060 TREE_TYPE (tem) = type;
6061 force_fit_type (tem, 0);
6062 TREE_OVERFLOW (tem) = TREE_OVERFLOW (arg0);
6063 TREE_CONSTANT_OVERFLOW (tem) = TREE_CONSTANT_OVERFLOW (arg0);
6064 return tem;
6066 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
6067 return TREE_OPERAND (arg0, 0);
6068 return t;
6070 case PLUS_EXPR:
6071 /* A + (-B) -> A - B */
6072 if (TREE_CODE (arg1) == NEGATE_EXPR)
6073 return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
6074 /* (-A) + B -> B - A */
6075 if (TREE_CODE (arg0) == NEGATE_EXPR
6076 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
6077 return fold (build (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
6078 if (! FLOAT_TYPE_P (type))
6080 if (integer_zerop (arg1))
6081 return non_lvalue (fold_convert (type, arg0));
6083 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
6084 with a constant, and the two constants have no bits in common,
6085 we should treat this as a BIT_IOR_EXPR since this may produce more
6086 simplifications. */
6087 if (TREE_CODE (arg0) == BIT_AND_EXPR
6088 && TREE_CODE (arg1) == BIT_AND_EXPR
6089 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6090 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
6091 && integer_zerop (const_binop (BIT_AND_EXPR,
6092 TREE_OPERAND (arg0, 1),
6093 TREE_OPERAND (arg1, 1), 0)))
6095 code = BIT_IOR_EXPR;
6096 goto bit_ior;
6099 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
6100 (plus (plus (mult) (mult)) (foo)) so that we can
6101 take advantage of the factoring cases below. */
6102 if ((TREE_CODE (arg0) == PLUS_EXPR
6103 && TREE_CODE (arg1) == MULT_EXPR)
6104 || (TREE_CODE (arg1) == PLUS_EXPR
6105 && TREE_CODE (arg0) == MULT_EXPR))
6107 tree parg0, parg1, parg, marg;
6109 if (TREE_CODE (arg0) == PLUS_EXPR)
6110 parg = arg0, marg = arg1;
6111 else
6112 parg = arg1, marg = arg0;
6113 parg0 = TREE_OPERAND (parg, 0);
6114 parg1 = TREE_OPERAND (parg, 1);
6115 STRIP_NOPS (parg0);
6116 STRIP_NOPS (parg1);
6118 if (TREE_CODE (parg0) == MULT_EXPR
6119 && TREE_CODE (parg1) != MULT_EXPR)
6120 return fold (build (PLUS_EXPR, type,
6121 fold (build (PLUS_EXPR, type,
6122 fold_convert (type, parg0),
6123 fold_convert (type, marg))),
6124 fold_convert (type, parg1)));
6125 if (TREE_CODE (parg0) != MULT_EXPR
6126 && TREE_CODE (parg1) == MULT_EXPR)
6127 return fold (build (PLUS_EXPR, type,
6128 fold (build (PLUS_EXPR, type,
6129 fold_convert (type, parg1),
6130 fold_convert (type, marg))),
6131 fold_convert (type, parg0)));
6134 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
6136 tree arg00, arg01, arg10, arg11;
6137 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6139 /* (A * C) + (B * C) -> (A+B) * C.
6140 We are most concerned about the case where C is a constant,
6141 but other combinations show up during loop reduction. Since
6142 it is not difficult, try all four possibilities. */
6144 arg00 = TREE_OPERAND (arg0, 0);
6145 arg01 = TREE_OPERAND (arg0, 1);
6146 arg10 = TREE_OPERAND (arg1, 0);
6147 arg11 = TREE_OPERAND (arg1, 1);
6148 same = NULL_TREE;
6150 if (operand_equal_p (arg01, arg11, 0))
6151 same = arg01, alt0 = arg00, alt1 = arg10;
6152 else if (operand_equal_p (arg00, arg10, 0))
6153 same = arg00, alt0 = arg01, alt1 = arg11;
6154 else if (operand_equal_p (arg00, arg11, 0))
6155 same = arg00, alt0 = arg01, alt1 = arg10;
6156 else if (operand_equal_p (arg01, arg10, 0))
6157 same = arg01, alt0 = arg00, alt1 = arg11;
6159 /* No identical multiplicands; see if we can find a common
6160 power-of-two factor in non-power-of-two multiplies. This
6161 can help in multi-dimensional array access. */
6162 else if (TREE_CODE (arg01) == INTEGER_CST
6163 && TREE_CODE (arg11) == INTEGER_CST
6164 && TREE_INT_CST_HIGH (arg01) == 0
6165 && TREE_INT_CST_HIGH (arg11) == 0)
6167 HOST_WIDE_INT int01, int11, tmp;
6168 int01 = TREE_INT_CST_LOW (arg01);
6169 int11 = TREE_INT_CST_LOW (arg11);
6171 /* Move min of absolute values to int11. */
6172 if ((int01 >= 0 ? int01 : -int01)
6173 < (int11 >= 0 ? int11 : -int11))
6175 tmp = int01, int01 = int11, int11 = tmp;
6176 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6177 alt0 = arg01, arg01 = arg11, arg11 = alt0;
6180 if (exact_log2 (int11) > 0 && int01 % int11 == 0)
6182 alt0 = fold (build (MULT_EXPR, type, arg00,
6183 build_int_2 (int01 / int11, 0)));
6184 alt1 = arg10;
6185 same = arg11;
6189 if (same)
6190 return fold (build (MULT_EXPR, type,
6191 fold (build (PLUS_EXPR, type, alt0, alt1)),
6192 same));
6195 else
6197 /* See if ARG1 is zero and X + ARG1 reduces to X. */
6198 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
6199 return non_lvalue (fold_convert (type, arg0));
6201 /* Likewise if the operands are reversed. */
6202 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
6203 return non_lvalue (fold_convert (type, arg1));
6205 /* Convert x+x into x*2.0. */
6206 if (operand_equal_p (arg0, arg1, 0)
6207 && SCALAR_FLOAT_TYPE_P (type))
6208 return fold (build (MULT_EXPR, type, arg0,
6209 build_real (type, dconst2)));
6211 /* Convert x*c+x into x*(c+1). */
6212 if (flag_unsafe_math_optimizations
6213 && TREE_CODE (arg0) == MULT_EXPR
6214 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
6215 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
6216 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
6218 REAL_VALUE_TYPE c;
6220 c = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
6221 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6222 return fold (build (MULT_EXPR, type, arg1,
6223 build_real (type, c)));
6226 /* Convert x+x*c into x*(c+1). */
6227 if (flag_unsafe_math_optimizations
6228 && TREE_CODE (arg1) == MULT_EXPR
6229 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
6230 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
6231 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
6233 REAL_VALUE_TYPE c;
6235 c = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
6236 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6237 return fold (build (MULT_EXPR, type, arg0,
6238 build_real (type, c)));
6241 /* Convert x*c1+x*c2 into x*(c1+c2). */
6242 if (flag_unsafe_math_optimizations
6243 && TREE_CODE (arg0) == MULT_EXPR
6244 && TREE_CODE (arg1) == MULT_EXPR
6245 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
6246 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
6247 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
6248 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
6249 && operand_equal_p (TREE_OPERAND (arg0, 0),
6250 TREE_OPERAND (arg1, 0), 0))
6252 REAL_VALUE_TYPE c1, c2;
6254 c1 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
6255 c2 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
6256 real_arithmetic (&c1, PLUS_EXPR, &c1, &c2);
6257 return fold (build (MULT_EXPR, type,
6258 TREE_OPERAND (arg0, 0),
6259 build_real (type, c1)));
6261 /* Convert a + (b*c + d*e) into (a + b*c) + d*e */
6262 if (flag_unsafe_math_optimizations
6263 && TREE_CODE (arg1) == PLUS_EXPR
6264 && TREE_CODE (arg0) != MULT_EXPR)
6266 tree tree10 = TREE_OPERAND (arg1, 0);
6267 tree tree11 = TREE_OPERAND (arg1, 1);
6268 if (TREE_CODE (tree11) == MULT_EXPR
6269 && TREE_CODE (tree10) == MULT_EXPR)
6271 tree tree0;
6272 tree0 = fold (build (PLUS_EXPR, type, arg0, tree10));
6273 return fold (build (PLUS_EXPR, type, tree0, tree11));
6276 /* Convert (b*c + d*e) + a into b*c + (d*e +a) */
6277 if (flag_unsafe_math_optimizations
6278 && TREE_CODE (arg0) == PLUS_EXPR
6279 && TREE_CODE (arg1) != MULT_EXPR)
6281 tree tree00 = TREE_OPERAND (arg0, 0);
6282 tree tree01 = TREE_OPERAND (arg0, 1);
6283 if (TREE_CODE (tree01) == MULT_EXPR
6284 && TREE_CODE (tree00) == MULT_EXPR)
6286 tree tree0;
6287 tree0 = fold (build (PLUS_EXPR, type, tree01, arg1));
6288 return fold (build (PLUS_EXPR, type, tree00, tree0));
6293 bit_rotate:
6294 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
6295 is a rotate of A by C1 bits. */
6296 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
6297 is a rotate of A by B bits. */
6299 enum tree_code code0, code1;
6300 code0 = TREE_CODE (arg0);
6301 code1 = TREE_CODE (arg1);
6302 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
6303 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
6304 && operand_equal_p (TREE_OPERAND (arg0, 0),
6305 TREE_OPERAND (arg1, 0), 0)
6306 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
6308 tree tree01, tree11;
6309 enum tree_code code01, code11;
6311 tree01 = TREE_OPERAND (arg0, 1);
6312 tree11 = TREE_OPERAND (arg1, 1);
6313 STRIP_NOPS (tree01);
6314 STRIP_NOPS (tree11);
6315 code01 = TREE_CODE (tree01);
6316 code11 = TREE_CODE (tree11);
6317 if (code01 == INTEGER_CST
6318 && code11 == INTEGER_CST
6319 && TREE_INT_CST_HIGH (tree01) == 0
6320 && TREE_INT_CST_HIGH (tree11) == 0
6321 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
6322 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
6323 return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
6324 code0 == LSHIFT_EXPR ? tree01 : tree11);
6325 else if (code11 == MINUS_EXPR)
6327 tree tree110, tree111;
6328 tree110 = TREE_OPERAND (tree11, 0);
6329 tree111 = TREE_OPERAND (tree11, 1);
6330 STRIP_NOPS (tree110);
6331 STRIP_NOPS (tree111);
6332 if (TREE_CODE (tree110) == INTEGER_CST
6333 && 0 == compare_tree_int (tree110,
6334 TYPE_PRECISION
6335 (TREE_TYPE (TREE_OPERAND
6336 (arg0, 0))))
6337 && operand_equal_p (tree01, tree111, 0))
6338 return build ((code0 == LSHIFT_EXPR
6339 ? LROTATE_EXPR
6340 : RROTATE_EXPR),
6341 type, TREE_OPERAND (arg0, 0), tree01);
6343 else if (code01 == MINUS_EXPR)
6345 tree tree010, tree011;
6346 tree010 = TREE_OPERAND (tree01, 0);
6347 tree011 = TREE_OPERAND (tree01, 1);
6348 STRIP_NOPS (tree010);
6349 STRIP_NOPS (tree011);
6350 if (TREE_CODE (tree010) == INTEGER_CST
6351 && 0 == compare_tree_int (tree010,
6352 TYPE_PRECISION
6353 (TREE_TYPE (TREE_OPERAND
6354 (arg0, 0))))
6355 && operand_equal_p (tree11, tree011, 0))
6356 return build ((code0 != LSHIFT_EXPR
6357 ? LROTATE_EXPR
6358 : RROTATE_EXPR),
6359 type, TREE_OPERAND (arg0, 0), tree11);
6364 associate:
6365 /* In most languages, can't associate operations on floats through
6366 parentheses. Rather than remember where the parentheses were, we
6367 don't associate floats at all, unless the user has specified
6368 -funsafe-math-optimizations. */
6370 if (! wins
6371 && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
6373 tree var0, con0, lit0, minus_lit0;
6374 tree var1, con1, lit1, minus_lit1;
6376 /* Split both trees into variables, constants, and literals. Then
6377 associate each group together, the constants with literals,
6378 then the result with variables. This increases the chances of
6379 literals being recombined later and of generating relocatable
6380 expressions for the sum of a constant and literal. */
6381 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
6382 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
6383 code == MINUS_EXPR);
6385 /* Only do something if we found more than two objects. Otherwise,
6386 nothing has changed and we risk infinite recursion. */
6387 if (2 < ((var0 != 0) + (var1 != 0)
6388 + (con0 != 0) + (con1 != 0)
6389 + (lit0 != 0) + (lit1 != 0)
6390 + (minus_lit0 != 0) + (minus_lit1 != 0)))
6392 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
6393 if (code == MINUS_EXPR)
6394 code = PLUS_EXPR;
6396 var0 = associate_trees (var0, var1, code, type);
6397 con0 = associate_trees (con0, con1, code, type);
6398 lit0 = associate_trees (lit0, lit1, code, type);
6399 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
6401 /* Preserve the MINUS_EXPR if the negative part of the literal is
6402 greater than the positive part. Otherwise, the multiplicative
6403 folding code (i.e extract_muldiv) may be fooled in case
6404 unsigned constants are subtracted, like in the following
6405 example: ((X*2 + 4) - 8U)/2. */
6406 if (minus_lit0 && lit0)
6408 if (TREE_CODE (lit0) == INTEGER_CST
6409 && TREE_CODE (minus_lit0) == INTEGER_CST
6410 && tree_int_cst_lt (lit0, minus_lit0))
6412 minus_lit0 = associate_trees (minus_lit0, lit0,
6413 MINUS_EXPR, type);
6414 lit0 = 0;
6416 else
6418 lit0 = associate_trees (lit0, minus_lit0,
6419 MINUS_EXPR, type);
6420 minus_lit0 = 0;
6423 if (minus_lit0)
6425 if (con0 == 0)
6426 return fold_convert (type,
6427 associate_trees (var0, minus_lit0,
6428 MINUS_EXPR, type));
6429 else
6431 con0 = associate_trees (con0, minus_lit0,
6432 MINUS_EXPR, type);
6433 return fold_convert (type,
6434 associate_trees (var0, con0,
6435 PLUS_EXPR, type));
6439 con0 = associate_trees (con0, lit0, code, type);
6440 return fold_convert (type, associate_trees (var0, con0,
6441 code, type));
6445 binary:
6446 if (wins)
6447 t1 = const_binop (code, arg0, arg1, 0);
6448 if (t1 != NULL_TREE)
6450 /* The return value should always have
6451 the same type as the original expression. */
6452 if (TREE_TYPE (t1) != type)
6453 t1 = fold_convert (type, t1);
6455 return t1;
6457 return t;
6459 case MINUS_EXPR:
6460 /* A - (-B) -> A + B */
6461 if (TREE_CODE (arg1) == NEGATE_EXPR)
6462 return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
6463 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
6464 if (TREE_CODE (arg0) == NEGATE_EXPR
6465 && (FLOAT_TYPE_P (type)
6466 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv))
6467 && negate_expr_p (arg1)
6468 && reorder_operands_p (arg0, arg1))
6469 return fold (build (MINUS_EXPR, type, negate_expr (arg1),
6470 TREE_OPERAND (arg0, 0)));
6472 if (! FLOAT_TYPE_P (type))
6474 if (! wins && integer_zerop (arg0))
6475 return negate_expr (fold_convert (type, arg1));
6476 if (integer_zerop (arg1))
6477 return non_lvalue (fold_convert (type, arg0));
6479 /* Fold A - (A & B) into ~B & A. */
6480 if (!TREE_SIDE_EFFECTS (arg0)
6481 && TREE_CODE (arg1) == BIT_AND_EXPR)
6483 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
6484 return fold (build (BIT_AND_EXPR, type,
6485 fold (build1 (BIT_NOT_EXPR, type,
6486 TREE_OPERAND (arg1, 0))),
6487 arg0));
6488 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
6489 return fold (build (BIT_AND_EXPR, type,
6490 fold (build1 (BIT_NOT_EXPR, type,
6491 TREE_OPERAND (arg1, 1))),
6492 arg0));
6495 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
6496 any power of 2 minus 1. */
6497 if (TREE_CODE (arg0) == BIT_AND_EXPR
6498 && TREE_CODE (arg1) == BIT_AND_EXPR
6499 && operand_equal_p (TREE_OPERAND (arg0, 0),
6500 TREE_OPERAND (arg1, 0), 0))
6502 tree mask0 = TREE_OPERAND (arg0, 1);
6503 tree mask1 = TREE_OPERAND (arg1, 1);
6504 tree tem = fold (build1 (BIT_NOT_EXPR, type, mask0));
6506 if (operand_equal_p (tem, mask1, 0))
6508 tem = fold (build (BIT_XOR_EXPR, type,
6509 TREE_OPERAND (arg0, 0), mask1));
6510 return fold (build (MINUS_EXPR, type, tem, mask1));
6515 /* See if ARG1 is zero and X - ARG1 reduces to X. */
6516 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
6517 return non_lvalue (fold_convert (type, arg0));
6519 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
6520 ARG0 is zero and X + ARG0 reduces to X, since that would mean
6521 (-ARG1 + ARG0) reduces to -ARG1. */
6522 else if (!wins && fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
6523 return negate_expr (fold_convert (type, arg1));
6525 /* Fold &x - &x. This can happen from &x.foo - &x.
6526 This is unsafe for certain floats even in non-IEEE formats.
6527 In IEEE, it is unsafe because it does wrong for NaNs.
6528 Also note that operand_equal_p is always false if an operand
6529 is volatile. */
6531 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
6532 && operand_equal_p (arg0, arg1, 0))
6533 return fold_convert (type, integer_zero_node);
6535 /* A - B -> A + (-B) if B is easily negatable. */
6536 if (!wins && negate_expr_p (arg1)
6537 && (FLOAT_TYPE_P (type)
6538 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv)))
6539 return fold (build (PLUS_EXPR, type, arg0, negate_expr (arg1)));
6541 if (TREE_CODE (arg0) == MULT_EXPR
6542 && TREE_CODE (arg1) == MULT_EXPR
6543 && (INTEGRAL_TYPE_P (type) || flag_unsafe_math_optimizations))
6545 /* (A * C) - (B * C) -> (A-B) * C. */
6546 if (operand_equal_p (TREE_OPERAND (arg0, 1),
6547 TREE_OPERAND (arg1, 1), 0))
6548 return fold (build (MULT_EXPR, type,
6549 fold (build (MINUS_EXPR, type,
6550 TREE_OPERAND (arg0, 0),
6551 TREE_OPERAND (arg1, 0))),
6552 TREE_OPERAND (arg0, 1)));
6553 /* (A * C1) - (A * C2) -> A * (C1-C2). */
6554 if (operand_equal_p (TREE_OPERAND (arg0, 0),
6555 TREE_OPERAND (arg1, 0), 0))
6556 return fold (build (MULT_EXPR, type,
6557 TREE_OPERAND (arg0, 0),
6558 fold (build (MINUS_EXPR, type,
6559 TREE_OPERAND (arg0, 1),
6560 TREE_OPERAND (arg1, 1)))));
6563 goto associate;
6565 case MULT_EXPR:
6566 /* (-A) * (-B) -> A * B */
6567 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
6568 return fold (build (MULT_EXPR, type,
6569 TREE_OPERAND (arg0, 0),
6570 negate_expr (arg1)));
6571 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
6572 return fold (build (MULT_EXPR, type,
6573 negate_expr (arg0),
6574 TREE_OPERAND (arg1, 0)));
6576 if (! FLOAT_TYPE_P (type))
6578 if (integer_zerop (arg1))
6579 return omit_one_operand (type, arg1, arg0);
6580 if (integer_onep (arg1))
6581 return non_lvalue (fold_convert (type, arg0));
6583 /* (a * (1 << b)) is (a << b) */
6584 if (TREE_CODE (arg1) == LSHIFT_EXPR
6585 && integer_onep (TREE_OPERAND (arg1, 0)))
6586 return fold (build (LSHIFT_EXPR, type, arg0,
6587 TREE_OPERAND (arg1, 1)));
6588 if (TREE_CODE (arg0) == LSHIFT_EXPR
6589 && integer_onep (TREE_OPERAND (arg0, 0)))
6590 return fold (build (LSHIFT_EXPR, type, arg1,
6591 TREE_OPERAND (arg0, 1)));
6593 if (TREE_CODE (arg1) == INTEGER_CST
6594 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0),
6595 fold_convert (type, arg1),
6596 code, NULL_TREE)))
6597 return fold_convert (type, tem);
6600 else
6602 /* Maybe fold x * 0 to 0. The expressions aren't the same
6603 when x is NaN, since x * 0 is also NaN. Nor are they the
6604 same in modes with signed zeros, since multiplying a
6605 negative value by 0 gives -0, not +0. */
6606 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
6607 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
6608 && real_zerop (arg1))
6609 return omit_one_operand (type, arg1, arg0);
6610 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
6611 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6612 && real_onep (arg1))
6613 return non_lvalue (fold_convert (type, arg0));
6615 /* Transform x * -1.0 into -x. */
6616 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6617 && real_minus_onep (arg1))
6618 return fold_convert (type, negate_expr (arg0));
6620 /* Convert (C1/X)*C2 into (C1*C2)/X. */
6621 if (flag_unsafe_math_optimizations
6622 && TREE_CODE (arg0) == RDIV_EXPR
6623 && TREE_CODE (arg1) == REAL_CST
6624 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
6626 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
6627 arg1, 0);
6628 if (tem)
6629 return fold (build (RDIV_EXPR, type, tem,
6630 TREE_OPERAND (arg0, 1)));
6633 if (flag_unsafe_math_optimizations)
6635 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
6636 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
6638 /* Optimizations of root(...)*root(...). */
6639 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
6641 tree rootfn, arg, arglist;
6642 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6643 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6645 /* Optimize sqrt(x)*sqrt(x) as x. */
6646 if (BUILTIN_SQRT_P (fcode0)
6647 && operand_equal_p (arg00, arg10, 0)
6648 && ! HONOR_SNANS (TYPE_MODE (type)))
6649 return arg00;
6651 /* Optimize root(x)*root(y) as root(x*y). */
6652 rootfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6653 arg = fold (build (MULT_EXPR, type, arg00, arg10));
6654 arglist = build_tree_list (NULL_TREE, arg);
6655 return build_function_call_expr (rootfn, arglist);
6658 /* Optimize expN(x)*expN(y) as expN(x+y). */
6659 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
6661 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6662 tree arg = build (PLUS_EXPR, type,
6663 TREE_VALUE (TREE_OPERAND (arg0, 1)),
6664 TREE_VALUE (TREE_OPERAND (arg1, 1)));
6665 tree arglist = build_tree_list (NULL_TREE, fold (arg));
6666 return build_function_call_expr (expfn, arglist);
6669 /* Optimizations of pow(...)*pow(...). */
6670 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
6671 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
6672 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
6674 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6675 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
6676 1)));
6677 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6678 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
6679 1)));
6681 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
6682 if (operand_equal_p (arg01, arg11, 0))
6684 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6685 tree arg = build (MULT_EXPR, type, arg00, arg10);
6686 tree arglist = tree_cons (NULL_TREE, fold (arg),
6687 build_tree_list (NULL_TREE,
6688 arg01));
6689 return build_function_call_expr (powfn, arglist);
6692 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
6693 if (operand_equal_p (arg00, arg10, 0))
6695 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6696 tree arg = fold (build (PLUS_EXPR, type, arg01, arg11));
6697 tree arglist = tree_cons (NULL_TREE, arg00,
6698 build_tree_list (NULL_TREE,
6699 arg));
6700 return build_function_call_expr (powfn, arglist);
6704 /* Optimize tan(x)*cos(x) as sin(x). */
6705 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
6706 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
6707 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
6708 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
6709 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
6710 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
6711 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
6712 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
6714 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
6716 if (sinfn != NULL_TREE)
6717 return build_function_call_expr (sinfn,
6718 TREE_OPERAND (arg0, 1));
6721 /* Optimize x*pow(x,c) as pow(x,c+1). */
6722 if (fcode1 == BUILT_IN_POW
6723 || fcode1 == BUILT_IN_POWF
6724 || fcode1 == BUILT_IN_POWL)
6726 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6727 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
6728 1)));
6729 if (TREE_CODE (arg11) == REAL_CST
6730 && ! TREE_CONSTANT_OVERFLOW (arg11)
6731 && operand_equal_p (arg0, arg10, 0))
6733 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
6734 REAL_VALUE_TYPE c;
6735 tree arg, arglist;
6737 c = TREE_REAL_CST (arg11);
6738 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6739 arg = build_real (type, c);
6740 arglist = build_tree_list (NULL_TREE, arg);
6741 arglist = tree_cons (NULL_TREE, arg0, arglist);
6742 return build_function_call_expr (powfn, arglist);
6746 /* Optimize pow(x,c)*x as pow(x,c+1). */
6747 if (fcode0 == BUILT_IN_POW
6748 || fcode0 == BUILT_IN_POWF
6749 || fcode0 == BUILT_IN_POWL)
6751 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6752 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
6753 1)));
6754 if (TREE_CODE (arg01) == REAL_CST
6755 && ! TREE_CONSTANT_OVERFLOW (arg01)
6756 && operand_equal_p (arg1, arg00, 0))
6758 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6759 REAL_VALUE_TYPE c;
6760 tree arg, arglist;
6762 c = TREE_REAL_CST (arg01);
6763 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6764 arg = build_real (type, c);
6765 arglist = build_tree_list (NULL_TREE, arg);
6766 arglist = tree_cons (NULL_TREE, arg1, arglist);
6767 return build_function_call_expr (powfn, arglist);
6771 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
6772 if (! optimize_size
6773 && operand_equal_p (arg0, arg1, 0))
6775 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6777 if (powfn)
6779 tree arg = build_real (type, dconst2);
6780 tree arglist = build_tree_list (NULL_TREE, arg);
6781 arglist = tree_cons (NULL_TREE, arg0, arglist);
6782 return build_function_call_expr (powfn, arglist);
6787 goto associate;
6789 case BIT_IOR_EXPR:
6790 bit_ior:
6791 if (integer_all_onesp (arg1))
6792 return omit_one_operand (type, arg1, arg0);
6793 if (integer_zerop (arg1))
6794 return non_lvalue (fold_convert (type, arg0));
6795 if (operand_equal_p (arg0, arg1, 0))
6796 return non_lvalue (fold_convert (type, arg0));
6797 t1 = distribute_bit_expr (code, type, arg0, arg1);
6798 if (t1 != NULL_TREE)
6799 return t1;
6801 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
6803 This results in more efficient code for machines without a NAND
6804 instruction. Combine will canonicalize to the first form
6805 which will allow use of NAND instructions provided by the
6806 backend if they exist. */
6807 if (TREE_CODE (arg0) == BIT_NOT_EXPR
6808 && TREE_CODE (arg1) == BIT_NOT_EXPR)
6810 return fold (build1 (BIT_NOT_EXPR, type,
6811 build (BIT_AND_EXPR, type,
6812 TREE_OPERAND (arg0, 0),
6813 TREE_OPERAND (arg1, 0))));
6816 /* See if this can be simplified into a rotate first. If that
6817 is unsuccessful continue in the association code. */
6818 goto bit_rotate;
6820 case BIT_XOR_EXPR:
6821 if (integer_zerop (arg1))
6822 return non_lvalue (fold_convert (type, arg0));
6823 if (integer_all_onesp (arg1))
6824 return fold (build1 (BIT_NOT_EXPR, type, arg0));
6825 if (operand_equal_p (arg0, arg1, 0))
6826 return omit_one_operand (type, integer_zero_node, arg0);
6828 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
6829 with a constant, and the two constants have no bits in common,
6830 we should treat this as a BIT_IOR_EXPR since this may produce more
6831 simplifications. */
6832 if (TREE_CODE (arg0) == BIT_AND_EXPR
6833 && TREE_CODE (arg1) == BIT_AND_EXPR
6834 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6835 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
6836 && integer_zerop (const_binop (BIT_AND_EXPR,
6837 TREE_OPERAND (arg0, 1),
6838 TREE_OPERAND (arg1, 1), 0)))
6840 code = BIT_IOR_EXPR;
6841 goto bit_ior;
6844 /* See if this can be simplified into a rotate first. If that
6845 is unsuccessful continue in the association code. */
6846 goto bit_rotate;
6848 case BIT_AND_EXPR:
6849 if (integer_all_onesp (arg1))
6850 return non_lvalue (fold_convert (type, arg0));
6851 if (integer_zerop (arg1))
6852 return omit_one_operand (type, arg1, arg0);
6853 if (operand_equal_p (arg0, arg1, 0))
6854 return non_lvalue (fold_convert (type, arg0));
6855 t1 = distribute_bit_expr (code, type, arg0, arg1);
6856 if (t1 != NULL_TREE)
6857 return t1;
6858 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
6859 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
6860 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
6862 unsigned int prec
6863 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
6865 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
6866 && (~TREE_INT_CST_LOW (arg1)
6867 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
6868 return fold_convert (type, TREE_OPERAND (arg0, 0));
6871 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
6873 This results in more efficient code for machines without a NOR
6874 instruction. Combine will canonicalize to the first form
6875 which will allow use of NOR instructions provided by the
6876 backend if they exist. */
6877 if (TREE_CODE (arg0) == BIT_NOT_EXPR
6878 && TREE_CODE (arg1) == BIT_NOT_EXPR)
6880 return fold (build1 (BIT_NOT_EXPR, type,
6881 build (BIT_IOR_EXPR, type,
6882 TREE_OPERAND (arg0, 0),
6883 TREE_OPERAND (arg1, 0))));
6886 goto associate;
6888 case RDIV_EXPR:
6889 /* Don't touch a floating-point divide by zero unless the mode
6890 of the constant can represent infinity. */
6891 if (TREE_CODE (arg1) == REAL_CST
6892 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
6893 && real_zerop (arg1))
6894 return t;
6896 /* (-A) / (-B) -> A / B */
6897 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
6898 return fold (build (RDIV_EXPR, type,
6899 TREE_OPERAND (arg0, 0),
6900 negate_expr (arg1)));
6901 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
6902 return fold (build (RDIV_EXPR, type,
6903 negate_expr (arg0),
6904 TREE_OPERAND (arg1, 0)));
6906 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
6907 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6908 && real_onep (arg1))
6909 return non_lvalue (fold_convert (type, arg0));
6911 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
6912 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6913 && real_minus_onep (arg1))
6914 return non_lvalue (fold_convert (type, negate_expr (arg0)));
6916 /* If ARG1 is a constant, we can convert this to a multiply by the
6917 reciprocal. This does not have the same rounding properties,
6918 so only do this if -funsafe-math-optimizations. We can actually
6919 always safely do it if ARG1 is a power of two, but it's hard to
6920 tell if it is or not in a portable manner. */
6921 if (TREE_CODE (arg1) == REAL_CST)
6923 if (flag_unsafe_math_optimizations
6924 && 0 != (tem = const_binop (code, build_real (type, dconst1),
6925 arg1, 0)))
6926 return fold (build (MULT_EXPR, type, arg0, tem));
6927 /* Find the reciprocal if optimizing and the result is exact. */
6928 if (optimize)
6930 REAL_VALUE_TYPE r;
6931 r = TREE_REAL_CST (arg1);
6932 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
6934 tem = build_real (type, r);
6935 return fold (build (MULT_EXPR, type, arg0, tem));
6939 /* Convert A/B/C to A/(B*C). */
6940 if (flag_unsafe_math_optimizations
6941 && TREE_CODE (arg0) == RDIV_EXPR)
6942 return fold (build (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
6943 fold (build (MULT_EXPR, type,
6944 TREE_OPERAND (arg0, 1), arg1))));
6946 /* Convert A/(B/C) to (A/B)*C. */
6947 if (flag_unsafe_math_optimizations
6948 && TREE_CODE (arg1) == RDIV_EXPR)
6949 return fold (build (MULT_EXPR, type,
6950 fold (build (RDIV_EXPR, type, arg0,
6951 TREE_OPERAND (arg1, 0))),
6952 TREE_OPERAND (arg1, 1)));
6954 /* Convert C1/(X*C2) into (C1/C2)/X. */
6955 if (flag_unsafe_math_optimizations
6956 && TREE_CODE (arg1) == MULT_EXPR
6957 && TREE_CODE (arg0) == REAL_CST
6958 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
6960 tree tem = const_binop (RDIV_EXPR, arg0,
6961 TREE_OPERAND (arg1, 1), 0);
6962 if (tem)
6963 return fold (build (RDIV_EXPR, type, tem,
6964 TREE_OPERAND (arg1, 0)));
6967 if (flag_unsafe_math_optimizations)
6969 enum built_in_function fcode = builtin_mathfn_code (arg1);
6970 /* Optimize x/expN(y) into x*expN(-y). */
6971 if (BUILTIN_EXPONENT_P (fcode))
6973 tree expfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
6974 tree arg = negate_expr (TREE_VALUE (TREE_OPERAND (arg1, 1)));
6975 tree arglist = build_tree_list (NULL_TREE,
6976 fold_convert (type, arg));
6977 arg1 = build_function_call_expr (expfn, arglist);
6978 return fold (build (MULT_EXPR, type, arg0, arg1));
6981 /* Optimize x/pow(y,z) into x*pow(y,-z). */
6982 if (fcode == BUILT_IN_POW
6983 || fcode == BUILT_IN_POWF
6984 || fcode == BUILT_IN_POWL)
6986 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
6987 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6988 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1, 1)));
6989 tree neg11 = fold_convert (type, negate_expr (arg11));
6990 tree arglist = tree_cons(NULL_TREE, arg10,
6991 build_tree_list (NULL_TREE, neg11));
6992 arg1 = build_function_call_expr (powfn, arglist);
6993 return fold (build (MULT_EXPR, type, arg0, arg1));
6997 if (flag_unsafe_math_optimizations)
6999 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
7000 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
7002 /* Optimize sin(x)/cos(x) as tan(x). */
7003 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
7004 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
7005 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
7006 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
7007 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
7009 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
7011 if (tanfn != NULL_TREE)
7012 return build_function_call_expr (tanfn,
7013 TREE_OPERAND (arg0, 1));
7016 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
7017 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
7018 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
7019 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
7020 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
7021 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
7023 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
7025 if (tanfn != NULL_TREE)
7027 tree tmp = TREE_OPERAND (arg0, 1);
7028 tmp = build_function_call_expr (tanfn, tmp);
7029 return fold (build (RDIV_EXPR, type,
7030 build_real (type, dconst1),
7031 tmp));
7035 /* Optimize pow(x,c)/x as pow(x,c-1). */
7036 if (fcode0 == BUILT_IN_POW
7037 || fcode0 == BUILT_IN_POWF
7038 || fcode0 == BUILT_IN_POWL)
7040 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7041 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7042 if (TREE_CODE (arg01) == REAL_CST
7043 && ! TREE_CONSTANT_OVERFLOW (arg01)
7044 && operand_equal_p (arg1, arg00, 0))
7046 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7047 REAL_VALUE_TYPE c;
7048 tree arg, arglist;
7050 c = TREE_REAL_CST (arg01);
7051 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
7052 arg = build_real (type, c);
7053 arglist = build_tree_list (NULL_TREE, arg);
7054 arglist = tree_cons (NULL_TREE, arg1, arglist);
7055 return build_function_call_expr (powfn, arglist);
7059 goto binary;
7061 case TRUNC_DIV_EXPR:
7062 case ROUND_DIV_EXPR:
7063 case FLOOR_DIV_EXPR:
7064 case CEIL_DIV_EXPR:
7065 case EXACT_DIV_EXPR:
7066 if (integer_onep (arg1))
7067 return non_lvalue (fold_convert (type, arg0));
7068 if (integer_zerop (arg1))
7069 return t;
7070 /* X / -1 is -X. */
7071 if (!TYPE_UNSIGNED (type)
7072 && TREE_CODE (arg1) == INTEGER_CST
7073 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
7074 && TREE_INT_CST_HIGH (arg1) == -1)
7075 return fold_convert (type, negate_expr (arg0));
7077 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
7078 operation, EXACT_DIV_EXPR.
7080 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
7081 At one time others generated faster code, it's not clear if they do
7082 after the last round to changes to the DIV code in expmed.c. */
7083 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
7084 && multiple_of_p (type, arg0, arg1))
7085 return fold (build (EXACT_DIV_EXPR, type, arg0, arg1));
7087 if (TREE_CODE (arg1) == INTEGER_CST
7088 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
7089 code, NULL_TREE)))
7090 return fold_convert (type, tem);
7092 goto binary;
7094 case CEIL_MOD_EXPR:
7095 case FLOOR_MOD_EXPR:
7096 case ROUND_MOD_EXPR:
7097 case TRUNC_MOD_EXPR:
7098 if (integer_onep (arg1))
7099 return omit_one_operand (type, integer_zero_node, arg0);
7100 if (integer_zerop (arg1))
7101 return t;
7102 /* X % -1 is zero. */
7103 if (!TYPE_UNSIGNED (type)
7104 && TREE_CODE (arg1) == INTEGER_CST
7105 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
7106 && TREE_INT_CST_HIGH (arg1) == -1)
7107 return omit_one_operand (type, integer_zero_node, arg0);
7109 if (TREE_CODE (arg1) == INTEGER_CST
7110 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
7111 code, NULL_TREE)))
7112 return fold_convert (type, tem);
7114 goto binary;
7116 case LROTATE_EXPR:
7117 case RROTATE_EXPR:
7118 if (integer_all_onesp (arg0))
7119 return omit_one_operand (type, arg0, arg1);
7120 goto shift;
7122 case RSHIFT_EXPR:
7123 /* Optimize -1 >> x for arithmetic right shifts. */
7124 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
7125 return omit_one_operand (type, arg0, arg1);
7126 /* ... fall through ... */
7128 case LSHIFT_EXPR:
7129 shift:
7130 if (integer_zerop (arg1))
7131 return non_lvalue (fold_convert (type, arg0));
7132 if (integer_zerop (arg0))
7133 return omit_one_operand (type, arg0, arg1);
7135 /* Since negative shift count is not well-defined,
7136 don't try to compute it in the compiler. */
7137 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
7138 return t;
7139 /* Rewrite an LROTATE_EXPR by a constant into an
7140 RROTATE_EXPR by a new constant. */
7141 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
7143 tree tem = build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0);
7144 tem = fold_convert (TREE_TYPE (arg1), tem);
7145 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
7146 return fold (build (RROTATE_EXPR, type, arg0, tem));
7149 /* If we have a rotate of a bit operation with the rotate count and
7150 the second operand of the bit operation both constant,
7151 permute the two operations. */
7152 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
7153 && (TREE_CODE (arg0) == BIT_AND_EXPR
7154 || TREE_CODE (arg0) == BIT_IOR_EXPR
7155 || TREE_CODE (arg0) == BIT_XOR_EXPR)
7156 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7157 return fold (build (TREE_CODE (arg0), type,
7158 fold (build (code, type,
7159 TREE_OPERAND (arg0, 0), arg1)),
7160 fold (build (code, type,
7161 TREE_OPERAND (arg0, 1), arg1))));
7163 /* Two consecutive rotates adding up to the width of the mode can
7164 be ignored. */
7165 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
7166 && TREE_CODE (arg0) == RROTATE_EXPR
7167 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7168 && TREE_INT_CST_HIGH (arg1) == 0
7169 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
7170 && ((TREE_INT_CST_LOW (arg1)
7171 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
7172 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
7173 return TREE_OPERAND (arg0, 0);
7175 goto binary;
7177 case MIN_EXPR:
7178 if (operand_equal_p (arg0, arg1, 0))
7179 return omit_one_operand (type, arg0, arg1);
7180 if (INTEGRAL_TYPE_P (type)
7181 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
7182 return omit_one_operand (type, arg1, arg0);
7183 goto associate;
7185 case MAX_EXPR:
7186 if (operand_equal_p (arg0, arg1, 0))
7187 return omit_one_operand (type, arg0, arg1);
7188 if (INTEGRAL_TYPE_P (type)
7189 && TYPE_MAX_VALUE (type)
7190 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
7191 return omit_one_operand (type, arg1, arg0);
7192 goto associate;
7194 case TRUTH_NOT_EXPR:
7195 /* Note that the operand of this must be an int
7196 and its values must be 0 or 1.
7197 ("true" is a fixed value perhaps depending on the language,
7198 but we don't handle values other than 1 correctly yet.) */
7199 tem = invert_truthvalue (arg0);
7200 /* Avoid infinite recursion. */
7201 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
7203 tem = fold_single_bit_test (code, arg0, arg1, type);
7204 if (tem)
7205 return tem;
7206 return t;
7208 return fold_convert (type, tem);
7210 case TRUTH_ANDIF_EXPR:
7211 /* Note that the operands of this must be ints
7212 and their values must be 0 or 1.
7213 ("true" is a fixed value perhaps depending on the language.) */
7214 /* If first arg is constant zero, return it. */
7215 if (integer_zerop (arg0))
7216 return fold_convert (type, arg0);
7217 case TRUTH_AND_EXPR:
7218 /* If either arg is constant true, drop it. */
7219 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
7220 return non_lvalue (fold_convert (type, arg1));
7221 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
7222 /* Preserve sequence points. */
7223 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
7224 return non_lvalue (fold_convert (type, arg0));
7225 /* If second arg is constant zero, result is zero, but first arg
7226 must be evaluated. */
7227 if (integer_zerop (arg1))
7228 return omit_one_operand (type, arg1, arg0);
7229 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
7230 case will be handled here. */
7231 if (integer_zerop (arg0))
7232 return omit_one_operand (type, arg0, arg1);
7234 truth_andor:
7235 /* We only do these simplifications if we are optimizing. */
7236 if (!optimize)
7237 return t;
7239 /* Check for things like (A || B) && (A || C). We can convert this
7240 to A || (B && C). Note that either operator can be any of the four
7241 truth and/or operations and the transformation will still be
7242 valid. Also note that we only care about order for the
7243 ANDIF and ORIF operators. If B contains side effects, this
7244 might change the truth-value of A. */
7245 if (TREE_CODE (arg0) == TREE_CODE (arg1)
7246 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
7247 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
7248 || TREE_CODE (arg0) == TRUTH_AND_EXPR
7249 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
7250 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
7252 tree a00 = TREE_OPERAND (arg0, 0);
7253 tree a01 = TREE_OPERAND (arg0, 1);
7254 tree a10 = TREE_OPERAND (arg1, 0);
7255 tree a11 = TREE_OPERAND (arg1, 1);
7256 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
7257 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
7258 && (code == TRUTH_AND_EXPR
7259 || code == TRUTH_OR_EXPR));
7261 if (operand_equal_p (a00, a10, 0))
7262 return fold (build (TREE_CODE (arg0), type, a00,
7263 fold (build (code, type, a01, a11))));
7264 else if (commutative && operand_equal_p (a00, a11, 0))
7265 return fold (build (TREE_CODE (arg0), type, a00,
7266 fold (build (code, type, a01, a10))));
7267 else if (commutative && operand_equal_p (a01, a10, 0))
7268 return fold (build (TREE_CODE (arg0), type, a01,
7269 fold (build (code, type, a00, a11))));
7271 /* This case if tricky because we must either have commutative
7272 operators or else A10 must not have side-effects. */
7274 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
7275 && operand_equal_p (a01, a11, 0))
7276 return fold (build (TREE_CODE (arg0), type,
7277 fold (build (code, type, a00, a10)),
7278 a01));
7281 /* See if we can build a range comparison. */
7282 if (0 != (tem = fold_range_test (t)))
7283 return tem;
7285 /* Check for the possibility of merging component references. If our
7286 lhs is another similar operation, try to merge its rhs with our
7287 rhs. Then try to merge our lhs and rhs. */
7288 if (TREE_CODE (arg0) == code
7289 && 0 != (tem = fold_truthop (code, type,
7290 TREE_OPERAND (arg0, 1), arg1)))
7291 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
7293 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
7294 return tem;
7296 return t;
7298 case TRUTH_ORIF_EXPR:
7299 /* Note that the operands of this must be ints
7300 and their values must be 0 or true.
7301 ("true" is a fixed value perhaps depending on the language.) */
7302 /* If first arg is constant true, return it. */
7303 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
7304 return fold_convert (type, arg0);
7305 case TRUTH_OR_EXPR:
7306 /* If either arg is constant zero, drop it. */
7307 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
7308 return non_lvalue (fold_convert (type, arg1));
7309 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
7310 /* Preserve sequence points. */
7311 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
7312 return non_lvalue (fold_convert (type, arg0));
7313 /* If second arg is constant true, result is true, but we must
7314 evaluate first arg. */
7315 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
7316 return omit_one_operand (type, arg1, arg0);
7317 /* Likewise for first arg, but note this only occurs here for
7318 TRUTH_OR_EXPR. */
7319 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
7320 return omit_one_operand (type, arg0, arg1);
7321 goto truth_andor;
7323 case TRUTH_XOR_EXPR:
7324 /* If either arg is constant zero, drop it. */
7325 if (integer_zerop (arg0))
7326 return non_lvalue (fold_convert (type, arg1));
7327 if (integer_zerop (arg1))
7328 return non_lvalue (fold_convert (type, arg0));
7329 /* If either arg is constant true, this is a logical inversion. */
7330 if (integer_onep (arg0))
7331 return non_lvalue (fold_convert (type, invert_truthvalue (arg1)));
7332 if (integer_onep (arg1))
7333 return non_lvalue (fold_convert (type, invert_truthvalue (arg0)));
7334 /* Identical arguments cancel to zero. */
7335 if (operand_equal_p (arg0, arg1, 0))
7336 return omit_one_operand (type, integer_zero_node, arg0);
7337 return t;
7339 case EQ_EXPR:
7340 case NE_EXPR:
7341 case LT_EXPR:
7342 case GT_EXPR:
7343 case LE_EXPR:
7344 case GE_EXPR:
7345 /* If one arg is a real or integer constant, put it last. */
7346 if (tree_swap_operands_p (arg0, arg1, true))
7347 return fold (build (swap_tree_comparison (code), type, arg1, arg0));
7349 /* If this is an equality comparison of the address of a non-weak
7350 object against zero, then we know the result. */
7351 if ((code == EQ_EXPR || code == NE_EXPR)
7352 && TREE_CODE (arg0) == ADDR_EXPR
7353 && DECL_P (TREE_OPERAND (arg0, 0))
7354 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
7355 && integer_zerop (arg1))
7357 if (code == EQ_EXPR)
7358 return fold_convert (type, integer_zero_node);
7359 else
7360 return fold_convert (type, integer_one_node);
7363 /* If this is an equality comparison of the address of two non-weak,
7364 unaliased symbols neither of which are extern (since we do not
7365 have access to attributes for externs), then we know the result. */
7366 if ((code == EQ_EXPR || code == NE_EXPR)
7367 && TREE_CODE (arg0) == ADDR_EXPR
7368 && DECL_P (TREE_OPERAND (arg0, 0))
7369 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
7370 && ! lookup_attribute ("alias",
7371 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
7372 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
7373 && TREE_CODE (arg1) == ADDR_EXPR
7374 && DECL_P (TREE_OPERAND (arg1, 0))
7375 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
7376 && ! lookup_attribute ("alias",
7377 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
7378 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
7380 if (code == EQ_EXPR)
7381 return fold_convert (type, (operand_equal_p (arg0, arg1, 0)
7382 ? integer_one_node : integer_zero_node));
7383 else
7384 return fold_convert (type, (operand_equal_p (arg0, arg1, 0)
7385 ? integer_zero_node : integer_one_node));
7388 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
7390 tree targ0 = strip_float_extensions (arg0);
7391 tree targ1 = strip_float_extensions (arg1);
7392 tree newtype = TREE_TYPE (targ0);
7394 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
7395 newtype = TREE_TYPE (targ1);
7397 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
7398 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
7399 return fold (build (code, type, fold_convert (newtype, targ0),
7400 fold_convert (newtype, targ1)));
7402 /* (-a) CMP (-b) -> b CMP a */
7403 if (TREE_CODE (arg0) == NEGATE_EXPR
7404 && TREE_CODE (arg1) == NEGATE_EXPR)
7405 return fold (build (code, type, TREE_OPERAND (arg1, 0),
7406 TREE_OPERAND (arg0, 0)));
7408 if (TREE_CODE (arg1) == REAL_CST)
7410 REAL_VALUE_TYPE cst;
7411 cst = TREE_REAL_CST (arg1);
7413 /* (-a) CMP CST -> a swap(CMP) (-CST) */
7414 if (TREE_CODE (arg0) == NEGATE_EXPR)
7415 return
7416 fold (build (swap_tree_comparison (code), type,
7417 TREE_OPERAND (arg0, 0),
7418 build_real (TREE_TYPE (arg1),
7419 REAL_VALUE_NEGATE (cst))));
7421 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
7422 /* a CMP (-0) -> a CMP 0 */
7423 if (REAL_VALUE_MINUS_ZERO (cst))
7424 return fold (build (code, type, arg0,
7425 build_real (TREE_TYPE (arg1), dconst0)));
7427 /* x != NaN is always true, other ops are always false. */
7428 if (REAL_VALUE_ISNAN (cst)
7429 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
7431 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
7432 return omit_one_operand (type, fold_convert (type, tem), arg0);
7435 /* Fold comparisons against infinity. */
7436 if (REAL_VALUE_ISINF (cst))
7438 tem = fold_inf_compare (code, type, arg0, arg1);
7439 if (tem != NULL_TREE)
7440 return tem;
7444 /* If this is a comparison of a real constant with a PLUS_EXPR
7445 or a MINUS_EXPR of a real constant, we can convert it into a
7446 comparison with a revised real constant as long as no overflow
7447 occurs when unsafe_math_optimizations are enabled. */
7448 if (flag_unsafe_math_optimizations
7449 && TREE_CODE (arg1) == REAL_CST
7450 && (TREE_CODE (arg0) == PLUS_EXPR
7451 || TREE_CODE (arg0) == MINUS_EXPR)
7452 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
7453 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
7454 ? MINUS_EXPR : PLUS_EXPR,
7455 arg1, TREE_OPERAND (arg0, 1), 0))
7456 && ! TREE_CONSTANT_OVERFLOW (tem))
7457 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
7459 /* Likewise, we can simplify a comparison of a real constant with
7460 a MINUS_EXPR whose first operand is also a real constant, i.e.
7461 (c1 - x) < c2 becomes x > c1-c2. */
7462 if (flag_unsafe_math_optimizations
7463 && TREE_CODE (arg1) == REAL_CST
7464 && TREE_CODE (arg0) == MINUS_EXPR
7465 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
7466 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
7467 arg1, 0))
7468 && ! TREE_CONSTANT_OVERFLOW (tem))
7469 return fold (build (swap_tree_comparison (code), type,
7470 TREE_OPERAND (arg0, 1), tem));
7472 /* Fold comparisons against built-in math functions. */
7473 if (TREE_CODE (arg1) == REAL_CST
7474 && flag_unsafe_math_optimizations
7475 && ! flag_errno_math)
7477 enum built_in_function fcode = builtin_mathfn_code (arg0);
7479 if (fcode != END_BUILTINS)
7481 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
7482 if (tem != NULL_TREE)
7483 return tem;
7488 /* Convert foo++ == CONST into ++foo == CONST + INCR. */
7489 if (TREE_CONSTANT (arg1)
7490 && (TREE_CODE (arg0) == POSTINCREMENT_EXPR
7491 || TREE_CODE (arg0) == POSTDECREMENT_EXPR)
7492 /* This optimization is invalid for ordered comparisons
7493 if CONST+INCR overflows or if foo+incr might overflow.
7494 This optimization is invalid for floating point due to rounding.
7495 For pointer types we assume overflow doesn't happen. */
7496 && (POINTER_TYPE_P (TREE_TYPE (arg0))
7497 || (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
7498 && (code == EQ_EXPR || code == NE_EXPR))))
7500 tree varop, newconst;
7502 if (TREE_CODE (arg0) == POSTINCREMENT_EXPR)
7504 newconst = fold (build2 (PLUS_EXPR, TREE_TYPE (arg0),
7505 arg1, TREE_OPERAND (arg0, 1)));
7506 varop = build2 (PREINCREMENT_EXPR, TREE_TYPE (arg0),
7507 TREE_OPERAND (arg0, 0),
7508 TREE_OPERAND (arg0, 1));
7510 else
7512 newconst = fold (build2 (MINUS_EXPR, TREE_TYPE (arg0),
7513 arg1, TREE_OPERAND (arg0, 1)));
7514 varop = build2 (PREDECREMENT_EXPR, TREE_TYPE (arg0),
7515 TREE_OPERAND (arg0, 0),
7516 TREE_OPERAND (arg0, 1));
7520 /* If VAROP is a reference to a bitfield, we must mask
7521 the constant by the width of the field. */
7522 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
7523 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (varop, 0), 1)))
7525 tree fielddecl = TREE_OPERAND (TREE_OPERAND (varop, 0), 1);
7526 int size = TREE_INT_CST_LOW (DECL_SIZE (fielddecl));
7527 tree folded_compare, shift;
7529 /* First check whether the comparison would come out
7530 always the same. If we don't do that we would
7531 change the meaning with the masking. */
7532 folded_compare = fold (build2 (code, type,
7533 TREE_OPERAND (varop, 0),
7534 arg1));
7535 if (integer_zerop (folded_compare)
7536 || integer_onep (folded_compare))
7537 return omit_one_operand (type, folded_compare, varop);
7539 shift = build_int_2 (TYPE_PRECISION (TREE_TYPE (varop)) - size,
7541 newconst = fold (build2 (LSHIFT_EXPR, TREE_TYPE (varop),
7542 newconst, shift));
7543 newconst = fold (build2 (RSHIFT_EXPR, TREE_TYPE (varop),
7544 newconst, shift));
7547 return fold (build2 (code, type, varop, newconst));
7550 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
7551 This transformation affects the cases which are handled in later
7552 optimizations involving comparisons with non-negative constants. */
7553 if (TREE_CODE (arg1) == INTEGER_CST
7554 && TREE_CODE (arg0) != INTEGER_CST
7555 && tree_int_cst_sgn (arg1) > 0)
7557 switch (code)
7559 case GE_EXPR:
7560 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7561 return fold (build (GT_EXPR, type, arg0, arg1));
7563 case LT_EXPR:
7564 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7565 return fold (build (LE_EXPR, type, arg0, arg1));
7567 default:
7568 break;
7572 /* Comparisons with the highest or lowest possible integer of
7573 the specified size will have known values.
7575 This is quite similar to fold_relational_hi_lo; however, my
7576 attempts to share the code have been nothing but trouble.
7577 I give up for now. */
7579 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
7581 if (TREE_CODE (arg1) == INTEGER_CST
7582 && ! TREE_CONSTANT_OVERFLOW (arg1)
7583 && width <= HOST_BITS_PER_WIDE_INT
7584 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
7585 || POINTER_TYPE_P (TREE_TYPE (arg1))))
7587 unsigned HOST_WIDE_INT signed_max;
7588 unsigned HOST_WIDE_INT max, min;
7590 signed_max = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1;
7592 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
7594 max = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
7595 min = 0;
7597 else
7599 max = signed_max;
7600 min = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
7603 if (TREE_INT_CST_HIGH (arg1) == 0
7604 && TREE_INT_CST_LOW (arg1) == max)
7605 switch (code)
7607 case GT_EXPR:
7608 return omit_one_operand (type,
7609 fold_convert (type,
7610 integer_zero_node),
7611 arg0);
7612 case GE_EXPR:
7613 return fold (build (EQ_EXPR, type, arg0, arg1));
7615 case LE_EXPR:
7616 return omit_one_operand (type,
7617 fold_convert (type,
7618 integer_one_node),
7619 arg0);
7620 case LT_EXPR:
7621 return fold (build (NE_EXPR, type, arg0, arg1));
7623 /* The GE_EXPR and LT_EXPR cases above are not normally
7624 reached because of previous transformations. */
7626 default:
7627 break;
7629 else if (TREE_INT_CST_HIGH (arg1) == 0
7630 && TREE_INT_CST_LOW (arg1) == max - 1)
7631 switch (code)
7633 case GT_EXPR:
7634 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
7635 return fold (build (EQ_EXPR, type, arg0, arg1));
7636 case LE_EXPR:
7637 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
7638 return fold (build (NE_EXPR, type, arg0, arg1));
7639 default:
7640 break;
7642 else if (TREE_INT_CST_HIGH (arg1) == (min ? -1 : 0)
7643 && TREE_INT_CST_LOW (arg1) == min)
7644 switch (code)
7646 case LT_EXPR:
7647 return omit_one_operand (type,
7648 fold_convert (type,
7649 integer_zero_node),
7650 arg0);
7651 case LE_EXPR:
7652 return fold (build (EQ_EXPR, type, arg0, arg1));
7654 case GE_EXPR:
7655 return omit_one_operand (type,
7656 fold_convert (type,
7657 integer_one_node),
7658 arg0);
7659 case GT_EXPR:
7660 return fold (build (NE_EXPR, type, arg0, arg1));
7662 default:
7663 break;
7665 else if (TREE_INT_CST_HIGH (arg1) == (min ? -1 : 0)
7666 && TREE_INT_CST_LOW (arg1) == min + 1)
7667 switch (code)
7669 case GE_EXPR:
7670 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7671 return fold (build (NE_EXPR, type, arg0, arg1));
7672 case LT_EXPR:
7673 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7674 return fold (build (EQ_EXPR, type, arg0, arg1));
7675 default:
7676 break;
7679 else if (!in_gimple_form
7680 && TREE_INT_CST_HIGH (arg1) == 0
7681 && TREE_INT_CST_LOW (arg1) == signed_max
7682 && TYPE_UNSIGNED (TREE_TYPE (arg1))
7683 /* signed_type does not work on pointer types. */
7684 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
7686 /* The following case also applies to X < signed_max+1
7687 and X >= signed_max+1 because previous transformations. */
7688 if (code == LE_EXPR || code == GT_EXPR)
7690 tree st0, st1;
7691 st0 = lang_hooks.types.signed_type (TREE_TYPE (arg0));
7692 st1 = lang_hooks.types.signed_type (TREE_TYPE (arg1));
7693 return fold
7694 (build (code == LE_EXPR ? GE_EXPR: LT_EXPR,
7695 type, fold_convert (st0, arg0),
7696 fold_convert (st1, integer_zero_node)));
7702 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
7703 a MINUS_EXPR of a constant, we can convert it into a comparison with
7704 a revised constant as long as no overflow occurs. */
7705 if ((code == EQ_EXPR || code == NE_EXPR)
7706 && TREE_CODE (arg1) == INTEGER_CST
7707 && (TREE_CODE (arg0) == PLUS_EXPR
7708 || TREE_CODE (arg0) == MINUS_EXPR)
7709 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7710 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
7711 ? MINUS_EXPR : PLUS_EXPR,
7712 arg1, TREE_OPERAND (arg0, 1), 0))
7713 && ! TREE_CONSTANT_OVERFLOW (tem))
7714 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
7716 /* Similarly for a NEGATE_EXPR. */
7717 else if ((code == EQ_EXPR || code == NE_EXPR)
7718 && TREE_CODE (arg0) == NEGATE_EXPR
7719 && TREE_CODE (arg1) == INTEGER_CST
7720 && 0 != (tem = negate_expr (arg1))
7721 && TREE_CODE (tem) == INTEGER_CST
7722 && ! TREE_CONSTANT_OVERFLOW (tem))
7723 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
7725 /* If we have X - Y == 0, we can convert that to X == Y and similarly
7726 for !=. Don't do this for ordered comparisons due to overflow. */
7727 else if ((code == NE_EXPR || code == EQ_EXPR)
7728 && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
7729 return fold (build (code, type,
7730 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
7732 /* If we are widening one operand of an integer comparison,
7733 see if the other operand is similarly being widened. Perhaps we
7734 can do the comparison in the narrower type. */
7735 else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
7736 && TREE_CODE (arg0) == NOP_EXPR
7737 && (tem = get_unwidened (arg0, NULL_TREE)) != arg0
7738 && (code == EQ_EXPR || code == NE_EXPR
7739 || TYPE_UNSIGNED (TREE_TYPE (arg0))
7740 == TYPE_UNSIGNED (TREE_TYPE (tem)))
7741 && (t1 = get_unwidened (arg1, TREE_TYPE (tem))) != 0
7742 && (TREE_TYPE (t1) == TREE_TYPE (tem)
7743 || (TREE_CODE (t1) == INTEGER_CST
7744 && int_fits_type_p (t1, TREE_TYPE (tem)))))
7745 return fold (build (code, type, tem,
7746 fold_convert (TREE_TYPE (tem), t1)));
7748 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
7749 constant, we can simplify it. */
7750 else if (TREE_CODE (arg1) == INTEGER_CST
7751 && (TREE_CODE (arg0) == MIN_EXPR
7752 || TREE_CODE (arg0) == MAX_EXPR)
7753 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7754 return optimize_minmax_comparison (t);
7756 /* If we are comparing an ABS_EXPR with a constant, we can
7757 convert all the cases into explicit comparisons, but they may
7758 well not be faster than doing the ABS and one comparison.
7759 But ABS (X) <= C is a range comparison, which becomes a subtraction
7760 and a comparison, and is probably faster. */
7761 else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
7762 && TREE_CODE (arg0) == ABS_EXPR
7763 && ! TREE_SIDE_EFFECTS (arg0)
7764 && (0 != (tem = negate_expr (arg1)))
7765 && TREE_CODE (tem) == INTEGER_CST
7766 && ! TREE_CONSTANT_OVERFLOW (tem))
7767 return fold (build (TRUTH_ANDIF_EXPR, type,
7768 build (GE_EXPR, type, TREE_OPERAND (arg0, 0), tem),
7769 build (LE_EXPR, type,
7770 TREE_OPERAND (arg0, 0), arg1)));
7772 /* If this is an EQ or NE comparison with zero and ARG0 is
7773 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
7774 two operations, but the latter can be done in one less insn
7775 on machines that have only two-operand insns or on which a
7776 constant cannot be the first operand. */
7777 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
7778 && TREE_CODE (arg0) == BIT_AND_EXPR)
7780 if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
7781 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
7782 return
7783 fold (build (code, type,
7784 build (BIT_AND_EXPR, TREE_TYPE (arg0),
7785 build (RSHIFT_EXPR,
7786 TREE_TYPE (TREE_OPERAND (arg0, 0)),
7787 TREE_OPERAND (arg0, 1),
7788 TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
7789 fold_convert (TREE_TYPE (arg0),
7790 integer_one_node)),
7791 arg1));
7792 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
7793 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
7794 return
7795 fold (build (code, type,
7796 build (BIT_AND_EXPR, TREE_TYPE (arg0),
7797 build (RSHIFT_EXPR,
7798 TREE_TYPE (TREE_OPERAND (arg0, 1)),
7799 TREE_OPERAND (arg0, 0),
7800 TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
7801 fold_convert (TREE_TYPE (arg0),
7802 integer_one_node)),
7803 arg1));
7806 /* If this is an NE or EQ comparison of zero against the result of a
7807 signed MOD operation whose second operand is a power of 2, make
7808 the MOD operation unsigned since it is simpler and equivalent. */
7809 if ((code == NE_EXPR || code == EQ_EXPR)
7810 && integer_zerop (arg1)
7811 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
7812 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
7813 || TREE_CODE (arg0) == CEIL_MOD_EXPR
7814 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
7815 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
7816 && integer_pow2p (TREE_OPERAND (arg0, 1)))
7818 tree newtype = lang_hooks.types.unsigned_type (TREE_TYPE (arg0));
7819 tree newmod = build (TREE_CODE (arg0), newtype,
7820 fold_convert (newtype,
7821 TREE_OPERAND (arg0, 0)),
7822 fold_convert (newtype,
7823 TREE_OPERAND (arg0, 1)));
7825 return build (code, type, newmod, fold_convert (newtype, arg1));
7828 /* If this is an NE comparison of zero with an AND of one, remove the
7829 comparison since the AND will give the correct value. */
7830 if (code == NE_EXPR && integer_zerop (arg1)
7831 && TREE_CODE (arg0) == BIT_AND_EXPR
7832 && integer_onep (TREE_OPERAND (arg0, 1)))
7833 return fold_convert (type, arg0);
7835 /* If we have (A & C) == C where C is a power of 2, convert this into
7836 (A & C) != 0. Similarly for NE_EXPR. */
7837 if ((code == EQ_EXPR || code == NE_EXPR)
7838 && TREE_CODE (arg0) == BIT_AND_EXPR
7839 && integer_pow2p (TREE_OPERAND (arg0, 1))
7840 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
7841 return fold (build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
7842 arg0, integer_zero_node));
7844 /* If we have (A & C) != 0 or (A & C) == 0 and C is a power of
7845 2, then fold the expression into shifts and logical operations. */
7846 tem = fold_single_bit_test (code, arg0, arg1, type);
7847 if (tem)
7848 return tem;
7850 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
7851 Similarly for NE_EXPR. */
7852 if ((code == EQ_EXPR || code == NE_EXPR)
7853 && TREE_CODE (arg0) == BIT_AND_EXPR
7854 && TREE_CODE (arg1) == INTEGER_CST
7855 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7857 tree dandnotc
7858 = fold (build (BIT_AND_EXPR, TREE_TYPE (arg0),
7859 arg1, build1 (BIT_NOT_EXPR,
7860 TREE_TYPE (TREE_OPERAND (arg0, 1)),
7861 TREE_OPERAND (arg0, 1))));
7862 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
7863 if (integer_nonzerop (dandnotc))
7864 return omit_one_operand (type, rslt, arg0);
7867 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
7868 Similarly for NE_EXPR. */
7869 if ((code == EQ_EXPR || code == NE_EXPR)
7870 && TREE_CODE (arg0) == BIT_IOR_EXPR
7871 && TREE_CODE (arg1) == INTEGER_CST
7872 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7874 tree candnotd
7875 = fold (build (BIT_AND_EXPR, TREE_TYPE (arg0),
7876 TREE_OPERAND (arg0, 1),
7877 build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1)));
7878 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
7879 if (integer_nonzerop (candnotd))
7880 return omit_one_operand (type, rslt, arg0);
7883 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
7884 and similarly for >= into !=. */
7885 if ((code == LT_EXPR || code == GE_EXPR)
7886 && TYPE_UNSIGNED (TREE_TYPE (arg0))
7887 && TREE_CODE (arg1) == LSHIFT_EXPR
7888 && integer_onep (TREE_OPERAND (arg1, 0)))
7889 return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
7890 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
7891 TREE_OPERAND (arg1, 1)),
7892 fold_convert (TREE_TYPE (arg0), integer_zero_node));
7894 else if ((code == LT_EXPR || code == GE_EXPR)
7895 && TYPE_UNSIGNED (TREE_TYPE (arg0))
7896 && (TREE_CODE (arg1) == NOP_EXPR
7897 || TREE_CODE (arg1) == CONVERT_EXPR)
7898 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
7899 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
7900 return
7901 build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
7902 fold_convert (TREE_TYPE (arg0),
7903 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
7904 TREE_OPERAND (TREE_OPERAND (arg1, 0),
7905 1))),
7906 fold_convert (TREE_TYPE (arg0), integer_zero_node));
7908 /* Simplify comparison of something with itself. (For IEEE
7909 floating-point, we can only do some of these simplifications.) */
7910 if (operand_equal_p (arg0, arg1, 0))
7912 switch (code)
7914 case EQ_EXPR:
7915 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7916 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7917 return constant_boolean_node (1, type);
7918 break;
7920 case GE_EXPR:
7921 case LE_EXPR:
7922 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7923 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7924 return constant_boolean_node (1, type);
7925 return fold (build (EQ_EXPR, type, arg0, arg1));
7927 case NE_EXPR:
7928 /* For NE, we can only do this simplification if integer
7929 or we don't honor IEEE floating point NaNs. */
7930 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
7931 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7932 break;
7933 /* ... fall through ... */
7934 case GT_EXPR:
7935 case LT_EXPR:
7936 return constant_boolean_node (0, type);
7937 default:
7938 abort ();
7942 /* If we are comparing an expression that just has comparisons
7943 of two integer values, arithmetic expressions of those comparisons,
7944 and constants, we can simplify it. There are only three cases
7945 to check: the two values can either be equal, the first can be
7946 greater, or the second can be greater. Fold the expression for
7947 those three values. Since each value must be 0 or 1, we have
7948 eight possibilities, each of which corresponds to the constant 0
7949 or 1 or one of the six possible comparisons.
7951 This handles common cases like (a > b) == 0 but also handles
7952 expressions like ((x > y) - (y > x)) > 0, which supposedly
7953 occur in macroized code. */
7955 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
7957 tree cval1 = 0, cval2 = 0;
7958 int save_p = 0;
7960 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
7961 /* Don't handle degenerate cases here; they should already
7962 have been handled anyway. */
7963 && cval1 != 0 && cval2 != 0
7964 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
7965 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
7966 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
7967 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
7968 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
7969 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
7970 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
7972 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
7973 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
7975 /* We can't just pass T to eval_subst in case cval1 or cval2
7976 was the same as ARG1. */
7978 tree high_result
7979 = fold (build (code, type,
7980 eval_subst (arg0, cval1, maxval, cval2, minval),
7981 arg1));
7982 tree equal_result
7983 = fold (build (code, type,
7984 eval_subst (arg0, cval1, maxval, cval2, maxval),
7985 arg1));
7986 tree low_result
7987 = fold (build (code, type,
7988 eval_subst (arg0, cval1, minval, cval2, maxval),
7989 arg1));
7991 /* All three of these results should be 0 or 1. Confirm they
7992 are. Then use those values to select the proper code
7993 to use. */
7995 if ((integer_zerop (high_result)
7996 || integer_onep (high_result))
7997 && (integer_zerop (equal_result)
7998 || integer_onep (equal_result))
7999 && (integer_zerop (low_result)
8000 || integer_onep (low_result)))
8002 /* Make a 3-bit mask with the high-order bit being the
8003 value for `>', the next for '=', and the low for '<'. */
8004 switch ((integer_onep (high_result) * 4)
8005 + (integer_onep (equal_result) * 2)
8006 + integer_onep (low_result))
8008 case 0:
8009 /* Always false. */
8010 return omit_one_operand (type, integer_zero_node, arg0);
8011 case 1:
8012 code = LT_EXPR;
8013 break;
8014 case 2:
8015 code = EQ_EXPR;
8016 break;
8017 case 3:
8018 code = LE_EXPR;
8019 break;
8020 case 4:
8021 code = GT_EXPR;
8022 break;
8023 case 5:
8024 code = NE_EXPR;
8025 break;
8026 case 6:
8027 code = GE_EXPR;
8028 break;
8029 case 7:
8030 /* Always true. */
8031 return omit_one_operand (type, integer_one_node, arg0);
8034 tem = build (code, type, cval1, cval2);
8035 if (save_p)
8036 return save_expr (tem);
8037 else
8038 return fold (tem);
8043 /* If this is a comparison of a field, we may be able to simplify it. */
8044 if (((TREE_CODE (arg0) == COMPONENT_REF
8045 && lang_hooks.can_use_bit_fields_p ())
8046 || TREE_CODE (arg0) == BIT_FIELD_REF)
8047 && (code == EQ_EXPR || code == NE_EXPR)
8048 /* Handle the constant case even without -O
8049 to make sure the warnings are given. */
8050 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
8052 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
8053 if (t1)
8054 return t1;
8057 /* If this is a comparison of complex values and either or both sides
8058 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
8059 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
8060 This may prevent needless evaluations. */
8061 if ((code == EQ_EXPR || code == NE_EXPR)
8062 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
8063 && (TREE_CODE (arg0) == COMPLEX_EXPR
8064 || TREE_CODE (arg1) == COMPLEX_EXPR
8065 || TREE_CODE (arg0) == COMPLEX_CST
8066 || TREE_CODE (arg1) == COMPLEX_CST))
8068 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
8069 tree real0, imag0, real1, imag1;
8071 arg0 = save_expr (arg0);
8072 arg1 = save_expr (arg1);
8073 real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
8074 imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
8075 real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
8076 imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
8078 return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
8079 : TRUTH_ORIF_EXPR),
8080 type,
8081 fold (build (code, type, real0, real1)),
8082 fold (build (code, type, imag0, imag1))));
8085 /* Optimize comparisons of strlen vs zero to a compare of the
8086 first character of the string vs zero. To wit,
8087 strlen(ptr) == 0 => *ptr == 0
8088 strlen(ptr) != 0 => *ptr != 0
8089 Other cases should reduce to one of these two (or a constant)
8090 due to the return value of strlen being unsigned. */
8091 if ((code == EQ_EXPR || code == NE_EXPR)
8092 && integer_zerop (arg1)
8093 && TREE_CODE (arg0) == CALL_EXPR)
8095 tree fndecl = get_callee_fndecl (arg0);
8096 tree arglist;
8098 if (fndecl
8099 && DECL_BUILT_IN (fndecl)
8100 && DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD
8101 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
8102 && (arglist = TREE_OPERAND (arg0, 1))
8103 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
8104 && ! TREE_CHAIN (arglist))
8105 return fold (build (code, type,
8106 build1 (INDIRECT_REF, char_type_node,
8107 TREE_VALUE(arglist)),
8108 integer_zero_node));
8111 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8112 into a single range test. */
8113 if (TREE_CODE (arg0) == TRUNC_DIV_EXPR
8114 && TREE_CODE (arg1) == INTEGER_CST
8115 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8116 && !integer_zerop (TREE_OPERAND (arg0, 1))
8117 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8118 && !TREE_OVERFLOW (arg1))
8120 t1 = fold_div_compare (code, type, arg0, arg1);
8121 if (t1 != NULL_TREE)
8122 return t1;
8125 /* Both ARG0 and ARG1 are known to be constants at this point. */
8126 t1 = fold_relational_const (code, type, arg0, arg1);
8127 return (t1 == NULL_TREE ? t : t1);
8129 case COND_EXPR:
8130 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
8131 so all simple results must be passed through pedantic_non_lvalue. */
8132 if (TREE_CODE (arg0) == INTEGER_CST)
8134 tem = TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1));
8135 /* Only optimize constant conditions when the selected branch
8136 has the same type as the COND_EXPR. This avoids optimizing
8137 away "c ? x : throw", where the throw has a void type. */
8138 if (! VOID_TYPE_P (TREE_TYPE (tem))
8139 || VOID_TYPE_P (type))
8140 return pedantic_non_lvalue (tem);
8141 return t;
8143 if (operand_equal_p (arg1, TREE_OPERAND (t, 2), 0))
8144 return pedantic_omit_one_operand (type, arg1, arg0);
8146 /* If we have A op B ? A : C, we may be able to convert this to a
8147 simpler expression, depending on the operation and the values
8148 of B and C. Signed zeros prevent all of these transformations,
8149 for reasons given above each one. */
8151 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
8152 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
8153 arg1, TREE_OPERAND (arg0, 1))
8154 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
8156 tree arg2 = TREE_OPERAND (t, 2);
8157 enum tree_code comp_code = TREE_CODE (arg0);
8159 STRIP_NOPS (arg2);
8161 /* If we have A op 0 ? A : -A, consider applying the following
8162 transformations:
8164 A == 0? A : -A same as -A
8165 A != 0? A : -A same as A
8166 A >= 0? A : -A same as abs (A)
8167 A > 0? A : -A same as abs (A)
8168 A <= 0? A : -A same as -abs (A)
8169 A < 0? A : -A same as -abs (A)
8171 None of these transformations work for modes with signed
8172 zeros. If A is +/-0, the first two transformations will
8173 change the sign of the result (from +0 to -0, or vice
8174 versa). The last four will fix the sign of the result,
8175 even though the original expressions could be positive or
8176 negative, depending on the sign of A.
8178 Note that all these transformations are correct if A is
8179 NaN, since the two alternatives (A and -A) are also NaNs. */
8180 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
8181 ? real_zerop (TREE_OPERAND (arg0, 1))
8182 : integer_zerop (TREE_OPERAND (arg0, 1)))
8183 && TREE_CODE (arg2) == NEGATE_EXPR
8184 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
8185 switch (comp_code)
8187 case EQ_EXPR:
8188 tem = fold_convert (TREE_TYPE (TREE_OPERAND (t, 1)), arg1);
8189 tem = fold_convert (type, negate_expr (tem));
8190 return pedantic_non_lvalue (tem);
8191 case NE_EXPR:
8192 return pedantic_non_lvalue (fold_convert (type, arg1));
8193 case GE_EXPR:
8194 case GT_EXPR:
8195 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
8196 arg1 = fold_convert (lang_hooks.types.signed_type
8197 (TREE_TYPE (arg1)), arg1);
8198 arg1 = fold (build1 (ABS_EXPR, TREE_TYPE (arg1), arg1));
8199 return pedantic_non_lvalue (fold_convert (type, arg1));
8200 case LE_EXPR:
8201 case LT_EXPR:
8202 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
8203 arg1 = fold_convert (lang_hooks.types.signed_type
8204 (TREE_TYPE (arg1)), arg1);
8205 arg1 = fold (build1 (ABS_EXPR, TREE_TYPE (arg1), arg1));
8206 arg1 = negate_expr (fold_convert (type, arg1));
8207 return pedantic_non_lvalue (arg1);
8208 default:
8209 abort ();
8212 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
8213 A == 0 ? A : 0 is always 0 unless A is -0. Note that
8214 both transformations are correct when A is NaN: A != 0
8215 is then true, and A == 0 is false. */
8217 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
8219 if (comp_code == NE_EXPR)
8220 return pedantic_non_lvalue (fold_convert (type, arg1));
8221 else if (comp_code == EQ_EXPR)
8222 return pedantic_non_lvalue (fold_convert (type, integer_zero_node));
8225 /* Try some transformations of A op B ? A : B.
8227 A == B? A : B same as B
8228 A != B? A : B same as A
8229 A >= B? A : B same as max (A, B)
8230 A > B? A : B same as max (B, A)
8231 A <= B? A : B same as min (A, B)
8232 A < B? A : B same as min (B, A)
8234 As above, these transformations don't work in the presence
8235 of signed zeros. For example, if A and B are zeros of
8236 opposite sign, the first two transformations will change
8237 the sign of the result. In the last four, the original
8238 expressions give different results for (A=+0, B=-0) and
8239 (A=-0, B=+0), but the transformed expressions do not.
8241 The first two transformations are correct if either A or B
8242 is a NaN. In the first transformation, the condition will
8243 be false, and B will indeed be chosen. In the case of the
8244 second transformation, the condition A != B will be true,
8245 and A will be chosen.
8247 The conversions to max() and min() are not correct if B is
8248 a number and A is not. The conditions in the original
8249 expressions will be false, so all four give B. The min()
8250 and max() versions would give a NaN instead. */
8251 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
8252 arg2, TREE_OPERAND (arg0, 0)))
8254 tree comp_op0 = TREE_OPERAND (arg0, 0);
8255 tree comp_op1 = TREE_OPERAND (arg0, 1);
8256 tree comp_type = TREE_TYPE (comp_op0);
8258 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
8259 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
8261 comp_type = type;
8262 comp_op0 = arg1;
8263 comp_op1 = arg2;
8266 switch (comp_code)
8268 case EQ_EXPR:
8269 return pedantic_non_lvalue (fold_convert (type, arg2));
8270 case NE_EXPR:
8271 return pedantic_non_lvalue (fold_convert (type, arg1));
8272 case LE_EXPR:
8273 case LT_EXPR:
8274 /* In C++ a ?: expression can be an lvalue, so put the
8275 operand which will be used if they are equal first
8276 so that we can convert this back to the
8277 corresponding COND_EXPR. */
8278 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
8279 return pedantic_non_lvalue (fold_convert
8280 (type, fold (build (MIN_EXPR, comp_type,
8281 (comp_code == LE_EXPR
8282 ? comp_op0 : comp_op1),
8283 (comp_code == LE_EXPR
8284 ? comp_op1 : comp_op0)))));
8285 break;
8286 case GE_EXPR:
8287 case GT_EXPR:
8288 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
8289 return pedantic_non_lvalue (fold_convert
8290 (type, fold (build (MAX_EXPR, comp_type,
8291 (comp_code == GE_EXPR
8292 ? comp_op0 : comp_op1),
8293 (comp_code == GE_EXPR
8294 ? comp_op1 : comp_op0)))));
8295 break;
8296 default:
8297 abort ();
8301 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
8302 we might still be able to simplify this. For example,
8303 if C1 is one less or one more than C2, this might have started
8304 out as a MIN or MAX and been transformed by this function.
8305 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
8307 if (INTEGRAL_TYPE_P (type)
8308 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8309 && TREE_CODE (arg2) == INTEGER_CST)
8310 switch (comp_code)
8312 case EQ_EXPR:
8313 /* We can replace A with C1 in this case. */
8314 arg1 = fold_convert (type, TREE_OPERAND (arg0, 1));
8315 return fold (build (code, type, TREE_OPERAND (t, 0), arg1,
8316 TREE_OPERAND (t, 2)));
8318 case LT_EXPR:
8319 /* If C1 is C2 + 1, this is min(A, C2). */
8320 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
8321 OEP_ONLY_CONST)
8322 && operand_equal_p (TREE_OPERAND (arg0, 1),
8323 const_binop (PLUS_EXPR, arg2,
8324 integer_one_node, 0),
8325 OEP_ONLY_CONST))
8326 return pedantic_non_lvalue
8327 (fold (build (MIN_EXPR, type, arg1, arg2)));
8328 break;
8330 case LE_EXPR:
8331 /* If C1 is C2 - 1, this is min(A, C2). */
8332 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
8333 OEP_ONLY_CONST)
8334 && operand_equal_p (TREE_OPERAND (arg0, 1),
8335 const_binop (MINUS_EXPR, arg2,
8336 integer_one_node, 0),
8337 OEP_ONLY_CONST))
8338 return pedantic_non_lvalue
8339 (fold (build (MIN_EXPR, type, arg1, arg2)));
8340 break;
8342 case GT_EXPR:
8343 /* If C1 is C2 - 1, this is max(A, C2). */
8344 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
8345 OEP_ONLY_CONST)
8346 && operand_equal_p (TREE_OPERAND (arg0, 1),
8347 const_binop (MINUS_EXPR, arg2,
8348 integer_one_node, 0),
8349 OEP_ONLY_CONST))
8350 return pedantic_non_lvalue
8351 (fold (build (MAX_EXPR, type, arg1, arg2)));
8352 break;
8354 case GE_EXPR:
8355 /* If C1 is C2 + 1, this is max(A, C2). */
8356 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
8357 OEP_ONLY_CONST)
8358 && operand_equal_p (TREE_OPERAND (arg0, 1),
8359 const_binop (PLUS_EXPR, arg2,
8360 integer_one_node, 0),
8361 OEP_ONLY_CONST))
8362 return pedantic_non_lvalue
8363 (fold (build (MAX_EXPR, type, arg1, arg2)));
8364 break;
8365 case NE_EXPR:
8366 break;
8367 default:
8368 abort ();
8372 /* If the second operand is simpler than the third, swap them
8373 since that produces better jump optimization results. */
8374 if (tree_swap_operands_p (TREE_OPERAND (t, 1),
8375 TREE_OPERAND (t, 2), false))
8377 /* See if this can be inverted. If it can't, possibly because
8378 it was a floating-point inequality comparison, don't do
8379 anything. */
8380 tem = invert_truthvalue (arg0);
8382 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
8383 return fold (build (code, type, tem,
8384 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1)));
8387 /* Convert A ? 1 : 0 to simply A. */
8388 if (integer_onep (TREE_OPERAND (t, 1))
8389 && integer_zerop (TREE_OPERAND (t, 2))
8390 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
8391 call to fold will try to move the conversion inside
8392 a COND, which will recurse. In that case, the COND_EXPR
8393 is probably the best choice, so leave it alone. */
8394 && type == TREE_TYPE (arg0))
8395 return pedantic_non_lvalue (arg0);
8397 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
8398 over COND_EXPR in cases such as floating point comparisons. */
8399 if (integer_zerop (TREE_OPERAND (t, 1))
8400 && integer_onep (TREE_OPERAND (t, 2))
8401 && truth_value_p (TREE_CODE (arg0)))
8402 return pedantic_non_lvalue (fold_convert (type,
8403 invert_truthvalue (arg0)));
8405 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
8406 operation is simply A & 2. */
8408 if (integer_zerop (TREE_OPERAND (t, 2))
8409 && TREE_CODE (arg0) == NE_EXPR
8410 && integer_zerop (TREE_OPERAND (arg0, 1))
8411 && integer_pow2p (arg1)
8412 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
8413 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
8414 arg1, OEP_ONLY_CONST))
8415 return pedantic_non_lvalue (fold_convert (type,
8416 TREE_OPERAND (arg0, 0)));
8418 /* Convert A ? B : 0 into A && B if A and B are truth values. */
8419 if (integer_zerop (TREE_OPERAND (t, 2))
8420 && truth_value_p (TREE_CODE (arg0))
8421 && truth_value_p (TREE_CODE (arg1)))
8422 return pedantic_non_lvalue (fold (build (TRUTH_ANDIF_EXPR, type,
8423 arg0, arg1)));
8425 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
8426 if (integer_onep (TREE_OPERAND (t, 2))
8427 && truth_value_p (TREE_CODE (arg0))
8428 && truth_value_p (TREE_CODE (arg1)))
8430 /* Only perform transformation if ARG0 is easily inverted. */
8431 tem = invert_truthvalue (arg0);
8432 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
8433 return pedantic_non_lvalue (fold (build (TRUTH_ORIF_EXPR, type,
8434 tem, arg1)));
8437 return t;
8439 case COMPOUND_EXPR:
8440 /* When pedantic, a compound expression can be neither an lvalue
8441 nor an integer constant expression. */
8442 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
8443 return t;
8444 /* Don't let (0, 0) be null pointer constant. */
8445 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
8446 : fold_convert (type, arg1);
8447 return pedantic_non_lvalue (tem);
8449 case COMPLEX_EXPR:
8450 if (wins)
8451 return build_complex (type, arg0, arg1);
8452 return t;
8454 case REALPART_EXPR:
8455 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8456 return t;
8457 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
8458 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8459 TREE_OPERAND (arg0, 1));
8460 else if (TREE_CODE (arg0) == COMPLEX_CST)
8461 return TREE_REALPART (arg0);
8462 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8463 return fold (build (TREE_CODE (arg0), type,
8464 fold (build1 (REALPART_EXPR, type,
8465 TREE_OPERAND (arg0, 0))),
8466 fold (build1 (REALPART_EXPR,
8467 type, TREE_OPERAND (arg0, 1)))));
8468 return t;
8470 case IMAGPART_EXPR:
8471 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8472 return fold_convert (type, integer_zero_node);
8473 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
8474 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8475 TREE_OPERAND (arg0, 0));
8476 else if (TREE_CODE (arg0) == COMPLEX_CST)
8477 return TREE_IMAGPART (arg0);
8478 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8479 return fold (build (TREE_CODE (arg0), type,
8480 fold (build1 (IMAGPART_EXPR, type,
8481 TREE_OPERAND (arg0, 0))),
8482 fold (build1 (IMAGPART_EXPR, type,
8483 TREE_OPERAND (arg0, 1)))));
8484 return t;
8486 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
8487 appropriate. */
8488 case CLEANUP_POINT_EXPR:
8489 if (! has_cleanups (arg0))
8490 return TREE_OPERAND (t, 0);
8493 enum tree_code code0 = TREE_CODE (arg0);
8494 int kind0 = TREE_CODE_CLASS (code0);
8495 tree arg00 = TREE_OPERAND (arg0, 0);
8496 tree arg01;
8498 if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
8499 return fold (build1 (code0, type,
8500 fold (build1 (CLEANUP_POINT_EXPR,
8501 TREE_TYPE (arg00), arg00))));
8503 if (kind0 == '<' || kind0 == '2'
8504 || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
8505 || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR
8506 || code0 == TRUTH_XOR_EXPR)
8508 arg01 = TREE_OPERAND (arg0, 1);
8510 if (TREE_CONSTANT (arg00)
8511 || ((code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR)
8512 && ! has_cleanups (arg00)))
8513 return fold (build (code0, type, arg00,
8514 fold (build1 (CLEANUP_POINT_EXPR,
8515 TREE_TYPE (arg01), arg01))));
8517 if (TREE_CONSTANT (arg01))
8518 return fold (build (code0, type,
8519 fold (build1 (CLEANUP_POINT_EXPR,
8520 TREE_TYPE (arg00), arg00)),
8521 arg01));
8524 return t;
8527 case CALL_EXPR:
8528 /* Check for a built-in function. */
8529 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
8530 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
8531 == FUNCTION_DECL)
8532 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
8534 tree tmp = fold_builtin (t);
8535 if (tmp)
8536 return tmp;
8538 return t;
8540 default:
8541 return t;
8542 } /* switch (code) */
8545 #ifdef ENABLE_FOLD_CHECKING
8546 #undef fold
8548 static void fold_checksum_tree (tree, struct md5_ctx *, htab_t);
8549 static void fold_check_failed (tree, tree);
8550 void print_fold_checksum (tree);
8552 /* When --enable-checking=fold, compute a digest of expr before
8553 and after actual fold call to see if fold did not accidentally
8554 change original expr. */
8556 tree
8557 fold (tree expr)
8559 tree ret;
8560 struct md5_ctx ctx;
8561 unsigned char checksum_before[16], checksum_after[16];
8562 htab_t ht;
8564 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
8565 md5_init_ctx (&ctx);
8566 fold_checksum_tree (expr, &ctx, ht);
8567 md5_finish_ctx (&ctx, checksum_before);
8568 htab_empty (ht);
8570 ret = fold_1 (expr);
8572 md5_init_ctx (&ctx);
8573 fold_checksum_tree (expr, &ctx, ht);
8574 md5_finish_ctx (&ctx, checksum_after);
8575 htab_delete (ht);
8577 if (memcmp (checksum_before, checksum_after, 16))
8578 fold_check_failed (expr, ret);
8580 return ret;
8583 void
8584 print_fold_checksum (tree expr)
8586 struct md5_ctx ctx;
8587 unsigned char checksum[16], cnt;
8588 htab_t ht;
8590 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
8591 md5_init_ctx (&ctx);
8592 fold_checksum_tree (expr, &ctx, ht);
8593 md5_finish_ctx (&ctx, checksum);
8594 htab_delete (ht);
8595 for (cnt = 0; cnt < 16; ++cnt)
8596 fprintf (stderr, "%02x", checksum[cnt]);
8597 putc ('\n', stderr);
8600 static void
8601 fold_check_failed (tree expr ATTRIBUTE_UNUSED, tree ret ATTRIBUTE_UNUSED)
8603 internal_error ("fold check: original tree changed by fold");
8606 static void
8607 fold_checksum_tree (tree expr, struct md5_ctx *ctx, htab_t ht)
8609 void **slot;
8610 enum tree_code code;
8611 char buf[sizeof (struct tree_decl)];
8612 int i, len;
8614 if (sizeof (struct tree_exp) + 5 * sizeof (tree)
8615 > sizeof (struct tree_decl)
8616 || sizeof (struct tree_type) > sizeof (struct tree_decl))
8617 abort ();
8618 if (expr == NULL)
8619 return;
8620 slot = htab_find_slot (ht, expr, INSERT);
8621 if (*slot != NULL)
8622 return;
8623 *slot = expr;
8624 code = TREE_CODE (expr);
8625 if (code == SAVE_EXPR && SAVE_EXPR_NOPLACEHOLDER (expr))
8627 /* Allow SAVE_EXPR_NOPLACEHOLDER flag to be modified. */
8628 memcpy (buf, expr, tree_size (expr));
8629 expr = (tree) buf;
8630 SAVE_EXPR_NOPLACEHOLDER (expr) = 0;
8632 else if (TREE_CODE_CLASS (code) == 'd' && DECL_ASSEMBLER_NAME_SET_P (expr))
8634 /* Allow DECL_ASSEMBLER_NAME to be modified. */
8635 memcpy (buf, expr, tree_size (expr));
8636 expr = (tree) buf;
8637 SET_DECL_ASSEMBLER_NAME (expr, NULL);
8639 else if (TREE_CODE_CLASS (code) == 't'
8640 && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)))
8642 /* Allow TYPE_POINTER_TO and TYPE_REFERENCE_TO to be modified. */
8643 memcpy (buf, expr, tree_size (expr));
8644 expr = (tree) buf;
8645 TYPE_POINTER_TO (expr) = NULL;
8646 TYPE_REFERENCE_TO (expr) = NULL;
8648 md5_process_bytes (expr, tree_size (expr), ctx);
8649 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
8650 if (TREE_CODE_CLASS (code) != 't' && TREE_CODE_CLASS (code) != 'd')
8651 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
8652 len = TREE_CODE_LENGTH (code);
8653 switch (TREE_CODE_CLASS (code))
8655 case 'c':
8656 switch (code)
8658 case STRING_CST:
8659 md5_process_bytes (TREE_STRING_POINTER (expr),
8660 TREE_STRING_LENGTH (expr), ctx);
8661 break;
8662 case COMPLEX_CST:
8663 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
8664 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
8665 break;
8666 case VECTOR_CST:
8667 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
8668 break;
8669 default:
8670 break;
8672 break;
8673 case 'x':
8674 switch (code)
8676 case TREE_LIST:
8677 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
8678 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
8679 break;
8680 case TREE_VEC:
8681 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
8682 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
8683 break;
8684 default:
8685 break;
8687 break;
8688 case 'e':
8689 switch (code)
8691 case SAVE_EXPR: len = 2; break;
8692 case GOTO_SUBROUTINE_EXPR: len = 0; break;
8693 case RTL_EXPR: len = 0; break;
8694 case WITH_CLEANUP_EXPR: len = 2; break;
8695 default: break;
8697 /* Fall through. */
8698 case 'r':
8699 case '<':
8700 case '1':
8701 case '2':
8702 case 's':
8703 for (i = 0; i < len; ++i)
8704 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
8705 break;
8706 case 'd':
8707 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
8708 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
8709 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
8710 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
8711 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
8712 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
8713 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
8714 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
8715 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
8716 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
8717 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
8718 break;
8719 case 't':
8720 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
8721 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
8722 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
8723 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
8724 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
8725 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
8726 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
8727 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
8728 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
8729 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
8730 break;
8731 default:
8732 break;
8736 #endif
8738 /* Perform constant folding and related simplification of initializer
8739 expression EXPR. This behaves identically to "fold" but ignores
8740 potential run-time traps and exceptions that fold must preserve. */
8742 tree
8743 fold_initializer (tree expr)
8745 int saved_signaling_nans = flag_signaling_nans;
8746 int saved_trapping_math = flag_trapping_math;
8747 int saved_trapv = flag_trapv;
8748 tree result;
8750 flag_signaling_nans = 0;
8751 flag_trapping_math = 0;
8752 flag_trapv = 0;
8754 result = fold (expr);
8756 flag_signaling_nans = saved_signaling_nans;
8757 flag_trapping_math = saved_trapping_math;
8758 flag_trapv = saved_trapv;
8760 return result;
8763 /* Determine if first argument is a multiple of second argument. Return 0 if
8764 it is not, or we cannot easily determined it to be.
8766 An example of the sort of thing we care about (at this point; this routine
8767 could surely be made more general, and expanded to do what the *_DIV_EXPR's
8768 fold cases do now) is discovering that
8770 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
8772 is a multiple of
8774 SAVE_EXPR (J * 8)
8776 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
8778 This code also handles discovering that
8780 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
8782 is a multiple of 8 so we don't have to worry about dealing with a
8783 possible remainder.
8785 Note that we *look* inside a SAVE_EXPR only to determine how it was
8786 calculated; it is not safe for fold to do much of anything else with the
8787 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
8788 at run time. For example, the latter example above *cannot* be implemented
8789 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
8790 evaluation time of the original SAVE_EXPR is not necessarily the same at
8791 the time the new expression is evaluated. The only optimization of this
8792 sort that would be valid is changing
8794 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
8796 divided by 8 to
8798 SAVE_EXPR (I) * SAVE_EXPR (J)
8800 (where the same SAVE_EXPR (J) is used in the original and the
8801 transformed version). */
8803 static int
8804 multiple_of_p (tree type, tree top, tree bottom)
8806 if (operand_equal_p (top, bottom, 0))
8807 return 1;
8809 if (TREE_CODE (type) != INTEGER_TYPE)
8810 return 0;
8812 switch (TREE_CODE (top))
8814 case MULT_EXPR:
8815 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
8816 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
8818 case PLUS_EXPR:
8819 case MINUS_EXPR:
8820 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
8821 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
8823 case LSHIFT_EXPR:
8824 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
8826 tree op1, t1;
8828 op1 = TREE_OPERAND (top, 1);
8829 /* const_binop may not detect overflow correctly,
8830 so check for it explicitly here. */
8831 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
8832 > TREE_INT_CST_LOW (op1)
8833 && TREE_INT_CST_HIGH (op1) == 0
8834 && 0 != (t1 = fold_convert (type,
8835 const_binop (LSHIFT_EXPR,
8836 size_one_node,
8837 op1, 0)))
8838 && ! TREE_OVERFLOW (t1))
8839 return multiple_of_p (type, t1, bottom);
8841 return 0;
8843 case NOP_EXPR:
8844 /* Can't handle conversions from non-integral or wider integral type. */
8845 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
8846 || (TYPE_PRECISION (type)
8847 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
8848 return 0;
8850 /* .. fall through ... */
8852 case SAVE_EXPR:
8853 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
8855 case INTEGER_CST:
8856 if (TREE_CODE (bottom) != INTEGER_CST
8857 || (TYPE_UNSIGNED (type)
8858 && (tree_int_cst_sgn (top) < 0
8859 || tree_int_cst_sgn (bottom) < 0)))
8860 return 0;
8861 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
8862 top, bottom, 0));
8864 default:
8865 return 0;
8869 /* Return true if `t' is known to be non-negative. */
8872 tree_expr_nonnegative_p (tree t)
8874 switch (TREE_CODE (t))
8876 case ABS_EXPR:
8877 return 1;
8879 case INTEGER_CST:
8880 return tree_int_cst_sgn (t) >= 0;
8882 case REAL_CST:
8883 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
8885 case PLUS_EXPR:
8886 if (FLOAT_TYPE_P (TREE_TYPE (t)))
8887 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8888 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8890 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
8891 both unsigned and at least 2 bits shorter than the result. */
8892 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
8893 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
8894 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
8896 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
8897 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
8898 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
8899 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
8901 unsigned int prec = MAX (TYPE_PRECISION (inner1),
8902 TYPE_PRECISION (inner2)) + 1;
8903 return prec < TYPE_PRECISION (TREE_TYPE (t));
8906 break;
8908 case MULT_EXPR:
8909 if (FLOAT_TYPE_P (TREE_TYPE (t)))
8911 /* x * x for floating point x is always non-negative. */
8912 if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
8913 return 1;
8914 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8915 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8918 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
8919 both unsigned and their total bits is shorter than the result. */
8920 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
8921 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
8922 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
8924 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
8925 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
8926 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
8927 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
8928 return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
8929 < TYPE_PRECISION (TREE_TYPE (t));
8931 return 0;
8933 case TRUNC_DIV_EXPR:
8934 case CEIL_DIV_EXPR:
8935 case FLOOR_DIV_EXPR:
8936 case ROUND_DIV_EXPR:
8937 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8938 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8940 case TRUNC_MOD_EXPR:
8941 case CEIL_MOD_EXPR:
8942 case FLOOR_MOD_EXPR:
8943 case ROUND_MOD_EXPR:
8944 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8946 case RDIV_EXPR:
8947 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8948 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8950 case BIT_AND_EXPR:
8951 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
8952 || tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8953 case BIT_IOR_EXPR:
8954 case BIT_XOR_EXPR:
8955 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8956 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8958 case NOP_EXPR:
8960 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
8961 tree outer_type = TREE_TYPE (t);
8963 if (TREE_CODE (outer_type) == REAL_TYPE)
8965 if (TREE_CODE (inner_type) == REAL_TYPE)
8966 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8967 if (TREE_CODE (inner_type) == INTEGER_TYPE)
8969 if (TYPE_UNSIGNED (inner_type))
8970 return 1;
8971 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8974 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
8976 if (TREE_CODE (inner_type) == REAL_TYPE)
8977 return tree_expr_nonnegative_p (TREE_OPERAND (t,0));
8978 if (TREE_CODE (inner_type) == INTEGER_TYPE)
8979 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
8980 && TYPE_UNSIGNED (inner_type);
8983 break;
8985 case COND_EXPR:
8986 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
8987 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
8988 case COMPOUND_EXPR:
8989 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8990 case MIN_EXPR:
8991 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8992 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8993 case MAX_EXPR:
8994 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8995 || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8996 case MODIFY_EXPR:
8997 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8998 case BIND_EXPR:
8999 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9000 case SAVE_EXPR:
9001 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9002 case NON_LVALUE_EXPR:
9003 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9004 case FLOAT_EXPR:
9005 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9006 case RTL_EXPR:
9007 return rtl_expr_nonnegative_p (RTL_EXPR_RTL (t));
9009 case CALL_EXPR:
9011 tree fndecl = get_callee_fndecl (t);
9012 tree arglist = TREE_OPERAND (t, 1);
9013 if (fndecl
9014 && DECL_BUILT_IN (fndecl)
9015 && DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD)
9016 switch (DECL_FUNCTION_CODE (fndecl))
9018 #define CASE_BUILTIN_F(BUILT_IN_FN) \
9019 case BUILT_IN_FN: case BUILT_IN_FN##F: case BUILT_IN_FN##L:
9020 #define CASE_BUILTIN_I(BUILT_IN_FN) \
9021 case BUILT_IN_FN: case BUILT_IN_FN##L: case BUILT_IN_FN##LL:
9023 CASE_BUILTIN_F (BUILT_IN_ACOS)
9024 CASE_BUILTIN_F (BUILT_IN_ACOSH)
9025 CASE_BUILTIN_F (BUILT_IN_CABS)
9026 CASE_BUILTIN_F (BUILT_IN_COSH)
9027 CASE_BUILTIN_F (BUILT_IN_ERFC)
9028 CASE_BUILTIN_F (BUILT_IN_EXP)
9029 CASE_BUILTIN_F (BUILT_IN_EXP10)
9030 CASE_BUILTIN_F (BUILT_IN_EXP2)
9031 CASE_BUILTIN_F (BUILT_IN_FABS)
9032 CASE_BUILTIN_F (BUILT_IN_FDIM)
9033 CASE_BUILTIN_F (BUILT_IN_FREXP)
9034 CASE_BUILTIN_F (BUILT_IN_HYPOT)
9035 CASE_BUILTIN_F (BUILT_IN_POW10)
9036 CASE_BUILTIN_F (BUILT_IN_SQRT)
9037 CASE_BUILTIN_I (BUILT_IN_FFS)
9038 CASE_BUILTIN_I (BUILT_IN_PARITY)
9039 CASE_BUILTIN_I (BUILT_IN_POPCOUNT)
9040 /* Always true. */
9041 return 1;
9043 CASE_BUILTIN_F (BUILT_IN_ASINH)
9044 CASE_BUILTIN_F (BUILT_IN_ATAN)
9045 CASE_BUILTIN_F (BUILT_IN_ATANH)
9046 CASE_BUILTIN_F (BUILT_IN_CBRT)
9047 CASE_BUILTIN_F (BUILT_IN_CEIL)
9048 CASE_BUILTIN_F (BUILT_IN_ERF)
9049 CASE_BUILTIN_F (BUILT_IN_EXPM1)
9050 CASE_BUILTIN_F (BUILT_IN_FLOOR)
9051 CASE_BUILTIN_F (BUILT_IN_FMOD)
9052 CASE_BUILTIN_F (BUILT_IN_LDEXP)
9053 CASE_BUILTIN_F (BUILT_IN_LLRINT)
9054 CASE_BUILTIN_F (BUILT_IN_LLROUND)
9055 CASE_BUILTIN_F (BUILT_IN_LRINT)
9056 CASE_BUILTIN_F (BUILT_IN_LROUND)
9057 CASE_BUILTIN_F (BUILT_IN_MODF)
9058 CASE_BUILTIN_F (BUILT_IN_NEARBYINT)
9059 CASE_BUILTIN_F (BUILT_IN_POW)
9060 CASE_BUILTIN_F (BUILT_IN_RINT)
9061 CASE_BUILTIN_F (BUILT_IN_ROUND)
9062 CASE_BUILTIN_F (BUILT_IN_SIGNBIT)
9063 CASE_BUILTIN_F (BUILT_IN_SINH)
9064 CASE_BUILTIN_F (BUILT_IN_TANH)
9065 CASE_BUILTIN_F (BUILT_IN_TRUNC)
9066 /* True if the 1st argument is nonnegative. */
9067 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
9069 CASE_BUILTIN_F(BUILT_IN_FMAX)
9070 /* True if the 1st OR 2nd arguments are nonnegative. */
9071 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
9072 || tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
9074 CASE_BUILTIN_F(BUILT_IN_FMIN)
9075 /* True if the 1st AND 2nd arguments are nonnegative. */
9076 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
9077 && tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
9079 CASE_BUILTIN_F(BUILT_IN_COPYSIGN)
9080 /* True if the 2nd argument is nonnegative. */
9081 return tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
9083 default:
9084 break;
9085 #undef CASE_BUILTIN_F
9086 #undef CASE_BUILTIN_I
9090 /* ... fall through ... */
9092 default:
9093 if (truth_value_p (TREE_CODE (t)))
9094 /* Truth values evaluate to 0 or 1, which is nonnegative. */
9095 return 1;
9098 /* We don't know sign of `t', so be conservative and return false. */
9099 return 0;
9102 /* Return true when T is an address and is known to be nonzero.
9103 For floating point we further ensure that T is not denormal.
9104 Similar logic is present in nonzero_address in rtlanal.h */
9106 static bool
9107 tree_expr_nonzero_p (tree t)
9109 tree type = TREE_TYPE (t);
9111 /* Doing something useful for floating point would need more work. */
9112 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9113 return false;
9115 switch (TREE_CODE (t))
9117 case ABS_EXPR:
9118 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
9119 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
9121 case INTEGER_CST:
9122 return !integer_zerop (t);
9124 case PLUS_EXPR:
9125 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
9127 /* With the presence of negative values it is hard
9128 to say something. */
9129 if (!tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
9130 || !tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
9131 return false;
9132 /* One of operands must be positive and the other non-negative. */
9133 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
9134 || tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
9136 break;
9138 case MULT_EXPR:
9139 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
9141 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
9142 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
9144 break;
9146 case NOP_EXPR:
9148 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
9149 tree outer_type = TREE_TYPE (t);
9151 return (TYPE_PRECISION (inner_type) >= TYPE_PRECISION (outer_type)
9152 && tree_expr_nonzero_p (TREE_OPERAND (t, 0)));
9154 break;
9156 case ADDR_EXPR:
9157 /* Weak declarations may link to NULL. */
9158 if (DECL_P (TREE_OPERAND (t, 0)))
9159 return !DECL_WEAK (TREE_OPERAND (t, 0));
9160 /* Constants and all other cases are never weak. */
9161 return true;
9163 case COND_EXPR:
9164 return (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
9165 && tree_expr_nonzero_p (TREE_OPERAND (t, 2)));
9167 case MIN_EXPR:
9168 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
9169 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
9171 case MAX_EXPR:
9172 if (tree_expr_nonzero_p (TREE_OPERAND (t, 0)))
9174 /* When both operands are nonzero, then MAX must be too. */
9175 if (tree_expr_nonzero_p (TREE_OPERAND (t, 1)))
9176 return true;
9178 /* MAX where operand 0 is positive is positive. */
9179 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9181 /* MAX where operand 1 is positive is positive. */
9182 else if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
9183 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
9184 return true;
9185 break;
9187 case COMPOUND_EXPR:
9188 case MODIFY_EXPR:
9189 case BIND_EXPR:
9190 return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
9192 case SAVE_EXPR:
9193 case NON_LVALUE_EXPR:
9194 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
9196 case BIT_IOR_EXPR:
9197 return tree_expr_nonzero_p (TREE_OPERAND (t, 1))
9198 || tree_expr_nonzero_p (TREE_OPERAND (t, 0));
9200 default:
9201 break;
9203 return false;
9206 /* Return true if `r' is known to be non-negative.
9207 Only handles constants at the moment. */
9210 rtl_expr_nonnegative_p (rtx r)
9212 switch (GET_CODE (r))
9214 case CONST_INT:
9215 return INTVAL (r) >= 0;
9217 case CONST_DOUBLE:
9218 if (GET_MODE (r) == VOIDmode)
9219 return CONST_DOUBLE_HIGH (r) >= 0;
9220 return 0;
9222 case CONST_VECTOR:
9224 int units, i;
9225 rtx elt;
9227 units = CONST_VECTOR_NUNITS (r);
9229 for (i = 0; i < units; ++i)
9231 elt = CONST_VECTOR_ELT (r, i);
9232 if (!rtl_expr_nonnegative_p (elt))
9233 return 0;
9236 return 1;
9239 case SYMBOL_REF:
9240 case LABEL_REF:
9241 /* These are always nonnegative. */
9242 return 1;
9244 default:
9245 return 0;
9250 /* See if we are applying CODE, a relational to the highest or lowest
9251 possible integer of TYPE. If so, then the result is a compile
9252 time constant. */
9254 static tree
9255 fold_relational_hi_lo (enum tree_code *code_p, const tree type, tree *op0_p,
9256 tree *op1_p)
9258 tree op0 = *op0_p;
9259 tree op1 = *op1_p;
9260 enum tree_code code = *code_p;
9261 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (op1)));
9263 if (TREE_CODE (op1) == INTEGER_CST
9264 && ! TREE_CONSTANT_OVERFLOW (op1)
9265 && width <= HOST_BITS_PER_WIDE_INT
9266 && (INTEGRAL_TYPE_P (TREE_TYPE (op1))
9267 || POINTER_TYPE_P (TREE_TYPE (op1))))
9269 unsigned HOST_WIDE_INT signed_max;
9270 unsigned HOST_WIDE_INT max, min;
9272 signed_max = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1;
9274 if (TYPE_UNSIGNED (TREE_TYPE (op1)))
9276 max = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
9277 min = 0;
9279 else
9281 max = signed_max;
9282 min = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
9285 if (TREE_INT_CST_HIGH (op1) == 0
9286 && TREE_INT_CST_LOW (op1) == max)
9287 switch (code)
9289 case GT_EXPR:
9290 return omit_one_operand (type,
9291 convert (type, integer_zero_node),
9292 op0);
9293 case GE_EXPR:
9294 *code_p = EQ_EXPR;
9295 break;
9296 case LE_EXPR:
9297 return omit_one_operand (type,
9298 convert (type, integer_one_node),
9299 op0);
9300 case LT_EXPR:
9301 *code_p = NE_EXPR;
9302 break;
9304 /* The GE_EXPR and LT_EXPR cases above are not normally
9305 reached because of previous transformations. */
9307 default:
9308 break;
9310 else if (TREE_INT_CST_HIGH (op1) == 0
9311 && TREE_INT_CST_LOW (op1) == max - 1)
9312 switch (code)
9314 case GT_EXPR:
9315 *code_p = EQ_EXPR;
9316 *op1_p = const_binop (PLUS_EXPR, op1, integer_one_node, 0);
9317 break;
9318 case LE_EXPR:
9319 *code_p = NE_EXPR;
9320 *op1_p = const_binop (PLUS_EXPR, op1, integer_one_node, 0);
9321 break;
9322 default:
9323 break;
9325 else if (TREE_INT_CST_HIGH (op1) == (min ? -1 : 0)
9326 && TREE_INT_CST_LOW (op1) == min)
9327 switch (code)
9329 case LT_EXPR:
9330 return omit_one_operand (type,
9331 convert (type, integer_zero_node),
9332 op0);
9333 case LE_EXPR:
9334 *code_p = EQ_EXPR;
9335 break;
9337 case GE_EXPR:
9338 return omit_one_operand (type,
9339 convert (type, integer_one_node),
9340 op0);
9341 case GT_EXPR:
9342 *code_p = NE_EXPR;
9343 break;
9345 default:
9346 break;
9348 else if (TREE_INT_CST_HIGH (op1) == (min ? -1 : 0)
9349 && TREE_INT_CST_LOW (op1) == min + 1)
9350 switch (code)
9352 case GE_EXPR:
9353 *code_p = NE_EXPR;
9354 *op1_p = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9355 break;
9356 case LT_EXPR:
9357 *code_p = EQ_EXPR;
9358 *op1_p = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9359 break;
9360 default:
9361 break;
9364 else if (TREE_INT_CST_HIGH (op1) == 0
9365 && TREE_INT_CST_LOW (op1) == signed_max
9366 && TYPE_UNSIGNED (TREE_TYPE (op1))
9367 /* signed_type does not work on pointer types. */
9368 && INTEGRAL_TYPE_P (TREE_TYPE (op1)))
9370 /* The following case also applies to X < signed_max+1
9371 and X >= signed_max+1 because previous transformations. */
9372 if (code == LE_EXPR || code == GT_EXPR)
9374 tree st0, st1, exp, retval;
9375 st0 = (*lang_hooks.types.signed_type) (TREE_TYPE (op0));
9376 st1 = (*lang_hooks.types.signed_type) (TREE_TYPE (op1));
9378 exp = build (code == LE_EXPR ? GE_EXPR: LT_EXPR,
9379 type,
9380 convert (st0, op0),
9381 convert (st1, integer_zero_node));
9383 retval
9384 = nondestructive_fold_binary_to_constant (TREE_CODE (exp),
9385 TREE_TYPE (exp),
9386 TREE_OPERAND (exp, 0),
9387 TREE_OPERAND (exp, 1));
9389 /* If we are in gimple form, then returning EXP would create
9390 non-gimple expressions. Clearing it is safe and insures
9391 we do not allow a non-gimple expression to escape. */
9392 if (in_gimple_form)
9393 exp = NULL;
9395 return (retval ? retval : exp);
9400 return NULL_TREE;
9404 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
9405 attempt to fold the expression to a constant without modifying TYPE,
9406 OP0 or OP1.
9408 If the expression could be simplified to a constant, then return
9409 the constant. If the expression would not be simplified to a
9410 constant, then return NULL_TREE.
9412 Note this is primarily designed to be called after gimplification
9413 of the tree structures and when at least one operand is a constant.
9414 As a result of those simplifying assumptions this routine is far
9415 simpler than the generic fold routine. */
9417 tree
9418 nondestructive_fold_binary_to_constant (enum tree_code code, tree type,
9419 tree op0, tree op1)
9421 int wins = 1;
9422 tree subop0;
9423 tree subop1;
9424 tree tem;
9426 /* If this is a commutative operation, and ARG0 is a constant, move it
9427 to ARG1 to reduce the number of tests below. */
9428 if (commutative_tree_code (code)
9429 && (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST))
9431 tem = op0;
9432 op0 = op1;
9433 op1 = tem;
9436 /* If either operand is a complex type, extract its real component. */
9437 if (TREE_CODE (op0) == COMPLEX_CST)
9438 subop0 = TREE_REALPART (op0);
9439 else
9440 subop0 = op0;
9442 if (TREE_CODE (op1) == COMPLEX_CST)
9443 subop1 = TREE_REALPART (op1);
9444 else
9445 subop1 = op1;
9447 /* Note if either argument is not a real or integer constant.
9448 With a few exceptions, simplification is limited to cases
9449 where both arguments are constants. */
9450 if ((TREE_CODE (subop0) != INTEGER_CST
9451 && TREE_CODE (subop0) != REAL_CST)
9452 || (TREE_CODE (subop1) != INTEGER_CST
9453 && TREE_CODE (subop1) != REAL_CST))
9454 wins = 0;
9456 switch (code)
9458 case PLUS_EXPR:
9459 /* (plus (address) (const_int)) is a constant. */
9460 if (TREE_CODE (op0) == PLUS_EXPR
9461 && TREE_CODE (op1) == INTEGER_CST
9462 && (TREE_CODE (TREE_OPERAND (op0, 0)) == ADDR_EXPR
9463 || (TREE_CODE (TREE_OPERAND (op0, 0)) == NOP_EXPR
9464 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (op0, 0), 0))
9465 == ADDR_EXPR)))
9466 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9468 return build (PLUS_EXPR, type, TREE_OPERAND (op0, 0),
9469 const_binop (PLUS_EXPR, op1, TREE_OPERAND (op0, 1), 0));
9471 case BIT_XOR_EXPR:
9473 binary:
9474 if (!wins)
9475 return NULL_TREE;
9477 /* Both arguments are constants. Simplify. */
9478 tem = const_binop (code, op0, op1, 0);
9479 if (tem != NULL_TREE)
9481 /* The return value should always have the same type as
9482 the original expression. */
9483 if (TREE_TYPE (tem) != type)
9484 tem = convert (type, tem);
9486 return tem;
9488 return NULL_TREE;
9490 case MINUS_EXPR:
9491 /* Fold &x - &x. This can happen from &x.foo - &x.
9492 This is unsafe for certain floats even in non-IEEE formats.
9493 In IEEE, it is unsafe because it does wrong for NaNs.
9494 Also note that operand_equal_p is always false if an
9495 operand is volatile. */
9496 if (! FLOAT_TYPE_P (type) && operand_equal_p (op0, op1, 0))
9497 return convert (type, integer_zero_node);
9499 goto binary;
9501 case MULT_EXPR:
9502 case BIT_AND_EXPR:
9503 /* Special case multiplication or bitwise AND where one argument
9504 is zero. */
9505 if (! FLOAT_TYPE_P (type) && integer_zerop (op1))
9506 return omit_one_operand (type, op1, op0);
9507 else
9508 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (op0)))
9509 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
9510 && real_zerop (op1))
9511 return omit_one_operand (type, op1, op0);
9513 goto binary;
9515 case BIT_IOR_EXPR:
9516 /* Special case when we know the result will be all ones. */
9517 if (integer_all_onesp (op1))
9518 return omit_one_operand (type, op1, op0);
9520 goto binary;
9522 case TRUNC_DIV_EXPR:
9523 case ROUND_DIV_EXPR:
9524 case FLOOR_DIV_EXPR:
9525 case CEIL_DIV_EXPR:
9526 case EXACT_DIV_EXPR:
9527 case TRUNC_MOD_EXPR:
9528 case ROUND_MOD_EXPR:
9529 case FLOOR_MOD_EXPR:
9530 case CEIL_MOD_EXPR:
9531 case RDIV_EXPR:
9532 /* Division by zero is undefined. */
9533 if (integer_zerop (op1))
9534 return NULL_TREE;
9536 if (TREE_CODE (op1) == REAL_CST
9537 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (op1)))
9538 && real_zerop (op1))
9539 return NULL_TREE;
9541 goto binary;
9543 case MIN_EXPR:
9544 if (INTEGRAL_TYPE_P (type)
9545 && operand_equal_p (op1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
9546 return omit_one_operand (type, op1, op0);
9548 goto binary;
9550 case MAX_EXPR:
9551 if (INTEGRAL_TYPE_P (type)
9552 && TYPE_MAX_VALUE (type)
9553 && operand_equal_p (op1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
9554 return omit_one_operand (type, op1, op0);
9556 goto binary;
9558 case RSHIFT_EXPR:
9559 /* Optimize -1 >> x for arithmetic right shifts. */
9560 if (integer_all_onesp (op0) && ! TYPE_UNSIGNED (type))
9561 return omit_one_operand (type, op0, op1);
9562 /* ... fall through ... */
9564 case LSHIFT_EXPR:
9565 if (integer_zerop (op0))
9566 return omit_one_operand (type, op0, op1);
9568 /* Since negative shift count is not well-defined, don't
9569 try to compute it in the compiler. */
9570 if (TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sgn (op1) < 0)
9571 return NULL_TREE;
9573 goto binary;
9575 case LROTATE_EXPR:
9576 case RROTATE_EXPR:
9577 /* -1 rotated either direction by any amount is still -1. */
9578 if (integer_all_onesp (op0))
9579 return omit_one_operand (type, op0, op1);
9581 /* 0 rotated either direction by any amount is still zero. */
9582 if (integer_zerop (op0))
9583 return omit_one_operand (type, op0, op1);
9585 goto binary;
9587 case COMPLEX_EXPR:
9588 if (wins)
9589 return build_complex (type, op0, op1);
9590 return NULL_TREE;
9592 case LT_EXPR:
9593 case LE_EXPR:
9594 case GT_EXPR:
9595 case GE_EXPR:
9596 case EQ_EXPR:
9597 case NE_EXPR:
9598 /* If one arg is a real or integer constant, put it last. */
9599 if ((TREE_CODE (op0) == INTEGER_CST
9600 && TREE_CODE (op1) != INTEGER_CST)
9601 || (TREE_CODE (op0) == REAL_CST
9602 && TREE_CODE (op0) != REAL_CST))
9604 tree temp;
9606 temp = op0;
9607 op0 = op1;
9608 op1 = temp;
9609 code = swap_tree_comparison (code);
9612 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
9613 This transformation affects the cases which are handled in later
9614 optimizations involving comparisons with non-negative constants. */
9615 if (TREE_CODE (op1) == INTEGER_CST
9616 && TREE_CODE (op0) != INTEGER_CST
9617 && tree_int_cst_sgn (op1) > 0)
9619 switch (code)
9621 case GE_EXPR:
9622 code = GT_EXPR;
9623 op1 = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9624 break;
9626 case LT_EXPR:
9627 code = LE_EXPR;
9628 op1 = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9629 break;
9631 default:
9632 break;
9636 tem = fold_relational_hi_lo (&code, type, &op0, &op1);
9637 if (tem)
9638 return tem;
9640 if (!wins)
9641 return NULL_TREE;
9643 return fold_relational_const (code, type, op0, op1);
9645 case RANGE_EXPR:
9646 /* This could probably be handled. */
9647 return NULL_TREE;
9649 case TRUTH_AND_EXPR:
9650 /* If second arg is constant zero, result is zero, but first arg
9651 must be evaluated. */
9652 if (integer_zerop (op1))
9653 return omit_one_operand (type, op1, op0);
9654 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
9655 case will be handled here. */
9656 if (integer_zerop (op0))
9657 return omit_one_operand (type, op0, op1);
9658 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
9660 int x1 = ! integer_zerop (op0);
9661 int x2 = ! integer_zerop (op1);
9663 return ((x1 & x2) ? integer_one_node : integer_zero_node);
9665 return NULL_TREE;
9667 case TRUTH_OR_EXPR:
9668 /* If second arg is constant true, result is true, but we must
9669 evaluate first arg. */
9670 if (TREE_CODE (op1) == INTEGER_CST && ! integer_zerop (op1))
9671 return omit_one_operand (type, op1, op0);
9672 /* Likewise for first arg, but note this only occurs here for
9673 TRUTH_OR_EXPR. */
9674 if (TREE_CODE (op0) == INTEGER_CST && ! integer_zerop (op0))
9675 return omit_one_operand (type, op0, op1);
9676 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
9678 int x1 = ! integer_zerop (op0);
9679 int x2 = ! integer_zerop (op1);
9681 return ((x1 | x2) ? integer_one_node : integer_zero_node);
9683 return NULL_TREE;
9685 case TRUTH_XOR_EXPR:
9686 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
9688 int x1 = ! integer_zerop (op0);
9689 int x2 = ! integer_zerop (op1);
9691 return ((x1 ^ x2) ? integer_one_node : integer_zero_node);
9693 return NULL_TREE;
9695 default:
9696 return NULL_TREE;
9700 /* Given the components of a unary expression CODE, TYPE and OP0,
9701 attempt to fold the expression to a constant without modifying
9702 TYPE or OP0.
9704 If the expression could be simplified to a constant, then return
9705 the constant. If the expression would not be simplified to a
9706 constant, then return NULL_TREE.
9708 Note this is primarily designed to be called after gimplification
9709 of the tree structures and when op0 is a constant. As a result
9710 of those simplifying assumptions this routine is far simpler than
9711 the generic fold routine. */
9713 tree
9714 nondestructive_fold_unary_to_constant (enum tree_code code, tree type,
9715 tree op0)
9717 tree t;
9719 /* Make sure we have a suitable constant argument. */
9720 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
9722 tree subop;
9724 if (TREE_CODE (op0) == COMPLEX_CST)
9725 subop = TREE_REALPART (op0);
9726 else
9727 subop = op0;
9729 if (TREE_CODE (subop) != INTEGER_CST && TREE_CODE (subop) != REAL_CST)
9730 return NULL_TREE;
9733 switch (code)
9735 case NOP_EXPR:
9736 case FLOAT_EXPR:
9737 case CONVERT_EXPR:
9738 case FIX_TRUNC_EXPR:
9739 case FIX_FLOOR_EXPR:
9740 case FIX_CEIL_EXPR:
9741 return fold_convert_const (code, type, op0);
9743 case NEGATE_EXPR:
9744 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
9745 return fold_negate_const (op0, type);
9746 else
9747 return NULL_TREE;
9749 case ABS_EXPR:
9750 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
9751 return fold_abs_const (op0, type);
9752 else
9753 return NULL_TREE;
9755 case BIT_NOT_EXPR:
9756 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
9758 t = build_int_2 (~ TREE_INT_CST_LOW (op0), ~ TREE_INT_CST_HIGH (op0));
9759 TREE_TYPE (t) = type;
9760 force_fit_type (t, 0);
9761 TREE_OVERFLOW (t) = TREE_OVERFLOW (op0);
9762 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (op0);
9763 return t;
9765 else
9766 return NULL_TREE;
9768 case REALPART_EXPR:
9769 if (TREE_CODE (op0) == COMPLEX_CST)
9770 return TREE_REALPART (op0);
9771 else
9772 return NULL_TREE;
9774 case IMAGPART_EXPR:
9775 if (TREE_CODE (op0) == COMPLEX_CST)
9776 return TREE_IMAGPART (op0);
9777 else
9778 return NULL_TREE;
9780 case CONJ_EXPR:
9781 if (TREE_CODE (op0) == COMPLEX_CST
9782 && TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
9783 return build_complex (type, TREE_REALPART (op0),
9784 negate_expr (TREE_IMAGPART (op0)));
9785 return NULL_TREE;
9787 default:
9788 return NULL_TREE;
9792 /* If EXP represents referencing an element in a constant string
9793 (either via pointer arithmetic or array indexing), return the
9794 tree representing the value accessed, otherwise return NULL. */
9796 tree
9797 fold_read_from_constant_string (tree exp)
9799 if (TREE_CODE (exp) == INDIRECT_REF || TREE_CODE (exp) == ARRAY_REF)
9801 tree exp1 = TREE_OPERAND (exp, 0);
9802 tree index;
9803 tree string;
9805 if (TREE_CODE (exp) == INDIRECT_REF)
9807 string = string_constant (exp1, &index);
9809 else
9811 tree domain = TYPE_DOMAIN (TREE_TYPE (exp1));
9812 tree low_bound = domain ? TYPE_MIN_VALUE (domain) : integer_zero_node;
9813 index = convert (sizetype, TREE_OPERAND (exp, 1));
9815 /* Optimize the special-case of a zero lower bound.
9817 We convert the low_bound to sizetype to avoid some problems
9818 with constant folding. (E.g. suppose the lower bound is 1,
9819 and its mode is QI. Without the conversion,l (ARRAY
9820 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
9821 +INDEX), which becomes (ARRAY+255+INDEX). Opps!) */
9822 if (! integer_zerop (low_bound))
9823 index = size_diffop (index, convert (sizetype, low_bound));
9825 string = exp1;
9828 if (string
9829 && TREE_CODE (string) == STRING_CST
9830 && TREE_CODE (index) == INTEGER_CST
9831 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
9832 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
9833 == MODE_INT)
9834 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
9835 return build_int_2 ((TREE_STRING_POINTER (string)
9836 [TREE_INT_CST_LOW (index)]), 0);
9838 return NULL;
9841 /* Return the tree for neg (ARG0) when ARG0 is known to be either
9842 an integer constant or real constant.
9844 TYPE is the type of the result. */
9846 static tree
9847 fold_negate_const (tree arg0, tree type)
9849 tree t = NULL_TREE;
9851 if (TREE_CODE (arg0) == INTEGER_CST)
9853 unsigned HOST_WIDE_INT low;
9854 HOST_WIDE_INT high;
9855 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
9856 TREE_INT_CST_HIGH (arg0),
9857 &low, &high);
9858 t = build_int_2 (low, high);
9859 TREE_TYPE (t) = type;
9860 TREE_OVERFLOW (t)
9861 = (TREE_OVERFLOW (arg0)
9862 | force_fit_type (t, overflow && !TYPE_UNSIGNED (type)));
9863 TREE_CONSTANT_OVERFLOW (t)
9864 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
9866 else if (TREE_CODE (arg0) == REAL_CST)
9867 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
9868 #ifdef ENABLE_CHECKING
9869 else
9870 abort ();
9871 #endif
9873 return t;
9876 /* Return the tree for abs (ARG0) when ARG0 is known to be either
9877 an integer constant or real constant.
9879 TYPE is the type of the result. */
9881 static tree
9882 fold_abs_const (tree arg0, tree type)
9884 tree t = NULL_TREE;
9886 if (TREE_CODE (arg0) == INTEGER_CST)
9888 /* If the value is unsigned, then the absolute value is
9889 the same as the ordinary value. */
9890 if (TYPE_UNSIGNED (type))
9891 return arg0;
9892 /* Similarly, if the value is non-negative. */
9893 else if (INT_CST_LT (integer_minus_one_node, arg0))
9894 return arg0;
9895 /* If the value is negative, then the absolute value is
9896 its negation. */
9897 else
9899 unsigned HOST_WIDE_INT low;
9900 HOST_WIDE_INT high;
9901 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
9902 TREE_INT_CST_HIGH (arg0),
9903 &low, &high);
9904 t = build_int_2 (low, high);
9905 TREE_TYPE (t) = type;
9906 TREE_OVERFLOW (t)
9907 = (TREE_OVERFLOW (arg0)
9908 | force_fit_type (t, overflow));
9909 TREE_CONSTANT_OVERFLOW (t)
9910 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
9911 return t;
9914 else if (TREE_CODE (arg0) == REAL_CST)
9916 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
9917 return build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
9918 else
9919 return arg0;
9921 #ifdef ENABLE_CHECKING
9922 else
9923 abort ();
9924 #endif
9926 return t;
9929 /* Given CODE, a relational operator, the target type, TYPE and two
9930 constant operands OP0 and OP1, return the result of the
9931 relational operation. If the result is not a compile time
9932 constant, then return NULL_TREE. */
9934 static tree
9935 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
9937 tree tem;
9938 int invert;
9940 /* From here on, the only cases we handle are when the result is
9941 known to be a constant.
9943 To compute GT, swap the arguments and do LT.
9944 To compute GE, do LT and invert the result.
9945 To compute LE, swap the arguments, do LT and invert the result.
9946 To compute NE, do EQ and invert the result.
9948 Therefore, the code below must handle only EQ and LT. */
9950 if (code == LE_EXPR || code == GT_EXPR)
9952 tem = op0, op0 = op1, op1 = tem;
9953 code = swap_tree_comparison (code);
9956 /* Note that it is safe to invert for real values here because we
9957 will check below in the one case that it matters. */
9959 tem = NULL_TREE;
9960 invert = 0;
9961 if (code == NE_EXPR || code == GE_EXPR)
9963 invert = 1;
9964 code = invert_tree_comparison (code);
9967 /* Compute a result for LT or EQ if args permit;
9968 Otherwise return T. */
9969 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
9971 if (code == EQ_EXPR)
9972 tem = build_int_2 (tree_int_cst_equal (op0, op1), 0);
9973 else
9974 tem = build_int_2 ((TYPE_UNSIGNED (TREE_TYPE (op0))
9975 ? INT_CST_LT_UNSIGNED (op0, op1)
9976 : INT_CST_LT (op0, op1)),
9980 else if (code == EQ_EXPR && !TREE_SIDE_EFFECTS (op0)
9981 && integer_zerop (op1) && tree_expr_nonzero_p (op0))
9982 tem = build_int_2 (0, 0);
9984 /* Two real constants can be compared explicitly. */
9985 else if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
9987 /* If either operand is a NaN, the result is false with two
9988 exceptions: First, an NE_EXPR is true on NaNs, but that case
9989 is already handled correctly since we will be inverting the
9990 result for NE_EXPR. Second, if we had inverted a LE_EXPR
9991 or a GE_EXPR into a LT_EXPR, we must return true so that it
9992 will be inverted into false. */
9994 if (REAL_VALUE_ISNAN (TREE_REAL_CST (op0))
9995 || REAL_VALUE_ISNAN (TREE_REAL_CST (op1)))
9996 tem = build_int_2 (invert && code == LT_EXPR, 0);
9998 else if (code == EQ_EXPR)
9999 tem = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (op0),
10000 TREE_REAL_CST (op1)),
10002 else
10003 tem = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (op0),
10004 TREE_REAL_CST (op1)),
10008 if (tem == NULL_TREE)
10009 return NULL_TREE;
10011 if (invert)
10012 TREE_INT_CST_LOW (tem) ^= 1;
10014 TREE_TYPE (tem) = type;
10015 if (TREE_CODE (type) == BOOLEAN_TYPE)
10016 return (*lang_hooks.truthvalue_conversion) (tem);
10017 return tem;
10020 #include "gt-fold-const.h"