PR c/8224
[official-gcc.git] / gcc / fold-const.c
blob0f9f3d419a57074e4bd8eb59dfea0859e744b71c
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 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"
60 static void encode PARAMS ((HOST_WIDE_INT *,
61 unsigned HOST_WIDE_INT,
62 HOST_WIDE_INT));
63 static void decode PARAMS ((HOST_WIDE_INT *,
64 unsigned HOST_WIDE_INT *,
65 HOST_WIDE_INT *));
66 static bool negate_expr_p PARAMS ((tree));
67 static tree negate_expr PARAMS ((tree));
68 static tree split_tree PARAMS ((tree, enum tree_code, tree *, tree *,
69 tree *, int));
70 static tree associate_trees PARAMS ((tree, tree, enum tree_code, tree));
71 static tree int_const_binop PARAMS ((enum tree_code, tree, tree, int));
72 static tree const_binop PARAMS ((enum tree_code, tree, tree, int));
73 static hashval_t size_htab_hash PARAMS ((const void *));
74 static int size_htab_eq PARAMS ((const void *, const void *));
75 static tree fold_convert PARAMS ((tree, tree));
76 static enum tree_code invert_tree_comparison PARAMS ((enum tree_code));
77 static enum tree_code swap_tree_comparison PARAMS ((enum tree_code));
78 static int comparison_to_compcode PARAMS ((enum tree_code));
79 static enum tree_code compcode_to_comparison PARAMS ((int));
80 static int truth_value_p PARAMS ((enum tree_code));
81 static int operand_equal_for_comparison_p PARAMS ((tree, tree, tree));
82 static int twoval_comparison_p PARAMS ((tree, tree *, tree *, int *));
83 static tree eval_subst PARAMS ((tree, tree, tree, tree, tree));
84 static tree pedantic_omit_one_operand PARAMS ((tree, tree, tree));
85 static tree distribute_bit_expr PARAMS ((enum tree_code, tree, tree, tree));
86 static tree make_bit_field_ref PARAMS ((tree, tree, int, int, int));
87 static tree optimize_bit_field_compare PARAMS ((enum tree_code, tree,
88 tree, tree));
89 static tree decode_field_reference PARAMS ((tree, HOST_WIDE_INT *,
90 HOST_WIDE_INT *,
91 enum machine_mode *, int *,
92 int *, tree *, tree *));
93 static int all_ones_mask_p PARAMS ((tree, int));
94 static tree sign_bit_p PARAMS ((tree, tree));
95 static int simple_operand_p PARAMS ((tree));
96 static tree range_binop PARAMS ((enum tree_code, tree, tree, int,
97 tree, int));
98 static tree make_range PARAMS ((tree, int *, tree *, tree *));
99 static tree build_range_check PARAMS ((tree, tree, int, tree, tree));
100 static int merge_ranges PARAMS ((int *, tree *, tree *, int, tree, tree,
101 int, tree, tree));
102 static tree fold_range_test PARAMS ((tree));
103 static tree unextend PARAMS ((tree, int, int, tree));
104 static tree fold_truthop PARAMS ((enum tree_code, tree, tree, tree));
105 static tree optimize_minmax_comparison PARAMS ((tree));
106 static tree extract_muldiv PARAMS ((tree, tree, enum tree_code, tree));
107 static tree extract_muldiv_1 PARAMS ((tree, tree, enum tree_code, tree));
108 static tree strip_compound_expr PARAMS ((tree, tree));
109 static int multiple_of_p PARAMS ((tree, tree, tree));
110 static tree constant_boolean_node PARAMS ((int, tree));
111 static int count_cond PARAMS ((tree, int));
112 static tree fold_binary_op_with_conditional_arg
113 PARAMS ((enum tree_code, tree, tree, tree, int));
114 static bool fold_real_zero_addition_p PARAMS ((tree, tree, int));
115 static tree fold_mathfn_compare PARAMS ((enum built_in_function,
116 enum tree_code, tree, tree, tree));
118 /* The following constants represent a bit based encoding of GCC's
119 comparison operators. This encoding simplifies transformations
120 on relational comparison operators, such as AND and OR. */
121 #define COMPCODE_FALSE 0
122 #define COMPCODE_LT 1
123 #define COMPCODE_EQ 2
124 #define COMPCODE_LE 3
125 #define COMPCODE_GT 4
126 #define COMPCODE_NE 5
127 #define COMPCODE_GE 6
128 #define COMPCODE_TRUE 7
130 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
131 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
132 and SUM1. Then this yields nonzero if overflow occurred during the
133 addition.
135 Overflow occurs if A and B have the same sign, but A and SUM differ in
136 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
137 sign. */
138 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
140 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
141 We do that by representing the two-word integer in 4 words, with only
142 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
143 number. The value of the word is LOWPART + HIGHPART * BASE. */
145 #define LOWPART(x) \
146 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
147 #define HIGHPART(x) \
148 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
149 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
151 /* Unpack a two-word integer into 4 words.
152 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
153 WORDS points to the array of HOST_WIDE_INTs. */
155 static void
156 encode (words, low, hi)
157 HOST_WIDE_INT *words;
158 unsigned HOST_WIDE_INT low;
159 HOST_WIDE_INT hi;
161 words[0] = LOWPART (low);
162 words[1] = HIGHPART (low);
163 words[2] = LOWPART (hi);
164 words[3] = HIGHPART (hi);
167 /* Pack an array of 4 words into a two-word integer.
168 WORDS points to the array of words.
169 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
171 static void
172 decode (words, low, hi)
173 HOST_WIDE_INT *words;
174 unsigned HOST_WIDE_INT *low;
175 HOST_WIDE_INT *hi;
177 *low = words[0] + words[1] * BASE;
178 *hi = words[2] + words[3] * BASE;
181 /* Make the integer constant T valid for its type by setting to 0 or 1 all
182 the bits in the constant that don't belong in the type.
184 Return 1 if a signed overflow occurs, 0 otherwise. If OVERFLOW is
185 nonzero, a signed overflow has already occurred in calculating T, so
186 propagate it. */
189 force_fit_type (t, overflow)
190 tree t;
191 int overflow;
193 unsigned HOST_WIDE_INT low;
194 HOST_WIDE_INT high;
195 unsigned int prec;
197 if (TREE_CODE (t) == REAL_CST)
199 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
200 Consider doing it via real_convert now. */
201 return overflow;
204 else if (TREE_CODE (t) != INTEGER_CST)
205 return overflow;
207 low = TREE_INT_CST_LOW (t);
208 high = TREE_INT_CST_HIGH (t);
210 if (POINTER_TYPE_P (TREE_TYPE (t)))
211 prec = POINTER_SIZE;
212 else
213 prec = TYPE_PRECISION (TREE_TYPE (t));
215 /* First clear all bits that are beyond the type's precision. */
217 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
219 else if (prec > HOST_BITS_PER_WIDE_INT)
220 TREE_INT_CST_HIGH (t)
221 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
222 else
224 TREE_INT_CST_HIGH (t) = 0;
225 if (prec < HOST_BITS_PER_WIDE_INT)
226 TREE_INT_CST_LOW (t) &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
229 /* Unsigned types do not suffer sign extension or overflow unless they
230 are a sizetype. */
231 if (TREE_UNSIGNED (TREE_TYPE (t))
232 && ! (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
233 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
234 return overflow;
236 /* If the value's sign bit is set, extend the sign. */
237 if (prec != 2 * HOST_BITS_PER_WIDE_INT
238 && (prec > HOST_BITS_PER_WIDE_INT
239 ? 0 != (TREE_INT_CST_HIGH (t)
240 & ((HOST_WIDE_INT) 1
241 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
242 : 0 != (TREE_INT_CST_LOW (t)
243 & ((unsigned HOST_WIDE_INT) 1 << (prec - 1)))))
245 /* Value is negative:
246 set to 1 all the bits that are outside this type's precision. */
247 if (prec > HOST_BITS_PER_WIDE_INT)
248 TREE_INT_CST_HIGH (t)
249 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
250 else
252 TREE_INT_CST_HIGH (t) = -1;
253 if (prec < HOST_BITS_PER_WIDE_INT)
254 TREE_INT_CST_LOW (t) |= ((unsigned HOST_WIDE_INT) (-1) << prec);
258 /* Return nonzero if signed overflow occurred. */
259 return
260 ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
261 != 0);
264 /* Add two doubleword integers with doubleword result.
265 Each argument is given as two `HOST_WIDE_INT' pieces.
266 One argument is L1 and H1; the other, L2 and H2.
267 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
270 add_double (l1, h1, l2, h2, lv, hv)
271 unsigned HOST_WIDE_INT l1, l2;
272 HOST_WIDE_INT h1, h2;
273 unsigned HOST_WIDE_INT *lv;
274 HOST_WIDE_INT *hv;
276 unsigned HOST_WIDE_INT l;
277 HOST_WIDE_INT h;
279 l = l1 + l2;
280 h = h1 + h2 + (l < l1);
282 *lv = l;
283 *hv = h;
284 return OVERFLOW_SUM_SIGN (h1, h2, h);
287 /* Negate a doubleword integer with doubleword result.
288 Return nonzero if the operation overflows, assuming it's signed.
289 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
290 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
293 neg_double (l1, h1, lv, hv)
294 unsigned HOST_WIDE_INT l1;
295 HOST_WIDE_INT h1;
296 unsigned HOST_WIDE_INT *lv;
297 HOST_WIDE_INT *hv;
299 if (l1 == 0)
301 *lv = 0;
302 *hv = - h1;
303 return (*hv & h1) < 0;
305 else
307 *lv = -l1;
308 *hv = ~h1;
309 return 0;
313 /* Multiply two doubleword integers with doubleword result.
314 Return nonzero if the operation overflows, assuming it's signed.
315 Each argument is given as two `HOST_WIDE_INT' pieces.
316 One argument is L1 and H1; the other, L2 and H2.
317 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
320 mul_double (l1, h1, l2, h2, lv, hv)
321 unsigned HOST_WIDE_INT l1, l2;
322 HOST_WIDE_INT h1, h2;
323 unsigned HOST_WIDE_INT *lv;
324 HOST_WIDE_INT *hv;
326 HOST_WIDE_INT arg1[4];
327 HOST_WIDE_INT arg2[4];
328 HOST_WIDE_INT prod[4 * 2];
329 unsigned HOST_WIDE_INT carry;
330 int i, j, k;
331 unsigned HOST_WIDE_INT toplow, neglow;
332 HOST_WIDE_INT tophigh, neghigh;
334 encode (arg1, l1, h1);
335 encode (arg2, l2, h2);
337 memset ((char *) prod, 0, sizeof prod);
339 for (i = 0; i < 4; i++)
341 carry = 0;
342 for (j = 0; j < 4; j++)
344 k = i + j;
345 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
346 carry += arg1[i] * arg2[j];
347 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
348 carry += prod[k];
349 prod[k] = LOWPART (carry);
350 carry = HIGHPART (carry);
352 prod[i + 4] = carry;
355 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
357 /* Check for overflow by calculating the top half of the answer in full;
358 it should agree with the low half's sign bit. */
359 decode (prod + 4, &toplow, &tophigh);
360 if (h1 < 0)
362 neg_double (l2, h2, &neglow, &neghigh);
363 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
365 if (h2 < 0)
367 neg_double (l1, h1, &neglow, &neghigh);
368 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
370 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
373 /* Shift the doubleword integer in L1, H1 left by COUNT places
374 keeping only PREC bits of result.
375 Shift right if COUNT is negative.
376 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
377 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
379 void
380 lshift_double (l1, h1, count, prec, lv, hv, arith)
381 unsigned HOST_WIDE_INT l1;
382 HOST_WIDE_INT h1, count;
383 unsigned int prec;
384 unsigned HOST_WIDE_INT *lv;
385 HOST_WIDE_INT *hv;
386 int arith;
388 unsigned HOST_WIDE_INT signmask;
390 if (count < 0)
392 rshift_double (l1, h1, -count, prec, lv, hv, arith);
393 return;
396 #ifdef SHIFT_COUNT_TRUNCATED
397 if (SHIFT_COUNT_TRUNCATED)
398 count %= prec;
399 #endif
401 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
403 /* Shifting by the host word size is undefined according to the
404 ANSI standard, so we must handle this as a special case. */
405 *hv = 0;
406 *lv = 0;
408 else if (count >= HOST_BITS_PER_WIDE_INT)
410 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
411 *lv = 0;
413 else
415 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
416 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
417 *lv = l1 << count;
420 /* Sign extend all bits that are beyond the precision. */
422 signmask = -((prec > HOST_BITS_PER_WIDE_INT
423 ? ((unsigned HOST_WIDE_INT) *hv
424 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
425 : (*lv >> (prec - 1))) & 1);
427 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
429 else if (prec >= HOST_BITS_PER_WIDE_INT)
431 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
432 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
434 else
436 *hv = signmask;
437 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
438 *lv |= signmask << prec;
442 /* Shift the doubleword integer in L1, H1 right by COUNT places
443 keeping only PREC bits of result. COUNT must be positive.
444 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
445 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
447 void
448 rshift_double (l1, h1, count, prec, lv, hv, arith)
449 unsigned HOST_WIDE_INT l1;
450 HOST_WIDE_INT h1, count;
451 unsigned int prec;
452 unsigned HOST_WIDE_INT *lv;
453 HOST_WIDE_INT *hv;
454 int arith;
456 unsigned HOST_WIDE_INT signmask;
458 signmask = (arith
459 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
460 : 0);
462 #ifdef SHIFT_COUNT_TRUNCATED
463 if (SHIFT_COUNT_TRUNCATED)
464 count %= prec;
465 #endif
467 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
469 /* Shifting by the host word size is undefined according to the
470 ANSI standard, so we must handle this as a special case. */
471 *hv = 0;
472 *lv = 0;
474 else if (count >= HOST_BITS_PER_WIDE_INT)
476 *hv = 0;
477 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
479 else
481 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
482 *lv = ((l1 >> count)
483 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
486 /* Zero / sign extend all bits that are beyond the precision. */
488 if (count >= (HOST_WIDE_INT)prec)
490 *hv = signmask;
491 *lv = signmask;
493 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
495 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
497 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
498 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
500 else
502 *hv = signmask;
503 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
504 *lv |= signmask << (prec - count);
508 /* Rotate the doubleword integer in L1, H1 left by COUNT places
509 keeping only PREC bits of result.
510 Rotate right if COUNT is negative.
511 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
513 void
514 lrotate_double (l1, h1, count, prec, lv, hv)
515 unsigned HOST_WIDE_INT l1;
516 HOST_WIDE_INT h1, count;
517 unsigned int prec;
518 unsigned HOST_WIDE_INT *lv;
519 HOST_WIDE_INT *hv;
521 unsigned HOST_WIDE_INT s1l, s2l;
522 HOST_WIDE_INT s1h, s2h;
524 count %= prec;
525 if (count < 0)
526 count += prec;
528 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
529 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
530 *lv = s1l | s2l;
531 *hv = s1h | s2h;
534 /* Rotate the doubleword integer in L1, H1 left by COUNT places
535 keeping only PREC bits of result. COUNT must be positive.
536 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
538 void
539 rrotate_double (l1, h1, count, prec, lv, hv)
540 unsigned HOST_WIDE_INT l1;
541 HOST_WIDE_INT h1, count;
542 unsigned int prec;
543 unsigned HOST_WIDE_INT *lv;
544 HOST_WIDE_INT *hv;
546 unsigned HOST_WIDE_INT s1l, s2l;
547 HOST_WIDE_INT s1h, s2h;
549 count %= prec;
550 if (count < 0)
551 count += prec;
553 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
554 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
555 *lv = s1l | s2l;
556 *hv = s1h | s2h;
559 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
560 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
561 CODE is a tree code for a kind of division, one of
562 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
563 or EXACT_DIV_EXPR
564 It controls how the quotient is rounded to an integer.
565 Return nonzero if the operation overflows.
566 UNS nonzero says do unsigned division. */
569 div_and_round_double (code, uns,
570 lnum_orig, hnum_orig, lden_orig, hden_orig,
571 lquo, hquo, lrem, hrem)
572 enum tree_code code;
573 int uns;
574 unsigned HOST_WIDE_INT lnum_orig; /* num == numerator == dividend */
575 HOST_WIDE_INT hnum_orig;
576 unsigned HOST_WIDE_INT lden_orig; /* den == denominator == divisor */
577 HOST_WIDE_INT hden_orig;
578 unsigned HOST_WIDE_INT *lquo, *lrem;
579 HOST_WIDE_INT *hquo, *hrem;
581 int quo_neg = 0;
582 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
583 HOST_WIDE_INT den[4], quo[4];
584 int i, j;
585 unsigned HOST_WIDE_INT work;
586 unsigned HOST_WIDE_INT carry = 0;
587 unsigned HOST_WIDE_INT lnum = lnum_orig;
588 HOST_WIDE_INT hnum = hnum_orig;
589 unsigned HOST_WIDE_INT lden = lden_orig;
590 HOST_WIDE_INT hden = hden_orig;
591 int overflow = 0;
593 if (hden == 0 && lden == 0)
594 overflow = 1, lden = 1;
596 /* calculate quotient sign and convert operands to unsigned. */
597 if (!uns)
599 if (hnum < 0)
601 quo_neg = ~ quo_neg;
602 /* (minimum integer) / (-1) is the only overflow case. */
603 if (neg_double (lnum, hnum, &lnum, &hnum)
604 && ((HOST_WIDE_INT) lden & hden) == -1)
605 overflow = 1;
607 if (hden < 0)
609 quo_neg = ~ quo_neg;
610 neg_double (lden, hden, &lden, &hden);
614 if (hnum == 0 && hden == 0)
615 { /* single precision */
616 *hquo = *hrem = 0;
617 /* This unsigned division rounds toward zero. */
618 *lquo = lnum / lden;
619 goto finish_up;
622 if (hnum == 0)
623 { /* trivial case: dividend < divisor */
624 /* hden != 0 already checked. */
625 *hquo = *lquo = 0;
626 *hrem = hnum;
627 *lrem = lnum;
628 goto finish_up;
631 memset ((char *) quo, 0, sizeof quo);
633 memset ((char *) num, 0, sizeof num); /* to zero 9th element */
634 memset ((char *) den, 0, sizeof den);
636 encode (num, lnum, hnum);
637 encode (den, lden, hden);
639 /* Special code for when the divisor < BASE. */
640 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
642 /* hnum != 0 already checked. */
643 for (i = 4 - 1; i >= 0; i--)
645 work = num[i] + carry * BASE;
646 quo[i] = work / lden;
647 carry = work % lden;
650 else
652 /* Full double precision division,
653 with thanks to Don Knuth's "Seminumerical Algorithms". */
654 int num_hi_sig, den_hi_sig;
655 unsigned HOST_WIDE_INT quo_est, scale;
657 /* Find the highest nonzero divisor digit. */
658 for (i = 4 - 1;; i--)
659 if (den[i] != 0)
661 den_hi_sig = i;
662 break;
665 /* Insure that the first digit of the divisor is at least BASE/2.
666 This is required by the quotient digit estimation algorithm. */
668 scale = BASE / (den[den_hi_sig] + 1);
669 if (scale > 1)
670 { /* scale divisor and dividend */
671 carry = 0;
672 for (i = 0; i <= 4 - 1; i++)
674 work = (num[i] * scale) + carry;
675 num[i] = LOWPART (work);
676 carry = HIGHPART (work);
679 num[4] = carry;
680 carry = 0;
681 for (i = 0; i <= 4 - 1; i++)
683 work = (den[i] * scale) + carry;
684 den[i] = LOWPART (work);
685 carry = HIGHPART (work);
686 if (den[i] != 0) den_hi_sig = i;
690 num_hi_sig = 4;
692 /* Main loop */
693 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
695 /* Guess the next quotient digit, quo_est, by dividing the first
696 two remaining dividend digits by the high order quotient digit.
697 quo_est is never low and is at most 2 high. */
698 unsigned HOST_WIDE_INT tmp;
700 num_hi_sig = i + den_hi_sig + 1;
701 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
702 if (num[num_hi_sig] != den[den_hi_sig])
703 quo_est = work / den[den_hi_sig];
704 else
705 quo_est = BASE - 1;
707 /* Refine quo_est so it's usually correct, and at most one high. */
708 tmp = work - quo_est * den[den_hi_sig];
709 if (tmp < BASE
710 && (den[den_hi_sig - 1] * quo_est
711 > (tmp * BASE + num[num_hi_sig - 2])))
712 quo_est--;
714 /* Try QUO_EST as the quotient digit, by multiplying the
715 divisor by QUO_EST and subtracting from the remaining dividend.
716 Keep in mind that QUO_EST is the I - 1st digit. */
718 carry = 0;
719 for (j = 0; j <= den_hi_sig; j++)
721 work = quo_est * den[j] + carry;
722 carry = HIGHPART (work);
723 work = num[i + j] - LOWPART (work);
724 num[i + j] = LOWPART (work);
725 carry += HIGHPART (work) != 0;
728 /* If quo_est was high by one, then num[i] went negative and
729 we need to correct things. */
730 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
732 quo_est--;
733 carry = 0; /* add divisor back in */
734 for (j = 0; j <= den_hi_sig; j++)
736 work = num[i + j] + den[j] + carry;
737 carry = HIGHPART (work);
738 num[i + j] = LOWPART (work);
741 num [num_hi_sig] += carry;
744 /* Store the quotient digit. */
745 quo[i] = quo_est;
749 decode (quo, lquo, hquo);
751 finish_up:
752 /* if result is negative, make it so. */
753 if (quo_neg)
754 neg_double (*lquo, *hquo, lquo, hquo);
756 /* compute trial remainder: rem = num - (quo * den) */
757 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
758 neg_double (*lrem, *hrem, lrem, hrem);
759 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
761 switch (code)
763 case TRUNC_DIV_EXPR:
764 case TRUNC_MOD_EXPR: /* round toward zero */
765 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
766 return overflow;
768 case FLOOR_DIV_EXPR:
769 case FLOOR_MOD_EXPR: /* round toward negative infinity */
770 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
772 /* quo = quo - 1; */
773 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
774 lquo, hquo);
776 else
777 return overflow;
778 break;
780 case CEIL_DIV_EXPR:
781 case CEIL_MOD_EXPR: /* round toward positive infinity */
782 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
784 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
785 lquo, hquo);
787 else
788 return overflow;
789 break;
791 case ROUND_DIV_EXPR:
792 case ROUND_MOD_EXPR: /* round to closest integer */
794 unsigned HOST_WIDE_INT labs_rem = *lrem;
795 HOST_WIDE_INT habs_rem = *hrem;
796 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
797 HOST_WIDE_INT habs_den = hden, htwice;
799 /* Get absolute values */
800 if (*hrem < 0)
801 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
802 if (hden < 0)
803 neg_double (lden, hden, &labs_den, &habs_den);
805 /* If (2 * abs (lrem) >= abs (lden)) */
806 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
807 labs_rem, habs_rem, &ltwice, &htwice);
809 if (((unsigned HOST_WIDE_INT) habs_den
810 < (unsigned HOST_WIDE_INT) htwice)
811 || (((unsigned HOST_WIDE_INT) habs_den
812 == (unsigned HOST_WIDE_INT) htwice)
813 && (labs_den < ltwice)))
815 if (*hquo < 0)
816 /* quo = quo - 1; */
817 add_double (*lquo, *hquo,
818 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
819 else
820 /* quo = quo + 1; */
821 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
822 lquo, hquo);
824 else
825 return overflow;
827 break;
829 default:
830 abort ();
833 /* compute true remainder: rem = num - (quo * den) */
834 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
835 neg_double (*lrem, *hrem, lrem, hrem);
836 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
837 return overflow;
840 /* Determine whether an expression T can be cheaply negated using
841 the function negate_expr. */
843 static bool
844 negate_expr_p (t)
845 tree t;
847 unsigned HOST_WIDE_INT val;
848 unsigned int prec;
849 tree type;
851 if (t == 0)
852 return false;
854 type = TREE_TYPE (t);
856 STRIP_SIGN_NOPS (t);
857 switch (TREE_CODE (t))
859 case INTEGER_CST:
860 if (TREE_UNSIGNED (type))
861 return false;
863 /* Check that -CST will not overflow type. */
864 prec = TYPE_PRECISION (type);
865 if (prec > HOST_BITS_PER_WIDE_INT)
867 if (TREE_INT_CST_LOW (t) != 0)
868 return true;
869 prec -= HOST_BITS_PER_WIDE_INT;
870 val = TREE_INT_CST_HIGH (t);
872 else
873 val = TREE_INT_CST_LOW (t);
874 if (prec < HOST_BITS_PER_WIDE_INT)
875 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
876 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
878 case REAL_CST:
879 case NEGATE_EXPR:
880 case MINUS_EXPR:
881 return true;
883 default:
884 break;
886 return false;
889 /* Given T, an expression, return the negation of T. Allow for T to be
890 null, in which case return null. */
892 static tree
893 negate_expr (t)
894 tree t;
896 tree type;
897 tree tem;
899 if (t == 0)
900 return 0;
902 type = TREE_TYPE (t);
903 STRIP_SIGN_NOPS (t);
905 switch (TREE_CODE (t))
907 case INTEGER_CST:
908 case REAL_CST:
909 if (! TREE_UNSIGNED (type)
910 && 0 != (tem = fold (build1 (NEGATE_EXPR, type, t)))
911 && ! TREE_OVERFLOW (tem))
912 return tem;
913 break;
915 case NEGATE_EXPR:
916 return convert (type, TREE_OPERAND (t, 0));
918 case MINUS_EXPR:
919 /* - (A - B) -> B - A */
920 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
921 return convert (type,
922 fold (build (MINUS_EXPR, TREE_TYPE (t),
923 TREE_OPERAND (t, 1),
924 TREE_OPERAND (t, 0))));
925 break;
927 default:
928 break;
931 return convert (type, fold (build1 (NEGATE_EXPR, TREE_TYPE (t), t)));
934 /* Split a tree IN into a constant, literal and variable parts that could be
935 combined with CODE to make IN. "constant" means an expression with
936 TREE_CONSTANT but that isn't an actual constant. CODE must be a
937 commutative arithmetic operation. Store the constant part into *CONP,
938 the literal in *LITP and return the variable part. If a part isn't
939 present, set it to null. If the tree does not decompose in this way,
940 return the entire tree as the variable part and the other parts as null.
942 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
943 case, we negate an operand that was subtracted. Except if it is a
944 literal for which we use *MINUS_LITP instead.
946 If NEGATE_P is true, we are negating all of IN, again except a literal
947 for which we use *MINUS_LITP instead.
949 If IN is itself a literal or constant, return it as appropriate.
951 Note that we do not guarantee that any of the three values will be the
952 same type as IN, but they will have the same signedness and mode. */
954 static tree
955 split_tree (in, code, conp, litp, minus_litp, negate_p)
956 tree in;
957 enum tree_code code;
958 tree *conp, *litp, *minus_litp;
959 int negate_p;
961 tree var = 0;
963 *conp = 0;
964 *litp = 0;
965 *minus_litp = 0;
967 /* Strip any conversions that don't change the machine mode or signedness. */
968 STRIP_SIGN_NOPS (in);
970 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
971 *litp = in;
972 else if (TREE_CODE (in) == code
973 || (! FLOAT_TYPE_P (TREE_TYPE (in))
974 /* We can associate addition and subtraction together (even
975 though the C standard doesn't say so) for integers because
976 the value is not affected. For reals, the value might be
977 affected, so we can't. */
978 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
979 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
981 tree op0 = TREE_OPERAND (in, 0);
982 tree op1 = TREE_OPERAND (in, 1);
983 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
984 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
986 /* First see if either of the operands is a literal, then a constant. */
987 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
988 *litp = op0, op0 = 0;
989 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
990 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
992 if (op0 != 0 && TREE_CONSTANT (op0))
993 *conp = op0, op0 = 0;
994 else if (op1 != 0 && TREE_CONSTANT (op1))
995 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
997 /* If we haven't dealt with either operand, this is not a case we can
998 decompose. Otherwise, VAR is either of the ones remaining, if any. */
999 if (op0 != 0 && op1 != 0)
1000 var = in;
1001 else if (op0 != 0)
1002 var = op0;
1003 else
1004 var = op1, neg_var_p = neg1_p;
1006 /* Now do any needed negations. */
1007 if (neg_litp_p)
1008 *minus_litp = *litp, *litp = 0;
1009 if (neg_conp_p)
1010 *conp = negate_expr (*conp);
1011 if (neg_var_p)
1012 var = negate_expr (var);
1014 else if (TREE_CONSTANT (in))
1015 *conp = in;
1016 else
1017 var = in;
1019 if (negate_p)
1021 if (*litp)
1022 *minus_litp = *litp, *litp = 0;
1023 else if (*minus_litp)
1024 *litp = *minus_litp, *minus_litp = 0;
1025 *conp = negate_expr (*conp);
1026 var = negate_expr (var);
1029 return var;
1032 /* Re-associate trees split by the above function. T1 and T2 are either
1033 expressions to associate or null. Return the new expression, if any. If
1034 we build an operation, do it in TYPE and with CODE. */
1036 static tree
1037 associate_trees (t1, t2, code, type)
1038 tree t1, t2;
1039 enum tree_code code;
1040 tree type;
1042 if (t1 == 0)
1043 return t2;
1044 else if (t2 == 0)
1045 return t1;
1047 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1048 try to fold this since we will have infinite recursion. But do
1049 deal with any NEGATE_EXPRs. */
1050 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1051 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1053 if (code == PLUS_EXPR)
1055 if (TREE_CODE (t1) == NEGATE_EXPR)
1056 return build (MINUS_EXPR, type, convert (type, t2),
1057 convert (type, TREE_OPERAND (t1, 0)));
1058 else if (TREE_CODE (t2) == NEGATE_EXPR)
1059 return build (MINUS_EXPR, type, convert (type, t1),
1060 convert (type, TREE_OPERAND (t2, 0)));
1062 return build (code, type, convert (type, t1), convert (type, t2));
1065 return fold (build (code, type, convert (type, t1), convert (type, t2)));
1068 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1069 to produce a new constant.
1071 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1073 static tree
1074 int_const_binop (code, arg1, arg2, notrunc)
1075 enum tree_code code;
1076 tree arg1, arg2;
1077 int notrunc;
1079 unsigned HOST_WIDE_INT int1l, int2l;
1080 HOST_WIDE_INT int1h, int2h;
1081 unsigned HOST_WIDE_INT low;
1082 HOST_WIDE_INT hi;
1083 unsigned HOST_WIDE_INT garbagel;
1084 HOST_WIDE_INT garbageh;
1085 tree t;
1086 tree type = TREE_TYPE (arg1);
1087 int uns = TREE_UNSIGNED (type);
1088 int is_sizetype
1089 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1090 int overflow = 0;
1091 int no_overflow = 0;
1093 int1l = TREE_INT_CST_LOW (arg1);
1094 int1h = TREE_INT_CST_HIGH (arg1);
1095 int2l = TREE_INT_CST_LOW (arg2);
1096 int2h = TREE_INT_CST_HIGH (arg2);
1098 switch (code)
1100 case BIT_IOR_EXPR:
1101 low = int1l | int2l, hi = int1h | int2h;
1102 break;
1104 case BIT_XOR_EXPR:
1105 low = int1l ^ int2l, hi = int1h ^ int2h;
1106 break;
1108 case BIT_AND_EXPR:
1109 low = int1l & int2l, hi = int1h & int2h;
1110 break;
1112 case BIT_ANDTC_EXPR:
1113 low = int1l & ~int2l, hi = int1h & ~int2h;
1114 break;
1116 case RSHIFT_EXPR:
1117 int2l = -int2l;
1118 case LSHIFT_EXPR:
1119 /* It's unclear from the C standard whether shifts can overflow.
1120 The following code ignores overflow; perhaps a C standard
1121 interpretation ruling is needed. */
1122 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1123 &low, &hi, !uns);
1124 no_overflow = 1;
1125 break;
1127 case RROTATE_EXPR:
1128 int2l = - int2l;
1129 case LROTATE_EXPR:
1130 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1131 &low, &hi);
1132 break;
1134 case PLUS_EXPR:
1135 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1136 break;
1138 case MINUS_EXPR:
1139 neg_double (int2l, int2h, &low, &hi);
1140 add_double (int1l, int1h, low, hi, &low, &hi);
1141 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1142 break;
1144 case MULT_EXPR:
1145 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1146 break;
1148 case TRUNC_DIV_EXPR:
1149 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1150 case EXACT_DIV_EXPR:
1151 /* This is a shortcut for a common special case. */
1152 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1153 && ! TREE_CONSTANT_OVERFLOW (arg1)
1154 && ! TREE_CONSTANT_OVERFLOW (arg2)
1155 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1157 if (code == CEIL_DIV_EXPR)
1158 int1l += int2l - 1;
1160 low = int1l / int2l, hi = 0;
1161 break;
1164 /* ... fall through ... */
1166 case ROUND_DIV_EXPR:
1167 if (int2h == 0 && int2l == 1)
1169 low = int1l, hi = int1h;
1170 break;
1172 if (int1l == int2l && int1h == int2h
1173 && ! (int1l == 0 && int1h == 0))
1175 low = 1, hi = 0;
1176 break;
1178 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1179 &low, &hi, &garbagel, &garbageh);
1180 break;
1182 case TRUNC_MOD_EXPR:
1183 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1184 /* This is a shortcut for a common special case. */
1185 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1186 && ! TREE_CONSTANT_OVERFLOW (arg1)
1187 && ! TREE_CONSTANT_OVERFLOW (arg2)
1188 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1190 if (code == CEIL_MOD_EXPR)
1191 int1l += int2l - 1;
1192 low = int1l % int2l, hi = 0;
1193 break;
1196 /* ... fall through ... */
1198 case ROUND_MOD_EXPR:
1199 overflow = div_and_round_double (code, uns,
1200 int1l, int1h, int2l, int2h,
1201 &garbagel, &garbageh, &low, &hi);
1202 break;
1204 case MIN_EXPR:
1205 case MAX_EXPR:
1206 if (uns)
1207 low = (((unsigned HOST_WIDE_INT) int1h
1208 < (unsigned HOST_WIDE_INT) int2h)
1209 || (((unsigned HOST_WIDE_INT) int1h
1210 == (unsigned HOST_WIDE_INT) int2h)
1211 && int1l < int2l));
1212 else
1213 low = (int1h < int2h
1214 || (int1h == int2h && int1l < int2l));
1216 if (low == (code == MIN_EXPR))
1217 low = int1l, hi = int1h;
1218 else
1219 low = int2l, hi = int2h;
1220 break;
1222 default:
1223 abort ();
1226 /* If this is for a sizetype, can be represented as one (signed)
1227 HOST_WIDE_INT word, and doesn't overflow, use size_int since it caches
1228 constants. */
1229 if (is_sizetype
1230 && ((hi == 0 && (HOST_WIDE_INT) low >= 0)
1231 || (hi == -1 && (HOST_WIDE_INT) low < 0))
1232 && overflow == 0 && ! TREE_OVERFLOW (arg1) && ! TREE_OVERFLOW (arg2))
1233 return size_int_type_wide (low, type);
1234 else
1236 t = build_int_2 (low, hi);
1237 TREE_TYPE (t) = TREE_TYPE (arg1);
1240 TREE_OVERFLOW (t)
1241 = ((notrunc
1242 ? (!uns || is_sizetype) && overflow
1243 : (force_fit_type (t, (!uns || is_sizetype) && overflow)
1244 && ! no_overflow))
1245 | TREE_OVERFLOW (arg1)
1246 | TREE_OVERFLOW (arg2));
1248 /* If we're doing a size calculation, unsigned arithmetic does overflow.
1249 So check if force_fit_type truncated the value. */
1250 if (is_sizetype
1251 && ! TREE_OVERFLOW (t)
1252 && (TREE_INT_CST_HIGH (t) != hi
1253 || TREE_INT_CST_LOW (t) != low))
1254 TREE_OVERFLOW (t) = 1;
1256 TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1257 | TREE_CONSTANT_OVERFLOW (arg1)
1258 | TREE_CONSTANT_OVERFLOW (arg2));
1259 return t;
1262 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1263 constant. We assume ARG1 and ARG2 have the same data type, or at least
1264 are the same kind of constant and the same machine mode.
1266 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1268 static tree
1269 const_binop (code, arg1, arg2, notrunc)
1270 enum tree_code code;
1271 tree arg1, arg2;
1272 int notrunc;
1274 STRIP_NOPS (arg1);
1275 STRIP_NOPS (arg2);
1277 if (TREE_CODE (arg1) == INTEGER_CST)
1278 return int_const_binop (code, arg1, arg2, notrunc);
1280 if (TREE_CODE (arg1) == REAL_CST)
1282 REAL_VALUE_TYPE d1;
1283 REAL_VALUE_TYPE d2;
1284 REAL_VALUE_TYPE value;
1285 tree t;
1287 d1 = TREE_REAL_CST (arg1);
1288 d2 = TREE_REAL_CST (arg2);
1290 /* If either operand is a NaN, just return it. Otherwise, set up
1291 for floating-point trap; we return an overflow. */
1292 if (REAL_VALUE_ISNAN (d1))
1293 return arg1;
1294 else if (REAL_VALUE_ISNAN (d2))
1295 return arg2;
1297 REAL_ARITHMETIC (value, code, d1, d2);
1299 t = build_real (TREE_TYPE (arg1),
1300 real_value_truncate (TYPE_MODE (TREE_TYPE (arg1)),
1301 value));
1303 TREE_OVERFLOW (t)
1304 = (force_fit_type (t, 0)
1305 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1306 TREE_CONSTANT_OVERFLOW (t)
1307 = TREE_OVERFLOW (t)
1308 | TREE_CONSTANT_OVERFLOW (arg1)
1309 | TREE_CONSTANT_OVERFLOW (arg2);
1310 return t;
1312 if (TREE_CODE (arg1) == COMPLEX_CST)
1314 tree type = TREE_TYPE (arg1);
1315 tree r1 = TREE_REALPART (arg1);
1316 tree i1 = TREE_IMAGPART (arg1);
1317 tree r2 = TREE_REALPART (arg2);
1318 tree i2 = TREE_IMAGPART (arg2);
1319 tree t;
1321 switch (code)
1323 case PLUS_EXPR:
1324 t = build_complex (type,
1325 const_binop (PLUS_EXPR, r1, r2, notrunc),
1326 const_binop (PLUS_EXPR, i1, i2, notrunc));
1327 break;
1329 case MINUS_EXPR:
1330 t = build_complex (type,
1331 const_binop (MINUS_EXPR, r1, r2, notrunc),
1332 const_binop (MINUS_EXPR, i1, i2, notrunc));
1333 break;
1335 case MULT_EXPR:
1336 t = build_complex (type,
1337 const_binop (MINUS_EXPR,
1338 const_binop (MULT_EXPR,
1339 r1, r2, notrunc),
1340 const_binop (MULT_EXPR,
1341 i1, i2, notrunc),
1342 notrunc),
1343 const_binop (PLUS_EXPR,
1344 const_binop (MULT_EXPR,
1345 r1, i2, notrunc),
1346 const_binop (MULT_EXPR,
1347 i1, r2, notrunc),
1348 notrunc));
1349 break;
1351 case RDIV_EXPR:
1353 tree magsquared
1354 = const_binop (PLUS_EXPR,
1355 const_binop (MULT_EXPR, r2, r2, notrunc),
1356 const_binop (MULT_EXPR, i2, i2, notrunc),
1357 notrunc);
1359 t = build_complex (type,
1360 const_binop
1361 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1362 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1363 const_binop (PLUS_EXPR,
1364 const_binop (MULT_EXPR, r1, r2,
1365 notrunc),
1366 const_binop (MULT_EXPR, i1, i2,
1367 notrunc),
1368 notrunc),
1369 magsquared, notrunc),
1370 const_binop
1371 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1372 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1373 const_binop (MINUS_EXPR,
1374 const_binop (MULT_EXPR, i1, r2,
1375 notrunc),
1376 const_binop (MULT_EXPR, r1, i2,
1377 notrunc),
1378 notrunc),
1379 magsquared, notrunc));
1381 break;
1383 default:
1384 abort ();
1386 return t;
1388 return 0;
1391 /* These are the hash table functions for the hash table of INTEGER_CST
1392 nodes of a sizetype. */
1394 /* Return the hash code code X, an INTEGER_CST. */
1396 static hashval_t
1397 size_htab_hash (x)
1398 const void *x;
1400 tree t = (tree) x;
1402 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1403 ^ htab_hash_pointer (TREE_TYPE (t))
1404 ^ (TREE_OVERFLOW (t) << 20));
1407 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1408 is the same as that given by *Y, which is the same. */
1410 static int
1411 size_htab_eq (x, y)
1412 const void *x;
1413 const void *y;
1415 tree xt = (tree) x;
1416 tree yt = (tree) y;
1418 return (TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1419 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt)
1420 && TREE_TYPE (xt) == TREE_TYPE (yt)
1421 && TREE_OVERFLOW (xt) == TREE_OVERFLOW (yt));
1424 /* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
1425 bits are given by NUMBER and of the sizetype represented by KIND. */
1427 tree
1428 size_int_wide (number, kind)
1429 HOST_WIDE_INT number;
1430 enum size_type_kind kind;
1432 return size_int_type_wide (number, sizetype_tab[(int) kind]);
1435 /* Likewise, but the desired type is specified explicitly. */
1437 static GTY (()) tree new_const;
1438 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
1439 htab_t size_htab;
1441 tree
1442 size_int_type_wide (number, type)
1443 HOST_WIDE_INT number;
1444 tree type;
1446 PTR *slot;
1448 if (size_htab == 0)
1450 size_htab = htab_create_ggc (1024, size_htab_hash, size_htab_eq, NULL);
1451 new_const = make_node (INTEGER_CST);
1454 /* Adjust NEW_CONST to be the constant we want. If it's already in the
1455 hash table, we return the value from the hash table. Otherwise, we
1456 place that in the hash table and make a new node for the next time. */
1457 TREE_INT_CST_LOW (new_const) = number;
1458 TREE_INT_CST_HIGH (new_const) = number < 0 ? -1 : 0;
1459 TREE_TYPE (new_const) = type;
1460 TREE_OVERFLOW (new_const) = TREE_CONSTANT_OVERFLOW (new_const)
1461 = force_fit_type (new_const, 0);
1463 slot = htab_find_slot (size_htab, new_const, INSERT);
1464 if (*slot == 0)
1466 tree t = new_const;
1468 *slot = (PTR) new_const;
1469 new_const = make_node (INTEGER_CST);
1470 return t;
1472 else
1473 return (tree) *slot;
1476 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1477 is a tree code. The type of the result is taken from the operands.
1478 Both must be the same type integer type and it must be a size type.
1479 If the operands are constant, so is the result. */
1481 tree
1482 size_binop (code, arg0, arg1)
1483 enum tree_code code;
1484 tree arg0, arg1;
1486 tree type = TREE_TYPE (arg0);
1488 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1489 || type != TREE_TYPE (arg1))
1490 abort ();
1492 /* Handle the special case of two integer constants faster. */
1493 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1495 /* And some specific cases even faster than that. */
1496 if (code == PLUS_EXPR && integer_zerop (arg0))
1497 return arg1;
1498 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1499 && integer_zerop (arg1))
1500 return arg0;
1501 else if (code == MULT_EXPR && integer_onep (arg0))
1502 return arg1;
1504 /* Handle general case of two integer constants. */
1505 return int_const_binop (code, arg0, arg1, 0);
1508 if (arg0 == error_mark_node || arg1 == error_mark_node)
1509 return error_mark_node;
1511 return fold (build (code, type, arg0, arg1));
1514 /* Given two values, either both of sizetype or both of bitsizetype,
1515 compute the difference between the two values. Return the value
1516 in signed type corresponding to the type of the operands. */
1518 tree
1519 size_diffop (arg0, arg1)
1520 tree arg0, arg1;
1522 tree type = TREE_TYPE (arg0);
1523 tree ctype;
1525 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1526 || type != TREE_TYPE (arg1))
1527 abort ();
1529 /* If the type is already signed, just do the simple thing. */
1530 if (! TREE_UNSIGNED (type))
1531 return size_binop (MINUS_EXPR, arg0, arg1);
1533 ctype = (type == bitsizetype || type == ubitsizetype
1534 ? sbitsizetype : ssizetype);
1536 /* If either operand is not a constant, do the conversions to the signed
1537 type and subtract. The hardware will do the right thing with any
1538 overflow in the subtraction. */
1539 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1540 return size_binop (MINUS_EXPR, convert (ctype, arg0),
1541 convert (ctype, arg1));
1543 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1544 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1545 overflow) and negate (which can't either). Special-case a result
1546 of zero while we're here. */
1547 if (tree_int_cst_equal (arg0, arg1))
1548 return convert (ctype, integer_zero_node);
1549 else if (tree_int_cst_lt (arg1, arg0))
1550 return convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1551 else
1552 return size_binop (MINUS_EXPR, convert (ctype, integer_zero_node),
1553 convert (ctype, size_binop (MINUS_EXPR, arg1, arg0)));
1557 /* Given T, a tree representing type conversion of ARG1, a constant,
1558 return a constant tree representing the result of conversion. */
1560 static tree
1561 fold_convert (t, arg1)
1562 tree t;
1563 tree arg1;
1565 tree type = TREE_TYPE (t);
1566 int overflow = 0;
1568 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
1570 if (TREE_CODE (arg1) == INTEGER_CST)
1572 /* If we would build a constant wider than GCC supports,
1573 leave the conversion unfolded. */
1574 if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
1575 return t;
1577 /* If we are trying to make a sizetype for a small integer, use
1578 size_int to pick up cached types to reduce duplicate nodes. */
1579 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
1580 && !TREE_CONSTANT_OVERFLOW (arg1)
1581 && compare_tree_int (arg1, 10000) < 0)
1582 return size_int_type_wide (TREE_INT_CST_LOW (arg1), type);
1584 /* Given an integer constant, make new constant with new type,
1585 appropriately sign-extended or truncated. */
1586 t = build_int_2 (TREE_INT_CST_LOW (arg1),
1587 TREE_INT_CST_HIGH (arg1));
1588 TREE_TYPE (t) = type;
1589 /* Indicate an overflow if (1) ARG1 already overflowed,
1590 or (2) force_fit_type indicates an overflow.
1591 Tell force_fit_type that an overflow has already occurred
1592 if ARG1 is a too-large unsigned value and T is signed.
1593 But don't indicate an overflow if converting a pointer. */
1594 TREE_OVERFLOW (t)
1595 = ((force_fit_type (t,
1596 (TREE_INT_CST_HIGH (arg1) < 0
1597 && (TREE_UNSIGNED (type)
1598 < TREE_UNSIGNED (TREE_TYPE (arg1)))))
1599 && ! POINTER_TYPE_P (TREE_TYPE (arg1)))
1600 || TREE_OVERFLOW (arg1));
1601 TREE_CONSTANT_OVERFLOW (t)
1602 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1604 else if (TREE_CODE (arg1) == REAL_CST)
1606 /* Don't initialize these, use assignments.
1607 Initialized local aggregates don't work on old compilers. */
1608 REAL_VALUE_TYPE x;
1609 REAL_VALUE_TYPE l;
1610 REAL_VALUE_TYPE u;
1611 tree type1 = TREE_TYPE (arg1);
1612 int no_upper_bound;
1614 x = TREE_REAL_CST (arg1);
1615 l = real_value_from_int_cst (type1, TYPE_MIN_VALUE (type));
1617 no_upper_bound = (TYPE_MAX_VALUE (type) == NULL);
1618 if (!no_upper_bound)
1619 u = real_value_from_int_cst (type1, TYPE_MAX_VALUE (type));
1621 /* See if X will be in range after truncation towards 0.
1622 To compensate for truncation, move the bounds away from 0,
1623 but reject if X exactly equals the adjusted bounds. */
1624 REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
1625 if (!no_upper_bound)
1626 REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
1627 /* If X is a NaN, use zero instead and show we have an overflow.
1628 Otherwise, range check. */
1629 if (REAL_VALUE_ISNAN (x))
1630 overflow = 1, x = dconst0;
1631 else if (! (REAL_VALUES_LESS (l, x)
1632 && !no_upper_bound
1633 && REAL_VALUES_LESS (x, u)))
1634 overflow = 1;
1637 HOST_WIDE_INT low, high;
1638 REAL_VALUE_TO_INT (&low, &high, x);
1639 t = build_int_2 (low, high);
1641 TREE_TYPE (t) = type;
1642 TREE_OVERFLOW (t)
1643 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
1644 TREE_CONSTANT_OVERFLOW (t)
1645 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1647 TREE_TYPE (t) = type;
1649 else if (TREE_CODE (type) == REAL_TYPE)
1651 if (TREE_CODE (arg1) == INTEGER_CST)
1652 return build_real_from_int_cst (type, arg1);
1653 if (TREE_CODE (arg1) == REAL_CST)
1655 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
1657 /* We make a copy of ARG1 so that we don't modify an
1658 existing constant tree. */
1659 t = copy_node (arg1);
1660 TREE_TYPE (t) = type;
1661 return t;
1664 t = build_real (type,
1665 real_value_truncate (TYPE_MODE (type),
1666 TREE_REAL_CST (arg1)));
1668 TREE_OVERFLOW (t)
1669 = TREE_OVERFLOW (arg1) | force_fit_type (t, 0);
1670 TREE_CONSTANT_OVERFLOW (t)
1671 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1672 return t;
1675 TREE_CONSTANT (t) = 1;
1676 return t;
1679 /* Return an expr equal to X but certainly not valid as an lvalue. */
1681 tree
1682 non_lvalue (x)
1683 tree x;
1685 tree result;
1687 /* These things are certainly not lvalues. */
1688 if (TREE_CODE (x) == NON_LVALUE_EXPR
1689 || TREE_CODE (x) == INTEGER_CST
1690 || TREE_CODE (x) == REAL_CST
1691 || TREE_CODE (x) == STRING_CST
1692 || TREE_CODE (x) == ADDR_EXPR)
1693 return x;
1695 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
1696 TREE_CONSTANT (result) = TREE_CONSTANT (x);
1697 return result;
1700 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
1701 Zero means allow extended lvalues. */
1703 int pedantic_lvalues;
1705 /* When pedantic, return an expr equal to X but certainly not valid as a
1706 pedantic lvalue. Otherwise, return X. */
1708 tree
1709 pedantic_non_lvalue (x)
1710 tree x;
1712 if (pedantic_lvalues)
1713 return non_lvalue (x);
1714 else
1715 return x;
1718 /* Given a tree comparison code, return the code that is the logical inverse
1719 of the given code. It is not safe to do this for floating-point
1720 comparisons, except for NE_EXPR and EQ_EXPR. */
1722 static enum tree_code
1723 invert_tree_comparison (code)
1724 enum tree_code code;
1726 switch (code)
1728 case EQ_EXPR:
1729 return NE_EXPR;
1730 case NE_EXPR:
1731 return EQ_EXPR;
1732 case GT_EXPR:
1733 return LE_EXPR;
1734 case GE_EXPR:
1735 return LT_EXPR;
1736 case LT_EXPR:
1737 return GE_EXPR;
1738 case LE_EXPR:
1739 return GT_EXPR;
1740 default:
1741 abort ();
1745 /* Similar, but return the comparison that results if the operands are
1746 swapped. This is safe for floating-point. */
1748 static enum tree_code
1749 swap_tree_comparison (code)
1750 enum tree_code code;
1752 switch (code)
1754 case EQ_EXPR:
1755 case NE_EXPR:
1756 return code;
1757 case GT_EXPR:
1758 return LT_EXPR;
1759 case GE_EXPR:
1760 return LE_EXPR;
1761 case LT_EXPR:
1762 return GT_EXPR;
1763 case LE_EXPR:
1764 return GE_EXPR;
1765 default:
1766 abort ();
1771 /* Convert a comparison tree code from an enum tree_code representation
1772 into a compcode bit-based encoding. This function is the inverse of
1773 compcode_to_comparison. */
1775 static int
1776 comparison_to_compcode (code)
1777 enum tree_code code;
1779 switch (code)
1781 case LT_EXPR:
1782 return COMPCODE_LT;
1783 case EQ_EXPR:
1784 return COMPCODE_EQ;
1785 case LE_EXPR:
1786 return COMPCODE_LE;
1787 case GT_EXPR:
1788 return COMPCODE_GT;
1789 case NE_EXPR:
1790 return COMPCODE_NE;
1791 case GE_EXPR:
1792 return COMPCODE_GE;
1793 default:
1794 abort ();
1798 /* Convert a compcode bit-based encoding of a comparison operator back
1799 to GCC's enum tree_code representation. This function is the
1800 inverse of comparison_to_compcode. */
1802 static enum tree_code
1803 compcode_to_comparison (code)
1804 int code;
1806 switch (code)
1808 case COMPCODE_LT:
1809 return LT_EXPR;
1810 case COMPCODE_EQ:
1811 return EQ_EXPR;
1812 case COMPCODE_LE:
1813 return LE_EXPR;
1814 case COMPCODE_GT:
1815 return GT_EXPR;
1816 case COMPCODE_NE:
1817 return NE_EXPR;
1818 case COMPCODE_GE:
1819 return GE_EXPR;
1820 default:
1821 abort ();
1825 /* Return nonzero if CODE is a tree code that represents a truth value. */
1827 static int
1828 truth_value_p (code)
1829 enum tree_code code;
1831 return (TREE_CODE_CLASS (code) == '<'
1832 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
1833 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
1834 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
1837 /* Return nonzero if two operands are necessarily equal.
1838 If ONLY_CONST is nonzero, only return nonzero for constants.
1839 This function tests whether the operands are indistinguishable;
1840 it does not test whether they are equal using C's == operation.
1841 The distinction is important for IEEE floating point, because
1842 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
1843 (2) two NaNs may be indistinguishable, but NaN!=NaN. */
1846 operand_equal_p (arg0, arg1, only_const)
1847 tree arg0, arg1;
1848 int only_const;
1850 /* If both types don't have the same signedness, then we can't consider
1851 them equal. We must check this before the STRIP_NOPS calls
1852 because they may change the signedness of the arguments. */
1853 if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
1854 return 0;
1856 STRIP_NOPS (arg0);
1857 STRIP_NOPS (arg1);
1859 if (TREE_CODE (arg0) != TREE_CODE (arg1)
1860 /* This is needed for conversions and for COMPONENT_REF.
1861 Might as well play it safe and always test this. */
1862 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
1863 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
1864 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
1865 return 0;
1867 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
1868 We don't care about side effects in that case because the SAVE_EXPR
1869 takes care of that for us. In all other cases, two expressions are
1870 equal if they have no side effects. If we have two identical
1871 expressions with side effects that should be treated the same due
1872 to the only side effects being identical SAVE_EXPR's, that will
1873 be detected in the recursive calls below. */
1874 if (arg0 == arg1 && ! only_const
1875 && (TREE_CODE (arg0) == SAVE_EXPR
1876 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
1877 return 1;
1879 /* Next handle constant cases, those for which we can return 1 even
1880 if ONLY_CONST is set. */
1881 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
1882 switch (TREE_CODE (arg0))
1884 case INTEGER_CST:
1885 return (! TREE_CONSTANT_OVERFLOW (arg0)
1886 && ! TREE_CONSTANT_OVERFLOW (arg1)
1887 && tree_int_cst_equal (arg0, arg1));
1889 case REAL_CST:
1890 return (! TREE_CONSTANT_OVERFLOW (arg0)
1891 && ! TREE_CONSTANT_OVERFLOW (arg1)
1892 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
1893 TREE_REAL_CST (arg1)));
1895 case VECTOR_CST:
1897 tree v1, v2;
1899 if (TREE_CONSTANT_OVERFLOW (arg0)
1900 || TREE_CONSTANT_OVERFLOW (arg1))
1901 return 0;
1903 v1 = TREE_VECTOR_CST_ELTS (arg0);
1904 v2 = TREE_VECTOR_CST_ELTS (arg1);
1905 while (v1 && v2)
1907 if (!operand_equal_p (v1, v2, only_const))
1908 return 0;
1909 v1 = TREE_CHAIN (v1);
1910 v2 = TREE_CHAIN (v2);
1913 return 1;
1916 case COMPLEX_CST:
1917 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
1918 only_const)
1919 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
1920 only_const));
1922 case STRING_CST:
1923 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
1924 && ! memcmp (TREE_STRING_POINTER (arg0),
1925 TREE_STRING_POINTER (arg1),
1926 TREE_STRING_LENGTH (arg0)));
1928 case ADDR_EXPR:
1929 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
1931 default:
1932 break;
1935 if (only_const)
1936 return 0;
1938 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
1940 case '1':
1941 /* Two conversions are equal only if signedness and modes match. */
1942 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
1943 && (TREE_UNSIGNED (TREE_TYPE (arg0))
1944 != TREE_UNSIGNED (TREE_TYPE (arg1))))
1945 return 0;
1947 return operand_equal_p (TREE_OPERAND (arg0, 0),
1948 TREE_OPERAND (arg1, 0), 0);
1950 case '<':
1951 case '2':
1952 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)
1953 && operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1),
1955 return 1;
1957 /* For commutative ops, allow the other order. */
1958 return ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MULT_EXPR
1959 || TREE_CODE (arg0) == MIN_EXPR || TREE_CODE (arg0) == MAX_EXPR
1960 || TREE_CODE (arg0) == BIT_IOR_EXPR
1961 || TREE_CODE (arg0) == BIT_XOR_EXPR
1962 || TREE_CODE (arg0) == BIT_AND_EXPR
1963 || TREE_CODE (arg0) == NE_EXPR || TREE_CODE (arg0) == EQ_EXPR)
1964 && operand_equal_p (TREE_OPERAND (arg0, 0),
1965 TREE_OPERAND (arg1, 1), 0)
1966 && operand_equal_p (TREE_OPERAND (arg0, 1),
1967 TREE_OPERAND (arg1, 0), 0));
1969 case 'r':
1970 /* If either of the pointer (or reference) expressions we are dereferencing
1971 contain a side effect, these cannot be equal. */
1972 if (TREE_SIDE_EFFECTS (arg0)
1973 || TREE_SIDE_EFFECTS (arg1))
1974 return 0;
1976 switch (TREE_CODE (arg0))
1978 case INDIRECT_REF:
1979 return operand_equal_p (TREE_OPERAND (arg0, 0),
1980 TREE_OPERAND (arg1, 0), 0);
1982 case COMPONENT_REF:
1983 case ARRAY_REF:
1984 case ARRAY_RANGE_REF:
1985 return (operand_equal_p (TREE_OPERAND (arg0, 0),
1986 TREE_OPERAND (arg1, 0), 0)
1987 && operand_equal_p (TREE_OPERAND (arg0, 1),
1988 TREE_OPERAND (arg1, 1), 0));
1990 case BIT_FIELD_REF:
1991 return (operand_equal_p (TREE_OPERAND (arg0, 0),
1992 TREE_OPERAND (arg1, 0), 0)
1993 && operand_equal_p (TREE_OPERAND (arg0, 1),
1994 TREE_OPERAND (arg1, 1), 0)
1995 && operand_equal_p (TREE_OPERAND (arg0, 2),
1996 TREE_OPERAND (arg1, 2), 0));
1997 default:
1998 return 0;
2001 case 'e':
2002 if (TREE_CODE (arg0) == RTL_EXPR)
2003 return rtx_equal_p (RTL_EXPR_RTL (arg0), RTL_EXPR_RTL (arg1));
2004 return 0;
2006 default:
2007 return 0;
2011 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2012 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2014 When in doubt, return 0. */
2016 static int
2017 operand_equal_for_comparison_p (arg0, arg1, other)
2018 tree arg0, arg1;
2019 tree other;
2021 int unsignedp1, unsignedpo;
2022 tree primarg0, primarg1, primother;
2023 unsigned int correct_width;
2025 if (operand_equal_p (arg0, arg1, 0))
2026 return 1;
2028 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2029 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2030 return 0;
2032 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2033 and see if the inner values are the same. This removes any
2034 signedness comparison, which doesn't matter here. */
2035 primarg0 = arg0, primarg1 = arg1;
2036 STRIP_NOPS (primarg0);
2037 STRIP_NOPS (primarg1);
2038 if (operand_equal_p (primarg0, primarg1, 0))
2039 return 1;
2041 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2042 actual comparison operand, ARG0.
2044 First throw away any conversions to wider types
2045 already present in the operands. */
2047 primarg1 = get_narrower (arg1, &unsignedp1);
2048 primother = get_narrower (other, &unsignedpo);
2050 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2051 if (unsignedp1 == unsignedpo
2052 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2053 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2055 tree type = TREE_TYPE (arg0);
2057 /* Make sure shorter operand is extended the right way
2058 to match the longer operand. */
2059 primarg1 = convert ((*lang_hooks.types.signed_or_unsigned_type)
2060 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2062 if (operand_equal_p (arg0, convert (type, primarg1), 0))
2063 return 1;
2066 return 0;
2069 /* See if ARG is an expression that is either a comparison or is performing
2070 arithmetic on comparisons. The comparisons must only be comparing
2071 two different values, which will be stored in *CVAL1 and *CVAL2; if
2072 they are nonzero it means that some operands have already been found.
2073 No variables may be used anywhere else in the expression except in the
2074 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2075 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2077 If this is true, return 1. Otherwise, return zero. */
2079 static int
2080 twoval_comparison_p (arg, cval1, cval2, save_p)
2081 tree arg;
2082 tree *cval1, *cval2;
2083 int *save_p;
2085 enum tree_code code = TREE_CODE (arg);
2086 char class = TREE_CODE_CLASS (code);
2088 /* We can handle some of the 'e' cases here. */
2089 if (class == 'e' && code == TRUTH_NOT_EXPR)
2090 class = '1';
2091 else if (class == 'e'
2092 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2093 || code == COMPOUND_EXPR))
2094 class = '2';
2096 else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0
2097 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2099 /* If we've already found a CVAL1 or CVAL2, this expression is
2100 two complex to handle. */
2101 if (*cval1 || *cval2)
2102 return 0;
2104 class = '1';
2105 *save_p = 1;
2108 switch (class)
2110 case '1':
2111 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2113 case '2':
2114 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2115 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2116 cval1, cval2, save_p));
2118 case 'c':
2119 return 1;
2121 case 'e':
2122 if (code == COND_EXPR)
2123 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2124 cval1, cval2, save_p)
2125 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2126 cval1, cval2, save_p)
2127 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2128 cval1, cval2, save_p));
2129 return 0;
2131 case '<':
2132 /* First see if we can handle the first operand, then the second. For
2133 the second operand, we know *CVAL1 can't be zero. It must be that
2134 one side of the comparison is each of the values; test for the
2135 case where this isn't true by failing if the two operands
2136 are the same. */
2138 if (operand_equal_p (TREE_OPERAND (arg, 0),
2139 TREE_OPERAND (arg, 1), 0))
2140 return 0;
2142 if (*cval1 == 0)
2143 *cval1 = TREE_OPERAND (arg, 0);
2144 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2146 else if (*cval2 == 0)
2147 *cval2 = TREE_OPERAND (arg, 0);
2148 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2150 else
2151 return 0;
2153 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2155 else if (*cval2 == 0)
2156 *cval2 = TREE_OPERAND (arg, 1);
2157 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2159 else
2160 return 0;
2162 return 1;
2164 default:
2165 return 0;
2169 /* ARG is a tree that is known to contain just arithmetic operations and
2170 comparisons. Evaluate the operations in the tree substituting NEW0 for
2171 any occurrence of OLD0 as an operand of a comparison and likewise for
2172 NEW1 and OLD1. */
2174 static tree
2175 eval_subst (arg, old0, new0, old1, new1)
2176 tree arg;
2177 tree old0, new0, old1, new1;
2179 tree type = TREE_TYPE (arg);
2180 enum tree_code code = TREE_CODE (arg);
2181 char class = TREE_CODE_CLASS (code);
2183 /* We can handle some of the 'e' cases here. */
2184 if (class == 'e' && code == TRUTH_NOT_EXPR)
2185 class = '1';
2186 else if (class == 'e'
2187 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2188 class = '2';
2190 switch (class)
2192 case '1':
2193 return fold (build1 (code, type,
2194 eval_subst (TREE_OPERAND (arg, 0),
2195 old0, new0, old1, new1)));
2197 case '2':
2198 return fold (build (code, type,
2199 eval_subst (TREE_OPERAND (arg, 0),
2200 old0, new0, old1, new1),
2201 eval_subst (TREE_OPERAND (arg, 1),
2202 old0, new0, old1, new1)));
2204 case 'e':
2205 switch (code)
2207 case SAVE_EXPR:
2208 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2210 case COMPOUND_EXPR:
2211 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2213 case COND_EXPR:
2214 return fold (build (code, type,
2215 eval_subst (TREE_OPERAND (arg, 0),
2216 old0, new0, old1, new1),
2217 eval_subst (TREE_OPERAND (arg, 1),
2218 old0, new0, old1, new1),
2219 eval_subst (TREE_OPERAND (arg, 2),
2220 old0, new0, old1, new1)));
2221 default:
2222 break;
2224 /* fall through - ??? */
2226 case '<':
2228 tree arg0 = TREE_OPERAND (arg, 0);
2229 tree arg1 = TREE_OPERAND (arg, 1);
2231 /* We need to check both for exact equality and tree equality. The
2232 former will be true if the operand has a side-effect. In that
2233 case, we know the operand occurred exactly once. */
2235 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2236 arg0 = new0;
2237 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2238 arg0 = new1;
2240 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2241 arg1 = new0;
2242 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2243 arg1 = new1;
2245 return fold (build (code, type, arg0, arg1));
2248 default:
2249 return arg;
2253 /* Return a tree for the case when the result of an expression is RESULT
2254 converted to TYPE and OMITTED was previously an operand of the expression
2255 but is now not needed (e.g., we folded OMITTED * 0).
2257 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2258 the conversion of RESULT to TYPE. */
2260 tree
2261 omit_one_operand (type, result, omitted)
2262 tree type, result, omitted;
2264 tree t = convert (type, result);
2266 if (TREE_SIDE_EFFECTS (omitted))
2267 return build (COMPOUND_EXPR, type, omitted, t);
2269 return non_lvalue (t);
2272 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2274 static tree
2275 pedantic_omit_one_operand (type, result, omitted)
2276 tree type, result, omitted;
2278 tree t = convert (type, result);
2280 if (TREE_SIDE_EFFECTS (omitted))
2281 return build (COMPOUND_EXPR, type, omitted, t);
2283 return pedantic_non_lvalue (t);
2286 /* Return a simplified tree node for the truth-negation of ARG. This
2287 never alters ARG itself. We assume that ARG is an operation that
2288 returns a truth value (0 or 1). */
2290 tree
2291 invert_truthvalue (arg)
2292 tree arg;
2294 tree type = TREE_TYPE (arg);
2295 enum tree_code code = TREE_CODE (arg);
2297 if (code == ERROR_MARK)
2298 return arg;
2300 /* If this is a comparison, we can simply invert it, except for
2301 floating-point non-equality comparisons, in which case we just
2302 enclose a TRUTH_NOT_EXPR around what we have. */
2304 if (TREE_CODE_CLASS (code) == '<')
2306 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
2307 && !flag_unsafe_math_optimizations
2308 && code != NE_EXPR
2309 && code != EQ_EXPR)
2310 return build1 (TRUTH_NOT_EXPR, type, arg);
2311 else
2312 return build (invert_tree_comparison (code), type,
2313 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2316 switch (code)
2318 case INTEGER_CST:
2319 return convert (type, build_int_2 (integer_zerop (arg), 0));
2321 case TRUTH_AND_EXPR:
2322 return build (TRUTH_OR_EXPR, type,
2323 invert_truthvalue (TREE_OPERAND (arg, 0)),
2324 invert_truthvalue (TREE_OPERAND (arg, 1)));
2326 case TRUTH_OR_EXPR:
2327 return build (TRUTH_AND_EXPR, type,
2328 invert_truthvalue (TREE_OPERAND (arg, 0)),
2329 invert_truthvalue (TREE_OPERAND (arg, 1)));
2331 case TRUTH_XOR_EXPR:
2332 /* Here we can invert either operand. We invert the first operand
2333 unless the second operand is a TRUTH_NOT_EXPR in which case our
2334 result is the XOR of the first operand with the inside of the
2335 negation of the second operand. */
2337 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2338 return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2339 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2340 else
2341 return build (TRUTH_XOR_EXPR, type,
2342 invert_truthvalue (TREE_OPERAND (arg, 0)),
2343 TREE_OPERAND (arg, 1));
2345 case TRUTH_ANDIF_EXPR:
2346 return build (TRUTH_ORIF_EXPR, type,
2347 invert_truthvalue (TREE_OPERAND (arg, 0)),
2348 invert_truthvalue (TREE_OPERAND (arg, 1)));
2350 case TRUTH_ORIF_EXPR:
2351 return build (TRUTH_ANDIF_EXPR, type,
2352 invert_truthvalue (TREE_OPERAND (arg, 0)),
2353 invert_truthvalue (TREE_OPERAND (arg, 1)));
2355 case TRUTH_NOT_EXPR:
2356 return TREE_OPERAND (arg, 0);
2358 case COND_EXPR:
2359 return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2360 invert_truthvalue (TREE_OPERAND (arg, 1)),
2361 invert_truthvalue (TREE_OPERAND (arg, 2)));
2363 case COMPOUND_EXPR:
2364 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2365 invert_truthvalue (TREE_OPERAND (arg, 1)));
2367 case WITH_RECORD_EXPR:
2368 return build (WITH_RECORD_EXPR, type,
2369 invert_truthvalue (TREE_OPERAND (arg, 0)),
2370 TREE_OPERAND (arg, 1));
2372 case NON_LVALUE_EXPR:
2373 return invert_truthvalue (TREE_OPERAND (arg, 0));
2375 case NOP_EXPR:
2376 case CONVERT_EXPR:
2377 case FLOAT_EXPR:
2378 return build1 (TREE_CODE (arg), type,
2379 invert_truthvalue (TREE_OPERAND (arg, 0)));
2381 case BIT_AND_EXPR:
2382 if (!integer_onep (TREE_OPERAND (arg, 1)))
2383 break;
2384 return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
2386 case SAVE_EXPR:
2387 return build1 (TRUTH_NOT_EXPR, type, arg);
2389 case CLEANUP_POINT_EXPR:
2390 return build1 (CLEANUP_POINT_EXPR, type,
2391 invert_truthvalue (TREE_OPERAND (arg, 0)));
2393 default:
2394 break;
2396 if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
2397 abort ();
2398 return build1 (TRUTH_NOT_EXPR, type, arg);
2401 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2402 operands are another bit-wise operation with a common input. If so,
2403 distribute the bit operations to save an operation and possibly two if
2404 constants are involved. For example, convert
2405 (A | B) & (A | C) into A | (B & C)
2406 Further simplification will occur if B and C are constants.
2408 If this optimization cannot be done, 0 will be returned. */
2410 static tree
2411 distribute_bit_expr (code, type, arg0, arg1)
2412 enum tree_code code;
2413 tree type;
2414 tree arg0, arg1;
2416 tree common;
2417 tree left, right;
2419 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2420 || TREE_CODE (arg0) == code
2421 || (TREE_CODE (arg0) != BIT_AND_EXPR
2422 && TREE_CODE (arg0) != BIT_IOR_EXPR))
2423 return 0;
2425 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2427 common = TREE_OPERAND (arg0, 0);
2428 left = TREE_OPERAND (arg0, 1);
2429 right = TREE_OPERAND (arg1, 1);
2431 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2433 common = TREE_OPERAND (arg0, 0);
2434 left = TREE_OPERAND (arg0, 1);
2435 right = TREE_OPERAND (arg1, 0);
2437 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2439 common = TREE_OPERAND (arg0, 1);
2440 left = TREE_OPERAND (arg0, 0);
2441 right = TREE_OPERAND (arg1, 1);
2443 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2445 common = TREE_OPERAND (arg0, 1);
2446 left = TREE_OPERAND (arg0, 0);
2447 right = TREE_OPERAND (arg1, 0);
2449 else
2450 return 0;
2452 return fold (build (TREE_CODE (arg0), type, common,
2453 fold (build (code, type, left, right))));
2456 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2457 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
2459 static tree
2460 make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
2461 tree inner;
2462 tree type;
2463 int bitsize, bitpos;
2464 int unsignedp;
2466 tree result = build (BIT_FIELD_REF, type, inner,
2467 size_int (bitsize), bitsize_int (bitpos));
2469 TREE_UNSIGNED (result) = unsignedp;
2471 return result;
2474 /* Optimize a bit-field compare.
2476 There are two cases: First is a compare against a constant and the
2477 second is a comparison of two items where the fields are at the same
2478 bit position relative to the start of a chunk (byte, halfword, word)
2479 large enough to contain it. In these cases we can avoid the shift
2480 implicit in bitfield extractions.
2482 For constants, we emit a compare of the shifted constant with the
2483 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2484 compared. For two fields at the same position, we do the ANDs with the
2485 similar mask and compare the result of the ANDs.
2487 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2488 COMPARE_TYPE is the type of the comparison, and LHS and RHS
2489 are the left and right operands of the comparison, respectively.
2491 If the optimization described above can be done, we return the resulting
2492 tree. Otherwise we return zero. */
2494 static tree
2495 optimize_bit_field_compare (code, compare_type, lhs, rhs)
2496 enum tree_code code;
2497 tree compare_type;
2498 tree lhs, rhs;
2500 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
2501 tree type = TREE_TYPE (lhs);
2502 tree signed_type, unsigned_type;
2503 int const_p = TREE_CODE (rhs) == INTEGER_CST;
2504 enum machine_mode lmode, rmode, nmode;
2505 int lunsignedp, runsignedp;
2506 int lvolatilep = 0, rvolatilep = 0;
2507 tree linner, rinner = NULL_TREE;
2508 tree mask;
2509 tree offset;
2511 /* Get all the information about the extractions being done. If the bit size
2512 if the same as the size of the underlying object, we aren't doing an
2513 extraction at all and so can do nothing. We also don't want to
2514 do anything if the inner expression is a PLACEHOLDER_EXPR since we
2515 then will no longer be able to replace it. */
2516 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
2517 &lunsignedp, &lvolatilep);
2518 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
2519 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
2520 return 0;
2522 if (!const_p)
2524 /* If this is not a constant, we can only do something if bit positions,
2525 sizes, and signedness are the same. */
2526 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
2527 &runsignedp, &rvolatilep);
2529 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
2530 || lunsignedp != runsignedp || offset != 0
2531 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
2532 return 0;
2535 /* See if we can find a mode to refer to this field. We should be able to,
2536 but fail if we can't. */
2537 nmode = get_best_mode (lbitsize, lbitpos,
2538 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
2539 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
2540 TYPE_ALIGN (TREE_TYPE (rinner))),
2541 word_mode, lvolatilep || rvolatilep);
2542 if (nmode == VOIDmode)
2543 return 0;
2545 /* Set signed and unsigned types of the precision of this mode for the
2546 shifts below. */
2547 signed_type = (*lang_hooks.types.type_for_mode) (nmode, 0);
2548 unsigned_type = (*lang_hooks.types.type_for_mode) (nmode, 1);
2550 /* Compute the bit position and size for the new reference and our offset
2551 within it. If the new reference is the same size as the original, we
2552 won't optimize anything, so return zero. */
2553 nbitsize = GET_MODE_BITSIZE (nmode);
2554 nbitpos = lbitpos & ~ (nbitsize - 1);
2555 lbitpos -= nbitpos;
2556 if (nbitsize == lbitsize)
2557 return 0;
2559 if (BYTES_BIG_ENDIAN)
2560 lbitpos = nbitsize - lbitsize - lbitpos;
2562 /* Make the mask to be used against the extracted field. */
2563 mask = build_int_2 (~0, ~0);
2564 TREE_TYPE (mask) = unsigned_type;
2565 force_fit_type (mask, 0);
2566 mask = convert (unsigned_type, mask);
2567 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
2568 mask = const_binop (RSHIFT_EXPR, mask,
2569 size_int (nbitsize - lbitsize - lbitpos), 0);
2571 if (! const_p)
2572 /* If not comparing with constant, just rework the comparison
2573 and return. */
2574 return build (code, compare_type,
2575 build (BIT_AND_EXPR, unsigned_type,
2576 make_bit_field_ref (linner, unsigned_type,
2577 nbitsize, nbitpos, 1),
2578 mask),
2579 build (BIT_AND_EXPR, unsigned_type,
2580 make_bit_field_ref (rinner, unsigned_type,
2581 nbitsize, nbitpos, 1),
2582 mask));
2584 /* Otherwise, we are handling the constant case. See if the constant is too
2585 big for the field. Warn and return a tree of for 0 (false) if so. We do
2586 this not only for its own sake, but to avoid having to test for this
2587 error case below. If we didn't, we might generate wrong code.
2589 For unsigned fields, the constant shifted right by the field length should
2590 be all zero. For signed fields, the high-order bits should agree with
2591 the sign bit. */
2593 if (lunsignedp)
2595 if (! integer_zerop (const_binop (RSHIFT_EXPR,
2596 convert (unsigned_type, rhs),
2597 size_int (lbitsize), 0)))
2599 warning ("comparison is always %d due to width of bit-field",
2600 code == NE_EXPR);
2601 return convert (compare_type,
2602 (code == NE_EXPR
2603 ? integer_one_node : integer_zero_node));
2606 else
2608 tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
2609 size_int (lbitsize - 1), 0);
2610 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
2612 warning ("comparison is always %d due to width of bit-field",
2613 code == NE_EXPR);
2614 return convert (compare_type,
2615 (code == NE_EXPR
2616 ? integer_one_node : integer_zero_node));
2620 /* Single-bit compares should always be against zero. */
2621 if (lbitsize == 1 && ! integer_zerop (rhs))
2623 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
2624 rhs = convert (type, integer_zero_node);
2627 /* Make a new bitfield reference, shift the constant over the
2628 appropriate number of bits and mask it with the computed mask
2629 (in case this was a signed field). If we changed it, make a new one. */
2630 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
2631 if (lvolatilep)
2633 TREE_SIDE_EFFECTS (lhs) = 1;
2634 TREE_THIS_VOLATILE (lhs) = 1;
2637 rhs = fold (const_binop (BIT_AND_EXPR,
2638 const_binop (LSHIFT_EXPR,
2639 convert (unsigned_type, rhs),
2640 size_int (lbitpos), 0),
2641 mask, 0));
2643 return build (code, compare_type,
2644 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
2645 rhs);
2648 /* Subroutine for fold_truthop: decode a field reference.
2650 If EXP is a comparison reference, we return the innermost reference.
2652 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
2653 set to the starting bit number.
2655 If the innermost field can be completely contained in a mode-sized
2656 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
2658 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
2659 otherwise it is not changed.
2661 *PUNSIGNEDP is set to the signedness of the field.
2663 *PMASK is set to the mask used. This is either contained in a
2664 BIT_AND_EXPR or derived from the width of the field.
2666 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
2668 Return 0 if this is not a component reference or is one that we can't
2669 do anything with. */
2671 static tree
2672 decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
2673 pvolatilep, pmask, pand_mask)
2674 tree exp;
2675 HOST_WIDE_INT *pbitsize, *pbitpos;
2676 enum machine_mode *pmode;
2677 int *punsignedp, *pvolatilep;
2678 tree *pmask;
2679 tree *pand_mask;
2681 tree and_mask = 0;
2682 tree mask, inner, offset;
2683 tree unsigned_type;
2684 unsigned int precision;
2686 /* All the optimizations using this function assume integer fields.
2687 There are problems with FP fields since the type_for_size call
2688 below can fail for, e.g., XFmode. */
2689 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
2690 return 0;
2692 STRIP_NOPS (exp);
2694 if (TREE_CODE (exp) == BIT_AND_EXPR)
2696 and_mask = TREE_OPERAND (exp, 1);
2697 exp = TREE_OPERAND (exp, 0);
2698 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
2699 if (TREE_CODE (and_mask) != INTEGER_CST)
2700 return 0;
2703 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
2704 punsignedp, pvolatilep);
2705 if ((inner == exp && and_mask == 0)
2706 || *pbitsize < 0 || offset != 0
2707 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
2708 return 0;
2710 /* Compute the mask to access the bitfield. */
2711 unsigned_type = (*lang_hooks.types.type_for_size) (*pbitsize, 1);
2712 precision = TYPE_PRECISION (unsigned_type);
2714 mask = build_int_2 (~0, ~0);
2715 TREE_TYPE (mask) = unsigned_type;
2716 force_fit_type (mask, 0);
2717 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
2718 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
2720 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
2721 if (and_mask != 0)
2722 mask = fold (build (BIT_AND_EXPR, unsigned_type,
2723 convert (unsigned_type, and_mask), mask));
2725 *pmask = mask;
2726 *pand_mask = and_mask;
2727 return inner;
2730 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
2731 bit positions. */
2733 static int
2734 all_ones_mask_p (mask, size)
2735 tree mask;
2736 int size;
2738 tree type = TREE_TYPE (mask);
2739 unsigned int precision = TYPE_PRECISION (type);
2740 tree tmask;
2742 tmask = build_int_2 (~0, ~0);
2743 TREE_TYPE (tmask) = (*lang_hooks.types.signed_type) (type);
2744 force_fit_type (tmask, 0);
2745 return
2746 tree_int_cst_equal (mask,
2747 const_binop (RSHIFT_EXPR,
2748 const_binop (LSHIFT_EXPR, tmask,
2749 size_int (precision - size),
2751 size_int (precision - size), 0));
2754 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
2755 represents the sign bit of EXP's type. If EXP represents a sign
2756 or zero extension, also test VAL against the unextended type.
2757 The return value is the (sub)expression whose sign bit is VAL,
2758 or NULL_TREE otherwise. */
2760 static tree
2761 sign_bit_p (exp, val)
2762 tree exp;
2763 tree val;
2765 unsigned HOST_WIDE_INT lo;
2766 HOST_WIDE_INT hi;
2767 int width;
2768 tree t;
2770 /* Tree EXP must have an integral type. */
2771 t = TREE_TYPE (exp);
2772 if (! INTEGRAL_TYPE_P (t))
2773 return NULL_TREE;
2775 /* Tree VAL must be an integer constant. */
2776 if (TREE_CODE (val) != INTEGER_CST
2777 || TREE_CONSTANT_OVERFLOW (val))
2778 return NULL_TREE;
2780 width = TYPE_PRECISION (t);
2781 if (width > HOST_BITS_PER_WIDE_INT)
2783 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
2784 lo = 0;
2786 else
2788 hi = 0;
2789 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
2792 if (TREE_INT_CST_HIGH (val) == hi && TREE_INT_CST_LOW (val) == lo)
2793 return exp;
2795 /* Handle extension from a narrower type. */
2796 if (TREE_CODE (exp) == NOP_EXPR
2797 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
2798 return sign_bit_p (TREE_OPERAND (exp, 0), val);
2800 return NULL_TREE;
2803 /* Subroutine for fold_truthop: determine if an operand is simple enough
2804 to be evaluated unconditionally. */
2806 static int
2807 simple_operand_p (exp)
2808 tree exp;
2810 /* Strip any conversions that don't change the machine mode. */
2811 while ((TREE_CODE (exp) == NOP_EXPR
2812 || TREE_CODE (exp) == CONVERT_EXPR)
2813 && (TYPE_MODE (TREE_TYPE (exp))
2814 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
2815 exp = TREE_OPERAND (exp, 0);
2817 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
2818 || (DECL_P (exp)
2819 && ! TREE_ADDRESSABLE (exp)
2820 && ! TREE_THIS_VOLATILE (exp)
2821 && ! DECL_NONLOCAL (exp)
2822 /* Don't regard global variables as simple. They may be
2823 allocated in ways unknown to the compiler (shared memory,
2824 #pragma weak, etc). */
2825 && ! TREE_PUBLIC (exp)
2826 && ! DECL_EXTERNAL (exp)
2827 /* Loading a static variable is unduly expensive, but global
2828 registers aren't expensive. */
2829 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
2832 /* The following functions are subroutines to fold_range_test and allow it to
2833 try to change a logical combination of comparisons into a range test.
2835 For example, both
2836 X == 2 || X == 3 || X == 4 || X == 5
2838 X >= 2 && X <= 5
2839 are converted to
2840 (unsigned) (X - 2) <= 3
2842 We describe each set of comparisons as being either inside or outside
2843 a range, using a variable named like IN_P, and then describe the
2844 range with a lower and upper bound. If one of the bounds is omitted,
2845 it represents either the highest or lowest value of the type.
2847 In the comments below, we represent a range by two numbers in brackets
2848 preceded by a "+" to designate being inside that range, or a "-" to
2849 designate being outside that range, so the condition can be inverted by
2850 flipping the prefix. An omitted bound is represented by a "-". For
2851 example, "- [-, 10]" means being outside the range starting at the lowest
2852 possible value and ending at 10, in other words, being greater than 10.
2853 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
2854 always false.
2856 We set up things so that the missing bounds are handled in a consistent
2857 manner so neither a missing bound nor "true" and "false" need to be
2858 handled using a special case. */
2860 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
2861 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
2862 and UPPER1_P are nonzero if the respective argument is an upper bound
2863 and zero for a lower. TYPE, if nonzero, is the type of the result; it
2864 must be specified for a comparison. ARG1 will be converted to ARG0's
2865 type if both are specified. */
2867 static tree
2868 range_binop (code, type, arg0, upper0_p, arg1, upper1_p)
2869 enum tree_code code;
2870 tree type;
2871 tree arg0, arg1;
2872 int upper0_p, upper1_p;
2874 tree tem;
2875 int result;
2876 int sgn0, sgn1;
2878 /* If neither arg represents infinity, do the normal operation.
2879 Else, if not a comparison, return infinity. Else handle the special
2880 comparison rules. Note that most of the cases below won't occur, but
2881 are handled for consistency. */
2883 if (arg0 != 0 && arg1 != 0)
2885 tem = fold (build (code, type != 0 ? type : TREE_TYPE (arg0),
2886 arg0, convert (TREE_TYPE (arg0), arg1)));
2887 STRIP_NOPS (tem);
2888 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
2891 if (TREE_CODE_CLASS (code) != '<')
2892 return 0;
2894 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
2895 for neither. In real maths, we cannot assume open ended ranges are
2896 the same. But, this is computer arithmetic, where numbers are finite.
2897 We can therefore make the transformation of any unbounded range with
2898 the value Z, Z being greater than any representable number. This permits
2899 us to treat unbounded ranges as equal. */
2900 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
2901 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
2902 switch (code)
2904 case EQ_EXPR:
2905 result = sgn0 == sgn1;
2906 break;
2907 case NE_EXPR:
2908 result = sgn0 != sgn1;
2909 break;
2910 case LT_EXPR:
2911 result = sgn0 < sgn1;
2912 break;
2913 case LE_EXPR:
2914 result = sgn0 <= sgn1;
2915 break;
2916 case GT_EXPR:
2917 result = sgn0 > sgn1;
2918 break;
2919 case GE_EXPR:
2920 result = sgn0 >= sgn1;
2921 break;
2922 default:
2923 abort ();
2926 return convert (type, result ? integer_one_node : integer_zero_node);
2929 /* Given EXP, a logical expression, set the range it is testing into
2930 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
2931 actually being tested. *PLOW and *PHIGH will be made of the same type
2932 as the returned expression. If EXP is not a comparison, we will most
2933 likely not be returning a useful value and range. */
2935 static tree
2936 make_range (exp, pin_p, plow, phigh)
2937 tree exp;
2938 int *pin_p;
2939 tree *plow, *phigh;
2941 enum tree_code code;
2942 tree arg0 = NULL_TREE, arg1 = NULL_TREE, type = NULL_TREE;
2943 tree orig_type = NULL_TREE;
2944 int in_p, n_in_p;
2945 tree low, high, n_low, n_high;
2947 /* Start with simply saying "EXP != 0" and then look at the code of EXP
2948 and see if we can refine the range. Some of the cases below may not
2949 happen, but it doesn't seem worth worrying about this. We "continue"
2950 the outer loop when we've changed something; otherwise we "break"
2951 the switch, which will "break" the while. */
2953 in_p = 0, low = high = convert (TREE_TYPE (exp), integer_zero_node);
2955 while (1)
2957 code = TREE_CODE (exp);
2959 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
2961 arg0 = TREE_OPERAND (exp, 0);
2962 if (TREE_CODE_CLASS (code) == '<'
2963 || TREE_CODE_CLASS (code) == '1'
2964 || TREE_CODE_CLASS (code) == '2')
2965 type = TREE_TYPE (arg0);
2966 if (TREE_CODE_CLASS (code) == '2'
2967 || TREE_CODE_CLASS (code) == '<'
2968 || (TREE_CODE_CLASS (code) == 'e'
2969 && TREE_CODE_LENGTH (code) > 1))
2970 arg1 = TREE_OPERAND (exp, 1);
2973 /* Set ORIG_TYPE as soon as TYPE is non-null so that we do not
2974 lose a cast by accident. */
2975 if (type != NULL_TREE && orig_type == NULL_TREE)
2976 orig_type = type;
2978 switch (code)
2980 case TRUTH_NOT_EXPR:
2981 in_p = ! in_p, exp = arg0;
2982 continue;
2984 case EQ_EXPR: case NE_EXPR:
2985 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
2986 /* We can only do something if the range is testing for zero
2987 and if the second operand is an integer constant. Note that
2988 saying something is "in" the range we make is done by
2989 complementing IN_P since it will set in the initial case of
2990 being not equal to zero; "out" is leaving it alone. */
2991 if (low == 0 || high == 0
2992 || ! integer_zerop (low) || ! integer_zerop (high)
2993 || TREE_CODE (arg1) != INTEGER_CST)
2994 break;
2996 switch (code)
2998 case NE_EXPR: /* - [c, c] */
2999 low = high = arg1;
3000 break;
3001 case EQ_EXPR: /* + [c, c] */
3002 in_p = ! in_p, low = high = arg1;
3003 break;
3004 case GT_EXPR: /* - [-, c] */
3005 low = 0, high = arg1;
3006 break;
3007 case GE_EXPR: /* + [c, -] */
3008 in_p = ! in_p, low = arg1, high = 0;
3009 break;
3010 case LT_EXPR: /* - [c, -] */
3011 low = arg1, high = 0;
3012 break;
3013 case LE_EXPR: /* + [-, c] */
3014 in_p = ! in_p, low = 0, high = arg1;
3015 break;
3016 default:
3017 abort ();
3020 exp = arg0;
3022 /* If this is an unsigned comparison, we also know that EXP is
3023 greater than or equal to zero. We base the range tests we make
3024 on that fact, so we record it here so we can parse existing
3025 range tests. */
3026 if (TREE_UNSIGNED (type) && (low == 0 || high == 0))
3028 if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
3029 1, convert (type, integer_zero_node),
3030 NULL_TREE))
3031 break;
3033 in_p = n_in_p, low = n_low, high = n_high;
3035 /* If the high bound is missing, but we
3036 have a low bound, reverse the range so
3037 it goes from zero to the low bound minus 1. */
3038 if (high == 0 && low)
3040 in_p = ! in_p;
3041 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3042 integer_one_node, 0);
3043 low = convert (type, integer_zero_node);
3046 continue;
3048 case NEGATE_EXPR:
3049 /* (-x) IN [a,b] -> x in [-b, -a] */
3050 n_low = range_binop (MINUS_EXPR, type,
3051 convert (type, integer_zero_node), 0, high, 1);
3052 n_high = range_binop (MINUS_EXPR, type,
3053 convert (type, integer_zero_node), 0, low, 0);
3054 low = n_low, high = n_high;
3055 exp = arg0;
3056 continue;
3058 case BIT_NOT_EXPR:
3059 /* ~ X -> -X - 1 */
3060 exp = build (MINUS_EXPR, type, negate_expr (arg0),
3061 convert (type, integer_one_node));
3062 continue;
3064 case PLUS_EXPR: case MINUS_EXPR:
3065 if (TREE_CODE (arg1) != INTEGER_CST)
3066 break;
3068 /* If EXP is signed, any overflow in the computation is undefined,
3069 so we don't worry about it so long as our computations on
3070 the bounds don't overflow. For unsigned, overflow is defined
3071 and this is exactly the right thing. */
3072 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3073 type, low, 0, arg1, 0);
3074 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3075 type, high, 1, arg1, 0);
3076 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3077 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3078 break;
3080 /* Check for an unsigned range which has wrapped around the maximum
3081 value thus making n_high < n_low, and normalize it. */
3082 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3084 low = range_binop (PLUS_EXPR, type, n_high, 0,
3085 integer_one_node, 0);
3086 high = range_binop (MINUS_EXPR, type, n_low, 0,
3087 integer_one_node, 0);
3089 /* If the range is of the form +/- [ x+1, x ], we won't
3090 be able to normalize it. But then, it represents the
3091 whole range or the empty set, so make it
3092 +/- [ -, - ]. */
3093 if (tree_int_cst_equal (n_low, low)
3094 && tree_int_cst_equal (n_high, high))
3095 low = high = 0;
3096 else
3097 in_p = ! in_p;
3099 else
3100 low = n_low, high = n_high;
3102 exp = arg0;
3103 continue;
3105 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
3106 if (TYPE_PRECISION (type) > TYPE_PRECISION (orig_type))
3107 break;
3109 if (! INTEGRAL_TYPE_P (type)
3110 || (low != 0 && ! int_fits_type_p (low, type))
3111 || (high != 0 && ! int_fits_type_p (high, type)))
3112 break;
3114 n_low = low, n_high = high;
3116 if (n_low != 0)
3117 n_low = convert (type, n_low);
3119 if (n_high != 0)
3120 n_high = convert (type, n_high);
3122 /* If we're converting from an unsigned to a signed type,
3123 we will be doing the comparison as unsigned. The tests above
3124 have already verified that LOW and HIGH are both positive.
3126 So we have to make sure that the original unsigned value will
3127 be interpreted as positive. */
3128 if (TREE_UNSIGNED (type) && ! TREE_UNSIGNED (TREE_TYPE (exp)))
3130 tree equiv_type = (*lang_hooks.types.type_for_mode)
3131 (TYPE_MODE (type), 1);
3132 tree high_positive;
3134 /* A range without an upper bound is, naturally, unbounded.
3135 Since convert would have cropped a very large value, use
3136 the max value for the destination type. */
3137 high_positive
3138 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3139 : TYPE_MAX_VALUE (type);
3141 if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (exp)))
3142 high_positive = fold (build (RSHIFT_EXPR, type,
3143 convert (type, high_positive),
3144 convert (type, integer_one_node)));
3146 /* If the low bound is specified, "and" the range with the
3147 range for which the original unsigned value will be
3148 positive. */
3149 if (low != 0)
3151 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3152 1, n_low, n_high,
3153 1, convert (type, integer_zero_node),
3154 high_positive))
3155 break;
3157 in_p = (n_in_p == in_p);
3159 else
3161 /* Otherwise, "or" the range with the range of the input
3162 that will be interpreted as negative. */
3163 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3164 0, n_low, n_high,
3165 1, convert (type, integer_zero_node),
3166 high_positive))
3167 break;
3169 in_p = (in_p != n_in_p);
3173 exp = arg0;
3174 low = n_low, high = n_high;
3175 continue;
3177 default:
3178 break;
3181 break;
3184 /* If EXP is a constant, we can evaluate whether this is true or false. */
3185 if (TREE_CODE (exp) == INTEGER_CST)
3187 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3188 exp, 0, low, 0))
3189 && integer_onep (range_binop (LE_EXPR, integer_type_node,
3190 exp, 1, high, 1)));
3191 low = high = 0;
3192 exp = 0;
3195 *pin_p = in_p, *plow = low, *phigh = high;
3196 return exp;
3199 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3200 type, TYPE, return an expression to test if EXP is in (or out of, depending
3201 on IN_P) the range. */
3203 static tree
3204 build_range_check (type, exp, in_p, low, high)
3205 tree type;
3206 tree exp;
3207 int in_p;
3208 tree low, high;
3210 tree etype = TREE_TYPE (exp);
3211 tree value;
3213 if (! in_p
3214 && (0 != (value = build_range_check (type, exp, 1, low, high))))
3215 return invert_truthvalue (value);
3217 if (low == 0 && high == 0)
3218 return convert (type, integer_one_node);
3220 if (low == 0)
3221 return fold (build (LE_EXPR, type, exp, high));
3223 if (high == 0)
3224 return fold (build (GE_EXPR, type, exp, low));
3226 if (operand_equal_p (low, high, 0))
3227 return fold (build (EQ_EXPR, type, exp, low));
3229 if (integer_zerop (low))
3231 if (! TREE_UNSIGNED (etype))
3233 etype = (*lang_hooks.types.unsigned_type) (etype);
3234 high = convert (etype, high);
3235 exp = convert (etype, exp);
3237 return build_range_check (type, exp, 1, 0, high);
3240 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
3241 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
3243 unsigned HOST_WIDE_INT lo;
3244 HOST_WIDE_INT hi;
3245 int prec;
3247 prec = TYPE_PRECISION (etype);
3248 if (prec <= HOST_BITS_PER_WIDE_INT)
3250 hi = 0;
3251 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
3253 else
3255 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
3256 lo = (unsigned HOST_WIDE_INT) -1;
3259 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
3261 if (TREE_UNSIGNED (etype))
3263 etype = (*lang_hooks.types.signed_type) (etype);
3264 exp = convert (etype, exp);
3266 return fold (build (GT_EXPR, type, exp,
3267 convert (etype, integer_zero_node)));
3271 if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
3272 && ! TREE_OVERFLOW (value))
3273 return build_range_check (type,
3274 fold (build (MINUS_EXPR, etype, exp, low)),
3275 1, convert (etype, integer_zero_node), value);
3277 return 0;
3280 /* Given two ranges, see if we can merge them into one. Return 1 if we
3281 can, 0 if we can't. Set the output range into the specified parameters. */
3283 static int
3284 merge_ranges (pin_p, plow, phigh, in0_p, low0, high0, in1_p, low1, high1)
3285 int *pin_p;
3286 tree *plow, *phigh;
3287 int in0_p, in1_p;
3288 tree low0, high0, low1, high1;
3290 int no_overlap;
3291 int subset;
3292 int temp;
3293 tree tem;
3294 int in_p;
3295 tree low, high;
3296 int lowequal = ((low0 == 0 && low1 == 0)
3297 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3298 low0, 0, low1, 0)));
3299 int highequal = ((high0 == 0 && high1 == 0)
3300 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3301 high0, 1, high1, 1)));
3303 /* Make range 0 be the range that starts first, or ends last if they
3304 start at the same value. Swap them if it isn't. */
3305 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3306 low0, 0, low1, 0))
3307 || (lowequal
3308 && integer_onep (range_binop (GT_EXPR, integer_type_node,
3309 high1, 1, high0, 1))))
3311 temp = in0_p, in0_p = in1_p, in1_p = temp;
3312 tem = low0, low0 = low1, low1 = tem;
3313 tem = high0, high0 = high1, high1 = tem;
3316 /* Now flag two cases, whether the ranges are disjoint or whether the
3317 second range is totally subsumed in the first. Note that the tests
3318 below are simplified by the ones above. */
3319 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3320 high0, 1, low1, 0));
3321 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3322 high1, 1, high0, 1));
3324 /* We now have four cases, depending on whether we are including or
3325 excluding the two ranges. */
3326 if (in0_p && in1_p)
3328 /* If they don't overlap, the result is false. If the second range
3329 is a subset it is the result. Otherwise, the range is from the start
3330 of the second to the end of the first. */
3331 if (no_overlap)
3332 in_p = 0, low = high = 0;
3333 else if (subset)
3334 in_p = 1, low = low1, high = high1;
3335 else
3336 in_p = 1, low = low1, high = high0;
3339 else if (in0_p && ! in1_p)
3341 /* If they don't overlap, the result is the first range. If they are
3342 equal, the result is false. If the second range is a subset of the
3343 first, and the ranges begin at the same place, we go from just after
3344 the end of the first range to the end of the second. If the second
3345 range is not a subset of the first, or if it is a subset and both
3346 ranges end at the same place, the range starts at the start of the
3347 first range and ends just before the second range.
3348 Otherwise, we can't describe this as a single range. */
3349 if (no_overlap)
3350 in_p = 1, low = low0, high = high0;
3351 else if (lowequal && highequal)
3352 in_p = 0, low = high = 0;
3353 else if (subset && lowequal)
3355 in_p = 1, high = high0;
3356 low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
3357 integer_one_node, 0);
3359 else if (! subset || highequal)
3361 in_p = 1, low = low0;
3362 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
3363 integer_one_node, 0);
3365 else
3366 return 0;
3369 else if (! in0_p && in1_p)
3371 /* If they don't overlap, the result is the second range. If the second
3372 is a subset of the first, the result is false. Otherwise,
3373 the range starts just after the first range and ends at the
3374 end of the second. */
3375 if (no_overlap)
3376 in_p = 1, low = low1, high = high1;
3377 else if (subset || highequal)
3378 in_p = 0, low = high = 0;
3379 else
3381 in_p = 1, high = high1;
3382 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3383 integer_one_node, 0);
3387 else
3389 /* The case where we are excluding both ranges. Here the complex case
3390 is if they don't overlap. In that case, the only time we have a
3391 range is if they are adjacent. If the second is a subset of the
3392 first, the result is the first. Otherwise, the range to exclude
3393 starts at the beginning of the first range and ends at the end of the
3394 second. */
3395 if (no_overlap)
3397 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3398 range_binop (PLUS_EXPR, NULL_TREE,
3399 high0, 1,
3400 integer_one_node, 1),
3401 1, low1, 0)))
3402 in_p = 0, low = low0, high = high1;
3403 else
3404 return 0;
3406 else if (subset)
3407 in_p = 0, low = low0, high = high0;
3408 else
3409 in_p = 0, low = low0, high = high1;
3412 *pin_p = in_p, *plow = low, *phigh = high;
3413 return 1;
3416 /* EXP is some logical combination of boolean tests. See if we can
3417 merge it into some range test. Return the new tree if so. */
3419 static tree
3420 fold_range_test (exp)
3421 tree exp;
3423 int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3424 || TREE_CODE (exp) == TRUTH_OR_EXPR);
3425 int in0_p, in1_p, in_p;
3426 tree low0, low1, low, high0, high1, high;
3427 tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
3428 tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
3429 tree tem;
3431 /* If this is an OR operation, invert both sides; we will invert
3432 again at the end. */
3433 if (or_op)
3434 in0_p = ! in0_p, in1_p = ! in1_p;
3436 /* If both expressions are the same, if we can merge the ranges, and we
3437 can build the range test, return it or it inverted. If one of the
3438 ranges is always true or always false, consider it to be the same
3439 expression as the other. */
3440 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
3441 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
3442 in1_p, low1, high1)
3443 && 0 != (tem = (build_range_check (TREE_TYPE (exp),
3444 lhs != 0 ? lhs
3445 : rhs != 0 ? rhs : integer_zero_node,
3446 in_p, low, high))))
3447 return or_op ? invert_truthvalue (tem) : tem;
3449 /* On machines where the branch cost is expensive, if this is a
3450 short-circuited branch and the underlying object on both sides
3451 is the same, make a non-short-circuit operation. */
3452 else if (BRANCH_COST >= 2
3453 && lhs != 0 && rhs != 0
3454 && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3455 || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
3456 && operand_equal_p (lhs, rhs, 0))
3458 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
3459 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
3460 which cases we can't do this. */
3461 if (simple_operand_p (lhs))
3462 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3463 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3464 TREE_TYPE (exp), TREE_OPERAND (exp, 0),
3465 TREE_OPERAND (exp, 1));
3467 else if ((*lang_hooks.decls.global_bindings_p) () == 0
3468 && ! contains_placeholder_p (lhs))
3470 tree common = save_expr (lhs);
3472 if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
3473 or_op ? ! in0_p : in0_p,
3474 low0, high0))
3475 && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
3476 or_op ? ! in1_p : in1_p,
3477 low1, high1))))
3478 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3479 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3480 TREE_TYPE (exp), lhs, rhs);
3484 return 0;
3487 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
3488 bit value. Arrange things so the extra bits will be set to zero if and
3489 only if C is signed-extended to its full width. If MASK is nonzero,
3490 it is an INTEGER_CST that should be AND'ed with the extra bits. */
3492 static tree
3493 unextend (c, p, unsignedp, mask)
3494 tree c;
3495 int p;
3496 int unsignedp;
3497 tree mask;
3499 tree type = TREE_TYPE (c);
3500 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
3501 tree temp;
3503 if (p == modesize || unsignedp)
3504 return c;
3506 /* We work by getting just the sign bit into the low-order bit, then
3507 into the high-order bit, then sign-extend. We then XOR that value
3508 with C. */
3509 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
3510 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
3512 /* We must use a signed type in order to get an arithmetic right shift.
3513 However, we must also avoid introducing accidental overflows, so that
3514 a subsequent call to integer_zerop will work. Hence we must
3515 do the type conversion here. At this point, the constant is either
3516 zero or one, and the conversion to a signed type can never overflow.
3517 We could get an overflow if this conversion is done anywhere else. */
3518 if (TREE_UNSIGNED (type))
3519 temp = convert ((*lang_hooks.types.signed_type) (type), temp);
3521 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
3522 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
3523 if (mask != 0)
3524 temp = const_binop (BIT_AND_EXPR, temp, convert (TREE_TYPE (c), mask), 0);
3525 /* If necessary, convert the type back to match the type of C. */
3526 if (TREE_UNSIGNED (type))
3527 temp = convert (type, temp);
3529 return convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
3532 /* Find ways of folding logical expressions of LHS and RHS:
3533 Try to merge two comparisons to the same innermost item.
3534 Look for range tests like "ch >= '0' && ch <= '9'".
3535 Look for combinations of simple terms on machines with expensive branches
3536 and evaluate the RHS unconditionally.
3538 For example, if we have p->a == 2 && p->b == 4 and we can make an
3539 object large enough to span both A and B, we can do this with a comparison
3540 against the object ANDed with the a mask.
3542 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
3543 operations to do this with one comparison.
3545 We check for both normal comparisons and the BIT_AND_EXPRs made this by
3546 function and the one above.
3548 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
3549 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
3551 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
3552 two operands.
3554 We return the simplified tree or 0 if no optimization is possible. */
3556 static tree
3557 fold_truthop (code, truth_type, lhs, rhs)
3558 enum tree_code code;
3559 tree truth_type, lhs, rhs;
3561 /* If this is the "or" of two comparisons, we can do something if
3562 the comparisons are NE_EXPR. If this is the "and", we can do something
3563 if the comparisons are EQ_EXPR. I.e.,
3564 (a->b == 2 && a->c == 4) can become (a->new == NEW).
3566 WANTED_CODE is this operation code. For single bit fields, we can
3567 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
3568 comparison for one-bit fields. */
3570 enum tree_code wanted_code;
3571 enum tree_code lcode, rcode;
3572 tree ll_arg, lr_arg, rl_arg, rr_arg;
3573 tree ll_inner, lr_inner, rl_inner, rr_inner;
3574 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
3575 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
3576 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
3577 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
3578 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
3579 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
3580 enum machine_mode lnmode, rnmode;
3581 tree ll_mask, lr_mask, rl_mask, rr_mask;
3582 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
3583 tree l_const, r_const;
3584 tree lntype, rntype, result;
3585 int first_bit, end_bit;
3586 int volatilep;
3588 /* Start by getting the comparison codes. Fail if anything is volatile.
3589 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
3590 it were surrounded with a NE_EXPR. */
3592 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
3593 return 0;
3595 lcode = TREE_CODE (lhs);
3596 rcode = TREE_CODE (rhs);
3598 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
3599 lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
3601 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
3602 rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
3604 if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
3605 return 0;
3607 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
3608 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
3610 ll_arg = TREE_OPERAND (lhs, 0);
3611 lr_arg = TREE_OPERAND (lhs, 1);
3612 rl_arg = TREE_OPERAND (rhs, 0);
3613 rr_arg = TREE_OPERAND (rhs, 1);
3615 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
3616 if (simple_operand_p (ll_arg)
3617 && simple_operand_p (lr_arg)
3618 && !FLOAT_TYPE_P (TREE_TYPE (ll_arg)))
3620 int compcode;
3622 if (operand_equal_p (ll_arg, rl_arg, 0)
3623 && operand_equal_p (lr_arg, rr_arg, 0))
3625 int lcompcode, rcompcode;
3627 lcompcode = comparison_to_compcode (lcode);
3628 rcompcode = comparison_to_compcode (rcode);
3629 compcode = (code == TRUTH_AND_EXPR)
3630 ? lcompcode & rcompcode
3631 : lcompcode | rcompcode;
3633 else if (operand_equal_p (ll_arg, rr_arg, 0)
3634 && operand_equal_p (lr_arg, rl_arg, 0))
3636 int lcompcode, rcompcode;
3638 rcode = swap_tree_comparison (rcode);
3639 lcompcode = comparison_to_compcode (lcode);
3640 rcompcode = comparison_to_compcode (rcode);
3641 compcode = (code == TRUTH_AND_EXPR)
3642 ? lcompcode & rcompcode
3643 : lcompcode | rcompcode;
3645 else
3646 compcode = -1;
3648 if (compcode == COMPCODE_TRUE)
3649 return convert (truth_type, integer_one_node);
3650 else if (compcode == COMPCODE_FALSE)
3651 return convert (truth_type, integer_zero_node);
3652 else if (compcode != -1)
3653 return build (compcode_to_comparison (compcode),
3654 truth_type, ll_arg, lr_arg);
3657 /* If the RHS can be evaluated unconditionally and its operands are
3658 simple, it wins to evaluate the RHS unconditionally on machines
3659 with expensive branches. In this case, this isn't a comparison
3660 that can be merged. Avoid doing this if the RHS is a floating-point
3661 comparison since those can trap. */
3663 if (BRANCH_COST >= 2
3664 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
3665 && simple_operand_p (rl_arg)
3666 && simple_operand_p (rr_arg))
3668 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
3669 if (code == TRUTH_OR_EXPR
3670 && lcode == NE_EXPR && integer_zerop (lr_arg)
3671 && rcode == NE_EXPR && integer_zerop (rr_arg)
3672 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
3673 return build (NE_EXPR, truth_type,
3674 build (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
3675 ll_arg, rl_arg),
3676 integer_zero_node);
3678 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
3679 if (code == TRUTH_AND_EXPR
3680 && lcode == EQ_EXPR && integer_zerop (lr_arg)
3681 && rcode == EQ_EXPR && integer_zerop (rr_arg)
3682 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
3683 return build (EQ_EXPR, truth_type,
3684 build (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
3685 ll_arg, rl_arg),
3686 integer_zero_node);
3688 return build (code, truth_type, lhs, rhs);
3691 /* See if the comparisons can be merged. Then get all the parameters for
3692 each side. */
3694 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
3695 || (rcode != EQ_EXPR && rcode != NE_EXPR))
3696 return 0;
3698 volatilep = 0;
3699 ll_inner = decode_field_reference (ll_arg,
3700 &ll_bitsize, &ll_bitpos, &ll_mode,
3701 &ll_unsignedp, &volatilep, &ll_mask,
3702 &ll_and_mask);
3703 lr_inner = decode_field_reference (lr_arg,
3704 &lr_bitsize, &lr_bitpos, &lr_mode,
3705 &lr_unsignedp, &volatilep, &lr_mask,
3706 &lr_and_mask);
3707 rl_inner = decode_field_reference (rl_arg,
3708 &rl_bitsize, &rl_bitpos, &rl_mode,
3709 &rl_unsignedp, &volatilep, &rl_mask,
3710 &rl_and_mask);
3711 rr_inner = decode_field_reference (rr_arg,
3712 &rr_bitsize, &rr_bitpos, &rr_mode,
3713 &rr_unsignedp, &volatilep, &rr_mask,
3714 &rr_and_mask);
3716 /* It must be true that the inner operation on the lhs of each
3717 comparison must be the same if we are to be able to do anything.
3718 Then see if we have constants. If not, the same must be true for
3719 the rhs's. */
3720 if (volatilep || ll_inner == 0 || rl_inner == 0
3721 || ! operand_equal_p (ll_inner, rl_inner, 0))
3722 return 0;
3724 if (TREE_CODE (lr_arg) == INTEGER_CST
3725 && TREE_CODE (rr_arg) == INTEGER_CST)
3726 l_const = lr_arg, r_const = rr_arg;
3727 else if (lr_inner == 0 || rr_inner == 0
3728 || ! operand_equal_p (lr_inner, rr_inner, 0))
3729 return 0;
3730 else
3731 l_const = r_const = 0;
3733 /* If either comparison code is not correct for our logical operation,
3734 fail. However, we can convert a one-bit comparison against zero into
3735 the opposite comparison against that bit being set in the field. */
3737 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
3738 if (lcode != wanted_code)
3740 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
3742 /* Make the left operand unsigned, since we are only interested
3743 in the value of one bit. Otherwise we are doing the wrong
3744 thing below. */
3745 ll_unsignedp = 1;
3746 l_const = ll_mask;
3748 else
3749 return 0;
3752 /* This is analogous to the code for l_const above. */
3753 if (rcode != wanted_code)
3755 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
3757 rl_unsignedp = 1;
3758 r_const = rl_mask;
3760 else
3761 return 0;
3764 /* After this point all optimizations will generate bit-field
3765 references, which we might not want. */
3766 if (! (*lang_hooks.can_use_bit_fields_p) ())
3767 return 0;
3769 /* See if we can find a mode that contains both fields being compared on
3770 the left. If we can't, fail. Otherwise, update all constants and masks
3771 to be relative to a field of that size. */
3772 first_bit = MIN (ll_bitpos, rl_bitpos);
3773 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
3774 lnmode = get_best_mode (end_bit - first_bit, first_bit,
3775 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
3776 volatilep);
3777 if (lnmode == VOIDmode)
3778 return 0;
3780 lnbitsize = GET_MODE_BITSIZE (lnmode);
3781 lnbitpos = first_bit & ~ (lnbitsize - 1);
3782 lntype = (*lang_hooks.types.type_for_size) (lnbitsize, 1);
3783 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
3785 if (BYTES_BIG_ENDIAN)
3787 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
3788 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
3791 ll_mask = const_binop (LSHIFT_EXPR, convert (lntype, ll_mask),
3792 size_int (xll_bitpos), 0);
3793 rl_mask = const_binop (LSHIFT_EXPR, convert (lntype, rl_mask),
3794 size_int (xrl_bitpos), 0);
3796 if (l_const)
3798 l_const = convert (lntype, l_const);
3799 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
3800 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
3801 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
3802 fold (build1 (BIT_NOT_EXPR,
3803 lntype, ll_mask)),
3804 0)))
3806 warning ("comparison is always %d", wanted_code == NE_EXPR);
3808 return convert (truth_type,
3809 wanted_code == NE_EXPR
3810 ? integer_one_node : integer_zero_node);
3813 if (r_const)
3815 r_const = convert (lntype, r_const);
3816 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
3817 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
3818 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
3819 fold (build1 (BIT_NOT_EXPR,
3820 lntype, rl_mask)),
3821 0)))
3823 warning ("comparison is always %d", wanted_code == NE_EXPR);
3825 return convert (truth_type,
3826 wanted_code == NE_EXPR
3827 ? integer_one_node : integer_zero_node);
3831 /* If the right sides are not constant, do the same for it. Also,
3832 disallow this optimization if a size or signedness mismatch occurs
3833 between the left and right sides. */
3834 if (l_const == 0)
3836 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
3837 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
3838 /* Make sure the two fields on the right
3839 correspond to the left without being swapped. */
3840 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
3841 return 0;
3843 first_bit = MIN (lr_bitpos, rr_bitpos);
3844 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
3845 rnmode = get_best_mode (end_bit - first_bit, first_bit,
3846 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
3847 volatilep);
3848 if (rnmode == VOIDmode)
3849 return 0;
3851 rnbitsize = GET_MODE_BITSIZE (rnmode);
3852 rnbitpos = first_bit & ~ (rnbitsize - 1);
3853 rntype = (*lang_hooks.types.type_for_size) (rnbitsize, 1);
3854 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
3856 if (BYTES_BIG_ENDIAN)
3858 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
3859 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
3862 lr_mask = const_binop (LSHIFT_EXPR, convert (rntype, lr_mask),
3863 size_int (xlr_bitpos), 0);
3864 rr_mask = const_binop (LSHIFT_EXPR, convert (rntype, rr_mask),
3865 size_int (xrr_bitpos), 0);
3867 /* Make a mask that corresponds to both fields being compared.
3868 Do this for both items being compared. If the operands are the
3869 same size and the bits being compared are in the same position
3870 then we can do this by masking both and comparing the masked
3871 results. */
3872 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
3873 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
3874 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
3876 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
3877 ll_unsignedp || rl_unsignedp);
3878 if (! all_ones_mask_p (ll_mask, lnbitsize))
3879 lhs = build (BIT_AND_EXPR, lntype, lhs, ll_mask);
3881 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
3882 lr_unsignedp || rr_unsignedp);
3883 if (! all_ones_mask_p (lr_mask, rnbitsize))
3884 rhs = build (BIT_AND_EXPR, rntype, rhs, lr_mask);
3886 return build (wanted_code, truth_type, lhs, rhs);
3889 /* There is still another way we can do something: If both pairs of
3890 fields being compared are adjacent, we may be able to make a wider
3891 field containing them both.
3893 Note that we still must mask the lhs/rhs expressions. Furthermore,
3894 the mask must be shifted to account for the shift done by
3895 make_bit_field_ref. */
3896 if ((ll_bitsize + ll_bitpos == rl_bitpos
3897 && lr_bitsize + lr_bitpos == rr_bitpos)
3898 || (ll_bitpos == rl_bitpos + rl_bitsize
3899 && lr_bitpos == rr_bitpos + rr_bitsize))
3901 tree type;
3903 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
3904 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
3905 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
3906 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
3908 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
3909 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
3910 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
3911 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
3913 /* Convert to the smaller type before masking out unwanted bits. */
3914 type = lntype;
3915 if (lntype != rntype)
3917 if (lnbitsize > rnbitsize)
3919 lhs = convert (rntype, lhs);
3920 ll_mask = convert (rntype, ll_mask);
3921 type = rntype;
3923 else if (lnbitsize < rnbitsize)
3925 rhs = convert (lntype, rhs);
3926 lr_mask = convert (lntype, lr_mask);
3927 type = lntype;
3931 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
3932 lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
3934 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
3935 rhs = build (BIT_AND_EXPR, type, rhs, lr_mask);
3937 return build (wanted_code, truth_type, lhs, rhs);
3940 return 0;
3943 /* Handle the case of comparisons with constants. If there is something in
3944 common between the masks, those bits of the constants must be the same.
3945 If not, the condition is always false. Test for this to avoid generating
3946 incorrect code below. */
3947 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
3948 if (! integer_zerop (result)
3949 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
3950 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
3952 if (wanted_code == NE_EXPR)
3954 warning ("`or' of unmatched not-equal tests is always 1");
3955 return convert (truth_type, integer_one_node);
3957 else
3959 warning ("`and' of mutually exclusive equal-tests is always 0");
3960 return convert (truth_type, integer_zero_node);
3964 /* Construct the expression we will return. First get the component
3965 reference we will make. Unless the mask is all ones the width of
3966 that field, perform the mask operation. Then compare with the
3967 merged constant. */
3968 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
3969 ll_unsignedp || rl_unsignedp);
3971 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
3972 if (! all_ones_mask_p (ll_mask, lnbitsize))
3973 result = build (BIT_AND_EXPR, lntype, result, ll_mask);
3975 return build (wanted_code, truth_type, result,
3976 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
3979 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
3980 constant. */
3982 static tree
3983 optimize_minmax_comparison (t)
3984 tree t;
3986 tree type = TREE_TYPE (t);
3987 tree arg0 = TREE_OPERAND (t, 0);
3988 enum tree_code op_code;
3989 tree comp_const = TREE_OPERAND (t, 1);
3990 tree minmax_const;
3991 int consts_equal, consts_lt;
3992 tree inner;
3994 STRIP_SIGN_NOPS (arg0);
3996 op_code = TREE_CODE (arg0);
3997 minmax_const = TREE_OPERAND (arg0, 1);
3998 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
3999 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4000 inner = TREE_OPERAND (arg0, 0);
4002 /* If something does not permit us to optimize, return the original tree. */
4003 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4004 || TREE_CODE (comp_const) != INTEGER_CST
4005 || TREE_CONSTANT_OVERFLOW (comp_const)
4006 || TREE_CODE (minmax_const) != INTEGER_CST
4007 || TREE_CONSTANT_OVERFLOW (minmax_const))
4008 return t;
4010 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4011 and GT_EXPR, doing the rest with recursive calls using logical
4012 simplifications. */
4013 switch (TREE_CODE (t))
4015 case NE_EXPR: case LT_EXPR: case LE_EXPR:
4016 return
4017 invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t)));
4019 case GE_EXPR:
4020 return
4021 fold (build (TRUTH_ORIF_EXPR, type,
4022 optimize_minmax_comparison
4023 (build (EQ_EXPR, type, arg0, comp_const)),
4024 optimize_minmax_comparison
4025 (build (GT_EXPR, type, arg0, comp_const))));
4027 case EQ_EXPR:
4028 if (op_code == MAX_EXPR && consts_equal)
4029 /* MAX (X, 0) == 0 -> X <= 0 */
4030 return fold (build (LE_EXPR, type, inner, comp_const));
4032 else if (op_code == MAX_EXPR && consts_lt)
4033 /* MAX (X, 0) == 5 -> X == 5 */
4034 return fold (build (EQ_EXPR, type, inner, comp_const));
4036 else if (op_code == MAX_EXPR)
4037 /* MAX (X, 0) == -1 -> false */
4038 return omit_one_operand (type, integer_zero_node, inner);
4040 else if (consts_equal)
4041 /* MIN (X, 0) == 0 -> X >= 0 */
4042 return fold (build (GE_EXPR, type, inner, comp_const));
4044 else if (consts_lt)
4045 /* MIN (X, 0) == 5 -> false */
4046 return omit_one_operand (type, integer_zero_node, inner);
4048 else
4049 /* MIN (X, 0) == -1 -> X == -1 */
4050 return fold (build (EQ_EXPR, type, inner, comp_const));
4052 case GT_EXPR:
4053 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
4054 /* MAX (X, 0) > 0 -> X > 0
4055 MAX (X, 0) > 5 -> X > 5 */
4056 return fold (build (GT_EXPR, type, inner, comp_const));
4058 else if (op_code == MAX_EXPR)
4059 /* MAX (X, 0) > -1 -> true */
4060 return omit_one_operand (type, integer_one_node, inner);
4062 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
4063 /* MIN (X, 0) > 0 -> false
4064 MIN (X, 0) > 5 -> false */
4065 return omit_one_operand (type, integer_zero_node, inner);
4067 else
4068 /* MIN (X, 0) > -1 -> X > -1 */
4069 return fold (build (GT_EXPR, type, inner, comp_const));
4071 default:
4072 return t;
4076 /* T is an integer expression that is being multiplied, divided, or taken a
4077 modulus (CODE says which and what kind of divide or modulus) by a
4078 constant C. See if we can eliminate that operation by folding it with
4079 other operations already in T. WIDE_TYPE, if non-null, is a type that
4080 should be used for the computation if wider than our type.
4082 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
4083 (X * 2) + (Y * 4). We must, however, be assured that either the original
4084 expression would not overflow or that overflow is undefined for the type
4085 in the language in question.
4087 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
4088 the machine has a multiply-accumulate insn or that this is part of an
4089 addressing calculation.
4091 If we return a non-null expression, it is an equivalent form of the
4092 original computation, but need not be in the original type. */
4094 static tree
4095 extract_muldiv (t, c, code, wide_type)
4096 tree t;
4097 tree c;
4098 enum tree_code code;
4099 tree wide_type;
4101 /* To avoid exponential search depth, refuse to allow recursion past
4102 three levels. Beyond that (1) it's highly unlikely that we'll find
4103 something interesting and (2) we've probably processed it before
4104 when we built the inner expression. */
4106 static int depth;
4107 tree ret;
4109 if (depth > 3)
4110 return NULL;
4112 depth++;
4113 ret = extract_muldiv_1 (t, c, code, wide_type);
4114 depth--;
4116 return ret;
4119 static tree
4120 extract_muldiv_1 (t, c, code, wide_type)
4121 tree t;
4122 tree c;
4123 enum tree_code code;
4124 tree wide_type;
4126 tree type = TREE_TYPE (t);
4127 enum tree_code tcode = TREE_CODE (t);
4128 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
4129 > GET_MODE_SIZE (TYPE_MODE (type)))
4130 ? wide_type : type);
4131 tree t1, t2;
4132 int same_p = tcode == code;
4133 tree op0 = NULL_TREE, op1 = NULL_TREE;
4135 /* Don't deal with constants of zero here; they confuse the code below. */
4136 if (integer_zerop (c))
4137 return NULL_TREE;
4139 if (TREE_CODE_CLASS (tcode) == '1')
4140 op0 = TREE_OPERAND (t, 0);
4142 if (TREE_CODE_CLASS (tcode) == '2')
4143 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
4145 /* Note that we need not handle conditional operations here since fold
4146 already handles those cases. So just do arithmetic here. */
4147 switch (tcode)
4149 case INTEGER_CST:
4150 /* For a constant, we can always simplify if we are a multiply
4151 or (for divide and modulus) if it is a multiple of our constant. */
4152 if (code == MULT_EXPR
4153 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
4154 return const_binop (code, convert (ctype, t), convert (ctype, c), 0);
4155 break;
4157 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
4158 /* If op0 is an expression ... */
4159 if ((TREE_CODE_CLASS (TREE_CODE (op0)) == '<'
4160 || TREE_CODE_CLASS (TREE_CODE (op0)) == '1'
4161 || TREE_CODE_CLASS (TREE_CODE (op0)) == '2'
4162 || TREE_CODE_CLASS (TREE_CODE (op0)) == 'e')
4163 /* ... and is unsigned, and its type is smaller than ctype,
4164 then we cannot pass through as widening. */
4165 && ((TREE_UNSIGNED (TREE_TYPE (op0))
4166 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
4167 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
4168 && (GET_MODE_SIZE (TYPE_MODE (ctype))
4169 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
4170 /* ... or its type is larger than ctype,
4171 then we cannot pass through this truncation. */
4172 || (GET_MODE_SIZE (TYPE_MODE (ctype))
4173 < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
4174 /* ... or signedness changes for division or modulus,
4175 then we cannot pass through this conversion. */
4176 || (code != MULT_EXPR
4177 && (TREE_UNSIGNED (ctype)
4178 != TREE_UNSIGNED (TREE_TYPE (op0))))))
4179 break;
4181 /* Pass the constant down and see if we can make a simplification. If
4182 we can, replace this expression with the inner simplification for
4183 possible later conversion to our or some other type. */
4184 if (0 != (t1 = extract_muldiv (op0, convert (TREE_TYPE (op0), c), code,
4185 code == MULT_EXPR ? ctype : NULL_TREE)))
4186 return t1;
4187 break;
4189 case NEGATE_EXPR: case ABS_EXPR:
4190 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4191 return fold (build1 (tcode, ctype, convert (ctype, t1)));
4192 break;
4194 case MIN_EXPR: case MAX_EXPR:
4195 /* If widening the type changes the signedness, then we can't perform
4196 this optimization as that changes the result. */
4197 if (TREE_UNSIGNED (ctype) != TREE_UNSIGNED (type))
4198 break;
4200 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
4201 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
4202 && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
4204 if (tree_int_cst_sgn (c) < 0)
4205 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
4207 return fold (build (tcode, ctype, convert (ctype, t1),
4208 convert (ctype, t2)));
4210 break;
4212 case WITH_RECORD_EXPR:
4213 if ((t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code, wide_type)) != 0)
4214 return build (WITH_RECORD_EXPR, TREE_TYPE (t1), t1,
4215 TREE_OPERAND (t, 1));
4216 break;
4218 case SAVE_EXPR:
4219 /* If this has not been evaluated and the operand has no side effects,
4220 we can see if we can do something inside it and make a new one.
4221 Note that this test is overly conservative since we can do this
4222 if the only reason it had side effects is that it was another
4223 similar SAVE_EXPR, but that isn't worth bothering with. */
4224 if (SAVE_EXPR_RTL (t) == 0 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0))
4225 && 0 != (t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code,
4226 wide_type)))
4228 t1 = save_expr (t1);
4229 if (SAVE_EXPR_PERSISTENT_P (t) && TREE_CODE (t1) == SAVE_EXPR)
4230 SAVE_EXPR_PERSISTENT_P (t1) = 1;
4231 if (is_pending_size (t))
4232 put_pending_size (t1);
4233 return t1;
4235 break;
4237 case LSHIFT_EXPR: case RSHIFT_EXPR:
4238 /* If the second operand is constant, this is a multiplication
4239 or floor division, by a power of two, so we can treat it that
4240 way unless the multiplier or divisor overflows. */
4241 if (TREE_CODE (op1) == INTEGER_CST
4242 /* const_binop may not detect overflow correctly,
4243 so check for it explicitly here. */
4244 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
4245 && TREE_INT_CST_HIGH (op1) == 0
4246 && 0 != (t1 = convert (ctype,
4247 const_binop (LSHIFT_EXPR, size_one_node,
4248 op1, 0)))
4249 && ! TREE_OVERFLOW (t1))
4250 return extract_muldiv (build (tcode == LSHIFT_EXPR
4251 ? MULT_EXPR : FLOOR_DIV_EXPR,
4252 ctype, convert (ctype, op0), t1),
4253 c, code, wide_type);
4254 break;
4256 case PLUS_EXPR: case MINUS_EXPR:
4257 /* See if we can eliminate the operation on both sides. If we can, we
4258 can return a new PLUS or MINUS. If we can't, the only remaining
4259 cases where we can do anything are if the second operand is a
4260 constant. */
4261 t1 = extract_muldiv (op0, c, code, wide_type);
4262 t2 = extract_muldiv (op1, c, code, wide_type);
4263 if (t1 != 0 && t2 != 0
4264 && (code == MULT_EXPR
4265 /* If not multiplication, we can only do this if both operands
4266 are divisible by c. */
4267 || (multiple_of_p (ctype, op0, c)
4268 && multiple_of_p (ctype, op1, c))))
4269 return fold (build (tcode, ctype, convert (ctype, t1),
4270 convert (ctype, t2)));
4272 /* If this was a subtraction, negate OP1 and set it to be an addition.
4273 This simplifies the logic below. */
4274 if (tcode == MINUS_EXPR)
4275 tcode = PLUS_EXPR, op1 = negate_expr (op1);
4277 if (TREE_CODE (op1) != INTEGER_CST)
4278 break;
4280 /* If either OP1 or C are negative, this optimization is not safe for
4281 some of the division and remainder types while for others we need
4282 to change the code. */
4283 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
4285 if (code == CEIL_DIV_EXPR)
4286 code = FLOOR_DIV_EXPR;
4287 else if (code == FLOOR_DIV_EXPR)
4288 code = CEIL_DIV_EXPR;
4289 else if (code != MULT_EXPR
4290 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
4291 break;
4294 /* If it's a multiply or a division/modulus operation of a multiple
4295 of our constant, do the operation and verify it doesn't overflow. */
4296 if (code == MULT_EXPR
4297 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4299 op1 = const_binop (code, convert (ctype, op1), convert (ctype, c), 0);
4300 if (op1 == 0 || TREE_OVERFLOW (op1))
4301 break;
4303 else
4304 break;
4306 /* If we have an unsigned type is not a sizetype, we cannot widen
4307 the operation since it will change the result if the original
4308 computation overflowed. */
4309 if (TREE_UNSIGNED (ctype)
4310 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
4311 && ctype != type)
4312 break;
4314 /* If we were able to eliminate our operation from the first side,
4315 apply our operation to the second side and reform the PLUS. */
4316 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
4317 return fold (build (tcode, ctype, convert (ctype, t1), op1));
4319 /* The last case is if we are a multiply. In that case, we can
4320 apply the distributive law to commute the multiply and addition
4321 if the multiplication of the constants doesn't overflow. */
4322 if (code == MULT_EXPR)
4323 return fold (build (tcode, ctype, fold (build (code, ctype,
4324 convert (ctype, op0),
4325 convert (ctype, c))),
4326 op1));
4328 break;
4330 case MULT_EXPR:
4331 /* We have a special case here if we are doing something like
4332 (C * 8) % 4 since we know that's zero. */
4333 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
4334 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
4335 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
4336 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4337 return omit_one_operand (type, integer_zero_node, op0);
4339 /* ... fall through ... */
4341 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
4342 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
4343 /* If we can extract our operation from the LHS, do so and return a
4344 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
4345 do something only if the second operand is a constant. */
4346 if (same_p
4347 && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4348 return fold (build (tcode, ctype, convert (ctype, t1),
4349 convert (ctype, op1)));
4350 else if (tcode == MULT_EXPR && code == MULT_EXPR
4351 && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
4352 return fold (build (tcode, ctype, convert (ctype, op0),
4353 convert (ctype, t1)));
4354 else if (TREE_CODE (op1) != INTEGER_CST)
4355 return 0;
4357 /* If these are the same operation types, we can associate them
4358 assuming no overflow. */
4359 if (tcode == code
4360 && 0 != (t1 = const_binop (MULT_EXPR, convert (ctype, op1),
4361 convert (ctype, c), 0))
4362 && ! TREE_OVERFLOW (t1))
4363 return fold (build (tcode, ctype, convert (ctype, op0), t1));
4365 /* If these operations "cancel" each other, we have the main
4366 optimizations of this pass, which occur when either constant is a
4367 multiple of the other, in which case we replace this with either an
4368 operation or CODE or TCODE.
4370 If we have an unsigned type that is not a sizetype, we cannot do
4371 this since it will change the result if the original computation
4372 overflowed. */
4373 if ((! TREE_UNSIGNED (ctype)
4374 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
4375 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
4376 || (tcode == MULT_EXPR
4377 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
4378 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
4380 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4381 return fold (build (tcode, ctype, convert (ctype, op0),
4382 convert (ctype,
4383 const_binop (TRUNC_DIV_EXPR,
4384 op1, c, 0))));
4385 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
4386 return fold (build (code, ctype, convert (ctype, op0),
4387 convert (ctype,
4388 const_binop (TRUNC_DIV_EXPR,
4389 c, op1, 0))));
4391 break;
4393 default:
4394 break;
4397 return 0;
4400 /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
4401 S, a SAVE_EXPR, return the expression actually being evaluated. Note
4402 that we may sometimes modify the tree. */
4404 static tree
4405 strip_compound_expr (t, s)
4406 tree t;
4407 tree s;
4409 enum tree_code code = TREE_CODE (t);
4411 /* See if this is the COMPOUND_EXPR we want to eliminate. */
4412 if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
4413 && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
4414 return TREE_OPERAND (t, 1);
4416 /* See if this is a COND_EXPR or a simple arithmetic operator. We
4417 don't bother handling any other types. */
4418 else if (code == COND_EXPR)
4420 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4421 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4422 TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
4424 else if (TREE_CODE_CLASS (code) == '1')
4425 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4426 else if (TREE_CODE_CLASS (code) == '<'
4427 || TREE_CODE_CLASS (code) == '2')
4429 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4430 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4433 return t;
4436 /* Return a node which has the indicated constant VALUE (either 0 or
4437 1), and is of the indicated TYPE. */
4439 static tree
4440 constant_boolean_node (value, type)
4441 int value;
4442 tree type;
4444 if (type == integer_type_node)
4445 return value ? integer_one_node : integer_zero_node;
4446 else if (TREE_CODE (type) == BOOLEAN_TYPE)
4447 return (*lang_hooks.truthvalue_conversion) (value ? integer_one_node :
4448 integer_zero_node);
4449 else
4451 tree t = build_int_2 (value, 0);
4453 TREE_TYPE (t) = type;
4454 return t;
4458 /* Utility function for the following routine, to see how complex a nesting of
4459 COND_EXPRs can be. EXPR is the expression and LIMIT is a count beyond which
4460 we don't care (to avoid spending too much time on complex expressions.). */
4462 static int
4463 count_cond (expr, lim)
4464 tree expr;
4465 int lim;
4467 int ctrue, cfalse;
4469 if (TREE_CODE (expr) != COND_EXPR)
4470 return 0;
4471 else if (lim <= 0)
4472 return 0;
4474 ctrue = count_cond (TREE_OPERAND (expr, 1), lim - 1);
4475 cfalse = count_cond (TREE_OPERAND (expr, 2), lim - 1 - ctrue);
4476 return MIN (lim, 1 + ctrue + cfalse);
4479 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
4480 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
4481 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
4482 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
4483 COND is the first argument to CODE; otherwise (as in the example
4484 given here), it is the second argument. TYPE is the type of the
4485 original expression. */
4487 static tree
4488 fold_binary_op_with_conditional_arg (code, type, cond, arg, cond_first_p)
4489 enum tree_code code;
4490 tree type;
4491 tree cond;
4492 tree arg;
4493 int cond_first_p;
4495 tree test, true_value, false_value;
4496 tree lhs = NULL_TREE;
4497 tree rhs = NULL_TREE;
4498 /* In the end, we'll produce a COND_EXPR. Both arms of the
4499 conditional expression will be binary operations. The left-hand
4500 side of the expression to be executed if the condition is true
4501 will be pointed to by TRUE_LHS. Similarly, the right-hand side
4502 of the expression to be executed if the condition is true will be
4503 pointed to by TRUE_RHS. FALSE_LHS and FALSE_RHS are analogous --
4504 but apply to the expression to be executed if the conditional is
4505 false. */
4506 tree *true_lhs;
4507 tree *true_rhs;
4508 tree *false_lhs;
4509 tree *false_rhs;
4510 /* These are the codes to use for the left-hand side and right-hand
4511 side of the COND_EXPR. Normally, they are the same as CODE. */
4512 enum tree_code lhs_code = code;
4513 enum tree_code rhs_code = code;
4514 /* And these are the types of the expressions. */
4515 tree lhs_type = type;
4516 tree rhs_type = type;
4517 int save = 0;
4519 if (cond_first_p)
4521 true_rhs = false_rhs = &arg;
4522 true_lhs = &true_value;
4523 false_lhs = &false_value;
4525 else
4527 true_lhs = false_lhs = &arg;
4528 true_rhs = &true_value;
4529 false_rhs = &false_value;
4532 if (TREE_CODE (cond) == COND_EXPR)
4534 test = TREE_OPERAND (cond, 0);
4535 true_value = TREE_OPERAND (cond, 1);
4536 false_value = TREE_OPERAND (cond, 2);
4537 /* If this operand throws an expression, then it does not make
4538 sense to try to perform a logical or arithmetic operation
4539 involving it. Instead of building `a + throw 3' for example,
4540 we simply build `a, throw 3'. */
4541 if (VOID_TYPE_P (TREE_TYPE (true_value)))
4543 if (! cond_first_p)
4545 lhs_code = COMPOUND_EXPR;
4546 lhs_type = void_type_node;
4548 else
4549 lhs = true_value;
4551 if (VOID_TYPE_P (TREE_TYPE (false_value)))
4553 if (! cond_first_p)
4555 rhs_code = COMPOUND_EXPR;
4556 rhs_type = void_type_node;
4558 else
4559 rhs = false_value;
4562 else
4564 tree testtype = TREE_TYPE (cond);
4565 test = cond;
4566 true_value = convert (testtype, integer_one_node);
4567 false_value = convert (testtype, integer_zero_node);
4570 /* If ARG is complex we want to make sure we only evaluate
4571 it once. Though this is only required if it is volatile, it
4572 might be more efficient even if it is not. However, if we
4573 succeed in folding one part to a constant, we do not need
4574 to make this SAVE_EXPR. Since we do this optimization
4575 primarily to see if we do end up with constant and this
4576 SAVE_EXPR interferes with later optimizations, suppressing
4577 it when we can is important.
4579 If we are not in a function, we can't make a SAVE_EXPR, so don't
4580 try to do so. Don't try to see if the result is a constant
4581 if an arm is a COND_EXPR since we get exponential behavior
4582 in that case. */
4584 if (TREE_CODE (arg) == SAVE_EXPR)
4585 save = 1;
4586 else if (lhs == 0 && rhs == 0
4587 && !TREE_CONSTANT (arg)
4588 && (*lang_hooks.decls.global_bindings_p) () == 0
4589 && ((TREE_CODE (arg) != VAR_DECL && TREE_CODE (arg) != PARM_DECL)
4590 || TREE_SIDE_EFFECTS (arg)))
4592 if (TREE_CODE (true_value) != COND_EXPR)
4593 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4595 if (TREE_CODE (false_value) != COND_EXPR)
4596 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4598 if ((lhs == 0 || ! TREE_CONSTANT (lhs))
4599 && (rhs == 0 || !TREE_CONSTANT (rhs)))
4601 arg = save_expr (arg);
4602 lhs = rhs = 0;
4603 save = 1;
4607 if (lhs == 0)
4608 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4609 if (rhs == 0)
4610 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4612 test = fold (build (COND_EXPR, type, test, lhs, rhs));
4614 if (save)
4615 return build (COMPOUND_EXPR, type,
4616 convert (void_type_node, arg),
4617 strip_compound_expr (test, arg));
4618 else
4619 return convert (type, test);
4623 /* Subroutine of fold() that checks for the addition of +/- 0.0.
4625 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
4626 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
4627 ADDEND is the same as X.
4629 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
4630 and finite. The problematic cases are when X is zero, and its mode
4631 has signed zeros. In the case of rounding towards -infinity,
4632 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
4633 modes, X + 0 is not the same as X because -0 + 0 is 0. */
4635 static bool
4636 fold_real_zero_addition_p (type, addend, negate)
4637 tree type, addend;
4638 int negate;
4640 if (!real_zerop (addend))
4641 return false;
4643 /* Don't allow the fold with -fsignaling-nans. */
4644 if (HONOR_SNANS (TYPE_MODE (type)))
4645 return false;
4647 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
4648 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
4649 return true;
4651 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
4652 if (TREE_CODE (addend) == REAL_CST
4653 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
4654 negate = !negate;
4656 /* The mode has signed zeros, and we have to honor their sign.
4657 In this situation, there is only one case we can return true for.
4658 X - 0 is the same as X unless rounding towards -infinity is
4659 supported. */
4660 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
4663 /* Subroutine of fold() that checks comparisons of built-in math
4664 functions against real constants.
4666 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
4667 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
4668 is the type of the result and ARG0 and ARG1 are the operands of the
4669 comparison. ARG1 must be a TREE_REAL_CST.
4671 The function returns the constant folded tree if a simplification
4672 can be made, and NULL_TREE otherwise. */
4674 static tree
4675 fold_mathfn_compare (fcode, code, type, arg0, arg1)
4676 enum built_in_function fcode;
4677 enum tree_code code;
4678 tree type, arg0, arg1;
4680 REAL_VALUE_TYPE c;
4682 if (fcode == BUILT_IN_SQRT
4683 || fcode == BUILT_IN_SQRTF
4684 || fcode == BUILT_IN_SQRTL)
4686 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
4687 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
4689 c = TREE_REAL_CST (arg1);
4690 if (REAL_VALUE_NEGATIVE (c))
4692 /* sqrt(x) < y is always false, if y is negative. */
4693 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
4694 return omit_one_operand (type,
4695 convert (type, integer_zero_node),
4696 arg);
4698 /* sqrt(x) > y is always true, if y is negative and we
4699 don't care about NaNs, i.e. negative values of x. */
4700 if (code == NE_EXPR || !HONOR_NANS (mode))
4701 return omit_one_operand (type,
4702 convert (type, integer_one_node),
4703 arg);
4705 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
4706 return fold (build (GE_EXPR, type, arg,
4707 build_real (TREE_TYPE (arg), dconst0)));
4709 else if (code == GT_EXPR || code == GE_EXPR)
4711 REAL_VALUE_TYPE c2;
4713 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
4714 real_convert (&c2, mode, &c2);
4716 if (REAL_VALUE_ISINF (c2))
4718 /* sqrt(x) > y is x == +Inf, when y is very large. */
4719 if (HONOR_INFINITIES (mode))
4720 return fold (build (EQ_EXPR, type, arg,
4721 build_real (TREE_TYPE (arg), c2)));
4723 /* sqrt(x) > y is always false, when y is very large
4724 and we don't care about infinities. */
4725 return omit_one_operand (type,
4726 convert (type, integer_zero_node),
4727 arg);
4730 /* sqrt(x) > c is the same as x > c*c. */
4731 return fold (build (code, type, arg,
4732 build_real (TREE_TYPE (arg), c2)));
4734 else if (code == LT_EXPR || code == LE_EXPR)
4736 REAL_VALUE_TYPE c2;
4738 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
4739 real_convert (&c2, mode, &c2);
4741 if (REAL_VALUE_ISINF (c2))
4743 /* sqrt(x) < y is always true, when y is a very large
4744 value and we don't care about NaNs or Infinities. */
4745 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
4746 return omit_one_operand (type,
4747 convert (type, integer_one_node),
4748 arg);
4750 /* sqrt(x) < y is x != +Inf when y is very large and we
4751 don't care about NaNs. */
4752 if (! HONOR_NANS (mode))
4753 return fold (build (NE_EXPR, type, arg,
4754 build_real (TREE_TYPE (arg), c2)));
4756 /* sqrt(x) < y is x >= 0 when y is very large and we
4757 don't care about Infinities. */
4758 if (! HONOR_INFINITIES (mode))
4759 return fold (build (GE_EXPR, type, arg,
4760 build_real (TREE_TYPE (arg), dconst0)));
4762 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
4763 if ((*lang_hooks.decls.global_bindings_p) () != 0
4764 || contains_placeholder_p (arg))
4765 return NULL_TREE;
4767 arg = save_expr (arg);
4768 return fold (build (TRUTH_ANDIF_EXPR, type,
4769 fold (build (GE_EXPR, type, arg,
4770 build_real (TREE_TYPE (arg),
4771 dconst0))),
4772 fold (build (NE_EXPR, type, arg,
4773 build_real (TREE_TYPE (arg),
4774 c2)))));
4777 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
4778 if (! HONOR_NANS (mode))
4779 return fold (build (code, type, arg,
4780 build_real (TREE_TYPE (arg), c2)));
4782 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
4783 if ((*lang_hooks.decls.global_bindings_p) () == 0
4784 && ! contains_placeholder_p (arg))
4786 arg = save_expr (arg);
4787 return fold (build (TRUTH_ANDIF_EXPR, type,
4788 fold (build (GE_EXPR, type, arg,
4789 build_real (TREE_TYPE (arg),
4790 dconst0))),
4791 fold (build (code, type, arg,
4792 build_real (TREE_TYPE (arg),
4793 c2)))));
4798 return NULL_TREE;
4802 /* Perform constant folding and related simplification of EXPR.
4803 The related simplifications include x*1 => x, x*0 => 0, etc.,
4804 and application of the associative law.
4805 NOP_EXPR conversions may be removed freely (as long as we
4806 are careful not to change the C type of the overall expression)
4807 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
4808 but we can constant-fold them if they have constant operands. */
4810 tree
4811 fold (expr)
4812 tree expr;
4814 tree t = expr;
4815 tree t1 = NULL_TREE;
4816 tree tem;
4817 tree type = TREE_TYPE (expr);
4818 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4819 enum tree_code code = TREE_CODE (t);
4820 int kind = TREE_CODE_CLASS (code);
4821 int invert;
4822 /* WINS will be nonzero when the switch is done
4823 if all operands are constant. */
4824 int wins = 1;
4826 /* Don't try to process an RTL_EXPR since its operands aren't trees.
4827 Likewise for a SAVE_EXPR that's already been evaluated. */
4828 if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t) != 0))
4829 return t;
4831 /* Return right away if a constant. */
4832 if (kind == 'c')
4833 return t;
4835 #ifdef MAX_INTEGER_COMPUTATION_MODE
4836 check_max_integer_computation_mode (expr);
4837 #endif
4839 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
4841 tree subop;
4843 /* Special case for conversion ops that can have fixed point args. */
4844 arg0 = TREE_OPERAND (t, 0);
4846 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
4847 if (arg0 != 0)
4848 STRIP_SIGN_NOPS (arg0);
4850 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
4851 subop = TREE_REALPART (arg0);
4852 else
4853 subop = arg0;
4855 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
4856 && TREE_CODE (subop) != REAL_CST
4858 /* Note that TREE_CONSTANT isn't enough:
4859 static var addresses are constant but we can't
4860 do arithmetic on them. */
4861 wins = 0;
4863 else if (IS_EXPR_CODE_CLASS (kind) || kind == 'r')
4865 int len = first_rtl_op (code);
4866 int i;
4867 for (i = 0; i < len; i++)
4869 tree op = TREE_OPERAND (t, i);
4870 tree subop;
4872 if (op == 0)
4873 continue; /* Valid for CALL_EXPR, at least. */
4875 if (kind == '<' || code == RSHIFT_EXPR)
4877 /* Signedness matters here. Perhaps we can refine this
4878 later. */
4879 STRIP_SIGN_NOPS (op);
4881 else
4882 /* Strip any conversions that don't change the mode. */
4883 STRIP_NOPS (op);
4885 if (TREE_CODE (op) == COMPLEX_CST)
4886 subop = TREE_REALPART (op);
4887 else
4888 subop = op;
4890 if (TREE_CODE (subop) != INTEGER_CST
4891 && TREE_CODE (subop) != REAL_CST)
4892 /* Note that TREE_CONSTANT isn't enough:
4893 static var addresses are constant but we can't
4894 do arithmetic on them. */
4895 wins = 0;
4897 if (i == 0)
4898 arg0 = op;
4899 else if (i == 1)
4900 arg1 = op;
4904 /* If this is a commutative operation, and ARG0 is a constant, move it
4905 to ARG1 to reduce the number of tests below. */
4906 if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
4907 || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
4908 || code == BIT_AND_EXPR)
4909 && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
4911 tem = arg0; arg0 = arg1; arg1 = tem;
4913 tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
4914 TREE_OPERAND (t, 1) = tem;
4917 /* Now WINS is set as described above,
4918 ARG0 is the first operand of EXPR,
4919 and ARG1 is the second operand (if it has more than one operand).
4921 First check for cases where an arithmetic operation is applied to a
4922 compound, conditional, or comparison operation. Push the arithmetic
4923 operation inside the compound or conditional to see if any folding
4924 can then be done. Convert comparison to conditional for this purpose.
4925 The also optimizes non-constant cases that used to be done in
4926 expand_expr.
4928 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
4929 one of the operands is a comparison and the other is a comparison, a
4930 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
4931 code below would make the expression more complex. Change it to a
4932 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
4933 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
4935 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
4936 || code == EQ_EXPR || code == NE_EXPR)
4937 && ((truth_value_p (TREE_CODE (arg0))
4938 && (truth_value_p (TREE_CODE (arg1))
4939 || (TREE_CODE (arg1) == BIT_AND_EXPR
4940 && integer_onep (TREE_OPERAND (arg1, 1)))))
4941 || (truth_value_p (TREE_CODE (arg1))
4942 && (truth_value_p (TREE_CODE (arg0))
4943 || (TREE_CODE (arg0) == BIT_AND_EXPR
4944 && integer_onep (TREE_OPERAND (arg0, 1)))))))
4946 t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
4947 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
4948 : TRUTH_XOR_EXPR,
4949 type, arg0, arg1));
4951 if (code == EQ_EXPR)
4952 t = invert_truthvalue (t);
4954 return t;
4957 if (TREE_CODE_CLASS (code) == '1')
4959 if (TREE_CODE (arg0) == COMPOUND_EXPR)
4960 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
4961 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
4962 else if (TREE_CODE (arg0) == COND_EXPR)
4964 tree arg01 = TREE_OPERAND (arg0, 1);
4965 tree arg02 = TREE_OPERAND (arg0, 2);
4966 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
4967 arg01 = fold (build1 (code, type, arg01));
4968 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
4969 arg02 = fold (build1 (code, type, arg02));
4970 t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
4971 arg01, arg02));
4973 /* If this was a conversion, and all we did was to move into
4974 inside the COND_EXPR, bring it back out. But leave it if
4975 it is a conversion from integer to integer and the
4976 result precision is no wider than a word since such a
4977 conversion is cheap and may be optimized away by combine,
4978 while it couldn't if it were outside the COND_EXPR. Then return
4979 so we don't get into an infinite recursion loop taking the
4980 conversion out and then back in. */
4982 if ((code == NOP_EXPR || code == CONVERT_EXPR
4983 || code == NON_LVALUE_EXPR)
4984 && TREE_CODE (t) == COND_EXPR
4985 && TREE_CODE (TREE_OPERAND (t, 1)) == code
4986 && TREE_CODE (TREE_OPERAND (t, 2)) == code
4987 && ! VOID_TYPE_P (TREE_OPERAND (t, 1))
4988 && ! VOID_TYPE_P (TREE_OPERAND (t, 2))
4989 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
4990 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0)))
4991 && ! (INTEGRAL_TYPE_P (TREE_TYPE (t))
4992 && (INTEGRAL_TYPE_P
4993 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))))
4994 && TYPE_PRECISION (TREE_TYPE (t)) <= BITS_PER_WORD))
4995 t = build1 (code, type,
4996 build (COND_EXPR,
4997 TREE_TYPE (TREE_OPERAND
4998 (TREE_OPERAND (t, 1), 0)),
4999 TREE_OPERAND (t, 0),
5000 TREE_OPERAND (TREE_OPERAND (t, 1), 0),
5001 TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
5002 return t;
5004 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
5005 return fold (build (COND_EXPR, type, arg0,
5006 fold (build1 (code, type, integer_one_node)),
5007 fold (build1 (code, type, integer_zero_node))));
5009 else if (TREE_CODE_CLASS (code) == '2'
5010 || TREE_CODE_CLASS (code) == '<')
5012 if (TREE_CODE (arg1) == COMPOUND_EXPR
5013 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg1, 0))
5014 && ! TREE_SIDE_EFFECTS (arg0))
5015 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5016 fold (build (code, type,
5017 arg0, TREE_OPERAND (arg1, 1))));
5018 else if ((TREE_CODE (arg1) == COND_EXPR
5019 || (TREE_CODE_CLASS (TREE_CODE (arg1)) == '<'
5020 && TREE_CODE_CLASS (code) != '<'))
5021 && (TREE_CODE (arg0) != COND_EXPR
5022 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
5023 && (! TREE_SIDE_EFFECTS (arg0)
5024 || ((*lang_hooks.decls.global_bindings_p) () == 0
5025 && ! contains_placeholder_p (arg0))))
5026 return
5027 fold_binary_op_with_conditional_arg (code, type, arg1, arg0,
5028 /*cond_first_p=*/0);
5029 else if (TREE_CODE (arg0) == COMPOUND_EXPR)
5030 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5031 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5032 else if ((TREE_CODE (arg0) == COND_EXPR
5033 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
5034 && TREE_CODE_CLASS (code) != '<'))
5035 && (TREE_CODE (arg1) != COND_EXPR
5036 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
5037 && (! TREE_SIDE_EFFECTS (arg1)
5038 || ((*lang_hooks.decls.global_bindings_p) () == 0
5039 && ! contains_placeholder_p (arg1))))
5040 return
5041 fold_binary_op_with_conditional_arg (code, type, arg0, arg1,
5042 /*cond_first_p=*/1);
5044 else if (TREE_CODE_CLASS (code) == '<'
5045 && TREE_CODE (arg0) == COMPOUND_EXPR)
5046 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5047 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5048 else if (TREE_CODE_CLASS (code) == '<'
5049 && TREE_CODE (arg1) == COMPOUND_EXPR)
5050 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5051 fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
5053 switch (code)
5055 case INTEGER_CST:
5056 case REAL_CST:
5057 case VECTOR_CST:
5058 case STRING_CST:
5059 case COMPLEX_CST:
5060 case CONSTRUCTOR:
5061 return t;
5063 case CONST_DECL:
5064 return fold (DECL_INITIAL (t));
5066 case NOP_EXPR:
5067 case FLOAT_EXPR:
5068 case CONVERT_EXPR:
5069 case FIX_TRUNC_EXPR:
5070 /* Other kinds of FIX are not handled properly by fold_convert. */
5072 if (TREE_TYPE (TREE_OPERAND (t, 0)) == TREE_TYPE (t))
5073 return TREE_OPERAND (t, 0);
5075 /* Handle cases of two conversions in a row. */
5076 if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
5077 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
5079 tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5080 tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
5081 tree final_type = TREE_TYPE (t);
5082 int inside_int = INTEGRAL_TYPE_P (inside_type);
5083 int inside_ptr = POINTER_TYPE_P (inside_type);
5084 int inside_float = FLOAT_TYPE_P (inside_type);
5085 unsigned int inside_prec = TYPE_PRECISION (inside_type);
5086 int inside_unsignedp = TREE_UNSIGNED (inside_type);
5087 int inter_int = INTEGRAL_TYPE_P (inter_type);
5088 int inter_ptr = POINTER_TYPE_P (inter_type);
5089 int inter_float = FLOAT_TYPE_P (inter_type);
5090 unsigned int inter_prec = TYPE_PRECISION (inter_type);
5091 int inter_unsignedp = TREE_UNSIGNED (inter_type);
5092 int final_int = INTEGRAL_TYPE_P (final_type);
5093 int final_ptr = POINTER_TYPE_P (final_type);
5094 int final_float = FLOAT_TYPE_P (final_type);
5095 unsigned int final_prec = TYPE_PRECISION (final_type);
5096 int final_unsignedp = TREE_UNSIGNED (final_type);
5098 /* In addition to the cases of two conversions in a row
5099 handled below, if we are converting something to its own
5100 type via an object of identical or wider precision, neither
5101 conversion is needed. */
5102 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (final_type)
5103 && ((inter_int && final_int) || (inter_float && final_float))
5104 && inter_prec >= final_prec)
5105 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5107 /* Likewise, if the intermediate and final types are either both
5108 float or both integer, we don't need the middle conversion if
5109 it is wider than the final type and doesn't change the signedness
5110 (for integers). Avoid this if the final type is a pointer
5111 since then we sometimes need the inner conversion. Likewise if
5112 the outer has a precision not equal to the size of its mode. */
5113 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
5114 || (inter_float && inside_float))
5115 && inter_prec >= inside_prec
5116 && (inter_float || inter_unsignedp == inside_unsignedp)
5117 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5118 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5119 && ! final_ptr)
5120 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5122 /* If we have a sign-extension of a zero-extended value, we can
5123 replace that by a single zero-extension. */
5124 if (inside_int && inter_int && final_int
5125 && inside_prec < inter_prec && inter_prec < final_prec
5126 && inside_unsignedp && !inter_unsignedp)
5127 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5129 /* Two conversions in a row are not needed unless:
5130 - some conversion is floating-point (overstrict for now), or
5131 - the intermediate type is narrower than both initial and
5132 final, or
5133 - the intermediate type and innermost type differ in signedness,
5134 and the outermost type is wider than the intermediate, or
5135 - the initial type is a pointer type and the precisions of the
5136 intermediate and final types differ, or
5137 - the final type is a pointer type and the precisions of the
5138 initial and intermediate types differ. */
5139 if (! inside_float && ! inter_float && ! final_float
5140 && (inter_prec > inside_prec || inter_prec > final_prec)
5141 && ! (inside_int && inter_int
5142 && inter_unsignedp != inside_unsignedp
5143 && inter_prec < final_prec)
5144 && ((inter_unsignedp && inter_prec > inside_prec)
5145 == (final_unsignedp && final_prec > inter_prec))
5146 && ! (inside_ptr && inter_prec != final_prec)
5147 && ! (final_ptr && inside_prec != inter_prec)
5148 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5149 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5150 && ! final_ptr)
5151 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5154 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
5155 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
5156 /* Detect assigning a bitfield. */
5157 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
5158 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
5160 /* Don't leave an assignment inside a conversion
5161 unless assigning a bitfield. */
5162 tree prev = TREE_OPERAND (t, 0);
5163 TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
5164 /* First do the assignment, then return converted constant. */
5165 t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
5166 TREE_USED (t) = 1;
5167 return t;
5170 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
5171 constants (if x has signed type, the sign bit cannot be set
5172 in c). This folds extension into the BIT_AND_EXPR. */
5173 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
5174 && TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE
5175 && TREE_CODE (TREE_OPERAND (t, 0)) == BIT_AND_EXPR
5176 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST)
5178 tree and = TREE_OPERAND (t, 0);
5179 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
5180 int change = 0;
5182 if (TREE_UNSIGNED (TREE_TYPE (and))
5183 || (TYPE_PRECISION (TREE_TYPE (t))
5184 <= TYPE_PRECISION (TREE_TYPE (and))))
5185 change = 1;
5186 else if (TYPE_PRECISION (TREE_TYPE (and1))
5187 <= HOST_BITS_PER_WIDE_INT
5188 && host_integerp (and1, 1))
5190 unsigned HOST_WIDE_INT cst;
5192 cst = tree_low_cst (and1, 1);
5193 cst &= (HOST_WIDE_INT) -1
5194 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
5195 change = (cst == 0);
5196 #ifdef LOAD_EXTEND_OP
5197 if (change
5198 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
5199 == ZERO_EXTEND))
5201 tree uns = (*lang_hooks.types.unsigned_type) (TREE_TYPE (and0));
5202 and0 = convert (uns, and0);
5203 and1 = convert (uns, and1);
5205 #endif
5207 if (change)
5208 return fold (build (BIT_AND_EXPR, TREE_TYPE (t),
5209 convert (TREE_TYPE (t), and0),
5210 convert (TREE_TYPE (t), and1)));
5213 if (!wins)
5215 TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
5216 return t;
5218 return fold_convert (t, arg0);
5220 case VIEW_CONVERT_EXPR:
5221 if (TREE_CODE (TREE_OPERAND (t, 0)) == VIEW_CONVERT_EXPR)
5222 return build1 (VIEW_CONVERT_EXPR, type,
5223 TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5224 return t;
5226 case COMPONENT_REF:
5227 if (TREE_CODE (arg0) == CONSTRUCTOR)
5229 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
5230 if (m)
5231 t = TREE_VALUE (m);
5233 return t;
5235 case RANGE_EXPR:
5236 TREE_CONSTANT (t) = wins;
5237 return t;
5239 case NEGATE_EXPR:
5240 if (wins)
5242 if (TREE_CODE (arg0) == INTEGER_CST)
5244 unsigned HOST_WIDE_INT low;
5245 HOST_WIDE_INT high;
5246 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5247 TREE_INT_CST_HIGH (arg0),
5248 &low, &high);
5249 t = build_int_2 (low, high);
5250 TREE_TYPE (t) = type;
5251 TREE_OVERFLOW (t)
5252 = (TREE_OVERFLOW (arg0)
5253 | force_fit_type (t, overflow && !TREE_UNSIGNED (type)));
5254 TREE_CONSTANT_OVERFLOW (t)
5255 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
5257 else if (TREE_CODE (arg0) == REAL_CST)
5258 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5260 else if (TREE_CODE (arg0) == NEGATE_EXPR)
5261 return TREE_OPERAND (arg0, 0);
5262 /* Convert -((double)float) into (double)(-float). */
5263 else if (TREE_CODE (arg0) == NOP_EXPR
5264 && TREE_CODE (type) == REAL_TYPE)
5266 tree targ0 = strip_float_extensions (arg0);
5267 if (targ0 != arg0)
5268 return convert (type, build1 (NEGATE_EXPR, TREE_TYPE (targ0), targ0));
5272 /* Convert - (a - b) to (b - a) for non-floating-point. */
5273 else if (TREE_CODE (arg0) == MINUS_EXPR
5274 && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
5275 return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
5276 TREE_OPERAND (arg0, 0));
5278 return t;
5280 case ABS_EXPR:
5281 if (wins)
5283 if (TREE_CODE (arg0) == INTEGER_CST)
5285 /* If the value is unsigned, then the absolute value is
5286 the same as the ordinary value. */
5287 if (TREE_UNSIGNED (type))
5288 return arg0;
5289 /* Similarly, if the value is non-negative. */
5290 else if (INT_CST_LT (integer_minus_one_node, arg0))
5291 return arg0;
5292 /* If the value is negative, then the absolute value is
5293 its negation. */
5294 else
5296 unsigned HOST_WIDE_INT low;
5297 HOST_WIDE_INT high;
5298 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5299 TREE_INT_CST_HIGH (arg0),
5300 &low, &high);
5301 t = build_int_2 (low, high);
5302 TREE_TYPE (t) = type;
5303 TREE_OVERFLOW (t)
5304 = (TREE_OVERFLOW (arg0)
5305 | force_fit_type (t, overflow));
5306 TREE_CONSTANT_OVERFLOW (t)
5307 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
5310 else if (TREE_CODE (arg0) == REAL_CST)
5312 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
5313 t = build_real (type,
5314 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5317 else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
5318 return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
5319 /* Convert fabs((double)float) into (double)fabsf(float). */
5320 else if (TREE_CODE (arg0) == NOP_EXPR
5321 && TREE_CODE (type) == REAL_TYPE)
5323 tree targ0 = strip_float_extensions (arg0);
5324 if (targ0 != arg0)
5325 return convert (type, build1 (ABS_EXPR, TREE_TYPE (targ0), targ0));
5328 else
5330 /* fabs(sqrt(x)) = sqrt(x) and fabs(exp(x)) = exp(x). */
5331 enum built_in_function fcode = builtin_mathfn_code (arg0);
5332 if (fcode == BUILT_IN_SQRT
5333 || fcode == BUILT_IN_SQRTF
5334 || fcode == BUILT_IN_SQRTL
5335 || fcode == BUILT_IN_EXP
5336 || fcode == BUILT_IN_EXPF
5337 || fcode == BUILT_IN_EXPL)
5338 t = arg0;
5340 return t;
5342 case CONJ_EXPR:
5343 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
5344 return convert (type, arg0);
5345 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
5346 return build (COMPLEX_EXPR, type,
5347 TREE_OPERAND (arg0, 0),
5348 negate_expr (TREE_OPERAND (arg0, 1)));
5349 else if (TREE_CODE (arg0) == COMPLEX_CST)
5350 return build_complex (type, TREE_REALPART (arg0),
5351 negate_expr (TREE_IMAGPART (arg0)));
5352 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
5353 return fold (build (TREE_CODE (arg0), type,
5354 fold (build1 (CONJ_EXPR, type,
5355 TREE_OPERAND (arg0, 0))),
5356 fold (build1 (CONJ_EXPR,
5357 type, TREE_OPERAND (arg0, 1)))));
5358 else if (TREE_CODE (arg0) == CONJ_EXPR)
5359 return TREE_OPERAND (arg0, 0);
5360 return t;
5362 case BIT_NOT_EXPR:
5363 if (wins)
5365 t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
5366 ~ TREE_INT_CST_HIGH (arg0));
5367 TREE_TYPE (t) = type;
5368 force_fit_type (t, 0);
5369 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
5370 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
5372 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
5373 return TREE_OPERAND (arg0, 0);
5374 return t;
5376 case PLUS_EXPR:
5377 /* A + (-B) -> A - B */
5378 if (TREE_CODE (arg1) == NEGATE_EXPR)
5379 return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5380 /* (-A) + B -> B - A */
5381 if (TREE_CODE (arg0) == NEGATE_EXPR)
5382 return fold (build (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
5383 else if (! FLOAT_TYPE_P (type))
5385 if (integer_zerop (arg1))
5386 return non_lvalue (convert (type, arg0));
5388 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
5389 with a constant, and the two constants have no bits in common,
5390 we should treat this as a BIT_IOR_EXPR since this may produce more
5391 simplifications. */
5392 if (TREE_CODE (arg0) == BIT_AND_EXPR
5393 && TREE_CODE (arg1) == BIT_AND_EXPR
5394 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5395 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5396 && integer_zerop (const_binop (BIT_AND_EXPR,
5397 TREE_OPERAND (arg0, 1),
5398 TREE_OPERAND (arg1, 1), 0)))
5400 code = BIT_IOR_EXPR;
5401 goto bit_ior;
5404 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
5405 (plus (plus (mult) (mult)) (foo)) so that we can
5406 take advantage of the factoring cases below. */
5407 if ((TREE_CODE (arg0) == PLUS_EXPR
5408 && TREE_CODE (arg1) == MULT_EXPR)
5409 || (TREE_CODE (arg1) == PLUS_EXPR
5410 && TREE_CODE (arg0) == MULT_EXPR))
5412 tree parg0, parg1, parg, marg;
5414 if (TREE_CODE (arg0) == PLUS_EXPR)
5415 parg = arg0, marg = arg1;
5416 else
5417 parg = arg1, marg = arg0;
5418 parg0 = TREE_OPERAND (parg, 0);
5419 parg1 = TREE_OPERAND (parg, 1);
5420 STRIP_NOPS (parg0);
5421 STRIP_NOPS (parg1);
5423 if (TREE_CODE (parg0) == MULT_EXPR
5424 && TREE_CODE (parg1) != MULT_EXPR)
5425 return fold (build (PLUS_EXPR, type,
5426 fold (build (PLUS_EXPR, type, parg0, marg)),
5427 parg1));
5428 if (TREE_CODE (parg0) != MULT_EXPR
5429 && TREE_CODE (parg1) == MULT_EXPR)
5430 return fold (build (PLUS_EXPR, type,
5431 fold (build (PLUS_EXPR, type, parg1, marg)),
5432 parg0));
5435 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
5437 tree arg00, arg01, arg10, arg11;
5438 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
5440 /* (A * C) + (B * C) -> (A+B) * C.
5441 We are most concerned about the case where C is a constant,
5442 but other combinations show up during loop reduction. Since
5443 it is not difficult, try all four possibilities. */
5445 arg00 = TREE_OPERAND (arg0, 0);
5446 arg01 = TREE_OPERAND (arg0, 1);
5447 arg10 = TREE_OPERAND (arg1, 0);
5448 arg11 = TREE_OPERAND (arg1, 1);
5449 same = NULL_TREE;
5451 if (operand_equal_p (arg01, arg11, 0))
5452 same = arg01, alt0 = arg00, alt1 = arg10;
5453 else if (operand_equal_p (arg00, arg10, 0))
5454 same = arg00, alt0 = arg01, alt1 = arg11;
5455 else if (operand_equal_p (arg00, arg11, 0))
5456 same = arg00, alt0 = arg01, alt1 = arg10;
5457 else if (operand_equal_p (arg01, arg10, 0))
5458 same = arg01, alt0 = arg00, alt1 = arg11;
5460 /* No identical multiplicands; see if we can find a common
5461 power-of-two factor in non-power-of-two multiplies. This
5462 can help in multi-dimensional array access. */
5463 else if (TREE_CODE (arg01) == INTEGER_CST
5464 && TREE_CODE (arg11) == INTEGER_CST
5465 && TREE_INT_CST_HIGH (arg01) == 0
5466 && TREE_INT_CST_HIGH (arg11) == 0)
5468 HOST_WIDE_INT int01, int11, tmp;
5469 int01 = TREE_INT_CST_LOW (arg01);
5470 int11 = TREE_INT_CST_LOW (arg11);
5472 /* Move min of absolute values to int11. */
5473 if ((int01 >= 0 ? int01 : -int01)
5474 < (int11 >= 0 ? int11 : -int11))
5476 tmp = int01, int01 = int11, int11 = tmp;
5477 alt0 = arg00, arg00 = arg10, arg10 = alt0;
5478 alt0 = arg01, arg01 = arg11, arg11 = alt0;
5481 if (exact_log2 (int11) > 0 && int01 % int11 == 0)
5483 alt0 = fold (build (MULT_EXPR, type, arg00,
5484 build_int_2 (int01 / int11, 0)));
5485 alt1 = arg10;
5486 same = arg11;
5490 if (same)
5491 return fold (build (MULT_EXPR, type,
5492 fold (build (PLUS_EXPR, type, alt0, alt1)),
5493 same));
5497 /* See if ARG1 is zero and X + ARG1 reduces to X. */
5498 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
5499 return non_lvalue (convert (type, arg0));
5501 /* Likewise if the operands are reversed. */
5502 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
5503 return non_lvalue (convert (type, arg1));
5505 bit_rotate:
5506 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
5507 is a rotate of A by C1 bits. */
5508 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
5509 is a rotate of A by B bits. */
5511 enum tree_code code0, code1;
5512 code0 = TREE_CODE (arg0);
5513 code1 = TREE_CODE (arg1);
5514 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
5515 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
5516 && operand_equal_p (TREE_OPERAND (arg0, 0),
5517 TREE_OPERAND (arg1, 0), 0)
5518 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5520 tree tree01, tree11;
5521 enum tree_code code01, code11;
5523 tree01 = TREE_OPERAND (arg0, 1);
5524 tree11 = TREE_OPERAND (arg1, 1);
5525 STRIP_NOPS (tree01);
5526 STRIP_NOPS (tree11);
5527 code01 = TREE_CODE (tree01);
5528 code11 = TREE_CODE (tree11);
5529 if (code01 == INTEGER_CST
5530 && code11 == INTEGER_CST
5531 && TREE_INT_CST_HIGH (tree01) == 0
5532 && TREE_INT_CST_HIGH (tree11) == 0
5533 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
5534 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
5535 return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
5536 code0 == LSHIFT_EXPR ? tree01 : tree11);
5537 else if (code11 == MINUS_EXPR)
5539 tree tree110, tree111;
5540 tree110 = TREE_OPERAND (tree11, 0);
5541 tree111 = TREE_OPERAND (tree11, 1);
5542 STRIP_NOPS (tree110);
5543 STRIP_NOPS (tree111);
5544 if (TREE_CODE (tree110) == INTEGER_CST
5545 && 0 == compare_tree_int (tree110,
5546 TYPE_PRECISION
5547 (TREE_TYPE (TREE_OPERAND
5548 (arg0, 0))))
5549 && operand_equal_p (tree01, tree111, 0))
5550 return build ((code0 == LSHIFT_EXPR
5551 ? LROTATE_EXPR
5552 : RROTATE_EXPR),
5553 type, TREE_OPERAND (arg0, 0), tree01);
5555 else if (code01 == MINUS_EXPR)
5557 tree tree010, tree011;
5558 tree010 = TREE_OPERAND (tree01, 0);
5559 tree011 = TREE_OPERAND (tree01, 1);
5560 STRIP_NOPS (tree010);
5561 STRIP_NOPS (tree011);
5562 if (TREE_CODE (tree010) == INTEGER_CST
5563 && 0 == compare_tree_int (tree010,
5564 TYPE_PRECISION
5565 (TREE_TYPE (TREE_OPERAND
5566 (arg0, 0))))
5567 && operand_equal_p (tree11, tree011, 0))
5568 return build ((code0 != LSHIFT_EXPR
5569 ? LROTATE_EXPR
5570 : RROTATE_EXPR),
5571 type, TREE_OPERAND (arg0, 0), tree11);
5576 associate:
5577 /* In most languages, can't associate operations on floats through
5578 parentheses. Rather than remember where the parentheses were, we
5579 don't associate floats at all. It shouldn't matter much. However,
5580 associating multiplications is only very slightly inaccurate, so do
5581 that if -funsafe-math-optimizations is specified. */
5583 if (! wins
5584 && (! FLOAT_TYPE_P (type)
5585 || (flag_unsafe_math_optimizations && code == MULT_EXPR)))
5587 tree var0, con0, lit0, minus_lit0;
5588 tree var1, con1, lit1, minus_lit1;
5590 /* Split both trees into variables, constants, and literals. Then
5591 associate each group together, the constants with literals,
5592 then the result with variables. This increases the chances of
5593 literals being recombined later and of generating relocatable
5594 expressions for the sum of a constant and literal. */
5595 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
5596 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
5597 code == MINUS_EXPR);
5599 /* Only do something if we found more than two objects. Otherwise,
5600 nothing has changed and we risk infinite recursion. */
5601 if (2 < ((var0 != 0) + (var1 != 0)
5602 + (con0 != 0) + (con1 != 0)
5603 + (lit0 != 0) + (lit1 != 0)
5604 + (minus_lit0 != 0) + (minus_lit1 != 0)))
5606 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
5607 if (code == MINUS_EXPR)
5608 code = PLUS_EXPR;
5610 var0 = associate_trees (var0, var1, code, type);
5611 con0 = associate_trees (con0, con1, code, type);
5612 lit0 = associate_trees (lit0, lit1, code, type);
5613 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
5615 /* Preserve the MINUS_EXPR if the negative part of the literal is
5616 greater than the positive part. Otherwise, the multiplicative
5617 folding code (i.e extract_muldiv) may be fooled in case
5618 unsigned constants are substracted, like in the following
5619 example: ((X*2 + 4) - 8U)/2. */
5620 if (minus_lit0 && lit0)
5622 if (tree_int_cst_lt (lit0, minus_lit0))
5624 minus_lit0 = associate_trees (minus_lit0, lit0,
5625 MINUS_EXPR, type);
5626 lit0 = 0;
5628 else
5630 lit0 = associate_trees (lit0, minus_lit0,
5631 MINUS_EXPR, type);
5632 minus_lit0 = 0;
5635 if (minus_lit0)
5637 if (con0 == 0)
5638 return convert (type, associate_trees (var0, minus_lit0,
5639 MINUS_EXPR, type));
5640 else
5642 con0 = associate_trees (con0, minus_lit0,
5643 MINUS_EXPR, type);
5644 return convert (type, associate_trees (var0, con0,
5645 PLUS_EXPR, type));
5649 con0 = associate_trees (con0, lit0, code, type);
5650 return convert (type, associate_trees (var0, con0, code, type));
5654 binary:
5655 if (wins)
5656 t1 = const_binop (code, arg0, arg1, 0);
5657 if (t1 != NULL_TREE)
5659 /* The return value should always have
5660 the same type as the original expression. */
5661 if (TREE_TYPE (t1) != TREE_TYPE (t))
5662 t1 = convert (TREE_TYPE (t), t1);
5664 return t1;
5666 return t;
5668 case MINUS_EXPR:
5669 /* A - (-B) -> A + B */
5670 if (TREE_CODE (arg1) == NEGATE_EXPR)
5671 return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5672 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
5673 if (TREE_CODE (arg0) == NEGATE_EXPR
5674 && FLOAT_TYPE_P (type)
5675 && negate_expr_p (arg1)
5676 && (! TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
5677 && (! TREE_SIDE_EFFECTS (arg1) || TREE_CONSTANT (arg0)))
5678 return fold (build (MINUS_EXPR, type, negate_expr (arg1),
5679 TREE_OPERAND (arg0, 0)));
5681 if (! FLOAT_TYPE_P (type))
5683 if (! wins && integer_zerop (arg0))
5684 return negate_expr (convert (type, arg1));
5685 if (integer_zerop (arg1))
5686 return non_lvalue (convert (type, arg0));
5688 /* (A * C) - (B * C) -> (A-B) * C. Since we are most concerned
5689 about the case where C is a constant, just try one of the
5690 four possibilities. */
5692 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
5693 && operand_equal_p (TREE_OPERAND (arg0, 1),
5694 TREE_OPERAND (arg1, 1), 0))
5695 return fold (build (MULT_EXPR, type,
5696 fold (build (MINUS_EXPR, type,
5697 TREE_OPERAND (arg0, 0),
5698 TREE_OPERAND (arg1, 0))),
5699 TREE_OPERAND (arg0, 1)));
5701 /* Fold A - (A & B) into ~B & A. */
5702 if (!TREE_SIDE_EFFECTS (arg0)
5703 && TREE_CODE (arg1) == BIT_AND_EXPR)
5705 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
5706 return fold (build (BIT_AND_EXPR, type,
5707 fold (build1 (BIT_NOT_EXPR, type,
5708 TREE_OPERAND (arg1, 0))),
5709 arg0));
5710 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
5711 return fold (build (BIT_AND_EXPR, type,
5712 fold (build1 (BIT_NOT_EXPR, type,
5713 TREE_OPERAND (arg1, 1))),
5714 arg0));
5718 /* See if ARG1 is zero and X - ARG1 reduces to X. */
5719 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
5720 return non_lvalue (convert (type, arg0));
5722 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
5723 ARG0 is zero and X + ARG0 reduces to X, since that would mean
5724 (-ARG1 + ARG0) reduces to -ARG1. */
5725 else if (!wins && fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
5726 return negate_expr (convert (type, arg1));
5728 /* Fold &x - &x. This can happen from &x.foo - &x.
5729 This is unsafe for certain floats even in non-IEEE formats.
5730 In IEEE, it is unsafe because it does wrong for NaNs.
5731 Also note that operand_equal_p is always false if an operand
5732 is volatile. */
5734 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
5735 && operand_equal_p (arg0, arg1, 0))
5736 return convert (type, integer_zero_node);
5738 goto associate;
5740 case MULT_EXPR:
5741 /* (-A) * (-B) -> A * B */
5742 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
5743 return fold (build (MULT_EXPR, type, TREE_OPERAND (arg0, 0),
5744 TREE_OPERAND (arg1, 0)));
5746 if (! FLOAT_TYPE_P (type))
5748 if (integer_zerop (arg1))
5749 return omit_one_operand (type, arg1, arg0);
5750 if (integer_onep (arg1))
5751 return non_lvalue (convert (type, arg0));
5753 /* (a * (1 << b)) is (a << b) */
5754 if (TREE_CODE (arg1) == LSHIFT_EXPR
5755 && integer_onep (TREE_OPERAND (arg1, 0)))
5756 return fold (build (LSHIFT_EXPR, type, arg0,
5757 TREE_OPERAND (arg1, 1)));
5758 if (TREE_CODE (arg0) == LSHIFT_EXPR
5759 && integer_onep (TREE_OPERAND (arg0, 0)))
5760 return fold (build (LSHIFT_EXPR, type, arg1,
5761 TREE_OPERAND (arg0, 1)));
5763 if (TREE_CODE (arg1) == INTEGER_CST
5764 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5765 code, NULL_TREE)))
5766 return convert (type, tem);
5769 else
5771 /* Maybe fold x * 0 to 0. The expressions aren't the same
5772 when x is NaN, since x * 0 is also NaN. Nor are they the
5773 same in modes with signed zeros, since multiplying a
5774 negative value by 0 gives -0, not +0. */
5775 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
5776 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
5777 && real_zerop (arg1))
5778 return omit_one_operand (type, arg1, arg0);
5779 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
5780 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
5781 && real_onep (arg1))
5782 return non_lvalue (convert (type, arg0));
5784 /* Transform x * -1.0 into -x. */
5785 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
5786 && real_minus_onep (arg1))
5787 return fold (build1 (NEGATE_EXPR, type, arg0));
5789 /* x*2 is x+x */
5790 if (! wins && real_twop (arg1)
5791 && (*lang_hooks.decls.global_bindings_p) () == 0
5792 && ! contains_placeholder_p (arg0))
5794 tree arg = save_expr (arg0);
5795 return build (PLUS_EXPR, type, arg, arg);
5798 if (flag_unsafe_math_optimizations)
5800 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
5801 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
5803 /* Optimize sqrt(x)*sqrt(y) as sqrt(x*y). */
5804 if ((fcode0 == BUILT_IN_SQRT && fcode1 == BUILT_IN_SQRT)
5805 || (fcode0 == BUILT_IN_SQRTF && fcode1 == BUILT_IN_SQRTF)
5806 || (fcode0 == BUILT_IN_SQRTL && fcode1 == BUILT_IN_SQRTL))
5808 tree sqrtfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
5809 tree arg = build (MULT_EXPR, type,
5810 TREE_VALUE (TREE_OPERAND (arg0, 1)),
5811 TREE_VALUE (TREE_OPERAND (arg1, 1)));
5812 tree arglist = build_tree_list (NULL_TREE, arg);
5813 return fold (build_function_call_expr (sqrtfn, arglist));
5816 /* Optimize exp(x)*exp(y) as exp(x+y). */
5817 if ((fcode0 == BUILT_IN_EXP && fcode1 == BUILT_IN_EXP)
5818 || (fcode0 == BUILT_IN_EXPF && fcode1 == BUILT_IN_EXPF)
5819 || (fcode0 == BUILT_IN_EXPL && fcode1 == BUILT_IN_EXPL))
5821 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
5822 tree arg = build (PLUS_EXPR, type,
5823 TREE_VALUE (TREE_OPERAND (arg0, 1)),
5824 TREE_VALUE (TREE_OPERAND (arg1, 1)));
5825 tree arglist = build_tree_list (NULL_TREE, arg);
5826 return fold (build_function_call_expr (expfn, arglist));
5830 goto associate;
5832 case BIT_IOR_EXPR:
5833 bit_ior:
5834 if (integer_all_onesp (arg1))
5835 return omit_one_operand (type, arg1, arg0);
5836 if (integer_zerop (arg1))
5837 return non_lvalue (convert (type, arg0));
5838 t1 = distribute_bit_expr (code, type, arg0, arg1);
5839 if (t1 != NULL_TREE)
5840 return t1;
5842 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
5844 This results in more efficient code for machines without a NAND
5845 instruction. Combine will canonicalize to the first form
5846 which will allow use of NAND instructions provided by the
5847 backend if they exist. */
5848 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5849 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5851 return fold (build1 (BIT_NOT_EXPR, type,
5852 build (BIT_AND_EXPR, type,
5853 TREE_OPERAND (arg0, 0),
5854 TREE_OPERAND (arg1, 0))));
5857 /* See if this can be simplified into a rotate first. If that
5858 is unsuccessful continue in the association code. */
5859 goto bit_rotate;
5861 case BIT_XOR_EXPR:
5862 if (integer_zerop (arg1))
5863 return non_lvalue (convert (type, arg0));
5864 if (integer_all_onesp (arg1))
5865 return fold (build1 (BIT_NOT_EXPR, type, arg0));
5867 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
5868 with a constant, and the two constants have no bits in common,
5869 we should treat this as a BIT_IOR_EXPR since this may produce more
5870 simplifications. */
5871 if (TREE_CODE (arg0) == BIT_AND_EXPR
5872 && TREE_CODE (arg1) == BIT_AND_EXPR
5873 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5874 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5875 && integer_zerop (const_binop (BIT_AND_EXPR,
5876 TREE_OPERAND (arg0, 1),
5877 TREE_OPERAND (arg1, 1), 0)))
5879 code = BIT_IOR_EXPR;
5880 goto bit_ior;
5883 /* See if this can be simplified into a rotate first. If that
5884 is unsuccessful continue in the association code. */
5885 goto bit_rotate;
5887 case BIT_AND_EXPR:
5888 bit_and:
5889 if (integer_all_onesp (arg1))
5890 return non_lvalue (convert (type, arg0));
5891 if (integer_zerop (arg1))
5892 return omit_one_operand (type, arg1, arg0);
5893 t1 = distribute_bit_expr (code, type, arg0, arg1);
5894 if (t1 != NULL_TREE)
5895 return t1;
5896 /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */
5897 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
5898 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5900 unsigned int prec
5901 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
5903 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5904 && (~TREE_INT_CST_LOW (arg1)
5905 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
5906 return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
5909 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
5911 This results in more efficient code for machines without a NOR
5912 instruction. Combine will canonicalize to the first form
5913 which will allow use of NOR instructions provided by the
5914 backend if they exist. */
5915 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5916 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5918 return fold (build1 (BIT_NOT_EXPR, type,
5919 build (BIT_IOR_EXPR, type,
5920 TREE_OPERAND (arg0, 0),
5921 TREE_OPERAND (arg1, 0))));
5924 goto associate;
5926 case BIT_ANDTC_EXPR:
5927 if (integer_all_onesp (arg0))
5928 return non_lvalue (convert (type, arg1));
5929 if (integer_zerop (arg0))
5930 return omit_one_operand (type, arg0, arg1);
5931 if (TREE_CODE (arg1) == INTEGER_CST)
5933 arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
5934 code = BIT_AND_EXPR;
5935 goto bit_and;
5937 goto binary;
5939 case RDIV_EXPR:
5940 /* Don't touch a floating-point divide by zero unless the mode
5941 of the constant can represent infinity. */
5942 if (TREE_CODE (arg1) == REAL_CST
5943 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
5944 && real_zerop (arg1))
5945 return t;
5947 /* (-A) / (-B) -> A / B */
5948 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
5949 return fold (build (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
5950 TREE_OPERAND (arg1, 0)));
5952 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
5953 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
5954 && real_onep (arg1))
5955 return non_lvalue (convert (type, arg0));
5957 /* If ARG1 is a constant, we can convert this to a multiply by the
5958 reciprocal. This does not have the same rounding properties,
5959 so only do this if -funsafe-math-optimizations. We can actually
5960 always safely do it if ARG1 is a power of two, but it's hard to
5961 tell if it is or not in a portable manner. */
5962 if (TREE_CODE (arg1) == REAL_CST)
5964 if (flag_unsafe_math_optimizations
5965 && 0 != (tem = const_binop (code, build_real (type, dconst1),
5966 arg1, 0)))
5967 return fold (build (MULT_EXPR, type, arg0, tem));
5968 /* Find the reciprocal if optimizing and the result is exact. */
5969 else if (optimize)
5971 REAL_VALUE_TYPE r;
5972 r = TREE_REAL_CST (arg1);
5973 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
5975 tem = build_real (type, r);
5976 return fold (build (MULT_EXPR, type, arg0, tem));
5980 /* Convert A/B/C to A/(B*C). */
5981 if (flag_unsafe_math_optimizations
5982 && TREE_CODE (arg0) == RDIV_EXPR)
5984 return fold (build (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
5985 build (MULT_EXPR, type, TREE_OPERAND (arg0, 1),
5986 arg1)));
5988 /* Convert A/(B/C) to (A/B)*C. */
5989 if (flag_unsafe_math_optimizations
5990 && TREE_CODE (arg1) == RDIV_EXPR)
5992 return fold (build (MULT_EXPR, type,
5993 build (RDIV_EXPR, type, arg0,
5994 TREE_OPERAND (arg1, 0)),
5995 TREE_OPERAND (arg1, 1)));
5998 /* Optimize x/exp(y) into x*exp(-y). */
5999 if (flag_unsafe_math_optimizations)
6001 enum built_in_function fcode = builtin_mathfn_code (arg1);
6002 if (fcode == BUILT_IN_EXP
6003 || fcode == BUILT_IN_EXPF
6004 || fcode == BUILT_IN_EXPL)
6006 tree expfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
6007 tree arg = build1 (NEGATE_EXPR, type,
6008 TREE_VALUE (TREE_OPERAND (arg1, 1)));
6009 tree arglist = build_tree_list (NULL_TREE, arg);
6010 arg1 = build_function_call_expr (expfn, arglist);
6011 return fold (build (MULT_EXPR, type, arg0, arg1));
6014 goto binary;
6016 case TRUNC_DIV_EXPR:
6017 case ROUND_DIV_EXPR:
6018 case FLOOR_DIV_EXPR:
6019 case CEIL_DIV_EXPR:
6020 case EXACT_DIV_EXPR:
6021 if (integer_onep (arg1))
6022 return non_lvalue (convert (type, arg0));
6023 if (integer_zerop (arg1))
6024 return t;
6026 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
6027 operation, EXACT_DIV_EXPR.
6029 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
6030 At one time others generated faster code, it's not clear if they do
6031 after the last round to changes to the DIV code in expmed.c. */
6032 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
6033 && multiple_of_p (type, arg0, arg1))
6034 return fold (build (EXACT_DIV_EXPR, type, arg0, arg1));
6036 if (TREE_CODE (arg1) == INTEGER_CST
6037 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
6038 code, NULL_TREE)))
6039 return convert (type, tem);
6041 goto binary;
6043 case CEIL_MOD_EXPR:
6044 case FLOOR_MOD_EXPR:
6045 case ROUND_MOD_EXPR:
6046 case TRUNC_MOD_EXPR:
6047 if (integer_onep (arg1))
6048 return omit_one_operand (type, integer_zero_node, arg0);
6049 if (integer_zerop (arg1))
6050 return t;
6052 if (TREE_CODE (arg1) == INTEGER_CST
6053 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
6054 code, NULL_TREE)))
6055 return convert (type, tem);
6057 goto binary;
6059 case LROTATE_EXPR:
6060 case RROTATE_EXPR:
6061 if (integer_all_onesp (arg0))
6062 return omit_one_operand (type, arg0, arg1);
6063 goto shift;
6065 case RSHIFT_EXPR:
6066 /* Optimize -1 >> x for arithmetic right shifts. */
6067 if (integer_all_onesp (arg0) && ! TREE_UNSIGNED (type))
6068 return omit_one_operand (type, arg0, arg1);
6069 /* ... fall through ... */
6071 case LSHIFT_EXPR:
6072 shift:
6073 if (integer_zerop (arg1))
6074 return non_lvalue (convert (type, arg0));
6075 if (integer_zerop (arg0))
6076 return omit_one_operand (type, arg0, arg1);
6078 /* Since negative shift count is not well-defined,
6079 don't try to compute it in the compiler. */
6080 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
6081 return t;
6082 /* Rewrite an LROTATE_EXPR by a constant into an
6083 RROTATE_EXPR by a new constant. */
6084 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
6086 TREE_SET_CODE (t, RROTATE_EXPR);
6087 code = RROTATE_EXPR;
6088 TREE_OPERAND (t, 1) = arg1
6089 = const_binop
6090 (MINUS_EXPR,
6091 convert (TREE_TYPE (arg1),
6092 build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0)),
6093 arg1, 0);
6094 if (tree_int_cst_sgn (arg1) < 0)
6095 return t;
6098 /* If we have a rotate of a bit operation with the rotate count and
6099 the second operand of the bit operation both constant,
6100 permute the two operations. */
6101 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6102 && (TREE_CODE (arg0) == BIT_AND_EXPR
6103 || TREE_CODE (arg0) == BIT_ANDTC_EXPR
6104 || TREE_CODE (arg0) == BIT_IOR_EXPR
6105 || TREE_CODE (arg0) == BIT_XOR_EXPR)
6106 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
6107 return fold (build (TREE_CODE (arg0), type,
6108 fold (build (code, type,
6109 TREE_OPERAND (arg0, 0), arg1)),
6110 fold (build (code, type,
6111 TREE_OPERAND (arg0, 1), arg1))));
6113 /* Two consecutive rotates adding up to the width of the mode can
6114 be ignored. */
6115 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6116 && TREE_CODE (arg0) == RROTATE_EXPR
6117 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6118 && TREE_INT_CST_HIGH (arg1) == 0
6119 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
6120 && ((TREE_INT_CST_LOW (arg1)
6121 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
6122 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
6123 return TREE_OPERAND (arg0, 0);
6125 goto binary;
6127 case MIN_EXPR:
6128 if (operand_equal_p (arg0, arg1, 0))
6129 return omit_one_operand (type, arg0, arg1);
6130 if (INTEGRAL_TYPE_P (type)
6131 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
6132 return omit_one_operand (type, arg1, arg0);
6133 goto associate;
6135 case MAX_EXPR:
6136 if (operand_equal_p (arg0, arg1, 0))
6137 return omit_one_operand (type, arg0, arg1);
6138 if (INTEGRAL_TYPE_P (type)
6139 && TYPE_MAX_VALUE (type)
6140 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
6141 return omit_one_operand (type, arg1, arg0);
6142 goto associate;
6144 case TRUTH_NOT_EXPR:
6145 /* Note that the operand of this must be an int
6146 and its values must be 0 or 1.
6147 ("true" is a fixed value perhaps depending on the language,
6148 but we don't handle values other than 1 correctly yet.) */
6149 tem = invert_truthvalue (arg0);
6150 /* Avoid infinite recursion. */
6151 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
6152 return t;
6153 return convert (type, tem);
6155 case TRUTH_ANDIF_EXPR:
6156 /* Note that the operands of this must be ints
6157 and their values must be 0 or 1.
6158 ("true" is a fixed value perhaps depending on the language.) */
6159 /* If first arg is constant zero, return it. */
6160 if (integer_zerop (arg0))
6161 return convert (type, arg0);
6162 case TRUTH_AND_EXPR:
6163 /* If either arg is constant true, drop it. */
6164 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6165 return non_lvalue (convert (type, arg1));
6166 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
6167 /* Preserve sequence points. */
6168 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
6169 return non_lvalue (convert (type, arg0));
6170 /* If second arg is constant zero, result is zero, but first arg
6171 must be evaluated. */
6172 if (integer_zerop (arg1))
6173 return omit_one_operand (type, arg1, arg0);
6174 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
6175 case will be handled here. */
6176 if (integer_zerop (arg0))
6177 return omit_one_operand (type, arg0, arg1);
6179 truth_andor:
6180 /* We only do these simplifications if we are optimizing. */
6181 if (!optimize)
6182 return t;
6184 /* Check for things like (A || B) && (A || C). We can convert this
6185 to A || (B && C). Note that either operator can be any of the four
6186 truth and/or operations and the transformation will still be
6187 valid. Also note that we only care about order for the
6188 ANDIF and ORIF operators. If B contains side effects, this
6189 might change the truth-value of A. */
6190 if (TREE_CODE (arg0) == TREE_CODE (arg1)
6191 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
6192 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
6193 || TREE_CODE (arg0) == TRUTH_AND_EXPR
6194 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
6195 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
6197 tree a00 = TREE_OPERAND (arg0, 0);
6198 tree a01 = TREE_OPERAND (arg0, 1);
6199 tree a10 = TREE_OPERAND (arg1, 0);
6200 tree a11 = TREE_OPERAND (arg1, 1);
6201 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
6202 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
6203 && (code == TRUTH_AND_EXPR
6204 || code == TRUTH_OR_EXPR));
6206 if (operand_equal_p (a00, a10, 0))
6207 return fold (build (TREE_CODE (arg0), type, a00,
6208 fold (build (code, type, a01, a11))));
6209 else if (commutative && operand_equal_p (a00, a11, 0))
6210 return fold (build (TREE_CODE (arg0), type, a00,
6211 fold (build (code, type, a01, a10))));
6212 else if (commutative && operand_equal_p (a01, a10, 0))
6213 return fold (build (TREE_CODE (arg0), type, a01,
6214 fold (build (code, type, a00, a11))));
6216 /* This case if tricky because we must either have commutative
6217 operators or else A10 must not have side-effects. */
6219 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
6220 && operand_equal_p (a01, a11, 0))
6221 return fold (build (TREE_CODE (arg0), type,
6222 fold (build (code, type, a00, a10)),
6223 a01));
6226 /* See if we can build a range comparison. */
6227 if (0 != (tem = fold_range_test (t)))
6228 return tem;
6230 /* Check for the possibility of merging component references. If our
6231 lhs is another similar operation, try to merge its rhs with our
6232 rhs. Then try to merge our lhs and rhs. */
6233 if (TREE_CODE (arg0) == code
6234 && 0 != (tem = fold_truthop (code, type,
6235 TREE_OPERAND (arg0, 1), arg1)))
6236 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6238 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
6239 return tem;
6241 return t;
6243 case TRUTH_ORIF_EXPR:
6244 /* Note that the operands of this must be ints
6245 and their values must be 0 or true.
6246 ("true" is a fixed value perhaps depending on the language.) */
6247 /* If first arg is constant true, return it. */
6248 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6249 return convert (type, arg0);
6250 case TRUTH_OR_EXPR:
6251 /* If either arg is constant zero, drop it. */
6252 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
6253 return non_lvalue (convert (type, arg1));
6254 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
6255 /* Preserve sequence points. */
6256 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
6257 return non_lvalue (convert (type, arg0));
6258 /* If second arg is constant true, result is true, but we must
6259 evaluate first arg. */
6260 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
6261 return omit_one_operand (type, arg1, arg0);
6262 /* Likewise for first arg, but note this only occurs here for
6263 TRUTH_OR_EXPR. */
6264 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6265 return omit_one_operand (type, arg0, arg1);
6266 goto truth_andor;
6268 case TRUTH_XOR_EXPR:
6269 /* If either arg is constant zero, drop it. */
6270 if (integer_zerop (arg0))
6271 return non_lvalue (convert (type, arg1));
6272 if (integer_zerop (arg1))
6273 return non_lvalue (convert (type, arg0));
6274 /* If either arg is constant true, this is a logical inversion. */
6275 if (integer_onep (arg0))
6276 return non_lvalue (convert (type, invert_truthvalue (arg1)));
6277 if (integer_onep (arg1))
6278 return non_lvalue (convert (type, invert_truthvalue (arg0)));
6279 return t;
6281 case EQ_EXPR:
6282 case NE_EXPR:
6283 case LT_EXPR:
6284 case GT_EXPR:
6285 case LE_EXPR:
6286 case GE_EXPR:
6287 /* If one arg is a real or integer constant, put it last. */
6288 if ((TREE_CODE (arg0) == INTEGER_CST
6289 && TREE_CODE (arg1) != INTEGER_CST)
6290 || (TREE_CODE (arg0) == REAL_CST
6291 && TREE_CODE (arg0) != REAL_CST))
6293 TREE_OPERAND (t, 0) = arg1;
6294 TREE_OPERAND (t, 1) = arg0;
6295 arg0 = TREE_OPERAND (t, 0);
6296 arg1 = TREE_OPERAND (t, 1);
6297 code = swap_tree_comparison (code);
6298 TREE_SET_CODE (t, code);
6301 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
6303 tree targ0 = strip_float_extensions (arg0);
6304 tree targ1 = strip_float_extensions (arg1);
6305 tree newtype = TREE_TYPE (targ0);
6307 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
6308 newtype = TREE_TYPE (targ1);
6310 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
6311 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
6312 return fold (build (code, type, convert (newtype, targ0),
6313 convert (newtype, targ1)));
6315 /* (-a) CMP (-b) -> b CMP a */
6316 if (TREE_CODE (arg0) == NEGATE_EXPR
6317 && TREE_CODE (arg1) == NEGATE_EXPR)
6318 return fold (build (code, type, TREE_OPERAND (arg1, 0),
6319 TREE_OPERAND (arg0, 0)));
6320 /* (-a) CMP CST -> a swap(CMP) (-CST) */
6321 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == REAL_CST)
6322 return
6323 fold (build
6324 (swap_tree_comparison (code), type,
6325 TREE_OPERAND (arg0, 0),
6326 build_real (TREE_TYPE (arg1),
6327 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1)))));
6328 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6329 /* a CMP (-0) -> a CMP 0 */
6330 if (TREE_CODE (arg1) == REAL_CST
6331 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1)))
6332 return fold (build (code, type, arg0,
6333 build_real (TREE_TYPE (arg1), dconst0)));
6335 /* If this is a comparison of a real constant with a PLUS_EXPR
6336 or a MINUS_EXPR of a real constant, we can convert it into a
6337 comparison with a revised real constant as long as no overflow
6338 occurs when unsafe_math_optimizations are enabled. */
6339 if (flag_unsafe_math_optimizations
6340 && TREE_CODE (arg1) == REAL_CST
6341 && (TREE_CODE (arg0) == PLUS_EXPR
6342 || TREE_CODE (arg0) == MINUS_EXPR)
6343 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
6344 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
6345 ? MINUS_EXPR : PLUS_EXPR,
6346 arg1, TREE_OPERAND (arg0, 1), 0))
6347 && ! TREE_CONSTANT_OVERFLOW (tem))
6348 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6350 /* Fold comparisons against built-in math functions. */
6351 if (TREE_CODE (arg1) == REAL_CST
6352 && flag_unsafe_math_optimizations
6353 && ! flag_errno_math)
6355 enum built_in_function fcode = builtin_mathfn_code (arg0);
6357 if (fcode != END_BUILTINS)
6359 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
6360 if (tem != NULL_TREE)
6361 return tem;
6366 /* Convert foo++ == CONST into ++foo == CONST + INCR.
6367 First, see if one arg is constant; find the constant arg
6368 and the other one. */
6370 tree constop = 0, varop = NULL_TREE;
6371 int constopnum = -1;
6373 if (TREE_CONSTANT (arg1))
6374 constopnum = 1, constop = arg1, varop = arg0;
6375 if (TREE_CONSTANT (arg0))
6376 constopnum = 0, constop = arg0, varop = arg1;
6378 if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
6380 /* This optimization is invalid for ordered comparisons
6381 if CONST+INCR overflows or if foo+incr might overflow.
6382 This optimization is invalid for floating point due to rounding.
6383 For pointer types we assume overflow doesn't happen. */
6384 if (POINTER_TYPE_P (TREE_TYPE (varop))
6385 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
6386 && (code == EQ_EXPR || code == NE_EXPR)))
6388 tree newconst
6389 = fold (build (PLUS_EXPR, TREE_TYPE (varop),
6390 constop, TREE_OPERAND (varop, 1)));
6392 /* Do not overwrite the current varop to be a preincrement,
6393 create a new node so that we won't confuse our caller who
6394 might create trees and throw them away, reusing the
6395 arguments that they passed to build. This shows up in
6396 the THEN or ELSE parts of ?: being postincrements. */
6397 varop = build (PREINCREMENT_EXPR, TREE_TYPE (varop),
6398 TREE_OPERAND (varop, 0),
6399 TREE_OPERAND (varop, 1));
6401 /* If VAROP is a reference to a bitfield, we must mask
6402 the constant by the width of the field. */
6403 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6404 && DECL_BIT_FIELD(TREE_OPERAND
6405 (TREE_OPERAND (varop, 0), 1)))
6407 int size
6408 = TREE_INT_CST_LOW (DECL_SIZE
6409 (TREE_OPERAND
6410 (TREE_OPERAND (varop, 0), 1)));
6411 tree mask, unsigned_type;
6412 unsigned int precision;
6413 tree folded_compare;
6415 /* First check whether the comparison would come out
6416 always the same. If we don't do that we would
6417 change the meaning with the masking. */
6418 if (constopnum == 0)
6419 folded_compare = fold (build (code, type, constop,
6420 TREE_OPERAND (varop, 0)));
6421 else
6422 folded_compare = fold (build (code, type,
6423 TREE_OPERAND (varop, 0),
6424 constop));
6425 if (integer_zerop (folded_compare)
6426 || integer_onep (folded_compare))
6427 return omit_one_operand (type, folded_compare, varop);
6429 unsigned_type = (*lang_hooks.types.type_for_size)(size, 1);
6430 precision = TYPE_PRECISION (unsigned_type);
6431 mask = build_int_2 (~0, ~0);
6432 TREE_TYPE (mask) = unsigned_type;
6433 force_fit_type (mask, 0);
6434 mask = const_binop (RSHIFT_EXPR, mask,
6435 size_int (precision - size), 0);
6436 newconst = fold (build (BIT_AND_EXPR,
6437 TREE_TYPE (varop), newconst,
6438 convert (TREE_TYPE (varop),
6439 mask)));
6442 t = build (code, type,
6443 (constopnum == 0) ? newconst : varop,
6444 (constopnum == 1) ? newconst : varop);
6445 return t;
6448 else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
6450 if (POINTER_TYPE_P (TREE_TYPE (varop))
6451 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
6452 && (code == EQ_EXPR || code == NE_EXPR)))
6454 tree newconst
6455 = fold (build (MINUS_EXPR, TREE_TYPE (varop),
6456 constop, TREE_OPERAND (varop, 1)));
6458 /* Do not overwrite the current varop to be a predecrement,
6459 create a new node so that we won't confuse our caller who
6460 might create trees and throw them away, reusing the
6461 arguments that they passed to build. This shows up in
6462 the THEN or ELSE parts of ?: being postdecrements. */
6463 varop = build (PREDECREMENT_EXPR, TREE_TYPE (varop),
6464 TREE_OPERAND (varop, 0),
6465 TREE_OPERAND (varop, 1));
6467 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6468 && DECL_BIT_FIELD(TREE_OPERAND
6469 (TREE_OPERAND (varop, 0), 1)))
6471 int size
6472 = TREE_INT_CST_LOW (DECL_SIZE
6473 (TREE_OPERAND
6474 (TREE_OPERAND (varop, 0), 1)));
6475 tree mask, unsigned_type;
6476 unsigned int precision;
6477 tree folded_compare;
6479 if (constopnum == 0)
6480 folded_compare = fold (build (code, type, constop,
6481 TREE_OPERAND (varop, 0)));
6482 else
6483 folded_compare = fold (build (code, type,
6484 TREE_OPERAND (varop, 0),
6485 constop));
6486 if (integer_zerop (folded_compare)
6487 || integer_onep (folded_compare))
6488 return omit_one_operand (type, folded_compare, varop);
6490 unsigned_type = (*lang_hooks.types.type_for_size)(size, 1);
6491 precision = TYPE_PRECISION (unsigned_type);
6492 mask = build_int_2 (~0, ~0);
6493 TREE_TYPE (mask) = TREE_TYPE (varop);
6494 force_fit_type (mask, 0);
6495 mask = const_binop (RSHIFT_EXPR, mask,
6496 size_int (precision - size), 0);
6497 newconst = fold (build (BIT_AND_EXPR,
6498 TREE_TYPE (varop), newconst,
6499 convert (TREE_TYPE (varop),
6500 mask)));
6503 t = build (code, type,
6504 (constopnum == 0) ? newconst : varop,
6505 (constopnum == 1) ? newconst : varop);
6506 return t;
6511 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
6512 This transformation affects the cases which are handled in later
6513 optimizations involving comparisons with non-negative constants. */
6514 if (TREE_CODE (arg1) == INTEGER_CST
6515 && TREE_CODE (arg0) != INTEGER_CST
6516 && tree_int_cst_sgn (arg1) > 0)
6518 switch (code)
6520 case GE_EXPR:
6521 code = GT_EXPR;
6522 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6523 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6524 break;
6526 case LT_EXPR:
6527 code = LE_EXPR;
6528 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6529 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6530 break;
6532 default:
6533 break;
6537 /* Comparisons with the highest or lowest possible integer of
6538 the specified size will have known values. */
6540 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
6542 if (TREE_CODE (arg1) == INTEGER_CST
6543 && ! TREE_CONSTANT_OVERFLOW (arg1)
6544 && width <= HOST_BITS_PER_WIDE_INT
6545 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6546 || POINTER_TYPE_P (TREE_TYPE (arg1))))
6548 unsigned HOST_WIDE_INT signed_max;
6549 unsigned HOST_WIDE_INT max, min;
6551 signed_max = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1;
6553 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
6555 max = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
6556 min = 0;
6558 else
6560 max = signed_max;
6561 min = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
6564 if (TREE_INT_CST_HIGH (arg1) == 0
6565 && TREE_INT_CST_LOW (arg1) == max)
6566 switch (code)
6568 case GT_EXPR:
6569 return omit_one_operand (type,
6570 convert (type, integer_zero_node),
6571 arg0);
6572 case GE_EXPR:
6573 code = EQ_EXPR;
6574 TREE_SET_CODE (t, EQ_EXPR);
6575 break;
6576 case LE_EXPR:
6577 return omit_one_operand (type,
6578 convert (type, integer_one_node),
6579 arg0);
6580 case LT_EXPR:
6581 code = NE_EXPR;
6582 TREE_SET_CODE (t, NE_EXPR);
6583 break;
6585 /* The GE_EXPR and LT_EXPR cases above are not normally
6586 reached because of previous transformations. */
6588 default:
6589 break;
6591 else if (TREE_INT_CST_HIGH (arg1) == 0
6592 && TREE_INT_CST_LOW (arg1) == max - 1)
6593 switch (code)
6595 case GT_EXPR:
6596 code = EQ_EXPR;
6597 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
6598 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6599 break;
6600 case LE_EXPR:
6601 code = NE_EXPR;
6602 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
6603 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6604 break;
6605 default:
6606 break;
6608 else if (TREE_INT_CST_HIGH (arg1) == (min ? -1 : 0)
6609 && TREE_INT_CST_LOW (arg1) == min)
6610 switch (code)
6612 case LT_EXPR:
6613 return omit_one_operand (type,
6614 convert (type, integer_zero_node),
6615 arg0);
6616 case LE_EXPR:
6617 code = EQ_EXPR;
6618 TREE_SET_CODE (t, EQ_EXPR);
6619 break;
6621 case GE_EXPR:
6622 return omit_one_operand (type,
6623 convert (type, integer_one_node),
6624 arg0);
6625 case GT_EXPR:
6626 code = NE_EXPR;
6627 TREE_SET_CODE (t, NE_EXPR);
6628 break;
6630 default:
6631 break;
6633 else if (TREE_INT_CST_HIGH (arg1) == (min ? -1 : 0)
6634 && TREE_INT_CST_LOW (arg1) == min + 1)
6635 switch (code)
6637 case GE_EXPR:
6638 code = NE_EXPR;
6639 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6640 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6641 break;
6642 case LT_EXPR:
6643 code = EQ_EXPR;
6644 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6645 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6646 break;
6647 default:
6648 break;
6651 else if (TREE_INT_CST_HIGH (arg1) == 0
6652 && TREE_INT_CST_LOW (arg1) == signed_max
6653 && TREE_UNSIGNED (TREE_TYPE (arg1))
6654 /* signed_type does not work on pointer types. */
6655 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
6657 /* The following case also applies to X < signed_max+1
6658 and X >= signed_max+1 because previous transformations. */
6659 if (code == LE_EXPR || code == GT_EXPR)
6661 tree st0, st1;
6662 st0 = (*lang_hooks.types.signed_type) (TREE_TYPE (arg0));
6663 st1 = (*lang_hooks.types.signed_type) (TREE_TYPE (arg1));
6664 return fold
6665 (build (code == LE_EXPR ? GE_EXPR: LT_EXPR,
6666 type, convert (st0, arg0),
6667 convert (st1, integer_zero_node)));
6673 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
6674 a MINUS_EXPR of a constant, we can convert it into a comparison with
6675 a revised constant as long as no overflow occurs. */
6676 if ((code == EQ_EXPR || code == NE_EXPR)
6677 && TREE_CODE (arg1) == INTEGER_CST
6678 && (TREE_CODE (arg0) == PLUS_EXPR
6679 || TREE_CODE (arg0) == MINUS_EXPR)
6680 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6681 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
6682 ? MINUS_EXPR : PLUS_EXPR,
6683 arg1, TREE_OPERAND (arg0, 1), 0))
6684 && ! TREE_CONSTANT_OVERFLOW (tem))
6685 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6687 /* Similarly for a NEGATE_EXPR. */
6688 else if ((code == EQ_EXPR || code == NE_EXPR)
6689 && TREE_CODE (arg0) == NEGATE_EXPR
6690 && TREE_CODE (arg1) == INTEGER_CST
6691 && 0 != (tem = negate_expr (arg1))
6692 && TREE_CODE (tem) == INTEGER_CST
6693 && ! TREE_CONSTANT_OVERFLOW (tem))
6694 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6696 /* If we have X - Y == 0, we can convert that to X == Y and similarly
6697 for !=. Don't do this for ordered comparisons due to overflow. */
6698 else if ((code == NE_EXPR || code == EQ_EXPR)
6699 && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
6700 return fold (build (code, type,
6701 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
6703 /* If we are widening one operand of an integer comparison,
6704 see if the other operand is similarly being widened. Perhaps we
6705 can do the comparison in the narrower type. */
6706 else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
6707 && TREE_CODE (arg0) == NOP_EXPR
6708 && (tem = get_unwidened (arg0, NULL_TREE)) != arg0
6709 && (t1 = get_unwidened (arg1, TREE_TYPE (tem))) != 0
6710 && (TREE_TYPE (t1) == TREE_TYPE (tem)
6711 || (TREE_CODE (t1) == INTEGER_CST
6712 && int_fits_type_p (t1, TREE_TYPE (tem)))))
6713 return fold (build (code, type, tem, convert (TREE_TYPE (tem), t1)));
6715 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
6716 constant, we can simplify it. */
6717 else if (TREE_CODE (arg1) == INTEGER_CST
6718 && (TREE_CODE (arg0) == MIN_EXPR
6719 || TREE_CODE (arg0) == MAX_EXPR)
6720 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
6721 return optimize_minmax_comparison (t);
6723 /* If we are comparing an ABS_EXPR with a constant, we can
6724 convert all the cases into explicit comparisons, but they may
6725 well not be faster than doing the ABS and one comparison.
6726 But ABS (X) <= C is a range comparison, which becomes a subtraction
6727 and a comparison, and is probably faster. */
6728 else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6729 && TREE_CODE (arg0) == ABS_EXPR
6730 && ! TREE_SIDE_EFFECTS (arg0)
6731 && (0 != (tem = negate_expr (arg1)))
6732 && TREE_CODE (tem) == INTEGER_CST
6733 && ! TREE_CONSTANT_OVERFLOW (tem))
6734 return fold (build (TRUTH_ANDIF_EXPR, type,
6735 build (GE_EXPR, type, TREE_OPERAND (arg0, 0), tem),
6736 build (LE_EXPR, type,
6737 TREE_OPERAND (arg0, 0), arg1)));
6739 /* If this is an EQ or NE comparison with zero and ARG0 is
6740 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
6741 two operations, but the latter can be done in one less insn
6742 on machines that have only two-operand insns or on which a
6743 constant cannot be the first operand. */
6744 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
6745 && TREE_CODE (arg0) == BIT_AND_EXPR)
6747 if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
6748 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
6749 return
6750 fold (build (code, type,
6751 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6752 build (RSHIFT_EXPR,
6753 TREE_TYPE (TREE_OPERAND (arg0, 0)),
6754 TREE_OPERAND (arg0, 1),
6755 TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
6756 convert (TREE_TYPE (arg0),
6757 integer_one_node)),
6758 arg1));
6759 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
6760 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
6761 return
6762 fold (build (code, type,
6763 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6764 build (RSHIFT_EXPR,
6765 TREE_TYPE (TREE_OPERAND (arg0, 1)),
6766 TREE_OPERAND (arg0, 0),
6767 TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
6768 convert (TREE_TYPE (arg0),
6769 integer_one_node)),
6770 arg1));
6773 /* If this is an NE or EQ comparison of zero against the result of a
6774 signed MOD operation whose second operand is a power of 2, make
6775 the MOD operation unsigned since it is simpler and equivalent. */
6776 if ((code == NE_EXPR || code == EQ_EXPR)
6777 && integer_zerop (arg1)
6778 && ! TREE_UNSIGNED (TREE_TYPE (arg0))
6779 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
6780 || TREE_CODE (arg0) == CEIL_MOD_EXPR
6781 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
6782 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
6783 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6785 tree newtype = (*lang_hooks.types.unsigned_type) (TREE_TYPE (arg0));
6786 tree newmod = build (TREE_CODE (arg0), newtype,
6787 convert (newtype, TREE_OPERAND (arg0, 0)),
6788 convert (newtype, TREE_OPERAND (arg0, 1)));
6790 return build (code, type, newmod, convert (newtype, arg1));
6793 /* If this is an NE comparison of zero with an AND of one, remove the
6794 comparison since the AND will give the correct value. */
6795 if (code == NE_EXPR && integer_zerop (arg1)
6796 && TREE_CODE (arg0) == BIT_AND_EXPR
6797 && integer_onep (TREE_OPERAND (arg0, 1)))
6798 return convert (type, arg0);
6800 /* If we have (A & C) == C where C is a power of 2, convert this into
6801 (A & C) != 0. Similarly for NE_EXPR. */
6802 if ((code == EQ_EXPR || code == NE_EXPR)
6803 && TREE_CODE (arg0) == BIT_AND_EXPR
6804 && integer_pow2p (TREE_OPERAND (arg0, 1))
6805 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
6806 return fold (build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
6807 arg0, integer_zero_node));
6809 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6810 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6811 if ((code == EQ_EXPR || code == NE_EXPR)
6812 && TREE_CODE (arg0) == BIT_AND_EXPR
6813 && integer_zerop (arg1))
6815 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0),
6816 TREE_OPERAND (arg0, 1));
6817 if (arg00 != NULL_TREE)
6819 tree stype = (*lang_hooks.types.signed_type) (TREE_TYPE (arg00));
6820 return fold (build (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
6821 convert (stype, arg00),
6822 convert (stype, integer_zero_node)));
6826 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
6827 and similarly for >= into !=. */
6828 if ((code == LT_EXPR || code == GE_EXPR)
6829 && TREE_UNSIGNED (TREE_TYPE (arg0))
6830 && TREE_CODE (arg1) == LSHIFT_EXPR
6831 && integer_onep (TREE_OPERAND (arg1, 0)))
6832 return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
6833 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6834 TREE_OPERAND (arg1, 1)),
6835 convert (TREE_TYPE (arg0), integer_zero_node));
6837 else if ((code == LT_EXPR || code == GE_EXPR)
6838 && TREE_UNSIGNED (TREE_TYPE (arg0))
6839 && (TREE_CODE (arg1) == NOP_EXPR
6840 || TREE_CODE (arg1) == CONVERT_EXPR)
6841 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
6842 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
6843 return
6844 build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
6845 convert (TREE_TYPE (arg0),
6846 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6847 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))),
6848 convert (TREE_TYPE (arg0), integer_zero_node));
6850 /* Simplify comparison of something with itself. (For IEEE
6851 floating-point, we can only do some of these simplifications.) */
6852 if (operand_equal_p (arg0, arg1, 0))
6854 switch (code)
6856 case EQ_EXPR:
6857 case GE_EXPR:
6858 case LE_EXPR:
6859 if (! FLOAT_TYPE_P (TREE_TYPE (arg0)))
6860 return constant_boolean_node (1, type);
6861 code = EQ_EXPR;
6862 TREE_SET_CODE (t, code);
6863 break;
6865 case NE_EXPR:
6866 /* For NE, we can only do this simplification if integer. */
6867 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
6868 break;
6869 /* ... fall through ... */
6870 case GT_EXPR:
6871 case LT_EXPR:
6872 return constant_boolean_node (0, type);
6873 default:
6874 abort ();
6878 /* If we are comparing an expression that just has comparisons
6879 of two integer values, arithmetic expressions of those comparisons,
6880 and constants, we can simplify it. There are only three cases
6881 to check: the two values can either be equal, the first can be
6882 greater, or the second can be greater. Fold the expression for
6883 those three values. Since each value must be 0 or 1, we have
6884 eight possibilities, each of which corresponds to the constant 0
6885 or 1 or one of the six possible comparisons.
6887 This handles common cases like (a > b) == 0 but also handles
6888 expressions like ((x > y) - (y > x)) > 0, which supposedly
6889 occur in macroized code. */
6891 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
6893 tree cval1 = 0, cval2 = 0;
6894 int save_p = 0;
6896 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
6897 /* Don't handle degenerate cases here; they should already
6898 have been handled anyway. */
6899 && cval1 != 0 && cval2 != 0
6900 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
6901 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
6902 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
6903 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
6904 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
6905 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
6906 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
6908 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
6909 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
6911 /* We can't just pass T to eval_subst in case cval1 or cval2
6912 was the same as ARG1. */
6914 tree high_result
6915 = fold (build (code, type,
6916 eval_subst (arg0, cval1, maxval, cval2, minval),
6917 arg1));
6918 tree equal_result
6919 = fold (build (code, type,
6920 eval_subst (arg0, cval1, maxval, cval2, maxval),
6921 arg1));
6922 tree low_result
6923 = fold (build (code, type,
6924 eval_subst (arg0, cval1, minval, cval2, maxval),
6925 arg1));
6927 /* All three of these results should be 0 or 1. Confirm they
6928 are. Then use those values to select the proper code
6929 to use. */
6931 if ((integer_zerop (high_result)
6932 || integer_onep (high_result))
6933 && (integer_zerop (equal_result)
6934 || integer_onep (equal_result))
6935 && (integer_zerop (low_result)
6936 || integer_onep (low_result)))
6938 /* Make a 3-bit mask with the high-order bit being the
6939 value for `>', the next for '=', and the low for '<'. */
6940 switch ((integer_onep (high_result) * 4)
6941 + (integer_onep (equal_result) * 2)
6942 + integer_onep (low_result))
6944 case 0:
6945 /* Always false. */
6946 return omit_one_operand (type, integer_zero_node, arg0);
6947 case 1:
6948 code = LT_EXPR;
6949 break;
6950 case 2:
6951 code = EQ_EXPR;
6952 break;
6953 case 3:
6954 code = LE_EXPR;
6955 break;
6956 case 4:
6957 code = GT_EXPR;
6958 break;
6959 case 5:
6960 code = NE_EXPR;
6961 break;
6962 case 6:
6963 code = GE_EXPR;
6964 break;
6965 case 7:
6966 /* Always true. */
6967 return omit_one_operand (type, integer_one_node, arg0);
6970 t = build (code, type, cval1, cval2);
6971 if (save_p)
6972 return save_expr (t);
6973 else
6974 return fold (t);
6979 /* If this is a comparison of a field, we may be able to simplify it. */
6980 if (((TREE_CODE (arg0) == COMPONENT_REF
6981 && (*lang_hooks.can_use_bit_fields_p) ())
6982 || TREE_CODE (arg0) == BIT_FIELD_REF)
6983 && (code == EQ_EXPR || code == NE_EXPR)
6984 /* Handle the constant case even without -O
6985 to make sure the warnings are given. */
6986 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
6988 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
6989 return t1 ? t1 : t;
6992 /* If this is a comparison of complex values and either or both sides
6993 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
6994 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
6995 This may prevent needless evaluations. */
6996 if ((code == EQ_EXPR || code == NE_EXPR)
6997 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
6998 && (TREE_CODE (arg0) == COMPLEX_EXPR
6999 || TREE_CODE (arg1) == COMPLEX_EXPR
7000 || TREE_CODE (arg0) == COMPLEX_CST
7001 || TREE_CODE (arg1) == COMPLEX_CST))
7003 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
7004 tree real0, imag0, real1, imag1;
7006 arg0 = save_expr (arg0);
7007 arg1 = save_expr (arg1);
7008 real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
7009 imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
7010 real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
7011 imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
7013 return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
7014 : TRUTH_ORIF_EXPR),
7015 type,
7016 fold (build (code, type, real0, real1)),
7017 fold (build (code, type, imag0, imag1))));
7020 /* Optimize comparisons of strlen vs zero to a compare of the
7021 first character of the string vs zero. To wit,
7022 strlen(ptr) == 0 => *ptr == 0
7023 strlen(ptr) != 0 => *ptr != 0
7024 Other cases should reduce to one of these two (or a constant)
7025 due to the return value of strlen being unsigned. */
7026 if ((code == EQ_EXPR || code == NE_EXPR)
7027 && integer_zerop (arg1)
7028 && TREE_CODE (arg0) == CALL_EXPR
7029 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR)
7031 tree fndecl = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7032 tree arglist;
7034 if (TREE_CODE (fndecl) == FUNCTION_DECL
7035 && DECL_BUILT_IN (fndecl)
7036 && DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD
7037 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
7038 && (arglist = TREE_OPERAND (arg0, 1))
7039 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
7040 && ! TREE_CHAIN (arglist))
7041 return fold (build (code, type,
7042 build1 (INDIRECT_REF, char_type_node,
7043 TREE_VALUE(arglist)),
7044 integer_zero_node));
7047 /* From here on, the only cases we handle are when the result is
7048 known to be a constant.
7050 To compute GT, swap the arguments and do LT.
7051 To compute GE, do LT and invert the result.
7052 To compute LE, swap the arguments, do LT and invert the result.
7053 To compute NE, do EQ and invert the result.
7055 Therefore, the code below must handle only EQ and LT. */
7057 if (code == LE_EXPR || code == GT_EXPR)
7059 tem = arg0, arg0 = arg1, arg1 = tem;
7060 code = swap_tree_comparison (code);
7063 /* Note that it is safe to invert for real values here because we
7064 will check below in the one case that it matters. */
7066 t1 = NULL_TREE;
7067 invert = 0;
7068 if (code == NE_EXPR || code == GE_EXPR)
7070 invert = 1;
7071 code = invert_tree_comparison (code);
7074 /* Compute a result for LT or EQ if args permit;
7075 otherwise return T. */
7076 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
7078 if (code == EQ_EXPR)
7079 t1 = build_int_2 (tree_int_cst_equal (arg0, arg1), 0);
7080 else
7081 t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
7082 ? INT_CST_LT_UNSIGNED (arg0, arg1)
7083 : INT_CST_LT (arg0, arg1)),
7087 #if 0 /* This is no longer useful, but breaks some real code. */
7088 /* Assume a nonexplicit constant cannot equal an explicit one,
7089 since such code would be undefined anyway.
7090 Exception: on sysvr4, using #pragma weak,
7091 a label can come out as 0. */
7092 else if (TREE_CODE (arg1) == INTEGER_CST
7093 && !integer_zerop (arg1)
7094 && TREE_CONSTANT (arg0)
7095 && TREE_CODE (arg0) == ADDR_EXPR
7096 && code == EQ_EXPR)
7097 t1 = build_int_2 (0, 0);
7098 #endif
7099 /* Two real constants can be compared explicitly. */
7100 else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
7102 /* If either operand is a NaN, the result is false with two
7103 exceptions: First, an NE_EXPR is true on NaNs, but that case
7104 is already handled correctly since we will be inverting the
7105 result for NE_EXPR. Second, if we had inverted a LE_EXPR
7106 or a GE_EXPR into a LT_EXPR, we must return true so that it
7107 will be inverted into false. */
7109 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
7110 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
7111 t1 = build_int_2 (invert && code == LT_EXPR, 0);
7113 else if (code == EQ_EXPR)
7114 t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
7115 TREE_REAL_CST (arg1)),
7117 else
7118 t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
7119 TREE_REAL_CST (arg1)),
7123 if (t1 == NULL_TREE)
7124 return t;
7126 if (invert)
7127 TREE_INT_CST_LOW (t1) ^= 1;
7129 TREE_TYPE (t1) = type;
7130 if (TREE_CODE (type) == BOOLEAN_TYPE)
7131 return (*lang_hooks.truthvalue_conversion) (t1);
7132 return t1;
7134 case COND_EXPR:
7135 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
7136 so all simple results must be passed through pedantic_non_lvalue. */
7137 if (TREE_CODE (arg0) == INTEGER_CST)
7138 return pedantic_non_lvalue
7139 (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
7140 else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
7141 return pedantic_omit_one_operand (type, arg1, arg0);
7143 /* If the second operand is zero, invert the comparison and swap
7144 the second and third operands. Likewise if the second operand
7145 is constant and the third is not or if the third operand is
7146 equivalent to the first operand of the comparison. */
7148 if (integer_zerop (arg1)
7149 || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
7150 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
7151 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
7152 TREE_OPERAND (t, 2),
7153 TREE_OPERAND (arg0, 1))))
7155 /* See if this can be inverted. If it can't, possibly because
7156 it was a floating-point inequality comparison, don't do
7157 anything. */
7158 tem = invert_truthvalue (arg0);
7160 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
7162 t = build (code, type, tem,
7163 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
7164 arg0 = tem;
7165 /* arg1 should be the first argument of the new T. */
7166 arg1 = TREE_OPERAND (t, 1);
7167 STRIP_NOPS (arg1);
7171 /* If we have A op B ? A : C, we may be able to convert this to a
7172 simpler expression, depending on the operation and the values
7173 of B and C. Signed zeros prevent all of these transformations,
7174 for reasons given above each one. */
7176 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
7177 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
7178 arg1, TREE_OPERAND (arg0, 1))
7179 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
7181 tree arg2 = TREE_OPERAND (t, 2);
7182 enum tree_code comp_code = TREE_CODE (arg0);
7184 STRIP_NOPS (arg2);
7186 /* If we have A op 0 ? A : -A, consider applying the following
7187 transformations:
7189 A == 0? A : -A same as -A
7190 A != 0? A : -A same as A
7191 A >= 0? A : -A same as abs (A)
7192 A > 0? A : -A same as abs (A)
7193 A <= 0? A : -A same as -abs (A)
7194 A < 0? A : -A same as -abs (A)
7196 None of these transformations work for modes with signed
7197 zeros. If A is +/-0, the first two transformations will
7198 change the sign of the result (from +0 to -0, or vice
7199 versa). The last four will fix the sign of the result,
7200 even though the original expressions could be positive or
7201 negative, depending on the sign of A.
7203 Note that all these transformations are correct if A is
7204 NaN, since the two alternatives (A and -A) are also NaNs. */
7205 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
7206 ? real_zerop (TREE_OPERAND (arg0, 1))
7207 : integer_zerop (TREE_OPERAND (arg0, 1)))
7208 && TREE_CODE (arg2) == NEGATE_EXPR
7209 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
7210 switch (comp_code)
7212 case EQ_EXPR:
7213 return
7214 pedantic_non_lvalue
7215 (convert (type,
7216 negate_expr
7217 (convert (TREE_TYPE (TREE_OPERAND (t, 1)),
7218 arg1))));
7219 case NE_EXPR:
7220 return pedantic_non_lvalue (convert (type, arg1));
7221 case GE_EXPR:
7222 case GT_EXPR:
7223 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
7224 arg1 = convert ((*lang_hooks.types.signed_type)
7225 (TREE_TYPE (arg1)), arg1);
7226 return pedantic_non_lvalue
7227 (convert (type, fold (build1 (ABS_EXPR,
7228 TREE_TYPE (arg1), arg1))));
7229 case LE_EXPR:
7230 case LT_EXPR:
7231 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
7232 arg1 = convert ((lang_hooks.types.signed_type)
7233 (TREE_TYPE (arg1)), arg1);
7234 return pedantic_non_lvalue
7235 (negate_expr (convert (type,
7236 fold (build1 (ABS_EXPR,
7237 TREE_TYPE (arg1),
7238 arg1)))));
7239 default:
7240 abort ();
7243 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
7244 A == 0 ? A : 0 is always 0 unless A is -0. Note that
7245 both transformations are correct when A is NaN: A != 0
7246 is then true, and A == 0 is false. */
7248 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
7250 if (comp_code == NE_EXPR)
7251 return pedantic_non_lvalue (convert (type, arg1));
7252 else if (comp_code == EQ_EXPR)
7253 return pedantic_non_lvalue (convert (type, integer_zero_node));
7256 /* Try some transformations of A op B ? A : B.
7258 A == B? A : B same as B
7259 A != B? A : B same as A
7260 A >= B? A : B same as max (A, B)
7261 A > B? A : B same as max (B, A)
7262 A <= B? A : B same as min (A, B)
7263 A < B? A : B same as min (B, A)
7265 As above, these transformations don't work in the presence
7266 of signed zeros. For example, if A and B are zeros of
7267 opposite sign, the first two transformations will change
7268 the sign of the result. In the last four, the original
7269 expressions give different results for (A=+0, B=-0) and
7270 (A=-0, B=+0), but the transformed expressions do not.
7272 The first two transformations are correct if either A or B
7273 is a NaN. In the first transformation, the condition will
7274 be false, and B will indeed be chosen. In the case of the
7275 second transformation, the condition A != B will be true,
7276 and A will be chosen.
7278 The conversions to max() and min() are not correct if B is
7279 a number and A is not. The conditions in the original
7280 expressions will be false, so all four give B. The min()
7281 and max() versions would give a NaN instead. */
7282 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
7283 arg2, TREE_OPERAND (arg0, 0)))
7285 tree comp_op0 = TREE_OPERAND (arg0, 0);
7286 tree comp_op1 = TREE_OPERAND (arg0, 1);
7287 tree comp_type = TREE_TYPE (comp_op0);
7289 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
7290 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
7292 comp_type = type;
7293 comp_op0 = arg1;
7294 comp_op1 = arg2;
7297 switch (comp_code)
7299 case EQ_EXPR:
7300 return pedantic_non_lvalue (convert (type, arg2));
7301 case NE_EXPR:
7302 return pedantic_non_lvalue (convert (type, arg1));
7303 case LE_EXPR:
7304 case LT_EXPR:
7305 /* In C++ a ?: expression can be an lvalue, so put the
7306 operand which will be used if they are equal first
7307 so that we can convert this back to the
7308 corresponding COND_EXPR. */
7309 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
7310 return pedantic_non_lvalue
7311 (convert (type, fold (build (MIN_EXPR, comp_type,
7312 (comp_code == LE_EXPR
7313 ? comp_op0 : comp_op1),
7314 (comp_code == LE_EXPR
7315 ? comp_op1 : comp_op0)))));
7316 break;
7317 case GE_EXPR:
7318 case GT_EXPR:
7319 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
7320 return pedantic_non_lvalue
7321 (convert (type, fold (build (MAX_EXPR, comp_type,
7322 (comp_code == GE_EXPR
7323 ? comp_op0 : comp_op1),
7324 (comp_code == GE_EXPR
7325 ? comp_op1 : comp_op0)))));
7326 break;
7327 default:
7328 abort ();
7332 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
7333 we might still be able to simplify this. For example,
7334 if C1 is one less or one more than C2, this might have started
7335 out as a MIN or MAX and been transformed by this function.
7336 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
7338 if (INTEGRAL_TYPE_P (type)
7339 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7340 && TREE_CODE (arg2) == INTEGER_CST)
7341 switch (comp_code)
7343 case EQ_EXPR:
7344 /* We can replace A with C1 in this case. */
7345 arg1 = convert (type, TREE_OPERAND (arg0, 1));
7346 t = build (code, type, TREE_OPERAND (t, 0), arg1,
7347 TREE_OPERAND (t, 2));
7348 break;
7350 case LT_EXPR:
7351 /* If C1 is C2 + 1, this is min(A, C2). */
7352 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
7353 && operand_equal_p (TREE_OPERAND (arg0, 1),
7354 const_binop (PLUS_EXPR, arg2,
7355 integer_one_node, 0), 1))
7356 return pedantic_non_lvalue
7357 (fold (build (MIN_EXPR, type, arg1, arg2)));
7358 break;
7360 case LE_EXPR:
7361 /* If C1 is C2 - 1, this is min(A, C2). */
7362 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
7363 && operand_equal_p (TREE_OPERAND (arg0, 1),
7364 const_binop (MINUS_EXPR, arg2,
7365 integer_one_node, 0), 1))
7366 return pedantic_non_lvalue
7367 (fold (build (MIN_EXPR, type, arg1, arg2)));
7368 break;
7370 case GT_EXPR:
7371 /* If C1 is C2 - 1, this is max(A, C2). */
7372 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
7373 && operand_equal_p (TREE_OPERAND (arg0, 1),
7374 const_binop (MINUS_EXPR, arg2,
7375 integer_one_node, 0), 1))
7376 return pedantic_non_lvalue
7377 (fold (build (MAX_EXPR, type, arg1, arg2)));
7378 break;
7380 case GE_EXPR:
7381 /* If C1 is C2 + 1, this is max(A, C2). */
7382 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
7383 && operand_equal_p (TREE_OPERAND (arg0, 1),
7384 const_binop (PLUS_EXPR, arg2,
7385 integer_one_node, 0), 1))
7386 return pedantic_non_lvalue
7387 (fold (build (MAX_EXPR, type, arg1, arg2)));
7388 break;
7389 case NE_EXPR:
7390 break;
7391 default:
7392 abort ();
7396 /* If the second operand is simpler than the third, swap them
7397 since that produces better jump optimization results. */
7398 if ((TREE_CONSTANT (arg1) || DECL_P (arg1)
7399 || TREE_CODE (arg1) == SAVE_EXPR)
7400 && ! (TREE_CONSTANT (TREE_OPERAND (t, 2))
7401 || DECL_P (TREE_OPERAND (t, 2))
7402 || TREE_CODE (TREE_OPERAND (t, 2)) == SAVE_EXPR))
7404 /* See if this can be inverted. If it can't, possibly because
7405 it was a floating-point inequality comparison, don't do
7406 anything. */
7407 tem = invert_truthvalue (arg0);
7409 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
7411 t = build (code, type, tem,
7412 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
7413 arg0 = tem;
7414 /* arg1 should be the first argument of the new T. */
7415 arg1 = TREE_OPERAND (t, 1);
7416 STRIP_NOPS (arg1);
7420 /* Convert A ? 1 : 0 to simply A. */
7421 if (integer_onep (TREE_OPERAND (t, 1))
7422 && integer_zerop (TREE_OPERAND (t, 2))
7423 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
7424 call to fold will try to move the conversion inside
7425 a COND, which will recurse. In that case, the COND_EXPR
7426 is probably the best choice, so leave it alone. */
7427 && type == TREE_TYPE (arg0))
7428 return pedantic_non_lvalue (arg0);
7430 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
7431 over COND_EXPR in cases such as floating point comparisons. */
7432 if (integer_zerop (TREE_OPERAND (t, 1))
7433 && integer_onep (TREE_OPERAND (t, 2))
7434 && truth_value_p (TREE_CODE (arg0)))
7435 return pedantic_non_lvalue (convert (type,
7436 invert_truthvalue (arg0)));
7438 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
7439 operation is simply A & 2. */
7441 if (integer_zerop (TREE_OPERAND (t, 2))
7442 && TREE_CODE (arg0) == NE_EXPR
7443 && integer_zerop (TREE_OPERAND (arg0, 1))
7444 && integer_pow2p (arg1)
7445 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
7446 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
7447 arg1, 1))
7448 return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
7450 /* Convert A ? B : 0 into A && B if A and B are truth values. */
7451 if (integer_zerop (TREE_OPERAND (t, 2))
7452 && truth_value_p (TREE_CODE (arg0))
7453 && truth_value_p (TREE_CODE (arg1)))
7454 return pedantic_non_lvalue (fold (build (TRUTH_ANDIF_EXPR, type,
7455 arg0, arg1)));
7457 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
7458 if (integer_onep (TREE_OPERAND (t, 2))
7459 && truth_value_p (TREE_CODE (arg0))
7460 && truth_value_p (TREE_CODE (arg1)))
7462 /* Only perform transformation if ARG0 is easily inverted. */
7463 tem = invert_truthvalue (arg0);
7464 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
7465 return pedantic_non_lvalue (fold (build (TRUTH_ORIF_EXPR, type,
7466 tem, arg1)));
7469 return t;
7471 case COMPOUND_EXPR:
7472 /* When pedantic, a compound expression can be neither an lvalue
7473 nor an integer constant expression. */
7474 if (TREE_SIDE_EFFECTS (arg0) || pedantic)
7475 return t;
7476 /* Don't let (0, 0) be null pointer constant. */
7477 if (integer_zerop (arg1))
7478 return build1 (NOP_EXPR, type, arg1);
7479 return convert (type, arg1);
7481 case COMPLEX_EXPR:
7482 if (wins)
7483 return build_complex (type, arg0, arg1);
7484 return t;
7486 case REALPART_EXPR:
7487 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7488 return t;
7489 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7490 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
7491 TREE_OPERAND (arg0, 1));
7492 else if (TREE_CODE (arg0) == COMPLEX_CST)
7493 return TREE_REALPART (arg0);
7494 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7495 return fold (build (TREE_CODE (arg0), type,
7496 fold (build1 (REALPART_EXPR, type,
7497 TREE_OPERAND (arg0, 0))),
7498 fold (build1 (REALPART_EXPR,
7499 type, TREE_OPERAND (arg0, 1)))));
7500 return t;
7502 case IMAGPART_EXPR:
7503 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7504 return convert (type, integer_zero_node);
7505 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7506 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
7507 TREE_OPERAND (arg0, 0));
7508 else if (TREE_CODE (arg0) == COMPLEX_CST)
7509 return TREE_IMAGPART (arg0);
7510 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7511 return fold (build (TREE_CODE (arg0), type,
7512 fold (build1 (IMAGPART_EXPR, type,
7513 TREE_OPERAND (arg0, 0))),
7514 fold (build1 (IMAGPART_EXPR, type,
7515 TREE_OPERAND (arg0, 1)))));
7516 return t;
7518 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
7519 appropriate. */
7520 case CLEANUP_POINT_EXPR:
7521 if (! has_cleanups (arg0))
7522 return TREE_OPERAND (t, 0);
7525 enum tree_code code0 = TREE_CODE (arg0);
7526 int kind0 = TREE_CODE_CLASS (code0);
7527 tree arg00 = TREE_OPERAND (arg0, 0);
7528 tree arg01;
7530 if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
7531 return fold (build1 (code0, type,
7532 fold (build1 (CLEANUP_POINT_EXPR,
7533 TREE_TYPE (arg00), arg00))));
7535 if (kind0 == '<' || kind0 == '2'
7536 || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
7537 || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR
7538 || code0 == TRUTH_XOR_EXPR)
7540 arg01 = TREE_OPERAND (arg0, 1);
7542 if (TREE_CONSTANT (arg00)
7543 || ((code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR)
7544 && ! has_cleanups (arg00)))
7545 return fold (build (code0, type, arg00,
7546 fold (build1 (CLEANUP_POINT_EXPR,
7547 TREE_TYPE (arg01), arg01))));
7549 if (TREE_CONSTANT (arg01))
7550 return fold (build (code0, type,
7551 fold (build1 (CLEANUP_POINT_EXPR,
7552 TREE_TYPE (arg00), arg00)),
7553 arg01));
7556 return t;
7559 case CALL_EXPR:
7560 /* Check for a built-in function. */
7561 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR
7562 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (expr, 0), 0))
7563 == FUNCTION_DECL)
7564 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (expr, 0), 0)))
7566 tree tmp = fold_builtin (expr);
7567 if (tmp)
7568 return tmp;
7570 return t;
7572 default:
7573 return t;
7574 } /* switch (code) */
7577 /* Determine if first argument is a multiple of second argument. Return 0 if
7578 it is not, or we cannot easily determined it to be.
7580 An example of the sort of thing we care about (at this point; this routine
7581 could surely be made more general, and expanded to do what the *_DIV_EXPR's
7582 fold cases do now) is discovering that
7584 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7586 is a multiple of
7588 SAVE_EXPR (J * 8)
7590 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
7592 This code also handles discovering that
7594 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7596 is a multiple of 8 so we don't have to worry about dealing with a
7597 possible remainder.
7599 Note that we *look* inside a SAVE_EXPR only to determine how it was
7600 calculated; it is not safe for fold to do much of anything else with the
7601 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
7602 at run time. For example, the latter example above *cannot* be implemented
7603 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
7604 evaluation time of the original SAVE_EXPR is not necessarily the same at
7605 the time the new expression is evaluated. The only optimization of this
7606 sort that would be valid is changing
7608 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
7610 divided by 8 to
7612 SAVE_EXPR (I) * SAVE_EXPR (J)
7614 (where the same SAVE_EXPR (J) is used in the original and the
7615 transformed version). */
7617 static int
7618 multiple_of_p (type, top, bottom)
7619 tree type;
7620 tree top;
7621 tree bottom;
7623 if (operand_equal_p (top, bottom, 0))
7624 return 1;
7626 if (TREE_CODE (type) != INTEGER_TYPE)
7627 return 0;
7629 switch (TREE_CODE (top))
7631 case MULT_EXPR:
7632 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7633 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7635 case PLUS_EXPR:
7636 case MINUS_EXPR:
7637 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7638 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7640 case LSHIFT_EXPR:
7641 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
7643 tree op1, t1;
7645 op1 = TREE_OPERAND (top, 1);
7646 /* const_binop may not detect overflow correctly,
7647 so check for it explicitly here. */
7648 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
7649 > TREE_INT_CST_LOW (op1)
7650 && TREE_INT_CST_HIGH (op1) == 0
7651 && 0 != (t1 = convert (type,
7652 const_binop (LSHIFT_EXPR, size_one_node,
7653 op1, 0)))
7654 && ! TREE_OVERFLOW (t1))
7655 return multiple_of_p (type, t1, bottom);
7657 return 0;
7659 case NOP_EXPR:
7660 /* Can't handle conversions from non-integral or wider integral type. */
7661 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
7662 || (TYPE_PRECISION (type)
7663 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
7664 return 0;
7666 /* .. fall through ... */
7668 case SAVE_EXPR:
7669 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
7671 case INTEGER_CST:
7672 if (TREE_CODE (bottom) != INTEGER_CST
7673 || (TREE_UNSIGNED (type)
7674 && (tree_int_cst_sgn (top) < 0
7675 || tree_int_cst_sgn (bottom) < 0)))
7676 return 0;
7677 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
7678 top, bottom, 0));
7680 default:
7681 return 0;
7685 /* Return true if `t' is known to be non-negative. */
7688 tree_expr_nonnegative_p (t)
7689 tree t;
7691 switch (TREE_CODE (t))
7693 case ABS_EXPR:
7694 case FFS_EXPR:
7695 case POPCOUNT_EXPR:
7696 case PARITY_EXPR:
7697 return 1;
7699 case CLZ_EXPR:
7700 case CTZ_EXPR:
7701 /* These are undefined at zero. This is true even if
7702 C[LT]Z_DEFINED_VALUE_AT_ZERO is set, since what we're
7703 computing here is a user-visible property. */
7704 return 0;
7706 case INTEGER_CST:
7707 return tree_int_cst_sgn (t) >= 0;
7708 case TRUNC_DIV_EXPR:
7709 case CEIL_DIV_EXPR:
7710 case FLOOR_DIV_EXPR:
7711 case ROUND_DIV_EXPR:
7712 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
7713 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7714 case TRUNC_MOD_EXPR:
7715 case CEIL_MOD_EXPR:
7716 case FLOOR_MOD_EXPR:
7717 case ROUND_MOD_EXPR:
7718 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
7719 case COND_EXPR:
7720 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
7721 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
7722 case COMPOUND_EXPR:
7723 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7724 case MIN_EXPR:
7725 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
7726 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7727 case MAX_EXPR:
7728 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
7729 || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7730 case MODIFY_EXPR:
7731 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7732 case BIND_EXPR:
7733 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7734 case SAVE_EXPR:
7735 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
7736 case NON_LVALUE_EXPR:
7737 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
7738 case RTL_EXPR:
7739 return rtl_expr_nonnegative_p (RTL_EXPR_RTL (t));
7741 default:
7742 if (truth_value_p (TREE_CODE (t)))
7743 /* Truth values evaluate to 0 or 1, which is nonnegative. */
7744 return 1;
7745 else
7746 /* We don't know sign of `t', so be conservative and return false. */
7747 return 0;
7751 /* Return true if `r' is known to be non-negative.
7752 Only handles constants at the moment. */
7755 rtl_expr_nonnegative_p (r)
7756 rtx r;
7758 switch (GET_CODE (r))
7760 case CONST_INT:
7761 return INTVAL (r) >= 0;
7763 case CONST_DOUBLE:
7764 if (GET_MODE (r) == VOIDmode)
7765 return CONST_DOUBLE_HIGH (r) >= 0;
7766 return 0;
7768 case CONST_VECTOR:
7770 int units, i;
7771 rtx elt;
7773 units = CONST_VECTOR_NUNITS (r);
7775 for (i = 0; i < units; ++i)
7777 elt = CONST_VECTOR_ELT (r, i);
7778 if (!rtl_expr_nonnegative_p (elt))
7779 return 0;
7782 return 1;
7785 case SYMBOL_REF:
7786 case LABEL_REF:
7787 /* These are always nonnegative. */
7788 return 1;
7790 default:
7791 return 0;
7795 #include "gt-fold-const.h"