]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fold-const.c
* dbxout.c (dbxout_range_type): Add LOW and HIGH parameters. Use them
[thirdparty/gcc.git] / gcc / fold-const.c
CommitLineData
2bc77e10 1/* Fold a constant sub-tree into a single node for C-compiler
22331643 2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
6a4f20ec 3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
eddad94a 4 Free Software Foundation, Inc.
2bc77e10 5
f12b58b3 6This file is part of GCC.
2bc77e10 7
f12b58b3 8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
8c4c00c1 10Software Foundation; either version 3, or (at your option) any later
f12b58b3 11version.
2bc77e10 12
f12b58b3 13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
2bc77e10 17
18You should have received a copy of the GNU General Public License
8c4c00c1 19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
2bc77e10 21
4bbea254 22/*@@ This file should be rewritten to use an arbitrary precision
2bc77e10 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. */
29
30384dcf 30/* The entry points in this file are fold, size_int_wide, size_binop
eddad94a 31 and force_fit_type_double.
2bc77e10 32
33 fold takes a tree as argument and returns a simplified tree.
34
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'.
38
39 size_int takes an integer value, and creates a tree constant
6e44befc 40 with type from `sizetype'.
41
eddad94a 42 force_fit_type_double takes a constant, an overflowable flag and a
43 prior overflow indicator. It forces the value to fit the type and
44 sets TREE_OVERFLOW.
45
35cc02b5 46 Note: Since the folders get called on non-gimple code as well as
47 gimple code, we need to handle GIMPLE tuples as well as their
48 corresponding tree equivalents. */
6e44befc 49
0dbd1c74 50#include "config.h"
5ee8fe30 51#include "system.h"
805e22b2 52#include "coretypes.h"
53#include "tm.h"
2bc77e10 54#include "flags.h"
55#include "tree.h"
ef258422 56#include "real.h"
06f0b99c 57#include "fixed-value.h"
0f9685e4 58#include "rtl.h"
aed0bd19 59#include "expr.h"
7953c610 60#include "tm_p.h"
ffdf1c47 61#include "target.h"
12874aaf 62#include "toplev.h"
add6ee5e 63#include "intl.h"
1bfd55c5 64#include "ggc.h"
15d769aa 65#include "hashtab.h"
20325f61 66#include "langhooks.h"
fc3df357 67#include "md5.h"
75a70cf9 68#include "gimple.h"
2bc77e10 69
80777cd8 70/* Nonzero if we are folding constants inside an initializer; zero
47be647d 71 otherwise. */
72int folding_initializer = 0;
73
318a728f 74/* The following constants represent a bit based encoding of GCC's
75 comparison operators. This encoding simplifies transformations
76 on relational comparison operators, such as AND and OR. */
77enum comparison_code {
78 COMPCODE_FALSE = 0,
79 COMPCODE_LT = 1,
80 COMPCODE_EQ = 2,
81 COMPCODE_LE = 3,
82 COMPCODE_GT = 4,
83 COMPCODE_LTGT = 5,
84 COMPCODE_GE = 6,
85 COMPCODE_ORD = 7,
86 COMPCODE_UNORD = 8,
87 COMPCODE_UNLT = 9,
88 COMPCODE_UNEQ = 10,
89 COMPCODE_UNLE = 11,
90 COMPCODE_UNGT = 12,
91 COMPCODE_NE = 13,
92 COMPCODE_UNGE = 14,
93 COMPCODE_TRUE = 15
94};
95
de1b648b 96static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
97static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
bd214d13 98static bool negate_mathfn_p (enum built_in_function);
de1b648b 99static bool negate_expr_p (tree);
100static tree negate_expr (tree);
101static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
102static tree associate_trees (tree, tree, enum tree_code, tree);
de1b648b 103static tree const_binop (enum tree_code, tree, tree, int);
318a728f 104static enum comparison_code comparison_to_compcode (enum tree_code);
105static enum tree_code compcode_to_comparison (enum comparison_code);
de1b648b 106static int operand_equal_for_comparison_p (tree, tree, tree);
107static int twoval_comparison_p (tree, tree *, tree *, int *);
108static tree eval_subst (tree, tree, tree, tree, tree);
109static tree pedantic_omit_one_operand (tree, tree, tree);
110static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
2a64c730 111static tree make_bit_field_ref (tree, tree, HOST_WIDE_INT, HOST_WIDE_INT, int);
112static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
de1b648b 113static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
114 enum machine_mode *, int *, int *,
115 tree *, tree *);
2a64c730 116static int all_ones_mask_p (const_tree, int);
b4b34335 117static tree sign_bit_p (tree, const_tree);
118static int simple_operand_p (const_tree);
de1b648b 119static tree range_binop (enum tree_code, tree, tree, int, tree, int);
66108e20 120static tree range_predecessor (tree);
121static tree range_successor (tree);
add6ee5e 122static tree make_range (tree, int *, tree *, tree *, bool *);
de1b648b 123static tree build_range_check (tree, tree, int, tree, tree);
124static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
125 tree);
2c17ebb2 126static tree fold_range_test (enum tree_code, tree, tree, tree);
9b1fa4a0 127static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
de1b648b 128static tree unextend (tree, int, int, tree);
129static tree fold_truthop (enum tree_code, tree, tree, tree);
155acab4 130static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
add6ee5e 131static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
132static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
1ebe9a83 133static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
134 tree, tree,
5fe1fe72 135 tree, tree, int);
de1b648b 136static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
137 tree, tree, tree);
138static tree fold_inf_compare (enum tree_code, tree, tree, tree);
270029e0 139static tree fold_div_compare (enum tree_code, tree, tree, tree);
b4b34335 140static bool reorder_operands_p (const_tree, const_tree);
9d77437d 141static tree fold_negate_const (tree, tree);
c183306c 142static tree fold_not_const (tree, tree);
ad46984d 143static tree fold_relational_const (enum tree_code, tree, tree, tree);
87de4c68 144static tree fold_convert_const (enum tree_code, tree, tree);
5f4092ed 145
9d77437d 146
083a2b5e 147/* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
148 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
149 and SUM1. Then this yields nonzero if overflow occurred during the
150 addition.
151
152 Overflow occurs if A and B have the same sign, but A and SUM differ in
153 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
154 sign. */
155#define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
2bc77e10 156\f
b572011e 157/* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
bd5b3bce 158 We do that by representing the two-word integer in 4 words, with only
083a2b5e 159 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
160 number. The value of the word is LOWPART + HIGHPART * BASE. */
bd5b3bce 161
162#define LOWPART(x) \
083a2b5e 163 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
bd5b3bce 164#define HIGHPART(x) \
083a2b5e 165 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
166#define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
2bc77e10 167
bd5b3bce 168/* Unpack a two-word integer into 4 words.
b572011e 169 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
bd5b3bce 170 WORDS points to the array of HOST_WIDE_INTs. */
2bc77e10 171
172static void
de1b648b 173encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
2bc77e10 174{
bd5b3bce 175 words[0] = LOWPART (low);
176 words[1] = HIGHPART (low);
177 words[2] = LOWPART (hi);
178 words[3] = HIGHPART (hi);
2bc77e10 179}
180
bd5b3bce 181/* Pack an array of 4 words into a two-word integer.
182 WORDS points to the array of words.
b572011e 183 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
2bc77e10 184
185static void
dc81944a 186decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
187 HOST_WIDE_INT *hi)
2bc77e10 188{
083a2b5e 189 *low = words[0] + words[1] * BASE;
190 *hi = words[2] + words[3] * BASE;
2bc77e10 191}
192\f
ca9b061d 193/* Force the double-word integer L1, H1 to be within the range of the
194 integer type TYPE. Stores the properly truncated and sign-extended
195 double-word integer in *LV, *HV. Returns true if the operation
196 overflows, that is, argument and result are different. */
197
198int
199fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
720082dc 200 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
2bc77e10 201{
ca9b061d 202 unsigned HOST_WIDE_INT low0 = l1;
203 HOST_WIDE_INT high0 = h1;
a0c2c45b 204 unsigned int prec;
4d28c5d1 205 int sign_extended_type;
2bc77e10 206
ca9b061d 207 if (POINTER_TYPE_P (type)
208 || TREE_CODE (type) == OFFSET_TYPE)
2bc77e10 209 prec = POINTER_SIZE;
817e5691 210 else
ca9b061d 211 prec = TYPE_PRECISION (type);
212
4d28c5d1 213 /* Size types *are* sign extended. */
ca9b061d 214 sign_extended_type = (!TYPE_UNSIGNED (type)
215 || (TREE_CODE (type) == INTEGER_TYPE
216 && TYPE_IS_SIZETYPE (type)));
2bc77e10 217
218 /* First clear all bits that are beyond the type's precision. */
b38d56be 219 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
2bc77e10 220 ;
b572011e 221 else if (prec > HOST_BITS_PER_WIDE_INT)
ca9b061d 222 h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
2bc77e10 223 else
224 {
ca9b061d 225 h1 = 0;
b572011e 226 if (prec < HOST_BITS_PER_WIDE_INT)
ca9b061d 227 l1 &= ~((HOST_WIDE_INT) (-1) << prec);
4d28c5d1 228 }
229
ca9b061d 230 /* Then do sign extension if necessary. */
4d28c5d1 231 if (!sign_extended_type)
232 /* No sign extension */;
b38d56be 233 else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
4d28c5d1 234 /* Correct width already. */;
235 else if (prec > HOST_BITS_PER_WIDE_INT)
236 {
237 /* Sign extend top half? */
ca9b061d 238 if (h1 & ((unsigned HOST_WIDE_INT)1
239 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
240 h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
4d28c5d1 241 }
242 else if (prec == HOST_BITS_PER_WIDE_INT)
243 {
ca9b061d 244 if ((HOST_WIDE_INT)l1 < 0)
245 h1 = -1;
4d28c5d1 246 }
247 else
248 {
249 /* Sign extend bottom half? */
ca9b061d 250 if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
2bc77e10 251 {
ca9b061d 252 h1 = -1;
253 l1 |= (HOST_WIDE_INT)(-1) << prec;
2bc77e10 254 }
255 }
f55401f0 256
ca9b061d 257 *lv = l1;
258 *hv = h1;
259
260 /* If the value didn't fit, signal overflow. */
261 return l1 != low0 || h1 != high0;
262}
263
c8110c8f 264/* We force the double-int HIGH:LOW to the range of the type TYPE by
265 sign or zero extending it.
266 OVERFLOWABLE indicates if we are interested
ca9b061d 267 in overflow of the value, when >0 we are only interested in signed
268 overflow, for <0 we are interested in any overflow. OVERFLOWED
269 indicates whether overflow has already occurred. CONST_OVERFLOWED
270 indicates whether constant overflow has already occurred. We force
271 T's value to be within range of T's type (by setting to 0 or 1 all
272 the bits outside the type's range). We set TREE_OVERFLOWED if,
273 OVERFLOWED is nonzero,
274 or OVERFLOWABLE is >0 and signed overflow occurs
275 or OVERFLOWABLE is <0 and any overflow occurs
c8110c8f 276 We return a new tree node for the extended double-int. The node
277 is shared if no overflow flags are set. */
ca9b061d 278
279tree
c8110c8f 280force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
281 HOST_WIDE_INT high, int overflowable,
eddad94a 282 bool overflowed)
ca9b061d 283{
ca9b061d 284 int sign_extended_type;
285 bool overflow;
286
ca9b061d 287 /* Size types *are* sign extended. */
c8110c8f 288 sign_extended_type = (!TYPE_UNSIGNED (type)
289 || (TREE_CODE (type) == INTEGER_TYPE
290 && TYPE_IS_SIZETYPE (type)));
ca9b061d 291
c8110c8f 292 overflow = fit_double_type (low, high, &low, &high, type);
ca9b061d 293
c8110c8f 294 /* If we need to set overflow flags, return a new unshared node. */
eddad94a 295 if (overflowed || overflow)
4d28c5d1 296 {
297 if (overflowed
298 || overflowable < 0
299 || (overflowable > 0 && sign_extended_type))
300 {
c8110c8f 301 tree t = make_node (INTEGER_CST);
302 TREE_INT_CST_LOW (t) = low;
303 TREE_INT_CST_HIGH (t) = high;
304 TREE_TYPE (t) = type;
4d28c5d1 305 TREE_OVERFLOW (t) = 1;
c8110c8f 306 return t;
00b76131 307 }
4d28c5d1 308 }
0c5713a2 309
c8110c8f 310 /* Else build a shared node. */
311 return build_int_cst_wide (type, low, high);
2bc77e10 312}
313\f
b572011e 314/* Add two doubleword integers with doubleword result.
81035ec1 315 Return nonzero if the operation overflows according to UNSIGNED_P.
b572011e 316 Each argument is given as two `HOST_WIDE_INT' pieces.
2bc77e10 317 One argument is L1 and H1; the other, L2 and H2.
bd5b3bce 318 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
2bc77e10 319
b9e999f0 320int
81035ec1 321add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
322 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
323 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
324 bool unsigned_p)
2bc77e10 325{
a0c2c45b 326 unsigned HOST_WIDE_INT l;
327 HOST_WIDE_INT h;
2bc77e10 328
bd5b3bce 329 l = l1 + l2;
a0c2c45b 330 h = h1 + h2 + (l < l1);
2bc77e10 331
bd5b3bce 332 *lv = l;
333 *hv = h;
81035ec1 334
335 if (unsigned_p)
336 return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
337 else
338 return OVERFLOW_SUM_SIGN (h1, h2, h);
2bc77e10 339}
340
b572011e 341/* Negate a doubleword integer with doubleword result.
b9e999f0 342 Return nonzero if the operation overflows, assuming it's signed.
b572011e 343 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
bd5b3bce 344 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
2bc77e10 345
b9e999f0 346int
dc81944a 347neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
348 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
2bc77e10 349{
350 if (l1 == 0)
351 {
352 *lv = 0;
353 *hv = - h1;
f55401f0 354 return (*hv & h1) < 0;
2bc77e10 355 }
356 else
357 {
cc049fa3 358 *lv = -l1;
359 *hv = ~h1;
b9e999f0 360 return 0;
2bc77e10 361 }
362}
363\f
b572011e 364/* Multiply two doubleword integers with doubleword result.
81035ec1 365 Return nonzero if the operation overflows according to UNSIGNED_P.
b572011e 366 Each argument is given as two `HOST_WIDE_INT' pieces.
2bc77e10 367 One argument is L1 and H1; the other, L2 and H2.
bd5b3bce 368 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
2bc77e10 369
b9e999f0 370int
81035ec1 371mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
372 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
373 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
374 bool unsigned_p)
2bc77e10 375{
bd5b3bce 376 HOST_WIDE_INT arg1[4];
377 HOST_WIDE_INT arg2[4];
378 HOST_WIDE_INT prod[4 * 2];
19cb6b50 379 unsigned HOST_WIDE_INT carry;
380 int i, j, k;
a0c2c45b 381 unsigned HOST_WIDE_INT toplow, neglow;
382 HOST_WIDE_INT tophigh, neghigh;
2bc77e10 383
2bc77e10 384 encode (arg1, l1, h1);
385 encode (arg2, l2, h2);
386
f0af5a88 387 memset (prod, 0, sizeof prod);
2bc77e10 388
bd5b3bce 389 for (i = 0; i < 4; i++)
390 {
391 carry = 0;
392 for (j = 0; j < 4; j++)
393 {
394 k = i + j;
395 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
396 carry += arg1[i] * arg2[j];
397 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
398 carry += prod[k];
399 prod[k] = LOWPART (carry);
400 carry = HIGHPART (carry);
401 }
402 prod[i + 4] = carry;
403 }
2bc77e10 404
81035ec1 405 decode (prod, lv, hv);
cc049fa3 406 decode (prod + 4, &toplow, &tophigh);
81035ec1 407
408 /* Unsigned overflow is immediate. */
409 if (unsigned_p)
410 return (toplow | tophigh) != 0;
411
412 /* Check for signed overflow by calculating the signed representation of the
413 top half of the result; it should agree with the low half's sign bit. */
b9e999f0 414 if (h1 < 0)
415 {
416 neg_double (l2, h2, &neglow, &neghigh);
417 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
418 }
419 if (h2 < 0)
420 {
421 neg_double (l1, h1, &neglow, &neghigh);
422 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
423 }
424 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
2bc77e10 425}
426\f
b572011e 427/* Shift the doubleword integer in L1, H1 left by COUNT places
2bc77e10 428 keeping only PREC bits of result.
429 Shift right if COUNT is negative.
430 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
b572011e 431 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
2bc77e10 432
f55401f0 433void
dc81944a 434lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
435 HOST_WIDE_INT count, unsigned int prec,
436 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
2bc77e10 437{
7c5b13dc 438 unsigned HOST_WIDE_INT signmask;
439
2bc77e10 440 if (count < 0)
441 {
cc049fa3 442 rshift_double (l1, h1, -count, prec, lv, hv, arith);
f55401f0 443 return;
2bc77e10 444 }
cc049fa3 445
0bb60c65 446 if (SHIFT_COUNT_TRUNCATED)
447 count %= prec;
2bc77e10 448
016d117a 449 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
450 {
451 /* Shifting by the host word size is undefined according to the
452 ANSI standard, so we must handle this as a special case. */
453 *hv = 0;
454 *lv = 0;
455 }
456 else if (count >= HOST_BITS_PER_WIDE_INT)
2bc77e10 457 {
a0c2c45b 458 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
bd5b3bce 459 *lv = 0;
460 }
461 else
462 {
463 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
a0c2c45b 464 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
465 *lv = l1 << count;
2bc77e10 466 }
7c5b13dc 467
468 /* Sign extend all bits that are beyond the precision. */
469
470 signmask = -((prec > HOST_BITS_PER_WIDE_INT
f9a532b0 471 ? ((unsigned HOST_WIDE_INT) *hv
d3371fcd 472 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
7c5b13dc 473 : (*lv >> (prec - 1))) & 1);
474
475 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
476 ;
477 else if (prec >= HOST_BITS_PER_WIDE_INT)
478 {
479 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
480 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
481 }
482 else
483 {
484 *hv = signmask;
485 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
486 *lv |= signmask << prec;
487 }
2bc77e10 488}
489
b572011e 490/* Shift the doubleword integer in L1, H1 right by COUNT places
2bc77e10 491 keeping only PREC bits of result. COUNT must be positive.
492 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
b572011e 493 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
2bc77e10 494
495void
dc81944a 496rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
497 HOST_WIDE_INT count, unsigned int prec,
498 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
de1b648b 499 int arith)
2bc77e10 500{
bd5b3bce 501 unsigned HOST_WIDE_INT signmask;
a0c2c45b 502
bd5b3bce 503 signmask = (arith
504 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
505 : 0);
2bc77e10 506
0bb60c65 507 if (SHIFT_COUNT_TRUNCATED)
508 count %= prec;
2bc77e10 509
016d117a 510 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
511 {
512 /* Shifting by the host word size is undefined according to the
513 ANSI standard, so we must handle this as a special case. */
7c5b13dc 514 *hv = 0;
515 *lv = 0;
016d117a 516 }
517 else if (count >= HOST_BITS_PER_WIDE_INT)
2bc77e10 518 {
7c5b13dc 519 *hv = 0;
520 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
bd5b3bce 521 }
522 else
523 {
7c5b13dc 524 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
a0c2c45b 525 *lv = ((l1 >> count)
5ee8fe30 526 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
7c5b13dc 527 }
528
529 /* Zero / sign extend all bits that are beyond the precision. */
530
531 if (count >= (HOST_WIDE_INT)prec)
532 {
533 *hv = signmask;
534 *lv = signmask;
535 }
536 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
537 ;
538 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
539 {
540 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
541 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
542 }
543 else
544 {
545 *hv = signmask;
546 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
547 *lv |= signmask << (prec - count);
2bc77e10 548 }
2bc77e10 549}
550\f
bd5b3bce 551/* Rotate the doubleword integer in L1, H1 left by COUNT places
2bc77e10 552 keeping only PREC bits of result.
553 Rotate right if COUNT is negative.
b572011e 554 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
2bc77e10 555
556void
dc81944a 557lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
558 HOST_WIDE_INT count, unsigned int prec,
559 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
2bc77e10 560{
a0c2c45b 561 unsigned HOST_WIDE_INT s1l, s2l;
562 HOST_WIDE_INT s1h, s2h;
2bc77e10 563
7a1b56a9 564 count %= prec;
2bc77e10 565 if (count < 0)
7a1b56a9 566 count += prec;
2bc77e10 567
7a1b56a9 568 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
569 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
570 *lv = s1l | s2l;
571 *hv = s1h | s2h;
2bc77e10 572}
573
b572011e 574/* Rotate the doubleword integer in L1, H1 left by COUNT places
2bc77e10 575 keeping only PREC bits of result. COUNT must be positive.
b572011e 576 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
2bc77e10 577
578void
dc81944a 579rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
580 HOST_WIDE_INT count, unsigned int prec,
581 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
2bc77e10 582{
a0c2c45b 583 unsigned HOST_WIDE_INT s1l, s2l;
584 HOST_WIDE_INT s1h, s2h;
2bc77e10 585
7a1b56a9 586 count %= prec;
587 if (count < 0)
588 count += prec;
2bc77e10 589
7a1b56a9 590 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
591 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
592 *lv = s1l | s2l;
593 *hv = s1h | s2h;
2bc77e10 594}
595\f
b572011e 596/* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
2bc77e10 597 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
598 CODE is a tree code for a kind of division, one of
599 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
600 or EXACT_DIV_EXPR
20dd417a 601 It controls how the quotient is rounded to an integer.
b9e999f0 602 Return nonzero if the operation overflows.
2bc77e10 603 UNS nonzero says do unsigned division. */
604
15ca565e 605int
de1b648b 606div_and_round_double (enum tree_code code, int uns,
607 unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
608 HOST_WIDE_INT hnum_orig,
609 unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
dc81944a 610 HOST_WIDE_INT hden_orig,
611 unsigned HOST_WIDE_INT *lquo,
de1b648b 612 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
613 HOST_WIDE_INT *hrem)
2bc77e10 614{
615 int quo_neg = 0;
bd5b3bce 616 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
617 HOST_WIDE_INT den[4], quo[4];
19cb6b50 618 int i, j;
bd5b3bce 619 unsigned HOST_WIDE_INT work;
a0c2c45b 620 unsigned HOST_WIDE_INT carry = 0;
621 unsigned HOST_WIDE_INT lnum = lnum_orig;
abd9ac9c 622 HOST_WIDE_INT hnum = hnum_orig;
a0c2c45b 623 unsigned HOST_WIDE_INT lden = lden_orig;
abd9ac9c 624 HOST_WIDE_INT hden = hden_orig;
b9e999f0 625 int overflow = 0;
2bc77e10 626
a0c2c45b 627 if (hden == 0 && lden == 0)
ad87de1e 628 overflow = 1, lden = 1;
2bc77e10 629
139c3f48 630 /* Calculate quotient sign and convert operands to unsigned. */
cc049fa3 631 if (!uns)
2bc77e10 632 {
b9e999f0 633 if (hnum < 0)
2bc77e10 634 {
635 quo_neg = ~ quo_neg;
b9e999f0 636 /* (minimum integer) / (-1) is the only overflow case. */
a0c2c45b 637 if (neg_double (lnum, hnum, &lnum, &hnum)
638 && ((HOST_WIDE_INT) lden & hden) == -1)
b9e999f0 639 overflow = 1;
2bc77e10 640 }
cc049fa3 641 if (hden < 0)
2bc77e10 642 {
643 quo_neg = ~ quo_neg;
b9e999f0 644 neg_double (lden, hden, &lden, &hden);
2bc77e10 645 }
646 }
647
648 if (hnum == 0 && hden == 0)
649 { /* single precision */
650 *hquo = *hrem = 0;
802ddb63 651 /* This unsigned division rounds toward zero. */
a0c2c45b 652 *lquo = lnum / lden;
2bc77e10 653 goto finish_up;
654 }
655
656 if (hnum == 0)
657 { /* trivial case: dividend < divisor */
658 /* hden != 0 already checked. */
659 *hquo = *lquo = 0;
660 *hrem = hnum;
661 *lrem = lnum;
662 goto finish_up;
663 }
664
f0af5a88 665 memset (quo, 0, sizeof quo);
2bc77e10 666
f0af5a88 667 memset (num, 0, sizeof num); /* to zero 9th element */
668 memset (den, 0, sizeof den);
2bc77e10 669
cc049fa3 670 encode (num, lnum, hnum);
2bc77e10 671 encode (den, lden, hden);
672
bd5b3bce 673 /* Special code for when the divisor < BASE. */
a0c2c45b 674 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
bd5b3bce 675 {
2bc77e10 676 /* hnum != 0 already checked. */
bd5b3bce 677 for (i = 4 - 1; i >= 0; i--)
2bc77e10 678 {
bd5b3bce 679 work = num[i] + carry * BASE;
a0c2c45b 680 quo[i] = work / lden;
681 carry = work % lden;
2bc77e10 682 }
683 }
bd5b3bce 684 else
685 {
686 /* Full double precision division,
687 with thanks to Don Knuth's "Seminumerical Algorithms". */
a0c2c45b 688 int num_hi_sig, den_hi_sig;
689 unsigned HOST_WIDE_INT quo_est, scale;
2bc77e10 690
6ef828f9 691 /* Find the highest nonzero divisor digit. */
cc049fa3 692 for (i = 4 - 1;; i--)
693 if (den[i] != 0)
694 {
695 den_hi_sig = i;
696 break;
697 }
bd5b3bce 698
a0c2c45b 699 /* Insure that the first digit of the divisor is at least BASE/2.
700 This is required by the quotient digit estimation algorithm. */
2bc77e10 701
a0c2c45b 702 scale = BASE / (den[den_hi_sig] + 1);
703 if (scale > 1)
704 { /* scale divisor and dividend */
705 carry = 0;
706 for (i = 0; i <= 4 - 1; i++)
707 {
708 work = (num[i] * scale) + carry;
709 num[i] = LOWPART (work);
710 carry = HIGHPART (work);
711 }
2bc77e10 712
a0c2c45b 713 num[4] = carry;
714 carry = 0;
715 for (i = 0; i <= 4 - 1; i++)
716 {
717 work = (den[i] * scale) + carry;
718 den[i] = LOWPART (work);
719 carry = HIGHPART (work);
720 if (den[i] != 0) den_hi_sig = i;
721 }
722 }
2bc77e10 723
a0c2c45b 724 num_hi_sig = 4;
2bc77e10 725
a0c2c45b 726 /* Main loop */
727 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
2bc77e10 728 {
a0c2c45b 729 /* Guess the next quotient digit, quo_est, by dividing the first
730 two remaining dividend digits by the high order quotient digit.
731 quo_est is never low and is at most 2 high. */
732 unsigned HOST_WIDE_INT tmp;
733
734 num_hi_sig = i + den_hi_sig + 1;
735 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
736 if (num[num_hi_sig] != den[den_hi_sig])
737 quo_est = work / den[den_hi_sig];
738 else
739 quo_est = BASE - 1;
2bc77e10 740
1e625a2e 741 /* Refine quo_est so it's usually correct, and at most one high. */
a0c2c45b 742 tmp = work - quo_est * den[den_hi_sig];
743 if (tmp < BASE
744 && (den[den_hi_sig - 1] * quo_est
745 > (tmp * BASE + num[num_hi_sig - 2])))
746 quo_est--;
2bc77e10 747
a0c2c45b 748 /* Try QUO_EST as the quotient digit, by multiplying the
749 divisor by QUO_EST and subtracting from the remaining dividend.
750 Keep in mind that QUO_EST is the I - 1st digit. */
751
752 carry = 0;
2bc77e10 753 for (j = 0; j <= den_hi_sig; j++)
754 {
a0c2c45b 755 work = quo_est * den[j] + carry;
bd5b3bce 756 carry = HIGHPART (work);
a0c2c45b 757 work = num[i + j] - LOWPART (work);
bd5b3bce 758 num[i + j] = LOWPART (work);
a0c2c45b 759 carry += HIGHPART (work) != 0;
2bc77e10 760 }
2bc77e10 761
a0c2c45b 762 /* If quo_est was high by one, then num[i] went negative and
763 we need to correct things. */
f9a532b0 764 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
a0c2c45b 765 {
766 quo_est--;
767 carry = 0; /* add divisor back in */
768 for (j = 0; j <= den_hi_sig; j++)
769 {
770 work = num[i + j] + den[j] + carry;
771 carry = HIGHPART (work);
772 num[i + j] = LOWPART (work);
773 }
774
775 num [num_hi_sig] += carry;
776 }
777
778 /* Store the quotient digit. */
779 quo[i] = quo_est;
780 }
2bc77e10 781 }
2bc77e10 782
783 decode (quo, lquo, hquo);
784
785 finish_up:
b4b174c3 786 /* If result is negative, make it so. */
2bc77e10 787 if (quo_neg)
788 neg_double (*lquo, *hquo, lquo, hquo);
789
aab2cf92 790 /* Compute trial remainder: rem = num - (quo * den) */
2bc77e10 791 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
792 neg_double (*lrem, *hrem, lrem, hrem);
793 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
794
795 switch (code)
796 {
797 case TRUNC_DIV_EXPR:
798 case TRUNC_MOD_EXPR: /* round toward zero */
799 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
b9e999f0 800 return overflow;
2bc77e10 801
802 case FLOOR_DIV_EXPR:
803 case FLOOR_MOD_EXPR: /* round toward negative infinity */
804 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
805 {
806 /* quo = quo - 1; */
b572011e 807 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
808 lquo, hquo);
2bc77e10 809 }
a0c2c45b 810 else
811 return overflow;
2bc77e10 812 break;
813
814 case CEIL_DIV_EXPR:
815 case CEIL_MOD_EXPR: /* round toward positive infinity */
816 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
817 {
b572011e 818 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
819 lquo, hquo);
2bc77e10 820 }
a0c2c45b 821 else
822 return overflow;
2bc77e10 823 break;
cc049fa3 824
2bc77e10 825 case ROUND_DIV_EXPR:
826 case ROUND_MOD_EXPR: /* round to closest integer */
827 {
a0c2c45b 828 unsigned HOST_WIDE_INT labs_rem = *lrem;
829 HOST_WIDE_INT habs_rem = *hrem;
830 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
831 HOST_WIDE_INT habs_den = hden, htwice;
832
2358393e 833 /* Get absolute values. */
a0c2c45b 834 if (*hrem < 0)
835 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
836 if (hden < 0)
837 neg_double (lden, hden, &labs_den, &habs_den);
838
f30408df 839 /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient. */
b572011e 840 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
841 labs_rem, habs_rem, &ltwice, &htwice);
a0c2c45b 842
b572011e 843 if (((unsigned HOST_WIDE_INT) habs_den
844 < (unsigned HOST_WIDE_INT) htwice)
845 || (((unsigned HOST_WIDE_INT) habs_den
846 == (unsigned HOST_WIDE_INT) htwice)
f30408df 847 && (labs_den <= ltwice)))
2bc77e10 848 {
849 if (*hquo < 0)
850 /* quo = quo - 1; */
b572011e 851 add_double (*lquo, *hquo,
852 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
2bc77e10 853 else
854 /* quo = quo + 1; */
b572011e 855 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
856 lquo, hquo);
2bc77e10 857 }
a0c2c45b 858 else
859 return overflow;
2bc77e10 860 }
861 break;
862
863 default:
fdada98f 864 gcc_unreachable ();
2bc77e10 865 }
866
21dda4ee 867 /* Compute true remainder: rem = num - (quo * den) */
2bc77e10 868 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
869 neg_double (*lrem, *hrem, lrem, hrem);
870 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
b9e999f0 871 return overflow;
2bc77e10 872}
86f023fe 873
874/* If ARG2 divides ARG1 with zero remainder, carries out the division
875 of type CODE and returns the quotient.
876 Otherwise returns NULL_TREE. */
877
401d1fb3 878tree
b4b34335 879div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
86f023fe 880{
881 unsigned HOST_WIDE_INT int1l, int2l;
882 HOST_WIDE_INT int1h, int2h;
883 unsigned HOST_WIDE_INT quol, reml;
884 HOST_WIDE_INT quoh, remh;
885 tree type = TREE_TYPE (arg1);
886 int uns = TYPE_UNSIGNED (type);
887
888 int1l = TREE_INT_CST_LOW (arg1);
889 int1h = TREE_INT_CST_HIGH (arg1);
310d2511 890 /* &obj[0] + -128 really should be compiled as &obj[-8] rather than
b2f88529 891 &obj[some_exotic_number]. */
892 if (POINTER_TYPE_P (type))
893 {
894 uns = false;
895 type = signed_type_for (type);
896 fit_double_type (int1l, int1h, &int1l, &int1h,
897 type);
898 }
899 else
900 fit_double_type (int1l, int1h, &int1l, &int1h, type);
86f023fe 901 int2l = TREE_INT_CST_LOW (arg2);
902 int2h = TREE_INT_CST_HIGH (arg2);
903
904 div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
905 &quol, &quoh, &reml, &remh);
906 if (remh != 0 || reml != 0)
907 return NULL_TREE;
908
909 return build_int_cst_wide (type, quol, quoh);
910}
2bc77e10 911\f
80777cd8 912/* This is nonzero if we should defer warnings about undefined
add6ee5e 913 overflow. This facility exists because these warnings are a
914 special case. The code to estimate loop iterations does not want
915 to issue any warnings, since it works with expressions which do not
916 occur in user code. Various bits of cleanup code call fold(), but
917 only use the result if it has certain characteristics (e.g., is a
918 constant); that code only wants to issue a warning if the result is
919 used. */
920
921static int fold_deferring_overflow_warnings;
922
923/* If a warning about undefined overflow is deferred, this is the
924 warning. Note that this may cause us to turn two warnings into
925 one, but that is fine since it is sufficient to only give one
926 warning per expression. */
927
928static const char* fold_deferred_overflow_warning;
929
930/* If a warning about undefined overflow is deferred, this is the
931 level at which the warning should be emitted. */
932
933static enum warn_strict_overflow_code fold_deferred_overflow_code;
934
935/* Start deferring overflow warnings. We could use a stack here to
936 permit nested calls, but at present it is not necessary. */
937
938void
939fold_defer_overflow_warnings (void)
940{
941 ++fold_deferring_overflow_warnings;
942}
943
944/* Stop deferring overflow warnings. If there is a pending warning,
945 and ISSUE is true, then issue the warning if appropriate. STMT is
946 the statement with which the warning should be associated (used for
947 location information); STMT may be NULL. CODE is the level of the
948 warning--a warn_strict_overflow_code value. This function will use
949 the smaller of CODE and the deferred code when deciding whether to
950 issue the warning. CODE may be zero to mean to always use the
951 deferred code. */
952
953void
75a70cf9 954fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
add6ee5e 955{
956 const char *warnmsg;
957 location_t locus;
958
959 gcc_assert (fold_deferring_overflow_warnings > 0);
960 --fold_deferring_overflow_warnings;
961 if (fold_deferring_overflow_warnings > 0)
962 {
963 if (fold_deferred_overflow_warning != NULL
964 && code != 0
965 && code < (int) fold_deferred_overflow_code)
8458f4ca 966 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
add6ee5e 967 return;
968 }
969
970 warnmsg = fold_deferred_overflow_warning;
971 fold_deferred_overflow_warning = NULL;
972
973 if (!issue || warnmsg == NULL)
974 return;
975
75a70cf9 976 if (gimple_no_warning_p (stmt))
72c59a18 977 return;
978
add6ee5e 979 /* Use the smallest code level when deciding to issue the
980 warning. */
981 if (code == 0 || code > (int) fold_deferred_overflow_code)
982 code = fold_deferred_overflow_code;
983
984 if (!issue_strict_overflow_warning (code))
985 return;
986
75a70cf9 987 if (stmt == NULL)
add6ee5e 988 locus = input_location;
989 else
75a70cf9 990 locus = gimple_location (stmt);
add6ee5e 991 warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
992}
993
994/* Stop deferring overflow warnings, ignoring any deferred
995 warnings. */
996
997void
998fold_undefer_and_ignore_overflow_warnings (void)
999{
75a70cf9 1000 fold_undefer_overflow_warnings (false, NULL, 0);
add6ee5e 1001}
1002
1003/* Whether we are deferring overflow warnings. */
1004
1005bool
1006fold_deferring_overflow_warnings_p (void)
1007{
1008 return fold_deferring_overflow_warnings > 0;
1009}
1010
1011/* This is called when we fold something based on the fact that signed
1012 overflow is undefined. */
1013
1014static void
1015fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1016{
add6ee5e 1017 if (fold_deferring_overflow_warnings > 0)
1018 {
1019 if (fold_deferred_overflow_warning == NULL
1020 || wc < fold_deferred_overflow_code)
1021 {
1022 fold_deferred_overflow_warning = gmsgid;
1023 fold_deferred_overflow_code = wc;
1024 }
1025 }
1026 else if (issue_strict_overflow_warning (wc))
1027 warning (OPT_Wstrict_overflow, gmsgid);
1028}
1029\f
352e5c7a 1030/* Return true if the built-in mathematical function specified by CODE
1031 is odd, i.e. -f(x) == f(-x). */
bd214d13 1032
1033static bool
1034negate_mathfn_p (enum built_in_function code)
1035{
1036 switch (code)
1037 {
4f35b1fc 1038 CASE_FLT_FN (BUILT_IN_ASIN):
1039 CASE_FLT_FN (BUILT_IN_ASINH):
1040 CASE_FLT_FN (BUILT_IN_ATAN):
1041 CASE_FLT_FN (BUILT_IN_ATANH):
503733d5 1042 CASE_FLT_FN (BUILT_IN_CASIN):
1043 CASE_FLT_FN (BUILT_IN_CASINH):
1044 CASE_FLT_FN (BUILT_IN_CATAN):
1045 CASE_FLT_FN (BUILT_IN_CATANH):
4f35b1fc 1046 CASE_FLT_FN (BUILT_IN_CBRT):
503733d5 1047 CASE_FLT_FN (BUILT_IN_CPROJ):
1048 CASE_FLT_FN (BUILT_IN_CSIN):
1049 CASE_FLT_FN (BUILT_IN_CSINH):
1050 CASE_FLT_FN (BUILT_IN_CTAN):
1051 CASE_FLT_FN (BUILT_IN_CTANH):
cacdc1af 1052 CASE_FLT_FN (BUILT_IN_ERF):
1053 CASE_FLT_FN (BUILT_IN_LLROUND):
1054 CASE_FLT_FN (BUILT_IN_LROUND):
1055 CASE_FLT_FN (BUILT_IN_ROUND):
4f35b1fc 1056 CASE_FLT_FN (BUILT_IN_SIN):
1057 CASE_FLT_FN (BUILT_IN_SINH):
1058 CASE_FLT_FN (BUILT_IN_TAN):
1059 CASE_FLT_FN (BUILT_IN_TANH):
cacdc1af 1060 CASE_FLT_FN (BUILT_IN_TRUNC):
bd214d13 1061 return true;
1062
cacdc1af 1063 CASE_FLT_FN (BUILT_IN_LLRINT):
1064 CASE_FLT_FN (BUILT_IN_LRINT):
1065 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1066 CASE_FLT_FN (BUILT_IN_RINT):
1067 return !flag_rounding_math;
1068
bd214d13 1069 default:
1070 break;
1071 }
1072 return false;
1073}
1074
bb445479 1075/* Check whether we may negate an integer constant T without causing
1076 overflow. */
1077
1078bool
b7bf20db 1079may_negate_without_overflow_p (const_tree t)
bb445479 1080{
1081 unsigned HOST_WIDE_INT val;
1082 unsigned int prec;
1083 tree type;
1084
fdada98f 1085 gcc_assert (TREE_CODE (t) == INTEGER_CST);
bb445479 1086
1087 type = TREE_TYPE (t);
1088 if (TYPE_UNSIGNED (type))
1089 return false;
1090
1091 prec = TYPE_PRECISION (type);
1092 if (prec > HOST_BITS_PER_WIDE_INT)
1093 {
1094 if (TREE_INT_CST_LOW (t) != 0)
1095 return true;
1096 prec -= HOST_BITS_PER_WIDE_INT;
1097 val = TREE_INT_CST_HIGH (t);
1098 }
1099 else
1100 val = TREE_INT_CST_LOW (t);
1101 if (prec < HOST_BITS_PER_WIDE_INT)
1102 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1103 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1104}
1105
22331643 1106/* Determine whether an expression T can be cheaply negated using
58b22aa6 1107 the function negate_expr without introducing undefined overflow. */
22331643 1108
1109static bool
de1b648b 1110negate_expr_p (tree t)
22331643 1111{
22331643 1112 tree type;
1113
1114 if (t == 0)
1115 return false;
1116
1117 type = TREE_TYPE (t);
1118
1119 STRIP_SIGN_NOPS (t);
1120 switch (TREE_CODE (t))
1121 {
1122 case INTEGER_CST:
981eb798 1123 if (TYPE_OVERFLOW_WRAPS (type))
bd214d13 1124 return true;
22331643 1125
1126 /* Check that -CST will not overflow type. */
bb445479 1127 return may_negate_without_overflow_p (t);
5ea8b65a 1128 case BIT_NOT_EXPR:
981eb798 1129 return (INTEGRAL_TYPE_P (type)
1130 && TYPE_OVERFLOW_WRAPS (type));
22331643 1131
06f0b99c 1132 case FIXED_CST:
22331643 1133 case REAL_CST:
1134 case NEGATE_EXPR:
22331643 1135 return true;
1136
bd214d13 1137 case COMPLEX_CST:
1138 return negate_expr_p (TREE_REALPART (t))
1139 && negate_expr_p (TREE_IMAGPART (t));
1140
7c4eaf72 1141 case COMPLEX_EXPR:
1142 return negate_expr_p (TREE_OPERAND (t, 0))
1143 && negate_expr_p (TREE_OPERAND (t, 1));
1144
6da8bb4f 1145 case CONJ_EXPR:
1146 return negate_expr_p (TREE_OPERAND (t, 0));
1147
2169cab6 1148 case PLUS_EXPR:
2815dfde 1149 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1150 || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
2169cab6 1151 return false;
1152 /* -(A + B) -> (-B) - A. */
1153 if (negate_expr_p (TREE_OPERAND (t, 1))
1154 && reorder_operands_p (TREE_OPERAND (t, 0),
1155 TREE_OPERAND (t, 1)))
1156 return true;
1157 /* -(A + B) -> (-A) - B. */
1158 return negate_expr_p (TREE_OPERAND (t, 0));
1159
d842742d 1160 case MINUS_EXPR:
1161 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
2815dfde 1162 return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1163 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
bd214d13 1164 && reorder_operands_p (TREE_OPERAND (t, 0),
1165 TREE_OPERAND (t, 1));
d842742d 1166
a12ecaaa 1167 case MULT_EXPR:
78a8ed03 1168 if (TYPE_UNSIGNED (TREE_TYPE (t)))
a12ecaaa 1169 break;
1170
1171 /* Fall through. */
1172
1173 case RDIV_EXPR:
1174 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1175 return negate_expr_p (TREE_OPERAND (t, 1))
1176 || negate_expr_p (TREE_OPERAND (t, 0));
1177 break;
1178
212f6d6d 1179 case TRUNC_DIV_EXPR:
1180 case ROUND_DIV_EXPR:
1181 case FLOOR_DIV_EXPR:
1182 case CEIL_DIV_EXPR:
1183 case EXACT_DIV_EXPR:
add6ee5e 1184 /* In general we can't negate A / B, because if A is INT_MIN and
1185 B is 1, we may turn this into INT_MIN / -1 which is undefined
1186 and actually traps on some architectures. But if overflow is
1187 undefined, we can negate, because - (INT_MIN / 1) is an
1188 overflow. */
981eb798 1189 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1190 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
212f6d6d 1191 break;
1192 return negate_expr_p (TREE_OPERAND (t, 1))
1193 || negate_expr_p (TREE_OPERAND (t, 0));
1194
bd214d13 1195 case NOP_EXPR:
1196 /* Negate -((double)float) as (double)(-float). */
1197 if (TREE_CODE (type) == REAL_TYPE)
1198 {
1199 tree tem = strip_float_extensions (t);
1200 if (tem != t)
1201 return negate_expr_p (tem);
1202 }
1203 break;
1204
1205 case CALL_EXPR:
1206 /* Negate -f(x) as f(-x). */
1207 if (negate_mathfn_p (builtin_mathfn_code (t)))
c2f47e15 1208 return negate_expr_p (CALL_EXPR_ARG (t, 0));
bd214d13 1209 break;
1210
a22fd555 1211 case RSHIFT_EXPR:
1212 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1213 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1214 {
1215 tree op1 = TREE_OPERAND (t, 1);
1216 if (TREE_INT_CST_HIGH (op1) == 0
1217 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1218 == TREE_INT_CST_LOW (op1))
1219 return true;
1220 }
1221 break;
1222
22331643 1223 default:
1224 break;
1225 }
1226 return false;
1227}
1228
58b22aa6 1229/* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1230 simplification is possible.
1231 If negate_expr_p would return true for T, NULL_TREE will never be
1232 returned. */
2bc77e10 1233
23ec2d5e 1234static tree
58b22aa6 1235fold_negate_expr (tree t)
23ec2d5e 1236{
58b22aa6 1237 tree type = TREE_TYPE (t);
23ec2d5e 1238 tree tem;
1239
23ec2d5e 1240 switch (TREE_CODE (t))
1241 {
5ea8b65a 1242 /* Convert - (~A) to A + 1. */
1243 case BIT_NOT_EXPR:
58b22aa6 1244 if (INTEGRAL_TYPE_P (type))
5ea8b65a 1245 return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
1246 build_int_cst (type, 1));
3a07b940 1247 break;
5ea8b65a 1248
23ec2d5e 1249 case INTEGER_CST:
9d77437d 1250 tem = fold_negate_const (t, type);
20e133d9 1251 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
981eb798 1252 || !TYPE_OVERFLOW_TRAPS (type))
23ec2d5e 1253 return tem;
1254 break;
1255
a12ecaaa 1256 case REAL_CST:
9d77437d 1257 tem = fold_negate_const (t, type);
a12ecaaa 1258 /* Two's complement FP formats, such as c4x, may overflow. */
f96bd2bf 1259 if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
58b22aa6 1260 return tem;
a12ecaaa 1261 break;
1262
06f0b99c 1263 case FIXED_CST:
1264 tem = fold_negate_const (t, type);
1265 return tem;
1266
bd214d13 1267 case COMPLEX_CST:
1268 {
1269 tree rpart = negate_expr (TREE_REALPART (t));
1270 tree ipart = negate_expr (TREE_IMAGPART (t));
1271
1272 if ((TREE_CODE (rpart) == REAL_CST
1273 && TREE_CODE (ipart) == REAL_CST)
1274 || (TREE_CODE (rpart) == INTEGER_CST
1275 && TREE_CODE (ipart) == INTEGER_CST))
1276 return build_complex (type, rpart, ipart);
1277 }
1278 break;
1279
7c4eaf72 1280 case COMPLEX_EXPR:
1281 if (negate_expr_p (t))
1282 return fold_build2 (COMPLEX_EXPR, type,
1283 fold_negate_expr (TREE_OPERAND (t, 0)),
1284 fold_negate_expr (TREE_OPERAND (t, 1)));
1285 break;
1286
6da8bb4f 1287 case CONJ_EXPR:
1288 if (negate_expr_p (t))
1289 return fold_build1 (CONJ_EXPR, type,
1290 fold_negate_expr (TREE_OPERAND (t, 0)));
1291 break;
1292
23ec2d5e 1293 case NEGATE_EXPR:
58b22aa6 1294 return TREE_OPERAND (t, 0);
23ec2d5e 1295
2169cab6 1296 case PLUS_EXPR:
2815dfde 1297 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1298 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
2169cab6 1299 {
1300 /* -(A + B) -> (-B) - A. */
1301 if (negate_expr_p (TREE_OPERAND (t, 1))
1302 && reorder_operands_p (TREE_OPERAND (t, 0),
1303 TREE_OPERAND (t, 1)))
fd96eeef 1304 {
1305 tem = negate_expr (TREE_OPERAND (t, 1));
58b22aa6 1306 return fold_build2 (MINUS_EXPR, type,
1307 tem, TREE_OPERAND (t, 0));
fd96eeef 1308 }
1309
2169cab6 1310 /* -(A + B) -> (-A) - B. */
1311 if (negate_expr_p (TREE_OPERAND (t, 0)))
fd96eeef 1312 {
1313 tem = negate_expr (TREE_OPERAND (t, 0));
58b22aa6 1314 return fold_build2 (MINUS_EXPR, type,
1315 tem, TREE_OPERAND (t, 1));
fd96eeef 1316 }
2169cab6 1317 }
1318 break;
1319
23ec2d5e 1320 case MINUS_EXPR:
1321 /* - (A - B) -> B - A */
2815dfde 1322 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1323 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
bd214d13 1324 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
58b22aa6 1325 return fold_build2 (MINUS_EXPR, type,
1326 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
23ec2d5e 1327 break;
1328
a12ecaaa 1329 case MULT_EXPR:
58b22aa6 1330 if (TYPE_UNSIGNED (type))
a12ecaaa 1331 break;
1332
1333 /* Fall through. */
1334
1335 case RDIV_EXPR:
58b22aa6 1336 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
a12ecaaa 1337 {
1338 tem = TREE_OPERAND (t, 1);
1339 if (negate_expr_p (tem))
58b22aa6 1340 return fold_build2 (TREE_CODE (t), type,
1341 TREE_OPERAND (t, 0), negate_expr (tem));
a12ecaaa 1342 tem = TREE_OPERAND (t, 0);
1343 if (negate_expr_p (tem))
58b22aa6 1344 return fold_build2 (TREE_CODE (t), type,
1345 negate_expr (tem), TREE_OPERAND (t, 1));
a12ecaaa 1346 }
1347 break;
1348
212f6d6d 1349 case TRUNC_DIV_EXPR:
1350 case ROUND_DIV_EXPR:
1351 case FLOOR_DIV_EXPR:
1352 case CEIL_DIV_EXPR:
1353 case EXACT_DIV_EXPR:
add6ee5e 1354 /* In general we can't negate A / B, because if A is INT_MIN and
1355 B is 1, we may turn this into INT_MIN / -1 which is undefined
1356 and actually traps on some architectures. But if overflow is
1357 undefined, we can negate, because - (INT_MIN / 1) is an
1358 overflow. */
981eb798 1359 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
212f6d6d 1360 {
add6ee5e 1361 const char * const warnmsg = G_("assuming signed overflow does not "
1362 "occur when negating a division");
212f6d6d 1363 tem = TREE_OPERAND (t, 1);
1364 if (negate_expr_p (tem))
add6ee5e 1365 {
1366 if (INTEGRAL_TYPE_P (type)
1367 && (TREE_CODE (tem) != INTEGER_CST
1368 || integer_onep (tem)))
1369 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1370 return fold_build2 (TREE_CODE (t), type,
1371 TREE_OPERAND (t, 0), negate_expr (tem));
1372 }
212f6d6d 1373 tem = TREE_OPERAND (t, 0);
1374 if (negate_expr_p (tem))
add6ee5e 1375 {
1376 if (INTEGRAL_TYPE_P (type)
1377 && (TREE_CODE (tem) != INTEGER_CST
1378 || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1379 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1380 return fold_build2 (TREE_CODE (t), type,
1381 negate_expr (tem), TREE_OPERAND (t, 1));
1382 }
212f6d6d 1383 }
1384 break;
1385
bd214d13 1386 case NOP_EXPR:
1387 /* Convert -((double)float) into (double)(-float). */
1388 if (TREE_CODE (type) == REAL_TYPE)
1389 {
1390 tem = strip_float_extensions (t);
1391 if (tem != t && negate_expr_p (tem))
843a4f91 1392 return fold_convert (type, negate_expr (tem));
bd214d13 1393 }
1394 break;
1395
1396 case CALL_EXPR:
1397 /* Negate -f(x) as f(-x). */
1398 if (negate_mathfn_p (builtin_mathfn_code (t))
c2f47e15 1399 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
bd214d13 1400 {
c2f47e15 1401 tree fndecl, arg;
bd214d13 1402
1403 fndecl = get_callee_fndecl (t);
c2f47e15 1404 arg = negate_expr (CALL_EXPR_ARG (t, 0));
1405 return build_call_expr (fndecl, 1, arg);
bd214d13 1406 }
1407 break;
1408
a22fd555 1409 case RSHIFT_EXPR:
1410 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1411 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1412 {
1413 tree op1 = TREE_OPERAND (t, 1);
1414 if (TREE_INT_CST_HIGH (op1) == 0
1415 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1416 == TREE_INT_CST_LOW (op1))
1417 {
78a8ed03 1418 tree ntype = TYPE_UNSIGNED (type)
11773141 1419 ? signed_type_for (type)
71eea85c 1420 : unsigned_type_for (type);
a22fd555 1421 tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
7ab7fd4f 1422 temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
a22fd555 1423 return fold_convert (type, temp);
1424 }
1425 }
1426 break;
1427
23ec2d5e 1428 default:
1429 break;
1430 }
1431
58b22aa6 1432 return NULL_TREE;
1433}
1434
1435/* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1436 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
1437 return NULL_TREE. */
1438
1439static tree
1440negate_expr (tree t)
1441{
1442 tree type, tem;
1443
1444 if (t == NULL_TREE)
1445 return NULL_TREE;
1446
1447 type = TREE_TYPE (t);
1448 STRIP_SIGN_NOPS (t);
1449
1450 tem = fold_negate_expr (t);
1451 if (!tem)
1452 tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
b30e3dbc 1453 return fold_convert (type, tem);
23ec2d5e 1454}
1455\f
1456/* Split a tree IN into a constant, literal and variable parts that could be
1457 combined with CODE to make IN. "constant" means an expression with
1458 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1459 commutative arithmetic operation. Store the constant part into *CONP,
b07ba9ff 1460 the literal in *LITP and return the variable part. If a part isn't
23ec2d5e 1461 present, set it to null. If the tree does not decompose in this way,
1462 return the entire tree as the variable part and the other parts as null.
1463
1464 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
b07ba9ff 1465 case, we negate an operand that was subtracted. Except if it is a
1466 literal for which we use *MINUS_LITP instead.
1467
1468 If NEGATE_P is true, we are negating all of IN, again except a literal
1469 for which we use *MINUS_LITP instead.
23ec2d5e 1470
1471 If IN is itself a literal or constant, return it as appropriate.
1472
1473 Note that we do not guarantee that any of the three values will be the
1474 same type as IN, but they will have the same signedness and mode. */
1475
1476static tree
dc81944a 1477split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1478 tree *minus_litp, int negate_p)
2bc77e10 1479{
23ec2d5e 1480 tree var = 0;
1481
2bc77e10 1482 *conp = 0;
23ec2d5e 1483 *litp = 0;
b07ba9ff 1484 *minus_litp = 0;
23ec2d5e 1485
6312a35e 1486 /* Strip any conversions that don't change the machine mode or signedness. */
23ec2d5e 1487 STRIP_SIGN_NOPS (in);
1488
06f0b99c 1489 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1490 || TREE_CODE (in) == FIXED_CST)
23ec2d5e 1491 *litp = in;
23ec2d5e 1492 else if (TREE_CODE (in) == code
a145256a 1493 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
06f0b99c 1494 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
23ec2d5e 1495 /* We can associate addition and subtraction together (even
1496 though the C standard doesn't say so) for integers because
1497 the value is not affected. For reals, the value might be
1498 affected, so we can't. */
1499 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1500 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1501 {
1502 tree op0 = TREE_OPERAND (in, 0);
1503 tree op1 = TREE_OPERAND (in, 1);
1504 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1505 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1506
1507 /* First see if either of the operands is a literal, then a constant. */
06f0b99c 1508 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1509 || TREE_CODE (op0) == FIXED_CST)
23ec2d5e 1510 *litp = op0, op0 = 0;
06f0b99c 1511 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1512 || TREE_CODE (op1) == FIXED_CST)
23ec2d5e 1513 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1514
1515 if (op0 != 0 && TREE_CONSTANT (op0))
1516 *conp = op0, op0 = 0;
1517 else if (op1 != 0 && TREE_CONSTANT (op1))
1518 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1519
1520 /* If we haven't dealt with either operand, this is not a case we can
6312a35e 1521 decompose. Otherwise, VAR is either of the ones remaining, if any. */
23ec2d5e 1522 if (op0 != 0 && op1 != 0)
1523 var = in;
1524 else if (op0 != 0)
1525 var = op0;
1526 else
1527 var = op1, neg_var_p = neg1_p;
2bc77e10 1528
23ec2d5e 1529 /* Now do any needed negations. */
b07ba9ff 1530 if (neg_litp_p)
1531 *minus_litp = *litp, *litp = 0;
1532 if (neg_conp_p)
1533 *conp = negate_expr (*conp);
1534 if (neg_var_p)
1535 var = negate_expr (var);
23ec2d5e 1536 }
8541c166 1537 else if (TREE_CONSTANT (in))
1538 *conp = in;
23ec2d5e 1539 else
1540 var = in;
1541
1542 if (negate_p)
2bc77e10 1543 {
b07ba9ff 1544 if (*litp)
1545 *minus_litp = *litp, *litp = 0;
1546 else if (*minus_litp)
1547 *litp = *minus_litp, *minus_litp = 0;
23ec2d5e 1548 *conp = negate_expr (*conp);
b07ba9ff 1549 var = negate_expr (var);
2bc77e10 1550 }
23ec2d5e 1551
1552 return var;
1553}
1554
1555/* Re-associate trees split by the above function. T1 and T2 are either
1556 expressions to associate or null. Return the new expression, if any. If
b07ba9ff 1557 we build an operation, do it in TYPE and with CODE. */
23ec2d5e 1558
1559static tree
de1b648b 1560associate_trees (tree t1, tree t2, enum tree_code code, tree type)
23ec2d5e 1561{
23ec2d5e 1562 if (t1 == 0)
1563 return t2;
1564 else if (t2 == 0)
1565 return t1;
1566
23ec2d5e 1567 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1568 try to fold this since we will have infinite recursion. But do
1569 deal with any NEGATE_EXPRs. */
1570 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1571 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1572 {
5a3fb4d3 1573 if (code == PLUS_EXPR)
1574 {
1575 if (TREE_CODE (t1) == NEGATE_EXPR)
fd96eeef 1576 return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1577 fold_convert (type, TREE_OPERAND (t1, 0)));
5a3fb4d3 1578 else if (TREE_CODE (t2) == NEGATE_EXPR)
fd96eeef 1579 return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1580 fold_convert (type, TREE_OPERAND (t2, 0)));
faab57e3 1581 else if (integer_zerop (t2))
1582 return fold_convert (type, t1);
5a3fb4d3 1583 }
faab57e3 1584 else if (code == MINUS_EXPR)
1585 {
1586 if (integer_zerop (t2))
1587 return fold_convert (type, t1);
1588 }
1589
fd96eeef 1590 return build2 (code, type, fold_convert (type, t1),
1591 fold_convert (type, t2));
23ec2d5e 1592 }
1593
7ab7fd4f 1594 return fold_build2 (code, type, fold_convert (type, t1),
1595 fold_convert (type, t2));
2bc77e10 1596}
1597\f
2455d3ef 1598/* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1599 for use in int_const_binop, size_binop and size_diffop. */
1600
1601static bool
b4b34335 1602int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
2455d3ef 1603{
1604 if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1605 return false;
1606 if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1607 return false;
1608
1609 switch (code)
1610 {
1611 case LSHIFT_EXPR:
1612 case RSHIFT_EXPR:
1613 case LROTATE_EXPR:
1614 case RROTATE_EXPR:
1615 return true;
1616
1617 default:
1618 break;
1619 }
1620
1621 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1622 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1623 && TYPE_MODE (type1) == TYPE_MODE (type2);
1624}
1625
1626
0dbd1c74 1627/* Combine two integer constants ARG1 and ARG2 under operation CODE
32cef1cc 1628 to produce a new constant. Return NULL_TREE if we don't know how
1629 to evaluate CODE at compile-time.
5485823f 1630
15d769aa 1631 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
2bc77e10 1632
4ee9c684 1633tree
b7bf20db 1634int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
2bc77e10 1635{
a0c2c45b 1636 unsigned HOST_WIDE_INT int1l, int2l;
1637 HOST_WIDE_INT int1h, int2h;
1638 unsigned HOST_WIDE_INT low;
1639 HOST_WIDE_INT hi;
1640 unsigned HOST_WIDE_INT garbagel;
1641 HOST_WIDE_INT garbageh;
19cb6b50 1642 tree t;
15d769aa 1643 tree type = TREE_TYPE (arg1);
78a8ed03 1644 int uns = TYPE_UNSIGNED (type);
15d769aa 1645 int is_sizetype
1646 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
0dbd1c74 1647 int overflow = 0;
8ea862a9 1648
0dbd1c74 1649 int1l = TREE_INT_CST_LOW (arg1);
1650 int1h = TREE_INT_CST_HIGH (arg1);
1651 int2l = TREE_INT_CST_LOW (arg2);
1652 int2h = TREE_INT_CST_HIGH (arg2);
1653
1654 switch (code)
2bc77e10 1655 {
0dbd1c74 1656 case BIT_IOR_EXPR:
1657 low = int1l | int2l, hi = int1h | int2h;
1658 break;
2bc77e10 1659
0dbd1c74 1660 case BIT_XOR_EXPR:
1661 low = int1l ^ int2l, hi = int1h ^ int2h;
1662 break;
2bc77e10 1663
0dbd1c74 1664 case BIT_AND_EXPR:
1665 low = int1l & int2l, hi = int1h & int2h;
1666 break;
2bc77e10 1667
0dbd1c74 1668 case RSHIFT_EXPR:
cc049fa3 1669 int2l = -int2l;
0dbd1c74 1670 case LSHIFT_EXPR:
1671 /* It's unclear from the C standard whether shifts can overflow.
1672 The following code ignores overflow; perhaps a C standard
1673 interpretation ruling is needed. */
15d769aa 1674 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
02e7a332 1675 &low, &hi, !uns);
0dbd1c74 1676 break;
2bc77e10 1677
0dbd1c74 1678 case RROTATE_EXPR:
1679 int2l = - int2l;
1680 case LROTATE_EXPR:
15d769aa 1681 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
0dbd1c74 1682 &low, &hi);
1683 break;
2bc77e10 1684
0dbd1c74 1685 case PLUS_EXPR:
1686 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1687 break;
2bc77e10 1688
0dbd1c74 1689 case MINUS_EXPR:
1690 neg_double (int2l, int2h, &low, &hi);
1691 add_double (int1l, int1h, low, hi, &low, &hi);
083a2b5e 1692 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
0dbd1c74 1693 break;
2bc77e10 1694
0dbd1c74 1695 case MULT_EXPR:
1696 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1697 break;
2bc77e10 1698
0dbd1c74 1699 case TRUNC_DIV_EXPR:
1700 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1701 case EXACT_DIV_EXPR:
1702 /* This is a shortcut for a common special case. */
a0c2c45b 1703 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
f96bd2bf 1704 && !TREE_OVERFLOW (arg1)
1705 && !TREE_OVERFLOW (arg2)
a0c2c45b 1706 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
0dbd1c74 1707 {
1708 if (code == CEIL_DIV_EXPR)
1709 int1l += int2l - 1;
a0c2c45b 1710
0dbd1c74 1711 low = int1l / int2l, hi = 0;
2bc77e10 1712 break;
0dbd1c74 1713 }
2bc77e10 1714
6312a35e 1715 /* ... fall through ... */
2bc77e10 1716
cc049fa3 1717 case ROUND_DIV_EXPR:
32cef1cc 1718 if (int2h == 0 && int2l == 0)
1719 return NULL_TREE;
0dbd1c74 1720 if (int2h == 0 && int2l == 1)
1721 {
1722 low = int1l, hi = int1h;
2bc77e10 1723 break;
0dbd1c74 1724 }
1725 if (int1l == int2l && int1h == int2h
1726 && ! (int1l == 0 && int1h == 0))
1727 {
1728 low = 1, hi = 0;
c13e6dce 1729 break;
0dbd1c74 1730 }
15d769aa 1731 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
0dbd1c74 1732 &low, &hi, &garbagel, &garbageh);
1733 break;
c13e6dce 1734
0dbd1c74 1735 case TRUNC_MOD_EXPR:
1736 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1737 /* This is a shortcut for a common special case. */
a0c2c45b 1738 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
f96bd2bf 1739 && !TREE_OVERFLOW (arg1)
1740 && !TREE_OVERFLOW (arg2)
a0c2c45b 1741 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
0dbd1c74 1742 {
1743 if (code == CEIL_MOD_EXPR)
1744 int1l += int2l - 1;
1745 low = int1l % int2l, hi = 0;
c13e6dce 1746 break;
0dbd1c74 1747 }
c13e6dce 1748
6312a35e 1749 /* ... fall through ... */
0dbd1c74 1750
cc049fa3 1751 case ROUND_MOD_EXPR:
32cef1cc 1752 if (int2h == 0 && int2l == 0)
1753 return NULL_TREE;
0dbd1c74 1754 overflow = div_and_round_double (code, uns,
1755 int1l, int1h, int2l, int2h,
1756 &garbagel, &garbageh, &low, &hi);
1757 break;
1758
1759 case MIN_EXPR:
1760 case MAX_EXPR:
1761 if (uns)
083a2b5e 1762 low = (((unsigned HOST_WIDE_INT) int1h
1763 < (unsigned HOST_WIDE_INT) int2h)
1764 || (((unsigned HOST_WIDE_INT) int1h
1765 == (unsigned HOST_WIDE_INT) int2h)
a0c2c45b 1766 && int1l < int2l));
a3f1e3ec 1767 else
a0c2c45b 1768 low = (int1h < int2h
1769 || (int1h == int2h && int1l < int2l));
083a2b5e 1770
0dbd1c74 1771 if (low == (code == MIN_EXPR))
1772 low = int1l, hi = int1h;
1773 else
1774 low = int2l, hi = int2h;
1775 break;
8ea862a9 1776
0dbd1c74 1777 default:
32cef1cc 1778 return NULL_TREE;
8ea862a9 1779 }
0dbd1c74 1780
4d28c5d1 1781 if (notrunc)
1782 {
c8110c8f 1783 t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1784
4d28c5d1 1785 /* Propagate overflow flags ourselves. */
1786 if (((!uns || is_sizetype) && overflow)
1787 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
00b76131 1788 {
1789 t = copy_node (t);
1790 TREE_OVERFLOW (t) = 1;
00b76131 1791 }
4d28c5d1 1792 }
1793 else
c8110c8f 1794 t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1795 ((!uns || is_sizetype) && overflow)
eddad94a 1796 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
0c5713a2 1797
0dbd1c74 1798 return t;
1799}
1800
083a2b5e 1801/* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1802 constant. We assume ARG1 and ARG2 have the same data type, or at least
0b6fa2ba 1803 are the same kind of constant and the same machine mode. Return zero if
1804 combining the constants is not allowed in the current operating mode.
0dbd1c74 1805
1806 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1807
1808static tree
de1b648b 1809const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
0dbd1c74 1810{
0b6fa2ba 1811 /* Sanity check for the recursive cases. */
1812 if (!arg1 || !arg2)
1813 return NULL_TREE;
1814
cc049fa3 1815 STRIP_NOPS (arg1);
1816 STRIP_NOPS (arg2);
0dbd1c74 1817
1818 if (TREE_CODE (arg1) == INTEGER_CST)
15d769aa 1819 return int_const_binop (code, arg1, arg2, notrunc);
0dbd1c74 1820
2bc77e10 1821 if (TREE_CODE (arg1) == REAL_CST)
1822 {
276beea2 1823 enum machine_mode mode;
9a24cfc6 1824 REAL_VALUE_TYPE d1;
1825 REAL_VALUE_TYPE d2;
536f5fb1 1826 REAL_VALUE_TYPE value;
a47b9d79 1827 REAL_VALUE_TYPE result;
1828 bool inexact;
276beea2 1829 tree t, type;
2bc77e10 1830
32cef1cc 1831 /* The following codes are handled by real_arithmetic. */
1832 switch (code)
1833 {
1834 case PLUS_EXPR:
1835 case MINUS_EXPR:
1836 case MULT_EXPR:
1837 case RDIV_EXPR:
1838 case MIN_EXPR:
1839 case MAX_EXPR:
1840 break;
1841
1842 default:
1843 return NULL_TREE;
1844 }
1845
9a24cfc6 1846 d1 = TREE_REAL_CST (arg1);
1847 d2 = TREE_REAL_CST (arg2);
9248d3e0 1848
276beea2 1849 type = TREE_TYPE (arg1);
1850 mode = TYPE_MODE (type);
1851
1852 /* Don't perform operation if we honor signaling NaNs and
1853 either operand is a NaN. */
1854 if (HONOR_SNANS (mode)
1855 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1856 return NULL_TREE;
1857
1858 /* Don't perform operation if it would raise a division
1859 by zero exception. */
1860 if (code == RDIV_EXPR
1861 && REAL_VALUES_EQUAL (d2, dconst0)
1862 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1863 return NULL_TREE;
1864
9248d3e0 1865 /* If either operand is a NaN, just return it. Otherwise, set up
1866 for floating-point trap; we return an overflow. */
1867 if (REAL_VALUE_ISNAN (d1))
1868 return arg1;
1869 else if (REAL_VALUE_ISNAN (d2))
1870 return arg2;
70192c5e 1871
a47b9d79 1872 inexact = real_arithmetic (&value, code, &d1, &d2);
1873 real_convert (&result, mode, &value);
cc049fa3 1874
f2e0dda3 1875 /* Don't constant fold this floating point operation if
1876 the result has overflowed and flag_trapping_math. */
f2e0dda3 1877 if (flag_trapping_math
1878 && MODE_HAS_INFINITIES (mode)
1879 && REAL_VALUE_ISINF (result)
1880 && !REAL_VALUE_ISINF (d1)
1881 && !REAL_VALUE_ISINF (d2))
1882 return NULL_TREE;
1883
a47b9d79 1884 /* Don't constant fold this floating point operation if the
1885 result may dependent upon the run-time rounding mode and
09fde96c 1886 flag_rounding_math is set, or if GCC's software emulation
1887 is unable to accurately represent the result. */
09fde96c 1888 if ((flag_rounding_math
f2b38121 1889 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
a47b9d79 1890 && (inexact || !real_identical (&result, &value)))
1891 return NULL_TREE;
1892
1893 t = build_real (type, result);
23fed9b2 1894
4d28c5d1 1895 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
c0244247 1896 return t;
2bc77e10 1897 }
32cef1cc 1898
06f0b99c 1899 if (TREE_CODE (arg1) == FIXED_CST)
1900 {
1901 FIXED_VALUE_TYPE f1;
1902 FIXED_VALUE_TYPE f2;
1903 FIXED_VALUE_TYPE result;
1904 tree t, type;
1905 int sat_p;
1906 bool overflow_p;
1907
1908 /* The following codes are handled by fixed_arithmetic. */
1909 switch (code)
1910 {
1911 case PLUS_EXPR:
1912 case MINUS_EXPR:
1913 case MULT_EXPR:
1914 case TRUNC_DIV_EXPR:
1915 f2 = TREE_FIXED_CST (arg2);
1916 break;
1917
1918 case LSHIFT_EXPR:
1919 case RSHIFT_EXPR:
1920 f2.data.high = TREE_INT_CST_HIGH (arg2);
1921 f2.data.low = TREE_INT_CST_LOW (arg2);
1922 f2.mode = SImode;
1923 break;
1924
1925 default:
1926 return NULL_TREE;
1927 }
1928
1929 f1 = TREE_FIXED_CST (arg1);
1930 type = TREE_TYPE (arg1);
1931 sat_p = TYPE_SATURATING (type);
1932 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1933 t = build_fixed (type, result);
1934 /* Propagate overflow flags. */
1935 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
62126877 1936 TREE_OVERFLOW (t) = 1;
06f0b99c 1937 return t;
1938 }
1939
2bc77e10 1940 if (TREE_CODE (arg1) == COMPLEX_CST)
1941 {
19cb6b50 1942 tree type = TREE_TYPE (arg1);
1943 tree r1 = TREE_REALPART (arg1);
1944 tree i1 = TREE_IMAGPART (arg1);
1945 tree r2 = TREE_REALPART (arg2);
1946 tree i2 = TREE_IMAGPART (arg2);
0b6fa2ba 1947 tree real, imag;
2bc77e10 1948
1949 switch (code)
1950 {
1951 case PLUS_EXPR:
2bc77e10 1952 case MINUS_EXPR:
0b6fa2ba 1953 real = const_binop (code, r1, r2, notrunc);
1954 imag = const_binop (code, i1, i2, notrunc);
2bc77e10 1955 break;
1956
1957 case MULT_EXPR:
0b6fa2ba 1958 real = const_binop (MINUS_EXPR,
1959 const_binop (MULT_EXPR, r1, r2, notrunc),
1960 const_binop (MULT_EXPR, i1, i2, notrunc),
1961 notrunc);
1962 imag = const_binop (PLUS_EXPR,
1963 const_binop (MULT_EXPR, r1, i2, notrunc),
1964 const_binop (MULT_EXPR, i1, r2, notrunc),
1965 notrunc);
2bc77e10 1966 break;
1967
1968 case RDIV_EXPR:
1969 {
19cb6b50 1970 tree magsquared
2bc77e10 1971 = const_binop (PLUS_EXPR,
5485823f 1972 const_binop (MULT_EXPR, r2, r2, notrunc),
1973 const_binop (MULT_EXPR, i2, i2, notrunc),
1974 notrunc);
0b6fa2ba 1975 tree t1
1976 = const_binop (PLUS_EXPR,
1977 const_binop (MULT_EXPR, r1, r2, notrunc),
1978 const_binop (MULT_EXPR, i1, i2, notrunc),
1979 notrunc);
1980 tree t2
1981 = const_binop (MINUS_EXPR,
1982 const_binop (MULT_EXPR, i1, r2, notrunc),
1983 const_binop (MULT_EXPR, r1, i2, notrunc),
1984 notrunc);
86a914ce 1985
1986 if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
0b6fa2ba 1987 code = TRUNC_DIV_EXPR;
1988
1989 real = const_binop (code, t1, magsquared, notrunc);
1990 imag = const_binop (code, t2, magsquared, notrunc);
2bc77e10 1991 }
1992 break;
1993
1994 default:
32cef1cc 1995 return NULL_TREE;
2bc77e10 1996 }
0b6fa2ba 1997
1998 if (real && imag)
1999 return build_complex (type, real, imag);
2bc77e10 2000 }
0b6fa2ba 2001
87de4c68 2002 if (TREE_CODE (arg1) == VECTOR_CST)
2003 {
2004 tree type = TREE_TYPE(arg1);
2005 int count = TYPE_VECTOR_SUBPARTS (type), i;
2006 tree elements1, elements2, list = NULL_TREE;
2007
2008 if(TREE_CODE(arg2) != VECTOR_CST)
2009 return NULL_TREE;
2010
2011 elements1 = TREE_VECTOR_CST_ELTS (arg1);
2012 elements2 = TREE_VECTOR_CST_ELTS (arg2);
2013
2014 for (i = 0; i < count; i++)
2015 {
2016 tree elem1, elem2, elem;
2017
2018 /* The trailing elements can be empty and should be treated as 0 */
2019 if(!elements1)
2020 elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2021 else
2022 {
2023 elem1 = TREE_VALUE(elements1);
2024 elements1 = TREE_CHAIN (elements1);
2025 }
2026
2027 if(!elements2)
2028 elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2029 else
2030 {
2031 elem2 = TREE_VALUE(elements2);
2032 elements2 = TREE_CHAIN (elements2);
2033 }
2034
2035 elem = const_binop (code, elem1, elem2, notrunc);
2036
2037 /* It is possible that const_binop cannot handle the given
2038 code and return NULL_TREE */
2039 if(elem == NULL_TREE)
2040 return NULL_TREE;
2041
2042 list = tree_cons (NULL_TREE, elem, list);
2043 }
2044 return build_vector(type, nreverse(list));
2045 }
32cef1cc 2046 return NULL_TREE;
2bc77e10 2047}
15d769aa 2048
85390276 2049/* Create a size type INT_CST node with NUMBER sign extended. KIND
2050 indicates which particular sizetype to create. */
083a2b5e 2051
902de8ed 2052tree
1e9d55d7 2053size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
902de8ed 2054{
85390276 2055 return build_int_cst (sizetype_tab[(int) kind], number);
902de8ed 2056}
85390276 2057\f
902de8ed 2058/* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2059 is a tree code. The type of the result is taken from the operands.
2455d3ef 2060 Both must be equivalent integer types, ala int_binop_types_match_p.
2bc77e10 2061 If the operands are constant, so is the result. */
2062
2063tree
de1b648b 2064size_binop (enum tree_code code, tree arg0, tree arg1)
2bc77e10 2065{
902de8ed 2066 tree type = TREE_TYPE (arg0);
2067
4a698d62 2068 if (arg0 == error_mark_node || arg1 == error_mark_node)
2069 return error_mark_node;
2070
2455d3ef 2071 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2072 TREE_TYPE (arg1)));
902de8ed 2073
2bc77e10 2074 /* Handle the special case of two integer constants faster. */
2075 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2076 {
2077 /* And some specific cases even faster than that. */
6117e415 2078 if (code == PLUS_EXPR)
2079 {
2080 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2081 return arg1;
2082 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2083 return arg0;
2084 }
2085 else if (code == MINUS_EXPR)
2086 {
2087 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2088 return arg0;
2089 }
2090 else if (code == MULT_EXPR)
2091 {
2092 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2093 return arg1;
2094 }
a7baffe5 2095
2bc77e10 2096 /* Handle general case of two integer constants. */
15d769aa 2097 return int_const_binop (code, arg0, arg1, 0);
2bc77e10 2098 }
2099
7ab7fd4f 2100 return fold_build2 (code, type, arg0, arg1);
2bc77e10 2101}
3fd3b688 2102
902de8ed 2103/* Given two values, either both of sizetype or both of bitsizetype,
2104 compute the difference between the two values. Return the value
2105 in signed type corresponding to the type of the operands. */
3fd3b688 2106
2107tree
de1b648b 2108size_diffop (tree arg0, tree arg1)
3fd3b688 2109{
902de8ed 2110 tree type = TREE_TYPE (arg0);
2111 tree ctype;
3fd3b688 2112
2455d3ef 2113 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2114 TREE_TYPE (arg1)));
3fd3b688 2115
902de8ed 2116 /* If the type is already signed, just do the simple thing. */
78a8ed03 2117 if (!TYPE_UNSIGNED (type))
902de8ed 2118 return size_binop (MINUS_EXPR, arg0, arg1);
2119
2455d3ef 2120 if (type == sizetype)
2121 ctype = ssizetype;
2122 else if (type == bitsizetype)
2123 ctype = sbitsizetype;
2124 else
11773141 2125 ctype = signed_type_for (type);
902de8ed 2126
2127 /* If either operand is not a constant, do the conversions to the signed
2128 type and subtract. The hardware will do the right thing with any
2129 overflow in the subtraction. */
2130 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
b30e3dbc 2131 return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2132 fold_convert (ctype, arg1));
902de8ed 2133
2134 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2135 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2136 overflow) and negate (which can't either). Special-case a result
2137 of zero while we're here. */
2138 if (tree_int_cst_equal (arg0, arg1))
3c6185f1 2139 return build_int_cst (ctype, 0);
902de8ed 2140 else if (tree_int_cst_lt (arg1, arg0))
b30e3dbc 2141 return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
902de8ed 2142 else
3c6185f1 2143 return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
b30e3dbc 2144 fold_convert (ctype, size_binop (MINUS_EXPR,
2145 arg1, arg0)));
3fd3b688 2146}
2bc77e10 2147\f
b38d56be 2148/* A subroutine of fold_convert_const handling conversions of an
2149 INTEGER_CST to another integer type. */
80db63ef 2150
2151static tree
b4b34335 2152fold_convert_const_int_from_int (tree type, const_tree arg1)
80db63ef 2153{
b38d56be 2154 tree t;
80db63ef 2155
b38d56be 2156 /* Given an integer constant, make new constant with new type,
2157 appropriately sign-extended or truncated. */
c8110c8f 2158 t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2159 TREE_INT_CST_HIGH (arg1),
2160 /* Don't set the overflow when
0cfec3e9 2161 converting from a pointer, */
2162 !POINTER_TYPE_P (TREE_TYPE (arg1))
2163 /* or to a sizetype with same signedness
2164 and the precision is unchanged.
2165 ??? sizetype is always sign-extended,
2166 but its signedness depends on the
2167 frontend. Thus we see spurious overflows
2168 here if we do not check this. */
2169 && !((TYPE_PRECISION (TREE_TYPE (arg1))
2170 == TYPE_PRECISION (type))
2171 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2172 == TYPE_UNSIGNED (type))
2173 && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2174 && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2175 || (TREE_CODE (type) == INTEGER_TYPE
2176 && TYPE_IS_SIZETYPE (type)))),
c8110c8f 2177 (TREE_INT_CST_HIGH (arg1) < 0
2178 && (TYPE_UNSIGNED (type)
2179 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
eddad94a 2180 | TREE_OVERFLOW (arg1));
80db63ef 2181
b38d56be 2182 return t;
80db63ef 2183}
2184
b38d56be 2185/* A subroutine of fold_convert_const handling conversions a REAL_CST
2186 to an integer type. */
2bc77e10 2187
2188static tree
b4b34335 2189fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2bc77e10 2190{
23fed9b2 2191 int overflow = 0;
04b253e8 2192 tree t;
2193
b38d56be 2194 /* The following code implements the floating point to integer
2195 conversion rules required by the Java Language Specification,
2196 that IEEE NaNs are mapped to zero and values that overflow
2197 the target precision saturate, i.e. values greater than
2198 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2199 are mapped to INT_MIN. These semantics are allowed by the
2200 C and C++ standards that simply state that the behavior of
2201 FP-to-integer conversion is unspecified upon overflow. */
2bc77e10 2202
b38d56be 2203 HOST_WIDE_INT high, low;
2204 REAL_VALUE_TYPE r;
2205 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2206
2207 switch (code)
2bc77e10 2208 {
b38d56be 2209 case FIX_TRUNC_EXPR:
2210 real_trunc (&r, VOIDmode, &x);
2211 break;
2212
b38d56be 2213 default:
2214 gcc_unreachable ();
2215 }
2216
2217 /* If R is NaN, return zero and show we have an overflow. */
2218 if (REAL_VALUE_ISNAN (r))
2219 {
2220 overflow = 1;
2221 high = 0;
2222 low = 0;
2223 }
2224
2225 /* See if R is less than the lower bound or greater than the
2226 upper bound. */
2227
2228 if (! overflow)
2229 {
2230 tree lt = TYPE_MIN_VALUE (type);
2231 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2232 if (REAL_VALUES_LESS (r, l))
2bc77e10 2233 {
b38d56be 2234 overflow = 1;
2235 high = TREE_INT_CST_HIGH (lt);
2236 low = TREE_INT_CST_LOW (lt);
2bc77e10 2237 }
b38d56be 2238 }
2239
2240 if (! overflow)
2241 {
2242 tree ut = TYPE_MAX_VALUE (type);
2243 if (ut)
2bc77e10 2244 {
b38d56be 2245 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2246 if (REAL_VALUES_LESS (u, r))
04b253e8 2247 {
b38d56be 2248 overflow = 1;
2249 high = TREE_INT_CST_HIGH (ut);
2250 low = TREE_INT_CST_LOW (ut);
2251 }
2252 }
2253 }
04b253e8 2254
b38d56be 2255 if (! overflow)
2256 REAL_VALUE_TO_INT (&low, &high, r);
04b253e8 2257
c8110c8f 2258 t = force_fit_type_double (type, low, high, -1,
eddad94a 2259 overflow | TREE_OVERFLOW (arg1));
b38d56be 2260 return t;
2261}
50c90ea2 2262
06f0b99c 2263/* A subroutine of fold_convert_const handling conversions of a
2264 FIXED_CST to an integer type. */
2265
2266static tree
b4b34335 2267fold_convert_const_int_from_fixed (tree type, const_tree arg1)
06f0b99c 2268{
2269 tree t;
2270 double_int temp, temp_trunc;
2271 unsigned int mode;
2272
2273 /* Right shift FIXED_CST to temp by fbit. */
2274 temp = TREE_FIXED_CST (arg1).data;
2275 mode = TREE_FIXED_CST (arg1).mode;
2276 if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2277 {
2278 lshift_double (temp.low, temp.high,
2279 - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2280 &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2281
2282 /* Left shift temp to temp_trunc by fbit. */
2283 lshift_double (temp.low, temp.high,
2284 GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2285 &temp_trunc.low, &temp_trunc.high,
2286 SIGNED_FIXED_POINT_MODE_P (mode));
2287 }
2288 else
2289 {
2290 temp.low = 0;
2291 temp.high = 0;
2292 temp_trunc.low = 0;
2293 temp_trunc.high = 0;
2294 }
2295
2296 /* If FIXED_CST is negative, we need to round the value toward 0.
2297 By checking if the fractional bits are not zero to add 1 to temp. */
2298 if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2299 && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2300 {
2301 double_int one;
2302 one.low = 1;
2303 one.high = 0;
2304 temp = double_int_add (temp, one);
2305 }
2306
2307 /* Given a fixed-point constant, make new constant with new type,
2308 appropriately sign-extended or truncated. */
2309 t = force_fit_type_double (type, temp.low, temp.high, -1,
2310 (temp.high < 0
2311 && (TYPE_UNSIGNED (type)
2312 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2313 | TREE_OVERFLOW (arg1));
2314
2315 return t;
2316}
2317
b38d56be 2318/* A subroutine of fold_convert_const handling conversions a REAL_CST
2319 to another floating point type. */
04b253e8 2320
b38d56be 2321static tree
b4b34335 2322fold_convert_const_real_from_real (tree type, const_tree arg1)
b38d56be 2323{
a47b9d79 2324 REAL_VALUE_TYPE value;
b38d56be 2325 tree t;
f52483b5 2326
a47b9d79 2327 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2328 t = build_real (type, value);
67c65562 2329
b38d56be 2330 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
b38d56be 2331 return t;
2332}
67c65562 2333
06f0b99c 2334/* A subroutine of fold_convert_const handling conversions a FIXED_CST
2335 to a floating point type. */
2336
2337static tree
b4b34335 2338fold_convert_const_real_from_fixed (tree type, const_tree arg1)
06f0b99c 2339{
2340 REAL_VALUE_TYPE value;
2341 tree t;
2342
2343 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2344 t = build_real (type, value);
2345
2346 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
06f0b99c 2347 return t;
2348}
2349
2350/* A subroutine of fold_convert_const handling conversions a FIXED_CST
2351 to another fixed-point type. */
2352
2353static tree
b4b34335 2354fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
06f0b99c 2355{
2356 FIXED_VALUE_TYPE value;
2357 tree t;
2358 bool overflow_p;
2359
2360 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2361 TYPE_SATURATING (type));
2362 t = build_fixed (type, value);
2363
2364 /* Propagate overflow flags. */
2365 if (overflow_p | TREE_OVERFLOW (arg1))
62126877 2366 TREE_OVERFLOW (t) = 1;
06f0b99c 2367 return t;
2368}
2369
2370/* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2371 to a fixed-point type. */
2372
2373static tree
b4b34335 2374fold_convert_const_fixed_from_int (tree type, const_tree arg1)
06f0b99c 2375{
2376 FIXED_VALUE_TYPE value;
2377 tree t;
2378 bool overflow_p;
2379
2380 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2381 TREE_INT_CST (arg1),
2382 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2383 TYPE_SATURATING (type));
2384 t = build_fixed (type, value);
2385
2386 /* Propagate overflow flags. */
2387 if (overflow_p | TREE_OVERFLOW (arg1))
62126877 2388 TREE_OVERFLOW (t) = 1;
06f0b99c 2389 return t;
2390}
2391
2392/* A subroutine of fold_convert_const handling conversions a REAL_CST
2393 to a fixed-point type. */
2394
2395static tree
b4b34335 2396fold_convert_const_fixed_from_real (tree type, const_tree arg1)
06f0b99c 2397{
2398 FIXED_VALUE_TYPE value;
2399 tree t;
2400 bool overflow_p;
2401
2402 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2403 &TREE_REAL_CST (arg1),
2404 TYPE_SATURATING (type));
2405 t = build_fixed (type, value);
2406
2407 /* Propagate overflow flags. */
2408 if (overflow_p | TREE_OVERFLOW (arg1))
62126877 2409 TREE_OVERFLOW (t) = 1;
06f0b99c 2410 return t;
2411}
2412
b38d56be 2413/* Attempt to fold type conversion operation CODE of expression ARG1 to
2414 type TYPE. If no simplification can be done return NULL_TREE. */
67c65562 2415
b38d56be 2416static tree
2417fold_convert_const (enum tree_code code, tree type, tree arg1)
2418{
2419 if (TREE_TYPE (arg1) == type)
2420 return arg1;
4d28c5d1 2421
0bafabac 2422 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2423 || TREE_CODE (type) == OFFSET_TYPE)
b38d56be 2424 {
2425 if (TREE_CODE (arg1) == INTEGER_CST)
2426 return fold_convert_const_int_from_int (type, arg1);
2427 else if (TREE_CODE (arg1) == REAL_CST)
2428 return fold_convert_const_int_from_real (code, type, arg1);
06f0b99c 2429 else if (TREE_CODE (arg1) == FIXED_CST)
2430 return fold_convert_const_int_from_fixed (type, arg1);
2bc77e10 2431 }
2432 else if (TREE_CODE (type) == REAL_TYPE)
2433 {
2bc77e10 2434 if (TREE_CODE (arg1) == INTEGER_CST)
2435 return build_real_from_int_cst (type, arg1);
06f0b99c 2436 else if (TREE_CODE (arg1) == REAL_CST)
b38d56be 2437 return fold_convert_const_real_from_real (type, arg1);
06f0b99c 2438 else if (TREE_CODE (arg1) == FIXED_CST)
2439 return fold_convert_const_real_from_fixed (type, arg1);
2440 }
2441 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2442 {
2443 if (TREE_CODE (arg1) == FIXED_CST)
2444 return fold_convert_const_fixed_from_fixed (type, arg1);
2445 else if (TREE_CODE (arg1) == INTEGER_CST)
2446 return fold_convert_const_fixed_from_int (type, arg1);
2447 else if (TREE_CODE (arg1) == REAL_CST)
2448 return fold_convert_const_fixed_from_real (type, arg1);
2bc77e10 2449 }
04b253e8 2450 return NULL_TREE;
2bc77e10 2451}
b30e3dbc 2452
b38d56be 2453/* Construct a vector of zero elements of vector type TYPE. */
2454
2455static tree
2456build_zero_vector (tree type)
2457{
2458 tree elem, list;
2459 int i, units;
2460
2461 elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2462 units = TYPE_VECTOR_SUBPARTS (type);
2463
2464 list = NULL_TREE;
2465 for (i = 0; i < units; i++)
2466 list = tree_cons (NULL_TREE, elem, list);
2467 return build_vector (type, list);
2468}
2469
f549b28d 2470/* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2471
2472bool
b7bf20db 2473fold_convertible_p (const_tree type, const_tree arg)
f549b28d 2474{
2475 tree orig = TREE_TYPE (arg);
2476
2477 if (type == orig)
2478 return true;
2479
2480 if (TREE_CODE (arg) == ERROR_MARK
2481 || TREE_CODE (type) == ERROR_MARK
2482 || TREE_CODE (orig) == ERROR_MARK)
2483 return false;
2484
2485 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2486 return true;
2487
2488 switch (TREE_CODE (type))
2489 {
2490 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2491 case POINTER_TYPE: case REFERENCE_TYPE:
2492 case OFFSET_TYPE:
2493 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2494 || TREE_CODE (orig) == OFFSET_TYPE)
2495 return true;
2496 return (TREE_CODE (orig) == VECTOR_TYPE
2497 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2498
4a2f7014 2499 case REAL_TYPE:
2500 case FIXED_POINT_TYPE:
2501 case COMPLEX_TYPE:
2502 case VECTOR_TYPE:
2503 case VOID_TYPE:
f549b28d 2504 return TREE_CODE (type) == TREE_CODE (orig);
4a2f7014 2505
2506 default:
2507 return false;
f549b28d 2508 }
2509}
2510
b30e3dbc 2511/* Convert expression ARG to type TYPE. Used by the middle-end for
2512 simple conversions in preference to calling the front-end's convert. */
2513
d7aeca92 2514tree
b30e3dbc 2515fold_convert (tree type, tree arg)
2516{
2517 tree orig = TREE_TYPE (arg);
2518 tree tem;
2519
2520 if (type == orig)
2521 return arg;
2522
2523 if (TREE_CODE (arg) == ERROR_MARK
2524 || TREE_CODE (type) == ERROR_MARK
2525 || TREE_CODE (orig) == ERROR_MARK)
2526 return error_mark_node;
2527
c8ca3ee7 2528 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
7ab7fd4f 2529 return fold_build1 (NOP_EXPR, type, arg);
b30e3dbc 2530
fdada98f 2531 switch (TREE_CODE (type))
b30e3dbc 2532 {
63bf54cf 2533 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
fdada98f 2534 case POINTER_TYPE: case REFERENCE_TYPE:
2535 case OFFSET_TYPE:
b30e3dbc 2536 if (TREE_CODE (arg) == INTEGER_CST)
2537 {
2538 tem = fold_convert_const (NOP_EXPR, type, arg);
2539 if (tem != NULL_TREE)
2540 return tem;
2541 }
8d4b8f86 2542 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2543 || TREE_CODE (orig) == OFFSET_TYPE)
7ab7fd4f 2544 return fold_build1 (NOP_EXPR, type, arg);
b30e3dbc 2545 if (TREE_CODE (orig) == COMPLEX_TYPE)
2546 {
7ab7fd4f 2547 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
b30e3dbc 2548 return fold_convert (type, tem);
2549 }
fdada98f 2550 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2551 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
7ab7fd4f 2552 return fold_build1 (NOP_EXPR, type, arg);
0c5713a2 2553
fdada98f 2554 case REAL_TYPE:
b30e3dbc 2555 if (TREE_CODE (arg) == INTEGER_CST)
2556 {
2557 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2558 if (tem != NULL_TREE)
2559 return tem;
2560 }
2561 else if (TREE_CODE (arg) == REAL_CST)
2562 {
2563 tem = fold_convert_const (NOP_EXPR, type, arg);
2564 if (tem != NULL_TREE)
2565 return tem;
2566 }
06f0b99c 2567 else if (TREE_CODE (arg) == FIXED_CST)
2568 {
2569 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2570 if (tem != NULL_TREE)
2571 return tem;
2572 }
b30e3dbc 2573
fdada98f 2574 switch (TREE_CODE (orig))
b30e3dbc 2575 {
63bf54cf 2576 case INTEGER_TYPE:
fdada98f 2577 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2578 case POINTER_TYPE: case REFERENCE_TYPE:
7ab7fd4f 2579 return fold_build1 (FLOAT_EXPR, type, arg);
0c5713a2 2580
fdada98f 2581 case REAL_TYPE:
1917f1d7 2582 return fold_build1 (NOP_EXPR, type, arg);
0c5713a2 2583
06f0b99c 2584 case FIXED_POINT_TYPE:
2585 return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2586
2587 case COMPLEX_TYPE:
2588 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2589 return fold_convert (type, tem);
2590
2591 default:
2592 gcc_unreachable ();
2593 }
2594
2595 case FIXED_POINT_TYPE:
2596 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2597 || TREE_CODE (arg) == REAL_CST)
2598 {
2599 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2600 if (tem != NULL_TREE)
2601 return tem;
2602 }
2603
2604 switch (TREE_CODE (orig))
2605 {
2606 case FIXED_POINT_TYPE:
2607 case INTEGER_TYPE:
2608 case ENUMERAL_TYPE:
2609 case BOOLEAN_TYPE:
2610 case REAL_TYPE:
2611 return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2612
fdada98f 2613 case COMPLEX_TYPE:
7ab7fd4f 2614 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
b30e3dbc 2615 return fold_convert (type, tem);
0c5713a2 2616
fdada98f 2617 default:
2618 gcc_unreachable ();
b30e3dbc 2619 }
0c5713a2 2620
fdada98f 2621 case COMPLEX_TYPE:
2622 switch (TREE_CODE (orig))
2623 {
63bf54cf 2624 case INTEGER_TYPE:
fdada98f 2625 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2626 case POINTER_TYPE: case REFERENCE_TYPE:
2627 case REAL_TYPE:
06f0b99c 2628 case FIXED_POINT_TYPE:
fdada98f 2629 return build2 (COMPLEX_EXPR, type,
2630 fold_convert (TREE_TYPE (type), arg),
2631 fold_convert (TREE_TYPE (type), integer_zero_node));
2632 case COMPLEX_TYPE:
2633 {
2634 tree rpart, ipart;
0c5713a2 2635
fdada98f 2636 if (TREE_CODE (arg) == COMPLEX_EXPR)
2637 {
2638 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2639 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
7ab7fd4f 2640 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
fdada98f 2641 }
0c5713a2 2642
fdada98f 2643 arg = save_expr (arg);
7ab7fd4f 2644 rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2645 ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
fdada98f 2646 rpart = fold_convert (TREE_TYPE (type), rpart);
2647 ipart = fold_convert (TREE_TYPE (type), ipart);
7ab7fd4f 2648 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
fdada98f 2649 }
0c5713a2 2650
fdada98f 2651 default:
2652 gcc_unreachable ();
2653 }
0c5713a2 2654
fdada98f 2655 case VECTOR_TYPE:
80db63ef 2656 if (integer_zerop (arg))
2657 return build_zero_vector (type);
fdada98f 2658 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2659 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2660 || TREE_CODE (orig) == VECTOR_TYPE);
a0bfd1b9 2661 return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
b30e3dbc 2662
fdada98f 2663 case VOID_TYPE:
df0137b9 2664 tem = fold_ignored_result (arg);
75a70cf9 2665 if (TREE_CODE (tem) == MODIFY_EXPR)
df0137b9 2666 return tem;
2667 return fold_build1 (NOP_EXPR, type, tem);
b30e3dbc 2668
fdada98f 2669 default:
2670 gcc_unreachable ();
b30e3dbc 2671 }
b30e3dbc 2672}
2bc77e10 2673\f
77aa6362 2674/* Return false if expr can be assumed not to be an lvalue, true
98fc7ffa 2675 otherwise. */
2bc77e10 2676
98fc7ffa 2677static bool
b4b34335 2678maybe_lvalue_p (const_tree x)
2bc77e10 2679{
f4d47aeb 2680 /* We only need to wrap lvalue tree codes. */
2681 switch (TREE_CODE (x))
2682 {
2683 case VAR_DECL:
2684 case PARM_DECL:
2685 case RESULT_DECL:
2686 case LABEL_DECL:
2687 case FUNCTION_DECL:
2688 case SSA_NAME:
2689
2690 case COMPONENT_REF:
2691 case INDIRECT_REF:
b056d812 2692 case ALIGN_INDIRECT_REF:
2693 case MISALIGNED_INDIRECT_REF:
f4d47aeb 2694 case ARRAY_REF:
6374121b 2695 case ARRAY_RANGE_REF:
f4d47aeb 2696 case BIT_FIELD_REF:
215e2f1d 2697 case OBJ_TYPE_REF:
f4d47aeb 2698
2699 case REALPART_EXPR:
2700 case IMAGPART_EXPR:
2701 case PREINCREMENT_EXPR:
2702 case PREDECREMENT_EXPR:
2703 case SAVE_EXPR:
f4d47aeb 2704 case TRY_CATCH_EXPR:
2705 case WITH_CLEANUP_EXPR:
2706 case COMPOUND_EXPR:
2707 case MODIFY_EXPR:
2708 case TARGET_EXPR:
2709 case COND_EXPR:
2710 case BIND_EXPR:
2711 case MIN_EXPR:
2712 case MAX_EXPR:
f4d47aeb 2713 break;
2714
2715 default:
2716 /* Assume the worst for front-end tree codes. */
2717 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2718 break;
98fc7ffa 2719 return false;
f4d47aeb 2720 }
98fc7ffa 2721
2722 return true;
2723}
2724
2725/* Return an expr equal to X but certainly not valid as an lvalue. */
2726
2727tree
2728non_lvalue (tree x)
2729{
2730 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2731 us. */
2732 if (in_gimple_form)
2733 return x;
2734
2735 if (! maybe_lvalue_p (x))
2736 return x;
4ee9c684 2737 return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2bc77e10 2738}
56753054 2739
b12c26dc 2740/* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2741 Zero means allow extended lvalues. */
2742
2743int pedantic_lvalues;
2744
56753054 2745/* When pedantic, return an expr equal to X but certainly not valid as a
2746 pedantic lvalue. Otherwise, return X. */
2747
d50efa49 2748static tree
de1b648b 2749pedantic_non_lvalue (tree x)
56753054 2750{
b12c26dc 2751 if (pedantic_lvalues)
56753054 2752 return non_lvalue (x);
2753 else
2754 return x;
2755}
e233264a 2756\f
2757/* Given a tree comparison code, return the code that is the logical inverse
2758 of the given code. It is not safe to do this for floating-point
318a728f 2759 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2760 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2bc77e10 2761
eea12c72 2762enum tree_code
318a728f 2763invert_tree_comparison (enum tree_code code, bool honor_nans)
e233264a 2764{
318a728f 2765 if (honor_nans && flag_trapping_math)
2766 return ERROR_MARK;
2767
e233264a 2768 switch (code)
2769 {
2770 case EQ_EXPR:
2771 return NE_EXPR;
2772 case NE_EXPR:
2773 return EQ_EXPR;
2774 case GT_EXPR:
318a728f 2775 return honor_nans ? UNLE_EXPR : LE_EXPR;
e233264a 2776 case GE_EXPR:
318a728f 2777 return honor_nans ? UNLT_EXPR : LT_EXPR;
e233264a 2778 case LT_EXPR:
318a728f 2779 return honor_nans ? UNGE_EXPR : GE_EXPR;
e233264a 2780 case LE_EXPR:
318a728f 2781 return honor_nans ? UNGT_EXPR : GT_EXPR;
2782 case LTGT_EXPR:
2783 return UNEQ_EXPR;
2784 case UNEQ_EXPR:
2785 return LTGT_EXPR;
2786 case UNGT_EXPR:
2787 return LE_EXPR;
2788 case UNGE_EXPR:
2789 return LT_EXPR;
2790 case UNLT_EXPR:
2791 return GE_EXPR;
2792 case UNLE_EXPR:
e233264a 2793 return GT_EXPR;
318a728f 2794 case ORDERED_EXPR:
2795 return UNORDERED_EXPR;
2796 case UNORDERED_EXPR:
2797 return ORDERED_EXPR;
e233264a 2798 default:
fdada98f 2799 gcc_unreachable ();
e233264a 2800 }
2801}
2802
2803/* Similar, but return the comparison that results if the operands are
2804 swapped. This is safe for floating-point. */
2805
cc0bdf91 2806enum tree_code
de1b648b 2807swap_tree_comparison (enum tree_code code)
e233264a 2808{
2809 switch (code)
2810 {
2811 case EQ_EXPR:
2812 case NE_EXPR:
6a0aeeaa 2813 case ORDERED_EXPR:
2814 case UNORDERED_EXPR:
2815 case LTGT_EXPR:
2816 case UNEQ_EXPR:
e233264a 2817 return code;
2818 case GT_EXPR:
2819 return LT_EXPR;
2820 case GE_EXPR:
2821 return LE_EXPR;
2822 case LT_EXPR:
2823 return GT_EXPR;
2824 case LE_EXPR:
2825 return GE_EXPR;
6a0aeeaa 2826 case UNGT_EXPR:
2827 return UNLT_EXPR;
2828 case UNGE_EXPR:
2829 return UNLE_EXPR;
2830 case UNLT_EXPR:
2831 return UNGT_EXPR;
2832 case UNLE_EXPR:
2833 return UNGE_EXPR;
e233264a 2834 default:
fdada98f 2835 gcc_unreachable ();
e233264a 2836 }
2837}
8b94828f 2838
7835f163 2839
2840/* Convert a comparison tree code from an enum tree_code representation
2841 into a compcode bit-based encoding. This function is the inverse of
2842 compcode_to_comparison. */
2843
318a728f 2844static enum comparison_code
de1b648b 2845comparison_to_compcode (enum tree_code code)
7835f163 2846{
2847 switch (code)
2848 {
2849 case LT_EXPR:
2850 return COMPCODE_LT;
2851 case EQ_EXPR:
2852 return COMPCODE_EQ;
2853 case LE_EXPR:
2854 return COMPCODE_LE;
2855 case GT_EXPR:
2856 return COMPCODE_GT;
2857 case NE_EXPR:
2858 return COMPCODE_NE;
2859 case GE_EXPR:
2860 return COMPCODE_GE;
318a728f 2861 case ORDERED_EXPR:
2862 return COMPCODE_ORD;
2863 case UNORDERED_EXPR:
2864 return COMPCODE_UNORD;
2865 case UNLT_EXPR:
2866 return COMPCODE_UNLT;
2867 case UNEQ_EXPR:
2868 return COMPCODE_UNEQ;
2869 case UNLE_EXPR:
2870 return COMPCODE_UNLE;
2871 case UNGT_EXPR:
2872 return COMPCODE_UNGT;
2873 case LTGT_EXPR:
2874 return COMPCODE_LTGT;
2875 case UNGE_EXPR:
2876 return COMPCODE_UNGE;
7835f163 2877 default:
fdada98f 2878 gcc_unreachable ();
7835f163 2879 }
2880}
2881
2882/* Convert a compcode bit-based encoding of a comparison operator back
2883 to GCC's enum tree_code representation. This function is the
2884 inverse of comparison_to_compcode. */
2885
2886static enum tree_code
318a728f 2887compcode_to_comparison (enum comparison_code code)
7835f163 2888{
2889 switch (code)
2890 {
2891 case COMPCODE_LT:
2892 return LT_EXPR;
2893 case COMPCODE_EQ:
2894 return EQ_EXPR;
2895 case COMPCODE_LE:
2896 return LE_EXPR;
2897 case COMPCODE_GT:
2898 return GT_EXPR;
2899 case COMPCODE_NE:
2900 return NE_EXPR;
2901 case COMPCODE_GE:
2902 return GE_EXPR;
318a728f 2903 case COMPCODE_ORD:
2904 return ORDERED_EXPR;
2905 case COMPCODE_UNORD:
2906 return UNORDERED_EXPR;
2907 case COMPCODE_UNLT:
2908 return UNLT_EXPR;
2909 case COMPCODE_UNEQ:
2910 return UNEQ_EXPR;
2911 case COMPCODE_UNLE:
2912 return UNLE_EXPR;
2913 case COMPCODE_UNGT:
2914 return UNGT_EXPR;
2915 case COMPCODE_LTGT:
2916 return LTGT_EXPR;
2917 case COMPCODE_UNGE:
2918 return UNGE_EXPR;
7835f163 2919 default:
fdada98f 2920 gcc_unreachable ();
7835f163 2921 }
2922}
2923
318a728f 2924/* Return a tree for the comparison which is the combination of
2925 doing the AND or OR (depending on CODE) of the two operations LCODE
2926 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2927 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2928 if this makes the transformation invalid. */
2929
2930tree
2931combine_comparisons (enum tree_code code, enum tree_code lcode,
2932 enum tree_code rcode, tree truth_type,
2933 tree ll_arg, tree lr_arg)
2934{
2935 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2936 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2937 enum comparison_code rcompcode = comparison_to_compcode (rcode);
8458f4ca 2938 int compcode;
318a728f 2939
2940 switch (code)
2941 {
2942 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2943 compcode = lcompcode & rcompcode;
2944 break;
2945
2946 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2947 compcode = lcompcode | rcompcode;
2948 break;
2949
2950 default:
2951 return NULL_TREE;
2952 }
2953
2954 if (!honor_nans)
2955 {
2956 /* Eliminate unordered comparisons, as well as LTGT and ORD
2957 which are not used unless the mode has NaNs. */
2958 compcode &= ~COMPCODE_UNORD;
2959 if (compcode == COMPCODE_LTGT)
2960 compcode = COMPCODE_NE;
2961 else if (compcode == COMPCODE_ORD)
2962 compcode = COMPCODE_TRUE;
2963 }
2964 else if (flag_trapping_math)
2965 {
7206da1b 2966 /* Check that the original operation and the optimized ones will trap
318a728f 2967 under the same condition. */
2968 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2969 && (lcompcode != COMPCODE_EQ)
2970 && (lcompcode != COMPCODE_ORD);
2971 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2972 && (rcompcode != COMPCODE_EQ)
2973 && (rcompcode != COMPCODE_ORD);
2974 bool trap = (compcode & COMPCODE_UNORD) == 0
2975 && (compcode != COMPCODE_EQ)
2976 && (compcode != COMPCODE_ORD);
2977
2978 /* In a short-circuited boolean expression the LHS might be
2979 such that the RHS, if evaluated, will never trap. For
2980 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2981 if neither x nor y is NaN. (This is a mixed blessing: for
2982 example, the expression above will never trap, hence
2983 optimizing it to x < y would be invalid). */
2984 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2985 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2986 rtrap = false;
2987
2988 /* If the comparison was short-circuited, and only the RHS
2989 trapped, we may now generate a spurious trap. */
2990 if (rtrap && !ltrap
2991 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2992 return NULL_TREE;
2993
2994 /* If we changed the conditions that cause a trap, we lose. */
2995 if ((ltrap || rtrap) != trap)
2996 return NULL_TREE;
2997 }
2998
2999 if (compcode == COMPCODE_TRUE)
20783f07 3000 return constant_boolean_node (true, truth_type);
318a728f 3001 else if (compcode == COMPCODE_FALSE)
20783f07 3002 return constant_boolean_node (false, truth_type);
318a728f 3003 else
8458f4ca 3004 {
3005 enum tree_code tcode;
3006
3007 tcode = compcode_to_comparison ((enum comparison_code) compcode);
3008 return fold_build2 (tcode, truth_type, ll_arg, lr_arg);
3009 }
318a728f 3010}
e233264a 3011\f
9e6f4cc9 3012/* Return nonzero if two operands (typically of the same tree node)
3013 are necessarily equal. If either argument has side-effects this
365db11e 3014 function returns zero. FLAGS modifies behavior as follows:
9e6f4cc9 3015
4ee9c684 3016 If OEP_ONLY_CONST is set, only return nonzero for constants.
11acc1df 3017 This function tests whether the operands are indistinguishable;
3018 it does not test whether they are equal using C's == operation.
3019 The distinction is important for IEEE floating point, because
3020 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
9e6f4cc9 3021 (2) two NaNs may be indistinguishable, but NaN!=NaN.
3022
4ee9c684 3023 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
9e6f4cc9 3024 even though it may hold multiple values during a function.
3025 This is because a GCC tree node guarantees that nothing else is
3026 executed between the evaluation of its "operands" (which may often
3027 be evaluated in arbitrary order). Hence if the operands themselves
3028 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
9b931277 3029 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3030 unset means assuming isochronic (or instantaneous) tree equivalence.
3031 Unless comparing arbitrary expression trees, such as from different
3032 statements, this flag can usually be left unset.
4ee9c684 3033
3034 If OEP_PURE_SAME is set, then pure functions with identical arguments
3035 are considered the same. It is used when the caller has other ways
3036 to ensure that global memory is unchanged in between. */
2bc77e10 3037
3038int
b7bf20db 3039operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2bc77e10 3040{
78a8ed03 3041 /* If either is ERROR_MARK, they aren't equal. */
fa31fec1 3042 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
78a8ed03 3043 return 0;
3044
659ce413 3045 /* Check equality of integer constants before bailing out due to
3046 precision differences. */
3047 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3048 return tree_int_cst_equal (arg0, arg1);
3049
2bc77e10 3050 /* If both types don't have the same signedness, then we can't consider
3051 them equal. We must check this before the STRIP_NOPS calls
07018da0 3052 because they may change the signedness of the arguments. As pointers
3053 strictly don't have a signedness, require either two pointers or
3054 two non-pointers as well. */
3055 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3056 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2bc77e10 3057 return 0;
3058
68826b15 3059 /* If both types don't have the same precision, then it is not safe
3060 to strip NOPs. */
3061 if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3062 return 0;
3063
2bc77e10 3064 STRIP_NOPS (arg0);
3065 STRIP_NOPS (arg1);
3066
ffb99bfe 3067 /* In case both args are comparisons but with different comparison
3068 code, try to swap the comparison operands of one arg to produce
3069 a match and compare that variant. */
3070 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3071 && COMPARISON_CLASS_P (arg0)
3072 && COMPARISON_CLASS_P (arg1))
3073 {
3074 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3075
3076 if (TREE_CODE (arg0) == swap_code)
3077 return operand_equal_p (TREE_OPERAND (arg0, 0),
3078 TREE_OPERAND (arg1, 1), flags)
3079 && operand_equal_p (TREE_OPERAND (arg0, 1),
3080 TREE_OPERAND (arg1, 0), flags);
3081 }
3082
8faaadf1 3083 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3084 /* This is needed for conversions and for COMPONENT_REF.
3085 Might as well play it safe and always test this. */
6a4737bf 3086 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3087 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
8faaadf1 3088 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2bc77e10 3089 return 0;
3090
8faaadf1 3091 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3092 We don't care about side effects in that case because the SAVE_EXPR
3093 takes care of that for us. In all other cases, two expressions are
3094 equal if they have no side effects. If we have two identical
3095 expressions with side effects that should be treated the same due
3096 to the only side effects being identical SAVE_EXPR's, that will
3097 be detected in the recursive calls below. */
4ee9c684 3098 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
8faaadf1 3099 && (TREE_CODE (arg0) == SAVE_EXPR
3100 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2bc77e10 3101 return 1;
3102
8faaadf1 3103 /* Next handle constant cases, those for which we can return 1 even
3104 if ONLY_CONST is set. */
3105 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3106 switch (TREE_CODE (arg0))
3107 {
3108 case INTEGER_CST:
bdb135dc 3109 return tree_int_cst_equal (arg0, arg1);
8faaadf1 3110
06f0b99c 3111 case FIXED_CST:
3112 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3113 TREE_FIXED_CST (arg1));
3114
8faaadf1 3115 case REAL_CST:
90b56f40 3116 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3117 TREE_REAL_CST (arg1)))
3118 return 1;
3119
3120
3121 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3122 {
3123 /* If we do not distinguish between signed and unsigned zero,
3124 consider them equal. */
3125 if (real_zerop (arg0) && real_zerop (arg1))
3126 return 1;
3127 }
3128 return 0;
8faaadf1 3129
886cfd4f 3130 case VECTOR_CST:
3131 {
3132 tree v1, v2;
3133
886cfd4f 3134 v1 = TREE_VECTOR_CST_ELTS (arg0);
3135 v2 = TREE_VECTOR_CST_ELTS (arg1);
3136 while (v1 && v2)
3137 {
11cb6006 3138 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
4ee9c684 3139 flags))
886cfd4f 3140 return 0;
3141 v1 = TREE_CHAIN (v1);
3142 v2 = TREE_CHAIN (v2);
3143 }
3144
6349b545 3145 return v1 == v2;
886cfd4f 3146 }
3147
8faaadf1 3148 case COMPLEX_CST:
3149 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
4ee9c684 3150 flags)
8faaadf1 3151 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
4ee9c684 3152 flags));
8faaadf1 3153
3154 case STRING_CST:
3155 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
6b918462 3156 && ! memcmp (TREE_STRING_POINTER (arg0),
8faaadf1 3157 TREE_STRING_POINTER (arg1),
3158 TREE_STRING_LENGTH (arg0)));
3159
3160 case ADDR_EXPR:
3161 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3162 0);
0dbd1c74 3163 default:
3164 break;
8faaadf1 3165 }
2bc77e10 3166
4ee9c684 3167 if (flags & OEP_ONLY_CONST)
2bc77e10 3168 return 0;
3169
66bab57a 3170/* Define macros to test an operand from arg0 and arg1 for equality and a
fa31fec1 3171 variant that allows null and views null as being different from any
3172 non-null value. In the latter case, if either is null, the both
3173 must be; otherwise, do the normal comparison. */
3174#define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3175 TREE_OPERAND (arg1, N), flags)
3176
3177#define OP_SAME_WITH_NULL(N) \
3178 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3179 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3180
2bc77e10 3181 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3182 {
ce45a448 3183 case tcc_unary:
2bc77e10 3184 /* Two conversions are equal only if signedness and modes match. */
e6546627 3185 switch (TREE_CODE (arg0))
3186 {
72dd6141 3187 CASE_CONVERT:
e6546627 3188 case FIX_TRUNC_EXPR:
e6546627 3189 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3190 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3191 return 0;
3192 break;
3193 default:
3194 break;
3195 }
2bc77e10 3196
fa31fec1 3197 return OP_SAME (0);
3198
2bc77e10 3199
ce45a448 3200 case tcc_comparison:
3201 case tcc_binary:
fa31fec1 3202 if (OP_SAME (0) && OP_SAME (1))
8faaadf1 3203 return 1;
3204
3205 /* For commutative ops, allow the other order. */
21dff555 3206 return (commutative_tree_code (TREE_CODE (arg0))
8faaadf1 3207 && operand_equal_p (TREE_OPERAND (arg0, 0),
4ee9c684 3208 TREE_OPERAND (arg1, 1), flags)
2bc77e10 3209 && operand_equal_p (TREE_OPERAND (arg0, 1),
4ee9c684 3210 TREE_OPERAND (arg1, 0), flags));
2bc77e10 3211
ce45a448 3212 case tcc_reference:
06506f5d 3213 /* If either of the pointer (or reference) expressions we are
3214 dereferencing contain a side effect, these cannot be equal. */
dbc71562 3215 if (TREE_SIDE_EFFECTS (arg0)
3216 || TREE_SIDE_EFFECTS (arg1))
3217 return 0;
3218
2bc77e10 3219 switch (TREE_CODE (arg0))
3220 {
3221 case INDIRECT_REF:
b056d812 3222 case ALIGN_INDIRECT_REF:
3223 case MISALIGNED_INDIRECT_REF:
b25de375 3224 case REALPART_EXPR:
3225 case IMAGPART_EXPR:
fa31fec1 3226 return OP_SAME (0);
2bc77e10 3227
2bc77e10 3228 case ARRAY_REF:
ba04d9d5 3229 case ARRAY_RANGE_REF:
a2501610 3230 /* Operands 2 and 3 may be null.
3231 Compare the array index by value if it is constant first as we
3232 may have different types but same value here. */
fa31fec1 3233 return (OP_SAME (0)
a2501610 3234 && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3235 TREE_OPERAND (arg1, 1))
3236 || OP_SAME (1))
fa31fec1 3237 && OP_SAME_WITH_NULL (2)
3238 && OP_SAME_WITH_NULL (3));
6ab43650 3239
3240 case COMPONENT_REF:
2f16183e 3241 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3242 may be NULL when we're called to compare MEM_EXPRs. */
3243 return OP_SAME_WITH_NULL (0)
3244 && OP_SAME (1)
3245 && OP_SAME_WITH_NULL (2);
8d061c60 3246
e715d92e 3247 case BIT_FIELD_REF:
fa31fec1 3248 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3249
0dbd1c74 3250 default:
3251 return 0;
2bc77e10 3252 }
1d322a97 3253
ce45a448 3254 case tcc_expression:
564989a5 3255 switch (TREE_CODE (arg0))
3256 {
3257 case ADDR_EXPR:
3258 case TRUTH_NOT_EXPR:
fa31fec1 3259 return OP_SAME (0);
564989a5 3260
bd975dc2 3261 case TRUTH_ANDIF_EXPR:
3262 case TRUTH_ORIF_EXPR:
fa31fec1 3263 return OP_SAME (0) && OP_SAME (1);
bd975dc2 3264
3265 case TRUTH_AND_EXPR:
3266 case TRUTH_OR_EXPR:
3267 case TRUTH_XOR_EXPR:
fa31fec1 3268 if (OP_SAME (0) && OP_SAME (1))
3269 return 1;
3270
3271 /* Otherwise take into account this is a commutative operation. */
bd975dc2 3272 return (operand_equal_p (TREE_OPERAND (arg0, 0),
fa31fec1 3273 TREE_OPERAND (arg1, 1), flags)
bd975dc2 3274 && operand_equal_p (TREE_OPERAND (arg0, 1),
fa31fec1 3275 TREE_OPERAND (arg1, 0), flags));
bd975dc2 3276
c319d56a 3277 case COND_EXPR:
3278 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3279
c2f47e15 3280 default:
3281 return 0;
3282 }
3283
3284 case tcc_vl_exp:
3285 switch (TREE_CODE (arg0))
3286 {
06506f5d 3287 case CALL_EXPR:
3288 /* If the CALL_EXPRs call different functions, then they
3289 clearly can not be equal. */
c2f47e15 3290 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3291 flags))
06506f5d 3292 return 0;
3293
4ee9c684 3294 {
3295 unsigned int cef = call_expr_flags (arg0);
3296 if (flags & OEP_PURE_SAME)
3297 cef &= ECF_CONST | ECF_PURE;
3298 else
3299 cef &= ECF_CONST;
3300 if (!cef)
3301 return 0;
3302 }
06506f5d 3303
c2f47e15 3304 /* Now see if all the arguments are the same. */
3305 {
b7bf20db 3306 const_call_expr_arg_iterator iter0, iter1;
3307 const_tree a0, a1;
3308 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3309 a1 = first_const_call_expr_arg (arg1, &iter1);
c2f47e15 3310 a0 && a1;
b7bf20db 3311 a0 = next_const_call_expr_arg (&iter0),
3312 a1 = next_const_call_expr_arg (&iter1))
c2f47e15 3313 if (! operand_equal_p (a0, a1, flags))
06506f5d 3314 return 0;
3315
c2f47e15 3316 /* If we get here and both argument lists are exhausted
3317 then the CALL_EXPRs are equal. */
3318 return ! (a0 || a1);
3319 }
564989a5 3320 default:
3321 return 0;
3322 }
cc049fa3 3323
ce45a448 3324 case tcc_declaration:
4ee9c684 3325 /* Consider __builtin_sqrt equal to sqrt. */
3326 return (TREE_CODE (arg0) == FUNCTION_DECL
3327 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3328 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3329 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
06506f5d 3330
0dbd1c74 3331 default:
3332 return 0;
2bc77e10 3333 }
fa31fec1 3334
3335#undef OP_SAME
3336#undef OP_SAME_WITH_NULL
2bc77e10 3337}
e233264a 3338\f
3339/* Similar to operand_equal_p, but see if ARG0 might have been made by
cc049fa3 3340 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2bc77e10 3341
2bc77e10 3342 When in doubt, return 0. */
3343
cc049fa3 3344static int
de1b648b 3345operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2bc77e10 3346{
e233264a 3347 int unsignedp1, unsignedpo;
df7caa7b 3348 tree primarg0, primarg1, primother;
02e7a332 3349 unsigned int correct_width;
2bc77e10 3350
e233264a 3351 if (operand_equal_p (arg0, arg1, 0))
2bc77e10 3352 return 1;
3353
154e6f12 3354 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3355 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2bc77e10 3356 return 0;
3357
df7caa7b 3358 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3359 and see if the inner values are the same. This removes any
3360 signedness comparison, which doesn't matter here. */
3361 primarg0 = arg0, primarg1 = arg1;
cc049fa3 3362 STRIP_NOPS (primarg0);
3363 STRIP_NOPS (primarg1);
df7caa7b 3364 if (operand_equal_p (primarg0, primarg1, 0))
3365 return 1;
3366
e233264a 3367 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3368 actual comparison operand, ARG0.
2bc77e10 3369
e233264a 3370 First throw away any conversions to wider types
2bc77e10 3371 already present in the operands. */
2bc77e10 3372
e233264a 3373 primarg1 = get_narrower (arg1, &unsignedp1);
3374 primother = get_narrower (other, &unsignedpo);
3375
3376 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3377 if (unsignedp1 == unsignedpo
3378 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3379 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2bc77e10 3380 {
e233264a 3381 tree type = TREE_TYPE (arg0);
2bc77e10 3382
3383 /* Make sure shorter operand is extended the right way
3384 to match the longer operand. */
11773141 3385 primarg1 = fold_convert (signed_or_unsigned_type_for
b30e3dbc 3386 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2bc77e10 3387
b30e3dbc 3388 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2bc77e10 3389 return 1;
3390 }
3391
3392 return 0;
3393}
3394\f
eb2f80f3 3395/* See if ARG is an expression that is either a comparison or is performing
e233264a 3396 arithmetic on comparisons. The comparisons must only be comparing
3397 two different values, which will be stored in *CVAL1 and *CVAL2; if
6ef828f9 3398 they are nonzero it means that some operands have already been found.
e233264a 3399 No variables may be used anywhere else in the expression except in the
d0314131 3400 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3401 the expression and save_expr needs to be called with CVAL1 and CVAL2.
e233264a 3402
3403 If this is true, return 1. Otherwise, return zero. */
3404
3405static int
de1b648b 3406twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
e233264a 3407{
3408 enum tree_code code = TREE_CODE (arg);
f4e36c33 3409 enum tree_code_class tclass = TREE_CODE_CLASS (code);
e233264a 3410
ce45a448 3411 /* We can handle some of the tcc_expression cases here. */
f4e36c33 3412 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3413 tclass = tcc_unary;
3414 else if (tclass == tcc_expression
e233264a 3415 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3416 || code == COMPOUND_EXPR))
f4e36c33 3417 tclass = tcc_binary;
8be91fe5 3418
f4e36c33 3419 else if (tclass == tcc_expression && code == SAVE_EXPR
083a2b5e 3420 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
d0314131 3421 {
3422 /* If we've already found a CVAL1 or CVAL2, this expression is
3423 two complex to handle. */
3424 if (*cval1 || *cval2)
3425 return 0;
3426
f4e36c33 3427 tclass = tcc_unary;
d0314131 3428 *save_p = 1;
3429 }
e233264a 3430
f4e36c33 3431 switch (tclass)
e233264a 3432 {
ce45a448 3433 case tcc_unary:
d0314131 3434 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
e233264a 3435
ce45a448 3436 case tcc_binary:
d0314131 3437 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3438 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3439 cval1, cval2, save_p));
e233264a 3440
ce45a448 3441 case tcc_constant:
e233264a 3442 return 1;
3443
ce45a448 3444 case tcc_expression:
e233264a 3445 if (code == COND_EXPR)
d0314131 3446 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3447 cval1, cval2, save_p)
3448 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3449 cval1, cval2, save_p)
e233264a 3450 && twoval_comparison_p (TREE_OPERAND (arg, 2),
d0314131 3451 cval1, cval2, save_p));
e233264a 3452 return 0;
cc049fa3 3453
ce45a448 3454 case tcc_comparison:
e233264a 3455 /* First see if we can handle the first operand, then the second. For
3456 the second operand, we know *CVAL1 can't be zero. It must be that
3457 one side of the comparison is each of the values; test for the
3458 case where this isn't true by failing if the two operands
3459 are the same. */
3460
3461 if (operand_equal_p (TREE_OPERAND (arg, 0),
3462 TREE_OPERAND (arg, 1), 0))
3463 return 0;
3464
3465 if (*cval1 == 0)
3466 *cval1 = TREE_OPERAND (arg, 0);
3467 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3468 ;
3469 else if (*cval2 == 0)
3470 *cval2 = TREE_OPERAND (arg, 0);
3471 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3472 ;
3473 else
3474 return 0;
3475
3476 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3477 ;
3478 else if (*cval2 == 0)
3479 *cval2 = TREE_OPERAND (arg, 1);
3480 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3481 ;
3482 else
3483 return 0;
3484
3485 return 1;
e233264a 3486
0dbd1c74 3487 default:
3488 return 0;
3489 }
e233264a 3490}
3491\f
3492/* ARG is a tree that is known to contain just arithmetic operations and
3493 comparisons. Evaluate the operations in the tree substituting NEW0 for
eb2f80f3 3494 any occurrence of OLD0 as an operand of a comparison and likewise for
e233264a 3495 NEW1 and OLD1. */
3496
3497static tree
de1b648b 3498eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
e233264a 3499{
3500 tree type = TREE_TYPE (arg);
3501 enum tree_code code = TREE_CODE (arg);
f4e36c33 3502 enum tree_code_class tclass = TREE_CODE_CLASS (code);
e233264a 3503
ce45a448 3504 /* We can handle some of the tcc_expression cases here. */
f4e36c33 3505 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3506 tclass = tcc_unary;
3507 else if (tclass == tcc_expression
e233264a 3508 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
f4e36c33 3509 tclass = tcc_binary;
e233264a 3510
f4e36c33 3511 switch (tclass)
e233264a 3512 {
ce45a448 3513 case tcc_unary:
7ab7fd4f 3514 return fold_build1 (code, type,
3515 eval_subst (TREE_OPERAND (arg, 0),
3516 old0, new0, old1, new1));
e233264a 3517
ce45a448 3518 case tcc_binary:
7ab7fd4f 3519 return fold_build2 (code, type,
3520 eval_subst (TREE_OPERAND (arg, 0),
3521 old0, new0, old1, new1),
3522 eval_subst (TREE_OPERAND (arg, 1),
3523 old0, new0, old1, new1));
e233264a 3524
ce45a448 3525 case tcc_expression:
e233264a 3526 switch (code)
3527 {
3528 case SAVE_EXPR:
3529 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3530
3531 case COMPOUND_EXPR:
3532 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3533
3534 case COND_EXPR:
7ab7fd4f 3535 return fold_build3 (code, type,
3536 eval_subst (TREE_OPERAND (arg, 0),
3537 old0, new0, old1, new1),
3538 eval_subst (TREE_OPERAND (arg, 1),
3539 old0, new0, old1, new1),
3540 eval_subst (TREE_OPERAND (arg, 2),
3541 old0, new0, old1, new1));
0dbd1c74 3542 default:
3543 break;
e233264a 3544 }
b4b174c3 3545 /* Fall through - ??? */
e233264a 3546
ce45a448 3547 case tcc_comparison:
e233264a 3548 {
3549 tree arg0 = TREE_OPERAND (arg, 0);
3550 tree arg1 = TREE_OPERAND (arg, 1);
3551
3552 /* We need to check both for exact equality and tree equality. The
3553 former will be true if the operand has a side-effect. In that
3554 case, we know the operand occurred exactly once. */
3555
3556 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3557 arg0 = new0;
3558 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3559 arg0 = new1;
3560
3561 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3562 arg1 = new0;
3563 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3564 arg1 = new1;
3565
7ab7fd4f 3566 return fold_build2 (code, type, arg0, arg1);
e233264a 3567 }
e233264a 3568
0dbd1c74 3569 default:
3570 return arg;
3571 }
e233264a 3572}
3573\f
2bc77e10 3574/* Return a tree for the case when the result of an expression is RESULT
3575 converted to TYPE and OMITTED was previously an operand of the expression
3576 but is now not needed (e.g., we folded OMITTED * 0).
3577
3578 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3579 the conversion of RESULT to TYPE. */
3580
e9f80ff5 3581tree
de1b648b 3582omit_one_operand (tree type, tree result, tree omitted)
2bc77e10 3583{
b30e3dbc 3584 tree t = fold_convert (type, result);
2bc77e10 3585
becfaa62 3586 /* If the resulting operand is an empty statement, just return the omitted
9e0e518b 3587 statement casted to void. */
3588 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3589 return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3590
2bc77e10 3591 if (TREE_SIDE_EFFECTS (omitted))
db97ad41 3592 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
2bc77e10 3593
c3ce5d04 3594 return non_lvalue (t);
2bc77e10 3595}
6df5edfa 3596
3597/* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
3598
3599static tree
de1b648b 3600pedantic_omit_one_operand (tree type, tree result, tree omitted)
6df5edfa 3601{
b30e3dbc 3602 tree t = fold_convert (type, result);
6df5edfa 3603
becfaa62 3604 /* If the resulting operand is an empty statement, just return the omitted
9e0e518b 3605 statement casted to void. */
3606 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3607 return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3608
6df5edfa 3609 if (TREE_SIDE_EFFECTS (omitted))
db97ad41 3610 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
6df5edfa 3611
3612 return pedantic_non_lvalue (t);
3613}
9bc9f15f 3614
3615/* Return a tree for the case when the result of an expression is RESULT
3616 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3617 of the expression but are now not needed.
3618
3619 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3620 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3621 evaluated before OMITTED2. Otherwise, if neither has side effects,
3622 just do the conversion of RESULT to TYPE. */
3623
3624tree
3625omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3626{
3627 tree t = fold_convert (type, result);
3628
3629 if (TREE_SIDE_EFFECTS (omitted2))
3630 t = build2 (COMPOUND_EXPR, type, omitted2, t);
3631 if (TREE_SIDE_EFFECTS (omitted1))
3632 t = build2 (COMPOUND_EXPR, type, omitted1, t);
3633
3634 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3635}
3636
2bc77e10 3637\f
46b0e007 3638/* Return a simplified tree node for the truth-negation of ARG. This
3639 never alters ARG itself. We assume that ARG is an operation that
318a728f 3640 returns a truth value (0 or 1).
2bc77e10 3641
318a728f 3642 FIXME: one would think we would fold the result, but it causes
3643 problems with the dominator optimizer. */
6758b11c 3644
2bc77e10 3645tree
6758b11c 3646fold_truth_not_expr (tree arg)
2bc77e10 3647{
43158006 3648 tree t, type = TREE_TYPE (arg);
e233264a 3649 enum tree_code code = TREE_CODE (arg);
2bc77e10 3650
e233264a 3651 /* If this is a comparison, we can simply invert it, except for
3652 floating-point non-equality comparisons, in which case we just
3653 enclose a TRUTH_NOT_EXPR around what we have. */
2bc77e10 3654
ce45a448 3655 if (TREE_CODE_CLASS (code) == tcc_comparison)
2bc77e10 3656 {
318a728f 3657 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3658 if (FLOAT_TYPE_P (op_type)
3659 && flag_trapping_math
3660 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3661 && code != NE_EXPR && code != EQ_EXPR)
6758b11c 3662 return NULL_TREE;
43158006 3663
3664 code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3665 if (code == ERROR_MARK)
3666 return NULL_TREE;
3667
3668 t = build2 (code, type, TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3669 if (EXPR_HAS_LOCATION (arg))
3670 SET_EXPR_LOCATION (t, EXPR_LOCATION (arg));
3671 return t;
e233264a 3672 }
2bc77e10 3673
e233264a 3674 switch (code)
3675 {
2bc77e10 3676 case INTEGER_CST:
b7f352d5 3677 return constant_boolean_node (integer_zerop (arg), type);
2bc77e10 3678
3679 case TRUTH_AND_EXPR:
43158006 3680 t = build2 (TRUTH_OR_EXPR, type,
3681 invert_truthvalue (TREE_OPERAND (arg, 0)),
3682 invert_truthvalue (TREE_OPERAND (arg, 1)));
3683 break;
2bc77e10 3684
3685 case TRUTH_OR_EXPR:
43158006 3686 t = build2 (TRUTH_AND_EXPR, type,
3687 invert_truthvalue (TREE_OPERAND (arg, 0)),
3688 invert_truthvalue (TREE_OPERAND (arg, 1)));
3689 break;
2bc77e10 3690
9a7b73a1 3691 case TRUTH_XOR_EXPR:
3692 /* Here we can invert either operand. We invert the first operand
3693 unless the second operand is a TRUTH_NOT_EXPR in which case our
3694 result is the XOR of the first operand with the inside of the
3695 negation of the second operand. */
3696
3697 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
43158006 3698 t = build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3699 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
9a7b73a1 3700 else
43158006 3701 t = build2 (TRUTH_XOR_EXPR, type,
3702 invert_truthvalue (TREE_OPERAND (arg, 0)),
3703 TREE_OPERAND (arg, 1));
3704 break;
9a7b73a1 3705
2bc77e10 3706 case TRUTH_ANDIF_EXPR:
43158006 3707 t = build2 (TRUTH_ORIF_EXPR, type,
3708 invert_truthvalue (TREE_OPERAND (arg, 0)),
3709 invert_truthvalue (TREE_OPERAND (arg, 1)));
3710 break;
2bc77e10 3711
3712 case TRUTH_ORIF_EXPR:
43158006 3713 t = build2 (TRUTH_ANDIF_EXPR, type,
3714 invert_truthvalue (TREE_OPERAND (arg, 0)),
3715 invert_truthvalue (TREE_OPERAND (arg, 1)));
3716 break;
2bc77e10 3717
3718 case TRUTH_NOT_EXPR:
3719 return TREE_OPERAND (arg, 0);
3720
3721 case COND_EXPR:
76ce1401 3722 {
3723 tree arg1 = TREE_OPERAND (arg, 1);
3724 tree arg2 = TREE_OPERAND (arg, 2);
3725 /* A COND_EXPR may have a throw as one operand, which
3726 then has void type. Just leave void operands
3727 as they are. */
43158006 3728 t = build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3729 VOID_TYPE_P (TREE_TYPE (arg1))
3730 ? arg1 : invert_truthvalue (arg1),
3731 VOID_TYPE_P (TREE_TYPE (arg2))
3732 ? arg2 : invert_truthvalue (arg2));
3733 break;
76ce1401 3734 }
2bc77e10 3735
3139f3ce 3736 case COMPOUND_EXPR:
43158006 3737 t = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3738 invert_truthvalue (TREE_OPERAND (arg, 1)));
3739 break;
3139f3ce 3740
2bc77e10 3741 case NON_LVALUE_EXPR:
3742 return invert_truthvalue (TREE_OPERAND (arg, 0));
3743
a9538d68 3744 CASE_CONVERT:
4ee9c684 3745 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
43158006 3746 {
3747 t = build1 (TRUTH_NOT_EXPR, type, arg);
3748 break;
3749 }
3750
3751 /* ... fall through ... */
4ee9c684 3752
2bc77e10 3753 case FLOAT_EXPR:
43158006 3754 t = build1 (TREE_CODE (arg), type,
3755 invert_truthvalue (TREE_OPERAND (arg, 0)));
3756 break;
2bc77e10 3757
3758 case BIT_AND_EXPR:
c35387e1 3759 if (!integer_onep (TREE_OPERAND (arg, 1)))
43158006 3760 return NULL_TREE;
3761 t = build2 (EQ_EXPR, type, arg, build_int_cst (type, 0));
3762 break;
2bc77e10 3763
468d693c 3764 case SAVE_EXPR:
43158006 3765 t = build1 (TRUTH_NOT_EXPR, type, arg);
3766 break;
f33c3a83 3767
3768 case CLEANUP_POINT_EXPR:
43158006 3769 t = build1 (CLEANUP_POINT_EXPR, type,
3770 invert_truthvalue (TREE_OPERAND (arg, 0)));
3771 break;
0dbd1c74 3772
3773 default:
43158006 3774 t = NULL_TREE;
0dbd1c74 3775 break;
c35387e1 3776 }
6758b11c 3777
43158006 3778 if (t && EXPR_HAS_LOCATION (arg))
3779 SET_EXPR_LOCATION (t, EXPR_LOCATION (arg));
3780
3781 return t;
6758b11c 3782}
3783
3784/* Return a simplified tree node for the truth-negation of ARG. This
3785 never alters ARG itself. We assume that ARG is an operation that
3786 returns a truth value (0 or 1).
3787
3788 FIXME: one would think we would fold the result, but it causes
3789 problems with the dominator optimizer. */
3790
3791tree
3792invert_truthvalue (tree arg)
3793{
3794 tree tem;
3795
3796 if (TREE_CODE (arg) == ERROR_MARK)
3797 return arg;
3798
3799 tem = fold_truth_not_expr (arg);
3800 if (!tem)
3801 tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3802
3803 return tem;
2bc77e10 3804}
3805
3806/* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3807 operands are another bit-wise operation with a common input. If so,
3808 distribute the bit operations to save an operation and possibly two if
3809 constants are involved. For example, convert
de1b648b 3810 (A | B) & (A | C) into A | (B & C)
2bc77e10 3811 Further simplification will occur if B and C are constants.
3812
3813 If this optimization cannot be done, 0 will be returned. */
3814
3815static tree
de1b648b 3816distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
2bc77e10 3817{
3818 tree common;
3819 tree left, right;
3820
3821 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3822 || TREE_CODE (arg0) == code
5b1de181 3823 || (TREE_CODE (arg0) != BIT_AND_EXPR
3824 && TREE_CODE (arg0) != BIT_IOR_EXPR))
2bc77e10 3825 return 0;
3826
3827 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3828 {
3829 common = TREE_OPERAND (arg0, 0);
3830 left = TREE_OPERAND (arg0, 1);
3831 right = TREE_OPERAND (arg1, 1);
3832 }
3833 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3834 {
3835 common = TREE_OPERAND (arg0, 0);
3836 left = TREE_OPERAND (arg0, 1);
3837 right = TREE_OPERAND (arg1, 0);
3838 }
3839 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3840 {
3841 common = TREE_OPERAND (arg0, 1);
3842 left = TREE_OPERAND (arg0, 0);
3843 right = TREE_OPERAND (arg1, 1);
3844 }
3845 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3846 {
3847 common = TREE_OPERAND (arg0, 1);
3848 left = TREE_OPERAND (arg0, 0);
3849 right = TREE_OPERAND (arg1, 0);
3850 }
3851 else
3852 return 0;
3853
89a45e5a 3854 common = fold_convert (type, common);
3855 left = fold_convert (type, left);
3856 right = fold_convert (type, right);
7ab7fd4f 3857 return fold_build2 (TREE_CODE (arg0), type, common,
3858 fold_build2 (code, type, left, right));
2bc77e10 3859}
429f2f90 3860
3861/* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3862 with code CODE. This optimization is unsafe. */
3863static tree
3864distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3865{
3866 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3867 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3868
3869 /* (A / C) +- (B / C) -> (A +- B) / C. */
3870 if (mul0 == mul1
3871 && operand_equal_p (TREE_OPERAND (arg0, 1),
3872 TREE_OPERAND (arg1, 1), 0))
3873 return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3874 fold_build2 (code, type,
3875 TREE_OPERAND (arg0, 0),
3876 TREE_OPERAND (arg1, 0)),
3877 TREE_OPERAND (arg0, 1));
3878
3879 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3880 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3881 TREE_OPERAND (arg1, 0), 0)
3882 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3883 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3884 {
3885 REAL_VALUE_TYPE r0, r1;
3886 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3887 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3888 if (!mul0)
3889 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3890 if (!mul1)
3891 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3892 real_arithmetic (&r0, code, &r0, &r1);
3893 return fold_build2 (MULT_EXPR, type,
3894 TREE_OPERAND (arg0, 0),
3895 build_real (type, r0));
3896 }
3897
3898 return NULL_TREE;
3899}
2bc77e10 3900\f
2a64c730 3901/* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3902 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3903
3904static tree
3905make_bit_field_ref (tree inner, tree type, HOST_WIDE_INT bitsize,
3906 HOST_WIDE_INT bitpos, int unsignedp)
3907{
3908 tree result, bftype;
3909
3910 if (bitpos == 0)
3911 {
3912 tree size = TYPE_SIZE (TREE_TYPE (inner));
3913 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3914 || POINTER_TYPE_P (TREE_TYPE (inner)))
3915 && host_integerp (size, 0)
3916 && tree_low_cst (size, 0) == bitsize)
3917 return fold_convert (type, inner);
3918 }
3919
3920 bftype = type;
3921 if (TYPE_PRECISION (bftype) != bitsize
3922 || TYPE_UNSIGNED (bftype) == !unsignedp)
3923 bftype = build_nonstandard_integer_type (bitsize, 0);
3924
3925 result = build3 (BIT_FIELD_REF, bftype, inner,
3926 size_int (bitsize), bitsize_int (bitpos));
3927
3928 if (bftype != type)
3929 result = fold_convert (type, result);
3930
3931 return result;
3932}
3933
3934/* Optimize a bit-field compare.
3935
3936 There are two cases: First is a compare against a constant and the
3937 second is a comparison of two items where the fields are at the same
3938 bit position relative to the start of a chunk (byte, halfword, word)
3939 large enough to contain it. In these cases we can avoid the shift
3940 implicit in bitfield extractions.
3941
3942 For constants, we emit a compare of the shifted constant with the
3943 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3944 compared. For two fields at the same position, we do the ANDs with the
3945 similar mask and compare the result of the ANDs.
3946
3947 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3948 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3949 are the left and right operands of the comparison, respectively.
3950
3951 If the optimization described above can be done, we return the resulting
3952 tree. Otherwise we return zero. */
3953
3954static tree
3955optimize_bit_field_compare (enum tree_code code, tree compare_type,
3956 tree lhs, tree rhs)
3957{
3958 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3959 tree type = TREE_TYPE (lhs);
3960 tree signed_type, unsigned_type;
3961 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3962 enum machine_mode lmode, rmode, nmode;
3963 int lunsignedp, runsignedp;
3964 int lvolatilep = 0, rvolatilep = 0;
3965 tree linner, rinner = NULL_TREE;
3966 tree mask;
3967 tree offset;
3968
3969 /* Get all the information about the extractions being done. If the bit size
3970 if the same as the size of the underlying object, we aren't doing an
3971 extraction at all and so can do nothing. We also don't want to
3972 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3973 then will no longer be able to replace it. */
3974 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3975 &lunsignedp, &lvolatilep, false);
3976 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3977 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3978 return 0;
3979
3980 if (!const_p)
3981 {
3982 /* If this is not a constant, we can only do something if bit positions,
3983 sizes, and signedness are the same. */
3984 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3985 &runsignedp, &rvolatilep, false);
3986
3987 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3988 || lunsignedp != runsignedp || offset != 0
3989 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3990 return 0;
3991 }
3992
3993 /* See if we can find a mode to refer to this field. We should be able to,
3994 but fail if we can't. */
3995 nmode = get_best_mode (lbitsize, lbitpos,
3996 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3997 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3998 TYPE_ALIGN (TREE_TYPE (rinner))),
3999 word_mode, lvolatilep || rvolatilep);
4000 if (nmode == VOIDmode)
4001 return 0;
4002
4003 /* Set signed and unsigned types of the precision of this mode for the
4004 shifts below. */
4005 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
4006 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4007
4008 /* Compute the bit position and size for the new reference and our offset
4009 within it. If the new reference is the same size as the original, we
4010 won't optimize anything, so return zero. */
4011 nbitsize = GET_MODE_BITSIZE (nmode);
4012 nbitpos = lbitpos & ~ (nbitsize - 1);
4013 lbitpos -= nbitpos;
4014 if (nbitsize == lbitsize)
4015 return 0;
4016
4017 if (BYTES_BIG_ENDIAN)
4018 lbitpos = nbitsize - lbitsize - lbitpos;
4019
4020 /* Make the mask to be used against the extracted field. */
4021 mask = build_int_cst_type (unsigned_type, -1);
4022 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
4023 mask = const_binop (RSHIFT_EXPR, mask,
4024 size_int (nbitsize - lbitsize - lbitpos), 0);
4025
4026 if (! const_p)
4027 /* If not comparing with constant, just rework the comparison
4028 and return. */
4029 return fold_build2 (code, compare_type,
4030 fold_build2 (BIT_AND_EXPR, unsigned_type,
4031 make_bit_field_ref (linner,
4032 unsigned_type,
4033 nbitsize, nbitpos,
4034 1),
4035 mask),
4036 fold_build2 (BIT_AND_EXPR, unsigned_type,
4037 make_bit_field_ref (rinner,
4038 unsigned_type,
4039 nbitsize, nbitpos,
4040 1),
4041 mask));
4042
4043 /* Otherwise, we are handling the constant case. See if the constant is too
4044 big for the field. Warn and return a tree of for 0 (false) if so. We do
4045 this not only for its own sake, but to avoid having to test for this
4046 error case below. If we didn't, we might generate wrong code.
4047
4048 For unsigned fields, the constant shifted right by the field length should
4049 be all zero. For signed fields, the high-order bits should agree with
4050 the sign bit. */
4051
4052 if (lunsignedp)
4053 {
4054 if (! integer_zerop (const_binop (RSHIFT_EXPR,
4055 fold_convert (unsigned_type, rhs),
4056 size_int (lbitsize), 0)))
4057 {
4058 warning (0, "comparison is always %d due to width of bit-field",
4059 code == NE_EXPR);
4060 return constant_boolean_node (code == NE_EXPR, compare_type);
4061 }
4062 }
4063 else
4064 {
4065 tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
4066 size_int (lbitsize - 1), 0);
4067 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
4068 {
4069 warning (0, "comparison is always %d due to width of bit-field",
4070 code == NE_EXPR);
4071 return constant_boolean_node (code == NE_EXPR, compare_type);
4072 }
4073 }
4074
4075 /* Single-bit compares should always be against zero. */
4076 if (lbitsize == 1 && ! integer_zerop (rhs))
4077 {
4078 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4079 rhs = build_int_cst (type, 0);
4080 }
4081
4082 /* Make a new bitfield reference, shift the constant over the
4083 appropriate number of bits and mask it with the computed mask
4084 (in case this was a signed field). If we changed it, make a new one. */
4085 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
4086 if (lvolatilep)
4087 {
4088 TREE_SIDE_EFFECTS (lhs) = 1;
4089 TREE_THIS_VOLATILE (lhs) = 1;
4090 }
4091
4092 rhs = const_binop (BIT_AND_EXPR,
4093 const_binop (LSHIFT_EXPR,
4094 fold_convert (unsigned_type, rhs),
4095 size_int (lbitpos), 0),
4096 mask, 0);
4097
4098 return build2 (code, compare_type,
4099 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4100 rhs);
4101}
4102\f
79109eec 4103/* Subroutine for fold_truthop: decode a field reference.
2bc77e10 4104
4105 If EXP is a comparison reference, we return the innermost reference.
4106
4107 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4108 set to the starting bit number.
4109
4110 If the innermost field can be completely contained in a mode-sized
4111 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4112
4113 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4114 otherwise it is not changed.
4115
4116 *PUNSIGNEDP is set to the signedness of the field.
4117
4118 *PMASK is set to the mask used. This is either contained in a
4119 BIT_AND_EXPR or derived from the width of the field.
4120
3398e91d 4121 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
2a6329ae 4122
2bc77e10 4123 Return 0 if this is not a component reference or is one that we can't
4124 do anything with. */
4125
4126static tree
dc81944a 4127decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
4128 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4129 int *punsignedp, int *pvolatilep,
de1b648b 4130 tree *pmask, tree *pand_mask)
2bc77e10 4131{
74878f86 4132 tree outer_type = 0;
4843fe7c 4133 tree and_mask = 0;
4134 tree mask, inner, offset;
4135 tree unsigned_type;
02e7a332 4136 unsigned int precision;
2bc77e10 4137
cc049fa3 4138 /* All the optimizations using this function assume integer fields.
e40566fc 4139 There are problems with FP fields since the type_for_size call
4140 below can fail for, e.g., XFmode. */
4141 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4142 return 0;
4143
74878f86 4144 /* We are interested in the bare arrangement of bits, so strip everything
4145 that doesn't affect the machine mode. However, record the type of the
4146 outermost expression if it may matter below. */
72dd6141 4147 if (CONVERT_EXPR_P (exp)
74878f86 4148 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4149 outer_type = TREE_TYPE (exp);
78379bd9 4150 STRIP_NOPS (exp);
2bc77e10 4151
4152 if (TREE_CODE (exp) == BIT_AND_EXPR)
4153 {
4843fe7c 4154 and_mask = TREE_OPERAND (exp, 1);
2bc77e10 4155 exp = TREE_OPERAND (exp, 0);
4843fe7c 4156 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4157 if (TREE_CODE (and_mask) != INTEGER_CST)
2bc77e10 4158 return 0;
4159 }
4160
bbfbdece 4161 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
e7e9416e 4162 punsignedp, pvolatilep, false);
94f29e88 4163 if ((inner == exp && and_mask == 0)
155b05dc 4164 || *pbitsize < 0 || offset != 0
4165 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
e233264a 4166 return 0;
cc049fa3 4167
74878f86 4168 /* If the number of bits in the reference is the same as the bitsize of
4169 the outer type, then the outer type gives the signedness. Otherwise
4170 (in case of a small bitfield) the signedness is unchanged. */
18dbec6f 4171 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
78a8ed03 4172 *punsignedp = TYPE_UNSIGNED (outer_type);
74878f86 4173
4843fe7c 4174 /* Compute the mask to access the bitfield. */
fa8b888f 4175 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4843fe7c 4176 precision = TYPE_PRECISION (unsigned_type);
4177
697bbc3f 4178 mask = build_int_cst_type (unsigned_type, -1);
0c5713a2 4179
4843fe7c 4180 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4181 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4182
4183 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4184 if (and_mask != 0)
7ab7fd4f 4185 mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
4186 fold_convert (unsigned_type, and_mask), mask);
2bc77e10 4187
4188 *pmask = mask;
2a6329ae 4189 *pand_mask = and_mask;
2bc77e10 4190 return inner;
4191}
4192
2a64c730 4193/* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4194 bit positions. */
4195
4196static int
4197all_ones_mask_p (const_tree mask, int size)
4198{
4199 tree type = TREE_TYPE (mask);
4200 unsigned int precision = TYPE_PRECISION (type);
4201 tree tmask;
4202
4203 tmask = build_int_cst_type (signed_type_for (type), -1);
4204
4205 return
4206 tree_int_cst_equal (mask,
4207 const_binop (RSHIFT_EXPR,
4208 const_binop (LSHIFT_EXPR, tmask,
4209 size_int (precision - size),
4210 0),
4211 size_int (precision - size), 0));
4212}
4213
203a24c4 4214/* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4215 represents the sign bit of EXP's type. If EXP represents a sign
4216 or zero extension, also test VAL against the unextended type.
4217 The return value is the (sub)expression whose sign bit is VAL,
4218 or NULL_TREE otherwise. */
4219
4220static tree
b4b34335 4221sign_bit_p (tree exp, const_tree val)
203a24c4 4222{
a4de5624 4223 unsigned HOST_WIDE_INT mask_lo, lo;
4224 HOST_WIDE_INT mask_hi, hi;
203a24c4 4225 int width;
4226 tree t;
4227
95cc2547 4228 /* Tree EXP must have an integral type. */
203a24c4 4229 t = TREE_TYPE (exp);
4230 if (! INTEGRAL_TYPE_P (t))
4231 return NULL_TREE;
4232
4233 /* Tree VAL must be an integer constant. */
4234 if (TREE_CODE (val) != INTEGER_CST
f96bd2bf 4235 || TREE_OVERFLOW (val))
203a24c4 4236 return NULL_TREE;
4237
4238 width = TYPE_PRECISION (t);
4239 if (width > HOST_BITS_PER_WIDE_INT)
4240 {
4241 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4242 lo = 0;
a4de5624 4243
4244 mask_hi = ((unsigned HOST_WIDE_INT) -1
4245 >> (2 * HOST_BITS_PER_WIDE_INT - width));
4246 mask_lo = -1;
203a24c4 4247 }
4248 else
4249 {
4250 hi = 0;
4251 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
a4de5624 4252
4253 mask_hi = 0;
4254 mask_lo = ((unsigned HOST_WIDE_INT) -1
4255 >> (HOST_BITS_PER_WIDE_INT - width));
203a24c4 4256 }
4257
a4de5624 4258 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4259 treat VAL as if it were unsigned. */
4260 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4261 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
203a24c4 4262 return exp;
4263
4264 /* Handle extension from a narrower type. */
4265 if (TREE_CODE (exp) == NOP_EXPR
4266 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4267 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4268
4269 return NULL_TREE;
4270}
4271
79109eec 4272/* Subroutine for fold_truthop: determine if an operand is simple enough
4273 to be evaluated unconditionally. */
4274
cc049fa3 4275static int
b4b34335 4276simple_operand_p (const_tree exp)
79109eec 4277{
4278 /* Strip any conversions that don't change the machine mode. */
9a73db25 4279 STRIP_NOPS (exp);
79109eec 4280
ce45a448 4281 return (CONSTANT_CLASS_P (exp)
9a73db25 4282 || TREE_CODE (exp) == SSA_NAME
9308e976 4283 || (DECL_P (exp)
79109eec 4284 && ! TREE_ADDRESSABLE (exp)
4285 && ! TREE_THIS_VOLATILE (exp)
7735dddb 4286 && ! DECL_NONLOCAL (exp)
4287 /* Don't regard global variables as simple. They may be
4288 allocated in ways unknown to the compiler (shared memory,
4289 #pragma weak, etc). */
4290 && ! TREE_PUBLIC (exp)
4291 && ! DECL_EXTERNAL (exp)
4292 /* Loading a static variable is unduly expensive, but global
4293 registers aren't expensive. */
4294 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
79109eec 4295}
2bc77e10 4296\f
12ec0a8a 4297/* The following functions are subroutines to fold_range_test and allow it to
4298 try to change a logical combination of comparisons into a range test.
4299
4300 For example, both
de1b648b 4301 X == 2 || X == 3 || X == 4 || X == 5
12ec0a8a 4302 and
de1b648b 4303 X >= 2 && X <= 5
12ec0a8a 4304 are converted to
4305 (unsigned) (X - 2) <= 3
4306
ad87de1e 4307 We describe each set of comparisons as being either inside or outside
12ec0a8a 4308 a range, using a variable named like IN_P, and then describe the
4309 range with a lower and upper bound. If one of the bounds is omitted,
4310 it represents either the highest or lowest value of the type.
4311
4312 In the comments below, we represent a range by two numbers in brackets
ad87de1e 4313 preceded by a "+" to designate being inside that range, or a "-" to
12ec0a8a 4314 designate being outside that range, so the condition can be inverted by
4315 flipping the prefix. An omitted bound is represented by a "-". For
4316 example, "- [-, 10]" means being outside the range starting at the lowest
4317 possible value and ending at 10, in other words, being greater than 10.
4318 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4319 always false.
4320
4321 We set up things so that the missing bounds are handled in a consistent
4322 manner so neither a missing bound nor "true" and "false" need to be
4323 handled using a special case. */
4324
4325/* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4326 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4327 and UPPER1_P are nonzero if the respective argument is an upper bound
4328 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4329 must be specified for a comparison. ARG1 will be converted to ARG0's
4330 type if both are specified. */
6f725368 4331
12ec0a8a 4332static tree
dc81944a 4333range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4334 tree arg1, int upper1_p)
12ec0a8a 4335{
7560c8de 4336 tree tem;
12ec0a8a 4337 int result;
4338 int sgn0, sgn1;
6f725368 4339
12ec0a8a 4340 /* If neither arg represents infinity, do the normal operation.
4341 Else, if not a comparison, return infinity. Else handle the special
4342 comparison rules. Note that most of the cases below won't occur, but
4343 are handled for consistency. */
6f725368 4344
12ec0a8a 4345 if (arg0 != 0 && arg1 != 0)
7560c8de 4346 {
7ab7fd4f 4347 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4348 arg0, fold_convert (TREE_TYPE (arg0), arg1));
7560c8de 4349 STRIP_NOPS (tem);
4350 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4351 }
6f725368 4352
ce45a448 4353 if (TREE_CODE_CLASS (code) != tcc_comparison)
12ec0a8a 4354 return 0;
4355
4356 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
621ba396 4357 for neither. In real maths, we cannot assume open ended ranges are
4358 the same. But, this is computer arithmetic, where numbers are finite.
4359 We can therefore make the transformation of any unbounded range with
4360 the value Z, Z being greater than any representable number. This permits
6312a35e 4361 us to treat unbounded ranges as equal. */
12ec0a8a 4362 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
263497ab 4363 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
12ec0a8a 4364 switch (code)
4365 {
621ba396 4366 case EQ_EXPR:
4367 result = sgn0 == sgn1;
4368 break;
4369 case NE_EXPR:
4370 result = sgn0 != sgn1;
12ec0a8a 4371 break;
621ba396 4372 case LT_EXPR:
12ec0a8a 4373 result = sgn0 < sgn1;
4374 break;
621ba396 4375 case LE_EXPR:
4376 result = sgn0 <= sgn1;
4377 break;
4378 case GT_EXPR:
12ec0a8a 4379 result = sgn0 > sgn1;
4380 break;
621ba396 4381 case GE_EXPR:
4382 result = sgn0 >= sgn1;
4383 break;
0dbd1c74 4384 default:
fdada98f 4385 gcc_unreachable ();
12ec0a8a 4386 }
4387
20783f07 4388 return constant_boolean_node (result, type);
12ec0a8a 4389}
cc049fa3 4390\f
12ec0a8a 4391/* Given EXP, a logical expression, set the range it is testing into
4392 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
add6ee5e 4393 actually being tested. *PLOW and *PHIGH will be made of the same
4394 type as the returned expression. If EXP is not a comparison, we
4395 will most likely not be returning a useful value and range. Set
4396 *STRICT_OVERFLOW_P to true if the return value is only valid
4397 because signed overflow is undefined; otherwise, do not change
4398 *STRICT_OVERFLOW_P. */
6f725368 4399
bfd67d2c 4400static tree
add6ee5e 4401make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4402 bool *strict_overflow_p)
6f725368 4403{
12ec0a8a 4404 enum tree_code code;
7206da1b 4405 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4406 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
12ec0a8a 4407 int in_p, n_in_p;
4408 tree low, high, n_low, n_high;
6f725368 4409
12ec0a8a 4410 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4411 and see if we can refine the range. Some of the cases below may not
4412 happen, but it doesn't seem worth worrying about this. We "continue"
4413 the outer loop when we've changed something; otherwise we "break"
4414 the switch, which will "break" the while. */
6f725368 4415
b30e3dbc 4416 in_p = 0;
3c6185f1 4417 low = high = build_int_cst (TREE_TYPE (exp), 0);
12ec0a8a 4418
4419 while (1)
6f725368 4420 {
12ec0a8a 4421 code = TREE_CODE (exp);
7206da1b 4422 exp_type = TREE_TYPE (exp);
5eb945de 4423
4424 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4425 {
c2f47e15 4426 if (TREE_OPERAND_LENGTH (exp) > 0)
13795292 4427 arg0 = TREE_OPERAND (exp, 0);
ce45a448 4428 if (TREE_CODE_CLASS (code) == tcc_comparison
4429 || TREE_CODE_CLASS (code) == tcc_unary
4430 || TREE_CODE_CLASS (code) == tcc_binary)
7206da1b 4431 arg0_type = TREE_TYPE (arg0);
ce45a448 4432 if (TREE_CODE_CLASS (code) == tcc_binary
4433 || TREE_CODE_CLASS (code) == tcc_comparison
4434 || (TREE_CODE_CLASS (code) == tcc_expression
c2f47e15 4435 && TREE_OPERAND_LENGTH (exp) > 1))
5eb945de 4436 arg1 = TREE_OPERAND (exp, 1);
4437 }
6f725368 4438
12ec0a8a 4439 switch (code)
4440 {
4441 case TRUTH_NOT_EXPR:
4442 in_p = ! in_p, exp = arg0;
4443 continue;
4444
4445 case EQ_EXPR: case NE_EXPR:
4446 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4447 /* We can only do something if the range is testing for zero
4448 and if the second operand is an integer constant. Note that
4449 saying something is "in" the range we make is done by
4450 complementing IN_P since it will set in the initial case of
4451 being not equal to zero; "out" is leaving it alone. */
4452 if (low == 0 || high == 0
4453 || ! integer_zerop (low) || ! integer_zerop (high)
4454 || TREE_CODE (arg1) != INTEGER_CST)
4455 break;
6f725368 4456
12ec0a8a 4457 switch (code)
4458 {
4459 case NE_EXPR: /* - [c, c] */
4460 low = high = arg1;
4461 break;
4462 case EQ_EXPR: /* + [c, c] */
4463 in_p = ! in_p, low = high = arg1;
4464 break;
4465 case GT_EXPR: /* - [-, c] */
4466 low = 0, high = arg1;
4467 break;
4468 case GE_EXPR: /* + [c, -] */
4469 in_p = ! in_p, low = arg1, high = 0;
4470 break;
4471 case LT_EXPR: /* - [c, -] */
4472 low = arg1, high = 0;
4473 break;
4474 case LE_EXPR: /* + [-, c] */
4475 in_p = ! in_p, low = 0, high = arg1;
4476 break;
0dbd1c74 4477 default:
fdada98f 4478 gcc_unreachable ();
12ec0a8a 4479 }
6f725368 4480
c317c285 4481 /* If this is an unsigned comparison, we also know that EXP is
a9e29e86 4482 greater than or equal to zero. We base the range tests we make
4483 on that fact, so we record it here so we can parse existing
7206da1b 4484 range tests. We test arg0_type since often the return type
4485 of, e.g. EQ_EXPR, is boolean. */
4486 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
12ec0a8a 4487 {
5c9198bd 4488 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4489 in_p, low, high, 1,
3c6185f1 4490 build_int_cst (arg0_type, 0),
a9e29e86 4491 NULL_TREE))
12ec0a8a 4492 break;
6f725368 4493
12ec0a8a 4494 in_p = n_in_p, low = n_low, high = n_high;
a9e29e86 4495
751e10d1 4496 /* If the high bound is missing, but we have a nonzero low
e524954a 4497 bound, reverse the range so it goes from zero to the low bound
4498 minus 1. */
4499 if (high == 0 && low && ! integer_zerop (low))
a9e29e86 4500 {
4501 in_p = ! in_p;
4502 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4503 integer_one_node, 0);
3c6185f1 4504 low = build_int_cst (arg0_type, 0);
a9e29e86 4505 }
12ec0a8a 4506 }
7206da1b 4507
4508 exp = arg0;
12ec0a8a 4509 continue;
4510
4511 case NEGATE_EXPR:
4512 /* (-x) IN [a,b] -> x in [-b, -a] */
7206da1b 4513 n_low = range_binop (MINUS_EXPR, exp_type,
3c6185f1 4514 build_int_cst (exp_type, 0),
b30e3dbc 4515 0, high, 1);
7206da1b 4516 n_high = range_binop (MINUS_EXPR, exp_type,
3c6185f1 4517 build_int_cst (exp_type, 0),
b30e3dbc 4518 0, low, 0);
12ec0a8a 4519 low = n_low, high = n_high;
4520 exp = arg0;
4521 continue;
4522
4523 case BIT_NOT_EXPR:
4524 /* ~ X -> -X - 1 */
7206da1b 4525 exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
3c6185f1 4526 build_int_cst (exp_type, 1));
12ec0a8a 4527 continue;
4528
4529 case PLUS_EXPR: case MINUS_EXPR:
4530 if (TREE_CODE (arg1) != INTEGER_CST)
4531 break;
4532
0a8b4135 4533 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4534 move a constant to the other side. */
981eb798 4535 if (!TYPE_UNSIGNED (arg0_type)
4536 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
0a8b4135 4537 break;
4538
12ec0a8a 4539 /* If EXP is signed, any overflow in the computation is undefined,
4540 so we don't worry about it so long as our computations on
4541 the bounds don't overflow. For unsigned, overflow is defined
4542 and this is exactly the right thing. */
4543 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
7206da1b 4544 arg0_type, low, 0, arg1, 0);
12ec0a8a 4545 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
7206da1b 4546 arg0_type, high, 1, arg1, 0);
12ec0a8a 4547 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4548 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4549 break;
4550
add6ee5e 4551 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4552 *strict_overflow_p = true;
4553
6b457c77 4554 /* Check for an unsigned range which has wrapped around the maximum
4555 value thus making n_high < n_low, and normalize it. */
98db800f 4556 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
6b457c77 4557 {
7206da1b 4558 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
a9e29e86 4559 integer_one_node, 0);
7206da1b 4560 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
a80d786b 4561 integer_one_node, 0);
4562
4563 /* If the range is of the form +/- [ x+1, x ], we won't
4564 be able to normalize it. But then, it represents the
4565 whole range or the empty set, so make it
4566 +/- [ -, - ]. */
4567 if (tree_int_cst_equal (n_low, low)
4568 && tree_int_cst_equal (n_high, high))
4569 low = high = 0;
4570 else
4571 in_p = ! in_p;
6b457c77 4572 }
98db800f 4573 else
4574 low = n_low, high = n_high;
7560c8de 4575
12ec0a8a 4576 exp = arg0;
4577 continue;
4578
72dd6141 4579 CASE_CONVERT: case NON_LVALUE_EXPR:
7206da1b 4580 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
d6d65bd2 4581 break;
4582
7206da1b 4583 if (! INTEGRAL_TYPE_P (arg0_type)
4584 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4585 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
12ec0a8a 4586 break;
4587
4cd44a59 4588 n_low = low, n_high = high;
12ec0a8a 4589
4cd44a59 4590 if (n_low != 0)
7206da1b 4591 n_low = fold_convert (arg0_type, n_low);
4cd44a59 4592
4593 if (n_high != 0)
7206da1b 4594 n_high = fold_convert (arg0_type, n_high);
4cd44a59 4595
4cd44a59 4596
7206da1b 4597 /* If we're converting arg0 from an unsigned type, to exp,
2c763ed4 4598 a signed type, we will be doing the comparison as unsigned.
7206da1b 4599 The tests above have already verified that LOW and HIGH
4600 are both positive.
4601
4602 So we have to ensure that we will handle large unsigned
4603 values the same way that the current signed bounds treat
4604 negative values. */
4605
4606 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4cd44a59 4607 {
f52483b5 4608 tree high_positive;
06f0b99c 4609 tree equiv_type;
4610 /* For fixed-point modes, we need to pass the saturating flag
4611 as the 2nd parameter. */
4612 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4613 equiv_type = lang_hooks.types.type_for_mode
4614 (TYPE_MODE (arg0_type),
4615 TYPE_SATURATING (arg0_type));
4616 else
4617 equiv_type = lang_hooks.types.type_for_mode
4618 (TYPE_MODE (arg0_type), 1);
f52483b5 4619
4620 /* A range without an upper bound is, naturally, unbounded.
4621 Since convert would have cropped a very large value, use
155b05dc 4622 the max value for the destination type. */
4623 high_positive
4624 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
7206da1b 4625 : TYPE_MAX_VALUE (arg0_type);
f52483b5 4626
7206da1b 4627 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
7ab7fd4f 4628 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4629 fold_convert (arg0_type,
4630 high_positive),
2455d3ef 4631 build_int_cst (arg0_type, 1));
cc049fa3 4632
4cd44a59 4633 /* If the low bound is specified, "and" the range with the
4634 range for which the original unsigned value will be
4635 positive. */
4636 if (low != 0)
4637 {
4638 if (! merge_ranges (&n_in_p, &n_low, &n_high,
b30e3dbc 4639 1, n_low, n_high, 1,
5c9198bd 4640 fold_convert (arg0_type,
4641 integer_zero_node),
4cd44a59 4642 high_positive))
4643 break;
4644
4645 in_p = (n_in_p == in_p);
4646 }
4647 else
4648 {
4649 /* Otherwise, "or" the range with the range of the input
4650 that will be interpreted as negative. */
4651 if (! merge_ranges (&n_in_p, &n_low, &n_high,
b30e3dbc 4652 0, n_low, n_high, 1,
5c9198bd 4653 fold_convert (arg0_type,
4654 integer_zero_node),
4cd44a59 4655 high_positive))
4656 break;
4657
4658 in_p = (in_p != n_in_p);
4659 }
4660 }
12ec0a8a 4661
4662 exp = arg0;
4cd44a59 4663 low = n_low, high = n_high;
12ec0a8a 4664 continue;
4cd44a59 4665
4666 default:
4667 break;
6f725368 4668 }
12ec0a8a 4669
4670 break;
6f725368 4671 }
12ec0a8a 4672
f83854c8 4673 /* If EXP is a constant, we can evaluate whether this is true or false. */
4674 if (TREE_CODE (exp) == INTEGER_CST)
4675 {
4676 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4677 exp, 0, low, 0))
4678 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4679 exp, 1, high, 1)));
4680 low = high = 0;
4681 exp = 0;
4682 }
4683
12ec0a8a 4684 *pin_p = in_p, *plow = low, *phigh = high;
4685 return exp;
4686}
4687\f
4688/* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4689 type, TYPE, return an expression to test if EXP is in (or out of, depending
3b3a787a 4690 on IN_P) the range. Return 0 if the test couldn't be created. */
12ec0a8a 4691
4692static tree
de1b648b 4693build_range_check (tree type, tree exp, int in_p, tree low, tree high)
12ec0a8a 4694{
f2143b56 4695 tree etype = TREE_TYPE (exp), value;
12ec0a8a 4696
d067185e 4697#ifdef HAVE_canonicalize_funcptr_for_compare
4698 /* Disable this optimization for function pointer expressions
4699 on targets that require function pointer canonicalization. */
4700 if (HAVE_canonicalize_funcptr_for_compare
4701 && TREE_CODE (etype) == POINTER_TYPE
4702 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4703 return NULL_TREE;
4704#endif
4705
3b3a787a 4706 if (! in_p)
4707 {
4708 value = build_range_check (type, exp, 1, low, high);
4709 if (value != 0)
4710 return invert_truthvalue (value);
4711
4712 return 0;
4713 }
12ec0a8a 4714
843dd7a3 4715 if (low == 0 && high == 0)
3c6185f1 4716 return build_int_cst (type, 1);
12ec0a8a 4717
843dd7a3 4718 if (low == 0)
2fdd1d9f 4719 return fold_build2 (LE_EXPR, type, exp,
4720 fold_convert (etype, high));
12ec0a8a 4721
843dd7a3 4722 if (high == 0)
2fdd1d9f 4723 return fold_build2 (GE_EXPR, type, exp,
4724 fold_convert (etype, low));
12ec0a8a 4725
843dd7a3 4726 if (operand_equal_p (low, high, 0))
2fdd1d9f 4727 return fold_build2 (EQ_EXPR, type, exp,
4728 fold_convert (etype, low));
12ec0a8a 4729
843dd7a3 4730 if (integer_zerop (low))
6f725368 4731 {
78a8ed03 4732 if (! TYPE_UNSIGNED (etype))
d3371fcd 4733 {
71eea85c 4734 etype = unsigned_type_for (etype);
b30e3dbc 4735 high = fold_convert (etype, high);
4736 exp = fold_convert (etype, exp);
d3371fcd 4737 }
843dd7a3 4738 return build_range_check (type, exp, 1, 0, high);
12ec0a8a 4739 }
6f725368 4740
843dd7a3 4741 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4742 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4743 {
4744 unsigned HOST_WIDE_INT lo;
4745 HOST_WIDE_INT hi;
4746 int prec;
4747
4748 prec = TYPE_PRECISION (etype);
4749 if (prec <= HOST_BITS_PER_WIDE_INT)
d3371fcd 4750 {
4751 hi = 0;
4752 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4753 }
843dd7a3 4754 else
d3371fcd 4755 {
4756 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4757 lo = (unsigned HOST_WIDE_INT) -1;
4758 }
843dd7a3 4759
4760 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
d3371fcd 4761 {
78a8ed03 4762 if (TYPE_UNSIGNED (etype))
d3371fcd 4763 {
17c3cb97 4764 tree signed_etype = signed_type_for (etype);
4765 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4766 etype
4767 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4768 else
4769 etype = signed_etype;
b30e3dbc 4770 exp = fold_convert (etype, exp);
d3371fcd 4771 }
7ab7fd4f 4772 return fold_build2 (GT_EXPR, type, exp,
3c6185f1 4773 build_int_cst (etype, 0));
d3371fcd 4774 }
843dd7a3 4775 }
4776
66108e20 4777 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
a9538d68 4778 This requires wrap-around arithmetics for the type of the expression.
4779 First make sure that arithmetics in this type is valid, then make sure
4780 that it wraps around. */
4781 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4782 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4783 TYPE_UNSIGNED (etype));
66108e20 4784
a9538d68 4785 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
3b3a787a 4786 {
4787 tree utype, minv, maxv;
4788
4789 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4790 for the type in question, as we rely on this here. */
71eea85c 4791 utype = unsigned_type_for (etype);
66108e20 4792 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4793 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4794 integer_one_node, 1);
4795 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4796
4797 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4798 minv, 1, maxv, 1)))
4799 etype = utype;
4800 else
4801 return 0;
3b3a787a 4802 }
4803
66108e20 4804 high = fold_convert (etype, high);
4805 low = fold_convert (etype, low);
4806 exp = fold_convert (etype, exp);
3f7c18bc 4807
66108e20 4808 value = const_binop (MINUS_EXPR, high, low, 0);
4809
0de36bdb 4810
4811 if (POINTER_TYPE_P (etype))
4812 {
4813 if (value != 0 && !TREE_OVERFLOW (value))
4814 {
4815 low = fold_convert (sizetype, low);
4816 low = fold_build1 (NEGATE_EXPR, sizetype, low);
4817 return build_range_check (type,
4818 fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4819 1, build_int_cst (etype, 0), value);
4820 }
4821 return 0;
4822 }
4823
66108e20 4824 if (value != 0 && !TREE_OVERFLOW (value))
4825 return build_range_check (type,
4826 fold_build2 (MINUS_EXPR, etype, exp, low),
4827 1, build_int_cst (etype, 0), value);
843dd7a3 4828
4829 return 0;
12ec0a8a 4830}
4831\f
1557b0a0 4832/* Return the predecessor of VAL in its type, handling the infinite case. */
4833
4834static tree
4835range_predecessor (tree val)
4836{
4837 tree type = TREE_TYPE (val);
4838
20efd591 4839 if (INTEGRAL_TYPE_P (type)
4840 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
1557b0a0 4841 return 0;
4842 else
4843 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4844}
4845
4846/* Return the successor of VAL in its type, handling the infinite case. */
4847
4848static tree
4849range_successor (tree val)
4850{
4851 tree type = TREE_TYPE (val);
4852
20efd591 4853 if (INTEGRAL_TYPE_P (type)
4854 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
1557b0a0 4855 return 0;
4856 else
4857 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4858}
4859
cc049fa3 4860/* Given two ranges, see if we can merge them into one. Return 1 if we
12ec0a8a 4861 can, 0 if we can't. Set the output range into the specified parameters. */
6f725368 4862
12ec0a8a 4863static int
dc81944a 4864merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4865 tree high0, int in1_p, tree low1, tree high1)
12ec0a8a 4866{
4867 int no_overlap;
4868 int subset;
4869 int temp;
4870 tree tem;
4871 int in_p;
4872 tree low, high;
4cd44a59 4873 int lowequal = ((low0 == 0 && low1 == 0)
4874 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4875 low0, 0, low1, 0)));
4876 int highequal = ((high0 == 0 && high1 == 0)
4877 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4878 high0, 1, high1, 1)));
4879
4880 /* Make range 0 be the range that starts first, or ends last if they
4881 start at the same value. Swap them if it isn't. */
cc049fa3 4882 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
12ec0a8a 4883 low0, 0, low1, 0))
4cd44a59 4884 || (lowequal
12ec0a8a 4885 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4cd44a59 4886 high1, 1, high0, 1))))
12ec0a8a 4887 {
4888 temp = in0_p, in0_p = in1_p, in1_p = temp;
4889 tem = low0, low0 = low1, low1 = tem;
4890 tem = high0, high0 = high1, high1 = tem;
4891 }
6f725368 4892
12ec0a8a 4893 /* Now flag two cases, whether the ranges are disjoint or whether the
4894 second range is totally subsumed in the first. Note that the tests
4895 below are simplified by the ones above. */
4896 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4897 high0, 1, low1, 0));
718acf6d 4898 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
12ec0a8a 4899 high1, 1, high0, 1));
4900
4901 /* We now have four cases, depending on whether we are including or
4902 excluding the two ranges. */
4903 if (in0_p && in1_p)
4904 {
4905 /* If they don't overlap, the result is false. If the second range
4906 is a subset it is the result. Otherwise, the range is from the start
4907 of the second to the end of the first. */
4908 if (no_overlap)
4909 in_p = 0, low = high = 0;
4910 else if (subset)
4911 in_p = 1, low = low1, high = high1;
4912 else
4913 in_p = 1, low = low1, high = high0;
4914 }
6f725368 4915
12ec0a8a 4916 else if (in0_p && ! in1_p)
4917 {
4cd44a59 4918 /* If they don't overlap, the result is the first range. If they are
4919 equal, the result is false. If the second range is a subset of the
4920 first, and the ranges begin at the same place, we go from just after
66108e20 4921 the end of the second range to the end of the first. If the second
4cd44a59 4922 range is not a subset of the first, or if it is a subset and both
4923 ranges end at the same place, the range starts at the start of the
4924 first range and ends just before the second range.
4925 Otherwise, we can't describe this as a single range. */
12ec0a8a 4926 if (no_overlap)
4927 in_p = 1, low = low0, high = high0;
4cd44a59 4928 else if (lowequal && highequal)
08986c47 4929 in_p = 0, low = high = 0;
4cd44a59 4930 else if (subset && lowequal)
4931 {
66108e20 4932 low = range_successor (high1);
4933 high = high0;
bdc68add 4934 in_p = 1;
4935 if (low == 0)
4936 {
4937 /* We are in the weird situation where high0 > high1 but
4938 high1 has no successor. Punt. */
4939 return 0;
4940 }
4cd44a59 4941 }
4942 else if (! subset || highequal)
12ec0a8a 4943 {
66108e20 4944 low = low0;
4945 high = range_predecessor (low1);
bdc68add 4946 in_p = 1;
4947 if (high == 0)
4948 {
4949 /* low0 < low1 but low1 has no predecessor. Punt. */
4950 return 0;
4951 }
12ec0a8a 4952 }
4cd44a59 4953 else
4954 return 0;
12ec0a8a 4955 }
6f725368 4956
12ec0a8a 4957 else if (! in0_p && in1_p)
4958 {
4959 /* If they don't overlap, the result is the second range. If the second
4960 is a subset of the first, the result is false. Otherwise,
4961 the range starts just after the first range and ends at the
4962 end of the second. */
4963 if (no_overlap)
4964 in_p = 1, low = low1, high = high1;
155b05dc 4965 else if (subset || highequal)
12ec0a8a 4966 in_p = 0, low = high = 0;
4967 else
4968 {
66108e20 4969 low = range_successor (high0);
4970 high = high1;
bdc68add 4971 in_p = 1;
4972 if (low == 0)
4973 {
4974 /* high1 > high0 but high0 has no successor. Punt. */
4975 return 0;
4976 }
6f725368 4977 }
4978 }
4979
12ec0a8a 4980 else
4981 {
4982 /* The case where we are excluding both ranges. Here the complex case
4983 is if they don't overlap. In that case, the only time we have a
4984 range is if they are adjacent. If the second is a subset of the
4985 first, the result is the first. Otherwise, the range to exclude
4986 starts at the beginning of the first range and ends at the end of the
4987 second. */
4988 if (no_overlap)
4989 {
4990 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
66108e20 4991 range_successor (high0),
12ec0a8a 4992 1, low1, 0)))
4993 in_p = 0, low = low0, high = high1;
4994 else
3b3a787a 4995 {
4996 /* Canonicalize - [min, x] into - [-, x]. */
4997 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4998 switch (TREE_CODE (TREE_TYPE (low0)))
4999 {
5000 case ENUMERAL_TYPE:
5001 if (TYPE_PRECISION (TREE_TYPE (low0))
5002 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
5003 break;
5004 /* FALLTHROUGH */
5005 case INTEGER_TYPE:
3b3a787a 5006 if (tree_int_cst_equal (low0,
5007 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5008 low0 = 0;
5009 break;
5010 case POINTER_TYPE:
5011 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5012 && integer_zerop (low0))
5013 low0 = 0;
5014 break;
5015 default:
5016 break;
5017 }
5018
5019 /* Canonicalize - [x, max] into - [x, -]. */
5020 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5021 switch (TREE_CODE (TREE_TYPE (high1)))
5022 {
5023 case ENUMERAL_TYPE:
5024 if (TYPE_PRECISION (TREE_TYPE (high1))
5025 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5026 break;
5027 /* FALLTHROUGH */
5028 case INTEGER_TYPE:
3b3a787a 5029 if (tree_int_cst_equal (high1,
5030 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5031 high1 = 0;
5032 break;
5033 case POINTER_TYPE:
5034 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5035 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5036 high1, 1,
5037 integer_one_node, 1)))
5038 high1 = 0;
5039 break;
5040 default:
5041 break;
5042 }
5043
5044 /* The ranges might be also adjacent between the maximum and
5045 minimum values of the given type. For
5046 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5047 return + [x + 1, y - 1]. */
5048 if (low0 == 0 && high1 == 0)
5049 {
1557b0a0 5050 low = range_successor (high0);
5051 high = range_predecessor (low1);
3b3a787a 5052 if (low == 0 || high == 0)
5053 return 0;
5054
5055 in_p = 1;
5056 }
5057 else
5058 return 0;
5059 }
12ec0a8a 5060 }
5061 else if (subset)
5062 in_p = 0, low = low0, high = high0;
5063 else
5064 in_p = 0, low = low0, high = high1;
5065 }
b29eae68 5066
12ec0a8a 5067 *pin_p = in_p, *plow = low, *phigh = high;
5068 return 1;
5069}
0023616d 5070\f
5071
5072/* Subroutine of fold, looking inside expressions of the form
9b1fa4a0 5073 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5074 of the COND_EXPR. This function is being used also to optimize
5075 A op B ? C : A, by reversing the comparison first.
0023616d 5076
5077 Return a folded expression whose code is not a COND_EXPR
5078 anymore, or NULL_TREE if no folding opportunity is found. */
5079
5080static tree
9b1fa4a0 5081fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
0023616d 5082{
5083 enum tree_code comp_code = TREE_CODE (arg0);
5084 tree arg00 = TREE_OPERAND (arg0, 0);
5085 tree arg01 = TREE_OPERAND (arg0, 1);
9b1fa4a0 5086 tree arg1_type = TREE_TYPE (arg1);
0023616d 5087 tree tem;
9b1fa4a0 5088
5089 STRIP_NOPS (arg1);
0023616d 5090 STRIP_NOPS (arg2);
5091
5092 /* If we have A op 0 ? A : -A, consider applying the following
5093 transformations:
5094
5095 A == 0? A : -A same as -A
5096 A != 0? A : -A same as A
5097 A >= 0? A : -A same as abs (A)
5098 A > 0? A : -A same as abs (A)
5099 A <= 0? A : -A same as -abs (A)
5100 A < 0? A : -A same as -abs (A)
5101
5102 None of these transformations work for modes with signed
5103 zeros. If A is +/-0, the first two transformations will
5104 change the sign of the result (from +0 to -0, or vice
5105 versa). The last four will fix the sign of the result,
5106 even though the original expressions could be positive or
5107 negative, depending on the sign of A.
5108
5109 Note that all these transformations are correct if A is
5110 NaN, since the two alternatives (A and -A) are also NaNs. */
01e93ec4 5111 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5112 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5113 ? real_zerop (arg01)
5114 : integer_zerop (arg01))
38f916c2 5115 && ((TREE_CODE (arg2) == NEGATE_EXPR
5116 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5117 /* In the case that A is of the form X-Y, '-A' (arg2) may
5118 have already been folded to Y-X, check for that. */
5119 || (TREE_CODE (arg1) == MINUS_EXPR
5120 && TREE_CODE (arg2) == MINUS_EXPR
5121 && operand_equal_p (TREE_OPERAND (arg1, 0),
5122 TREE_OPERAND (arg2, 1), 0)
5123 && operand_equal_p (TREE_OPERAND (arg1, 1),
5124 TREE_OPERAND (arg2, 0), 0))))
0023616d 5125 switch (comp_code)
5126 {
5127 case EQ_EXPR:
fe9b47eb 5128 case UNEQ_EXPR:
9b1fa4a0 5129 tem = fold_convert (arg1_type, arg1);
5130 return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
0023616d 5131 case NE_EXPR:
fe9b47eb 5132 case LTGT_EXPR:
9b1fa4a0 5133 return pedantic_non_lvalue (fold_convert (type, arg1));
fe9b47eb 5134 case UNGE_EXPR:
5135 case UNGT_EXPR:
5136 if (flag_trapping_math)
5137 break;
5138 /* Fall through. */
0023616d 5139 case GE_EXPR:
5140 case GT_EXPR:
9b1fa4a0 5141 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
11773141 5142 arg1 = fold_convert (signed_type_for
9b1fa4a0 5143 (TREE_TYPE (arg1)), arg1);
7ab7fd4f 5144 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
0023616d 5145 return pedantic_non_lvalue (fold_convert (type, tem));
fe9b47eb 5146 case UNLE_EXPR:
5147 case UNLT_EXPR:
5148 if (flag_trapping_math)
5149 break;
0023616d 5150 case LE_EXPR:
5151 case LT_EXPR:
9b1fa4a0 5152 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
11773141 5153 arg1 = fold_convert (signed_type_for
9b1fa4a0 5154 (TREE_TYPE (arg1)), arg1);
7ab7fd4f 5155 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
0023616d 5156 return negate_expr (fold_convert (type, tem));
5157 default:
ce45a448 5158 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
fe9b47eb 5159 break;
0023616d 5160 }
5161
5162 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5163 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5164 both transformations are correct when A is NaN: A != 0
5165 is then true, and A == 0 is false. */
5166
01e93ec4 5167 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5168 && integer_zerop (arg01) && integer_zerop (arg2))
0023616d 5169 {
5170 if (comp_code == NE_EXPR)
9b1fa4a0 5171 return pedantic_non_lvalue (fold_convert (type, arg1));
0023616d 5172 else if (comp_code == EQ_EXPR)
3c6185f1 5173 return build_int_cst (type, 0);
0023616d 5174 }
5175
5176 /* Try some transformations of A op B ? A : B.
5177
5178 A == B? A : B same as B
5179 A != B? A : B same as A
5180 A >= B? A : B same as max (A, B)
5181 A > B? A : B same as max (B, A)
5182 A <= B? A : B same as min (A, B)
5183 A < B? A : B same as min (B, A)
5184
5185 As above, these transformations don't work in the presence
5186 of signed zeros. For example, if A and B are zeros of
5187 opposite sign, the first two transformations will change
5188 the sign of the result. In the last four, the original
5189 expressions give different results for (A=+0, B=-0) and
5190 (A=-0, B=+0), but the transformed expressions do not.
5191
5192 The first two transformations are correct if either A or B
5193 is a NaN. In the first transformation, the condition will
5194 be false, and B will indeed be chosen. In the case of the
5195 second transformation, the condition A != B will be true,
5196 and A will be chosen.
5197
5198 The conversions to max() and min() are not correct if B is
5199 a number and A is not. The conditions in the original
5200 expressions will be false, so all four give B. The min()
5201 and max() versions would give a NaN instead. */
01e93ec4 5202 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5203 && operand_equal_for_comparison_p (arg01, arg2, arg00)
98fc7ffa 5204 /* Avoid these transformations if the COND_EXPR may be used
5205 as an lvalue in the C++ front-end. PR c++/19199. */
5206 && (in_gimple_form
d0911b8e 5207 || (strcmp (lang_hooks.name, "GNU C++") != 0
5208 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
98fc7ffa 5209 || ! maybe_lvalue_p (arg1)
5210 || ! maybe_lvalue_p (arg2)))
0023616d 5211 {
5212 tree comp_op0 = arg00;
5213 tree comp_op1 = arg01;
5214 tree comp_type = TREE_TYPE (comp_op0);
5215
5216 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5217 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5218 {
5219 comp_type = type;
9b1fa4a0 5220 comp_op0 = arg1;
0023616d 5221 comp_op1 = arg2;
5222 }
5223
5224 switch (comp_code)
5225 {
5226 case EQ_EXPR:
5227 return pedantic_non_lvalue (fold_convert (type, arg2));
5228 case NE_EXPR:
9b1fa4a0 5229 return pedantic_non_lvalue (fold_convert (type, arg1));
0023616d 5230 case LE_EXPR:
5231 case LT_EXPR:
fe9b47eb 5232 case UNLE_EXPR:
5233 case UNLT_EXPR:
0023616d 5234 /* In C++ a ?: expression can be an lvalue, so put the
5235 operand which will be used if they are equal first
5236 so that we can convert this back to the
5237 corresponding COND_EXPR. */
9b1fa4a0 5238 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5c9198bd 5239 {
5240 comp_op0 = fold_convert (comp_type, comp_op0);
5241 comp_op1 = fold_convert (comp_type, comp_op1);
fe9b47eb 5242 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
7ab7fd4f 5243 ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
5244 : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
5c9198bd 5245 return pedantic_non_lvalue (fold_convert (type, tem));
5246 }
0023616d 5247 break;
5248 case GE_EXPR:
5249 case GT_EXPR:
fe9b47eb 5250 case UNGE_EXPR:
5251 case UNGT_EXPR:
9b1fa4a0 5252 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5c9198bd 5253 {
5254 comp_op0 = fold_convert (comp_type, comp_op0);
5255 comp_op1 = fold_convert (comp_type, comp_op1);
fe9b47eb 5256 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
7ab7fd4f 5257 ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5258 : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5c9198bd 5259 return pedantic_non_lvalue (fold_convert (type, tem));
5260 }
0023616d 5261 break;
fe9b47eb 5262 case UNEQ_EXPR:
5263 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5264 return pedantic_non_lvalue (fold_convert (type, arg2));
5265 break;
5266 case LTGT_EXPR:
5267 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5268 return pedantic_non_lvalue (fold_convert (type, arg1));
5269 break;
0023616d 5270 default:
ce45a448 5271 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
fe9b47eb 5272 break;
0023616d 5273 }
5274 }
5275
5276 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5277 we might still be able to simplify this. For example,
5278 if C1 is one less or one more than C2, this might have started
5279 out as a MIN or MAX and been transformed by this function.
5280 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5281
5282 if (INTEGRAL_TYPE_P (type)
5283 && TREE_CODE (arg01) == INTEGER_CST
5284 && TREE_CODE (arg2) == INTEGER_CST)
5285 switch (comp_code)
5286 {
5287 case EQ_EXPR:
5288 /* We can replace A with C1 in this case. */
9b1fa4a0 5289 arg1 = fold_convert (type, arg01);
7ab7fd4f 5290 return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
0023616d 5291
5292 case LT_EXPR:
5293 /* If C1 is C2 + 1, this is min(A, C2). */
5294 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5295 OEP_ONLY_CONST)
5296 && operand_equal_p (arg01,
5297 const_binop (PLUS_EXPR, arg2,
2455d3ef 5298 build_int_cst (type, 1), 0),
0023616d 5299 OEP_ONLY_CONST))
7ab7fd4f 5300 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
de165fbd 5301 type,
5302 fold_convert (type, arg1),
5303 arg2));
0023616d 5304 break;
5305
5306 case LE_EXPR:
5307 /* If C1 is C2 - 1, this is min(A, C2). */
5308 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5309 OEP_ONLY_CONST)
5310 && operand_equal_p (arg01,
5311 const_binop (MINUS_EXPR, arg2,
2455d3ef 5312 build_int_cst (type, 1), 0),
0023616d 5313 OEP_ONLY_CONST))
7ab7fd4f 5314 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
de165fbd 5315 type,
5316 fold_convert (type, arg1),
5317 arg2));
0023616d 5318 break;
5319
5320 case GT_EXPR:
00211027 5321 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5322 MAX_EXPR, to preserve the signedness of the comparison. */
0023616d 5323 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5324 OEP_ONLY_CONST)
5325 && operand_equal_p (arg01,
5326 const_binop (MINUS_EXPR, arg2,
2455d3ef 5327 build_int_cst (type, 1), 0),
0023616d 5328 OEP_ONLY_CONST))
00211027 5329 return pedantic_non_lvalue (fold_convert (type,
5330 fold_build2 (MAX_EXPR, TREE_TYPE (arg00),
5331 arg00,
5332 fold_convert (TREE_TYPE (arg00),
5333 arg2))));
0023616d 5334 break;
5335
5336 case GE_EXPR:
00211027 5337 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
0023616d 5338 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5339 OEP_ONLY_CONST)
5340 && operand_equal_p (arg01,
5341 const_binop (PLUS_EXPR, arg2,
2455d3ef 5342 build_int_cst (type, 1), 0),
0023616d 5343 OEP_ONLY_CONST))
00211027 5344 return pedantic_non_lvalue (fold_convert (type,
5345 fold_build2 (MAX_EXPR, TREE_TYPE (arg00),
5346 arg00,
5347 fold_convert (TREE_TYPE (arg00),
5348 arg2))));
0023616d 5349 break;
5350 case NE_EXPR:
5351 break;
5352 default:
fdada98f 5353 gcc_unreachable ();
0023616d 5354 }
5355
5356 return NULL_TREE;
5357}
5358
5359
12ec0a8a 5360\f
17529f98 5361#ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4a9d7ef7 5362#define LOGICAL_OP_NON_SHORT_CIRCUIT \
ad4341e8 5363 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4a9d7ef7 5364 false) >= 2)
cf451ad8 5365#endif
5366
12ec0a8a 5367/* EXP is some logical combination of boolean tests. See if we can
5368 merge it into some range test. Return the new tree if so. */
6f725368 5369
12ec0a8a 5370static tree
2c17ebb2 5371fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
12ec0a8a 5372{
2c17ebb2 5373 int or_op = (code == TRUTH_ORIF_EXPR
5374 || code == TRUTH_OR_EXPR);
12ec0a8a 5375 int in0_p, in1_p, in_p;
5376 tree low0, low1, low, high0, high1, high;
add6ee5e 5377 bool strict_overflow_p = false;
5378 tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5379 tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
12ec0a8a 5380 tree tem;
add6ee5e 5381 const char * const warnmsg = G_("assuming signed overflow does not occur "
5382 "when simplifying range test");
6f725368 5383
12ec0a8a 5384 /* If this is an OR operation, invert both sides; we will invert
5385 again at the end. */
5386 if (or_op)
5387 in0_p = ! in0_p, in1_p = ! in1_p;
5388
5389 /* If both expressions are the same, if we can merge the ranges, and we
f83854c8 5390 can build the range test, return it or it inverted. If one of the
5391 ranges is always true or always false, consider it to be the same
5392 expression as the other. */
5393 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
12ec0a8a 5394 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5395 in1_p, low1, high1)
2c17ebb2 5396 && 0 != (tem = (build_range_check (type,
f83854c8 5397 lhs != 0 ? lhs
5398 : rhs != 0 ? rhs : integer_zero_node,
12ec0a8a 5399 in_p, low, high))))
add6ee5e 5400 {
5401 if (strict_overflow_p)
5402 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5403 return or_op ? invert_truthvalue (tem) : tem;
5404 }
12ec0a8a 5405
5406 /* On machines where the branch cost is expensive, if this is a
5407 short-circuited branch and the underlying object on both sides
5408 is the same, make a non-short-circuit operation. */
17529f98 5409 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
1fdbc76b 5410 && lhs != 0 && rhs != 0
2c17ebb2 5411 && (code == TRUTH_ANDIF_EXPR
5412 || code == TRUTH_ORIF_EXPR)
12ec0a8a 5413 && operand_equal_p (lhs, rhs, 0))
6f725368 5414 {
90a73592 5415 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
9e042f31 5416 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5417 which cases we can't do this. */
12ec0a8a 5418 if (simple_operand_p (lhs))
2c17ebb2 5419 return build2 (code == TRUTH_ANDIF_EXPR
fd96eeef 5420 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
2c17ebb2 5421 type, op0, op1);
90a73592 5422
fa8b888f 5423 else if (lang_hooks.decls.global_bindings_p () == 0
ce3fb06e 5424 && ! CONTAINS_PLACEHOLDER_P (lhs))
12ec0a8a 5425 {
5426 tree common = save_expr (lhs);
5427
2c17ebb2 5428 if (0 != (lhs = build_range_check (type, common,
12ec0a8a 5429 or_op ? ! in0_p : in0_p,
5430 low0, high0))
2c17ebb2 5431 && (0 != (rhs = build_range_check (type, common,
12ec0a8a 5432 or_op ? ! in1_p : in1_p,
5433 low1, high1))))
add6ee5e 5434 {
5435 if (strict_overflow_p)
5436 fold_overflow_warning (warnmsg,
5437 WARN_STRICT_OVERFLOW_COMPARISON);
5438 return build2 (code == TRUTH_ANDIF_EXPR
5439 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5440 type, lhs, rhs);
5441 }
12ec0a8a 5442 }
6f725368 5443 }
831e3af4 5444
831e3af4 5445 return 0;
6f725368 5446}
5447\f
94f29e88 5448/* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
b2dcfbf7 5449 bit value. Arrange things so the extra bits will be set to zero if and
2a6329ae 5450 only if C is signed-extended to its full width. If MASK is nonzero,
5451 it is an INTEGER_CST that should be AND'ed with the extra bits. */
94f29e88 5452
5453static tree
de1b648b 5454unextend (tree c, int p, int unsignedp, tree mask)
94f29e88 5455{
5456 tree type = TREE_TYPE (c);
5457 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5458 tree temp;
5459
5460 if (p == modesize || unsignedp)
5461 return c;
5462
94f29e88 5463 /* We work by getting just the sign bit into the low-order bit, then
c3418f42 5464 into the high-order bit, then sign-extend. We then XOR that value
94f29e88 5465 with C. */
5466 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5467 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
dd5f6dae 5468
5469 /* We must use a signed type in order to get an arithmetic right shift.
5470 However, we must also avoid introducing accidental overflows, so that
cc049fa3 5471 a subsequent call to integer_zerop will work. Hence we must
dd5f6dae 5472 do the type conversion here. At this point, the constant is either
5473 zero or one, and the conversion to a signed type can never overflow.
5474 We could get an overflow if this conversion is done anywhere else. */
78a8ed03 5475 if (TYPE_UNSIGNED (type))
11773141 5476 temp = fold_convert (signed_type_for (type), temp);
dd5f6dae 5477
94f29e88 5478 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5479 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
2a6329ae 5480 if (mask != 0)
b30e3dbc 5481 temp = const_binop (BIT_AND_EXPR, temp,
5482 fold_convert (TREE_TYPE (c), mask), 0);
dd5f6dae 5483 /* If necessary, convert the type back to match the type of C. */
78a8ed03 5484 if (TYPE_UNSIGNED (type))
b30e3dbc 5485 temp = fold_convert (type, temp);
2a6329ae 5486
b30e3dbc 5487 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
94f29e88 5488}
5489\f
79109eec 5490/* Find ways of folding logical expressions of LHS and RHS:
5491 Try to merge two comparisons to the same innermost item.
5492 Look for range tests like "ch >= '0' && ch <= '9'".
5493 Look for combinations of simple terms on machines with expensive branches
5494 and evaluate the RHS unconditionally.
2bc77e10 5495
5496 For example, if we have p->a == 2 && p->b == 4 and we can make an
5497 object large enough to span both A and B, we can do this with a comparison
5498 against the object ANDed with the a mask.
5499
5500 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5501 operations to do this with one comparison.
5502
5503 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5504 function and the one above.
5505
5506 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5507 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5508
5509 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5510 two operands.
5511
5512 We return the simplified tree or 0 if no optimization is possible. */
5513
5514static tree
de1b648b 5515fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
2bc77e10 5516{
62af9abe 5517 /* If this is the "or" of two comparisons, we can do something if
2bc77e10 5518 the comparisons are NE_EXPR. If this is the "and", we can do something
cc049fa3 5519 if the comparisons are EQ_EXPR. I.e.,
de1b648b 5520 (a->b == 2 && a->c == 4) can become (a->new == NEW).
2bc77e10 5521
5522 WANTED_CODE is this operation code. For single bit fields, we can
5523 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5524 comparison for one-bit fields. */
5525
79109eec 5526 enum tree_code wanted_code;
2bc77e10 5527 enum tree_code lcode, rcode;
79109eec 5528 tree ll_arg, lr_arg, rl_arg, rr_arg;
2bc77e10 5529 tree ll_inner, lr_inner, rl_inner, rr_inner;
02e7a332 5530 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5531 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
2a64c730 5532 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5533 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
2bc77e10 5534 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5535 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
2a64c730 5536 enum machine_mode lnmode, rnmode;
2bc77e10 5537 tree ll_mask, lr_mask, rl_mask, rr_mask;
2a6329ae 5538 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
79109eec 5539 tree l_const, r_const;
2a64c730 5540 tree lntype, rntype, result;
5541 HOST_WIDE_INT first_bit, end_bit;
79109eec 5542 int volatilep;
40c3c1b3 5543 tree orig_lhs = lhs, orig_rhs = rhs;
5544 enum tree_code orig_code = code;
2bc77e10 5545
12ec0a8a 5546 /* Start by getting the comparison codes. Fail if anything is volatile.
5547 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5548 it were surrounded with a NE_EXPR. */
2bc77e10 5549
12ec0a8a 5550 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
79109eec 5551 return 0;
5552
2bc77e10 5553 lcode = TREE_CODE (lhs);
5554 rcode = TREE_CODE (rhs);
6f725368 5555
b5ab1edd 5556 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
fd96eeef 5557 {
5c9198bd 5558 lhs = build2 (NE_EXPR, truth_type, lhs,
3c6185f1 5559 build_int_cst (TREE_TYPE (lhs), 0));
fd96eeef 5560 lcode = NE_EXPR;
5561 }
b5ab1edd 5562
5563 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
fd96eeef 5564 {
5c9198bd 5565 rhs = build2 (NE_EXPR, truth_type, rhs,
3c6185f1 5566 build_int_cst (TREE_TYPE (rhs), 0));
fd96eeef 5567 rcode = NE_EXPR;
5568 }
b5ab1edd 5569
ce45a448 5570 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5571 || TREE_CODE_CLASS (rcode) != tcc_comparison)
6f725368 5572 return 0;
5573
79109eec 5574 ll_arg = TREE_OPERAND (lhs, 0);
5575 lr_arg = TREE_OPERAND (lhs, 1);
5576 rl_arg = TREE_OPERAND (rhs, 0);
5577 rr_arg = TREE_OPERAND (rhs, 1);
cc049fa3 5578
7835f163 5579 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5580 if (simple_operand_p (ll_arg)
318a728f 5581 && simple_operand_p (lr_arg))
7835f163 5582 {
318a728f 5583 tree result;
7835f163 5584 if (operand_equal_p (ll_arg, rl_arg, 0)
5585 && operand_equal_p (lr_arg, rr_arg, 0))
318a728f 5586 {
5587 result = combine_comparisons (code, lcode, rcode,
5588 truth_type, ll_arg, lr_arg);
5589 if (result)
5590 return result;
5591 }
7835f163 5592 else if (operand_equal_p (ll_arg, rr_arg, 0)
5593 && operand_equal_p (lr_arg, rl_arg, 0))
318a728f 5594 {
5595 result = combine_comparisons (code, lcode,
5596 swap_tree_comparison (rcode),
5597 truth_type, ll_arg, lr_arg);
5598 if (result)
5599 return result;
5600 }
7835f163 5601 }
5602
318a728f 5603 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5604 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5605
7735dddb 5606 /* If the RHS can be evaluated unconditionally and its operands are
79109eec 5607 simple, it wins to evaluate the RHS unconditionally on machines
5608 with expensive branches. In this case, this isn't a comparison
35212e61 5609 that can be merged. Avoid doing this if the RHS is a floating-point
5610 comparison since those can trap. */
79109eec 5611
ad4341e8 5612 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
4a9d7ef7 5613 false) >= 2
35212e61 5614 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
79109eec 5615 && simple_operand_p (rl_arg)
7735dddb 5616 && simple_operand_p (rr_arg))
0425437e 5617 {
5618 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5619 if (code == TRUTH_OR_EXPR
5620 && lcode == NE_EXPR && integer_zerop (lr_arg)
5621 && rcode == NE_EXPR && integer_zerop (rr_arg)
d159b72d 5622 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5623 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
fd96eeef 5624 return build2 (NE_EXPR, truth_type,
5625 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5626 ll_arg, rl_arg),
3c6185f1 5627 build_int_cst (TREE_TYPE (ll_arg), 0));
0425437e 5628
5629 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5630 if (code == TRUTH_AND_EXPR
5631 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5632 && rcode == EQ_EXPR && integer_zerop (rr_arg)
d159b72d 5633 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5634 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
fd96eeef 5635 return build2 (EQ_EXPR, truth_type,
5636 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5637 ll_arg, rl_arg),
3c6185f1 5638 build_int_cst (TREE_TYPE (ll_arg), 0));
0425437e 5639
17529f98 5640 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
40c3c1b3 5641 {
5642 if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5643 return build2 (code, truth_type, lhs, rhs);
5644 return NULL_TREE;
5645 }
0425437e 5646 }
79109eec 5647
6f725368 5648 /* See if the comparisons can be merged. Then get all the parameters for
5649 each side. */
5650
2bc77e10 5651 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
6f725368 5652 || (rcode != EQ_EXPR && rcode != NE_EXPR))
2bc77e10 5653 return 0;
5654
79109eec 5655 volatilep = 0;
5656 ll_inner = decode_field_reference (ll_arg,
2bc77e10 5657 &ll_bitsize, &ll_bitpos, &ll_mode,
2a6329ae 5658 &ll_unsignedp, &volatilep, &ll_mask,
5659 &ll_and_mask);
79109eec 5660 lr_inner = decode_field_reference (lr_arg,
2bc77e10 5661 &lr_bitsize, &lr_bitpos, &lr_mode,
2a6329ae 5662 &lr_unsignedp, &volatilep, &lr_mask,
5663 &lr_and_mask);
79109eec 5664 rl_inner = decode_field_reference (rl_arg,
2bc77e10 5665 &rl_bitsize, &rl_bitpos, &rl_mode,
2a6329ae 5666 &rl_unsignedp, &volatilep, &rl_mask,
5667 &rl_and_mask);
79109eec 5668 rr_inner = decode_field_reference (rr_arg,
2bc77e10 5669 &rr_bitsize, &rr_bitpos, &rr_mode,
2a6329ae 5670 &rr_unsignedp, &volatilep, &rr_mask,
5671 &rr_and_mask);
2bc77e10 5672
5673 /* It must be true that the inner operation on the lhs of each
5674 comparison must be the same if we are to be able to do anything.
5675 Then see if we have constants. If not, the same must be true for
5676 the rhs's. */
5677 if (volatilep || ll_inner == 0 || rl_inner == 0
5678 || ! operand_equal_p (ll_inner, rl_inner, 0))
5679 return 0;
5680
79109eec 5681 if (TREE_CODE (lr_arg) == INTEGER_CST
5682 && TREE_CODE (rr_arg) == INTEGER_CST)
5683 l_const = lr_arg, r_const = rr_arg;
2bc77e10 5684 else if (lr_inner == 0 || rr_inner == 0
5685 || ! operand_equal_p (lr_inner, rr_inner, 0))
5686 return 0;
79109eec 5687 else
5688 l_const = r_const = 0;
2bc77e10 5689
5690 /* If either comparison code is not correct for our logical operation,
5691 fail. However, we can convert a one-bit comparison against zero into
5692 the opposite comparison against that bit being set in the field. */
79109eec 5693
76e4a18b 5694 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
2bc77e10 5695 if (lcode != wanted_code)
5696 {
5697 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
c6107ab0 5698 {
28bb328d 5699 /* Make the left operand unsigned, since we are only interested
5700 in the value of one bit. Otherwise we are doing the wrong
5701 thing below. */
5702 ll_unsignedp = 1;
68ae709d 5703 l_const = ll_mask;
c6107ab0 5704 }
2bc77e10 5705 else
5706 return 0;
5707 }
5708
68ae709d 5709 /* This is analogous to the code for l_const above. */
2bc77e10 5710 if (rcode != wanted_code)
5711 {
5712 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
c6107ab0 5713 {
28bb328d 5714 rl_unsignedp = 1;
68ae709d 5715 r_const = rl_mask;
c6107ab0 5716 }
2bc77e10 5717 else
5718 return 0;
5719 }
5720
5721 /* See if we can find a mode that contains both fields being compared on
5722 the left. If we can't, fail. Otherwise, update all constants and masks
5723 to be relative to a field of that size. */
5724 first_bit = MIN (ll_bitpos, rl_bitpos);
5725 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5726 lnmode = get_best_mode (end_bit - first_bit, first_bit,
5727 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5728 volatilep);
5729 if (lnmode == VOIDmode)
5730 return 0;
5731
5732 lnbitsize = GET_MODE_BITSIZE (lnmode);
5733 lnbitpos = first_bit & ~ (lnbitsize - 1);
fa8b888f 5734 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
2bc77e10 5735 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5736
51356f86 5737 if (BYTES_BIG_ENDIAN)
5738 {
5739 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5740 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5741 }
2bc77e10 5742
b30e3dbc 5743 ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5485823f 5744 size_int (xll_bitpos), 0);
b30e3dbc 5745 rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5485823f 5746 size_int (xrl_bitpos), 0);
2bc77e10 5747
2bc77e10 5748 if (l_const)
5749 {
b30e3dbc 5750 l_const = fold_convert (lntype, l_const);
cc049fa3 5751 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
94f29e88 5752 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5753 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
7ab7fd4f 5754 fold_build1 (BIT_NOT_EXPR,
5755 lntype, ll_mask),
94f29e88 5756 0)))
5757 {
c3ceba8e 5758 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
cc049fa3 5759
20783f07 5760 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
94f29e88 5761 }
2bc77e10 5762 }
5763 if (r_const)
5764 {
b30e3dbc 5765 r_const = fold_convert (lntype, r_const);
2a6329ae 5766 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
94f29e88 5767 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5768 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
7ab7fd4f 5769 fold_build1 (BIT_NOT_EXPR,
5770 lntype, rl_mask),
94f29e88 5771 0)))
5772 {
c3ceba8e 5773 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
be2828ce 5774
20783f07 5775 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
94f29e88 5776 }
2bc77e10 5777 }
5778
2a64c730 5779 /* If the right sides are not constant, do the same for it. Also,
5780 disallow this optimization if a size or signedness mismatch occurs
5781 between the left and right sides. */
5782 if (l_const == 0)
5783 {
5784 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5785 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5786 /* Make sure the two fields on the right
5787 correspond to the left without being swapped. */
5788 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5789 return 0;
5790
5791 first_bit = MIN (lr_bitpos, rr_bitpos);
5792 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5793 rnmode = get_best_mode (end_bit - first_bit, first_bit,
5794 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5795 volatilep);
5796 if (rnmode == VOIDmode)
5797 return 0;
5798
5799 rnbitsize = GET_MODE_BITSIZE (rnmode);
5800 rnbitpos = first_bit & ~ (rnbitsize - 1);
5801 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5802 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5803
5804 if (BYTES_BIG_ENDIAN)
5805 {
5806 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5807 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5808 }
5809
5810 lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
5811 size_int (xlr_bitpos), 0);
5812 rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
5813 size_int (xrr_bitpos), 0);
5814
5815 /* Make a mask that corresponds to both fields being compared.
5816 Do this for both items being compared. If the operands are the
5817 same size and the bits being compared are in the same position
5818 then we can do this by masking both and comparing the masked
5819 results. */
5820 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5821 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5822 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5823 {
5824 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5825 ll_unsignedp || rl_unsignedp);
5826 if (! all_ones_mask_p (ll_mask, lnbitsize))
5827 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5828
5829 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
5830 lr_unsignedp || rr_unsignedp);
5831 if (! all_ones_mask_p (lr_mask, rnbitsize))
5832 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5833
5834 return build2 (wanted_code, truth_type, lhs, rhs);
5835 }
5836
5837 /* There is still another way we can do something: If both pairs of
5838 fields being compared are adjacent, we may be able to make a wider
5839 field containing them both.
5840
5841 Note that we still must mask the lhs/rhs expressions. Furthermore,
5842 the mask must be shifted to account for the shift done by
5843 make_bit_field_ref. */
5844 if ((ll_bitsize + ll_bitpos == rl_bitpos
5845 && lr_bitsize + lr_bitpos == rr_bitpos)
5846 || (ll_bitpos == rl_bitpos + rl_bitsize
5847 && lr_bitpos == rr_bitpos + rr_bitsize))
5848 {
5849 tree type;
5850
5851 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
5852 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5853 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
5854 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5855
5856 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5857 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5858 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5859 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5860
5861 /* Convert to the smaller type before masking out unwanted bits. */
5862 type = lntype;
5863 if (lntype != rntype)
5864 {
5865 if (lnbitsize > rnbitsize)
5866 {
5867 lhs = fold_convert (rntype, lhs);
5868 ll_mask = fold_convert (rntype, ll_mask);
5869 type = rntype;
5870 }
5871 else if (lnbitsize < rnbitsize)
5872 {
5873 rhs = fold_convert (lntype, rhs);
5874 lr_mask = fold_convert (lntype, lr_mask);
5875 type = lntype;
5876 }
5877 }
5878
5879 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5880 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5881
5882 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5883 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5884
5885 return build2 (wanted_code, truth_type, lhs, rhs);
5886 }
5887
5888 return 0;
5889 }
5890
2bc77e10 5891 /* Handle the case of comparisons with constants. If there is something in
5892 common between the masks, those bits of the constants must be the same.
5893 If not, the condition is always false. Test for this to avoid generating
5894 incorrect code below. */
5485823f 5895 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
2bc77e10 5896 if (! integer_zerop (result)
5485823f 5897 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5898 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
2bc77e10 5899 {
5900 if (wanted_code == NE_EXPR)
5901 {
c3ceba8e 5902 warning (0, "%<or%> of unmatched not-equal tests is always 1");
20783f07 5903 return constant_boolean_node (true, truth_type);
2bc77e10 5904 }
5905 else
5906 {
c3ceba8e 5907 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
20783f07 5908 return constant_boolean_node (false, truth_type);
2bc77e10 5909 }
5910 }
5911
2a64c730 5912 /* Construct the expression we will return. First get the component
5913 reference we will make. Unless the mask is all ones the width of
5914 that field, perform the mask operation. Then compare with the
5915 merged constant. */
5916 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5917 ll_unsignedp || rl_unsignedp);
5918
5919 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5920 if (! all_ones_mask_p (ll_mask, lnbitsize))
5921 result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5922
5923 return build2 (wanted_code, truth_type, result,
5924 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
2bc77e10 5925}
5926\f
cc049fa3 5927/* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
155b05dc 5928 constant. */
5929
5930static tree
155acab4 5931optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
155b05dc 5932{
155acab4 5933 tree arg0 = op0;
155b05dc 5934 enum tree_code op_code;
226c5ce4 5935 tree comp_const;
155b05dc 5936 tree minmax_const;
5937 int consts_equal, consts_lt;
5938 tree inner;
5939
5940 STRIP_SIGN_NOPS (arg0);
5941
5942 op_code = TREE_CODE (arg0);
5943 minmax_const = TREE_OPERAND (arg0, 1);
226c5ce4 5944 comp_const = fold_convert (TREE_TYPE (arg0), op1);
155b05dc 5945 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5946 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5947 inner = TREE_OPERAND (arg0, 0);
5948
5949 /* If something does not permit us to optimize, return the original tree. */
5950 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5951 || TREE_CODE (comp_const) != INTEGER_CST
f96bd2bf 5952 || TREE_OVERFLOW (comp_const)
155b05dc 5953 || TREE_CODE (minmax_const) != INTEGER_CST
f96bd2bf 5954 || TREE_OVERFLOW (minmax_const))
155acab4 5955 return NULL_TREE;
155b05dc 5956
5957 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5958 and GT_EXPR, doing the rest with recursive calls using logical
5959 simplifications. */
155acab4 5960 switch (code)
155b05dc 5961 {
5962 case NE_EXPR: case LT_EXPR: case LE_EXPR:
155acab4 5963 {
6758b11c 5964 tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5965 type, op0, op1);
5966 if (tem)
5967 return invert_truthvalue (tem);
5968 return NULL_TREE;
155acab4 5969 }
155b05dc 5970
5971 case GE_EXPR:
5972 return
7ab7fd4f 5973 fold_build2 (TRUTH_ORIF_EXPR, type,
5974 optimize_minmax_comparison
5975 (EQ_EXPR, type, arg0, comp_const),
5976 optimize_minmax_comparison
5977 (GT_EXPR, type, arg0, comp_const));
155b05dc 5978
5979 case EQ_EXPR:
5980 if (op_code == MAX_EXPR && consts_equal)
5981 /* MAX (X, 0) == 0 -> X <= 0 */
7ab7fd4f 5982 return fold_build2 (LE_EXPR, type, inner, comp_const);
155b05dc 5983
5984 else if (op_code == MAX_EXPR && consts_lt)
5985 /* MAX (X, 0) == 5 -> X == 5 */
7ab7fd4f 5986 return fold_build2 (EQ_EXPR, type, inner, comp_const);
155b05dc 5987
5988 else if (op_code == MAX_EXPR)
5989 /* MAX (X, 0) == -1 -> false */
5990 return omit_one_operand (type, integer_zero_node, inner);
5991
5992 else if (consts_equal)
5993 /* MIN (X, 0) == 0 -> X >= 0 */
7ab7fd4f 5994 return fold_build2 (GE_EXPR, type, inner, comp_const);
155b05dc 5995
5996 else if (consts_lt)
5997 /* MIN (X, 0) == 5 -> false */
5998 return omit_one_operand (type, integer_zero_node, inner);
5999
6000 else
6001 /* MIN (X, 0) == -1 -> X == -1 */
7ab7fd4f 6002 return fold_build2 (EQ_EXPR, type, inner, comp_const);
155b05dc 6003
6004 case GT_EXPR:
6005 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
6006 /* MAX (X, 0) > 0 -> X > 0
6007 MAX (X, 0) > 5 -> X > 5 */
7ab7fd4f 6008 return fold_build2 (GT_EXPR, type, inner, comp_const);
155b05dc 6009
6010 else if (op_code == MAX_EXPR)
6011 /* MAX (X, 0) > -1 -> true */
6012 return omit_one_operand (type, integer_one_node, inner);
6013
6014 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
6015 /* MIN (X, 0) > 0 -> false
6016 MIN (X, 0) > 5 -> false */
6017 return omit_one_operand (type, integer_zero_node, inner);
6018
6019 else
6020 /* MIN (X, 0) > -1 -> X > -1 */
7ab7fd4f 6021 return fold_build2 (GT_EXPR, type, inner, comp_const);
155b05dc 6022
6023 default:
155acab4 6024 return NULL_TREE;
155b05dc 6025 }
6026}
6027\f
23ec2d5e 6028/* T is an integer expression that is being multiplied, divided, or taken a
6029 modulus (CODE says which and what kind of divide or modulus) by a
6030 constant C. See if we can eliminate that operation by folding it with
6031 other operations already in T. WIDE_TYPE, if non-null, is a type that
6032 should be used for the computation if wider than our type.
6033
b07ba9ff 6034 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6035 (X * 2) + (Y * 4). We must, however, be assured that either the original
2f5cf552 6036 expression would not overflow or that overflow is undefined for the type
6037 in the language in question.
6038
23ec2d5e 6039 If we return a non-null expression, it is an equivalent form of the
add6ee5e 6040 original computation, but need not be in the original type.
6041
6042 We set *STRICT_OVERFLOW_P to true if the return values depends on
6043 signed overflow being undefined. Otherwise we do not change
6044 *STRICT_OVERFLOW_P. */
23ec2d5e 6045
6046static tree
add6ee5e 6047extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6048 bool *strict_overflow_p)
009f6e1c 6049{
6050 /* To avoid exponential search depth, refuse to allow recursion past
6051 three levels. Beyond that (1) it's highly unlikely that we'll find
6052 something interesting and (2) we've probably processed it before
6053 when we built the inner expression. */
6054
6055 static int depth;
6056 tree ret;
6057
6058 if (depth > 3)
6059 return NULL;
6060
6061 depth++;
add6ee5e 6062 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
009f6e1c 6063 depth--;
6064
6065 return ret;
6066}
6067
6068static tree
add6ee5e 6069extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6070 bool *strict_overflow_p)
23ec2d5e 6071{
6072 tree type = TREE_TYPE (t);
6073 enum tree_code tcode = TREE_CODE (t);
cc049fa3 6074 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
23ec2d5e 6075 > GET_MODE_SIZE (TYPE_MODE (type)))
6076 ? wide_type : type);
6077 tree t1, t2;
6078 int same_p = tcode == code;
03435587 6079 tree op0 = NULL_TREE, op1 = NULL_TREE;
add6ee5e 6080 bool sub_strict_overflow_p;
23ec2d5e 6081
6082 /* Don't deal with constants of zero here; they confuse the code below. */
6083 if (integer_zerop (c))
2f5cf552 6084 return NULL_TREE;
23ec2d5e 6085
ce45a448 6086 if (TREE_CODE_CLASS (tcode) == tcc_unary)
23ec2d5e 6087 op0 = TREE_OPERAND (t, 0);
6088
ce45a448 6089 if (TREE_CODE_CLASS (tcode) == tcc_binary)
23ec2d5e 6090 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6091
6092 /* Note that we need not handle conditional operations here since fold
6093 already handles those cases. So just do arithmetic here. */
6094 switch (tcode)
6095 {
6096 case INTEGER_CST:
6097 /* For a constant, we can always simplify if we are a multiply
6098 or (for divide and modulus) if it is a multiple of our constant. */
6099 if (code == MULT_EXPR
6100 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
b30e3dbc 6101 return const_binop (code, fold_convert (ctype, t),
6102 fold_convert (ctype, c), 0);
23ec2d5e 6103 break;
6104
72dd6141 6105 CASE_CONVERT: case NON_LVALUE_EXPR:
12480406 6106 /* If op0 is an expression ... */
ce45a448 6107 if ((COMPARISON_CLASS_P (op0)
6108 || UNARY_CLASS_P (op0)
6109 || BINARY_CLASS_P (op0)
c2f47e15 6110 || VL_EXP_CLASS_P (op0)
ce45a448 6111 || EXPRESSION_CLASS_P (op0))
32054974 6112 /* ... and has wrapping overflow, and its type is smaller
6113 than ctype, then we cannot pass through as widening. */
6114 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
12480406 6115 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6116 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
32054974 6117 && (TYPE_PRECISION (ctype)
6118 > TYPE_PRECISION (TREE_TYPE (op0))))
40309554 6119 /* ... or this is a truncation (t is narrower than op0),
6120 then we cannot pass through this narrowing. */
32054974 6121 || (TYPE_PRECISION (type)
6122 < TYPE_PRECISION (TREE_TYPE (op0)))
cee280ef 6123 /* ... or signedness changes for division or modulus,
6124 then we cannot pass through this conversion. */
6125 || (code != MULT_EXPR
78a8ed03 6126 && (TYPE_UNSIGNED (ctype)
28fa8094 6127 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6128 /* ... or has undefined overflow while the converted to
6129 type has not, we cannot do the operation in the inner type
6130 as that would introduce undefined overflow. */
6131 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6132 && !TYPE_OVERFLOW_UNDEFINED (type))))
3cb1a3c6 6133 break;
6134
23ec2d5e 6135 /* Pass the constant down and see if we can make a simplification. If
5f0002b0 6136 we can, replace this expression with the inner simplification for
6137 possible later conversion to our or some other type. */
b30e3dbc 6138 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
f2fa1510 6139 && TREE_CODE (t2) == INTEGER_CST
f96bd2bf 6140 && !TREE_OVERFLOW (t2)
f2fa1510 6141 && (0 != (t1 = extract_muldiv (op0, t2, code,
6142 code == MULT_EXPR
add6ee5e 6143 ? ctype : NULL_TREE,
6144 strict_overflow_p))))
23ec2d5e 6145 return t1;
6146 break;
6147
24877233 6148 case ABS_EXPR:
6149 /* If widening the type changes it from signed to unsigned, then we
6150 must avoid building ABS_EXPR itself as unsigned. */
6151 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6152 {
11773141 6153 tree cstype = (*signed_type_for) (ctype);
add6ee5e 6154 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6155 != 0)
24877233 6156 {
7ab7fd4f 6157 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
24877233 6158 return fold_convert (ctype, t1);
6159 }
6160 break;
6161 }
d8100984 6162 /* If the constant is negative, we cannot simplify this. */
6163 if (tree_int_cst_sgn (c) == -1)
6164 break;
24877233 6165 /* FALLTHROUGH */
6166 case NEGATE_EXPR:
add6ee5e 6167 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6168 != 0)
7ab7fd4f 6169 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
23ec2d5e 6170 break;
6171
6172 case MIN_EXPR: case MAX_EXPR:
6269027b 6173 /* If widening the type changes the signedness, then we can't perform
6174 this optimization as that changes the result. */
78a8ed03 6175 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6269027b 6176 break;
6177
23ec2d5e 6178 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
add6ee5e 6179 sub_strict_overflow_p = false;
6180 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6181 &sub_strict_overflow_p)) != 0
6182 && (t2 = extract_muldiv (op1, c, code, wide_type,
6183 &sub_strict_overflow_p)) != 0)
5f0002b0 6184 {
6185 if (tree_int_cst_sgn (c) < 0)
6186 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
add6ee5e 6187 if (sub_strict_overflow_p)
6188 *strict_overflow_p = true;
7ab7fd4f 6189 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6190 fold_convert (ctype, t2));
5f0002b0 6191 }
23ec2d5e 6192 break;
6193
23ec2d5e 6194 case LSHIFT_EXPR: case RSHIFT_EXPR:
6195 /* If the second operand is constant, this is a multiplication
6196 or floor division, by a power of two, so we can treat it that
dceee6fb 6197 way unless the multiplier or divisor overflows. Signed
6198 left-shift overflow is implementation-defined rather than
6199 undefined in C90, so do not convert signed left shift into
6200 multiplication. */
23ec2d5e 6201 if (TREE_CODE (op1) == INTEGER_CST
dceee6fb 6202 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
c011f821 6203 /* const_binop may not detect overflow correctly,
6204 so check for it explicitly here. */
6205 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6206 && TREE_INT_CST_HIGH (op1) == 0
b30e3dbc 6207 && 0 != (t1 = fold_convert (ctype,
6208 const_binop (LSHIFT_EXPR,
6209 size_one_node,
6210 op1, 0)))
f96bd2bf 6211 && !TREE_OVERFLOW (t1))
fd96eeef 6212 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6213 ? MULT_EXPR : FLOOR_DIV_EXPR,
6214 ctype, fold_convert (ctype, op0), t1),
add6ee5e 6215 c, code, wide_type, strict_overflow_p);
23ec2d5e 6216 break;
6217
6218 case PLUS_EXPR: case MINUS_EXPR:
6219 /* See if we can eliminate the operation on both sides. If we can, we
6220 can return a new PLUS or MINUS. If we can't, the only remaining
6221 cases where we can do anything are if the second operand is a
6222 constant. */
add6ee5e 6223 sub_strict_overflow_p = false;
6224 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6225 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
17e3940f 6226 if (t1 != 0 && t2 != 0
6227 && (code == MULT_EXPR
e5b30d78 6228 /* If not multiplication, we can only do this if both operands
6229 are divisible by c. */
6230 || (multiple_of_p (ctype, op0, c)
6231 && multiple_of_p (ctype, op1, c))))
add6ee5e 6232 {
6233 if (sub_strict_overflow_p)
6234 *strict_overflow_p = true;
6235 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6236 fold_convert (ctype, t2));
6237 }
23ec2d5e 6238
5f0002b0 6239 /* If this was a subtraction, negate OP1 and set it to be an addition.
6240 This simplifies the logic below. */
6241 if (tcode == MINUS_EXPR)
6242 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6243
ec4d93b0 6244 if (TREE_CODE (op1) != INTEGER_CST)
6245 break;
6246
5f0002b0 6247 /* If either OP1 or C are negative, this optimization is not safe for
6248 some of the division and remainder types while for others we need
6249 to change the code. */
6250 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6251 {
6252 if (code == CEIL_DIV_EXPR)
6253 code = FLOOR_DIV_EXPR;
5f0002b0 6254 else if (code == FLOOR_DIV_EXPR)
6255 code = CEIL_DIV_EXPR;
b575bb01 6256 else if (code != MULT_EXPR
6257 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5f0002b0 6258 break;
6259 }
6260
98248b34 6261 /* If it's a multiply or a division/modulus operation of a multiple
6262 of our constant, do the operation and verify it doesn't overflow. */
6263 if (code == MULT_EXPR
6264 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
d3371fcd 6265 {
b30e3dbc 6266 op1 = const_binop (code, fold_convert (ctype, op1),
6267 fold_convert (ctype, c), 0);
f5c47dd7 6268 /* We allow the constant to overflow with wrapping semantics. */
6269 if (op1 == 0
981eb798 6270 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
d3371fcd 6271 break;
6272 }
98248b34 6273 else
d3371fcd 6274 break;
5f0002b0 6275
fc452262 6276 /* If we have an unsigned type is not a sizetype, we cannot widen
6277 the operation since it will change the result if the original
6278 computation overflowed. */
78a8ed03 6279 if (TYPE_UNSIGNED (ctype)
d490e2f2 6280 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
fc452262 6281 && ctype != type)
6282 break;
6283
23ec2d5e 6284 /* If we were able to eliminate our operation from the first side,
5f0002b0 6285 apply our operation to the second side and reform the PLUS. */
6286 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
7ab7fd4f 6287 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
23ec2d5e 6288
6289 /* The last case is if we are a multiply. In that case, we can
6290 apply the distributive law to commute the multiply and addition
6312a35e 6291 if the multiplication of the constants doesn't overflow. */
5f0002b0 6292 if (code == MULT_EXPR)
7ab7fd4f 6293 return fold_build2 (tcode, ctype,
6294 fold_build2 (code, ctype,
6295 fold_convert (ctype, op0),
6296 fold_convert (ctype, c)),
6297 op1);
23ec2d5e 6298
6299 break;
6300
6301 case MULT_EXPR:
6302 /* We have a special case here if we are doing something like
6303 (C * 8) % 4 since we know that's zero. */
6304 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6305 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
67f36f78 6306 /* If the multiplication can overflow we cannot optimize this.
6307 ??? Until we can properly mark individual operations as
6308 not overflowing we need to treat sizetype special here as
6309 stor-layout relies on this opimization to make
6310 DECL_FIELD_BIT_OFFSET always a constant. */
6311 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6312 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6313 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
23ec2d5e 6314 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6315 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
67f36f78 6316 {
6317 *strict_overflow_p = true;
6318 return omit_one_operand (type, integer_zero_node, op0);
6319 }
23ec2d5e 6320
6312a35e 6321 /* ... fall through ... */
23ec2d5e 6322
6323 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6324 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6325 /* If we can extract our operation from the LHS, do so and return a
6326 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6327 do something only if the second operand is a constant. */
6328 if (same_p
add6ee5e 6329 && (t1 = extract_muldiv (op0, c, code, wide_type,
6330 strict_overflow_p)) != 0)
7ab7fd4f 6331 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6332 fold_convert (ctype, op1));
23ec2d5e 6333 else if (tcode == MULT_EXPR && code == MULT_EXPR
add6ee5e 6334 && (t1 = extract_muldiv (op1, c, code, wide_type,
6335 strict_overflow_p)) != 0)
7ab7fd4f 6336 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6337 fold_convert (ctype, t1));
23ec2d5e 6338 else if (TREE_CODE (op1) != INTEGER_CST)
6339 return 0;
6340
6341 /* If these are the same operation types, we can associate them
6342 assuming no overflow. */
6343 if (tcode == code
c79abec2 6344 && 0 != (t1 = int_const_binop (MULT_EXPR, fold_convert (ctype, op1),
6345 fold_convert (ctype, c), 1))
6346 && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
6347 TREE_INT_CST_HIGH (t1),
6348 (TYPE_UNSIGNED (ctype)
6349 && tcode != MULT_EXPR) ? -1 : 1,
6350 TREE_OVERFLOW (t1)))
f96bd2bf 6351 && !TREE_OVERFLOW (t1))
7ab7fd4f 6352 return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
23ec2d5e 6353
6354 /* If these operations "cancel" each other, we have the main
6355 optimizations of this pass, which occur when either constant is a
6356 multiple of the other, in which case we replace this with either an
cc049fa3 6357 operation or CODE or TCODE.
2f5cf552 6358
35a3065a 6359 If we have an unsigned type that is not a sizetype, we cannot do
2f5cf552 6360 this since it will change the result if the original computation
6361 overflowed. */
981eb798 6362 if ((TYPE_OVERFLOW_UNDEFINED (ctype)
d490e2f2 6363 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
2f5cf552 6364 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6365 || (tcode == MULT_EXPR
6366 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
c50ab071 6367 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6368 && code != MULT_EXPR)))
23ec2d5e 6369 {
6370 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
add6ee5e 6371 {
6372 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6373 *strict_overflow_p = true;
6374 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6375 fold_convert (ctype,
6376 const_binop (TRUNC_DIV_EXPR,
6377 op1, c, 0)));
6378 }
23ec2d5e 6379 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
add6ee5e 6380 {
6381 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6382 *strict_overflow_p = true;
6383 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6384 fold_convert (ctype,
6385 const_binop (TRUNC_DIV_EXPR,
6386 c, op1, 0)));
6387 }
23ec2d5e 6388 }
6389 break;
6390
6391 default:
6392 break;
6393 }
6394
6395 return 0;
6396}
6397\f
b4af30fd 6398/* Return a node which has the indicated constant VALUE (either 0 or
6399 1), and is of the indicated TYPE. */
6400
5c9198bd 6401tree
de1b648b 6402constant_boolean_node (int value, tree type)
b4af30fd 6403{
6404 if (type == integer_type_node)
6405 return value ? integer_one_node : integer_zero_node;
c4e122e7 6406 else if (type == boolean_type_node)
6407 return value ? boolean_true_node : boolean_false_node;
cc049fa3 6408 else
7016c612 6409 return build_int_cst (type, value);
b4af30fd 6410}
6411
6d24c9aa 6412
203a24c4 6413/* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
47cbd05d 6414 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6415 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6ef828f9 6416 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
47cbd05d 6417 COND is the first argument to CODE; otherwise (as in the example
6418 given here), it is the second argument. TYPE is the type of the
9c9bad97 6419 original expression. Return NULL_TREE if no simplification is
a6661800 6420 possible. */
47cbd05d 6421
6422static tree
1ebe9a83 6423fold_binary_op_with_conditional_arg (enum tree_code code,
6424 tree type, tree op0, tree op1,
6425 tree cond, tree arg, int cond_first_p)
47cbd05d 6426{
1ebe9a83 6427 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
84b251e4 6428 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
47cbd05d 6429 tree test, true_value, false_value;
6430 tree lhs = NULL_TREE;
6431 tree rhs = NULL_TREE;
a6661800 6432
f2b83d13 6433 /* This transformation is only worthwhile if we don't have to wrap
0975351b 6434 arg in a SAVE_EXPR, and the operation can be simplified on at least
f2b83d13 6435 one of the branches once its pushed inside the COND_EXPR. */
6436 if (!TREE_CONSTANT (arg))
a6661800 6437 return NULL_TREE;
6438
47cbd05d 6439 if (TREE_CODE (cond) == COND_EXPR)
6440 {
6441 test = TREE_OPERAND (cond, 0);
6442 true_value = TREE_OPERAND (cond, 1);
6443 false_value = TREE_OPERAND (cond, 2);
6444 /* If this operand throws an expression, then it does not make
6445 sense to try to perform a logical or arithmetic operation
f2b83d13 6446 involving it. */
47cbd05d 6447 if (VOID_TYPE_P (TREE_TYPE (true_value)))
f2b83d13 6448 lhs = true_value;
47cbd05d 6449 if (VOID_TYPE_P (TREE_TYPE (false_value)))
f2b83d13 6450 rhs = false_value;
47cbd05d 6451 }
6452 else
6453 {
6454 tree testtype = TREE_TYPE (cond);
6455 test = cond;
20783f07 6456 true_value = constant_boolean_node (true, testtype);
6457 false_value = constant_boolean_node (false, testtype);
47cbd05d 6458 }
d3371fcd 6459
5fe1fe72 6460 arg = fold_convert (arg_type, arg);
47cbd05d 6461 if (lhs == 0)
5fe1fe72 6462 {
6463 true_value = fold_convert (cond_type, true_value);
b085d4e5 6464 if (cond_first_p)
6465 lhs = fold_build2 (code, type, true_value, arg);
6466 else
6467 lhs = fold_build2 (code, type, arg, true_value);
5fe1fe72 6468 }
47cbd05d 6469 if (rhs == 0)
5fe1fe72 6470 {
6471 false_value = fold_convert (cond_type, false_value);
b085d4e5 6472 if (cond_first_p)
6473 rhs = fold_build2 (code, type, false_value, arg);
6474 else
6475 rhs = fold_build2 (code, type, arg, false_value);
5fe1fe72 6476 }
f2b83d13 6477
7ab7fd4f 6478 test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
f2b83d13 6479 return fold_convert (type, test);
47cbd05d 6480}
6481
be2828ce 6482\f
920d0fb5 6483/* Subroutine of fold() that checks for the addition of +/- 0.0.
6484
6485 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6486 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6487 ADDEND is the same as X.
6488
6ef828f9 6489 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
920d0fb5 6490 and finite. The problematic cases are when X is zero, and its mode
6491 has signed zeros. In the case of rounding towards -infinity,
6492 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6493 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6494
46ef5347 6495bool
b4b34335 6496fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
920d0fb5 6497{
6498 if (!real_zerop (addend))
6499 return false;
6500
c7590f7e 6501 /* Don't allow the fold with -fsignaling-nans. */
6502 if (HONOR_SNANS (TYPE_MODE (type)))
6503 return false;
6504
920d0fb5 6505 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6506 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6507 return true;
6508
6509 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6510 if (TREE_CODE (addend) == REAL_CST
6511 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6512 negate = !negate;
6513
6514 /* The mode has signed zeros, and we have to honor their sign.
6515 In this situation, there is only one case we can return true for.
6516 X - 0 is the same as X unless rounding towards -infinity is
6517 supported. */
6518 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6519}
6520
4b0b9adb 6521/* Subroutine of fold() that checks comparisons of built-in math
6522 functions against real constants.
6523
6524 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6525 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6526 is the type of the result and ARG0 and ARG1 are the operands of the
6527 comparison. ARG1 must be a TREE_REAL_CST.
6528
6529 The function returns the constant folded tree if a simplification
6530 can be made, and NULL_TREE otherwise. */
6531
6532static tree
dc81944a 6533fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6534 tree type, tree arg0, tree arg1)
4b0b9adb 6535{
6536 REAL_VALUE_TYPE c;
6537
852da3c3 6538 if (BUILTIN_SQRT_P (fcode))
4b0b9adb 6539 {
c2f47e15 6540 tree arg = CALL_EXPR_ARG (arg0, 0);
4b0b9adb 6541 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6542
6543 c = TREE_REAL_CST (arg1);
6544 if (REAL_VALUE_NEGATIVE (c))
6545 {
6546 /* sqrt(x) < y is always false, if y is negative. */
6547 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
20783f07 6548 return omit_one_operand (type, integer_zero_node, arg);
4b0b9adb 6549
6550 /* sqrt(x) > y is always true, if y is negative and we
6551 don't care about NaNs, i.e. negative values of x. */
6552 if (code == NE_EXPR || !HONOR_NANS (mode))
20783f07 6553 return omit_one_operand (type, integer_one_node, arg);
4b0b9adb 6554
6555 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
7ab7fd4f 6556 return fold_build2 (GE_EXPR, type, arg,
6557 build_real (TREE_TYPE (arg), dconst0));
4b0b9adb 6558 }
6559 else if (code == GT_EXPR || code == GE_EXPR)
6560 {
6561 REAL_VALUE_TYPE c2;
6562
6563 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6564 real_convert (&c2, mode, &c2);
6565
6566 if (REAL_VALUE_ISINF (c2))
6567 {
6568 /* sqrt(x) > y is x == +Inf, when y is very large. */
6569 if (HONOR_INFINITIES (mode))
7ab7fd4f 6570 return fold_build2 (EQ_EXPR, type, arg,
6571 build_real (TREE_TYPE (arg), c2));
4b0b9adb 6572
6573 /* sqrt(x) > y is always false, when y is very large
6574 and we don't care about infinities. */
20783f07 6575 return omit_one_operand (type, integer_zero_node, arg);
4b0b9adb 6576 }
6577
6578 /* sqrt(x) > c is the same as x > c*c. */
7ab7fd4f 6579 return fold_build2 (code, type, arg,
6580 build_real (TREE_TYPE (arg), c2));
4b0b9adb 6581 }
6582 else if (code == LT_EXPR || code == LE_EXPR)
6583 {
6584 REAL_VALUE_TYPE c2;
6585
6586 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6587 real_convert (&c2, mode, &c2);
6588
6589 if (REAL_VALUE_ISINF (c2))
6590 {
6591 /* sqrt(x) < y is always true, when y is a very large
6592 value and we don't care about NaNs or Infinities. */
6593 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
20783f07 6594 return omit_one_operand (type, integer_one_node, arg);
4b0b9adb 6595
6596 /* sqrt(x) < y is x != +Inf when y is very large and we
6597 don't care about NaNs. */
6598 if (! HONOR_NANS (mode))
7ab7fd4f 6599 return fold_build2 (NE_EXPR, type, arg,
6600 build_real (TREE_TYPE (arg), c2));
4b0b9adb 6601
6602 /* sqrt(x) < y is x >= 0 when y is very large and we
6603 don't care about Infinities. */
6604 if (! HONOR_INFINITIES (mode))
7ab7fd4f 6605 return fold_build2 (GE_EXPR, type, arg,
6606 build_real (TREE_TYPE (arg), dconst0));
4b0b9adb 6607
6608 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
fa8b888f 6609 if (lang_hooks.decls.global_bindings_p () != 0
ce3fb06e 6610 || CONTAINS_PLACEHOLDER_P (arg))
4b0b9adb 6611 return NULL_TREE;
6612
6613 arg = save_expr (arg);
7ab7fd4f 6614 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6615 fold_build2 (GE_EXPR, type, arg,
6616 build_real (TREE_TYPE (arg),
6617 dconst0)),
6618 fold_build2 (NE_EXPR, type, arg,
6619 build_real (TREE_TYPE (arg),
6620 c2)));
4b0b9adb 6621 }
6622
6623 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6624 if (! HONOR_NANS (mode))
7ab7fd4f 6625 return fold_build2 (code, type, arg,
6626 build_real (TREE_TYPE (arg), c2));
4b0b9adb 6627
6628 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
fa8b888f 6629 if (lang_hooks.decls.global_bindings_p () == 0
ce3fb06e 6630 && ! CONTAINS_PLACEHOLDER_P (arg))
4b0b9adb 6631 {
6632 arg = save_expr (arg);
7ab7fd4f 6633 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6634 fold_build2 (GE_EXPR, type, arg,
6635 build_real (TREE_TYPE (arg),
6636 dconst0)),
6637 fold_build2 (code, type, arg,
6638 build_real (TREE_TYPE (arg),
6639 c2)));
4b0b9adb 6640 }
6641 }
6642 }
6643
6644 return NULL_TREE;
6645}
6646
6d2e901f 6647/* Subroutine of fold() that optimizes comparisons against Infinities,
6648 either +Inf or -Inf.
6649
6650 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6651 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6652 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6653
6654 The function returns the constant folded tree if a simplification
6655 can be made, and NULL_TREE otherwise. */
6656
6657static tree
de1b648b 6658fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6d2e901f 6659{
ac4bd9a0 6660 enum machine_mode mode;
6661 REAL_VALUE_TYPE max;
6662 tree temp;
6663 bool neg;
6664
6665 mode = TYPE_MODE (TREE_TYPE (arg0));
6666
6d2e901f 6667 /* For negative infinity swap the sense of the comparison. */
ac4bd9a0 6668 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6669 if (neg)
6d2e901f 6670 code = swap_tree_comparison (code);
6671
6672 switch (code)
6673 {
6674 case GT_EXPR:
6675 /* x > +Inf is always false, if with ignore sNANs. */
ac4bd9a0 6676 if (HONOR_SNANS (mode))
6d2e901f 6677 return NULL_TREE;
20783f07 6678 return omit_one_operand (type, integer_zero_node, arg0);
6d2e901f 6679
6680 case LE_EXPR:
6681 /* x <= +Inf is always true, if we don't case about NaNs. */
ac4bd9a0 6682 if (! HONOR_NANS (mode))
20783f07 6683 return omit_one_operand (type, integer_one_node, arg0);
6d2e901f 6684
6685 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
fa8b888f 6686 if (lang_hooks.decls.global_bindings_p () == 0
ce3fb06e 6687 && ! CONTAINS_PLACEHOLDER_P (arg0))
6d2e901f 6688 {
6689 arg0 = save_expr (arg0);
7ab7fd4f 6690 return fold_build2 (EQ_EXPR, type, arg0, arg0);
6d2e901f 6691 }
6692 break;
6693
ac4bd9a0 6694 case EQ_EXPR:
6695 case GE_EXPR:
6696 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6697 real_maxval (&max, neg, mode);
7ab7fd4f 6698 return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6699 arg0, build_real (TREE_TYPE (arg0), max));
ac4bd9a0 6700
6701 case LT_EXPR:
6702 /* x < +Inf is always equal to x <= DBL_MAX. */
6703 real_maxval (&max, neg, mode);
7ab7fd4f 6704 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6705 arg0, build_real (TREE_TYPE (arg0), max));
ac4bd9a0 6706
6707 case NE_EXPR:
6708 /* x != +Inf is always equal to !(x > DBL_MAX). */
6709 real_maxval (&max, neg, mode);
6710 if (! HONOR_NANS (mode))
7ab7fd4f 6711 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6712 arg0, build_real (TREE_TYPE (arg0), max));
bd1ec513 6713
7ab7fd4f 6714 temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6715 arg0, build_real (TREE_TYPE (arg0), max));
6716 return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6d2e901f 6717
6718 default:
6719 break;
6720 }
6721
6722 return NULL_TREE;
6723}
920d0fb5 6724
270029e0 6725/* Subroutine of fold() that optimizes comparisons of a division by
365db11e 6726 a nonzero integer constant against an integer constant, i.e.
270029e0 6727 X/C1 op C2.
6728
6729 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6730 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6731 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6732
6733 The function returns the constant folded tree if a simplification
6734 can be made, and NULL_TREE otherwise. */
6735
6736static tree
6737fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6738{
6739 tree prod, tmp, hi, lo;
6740 tree arg00 = TREE_OPERAND (arg0, 0);
6741 tree arg01 = TREE_OPERAND (arg0, 1);
6742 unsigned HOST_WIDE_INT lpart;
6743 HOST_WIDE_INT hpart;
81035ec1 6744 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
4e35b483 6745 bool neg_overflow;
270029e0 6746 int overflow;
6747
6748 /* We have to do this the hard way to detect unsigned overflow.
6749 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
81035ec1 6750 overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6751 TREE_INT_CST_HIGH (arg01),
6752 TREE_INT_CST_LOW (arg1),
6753 TREE_INT_CST_HIGH (arg1),
6754 &lpart, &hpart, unsigned_p);
c8110c8f 6755 prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
eddad94a 6756 -1, overflow);
4e35b483 6757 neg_overflow = false;
270029e0 6758
81035ec1 6759 if (unsigned_p)
270029e0 6760 {
2455d3ef 6761 tmp = int_const_binop (MINUS_EXPR, arg01,
6762 build_int_cst (TREE_TYPE (arg01), 1), 0);
270029e0 6763 lo = prod;
6764
6765 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
81035ec1 6766 overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6767 TREE_INT_CST_HIGH (prod),
6768 TREE_INT_CST_LOW (tmp),
6769 TREE_INT_CST_HIGH (tmp),
6770 &lpart, &hpart, unsigned_p);
c8110c8f 6771 hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
eddad94a 6772 -1, overflow | TREE_OVERFLOW (prod));
270029e0 6773 }
6774 else if (tree_int_cst_sgn (arg01) >= 0)
6775 {
2455d3ef 6776 tmp = int_const_binop (MINUS_EXPR, arg01,
6777 build_int_cst (TREE_TYPE (arg01), 1), 0);
270029e0 6778 switch (tree_int_cst_sgn (arg1))
6779 {
6780 case -1:
4e35b483 6781 neg_overflow = true;
270029e0 6782 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6783 hi = prod;
6784 break;
6785
6786 case 0:
6787 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6788 hi = tmp;
6789 break;
6790
6791 case 1:
6792 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6793 lo = prod;
6794 break;
6795
6796 default:
fdada98f 6797 gcc_unreachable ();
270029e0 6798 }
6799 }
6800 else
6801 {
460c8e36 6802 /* A negative divisor reverses the relational operators. */
6803 code = swap_tree_comparison (code);
6804
2455d3ef 6805 tmp = int_const_binop (PLUS_EXPR, arg01,
6806 build_int_cst (TREE_TYPE (arg01), 1), 0);
270029e0 6807 switch (tree_int_cst_sgn (arg1))
6808 {
6809 case -1:
6810 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6811 lo = prod;
6812 break;
6813
6814 case 0:
6815 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6816 lo = tmp;
6817 break;
6818
6819 case 1:
4e35b483 6820 neg_overflow = true;
6821 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
270029e0 6822 hi = prod;
6823 break;
6824
6825 default:
fdada98f 6826 gcc_unreachable ();
270029e0 6827 }
6828 }
6829
6830 switch (code)
6831 {
6832 case EQ_EXPR:
6833 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6834 return omit_one_operand (type, integer_zero_node, arg00);
6835 if (TREE_OVERFLOW (hi))
7ab7fd4f 6836 return fold_build2 (GE_EXPR, type, arg00, lo);
270029e0 6837 if (TREE_OVERFLOW (lo))
7ab7fd4f 6838 return fold_build2 (LE_EXPR, type, arg00, hi);
270029e0 6839 return build_range_check (type, arg00, 1, lo, hi);
6840
6841 case NE_EXPR:
6842 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6843 return omit_one_operand (type, integer_one_node, arg00);
6844 if (TREE_OVERFLOW (hi))
7ab7fd4f 6845 return fold_build2 (LT_EXPR, type, arg00, lo);
270029e0 6846 if (TREE_OVERFLOW (lo))
7ab7fd4f 6847 return fold_build2 (GT_EXPR, type, arg00, hi);
270029e0 6848 return build_range_check (type, arg00, 0, lo, hi);
6849
6850 case LT_EXPR:
6851 if (TREE_OVERFLOW (lo))
4e35b483 6852 {
6853 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6854 return omit_one_operand (type, tmp, arg00);
6855 }
7ab7fd4f 6856 return fold_build2 (LT_EXPR, type, arg00, lo);
270029e0 6857
6858 case LE_EXPR:
6859 if (TREE_OVERFLOW (hi))
4e35b483 6860 {
6861 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6862 return omit_one_operand (type, tmp, arg00);
6863 }
7ab7fd4f 6864 return fold_build2 (LE_EXPR, type, arg00, hi);
270029e0 6865
6866 case GT_EXPR:
6867 if (TREE_OVERFLOW (hi))
4e35b483 6868 {
6869 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6870 return omit_one_operand (type, tmp, arg00);
6871 }
7ab7fd4f 6872 return fold_build2 (GT_EXPR, type, arg00, hi);
270029e0 6873
6874 case GE_EXPR:
6875 if (TREE_OVERFLOW (lo))
4e35b483 6876 {
6877 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6878 return omit_one_operand (type, tmp, arg00);
6879 }
7ab7fd4f 6880 return fold_build2 (GE_EXPR, type, arg00, lo);
270029e0 6881
6882 default:
6883 break;
6884 }
6885
6886 return NULL_TREE;
6887}
6888
6889
6881f973 6890/* If CODE with arguments ARG0 and ARG1 represents a single bit
149f0db4 6891 equality/inequality test, then return a simplified form of the test
6892 using a sign testing. Otherwise return NULL. TYPE is the desired
6893 result type. */
7206da1b 6894
149f0db4 6895static tree
6896fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6897 tree result_type)
6881f973 6898{
6881f973 6899 /* If this is testing a single bit, we can optimize the test. */
6900 if ((code == NE_EXPR || code == EQ_EXPR)
6901 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6902 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6903 {
6881f973 6904 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6905 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
149f0db4 6906 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6907
7cc00cbd 6908 if (arg00 != NULL_TREE
6909 /* This is only a win if casting to a signed type is cheap,
6910 i.e. when arg00's type is not a partial mode. */
6911 && TYPE_PRECISION (TREE_TYPE (arg00))
6912 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6881f973 6913 {
11773141 6914 tree stype = signed_type_for (TREE_TYPE (arg00));
7ab7fd4f 6915 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6916 result_type, fold_convert (stype, arg00),
3c6185f1 6917 build_int_cst (stype, 0));
6881f973 6918 }
149f0db4 6919 }
6920
6921 return NULL_TREE;
6922}
6923
6924/* If CODE with arguments ARG0 and ARG1 represents a single bit
6925 equality/inequality test, then return a simplified form of
6926 the test using shifts and logical operations. Otherwise return
6927 NULL. TYPE is the desired result type. */
6928
6929tree
6930fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6931 tree result_type)
6932{
6933 /* If this is testing a single bit, we can optimize the test. */
6934 if ((code == NE_EXPR || code == EQ_EXPR)
6935 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6936 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6937 {
6938 tree inner = TREE_OPERAND (arg0, 0);
6939 tree type = TREE_TYPE (arg0);
6940 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6941 enum machine_mode operand_mode = TYPE_MODE (type);
6942 int ops_unsigned;
6943 tree signed_type, unsigned_type, intermediate_type;
2455d3ef 6944 tree tem, one;
149f0db4 6945
6946 /* First, see if we can fold the single bit test into a sign-bit
6947 test. */
6948 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6949 result_type);
6950 if (tem)
6951 return tem;
a4de5624 6952
7206da1b 6953 /* Otherwise we have (A & C) != 0 where C is a single bit,
6881f973 6954 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6955 Similarly for (A & C) == 0. */
6956
6957 /* If INNER is a right shift of a constant and it plus BITNUM does
6958 not overflow, adjust BITNUM and INNER. */
6959 if (TREE_CODE (inner) == RSHIFT_EXPR
6960 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6961 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6962 && bitnum < TYPE_PRECISION (type)
6963 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6964 bitnum - TYPE_PRECISION (type)))
6965 {
6966 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6967 inner = TREE_OPERAND (inner, 0);
6968 }
6969
6970 /* If we are going to be able to omit the AND below, we must do our
6971 operations as unsigned. If we must use the AND, we have a choice.
6972 Normally unsigned is faster, but for some machines signed is. */
6881f973 6973#ifdef LOAD_EXTEND_OP
a38d3d8b 6974 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6975 && !flag_syntax_only) ? 0 : 1;
6881f973 6976#else
a4de5624 6977 ops_unsigned = 1;
6881f973 6978#endif
6881f973 6979
fa8b888f 6980 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6981 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
654d0fed 6982 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
b30e3dbc 6983 inner = fold_convert (intermediate_type, inner);
6881f973 6984
6985 if (bitnum != 0)
fd96eeef 6986 inner = build2 (RSHIFT_EXPR, intermediate_type,
6987 inner, size_int (bitnum));
6881f973 6988
2455d3ef 6989 one = build_int_cst (intermediate_type, 1);
6990
6881f973 6991 if (code == EQ_EXPR)
2455d3ef 6992 inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6881f973 6993
6994 /* Put the AND last so it can combine with more things. */
2455d3ef 6995 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6881f973 6996
6997 /* Make sure to return the proper type. */
b30e3dbc 6998 inner = fold_convert (result_type, inner);
6881f973 6999
7000 return inner;
7001 }
7002 return NULL_TREE;
7003}
fc3df357 7004
bd214d13 7005/* Check whether we are allowed to reorder operands arg0 and arg1,
7006 such that the evaluation of arg1 occurs before arg0. */
7007
7008static bool
b4b34335 7009reorder_operands_p (const_tree arg0, const_tree arg1)
bd214d13 7010{
7011 if (! flag_evaluation_order)
0c5713a2 7012 return true;
bd214d13 7013 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
7014 return true;
7015 return ! TREE_SIDE_EFFECTS (arg0)
7016 && ! TREE_SIDE_EFFECTS (arg1);
7017}
7018
88e11d8f 7019/* Test whether it is preferable two swap two operands, ARG0 and
7020 ARG1, for example because ARG0 is an integer constant and ARG1
bd214d13 7021 isn't. If REORDER is true, only recommend swapping if we can
7022 evaluate the operands in reverse order. */
88e11d8f 7023
cc0bdf91 7024bool
b7bf20db 7025tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
88e11d8f 7026{
7027 STRIP_SIGN_NOPS (arg0);
7028 STRIP_SIGN_NOPS (arg1);
7029
7030 if (TREE_CODE (arg1) == INTEGER_CST)
7031 return 0;
7032 if (TREE_CODE (arg0) == INTEGER_CST)
7033 return 1;
7034
7035 if (TREE_CODE (arg1) == REAL_CST)
7036 return 0;
7037 if (TREE_CODE (arg0) == REAL_CST)
7038 return 1;
7039
06f0b99c 7040 if (TREE_CODE (arg1) == FIXED_CST)
7041 return 0;
7042 if (TREE_CODE (arg0) == FIXED_CST)
7043 return 1;
7044
88e11d8f 7045 if (TREE_CODE (arg1) == COMPLEX_CST)
7046 return 0;
7047 if (TREE_CODE (arg0) == COMPLEX_CST)
7048 return 1;
7049
7050 if (TREE_CONSTANT (arg1))
7051 return 0;
7052 if (TREE_CONSTANT (arg0))
7053 return 1;
7206da1b 7054
ad4341e8 7055 if (optimize_function_for_size_p (cfun))
f9464d30 7056 return 0;
88e11d8f 7057
bd214d13 7058 if (reorder && flag_evaluation_order
7059 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7060 return 0;
7061
cc0bdf91 7062 /* It is preferable to swap two SSA_NAME to ensure a canonical form
7063 for commutative and comparison operators. Ensuring a canonical
7064 form allows the optimizers to find additional redundancies without
7065 having to explicitly check for both orderings. */
7066 if (TREE_CODE (arg0) == SSA_NAME
7067 && TREE_CODE (arg1) == SSA_NAME
7068 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7069 return 1;
7070
d1d2495d 7071 /* Put SSA_NAMEs last. */
7072 if (TREE_CODE (arg1) == SSA_NAME)
7073 return 0;
7074 if (TREE_CODE (arg0) == SSA_NAME)
7075 return 1;
7076
7077 /* Put variables last. */
7078 if (DECL_P (arg1))
7079 return 0;
7080 if (DECL_P (arg0))
7081 return 1;
7082
88e11d8f 7083 return 0;
7084}
7085
faab57e3 7086/* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7087 ARG0 is extended to a wider type. */
7088
7089static tree
7090fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
7091{
7092 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7093 tree arg1_unw;
7094 tree shorter_type, outer_type;
7095 tree min, max;
7096 bool above, below;
7097
7098 if (arg0_unw == arg0)
7099 return NULL_TREE;
7100 shorter_type = TREE_TYPE (arg0_unw);
fd66f095 7101
085bb6ea 7102#ifdef HAVE_canonicalize_funcptr_for_compare
7103 /* Disable this optimization if we're casting a function pointer
7104 type on targets that require function pointer canonicalization. */
7105 if (HAVE_canonicalize_funcptr_for_compare
7106 && TREE_CODE (shorter_type) == POINTER_TYPE
7107 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7108 return NULL_TREE;
7109#endif
7110
fd66f095 7111 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7112 return NULL_TREE;
7113
d06ed0b1 7114 arg1_unw = get_unwidened (arg1, NULL_TREE);
faab57e3 7115
7116 /* If possible, express the comparison in the shorter mode. */
7117 if ((code == EQ_EXPR || code == NE_EXPR
7118 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7119 && (TREE_TYPE (arg1_unw) == shorter_type
ff5dd140 7120 || ((TYPE_PRECISION (shorter_type)
7ef0e31e 7121 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
ff5dd140 7122 && (TYPE_UNSIGNED (shorter_type)
7123 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
faab57e3 7124 || (TREE_CODE (arg1_unw) == INTEGER_CST
66787d4f 7125 && (TREE_CODE (shorter_type) == INTEGER_TYPE
7126 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
faab57e3 7127 && int_fits_type_p (arg1_unw, shorter_type))))
7ab7fd4f 7128 return fold_build2 (code, type, arg0_unw,
7129 fold_convert (shorter_type, arg1_unw));
faab57e3 7130
a5543a83 7131 if (TREE_CODE (arg1_unw) != INTEGER_CST
7132 || TREE_CODE (shorter_type) != INTEGER_TYPE
7133 || !int_fits_type_p (arg1_unw, shorter_type))
faab57e3 7134 return NULL_TREE;
7135
7136 /* If we are comparing with the integer that does not fit into the range
7137 of the shorter type, the result is known. */
7138 outer_type = TREE_TYPE (arg1_unw);
7139 min = lower_bound_in_type (outer_type, shorter_type);
7140 max = upper_bound_in_type (outer_type, shorter_type);
7141
7142 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7143 max, arg1_unw));
7144 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7145 arg1_unw, min));
7146
7147 switch (code)
7148 {
7149 case EQ_EXPR:
7150 if (above || below)
20a8bb20 7151 return omit_one_operand (type, integer_zero_node, arg0);
faab57e3 7152 break;
7153
7154 case NE_EXPR:
7155 if (above || below)
20a8bb20 7156 return omit_one_operand (type, integer_one_node, arg0);
faab57e3 7157 break;
7158
7159 case LT_EXPR:
7160 case LE_EXPR:
7161 if (above)
20a8bb20 7162 return omit_one_operand (type, integer_one_node, arg0);
faab57e3 7163 else if (below)
20a8bb20 7164 return omit_one_operand (type, integer_zero_node, arg0);
faab57e3 7165
7166 case GT_EXPR:
7167 case GE_EXPR:
7168 if (above)
20a8bb20 7169 return omit_one_operand (type, integer_zero_node, arg0);
faab57e3 7170 else if (below)
20a8bb20 7171 return omit_one_operand (type, integer_one_node, arg0);
faab57e3 7172
7173 default:
7174 break;
7175 }
7176
7177 return NULL_TREE;
7178}
7179
7180/* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7181 ARG0 just the signedness is changed. */
7182
7183static tree
7184fold_sign_changed_comparison (enum tree_code code, tree type,
7185 tree arg0, tree arg1)
7186{
c8110c8f 7187 tree arg0_inner;
faab57e3 7188 tree inner_type, outer_type;
7189
72dd6141 7190 if (!CONVERT_EXPR_P (arg0))
faab57e3 7191 return NULL_TREE;
7192
7193 outer_type = TREE_TYPE (arg0);
7194 arg0_inner = TREE_OPERAND (arg0, 0);
7195 inner_type = TREE_TYPE (arg0_inner);
7196
085bb6ea 7197#ifdef HAVE_canonicalize_funcptr_for_compare
7198 /* Disable this optimization if we're casting a function pointer
7199 type on targets that require function pointer canonicalization. */
7200 if (HAVE_canonicalize_funcptr_for_compare
7201 && TREE_CODE (inner_type) == POINTER_TYPE
7202 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7203 return NULL_TREE;
7204#endif
7205
faab57e3 7206 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7207 return NULL_TREE;
7208
7209 if (TREE_CODE (arg1) != INTEGER_CST
72dd6141 7210 && !(CONVERT_EXPR_P (arg1)
faab57e3 7211 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7212 return NULL_TREE;
7213
4b475af8 7214 if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7215 || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
faab57e3 7216 && code != NE_EXPR
7217 && code != EQ_EXPR)
7218 return NULL_TREE;
7219
7220 if (TREE_CODE (arg1) == INTEGER_CST)
c8110c8f 7221 arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7222 TREE_INT_CST_HIGH (arg1), 0,
eddad94a 7223 TREE_OVERFLOW (arg1));
faab57e3 7224 else
7225 arg1 = fold_convert (inner_type, arg1);
7226
7ab7fd4f 7227 return fold_build2 (code, type, arg0_inner, arg1);
faab57e3 7228}
7229
0de36bdb 7230/* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
ad1f9c12 7231 step of the array. Reconstructs s and delta in the case of s * delta
7232 being an integer constant (and thus already folded).
7233 ADDR is the address. MULT is the multiplicative expression.
e709f9ea 7234 If the function succeeds, the new address expression is returned. Otherwise
7235 NULL_TREE is returned. */
dede8dcc 7236
7237static tree
0de36bdb 7238try_move_mult_to_index (tree addr, tree op1)
dede8dcc 7239{
7240 tree s, delta, step;
dede8dcc 7241 tree ref = TREE_OPERAND (addr, 0), pref;
7242 tree ret, pos;
7243 tree itype;
98f4d382 7244 bool mdim = false;
dede8dcc 7245
0de36bdb 7246 /* Strip the nops that might be added when converting op1 to sizetype. */
7247 STRIP_NOPS (op1);
7248
ad1f9c12 7249 /* Canonicalize op1 into a possibly non-constant delta
7250 and an INTEGER_CST s. */
7251 if (TREE_CODE (op1) == MULT_EXPR)
dede8dcc 7252 {
ad1f9c12 7253 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7254
7255 STRIP_NOPS (arg0);
7256 STRIP_NOPS (arg1);
7257
7258 if (TREE_CODE (arg0) == INTEGER_CST)
7259 {
7260 s = arg0;
7261 delta = arg1;
7262 }
7263 else if (TREE_CODE (arg1) == INTEGER_CST)
7264 {
7265 s = arg1;
7266 delta = arg0;
7267 }
7268 else
7269 return NULL_TREE;
dede8dcc 7270 }
ad1f9c12 7271 else if (TREE_CODE (op1) == INTEGER_CST)
dede8dcc 7272 {
ad1f9c12 7273 delta = op1;
7274 s = NULL_TREE;
dede8dcc 7275 }
7276 else
ad1f9c12 7277 {
7278 /* Simulate we are delta * 1. */
7279 delta = op1;
7280 s = integer_one_node;
7281 }
dede8dcc 7282
7283 for (;; ref = TREE_OPERAND (ref, 0))
7284 {
7285 if (TREE_CODE (ref) == ARRAY_REF)
7286 {
98f4d382 7287 /* Remember if this was a multi-dimensional array. */
7288 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7289 mdim = true;
7290
86f023fe 7291 itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7292 if (! itype)
7293 continue;
7294
dede8dcc 7295 step = array_ref_element_size (ref);
dede8dcc 7296 if (TREE_CODE (step) != INTEGER_CST)
7297 continue;
7298
ad1f9c12 7299 if (s)
7300 {
7301 if (! tree_int_cst_equal (step, s))
7302 continue;
7303 }
7304 else
7305 {
7306 /* Try if delta is a multiple of step. */
5791999f 7307 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
86f023fe 7308 if (! tmp)
ad1f9c12 7309 continue;
86f023fe 7310 delta = tmp;
ad1f9c12 7311 }
dede8dcc 7312
98f4d382 7313 /* Only fold here if we can verify we do not overflow one
7314 dimension of a multi-dimensional array. */
7315 if (mdim)
7316 {
7317 tree tmp;
7318
7319 if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7320 || !INTEGRAL_TYPE_P (itype)
7321 || !TYPE_MAX_VALUE (itype)
7322 || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
7323 continue;
7324
0de36bdb 7325 tmp = fold_binary (PLUS_EXPR, itype,
98f4d382 7326 fold_convert (itype,
7327 TREE_OPERAND (ref, 1)),
7328 fold_convert (itype, delta));
7329 if (!tmp
7330 || TREE_CODE (tmp) != INTEGER_CST
7331 || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
7332 continue;
7333 }
7334
dede8dcc 7335 break;
7336 }
98f4d382 7337 else
7338 mdim = false;
dede8dcc 7339
7340 if (!handled_component_p (ref))
7341 return NULL_TREE;
7342 }
7343
7344 /* We found the suitable array reference. So copy everything up to it,
7345 and replace the index. */
7346
7347 pref = TREE_OPERAND (addr, 0);
7348 ret = copy_node (pref);
7349 pos = ret;
7350
7351 while (pref != ref)
7352 {
7353 pref = TREE_OPERAND (pref, 0);
7354 TREE_OPERAND (pos, 0) = copy_node (pref);
7355 pos = TREE_OPERAND (pos, 0);
7356 }
7357
0de36bdb 7358 TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
ad1f9c12 7359 fold_convert (itype,
7360 TREE_OPERAND (pos, 1)),
7361 fold_convert (itype, delta));
dede8dcc 7362
e7be49a3 7363 return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
dede8dcc 7364}
7365
9a73db25 7366
7367/* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7368 means A >= Y && A != MAX, but in this case we know that
7369 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7370
7371static tree
7372fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
7373{
7374 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7375
7376 if (TREE_CODE (bound) == LT_EXPR)
7377 a = TREE_OPERAND (bound, 0);
7378 else if (TREE_CODE (bound) == GT_EXPR)
7379 a = TREE_OPERAND (bound, 1);
7380 else
7381 return NULL_TREE;
7382
7383 typea = TREE_TYPE (a);
7384 if (!INTEGRAL_TYPE_P (typea)
7385 && !POINTER_TYPE_P (typea))
7386 return NULL_TREE;
7387
7388 if (TREE_CODE (ineq) == LT_EXPR)
7389 {
7390 a1 = TREE_OPERAND (ineq, 1);
7391 y = TREE_OPERAND (ineq, 0);
7392 }
7393 else if (TREE_CODE (ineq) == GT_EXPR)
7394 {
7395 a1 = TREE_OPERAND (ineq, 0);
7396 y = TREE_OPERAND (ineq, 1);
7397 }
7398 else
7399 return NULL_TREE;
7400
7401 if (TREE_TYPE (a1) != typea)
7402 return NULL_TREE;
7403
0de36bdb 7404 if (POINTER_TYPE_P (typea))
7405 {
7406 /* Convert the pointer types into integer before taking the difference. */
7407 tree ta = fold_convert (ssizetype, a);
7408 tree ta1 = fold_convert (ssizetype, a1);
7409 diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7410 }
7411 else
7412 diff = fold_binary (MINUS_EXPR, typea, a1, a);
7413
7414 if (!diff || !integer_onep (diff))
7415 return NULL_TREE;
9a73db25 7416
7ab7fd4f 7417 return fold_build2 (GE_EXPR, type, a, y);
9a73db25 7418}
7419
1c9af531 7420/* Fold a sum or difference of at least one multiplication.
7421 Returns the folded tree or NULL if no simplification could be made. */
7422
7423static tree
7424fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7425{
7426 tree arg00, arg01, arg10, arg11;
7427 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7428
7429 /* (A * C) +- (B * C) -> (A+-B) * C.
7430 (A * C) +- A -> A * (C+-1).
7431 We are most concerned about the case where C is a constant,
7432 but other combinations show up during loop reduction. Since
7433 it is not difficult, try all four possibilities. */
7434
7435 if (TREE_CODE (arg0) == MULT_EXPR)
7436 {
7437 arg00 = TREE_OPERAND (arg0, 0);
7438 arg01 = TREE_OPERAND (arg0, 1);
7439 }
efd4cd99 7440 else if (TREE_CODE (arg0) == INTEGER_CST)
7441 {
7442 arg00 = build_one_cst (type);
7443 arg01 = arg0;
7444 }
1c9af531 7445 else
7446 {
06f0b99c 7447 /* We cannot generate constant 1 for fract. */
7448 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7449 return NULL_TREE;
1c9af531 7450 arg00 = arg0;
ba56cb50 7451 arg01 = build_one_cst (type);
1c9af531 7452 }
7453 if (TREE_CODE (arg1) == MULT_EXPR)
7454 {
7455 arg10 = TREE_OPERAND (arg1, 0);
7456 arg11 = TREE_OPERAND (arg1, 1);
7457 }
efd4cd99 7458 else if (TREE_CODE (arg1) == INTEGER_CST)
7459 {
7460 arg10 = build_one_cst (type);
4f1351a2 7461 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7462 the purpose of this canonicalization. */
7463 if (TREE_INT_CST_HIGH (arg1) == -1
7464 && negate_expr_p (arg1)
7465 && code == PLUS_EXPR)
7466 {
7467 arg11 = negate_expr (arg1);
7468 code = MINUS_EXPR;
7469 }
7470 else
7471 arg11 = arg1;
efd4cd99 7472 }
1c9af531 7473 else
7474 {
06f0b99c 7475 /* We cannot generate constant 1 for fract. */
7476 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7477 return NULL_TREE;
1c9af531 7478 arg10 = arg1;
ba56cb50 7479 arg11 = build_one_cst (type);
1c9af531 7480 }
7481 same = NULL_TREE;
7482
7483 if (operand_equal_p (arg01, arg11, 0))
7484 same = arg01, alt0 = arg00, alt1 = arg10;
7485 else if (operand_equal_p (arg00, arg10, 0))
7486 same = arg00, alt0 = arg01, alt1 = arg11;
7487 else if (operand_equal_p (arg00, arg11, 0))
7488 same = arg00, alt0 = arg01, alt1 = arg10;
7489 else if (operand_equal_p (arg01, arg10, 0))
7490 same = arg01, alt0 = arg00, alt1 = arg11;
7491
7492 /* No identical multiplicands; see if we can find a common
7493 power-of-two factor in non-power-of-two multiplies. This
7494 can help in multi-dimensional array access. */
7495 else if (host_integerp (arg01, 0)
7496 && host_integerp (arg11, 0))
7497 {
7498 HOST_WIDE_INT int01, int11, tmp;
7499 bool swap = false;
7500 tree maybe_same;
7501 int01 = TREE_INT_CST_LOW (arg01);
7502 int11 = TREE_INT_CST_LOW (arg11);
7503
7504 /* Move min of absolute values to int11. */
7505 if ((int01 >= 0 ? int01 : -int01)
7506 < (int11 >= 0 ? int11 : -int11))
7507 {
7508 tmp = int01, int01 = int11, int11 = tmp;
7509 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7510 maybe_same = arg01;
7511 swap = true;
7512 }
7513 else
7514 maybe_same = arg11;
7515
04b63ffe 7516 if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7517 /* The remainder should not be a constant, otherwise we
7518 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7519 increased the number of multiplications necessary. */
7520 && TREE_CODE (arg10) != INTEGER_CST)
1c9af531 7521 {
7522 alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7523 build_int_cst (TREE_TYPE (arg00),
7524 int01 / int11));
7525 alt1 = arg10;
7526 same = maybe_same;
7527 if (swap)
7528 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7529 }
7530 }
7531
7532 if (same)
7533 return fold_build2 (MULT_EXPR, type,
7534 fold_build2 (code, type,
7535 fold_convert (type, alt0),
7536 fold_convert (type, alt1)),
7537 fold_convert (type, same));
7538
7539 return NULL_TREE;
7540}
7541
5f4092ed 7542/* Subroutine of native_encode_expr. Encode the INTEGER_CST
7543 specified by EXPR into the buffer PTR of length LEN bytes.
7544 Return the number of bytes placed in the buffer, or zero
7545 upon failure. */
7546
7547static int
b7bf20db 7548native_encode_int (const_tree expr, unsigned char *ptr, int len)
5f4092ed 7549{
7550 tree type = TREE_TYPE (expr);
7551 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7552 int byte, offset, word, words;
7553 unsigned char value;
7554
7555 if (total_bytes > len)
7556 return 0;
7557 words = total_bytes / UNITS_PER_WORD;
7558
7559 for (byte = 0; byte < total_bytes; byte++)
7560 {
7561 int bitpos = byte * BITS_PER_UNIT;
7562 if (bitpos < HOST_BITS_PER_WIDE_INT)
7563 value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7564 else
7565 value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7566 >> (bitpos - HOST_BITS_PER_WIDE_INT));
7567
7568 if (total_bytes > UNITS_PER_WORD)
7569 {
7570 word = byte / UNITS_PER_WORD;
7571 if (WORDS_BIG_ENDIAN)
7572 word = (words - 1) - word;
7573 offset = word * UNITS_PER_WORD;
7574 if (BYTES_BIG_ENDIAN)
7575 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7576 else
7577 offset += byte % UNITS_PER_WORD;
7578 }
7579 else
7580 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7581 ptr[offset] = value;
7582 }
7583 return total_bytes;
7584}
7585
7586
7587/* Subroutine of native_encode_expr. Encode the REAL_CST
7588 specified by EXPR into the buffer PTR of length LEN bytes.
7589 Return the number of bytes placed in the buffer, or zero
7590 upon failure. */
7591
7592static int
b7bf20db 7593native_encode_real (const_tree expr, unsigned char *ptr, int len)
5f4092ed 7594{
7595 tree type = TREE_TYPE (expr);
7596 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
f83924bb 7597 int byte, offset, word, words, bitpos;
5f4092ed 7598 unsigned char value;
7599
7600 /* There are always 32 bits in each long, no matter the size of
7601 the hosts long. We handle floating point representations with
7602 up to 192 bits. */
7603 long tmp[6];
7604
7605 if (total_bytes > len)
7606 return 0;
0800f6ae 7607 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
5f4092ed 7608
7609 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7610
f83924bb 7611 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7612 bitpos += BITS_PER_UNIT)
5f4092ed 7613 {
f83924bb 7614 byte = (bitpos / BITS_PER_UNIT) & 3;
5f4092ed 7615 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7616
f83924bb 7617 if (UNITS_PER_WORD < 4)
5f4092ed 7618 {
7619 word = byte / UNITS_PER_WORD;
f83924bb 7620 if (WORDS_BIG_ENDIAN)
5f4092ed 7621 word = (words - 1) - word;
7622 offset = word * UNITS_PER_WORD;
7623 if (BYTES_BIG_ENDIAN)
7624 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7625 else
7626 offset += byte % UNITS_PER_WORD;
7627 }
7628 else
f83924bb 7629 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7630 ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
5f4092ed 7631 }
7632 return total_bytes;
7633}
7634
7635/* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7636 specified by EXPR into the buffer PTR of length LEN bytes.
7637 Return the number of bytes placed in the buffer, or zero
7638 upon failure. */
7639
7640static int
b7bf20db 7641native_encode_complex (const_tree expr, unsigned char *ptr, int len)
5f4092ed 7642{
7643 int rsize, isize;
7644 tree part;
7645
7646 part = TREE_REALPART (expr);
7647 rsize = native_encode_expr (part, ptr, len);
7648 if (rsize == 0)
7649 return 0;
7650 part = TREE_IMAGPART (expr);
7651 isize = native_encode_expr (part, ptr+rsize, len-rsize);
7652 if (isize != rsize)
7653 return 0;
7654 return rsize + isize;
7655}
7656
7657
7658/* Subroutine of native_encode_expr. Encode the VECTOR_CST
7659 specified by EXPR into the buffer PTR of length LEN bytes.
7660 Return the number of bytes placed in the buffer, or zero
7661 upon failure. */
7662
7663static int
b7bf20db 7664native_encode_vector (const_tree expr, unsigned char *ptr, int len)
5f4092ed 7665{
3fa15ed1 7666 int i, size, offset, count;
9fd22806 7667 tree itype, elem, elements;
5f4092ed 7668
5f4092ed 7669 offset = 0;
7670 elements = TREE_VECTOR_CST_ELTS (expr);
7671 count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
9fd22806 7672 itype = TREE_TYPE (TREE_TYPE (expr));
7673 size = GET_MODE_SIZE (TYPE_MODE (itype));
5f4092ed 7674 for (i = 0; i < count; i++)
7675 {
7676 if (elements)
7677 {
7678 elem = TREE_VALUE (elements);
7679 elements = TREE_CHAIN (elements);
7680 }
7681 else
7682 elem = NULL_TREE;
7683
7684 if (elem)
7685 {
9fd22806 7686 if (native_encode_expr (elem, ptr+offset, len-offset) != size)
5f4092ed 7687 return 0;
7688 }
9fd22806 7689 else
5f4092ed 7690 {
7691 if (offset + size > len)
7692 return 0;
7693 memset (ptr+offset, 0, size);
7694 }
5f4092ed 7695 offset += size;
7696 }
7697 return offset;
7698}
7699
7700
95b7221a 7701/* Subroutine of native_encode_expr. Encode the STRING_CST
7702 specified by EXPR into the buffer PTR of length LEN bytes.
7703 Return the number of bytes placed in the buffer, or zero
7704 upon failure. */
7705
7706static int
7707native_encode_string (const_tree expr, unsigned char *ptr, int len)
7708{
7709 tree type = TREE_TYPE (expr);
7710 HOST_WIDE_INT total_bytes;
7711
7712 if (TREE_CODE (type) != ARRAY_TYPE
7713 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7714 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7715 || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7716 return 0;
7717 total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7718 if (total_bytes > len)
7719 return 0;
7720 if (TREE_STRING_LENGTH (expr) < total_bytes)
7721 {
7722 memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7723 memset (ptr + TREE_STRING_LENGTH (expr), 0,
7724 total_bytes - TREE_STRING_LENGTH (expr));
7725 }
7726 else
7727 memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7728 return total_bytes;
7729}
7730
7731
5f4092ed 7732/* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7733 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7734 buffer PTR of length LEN bytes. Return the number of bytes
7735 placed in the buffer, or zero upon failure. */
7736
bd56c1f7 7737int
b7bf20db 7738native_encode_expr (const_tree expr, unsigned char *ptr, int len)
5f4092ed 7739{
7740 switch (TREE_CODE (expr))
7741 {
7742 case INTEGER_CST:
7743 return native_encode_int (expr, ptr, len);
7744
7745 case REAL_CST:
7746 return native_encode_real (expr, ptr, len);
7747
7748 case COMPLEX_CST:
7749 return native_encode_complex (expr, ptr, len);
7750
7751 case VECTOR_CST:
7752 return native_encode_vector (expr, ptr, len);
7753
95b7221a 7754 case STRING_CST:
7755 return native_encode_string (expr, ptr, len);
7756
5f4092ed 7757 default:
7758 return 0;
7759 }
7760}
7761
7762
7763/* Subroutine of native_interpret_expr. Interpret the contents of
7764 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7765 If the buffer cannot be interpreted, return NULL_TREE. */
7766
7767static tree
b7bf20db 7768native_interpret_int (tree type, const unsigned char *ptr, int len)
5f4092ed 7769{
7770 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7771 int byte, offset, word, words;
7772 unsigned char value;
7773 unsigned int HOST_WIDE_INT lo = 0;
7774 HOST_WIDE_INT hi = 0;
7775
7776 if (total_bytes > len)
7777 return NULL_TREE;
7778 if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7779 return NULL_TREE;
7780 words = total_bytes / UNITS_PER_WORD;
7781
7782 for (byte = 0; byte < total_bytes; byte++)
7783 {
7784 int bitpos = byte * BITS_PER_UNIT;
7785 if (total_bytes > UNITS_PER_WORD)
7786 {
7787 word = byte / UNITS_PER_WORD;
7788 if (WORDS_BIG_ENDIAN)
7789 word = (words - 1) - word;
7790 offset = word * UNITS_PER_WORD;
7791 if (BYTES_BIG_ENDIAN)
7792 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7793 else
7794 offset += byte % UNITS_PER_WORD;
7795 }
7796 else
7797 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7798 value = ptr[offset];
7799
7800 if (bitpos < HOST_BITS_PER_WIDE_INT)
7801 lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7802 else
7803 hi |= (unsigned HOST_WIDE_INT) value
7804 << (bitpos - HOST_BITS_PER_WIDE_INT);
7805 }
7806
697bbc3f 7807 return build_int_cst_wide_type (type, lo, hi);
5f4092ed 7808}
7809
7810
7811/* Subroutine of native_interpret_expr. Interpret the contents of
7812 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7813 If the buffer cannot be interpreted, return NULL_TREE. */
7814
7815static tree
b7bf20db 7816native_interpret_real (tree type, const unsigned char *ptr, int len)
5f4092ed 7817{
3fa15ed1 7818 enum machine_mode mode = TYPE_MODE (type);
7819 int total_bytes = GET_MODE_SIZE (mode);
f83924bb 7820 int byte, offset, word, words, bitpos;
5f4092ed 7821 unsigned char value;
7822 /* There are always 32 bits in each long, no matter the size of
7823 the hosts long. We handle floating point representations with
7824 up to 192 bits. */
7825 REAL_VALUE_TYPE r;
7826 long tmp[6];
7827
7828 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7829 if (total_bytes > len || total_bytes > 24)
7830 return NULL_TREE;
0800f6ae 7831 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
5f4092ed 7832
7833 memset (tmp, 0, sizeof (tmp));
f83924bb 7834 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7835 bitpos += BITS_PER_UNIT)
5f4092ed 7836 {
f83924bb 7837 byte = (bitpos / BITS_PER_UNIT) & 3;
7838 if (UNITS_PER_WORD < 4)
5f4092ed 7839 {
7840 word = byte / UNITS_PER_WORD;
f83924bb 7841 if (WORDS_BIG_ENDIAN)
5f4092ed 7842 word = (words - 1) - word;
7843 offset = word * UNITS_PER_WORD;
7844 if (BYTES_BIG_ENDIAN)
7845 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7846 else
7847 offset += byte % UNITS_PER_WORD;
7848 }
7849 else
f83924bb 7850 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7851 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
5f4092ed 7852
7853 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7854 }
7855
7856 real_from_target (&r, tmp, mode);
7857 return build_real (type, r);
7858}
7859
7860
7861/* Subroutine of native_interpret_expr. Interpret the contents of
7862 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7863 If the buffer cannot be interpreted, return NULL_TREE. */
7864
7865static tree
b7bf20db 7866native_interpret_complex (tree type, const unsigned char *ptr, int len)
5f4092ed 7867{
7868 tree etype, rpart, ipart;
7869 int size;
7870
7871 etype = TREE_TYPE (type);
7872 size = GET_MODE_SIZE (TYPE_MODE (etype));
7873 if (size * 2 > len)
7874 return NULL_TREE;
7875 rpart = native_interpret_expr (etype, ptr, size);
7876 if (!rpart)
7877 return NULL_TREE;
7878 ipart = native_interpret_expr (etype, ptr+size, size);
7879 if (!ipart)
7880 return NULL_TREE;
7881 return build_complex (type, rpart, ipart);
7882}
7883
7884
7885/* Subroutine of native_interpret_expr. Interpret the contents of
7886 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7887 If the buffer cannot be interpreted, return NULL_TREE. */
7888
7889static tree
b7bf20db 7890native_interpret_vector (tree type, const unsigned char *ptr, int len)
5f4092ed 7891{
7892 tree etype, elem, elements;
7893 int i, size, count;
7894
7895 etype = TREE_TYPE (type);
7896 size = GET_MODE_SIZE (TYPE_MODE (etype));
7897 count = TYPE_VECTOR_SUBPARTS (type);
7898 if (size * count > len)
7899 return NULL_TREE;
7900
7901 elements = NULL_TREE;
7902 for (i = count - 1; i >= 0; i--)
7903 {
7904 elem = native_interpret_expr (etype, ptr+(i*size), size);
7905 if (!elem)
7906 return NULL_TREE;
7907 elements = tree_cons (NULL_TREE, elem, elements);
7908 }
7909 return build_vector (type, elements);
7910}
7911
7912
d961ae3a 7913/* Subroutine of fold_view_convert_expr. Interpret the contents of
5f4092ed 7914 the buffer PTR of length LEN as a constant of type TYPE. For
7915 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7916 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7917 return NULL_TREE. */
7918
bd56c1f7 7919tree
b7bf20db 7920native_interpret_expr (tree type, const unsigned char *ptr, int len)
5f4092ed 7921{
7922 switch (TREE_CODE (type))
7923 {
7924 case INTEGER_TYPE:
7925 case ENUMERAL_TYPE:
7926 case BOOLEAN_TYPE:
7927 return native_interpret_int (type, ptr, len);
7928
7929 case REAL_TYPE:
7930 return native_interpret_real (type, ptr, len);
7931
7932 case COMPLEX_TYPE:
7933 return native_interpret_complex (type, ptr, len);
7934
7935 case VECTOR_TYPE:
7936 return native_interpret_vector (type, ptr, len);
7937
7938 default:
7939 return NULL_TREE;
7940 }
7941}
7942
7943
7944/* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7945 TYPE at compile-time. If we're unable to perform the conversion
7946 return NULL_TREE. */
7947
7948static tree
7949fold_view_convert_expr (tree type, tree expr)
7950{
7951 /* We support up to 512-bit values (for V8DFmode). */
7952 unsigned char buffer[64];
7953 int len;
7954
7955 /* Check that the host and target are sane. */
7956 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7957 return NULL_TREE;
7958
7959 len = native_encode_expr (expr, buffer, sizeof (buffer));
7960 if (len == 0)
7961 return NULL_TREE;
7962
7963 return native_interpret_expr (type, buffer, len);
7964}
7965
2bf4108d 7966/* Build an expression for the address of T. Folds away INDIRECT_REF
86f2ad37 7967 to avoid confusing the gimplify process. */
2bf4108d 7968
86f2ad37 7969tree
7970build_fold_addr_expr_with_type (tree t, tree ptrtype)
2bf4108d 7971{
7972 /* The size of the object is not relevant when talking about its address. */
7973 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7974 t = TREE_OPERAND (t, 0);
7975
7976 /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7977 if (TREE_CODE (t) == INDIRECT_REF
7978 || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7979 {
7980 t = TREE_OPERAND (t, 0);
7981
7982 if (TREE_TYPE (t) != ptrtype)
7983 t = build1 (NOP_EXPR, ptrtype, t);
7984 }
2bf4108d 7985 else
7986 t = build1 (ADDR_EXPR, ptrtype, t);
7987
7988 return t;
7989}
7990
86f2ad37 7991/* Build an expression for the address of T. */
2bf4108d 7992
7993tree
7994build_fold_addr_expr (tree t)
2bf4108d 7995{
7996 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7997
86f2ad37 7998 return build_fold_addr_expr_with_type (t, ptrtype);
2bf4108d 7999}
5f4092ed 8000
0d3711e2 8001/* Fold a unary expression of code CODE and type TYPE with operand
8002 OP0. Return the folded expression if folding is successful.
8003 Otherwise, return NULL_TREE. */
422c18cb 8004
d3858e14 8005tree
0052b98e 8006fold_unary (enum tree_code code, tree type, tree op0)
422c18cb 8007{
422c18cb 8008 tree tem;
0052b98e 8009 tree arg0;
422c18cb 8010 enum tree_code_class kind = TREE_CODE_CLASS (code);
8011
8012 gcc_assert (IS_EXPR_CODE_CLASS (kind)
8013 && TREE_CODE_LENGTH (code) == 1);
8014
0052b98e 8015 arg0 = op0;
422c18cb 8016 if (arg0)
8017 {
d9659041 8018 if (CONVERT_EXPR_CODE_P (code)
00bb4a78 8019 || code == FLOAT_EXPR || code == ABS_EXPR)
422c18cb 8020 {
00bb4a78 8021 /* Don't use STRIP_NOPS, because signedness of argument type
8022 matters. */
422c18cb 8023 STRIP_SIGN_NOPS (arg0);
8024 }
8025 else
8026 {
8027 /* Strip any conversions that don't change the mode. This
8028 is safe for every expression, except for a comparison
8029 expression because its signedness is derived from its
8030 operands.
8031
8032 Note that this is done as an internal manipulation within
8033 the constant folder, in order to find the simplest
8034 representation of the arguments so that their form can be
8035 studied. In any cases, the appropriate type conversions
8036 should be put back in the tree that will get out of the
8037 constant folder. */
8038 STRIP_NOPS (arg0);
8039 }
8040 }
8041
8042 if (TREE_CODE_CLASS (code) == tcc_unary)
8043 {
8044 if (TREE_CODE (arg0) == COMPOUND_EXPR)
8045 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
4e7cb1c5 8046 fold_build1 (code, type,
8047 fold_convert (TREE_TYPE (op0),
8048 TREE_OPERAND (arg0, 1))));
422c18cb 8049 else if (TREE_CODE (arg0) == COND_EXPR)
8050 {
8051 tree arg01 = TREE_OPERAND (arg0, 1);
8052 tree arg02 = TREE_OPERAND (arg0, 2);
8053 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
4e7cb1c5 8054 arg01 = fold_build1 (code, type,
8055 fold_convert (TREE_TYPE (op0), arg01));
422c18cb 8056 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
4e7cb1c5 8057 arg02 = fold_build1 (code, type,
8058 fold_convert (TREE_TYPE (op0), arg02));
7ab7fd4f 8059 tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
8060 arg01, arg02);
422c18cb 8061
8062 /* If this was a conversion, and all we did was to move into
8063 inside the COND_EXPR, bring it back out. But leave it if
8064 it is a conversion from integer to integer and the
8065 result precision is no wider than a word since such a
8066 conversion is cheap and may be optimized away by combine,
8067 while it couldn't if it were outside the COND_EXPR. Then return
8068 so we don't get into an infinite recursion loop taking the
8069 conversion out and then back in. */
8070
d9659041 8071 if ((CONVERT_EXPR_CODE_P (code)
422c18cb 8072 || code == NON_LVALUE_EXPR)
8073 && TREE_CODE (tem) == COND_EXPR
8074 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8075 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8076 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8077 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8078 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8079 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8080 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8081 && (INTEGRAL_TYPE_P
8082 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8083 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8084 || flag_syntax_only))
8085 tem = build1 (code, type,
8086 build3 (COND_EXPR,
8087 TREE_TYPE (TREE_OPERAND
8088 (TREE_OPERAND (tem, 1), 0)),
8089 TREE_OPERAND (tem, 0),
8090 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8091 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8092 return tem;
8093 }
8094 else if (COMPARISON_CLASS_P (arg0))
8095 {
8096 if (TREE_CODE (type) == BOOLEAN_TYPE)
8097 {
8098 arg0 = copy_node (arg0);
8099 TREE_TYPE (arg0) = type;
8100 return arg0;
8101 }
8102 else if (TREE_CODE (type) != INTEGER_TYPE)
7ab7fd4f 8103 return fold_build3 (COND_EXPR, type, arg0,
8104 fold_build1 (code, type,
8105 integer_one_node),
8106 fold_build1 (code, type,
8107 integer_zero_node));
422c18cb 8108 }
8109 }
8110
8111 switch (code)
8112 {
751ff693 8113 case PAREN_EXPR:
8114 /* Re-association barriers around constants and other re-association
8115 barriers can be removed. */
8116 if (CONSTANT_CLASS_P (op0)
8117 || TREE_CODE (op0) == PAREN_EXPR)
8118 return fold_convert (type, op0);
8119 return NULL_TREE;
8120
72dd6141 8121 CASE_CONVERT:
422c18cb 8122 case FLOAT_EXPR:
422c18cb 8123 case FIX_TRUNC_EXPR:
fac5aff3 8124 if (TREE_TYPE (op0) == type)
8125 return op0;
8aa776be 8126
191ec5a2 8127 /* If we have (type) (a CMP b) and type is an integral type, return
8aa776be 8128 new expression involving the new type. */
8129 if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8130 return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8131 TREE_OPERAND (op0, 1));
422c18cb 8132
8133 /* Handle cases of two conversions in a row. */
72dd6141 8134 if (CONVERT_EXPR_P (op0))
422c18cb 8135 {
fac5aff3 8136 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8137 tree inter_type = TREE_TYPE (op0);
422c18cb 8138 int inside_int = INTEGRAL_TYPE_P (inside_type);
8139 int inside_ptr = POINTER_TYPE_P (inside_type);
8140 int inside_float = FLOAT_TYPE_P (inside_type);
6ff828af 8141 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
422c18cb 8142 unsigned int inside_prec = TYPE_PRECISION (inside_type);
8143 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8144 int inter_int = INTEGRAL_TYPE_P (inter_type);
8145 int inter_ptr = POINTER_TYPE_P (inter_type);
8146 int inter_float = FLOAT_TYPE_P (inter_type);
6ff828af 8147 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
422c18cb 8148 unsigned int inter_prec = TYPE_PRECISION (inter_type);
8149 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8150 int final_int = INTEGRAL_TYPE_P (type);
8151 int final_ptr = POINTER_TYPE_P (type);
8152 int final_float = FLOAT_TYPE_P (type);
6ff828af 8153 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
422c18cb 8154 unsigned int final_prec = TYPE_PRECISION (type);
8155 int final_unsignedp = TYPE_UNSIGNED (type);
8156
8157 /* In addition to the cases of two conversions in a row
8158 handled below, if we are converting something to its own
8159 type via an object of identical or wider precision, neither
8160 conversion is needed. */
8161 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
219dad96 8162 && (((inter_int || inter_ptr) && final_int)
8163 || (inter_float && final_float))
422c18cb 8164 && inter_prec >= final_prec)
7ab7fd4f 8165 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
422c18cb 8166
4ce9876e 8167 /* Likewise, if the intermediate and initial types are either both
8168 float or both integer, we don't need the middle conversion if the
8169 former is wider than the latter and doesn't change the signedness
8170 (for integers). Avoid this if the final type is a pointer since
8171 then we sometimes need the middle conversion. Likewise if the
8172 final type has a precision not equal to the size of its mode. */
0fd56ba6 8173 if (((inter_int && inside_int)
6ff828af 8174 || (inter_float && inside_float)
8175 || (inter_vec && inside_vec))
422c18cb 8176 && inter_prec >= inside_prec
6ff828af 8177 && (inter_float || inter_vec
8178 || inter_unsignedp == inside_unsignedp)
422c18cb 8179 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8180 && TYPE_MODE (type) == TYPE_MODE (inter_type))
6ff828af 8181 && ! final_ptr
8182 && (! final_vec || inter_prec == inside_prec))
7ab7fd4f 8183 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
422c18cb 8184
8185 /* If we have a sign-extension of a zero-extended value, we can
8186 replace that by a single zero-extension. */
8187 if (inside_int && inter_int && final_int
8188 && inside_prec < inter_prec && inter_prec < final_prec
8189 && inside_unsignedp && !inter_unsignedp)
7ab7fd4f 8190 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
422c18cb 8191
8192 /* Two conversions in a row are not needed unless:
8193 - some conversion is floating-point (overstrict for now), or
6ff828af 8194 - some conversion is a vector (overstrict for now), or
422c18cb 8195 - the intermediate type is narrower than both initial and
8196 final, or
8197 - the intermediate type and innermost type differ in signedness,
8198 and the outermost type is wider than the intermediate, or
8199 - the initial type is a pointer type and the precisions of the
8200 intermediate and final types differ, or
8201 - the final type is a pointer type and the precisions of the
0b4a6afc 8202 initial and intermediate types differ. */
422c18cb 8203 if (! inside_float && ! inter_float && ! final_float
6ff828af 8204 && ! inside_vec && ! inter_vec && ! final_vec
219dad96 8205 && (inter_prec >= inside_prec || inter_prec >= final_prec)
422c18cb 8206 && ! (inside_int && inter_int
8207 && inter_unsignedp != inside_unsignedp
8208 && inter_prec < final_prec)
8209 && ((inter_unsignedp && inter_prec > inside_prec)
8210 == (final_unsignedp && final_prec > inter_prec))
8211 && ! (inside_ptr && inter_prec != final_prec)
8212 && ! (final_ptr && inside_prec != inter_prec)
8213 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
0b4a6afc 8214 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7ab7fd4f 8215 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
422c18cb 8216 }
8217
73d9e97d 8218 /* Handle (T *)&A.B.C for A being of type T and B and C
23943319 8219 living at offset zero. This occurs frequently in
73d9e97d 8220 C++ upcasting and then accessing the base. */
8221 if (TREE_CODE (op0) == ADDR_EXPR
8222 && POINTER_TYPE_P (type)
8223 && handled_component_p (TREE_OPERAND (op0, 0)))
8224 {
8225 HOST_WIDE_INT bitsize, bitpos;
8226 tree offset;
8227 enum machine_mode mode;
8228 int unsignedp, volatilep;
8229 tree base = TREE_OPERAND (op0, 0);
8230 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8231 &mode, &unsignedp, &volatilep, false);
8232 /* If the reference was to a (constant) zero offset, we can use
8233 the address of the base if it has the same base type
8234 as the result type. */
8235 if (! offset && bitpos == 0
8236 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8237 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
86f2ad37 8238 return fold_convert (type, build_fold_addr_expr (base));
73d9e97d 8239 }
8240
75a70cf9 8241 if (TREE_CODE (op0) == MODIFY_EXPR
8242 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
422c18cb 8243 /* Detect assigning a bitfield. */
75a70cf9 8244 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
35cc02b5 8245 && DECL_BIT_FIELD
75a70cf9 8246 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
422c18cb 8247 {
8248 /* Don't leave an assignment inside a conversion
8249 unless assigning a bitfield. */
75a70cf9 8250 tem = fold_build1 (code, type, TREE_OPERAND (op0, 1));
422c18cb 8251 /* First do the assignment, then return converted constant. */
b085d4e5 8252 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
422c18cb 8253 TREE_NO_WARNING (tem) = 1;
8254 TREE_USED (tem) = 1;
8255 return tem;
8256 }
8257
8258 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8259 constants (if x has signed type, the sign bit cannot be set
e7b454ed 8260 in c). This folds extension into the BIT_AND_EXPR.
8261 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8262 very likely don't have maximal range for their precision and this
8263 transformation effectively doesn't preserve non-maximal ranges. */
101f2414 8264 if (TREE_CODE (type) == INTEGER_TYPE
fac5aff3 8265 && TREE_CODE (op0) == BIT_AND_EXPR
a9538d68 8266 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
422c18cb 8267 {
fac5aff3 8268 tree and = op0;
422c18cb 8269 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
8270 int change = 0;
8271
8272 if (TYPE_UNSIGNED (TREE_TYPE (and))
8273 || (TYPE_PRECISION (type)
8274 <= TYPE_PRECISION (TREE_TYPE (and))))
8275 change = 1;
8276 else if (TYPE_PRECISION (TREE_TYPE (and1))
8277 <= HOST_BITS_PER_WIDE_INT
8278 && host_integerp (and1, 1))
8279 {
8280 unsigned HOST_WIDE_INT cst;
8281
8282 cst = tree_low_cst (and1, 1);
8283 cst &= (HOST_WIDE_INT) -1
8284 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8285 change = (cst == 0);
8286#ifdef LOAD_EXTEND_OP
8287 if (change
8288 && !flag_syntax_only
8289 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8290 == ZERO_EXTEND))
8291 {
71eea85c 8292 tree uns = unsigned_type_for (TREE_TYPE (and0));
422c18cb 8293 and0 = fold_convert (uns, and0);
8294 and1 = fold_convert (uns, and1);
8295 }
8296#endif
8297 }
8298 if (change)
8299 {
c8110c8f 8300 tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8301 TREE_INT_CST_HIGH (and1), 0,
eddad94a 8302 TREE_OVERFLOW (and1));
7ab7fd4f 8303 return fold_build2 (BIT_AND_EXPR, type,
8304 fold_convert (type, and0), tem);
422c18cb 8305 }
8306 }
8307
0de36bdb 8308 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
06f9fe3e 8309 when one of the new casts will fold away. Conservatively we assume
0de36bdb 8310 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8311 if (POINTER_TYPE_P (type)
8312 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
06f9fe3e 8313 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8314 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8315 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
422c18cb 8316 {
8317 tree arg00 = TREE_OPERAND (arg0, 0);
06f9fe3e 8318 tree arg01 = TREE_OPERAND (arg0, 1);
8319
8320 return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
0de36bdb 8321 fold_convert (sizetype, arg01));
422c18cb 8322 }
8323
c348f27f 8324 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
80777cd8 8325 of the same precision, and X is an integer type not narrower than
c348f27f 8326 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
8327 if (INTEGRAL_TYPE_P (type)
8328 && TREE_CODE (op0) == BIT_NOT_EXPR
8329 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
72dd6141 8330 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
c348f27f 8331 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8332 {
8333 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8334 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8335 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8336 return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
8337 }
8338
92b2f241 8339 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8340 type of X and Y (integer types only). */
8341 if (INTEGRAL_TYPE_P (type)
8342 && TREE_CODE (op0) == MULT_EXPR
8343 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8344 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8345 {
8346 /* Be careful not to introduce new overflows. */
8347 tree mult_type;
8348 if (TYPE_OVERFLOW_WRAPS (type))
8349 mult_type = type;
8350 else
8351 mult_type = unsigned_type_for (type);
6d5aa66a 8352
8353 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8354 {
8355 tem = fold_build2 (MULT_EXPR, mult_type,
8356 fold_convert (mult_type,
8357 TREE_OPERAND (op0, 0)),
8358 fold_convert (mult_type,
8359 TREE_OPERAND (op0, 1)));
8360 return fold_convert (type, tem);
8361 }
92b2f241 8362 }
8363
7a6537b3 8364 tem = fold_convert_const (code, type, op0);
e7edfbbd 8365 return tem ? tem : NULL_TREE;
422c18cb 8366
06f0b99c 8367 case FIXED_CONVERT_EXPR:
8368 tem = fold_convert_const (code, type, arg0);
8369 return tem ? tem : NULL_TREE;
8370
422c18cb 8371 case VIEW_CONVERT_EXPR:
eb381097 8372 if (TREE_TYPE (op0) == type)
8373 return op0;
802d9f2f 8374 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
5f4092ed 8375 return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
802d9f2f 8376
8377 /* For integral conversions with the same precision or pointer
8378 conversions use a NOP_EXPR instead. */
1d9353f3 8379 if ((INTEGRAL_TYPE_P (type)
8380 || POINTER_TYPE_P (type))
8381 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8382 || POINTER_TYPE_P (TREE_TYPE (op0)))
a9538d68 8383 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
802d9f2f 8384 return fold_convert (type, op0);
8385
8386 /* Strip inner integral conversions that do not change the precision. */
72dd6141 8387 if (CONVERT_EXPR_P (op0)
1d9353f3 8388 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8389 || POINTER_TYPE_P (TREE_TYPE (op0)))
8390 && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8391 || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
802d9f2f 8392 && (TYPE_PRECISION (TREE_TYPE (op0))
8393 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8394 return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8395
5f4092ed 8396 return fold_view_convert_expr (type, op0);
422c18cb 8397
8398 case NEGATE_EXPR:
58b22aa6 8399 tem = fold_negate_expr (arg0);
8400 if (tem)
8401 return fold_convert (type, tem);
e7edfbbd 8402 return NULL_TREE;
422c18cb 8403
8404 case ABS_EXPR:
8405 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8406 return fold_abs_const (arg0, type);
8407 else if (TREE_CODE (arg0) == NEGATE_EXPR)
7ab7fd4f 8408 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
422c18cb 8409 /* Convert fabs((double)float) into (double)fabsf(float). */
8410 else if (TREE_CODE (arg0) == NOP_EXPR
8411 && TREE_CODE (type) == REAL_TYPE)
8412 {
8413 tree targ0 = strip_float_extensions (arg0);
8414 if (targ0 != arg0)
7ab7fd4f 8415 return fold_convert (type, fold_build1 (ABS_EXPR,
8416 TREE_TYPE (targ0),
8417 targ0));
422c18cb 8418 }
8040d1c5 8419 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
add6ee5e 8420 else if (TREE_CODE (arg0) == ABS_EXPR)
8421 return arg0;
8422 else if (tree_expr_nonnegative_p (arg0))
422c18cb 8423 return arg0;
8424
8425 /* Strip sign ops from argument. */
8426 if (TREE_CODE (type) == REAL_TYPE)
8427 {
8428 tem = fold_strip_sign_ops (arg0);
8429 if (tem)
7ab7fd4f 8430 return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
422c18cb 8431 }
e7edfbbd 8432 return NULL_TREE;
422c18cb 8433
8434 case CONJ_EXPR:
8435 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8436 return fold_convert (type, arg0);
75e3ef6e 8437 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8438 {
8439 tree itype = TREE_TYPE (type);
8440 tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8441 tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8442 return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8443 }
8444 if (TREE_CODE (arg0) == COMPLEX_CST)
8445 {
8446 tree itype = TREE_TYPE (type);
8447 tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8448 tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8449 return build_complex (type, rpart, negate_expr (ipart));
8450 }
8451 if (TREE_CODE (arg0) == CONJ_EXPR)
8452 return fold_convert (type, TREE_OPERAND (arg0, 0));
e7edfbbd 8453 return NULL_TREE;
422c18cb 8454
8455 case BIT_NOT_EXPR:
8456 if (TREE_CODE (arg0) == INTEGER_CST)
8457 return fold_not_const (arg0, type);
8458 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
c2f5179d 8459 return fold_convert (type, TREE_OPERAND (arg0, 0));
422c18cb 8460 /* Convert ~ (-A) to A - 1. */
8461 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
05e1595b 8462 return fold_build2 (MINUS_EXPR, type,
8463 fold_convert (type, TREE_OPERAND (arg0, 0)),
7ab7fd4f 8464 build_int_cst (type, 1));
422c18cb 8465 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8466 else if (INTEGRAL_TYPE_P (type)
8467 && ((TREE_CODE (arg0) == MINUS_EXPR
8468 && integer_onep (TREE_OPERAND (arg0, 1)))
8469 || (TREE_CODE (arg0) == PLUS_EXPR
8470 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
05e1595b 8471 return fold_build1 (NEGATE_EXPR, type,
8472 fold_convert (type, TREE_OPERAND (arg0, 0)));
039f212d 8473 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8474 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8475 && (tem = fold_unary (BIT_NOT_EXPR, type,
8476 fold_convert (type,
8477 TREE_OPERAND (arg0, 0)))))
8478 return fold_build2 (BIT_XOR_EXPR, type, tem,
8479 fold_convert (type, TREE_OPERAND (arg0, 1)));
8480 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8481 && (tem = fold_unary (BIT_NOT_EXPR, type,
8482 fold_convert (type,
8483 TREE_OPERAND (arg0, 1)))))
8484 return fold_build2 (BIT_XOR_EXPR, type,
8485 fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
89ee75ae 8486 /* Perform BIT_NOT_EXPR on each element individually. */
8487 else if (TREE_CODE (arg0) == VECTOR_CST)
8488 {
8489 tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8490 int count = TYPE_VECTOR_SUBPARTS (type), i;
8491
8492 for (i = 0; i < count; i++)
8493 {
8494 if (elements)
8495 {
8496 elem = TREE_VALUE (elements);
8497 elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8498 if (elem == NULL_TREE)
8499 break;
8500 elements = TREE_CHAIN (elements);
8501 }
8502 else
8503 elem = build_int_cst (TREE_TYPE (type), -1);
8504 list = tree_cons (NULL_TREE, elem, list);
8505 }
8506 if (i == count)
8507 return build_vector (type, nreverse (list));
8508 }
039f212d 8509
e7edfbbd 8510 return NULL_TREE;
422c18cb 8511
8512 case TRUTH_NOT_EXPR:
8513 /* The argument to invert_truthvalue must have Boolean type. */
8514 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8515 arg0 = fold_convert (boolean_type_node, arg0);
8516
8517 /* Note that the operand of this must be an int
8518 and its values must be 0 or 1.
8519 ("true" is a fixed value perhaps depending on the language,
8520 but we don't handle values other than 1 correctly yet.) */
6758b11c 8521 tem = fold_truth_not_expr (arg0);
8522 if (!tem)
e7edfbbd 8523 return NULL_TREE;
422c18cb 8524 return fold_convert (type, tem);
8525
8526 case REALPART_EXPR:
8527 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
75e3ef6e 8528 return fold_convert (type, arg0);
8529 if (TREE_CODE (arg0) == COMPLEX_EXPR)
422c18cb 8530 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8531 TREE_OPERAND (arg0, 1));
75e3ef6e 8532 if (TREE_CODE (arg0) == COMPLEX_CST)
8533 return fold_convert (type, TREE_REALPART (arg0));
8534 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8535 {
8536 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8537 tem = fold_build2 (TREE_CODE (arg0), itype,
8538 fold_build1 (REALPART_EXPR, itype,
8539 TREE_OPERAND (arg0, 0)),
8540 fold_build1 (REALPART_EXPR, itype,
8541 TREE_OPERAND (arg0, 1)));
8542 return fold_convert (type, tem);
8543 }
8544 if (TREE_CODE (arg0) == CONJ_EXPR)
8545 {
8546 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8547 tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8548 return fold_convert (type, tem);
8549 }
41ac51b1 8550 if (TREE_CODE (arg0) == CALL_EXPR)
8551 {
8552 tree fn = get_callee_fndecl (arg0);
71bf42bb 8553 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
41ac51b1 8554 switch (DECL_FUNCTION_CODE (fn))
8555 {
8556 CASE_FLT_FN (BUILT_IN_CEXPI):
8557 fn = mathfn_built_in (type, BUILT_IN_COS);
2a6b4c77 8558 if (fn)
c2f47e15 8559 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
2a6b4c77 8560 break;
41ac51b1 8561
2a6b4c77 8562 default:
8563 break;
41ac51b1 8564 }
8565 }
e7edfbbd 8566 return NULL_TREE;
422c18cb 8567
8568 case IMAGPART_EXPR:
8569 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8570 return fold_convert (type, integer_zero_node);
75e3ef6e 8571 if (TREE_CODE (arg0) == COMPLEX_EXPR)
422c18cb 8572 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8573 TREE_OPERAND (arg0, 0));
75e3ef6e 8574 if (TREE_CODE (arg0) == COMPLEX_CST)
8575 return fold_convert (type, TREE_IMAGPART (arg0));
8576 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8577 {
8578 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8579 tem = fold_build2 (TREE_CODE (arg0), itype,
8580 fold_build1 (IMAGPART_EXPR, itype,
8581 TREE_OPERAND (arg0, 0)),
8582 fold_build1 (IMAGPART_EXPR, itype,
8583 TREE_OPERAND (arg0, 1)));
8584 return fold_convert (type, tem);
8585 }
8586 if (TREE_CODE (arg0) == CONJ_EXPR)
8587 {
8588 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8589 tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8590 return fold_convert (type, negate_expr (tem));
8591 }
41ac51b1 8592 if (TREE_CODE (arg0) == CALL_EXPR)
8593 {
8594 tree fn = get_callee_fndecl (arg0);
71bf42bb 8595 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
41ac51b1 8596 switch (DECL_FUNCTION_CODE (fn))
8597 {
8598 CASE_FLT_FN (BUILT_IN_CEXPI):
8599 fn = mathfn_built_in (type, BUILT_IN_SIN);
2a6b4c77 8600 if (fn)
c2f47e15 8601 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
2a6b4c77 8602 break;
41ac51b1 8603
2a6b4c77 8604 default:
8605 break;
41ac51b1 8606 }
8607 }
e7edfbbd 8608 return NULL_TREE;
422c18cb 8609
8610 default:
e7edfbbd 8611 return NULL_TREE;
422c18cb 8612 } /* switch (code) */
8613}
8614
cd30b839 8615
8616/* If the operation was a conversion do _not_ mark a resulting constant
8617 with TREE_OVERFLOW if the original constant was not. These conversions
8618 have implementation defined behavior and retaining the TREE_OVERFLOW
8619 flag here would confuse later passes such as VRP. */
8620tree
8621fold_unary_ignore_overflow (enum tree_code code, tree type, tree op0)
8622{
8623 tree res = fold_unary (code, type, op0);
8624 if (res
8625 && TREE_CODE (res) == INTEGER_CST
8626 && TREE_CODE (op0) == INTEGER_CST
8627 && CONVERT_EXPR_CODE_P (code))
8628 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8629
8630 return res;
8631}
8632
7e50ecae 8633/* Fold a binary expression of code CODE and type TYPE with operands
8634 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8635 Return the folded expression if folding is successful. Otherwise,
8636 return NULL_TREE. */
8637
8638static tree
8639fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8640{
8641 enum tree_code compl_code;
8642
8643 if (code == MIN_EXPR)
8644 compl_code = MAX_EXPR;
8645 else if (code == MAX_EXPR)
8646 compl_code = MIN_EXPR;
8647 else
d9560eb6 8648 gcc_unreachable ();
7e50ecae 8649
8aa01816 8650 /* MIN (MAX (a, b), b) == b. */
7e50ecae 8651 if (TREE_CODE (op0) == compl_code
8652 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8653 return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8654
8aa01816 8655 /* MIN (MAX (b, a), b) == b. */
7e50ecae 8656 if (TREE_CODE (op0) == compl_code
8657 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8658 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8659 return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8660
8aa01816 8661 /* MIN (a, MAX (a, b)) == a. */
7e50ecae 8662 if (TREE_CODE (op1) == compl_code
8663 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8664 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8665 return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8666
8aa01816 8667 /* MIN (a, MAX (b, a)) == a. */
7e50ecae 8668 if (TREE_CODE (op1) == compl_code
8669 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8670 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8671 return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8672
8673 return NULL_TREE;
8674}
8675
26e1261a 8676/* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8677 by changing CODE to reduce the magnitude of constants involved in
8678 ARG0 of the comparison.
8679 Returns a canonicalized comparison tree if a simplification was
add6ee5e 8680 possible, otherwise returns NULL_TREE.
8681 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8682 valid if signed overflow is undefined. */
26e1261a 8683
8684static tree
8685maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
add6ee5e 8686 tree arg0, tree arg1,
8687 bool *strict_overflow_p)
26e1261a 8688{
8689 enum tree_code code0 = TREE_CODE (arg0);
8690 tree t, cst0 = NULL_TREE;
8691 int sgn0;
8692 bool swap = false;
8693
dc5e8aba 8694 /* Match A +- CST code arg1 and CST code arg1. We can change the
8695 first form only if overflow is undefined. */
8696 if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8697 /* In principle pointers also have undefined overflow behavior,
8698 but that causes problems elsewhere. */
8699 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8700 && (code0 == MINUS_EXPR
8701 || code0 == PLUS_EXPR)
26e1261a 8702 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8703 || code0 == INTEGER_CST))
8704 return NULL_TREE;
8705
8706 /* Identify the constant in arg0 and its sign. */
8707 if (code0 == INTEGER_CST)
8708 cst0 = arg0;
8709 else
8710 cst0 = TREE_OPERAND (arg0, 1);
8711 sgn0 = tree_int_cst_sgn (cst0);
8712
8713 /* Overflowed constants and zero will cause problems. */
8714 if (integer_zerop (cst0)
8715 || TREE_OVERFLOW (cst0))
8716 return NULL_TREE;
8717
fa7637bd 8718 /* See if we can reduce the magnitude of the constant in
26e1261a 8719 arg0 by changing the comparison code. */
8720 if (code0 == INTEGER_CST)
8721 {
8722 /* CST <= arg1 -> CST-1 < arg1. */
8723 if (code == LE_EXPR && sgn0 == 1)
8724 code = LT_EXPR;
8725 /* -CST < arg1 -> -CST-1 <= arg1. */
8726 else if (code == LT_EXPR && sgn0 == -1)
8727 code = LE_EXPR;
8728 /* CST > arg1 -> CST-1 >= arg1. */
8729 else if (code == GT_EXPR && sgn0 == 1)
8730 code = GE_EXPR;
8731 /* -CST >= arg1 -> -CST-1 > arg1. */
8732 else if (code == GE_EXPR && sgn0 == -1)
8733 code = GT_EXPR;
8734 else
8735 return NULL_TREE;
8736 /* arg1 code' CST' might be more canonical. */
8737 swap = true;
8738 }
8739 else
8740 {
8741 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8742 if (code == LT_EXPR
8743 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8744 code = LE_EXPR;
8745 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8746 else if (code == GT_EXPR
8747 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8748 code = GE_EXPR;
8749 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8750 else if (code == LE_EXPR
8751 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8752 code = LT_EXPR;
8753 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8754 else if (code == GE_EXPR
8755 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8756 code = GT_EXPR;
8757 else
8758 return NULL_TREE;
add6ee5e 8759 *strict_overflow_p = true;
26e1261a 8760 }
8761
dc5e8aba 8762 /* Now build the constant reduced in magnitude. But not if that
8763 would produce one outside of its types range. */
8764 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8765 && ((sgn0 == 1
8766 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8767 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8768 || (sgn0 == -1
8769 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8770 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8771 /* We cannot swap the comparison here as that would cause us to
8772 endlessly recurse. */
8773 return NULL_TREE;
8774
26e1261a 8775 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
dc5e8aba 8776 cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
26e1261a 8777 if (code0 != INTEGER_CST)
8778 t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8779
8780 /* If swapping might yield to a more canonical form, do so. */
8781 if (swap)
8782 return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8783 else
8784 return fold_build2 (code, type, t, arg1);
8785}
8786
8787/* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8788 overflow further. Try to decrease the magnitude of constants involved
8789 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8790 and put sole constants at the second argument position.
8791 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8792
8793static tree
8794maybe_canonicalize_comparison (enum tree_code code, tree type,
8795 tree arg0, tree arg1)
8796{
8797 tree t;
add6ee5e 8798 bool strict_overflow_p;
8799 const char * const warnmsg = G_("assuming signed overflow does not occur "
8800 "when reducing constant in comparison");
26e1261a 8801
26e1261a 8802 /* Try canonicalization by simplifying arg0. */
add6ee5e 8803 strict_overflow_p = false;
8804 t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8805 &strict_overflow_p);
26e1261a 8806 if (t)
add6ee5e 8807 {
8808 if (strict_overflow_p)
8809 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8810 return t;
8811 }
26e1261a 8812
8813 /* Try canonicalization by simplifying arg1 using the swapped
fa7637bd 8814 comparison. */
26e1261a 8815 code = swap_tree_comparison (code);
add6ee5e 8816 strict_overflow_p = false;
8817 t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8818 &strict_overflow_p);
8819 if (t && strict_overflow_p)
8820 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8821 return t;
26e1261a 8822}
8823
9fa14f44 8824/* Return whether BASE + OFFSET + BITPOS may wrap around the address
8825 space. This is used to avoid issuing overflow warnings for
8826 expressions like &p->x which can not wrap. */
8827
8828static bool
8829pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8830{
9fa14f44 8831 unsigned HOST_WIDE_INT offset_low, total_low;
7ded8d6e 8832 HOST_WIDE_INT size, offset_high, total_high;
9fa14f44 8833
8834 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8835 return true;
8836
8837 if (bitpos < 0)
8838 return true;
8839
9fa14f44 8840 if (offset == NULL_TREE)
8841 {
8842 offset_low = 0;
8843 offset_high = 0;
8844 }
8845 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8846 return true;
8847 else
8848 {
8849 offset_low = TREE_INT_CST_LOW (offset);
8850 offset_high = TREE_INT_CST_HIGH (offset);
8851 }
8852
8853 if (add_double_with_sign (offset_low, offset_high,
8854 bitpos / BITS_PER_UNIT, 0,
8855 &total_low, &total_high,
8856 true))
8857 return true;
8858
7ded8d6e 8859 if (total_high != 0)
9fa14f44 8860 return true;
7ded8d6e 8861
8862 size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8863 if (size <= 0)
8864 return true;
8865
8866 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8867 array. */
8868 if (TREE_CODE (base) == ADDR_EXPR)
8869 {
8870 HOST_WIDE_INT base_size;
8871
8872 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8873 if (base_size > 0 && size < base_size)
8874 size = base_size;
8875 }
8876
8877 return total_low > (unsigned HOST_WIDE_INT) size;
9fa14f44 8878}
8879
6a451e87 8880/* Subroutine of fold_binary. This routine performs all of the
8881 transformations that are common to the equality/inequality
8882 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8883 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8884 fold_binary should call fold_binary. Fold a comparison with
8885 tree code CODE and type TYPE with operands OP0 and OP1. Return
8886 the folded comparison or NULL_TREE. */
8887
8888static tree
8889fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8890{
8891 tree arg0, arg1, tem;
8892
8893 arg0 = op0;
8894 arg1 = op1;
8895
8896 STRIP_SIGN_NOPS (arg0);
8897 STRIP_SIGN_NOPS (arg1);
8898
8899 tem = fold_relational_const (code, type, arg0, arg1);
8900 if (tem != NULL_TREE)
8901 return tem;
8902
8903 /* If one arg is a real or integer constant, put it last. */
8904 if (tree_swap_operands_p (arg0, arg1, true))
8905 return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8906
6a451e87 8907 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
8908 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8909 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8910 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
981eb798 8911 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
6a451e87 8912 && (TREE_CODE (arg1) == INTEGER_CST
8913 && !TREE_OVERFLOW (arg1)))
8914 {
8915 tree const1 = TREE_OPERAND (arg0, 1);
8916 tree const2 = arg1;
8917 tree variable = TREE_OPERAND (arg0, 0);
8918 tree lhs;
8919 int lhs_add;
8920 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8921
8922 lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8923 TREE_TYPE (arg1), const2, const1);
a9995c22 8924
8925 /* If the constant operation overflowed this can be
8926 simplified as a comparison against INT_MAX/INT_MIN. */
8927 if (TREE_CODE (lhs) == INTEGER_CST
8928 && TREE_OVERFLOW (lhs))
8929 {
8930 int const1_sgn = tree_int_cst_sgn (const1);
8931 enum tree_code code2 = code;
8932
8933 /* Get the sign of the constant on the lhs if the
8934 operation were VARIABLE + CONST1. */
8935 if (TREE_CODE (arg0) == MINUS_EXPR)
8936 const1_sgn = -const1_sgn;
8937
8938 /* The sign of the constant determines if we overflowed
8939 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8940 Canonicalize to the INT_MIN overflow by swapping the comparison
8941 if necessary. */
8942 if (const1_sgn == -1)
8943 code2 = swap_tree_comparison (code);
8944
8945 /* We now can look at the canonicalized case
8946 VARIABLE + 1 CODE2 INT_MIN
8947 and decide on the result. */
8948 if (code2 == LT_EXPR
8949 || code2 == LE_EXPR
8950 || code2 == EQ_EXPR)
8951 return omit_one_operand (type, boolean_false_node, variable);
8952 else if (code2 == NE_EXPR
8953 || code2 == GE_EXPR
8954 || code2 == GT_EXPR)
8955 return omit_one_operand (type, boolean_true_node, variable);
8956 }
8957
6a451e87 8958 if (TREE_CODE (lhs) == TREE_CODE (arg1)
8959 && (TREE_CODE (lhs) != INTEGER_CST
8960 || !TREE_OVERFLOW (lhs)))
add6ee5e 8961 {
8962 fold_overflow_warning (("assuming signed overflow does not occur "
8963 "when changing X +- C1 cmp C2 to "
8964 "X cmp C1 +- C2"),
8965 WARN_STRICT_OVERFLOW_COMPARISON);
8966 return fold_build2 (code, type, variable, lhs);
8967 }
6a451e87 8968 }
8969
cb8fc1a4 8970 /* For comparisons of pointers we can decompose it to a compile time
8971 comparison of the base objects and the offsets into the object.
ad92d3a8 8972 This requires at least one operand being an ADDR_EXPR or a
8973 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
cb8fc1a4 8974 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8975 && (TREE_CODE (arg0) == ADDR_EXPR
ad92d3a8 8976 || TREE_CODE (arg1) == ADDR_EXPR
8977 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8978 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
cb8fc1a4 8979 {
8980 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8981 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8982 enum machine_mode mode;
8983 int volatilep, unsignedp;
ffdf1c47 8984 bool indirect_base0 = false, indirect_base1 = false;
cb8fc1a4 8985
8986 /* Get base and offset for the access. Strip ADDR_EXPR for
8987 get_inner_reference, but put it back by stripping INDIRECT_REF
ffdf1c47 8988 off the base object if possible. indirect_baseN will be true
8989 if baseN is not an address but refers to the object itself. */
cb8fc1a4 8990 base0 = arg0;
8991 if (TREE_CODE (arg0) == ADDR_EXPR)
8992 {
8993 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8994 &bitsize, &bitpos0, &offset0, &mode,
8995 &unsignedp, &volatilep, false);
8996 if (TREE_CODE (base0) == INDIRECT_REF)
8997 base0 = TREE_OPERAND (base0, 0);
8998 else
8999 indirect_base0 = true;
9000 }
ad92d3a8 9001 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9002 {
9003 base0 = TREE_OPERAND (arg0, 0);
9004 offset0 = TREE_OPERAND (arg0, 1);
9005 }
cb8fc1a4 9006
9007 base1 = arg1;
9008 if (TREE_CODE (arg1) == ADDR_EXPR)
9009 {
9010 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
9011 &bitsize, &bitpos1, &offset1, &mode,
9012 &unsignedp, &volatilep, false);
ffdf1c47 9013 if (TREE_CODE (base1) == INDIRECT_REF)
cb8fc1a4 9014 base1 = TREE_OPERAND (base1, 0);
ffdf1c47 9015 else
9016 indirect_base1 = true;
cb8fc1a4 9017 }
ad92d3a8 9018 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9019 {
9020 base1 = TREE_OPERAND (arg1, 0);
9021 offset1 = TREE_OPERAND (arg1, 1);
9022 }
cb8fc1a4 9023
9024 /* If we have equivalent bases we might be able to simplify. */
ffdf1c47 9025 if (indirect_base0 == indirect_base1
cb8fc1a4 9026 && operand_equal_p (base0, base1, 0))
9027 {
9028 /* We can fold this expression to a constant if the non-constant
9029 offset parts are equal. */
9fa14f44 9030 if ((offset0 == offset1
9031 || (offset0 && offset1
9032 && operand_equal_p (offset0, offset1, 0)))
9033 && (code == EQ_EXPR
9034 || code == NE_EXPR
9035 || POINTER_TYPE_OVERFLOW_UNDEFINED))
9036
cb8fc1a4 9037 {
9fa14f44 9038 if (code != EQ_EXPR
9039 && code != NE_EXPR
9040 && bitpos0 != bitpos1
9041 && (pointer_may_wrap_p (base0, offset0, bitpos0)
9042 || pointer_may_wrap_p (base1, offset1, bitpos1)))
9043 fold_overflow_warning (("assuming pointer wraparound does not "
9044 "occur when comparing P +- C1 with "
9045 "P +- C2"),
9046 WARN_STRICT_OVERFLOW_CONDITIONAL);
9047
cb8fc1a4 9048 switch (code)
9049 {
9050 case EQ_EXPR:
bd233430 9051 return constant_boolean_node (bitpos0 == bitpos1, type);
cb8fc1a4 9052 case NE_EXPR:
bd233430 9053 return constant_boolean_node (bitpos0 != bitpos1, type);
cb8fc1a4 9054 case LT_EXPR:
bd233430 9055 return constant_boolean_node (bitpos0 < bitpos1, type);
cb8fc1a4 9056 case LE_EXPR:
bd233430 9057 return constant_boolean_node (bitpos0 <= bitpos1, type);
cb8fc1a4 9058 case GE_EXPR:
bd233430 9059 return constant_boolean_node (bitpos0 >= bitpos1, type);
cb8fc1a4 9060 case GT_EXPR:
bd233430 9061 return constant_boolean_node (bitpos0 > bitpos1, type);
cb8fc1a4 9062 default:;
9063 }
9064 }
9065 /* We can simplify the comparison to a comparison of the variable
9066 offset parts if the constant offset parts are equal.
9067 Be careful to use signed size type here because otherwise we
9068 mess with array offsets in the wrong way. This is possible
9069 because pointer arithmetic is restricted to retain within an
9070 object and overflow on pointer differences is undefined as of
9071 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
e867fa7f 9072 else if (bitpos0 == bitpos1
9073 && ((code == EQ_EXPR || code == NE_EXPR)
9074 || POINTER_TYPE_OVERFLOW_UNDEFINED))
cb8fc1a4 9075 {
9076 tree signed_size_type_node;
9077 signed_size_type_node = signed_type_for (size_type_node);
9078
9079 /* By converting to signed size type we cover middle-end pointer
9080 arithmetic which operates on unsigned pointer types of size
9081 type size and ARRAY_REF offsets which are properly sign or
9082 zero extended from their type in case it is narrower than
9083 size type. */
9084 if (offset0 == NULL_TREE)
9085 offset0 = build_int_cst (signed_size_type_node, 0);
9086 else
9087 offset0 = fold_convert (signed_size_type_node, offset0);
9088 if (offset1 == NULL_TREE)
9089 offset1 = build_int_cst (signed_size_type_node, 0);
9090 else
9091 offset1 = fold_convert (signed_size_type_node, offset1);
9092
9fa14f44 9093 if (code != EQ_EXPR
9094 && code != NE_EXPR
9095 && (pointer_may_wrap_p (base0, offset0, bitpos0)
9096 || pointer_may_wrap_p (base1, offset1, bitpos1)))
e867fa7f 9097 fold_overflow_warning (("assuming pointer wraparound does not "
9098 "occur when comparing P +- C1 with "
9099 "P +- C2"),
9100 WARN_STRICT_OVERFLOW_COMPARISON);
9101
cb8fc1a4 9102 return fold_build2 (code, type, offset0, offset1);
9103 }
9104 }
ffdf1c47 9105 /* For non-equal bases we can simplify if they are addresses
9106 of local binding decls or constants. */
9107 else if (indirect_base0 && indirect_base1
9108 /* We know that !operand_equal_p (base0, base1, 0)
dd691fd8 9109 because the if condition was false. But make
9110 sure two decls are not the same. */
9111 && base0 != base1
ffdf1c47 9112 && TREE_CODE (arg0) == ADDR_EXPR
9113 && TREE_CODE (arg1) == ADDR_EXPR
dd691fd8 9114 && (((TREE_CODE (base0) == VAR_DECL
9115 || TREE_CODE (base0) == PARM_DECL)
ffdf1c47 9116 && (targetm.binds_local_p (base0)
9117 || CONSTANT_CLASS_P (base1)))
9118 || CONSTANT_CLASS_P (base0))
dd691fd8 9119 && (((TREE_CODE (base1) == VAR_DECL
9120 || TREE_CODE (base1) == PARM_DECL)
ffdf1c47 9121 && (targetm.binds_local_p (base1)
9122 || CONSTANT_CLASS_P (base0)))
9123 || CONSTANT_CLASS_P (base1)))
9124 {
9125 if (code == EQ_EXPR)
9126 return omit_two_operands (type, boolean_false_node, arg0, arg1);
9127 else if (code == NE_EXPR)
9128 return omit_two_operands (type, boolean_true_node, arg0, arg1);
9129 }
9130 /* For equal offsets we can simplify to a comparison of the
9131 base addresses. */
9132 else if (bitpos0 == bitpos1
9133 && (indirect_base0
9134 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9135 && (indirect_base1
9136 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9137 && ((offset0 == offset1)
9138 || (offset0 && offset1
9139 && operand_equal_p (offset0, offset1, 0))))
9140 {
9141 if (indirect_base0)
86f2ad37 9142 base0 = build_fold_addr_expr (base0);
ffdf1c47 9143 if (indirect_base1)
86f2ad37 9144 base1 = build_fold_addr_expr (base1);
ffdf1c47 9145 return fold_build2 (code, type, base0, base1);
9146 }
cb8fc1a4 9147 }
9148
91ceb6b7 9149 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9150 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
9151 the resulting offset is smaller in absolute value than the
9152 original one. */
981eb798 9153 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
91ceb6b7 9154 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9155 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9156 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9157 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9158 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9159 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9160 {
9161 tree const1 = TREE_OPERAND (arg0, 1);
9162 tree const2 = TREE_OPERAND (arg1, 1);
9163 tree variable1 = TREE_OPERAND (arg0, 0);
9164 tree variable2 = TREE_OPERAND (arg1, 0);
9165 tree cst;
add6ee5e 9166 const char * const warnmsg = G_("assuming signed overflow does not "
9167 "occur when combining constants around "
9168 "a comparison");
91ceb6b7 9169
9170 /* Put the constant on the side where it doesn't overflow and is
9171 of lower absolute value than before. */
9172 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9173 ? MINUS_EXPR : PLUS_EXPR,
9174 const2, const1, 0);
9175 if (!TREE_OVERFLOW (cst)
9176 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
add6ee5e 9177 {
9178 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9179 return fold_build2 (code, type,
9180 variable1,
9181 fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
9182 variable2, cst));
9183 }
91ceb6b7 9184
9185 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9186 ? MINUS_EXPR : PLUS_EXPR,
9187 const1, const2, 0);
9188 if (!TREE_OVERFLOW (cst)
9189 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
add6ee5e 9190 {
9191 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9192 return fold_build2 (code, type,
9193 fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
9194 variable1, cst),
9195 variable2);
9196 }
91ceb6b7 9197 }
9198
9112c6d3 9199 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9200 signed arithmetic case. That form is created by the compiler
9201 often enough for folding it to be of value. One example is in
9202 computing loop trip counts after Operator Strength Reduction. */
981eb798 9203 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9112c6d3 9204 && TREE_CODE (arg0) == MULT_EXPR
9205 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9206 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9207 && integer_zerop (arg1))
9208 {
9209 tree const1 = TREE_OPERAND (arg0, 1);
9210 tree const2 = arg1; /* zero */
9211 tree variable1 = TREE_OPERAND (arg0, 0);
9212 enum tree_code cmp_code = code;
9213
9214 gcc_assert (!integer_zerop (const1));
9215
add6ee5e 9216 fold_overflow_warning (("assuming signed overflow does not occur when "
9217 "eliminating multiplication in comparison "
9218 "with zero"),
9219 WARN_STRICT_OVERFLOW_COMPARISON);
9220
9112c6d3 9221 /* If const1 is negative we swap the sense of the comparison. */
9222 if (tree_int_cst_sgn (const1) < 0)
9223 cmp_code = swap_tree_comparison (cmp_code);
9224
9225 return fold_build2 (cmp_code, type, variable1, const2);
9226 }
9227
4fafe0c4 9228 tem = maybe_canonicalize_comparison (code, type, op0, op1);
26e1261a 9229 if (tem)
9230 return tem;
9231
6a451e87 9232 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9233 {
9234 tree targ0 = strip_float_extensions (arg0);
9235 tree targ1 = strip_float_extensions (arg1);
9236 tree newtype = TREE_TYPE (targ0);
9237
9238 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9239 newtype = TREE_TYPE (targ1);
9240
9241 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
9242 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9243 return fold_build2 (code, type, fold_convert (newtype, targ0),
9244 fold_convert (newtype, targ1));
9245
9246 /* (-a) CMP (-b) -> b CMP a */
9247 if (TREE_CODE (arg0) == NEGATE_EXPR
9248 && TREE_CODE (arg1) == NEGATE_EXPR)
9249 return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
9250 TREE_OPERAND (arg0, 0));
9251
9252 if (TREE_CODE (arg1) == REAL_CST)
9253 {
9254 REAL_VALUE_TYPE cst;
9255 cst = TREE_REAL_CST (arg1);
9256
9257 /* (-a) CMP CST -> a swap(CMP) (-CST) */
9258 if (TREE_CODE (arg0) == NEGATE_EXPR)
9259 return fold_build2 (swap_tree_comparison (code), type,
9260 TREE_OPERAND (arg0, 0),
9261 build_real (TREE_TYPE (arg1),
9262 REAL_VALUE_NEGATE (cst)));
9263
9264 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
9265 /* a CMP (-0) -> a CMP 0 */
9266 if (REAL_VALUE_MINUS_ZERO (cst))
9267 return fold_build2 (code, type, arg0,
9268 build_real (TREE_TYPE (arg1), dconst0));
9269
9270 /* x != NaN is always true, other ops are always false. */
9271 if (REAL_VALUE_ISNAN (cst)
9272 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9273 {
9274 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9275 return omit_one_operand (type, tem, arg0);
9276 }
9277
9278 /* Fold comparisons against infinity. */
944017fd 9279 if (REAL_VALUE_ISINF (cst)
9280 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
6a451e87 9281 {
9282 tem = fold_inf_compare (code, type, arg0, arg1);
9283 if (tem != NULL_TREE)
9284 return tem;
9285 }
9286 }
9287
9288 /* If this is a comparison of a real constant with a PLUS_EXPR
9289 or a MINUS_EXPR of a real constant, we can convert it into a
9290 comparison with a revised real constant as long as no overflow
9291 occurs when unsafe_math_optimizations are enabled. */
9292 if (flag_unsafe_math_optimizations
9293 && TREE_CODE (arg1) == REAL_CST
9294 && (TREE_CODE (arg0) == PLUS_EXPR
9295 || TREE_CODE (arg0) == MINUS_EXPR)
9296 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9297 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9298 ? MINUS_EXPR : PLUS_EXPR,
9299 arg1, TREE_OPERAND (arg0, 1), 0))
f96bd2bf 9300 && !TREE_OVERFLOW (tem))
6a451e87 9301 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9302
9303 /* Likewise, we can simplify a comparison of a real constant with
9304 a MINUS_EXPR whose first operand is also a real constant, i.e.
49d060d7 9305 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
9306 floating-point types only if -fassociative-math is set. */
9307 if (flag_associative_math
6a451e87 9308 && TREE_CODE (arg1) == REAL_CST
9309 && TREE_CODE (arg0) == MINUS_EXPR
9310 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9311 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9312 arg1, 0))
f96bd2bf 9313 && !TREE_OVERFLOW (tem))
6a451e87 9314 return fold_build2 (swap_tree_comparison (code), type,
9315 TREE_OPERAND (arg0, 1), tem);
9316
9317 /* Fold comparisons against built-in math functions. */
9318 if (TREE_CODE (arg1) == REAL_CST
9319 && flag_unsafe_math_optimizations
9320 && ! flag_errno_math)
9321 {
9322 enum built_in_function fcode = builtin_mathfn_code (arg0);
9323
9324 if (fcode != END_BUILTINS)
9325 {
9326 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
9327 if (tem != NULL_TREE)
9328 return tem;
9329 }
9330 }
9331 }
9332
6a451e87 9333 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
72dd6141 9334 && CONVERT_EXPR_P (arg0))
6a451e87 9335 {
9336 /* If we are widening one operand of an integer comparison,
9337 see if the other operand is similarly being widened. Perhaps we
9338 can do the comparison in the narrower type. */
9339 tem = fold_widened_comparison (code, type, arg0, arg1);
9340 if (tem)
9341 return tem;
9342
9343 /* Or if we are changing signedness. */
9344 tem = fold_sign_changed_comparison (code, type, arg0, arg1);
9345 if (tem)
9346 return tem;
9347 }
9348
9349 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9350 constant, we can simplify it. */
9351 if (TREE_CODE (arg1) == INTEGER_CST
9352 && (TREE_CODE (arg0) == MIN_EXPR
9353 || TREE_CODE (arg0) == MAX_EXPR)
9354 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9355 {
9356 tem = optimize_minmax_comparison (code, type, op0, op1);
9357 if (tem)
9358 return tem;
9359 }
9360
9361 /* Simplify comparison of something with itself. (For IEEE
9362 floating-point, we can only do some of these simplifications.) */
9363 if (operand_equal_p (arg0, arg1, 0))
9364 {
9365 switch (code)
9366 {
9367 case EQ_EXPR:
9368 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9369 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9370 return constant_boolean_node (1, type);
9371 break;
9372
9373 case GE_EXPR:
9374 case LE_EXPR:
9375 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9376 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9377 return constant_boolean_node (1, type);
9378 return fold_build2 (EQ_EXPR, type, arg0, arg1);
9379
9380 case NE_EXPR:
9381 /* For NE, we can only do this simplification if integer
9382 or we don't honor IEEE floating point NaNs. */
9383 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9384 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9385 break;
9386 /* ... fall through ... */
9387 case GT_EXPR:
9388 case LT_EXPR:
9389 return constant_boolean_node (0, type);
9390 default:
9391 gcc_unreachable ();
9392 }
9393 }
9394
9395 /* If we are comparing an expression that just has comparisons
9396 of two integer values, arithmetic expressions of those comparisons,
9397 and constants, we can simplify it. There are only three cases
9398 to check: the two values can either be equal, the first can be
9399 greater, or the second can be greater. Fold the expression for
9400 those three values. Since each value must be 0 or 1, we have
9401 eight possibilities, each of which corresponds to the constant 0
9402 or 1 or one of the six possible comparisons.
9403
9404 This handles common cases like (a > b) == 0 but also handles
9405 expressions like ((x > y) - (y > x)) > 0, which supposedly
9406 occur in macroized code. */
9407
9408 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9409 {
9410 tree cval1 = 0, cval2 = 0;
9411 int save_p = 0;
9412
9413 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9414 /* Don't handle degenerate cases here; they should already
9415 have been handled anyway. */
9416 && cval1 != 0 && cval2 != 0
9417 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9418 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9419 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9420 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9421 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9422 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9423 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9424 {
9425 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9426 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9427
9428 /* We can't just pass T to eval_subst in case cval1 or cval2
9429 was the same as ARG1. */
9430
9431 tree high_result
9432 = fold_build2 (code, type,
9433 eval_subst (arg0, cval1, maxval,
9434 cval2, minval),
9435 arg1);
9436 tree equal_result
9437 = fold_build2 (code, type,
9438 eval_subst (arg0, cval1, maxval,
9439 cval2, maxval),
9440 arg1);
9441 tree low_result
9442 = fold_build2 (code, type,
9443 eval_subst (arg0, cval1, minval,
9444 cval2, maxval),
9445 arg1);
9446
9447 /* All three of these results should be 0 or 1. Confirm they are.
9448 Then use those values to select the proper code to use. */
9449
9450 if (TREE_CODE (high_result) == INTEGER_CST
9451 && TREE_CODE (equal_result) == INTEGER_CST
9452 && TREE_CODE (low_result) == INTEGER_CST)
9453 {
9454 /* Make a 3-bit mask with the high-order bit being the
9455 value for `>', the next for '=', and the low for '<'. */
9456 switch ((integer_onep (high_result) * 4)
9457 + (integer_onep (equal_result) * 2)
9458 + integer_onep (low_result))
9459 {
9460 case 0:
9461 /* Always false. */
9462 return omit_one_operand (type, integer_zero_node, arg0);
9463 case 1:
9464 code = LT_EXPR;
9465 break;
9466 case 2:
9467 code = EQ_EXPR;
9468 break;
9469 case 3:
9470 code = LE_EXPR;
9471 break;
9472 case 4:
9473 code = GT_EXPR;
9474 break;
9475 case 5:
9476 code = NE_EXPR;
9477 break;
9478 case 6:
9479 code = GE_EXPR;
9480 break;
9481 case 7:
9482 /* Always true. */
9483 return omit_one_operand (type, integer_one_node, arg0);
9484 }
9485
9486 if (save_p)
9487 return save_expr (build2 (code, type, cval1, cval2));
9488 return fold_build2 (code, type, cval1, cval2);
9489 }
9490 }
9491 }
9492
6a451e87 9493 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9494 into a single range test. */
9495 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9496 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9497 && TREE_CODE (arg1) == INTEGER_CST
9498 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9499 && !integer_zerop (TREE_OPERAND (arg0, 1))
9500 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9501 && !TREE_OVERFLOW (arg1))
9502 {
9503 tem = fold_div_compare (code, type, arg0, arg1);
9504 if (tem != NULL_TREE)
9505 return tem;
9506 }
9507
746443a2 9508 /* Fold ~X op ~Y as Y op X. */
9509 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9510 && TREE_CODE (arg1) == BIT_NOT_EXPR)
626b33bb 9511 {
9512 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9513 return fold_build2 (code, type,
9514 fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9515 TREE_OPERAND (arg0, 0));
9516 }
746443a2 9517
9518 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9519 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9520 && TREE_CODE (arg1) == INTEGER_CST)
626b33bb 9521 {
9522 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9523 return fold_build2 (swap_tree_comparison (code), type,
9524 TREE_OPERAND (arg0, 0),
9525 fold_build1 (BIT_NOT_EXPR, cmp_type,
9526 fold_convert (cmp_type, arg1)));
9527 }
746443a2 9528
6a451e87 9529 return NULL_TREE;
9530}
9531
5f4f3617 9532
9533/* Subroutine of fold_binary. Optimize complex multiplications of the
9534 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9535 argument EXPR represents the expression "z" of type TYPE. */
9536
9537static tree
9538fold_mult_zconjz (tree type, tree expr)
9539{
9540 tree itype = TREE_TYPE (type);
9541 tree rpart, ipart, tem;
9542
9543 if (TREE_CODE (expr) == COMPLEX_EXPR)
9544 {
9545 rpart = TREE_OPERAND (expr, 0);
9546 ipart = TREE_OPERAND (expr, 1);
9547 }
9548 else if (TREE_CODE (expr) == COMPLEX_CST)
9549 {
9550 rpart = TREE_REALPART (expr);
9551 ipart = TREE_IMAGPART (expr);
9552 }
9553 else
9554 {
9555 expr = save_expr (expr);
9556 rpart = fold_build1 (REALPART_EXPR, itype, expr);
9557 ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9558 }
9559
9560 rpart = save_expr (rpart);
9561 ipart = save_expr (ipart);
9562 tem = fold_build2 (PLUS_EXPR, itype,
9563 fold_build2 (MULT_EXPR, itype, rpart, rpart),
9564 fold_build2 (MULT_EXPR, itype, ipart, ipart));
9565 return fold_build2 (COMPLEX_EXPR, type, tem,
9566 fold_convert (itype, integer_zero_node));
9567}
9568
9569
4486d2b7 9570/* Subroutine of fold_binary. If P is the value of EXPR, computes
9571 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9572 guarantees that P and N have the same least significant log2(M) bits.
9573 N is not otherwise constrained. In particular, N is not normalized to
9574 0 <= N < M as is common. In general, the precise value of P is unknown.
9575 M is chosen as large as possible such that constant N can be determined.
9576
1c4607fd 9577 Returns M and sets *RESIDUE to N.
9578
9579 If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9580 account. This is not always possible due to PR 35705.
9581 */
4486d2b7 9582
9583static unsigned HOST_WIDE_INT
1c4607fd 9584get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9585 bool allow_func_align)
4486d2b7 9586{
9587 enum tree_code code;
9588
9589 *residue = 0;
9590
9591 code = TREE_CODE (expr);
9592 if (code == ADDR_EXPR)
9593 {
9594 expr = TREE_OPERAND (expr, 0);
9595 if (handled_component_p (expr))
9596 {
9597 HOST_WIDE_INT bitsize, bitpos;
9598 tree offset;
9599 enum machine_mode mode;
9600 int unsignedp, volatilep;
9601
9602 expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9603 &mode, &unsignedp, &volatilep, false);
9604 *residue = bitpos / BITS_PER_UNIT;
9605 if (offset)
9606 {
9607 if (TREE_CODE (offset) == INTEGER_CST)
9608 *residue += TREE_INT_CST_LOW (offset);
9609 else
9610 /* We don't handle more complicated offset expressions. */
9611 return 1;
9612 }
9613 }
9614
1c4607fd 9615 if (DECL_P (expr)
9616 && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
4486d2b7 9617 return DECL_ALIGN_UNIT (expr);
9618 }
9619 else if (code == POINTER_PLUS_EXPR)
9620 {
9621 tree op0, op1;
9622 unsigned HOST_WIDE_INT modulus;
9623 enum tree_code inner_code;
9624
9625 op0 = TREE_OPERAND (expr, 0);
9626 STRIP_NOPS (op0);
1c4607fd 9627 modulus = get_pointer_modulus_and_residue (op0, residue,
9628 allow_func_align);
4486d2b7 9629
9630 op1 = TREE_OPERAND (expr, 1);
9631 STRIP_NOPS (op1);
9632 inner_code = TREE_CODE (op1);
9633 if (inner_code == INTEGER_CST)
9634 {
9635 *residue += TREE_INT_CST_LOW (op1);
9636 return modulus;
9637 }
9638 else if (inner_code == MULT_EXPR)
9639 {
9640 op1 = TREE_OPERAND (op1, 1);
9641 if (TREE_CODE (op1) == INTEGER_CST)
9642 {
9643 unsigned HOST_WIDE_INT align;
9644
9645 /* Compute the greatest power-of-2 divisor of op1. */
9646 align = TREE_INT_CST_LOW (op1);
9647 align &= -align;
9648
9649 /* If align is non-zero and less than *modulus, replace
9650 *modulus with align., If align is 0, then either op1 is 0
9651 or the greatest power-of-2 divisor of op1 doesn't fit in an
9652 unsigned HOST_WIDE_INT. In either case, no additional
9653 constraint is imposed. */
9654 if (align)
9655 modulus = MIN (modulus, align);
9656
9657 return modulus;
9658 }
9659 }
9660 }
9661
9662 /* If we get here, we were unable to determine anything useful about the
9663 expression. */
9664 return 1;
9665}
9666
9667
0d3711e2 9668/* Fold a binary expression of code CODE and type TYPE with operands
9669 OP0 and OP1. Return the folded expression if folding is
9670 successful. Otherwise, return NULL_TREE. */
fef10b60 9671
d3858e14 9672tree
0052b98e 9673fold_binary (enum tree_code code, tree type, tree op0, tree op1)
fef10b60 9674{
fef10b60 9675 enum tree_code_class kind = TREE_CODE_CLASS (code);
6a451e87 9676 tree arg0, arg1, tem;
9677 tree t1 = NULL_TREE;
add6ee5e 9678 bool strict_overflow_p;
fef10b60 9679
75a70cf9 9680 gcc_assert (IS_EXPR_CODE_CLASS (kind)
32cef1cc 9681 && TREE_CODE_LENGTH (code) == 2
9682 && op0 != NULL_TREE
9683 && op1 != NULL_TREE);
fef10b60 9684
0052b98e 9685 arg0 = op0;
9686 arg1 = op1;
2431fed3 9687
32cef1cc 9688 /* Strip any conversions that don't change the mode. This is
9689 safe for every expression, except for a comparison expression
9690 because its signedness is derived from its operands. So, in
9691 the latter case, only strip conversions that don't change the
7db628eb 9692 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9693 preserved.
fef10b60 9694
32cef1cc 9695 Note that this is done as an internal manipulation within the
9696 constant folder, in order to find the simplest representation
9697 of the arguments so that their form can be studied. In any
9698 cases, the appropriate type conversions should be put back in
9699 the tree that will get out of the constant folder. */
fef10b60 9700
7db628eb 9701 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
32cef1cc 9702 {
9703 STRIP_SIGN_NOPS (arg0);
9704 STRIP_SIGN_NOPS (arg1);
2431fed3 9705 }
32cef1cc 9706 else
2431fed3 9707 {
32cef1cc 9708 STRIP_NOPS (arg0);
9709 STRIP_NOPS (arg1);
9710 }
fef10b60 9711
32cef1cc 9712 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9713 constant but we can't do arithmetic on them. */
9714 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9715 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
06f0b99c 9716 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9717 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
32cef1cc 9718 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9719 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9720 {
9721 if (kind == tcc_binary)
06f0b99c 9722 {
9723 /* Make sure type and arg0 have the same saturating flag. */
9724 gcc_assert (TYPE_SATURATING (type)
9725 == TYPE_SATURATING (TREE_TYPE (arg0)));
9726 tem = const_binop (code, arg0, arg1, 0);
9727 }
32cef1cc 9728 else if (kind == tcc_comparison)
9729 tem = fold_relational_const (code, type, arg0, arg1);
2431fed3 9730 else
32cef1cc 9731 tem = NULL_TREE;
2431fed3 9732
32cef1cc 9733 if (tem != NULL_TREE)
9734 {
9735 if (TREE_TYPE (tem) != type)
9736 tem = fold_convert (type, tem);
9737 return tem;
9738 }
fef10b60 9739 }
9740
9741 /* If this is a commutative operation, and ARG0 is a constant, move it
9742 to ARG1 to reduce the number of tests below. */
9743 if (commutative_tree_code (code)
9744 && tree_swap_operands_p (arg0, arg1, true))
7ab7fd4f 9745 return fold_build2 (code, type, op1, op0);
fef10b60 9746
32cef1cc 9747 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
fef10b60 9748
9749 First check for cases where an arithmetic operation is applied to a
9750 compound, conditional, or comparison operation. Push the arithmetic
9751 operation inside the compound or conditional to see if any folding
9752 can then be done. Convert comparison to conditional for this purpose.
9753 The also optimizes non-constant cases that used to be done in
9754 expand_expr.
9755
9756 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9757 one of the operands is a comparison and the other is a comparison, a
9758 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9759 code below would make the expression more complex. Change it to a
9760 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9761 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9762
9763 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9764 || code == EQ_EXPR || code == NE_EXPR)
9765 && ((truth_value_p (TREE_CODE (arg0))
9766 && (truth_value_p (TREE_CODE (arg1))
9767 || (TREE_CODE (arg1) == BIT_AND_EXPR
9768 && integer_onep (TREE_OPERAND (arg1, 1)))))
9769 || (truth_value_p (TREE_CODE (arg1))
9770 && (truth_value_p (TREE_CODE (arg0))
9771 || (TREE_CODE (arg0) == BIT_AND_EXPR
9772 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9773 {
7ab7fd4f 9774 tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9775 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9776 : TRUTH_XOR_EXPR,
9777 boolean_type_node,
9778 fold_convert (boolean_type_node, arg0),
9779 fold_convert (boolean_type_node, arg1));
fef10b60 9780
9781 if (code == EQ_EXPR)
9782 tem = invert_truthvalue (tem);
9783
ce04dcdc 9784 return fold_convert (type, tem);
fef10b60 9785 }
9786
ea43e860 9787 if (TREE_CODE_CLASS (code) == tcc_binary
9788 || TREE_CODE_CLASS (code) == tcc_comparison)
fef10b60 9789 {
9790 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9791 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
ea43e860 9792 fold_build2 (code, type,
02dc5c73 9793 fold_convert (TREE_TYPE (op0),
9794 TREE_OPERAND (arg0, 1)),
9795 op1));
fef10b60 9796 if (TREE_CODE (arg1) == COMPOUND_EXPR
9797 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9798 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
02dc5c73 9799 fold_build2 (code, type, op0,
9800 fold_convert (TREE_TYPE (op1),
9801 TREE_OPERAND (arg1, 1))));
fef10b60 9802
9803 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9804 {
1ebe9a83 9805 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9806 arg0, arg1,
fef10b60 9807 /*cond_first_p=*/1);
9808 if (tem != NULL_TREE)
9809 return tem;
9810 }
9811
9812 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9813 {
1ebe9a83 9814 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9815 arg1, arg0,
fef10b60 9816 /*cond_first_p=*/0);
9817 if (tem != NULL_TREE)
9818 return tem;
9819 }
9820 }
9821
9822 switch (code)
9823 {
0de36bdb 9824 case POINTER_PLUS_EXPR:
9825 /* 0 +p index -> (type)index */
9826 if (integer_zerop (arg0))
9827 return non_lvalue (fold_convert (type, arg1));
9828
9829 /* PTR +p 0 -> PTR */
9830 if (integer_zerop (arg1))
9831 return non_lvalue (fold_convert (type, arg0));
9832
9833 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9834 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9835 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9836 return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9837 fold_convert (sizetype, arg1),
9838 fold_convert (sizetype, arg0)));
9839
69f111d5 9840 /* index +p PTR -> PTR +p index */
9841 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9842 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9843 return fold_build2 (POINTER_PLUS_EXPR, type,
9844 fold_convert (type, arg1),
9845 fold_convert (sizetype, arg0));
9846
0de36bdb 9847 /* (PTR +p B) +p A -> PTR +p (B + A) */
9848 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9849 {
9850 tree inner;
9851 tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9852 tree arg00 = TREE_OPERAND (arg0, 0);
15796f61 9853 inner = fold_build2 (PLUS_EXPR, sizetype,
9854 arg01, fold_convert (sizetype, arg1));
9855 return fold_convert (type,
9856 fold_build2 (POINTER_PLUS_EXPR,
9857 TREE_TYPE (arg00), arg00, inner));
0de36bdb 9858 }
9859
9860 /* PTR_CST +p CST -> CST1 */
9861 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9862 return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9863
9864 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9865 of the array. Loop optimizer sometimes produce this type of
9866 expressions. */
9867 if (TREE_CODE (arg0) == ADDR_EXPR)
9868 {
9869 tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9870 if (tem)
9871 return fold_convert (type, tem);
9872 }
9873
9874 return NULL_TREE;
15796f61 9875
fef10b60 9876 case PLUS_EXPR:
9877 /* A + (-B) -> A - B */
9878 if (TREE_CODE (arg1) == NEGATE_EXPR)
7bb1accd 9879 return fold_build2 (MINUS_EXPR, type,
9880 fold_convert (type, arg0),
9881 fold_convert (type, TREE_OPERAND (arg1, 0)));
fef10b60 9882 /* (-A) + B -> B - A */
9883 if (TREE_CODE (arg0) == NEGATE_EXPR
9884 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
7bb1accd 9885 return fold_build2 (MINUS_EXPR, type,
9886 fold_convert (type, arg1),
9887 fold_convert (type, TREE_OPERAND (arg0, 0)));
1c9af531 9888
b30baf88 9889 if (INTEGRAL_TYPE_P (type))
fef10b60 9890 {
b30baf88 9891 /* Convert ~A + 1 to -A. */
9892 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9893 && integer_onep (arg1))
cad86ee8 9894 return fold_build1 (NEGATE_EXPR, type,
9895 fold_convert (type, TREE_OPERAND (arg0, 0)));
fef10b60 9896
0673139b 9897 /* ~X + X is -1. */
9898 if (TREE_CODE (arg0) == BIT_NOT_EXPR
981eb798 9899 && !TYPE_OVERFLOW_TRAPS (type))
0673139b 9900 {
5bd7f4f4 9901 tree tem = TREE_OPERAND (arg0, 0);
9902
9903 STRIP_NOPS (tem);
9904 if (operand_equal_p (tem, arg1, 0))
9905 {
9906 t1 = build_int_cst_type (type, -1);
9907 return omit_one_operand (type, t1, arg1);
9908 }
0673139b 9909 }
9910
9911 /* X + ~X is -1. */
9912 if (TREE_CODE (arg1) == BIT_NOT_EXPR
981eb798 9913 && !TYPE_OVERFLOW_TRAPS (type))
0673139b 9914 {
5bd7f4f4 9915 tree tem = TREE_OPERAND (arg1, 0);
9916
9917 STRIP_NOPS (tem);
9918 if (operand_equal_p (arg0, tem, 0))
9919 {
9920 t1 = build_int_cst_type (type, -1);
9921 return omit_one_operand (type, t1, arg0);
9922 }
9923 }
d997554f 9924
9925 /* X + (X / CST) * -CST is X % CST. */
9926 if (TREE_CODE (arg1) == MULT_EXPR
9927 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9928 && operand_equal_p (arg0,
9929 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9930 {
9931 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9932 tree cst1 = TREE_OPERAND (arg1, 1);
9933 tree sum = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1, cst0);
9934 if (sum && integer_zerop (sum))
9935 return fold_convert (type,
9936 fold_build2 (TRUNC_MOD_EXPR,
9937 TREE_TYPE (arg0), arg0, cst0));
9938 }
b30baf88 9939 }
9940
9941 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
49d060d7 9942 same or one. Make sure type is not saturating.
9943 fold_plusminus_mult_expr will re-associate. */
b30baf88 9944 if ((TREE_CODE (arg0) == MULT_EXPR
9945 || TREE_CODE (arg1) == MULT_EXPR)
06f0b99c 9946 && !TYPE_SATURATING (type)
49d060d7 9947 && (!FLOAT_TYPE_P (type) || flag_associative_math))
b30baf88 9948 {
9949 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9950 if (tem)
9951 return tem;
9952 }
9953
9954 if (! FLOAT_TYPE_P (type))
9955 {
9956 if (integer_zerop (arg1))
9957 return non_lvalue (fold_convert (type, arg0));
0673139b 9958
fef10b60 9959 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9960 with a constant, and the two constants have no bits in common,
9961 we should treat this as a BIT_IOR_EXPR since this may produce more
9962 simplifications. */
9963 if (TREE_CODE (arg0) == BIT_AND_EXPR
9964 && TREE_CODE (arg1) == BIT_AND_EXPR
9965 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9966 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9967 && integer_zerop (const_binop (BIT_AND_EXPR,
9968 TREE_OPERAND (arg0, 1),
9969 TREE_OPERAND (arg1, 1), 0)))
9970 {
9971 code = BIT_IOR_EXPR;
9972 goto bit_ior;
9973 }
9974
9975 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9976 (plus (plus (mult) (mult)) (foo)) so that we can
9977 take advantage of the factoring cases below. */
9978 if (((TREE_CODE (arg0) == PLUS_EXPR
9979 || TREE_CODE (arg0) == MINUS_EXPR)
9980 && TREE_CODE (arg1) == MULT_EXPR)
9981 || ((TREE_CODE (arg1) == PLUS_EXPR
9982 || TREE_CODE (arg1) == MINUS_EXPR)
9983 && TREE_CODE (arg0) == MULT_EXPR))
9984 {
9985 tree parg0, parg1, parg, marg;
9986 enum tree_code pcode;
9987
9988 if (TREE_CODE (arg1) == MULT_EXPR)
9989 parg = arg0, marg = arg1;
9990 else
9991 parg = arg1, marg = arg0;
9992 pcode = TREE_CODE (parg);
9993 parg0 = TREE_OPERAND (parg, 0);
9994 parg1 = TREE_OPERAND (parg, 1);
9995 STRIP_NOPS (parg0);
9996 STRIP_NOPS (parg1);
9997
9998 if (TREE_CODE (parg0) == MULT_EXPR
9999 && TREE_CODE (parg1) != MULT_EXPR)
7ab7fd4f 10000 return fold_build2 (pcode, type,
10001 fold_build2 (PLUS_EXPR, type,
10002 fold_convert (type, parg0),
10003 fold_convert (type, marg)),
10004 fold_convert (type, parg1));
fef10b60 10005 if (TREE_CODE (parg0) != MULT_EXPR
10006 && TREE_CODE (parg1) == MULT_EXPR)
7ab7fd4f 10007 return fold_build2 (PLUS_EXPR, type,
10008 fold_convert (type, parg0),
10009 fold_build2 (pcode, type,
10010 fold_convert (type, marg),
10011 fold_convert (type,
10012 parg1)));
fef10b60 10013 }
fef10b60 10014 }
10015 else
10016 {
10017 /* See if ARG1 is zero and X + ARG1 reduces to X. */
10018 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10019 return non_lvalue (fold_convert (type, arg0));
10020
10021 /* Likewise if the operands are reversed. */
10022 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10023 return non_lvalue (fold_convert (type, arg1));
10024
10025 /* Convert X + -C into X - C. */
10026 if (TREE_CODE (arg1) == REAL_CST
10027 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10028 {
10029 tem = fold_negate_const (arg1, type);
10030 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
7ab7fd4f 10031 return fold_build2 (MINUS_EXPR, type,
10032 fold_convert (type, arg0),
10033 fold_convert (type, tem));
fef10b60 10034 }
10035
ed97ac4e 10036 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10037 to __complex__ ( x, y ). This is not the same for SNaNs or
1af0d139 10038 if signed zeros are involved. */
ed97ac4e 10039 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10040 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10041 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10042 {
10043 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10044 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10045 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10046 bool arg0rz = false, arg0iz = false;
10047 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10048 || (arg0i && (arg0iz = real_zerop (arg0i))))
10049 {
10050 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10051 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10052 if (arg0rz && arg1i && real_zerop (arg1i))
10053 {
10054 tree rp = arg1r ? arg1r
10055 : build1 (REALPART_EXPR, rtype, arg1);
10056 tree ip = arg0i ? arg0i
10057 : build1 (IMAGPART_EXPR, rtype, arg0);
10058 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10059 }
10060 else if (arg0iz && arg1r && real_zerop (arg1r))
10061 {
10062 tree rp = arg0r ? arg0r
10063 : build1 (REALPART_EXPR, rtype, arg0);
10064 tree ip = arg1i ? arg1i
10065 : build1 (IMAGPART_EXPR, rtype, arg1);
10066 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10067 }
10068 }
10069 }
10070
1e5de3bd 10071 if (flag_unsafe_math_optimizations
429f2f90 10072 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10073 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10074 && (tem = distribute_real_division (code, type, arg0, arg1)))
10075 return tem;
10076
fef10b60 10077 /* Convert x+x into x*2.0. */
10078 if (operand_equal_p (arg0, arg1, 0)
10079 && SCALAR_FLOAT_TYPE_P (type))
7ab7fd4f 10080 return fold_build2 (MULT_EXPR, type, arg0,
10081 build_real (type, dconst2));
fef10b60 10082
49d060d7 10083 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10084 We associate floats only if the user has specified
10085 -fassociative-math. */
10086 if (flag_associative_math
fef10b60 10087 && TREE_CODE (arg1) == PLUS_EXPR
10088 && TREE_CODE (arg0) != MULT_EXPR)
10089 {
10090 tree tree10 = TREE_OPERAND (arg1, 0);
10091 tree tree11 = TREE_OPERAND (arg1, 1);
10092 if (TREE_CODE (tree11) == MULT_EXPR
10093 && TREE_CODE (tree10) == MULT_EXPR)
10094 {
10095 tree tree0;
7ab7fd4f 10096 tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
10097 return fold_build2 (PLUS_EXPR, type, tree0, tree11);
fef10b60 10098 }
10099 }
49d060d7 10100 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10101 We associate floats only if the user has specified
10102 -fassociative-math. */
10103 if (flag_associative_math
fef10b60 10104 && TREE_CODE (arg0) == PLUS_EXPR
10105 && TREE_CODE (arg1) != MULT_EXPR)
10106 {
10107 tree tree00 = TREE_OPERAND (arg0, 0);
10108 tree tree01 = TREE_OPERAND (arg0, 1);
10109 if (TREE_CODE (tree01) == MULT_EXPR
10110 && TREE_CODE (tree00) == MULT_EXPR)
10111 {
10112 tree tree0;
7ab7fd4f 10113 tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
10114 return fold_build2 (PLUS_EXPR, type, tree00, tree0);
fef10b60 10115 }
10116 }
10117 }
10118
10119 bit_rotate:
10120 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10121 is a rotate of A by C1 bits. */
10122 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10123 is a rotate of A by B bits. */
10124 {
10125 enum tree_code code0, code1;
6295ca72 10126 tree rtype;
fef10b60 10127 code0 = TREE_CODE (arg0);
10128 code1 = TREE_CODE (arg1);
10129 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10130 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10131 && operand_equal_p (TREE_OPERAND (arg0, 0),
10132 TREE_OPERAND (arg1, 0), 0)
6295ca72 10133 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10134 TYPE_UNSIGNED (rtype))
10135 /* Only create rotates in complete modes. Other cases are not
10136 expanded properly. */
10137 && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
fef10b60 10138 {
10139 tree tree01, tree11;
10140 enum tree_code code01, code11;
10141
10142 tree01 = TREE_OPERAND (arg0, 1);
10143 tree11 = TREE_OPERAND (arg1, 1);
10144 STRIP_NOPS (tree01);
10145 STRIP_NOPS (tree11);
10146 code01 = TREE_CODE (tree01);
10147 code11 = TREE_CODE (tree11);
10148 if (code01 == INTEGER_CST
10149 && code11 == INTEGER_CST
10150 && TREE_INT_CST_HIGH (tree01) == 0
10151 && TREE_INT_CST_HIGH (tree11) == 0
10152 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10153 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10154 return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
10155 code0 == LSHIFT_EXPR ? tree01 : tree11);
10156 else if (code11 == MINUS_EXPR)
10157 {
10158 tree tree110, tree111;
10159 tree110 = TREE_OPERAND (tree11, 0);
10160 tree111 = TREE_OPERAND (tree11, 1);
10161 STRIP_NOPS (tree110);
10162 STRIP_NOPS (tree111);
10163 if (TREE_CODE (tree110) == INTEGER_CST
10164 && 0 == compare_tree_int (tree110,
10165 TYPE_PRECISION
10166 (TREE_TYPE (TREE_OPERAND
10167 (arg0, 0))))
10168 && operand_equal_p (tree01, tree111, 0))
10169 return build2 ((code0 == LSHIFT_EXPR
10170 ? LROTATE_EXPR
10171 : RROTATE_EXPR),
10172 type, TREE_OPERAND (arg0, 0), tree01);
10173 }
10174 else if (code01 == MINUS_EXPR)
10175 {
10176 tree tree010, tree011;
10177 tree010 = TREE_OPERAND (tree01, 0);
10178 tree011 = TREE_OPERAND (tree01, 1);
10179 STRIP_NOPS (tree010);
10180 STRIP_NOPS (tree011);
10181 if (TREE_CODE (tree010) == INTEGER_CST
10182 && 0 == compare_tree_int (tree010,
10183 TYPE_PRECISION
10184 (TREE_TYPE (TREE_OPERAND
10185 (arg0, 0))))
10186 && operand_equal_p (tree11, tree011, 0))
10187 return build2 ((code0 != LSHIFT_EXPR
10188 ? LROTATE_EXPR
10189 : RROTATE_EXPR),
10190 type, TREE_OPERAND (arg0, 0), tree11);
10191 }
10192 }
10193 }
10194
10195 associate:
10196 /* In most languages, can't associate operations on floats through
10197 parentheses. Rather than remember where the parentheses were, we
10198 don't associate floats at all, unless the user has specified
49d060d7 10199 -fassociative-math.
06f0b99c 10200 And, we need to make sure type is not saturating. */
fef10b60 10201
49d060d7 10202 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
06f0b99c 10203 && !TYPE_SATURATING (type))
fef10b60 10204 {
10205 tree var0, con0, lit0, minus_lit0;
10206 tree var1, con1, lit1, minus_lit1;
fb700337 10207 bool ok = true;
fef10b60 10208
10209 /* Split both trees into variables, constants, and literals. Then
10210 associate each group together, the constants with literals,
10211 then the result with variables. This increases the chances of
10212 literals being recombined later and of generating relocatable
10213 expressions for the sum of a constant and literal. */
10214 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10215 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10216 code == MINUS_EXPR);
10217
fb700337 10218 /* With undefined overflow we can only associate constants
10219 with one variable. */
e867fa7f 10220 if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
fb700337 10221 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10222 && var0 && var1)
10223 {
10224 tree tmp0 = var0;
10225 tree tmp1 = var1;
10226
10227 if (TREE_CODE (tmp0) == NEGATE_EXPR)
10228 tmp0 = TREE_OPERAND (tmp0, 0);
10229 if (TREE_CODE (tmp1) == NEGATE_EXPR)
10230 tmp1 = TREE_OPERAND (tmp1, 0);
10231 /* The only case we can still associate with two variables
10232 is if they are the same, modulo negation. */
10233 if (!operand_equal_p (tmp0, tmp1, 0))
10234 ok = false;
10235 }
10236
fef10b60 10237 /* Only do something if we found more than two objects. Otherwise,
10238 nothing has changed and we risk infinite recursion. */
fb700337 10239 if (ok
10240 && (2 < ((var0 != 0) + (var1 != 0)
10241 + (con0 != 0) + (con1 != 0)
10242 + (lit0 != 0) + (lit1 != 0)
10243 + (minus_lit0 != 0) + (minus_lit1 != 0))))
fef10b60 10244 {
10245 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
10246 if (code == MINUS_EXPR)
10247 code = PLUS_EXPR;
10248
10249 var0 = associate_trees (var0, var1, code, type);
10250 con0 = associate_trees (con0, con1, code, type);
10251 lit0 = associate_trees (lit0, lit1, code, type);
10252 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
10253
10254 /* Preserve the MINUS_EXPR if the negative part of the literal is
10255 greater than the positive part. Otherwise, the multiplicative
10256 folding code (i.e extract_muldiv) may be fooled in case
10257 unsigned constants are subtracted, like in the following
10258 example: ((X*2 + 4) - 8U)/2. */
10259 if (minus_lit0 && lit0)
10260 {
10261 if (TREE_CODE (lit0) == INTEGER_CST
10262 && TREE_CODE (minus_lit0) == INTEGER_CST
10263 && tree_int_cst_lt (lit0, minus_lit0))
10264 {
10265 minus_lit0 = associate_trees (minus_lit0, lit0,
10266 MINUS_EXPR, type);
10267 lit0 = 0;
10268 }
10269 else
10270 {
10271 lit0 = associate_trees (lit0, minus_lit0,
10272 MINUS_EXPR, type);
10273 minus_lit0 = 0;
10274 }
10275 }
10276 if (minus_lit0)
10277 {
10278 if (con0 == 0)
10279 return fold_convert (type,
10280 associate_trees (var0, minus_lit0,
10281 MINUS_EXPR, type));
10282 else
10283 {
10284 con0 = associate_trees (con0, minus_lit0,
10285 MINUS_EXPR, type);
10286 return fold_convert (type,
10287 associate_trees (var0, con0,
10288 PLUS_EXPR, type));
10289 }
10290 }
10291
10292 con0 = associate_trees (con0, lit0, code, type);
10293 return fold_convert (type, associate_trees (var0, con0,
10294 code, type));
10295 }
10296 }
10297
e7edfbbd 10298 return NULL_TREE;
fef10b60 10299
10300 case MINUS_EXPR:
0de36bdb 10301 /* Pointer simplifications for subtraction, simple reassociations. */
10302 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10303 {
10304 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10305 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10306 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10307 {
10308 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10309 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10310 tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10311 tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10312 return fold_build2 (PLUS_EXPR, type,
10313 fold_build2 (MINUS_EXPR, type, arg00, arg10),
10314 fold_build2 (MINUS_EXPR, type, arg01, arg11));
10315 }
10316 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10317 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10318 {
10319 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10320 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10321 tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
10322 if (tmp)
10323 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
10324 }
10325 }
fef10b60 10326 /* A - (-B) -> A + B */
10327 if (TREE_CODE (arg1) == NEGATE_EXPR)
05e1595b 10328 return fold_build2 (PLUS_EXPR, type, op0,
10329 fold_convert (type, TREE_OPERAND (arg1, 0)));
fef10b60 10330 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10331 if (TREE_CODE (arg0) == NEGATE_EXPR
10332 && (FLOAT_TYPE_P (type)
8a7907c1 10333 || INTEGRAL_TYPE_P (type))
fef10b60 10334 && negate_expr_p (arg1)
10335 && reorder_operands_p (arg0, arg1))
05e1595b 10336 return fold_build2 (MINUS_EXPR, type,
10337 fold_convert (type, negate_expr (arg1)),
10338 fold_convert (type, TREE_OPERAND (arg0, 0)));
c860f88e 10339 /* Convert -A - 1 to ~A. */
10340 if (INTEGRAL_TYPE_P (type)
10341 && TREE_CODE (arg0) == NEGATE_EXPR
0673139b 10342 && integer_onep (arg1)
981eb798 10343 && !TYPE_OVERFLOW_TRAPS (type))
ecf7be71 10344 return fold_build1 (BIT_NOT_EXPR, type,
10345 fold_convert (type, TREE_OPERAND (arg0, 0)));
c860f88e 10346
10347 /* Convert -1 - A to ~A. */
10348 if (INTEGRAL_TYPE_P (type)
10349 && integer_all_onesp (arg0))
a7ede36a 10350 return fold_build1 (BIT_NOT_EXPR, type, op1);
fef10b60 10351
d997554f 10352
10353 /* X - (X / CST) * CST is X % CST. */
10354 if (INTEGRAL_TYPE_P (type)
10355 && TREE_CODE (arg1) == MULT_EXPR
10356 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10357 && operand_equal_p (arg0,
10358 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10359 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10360 TREE_OPERAND (arg1, 1), 0))
10361 return fold_convert (type,
10362 fold_build2 (TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10363 arg0, TREE_OPERAND (arg1, 1)));
10364
fef10b60 10365 if (! FLOAT_TYPE_P (type))
10366 {
32cef1cc 10367 if (integer_zerop (arg0))
fef10b60 10368 return negate_expr (fold_convert (type, arg1));
10369 if (integer_zerop (arg1))
10370 return non_lvalue (fold_convert (type, arg0));
10371
10372 /* Fold A - (A & B) into ~B & A. */
10373 if (!TREE_SIDE_EFFECTS (arg0)
10374 && TREE_CODE (arg1) == BIT_AND_EXPR)
10375 {
10376 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
f6b25e1c 10377 {
10378 tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10379 return fold_build2 (BIT_AND_EXPR, type,
10380 fold_build1 (BIT_NOT_EXPR, type, arg10),
10381 fold_convert (type, arg0));
10382 }
fef10b60 10383 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
f6b25e1c 10384 {
10385 tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10386 return fold_build2 (BIT_AND_EXPR, type,
10387 fold_build1 (BIT_NOT_EXPR, type, arg11),
10388 fold_convert (type, arg0));
10389 }
fef10b60 10390 }
10391
10392 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10393 any power of 2 minus 1. */
10394 if (TREE_CODE (arg0) == BIT_AND_EXPR
10395 && TREE_CODE (arg1) == BIT_AND_EXPR
10396 && operand_equal_p (TREE_OPERAND (arg0, 0),
10397 TREE_OPERAND (arg1, 0), 0))
10398 {
10399 tree mask0 = TREE_OPERAND (arg0, 1);
10400 tree mask1 = TREE_OPERAND (arg1, 1);
7ab7fd4f 10401 tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
fef10b60 10402
10403 if (operand_equal_p (tem, mask1, 0))
10404 {
7ab7fd4f 10405 tem = fold_build2 (BIT_XOR_EXPR, type,
10406 TREE_OPERAND (arg0, 0), mask1);
10407 return fold_build2 (MINUS_EXPR, type, tem, mask1);
fef10b60 10408 }
10409 }
10410 }
10411
10412 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10413 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10414 return non_lvalue (fold_convert (type, arg0));
10415
10416 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10417 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10418 (-ARG1 + ARG0) reduces to -ARG1. */
32cef1cc 10419 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
fef10b60 10420 return negate_expr (fold_convert (type, arg1));
10421
1af0d139 10422 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10423 __complex__ ( x, -y ). This is not the same for SNaNs or if
10424 signed zeros are involved. */
10425 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10426 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10427 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10428 {
10429 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10430 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10431 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10432 bool arg0rz = false, arg0iz = false;
10433 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10434 || (arg0i && (arg0iz = real_zerop (arg0i))))
10435 {
10436 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10437 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10438 if (arg0rz && arg1i && real_zerop (arg1i))
10439 {
10440 tree rp = fold_build1 (NEGATE_EXPR, rtype,
10441 arg1r ? arg1r
10442 : build1 (REALPART_EXPR, rtype, arg1));
10443 tree ip = arg0i ? arg0i
10444 : build1 (IMAGPART_EXPR, rtype, arg0);
10445 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10446 }
10447 else if (arg0iz && arg1r && real_zerop (arg1r))
10448 {
10449 tree rp = arg0r ? arg0r
10450 : build1 (REALPART_EXPR, rtype, arg0);
10451 tree ip = fold_build1 (NEGATE_EXPR, rtype,
10452 arg1i ? arg1i
10453 : build1 (IMAGPART_EXPR, rtype, arg1));
10454 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10455 }
10456 }
10457 }
10458
fef10b60 10459 /* Fold &x - &x. This can happen from &x.foo - &x.
10460 This is unsafe for certain floats even in non-IEEE formats.
10461 In IEEE, it is unsafe because it does wrong for NaNs.
10462 Also note that operand_equal_p is always false if an operand
10463 is volatile. */
10464
19da70e0 10465 if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
fef10b60 10466 && operand_equal_p (arg0, arg1, 0))
10467 return fold_convert (type, integer_zero_node);
10468
10469 /* A - B -> A + (-B) if B is easily negatable. */
32cef1cc 10470 if (negate_expr_p (arg1)
fef10b60 10471 && ((FLOAT_TYPE_P (type)
10472 /* Avoid this transformation if B is a positive REAL_CST. */
10473 && (TREE_CODE (arg1) != REAL_CST
10474 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
8a7907c1 10475 || INTEGRAL_TYPE_P (type)))
b4bd9527 10476 return fold_build2 (PLUS_EXPR, type,
10477 fold_convert (type, arg0),
10478 fold_convert (type, negate_expr (arg1)));
fef10b60 10479
10480 /* Try folding difference of addresses. */
10481 {
10482 HOST_WIDE_INT diff;
10483
10484 if ((TREE_CODE (arg0) == ADDR_EXPR
10485 || TREE_CODE (arg1) == ADDR_EXPR)
10486 && ptr_difference_const (arg0, arg1, &diff))
10487 return build_int_cst_type (type, diff);
10488 }
d4e7fe89 10489
10490 /* Fold &a[i] - &a[j] to i-j. */
10491 if (TREE_CODE (arg0) == ADDR_EXPR
10492 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10493 && TREE_CODE (arg1) == ADDR_EXPR
10494 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10495 {
10496 tree aref0 = TREE_OPERAND (arg0, 0);
10497 tree aref1 = TREE_OPERAND (arg1, 0);
10498 if (operand_equal_p (TREE_OPERAND (aref0, 0),
10499 TREE_OPERAND (aref1, 0), 0))
10500 {
10501 tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10502 tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10503 tree esz = array_ref_element_size (aref0);
10504 tree diff = build2 (MINUS_EXPR, type, op0, op1);
10505 return fold_build2 (MULT_EXPR, type, diff,
10506 fold_convert (type, esz));
10507
10508 }
10509 }
10510
1e5de3bd 10511 if (FLOAT_TYPE_P (type)
10512 && flag_unsafe_math_optimizations
429f2f90 10513 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10514 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10515 && (tem = distribute_real_division (code, type, arg0, arg1)))
10516 return tem;
10517
1c9af531 10518 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
49d060d7 10519 same or one. Make sure type is not saturating.
10520 fold_plusminus_mult_expr will re-associate. */
1c9af531 10521 if ((TREE_CODE (arg0) == MULT_EXPR
10522 || TREE_CODE (arg1) == MULT_EXPR)
06f0b99c 10523 && !TYPE_SATURATING (type)
49d060d7 10524 && (!FLOAT_TYPE_P (type) || flag_associative_math))
1c9af531 10525 {
10526 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10527 if (tem)
10528 return tem;
fef10b60 10529 }
10530
10531 goto associate;
10532
10533 case MULT_EXPR:
10534 /* (-A) * (-B) -> A * B */
10535 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
7ab7fd4f 10536 return fold_build2 (MULT_EXPR, type,
47652819 10537 fold_convert (type, TREE_OPERAND (arg0, 0)),
10538 fold_convert (type, negate_expr (arg1)));
fef10b60 10539 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
7ab7fd4f 10540 return fold_build2 (MULT_EXPR, type,
47652819 10541 fold_convert (type, negate_expr (arg0)),
10542 fold_convert (type, TREE_OPERAND (arg1, 0)));
fef10b60 10543
fef10b60 10544 if (! FLOAT_TYPE_P (type))
10545 {
10546 if (integer_zerop (arg1))
10547 return omit_one_operand (type, arg1, arg0);
10548 if (integer_onep (arg1))
10549 return non_lvalue (fold_convert (type, arg0));
a69a910a 10550 /* Transform x * -1 into -x. Make sure to do the negation
10551 on the original operand with conversions not stripped
10552 because we can only strip non-sign-changing conversions. */
48854785 10553 if (integer_all_onesp (arg1))
a69a910a 10554 return fold_convert (type, negate_expr (op0));
8a7907c1 10555 /* Transform x * -C into -x * C if x is easily negatable. */
10556 if (TREE_CODE (arg1) == INTEGER_CST
10557 && tree_int_cst_sgn (arg1) == -1
10558 && negate_expr_p (arg0)
10559 && (tem = negate_expr (arg1)) != arg1
10560 && !TREE_OVERFLOW (tem))
10561 return fold_build2 (MULT_EXPR, type,
984e8ecc 10562 fold_convert (type, negate_expr (arg0)), tem);
fef10b60 10563
10564 /* (a * (1 << b)) is (a << b) */
10565 if (TREE_CODE (arg1) == LSHIFT_EXPR
10566 && integer_onep (TREE_OPERAND (arg1, 0)))
984e8ecc 10567 return fold_build2 (LSHIFT_EXPR, type, op0,
7ab7fd4f 10568 TREE_OPERAND (arg1, 1));
fef10b60 10569 if (TREE_CODE (arg0) == LSHIFT_EXPR
10570 && integer_onep (TREE_OPERAND (arg0, 0)))
984e8ecc 10571 return fold_build2 (LSHIFT_EXPR, type, op1,
7ab7fd4f 10572 TREE_OPERAND (arg0, 1));
fef10b60 10573
f4cd9b29 10574 /* (A + A) * C -> A * 2 * C */
10575 if (TREE_CODE (arg0) == PLUS_EXPR
10576 && TREE_CODE (arg1) == INTEGER_CST
10577 && operand_equal_p (TREE_OPERAND (arg0, 0),
10578 TREE_OPERAND (arg0, 1), 0))
10579 return fold_build2 (MULT_EXPR, type,
10580 omit_one_operand (type, TREE_OPERAND (arg0, 0),
10581 TREE_OPERAND (arg0, 1)),
10582 fold_build2 (MULT_EXPR, type,
10583 build_int_cst (type, 2) , arg1));
10584
add6ee5e 10585 strict_overflow_p = false;
fef10b60 10586 if (TREE_CODE (arg1) == INTEGER_CST
28fa8094 10587 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
add6ee5e 10588 &strict_overflow_p)))
10589 {
10590 if (strict_overflow_p)
10591 fold_overflow_warning (("assuming signed overflow does not "
10592 "occur when simplifying "
10593 "multiplication"),
10594 WARN_STRICT_OVERFLOW_MISC);
10595 return fold_convert (type, tem);
10596 }
fef10b60 10597
5f4f3617 10598 /* Optimize z * conj(z) for integer complex numbers. */
10599 if (TREE_CODE (arg0) == CONJ_EXPR
10600 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10601 return fold_mult_zconjz (type, arg1);
10602 if (TREE_CODE (arg1) == CONJ_EXPR
10603 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10604 return fold_mult_zconjz (type, arg0);
fef10b60 10605 }
10606 else
10607 {
10608 /* Maybe fold x * 0 to 0. The expressions aren't the same
10609 when x is NaN, since x * 0 is also NaN. Nor are they the
10610 same in modes with signed zeros, since multiplying a
10611 negative value by 0 gives -0, not +0. */
10612 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10613 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10614 && real_zerop (arg1))
10615 return omit_one_operand (type, arg1, arg0);
fc86f9df 10616 /* In IEEE floating point, x*1 is not equivalent to x for snans.
10617 Likewise for complex arithmetic with signed zeros. */
fef10b60 10618 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
fc86f9df 10619 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10620 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
fef10b60 10621 && real_onep (arg1))
10622 return non_lvalue (fold_convert (type, arg0));
10623
10624 /* Transform x * -1.0 into -x. */
10625 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
fc86f9df 10626 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10627 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
fef10b60 10628 && real_minus_onep (arg1))
10629 return fold_convert (type, negate_expr (arg0));
10630
49d060d7 10631 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
10632 the result for floating point types due to rounding so it is applied
10633 only if -fassociative-math was specify. */
10634 if (flag_associative_math
fef10b60 10635 && TREE_CODE (arg0) == RDIV_EXPR
10636 && TREE_CODE (arg1) == REAL_CST
10637 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10638 {
10639 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10640 arg1, 0);
10641 if (tem)
7ab7fd4f 10642 return fold_build2 (RDIV_EXPR, type, tem,
10643 TREE_OPERAND (arg0, 1));
fef10b60 10644 }
10645
10646 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10647 if (operand_equal_p (arg0, arg1, 0))
10648 {
10649 tree tem = fold_strip_sign_ops (arg0);
10650 if (tem != NULL_TREE)
10651 {
10652 tem = fold_convert (type, tem);
7ab7fd4f 10653 return fold_build2 (MULT_EXPR, type, tem, tem);
fef10b60 10654 }
10655 }
10656
ed97ac4e 10657 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
1af0d139 10658 This is not the same for NaNs or if signed zeros are
ed97ac4e 10659 involved. */
10660 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10661 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10662 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10663 && TREE_CODE (arg1) == COMPLEX_CST
10664 && real_zerop (TREE_REALPART (arg1)))
10665 {
10666 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10667 if (real_onep (TREE_IMAGPART (arg1)))
10668 return fold_build2 (COMPLEX_EXPR, type,
10669 negate_expr (fold_build1 (IMAGPART_EXPR,
10670 rtype, arg0)),
10671 fold_build1 (REALPART_EXPR, rtype, arg0));
10672 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10673 return fold_build2 (COMPLEX_EXPR, type,
10674 fold_build1 (IMAGPART_EXPR, rtype, arg0),
10675 negate_expr (fold_build1 (REALPART_EXPR,
10676 rtype, arg0)));
10677 }
10678
5f4f3617 10679 /* Optimize z * conj(z) for floating point complex numbers.
10680 Guarded by flag_unsafe_math_optimizations as non-finite
10681 imaginary components don't produce scalar results. */
10682 if (flag_unsafe_math_optimizations
10683 && TREE_CODE (arg0) == CONJ_EXPR
10684 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10685 return fold_mult_zconjz (type, arg1);
10686 if (flag_unsafe_math_optimizations
10687 && TREE_CODE (arg1) == CONJ_EXPR
10688 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10689 return fold_mult_zconjz (type, arg0);
10690
fef10b60 10691 if (flag_unsafe_math_optimizations)
10692 {
10693 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10694 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10695
10696 /* Optimizations of root(...)*root(...). */
10697 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10698 {
c2f47e15 10699 tree rootfn, arg;
10700 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10701 tree arg10 = CALL_EXPR_ARG (arg1, 0);
fef10b60 10702
10703 /* Optimize sqrt(x)*sqrt(x) as x. */
10704 if (BUILTIN_SQRT_P (fcode0)
10705 && operand_equal_p (arg00, arg10, 0)
10706 && ! HONOR_SNANS (TYPE_MODE (type)))
10707 return arg00;
10708
10709 /* Optimize root(x)*root(y) as root(x*y). */
c2f47e15 10710 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
7ab7fd4f 10711 arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
c2f47e15 10712 return build_call_expr (rootfn, 1, arg);
fef10b60 10713 }
10714
10715 /* Optimize expN(x)*expN(y) as expN(x+y). */
10716 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10717 {
c2f47e15 10718 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
b085d4e5 10719 tree arg = fold_build2 (PLUS_EXPR, type,
c2f47e15 10720 CALL_EXPR_ARG (arg0, 0),
10721 CALL_EXPR_ARG (arg1, 0));
10722 return build_call_expr (expfn, 1, arg);
fef10b60 10723 }
10724
10725 /* Optimizations of pow(...)*pow(...). */
10726 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10727 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10728 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10729 {
c2f47e15 10730 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10731 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10732 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10733 tree arg11 = CALL_EXPR_ARG (arg1, 1);
fef10b60 10734
10735 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10736 if (operand_equal_p (arg01, arg11, 0))
10737 {
c2f47e15 10738 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
b085d4e5 10739 tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
c2f47e15 10740 return build_call_expr (powfn, 2, arg, arg01);
fef10b60 10741 }
10742
10743 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10744 if (operand_equal_p (arg00, arg10, 0))
10745 {
c2f47e15 10746 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
7ab7fd4f 10747 tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
c2f47e15 10748 return build_call_expr (powfn, 2, arg00, arg);
fef10b60 10749 }
10750 }
10751
10752 /* Optimize tan(x)*cos(x) as sin(x). */
10753 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10754 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10755 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10756 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10757 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10758 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
c2f47e15 10759 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10760 CALL_EXPR_ARG (arg1, 0), 0))
fef10b60 10761 {
10762 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10763
10764 if (sinfn != NULL_TREE)
c2f47e15 10765 return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
fef10b60 10766 }
10767
10768 /* Optimize x*pow(x,c) as pow(x,c+1). */
10769 if (fcode1 == BUILT_IN_POW
10770 || fcode1 == BUILT_IN_POWF
10771 || fcode1 == BUILT_IN_POWL)
10772 {
c2f47e15 10773 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10774 tree arg11 = CALL_EXPR_ARG (arg1, 1);
fef10b60 10775 if (TREE_CODE (arg11) == REAL_CST
f96bd2bf 10776 && !TREE_OVERFLOW (arg11)
fef10b60 10777 && operand_equal_p (arg0, arg10, 0))
10778 {
c2f47e15 10779 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
fef10b60 10780 REAL_VALUE_TYPE c;
c2f47e15 10781 tree arg;
fef10b60 10782
10783 c = TREE_REAL_CST (arg11);
10784 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10785 arg = build_real (type, c);
c2f47e15 10786 return build_call_expr (powfn, 2, arg0, arg);
fef10b60 10787 }
10788 }
10789
10790 /* Optimize pow(x,c)*x as pow(x,c+1). */
10791 if (fcode0 == BUILT_IN_POW
10792 || fcode0 == BUILT_IN_POWF
10793 || fcode0 == BUILT_IN_POWL)
10794 {
c2f47e15 10795 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10796 tree arg01 = CALL_EXPR_ARG (arg0, 1);
fef10b60 10797 if (TREE_CODE (arg01) == REAL_CST
f96bd2bf 10798 && !TREE_OVERFLOW (arg01)
fef10b60 10799 && operand_equal_p (arg1, arg00, 0))
10800 {
c2f47e15 10801 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
fef10b60 10802 REAL_VALUE_TYPE c;
c2f47e15 10803 tree arg;
fef10b60 10804
10805 c = TREE_REAL_CST (arg01);
10806 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10807 arg = build_real (type, c);
c2f47e15 10808 return build_call_expr (powfn, 2, arg1, arg);
fef10b60 10809 }
10810 }
10811
10812 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
0bfd8d5c 10813 if (optimize_function_for_speed_p (cfun)
fef10b60 10814 && operand_equal_p (arg0, arg1, 0))
10815 {
10816 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10817
10818 if (powfn)
10819 {
10820 tree arg = build_real (type, dconst2);
c2f47e15 10821 return build_call_expr (powfn, 2, arg0, arg);
fef10b60 10822 }
10823 }
10824 }
10825 }
10826 goto associate;
10827
10828 case BIT_IOR_EXPR:
10829 bit_ior:
10830 if (integer_all_onesp (arg1))
10831 return omit_one_operand (type, arg1, arg0);
10832 if (integer_zerop (arg1))
10833 return non_lvalue (fold_convert (type, arg0));
10834 if (operand_equal_p (arg0, arg1, 0))
10835 return non_lvalue (fold_convert (type, arg0));
10836
10837 /* ~X | X is -1. */
10838 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10839 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10840 {
89ee75ae 10841 t1 = fold_convert (type, integer_zero_node);
10842 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
fef10b60 10843 return omit_one_operand (type, t1, arg1);
10844 }
10845
10846 /* X | ~X is -1. */
10847 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10848 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10849 {
89ee75ae 10850 t1 = fold_convert (type, integer_zero_node);
10851 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
fef10b60 10852 return omit_one_operand (type, t1, arg0);
10853 }
10854
191611dd 10855 /* Canonicalize (X & C1) | C2. */
10856 if (TREE_CODE (arg0) == BIT_AND_EXPR
10857 && TREE_CODE (arg1) == INTEGER_CST
10858 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10859 {
fd3e569d 10860 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10861 int width = TYPE_PRECISION (type), w;
191611dd 10862 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10863 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10864 hi2 = TREE_INT_CST_HIGH (arg1);
10865 lo2 = TREE_INT_CST_LOW (arg1);
10866
10867 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10868 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10869 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10870
10871 if (width > HOST_BITS_PER_WIDE_INT)
10872 {
10873 mhi = (unsigned HOST_WIDE_INT) -1
10874 >> (2 * HOST_BITS_PER_WIDE_INT - width);
10875 mlo = -1;
10876 }
10877 else
10878 {
10879 mhi = 0;
10880 mlo = (unsigned HOST_WIDE_INT) -1
10881 >> (HOST_BITS_PER_WIDE_INT - width);
10882 }
10883
10884 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10885 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10886 return fold_build2 (BIT_IOR_EXPR, type,
10887 TREE_OPERAND (arg0, 0), arg1);
10888
fd3e569d 10889 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10890 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10891 mode which allows further optimizations. */
191611dd 10892 hi1 &= mhi;
10893 lo1 &= mlo;
fd3e569d 10894 hi2 &= mhi;
10895 lo2 &= mlo;
10896 hi3 = hi1 & ~hi2;
10897 lo3 = lo1 & ~lo2;
10898 for (w = BITS_PER_UNIT;
10899 w <= width && w <= HOST_BITS_PER_WIDE_INT;
10900 w <<= 1)
10901 {
10902 unsigned HOST_WIDE_INT mask
10903 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10904 if (((lo1 | lo2) & mask) == mask
10905 && (lo1 & ~mask) == 0 && hi1 == 0)
10906 {
10907 hi3 = 0;
10908 lo3 = mask;
10909 break;
10910 }
10911 }
10912 if (hi3 != hi1 || lo3 != lo1)
191611dd 10913 return fold_build2 (BIT_IOR_EXPR, type,
10914 fold_build2 (BIT_AND_EXPR, type,
10915 TREE_OPERAND (arg0, 0),
10916 build_int_cst_wide (type,
fd3e569d 10917 lo3, hi3)),
191611dd 10918 arg1);
10919 }
10920
af55979c 10921 /* (X & Y) | Y is (X, Y). */
10922 if (TREE_CODE (arg0) == BIT_AND_EXPR
10923 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10924 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10925 /* (X & Y) | X is (Y, X). */
10926 if (TREE_CODE (arg0) == BIT_AND_EXPR
10927 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10928 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10929 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10930 /* X | (X & Y) is (Y, X). */
10931 if (TREE_CODE (arg1) == BIT_AND_EXPR
10932 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10933 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10934 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10935 /* X | (Y & X) is (Y, X). */
10936 if (TREE_CODE (arg1) == BIT_AND_EXPR
10937 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10938 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10939 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10940
fef10b60 10941 t1 = distribute_bit_expr (code, type, arg0, arg1);
10942 if (t1 != NULL_TREE)
10943 return t1;
10944
10945 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10946
10947 This results in more efficient code for machines without a NAND
10948 instruction. Combine will canonicalize to the first form
10949 which will allow use of NAND instructions provided by the
10950 backend if they exist. */
10951 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10952 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10953 {
7ab7fd4f 10954 return fold_build1 (BIT_NOT_EXPR, type,
10955 build2 (BIT_AND_EXPR, type,
54bd17de 10956 fold_convert (type,
10957 TREE_OPERAND (arg0, 0)),
10958 fold_convert (type,
10959 TREE_OPERAND (arg1, 0))));
fef10b60 10960 }
10961
10962 /* See if this can be simplified into a rotate first. If that
10963 is unsuccessful continue in the association code. */
10964 goto bit_rotate;
10965
10966 case BIT_XOR_EXPR:
10967 if (integer_zerop (arg1))
10968 return non_lvalue (fold_convert (type, arg0));
10969 if (integer_all_onesp (arg1))
29988845 10970 return fold_build1 (BIT_NOT_EXPR, type, op0);
fef10b60 10971 if (operand_equal_p (arg0, arg1, 0))
10972 return omit_one_operand (type, integer_zero_node, arg0);
10973
10974 /* ~X ^ X is -1. */
10975 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10976 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10977 {
89ee75ae 10978 t1 = fold_convert (type, integer_zero_node);
10979 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
fef10b60 10980 return omit_one_operand (type, t1, arg1);
10981 }
10982
10983 /* X ^ ~X is -1. */
10984 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10985 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10986 {
89ee75ae 10987 t1 = fold_convert (type, integer_zero_node);
10988 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
fef10b60 10989 return omit_one_operand (type, t1, arg0);
10990 }
10991
10992 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10993 with a constant, and the two constants have no bits in common,
10994 we should treat this as a BIT_IOR_EXPR since this may produce more
10995 simplifications. */
10996 if (TREE_CODE (arg0) == BIT_AND_EXPR
10997 && TREE_CODE (arg1) == BIT_AND_EXPR
10998 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10999 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11000 && integer_zerop (const_binop (BIT_AND_EXPR,
11001 TREE_OPERAND (arg0, 1),
11002 TREE_OPERAND (arg1, 1), 0)))
11003 {
11004 code = BIT_IOR_EXPR;
11005 goto bit_ior;
11006 }
11007
a433d54b 11008 /* (X | Y) ^ X -> Y & ~ X*/
11009 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11010 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11011 {
11012 tree t2 = TREE_OPERAND (arg0, 1);
11013 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
11014 arg1);
11015 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11016 fold_convert (type, t1));
11017 return t1;
11018 }
11019
11020 /* (Y | X) ^ X -> Y & ~ X*/
11021 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11022 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11023 {
11024 tree t2 = TREE_OPERAND (arg0, 0);
11025 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
11026 arg1);
11027 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11028 fold_convert (type, t1));
11029 return t1;
11030 }
11031
11032 /* X ^ (X | Y) -> Y & ~ X*/
11033 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11034 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11035 {
11036 tree t2 = TREE_OPERAND (arg1, 1);
11037 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11038 arg0);
11039 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11040 fold_convert (type, t1));
11041 return t1;
11042 }
11043
11044 /* X ^ (Y | X) -> Y & ~ X*/
11045 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11046 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11047 {
11048 tree t2 = TREE_OPERAND (arg1, 0);
11049 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11050 arg0);
11051 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11052 fold_convert (type, t1));
11053 return t1;
11054 }
11055
8cadcd8f 11056 /* Convert ~X ^ ~Y to X ^ Y. */
11057 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11058 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11059 return fold_build2 (code, type,
11060 fold_convert (type, TREE_OPERAND (arg0, 0)),
11061 fold_convert (type, TREE_OPERAND (arg1, 0)));
11062
1518c00b 11063 /* Convert ~X ^ C to X ^ ~C. */
11064 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11065 && TREE_CODE (arg1) == INTEGER_CST)
11066 return fold_build2 (code, type,
11067 fold_convert (type, TREE_OPERAND (arg0, 0)),
11068 fold_build1 (BIT_NOT_EXPR, type, arg1));
11069
7ddc480e 11070 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11071 if (TREE_CODE (arg0) == BIT_AND_EXPR
11072 && integer_onep (TREE_OPERAND (arg0, 1))
11073 && integer_onep (arg1))
11074 return fold_build2 (EQ_EXPR, type, arg0,
11075 build_int_cst (TREE_TYPE (arg0), 0));
11076
a3cf48f5 11077 /* Fold (X & Y) ^ Y as ~X & Y. */
11078 if (TREE_CODE (arg0) == BIT_AND_EXPR
11079 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11080 {
11081 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11082 return fold_build2 (BIT_AND_EXPR, type,
11083 fold_build1 (BIT_NOT_EXPR, type, tem),
11084 fold_convert (type, arg1));
11085 }
11086 /* Fold (X & Y) ^ X as ~Y & X. */
11087 if (TREE_CODE (arg0) == BIT_AND_EXPR
11088 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11089 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11090 {
11091 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11092 return fold_build2 (BIT_AND_EXPR, type,
11093 fold_build1 (BIT_NOT_EXPR, type, tem),
11094 fold_convert (type, arg1));
11095 }
11096 /* Fold X ^ (X & Y) as X & ~Y. */
11097 if (TREE_CODE (arg1) == BIT_AND_EXPR
11098 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11099 {
11100 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11101 return fold_build2 (BIT_AND_EXPR, type,
11102 fold_convert (type, arg0),
11103 fold_build1 (BIT_NOT_EXPR, type, tem));
11104 }
11105 /* Fold X ^ (Y & X) as ~Y & X. */
11106 if (TREE_CODE (arg1) == BIT_AND_EXPR
11107 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11108 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11109 {
11110 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11111 return fold_build2 (BIT_AND_EXPR, type,
11112 fold_build1 (BIT_NOT_EXPR, type, tem),
11113 fold_convert (type, arg0));
11114 }
11115
fef10b60 11116 /* See if this can be simplified into a rotate first. If that
11117 is unsuccessful continue in the association code. */
11118 goto bit_rotate;
11119
11120 case BIT_AND_EXPR:
11121 if (integer_all_onesp (arg1))
11122 return non_lvalue (fold_convert (type, arg0));
11123 if (integer_zerop (arg1))
11124 return omit_one_operand (type, arg1, arg0);
11125 if (operand_equal_p (arg0, arg1, 0))
11126 return non_lvalue (fold_convert (type, arg0));
11127
11128 /* ~X & X is always zero. */
11129 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11130 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11131 return omit_one_operand (type, integer_zero_node, arg1);
11132
11133 /* X & ~X is always zero. */
11134 if (TREE_CODE (arg1) == BIT_NOT_EXPR
11135 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11136 return omit_one_operand (type, integer_zero_node, arg0);
11137
191611dd 11138 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
11139 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11140 && TREE_CODE (arg1) == INTEGER_CST
11141 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
05e1595b 11142 {
2803a214 11143 tree tmp1 = fold_convert (type, arg1);
11144 tree tmp2 = fold_convert (type, TREE_OPERAND (arg0, 0));
11145 tree tmp3 = fold_convert (type, TREE_OPERAND (arg0, 1));
11146 tmp2 = fold_build2 (BIT_AND_EXPR, type, tmp2, tmp1);
11147 tmp3 = fold_build2 (BIT_AND_EXPR, type, tmp3, tmp1);
05e1595b 11148 return fold_convert (type,
2803a214 11149 fold_build2 (BIT_IOR_EXPR, type, tmp2, tmp3));
05e1595b 11150 }
191611dd 11151
af55979c 11152 /* (X | Y) & Y is (X, Y). */
11153 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11154 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11155 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
11156 /* (X | Y) & X is (Y, X). */
11157 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11158 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11159 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11160 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
11161 /* X & (X | Y) is (Y, X). */
11162 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11163 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11164 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11165 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
11166 /* X & (Y | X) is (Y, X). */
11167 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11168 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11169 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11170 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
11171
7ddc480e 11172 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11173 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11174 && integer_onep (TREE_OPERAND (arg0, 1))
11175 && integer_onep (arg1))
11176 {
11177 tem = TREE_OPERAND (arg0, 0);
11178 return fold_build2 (EQ_EXPR, type,
11179 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11180 build_int_cst (TREE_TYPE (tem), 1)),
11181 build_int_cst (TREE_TYPE (tem), 0));
11182 }
11183 /* Fold ~X & 1 as (X & 1) == 0. */
11184 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11185 && integer_onep (arg1))
11186 {
11187 tem = TREE_OPERAND (arg0, 0);
11188 return fold_build2 (EQ_EXPR, type,
11189 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11190 build_int_cst (TREE_TYPE (tem), 1)),
11191 build_int_cst (TREE_TYPE (tem), 0));
11192 }
11193
a3cf48f5 11194 /* Fold (X ^ Y) & Y as ~X & Y. */
11195 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11196 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11197 {
11198 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11199 return fold_build2 (BIT_AND_EXPR, type,
11200 fold_build1 (BIT_NOT_EXPR, type, tem),
11201 fold_convert (type, arg1));
11202 }
11203 /* Fold (X ^ Y) & X as ~Y & X. */
11204 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11205 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11206 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11207 {
11208 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11209 return fold_build2 (BIT_AND_EXPR, type,
11210 fold_build1 (BIT_NOT_EXPR, type, tem),
11211 fold_convert (type, arg1));
11212 }
11213 /* Fold X & (X ^ Y) as X & ~Y. */
11214 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11215 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11216 {
11217 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11218 return fold_build2 (BIT_AND_EXPR, type,
11219 fold_convert (type, arg0),
11220 fold_build1 (BIT_NOT_EXPR, type, tem));
11221 }
11222 /* Fold X & (Y ^ X) as ~Y & X. */
11223 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11224 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11225 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11226 {
11227 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11228 return fold_build2 (BIT_AND_EXPR, type,
11229 fold_build1 (BIT_NOT_EXPR, type, tem),
11230 fold_convert (type, arg0));
11231 }
11232
fef10b60 11233 t1 = distribute_bit_expr (code, type, arg0, arg1);
11234 if (t1 != NULL_TREE)
11235 return t1;
11236 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11237 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11238 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11239 {
11240 unsigned int prec
11241 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11242
11243 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11244 && (~TREE_INT_CST_LOW (arg1)
11245 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11246 return fold_convert (type, TREE_OPERAND (arg0, 0));
11247 }
11248
11249 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11250
11251 This results in more efficient code for machines without a NOR
11252 instruction. Combine will canonicalize to the first form
11253 which will allow use of NOR instructions provided by the
11254 backend if they exist. */
11255 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11256 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11257 {
7ab7fd4f 11258 return fold_build1 (BIT_NOT_EXPR, type,
11259 build2 (BIT_IOR_EXPR, type,
984e8ecc 11260 fold_convert (type,
11261 TREE_OPERAND (arg0, 0)),
11262 fold_convert (type,
11263 TREE_OPERAND (arg1, 0))));
fef10b60 11264 }
11265
4486d2b7 11266 /* If arg0 is derived from the address of an object or function, we may
11267 be able to fold this expression using the object or function's
11268 alignment. */
11269 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11270 {
11271 unsigned HOST_WIDE_INT modulus, residue;
11272 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11273
1c4607fd 11274 modulus = get_pointer_modulus_and_residue (arg0, &residue,
11275 integer_onep (arg1));
4486d2b7 11276
11277 /* This works because modulus is a power of 2. If this weren't the
11278 case, we'd have to replace it by its greatest power-of-2
11279 divisor: modulus & -modulus. */
11280 if (low < modulus)
11281 return build_int_cst (type, residue & low);
11282 }
11283
2cf28ced 11284 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11285 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11286 if the new mask might be further optimized. */
11287 if ((TREE_CODE (arg0) == LSHIFT_EXPR
11288 || TREE_CODE (arg0) == RSHIFT_EXPR)
11289 && host_integerp (TREE_OPERAND (arg0, 1), 1)
11290 && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11291 && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11292 < TYPE_PRECISION (TREE_TYPE (arg0))
11293 && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11294 && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11295 {
11296 unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11297 unsigned HOST_WIDE_INT mask
11298 = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11299 unsigned HOST_WIDE_INT newmask, zerobits = 0;
11300 tree shift_type = TREE_TYPE (arg0);
11301
11302 if (TREE_CODE (arg0) == LSHIFT_EXPR)
11303 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11304 else if (TREE_CODE (arg0) == RSHIFT_EXPR
11305 && TYPE_PRECISION (TREE_TYPE (arg0))
11306 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11307 {
11308 unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11309 tree arg00 = TREE_OPERAND (arg0, 0);
11310 /* See if more bits can be proven as zero because of
11311 zero extension. */
11312 if (TREE_CODE (arg00) == NOP_EXPR
11313 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11314 {
11315 tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11316 if (TYPE_PRECISION (inner_type)
11317 == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11318 && TYPE_PRECISION (inner_type) < prec)
11319 {
11320 prec = TYPE_PRECISION (inner_type);
11321 /* See if we can shorten the right shift. */
11322 if (shiftc < prec)
11323 shift_type = inner_type;
11324 }
11325 }
11326 zerobits = ~(unsigned HOST_WIDE_INT) 0;
11327 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11328 zerobits <<= prec - shiftc;
11329 /* For arithmetic shift if sign bit could be set, zerobits
11330 can contain actually sign bits, so no transformation is
11331 possible, unless MASK masks them all away. In that
11332 case the shift needs to be converted into logical shift. */
11333 if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11334 && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11335 {
11336 if ((mask & zerobits) == 0)
11337 shift_type = unsigned_type_for (TREE_TYPE (arg0));
11338 else
11339 zerobits = 0;
11340 }
11341 }
11342
11343 /* ((X << 16) & 0xff00) is (X, 0). */
11344 if ((mask & zerobits) == mask)
11345 return omit_one_operand (type, build_int_cst (type, 0), arg0);
11346
11347 newmask = mask | zerobits;
11348 if (newmask != mask && (newmask & (newmask + 1)) == 0)
11349 {
11350 unsigned int prec;
11351
11352 /* Only do the transformation if NEWMASK is some integer
11353 mode's mask. */
11354 for (prec = BITS_PER_UNIT;
11355 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11356 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11357 break;
11358 if (prec < HOST_BITS_PER_WIDE_INT
11359 || newmask == ~(unsigned HOST_WIDE_INT) 0)
11360 {
11361 if (shift_type != TREE_TYPE (arg0))
11362 {
11363 tem = fold_build2 (TREE_CODE (arg0), shift_type,
11364 fold_convert (shift_type,
11365 TREE_OPERAND (arg0, 0)),
11366 TREE_OPERAND (arg0, 1));
11367 tem = fold_convert (type, tem);
11368 }
11369 else
11370 tem = op0;
11371 return fold_build2 (BIT_AND_EXPR, type, tem,
11372 build_int_cst_type (TREE_TYPE (op1),
11373 newmask));
11374 }
11375 }
11376 }
11377
fef10b60 11378 goto associate;
11379
11380 case RDIV_EXPR:
11381 /* Don't touch a floating-point divide by zero unless the mode
11382 of the constant can represent infinity. */
11383 if (TREE_CODE (arg1) == REAL_CST
11384 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11385 && real_zerop (arg1))
e7edfbbd 11386 return NULL_TREE;
fef10b60 11387
73444c65 11388 /* Optimize A / A to 1.0 if we don't care about
c2c96591 11389 NaNs or Infinities. Skip the transformation
11390 for non-real operands. */
11391 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11392 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
73444c65 11393 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11394 && operand_equal_p (arg0, arg1, 0))
11395 {
11396 tree r = build_real (TREE_TYPE (arg0), dconst1);
11397
11398 return omit_two_operands (type, r, arg0, arg1);
11399 }
11400
c2c96591 11401 /* The complex version of the above A / A optimization. */
11402 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11403 && operand_equal_p (arg0, arg1, 0))
11404 {
11405 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11406 if (! HONOR_NANS (TYPE_MODE (elem_type))
11407 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11408 {
11409 tree r = build_real (elem_type, dconst1);
11410 /* omit_two_operands will call fold_convert for us. */
11411 return omit_two_operands (type, r, arg0, arg1);
11412 }
11413 }
11414
fef10b60 11415 /* (-A) / (-B) -> A / B */
11416 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
7ab7fd4f 11417 return fold_build2 (RDIV_EXPR, type,
11418 TREE_OPERAND (arg0, 0),
11419 negate_expr (arg1));
fef10b60 11420 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
7ab7fd4f 11421 return fold_build2 (RDIV_EXPR, type,
11422 negate_expr (arg0),
11423 TREE_OPERAND (arg1, 0));
fef10b60 11424
11425 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
11426 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11427 && real_onep (arg1))
11428 return non_lvalue (fold_convert (type, arg0));
11429
11430 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
11431 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11432 && real_minus_onep (arg1))
11433 return non_lvalue (fold_convert (type, negate_expr (arg0)));
11434
11435 /* If ARG1 is a constant, we can convert this to a multiply by the
11436 reciprocal. This does not have the same rounding properties,
49d060d7 11437 so only do this if -freciprocal-math. We can actually
fef10b60 11438 always safely do it if ARG1 is a power of two, but it's hard to
11439 tell if it is or not in a portable manner. */
11440 if (TREE_CODE (arg1) == REAL_CST)
11441 {
49d060d7 11442 if (flag_reciprocal_math
fef10b60 11443 && 0 != (tem = const_binop (code, build_real (type, dconst1),
11444 arg1, 0)))
7ab7fd4f 11445 return fold_build2 (MULT_EXPR, type, arg0, tem);
fef10b60 11446 /* Find the reciprocal if optimizing and the result is exact. */
11447 if (optimize)
11448 {
11449 REAL_VALUE_TYPE r;
11450 r = TREE_REAL_CST (arg1);
11451 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11452 {
11453 tem = build_real (type, r);
b4bd9527 11454 return fold_build2 (MULT_EXPR, type,
11455 fold_convert (type, arg0), tem);
fef10b60 11456 }
11457 }
11458 }
49d060d7 11459 /* Convert A/B/C to A/(B*C). */
11460 if (flag_reciprocal_math
fef10b60 11461 && TREE_CODE (arg0) == RDIV_EXPR)
7ab7fd4f 11462 return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11463 fold_build2 (MULT_EXPR, type,
11464 TREE_OPERAND (arg0, 1), arg1));
fef10b60 11465
11466 /* Convert A/(B/C) to (A/B)*C. */
49d060d7 11467 if (flag_reciprocal_math
fef10b60 11468 && TREE_CODE (arg1) == RDIV_EXPR)
7ab7fd4f 11469 return fold_build2 (MULT_EXPR, type,
11470 fold_build2 (RDIV_EXPR, type, arg0,
11471 TREE_OPERAND (arg1, 0)),
11472 TREE_OPERAND (arg1, 1));
fef10b60 11473
11474 /* Convert C1/(X*C2) into (C1/C2)/X. */
49d060d7 11475 if (flag_reciprocal_math
fef10b60 11476 && TREE_CODE (arg1) == MULT_EXPR
11477 && TREE_CODE (arg0) == REAL_CST
11478 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11479 {
11480 tree tem = const_binop (RDIV_EXPR, arg0,
11481 TREE_OPERAND (arg1, 1), 0);
11482 if (tem)
7ab7fd4f 11483 return fold_build2 (RDIV_EXPR, type, tem,
11484 TREE_OPERAND (arg1, 0));
fef10b60 11485 }
11486
fef10b60 11487 if (flag_unsafe_math_optimizations)
11488 {
11489 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11490 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11491
11492 /* Optimize sin(x)/cos(x) as tan(x). */
11493 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11494 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11495 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
c2f47e15 11496 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11497 CALL_EXPR_ARG (arg1, 0), 0))
fef10b60 11498 {
11499 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11500
11501 if (tanfn != NULL_TREE)
c2f47e15 11502 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
fef10b60 11503 }
11504
11505 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11506 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11507 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11508 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
c2f47e15 11509 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11510 CALL_EXPR_ARG (arg1, 0), 0))
fef10b60 11511 {
11512 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11513
11514 if (tanfn != NULL_TREE)
11515 {
c2f47e15 11516 tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
7ab7fd4f 11517 return fold_build2 (RDIV_EXPR, type,
11518 build_real (type, dconst1), tmp);
fef10b60 11519 }
11520 }
11521
29f9683a 11522 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11523 NaNs or Infinities. */
11524 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11525 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11526 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11527 {
c2f47e15 11528 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11529 tree arg01 = CALL_EXPR_ARG (arg1, 0);
29f9683a 11530
11531 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11532 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11533 && operand_equal_p (arg00, arg01, 0))
11534 {
11535 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11536
11537 if (cosfn != NULL_TREE)
c2f47e15 11538 return build_call_expr (cosfn, 1, arg00);
29f9683a 11539 }
11540 }
11541
11542 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
191ec5a2 11543 NaNs or Infinities. */
29f9683a 11544 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11545 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11546 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11547 {
c2f47e15 11548 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11549 tree arg01 = CALL_EXPR_ARG (arg1, 0);
29f9683a 11550
11551 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11552 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11553 && operand_equal_p (arg00, arg01, 0))
11554 {
11555 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11556
11557 if (cosfn != NULL_TREE)
11558 {
c2f47e15 11559 tree tmp = build_call_expr (cosfn, 1, arg00);
f9c71cdf 11560 return fold_build2 (RDIV_EXPR, type,
29f9683a 11561 build_real (type, dconst1),
f9c71cdf 11562 tmp);
29f9683a 11563 }
11564 }
11565 }
11566
fef10b60 11567 /* Optimize pow(x,c)/x as pow(x,c-1). */
11568 if (fcode0 == BUILT_IN_POW
11569 || fcode0 == BUILT_IN_POWF
11570 || fcode0 == BUILT_IN_POWL)
11571 {
c2f47e15 11572 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11573 tree arg01 = CALL_EXPR_ARG (arg0, 1);
fef10b60 11574 if (TREE_CODE (arg01) == REAL_CST
f96bd2bf 11575 && !TREE_OVERFLOW (arg01)
fef10b60 11576 && operand_equal_p (arg1, arg00, 0))
11577 {
c2f47e15 11578 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
fef10b60 11579 REAL_VALUE_TYPE c;
c2f47e15 11580 tree arg;
fef10b60 11581
11582 c = TREE_REAL_CST (arg01);
11583 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11584 arg = build_real (type, c);
c2f47e15 11585 return build_call_expr (powfn, 2, arg1, arg);
fef10b60 11586 }
11587 }
29f9683a 11588
a307ebdf 11589 /* Optimize a/root(b/c) into a*root(c/b). */
11590 if (BUILTIN_ROOT_P (fcode1))
93ad369f 11591 {
11592 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11593
11594 if (TREE_CODE (rootarg) == RDIV_EXPR)
11595 {
11596 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11597 tree b = TREE_OPERAND (rootarg, 0);
11598 tree c = TREE_OPERAND (rootarg, 1);
11599
11600 tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11601
11602 tmp = build_call_expr (rootfn, 1, tmp);
11603 return fold_build2 (MULT_EXPR, type, arg0, tmp);
11604 }
11605 }
11606
29f9683a 11607 /* Optimize x/expN(y) into x*expN(-y). */
11608 if (BUILTIN_EXPONENT_P (fcode1))
11609 {
c2f47e15 11610 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11611 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11612 arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
29f9683a 11613 return fold_build2 (MULT_EXPR, type, arg0, arg1);
11614 }
11615
11616 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11617 if (fcode1 == BUILT_IN_POW
11618 || fcode1 == BUILT_IN_POWF
11619 || fcode1 == BUILT_IN_POWL)
11620 {
c2f47e15 11621 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11622 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11623 tree arg11 = CALL_EXPR_ARG (arg1, 1);
29f9683a 11624 tree neg11 = fold_convert (type, negate_expr (arg11));
c2f47e15 11625 arg1 = build_call_expr (powfn, 2, arg10, neg11);
29f9683a 11626 return fold_build2 (MULT_EXPR, type, arg0, arg1);
11627 }
fef10b60 11628 }
32cef1cc 11629 return NULL_TREE;
fef10b60 11630
11631 case TRUNC_DIV_EXPR:
fef10b60 11632 case FLOOR_DIV_EXPR:
8af0edf2 11633 /* Simplify A / (B << N) where A and B are positive and B is
11634 a power of 2, to A >> (N + log2(B)). */
add6ee5e 11635 strict_overflow_p = false;
8af0edf2 11636 if (TREE_CODE (arg1) == LSHIFT_EXPR
add6ee5e 11637 && (TYPE_UNSIGNED (type)
3778af05 11638 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
8af0edf2 11639 {
11640 tree sval = TREE_OPERAND (arg1, 0);
11641 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11642 {
11643 tree sh_cnt = TREE_OPERAND (arg1, 1);
11644 unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11645
add6ee5e 11646 if (strict_overflow_p)
11647 fold_overflow_warning (("assuming signed overflow does not "
11648 "occur when simplifying A / (B << N)"),
11649 WARN_STRICT_OVERFLOW_MISC);
11650
8af0edf2 11651 sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11652 sh_cnt, build_int_cst (NULL_TREE, pow2));
11653 return fold_build2 (RSHIFT_EXPR, type,
11654 fold_convert (type, arg0), sh_cnt);
11655 }
11656 }
d997554f 11657
11658 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11659 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
11660 if (INTEGRAL_TYPE_P (type)
11661 && TYPE_UNSIGNED (type)
11662 && code == FLOOR_DIV_EXPR)
11663 return fold_build2 (TRUNC_DIV_EXPR, type, op0, op1);
11664
8af0edf2 11665 /* Fall thru */
11666
11667 case ROUND_DIV_EXPR:
fef10b60 11668 case CEIL_DIV_EXPR:
11669 case EXACT_DIV_EXPR:
11670 if (integer_onep (arg1))
11671 return non_lvalue (fold_convert (type, arg0));
11672 if (integer_zerop (arg1))
e7edfbbd 11673 return NULL_TREE;
fef10b60 11674 /* X / -1 is -X. */
11675 if (!TYPE_UNSIGNED (type)
11676 && TREE_CODE (arg1) == INTEGER_CST
11677 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11678 && TREE_INT_CST_HIGH (arg1) == -1)
11679 return fold_convert (type, negate_expr (arg0));
11680
4df6b6e5 11681 /* Convert -A / -B to A / B when the type is signed and overflow is
11682 undefined. */
981eb798 11683 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
4df6b6e5 11684 && TREE_CODE (arg0) == NEGATE_EXPR
11685 && negate_expr_p (arg1))
add6ee5e 11686 {
11687 if (INTEGRAL_TYPE_P (type))
11688 fold_overflow_warning (("assuming signed overflow does not occur "
11689 "when distributing negation across "
11690 "division"),
11691 WARN_STRICT_OVERFLOW_MISC);
29988845 11692 return fold_build2 (code, type,
11693 fold_convert (type, TREE_OPERAND (arg0, 0)),
ac828e93 11694 fold_convert (type, negate_expr (arg1)));
add6ee5e 11695 }
981eb798 11696 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
4df6b6e5 11697 && TREE_CODE (arg1) == NEGATE_EXPR
11698 && negate_expr_p (arg0))
add6ee5e 11699 {
11700 if (INTEGRAL_TYPE_P (type))
11701 fold_overflow_warning (("assuming signed overflow does not occur "
11702 "when distributing negation across "
11703 "division"),
11704 WARN_STRICT_OVERFLOW_MISC);
ac828e93 11705 return fold_build2 (code, type,
11706 fold_convert (type, negate_expr (arg0)),
11707 fold_convert (type, TREE_OPERAND (arg1, 0)));
add6ee5e 11708 }
4df6b6e5 11709
fef10b60 11710 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11711 operation, EXACT_DIV_EXPR.
11712
11713 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11714 At one time others generated faster code, it's not clear if they do
11715 after the last round to changes to the DIV code in expmed.c. */
11716 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11717 && multiple_of_p (type, arg0, arg1))
7ab7fd4f 11718 return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
fef10b60 11719
add6ee5e 11720 strict_overflow_p = false;
fef10b60 11721 if (TREE_CODE (arg1) == INTEGER_CST
add6ee5e 11722 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11723 &strict_overflow_p)))
11724 {
11725 if (strict_overflow_p)
11726 fold_overflow_warning (("assuming signed overflow does not occur "
11727 "when simplifying division"),
11728 WARN_STRICT_OVERFLOW_MISC);
11729 return fold_convert (type, tem);
11730 }
fef10b60 11731
32cef1cc 11732 return NULL_TREE;
fef10b60 11733
11734 case CEIL_MOD_EXPR:
11735 case FLOOR_MOD_EXPR:
11736 case ROUND_MOD_EXPR:
11737 case TRUNC_MOD_EXPR:
11738 /* X % 1 is always zero, but be sure to preserve any side
11739 effects in X. */
11740 if (integer_onep (arg1))
11741 return omit_one_operand (type, integer_zero_node, arg0);
11742
11743 /* X % 0, return X % 0 unchanged so that we can get the
11744 proper warnings and errors. */
11745 if (integer_zerop (arg1))
e7edfbbd 11746 return NULL_TREE;
fef10b60 11747
11748 /* 0 % X is always zero, but be sure to preserve any side
11749 effects in X. Place this after checking for X == 0. */
11750 if (integer_zerop (arg0))
11751 return omit_one_operand (type, integer_zero_node, arg1);
11752
11753 /* X % -1 is zero. */
11754 if (!TYPE_UNSIGNED (type)
11755 && TREE_CODE (arg1) == INTEGER_CST
11756 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11757 && TREE_INT_CST_HIGH (arg1) == -1)
11758 return omit_one_operand (type, integer_zero_node, arg0);
11759
c44b9c58 11760 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
8af0edf2 11761 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
add6ee5e 11762 strict_overflow_p = false;
c44b9c58 11763 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
add6ee5e 11764 && (TYPE_UNSIGNED (type)
3778af05 11765 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
fef10b60 11766 {
8af0edf2 11767 tree c = arg1;
11768 /* Also optimize A % (C << N) where C is a power of 2,
11769 to A & ((C << N) - 1). */
11770 if (TREE_CODE (arg1) == LSHIFT_EXPR)
11771 c = TREE_OPERAND (arg1, 0);
fef10b60 11772
8af0edf2 11773 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
fef10b60 11774 {
2455d3ef 11775 tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11776 build_int_cst (TREE_TYPE (arg1), 1));
add6ee5e 11777 if (strict_overflow_p)
11778 fold_overflow_warning (("assuming signed overflow does not "
11779 "occur when simplifying "
11780 "X % (power of two)"),
11781 WARN_STRICT_OVERFLOW_MISC);
8af0edf2 11782 return fold_build2 (BIT_AND_EXPR, type,
11783 fold_convert (type, arg0),
11784 fold_convert (type, mask));
fef10b60 11785 }
fef10b60 11786 }
11787
11788 /* X % -C is the same as X % C. */
11789 if (code == TRUNC_MOD_EXPR
11790 && !TYPE_UNSIGNED (type)
11791 && TREE_CODE (arg1) == INTEGER_CST
f96bd2bf 11792 && !TREE_OVERFLOW (arg1)
fef10b60 11793 && TREE_INT_CST_HIGH (arg1) < 0
981eb798 11794 && !TYPE_OVERFLOW_TRAPS (type)
fef10b60 11795 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
11796 && !sign_bit_p (arg1, arg1))
7ab7fd4f 11797 return fold_build2 (code, type, fold_convert (type, arg0),
11798 fold_convert (type, negate_expr (arg1)));
fef10b60 11799
11800 /* X % -Y is the same as X % Y. */
11801 if (code == TRUNC_MOD_EXPR
11802 && !TYPE_UNSIGNED (type)
11803 && TREE_CODE (arg1) == NEGATE_EXPR
981eb798 11804 && !TYPE_OVERFLOW_TRAPS (type))
7ab7fd4f 11805 return fold_build2 (code, type, fold_convert (type, arg0),
11806 fold_convert (type, TREE_OPERAND (arg1, 0)));
fef10b60 11807
11808 if (TREE_CODE (arg1) == INTEGER_CST
add6ee5e 11809 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11810 &strict_overflow_p)))
11811 {
11812 if (strict_overflow_p)
11813 fold_overflow_warning (("assuming signed overflow does not occur "
f0b5f617 11814 "when simplifying modulus"),
add6ee5e 11815 WARN_STRICT_OVERFLOW_MISC);
11816 return fold_convert (type, tem);
11817 }
fef10b60 11818
32cef1cc 11819 return NULL_TREE;
fef10b60 11820
11821 case LROTATE_EXPR:
11822 case RROTATE_EXPR:
11823 if (integer_all_onesp (arg0))
11824 return omit_one_operand (type, arg0, arg1);
11825 goto shift;
11826
11827 case RSHIFT_EXPR:
11828 /* Optimize -1 >> x for arithmetic right shifts. */
53cfcf36 11829 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11830 && tree_expr_nonnegative_p (arg1))
fef10b60 11831 return omit_one_operand (type, arg0, arg1);
11832 /* ... fall through ... */
11833
11834 case LSHIFT_EXPR:
11835 shift:
11836 if (integer_zerop (arg1))
11837 return non_lvalue (fold_convert (type, arg0));
11838 if (integer_zerop (arg0))
11839 return omit_one_operand (type, arg0, arg1);
11840
11841 /* Since negative shift count is not well-defined,
11842 don't try to compute it in the compiler. */
11843 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
e7edfbbd 11844 return NULL_TREE;
d810c37d 11845
11846 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2b2c6e11 11847 if (TREE_CODE (op0) == code && host_integerp (arg1, false)
d810c37d 11848 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11849 && host_integerp (TREE_OPERAND (arg0, 1), false)
11850 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11851 {
11852 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11853 + TREE_INT_CST_LOW (arg1));
11854
11855 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11856 being well defined. */
11857 if (low >= TYPE_PRECISION (type))
11858 {
11859 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11860 low = low % TYPE_PRECISION (type);
11861 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
4377e55f 11862 return omit_one_operand (type, build_int_cst (type, 0),
11863 TREE_OPERAND (arg0, 0));
d810c37d 11864 else
11865 low = TYPE_PRECISION (type) - 1;
11866 }
11867
11868 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11869 build_int_cst (type, low));
11870 }
11871
0e7e788d 11872 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11873 into x & ((unsigned)-1 >> c) for unsigned types. */
11874 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11875 || (TYPE_UNSIGNED (type)
11876 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
d810c37d 11877 && host_integerp (arg1, false)
11878 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11879 && host_integerp (TREE_OPERAND (arg0, 1), false)
11880 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11881 {
11882 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11883 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
d810c37d 11884 tree lshift;
11885 tree arg00;
11886
11887 if (low0 == low1)
11888 {
11889 arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11890
0e7e788d 11891 lshift = build_int_cst (type, -1);
11892 lshift = int_const_binop (code, lshift, arg1, 0);
d810c37d 11893
11894 return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11895 }
11896 }
11897
fef10b60 11898 /* Rewrite an LROTATE_EXPR by a constant into an
11899 RROTATE_EXPR by a new constant. */
11900 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11901 {
2455d3ef 11902 tree tem = build_int_cst (TREE_TYPE (arg1),
6295ca72 11903 TYPE_PRECISION (type));
fef10b60 11904 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
984e8ecc 11905 return fold_build2 (RROTATE_EXPR, type, op0, tem);
fef10b60 11906 }
11907
11908 /* If we have a rotate of a bit operation with the rotate count and
11909 the second operand of the bit operation both constant,
11910 permute the two operations. */
11911 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11912 && (TREE_CODE (arg0) == BIT_AND_EXPR
11913 || TREE_CODE (arg0) == BIT_IOR_EXPR
11914 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11915 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7ab7fd4f 11916 return fold_build2 (TREE_CODE (arg0), type,
11917 fold_build2 (code, type,
11918 TREE_OPERAND (arg0, 0), arg1),
11919 fold_build2 (code, type,
11920 TREE_OPERAND (arg0, 1), arg1));
fef10b60 11921
6295ca72 11922 /* Two consecutive rotates adding up to the precision of the
11923 type can be ignored. */
fef10b60 11924 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11925 && TREE_CODE (arg0) == RROTATE_EXPR
11926 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11927 && TREE_INT_CST_HIGH (arg1) == 0
11928 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11929 && ((TREE_INT_CST_LOW (arg1)
11930 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
6295ca72 11931 == (unsigned int) TYPE_PRECISION (type)))
fef10b60 11932 return TREE_OPERAND (arg0, 0);
11933
2cf28ced 11934 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11935 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11936 if the latter can be further optimized. */
11937 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11938 && TREE_CODE (arg0) == BIT_AND_EXPR
11939 && TREE_CODE (arg1) == INTEGER_CST
11940 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11941 {
11942 tree mask = fold_build2 (code, type,
11943 fold_convert (type, TREE_OPERAND (arg0, 1)),
11944 arg1);
11945 tree shift = fold_build2 (code, type,
11946 fold_convert (type, TREE_OPERAND (arg0, 0)),
11947 arg1);
11948 tem = fold_binary (BIT_AND_EXPR, type, shift, mask);
11949 if (tem)
11950 return tem;
11951 }
11952
32cef1cc 11953 return NULL_TREE;
fef10b60 11954
11955 case MIN_EXPR:
11956 if (operand_equal_p (arg0, arg1, 0))
11957 return omit_one_operand (type, arg0, arg1);
11958 if (INTEGRAL_TYPE_P (type)
11959 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11960 return omit_one_operand (type, arg1, arg0);
7e50ecae 11961 tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11962 if (tem)
11963 return tem;
fef10b60 11964 goto associate;
11965
11966 case MAX_EXPR:
11967 if (operand_equal_p (arg0, arg1, 0))
11968 return omit_one_operand (type, arg0, arg1);
11969 if (INTEGRAL_TYPE_P (type)
11970 && TYPE_MAX_VALUE (type)
11971 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11972 return omit_one_operand (type, arg1, arg0);
7e50ecae 11973 tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11974 if (tem)
11975 return tem;
fef10b60 11976 goto associate;
11977
11978 case TRUTH_ANDIF_EXPR:
11979 /* Note that the operands of this must be ints
11980 and their values must be 0 or 1.
11981 ("true" is a fixed value perhaps depending on the language.) */
11982 /* If first arg is constant zero, return it. */
11983 if (integer_zerop (arg0))
11984 return fold_convert (type, arg0);
11985 case TRUTH_AND_EXPR:
11986 /* If either arg is constant true, drop it. */
11987 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11988 return non_lvalue (fold_convert (type, arg1));
11989 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11990 /* Preserve sequence points. */
11991 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11992 return non_lvalue (fold_convert (type, arg0));
11993 /* If second arg is constant zero, result is zero, but first arg
11994 must be evaluated. */
11995 if (integer_zerop (arg1))
11996 return omit_one_operand (type, arg1, arg0);
11997 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11998 case will be handled here. */
11999 if (integer_zerop (arg0))
12000 return omit_one_operand (type, arg0, arg1);
12001
12002 /* !X && X is always false. */
12003 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12004 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12005 return omit_one_operand (type, integer_zero_node, arg1);
12006 /* X && !X is always false. */
12007 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12008 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12009 return omit_one_operand (type, integer_zero_node, arg0);
12010
12011 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12012 means A >= Y && A != MAX, but in this case we know that
12013 A < X <= MAX. */
12014
12015 if (!TREE_SIDE_EFFECTS (arg0)
12016 && !TREE_SIDE_EFFECTS (arg1))
12017 {
12018 tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
c2352383 12019 if (tem && !operand_equal_p (tem, arg0, 0))
7ab7fd4f 12020 return fold_build2 (code, type, tem, arg1);
fef10b60 12021
12022 tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
c2352383 12023 if (tem && !operand_equal_p (tem, arg1, 0))
7ab7fd4f 12024 return fold_build2 (code, type, arg0, tem);
fef10b60 12025 }
12026
12027 truth_andor:
12028 /* We only do these simplifications if we are optimizing. */
12029 if (!optimize)
e7edfbbd 12030 return NULL_TREE;
fef10b60 12031
12032 /* Check for things like (A || B) && (A || C). We can convert this
12033 to A || (B && C). Note that either operator can be any of the four
12034 truth and/or operations and the transformation will still be
12035 valid. Also note that we only care about order for the
12036 ANDIF and ORIF operators. If B contains side effects, this
12037 might change the truth-value of A. */
12038 if (TREE_CODE (arg0) == TREE_CODE (arg1)
12039 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12040 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12041 || TREE_CODE (arg0) == TRUTH_AND_EXPR
12042 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12043 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12044 {
12045 tree a00 = TREE_OPERAND (arg0, 0);
12046 tree a01 = TREE_OPERAND (arg0, 1);
12047 tree a10 = TREE_OPERAND (arg1, 0);
12048 tree a11 = TREE_OPERAND (arg1, 1);
12049 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12050 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12051 && (code == TRUTH_AND_EXPR
12052 || code == TRUTH_OR_EXPR));
12053
12054 if (operand_equal_p (a00, a10, 0))
7ab7fd4f 12055 return fold_build2 (TREE_CODE (arg0), type, a00,
12056 fold_build2 (code, type, a01, a11));
fef10b60 12057 else if (commutative && operand_equal_p (a00, a11, 0))
7ab7fd4f 12058 return fold_build2 (TREE_CODE (arg0), type, a00,
12059 fold_build2 (code, type, a01, a10));
fef10b60 12060 else if (commutative && operand_equal_p (a01, a10, 0))
7ab7fd4f 12061 return fold_build2 (TREE_CODE (arg0), type, a01,
12062 fold_build2 (code, type, a00, a11));
fef10b60 12063
12064 /* This case if tricky because we must either have commutative
12065 operators or else A10 must not have side-effects. */
12066
12067 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12068 && operand_equal_p (a01, a11, 0))
7ab7fd4f 12069 return fold_build2 (TREE_CODE (arg0), type,
12070 fold_build2 (code, type, a00, a10),
12071 a01);
fef10b60 12072 }
12073
12074 /* See if we can build a range comparison. */
2c17ebb2 12075 if (0 != (tem = fold_range_test (code, type, op0, op1)))
fef10b60 12076 return tem;
12077
12078 /* Check for the possibility of merging component references. If our
12079 lhs is another similar operation, try to merge its rhs with our
12080 rhs. Then try to merge our lhs and rhs. */
12081 if (TREE_CODE (arg0) == code
12082 && 0 != (tem = fold_truthop (code, type,
12083 TREE_OPERAND (arg0, 1), arg1)))
7ab7fd4f 12084 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
fef10b60 12085
12086 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
12087 return tem;
12088
e7edfbbd 12089 return NULL_TREE;
fef10b60 12090
12091 case TRUTH_ORIF_EXPR:
12092 /* Note that the operands of this must be ints
12093 and their values must be 0 or true.
12094 ("true" is a fixed value perhaps depending on the language.) */
12095 /* If first arg is constant true, return it. */
12096 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12097 return fold_convert (type, arg0);
12098 case TRUTH_OR_EXPR:
12099 /* If either arg is constant zero, drop it. */
12100 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12101 return non_lvalue (fold_convert (type, arg1));
12102 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12103 /* Preserve sequence points. */
12104 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12105 return non_lvalue (fold_convert (type, arg0));
12106 /* If second arg is constant true, result is true, but we must
12107 evaluate first arg. */
12108 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12109 return omit_one_operand (type, arg1, arg0);
12110 /* Likewise for first arg, but note this only occurs here for
12111 TRUTH_OR_EXPR. */
12112 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12113 return omit_one_operand (type, arg0, arg1);
12114
12115 /* !X || X is always true. */
12116 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12117 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12118 return omit_one_operand (type, integer_one_node, arg1);
12119 /* X || !X is always true. */
12120 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12121 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12122 return omit_one_operand (type, integer_one_node, arg0);
12123
12124 goto truth_andor;
12125
12126 case TRUTH_XOR_EXPR:
12127 /* If the second arg is constant zero, drop it. */
12128 if (integer_zerop (arg1))
12129 return non_lvalue (fold_convert (type, arg0));
12130 /* If the second arg is constant true, this is a logical inversion. */
12131 if (integer_onep (arg1))
ce04dcdc 12132 {
12133 /* Only call invert_truthvalue if operand is a truth value. */
12134 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
7ab7fd4f 12135 tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
ce04dcdc 12136 else
12137 tem = invert_truthvalue (arg0);
12138 return non_lvalue (fold_convert (type, tem));
12139 }
fef10b60 12140 /* Identical arguments cancel to zero. */
12141 if (operand_equal_p (arg0, arg1, 0))
12142 return omit_one_operand (type, integer_zero_node, arg0);
12143
12144 /* !X ^ X is always true. */
12145 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12146 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12147 return omit_one_operand (type, integer_one_node, arg1);
12148
12149 /* X ^ !X is always true. */
12150 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12151 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12152 return omit_one_operand (type, integer_one_node, arg0);
12153
e7edfbbd 12154 return NULL_TREE;
fef10b60 12155
12156 case EQ_EXPR:
12157 case NE_EXPR:
6a451e87 12158 tem = fold_comparison (code, type, op0, op1);
12159 if (tem != NULL_TREE)
12160 return tem;
f4a6516d 12161
66787d4f 12162 /* bool_var != 0 becomes bool_var. */
12163 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12164 && code == NE_EXPR)
12165 return non_lvalue (fold_convert (type, arg0));
6a451e87 12166
66787d4f 12167 /* bool_var == 1 becomes bool_var. */
12168 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12169 && code == EQ_EXPR)
12170 return non_lvalue (fold_convert (type, arg0));
fef10b60 12171
931c6823 12172 /* bool_var != 1 becomes !bool_var. */
12173 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12174 && code == NE_EXPR)
ed14361e 12175 return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
931c6823 12176
12177 /* bool_var == 0 becomes !bool_var. */
12178 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12179 && code == EQ_EXPR)
ed14361e 12180 return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
931c6823 12181
fef10b60 12182 /* If this is an equality comparison of the address of two non-weak,
12183 unaliased symbols neither of which are extern (since we do not
12184 have access to attributes for externs), then we know the result. */
6a451e87 12185 if (TREE_CODE (arg0) == ADDR_EXPR
5ded8c6f 12186 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
fef10b60 12187 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12188 && ! lookup_attribute ("alias",
12189 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12190 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12191 && TREE_CODE (arg1) == ADDR_EXPR
5ded8c6f 12192 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
fef10b60 12193 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12194 && ! lookup_attribute ("alias",
12195 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12196 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
16bf25cd 12197 {
12198 /* We know that we're looking at the address of two
12199 non-weak, unaliased, static _DECL nodes.
12200
12201 It is both wasteful and incorrect to call operand_equal_p
12202 to compare the two ADDR_EXPR nodes. It is wasteful in that
12203 all we need to do is test pointer equality for the arguments
12204 to the two ADDR_EXPR nodes. It is incorrect to use
12205 operand_equal_p as that function is NOT equivalent to a
12206 C equality test. It can in fact return false for two
12207 objects which would test as equal using the C equality
12208 operator. */
12209 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12210 return constant_boolean_node (equal
12211 ? code == EQ_EXPR : code != EQ_EXPR,
12212 type);
12213 }
fef10b60 12214
6a451e87 12215 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12216 a MINUS_EXPR of a constant, we can convert it into a comparison with
12217 a revised constant as long as no overflow occurs. */
12218 if (TREE_CODE (arg1) == INTEGER_CST
12219 && (TREE_CODE (arg0) == PLUS_EXPR
12220 || TREE_CODE (arg0) == MINUS_EXPR)
12221 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12222 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12223 ? MINUS_EXPR : PLUS_EXPR,
a73dee2b 12224 fold_convert (TREE_TYPE (arg0), arg1),
12225 TREE_OPERAND (arg0, 1), 0))
f96bd2bf 12226 && !TREE_OVERFLOW (tem))
6a451e87 12227 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
89a6fcda 12228
6a451e87 12229 /* Similarly for a NEGATE_EXPR. */
12230 if (TREE_CODE (arg0) == NEGATE_EXPR
12231 && TREE_CODE (arg1) == INTEGER_CST
12232 && 0 != (tem = negate_expr (arg1))
12233 && TREE_CODE (tem) == INTEGER_CST
f96bd2bf 12234 && !TREE_OVERFLOW (tem))
6a451e87 12235 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
89a6fcda 12236
89476fc9 12237 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12238 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12239 && TREE_CODE (arg1) == INTEGER_CST
12240 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12241 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12242 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
12243 fold_convert (TREE_TYPE (arg0), arg1),
12244 TREE_OPERAND (arg0, 1)));
12245
172e662b 12246 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
12247 if ((TREE_CODE (arg0) == PLUS_EXPR
12248 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12249 || TREE_CODE (arg0) == MINUS_EXPR)
428b02b4 12250 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
428b02b4 12251 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12252 || POINTER_TYPE_P (TREE_TYPE (arg0))))
12253 {
172e662b 12254 tree val = TREE_OPERAND (arg0, 1);
12255 return omit_two_operands (type,
12256 fold_build2 (code, type,
12257 val,
12258 build_int_cst (TREE_TYPE (val),
12259 0)),
12260 TREE_OPERAND (arg0, 0), arg1);
12261 }
12262
12263 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
12264 if (TREE_CODE (arg0) == MINUS_EXPR
12265 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12266 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
12267 && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12268 {
12269 return omit_two_operands (type,
12270 code == NE_EXPR
12271 ? boolean_true_node : boolean_false_node,
12272 TREE_OPERAND (arg0, 1), arg1);
428b02b4 12273 }
12274
6a451e87 12275 /* If we have X - Y == 0, we can convert that to X == Y and similarly
12276 for !=. Don't do this for ordered comparisons due to overflow. */
12277 if (TREE_CODE (arg0) == MINUS_EXPR
12278 && integer_zerop (arg1))
12279 return fold_build2 (code, type,
12280 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
89a6fcda 12281
6a451e87 12282 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12283 if (TREE_CODE (arg0) == ABS_EXPR
12284 && (integer_zerop (arg1) || real_zerop (arg1)))
12285 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
89a6fcda 12286
6a451e87 12287 /* If this is an EQ or NE comparison with zero and ARG0 is
12288 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12289 two operations, but the latter can be done in one less insn
12290 on machines that have only two-operand insns or on which a
12291 constant cannot be the first operand. */
12292 if (TREE_CODE (arg0) == BIT_AND_EXPR
12293 && integer_zerop (arg1))
12294 {
12295 tree arg00 = TREE_OPERAND (arg0, 0);
12296 tree arg01 = TREE_OPERAND (arg0, 1);
12297 if (TREE_CODE (arg00) == LSHIFT_EXPR
12298 && integer_onep (TREE_OPERAND (arg00, 0)))
66fa37a1 12299 {
12300 tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
12301 arg01, TREE_OPERAND (arg00, 1));
12302 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12303 build_int_cst (TREE_TYPE (arg0), 1));
12304 return fold_build2 (code, type,
12305 fold_convert (TREE_TYPE (arg1), tem), arg1);
12306 }
12307 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12308 && integer_onep (TREE_OPERAND (arg01, 0)))
12309 {
12310 tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
12311 arg00, TREE_OPERAND (arg01, 1));
12312 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12313 build_int_cst (TREE_TYPE (arg0), 1));
12314 return fold_build2 (code, type,
12315 fold_convert (TREE_TYPE (arg1), tem), arg1);
12316 }
6a451e87 12317 }
12318
12319 /* If this is an NE or EQ comparison of zero against the result of a
12320 signed MOD operation whose second operand is a power of 2, make
12321 the MOD operation unsigned since it is simpler and equivalent. */
12322 if (integer_zerop (arg1)
12323 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12324 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12325 || TREE_CODE (arg0) == CEIL_MOD_EXPR
12326 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12327 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12328 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12329 {
71eea85c 12330 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
6a451e87 12331 tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
12332 fold_convert (newtype,
12333 TREE_OPERAND (arg0, 0)),
12334 fold_convert (newtype,
12335 TREE_OPERAND (arg0, 1)));
12336
12337 return fold_build2 (code, type, newmod,
12338 fold_convert (newtype, arg1));
12339 }
12340
1d8ff0d2 12341 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12342 C1 is a valid shift constant, and C2 is a power of two, i.e.
12343 a single bit. */
12344 if (TREE_CODE (arg0) == BIT_AND_EXPR
12345 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12346 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12347 == INTEGER_CST
12348 && integer_pow2p (TREE_OPERAND (arg0, 1))
12349 && integer_zerop (arg1))
12350 {
12351 tree itype = TREE_TYPE (arg0);
12352 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12353 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12354
12355 /* Check for a valid shift count. */
12356 if (TREE_INT_CST_HIGH (arg001) == 0
12357 && TREE_INT_CST_LOW (arg001) < prec)
12358 {
12359 tree arg01 = TREE_OPERAND (arg0, 1);
12360 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12361 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12362 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12363 can be rewritten as (X & (C2 << C1)) != 0. */
6b651543 12364 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
1d8ff0d2 12365 {
12366 tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
12367 tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
12368 return fold_build2 (code, type, tem, arg1);
12369 }
12370 /* Otherwise, for signed (arithmetic) shifts,
12371 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12372 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12373 else if (!TYPE_UNSIGNED (itype))
12374 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12375 arg000, build_int_cst (itype, 0));
12376 /* Otherwise, of unsigned (logical) shifts,
12377 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12378 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12379 else
12380 return omit_one_operand (type,
12381 code == EQ_EXPR ? integer_one_node
12382 : integer_zero_node,
12383 arg000);
12384 }
12385 }
12386
6a451e87 12387 /* If this is an NE comparison of zero with an AND of one, remove the
12388 comparison since the AND will give the correct value. */
12389 if (code == NE_EXPR
12390 && integer_zerop (arg1)
12391 && TREE_CODE (arg0) == BIT_AND_EXPR
12392 && integer_onep (TREE_OPERAND (arg0, 1)))
12393 return fold_convert (type, arg0);
12394
12395 /* If we have (A & C) == C where C is a power of 2, convert this into
12396 (A & C) != 0. Similarly for NE_EXPR. */
12397 if (TREE_CODE (arg0) == BIT_AND_EXPR
12398 && integer_pow2p (TREE_OPERAND (arg0, 1))
12399 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12400 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12401 arg0, fold_convert (TREE_TYPE (arg0),
12402 integer_zero_node));
12403
12404 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12405 bit, then fold the expression into A < 0 or A >= 0. */
12406 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
12407 if (tem)
12408 return tem;
12409
12410 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12411 Similarly for NE_EXPR. */
12412 if (TREE_CODE (arg0) == BIT_AND_EXPR
12413 && TREE_CODE (arg1) == INTEGER_CST
12414 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12415 {
12416 tree notc = fold_build1 (BIT_NOT_EXPR,
12417 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12418 TREE_OPERAND (arg0, 1));
12419 tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12420 arg1, notc);
12421 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12422 if (integer_nonzerop (dandnotc))
12423 return omit_one_operand (type, rslt, arg0);
12424 }
12425
12426 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12427 Similarly for NE_EXPR. */
12428 if (TREE_CODE (arg0) == BIT_IOR_EXPR
12429 && TREE_CODE (arg1) == INTEGER_CST
12430 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12431 {
12432 tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12433 tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12434 TREE_OPERAND (arg0, 1), notd);
12435 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12436 if (integer_nonzerop (candnotd))
12437 return omit_one_operand (type, rslt, arg0);
12438 }
12439
2a64c730 12440 /* If this is a comparison of a field, we may be able to simplify it. */
12441 if ((TREE_CODE (arg0) == COMPONENT_REF
12442 || TREE_CODE (arg0) == BIT_FIELD_REF)
12443 /* Handle the constant case even without -O
12444 to make sure the warnings are given. */
12445 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12446 {
12447 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
12448 if (t1)
12449 return t1;
12450 }
12451
6a451e87 12452 /* Optimize comparisons of strlen vs zero to a compare of the
12453 first character of the string vs zero. To wit,
12454 strlen(ptr) == 0 => *ptr == 0
12455 strlen(ptr) != 0 => *ptr != 0
12456 Other cases should reduce to one of these two (or a constant)
12457 due to the return value of strlen being unsigned. */
12458 if (TREE_CODE (arg0) == CALL_EXPR
12459 && integer_zerop (arg1))
12460 {
12461 tree fndecl = get_callee_fndecl (arg0);
6a451e87 12462
12463 if (fndecl
12464 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12465 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
c2f47e15 12466 && call_expr_nargs (arg0) == 1
12467 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
6a451e87 12468 {
c2f47e15 12469 tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
6a451e87 12470 return fold_build2 (code, type, iref,
12471 build_int_cst (TREE_TYPE (iref), 0));
12472 }
12473 }
12474
12475 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12476 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12477 if (TREE_CODE (arg0) == RSHIFT_EXPR
12478 && integer_zerop (arg1)
12479 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12480 {
12481 tree arg00 = TREE_OPERAND (arg0, 0);
12482 tree arg01 = TREE_OPERAND (arg0, 1);
12483 tree itype = TREE_TYPE (arg00);
12484 if (TREE_INT_CST_HIGH (arg01) == 0
12485 && TREE_INT_CST_LOW (arg01)
12486 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12487 {
12488 if (TYPE_UNSIGNED (itype))
12489 {
11773141 12490 itype = signed_type_for (itype);
6a451e87 12491 arg00 = fold_convert (itype, arg00);
12492 }
12493 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12494 type, arg00, build_int_cst (itype, 0));
12495 }
12496 }
12497
47da8b49 12498 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
12499 if (integer_zerop (arg1)
12500 && TREE_CODE (arg0) == BIT_XOR_EXPR)
12501 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12502 TREE_OPERAND (arg0, 1));
12503
12504 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
12505 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12506 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12507 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12508 build_int_cst (TREE_TYPE (arg1), 0));
12509 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
12510 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12511 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12512 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12513 return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
12514 build_int_cst (TREE_TYPE (arg1), 0));
12515
12516 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
12517 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12518 && TREE_CODE (arg1) == INTEGER_CST
12519 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12520 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12521 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
12522 TREE_OPERAND (arg0, 1), arg1));
12523
e08a39b3 12524 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12525 (X & C) == 0 when C is a single bit. */
12526 if (TREE_CODE (arg0) == BIT_AND_EXPR
12527 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12528 && integer_zerop (arg1)
12529 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12530 {
12531 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12532 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12533 TREE_OPERAND (arg0, 1));
12534 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12535 type, tem, arg1);
12536 }
12537
12538 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12539 constant C is a power of two, i.e. a single bit. */
12540 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12541 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12542 && integer_zerop (arg1)
12543 && integer_pow2p (TREE_OPERAND (arg0, 1))
12544 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12545 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12546 {
12547 tree arg00 = TREE_OPERAND (arg0, 0);
12548 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12549 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12550 }
12551
12552 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12553 when is C is a power of two, i.e. a single bit. */
12554 if (TREE_CODE (arg0) == BIT_AND_EXPR
12555 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12556 && integer_zerop (arg1)
12557 && integer_pow2p (TREE_OPERAND (arg0, 1))
12558 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12559 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12560 {
12561 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12562 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12563 arg000, TREE_OPERAND (arg0, 1));
12564 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12565 tem, build_int_cst (TREE_TYPE (tem), 0));
12566 }
12567
6a451e87 12568 if (integer_zerop (arg1)
12569 && tree_expr_nonzero_p (arg0))
12570 {
12571 tree res = constant_boolean_node (code==NE_EXPR, type);
12572 return omit_one_operand (type, res, arg0);
12573 }
746443a2 12574
12575 /* Fold -X op -Y as X op Y, where op is eq/ne. */
12576 if (TREE_CODE (arg0) == NEGATE_EXPR
12577 && TREE_CODE (arg1) == NEGATE_EXPR)
12578 return fold_build2 (code, type,
12579 TREE_OPERAND (arg0, 0),
12580 TREE_OPERAND (arg1, 0));
12581
32484276 12582 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12583 if (TREE_CODE (arg0) == BIT_AND_EXPR
12584 && TREE_CODE (arg1) == BIT_AND_EXPR)
12585 {
12586 tree arg00 = TREE_OPERAND (arg0, 0);
12587 tree arg01 = TREE_OPERAND (arg0, 1);
12588 tree arg10 = TREE_OPERAND (arg1, 0);
12589 tree arg11 = TREE_OPERAND (arg1, 1);
12590 tree itype = TREE_TYPE (arg0);
12591
12592 if (operand_equal_p (arg01, arg11, 0))
12593 return fold_build2 (code, type,
12594 fold_build2 (BIT_AND_EXPR, itype,
12595 fold_build2 (BIT_XOR_EXPR, itype,
12596 arg00, arg10),
12597 arg01),
12598 build_int_cst (itype, 0));
12599
12600 if (operand_equal_p (arg01, arg10, 0))
12601 return fold_build2 (code, type,
12602 fold_build2 (BIT_AND_EXPR, itype,
12603 fold_build2 (BIT_XOR_EXPR, itype,
12604 arg00, arg11),
12605 arg01),
12606 build_int_cst (itype, 0));
12607
12608 if (operand_equal_p (arg00, arg11, 0))
12609 return fold_build2 (code, type,
12610 fold_build2 (BIT_AND_EXPR, itype,
12611 fold_build2 (BIT_XOR_EXPR, itype,
12612 arg01, arg10),
12613 arg00),
12614 build_int_cst (itype, 0));
12615
12616 if (operand_equal_p (arg00, arg10, 0))
12617 return fold_build2 (code, type,
12618 fold_build2 (BIT_AND_EXPR, itype,
12619 fold_build2 (BIT_XOR_EXPR, itype,
12620 arg01, arg11),
12621 arg00),
12622 build_int_cst (itype, 0));
12623 }
12624
89476fc9 12625 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12626 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12627 {
12628 tree arg00 = TREE_OPERAND (arg0, 0);
12629 tree arg01 = TREE_OPERAND (arg0, 1);
12630 tree arg10 = TREE_OPERAND (arg1, 0);
12631 tree arg11 = TREE_OPERAND (arg1, 1);
12632 tree itype = TREE_TYPE (arg0);
12633
12634 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12635 operand_equal_p guarantees no side-effects so we don't need
12636 to use omit_one_operand on Z. */
12637 if (operand_equal_p (arg01, arg11, 0))
12638 return fold_build2 (code, type, arg00, arg10);
12639 if (operand_equal_p (arg01, arg10, 0))
12640 return fold_build2 (code, type, arg00, arg11);
12641 if (operand_equal_p (arg00, arg11, 0))
12642 return fold_build2 (code, type, arg01, arg10);
12643 if (operand_equal_p (arg00, arg10, 0))
12644 return fold_build2 (code, type, arg01, arg11);
12645
12646 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12647 if (TREE_CODE (arg01) == INTEGER_CST
12648 && TREE_CODE (arg11) == INTEGER_CST)
12649 return fold_build2 (code, type,
12650 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12651 fold_build2 (BIT_XOR_EXPR, itype,
12652 arg01, arg11)),
12653 arg10);
12654 }
5dbcb7c4 12655
12656 /* Attempt to simplify equality/inequality comparisons of complex
12657 values. Only lower the comparison if the result is known or
12658 can be simplified to a single scalar comparison. */
12659 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12660 || TREE_CODE (arg0) == COMPLEX_CST)
12661 && (TREE_CODE (arg1) == COMPLEX_EXPR
12662 || TREE_CODE (arg1) == COMPLEX_CST))
12663 {
12664 tree real0, imag0, real1, imag1;
12665 tree rcond, icond;
12666
12667 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12668 {
12669 real0 = TREE_OPERAND (arg0, 0);
12670 imag0 = TREE_OPERAND (arg0, 1);
12671 }
12672 else
12673 {
12674 real0 = TREE_REALPART (arg0);
12675 imag0 = TREE_IMAGPART (arg0);
12676 }
12677
12678 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12679 {
12680 real1 = TREE_OPERAND (arg1, 0);
12681 imag1 = TREE_OPERAND (arg1, 1);
12682 }
12683 else
12684 {
12685 real1 = TREE_REALPART (arg1);
12686 imag1 = TREE_IMAGPART (arg1);
12687 }
12688
12689 rcond = fold_binary (code, type, real0, real1);
12690 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12691 {
12692 if (integer_zerop (rcond))
12693 {
12694 if (code == EQ_EXPR)
12695 return omit_two_operands (type, boolean_false_node,
12696 imag0, imag1);
12697 return fold_build2 (NE_EXPR, type, imag0, imag1);
12698 }
12699 else
12700 {
12701 if (code == NE_EXPR)
12702 return omit_two_operands (type, boolean_true_node,
12703 imag0, imag1);
12704 return fold_build2 (EQ_EXPR, type, imag0, imag1);
12705 }
12706 }
12707
12708 icond = fold_binary (code, type, imag0, imag1);
12709 if (icond && TREE_CODE (icond) == INTEGER_CST)
12710 {
12711 if (integer_zerop (icond))
12712 {
12713 if (code == EQ_EXPR)
12714 return omit_two_operands (type, boolean_false_node,
12715 real0, real1);
12716 return fold_build2 (NE_EXPR, type, real0, real1);
12717 }
12718 else
12719 {
12720 if (code == NE_EXPR)
12721 return omit_two_operands (type, boolean_true_node,
12722 real0, real1);
12723 return fold_build2 (EQ_EXPR, type, real0, real1);
12724 }
12725 }
12726 }
12727
6a451e87 12728 return NULL_TREE;
12729
12730 case LT_EXPR:
12731 case GT_EXPR:
12732 case LE_EXPR:
12733 case GE_EXPR:
12734 tem = fold_comparison (code, type, op0, op1);
12735 if (tem != NULL_TREE)
12736 return tem;
12737
12738 /* Transform comparisons of the form X +- C CMP X. */
12739 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12740 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12741 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12742 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12743 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
981eb798 12744 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
6a451e87 12745 {
12746 tree arg01 = TREE_OPERAND (arg0, 1);
12747 enum tree_code code0 = TREE_CODE (arg0);
12748 int is_positive;
12749
12750 if (TREE_CODE (arg01) == REAL_CST)
12751 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12752 else
12753 is_positive = tree_int_cst_sgn (arg01);
12754
12755 /* (X - c) > X becomes false. */
12756 if (code == GT_EXPR
12757 && ((code0 == MINUS_EXPR && is_positive >= 0)
12758 || (code0 == PLUS_EXPR && is_positive <= 0)))
add6ee5e 12759 {
12760 if (TREE_CODE (arg01) == INTEGER_CST
12761 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12762 fold_overflow_warning (("assuming signed overflow does not "
12763 "occur when assuming that (X - c) > X "
12764 "is always false"),
12765 WARN_STRICT_OVERFLOW_ALL);
12766 return constant_boolean_node (0, type);
12767 }
6a451e87 12768
12769 /* Likewise (X + c) < X becomes false. */
12770 if (code == LT_EXPR
12771 && ((code0 == PLUS_EXPR && is_positive >= 0)
12772 || (code0 == MINUS_EXPR && is_positive <= 0)))
add6ee5e 12773 {
12774 if (TREE_CODE (arg01) == INTEGER_CST
12775 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12776 fold_overflow_warning (("assuming signed overflow does not "
12777 "occur when assuming that "
12778 "(X + c) < X is always false"),
12779 WARN_STRICT_OVERFLOW_ALL);
12780 return constant_boolean_node (0, type);
12781 }
6a451e87 12782
12783 /* Convert (X - c) <= X to true. */
12784 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12785 && code == LE_EXPR
89a6fcda 12786 && ((code0 == MINUS_EXPR && is_positive >= 0)
12787 || (code0 == PLUS_EXPR && is_positive <= 0)))
add6ee5e 12788 {
12789 if (TREE_CODE (arg01) == INTEGER_CST
12790 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12791 fold_overflow_warning (("assuming signed overflow does not "
12792 "occur when assuming that "
12793 "(X - c) <= X is always true"),
12794 WARN_STRICT_OVERFLOW_ALL);
12795 return constant_boolean_node (1, type);
12796 }
89a6fcda 12797
12798 /* Convert (X + c) >= X to true. */
12799 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12800 && code == GE_EXPR
12801 && ((code0 == PLUS_EXPR && is_positive >= 0)
12802 || (code0 == MINUS_EXPR && is_positive <= 0)))
add6ee5e 12803 {
12804 if (TREE_CODE (arg01) == INTEGER_CST
12805 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12806 fold_overflow_warning (("assuming signed overflow does not "
12807 "occur when assuming that "
12808 "(X + c) >= X is always true"),
12809 WARN_STRICT_OVERFLOW_ALL);
12810 return constant_boolean_node (1, type);
12811 }
89a6fcda 12812
12813 if (TREE_CODE (arg01) == INTEGER_CST)
12814 {
12815 /* Convert X + c > X and X - c < X to true for integers. */
12816 if (code == GT_EXPR
12817 && ((code0 == PLUS_EXPR && is_positive > 0)
12818 || (code0 == MINUS_EXPR && is_positive < 0)))
add6ee5e 12819 {
12820 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12821 fold_overflow_warning (("assuming signed overflow does "
12822 "not occur when assuming that "
12823 "(X + c) > X is always true"),
12824 WARN_STRICT_OVERFLOW_ALL);
12825 return constant_boolean_node (1, type);
12826 }
89a6fcda 12827
12828 if (code == LT_EXPR
12829 && ((code0 == MINUS_EXPR && is_positive > 0)
12830 || (code0 == PLUS_EXPR && is_positive < 0)))
add6ee5e 12831 {
12832 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12833 fold_overflow_warning (("assuming signed overflow does "
12834 "not occur when assuming that "
12835 "(X - c) < X is always true"),
12836 WARN_STRICT_OVERFLOW_ALL);
12837 return constant_boolean_node (1, type);
12838 }
89a6fcda 12839
12840 /* Convert X + c <= X and X - c >= X to false for integers. */
12841 if (code == LE_EXPR
12842 && ((code0 == PLUS_EXPR && is_positive > 0)
12843 || (code0 == MINUS_EXPR && is_positive < 0)))
add6ee5e 12844 {
12845 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12846 fold_overflow_warning (("assuming signed overflow does "
12847 "not occur when assuming that "
12848 "(X + c) <= X is always false"),
12849 WARN_STRICT_OVERFLOW_ALL);
12850 return constant_boolean_node (0, type);
12851 }
89a6fcda 12852
12853 if (code == GE_EXPR
12854 && ((code0 == MINUS_EXPR && is_positive > 0)
12855 || (code0 == PLUS_EXPR && is_positive < 0)))
add6ee5e 12856 {
12857 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12858 fold_overflow_warning (("assuming signed overflow does "
12859 "not occur when assuming that "
558517fd 12860 "(X - c) >= X is always false"),
add6ee5e 12861 WARN_STRICT_OVERFLOW_ALL);
12862 return constant_boolean_node (0, type);
12863 }
89a6fcda 12864 }
12865 }
12866
fef10b60 12867 /* Comparisons with the highest or lowest possible integer of
8aa01816 12868 the specified precision will have known values. */
fef10b60 12869 {
8aa01816 12870 tree arg1_type = TREE_TYPE (arg1);
12871 unsigned int width = TYPE_PRECISION (arg1_type);
fef10b60 12872
12873 if (TREE_CODE (arg1) == INTEGER_CST
fef10b60 12874 && width <= 2 * HOST_BITS_PER_WIDE_INT
8aa01816 12875 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
fef10b60 12876 {
12877 HOST_WIDE_INT signed_max_hi;
12878 unsigned HOST_WIDE_INT signed_max_lo;
12879 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12880
12881 if (width <= HOST_BITS_PER_WIDE_INT)
12882 {
12883 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12884 - 1;
12885 signed_max_hi = 0;
12886 max_hi = 0;
12887
8aa01816 12888 if (TYPE_UNSIGNED (arg1_type))
fef10b60 12889 {
12890 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12891 min_lo = 0;
12892 min_hi = 0;
12893 }
12894 else
12895 {
12896 max_lo = signed_max_lo;
12897 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12898 min_hi = -1;
12899 }
12900 }
12901 else
12902 {
12903 width -= HOST_BITS_PER_WIDE_INT;
12904 signed_max_lo = -1;
12905 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12906 - 1;
12907 max_lo = -1;
12908 min_lo = 0;
12909
8aa01816 12910 if (TYPE_UNSIGNED (arg1_type))
fef10b60 12911 {
12912 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12913 min_hi = 0;
12914 }
12915 else
12916 {
12917 max_hi = signed_max_hi;
12918 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12919 }
12920 }
12921
12922 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12923 && TREE_INT_CST_LOW (arg1) == max_lo)
12924 switch (code)
12925 {
12926 case GT_EXPR:
12927 return omit_one_operand (type, integer_zero_node, arg0);
12928
12929 case GE_EXPR:
ddb8b0be 12930 return fold_build2 (EQ_EXPR, type, op0, op1);
fef10b60 12931
12932 case LE_EXPR:
12933 return omit_one_operand (type, integer_one_node, arg0);
12934
12935 case LT_EXPR:
ddb8b0be 12936 return fold_build2 (NE_EXPR, type, op0, op1);
fef10b60 12937
12938 /* The GE_EXPR and LT_EXPR cases above are not normally
12939 reached because of previous transformations. */
12940
12941 default:
12942 break;
12943 }
12944 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12945 == max_hi
12946 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12947 switch (code)
12948 {
12949 case GT_EXPR:
2455d3ef 12950 arg1 = const_binop (PLUS_EXPR, arg1,
12951 build_int_cst (TREE_TYPE (arg1), 1), 0);
ddb8b0be 12952 return fold_build2 (EQ_EXPR, type,
12953 fold_convert (TREE_TYPE (arg1), arg0),
12954 arg1);
fef10b60 12955 case LE_EXPR:
2455d3ef 12956 arg1 = const_binop (PLUS_EXPR, arg1,
12957 build_int_cst (TREE_TYPE (arg1), 1), 0);
ddb8b0be 12958 return fold_build2 (NE_EXPR, type,
12959 fold_convert (TREE_TYPE (arg1), arg0),
12960 arg1);
fef10b60 12961 default:
12962 break;
12963 }
12964 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12965 == min_hi
12966 && TREE_INT_CST_LOW (arg1) == min_lo)
12967 switch (code)
12968 {
12969 case LT_EXPR:
12970 return omit_one_operand (type, integer_zero_node, arg0);
12971
12972 case LE_EXPR:
ddb8b0be 12973 return fold_build2 (EQ_EXPR, type, op0, op1);
fef10b60 12974
12975 case GE_EXPR:
12976 return omit_one_operand (type, integer_one_node, arg0);
12977
12978 case GT_EXPR:
e3359949 12979 return fold_build2 (NE_EXPR, type, op0, op1);
fef10b60 12980
12981 default:
12982 break;
12983 }
12984 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12985 == min_hi
12986 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12987 switch (code)
12988 {
12989 case GE_EXPR:
12990 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
ddb8b0be 12991 return fold_build2 (NE_EXPR, type,
12992 fold_convert (TREE_TYPE (arg1), arg0),
12993 arg1);
fef10b60 12994 case LT_EXPR:
12995 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
ddb8b0be 12996 return fold_build2 (EQ_EXPR, type,
12997 fold_convert (TREE_TYPE (arg1), arg0),
12998 arg1);
fef10b60 12999 default:
13000 break;
13001 }
13002
f2532264 13003 else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
fef10b60 13004 && TREE_INT_CST_LOW (arg1) == signed_max_lo
8aa01816 13005 && TYPE_UNSIGNED (arg1_type)
13006 /* We will flip the signedness of the comparison operator
13007 associated with the mode of arg1, so the sign bit is
13008 specified by this mode. Check that arg1 is the signed
13009 max associated with this sign bit. */
13010 && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
fef10b60 13011 /* signed_type does not work on pointer types. */
8aa01816 13012 && INTEGRAL_TYPE_P (arg1_type))
fef10b60 13013 {
13014 /* The following case also applies to X < signed_max+1
13015 and X >= signed_max+1 because previous transformations. */
13016 if (code == LE_EXPR || code == GT_EXPR)
13017 {
ddb8b0be 13018 tree st;
11773141 13019 st = signed_type_for (TREE_TYPE (arg1));
ddb8b0be 13020 return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
13021 type, fold_convert (st, arg0),
13022 build_int_cst (st, 0));
fef10b60 13023 }
13024 }
13025 }
13026 }
13027
fef10b60 13028 /* If we are comparing an ABS_EXPR with a constant, we can
13029 convert all the cases into explicit comparisons, but they may
13030 well not be faster than doing the ABS and one comparison.
13031 But ABS (X) <= C is a range comparison, which becomes a subtraction
13032 and a comparison, and is probably faster. */
6a451e87 13033 if (code == LE_EXPR
13034 && TREE_CODE (arg1) == INTEGER_CST
13035 && TREE_CODE (arg0) == ABS_EXPR
13036 && ! TREE_SIDE_EFFECTS (arg0)
13037 && (0 != (tem = negate_expr (arg1)))
13038 && TREE_CODE (tem) == INTEGER_CST
f96bd2bf 13039 && !TREE_OVERFLOW (tem))
7ab7fd4f 13040 return fold_build2 (TRUTH_ANDIF_EXPR, type,
13041 build2 (GE_EXPR, type,
13042 TREE_OPERAND (arg0, 0), tem),
13043 build2 (LE_EXPR, type,
13044 TREE_OPERAND (arg0, 0), arg1));
fef10b60 13045
13046 /* Convert ABS_EXPR<x> >= 0 to true. */
add6ee5e 13047 strict_overflow_p = false;
6a451e87 13048 if (code == GE_EXPR
6a451e87 13049 && (integer_zerop (arg1)
13050 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
add6ee5e 13051 && real_zerop (arg1)))
13052 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13053 {
13054 if (strict_overflow_p)
13055 fold_overflow_warning (("assuming signed overflow does not occur "
13056 "when simplifying comparison of "
13057 "absolute value and zero"),
13058 WARN_STRICT_OVERFLOW_CONDITIONAL);
13059 return omit_one_operand (type, integer_one_node, arg0);
13060 }
fef10b60 13061
13062 /* Convert ABS_EXPR<x> < 0 to false. */
add6ee5e 13063 strict_overflow_p = false;
6a451e87 13064 if (code == LT_EXPR
add6ee5e 13065 && (integer_zerop (arg1) || real_zerop (arg1))
13066 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13067 {
13068 if (strict_overflow_p)
13069 fold_overflow_warning (("assuming signed overflow does not occur "
13070 "when simplifying comparison of "
13071 "absolute value and zero"),
13072 WARN_STRICT_OVERFLOW_CONDITIONAL);
13073 return omit_one_operand (type, integer_zero_node, arg0);
13074 }
fef10b60 13075
fef10b60 13076 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13077 and similarly for >= into !=. */
13078 if ((code == LT_EXPR || code == GE_EXPR)
13079 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13080 && TREE_CODE (arg1) == LSHIFT_EXPR
13081 && integer_onep (TREE_OPERAND (arg1, 0)))
13082 return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13083 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13084 TREE_OPERAND (arg1, 1)),
3c6185f1 13085 build_int_cst (TREE_TYPE (arg0), 0));
fef10b60 13086
6a451e87 13087 if ((code == LT_EXPR || code == GE_EXPR)
13088 && TYPE_UNSIGNED (TREE_TYPE (arg0))
72dd6141 13089 && CONVERT_EXPR_P (arg1)
6a451e87 13090 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13091 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
fef10b60 13092 return
13093 build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13094 fold_convert (TREE_TYPE (arg0),
13095 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13096 TREE_OPERAND (TREE_OPERAND (arg1, 0),
13097 1))),
3c6185f1 13098 build_int_cst (TREE_TYPE (arg0), 0));
fef10b60 13099
6a451e87 13100 return NULL_TREE;
fef10b60 13101
13102 case UNORDERED_EXPR:
13103 case ORDERED_EXPR:
13104 case UNLT_EXPR:
13105 case UNLE_EXPR:
13106 case UNGT_EXPR:
13107 case UNGE_EXPR:
13108 case UNEQ_EXPR:
13109 case LTGT_EXPR:
13110 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13111 {
13112 t1 = fold_relational_const (code, type, arg0, arg1);
13113 if (t1 != NULL_TREE)
13114 return t1;
13115 }
13116
13117 /* If the first operand is NaN, the result is constant. */
13118 if (TREE_CODE (arg0) == REAL_CST
13119 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13120 && (code != LTGT_EXPR || ! flag_trapping_math))
13121 {
13122 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13123 ? integer_zero_node
13124 : integer_one_node;
13125 return omit_one_operand (type, t1, arg1);
13126 }
13127
13128 /* If the second operand is NaN, the result is constant. */
13129 if (TREE_CODE (arg1) == REAL_CST
13130 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13131 && (code != LTGT_EXPR || ! flag_trapping_math))
13132 {
13133 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13134 ? integer_zero_node
13135 : integer_one_node;
13136 return omit_one_operand (type, t1, arg0);
13137 }
13138
13139 /* Simplify unordered comparison of something with itself. */
13140 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13141 && operand_equal_p (arg0, arg1, 0))
13142 return constant_boolean_node (1, type);
13143
13144 if (code == LTGT_EXPR
13145 && !flag_trapping_math
13146 && operand_equal_p (arg0, arg1, 0))
13147 return constant_boolean_node (0, type);
13148
13149 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
13150 {
13151 tree targ0 = strip_float_extensions (arg0);
13152 tree targ1 = strip_float_extensions (arg1);
13153 tree newtype = TREE_TYPE (targ0);
13154
13155 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13156 newtype = TREE_TYPE (targ1);
13157
13158 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
7ab7fd4f 13159 return fold_build2 (code, type, fold_convert (newtype, targ0),
13160 fold_convert (newtype, targ1));
fef10b60 13161 }
13162
e7edfbbd 13163 return NULL_TREE;
fef10b60 13164
13165 case COMPOUND_EXPR:
13166 /* When pedantic, a compound expression can be neither an lvalue
13167 nor an integer constant expression. */
13168 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
e7edfbbd 13169 return NULL_TREE;
fef10b60 13170 /* Don't let (0, 0) be null pointer constant. */
13171 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13172 : fold_convert (type, arg1);
13173 return pedantic_non_lvalue (tem);
13174
13175 case COMPLEX_EXPR:
32cef1cc 13176 if ((TREE_CODE (arg0) == REAL_CST
13177 && TREE_CODE (arg1) == REAL_CST)
13178 || (TREE_CODE (arg0) == INTEGER_CST
13179 && TREE_CODE (arg1) == INTEGER_CST))
fef10b60 13180 return build_complex (type, arg0, arg1);
e7edfbbd 13181 return NULL_TREE;
fef10b60 13182
b0273ac6 13183 case ASSERT_EXPR:
13184 /* An ASSERT_EXPR should never be passed to fold_binary. */
13185 gcc_unreachable ();
13186
fef10b60 13187 default:
e7edfbbd 13188 return NULL_TREE;
fef10b60 13189 } /* switch (code) */
13190}
13191
f279e190 13192/* Callback for walk_tree, looking for LABEL_EXPR.
13193 Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
13194 Do not check the sub-tree of GOTO_EXPR. */
13195
13196static tree
13197contains_label_1 (tree *tp,
13198 int *walk_subtrees,
13199 void *data ATTRIBUTE_UNUSED)
13200{
13201 switch (TREE_CODE (*tp))
13202 {
13203 case LABEL_EXPR:
13204 return *tp;
13205 case GOTO_EXPR:
13206 *walk_subtrees = 0;
13207 /* no break */
13208 default:
13209 return NULL_TREE;
13210 }
13211}
13212
dd497b6a 13213/* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
f279e190 13214 accessible from outside the sub-tree. Returns NULL_TREE if no
13215 addressable label is found. */
13216
13217static bool
13218contains_label_p (tree st)
13219{
13220 return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
13221}
13222
6a4a1704 13223/* Fold a ternary expression of code CODE and type TYPE with operands
13224 OP0, OP1, and OP2. Return the folded expression if folding is
13225 successful. Otherwise, return NULL_TREE. */
6ce29c48 13226
d3858e14 13227tree
6a4a1704 13228fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
6ce29c48 13229{
6ce29c48 13230 tree tem;
13231 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
6ce29c48 13232 enum tree_code_class kind = TREE_CODE_CLASS (code);
6ce29c48 13233
13234 gcc_assert (IS_EXPR_CODE_CLASS (kind)
13235 && TREE_CODE_LENGTH (code) == 3);
13236
213c530d 13237 /* Strip any conversions that don't change the mode. This is safe
13238 for every expression, except for a comparison expression because
13239 its signedness is derived from its operands. So, in the latter
13240 case, only strip conversions that don't change the signedness.
6ce29c48 13241
213c530d 13242 Note that this is done as an internal manipulation within the
13243 constant folder, in order to find the simplest representation of
13244 the arguments so that their form can be studied. In any cases,
13245 the appropriate type conversions should be put back in the tree
13246 that will get out of the constant folder. */
13247 if (op0)
13248 {
13249 arg0 = op0;
13250 STRIP_NOPS (arg0);
13251 }
6ce29c48 13252
213c530d 13253 if (op1)
13254 {
13255 arg1 = op1;
13256 STRIP_NOPS (arg1);
6ce29c48 13257 }
13258
13259 switch (code)
13260 {
13261 case COMPONENT_REF:
13262 if (TREE_CODE (arg0) == CONSTRUCTOR
13263 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13264 {
c75b4594 13265 unsigned HOST_WIDE_INT idx;
13266 tree field, value;
13267 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13268 if (field == arg1)
13269 return value;
6ce29c48 13270 }
e7edfbbd 13271 return NULL_TREE;
6ce29c48 13272
13273 case COND_EXPR:
13274 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13275 so all simple results must be passed through pedantic_non_lvalue. */
13276 if (TREE_CODE (arg0) == INTEGER_CST)
13277 {
f279e190 13278 tree unused_op = integer_zerop (arg0) ? op1 : op2;
213c530d 13279 tem = integer_zerop (arg0) ? op2 : op1;
6ce29c48 13280 /* Only optimize constant conditions when the selected branch
13281 has the same type as the COND_EXPR. This avoids optimizing
f279e190 13282 away "c ? x : throw", where the throw has a void type.
13283 Avoid throwing away that operand which contains label. */
13284 if ((!TREE_SIDE_EFFECTS (unused_op)
13285 || !contains_label_p (unused_op))
13286 && (! VOID_TYPE_P (TREE_TYPE (tem))
13287 || VOID_TYPE_P (type)))
6ce29c48 13288 return pedantic_non_lvalue (tem);
e7edfbbd 13289 return NULL_TREE;
6ce29c48 13290 }
213c530d 13291 if (operand_equal_p (arg1, op2, 0))
6ce29c48 13292 return pedantic_omit_one_operand (type, arg1, arg0);
13293
13294 /* If we have A op B ? A : C, we may be able to convert this to a
13295 simpler expression, depending on the operation and the values
13296 of B and C. Signed zeros prevent all of these transformations,
13297 for reasons given above each one.
13298
13299 Also try swapping the arguments and inverting the conditional. */
13300 if (COMPARISON_CLASS_P (arg0)
13301 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13302 arg1, TREE_OPERAND (arg0, 1))
13303 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13304 {
213c530d 13305 tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
6ce29c48 13306 if (tem)
13307 return tem;
13308 }
13309
13310 if (COMPARISON_CLASS_P (arg0)
13311 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
213c530d 13312 op2,
6ce29c48 13313 TREE_OPERAND (arg0, 1))
213c530d 13314 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
6ce29c48 13315 {
6758b11c 13316 tem = fold_truth_not_expr (arg0);
13317 if (tem && COMPARISON_CLASS_P (tem))
6ce29c48 13318 {
213c530d 13319 tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
6ce29c48 13320 if (tem)
13321 return tem;
13322 }
13323 }
13324
13325 /* If the second operand is simpler than the third, swap them
13326 since that produces better jump optimization results. */
51164bd6 13327 if (truth_value_p (TREE_CODE (arg0))
13328 && tree_swap_operands_p (op1, op2, false))
6ce29c48 13329 {
13330 /* See if this can be inverted. If it can't, possibly because
13331 it was a floating-point inequality comparison, don't do
13332 anything. */
6758b11c 13333 tem = fold_truth_not_expr (arg0);
13334 if (tem)
7ab7fd4f 13335 return fold_build3 (code, type, tem, op2, op1);
6ce29c48 13336 }
13337
13338 /* Convert A ? 1 : 0 to simply A. */
213c530d 13339 if (integer_onep (op1)
13340 && integer_zerop (op2)
13341 /* If we try to convert OP0 to our type, the
6ce29c48 13342 call to fold will try to move the conversion inside
13343 a COND, which will recurse. In that case, the COND_EXPR
13344 is probably the best choice, so leave it alone. */
13345 && type == TREE_TYPE (arg0))
13346 return pedantic_non_lvalue (arg0);
13347
13348 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13349 over COND_EXPR in cases such as floating point comparisons. */
213c530d 13350 if (integer_zerop (op1)
13351 && integer_onep (op2)
6ce29c48 13352 && truth_value_p (TREE_CODE (arg0)))
13353 return pedantic_non_lvalue (fold_convert (type,
13354 invert_truthvalue (arg0)));
13355
13356 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13357 if (TREE_CODE (arg0) == LT_EXPR
71f1bd0c 13358 && integer_zerop (TREE_OPERAND (arg0, 1))
13359 && integer_zerop (op2)
13360 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13361 {
13362 /* sign_bit_p only checks ARG1 bits within A's precision.
13363 If <sign bit of A> has wider type than A, bits outside
13364 of A's precision in <sign bit of A> need to be checked.
13365 If they are all 0, this optimization needs to be done
13366 in unsigned A's type, if they are all 1 in signed A's type,
13367 otherwise this can't be done. */
13368 if (TYPE_PRECISION (TREE_TYPE (tem))
13369 < TYPE_PRECISION (TREE_TYPE (arg1))
13370 && TYPE_PRECISION (TREE_TYPE (tem))
13371 < TYPE_PRECISION (type))
13372 {
13373 unsigned HOST_WIDE_INT mask_lo;
13374 HOST_WIDE_INT mask_hi;
13375 int inner_width, outer_width;
13376 tree tem_type;
13377
13378 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13379 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13380 if (outer_width > TYPE_PRECISION (type))
13381 outer_width = TYPE_PRECISION (type);
13382
13383 if (outer_width > HOST_BITS_PER_WIDE_INT)
13384 {
13385 mask_hi = ((unsigned HOST_WIDE_INT) -1
13386 >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13387 mask_lo = -1;
13388 }
13389 else
13390 {
13391 mask_hi = 0;
13392 mask_lo = ((unsigned HOST_WIDE_INT) -1
13393 >> (HOST_BITS_PER_WIDE_INT - outer_width));
13394 }
13395 if (inner_width > HOST_BITS_PER_WIDE_INT)
13396 {
13397 mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13398 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13399 mask_lo = 0;
13400 }
13401 else
13402 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13403 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13404
13405 if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13406 && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13407 {
11773141 13408 tem_type = signed_type_for (TREE_TYPE (tem));
71f1bd0c 13409 tem = fold_convert (tem_type, tem);
13410 }
13411 else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13412 && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13413 {
71eea85c 13414 tem_type = unsigned_type_for (TREE_TYPE (tem));
71f1bd0c 13415 tem = fold_convert (tem_type, tem);
13416 }
13417 else
13418 tem = NULL;
13419 }
13420
13421 if (tem)
13422 return fold_convert (type,
13423 fold_build2 (BIT_AND_EXPR,
13424 TREE_TYPE (tem), tem,
13425 fold_convert (TREE_TYPE (tem),
13426 arg1)));
13427 }
6ce29c48 13428
13429 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13430 already handled above. */
13431 if (TREE_CODE (arg0) == BIT_AND_EXPR
13432 && integer_onep (TREE_OPERAND (arg0, 1))
213c530d 13433 && integer_zerop (op2)
6ce29c48 13434 && integer_pow2p (arg1))
13435 {
13436 tree tem = TREE_OPERAND (arg0, 0);
13437 STRIP_NOPS (tem);
13438 if (TREE_CODE (tem) == RSHIFT_EXPR
13439 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13440 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13441 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
7ab7fd4f 13442 return fold_build2 (BIT_AND_EXPR, type,
13443 TREE_OPERAND (tem, 0), arg1);
6ce29c48 13444 }
13445
13446 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13447 is probably obsolete because the first operand should be a
13448 truth value (that's why we have the two cases above), but let's
13449 leave it in until we can confirm this for all front-ends. */
213c530d 13450 if (integer_zerop (op2)
6ce29c48 13451 && TREE_CODE (arg0) == NE_EXPR
13452 && integer_zerop (TREE_OPERAND (arg0, 1))
13453 && integer_pow2p (arg1)
13454 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13455 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13456 arg1, OEP_ONLY_CONST))
13457 return pedantic_non_lvalue (fold_convert (type,
13458 TREE_OPERAND (arg0, 0)));
13459
13460 /* Convert A ? B : 0 into A && B if A and B are truth values. */
213c530d 13461 if (integer_zerop (op2)
6ce29c48 13462 && truth_value_p (TREE_CODE (arg0))
13463 && truth_value_p (TREE_CODE (arg1)))
a0d17866 13464 return fold_build2 (TRUTH_ANDIF_EXPR, type,
13465 fold_convert (type, arg0),
13466 arg1);
6ce29c48 13467
13468 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
213c530d 13469 if (integer_onep (op2)
6ce29c48 13470 && truth_value_p (TREE_CODE (arg0))
13471 && truth_value_p (TREE_CODE (arg1)))
13472 {
13473 /* Only perform transformation if ARG0 is easily inverted. */
6758b11c 13474 tem = fold_truth_not_expr (arg0);
13475 if (tem)
a0d17866 13476 return fold_build2 (TRUTH_ORIF_EXPR, type,
13477 fold_convert (type, tem),
13478 arg1);
6ce29c48 13479 }
13480
13481 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13482 if (integer_zerop (arg1)
13483 && truth_value_p (TREE_CODE (arg0))
213c530d 13484 && truth_value_p (TREE_CODE (op2)))
6ce29c48 13485 {
13486 /* Only perform transformation if ARG0 is easily inverted. */
6758b11c 13487 tem = fold_truth_not_expr (arg0);
13488 if (tem)
a0d17866 13489 return fold_build2 (TRUTH_ANDIF_EXPR, type,
13490 fold_convert (type, tem),
13491 op2);
6ce29c48 13492 }
13493
13494 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13495 if (integer_onep (arg1)
13496 && truth_value_p (TREE_CODE (arg0))
213c530d 13497 && truth_value_p (TREE_CODE (op2)))
a0d17866 13498 return fold_build2 (TRUTH_ORIF_EXPR, type,
13499 fold_convert (type, arg0),
13500 op2);
6ce29c48 13501
e7edfbbd 13502 return NULL_TREE;
6ce29c48 13503
13504 case CALL_EXPR:
c2f47e15 13505 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13506 of fold_ternary on them. */
13507 gcc_unreachable ();
6ce29c48 13508
b8ddd49b 13509 case BIT_FIELD_REF:
3cbad267 13510 if ((TREE_CODE (arg0) == VECTOR_CST
13511 || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
70d2daf7 13512 && type == TREE_TYPE (TREE_TYPE (arg0)))
b8ddd49b 13513 {
13514 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13515 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13516
13517 if (width != 0
13518 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13519 && (idx % width) == 0
13520 && (idx = idx / width)
13521 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13522 {
3cbad267 13523 tree elements = NULL_TREE;
13524
13525 if (TREE_CODE (arg0) == VECTOR_CST)
13526 elements = TREE_VECTOR_CST_ELTS (arg0);
13527 else
13528 {
13529 unsigned HOST_WIDE_INT idx;
13530 tree value;
13531
13532 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13533 elements = tree_cons (NULL_TREE, value, elements);
13534 }
6349b545 13535 while (idx-- > 0 && elements)
b8ddd49b 13536 elements = TREE_CHAIN (elements);
6349b545 13537 if (elements)
13538 return TREE_VALUE (elements);
13539 else
13540 return fold_convert (type, integer_zero_node);
b8ddd49b 13541 }
13542 }
9e8a83b4 13543
13544 /* A bit-field-ref that referenced the full argument can be stripped. */
13545 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13546 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13547 && integer_zerop (op2))
13548 return fold_convert (type, arg0);
13549
b8ddd49b 13550 return NULL_TREE;
13551
6ce29c48 13552 default:
e7edfbbd 13553 return NULL_TREE;
6ce29c48 13554 } /* switch (code) */
13555}
13556
2bc77e10 13557/* Perform constant folding and related simplification of EXPR.
13558 The related simplifications include x*1 => x, x*0 => 0, etc.,
13559 and application of the associative law.
13560 NOP_EXPR conversions may be removed freely (as long as we
c4b03c0f 13561 are careful not to change the type of the overall expression).
2bc77e10 13562 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13563 but we can constant-fold them if they have constant operands. */
13564
fc3df357 13565#ifdef ENABLE_FOLD_CHECKING
13566# define fold(x) fold_1 (x)
13567static tree fold_1 (tree);
13568static
13569#endif
2bc77e10 13570tree
de1b648b 13571fold (tree expr)
2bc77e10 13572{
53f78329 13573 const tree t = expr;
19cb6b50 13574 enum tree_code code = TREE_CODE (t);
ce45a448 13575 enum tree_code_class kind = TREE_CODE_CLASS (code);
e7edfbbd 13576 tree tem;
4ee9c684 13577
8541c166 13578 /* Return right away if a constant. */
ce45a448 13579 if (kind == tcc_constant)
8541c166 13580 return t;
cc049fa3 13581
c2f47e15 13582 /* CALL_EXPR-like objects with variable numbers of operands are
13583 treated specially. */
13584 if (kind == tcc_vl_exp)
13585 {
13586 if (code == CALL_EXPR)
13587 {
13588 tem = fold_call_expr (expr, false);
13589 return tem ? tem : expr;
13590 }
13591 return expr;
13592 }
13593
75a70cf9 13594 if (IS_EXPR_CODE_CLASS (kind))
422c18cb 13595 {
0052b98e 13596 tree type = TREE_TYPE (t);
6a4a1704 13597 tree op0, op1, op2;
0052b98e 13598
422c18cb 13599 switch (TREE_CODE_LENGTH (code))
13600 {
13601 case 1:
0052b98e 13602 op0 = TREE_OPERAND (t, 0);
13603 tem = fold_unary (code, type, op0);
e7edfbbd 13604 return tem ? tem : expr;
fef10b60 13605 case 2:
0052b98e 13606 op0 = TREE_OPERAND (t, 0);
13607 op1 = TREE_OPERAND (t, 1);
13608 tem = fold_binary (code, type, op0, op1);
e7edfbbd 13609 return tem ? tem : expr;
6ce29c48 13610 case 3:
6a4a1704 13611 op0 = TREE_OPERAND (t, 0);
13612 op1 = TREE_OPERAND (t, 1);
13613 op2 = TREE_OPERAND (t, 2);
13614 tem = fold_ternary (code, type, op0, op1, op2);
e7edfbbd 13615 return tem ? tem : expr;
422c18cb 13616 default:
13617 break;
13618 }
13619 }
13620
2bc77e10 13621 switch (code)
13622 {
27e9f331 13623 case ARRAY_REF:
13624 {
13625 tree op0 = TREE_OPERAND (t, 0);
13626 tree op1 = TREE_OPERAND (t, 1);
13627
13628 if (TREE_CODE (op1) == INTEGER_CST
13629 && TREE_CODE (op0) == CONSTRUCTOR
13630 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13631 {
13632 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13633 unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13634 unsigned HOST_WIDE_INT begin = 0;
13635
13636 /* Find a matching index by means of a binary search. */
13637 while (begin != end)
13638 {
13639 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13640 tree index = VEC_index (constructor_elt, elts, middle)->index;
13641
13642 if (TREE_CODE (index) == INTEGER_CST
13643 && tree_int_cst_lt (index, op1))
13644 begin = middle + 1;
13645 else if (TREE_CODE (index) == INTEGER_CST
13646 && tree_int_cst_lt (op1, index))
13647 end = middle;
13648 else if (TREE_CODE (index) == RANGE_EXPR
13649 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13650 begin = middle + 1;
13651 else if (TREE_CODE (index) == RANGE_EXPR
13652 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13653 end = middle;
13654 else
13655 return VEC_index (constructor_elt, elts, middle)->value;
13656 }
13657 }
13658
13659 return t;
13660 }
13661
2bc77e10 13662 case CONST_DECL:
13663 return fold (DECL_INITIAL (t));
13664
2bc77e10 13665 default:
13666 return t;
13667 } /* switch (code) */
13668}
76a0ced5 13669
fc3df357 13670#ifdef ENABLE_FOLD_CHECKING
13671#undef fold
13672
b4b34335 13673static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13674static void fold_check_failed (const_tree, const_tree);
13675void print_fold_checksum (const_tree);
fc3df357 13676
13677/* When --enable-checking=fold, compute a digest of expr before
13678 and after actual fold call to see if fold did not accidentally
13679 change original expr. */
13680
13681tree
13682fold (tree expr)
13683{
13684 tree ret;
13685 struct md5_ctx ctx;
13686 unsigned char checksum_before[16], checksum_after[16];
13687 htab_t ht;
13688
13689 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13690 md5_init_ctx (&ctx);
13691 fold_checksum_tree (expr, &ctx, ht);
13692 md5_finish_ctx (&ctx, checksum_before);
13693 htab_empty (ht);
13694
13695 ret = fold_1 (expr);
13696
13697 md5_init_ctx (&ctx);
13698 fold_checksum_tree (expr, &ctx, ht);
13699 md5_finish_ctx (&ctx, checksum_after);
13700 htab_delete (ht);
13701
13702 if (memcmp (checksum_before, checksum_after, 16))
13703 fold_check_failed (expr, ret);
13704
13705 return ret;
13706}
13707
13708void
b4b34335 13709print_fold_checksum (const_tree expr)
fc3df357 13710{
13711 struct md5_ctx ctx;
13712 unsigned char checksum[16], cnt;
13713 htab_t ht;
13714
13715 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13716 md5_init_ctx (&ctx);
13717 fold_checksum_tree (expr, &ctx, ht);
13718 md5_finish_ctx (&ctx, checksum);
13719 htab_delete (ht);
13720 for (cnt = 0; cnt < 16; ++cnt)
13721 fprintf (stderr, "%02x", checksum[cnt]);
13722 putc ('\n', stderr);
13723}
13724
13725static void
b4b34335 13726fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
fc3df357 13727{
13728 internal_error ("fold check: original tree changed by fold");
13729}
13730
13731static void
b4b34335 13732fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
fc3df357 13733{
b4b34335 13734 const void **slot;
fc3df357 13735 enum tree_code code;
7718e3c3 13736 union tree_node buf;
fc3df357 13737 int i, len;
b732b5f3 13738
13739recursive_label:
fc3df357 13740
fdada98f 13741 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
68239da4 13742 <= sizeof (struct tree_function_decl))
13743 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
fc3df357 13744 if (expr == NULL)
13745 return;
b4b34335 13746 slot = (const void **) htab_find_slot (ht, expr, INSERT);
fc3df357 13747 if (*slot != NULL)
13748 return;
13749 *slot = expr;
13750 code = TREE_CODE (expr);
ce45a448 13751 if (TREE_CODE_CLASS (code) == tcc_declaration
13752 && DECL_ASSEMBLER_NAME_SET_P (expr))
fc3df357 13753 {
13754 /* Allow DECL_ASSEMBLER_NAME to be modified. */
28aefcdd 13755 memcpy ((char *) &buf, expr, tree_size (expr));
b4b34335 13756 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
28aefcdd 13757 expr = (tree) &buf;
fc3df357 13758 }
ce45a448 13759 else if (TREE_CODE_CLASS (code) == tcc_type
4edf9595 13760 && (TYPE_POINTER_TO (expr)
13761 || TYPE_REFERENCE_TO (expr)
b732b5f3 13762 || TYPE_CACHED_VALUES_P (expr)
4edf9595 13763 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13764 || TYPE_NEXT_VARIANT (expr)))
fc3df357 13765 {
6b29892c 13766 /* Allow these fields to be modified. */
b4b34335 13767 tree tmp;
28aefcdd 13768 memcpy ((char *) &buf, expr, tree_size (expr));
b4b34335 13769 expr = tmp = (tree) &buf;
13770 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13771 TYPE_POINTER_TO (tmp) = NULL;
13772 TYPE_REFERENCE_TO (tmp) = NULL;
4edf9595 13773 TYPE_NEXT_VARIANT (tmp) = NULL;
b4b34335 13774 if (TYPE_CACHED_VALUES_P (tmp))
1e612ca4 13775 {
b4b34335 13776 TYPE_CACHED_VALUES_P (tmp) = 0;
13777 TYPE_CACHED_VALUES (tmp) = NULL;
1e612ca4 13778 }
fc3df357 13779 }
13780 md5_process_bytes (expr, tree_size (expr), ctx);
13781 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
ce45a448 13782 if (TREE_CODE_CLASS (code) != tcc_type
b732b5f3 13783 && TREE_CODE_CLASS (code) != tcc_declaration
2bf4108d 13784 && code != TREE_LIST
13785 && code != SSA_NAME)
fc3df357 13786 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
fc3df357 13787 switch (TREE_CODE_CLASS (code))
13788 {
ce45a448 13789 case tcc_constant:
fc3df357 13790 switch (code)
13791 {
13792 case STRING_CST:
13793 md5_process_bytes (TREE_STRING_POINTER (expr),
13794 TREE_STRING_LENGTH (expr), ctx);
13795 break;
13796 case COMPLEX_CST:
13797 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13798 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13799 break;
13800 case VECTOR_CST:
13801 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13802 break;
13803 default:
13804 break;
13805 }
13806 break;
ce45a448 13807 case tcc_exceptional:
fc3df357 13808 switch (code)
13809 {
13810 case TREE_LIST:
13811 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13812 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
b732b5f3 13813 expr = TREE_CHAIN (expr);
13814 goto recursive_label;
fc3df357 13815 break;
13816 case TREE_VEC:
13817 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13818 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13819 break;
13820 default:
13821 break;
13822 }
13823 break;
ce45a448 13824 case tcc_expression:
13825 case tcc_reference:
13826 case tcc_comparison:
13827 case tcc_unary:
13828 case tcc_binary:
13829 case tcc_statement:
c2f47e15 13830 case tcc_vl_exp:
13831 len = TREE_OPERAND_LENGTH (expr);
fc3df357 13832 for (i = 0; i < len; ++i)
13833 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13834 break;
ce45a448 13835 case tcc_declaration:
fc3df357 13836 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13837 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
aa4936de 13838 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13839 {
13840 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13841 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13842 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13843 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13844 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13845 }
68239da4 13846 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13847 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13848
13849 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13850 {
13851 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13852 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13853 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13854 }
fc3df357 13855 break;
ce45a448 13856 case tcc_type:
419ec660 13857 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13858 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
fc3df357 13859 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13860 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13861 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13862 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
419ec660 13863 if (INTEGRAL_TYPE_P (expr)
13864 || SCALAR_FLOAT_TYPE_P (expr))
13865 {
13866 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13867 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13868 }
fc3df357 13869 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
6b29892c 13870 if (TREE_CODE (expr) == RECORD_TYPE
13871 || TREE_CODE (expr) == UNION_TYPE
13872 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13873 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
fc3df357 13874 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13875 break;
13876 default:
13877 break;
13878 }
13879}
13880
394e718d 13881/* Helper function for outputting the checksum of a tree T. When
13882 debugging with gdb, you can "define mynext" to be "next" followed
13883 by "call debug_fold_checksum (op0)", then just trace down till the
13884 outputs differ. */
13885
13886void
b4b34335 13887debug_fold_checksum (const_tree t)
394e718d 13888{
13889 int i;
13890 unsigned char checksum[16];
13891 struct md5_ctx ctx;
13892 htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13893
13894 md5_init_ctx (&ctx);
13895 fold_checksum_tree (t, &ctx, ht);
13896 md5_finish_ctx (&ctx, checksum);
13897 htab_empty (ht);
13898
13899 for (i = 0; i < 16; i++)
13900 fprintf (stderr, "%d ", checksum[i]);
13901
13902 fprintf (stderr, "\n");
13903}
13904
fc3df357 13905#endif
13906
cfd3d1cc 13907/* Fold a unary tree expression with code CODE of type TYPE with an
977b7486 13908 operand OP0. Return a folded expression if successful. Otherwise,
cfd3d1cc 13909 return a tree expression with code CODE of type TYPE with an
13910 operand OP0. */
13911
13912tree
ba04ccb0 13913fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
cfd3d1cc 13914{
e6e279fe 13915 tree tem;
13916#ifdef ENABLE_FOLD_CHECKING
13917 unsigned char checksum_before[16], checksum_after[16];
13918 struct md5_ctx ctx;
13919 htab_t ht;
13920
13921 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13922 md5_init_ctx (&ctx);
13923 fold_checksum_tree (op0, &ctx, ht);
13924 md5_finish_ctx (&ctx, checksum_before);
13925 htab_empty (ht);
13926#endif
13927
13928 tem = fold_unary (code, type, op0);
13929 if (!tem)
ba04ccb0 13930 tem = build1_stat (code, type, op0 PASS_MEM_STAT);
e6e279fe 13931
13932#ifdef ENABLE_FOLD_CHECKING
13933 md5_init_ctx (&ctx);
13934 fold_checksum_tree (op0, &ctx, ht);
13935 md5_finish_ctx (&ctx, checksum_after);
13936 htab_delete (ht);
cfd3d1cc 13937
e6e279fe 13938 if (memcmp (checksum_before, checksum_after, 16))
13939 fold_check_failed (op0, tem);
13940#endif
13941 return tem;
cfd3d1cc 13942}
13943
13944/* Fold a binary tree expression with code CODE of type TYPE with
977b7486 13945 operands OP0 and OP1. Return a folded expression if successful.
cfd3d1cc 13946 Otherwise, return a tree expression with code CODE of type TYPE
13947 with operands OP0 and OP1. */
13948
13949tree
ba04ccb0 13950fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13951 MEM_STAT_DECL)
cfd3d1cc 13952{
e6e279fe 13953 tree tem;
13954#ifdef ENABLE_FOLD_CHECKING
13955 unsigned char checksum_before_op0[16],
13956 checksum_before_op1[16],
13957 checksum_after_op0[16],
13958 checksum_after_op1[16];
13959 struct md5_ctx ctx;
13960 htab_t ht;
13961
13962 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13963 md5_init_ctx (&ctx);
13964 fold_checksum_tree (op0, &ctx, ht);
13965 md5_finish_ctx (&ctx, checksum_before_op0);
13966 htab_empty (ht);
13967
13968 md5_init_ctx (&ctx);
13969 fold_checksum_tree (op1, &ctx, ht);
13970 md5_finish_ctx (&ctx, checksum_before_op1);
13971 htab_empty (ht);
13972#endif
13973
13974 tem = fold_binary (code, type, op0, op1);
13975 if (!tem)
ba04ccb0 13976 tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
e6e279fe 13977
13978#ifdef ENABLE_FOLD_CHECKING
13979 md5_init_ctx (&ctx);
13980 fold_checksum_tree (op0, &ctx, ht);
13981 md5_finish_ctx (&ctx, checksum_after_op0);
13982 htab_empty (ht);
13983
13984 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13985 fold_check_failed (op0, tem);
13986
13987 md5_init_ctx (&ctx);
13988 fold_checksum_tree (op1, &ctx, ht);
13989 md5_finish_ctx (&ctx, checksum_after_op1);
13990 htab_delete (ht);
cfd3d1cc 13991
e6e279fe 13992 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13993 fold_check_failed (op1, tem);
13994#endif
13995 return tem;
cfd3d1cc 13996}
13997
13998/* Fold a ternary tree expression with code CODE of type TYPE with
977b7486 13999 operands OP0, OP1, and OP2. Return a folded expression if
cfd3d1cc 14000 successful. Otherwise, return a tree expression with code CODE of
14001 type TYPE with operands OP0, OP1, and OP2. */
14002
14003tree
ba04ccb0 14004fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
14005 MEM_STAT_DECL)
14006{
14007 tree tem;
e6e279fe 14008#ifdef ENABLE_FOLD_CHECKING
14009 unsigned char checksum_before_op0[16],
14010 checksum_before_op1[16],
14011 checksum_before_op2[16],
14012 checksum_after_op0[16],
14013 checksum_after_op1[16],
14014 checksum_after_op2[16];
14015 struct md5_ctx ctx;
14016 htab_t ht;
14017
14018 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14019 md5_init_ctx (&ctx);
14020 fold_checksum_tree (op0, &ctx, ht);
14021 md5_finish_ctx (&ctx, checksum_before_op0);
14022 htab_empty (ht);
cfd3d1cc 14023
e6e279fe 14024 md5_init_ctx (&ctx);
14025 fold_checksum_tree (op1, &ctx, ht);
14026 md5_finish_ctx (&ctx, checksum_before_op1);
14027 htab_empty (ht);
14028
14029 md5_init_ctx (&ctx);
14030 fold_checksum_tree (op2, &ctx, ht);
14031 md5_finish_ctx (&ctx, checksum_before_op2);
14032 htab_empty (ht);
14033#endif
c2f47e15 14034
14035 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
e6e279fe 14036 tem = fold_ternary (code, type, op0, op1, op2);
14037 if (!tem)
ba04ccb0 14038 tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
e6e279fe 14039
14040#ifdef ENABLE_FOLD_CHECKING
14041 md5_init_ctx (&ctx);
14042 fold_checksum_tree (op0, &ctx, ht);
14043 md5_finish_ctx (&ctx, checksum_after_op0);
14044 htab_empty (ht);
14045
14046 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14047 fold_check_failed (op0, tem);
14048
14049 md5_init_ctx (&ctx);
14050 fold_checksum_tree (op1, &ctx, ht);
14051 md5_finish_ctx (&ctx, checksum_after_op1);
14052 htab_empty (ht);
14053
14054 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14055 fold_check_failed (op1, tem);
14056
14057 md5_init_ctx (&ctx);
14058 fold_checksum_tree (op2, &ctx, ht);
14059 md5_finish_ctx (&ctx, checksum_after_op2);
14060 htab_delete (ht);
14061
14062 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14063 fold_check_failed (op2, tem);
14064#endif
14065 return tem;
cfd3d1cc 14066}
14067
d01f58f9 14068/* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14069 arguments in ARGARRAY, and a null static chain.
c2f47e15 14070 Return a folded expression if successful. Otherwise, return a CALL_EXPR
d01f58f9 14071 of type TYPE from the given operands as constructed by build_call_array. */
c2f47e15 14072
14073tree
d01f58f9 14074fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
c2f47e15 14075{
14076 tree tem;
14077#ifdef ENABLE_FOLD_CHECKING
14078 unsigned char checksum_before_fn[16],
14079 checksum_before_arglist[16],
14080 checksum_after_fn[16],
14081 checksum_after_arglist[16];
14082 struct md5_ctx ctx;
14083 htab_t ht;
d01f58f9 14084 int i;
c2f47e15 14085
14086 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14087 md5_init_ctx (&ctx);
14088 fold_checksum_tree (fn, &ctx, ht);
14089 md5_finish_ctx (&ctx, checksum_before_fn);
14090 htab_empty (ht);
14091
14092 md5_init_ctx (&ctx);
d01f58f9 14093 for (i = 0; i < nargs; i++)
14094 fold_checksum_tree (argarray[i], &ctx, ht);
c2f47e15 14095 md5_finish_ctx (&ctx, checksum_before_arglist);
14096 htab_empty (ht);
14097#endif
14098
d01f58f9 14099 tem = fold_builtin_call_array (type, fn, nargs, argarray);
c2f47e15 14100
14101#ifdef ENABLE_FOLD_CHECKING
14102 md5_init_ctx (&ctx);
14103 fold_checksum_tree (fn, &ctx, ht);
14104 md5_finish_ctx (&ctx, checksum_after_fn);
14105 htab_empty (ht);
14106
14107 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14108 fold_check_failed (fn, tem);
14109
14110 md5_init_ctx (&ctx);
d01f58f9 14111 for (i = 0; i < nargs; i++)
14112 fold_checksum_tree (argarray[i], &ctx, ht);
c2f47e15 14113 md5_finish_ctx (&ctx, checksum_after_arglist);
14114 htab_delete (ht);
14115
14116 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
d01f58f9 14117 fold_check_failed (NULL_TREE, tem);
c2f47e15 14118#endif
14119 return tem;
14120}
14121
91c82c20 14122/* Perform constant folding and related simplification of initializer
a62b6979 14123 expression EXPR. These behave identically to "fold_buildN" but ignore
276beea2 14124 potential run-time traps and exceptions that fold must preserve. */
14125
a62b6979 14126#define START_FOLD_INIT \
14127 int saved_signaling_nans = flag_signaling_nans;\
14128 int saved_trapping_math = flag_trapping_math;\
14129 int saved_rounding_math = flag_rounding_math;\
14130 int saved_trapv = flag_trapv;\
47be647d 14131 int saved_folding_initializer = folding_initializer;\
a62b6979 14132 flag_signaling_nans = 0;\
14133 flag_trapping_math = 0;\
14134 flag_rounding_math = 0;\
47be647d 14135 flag_trapv = 0;\
14136 folding_initializer = 1;
a62b6979 14137
14138#define END_FOLD_INIT \
14139 flag_signaling_nans = saved_signaling_nans;\
14140 flag_trapping_math = saved_trapping_math;\
14141 flag_rounding_math = saved_rounding_math;\
47be647d 14142 flag_trapv = saved_trapv;\
14143 folding_initializer = saved_folding_initializer;
a62b6979 14144
14145tree
14146fold_build1_initializer (enum tree_code code, tree type, tree op)
14147{
14148 tree result;
14149 START_FOLD_INIT;
14150
14151 result = fold_build1 (code, type, op);
14152
14153 END_FOLD_INIT;
14154 return result;
14155}
14156
276beea2 14157tree
a62b6979 14158fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
276beea2 14159{
276beea2 14160 tree result;
a62b6979 14161 START_FOLD_INIT;
14162
14163 result = fold_build2 (code, type, op0, op1);
276beea2 14164
a62b6979 14165 END_FOLD_INIT;
14166 return result;
14167}
276beea2 14168
a62b6979 14169tree
14170fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
14171 tree op2)
14172{
14173 tree result;
14174 START_FOLD_INIT;
276beea2 14175
a62b6979 14176 result = fold_build3 (code, type, op0, op1, op2);
276beea2 14177
a62b6979 14178 END_FOLD_INIT;
276beea2 14179 return result;
14180}
14181
c2f47e15 14182tree
d01f58f9 14183fold_build_call_array_initializer (tree type, tree fn,
14184 int nargs, tree *argarray)
c2f47e15 14185{
14186 tree result;
14187 START_FOLD_INIT;
14188
d01f58f9 14189 result = fold_build_call_array (type, fn, nargs, argarray);
c2f47e15 14190
14191 END_FOLD_INIT;
14192 return result;
14193}
14194
a62b6979 14195#undef START_FOLD_INIT
14196#undef END_FOLD_INIT
14197
7014838c 14198/* Determine if first argument is a multiple of second argument. Return 0 if
14199 it is not, or we cannot easily determined it to be.
76a0ced5 14200
7014838c 14201 An example of the sort of thing we care about (at this point; this routine
14202 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14203 fold cases do now) is discovering that
76a0ced5 14204
14205 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14206
14207 is a multiple of
14208
14209 SAVE_EXPR (J * 8)
14210
7014838c 14211 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
76a0ced5 14212
14213 This code also handles discovering that
14214
14215 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14216
7014838c 14217 is a multiple of 8 so we don't have to worry about dealing with a
76a0ced5 14218 possible remainder.
14219
7014838c 14220 Note that we *look* inside a SAVE_EXPR only to determine how it was
14221 calculated; it is not safe for fold to do much of anything else with the
14222 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14223 at run time. For example, the latter example above *cannot* be implemented
14224 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14225 evaluation time of the original SAVE_EXPR is not necessarily the same at
14226 the time the new expression is evaluated. The only optimization of this
76a0ced5 14227 sort that would be valid is changing
14228
14229 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
76a0ced5 14230
7014838c 14231 divided by 8 to
76a0ced5 14232
14233 SAVE_EXPR (I) * SAVE_EXPR (J)
14234
14235 (where the same SAVE_EXPR (J) is used in the original and the
14236 transformed version). */
14237
96b038b0 14238int
b4b34335 14239multiple_of_p (tree type, const_tree top, const_tree bottom)
76a0ced5 14240{
14241 if (operand_equal_p (top, bottom, 0))
14242 return 1;
14243
14244 if (TREE_CODE (type) != INTEGER_TYPE)
14245 return 0;
14246
14247 switch (TREE_CODE (top))
14248 {
d5dd61a2 14249 case BIT_AND_EXPR:
14250 /* Bitwise and provides a power of two multiple. If the mask is
14251 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14252 if (!integer_pow2p (bottom))
14253 return 0;
14254 /* FALLTHRU */
14255
76a0ced5 14256 case MULT_EXPR:
14257 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14258 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14259
14260 case PLUS_EXPR:
14261 case MINUS_EXPR:
14262 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14263 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14264
17e3940f 14265 case LSHIFT_EXPR:
14266 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14267 {
14268 tree op1, t1;
14269
14270 op1 = TREE_OPERAND (top, 1);
14271 /* const_binop may not detect overflow correctly,
14272 so check for it explicitly here. */
14273 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14274 > TREE_INT_CST_LOW (op1)
14275 && TREE_INT_CST_HIGH (op1) == 0
b30e3dbc 14276 && 0 != (t1 = fold_convert (type,
14277 const_binop (LSHIFT_EXPR,
14278 size_one_node,
14279 op1, 0)))
f96bd2bf 14280 && !TREE_OVERFLOW (t1))
17e3940f 14281 return multiple_of_p (type, t1, bottom);
14282 }
14283 return 0;
14284
76a0ced5 14285 case NOP_EXPR:
7014838c 14286 /* Can't handle conversions from non-integral or wider integral type. */
76a0ced5 14287 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14288 || (TYPE_PRECISION (type)
14289 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14290 return 0;
7014838c 14291
6312a35e 14292 /* .. fall through ... */
7014838c 14293
76a0ced5 14294 case SAVE_EXPR:
14295 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14296
14297 case INTEGER_CST:
17e3940f 14298 if (TREE_CODE (bottom) != INTEGER_CST
ee96af51 14299 || integer_zerop (bottom)
78a8ed03 14300 || (TYPE_UNSIGNED (type)
17e3940f 14301 && (tree_int_cst_sgn (top) < 0
14302 || tree_int_cst_sgn (bottom) < 0)))
76a0ced5 14303 return 0;
426a138f 14304 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14305 top, bottom, 0));
76a0ced5 14306
14307 default:
14308 return 0;
14309 }
14310}
0f221fb7 14311
ea1a85df 14312/* Return true if CODE or TYPE is known to be non-negative. */
14313
14314static bool
14315tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14316{
14317 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14318 && truth_value_p (code))
14319 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14320 have a signed:1 type (where the value is -1 and 0). */
14321 return true;
14322 return false;
14323}
14324
14325/* Return true if (CODE OP0) is known to be non-negative. If the return
add6ee5e 14326 value is based on the assumption that signed overflow is undefined,
14327 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14328 *STRICT_OVERFLOW_P. */
0f221fb7 14329
93116081 14330bool
ea1a85df 14331tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14332 bool *strict_overflow_p)
0f221fb7 14333{
ea1a85df 14334 if (TYPE_UNSIGNED (type))
cd29ee4b 14335 return true;
00bb4a78 14336
ea1a85df 14337 switch (code)
0f221fb7 14338 {
cde9d0c7 14339 case ABS_EXPR:
8040d1c5 14340 /* We can't return 1 if flag_wrapv is set because
14341 ABS_EXPR<INT_MIN> = INT_MIN. */
ea1a85df 14342 if (!INTEGRAL_TYPE_P (type))
981eb798 14343 return true;
ea1a85df 14344 if (TYPE_OVERFLOW_UNDEFINED (type))
add6ee5e 14345 {
14346 *strict_overflow_p = true;
14347 return true;
14348 }
8040d1c5 14349 break;
8f4be2be 14350
ea1a85df 14351 case NON_LVALUE_EXPR:
14352 case FLOAT_EXPR:
14353 case FIX_TRUNC_EXPR:
14354 return tree_expr_nonnegative_warnv_p (op0,
14355 strict_overflow_p);
cfb7235b 14356
ea1a85df 14357 case NOP_EXPR:
14358 {
14359 tree inner_type = TREE_TYPE (op0);
14360 tree outer_type = type;
cfb7235b 14361
ea1a85df 14362 if (TREE_CODE (outer_type) == REAL_TYPE)
14363 {
14364 if (TREE_CODE (inner_type) == REAL_TYPE)
14365 return tree_expr_nonnegative_warnv_p (op0,
14366 strict_overflow_p);
14367 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14368 {
14369 if (TYPE_UNSIGNED (inner_type))
14370 return true;
14371 return tree_expr_nonnegative_warnv_p (op0,
14372 strict_overflow_p);
14373 }
14374 }
14375 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14376 {
14377 if (TREE_CODE (inner_type) == REAL_TYPE)
14378 return tree_expr_nonnegative_warnv_p (op0,
14379 strict_overflow_p);
14380 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14381 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14382 && TYPE_UNSIGNED (inner_type);
14383 }
14384 }
14385 break;
14386
14387 default:
14388 return tree_simple_nonnegative_warnv_p (code, type);
14389 }
14390
14391 /* We don't know sign of `t', so be conservative and return false. */
14392 return false;
14393}
06f0b99c 14394
ea1a85df 14395/* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14396 value is based on the assumption that signed overflow is undefined,
14397 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14398 *STRICT_OVERFLOW_P. */
14399
93116081 14400bool
ea1a85df 14401tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14402 tree op1, bool *strict_overflow_p)
14403{
14404 if (TYPE_UNSIGNED (type))
14405 return true;
14406
14407 switch (code)
14408 {
0de36bdb 14409 case POINTER_PLUS_EXPR:
cfb7235b 14410 case PLUS_EXPR:
ea1a85df 14411 if (FLOAT_TYPE_P (type))
14412 return (tree_expr_nonnegative_warnv_p (op0,
add6ee5e 14413 strict_overflow_p)
ea1a85df 14414 && tree_expr_nonnegative_warnv_p (op1,
add6ee5e 14415 strict_overflow_p));
ae98dc4b 14416
dfcd8f35 14417 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
2b8ef647 14418 both unsigned and at least 2 bits shorter than the result. */
ea1a85df 14419 if (TREE_CODE (type) == INTEGER_TYPE
14420 && TREE_CODE (op0) == NOP_EXPR
14421 && TREE_CODE (op1) == NOP_EXPR)
ae98dc4b 14422 {
ea1a85df 14423 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14424 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
78a8ed03 14425 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14426 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
ae98dc4b 14427 {
14428 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14429 TYPE_PRECISION (inner2)) + 1;
ea1a85df 14430 return prec < TYPE_PRECISION (type);
ae98dc4b 14431 }
14432 }
14433 break;
cfb7235b 14434
14435 case MULT_EXPR:
ea1a85df 14436 if (FLOAT_TYPE_P (type))
cfb7235b 14437 {
14438 /* x * x for floating point x is always non-negative. */
ea1a85df 14439 if (operand_equal_p (op0, op1, 0))
cd29ee4b 14440 return true;
ea1a85df 14441 return (tree_expr_nonnegative_warnv_p (op0,
add6ee5e 14442 strict_overflow_p)
ea1a85df 14443 && tree_expr_nonnegative_warnv_p (op1,
add6ee5e 14444 strict_overflow_p));
cfb7235b 14445 }
ae98dc4b 14446
dfcd8f35 14447 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
ae98dc4b 14448 both unsigned and their total bits is shorter than the result. */
ea1a85df 14449 if (TREE_CODE (type) == INTEGER_TYPE
5485b959 14450 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14451 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
ae98dc4b 14452 {
5485b959 14453 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14454 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14455 : TREE_TYPE (op0);
14456 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14457 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14458 : TREE_TYPE (op1);
14459
14460 bool unsigned0 = TYPE_UNSIGNED (inner0);
14461 bool unsigned1 = TYPE_UNSIGNED (inner1);
14462
14463 if (TREE_CODE (op0) == INTEGER_CST)
14464 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14465
14466 if (TREE_CODE (op1) == INTEGER_CST)
14467 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14468
14469 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14470 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14471 {
14472 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14473 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14474 : TYPE_PRECISION (inner0);
14475
14476 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14477 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14478 : TYPE_PRECISION (inner1);
14479
14480 return precision0 + precision1 < TYPE_PRECISION (type);
14481 }
ae98dc4b 14482 }
cd29ee4b 14483 return false;
cfb7235b 14484
4fc636aa 14485 case BIT_AND_EXPR:
14486 case MAX_EXPR:
ea1a85df 14487 return (tree_expr_nonnegative_warnv_p (op0,
add6ee5e 14488 strict_overflow_p)
ea1a85df 14489 || tree_expr_nonnegative_warnv_p (op1,
add6ee5e 14490 strict_overflow_p));
4fc636aa 14491
14492 case BIT_IOR_EXPR:
14493 case BIT_XOR_EXPR:
14494 case MIN_EXPR:
14495 case RDIV_EXPR:
a9436f5c 14496 case TRUNC_DIV_EXPR:
14497 case CEIL_DIV_EXPR:
14498 case FLOOR_DIV_EXPR:
14499 case ROUND_DIV_EXPR:
ea1a85df 14500 return (tree_expr_nonnegative_warnv_p (op0,
add6ee5e 14501 strict_overflow_p)
ea1a85df 14502 && tree_expr_nonnegative_warnv_p (op1,
add6ee5e 14503 strict_overflow_p));
ae98dc4b 14504
a9436f5c 14505 case TRUNC_MOD_EXPR:
14506 case CEIL_MOD_EXPR:
14507 case FLOOR_MOD_EXPR:
14508 case ROUND_MOD_EXPR:
ea1a85df 14509 return tree_expr_nonnegative_warnv_p (op0,
add6ee5e 14510 strict_overflow_p);
ea1a85df 14511 default:
14512 return tree_simple_nonnegative_warnv_p (code, type);
14513 }
ae98dc4b 14514
ea1a85df 14515 /* We don't know sign of `t', so be conservative and return false. */
14516 return false;
14517}
ae98dc4b 14518
ea1a85df 14519/* Return true if T is known to be non-negative. If the return
14520 value is based on the assumption that signed overflow is undefined,
14521 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14522 *STRICT_OVERFLOW_P. */
14523
93116081 14524bool
ea1a85df 14525tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14526{
14527 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14528 return true;
14529
ad7c187a 14530 switch (TREE_CODE (t))
ea1a85df 14531 {
ea1a85df 14532 case INTEGER_CST:
14533 return tree_int_cst_sgn (t) >= 0;
14534
14535 case REAL_CST:
14536 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14537
14538 case FIXED_CST:
14539 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
4fc636aa 14540
14541 case COND_EXPR:
add6ee5e 14542 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14543 strict_overflow_p)
14544 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14545 strict_overflow_p));
ea1a85df 14546 default:
14547 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14548 TREE_TYPE (t));
14549 }
14550 /* We don't know sign of `t', so be conservative and return false. */
14551 return false;
14552}
44c9fd6a 14553
18f22ec3 14554/* Return true if T is known to be non-negative. If the return
14555 value is based on the assumption that signed overflow is undefined,
14556 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14557 *STRICT_OVERFLOW_P. */
14558
14559bool
75a70cf9 14560tree_call_nonnegative_warnv_p (tree type, tree fndecl,
18f22ec3 14561 tree arg0, tree arg1, bool *strict_overflow_p)
14562{
14563 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14564 switch (DECL_FUNCTION_CODE (fndecl))
14565 {
14566 CASE_FLT_FN (BUILT_IN_ACOS):
14567 CASE_FLT_FN (BUILT_IN_ACOSH):
14568 CASE_FLT_FN (BUILT_IN_CABS):
14569 CASE_FLT_FN (BUILT_IN_COSH):
14570 CASE_FLT_FN (BUILT_IN_ERFC):
14571 CASE_FLT_FN (BUILT_IN_EXP):
14572 CASE_FLT_FN (BUILT_IN_EXP10):
14573 CASE_FLT_FN (BUILT_IN_EXP2):
14574 CASE_FLT_FN (BUILT_IN_FABS):
14575 CASE_FLT_FN (BUILT_IN_FDIM):
14576 CASE_FLT_FN (BUILT_IN_HYPOT):
14577 CASE_FLT_FN (BUILT_IN_POW10):
14578 CASE_INT_FN (BUILT_IN_FFS):
14579 CASE_INT_FN (BUILT_IN_PARITY):
14580 CASE_INT_FN (BUILT_IN_POPCOUNT):
14581 case BUILT_IN_BSWAP32:
14582 case BUILT_IN_BSWAP64:
14583 /* Always true. */
14584 return true;
14585
14586 CASE_FLT_FN (BUILT_IN_SQRT):
14587 /* sqrt(-0.0) is -0.0. */
14588 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14589 return true;
14590 return tree_expr_nonnegative_warnv_p (arg0,
14591 strict_overflow_p);
14592
14593 CASE_FLT_FN (BUILT_IN_ASINH):
14594 CASE_FLT_FN (BUILT_IN_ATAN):
14595 CASE_FLT_FN (BUILT_IN_ATANH):
14596 CASE_FLT_FN (BUILT_IN_CBRT):
14597 CASE_FLT_FN (BUILT_IN_CEIL):
14598 CASE_FLT_FN (BUILT_IN_ERF):
14599 CASE_FLT_FN (BUILT_IN_EXPM1):
14600 CASE_FLT_FN (BUILT_IN_FLOOR):
14601 CASE_FLT_FN (BUILT_IN_FMOD):
14602 CASE_FLT_FN (BUILT_IN_FREXP):
14603 CASE_FLT_FN (BUILT_IN_LCEIL):
14604 CASE_FLT_FN (BUILT_IN_LDEXP):
14605 CASE_FLT_FN (BUILT_IN_LFLOOR):
14606 CASE_FLT_FN (BUILT_IN_LLCEIL):
14607 CASE_FLT_FN (BUILT_IN_LLFLOOR):
14608 CASE_FLT_FN (BUILT_IN_LLRINT):
14609 CASE_FLT_FN (BUILT_IN_LLROUND):
14610 CASE_FLT_FN (BUILT_IN_LRINT):
14611 CASE_FLT_FN (BUILT_IN_LROUND):
14612 CASE_FLT_FN (BUILT_IN_MODF):
14613 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14614 CASE_FLT_FN (BUILT_IN_RINT):
14615 CASE_FLT_FN (BUILT_IN_ROUND):
14616 CASE_FLT_FN (BUILT_IN_SCALB):
14617 CASE_FLT_FN (BUILT_IN_SCALBLN):
14618 CASE_FLT_FN (BUILT_IN_SCALBN):
14619 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14620 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14621 CASE_FLT_FN (BUILT_IN_SINH):
14622 CASE_FLT_FN (BUILT_IN_TANH):
14623 CASE_FLT_FN (BUILT_IN_TRUNC):
14624 /* True if the 1st argument is nonnegative. */
14625 return tree_expr_nonnegative_warnv_p (arg0,
14626 strict_overflow_p);
14627
14628 CASE_FLT_FN (BUILT_IN_FMAX):
14629 /* True if the 1st OR 2nd arguments are nonnegative. */
14630 return (tree_expr_nonnegative_warnv_p (arg0,
14631 strict_overflow_p)
14632 || (tree_expr_nonnegative_warnv_p (arg1,
14633 strict_overflow_p)));
14634
14635 CASE_FLT_FN (BUILT_IN_FMIN):
14636 /* True if the 1st AND 2nd arguments are nonnegative. */
14637 return (tree_expr_nonnegative_warnv_p (arg0,
14638 strict_overflow_p)
14639 && (tree_expr_nonnegative_warnv_p (arg1,
14640 strict_overflow_p)));
14641
14642 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14643 /* True if the 2nd argument is nonnegative. */
14644 return tree_expr_nonnegative_warnv_p (arg1,
14645 strict_overflow_p);
14646
14647 CASE_FLT_FN (BUILT_IN_POWI):
14648 /* True if the 1st argument is nonnegative or the second
14649 argument is an even integer. */
d14c123b 14650 if (TREE_CODE (arg1) == INTEGER_CST
14651 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14652 return true;
18f22ec3 14653 return tree_expr_nonnegative_warnv_p (arg0,
14654 strict_overflow_p);
14655
14656 CASE_FLT_FN (BUILT_IN_POW):
14657 /* True if the 1st argument is nonnegative or the second
14658 argument is an even integer valued real. */
14659 if (TREE_CODE (arg1) == REAL_CST)
14660 {
14661 REAL_VALUE_TYPE c;
14662 HOST_WIDE_INT n;
14663
14664 c = TREE_REAL_CST (arg1);
14665 n = real_to_integer (&c);
14666 if ((n & 1) == 0)
14667 {
14668 REAL_VALUE_TYPE cint;
14669 real_from_integer (&cint, VOIDmode, n,
14670 n < 0 ? -1 : 0, 0);
14671 if (real_identical (&c, &cint))
14672 return true;
14673 }
14674 }
14675 return tree_expr_nonnegative_warnv_p (arg0,
14676 strict_overflow_p);
14677
14678 default:
14679 break;
14680 }
75a70cf9 14681 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
18f22ec3 14682 type);
14683}
14684
ea1a85df 14685/* Return true if T is known to be non-negative. If the return
14686 value is based on the assumption that signed overflow is undefined,
14687 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14688 *STRICT_OVERFLOW_P. */
ae98dc4b 14689
93116081 14690bool
ea1a85df 14691tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14692{
ad7c187a 14693 enum tree_code code = TREE_CODE (t);
ea1a85df 14694 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14695 return true;
ae98dc4b 14696
ea1a85df 14697 switch (code)
14698 {
2569a1be 14699 case TARGET_EXPR:
14700 {
14701 tree temp = TARGET_EXPR_SLOT (t);
14702 t = TARGET_EXPR_INITIAL (t);
14703
14704 /* If the initializer is non-void, then it's a normal expression
14705 that will be assigned to the slot. */
14706 if (!VOID_TYPE_P (t))
add6ee5e 14707 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
2569a1be 14708
14709 /* Otherwise, the initializer sets the slot in some way. One common
14710 way is an assignment statement at the end of the initializer. */
14711 while (1)
14712 {
14713 if (TREE_CODE (t) == BIND_EXPR)
14714 t = expr_last (BIND_EXPR_BODY (t));
14715 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14716 || TREE_CODE (t) == TRY_CATCH_EXPR)
14717 t = expr_last (TREE_OPERAND (t, 0));
14718 else if (TREE_CODE (t) == STATEMENT_LIST)
14719 t = expr_last (t);
14720 else
14721 break;
14722 }
75a70cf9 14723 if (TREE_CODE (t) == MODIFY_EXPR
14724 && TREE_OPERAND (t, 0) == temp)
14725 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
add6ee5e 14726 strict_overflow_p);
2569a1be 14727
cd29ee4b 14728 return false;
2569a1be 14729 }
14730
c63f4ad3 14731 case CALL_EXPR:
c6e6ecb1 14732 {
18f22ec3 14733 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14734 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14735
75a70cf9 14736 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
18f22ec3 14737 get_callee_fndecl (t),
14738 arg0,
14739 arg1,
14740 strict_overflow_p);
c6e6ecb1 14741 }
ea1a85df 14742 case COMPOUND_EXPR:
14743 case MODIFY_EXPR:
75a70cf9 14744 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
ea1a85df 14745 strict_overflow_p);
14746 case BIND_EXPR:
14747 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14748 strict_overflow_p);
14749 case SAVE_EXPR:
14750 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14751 strict_overflow_p);
c63f4ad3 14752
0f221fb7 14753 default:
ea1a85df 14754 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14755 TREE_TYPE (t));
0f221fb7 14756 }
ae98dc4b 14757
14758 /* We don't know sign of `t', so be conservative and return false. */
cd29ee4b 14759 return false;
0f221fb7 14760}
14761
ea1a85df 14762/* Return true if T is known to be non-negative. If the return
14763 value is based on the assumption that signed overflow is undefined,
14764 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14765 *STRICT_OVERFLOW_P. */
14766
14767bool
14768tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14769{
14770 enum tree_code code;
14771 if (t == error_mark_node)
14772 return false;
14773
14774 code = TREE_CODE (t);
14775 switch (TREE_CODE_CLASS (code))
14776 {
14777 case tcc_binary:
14778 case tcc_comparison:
14779 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14780 TREE_TYPE (t),
14781 TREE_OPERAND (t, 0),
14782 TREE_OPERAND (t, 1),
14783 strict_overflow_p);
14784
14785 case tcc_unary:
14786 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14787 TREE_TYPE (t),
14788 TREE_OPERAND (t, 0),
14789 strict_overflow_p);
14790
14791 case tcc_constant:
14792 case tcc_declaration:
14793 case tcc_reference:
14794 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14795
14796 default:
14797 break;
14798 }
14799
14800 switch (code)
14801 {
14802 case TRUTH_AND_EXPR:
14803 case TRUTH_OR_EXPR:
14804 case TRUTH_XOR_EXPR:
14805 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14806 TREE_TYPE (t),
14807 TREE_OPERAND (t, 0),
14808 TREE_OPERAND (t, 1),
14809 strict_overflow_p);
14810 case TRUTH_NOT_EXPR:
14811 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14812 TREE_TYPE (t),
14813 TREE_OPERAND (t, 0),
14814 strict_overflow_p);
14815
14816 case COND_EXPR:
14817 case CONSTRUCTOR:
14818 case OBJ_TYPE_REF:
14819 case ASSERT_EXPR:
14820 case ADDR_EXPR:
14821 case WITH_SIZE_EXPR:
14822 case EXC_PTR_EXPR:
14823 case SSA_NAME:
14824 case FILTER_EXPR:
14825 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14826
14827 default:
14828 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14829 }
14830}
14831
add6ee5e 14832/* Return true if `t' is known to be non-negative. Handle warnings
14833 about undefined signed overflow. */
14834
14835bool
14836tree_expr_nonnegative_p (tree t)
14837{
14838 bool ret, strict_overflow_p;
14839
14840 strict_overflow_p = false;
14841 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14842 if (strict_overflow_p)
14843 fold_overflow_warning (("assuming signed overflow does not occur when "
14844 "determining that expression is always "
14845 "non-negative"),
14846 WARN_STRICT_OVERFLOW_MISC);
14847 return ret;
14848}
14849
937bceaf 14850
14851/* Return true when (CODE OP0) is an address and is known to be nonzero.
ad46984d 14852 For floating point we further ensure that T is not denormal.
add6ee5e 14853 Similar logic is present in nonzero_address in rtlanal.h.
14854
14855 If the return value is based on the assumption that signed overflow
14856 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14857 change *STRICT_OVERFLOW_P. */
ad46984d 14858
93116081 14859bool
937bceaf 14860tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14861 bool *strict_overflow_p)
ad46984d 14862{
937bceaf 14863 switch (code)
14864 {
14865 case ABS_EXPR:
14866 return tree_expr_nonzero_warnv_p (op0,
14867 strict_overflow_p);
ad46984d 14868
937bceaf 14869 case NOP_EXPR:
14870 {
14871 tree inner_type = TREE_TYPE (op0);
14872 tree outer_type = type;
ad46984d 14873
937bceaf 14874 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14875 && tree_expr_nonzero_warnv_p (op0,
14876 strict_overflow_p));
14877 }
14878 break;
8dbf774a 14879
937bceaf 14880 case NON_LVALUE_EXPR:
14881 return tree_expr_nonzero_warnv_p (op0,
add6ee5e 14882 strict_overflow_p);
ad46984d 14883
937bceaf 14884 default:
14885 break;
14886 }
14887
14888 return false;
14889}
14890
14891/* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14892 For floating point we further ensure that T is not denormal.
14893 Similar logic is present in nonzero_address in rtlanal.h.
14894
14895 If the return value is based on the assumption that signed overflow
14896 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14897 change *STRICT_OVERFLOW_P. */
ad46984d 14898
93116081 14899bool
937bceaf 14900tree_binary_nonzero_warnv_p (enum tree_code code,
14901 tree type,
14902 tree op0,
14903 tree op1, bool *strict_overflow_p)
14904{
14905 bool sub_strict_overflow_p;
14906 switch (code)
14907 {
0de36bdb 14908 case POINTER_PLUS_EXPR:
ad46984d 14909 case PLUS_EXPR:
981eb798 14910 if (TYPE_OVERFLOW_UNDEFINED (type))
ad46984d 14911 {
14912 /* With the presence of negative values it is hard
14913 to say something. */
add6ee5e 14914 sub_strict_overflow_p = false;
937bceaf 14915 if (!tree_expr_nonnegative_warnv_p (op0,
add6ee5e 14916 &sub_strict_overflow_p)
937bceaf 14917 || !tree_expr_nonnegative_warnv_p (op1,
add6ee5e 14918 &sub_strict_overflow_p))
ad46984d 14919 return false;
14920 /* One of operands must be positive and the other non-negative. */
add6ee5e 14921 /* We don't set *STRICT_OVERFLOW_P here: even if this value
14922 overflows, on a twos-complement machine the sum of two
14923 nonnegative numbers can never be zero. */
937bceaf 14924 return (tree_expr_nonzero_warnv_p (op0,
add6ee5e 14925 strict_overflow_p)
937bceaf 14926 || tree_expr_nonzero_warnv_p (op1,
add6ee5e 14927 strict_overflow_p));
ad46984d 14928 }
14929 break;
14930
14931 case MULT_EXPR:
981eb798 14932 if (TYPE_OVERFLOW_UNDEFINED (type))
ad46984d 14933 {
937bceaf 14934 if (tree_expr_nonzero_warnv_p (op0,
add6ee5e 14935 strict_overflow_p)
937bceaf 14936 && tree_expr_nonzero_warnv_p (op1,
add6ee5e 14937 strict_overflow_p))
14938 {
14939 *strict_overflow_p = true;
14940 return true;
14941 }
ad46984d 14942 }
14943 break;
14944
937bceaf 14945 case MIN_EXPR:
14946 sub_strict_overflow_p = false;
14947 if (tree_expr_nonzero_warnv_p (op0,
14948 &sub_strict_overflow_p)
14949 && tree_expr_nonzero_warnv_p (op1,
14950 &sub_strict_overflow_p))
14951 {
14952 if (sub_strict_overflow_p)
14953 *strict_overflow_p = true;
14954 }
14955 break;
ad46984d 14956
937bceaf 14957 case MAX_EXPR:
14958 sub_strict_overflow_p = false;
14959 if (tree_expr_nonzero_warnv_p (op0,
14960 &sub_strict_overflow_p))
14961 {
14962 if (sub_strict_overflow_p)
14963 *strict_overflow_p = true;
14964
14965 /* When both operands are nonzero, then MAX must be too. */
14966 if (tree_expr_nonzero_warnv_p (op1,
14967 strict_overflow_p))
14968 return true;
14969
14970 /* MAX where operand 0 is positive is positive. */
14971 return tree_expr_nonnegative_warnv_p (op0,
14972 strict_overflow_p);
14973 }
14974 /* MAX where operand 1 is positive is positive. */
14975 else if (tree_expr_nonzero_warnv_p (op1,
14976 &sub_strict_overflow_p)
14977 && tree_expr_nonnegative_warnv_p (op1,
14978 &sub_strict_overflow_p))
14979 {
14980 if (sub_strict_overflow_p)
14981 *strict_overflow_p = true;
14982 return true;
14983 }
14984 break;
14985
14986 case BIT_IOR_EXPR:
14987 return (tree_expr_nonzero_warnv_p (op1,
14988 strict_overflow_p)
14989 || tree_expr_nonzero_warnv_p (op0,
14990 strict_overflow_p));
14991
14992 default:
ad46984d 14993 break;
937bceaf 14994 }
ad46984d 14995
937bceaf 14996 return false;
14997}
14998
14999/* Return true when T is an address and is known to be nonzero.
15000 For floating point we further ensure that T is not denormal.
15001 Similar logic is present in nonzero_address in rtlanal.h.
15002
15003 If the return value is based on the assumption that signed overflow
15004 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15005 change *STRICT_OVERFLOW_P. */
15006
93116081 15007bool
937bceaf 15008tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15009{
15010 bool sub_strict_overflow_p;
15011 switch (TREE_CODE (t))
15012 {
937bceaf 15013 case INTEGER_CST:
15014 return !integer_zerop (t);
15015
15016 case ADDR_EXPR:
3d1c55e6 15017 {
15018 tree base = get_base_address (TREE_OPERAND (t, 0));
15019
15020 if (!base)
15021 return false;
15022
0f921b42 15023 /* Weak declarations may link to NULL. Other things may also be NULL
15024 so protect with -fdelete-null-pointer-checks; but not variables
15025 allocated on the stack. */
15026 if (DECL_P (base)
15027 && (flag_delete_null_pointer_checks
15028 || (TREE_CODE (base) == VAR_DECL && !TREE_STATIC (base))))
dab963fb 15029 return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
3d1c55e6 15030
15031 /* Constants are never weak. */
ce45a448 15032 if (CONSTANT_CLASS_P (base))
3d1c55e6 15033 return true;
15034
15035 return false;
15036 }
ad46984d 15037
15038 case COND_EXPR:
add6ee5e 15039 sub_strict_overflow_p = false;
15040 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15041 &sub_strict_overflow_p)
15042 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15043 &sub_strict_overflow_p))
15044 {
15045 if (sub_strict_overflow_p)
15046 *strict_overflow_p = true;
15047 return true;
15048 }
15049 break;
ad46984d 15050
937bceaf 15051 default:
add6ee5e 15052 break;
937bceaf 15053 }
15054 return false;
15055}
ad46984d 15056
937bceaf 15057/* Return true when T is an address and is known to be nonzero.
15058 For floating point we further ensure that T is not denormal.
15059 Similar logic is present in nonzero_address in rtlanal.h.
add6ee5e 15060
937bceaf 15061 If the return value is based on the assumption that signed overflow
15062 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15063 change *STRICT_OVERFLOW_P. */
ad46984d 15064
937bceaf 15065bool
15066tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15067{
15068 tree type = TREE_TYPE (t);
15069 enum tree_code code;
15070
15071 /* Doing something useful for floating point would need more work. */
15072 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15073 return false;
15074
15075 code = TREE_CODE (t);
15076 switch (TREE_CODE_CLASS (code))
15077 {
15078 case tcc_unary:
15079 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15080 strict_overflow_p);
15081 case tcc_binary:
15082 case tcc_comparison:
15083 return tree_binary_nonzero_warnv_p (code, type,
15084 TREE_OPERAND (t, 0),
15085 TREE_OPERAND (t, 1),
add6ee5e 15086 strict_overflow_p);
937bceaf 15087 case tcc_constant:
15088 case tcc_declaration:
15089 case tcc_reference:
15090 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15091
15092 default:
ad46984d 15093 break;
937bceaf 15094 }
15095
15096 switch (code)
15097 {
15098 case TRUTH_NOT_EXPR:
15099 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15100 strict_overflow_p);
15101
15102 case TRUTH_AND_EXPR:
15103 case TRUTH_OR_EXPR:
15104 case TRUTH_XOR_EXPR:
15105 return tree_binary_nonzero_warnv_p (code, type,
15106 TREE_OPERAND (t, 0),
15107 TREE_OPERAND (t, 1),
15108 strict_overflow_p);
15109
15110 case COND_EXPR:
15111 case CONSTRUCTOR:
15112 case OBJ_TYPE_REF:
15113 case ASSERT_EXPR:
15114 case ADDR_EXPR:
15115 case WITH_SIZE_EXPR:
15116 case EXC_PTR_EXPR:
15117 case SSA_NAME:
15118 case FILTER_EXPR:
15119 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
ad46984d 15120
15121 case COMPOUND_EXPR:
15122 case MODIFY_EXPR:
15123 case BIND_EXPR:
75a70cf9 15124 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
add6ee5e 15125 strict_overflow_p);
ad46984d 15126
15127 case SAVE_EXPR:
add6ee5e 15128 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15129 strict_overflow_p);
ad46984d 15130
de452836 15131 case CALL_EXPR:
15132 return alloca_call_p (t);
15133
ad46984d 15134 default:
15135 break;
15136 }
15137 return false;
15138}
15139
add6ee5e 15140/* Return true when T is an address and is known to be nonzero.
15141 Handle warnings about undefined signed overflow. */
15142
15143bool
15144tree_expr_nonzero_p (tree t)
15145{
15146 bool ret, strict_overflow_p;
15147
15148 strict_overflow_p = false;
15149 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15150 if (strict_overflow_p)
15151 fold_overflow_warning (("assuming signed overflow does not occur when "
15152 "determining that expression is always "
15153 "non-zero"),
15154 WARN_STRICT_OVERFLOW_MISC);
15155 return ret;
15156}
15157
4ee9c684 15158/* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15159 attempt to fold the expression to a constant without modifying TYPE,
15160 OP0 or OP1.
15161
15162 If the expression could be simplified to a constant, then return
15163 the constant. If the expression would not be simplified to a
f80ab0b5 15164 constant, then return NULL_TREE. */
4ee9c684 15165
15166tree
c1677a03 15167fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
4ee9c684 15168{
9a4edb3d 15169 tree tem = fold_binary (code, type, op0, op1);
15170 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
4ee9c684 15171}
15172
15173/* Given the components of a unary expression CODE, TYPE and OP0,
15174 attempt to fold the expression to a constant without modifying
7206da1b 15175 TYPE or OP0.
4ee9c684 15176
15177 If the expression could be simplified to a constant, then return
15178 the constant. If the expression would not be simplified to a
f80ab0b5 15179 constant, then return NULL_TREE. */
4ee9c684 15180
15181tree
c1677a03 15182fold_unary_to_constant (enum tree_code code, tree type, tree op0)
4ee9c684 15183{
9a4edb3d 15184 tree tem = fold_unary (code, type, op0);
15185 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
4ee9c684 15186}
15187
15188/* If EXP represents referencing an element in a constant string
15189 (either via pointer arithmetic or array indexing), return the
15190 tree representing the value accessed, otherwise return NULL. */
15191
15192tree
15193fold_read_from_constant_string (tree exp)
15194{
dd824901 15195 if ((TREE_CODE (exp) == INDIRECT_REF
15196 || TREE_CODE (exp) == ARRAY_REF)
15197 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
4ee9c684 15198 {
15199 tree exp1 = TREE_OPERAND (exp, 0);
15200 tree index;
15201 tree string;
15202
15203 if (TREE_CODE (exp) == INDIRECT_REF)
6374121b 15204 string = string_constant (exp1, &index);
4ee9c684 15205 else
15206 {
6374121b 15207 tree low_bound = array_ref_low_bound (exp);
c0c67e38 15208 index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
7206da1b 15209
4ee9c684 15210 /* Optimize the special-case of a zero lower bound.
15211
15212 We convert the low_bound to sizetype to avoid some problems
15213 with constant folding. (E.g. suppose the lower bound is 1,
15214 and its mode is QI. Without the conversion,l (ARRAY
15215 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
f0b5f617 15216 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
4ee9c684 15217 if (! integer_zerop (low_bound))
c0c67e38 15218 index = size_diffop (index, fold_convert (sizetype, low_bound));
4ee9c684 15219
15220 string = exp1;
15221 }
15222
15223 if (string
b3acd74f 15224 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
4ee9c684 15225 && TREE_CODE (string) == STRING_CST
15226 && TREE_CODE (index) == INTEGER_CST
15227 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15228 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15229 == MODE_INT)
15230 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
7b050b7b 15231 return build_int_cst_type (TREE_TYPE (exp),
15232 (TREE_STRING_POINTER (string)
15233 [TREE_INT_CST_LOW (index)]));
4ee9c684 15234 }
15235 return NULL;
15236}
15237
9d77437d 15238/* Return the tree for neg (ARG0) when ARG0 is known to be either
06f0b99c 15239 an integer constant, real, or fixed-point constant.
9d77437d 15240
15241 TYPE is the type of the result. */
15242
15243static tree
15244fold_negate_const (tree arg0, tree type)
15245{
15246 tree t = NULL_TREE;
15247
fdada98f 15248 switch (TREE_CODE (arg0))
9d77437d 15249 {
fdada98f 15250 case INTEGER_CST:
15251 {
15252 unsigned HOST_WIDE_INT low;
15253 HOST_WIDE_INT high;
15254 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15255 TREE_INT_CST_HIGH (arg0),
15256 &low, &high);
c8110c8f 15257 t = force_fit_type_double (type, low, high, 1,
15258 (overflow | TREE_OVERFLOW (arg0))
eddad94a 15259 && !TYPE_UNSIGNED (type));
fdada98f 15260 break;
15261 }
0c5713a2 15262
fdada98f 15263 case REAL_CST:
15264 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15265 break;
7206da1b 15266
06f0b99c 15267 case FIXED_CST:
15268 {
15269 FIXED_VALUE_TYPE f;
15270 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15271 &(TREE_FIXED_CST (arg0)), NULL,
15272 TYPE_SATURATING (type));
15273 t = build_fixed (type, f);
15274 /* Propagate overflow flags. */
15275 if (overflow_p | TREE_OVERFLOW (arg0))
62126877 15276 TREE_OVERFLOW (t) = 1;
06f0b99c 15277 break;
15278 }
15279
fdada98f 15280 default:
15281 gcc_unreachable ();
15282 }
0c5713a2 15283
9d77437d 15284 return t;
15285}
15286
5221d284 15287/* Return the tree for abs (ARG0) when ARG0 is known to be either
15288 an integer constant or real constant.
15289
15290 TYPE is the type of the result. */
15291
d1aade50 15292tree
5221d284 15293fold_abs_const (tree arg0, tree type)
15294{
15295 tree t = NULL_TREE;
15296
fdada98f 15297 switch (TREE_CODE (arg0))
5221d284 15298 {
fdada98f 15299 case INTEGER_CST:
5221d284 15300 /* If the value is unsigned, then the absolute value is
15301 the same as the ordinary value. */
78a8ed03 15302 if (TYPE_UNSIGNED (type))
fdada98f 15303 t = arg0;
5221d284 15304 /* Similarly, if the value is non-negative. */
15305 else if (INT_CST_LT (integer_minus_one_node, arg0))
fdada98f 15306 t = arg0;
5221d284 15307 /* If the value is negative, then the absolute value is
15308 its negation. */
15309 else
15310 {
15311 unsigned HOST_WIDE_INT low;
15312 HOST_WIDE_INT high;
15313 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15314 TREE_INT_CST_HIGH (arg0),
15315 &low, &high);
c8110c8f 15316 t = force_fit_type_double (type, low, high, -1,
eddad94a 15317 overflow | TREE_OVERFLOW (arg0));
5221d284 15318 }
fdada98f 15319 break;
0c5713a2 15320
fdada98f 15321 case REAL_CST:
5221d284 15322 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
fdada98f 15323 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5221d284 15324 else
fdada98f 15325 t = arg0;
15326 break;
0c5713a2 15327
fdada98f 15328 default:
15329 gcc_unreachable ();
5221d284 15330 }
0c5713a2 15331
5221d284 15332 return t;
15333}
15334
c183306c 15335/* Return the tree for not (ARG0) when ARG0 is known to be an integer
15336 constant. TYPE is the type of the result. */
15337
15338static tree
15339fold_not_const (tree arg0, tree type)
15340{
15341 tree t = NULL_TREE;
15342
fdada98f 15343 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
0c5713a2 15344
c8110c8f 15345 t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15346 ~TREE_INT_CST_HIGH (arg0), 0,
eddad94a 15347 TREE_OVERFLOW (arg0));
0c5713a2 15348
c183306c 15349 return t;
15350}
15351
ad46984d 15352/* Given CODE, a relational operator, the target type, TYPE and two
15353 constant operands OP0 and OP1, return the result of the
15354 relational operation. If the result is not a compile time
15355 constant, then return NULL_TREE. */
15356
15357static tree
15358fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15359{
39d4c6de 15360 int result, invert;
ad46984d 15361
15362 /* From here on, the only cases we handle are when the result is
2f64c430 15363 known to be a constant. */
15364
15365 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15366 {
990af12c 15367 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15368 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15369
2f64c430 15370 /* Handle the cases where either operand is a NaN. */
990af12c 15371 if (real_isnan (c0) || real_isnan (c1))
2f64c430 15372 {
15373 switch (code)
15374 {
15375 case EQ_EXPR:
15376 case ORDERED_EXPR:
15377 result = 0;
15378 break;
15379
15380 case NE_EXPR:
15381 case UNORDERED_EXPR:
15382 case UNLT_EXPR:
15383 case UNLE_EXPR:
15384 case UNGT_EXPR:
15385 case UNGE_EXPR:
15386 case UNEQ_EXPR:
15387 result = 1;
15388 break;
15389
15390 case LT_EXPR:
15391 case LE_EXPR:
15392 case GT_EXPR:
15393 case GE_EXPR:
15394 case LTGT_EXPR:
15395 if (flag_trapping_math)
15396 return NULL_TREE;
15397 result = 0;
15398 break;
15399
15400 default:
fdada98f 15401 gcc_unreachable ();
2f64c430 15402 }
15403
15404 return constant_boolean_node (result, type);
15405 }
15406
990af12c 15407 return constant_boolean_node (real_compare (code, c0, c1), type);
2f64c430 15408 }
15409
06f0b99c 15410 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15411 {
15412 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15413 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15414 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15415 }
15416
5dbcb7c4 15417 /* Handle equality/inequality of complex constants. */
15418 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15419 {
15420 tree rcond = fold_relational_const (code, type,
15421 TREE_REALPART (op0),
15422 TREE_REALPART (op1));
15423 tree icond = fold_relational_const (code, type,
15424 TREE_IMAGPART (op0),
15425 TREE_IMAGPART (op1));
15426 if (code == EQ_EXPR)
15427 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15428 else if (code == NE_EXPR)
15429 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15430 else
15431 return NULL_TREE;
15432 }
15433
2f64c430 15434 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
ad46984d 15435
15436 To compute GT, swap the arguments and do LT.
15437 To compute GE, do LT and invert the result.
15438 To compute LE, swap the arguments, do LT and invert the result.
15439 To compute NE, do EQ and invert the result.
15440
15441 Therefore, the code below must handle only EQ and LT. */
15442
15443 if (code == LE_EXPR || code == GT_EXPR)
15444 {
39d4c6de 15445 tree tem = op0;
15446 op0 = op1;
15447 op1 = tem;
ad46984d 15448 code = swap_tree_comparison (code);
15449 }
15450
15451 /* Note that it is safe to invert for real values here because we
2f64c430 15452 have already handled the one case that it matters. */
ad46984d 15453
ad46984d 15454 invert = 0;
15455 if (code == NE_EXPR || code == GE_EXPR)
15456 {
15457 invert = 1;
318a728f 15458 code = invert_tree_comparison (code, false);
ad46984d 15459 }
15460
15461 /* Compute a result for LT or EQ if args permit;
15462 Otherwise return T. */
15463 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15464 {
15465 if (code == EQ_EXPR)
39d4c6de 15466 result = tree_int_cst_equal (op0, op1);
15467 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15468 result = INT_CST_LT_UNSIGNED (op0, op1);
ad46984d 15469 else
39d4c6de 15470 result = INT_CST_LT (op0, op1);
ad46984d 15471 }
39d4c6de 15472 else
ad46984d 15473 return NULL_TREE;
15474
15475 if (invert)
39d4c6de 15476 result ^= 1;
15477 return constant_boolean_node (result, type);
ad46984d 15478}
15479
098a01e7 15480/* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15481 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15482 itself. */
acbc760a 15483
15484tree
15485fold_build_cleanup_point_expr (tree type, tree expr)
15486{
15487 /* If the expression does not have side effects then we don't have to wrap
15488 it with a cleanup point expression. */
15489 if (!TREE_SIDE_EFFECTS (expr))
15490 return expr;
b997b4b2 15491
15492 /* If the expression is a return, check to see if the expression inside the
15493 return has no side effects or the right hand side of the modify expression
15494 inside the return. If either don't have side effects set we don't need to
15495 wrap the expression in a cleanup point expression. Note we don't check the
15496 left hand side of the modify because it should always be a return decl. */
15497 if (TREE_CODE (expr) == RETURN_EXPR)
15498 {
15499 tree op = TREE_OPERAND (expr, 0);
15500 if (!op || !TREE_SIDE_EFFECTS (op))
15501 return expr;
15502 op = TREE_OPERAND (op, 1);
15503 if (!TREE_SIDE_EFFECTS (op))
15504 return expr;
15505 }
acbc760a 15506
15507 return build1 (CLEANUP_POINT_EXPR, type, expr);
15508}
15509
51fc648f 15510/* Given a pointer value OP0 and a type TYPE, return a simplified version
15511 of an indirection through OP0, or NULL_TREE if no simplification is
15512 possible. */
fcdd3ab3 15513
9ea9fcf0 15514tree
51fc648f 15515fold_indirect_ref_1 (tree type, tree op0)
fcdd3ab3 15516{
51fc648f 15517 tree sub = op0;
fcdd3ab3 15518 tree subtype;
15519
e7b5ce74 15520 STRIP_NOPS (sub);
1928904f 15521 subtype = TREE_TYPE (sub);
15522 if (!POINTER_TYPE_P (subtype))
15523 return NULL_TREE;
15524
fcdd3ab3 15525 if (TREE_CODE (sub) == ADDR_EXPR)
15526 {
15527 tree op = TREE_OPERAND (sub, 0);
15528 tree optype = TREE_TYPE (op);
2350f40c 15529 /* *&CONST_DECL -> to the value of the const decl. */
15530 if (TREE_CODE (op) == CONST_DECL)
15531 return DECL_INITIAL (op);
85ca5610 15532 /* *&p => p; make sure to handle *&"str"[cst] here. */
51fc648f 15533 if (type == optype)
85ca5610 15534 {
15535 tree fop = fold_read_from_constant_string (op);
15536 if (fop)
15537 return fop;
15538 else
15539 return op;
15540 }
fcdd3ab3 15541 /* *(foo *)&fooarray => fooarray[0] */
15542 else if (TREE_CODE (optype) == ARRAY_TYPE
51fc648f 15543 && type == TREE_TYPE (optype))
15274a5a 15544 {
15545 tree type_domain = TYPE_DOMAIN (optype);
15546 tree min_val = size_zero_node;
15547 if (type_domain && TYPE_MIN_VALUE (type_domain))
15548 min_val = TYPE_MIN_VALUE (type_domain);
15549 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15550 }
1e4039e0 15551 /* *(foo *)&complexfoo => __real__ complexfoo */
15552 else if (TREE_CODE (optype) == COMPLEX_TYPE
15553 && type == TREE_TYPE (optype))
15554 return fold_build1 (REALPART_EXPR, type, op);
8ea8de24 15555 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15556 else if (TREE_CODE (optype) == VECTOR_TYPE
15557 && type == TREE_TYPE (optype))
15558 {
15559 tree part_width = TYPE_SIZE (type);
15560 tree index = bitsize_int (0);
15561 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
15562 }
fcdd3ab3 15563 }
15564
70a866cf 15565 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15566 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15567 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15568 {
15569 tree op00 = TREE_OPERAND (sub, 0);
15570 tree op01 = TREE_OPERAND (sub, 1);
15571 tree op00type;
15572
15573 STRIP_NOPS (op00);
15574 op00type = TREE_TYPE (op00);
15575 if (TREE_CODE (op00) == ADDR_EXPR
15576 && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15577 && type == TREE_TYPE (TREE_TYPE (op00type)))
15578 {
15579 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15580 tree part_width = TYPE_SIZE (type);
15581 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15582 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15583 tree index = bitsize_int (indexi);
15584
15585 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15586 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15587 part_width, index);
15588
15589 }
15590 }
15591
15592
1e4039e0 15593 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
0de36bdb 15594 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
1e4039e0 15595 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15596 {
15597 tree op00 = TREE_OPERAND (sub, 0);
15598 tree op01 = TREE_OPERAND (sub, 1);
15599 tree op00type;
15600
15601 STRIP_NOPS (op00);
15602 op00type = TREE_TYPE (op00);
15603 if (TREE_CODE (op00) == ADDR_EXPR
15604 && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15605 && type == TREE_TYPE (TREE_TYPE (op00type)))
15606 {
15607 tree size = TYPE_SIZE_UNIT (type);
15608 if (tree_int_cst_equal (size, op01))
15609 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
15610 }
15611 }
15612
fcdd3ab3 15613 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
fcdd3ab3 15614 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
51fc648f 15615 && type == TREE_TYPE (TREE_TYPE (subtype)))
fcdd3ab3 15616 {
15274a5a 15617 tree type_domain;
15618 tree min_val = size_zero_node;
fcdd3ab3 15619 sub = build_fold_indirect_ref (sub);
15274a5a 15620 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15621 if (type_domain && TYPE_MIN_VALUE (type_domain))
15622 min_val = TYPE_MIN_VALUE (type_domain);
15623 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
fcdd3ab3 15624 }
15625
1928904f 15626 return NULL_TREE;
15627}
15628
15629/* Builds an expression for an indirection through T, simplifying some
15630 cases. */
15631
15632tree
15633build_fold_indirect_ref (tree t)
15634{
51fc648f 15635 tree type = TREE_TYPE (TREE_TYPE (t));
15636 tree sub = fold_indirect_ref_1 (type, t);
1928904f 15637
15638 if (sub)
15639 return sub;
15640 else
51fc648f 15641 return build1 (INDIRECT_REF, type, t);
1928904f 15642}
15643
15644/* Given an INDIRECT_REF T, return either T or a simplified version. */
15645
15646tree
15647fold_indirect_ref (tree t)
15648{
51fc648f 15649 tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
1928904f 15650
15651 if (sub)
15652 return sub;
15653 else
15654 return t;
fcdd3ab3 15655}
15656
db97ad41 15657/* Strip non-trapping, non-side-effecting tree nodes from an expression
15658 whose result is ignored. The type of the returned tree need not be
15659 the same as the original expression. */
15660
15661tree
15662fold_ignored_result (tree t)
15663{
15664 if (!TREE_SIDE_EFFECTS (t))
15665 return integer_zero_node;
15666
15667 for (;;)
15668 switch (TREE_CODE_CLASS (TREE_CODE (t)))
15669 {
ce45a448 15670 case tcc_unary:
db97ad41 15671 t = TREE_OPERAND (t, 0);
15672 break;
15673
ce45a448 15674 case tcc_binary:
15675 case tcc_comparison:
db97ad41 15676 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15677 t = TREE_OPERAND (t, 0);
15678 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15679 t = TREE_OPERAND (t, 1);
15680 else
15681 return t;
15682 break;
15683
ce45a448 15684 case tcc_expression:
db97ad41 15685 switch (TREE_CODE (t))
15686 {
15687 case COMPOUND_EXPR:
15688 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15689 return t;
15690 t = TREE_OPERAND (t, 0);
15691 break;
15692
15693 case COND_EXPR:
15694 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15695 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15696 return t;
15697 t = TREE_OPERAND (t, 0);
15698 break;
15699
15700 default:
15701 return t;
15702 }
15703 break;
15704
15705 default:
15706 return t;
15707 }
15708}
15709
59ff7b6e 15710/* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15711 This can only be applied to objects of a sizetype. */
15712
15713tree
15714round_up (tree value, int divisor)
15715{
cda13ce3 15716 tree div = NULL_TREE;
59ff7b6e 15717
fdada98f 15718 gcc_assert (divisor > 0);
59ff7b6e 15719 if (divisor == 1)
15720 return value;
15721
59ff7b6e 15722 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
cda13ce3 15723 have to do anything. Only do this when we are not given a const,
15724 because in that case, this check is more expensive than just
fbf0afd1 15725 doing it. */
cda13ce3 15726 if (TREE_CODE (value) != INTEGER_CST)
15727 {
85390276 15728 div = build_int_cst (TREE_TYPE (value), divisor);
cda13ce3 15729
15730 if (multiple_of_p (TREE_TYPE (value), value, div))
15731 return value;
15732 }
59ff7b6e 15733
15734 /* If divisor is a power of two, simplify this to bit manipulation. */
15735 if (divisor == (divisor & -divisor))
15736 {
6117e415 15737 if (TREE_CODE (value) == INTEGER_CST)
15738 {
15739 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
816f7496 15740 unsigned HOST_WIDE_INT high;
15741 bool overflow_p;
6117e415 15742
15743 if ((low & (divisor - 1)) == 0)
15744 return value;
15745
816f7496 15746 overflow_p = TREE_OVERFLOW (value);
6117e415 15747 high = TREE_INT_CST_HIGH (value);
15748 low &= ~(divisor - 1);
15749 low += divisor;
15750 if (low == 0)
6117e415 15751 {
816f7496 15752 high++;
15753 if (high == 0)
15754 overflow_p = true;
6117e415 15755 }
816f7496 15756
15757 return force_fit_type_double (TREE_TYPE (value), low, high,
15758 -1, overflow_p);
6117e415 15759 }
15760 else
15761 {
816f7496 15762 tree t;
15763
6117e415 15764 t = build_int_cst (TREE_TYPE (value), divisor - 1);
15765 value = size_binop (PLUS_EXPR, value, t);
15766 t = build_int_cst (TREE_TYPE (value), -divisor);
15767 value = size_binop (BIT_AND_EXPR, value, t);
15768 }
59ff7b6e 15769 }
15770 else
15771 {
cda13ce3 15772 if (!div)
85390276 15773 div = build_int_cst (TREE_TYPE (value), divisor);
59ff7b6e 15774 value = size_binop (CEIL_DIV_EXPR, value, div);
15775 value = size_binop (MULT_EXPR, value, div);
15776 }
15777
15778 return value;
15779}
15780
15781/* Likewise, but round down. */
15782
15783tree
15784round_down (tree value, int divisor)
15785{
cda13ce3 15786 tree div = NULL_TREE;
59ff7b6e 15787
fdada98f 15788 gcc_assert (divisor > 0);
59ff7b6e 15789 if (divisor == 1)
15790 return value;
15791
59ff7b6e 15792 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
cda13ce3 15793 have to do anything. Only do this when we are not given a const,
15794 because in that case, this check is more expensive than just
fbf0afd1 15795 doing it. */
cda13ce3 15796 if (TREE_CODE (value) != INTEGER_CST)
15797 {
85390276 15798 div = build_int_cst (TREE_TYPE (value), divisor);
cda13ce3 15799
15800 if (multiple_of_p (TREE_TYPE (value), value, div))
15801 return value;
15802 }
59ff7b6e 15803
15804 /* If divisor is a power of two, simplify this to bit manipulation. */
15805 if (divisor == (divisor & -divisor))
15806 {
cda13ce3 15807 tree t;
0c5713a2 15808
7016c612 15809 t = build_int_cst (TREE_TYPE (value), -divisor);
59ff7b6e 15810 value = size_binop (BIT_AND_EXPR, value, t);
15811 }
15812 else
15813 {
cda13ce3 15814 if (!div)
85390276 15815 div = build_int_cst (TREE_TYPE (value), divisor);
59ff7b6e 15816 value = size_binop (FLOOR_DIV_EXPR, value, div);
15817 value = size_binop (MULT_EXPR, value, div);
15818 }
15819
15820 return value;
15821}
dbc64c75 15822
eb91f88e 15823/* Returns the pointer to the base of the object addressed by EXP and
15824 extracts the information about the offset of the access, storing it
15825 to PBITPOS and POFFSET. */
15826
15827static tree
15828split_address_to_core_and_offset (tree exp,
15829 HOST_WIDE_INT *pbitpos, tree *poffset)
15830{
15831 tree core;
15832 enum machine_mode mode;
15833 int unsignedp, volatilep;
15834 HOST_WIDE_INT bitsize;
15835
15836 if (TREE_CODE (exp) == ADDR_EXPR)
15837 {
15838 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
e7e9416e 15839 poffset, &mode, &unsignedp, &volatilep,
15840 false);
86f2ad37 15841 core = build_fold_addr_expr (core);
eb91f88e 15842 }
15843 else
15844 {
15845 core = exp;
15846 *pbitpos = 0;
15847 *poffset = NULL_TREE;
15848 }
15849
15850 return core;
15851}
15852
dbc64c75 15853/* Returns true if addresses of E1 and E2 differ by a constant, false
eb91f88e 15854 otherwise. If they do, E1 - E2 is stored in *DIFF. */
dbc64c75 15855
15856bool
15857ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15858{
15859 tree core1, core2;
dbc64c75 15860 HOST_WIDE_INT bitpos1, bitpos2;
15861 tree toffset1, toffset2, tdiff, type;
0c5713a2 15862
eb91f88e 15863 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15864 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
dbc64c75 15865
15866 if (bitpos1 % BITS_PER_UNIT != 0
15867 || bitpos2 % BITS_PER_UNIT != 0
15868 || !operand_equal_p (core1, core2, 0))
15869 return false;
15870
15871 if (toffset1 && toffset2)
15872 {
15873 type = TREE_TYPE (toffset1);
15874 if (type != TREE_TYPE (toffset2))
15875 toffset2 = fold_convert (type, toffset2);
15876
7ab7fd4f 15877 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
4e359230 15878 if (!cst_and_fits_in_hwi (tdiff))
dbc64c75 15879 return false;
15880
4e359230 15881 *diff = int_cst_value (tdiff);
dbc64c75 15882 }
15883 else if (toffset1 || toffset2)
15884 {
15885 /* If only one of the offsets is non-constant, the difference cannot
15886 be a constant. */
15887 return false;
15888 }
15889 else
15890 *diff = 0;
15891
15892 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15893 return true;
15894}
198d9bbe 15895
15896/* Simplify the floating point expression EXP when the sign of the
15897 result is not significant. Return NULL_TREE if no simplification
15898 is possible. */
15899
15900tree
15901fold_strip_sign_ops (tree exp)
15902{
15903 tree arg0, arg1;
15904
15905 switch (TREE_CODE (exp))
15906 {
15907 case ABS_EXPR:
15908 case NEGATE_EXPR:
15909 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15910 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15911
15912 case MULT_EXPR:
15913 case RDIV_EXPR:
15914 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15915 return NULL_TREE;
15916 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15917 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15918 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
7ab7fd4f 15919 return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15920 arg0 ? arg0 : TREE_OPERAND (exp, 0),
15921 arg1 ? arg1 : TREE_OPERAND (exp, 1));
198d9bbe 15922 break;
15923
2c26730b 15924 case COMPOUND_EXPR:
15925 arg0 = TREE_OPERAND (exp, 0);
15926 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15927 if (arg1)
15928 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15929 break;
15930
15931 case COND_EXPR:
15932 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15933 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15934 if (arg0 || arg1)
15935 return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15936 arg0 ? arg0 : TREE_OPERAND (exp, 1),
15937 arg1 ? arg1 : TREE_OPERAND (exp, 2));
15938 break;
15939
e6ab33d8 15940 case CALL_EXPR:
b145cb59 15941 {
15942 const enum built_in_function fcode = builtin_mathfn_code (exp);
15943 switch (fcode)
15944 {
15945 CASE_FLT_FN (BUILT_IN_COPYSIGN):
15946 /* Strip copysign function call, return the 1st argument. */
c2f47e15 15947 arg0 = CALL_EXPR_ARG (exp, 0);
15948 arg1 = CALL_EXPR_ARG (exp, 1);
b145cb59 15949 return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15950
15951 default:
15952 /* Strip sign ops from the argument of "odd" math functions. */
15953 if (negate_mathfn_p (fcode))
15954 {
c2f47e15 15955 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
b145cb59 15956 if (arg0)
c2f47e15 15957 return build_call_expr (get_callee_fndecl (exp), 1, arg0);
b145cb59 15958 }
15959 break;
e6ab33d8 15960 }
b145cb59 15961 }
e6ab33d8 15962 break;
15963
198d9bbe 15964 default:
15965 break;
15966 }
15967 return NULL_TREE;
15968}