1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
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
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
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
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. */
47 #include "coretypes.h"
58 #include "langhooks.h"
61 static void encode (HOST_WIDE_INT
*, unsigned HOST_WIDE_INT
, HOST_WIDE_INT
);
62 static void decode (HOST_WIDE_INT
*, unsigned HOST_WIDE_INT
*, HOST_WIDE_INT
*);
63 static bool negate_mathfn_p (enum built_in_function
);
64 static bool negate_expr_p (tree
);
65 static tree
negate_expr (tree
);
66 static tree
split_tree (tree
, enum tree_code
, tree
*, tree
*, tree
*, int);
67 static tree
associate_trees (tree
, tree
, enum tree_code
, tree
);
68 static tree
int_const_binop (enum tree_code
, tree
, tree
, int);
69 static tree
const_binop (enum tree_code
, tree
, tree
, int);
70 static hashval_t
size_htab_hash (const void *);
71 static int size_htab_eq (const void *, const void *);
72 static tree
fold_convert_const (enum tree_code
, tree
, tree
);
73 static enum tree_code
invert_tree_comparison (enum tree_code
);
74 static enum tree_code
swap_tree_comparison (enum tree_code
);
75 static int comparison_to_compcode (enum tree_code
);
76 static enum tree_code
compcode_to_comparison (int);
77 static int truth_value_p (enum tree_code
);
78 static int operand_equal_for_comparison_p (tree
, tree
, tree
);
79 static int twoval_comparison_p (tree
, tree
*, tree
*, int *);
80 static tree
eval_subst (tree
, tree
, tree
, tree
, tree
);
81 static tree
pedantic_omit_one_operand (tree
, tree
, tree
);
82 static tree
distribute_bit_expr (enum tree_code
, tree
, tree
, tree
);
83 static tree
make_bit_field_ref (tree
, tree
, int, int, int);
84 static tree
optimize_bit_field_compare (enum tree_code
, tree
, tree
, tree
);
85 static tree
decode_field_reference (tree
, HOST_WIDE_INT
*, HOST_WIDE_INT
*,
86 enum machine_mode
*, int *, int *,
88 static int all_ones_mask_p (tree
, int);
89 static tree
sign_bit_p (tree
, tree
);
90 static int simple_operand_p (tree
);
91 static tree
range_binop (enum tree_code
, tree
, tree
, int, tree
, int);
92 static tree
make_range (tree
, int *, tree
*, tree
*);
93 static tree
build_range_check (tree
, tree
, int, tree
, tree
);
94 static int merge_ranges (int *, tree
*, tree
*, int, tree
, tree
, int, tree
,
96 static tree
fold_range_test (tree
);
97 static tree
unextend (tree
, int, int, tree
);
98 static tree
fold_truthop (enum tree_code
, tree
, tree
, tree
);
99 static tree
optimize_minmax_comparison (tree
);
100 static tree
extract_muldiv (tree
, tree
, enum tree_code
, tree
);
101 static tree
extract_muldiv_1 (tree
, tree
, enum tree_code
, tree
);
102 static tree
strip_compound_expr (tree
, tree
);
103 static int multiple_of_p (tree
, tree
, tree
);
104 static tree
constant_boolean_node (int, tree
);
105 static int count_cond (tree
, int);
106 static tree
fold_binary_op_with_conditional_arg (enum tree_code
, tree
, tree
,
108 static bool fold_real_zero_addition_p (tree
, tree
, int);
109 static tree
fold_mathfn_compare (enum built_in_function
, enum tree_code
,
111 static tree
fold_inf_compare (enum tree_code
, tree
, tree
, tree
);
112 static bool reorder_operands_p (tree
, tree
);
113 static bool tree_swap_operands_p (tree
, tree
, bool);
115 /* The following constants represent a bit based encoding of GCC's
116 comparison operators. This encoding simplifies transformations
117 on relational comparison operators, such as AND and OR. */
118 #define COMPCODE_FALSE 0
119 #define COMPCODE_LT 1
120 #define COMPCODE_EQ 2
121 #define COMPCODE_LE 3
122 #define COMPCODE_GT 4
123 #define COMPCODE_NE 5
124 #define COMPCODE_GE 6
125 #define COMPCODE_TRUE 7
127 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
128 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
129 and SUM1. Then this yields nonzero if overflow occurred during the
132 Overflow occurs if A and B have the same sign, but A and SUM differ in
133 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
135 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
137 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
138 We do that by representing the two-word integer in 4 words, with only
139 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
140 number. The value of the word is LOWPART + HIGHPART * BASE. */
143 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
144 #define HIGHPART(x) \
145 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
146 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
148 /* Unpack a two-word integer into 4 words.
149 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
150 WORDS points to the array of HOST_WIDE_INTs. */
153 encode (HOST_WIDE_INT
*words
, unsigned HOST_WIDE_INT low
, HOST_WIDE_INT hi
)
155 words
[0] = LOWPART (low
);
156 words
[1] = HIGHPART (low
);
157 words
[2] = LOWPART (hi
);
158 words
[3] = HIGHPART (hi
);
161 /* Pack an array of 4 words into a two-word integer.
162 WORDS points to the array of words.
163 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
166 decode (HOST_WIDE_INT
*words
, unsigned HOST_WIDE_INT
*low
,
169 *low
= words
[0] + words
[1] * BASE
;
170 *hi
= words
[2] + words
[3] * BASE
;
173 /* Make the integer constant T valid for its type by setting to 0 or 1 all
174 the bits in the constant that don't belong in the type.
176 Return 1 if a signed overflow occurs, 0 otherwise. If OVERFLOW is
177 nonzero, a signed overflow has already occurred in calculating T, so
181 force_fit_type (tree t
, int overflow
)
183 unsigned HOST_WIDE_INT low
;
187 if (TREE_CODE (t
) == REAL_CST
)
189 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
190 Consider doing it via real_convert now. */
194 else if (TREE_CODE (t
) != INTEGER_CST
)
197 low
= TREE_INT_CST_LOW (t
);
198 high
= TREE_INT_CST_HIGH (t
);
200 if (POINTER_TYPE_P (TREE_TYPE (t
))
201 || TREE_CODE (TREE_TYPE (t
)) == OFFSET_TYPE
)
204 prec
= TYPE_PRECISION (TREE_TYPE (t
));
206 /* First clear all bits that are beyond the type's precision. */
208 if (prec
== 2 * HOST_BITS_PER_WIDE_INT
)
210 else if (prec
> HOST_BITS_PER_WIDE_INT
)
211 TREE_INT_CST_HIGH (t
)
212 &= ~((HOST_WIDE_INT
) (-1) << (prec
- HOST_BITS_PER_WIDE_INT
));
215 TREE_INT_CST_HIGH (t
) = 0;
216 if (prec
< HOST_BITS_PER_WIDE_INT
)
217 TREE_INT_CST_LOW (t
) &= ~((unsigned HOST_WIDE_INT
) (-1) << prec
);
220 /* Unsigned types do not suffer sign extension or overflow unless they
222 if (TREE_UNSIGNED (TREE_TYPE (t
))
223 && ! (TREE_CODE (TREE_TYPE (t
)) == INTEGER_TYPE
224 && TYPE_IS_SIZETYPE (TREE_TYPE (t
))))
227 /* If the value's sign bit is set, extend the sign. */
228 if (prec
!= 2 * HOST_BITS_PER_WIDE_INT
229 && (prec
> HOST_BITS_PER_WIDE_INT
230 ? 0 != (TREE_INT_CST_HIGH (t
)
232 << (prec
- HOST_BITS_PER_WIDE_INT
- 1)))
233 : 0 != (TREE_INT_CST_LOW (t
)
234 & ((unsigned HOST_WIDE_INT
) 1 << (prec
- 1)))))
236 /* Value is negative:
237 set to 1 all the bits that are outside this type's precision. */
238 if (prec
> HOST_BITS_PER_WIDE_INT
)
239 TREE_INT_CST_HIGH (t
)
240 |= ((HOST_WIDE_INT
) (-1) << (prec
- HOST_BITS_PER_WIDE_INT
));
243 TREE_INT_CST_HIGH (t
) = -1;
244 if (prec
< HOST_BITS_PER_WIDE_INT
)
245 TREE_INT_CST_LOW (t
) |= ((unsigned HOST_WIDE_INT
) (-1) << prec
);
249 /* Return nonzero if signed overflow occurred. */
251 ((overflow
| (low
^ TREE_INT_CST_LOW (t
)) | (high
^ TREE_INT_CST_HIGH (t
)))
255 /* Add two doubleword integers with doubleword result.
256 Each argument is given as two `HOST_WIDE_INT' pieces.
257 One argument is L1 and H1; the other, L2 and H2.
258 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
261 add_double (unsigned HOST_WIDE_INT l1
, HOST_WIDE_INT h1
,
262 unsigned HOST_WIDE_INT l2
, HOST_WIDE_INT h2
,
263 unsigned HOST_WIDE_INT
*lv
, HOST_WIDE_INT
*hv
)
265 unsigned HOST_WIDE_INT l
;
269 h
= h1
+ h2
+ (l
< l1
);
273 return OVERFLOW_SUM_SIGN (h1
, h2
, h
);
276 /* Negate a doubleword integer with doubleword result.
277 Return nonzero if the operation overflows, assuming it's signed.
278 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
279 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
282 neg_double (unsigned HOST_WIDE_INT l1
, HOST_WIDE_INT h1
,
283 unsigned HOST_WIDE_INT
*lv
, HOST_WIDE_INT
*hv
)
289 return (*hv
& h1
) < 0;
299 /* Multiply two doubleword integers with doubleword result.
300 Return nonzero if the operation overflows, assuming it's signed.
301 Each argument is given as two `HOST_WIDE_INT' pieces.
302 One argument is L1 and H1; the other, L2 and H2.
303 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
306 mul_double (unsigned HOST_WIDE_INT l1
, HOST_WIDE_INT h1
,
307 unsigned HOST_WIDE_INT l2
, HOST_WIDE_INT h2
,
308 unsigned HOST_WIDE_INT
*lv
, HOST_WIDE_INT
*hv
)
310 HOST_WIDE_INT arg1
[4];
311 HOST_WIDE_INT arg2
[4];
312 HOST_WIDE_INT prod
[4 * 2];
313 unsigned HOST_WIDE_INT carry
;
315 unsigned HOST_WIDE_INT toplow
, neglow
;
316 HOST_WIDE_INT tophigh
, neghigh
;
318 encode (arg1
, l1
, h1
);
319 encode (arg2
, l2
, h2
);
321 memset (prod
, 0, sizeof prod
);
323 for (i
= 0; i
< 4; i
++)
326 for (j
= 0; j
< 4; j
++)
329 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
330 carry
+= arg1
[i
] * arg2
[j
];
331 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
333 prod
[k
] = LOWPART (carry
);
334 carry
= HIGHPART (carry
);
339 decode (prod
, lv
, hv
); /* This ignores prod[4] through prod[4*2-1] */
341 /* Check for overflow by calculating the top half of the answer in full;
342 it should agree with the low half's sign bit. */
343 decode (prod
+ 4, &toplow
, &tophigh
);
346 neg_double (l2
, h2
, &neglow
, &neghigh
);
347 add_double (neglow
, neghigh
, toplow
, tophigh
, &toplow
, &tophigh
);
351 neg_double (l1
, h1
, &neglow
, &neghigh
);
352 add_double (neglow
, neghigh
, toplow
, tophigh
, &toplow
, &tophigh
);
354 return (*hv
< 0 ? ~(toplow
& tophigh
) : toplow
| tophigh
) != 0;
357 /* Shift the doubleword integer in L1, H1 left by COUNT places
358 keeping only PREC bits of result.
359 Shift right if COUNT is negative.
360 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
361 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
364 lshift_double (unsigned HOST_WIDE_INT l1
, HOST_WIDE_INT h1
,
365 HOST_WIDE_INT count
, unsigned int prec
,
366 unsigned HOST_WIDE_INT
*lv
, HOST_WIDE_INT
*hv
, int arith
)
368 unsigned HOST_WIDE_INT signmask
;
372 rshift_double (l1
, h1
, -count
, prec
, lv
, hv
, arith
);
376 #ifdef SHIFT_COUNT_TRUNCATED
377 if (SHIFT_COUNT_TRUNCATED
)
381 if (count
>= 2 * HOST_BITS_PER_WIDE_INT
)
383 /* Shifting by the host word size is undefined according to the
384 ANSI standard, so we must handle this as a special case. */
388 else if (count
>= HOST_BITS_PER_WIDE_INT
)
390 *hv
= l1
<< (count
- HOST_BITS_PER_WIDE_INT
);
395 *hv
= (((unsigned HOST_WIDE_INT
) h1
<< count
)
396 | (l1
>> (HOST_BITS_PER_WIDE_INT
- count
- 1) >> 1));
400 /* Sign extend all bits that are beyond the precision. */
402 signmask
= -((prec
> HOST_BITS_PER_WIDE_INT
403 ? ((unsigned HOST_WIDE_INT
) *hv
404 >> (prec
- HOST_BITS_PER_WIDE_INT
- 1))
405 : (*lv
>> (prec
- 1))) & 1);
407 if (prec
>= 2 * HOST_BITS_PER_WIDE_INT
)
409 else if (prec
>= HOST_BITS_PER_WIDE_INT
)
411 *hv
&= ~((HOST_WIDE_INT
) (-1) << (prec
- HOST_BITS_PER_WIDE_INT
));
412 *hv
|= signmask
<< (prec
- HOST_BITS_PER_WIDE_INT
);
417 *lv
&= ~((unsigned HOST_WIDE_INT
) (-1) << prec
);
418 *lv
|= signmask
<< prec
;
422 /* Shift the doubleword integer in L1, H1 right by COUNT places
423 keeping only PREC bits of result. COUNT must be positive.
424 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
425 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
428 rshift_double (unsigned HOST_WIDE_INT l1
, HOST_WIDE_INT h1
,
429 HOST_WIDE_INT count
, unsigned int prec
,
430 unsigned HOST_WIDE_INT
*lv
, HOST_WIDE_INT
*hv
,
433 unsigned HOST_WIDE_INT signmask
;
436 ? -((unsigned HOST_WIDE_INT
) h1
>> (HOST_BITS_PER_WIDE_INT
- 1))
439 #ifdef SHIFT_COUNT_TRUNCATED
440 if (SHIFT_COUNT_TRUNCATED
)
444 if (count
>= 2 * HOST_BITS_PER_WIDE_INT
)
446 /* Shifting by the host word size is undefined according to the
447 ANSI standard, so we must handle this as a special case. */
451 else if (count
>= HOST_BITS_PER_WIDE_INT
)
454 *lv
= (unsigned HOST_WIDE_INT
) h1
>> (count
- HOST_BITS_PER_WIDE_INT
);
458 *hv
= (unsigned HOST_WIDE_INT
) h1
>> count
;
460 | ((unsigned HOST_WIDE_INT
) h1
<< (HOST_BITS_PER_WIDE_INT
- count
- 1) << 1));
463 /* Zero / sign extend all bits that are beyond the precision. */
465 if (count
>= (HOST_WIDE_INT
)prec
)
470 else if ((prec
- count
) >= 2 * HOST_BITS_PER_WIDE_INT
)
472 else if ((prec
- count
) >= HOST_BITS_PER_WIDE_INT
)
474 *hv
&= ~((HOST_WIDE_INT
) (-1) << (prec
- count
- HOST_BITS_PER_WIDE_INT
));
475 *hv
|= signmask
<< (prec
- count
- HOST_BITS_PER_WIDE_INT
);
480 *lv
&= ~((unsigned HOST_WIDE_INT
) (-1) << (prec
- count
));
481 *lv
|= signmask
<< (prec
- count
);
485 /* Rotate the doubleword integer in L1, H1 left by COUNT places
486 keeping only PREC bits of result.
487 Rotate right if COUNT is negative.
488 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
491 lrotate_double (unsigned HOST_WIDE_INT l1
, HOST_WIDE_INT h1
,
492 HOST_WIDE_INT count
, unsigned int prec
,
493 unsigned HOST_WIDE_INT
*lv
, HOST_WIDE_INT
*hv
)
495 unsigned HOST_WIDE_INT s1l
, s2l
;
496 HOST_WIDE_INT s1h
, s2h
;
502 lshift_double (l1
, h1
, count
, prec
, &s1l
, &s1h
, 0);
503 rshift_double (l1
, h1
, prec
- count
, prec
, &s2l
, &s2h
, 0);
508 /* Rotate the doubleword integer in L1, H1 left by COUNT places
509 keeping only PREC bits of result. COUNT must be positive.
510 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
513 rrotate_double (unsigned HOST_WIDE_INT l1
, HOST_WIDE_INT h1
,
514 HOST_WIDE_INT count
, unsigned int prec
,
515 unsigned HOST_WIDE_INT
*lv
, HOST_WIDE_INT
*hv
)
517 unsigned HOST_WIDE_INT s1l
, s2l
;
518 HOST_WIDE_INT s1h
, s2h
;
524 rshift_double (l1
, h1
, count
, prec
, &s1l
, &s1h
, 0);
525 lshift_double (l1
, h1
, prec
- count
, prec
, &s2l
, &s2h
, 0);
530 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
531 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
532 CODE is a tree code for a kind of division, one of
533 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
535 It controls how the quotient is rounded to an integer.
536 Return nonzero if the operation overflows.
537 UNS nonzero says do unsigned division. */
540 div_and_round_double (enum tree_code code
, int uns
,
541 unsigned HOST_WIDE_INT lnum_orig
, /* num == numerator == dividend */
542 HOST_WIDE_INT hnum_orig
,
543 unsigned HOST_WIDE_INT lden_orig
, /* den == denominator == divisor */
544 HOST_WIDE_INT hden_orig
,
545 unsigned HOST_WIDE_INT
*lquo
,
546 HOST_WIDE_INT
*hquo
, unsigned HOST_WIDE_INT
*lrem
,
550 HOST_WIDE_INT num
[4 + 1]; /* extra element for scaling. */
551 HOST_WIDE_INT den
[4], quo
[4];
553 unsigned HOST_WIDE_INT work
;
554 unsigned HOST_WIDE_INT carry
= 0;
555 unsigned HOST_WIDE_INT lnum
= lnum_orig
;
556 HOST_WIDE_INT hnum
= hnum_orig
;
557 unsigned HOST_WIDE_INT lden
= lden_orig
;
558 HOST_WIDE_INT hden
= hden_orig
;
561 if (hden
== 0 && lden
== 0)
562 overflow
= 1, lden
= 1;
564 /* Calculate quotient sign and convert operands to unsigned. */
570 /* (minimum integer) / (-1) is the only overflow case. */
571 if (neg_double (lnum
, hnum
, &lnum
, &hnum
)
572 && ((HOST_WIDE_INT
) lden
& hden
) == -1)
578 neg_double (lden
, hden
, &lden
, &hden
);
582 if (hnum
== 0 && hden
== 0)
583 { /* single precision */
585 /* This unsigned division rounds toward zero. */
591 { /* trivial case: dividend < divisor */
592 /* hden != 0 already checked. */
599 memset (quo
, 0, sizeof quo
);
601 memset (num
, 0, sizeof num
); /* to zero 9th element */
602 memset (den
, 0, sizeof den
);
604 encode (num
, lnum
, hnum
);
605 encode (den
, lden
, hden
);
607 /* Special code for when the divisor < BASE. */
608 if (hden
== 0 && lden
< (unsigned HOST_WIDE_INT
) BASE
)
610 /* hnum != 0 already checked. */
611 for (i
= 4 - 1; i
>= 0; i
--)
613 work
= num
[i
] + carry
* BASE
;
614 quo
[i
] = work
/ lden
;
620 /* Full double precision division,
621 with thanks to Don Knuth's "Seminumerical Algorithms". */
622 int num_hi_sig
, den_hi_sig
;
623 unsigned HOST_WIDE_INT quo_est
, scale
;
625 /* Find the highest nonzero divisor digit. */
626 for (i
= 4 - 1;; i
--)
633 /* Insure that the first digit of the divisor is at least BASE/2.
634 This is required by the quotient digit estimation algorithm. */
636 scale
= BASE
/ (den
[den_hi_sig
] + 1);
638 { /* scale divisor and dividend */
640 for (i
= 0; i
<= 4 - 1; i
++)
642 work
= (num
[i
] * scale
) + carry
;
643 num
[i
] = LOWPART (work
);
644 carry
= HIGHPART (work
);
649 for (i
= 0; i
<= 4 - 1; i
++)
651 work
= (den
[i
] * scale
) + carry
;
652 den
[i
] = LOWPART (work
);
653 carry
= HIGHPART (work
);
654 if (den
[i
] != 0) den_hi_sig
= i
;
661 for (i
= num_hi_sig
- den_hi_sig
- 1; i
>= 0; i
--)
663 /* Guess the next quotient digit, quo_est, by dividing the first
664 two remaining dividend digits by the high order quotient digit.
665 quo_est is never low and is at most 2 high. */
666 unsigned HOST_WIDE_INT tmp
;
668 num_hi_sig
= i
+ den_hi_sig
+ 1;
669 work
= num
[num_hi_sig
] * BASE
+ num
[num_hi_sig
- 1];
670 if (num
[num_hi_sig
] != den
[den_hi_sig
])
671 quo_est
= work
/ den
[den_hi_sig
];
675 /* Refine quo_est so it's usually correct, and at most one high. */
676 tmp
= work
- quo_est
* den
[den_hi_sig
];
678 && (den
[den_hi_sig
- 1] * quo_est
679 > (tmp
* BASE
+ num
[num_hi_sig
- 2])))
682 /* Try QUO_EST as the quotient digit, by multiplying the
683 divisor by QUO_EST and subtracting from the remaining dividend.
684 Keep in mind that QUO_EST is the I - 1st digit. */
687 for (j
= 0; j
<= den_hi_sig
; j
++)
689 work
= quo_est
* den
[j
] + carry
;
690 carry
= HIGHPART (work
);
691 work
= num
[i
+ j
] - LOWPART (work
);
692 num
[i
+ j
] = LOWPART (work
);
693 carry
+= HIGHPART (work
) != 0;
696 /* If quo_est was high by one, then num[i] went negative and
697 we need to correct things. */
698 if (num
[num_hi_sig
] < (HOST_WIDE_INT
) carry
)
701 carry
= 0; /* add divisor back in */
702 for (j
= 0; j
<= den_hi_sig
; j
++)
704 work
= num
[i
+ j
] + den
[j
] + carry
;
705 carry
= HIGHPART (work
);
706 num
[i
+ j
] = LOWPART (work
);
709 num
[num_hi_sig
] += carry
;
712 /* Store the quotient digit. */
717 decode (quo
, lquo
, hquo
);
720 /* If result is negative, make it so. */
722 neg_double (*lquo
, *hquo
, lquo
, hquo
);
724 /* compute trial remainder: rem = num - (quo * den) */
725 mul_double (*lquo
, *hquo
, lden_orig
, hden_orig
, lrem
, hrem
);
726 neg_double (*lrem
, *hrem
, lrem
, hrem
);
727 add_double (lnum_orig
, hnum_orig
, *lrem
, *hrem
, lrem
, hrem
);
732 case TRUNC_MOD_EXPR
: /* round toward zero */
733 case EXACT_DIV_EXPR
: /* for this one, it shouldn't matter */
737 case FLOOR_MOD_EXPR
: /* round toward negative infinity */
738 if (quo_neg
&& (*lrem
!= 0 || *hrem
!= 0)) /* ratio < 0 && rem != 0 */
741 add_double (*lquo
, *hquo
, (HOST_WIDE_INT
) -1, (HOST_WIDE_INT
) -1,
749 case CEIL_MOD_EXPR
: /* round toward positive infinity */
750 if (!quo_neg
&& (*lrem
!= 0 || *hrem
!= 0)) /* ratio > 0 && rem != 0 */
752 add_double (*lquo
, *hquo
, (HOST_WIDE_INT
) 1, (HOST_WIDE_INT
) 0,
760 case ROUND_MOD_EXPR
: /* round to closest integer */
762 unsigned HOST_WIDE_INT labs_rem
= *lrem
;
763 HOST_WIDE_INT habs_rem
= *hrem
;
764 unsigned HOST_WIDE_INT labs_den
= lden
, ltwice
;
765 HOST_WIDE_INT habs_den
= hden
, htwice
;
767 /* Get absolute values. */
769 neg_double (*lrem
, *hrem
, &labs_rem
, &habs_rem
);
771 neg_double (lden
, hden
, &labs_den
, &habs_den
);
773 /* If (2 * abs (lrem) >= abs (lden)) */
774 mul_double ((HOST_WIDE_INT
) 2, (HOST_WIDE_INT
) 0,
775 labs_rem
, habs_rem
, <wice
, &htwice
);
777 if (((unsigned HOST_WIDE_INT
) habs_den
778 < (unsigned HOST_WIDE_INT
) htwice
)
779 || (((unsigned HOST_WIDE_INT
) habs_den
780 == (unsigned HOST_WIDE_INT
) htwice
)
781 && (labs_den
< ltwice
)))
785 add_double (*lquo
, *hquo
,
786 (HOST_WIDE_INT
) -1, (HOST_WIDE_INT
) -1, lquo
, hquo
);
789 add_double (*lquo
, *hquo
, (HOST_WIDE_INT
) 1, (HOST_WIDE_INT
) 0,
801 /* Compute true remainder: rem = num - (quo * den) */
802 mul_double (*lquo
, *hquo
, lden_orig
, hden_orig
, lrem
, hrem
);
803 neg_double (*lrem
, *hrem
, lrem
, hrem
);
804 add_double (lnum_orig
, hnum_orig
, *lrem
, *hrem
, lrem
, hrem
);
808 /* Return true if built-in mathematical function specified by CODE
809 preserves the sign of it argument, i.e. -f(x) == f(-x). */
812 negate_mathfn_p (enum built_in_function code
)
836 /* Determine whether an expression T can be cheaply negated using
837 the function negate_expr. */
840 negate_expr_p (tree t
)
842 unsigned HOST_WIDE_INT val
;
849 type
= TREE_TYPE (t
);
852 switch (TREE_CODE (t
))
855 if (TREE_UNSIGNED (type
) || ! flag_trapv
)
858 /* Check that -CST will not overflow type. */
859 prec
= TYPE_PRECISION (type
);
860 if (prec
> HOST_BITS_PER_WIDE_INT
)
862 if (TREE_INT_CST_LOW (t
) != 0)
864 prec
-= HOST_BITS_PER_WIDE_INT
;
865 val
= TREE_INT_CST_HIGH (t
);
868 val
= TREE_INT_CST_LOW (t
);
869 if (prec
< HOST_BITS_PER_WIDE_INT
)
870 val
&= ((unsigned HOST_WIDE_INT
) 1 << prec
) - 1;
871 return val
!= ((unsigned HOST_WIDE_INT
) 1 << (prec
- 1));
878 return negate_expr_p (TREE_REALPART (t
))
879 && negate_expr_p (TREE_IMAGPART (t
));
882 if (FLOAT_TYPE_P (type
) && !flag_unsafe_math_optimizations
)
884 /* -(A + B) -> (-B) - A. */
885 if (negate_expr_p (TREE_OPERAND (t
, 1))
886 && reorder_operands_p (TREE_OPERAND (t
, 0),
887 TREE_OPERAND (t
, 1)))
889 /* -(A + B) -> (-A) - B. */
890 return negate_expr_p (TREE_OPERAND (t
, 0));
893 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
894 return (! FLOAT_TYPE_P (type
) || flag_unsafe_math_optimizations
)
895 && reorder_operands_p (TREE_OPERAND (t
, 0),
896 TREE_OPERAND (t
, 1));
899 if (TREE_UNSIGNED (TREE_TYPE (t
)))
905 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t
))))
906 return negate_expr_p (TREE_OPERAND (t
, 1))
907 || negate_expr_p (TREE_OPERAND (t
, 0));
911 /* Negate -((double)float) as (double)(-float). */
912 if (TREE_CODE (type
) == REAL_TYPE
)
914 tree tem
= strip_float_extensions (t
);
916 return negate_expr_p (tem
);
921 /* Negate -f(x) as f(-x). */
922 if (negate_mathfn_p (builtin_mathfn_code (t
)))
923 return negate_expr_p (TREE_VALUE (TREE_OPERAND (t
, 1)));
932 /* Given T, an expression, return the negation of T. Allow for T to be
933 null, in which case return null. */
944 type
= TREE_TYPE (t
);
947 switch (TREE_CODE (t
))
951 unsigned HOST_WIDE_INT low
;
953 int overflow
= neg_double (TREE_INT_CST_LOW (t
),
954 TREE_INT_CST_HIGH (t
),
956 tem
= build_int_2 (low
, high
);
957 TREE_TYPE (tem
) = type
;
960 | force_fit_type (tem
, overflow
&& !TREE_UNSIGNED (type
)));
961 TREE_CONSTANT_OVERFLOW (tem
)
962 = TREE_OVERFLOW (tem
) | TREE_CONSTANT_OVERFLOW (t
);
964 if (! TREE_OVERFLOW (tem
)
965 || TREE_UNSIGNED (type
)
971 tem
= build_real (type
, REAL_VALUE_NEGATE (TREE_REAL_CST (t
)));
972 /* Two's complement FP formats, such as c4x, may overflow. */
973 if (! TREE_OVERFLOW (tem
) || ! flag_trapping_math
)
974 return convert (type
, tem
);
979 tree rpart
= negate_expr (TREE_REALPART (t
));
980 tree ipart
= negate_expr (TREE_IMAGPART (t
));
982 if ((TREE_CODE (rpart
) == REAL_CST
983 && TREE_CODE (ipart
) == REAL_CST
)
984 || (TREE_CODE (rpart
) == INTEGER_CST
985 && TREE_CODE (ipart
) == INTEGER_CST
))
986 return build_complex (type
, rpart
, ipart
);
991 return convert (type
, TREE_OPERAND (t
, 0));
994 if (! FLOAT_TYPE_P (type
) || flag_unsafe_math_optimizations
)
996 /* -(A + B) -> (-B) - A. */
997 if (negate_expr_p (TREE_OPERAND (t
, 1))
998 && reorder_operands_p (TREE_OPERAND (t
, 0),
999 TREE_OPERAND (t
, 1)))
1000 return convert (type
,
1001 fold (build (MINUS_EXPR
, TREE_TYPE (t
),
1002 negate_expr (TREE_OPERAND (t
, 1)),
1003 TREE_OPERAND (t
, 0))));
1004 /* -(A + B) -> (-A) - B. */
1005 if (negate_expr_p (TREE_OPERAND (t
, 0)))
1006 return convert (type
,
1007 fold (build (MINUS_EXPR
, TREE_TYPE (t
),
1008 negate_expr (TREE_OPERAND (t
, 0)),
1009 TREE_OPERAND (t
, 1))));
1014 /* - (A - B) -> B - A */
1015 if ((! FLOAT_TYPE_P (type
) || flag_unsafe_math_optimizations
)
1016 && reorder_operands_p (TREE_OPERAND (t
, 0), TREE_OPERAND (t
, 1)))
1017 return convert (type
,
1018 fold (build (MINUS_EXPR
, TREE_TYPE (t
),
1019 TREE_OPERAND (t
, 1),
1020 TREE_OPERAND (t
, 0))));
1024 if (TREE_UNSIGNED (TREE_TYPE (t
)))
1030 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t
))))
1032 tem
= TREE_OPERAND (t
, 1);
1033 if (negate_expr_p (tem
))
1034 return convert (type
,
1035 fold (build (TREE_CODE (t
), TREE_TYPE (t
),
1036 TREE_OPERAND (t
, 0),
1037 negate_expr (tem
))));
1038 tem
= TREE_OPERAND (t
, 0);
1039 if (negate_expr_p (tem
))
1040 return convert (type
,
1041 fold (build (TREE_CODE (t
), TREE_TYPE (t
),
1043 TREE_OPERAND (t
, 1))));
1048 /* Convert -((double)float) into (double)(-float). */
1049 if (TREE_CODE (type
) == REAL_TYPE
)
1051 tem
= strip_float_extensions (t
);
1052 if (tem
!= t
&& negate_expr_p (tem
))
1053 return convert (type
, negate_expr (tem
));
1058 /* Negate -f(x) as f(-x). */
1059 if (negate_mathfn_p (builtin_mathfn_code (t
))
1060 && negate_expr_p (TREE_VALUE (TREE_OPERAND (t
, 1))))
1062 tree fndecl
, arg
, arglist
;
1064 fndecl
= get_callee_fndecl (t
);
1065 arg
= negate_expr (TREE_VALUE (TREE_OPERAND (t
, 1)));
1066 arglist
= build_tree_list (NULL_TREE
, arg
);
1067 return build_function_call_expr (fndecl
, arglist
);
1075 return convert (type
, fold (build1 (NEGATE_EXPR
, TREE_TYPE (t
), t
)));
1078 /* Split a tree IN into a constant, literal and variable parts that could be
1079 combined with CODE to make IN. "constant" means an expression with
1080 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1081 commutative arithmetic operation. Store the constant part into *CONP,
1082 the literal in *LITP and return the variable part. If a part isn't
1083 present, set it to null. If the tree does not decompose in this way,
1084 return the entire tree as the variable part and the other parts as null.
1086 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1087 case, we negate an operand that was subtracted. Except if it is a
1088 literal for which we use *MINUS_LITP instead.
1090 If NEGATE_P is true, we are negating all of IN, again except a literal
1091 for which we use *MINUS_LITP instead.
1093 If IN is itself a literal or constant, return it as appropriate.
1095 Note that we do not guarantee that any of the three values will be the
1096 same type as IN, but they will have the same signedness and mode. */
1099 split_tree (tree in
, enum tree_code code
, tree
*conp
, tree
*litp
,
1100 tree
*minus_litp
, int negate_p
)
1108 /* Strip any conversions that don't change the machine mode or signedness. */
1109 STRIP_SIGN_NOPS (in
);
1111 if (TREE_CODE (in
) == INTEGER_CST
|| TREE_CODE (in
) == REAL_CST
)
1113 else if (TREE_CODE (in
) == code
1114 || (! FLOAT_TYPE_P (TREE_TYPE (in
))
1115 /* We can associate addition and subtraction together (even
1116 though the C standard doesn't say so) for integers because
1117 the value is not affected. For reals, the value might be
1118 affected, so we can't. */
1119 && ((code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
1120 || (code
== MINUS_EXPR
&& TREE_CODE (in
) == PLUS_EXPR
))))
1122 tree op0
= TREE_OPERAND (in
, 0);
1123 tree op1
= TREE_OPERAND (in
, 1);
1124 int neg1_p
= TREE_CODE (in
) == MINUS_EXPR
;
1125 int neg_litp_p
= 0, neg_conp_p
= 0, neg_var_p
= 0;
1127 /* First see if either of the operands is a literal, then a constant. */
1128 if (TREE_CODE (op0
) == INTEGER_CST
|| TREE_CODE (op0
) == REAL_CST
)
1129 *litp
= op0
, op0
= 0;
1130 else if (TREE_CODE (op1
) == INTEGER_CST
|| TREE_CODE (op1
) == REAL_CST
)
1131 *litp
= op1
, neg_litp_p
= neg1_p
, op1
= 0;
1133 if (op0
!= 0 && TREE_CONSTANT (op0
))
1134 *conp
= op0
, op0
= 0;
1135 else if (op1
!= 0 && TREE_CONSTANT (op1
))
1136 *conp
= op1
, neg_conp_p
= neg1_p
, op1
= 0;
1138 /* If we haven't dealt with either operand, this is not a case we can
1139 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1140 if (op0
!= 0 && op1
!= 0)
1145 var
= op1
, neg_var_p
= neg1_p
;
1147 /* Now do any needed negations. */
1149 *minus_litp
= *litp
, *litp
= 0;
1151 *conp
= negate_expr (*conp
);
1153 var
= negate_expr (var
);
1155 else if (TREE_CONSTANT (in
))
1163 *minus_litp
= *litp
, *litp
= 0;
1164 else if (*minus_litp
)
1165 *litp
= *minus_litp
, *minus_litp
= 0;
1166 *conp
= negate_expr (*conp
);
1167 var
= negate_expr (var
);
1173 /* Re-associate trees split by the above function. T1 and T2 are either
1174 expressions to associate or null. Return the new expression, if any. If
1175 we build an operation, do it in TYPE and with CODE. */
1178 associate_trees (tree t1
, tree t2
, enum tree_code code
, tree type
)
1185 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1186 try to fold this since we will have infinite recursion. But do
1187 deal with any NEGATE_EXPRs. */
1188 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
1189 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
1191 if (code
== PLUS_EXPR
)
1193 if (TREE_CODE (t1
) == NEGATE_EXPR
)
1194 return build (MINUS_EXPR
, type
, convert (type
, t2
),
1195 convert (type
, TREE_OPERAND (t1
, 0)));
1196 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
1197 return build (MINUS_EXPR
, type
, convert (type
, t1
),
1198 convert (type
, TREE_OPERAND (t2
, 0)));
1200 return build (code
, type
, convert (type
, t1
), convert (type
, t2
));
1203 return fold (build (code
, type
, convert (type
, t1
), convert (type
, t2
)));
1206 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1207 to produce a new constant.
1209 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1212 int_const_binop (enum tree_code code
, tree arg1
, tree arg2
, int notrunc
)
1214 unsigned HOST_WIDE_INT int1l
, int2l
;
1215 HOST_WIDE_INT int1h
, int2h
;
1216 unsigned HOST_WIDE_INT low
;
1218 unsigned HOST_WIDE_INT garbagel
;
1219 HOST_WIDE_INT garbageh
;
1221 tree type
= TREE_TYPE (arg1
);
1222 int uns
= TREE_UNSIGNED (type
);
1224 = (TREE_CODE (type
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (type
));
1226 int no_overflow
= 0;
1228 int1l
= TREE_INT_CST_LOW (arg1
);
1229 int1h
= TREE_INT_CST_HIGH (arg1
);
1230 int2l
= TREE_INT_CST_LOW (arg2
);
1231 int2h
= TREE_INT_CST_HIGH (arg2
);
1236 low
= int1l
| int2l
, hi
= int1h
| int2h
;
1240 low
= int1l
^ int2l
, hi
= int1h
^ int2h
;
1244 low
= int1l
& int2l
, hi
= int1h
& int2h
;
1250 /* It's unclear from the C standard whether shifts can overflow.
1251 The following code ignores overflow; perhaps a C standard
1252 interpretation ruling is needed. */
1253 lshift_double (int1l
, int1h
, int2l
, TYPE_PRECISION (type
),
1261 lrotate_double (int1l
, int1h
, int2l
, TYPE_PRECISION (type
),
1266 overflow
= add_double (int1l
, int1h
, int2l
, int2h
, &low
, &hi
);
1270 neg_double (int2l
, int2h
, &low
, &hi
);
1271 add_double (int1l
, int1h
, low
, hi
, &low
, &hi
);
1272 overflow
= OVERFLOW_SUM_SIGN (hi
, int2h
, int1h
);
1276 overflow
= mul_double (int1l
, int1h
, int2l
, int2h
, &low
, &hi
);
1279 case TRUNC_DIV_EXPR
:
1280 case FLOOR_DIV_EXPR
: case CEIL_DIV_EXPR
:
1281 case EXACT_DIV_EXPR
:
1282 /* This is a shortcut for a common special case. */
1283 if (int2h
== 0 && (HOST_WIDE_INT
) int2l
> 0
1284 && ! TREE_CONSTANT_OVERFLOW (arg1
)
1285 && ! TREE_CONSTANT_OVERFLOW (arg2
)
1286 && int1h
== 0 && (HOST_WIDE_INT
) int1l
>= 0)
1288 if (code
== CEIL_DIV_EXPR
)
1291 low
= int1l
/ int2l
, hi
= 0;
1295 /* ... fall through ... */
1297 case ROUND_DIV_EXPR
:
1298 if (int2h
== 0 && int2l
== 1)
1300 low
= int1l
, hi
= int1h
;
1303 if (int1l
== int2l
&& int1h
== int2h
1304 && ! (int1l
== 0 && int1h
== 0))
1309 overflow
= div_and_round_double (code
, uns
, int1l
, int1h
, int2l
, int2h
,
1310 &low
, &hi
, &garbagel
, &garbageh
);
1313 case TRUNC_MOD_EXPR
:
1314 case FLOOR_MOD_EXPR
: case CEIL_MOD_EXPR
:
1315 /* This is a shortcut for a common special case. */
1316 if (int2h
== 0 && (HOST_WIDE_INT
) int2l
> 0
1317 && ! TREE_CONSTANT_OVERFLOW (arg1
)
1318 && ! TREE_CONSTANT_OVERFLOW (arg2
)
1319 && int1h
== 0 && (HOST_WIDE_INT
) int1l
>= 0)
1321 if (code
== CEIL_MOD_EXPR
)
1323 low
= int1l
% int2l
, hi
= 0;
1327 /* ... fall through ... */
1329 case ROUND_MOD_EXPR
:
1330 overflow
= div_and_round_double (code
, uns
,
1331 int1l
, int1h
, int2l
, int2h
,
1332 &garbagel
, &garbageh
, &low
, &hi
);
1338 low
= (((unsigned HOST_WIDE_INT
) int1h
1339 < (unsigned HOST_WIDE_INT
) int2h
)
1340 || (((unsigned HOST_WIDE_INT
) int1h
1341 == (unsigned HOST_WIDE_INT
) int2h
)
1344 low
= (int1h
< int2h
1345 || (int1h
== int2h
&& int1l
< int2l
));
1347 if (low
== (code
== MIN_EXPR
))
1348 low
= int1l
, hi
= int1h
;
1350 low
= int2l
, hi
= int2h
;
1357 /* If this is for a sizetype, can be represented as one (signed)
1358 HOST_WIDE_INT word, and doesn't overflow, use size_int since it caches
1361 && ((hi
== 0 && (HOST_WIDE_INT
) low
>= 0)
1362 || (hi
== -1 && (HOST_WIDE_INT
) low
< 0))
1363 && overflow
== 0 && ! TREE_OVERFLOW (arg1
) && ! TREE_OVERFLOW (arg2
))
1364 return size_int_type_wide (low
, type
);
1367 t
= build_int_2 (low
, hi
);
1368 TREE_TYPE (t
) = TREE_TYPE (arg1
);
1373 ? (!uns
|| is_sizetype
) && overflow
1374 : (force_fit_type (t
, (!uns
|| is_sizetype
) && overflow
)
1376 | TREE_OVERFLOW (arg1
)
1377 | TREE_OVERFLOW (arg2
));
1379 /* If we're doing a size calculation, unsigned arithmetic does overflow.
1380 So check if force_fit_type truncated the value. */
1382 && ! TREE_OVERFLOW (t
)
1383 && (TREE_INT_CST_HIGH (t
) != hi
1384 || TREE_INT_CST_LOW (t
) != low
))
1385 TREE_OVERFLOW (t
) = 1;
1387 TREE_CONSTANT_OVERFLOW (t
) = (TREE_OVERFLOW (t
)
1388 | TREE_CONSTANT_OVERFLOW (arg1
)
1389 | TREE_CONSTANT_OVERFLOW (arg2
));
1393 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1394 constant. We assume ARG1 and ARG2 have the same data type, or at least
1395 are the same kind of constant and the same machine mode.
1397 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1400 const_binop (enum tree_code code
, tree arg1
, tree arg2
, int notrunc
)
1405 if (TREE_CODE (arg1
) == INTEGER_CST
)
1406 return int_const_binop (code
, arg1
, arg2
, notrunc
);
1408 if (TREE_CODE (arg1
) == REAL_CST
)
1410 enum machine_mode mode
;
1413 REAL_VALUE_TYPE value
;
1416 d1
= TREE_REAL_CST (arg1
);
1417 d2
= TREE_REAL_CST (arg2
);
1419 type
= TREE_TYPE (arg1
);
1420 mode
= TYPE_MODE (type
);
1422 /* Don't perform operation if we honor signaling NaNs and
1423 either operand is a NaN. */
1424 if (HONOR_SNANS (mode
)
1425 && (REAL_VALUE_ISNAN (d1
) || REAL_VALUE_ISNAN (d2
)))
1428 /* Don't perform operation if it would raise a division
1429 by zero exception. */
1430 if (code
== RDIV_EXPR
1431 && REAL_VALUES_EQUAL (d2
, dconst0
)
1432 && (flag_trapping_math
|| ! MODE_HAS_INFINITIES (mode
)))
1435 /* If either operand is a NaN, just return it. Otherwise, set up
1436 for floating-point trap; we return an overflow. */
1437 if (REAL_VALUE_ISNAN (d1
))
1439 else if (REAL_VALUE_ISNAN (d2
))
1442 REAL_ARITHMETIC (value
, code
, d1
, d2
);
1444 t
= build_real (type
, real_value_truncate (mode
, value
));
1447 = (force_fit_type (t
, 0)
1448 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
));
1449 TREE_CONSTANT_OVERFLOW (t
)
1451 | TREE_CONSTANT_OVERFLOW (arg1
)
1452 | TREE_CONSTANT_OVERFLOW (arg2
);
1455 if (TREE_CODE (arg1
) == COMPLEX_CST
)
1457 tree type
= TREE_TYPE (arg1
);
1458 tree r1
= TREE_REALPART (arg1
);
1459 tree i1
= TREE_IMAGPART (arg1
);
1460 tree r2
= TREE_REALPART (arg2
);
1461 tree i2
= TREE_IMAGPART (arg2
);
1467 t
= build_complex (type
,
1468 const_binop (PLUS_EXPR
, r1
, r2
, notrunc
),
1469 const_binop (PLUS_EXPR
, i1
, i2
, notrunc
));
1473 t
= build_complex (type
,
1474 const_binop (MINUS_EXPR
, r1
, r2
, notrunc
),
1475 const_binop (MINUS_EXPR
, i1
, i2
, notrunc
));
1479 t
= build_complex (type
,
1480 const_binop (MINUS_EXPR
,
1481 const_binop (MULT_EXPR
,
1483 const_binop (MULT_EXPR
,
1486 const_binop (PLUS_EXPR
,
1487 const_binop (MULT_EXPR
,
1489 const_binop (MULT_EXPR
,
1497 = const_binop (PLUS_EXPR
,
1498 const_binop (MULT_EXPR
, r2
, r2
, notrunc
),
1499 const_binop (MULT_EXPR
, i2
, i2
, notrunc
),
1502 t
= build_complex (type
,
1504 (INTEGRAL_TYPE_P (TREE_TYPE (r1
))
1505 ? TRUNC_DIV_EXPR
: RDIV_EXPR
,
1506 const_binop (PLUS_EXPR
,
1507 const_binop (MULT_EXPR
, r1
, r2
,
1509 const_binop (MULT_EXPR
, i1
, i2
,
1512 magsquared
, notrunc
),
1514 (INTEGRAL_TYPE_P (TREE_TYPE (r1
))
1515 ? TRUNC_DIV_EXPR
: RDIV_EXPR
,
1516 const_binop (MINUS_EXPR
,
1517 const_binop (MULT_EXPR
, i1
, r2
,
1519 const_binop (MULT_EXPR
, r1
, i2
,
1522 magsquared
, notrunc
));
1534 /* These are the hash table functions for the hash table of INTEGER_CST
1535 nodes of a sizetype. */
1537 /* Return the hash code code X, an INTEGER_CST. */
1540 size_htab_hash (const void *x
)
1544 return (TREE_INT_CST_HIGH (t
) ^ TREE_INT_CST_LOW (t
)
1545 ^ htab_hash_pointer (TREE_TYPE (t
))
1546 ^ (TREE_OVERFLOW (t
) << 20));
1549 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1550 is the same as that given by *Y, which is the same. */
1553 size_htab_eq (const void *x
, const void *y
)
1558 return (TREE_INT_CST_HIGH (xt
) == TREE_INT_CST_HIGH (yt
)
1559 && TREE_INT_CST_LOW (xt
) == TREE_INT_CST_LOW (yt
)
1560 && TREE_TYPE (xt
) == TREE_TYPE (yt
)
1561 && TREE_OVERFLOW (xt
) == TREE_OVERFLOW (yt
));
1564 /* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
1565 bits are given by NUMBER and of the sizetype represented by KIND. */
1568 size_int_wide (HOST_WIDE_INT number
, enum size_type_kind kind
)
1570 return size_int_type_wide (number
, sizetype_tab
[(int) kind
]);
1573 /* Likewise, but the desired type is specified explicitly. */
1575 static GTY (()) tree new_const
;
1576 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node
)))
1580 size_int_type_wide (HOST_WIDE_INT number
, tree type
)
1586 size_htab
= htab_create_ggc (1024, size_htab_hash
, size_htab_eq
, NULL
);
1587 new_const
= make_node (INTEGER_CST
);
1590 /* Adjust NEW_CONST to be the constant we want. If it's already in the
1591 hash table, we return the value from the hash table. Otherwise, we
1592 place that in the hash table and make a new node for the next time. */
1593 TREE_INT_CST_LOW (new_const
) = number
;
1594 TREE_INT_CST_HIGH (new_const
) = number
< 0 ? -1 : 0;
1595 TREE_TYPE (new_const
) = type
;
1596 TREE_OVERFLOW (new_const
) = TREE_CONSTANT_OVERFLOW (new_const
)
1597 = force_fit_type (new_const
, 0);
1599 slot
= htab_find_slot (size_htab
, new_const
, INSERT
);
1605 new_const
= make_node (INTEGER_CST
);
1609 return (tree
) *slot
;
1612 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1613 is a tree code. The type of the result is taken from the operands.
1614 Both must be the same type integer type and it must be a size type.
1615 If the operands are constant, so is the result. */
1618 size_binop (enum tree_code code
, tree arg0
, tree arg1
)
1620 tree type
= TREE_TYPE (arg0
);
1622 if (TREE_CODE (type
) != INTEGER_TYPE
|| ! TYPE_IS_SIZETYPE (type
)
1623 || type
!= TREE_TYPE (arg1
))
1626 /* Handle the special case of two integer constants faster. */
1627 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1629 /* And some specific cases even faster than that. */
1630 if (code
== PLUS_EXPR
&& integer_zerop (arg0
))
1632 else if ((code
== MINUS_EXPR
|| code
== PLUS_EXPR
)
1633 && integer_zerop (arg1
))
1635 else if (code
== MULT_EXPR
&& integer_onep (arg0
))
1638 /* Handle general case of two integer constants. */
1639 return int_const_binop (code
, arg0
, arg1
, 0);
1642 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1643 return error_mark_node
;
1645 return fold (build (code
, type
, arg0
, arg1
));
1648 /* Given two values, either both of sizetype or both of bitsizetype,
1649 compute the difference between the two values. Return the value
1650 in signed type corresponding to the type of the operands. */
1653 size_diffop (tree arg0
, tree arg1
)
1655 tree type
= TREE_TYPE (arg0
);
1658 if (TREE_CODE (type
) != INTEGER_TYPE
|| ! TYPE_IS_SIZETYPE (type
)
1659 || type
!= TREE_TYPE (arg1
))
1662 /* If the type is already signed, just do the simple thing. */
1663 if (! TREE_UNSIGNED (type
))
1664 return size_binop (MINUS_EXPR
, arg0
, arg1
);
1666 ctype
= (type
== bitsizetype
|| type
== ubitsizetype
1667 ? sbitsizetype
: ssizetype
);
1669 /* If either operand is not a constant, do the conversions to the signed
1670 type and subtract. The hardware will do the right thing with any
1671 overflow in the subtraction. */
1672 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
1673 return size_binop (MINUS_EXPR
, convert (ctype
, arg0
),
1674 convert (ctype
, arg1
));
1676 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1677 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1678 overflow) and negate (which can't either). Special-case a result
1679 of zero while we're here. */
1680 if (tree_int_cst_equal (arg0
, arg1
))
1681 return convert (ctype
, integer_zero_node
);
1682 else if (tree_int_cst_lt (arg1
, arg0
))
1683 return convert (ctype
, size_binop (MINUS_EXPR
, arg0
, arg1
));
1685 return size_binop (MINUS_EXPR
, convert (ctype
, integer_zero_node
),
1686 convert (ctype
, size_binop (MINUS_EXPR
, arg1
, arg0
)));
1690 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1691 type TYPE. If no simplification can be done return NULL_TREE. */
1694 fold_convert_const (enum tree_code code
, tree type
, tree arg1
)
1699 if (TREE_TYPE (arg1
) == type
)
1702 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
))
1704 if (TREE_CODE (arg1
) == INTEGER_CST
)
1706 /* If we would build a constant wider than GCC supports,
1707 leave the conversion unfolded. */
1708 if (TYPE_PRECISION (type
) > 2 * HOST_BITS_PER_WIDE_INT
)
1711 /* If we are trying to make a sizetype for a small integer, use
1712 size_int to pick up cached types to reduce duplicate nodes. */
1713 if (TREE_CODE (type
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (type
)
1714 && !TREE_CONSTANT_OVERFLOW (arg1
)
1715 && compare_tree_int (arg1
, 10000) < 0)
1716 return size_int_type_wide (TREE_INT_CST_LOW (arg1
), type
);
1718 /* Given an integer constant, make new constant with new type,
1719 appropriately sign-extended or truncated. */
1720 t
= build_int_2 (TREE_INT_CST_LOW (arg1
),
1721 TREE_INT_CST_HIGH (arg1
));
1722 TREE_TYPE (t
) = type
;
1723 /* Indicate an overflow if (1) ARG1 already overflowed,
1724 or (2) force_fit_type indicates an overflow.
1725 Tell force_fit_type that an overflow has already occurred
1726 if ARG1 is a too-large unsigned value and T is signed.
1727 But don't indicate an overflow if converting a pointer. */
1729 = ((force_fit_type (t
,
1730 (TREE_INT_CST_HIGH (arg1
) < 0
1731 && (TREE_UNSIGNED (type
)
1732 < TREE_UNSIGNED (TREE_TYPE (arg1
)))))
1733 && ! POINTER_TYPE_P (TREE_TYPE (arg1
)))
1734 || TREE_OVERFLOW (arg1
));
1735 TREE_CONSTANT_OVERFLOW (t
)
1736 = TREE_OVERFLOW (t
) | TREE_CONSTANT_OVERFLOW (arg1
);
1739 else if (TREE_CODE (arg1
) == REAL_CST
)
1741 /* The following code implements the floating point to integer
1742 conversion rules required by the Java Language Specification,
1743 that IEEE NaNs are mapped to zero and values that overflow
1744 the target precision saturate, i.e. values greater than
1745 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1746 are mapped to INT_MIN. These semantics are allowed by the
1747 C and C++ standards that simply state that the behavior of
1748 FP-to-integer conversion is unspecified upon overflow. */
1750 HOST_WIDE_INT high
, low
;
1753 REAL_VALUE_TYPE x
= TREE_REAL_CST (arg1
);
1757 case FIX_TRUNC_EXPR
:
1758 real_trunc (&r
, VOIDmode
, &x
);
1762 real_ceil (&r
, VOIDmode
, &x
);
1765 case FIX_FLOOR_EXPR
:
1766 real_floor (&r
, VOIDmode
, &x
);
1773 /* If R is NaN, return zero and show we have an overflow. */
1774 if (REAL_VALUE_ISNAN (r
))
1781 /* See if R is less than the lower bound or greater than the
1786 tree lt
= TYPE_MIN_VALUE (type
);
1787 REAL_VALUE_TYPE l
= real_value_from_int_cst (NULL_TREE
, lt
);
1788 if (REAL_VALUES_LESS (r
, l
))
1791 high
= TREE_INT_CST_HIGH (lt
);
1792 low
= TREE_INT_CST_LOW (lt
);
1798 tree ut
= TYPE_MAX_VALUE (type
);
1801 REAL_VALUE_TYPE u
= real_value_from_int_cst (NULL_TREE
, ut
);
1802 if (REAL_VALUES_LESS (u
, r
))
1805 high
= TREE_INT_CST_HIGH (ut
);
1806 low
= TREE_INT_CST_LOW (ut
);
1812 REAL_VALUE_TO_INT (&low
, &high
, r
);
1814 t
= build_int_2 (low
, high
);
1815 TREE_TYPE (t
) = type
;
1817 = TREE_OVERFLOW (arg1
) | force_fit_type (t
, overflow
);
1818 TREE_CONSTANT_OVERFLOW (t
)
1819 = TREE_OVERFLOW (t
) | TREE_CONSTANT_OVERFLOW (arg1
);
1823 else if (TREE_CODE (type
) == REAL_TYPE
)
1825 if (TREE_CODE (arg1
) == INTEGER_CST
)
1826 return build_real_from_int_cst (type
, arg1
);
1827 if (TREE_CODE (arg1
) == REAL_CST
)
1829 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
)))
1831 /* We make a copy of ARG1 so that we don't modify an
1832 existing constant tree. */
1833 t
= copy_node (arg1
);
1834 TREE_TYPE (t
) = type
;
1838 t
= build_real (type
,
1839 real_value_truncate (TYPE_MODE (type
),
1840 TREE_REAL_CST (arg1
)));
1843 = TREE_OVERFLOW (arg1
) | force_fit_type (t
, 0);
1844 TREE_CONSTANT_OVERFLOW (t
)
1845 = TREE_OVERFLOW (t
) | TREE_CONSTANT_OVERFLOW (arg1
);
1852 /* Return an expr equal to X but certainly not valid as an lvalue. */
1859 /* These things are certainly not lvalues. */
1860 if (TREE_CODE (x
) == NON_LVALUE_EXPR
1861 || TREE_CODE (x
) == INTEGER_CST
1862 || TREE_CODE (x
) == REAL_CST
1863 || TREE_CODE (x
) == STRING_CST
1864 || TREE_CODE (x
) == ADDR_EXPR
)
1867 result
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
1868 TREE_CONSTANT (result
) = TREE_CONSTANT (x
);
1872 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
1873 Zero means allow extended lvalues. */
1875 int pedantic_lvalues
;
1877 /* When pedantic, return an expr equal to X but certainly not valid as a
1878 pedantic lvalue. Otherwise, return X. */
1881 pedantic_non_lvalue (tree x
)
1883 if (pedantic_lvalues
)
1884 return non_lvalue (x
);
1889 /* Given a tree comparison code, return the code that is the logical inverse
1890 of the given code. It is not safe to do this for floating-point
1891 comparisons, except for NE_EXPR and EQ_EXPR. */
1893 static enum tree_code
1894 invert_tree_comparison (enum tree_code code
)
1915 /* Similar, but return the comparison that results if the operands are
1916 swapped. This is safe for floating-point. */
1918 static enum tree_code
1919 swap_tree_comparison (enum tree_code code
)
1940 /* Convert a comparison tree code from an enum tree_code representation
1941 into a compcode bit-based encoding. This function is the inverse of
1942 compcode_to_comparison. */
1945 comparison_to_compcode (enum tree_code code
)
1966 /* Convert a compcode bit-based encoding of a comparison operator back
1967 to GCC's enum tree_code representation. This function is the
1968 inverse of comparison_to_compcode. */
1970 static enum tree_code
1971 compcode_to_comparison (int code
)
1992 /* Return nonzero if CODE is a tree code that represents a truth value. */
1995 truth_value_p (enum tree_code code
)
1997 return (TREE_CODE_CLASS (code
) == '<'
1998 || code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
1999 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
2000 || code
== TRUTH_XOR_EXPR
|| code
== TRUTH_NOT_EXPR
);
2003 /* Return nonzero if two operands (typically of the same tree node)
2004 are necessarily equal. If either argument has side-effects this
2005 function returns zero.
2007 If ONLY_CONST is nonzero, only return nonzero for constants.
2008 This function tests whether the operands are indistinguishable;
2009 it does not test whether they are equal using C's == operation.
2010 The distinction is important for IEEE floating point, because
2011 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2012 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2014 If ONLY_CONST is zero, a VAR_DECL is considered equal to itself
2015 even though it may hold multiple values during a function.
2016 This is because a GCC tree node guarantees that nothing else is
2017 executed between the evaluation of its "operands" (which may often
2018 be evaluated in arbitrary order). Hence if the operands themselves
2019 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2020 same value in each operand/subexpression. Hence a zero value for
2021 ONLY_CONST assumes isochronic (or instantaneous) tree equivalence.
2022 If comparing arbitrary expression trees, such as from different
2023 statements, ONLY_CONST must usually be nonzero. */
2026 operand_equal_p (tree arg0
, tree arg1
, int only_const
)
2030 /* If both types don't have the same signedness, then we can't consider
2031 them equal. We must check this before the STRIP_NOPS calls
2032 because they may change the signedness of the arguments. */
2033 if (TREE_UNSIGNED (TREE_TYPE (arg0
)) != TREE_UNSIGNED (TREE_TYPE (arg1
)))
2039 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
2040 /* This is needed for conversions and for COMPONENT_REF.
2041 Might as well play it safe and always test this. */
2042 || TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
2043 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
2044 || TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
)))
2047 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2048 We don't care about side effects in that case because the SAVE_EXPR
2049 takes care of that for us. In all other cases, two expressions are
2050 equal if they have no side effects. If we have two identical
2051 expressions with side effects that should be treated the same due
2052 to the only side effects being identical SAVE_EXPR's, that will
2053 be detected in the recursive calls below. */
2054 if (arg0
== arg1
&& ! only_const
2055 && (TREE_CODE (arg0
) == SAVE_EXPR
2056 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
2059 /* Next handle constant cases, those for which we can return 1 even
2060 if ONLY_CONST is set. */
2061 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
2062 switch (TREE_CODE (arg0
))
2065 return (! TREE_CONSTANT_OVERFLOW (arg0
)
2066 && ! TREE_CONSTANT_OVERFLOW (arg1
)
2067 && tree_int_cst_equal (arg0
, arg1
));
2070 return (! TREE_CONSTANT_OVERFLOW (arg0
)
2071 && ! TREE_CONSTANT_OVERFLOW (arg1
)
2072 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0
),
2073 TREE_REAL_CST (arg1
)));
2079 if (TREE_CONSTANT_OVERFLOW (arg0
)
2080 || TREE_CONSTANT_OVERFLOW (arg1
))
2083 v1
= TREE_VECTOR_CST_ELTS (arg0
);
2084 v2
= TREE_VECTOR_CST_ELTS (arg1
);
2087 if (!operand_equal_p (v1
, v2
, only_const
))
2089 v1
= TREE_CHAIN (v1
);
2090 v2
= TREE_CHAIN (v2
);
2097 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
2099 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
2103 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
2104 && ! memcmp (TREE_STRING_POINTER (arg0
),
2105 TREE_STRING_POINTER (arg1
),
2106 TREE_STRING_LENGTH (arg0
)));
2109 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
2118 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
2121 /* Two conversions are equal only if signedness and modes match. */
2122 if ((TREE_CODE (arg0
) == NOP_EXPR
|| TREE_CODE (arg0
) == CONVERT_EXPR
)
2123 && (TREE_UNSIGNED (TREE_TYPE (arg0
))
2124 != TREE_UNSIGNED (TREE_TYPE (arg1
))))
2127 return operand_equal_p (TREE_OPERAND (arg0
, 0),
2128 TREE_OPERAND (arg1
, 0), 0);
2132 if (operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0), 0)
2133 && operand_equal_p (TREE_OPERAND (arg0
, 1), TREE_OPERAND (arg1
, 1),
2137 /* For commutative ops, allow the other order. */
2138 return (commutative_tree_code (TREE_CODE (arg0
))
2139 && operand_equal_p (TREE_OPERAND (arg0
, 0),
2140 TREE_OPERAND (arg1
, 1), 0)
2141 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2142 TREE_OPERAND (arg1
, 0), 0));
2145 /* If either of the pointer (or reference) expressions we are
2146 dereferencing contain a side effect, these cannot be equal. */
2147 if (TREE_SIDE_EFFECTS (arg0
)
2148 || TREE_SIDE_EFFECTS (arg1
))
2151 switch (TREE_CODE (arg0
))
2154 return operand_equal_p (TREE_OPERAND (arg0
, 0),
2155 TREE_OPERAND (arg1
, 0), 0);
2159 case ARRAY_RANGE_REF
:
2160 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
2161 TREE_OPERAND (arg1
, 0), 0)
2162 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2163 TREE_OPERAND (arg1
, 1), 0));
2166 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
2167 TREE_OPERAND (arg1
, 0), 0)
2168 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2169 TREE_OPERAND (arg1
, 1), 0)
2170 && operand_equal_p (TREE_OPERAND (arg0
, 2),
2171 TREE_OPERAND (arg1
, 2), 0));
2177 switch (TREE_CODE (arg0
))
2180 case TRUTH_NOT_EXPR
:
2181 return operand_equal_p (TREE_OPERAND (arg0
, 0),
2182 TREE_OPERAND (arg1
, 0), 0);
2185 return rtx_equal_p (RTL_EXPR_RTL (arg0
), RTL_EXPR_RTL (arg1
));
2188 /* If the CALL_EXPRs call different functions, then they
2189 clearly can not be equal. */
2190 if (! operand_equal_p (TREE_OPERAND (arg0
, 0),
2191 TREE_OPERAND (arg1
, 0), 0))
2194 /* Only consider const functions equivalent. */
2195 fndecl
= get_callee_fndecl (arg0
);
2196 if (fndecl
== NULL_TREE
2197 || ! (flags_from_decl_or_type (fndecl
) & ECF_CONST
))
2200 /* Now see if all the arguments are the same. operand_equal_p
2201 does not handle TREE_LIST, so we walk the operands here
2202 feeding them to operand_equal_p. */
2203 arg0
= TREE_OPERAND (arg0
, 1);
2204 arg1
= TREE_OPERAND (arg1
, 1);
2205 while (arg0
&& arg1
)
2207 if (! operand_equal_p (TREE_VALUE (arg0
), TREE_VALUE (arg1
), 0))
2210 arg0
= TREE_CHAIN (arg0
);
2211 arg1
= TREE_CHAIN (arg1
);
2214 /* If we get here and both argument lists are exhausted
2215 then the CALL_EXPRs are equal. */
2216 return ! (arg0
|| arg1
);
2223 /* Consider __builtin_sqrt equal to sqrt. */
2224 return TREE_CODE (arg0
) == FUNCTION_DECL
2225 && DECL_BUILT_IN (arg0
) && DECL_BUILT_IN (arg1
)
2226 && DECL_BUILT_IN_CLASS (arg0
) == DECL_BUILT_IN_CLASS (arg1
)
2227 && DECL_FUNCTION_CODE (arg0
) == DECL_FUNCTION_CODE (arg1
);
2234 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2235 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2237 When in doubt, return 0. */
2240 operand_equal_for_comparison_p (tree arg0
, tree arg1
, tree other
)
2242 int unsignedp1
, unsignedpo
;
2243 tree primarg0
, primarg1
, primother
;
2244 unsigned int correct_width
;
2246 if (operand_equal_p (arg0
, arg1
, 0))
2249 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
2250 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
2253 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2254 and see if the inner values are the same. This removes any
2255 signedness comparison, which doesn't matter here. */
2256 primarg0
= arg0
, primarg1
= arg1
;
2257 STRIP_NOPS (primarg0
);
2258 STRIP_NOPS (primarg1
);
2259 if (operand_equal_p (primarg0
, primarg1
, 0))
2262 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2263 actual comparison operand, ARG0.
2265 First throw away any conversions to wider types
2266 already present in the operands. */
2268 primarg1
= get_narrower (arg1
, &unsignedp1
);
2269 primother
= get_narrower (other
, &unsignedpo
);
2271 correct_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
2272 if (unsignedp1
== unsignedpo
2273 && TYPE_PRECISION (TREE_TYPE (primarg1
)) < correct_width
2274 && TYPE_PRECISION (TREE_TYPE (primother
)) < correct_width
)
2276 tree type
= TREE_TYPE (arg0
);
2278 /* Make sure shorter operand is extended the right way
2279 to match the longer operand. */
2280 primarg1
= convert ((*lang_hooks
.types
.signed_or_unsigned_type
)
2281 (unsignedp1
, TREE_TYPE (primarg1
)), primarg1
);
2283 if (operand_equal_p (arg0
, convert (type
, primarg1
), 0))
2290 /* See if ARG is an expression that is either a comparison or is performing
2291 arithmetic on comparisons. The comparisons must only be comparing
2292 two different values, which will be stored in *CVAL1 and *CVAL2; if
2293 they are nonzero it means that some operands have already been found.
2294 No variables may be used anywhere else in the expression except in the
2295 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2296 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2298 If this is true, return 1. Otherwise, return zero. */
2301 twoval_comparison_p (tree arg
, tree
*cval1
, tree
*cval2
, int *save_p
)
2303 enum tree_code code
= TREE_CODE (arg
);
2304 char class = TREE_CODE_CLASS (code
);
2306 /* We can handle some of the 'e' cases here. */
2307 if (class == 'e' && code
== TRUTH_NOT_EXPR
)
2309 else if (class == 'e'
2310 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
2311 || code
== COMPOUND_EXPR
))
2314 else if (class == 'e' && code
== SAVE_EXPR
&& SAVE_EXPR_RTL (arg
) == 0
2315 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
2317 /* If we've already found a CVAL1 or CVAL2, this expression is
2318 two complex to handle. */
2319 if (*cval1
|| *cval2
)
2329 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
);
2332 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
)
2333 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
2334 cval1
, cval2
, save_p
));
2340 if (code
== COND_EXPR
)
2341 return (twoval_comparison_p (TREE_OPERAND (arg
, 0),
2342 cval1
, cval2
, save_p
)
2343 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
2344 cval1
, cval2
, save_p
)
2345 && twoval_comparison_p (TREE_OPERAND (arg
, 2),
2346 cval1
, cval2
, save_p
));
2350 /* First see if we can handle the first operand, then the second. For
2351 the second operand, we know *CVAL1 can't be zero. It must be that
2352 one side of the comparison is each of the values; test for the
2353 case where this isn't true by failing if the two operands
2356 if (operand_equal_p (TREE_OPERAND (arg
, 0),
2357 TREE_OPERAND (arg
, 1), 0))
2361 *cval1
= TREE_OPERAND (arg
, 0);
2362 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
2364 else if (*cval2
== 0)
2365 *cval2
= TREE_OPERAND (arg
, 0);
2366 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
2371 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
2373 else if (*cval2
== 0)
2374 *cval2
= TREE_OPERAND (arg
, 1);
2375 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
2387 /* ARG is a tree that is known to contain just arithmetic operations and
2388 comparisons. Evaluate the operations in the tree substituting NEW0 for
2389 any occurrence of OLD0 as an operand of a comparison and likewise for
2393 eval_subst (tree arg
, tree old0
, tree new0
, tree old1
, tree new1
)
2395 tree type
= TREE_TYPE (arg
);
2396 enum tree_code code
= TREE_CODE (arg
);
2397 char class = TREE_CODE_CLASS (code
);
2399 /* We can handle some of the 'e' cases here. */
2400 if (class == 'e' && code
== TRUTH_NOT_EXPR
)
2402 else if (class == 'e'
2403 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2409 return fold (build1 (code
, type
,
2410 eval_subst (TREE_OPERAND (arg
, 0),
2411 old0
, new0
, old1
, new1
)));
2414 return fold (build (code
, type
,
2415 eval_subst (TREE_OPERAND (arg
, 0),
2416 old0
, new0
, old1
, new1
),
2417 eval_subst (TREE_OPERAND (arg
, 1),
2418 old0
, new0
, old1
, new1
)));
2424 return eval_subst (TREE_OPERAND (arg
, 0), old0
, new0
, old1
, new1
);
2427 return eval_subst (TREE_OPERAND (arg
, 1), old0
, new0
, old1
, new1
);
2430 return fold (build (code
, type
,
2431 eval_subst (TREE_OPERAND (arg
, 0),
2432 old0
, new0
, old1
, new1
),
2433 eval_subst (TREE_OPERAND (arg
, 1),
2434 old0
, new0
, old1
, new1
),
2435 eval_subst (TREE_OPERAND (arg
, 2),
2436 old0
, new0
, old1
, new1
)));
2440 /* Fall through - ??? */
2444 tree arg0
= TREE_OPERAND (arg
, 0);
2445 tree arg1
= TREE_OPERAND (arg
, 1);
2447 /* We need to check both for exact equality and tree equality. The
2448 former will be true if the operand has a side-effect. In that
2449 case, we know the operand occurred exactly once. */
2451 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
2453 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
2456 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
2458 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
2461 return fold (build (code
, type
, arg0
, arg1
));
2469 /* Return a tree for the case when the result of an expression is RESULT
2470 converted to TYPE and OMITTED was previously an operand of the expression
2471 but is now not needed (e.g., we folded OMITTED * 0).
2473 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2474 the conversion of RESULT to TYPE. */
2477 omit_one_operand (tree type
, tree result
, tree omitted
)
2479 tree t
= convert (type
, result
);
2481 if (TREE_SIDE_EFFECTS (omitted
))
2482 return build (COMPOUND_EXPR
, type
, omitted
, t
);
2484 return non_lvalue (t
);
2487 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2490 pedantic_omit_one_operand (tree type
, tree result
, tree omitted
)
2492 tree t
= convert (type
, result
);
2494 if (TREE_SIDE_EFFECTS (omitted
))
2495 return build (COMPOUND_EXPR
, type
, omitted
, t
);
2497 return pedantic_non_lvalue (t
);
2500 /* Return a simplified tree node for the truth-negation of ARG. This
2501 never alters ARG itself. We assume that ARG is an operation that
2502 returns a truth value (0 or 1). */
2505 invert_truthvalue (tree arg
)
2507 tree type
= TREE_TYPE (arg
);
2508 enum tree_code code
= TREE_CODE (arg
);
2510 if (code
== ERROR_MARK
)
2513 /* If this is a comparison, we can simply invert it, except for
2514 floating-point non-equality comparisons, in which case we just
2515 enclose a TRUTH_NOT_EXPR around what we have. */
2517 if (TREE_CODE_CLASS (code
) == '<')
2519 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg
, 0)))
2520 && !flag_unsafe_math_optimizations
2523 return build1 (TRUTH_NOT_EXPR
, type
, arg
);
2525 return build (invert_tree_comparison (code
), type
,
2526 TREE_OPERAND (arg
, 0), TREE_OPERAND (arg
, 1));
2532 return convert (type
, build_int_2 (integer_zerop (arg
), 0));
2534 case TRUTH_AND_EXPR
:
2535 return build (TRUTH_OR_EXPR
, type
,
2536 invert_truthvalue (TREE_OPERAND (arg
, 0)),
2537 invert_truthvalue (TREE_OPERAND (arg
, 1)));
2540 return build (TRUTH_AND_EXPR
, type
,
2541 invert_truthvalue (TREE_OPERAND (arg
, 0)),
2542 invert_truthvalue (TREE_OPERAND (arg
, 1)));
2544 case TRUTH_XOR_EXPR
:
2545 /* Here we can invert either operand. We invert the first operand
2546 unless the second operand is a TRUTH_NOT_EXPR in which case our
2547 result is the XOR of the first operand with the inside of the
2548 negation of the second operand. */
2550 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
2551 return build (TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
2552 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
2554 return build (TRUTH_XOR_EXPR
, type
,
2555 invert_truthvalue (TREE_OPERAND (arg
, 0)),
2556 TREE_OPERAND (arg
, 1));
2558 case TRUTH_ANDIF_EXPR
:
2559 return build (TRUTH_ORIF_EXPR
, type
,
2560 invert_truthvalue (TREE_OPERAND (arg
, 0)),
2561 invert_truthvalue (TREE_OPERAND (arg
, 1)));
2563 case TRUTH_ORIF_EXPR
:
2564 return build (TRUTH_ANDIF_EXPR
, type
,
2565 invert_truthvalue (TREE_OPERAND (arg
, 0)),
2566 invert_truthvalue (TREE_OPERAND (arg
, 1)));
2568 case TRUTH_NOT_EXPR
:
2569 return TREE_OPERAND (arg
, 0);
2572 return build (COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
2573 invert_truthvalue (TREE_OPERAND (arg
, 1)),
2574 invert_truthvalue (TREE_OPERAND (arg
, 2)));
2577 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg
, 0),
2578 invert_truthvalue (TREE_OPERAND (arg
, 1)));
2580 case WITH_RECORD_EXPR
:
2581 return build (WITH_RECORD_EXPR
, type
,
2582 invert_truthvalue (TREE_OPERAND (arg
, 0)),
2583 TREE_OPERAND (arg
, 1));
2585 case NON_LVALUE_EXPR
:
2586 return invert_truthvalue (TREE_OPERAND (arg
, 0));
2591 return build1 (TREE_CODE (arg
), type
,
2592 invert_truthvalue (TREE_OPERAND (arg
, 0)));
2595 if (!integer_onep (TREE_OPERAND (arg
, 1)))
2597 return build (EQ_EXPR
, type
, arg
, convert (type
, integer_zero_node
));
2600 return build1 (TRUTH_NOT_EXPR
, type
, arg
);
2602 case CLEANUP_POINT_EXPR
:
2603 return build1 (CLEANUP_POINT_EXPR
, type
,
2604 invert_truthvalue (TREE_OPERAND (arg
, 0)));
2609 if (TREE_CODE (TREE_TYPE (arg
)) != BOOLEAN_TYPE
)
2611 return build1 (TRUTH_NOT_EXPR
, type
, arg
);
2614 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2615 operands are another bit-wise operation with a common input. If so,
2616 distribute the bit operations to save an operation and possibly two if
2617 constants are involved. For example, convert
2618 (A | B) & (A | C) into A | (B & C)
2619 Further simplification will occur if B and C are constants.
2621 If this optimization cannot be done, 0 will be returned. */
2624 distribute_bit_expr (enum tree_code code
, tree type
, tree arg0
, tree arg1
)
2629 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
2630 || TREE_CODE (arg0
) == code
2631 || (TREE_CODE (arg0
) != BIT_AND_EXPR
2632 && TREE_CODE (arg0
) != BIT_IOR_EXPR
))
2635 if (operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0), 0))
2637 common
= TREE_OPERAND (arg0
, 0);
2638 left
= TREE_OPERAND (arg0
, 1);
2639 right
= TREE_OPERAND (arg1
, 1);
2641 else if (operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 1), 0))
2643 common
= TREE_OPERAND (arg0
, 0);
2644 left
= TREE_OPERAND (arg0
, 1);
2645 right
= TREE_OPERAND (arg1
, 0);
2647 else if (operand_equal_p (TREE_OPERAND (arg0
, 1), TREE_OPERAND (arg1
, 0), 0))
2649 common
= TREE_OPERAND (arg0
, 1);
2650 left
= TREE_OPERAND (arg0
, 0);
2651 right
= TREE_OPERAND (arg1
, 1);
2653 else if (operand_equal_p (TREE_OPERAND (arg0
, 1), TREE_OPERAND (arg1
, 1), 0))
2655 common
= TREE_OPERAND (arg0
, 1);
2656 left
= TREE_OPERAND (arg0
, 0);
2657 right
= TREE_OPERAND (arg1
, 0);
2662 return fold (build (TREE_CODE (arg0
), type
, common
,
2663 fold (build (code
, type
, left
, right
))));
2666 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2667 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
2670 make_bit_field_ref (tree inner
, tree type
, int bitsize
, int bitpos
,
2673 tree result
= build (BIT_FIELD_REF
, type
, inner
,
2674 size_int (bitsize
), bitsize_int (bitpos
));
2676 TREE_UNSIGNED (result
) = unsignedp
;
2681 /* Optimize a bit-field compare.
2683 There are two cases: First is a compare against a constant and the
2684 second is a comparison of two items where the fields are at the same
2685 bit position relative to the start of a chunk (byte, halfword, word)
2686 large enough to contain it. In these cases we can avoid the shift
2687 implicit in bitfield extractions.
2689 For constants, we emit a compare of the shifted constant with the
2690 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2691 compared. For two fields at the same position, we do the ANDs with the
2692 similar mask and compare the result of the ANDs.
2694 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2695 COMPARE_TYPE is the type of the comparison, and LHS and RHS
2696 are the left and right operands of the comparison, respectively.
2698 If the optimization described above can be done, we return the resulting
2699 tree. Otherwise we return zero. */
2702 optimize_bit_field_compare (enum tree_code code
, tree compare_type
,
2705 HOST_WIDE_INT lbitpos
, lbitsize
, rbitpos
, rbitsize
, nbitpos
, nbitsize
;
2706 tree type
= TREE_TYPE (lhs
);
2707 tree signed_type
, unsigned_type
;
2708 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
2709 enum machine_mode lmode
, rmode
, nmode
;
2710 int lunsignedp
, runsignedp
;
2711 int lvolatilep
= 0, rvolatilep
= 0;
2712 tree linner
, rinner
= NULL_TREE
;
2716 /* Get all the information about the extractions being done. If the bit size
2717 if the same as the size of the underlying object, we aren't doing an
2718 extraction at all and so can do nothing. We also don't want to
2719 do anything if the inner expression is a PLACEHOLDER_EXPR since we
2720 then will no longer be able to replace it. */
2721 linner
= get_inner_reference (lhs
, &lbitsize
, &lbitpos
, &offset
, &lmode
,
2722 &lunsignedp
, &lvolatilep
);
2723 if (linner
== lhs
|| lbitsize
== GET_MODE_BITSIZE (lmode
) || lbitsize
< 0
2724 || offset
!= 0 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
)
2729 /* If this is not a constant, we can only do something if bit positions,
2730 sizes, and signedness are the same. */
2731 rinner
= get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
2732 &runsignedp
, &rvolatilep
);
2734 if (rinner
== rhs
|| lbitpos
!= rbitpos
|| lbitsize
!= rbitsize
2735 || lunsignedp
!= runsignedp
|| offset
!= 0
2736 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
)
2740 /* See if we can find a mode to refer to this field. We should be able to,
2741 but fail if we can't. */
2742 nmode
= get_best_mode (lbitsize
, lbitpos
,
2743 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
2744 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
2745 TYPE_ALIGN (TREE_TYPE (rinner
))),
2746 word_mode
, lvolatilep
|| rvolatilep
);
2747 if (nmode
== VOIDmode
)
2750 /* Set signed and unsigned types of the precision of this mode for the
2752 signed_type
= (*lang_hooks
.types
.type_for_mode
) (nmode
, 0);
2753 unsigned_type
= (*lang_hooks
.types
.type_for_mode
) (nmode
, 1);
2755 /* Compute the bit position and size for the new reference and our offset
2756 within it. If the new reference is the same size as the original, we
2757 won't optimize anything, so return zero. */
2758 nbitsize
= GET_MODE_BITSIZE (nmode
);
2759 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
2761 if (nbitsize
== lbitsize
)
2764 if (BYTES_BIG_ENDIAN
)
2765 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
2767 /* Make the mask to be used against the extracted field. */
2768 mask
= build_int_2 (~0, ~0);
2769 TREE_TYPE (mask
) = unsigned_type
;
2770 force_fit_type (mask
, 0);
2771 mask
= convert (unsigned_type
, mask
);
2772 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
), 0);
2773 mask
= const_binop (RSHIFT_EXPR
, mask
,
2774 size_int (nbitsize
- lbitsize
- lbitpos
), 0);
2777 /* If not comparing with constant, just rework the comparison
2779 return build (code
, compare_type
,
2780 build (BIT_AND_EXPR
, unsigned_type
,
2781 make_bit_field_ref (linner
, unsigned_type
,
2782 nbitsize
, nbitpos
, 1),
2784 build (BIT_AND_EXPR
, unsigned_type
,
2785 make_bit_field_ref (rinner
, unsigned_type
,
2786 nbitsize
, nbitpos
, 1),
2789 /* Otherwise, we are handling the constant case. See if the constant is too
2790 big for the field. Warn and return a tree of for 0 (false) if so. We do
2791 this not only for its own sake, but to avoid having to test for this
2792 error case below. If we didn't, we might generate wrong code.
2794 For unsigned fields, the constant shifted right by the field length should
2795 be all zero. For signed fields, the high-order bits should agree with
2800 if (! integer_zerop (const_binop (RSHIFT_EXPR
,
2801 convert (unsigned_type
, rhs
),
2802 size_int (lbitsize
), 0)))
2804 warning ("comparison is always %d due to width of bit-field",
2806 return convert (compare_type
,
2808 ? integer_one_node
: integer_zero_node
));
2813 tree tem
= const_binop (RSHIFT_EXPR
, convert (signed_type
, rhs
),
2814 size_int (lbitsize
- 1), 0);
2815 if (! integer_zerop (tem
) && ! integer_all_onesp (tem
))
2817 warning ("comparison is always %d due to width of bit-field",
2819 return convert (compare_type
,
2821 ? integer_one_node
: integer_zero_node
));
2825 /* Single-bit compares should always be against zero. */
2826 if (lbitsize
== 1 && ! integer_zerop (rhs
))
2828 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
2829 rhs
= convert (type
, integer_zero_node
);
2832 /* Make a new bitfield reference, shift the constant over the
2833 appropriate number of bits and mask it with the computed mask
2834 (in case this was a signed field). If we changed it, make a new one. */
2835 lhs
= make_bit_field_ref (linner
, unsigned_type
, nbitsize
, nbitpos
, 1);
2838 TREE_SIDE_EFFECTS (lhs
) = 1;
2839 TREE_THIS_VOLATILE (lhs
) = 1;
2842 rhs
= fold (const_binop (BIT_AND_EXPR
,
2843 const_binop (LSHIFT_EXPR
,
2844 convert (unsigned_type
, rhs
),
2845 size_int (lbitpos
), 0),
2848 return build (code
, compare_type
,
2849 build (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
),
2853 /* Subroutine for fold_truthop: decode a field reference.
2855 If EXP is a comparison reference, we return the innermost reference.
2857 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
2858 set to the starting bit number.
2860 If the innermost field can be completely contained in a mode-sized
2861 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
2863 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
2864 otherwise it is not changed.
2866 *PUNSIGNEDP is set to the signedness of the field.
2868 *PMASK is set to the mask used. This is either contained in a
2869 BIT_AND_EXPR or derived from the width of the field.
2871 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
2873 Return 0 if this is not a component reference or is one that we can't
2874 do anything with. */
2877 decode_field_reference (tree exp
, HOST_WIDE_INT
*pbitsize
,
2878 HOST_WIDE_INT
*pbitpos
, enum machine_mode
*pmode
,
2879 int *punsignedp
, int *pvolatilep
,
2880 tree
*pmask
, tree
*pand_mask
)
2882 tree outer_type
= 0;
2884 tree mask
, inner
, offset
;
2886 unsigned int precision
;
2888 /* All the optimizations using this function assume integer fields.
2889 There are problems with FP fields since the type_for_size call
2890 below can fail for, e.g., XFmode. */
2891 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
2894 /* We are interested in the bare arrangement of bits, so strip everything
2895 that doesn't affect the machine mode. However, record the type of the
2896 outermost expression if it may matter below. */
2897 if (TREE_CODE (exp
) == NOP_EXPR
2898 || TREE_CODE (exp
) == CONVERT_EXPR
2899 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
2900 outer_type
= TREE_TYPE (exp
);
2903 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
2905 and_mask
= TREE_OPERAND (exp
, 1);
2906 exp
= TREE_OPERAND (exp
, 0);
2907 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
2908 if (TREE_CODE (and_mask
) != INTEGER_CST
)
2912 inner
= get_inner_reference (exp
, pbitsize
, pbitpos
, &offset
, pmode
,
2913 punsignedp
, pvolatilep
);
2914 if ((inner
== exp
&& and_mask
== 0)
2915 || *pbitsize
< 0 || offset
!= 0
2916 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
)
2919 /* If the number of bits in the reference is the same as the bitsize of
2920 the outer type, then the outer type gives the signedness. Otherwise
2921 (in case of a small bitfield) the signedness is unchanged. */
2922 if (outer_type
&& *pbitsize
== tree_low_cst (TYPE_SIZE (outer_type
), 1))
2923 *punsignedp
= TREE_UNSIGNED (outer_type
);
2925 /* Compute the mask to access the bitfield. */
2926 unsigned_type
= (*lang_hooks
.types
.type_for_size
) (*pbitsize
, 1);
2927 precision
= TYPE_PRECISION (unsigned_type
);
2929 mask
= build_int_2 (~0, ~0);
2930 TREE_TYPE (mask
) = unsigned_type
;
2931 force_fit_type (mask
, 0);
2932 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
), 0);
2933 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
), 0);
2935 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
2937 mask
= fold (build (BIT_AND_EXPR
, unsigned_type
,
2938 convert (unsigned_type
, and_mask
), mask
));
2941 *pand_mask
= and_mask
;
2945 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
2949 all_ones_mask_p (tree mask
, int size
)
2951 tree type
= TREE_TYPE (mask
);
2952 unsigned int precision
= TYPE_PRECISION (type
);
2955 tmask
= build_int_2 (~0, ~0);
2956 TREE_TYPE (tmask
) = (*lang_hooks
.types
.signed_type
) (type
);
2957 force_fit_type (tmask
, 0);
2959 tree_int_cst_equal (mask
,
2960 const_binop (RSHIFT_EXPR
,
2961 const_binop (LSHIFT_EXPR
, tmask
,
2962 size_int (precision
- size
),
2964 size_int (precision
- size
), 0));
2967 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
2968 represents the sign bit of EXP's type. If EXP represents a sign
2969 or zero extension, also test VAL against the unextended type.
2970 The return value is the (sub)expression whose sign bit is VAL,
2971 or NULL_TREE otherwise. */
2974 sign_bit_p (tree exp
, tree val
)
2976 unsigned HOST_WIDE_INT mask_lo
, lo
;
2977 HOST_WIDE_INT mask_hi
, hi
;
2981 /* Tree EXP must have an integral type. */
2982 t
= TREE_TYPE (exp
);
2983 if (! INTEGRAL_TYPE_P (t
))
2986 /* Tree VAL must be an integer constant. */
2987 if (TREE_CODE (val
) != INTEGER_CST
2988 || TREE_CONSTANT_OVERFLOW (val
))
2991 width
= TYPE_PRECISION (t
);
2992 if (width
> HOST_BITS_PER_WIDE_INT
)
2994 hi
= (unsigned HOST_WIDE_INT
) 1 << (width
- HOST_BITS_PER_WIDE_INT
- 1);
2997 mask_hi
= ((unsigned HOST_WIDE_INT
) -1
2998 >> (2 * HOST_BITS_PER_WIDE_INT
- width
));
3004 lo
= (unsigned HOST_WIDE_INT
) 1 << (width
- 1);
3007 mask_lo
= ((unsigned HOST_WIDE_INT
) -1
3008 >> (HOST_BITS_PER_WIDE_INT
- width
));
3011 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3012 treat VAL as if it were unsigned. */
3013 if ((TREE_INT_CST_HIGH (val
) & mask_hi
) == hi
3014 && (TREE_INT_CST_LOW (val
) & mask_lo
) == lo
)
3017 /* Handle extension from a narrower type. */
3018 if (TREE_CODE (exp
) == NOP_EXPR
3019 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0))) < width
)
3020 return sign_bit_p (TREE_OPERAND (exp
, 0), val
);
3025 /* Subroutine for fold_truthop: determine if an operand is simple enough
3026 to be evaluated unconditionally. */
3029 simple_operand_p (tree exp
)
3031 /* Strip any conversions that don't change the machine mode. */
3032 while ((TREE_CODE (exp
) == NOP_EXPR
3033 || TREE_CODE (exp
) == CONVERT_EXPR
)
3034 && (TYPE_MODE (TREE_TYPE (exp
))
3035 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp
, 0)))))
3036 exp
= TREE_OPERAND (exp
, 0);
3038 return (TREE_CODE_CLASS (TREE_CODE (exp
)) == 'c'
3040 && ! TREE_ADDRESSABLE (exp
)
3041 && ! TREE_THIS_VOLATILE (exp
)
3042 && ! DECL_NONLOCAL (exp
)
3043 /* Don't regard global variables as simple. They may be
3044 allocated in ways unknown to the compiler (shared memory,
3045 #pragma weak, etc). */
3046 && ! TREE_PUBLIC (exp
)
3047 && ! DECL_EXTERNAL (exp
)
3048 /* Loading a static variable is unduly expensive, but global
3049 registers aren't expensive. */
3050 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
3053 /* The following functions are subroutines to fold_range_test and allow it to
3054 try to change a logical combination of comparisons into a range test.
3057 X == 2 || X == 3 || X == 4 || X == 5
3061 (unsigned) (X - 2) <= 3
3063 We describe each set of comparisons as being either inside or outside
3064 a range, using a variable named like IN_P, and then describe the
3065 range with a lower and upper bound. If one of the bounds is omitted,
3066 it represents either the highest or lowest value of the type.
3068 In the comments below, we represent a range by two numbers in brackets
3069 preceded by a "+" to designate being inside that range, or a "-" to
3070 designate being outside that range, so the condition can be inverted by
3071 flipping the prefix. An omitted bound is represented by a "-". For
3072 example, "- [-, 10]" means being outside the range starting at the lowest
3073 possible value and ending at 10, in other words, being greater than 10.
3074 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3077 We set up things so that the missing bounds are handled in a consistent
3078 manner so neither a missing bound nor "true" and "false" need to be
3079 handled using a special case. */
3081 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3082 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3083 and UPPER1_P are nonzero if the respective argument is an upper bound
3084 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3085 must be specified for a comparison. ARG1 will be converted to ARG0's
3086 type if both are specified. */
3089 range_binop (enum tree_code code
, tree type
, tree arg0
, int upper0_p
,
3090 tree arg1
, int upper1_p
)
3096 /* If neither arg represents infinity, do the normal operation.
3097 Else, if not a comparison, return infinity. Else handle the special
3098 comparison rules. Note that most of the cases below won't occur, but
3099 are handled for consistency. */
3101 if (arg0
!= 0 && arg1
!= 0)
3103 tem
= fold (build (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
3104 arg0
, convert (TREE_TYPE (arg0
), arg1
)));
3106 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
3109 if (TREE_CODE_CLASS (code
) != '<')
3112 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3113 for neither. In real maths, we cannot assume open ended ranges are
3114 the same. But, this is computer arithmetic, where numbers are finite.
3115 We can therefore make the transformation of any unbounded range with
3116 the value Z, Z being greater than any representable number. This permits
3117 us to treat unbounded ranges as equal. */
3118 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
3119 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
3123 result
= sgn0
== sgn1
;
3126 result
= sgn0
!= sgn1
;
3129 result
= sgn0
< sgn1
;
3132 result
= sgn0
<= sgn1
;
3135 result
= sgn0
> sgn1
;
3138 result
= sgn0
>= sgn1
;
3144 return convert (type
, result
? integer_one_node
: integer_zero_node
);
3147 /* Given EXP, a logical expression, set the range it is testing into
3148 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3149 actually being tested. *PLOW and *PHIGH will be made of the same type
3150 as the returned expression. If EXP is not a comparison, we will most
3151 likely not be returning a useful value and range. */
3154 make_range (tree exp
, int *pin_p
, tree
*plow
, tree
*phigh
)
3156 enum tree_code code
;
3157 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, type
= NULL_TREE
;
3158 tree orig_type
= NULL_TREE
;
3160 tree low
, high
, n_low
, n_high
;
3162 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3163 and see if we can refine the range. Some of the cases below may not
3164 happen, but it doesn't seem worth worrying about this. We "continue"
3165 the outer loop when we've changed something; otherwise we "break"
3166 the switch, which will "break" the while. */
3168 in_p
= 0, low
= high
= convert (TREE_TYPE (exp
), integer_zero_node
);
3172 code
= TREE_CODE (exp
);
3174 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
3176 if (first_rtl_op (code
) > 0)
3177 arg0
= TREE_OPERAND (exp
, 0);
3178 if (TREE_CODE_CLASS (code
) == '<'
3179 || TREE_CODE_CLASS (code
) == '1'
3180 || TREE_CODE_CLASS (code
) == '2')
3181 type
= TREE_TYPE (arg0
);
3182 if (TREE_CODE_CLASS (code
) == '2'
3183 || TREE_CODE_CLASS (code
) == '<'
3184 || (TREE_CODE_CLASS (code
) == 'e'
3185 && TREE_CODE_LENGTH (code
) > 1))
3186 arg1
= TREE_OPERAND (exp
, 1);
3189 /* Set ORIG_TYPE as soon as TYPE is non-null so that we do not
3190 lose a cast by accident. */
3191 if (type
!= NULL_TREE
&& orig_type
== NULL_TREE
)
3196 case TRUTH_NOT_EXPR
:
3197 in_p
= ! in_p
, exp
= arg0
;
3200 case EQ_EXPR
: case NE_EXPR
:
3201 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
3202 /* We can only do something if the range is testing for zero
3203 and if the second operand is an integer constant. Note that
3204 saying something is "in" the range we make is done by
3205 complementing IN_P since it will set in the initial case of
3206 being not equal to zero; "out" is leaving it alone. */
3207 if (low
== 0 || high
== 0
3208 || ! integer_zerop (low
) || ! integer_zerop (high
)
3209 || TREE_CODE (arg1
) != INTEGER_CST
)
3214 case NE_EXPR
: /* - [c, c] */
3217 case EQ_EXPR
: /* + [c, c] */
3218 in_p
= ! in_p
, low
= high
= arg1
;
3220 case GT_EXPR
: /* - [-, c] */
3221 low
= 0, high
= arg1
;
3223 case GE_EXPR
: /* + [c, -] */
3224 in_p
= ! in_p
, low
= arg1
, high
= 0;
3226 case LT_EXPR
: /* - [c, -] */
3227 low
= arg1
, high
= 0;
3229 case LE_EXPR
: /* + [-, c] */
3230 in_p
= ! in_p
, low
= 0, high
= arg1
;
3238 /* If this is an unsigned comparison, we also know that EXP is
3239 greater than or equal to zero. We base the range tests we make
3240 on that fact, so we record it here so we can parse existing
3242 if (TREE_UNSIGNED (type
) && (low
== 0 || high
== 0))
3244 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, in_p
, low
, high
,
3245 1, convert (type
, integer_zero_node
),
3249 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
3251 /* If the high bound is missing, but we have a nonzero low
3252 bound, reverse the range so it goes from zero to the low bound
3254 if (high
== 0 && low
&& ! integer_zerop (low
))
3257 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
3258 integer_one_node
, 0);
3259 low
= convert (type
, integer_zero_node
);
3265 /* (-x) IN [a,b] -> x in [-b, -a] */
3266 n_low
= range_binop (MINUS_EXPR
, type
,
3267 convert (type
, integer_zero_node
), 0, high
, 1);
3268 n_high
= range_binop (MINUS_EXPR
, type
,
3269 convert (type
, integer_zero_node
), 0, low
, 0);
3270 low
= n_low
, high
= n_high
;
3276 exp
= build (MINUS_EXPR
, type
, negate_expr (arg0
),
3277 convert (type
, integer_one_node
));
3280 case PLUS_EXPR
: case MINUS_EXPR
:
3281 if (TREE_CODE (arg1
) != INTEGER_CST
)
3284 /* If EXP is signed, any overflow in the computation is undefined,
3285 so we don't worry about it so long as our computations on
3286 the bounds don't overflow. For unsigned, overflow is defined
3287 and this is exactly the right thing. */
3288 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
3289 type
, low
, 0, arg1
, 0);
3290 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
3291 type
, high
, 1, arg1
, 0);
3292 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
3293 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
3296 /* Check for an unsigned range which has wrapped around the maximum
3297 value thus making n_high < n_low, and normalize it. */
3298 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
3300 low
= range_binop (PLUS_EXPR
, type
, n_high
, 0,
3301 integer_one_node
, 0);
3302 high
= range_binop (MINUS_EXPR
, type
, n_low
, 0,
3303 integer_one_node
, 0);
3305 /* If the range is of the form +/- [ x+1, x ], we won't
3306 be able to normalize it. But then, it represents the
3307 whole range or the empty set, so make it
3309 if (tree_int_cst_equal (n_low
, low
)
3310 && tree_int_cst_equal (n_high
, high
))
3316 low
= n_low
, high
= n_high
;
3321 case NOP_EXPR
: case NON_LVALUE_EXPR
: case CONVERT_EXPR
:
3322 if (TYPE_PRECISION (type
) > TYPE_PRECISION (orig_type
))
3325 if (! INTEGRAL_TYPE_P (type
)
3326 || (low
!= 0 && ! int_fits_type_p (low
, type
))
3327 || (high
!= 0 && ! int_fits_type_p (high
, type
)))
3330 n_low
= low
, n_high
= high
;
3333 n_low
= convert (type
, n_low
);
3336 n_high
= convert (type
, n_high
);
3338 /* If we're converting from an unsigned to a signed type,
3339 we will be doing the comparison as unsigned. The tests above
3340 have already verified that LOW and HIGH are both positive.
3342 So we have to make sure that the original unsigned value will
3343 be interpreted as positive. */
3344 if (TREE_UNSIGNED (type
) && ! TREE_UNSIGNED (TREE_TYPE (exp
)))
3346 tree equiv_type
= (*lang_hooks
.types
.type_for_mode
)
3347 (TYPE_MODE (type
), 1);
3350 /* A range without an upper bound is, naturally, unbounded.
3351 Since convert would have cropped a very large value, use
3352 the max value for the destination type. */
3354 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
3355 : TYPE_MAX_VALUE (type
);
3357 if (TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (exp
)))
3358 high_positive
= fold (build (RSHIFT_EXPR
, type
,
3359 convert (type
, high_positive
),
3360 convert (type
, integer_one_node
)));
3362 /* If the low bound is specified, "and" the range with the
3363 range for which the original unsigned value will be
3367 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
3369 1, convert (type
, integer_zero_node
),
3373 in_p
= (n_in_p
== in_p
);
3377 /* Otherwise, "or" the range with the range of the input
3378 that will be interpreted as negative. */
3379 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
3381 1, convert (type
, integer_zero_node
),
3385 in_p
= (in_p
!= n_in_p
);
3390 low
= n_low
, high
= n_high
;
3400 /* If EXP is a constant, we can evaluate whether this is true or false. */
3401 if (TREE_CODE (exp
) == INTEGER_CST
)
3403 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
3405 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
3411 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
3415 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3416 type, TYPE, return an expression to test if EXP is in (or out of, depending
3417 on IN_P) the range. */
3420 build_range_check (tree type
, tree exp
, int in_p
, tree low
, tree high
)
3422 tree etype
= TREE_TYPE (exp
);
3426 && (0 != (value
= build_range_check (type
, exp
, 1, low
, high
))))
3427 return invert_truthvalue (value
);
3429 if (low
== 0 && high
== 0)
3430 return convert (type
, integer_one_node
);
3433 return fold (build (LE_EXPR
, type
, exp
, high
));
3436 return fold (build (GE_EXPR
, type
, exp
, low
));
3438 if (operand_equal_p (low
, high
, 0))
3439 return fold (build (EQ_EXPR
, type
, exp
, low
));
3441 if (integer_zerop (low
))
3443 if (! TREE_UNSIGNED (etype
))
3445 etype
= (*lang_hooks
.types
.unsigned_type
) (etype
);
3446 high
= convert (etype
, high
);
3447 exp
= convert (etype
, exp
);
3449 return build_range_check (type
, exp
, 1, 0, high
);
3452 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
3453 if (integer_onep (low
) && TREE_CODE (high
) == INTEGER_CST
)
3455 unsigned HOST_WIDE_INT lo
;
3459 prec
= TYPE_PRECISION (etype
);
3460 if (prec
<= HOST_BITS_PER_WIDE_INT
)
3463 lo
= ((unsigned HOST_WIDE_INT
) 1 << (prec
- 1)) - 1;
3467 hi
= ((HOST_WIDE_INT
) 1 << (prec
- HOST_BITS_PER_WIDE_INT
- 1)) - 1;
3468 lo
= (unsigned HOST_WIDE_INT
) -1;
3471 if (TREE_INT_CST_HIGH (high
) == hi
&& TREE_INT_CST_LOW (high
) == lo
)
3473 if (TREE_UNSIGNED (etype
))
3475 etype
= (*lang_hooks
.types
.signed_type
) (etype
);
3476 exp
= convert (etype
, exp
);
3478 return fold (build (GT_EXPR
, type
, exp
,
3479 convert (etype
, integer_zero_node
)));
3483 if (0 != (value
= const_binop (MINUS_EXPR
, high
, low
, 0))
3484 && ! TREE_OVERFLOW (value
))
3485 return build_range_check (type
,
3486 fold (build (MINUS_EXPR
, etype
, exp
, low
)),
3487 1, convert (etype
, integer_zero_node
), value
);
3492 /* Given two ranges, see if we can merge them into one. Return 1 if we
3493 can, 0 if we can't. Set the output range into the specified parameters. */
3496 merge_ranges (int *pin_p
, tree
*plow
, tree
*phigh
, int in0_p
, tree low0
,
3497 tree high0
, int in1_p
, tree low1
, tree high1
)
3505 int lowequal
= ((low0
== 0 && low1
== 0)
3506 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
3507 low0
, 0, low1
, 0)));
3508 int highequal
= ((high0
== 0 && high1
== 0)
3509 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
3510 high0
, 1, high1
, 1)));
3512 /* Make range 0 be the range that starts first, or ends last if they
3513 start at the same value. Swap them if it isn't. */
3514 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
3517 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
3518 high1
, 1, high0
, 1))))
3520 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
3521 tem
= low0
, low0
= low1
, low1
= tem
;
3522 tem
= high0
, high0
= high1
, high1
= tem
;
3525 /* Now flag two cases, whether the ranges are disjoint or whether the
3526 second range is totally subsumed in the first. Note that the tests
3527 below are simplified by the ones above. */
3528 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
3529 high0
, 1, low1
, 0));
3530 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
3531 high1
, 1, high0
, 1));
3533 /* We now have four cases, depending on whether we are including or
3534 excluding the two ranges. */
3537 /* If they don't overlap, the result is false. If the second range
3538 is a subset it is the result. Otherwise, the range is from the start
3539 of the second to the end of the first. */
3541 in_p
= 0, low
= high
= 0;
3543 in_p
= 1, low
= low1
, high
= high1
;
3545 in_p
= 1, low
= low1
, high
= high0
;
3548 else if (in0_p
&& ! in1_p
)
3550 /* If they don't overlap, the result is the first range. If they are
3551 equal, the result is false. If the second range is a subset of the
3552 first, and the ranges begin at the same place, we go from just after
3553 the end of the first range to the end of the second. If the second
3554 range is not a subset of the first, or if it is a subset and both
3555 ranges end at the same place, the range starts at the start of the
3556 first range and ends just before the second range.
3557 Otherwise, we can't describe this as a single range. */
3559 in_p
= 1, low
= low0
, high
= high0
;
3560 else if (lowequal
&& highequal
)
3561 in_p
= 0, low
= high
= 0;
3562 else if (subset
&& lowequal
)
3564 in_p
= 1, high
= high0
;
3565 low
= range_binop (PLUS_EXPR
, NULL_TREE
, high1
, 0,
3566 integer_one_node
, 0);
3568 else if (! subset
|| highequal
)
3570 in_p
= 1, low
= low0
;
3571 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low1
, 0,
3572 integer_one_node
, 0);
3578 else if (! in0_p
&& in1_p
)
3580 /* If they don't overlap, the result is the second range. If the second
3581 is a subset of the first, the result is false. Otherwise,
3582 the range starts just after the first range and ends at the
3583 end of the second. */
3585 in_p
= 1, low
= low1
, high
= high1
;
3586 else if (subset
|| highequal
)
3587 in_p
= 0, low
= high
= 0;
3590 in_p
= 1, high
= high1
;
3591 low
= range_binop (PLUS_EXPR
, NULL_TREE
, high0
, 1,
3592 integer_one_node
, 0);
3598 /* The case where we are excluding both ranges. Here the complex case
3599 is if they don't overlap. In that case, the only time we have a
3600 range is if they are adjacent. If the second is a subset of the
3601 first, the result is the first. Otherwise, the range to exclude
3602 starts at the beginning of the first range and ends at the end of the
3606 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
3607 range_binop (PLUS_EXPR
, NULL_TREE
,
3609 integer_one_node
, 1),
3611 in_p
= 0, low
= low0
, high
= high1
;
3616 in_p
= 0, low
= low0
, high
= high0
;
3618 in_p
= 0, low
= low0
, high
= high1
;
3621 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
3625 #ifndef RANGE_TEST_NON_SHORT_CIRCUIT
3626 #define RANGE_TEST_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
3629 /* EXP is some logical combination of boolean tests. See if we can
3630 merge it into some range test. Return the new tree if so. */
3633 fold_range_test (tree exp
)
3635 int or_op
= (TREE_CODE (exp
) == TRUTH_ORIF_EXPR
3636 || TREE_CODE (exp
) == TRUTH_OR_EXPR
);
3637 int in0_p
, in1_p
, in_p
;
3638 tree low0
, low1
, low
, high0
, high1
, high
;
3639 tree lhs
= make_range (TREE_OPERAND (exp
, 0), &in0_p
, &low0
, &high0
);
3640 tree rhs
= make_range (TREE_OPERAND (exp
, 1), &in1_p
, &low1
, &high1
);
3643 /* If this is an OR operation, invert both sides; we will invert
3644 again at the end. */
3646 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
3648 /* If both expressions are the same, if we can merge the ranges, and we
3649 can build the range test, return it or it inverted. If one of the
3650 ranges is always true or always false, consider it to be the same
3651 expression as the other. */
3652 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
3653 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
3655 && 0 != (tem
= (build_range_check (TREE_TYPE (exp
),
3657 : rhs
!= 0 ? rhs
: integer_zero_node
,
3659 return or_op
? invert_truthvalue (tem
) : tem
;
3661 /* On machines where the branch cost is expensive, if this is a
3662 short-circuited branch and the underlying object on both sides
3663 is the same, make a non-short-circuit operation. */
3664 else if (RANGE_TEST_NON_SHORT_CIRCUIT
3665 && lhs
!= 0 && rhs
!= 0
3666 && (TREE_CODE (exp
) == TRUTH_ANDIF_EXPR
3667 || TREE_CODE (exp
) == TRUTH_ORIF_EXPR
)
3668 && operand_equal_p (lhs
, rhs
, 0))
3670 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
3671 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
3672 which cases we can't do this. */
3673 if (simple_operand_p (lhs
))
3674 return build (TREE_CODE (exp
) == TRUTH_ANDIF_EXPR
3675 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
3676 TREE_TYPE (exp
), TREE_OPERAND (exp
, 0),
3677 TREE_OPERAND (exp
, 1));
3679 else if ((*lang_hooks
.decls
.global_bindings_p
) () == 0
3680 && ! CONTAINS_PLACEHOLDER_P (lhs
))
3682 tree common
= save_expr (lhs
);
3684 if (0 != (lhs
= build_range_check (TREE_TYPE (exp
), common
,
3685 or_op
? ! in0_p
: in0_p
,
3687 && (0 != (rhs
= build_range_check (TREE_TYPE (exp
), common
,
3688 or_op
? ! in1_p
: in1_p
,
3690 return build (TREE_CODE (exp
) == TRUTH_ANDIF_EXPR
3691 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
3692 TREE_TYPE (exp
), lhs
, rhs
);
3699 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
3700 bit value. Arrange things so the extra bits will be set to zero if and
3701 only if C is signed-extended to its full width. If MASK is nonzero,
3702 it is an INTEGER_CST that should be AND'ed with the extra bits. */
3705 unextend (tree c
, int p
, int unsignedp
, tree mask
)
3707 tree type
= TREE_TYPE (c
);
3708 int modesize
= GET_MODE_BITSIZE (TYPE_MODE (type
));
3711 if (p
== modesize
|| unsignedp
)
3714 /* We work by getting just the sign bit into the low-order bit, then
3715 into the high-order bit, then sign-extend. We then XOR that value
3717 temp
= const_binop (RSHIFT_EXPR
, c
, size_int (p
- 1), 0);
3718 temp
= const_binop (BIT_AND_EXPR
, temp
, size_int (1), 0);
3720 /* We must use a signed type in order to get an arithmetic right shift.
3721 However, we must also avoid introducing accidental overflows, so that
3722 a subsequent call to integer_zerop will work. Hence we must
3723 do the type conversion here. At this point, the constant is either
3724 zero or one, and the conversion to a signed type can never overflow.
3725 We could get an overflow if this conversion is done anywhere else. */
3726 if (TREE_UNSIGNED (type
))
3727 temp
= convert ((*lang_hooks
.types
.signed_type
) (type
), temp
);
3729 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1), 0);
3730 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1), 0);
3732 temp
= const_binop (BIT_AND_EXPR
, temp
, convert (TREE_TYPE (c
), mask
), 0);
3733 /* If necessary, convert the type back to match the type of C. */
3734 if (TREE_UNSIGNED (type
))
3735 temp
= convert (type
, temp
);
3737 return convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
, 0));
3740 /* Find ways of folding logical expressions of LHS and RHS:
3741 Try to merge two comparisons to the same innermost item.
3742 Look for range tests like "ch >= '0' && ch <= '9'".
3743 Look for combinations of simple terms on machines with expensive branches
3744 and evaluate the RHS unconditionally.
3746 For example, if we have p->a == 2 && p->b == 4 and we can make an
3747 object large enough to span both A and B, we can do this with a comparison
3748 against the object ANDed with the a mask.
3750 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
3751 operations to do this with one comparison.
3753 We check for both normal comparisons and the BIT_AND_EXPRs made this by
3754 function and the one above.
3756 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
3757 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
3759 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
3762 We return the simplified tree or 0 if no optimization is possible. */
3765 fold_truthop (enum tree_code code
, tree truth_type
, tree lhs
, tree rhs
)
3767 /* If this is the "or" of two comparisons, we can do something if
3768 the comparisons are NE_EXPR. If this is the "and", we can do something
3769 if the comparisons are EQ_EXPR. I.e.,
3770 (a->b == 2 && a->c == 4) can become (a->new == NEW).
3772 WANTED_CODE is this operation code. For single bit fields, we can
3773 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
3774 comparison for one-bit fields. */
3776 enum tree_code wanted_code
;
3777 enum tree_code lcode
, rcode
;
3778 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
3779 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
3780 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
3781 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
3782 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
3783 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
3784 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
3785 enum machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
3786 enum machine_mode lnmode
, rnmode
;
3787 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
3788 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
3789 tree l_const
, r_const
;
3790 tree lntype
, rntype
, result
;
3791 int first_bit
, end_bit
;
3794 /* Start by getting the comparison codes. Fail if anything is volatile.
3795 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
3796 it were surrounded with a NE_EXPR. */
3798 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
3801 lcode
= TREE_CODE (lhs
);
3802 rcode
= TREE_CODE (rhs
);
3804 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
3805 lcode
= NE_EXPR
, lhs
= build (NE_EXPR
, truth_type
, lhs
, integer_zero_node
);
3807 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
3808 rcode
= NE_EXPR
, rhs
= build (NE_EXPR
, truth_type
, rhs
, integer_zero_node
);
3810 if (TREE_CODE_CLASS (lcode
) != '<' || TREE_CODE_CLASS (rcode
) != '<')
3813 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
3814 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
3816 ll_arg
= TREE_OPERAND (lhs
, 0);
3817 lr_arg
= TREE_OPERAND (lhs
, 1);
3818 rl_arg
= TREE_OPERAND (rhs
, 0);
3819 rr_arg
= TREE_OPERAND (rhs
, 1);
3821 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
3822 if (simple_operand_p (ll_arg
)
3823 && simple_operand_p (lr_arg
)
3824 && !FLOAT_TYPE_P (TREE_TYPE (ll_arg
)))
3828 if (operand_equal_p (ll_arg
, rl_arg
, 0)
3829 && operand_equal_p (lr_arg
, rr_arg
, 0))
3831 int lcompcode
, rcompcode
;
3833 lcompcode
= comparison_to_compcode (lcode
);
3834 rcompcode
= comparison_to_compcode (rcode
);
3835 compcode
= (code
== TRUTH_AND_EXPR
)
3836 ? lcompcode
& rcompcode
3837 : lcompcode
| rcompcode
;
3839 else if (operand_equal_p (ll_arg
, rr_arg
, 0)
3840 && operand_equal_p (lr_arg
, rl_arg
, 0))
3842 int lcompcode
, rcompcode
;
3844 rcode
= swap_tree_comparison (rcode
);
3845 lcompcode
= comparison_to_compcode (lcode
);
3846 rcompcode
= comparison_to_compcode (rcode
);
3847 compcode
= (code
== TRUTH_AND_EXPR
)
3848 ? lcompcode
& rcompcode
3849 : lcompcode
| rcompcode
;
3854 if (compcode
== COMPCODE_TRUE
)
3855 return convert (truth_type
, integer_one_node
);
3856 else if (compcode
== COMPCODE_FALSE
)
3857 return convert (truth_type
, integer_zero_node
);
3858 else if (compcode
!= -1)
3859 return build (compcode_to_comparison (compcode
),
3860 truth_type
, ll_arg
, lr_arg
);
3863 /* If the RHS can be evaluated unconditionally and its operands are
3864 simple, it wins to evaluate the RHS unconditionally on machines
3865 with expensive branches. In this case, this isn't a comparison
3866 that can be merged. Avoid doing this if the RHS is a floating-point
3867 comparison since those can trap. */
3869 if (BRANCH_COST
>= 2
3870 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
3871 && simple_operand_p (rl_arg
)
3872 && simple_operand_p (rr_arg
))
3874 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
3875 if (code
== TRUTH_OR_EXPR
3876 && lcode
== NE_EXPR
&& integer_zerop (lr_arg
)
3877 && rcode
== NE_EXPR
&& integer_zerop (rr_arg
)
3878 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
))
3879 return build (NE_EXPR
, truth_type
,
3880 build (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
3884 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
3885 if (code
== TRUTH_AND_EXPR
3886 && lcode
== EQ_EXPR
&& integer_zerop (lr_arg
)
3887 && rcode
== EQ_EXPR
&& integer_zerop (rr_arg
)
3888 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
))
3889 return build (EQ_EXPR
, truth_type
,
3890 build (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
3894 return build (code
, truth_type
, lhs
, rhs
);
3897 /* See if the comparisons can be merged. Then get all the parameters for
3900 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
3901 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
3905 ll_inner
= decode_field_reference (ll_arg
,
3906 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
3907 &ll_unsignedp
, &volatilep
, &ll_mask
,
3909 lr_inner
= decode_field_reference (lr_arg
,
3910 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
3911 &lr_unsignedp
, &volatilep
, &lr_mask
,
3913 rl_inner
= decode_field_reference (rl_arg
,
3914 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
3915 &rl_unsignedp
, &volatilep
, &rl_mask
,
3917 rr_inner
= decode_field_reference (rr_arg
,
3918 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
3919 &rr_unsignedp
, &volatilep
, &rr_mask
,
3922 /* It must be true that the inner operation on the lhs of each
3923 comparison must be the same if we are to be able to do anything.
3924 Then see if we have constants. If not, the same must be true for
3926 if (volatilep
|| ll_inner
== 0 || rl_inner
== 0
3927 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
3930 if (TREE_CODE (lr_arg
) == INTEGER_CST
3931 && TREE_CODE (rr_arg
) == INTEGER_CST
)
3932 l_const
= lr_arg
, r_const
= rr_arg
;
3933 else if (lr_inner
== 0 || rr_inner
== 0
3934 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
3937 l_const
= r_const
= 0;
3939 /* If either comparison code is not correct for our logical operation,
3940 fail. However, we can convert a one-bit comparison against zero into
3941 the opposite comparison against that bit being set in the field. */
3943 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
3944 if (lcode
!= wanted_code
)
3946 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
3948 /* Make the left operand unsigned, since we are only interested
3949 in the value of one bit. Otherwise we are doing the wrong
3958 /* This is analogous to the code for l_const above. */
3959 if (rcode
!= wanted_code
)
3961 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
3970 /* After this point all optimizations will generate bit-field
3971 references, which we might not want. */
3972 if (! (*lang_hooks
.can_use_bit_fields_p
) ())
3975 /* See if we can find a mode that contains both fields being compared on
3976 the left. If we can't, fail. Otherwise, update all constants and masks
3977 to be relative to a field of that size. */
3978 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
3979 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
3980 lnmode
= get_best_mode (end_bit
- first_bit
, first_bit
,
3981 TYPE_ALIGN (TREE_TYPE (ll_inner
)), word_mode
,
3983 if (lnmode
== VOIDmode
)
3986 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
3987 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
3988 lntype
= (*lang_hooks
.types
.type_for_size
) (lnbitsize
, 1);
3989 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
3991 if (BYTES_BIG_ENDIAN
)
3993 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
3994 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
3997 ll_mask
= const_binop (LSHIFT_EXPR
, convert (lntype
, ll_mask
),
3998 size_int (xll_bitpos
), 0);
3999 rl_mask
= const_binop (LSHIFT_EXPR
, convert (lntype
, rl_mask
),
4000 size_int (xrl_bitpos
), 0);
4004 l_const
= convert (lntype
, l_const
);
4005 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
4006 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
), 0);
4007 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
4008 fold (build1 (BIT_NOT_EXPR
,
4012 warning ("comparison is always %d", wanted_code
== NE_EXPR
);
4014 return convert (truth_type
,
4015 wanted_code
== NE_EXPR
4016 ? integer_one_node
: integer_zero_node
);
4021 r_const
= convert (lntype
, r_const
);
4022 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
4023 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
), 0);
4024 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
4025 fold (build1 (BIT_NOT_EXPR
,
4029 warning ("comparison is always %d", wanted_code
== NE_EXPR
);
4031 return convert (truth_type
,
4032 wanted_code
== NE_EXPR
4033 ? integer_one_node
: integer_zero_node
);
4037 /* If the right sides are not constant, do the same for it. Also,
4038 disallow this optimization if a size or signedness mismatch occurs
4039 between the left and right sides. */
4042 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
4043 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
4044 /* Make sure the two fields on the right
4045 correspond to the left without being swapped. */
4046 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
4049 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
4050 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
4051 rnmode
= get_best_mode (end_bit
- first_bit
, first_bit
,
4052 TYPE_ALIGN (TREE_TYPE (lr_inner
)), word_mode
,
4054 if (rnmode
== VOIDmode
)
4057 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
4058 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
4059 rntype
= (*lang_hooks
.types
.type_for_size
) (rnbitsize
, 1);
4060 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
4062 if (BYTES_BIG_ENDIAN
)
4064 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
4065 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
4068 lr_mask
= const_binop (LSHIFT_EXPR
, convert (rntype
, lr_mask
),
4069 size_int (xlr_bitpos
), 0);
4070 rr_mask
= const_binop (LSHIFT_EXPR
, convert (rntype
, rr_mask
),
4071 size_int (xrr_bitpos
), 0);
4073 /* Make a mask that corresponds to both fields being compared.
4074 Do this for both items being compared. If the operands are the
4075 same size and the bits being compared are in the same position
4076 then we can do this by masking both and comparing the masked
4078 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
, 0);
4079 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
, 0);
4080 if (lnbitsize
== rnbitsize
&& xll_bitpos
== xlr_bitpos
)
4082 lhs
= make_bit_field_ref (ll_inner
, lntype
, lnbitsize
, lnbitpos
,
4083 ll_unsignedp
|| rl_unsignedp
);
4084 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
4085 lhs
= build (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
4087 rhs
= make_bit_field_ref (lr_inner
, rntype
, rnbitsize
, rnbitpos
,
4088 lr_unsignedp
|| rr_unsignedp
);
4089 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
4090 rhs
= build (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
4092 return build (wanted_code
, truth_type
, lhs
, rhs
);
4095 /* There is still another way we can do something: If both pairs of
4096 fields being compared are adjacent, we may be able to make a wider
4097 field containing them both.
4099 Note that we still must mask the lhs/rhs expressions. Furthermore,
4100 the mask must be shifted to account for the shift done by
4101 make_bit_field_ref. */
4102 if ((ll_bitsize
+ ll_bitpos
== rl_bitpos
4103 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
4104 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
4105 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
4109 lhs
= make_bit_field_ref (ll_inner
, lntype
, ll_bitsize
+ rl_bitsize
,
4110 MIN (ll_bitpos
, rl_bitpos
), ll_unsignedp
);
4111 rhs
= make_bit_field_ref (lr_inner
, rntype
, lr_bitsize
+ rr_bitsize
,
4112 MIN (lr_bitpos
, rr_bitpos
), lr_unsignedp
);
4114 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
4115 size_int (MIN (xll_bitpos
, xrl_bitpos
)), 0);
4116 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
4117 size_int (MIN (xlr_bitpos
, xrr_bitpos
)), 0);
4119 /* Convert to the smaller type before masking out unwanted bits. */
4121 if (lntype
!= rntype
)
4123 if (lnbitsize
> rnbitsize
)
4125 lhs
= convert (rntype
, lhs
);
4126 ll_mask
= convert (rntype
, ll_mask
);
4129 else if (lnbitsize
< rnbitsize
)
4131 rhs
= convert (lntype
, rhs
);
4132 lr_mask
= convert (lntype
, lr_mask
);
4137 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
4138 lhs
= build (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
4140 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
4141 rhs
= build (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
4143 return build (wanted_code
, truth_type
, lhs
, rhs
);
4149 /* Handle the case of comparisons with constants. If there is something in
4150 common between the masks, those bits of the constants must be the same.
4151 If not, the condition is always false. Test for this to avoid generating
4152 incorrect code below. */
4153 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
, 0);
4154 if (! integer_zerop (result
)
4155 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
, 0),
4156 const_binop (BIT_AND_EXPR
, result
, r_const
, 0)) != 1)
4158 if (wanted_code
== NE_EXPR
)
4160 warning ("`or' of unmatched not-equal tests is always 1");
4161 return convert (truth_type
, integer_one_node
);
4165 warning ("`and' of mutually exclusive equal-tests is always 0");
4166 return convert (truth_type
, integer_zero_node
);
4170 /* Construct the expression we will return. First get the component
4171 reference we will make. Unless the mask is all ones the width of
4172 that field, perform the mask operation. Then compare with the
4174 result
= make_bit_field_ref (ll_inner
, lntype
, lnbitsize
, lnbitpos
,
4175 ll_unsignedp
|| rl_unsignedp
);
4177 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
, 0);
4178 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
4179 result
= build (BIT_AND_EXPR
, lntype
, result
, ll_mask
);
4181 return build (wanted_code
, truth_type
, result
,
4182 const_binop (BIT_IOR_EXPR
, l_const
, r_const
, 0));
4185 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4189 optimize_minmax_comparison (tree t
)
4191 tree type
= TREE_TYPE (t
);
4192 tree arg0
= TREE_OPERAND (t
, 0);
4193 enum tree_code op_code
;
4194 tree comp_const
= TREE_OPERAND (t
, 1);
4196 int consts_equal
, consts_lt
;
4199 STRIP_SIGN_NOPS (arg0
);
4201 op_code
= TREE_CODE (arg0
);
4202 minmax_const
= TREE_OPERAND (arg0
, 1);
4203 consts_equal
= tree_int_cst_equal (minmax_const
, comp_const
);
4204 consts_lt
= tree_int_cst_lt (minmax_const
, comp_const
);
4205 inner
= TREE_OPERAND (arg0
, 0);
4207 /* If something does not permit us to optimize, return the original tree. */
4208 if ((op_code
!= MIN_EXPR
&& op_code
!= MAX_EXPR
)
4209 || TREE_CODE (comp_const
) != INTEGER_CST
4210 || TREE_CONSTANT_OVERFLOW (comp_const
)
4211 || TREE_CODE (minmax_const
) != INTEGER_CST
4212 || TREE_CONSTANT_OVERFLOW (minmax_const
))
4215 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4216 and GT_EXPR, doing the rest with recursive calls using logical
4218 switch (TREE_CODE (t
))
4220 case NE_EXPR
: case LT_EXPR
: case LE_EXPR
:
4222 invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t
)));
4226 fold (build (TRUTH_ORIF_EXPR
, type
,
4227 optimize_minmax_comparison
4228 (build (EQ_EXPR
, type
, arg0
, comp_const
)),
4229 optimize_minmax_comparison
4230 (build (GT_EXPR
, type
, arg0
, comp_const
))));
4233 if (op_code
== MAX_EXPR
&& consts_equal
)
4234 /* MAX (X, 0) == 0 -> X <= 0 */
4235 return fold (build (LE_EXPR
, type
, inner
, comp_const
));
4237 else if (op_code
== MAX_EXPR
&& consts_lt
)
4238 /* MAX (X, 0) == 5 -> X == 5 */
4239 return fold (build (EQ_EXPR
, type
, inner
, comp_const
));
4241 else if (op_code
== MAX_EXPR
)
4242 /* MAX (X, 0) == -1 -> false */
4243 return omit_one_operand (type
, integer_zero_node
, inner
);
4245 else if (consts_equal
)
4246 /* MIN (X, 0) == 0 -> X >= 0 */
4247 return fold (build (GE_EXPR
, type
, inner
, comp_const
));
4250 /* MIN (X, 0) == 5 -> false */
4251 return omit_one_operand (type
, integer_zero_node
, inner
);
4254 /* MIN (X, 0) == -1 -> X == -1 */
4255 return fold (build (EQ_EXPR
, type
, inner
, comp_const
));
4258 if (op_code
== MAX_EXPR
&& (consts_equal
|| consts_lt
))
4259 /* MAX (X, 0) > 0 -> X > 0
4260 MAX (X, 0) > 5 -> X > 5 */
4261 return fold (build (GT_EXPR
, type
, inner
, comp_const
));
4263 else if (op_code
== MAX_EXPR
)
4264 /* MAX (X, 0) > -1 -> true */
4265 return omit_one_operand (type
, integer_one_node
, inner
);
4267 else if (op_code
== MIN_EXPR
&& (consts_equal
|| consts_lt
))
4268 /* MIN (X, 0) > 0 -> false
4269 MIN (X, 0) > 5 -> false */
4270 return omit_one_operand (type
, integer_zero_node
, inner
);
4273 /* MIN (X, 0) > -1 -> X > -1 */
4274 return fold (build (GT_EXPR
, type
, inner
, comp_const
));
4281 /* T is an integer expression that is being multiplied, divided, or taken a
4282 modulus (CODE says which and what kind of divide or modulus) by a
4283 constant C. See if we can eliminate that operation by folding it with
4284 other operations already in T. WIDE_TYPE, if non-null, is a type that
4285 should be used for the computation if wider than our type.
4287 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
4288 (X * 2) + (Y * 4). We must, however, be assured that either the original
4289 expression would not overflow or that overflow is undefined for the type
4290 in the language in question.
4292 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
4293 the machine has a multiply-accumulate insn or that this is part of an
4294 addressing calculation.
4296 If we return a non-null expression, it is an equivalent form of the
4297 original computation, but need not be in the original type. */
4300 extract_muldiv (tree t
, tree c
, enum tree_code code
, tree wide_type
)
4302 /* To avoid exponential search depth, refuse to allow recursion past
4303 three levels. Beyond that (1) it's highly unlikely that we'll find
4304 something interesting and (2) we've probably processed it before
4305 when we built the inner expression. */
4314 ret
= extract_muldiv_1 (t
, c
, code
, wide_type
);
4321 extract_muldiv_1 (tree t
, tree c
, enum tree_code code
, tree wide_type
)
4323 tree type
= TREE_TYPE (t
);
4324 enum tree_code tcode
= TREE_CODE (t
);
4325 tree ctype
= (wide_type
!= 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type
))
4326 > GET_MODE_SIZE (TYPE_MODE (type
)))
4327 ? wide_type
: type
);
4329 int same_p
= tcode
== code
;
4330 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
4332 /* Don't deal with constants of zero here; they confuse the code below. */
4333 if (integer_zerop (c
))
4336 if (TREE_CODE_CLASS (tcode
) == '1')
4337 op0
= TREE_OPERAND (t
, 0);
4339 if (TREE_CODE_CLASS (tcode
) == '2')
4340 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
4342 /* Note that we need not handle conditional operations here since fold
4343 already handles those cases. So just do arithmetic here. */
4347 /* For a constant, we can always simplify if we are a multiply
4348 or (for divide and modulus) if it is a multiple of our constant. */
4349 if (code
== MULT_EXPR
4350 || integer_zerop (const_binop (TRUNC_MOD_EXPR
, t
, c
, 0)))
4351 return const_binop (code
, convert (ctype
, t
), convert (ctype
, c
), 0);
4354 case CONVERT_EXPR
: case NON_LVALUE_EXPR
: case NOP_EXPR
:
4355 /* If op0 is an expression ... */
4356 if ((TREE_CODE_CLASS (TREE_CODE (op0
)) == '<'
4357 || TREE_CODE_CLASS (TREE_CODE (op0
)) == '1'
4358 || TREE_CODE_CLASS (TREE_CODE (op0
)) == '2'
4359 || TREE_CODE_CLASS (TREE_CODE (op0
)) == 'e')
4360 /* ... and is unsigned, and its type is smaller than ctype,
4361 then we cannot pass through as widening. */
4362 && ((TREE_UNSIGNED (TREE_TYPE (op0
))
4363 && ! (TREE_CODE (TREE_TYPE (op0
)) == INTEGER_TYPE
4364 && TYPE_IS_SIZETYPE (TREE_TYPE (op0
)))
4365 && (GET_MODE_SIZE (TYPE_MODE (ctype
))
4366 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0
)))))
4367 /* ... or its type is larger than ctype,
4368 then we cannot pass through this truncation. */
4369 || (GET_MODE_SIZE (TYPE_MODE (ctype
))
4370 < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0
))))
4371 /* ... or signedness changes for division or modulus,
4372 then we cannot pass through this conversion. */
4373 || (code
!= MULT_EXPR
4374 && (TREE_UNSIGNED (ctype
)
4375 != TREE_UNSIGNED (TREE_TYPE (op0
))))))
4378 /* Pass the constant down and see if we can make a simplification. If
4379 we can, replace this expression with the inner simplification for
4380 possible later conversion to our or some other type. */
4381 if ((t2
= convert (TREE_TYPE (op0
), c
)) != 0
4382 && TREE_CODE (t2
) == INTEGER_CST
4383 && ! TREE_CONSTANT_OVERFLOW (t2
)
4384 && (0 != (t1
= extract_muldiv (op0
, t2
, code
,
4386 ? ctype
: NULL_TREE
))))
4390 case NEGATE_EXPR
: case ABS_EXPR
:
4391 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
)) != 0)
4392 return fold (build1 (tcode
, ctype
, convert (ctype
, t1
)));
4395 case MIN_EXPR
: case MAX_EXPR
:
4396 /* If widening the type changes the signedness, then we can't perform
4397 this optimization as that changes the result. */
4398 if (TREE_UNSIGNED (ctype
) != TREE_UNSIGNED (type
))
4401 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
4402 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
)) != 0
4403 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
)) != 0)
4405 if (tree_int_cst_sgn (c
) < 0)
4406 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
4408 return fold (build (tcode
, ctype
, convert (ctype
, t1
),
4409 convert (ctype
, t2
)));
4413 case WITH_RECORD_EXPR
:
4414 if ((t1
= extract_muldiv (TREE_OPERAND (t
, 0), c
, code
, wide_type
)) != 0)
4415 return build (WITH_RECORD_EXPR
, TREE_TYPE (t1
), t1
,
4416 TREE_OPERAND (t
, 1));
4419 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
4420 /* If the second operand is constant, this is a multiplication
4421 or floor division, by a power of two, so we can treat it that
4422 way unless the multiplier or divisor overflows. */
4423 if (TREE_CODE (op1
) == INTEGER_CST
4424 /* const_binop may not detect overflow correctly,
4425 so check for it explicitly here. */
4426 && TYPE_PRECISION (TREE_TYPE (size_one_node
)) > TREE_INT_CST_LOW (op1
)
4427 && TREE_INT_CST_HIGH (op1
) == 0
4428 && 0 != (t1
= convert (ctype
,
4429 const_binop (LSHIFT_EXPR
, size_one_node
,
4431 && ! TREE_OVERFLOW (t1
))
4432 return extract_muldiv (build (tcode
== LSHIFT_EXPR
4433 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
4434 ctype
, convert (ctype
, op0
), t1
),
4435 c
, code
, wide_type
);
4438 case PLUS_EXPR
: case MINUS_EXPR
:
4439 /* See if we can eliminate the operation on both sides. If we can, we
4440 can return a new PLUS or MINUS. If we can't, the only remaining
4441 cases where we can do anything are if the second operand is a
4443 t1
= extract_muldiv (op0
, c
, code
, wide_type
);
4444 t2
= extract_muldiv (op1
, c
, code
, wide_type
);
4445 if (t1
!= 0 && t2
!= 0
4446 && (code
== MULT_EXPR
4447 /* If not multiplication, we can only do this if both operands
4448 are divisible by c. */
4449 || (multiple_of_p (ctype
, op0
, c
)
4450 && multiple_of_p (ctype
, op1
, c
))))
4451 return fold (build (tcode
, ctype
, convert (ctype
, t1
),
4452 convert (ctype
, t2
)));
4454 /* If this was a subtraction, negate OP1 and set it to be an addition.
4455 This simplifies the logic below. */
4456 if (tcode
== MINUS_EXPR
)
4457 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
4459 if (TREE_CODE (op1
) != INTEGER_CST
)
4462 /* If either OP1 or C are negative, this optimization is not safe for
4463 some of the division and remainder types while for others we need
4464 to change the code. */
4465 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
4467 if (code
== CEIL_DIV_EXPR
)
4468 code
= FLOOR_DIV_EXPR
;
4469 else if (code
== FLOOR_DIV_EXPR
)
4470 code
= CEIL_DIV_EXPR
;
4471 else if (code
!= MULT_EXPR
4472 && code
!= CEIL_MOD_EXPR
&& code
!= FLOOR_MOD_EXPR
)
4476 /* If it's a multiply or a division/modulus operation of a multiple
4477 of our constant, do the operation and verify it doesn't overflow. */
4478 if (code
== MULT_EXPR
4479 || integer_zerop (const_binop (TRUNC_MOD_EXPR
, op1
, c
, 0)))
4481 op1
= const_binop (code
, convert (ctype
, op1
),
4482 convert (ctype
, c
), 0);
4483 /* We allow the constant to overflow with wrapping semantics. */
4485 || (TREE_OVERFLOW (op1
) && ! flag_wrapv
))
4491 /* If we have an unsigned type is not a sizetype, we cannot widen
4492 the operation since it will change the result if the original
4493 computation overflowed. */
4494 if (TREE_UNSIGNED (ctype
)
4495 && ! (TREE_CODE (ctype
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (ctype
))
4499 /* If we were able to eliminate our operation from the first side,
4500 apply our operation to the second side and reform the PLUS. */
4501 if (t1
!= 0 && (TREE_CODE (t1
) != code
|| code
== MULT_EXPR
))
4502 return fold (build (tcode
, ctype
, convert (ctype
, t1
), op1
));
4504 /* The last case is if we are a multiply. In that case, we can
4505 apply the distributive law to commute the multiply and addition
4506 if the multiplication of the constants doesn't overflow. */
4507 if (code
== MULT_EXPR
)
4508 return fold (build (tcode
, ctype
, fold (build (code
, ctype
,
4509 convert (ctype
, op0
),
4510 convert (ctype
, c
))),
4516 /* We have a special case here if we are doing something like
4517 (C * 8) % 4 since we know that's zero. */
4518 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
4519 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
4520 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
4521 && integer_zerop (const_binop (TRUNC_MOD_EXPR
, op1
, c
, 0)))
4522 return omit_one_operand (type
, integer_zero_node
, op0
);
4524 /* ... fall through ... */
4526 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
4527 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
4528 /* If we can extract our operation from the LHS, do so and return a
4529 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
4530 do something only if the second operand is a constant. */
4532 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
)) != 0)
4533 return fold (build (tcode
, ctype
, convert (ctype
, t1
),
4534 convert (ctype
, op1
)));
4535 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
4536 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
)) != 0)
4537 return fold (build (tcode
, ctype
, convert (ctype
, op0
),
4538 convert (ctype
, t1
)));
4539 else if (TREE_CODE (op1
) != INTEGER_CST
)
4542 /* If these are the same operation types, we can associate them
4543 assuming no overflow. */
4545 && 0 != (t1
= const_binop (MULT_EXPR
, convert (ctype
, op1
),
4546 convert (ctype
, c
), 0))
4547 && ! TREE_OVERFLOW (t1
))
4548 return fold (build (tcode
, ctype
, convert (ctype
, op0
), t1
));
4550 /* If these operations "cancel" each other, we have the main
4551 optimizations of this pass, which occur when either constant is a
4552 multiple of the other, in which case we replace this with either an
4553 operation or CODE or TCODE.
4555 If we have an unsigned type that is not a sizetype, we cannot do
4556 this since it will change the result if the original computation
4558 if ((! TREE_UNSIGNED (ctype
)
4559 || (TREE_CODE (ctype
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (ctype
)))
4561 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
4562 || (tcode
== MULT_EXPR
4563 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
4564 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
)))
4566 if (integer_zerop (const_binop (TRUNC_MOD_EXPR
, op1
, c
, 0)))
4567 return fold (build (tcode
, ctype
, convert (ctype
, op0
),
4569 const_binop (TRUNC_DIV_EXPR
,
4571 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR
, c
, op1
, 0)))
4572 return fold (build (code
, ctype
, convert (ctype
, op0
),
4574 const_binop (TRUNC_DIV_EXPR
,
4586 /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
4587 S, a SAVE_EXPR, return the expression actually being evaluated. Note
4588 that we may sometimes modify the tree. */
4591 strip_compound_expr (tree t
, tree s
)
4593 enum tree_code code
= TREE_CODE (t
);
4595 /* See if this is the COMPOUND_EXPR we want to eliminate. */
4596 if (code
== COMPOUND_EXPR
&& TREE_CODE (TREE_OPERAND (t
, 0)) == CONVERT_EXPR
4597 && TREE_OPERAND (TREE_OPERAND (t
, 0), 0) == s
)
4598 return TREE_OPERAND (t
, 1);
4600 /* See if this is a COND_EXPR or a simple arithmetic operator. We
4601 don't bother handling any other types. */
4602 else if (code
== COND_EXPR
)
4604 TREE_OPERAND (t
, 0) = strip_compound_expr (TREE_OPERAND (t
, 0), s
);
4605 TREE_OPERAND (t
, 1) = strip_compound_expr (TREE_OPERAND (t
, 1), s
);
4606 TREE_OPERAND (t
, 2) = strip_compound_expr (TREE_OPERAND (t
, 2), s
);
4608 else if (TREE_CODE_CLASS (code
) == '1')
4609 TREE_OPERAND (t
, 0) = strip_compound_expr (TREE_OPERAND (t
, 0), s
);
4610 else if (TREE_CODE_CLASS (code
) == '<'
4611 || TREE_CODE_CLASS (code
) == '2')
4613 TREE_OPERAND (t
, 0) = strip_compound_expr (TREE_OPERAND (t
, 0), s
);
4614 TREE_OPERAND (t
, 1) = strip_compound_expr (TREE_OPERAND (t
, 1), s
);
4620 /* Return a node which has the indicated constant VALUE (either 0 or
4621 1), and is of the indicated TYPE. */
4624 constant_boolean_node (int value
, tree type
)
4626 if (type
== integer_type_node
)
4627 return value
? integer_one_node
: integer_zero_node
;
4628 else if (TREE_CODE (type
) == BOOLEAN_TYPE
)
4629 return (*lang_hooks
.truthvalue_conversion
) (value
? integer_one_node
:
4633 tree t
= build_int_2 (value
, 0);
4635 TREE_TYPE (t
) = type
;
4640 /* Utility function for the following routine, to see how complex a nesting of
4641 COND_EXPRs can be. EXPR is the expression and LIMIT is a count beyond which
4642 we don't care (to avoid spending too much time on complex expressions.). */
4645 count_cond (tree expr
, int lim
)
4649 if (TREE_CODE (expr
) != COND_EXPR
)
4654 ctrue
= count_cond (TREE_OPERAND (expr
, 1), lim
- 1);
4655 cfalse
= count_cond (TREE_OPERAND (expr
, 2), lim
- 1 - ctrue
);
4656 return MIN (lim
, 1 + ctrue
+ cfalse
);
4659 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
4660 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
4661 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
4662 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
4663 COND is the first argument to CODE; otherwise (as in the example
4664 given here), it is the second argument. TYPE is the type of the
4665 original expression. */
4668 fold_binary_op_with_conditional_arg (enum tree_code code
, tree type
,
4669 tree cond
, tree arg
, int cond_first_p
)
4671 tree test
, true_value
, false_value
;
4672 tree lhs
= NULL_TREE
;
4673 tree rhs
= NULL_TREE
;
4674 /* In the end, we'll produce a COND_EXPR. Both arms of the
4675 conditional expression will be binary operations. The left-hand
4676 side of the expression to be executed if the condition is true
4677 will be pointed to by TRUE_LHS. Similarly, the right-hand side
4678 of the expression to be executed if the condition is true will be
4679 pointed to by TRUE_RHS. FALSE_LHS and FALSE_RHS are analogous --
4680 but apply to the expression to be executed if the conditional is
4686 /* These are the codes to use for the left-hand side and right-hand
4687 side of the COND_EXPR. Normally, they are the same as CODE. */
4688 enum tree_code lhs_code
= code
;
4689 enum tree_code rhs_code
= code
;
4690 /* And these are the types of the expressions. */
4691 tree lhs_type
= type
;
4692 tree rhs_type
= type
;
4697 true_rhs
= false_rhs
= &arg
;
4698 true_lhs
= &true_value
;
4699 false_lhs
= &false_value
;
4703 true_lhs
= false_lhs
= &arg
;
4704 true_rhs
= &true_value
;
4705 false_rhs
= &false_value
;
4708 if (TREE_CODE (cond
) == COND_EXPR
)
4710 test
= TREE_OPERAND (cond
, 0);
4711 true_value
= TREE_OPERAND (cond
, 1);
4712 false_value
= TREE_OPERAND (cond
, 2);
4713 /* If this operand throws an expression, then it does not make
4714 sense to try to perform a logical or arithmetic operation
4715 involving it. Instead of building `a + throw 3' for example,
4716 we simply build `a, throw 3'. */
4717 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
4721 lhs_code
= COMPOUND_EXPR
;
4722 lhs_type
= void_type_node
;
4727 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
4731 rhs_code
= COMPOUND_EXPR
;
4732 rhs_type
= void_type_node
;
4740 tree testtype
= TREE_TYPE (cond
);
4742 true_value
= convert (testtype
, integer_one_node
);
4743 false_value
= convert (testtype
, integer_zero_node
);
4746 /* If ARG is complex we want to make sure we only evaluate it once. Though
4747 this is only required if it is volatile, it might be more efficient even
4748 if it is not. However, if we succeed in folding one part to a constant,
4749 we do not need to make this SAVE_EXPR. Since we do this optimization
4750 primarily to see if we do end up with constant and this SAVE_EXPR
4751 interferes with later optimizations, suppressing it when we can is
4754 If we are not in a function, we can't make a SAVE_EXPR, so don't try to
4755 do so. Don't try to see if the result is a constant if an arm is a
4756 COND_EXPR since we get exponential behavior in that case. */
4758 if (saved_expr_p (arg
))
4760 else if (lhs
== 0 && rhs
== 0
4761 && !TREE_CONSTANT (arg
)
4762 && (*lang_hooks
.decls
.global_bindings_p
) () == 0
4763 && ((TREE_CODE (arg
) != VAR_DECL
&& TREE_CODE (arg
) != PARM_DECL
)
4764 || TREE_SIDE_EFFECTS (arg
)))
4766 if (TREE_CODE (true_value
) != COND_EXPR
)
4767 lhs
= fold (build (lhs_code
, lhs_type
, *true_lhs
, *true_rhs
));
4769 if (TREE_CODE (false_value
) != COND_EXPR
)
4770 rhs
= fold (build (rhs_code
, rhs_type
, *false_lhs
, *false_rhs
));
4772 if ((lhs
== 0 || ! TREE_CONSTANT (lhs
))
4773 && (rhs
== 0 || !TREE_CONSTANT (rhs
)))
4775 arg
= save_expr (arg
);
4777 save
= saved_expr_p (arg
);
4782 lhs
= fold (build (lhs_code
, lhs_type
, *true_lhs
, *true_rhs
));
4784 rhs
= fold (build (rhs_code
, rhs_type
, *false_lhs
, *false_rhs
));
4786 test
= fold (build (COND_EXPR
, type
, test
, lhs
, rhs
));
4788 /* If ARG involves a SAVE_EXPR, we need to ensure it is evaluated
4789 ahead of the COND_EXPR we made. Otherwise we would have it only
4790 evaluated in one branch, with the other branch using the result
4791 but missing the evaluation code. Beware that the save_expr call
4792 above might not return a SAVE_EXPR, so testing the TREE_CODE
4793 of ARG is not enough to decide here. Â */
4795 return build (COMPOUND_EXPR
, type
,
4796 convert (void_type_node
, arg
),
4797 strip_compound_expr (test
, arg
));
4799 return convert (type
, test
);
4803 /* Subroutine of fold() that checks for the addition of +/- 0.0.
4805 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
4806 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
4807 ADDEND is the same as X.
4809 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
4810 and finite. The problematic cases are when X is zero, and its mode
4811 has signed zeros. In the case of rounding towards -infinity,
4812 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
4813 modes, X + 0 is not the same as X because -0 + 0 is 0. */
4816 fold_real_zero_addition_p (tree type
, tree addend
, int negate
)
4818 if (!real_zerop (addend
))
4821 /* Don't allow the fold with -fsignaling-nans. */
4822 if (HONOR_SNANS (TYPE_MODE (type
)))
4825 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
4826 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type
)))
4829 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
4830 if (TREE_CODE (addend
) == REAL_CST
4831 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend
)))
4834 /* The mode has signed zeros, and we have to honor their sign.
4835 In this situation, there is only one case we can return true for.
4836 X - 0 is the same as X unless rounding towards -infinity is
4838 return negate
&& !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type
));
4841 /* Subroutine of fold() that checks comparisons of built-in math
4842 functions against real constants.
4844 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
4845 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
4846 is the type of the result and ARG0 and ARG1 are the operands of the
4847 comparison. ARG1 must be a TREE_REAL_CST.
4849 The function returns the constant folded tree if a simplification
4850 can be made, and NULL_TREE otherwise. */
4853 fold_mathfn_compare (enum built_in_function fcode
, enum tree_code code
,
4854 tree type
, tree arg0
, tree arg1
)
4858 if (fcode
== BUILT_IN_SQRT
4859 || fcode
== BUILT_IN_SQRTF
4860 || fcode
== BUILT_IN_SQRTL
)
4862 tree arg
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
4863 enum machine_mode mode
= TYPE_MODE (TREE_TYPE (arg0
));
4865 c
= TREE_REAL_CST (arg1
);
4866 if (REAL_VALUE_NEGATIVE (c
))
4868 /* sqrt(x) < y is always false, if y is negative. */
4869 if (code
== EQ_EXPR
|| code
== LT_EXPR
|| code
== LE_EXPR
)
4870 return omit_one_operand (type
,
4871 convert (type
, integer_zero_node
),
4874 /* sqrt(x) > y is always true, if y is negative and we
4875 don't care about NaNs, i.e. negative values of x. */
4876 if (code
== NE_EXPR
|| !HONOR_NANS (mode
))
4877 return omit_one_operand (type
,
4878 convert (type
, integer_one_node
),
4881 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
4882 return fold (build (GE_EXPR
, type
, arg
,
4883 build_real (TREE_TYPE (arg
), dconst0
)));
4885 else if (code
== GT_EXPR
|| code
== GE_EXPR
)
4889 REAL_ARITHMETIC (c2
, MULT_EXPR
, c
, c
);
4890 real_convert (&c2
, mode
, &c2
);
4892 if (REAL_VALUE_ISINF (c2
))
4894 /* sqrt(x) > y is x == +Inf, when y is very large. */
4895 if (HONOR_INFINITIES (mode
))
4896 return fold (build (EQ_EXPR
, type
, arg
,
4897 build_real (TREE_TYPE (arg
), c2
)));
4899 /* sqrt(x) > y is always false, when y is very large
4900 and we don't care about infinities. */
4901 return omit_one_operand (type
,
4902 convert (type
, integer_zero_node
),
4906 /* sqrt(x) > c is the same as x > c*c. */
4907 return fold (build (code
, type
, arg
,
4908 build_real (TREE_TYPE (arg
), c2
)));
4910 else if (code
== LT_EXPR
|| code
== LE_EXPR
)
4914 REAL_ARITHMETIC (c2
, MULT_EXPR
, c
, c
);
4915 real_convert (&c2
, mode
, &c2
);
4917 if (REAL_VALUE_ISINF (c2
))
4919 /* sqrt(x) < y is always true, when y is a very large
4920 value and we don't care about NaNs or Infinities. */
4921 if (! HONOR_NANS (mode
) && ! HONOR_INFINITIES (mode
))
4922 return omit_one_operand (type
,
4923 convert (type
, integer_one_node
),
4926 /* sqrt(x) < y is x != +Inf when y is very large and we
4927 don't care about NaNs. */
4928 if (! HONOR_NANS (mode
))
4929 return fold (build (NE_EXPR
, type
, arg
,
4930 build_real (TREE_TYPE (arg
), c2
)));
4932 /* sqrt(x) < y is x >= 0 when y is very large and we
4933 don't care about Infinities. */
4934 if (! HONOR_INFINITIES (mode
))
4935 return fold (build (GE_EXPR
, type
, arg
,
4936 build_real (TREE_TYPE (arg
), dconst0
)));
4938 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
4939 if ((*lang_hooks
.decls
.global_bindings_p
) () != 0
4940 || CONTAINS_PLACEHOLDER_P (arg
))
4943 arg
= save_expr (arg
);
4944 return fold (build (TRUTH_ANDIF_EXPR
, type
,
4945 fold (build (GE_EXPR
, type
, arg
,
4946 build_real (TREE_TYPE (arg
),
4948 fold (build (NE_EXPR
, type
, arg
,
4949 build_real (TREE_TYPE (arg
),
4953 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
4954 if (! HONOR_NANS (mode
))
4955 return fold (build (code
, type
, arg
,
4956 build_real (TREE_TYPE (arg
), c2
)));
4958 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
4959 if ((*lang_hooks
.decls
.global_bindings_p
) () == 0
4960 && ! CONTAINS_PLACEHOLDER_P (arg
))
4962 arg
= save_expr (arg
);
4963 return fold (build (TRUTH_ANDIF_EXPR
, type
,
4964 fold (build (GE_EXPR
, type
, arg
,
4965 build_real (TREE_TYPE (arg
),
4967 fold (build (code
, type
, arg
,
4968 build_real (TREE_TYPE (arg
),
4977 /* Subroutine of fold() that optimizes comparisons against Infinities,
4978 either +Inf or -Inf.
4980 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
4981 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
4982 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
4984 The function returns the constant folded tree if a simplification
4985 can be made, and NULL_TREE otherwise. */
4988 fold_inf_compare (enum tree_code code
, tree type
, tree arg0
, tree arg1
)
4990 enum machine_mode mode
;
4991 REAL_VALUE_TYPE max
;
4995 mode
= TYPE_MODE (TREE_TYPE (arg0
));
4997 /* For negative infinity swap the sense of the comparison. */
4998 neg
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1
));
5000 code
= swap_tree_comparison (code
);
5005 /* x > +Inf is always false, if with ignore sNANs. */
5006 if (HONOR_SNANS (mode
))
5008 return omit_one_operand (type
,
5009 convert (type
, integer_zero_node
),
5013 /* x <= +Inf is always true, if we don't case about NaNs. */
5014 if (! HONOR_NANS (mode
))
5015 return omit_one_operand (type
,
5016 convert (type
, integer_one_node
),
5019 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
5020 if ((*lang_hooks
.decls
.global_bindings_p
) () == 0
5021 && ! CONTAINS_PLACEHOLDER_P (arg0
))
5023 arg0
= save_expr (arg0
);
5024 return fold (build (EQ_EXPR
, type
, arg0
, arg0
));
5030 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
5031 real_maxval (&max
, neg
, mode
);
5032 return fold (build (neg
? LT_EXPR
: GT_EXPR
, type
,
5033 arg0
, build_real (TREE_TYPE (arg0
), max
)));
5036 /* x < +Inf is always equal to x <= DBL_MAX. */
5037 real_maxval (&max
, neg
, mode
);
5038 return fold (build (neg
? GE_EXPR
: LE_EXPR
, type
,
5039 arg0
, build_real (TREE_TYPE (arg0
), max
)));
5042 /* x != +Inf is always equal to !(x > DBL_MAX). */
5043 real_maxval (&max
, neg
, mode
);
5044 if (! HONOR_NANS (mode
))
5045 return fold (build (neg
? GE_EXPR
: LE_EXPR
, type
,
5046 arg0
, build_real (TREE_TYPE (arg0
), max
)));
5047 temp
= fold (build (neg
? LT_EXPR
: GT_EXPR
, type
,
5048 arg0
, build_real (TREE_TYPE (arg0
), max
)));
5049 return fold (build1 (TRUTH_NOT_EXPR
, type
, temp
));
5058 /* If CODE with arguments ARG0 and ARG1 represents a single bit
5059 equality/inequality test, then return a simplified form of
5060 the test using shifts and logical operations. Otherwise return
5061 NULL. TYPE is the desired result type. */
5064 fold_single_bit_test (enum tree_code code
, tree arg0
, tree arg1
,
5067 /* If this is a TRUTH_NOT_EXPR, it may have a single bit test inside
5069 if (code
== TRUTH_NOT_EXPR
)
5071 code
= TREE_CODE (arg0
);
5072 if (code
!= NE_EXPR
&& code
!= EQ_EXPR
)
5075 /* Extract the arguments of the EQ/NE. */
5076 arg1
= TREE_OPERAND (arg0
, 1);
5077 arg0
= TREE_OPERAND (arg0
, 0);
5079 /* This requires us to invert the code. */
5080 code
= (code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
);
5083 /* If this is testing a single bit, we can optimize the test. */
5084 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
5085 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
5086 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
5088 tree inner
= TREE_OPERAND (arg0
, 0);
5089 tree type
= TREE_TYPE (arg0
);
5090 int bitnum
= tree_log2 (TREE_OPERAND (arg0
, 1));
5091 enum machine_mode operand_mode
= TYPE_MODE (type
);
5093 tree signed_type
, unsigned_type
, intermediate_type
;
5096 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5097 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5098 arg00
= sign_bit_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1));
5099 if (arg00
!= NULL_TREE
)
5101 tree stype
= (*lang_hooks
.types
.signed_type
) (TREE_TYPE (arg00
));
5102 return fold (build (code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
, result_type
,
5103 convert (stype
, arg00
),
5104 convert (stype
, integer_zero_node
)));
5107 /* At this point, we know that arg0 is not testing the sign bit. */
5108 if (TYPE_PRECISION (type
) - 1 == bitnum
)
5111 /* Otherwise we have (A & C) != 0 where C is a single bit,
5112 convert that into ((A >> C2) & 1). Where C2 = log2(C).
5113 Similarly for (A & C) == 0. */
5115 /* If INNER is a right shift of a constant and it plus BITNUM does
5116 not overflow, adjust BITNUM and INNER. */
5117 if (TREE_CODE (inner
) == RSHIFT_EXPR
5118 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
5119 && TREE_INT_CST_HIGH (TREE_OPERAND (inner
, 1)) == 0
5120 && bitnum
< TYPE_PRECISION (type
)
5121 && 0 > compare_tree_int (TREE_OPERAND (inner
, 1),
5122 bitnum
- TYPE_PRECISION (type
)))
5124 bitnum
+= TREE_INT_CST_LOW (TREE_OPERAND (inner
, 1));
5125 inner
= TREE_OPERAND (inner
, 0);
5128 /* If we are going to be able to omit the AND below, we must do our
5129 operations as unsigned. If we must use the AND, we have a choice.
5130 Normally unsigned is faster, but for some machines signed is. */
5131 #ifdef LOAD_EXTEND_OP
5132 ops_unsigned
= (LOAD_EXTEND_OP (operand_mode
) == SIGN_EXTEND
? 0 : 1);
5137 signed_type
= (*lang_hooks
.types
.type_for_mode
) (operand_mode
, 0);
5138 unsigned_type
= (*lang_hooks
.types
.type_for_mode
) (operand_mode
, 1);
5139 intermediate_type
= ops_unsigned
? unsigned_type
: signed_type
;
5140 inner
= convert (intermediate_type
, inner
);
5143 inner
= build (RSHIFT_EXPR
, intermediate_type
,
5144 inner
, size_int (bitnum
));
5146 if (code
== EQ_EXPR
)
5147 inner
= build (BIT_XOR_EXPR
, intermediate_type
,
5148 inner
, integer_one_node
);
5150 /* Put the AND last so it can combine with more things. */
5151 inner
= build (BIT_AND_EXPR
, intermediate_type
,
5152 inner
, integer_one_node
);
5154 /* Make sure to return the proper type. */
5155 inner
= convert (result_type
, inner
);
5162 /* Check whether we are allowed to reorder operands arg0 and arg1,
5163 such that the evaluation of arg1 occurs before arg0. */
5166 reorder_operands_p (tree arg0
, tree arg1
)
5168 if (! flag_evaluation_order
)
5170 if (TREE_CONSTANT (arg0
) || TREE_CONSTANT (arg1
))
5172 return ! TREE_SIDE_EFFECTS (arg0
)
5173 && ! TREE_SIDE_EFFECTS (arg1
);
5176 /* Test whether it is preferable two swap two operands, ARG0 and
5177 ARG1, for example because ARG0 is an integer constant and ARG1
5178 isn't. If REORDER is true, only recommend swapping if we can
5179 evaluate the operands in reverse order. */
5182 tree_swap_operands_p (tree arg0
, tree arg1
, bool reorder
)
5184 STRIP_SIGN_NOPS (arg0
);
5185 STRIP_SIGN_NOPS (arg1
);
5187 if (TREE_CODE (arg1
) == INTEGER_CST
)
5189 if (TREE_CODE (arg0
) == INTEGER_CST
)
5192 if (TREE_CODE (arg1
) == REAL_CST
)
5194 if (TREE_CODE (arg0
) == REAL_CST
)
5197 if (TREE_CODE (arg1
) == COMPLEX_CST
)
5199 if (TREE_CODE (arg0
) == COMPLEX_CST
)
5202 if (TREE_CONSTANT (arg1
))
5204 if (TREE_CONSTANT (arg0
))
5210 if (reorder
&& flag_evaluation_order
5211 && (TREE_SIDE_EFFECTS (arg0
) || TREE_SIDE_EFFECTS (arg1
)))
5222 /* Perform constant folding and related simplification of EXPR.
5223 The related simplifications include x*1 => x, x*0 => 0, etc.,
5224 and application of the associative law.
5225 NOP_EXPR conversions may be removed freely (as long as we
5226 are careful not to change the C type of the overall expression)
5227 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
5228 but we can constant-fold them if they have constant operands. */
5230 #ifdef ENABLE_FOLD_CHECKING
5231 # define fold(x) fold_1 (x)
5232 static tree
fold_1 (tree
);
5238 tree t
= expr
, orig_t
;
5239 tree t1
= NULL_TREE
;
5241 tree type
= TREE_TYPE (expr
);
5242 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
;
5243 enum tree_code code
= TREE_CODE (t
);
5244 int kind
= TREE_CODE_CLASS (code
);
5246 /* WINS will be nonzero when the switch is done
5247 if all operands are constant. */
5250 /* Don't try to process an RTL_EXPR since its operands aren't trees.
5251 Likewise for a SAVE_EXPR that's already been evaluated. */
5252 if (code
== RTL_EXPR
|| (code
== SAVE_EXPR
&& SAVE_EXPR_RTL (t
) != 0))
5255 /* Return right away if a constant. */
5261 if (code
== NOP_EXPR
|| code
== FLOAT_EXPR
|| code
== CONVERT_EXPR
)
5265 /* Special case for conversion ops that can have fixed point args. */
5266 arg0
= TREE_OPERAND (t
, 0);
5268 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
5270 STRIP_SIGN_NOPS (arg0
);
5272 if (arg0
!= 0 && TREE_CODE (arg0
) == COMPLEX_CST
)
5273 subop
= TREE_REALPART (arg0
);
5277 if (subop
!= 0 && TREE_CODE (subop
) != INTEGER_CST
5278 && TREE_CODE (subop
) != REAL_CST
)
5279 /* Note that TREE_CONSTANT isn't enough:
5280 static var addresses are constant but we can't
5281 do arithmetic on them. */
5284 else if (IS_EXPR_CODE_CLASS (kind
))
5286 int len
= first_rtl_op (code
);
5288 for (i
= 0; i
< len
; i
++)
5290 tree op
= TREE_OPERAND (t
, i
);
5294 continue; /* Valid for CALL_EXPR, at least. */
5296 if (kind
== '<' || code
== RSHIFT_EXPR
)
5298 /* Signedness matters here. Perhaps we can refine this
5300 STRIP_SIGN_NOPS (op
);
5303 /* Strip any conversions that don't change the mode. */
5306 if (TREE_CODE (op
) == COMPLEX_CST
)
5307 subop
= TREE_REALPART (op
);
5311 if (TREE_CODE (subop
) != INTEGER_CST
5312 && TREE_CODE (subop
) != REAL_CST
)
5313 /* Note that TREE_CONSTANT isn't enough:
5314 static var addresses are constant but we can't
5315 do arithmetic on them. */
5325 /* If this is a commutative operation, and ARG0 is a constant, move it
5326 to ARG1 to reduce the number of tests below. */
5327 if (commutative_tree_code (code
)
5328 && tree_swap_operands_p (arg0
, arg1
, true))
5329 return fold (build (code
, type
, arg1
, arg0
));
5331 /* Now WINS is set as described above,
5332 ARG0 is the first operand of EXPR,
5333 and ARG1 is the second operand (if it has more than one operand).
5335 First check for cases where an arithmetic operation is applied to a
5336 compound, conditional, or comparison operation. Push the arithmetic
5337 operation inside the compound or conditional to see if any folding
5338 can then be done. Convert comparison to conditional for this purpose.
5339 The also optimizes non-constant cases that used to be done in
5342 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
5343 one of the operands is a comparison and the other is a comparison, a
5344 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
5345 code below would make the expression more complex. Change it to a
5346 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
5347 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
5349 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
5350 || code
== EQ_EXPR
|| code
== NE_EXPR
)
5351 && ((truth_value_p (TREE_CODE (arg0
))
5352 && (truth_value_p (TREE_CODE (arg1
))
5353 || (TREE_CODE (arg1
) == BIT_AND_EXPR
5354 && integer_onep (TREE_OPERAND (arg1
, 1)))))
5355 || (truth_value_p (TREE_CODE (arg1
))
5356 && (truth_value_p (TREE_CODE (arg0
))
5357 || (TREE_CODE (arg0
) == BIT_AND_EXPR
5358 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
5360 t
= fold (build (code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
5361 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
5365 if (code
== EQ_EXPR
)
5366 t
= invert_truthvalue (t
);
5371 if (TREE_CODE_CLASS (code
) == '1')
5373 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
5374 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
5375 fold (build1 (code
, type
, TREE_OPERAND (arg0
, 1))));
5376 else if (TREE_CODE (arg0
) == COND_EXPR
)
5378 tree arg01
= TREE_OPERAND (arg0
, 1);
5379 tree arg02
= TREE_OPERAND (arg0
, 2);
5380 if (! VOID_TYPE_P (TREE_TYPE (arg01
)))
5381 arg01
= fold (build1 (code
, type
, arg01
));
5382 if (! VOID_TYPE_P (TREE_TYPE (arg02
)))
5383 arg02
= fold (build1 (code
, type
, arg02
));
5384 t
= fold (build (COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
5387 /* If this was a conversion, and all we did was to move into
5388 inside the COND_EXPR, bring it back out. But leave it if
5389 it is a conversion from integer to integer and the
5390 result precision is no wider than a word since such a
5391 conversion is cheap and may be optimized away by combine,
5392 while it couldn't if it were outside the COND_EXPR. Then return
5393 so we don't get into an infinite recursion loop taking the
5394 conversion out and then back in. */
5396 if ((code
== NOP_EXPR
|| code
== CONVERT_EXPR
5397 || code
== NON_LVALUE_EXPR
)
5398 && TREE_CODE (t
) == COND_EXPR
5399 && TREE_CODE (TREE_OPERAND (t
, 1)) == code
5400 && TREE_CODE (TREE_OPERAND (t
, 2)) == code
5401 && ! VOID_TYPE_P (TREE_OPERAND (t
, 1))
5402 && ! VOID_TYPE_P (TREE_OPERAND (t
, 2))
5403 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 1), 0))
5404 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 2), 0)))
5405 && ! (INTEGRAL_TYPE_P (TREE_TYPE (t
))
5407 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 1), 0))))
5408 && TYPE_PRECISION (TREE_TYPE (t
)) <= BITS_PER_WORD
))
5409 t
= build1 (code
, type
,
5411 TREE_TYPE (TREE_OPERAND
5412 (TREE_OPERAND (t
, 1), 0)),
5413 TREE_OPERAND (t
, 0),
5414 TREE_OPERAND (TREE_OPERAND (t
, 1), 0),
5415 TREE_OPERAND (TREE_OPERAND (t
, 2), 0)));
5418 else if (TREE_CODE_CLASS (TREE_CODE (arg0
)) == '<')
5419 return fold (build (COND_EXPR
, type
, arg0
,
5420 fold (build1 (code
, type
, integer_one_node
)),
5421 fold (build1 (code
, type
, integer_zero_node
))));
5423 else if (TREE_CODE_CLASS (code
) == '<'
5424 && TREE_CODE (arg0
) == COMPOUND_EXPR
)
5425 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
5426 fold (build (code
, type
, TREE_OPERAND (arg0
, 1), arg1
)));
5427 else if (TREE_CODE_CLASS (code
) == '<'
5428 && TREE_CODE (arg1
) == COMPOUND_EXPR
)
5429 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
5430 fold (build (code
, type
, arg0
, TREE_OPERAND (arg1
, 1))));
5431 else if (TREE_CODE_CLASS (code
) == '2'
5432 || TREE_CODE_CLASS (code
) == '<')
5434 if (TREE_CODE (arg1
) == COMPOUND_EXPR
5435 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg1
, 0))
5436 && ! TREE_SIDE_EFFECTS (arg0
))
5437 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
5438 fold (build (code
, type
,
5439 arg0
, TREE_OPERAND (arg1
, 1))));
5440 else if ((TREE_CODE (arg1
) == COND_EXPR
5441 || (TREE_CODE_CLASS (TREE_CODE (arg1
)) == '<'
5442 && TREE_CODE_CLASS (code
) != '<'))
5443 && (TREE_CODE (arg0
) != COND_EXPR
5444 || count_cond (arg0
, 25) + count_cond (arg1
, 25) <= 25)
5445 && (! TREE_SIDE_EFFECTS (arg0
)
5446 || ((*lang_hooks
.decls
.global_bindings_p
) () == 0
5447 && ! CONTAINS_PLACEHOLDER_P (arg0
))))
5449 fold_binary_op_with_conditional_arg (code
, type
, arg1
, arg0
,
5450 /*cond_first_p=*/0);
5451 else if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
5452 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
5453 fold (build (code
, type
, TREE_OPERAND (arg0
, 1), arg1
)));
5454 else if ((TREE_CODE (arg0
) == COND_EXPR
5455 || (TREE_CODE_CLASS (TREE_CODE (arg0
)) == '<'
5456 && TREE_CODE_CLASS (code
) != '<'))
5457 && (TREE_CODE (arg1
) != COND_EXPR
5458 || count_cond (arg0
, 25) + count_cond (arg1
, 25) <= 25)
5459 && (! TREE_SIDE_EFFECTS (arg1
)
5460 || ((*lang_hooks
.decls
.global_bindings_p
) () == 0
5461 && ! CONTAINS_PLACEHOLDER_P (arg1
))))
5463 fold_binary_op_with_conditional_arg (code
, type
, arg0
, arg1
,
5464 /*cond_first_p=*/1);
5478 return fold (DECL_INITIAL (t
));
5483 case FIX_TRUNC_EXPR
:
5485 case FIX_FLOOR_EXPR
:
5486 if (TREE_TYPE (TREE_OPERAND (t
, 0)) == TREE_TYPE (t
))
5487 return TREE_OPERAND (t
, 0);
5489 /* Handle cases of two conversions in a row. */
5490 if (TREE_CODE (TREE_OPERAND (t
, 0)) == NOP_EXPR
5491 || TREE_CODE (TREE_OPERAND (t
, 0)) == CONVERT_EXPR
)
5493 tree inside_type
= TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0));
5494 tree inter_type
= TREE_TYPE (TREE_OPERAND (t
, 0));
5495 tree final_type
= TREE_TYPE (t
);
5496 int inside_int
= INTEGRAL_TYPE_P (inside_type
);
5497 int inside_ptr
= POINTER_TYPE_P (inside_type
);
5498 int inside_float
= FLOAT_TYPE_P (inside_type
);
5499 unsigned int inside_prec
= TYPE_PRECISION (inside_type
);
5500 int inside_unsignedp
= TREE_UNSIGNED (inside_type
);
5501 int inter_int
= INTEGRAL_TYPE_P (inter_type
);
5502 int inter_ptr
= POINTER_TYPE_P (inter_type
);
5503 int inter_float
= FLOAT_TYPE_P (inter_type
);
5504 unsigned int inter_prec
= TYPE_PRECISION (inter_type
);
5505 int inter_unsignedp
= TREE_UNSIGNED (inter_type
);
5506 int final_int
= INTEGRAL_TYPE_P (final_type
);
5507 int final_ptr
= POINTER_TYPE_P (final_type
);
5508 int final_float
= FLOAT_TYPE_P (final_type
);
5509 unsigned int final_prec
= TYPE_PRECISION (final_type
);
5510 int final_unsignedp
= TREE_UNSIGNED (final_type
);
5512 /* In addition to the cases of two conversions in a row
5513 handled below, if we are converting something to its own
5514 type via an object of identical or wider precision, neither
5515 conversion is needed. */
5516 if (TYPE_MAIN_VARIANT (inside_type
) == TYPE_MAIN_VARIANT (final_type
)
5517 && ((inter_int
&& final_int
) || (inter_float
&& final_float
))
5518 && inter_prec
>= final_prec
)
5519 return convert (final_type
, TREE_OPERAND (TREE_OPERAND (t
, 0), 0));
5521 /* Likewise, if the intermediate and final types are either both
5522 float or both integer, we don't need the middle conversion if
5523 it is wider than the final type and doesn't change the signedness
5524 (for integers). Avoid this if the final type is a pointer
5525 since then we sometimes need the inner conversion. Likewise if
5526 the outer has a precision not equal to the size of its mode. */
5527 if ((((inter_int
|| inter_ptr
) && (inside_int
|| inside_ptr
))
5528 || (inter_float
&& inside_float
))
5529 && inter_prec
>= inside_prec
5530 && (inter_float
|| inter_unsignedp
== inside_unsignedp
)
5531 && ! (final_prec
!= GET_MODE_BITSIZE (TYPE_MODE (final_type
))
5532 && TYPE_MODE (final_type
) == TYPE_MODE (inter_type
))
5534 return convert (final_type
, TREE_OPERAND (TREE_OPERAND (t
, 0), 0));
5536 /* If we have a sign-extension of a zero-extended value, we can
5537 replace that by a single zero-extension. */
5538 if (inside_int
&& inter_int
&& final_int
5539 && inside_prec
< inter_prec
&& inter_prec
< final_prec
5540 && inside_unsignedp
&& !inter_unsignedp
)
5541 return convert (final_type
, TREE_OPERAND (TREE_OPERAND (t
, 0), 0));
5543 /* Two conversions in a row are not needed unless:
5544 - some conversion is floating-point (overstrict for now), or
5545 - the intermediate type is narrower than both initial and
5547 - the intermediate type and innermost type differ in signedness,
5548 and the outermost type is wider than the intermediate, or
5549 - the initial type is a pointer type and the precisions of the
5550 intermediate and final types differ, or
5551 - the final type is a pointer type and the precisions of the
5552 initial and intermediate types differ. */
5553 if (! inside_float
&& ! inter_float
&& ! final_float
5554 && (inter_prec
> inside_prec
|| inter_prec
> final_prec
)
5555 && ! (inside_int
&& inter_int
5556 && inter_unsignedp
!= inside_unsignedp
5557 && inter_prec
< final_prec
)
5558 && ((inter_unsignedp
&& inter_prec
> inside_prec
)
5559 == (final_unsignedp
&& final_prec
> inter_prec
))
5560 && ! (inside_ptr
&& inter_prec
!= final_prec
)
5561 && ! (final_ptr
&& inside_prec
!= inter_prec
)
5562 && ! (final_prec
!= GET_MODE_BITSIZE (TYPE_MODE (final_type
))
5563 && TYPE_MODE (final_type
) == TYPE_MODE (inter_type
))
5565 return convert (final_type
, TREE_OPERAND (TREE_OPERAND (t
, 0), 0));
5568 if (TREE_CODE (TREE_OPERAND (t
, 0)) == MODIFY_EXPR
5569 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t
, 0), 1))
5570 /* Detect assigning a bitfield. */
5571 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)) == COMPONENT_REF
5572 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 1))))
5574 /* Don't leave an assignment inside a conversion
5575 unless assigning a bitfield. */
5576 tree prev
= TREE_OPERAND (t
, 0);
5579 TREE_OPERAND (t
, 0) = TREE_OPERAND (prev
, 1);
5580 /* First do the assignment, then return converted constant. */
5581 t
= build (COMPOUND_EXPR
, TREE_TYPE (t
), prev
, fold (t
));
5586 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
5587 constants (if x has signed type, the sign bit cannot be set
5588 in c). This folds extension into the BIT_AND_EXPR. */
5589 if (INTEGRAL_TYPE_P (TREE_TYPE (t
))
5590 && TREE_CODE (TREE_TYPE (t
)) != BOOLEAN_TYPE
5591 && TREE_CODE (TREE_OPERAND (t
, 0)) == BIT_AND_EXPR
5592 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 1)) == INTEGER_CST
)
5594 tree
and = TREE_OPERAND (t
, 0);
5595 tree and0
= TREE_OPERAND (and, 0), and1
= TREE_OPERAND (and, 1);
5598 if (TREE_UNSIGNED (TREE_TYPE (and))
5599 || (TYPE_PRECISION (TREE_TYPE (t
))
5600 <= TYPE_PRECISION (TREE_TYPE (and))))
5602 else if (TYPE_PRECISION (TREE_TYPE (and1
))
5603 <= HOST_BITS_PER_WIDE_INT
5604 && host_integerp (and1
, 1))
5606 unsigned HOST_WIDE_INT cst
;
5608 cst
= tree_low_cst (and1
, 1);
5609 cst
&= (HOST_WIDE_INT
) -1
5610 << (TYPE_PRECISION (TREE_TYPE (and1
)) - 1);
5611 change
= (cst
== 0);
5612 #ifdef LOAD_EXTEND_OP
5614 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0
)))
5617 tree uns
= (*lang_hooks
.types
.unsigned_type
) (TREE_TYPE (and0
));
5618 and0
= convert (uns
, and0
);
5619 and1
= convert (uns
, and1
);
5624 return fold (build (BIT_AND_EXPR
, TREE_TYPE (t
),
5625 convert (TREE_TYPE (t
), and0
),
5626 convert (TREE_TYPE (t
), and1
)));
5629 tem
= fold_convert_const (code
, TREE_TYPE (t
), arg0
);
5630 return tem
? tem
: t
;
5632 case VIEW_CONVERT_EXPR
:
5633 if (TREE_CODE (TREE_OPERAND (t
, 0)) == VIEW_CONVERT_EXPR
)
5634 return build1 (VIEW_CONVERT_EXPR
, type
,
5635 TREE_OPERAND (TREE_OPERAND (t
, 0), 0));
5639 if (TREE_CODE (arg0
) == CONSTRUCTOR
5640 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
5642 tree m
= purpose_member (arg1
, CONSTRUCTOR_ELTS (arg0
));
5649 if (TREE_CONSTANT (t
) != wins
)
5653 TREE_CONSTANT (t
) = wins
;
5658 if (negate_expr_p (arg0
))
5659 return negate_expr (arg0
);
5665 if (TREE_CODE (arg0
) == INTEGER_CST
)
5667 /* If the value is unsigned, then the absolute value is
5668 the same as the ordinary value. */
5669 if (TREE_UNSIGNED (type
))
5671 /* Similarly, if the value is non-negative. */
5672 else if (INT_CST_LT (integer_minus_one_node
, arg0
))
5674 /* If the value is negative, then the absolute value is
5678 unsigned HOST_WIDE_INT low
;
5680 int overflow
= neg_double (TREE_INT_CST_LOW (arg0
),
5681 TREE_INT_CST_HIGH (arg0
),
5683 t
= build_int_2 (low
, high
);
5684 TREE_TYPE (t
) = type
;
5686 = (TREE_OVERFLOW (arg0
)
5687 | force_fit_type (t
, overflow
));
5688 TREE_CONSTANT_OVERFLOW (t
)
5689 = TREE_OVERFLOW (t
) | TREE_CONSTANT_OVERFLOW (arg0
);
5692 else if (TREE_CODE (arg0
) == REAL_CST
)
5694 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
5695 t
= build_real (type
,
5696 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0
)));
5699 else if (TREE_CODE (arg0
) == NEGATE_EXPR
)
5700 return fold (build1 (ABS_EXPR
, type
, TREE_OPERAND (arg0
, 0)));
5701 /* Convert fabs((double)float) into (double)fabsf(float). */
5702 else if (TREE_CODE (arg0
) == NOP_EXPR
5703 && TREE_CODE (type
) == REAL_TYPE
)
5705 tree targ0
= strip_float_extensions (arg0
);
5707 return convert (type
, fold (build1 (ABS_EXPR
, TREE_TYPE (targ0
),
5710 else if (tree_expr_nonnegative_p (arg0
))
5715 if (TREE_CODE (TREE_TYPE (arg0
)) != COMPLEX_TYPE
)
5716 return convert (type
, arg0
);
5717 else if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
5718 return build (COMPLEX_EXPR
, type
,
5719 TREE_OPERAND (arg0
, 0),
5720 negate_expr (TREE_OPERAND (arg0
, 1)));
5721 else if (TREE_CODE (arg0
) == COMPLEX_CST
)
5722 return build_complex (type
, TREE_REALPART (arg0
),
5723 negate_expr (TREE_IMAGPART (arg0
)));
5724 else if (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
5725 return fold (build (TREE_CODE (arg0
), type
,
5726 fold (build1 (CONJ_EXPR
, type
,
5727 TREE_OPERAND (arg0
, 0))),
5728 fold (build1 (CONJ_EXPR
,
5729 type
, TREE_OPERAND (arg0
, 1)))));
5730 else if (TREE_CODE (arg0
) == CONJ_EXPR
)
5731 return TREE_OPERAND (arg0
, 0);
5737 t
= build_int_2 (~ TREE_INT_CST_LOW (arg0
),
5738 ~ TREE_INT_CST_HIGH (arg0
));
5739 TREE_TYPE (t
) = type
;
5740 force_fit_type (t
, 0);
5741 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg0
);
5742 TREE_CONSTANT_OVERFLOW (t
) = TREE_CONSTANT_OVERFLOW (arg0
);
5744 else if (TREE_CODE (arg0
) == BIT_NOT_EXPR
)
5745 return TREE_OPERAND (arg0
, 0);
5749 /* A + (-B) -> A - B */
5750 if (TREE_CODE (arg1
) == NEGATE_EXPR
)
5751 return fold (build (MINUS_EXPR
, type
, arg0
, TREE_OPERAND (arg1
, 0)));
5752 /* (-A) + B -> B - A */
5753 if (TREE_CODE (arg0
) == NEGATE_EXPR
)
5754 return fold (build (MINUS_EXPR
, type
, arg1
, TREE_OPERAND (arg0
, 0)));
5755 else if (! FLOAT_TYPE_P (type
))
5757 if (integer_zerop (arg1
))
5758 return non_lvalue (convert (type
, arg0
));
5760 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
5761 with a constant, and the two constants have no bits in common,
5762 we should treat this as a BIT_IOR_EXPR since this may produce more
5764 if (TREE_CODE (arg0
) == BIT_AND_EXPR
5765 && TREE_CODE (arg1
) == BIT_AND_EXPR
5766 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
5767 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
5768 && integer_zerop (const_binop (BIT_AND_EXPR
,
5769 TREE_OPERAND (arg0
, 1),
5770 TREE_OPERAND (arg1
, 1), 0)))
5772 code
= BIT_IOR_EXPR
;
5776 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
5777 (plus (plus (mult) (mult)) (foo)) so that we can
5778 take advantage of the factoring cases below. */
5779 if ((TREE_CODE (arg0
) == PLUS_EXPR
5780 && TREE_CODE (arg1
) == MULT_EXPR
)
5781 || (TREE_CODE (arg1
) == PLUS_EXPR
5782 && TREE_CODE (arg0
) == MULT_EXPR
))
5784 tree parg0
, parg1
, parg
, marg
;
5786 if (TREE_CODE (arg0
) == PLUS_EXPR
)
5787 parg
= arg0
, marg
= arg1
;
5789 parg
= arg1
, marg
= arg0
;
5790 parg0
= TREE_OPERAND (parg
, 0);
5791 parg1
= TREE_OPERAND (parg
, 1);
5795 if (TREE_CODE (parg0
) == MULT_EXPR
5796 && TREE_CODE (parg1
) != MULT_EXPR
)
5797 return fold (build (PLUS_EXPR
, type
,
5798 fold (build (PLUS_EXPR
, type
,
5799 convert (type
, parg0
),
5800 convert (type
, marg
))),
5801 convert (type
, parg1
)));
5802 if (TREE_CODE (parg0
) != MULT_EXPR
5803 && TREE_CODE (parg1
) == MULT_EXPR
)
5804 return fold (build (PLUS_EXPR
, type
,
5805 fold (build (PLUS_EXPR
, type
,
5806 convert (type
, parg1
),
5807 convert (type
, marg
))),
5808 convert (type
, parg0
)));
5811 if (TREE_CODE (arg0
) == MULT_EXPR
&& TREE_CODE (arg1
) == MULT_EXPR
)
5813 tree arg00
, arg01
, arg10
, arg11
;
5814 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
5816 /* (A * C) + (B * C) -> (A+B) * C.
5817 We are most concerned about the case where C is a constant,
5818 but other combinations show up during loop reduction. Since
5819 it is not difficult, try all four possibilities. */
5821 arg00
= TREE_OPERAND (arg0
, 0);
5822 arg01
= TREE_OPERAND (arg0
, 1);
5823 arg10
= TREE_OPERAND (arg1
, 0);
5824 arg11
= TREE_OPERAND (arg1
, 1);
5827 if (operand_equal_p (arg01
, arg11
, 0))
5828 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
5829 else if (operand_equal_p (arg00
, arg10
, 0))
5830 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
5831 else if (operand_equal_p (arg00
, arg11
, 0))
5832 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
5833 else if (operand_equal_p (arg01
, arg10
, 0))
5834 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
5836 /* No identical multiplicands; see if we can find a common
5837 power-of-two factor in non-power-of-two multiplies. This
5838 can help in multi-dimensional array access. */
5839 else if (TREE_CODE (arg01
) == INTEGER_CST
5840 && TREE_CODE (arg11
) == INTEGER_CST
5841 && TREE_INT_CST_HIGH (arg01
) == 0
5842 && TREE_INT_CST_HIGH (arg11
) == 0)
5844 HOST_WIDE_INT int01
, int11
, tmp
;
5845 int01
= TREE_INT_CST_LOW (arg01
);
5846 int11
= TREE_INT_CST_LOW (arg11
);
5848 /* Move min of absolute values to int11. */
5849 if ((int01
>= 0 ? int01
: -int01
)
5850 < (int11
>= 0 ? int11
: -int11
))
5852 tmp
= int01
, int01
= int11
, int11
= tmp
;
5853 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
5854 alt0
= arg01
, arg01
= arg11
, arg11
= alt0
;
5857 if (exact_log2 (int11
) > 0 && int01
% int11
== 0)
5859 alt0
= fold (build (MULT_EXPR
, type
, arg00
,
5860 build_int_2 (int01
/ int11
, 0)));
5867 return fold (build (MULT_EXPR
, type
,
5868 fold (build (PLUS_EXPR
, type
, alt0
, alt1
)),
5874 /* See if ARG1 is zero and X + ARG1 reduces to X. */
5875 if (fold_real_zero_addition_p (TREE_TYPE (arg0
), arg1
, 0))
5876 return non_lvalue (convert (type
, arg0
));
5878 /* Likewise if the operands are reversed. */
5879 if (fold_real_zero_addition_p (TREE_TYPE (arg1
), arg0
, 0))
5880 return non_lvalue (convert (type
, arg1
));
5882 /* Convert x+x into x*2.0. */
5883 if (operand_equal_p (arg0
, arg1
, 0)
5884 && SCALAR_FLOAT_TYPE_P (type
))
5885 return fold (build (MULT_EXPR
, type
, arg0
,
5886 build_real (type
, dconst2
)));
5888 /* Convert x*c+x into x*(c+1). */
5889 if (flag_unsafe_math_optimizations
5890 && TREE_CODE (arg0
) == MULT_EXPR
5891 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
5892 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0
, 1))
5893 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
5897 c
= TREE_REAL_CST (TREE_OPERAND (arg0
, 1));
5898 real_arithmetic (&c
, PLUS_EXPR
, &c
, &dconst1
);
5899 return fold (build (MULT_EXPR
, type
, arg1
,
5900 build_real (type
, c
)));
5903 /* Convert x+x*c into x*(c+1). */
5904 if (flag_unsafe_math_optimizations
5905 && TREE_CODE (arg1
) == MULT_EXPR
5906 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == REAL_CST
5907 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1
, 1))
5908 && operand_equal_p (TREE_OPERAND (arg1
, 0), arg0
, 0))
5912 c
= TREE_REAL_CST (TREE_OPERAND (arg1
, 1));
5913 real_arithmetic (&c
, PLUS_EXPR
, &c
, &dconst1
);
5914 return fold (build (MULT_EXPR
, type
, arg0
,
5915 build_real (type
, c
)));
5918 /* Convert x*c1+x*c2 into x*(c1+c2). */
5919 if (flag_unsafe_math_optimizations
5920 && TREE_CODE (arg0
) == MULT_EXPR
5921 && TREE_CODE (arg1
) == MULT_EXPR
5922 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
5923 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0
, 1))
5924 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == REAL_CST
5925 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1
, 1))
5926 && operand_equal_p (TREE_OPERAND (arg0
, 0),
5927 TREE_OPERAND (arg1
, 0), 0))
5929 REAL_VALUE_TYPE c1
, c2
;
5931 c1
= TREE_REAL_CST (TREE_OPERAND (arg0
, 1));
5932 c2
= TREE_REAL_CST (TREE_OPERAND (arg1
, 1));
5933 real_arithmetic (&c1
, PLUS_EXPR
, &c1
, &c2
);
5934 return fold (build (MULT_EXPR
, type
,
5935 TREE_OPERAND (arg0
, 0),
5936 build_real (type
, c1
)));
5941 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
5942 is a rotate of A by C1 bits. */
5943 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
5944 is a rotate of A by B bits. */
5946 enum tree_code code0
, code1
;
5947 code0
= TREE_CODE (arg0
);
5948 code1
= TREE_CODE (arg1
);
5949 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
5950 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
5951 && operand_equal_p (TREE_OPERAND (arg0
, 0),
5952 TREE_OPERAND (arg1
, 0), 0)
5953 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
5955 tree tree01
, tree11
;
5956 enum tree_code code01
, code11
;
5958 tree01
= TREE_OPERAND (arg0
, 1);
5959 tree11
= TREE_OPERAND (arg1
, 1);
5960 STRIP_NOPS (tree01
);
5961 STRIP_NOPS (tree11
);
5962 code01
= TREE_CODE (tree01
);
5963 code11
= TREE_CODE (tree11
);
5964 if (code01
== INTEGER_CST
5965 && code11
== INTEGER_CST
5966 && TREE_INT_CST_HIGH (tree01
) == 0
5967 && TREE_INT_CST_HIGH (tree11
) == 0
5968 && ((TREE_INT_CST_LOW (tree01
) + TREE_INT_CST_LOW (tree11
))
5969 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0
, 0)))))
5970 return build (LROTATE_EXPR
, type
, TREE_OPERAND (arg0
, 0),
5971 code0
== LSHIFT_EXPR
? tree01
: tree11
);
5972 else if (code11
== MINUS_EXPR
)
5974 tree tree110
, tree111
;
5975 tree110
= TREE_OPERAND (tree11
, 0);
5976 tree111
= TREE_OPERAND (tree11
, 1);
5977 STRIP_NOPS (tree110
);
5978 STRIP_NOPS (tree111
);
5979 if (TREE_CODE (tree110
) == INTEGER_CST
5980 && 0 == compare_tree_int (tree110
,
5982 (TREE_TYPE (TREE_OPERAND
5984 && operand_equal_p (tree01
, tree111
, 0))
5985 return build ((code0
== LSHIFT_EXPR
5988 type
, TREE_OPERAND (arg0
, 0), tree01
);
5990 else if (code01
== MINUS_EXPR
)
5992 tree tree010
, tree011
;
5993 tree010
= TREE_OPERAND (tree01
, 0);
5994 tree011
= TREE_OPERAND (tree01
, 1);
5995 STRIP_NOPS (tree010
);
5996 STRIP_NOPS (tree011
);
5997 if (TREE_CODE (tree010
) == INTEGER_CST
5998 && 0 == compare_tree_int (tree010
,
6000 (TREE_TYPE (TREE_OPERAND
6002 && operand_equal_p (tree11
, tree011
, 0))
6003 return build ((code0
!= LSHIFT_EXPR
6006 type
, TREE_OPERAND (arg0
, 0), tree11
);
6012 /* In most languages, can't associate operations on floats through
6013 parentheses. Rather than remember where the parentheses were, we
6014 don't associate floats at all, unless the user has specified
6015 -funsafe-math-optimizations. */
6018 && (! FLOAT_TYPE_P (type
) || flag_unsafe_math_optimizations
))
6020 tree var0
, con0
, lit0
, minus_lit0
;
6021 tree var1
, con1
, lit1
, minus_lit1
;
6023 /* Split both trees into variables, constants, and literals. Then
6024 associate each group together, the constants with literals,
6025 then the result with variables. This increases the chances of
6026 literals being recombined later and of generating relocatable
6027 expressions for the sum of a constant and literal. */
6028 var0
= split_tree (arg0
, code
, &con0
, &lit0
, &minus_lit0
, 0);
6029 var1
= split_tree (arg1
, code
, &con1
, &lit1
, &minus_lit1
,
6030 code
== MINUS_EXPR
);
6032 /* Only do something if we found more than two objects. Otherwise,
6033 nothing has changed and we risk infinite recursion. */
6034 if (2 < ((var0
!= 0) + (var1
!= 0)
6035 + (con0
!= 0) + (con1
!= 0)
6036 + (lit0
!= 0) + (lit1
!= 0)
6037 + (minus_lit0
!= 0) + (minus_lit1
!= 0)))
6039 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
6040 if (code
== MINUS_EXPR
)
6043 var0
= associate_trees (var0
, var1
, code
, type
);
6044 con0
= associate_trees (con0
, con1
, code
, type
);
6045 lit0
= associate_trees (lit0
, lit1
, code
, type
);
6046 minus_lit0
= associate_trees (minus_lit0
, minus_lit1
, code
, type
);
6048 /* Preserve the MINUS_EXPR if the negative part of the literal is
6049 greater than the positive part. Otherwise, the multiplicative
6050 folding code (i.e extract_muldiv) may be fooled in case
6051 unsigned constants are subtracted, like in the following
6052 example: ((X*2 + 4) - 8U)/2. */
6053 if (minus_lit0
&& lit0
)
6055 if (TREE_CODE (lit0
) == INTEGER_CST
6056 && TREE_CODE (minus_lit0
) == INTEGER_CST
6057 && tree_int_cst_lt (lit0
, minus_lit0
))
6059 minus_lit0
= associate_trees (minus_lit0
, lit0
,
6065 lit0
= associate_trees (lit0
, minus_lit0
,
6073 return convert (type
, associate_trees (var0
, minus_lit0
,
6077 con0
= associate_trees (con0
, minus_lit0
,
6079 return convert (type
, associate_trees (var0
, con0
,
6084 con0
= associate_trees (con0
, lit0
, code
, type
);
6085 return convert (type
, associate_trees (var0
, con0
, code
, type
));
6091 t1
= const_binop (code
, arg0
, arg1
, 0);
6092 if (t1
!= NULL_TREE
)
6094 /* The return value should always have
6095 the same type as the original expression. */
6096 if (TREE_TYPE (t1
) != TREE_TYPE (t
))
6097 t1
= convert (TREE_TYPE (t
), t1
);
6104 /* A - (-B) -> A + B */
6105 if (TREE_CODE (arg1
) == NEGATE_EXPR
)
6106 return fold (build (PLUS_EXPR
, type
, arg0
, TREE_OPERAND (arg1
, 0)));
6107 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
6108 if (TREE_CODE (arg0
) == NEGATE_EXPR
6109 && (FLOAT_TYPE_P (type
)
6110 || (INTEGRAL_TYPE_P (type
) && flag_wrapv
&& !flag_trapv
))
6111 && negate_expr_p (arg1
)
6112 && reorder_operands_p (arg0
, arg1
))
6113 return fold (build (MINUS_EXPR
, type
, negate_expr (arg1
),
6114 TREE_OPERAND (arg0
, 0)));
6116 if (! FLOAT_TYPE_P (type
))
6118 if (! wins
&& integer_zerop (arg0
))
6119 return negate_expr (convert (type
, arg1
));
6120 if (integer_zerop (arg1
))
6121 return non_lvalue (convert (type
, arg0
));
6123 /* Fold A - (A & B) into ~B & A. */
6124 if (!TREE_SIDE_EFFECTS (arg0
)
6125 && TREE_CODE (arg1
) == BIT_AND_EXPR
)
6127 if (operand_equal_p (arg0
, TREE_OPERAND (arg1
, 1), 0))
6128 return fold (build (BIT_AND_EXPR
, type
,
6129 fold (build1 (BIT_NOT_EXPR
, type
,
6130 TREE_OPERAND (arg1
, 0))),
6132 if (operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
6133 return fold (build (BIT_AND_EXPR
, type
,
6134 fold (build1 (BIT_NOT_EXPR
, type
,
6135 TREE_OPERAND (arg1
, 1))),
6139 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
6140 any power of 2 minus 1. */
6141 if (TREE_CODE (arg0
) == BIT_AND_EXPR
6142 && TREE_CODE (arg1
) == BIT_AND_EXPR
6143 && operand_equal_p (TREE_OPERAND (arg0
, 0),
6144 TREE_OPERAND (arg1
, 0), 0))
6146 tree mask0
= TREE_OPERAND (arg0
, 1);
6147 tree mask1
= TREE_OPERAND (arg1
, 1);
6148 tree tem
= fold (build1 (BIT_NOT_EXPR
, type
, mask0
));
6150 if (operand_equal_p (tem
, mask1
, 0))
6152 tem
= fold (build (BIT_XOR_EXPR
, type
,
6153 TREE_OPERAND (arg0
, 0), mask1
));
6154 return fold (build (MINUS_EXPR
, type
, tem
, mask1
));
6159 /* See if ARG1 is zero and X - ARG1 reduces to X. */
6160 else if (fold_real_zero_addition_p (TREE_TYPE (arg0
), arg1
, 1))
6161 return non_lvalue (convert (type
, arg0
));
6163 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
6164 ARG0 is zero and X + ARG0 reduces to X, since that would mean
6165 (-ARG1 + ARG0) reduces to -ARG1. */
6166 else if (!wins
&& fold_real_zero_addition_p (TREE_TYPE (arg1
), arg0
, 0))
6167 return negate_expr (convert (type
, arg1
));
6169 /* Fold &x - &x. This can happen from &x.foo - &x.
6170 This is unsafe for certain floats even in non-IEEE formats.
6171 In IEEE, it is unsafe because it does wrong for NaNs.
6172 Also note that operand_equal_p is always false if an operand
6175 if ((! FLOAT_TYPE_P (type
) || flag_unsafe_math_optimizations
)
6176 && operand_equal_p (arg0
, arg1
, 0))
6177 return convert (type
, integer_zero_node
);
6179 /* A - B -> A + (-B) if B is easily negatable. */
6180 if (!wins
&& negate_expr_p (arg1
)
6181 && (FLOAT_TYPE_P (type
)
6182 || (INTEGRAL_TYPE_P (type
) && flag_wrapv
&& !flag_trapv
)))
6183 return fold (build (PLUS_EXPR
, type
, arg0
, negate_expr (arg1
)));
6185 if (TREE_CODE (arg0
) == MULT_EXPR
6186 && TREE_CODE (arg1
) == MULT_EXPR
6187 && (INTEGRAL_TYPE_P (type
) || flag_unsafe_math_optimizations
))
6189 /* (A * C) - (B * C) -> (A-B) * C. */
6190 if (operand_equal_p (TREE_OPERAND (arg0
, 1),
6191 TREE_OPERAND (arg1
, 1), 0))
6192 return fold (build (MULT_EXPR
, type
,
6193 fold (build (MINUS_EXPR
, type
,
6194 TREE_OPERAND (arg0
, 0),
6195 TREE_OPERAND (arg1
, 0))),
6196 TREE_OPERAND (arg0
, 1)));
6197 /* (A * C1) - (A * C2) -> A * (C1-C2). */
6198 if (operand_equal_p (TREE_OPERAND (arg0
, 0),
6199 TREE_OPERAND (arg1
, 0), 0))
6200 return fold (build (MULT_EXPR
, type
,
6201 TREE_OPERAND (arg0
, 0),
6202 fold (build (MINUS_EXPR
, type
,
6203 TREE_OPERAND (arg0
, 1),
6204 TREE_OPERAND (arg1
, 1)))));
6210 /* (-A) * (-B) -> A * B */
6211 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
6212 return fold (build (MULT_EXPR
, type
,
6213 TREE_OPERAND (arg0
, 0),
6214 negate_expr (arg1
)));
6215 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
6216 return fold (build (MULT_EXPR
, type
,
6218 TREE_OPERAND (arg1
, 0)));
6220 if (! FLOAT_TYPE_P (type
))
6222 if (integer_zerop (arg1
))
6223 return omit_one_operand (type
, arg1
, arg0
);
6224 if (integer_onep (arg1
))
6225 return non_lvalue (convert (type
, arg0
));
6227 /* (a * (1 << b)) is (a << b) */
6228 if (TREE_CODE (arg1
) == LSHIFT_EXPR
6229 && integer_onep (TREE_OPERAND (arg1
, 0)))
6230 return fold (build (LSHIFT_EXPR
, type
, arg0
,
6231 TREE_OPERAND (arg1
, 1)));
6232 if (TREE_CODE (arg0
) == LSHIFT_EXPR
6233 && integer_onep (TREE_OPERAND (arg0
, 0)))
6234 return fold (build (LSHIFT_EXPR
, type
, arg1
,
6235 TREE_OPERAND (arg0
, 1)));
6237 if (TREE_CODE (arg1
) == INTEGER_CST
6238 && 0 != (tem
= extract_muldiv (TREE_OPERAND (t
, 0),
6239 convert (type
, arg1
),
6241 return convert (type
, tem
);
6246 /* Maybe fold x * 0 to 0. The expressions aren't the same
6247 when x is NaN, since x * 0 is also NaN. Nor are they the
6248 same in modes with signed zeros, since multiplying a
6249 negative value by 0 gives -0, not +0. */
6250 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
)))
6251 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0
)))
6252 && real_zerop (arg1
))
6253 return omit_one_operand (type
, arg1
, arg0
);
6254 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
6255 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
6256 && real_onep (arg1
))
6257 return non_lvalue (convert (type
, arg0
));
6259 /* Transform x * -1.0 into -x. */
6260 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
6261 && real_minus_onep (arg1
))
6262 return fold (build1 (NEGATE_EXPR
, type
, arg0
));
6264 /* Convert (C1/X)*C2 into (C1*C2)/X. */
6265 if (flag_unsafe_math_optimizations
6266 && TREE_CODE (arg0
) == RDIV_EXPR
6267 && TREE_CODE (arg1
) == REAL_CST
6268 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == REAL_CST
)
6270 tree tem
= const_binop (MULT_EXPR
, TREE_OPERAND (arg0
, 0),
6273 return fold (build (RDIV_EXPR
, type
, tem
,
6274 TREE_OPERAND (arg0
, 1)));
6277 if (flag_unsafe_math_optimizations
)
6279 enum built_in_function fcode0
= builtin_mathfn_code (arg0
);
6280 enum built_in_function fcode1
= builtin_mathfn_code (arg1
);
6282 /* Optimizations of sqrt(...)*sqrt(...). */
6283 if ((fcode0
== BUILT_IN_SQRT
&& fcode1
== BUILT_IN_SQRT
)
6284 || (fcode0
== BUILT_IN_SQRTF
&& fcode1
== BUILT_IN_SQRTF
)
6285 || (fcode0
== BUILT_IN_SQRTL
&& fcode1
== BUILT_IN_SQRTL
))
6287 tree sqrtfn
, arg
, arglist
;
6288 tree arg00
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
6289 tree arg10
= TREE_VALUE (TREE_OPERAND (arg1
, 1));
6291 /* Optimize sqrt(x)*sqrt(x) as x. */
6292 if (operand_equal_p (arg00
, arg10
, 0)
6293 && ! HONOR_SNANS (TYPE_MODE (type
)))
6296 /* Optimize sqrt(x)*sqrt(y) as sqrt(x*y). */
6297 sqrtfn
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
6298 arg
= fold (build (MULT_EXPR
, type
, arg00
, arg10
));
6299 arglist
= build_tree_list (NULL_TREE
, arg
);
6300 return build_function_call_expr (sqrtfn
, arglist
);
6303 /* Optimize expN(x)*expN(y) as expN(x+y). */
6304 if (fcode0
== fcode1
6305 && (fcode0
== BUILT_IN_EXP
6306 || fcode0
== BUILT_IN_EXPF
6307 || fcode0
== BUILT_IN_EXPL
6308 || fcode0
== BUILT_IN_EXP2
6309 || fcode0
== BUILT_IN_EXP2F
6310 || fcode0
== BUILT_IN_EXP2L
6311 || fcode0
== BUILT_IN_EXP10
6312 || fcode0
== BUILT_IN_EXP10F
6313 || fcode0
== BUILT_IN_EXP10L
6314 || fcode0
== BUILT_IN_POW10
6315 || fcode0
== BUILT_IN_POW10F
6316 || fcode0
== BUILT_IN_POW10L
))
6318 tree expfn
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
6319 tree arg
= build (PLUS_EXPR
, type
,
6320 TREE_VALUE (TREE_OPERAND (arg0
, 1)),
6321 TREE_VALUE (TREE_OPERAND (arg1
, 1)));
6322 tree arglist
= build_tree_list (NULL_TREE
, fold (arg
));
6323 return build_function_call_expr (expfn
, arglist
);
6326 /* Optimizations of pow(...)*pow(...). */
6327 if ((fcode0
== BUILT_IN_POW
&& fcode1
== BUILT_IN_POW
)
6328 || (fcode0
== BUILT_IN_POWF
&& fcode1
== BUILT_IN_POWF
)
6329 || (fcode0
== BUILT_IN_POWL
&& fcode1
== BUILT_IN_POWL
))
6331 tree arg00
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
6332 tree arg01
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0
,
6334 tree arg10
= TREE_VALUE (TREE_OPERAND (arg1
, 1));
6335 tree arg11
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1
,
6338 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
6339 if (operand_equal_p (arg01
, arg11
, 0))
6341 tree powfn
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
6342 tree arg
= build (MULT_EXPR
, type
, arg00
, arg10
);
6343 tree arglist
= tree_cons (NULL_TREE
, fold (arg
),
6344 build_tree_list (NULL_TREE
,
6346 return build_function_call_expr (powfn
, arglist
);
6349 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
6350 if (operand_equal_p (arg00
, arg10
, 0))
6352 tree powfn
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
6353 tree arg
= fold (build (PLUS_EXPR
, type
, arg01
, arg11
));
6354 tree arglist
= tree_cons (NULL_TREE
, arg00
,
6355 build_tree_list (NULL_TREE
,
6357 return build_function_call_expr (powfn
, arglist
);
6361 /* Optimize tan(x)*cos(x) as sin(x). */
6362 if (((fcode0
== BUILT_IN_TAN
&& fcode1
== BUILT_IN_COS
)
6363 || (fcode0
== BUILT_IN_TANF
&& fcode1
== BUILT_IN_COSF
)
6364 || (fcode0
== BUILT_IN_TANL
&& fcode1
== BUILT_IN_COSL
)
6365 || (fcode0
== BUILT_IN_COS
&& fcode1
== BUILT_IN_TAN
)
6366 || (fcode0
== BUILT_IN_COSF
&& fcode1
== BUILT_IN_TANF
)
6367 || (fcode0
== BUILT_IN_COSL
&& fcode1
== BUILT_IN_TANL
))
6368 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0
, 1)),
6369 TREE_VALUE (TREE_OPERAND (arg1
, 1)), 0))
6377 sinfn
= implicit_built_in_decls
[BUILT_IN_SIN
];
6381 sinfn
= implicit_built_in_decls
[BUILT_IN_SINF
];
6385 sinfn
= implicit_built_in_decls
[BUILT_IN_SINL
];
6391 if (sinfn
!= NULL_TREE
)
6392 return build_function_call_expr (sinfn
,
6393 TREE_OPERAND (arg0
, 1));
6396 /* Optimize x*pow(x,c) as pow(x,c+1). */
6397 if (fcode1
== BUILT_IN_POW
6398 || fcode1
== BUILT_IN_POWF
6399 || fcode1
== BUILT_IN_POWL
)
6401 tree arg10
= TREE_VALUE (TREE_OPERAND (arg1
, 1));
6402 tree arg11
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1
,
6404 if (TREE_CODE (arg11
) == REAL_CST
6405 && ! TREE_CONSTANT_OVERFLOW (arg11
)
6406 && operand_equal_p (arg0
, arg10
, 0))
6408 tree powfn
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0);
6412 c
= TREE_REAL_CST (arg11
);
6413 real_arithmetic (&c
, PLUS_EXPR
, &c
, &dconst1
);
6414 arg
= build_real (type
, c
);
6415 arglist
= build_tree_list (NULL_TREE
, arg
);
6416 arglist
= tree_cons (NULL_TREE
, arg0
, arglist
);
6417 return build_function_call_expr (powfn
, arglist
);
6421 /* Optimize pow(x,c)*x as pow(x,c+1). */
6422 if (fcode0
== BUILT_IN_POW
6423 || fcode0
== BUILT_IN_POWF
6424 || fcode0
== BUILT_IN_POWL
)
6426 tree arg00
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
6427 tree arg01
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0
,
6429 if (TREE_CODE (arg01
) == REAL_CST
6430 && ! TREE_CONSTANT_OVERFLOW (arg01
)
6431 && operand_equal_p (arg1
, arg00
, 0))
6433 tree powfn
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
6437 c
= TREE_REAL_CST (arg01
);
6438 real_arithmetic (&c
, PLUS_EXPR
, &c
, &dconst1
);
6439 arg
= build_real (type
, c
);
6440 arglist
= build_tree_list (NULL_TREE
, arg
);
6441 arglist
= tree_cons (NULL_TREE
, arg1
, arglist
);
6442 return build_function_call_expr (powfn
, arglist
);
6446 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
6448 && operand_equal_p (arg0
, arg1
, 0))
6452 if (type
== double_type_node
)
6453 powfn
= implicit_built_in_decls
[BUILT_IN_POW
];
6454 else if (type
== float_type_node
)
6455 powfn
= implicit_built_in_decls
[BUILT_IN_POWF
];
6456 else if (type
== long_double_type_node
)
6457 powfn
= implicit_built_in_decls
[BUILT_IN_POWL
];
6463 tree arg
= build_real (type
, dconst2
);
6464 tree arglist
= build_tree_list (NULL_TREE
, arg
);
6465 arglist
= tree_cons (NULL_TREE
, arg0
, arglist
);
6466 return build_function_call_expr (powfn
, arglist
);
6475 if (integer_all_onesp (arg1
))
6476 return omit_one_operand (type
, arg1
, arg0
);
6477 if (integer_zerop (arg1
))
6478 return non_lvalue (convert (type
, arg0
));
6479 t1
= distribute_bit_expr (code
, type
, arg0
, arg1
);
6480 if (t1
!= NULL_TREE
)
6483 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
6485 This results in more efficient code for machines without a NAND
6486 instruction. Combine will canonicalize to the first form
6487 which will allow use of NAND instructions provided by the
6488 backend if they exist. */
6489 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
6490 && TREE_CODE (arg1
) == BIT_NOT_EXPR
)
6492 return fold (build1 (BIT_NOT_EXPR
, type
,
6493 build (BIT_AND_EXPR
, type
,
6494 TREE_OPERAND (arg0
, 0),
6495 TREE_OPERAND (arg1
, 0))));
6498 /* See if this can be simplified into a rotate first. If that
6499 is unsuccessful continue in the association code. */
6503 if (integer_zerop (arg1
))
6504 return non_lvalue (convert (type
, arg0
));
6505 if (integer_all_onesp (arg1
))
6506 return fold (build1 (BIT_NOT_EXPR
, type
, arg0
));
6508 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
6509 with a constant, and the two constants have no bits in common,
6510 we should treat this as a BIT_IOR_EXPR since this may produce more
6512 if (TREE_CODE (arg0
) == BIT_AND_EXPR
6513 && TREE_CODE (arg1
) == BIT_AND_EXPR
6514 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
6515 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
6516 && integer_zerop (const_binop (BIT_AND_EXPR
,
6517 TREE_OPERAND (arg0
, 1),
6518 TREE_OPERAND (arg1
, 1), 0)))
6520 code
= BIT_IOR_EXPR
;
6524 /* See if this can be simplified into a rotate first. If that
6525 is unsuccessful continue in the association code. */
6529 if (integer_all_onesp (arg1
))
6530 return non_lvalue (convert (type
, arg0
));
6531 if (integer_zerop (arg1
))
6532 return omit_one_operand (type
, arg1
, arg0
);
6533 t1
= distribute_bit_expr (code
, type
, arg0
, arg1
);
6534 if (t1
!= NULL_TREE
)
6536 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
6537 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
6538 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
6541 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
6543 if (prec
< BITS_PER_WORD
&& prec
< HOST_BITS_PER_WIDE_INT
6544 && (~TREE_INT_CST_LOW (arg1
)
6545 & (((HOST_WIDE_INT
) 1 << prec
) - 1)) == 0)
6546 return build1 (NOP_EXPR
, type
, TREE_OPERAND (arg0
, 0));
6549 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
6551 This results in more efficient code for machines without a NOR
6552 instruction. Combine will canonicalize to the first form
6553 which will allow use of NOR instructions provided by the
6554 backend if they exist. */
6555 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
6556 && TREE_CODE (arg1
) == BIT_NOT_EXPR
)
6558 return fold (build1 (BIT_NOT_EXPR
, type
,
6559 build (BIT_IOR_EXPR
, type
,
6560 TREE_OPERAND (arg0
, 0),
6561 TREE_OPERAND (arg1
, 0))));
6567 /* Don't touch a floating-point divide by zero unless the mode
6568 of the constant can represent infinity. */
6569 if (TREE_CODE (arg1
) == REAL_CST
6570 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
6571 && real_zerop (arg1
))
6574 /* (-A) / (-B) -> A / B */
6575 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
6576 return fold (build (RDIV_EXPR
, type
,
6577 TREE_OPERAND (arg0
, 0),
6578 negate_expr (arg1
)));
6579 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
6580 return fold (build (RDIV_EXPR
, type
,
6582 TREE_OPERAND (arg1
, 0)));
6584 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
6585 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
6586 && real_onep (arg1
))
6587 return non_lvalue (convert (type
, arg0
));
6589 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
6590 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
6591 && real_minus_onep (arg1
))
6592 return non_lvalue (convert (type
, negate_expr (arg0
)));
6594 /* If ARG1 is a constant, we can convert this to a multiply by the
6595 reciprocal. This does not have the same rounding properties,
6596 so only do this if -funsafe-math-optimizations. We can actually
6597 always safely do it if ARG1 is a power of two, but it's hard to
6598 tell if it is or not in a portable manner. */
6599 if (TREE_CODE (arg1
) == REAL_CST
)
6601 if (flag_unsafe_math_optimizations
6602 && 0 != (tem
= const_binop (code
, build_real (type
, dconst1
),
6604 return fold (build (MULT_EXPR
, type
, arg0
, tem
));
6605 /* Find the reciprocal if optimizing and the result is exact. */
6609 r
= TREE_REAL_CST (arg1
);
6610 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0
)), &r
))
6612 tem
= build_real (type
, r
);
6613 return fold (build (MULT_EXPR
, type
, arg0
, tem
));
6617 /* Convert A/B/C to A/(B*C). */
6618 if (flag_unsafe_math_optimizations
6619 && TREE_CODE (arg0
) == RDIV_EXPR
)
6620 return fold (build (RDIV_EXPR
, type
, TREE_OPERAND (arg0
, 0),
6621 fold (build (MULT_EXPR
, type
,
6622 TREE_OPERAND (arg0
, 1), arg1
))));
6624 /* Convert A/(B/C) to (A/B)*C. */
6625 if (flag_unsafe_math_optimizations
6626 && TREE_CODE (arg1
) == RDIV_EXPR
)
6627 return fold (build (MULT_EXPR
, type
,
6628 fold (build (RDIV_EXPR
, type
, arg0
,
6629 TREE_OPERAND (arg1
, 0))),
6630 TREE_OPERAND (arg1
, 1)));
6632 /* Convert C1/(X*C2) into (C1/C2)/X. */
6633 if (flag_unsafe_math_optimizations
6634 && TREE_CODE (arg1
) == MULT_EXPR
6635 && TREE_CODE (arg0
) == REAL_CST
6636 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == REAL_CST
)
6638 tree tem
= const_binop (RDIV_EXPR
, arg0
,
6639 TREE_OPERAND (arg1
, 1), 0);
6641 return fold (build (RDIV_EXPR
, type
, tem
,
6642 TREE_OPERAND (arg1
, 0)));
6645 if (flag_unsafe_math_optimizations
)
6647 enum built_in_function fcode
= builtin_mathfn_code (arg1
);
6648 /* Optimize x/expN(y) into x*expN(-y). */
6649 if (fcode
== BUILT_IN_EXP
6650 || fcode
== BUILT_IN_EXPF
6651 || fcode
== BUILT_IN_EXPL
6652 || fcode
== BUILT_IN_EXP2
6653 || fcode
== BUILT_IN_EXP2F
6654 || fcode
== BUILT_IN_EXP2L
6655 || fcode
== BUILT_IN_EXP10
6656 || fcode
== BUILT_IN_EXP10F
6657 || fcode
== BUILT_IN_EXP10L
6658 || fcode
== BUILT_IN_POW10
6659 || fcode
== BUILT_IN_POW10F
6660 || fcode
== BUILT_IN_POW10L
)
6662 tree expfn
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0);
6663 tree arg
= build1 (NEGATE_EXPR
, type
,
6664 TREE_VALUE (TREE_OPERAND (arg1
, 1)));
6665 tree arglist
= build_tree_list (NULL_TREE
, fold (arg
));
6666 arg1
= build_function_call_expr (expfn
, arglist
);
6667 return fold (build (MULT_EXPR
, type
, arg0
, arg1
));
6670 /* Optimize x/pow(y,z) into x*pow(y,-z). */
6671 if (fcode
== BUILT_IN_POW
6672 || fcode
== BUILT_IN_POWF
6673 || fcode
== BUILT_IN_POWL
)
6675 tree powfn
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0);
6676 tree arg10
= TREE_VALUE (TREE_OPERAND (arg1
, 1));
6677 tree arg11
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1
, 1)));
6678 tree neg11
= fold (build1 (NEGATE_EXPR
, type
, arg11
));
6679 tree arglist
= tree_cons(NULL_TREE
, arg10
,
6680 build_tree_list (NULL_TREE
, neg11
));
6681 arg1
= build_function_call_expr (powfn
, arglist
);
6682 return fold (build (MULT_EXPR
, type
, arg0
, arg1
));
6686 if (flag_unsafe_math_optimizations
)
6688 enum built_in_function fcode0
= builtin_mathfn_code (arg0
);
6689 enum built_in_function fcode1
= builtin_mathfn_code (arg1
);
6691 /* Optimize sin(x)/cos(x) as tan(x). */
6692 if (((fcode0
== BUILT_IN_SIN
&& fcode1
== BUILT_IN_COS
)
6693 || (fcode0
== BUILT_IN_SINF
&& fcode1
== BUILT_IN_COSF
)
6694 || (fcode0
== BUILT_IN_SINL
&& fcode1
== BUILT_IN_COSL
))
6695 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0
, 1)),
6696 TREE_VALUE (TREE_OPERAND (arg1
, 1)), 0))
6700 if (fcode0
== BUILT_IN_SIN
)
6701 tanfn
= implicit_built_in_decls
[BUILT_IN_TAN
];
6702 else if (fcode0
== BUILT_IN_SINF
)
6703 tanfn
= implicit_built_in_decls
[BUILT_IN_TANF
];
6704 else if (fcode0
== BUILT_IN_SINL
)
6705 tanfn
= implicit_built_in_decls
[BUILT_IN_TANL
];
6709 if (tanfn
!= NULL_TREE
)
6710 return build_function_call_expr (tanfn
,
6711 TREE_OPERAND (arg0
, 1));
6714 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
6715 if (((fcode0
== BUILT_IN_COS
&& fcode1
== BUILT_IN_SIN
)
6716 || (fcode0
== BUILT_IN_COSF
&& fcode1
== BUILT_IN_SINF
)
6717 || (fcode0
== BUILT_IN_COSL
&& fcode1
== BUILT_IN_SINL
))
6718 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0
, 1)),
6719 TREE_VALUE (TREE_OPERAND (arg1
, 1)), 0))
6723 if (fcode0
== BUILT_IN_COS
)
6724 tanfn
= implicit_built_in_decls
[BUILT_IN_TAN
];
6725 else if (fcode0
== BUILT_IN_COSF
)
6726 tanfn
= implicit_built_in_decls
[BUILT_IN_TANF
];
6727 else if (fcode0
== BUILT_IN_COSL
)
6728 tanfn
= implicit_built_in_decls
[BUILT_IN_TANL
];
6732 if (tanfn
!= NULL_TREE
)
6734 tree tmp
= TREE_OPERAND (arg0
, 1);
6735 tmp
= build_function_call_expr (tanfn
, tmp
);
6736 return fold (build (RDIV_EXPR
, type
,
6737 build_real (type
, dconst1
),
6742 /* Optimize pow(x,c)/x as pow(x,c-1). */
6743 if (fcode0
== BUILT_IN_POW
6744 || fcode0
== BUILT_IN_POWF
6745 || fcode0
== BUILT_IN_POWL
)
6747 tree arg00
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
6748 tree arg01
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0
, 1)));
6749 if (TREE_CODE (arg01
) == REAL_CST
6750 && ! TREE_CONSTANT_OVERFLOW (arg01
)
6751 && operand_equal_p (arg1
, arg00
, 0))
6753 tree powfn
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
6757 c
= TREE_REAL_CST (arg01
);
6758 real_arithmetic (&c
, MINUS_EXPR
, &c
, &dconst1
);
6759 arg
= build_real (type
, c
);
6760 arglist
= build_tree_list (NULL_TREE
, arg
);
6761 arglist
= tree_cons (NULL_TREE
, arg1
, arglist
);
6762 return build_function_call_expr (powfn
, arglist
);
6768 case TRUNC_DIV_EXPR
:
6769 case ROUND_DIV_EXPR
:
6770 case FLOOR_DIV_EXPR
:
6772 case EXACT_DIV_EXPR
:
6773 if (integer_onep (arg1
))
6774 return non_lvalue (convert (type
, arg0
));
6775 if (integer_zerop (arg1
))
6778 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
6779 operation, EXACT_DIV_EXPR.
6781 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
6782 At one time others generated faster code, it's not clear if they do
6783 after the last round to changes to the DIV code in expmed.c. */
6784 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
6785 && multiple_of_p (type
, arg0
, arg1
))
6786 return fold (build (EXACT_DIV_EXPR
, type
, arg0
, arg1
));
6788 if (TREE_CODE (arg1
) == INTEGER_CST
6789 && 0 != (tem
= extract_muldiv (TREE_OPERAND (t
, 0), arg1
,
6791 return convert (type
, tem
);
6796 case FLOOR_MOD_EXPR
:
6797 case ROUND_MOD_EXPR
:
6798 case TRUNC_MOD_EXPR
:
6799 if (integer_onep (arg1
))
6800 return omit_one_operand (type
, integer_zero_node
, arg0
);
6801 if (integer_zerop (arg1
))
6804 if (TREE_CODE (arg1
) == INTEGER_CST
6805 && 0 != (tem
= extract_muldiv (TREE_OPERAND (t
, 0), arg1
,
6807 return convert (type
, tem
);
6813 if (integer_all_onesp (arg0
))
6814 return omit_one_operand (type
, arg0
, arg1
);
6818 /* Optimize -1 >> x for arithmetic right shifts. */
6819 if (integer_all_onesp (arg0
) && ! TREE_UNSIGNED (type
))
6820 return omit_one_operand (type
, arg0
, arg1
);
6821 /* ... fall through ... */
6825 if (integer_zerop (arg1
))
6826 return non_lvalue (convert (type
, arg0
));
6827 if (integer_zerop (arg0
))
6828 return omit_one_operand (type
, arg0
, arg1
);
6830 /* Since negative shift count is not well-defined,
6831 don't try to compute it in the compiler. */
6832 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
6834 /* Rewrite an LROTATE_EXPR by a constant into an
6835 RROTATE_EXPR by a new constant. */
6836 if (code
== LROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
)
6838 tree tem
= build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type
)), 0);
6839 tem
= convert (TREE_TYPE (arg1
), tem
);
6840 tem
= const_binop (MINUS_EXPR
, tem
, arg1
, 0);
6841 return fold (build (RROTATE_EXPR
, type
, arg0
, tem
));
6844 /* If we have a rotate of a bit operation with the rotate count and
6845 the second operand of the bit operation both constant,
6846 permute the two operations. */
6847 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
6848 && (TREE_CODE (arg0
) == BIT_AND_EXPR
6849 || TREE_CODE (arg0
) == BIT_IOR_EXPR
6850 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
6851 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
6852 return fold (build (TREE_CODE (arg0
), type
,
6853 fold (build (code
, type
,
6854 TREE_OPERAND (arg0
, 0), arg1
)),
6855 fold (build (code
, type
,
6856 TREE_OPERAND (arg0
, 1), arg1
))));
6858 /* Two consecutive rotates adding up to the width of the mode can
6860 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
6861 && TREE_CODE (arg0
) == RROTATE_EXPR
6862 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
6863 && TREE_INT_CST_HIGH (arg1
) == 0
6864 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0
, 1)) == 0
6865 && ((TREE_INT_CST_LOW (arg1
)
6866 + TREE_INT_CST_LOW (TREE_OPERAND (arg0
, 1)))
6867 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type
))))
6868 return TREE_OPERAND (arg0
, 0);
6873 if (operand_equal_p (arg0
, arg1
, 0))
6874 return omit_one_operand (type
, arg0
, arg1
);
6875 if (INTEGRAL_TYPE_P (type
)
6876 && operand_equal_p (arg1
, TYPE_MIN_VALUE (type
), 1))
6877 return omit_one_operand (type
, arg1
, arg0
);
6881 if (operand_equal_p (arg0
, arg1
, 0))
6882 return omit_one_operand (type
, arg0
, arg1
);
6883 if (INTEGRAL_TYPE_P (type
)
6884 && TYPE_MAX_VALUE (type
)
6885 && operand_equal_p (arg1
, TYPE_MAX_VALUE (type
), 1))
6886 return omit_one_operand (type
, arg1
, arg0
);
6889 case TRUTH_NOT_EXPR
:
6890 /* Note that the operand of this must be an int
6891 and its values must be 0 or 1.
6892 ("true" is a fixed value perhaps depending on the language,
6893 but we don't handle values other than 1 correctly yet.) */
6894 tem
= invert_truthvalue (arg0
);
6895 /* Avoid infinite recursion. */
6896 if (TREE_CODE (tem
) == TRUTH_NOT_EXPR
)
6898 tem
= fold_single_bit_test (code
, arg0
, arg1
, type
);
6903 return convert (type
, tem
);
6905 case TRUTH_ANDIF_EXPR
:
6906 /* Note that the operands of this must be ints
6907 and their values must be 0 or 1.
6908 ("true" is a fixed value perhaps depending on the language.) */
6909 /* If first arg is constant zero, return it. */
6910 if (integer_zerop (arg0
))
6911 return convert (type
, arg0
);
6912 case TRUTH_AND_EXPR
:
6913 /* If either arg is constant true, drop it. */
6914 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
6915 return non_lvalue (convert (type
, arg1
));
6916 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
6917 /* Preserve sequence points. */
6918 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
6919 return non_lvalue (convert (type
, arg0
));
6920 /* If second arg is constant zero, result is zero, but first arg
6921 must be evaluated. */
6922 if (integer_zerop (arg1
))
6923 return omit_one_operand (type
, arg1
, arg0
);
6924 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
6925 case will be handled here. */
6926 if (integer_zerop (arg0
))
6927 return omit_one_operand (type
, arg0
, arg1
);
6930 /* We only do these simplifications if we are optimizing. */
6934 /* Check for things like (A || B) && (A || C). We can convert this
6935 to A || (B && C). Note that either operator can be any of the four
6936 truth and/or operations and the transformation will still be
6937 valid. Also note that we only care about order for the
6938 ANDIF and ORIF operators. If B contains side effects, this
6939 might change the truth-value of A. */
6940 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
6941 && (TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
6942 || TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
6943 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
6944 || TREE_CODE (arg0
) == TRUTH_OR_EXPR
)
6945 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0
, 1)))
6947 tree a00
= TREE_OPERAND (arg0
, 0);
6948 tree a01
= TREE_OPERAND (arg0
, 1);
6949 tree a10
= TREE_OPERAND (arg1
, 0);
6950 tree a11
= TREE_OPERAND (arg1
, 1);
6951 int commutative
= ((TREE_CODE (arg0
) == TRUTH_OR_EXPR
6952 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
)
6953 && (code
== TRUTH_AND_EXPR
6954 || code
== TRUTH_OR_EXPR
));
6956 if (operand_equal_p (a00
, a10
, 0))
6957 return fold (build (TREE_CODE (arg0
), type
, a00
,
6958 fold (build (code
, type
, a01
, a11
))));
6959 else if (commutative
&& operand_equal_p (a00
, a11
, 0))
6960 return fold (build (TREE_CODE (arg0
), type
, a00
,
6961 fold (build (code
, type
, a01
, a10
))));
6962 else if (commutative
&& operand_equal_p (a01
, a10
, 0))
6963 return fold (build (TREE_CODE (arg0
), type
, a01
,
6964 fold (build (code
, type
, a00
, a11
))));
6966 /* This case if tricky because we must either have commutative
6967 operators or else A10 must not have side-effects. */
6969 else if ((commutative
|| ! TREE_SIDE_EFFECTS (a10
))
6970 && operand_equal_p (a01
, a11
, 0))
6971 return fold (build (TREE_CODE (arg0
), type
,
6972 fold (build (code
, type
, a00
, a10
)),
6976 /* See if we can build a range comparison. */
6977 if (0 != (tem
= fold_range_test (t
)))
6980 /* Check for the possibility of merging component references. If our
6981 lhs is another similar operation, try to merge its rhs with our
6982 rhs. Then try to merge our lhs and rhs. */
6983 if (TREE_CODE (arg0
) == code
6984 && 0 != (tem
= fold_truthop (code
, type
,
6985 TREE_OPERAND (arg0
, 1), arg1
)))
6986 return fold (build (code
, type
, TREE_OPERAND (arg0
, 0), tem
));
6988 if ((tem
= fold_truthop (code
, type
, arg0
, arg1
)) != 0)
6993 case TRUTH_ORIF_EXPR
:
6994 /* Note that the operands of this must be ints
6995 and their values must be 0 or true.
6996 ("true" is a fixed value perhaps depending on the language.) */
6997 /* If first arg is constant true, return it. */
6998 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
6999 return convert (type
, arg0
);
7001 /* If either arg is constant zero, drop it. */
7002 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
7003 return non_lvalue (convert (type
, arg1
));
7004 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
7005 /* Preserve sequence points. */
7006 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
7007 return non_lvalue (convert (type
, arg0
));
7008 /* If second arg is constant true, result is true, but we must
7009 evaluate first arg. */
7010 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
7011 return omit_one_operand (type
, arg1
, arg0
);
7012 /* Likewise for first arg, but note this only occurs here for
7014 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
7015 return omit_one_operand (type
, arg0
, arg1
);
7018 case TRUTH_XOR_EXPR
:
7019 /* If either arg is constant zero, drop it. */
7020 if (integer_zerop (arg0
))
7021 return non_lvalue (convert (type
, arg1
));
7022 if (integer_zerop (arg1
))
7023 return non_lvalue (convert (type
, arg0
));
7024 /* If either arg is constant true, this is a logical inversion. */
7025 if (integer_onep (arg0
))
7026 return non_lvalue (convert (type
, invert_truthvalue (arg1
)));
7027 if (integer_onep (arg1
))
7028 return non_lvalue (convert (type
, invert_truthvalue (arg0
)));
7037 /* If one arg is a real or integer constant, put it last. */
7038 if (tree_swap_operands_p (arg0
, arg1
, true))
7039 return fold (build (swap_tree_comparison (code
), type
, arg1
, arg0
));
7041 if (FLOAT_TYPE_P (TREE_TYPE (arg0
)))
7043 tree targ0
= strip_float_extensions (arg0
);
7044 tree targ1
= strip_float_extensions (arg1
);
7045 tree newtype
= TREE_TYPE (targ0
);
7047 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
7048 newtype
= TREE_TYPE (targ1
);
7050 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
7051 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
7052 return fold (build (code
, type
, convert (newtype
, targ0
),
7053 convert (newtype
, targ1
)));
7055 /* (-a) CMP (-b) -> b CMP a */
7056 if (TREE_CODE (arg0
) == NEGATE_EXPR
7057 && TREE_CODE (arg1
) == NEGATE_EXPR
)
7058 return fold (build (code
, type
, TREE_OPERAND (arg1
, 0),
7059 TREE_OPERAND (arg0
, 0)));
7061 if (TREE_CODE (arg1
) == REAL_CST
)
7063 REAL_VALUE_TYPE cst
;
7064 cst
= TREE_REAL_CST (arg1
);
7066 /* (-a) CMP CST -> a swap(CMP) (-CST) */
7067 if (TREE_CODE (arg0
) == NEGATE_EXPR
)
7069 fold (build (swap_tree_comparison (code
), type
,
7070 TREE_OPERAND (arg0
, 0),
7071 build_real (TREE_TYPE (arg1
),
7072 REAL_VALUE_NEGATE (cst
))));
7074 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
7075 /* a CMP (-0) -> a CMP 0 */
7076 if (REAL_VALUE_MINUS_ZERO (cst
))
7077 return fold (build (code
, type
, arg0
,
7078 build_real (TREE_TYPE (arg1
), dconst0
)));
7080 /* x != NaN is always true, other ops are always false. */
7081 if (REAL_VALUE_ISNAN (cst
)
7082 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1
))))
7084 t
= (code
== NE_EXPR
) ? integer_one_node
: integer_zero_node
;
7085 return omit_one_operand (type
, convert (type
, t
), arg0
);
7088 /* Fold comparisons against infinity. */
7089 if (REAL_VALUE_ISINF (cst
))
7091 tem
= fold_inf_compare (code
, type
, arg0
, arg1
);
7092 if (tem
!= NULL_TREE
)
7097 /* If this is a comparison of a real constant with a PLUS_EXPR
7098 or a MINUS_EXPR of a real constant, we can convert it into a
7099 comparison with a revised real constant as long as no overflow
7100 occurs when unsafe_math_optimizations are enabled. */
7101 if (flag_unsafe_math_optimizations
7102 && TREE_CODE (arg1
) == REAL_CST
7103 && (TREE_CODE (arg0
) == PLUS_EXPR
7104 || TREE_CODE (arg0
) == MINUS_EXPR
)
7105 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
7106 && 0 != (tem
= const_binop (TREE_CODE (arg0
) == PLUS_EXPR
7107 ? MINUS_EXPR
: PLUS_EXPR
,
7108 arg1
, TREE_OPERAND (arg0
, 1), 0))
7109 && ! TREE_CONSTANT_OVERFLOW (tem
))
7110 return fold (build (code
, type
, TREE_OPERAND (arg0
, 0), tem
));
7112 /* Likewise, we can simplify a comparison of a real constant with
7113 a MINUS_EXPR whose first operand is also a real constant, i.e.
7114 (c1 - x) < c2 becomes x > c1-c2. */
7115 if (flag_unsafe_math_optimizations
7116 && TREE_CODE (arg1
) == REAL_CST
7117 && TREE_CODE (arg0
) == MINUS_EXPR
7118 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == REAL_CST
7119 && 0 != (tem
= const_binop (MINUS_EXPR
, TREE_OPERAND (arg0
, 0),
7121 && ! TREE_CONSTANT_OVERFLOW (tem
))
7122 return fold (build (swap_tree_comparison (code
), type
,
7123 TREE_OPERAND (arg0
, 1), tem
));
7125 /* Fold comparisons against built-in math functions. */
7126 if (TREE_CODE (arg1
) == REAL_CST
7127 && flag_unsafe_math_optimizations
7128 && ! flag_errno_math
)
7130 enum built_in_function fcode
= builtin_mathfn_code (arg0
);
7132 if (fcode
!= END_BUILTINS
)
7134 tem
= fold_mathfn_compare (fcode
, code
, type
, arg0
, arg1
);
7135 if (tem
!= NULL_TREE
)
7141 /* Convert foo++ == CONST into ++foo == CONST + INCR.
7142 First, see if one arg is constant; find the constant arg
7143 and the other one. */
7145 tree constop
= 0, varop
= NULL_TREE
;
7146 int constopnum
= -1;
7148 if (TREE_CONSTANT (arg1
))
7149 constopnum
= 1, constop
= arg1
, varop
= arg0
;
7150 if (TREE_CONSTANT (arg0
))
7151 constopnum
= 0, constop
= arg0
, varop
= arg1
;
7153 if (constop
&& TREE_CODE (varop
) == POSTINCREMENT_EXPR
)
7155 /* This optimization is invalid for ordered comparisons
7156 if CONST+INCR overflows or if foo+incr might overflow.
7157 This optimization is invalid for floating point due to rounding.
7158 For pointer types we assume overflow doesn't happen. */
7159 if (POINTER_TYPE_P (TREE_TYPE (varop
))
7160 || (! FLOAT_TYPE_P (TREE_TYPE (varop
))
7161 && (code
== EQ_EXPR
|| code
== NE_EXPR
)))
7164 = fold (build (PLUS_EXPR
, TREE_TYPE (varop
),
7165 constop
, TREE_OPERAND (varop
, 1)));
7167 /* Do not overwrite the current varop to be a preincrement,
7168 create a new node so that we won't confuse our caller who
7169 might create trees and throw them away, reusing the
7170 arguments that they passed to build. This shows up in
7171 the THEN or ELSE parts of ?: being postincrements. */
7172 varop
= build (PREINCREMENT_EXPR
, TREE_TYPE (varop
),
7173 TREE_OPERAND (varop
, 0),
7174 TREE_OPERAND (varop
, 1));
7176 /* If VAROP is a reference to a bitfield, we must mask
7177 the constant by the width of the field. */
7178 if (TREE_CODE (TREE_OPERAND (varop
, 0)) == COMPONENT_REF
7179 && DECL_BIT_FIELD(TREE_OPERAND
7180 (TREE_OPERAND (varop
, 0), 1)))
7183 = TREE_INT_CST_LOW (DECL_SIZE
7185 (TREE_OPERAND (varop
, 0), 1)));
7186 tree mask
, unsigned_type
;
7187 unsigned int precision
;
7188 tree folded_compare
;
7190 /* First check whether the comparison would come out
7191 always the same. If we don't do that we would
7192 change the meaning with the masking. */
7193 if (constopnum
== 0)
7194 folded_compare
= fold (build (code
, type
, constop
,
7195 TREE_OPERAND (varop
, 0)));
7197 folded_compare
= fold (build (code
, type
,
7198 TREE_OPERAND (varop
, 0),
7200 if (integer_zerop (folded_compare
)
7201 || integer_onep (folded_compare
))
7202 return omit_one_operand (type
, folded_compare
, varop
);
7204 unsigned_type
= (*lang_hooks
.types
.type_for_size
)(size
, 1);
7205 precision
= TYPE_PRECISION (unsigned_type
);
7206 mask
= build_int_2 (~0, ~0);
7207 TREE_TYPE (mask
) = unsigned_type
;
7208 force_fit_type (mask
, 0);
7209 mask
= const_binop (RSHIFT_EXPR
, mask
,
7210 size_int (precision
- size
), 0);
7211 newconst
= fold (build (BIT_AND_EXPR
,
7212 TREE_TYPE (varop
), newconst
,
7213 convert (TREE_TYPE (varop
),
7217 t
= build (code
, type
,
7218 (constopnum
== 0) ? newconst
: varop
,
7219 (constopnum
== 1) ? newconst
: varop
);
7223 else if (constop
&& TREE_CODE (varop
) == POSTDECREMENT_EXPR
)
7225 if (POINTER_TYPE_P (TREE_TYPE (varop
))
7226 || (! FLOAT_TYPE_P (TREE_TYPE (varop
))
7227 && (code
== EQ_EXPR
|| code
== NE_EXPR
)))
7230 = fold (build (MINUS_EXPR
, TREE_TYPE (varop
),
7231 constop
, TREE_OPERAND (varop
, 1)));
7233 /* Do not overwrite the current varop to be a predecrement,
7234 create a new node so that we won't confuse our caller who
7235 might create trees and throw them away, reusing the
7236 arguments that they passed to build. This shows up in
7237 the THEN or ELSE parts of ?: being postdecrements. */
7238 varop
= build (PREDECREMENT_EXPR
, TREE_TYPE (varop
),
7239 TREE_OPERAND (varop
, 0),
7240 TREE_OPERAND (varop
, 1));
7242 if (TREE_CODE (TREE_OPERAND (varop
, 0)) == COMPONENT_REF
7243 && DECL_BIT_FIELD(TREE_OPERAND
7244 (TREE_OPERAND (varop
, 0), 1)))
7247 = TREE_INT_CST_LOW (DECL_SIZE
7249 (TREE_OPERAND (varop
, 0), 1)));
7250 tree mask
, unsigned_type
;
7251 unsigned int precision
;
7252 tree folded_compare
;
7254 if (constopnum
== 0)
7255 folded_compare
= fold (build (code
, type
, constop
,
7256 TREE_OPERAND (varop
, 0)));
7258 folded_compare
= fold (build (code
, type
,
7259 TREE_OPERAND (varop
, 0),
7261 if (integer_zerop (folded_compare
)
7262 || integer_onep (folded_compare
))
7263 return omit_one_operand (type
, folded_compare
, varop
);
7265 unsigned_type
= (*lang_hooks
.types
.type_for_size
)(size
, 1);
7266 precision
= TYPE_PRECISION (unsigned_type
);
7267 mask
= build_int_2 (~0, ~0);
7268 TREE_TYPE (mask
) = TREE_TYPE (varop
);
7269 force_fit_type (mask
, 0);
7270 mask
= const_binop (RSHIFT_EXPR
, mask
,
7271 size_int (precision
- size
), 0);
7272 newconst
= fold (build (BIT_AND_EXPR
,
7273 TREE_TYPE (varop
), newconst
,
7274 convert (TREE_TYPE (varop
),
7278 t
= build (code
, type
,
7279 (constopnum
== 0) ? newconst
: varop
,
7280 (constopnum
== 1) ? newconst
: varop
);
7286 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
7287 This transformation affects the cases which are handled in later
7288 optimizations involving comparisons with non-negative constants. */
7289 if (TREE_CODE (arg1
) == INTEGER_CST
7290 && TREE_CODE (arg0
) != INTEGER_CST
7291 && tree_int_cst_sgn (arg1
) > 0)
7296 arg1
= const_binop (MINUS_EXPR
, arg1
, integer_one_node
, 0);
7297 return fold (build (GT_EXPR
, type
, arg0
, arg1
));
7300 arg1
= const_binop (MINUS_EXPR
, arg1
, integer_one_node
, 0);
7301 return fold (build (LE_EXPR
, type
, arg0
, arg1
));
7308 /* Comparisons with the highest or lowest possible integer of
7309 the specified size will have known values. */
7311 int width
= GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1
)));
7313 if (TREE_CODE (arg1
) == INTEGER_CST
7314 && ! TREE_CONSTANT_OVERFLOW (arg1
)
7315 && width
<= HOST_BITS_PER_WIDE_INT
7316 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
7317 || POINTER_TYPE_P (TREE_TYPE (arg1
))))
7319 unsigned HOST_WIDE_INT signed_max
;
7320 unsigned HOST_WIDE_INT max
, min
;
7322 signed_max
= ((unsigned HOST_WIDE_INT
) 1 << (width
- 1)) - 1;
7324 if (TREE_UNSIGNED (TREE_TYPE (arg1
)))
7326 max
= ((unsigned HOST_WIDE_INT
) 2 << (width
- 1)) - 1;
7332 min
= ((unsigned HOST_WIDE_INT
) -1 << (width
- 1));
7335 if (TREE_INT_CST_HIGH (arg1
) == 0
7336 && TREE_INT_CST_LOW (arg1
) == max
)
7340 return omit_one_operand (type
,
7341 convert (type
, integer_zero_node
),
7344 return fold (build (EQ_EXPR
, type
, arg0
, arg1
));
7347 return omit_one_operand (type
,
7348 convert (type
, integer_one_node
),
7351 return fold (build (NE_EXPR
, type
, arg0
, arg1
));
7353 /* The GE_EXPR and LT_EXPR cases above are not normally
7354 reached because of previous transformations. */
7359 else if (TREE_INT_CST_HIGH (arg1
) == 0
7360 && TREE_INT_CST_LOW (arg1
) == max
- 1)
7364 arg1
= const_binop (PLUS_EXPR
, arg1
, integer_one_node
, 0);
7365 return fold (build (EQ_EXPR
, type
, arg0
, arg1
));
7367 arg1
= const_binop (PLUS_EXPR
, arg1
, integer_one_node
, 0);
7368 return fold (build (NE_EXPR
, type
, arg0
, arg1
));
7372 else if (TREE_INT_CST_HIGH (arg1
) == (min
? -1 : 0)
7373 && TREE_INT_CST_LOW (arg1
) == min
)
7377 return omit_one_operand (type
,
7378 convert (type
, integer_zero_node
),
7381 return fold (build (EQ_EXPR
, type
, arg0
, arg1
));
7384 return omit_one_operand (type
,
7385 convert (type
, integer_one_node
),
7388 return fold (build (NE_EXPR
, type
, arg0
, arg1
));
7393 else if (TREE_INT_CST_HIGH (arg1
) == (min
? -1 : 0)
7394 && TREE_INT_CST_LOW (arg1
) == min
+ 1)
7398 arg1
= const_binop (MINUS_EXPR
, arg1
, integer_one_node
, 0);
7399 return fold (build (NE_EXPR
, type
, arg0
, arg1
));
7401 arg1
= const_binop (MINUS_EXPR
, arg1
, integer_one_node
, 0);
7402 return fold (build (EQ_EXPR
, type
, arg0
, arg1
));
7407 else if (TREE_INT_CST_HIGH (arg1
) == 0
7408 && TREE_INT_CST_LOW (arg1
) == signed_max
7409 && TREE_UNSIGNED (TREE_TYPE (arg1
))
7410 /* signed_type does not work on pointer types. */
7411 && INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
7413 /* The following case also applies to X < signed_max+1
7414 and X >= signed_max+1 because previous transformations. */
7415 if (code
== LE_EXPR
|| code
== GT_EXPR
)
7418 st0
= (*lang_hooks
.types
.signed_type
) (TREE_TYPE (arg0
));
7419 st1
= (*lang_hooks
.types
.signed_type
) (TREE_TYPE (arg1
));
7421 (build (code
== LE_EXPR
? GE_EXPR
: LT_EXPR
,
7422 type
, convert (st0
, arg0
),
7423 convert (st1
, integer_zero_node
)));
7429 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
7430 a MINUS_EXPR of a constant, we can convert it into a comparison with
7431 a revised constant as long as no overflow occurs. */
7432 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
7433 && TREE_CODE (arg1
) == INTEGER_CST
7434 && (TREE_CODE (arg0
) == PLUS_EXPR
7435 || TREE_CODE (arg0
) == MINUS_EXPR
)
7436 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
7437 && 0 != (tem
= const_binop (TREE_CODE (arg0
) == PLUS_EXPR
7438 ? MINUS_EXPR
: PLUS_EXPR
,
7439 arg1
, TREE_OPERAND (arg0
, 1), 0))
7440 && ! TREE_CONSTANT_OVERFLOW (tem
))
7441 return fold (build (code
, type
, TREE_OPERAND (arg0
, 0), tem
));
7443 /* Similarly for a NEGATE_EXPR. */
7444 else if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
7445 && TREE_CODE (arg0
) == NEGATE_EXPR
7446 && TREE_CODE (arg1
) == INTEGER_CST
7447 && 0 != (tem
= negate_expr (arg1
))
7448 && TREE_CODE (tem
) == INTEGER_CST
7449 && ! TREE_CONSTANT_OVERFLOW (tem
))
7450 return fold (build (code
, type
, TREE_OPERAND (arg0
, 0), tem
));
7452 /* If we have X - Y == 0, we can convert that to X == Y and similarly
7453 for !=. Don't do this for ordered comparisons due to overflow. */
7454 else if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
7455 && integer_zerop (arg1
) && TREE_CODE (arg0
) == MINUS_EXPR
)
7456 return fold (build (code
, type
,
7457 TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1)));
7459 /* If we are widening one operand of an integer comparison,
7460 see if the other operand is similarly being widened. Perhaps we
7461 can do the comparison in the narrower type. */
7462 else if (TREE_CODE (TREE_TYPE (arg0
)) == INTEGER_TYPE
7463 && TREE_CODE (arg0
) == NOP_EXPR
7464 && (tem
= get_unwidened (arg0
, NULL_TREE
)) != arg0
7465 && (t1
= get_unwidened (arg1
, TREE_TYPE (tem
))) != 0
7466 && (TREE_TYPE (t1
) == TREE_TYPE (tem
)
7467 || (TREE_CODE (t1
) == INTEGER_CST
7468 && int_fits_type_p (t1
, TREE_TYPE (tem
)))))
7469 return fold (build (code
, type
, tem
, convert (TREE_TYPE (tem
), t1
)));
7471 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
7472 constant, we can simplify it. */
7473 else if (TREE_CODE (arg1
) == INTEGER_CST
7474 && (TREE_CODE (arg0
) == MIN_EXPR
7475 || TREE_CODE (arg0
) == MAX_EXPR
)
7476 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
7477 return optimize_minmax_comparison (t
);
7479 /* If we are comparing an ABS_EXPR with a constant, we can
7480 convert all the cases into explicit comparisons, but they may
7481 well not be faster than doing the ABS and one comparison.
7482 But ABS (X) <= C is a range comparison, which becomes a subtraction
7483 and a comparison, and is probably faster. */
7484 else if (code
== LE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
7485 && TREE_CODE (arg0
) == ABS_EXPR
7486 && ! TREE_SIDE_EFFECTS (arg0
)
7487 && (0 != (tem
= negate_expr (arg1
)))
7488 && TREE_CODE (tem
) == INTEGER_CST
7489 && ! TREE_CONSTANT_OVERFLOW (tem
))
7490 return fold (build (TRUTH_ANDIF_EXPR
, type
,
7491 build (GE_EXPR
, type
, TREE_OPERAND (arg0
, 0), tem
),
7492 build (LE_EXPR
, type
,
7493 TREE_OPERAND (arg0
, 0), arg1
)));
7495 /* If this is an EQ or NE comparison with zero and ARG0 is
7496 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
7497 two operations, but the latter can be done in one less insn
7498 on machines that have only two-operand insns or on which a
7499 constant cannot be the first operand. */
7500 if (integer_zerop (arg1
) && (code
== EQ_EXPR
|| code
== NE_EXPR
)
7501 && TREE_CODE (arg0
) == BIT_AND_EXPR
)
7503 if (TREE_CODE (TREE_OPERAND (arg0
, 0)) == LSHIFT_EXPR
7504 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0)))
7506 fold (build (code
, type
,
7507 build (BIT_AND_EXPR
, TREE_TYPE (arg0
),
7509 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
7510 TREE_OPERAND (arg0
, 1),
7511 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1)),
7512 convert (TREE_TYPE (arg0
),
7515 else if (TREE_CODE (TREE_OPERAND (arg0
, 1)) == LSHIFT_EXPR
7516 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0
, 1), 0)))
7518 fold (build (code
, type
,
7519 build (BIT_AND_EXPR
, TREE_TYPE (arg0
),
7521 TREE_TYPE (TREE_OPERAND (arg0
, 1)),
7522 TREE_OPERAND (arg0
, 0),
7523 TREE_OPERAND (TREE_OPERAND (arg0
, 1), 1)),
7524 convert (TREE_TYPE (arg0
),
7529 /* If this is an NE or EQ comparison of zero against the result of a
7530 signed MOD operation whose second operand is a power of 2, make
7531 the MOD operation unsigned since it is simpler and equivalent. */
7532 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
7533 && integer_zerop (arg1
)
7534 && ! TREE_UNSIGNED (TREE_TYPE (arg0
))
7535 && (TREE_CODE (arg0
) == TRUNC_MOD_EXPR
7536 || TREE_CODE (arg0
) == CEIL_MOD_EXPR
7537 || TREE_CODE (arg0
) == FLOOR_MOD_EXPR
7538 || TREE_CODE (arg0
) == ROUND_MOD_EXPR
)
7539 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
7541 tree newtype
= (*lang_hooks
.types
.unsigned_type
) (TREE_TYPE (arg0
));
7542 tree newmod
= build (TREE_CODE (arg0
), newtype
,
7543 convert (newtype
, TREE_OPERAND (arg0
, 0)),
7544 convert (newtype
, TREE_OPERAND (arg0
, 1)));
7546 return build (code
, type
, newmod
, convert (newtype
, arg1
));
7549 /* If this is an NE comparison of zero with an AND of one, remove the
7550 comparison since the AND will give the correct value. */
7551 if (code
== NE_EXPR
&& integer_zerop (arg1
)
7552 && TREE_CODE (arg0
) == BIT_AND_EXPR
7553 && integer_onep (TREE_OPERAND (arg0
, 1)))
7554 return convert (type
, arg0
);
7556 /* If we have (A & C) == C where C is a power of 2, convert this into
7557 (A & C) != 0. Similarly for NE_EXPR. */
7558 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
7559 && TREE_CODE (arg0
) == BIT_AND_EXPR
7560 && integer_pow2p (TREE_OPERAND (arg0
, 1))
7561 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
7562 return fold (build (code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
7563 arg0
, integer_zero_node
));
7565 /* If we have (A & C) != 0 or (A & C) == 0 and C is a power of
7566 2, then fold the expression into shifts and logical operations. */
7567 tem
= fold_single_bit_test (code
, arg0
, arg1
, type
);
7571 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
7572 Similarly for NE_EXPR. */
7573 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
7574 && TREE_CODE (arg0
) == BIT_AND_EXPR
7575 && TREE_CODE (arg1
) == INTEGER_CST
7576 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
7579 = fold (build (BIT_AND_EXPR
, TREE_TYPE (arg0
),
7580 arg1
, build1 (BIT_NOT_EXPR
,
7581 TREE_TYPE (TREE_OPERAND (arg0
, 1)),
7582 TREE_OPERAND (arg0
, 1))));
7583 tree rslt
= code
== EQ_EXPR
? integer_zero_node
: integer_one_node
;
7584 if (integer_nonzerop (dandnotc
))
7585 return omit_one_operand (type
, rslt
, arg0
);
7588 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
7589 Similarly for NE_EXPR. */
7590 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
7591 && TREE_CODE (arg0
) == BIT_IOR_EXPR
7592 && TREE_CODE (arg1
) == INTEGER_CST
7593 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
7596 = fold (build (BIT_AND_EXPR
, TREE_TYPE (arg0
),
7597 TREE_OPERAND (arg0
, 1),
7598 build1 (BIT_NOT_EXPR
, TREE_TYPE (arg1
), arg1
)));
7599 tree rslt
= code
== EQ_EXPR
? integer_zero_node
: integer_one_node
;
7600 if (integer_nonzerop (candnotd
))
7601 return omit_one_operand (type
, rslt
, arg0
);
7604 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
7605 and similarly for >= into !=. */
7606 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
7607 && TREE_UNSIGNED (TREE_TYPE (arg0
))
7608 && TREE_CODE (arg1
) == LSHIFT_EXPR
7609 && integer_onep (TREE_OPERAND (arg1
, 0)))
7610 return build (code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
7611 build (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
7612 TREE_OPERAND (arg1
, 1)),
7613 convert (TREE_TYPE (arg0
), integer_zero_node
));
7615 else if ((code
== LT_EXPR
|| code
== GE_EXPR
)
7616 && TREE_UNSIGNED (TREE_TYPE (arg0
))
7617 && (TREE_CODE (arg1
) == NOP_EXPR
7618 || TREE_CODE (arg1
) == CONVERT_EXPR
)
7619 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
7620 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
7622 build (code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
7623 convert (TREE_TYPE (arg0
),
7624 build (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
7625 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1))),
7626 convert (TREE_TYPE (arg0
), integer_zero_node
));
7628 /* Simplify comparison of something with itself. (For IEEE
7629 floating-point, we can only do some of these simplifications.) */
7630 if (operand_equal_p (arg0
, arg1
, 0))
7635 if (! FLOAT_TYPE_P (TREE_TYPE (arg0
))
7636 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))))
7637 return constant_boolean_node (1, type
);
7642 if (! FLOAT_TYPE_P (TREE_TYPE (arg0
))
7643 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))))
7644 return constant_boolean_node (1, type
);
7645 return fold (build (EQ_EXPR
, type
, arg0
, arg1
));
7648 /* For NE, we can only do this simplification if integer
7649 or we don't honor IEEE floating point NaNs. */
7650 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
7651 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))))
7653 /* ... fall through ... */
7656 return constant_boolean_node (0, type
);
7662 /* If we are comparing an expression that just has comparisons
7663 of two integer values, arithmetic expressions of those comparisons,
7664 and constants, we can simplify it. There are only three cases
7665 to check: the two values can either be equal, the first can be
7666 greater, or the second can be greater. Fold the expression for
7667 those three values. Since each value must be 0 or 1, we have
7668 eight possibilities, each of which corresponds to the constant 0
7669 or 1 or one of the six possible comparisons.
7671 This handles common cases like (a > b) == 0 but also handles
7672 expressions like ((x > y) - (y > x)) > 0, which supposedly
7673 occur in macroized code. */
7675 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
7677 tree cval1
= 0, cval2
= 0;
7680 if (twoval_comparison_p (arg0
, &cval1
, &cval2
, &save_p
)
7681 /* Don't handle degenerate cases here; they should already
7682 have been handled anyway. */
7683 && cval1
!= 0 && cval2
!= 0
7684 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
7685 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
7686 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
7687 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
7688 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
7689 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
7690 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
7692 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
7693 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
7695 /* We can't just pass T to eval_subst in case cval1 or cval2
7696 was the same as ARG1. */
7699 = fold (build (code
, type
,
7700 eval_subst (arg0
, cval1
, maxval
, cval2
, minval
),
7703 = fold (build (code
, type
,
7704 eval_subst (arg0
, cval1
, maxval
, cval2
, maxval
),
7707 = fold (build (code
, type
,
7708 eval_subst (arg0
, cval1
, minval
, cval2
, maxval
),
7711 /* All three of these results should be 0 or 1. Confirm they
7712 are. Then use those values to select the proper code
7715 if ((integer_zerop (high_result
)
7716 || integer_onep (high_result
))
7717 && (integer_zerop (equal_result
)
7718 || integer_onep (equal_result
))
7719 && (integer_zerop (low_result
)
7720 || integer_onep (low_result
)))
7722 /* Make a 3-bit mask with the high-order bit being the
7723 value for `>', the next for '=', and the low for '<'. */
7724 switch ((integer_onep (high_result
) * 4)
7725 + (integer_onep (equal_result
) * 2)
7726 + integer_onep (low_result
))
7730 return omit_one_operand (type
, integer_zero_node
, arg0
);
7751 return omit_one_operand (type
, integer_one_node
, arg0
);
7754 t
= build (code
, type
, cval1
, cval2
);
7756 return save_expr (t
);
7763 /* If this is a comparison of a field, we may be able to simplify it. */
7764 if (((TREE_CODE (arg0
) == COMPONENT_REF
7765 && (*lang_hooks
.can_use_bit_fields_p
) ())
7766 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
7767 && (code
== EQ_EXPR
|| code
== NE_EXPR
)
7768 /* Handle the constant case even without -O
7769 to make sure the warnings are given. */
7770 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
7772 t1
= optimize_bit_field_compare (code
, type
, arg0
, arg1
);
7777 /* If this is a comparison of complex values and either or both sides
7778 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
7779 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
7780 This may prevent needless evaluations. */
7781 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
7782 && TREE_CODE (TREE_TYPE (arg0
)) == COMPLEX_TYPE
7783 && (TREE_CODE (arg0
) == COMPLEX_EXPR
7784 || TREE_CODE (arg1
) == COMPLEX_EXPR
7785 || TREE_CODE (arg0
) == COMPLEX_CST
7786 || TREE_CODE (arg1
) == COMPLEX_CST
))
7788 tree subtype
= TREE_TYPE (TREE_TYPE (arg0
));
7789 tree real0
, imag0
, real1
, imag1
;
7791 arg0
= save_expr (arg0
);
7792 arg1
= save_expr (arg1
);
7793 real0
= fold (build1 (REALPART_EXPR
, subtype
, arg0
));
7794 imag0
= fold (build1 (IMAGPART_EXPR
, subtype
, arg0
));
7795 real1
= fold (build1 (REALPART_EXPR
, subtype
, arg1
));
7796 imag1
= fold (build1 (IMAGPART_EXPR
, subtype
, arg1
));
7798 return fold (build ((code
== EQ_EXPR
? TRUTH_ANDIF_EXPR
7801 fold (build (code
, type
, real0
, real1
)),
7802 fold (build (code
, type
, imag0
, imag1
))));
7805 /* Optimize comparisons of strlen vs zero to a compare of the
7806 first character of the string vs zero. To wit,
7807 strlen(ptr) == 0 => *ptr == 0
7808 strlen(ptr) != 0 => *ptr != 0
7809 Other cases should reduce to one of these two (or a constant)
7810 due to the return value of strlen being unsigned. */
7811 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
7812 && integer_zerop (arg1
)
7813 && TREE_CODE (arg0
) == CALL_EXPR
)
7815 tree fndecl
= get_callee_fndecl (arg0
);
7819 && DECL_BUILT_IN (fndecl
)
7820 && DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_MD
7821 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STRLEN
7822 && (arglist
= TREE_OPERAND (arg0
, 1))
7823 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) == POINTER_TYPE
7824 && ! TREE_CHAIN (arglist
))
7825 return fold (build (code
, type
,
7826 build1 (INDIRECT_REF
, char_type_node
,
7827 TREE_VALUE(arglist
)),
7828 integer_zero_node
));
7831 /* From here on, the only cases we handle are when the result is
7832 known to be a constant.
7834 To compute GT, swap the arguments and do LT.
7835 To compute GE, do LT and invert the result.
7836 To compute LE, swap the arguments, do LT and invert the result.
7837 To compute NE, do EQ and invert the result.
7839 Therefore, the code below must handle only EQ and LT. */
7841 if (code
== LE_EXPR
|| code
== GT_EXPR
)
7843 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
7844 code
= swap_tree_comparison (code
);
7847 /* Note that it is safe to invert for real values here because we
7848 will check below in the one case that it matters. */
7852 if (code
== NE_EXPR
|| code
== GE_EXPR
)
7855 code
= invert_tree_comparison (code
);
7858 /* Compute a result for LT or EQ if args permit;
7859 otherwise return T. */
7860 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
7862 if (code
== EQ_EXPR
)
7863 t1
= build_int_2 (tree_int_cst_equal (arg0
, arg1
), 0);
7865 t1
= build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0
))
7866 ? INT_CST_LT_UNSIGNED (arg0
, arg1
)
7867 : INT_CST_LT (arg0
, arg1
)),
7871 #if 0 /* This is no longer useful, but breaks some real code. */
7872 /* Assume a nonexplicit constant cannot equal an explicit one,
7873 since such code would be undefined anyway.
7874 Exception: on sysvr4, using #pragma weak,
7875 a label can come out as 0. */
7876 else if (TREE_CODE (arg1
) == INTEGER_CST
7877 && !integer_zerop (arg1
)
7878 && TREE_CONSTANT (arg0
)
7879 && TREE_CODE (arg0
) == ADDR_EXPR
7881 t1
= build_int_2 (0, 0);
7883 /* Two real constants can be compared explicitly. */
7884 else if (TREE_CODE (arg0
) == REAL_CST
&& TREE_CODE (arg1
) == REAL_CST
)
7886 /* If either operand is a NaN, the result is false with two
7887 exceptions: First, an NE_EXPR is true on NaNs, but that case
7888 is already handled correctly since we will be inverting the
7889 result for NE_EXPR. Second, if we had inverted a LE_EXPR
7890 or a GE_EXPR into a LT_EXPR, we must return true so that it
7891 will be inverted into false. */
7893 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0
))
7894 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
)))
7895 t1
= build_int_2 (invert
&& code
== LT_EXPR
, 0);
7897 else if (code
== EQ_EXPR
)
7898 t1
= build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0
),
7899 TREE_REAL_CST (arg1
)),
7902 t1
= build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0
),
7903 TREE_REAL_CST (arg1
)),
7907 if (t1
== NULL_TREE
)
7911 TREE_INT_CST_LOW (t1
) ^= 1;
7913 TREE_TYPE (t1
) = type
;
7914 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
7915 return (*lang_hooks
.truthvalue_conversion
) (t1
);
7919 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
7920 so all simple results must be passed through pedantic_non_lvalue. */
7921 if (TREE_CODE (arg0
) == INTEGER_CST
)
7923 tem
= TREE_OPERAND (t
, (integer_zerop (arg0
) ? 2 : 1));
7924 /* Only optimize constant conditions when the selected branch
7925 has the same type as the COND_EXPR. This avoids optimizing
7926 away "c ? x : throw", where the throw has a void type. */
7927 if (! VOID_TYPE_P (TREE_TYPE (tem
))
7928 || VOID_TYPE_P (TREE_TYPE (t
)))
7929 return pedantic_non_lvalue (tem
);
7932 if (operand_equal_p (arg1
, TREE_OPERAND (expr
, 2), 0))
7933 return pedantic_omit_one_operand (type
, arg1
, arg0
);
7935 /* If we have A op B ? A : C, we may be able to convert this to a
7936 simpler expression, depending on the operation and the values
7937 of B and C. Signed zeros prevent all of these transformations,
7938 for reasons given above each one. */
7940 if (TREE_CODE_CLASS (TREE_CODE (arg0
)) == '<'
7941 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
7942 arg1
, TREE_OPERAND (arg0
, 1))
7943 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
7945 tree arg2
= TREE_OPERAND (t
, 2);
7946 enum tree_code comp_code
= TREE_CODE (arg0
);
7950 /* If we have A op 0 ? A : -A, consider applying the following
7953 A == 0? A : -A same as -A
7954 A != 0? A : -A same as A
7955 A >= 0? A : -A same as abs (A)
7956 A > 0? A : -A same as abs (A)
7957 A <= 0? A : -A same as -abs (A)
7958 A < 0? A : -A same as -abs (A)
7960 None of these transformations work for modes with signed
7961 zeros. If A is +/-0, the first two transformations will
7962 change the sign of the result (from +0 to -0, or vice
7963 versa). The last four will fix the sign of the result,
7964 even though the original expressions could be positive or
7965 negative, depending on the sign of A.
7967 Note that all these transformations are correct if A is
7968 NaN, since the two alternatives (A and -A) are also NaNs. */
7969 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 1)))
7970 ? real_zerop (TREE_OPERAND (arg0
, 1))
7971 : integer_zerop (TREE_OPERAND (arg0
, 1)))
7972 && TREE_CODE (arg2
) == NEGATE_EXPR
7973 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
7981 (convert (TREE_TYPE (TREE_OPERAND (t
, 1)),
7984 return pedantic_non_lvalue (convert (type
, arg1
));
7987 if (TREE_UNSIGNED (TREE_TYPE (arg1
)))
7988 arg1
= convert ((*lang_hooks
.types
.signed_type
)
7989 (TREE_TYPE (arg1
)), arg1
);
7990 return pedantic_non_lvalue
7991 (convert (type
, fold (build1 (ABS_EXPR
,
7992 TREE_TYPE (arg1
), arg1
))));
7995 if (TREE_UNSIGNED (TREE_TYPE (arg1
)))
7996 arg1
= convert ((lang_hooks
.types
.signed_type
)
7997 (TREE_TYPE (arg1
)), arg1
);
7998 return pedantic_non_lvalue
7999 (negate_expr (convert (type
,
8000 fold (build1 (ABS_EXPR
,
8007 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
8008 A == 0 ? A : 0 is always 0 unless A is -0. Note that
8009 both transformations are correct when A is NaN: A != 0
8010 is then true, and A == 0 is false. */
8012 if (integer_zerop (TREE_OPERAND (arg0
, 1)) && integer_zerop (arg2
))
8014 if (comp_code
== NE_EXPR
)
8015 return pedantic_non_lvalue (convert (type
, arg1
));
8016 else if (comp_code
== EQ_EXPR
)
8017 return pedantic_non_lvalue (convert (type
, integer_zero_node
));
8020 /* Try some transformations of A op B ? A : B.
8022 A == B? A : B same as B
8023 A != B? A : B same as A
8024 A >= B? A : B same as max (A, B)
8025 A > B? A : B same as max (B, A)
8026 A <= B? A : B same as min (A, B)
8027 A < B? A : B same as min (B, A)
8029 As above, these transformations don't work in the presence
8030 of signed zeros. For example, if A and B are zeros of
8031 opposite sign, the first two transformations will change
8032 the sign of the result. In the last four, the original
8033 expressions give different results for (A=+0, B=-0) and
8034 (A=-0, B=+0), but the transformed expressions do not.
8036 The first two transformations are correct if either A or B
8037 is a NaN. In the first transformation, the condition will
8038 be false, and B will indeed be chosen. In the case of the
8039 second transformation, the condition A != B will be true,
8040 and A will be chosen.
8042 The conversions to max() and min() are not correct if B is
8043 a number and A is not. The conditions in the original
8044 expressions will be false, so all four give B. The min()
8045 and max() versions would give a NaN instead. */
8046 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 1),
8047 arg2
, TREE_OPERAND (arg0
, 0)))
8049 tree comp_op0
= TREE_OPERAND (arg0
, 0);
8050 tree comp_op1
= TREE_OPERAND (arg0
, 1);
8051 tree comp_type
= TREE_TYPE (comp_op0
);
8053 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
8054 if (TYPE_MAIN_VARIANT (comp_type
) == TYPE_MAIN_VARIANT (type
))
8064 return pedantic_non_lvalue (convert (type
, arg2
));
8066 return pedantic_non_lvalue (convert (type
, arg1
));
8069 /* In C++ a ?: expression can be an lvalue, so put the
8070 operand which will be used if they are equal first
8071 so that we can convert this back to the
8072 corresponding COND_EXPR. */
8073 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1
))))
8074 return pedantic_non_lvalue
8075 (convert (type
, fold (build (MIN_EXPR
, comp_type
,
8076 (comp_code
== LE_EXPR
8077 ? comp_op0
: comp_op1
),
8078 (comp_code
== LE_EXPR
8079 ? comp_op1
: comp_op0
)))));
8083 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1
))))
8084 return pedantic_non_lvalue
8085 (convert (type
, fold (build (MAX_EXPR
, comp_type
,
8086 (comp_code
== GE_EXPR
8087 ? comp_op0
: comp_op1
),
8088 (comp_code
== GE_EXPR
8089 ? comp_op1
: comp_op0
)))));
8096 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
8097 we might still be able to simplify this. For example,
8098 if C1 is one less or one more than C2, this might have started
8099 out as a MIN or MAX and been transformed by this function.
8100 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
8102 if (INTEGRAL_TYPE_P (type
)
8103 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8104 && TREE_CODE (arg2
) == INTEGER_CST
)
8108 /* We can replace A with C1 in this case. */
8109 arg1
= convert (type
, TREE_OPERAND (arg0
, 1));
8110 return fold (build (code
, type
, TREE_OPERAND (t
, 0), arg1
,
8111 TREE_OPERAND (t
, 2)));
8114 /* If C1 is C2 + 1, this is min(A, C2). */
8115 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
), 1)
8116 && operand_equal_p (TREE_OPERAND (arg0
, 1),
8117 const_binop (PLUS_EXPR
, arg2
,
8118 integer_one_node
, 0), 1))
8119 return pedantic_non_lvalue
8120 (fold (build (MIN_EXPR
, type
, arg1
, arg2
)));
8124 /* If C1 is C2 - 1, this is min(A, C2). */
8125 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
), 1)
8126 && operand_equal_p (TREE_OPERAND (arg0
, 1),
8127 const_binop (MINUS_EXPR
, arg2
,
8128 integer_one_node
, 0), 1))
8129 return pedantic_non_lvalue
8130 (fold (build (MIN_EXPR
, type
, arg1
, arg2
)));
8134 /* If C1 is C2 - 1, this is max(A, C2). */
8135 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
), 1)
8136 && operand_equal_p (TREE_OPERAND (arg0
, 1),
8137 const_binop (MINUS_EXPR
, arg2
,
8138 integer_one_node
, 0), 1))
8139 return pedantic_non_lvalue
8140 (fold (build (MAX_EXPR
, type
, arg1
, arg2
)));
8144 /* If C1 is C2 + 1, this is max(A, C2). */
8145 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
), 1)
8146 && operand_equal_p (TREE_OPERAND (arg0
, 1),
8147 const_binop (PLUS_EXPR
, arg2
,
8148 integer_one_node
, 0), 1))
8149 return pedantic_non_lvalue
8150 (fold (build (MAX_EXPR
, type
, arg1
, arg2
)));
8159 /* If the second operand is simpler than the third, swap them
8160 since that produces better jump optimization results. */
8161 if (tree_swap_operands_p (TREE_OPERAND (t
, 1),
8162 TREE_OPERAND (t
, 2), false))
8164 /* See if this can be inverted. If it can't, possibly because
8165 it was a floating-point inequality comparison, don't do
8167 tem
= invert_truthvalue (arg0
);
8169 if (TREE_CODE (tem
) != TRUTH_NOT_EXPR
)
8170 return fold (build (code
, type
, tem
,
8171 TREE_OPERAND (t
, 2), TREE_OPERAND (t
, 1)));
8174 /* Convert A ? 1 : 0 to simply A. */
8175 if (integer_onep (TREE_OPERAND (t
, 1))
8176 && integer_zerop (TREE_OPERAND (t
, 2))
8177 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
8178 call to fold will try to move the conversion inside
8179 a COND, which will recurse. In that case, the COND_EXPR
8180 is probably the best choice, so leave it alone. */
8181 && type
== TREE_TYPE (arg0
))
8182 return pedantic_non_lvalue (arg0
);
8184 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
8185 over COND_EXPR in cases such as floating point comparisons. */
8186 if (integer_zerop (TREE_OPERAND (t
, 1))
8187 && integer_onep (TREE_OPERAND (t
, 2))
8188 && truth_value_p (TREE_CODE (arg0
)))
8189 return pedantic_non_lvalue (convert (type
,
8190 invert_truthvalue (arg0
)));
8192 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
8193 operation is simply A & 2. */
8195 if (integer_zerop (TREE_OPERAND (t
, 2))
8196 && TREE_CODE (arg0
) == NE_EXPR
8197 && integer_zerop (TREE_OPERAND (arg0
, 1))
8198 && integer_pow2p (arg1
)
8199 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
8200 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
8202 return pedantic_non_lvalue (convert (type
, TREE_OPERAND (arg0
, 0)));
8204 /* Convert A ? B : 0 into A && B if A and B are truth values. */
8205 if (integer_zerop (TREE_OPERAND (t
, 2))
8206 && truth_value_p (TREE_CODE (arg0
))
8207 && truth_value_p (TREE_CODE (arg1
)))
8208 return pedantic_non_lvalue (fold (build (TRUTH_ANDIF_EXPR
, type
,
8211 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
8212 if (integer_onep (TREE_OPERAND (t
, 2))
8213 && truth_value_p (TREE_CODE (arg0
))
8214 && truth_value_p (TREE_CODE (arg1
)))
8216 /* Only perform transformation if ARG0 is easily inverted. */
8217 tem
= invert_truthvalue (arg0
);
8218 if (TREE_CODE (tem
) != TRUTH_NOT_EXPR
)
8219 return pedantic_non_lvalue (fold (build (TRUTH_ORIF_EXPR
, type
,
8226 /* When pedantic, a compound expression can be neither an lvalue
8227 nor an integer constant expression. */
8228 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
8230 /* Don't let (0, 0) be null pointer constant. */
8231 if (integer_zerop (arg1
))
8232 return pedantic_non_lvalue (build1 (NOP_EXPR
, type
, arg1
));
8233 return pedantic_non_lvalue (convert (type
, arg1
));
8237 return build_complex (type
, arg0
, arg1
);
8241 if (TREE_CODE (TREE_TYPE (arg0
)) != COMPLEX_TYPE
)
8243 else if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
8244 return omit_one_operand (type
, TREE_OPERAND (arg0
, 0),
8245 TREE_OPERAND (arg0
, 1));
8246 else if (TREE_CODE (arg0
) == COMPLEX_CST
)
8247 return TREE_REALPART (arg0
);
8248 else if (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8249 return fold (build (TREE_CODE (arg0
), type
,
8250 fold (build1 (REALPART_EXPR
, type
,
8251 TREE_OPERAND (arg0
, 0))),
8252 fold (build1 (REALPART_EXPR
,
8253 type
, TREE_OPERAND (arg0
, 1)))));
8257 if (TREE_CODE (TREE_TYPE (arg0
)) != COMPLEX_TYPE
)
8258 return convert (type
, integer_zero_node
);
8259 else if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
8260 return omit_one_operand (type
, TREE_OPERAND (arg0
, 1),
8261 TREE_OPERAND (arg0
, 0));
8262 else if (TREE_CODE (arg0
) == COMPLEX_CST
)
8263 return TREE_IMAGPART (arg0
);
8264 else if (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8265 return fold (build (TREE_CODE (arg0
), type
,
8266 fold (build1 (IMAGPART_EXPR
, type
,
8267 TREE_OPERAND (arg0
, 0))),
8268 fold (build1 (IMAGPART_EXPR
, type
,
8269 TREE_OPERAND (arg0
, 1)))));
8272 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
8274 case CLEANUP_POINT_EXPR
:
8275 if (! has_cleanups (arg0
))
8276 return TREE_OPERAND (t
, 0);
8279 enum tree_code code0
= TREE_CODE (arg0
);
8280 int kind0
= TREE_CODE_CLASS (code0
);
8281 tree arg00
= TREE_OPERAND (arg0
, 0);
8284 if (kind0
== '1' || code0
== TRUTH_NOT_EXPR
)
8285 return fold (build1 (code0
, type
,
8286 fold (build1 (CLEANUP_POINT_EXPR
,
8287 TREE_TYPE (arg00
), arg00
))));
8289 if (kind0
== '<' || kind0
== '2'
8290 || code0
== TRUTH_ANDIF_EXPR
|| code0
== TRUTH_ORIF_EXPR
8291 || code0
== TRUTH_AND_EXPR
|| code0
== TRUTH_OR_EXPR
8292 || code0
== TRUTH_XOR_EXPR
)
8294 arg01
= TREE_OPERAND (arg0
, 1);
8296 if (TREE_CONSTANT (arg00
)
8297 || ((code0
== TRUTH_ANDIF_EXPR
|| code0
== TRUTH_ORIF_EXPR
)
8298 && ! has_cleanups (arg00
)))
8299 return fold (build (code0
, type
, arg00
,
8300 fold (build1 (CLEANUP_POINT_EXPR
,
8301 TREE_TYPE (arg01
), arg01
))));
8303 if (TREE_CONSTANT (arg01
))
8304 return fold (build (code0
, type
,
8305 fold (build1 (CLEANUP_POINT_EXPR
,
8306 TREE_TYPE (arg00
), arg00
)),
8314 /* Check for a built-in function. */
8315 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == ADDR_EXPR
8316 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (expr
, 0), 0))
8318 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (expr
, 0), 0)))
8320 tree tmp
= fold_builtin (expr
);
8328 } /* switch (code) */
8331 #ifdef ENABLE_FOLD_CHECKING
8334 static void fold_checksum_tree (tree
, struct md5_ctx
*, htab_t
);
8335 static void fold_check_failed (tree
, tree
);
8336 void print_fold_checksum (tree
);
8338 /* When --enable-checking=fold, compute a digest of expr before
8339 and after actual fold call to see if fold did not accidentally
8340 change original expr. */
8347 unsigned char checksum_before
[16], checksum_after
[16];
8350 ht
= htab_create (32, htab_hash_pointer
, htab_eq_pointer
, NULL
);
8351 md5_init_ctx (&ctx
);
8352 fold_checksum_tree (expr
, &ctx
, ht
);
8353 md5_finish_ctx (&ctx
, checksum_before
);
8356 ret
= fold_1 (expr
);
8358 md5_init_ctx (&ctx
);
8359 fold_checksum_tree (expr
, &ctx
, ht
);
8360 md5_finish_ctx (&ctx
, checksum_after
);
8363 if (memcmp (checksum_before
, checksum_after
, 16))
8364 fold_check_failed (expr
, ret
);
8370 print_fold_checksum (tree expr
)
8373 unsigned char checksum
[16], cnt
;
8376 ht
= htab_create (32, htab_hash_pointer
, htab_eq_pointer
, NULL
);
8377 md5_init_ctx (&ctx
);
8378 fold_checksum_tree (expr
, &ctx
, ht
);
8379 md5_finish_ctx (&ctx
, checksum
);
8381 for (cnt
= 0; cnt
< 16; ++cnt
)
8382 fprintf (stderr
, "%02x", checksum
[cnt
]);
8383 putc ('\n', stderr
);
8387 fold_check_failed (tree expr ATTRIBUTE_UNUSED
, tree ret ATTRIBUTE_UNUSED
)
8389 internal_error ("fold check: original tree changed by fold");
8393 fold_checksum_tree (tree expr
, struct md5_ctx
*ctx
, htab_t ht
)
8396 enum tree_code code
;
8397 char buf
[sizeof (struct tree_decl
)];
8400 if (sizeof (struct tree_exp
) + 5 * sizeof (tree
)
8401 > sizeof (struct tree_decl
)
8402 || sizeof (struct tree_type
) > sizeof (struct tree_decl
))
8406 slot
= htab_find_slot (ht
, expr
, INSERT
);
8410 code
= TREE_CODE (expr
);
8411 if (code
== SAVE_EXPR
&& SAVE_EXPR_NOPLACEHOLDER (expr
))
8413 /* Allow SAVE_EXPR_NOPLACEHOLDER flag to be modified. */
8414 memcpy (buf
, expr
, tree_size (expr
));
8416 SAVE_EXPR_NOPLACEHOLDER (expr
) = 0;
8418 else if (TREE_CODE_CLASS (code
) == 'd' && DECL_ASSEMBLER_NAME_SET_P (expr
))
8420 /* Allow DECL_ASSEMBLER_NAME to be modified. */
8421 memcpy (buf
, expr
, tree_size (expr
));
8423 SET_DECL_ASSEMBLER_NAME (expr
, NULL
);
8425 else if (TREE_CODE_CLASS (code
) == 't'
8426 && (TYPE_POINTER_TO (expr
) || TYPE_REFERENCE_TO (expr
)))
8428 /* Allow TYPE_POINTER_TO and TYPE_REFERENCE_TO to be modified. */
8429 memcpy (buf
, expr
, tree_size (expr
));
8431 TYPE_POINTER_TO (expr
) = NULL
;
8432 TYPE_REFERENCE_TO (expr
) = NULL
;
8434 md5_process_bytes (expr
, tree_size (expr
), ctx
);
8435 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
8436 if (TREE_CODE_CLASS (code
) != 't' && TREE_CODE_CLASS (code
) != 'd')
8437 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
8438 len
= TREE_CODE_LENGTH (code
);
8439 switch (TREE_CODE_CLASS (code
))
8445 md5_process_bytes (TREE_STRING_POINTER (expr
),
8446 TREE_STRING_LENGTH (expr
), ctx
);
8449 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
8450 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
8453 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr
), ctx
, ht
);
8463 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
8464 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
8467 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
8468 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
8477 case SAVE_EXPR
: len
= 2; break;
8478 case GOTO_SUBROUTINE_EXPR
: len
= 0; break;
8479 case RTL_EXPR
: len
= 0; break;
8480 case WITH_CLEANUP_EXPR
: len
= 2; break;
8489 for (i
= 0; i
< len
; ++i
)
8490 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
8493 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
8494 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
8495 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
8496 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
8497 fold_checksum_tree (DECL_ARGUMENTS (expr
), ctx
, ht
);
8498 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
8499 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
8500 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
8501 fold_checksum_tree (DECL_SECTION_NAME (expr
), ctx
, ht
);
8502 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
8503 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
8506 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
8507 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
8508 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
8509 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
8510 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
8511 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
8512 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
8513 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
8514 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
8515 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
8524 /* Perform constant folding and related simplification of initializer
8525 expression EXPR. This behaves identically to "fold" but ignores
8526 potential run-time traps and exceptions that fold must preserve. */
8529 fold_initializer (tree expr
)
8531 int saved_signaling_nans
= flag_signaling_nans
;
8532 int saved_trapping_math
= flag_trapping_math
;
8533 int saved_trapv
= flag_trapv
;
8536 flag_signaling_nans
= 0;
8537 flag_trapping_math
= 0;
8540 result
= fold (expr
);
8542 flag_signaling_nans
= saved_signaling_nans
;
8543 flag_trapping_math
= saved_trapping_math
;
8544 flag_trapv
= saved_trapv
;
8549 /* Determine if first argument is a multiple of second argument. Return 0 if
8550 it is not, or we cannot easily determined it to be.
8552 An example of the sort of thing we care about (at this point; this routine
8553 could surely be made more general, and expanded to do what the *_DIV_EXPR's
8554 fold cases do now) is discovering that
8556 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
8562 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
8564 This code also handles discovering that
8566 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
8568 is a multiple of 8 so we don't have to worry about dealing with a
8571 Note that we *look* inside a SAVE_EXPR only to determine how it was
8572 calculated; it is not safe for fold to do much of anything else with the
8573 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
8574 at run time. For example, the latter example above *cannot* be implemented
8575 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
8576 evaluation time of the original SAVE_EXPR is not necessarily the same at
8577 the time the new expression is evaluated. The only optimization of this
8578 sort that would be valid is changing
8580 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
8584 SAVE_EXPR (I) * SAVE_EXPR (J)
8586 (where the same SAVE_EXPR (J) is used in the original and the
8587 transformed version). */
8590 multiple_of_p (tree type
, tree top
, tree bottom
)
8592 if (operand_equal_p (top
, bottom
, 0))
8595 if (TREE_CODE (type
) != INTEGER_TYPE
)
8598 switch (TREE_CODE (top
))
8601 return (multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
)
8602 || multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
));
8606 return (multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
)
8607 && multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
));
8610 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
8614 op1
= TREE_OPERAND (top
, 1);
8615 /* const_binop may not detect overflow correctly,
8616 so check for it explicitly here. */
8617 if (TYPE_PRECISION (TREE_TYPE (size_one_node
))
8618 > TREE_INT_CST_LOW (op1
)
8619 && TREE_INT_CST_HIGH (op1
) == 0
8620 && 0 != (t1
= convert (type
,
8621 const_binop (LSHIFT_EXPR
, size_one_node
,
8623 && ! TREE_OVERFLOW (t1
))
8624 return multiple_of_p (type
, t1
, bottom
);
8629 /* Can't handle conversions from non-integral or wider integral type. */
8630 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
8631 || (TYPE_PRECISION (type
)
8632 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
8635 /* .. fall through ... */
8638 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
);
8641 if (TREE_CODE (bottom
) != INTEGER_CST
8642 || (TREE_UNSIGNED (type
)
8643 && (tree_int_cst_sgn (top
) < 0
8644 || tree_int_cst_sgn (bottom
) < 0)))
8646 return integer_zerop (const_binop (TRUNC_MOD_EXPR
,
8654 /* Return true if `t' is known to be non-negative. */
8657 tree_expr_nonnegative_p (tree t
)
8659 switch (TREE_CODE (t
))
8665 return tree_int_cst_sgn (t
) >= 0;
8668 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
8671 if (FLOAT_TYPE_P (TREE_TYPE (t
)))
8672 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0))
8673 && tree_expr_nonnegative_p (TREE_OPERAND (t
, 1));
8675 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
8676 both unsigned and at least 2 bits shorter than the result. */
8677 if (TREE_CODE (TREE_TYPE (t
)) == INTEGER_TYPE
8678 && TREE_CODE (TREE_OPERAND (t
, 0)) == NOP_EXPR
8679 && TREE_CODE (TREE_OPERAND (t
, 1)) == NOP_EXPR
)
8681 tree inner1
= TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0));
8682 tree inner2
= TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 1), 0));
8683 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TREE_UNSIGNED (inner1
)
8684 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TREE_UNSIGNED (inner2
))
8686 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
8687 TYPE_PRECISION (inner2
)) + 1;
8688 return prec
< TYPE_PRECISION (TREE_TYPE (t
));
8694 if (FLOAT_TYPE_P (TREE_TYPE (t
)))
8696 /* x * x for floating point x is always non-negative. */
8697 if (operand_equal_p (TREE_OPERAND (t
, 0), TREE_OPERAND (t
, 1), 0))
8699 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0))
8700 && tree_expr_nonnegative_p (TREE_OPERAND (t
, 1));
8703 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
8704 both unsigned and their total bits is shorter than the result. */
8705 if (TREE_CODE (TREE_TYPE (t
)) == INTEGER_TYPE
8706 && TREE_CODE (TREE_OPERAND (t
, 0)) == NOP_EXPR
8707 && TREE_CODE (TREE_OPERAND (t
, 1)) == NOP_EXPR
)
8709 tree inner1
= TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0));
8710 tree inner2
= TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 1), 0));
8711 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TREE_UNSIGNED (inner1
)
8712 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TREE_UNSIGNED (inner2
))
8713 return TYPE_PRECISION (inner1
) + TYPE_PRECISION (inner2
)
8714 < TYPE_PRECISION (TREE_TYPE (t
));
8718 case TRUNC_DIV_EXPR
:
8720 case FLOOR_DIV_EXPR
:
8721 case ROUND_DIV_EXPR
:
8722 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0))
8723 && tree_expr_nonnegative_p (TREE_OPERAND (t
, 1));
8725 case TRUNC_MOD_EXPR
:
8727 case FLOOR_MOD_EXPR
:
8728 case ROUND_MOD_EXPR
:
8729 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0));
8732 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0))
8733 && tree_expr_nonnegative_p (TREE_OPERAND (t
, 1));
8737 tree inner_type
= TREE_TYPE (TREE_OPERAND (t
, 0));
8738 tree outer_type
= TREE_TYPE (t
);
8740 if (TREE_CODE (outer_type
) == REAL_TYPE
)
8742 if (TREE_CODE (inner_type
) == REAL_TYPE
)
8743 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0));
8744 if (TREE_CODE (inner_type
) == INTEGER_TYPE
)
8746 if (TREE_UNSIGNED (inner_type
))
8748 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0));
8751 else if (TREE_CODE (outer_type
) == INTEGER_TYPE
)
8753 if (TREE_CODE (inner_type
) == REAL_TYPE
)
8754 return tree_expr_nonnegative_p (TREE_OPERAND (t
,0));
8755 if (TREE_CODE (inner_type
) == INTEGER_TYPE
)
8756 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
8757 && TREE_UNSIGNED (inner_type
);
8763 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 1))
8764 && tree_expr_nonnegative_p (TREE_OPERAND (t
, 2));
8766 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 1));
8768 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0))
8769 && tree_expr_nonnegative_p (TREE_OPERAND (t
, 1));
8771 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0))
8772 || tree_expr_nonnegative_p (TREE_OPERAND (t
, 1));
8774 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 1));
8776 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 1));
8778 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0));
8779 case NON_LVALUE_EXPR
:
8780 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0));
8782 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0));
8784 return rtl_expr_nonnegative_p (RTL_EXPR_RTL (t
));
8788 tree fndecl
= get_callee_fndecl (t
);
8789 tree arglist
= TREE_OPERAND (t
, 1);
8791 && DECL_BUILT_IN (fndecl
)
8792 && DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_MD
)
8793 switch (DECL_FUNCTION_CODE (fndecl
))
8796 case BUILT_IN_CABSL
:
8797 case BUILT_IN_CABSF
:
8802 case BUILT_IN_EXP2F
:
8803 case BUILT_IN_EXP2L
:
8804 case BUILT_IN_EXP10
:
8805 case BUILT_IN_EXP10F
:
8806 case BUILT_IN_EXP10L
:
8808 case BUILT_IN_FABSF
:
8809 case BUILT_IN_FABSL
:
8812 case BUILT_IN_FFSLL
:
8813 case BUILT_IN_PARITY
:
8814 case BUILT_IN_PARITYL
:
8815 case BUILT_IN_PARITYLL
:
8816 case BUILT_IN_POPCOUNT
:
8817 case BUILT_IN_POPCOUNTL
:
8818 case BUILT_IN_POPCOUNTLL
:
8819 case BUILT_IN_POW10
:
8820 case BUILT_IN_POW10F
:
8821 case BUILT_IN_POW10L
:
8823 case BUILT_IN_SQRTF
:
8824 case BUILT_IN_SQRTL
:
8828 case BUILT_IN_ATANF
:
8829 case BUILT_IN_ATANL
:
8831 case BUILT_IN_CEILF
:
8832 case BUILT_IN_CEILL
:
8833 case BUILT_IN_FLOOR
:
8834 case BUILT_IN_FLOORF
:
8835 case BUILT_IN_FLOORL
:
8836 case BUILT_IN_NEARBYINT
:
8837 case BUILT_IN_NEARBYINTF
:
8838 case BUILT_IN_NEARBYINTL
:
8839 case BUILT_IN_ROUND
:
8840 case BUILT_IN_ROUNDF
:
8841 case BUILT_IN_ROUNDL
:
8842 case BUILT_IN_TRUNC
:
8843 case BUILT_IN_TRUNCF
:
8844 case BUILT_IN_TRUNCL
:
8845 return tree_expr_nonnegative_p (TREE_VALUE (arglist
));
8850 return tree_expr_nonnegative_p (TREE_VALUE (arglist
));
8857 /* ... fall through ... */
8860 if (truth_value_p (TREE_CODE (t
)))
8861 /* Truth values evaluate to 0 or 1, which is nonnegative. */
8865 /* We don't know sign of `t', so be conservative and return false. */
8869 /* Return true if `r' is known to be non-negative.
8870 Only handles constants at the moment. */
8873 rtl_expr_nonnegative_p (rtx r
)
8875 switch (GET_CODE (r
))
8878 return INTVAL (r
) >= 0;
8881 if (GET_MODE (r
) == VOIDmode
)
8882 return CONST_DOUBLE_HIGH (r
) >= 0;
8890 units
= CONST_VECTOR_NUNITS (r
);
8892 for (i
= 0; i
< units
; ++i
)
8894 elt
= CONST_VECTOR_ELT (r
, i
);
8895 if (!rtl_expr_nonnegative_p (elt
))
8904 /* These are always nonnegative. */
8912 #include "gt-fold-const.h"