]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fold-const.c
configure.ac: Check for .gnu_attribute on Power.
[thirdparty/gcc.git] / gcc / fold-const.c
CommitLineData
6d716ca8 1/* Fold a constant sub-tree into a single node for C-compiler
080ea642 2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
d95787e6
RS
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6d716ca8 5
1322177d 6This file is part of GCC.
6d716ca8 7
1322177d
LB
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
10Software Foundation; either version 2, or (at your option) any later
11version.
6d716ca8 12
1322177d
LB
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.
6d716ca8
RS
17
18You should have received a copy of the GNU General Public License
1322177d 19along with GCC; see the file COPYING. If not, write to the Free
366ccddb
KC
20Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2102110-1301, USA. */
6d716ca8 22
6dc42e49 23/*@@ This file should be rewritten to use an arbitrary precision
6d716ca8
RS
24 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
25 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
26 @@ The routines that translate from the ap rep should
27 @@ warn if precision et. al. is lost.
28 @@ This would also make life easier when this technology is used
29 @@ for cross-compilers. */
30
f8dac6eb 31/* The entry points in this file are fold, size_int_wide, size_binop
d95787e6 32 and force_fit_type_double.
6d716ca8
RS
33
34 fold takes a tree as argument and returns a simplified tree.
35
36 size_binop takes a tree code for an arithmetic operation
37 and two operands that are trees, and produces a tree for the
38 result, assuming the type comes from `sizetype'.
39
40 size_int takes an integer value, and creates a tree constant
0da6f3db
DE
41 with type from `sizetype'.
42
d95787e6
RS
43 force_fit_type_double takes a constant, an overflowable flag and a
44 prior overflow indicator. It forces the value to fit the type and
45 sets TREE_OVERFLOW.
46
07beea0d
AH
47 Note: Since the folders get called on non-gimple code as well as
48 gimple code, we need to handle GIMPLE tuples as well as their
49 corresponding tree equivalents. */
0da6f3db 50
e9a25f70 51#include "config.h"
2fde567e 52#include "system.h"
4977bab6
ZW
53#include "coretypes.h"
54#include "tm.h"
6d716ca8
RS
55#include "flags.h"
56#include "tree.h"
11ad4784 57#include "real.h"
efe3eb65 58#include "rtl.h"
0e9295cf 59#include "expr.h"
6baf1cc8 60#include "tm_p.h"
10f0ad3d 61#include "toplev.h"
6ac01510 62#include "intl.h"
a3770a81 63#include "ggc.h"
4c160717 64#include "hashtab.h"
43577e6b 65#include "langhooks.h"
5dfa45d0 66#include "md5.h"
6d716ca8 67
110abdbc 68/* Nonzero if we are folding constants inside an initializer; zero
63b48197
MS
69 otherwise. */
70int folding_initializer = 0;
71
d1a7edaf
PB
72/* The following constants represent a bit based encoding of GCC's
73 comparison operators. This encoding simplifies transformations
74 on relational comparison operators, such as AND and OR. */
75enum comparison_code {
76 COMPCODE_FALSE = 0,
77 COMPCODE_LT = 1,
78 COMPCODE_EQ = 2,
79 COMPCODE_LE = 3,
80 COMPCODE_GT = 4,
81 COMPCODE_LTGT = 5,
82 COMPCODE_GE = 6,
83 COMPCODE_ORD = 7,
84 COMPCODE_UNORD = 8,
85 COMPCODE_UNLT = 9,
86 COMPCODE_UNEQ = 10,
87 COMPCODE_UNLE = 11,
88 COMPCODE_UNGT = 12,
89 COMPCODE_NE = 13,
90 COMPCODE_UNGE = 14,
91 COMPCODE_TRUE = 15
92};
93
fa8db1f7
AJ
94static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
95static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
05d362b8 96static bool negate_mathfn_p (enum built_in_function);
fa8db1f7
AJ
97static bool negate_expr_p (tree);
98static tree negate_expr (tree);
99static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
100static tree associate_trees (tree, tree, enum tree_code, tree);
fa8db1f7 101static tree const_binop (enum tree_code, tree, tree, int);
d1a7edaf
PB
102static enum comparison_code comparison_to_compcode (enum tree_code);
103static enum tree_code compcode_to_comparison (enum comparison_code);
104static tree combine_comparisons (enum tree_code, enum tree_code,
105 enum tree_code, tree, tree, tree);
fa8db1f7
AJ
106static int truth_value_p (enum tree_code);
107static int operand_equal_for_comparison_p (tree, tree, tree);
108static int twoval_comparison_p (tree, tree *, tree *, int *);
109static tree eval_subst (tree, tree, tree, tree, tree);
110static tree pedantic_omit_one_operand (tree, tree, tree);
111static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
112static tree make_bit_field_ref (tree, tree, int, int, int);
113static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
114static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
115 enum machine_mode *, int *, int *,
116 tree *, tree *);
117static int all_ones_mask_p (tree, int);
118static tree sign_bit_p (tree, tree);
119static int simple_operand_p (tree);
120static tree range_binop (enum tree_code, tree, tree, int, tree, int);
f8fe0545
EB
121static tree range_predecessor (tree);
122static tree range_successor (tree);
6ac01510 123static tree make_range (tree, int *, tree *, tree *, bool *);
fa8db1f7
AJ
124static tree build_range_check (tree, tree, int, tree, tree);
125static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
126 tree);
e1f04615 127static tree fold_range_test (enum tree_code, tree, tree, tree);
2851dd68 128static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
fa8db1f7
AJ
129static tree unextend (tree, int, int, tree);
130static tree fold_truthop (enum tree_code, tree, tree, tree);
d7e5b287 131static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
6ac01510
ILT
132static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
133static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
e9da788c
KH
134static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
135 tree, tree,
3b70b82a 136 tree, tree, int);
fa8db1f7
AJ
137static bool fold_real_zero_addition_p (tree, tree, int);
138static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
139 tree, tree, tree);
140static tree fold_inf_compare (enum tree_code, tree, tree, tree);
8dc2384c 141static tree fold_div_compare (enum tree_code, tree, tree, tree);
05d362b8 142static bool reorder_operands_p (tree, tree);
33d13fac 143static tree fold_negate_const (tree, tree);
a653e758 144static tree fold_not_const (tree, tree);
8e7b3a43 145static tree fold_relational_const (enum tree_code, tree, tree, tree);
78bf6e2f 146
33d13fac 147
d4b60170
RK
148/* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
149 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
150 and SUM1. Then this yields nonzero if overflow occurred during the
151 addition.
152
153 Overflow occurs if A and B have the same sign, but A and SUM differ in
154 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
155 sign. */
156#define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
6d716ca8 157\f
906c4e36 158/* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
37bdb7e3 159 We do that by representing the two-word integer in 4 words, with only
d4b60170
RK
160 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
161 number. The value of the word is LOWPART + HIGHPART * BASE. */
37bdb7e3
TG
162
163#define LOWPART(x) \
d4b60170 164 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
37bdb7e3 165#define HIGHPART(x) \
d4b60170
RK
166 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
167#define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
6d716ca8 168
37bdb7e3 169/* Unpack a two-word integer into 4 words.
906c4e36 170 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
37bdb7e3 171 WORDS points to the array of HOST_WIDE_INTs. */
6d716ca8
RS
172
173static void
fa8db1f7 174encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
6d716ca8 175{
37bdb7e3
TG
176 words[0] = LOWPART (low);
177 words[1] = HIGHPART (low);
178 words[2] = LOWPART (hi);
179 words[3] = HIGHPART (hi);
6d716ca8
RS
180}
181
37bdb7e3
TG
182/* Pack an array of 4 words into a two-word integer.
183 WORDS points to the array of words.
906c4e36 184 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
6d716ca8
RS
185
186static void
75040a04
AJ
187decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
188 HOST_WIDE_INT *hi)
6d716ca8 189{
d4b60170
RK
190 *low = words[0] + words[1] * BASE;
191 *hi = words[2] + words[3] * BASE;
6d716ca8
RS
192}
193\f
2b60792f
RG
194/* Force the double-word integer L1, H1 to be within the range of the
195 integer type TYPE. Stores the properly truncated and sign-extended
196 double-word integer in *LV, *HV. Returns true if the operation
197 overflows, that is, argument and result are different. */
198
199int
200fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
201 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, tree type)
6d716ca8 202{
2b60792f
RG
203 unsigned HOST_WIDE_INT low0 = l1;
204 HOST_WIDE_INT high0 = h1;
05bccae2 205 unsigned int prec;
ca7a3bd7 206 int sign_extended_type;
6d716ca8 207
2b60792f
RG
208 if (POINTER_TYPE_P (type)
209 || TREE_CODE (type) == OFFSET_TYPE)
6d716ca8 210 prec = POINTER_SIZE;
ef2bf0c0 211 else
2b60792f
RG
212 prec = TYPE_PRECISION (type);
213
ca7a3bd7 214 /* Size types *are* sign extended. */
2b60792f
RG
215 sign_extended_type = (!TYPE_UNSIGNED (type)
216 || (TREE_CODE (type) == INTEGER_TYPE
217 && TYPE_IS_SIZETYPE (type)));
6d716ca8
RS
218
219 /* First clear all bits that are beyond the type's precision. */
c756af79 220 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
6d716ca8 221 ;
906c4e36 222 else if (prec > HOST_BITS_PER_WIDE_INT)
2b60792f 223 h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
6d716ca8
RS
224 else
225 {
2b60792f 226 h1 = 0;
906c4e36 227 if (prec < HOST_BITS_PER_WIDE_INT)
2b60792f 228 l1 &= ~((HOST_WIDE_INT) (-1) << prec);
ca7a3bd7
NS
229 }
230
2b60792f 231 /* Then do sign extension if necessary. */
ca7a3bd7
NS
232 if (!sign_extended_type)
233 /* No sign extension */;
c756af79 234 else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
ca7a3bd7
NS
235 /* Correct width already. */;
236 else if (prec > HOST_BITS_PER_WIDE_INT)
237 {
238 /* Sign extend top half? */
2b60792f
RG
239 if (h1 & ((unsigned HOST_WIDE_INT)1
240 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
241 h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
ca7a3bd7
NS
242 }
243 else if (prec == HOST_BITS_PER_WIDE_INT)
244 {
2b60792f
RG
245 if ((HOST_WIDE_INT)l1 < 0)
246 h1 = -1;
ca7a3bd7
NS
247 }
248 else
249 {
250 /* Sign extend bottom half? */
2b60792f 251 if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
6d716ca8 252 {
2b60792f
RG
253 h1 = -1;
254 l1 |= (HOST_WIDE_INT)(-1) << prec;
6d716ca8
RS
255 }
256 }
e0f776fb 257
2b60792f
RG
258 *lv = l1;
259 *hv = h1;
260
261 /* If the value didn't fit, signal overflow. */
262 return l1 != low0 || h1 != high0;
263}
264
b8fca551
RG
265/* We force the double-int HIGH:LOW to the range of the type TYPE by
266 sign or zero extending it.
267 OVERFLOWABLE indicates if we are interested
2b60792f
RG
268 in overflow of the value, when >0 we are only interested in signed
269 overflow, for <0 we are interested in any overflow. OVERFLOWED
270 indicates whether overflow has already occurred. CONST_OVERFLOWED
271 indicates whether constant overflow has already occurred. We force
272 T's value to be within range of T's type (by setting to 0 or 1 all
273 the bits outside the type's range). We set TREE_OVERFLOWED if,
274 OVERFLOWED is nonzero,
275 or OVERFLOWABLE is >0 and signed overflow occurs
276 or OVERFLOWABLE is <0 and any overflow occurs
b8fca551
RG
277 We return a new tree node for the extended double-int. The node
278 is shared if no overflow flags are set. */
2b60792f
RG
279
280tree
b8fca551
RG
281force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
282 HOST_WIDE_INT high, int overflowable,
d95787e6 283 bool overflowed)
2b60792f 284{
2b60792f
RG
285 int sign_extended_type;
286 bool overflow;
287
2b60792f 288 /* Size types *are* sign extended. */
b8fca551
RG
289 sign_extended_type = (!TYPE_UNSIGNED (type)
290 || (TREE_CODE (type) == INTEGER_TYPE
291 && TYPE_IS_SIZETYPE (type)));
2b60792f 292
b8fca551 293 overflow = fit_double_type (low, high, &low, &high, type);
2b60792f 294
b8fca551 295 /* If we need to set overflow flags, return a new unshared node. */
d95787e6 296 if (overflowed || overflow)
ca7a3bd7
NS
297 {
298 if (overflowed
299 || overflowable < 0
300 || (overflowable > 0 && sign_extended_type))
301 {
b8fca551
RG
302 tree t = make_node (INTEGER_CST);
303 TREE_INT_CST_LOW (t) = low;
304 TREE_INT_CST_HIGH (t) = high;
305 TREE_TYPE (t) = type;
ca7a3bd7 306 TREE_OVERFLOW (t) = 1;
b8fca551 307 return t;
89b0433e 308 }
ca7a3bd7 309 }
3e6688a7 310
b8fca551
RG
311 /* Else build a shared node. */
312 return build_int_cst_wide (type, low, high);
6d716ca8
RS
313}
314\f
906c4e36 315/* Add two doubleword integers with doubleword result.
6b7283ac 316 Return nonzero if the operation overflows according to UNSIGNED_P.
906c4e36 317 Each argument is given as two `HOST_WIDE_INT' pieces.
6d716ca8 318 One argument is L1 and H1; the other, L2 and H2.
37bdb7e3 319 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8 320
fe3e8e40 321int
6b7283ac
EB
322add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
323 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
324 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
325 bool unsigned_p)
6d716ca8 326{
05bccae2
RK
327 unsigned HOST_WIDE_INT l;
328 HOST_WIDE_INT h;
6d716ca8 329
37bdb7e3 330 l = l1 + l2;
05bccae2 331 h = h1 + h2 + (l < l1);
6d716ca8 332
37bdb7e3
TG
333 *lv = l;
334 *hv = h;
6b7283ac
EB
335
336 if (unsigned_p)
337 return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
338 else
339 return OVERFLOW_SUM_SIGN (h1, h2, h);
6d716ca8
RS
340}
341
906c4e36 342/* Negate a doubleword integer with doubleword result.
fe3e8e40 343 Return nonzero if the operation overflows, assuming it's signed.
906c4e36 344 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
37bdb7e3 345 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8 346
fe3e8e40 347int
75040a04
AJ
348neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
349 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
6d716ca8
RS
350{
351 if (l1 == 0)
352 {
353 *lv = 0;
354 *hv = - h1;
e0f776fb 355 return (*hv & h1) < 0;
6d716ca8
RS
356 }
357 else
358 {
b6cc0a72
KH
359 *lv = -l1;
360 *hv = ~h1;
fe3e8e40 361 return 0;
6d716ca8
RS
362 }
363}
364\f
906c4e36 365/* Multiply two doubleword integers with doubleword result.
6b7283ac 366 Return nonzero if the operation overflows according to UNSIGNED_P.
906c4e36 367 Each argument is given as two `HOST_WIDE_INT' pieces.
6d716ca8 368 One argument is L1 and H1; the other, L2 and H2.
37bdb7e3 369 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8 370
fe3e8e40 371int
6b7283ac
EB
372mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
373 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
374 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
375 bool unsigned_p)
6d716ca8 376{
37bdb7e3
TG
377 HOST_WIDE_INT arg1[4];
378 HOST_WIDE_INT arg2[4];
379 HOST_WIDE_INT prod[4 * 2];
b3694847
SS
380 unsigned HOST_WIDE_INT carry;
381 int i, j, k;
05bccae2
RK
382 unsigned HOST_WIDE_INT toplow, neglow;
383 HOST_WIDE_INT tophigh, neghigh;
6d716ca8 384
6d716ca8
RS
385 encode (arg1, l1, h1);
386 encode (arg2, l2, h2);
387
703ad42b 388 memset (prod, 0, sizeof prod);
6d716ca8 389
37bdb7e3
TG
390 for (i = 0; i < 4; i++)
391 {
392 carry = 0;
393 for (j = 0; j < 4; j++)
394 {
395 k = i + j;
396 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
397 carry += arg1[i] * arg2[j];
398 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
399 carry += prod[k];
400 prod[k] = LOWPART (carry);
401 carry = HIGHPART (carry);
402 }
403 prod[i + 4] = carry;
404 }
6d716ca8 405
6b7283ac 406 decode (prod, lv, hv);
b6cc0a72 407 decode (prod + 4, &toplow, &tophigh);
6b7283ac
EB
408
409 /* Unsigned overflow is immediate. */
410 if (unsigned_p)
411 return (toplow | tophigh) != 0;
412
413 /* Check for signed overflow by calculating the signed representation of the
414 top half of the result; it should agree with the low half's sign bit. */
fe3e8e40
RS
415 if (h1 < 0)
416 {
417 neg_double (l2, h2, &neglow, &neghigh);
418 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
419 }
420 if (h2 < 0)
421 {
422 neg_double (l1, h1, &neglow, &neghigh);
423 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
424 }
425 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
6d716ca8
RS
426}
427\f
906c4e36 428/* Shift the doubleword integer in L1, H1 left by COUNT places
6d716ca8
RS
429 keeping only PREC bits of result.
430 Shift right if COUNT is negative.
431 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
906c4e36 432 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8 433
e0f776fb 434void
75040a04
AJ
435lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
436 HOST_WIDE_INT count, unsigned int prec,
437 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
6d716ca8 438{
11b161d0
JW
439 unsigned HOST_WIDE_INT signmask;
440
6d716ca8
RS
441 if (count < 0)
442 {
b6cc0a72 443 rshift_double (l1, h1, -count, prec, lv, hv, arith);
e0f776fb 444 return;
6d716ca8 445 }
b6cc0a72 446
3d1877b1
RK
447 if (SHIFT_COUNT_TRUNCATED)
448 count %= prec;
6d716ca8 449
cb0a34c4
JW
450 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
451 {
452 /* Shifting by the host word size is undefined according to the
453 ANSI standard, so we must handle this as a special case. */
454 *hv = 0;
455 *lv = 0;
456 }
457 else if (count >= HOST_BITS_PER_WIDE_INT)
6d716ca8 458 {
05bccae2 459 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
37bdb7e3
TG
460 *lv = 0;
461 }
462 else
463 {
464 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
05bccae2
RK
465 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
466 *lv = l1 << count;
6d716ca8 467 }
11b161d0
JW
468
469 /* Sign extend all bits that are beyond the precision. */
470
471 signmask = -((prec > HOST_BITS_PER_WIDE_INT
0316d49b 472 ? ((unsigned HOST_WIDE_INT) *hv
dd3f0101 473 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
11b161d0
JW
474 : (*lv >> (prec - 1))) & 1);
475
476 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
477 ;
478 else if (prec >= HOST_BITS_PER_WIDE_INT)
479 {
480 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
481 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
482 }
483 else
484 {
485 *hv = signmask;
486 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
487 *lv |= signmask << prec;
488 }
6d716ca8
RS
489}
490
906c4e36 491/* Shift the doubleword integer in L1, H1 right by COUNT places
6d716ca8
RS
492 keeping only PREC bits of result. COUNT must be positive.
493 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
906c4e36 494 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8
RS
495
496void
75040a04
AJ
497rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
498 HOST_WIDE_INT count, unsigned int prec,
499 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
fa8db1f7 500 int arith)
6d716ca8 501{
37bdb7e3 502 unsigned HOST_WIDE_INT signmask;
05bccae2 503
37bdb7e3
TG
504 signmask = (arith
505 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
506 : 0);
6d716ca8 507
3d1877b1
RK
508 if (SHIFT_COUNT_TRUNCATED)
509 count %= prec;
6d716ca8 510
cb0a34c4
JW
511 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
512 {
513 /* Shifting by the host word size is undefined according to the
514 ANSI standard, so we must handle this as a special case. */
11b161d0
JW
515 *hv = 0;
516 *lv = 0;
cb0a34c4
JW
517 }
518 else if (count >= HOST_BITS_PER_WIDE_INT)
6d716ca8 519 {
11b161d0
JW
520 *hv = 0;
521 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
37bdb7e3
TG
522 }
523 else
524 {
11b161d0 525 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
05bccae2 526 *lv = ((l1 >> count)
2fde567e 527 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
11b161d0
JW
528 }
529
530 /* Zero / sign extend all bits that are beyond the precision. */
531
532 if (count >= (HOST_WIDE_INT)prec)
533 {
534 *hv = signmask;
535 *lv = signmask;
536 }
537 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
538 ;
539 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
540 {
541 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
542 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
543 }
544 else
545 {
546 *hv = signmask;
547 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
548 *lv |= signmask << (prec - count);
6d716ca8 549 }
6d716ca8
RS
550}
551\f
37bdb7e3 552/* Rotate the doubleword integer in L1, H1 left by COUNT places
6d716ca8
RS
553 keeping only PREC bits of result.
554 Rotate right if COUNT is negative.
906c4e36 555 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8
RS
556
557void
75040a04
AJ
558lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
559 HOST_WIDE_INT count, unsigned int prec,
560 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
6d716ca8 561{
05bccae2
RK
562 unsigned HOST_WIDE_INT s1l, s2l;
563 HOST_WIDE_INT s1h, s2h;
6d716ca8 564
4d39710e 565 count %= prec;
6d716ca8 566 if (count < 0)
4d39710e 567 count += prec;
6d716ca8 568
4d39710e
RK
569 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
570 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
571 *lv = s1l | s2l;
572 *hv = s1h | s2h;
6d716ca8
RS
573}
574
906c4e36 575/* Rotate the doubleword integer in L1, H1 left by COUNT places
6d716ca8 576 keeping only PREC bits of result. COUNT must be positive.
906c4e36 577 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8
RS
578
579void
75040a04
AJ
580rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
581 HOST_WIDE_INT count, unsigned int prec,
582 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
6d716ca8 583{
05bccae2
RK
584 unsigned HOST_WIDE_INT s1l, s2l;
585 HOST_WIDE_INT s1h, s2h;
6d716ca8 586
4d39710e
RK
587 count %= prec;
588 if (count < 0)
589 count += prec;
6d716ca8 590
4d39710e
RK
591 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
592 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
593 *lv = s1l | s2l;
594 *hv = s1h | s2h;
6d716ca8
RS
595}
596\f
906c4e36 597/* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
6d716ca8
RS
598 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
599 CODE is a tree code for a kind of division, one of
600 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
601 or EXACT_DIV_EXPR
4fe9b91c 602 It controls how the quotient is rounded to an integer.
fe3e8e40 603 Return nonzero if the operation overflows.
6d716ca8
RS
604 UNS nonzero says do unsigned division. */
605
dbb5b3ce 606int
fa8db1f7
AJ
607div_and_round_double (enum tree_code code, int uns,
608 unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
609 HOST_WIDE_INT hnum_orig,
610 unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
75040a04
AJ
611 HOST_WIDE_INT hden_orig,
612 unsigned HOST_WIDE_INT *lquo,
fa8db1f7
AJ
613 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
614 HOST_WIDE_INT *hrem)
6d716ca8
RS
615{
616 int quo_neg = 0;
37bdb7e3
TG
617 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
618 HOST_WIDE_INT den[4], quo[4];
b3694847 619 int i, j;
37bdb7e3 620 unsigned HOST_WIDE_INT work;
05bccae2
RK
621 unsigned HOST_WIDE_INT carry = 0;
622 unsigned HOST_WIDE_INT lnum = lnum_orig;
b8eb43a2 623 HOST_WIDE_INT hnum = hnum_orig;
05bccae2 624 unsigned HOST_WIDE_INT lden = lden_orig;
b8eb43a2 625 HOST_WIDE_INT hden = hden_orig;
fe3e8e40 626 int overflow = 0;
6d716ca8 627
05bccae2 628 if (hden == 0 && lden == 0)
956d6950 629 overflow = 1, lden = 1;
6d716ca8 630
beb235f8 631 /* Calculate quotient sign and convert operands to unsigned. */
b6cc0a72 632 if (!uns)
6d716ca8 633 {
fe3e8e40 634 if (hnum < 0)
6d716ca8
RS
635 {
636 quo_neg = ~ quo_neg;
fe3e8e40 637 /* (minimum integer) / (-1) is the only overflow case. */
05bccae2
RK
638 if (neg_double (lnum, hnum, &lnum, &hnum)
639 && ((HOST_WIDE_INT) lden & hden) == -1)
fe3e8e40 640 overflow = 1;
6d716ca8 641 }
b6cc0a72 642 if (hden < 0)
6d716ca8
RS
643 {
644 quo_neg = ~ quo_neg;
fe3e8e40 645 neg_double (lden, hden, &lden, &hden);
6d716ca8
RS
646 }
647 }
648
649 if (hnum == 0 && hden == 0)
650 { /* single precision */
651 *hquo = *hrem = 0;
88ee2651 652 /* This unsigned division rounds toward zero. */
05bccae2 653 *lquo = lnum / lden;
6d716ca8
RS
654 goto finish_up;
655 }
656
657 if (hnum == 0)
658 { /* trivial case: dividend < divisor */
659 /* hden != 0 already checked. */
660 *hquo = *lquo = 0;
661 *hrem = hnum;
662 *lrem = lnum;
663 goto finish_up;
664 }
665
703ad42b 666 memset (quo, 0, sizeof quo);
6d716ca8 667
703ad42b
KG
668 memset (num, 0, sizeof num); /* to zero 9th element */
669 memset (den, 0, sizeof den);
6d716ca8 670
b6cc0a72 671 encode (num, lnum, hnum);
6d716ca8
RS
672 encode (den, lden, hden);
673
37bdb7e3 674 /* Special code for when the divisor < BASE. */
05bccae2 675 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
37bdb7e3 676 {
6d716ca8 677 /* hnum != 0 already checked. */
37bdb7e3 678 for (i = 4 - 1; i >= 0; i--)
6d716ca8 679 {
37bdb7e3 680 work = num[i] + carry * BASE;
05bccae2
RK
681 quo[i] = work / lden;
682 carry = work % lden;
6d716ca8
RS
683 }
684 }
37bdb7e3
TG
685 else
686 {
687 /* Full double precision division,
688 with thanks to Don Knuth's "Seminumerical Algorithms". */
05bccae2
RK
689 int num_hi_sig, den_hi_sig;
690 unsigned HOST_WIDE_INT quo_est, scale;
6d716ca8 691
cc2902df 692 /* Find the highest nonzero divisor digit. */
b6cc0a72
KH
693 for (i = 4 - 1;; i--)
694 if (den[i] != 0)
695 {
696 den_hi_sig = i;
697 break;
698 }
37bdb7e3 699
05bccae2
RK
700 /* Insure that the first digit of the divisor is at least BASE/2.
701 This is required by the quotient digit estimation algorithm. */
6d716ca8 702
05bccae2
RK
703 scale = BASE / (den[den_hi_sig] + 1);
704 if (scale > 1)
705 { /* scale divisor and dividend */
706 carry = 0;
707 for (i = 0; i <= 4 - 1; i++)
708 {
709 work = (num[i] * scale) + carry;
710 num[i] = LOWPART (work);
711 carry = HIGHPART (work);
712 }
6d716ca8 713
05bccae2
RK
714 num[4] = carry;
715 carry = 0;
716 for (i = 0; i <= 4 - 1; i++)
717 {
718 work = (den[i] * scale) + carry;
719 den[i] = LOWPART (work);
720 carry = HIGHPART (work);
721 if (den[i] != 0) den_hi_sig = i;
722 }
723 }
6d716ca8 724
05bccae2 725 num_hi_sig = 4;
6d716ca8 726
05bccae2
RK
727 /* Main loop */
728 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
6d716ca8 729 {
05bccae2
RK
730 /* Guess the next quotient digit, quo_est, by dividing the first
731 two remaining dividend digits by the high order quotient digit.
732 quo_est is never low and is at most 2 high. */
733 unsigned HOST_WIDE_INT tmp;
734
735 num_hi_sig = i + den_hi_sig + 1;
736 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
737 if (num[num_hi_sig] != den[den_hi_sig])
738 quo_est = work / den[den_hi_sig];
739 else
740 quo_est = BASE - 1;
6d716ca8 741
6d2f8887 742 /* Refine quo_est so it's usually correct, and at most one high. */
05bccae2
RK
743 tmp = work - quo_est * den[den_hi_sig];
744 if (tmp < BASE
745 && (den[den_hi_sig - 1] * quo_est
746 > (tmp * BASE + num[num_hi_sig - 2])))
747 quo_est--;
6d716ca8 748
05bccae2
RK
749 /* Try QUO_EST as the quotient digit, by multiplying the
750 divisor by QUO_EST and subtracting from the remaining dividend.
751 Keep in mind that QUO_EST is the I - 1st digit. */
752
753 carry = 0;
6d716ca8
RS
754 for (j = 0; j <= den_hi_sig; j++)
755 {
05bccae2 756 work = quo_est * den[j] + carry;
37bdb7e3 757 carry = HIGHPART (work);
05bccae2 758 work = num[i + j] - LOWPART (work);
37bdb7e3 759 num[i + j] = LOWPART (work);
05bccae2 760 carry += HIGHPART (work) != 0;
6d716ca8 761 }
6d716ca8 762
05bccae2
RK
763 /* If quo_est was high by one, then num[i] went negative and
764 we need to correct things. */
0316d49b 765 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
05bccae2
RK
766 {
767 quo_est--;
768 carry = 0; /* add divisor back in */
769 for (j = 0; j <= den_hi_sig; j++)
770 {
771 work = num[i + j] + den[j] + carry;
772 carry = HIGHPART (work);
773 num[i + j] = LOWPART (work);
774 }
775
776 num [num_hi_sig] += carry;
777 }
778
779 /* Store the quotient digit. */
780 quo[i] = quo_est;
781 }
6d716ca8 782 }
6d716ca8
RS
783
784 decode (quo, lquo, hquo);
785
786 finish_up:
938d968e 787 /* If result is negative, make it so. */
6d716ca8
RS
788 if (quo_neg)
789 neg_double (*lquo, *hquo, lquo, hquo);
790
a1105617 791 /* Compute trial remainder: rem = num - (quo * den) */
6d716ca8
RS
792 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
793 neg_double (*lrem, *hrem, lrem, hrem);
794 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
795
796 switch (code)
797 {
798 case TRUNC_DIV_EXPR:
799 case TRUNC_MOD_EXPR: /* round toward zero */
800 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
fe3e8e40 801 return overflow;
6d716ca8
RS
802
803 case FLOOR_DIV_EXPR:
804 case FLOOR_MOD_EXPR: /* round toward negative infinity */
805 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
806 {
807 /* quo = quo - 1; */
906c4e36
RK
808 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
809 lquo, hquo);
6d716ca8 810 }
05bccae2
RK
811 else
812 return overflow;
6d716ca8
RS
813 break;
814
815 case CEIL_DIV_EXPR:
816 case CEIL_MOD_EXPR: /* round toward positive infinity */
817 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
818 {
906c4e36
RK
819 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
820 lquo, hquo);
6d716ca8 821 }
05bccae2
RK
822 else
823 return overflow;
6d716ca8 824 break;
b6cc0a72 825
6d716ca8
RS
826 case ROUND_DIV_EXPR:
827 case ROUND_MOD_EXPR: /* round to closest integer */
828 {
05bccae2
RK
829 unsigned HOST_WIDE_INT labs_rem = *lrem;
830 HOST_WIDE_INT habs_rem = *hrem;
831 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
832 HOST_WIDE_INT habs_den = hden, htwice;
833
f9da5064 834 /* Get absolute values. */
05bccae2
RK
835 if (*hrem < 0)
836 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
837 if (hden < 0)
838 neg_double (lden, hden, &labs_den, &habs_den);
839
840 /* If (2 * abs (lrem) >= abs (lden)) */
906c4e36
RK
841 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
842 labs_rem, habs_rem, &ltwice, &htwice);
05bccae2 843
906c4e36
RK
844 if (((unsigned HOST_WIDE_INT) habs_den
845 < (unsigned HOST_WIDE_INT) htwice)
846 || (((unsigned HOST_WIDE_INT) habs_den
847 == (unsigned HOST_WIDE_INT) htwice)
05bccae2 848 && (labs_den < ltwice)))
6d716ca8
RS
849 {
850 if (*hquo < 0)
851 /* quo = quo - 1; */
906c4e36
RK
852 add_double (*lquo, *hquo,
853 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
6d716ca8
RS
854 else
855 /* quo = quo + 1; */
906c4e36
RK
856 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
857 lquo, hquo);
6d716ca8 858 }
05bccae2
RK
859 else
860 return overflow;
6d716ca8
RS
861 }
862 break;
863
864 default:
0bccc606 865 gcc_unreachable ();
6d716ca8
RS
866 }
867
e0a21ab9 868 /* Compute true remainder: rem = num - (quo * den) */
6d716ca8
RS
869 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
870 neg_double (*lrem, *hrem, lrem, hrem);
871 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
fe3e8e40 872 return overflow;
6d716ca8 873}
03b0db0a
RG
874
875/* If ARG2 divides ARG1 with zero remainder, carries out the division
876 of type CODE and returns the quotient.
877 Otherwise returns NULL_TREE. */
878
879static tree
880div_if_zero_remainder (enum tree_code code, tree arg1, tree arg2)
881{
882 unsigned HOST_WIDE_INT int1l, int2l;
883 HOST_WIDE_INT int1h, int2h;
884 unsigned HOST_WIDE_INT quol, reml;
885 HOST_WIDE_INT quoh, remh;
886 tree type = TREE_TYPE (arg1);
887 int uns = TYPE_UNSIGNED (type);
888
889 int1l = TREE_INT_CST_LOW (arg1);
890 int1h = TREE_INT_CST_HIGH (arg1);
c80b4100 891 /* &obj[0] + -128 really should be compiled as &obj[-8] rather than
8d5d5865
JH
892 &obj[some_exotic_number]. */
893 if (POINTER_TYPE_P (type))
894 {
895 uns = false;
896 type = signed_type_for (type);
897 fit_double_type (int1l, int1h, &int1l, &int1h,
898 type);
899 }
900 else
901 fit_double_type (int1l, int1h, &int1l, &int1h, type);
03b0db0a
RG
902 int2l = TREE_INT_CST_LOW (arg2);
903 int2h = TREE_INT_CST_HIGH (arg2);
904
905 div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
906 &quol, &quoh, &reml, &remh);
907 if (remh != 0 || reml != 0)
908 return NULL_TREE;
909
910 return build_int_cst_wide (type, quol, quoh);
911}
6d716ca8 912\f
110abdbc 913/* This is nonzero if we should defer warnings about undefined
6ac01510
ILT
914 overflow. This facility exists because these warnings are a
915 special case. The code to estimate loop iterations does not want
916 to issue any warnings, since it works with expressions which do not
917 occur in user code. Various bits of cleanup code call fold(), but
918 only use the result if it has certain characteristics (e.g., is a
919 constant); that code only wants to issue a warning if the result is
920 used. */
921
922static int fold_deferring_overflow_warnings;
923
924/* If a warning about undefined overflow is deferred, this is the
925 warning. Note that this may cause us to turn two warnings into
926 one, but that is fine since it is sufficient to only give one
927 warning per expression. */
928
929static const char* fold_deferred_overflow_warning;
930
931/* If a warning about undefined overflow is deferred, this is the
932 level at which the warning should be emitted. */
933
934static enum warn_strict_overflow_code fold_deferred_overflow_code;
935
936/* Start deferring overflow warnings. We could use a stack here to
937 permit nested calls, but at present it is not necessary. */
938
939void
940fold_defer_overflow_warnings (void)
941{
942 ++fold_deferring_overflow_warnings;
943}
944
945/* Stop deferring overflow warnings. If there is a pending warning,
946 and ISSUE is true, then issue the warning if appropriate. STMT is
947 the statement with which the warning should be associated (used for
948 location information); STMT may be NULL. CODE is the level of the
949 warning--a warn_strict_overflow_code value. This function will use
950 the smaller of CODE and the deferred code when deciding whether to
951 issue the warning. CODE may be zero to mean to always use the
952 deferred code. */
953
954void
955fold_undefer_overflow_warnings (bool issue, tree stmt, int code)
956{
957 const char *warnmsg;
958 location_t locus;
959
960 gcc_assert (fold_deferring_overflow_warnings > 0);
961 --fold_deferring_overflow_warnings;
962 if (fold_deferring_overflow_warnings > 0)
963 {
964 if (fold_deferred_overflow_warning != NULL
965 && code != 0
966 && code < (int) fold_deferred_overflow_code)
967 fold_deferred_overflow_code = code;
968 return;
969 }
970
971 warnmsg = fold_deferred_overflow_warning;
972 fold_deferred_overflow_warning = NULL;
973
974 if (!issue || warnmsg == NULL)
975 return;
976
977 /* Use the smallest code level when deciding to issue the
978 warning. */
979 if (code == 0 || code > (int) fold_deferred_overflow_code)
980 code = fold_deferred_overflow_code;
981
982 if (!issue_strict_overflow_warning (code))
983 return;
984
985 if (stmt == NULL_TREE || !expr_has_location (stmt))
986 locus = input_location;
987 else
988 locus = expr_location (stmt);
989 warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
990}
991
992/* Stop deferring overflow warnings, ignoring any deferred
993 warnings. */
994
995void
996fold_undefer_and_ignore_overflow_warnings (void)
997{
998 fold_undefer_overflow_warnings (false, NULL_TREE, 0);
999}
1000
1001/* Whether we are deferring overflow warnings. */
1002
1003bool
1004fold_deferring_overflow_warnings_p (void)
1005{
1006 return fold_deferring_overflow_warnings > 0;
1007}
1008
1009/* This is called when we fold something based on the fact that signed
1010 overflow is undefined. */
1011
1012static void
1013fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1014{
1015 gcc_assert (!flag_wrapv && !flag_trapv);
1016 if (fold_deferring_overflow_warnings > 0)
1017 {
1018 if (fold_deferred_overflow_warning == NULL
1019 || wc < fold_deferred_overflow_code)
1020 {
1021 fold_deferred_overflow_warning = gmsgid;
1022 fold_deferred_overflow_code = wc;
1023 }
1024 }
1025 else if (issue_strict_overflow_warning (wc))
1026 warning (OPT_Wstrict_overflow, gmsgid);
1027}
1028\f
dd6f2a43
VR
1029/* Return true if the built-in mathematical function specified by CODE
1030 is odd, i.e. -f(x) == f(-x). */
05d362b8
RS
1031
1032static bool
1033negate_mathfn_p (enum built_in_function code)
1034{
1035 switch (code)
1036 {
ea6a6627
VR
1037 CASE_FLT_FN (BUILT_IN_ASIN):
1038 CASE_FLT_FN (BUILT_IN_ASINH):
1039 CASE_FLT_FN (BUILT_IN_ATAN):
1040 CASE_FLT_FN (BUILT_IN_ATANH):
4b26d10b
KG
1041 CASE_FLT_FN (BUILT_IN_CASIN):
1042 CASE_FLT_FN (BUILT_IN_CASINH):
1043 CASE_FLT_FN (BUILT_IN_CATAN):
1044 CASE_FLT_FN (BUILT_IN_CATANH):
ea6a6627 1045 CASE_FLT_FN (BUILT_IN_CBRT):
4b26d10b
KG
1046 CASE_FLT_FN (BUILT_IN_CPROJ):
1047 CASE_FLT_FN (BUILT_IN_CSIN):
1048 CASE_FLT_FN (BUILT_IN_CSINH):
1049 CASE_FLT_FN (BUILT_IN_CTAN):
1050 CASE_FLT_FN (BUILT_IN_CTANH):
5c5b2155
KG
1051 CASE_FLT_FN (BUILT_IN_ERF):
1052 CASE_FLT_FN (BUILT_IN_LLROUND):
1053 CASE_FLT_FN (BUILT_IN_LROUND):
1054 CASE_FLT_FN (BUILT_IN_ROUND):
ea6a6627
VR
1055 CASE_FLT_FN (BUILT_IN_SIN):
1056 CASE_FLT_FN (BUILT_IN_SINH):
1057 CASE_FLT_FN (BUILT_IN_TAN):
1058 CASE_FLT_FN (BUILT_IN_TANH):
5c5b2155 1059 CASE_FLT_FN (BUILT_IN_TRUNC):
05d362b8
RS
1060 return true;
1061
5c5b2155
KG
1062 CASE_FLT_FN (BUILT_IN_LLRINT):
1063 CASE_FLT_FN (BUILT_IN_LRINT):
1064 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1065 CASE_FLT_FN (BUILT_IN_RINT):
1066 return !flag_rounding_math;
1067
05d362b8
RS
1068 default:
1069 break;
1070 }
1071 return false;
1072}
1073
82b85a85
ZD
1074/* Check whether we may negate an integer constant T without causing
1075 overflow. */
1076
1077bool
1078may_negate_without_overflow_p (tree t)
1079{
1080 unsigned HOST_WIDE_INT val;
1081 unsigned int prec;
1082 tree type;
1083
0bccc606 1084 gcc_assert (TREE_CODE (t) == INTEGER_CST);
82b85a85
ZD
1085
1086 type = TREE_TYPE (t);
1087 if (TYPE_UNSIGNED (type))
1088 return false;
1089
1090 prec = TYPE_PRECISION (type);
1091 if (prec > HOST_BITS_PER_WIDE_INT)
1092 {
1093 if (TREE_INT_CST_LOW (t) != 0)
1094 return true;
1095 prec -= HOST_BITS_PER_WIDE_INT;
1096 val = TREE_INT_CST_HIGH (t);
1097 }
1098 else
1099 val = TREE_INT_CST_LOW (t);
1100 if (prec < HOST_BITS_PER_WIDE_INT)
1101 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1102 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1103}
1104
080ea642 1105/* Determine whether an expression T can be cheaply negated using
1af8dcbf 1106 the function negate_expr without introducing undefined overflow. */
080ea642
RS
1107
1108static bool
fa8db1f7 1109negate_expr_p (tree t)
080ea642 1110{
080ea642
RS
1111 tree type;
1112
1113 if (t == 0)
1114 return false;
1115
1116 type = TREE_TYPE (t);
1117
1118 STRIP_SIGN_NOPS (t);
1119 switch (TREE_CODE (t))
1120 {
1121 case INTEGER_CST:
eeef0e45 1122 if (TYPE_OVERFLOW_WRAPS (type))
05d362b8 1123 return true;
080ea642
RS
1124
1125 /* Check that -CST will not overflow type. */
82b85a85 1126 return may_negate_without_overflow_p (t);
189d4130 1127 case BIT_NOT_EXPR:
eeef0e45
ILT
1128 return (INTEGRAL_TYPE_P (type)
1129 && TYPE_OVERFLOW_WRAPS (type));
080ea642
RS
1130
1131 case REAL_CST:
1132 case NEGATE_EXPR:
080ea642
RS
1133 return true;
1134
05d362b8
RS
1135 case COMPLEX_CST:
1136 return negate_expr_p (TREE_REALPART (t))
1137 && negate_expr_p (TREE_IMAGPART (t));
1138
1aeef526
KG
1139 case COMPLEX_EXPR:
1140 return negate_expr_p (TREE_OPERAND (t, 0))
1141 && negate_expr_p (TREE_OPERAND (t, 1));
1142
8fbbe90b
KG
1143 case CONJ_EXPR:
1144 return negate_expr_p (TREE_OPERAND (t, 0));
1145
dfb36f9b 1146 case PLUS_EXPR:
1b43b967
RS
1147 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1148 || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
dfb36f9b
RS
1149 return false;
1150 /* -(A + B) -> (-B) - A. */
1151 if (negate_expr_p (TREE_OPERAND (t, 1))
1152 && reorder_operands_p (TREE_OPERAND (t, 0),
1153 TREE_OPERAND (t, 1)))
1154 return true;
1155 /* -(A + B) -> (-A) - B. */
1156 return negate_expr_p (TREE_OPERAND (t, 0));
1157
02a1994c
RS
1158 case MINUS_EXPR:
1159 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
1b43b967
RS
1160 return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1161 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
05d362b8
RS
1162 && reorder_operands_p (TREE_OPERAND (t, 0),
1163 TREE_OPERAND (t, 1));
02a1994c 1164
8ab49fef 1165 case MULT_EXPR:
8df83eae 1166 if (TYPE_UNSIGNED (TREE_TYPE (t)))
8ab49fef
RS
1167 break;
1168
1169 /* Fall through. */
1170
1171 case RDIV_EXPR:
1172 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1173 return negate_expr_p (TREE_OPERAND (t, 1))
1174 || negate_expr_p (TREE_OPERAND (t, 0));
1175 break;
1176
965d7fa4
AP
1177 case TRUNC_DIV_EXPR:
1178 case ROUND_DIV_EXPR:
1179 case FLOOR_DIV_EXPR:
1180 case CEIL_DIV_EXPR:
1181 case EXACT_DIV_EXPR:
6ac01510
ILT
1182 /* In general we can't negate A / B, because if A is INT_MIN and
1183 B is 1, we may turn this into INT_MIN / -1 which is undefined
1184 and actually traps on some architectures. But if overflow is
1185 undefined, we can negate, because - (INT_MIN / 1) is an
1186 overflow. */
eeef0e45
ILT
1187 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1188 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
965d7fa4
AP
1189 break;
1190 return negate_expr_p (TREE_OPERAND (t, 1))
1191 || negate_expr_p (TREE_OPERAND (t, 0));
1192
05d362b8
RS
1193 case NOP_EXPR:
1194 /* Negate -((double)float) as (double)(-float). */
1195 if (TREE_CODE (type) == REAL_TYPE)
1196 {
1197 tree tem = strip_float_extensions (t);
1198 if (tem != t)
1199 return negate_expr_p (tem);
1200 }
1201 break;
1202
1203 case CALL_EXPR:
1204 /* Negate -f(x) as f(-x). */
1205 if (negate_mathfn_p (builtin_mathfn_code (t)))
5039610b 1206 return negate_expr_p (CALL_EXPR_ARG (t, 0));
05d362b8
RS
1207 break;
1208
239a625e
RS
1209 case RSHIFT_EXPR:
1210 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1211 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1212 {
1213 tree op1 = TREE_OPERAND (t, 1);
1214 if (TREE_INT_CST_HIGH (op1) == 0
1215 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1216 == TREE_INT_CST_LOW (op1))
1217 return true;
1218 }
1219 break;
1220
080ea642
RS
1221 default:
1222 break;
1223 }
1224 return false;
1225}
1226
1af8dcbf
RG
1227/* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1228 simplification is possible.
1229 If negate_expr_p would return true for T, NULL_TREE will never be
1230 returned. */
6d716ca8 1231
1baa375f 1232static tree
1af8dcbf 1233fold_negate_expr (tree t)
1baa375f 1234{
1af8dcbf 1235 tree type = TREE_TYPE (t);
1baa375f
RK
1236 tree tem;
1237
1baa375f
RK
1238 switch (TREE_CODE (t))
1239 {
189d4130
AP
1240 /* Convert - (~A) to A + 1. */
1241 case BIT_NOT_EXPR:
1af8dcbf 1242 if (INTEGRAL_TYPE_P (type))
189d4130
AP
1243 return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
1244 build_int_cst (type, 1));
8bce9e98 1245 break;
189d4130 1246
1baa375f 1247 case INTEGER_CST:
33d13fac 1248 tem = fold_negate_const (t, type);
ee7d8048 1249 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
eeef0e45 1250 || !TYPE_OVERFLOW_TRAPS (type))
1baa375f
RK
1251 return tem;
1252 break;
1253
8ab49fef 1254 case REAL_CST:
33d13fac 1255 tem = fold_negate_const (t, type);
8ab49fef 1256 /* Two's complement FP formats, such as c4x, may overflow. */
455f14dd 1257 if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1af8dcbf 1258 return tem;
8ab49fef
RS
1259 break;
1260
05d362b8
RS
1261 case COMPLEX_CST:
1262 {
1263 tree rpart = negate_expr (TREE_REALPART (t));
1264 tree ipart = negate_expr (TREE_IMAGPART (t));
1265
1266 if ((TREE_CODE (rpart) == REAL_CST
1267 && TREE_CODE (ipart) == REAL_CST)
1268 || (TREE_CODE (rpart) == INTEGER_CST
1269 && TREE_CODE (ipart) == INTEGER_CST))
1270 return build_complex (type, rpart, ipart);
1271 }
1272 break;
1273
1aeef526
KG
1274 case COMPLEX_EXPR:
1275 if (negate_expr_p (t))
1276 return fold_build2 (COMPLEX_EXPR, type,
1277 fold_negate_expr (TREE_OPERAND (t, 0)),
1278 fold_negate_expr (TREE_OPERAND (t, 1)));
1279 break;
1280
8fbbe90b
KG
1281 case CONJ_EXPR:
1282 if (negate_expr_p (t))
1283 return fold_build1 (CONJ_EXPR, type,
1284 fold_negate_expr (TREE_OPERAND (t, 0)));
1285 break;
1286
1baa375f 1287 case NEGATE_EXPR:
1af8dcbf 1288 return TREE_OPERAND (t, 0);
1baa375f 1289
dfb36f9b 1290 case PLUS_EXPR:
1b43b967
RS
1291 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1292 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
dfb36f9b
RS
1293 {
1294 /* -(A + B) -> (-B) - A. */
1295 if (negate_expr_p (TREE_OPERAND (t, 1))
1296 && reorder_operands_p (TREE_OPERAND (t, 0),
1297 TREE_OPERAND (t, 1)))
59ce6d6b
RS
1298 {
1299 tem = negate_expr (TREE_OPERAND (t, 1));
1af8dcbf
RG
1300 return fold_build2 (MINUS_EXPR, type,
1301 tem, TREE_OPERAND (t, 0));
59ce6d6b
RS
1302 }
1303
dfb36f9b
RS
1304 /* -(A + B) -> (-A) - B. */
1305 if (negate_expr_p (TREE_OPERAND (t, 0)))
59ce6d6b
RS
1306 {
1307 tem = negate_expr (TREE_OPERAND (t, 0));
1af8dcbf
RG
1308 return fold_build2 (MINUS_EXPR, type,
1309 tem, TREE_OPERAND (t, 1));
59ce6d6b 1310 }
dfb36f9b
RS
1311 }
1312 break;
1313
1baa375f
RK
1314 case MINUS_EXPR:
1315 /* - (A - B) -> B - A */
1b43b967
RS
1316 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1317 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
05d362b8 1318 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1af8dcbf
RG
1319 return fold_build2 (MINUS_EXPR, type,
1320 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1baa375f
RK
1321 break;
1322
8ab49fef 1323 case MULT_EXPR:
1af8dcbf 1324 if (TYPE_UNSIGNED (type))
8ab49fef
RS
1325 break;
1326
1327 /* Fall through. */
1328
1329 case RDIV_EXPR:
1af8dcbf 1330 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
8ab49fef
RS
1331 {
1332 tem = TREE_OPERAND (t, 1);
1333 if (negate_expr_p (tem))
1af8dcbf
RG
1334 return fold_build2 (TREE_CODE (t), type,
1335 TREE_OPERAND (t, 0), negate_expr (tem));
8ab49fef
RS
1336 tem = TREE_OPERAND (t, 0);
1337 if (negate_expr_p (tem))
1af8dcbf
RG
1338 return fold_build2 (TREE_CODE (t), type,
1339 negate_expr (tem), TREE_OPERAND (t, 1));
8ab49fef
RS
1340 }
1341 break;
1342
965d7fa4
AP
1343 case TRUNC_DIV_EXPR:
1344 case ROUND_DIV_EXPR:
1345 case FLOOR_DIV_EXPR:
1346 case CEIL_DIV_EXPR:
1347 case EXACT_DIV_EXPR:
6ac01510
ILT
1348 /* In general we can't negate A / B, because if A is INT_MIN and
1349 B is 1, we may turn this into INT_MIN / -1 which is undefined
1350 and actually traps on some architectures. But if overflow is
1351 undefined, we can negate, because - (INT_MIN / 1) is an
1352 overflow. */
eeef0e45 1353 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
965d7fa4 1354 {
6ac01510
ILT
1355 const char * const warnmsg = G_("assuming signed overflow does not "
1356 "occur when negating a division");
965d7fa4
AP
1357 tem = TREE_OPERAND (t, 1);
1358 if (negate_expr_p (tem))
6ac01510
ILT
1359 {
1360 if (INTEGRAL_TYPE_P (type)
1361 && (TREE_CODE (tem) != INTEGER_CST
1362 || integer_onep (tem)))
1363 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1364 return fold_build2 (TREE_CODE (t), type,
1365 TREE_OPERAND (t, 0), negate_expr (tem));
1366 }
965d7fa4
AP
1367 tem = TREE_OPERAND (t, 0);
1368 if (negate_expr_p (tem))
6ac01510
ILT
1369 {
1370 if (INTEGRAL_TYPE_P (type)
1371 && (TREE_CODE (tem) != INTEGER_CST
1372 || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1373 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1374 return fold_build2 (TREE_CODE (t), type,
1375 negate_expr (tem), TREE_OPERAND (t, 1));
1376 }
965d7fa4
AP
1377 }
1378 break;
1379
05d362b8
RS
1380 case NOP_EXPR:
1381 /* Convert -((double)float) into (double)(-float). */
1382 if (TREE_CODE (type) == REAL_TYPE)
1383 {
1384 tem = strip_float_extensions (t);
1385 if (tem != t && negate_expr_p (tem))
1af8dcbf 1386 return negate_expr (tem);
05d362b8
RS
1387 }
1388 break;
1389
1390 case CALL_EXPR:
1391 /* Negate -f(x) as f(-x). */
1392 if (negate_mathfn_p (builtin_mathfn_code (t))
5039610b 1393 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
05d362b8 1394 {
5039610b 1395 tree fndecl, arg;
05d362b8
RS
1396
1397 fndecl = get_callee_fndecl (t);
5039610b
SL
1398 arg = negate_expr (CALL_EXPR_ARG (t, 0));
1399 return build_call_expr (fndecl, 1, arg);
05d362b8
RS
1400 }
1401 break;
1402
239a625e
RS
1403 case RSHIFT_EXPR:
1404 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1405 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1406 {
1407 tree op1 = TREE_OPERAND (t, 1);
1408 if (TREE_INT_CST_HIGH (op1) == 0
1409 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1410 == TREE_INT_CST_LOW (op1))
1411 {
8df83eae 1412 tree ntype = TYPE_UNSIGNED (type)
12753674 1413 ? signed_type_for (type)
ca5ba2a3 1414 : unsigned_type_for (type);
239a625e 1415 tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
7f20a5b7 1416 temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
239a625e
RS
1417 return fold_convert (type, temp);
1418 }
1419 }
1420 break;
1421
1baa375f
RK
1422 default:
1423 break;
1424 }
1425
1af8dcbf
RG
1426 return NULL_TREE;
1427}
1428
1429/* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1430 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
1431 return NULL_TREE. */
1432
1433static tree
1434negate_expr (tree t)
1435{
1436 tree type, tem;
1437
1438 if (t == NULL_TREE)
1439 return NULL_TREE;
1440
1441 type = TREE_TYPE (t);
1442 STRIP_SIGN_NOPS (t);
1443
1444 tem = fold_negate_expr (t);
1445 if (!tem)
1446 tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
088414c1 1447 return fold_convert (type, tem);
1baa375f
RK
1448}
1449\f
1450/* Split a tree IN into a constant, literal and variable parts that could be
1451 combined with CODE to make IN. "constant" means an expression with
1452 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1453 commutative arithmetic operation. Store the constant part into *CONP,
cff27795 1454 the literal in *LITP and return the variable part. If a part isn't
1baa375f
RK
1455 present, set it to null. If the tree does not decompose in this way,
1456 return the entire tree as the variable part and the other parts as null.
1457
1458 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
cff27795
EB
1459 case, we negate an operand that was subtracted. Except if it is a
1460 literal for which we use *MINUS_LITP instead.
1461
1462 If NEGATE_P is true, we are negating all of IN, again except a literal
1463 for which we use *MINUS_LITP instead.
1baa375f
RK
1464
1465 If IN is itself a literal or constant, return it as appropriate.
1466
1467 Note that we do not guarantee that any of the three values will be the
1468 same type as IN, but they will have the same signedness and mode. */
1469
1470static tree
75040a04
AJ
1471split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1472 tree *minus_litp, int negate_p)
6d716ca8 1473{
1baa375f
RK
1474 tree var = 0;
1475
6d716ca8 1476 *conp = 0;
1baa375f 1477 *litp = 0;
cff27795 1478 *minus_litp = 0;
1baa375f 1479
30f7a378 1480 /* Strip any conversions that don't change the machine mode or signedness. */
1baa375f
RK
1481 STRIP_SIGN_NOPS (in);
1482
1483 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1484 *litp = in;
1baa375f
RK
1485 else if (TREE_CODE (in) == code
1486 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1487 /* We can associate addition and subtraction together (even
1488 though the C standard doesn't say so) for integers because
1489 the value is not affected. For reals, the value might be
1490 affected, so we can't. */
1491 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1492 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1493 {
1494 tree op0 = TREE_OPERAND (in, 0);
1495 tree op1 = TREE_OPERAND (in, 1);
1496 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1497 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1498
1499 /* First see if either of the operands is a literal, then a constant. */
1500 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1501 *litp = op0, op0 = 0;
1502 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1503 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1504
1505 if (op0 != 0 && TREE_CONSTANT (op0))
1506 *conp = op0, op0 = 0;
1507 else if (op1 != 0 && TREE_CONSTANT (op1))
1508 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1509
1510 /* If we haven't dealt with either operand, this is not a case we can
30f7a378 1511 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1baa375f
RK
1512 if (op0 != 0 && op1 != 0)
1513 var = in;
1514 else if (op0 != 0)
1515 var = op0;
1516 else
1517 var = op1, neg_var_p = neg1_p;
6d716ca8 1518
1baa375f 1519 /* Now do any needed negations. */
cff27795
EB
1520 if (neg_litp_p)
1521 *minus_litp = *litp, *litp = 0;
1522 if (neg_conp_p)
1523 *conp = negate_expr (*conp);
1524 if (neg_var_p)
1525 var = negate_expr (var);
1baa375f 1526 }
1796dff4
RH
1527 else if (TREE_CONSTANT (in))
1528 *conp = in;
1baa375f
RK
1529 else
1530 var = in;
1531
1532 if (negate_p)
6d716ca8 1533 {
cff27795
EB
1534 if (*litp)
1535 *minus_litp = *litp, *litp = 0;
1536 else if (*minus_litp)
1537 *litp = *minus_litp, *minus_litp = 0;
1baa375f 1538 *conp = negate_expr (*conp);
cff27795 1539 var = negate_expr (var);
6d716ca8 1540 }
1baa375f
RK
1541
1542 return var;
1543}
1544
1545/* Re-associate trees split by the above function. T1 and T2 are either
1546 expressions to associate or null. Return the new expression, if any. If
cff27795 1547 we build an operation, do it in TYPE and with CODE. */
1baa375f
RK
1548
1549static tree
fa8db1f7 1550associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1baa375f 1551{
1baa375f
RK
1552 if (t1 == 0)
1553 return t2;
1554 else if (t2 == 0)
1555 return t1;
1556
1baa375f
RK
1557 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1558 try to fold this since we will have infinite recursion. But do
1559 deal with any NEGATE_EXPRs. */
1560 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1561 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1562 {
1bed5ee3
JJ
1563 if (code == PLUS_EXPR)
1564 {
1565 if (TREE_CODE (t1) == NEGATE_EXPR)
59ce6d6b
RS
1566 return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1567 fold_convert (type, TREE_OPERAND (t1, 0)));
1bed5ee3 1568 else if (TREE_CODE (t2) == NEGATE_EXPR)
59ce6d6b
RS
1569 return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1570 fold_convert (type, TREE_OPERAND (t2, 0)));
18522563
ZD
1571 else if (integer_zerop (t2))
1572 return fold_convert (type, t1);
1bed5ee3 1573 }
18522563
ZD
1574 else if (code == MINUS_EXPR)
1575 {
1576 if (integer_zerop (t2))
1577 return fold_convert (type, t1);
1578 }
1579
59ce6d6b
RS
1580 return build2 (code, type, fold_convert (type, t1),
1581 fold_convert (type, t2));
1baa375f
RK
1582 }
1583
7f20a5b7
KH
1584 return fold_build2 (code, type, fold_convert (type, t1),
1585 fold_convert (type, t2));
6d716ca8
RS
1586}
1587\f
000d8d44
RS
1588/* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1589 for use in int_const_binop, size_binop and size_diffop. */
1590
1591static bool
1592int_binop_types_match_p (enum tree_code code, tree type1, tree type2)
1593{
1594 if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1595 return false;
1596 if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1597 return false;
1598
1599 switch (code)
1600 {
1601 case LSHIFT_EXPR:
1602 case RSHIFT_EXPR:
1603 case LROTATE_EXPR:
1604 case RROTATE_EXPR:
1605 return true;
1606
1607 default:
1608 break;
1609 }
1610
1611 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1612 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1613 && TYPE_MODE (type1) == TYPE_MODE (type2);
1614}
1615
1616
e9a25f70 1617/* Combine two integer constants ARG1 and ARG2 under operation CODE
fd6c76f4
RS
1618 to produce a new constant. Return NULL_TREE if we don't know how
1619 to evaluate CODE at compile-time.
91d33e36 1620
4c160717 1621 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
6d716ca8 1622
6de9cd9a 1623tree
fa8db1f7 1624int_const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
6d716ca8 1625{
05bccae2
RK
1626 unsigned HOST_WIDE_INT int1l, int2l;
1627 HOST_WIDE_INT int1h, int2h;
1628 unsigned HOST_WIDE_INT low;
1629 HOST_WIDE_INT hi;
1630 unsigned HOST_WIDE_INT garbagel;
1631 HOST_WIDE_INT garbageh;
b3694847 1632 tree t;
4c160717 1633 tree type = TREE_TYPE (arg1);
8df83eae 1634 int uns = TYPE_UNSIGNED (type);
4c160717
RK
1635 int is_sizetype
1636 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
e9a25f70 1637 int overflow = 0;
3dedc65a 1638
e9a25f70
JL
1639 int1l = TREE_INT_CST_LOW (arg1);
1640 int1h = TREE_INT_CST_HIGH (arg1);
1641 int2l = TREE_INT_CST_LOW (arg2);
1642 int2h = TREE_INT_CST_HIGH (arg2);
1643
1644 switch (code)
6d716ca8 1645 {
e9a25f70
JL
1646 case BIT_IOR_EXPR:
1647 low = int1l | int2l, hi = int1h | int2h;
1648 break;
6d716ca8 1649
e9a25f70
JL
1650 case BIT_XOR_EXPR:
1651 low = int1l ^ int2l, hi = int1h ^ int2h;
1652 break;
6d716ca8 1653
e9a25f70
JL
1654 case BIT_AND_EXPR:
1655 low = int1l & int2l, hi = int1h & int2h;
1656 break;
6d716ca8 1657
e9a25f70 1658 case RSHIFT_EXPR:
b6cc0a72 1659 int2l = -int2l;
e9a25f70
JL
1660 case LSHIFT_EXPR:
1661 /* It's unclear from the C standard whether shifts can overflow.
1662 The following code ignores overflow; perhaps a C standard
1663 interpretation ruling is needed. */
4c160717 1664 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
770ae6cc 1665 &low, &hi, !uns);
e9a25f70 1666 break;
6d716ca8 1667
e9a25f70
JL
1668 case RROTATE_EXPR:
1669 int2l = - int2l;
1670 case LROTATE_EXPR:
4c160717 1671 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
e9a25f70
JL
1672 &low, &hi);
1673 break;
6d716ca8 1674
e9a25f70
JL
1675 case PLUS_EXPR:
1676 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1677 break;
6d716ca8 1678
e9a25f70
JL
1679 case MINUS_EXPR:
1680 neg_double (int2l, int2h, &low, &hi);
1681 add_double (int1l, int1h, low, hi, &low, &hi);
d4b60170 1682 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
e9a25f70 1683 break;
6d716ca8 1684
e9a25f70
JL
1685 case MULT_EXPR:
1686 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1687 break;
6d716ca8 1688
e9a25f70
JL
1689 case TRUNC_DIV_EXPR:
1690 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1691 case EXACT_DIV_EXPR:
1692 /* This is a shortcut for a common special case. */
05bccae2 1693 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
455f14dd
RS
1694 && !TREE_OVERFLOW (arg1)
1695 && !TREE_OVERFLOW (arg2)
05bccae2 1696 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
e9a25f70
JL
1697 {
1698 if (code == CEIL_DIV_EXPR)
1699 int1l += int2l - 1;
05bccae2 1700
e9a25f70 1701 low = int1l / int2l, hi = 0;
6d716ca8 1702 break;
e9a25f70 1703 }
6d716ca8 1704
30f7a378 1705 /* ... fall through ... */
6d716ca8 1706
b6cc0a72 1707 case ROUND_DIV_EXPR:
fd6c76f4
RS
1708 if (int2h == 0 && int2l == 0)
1709 return NULL_TREE;
e9a25f70
JL
1710 if (int2h == 0 && int2l == 1)
1711 {
1712 low = int1l, hi = int1h;
6d716ca8 1713 break;
e9a25f70
JL
1714 }
1715 if (int1l == int2l && int1h == int2h
1716 && ! (int1l == 0 && int1h == 0))
1717 {
1718 low = 1, hi = 0;
63e7fe9b 1719 break;
e9a25f70 1720 }
4c160717 1721 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
e9a25f70
JL
1722 &low, &hi, &garbagel, &garbageh);
1723 break;
63e7fe9b 1724
e9a25f70
JL
1725 case TRUNC_MOD_EXPR:
1726 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1727 /* This is a shortcut for a common special case. */
05bccae2 1728 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
455f14dd
RS
1729 && !TREE_OVERFLOW (arg1)
1730 && !TREE_OVERFLOW (arg2)
05bccae2 1731 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
e9a25f70
JL
1732 {
1733 if (code == CEIL_MOD_EXPR)
1734 int1l += int2l - 1;
1735 low = int1l % int2l, hi = 0;
63e7fe9b 1736 break;
e9a25f70 1737 }
63e7fe9b 1738
30f7a378 1739 /* ... fall through ... */
e9a25f70 1740
b6cc0a72 1741 case ROUND_MOD_EXPR:
fd6c76f4
RS
1742 if (int2h == 0 && int2l == 0)
1743 return NULL_TREE;
e9a25f70
JL
1744 overflow = div_and_round_double (code, uns,
1745 int1l, int1h, int2l, int2h,
1746 &garbagel, &garbageh, &low, &hi);
1747 break;
1748
1749 case MIN_EXPR:
1750 case MAX_EXPR:
1751 if (uns)
d4b60170
RK
1752 low = (((unsigned HOST_WIDE_INT) int1h
1753 < (unsigned HOST_WIDE_INT) int2h)
1754 || (((unsigned HOST_WIDE_INT) int1h
1755 == (unsigned HOST_WIDE_INT) int2h)
05bccae2 1756 && int1l < int2l));
380ff34a 1757 else
05bccae2
RK
1758 low = (int1h < int2h
1759 || (int1h == int2h && int1l < int2l));
d4b60170 1760
e9a25f70
JL
1761 if (low == (code == MIN_EXPR))
1762 low = int1l, hi = int1h;
1763 else
1764 low = int2l, hi = int2h;
1765 break;
3dedc65a 1766
e9a25f70 1767 default:
fd6c76f4 1768 return NULL_TREE;
3dedc65a 1769 }
e9a25f70 1770
ca7a3bd7
NS
1771 if (notrunc)
1772 {
b8fca551
RG
1773 t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1774
ca7a3bd7
NS
1775 /* Propagate overflow flags ourselves. */
1776 if (((!uns || is_sizetype) && overflow)
1777 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
89b0433e
NS
1778 {
1779 t = copy_node (t);
1780 TREE_OVERFLOW (t) = 1;
89b0433e 1781 }
ca7a3bd7
NS
1782 }
1783 else
b8fca551
RG
1784 t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1785 ((!uns || is_sizetype) && overflow)
d95787e6 1786 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
3e6688a7 1787
e9a25f70
JL
1788 return t;
1789}
1790
d4b60170
RK
1791/* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1792 constant. We assume ARG1 and ARG2 have the same data type, or at least
858214db
EB
1793 are the same kind of constant and the same machine mode. Return zero if
1794 combining the constants is not allowed in the current operating mode.
e9a25f70
JL
1795
1796 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1797
1798static tree
fa8db1f7 1799const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
e9a25f70 1800{
858214db
EB
1801 /* Sanity check for the recursive cases. */
1802 if (!arg1 || !arg2)
1803 return NULL_TREE;
1804
b6cc0a72
KH
1805 STRIP_NOPS (arg1);
1806 STRIP_NOPS (arg2);
e9a25f70
JL
1807
1808 if (TREE_CODE (arg1) == INTEGER_CST)
4c160717 1809 return int_const_binop (code, arg1, arg2, notrunc);
e9a25f70 1810
6d716ca8
RS
1811 if (TREE_CODE (arg1) == REAL_CST)
1812 {
3e4093b6 1813 enum machine_mode mode;
79c844cd
RK
1814 REAL_VALUE_TYPE d1;
1815 REAL_VALUE_TYPE d2;
15e5ad76 1816 REAL_VALUE_TYPE value;
d284eb28
RS
1817 REAL_VALUE_TYPE result;
1818 bool inexact;
3e4093b6 1819 tree t, type;
6d716ca8 1820
fd6c76f4
RS
1821 /* The following codes are handled by real_arithmetic. */
1822 switch (code)
1823 {
1824 case PLUS_EXPR:
1825 case MINUS_EXPR:
1826 case MULT_EXPR:
1827 case RDIV_EXPR:
1828 case MIN_EXPR:
1829 case MAX_EXPR:
1830 break;
1831
1832 default:
1833 return NULL_TREE;
1834 }
1835
79c844cd
RK
1836 d1 = TREE_REAL_CST (arg1);
1837 d2 = TREE_REAL_CST (arg2);
5f610074 1838
3e4093b6
RS
1839 type = TREE_TYPE (arg1);
1840 mode = TYPE_MODE (type);
1841
1842 /* Don't perform operation if we honor signaling NaNs and
1843 either operand is a NaN. */
1844 if (HONOR_SNANS (mode)
1845 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1846 return NULL_TREE;
1847
1848 /* Don't perform operation if it would raise a division
1849 by zero exception. */
1850 if (code == RDIV_EXPR
1851 && REAL_VALUES_EQUAL (d2, dconst0)
1852 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1853 return NULL_TREE;
1854
5f610074
RK
1855 /* If either operand is a NaN, just return it. Otherwise, set up
1856 for floating-point trap; we return an overflow. */
1857 if (REAL_VALUE_ISNAN (d1))
1858 return arg1;
1859 else if (REAL_VALUE_ISNAN (d2))
1860 return arg2;
a4d3481d 1861
d284eb28
RS
1862 inexact = real_arithmetic (&value, code, &d1, &d2);
1863 real_convert (&result, mode, &value);
b6cc0a72 1864
68328cda
EB
1865 /* Don't constant fold this floating point operation if
1866 the result has overflowed and flag_trapping_math. */
68328cda
EB
1867 if (flag_trapping_math
1868 && MODE_HAS_INFINITIES (mode)
1869 && REAL_VALUE_ISINF (result)
1870 && !REAL_VALUE_ISINF (d1)
1871 && !REAL_VALUE_ISINF (d2))
1872 return NULL_TREE;
1873
d284eb28
RS
1874 /* Don't constant fold this floating point operation if the
1875 result may dependent upon the run-time rounding mode and
762297d9
RS
1876 flag_rounding_math is set, or if GCC's software emulation
1877 is unable to accurately represent the result. */
762297d9
RS
1878 if ((flag_rounding_math
1879 || (REAL_MODE_FORMAT_COMPOSITE_P (mode)
1880 && !flag_unsafe_math_optimizations))
d284eb28
RS
1881 && (inexact || !real_identical (&result, &value)))
1882 return NULL_TREE;
1883
1884 t = build_real (type, result);
649ff3b4 1885
ca7a3bd7 1886 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
7c7b029d 1887 return t;
6d716ca8 1888 }
fd6c76f4 1889
6d716ca8
RS
1890 if (TREE_CODE (arg1) == COMPLEX_CST)
1891 {
b3694847
SS
1892 tree type = TREE_TYPE (arg1);
1893 tree r1 = TREE_REALPART (arg1);
1894 tree i1 = TREE_IMAGPART (arg1);
1895 tree r2 = TREE_REALPART (arg2);
1896 tree i2 = TREE_IMAGPART (arg2);
858214db 1897 tree real, imag;
6d716ca8
RS
1898
1899 switch (code)
1900 {
1901 case PLUS_EXPR:
6d716ca8 1902 case MINUS_EXPR:
858214db
EB
1903 real = const_binop (code, r1, r2, notrunc);
1904 imag = const_binop (code, i1, i2, notrunc);
6d716ca8
RS
1905 break;
1906
1907 case MULT_EXPR:
858214db
EB
1908 real = const_binop (MINUS_EXPR,
1909 const_binop (MULT_EXPR, r1, r2, notrunc),
1910 const_binop (MULT_EXPR, i1, i2, notrunc),
1911 notrunc);
1912 imag = const_binop (PLUS_EXPR,
1913 const_binop (MULT_EXPR, r1, i2, notrunc),
1914 const_binop (MULT_EXPR, i1, r2, notrunc),
1915 notrunc);
6d716ca8
RS
1916 break;
1917
1918 case RDIV_EXPR:
1919 {
b3694847 1920 tree magsquared
6d716ca8 1921 = const_binop (PLUS_EXPR,
91d33e36
RS
1922 const_binop (MULT_EXPR, r2, r2, notrunc),
1923 const_binop (MULT_EXPR, i2, i2, notrunc),
1924 notrunc);
858214db
EB
1925 tree t1
1926 = const_binop (PLUS_EXPR,
1927 const_binop (MULT_EXPR, r1, r2, notrunc),
1928 const_binop (MULT_EXPR, i1, i2, notrunc),
1929 notrunc);
1930 tree t2
1931 = const_binop (MINUS_EXPR,
1932 const_binop (MULT_EXPR, i1, r2, notrunc),
1933 const_binop (MULT_EXPR, r1, i2, notrunc),
1934 notrunc);
c10166c4
RS
1935
1936 if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
858214db
EB
1937 code = TRUNC_DIV_EXPR;
1938
1939 real = const_binop (code, t1, magsquared, notrunc);
1940 imag = const_binop (code, t2, magsquared, notrunc);
6d716ca8
RS
1941 }
1942 break;
1943
1944 default:
fd6c76f4 1945 return NULL_TREE;
6d716ca8 1946 }
858214db
EB
1947
1948 if (real && imag)
1949 return build_complex (type, real, imag);
6d716ca8 1950 }
858214db 1951
fd6c76f4 1952 return NULL_TREE;
6d716ca8 1953}
4c160717 1954
ce552f75
NS
1955/* Create a size type INT_CST node with NUMBER sign extended. KIND
1956 indicates which particular sizetype to create. */
d4b60170 1957
fed3cef0 1958tree
3e95a7cb 1959size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
fed3cef0 1960{
ce552f75 1961 return build_int_cst (sizetype_tab[(int) kind], number);
fed3cef0 1962}
ce552f75 1963\f
fed3cef0
RK
1964/* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1965 is a tree code. The type of the result is taken from the operands.
000d8d44 1966 Both must be equivalent integer types, ala int_binop_types_match_p.
6d716ca8
RS
1967 If the operands are constant, so is the result. */
1968
1969tree
fa8db1f7 1970size_binop (enum tree_code code, tree arg0, tree arg1)
6d716ca8 1971{
fed3cef0
RK
1972 tree type = TREE_TYPE (arg0);
1973
7ebcc52c
VR
1974 if (arg0 == error_mark_node || arg1 == error_mark_node)
1975 return error_mark_node;
1976
000d8d44
RS
1977 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1978 TREE_TYPE (arg1)));
fed3cef0 1979
6d716ca8
RS
1980 /* Handle the special case of two integer constants faster. */
1981 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1982 {
1983 /* And some specific cases even faster than that. */
74890d7b
RS
1984 if (code == PLUS_EXPR)
1985 {
1986 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1987 return arg1;
1988 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1989 return arg0;
1990 }
1991 else if (code == MINUS_EXPR)
1992 {
1993 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1994 return arg0;
1995 }
1996 else if (code == MULT_EXPR)
1997 {
1998 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1999 return arg1;
2000 }
9898deac 2001
6d716ca8 2002 /* Handle general case of two integer constants. */
4c160717 2003 return int_const_binop (code, arg0, arg1, 0);
6d716ca8
RS
2004 }
2005
7f20a5b7 2006 return fold_build2 (code, type, arg0, arg1);
6d716ca8 2007}
697073d9 2008
fed3cef0
RK
2009/* Given two values, either both of sizetype or both of bitsizetype,
2010 compute the difference between the two values. Return the value
2011 in signed type corresponding to the type of the operands. */
697073d9
JM
2012
2013tree
fa8db1f7 2014size_diffop (tree arg0, tree arg1)
697073d9 2015{
fed3cef0
RK
2016 tree type = TREE_TYPE (arg0);
2017 tree ctype;
697073d9 2018
000d8d44
RS
2019 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2020 TREE_TYPE (arg1)));
697073d9 2021
fed3cef0 2022 /* If the type is already signed, just do the simple thing. */
8df83eae 2023 if (!TYPE_UNSIGNED (type))
fed3cef0
RK
2024 return size_binop (MINUS_EXPR, arg0, arg1);
2025
000d8d44
RS
2026 if (type == sizetype)
2027 ctype = ssizetype;
2028 else if (type == bitsizetype)
2029 ctype = sbitsizetype;
2030 else
12753674 2031 ctype = signed_type_for (type);
fed3cef0
RK
2032
2033 /* If either operand is not a constant, do the conversions to the signed
2034 type and subtract. The hardware will do the right thing with any
2035 overflow in the subtraction. */
2036 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
088414c1
RS
2037 return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2038 fold_convert (ctype, arg1));
fed3cef0
RK
2039
2040 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2041 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2042 overflow) and negate (which can't either). Special-case a result
2043 of zero while we're here. */
2044 if (tree_int_cst_equal (arg0, arg1))
57decb7e 2045 return build_int_cst (ctype, 0);
fed3cef0 2046 else if (tree_int_cst_lt (arg1, arg0))
088414c1 2047 return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
fed3cef0 2048 else
57decb7e 2049 return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
088414c1
RS
2050 fold_convert (ctype, size_binop (MINUS_EXPR,
2051 arg1, arg0)));
697073d9 2052}
6d716ca8 2053\f
c756af79
RH
2054/* A subroutine of fold_convert_const handling conversions of an
2055 INTEGER_CST to another integer type. */
049e524f
RS
2056
2057static tree
c756af79 2058fold_convert_const_int_from_int (tree type, tree arg1)
049e524f 2059{
c756af79 2060 tree t;
049e524f 2061
c756af79
RH
2062 /* Given an integer constant, make new constant with new type,
2063 appropriately sign-extended or truncated. */
b8fca551
RG
2064 t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2065 TREE_INT_CST_HIGH (arg1),
2066 /* Don't set the overflow when
2067 converting a pointer */
2068 !POINTER_TYPE_P (TREE_TYPE (arg1)),
2069 (TREE_INT_CST_HIGH (arg1) < 0
2070 && (TYPE_UNSIGNED (type)
2071 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
d95787e6 2072 | TREE_OVERFLOW (arg1));
049e524f 2073
c756af79 2074 return t;
049e524f
RS
2075}
2076
c756af79
RH
2077/* A subroutine of fold_convert_const handling conversions a REAL_CST
2078 to an integer type. */
6d716ca8
RS
2079
2080static tree
c756af79 2081fold_convert_const_int_from_real (enum tree_code code, tree type, tree arg1)
6d716ca8 2082{
649ff3b4 2083 int overflow = 0;
fdb33708
RS
2084 tree t;
2085
c756af79
RH
2086 /* The following code implements the floating point to integer
2087 conversion rules required by the Java Language Specification,
2088 that IEEE NaNs are mapped to zero and values that overflow
2089 the target precision saturate, i.e. values greater than
2090 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2091 are mapped to INT_MIN. These semantics are allowed by the
2092 C and C++ standards that simply state that the behavior of
2093 FP-to-integer conversion is unspecified upon overflow. */
6d716ca8 2094
c756af79
RH
2095 HOST_WIDE_INT high, low;
2096 REAL_VALUE_TYPE r;
2097 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2098
2099 switch (code)
6d716ca8 2100 {
c756af79
RH
2101 case FIX_TRUNC_EXPR:
2102 real_trunc (&r, VOIDmode, &x);
2103 break;
2104
c756af79
RH
2105 default:
2106 gcc_unreachable ();
2107 }
2108
2109 /* If R is NaN, return zero and show we have an overflow. */
2110 if (REAL_VALUE_ISNAN (r))
2111 {
2112 overflow = 1;
2113 high = 0;
2114 low = 0;
2115 }
2116
2117 /* See if R is less than the lower bound or greater than the
2118 upper bound. */
2119
2120 if (! overflow)
2121 {
2122 tree lt = TYPE_MIN_VALUE (type);
2123 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2124 if (REAL_VALUES_LESS (r, l))
6d716ca8 2125 {
c756af79
RH
2126 overflow = 1;
2127 high = TREE_INT_CST_HIGH (lt);
2128 low = TREE_INT_CST_LOW (lt);
6d716ca8 2129 }
c756af79
RH
2130 }
2131
2132 if (! overflow)
2133 {
2134 tree ut = TYPE_MAX_VALUE (type);
2135 if (ut)
6d716ca8 2136 {
c756af79
RH
2137 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2138 if (REAL_VALUES_LESS (u, r))
fdb33708 2139 {
c756af79
RH
2140 overflow = 1;
2141 high = TREE_INT_CST_HIGH (ut);
2142 low = TREE_INT_CST_LOW (ut);
2143 }
2144 }
2145 }
fdb33708 2146
c756af79
RH
2147 if (! overflow)
2148 REAL_VALUE_TO_INT (&low, &high, r);
fdb33708 2149
b8fca551 2150 t = force_fit_type_double (type, low, high, -1,
d95787e6 2151 overflow | TREE_OVERFLOW (arg1));
c756af79
RH
2152 return t;
2153}
fc627530 2154
c756af79
RH
2155/* A subroutine of fold_convert_const handling conversions a REAL_CST
2156 to another floating point type. */
fdb33708 2157
c756af79
RH
2158static tree
2159fold_convert_const_real_from_real (tree type, tree arg1)
2160{
d284eb28 2161 REAL_VALUE_TYPE value;
c756af79 2162 tree t;
e1ee5cdc 2163
d284eb28
RS
2164 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2165 t = build_real (type, value);
875eda9c 2166
c756af79 2167 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
c756af79
RH
2168 return t;
2169}
875eda9c 2170
c756af79
RH
2171/* Attempt to fold type conversion operation CODE of expression ARG1 to
2172 type TYPE. If no simplification can be done return NULL_TREE. */
875eda9c 2173
c756af79
RH
2174static tree
2175fold_convert_const (enum tree_code code, tree type, tree arg1)
2176{
2177 if (TREE_TYPE (arg1) == type)
2178 return arg1;
ca7a3bd7 2179
c756af79
RH
2180 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2181 {
2182 if (TREE_CODE (arg1) == INTEGER_CST)
2183 return fold_convert_const_int_from_int (type, arg1);
2184 else if (TREE_CODE (arg1) == REAL_CST)
2185 return fold_convert_const_int_from_real (code, type, arg1);
6d716ca8
RS
2186 }
2187 else if (TREE_CODE (type) == REAL_TYPE)
2188 {
6d716ca8
RS
2189 if (TREE_CODE (arg1) == INTEGER_CST)
2190 return build_real_from_int_cst (type, arg1);
6d716ca8 2191 if (TREE_CODE (arg1) == REAL_CST)
c756af79 2192 return fold_convert_const_real_from_real (type, arg1);
6d716ca8 2193 }
fdb33708 2194 return NULL_TREE;
6d716ca8 2195}
088414c1 2196
c756af79
RH
2197/* Construct a vector of zero elements of vector type TYPE. */
2198
2199static tree
2200build_zero_vector (tree type)
2201{
2202 tree elem, list;
2203 int i, units;
2204
2205 elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2206 units = TYPE_VECTOR_SUBPARTS (type);
2207
2208 list = NULL_TREE;
2209 for (i = 0; i < units; i++)
2210 list = tree_cons (NULL_TREE, elem, list);
2211 return build_vector (type, list);
2212}
2213
3b357646
RG
2214/* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2215
2216bool
2217fold_convertible_p (tree type, tree arg)
2218{
2219 tree orig = TREE_TYPE (arg);
2220
2221 if (type == orig)
2222 return true;
2223
2224 if (TREE_CODE (arg) == ERROR_MARK
2225 || TREE_CODE (type) == ERROR_MARK
2226 || TREE_CODE (orig) == ERROR_MARK)
2227 return false;
2228
2229 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2230 return true;
2231
2232 switch (TREE_CODE (type))
2233 {
2234 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2235 case POINTER_TYPE: case REFERENCE_TYPE:
2236 case OFFSET_TYPE:
2237 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2238 || TREE_CODE (orig) == OFFSET_TYPE)
2239 return true;
2240 return (TREE_CODE (orig) == VECTOR_TYPE
2241 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2242
2243 default:
2244 return TREE_CODE (type) == TREE_CODE (orig);
2245 }
2246}
2247
088414c1
RS
2248/* Convert expression ARG to type TYPE. Used by the middle-end for
2249 simple conversions in preference to calling the front-end's convert. */
2250
e419fe91 2251tree
088414c1
RS
2252fold_convert (tree type, tree arg)
2253{
2254 tree orig = TREE_TYPE (arg);
2255 tree tem;
2256
2257 if (type == orig)
2258 return arg;
2259
2260 if (TREE_CODE (arg) == ERROR_MARK
2261 || TREE_CODE (type) == ERROR_MARK
2262 || TREE_CODE (orig) == ERROR_MARK)
2263 return error_mark_node;
2264
b713a445
AP
2265 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig)
2266 || lang_hooks.types_compatible_p (TYPE_MAIN_VARIANT (type),
2267 TYPE_MAIN_VARIANT (orig)))
7f20a5b7 2268 return fold_build1 (NOP_EXPR, type, arg);
088414c1 2269
0bccc606 2270 switch (TREE_CODE (type))
088414c1 2271 {
71d59383 2272 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
0bccc606
NS
2273 case POINTER_TYPE: case REFERENCE_TYPE:
2274 case OFFSET_TYPE:
088414c1
RS
2275 if (TREE_CODE (arg) == INTEGER_CST)
2276 {
2277 tem = fold_convert_const (NOP_EXPR, type, arg);
2278 if (tem != NULL_TREE)
2279 return tem;
2280 }
908d0773
AP
2281 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2282 || TREE_CODE (orig) == OFFSET_TYPE)
7f20a5b7 2283 return fold_build1 (NOP_EXPR, type, arg);
088414c1
RS
2284 if (TREE_CODE (orig) == COMPLEX_TYPE)
2285 {
7f20a5b7 2286 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
088414c1
RS
2287 return fold_convert (type, tem);
2288 }
0bccc606
NS
2289 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2290 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
7f20a5b7 2291 return fold_build1 (NOP_EXPR, type, arg);
3e6688a7 2292
0bccc606 2293 case REAL_TYPE:
088414c1
RS
2294 if (TREE_CODE (arg) == INTEGER_CST)
2295 {
2296 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2297 if (tem != NULL_TREE)
2298 return tem;
2299 }
2300 else if (TREE_CODE (arg) == REAL_CST)
2301 {
2302 tem = fold_convert_const (NOP_EXPR, type, arg);
2303 if (tem != NULL_TREE)
2304 return tem;
2305 }
2306
0bccc606 2307 switch (TREE_CODE (orig))
088414c1 2308 {
71d59383 2309 case INTEGER_TYPE:
0bccc606
NS
2310 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2311 case POINTER_TYPE: case REFERENCE_TYPE:
7f20a5b7 2312 return fold_build1 (FLOAT_EXPR, type, arg);
3e6688a7 2313
0bccc606 2314 case REAL_TYPE:
4564b2d2 2315 return fold_build1 (NOP_EXPR, type, arg);
3e6688a7 2316
0bccc606 2317 case COMPLEX_TYPE:
7f20a5b7 2318 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
088414c1 2319 return fold_convert (type, tem);
3e6688a7 2320
0bccc606
NS
2321 default:
2322 gcc_unreachable ();
088414c1 2323 }
3e6688a7 2324
0bccc606
NS
2325 case COMPLEX_TYPE:
2326 switch (TREE_CODE (orig))
2327 {
71d59383 2328 case INTEGER_TYPE:
0bccc606
NS
2329 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2330 case POINTER_TYPE: case REFERENCE_TYPE:
2331 case REAL_TYPE:
2332 return build2 (COMPLEX_EXPR, type,
2333 fold_convert (TREE_TYPE (type), arg),
2334 fold_convert (TREE_TYPE (type), integer_zero_node));
2335 case COMPLEX_TYPE:
2336 {
2337 tree rpart, ipart;
3e6688a7 2338
0bccc606
NS
2339 if (TREE_CODE (arg) == COMPLEX_EXPR)
2340 {
2341 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2342 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
7f20a5b7 2343 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
0bccc606 2344 }
3e6688a7 2345
0bccc606 2346 arg = save_expr (arg);
7f20a5b7
KH
2347 rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2348 ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
0bccc606
NS
2349 rpart = fold_convert (TREE_TYPE (type), rpart);
2350 ipart = fold_convert (TREE_TYPE (type), ipart);
7f20a5b7 2351 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
0bccc606 2352 }
3e6688a7 2353
0bccc606
NS
2354 default:
2355 gcc_unreachable ();
2356 }
3e6688a7 2357
0bccc606 2358 case VECTOR_TYPE:
049e524f
RS
2359 if (integer_zerop (arg))
2360 return build_zero_vector (type);
0bccc606
NS
2361 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2362 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2363 || TREE_CODE (orig) == VECTOR_TYPE);
4d3c798d 2364 return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
088414c1 2365
0bccc606 2366 case VOID_TYPE:
bd7e4636
RS
2367 tem = fold_ignored_result (arg);
2368 if (TREE_CODE (tem) == GIMPLE_MODIFY_STMT)
2369 return tem;
2370 return fold_build1 (NOP_EXPR, type, tem);
088414c1 2371
0bccc606
NS
2372 default:
2373 gcc_unreachable ();
088414c1 2374 }
088414c1 2375}
6d716ca8 2376\f
569b7f6a 2377/* Return false if expr can be assumed not to be an lvalue, true
283da5df 2378 otherwise. */
6d716ca8 2379
283da5df
RS
2380static bool
2381maybe_lvalue_p (tree x)
6d716ca8 2382{
8d4a2ff6
RS
2383 /* We only need to wrap lvalue tree codes. */
2384 switch (TREE_CODE (x))
2385 {
2386 case VAR_DECL:
2387 case PARM_DECL:
2388 case RESULT_DECL:
2389 case LABEL_DECL:
2390 case FUNCTION_DECL:
2391 case SSA_NAME:
2392
2393 case COMPONENT_REF:
2394 case INDIRECT_REF:
7ccf35ed
DN
2395 case ALIGN_INDIRECT_REF:
2396 case MISALIGNED_INDIRECT_REF:
8d4a2ff6 2397 case ARRAY_REF:
44de5aeb 2398 case ARRAY_RANGE_REF:
8d4a2ff6 2399 case BIT_FIELD_REF:
0f59171d 2400 case OBJ_TYPE_REF:
8d4a2ff6
RS
2401
2402 case REALPART_EXPR:
2403 case IMAGPART_EXPR:
2404 case PREINCREMENT_EXPR:
2405 case PREDECREMENT_EXPR:
2406 case SAVE_EXPR:
8d4a2ff6
RS
2407 case TRY_CATCH_EXPR:
2408 case WITH_CLEANUP_EXPR:
2409 case COMPOUND_EXPR:
2410 case MODIFY_EXPR:
07beea0d 2411 case GIMPLE_MODIFY_STMT:
8d4a2ff6
RS
2412 case TARGET_EXPR:
2413 case COND_EXPR:
2414 case BIND_EXPR:
2415 case MIN_EXPR:
2416 case MAX_EXPR:
8d4a2ff6
RS
2417 break;
2418
2419 default:
2420 /* Assume the worst for front-end tree codes. */
2421 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2422 break;
283da5df 2423 return false;
8d4a2ff6 2424 }
283da5df
RS
2425
2426 return true;
2427}
2428
2429/* Return an expr equal to X but certainly not valid as an lvalue. */
2430
2431tree
2432non_lvalue (tree x)
2433{
2434 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2435 us. */
2436 if (in_gimple_form)
2437 return x;
2438
2439 if (! maybe_lvalue_p (x))
2440 return x;
6de9cd9a 2441 return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
6d716ca8 2442}
a5e9b124 2443
e9866da3
JM
2444/* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2445 Zero means allow extended lvalues. */
2446
2447int pedantic_lvalues;
2448
a5e9b124
JW
2449/* When pedantic, return an expr equal to X but certainly not valid as a
2450 pedantic lvalue. Otherwise, return X. */
2451
49995c8e 2452static tree
fa8db1f7 2453pedantic_non_lvalue (tree x)
a5e9b124 2454{
e9866da3 2455 if (pedantic_lvalues)
a5e9b124
JW
2456 return non_lvalue (x);
2457 else
2458 return x;
2459}
c05a9b68
RS
2460\f
2461/* Given a tree comparison code, return the code that is the logical inverse
2462 of the given code. It is not safe to do this for floating-point
d1a7edaf
PB
2463 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2464 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
6d716ca8 2465
227858d1 2466enum tree_code
d1a7edaf 2467invert_tree_comparison (enum tree_code code, bool honor_nans)
c05a9b68 2468{
d1a7edaf
PB
2469 if (honor_nans && flag_trapping_math)
2470 return ERROR_MARK;
2471
c05a9b68
RS
2472 switch (code)
2473 {
2474 case EQ_EXPR:
2475 return NE_EXPR;
2476 case NE_EXPR:
2477 return EQ_EXPR;
2478 case GT_EXPR:
d1a7edaf 2479 return honor_nans ? UNLE_EXPR : LE_EXPR;
c05a9b68 2480 case GE_EXPR:
d1a7edaf 2481 return honor_nans ? UNLT_EXPR : LT_EXPR;
c05a9b68 2482 case LT_EXPR:
d1a7edaf 2483 return honor_nans ? UNGE_EXPR : GE_EXPR;
c05a9b68 2484 case LE_EXPR:
d1a7edaf
PB
2485 return honor_nans ? UNGT_EXPR : GT_EXPR;
2486 case LTGT_EXPR:
2487 return UNEQ_EXPR;
2488 case UNEQ_EXPR:
2489 return LTGT_EXPR;
2490 case UNGT_EXPR:
2491 return LE_EXPR;
2492 case UNGE_EXPR:
2493 return LT_EXPR;
2494 case UNLT_EXPR:
2495 return GE_EXPR;
2496 case UNLE_EXPR:
c05a9b68 2497 return GT_EXPR;
d1a7edaf
PB
2498 case ORDERED_EXPR:
2499 return UNORDERED_EXPR;
2500 case UNORDERED_EXPR:
2501 return ORDERED_EXPR;
c05a9b68 2502 default:
0bccc606 2503 gcc_unreachable ();
c05a9b68
RS
2504 }
2505}
2506
2507/* Similar, but return the comparison that results if the operands are
2508 swapped. This is safe for floating-point. */
2509
fd660b1b 2510enum tree_code
fa8db1f7 2511swap_tree_comparison (enum tree_code code)
c05a9b68
RS
2512{
2513 switch (code)
2514 {
2515 case EQ_EXPR:
2516 case NE_EXPR:
09b2f9e8
RS
2517 case ORDERED_EXPR:
2518 case UNORDERED_EXPR:
2519 case LTGT_EXPR:
2520 case UNEQ_EXPR:
c05a9b68
RS
2521 return code;
2522 case GT_EXPR:
2523 return LT_EXPR;
2524 case GE_EXPR:
2525 return LE_EXPR;
2526 case LT_EXPR:
2527 return GT_EXPR;
2528 case LE_EXPR:
2529 return GE_EXPR;
09b2f9e8
RS
2530 case UNGT_EXPR:
2531 return UNLT_EXPR;
2532 case UNGE_EXPR:
2533 return UNLE_EXPR;
2534 case UNLT_EXPR:
2535 return UNGT_EXPR;
2536 case UNLE_EXPR:
2537 return UNGE_EXPR;
c05a9b68 2538 default:
0bccc606 2539 gcc_unreachable ();
c05a9b68
RS
2540 }
2541}
61f275ff 2542
8dcb27ed
RS
2543
2544/* Convert a comparison tree code from an enum tree_code representation
2545 into a compcode bit-based encoding. This function is the inverse of
2546 compcode_to_comparison. */
2547
d1a7edaf 2548static enum comparison_code
fa8db1f7 2549comparison_to_compcode (enum tree_code code)
8dcb27ed
RS
2550{
2551 switch (code)
2552 {
2553 case LT_EXPR:
2554 return COMPCODE_LT;
2555 case EQ_EXPR:
2556 return COMPCODE_EQ;
2557 case LE_EXPR:
2558 return COMPCODE_LE;
2559 case GT_EXPR:
2560 return COMPCODE_GT;
2561 case NE_EXPR:
2562 return COMPCODE_NE;
2563 case GE_EXPR:
2564 return COMPCODE_GE;
d1a7edaf
PB
2565 case ORDERED_EXPR:
2566 return COMPCODE_ORD;
2567 case UNORDERED_EXPR:
2568 return COMPCODE_UNORD;
2569 case UNLT_EXPR:
2570 return COMPCODE_UNLT;
2571 case UNEQ_EXPR:
2572 return COMPCODE_UNEQ;
2573 case UNLE_EXPR:
2574 return COMPCODE_UNLE;
2575 case UNGT_EXPR:
2576 return COMPCODE_UNGT;
2577 case LTGT_EXPR:
2578 return COMPCODE_LTGT;
2579 case UNGE_EXPR:
2580 return COMPCODE_UNGE;
8dcb27ed 2581 default:
0bccc606 2582 gcc_unreachable ();
8dcb27ed
RS
2583 }
2584}
2585
2586/* Convert a compcode bit-based encoding of a comparison operator back
2587 to GCC's enum tree_code representation. This function is the
2588 inverse of comparison_to_compcode. */
2589
2590static enum tree_code
d1a7edaf 2591compcode_to_comparison (enum comparison_code code)
8dcb27ed
RS
2592{
2593 switch (code)
2594 {
2595 case COMPCODE_LT:
2596 return LT_EXPR;
2597 case COMPCODE_EQ:
2598 return EQ_EXPR;
2599 case COMPCODE_LE:
2600 return LE_EXPR;
2601 case COMPCODE_GT:
2602 return GT_EXPR;
2603 case COMPCODE_NE:
2604 return NE_EXPR;
2605 case COMPCODE_GE:
2606 return GE_EXPR;
d1a7edaf
PB
2607 case COMPCODE_ORD:
2608 return ORDERED_EXPR;
2609 case COMPCODE_UNORD:
2610 return UNORDERED_EXPR;
2611 case COMPCODE_UNLT:
2612 return UNLT_EXPR;
2613 case COMPCODE_UNEQ:
2614 return UNEQ_EXPR;
2615 case COMPCODE_UNLE:
2616 return UNLE_EXPR;
2617 case COMPCODE_UNGT:
2618 return UNGT_EXPR;
2619 case COMPCODE_LTGT:
2620 return LTGT_EXPR;
2621 case COMPCODE_UNGE:
2622 return UNGE_EXPR;
8dcb27ed 2623 default:
0bccc606 2624 gcc_unreachable ();
8dcb27ed
RS
2625 }
2626}
2627
d1a7edaf
PB
2628/* Return a tree for the comparison which is the combination of
2629 doing the AND or OR (depending on CODE) of the two operations LCODE
2630 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2631 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2632 if this makes the transformation invalid. */
2633
2634tree
2635combine_comparisons (enum tree_code code, enum tree_code lcode,
2636 enum tree_code rcode, tree truth_type,
2637 tree ll_arg, tree lr_arg)
2638{
2639 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2640 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2641 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2642 enum comparison_code compcode;
2643
2644 switch (code)
2645 {
2646 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2647 compcode = lcompcode & rcompcode;
2648 break;
2649
2650 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2651 compcode = lcompcode | rcompcode;
2652 break;
2653
2654 default:
2655 return NULL_TREE;
2656 }
2657
2658 if (!honor_nans)
2659 {
2660 /* Eliminate unordered comparisons, as well as LTGT and ORD
2661 which are not used unless the mode has NaNs. */
2662 compcode &= ~COMPCODE_UNORD;
2663 if (compcode == COMPCODE_LTGT)
2664 compcode = COMPCODE_NE;
2665 else if (compcode == COMPCODE_ORD)
2666 compcode = COMPCODE_TRUE;
2667 }
2668 else if (flag_trapping_math)
2669 {
d1822754 2670 /* Check that the original operation and the optimized ones will trap
d1a7edaf
PB
2671 under the same condition. */
2672 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2673 && (lcompcode != COMPCODE_EQ)
2674 && (lcompcode != COMPCODE_ORD);
2675 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2676 && (rcompcode != COMPCODE_EQ)
2677 && (rcompcode != COMPCODE_ORD);
2678 bool trap = (compcode & COMPCODE_UNORD) == 0
2679 && (compcode != COMPCODE_EQ)
2680 && (compcode != COMPCODE_ORD);
2681
2682 /* In a short-circuited boolean expression the LHS might be
2683 such that the RHS, if evaluated, will never trap. For
2684 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2685 if neither x nor y is NaN. (This is a mixed blessing: for
2686 example, the expression above will never trap, hence
2687 optimizing it to x < y would be invalid). */
2688 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2689 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2690 rtrap = false;
2691
2692 /* If the comparison was short-circuited, and only the RHS
2693 trapped, we may now generate a spurious trap. */
2694 if (rtrap && !ltrap
2695 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2696 return NULL_TREE;
2697
2698 /* If we changed the conditions that cause a trap, we lose. */
2699 if ((ltrap || rtrap) != trap)
2700 return NULL_TREE;
2701 }
2702
2703 if (compcode == COMPCODE_TRUE)
1b0f3e79 2704 return constant_boolean_node (true, truth_type);
d1a7edaf 2705 else if (compcode == COMPCODE_FALSE)
1b0f3e79 2706 return constant_boolean_node (false, truth_type);
d1a7edaf 2707 else
7f20a5b7
KH
2708 return fold_build2 (compcode_to_comparison (compcode),
2709 truth_type, ll_arg, lr_arg);
d1a7edaf
PB
2710}
2711
61f275ff
RK
2712/* Return nonzero if CODE is a tree code that represents a truth value. */
2713
2714static int
fa8db1f7 2715truth_value_p (enum tree_code code)
61f275ff 2716{
6615c446 2717 return (TREE_CODE_CLASS (code) == tcc_comparison
61f275ff
RK
2718 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2719 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2720 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2721}
c05a9b68 2722\f
fae111c1
RS
2723/* Return nonzero if two operands (typically of the same tree node)
2724 are necessarily equal. If either argument has side-effects this
1ea7e6ad 2725 function returns zero. FLAGS modifies behavior as follows:
fae111c1 2726
6de9cd9a 2727 If OEP_ONLY_CONST is set, only return nonzero for constants.
6a1746af
RS
2728 This function tests whether the operands are indistinguishable;
2729 it does not test whether they are equal using C's == operation.
2730 The distinction is important for IEEE floating point, because
2731 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
fae111c1
RS
2732 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2733
6de9cd9a 2734 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
fae111c1
RS
2735 even though it may hold multiple values during a function.
2736 This is because a GCC tree node guarantees that nothing else is
2737 executed between the evaluation of its "operands" (which may often
2738 be evaluated in arbitrary order). Hence if the operands themselves
2739 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3dd8069d
PB
2740 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2741 unset means assuming isochronic (or instantaneous) tree equivalence.
2742 Unless comparing arbitrary expression trees, such as from different
2743 statements, this flag can usually be left unset.
6de9cd9a
DN
2744
2745 If OEP_PURE_SAME is set, then pure functions with identical arguments
2746 are considered the same. It is used when the caller has other ways
2747 to ensure that global memory is unchanged in between. */
6d716ca8
RS
2748
2749int
6de9cd9a 2750operand_equal_p (tree arg0, tree arg1, unsigned int flags)
6d716ca8 2751{
8df83eae 2752 /* If either is ERROR_MARK, they aren't equal. */
624b15fa 2753 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
8df83eae
RK
2754 return 0;
2755
6d716ca8
RS
2756 /* If both types don't have the same signedness, then we can't consider
2757 them equal. We must check this before the STRIP_NOPS calls
2758 because they may change the signedness of the arguments. */
8df83eae 2759 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
6d716ca8
RS
2760 return 0;
2761
096dce1b
RG
2762 /* If both types don't have the same precision, then it is not safe
2763 to strip NOPs. */
2764 if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2765 return 0;
2766
6d716ca8
RS
2767 STRIP_NOPS (arg0);
2768 STRIP_NOPS (arg1);
2769
a04d8591
RG
2770 /* In case both args are comparisons but with different comparison
2771 code, try to swap the comparison operands of one arg to produce
2772 a match and compare that variant. */
2773 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2774 && COMPARISON_CLASS_P (arg0)
2775 && COMPARISON_CLASS_P (arg1))
2776 {
2777 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2778
2779 if (TREE_CODE (arg0) == swap_code)
2780 return operand_equal_p (TREE_OPERAND (arg0, 0),
2781 TREE_OPERAND (arg1, 1), flags)
2782 && operand_equal_p (TREE_OPERAND (arg0, 1),
2783 TREE_OPERAND (arg1, 0), flags);
2784 }
2785
c7cfe938
RK
2786 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2787 /* This is needed for conversions and for COMPONENT_REF.
2788 Might as well play it safe and always test this. */
e89a9554
ZW
2789 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2790 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
c7cfe938 2791 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
6d716ca8
RS
2792 return 0;
2793
c7cfe938
RK
2794 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2795 We don't care about side effects in that case because the SAVE_EXPR
2796 takes care of that for us. In all other cases, two expressions are
2797 equal if they have no side effects. If we have two identical
2798 expressions with side effects that should be treated the same due
2799 to the only side effects being identical SAVE_EXPR's, that will
2800 be detected in the recursive calls below. */
6de9cd9a 2801 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
c7cfe938
RK
2802 && (TREE_CODE (arg0) == SAVE_EXPR
2803 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
6d716ca8
RS
2804 return 1;
2805
c7cfe938
RK
2806 /* Next handle constant cases, those for which we can return 1 even
2807 if ONLY_CONST is set. */
2808 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2809 switch (TREE_CODE (arg0))
2810 {
2811 case INTEGER_CST:
85914552 2812 return tree_int_cst_equal (arg0, arg1);
c7cfe938
RK
2813
2814 case REAL_CST:
0446c9f3
ZD
2815 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2816 TREE_REAL_CST (arg1)))
2817 return 1;
2818
2819
2820 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2821 {
2822 /* If we do not distinguish between signed and unsigned zero,
2823 consider them equal. */
2824 if (real_zerop (arg0) && real_zerop (arg1))
2825 return 1;
2826 }
2827 return 0;
c7cfe938 2828
69ef87e2
AH
2829 case VECTOR_CST:
2830 {
2831 tree v1, v2;
2832
69ef87e2
AH
2833 v1 = TREE_VECTOR_CST_ELTS (arg0);
2834 v2 = TREE_VECTOR_CST_ELTS (arg1);
2835 while (v1 && v2)
2836 {
875427f0 2837 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
6de9cd9a 2838 flags))
69ef87e2
AH
2839 return 0;
2840 v1 = TREE_CHAIN (v1);
2841 v2 = TREE_CHAIN (v2);
2842 }
2843
40182dbf 2844 return v1 == v2;
69ef87e2
AH
2845 }
2846
c7cfe938
RK
2847 case COMPLEX_CST:
2848 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
6de9cd9a 2849 flags)
c7cfe938 2850 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
6de9cd9a 2851 flags));
c7cfe938
RK
2852
2853 case STRING_CST:
2854 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
71145810 2855 && ! memcmp (TREE_STRING_POINTER (arg0),
c7cfe938
RK
2856 TREE_STRING_POINTER (arg1),
2857 TREE_STRING_LENGTH (arg0)));
2858
2859 case ADDR_EXPR:
2860 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2861 0);
e9a25f70
JL
2862 default:
2863 break;
c7cfe938 2864 }
6d716ca8 2865
6de9cd9a 2866 if (flags & OEP_ONLY_CONST)
6d716ca8
RS
2867 return 0;
2868
38318b73 2869/* Define macros to test an operand from arg0 and arg1 for equality and a
624b15fa
RK
2870 variant that allows null and views null as being different from any
2871 non-null value. In the latter case, if either is null, the both
2872 must be; otherwise, do the normal comparison. */
2873#define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2874 TREE_OPERAND (arg1, N), flags)
2875
2876#define OP_SAME_WITH_NULL(N) \
2877 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2878 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2879
6d716ca8
RS
2880 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2881 {
6615c446 2882 case tcc_unary:
6d716ca8 2883 /* Two conversions are equal only if signedness and modes match. */
266bff3a
JJ
2884 switch (TREE_CODE (arg0))
2885 {
2886 case NOP_EXPR:
2887 case CONVERT_EXPR:
266bff3a 2888 case FIX_TRUNC_EXPR:
266bff3a
JJ
2889 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2890 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2891 return 0;
2892 break;
2893 default:
2894 break;
2895 }
6d716ca8 2896
624b15fa
RK
2897 return OP_SAME (0);
2898
6d716ca8 2899
6615c446
JO
2900 case tcc_comparison:
2901 case tcc_binary:
624b15fa 2902 if (OP_SAME (0) && OP_SAME (1))
c7cfe938
RK
2903 return 1;
2904
2905 /* For commutative ops, allow the other order. */
3168cb99 2906 return (commutative_tree_code (TREE_CODE (arg0))
c7cfe938 2907 && operand_equal_p (TREE_OPERAND (arg0, 0),
6de9cd9a 2908 TREE_OPERAND (arg1, 1), flags)
6d716ca8 2909 && operand_equal_p (TREE_OPERAND (arg0, 1),
6de9cd9a 2910 TREE_OPERAND (arg1, 0), flags));
6d716ca8 2911
6615c446 2912 case tcc_reference:
21c43754
RS
2913 /* If either of the pointer (or reference) expressions we are
2914 dereferencing contain a side effect, these cannot be equal. */
05ca5990
GRK
2915 if (TREE_SIDE_EFFECTS (arg0)
2916 || TREE_SIDE_EFFECTS (arg1))
2917 return 0;
2918
6d716ca8
RS
2919 switch (TREE_CODE (arg0))
2920 {
2921 case INDIRECT_REF:
7ccf35ed
DN
2922 case ALIGN_INDIRECT_REF:
2923 case MISALIGNED_INDIRECT_REF:
497be978
RH
2924 case REALPART_EXPR:
2925 case IMAGPART_EXPR:
624b15fa 2926 return OP_SAME (0);
6d716ca8 2927
6d716ca8 2928 case ARRAY_REF:
b4e3fabb 2929 case ARRAY_RANGE_REF:
5852948c
RG
2930 /* Operands 2 and 3 may be null.
2931 Compare the array index by value if it is constant first as we
2932 may have different types but same value here. */
624b15fa 2933 return (OP_SAME (0)
5852948c
RG
2934 && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2935 TREE_OPERAND (arg1, 1))
2936 || OP_SAME (1))
624b15fa
RK
2937 && OP_SAME_WITH_NULL (2)
2938 && OP_SAME_WITH_NULL (3));
462fdcce
RK
2939
2940 case COMPONENT_REF:
78b76d08
SB
2941 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2942 may be NULL when we're called to compare MEM_EXPRs. */
2943 return OP_SAME_WITH_NULL (0)
2944 && OP_SAME (1)
2945 && OP_SAME_WITH_NULL (2);
a60749f5 2946
40b32ef8 2947 case BIT_FIELD_REF:
624b15fa
RK
2948 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2949
e9a25f70
JL
2950 default:
2951 return 0;
6d716ca8 2952 }
45f97e2e 2953
6615c446 2954 case tcc_expression:
1bfedcc8
JM
2955 switch (TREE_CODE (arg0))
2956 {
2957 case ADDR_EXPR:
2958 case TRUTH_NOT_EXPR:
624b15fa 2959 return OP_SAME (0);
1bfedcc8 2960
54d581a2
RS
2961 case TRUTH_ANDIF_EXPR:
2962 case TRUTH_ORIF_EXPR:
624b15fa 2963 return OP_SAME (0) && OP_SAME (1);
54d581a2
RS
2964
2965 case TRUTH_AND_EXPR:
2966 case TRUTH_OR_EXPR:
2967 case TRUTH_XOR_EXPR:
624b15fa
RK
2968 if (OP_SAME (0) && OP_SAME (1))
2969 return 1;
2970
2971 /* Otherwise take into account this is a commutative operation. */
54d581a2 2972 return (operand_equal_p (TREE_OPERAND (arg0, 0),
624b15fa 2973 TREE_OPERAND (arg1, 1), flags)
54d581a2 2974 && operand_equal_p (TREE_OPERAND (arg0, 1),
624b15fa 2975 TREE_OPERAND (arg1, 0), flags));
54d581a2 2976
5039610b
SL
2977 default:
2978 return 0;
2979 }
2980
2981 case tcc_vl_exp:
2982 switch (TREE_CODE (arg0))
2983 {
21c43754
RS
2984 case CALL_EXPR:
2985 /* If the CALL_EXPRs call different functions, then they
2986 clearly can not be equal. */
5039610b
SL
2987 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2988 flags))
21c43754
RS
2989 return 0;
2990
6de9cd9a
DN
2991 {
2992 unsigned int cef = call_expr_flags (arg0);
2993 if (flags & OEP_PURE_SAME)
2994 cef &= ECF_CONST | ECF_PURE;
2995 else
2996 cef &= ECF_CONST;
2997 if (!cef)
2998 return 0;
2999 }
21c43754 3000
5039610b
SL
3001 /* Now see if all the arguments are the same. */
3002 {
3003 call_expr_arg_iterator iter0, iter1;
3004 tree a0, a1;
3005 for (a0 = first_call_expr_arg (arg0, &iter0),
3006 a1 = first_call_expr_arg (arg1, &iter1);
3007 a0 && a1;
3008 a0 = next_call_expr_arg (&iter0),
3009 a1 = next_call_expr_arg (&iter1))
3010 if (! operand_equal_p (a0, a1, flags))
21c43754
RS
3011 return 0;
3012
5039610b
SL
3013 /* If we get here and both argument lists are exhausted
3014 then the CALL_EXPRs are equal. */
3015 return ! (a0 || a1);
3016 }
1bfedcc8
JM
3017 default:
3018 return 0;
3019 }
b6cc0a72 3020
6615c446 3021 case tcc_declaration:
6de9cd9a
DN
3022 /* Consider __builtin_sqrt equal to sqrt. */
3023 return (TREE_CODE (arg0) == FUNCTION_DECL
3024 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3025 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3026 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
21c43754 3027
e9a25f70
JL
3028 default:
3029 return 0;
6d716ca8 3030 }
624b15fa
RK
3031
3032#undef OP_SAME
3033#undef OP_SAME_WITH_NULL
6d716ca8 3034}
c05a9b68
RS
3035\f
3036/* Similar to operand_equal_p, but see if ARG0 might have been made by
b6cc0a72 3037 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
6d716ca8 3038
6d716ca8
RS
3039 When in doubt, return 0. */
3040
b6cc0a72 3041static int
fa8db1f7 3042operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
6d716ca8 3043{
c05a9b68 3044 int unsignedp1, unsignedpo;
52de9b6c 3045 tree primarg0, primarg1, primother;
770ae6cc 3046 unsigned int correct_width;
6d716ca8 3047
c05a9b68 3048 if (operand_equal_p (arg0, arg1, 0))
6d716ca8
RS
3049 return 1;
3050
0982a4b8
JM
3051 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3052 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
6d716ca8
RS
3053 return 0;
3054
52de9b6c
RK
3055 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3056 and see if the inner values are the same. This removes any
3057 signedness comparison, which doesn't matter here. */
3058 primarg0 = arg0, primarg1 = arg1;
b6cc0a72
KH
3059 STRIP_NOPS (primarg0);
3060 STRIP_NOPS (primarg1);
52de9b6c
RK
3061 if (operand_equal_p (primarg0, primarg1, 0))
3062 return 1;
3063
c05a9b68
RS
3064 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3065 actual comparison operand, ARG0.
6d716ca8 3066
c05a9b68 3067 First throw away any conversions to wider types
6d716ca8 3068 already present in the operands. */
6d716ca8 3069
c05a9b68
RS
3070 primarg1 = get_narrower (arg1, &unsignedp1);
3071 primother = get_narrower (other, &unsignedpo);
3072
3073 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3074 if (unsignedp1 == unsignedpo
3075 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3076 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
6d716ca8 3077 {
c05a9b68 3078 tree type = TREE_TYPE (arg0);
6d716ca8
RS
3079
3080 /* Make sure shorter operand is extended the right way
3081 to match the longer operand. */
12753674 3082 primarg1 = fold_convert (signed_or_unsigned_type_for
088414c1 3083 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
6d716ca8 3084
088414c1 3085 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
6d716ca8
RS
3086 return 1;
3087 }
3088
3089 return 0;
3090}
3091\f
f72aed24 3092/* See if ARG is an expression that is either a comparison or is performing
c05a9b68
RS
3093 arithmetic on comparisons. The comparisons must only be comparing
3094 two different values, which will be stored in *CVAL1 and *CVAL2; if
cc2902df 3095 they are nonzero it means that some operands have already been found.
c05a9b68 3096 No variables may be used anywhere else in the expression except in the
35e66bd1
RK
3097 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3098 the expression and save_expr needs to be called with CVAL1 and CVAL2.
c05a9b68
RS
3099
3100 If this is true, return 1. Otherwise, return zero. */
3101
3102static int
fa8db1f7 3103twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
c05a9b68
RS
3104{
3105 enum tree_code code = TREE_CODE (arg);
6615c446 3106 enum tree_code_class class = TREE_CODE_CLASS (code);
c05a9b68 3107
6615c446
JO
3108 /* We can handle some of the tcc_expression cases here. */
3109 if (class == tcc_expression && code == TRUTH_NOT_EXPR)
3110 class = tcc_unary;
3111 else if (class == tcc_expression
c05a9b68
RS
3112 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3113 || code == COMPOUND_EXPR))
6615c446 3114 class = tcc_binary;
2315a5db 3115
6615c446 3116 else if (class == tcc_expression && code == SAVE_EXPR
d4b60170 3117 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
35e66bd1
RK
3118 {
3119 /* If we've already found a CVAL1 or CVAL2, this expression is
3120 two complex to handle. */
3121 if (*cval1 || *cval2)
3122 return 0;
3123
6615c446 3124 class = tcc_unary;
35e66bd1
RK
3125 *save_p = 1;
3126 }
c05a9b68
RS
3127
3128 switch (class)
3129 {
6615c446 3130 case tcc_unary:
35e66bd1 3131 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
c05a9b68 3132
6615c446 3133 case tcc_binary:
35e66bd1
RK
3134 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3135 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3136 cval1, cval2, save_p));
c05a9b68 3137
6615c446 3138 case tcc_constant:
c05a9b68
RS
3139 return 1;
3140
6615c446 3141 case tcc_expression:
c05a9b68 3142 if (code == COND_EXPR)
35e66bd1
RK
3143 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3144 cval1, cval2, save_p)
3145 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3146 cval1, cval2, save_p)
c05a9b68 3147 && twoval_comparison_p (TREE_OPERAND (arg, 2),
35e66bd1 3148 cval1, cval2, save_p));
c05a9b68 3149 return 0;
b6cc0a72 3150
6615c446 3151 case tcc_comparison:
c05a9b68
RS
3152 /* First see if we can handle the first operand, then the second. For
3153 the second operand, we know *CVAL1 can't be zero. It must be that
3154 one side of the comparison is each of the values; test for the
3155 case where this isn't true by failing if the two operands
3156 are the same. */
3157
3158 if (operand_equal_p (TREE_OPERAND (arg, 0),
3159 TREE_OPERAND (arg, 1), 0))
3160 return 0;
3161
3162 if (*cval1 == 0)
3163 *cval1 = TREE_OPERAND (arg, 0);
3164 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3165 ;
3166 else if (*cval2 == 0)
3167 *cval2 = TREE_OPERAND (arg, 0);
3168 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3169 ;
3170 else
3171 return 0;
3172
3173 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3174 ;
3175 else if (*cval2 == 0)
3176 *cval2 = TREE_OPERAND (arg, 1);
3177 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3178 ;
3179 else
3180 return 0;
3181
3182 return 1;
c05a9b68 3183
e9a25f70
JL
3184 default:
3185 return 0;
3186 }
c05a9b68
RS
3187}
3188\f
3189/* ARG is a tree that is known to contain just arithmetic operations and
3190 comparisons. Evaluate the operations in the tree substituting NEW0 for
f72aed24 3191 any occurrence of OLD0 as an operand of a comparison and likewise for
c05a9b68
RS
3192 NEW1 and OLD1. */
3193
3194static tree
fa8db1f7 3195eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
c05a9b68
RS
3196{
3197 tree type = TREE_TYPE (arg);
3198 enum tree_code code = TREE_CODE (arg);
6615c446 3199 enum tree_code_class class = TREE_CODE_CLASS (code);
c05a9b68 3200
6615c446
JO
3201 /* We can handle some of the tcc_expression cases here. */
3202 if (class == tcc_expression && code == TRUTH_NOT_EXPR)
3203 class = tcc_unary;
3204 else if (class == tcc_expression
c05a9b68 3205 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
6615c446 3206 class = tcc_binary;
c05a9b68
RS
3207
3208 switch (class)
3209 {
6615c446 3210 case tcc_unary:
7f20a5b7
KH
3211 return fold_build1 (code, type,
3212 eval_subst (TREE_OPERAND (arg, 0),
3213 old0, new0, old1, new1));
c05a9b68 3214
6615c446 3215 case tcc_binary:
7f20a5b7
KH
3216 return fold_build2 (code, type,
3217 eval_subst (TREE_OPERAND (arg, 0),
3218 old0, new0, old1, new1),
3219 eval_subst (TREE_OPERAND (arg, 1),
3220 old0, new0, old1, new1));
c05a9b68 3221
6615c446 3222 case tcc_expression:
c05a9b68
RS
3223 switch (code)
3224 {
3225 case SAVE_EXPR:
3226 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3227
3228 case COMPOUND_EXPR:
3229 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3230
3231 case COND_EXPR:
7f20a5b7
KH
3232 return fold_build3 (code, type,
3233 eval_subst (TREE_OPERAND (arg, 0),
3234 old0, new0, old1, new1),
3235 eval_subst (TREE_OPERAND (arg, 1),
3236 old0, new0, old1, new1),
3237 eval_subst (TREE_OPERAND (arg, 2),
3238 old0, new0, old1, new1));
e9a25f70
JL
3239 default:
3240 break;
c05a9b68 3241 }
938d968e 3242 /* Fall through - ??? */
c05a9b68 3243
6615c446 3244 case tcc_comparison:
c05a9b68
RS
3245 {
3246 tree arg0 = TREE_OPERAND (arg, 0);
3247 tree arg1 = TREE_OPERAND (arg, 1);
3248
3249 /* We need to check both for exact equality and tree equality. The
3250 former will be true if the operand has a side-effect. In that
3251 case, we know the operand occurred exactly once. */
3252
3253 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3254 arg0 = new0;
3255 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3256 arg0 = new1;
3257
3258 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3259 arg1 = new0;
3260 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3261 arg1 = new1;
3262
7f20a5b7 3263 return fold_build2 (code, type, arg0, arg1);
c05a9b68 3264 }
c05a9b68 3265
e9a25f70
JL
3266 default:
3267 return arg;
3268 }
c05a9b68
RS
3269}
3270\f
6d716ca8
RS
3271/* Return a tree for the case when the result of an expression is RESULT
3272 converted to TYPE and OMITTED was previously an operand of the expression
3273 but is now not needed (e.g., we folded OMITTED * 0).
3274
3275 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3276 the conversion of RESULT to TYPE. */
3277
c0a47a61 3278tree
fa8db1f7 3279omit_one_operand (tree type, tree result, tree omitted)
6d716ca8 3280{
088414c1 3281 tree t = fold_convert (type, result);
6d716ca8
RS
3282
3283 if (TREE_SIDE_EFFECTS (omitted))
9675412f 3284 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
6d716ca8 3285
d023bff9 3286 return non_lvalue (t);
6d716ca8 3287}
4ab3cb65
RK
3288
3289/* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
3290
3291static tree
fa8db1f7 3292pedantic_omit_one_operand (tree type, tree result, tree omitted)
4ab3cb65 3293{
088414c1 3294 tree t = fold_convert (type, result);
4ab3cb65
RK
3295
3296 if (TREE_SIDE_EFFECTS (omitted))
9675412f 3297 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
4ab3cb65
RK
3298
3299 return pedantic_non_lvalue (t);
3300}
08039bd8
RS
3301
3302/* Return a tree for the case when the result of an expression is RESULT
3303 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3304 of the expression but are now not needed.
3305
3306 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3307 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3308 evaluated before OMITTED2. Otherwise, if neither has side effects,
3309 just do the conversion of RESULT to TYPE. */
3310
3311tree
3312omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3313{
3314 tree t = fold_convert (type, result);
3315
3316 if (TREE_SIDE_EFFECTS (omitted2))
3317 t = build2 (COMPOUND_EXPR, type, omitted2, t);
3318 if (TREE_SIDE_EFFECTS (omitted1))
3319 t = build2 (COMPOUND_EXPR, type, omitted1, t);
3320
3321 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3322}
3323
6d716ca8 3324\f
3f783329
RS
3325/* Return a simplified tree node for the truth-negation of ARG. This
3326 never alters ARG itself. We assume that ARG is an operation that
d1a7edaf 3327 returns a truth value (0 or 1).
6d716ca8 3328
d1a7edaf
PB
3329 FIXME: one would think we would fold the result, but it causes
3330 problems with the dominator optimizer. */
d817ed3b 3331
6d716ca8 3332tree
d817ed3b 3333fold_truth_not_expr (tree arg)
6d716ca8
RS
3334{
3335 tree type = TREE_TYPE (arg);
c05a9b68 3336 enum tree_code code = TREE_CODE (arg);
6d716ca8 3337
c05a9b68
RS
3338 /* If this is a comparison, we can simply invert it, except for
3339 floating-point non-equality comparisons, in which case we just
3340 enclose a TRUTH_NOT_EXPR around what we have. */
6d716ca8 3341
6615c446 3342 if (TREE_CODE_CLASS (code) == tcc_comparison)
6d716ca8 3343 {
d1a7edaf
PB
3344 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3345 if (FLOAT_TYPE_P (op_type)
3346 && flag_trapping_math
3347 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3348 && code != NE_EXPR && code != EQ_EXPR)
d817ed3b 3349 return NULL_TREE;
c05a9b68 3350 else
d1a7edaf
PB
3351 {
3352 code = invert_tree_comparison (code,
3353 HONOR_NANS (TYPE_MODE (op_type)));
3354 if (code == ERROR_MARK)
d817ed3b 3355 return NULL_TREE;
d1a7edaf
PB
3356 else
3357 return build2 (code, type,
3358 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3359 }
c05a9b68 3360 }
6d716ca8 3361
c05a9b68
RS
3362 switch (code)
3363 {
6d716ca8 3364 case INTEGER_CST:
9ace7f9e 3365 return constant_boolean_node (integer_zerop (arg), type);
6d716ca8
RS
3366
3367 case TRUTH_AND_EXPR:
59ce6d6b
RS
3368 return build2 (TRUTH_OR_EXPR, type,
3369 invert_truthvalue (TREE_OPERAND (arg, 0)),
3370 invert_truthvalue (TREE_OPERAND (arg, 1)));
6d716ca8
RS
3371
3372 case TRUTH_OR_EXPR:
59ce6d6b
RS
3373 return build2 (TRUTH_AND_EXPR, type,
3374 invert_truthvalue (TREE_OPERAND (arg, 0)),
3375 invert_truthvalue (TREE_OPERAND (arg, 1)));
6d716ca8 3376
772447c5
RK
3377 case TRUTH_XOR_EXPR:
3378 /* Here we can invert either operand. We invert the first operand
3379 unless the second operand is a TRUTH_NOT_EXPR in which case our
3380 result is the XOR of the first operand with the inside of the
3381 negation of the second operand. */
3382
3383 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
59ce6d6b
RS
3384 return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3385 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
772447c5 3386 else
59ce6d6b
RS
3387 return build2 (TRUTH_XOR_EXPR, type,
3388 invert_truthvalue (TREE_OPERAND (arg, 0)),
3389 TREE_OPERAND (arg, 1));
772447c5 3390
6d716ca8 3391 case TRUTH_ANDIF_EXPR:
59ce6d6b
RS
3392 return build2 (TRUTH_ORIF_EXPR, type,
3393 invert_truthvalue (TREE_OPERAND (arg, 0)),
3394 invert_truthvalue (TREE_OPERAND (arg, 1)));
6d716ca8
RS
3395
3396 case TRUTH_ORIF_EXPR:
59ce6d6b
RS
3397 return build2 (TRUTH_ANDIF_EXPR, type,
3398 invert_truthvalue (TREE_OPERAND (arg, 0)),
3399 invert_truthvalue (TREE_OPERAND (arg, 1)));
6d716ca8
RS
3400
3401 case TRUTH_NOT_EXPR:
3402 return TREE_OPERAND (arg, 0);
3403
3404 case COND_EXPR:
9ca4afb9
RG
3405 {
3406 tree arg1 = TREE_OPERAND (arg, 1);
3407 tree arg2 = TREE_OPERAND (arg, 2);
3408 /* A COND_EXPR may have a throw as one operand, which
3409 then has void type. Just leave void operands
3410 as they are. */
3411 return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3412 VOID_TYPE_P (TREE_TYPE (arg1))
3413 ? arg1 : invert_truthvalue (arg1),
3414 VOID_TYPE_P (TREE_TYPE (arg2))
3415 ? arg2 : invert_truthvalue (arg2));
3416 }
6d716ca8 3417
ef9fe0da 3418 case COMPOUND_EXPR:
59ce6d6b
RS
3419 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3420 invert_truthvalue (TREE_OPERAND (arg, 1)));
ef9fe0da 3421
6d716ca8
RS
3422 case NON_LVALUE_EXPR:
3423 return invert_truthvalue (TREE_OPERAND (arg, 0));
3424
3425 case NOP_EXPR:
6de9cd9a 3426 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
d817ed3b 3427 return build1 (TRUTH_NOT_EXPR, type, arg);
6de9cd9a 3428
6d716ca8
RS
3429 case CONVERT_EXPR:
3430 case FLOAT_EXPR:
3431 return build1 (TREE_CODE (arg), type,
3432 invert_truthvalue (TREE_OPERAND (arg, 0)));
3433
3434 case BIT_AND_EXPR:
efc1a4d9
PB
3435 if (!integer_onep (TREE_OPERAND (arg, 1)))
3436 break;
59ce6d6b 3437 return build2 (EQ_EXPR, type, arg,
57decb7e 3438 build_int_cst (type, 0));
6d716ca8 3439
dfa90b42
RS
3440 case SAVE_EXPR:
3441 return build1 (TRUTH_NOT_EXPR, type, arg);
a25ee332
RK
3442
3443 case CLEANUP_POINT_EXPR:
3444 return build1 (CLEANUP_POINT_EXPR, type,
3445 invert_truthvalue (TREE_OPERAND (arg, 0)));
e9a25f70
JL
3446
3447 default:
3448 break;
efc1a4d9 3449 }
d817ed3b
RG
3450
3451 return NULL_TREE;
3452}
3453
3454/* Return a simplified tree node for the truth-negation of ARG. This
3455 never alters ARG itself. We assume that ARG is an operation that
3456 returns a truth value (0 or 1).
3457
3458 FIXME: one would think we would fold the result, but it causes
3459 problems with the dominator optimizer. */
3460
3461tree
3462invert_truthvalue (tree arg)
3463{
3464 tree tem;
3465
3466 if (TREE_CODE (arg) == ERROR_MARK)
3467 return arg;
3468
3469 tem = fold_truth_not_expr (arg);
3470 if (!tem)
3471 tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3472
3473 return tem;
6d716ca8
RS
3474}
3475
3476/* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3477 operands are another bit-wise operation with a common input. If so,
3478 distribute the bit operations to save an operation and possibly two if
3479 constants are involved. For example, convert
fa8db1f7 3480 (A | B) & (A | C) into A | (B & C)
6d716ca8
RS
3481 Further simplification will occur if B and C are constants.
3482
3483 If this optimization cannot be done, 0 will be returned. */
3484
3485static tree
fa8db1f7 3486distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
6d716ca8
RS
3487{
3488 tree common;
3489 tree left, right;
3490
3491 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3492 || TREE_CODE (arg0) == code
fced8ba3
RS
3493 || (TREE_CODE (arg0) != BIT_AND_EXPR
3494 && TREE_CODE (arg0) != BIT_IOR_EXPR))
6d716ca8
RS
3495 return 0;
3496
3497 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3498 {
3499 common = TREE_OPERAND (arg0, 0);
3500 left = TREE_OPERAND (arg0, 1);
3501 right = TREE_OPERAND (arg1, 1);
3502 }
3503 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3504 {
3505 common = TREE_OPERAND (arg0, 0);
3506 left = TREE_OPERAND (arg0, 1);
3507 right = TREE_OPERAND (arg1, 0);
3508 }
3509 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3510 {
3511 common = TREE_OPERAND (arg0, 1);
3512 left = TREE_OPERAND (arg0, 0);
3513 right = TREE_OPERAND (arg1, 1);
3514 }
3515 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3516 {
3517 common = TREE_OPERAND (arg0, 1);
3518 left = TREE_OPERAND (arg0, 0);
3519 right = TREE_OPERAND (arg1, 0);
3520 }
3521 else
3522 return 0;
3523
7f20a5b7
KH
3524 return fold_build2 (TREE_CODE (arg0), type, common,
3525 fold_build2 (code, type, left, right));
6d716ca8 3526}
f8912a55
PB
3527
3528/* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3529 with code CODE. This optimization is unsafe. */
3530static tree
3531distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3532{
3533 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3534 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3535
3536 /* (A / C) +- (B / C) -> (A +- B) / C. */
3537 if (mul0 == mul1
3538 && operand_equal_p (TREE_OPERAND (arg0, 1),
3539 TREE_OPERAND (arg1, 1), 0))
3540 return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3541 fold_build2 (code, type,
3542 TREE_OPERAND (arg0, 0),
3543 TREE_OPERAND (arg1, 0)),
3544 TREE_OPERAND (arg0, 1));
3545
3546 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3547 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3548 TREE_OPERAND (arg1, 0), 0)
3549 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3550 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3551 {
3552 REAL_VALUE_TYPE r0, r1;
3553 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3554 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3555 if (!mul0)
3556 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3557 if (!mul1)
3558 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3559 real_arithmetic (&r0, code, &r0, &r1);
3560 return fold_build2 (MULT_EXPR, type,
3561 TREE_OPERAND (arg0, 0),
3562 build_real (type, r0));
3563 }
3564
3565 return NULL_TREE;
3566}
6d716ca8
RS
3567\f
3568/* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
cc2902df 3569 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
6d716ca8
RS
3570
3571static tree
75040a04
AJ
3572make_bit_field_ref (tree inner, tree type, int bitsize, int bitpos,
3573 int unsignedp)
6d716ca8 3574{
97e9692b
JJ
3575 tree result;
3576
3577 if (bitpos == 0)
3578 {
3579 tree size = TYPE_SIZE (TREE_TYPE (inner));
3580 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3581 || POINTER_TYPE_P (TREE_TYPE (inner)))
3582 && host_integerp (size, 0)
3583 && tree_low_cst (size, 0) == bitsize)
3584 return fold_convert (type, inner);
3585 }
3586
3587 result = build3 (BIT_FIELD_REF, type, inner,
3588 size_int (bitsize), bitsize_int (bitpos));
6d716ca8 3589
a150de29 3590 BIT_FIELD_REF_UNSIGNED (result) = unsignedp;
6d716ca8
RS
3591
3592 return result;
3593}
3594
3595/* Optimize a bit-field compare.
3596
3597 There are two cases: First is a compare against a constant and the
3598 second is a comparison of two items where the fields are at the same
3599 bit position relative to the start of a chunk (byte, halfword, word)
3600 large enough to contain it. In these cases we can avoid the shift
3601 implicit in bitfield extractions.
3602
3603 For constants, we emit a compare of the shifted constant with the
3604 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3605 compared. For two fields at the same position, we do the ANDs with the
3606 similar mask and compare the result of the ANDs.
3607
3608 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3609 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3610 are the left and right operands of the comparison, respectively.
3611
6dc42e49 3612 If the optimization described above can be done, we return the resulting
6d716ca8
RS
3613 tree. Otherwise we return zero. */
3614
3615static tree
75040a04
AJ
3616optimize_bit_field_compare (enum tree_code code, tree compare_type,
3617 tree lhs, tree rhs)
6d716ca8 3618{
770ae6cc 3619 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
6d716ca8
RS
3620 tree type = TREE_TYPE (lhs);
3621 tree signed_type, unsigned_type;
3622 int const_p = TREE_CODE (rhs) == INTEGER_CST;
5826cacf 3623 enum machine_mode lmode, rmode, nmode;
6d716ca8
RS
3624 int lunsignedp, runsignedp;
3625 int lvolatilep = 0, rvolatilep = 0;
4e86caed 3626 tree linner, rinner = NULL_TREE;
6d716ca8 3627 tree mask;
f1e60ec6 3628 tree offset;
6d716ca8
RS
3629
3630 /* Get all the information about the extractions being done. If the bit size
3631 if the same as the size of the underlying object, we aren't doing an
14a774a9
RK
3632 extraction at all and so can do nothing. We also don't want to
3633 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3634 then will no longer be able to replace it. */
f1e60ec6 3635 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
2614034e 3636 &lunsignedp, &lvolatilep, false);
9f5e873c 3637 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
14a774a9 3638 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
6d716ca8
RS
3639 return 0;
3640
3641 if (!const_p)
3642 {
3643 /* If this is not a constant, we can only do something if bit positions,
6d2f8887 3644 sizes, and signedness are the same. */
23bd99ae 3645 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
2614034e 3646 &runsignedp, &rvolatilep, false);
6d716ca8 3647
9f5e873c 3648 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
14a774a9
RK
3649 || lunsignedp != runsignedp || offset != 0
3650 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
6d716ca8
RS
3651 return 0;
3652 }
3653
3654 /* See if we can find a mode to refer to this field. We should be able to,
3655 but fail if we can't. */
5826cacf
RK
3656 nmode = get_best_mode (lbitsize, lbitpos,
3657 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3658 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3659 TYPE_ALIGN (TREE_TYPE (rinner))),
3660 word_mode, lvolatilep || rvolatilep);
3661 if (nmode == VOIDmode)
6d716ca8
RS
3662 return 0;
3663
3664 /* Set signed and unsigned types of the precision of this mode for the
3665 shifts below. */
5785c7de
RS
3666 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3667 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
6d716ca8 3668
6d716ca8
RS
3669 /* Compute the bit position and size for the new reference and our offset
3670 within it. If the new reference is the same size as the original, we
3671 won't optimize anything, so return zero. */
5826cacf
RK
3672 nbitsize = GET_MODE_BITSIZE (nmode);
3673 nbitpos = lbitpos & ~ (nbitsize - 1);
3674 lbitpos -= nbitpos;
3675 if (nbitsize == lbitsize)
6d716ca8
RS
3676 return 0;
3677
f76b9db2 3678 if (BYTES_BIG_ENDIAN)
5826cacf 3679 lbitpos = nbitsize - lbitsize - lbitpos;
6d716ca8
RS
3680
3681 /* Make the mask to be used against the extracted field. */
2ac7cbb5 3682 mask = build_int_cst_type (unsigned_type, -1);
5826cacf 3683 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
6d716ca8 3684 mask = const_binop (RSHIFT_EXPR, mask,
5826cacf 3685 size_int (nbitsize - lbitsize - lbitpos), 0);
6d716ca8
RS
3686
3687 if (! const_p)
3688 /* If not comparing with constant, just rework the comparison
3689 and return. */
7c06f565
RS
3690 return fold_build2 (code, compare_type,
3691 fold_build2 (BIT_AND_EXPR, unsigned_type,
3692 make_bit_field_ref (linner,
3693 unsigned_type,
3694 nbitsize, nbitpos,
3695 1),
3696 mask),
3697 fold_build2 (BIT_AND_EXPR, unsigned_type,
3698 make_bit_field_ref (rinner,
3699 unsigned_type,
3700 nbitsize, nbitpos,
3701 1),
3702 mask));
6d716ca8
RS
3703
3704 /* Otherwise, we are handling the constant case. See if the constant is too
3705 big for the field. Warn and return a tree of for 0 (false) if so. We do
3706 this not only for its own sake, but to avoid having to test for this
3707 error case below. If we didn't, we might generate wrong code.
3708
3709 For unsigned fields, the constant shifted right by the field length should
b6cc0a72 3710 be all zero. For signed fields, the high-order bits should agree with
6d716ca8
RS
3711 the sign bit. */
3712
3713 if (lunsignedp)
3714 {
3715 if (! integer_zerop (const_binop (RSHIFT_EXPR,
088414c1 3716 fold_convert (unsigned_type, rhs),
91d33e36 3717 size_int (lbitsize), 0)))
6d716ca8 3718 {
d4ee4d25 3719 warning (0, "comparison is always %d due to width of bit-field",
ab87f8c8 3720 code == NE_EXPR);
1b0f3e79 3721 return constant_boolean_node (code == NE_EXPR, compare_type);
6d716ca8
RS
3722 }
3723 }
3724 else
3725 {
088414c1 3726 tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
91d33e36 3727 size_int (lbitsize - 1), 0);
6d716ca8
RS
3728 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3729 {
d4ee4d25 3730 warning (0, "comparison is always %d due to width of bit-field",
ab87f8c8 3731 code == NE_EXPR);
1b0f3e79 3732 return constant_boolean_node (code == NE_EXPR, compare_type);
6d716ca8
RS
3733 }
3734 }
3735
3736 /* Single-bit compares should always be against zero. */
3737 if (lbitsize == 1 && ! integer_zerop (rhs))
3738 {
3739 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
57decb7e 3740 rhs = build_int_cst (type, 0);
6d716ca8
RS
3741 }
3742
3743 /* Make a new bitfield reference, shift the constant over the
3744 appropriate number of bits and mask it with the computed mask
3745 (in case this was a signed field). If we changed it, make a new one. */
5826cacf 3746 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
9db73acb
RK
3747 if (lvolatilep)
3748 {
3749 TREE_SIDE_EFFECTS (lhs) = 1;
3750 TREE_THIS_VOLATILE (lhs) = 1;
3751 }
6d716ca8 3752
f457cf40
JM
3753 rhs = const_binop (BIT_AND_EXPR,
3754 const_binop (LSHIFT_EXPR,
3755 fold_convert (unsigned_type, rhs),
3756 size_int (lbitpos), 0),
3757 mask, 0);
6d716ca8 3758
59ce6d6b
RS
3759 return build2 (code, compare_type,
3760 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3761 rhs);
6d716ca8
RS
3762}
3763\f
b2215d83 3764/* Subroutine for fold_truthop: decode a field reference.
6d716ca8
RS
3765
3766 If EXP is a comparison reference, we return the innermost reference.
3767
3768 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3769 set to the starting bit number.
3770
3771 If the innermost field can be completely contained in a mode-sized
3772 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3773
3774 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3775 otherwise it is not changed.
3776
3777 *PUNSIGNEDP is set to the signedness of the field.
3778
3779 *PMASK is set to the mask used. This is either contained in a
3780 BIT_AND_EXPR or derived from the width of the field.
3781
38e01259 3782 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
d4453ee5 3783
6d716ca8
RS
3784 Return 0 if this is not a component reference or is one that we can't
3785 do anything with. */
3786
3787static tree
75040a04
AJ
3788decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3789 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3790 int *punsignedp, int *pvolatilep,
fa8db1f7 3791 tree *pmask, tree *pand_mask)
6d716ca8 3792{
1a8c4ca6 3793 tree outer_type = 0;
6d9f1f5f
RK
3794 tree and_mask = 0;
3795 tree mask, inner, offset;
3796 tree unsigned_type;
770ae6cc 3797 unsigned int precision;
6d716ca8 3798
b6cc0a72 3799 /* All the optimizations using this function assume integer fields.
772ae9f0
RK
3800 There are problems with FP fields since the type_for_size call
3801 below can fail for, e.g., XFmode. */
3802 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3803 return 0;
3804
1a8c4ca6
EB
3805 /* We are interested in the bare arrangement of bits, so strip everything
3806 that doesn't affect the machine mode. However, record the type of the
3807 outermost expression if it may matter below. */
3808 if (TREE_CODE (exp) == NOP_EXPR
3809 || TREE_CODE (exp) == CONVERT_EXPR
3810 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3811 outer_type = TREE_TYPE (exp);
df7fb8f9 3812 STRIP_NOPS (exp);
6d716ca8
RS
3813
3814 if (TREE_CODE (exp) == BIT_AND_EXPR)
3815 {
6d9f1f5f 3816 and_mask = TREE_OPERAND (exp, 1);
6d716ca8 3817 exp = TREE_OPERAND (exp, 0);
6d9f1f5f
RK
3818 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3819 if (TREE_CODE (and_mask) != INTEGER_CST)
6d716ca8
RS
3820 return 0;
3821 }
3822
f1e60ec6 3823 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
2614034e 3824 punsignedp, pvolatilep, false);
02103577 3825 if ((inner == exp && and_mask == 0)
14a774a9
RK
3826 || *pbitsize < 0 || offset != 0
3827 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
c05a9b68 3828 return 0;
b6cc0a72 3829
1a8c4ca6
EB
3830 /* If the number of bits in the reference is the same as the bitsize of
3831 the outer type, then the outer type gives the signedness. Otherwise
3832 (in case of a small bitfield) the signedness is unchanged. */
fae1b38d 3833 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
8df83eae 3834 *punsignedp = TYPE_UNSIGNED (outer_type);
1a8c4ca6 3835
6d9f1f5f 3836 /* Compute the mask to access the bitfield. */
5785c7de 3837 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
6d9f1f5f
RK
3838 precision = TYPE_PRECISION (unsigned_type);
3839
2ac7cbb5 3840 mask = build_int_cst_type (unsigned_type, -1);
3e6688a7 3841
6d9f1f5f
RK
3842 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3843 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3844
3845 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3846 if (and_mask != 0)
7f20a5b7
KH
3847 mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
3848 fold_convert (unsigned_type, and_mask), mask);
6d716ca8
RS
3849
3850 *pmask = mask;
d4453ee5 3851 *pand_mask = and_mask;
6d716ca8
RS
3852 return inner;
3853}
3854
cc2902df 3855/* Return nonzero if MASK represents a mask of SIZE ones in the low-order
6d716ca8
RS
3856 bit positions. */
3857
3858static int
fa8db1f7 3859all_ones_mask_p (tree mask, int size)
6d716ca8
RS
3860{
3861 tree type = TREE_TYPE (mask);
770ae6cc 3862 unsigned int precision = TYPE_PRECISION (type);
13af526d 3863 tree tmask;
6d716ca8 3864
12753674 3865 tmask = build_int_cst_type (signed_type_for (type), -1);
3e6688a7 3866
6d716ca8 3867 return
b6cc0a72 3868 tree_int_cst_equal (mask,
02103577
RK
3869 const_binop (RSHIFT_EXPR,
3870 const_binop (LSHIFT_EXPR, tmask,
3871 size_int (precision - size),
3872 0),
3873 size_int (precision - size), 0));
6d716ca8 3874}
b2215d83 3875
1f77b5da
RS
3876/* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3877 represents the sign bit of EXP's type. If EXP represents a sign
3878 or zero extension, also test VAL against the unextended type.
3879 The return value is the (sub)expression whose sign bit is VAL,
3880 or NULL_TREE otherwise. */
3881
3882static tree
fa8db1f7 3883sign_bit_p (tree exp, tree val)
1f77b5da 3884{
c87d821b
KH
3885 unsigned HOST_WIDE_INT mask_lo, lo;
3886 HOST_WIDE_INT mask_hi, hi;
1f77b5da
RS
3887 int width;
3888 tree t;
3889
68e82b83 3890 /* Tree EXP must have an integral type. */
1f77b5da
RS
3891 t = TREE_TYPE (exp);
3892 if (! INTEGRAL_TYPE_P (t))
3893 return NULL_TREE;
3894
3895 /* Tree VAL must be an integer constant. */
3896 if (TREE_CODE (val) != INTEGER_CST
455f14dd 3897 || TREE_OVERFLOW (val))
1f77b5da
RS
3898 return NULL_TREE;
3899
3900 width = TYPE_PRECISION (t);
3901 if (width > HOST_BITS_PER_WIDE_INT)
3902 {
3903 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3904 lo = 0;
c87d821b
KH
3905
3906 mask_hi = ((unsigned HOST_WIDE_INT) -1
3907 >> (2 * HOST_BITS_PER_WIDE_INT - width));
3908 mask_lo = -1;
1f77b5da
RS
3909 }
3910 else
3911 {
3912 hi = 0;
3913 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
c87d821b
KH
3914
3915 mask_hi = 0;
3916 mask_lo = ((unsigned HOST_WIDE_INT) -1
3917 >> (HOST_BITS_PER_WIDE_INT - width));
1f77b5da
RS
3918 }
3919
c87d821b
KH
3920 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3921 treat VAL as if it were unsigned. */
3922 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3923 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
1f77b5da
RS
3924 return exp;
3925
3926 /* Handle extension from a narrower type. */
3927 if (TREE_CODE (exp) == NOP_EXPR
3928 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3929 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3930
3931 return NULL_TREE;
3932}
3933
b2215d83
TW
3934/* Subroutine for fold_truthop: determine if an operand is simple enough
3935 to be evaluated unconditionally. */
3936
b6cc0a72 3937static int
fa8db1f7 3938simple_operand_p (tree exp)
b2215d83
TW
3939{
3940 /* Strip any conversions that don't change the machine mode. */
1d481ba8 3941 STRIP_NOPS (exp);
b2215d83 3942
6615c446 3943 return (CONSTANT_CLASS_P (exp)
1d481ba8 3944 || TREE_CODE (exp) == SSA_NAME
2f939d94 3945 || (DECL_P (exp)
b2215d83
TW
3946 && ! TREE_ADDRESSABLE (exp)
3947 && ! TREE_THIS_VOLATILE (exp)
8227896c
TW
3948 && ! DECL_NONLOCAL (exp)
3949 /* Don't regard global variables as simple. They may be
3950 allocated in ways unknown to the compiler (shared memory,
3951 #pragma weak, etc). */
3952 && ! TREE_PUBLIC (exp)
3953 && ! DECL_EXTERNAL (exp)
3954 /* Loading a static variable is unduly expensive, but global
3955 registers aren't expensive. */
3956 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
b2215d83 3957}
6d716ca8 3958\f
ebde8a27
RK
3959/* The following functions are subroutines to fold_range_test and allow it to
3960 try to change a logical combination of comparisons into a range test.
3961
3962 For example, both
fa8db1f7 3963 X == 2 || X == 3 || X == 4 || X == 5
ebde8a27 3964 and
fa8db1f7 3965 X >= 2 && X <= 5
ebde8a27
RK
3966 are converted to
3967 (unsigned) (X - 2) <= 3
3968
956d6950 3969 We describe each set of comparisons as being either inside or outside
ebde8a27
RK
3970 a range, using a variable named like IN_P, and then describe the
3971 range with a lower and upper bound. If one of the bounds is omitted,
3972 it represents either the highest or lowest value of the type.
3973
3974 In the comments below, we represent a range by two numbers in brackets
956d6950 3975 preceded by a "+" to designate being inside that range, or a "-" to
ebde8a27
RK
3976 designate being outside that range, so the condition can be inverted by
3977 flipping the prefix. An omitted bound is represented by a "-". For
3978 example, "- [-, 10]" means being outside the range starting at the lowest
3979 possible value and ending at 10, in other words, being greater than 10.
3980 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3981 always false.
3982
3983 We set up things so that the missing bounds are handled in a consistent
3984 manner so neither a missing bound nor "true" and "false" need to be
3985 handled using a special case. */
3986
3987/* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3988 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3989 and UPPER1_P are nonzero if the respective argument is an upper bound
3990 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3991 must be specified for a comparison. ARG1 will be converted to ARG0's
3992 type if both are specified. */
ef659ec0 3993
ebde8a27 3994static tree
75040a04
AJ
3995range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3996 tree arg1, int upper1_p)
ebde8a27 3997{
27bae8e5 3998 tree tem;
ebde8a27
RK
3999 int result;
4000 int sgn0, sgn1;
ef659ec0 4001
ebde8a27
RK
4002 /* If neither arg represents infinity, do the normal operation.
4003 Else, if not a comparison, return infinity. Else handle the special
4004 comparison rules. Note that most of the cases below won't occur, but
4005 are handled for consistency. */
ef659ec0 4006
ebde8a27 4007 if (arg0 != 0 && arg1 != 0)
27bae8e5 4008 {
7f20a5b7
KH
4009 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4010 arg0, fold_convert (TREE_TYPE (arg0), arg1));
27bae8e5
RK
4011 STRIP_NOPS (tem);
4012 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4013 }
ef659ec0 4014
6615c446 4015 if (TREE_CODE_CLASS (code) != tcc_comparison)
ebde8a27
RK
4016 return 0;
4017
4018 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
d7b3ea38
NS
4019 for neither. In real maths, we cannot assume open ended ranges are
4020 the same. But, this is computer arithmetic, where numbers are finite.
4021 We can therefore make the transformation of any unbounded range with
4022 the value Z, Z being greater than any representable number. This permits
30f7a378 4023 us to treat unbounded ranges as equal. */
ebde8a27 4024 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4e644c93 4025 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
ebde8a27
RK
4026 switch (code)
4027 {
d7b3ea38
NS
4028 case EQ_EXPR:
4029 result = sgn0 == sgn1;
4030 break;
4031 case NE_EXPR:
4032 result = sgn0 != sgn1;
ebde8a27 4033 break;
d7b3ea38 4034 case LT_EXPR:
ebde8a27
RK
4035 result = sgn0 < sgn1;
4036 break;
d7b3ea38
NS
4037 case LE_EXPR:
4038 result = sgn0 <= sgn1;
4039 break;
4040 case GT_EXPR:
ebde8a27
RK
4041 result = sgn0 > sgn1;
4042 break;
d7b3ea38
NS
4043 case GE_EXPR:
4044 result = sgn0 >= sgn1;
4045 break;
e9a25f70 4046 default:
0bccc606 4047 gcc_unreachable ();
ebde8a27
RK
4048 }
4049
1b0f3e79 4050 return constant_boolean_node (result, type);
ebde8a27 4051}
b6cc0a72 4052\f
ebde8a27
RK
4053/* Given EXP, a logical expression, set the range it is testing into
4054 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
6ac01510
ILT
4055 actually being tested. *PLOW and *PHIGH will be made of the same
4056 type as the returned expression. If EXP is not a comparison, we
4057 will most likely not be returning a useful value and range. Set
4058 *STRICT_OVERFLOW_P to true if the return value is only valid
4059 because signed overflow is undefined; otherwise, do not change
4060 *STRICT_OVERFLOW_P. */
ef659ec0 4061
6dc7571d 4062static tree
6ac01510
ILT
4063make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4064 bool *strict_overflow_p)
ef659ec0 4065{
ebde8a27 4066 enum tree_code code;
d1822754
EC
4067 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4068 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
ebde8a27
RK
4069 int in_p, n_in_p;
4070 tree low, high, n_low, n_high;
ef659ec0 4071
ebde8a27
RK
4072 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4073 and see if we can refine the range. Some of the cases below may not
4074 happen, but it doesn't seem worth worrying about this. We "continue"
4075 the outer loop when we've changed something; otherwise we "break"
4076 the switch, which will "break" the while. */
ef659ec0 4077
088414c1 4078 in_p = 0;
57decb7e 4079 low = high = build_int_cst (TREE_TYPE (exp), 0);
ebde8a27
RK
4080
4081 while (1)
ef659ec0 4082 {
ebde8a27 4083 code = TREE_CODE (exp);
d1822754 4084 exp_type = TREE_TYPE (exp);
30d68b86
MM
4085
4086 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4087 {
5039610b 4088 if (TREE_OPERAND_LENGTH (exp) > 0)
d17811fd 4089 arg0 = TREE_OPERAND (exp, 0);
6615c446
JO
4090 if (TREE_CODE_CLASS (code) == tcc_comparison
4091 || TREE_CODE_CLASS (code) == tcc_unary
4092 || TREE_CODE_CLASS (code) == tcc_binary)
d1822754 4093 arg0_type = TREE_TYPE (arg0);
6615c446
JO
4094 if (TREE_CODE_CLASS (code) == tcc_binary
4095 || TREE_CODE_CLASS (code) == tcc_comparison
4096 || (TREE_CODE_CLASS (code) == tcc_expression
5039610b 4097 && TREE_OPERAND_LENGTH (exp) > 1))
30d68b86
MM
4098 arg1 = TREE_OPERAND (exp, 1);
4099 }
ef659ec0 4100
ebde8a27
RK
4101 switch (code)
4102 {
4103 case TRUTH_NOT_EXPR:
4104 in_p = ! in_p, exp = arg0;
4105 continue;
4106
4107 case EQ_EXPR: case NE_EXPR:
4108 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4109 /* We can only do something if the range is testing for zero
4110 and if the second operand is an integer constant. Note that
4111 saying something is "in" the range we make is done by
4112 complementing IN_P since it will set in the initial case of
4113 being not equal to zero; "out" is leaving it alone. */
4114 if (low == 0 || high == 0
4115 || ! integer_zerop (low) || ! integer_zerop (high)
4116 || TREE_CODE (arg1) != INTEGER_CST)
4117 break;
ef659ec0 4118
ebde8a27
RK
4119 switch (code)
4120 {
4121 case NE_EXPR: /* - [c, c] */
4122 low = high = arg1;
4123 break;
4124 case EQ_EXPR: /* + [c, c] */
4125 in_p = ! in_p, low = high = arg1;
4126 break;
4127 case GT_EXPR: /* - [-, c] */
4128 low = 0, high = arg1;
4129 break;
4130 case GE_EXPR: /* + [c, -] */
4131 in_p = ! in_p, low = arg1, high = 0;
4132 break;
4133 case LT_EXPR: /* - [c, -] */
4134 low = arg1, high = 0;
4135 break;
4136 case LE_EXPR: /* + [-, c] */
4137 in_p = ! in_p, low = 0, high = arg1;
4138 break;
e9a25f70 4139 default:
0bccc606 4140 gcc_unreachable ();
ebde8a27 4141 }
ef659ec0 4142
7f423031 4143 /* If this is an unsigned comparison, we also know that EXP is
0e1c7fc7
RK
4144 greater than or equal to zero. We base the range tests we make
4145 on that fact, so we record it here so we can parse existing
d1822754
EC
4146 range tests. We test arg0_type since often the return type
4147 of, e.g. EQ_EXPR, is boolean. */
4148 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
ebde8a27 4149 {
e9ea8bd5
RS
4150 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4151 in_p, low, high, 1,
57decb7e 4152 build_int_cst (arg0_type, 0),
0e1c7fc7 4153 NULL_TREE))
ebde8a27 4154 break;
ef659ec0 4155
ebde8a27 4156 in_p = n_in_p, low = n_low, high = n_high;
0e1c7fc7 4157
368ebcd6 4158 /* If the high bound is missing, but we have a nonzero low
1358cdc5
RK
4159 bound, reverse the range so it goes from zero to the low bound
4160 minus 1. */
4161 if (high == 0 && low && ! integer_zerop (low))
0e1c7fc7
RK
4162 {
4163 in_p = ! in_p;
4164 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4165 integer_one_node, 0);
57decb7e 4166 low = build_int_cst (arg0_type, 0);
0e1c7fc7 4167 }
ebde8a27 4168 }
d1822754
EC
4169
4170 exp = arg0;
ebde8a27
RK
4171 continue;
4172
4173 case NEGATE_EXPR:
4174 /* (-x) IN [a,b] -> x in [-b, -a] */
d1822754 4175 n_low = range_binop (MINUS_EXPR, exp_type,
57decb7e 4176 build_int_cst (exp_type, 0),
088414c1 4177 0, high, 1);
d1822754 4178 n_high = range_binop (MINUS_EXPR, exp_type,
57decb7e 4179 build_int_cst (exp_type, 0),
088414c1 4180 0, low, 0);
ebde8a27
RK
4181 low = n_low, high = n_high;
4182 exp = arg0;
4183 continue;
4184
4185 case BIT_NOT_EXPR:
4186 /* ~ X -> -X - 1 */
d1822754 4187 exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
57decb7e 4188 build_int_cst (exp_type, 1));
ebde8a27
RK
4189 continue;
4190
4191 case PLUS_EXPR: case MINUS_EXPR:
4192 if (TREE_CODE (arg1) != INTEGER_CST)
4193 break;
4194
c078a437
KH
4195 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4196 move a constant to the other side. */
eeef0e45
ILT
4197 if (!TYPE_UNSIGNED (arg0_type)
4198 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
c078a437
KH
4199 break;
4200
ebde8a27
RK
4201 /* If EXP is signed, any overflow in the computation is undefined,
4202 so we don't worry about it so long as our computations on
4203 the bounds don't overflow. For unsigned, overflow is defined
4204 and this is exactly the right thing. */
4205 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
d1822754 4206 arg0_type, low, 0, arg1, 0);
ebde8a27 4207 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
d1822754 4208 arg0_type, high, 1, arg1, 0);
ebde8a27
RK
4209 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4210 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4211 break;
4212
6ac01510
ILT
4213 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4214 *strict_overflow_p = true;
4215
3c00684e
JL
4216 /* Check for an unsigned range which has wrapped around the maximum
4217 value thus making n_high < n_low, and normalize it. */
5a9d82a6 4218 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3c00684e 4219 {
d1822754 4220 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
0e1c7fc7 4221 integer_one_node, 0);
d1822754 4222 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
c2b63960
AO
4223 integer_one_node, 0);
4224
4225 /* If the range is of the form +/- [ x+1, x ], we won't
4226 be able to normalize it. But then, it represents the
4227 whole range or the empty set, so make it
4228 +/- [ -, - ]. */
4229 if (tree_int_cst_equal (n_low, low)
4230 && tree_int_cst_equal (n_high, high))
4231 low = high = 0;
4232 else
4233 in_p = ! in_p;
3c00684e 4234 }
5a9d82a6
JW
4235 else
4236 low = n_low, high = n_high;
27bae8e5 4237
ebde8a27
RK
4238 exp = arg0;
4239 continue;
4240
4241 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
d1822754 4242 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
7d12cee1
JL
4243 break;
4244
d1822754
EC
4245 if (! INTEGRAL_TYPE_P (arg0_type)
4246 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4247 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
ebde8a27
RK
4248 break;
4249
ce2157a1 4250 n_low = low, n_high = high;
ebde8a27 4251
ce2157a1 4252 if (n_low != 0)
d1822754 4253 n_low = fold_convert (arg0_type, n_low);
ce2157a1
JL
4254
4255 if (n_high != 0)
d1822754 4256 n_high = fold_convert (arg0_type, n_high);
ce2157a1 4257
ce2157a1 4258
d1822754 4259 /* If we're converting arg0 from an unsigned type, to exp,
61ada8ae 4260 a signed type, we will be doing the comparison as unsigned.
d1822754
EC
4261 The tests above have already verified that LOW and HIGH
4262 are both positive.
4263
4264 So we have to ensure that we will handle large unsigned
4265 values the same way that the current signed bounds treat
4266 negative values. */
4267
4268 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
ce2157a1 4269 {
e1ee5cdc 4270 tree high_positive;
d1822754
EC
4271 tree equiv_type = lang_hooks.types.type_for_mode
4272 (TYPE_MODE (arg0_type), 1);
e1ee5cdc
RH
4273
4274 /* A range without an upper bound is, naturally, unbounded.
4275 Since convert would have cropped a very large value, use
14a774a9
RK
4276 the max value for the destination type. */
4277 high_positive
4278 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
d1822754 4279 : TYPE_MAX_VALUE (arg0_type);
e1ee5cdc 4280
d1822754 4281 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
7f20a5b7
KH
4282 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4283 fold_convert (arg0_type,
4284 high_positive),
000d8d44 4285 build_int_cst (arg0_type, 1));
b6cc0a72 4286
ce2157a1
JL
4287 /* If the low bound is specified, "and" the range with the
4288 range for which the original unsigned value will be
4289 positive. */
4290 if (low != 0)
4291 {
4292 if (! merge_ranges (&n_in_p, &n_low, &n_high,
088414c1 4293 1, n_low, n_high, 1,
e9ea8bd5
RS
4294 fold_convert (arg0_type,
4295 integer_zero_node),
ce2157a1
JL
4296 high_positive))
4297 break;
4298
4299 in_p = (n_in_p == in_p);
4300 }
4301 else
4302 {
4303 /* Otherwise, "or" the range with the range of the input
4304 that will be interpreted as negative. */
4305 if (! merge_ranges (&n_in_p, &n_low, &n_high,
088414c1 4306 0, n_low, n_high, 1,
e9ea8bd5
RS
4307 fold_convert (arg0_type,
4308 integer_zero_node),
ce2157a1
JL
4309 high_positive))
4310 break;
4311
4312 in_p = (in_p != n_in_p);
4313 }
4314 }
ebde8a27
RK
4315
4316 exp = arg0;
ce2157a1 4317 low = n_low, high = n_high;
ebde8a27 4318 continue;
ce2157a1
JL
4319
4320 default:
4321 break;
ef659ec0 4322 }
ebde8a27
RK
4323
4324 break;
ef659ec0 4325 }
ebde8a27 4326
80906567
RK
4327 /* If EXP is a constant, we can evaluate whether this is true or false. */
4328 if (TREE_CODE (exp) == INTEGER_CST)
4329 {
4330 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4331 exp, 0, low, 0))
4332 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4333 exp, 1, high, 1)));
4334 low = high = 0;
4335 exp = 0;
4336 }
4337
ebde8a27
RK
4338 *pin_p = in_p, *plow = low, *phigh = high;
4339 return exp;
4340}
4341\f
4342/* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4343 type, TYPE, return an expression to test if EXP is in (or out of, depending
e1af8299 4344 on IN_P) the range. Return 0 if the test couldn't be created. */
ebde8a27
RK
4345
4346static tree
fa8db1f7 4347build_range_check (tree type, tree exp, int in_p, tree low, tree high)
ebde8a27
RK
4348{
4349 tree etype = TREE_TYPE (exp);
dbfb1116 4350 tree value;
ebde8a27 4351
f60c951c
JDA
4352#ifdef HAVE_canonicalize_funcptr_for_compare
4353 /* Disable this optimization for function pointer expressions
4354 on targets that require function pointer canonicalization. */
4355 if (HAVE_canonicalize_funcptr_for_compare
4356 && TREE_CODE (etype) == POINTER_TYPE
4357 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4358 return NULL_TREE;
4359#endif
4360
e1af8299
JJ
4361 if (! in_p)
4362 {
4363 value = build_range_check (type, exp, 1, low, high);
4364 if (value != 0)
4365 return invert_truthvalue (value);
4366
4367 return 0;
4368 }
ebde8a27 4369
dbfb1116 4370 if (low == 0 && high == 0)
57decb7e 4371 return build_int_cst (type, 1);
ebde8a27 4372
dbfb1116 4373 if (low == 0)
01c0a9fa
AP
4374 return fold_build2 (LE_EXPR, type, exp,
4375 fold_convert (etype, high));
ebde8a27 4376
dbfb1116 4377 if (high == 0)
01c0a9fa
AP
4378 return fold_build2 (GE_EXPR, type, exp,
4379 fold_convert (etype, low));
ebde8a27 4380
dbfb1116 4381 if (operand_equal_p (low, high, 0))
01c0a9fa
AP
4382 return fold_build2 (EQ_EXPR, type, exp,
4383 fold_convert (etype, low));
ebde8a27 4384
dbfb1116 4385 if (integer_zerop (low))
ef659ec0 4386 {
8df83eae 4387 if (! TYPE_UNSIGNED (etype))
dd3f0101 4388 {
ca5ba2a3 4389 etype = unsigned_type_for (etype);
088414c1
RS
4390 high = fold_convert (etype, high);
4391 exp = fold_convert (etype, exp);
dd3f0101 4392 }
dbfb1116 4393 return build_range_check (type, exp, 1, 0, high);
ebde8a27 4394 }
ef659ec0 4395
dbfb1116
RS
4396 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4397 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4398 {
4399 unsigned HOST_WIDE_INT lo;
4400 HOST_WIDE_INT hi;
4401 int prec;
4402
4403 prec = TYPE_PRECISION (etype);
4404 if (prec <= HOST_BITS_PER_WIDE_INT)
dd3f0101
KH
4405 {
4406 hi = 0;
4407 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4408 }
dbfb1116 4409 else
dd3f0101
KH
4410 {
4411 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4412 lo = (unsigned HOST_WIDE_INT) -1;
4413 }
dbfb1116
RS
4414
4415 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
dd3f0101 4416 {
8df83eae 4417 if (TYPE_UNSIGNED (etype))
dd3f0101 4418 {
12753674 4419 etype = signed_type_for (etype);
088414c1 4420 exp = fold_convert (etype, exp);
dd3f0101 4421 }
7f20a5b7 4422 return fold_build2 (GT_EXPR, type, exp,
57decb7e 4423 build_int_cst (etype, 0));
dd3f0101 4424 }
dbfb1116
RS
4425 }
4426
f8fe0545
EB
4427 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4428 This requires wrap-around arithmetics for the type of the expression. */
4429 switch (TREE_CODE (etype))
4430 {
4431 case INTEGER_TYPE:
4432 /* There is no requirement that LOW be within the range of ETYPE
4433 if the latter is a subtype. It must, however, be within the base
4434 type of ETYPE. So be sure we do the subtraction in that type. */
4435 if (TREE_TYPE (etype))
4436 etype = TREE_TYPE (etype);
4437 break;
4438
4439 case ENUMERAL_TYPE:
4440 case BOOLEAN_TYPE:
4441 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4442 TYPE_UNSIGNED (etype));
4443 break;
4444
4445 default:
4446 break;
4447 }
4448
4449 /* If we don't have wrap-around arithmetics upfront, try to force it. */
4450 if (TREE_CODE (etype) == INTEGER_TYPE
eeef0e45 4451 && !TYPE_OVERFLOW_WRAPS (etype))
e1af8299
JJ
4452 {
4453 tree utype, minv, maxv;
4454
4455 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4456 for the type in question, as we rely on this here. */
ca5ba2a3 4457 utype = unsigned_type_for (etype);
f8fe0545
EB
4458 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4459 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4460 integer_one_node, 1);
4461 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4462
4463 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4464 minv, 1, maxv, 1)))
4465 etype = utype;
4466 else
4467 return 0;
e1af8299
JJ
4468 }
4469
f8fe0545
EB
4470 high = fold_convert (etype, high);
4471 low = fold_convert (etype, low);
4472 exp = fold_convert (etype, exp);
438090c3 4473
f8fe0545
EB
4474 value = const_binop (MINUS_EXPR, high, low, 0);
4475
5be014d5
AP
4476
4477 if (POINTER_TYPE_P (etype))
4478 {
4479 if (value != 0 && !TREE_OVERFLOW (value))
4480 {
4481 low = fold_convert (sizetype, low);
4482 low = fold_build1 (NEGATE_EXPR, sizetype, low);
4483 return build_range_check (type,
4484 fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4485 1, build_int_cst (etype, 0), value);
4486 }
4487 return 0;
4488 }
4489
f8fe0545
EB
4490 if (value != 0 && !TREE_OVERFLOW (value))
4491 return build_range_check (type,
4492 fold_build2 (MINUS_EXPR, etype, exp, low),
4493 1, build_int_cst (etype, 0), value);
dbfb1116
RS
4494
4495 return 0;
ebde8a27
RK
4496}
4497\f
2f96b754
EB
4498/* Return the predecessor of VAL in its type, handling the infinite case. */
4499
4500static tree
4501range_predecessor (tree val)
4502{
4503 tree type = TREE_TYPE (val);
4504
1464eeb8
EB
4505 if (INTEGRAL_TYPE_P (type)
4506 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
2f96b754
EB
4507 return 0;
4508 else
4509 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4510}
4511
4512/* Return the successor of VAL in its type, handling the infinite case. */
4513
4514static tree
4515range_successor (tree val)
4516{
4517 tree type = TREE_TYPE (val);
4518
1464eeb8
EB
4519 if (INTEGRAL_TYPE_P (type)
4520 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
2f96b754
EB
4521 return 0;
4522 else
4523 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4524}
4525
b6cc0a72 4526/* Given two ranges, see if we can merge them into one. Return 1 if we
ebde8a27 4527 can, 0 if we can't. Set the output range into the specified parameters. */
ef659ec0 4528
ebde8a27 4529static int
75040a04
AJ
4530merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4531 tree high0, int in1_p, tree low1, tree high1)
ebde8a27
RK
4532{
4533 int no_overlap;
4534 int subset;
4535 int temp;
4536 tree tem;
4537 int in_p;
4538 tree low, high;
ce2157a1
JL
4539 int lowequal = ((low0 == 0 && low1 == 0)
4540 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4541 low0, 0, low1, 0)));
4542 int highequal = ((high0 == 0 && high1 == 0)
4543 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4544 high0, 1, high1, 1)));
4545
4546 /* Make range 0 be the range that starts first, or ends last if they
4547 start at the same value. Swap them if it isn't. */
b6cc0a72 4548 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
ebde8a27 4549 low0, 0, low1, 0))
ce2157a1 4550 || (lowequal
ebde8a27 4551 && integer_onep (range_binop (GT_EXPR, integer_type_node,
ce2157a1 4552 high1, 1, high0, 1))))
ebde8a27
RK
4553 {
4554 temp = in0_p, in0_p = in1_p, in1_p = temp;
4555 tem = low0, low0 = low1, low1 = tem;
4556 tem = high0, high0 = high1, high1 = tem;
4557 }
ef659ec0 4558
ebde8a27
RK
4559 /* Now flag two cases, whether the ranges are disjoint or whether the
4560 second range is totally subsumed in the first. Note that the tests
4561 below are simplified by the ones above. */
4562 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4563 high0, 1, low1, 0));
5df8a1f2 4564 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
ebde8a27
RK
4565 high1, 1, high0, 1));
4566
4567 /* We now have four cases, depending on whether we are including or
4568 excluding the two ranges. */
4569 if (in0_p && in1_p)
4570 {
4571 /* If they don't overlap, the result is false. If the second range
4572 is a subset it is the result. Otherwise, the range is from the start
4573 of the second to the end of the first. */
4574 if (no_overlap)
4575 in_p = 0, low = high = 0;
4576 else if (subset)
4577 in_p = 1, low = low1, high = high1;
4578 else
4579 in_p = 1, low = low1, high = high0;
4580 }
ef659ec0 4581
ebde8a27
RK
4582 else if (in0_p && ! in1_p)
4583 {
ce2157a1
JL
4584 /* If they don't overlap, the result is the first range. If they are
4585 equal, the result is false. If the second range is a subset of the
4586 first, and the ranges begin at the same place, we go from just after
f8fe0545 4587 the end of the second range to the end of the first. If the second
ce2157a1
JL
4588 range is not a subset of the first, or if it is a subset and both
4589 ranges end at the same place, the range starts at the start of the
4590 first range and ends just before the second range.
4591 Otherwise, we can't describe this as a single range. */
ebde8a27
RK
4592 if (no_overlap)
4593 in_p = 1, low = low0, high = high0;
ce2157a1 4594 else if (lowequal && highequal)
405862dd 4595 in_p = 0, low = high = 0;
ce2157a1
JL
4596 else if (subset && lowequal)
4597 {
f8fe0545
EB
4598 low = range_successor (high1);
4599 high = high0;
39ac2ffc
ILT
4600 in_p = 1;
4601 if (low == 0)
4602 {
4603 /* We are in the weird situation where high0 > high1 but
4604 high1 has no successor. Punt. */
4605 return 0;
4606 }
ce2157a1
JL
4607 }
4608 else if (! subset || highequal)
ebde8a27 4609 {
f8fe0545
EB
4610 low = low0;
4611 high = range_predecessor (low1);
39ac2ffc
ILT
4612 in_p = 1;
4613 if (high == 0)
4614 {
4615 /* low0 < low1 but low1 has no predecessor. Punt. */
4616 return 0;
4617 }
ebde8a27 4618 }
ce2157a1
JL
4619 else
4620 return 0;
ebde8a27 4621 }
ef659ec0 4622
ebde8a27
RK
4623 else if (! in0_p && in1_p)
4624 {
4625 /* If they don't overlap, the result is the second range. If the second
4626 is a subset of the first, the result is false. Otherwise,
4627 the range starts just after the first range and ends at the
4628 end of the second. */
4629 if (no_overlap)
4630 in_p = 1, low = low1, high = high1;
14a774a9 4631 else if (subset || highequal)
ebde8a27
RK
4632 in_p = 0, low = high = 0;
4633 else
4634 {
f8fe0545
EB
4635 low = range_successor (high0);
4636 high = high1;
39ac2ffc
ILT
4637 in_p = 1;
4638 if (low == 0)
4639 {
4640 /* high1 > high0 but high0 has no successor. Punt. */
4641 return 0;
4642 }
ef659ec0
TW
4643 }
4644 }
4645
ebde8a27
RK
4646 else
4647 {
4648 /* The case where we are excluding both ranges. Here the complex case
4649 is if they don't overlap. In that case, the only time we have a
4650 range is if they are adjacent. If the second is a subset of the
4651 first, the result is the first. Otherwise, the range to exclude
4652 starts at the beginning of the first range and ends at the end of the
4653 second. */
4654 if (no_overlap)
4655 {
4656 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
f8fe0545 4657 range_successor (high0),
ebde8a27
RK
4658 1, low1, 0)))
4659 in_p = 0, low = low0, high = high1;
4660 else
e1af8299
JJ
4661 {
4662 /* Canonicalize - [min, x] into - [-, x]. */
4663 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4664 switch (TREE_CODE (TREE_TYPE (low0)))
4665 {
4666 case ENUMERAL_TYPE:
4667 if (TYPE_PRECISION (TREE_TYPE (low0))
4668 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4669 break;
4670 /* FALLTHROUGH */
4671 case INTEGER_TYPE:
e1af8299
JJ
4672 if (tree_int_cst_equal (low0,
4673 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4674 low0 = 0;
4675 break;
4676 case POINTER_TYPE:
4677 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4678 && integer_zerop (low0))
4679 low0 = 0;
4680 break;
4681 default:
4682 break;
4683 }
4684
4685 /* Canonicalize - [x, max] into - [x, -]. */
4686 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4687 switch (TREE_CODE (TREE_TYPE (high1)))
4688 {
4689 case ENUMERAL_TYPE:
4690 if (TYPE_PRECISION (TREE_TYPE (high1))
4691 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4692 break;
4693 /* FALLTHROUGH */
4694 case INTEGER_TYPE:
e1af8299
JJ
4695 if (tree_int_cst_equal (high1,
4696 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4697 high1 = 0;
4698 break;
4699 case POINTER_TYPE:
4700 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4701 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4702 high1, 1,
4703 integer_one_node, 1)))
4704 high1 = 0;
4705 break;
4706 default:
4707 break;
4708 }
4709
4710 /* The ranges might be also adjacent between the maximum and
4711 minimum values of the given type. For
4712 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4713 return + [x + 1, y - 1]. */
4714 if (low0 == 0 && high1 == 0)
4715 {
2f96b754
EB
4716 low = range_successor (high0);
4717 high = range_predecessor (low1);
e1af8299
JJ
4718 if (low == 0 || high == 0)
4719 return 0;
4720
4721 in_p = 1;
4722 }
4723 else
4724 return 0;
4725 }
ebde8a27
RK
4726 }
4727 else if (subset)
4728 in_p = 0, low = low0, high = high0;
4729 else
4730 in_p = 0, low = low0, high = high1;
4731 }
f5902869 4732
ebde8a27
RK
4733 *pin_p = in_p, *plow = low, *phigh = high;
4734 return 1;
4735}
2c486ea7
PB
4736\f
4737
4738/* Subroutine of fold, looking inside expressions of the form
2851dd68
PB
4739 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4740 of the COND_EXPR. This function is being used also to optimize
4741 A op B ? C : A, by reversing the comparison first.
2c486ea7
PB
4742
4743 Return a folded expression whose code is not a COND_EXPR
4744 anymore, or NULL_TREE if no folding opportunity is found. */
4745
4746static tree
2851dd68 4747fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
2c486ea7
PB
4748{
4749 enum tree_code comp_code = TREE_CODE (arg0);
4750 tree arg00 = TREE_OPERAND (arg0, 0);
4751 tree arg01 = TREE_OPERAND (arg0, 1);
2851dd68 4752 tree arg1_type = TREE_TYPE (arg1);
2c486ea7 4753 tree tem;
2851dd68
PB
4754
4755 STRIP_NOPS (arg1);
2c486ea7
PB
4756 STRIP_NOPS (arg2);
4757
4758 /* If we have A op 0 ? A : -A, consider applying the following
4759 transformations:
4760
4761 A == 0? A : -A same as -A
4762 A != 0? A : -A same as A
4763 A >= 0? A : -A same as abs (A)
4764 A > 0? A : -A same as abs (A)
4765 A <= 0? A : -A same as -abs (A)
4766 A < 0? A : -A same as -abs (A)
4767
4768 None of these transformations work for modes with signed
4769 zeros. If A is +/-0, the first two transformations will
4770 change the sign of the result (from +0 to -0, or vice
4771 versa). The last four will fix the sign of the result,
4772 even though the original expressions could be positive or
4773 negative, depending on the sign of A.
4774
4775 Note that all these transformations are correct if A is
4776 NaN, since the two alternatives (A and -A) are also NaNs. */
4777 if ((FLOAT_TYPE_P (TREE_TYPE (arg01))
4778 ? real_zerop (arg01)
4779 : integer_zerop (arg01))
a10d70ba
PH
4780 && ((TREE_CODE (arg2) == NEGATE_EXPR
4781 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4782 /* In the case that A is of the form X-Y, '-A' (arg2) may
4783 have already been folded to Y-X, check for that. */
4784 || (TREE_CODE (arg1) == MINUS_EXPR
4785 && TREE_CODE (arg2) == MINUS_EXPR
4786 && operand_equal_p (TREE_OPERAND (arg1, 0),
4787 TREE_OPERAND (arg2, 1), 0)
4788 && operand_equal_p (TREE_OPERAND (arg1, 1),
4789 TREE_OPERAND (arg2, 0), 0))))
2c486ea7
PB
4790 switch (comp_code)
4791 {
4792 case EQ_EXPR:
3ae472c2 4793 case UNEQ_EXPR:
2851dd68
PB
4794 tem = fold_convert (arg1_type, arg1);
4795 return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
2c486ea7 4796 case NE_EXPR:
3ae472c2 4797 case LTGT_EXPR:
2851dd68 4798 return pedantic_non_lvalue (fold_convert (type, arg1));
3ae472c2
RS
4799 case UNGE_EXPR:
4800 case UNGT_EXPR:
4801 if (flag_trapping_math)
4802 break;
4803 /* Fall through. */
2c486ea7
PB
4804 case GE_EXPR:
4805 case GT_EXPR:
2851dd68 4806 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
12753674 4807 arg1 = fold_convert (signed_type_for
2851dd68 4808 (TREE_TYPE (arg1)), arg1);
7f20a5b7 4809 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
2c486ea7 4810 return pedantic_non_lvalue (fold_convert (type, tem));
3ae472c2
RS
4811 case UNLE_EXPR:
4812 case UNLT_EXPR:
4813 if (flag_trapping_math)
4814 break;
2c486ea7
PB
4815 case LE_EXPR:
4816 case LT_EXPR:
2851dd68 4817 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
12753674 4818 arg1 = fold_convert (signed_type_for
2851dd68 4819 (TREE_TYPE (arg1)), arg1);
7f20a5b7 4820 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
2c486ea7
PB
4821 return negate_expr (fold_convert (type, tem));
4822 default:
6615c446 4823 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
3ae472c2 4824 break;
2c486ea7
PB
4825 }
4826
4827 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4828 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4829 both transformations are correct when A is NaN: A != 0
4830 is then true, and A == 0 is false. */
4831
4832 if (integer_zerop (arg01) && integer_zerop (arg2))
4833 {
4834 if (comp_code == NE_EXPR)
2851dd68 4835 return pedantic_non_lvalue (fold_convert (type, arg1));
2c486ea7 4836 else if (comp_code == EQ_EXPR)
57decb7e 4837 return build_int_cst (type, 0);
2c486ea7
PB
4838 }
4839
4840 /* Try some transformations of A op B ? A : B.
4841
4842 A == B? A : B same as B
4843 A != B? A : B same as A
4844 A >= B? A : B same as max (A, B)
4845 A > B? A : B same as max (B, A)
4846 A <= B? A : B same as min (A, B)
4847 A < B? A : B same as min (B, A)
4848
4849 As above, these transformations don't work in the presence
4850 of signed zeros. For example, if A and B are zeros of
4851 opposite sign, the first two transformations will change
4852 the sign of the result. In the last four, the original
4853 expressions give different results for (A=+0, B=-0) and
4854 (A=-0, B=+0), but the transformed expressions do not.
4855
4856 The first two transformations are correct if either A or B
4857 is a NaN. In the first transformation, the condition will
4858 be false, and B will indeed be chosen. In the case of the
4859 second transformation, the condition A != B will be true,
4860 and A will be chosen.
4861
4862 The conversions to max() and min() are not correct if B is
4863 a number and A is not. The conditions in the original
4864 expressions will be false, so all four give B. The min()
4865 and max() versions would give a NaN instead. */
283da5df
RS
4866 if (operand_equal_for_comparison_p (arg01, arg2, arg00)
4867 /* Avoid these transformations if the COND_EXPR may be used
4868 as an lvalue in the C++ front-end. PR c++/19199. */
4869 && (in_gimple_form
6b4e9576
FJ
4870 || (strcmp (lang_hooks.name, "GNU C++") != 0
4871 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
283da5df
RS
4872 || ! maybe_lvalue_p (arg1)
4873 || ! maybe_lvalue_p (arg2)))
2c486ea7
PB
4874 {
4875 tree comp_op0 = arg00;
4876 tree comp_op1 = arg01;
4877 tree comp_type = TREE_TYPE (comp_op0);
4878
4879 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4880 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4881 {
4882 comp_type = type;
2851dd68 4883 comp_op0 = arg1;
2c486ea7
PB
4884 comp_op1 = arg2;
4885 }
4886
4887 switch (comp_code)
4888 {
4889 case EQ_EXPR:
4890 return pedantic_non_lvalue (fold_convert (type, arg2));
4891 case NE_EXPR:
2851dd68 4892 return pedantic_non_lvalue (fold_convert (type, arg1));
2c486ea7
PB
4893 case LE_EXPR:
4894 case LT_EXPR:
3ae472c2
RS
4895 case UNLE_EXPR:
4896 case UNLT_EXPR:
2c486ea7
PB
4897 /* In C++ a ?: expression can be an lvalue, so put the
4898 operand which will be used if they are equal first
4899 so that we can convert this back to the
4900 corresponding COND_EXPR. */
2851dd68 4901 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
e9ea8bd5
RS
4902 {
4903 comp_op0 = fold_convert (comp_type, comp_op0);
4904 comp_op1 = fold_convert (comp_type, comp_op1);
3ae472c2 4905 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
7f20a5b7
KH
4906 ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
4907 : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
e9ea8bd5
RS
4908 return pedantic_non_lvalue (fold_convert (type, tem));
4909 }
2c486ea7
PB
4910 break;
4911 case GE_EXPR:
4912 case GT_EXPR:
3ae472c2
RS
4913 case UNGE_EXPR:
4914 case UNGT_EXPR:
2851dd68 4915 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
e9ea8bd5
RS
4916 {
4917 comp_op0 = fold_convert (comp_type, comp_op0);
4918 comp_op1 = fold_convert (comp_type, comp_op1);
3ae472c2 4919 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
7f20a5b7
KH
4920 ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
4921 : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
e9ea8bd5
RS
4922 return pedantic_non_lvalue (fold_convert (type, tem));
4923 }
2c486ea7 4924 break;
3ae472c2
RS
4925 case UNEQ_EXPR:
4926 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4927 return pedantic_non_lvalue (fold_convert (type, arg2));
4928 break;
4929 case LTGT_EXPR:
4930 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4931 return pedantic_non_lvalue (fold_convert (type, arg1));
4932 break;
2c486ea7 4933 default:
6615c446 4934 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
3ae472c2 4935 break;
2c486ea7
PB
4936 }
4937 }
4938
4939 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4940 we might still be able to simplify this. For example,
4941 if C1 is one less or one more than C2, this might have started
4942 out as a MIN or MAX and been transformed by this function.
4943 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
4944
4945 if (INTEGRAL_TYPE_P (type)
4946 && TREE_CODE (arg01) == INTEGER_CST
4947 && TREE_CODE (arg2) == INTEGER_CST)
4948 switch (comp_code)
4949 {
4950 case EQ_EXPR:
4951 /* We can replace A with C1 in this case. */
2851dd68 4952 arg1 = fold_convert (type, arg01);
7f20a5b7 4953 return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
2c486ea7
PB
4954
4955 case LT_EXPR:
4956 /* If C1 is C2 + 1, this is min(A, C2). */
4957 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4958 OEP_ONLY_CONST)
4959 && operand_equal_p (arg01,
4960 const_binop (PLUS_EXPR, arg2,
000d8d44 4961 build_int_cst (type, 1), 0),
2c486ea7 4962 OEP_ONLY_CONST))
7f20a5b7
KH
4963 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
4964 type, arg1, arg2));
2c486ea7
PB
4965 break;
4966
4967 case LE_EXPR:
4968 /* If C1 is C2 - 1, this is min(A, C2). */
4969 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4970 OEP_ONLY_CONST)
4971 && operand_equal_p (arg01,
4972 const_binop (MINUS_EXPR, arg2,
000d8d44 4973 build_int_cst (type, 1), 0),
2c486ea7 4974 OEP_ONLY_CONST))
7f20a5b7
KH
4975 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
4976 type, arg1, arg2));
2c486ea7
PB
4977 break;
4978
4979 case GT_EXPR:
4980 /* If C1 is C2 - 1, this is max(A, C2). */
4981 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4982 OEP_ONLY_CONST)
4983 && operand_equal_p (arg01,
4984 const_binop (MINUS_EXPR, arg2,
000d8d44 4985 build_int_cst (type, 1), 0),
2c486ea7 4986 OEP_ONLY_CONST))
7f20a5b7
KH
4987 return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
4988 type, arg1, arg2));
2c486ea7
PB
4989 break;
4990
4991 case GE_EXPR:
4992 /* If C1 is C2 + 1, this is max(A, C2). */
4993 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4994 OEP_ONLY_CONST)
4995 && operand_equal_p (arg01,
4996 const_binop (PLUS_EXPR, arg2,
000d8d44 4997 build_int_cst (type, 1), 0),
2c486ea7 4998 OEP_ONLY_CONST))
7f20a5b7
KH
4999 return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5000 type, arg1, arg2));
2c486ea7
PB
5001 break;
5002 case NE_EXPR:
5003 break;
5004 default:
0bccc606 5005 gcc_unreachable ();
2c486ea7
PB
5006 }
5007
5008 return NULL_TREE;
5009}
5010
5011
ebde8a27 5012\f
b8610a53
NS
5013#ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5014#define LOGICAL_OP_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
85e50b6b
DE
5015#endif
5016
ebde8a27
RK
5017/* EXP is some logical combination of boolean tests. See if we can
5018 merge it into some range test. Return the new tree if so. */
ef659ec0 5019
ebde8a27 5020static tree
e1f04615 5021fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
ebde8a27 5022{
e1f04615
KH
5023 int or_op = (code == TRUTH_ORIF_EXPR
5024 || code == TRUTH_OR_EXPR);
ebde8a27
RK
5025 int in0_p, in1_p, in_p;
5026 tree low0, low1, low, high0, high1, high;
6ac01510
ILT
5027 bool strict_overflow_p = false;
5028 tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5029 tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
ebde8a27 5030 tree tem;
6ac01510
ILT
5031 const char * const warnmsg = G_("assuming signed overflow does not occur "
5032 "when simplifying range test");
ef659ec0 5033
ebde8a27
RK
5034 /* If this is an OR operation, invert both sides; we will invert
5035 again at the end. */
5036 if (or_op)
5037 in0_p = ! in0_p, in1_p = ! in1_p;
5038
5039 /* If both expressions are the same, if we can merge the ranges, and we
80906567
RK
5040 can build the range test, return it or it inverted. If one of the
5041 ranges is always true or always false, consider it to be the same
5042 expression as the other. */
5043 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
ebde8a27
RK
5044 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5045 in1_p, low1, high1)
e1f04615 5046 && 0 != (tem = (build_range_check (type,
80906567
RK
5047 lhs != 0 ? lhs
5048 : rhs != 0 ? rhs : integer_zero_node,
ebde8a27 5049 in_p, low, high))))
6ac01510
ILT
5050 {
5051 if (strict_overflow_p)
5052 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5053 return or_op ? invert_truthvalue (tem) : tem;
5054 }
ebde8a27
RK
5055
5056 /* On machines where the branch cost is expensive, if this is a
5057 short-circuited branch and the underlying object on both sides
5058 is the same, make a non-short-circuit operation. */
b8610a53 5059 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
7cf5c9e1 5060 && lhs != 0 && rhs != 0
e1f04615
KH
5061 && (code == TRUTH_ANDIF_EXPR
5062 || code == TRUTH_ORIF_EXPR)
ebde8a27 5063 && operand_equal_p (lhs, rhs, 0))
ef659ec0 5064 {
f0eebf28 5065 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
9ec36da5
JL
5066 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5067 which cases we can't do this. */
ebde8a27 5068 if (simple_operand_p (lhs))
e1f04615 5069 return build2 (code == TRUTH_ANDIF_EXPR
59ce6d6b 5070 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
e1f04615 5071 type, op0, op1);
f0eebf28 5072
5785c7de 5073 else if (lang_hooks.decls.global_bindings_p () == 0
7a6cdb44 5074 && ! CONTAINS_PLACEHOLDER_P (lhs))
ebde8a27
RK
5075 {
5076 tree common = save_expr (lhs);
5077
e1f04615 5078 if (0 != (lhs = build_range_check (type, common,
ebde8a27
RK
5079 or_op ? ! in0_p : in0_p,
5080 low0, high0))
e1f04615 5081 && (0 != (rhs = build_range_check (type, common,
ebde8a27
RK
5082 or_op ? ! in1_p : in1_p,
5083 low1, high1))))
6ac01510
ILT
5084 {
5085 if (strict_overflow_p)
5086 fold_overflow_warning (warnmsg,
5087 WARN_STRICT_OVERFLOW_COMPARISON);
5088 return build2 (code == TRUTH_ANDIF_EXPR
5089 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5090 type, lhs, rhs);
5091 }
ebde8a27 5092 }
ef659ec0 5093 }
de153e82 5094
de153e82 5095 return 0;
ef659ec0
TW
5096}
5097\f
02103577 5098/* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
25216284 5099 bit value. Arrange things so the extra bits will be set to zero if and
d4453ee5
RK
5100 only if C is signed-extended to its full width. If MASK is nonzero,
5101 it is an INTEGER_CST that should be AND'ed with the extra bits. */
02103577
RK
5102
5103static tree
fa8db1f7 5104unextend (tree c, int p, int unsignedp, tree mask)
02103577
RK
5105{
5106 tree type = TREE_TYPE (c);
5107 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5108 tree temp;
5109
5110 if (p == modesize || unsignedp)
5111 return c;
5112
02103577 5113 /* We work by getting just the sign bit into the low-order bit, then
9faa82d8 5114 into the high-order bit, then sign-extend. We then XOR that value
02103577
RK
5115 with C. */
5116 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5117 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
cf85c69b
JW
5118
5119 /* We must use a signed type in order to get an arithmetic right shift.
5120 However, we must also avoid introducing accidental overflows, so that
b6cc0a72 5121 a subsequent call to integer_zerop will work. Hence we must
cf85c69b
JW
5122 do the type conversion here. At this point, the constant is either
5123 zero or one, and the conversion to a signed type can never overflow.
5124 We could get an overflow if this conversion is done anywhere else. */
8df83eae 5125 if (TYPE_UNSIGNED (type))
12753674 5126 temp = fold_convert (signed_type_for (type), temp);
cf85c69b 5127
02103577
RK
5128 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5129 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
d4453ee5 5130 if (mask != 0)
088414c1
RS
5131 temp = const_binop (BIT_AND_EXPR, temp,
5132 fold_convert (TREE_TYPE (c), mask), 0);
cf85c69b 5133 /* If necessary, convert the type back to match the type of C. */
8df83eae 5134 if (TYPE_UNSIGNED (type))
088414c1 5135 temp = fold_convert (type, temp);
d4453ee5 5136
088414c1 5137 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
02103577
RK
5138}
5139\f
b2215d83
TW
5140/* Find ways of folding logical expressions of LHS and RHS:
5141 Try to merge two comparisons to the same innermost item.
5142 Look for range tests like "ch >= '0' && ch <= '9'".
5143 Look for combinations of simple terms on machines with expensive branches
5144 and evaluate the RHS unconditionally.
6d716ca8
RS
5145
5146 For example, if we have p->a == 2 && p->b == 4 and we can make an
5147 object large enough to span both A and B, we can do this with a comparison
5148 against the object ANDed with the a mask.
5149
5150 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5151 operations to do this with one comparison.
5152
5153 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5154 function and the one above.
5155
5156 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5157 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5158
5159 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5160 two operands.
5161
5162 We return the simplified tree or 0 if no optimization is possible. */
5163
5164static tree
fa8db1f7 5165fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
6d716ca8 5166{
f42ef510 5167 /* If this is the "or" of two comparisons, we can do something if
6d716ca8 5168 the comparisons are NE_EXPR. If this is the "and", we can do something
b6cc0a72 5169 if the comparisons are EQ_EXPR. I.e.,
fa8db1f7 5170 (a->b == 2 && a->c == 4) can become (a->new == NEW).
6d716ca8
RS
5171
5172 WANTED_CODE is this operation code. For single bit fields, we can
5173 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5174 comparison for one-bit fields. */
5175
b2215d83 5176 enum tree_code wanted_code;
6d716ca8 5177 enum tree_code lcode, rcode;
b2215d83 5178 tree ll_arg, lr_arg, rl_arg, rr_arg;
6d716ca8 5179 tree ll_inner, lr_inner, rl_inner, rr_inner;
770ae6cc
RK
5180 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5181 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5182 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5183 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
6d716ca8
RS
5184 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5185 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5186 enum machine_mode lnmode, rnmode;
5187 tree ll_mask, lr_mask, rl_mask, rr_mask;
d4453ee5 5188 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
b2215d83 5189 tree l_const, r_const;
bd910dcf 5190 tree lntype, rntype, result;
6d716ca8 5191 int first_bit, end_bit;
b2215d83 5192 int volatilep;
47392a21
MM
5193 tree orig_lhs = lhs, orig_rhs = rhs;
5194 enum tree_code orig_code = code;
6d716ca8 5195
ebde8a27
RK
5196 /* Start by getting the comparison codes. Fail if anything is volatile.
5197 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5198 it were surrounded with a NE_EXPR. */
6d716ca8 5199
ebde8a27 5200 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
b2215d83
TW
5201 return 0;
5202
6d716ca8
RS
5203 lcode = TREE_CODE (lhs);
5204 rcode = TREE_CODE (rhs);
ef659ec0 5205
96d4cf0a 5206 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
59ce6d6b 5207 {
e9ea8bd5 5208 lhs = build2 (NE_EXPR, truth_type, lhs,
57decb7e 5209 build_int_cst (TREE_TYPE (lhs), 0));
59ce6d6b
RS
5210 lcode = NE_EXPR;
5211 }
96d4cf0a
RK
5212
5213 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
59ce6d6b 5214 {
e9ea8bd5 5215 rhs = build2 (NE_EXPR, truth_type, rhs,
57decb7e 5216 build_int_cst (TREE_TYPE (rhs), 0));
59ce6d6b
RS
5217 rcode = NE_EXPR;
5218 }
96d4cf0a 5219
6615c446
JO
5220 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5221 || TREE_CODE_CLASS (rcode) != tcc_comparison)
ef659ec0
TW
5222 return 0;
5223
b2215d83
TW
5224 ll_arg = TREE_OPERAND (lhs, 0);
5225 lr_arg = TREE_OPERAND (lhs, 1);
5226 rl_arg = TREE_OPERAND (rhs, 0);
5227 rr_arg = TREE_OPERAND (rhs, 1);
b6cc0a72 5228
8dcb27ed
RS
5229 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5230 if (simple_operand_p (ll_arg)
d1a7edaf 5231 && simple_operand_p (lr_arg))
8dcb27ed 5232 {
d1a7edaf 5233 tree result;
8dcb27ed
RS
5234 if (operand_equal_p (ll_arg, rl_arg, 0)
5235 && operand_equal_p (lr_arg, rr_arg, 0))
d1a7edaf
PB
5236 {
5237 result = combine_comparisons (code, lcode, rcode,
5238 truth_type, ll_arg, lr_arg);
5239 if (result)
5240 return result;
5241 }
8dcb27ed
RS
5242 else if (operand_equal_p (ll_arg, rr_arg, 0)
5243 && operand_equal_p (lr_arg, rl_arg, 0))
d1a7edaf
PB
5244 {
5245 result = combine_comparisons (code, lcode,
5246 swap_tree_comparison (rcode),
5247 truth_type, ll_arg, lr_arg);
5248 if (result)
5249 return result;
5250 }
8dcb27ed
RS
5251 }
5252
d1a7edaf
PB
5253 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5254 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5255
8227896c 5256 /* If the RHS can be evaluated unconditionally and its operands are
b2215d83
TW
5257 simple, it wins to evaluate the RHS unconditionally on machines
5258 with expensive branches. In this case, this isn't a comparison
1d691c53
RK
5259 that can be merged. Avoid doing this if the RHS is a floating-point
5260 comparison since those can trap. */
b2215d83
TW
5261
5262 if (BRANCH_COST >= 2
1d691c53 5263 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
b2215d83 5264 && simple_operand_p (rl_arg)
8227896c 5265 && simple_operand_p (rr_arg))
01c58f26
RS
5266 {
5267 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5268 if (code == TRUTH_OR_EXPR
5269 && lcode == NE_EXPR && integer_zerop (lr_arg)
5270 && rcode == NE_EXPR && integer_zerop (rr_arg)
5271 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
59ce6d6b
RS
5272 return build2 (NE_EXPR, truth_type,
5273 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5274 ll_arg, rl_arg),
57decb7e 5275 build_int_cst (TREE_TYPE (ll_arg), 0));
01c58f26
RS
5276
5277 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5278 if (code == TRUTH_AND_EXPR
5279 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5280 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5281 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
59ce6d6b
RS
5282 return build2 (EQ_EXPR, truth_type,
5283 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5284 ll_arg, rl_arg),
57decb7e 5285 build_int_cst (TREE_TYPE (ll_arg), 0));
01c58f26 5286
b8610a53 5287 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
47392a21
MM
5288 {
5289 if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5290 return build2 (code, truth_type, lhs, rhs);
5291 return NULL_TREE;
5292 }
01c58f26 5293 }
b2215d83 5294
ef659ec0
TW
5295 /* See if the comparisons can be merged. Then get all the parameters for
5296 each side. */
5297
6d716ca8 5298 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
ef659ec0 5299 || (rcode != EQ_EXPR && rcode != NE_EXPR))
6d716ca8
RS
5300 return 0;
5301
b2215d83
TW
5302 volatilep = 0;
5303 ll_inner = decode_field_reference (ll_arg,
6d716ca8 5304 &ll_bitsize, &ll_bitpos, &ll_mode,
d4453ee5
RK
5305 &ll_unsignedp, &volatilep, &ll_mask,
5306 &ll_and_mask);
b2215d83 5307 lr_inner = decode_field_reference (lr_arg,
6d716ca8 5308 &lr_bitsize, &lr_bitpos, &lr_mode,
d4453ee5
RK
5309 &lr_unsignedp, &volatilep, &lr_mask,
5310 &lr_and_mask);
b2215d83 5311 rl_inner = decode_field_reference (rl_arg,
6d716ca8 5312 &rl_bitsize, &rl_bitpos, &rl_mode,
d4453ee5
RK
5313 &rl_unsignedp, &volatilep, &rl_mask,
5314 &rl_and_mask);
b2215d83 5315 rr_inner = decode_field_reference (rr_arg,
6d716ca8 5316 &rr_bitsize, &rr_bitpos, &rr_mode,
d4453ee5
RK
5317 &rr_unsignedp, &volatilep, &rr_mask,
5318 &rr_and_mask);
6d716ca8
RS
5319
5320 /* It must be true that the inner operation on the lhs of each
5321 comparison must be the same if we are to be able to do anything.
5322 Then see if we have constants. If not, the same must be true for
5323 the rhs's. */
5324 if (volatilep || ll_inner == 0 || rl_inner == 0
5325 || ! operand_equal_p (ll_inner, rl_inner, 0))
5326 return 0;
5327
b2215d83
TW
5328 if (TREE_CODE (lr_arg) == INTEGER_CST
5329 && TREE_CODE (rr_arg) == INTEGER_CST)
5330 l_const = lr_arg, r_const = rr_arg;
6d716ca8
RS
5331 else if (lr_inner == 0 || rr_inner == 0
5332 || ! operand_equal_p (lr_inner, rr_inner, 0))
5333 return 0;
b2215d83
TW
5334 else
5335 l_const = r_const = 0;
6d716ca8
RS
5336
5337 /* If either comparison code is not correct for our logical operation,
5338 fail. However, we can convert a one-bit comparison against zero into
5339 the opposite comparison against that bit being set in the field. */
b2215d83 5340
9c0ae98b 5341 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
6d716ca8
RS
5342 if (lcode != wanted_code)
5343 {
5344 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5a6b3365 5345 {
2bd21a02
AS
5346 /* Make the left operand unsigned, since we are only interested
5347 in the value of one bit. Otherwise we are doing the wrong
5348 thing below. */
5349 ll_unsignedp = 1;
71a874cd 5350 l_const = ll_mask;
5a6b3365 5351 }
6d716ca8
RS
5352 else
5353 return 0;
5354 }
5355
71a874cd 5356 /* This is analogous to the code for l_const above. */
6d716ca8
RS
5357 if (rcode != wanted_code)
5358 {
5359 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5a6b3365 5360 {
2bd21a02 5361 rl_unsignedp = 1;
71a874cd 5362 r_const = rl_mask;
5a6b3365 5363 }
6d716ca8
RS
5364 else
5365 return 0;
5366 }
5367
5368 /* See if we can find a mode that contains both fields being compared on
5369 the left. If we can't, fail. Otherwise, update all constants and masks
5370 to be relative to a field of that size. */
5371 first_bit = MIN (ll_bitpos, rl_bitpos);
5372 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5373 lnmode = get_best_mode (end_bit - first_bit, first_bit,
5374 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5375 volatilep);
5376 if (lnmode == VOIDmode)
5377 return 0;
5378
5379 lnbitsize = GET_MODE_BITSIZE (lnmode);
5380 lnbitpos = first_bit & ~ (lnbitsize - 1);
5785c7de 5381 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
6d716ca8
RS
5382 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5383
f76b9db2
ILT
5384 if (BYTES_BIG_ENDIAN)
5385 {
5386 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5387 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5388 }
6d716ca8 5389
088414c1 5390 ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
91d33e36 5391 size_int (xll_bitpos), 0);
088414c1 5392 rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
91d33e36 5393 size_int (xrl_bitpos), 0);
6d716ca8 5394
6d716ca8
RS
5395 if (l_const)
5396 {
088414c1 5397 l_const = fold_convert (lntype, l_const);
b6cc0a72 5398 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
02103577
RK
5399 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5400 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
7f20a5b7
KH
5401 fold_build1 (BIT_NOT_EXPR,
5402 lntype, ll_mask),
02103577
RK
5403 0)))
5404 {
d4ee4d25 5405 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
b6cc0a72 5406
1b0f3e79 5407 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
02103577 5408 }
6d716ca8
RS
5409 }
5410 if (r_const)
5411 {
088414c1 5412 r_const = fold_convert (lntype, r_const);
d4453ee5 5413 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
02103577
RK
5414 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5415 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
7f20a5b7
KH
5416 fold_build1 (BIT_NOT_EXPR,
5417 lntype, rl_mask),
02103577
RK
5418 0)))
5419 {
d4ee4d25 5420 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
ab87f8c8 5421
1b0f3e79 5422 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
02103577 5423 }
6d716ca8
RS
5424 }
5425
5426 /* If the right sides are not constant, do the same for it. Also,
5427 disallow this optimization if a size or signedness mismatch occurs
5428 between the left and right sides. */
5429 if (l_const == 0)
5430 {
5431 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
e6a28f26
RS
5432 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5433 /* Make sure the two fields on the right
5434 correspond to the left without being swapped. */
5435 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
6d716ca8
RS
5436 return 0;
5437
5438 first_bit = MIN (lr_bitpos, rr_bitpos);
5439 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5440 rnmode = get_best_mode (end_bit - first_bit, first_bit,
5441 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5442 volatilep);
5443 if (rnmode == VOIDmode)
5444 return 0;
5445
5446 rnbitsize = GET_MODE_BITSIZE (rnmode);
5447 rnbitpos = first_bit & ~ (rnbitsize - 1);
5785c7de 5448 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
6d716ca8
RS
5449 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5450
f76b9db2
ILT
5451 if (BYTES_BIG_ENDIAN)
5452 {
5453 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5454 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5455 }
6d716ca8 5456
088414c1 5457 lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
91d33e36 5458 size_int (xlr_bitpos), 0);
088414c1 5459 rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
91d33e36 5460 size_int (xrr_bitpos), 0);
6d716ca8
RS
5461
5462 /* Make a mask that corresponds to both fields being compared.
11a86c56
CH
5463 Do this for both items being compared. If the operands are the
5464 same size and the bits being compared are in the same position
5465 then we can do this by masking both and comparing the masked
5466 results. */
91d33e36
RS
5467 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5468 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
11a86c56 5469 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
6d716ca8 5470 {
bd910dcf 5471 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
6d716ca8 5472 ll_unsignedp || rl_unsignedp);
11a86c56 5473 if (! all_ones_mask_p (ll_mask, lnbitsize))
59ce6d6b 5474 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
11a86c56 5475
bd910dcf 5476 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
6d716ca8 5477 lr_unsignedp || rr_unsignedp);
11a86c56 5478 if (! all_ones_mask_p (lr_mask, rnbitsize))
59ce6d6b 5479 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
11a86c56 5480
59ce6d6b 5481 return build2 (wanted_code, truth_type, lhs, rhs);
6d716ca8
RS
5482 }
5483
5484 /* There is still another way we can do something: If both pairs of
5485 fields being compared are adjacent, we may be able to make a wider
97ea7176
CH
5486 field containing them both.
5487
5488 Note that we still must mask the lhs/rhs expressions. Furthermore,
b6cc0a72 5489 the mask must be shifted to account for the shift done by
97ea7176 5490 make_bit_field_ref. */
6d716ca8
RS
5491 if ((ll_bitsize + ll_bitpos == rl_bitpos
5492 && lr_bitsize + lr_bitpos == rr_bitpos)
5493 || (ll_bitpos == rl_bitpos + rl_bitsize
5494 && lr_bitpos == rr_bitpos + rr_bitsize))
97ea7176 5495 {
bd910dcf
CH
5496 tree type;
5497
5498 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
97ea7176 5499 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
bd910dcf
CH
5500 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
5501 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5502
97ea7176
CH
5503 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5504 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
bd910dcf
CH
5505 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5506 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5507
5508 /* Convert to the smaller type before masking out unwanted bits. */
5509 type = lntype;
5510 if (lntype != rntype)
5511 {
5512 if (lnbitsize > rnbitsize)
5513 {
088414c1
RS
5514 lhs = fold_convert (rntype, lhs);
5515 ll_mask = fold_convert (rntype, ll_mask);
bd910dcf
CH
5516 type = rntype;
5517 }
5518 else if (lnbitsize < rnbitsize)
5519 {
088414c1
RS
5520 rhs = fold_convert (lntype, rhs);
5521 lr_mask = fold_convert (lntype, lr_mask);
bd910dcf
CH
5522 type = lntype;
5523 }
5524 }
5525
97ea7176 5526 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
59ce6d6b 5527 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
97ea7176 5528
97ea7176 5529 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
59ce6d6b 5530 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
97ea7176 5531
59ce6d6b 5532 return build2 (wanted_code, truth_type, lhs, rhs);
97ea7176 5533 }
6d716ca8
RS
5534
5535 return 0;
5536 }
5537
5538 /* Handle the case of comparisons with constants. If there is something in
5539 common between the masks, those bits of the constants must be the same.
5540 If not, the condition is always false. Test for this to avoid generating
5541 incorrect code below. */
91d33e36 5542 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
6d716ca8 5543 if (! integer_zerop (result)
91d33e36
RS
5544 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5545 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
6d716ca8
RS
5546 {
5547 if (wanted_code == NE_EXPR)
5548 {
d4ee4d25 5549 warning (0, "%<or%> of unmatched not-equal tests is always 1");
1b0f3e79 5550 return constant_boolean_node (true, truth_type);
6d716ca8
RS
5551 }
5552 else
5553 {
d4ee4d25 5554 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
1b0f3e79 5555 return constant_boolean_node (false, truth_type);
6d716ca8
RS
5556 }
5557 }
5558
5559 /* Construct the expression we will return. First get the component
5560 reference we will make. Unless the mask is all ones the width of
5561 that field, perform the mask operation. Then compare with the
5562 merged constant. */
bd910dcf 5563 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
6d716ca8
RS
5564 ll_unsignedp || rl_unsignedp);
5565
91d33e36 5566 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
6d716ca8 5567 if (! all_ones_mask_p (ll_mask, lnbitsize))
59ce6d6b 5568 result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
6d716ca8 5569
59ce6d6b
RS
5570 return build2 (wanted_code, truth_type, result,
5571 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
6d716ca8
RS
5572}
5573\f
b6cc0a72 5574/* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
14a774a9
RK
5575 constant. */
5576
5577static tree
d7e5b287 5578optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
14a774a9 5579{
d7e5b287 5580 tree arg0 = op0;
14a774a9 5581 enum tree_code op_code;
d7e5b287 5582 tree comp_const = op1;
14a774a9
RK
5583 tree minmax_const;
5584 int consts_equal, consts_lt;
5585 tree inner;
5586
5587 STRIP_SIGN_NOPS (arg0);
5588
5589 op_code = TREE_CODE (arg0);
5590 minmax_const = TREE_OPERAND (arg0, 1);
5591 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5592 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5593 inner = TREE_OPERAND (arg0, 0);
5594
5595 /* If something does not permit us to optimize, return the original tree. */
5596 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5597 || TREE_CODE (comp_const) != INTEGER_CST
455f14dd 5598 || TREE_OVERFLOW (comp_const)
14a774a9 5599 || TREE_CODE (minmax_const) != INTEGER_CST
455f14dd 5600 || TREE_OVERFLOW (minmax_const))
d7e5b287 5601 return NULL_TREE;
14a774a9
RK
5602
5603 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5604 and GT_EXPR, doing the rest with recursive calls using logical
5605 simplifications. */
d7e5b287 5606 switch (code)
14a774a9
RK
5607 {
5608 case NE_EXPR: case LT_EXPR: case LE_EXPR:
d7e5b287 5609 {
d817ed3b
RG
5610 tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5611 type, op0, op1);
5612 if (tem)
5613 return invert_truthvalue (tem);
5614 return NULL_TREE;
d7e5b287 5615 }
14a774a9
RK
5616
5617 case GE_EXPR:
5618 return
7f20a5b7
KH
5619 fold_build2 (TRUTH_ORIF_EXPR, type,
5620 optimize_minmax_comparison
5621 (EQ_EXPR, type, arg0, comp_const),
5622 optimize_minmax_comparison
5623 (GT_EXPR, type, arg0, comp_const));
14a774a9
RK
5624
5625 case EQ_EXPR:
5626 if (op_code == MAX_EXPR && consts_equal)
5627 /* MAX (X, 0) == 0 -> X <= 0 */
7f20a5b7 5628 return fold_build2 (LE_EXPR, type, inner, comp_const);
14a774a9
RK
5629
5630 else if (op_code == MAX_EXPR && consts_lt)
5631 /* MAX (X, 0) == 5 -> X == 5 */
7f20a5b7 5632 return fold_build2 (EQ_EXPR, type, inner, comp_const);
14a774a9
RK
5633
5634 else if (op_code == MAX_EXPR)
5635 /* MAX (X, 0) == -1 -> false */
5636 return omit_one_operand (type, integer_zero_node, inner);
5637
5638 else if (consts_equal)
5639 /* MIN (X, 0) == 0 -> X >= 0 */
7f20a5b7 5640 return fold_build2 (GE_EXPR, type, inner, comp_const);
14a774a9
RK
5641
5642 else if (consts_lt)
5643 /* MIN (X, 0) == 5 -> false */
5644 return omit_one_operand (type, integer_zero_node, inner);
5645
5646 else
5647 /* MIN (X, 0) == -1 -> X == -1 */
7f20a5b7 5648 return fold_build2 (EQ_EXPR, type, inner, comp_const);
14a774a9
RK
5649
5650 case GT_EXPR:
5651 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5652 /* MAX (X, 0) > 0 -> X > 0
5653 MAX (X, 0) > 5 -> X > 5 */
7f20a5b7 5654 return fold_build2 (GT_EXPR, type, inner, comp_const);
14a774a9
RK
5655
5656 else if (op_code == MAX_EXPR)
5657 /* MAX (X, 0) > -1 -> true */
5658 return omit_one_operand (type, integer_one_node, inner);
5659
5660 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5661 /* MIN (X, 0) > 0 -> false
5662 MIN (X, 0) > 5 -> false */
5663 return omit_one_operand (type, integer_zero_node, inner);
5664
5665 else
5666 /* MIN (X, 0) > -1 -> X > -1 */
7f20a5b7 5667 return fold_build2 (GT_EXPR, type, inner, comp_const);
14a774a9
RK
5668
5669 default:
d7e5b287 5670 return NULL_TREE;
14a774a9
RK
5671 }
5672}
5673\f
1baa375f
RK
5674/* T is an integer expression that is being multiplied, divided, or taken a
5675 modulus (CODE says which and what kind of divide or modulus) by a
5676 constant C. See if we can eliminate that operation by folding it with
5677 other operations already in T. WIDE_TYPE, if non-null, is a type that
5678 should be used for the computation if wider than our type.
5679
cff27795
EB
5680 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5681 (X * 2) + (Y * 4). We must, however, be assured that either the original
8e1ca098
RH
5682 expression would not overflow or that overflow is undefined for the type
5683 in the language in question.
5684
5685 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
5686 the machine has a multiply-accumulate insn or that this is part of an
5687 addressing calculation.
1baa375f
RK
5688
5689 If we return a non-null expression, it is an equivalent form of the
6ac01510
ILT
5690 original computation, but need not be in the original type.
5691
5692 We set *STRICT_OVERFLOW_P to true if the return values depends on
5693 signed overflow being undefined. Otherwise we do not change
5694 *STRICT_OVERFLOW_P. */
1baa375f
RK
5695
5696static tree
6ac01510
ILT
5697extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5698 bool *strict_overflow_p)
cdd4b0d4
AB
5699{
5700 /* To avoid exponential search depth, refuse to allow recursion past
5701 three levels. Beyond that (1) it's highly unlikely that we'll find
5702 something interesting and (2) we've probably processed it before
5703 when we built the inner expression. */
5704
5705 static int depth;
5706 tree ret;
5707
5708 if (depth > 3)
5709 return NULL;
5710
5711 depth++;
6ac01510 5712 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
cdd4b0d4
AB
5713 depth--;
5714
5715 return ret;
5716}
5717
5718static tree
6ac01510
ILT
5719extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5720 bool *strict_overflow_p)
1baa375f
RK
5721{
5722 tree type = TREE_TYPE (t);
5723 enum tree_code tcode = TREE_CODE (t);
b6cc0a72 5724 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
1baa375f
RK
5725 > GET_MODE_SIZE (TYPE_MODE (type)))
5726 ? wide_type : type);
5727 tree t1, t2;
5728 int same_p = tcode == code;
9d0878fd 5729 tree op0 = NULL_TREE, op1 = NULL_TREE;
6ac01510 5730 bool sub_strict_overflow_p;
1baa375f
RK
5731
5732 /* Don't deal with constants of zero here; they confuse the code below. */
5733 if (integer_zerop (c))
8e1ca098 5734 return NULL_TREE;
1baa375f 5735
6615c446 5736 if (TREE_CODE_CLASS (tcode) == tcc_unary)
1baa375f
RK
5737 op0 = TREE_OPERAND (t, 0);
5738
6615c446 5739 if (TREE_CODE_CLASS (tcode) == tcc_binary)
1baa375f
RK
5740 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5741
5742 /* Note that we need not handle conditional operations here since fold
5743 already handles those cases. So just do arithmetic here. */
5744 switch (tcode)
5745 {
5746 case INTEGER_CST:
5747 /* For a constant, we can always simplify if we are a multiply
5748 or (for divide and modulus) if it is a multiple of our constant. */
5749 if (code == MULT_EXPR
5750 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
088414c1
RS
5751 return const_binop (code, fold_convert (ctype, t),
5752 fold_convert (ctype, c), 0);
1baa375f
RK
5753 break;
5754
5755 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
43e4a9d8 5756 /* If op0 is an expression ... */
6615c446
JO
5757 if ((COMPARISON_CLASS_P (op0)
5758 || UNARY_CLASS_P (op0)
5759 || BINARY_CLASS_P (op0)
5039610b 5760 || VL_EXP_CLASS_P (op0)
6615c446 5761 || EXPRESSION_CLASS_P (op0))
43e4a9d8
EB
5762 /* ... and is unsigned, and its type is smaller than ctype,
5763 then we cannot pass through as widening. */
8df83eae 5764 && ((TYPE_UNSIGNED (TREE_TYPE (op0))
43e4a9d8
EB
5765 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5766 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5767 && (GET_MODE_SIZE (TYPE_MODE (ctype))
5768 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
a0fac73d
RS
5769 /* ... or this is a truncation (t is narrower than op0),
5770 then we cannot pass through this narrowing. */
5771 || (GET_MODE_SIZE (TYPE_MODE (type))
068d2c9d
MM
5772 < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
5773 /* ... or signedness changes for division or modulus,
5774 then we cannot pass through this conversion. */
5775 || (code != MULT_EXPR
8df83eae
RK
5776 && (TYPE_UNSIGNED (ctype)
5777 != TYPE_UNSIGNED (TREE_TYPE (op0))))))
eff9c80d
RH
5778 break;
5779
1baa375f 5780 /* Pass the constant down and see if we can make a simplification. If
59adecfa
RK
5781 we can, replace this expression with the inner simplification for
5782 possible later conversion to our or some other type. */
088414c1 5783 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
3cd58fd7 5784 && TREE_CODE (t2) == INTEGER_CST
455f14dd 5785 && !TREE_OVERFLOW (t2)
3cd58fd7
OH
5786 && (0 != (t1 = extract_muldiv (op0, t2, code,
5787 code == MULT_EXPR
6ac01510
ILT
5788 ? ctype : NULL_TREE,
5789 strict_overflow_p))))
1baa375f
RK
5790 return t1;
5791 break;
5792
47d42ce2
JJ
5793 case ABS_EXPR:
5794 /* If widening the type changes it from signed to unsigned, then we
5795 must avoid building ABS_EXPR itself as unsigned. */
5796 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5797 {
12753674 5798 tree cstype = (*signed_type_for) (ctype);
6ac01510
ILT
5799 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5800 != 0)
47d42ce2 5801 {
7f20a5b7 5802 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
47d42ce2
JJ
5803 return fold_convert (ctype, t1);
5804 }
5805 break;
5806 }
5807 /* FALLTHROUGH */
5808 case NEGATE_EXPR:
6ac01510
ILT
5809 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5810 != 0)
7f20a5b7 5811 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
1baa375f
RK
5812 break;
5813
5814 case MIN_EXPR: case MAX_EXPR:
13393c8a
JW
5815 /* If widening the type changes the signedness, then we can't perform
5816 this optimization as that changes the result. */
8df83eae 5817 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
13393c8a
JW
5818 break;
5819
1baa375f 5820 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6ac01510
ILT
5821 sub_strict_overflow_p = false;
5822 if ((t1 = extract_muldiv (op0, c, code, wide_type,
5823 &sub_strict_overflow_p)) != 0
5824 && (t2 = extract_muldiv (op1, c, code, wide_type,
5825 &sub_strict_overflow_p)) != 0)
59adecfa
RK
5826 {
5827 if (tree_int_cst_sgn (c) < 0)
5828 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6ac01510
ILT
5829 if (sub_strict_overflow_p)
5830 *strict_overflow_p = true;
7f20a5b7
KH
5831 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5832 fold_convert (ctype, t2));
59adecfa 5833 }
1baa375f
RK
5834 break;
5835
1baa375f
RK
5836 case LSHIFT_EXPR: case RSHIFT_EXPR:
5837 /* If the second operand is constant, this is a multiplication
5838 or floor division, by a power of two, so we can treat it that
9e629a80
JM
5839 way unless the multiplier or divisor overflows. Signed
5840 left-shift overflow is implementation-defined rather than
5841 undefined in C90, so do not convert signed left shift into
5842 multiplication. */
1baa375f 5843 if (TREE_CODE (op1) == INTEGER_CST
9e629a80 5844 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
d08230fe
NC
5845 /* const_binop may not detect overflow correctly,
5846 so check for it explicitly here. */
5847 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5848 && TREE_INT_CST_HIGH (op1) == 0
088414c1
RS
5849 && 0 != (t1 = fold_convert (ctype,
5850 const_binop (LSHIFT_EXPR,
5851 size_one_node,
5852 op1, 0)))
455f14dd 5853 && !TREE_OVERFLOW (t1))
59ce6d6b
RS
5854 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5855 ? MULT_EXPR : FLOOR_DIV_EXPR,
5856 ctype, fold_convert (ctype, op0), t1),
6ac01510 5857 c, code, wide_type, strict_overflow_p);
1baa375f
RK
5858 break;
5859
5860 case PLUS_EXPR: case MINUS_EXPR:
5861 /* See if we can eliminate the operation on both sides. If we can, we
5862 can return a new PLUS or MINUS. If we can't, the only remaining
5863 cases where we can do anything are if the second operand is a
5864 constant. */
6ac01510
ILT
5865 sub_strict_overflow_p = false;
5866 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5867 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
fba2c0cd
JJ
5868 if (t1 != 0 && t2 != 0
5869 && (code == MULT_EXPR
b77f3744
CE
5870 /* If not multiplication, we can only do this if both operands
5871 are divisible by c. */
5872 || (multiple_of_p (ctype, op0, c)
5873 && multiple_of_p (ctype, op1, c))))
6ac01510
ILT
5874 {
5875 if (sub_strict_overflow_p)
5876 *strict_overflow_p = true;
5877 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5878 fold_convert (ctype, t2));
5879 }
1baa375f 5880
59adecfa
RK
5881 /* If this was a subtraction, negate OP1 and set it to be an addition.
5882 This simplifies the logic below. */
5883 if (tcode == MINUS_EXPR)
5884 tcode = PLUS_EXPR, op1 = negate_expr (op1);
5885
f9011d04
RK
5886 if (TREE_CODE (op1) != INTEGER_CST)
5887 break;
5888
59adecfa
RK
5889 /* If either OP1 or C are negative, this optimization is not safe for
5890 some of the division and remainder types while for others we need
5891 to change the code. */
5892 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5893 {
5894 if (code == CEIL_DIV_EXPR)
5895 code = FLOOR_DIV_EXPR;
59adecfa
RK
5896 else if (code == FLOOR_DIV_EXPR)
5897 code = CEIL_DIV_EXPR;
0629440f
RK
5898 else if (code != MULT_EXPR
5899 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
59adecfa
RK
5900 break;
5901 }
5902
12644a9a
TM
5903 /* If it's a multiply or a division/modulus operation of a multiple
5904 of our constant, do the operation and verify it doesn't overflow. */
5905 if (code == MULT_EXPR
5906 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
dd3f0101 5907 {
088414c1
RS
5908 op1 = const_binop (code, fold_convert (ctype, op1),
5909 fold_convert (ctype, c), 0);
41ba7ed7
RS
5910 /* We allow the constant to overflow with wrapping semantics. */
5911 if (op1 == 0
eeef0e45 5912 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
dd3f0101
KH
5913 break;
5914 }
12644a9a 5915 else
dd3f0101 5916 break;
59adecfa 5917
23cdce68
RH
5918 /* If we have an unsigned type is not a sizetype, we cannot widen
5919 the operation since it will change the result if the original
5920 computation overflowed. */
8df83eae 5921 if (TYPE_UNSIGNED (ctype)
7393c642 5922 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
23cdce68
RH
5923 && ctype != type)
5924 break;
5925
1baa375f 5926 /* If we were able to eliminate our operation from the first side,
59adecfa
RK
5927 apply our operation to the second side and reform the PLUS. */
5928 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
7f20a5b7 5929 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
1baa375f
RK
5930
5931 /* The last case is if we are a multiply. In that case, we can
5932 apply the distributive law to commute the multiply and addition
30f7a378 5933 if the multiplication of the constants doesn't overflow. */
59adecfa 5934 if (code == MULT_EXPR)
7f20a5b7
KH
5935 return fold_build2 (tcode, ctype,
5936 fold_build2 (code, ctype,
5937 fold_convert (ctype, op0),
5938 fold_convert (ctype, c)),
5939 op1);
1baa375f
RK
5940
5941 break;
5942
5943 case MULT_EXPR:
5944 /* We have a special case here if we are doing something like
5945 (C * 8) % 4 since we know that's zero. */
5946 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5947 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5948 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5949 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5950 return omit_one_operand (type, integer_zero_node, op0);
5951
30f7a378 5952 /* ... fall through ... */
1baa375f
RK
5953
5954 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
5955 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
5956 /* If we can extract our operation from the LHS, do so and return a
5957 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
5958 do something only if the second operand is a constant. */
5959 if (same_p
6ac01510
ILT
5960 && (t1 = extract_muldiv (op0, c, code, wide_type,
5961 strict_overflow_p)) != 0)
7f20a5b7
KH
5962 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5963 fold_convert (ctype, op1));
1baa375f 5964 else if (tcode == MULT_EXPR && code == MULT_EXPR
6ac01510
ILT
5965 && (t1 = extract_muldiv (op1, c, code, wide_type,
5966 strict_overflow_p)) != 0)
7f20a5b7
KH
5967 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5968 fold_convert (ctype, t1));
1baa375f
RK
5969 else if (TREE_CODE (op1) != INTEGER_CST)
5970 return 0;
5971
5972 /* If these are the same operation types, we can associate them
5973 assuming no overflow. */
5974 if (tcode == code
088414c1
RS
5975 && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
5976 fold_convert (ctype, c), 0))
455f14dd 5977 && !TREE_OVERFLOW (t1))
7f20a5b7 5978 return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
1baa375f
RK
5979
5980 /* If these operations "cancel" each other, we have the main
5981 optimizations of this pass, which occur when either constant is a
5982 multiple of the other, in which case we replace this with either an
b6cc0a72 5983 operation or CODE or TCODE.
8e1ca098 5984
f5143c46 5985 If we have an unsigned type that is not a sizetype, we cannot do
8e1ca098
RH
5986 this since it will change the result if the original computation
5987 overflowed. */
eeef0e45 5988 if ((TYPE_OVERFLOW_UNDEFINED (ctype)
7393c642 5989 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
8e1ca098
RH
5990 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5991 || (tcode == MULT_EXPR
5992 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5993 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
1baa375f
RK
5994 {
5995 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6ac01510
ILT
5996 {
5997 if (TYPE_OVERFLOW_UNDEFINED (ctype))
5998 *strict_overflow_p = true;
5999 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6000 fold_convert (ctype,
6001 const_binop (TRUNC_DIV_EXPR,
6002 op1, c, 0)));
6003 }
1baa375f 6004 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6ac01510
ILT
6005 {
6006 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6007 *strict_overflow_p = true;
6008 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6009 fold_convert (ctype,
6010 const_binop (TRUNC_DIV_EXPR,
6011 c, op1, 0)));
6012 }
1baa375f
RK
6013 }
6014 break;
6015
6016 default:
6017 break;
6018 }
6019
6020 return 0;
6021}
6022\f
f628873f
MM
6023/* Return a node which has the indicated constant VALUE (either 0 or
6024 1), and is of the indicated TYPE. */
6025
e9ea8bd5 6026tree
fa8db1f7 6027constant_boolean_node (int value, tree type)
f628873f
MM
6028{
6029 if (type == integer_type_node)
6030 return value ? integer_one_node : integer_zero_node;
9bb80bb2
RS
6031 else if (type == boolean_type_node)
6032 return value ? boolean_true_node : boolean_false_node;
b6cc0a72 6033 else
7d60be94 6034 return build_int_cst (type, value);
f628873f
MM
6035}
6036
020d90ee
RG
6037
6038/* Return true if expr looks like an ARRAY_REF and set base and
6039 offset to the appropriate trees. If there is no offset,
4ea73bfa
JL
6040 offset is set to NULL_TREE. Base will be canonicalized to
6041 something you can get the element type from using
0bc52d42 6042 TREE_TYPE (TREE_TYPE (base)). Offset will be the offset
5be014d5 6043 in bytes to the base in sizetype. */
020d90ee
RG
6044
6045static bool
6046extract_array_ref (tree expr, tree *base, tree *offset)
6047{
020d90ee
RG
6048 /* One canonical form is a PLUS_EXPR with the first
6049 argument being an ADDR_EXPR with a possible NOP_EXPR
6050 attached. */
5be014d5 6051 if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
020d90ee
RG
6052 {
6053 tree op0 = TREE_OPERAND (expr, 0);
4ea73bfa
JL
6054 tree inner_base, dummy1;
6055 /* Strip NOP_EXPRs here because the C frontends and/or
5be014d5 6056 folders present us (int *)&x.a p+ 4 possibly. */
020d90ee 6057 STRIP_NOPS (op0);
4ea73bfa 6058 if (extract_array_ref (op0, &inner_base, &dummy1))
020d90ee 6059 {
4ea73bfa 6060 *base = inner_base;
5be014d5
AP
6061 *offset = fold_convert (sizetype, TREE_OPERAND (expr, 1));
6062 if (dummy1 != NULL_TREE)
6063 *offset = fold_build2 (PLUS_EXPR, sizetype,
6064 dummy1, *offset);
020d90ee
RG
6065 return true;
6066 }
6067 }
6068 /* Other canonical form is an ADDR_EXPR of an ARRAY_REF,
6069 which we transform into an ADDR_EXPR with appropriate
6070 offset. For other arguments to the ADDR_EXPR we assume
6071 zero offset and as such do not care about the ADDR_EXPR
6072 type and strip possible nops from it. */
4ea73bfa 6073 else if (TREE_CODE (expr) == ADDR_EXPR)
020d90ee 6074 {
4ea73bfa 6075 tree op0 = TREE_OPERAND (expr, 0);
020d90ee
RG
6076 if (TREE_CODE (op0) == ARRAY_REF)
6077 {
0bc52d42 6078 tree idx = TREE_OPERAND (op0, 1);
4ea73bfa 6079 *base = TREE_OPERAND (op0, 0);
0bc52d42
RG
6080 *offset = fold_build2 (MULT_EXPR, TREE_TYPE (idx), idx,
6081 array_ref_element_size (op0));
5be014d5 6082 *offset = fold_convert (sizetype, *offset);
020d90ee
RG
6083 }
6084 else
6085 {
4ea73bfa
JL
6086 /* Handle array-to-pointer decay as &a. */
6087 if (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE)
6088 *base = TREE_OPERAND (expr, 0);
6089 else
6090 *base = expr;
020d90ee
RG
6091 *offset = NULL_TREE;
6092 }
6093 return true;
6094 }
4ea73bfa
JL
6095 /* The next canonical form is a VAR_DECL with POINTER_TYPE. */
6096 else if (SSA_VAR_P (expr)
6097 && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
6098 {
6099 *base = expr;
6100 *offset = NULL_TREE;
6101 return true;
6102 }
020d90ee
RG
6103
6104 return false;
6105}
6106
6107
1f77b5da 6108/* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
68626d4f
MM
6109 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6110 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
cc2902df 6111 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
68626d4f
MM
6112 COND is the first argument to CODE; otherwise (as in the example
6113 given here), it is the second argument. TYPE is the type of the
2b8a92de 6114 original expression. Return NULL_TREE if no simplification is
b3e65ebb 6115 possible. */
68626d4f
MM
6116
6117static tree
e9da788c
KH
6118fold_binary_op_with_conditional_arg (enum tree_code code,
6119 tree type, tree op0, tree op1,
6120 tree cond, tree arg, int cond_first_p)
68626d4f 6121{
e9da788c 6122 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
92db3ec9 6123 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
68626d4f
MM
6124 tree test, true_value, false_value;
6125 tree lhs = NULL_TREE;
6126 tree rhs = NULL_TREE;
b3e65ebb 6127
f4085d4c 6128 /* This transformation is only worthwhile if we don't have to wrap
35fd3193 6129 arg in a SAVE_EXPR, and the operation can be simplified on at least
f4085d4c
RS
6130 one of the branches once its pushed inside the COND_EXPR. */
6131 if (!TREE_CONSTANT (arg))
b3e65ebb
RS
6132 return NULL_TREE;
6133
68626d4f
MM
6134 if (TREE_CODE (cond) == COND_EXPR)
6135 {
6136 test = TREE_OPERAND (cond, 0);
6137 true_value = TREE_OPERAND (cond, 1);
6138 false_value = TREE_OPERAND (cond, 2);
6139 /* If this operand throws an expression, then it does not make
6140 sense to try to perform a logical or arithmetic operation
f4085d4c 6141 involving it. */
68626d4f 6142 if (VOID_TYPE_P (TREE_TYPE (true_value)))
f4085d4c 6143 lhs = true_value;
68626d4f 6144 if (VOID_TYPE_P (TREE_TYPE (false_value)))
f4085d4c 6145 rhs = false_value;
68626d4f
MM
6146 }
6147 else
6148 {
6149 tree testtype = TREE_TYPE (cond);
6150 test = cond;
1b0f3e79
RS
6151 true_value = constant_boolean_node (true, testtype);
6152 false_value = constant_boolean_node (false, testtype);
68626d4f 6153 }
dd3f0101 6154
3b70b82a 6155 arg = fold_convert (arg_type, arg);
68626d4f 6156 if (lhs == 0)
3b70b82a
DJ
6157 {
6158 true_value = fold_convert (cond_type, true_value);
6405f32f
AP
6159 if (cond_first_p)
6160 lhs = fold_build2 (code, type, true_value, arg);
6161 else
6162 lhs = fold_build2 (code, type, arg, true_value);
3b70b82a 6163 }
68626d4f 6164 if (rhs == 0)
3b70b82a
DJ
6165 {
6166 false_value = fold_convert (cond_type, false_value);
6405f32f
AP
6167 if (cond_first_p)
6168 rhs = fold_build2 (code, type, false_value, arg);
6169 else
6170 rhs = fold_build2 (code, type, arg, false_value);
3b70b82a 6171 }
f4085d4c 6172
7f20a5b7 6173 test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
f4085d4c 6174 return fold_convert (type, test);
68626d4f
MM
6175}
6176
ab87f8c8 6177\f
71925bc0
RS
6178/* Subroutine of fold() that checks for the addition of +/- 0.0.
6179
6180 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6181 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6182 ADDEND is the same as X.
6183
cc2902df 6184 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
71925bc0
RS
6185 and finite. The problematic cases are when X is zero, and its mode
6186 has signed zeros. In the case of rounding towards -infinity,
6187 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6188 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6189
6190static bool
fa8db1f7 6191fold_real_zero_addition_p (tree type, tree addend, int negate)
71925bc0
RS
6192{
6193 if (!real_zerop (addend))
6194 return false;
6195
3bc400cd
RS
6196 /* Don't allow the fold with -fsignaling-nans. */
6197 if (HONOR_SNANS (TYPE_MODE (type)))
6198 return false;
6199
71925bc0
RS
6200 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6201 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6202 return true;
6203
6204 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6205 if (TREE_CODE (addend) == REAL_CST
6206 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6207 negate = !negate;
6208
6209 /* The mode has signed zeros, and we have to honor their sign.
6210 In this situation, there is only one case we can return true for.
6211 X - 0 is the same as X unless rounding towards -infinity is
6212 supported. */
6213 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6214}
6215
c876997f
RS
6216/* Subroutine of fold() that checks comparisons of built-in math
6217 functions against real constants.
6218
6219 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6220 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6221 is the type of the result and ARG0 and ARG1 are the operands of the
6222 comparison. ARG1 must be a TREE_REAL_CST.
6223
6224 The function returns the constant folded tree if a simplification
6225 can be made, and NULL_TREE otherwise. */
6226
6227static tree
75040a04
AJ
6228fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6229 tree type, tree arg0, tree arg1)
c876997f
RS
6230{
6231 REAL_VALUE_TYPE c;
6232
82b4201f 6233 if (BUILTIN_SQRT_P (fcode))
c876997f 6234 {
5039610b 6235 tree arg = CALL_EXPR_ARG (arg0, 0);
c876997f
RS
6236 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6237
6238 c = TREE_REAL_CST (arg1);
6239 if (REAL_VALUE_NEGATIVE (c))
6240 {
6241 /* sqrt(x) < y is always false, if y is negative. */
6242 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
1b0f3e79 6243 return omit_one_operand (type, integer_zero_node, arg);
c876997f
RS
6244
6245 /* sqrt(x) > y is always true, if y is negative and we
6246 don't care about NaNs, i.e. negative values of x. */
6247 if (code == NE_EXPR || !HONOR_NANS (mode))
1b0f3e79 6248 return omit_one_operand (type, integer_one_node, arg);
c876997f
RS
6249
6250 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
7f20a5b7
KH
6251 return fold_build2 (GE_EXPR, type, arg,
6252 build_real (TREE_TYPE (arg), dconst0));
c876997f
RS
6253 }
6254 else if (code == GT_EXPR || code == GE_EXPR)
6255 {
6256 REAL_VALUE_TYPE c2;
6257
6258 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6259 real_convert (&c2, mode, &c2);
6260
6261 if (REAL_VALUE_ISINF (c2))
6262 {
6263 /* sqrt(x) > y is x == +Inf, when y is very large. */
6264 if (HONOR_INFINITIES (mode))
7f20a5b7
KH
6265 return fold_build2 (EQ_EXPR, type, arg,
6266 build_real (TREE_TYPE (arg), c2));
c876997f
RS
6267
6268 /* sqrt(x) > y is always false, when y is very large
6269 and we don't care about infinities. */
1b0f3e79 6270 return omit_one_operand (type, integer_zero_node, arg);
c876997f
RS
6271 }
6272
6273 /* sqrt(x) > c is the same as x > c*c. */
7f20a5b7
KH
6274 return fold_build2 (code, type, arg,
6275 build_real (TREE_TYPE (arg), c2));
c876997f
RS
6276 }
6277 else if (code == LT_EXPR || code == LE_EXPR)
6278 {
6279 REAL_VALUE_TYPE c2;
6280
6281 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6282 real_convert (&c2, mode, &c2);
6283
6284 if (REAL_VALUE_ISINF (c2))
6285 {
6286 /* sqrt(x) < y is always true, when y is a very large
6287 value and we don't care about NaNs or Infinities. */
6288 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
1b0f3e79 6289 return omit_one_operand (type, integer_one_node, arg);
c876997f
RS
6290
6291 /* sqrt(x) < y is x != +Inf when y is very large and we
6292 don't care about NaNs. */
6293 if (! HONOR_NANS (mode))
7f20a5b7
KH
6294 return fold_build2 (NE_EXPR, type, arg,
6295 build_real (TREE_TYPE (arg), c2));
c876997f
RS
6296
6297 /* sqrt(x) < y is x >= 0 when y is very large and we
6298 don't care about Infinities. */
6299 if (! HONOR_INFINITIES (mode))
7f20a5b7
KH
6300 return fold_build2 (GE_EXPR, type, arg,
6301 build_real (TREE_TYPE (arg), dconst0));
c876997f
RS
6302
6303 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
5785c7de 6304 if (lang_hooks.decls.global_bindings_p () != 0
7a6cdb44 6305 || CONTAINS_PLACEHOLDER_P (arg))
c876997f
RS
6306 return NULL_TREE;
6307
6308 arg = save_expr (arg);
7f20a5b7
KH
6309 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6310 fold_build2 (GE_EXPR, type, arg,
6311 build_real (TREE_TYPE (arg),
6312 dconst0)),
6313 fold_build2 (NE_EXPR, type, arg,
6314 build_real (TREE_TYPE (arg),
6315 c2)));
c876997f
RS
6316 }
6317
6318 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6319 if (! HONOR_NANS (mode))
7f20a5b7
KH
6320 return fold_build2 (code, type, arg,
6321 build_real (TREE_TYPE (arg), c2));
c876997f
RS
6322
6323 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
5785c7de 6324 if (lang_hooks.decls.global_bindings_p () == 0
7a6cdb44 6325 && ! CONTAINS_PLACEHOLDER_P (arg))
c876997f
RS
6326 {
6327 arg = save_expr (arg);
7f20a5b7
KH
6328 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6329 fold_build2 (GE_EXPR, type, arg,
6330 build_real (TREE_TYPE (arg),
6331 dconst0)),
6332 fold_build2 (code, type, arg,
6333 build_real (TREE_TYPE (arg),
6334 c2)));
c876997f
RS
6335 }
6336 }
6337 }
6338
6339 return NULL_TREE;
6340}
6341
9ddae796
RS
6342/* Subroutine of fold() that optimizes comparisons against Infinities,
6343 either +Inf or -Inf.
6344
6345 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6346 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6347 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6348
6349 The function returns the constant folded tree if a simplification
6350 can be made, and NULL_TREE otherwise. */
6351
6352static tree
fa8db1f7 6353fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
9ddae796 6354{
18c2511c
RS
6355 enum machine_mode mode;
6356 REAL_VALUE_TYPE max;
6357 tree temp;
6358 bool neg;
6359
6360 mode = TYPE_MODE (TREE_TYPE (arg0));
6361
9ddae796 6362 /* For negative infinity swap the sense of the comparison. */
18c2511c
RS
6363 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6364 if (neg)
9ddae796
RS
6365 code = swap_tree_comparison (code);
6366
6367 switch (code)
6368 {
6369 case GT_EXPR:
6370 /* x > +Inf is always false, if with ignore sNANs. */
18c2511c 6371 if (HONOR_SNANS (mode))
9ddae796 6372 return NULL_TREE;
1b0f3e79 6373 return omit_one_operand (type, integer_zero_node, arg0);
9ddae796
RS
6374
6375 case LE_EXPR:
6376 /* x <= +Inf is always true, if we don't case about NaNs. */
18c2511c 6377 if (! HONOR_NANS (mode))
1b0f3e79 6378 return omit_one_operand (type, integer_one_node, arg0);
9ddae796
RS
6379
6380 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
5785c7de 6381 if (lang_hooks.decls.global_bindings_p () == 0
7a6cdb44 6382 && ! CONTAINS_PLACEHOLDER_P (arg0))
9ddae796
RS
6383 {
6384 arg0 = save_expr (arg0);
7f20a5b7 6385 return fold_build2 (EQ_EXPR, type, arg0, arg0);
9ddae796
RS
6386 }
6387 break;
6388
18c2511c
RS
6389 case EQ_EXPR:
6390 case GE_EXPR:
6391 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6392 real_maxval (&max, neg, mode);
7f20a5b7
KH
6393 return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6394 arg0, build_real (TREE_TYPE (arg0), max));
18c2511c
RS
6395
6396 case LT_EXPR:
6397 /* x < +Inf is always equal to x <= DBL_MAX. */
6398 real_maxval (&max, neg, mode);
7f20a5b7
KH
6399 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6400 arg0, build_real (TREE_TYPE (arg0), max));
18c2511c
RS
6401
6402 case NE_EXPR:
6403 /* x != +Inf is always equal to !(x > DBL_MAX). */
6404 real_maxval (&max, neg, mode);
6405 if (! HONOR_NANS (mode))
7f20a5b7
KH
6406 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6407 arg0, build_real (TREE_TYPE (arg0), max));
3100d647 6408
7f20a5b7
KH
6409 temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6410 arg0, build_real (TREE_TYPE (arg0), max));
6411 return fold_build1 (TRUTH_NOT_EXPR, type, temp);
9ddae796
RS
6412
6413 default:
6414 break;
6415 }
6416
6417 return NULL_TREE;
6418}
71925bc0 6419
8dc2384c 6420/* Subroutine of fold() that optimizes comparisons of a division by
1ea7e6ad 6421 a nonzero integer constant against an integer constant, i.e.
8dc2384c
RS
6422 X/C1 op C2.
6423
6424 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6425 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6426 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6427
6428 The function returns the constant folded tree if a simplification
6429 can be made, and NULL_TREE otherwise. */
6430
6431static tree
6432fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6433{
6434 tree prod, tmp, hi, lo;
6435 tree arg00 = TREE_OPERAND (arg0, 0);
6436 tree arg01 = TREE_OPERAND (arg0, 1);
6437 unsigned HOST_WIDE_INT lpart;
6438 HOST_WIDE_INT hpart;
6b7283ac 6439 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
d56ee62b 6440 bool neg_overflow;
8dc2384c
RS
6441 int overflow;
6442
6443 /* We have to do this the hard way to detect unsigned overflow.
6444 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
6b7283ac
EB
6445 overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6446 TREE_INT_CST_HIGH (arg01),
6447 TREE_INT_CST_LOW (arg1),
6448 TREE_INT_CST_HIGH (arg1),
6449 &lpart, &hpart, unsigned_p);
b8fca551 6450 prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
d95787e6 6451 -1, overflow);
d56ee62b 6452 neg_overflow = false;
8dc2384c 6453
6b7283ac 6454 if (unsigned_p)
8dc2384c 6455 {
000d8d44
RS
6456 tmp = int_const_binop (MINUS_EXPR, arg01,
6457 build_int_cst (TREE_TYPE (arg01), 1), 0);
8dc2384c
RS
6458 lo = prod;
6459
6460 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
6b7283ac
EB
6461 overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6462 TREE_INT_CST_HIGH (prod),
6463 TREE_INT_CST_LOW (tmp),
6464 TREE_INT_CST_HIGH (tmp),
6465 &lpart, &hpart, unsigned_p);
b8fca551 6466 hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
d95787e6 6467 -1, overflow | TREE_OVERFLOW (prod));
8dc2384c
RS
6468 }
6469 else if (tree_int_cst_sgn (arg01) >= 0)
6470 {
000d8d44
RS
6471 tmp = int_const_binop (MINUS_EXPR, arg01,
6472 build_int_cst (TREE_TYPE (arg01), 1), 0);
8dc2384c
RS
6473 switch (tree_int_cst_sgn (arg1))
6474 {
6475 case -1:
d56ee62b 6476 neg_overflow = true;
8dc2384c
RS
6477 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6478 hi = prod;
6479 break;
6480
6481 case 0:
6482 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6483 hi = tmp;
6484 break;
6485
6486 case 1:
6487 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6488 lo = prod;
6489 break;
6490
6491 default:
0bccc606 6492 gcc_unreachable ();
8dc2384c
RS
6493 }
6494 }
6495 else
6496 {
d2e74f6f
RS
6497 /* A negative divisor reverses the relational operators. */
6498 code = swap_tree_comparison (code);
6499
000d8d44
RS
6500 tmp = int_const_binop (PLUS_EXPR, arg01,
6501 build_int_cst (TREE_TYPE (arg01), 1), 0);
8dc2384c
RS
6502 switch (tree_int_cst_sgn (arg1))
6503 {
6504 case -1:
6505 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6506 lo = prod;
6507 break;
6508
6509 case 0:
6510 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6511 lo = tmp;
6512 break;
6513
6514 case 1:
d56ee62b
RS
6515 neg_overflow = true;
6516 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
8dc2384c
RS
6517 hi = prod;
6518 break;
6519
6520 default:
0bccc606 6521 gcc_unreachable ();
8dc2384c
RS
6522 }
6523 }
6524
6525 switch (code)
6526 {
6527 case EQ_EXPR:
6528 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6529 return omit_one_operand (type, integer_zero_node, arg00);
6530 if (TREE_OVERFLOW (hi))
7f20a5b7 6531 return fold_build2 (GE_EXPR, type, arg00, lo);
8dc2384c 6532 if (TREE_OVERFLOW (lo))
7f20a5b7 6533 return fold_build2 (LE_EXPR, type, arg00, hi);
8dc2384c
RS
6534 return build_range_check (type, arg00, 1, lo, hi);
6535
6536 case NE_EXPR:
6537 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6538 return omit_one_operand (type, integer_one_node, arg00);
6539 if (TREE_OVERFLOW (hi))
7f20a5b7 6540 return fold_build2 (LT_EXPR, type, arg00, lo);
8dc2384c 6541 if (TREE_OVERFLOW (lo))
7f20a5b7 6542 return fold_build2 (GT_EXPR, type, arg00, hi);
8dc2384c
RS
6543 return build_range_check (type, arg00, 0, lo, hi);
6544
6545 case LT_EXPR:
6546 if (TREE_OVERFLOW (lo))
d56ee62b
RS
6547 {
6548 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6549 return omit_one_operand (type, tmp, arg00);
6550 }
7f20a5b7 6551 return fold_build2 (LT_EXPR, type, arg00, lo);
8dc2384c
RS
6552
6553 case LE_EXPR:
6554 if (TREE_OVERFLOW (hi))
d56ee62b
RS
6555 {
6556 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6557 return omit_one_operand (type, tmp, arg00);
6558 }
7f20a5b7 6559 return fold_build2 (LE_EXPR, type, arg00, hi);
8dc2384c
RS
6560
6561 case GT_EXPR:
6562 if (TREE_OVERFLOW (hi))
d56ee62b
RS
6563 {
6564 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6565 return omit_one_operand (type, tmp, arg00);
6566 }
7f20a5b7 6567 return fold_build2 (GT_EXPR, type, arg00, hi);
8dc2384c
RS
6568
6569 case GE_EXPR:
6570 if (TREE_OVERFLOW (lo))
d56ee62b
RS
6571 {
6572 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6573 return omit_one_operand (type, tmp, arg00);
6574 }
7f20a5b7 6575 return fold_build2 (GE_EXPR, type, arg00, lo);
8dc2384c
RS
6576
6577 default:
6578 break;
6579 }
6580
6581 return NULL_TREE;
6582}
6583
6584
7960bf22 6585/* If CODE with arguments ARG0 and ARG1 represents a single bit
a94400fd
KH
6586 equality/inequality test, then return a simplified form of the test
6587 using a sign testing. Otherwise return NULL. TYPE is the desired
6588 result type. */
d1822754 6589
a94400fd
KH
6590static tree
6591fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6592 tree result_type)
7960bf22 6593{
7960bf22
JL
6594 /* If this is testing a single bit, we can optimize the test. */
6595 if ((code == NE_EXPR || code == EQ_EXPR)
6596 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6597 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6598 {
7960bf22
JL
6599 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6600 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
a94400fd
KH
6601 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6602
1f7a8dcc
RS
6603 if (arg00 != NULL_TREE
6604 /* This is only a win if casting to a signed type is cheap,
6605 i.e. when arg00's type is not a partial mode. */
6606 && TYPE_PRECISION (TREE_TYPE (arg00))
6607 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
7960bf22 6608 {
12753674 6609 tree stype = signed_type_for (TREE_TYPE (arg00));
7f20a5b7
KH
6610 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6611 result_type, fold_convert (stype, arg00),
57decb7e 6612 build_int_cst (stype, 0));
7960bf22 6613 }
a94400fd
KH
6614 }
6615
6616 return NULL_TREE;
6617}
6618
6619/* If CODE with arguments ARG0 and ARG1 represents a single bit
6620 equality/inequality test, then return a simplified form of
6621 the test using shifts and logical operations. Otherwise return
6622 NULL. TYPE is the desired result type. */
6623
6624tree
6625fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6626 tree result_type)
6627{
6628 /* If this is testing a single bit, we can optimize the test. */
6629 if ((code == NE_EXPR || code == EQ_EXPR)
6630 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6631 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6632 {
6633 tree inner = TREE_OPERAND (arg0, 0);
6634 tree type = TREE_TYPE (arg0);
6635 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6636 enum machine_mode operand_mode = TYPE_MODE (type);
6637 int ops_unsigned;
6638 tree signed_type, unsigned_type, intermediate_type;
000d8d44 6639 tree tem, one;
a94400fd
KH
6640
6641 /* First, see if we can fold the single bit test into a sign-bit
6642 test. */
6643 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6644 result_type);
6645 if (tem)
6646 return tem;
c87d821b 6647
d1822754 6648 /* Otherwise we have (A & C) != 0 where C is a single bit,
7960bf22
JL
6649 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6650 Similarly for (A & C) == 0. */
6651
6652 /* If INNER is a right shift of a constant and it plus BITNUM does
6653 not overflow, adjust BITNUM and INNER. */
6654 if (TREE_CODE (inner) == RSHIFT_EXPR
6655 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6656 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6657 && bitnum < TYPE_PRECISION (type)
6658 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6659 bitnum - TYPE_PRECISION (type)))
6660 {
6661 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6662 inner = TREE_OPERAND (inner, 0);
6663 }
6664
6665 /* If we are going to be able to omit the AND below, we must do our
6666 operations as unsigned. If we must use the AND, we have a choice.
6667 Normally unsigned is faster, but for some machines signed is. */
7960bf22 6668#ifdef LOAD_EXTEND_OP
2a1a3cd5
JJ
6669 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6670 && !flag_syntax_only) ? 0 : 1;
7960bf22 6671#else
c87d821b 6672 ops_unsigned = 1;
7960bf22 6673#endif
7960bf22 6674
5785c7de
RS
6675 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6676 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
e7824b3e 6677 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
088414c1 6678 inner = fold_convert (intermediate_type, inner);
7960bf22
JL
6679
6680 if (bitnum != 0)
59ce6d6b
RS
6681 inner = build2 (RSHIFT_EXPR, intermediate_type,
6682 inner, size_int (bitnum));
7960bf22 6683
000d8d44
RS
6684 one = build_int_cst (intermediate_type, 1);
6685
7960bf22 6686 if (code == EQ_EXPR)
000d8d44 6687 inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
7960bf22
JL
6688
6689 /* Put the AND last so it can combine with more things. */
000d8d44 6690 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
7960bf22
JL
6691
6692 /* Make sure to return the proper type. */
088414c1 6693 inner = fold_convert (result_type, inner);
7960bf22
JL
6694
6695 return inner;
6696 }
6697 return NULL_TREE;
6698}
5dfa45d0 6699
05d362b8
RS
6700/* Check whether we are allowed to reorder operands arg0 and arg1,
6701 such that the evaluation of arg1 occurs before arg0. */
6702
6703static bool
6704reorder_operands_p (tree arg0, tree arg1)
6705{
6706 if (! flag_evaluation_order)
3e6688a7 6707 return true;
05d362b8
RS
6708 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6709 return true;
6710 return ! TREE_SIDE_EFFECTS (arg0)
6711 && ! TREE_SIDE_EFFECTS (arg1);
6712}
6713
37af03cb
RS
6714/* Test whether it is preferable two swap two operands, ARG0 and
6715 ARG1, for example because ARG0 is an integer constant and ARG1
05d362b8
RS
6716 isn't. If REORDER is true, only recommend swapping if we can
6717 evaluate the operands in reverse order. */
37af03cb 6718
fd660b1b 6719bool
05d362b8 6720tree_swap_operands_p (tree arg0, tree arg1, bool reorder)
37af03cb
RS
6721{
6722 STRIP_SIGN_NOPS (arg0);
6723 STRIP_SIGN_NOPS (arg1);
6724
6725 if (TREE_CODE (arg1) == INTEGER_CST)
6726 return 0;
6727 if (TREE_CODE (arg0) == INTEGER_CST)
6728 return 1;
6729
6730 if (TREE_CODE (arg1) == REAL_CST)
6731 return 0;
6732 if (TREE_CODE (arg0) == REAL_CST)
6733 return 1;
6734
6735 if (TREE_CODE (arg1) == COMPLEX_CST)
6736 return 0;
6737 if (TREE_CODE (arg0) == COMPLEX_CST)
6738 return 1;
6739
6740 if (TREE_CONSTANT (arg1))
6741 return 0;
6742 if (TREE_CONSTANT (arg0))
6743 return 1;
d1822754 6744
a352244f
GL
6745 if (optimize_size)
6746 return 0;
37af03cb 6747
05d362b8
RS
6748 if (reorder && flag_evaluation_order
6749 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6750 return 0;
6751
fd660b1b
JL
6752 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6753 for commutative and comparison operators. Ensuring a canonical
6754 form allows the optimizers to find additional redundancies without
6755 having to explicitly check for both orderings. */
6756 if (TREE_CODE (arg0) == SSA_NAME
6757 && TREE_CODE (arg1) == SSA_NAME
6758 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6759 return 1;
6760
421076b5
RG
6761 /* Put SSA_NAMEs last. */
6762 if (TREE_CODE (arg1) == SSA_NAME)
6763 return 0;
6764 if (TREE_CODE (arg0) == SSA_NAME)
6765 return 1;
6766
6767 /* Put variables last. */
6768 if (DECL_P (arg1))
6769 return 0;
6770 if (DECL_P (arg0))
6771 return 1;
6772
37af03cb
RS
6773 return 0;
6774}
6775
18522563
ZD
6776/* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6777 ARG0 is extended to a wider type. */
6778
6779static tree
6780fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
6781{
6782 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6783 tree arg1_unw;
6784 tree shorter_type, outer_type;
6785 tree min, max;
6786 bool above, below;
6787
6788 if (arg0_unw == arg0)
6789 return NULL_TREE;
6790 shorter_type = TREE_TYPE (arg0_unw);
2a0958c5 6791
6c6d9d33
JDA
6792#ifdef HAVE_canonicalize_funcptr_for_compare
6793 /* Disable this optimization if we're casting a function pointer
6794 type on targets that require function pointer canonicalization. */
6795 if (HAVE_canonicalize_funcptr_for_compare
6796 && TREE_CODE (shorter_type) == POINTER_TYPE
6797 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6798 return NULL_TREE;
6799#endif
6800
2a0958c5
JJ
6801 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6802 return NULL_TREE;
6803
18522563 6804 arg1_unw = get_unwidened (arg1, shorter_type);
18522563
ZD
6805
6806 /* If possible, express the comparison in the shorter mode. */
6807 if ((code == EQ_EXPR || code == NE_EXPR
6808 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6809 && (TREE_TYPE (arg1_unw) == shorter_type
6810 || (TREE_CODE (arg1_unw) == INTEGER_CST
a7e1c928
AP
6811 && (TREE_CODE (shorter_type) == INTEGER_TYPE
6812 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
18522563 6813 && int_fits_type_p (arg1_unw, shorter_type))))
7f20a5b7
KH
6814 return fold_build2 (code, type, arg0_unw,
6815 fold_convert (shorter_type, arg1_unw));
18522563 6816
1630e763
AS
6817 if (TREE_CODE (arg1_unw) != INTEGER_CST
6818 || TREE_CODE (shorter_type) != INTEGER_TYPE
6819 || !int_fits_type_p (arg1_unw, shorter_type))
18522563
ZD
6820 return NULL_TREE;
6821
6822 /* If we are comparing with the integer that does not fit into the range
6823 of the shorter type, the result is known. */
6824 outer_type = TREE_TYPE (arg1_unw);
6825 min = lower_bound_in_type (outer_type, shorter_type);
6826 max = upper_bound_in_type (outer_type, shorter_type);
6827
6828 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6829 max, arg1_unw));
6830 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6831 arg1_unw, min));
6832
6833 switch (code)
6834 {
6835 case EQ_EXPR:
6836 if (above || below)
ec7e2228 6837 return omit_one_operand (type, integer_zero_node, arg0);
18522563
ZD
6838 break;
6839
6840 case NE_EXPR:
6841 if (above || below)
ec7e2228 6842 return omit_one_operand (type, integer_one_node, arg0);
18522563
ZD
6843 break;
6844
6845 case LT_EXPR:
6846 case LE_EXPR:
6847 if (above)
ec7e2228 6848 return omit_one_operand (type, integer_one_node, arg0);
18522563 6849 else if (below)
ec7e2228 6850 return omit_one_operand (type, integer_zero_node, arg0);
18522563
ZD
6851
6852 case GT_EXPR:
6853 case GE_EXPR:
6854 if (above)
ec7e2228 6855 return omit_one_operand (type, integer_zero_node, arg0);
18522563 6856 else if (below)
ec7e2228 6857 return omit_one_operand (type, integer_one_node, arg0);
18522563
ZD
6858
6859 default:
6860 break;
6861 }
6862
6863 return NULL_TREE;
6864}
6865
6866/* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6867 ARG0 just the signedness is changed. */
6868
6869static tree
6870fold_sign_changed_comparison (enum tree_code code, tree type,
6871 tree arg0, tree arg1)
6872{
b8fca551 6873 tree arg0_inner;
18522563
ZD
6874 tree inner_type, outer_type;
6875
362cb1bb
RS
6876 if (TREE_CODE (arg0) != NOP_EXPR
6877 && TREE_CODE (arg0) != CONVERT_EXPR)
18522563
ZD
6878 return NULL_TREE;
6879
6880 outer_type = TREE_TYPE (arg0);
6881 arg0_inner = TREE_OPERAND (arg0, 0);
6882 inner_type = TREE_TYPE (arg0_inner);
6883
6c6d9d33
JDA
6884#ifdef HAVE_canonicalize_funcptr_for_compare
6885 /* Disable this optimization if we're casting a function pointer
6886 type on targets that require function pointer canonicalization. */
6887 if (HAVE_canonicalize_funcptr_for_compare
6888 && TREE_CODE (inner_type) == POINTER_TYPE
6889 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6890 return NULL_TREE;
6891#endif
6892
18522563
ZD
6893 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6894 return NULL_TREE;
6895
6896 if (TREE_CODE (arg1) != INTEGER_CST
362cb1bb
RS
6897 && !((TREE_CODE (arg1) == NOP_EXPR
6898 || TREE_CODE (arg1) == CONVERT_EXPR)
18522563
ZD
6899 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6900 return NULL_TREE;
6901
6902 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6903 && code != NE_EXPR
6904 && code != EQ_EXPR)
6905 return NULL_TREE;
6906
6907 if (TREE_CODE (arg1) == INTEGER_CST)
b8fca551
RG
6908 arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
6909 TREE_INT_CST_HIGH (arg1), 0,
d95787e6 6910 TREE_OVERFLOW (arg1));
18522563
ZD
6911 else
6912 arg1 = fold_convert (inner_type, arg1);
6913
7f20a5b7 6914 return fold_build2 (code, type, arg0_inner, arg1);
18522563
ZD
6915}
6916
5be014d5 6917/* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
c5542940
RG
6918 step of the array. Reconstructs s and delta in the case of s * delta
6919 being an integer constant (and thus already folded).
6920 ADDR is the address. MULT is the multiplicative expression.
0c6c135b
RG
6921 If the function succeeds, the new address expression is returned. Otherwise
6922 NULL_TREE is returned. */
38b0dcb8
ZD
6923
6924static tree
5be014d5 6925try_move_mult_to_index (tree addr, tree op1)
38b0dcb8
ZD
6926{
6927 tree s, delta, step;
38b0dcb8
ZD
6928 tree ref = TREE_OPERAND (addr, 0), pref;
6929 tree ret, pos;
6930 tree itype;
713e3ec9 6931 bool mdim = false;
38b0dcb8 6932
5be014d5
AP
6933 /* Strip the nops that might be added when converting op1 to sizetype. */
6934 STRIP_NOPS (op1);
6935
c5542940
RG
6936 /* Canonicalize op1 into a possibly non-constant delta
6937 and an INTEGER_CST s. */
6938 if (TREE_CODE (op1) == MULT_EXPR)
38b0dcb8 6939 {
c5542940
RG
6940 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6941
6942 STRIP_NOPS (arg0);
6943 STRIP_NOPS (arg1);
6944
6945 if (TREE_CODE (arg0) == INTEGER_CST)
6946 {
6947 s = arg0;
6948 delta = arg1;
6949 }
6950 else if (TREE_CODE (arg1) == INTEGER_CST)
6951 {
6952 s = arg1;
6953 delta = arg0;
6954 }
6955 else
6956 return NULL_TREE;
38b0dcb8 6957 }
c5542940 6958 else if (TREE_CODE (op1) == INTEGER_CST)
38b0dcb8 6959 {
c5542940
RG
6960 delta = op1;
6961 s = NULL_TREE;
38b0dcb8
ZD
6962 }
6963 else
c5542940
RG
6964 {
6965 /* Simulate we are delta * 1. */
6966 delta = op1;
6967 s = integer_one_node;
6968 }
38b0dcb8
ZD
6969
6970 for (;; ref = TREE_OPERAND (ref, 0))
6971 {
6972 if (TREE_CODE (ref) == ARRAY_REF)
6973 {
713e3ec9
RG
6974 /* Remember if this was a multi-dimensional array. */
6975 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6976 mdim = true;
6977
03b0db0a
RG
6978 itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6979 if (! itype)
6980 continue;
6981
38b0dcb8 6982 step = array_ref_element_size (ref);
38b0dcb8
ZD
6983 if (TREE_CODE (step) != INTEGER_CST)
6984 continue;
6985
c5542940
RG
6986 if (s)
6987 {
6988 if (! tree_int_cst_equal (step, s))
6989 continue;
6990 }
6991 else
6992 {
6993 /* Try if delta is a multiple of step. */
03b0db0a
RG
6994 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, delta, step);
6995 if (! tmp)
c5542940 6996 continue;
03b0db0a 6997 delta = tmp;
c5542940 6998 }
38b0dcb8 6999
713e3ec9
RG
7000 /* Only fold here if we can verify we do not overflow one
7001 dimension of a multi-dimensional array. */
7002 if (mdim)
7003 {
7004 tree tmp;
7005
7006 if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7007 || !INTEGRAL_TYPE_P (itype)
7008 || !TYPE_MAX_VALUE (itype)
7009 || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
7010 continue;
7011
5be014d5 7012 tmp = fold_binary (PLUS_EXPR, itype,
713e3ec9
RG
7013 fold_convert (itype,
7014 TREE_OPERAND (ref, 1)),
7015 fold_convert (itype, delta));
7016 if (!tmp
7017 || TREE_CODE (tmp) != INTEGER_CST
7018 || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
7019 continue;
7020 }
7021
38b0dcb8
ZD
7022 break;
7023 }
713e3ec9
RG
7024 else
7025 mdim = false;
38b0dcb8
ZD
7026
7027 if (!handled_component_p (ref))
7028 return NULL_TREE;
7029 }
7030
7031 /* We found the suitable array reference. So copy everything up to it,
7032 and replace the index. */
7033
7034 pref = TREE_OPERAND (addr, 0);
7035 ret = copy_node (pref);
7036 pos = ret;
7037
7038 while (pref != ref)
7039 {
7040 pref = TREE_OPERAND (pref, 0);
7041 TREE_OPERAND (pos, 0) = copy_node (pref);
7042 pos = TREE_OPERAND (pos, 0);
7043 }
7044
5be014d5 7045 TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
c5542940
RG
7046 fold_convert (itype,
7047 TREE_OPERAND (pos, 1)),
7048 fold_convert (itype, delta));
38b0dcb8 7049
f457cf40 7050 return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
38b0dcb8
ZD
7051}
7052
1d481ba8
ZD
7053
7054/* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7055 means A >= Y && A != MAX, but in this case we know that
7056 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7057
7058static tree
7059fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
7060{
7061 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7062
7063 if (TREE_CODE (bound) == LT_EXPR)
7064 a = TREE_OPERAND (bound, 0);
7065 else if (TREE_CODE (bound) == GT_EXPR)
7066 a = TREE_OPERAND (bound, 1);
7067 else
7068 return NULL_TREE;
7069
7070 typea = TREE_TYPE (a);
7071 if (!INTEGRAL_TYPE_P (typea)
7072 && !POINTER_TYPE_P (typea))
7073 return NULL_TREE;
7074
7075 if (TREE_CODE (ineq) == LT_EXPR)
7076 {
7077 a1 = TREE_OPERAND (ineq, 1);
7078 y = TREE_OPERAND (ineq, 0);
7079 }
7080 else if (TREE_CODE (ineq) == GT_EXPR)
7081 {
7082 a1 = TREE_OPERAND (ineq, 0);
7083 y = TREE_OPERAND (ineq, 1);
7084 }
7085 else
7086 return NULL_TREE;
7087
7088 if (TREE_TYPE (a1) != typea)
7089 return NULL_TREE;
7090
5be014d5
AP
7091 if (POINTER_TYPE_P (typea))
7092 {
7093 /* Convert the pointer types into integer before taking the difference. */
7094 tree ta = fold_convert (ssizetype, a);
7095 tree ta1 = fold_convert (ssizetype, a1);
7096 diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7097 }
7098 else
7099 diff = fold_binary (MINUS_EXPR, typea, a1, a);
7100
7101 if (!diff || !integer_onep (diff))
7102 return NULL_TREE;
1d481ba8 7103
7f20a5b7 7104 return fold_build2 (GE_EXPR, type, a, y);
1d481ba8
ZD
7105}
7106
0ed9a3e3
RG
7107/* Fold a sum or difference of at least one multiplication.
7108 Returns the folded tree or NULL if no simplification could be made. */
7109
7110static tree
7111fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7112{
7113 tree arg00, arg01, arg10, arg11;
7114 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7115
7116 /* (A * C) +- (B * C) -> (A+-B) * C.
7117 (A * C) +- A -> A * (C+-1).
7118 We are most concerned about the case where C is a constant,
7119 but other combinations show up during loop reduction. Since
7120 it is not difficult, try all four possibilities. */
7121
7122 if (TREE_CODE (arg0) == MULT_EXPR)
7123 {
7124 arg00 = TREE_OPERAND (arg0, 0);
7125 arg01 = TREE_OPERAND (arg0, 1);
7126 }
7127 else
7128 {
7129 arg00 = arg0;
bfabddb6 7130 arg01 = build_one_cst (type);
0ed9a3e3
RG
7131 }
7132 if (TREE_CODE (arg1) == MULT_EXPR)
7133 {
7134 arg10 = TREE_OPERAND (arg1, 0);
7135 arg11 = TREE_OPERAND (arg1, 1);
7136 }
7137 else
7138 {
7139 arg10 = arg1;
bfabddb6 7140 arg11 = build_one_cst (type);
0ed9a3e3
RG
7141 }
7142 same = NULL_TREE;
7143
7144 if (operand_equal_p (arg01, arg11, 0))
7145 same = arg01, alt0 = arg00, alt1 = arg10;
7146 else if (operand_equal_p (arg00, arg10, 0))
7147 same = arg00, alt0 = arg01, alt1 = arg11;
7148 else if (operand_equal_p (arg00, arg11, 0))
7149 same = arg00, alt0 = arg01, alt1 = arg10;
7150 else if (operand_equal_p (arg01, arg10, 0))
7151 same = arg01, alt0 = arg00, alt1 = arg11;
7152
7153 /* No identical multiplicands; see if we can find a common
7154 power-of-two factor in non-power-of-two multiplies. This
7155 can help in multi-dimensional array access. */
7156 else if (host_integerp (arg01, 0)
7157 && host_integerp (arg11, 0))
7158 {
7159 HOST_WIDE_INT int01, int11, tmp;
7160 bool swap = false;
7161 tree maybe_same;
7162 int01 = TREE_INT_CST_LOW (arg01);
7163 int11 = TREE_INT_CST_LOW (arg11);
7164
7165 /* Move min of absolute values to int11. */
7166 if ((int01 >= 0 ? int01 : -int01)
7167 < (int11 >= 0 ? int11 : -int11))
7168 {
7169 tmp = int01, int01 = int11, int11 = tmp;
7170 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7171 maybe_same = arg01;
7172 swap = true;
7173 }
7174 else
7175 maybe_same = arg11;
7176
b0cd88d2 7177 if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
0ed9a3e3
RG
7178 {
7179 alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7180 build_int_cst (TREE_TYPE (arg00),
7181 int01 / int11));
7182 alt1 = arg10;
7183 same = maybe_same;
7184 if (swap)
7185 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7186 }
7187 }
7188
7189 if (same)
7190 return fold_build2 (MULT_EXPR, type,
7191 fold_build2 (code, type,
7192 fold_convert (type, alt0),
7193 fold_convert (type, alt1)),
7194 fold_convert (type, same));
7195
7196 return NULL_TREE;
7197}
7198
78bf6e2f
RS
7199/* Subroutine of native_encode_expr. Encode the INTEGER_CST
7200 specified by EXPR into the buffer PTR of length LEN bytes.
7201 Return the number of bytes placed in the buffer, or zero
7202 upon failure. */
7203
7204static int
7205native_encode_int (tree expr, unsigned char *ptr, int len)
7206{
7207 tree type = TREE_TYPE (expr);
7208 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7209 int byte, offset, word, words;
7210 unsigned char value;
7211
7212 if (total_bytes > len)
7213 return 0;
7214 words = total_bytes / UNITS_PER_WORD;
7215
7216 for (byte = 0; byte < total_bytes; byte++)
7217 {
7218 int bitpos = byte * BITS_PER_UNIT;
7219 if (bitpos < HOST_BITS_PER_WIDE_INT)
7220 value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7221 else
7222 value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7223 >> (bitpos - HOST_BITS_PER_WIDE_INT));
7224
7225 if (total_bytes > UNITS_PER_WORD)
7226 {
7227 word = byte / UNITS_PER_WORD;
7228 if (WORDS_BIG_ENDIAN)
7229 word = (words - 1) - word;
7230 offset = word * UNITS_PER_WORD;
7231 if (BYTES_BIG_ENDIAN)
7232 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7233 else
7234 offset += byte % UNITS_PER_WORD;
7235 }
7236 else
7237 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7238 ptr[offset] = value;
7239 }
7240 return total_bytes;
7241}
7242
7243
7244/* Subroutine of native_encode_expr. Encode the REAL_CST
7245 specified by EXPR into the buffer PTR of length LEN bytes.
7246 Return the number of bytes placed in the buffer, or zero
7247 upon failure. */
7248
7249static int
7250native_encode_real (tree expr, unsigned char *ptr, int len)
7251{
7252 tree type = TREE_TYPE (expr);
7253 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
0a9430a8 7254 int byte, offset, word, words, bitpos;
78bf6e2f
RS
7255 unsigned char value;
7256
7257 /* There are always 32 bits in each long, no matter the size of
7258 the hosts long. We handle floating point representations with
7259 up to 192 bits. */
7260 long tmp[6];
7261
7262 if (total_bytes > len)
7263 return 0;
0a9430a8 7264 words = 32 / UNITS_PER_WORD;
78bf6e2f
RS
7265
7266 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7267
0a9430a8
JJ
7268 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7269 bitpos += BITS_PER_UNIT)
78bf6e2f 7270 {
0a9430a8 7271 byte = (bitpos / BITS_PER_UNIT) & 3;
78bf6e2f
RS
7272 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7273
0a9430a8 7274 if (UNITS_PER_WORD < 4)
78bf6e2f
RS
7275 {
7276 word = byte / UNITS_PER_WORD;
0a9430a8 7277 if (WORDS_BIG_ENDIAN)
78bf6e2f
RS
7278 word = (words - 1) - word;
7279 offset = word * UNITS_PER_WORD;
7280 if (BYTES_BIG_ENDIAN)
7281 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7282 else
7283 offset += byte % UNITS_PER_WORD;
7284 }
7285 else
0a9430a8
JJ
7286 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7287 ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
78bf6e2f
RS
7288 }
7289 return total_bytes;
7290}
7291
7292/* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7293 specified by EXPR into the buffer PTR of length LEN bytes.
7294 Return the number of bytes placed in the buffer, or zero
7295 upon failure. */
7296
7297static int
7298native_encode_complex (tree expr, unsigned char *ptr, int len)
7299{
7300 int rsize, isize;
7301 tree part;
7302
7303 part = TREE_REALPART (expr);
7304 rsize = native_encode_expr (part, ptr, len);
7305 if (rsize == 0)
7306 return 0;
7307 part = TREE_IMAGPART (expr);
7308 isize = native_encode_expr (part, ptr+rsize, len-rsize);
7309 if (isize != rsize)
7310 return 0;
7311 return rsize + isize;
7312}
7313
7314
7315/* Subroutine of native_encode_expr. Encode the VECTOR_CST
7316 specified by EXPR into the buffer PTR of length LEN bytes.
7317 Return the number of bytes placed in the buffer, or zero
7318 upon failure. */
7319
7320static int
7321native_encode_vector (tree expr, unsigned char *ptr, int len)
7322{
15b1c12a 7323 int i, size, offset, count;
1000b34d 7324 tree itype, elem, elements;
78bf6e2f 7325
78bf6e2f
RS
7326 offset = 0;
7327 elements = TREE_VECTOR_CST_ELTS (expr);
7328 count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
1000b34d
RS
7329 itype = TREE_TYPE (TREE_TYPE (expr));
7330 size = GET_MODE_SIZE (TYPE_MODE (itype));
78bf6e2f
RS
7331 for (i = 0; i < count; i++)
7332 {
7333 if (elements)
7334 {
7335 elem = TREE_VALUE (elements);
7336 elements = TREE_CHAIN (elements);
7337 }
7338 else
7339 elem = NULL_TREE;
7340
7341 if (elem)
7342 {
1000b34d 7343 if (native_encode_expr (elem, ptr+offset, len-offset) != size)
78bf6e2f
RS
7344 return 0;
7345 }
1000b34d 7346 else
78bf6e2f
RS
7347 {
7348 if (offset + size > len)
7349 return 0;
7350 memset (ptr+offset, 0, size);
7351 }
78bf6e2f
RS
7352 offset += size;
7353 }
7354 return offset;
7355}
7356
7357
7358/* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7359 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7360 buffer PTR of length LEN bytes. Return the number of bytes
7361 placed in the buffer, or zero upon failure. */
7362
db136335 7363int
78bf6e2f
RS
7364native_encode_expr (tree expr, unsigned char *ptr, int len)
7365{
7366 switch (TREE_CODE (expr))
7367 {
7368 case INTEGER_CST:
7369 return native_encode_int (expr, ptr, len);
7370
7371 case REAL_CST:
7372 return native_encode_real (expr, ptr, len);
7373
7374 case COMPLEX_CST:
7375 return native_encode_complex (expr, ptr, len);
7376
7377 case VECTOR_CST:
7378 return native_encode_vector (expr, ptr, len);
7379
7380 default:
7381 return 0;
7382 }
7383}
7384
7385
7386/* Subroutine of native_interpret_expr. Interpret the contents of
7387 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7388 If the buffer cannot be interpreted, return NULL_TREE. */
7389
7390static tree
7391native_interpret_int (tree type, unsigned char *ptr, int len)
7392{
7393 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7394 int byte, offset, word, words;
7395 unsigned char value;
7396 unsigned int HOST_WIDE_INT lo = 0;
7397 HOST_WIDE_INT hi = 0;
7398
7399 if (total_bytes > len)
7400 return NULL_TREE;
7401 if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7402 return NULL_TREE;
7403 words = total_bytes / UNITS_PER_WORD;
7404
7405 for (byte = 0; byte < total_bytes; byte++)
7406 {
7407 int bitpos = byte * BITS_PER_UNIT;
7408 if (total_bytes > UNITS_PER_WORD)
7409 {
7410 word = byte / UNITS_PER_WORD;
7411 if (WORDS_BIG_ENDIAN)
7412 word = (words - 1) - word;
7413 offset = word * UNITS_PER_WORD;
7414 if (BYTES_BIG_ENDIAN)
7415 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7416 else
7417 offset += byte % UNITS_PER_WORD;
7418 }
7419 else
7420 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7421 value = ptr[offset];
7422
7423 if (bitpos < HOST_BITS_PER_WIDE_INT)
7424 lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7425 else
7426 hi |= (unsigned HOST_WIDE_INT) value
7427 << (bitpos - HOST_BITS_PER_WIDE_INT);
7428 }
7429
2ac7cbb5 7430 return build_int_cst_wide_type (type, lo, hi);
78bf6e2f
RS
7431}
7432
7433
7434/* Subroutine of native_interpret_expr. Interpret the contents of
7435 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7436 If the buffer cannot be interpreted, return NULL_TREE. */
7437
7438static tree
7439native_interpret_real (tree type, unsigned char *ptr, int len)
7440{
15b1c12a
RS
7441 enum machine_mode mode = TYPE_MODE (type);
7442 int total_bytes = GET_MODE_SIZE (mode);
0a9430a8 7443 int byte, offset, word, words, bitpos;
78bf6e2f
RS
7444 unsigned char value;
7445 /* There are always 32 bits in each long, no matter the size of
7446 the hosts long. We handle floating point representations with
7447 up to 192 bits. */
7448 REAL_VALUE_TYPE r;
7449 long tmp[6];
7450
7451 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7452 if (total_bytes > len || total_bytes > 24)
7453 return NULL_TREE;
0a9430a8 7454 words = 32 / UNITS_PER_WORD;
78bf6e2f
RS
7455
7456 memset (tmp, 0, sizeof (tmp));
0a9430a8
JJ
7457 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7458 bitpos += BITS_PER_UNIT)
78bf6e2f 7459 {
0a9430a8
JJ
7460 byte = (bitpos / BITS_PER_UNIT) & 3;
7461 if (UNITS_PER_WORD < 4)
78bf6e2f
RS
7462 {
7463 word = byte / UNITS_PER_WORD;
0a9430a8 7464 if (WORDS_BIG_ENDIAN)
78bf6e2f
RS
7465 word = (words - 1) - word;
7466 offset = word * UNITS_PER_WORD;
7467 if (BYTES_BIG_ENDIAN)
7468 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7469 else
7470 offset += byte % UNITS_PER_WORD;
7471 }
7472 else
0a9430a8
JJ
7473 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7474 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
78bf6e2f
RS
7475
7476 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7477 }
7478
7479 real_from_target (&r, tmp, mode);
7480 return build_real (type, r);
7481}
7482
7483
7484/* Subroutine of native_interpret_expr. Interpret the contents of
7485 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7486 If the buffer cannot be interpreted, return NULL_TREE. */
7487
7488static tree
7489native_interpret_complex (tree type, unsigned char *ptr, int len)
7490{
7491 tree etype, rpart, ipart;
7492 int size;
7493
7494 etype = TREE_TYPE (type);
7495 size = GET_MODE_SIZE (TYPE_MODE (etype));
7496 if (size * 2 > len)
7497 return NULL_TREE;
7498 rpart = native_interpret_expr (etype, ptr, size);
7499 if (!rpart)
7500 return NULL_TREE;
7501 ipart = native_interpret_expr (etype, ptr+size, size);
7502 if (!ipart)
7503 return NULL_TREE;
7504 return build_complex (type, rpart, ipart);
7505}
7506
7507
7508/* Subroutine of native_interpret_expr. Interpret the contents of
7509 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7510 If the buffer cannot be interpreted, return NULL_TREE. */
7511
7512static tree
7513native_interpret_vector (tree type, unsigned char *ptr, int len)
7514{
7515 tree etype, elem, elements;
7516 int i, size, count;
7517
7518 etype = TREE_TYPE (type);
7519 size = GET_MODE_SIZE (TYPE_MODE (etype));
7520 count = TYPE_VECTOR_SUBPARTS (type);
7521 if (size * count > len)
7522 return NULL_TREE;
7523
7524 elements = NULL_TREE;
7525 for (i = count - 1; i >= 0; i--)
7526 {
7527 elem = native_interpret_expr (etype, ptr+(i*size), size);
7528 if (!elem)
7529 return NULL_TREE;
7530 elements = tree_cons (NULL_TREE, elem, elements);
7531 }
7532 return build_vector (type, elements);
7533}
7534
7535
75c40d56 7536/* Subroutine of fold_view_convert_expr. Interpret the contents of
78bf6e2f
RS
7537 the buffer PTR of length LEN as a constant of type TYPE. For
7538 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7539 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7540 return NULL_TREE. */
7541
db136335 7542tree
78bf6e2f
RS
7543native_interpret_expr (tree type, unsigned char *ptr, int len)
7544{
7545 switch (TREE_CODE (type))
7546 {
7547 case INTEGER_TYPE:
7548 case ENUMERAL_TYPE:
7549 case BOOLEAN_TYPE:
7550 return native_interpret_int (type, ptr, len);
7551
7552 case REAL_TYPE:
7553 return native_interpret_real (type, ptr, len);
7554
7555 case COMPLEX_TYPE:
7556 return native_interpret_complex (type, ptr, len);
7557
7558 case VECTOR_TYPE:
7559 return native_interpret_vector (type, ptr, len);
7560
7561 default:
7562 return NULL_TREE;
7563 }
7564}
7565
7566
7567/* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7568 TYPE at compile-time. If we're unable to perform the conversion
7569 return NULL_TREE. */
7570
7571static tree
7572fold_view_convert_expr (tree type, tree expr)
7573{
7574 /* We support up to 512-bit values (for V8DFmode). */
7575 unsigned char buffer[64];
7576 int len;
7577
7578 /* Check that the host and target are sane. */
7579 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7580 return NULL_TREE;
7581
7582 len = native_encode_expr (expr, buffer, sizeof (buffer));
7583 if (len == 0)
7584 return NULL_TREE;
7585
7586 return native_interpret_expr (type, buffer, len);
7587}
7588
70826cbb
SP
7589/* Build an expression for the address of T. Folds away INDIRECT_REF
7590 to avoid confusing the gimplify process. When IN_FOLD is true
7591 avoid modifications of T. */
7592
7593static tree
7594build_fold_addr_expr_with_type_1 (tree t, tree ptrtype, bool in_fold)
7595{
7596 /* The size of the object is not relevant when talking about its address. */
7597 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7598 t = TREE_OPERAND (t, 0);
7599
7600 /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7601 if (TREE_CODE (t) == INDIRECT_REF
7602 || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7603 {
7604 t = TREE_OPERAND (t, 0);
7605
7606 if (TREE_TYPE (t) != ptrtype)
7607 t = build1 (NOP_EXPR, ptrtype, t);
7608 }
7609 else if (!in_fold)
7610 {
7611 tree base = t;
7612
7613 while (handled_component_p (base))
7614 base = TREE_OPERAND (base, 0);
7615
7616 if (DECL_P (base))
7617 TREE_ADDRESSABLE (base) = 1;
7618
7619 t = build1 (ADDR_EXPR, ptrtype, t);
7620 }
7621 else
7622 t = build1 (ADDR_EXPR, ptrtype, t);
7623
7624 return t;
7625}
7626
7627/* Build an expression for the address of T with type PTRTYPE. This
7628 function modifies the input parameter 'T' by sometimes setting the
7629 TREE_ADDRESSABLE flag. */
7630
7631tree
7632build_fold_addr_expr_with_type (tree t, tree ptrtype)
7633{
7634 return build_fold_addr_expr_with_type_1 (t, ptrtype, false);
7635}
7636
7637/* Build an expression for the address of T. This function modifies
7638 the input parameter 'T' by sometimes setting the TREE_ADDRESSABLE
7639 flag. When called from fold functions, use fold_addr_expr instead. */
7640
7641tree
7642build_fold_addr_expr (tree t)
7643{
7644 return build_fold_addr_expr_with_type_1 (t,
7645 build_pointer_type (TREE_TYPE (t)),
7646 false);
7647}
7648
7649/* Same as build_fold_addr_expr, builds an expression for the address
7650 of T, but avoids touching the input node 't'. Fold functions
7651 should use this version. */
7652
7653static tree
7654fold_addr_expr (tree t)
7655{
7656 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7657
7658 return build_fold_addr_expr_with_type_1 (t, ptrtype, true);
7659}
78bf6e2f 7660
7107fa7c
KH
7661/* Fold a unary expression of code CODE and type TYPE with operand
7662 OP0. Return the folded expression if folding is successful.
7663 Otherwise, return NULL_TREE. */
659d8efa 7664
721425b6 7665tree
fbaa905c 7666fold_unary (enum tree_code code, tree type, tree op0)
659d8efa 7667{
659d8efa 7668 tree tem;
fbaa905c 7669 tree arg0;
659d8efa
KH
7670 enum tree_code_class kind = TREE_CODE_CLASS (code);
7671
7672 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7673 && TREE_CODE_LENGTH (code) == 1);
7674
fbaa905c 7675 arg0 = op0;
659d8efa
KH
7676 if (arg0)
7677 {
b49ceb45
JM
7678 if (code == NOP_EXPR || code == CONVERT_EXPR
7679 || code == FLOAT_EXPR || code == ABS_EXPR)
659d8efa 7680 {
b49ceb45
JM
7681 /* Don't use STRIP_NOPS, because signedness of argument type
7682 matters. */
659d8efa
KH
7683 STRIP_SIGN_NOPS (arg0);
7684 }
7685 else
7686 {
7687 /* Strip any conversions that don't change the mode. This
7688 is safe for every expression, except for a comparison
7689 expression because its signedness is derived from its
7690 operands.
7691
7692 Note that this is done as an internal manipulation within
7693 the constant folder, in order to find the simplest
7694 representation of the arguments so that their form can be
7695 studied. In any cases, the appropriate type conversions
7696 should be put back in the tree that will get out of the
7697 constant folder. */
7698 STRIP_NOPS (arg0);
7699 }
7700 }
7701
7702 if (TREE_CODE_CLASS (code) == tcc_unary)
7703 {
7704 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7705 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7f20a5b7 7706 fold_build1 (code, type, TREE_OPERAND (arg0, 1)));
659d8efa
KH
7707 else if (TREE_CODE (arg0) == COND_EXPR)
7708 {
7709 tree arg01 = TREE_OPERAND (arg0, 1);
7710 tree arg02 = TREE_OPERAND (arg0, 2);
7711 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7f20a5b7 7712 arg01 = fold_build1 (code, type, arg01);
659d8efa 7713 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7f20a5b7
KH
7714 arg02 = fold_build1 (code, type, arg02);
7715 tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
7716 arg01, arg02);
659d8efa
KH
7717
7718 /* If this was a conversion, and all we did was to move into
7719 inside the COND_EXPR, bring it back out. But leave it if
7720 it is a conversion from integer to integer and the
7721 result precision is no wider than a word since such a
7722 conversion is cheap and may be optimized away by combine,
7723 while it couldn't if it were outside the COND_EXPR. Then return
7724 so we don't get into an infinite recursion loop taking the
7725 conversion out and then back in. */
7726
7727 if ((code == NOP_EXPR || code == CONVERT_EXPR
7728 || code == NON_LVALUE_EXPR)
7729 && TREE_CODE (tem) == COND_EXPR
7730 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7731 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7732 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7733 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7734 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7735 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7736 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7737 && (INTEGRAL_TYPE_P
7738 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7739 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7740 || flag_syntax_only))
7741 tem = build1 (code, type,
7742 build3 (COND_EXPR,
7743 TREE_TYPE (TREE_OPERAND
7744 (TREE_OPERAND (tem, 1), 0)),
7745 TREE_OPERAND (tem, 0),
7746 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7747 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
7748 return tem;
7749 }
7750 else if (COMPARISON_CLASS_P (arg0))
7751 {
7752 if (TREE_CODE (type) == BOOLEAN_TYPE)
7753 {
7754 arg0 = copy_node (arg0);
7755 TREE_TYPE (arg0) = type;
7756 return arg0;
7757 }
7758 else if (TREE_CODE (type) != INTEGER_TYPE)
7f20a5b7
KH
7759 return fold_build3 (COND_EXPR, type, arg0,
7760 fold_build1 (code, type,
7761 integer_one_node),
7762 fold_build1 (code, type,
7763 integer_zero_node));
659d8efa
KH
7764 }
7765 }
7766
7767 switch (code)
7768 {
7769 case NOP_EXPR:
7770 case FLOAT_EXPR:
7771 case CONVERT_EXPR:
7772 case FIX_TRUNC_EXPR:
4b58fc4d
KH
7773 if (TREE_TYPE (op0) == type)
7774 return op0;
d998dd65 7775
6416ae7f 7776 /* If we have (type) (a CMP b) and type is an integral type, return
d998dd65
AP
7777 new expression involving the new type. */
7778 if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7779 return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7780 TREE_OPERAND (op0, 1));
659d8efa
KH
7781
7782 /* Handle cases of two conversions in a row. */
4b58fc4d
KH
7783 if (TREE_CODE (op0) == NOP_EXPR
7784 || TREE_CODE (op0) == CONVERT_EXPR)
659d8efa 7785 {
4b58fc4d
KH
7786 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7787 tree inter_type = TREE_TYPE (op0);
659d8efa
KH
7788 int inside_int = INTEGRAL_TYPE_P (inside_type);
7789 int inside_ptr = POINTER_TYPE_P (inside_type);
7790 int inside_float = FLOAT_TYPE_P (inside_type);
4b8d544b 7791 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
659d8efa
KH
7792 unsigned int inside_prec = TYPE_PRECISION (inside_type);
7793 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7794 int inter_int = INTEGRAL_TYPE_P (inter_type);
7795 int inter_ptr = POINTER_TYPE_P (inter_type);
7796 int inter_float = FLOAT_TYPE_P (inter_type);
4b8d544b 7797 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
659d8efa
KH
7798 unsigned int inter_prec = TYPE_PRECISION (inter_type);
7799 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7800 int final_int = INTEGRAL_TYPE_P (type);
7801 int final_ptr = POINTER_TYPE_P (type);
7802 int final_float = FLOAT_TYPE_P (type);
4b8d544b 7803 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
659d8efa
KH
7804 unsigned int final_prec = TYPE_PRECISION (type);
7805 int final_unsignedp = TYPE_UNSIGNED (type);
7806
7807 /* In addition to the cases of two conversions in a row
7808 handled below, if we are converting something to its own
7809 type via an object of identical or wider precision, neither
7810 conversion is needed. */
7811 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
497cfe24
RG
7812 && (((inter_int || inter_ptr) && final_int)
7813 || (inter_float && final_float))
659d8efa 7814 && inter_prec >= final_prec)
7f20a5b7 7815 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
659d8efa
KH
7816
7817 /* Likewise, if the intermediate and final types are either both
7818 float or both integer, we don't need the middle conversion if
7819 it is wider than the final type and doesn't change the signedness
7820 (for integers). Avoid this if the final type is a pointer
7821 since then we sometimes need the inner conversion. Likewise if
7822 the outer has a precision not equal to the size of its mode. */
7823 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
4b8d544b
JJ
7824 || (inter_float && inside_float)
7825 || (inter_vec && inside_vec))
659d8efa 7826 && inter_prec >= inside_prec
4b8d544b
JJ
7827 && (inter_float || inter_vec
7828 || inter_unsignedp == inside_unsignedp)
659d8efa
KH
7829 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7830 && TYPE_MODE (type) == TYPE_MODE (inter_type))
4b8d544b
JJ
7831 && ! final_ptr
7832 && (! final_vec || inter_prec == inside_prec))
7f20a5b7 7833 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
659d8efa
KH
7834
7835 /* If we have a sign-extension of a zero-extended value, we can
7836 replace that by a single zero-extension. */
7837 if (inside_int && inter_int && final_int
7838 && inside_prec < inter_prec && inter_prec < final_prec
7839 && inside_unsignedp && !inter_unsignedp)
7f20a5b7 7840 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
659d8efa
KH
7841
7842 /* Two conversions in a row are not needed unless:
7843 - some conversion is floating-point (overstrict for now), or
4b8d544b 7844 - some conversion is a vector (overstrict for now), or
659d8efa
KH
7845 - the intermediate type is narrower than both initial and
7846 final, or
7847 - the intermediate type and innermost type differ in signedness,
7848 and the outermost type is wider than the intermediate, or
7849 - the initial type is a pointer type and the precisions of the
7850 intermediate and final types differ, or
7851 - the final type is a pointer type and the precisions of the
497cfe24
RG
7852 initial and intermediate types differ.
7853 - the final type is a pointer type and the initial type not
7854 - the initial type is a pointer to an array and the final type
7855 not. */
659d8efa 7856 if (! inside_float && ! inter_float && ! final_float
4b8d544b 7857 && ! inside_vec && ! inter_vec && ! final_vec
497cfe24 7858 && (inter_prec >= inside_prec || inter_prec >= final_prec)
659d8efa
KH
7859 && ! (inside_int && inter_int
7860 && inter_unsignedp != inside_unsignedp
7861 && inter_prec < final_prec)
7862 && ((inter_unsignedp && inter_prec > inside_prec)
7863 == (final_unsignedp && final_prec > inter_prec))
7864 && ! (inside_ptr && inter_prec != final_prec)
7865 && ! (final_ptr && inside_prec != inter_prec)
7866 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7867 && TYPE_MODE (type) == TYPE_MODE (inter_type))
497cfe24
RG
7868 && final_ptr == inside_ptr
7869 && ! (inside_ptr
7870 && TREE_CODE (TREE_TYPE (inside_type)) == ARRAY_TYPE
7871 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7f20a5b7 7872 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
659d8efa
KH
7873 }
7874
46c0a59d 7875 /* Handle (T *)&A.B.C for A being of type T and B and C
a4174ebf 7876 living at offset zero. This occurs frequently in
46c0a59d
RG
7877 C++ upcasting and then accessing the base. */
7878 if (TREE_CODE (op0) == ADDR_EXPR
7879 && POINTER_TYPE_P (type)
7880 && handled_component_p (TREE_OPERAND (op0, 0)))
7881 {
7882 HOST_WIDE_INT bitsize, bitpos;
7883 tree offset;
7884 enum machine_mode mode;
7885 int unsignedp, volatilep;
7886 tree base = TREE_OPERAND (op0, 0);
7887 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7888 &mode, &unsignedp, &volatilep, false);
7889 /* If the reference was to a (constant) zero offset, we can use
7890 the address of the base if it has the same base type
7891 as the result type. */
7892 if (! offset && bitpos == 0
7893 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
7894 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
70826cbb 7895 return fold_convert (type, fold_addr_expr (base));
46c0a59d
RG
7896 }
7897
07beea0d
AH
7898 if ((TREE_CODE (op0) == MODIFY_EXPR
7899 || TREE_CODE (op0) == GIMPLE_MODIFY_STMT)
7900 && TREE_CONSTANT (GENERIC_TREE_OPERAND (op0, 1))
659d8efa 7901 /* Detect assigning a bitfield. */
07beea0d
AH
7902 && !(TREE_CODE (GENERIC_TREE_OPERAND (op0, 0)) == COMPONENT_REF
7903 && DECL_BIT_FIELD
7904 (TREE_OPERAND (GENERIC_TREE_OPERAND (op0, 0), 1))))
659d8efa
KH
7905 {
7906 /* Don't leave an assignment inside a conversion
7907 unless assigning a bitfield. */
07beea0d 7908 tem = fold_build1 (code, type, GENERIC_TREE_OPERAND (op0, 1));
659d8efa 7909 /* First do the assignment, then return converted constant. */
6405f32f 7910 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
659d8efa
KH
7911 TREE_NO_WARNING (tem) = 1;
7912 TREE_USED (tem) = 1;
7913 return tem;
7914 }
7915
7916 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7917 constants (if x has signed type, the sign bit cannot be set
7918 in c). This folds extension into the BIT_AND_EXPR. */
7919 if (INTEGRAL_TYPE_P (type)
7920 && TREE_CODE (type) != BOOLEAN_TYPE
4b58fc4d
KH
7921 && TREE_CODE (op0) == BIT_AND_EXPR
7922 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
659d8efa 7923 {
4b58fc4d 7924 tree and = op0;
659d8efa
KH
7925 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
7926 int change = 0;
7927
7928 if (TYPE_UNSIGNED (TREE_TYPE (and))
7929 || (TYPE_PRECISION (type)
7930 <= TYPE_PRECISION (TREE_TYPE (and))))
7931 change = 1;
7932 else if (TYPE_PRECISION (TREE_TYPE (and1))
7933 <= HOST_BITS_PER_WIDE_INT
7934 && host_integerp (and1, 1))
7935 {
7936 unsigned HOST_WIDE_INT cst;
7937
7938 cst = tree_low_cst (and1, 1);
7939 cst &= (HOST_WIDE_INT) -1
7940 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7941 change = (cst == 0);
7942#ifdef LOAD_EXTEND_OP
7943 if (change
7944 && !flag_syntax_only
7945 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7946 == ZERO_EXTEND))
7947 {
ca5ba2a3 7948 tree uns = unsigned_type_for (TREE_TYPE (and0));
659d8efa
KH
7949 and0 = fold_convert (uns, and0);
7950 and1 = fold_convert (uns, and1);
7951 }
7952#endif
7953 }
7954 if (change)
7955 {
b8fca551
RG
7956 tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
7957 TREE_INT_CST_HIGH (and1), 0,
d95787e6 7958 TREE_OVERFLOW (and1));
7f20a5b7
KH
7959 return fold_build2 (BIT_AND_EXPR, type,
7960 fold_convert (type, and0), tem);
659d8efa
KH
7961 }
7962 }
7963
5be014d5 7964 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
ac5a28a6 7965 when one of the new casts will fold away. Conservatively we assume
5be014d5
AP
7966 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7967 if (POINTER_TYPE_P (type)
7968 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
ac5a28a6
JH
7969 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7970 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7971 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
659d8efa
KH
7972 {
7973 tree arg00 = TREE_OPERAND (arg0, 0);
ac5a28a6
JH
7974 tree arg01 = TREE_OPERAND (arg0, 1);
7975
7976 return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
5be014d5 7977 fold_convert (sizetype, arg01));
659d8efa
KH
7978 }
7979
e8206491 7980 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
110abdbc 7981 of the same precision, and X is an integer type not narrower than
e8206491
RS
7982 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7983 if (INTEGRAL_TYPE_P (type)
7984 && TREE_CODE (op0) == BIT_NOT_EXPR
7985 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7986 && (TREE_CODE (TREE_OPERAND (op0, 0)) == NOP_EXPR
7987 || TREE_CODE (TREE_OPERAND (op0, 0)) == CONVERT_EXPR)
7988 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7989 {
7990 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7991 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7992 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7993 return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
7994 }
7995
84ece8ef 7996 tem = fold_convert_const (code, type, op0);
62ab45cc 7997 return tem ? tem : NULL_TREE;
659d8efa
KH
7998
7999 case VIEW_CONVERT_EXPR:
f85242f0
RS
8000 if (TREE_TYPE (op0) == type)
8001 return op0;
4b58fc4d 8002 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
78bf6e2f
RS
8003 return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8004 return fold_view_convert_expr (type, op0);
659d8efa
KH
8005
8006 case NEGATE_EXPR:
1af8dcbf
RG
8007 tem = fold_negate_expr (arg0);
8008 if (tem)
8009 return fold_convert (type, tem);
62ab45cc 8010 return NULL_TREE;
659d8efa
KH
8011
8012 case ABS_EXPR:
8013 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8014 return fold_abs_const (arg0, type);
8015 else if (TREE_CODE (arg0) == NEGATE_EXPR)
7f20a5b7 8016 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
659d8efa
KH
8017 /* Convert fabs((double)float) into (double)fabsf(float). */
8018 else if (TREE_CODE (arg0) == NOP_EXPR
8019 && TREE_CODE (type) == REAL_TYPE)
8020 {
8021 tree targ0 = strip_float_extensions (arg0);
8022 if (targ0 != arg0)
7f20a5b7
KH
8023 return fold_convert (type, fold_build1 (ABS_EXPR,
8024 TREE_TYPE (targ0),
8025 targ0));
659d8efa 8026 }
1ade5842 8027 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
6ac01510
ILT
8028 else if (TREE_CODE (arg0) == ABS_EXPR)
8029 return arg0;
8030 else if (tree_expr_nonnegative_p (arg0))
659d8efa
KH
8031 return arg0;
8032
8033 /* Strip sign ops from argument. */
8034 if (TREE_CODE (type) == REAL_TYPE)
8035 {
8036 tem = fold_strip_sign_ops (arg0);
8037 if (tem)
7f20a5b7 8038 return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
659d8efa 8039 }
62ab45cc 8040 return NULL_TREE;
659d8efa
KH
8041
8042 case CONJ_EXPR:
8043 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8044 return fold_convert (type, arg0);
9734ebaf
RS
8045 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8046 {
8047 tree itype = TREE_TYPE (type);
8048 tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8049 tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8050 return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8051 }
8052 if (TREE_CODE (arg0) == COMPLEX_CST)
8053 {
8054 tree itype = TREE_TYPE (type);
8055 tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8056 tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8057 return build_complex (type, rpart, negate_expr (ipart));
8058 }
8059 if (TREE_CODE (arg0) == CONJ_EXPR)
8060 return fold_convert (type, TREE_OPERAND (arg0, 0));
62ab45cc 8061 return NULL_TREE;
659d8efa
KH
8062
8063 case BIT_NOT_EXPR:
8064 if (TREE_CODE (arg0) == INTEGER_CST)
8065 return fold_not_const (arg0, type);
8066 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8067 return TREE_OPERAND (arg0, 0);
8068 /* Convert ~ (-A) to A - 1. */
8069 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
7f20a5b7
KH
8070 return fold_build2 (MINUS_EXPR, type, TREE_OPERAND (arg0, 0),
8071 build_int_cst (type, 1));
659d8efa
KH
8072 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8073 else if (INTEGRAL_TYPE_P (type)
8074 && ((TREE_CODE (arg0) == MINUS_EXPR
8075 && integer_onep (TREE_OPERAND (arg0, 1)))
8076 || (TREE_CODE (arg0) == PLUS_EXPR
8077 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
7f20a5b7 8078 return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
f242e769
JM
8079 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8080 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8081 && (tem = fold_unary (BIT_NOT_EXPR, type,
8082 fold_convert (type,
8083 TREE_OPERAND (arg0, 0)))))
8084 return fold_build2 (BIT_XOR_EXPR, type, tem,
8085 fold_convert (type, TREE_OPERAND (arg0, 1)));
8086 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8087 && (tem = fold_unary (BIT_NOT_EXPR, type,
8088 fold_convert (type,
8089 TREE_OPERAND (arg0, 1)))))
8090 return fold_build2 (BIT_XOR_EXPR, type,
8091 fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8092
62ab45cc 8093 return NULL_TREE;
659d8efa
KH
8094
8095 case TRUTH_NOT_EXPR:
8096 /* The argument to invert_truthvalue must have Boolean type. */
8097 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8098 arg0 = fold_convert (boolean_type_node, arg0);
8099
8100 /* Note that the operand of this must be an int
8101 and its values must be 0 or 1.
8102 ("true" is a fixed value perhaps depending on the language,
8103 but we don't handle values other than 1 correctly yet.) */
d817ed3b
RG
8104 tem = fold_truth_not_expr (arg0);
8105 if (!tem)
62ab45cc 8106 return NULL_TREE;
659d8efa
KH
8107 return fold_convert (type, tem);
8108
8109 case REALPART_EXPR:
8110 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
9734ebaf
RS
8111 return fold_convert (type, arg0);
8112 if (TREE_CODE (arg0) == COMPLEX_EXPR)
659d8efa
KH
8113 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8114 TREE_OPERAND (arg0, 1));
9734ebaf
RS
8115 if (TREE_CODE (arg0) == COMPLEX_CST)
8116 return fold_convert (type, TREE_REALPART (arg0));
8117 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8118 {
8119 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8120 tem = fold_build2 (TREE_CODE (arg0), itype,
8121 fold_build1 (REALPART_EXPR, itype,
8122 TREE_OPERAND (arg0, 0)),
8123 fold_build1 (REALPART_EXPR, itype,
8124 TREE_OPERAND (arg0, 1)));
8125 return fold_convert (type, tem);
8126 }
8127 if (TREE_CODE (arg0) == CONJ_EXPR)
8128 {
8129 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8130 tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8131 return fold_convert (type, tem);
8132 }
85aef79f
RG
8133 if (TREE_CODE (arg0) == CALL_EXPR)
8134 {
8135 tree fn = get_callee_fndecl (arg0);
8136 if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8137 switch (DECL_FUNCTION_CODE (fn))
8138 {
8139 CASE_FLT_FN (BUILT_IN_CEXPI):
8140 fn = mathfn_built_in (type, BUILT_IN_COS);
2d38026b 8141 if (fn)
5039610b 8142 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
2d38026b 8143 break;
85aef79f 8144
2d38026b
RS
8145 default:
8146 break;
85aef79f
RG
8147 }
8148 }
62ab45cc 8149 return NULL_TREE;
659d8efa
KH
8150
8151 case IMAGPART_EXPR:
8152 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8153 return fold_convert (type, integer_zero_node);
9734ebaf 8154 if (TREE_CODE (arg0) == COMPLEX_EXPR)
659d8efa
KH
8155 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8156 TREE_OPERAND (arg0, 0));
9734ebaf
RS
8157 if (TREE_CODE (arg0) == COMPLEX_CST)
8158 return fold_convert (type, TREE_IMAGPART (arg0));
8159 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8160 {
8161 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8162 tem = fold_build2 (TREE_CODE (arg0), itype,
8163 fold_build1 (IMAGPART_EXPR, itype,
8164 TREE_OPERAND (arg0, 0)),
8165 fold_build1 (IMAGPART_EXPR, itype,
8166 TREE_OPERAND (arg0, 1)));
8167 return fold_convert (type, tem);
8168 }
8169 if (TREE_CODE (arg0) == CONJ_EXPR)
8170 {
8171 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8172 tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8173 return fold_convert (type, negate_expr (tem));
8174 }
85aef79f
RG
8175 if (TREE_CODE (arg0) == CALL_EXPR)
8176 {
8177 tree fn = get_callee_fndecl (arg0);
8178 if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8179 switch (DECL_FUNCTION_CODE (fn))
8180 {
8181 CASE_FLT_FN (BUILT_IN_CEXPI):
8182 fn = mathfn_built_in (type, BUILT_IN_SIN);
2d38026b 8183 if (fn)
5039610b 8184 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
2d38026b 8185 break;
85aef79f 8186
2d38026b
RS
8187 default:
8188 break;
85aef79f
RG
8189 }
8190 }
62ab45cc 8191 return NULL_TREE;
659d8efa
KH
8192
8193 default:
62ab45cc 8194 return NULL_TREE;
659d8efa
KH
8195 } /* switch (code) */
8196}
8197
292f30c5
EB
8198/* Fold a binary expression of code CODE and type TYPE with operands
8199 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8200 Return the folded expression if folding is successful. Otherwise,
8201 return NULL_TREE. */
8202
8203static tree
8204fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8205{
8206 enum tree_code compl_code;
8207
8208 if (code == MIN_EXPR)
8209 compl_code = MAX_EXPR;
8210 else if (code == MAX_EXPR)
8211 compl_code = MIN_EXPR;
8212 else
5f180d36 8213 gcc_unreachable ();
292f30c5 8214
f0dbdfbb 8215 /* MIN (MAX (a, b), b) == b. */
292f30c5
EB
8216 if (TREE_CODE (op0) == compl_code
8217 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8218 return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8219
f0dbdfbb 8220 /* MIN (MAX (b, a), b) == b. */
292f30c5
EB
8221 if (TREE_CODE (op0) == compl_code
8222 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8223 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8224 return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8225
f0dbdfbb 8226 /* MIN (a, MAX (a, b)) == a. */
292f30c5
EB
8227 if (TREE_CODE (op1) == compl_code
8228 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8229 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8230 return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8231
f0dbdfbb 8232 /* MIN (a, MAX (b, a)) == a. */
292f30c5
EB
8233 if (TREE_CODE (op1) == compl_code
8234 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8235 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8236 return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8237
8238 return NULL_TREE;
8239}
8240
e73dbcae
RG
8241/* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8242 by changing CODE to reduce the magnitude of constants involved in
8243 ARG0 of the comparison.
8244 Returns a canonicalized comparison tree if a simplification was
6ac01510
ILT
8245 possible, otherwise returns NULL_TREE.
8246 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8247 valid if signed overflow is undefined. */
e73dbcae
RG
8248
8249static tree
8250maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
6ac01510
ILT
8251 tree arg0, tree arg1,
8252 bool *strict_overflow_p)
e73dbcae
RG
8253{
8254 enum tree_code code0 = TREE_CODE (arg0);
8255 tree t, cst0 = NULL_TREE;
8256 int sgn0;
8257 bool swap = false;
8258
8259 /* Match A +- CST code arg1 and CST code arg1. */
8260 if (!(((code0 == MINUS_EXPR
8261 || code0 == PLUS_EXPR)
8262 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8263 || code0 == INTEGER_CST))
8264 return NULL_TREE;
8265
8266 /* Identify the constant in arg0 and its sign. */
8267 if (code0 == INTEGER_CST)
8268 cst0 = arg0;
8269 else
8270 cst0 = TREE_OPERAND (arg0, 1);
8271 sgn0 = tree_int_cst_sgn (cst0);
8272
8273 /* Overflowed constants and zero will cause problems. */
8274 if (integer_zerop (cst0)
8275 || TREE_OVERFLOW (cst0))
8276 return NULL_TREE;
8277
2f8e468b 8278 /* See if we can reduce the magnitude of the constant in
e73dbcae
RG
8279 arg0 by changing the comparison code. */
8280 if (code0 == INTEGER_CST)
8281 {
8282 /* CST <= arg1 -> CST-1 < arg1. */
8283 if (code == LE_EXPR && sgn0 == 1)
8284 code = LT_EXPR;
8285 /* -CST < arg1 -> -CST-1 <= arg1. */
8286 else if (code == LT_EXPR && sgn0 == -1)
8287 code = LE_EXPR;
8288 /* CST > arg1 -> CST-1 >= arg1. */
8289 else if (code == GT_EXPR && sgn0 == 1)
8290 code = GE_EXPR;
8291 /* -CST >= arg1 -> -CST-1 > arg1. */
8292 else if (code == GE_EXPR && sgn0 == -1)
8293 code = GT_EXPR;
8294 else
8295 return NULL_TREE;
8296 /* arg1 code' CST' might be more canonical. */
8297 swap = true;
8298 }
8299 else
8300 {
8301 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8302 if (code == LT_EXPR
8303 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8304 code = LE_EXPR;
8305 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8306 else if (code == GT_EXPR
8307 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8308 code = GE_EXPR;
8309 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8310 else if (code == LE_EXPR
8311 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8312 code = LT_EXPR;
8313 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8314 else if (code == GE_EXPR
8315 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8316 code = GT_EXPR;
8317 else
8318 return NULL_TREE;
6ac01510 8319 *strict_overflow_p = true;
e73dbcae
RG
8320 }
8321
8322 /* Now build the constant reduced in magnitude. */
8323 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8324 cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8325 if (code0 != INTEGER_CST)
8326 t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8327
8328 /* If swapping might yield to a more canonical form, do so. */
8329 if (swap)
8330 return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8331 else
8332 return fold_build2 (code, type, t, arg1);
8333}
8334
8335/* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8336 overflow further. Try to decrease the magnitude of constants involved
8337 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8338 and put sole constants at the second argument position.
8339 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8340
8341static tree
8342maybe_canonicalize_comparison (enum tree_code code, tree type,
8343 tree arg0, tree arg1)
8344{
8345 tree t;
6ac01510
ILT
8346 bool strict_overflow_p;
8347 const char * const warnmsg = G_("assuming signed overflow does not occur "
8348 "when reducing constant in comparison");
e73dbcae
RG
8349
8350 /* In principle pointers also have undefined overflow behavior,
8351 but that causes problems elsewhere. */
eeef0e45
ILT
8352 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8353 || POINTER_TYPE_P (TREE_TYPE (arg0)))
e73dbcae
RG
8354 return NULL_TREE;
8355
8356 /* Try canonicalization by simplifying arg0. */
6ac01510
ILT
8357 strict_overflow_p = false;
8358 t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8359 &strict_overflow_p);
e73dbcae 8360 if (t)
6ac01510
ILT
8361 {
8362 if (strict_overflow_p)
8363 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8364 return t;
8365 }
e73dbcae
RG
8366
8367 /* Try canonicalization by simplifying arg1 using the swapped
2f8e468b 8368 comparison. */
e73dbcae 8369 code = swap_tree_comparison (code);
6ac01510
ILT
8370 strict_overflow_p = false;
8371 t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8372 &strict_overflow_p);
8373 if (t && strict_overflow_p)
8374 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8375 return t;
e73dbcae
RG
8376}
8377
e26ec0bb
RS
8378/* Subroutine of fold_binary. This routine performs all of the
8379 transformations that are common to the equality/inequality
8380 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8381 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8382 fold_binary should call fold_binary. Fold a comparison with
8383 tree code CODE and type TYPE with operands OP0 and OP1. Return
8384 the folded comparison or NULL_TREE. */
8385
8386static tree
8387fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8388{
8389 tree arg0, arg1, tem;
8390
8391 arg0 = op0;
8392 arg1 = op1;
8393
8394 STRIP_SIGN_NOPS (arg0);
8395 STRIP_SIGN_NOPS (arg1);
8396
8397 tem = fold_relational_const (code, type, arg0, arg1);
8398 if (tem != NULL_TREE)
8399 return tem;
8400
8401 /* If one arg is a real or integer constant, put it last. */
8402 if (tree_swap_operands_p (arg0, arg1, true))
8403 return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8404
e26ec0bb
RS
8405 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
8406 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8407 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8408 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
eeef0e45 8409 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
e26ec0bb
RS
8410 && (TREE_CODE (arg1) == INTEGER_CST
8411 && !TREE_OVERFLOW (arg1)))
8412 {
8413 tree const1 = TREE_OPERAND (arg0, 1);
8414 tree const2 = arg1;
8415 tree variable = TREE_OPERAND (arg0, 0);
8416 tree lhs;
8417 int lhs_add;
8418 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8419
8420 lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8421 TREE_TYPE (arg1), const2, const1);
b44e7f07
ZD
8422
8423 /* If the constant operation overflowed this can be
8424 simplified as a comparison against INT_MAX/INT_MIN. */
8425 if (TREE_CODE (lhs) == INTEGER_CST
8426 && TREE_OVERFLOW (lhs))
8427 {
8428 int const1_sgn = tree_int_cst_sgn (const1);
8429 enum tree_code code2 = code;
8430
8431 /* Get the sign of the constant on the lhs if the
8432 operation were VARIABLE + CONST1. */
8433 if (TREE_CODE (arg0) == MINUS_EXPR)
8434 const1_sgn = -const1_sgn;
8435
8436 /* The sign of the constant determines if we overflowed
8437 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8438 Canonicalize to the INT_MIN overflow by swapping the comparison
8439 if necessary. */
8440 if (const1_sgn == -1)
8441 code2 = swap_tree_comparison (code);
8442
8443 /* We now can look at the canonicalized case
8444 VARIABLE + 1 CODE2 INT_MIN
8445 and decide on the result. */
8446 if (code2 == LT_EXPR
8447 || code2 == LE_EXPR
8448 || code2 == EQ_EXPR)
8449 return omit_one_operand (type, boolean_false_node, variable);
8450 else if (code2 == NE_EXPR
8451 || code2 == GE_EXPR
8452 || code2 == GT_EXPR)
8453 return omit_one_operand (type, boolean_true_node, variable);
8454 }
8455
e26ec0bb
RS
8456 if (TREE_CODE (lhs) == TREE_CODE (arg1)
8457 && (TREE_CODE (lhs) != INTEGER_CST
8458 || !TREE_OVERFLOW (lhs)))
6ac01510
ILT
8459 {
8460 fold_overflow_warning (("assuming signed overflow does not occur "
8461 "when changing X +- C1 cmp C2 to "
8462 "X cmp C1 +- C2"),
8463 WARN_STRICT_OVERFLOW_COMPARISON);
8464 return fold_build2 (code, type, variable, lhs);
8465 }
e26ec0bb
RS
8466 }
8467
e015f578
RG
8468 /* For comparisons of pointers we can decompose it to a compile time
8469 comparison of the base objects and the offsets into the object.
8470 This requires at least one operand being an ADDR_EXPR to do more
8471 than the operand_equal_p test below. */
8472 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8473 && (TREE_CODE (arg0) == ADDR_EXPR
8474 || TREE_CODE (arg1) == ADDR_EXPR))
8475 {
8476 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8477 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8478 enum machine_mode mode;
8479 int volatilep, unsignedp;
8480 bool indirect_base0 = false;
8481
8482 /* Get base and offset for the access. Strip ADDR_EXPR for
8483 get_inner_reference, but put it back by stripping INDIRECT_REF
8484 off the base object if possible. */
8485 base0 = arg0;
8486 if (TREE_CODE (arg0) == ADDR_EXPR)
8487 {
8488 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8489 &bitsize, &bitpos0, &offset0, &mode,
8490 &unsignedp, &volatilep, false);
8491 if (TREE_CODE (base0) == INDIRECT_REF)
8492 base0 = TREE_OPERAND (base0, 0);
8493 else
8494 indirect_base0 = true;
8495 }
8496
8497 base1 = arg1;
8498 if (TREE_CODE (arg1) == ADDR_EXPR)
8499 {
8500 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8501 &bitsize, &bitpos1, &offset1, &mode,
8502 &unsignedp, &volatilep, false);
8503 /* We have to make sure to have an indirect/non-indirect base1
8504 just the same as we did for base0. */
8505 if (TREE_CODE (base1) == INDIRECT_REF
8506 && !indirect_base0)
8507 base1 = TREE_OPERAND (base1, 0);
8508 else if (!indirect_base0)
8509 base1 = NULL_TREE;
8510 }
8511 else if (indirect_base0)
8512 base1 = NULL_TREE;
8513
8514 /* If we have equivalent bases we might be able to simplify. */
8515 if (base0 && base1
8516 && operand_equal_p (base0, base1, 0))
8517 {
8518 /* We can fold this expression to a constant if the non-constant
8519 offset parts are equal. */
8520 if (offset0 == offset1
8521 || (offset0 && offset1
8522 && operand_equal_p (offset0, offset1, 0)))
8523 {
8524 switch (code)
8525 {
8526 case EQ_EXPR:
8527 return build_int_cst (boolean_type_node, bitpos0 == bitpos1);
8528 case NE_EXPR:
8529 return build_int_cst (boolean_type_node, bitpos0 != bitpos1);
8530 case LT_EXPR:
8531 return build_int_cst (boolean_type_node, bitpos0 < bitpos1);
8532 case LE_EXPR:
8533 return build_int_cst (boolean_type_node, bitpos0 <= bitpos1);
8534 case GE_EXPR:
8535 return build_int_cst (boolean_type_node, bitpos0 >= bitpos1);
8536 case GT_EXPR:
8537 return build_int_cst (boolean_type_node, bitpos0 > bitpos1);
8538 default:;
8539 }
8540 }
8541 /* We can simplify the comparison to a comparison of the variable
8542 offset parts if the constant offset parts are equal.
8543 Be careful to use signed size type here because otherwise we
8544 mess with array offsets in the wrong way. This is possible
8545 because pointer arithmetic is restricted to retain within an
8546 object and overflow on pointer differences is undefined as of
8547 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8548 else if (bitpos0 == bitpos1)
8549 {
8550 tree signed_size_type_node;
8551 signed_size_type_node = signed_type_for (size_type_node);
8552
8553 /* By converting to signed size type we cover middle-end pointer
8554 arithmetic which operates on unsigned pointer types of size
8555 type size and ARRAY_REF offsets which are properly sign or
8556 zero extended from their type in case it is narrower than
8557 size type. */
8558 if (offset0 == NULL_TREE)
8559 offset0 = build_int_cst (signed_size_type_node, 0);
8560 else
8561 offset0 = fold_convert (signed_size_type_node, offset0);
8562 if (offset1 == NULL_TREE)
8563 offset1 = build_int_cst (signed_size_type_node, 0);
8564 else
8565 offset1 = fold_convert (signed_size_type_node, offset1);
8566
8567 return fold_build2 (code, type, offset0, offset1);
8568 }
8569 }
8570 }
8571
7ec434b8
RG
8572 /* If this is a comparison of two exprs that look like an ARRAY_REF of the
8573 same object, then we can fold this to a comparison of the two offsets in
8574 signed size type. This is possible because pointer arithmetic is
8575 restricted to retain within an object and overflow on pointer differences
eeef0e45
ILT
8576 is undefined as of 6.5.6/8 and /9 with respect to the signed ptrdiff_t.
8577
8578 We check flag_wrapv directly because pointers types are unsigned,
8579 and therefore TYPE_OVERFLOW_WRAPS returns true for them. That is
8580 normally what we want to avoid certain odd overflow cases, but
8581 not here. */
7ec434b8 8582 if (POINTER_TYPE_P (TREE_TYPE (arg0))
eeef0e45
ILT
8583 && !flag_wrapv
8584 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (arg0)))
7ec434b8
RG
8585 {
8586 tree base0, offset0, base1, offset1;
8587
8588 if (extract_array_ref (arg0, &base0, &offset0)
8589 && extract_array_ref (arg1, &base1, &offset1)
8590 && operand_equal_p (base0, base1, 0))
8591 {
8592 tree signed_size_type_node;
8593 signed_size_type_node = signed_type_for (size_type_node);
8594
8595 /* By converting to signed size type we cover middle-end pointer
8596 arithmetic which operates on unsigned pointer types of size
8597 type size and ARRAY_REF offsets which are properly sign or
8598 zero extended from their type in case it is narrower than
8599 size type. */
8600 if (offset0 == NULL_TREE)
8601 offset0 = build_int_cst (signed_size_type_node, 0);
8602 else
8603 offset0 = fold_convert (signed_size_type_node, offset0);
8604 if (offset1 == NULL_TREE)
8605 offset1 = build_int_cst (signed_size_type_node, 0);
8606 else
8607 offset1 = fold_convert (signed_size_type_node, offset1);
8608
8609 return fold_build2 (code, type, offset0, offset1);
8610 }
8611 }
8612
8a1eca08
RG
8613 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8614 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8615 the resulting offset is smaller in absolute value than the
8616 original one. */
eeef0e45 8617 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8a1eca08
RG
8618 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8619 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8620 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8621 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8622 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8623 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8624 {
8625 tree const1 = TREE_OPERAND (arg0, 1);
8626 tree const2 = TREE_OPERAND (arg1, 1);
8627 tree variable1 = TREE_OPERAND (arg0, 0);
8628 tree variable2 = TREE_OPERAND (arg1, 0);
8629 tree cst;
6ac01510
ILT
8630 const char * const warnmsg = G_("assuming signed overflow does not "
8631 "occur when combining constants around "
8632 "a comparison");
8a1eca08
RG
8633
8634 /* Put the constant on the side where it doesn't overflow and is
8635 of lower absolute value than before. */
8636 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8637 ? MINUS_EXPR : PLUS_EXPR,
8638 const2, const1, 0);
8639 if (!TREE_OVERFLOW (cst)
8640 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
6ac01510
ILT
8641 {
8642 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8643 return fold_build2 (code, type,
8644 variable1,
8645 fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
8646 variable2, cst));
8647 }
8a1eca08
RG
8648
8649 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8650 ? MINUS_EXPR : PLUS_EXPR,
8651 const1, const2, 0);
8652 if (!TREE_OVERFLOW (cst)
8653 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
6ac01510
ILT
8654 {
8655 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8656 return fold_build2 (code, type,
8657 fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
8658 variable1, cst),
8659 variable2);
8660 }
8a1eca08
RG
8661 }
8662
6b074ef6
RK
8663 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8664 signed arithmetic case. That form is created by the compiler
8665 often enough for folding it to be of value. One example is in
8666 computing loop trip counts after Operator Strength Reduction. */
eeef0e45 8667 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
6b074ef6
RK
8668 && TREE_CODE (arg0) == MULT_EXPR
8669 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8670 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8671 && integer_zerop (arg1))
8672 {
8673 tree const1 = TREE_OPERAND (arg0, 1);
8674 tree const2 = arg1; /* zero */
8675 tree variable1 = TREE_OPERAND (arg0, 0);
8676 enum tree_code cmp_code = code;
8677
8678 gcc_assert (!integer_zerop (const1));
8679
6ac01510
ILT
8680 fold_overflow_warning (("assuming signed overflow does not occur when "
8681 "eliminating multiplication in comparison "
8682 "with zero"),
8683 WARN_STRICT_OVERFLOW_COMPARISON);
8684
6b074ef6
RK
8685 /* If const1 is negative we swap the sense of the comparison. */
8686 if (tree_int_cst_sgn (const1) < 0)
8687 cmp_code = swap_tree_comparison (cmp_code);
8688
8689 return fold_build2 (cmp_code, type, variable1, const2);
8690 }
8691
fa139765 8692 tem = maybe_canonicalize_comparison (code, type, op0, op1);
e73dbcae
RG
8693 if (tem)
8694 return tem;
8695
e26ec0bb
RS
8696 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8697 {
8698 tree targ0 = strip_float_extensions (arg0);
8699 tree targ1 = strip_float_extensions (arg1);
8700 tree newtype = TREE_TYPE (targ0);
8701
8702 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8703 newtype = TREE_TYPE (targ1);
8704
8705 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
8706 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8707 return fold_build2 (code, type, fold_convert (newtype, targ0),
8708 fold_convert (newtype, targ1));
8709
8710 /* (-a) CMP (-b) -> b CMP a */
8711 if (TREE_CODE (arg0) == NEGATE_EXPR
8712 && TREE_CODE (arg1) == NEGATE_EXPR)
8713 return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
8714 TREE_OPERAND (arg0, 0));
8715
8716 if (TREE_CODE (arg1) == REAL_CST)
8717 {
8718 REAL_VALUE_TYPE cst;
8719 cst = TREE_REAL_CST (arg1);
8720
8721 /* (-a) CMP CST -> a swap(CMP) (-CST) */
8722 if (TREE_CODE (arg0) == NEGATE_EXPR)
8723 return fold_build2 (swap_tree_comparison (code), type,
8724 TREE_OPERAND (arg0, 0),
8725 build_real (TREE_TYPE (arg1),
8726 REAL_VALUE_NEGATE (cst)));
8727
8728 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
8729 /* a CMP (-0) -> a CMP 0 */
8730 if (REAL_VALUE_MINUS_ZERO (cst))
8731 return fold_build2 (code, type, arg0,
8732 build_real (TREE_TYPE (arg1), dconst0));
8733
8734 /* x != NaN is always true, other ops are always false. */
8735 if (REAL_VALUE_ISNAN (cst)
8736 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8737 {
8738 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8739 return omit_one_operand (type, tem, arg0);
8740 }
8741
8742 /* Fold comparisons against infinity. */
8743 if (REAL_VALUE_ISINF (cst))
8744 {
8745 tem = fold_inf_compare (code, type, arg0, arg1);
8746 if (tem != NULL_TREE)
8747 return tem;
8748 }
8749 }
8750
8751 /* If this is a comparison of a real constant with a PLUS_EXPR
8752 or a MINUS_EXPR of a real constant, we can convert it into a
8753 comparison with a revised real constant as long as no overflow
8754 occurs when unsafe_math_optimizations are enabled. */
8755 if (flag_unsafe_math_optimizations
8756 && TREE_CODE (arg1) == REAL_CST
8757 && (TREE_CODE (arg0) == PLUS_EXPR
8758 || TREE_CODE (arg0) == MINUS_EXPR)
8759 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
8760 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8761 ? MINUS_EXPR : PLUS_EXPR,
8762 arg1, TREE_OPERAND (arg0, 1), 0))
455f14dd 8763 && !TREE_OVERFLOW (tem))
e26ec0bb
RS
8764 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
8765
8766 /* Likewise, we can simplify a comparison of a real constant with
8767 a MINUS_EXPR whose first operand is also a real constant, i.e.
8768 (c1 - x) < c2 becomes x > c1-c2. */
8769 if (flag_unsafe_math_optimizations
8770 && TREE_CODE (arg1) == REAL_CST
8771 && TREE_CODE (arg0) == MINUS_EXPR
8772 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
8773 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
8774 arg1, 0))
455f14dd 8775 && !TREE_OVERFLOW (tem))
e26ec0bb
RS
8776 return fold_build2 (swap_tree_comparison (code), type,
8777 TREE_OPERAND (arg0, 1), tem);
8778
8779 /* Fold comparisons against built-in math functions. */
8780 if (TREE_CODE (arg1) == REAL_CST
8781 && flag_unsafe_math_optimizations
8782 && ! flag_errno_math)
8783 {
8784 enum built_in_function fcode = builtin_mathfn_code (arg0);
8785
8786 if (fcode != END_BUILTINS)
8787 {
8788 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
8789 if (tem != NULL_TREE)
8790 return tem;
8791 }
8792 }
8793 }
8794
e26ec0bb
RS
8795 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
8796 && (TREE_CODE (arg0) == NOP_EXPR
8797 || TREE_CODE (arg0) == CONVERT_EXPR))
8798 {
8799 /* If we are widening one operand of an integer comparison,
8800 see if the other operand is similarly being widened. Perhaps we
8801 can do the comparison in the narrower type. */
8802 tem = fold_widened_comparison (code, type, arg0, arg1);
8803 if (tem)
8804 return tem;
8805
8806 /* Or if we are changing signedness. */
8807 tem = fold_sign_changed_comparison (code, type, arg0, arg1);
8808 if (tem)
8809 return tem;
8810 }
8811
8812 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8813 constant, we can simplify it. */
8814 if (TREE_CODE (arg1) == INTEGER_CST
8815 && (TREE_CODE (arg0) == MIN_EXPR
8816 || TREE_CODE (arg0) == MAX_EXPR)
8817 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8818 {
8819 tem = optimize_minmax_comparison (code, type, op0, op1);
8820 if (tem)
8821 return tem;
8822 }
8823
8824 /* Simplify comparison of something with itself. (For IEEE
8825 floating-point, we can only do some of these simplifications.) */
8826 if (operand_equal_p (arg0, arg1, 0))
8827 {
8828 switch (code)
8829 {
8830 case EQ_EXPR:
8831 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8832 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8833 return constant_boolean_node (1, type);
8834 break;
8835
8836 case GE_EXPR:
8837 case LE_EXPR:
8838 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8839 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8840 return constant_boolean_node (1, type);
8841 return fold_build2 (EQ_EXPR, type, arg0, arg1);
8842
8843 case NE_EXPR:
8844 /* For NE, we can only do this simplification if integer
8845 or we don't honor IEEE floating point NaNs. */
8846 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
8847 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8848 break;
8849 /* ... fall through ... */
8850 case GT_EXPR:
8851 case LT_EXPR:
8852 return constant_boolean_node (0, type);
8853 default:
8854 gcc_unreachable ();
8855 }
8856 }
8857
8858 /* If we are comparing an expression that just has comparisons
8859 of two integer values, arithmetic expressions of those comparisons,
8860 and constants, we can simplify it. There are only three cases
8861 to check: the two values can either be equal, the first can be
8862 greater, or the second can be greater. Fold the expression for
8863 those three values. Since each value must be 0 or 1, we have
8864 eight possibilities, each of which corresponds to the constant 0
8865 or 1 or one of the six possible comparisons.
8866
8867 This handles common cases like (a > b) == 0 but also handles
8868 expressions like ((x > y) - (y > x)) > 0, which supposedly
8869 occur in macroized code. */
8870
8871 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8872 {
8873 tree cval1 = 0, cval2 = 0;
8874 int save_p = 0;
8875
8876 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8877 /* Don't handle degenerate cases here; they should already
8878 have been handled anyway. */
8879 && cval1 != 0 && cval2 != 0
8880 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8881 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8882 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8883 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8884 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8885 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8886 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8887 {
8888 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8889 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8890
8891 /* We can't just pass T to eval_subst in case cval1 or cval2
8892 was the same as ARG1. */
8893
8894 tree high_result
8895 = fold_build2 (code, type,
8896 eval_subst (arg0, cval1, maxval,
8897 cval2, minval),
8898 arg1);
8899 tree equal_result
8900 = fold_build2 (code, type,
8901 eval_subst (arg0, cval1, maxval,
8902 cval2, maxval),
8903 arg1);
8904 tree low_result
8905 = fold_build2 (code, type,
8906 eval_subst (arg0, cval1, minval,
8907 cval2, maxval),
8908 arg1);
8909
8910 /* All three of these results should be 0 or 1. Confirm they are.
8911 Then use those values to select the proper code to use. */
8912
8913 if (TREE_CODE (high_result) == INTEGER_CST
8914 && TREE_CODE (equal_result) == INTEGER_CST
8915 && TREE_CODE (low_result) == INTEGER_CST)
8916 {
8917 /* Make a 3-bit mask with the high-order bit being the
8918 value for `>', the next for '=', and the low for '<'. */
8919 switch ((integer_onep (high_result) * 4)
8920 + (integer_onep (equal_result) * 2)
8921 + integer_onep (low_result))
8922 {
8923 case 0:
8924 /* Always false. */
8925 return omit_one_operand (type, integer_zero_node, arg0);
8926 case 1:
8927 code = LT_EXPR;
8928 break;
8929 case 2:
8930 code = EQ_EXPR;
8931 break;
8932 case 3:
8933 code = LE_EXPR;
8934 break;
8935 case 4:
8936 code = GT_EXPR;
8937 break;
8938 case 5:
8939 code = NE_EXPR;
8940 break;
8941 case 6:
8942 code = GE_EXPR;
8943 break;
8944 case 7:
8945 /* Always true. */
8946 return omit_one_operand (type, integer_one_node, arg0);
8947 }
8948
8949 if (save_p)
8950 return save_expr (build2 (code, type, cval1, cval2));
8951 return fold_build2 (code, type, cval1, cval2);
8952 }
8953 }
8954 }
8955
8956 /* Fold a comparison of the address of COMPONENT_REFs with the same
8957 type and component to a comparison of the address of the base
8958 object. In short, &x->a OP &y->a to x OP y and
8959 &x->a OP &y.a to x OP &y */
8960 if (TREE_CODE (arg0) == ADDR_EXPR
8961 && TREE_CODE (TREE_OPERAND (arg0, 0)) == COMPONENT_REF
8962 && TREE_CODE (arg1) == ADDR_EXPR
8963 && TREE_CODE (TREE_OPERAND (arg1, 0)) == COMPONENT_REF)
8964 {
8965 tree cref0 = TREE_OPERAND (arg0, 0);
8966 tree cref1 = TREE_OPERAND (arg1, 0);
8967 if (TREE_OPERAND (cref0, 1) == TREE_OPERAND (cref1, 1))
8968 {
8969 tree op0 = TREE_OPERAND (cref0, 0);
8970 tree op1 = TREE_OPERAND (cref1, 0);
8971 return fold_build2 (code, type,
70826cbb
SP
8972 fold_addr_expr (op0),
8973 fold_addr_expr (op1));
e26ec0bb
RS
8974 }
8975 }
8976
8977 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8978 into a single range test. */
8979 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8980 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8981 && TREE_CODE (arg1) == INTEGER_CST
8982 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8983 && !integer_zerop (TREE_OPERAND (arg0, 1))
8984 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8985 && !TREE_OVERFLOW (arg1))
8986 {
8987 tem = fold_div_compare (code, type, arg0, arg1);
8988 if (tem != NULL_TREE)
8989 return tem;
8990 }
8991
c159ffe7
RS
8992 /* Fold ~X op ~Y as Y op X. */
8993 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8994 && TREE_CODE (arg1) == BIT_NOT_EXPR)
270d43bf
RS
8995 {
8996 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
8997 return fold_build2 (code, type,
8998 fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
8999 TREE_OPERAND (arg0, 0));
9000 }
c159ffe7
RS
9001
9002 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9003 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9004 && TREE_CODE (arg1) == INTEGER_CST)
270d43bf
RS
9005 {
9006 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9007 return fold_build2 (swap_tree_comparison (code), type,
9008 TREE_OPERAND (arg0, 0),
9009 fold_build1 (BIT_NOT_EXPR, cmp_type,
9010 fold_convert (cmp_type, arg1)));
9011 }
c159ffe7 9012
e26ec0bb
RS
9013 return NULL_TREE;
9014}
9015
99b25753
RS
9016
9017/* Subroutine of fold_binary. Optimize complex multiplications of the
9018 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9019 argument EXPR represents the expression "z" of type TYPE. */
9020
9021static tree
9022fold_mult_zconjz (tree type, tree expr)
9023{
9024 tree itype = TREE_TYPE (type);
9025 tree rpart, ipart, tem;
9026
9027 if (TREE_CODE (expr) == COMPLEX_EXPR)
9028 {
9029 rpart = TREE_OPERAND (expr, 0);
9030 ipart = TREE_OPERAND (expr, 1);
9031 }
9032 else if (TREE_CODE (expr) == COMPLEX_CST)
9033 {
9034 rpart = TREE_REALPART (expr);
9035 ipart = TREE_IMAGPART (expr);
9036 }
9037 else
9038 {
9039 expr = save_expr (expr);
9040 rpart = fold_build1 (REALPART_EXPR, itype, expr);
9041 ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9042 }
9043
9044 rpart = save_expr (rpart);
9045 ipart = save_expr (ipart);
9046 tem = fold_build2 (PLUS_EXPR, itype,
9047 fold_build2 (MULT_EXPR, itype, rpart, rpart),
9048 fold_build2 (MULT_EXPR, itype, ipart, ipart));
9049 return fold_build2 (COMPLEX_EXPR, type, tem,
9050 fold_convert (itype, integer_zero_node));
9051}
9052
9053
7107fa7c
KH
9054/* Fold a binary expression of code CODE and type TYPE with operands
9055 OP0 and OP1. Return the folded expression if folding is
9056 successful. Otherwise, return NULL_TREE. */
0aee4751 9057
721425b6 9058tree
fbaa905c 9059fold_binary (enum tree_code code, tree type, tree op0, tree op1)
0aee4751 9060{
0aee4751 9061 enum tree_code_class kind = TREE_CODE_CLASS (code);
e26ec0bb
RS
9062 tree arg0, arg1, tem;
9063 tree t1 = NULL_TREE;
6ac01510 9064 bool strict_overflow_p;
0aee4751 9065
07beea0d
AH
9066 gcc_assert ((IS_EXPR_CODE_CLASS (kind)
9067 || IS_GIMPLE_STMT_CODE_CLASS (kind))
fd6c76f4
RS
9068 && TREE_CODE_LENGTH (code) == 2
9069 && op0 != NULL_TREE
9070 && op1 != NULL_TREE);
0aee4751 9071
fbaa905c
KH
9072 arg0 = op0;
9073 arg1 = op1;
1eaea409 9074
fd6c76f4
RS
9075 /* Strip any conversions that don't change the mode. This is
9076 safe for every expression, except for a comparison expression
9077 because its signedness is derived from its operands. So, in
9078 the latter case, only strip conversions that don't change the
9079 signedness.
0aee4751 9080
fd6c76f4
RS
9081 Note that this is done as an internal manipulation within the
9082 constant folder, in order to find the simplest representation
9083 of the arguments so that their form can be studied. In any
9084 cases, the appropriate type conversions should be put back in
9085 the tree that will get out of the constant folder. */
0aee4751 9086
fd6c76f4
RS
9087 if (kind == tcc_comparison)
9088 {
9089 STRIP_SIGN_NOPS (arg0);
9090 STRIP_SIGN_NOPS (arg1);
1eaea409 9091 }
fd6c76f4 9092 else
1eaea409 9093 {
fd6c76f4
RS
9094 STRIP_NOPS (arg0);
9095 STRIP_NOPS (arg1);
9096 }
0aee4751 9097
fd6c76f4
RS
9098 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9099 constant but we can't do arithmetic on them. */
9100 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9101 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9102 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9103 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9104 {
9105 if (kind == tcc_binary)
9106 tem = const_binop (code, arg0, arg1, 0);
9107 else if (kind == tcc_comparison)
9108 tem = fold_relational_const (code, type, arg0, arg1);
1eaea409 9109 else
fd6c76f4 9110 tem = NULL_TREE;
1eaea409 9111
fd6c76f4
RS
9112 if (tem != NULL_TREE)
9113 {
9114 if (TREE_TYPE (tem) != type)
9115 tem = fold_convert (type, tem);
9116 return tem;
9117 }
0aee4751
KH
9118 }
9119
9120 /* If this is a commutative operation, and ARG0 is a constant, move it
9121 to ARG1 to reduce the number of tests below. */
9122 if (commutative_tree_code (code)
9123 && tree_swap_operands_p (arg0, arg1, true))
7f20a5b7 9124 return fold_build2 (code, type, op1, op0);
0aee4751 9125
fd6c76f4 9126 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
0aee4751
KH
9127
9128 First check for cases where an arithmetic operation is applied to a
9129 compound, conditional, or comparison operation. Push the arithmetic
9130 operation inside the compound or conditional to see if any folding
9131 can then be done. Convert comparison to conditional for this purpose.
9132 The also optimizes non-constant cases that used to be done in
9133 expand_expr.
9134
9135 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9136 one of the operands is a comparison and the other is a comparison, a
9137 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9138 code below would make the expression more complex. Change it to a
9139 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9140 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9141
9142 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9143 || code == EQ_EXPR || code == NE_EXPR)
9144 && ((truth_value_p (TREE_CODE (arg0))
9145 && (truth_value_p (TREE_CODE (arg1))
9146 || (TREE_CODE (arg1) == BIT_AND_EXPR
9147 && integer_onep (TREE_OPERAND (arg1, 1)))))
9148 || (truth_value_p (TREE_CODE (arg1))
9149 && (truth_value_p (TREE_CODE (arg0))
9150 || (TREE_CODE (arg0) == BIT_AND_EXPR
9151 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9152 {
7f20a5b7
KH
9153 tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9154 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9155 : TRUTH_XOR_EXPR,
9156 boolean_type_node,
9157 fold_convert (boolean_type_node, arg0),
9158 fold_convert (boolean_type_node, arg1));
0aee4751
KH
9159
9160 if (code == EQ_EXPR)
9161 tem = invert_truthvalue (tem);
9162
90ec750d 9163 return fold_convert (type, tem);
0aee4751
KH
9164 }
9165
4c17e288
RG
9166 if (TREE_CODE_CLASS (code) == tcc_binary
9167 || TREE_CODE_CLASS (code) == tcc_comparison)
0aee4751
KH
9168 {
9169 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9170 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
4c17e288
RG
9171 fold_build2 (code, type,
9172 TREE_OPERAND (arg0, 1), op1));
0aee4751
KH
9173 if (TREE_CODE (arg1) == COMPOUND_EXPR
9174 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9175 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
7f20a5b7 9176 fold_build2 (code, type,
4c17e288 9177 op0, TREE_OPERAND (arg1, 1)));
0aee4751
KH
9178
9179 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9180 {
e9da788c
KH
9181 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9182 arg0, arg1,
0aee4751
KH
9183 /*cond_first_p=*/1);
9184 if (tem != NULL_TREE)
9185 return tem;
9186 }
9187
9188 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9189 {
e9da788c
KH
9190 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9191 arg1, arg0,
0aee4751
KH
9192 /*cond_first_p=*/0);
9193 if (tem != NULL_TREE)
9194 return tem;
9195 }
9196 }
9197
9198 switch (code)
9199 {
5be014d5
AP
9200 case POINTER_PLUS_EXPR:
9201 /* 0 +p index -> (type)index */
9202 if (integer_zerop (arg0))
9203 return non_lvalue (fold_convert (type, arg1));
9204
9205 /* PTR +p 0 -> PTR */
9206 if (integer_zerop (arg1))
9207 return non_lvalue (fold_convert (type, arg0));
9208
9209 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9210 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9211 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9212 return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9213 fold_convert (sizetype, arg1),
9214 fold_convert (sizetype, arg0)));
9215
9216 /* index +p PTR -> PTR +p index */
9217 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9218 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9219 return fold_build2 (POINTER_PLUS_EXPR, type,
9220 fold_convert (type, arg1), fold_convert (sizetype, arg0));
9221
9222 /* (PTR +p B) +p A -> PTR +p (B + A) */
9223 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9224 {
9225 tree inner;
9226 tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9227 tree arg00 = TREE_OPERAND (arg0, 0);
9228 inner = fold_build2 (PLUS_EXPR, sizetype, arg01, fold_convert (sizetype, arg1));
9229 return fold_build2 (POINTER_PLUS_EXPR, type, arg00, inner);
9230 }
9231
9232 /* PTR_CST +p CST -> CST1 */
9233 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9234 return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9235
9236 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9237 of the array. Loop optimizer sometimes produce this type of
9238 expressions. */
9239 if (TREE_CODE (arg0) == ADDR_EXPR)
9240 {
9241 tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9242 if (tem)
9243 return fold_convert (type, tem);
9244 }
9245
9246 return NULL_TREE;
0aee4751 9247 case PLUS_EXPR:
5be014d5
AP
9248 /* PTR + INT -> (INT)(PTR p+ INT) */
9249 if (POINTER_TYPE_P (TREE_TYPE (arg0))
9250 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
9251 return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9252 TREE_TYPE (arg0),
9253 arg0,
9254 fold_convert (sizetype, arg1)));
9255 /* INT + PTR -> (INT)(PTR p+ INT) */
9256 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9257 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9258 return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9259 TREE_TYPE (arg1),
9260 arg1,
9261 fold_convert (sizetype, arg0)));
0aee4751
KH
9262 /* A + (-B) -> A - B */
9263 if (TREE_CODE (arg1) == NEGATE_EXPR)
10dcf221
KH
9264 return fold_build2 (MINUS_EXPR, type,
9265 fold_convert (type, arg0),
9266 fold_convert (type, TREE_OPERAND (arg1, 0)));
0aee4751
KH
9267 /* (-A) + B -> B - A */
9268 if (TREE_CODE (arg0) == NEGATE_EXPR
9269 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
10dcf221
KH
9270 return fold_build2 (MINUS_EXPR, type,
9271 fold_convert (type, arg1),
9272 fold_convert (type, TREE_OPERAND (arg0, 0)));
0ed9a3e3 9273
c22f6d33 9274 if (INTEGRAL_TYPE_P (type))
0aee4751 9275 {
c22f6d33
UB
9276 /* Convert ~A + 1 to -A. */
9277 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9278 && integer_onep (arg1))
9279 return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
0aee4751 9280
870aa1eb
RS
9281 /* ~X + X is -1. */
9282 if (TREE_CODE (arg0) == BIT_NOT_EXPR
eeef0e45 9283 && !TYPE_OVERFLOW_TRAPS (type))
870aa1eb 9284 {
a49c5793
SP
9285 tree tem = TREE_OPERAND (arg0, 0);
9286
9287 STRIP_NOPS (tem);
9288 if (operand_equal_p (tem, arg1, 0))
9289 {
9290 t1 = build_int_cst_type (type, -1);
9291 return omit_one_operand (type, t1, arg1);
9292 }
870aa1eb
RS
9293 }
9294
9295 /* X + ~X is -1. */
9296 if (TREE_CODE (arg1) == BIT_NOT_EXPR
eeef0e45 9297 && !TYPE_OVERFLOW_TRAPS (type))
870aa1eb 9298 {
a49c5793
SP
9299 tree tem = TREE_OPERAND (arg1, 0);
9300
9301 STRIP_NOPS (tem);
9302 if (operand_equal_p (arg0, tem, 0))
9303 {
9304 t1 = build_int_cst_type (type, -1);
9305 return omit_one_operand (type, t1, arg0);
9306 }
9307 }
c22f6d33
UB
9308 }
9309
9310 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9311 same or one. */
9312 if ((TREE_CODE (arg0) == MULT_EXPR
9313 || TREE_CODE (arg1) == MULT_EXPR)
9314 && (!FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
9315 {
9316 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9317 if (tem)
9318 return tem;
9319 }
9320
9321 if (! FLOAT_TYPE_P (type))
9322 {
9323 if (integer_zerop (arg1))
9324 return non_lvalue (fold_convert (type, arg0));
870aa1eb 9325
0aee4751
KH
9326 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9327 with a constant, and the two constants have no bits in common,
9328 we should treat this as a BIT_IOR_EXPR since this may produce more
9329 simplifications. */
9330 if (TREE_CODE (arg0) == BIT_AND_EXPR
9331 && TREE_CODE (arg1) == BIT_AND_EXPR
9332 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9333 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9334 && integer_zerop (const_binop (BIT_AND_EXPR,
9335 TREE_OPERAND (arg0, 1),
9336 TREE_OPERAND (arg1, 1), 0)))
9337 {
9338 code = BIT_IOR_EXPR;
9339 goto bit_ior;
9340 }
9341
9342 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9343 (plus (plus (mult) (mult)) (foo)) so that we can
9344 take advantage of the factoring cases below. */
9345 if (((TREE_CODE (arg0) == PLUS_EXPR
9346 || TREE_CODE (arg0) == MINUS_EXPR)
9347 && TREE_CODE (arg1) == MULT_EXPR)
9348 || ((TREE_CODE (arg1) == PLUS_EXPR
9349 || TREE_CODE (arg1) == MINUS_EXPR)
9350 && TREE_CODE (arg0) == MULT_EXPR))
9351 {
9352 tree parg0, parg1, parg, marg;
9353 enum tree_code pcode;
9354
9355 if (TREE_CODE (arg1) == MULT_EXPR)
9356 parg = arg0, marg = arg1;
9357 else
9358 parg = arg1, marg = arg0;
9359 pcode = TREE_CODE (parg);
9360 parg0 = TREE_OPERAND (parg, 0);
9361 parg1 = TREE_OPERAND (parg, 1);
9362 STRIP_NOPS (parg0);
9363 STRIP_NOPS (parg1);
9364
9365 if (TREE_CODE (parg0) == MULT_EXPR
9366 && TREE_CODE (parg1) != MULT_EXPR)
7f20a5b7
KH
9367 return fold_build2 (pcode, type,
9368 fold_build2 (PLUS_EXPR, type,
9369 fold_convert (type, parg0),
9370 fold_convert (type, marg)),
9371 fold_convert (type, parg1));
0aee4751
KH
9372 if (TREE_CODE (parg0) != MULT_EXPR
9373 && TREE_CODE (parg1) == MULT_EXPR)
7f20a5b7
KH
9374 return fold_build2 (PLUS_EXPR, type,
9375 fold_convert (type, parg0),
9376 fold_build2 (pcode, type,
9377 fold_convert (type, marg),
9378 fold_convert (type,
9379 parg1)));
0aee4751 9380 }
0aee4751
KH
9381 }
9382 else
9383 {
9384 /* See if ARG1 is zero and X + ARG1 reduces to X. */
9385 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9386 return non_lvalue (fold_convert (type, arg0));
9387
9388 /* Likewise if the operands are reversed. */
9389 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9390 return non_lvalue (fold_convert (type, arg1));
9391
9392 /* Convert X + -C into X - C. */
9393 if (TREE_CODE (arg1) == REAL_CST
9394 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9395 {
9396 tem = fold_negate_const (arg1, type);
9397 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
7f20a5b7
KH
9398 return fold_build2 (MINUS_EXPR, type,
9399 fold_convert (type, arg0),
9400 fold_convert (type, tem));
0aee4751
KH
9401 }
9402
9f539671
RG
9403 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9404 to __complex__ ( x, y ). This is not the same for SNaNs or
d1ad84c2 9405 if signed zeros are involved. */
9f539671
RG
9406 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9407 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9408 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9409 {
9410 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9411 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
9412 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
9413 bool arg0rz = false, arg0iz = false;
9414 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9415 || (arg0i && (arg0iz = real_zerop (arg0i))))
9416 {
9417 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
9418 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
9419 if (arg0rz && arg1i && real_zerop (arg1i))
9420 {
9421 tree rp = arg1r ? arg1r
9422 : build1 (REALPART_EXPR, rtype, arg1);
9423 tree ip = arg0i ? arg0i
9424 : build1 (IMAGPART_EXPR, rtype, arg0);
9425 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9426 }
9427 else if (arg0iz && arg1r && real_zerop (arg1r))
9428 {
9429 tree rp = arg0r ? arg0r
9430 : build1 (REALPART_EXPR, rtype, arg0);
9431 tree ip = arg1i ? arg1i
9432 : build1 (IMAGPART_EXPR, rtype, arg1);
9433 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9434 }
9435 }
9436 }
9437
f8912a55
PB
9438 if (flag_unsafe_math_optimizations
9439 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9440 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9441 && (tem = distribute_real_division (code, type, arg0, arg1)))
9442 return tem;
9443
0aee4751
KH
9444 /* Convert x+x into x*2.0. */
9445 if (operand_equal_p (arg0, arg1, 0)
9446 && SCALAR_FLOAT_TYPE_P (type))
7f20a5b7
KH
9447 return fold_build2 (MULT_EXPR, type, arg0,
9448 build_real (type, dconst2));
0aee4751 9449
0aee4751
KH
9450 /* Convert a + (b*c + d*e) into (a + b*c) + d*e. */
9451 if (flag_unsafe_math_optimizations
9452 && TREE_CODE (arg1) == PLUS_EXPR
9453 && TREE_CODE (arg0) != MULT_EXPR)
9454 {
9455 tree tree10 = TREE_OPERAND (arg1, 0);
9456 tree tree11 = TREE_OPERAND (arg1, 1);
9457 if (TREE_CODE (tree11) == MULT_EXPR
9458 && TREE_CODE (tree10) == MULT_EXPR)
9459 {
9460 tree tree0;
7f20a5b7
KH
9461 tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
9462 return fold_build2 (PLUS_EXPR, type, tree0, tree11);
0aee4751
KH
9463 }
9464 }
9465 /* Convert (b*c + d*e) + a into b*c + (d*e +a). */
9466 if (flag_unsafe_math_optimizations
9467 && TREE_CODE (arg0) == PLUS_EXPR
9468 && TREE_CODE (arg1) != MULT_EXPR)
9469 {
9470 tree tree00 = TREE_OPERAND (arg0, 0);
9471 tree tree01 = TREE_OPERAND (arg0, 1);
9472 if (TREE_CODE (tree01) == MULT_EXPR
9473 && TREE_CODE (tree00) == MULT_EXPR)
9474 {
9475 tree tree0;
7f20a5b7
KH
9476 tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
9477 return fold_build2 (PLUS_EXPR, type, tree00, tree0);
0aee4751
KH
9478 }
9479 }
9480 }
9481
9482 bit_rotate:
9483 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9484 is a rotate of A by C1 bits. */
9485 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9486 is a rotate of A by B bits. */
9487 {
9488 enum tree_code code0, code1;
9489 code0 = TREE_CODE (arg0);
9490 code1 = TREE_CODE (arg1);
9491 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9492 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9493 && operand_equal_p (TREE_OPERAND (arg0, 0),
9494 TREE_OPERAND (arg1, 0), 0)
9495 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
9496 {
9497 tree tree01, tree11;
9498 enum tree_code code01, code11;
9499
9500 tree01 = TREE_OPERAND (arg0, 1);
9501 tree11 = TREE_OPERAND (arg1, 1);
9502 STRIP_NOPS (tree01);
9503 STRIP_NOPS (tree11);
9504 code01 = TREE_CODE (tree01);
9505 code11 = TREE_CODE (tree11);
9506 if (code01 == INTEGER_CST
9507 && code11 == INTEGER_CST
9508 && TREE_INT_CST_HIGH (tree01) == 0
9509 && TREE_INT_CST_HIGH (tree11) == 0
9510 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9511 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9512 return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
9513 code0 == LSHIFT_EXPR ? tree01 : tree11);
9514 else if (code11 == MINUS_EXPR)
9515 {
9516 tree tree110, tree111;
9517 tree110 = TREE_OPERAND (tree11, 0);
9518 tree111 = TREE_OPERAND (tree11, 1);
9519 STRIP_NOPS (tree110);
9520 STRIP_NOPS (tree111);
9521 if (TREE_CODE (tree110) == INTEGER_CST
9522 && 0 == compare_tree_int (tree110,
9523 TYPE_PRECISION
9524 (TREE_TYPE (TREE_OPERAND
9525 (arg0, 0))))
9526 && operand_equal_p (tree01, tree111, 0))
9527 return build2 ((code0 == LSHIFT_EXPR
9528 ? LROTATE_EXPR
9529 : RROTATE_EXPR),
9530 type, TREE_OPERAND (arg0, 0), tree01);
9531 }
9532 else if (code01 == MINUS_EXPR)
9533 {
9534 tree tree010, tree011;
9535 tree010 = TREE_OPERAND (tree01, 0);
9536 tree011 = TREE_OPERAND (tree01, 1);
9537 STRIP_NOPS (tree010);
9538 STRIP_NOPS (tree011);
9539 if (TREE_CODE (tree010) == INTEGER_CST
9540 && 0 == compare_tree_int (tree010,
9541 TYPE_PRECISION
9542 (TREE_TYPE (TREE_OPERAND
9543 (arg0, 0))))
9544 && operand_equal_p (tree11, tree011, 0))
9545 return build2 ((code0 != LSHIFT_EXPR
9546 ? LROTATE_EXPR
9547 : RROTATE_EXPR),
9548 type, TREE_OPERAND (arg0, 0), tree11);
9549 }
9550 }
9551 }
9552
9553 associate:
9554 /* In most languages, can't associate operations on floats through
9555 parentheses. Rather than remember where the parentheses were, we
9556 don't associate floats at all, unless the user has specified
9557 -funsafe-math-optimizations. */
9558
fd6c76f4 9559 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
0aee4751
KH
9560 {
9561 tree var0, con0, lit0, minus_lit0;
9562 tree var1, con1, lit1, minus_lit1;
a6d5f37c 9563 bool ok = true;
0aee4751
KH
9564
9565 /* Split both trees into variables, constants, and literals. Then
9566 associate each group together, the constants with literals,
9567 then the result with variables. This increases the chances of
9568 literals being recombined later and of generating relocatable
9569 expressions for the sum of a constant and literal. */
9570 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9571 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9572 code == MINUS_EXPR);
9573
a6d5f37c
RG
9574 /* With undefined overflow we can only associate constants
9575 with one variable. */
9576 if ((POINTER_TYPE_P (type)
9577 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9578 && var0 && var1)
9579 {
9580 tree tmp0 = var0;
9581 tree tmp1 = var1;
9582
9583 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9584 tmp0 = TREE_OPERAND (tmp0, 0);
9585 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9586 tmp1 = TREE_OPERAND (tmp1, 0);
9587 /* The only case we can still associate with two variables
9588 is if they are the same, modulo negation. */
9589 if (!operand_equal_p (tmp0, tmp1, 0))
9590 ok = false;
9591 }
9592
0aee4751
KH
9593 /* Only do something if we found more than two objects. Otherwise,
9594 nothing has changed and we risk infinite recursion. */
a6d5f37c
RG
9595 if (ok
9596 && (2 < ((var0 != 0) + (var1 != 0)
9597 + (con0 != 0) + (con1 != 0)
9598 + (lit0 != 0) + (lit1 != 0)
9599 + (minus_lit0 != 0) + (minus_lit1 != 0))))
0aee4751
KH
9600 {
9601 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9602 if (code == MINUS_EXPR)
9603 code = PLUS_EXPR;
9604
9605 var0 = associate_trees (var0, var1, code, type);
9606 con0 = associate_trees (con0, con1, code, type);
9607 lit0 = associate_trees (lit0, lit1, code, type);
9608 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
9609
9610 /* Preserve the MINUS_EXPR if the negative part of the literal is
9611 greater than the positive part. Otherwise, the multiplicative
9612 folding code (i.e extract_muldiv) may be fooled in case
9613 unsigned constants are subtracted, like in the following
9614 example: ((X*2 + 4) - 8U)/2. */
9615 if (minus_lit0 && lit0)
9616 {
9617 if (TREE_CODE (lit0) == INTEGER_CST
9618 && TREE_CODE (minus_lit0) == INTEGER_CST
9619 && tree_int_cst_lt (lit0, minus_lit0))
9620 {
9621 minus_lit0 = associate_trees (minus_lit0, lit0,
9622 MINUS_EXPR, type);
9623 lit0 = 0;
9624 }
9625 else
9626 {
9627 lit0 = associate_trees (lit0, minus_lit0,
9628 MINUS_EXPR, type);
9629 minus_lit0 = 0;
9630 }
9631 }
9632 if (minus_lit0)
9633 {
9634 if (con0 == 0)
9635 return fold_convert (type,
9636 associate_trees (var0, minus_lit0,
9637 MINUS_EXPR, type));
9638 else
9639 {
9640 con0 = associate_trees (con0, minus_lit0,
9641 MINUS_EXPR, type);
9642 return fold_convert (type,
9643 associate_trees (var0, con0,
9644 PLUS_EXPR, type));
9645 }
9646 }
9647
9648 con0 = associate_trees (con0, lit0, code, type);
9649 return fold_convert (type, associate_trees (var0, con0,
9650 code, type));
9651 }
9652 }
9653
62ab45cc 9654 return NULL_TREE;
0aee4751
KH
9655
9656 case MINUS_EXPR:
5be014d5
AP
9657 /* Pointer simplifications for subtraction, simple reassociations. */
9658 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
9659 {
9660 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
9661 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
9662 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9663 {
9664 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9665 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
9666 tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
9667 tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
9668 return fold_build2 (PLUS_EXPR, type,
9669 fold_build2 (MINUS_EXPR, type, arg00, arg10),
9670 fold_build2 (MINUS_EXPR, type, arg01, arg11));
9671 }
9672 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
9673 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9674 {
9675 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9676 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
9677 tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
9678 if (tmp)
9679 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
9680 }
9681 }
0aee4751
KH
9682 /* A - (-B) -> A + B */
9683 if (TREE_CODE (arg1) == NEGATE_EXPR)
7f20a5b7 9684 return fold_build2 (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0));
0aee4751
KH
9685 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9686 if (TREE_CODE (arg0) == NEGATE_EXPR
9687 && (FLOAT_TYPE_P (type)
b0cd88d2 9688 || INTEGRAL_TYPE_P (type))
0aee4751
KH
9689 && negate_expr_p (arg1)
9690 && reorder_operands_p (arg0, arg1))
7f20a5b7
KH
9691 return fold_build2 (MINUS_EXPR, type, negate_expr (arg1),
9692 TREE_OPERAND (arg0, 0));
cbefb99c
JL
9693 /* Convert -A - 1 to ~A. */
9694 if (INTEGRAL_TYPE_P (type)
9695 && TREE_CODE (arg0) == NEGATE_EXPR
870aa1eb 9696 && integer_onep (arg1)
eeef0e45 9697 && !TYPE_OVERFLOW_TRAPS (type))
7f166b0a
RS
9698 return fold_build1 (BIT_NOT_EXPR, type,
9699 fold_convert (type, TREE_OPERAND (arg0, 0)));
cbefb99c
JL
9700
9701 /* Convert -1 - A to ~A. */
9702 if (INTEGRAL_TYPE_P (type)
9703 && integer_all_onesp (arg0))
72ff1a96 9704 return fold_build1 (BIT_NOT_EXPR, type, op1);
0aee4751 9705
0aee4751
KH
9706 if (! FLOAT_TYPE_P (type))
9707 {
fd6c76f4 9708 if (integer_zerop (arg0))
0aee4751
KH
9709 return negate_expr (fold_convert (type, arg1));
9710 if (integer_zerop (arg1))
9711 return non_lvalue (fold_convert (type, arg0));
9712
9713 /* Fold A - (A & B) into ~B & A. */
9714 if (!TREE_SIDE_EFFECTS (arg0)
9715 && TREE_CODE (arg1) == BIT_AND_EXPR)
9716 {
9717 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
7f20a5b7
KH
9718 return fold_build2 (BIT_AND_EXPR, type,
9719 fold_build1 (BIT_NOT_EXPR, type,
9720 TREE_OPERAND (arg1, 0)),
9721 arg0);
0aee4751 9722 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
7f20a5b7
KH
9723 return fold_build2 (BIT_AND_EXPR, type,
9724 fold_build1 (BIT_NOT_EXPR, type,
9725 TREE_OPERAND (arg1, 1)),
9726 arg0);
0aee4751
KH
9727 }
9728
9729 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
9730 any power of 2 minus 1. */
9731 if (TREE_CODE (arg0) == BIT_AND_EXPR
9732 && TREE_CODE (arg1) == BIT_AND_EXPR
9733 && operand_equal_p (TREE_OPERAND (arg0, 0),
9734 TREE_OPERAND (arg1, 0), 0))
9735 {
9736 tree mask0 = TREE_OPERAND (arg0, 1);
9737 tree mask1 = TREE_OPERAND (arg1, 1);
7f20a5b7 9738 tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
0aee4751
KH
9739
9740 if (operand_equal_p (tem, mask1, 0))
9741 {
7f20a5b7
KH
9742 tem = fold_build2 (BIT_XOR_EXPR, type,
9743 TREE_OPERAND (arg0, 0), mask1);
9744 return fold_build2 (MINUS_EXPR, type, tem, mask1);
0aee4751
KH
9745 }
9746 }
9747 }
9748
9749 /* See if ARG1 is zero and X - ARG1 reduces to X. */
9750 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
9751 return non_lvalue (fold_convert (type, arg0));
9752
9753 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
9754 ARG0 is zero and X + ARG0 reduces to X, since that would mean
9755 (-ARG1 + ARG0) reduces to -ARG1. */
fd6c76f4 9756 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
0aee4751
KH
9757 return negate_expr (fold_convert (type, arg1));
9758
d1ad84c2
KG
9759 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9760 __complex__ ( x, -y ). This is not the same for SNaNs or if
9761 signed zeros are involved. */
9762 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9763 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9764 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9765 {
9766 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9767 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
9768 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
9769 bool arg0rz = false, arg0iz = false;
9770 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9771 || (arg0i && (arg0iz = real_zerop (arg0i))))
9772 {
9773 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
9774 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
9775 if (arg0rz && arg1i && real_zerop (arg1i))
9776 {
9777 tree rp = fold_build1 (NEGATE_EXPR, rtype,
9778 arg1r ? arg1r
9779 : build1 (REALPART_EXPR, rtype, arg1));
9780 tree ip = arg0i ? arg0i
9781 : build1 (IMAGPART_EXPR, rtype, arg0);
9782 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9783 }
9784 else if (arg0iz && arg1r && real_zerop (arg1r))
9785 {
9786 tree rp = arg0r ? arg0r
9787 : build1 (REALPART_EXPR, rtype, arg0);
9788 tree ip = fold_build1 (NEGATE_EXPR, rtype,
9789 arg1i ? arg1i
9790 : build1 (IMAGPART_EXPR, rtype, arg1));
9791 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9792 }
9793 }
9794 }
9795
0aee4751
KH
9796 /* Fold &x - &x. This can happen from &x.foo - &x.
9797 This is unsafe for certain floats even in non-IEEE formats.
9798 In IEEE, it is unsafe because it does wrong for NaNs.
9799 Also note that operand_equal_p is always false if an operand
9800 is volatile. */
9801
52a39a4c
KG
9802 if ((! FLOAT_TYPE_P (type)
9803 || (flag_unsafe_math_optimizations
9804 && !HONOR_NANS (TYPE_MODE (type))
9805 && !HONOR_INFINITIES (TYPE_MODE (type))))
0aee4751
KH
9806 && operand_equal_p (arg0, arg1, 0))
9807 return fold_convert (type, integer_zero_node);
9808
9809 /* A - B -> A + (-B) if B is easily negatable. */
fd6c76f4 9810 if (negate_expr_p (arg1)
0aee4751
KH
9811 && ((FLOAT_TYPE_P (type)
9812 /* Avoid this transformation if B is a positive REAL_CST. */
9813 && (TREE_CODE (arg1) != REAL_CST
9814 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
b0cd88d2 9815 || INTEGRAL_TYPE_P (type)))
3bedcc89
RG
9816 return fold_build2 (PLUS_EXPR, type,
9817 fold_convert (type, arg0),
9818 fold_convert (type, negate_expr (arg1)));
0aee4751
KH
9819
9820 /* Try folding difference of addresses. */
9821 {
9822 HOST_WIDE_INT diff;
9823
9824 if ((TREE_CODE (arg0) == ADDR_EXPR
9825 || TREE_CODE (arg1) == ADDR_EXPR)
9826 && ptr_difference_const (arg0, arg1, &diff))
9827 return build_int_cst_type (type, diff);
9828 }
75cf42cc
RG
9829
9830 /* Fold &a[i] - &a[j] to i-j. */
9831 if (TREE_CODE (arg0) == ADDR_EXPR
9832 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9833 && TREE_CODE (arg1) == ADDR_EXPR
9834 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9835 {
9836 tree aref0 = TREE_OPERAND (arg0, 0);
9837 tree aref1 = TREE_OPERAND (arg1, 0);
9838 if (operand_equal_p (TREE_OPERAND (aref0, 0),
9839 TREE_OPERAND (aref1, 0), 0))
9840 {
9841 tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
9842 tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
9843 tree esz = array_ref_element_size (aref0);
9844 tree diff = build2 (MINUS_EXPR, type, op0, op1);
9845 return fold_build2 (MULT_EXPR, type, diff,
9846 fold_convert (type, esz));
9847
9848 }
9849 }
9850
f8912a55
PB
9851 if (flag_unsafe_math_optimizations
9852 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9853 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9854 && (tem = distribute_real_division (code, type, arg0, arg1)))
9855 return tem;
9856
0ed9a3e3
RG
9857 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
9858 same or one. */
9859 if ((TREE_CODE (arg0) == MULT_EXPR
9860 || TREE_CODE (arg1) == MULT_EXPR)
0aee4751 9861 && (!FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
0ed9a3e3
RG
9862 {
9863 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9864 if (tem)
9865 return tem;
0aee4751
KH
9866 }
9867
9868 goto associate;
9869
9870 case MULT_EXPR:
9871 /* (-A) * (-B) -> A * B */
9872 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
7f20a5b7 9873 return fold_build2 (MULT_EXPR, type,
30a843c3
RG
9874 fold_convert (type, TREE_OPERAND (arg0, 0)),
9875 fold_convert (type, negate_expr (arg1)));
0aee4751 9876 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
7f20a5b7 9877 return fold_build2 (MULT_EXPR, type,
30a843c3
RG
9878 fold_convert (type, negate_expr (arg0)),
9879 fold_convert (type, TREE_OPERAND (arg1, 0)));
0aee4751 9880
0aee4751
KH
9881 if (! FLOAT_TYPE_P (type))
9882 {
9883 if (integer_zerop (arg1))
9884 return omit_one_operand (type, arg1, arg0);
9885 if (integer_onep (arg1))
9886 return non_lvalue (fold_convert (type, arg0));
694d73e1
JM
9887 /* Transform x * -1 into -x. */
9888 if (integer_all_onesp (arg1))
9889 return fold_convert (type, negate_expr (arg0));
b0cd88d2
RG
9890 /* Transform x * -C into -x * C if x is easily negatable. */
9891 if (TREE_CODE (arg1) == INTEGER_CST
9892 && tree_int_cst_sgn (arg1) == -1
9893 && negate_expr_p (arg0)
9894 && (tem = negate_expr (arg1)) != arg1
9895 && !TREE_OVERFLOW (tem))
9896 return fold_build2 (MULT_EXPR, type,
9897 negate_expr (arg0), tem);
0aee4751
KH
9898
9899 /* (a * (1 << b)) is (a << b) */
9900 if (TREE_CODE (arg1) == LSHIFT_EXPR
9901 && integer_onep (TREE_OPERAND (arg1, 0)))
7f20a5b7
KH
9902 return fold_build2 (LSHIFT_EXPR, type, arg0,
9903 TREE_OPERAND (arg1, 1));
0aee4751
KH
9904 if (TREE_CODE (arg0) == LSHIFT_EXPR
9905 && integer_onep (TREE_OPERAND (arg0, 0)))
7f20a5b7
KH
9906 return fold_build2 (LSHIFT_EXPR, type, arg1,
9907 TREE_OPERAND (arg0, 1));
0aee4751 9908
6ac01510 9909 strict_overflow_p = false;
0aee4751 9910 if (TREE_CODE (arg1) == INTEGER_CST
71047303 9911 && 0 != (tem = extract_muldiv (op0,
0aee4751 9912 fold_convert (type, arg1),
6ac01510
ILT
9913 code, NULL_TREE,
9914 &strict_overflow_p)))
9915 {
9916 if (strict_overflow_p)
9917 fold_overflow_warning (("assuming signed overflow does not "
9918 "occur when simplifying "
9919 "multiplication"),
9920 WARN_STRICT_OVERFLOW_MISC);
9921 return fold_convert (type, tem);
9922 }
0aee4751 9923
99b25753
RS
9924 /* Optimize z * conj(z) for integer complex numbers. */
9925 if (TREE_CODE (arg0) == CONJ_EXPR
9926 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9927 return fold_mult_zconjz (type, arg1);
9928 if (TREE_CODE (arg1) == CONJ_EXPR
9929 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9930 return fold_mult_zconjz (type, arg0);
0aee4751
KH
9931 }
9932 else
9933 {
9934 /* Maybe fold x * 0 to 0. The expressions aren't the same
9935 when x is NaN, since x * 0 is also NaN. Nor are they the
9936 same in modes with signed zeros, since multiplying a
9937 negative value by 0 gives -0, not +0. */
9938 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
9939 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9940 && real_zerop (arg1))
9941 return omit_one_operand (type, arg1, arg0);
9942 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
9943 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9944 && real_onep (arg1))
9945 return non_lvalue (fold_convert (type, arg0));
9946
9947 /* Transform x * -1.0 into -x. */
9948 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9949 && real_minus_onep (arg1))
9950 return fold_convert (type, negate_expr (arg0));
9951
9952 /* Convert (C1/X)*C2 into (C1*C2)/X. */
9953 if (flag_unsafe_math_optimizations
9954 && TREE_CODE (arg0) == RDIV_EXPR
9955 && TREE_CODE (arg1) == REAL_CST
9956 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
9957 {
9958 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
9959 arg1, 0);
9960 if (tem)
7f20a5b7
KH
9961 return fold_build2 (RDIV_EXPR, type, tem,
9962 TREE_OPERAND (arg0, 1));
0aee4751
KH
9963 }
9964
9965 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
9966 if (operand_equal_p (arg0, arg1, 0))
9967 {
9968 tree tem = fold_strip_sign_ops (arg0);
9969 if (tem != NULL_TREE)
9970 {
9971 tem = fold_convert (type, tem);
7f20a5b7 9972 return fold_build2 (MULT_EXPR, type, tem, tem);
0aee4751
KH
9973 }
9974 }
9975
9f539671 9976 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
d1ad84c2 9977 This is not the same for NaNs or if signed zeros are
9f539671
RG
9978 involved. */
9979 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
9980 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9981 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9982 && TREE_CODE (arg1) == COMPLEX_CST
9983 && real_zerop (TREE_REALPART (arg1)))
9984 {
9985 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9986 if (real_onep (TREE_IMAGPART (arg1)))
9987 return fold_build2 (COMPLEX_EXPR, type,
9988 negate_expr (fold_build1 (IMAGPART_EXPR,
9989 rtype, arg0)),
9990 fold_build1 (REALPART_EXPR, rtype, arg0));
9991 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9992 return fold_build2 (COMPLEX_EXPR, type,
9993 fold_build1 (IMAGPART_EXPR, rtype, arg0),
9994 negate_expr (fold_build1 (REALPART_EXPR,
9995 rtype, arg0)));
9996 }
9997
99b25753
RS
9998 /* Optimize z * conj(z) for floating point complex numbers.
9999 Guarded by flag_unsafe_math_optimizations as non-finite
10000 imaginary components don't produce scalar results. */
10001 if (flag_unsafe_math_optimizations
10002 && TREE_CODE (arg0) == CONJ_EXPR
10003 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10004 return fold_mult_zconjz (type, arg1);
10005 if (flag_unsafe_math_optimizations
10006 && TREE_CODE (arg1) == CONJ_EXPR
10007 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10008 return fold_mult_zconjz (type, arg0);
10009
0aee4751
KH
10010 if (flag_unsafe_math_optimizations)
10011 {
10012 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10013 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10014
10015 /* Optimizations of root(...)*root(...). */
10016 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10017 {
5039610b
SL
10018 tree rootfn, arg;
10019 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10020 tree arg10 = CALL_EXPR_ARG (arg1, 0);
0aee4751
KH
10021
10022 /* Optimize sqrt(x)*sqrt(x) as x. */
10023 if (BUILTIN_SQRT_P (fcode0)
10024 && operand_equal_p (arg00, arg10, 0)
10025 && ! HONOR_SNANS (TYPE_MODE (type)))
10026 return arg00;
10027
10028 /* Optimize root(x)*root(y) as root(x*y). */
5039610b 10029 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
7f20a5b7 10030 arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
5039610b 10031 return build_call_expr (rootfn, 1, arg);
0aee4751
KH
10032 }
10033
10034 /* Optimize expN(x)*expN(y) as expN(x+y). */
10035 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10036 {
5039610b 10037 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
6405f32f 10038 tree arg = fold_build2 (PLUS_EXPR, type,
5039610b
SL
10039 CALL_EXPR_ARG (arg0, 0),
10040 CALL_EXPR_ARG (arg1, 0));
10041 return build_call_expr (expfn, 1, arg);
0aee4751
KH
10042 }
10043
10044 /* Optimizations of pow(...)*pow(...). */
10045 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10046 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10047 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10048 {
5039610b
SL
10049 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10050 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10051 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10052 tree arg11 = CALL_EXPR_ARG (arg1, 1);
0aee4751
KH
10053
10054 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10055 if (operand_equal_p (arg01, arg11, 0))
10056 {
5039610b 10057 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
6405f32f 10058 tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
5039610b 10059 return build_call_expr (powfn, 2, arg, arg01);
0aee4751
KH
10060 }
10061
10062 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10063 if (operand_equal_p (arg00, arg10, 0))
10064 {
5039610b 10065 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
7f20a5b7 10066 tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
5039610b 10067 return build_call_expr (powfn, 2, arg00, arg);
0aee4751
KH
10068 }
10069 }
10070
10071 /* Optimize tan(x)*cos(x) as sin(x). */
10072 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10073 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10074 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10075 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10076 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10077 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
5039610b
SL
10078 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10079 CALL_EXPR_ARG (arg1, 0), 0))
0aee4751
KH
10080 {
10081 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10082
10083 if (sinfn != NULL_TREE)
5039610b 10084 return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
0aee4751
KH
10085 }
10086
10087 /* Optimize x*pow(x,c) as pow(x,c+1). */
10088 if (fcode1 == BUILT_IN_POW
10089 || fcode1 == BUILT_IN_POWF
10090 || fcode1 == BUILT_IN_POWL)
10091 {
5039610b
SL
10092 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10093 tree arg11 = CALL_EXPR_ARG (arg1, 1);
0aee4751 10094 if (TREE_CODE (arg11) == REAL_CST
455f14dd 10095 && !TREE_OVERFLOW (arg11)
0aee4751
KH
10096 && operand_equal_p (arg0, arg10, 0))
10097 {
5039610b 10098 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
0aee4751 10099 REAL_VALUE_TYPE c;
5039610b 10100 tree arg;
0aee4751
KH
10101
10102 c = TREE_REAL_CST (arg11);
10103 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10104 arg = build_real (type, c);
5039610b 10105 return build_call_expr (powfn, 2, arg0, arg);
0aee4751
KH
10106 }
10107 }
10108
10109 /* Optimize pow(x,c)*x as pow(x,c+1). */
10110 if (fcode0 == BUILT_IN_POW
10111 || fcode0 == BUILT_IN_POWF
10112 || fcode0 == BUILT_IN_POWL)
10113 {
5039610b
SL
10114 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10115 tree arg01 = CALL_EXPR_ARG (arg0, 1);
0aee4751 10116 if (TREE_CODE (arg01) == REAL_CST
455f14dd 10117 && !TREE_OVERFLOW (arg01)
0aee4751
KH
10118 && operand_equal_p (arg1, arg00, 0))
10119 {
5039610b 10120 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
0aee4751 10121 REAL_VALUE_TYPE c;
5039610b 10122 tree arg;
0aee4751
KH
10123
10124 c = TREE_REAL_CST (arg01);
10125 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10126 arg = build_real (type, c);
5039610b 10127 return build_call_expr (powfn, 2, arg1, arg);
0aee4751
KH
10128 }
10129 }
10130
10131 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
10132 if (! optimize_size
10133 && operand_equal_p (arg0, arg1, 0))
10134 {
10135 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10136
10137 if (powfn)
10138 {
10139 tree arg = build_real (type, dconst2);
5039610b 10140 return build_call_expr (powfn, 2, arg0, arg);
0aee4751
KH
10141 }
10142 }
10143 }
10144 }
10145 goto associate;
10146
10147 case BIT_IOR_EXPR:
10148 bit_ior:
10149 if (integer_all_onesp (arg1))
10150 return omit_one_operand (type, arg1, arg0);
10151 if (integer_zerop (arg1))
10152 return non_lvalue (fold_convert (type, arg0));
10153 if (operand_equal_p (arg0, arg1, 0))
10154 return non_lvalue (fold_convert (type, arg0));
10155
10156 /* ~X | X is -1. */
10157 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10158 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10159 {
2ac7cbb5 10160 t1 = build_int_cst_type (type, -1);
0aee4751
KH
10161 return omit_one_operand (type, t1, arg1);
10162 }
10163
10164 /* X | ~X is -1. */
10165 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10166 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10167 {
2ac7cbb5 10168 t1 = build_int_cst_type (type, -1);
0aee4751
KH
10169 return omit_one_operand (type, t1, arg0);
10170 }
10171
840992bd
RS
10172 /* Canonicalize (X & C1) | C2. */
10173 if (TREE_CODE (arg0) == BIT_AND_EXPR
10174 && TREE_CODE (arg1) == INTEGER_CST
10175 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10176 {
10177 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, mlo, mhi;
10178 int width = TYPE_PRECISION (type);
10179 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10180 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10181 hi2 = TREE_INT_CST_HIGH (arg1);
10182 lo2 = TREE_INT_CST_LOW (arg1);
10183
10184 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10185 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10186 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10187
10188 if (width > HOST_BITS_PER_WIDE_INT)
10189 {
10190 mhi = (unsigned HOST_WIDE_INT) -1
10191 >> (2 * HOST_BITS_PER_WIDE_INT - width);
10192 mlo = -1;
10193 }
10194 else
10195 {
10196 mhi = 0;
10197 mlo = (unsigned HOST_WIDE_INT) -1
10198 >> (HOST_BITS_PER_WIDE_INT - width);
10199 }
10200
10201 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10202 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10203 return fold_build2 (BIT_IOR_EXPR, type,
10204 TREE_OPERAND (arg0, 0), arg1);
10205
10206 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2. */
10207 hi1 &= mhi;
10208 lo1 &= mlo;
10209 if ((hi1 & ~hi2) != hi1 || (lo1 & ~lo2) != lo1)
10210 return fold_build2 (BIT_IOR_EXPR, type,
10211 fold_build2 (BIT_AND_EXPR, type,
10212 TREE_OPERAND (arg0, 0),
10213 build_int_cst_wide (type,
10214 lo1 & ~lo2,
10215 hi1 & ~hi2)),
10216 arg1);
10217 }
10218
03bebcac
RS
10219 /* (X & Y) | Y is (X, Y). */
10220 if (TREE_CODE (arg0) == BIT_AND_EXPR
10221 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10222 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10223 /* (X & Y) | X is (Y, X). */
10224 if (TREE_CODE (arg0) == BIT_AND_EXPR
10225 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10226 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10227 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10228 /* X | (X & Y) is (Y, X). */
10229 if (TREE_CODE (arg1) == BIT_AND_EXPR
10230 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10231 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10232 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10233 /* X | (Y & X) is (Y, X). */
10234 if (TREE_CODE (arg1) == BIT_AND_EXPR
10235 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10236 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10237 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10238
0aee4751
KH
10239 t1 = distribute_bit_expr (code, type, arg0, arg1);
10240 if (t1 != NULL_TREE)
10241 return t1;
10242
10243 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10244
10245 This results in more efficient code for machines without a NAND
10246 instruction. Combine will canonicalize to the first form
10247 which will allow use of NAND instructions provided by the
10248 backend if they exist. */
10249 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10250 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10251 {
7f20a5b7
KH
10252 return fold_build1 (BIT_NOT_EXPR, type,
10253 build2 (BIT_AND_EXPR, type,
10254 TREE_OPERAND (arg0, 0),
10255 TREE_OPERAND (arg1, 0)));
0aee4751
KH
10256 }
10257
10258 /* See if this can be simplified into a rotate first. If that
10259 is unsuccessful continue in the association code. */
10260 goto bit_rotate;
10261
10262 case BIT_XOR_EXPR:
10263 if (integer_zerop (arg1))
10264 return non_lvalue (fold_convert (type, arg0));
10265 if (integer_all_onesp (arg1))
7f20a5b7 10266 return fold_build1 (BIT_NOT_EXPR, type, arg0);
0aee4751
KH
10267 if (operand_equal_p (arg0, arg1, 0))
10268 return omit_one_operand (type, integer_zero_node, arg0);
10269
10270 /* ~X ^ X is -1. */
10271 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10272 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10273 {
2ac7cbb5 10274 t1 = build_int_cst_type (type, -1);
0aee4751
KH
10275 return omit_one_operand (type, t1, arg1);
10276 }
10277
10278 /* X ^ ~X is -1. */
10279 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10280 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10281 {
2ac7cbb5 10282 t1 = build_int_cst_type (type, -1);
0aee4751
KH
10283 return omit_one_operand (type, t1, arg0);
10284 }
10285
10286 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10287 with a constant, and the two constants have no bits in common,
10288 we should treat this as a BIT_IOR_EXPR since this may produce more
10289 simplifications. */
10290 if (TREE_CODE (arg0) == BIT_AND_EXPR
10291 && TREE_CODE (arg1) == BIT_AND_EXPR
10292 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10293 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10294 && integer_zerop (const_binop (BIT_AND_EXPR,
10295 TREE_OPERAND (arg0, 1),
10296 TREE_OPERAND (arg1, 1), 0)))
10297 {
10298 code = BIT_IOR_EXPR;
10299 goto bit_ior;
10300 }
10301
9d24eb54
AP
10302 /* (X | Y) ^ X -> Y & ~ X*/
10303 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10304 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10305 {
10306 tree t2 = TREE_OPERAND (arg0, 1);
10307 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10308 arg1);
10309 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10310 fold_convert (type, t1));
10311 return t1;
10312 }
10313
10314 /* (Y | X) ^ X -> Y & ~ X*/
10315 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10316 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10317 {
10318 tree t2 = TREE_OPERAND (arg0, 0);
10319 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10320 arg1);
10321 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10322 fold_convert (type, t1));
10323 return t1;
10324 }
10325
10326 /* X ^ (X | Y) -> Y & ~ X*/
10327 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10328 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10329 {
10330 tree t2 = TREE_OPERAND (arg1, 1);
10331 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10332 arg0);
10333 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10334 fold_convert (type, t1));
10335 return t1;
10336 }
10337
10338 /* X ^ (Y | X) -> Y & ~ X*/
10339 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10340 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10341 {
10342 tree t2 = TREE_OPERAND (arg1, 0);
10343 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10344 arg0);
10345 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10346 fold_convert (type, t1));
10347 return t1;
10348 }
10349
33ab6245
JM
10350 /* Convert ~X ^ ~Y to X ^ Y. */
10351 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10352 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10353 return fold_build2 (code, type,
10354 fold_convert (type, TREE_OPERAND (arg0, 0)),
10355 fold_convert (type, TREE_OPERAND (arg1, 0)));
10356
f8ed9a1c
RS
10357 /* Convert ~X ^ C to X ^ ~C. */
10358 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10359 && TREE_CODE (arg1) == INTEGER_CST)
10360 return fold_build2 (code, type,
10361 fold_convert (type, TREE_OPERAND (arg0, 0)),
10362 fold_build1 (BIT_NOT_EXPR, type, arg1));
10363
cef65eaa
RS
10364 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10365 if (TREE_CODE (arg0) == BIT_AND_EXPR
10366 && integer_onep (TREE_OPERAND (arg0, 1))
10367 && integer_onep (arg1))
10368 return fold_build2 (EQ_EXPR, type, arg0,
10369 build_int_cst (TREE_TYPE (arg0), 0));
10370
dd2c62dc
RS
10371 /* Fold (X & Y) ^ Y as ~X & Y. */
10372 if (TREE_CODE (arg0) == BIT_AND_EXPR
10373 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10374 {
10375 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10376 return fold_build2 (BIT_AND_EXPR, type,
10377 fold_build1 (BIT_NOT_EXPR, type, tem),
10378 fold_convert (type, arg1));
10379 }
10380 /* Fold (X & Y) ^ X as ~Y & X. */
10381 if (TREE_CODE (arg0) == BIT_AND_EXPR
10382 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10383 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10384 {
10385 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10386 return fold_build2 (BIT_AND_EXPR, type,
10387 fold_build1 (BIT_NOT_EXPR, type, tem),
10388 fold_convert (type, arg1));
10389 }
10390 /* Fold X ^ (X & Y) as X & ~Y. */
10391 if (TREE_CODE (arg1) == BIT_AND_EXPR
10392 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10393 {
10394 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10395 return fold_build2 (BIT_AND_EXPR, type,
10396 fold_convert (type, arg0),
10397 fold_build1 (BIT_NOT_EXPR, type, tem));
10398 }
10399 /* Fold X ^ (Y & X) as ~Y & X. */
10400 if (TREE_CODE (arg1) == BIT_AND_EXPR
10401 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10402 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10403 {
10404 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10405 return fold_build2 (BIT_AND_EXPR, type,
10406 fold_build1 (BIT_NOT_EXPR, type, tem),
10407 fold_convert (type, arg0));
10408 }
10409
0aee4751
KH
10410 /* See if this can be simplified into a rotate first. If that
10411 is unsuccessful continue in the association code. */
10412 goto bit_rotate;
10413
10414 case BIT_AND_EXPR:
10415 if (integer_all_onesp (arg1))
10416 return non_lvalue (fold_convert (type, arg0));
10417 if (integer_zerop (arg1))
10418 return omit_one_operand (type, arg1, arg0);
10419 if (operand_equal_p (arg0, arg1, 0))
10420 return non_lvalue (fold_convert (type, arg0));
10421
10422 /* ~X & X is always zero. */
10423 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10424 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10425 return omit_one_operand (type, integer_zero_node, arg1);
10426
10427 /* X & ~X is always zero. */
10428 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10429 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10430 return omit_one_operand (type, integer_zero_node, arg0);
10431
840992bd
RS
10432 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
10433 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10434 && TREE_CODE (arg1) == INTEGER_CST
10435 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10436 return fold_build2 (BIT_IOR_EXPR, type,
10437 fold_build2 (BIT_AND_EXPR, type,
10438 TREE_OPERAND (arg0, 0), arg1),
10439 fold_build2 (BIT_AND_EXPR, type,
10440 TREE_OPERAND (arg0, 1), arg1));
10441
03bebcac
RS
10442 /* (X | Y) & Y is (X, Y). */
10443 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10444 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10445 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10446 /* (X | Y) & X is (Y, X). */
10447 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10448 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10449 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10450 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10451 /* X & (X | Y) is (Y, X). */
10452 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10453 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10454 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10455 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10456 /* X & (Y | X) is (Y, X). */
10457 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10458 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10459 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10460 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10461
cef65eaa
RS
10462 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10463 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10464 && integer_onep (TREE_OPERAND (arg0, 1))
10465 && integer_onep (arg1))
10466 {
10467 tem = TREE_OPERAND (arg0, 0);
10468 return fold_build2 (EQ_EXPR, type,
10469 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10470 build_int_cst (TREE_TYPE (tem), 1)),
10471 build_int_cst (TREE_TYPE (tem), 0));
10472 }
10473 /* Fold ~X & 1 as (X & 1) == 0. */
10474 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10475 && integer_onep (arg1))
10476 {
10477 tem = TREE_OPERAND (arg0, 0);
10478 return fold_build2 (EQ_EXPR, type,
10479 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10480 build_int_cst (TREE_TYPE (tem), 1)),
10481 build_int_cst (TREE_TYPE (tem), 0));
10482 }
10483
dd2c62dc
RS
10484 /* Fold (X ^ Y) & Y as ~X & Y. */
10485 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10486 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10487 {
10488 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10489 return fold_build2 (BIT_AND_EXPR, type,
10490 fold_build1 (BIT_NOT_EXPR, type, tem),
10491 fold_convert (type, arg1));
10492 }
10493 /* Fold (X ^ Y) & X as ~Y & X. */
10494 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10495 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10496 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10497 {
10498 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10499 return fold_build2 (BIT_AND_EXPR, type,
10500 fold_build1 (BIT_NOT_EXPR, type, tem),
10501 fold_convert (type, arg1));
10502 }
10503 /* Fold X & (X ^ Y) as X & ~Y. */
10504 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10505 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10506 {
10507 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10508 return fold_build2 (BIT_AND_EXPR, type,
10509 fold_convert (type, arg0),
10510 fold_build1 (BIT_NOT_EXPR, type, tem));
10511 }
10512 /* Fold X & (Y ^ X) as ~Y & X. */
10513 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10514 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10515 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10516 {
10517 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10518 return fold_build2 (BIT_AND_EXPR, type,
10519 fold_build1 (BIT_NOT_EXPR, type, tem),
10520 fold_convert (type, arg0));
10521 }
10522
0aee4751
KH
10523 t1 = distribute_bit_expr (code, type, arg0, arg1);
10524 if (t1 != NULL_TREE)
10525 return t1;
10526 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10527 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10528 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10529 {
10530 unsigned int prec
10531 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10532
10533 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
10534 && (~TREE_INT_CST_LOW (arg1)
10535 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
10536 return fold_convert (type, TREE_OPERAND (arg0, 0));
10537 }
10538
10539 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
10540
10541 This results in more efficient code for machines without a NOR
10542 instruction. Combine will canonicalize to the first form
10543 which will allow use of NOR instructions provided by the
10544 backend if they exist. */
10545 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10546 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10547 {
7f20a5b7
KH
10548 return fold_build1 (BIT_NOT_EXPR, type,
10549 build2 (BIT_IOR_EXPR, type,
10550 TREE_OPERAND (arg0, 0),
10551 TREE_OPERAND (arg1, 0)));
0aee4751
KH
10552 }
10553
10554 goto associate;
10555
10556 case RDIV_EXPR:
10557 /* Don't touch a floating-point divide by zero unless the mode
10558 of the constant can represent infinity. */
10559 if (TREE_CODE (arg1) == REAL_CST
10560 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10561 && real_zerop (arg1))
62ab45cc 10562 return NULL_TREE;
0aee4751 10563
ffbc33cc 10564 /* Optimize A / A to 1.0 if we don't care about
1d8b38a0
UB
10565 NaNs or Infinities. Skip the transformation
10566 for non-real operands. */
10567 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
10568 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
ffbc33cc
UB
10569 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
10570 && operand_equal_p (arg0, arg1, 0))
10571 {
10572 tree r = build_real (TREE_TYPE (arg0), dconst1);
10573
10574 return omit_two_operands (type, r, arg0, arg1);
10575 }
10576
1d8b38a0
UB
10577 /* The complex version of the above A / A optimization. */
10578 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10579 && operand_equal_p (arg0, arg1, 0))
10580 {
10581 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
10582 if (! HONOR_NANS (TYPE_MODE (elem_type))
10583 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
10584 {
10585 tree r = build_real (elem_type, dconst1);
10586 /* omit_two_operands will call fold_convert for us. */
10587 return omit_two_operands (type, r, arg0, arg1);
10588 }
10589 }
10590
0aee4751
KH
10591 /* (-A) / (-B) -> A / B */
10592 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
7f20a5b7
KH
10593 return fold_build2 (RDIV_EXPR, type,
10594 TREE_OPERAND (arg0, 0),
10595 negate_expr (arg1));
0aee4751 10596 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
7f20a5b7
KH
10597 return fold_build2 (RDIV_EXPR, type,
10598 negate_expr (arg0),
10599 TREE_OPERAND (arg1, 0));
0aee4751
KH
10600
10601 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
10602 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10603 && real_onep (arg1))
10604 return non_lvalue (fold_convert (type, arg0));
10605
10606 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
10607 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10608 && real_minus_onep (arg1))
10609 return non_lvalue (fold_convert (type, negate_expr (arg0)));
10610
10611 /* If ARG1 is a constant, we can convert this to a multiply by the
10612 reciprocal. This does not have the same rounding properties,
10613 so only do this if -funsafe-math-optimizations. We can actually
10614 always safely do it if ARG1 is a power of two, but it's hard to
10615 tell if it is or not in a portable manner. */
10616 if (TREE_CODE (arg1) == REAL_CST)
10617 {
10618 if (flag_unsafe_math_optimizations
10619 && 0 != (tem = const_binop (code, build_real (type, dconst1),
10620 arg1, 0)))
7f20a5b7 10621 return fold_build2 (MULT_EXPR, type, arg0, tem);
0aee4751
KH
10622 /* Find the reciprocal if optimizing and the result is exact. */
10623 if (optimize)
10624 {
10625 REAL_VALUE_TYPE r;
10626 r = TREE_REAL_CST (arg1);
10627 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
10628 {
10629 tem = build_real (type, r);
3bedcc89
RG
10630 return fold_build2 (MULT_EXPR, type,
10631 fold_convert (type, arg0), tem);
0aee4751
KH
10632 }
10633 }
10634 }
10635 /* Convert A/B/C to A/(B*C). */
10636 if (flag_unsafe_math_optimizations
10637 && TREE_CODE (arg0) == RDIV_EXPR)
7f20a5b7
KH
10638 return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
10639 fold_build2 (MULT_EXPR, type,
10640 TREE_OPERAND (arg0, 1), arg1));
0aee4751
KH
10641
10642 /* Convert A/(B/C) to (A/B)*C. */
10643 if (flag_unsafe_math_optimizations
10644 && TREE_CODE (arg1) == RDIV_EXPR)
7f20a5b7
KH
10645 return fold_build2 (MULT_EXPR, type,
10646 fold_build2 (RDIV_EXPR, type, arg0,
10647 TREE_OPERAND (arg1, 0)),
10648 TREE_OPERAND (arg1, 1));
0aee4751
KH
10649
10650 /* Convert C1/(X*C2) into (C1/C2)/X. */
10651 if (flag_unsafe_math_optimizations
10652 && TREE_CODE (arg1) == MULT_EXPR
10653 && TREE_CODE (arg0) == REAL_CST
10654 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
10655 {
10656 tree tem = const_binop (RDIV_EXPR, arg0,
10657 TREE_OPERAND (arg1, 1), 0);
10658 if (tem)
7f20a5b7
KH
10659 return fold_build2 (RDIV_EXPR, type, tem,
10660 TREE_OPERAND (arg1, 0));
0aee4751
KH
10661 }
10662
0aee4751
KH
10663 if (flag_unsafe_math_optimizations)
10664 {
10665 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10666 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10667
10668 /* Optimize sin(x)/cos(x) as tan(x). */
10669 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
10670 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
10671 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
5039610b
SL
10672 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10673 CALL_EXPR_ARG (arg1, 0), 0))
0aee4751
KH
10674 {
10675 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
10676
10677 if (tanfn != NULL_TREE)
5039610b 10678 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
0aee4751
KH
10679 }
10680
10681 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
10682 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
10683 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
10684 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
5039610b
SL
10685 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10686 CALL_EXPR_ARG (arg1, 0), 0))
0aee4751
KH
10687 {
10688 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
10689
10690 if (tanfn != NULL_TREE)
10691 {
5039610b 10692 tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
7f20a5b7
KH
10693 return fold_build2 (RDIV_EXPR, type,
10694 build_real (type, dconst1), tmp);
0aee4751
KH
10695 }
10696 }
10697
d531830f
RS
10698 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
10699 NaNs or Infinities. */
10700 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
10701 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
10702 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
10703 {
5039610b
SL
10704 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10705 tree arg01 = CALL_EXPR_ARG (arg1, 0);
d531830f
RS
10706
10707 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
10708 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
10709 && operand_equal_p (arg00, arg01, 0))
10710 {
10711 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
10712
10713 if (cosfn != NULL_TREE)
5039610b 10714 return build_call_expr (cosfn, 1, arg00);
d531830f
RS
10715 }
10716 }
10717
10718 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
6416ae7f 10719 NaNs or Infinities. */
d531830f
RS
10720 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
10721 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
10722 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
10723 {
5039610b
SL
10724 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10725 tree arg01 = CALL_EXPR_ARG (arg1, 0);
d531830f
RS
10726
10727 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
10728 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
10729 && operand_equal_p (arg00, arg01, 0))
10730 {
10731 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
10732
10733 if (cosfn != NULL_TREE)
10734 {
5039610b 10735 tree tmp = build_call_expr (cosfn, 1, arg00);
b71b8086 10736 return fold_build2 (RDIV_EXPR, type,
d531830f 10737 build_real (type, dconst1),
b71b8086 10738 tmp);
d531830f
RS
10739 }
10740 }
10741 }
10742
0aee4751
KH
10743 /* Optimize pow(x,c)/x as pow(x,c-1). */
10744 if (fcode0 == BUILT_IN_POW
10745 || fcode0 == BUILT_IN_POWF
10746 || fcode0 == BUILT_IN_POWL)
10747 {
5039610b
SL
10748 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10749 tree arg01 = CALL_EXPR_ARG (arg0, 1);
0aee4751 10750 if (TREE_CODE (arg01) == REAL_CST
455f14dd 10751 && !TREE_OVERFLOW (arg01)
0aee4751
KH
10752 && operand_equal_p (arg1, arg00, 0))
10753 {
5039610b 10754 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
0aee4751 10755 REAL_VALUE_TYPE c;
5039610b 10756 tree arg;
0aee4751
KH
10757
10758 c = TREE_REAL_CST (arg01);
10759 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
10760 arg = build_real (type, c);
5039610b 10761 return build_call_expr (powfn, 2, arg1, arg);
0aee4751
KH
10762 }
10763 }
d531830f 10764
9883e373
UB
10765 /* Optimize a/root(b/c) into a*root(c/b). */
10766 if (BUILTIN_ROOT_P (fcode1))
f1da2df1
UB
10767 {
10768 tree rootarg = CALL_EXPR_ARG (arg1, 0);
10769
10770 if (TREE_CODE (rootarg) == RDIV_EXPR)
10771 {
10772 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10773 tree b = TREE_OPERAND (rootarg, 0);
10774 tree c = TREE_OPERAND (rootarg, 1);
10775
10776 tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
10777
10778 tmp = build_call_expr (rootfn, 1, tmp);
10779 return fold_build2 (MULT_EXPR, type, arg0, tmp);
10780 }
10781 }
10782
d531830f
RS
10783 /* Optimize x/expN(y) into x*expN(-y). */
10784 if (BUILTIN_EXPONENT_P (fcode1))
10785 {
5039610b
SL
10786 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10787 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
10788 arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
d531830f
RS
10789 return fold_build2 (MULT_EXPR, type, arg0, arg1);
10790 }
10791
10792 /* Optimize x/pow(y,z) into x*pow(y,-z). */
10793 if (fcode1 == BUILT_IN_POW
10794 || fcode1 == BUILT_IN_POWF
10795 || fcode1 == BUILT_IN_POWL)
10796 {
5039610b
SL
10797 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10798 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10799 tree arg11 = CALL_EXPR_ARG (arg1, 1);
d531830f 10800 tree neg11 = fold_convert (type, negate_expr (arg11));
5039610b 10801 arg1 = build_call_expr (powfn, 2, arg10, neg11);
d531830f
RS
10802 return fold_build2 (MULT_EXPR, type, arg0, arg1);
10803 }
0aee4751 10804 }
fd6c76f4 10805 return NULL_TREE;
0aee4751
KH
10806
10807 case TRUNC_DIV_EXPR:
0aee4751 10808 case FLOOR_DIV_EXPR:
0f35201e
AM
10809 /* Simplify A / (B << N) where A and B are positive and B is
10810 a power of 2, to A >> (N + log2(B)). */
6ac01510 10811 strict_overflow_p = false;
0f35201e 10812 if (TREE_CODE (arg1) == LSHIFT_EXPR
6ac01510
ILT
10813 && (TYPE_UNSIGNED (type)
10814 || tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)))
0f35201e
AM
10815 {
10816 tree sval = TREE_OPERAND (arg1, 0);
10817 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10818 {
10819 tree sh_cnt = TREE_OPERAND (arg1, 1);
10820 unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
10821
6ac01510
ILT
10822 if (strict_overflow_p)
10823 fold_overflow_warning (("assuming signed overflow does not "
10824 "occur when simplifying A / (B << N)"),
10825 WARN_STRICT_OVERFLOW_MISC);
10826
0f35201e
AM
10827 sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
10828 sh_cnt, build_int_cst (NULL_TREE, pow2));
10829 return fold_build2 (RSHIFT_EXPR, type,
10830 fold_convert (type, arg0), sh_cnt);
10831 }
10832 }
10833 /* Fall thru */
10834
10835 case ROUND_DIV_EXPR:
0aee4751
KH
10836 case CEIL_DIV_EXPR:
10837 case EXACT_DIV_EXPR:
10838 if (integer_onep (arg1))
10839 return non_lvalue (fold_convert (type, arg0));
10840 if (integer_zerop (arg1))
62ab45cc 10841 return NULL_TREE;
0aee4751
KH
10842 /* X / -1 is -X. */
10843 if (!TYPE_UNSIGNED (type)
10844 && TREE_CODE (arg1) == INTEGER_CST
10845 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
10846 && TREE_INT_CST_HIGH (arg1) == -1)
10847 return fold_convert (type, negate_expr (arg0));
10848
37d3243d
AP
10849 /* Convert -A / -B to A / B when the type is signed and overflow is
10850 undefined. */
eeef0e45 10851 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
37d3243d
AP
10852 && TREE_CODE (arg0) == NEGATE_EXPR
10853 && negate_expr_p (arg1))
6ac01510
ILT
10854 {
10855 if (INTEGRAL_TYPE_P (type))
10856 fold_overflow_warning (("assuming signed overflow does not occur "
10857 "when distributing negation across "
10858 "division"),
10859 WARN_STRICT_OVERFLOW_MISC);
10860 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
10861 negate_expr (arg1));
10862 }
eeef0e45 10863 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
37d3243d
AP
10864 && TREE_CODE (arg1) == NEGATE_EXPR
10865 && negate_expr_p (arg0))
6ac01510
ILT
10866 {
10867 if (INTEGRAL_TYPE_P (type))
10868 fold_overflow_warning (("assuming signed overflow does not occur "
10869 "when distributing negation across "
10870 "division"),
10871 WARN_STRICT_OVERFLOW_MISC);
10872 return fold_build2 (code, type, negate_expr (arg0),
10873 TREE_OPERAND (arg1, 0));
10874 }
37d3243d 10875
0aee4751
KH
10876 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10877 operation, EXACT_DIV_EXPR.
10878
10879 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10880 At one time others generated faster code, it's not clear if they do
10881 after the last round to changes to the DIV code in expmed.c. */
10882 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10883 && multiple_of_p (type, arg0, arg1))
7f20a5b7 10884 return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
0aee4751 10885
6ac01510 10886 strict_overflow_p = false;
0aee4751 10887 if (TREE_CODE (arg1) == INTEGER_CST
6ac01510
ILT
10888 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10889 &strict_overflow_p)))
10890 {
10891 if (strict_overflow_p)
10892 fold_overflow_warning (("assuming signed overflow does not occur "
10893 "when simplifying division"),
10894 WARN_STRICT_OVERFLOW_MISC);
10895 return fold_convert (type, tem);
10896 }
0aee4751 10897
fd6c76f4 10898 return NULL_TREE;
0aee4751
KH
10899
10900 case CEIL_MOD_EXPR:
10901 case FLOOR_MOD_EXPR:
10902 case ROUND_MOD_EXPR:
10903 case TRUNC_MOD_EXPR:
10904 /* X % 1 is always zero, but be sure to preserve any side
10905 effects in X. */
10906 if (integer_onep (arg1))
10907 return omit_one_operand (type, integer_zero_node, arg0);
10908
10909 /* X % 0, return X % 0 unchanged so that we can get the
10910 proper warnings and errors. */
10911 if (integer_zerop (arg1))
62ab45cc 10912 return NULL_TREE;
0aee4751
KH
10913
10914 /* 0 % X is always zero, but be sure to preserve any side
10915 effects in X. Place this after checking for X == 0. */
10916 if (integer_zerop (arg0))
10917 return omit_one_operand (type, integer_zero_node, arg1);
10918
10919 /* X % -1 is zero. */
10920 if (!TYPE_UNSIGNED (type)
10921 && TREE_CODE (arg1) == INTEGER_CST
10922 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
10923 && TREE_INT_CST_HIGH (arg1) == -1)
10924 return omit_one_operand (type, integer_zero_node, arg0);
10925
2d9474df 10926 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
0f35201e 10927 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
6ac01510 10928 strict_overflow_p = false;
2d9474df 10929 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
6ac01510
ILT
10930 && (TYPE_UNSIGNED (type)
10931 || tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)))
0aee4751 10932 {
0f35201e
AM
10933 tree c = arg1;
10934 /* Also optimize A % (C << N) where C is a power of 2,
10935 to A & ((C << N) - 1). */
10936 if (TREE_CODE (arg1) == LSHIFT_EXPR)
10937 c = TREE_OPERAND (arg1, 0);
0aee4751 10938
0f35201e 10939 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
0aee4751 10940 {
000d8d44
RS
10941 tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
10942 build_int_cst (TREE_TYPE (arg1), 1));
6ac01510
ILT
10943 if (strict_overflow_p)
10944 fold_overflow_warning (("assuming signed overflow does not "
10945 "occur when simplifying "
10946 "X % (power of two)"),
10947 WARN_STRICT_OVERFLOW_MISC);
0f35201e
AM
10948 return fold_build2 (BIT_AND_EXPR, type,
10949 fold_convert (type, arg0),
10950 fold_convert (type, mask));
0aee4751 10951 }
0aee4751
KH
10952 }
10953
10954 /* X % -C is the same as X % C. */
10955 if (code == TRUNC_MOD_EXPR
10956 && !TYPE_UNSIGNED (type)
10957 && TREE_CODE (arg1) == INTEGER_CST
455f14dd 10958 && !TREE_OVERFLOW (arg1)
0aee4751 10959 && TREE_INT_CST_HIGH (arg1) < 0
eeef0e45 10960 && !TYPE_OVERFLOW_TRAPS (type)
0aee4751
KH
10961 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
10962 && !sign_bit_p (arg1, arg1))
7f20a5b7
KH
10963 return fold_build2 (code, type, fold_convert (type, arg0),
10964 fold_convert (type, negate_expr (arg1)));
0aee4751
KH
10965
10966 /* X % -Y is the same as X % Y. */
10967 if (code == TRUNC_MOD_EXPR
10968 && !TYPE_UNSIGNED (type)
10969 && TREE_CODE (arg1) == NEGATE_EXPR
eeef0e45 10970 && !TYPE_OVERFLOW_TRAPS (type))
7f20a5b7
KH
10971 return fold_build2 (code, type, fold_convert (type, arg0),
10972 fold_convert (type, TREE_OPERAND (arg1, 0)));
0aee4751
KH
10973
10974 if (TREE_CODE (arg1) == INTEGER_CST
6ac01510
ILT
10975 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10976 &strict_overflow_p)))
10977 {
10978 if (strict_overflow_p)
10979 fold_overflow_warning (("assuming signed overflow does not occur "
10980 "when simplifying modulos"),
10981 WARN_STRICT_OVERFLOW_MISC);
10982 return fold_convert (type, tem);
10983 }
0aee4751 10984
fd6c76f4 10985 return NULL_TREE;
0aee4751
KH
10986
10987 case LROTATE_EXPR:
10988 case RROTATE_EXPR:
10989 if (integer_all_onesp (arg0))
10990 return omit_one_operand (type, arg0, arg1);
10991 goto shift;
10992
10993 case RSHIFT_EXPR:
10994 /* Optimize -1 >> x for arithmetic right shifts. */
10995 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
10996 return omit_one_operand (type, arg0, arg1);
10997 /* ... fall through ... */
10998
10999 case LSHIFT_EXPR:
11000 shift:
11001 if (integer_zerop (arg1))
11002 return non_lvalue (fold_convert (type, arg0));
11003 if (integer_zerop (arg0))
11004 return omit_one_operand (type, arg0, arg1);
11005
11006 /* Since negative shift count is not well-defined,
11007 don't try to compute it in the compiler. */
11008 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
62ab45cc 11009 return NULL_TREE;
e3d025cb
JM
11010
11011 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2d60e929 11012 if (TREE_CODE (op0) == code && host_integerp (arg1, false)
e3d025cb
JM
11013 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11014 && host_integerp (TREE_OPERAND (arg0, 1), false)
11015 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11016 {
11017 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11018 + TREE_INT_CST_LOW (arg1));
11019
11020 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11021 being well defined. */
11022 if (low >= TYPE_PRECISION (type))
11023 {
11024 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11025 low = low % TYPE_PRECISION (type);
11026 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11027 return build_int_cst (type, 0);
11028 else
11029 low = TYPE_PRECISION (type) - 1;
11030 }
11031
11032 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11033 build_int_cst (type, low));
11034 }
11035
a165e746
JM
11036 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11037 into x & ((unsigned)-1 >> c) for unsigned types. */
11038 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11039 || (TYPE_UNSIGNED (type)
11040 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
e3d025cb
JM
11041 && host_integerp (arg1, false)
11042 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11043 && host_integerp (TREE_OPERAND (arg0, 1), false)
11044 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11045 {
11046 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11047 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
e3d025cb
JM
11048 tree lshift;
11049 tree arg00;
11050
11051 if (low0 == low1)
11052 {
11053 arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11054
a165e746
JM
11055 lshift = build_int_cst (type, -1);
11056 lshift = int_const_binop (code, lshift, arg1, 0);
e3d025cb
JM
11057
11058 return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11059 }
11060 }
11061
0aee4751
KH
11062 /* Rewrite an LROTATE_EXPR by a constant into an
11063 RROTATE_EXPR by a new constant. */
11064 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11065 {
000d8d44 11066 tree tem = build_int_cst (TREE_TYPE (arg1),
0aee4751 11067 GET_MODE_BITSIZE (TYPE_MODE (type)));
0aee4751 11068 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
7f20a5b7 11069 return fold_build2 (RROTATE_EXPR, type, arg0, tem);
0aee4751
KH
11070 }
11071
11072 /* If we have a rotate of a bit operation with the rotate count and
11073 the second operand of the bit operation both constant,
11074 permute the two operations. */
11075 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11076 && (TREE_CODE (arg0) == BIT_AND_EXPR
11077 || TREE_CODE (arg0) == BIT_IOR_EXPR
11078 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11079 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7f20a5b7
KH
11080 return fold_build2 (TREE_CODE (arg0), type,
11081 fold_build2 (code, type,
11082 TREE_OPERAND (arg0, 0), arg1),
11083 fold_build2 (code, type,
11084 TREE_OPERAND (arg0, 1), arg1));
0aee4751
KH
11085
11086 /* Two consecutive rotates adding up to the width of the mode can
11087 be ignored. */
11088 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11089 && TREE_CODE (arg0) == RROTATE_EXPR
11090 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11091 && TREE_INT_CST_HIGH (arg1) == 0
11092 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11093 && ((TREE_INT_CST_LOW (arg1)
11094 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11095 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
11096 return TREE_OPERAND (arg0, 0);
11097
fd6c76f4 11098 return NULL_TREE;
0aee4751
KH
11099
11100 case MIN_EXPR:
11101 if (operand_equal_p (arg0, arg1, 0))
11102 return omit_one_operand (type, arg0, arg1);
11103 if (INTEGRAL_TYPE_P (type)
11104 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11105 return omit_one_operand (type, arg1, arg0);
292f30c5
EB
11106 tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11107 if (tem)
11108 return tem;
0aee4751
KH
11109 goto associate;
11110
11111 case MAX_EXPR:
11112 if (operand_equal_p (arg0, arg1, 0))
11113 return omit_one_operand (type, arg0, arg1);
11114 if (INTEGRAL_TYPE_P (type)
11115 && TYPE_MAX_VALUE (type)
11116 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11117 return omit_one_operand (type, arg1, arg0);
292f30c5
EB
11118 tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11119 if (tem)
11120 return tem;
0aee4751
KH
11121 goto associate;
11122
11123 case TRUTH_ANDIF_EXPR:
11124 /* Note that the operands of this must be ints
11125 and their values must be 0 or 1.
11126 ("true" is a fixed value perhaps depending on the language.) */
11127 /* If first arg is constant zero, return it. */
11128 if (integer_zerop (arg0))
11129 return fold_convert (type, arg0);
11130 case TRUTH_AND_EXPR:
11131 /* If either arg is constant true, drop it. */
11132 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11133 return non_lvalue (fold_convert (type, arg1));
11134 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11135 /* Preserve sequence points. */
11136 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11137 return non_lvalue (fold_convert (type, arg0));
11138 /* If second arg is constant zero, result is zero, but first arg
11139 must be evaluated. */
11140 if (integer_zerop (arg1))
11141 return omit_one_operand (type, arg1, arg0);
11142 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11143 case will be handled here. */
11144 if (integer_zerop (arg0))
11145 return omit_one_operand (type, arg0, arg1);
11146
11147 /* !X && X is always false. */
11148 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11149 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11150 return omit_one_operand (type, integer_zero_node, arg1);
11151 /* X && !X is always false. */
11152 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11153 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11154 return omit_one_operand (type, integer_zero_node, arg0);
11155
11156 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11157 means A >= Y && A != MAX, but in this case we know that
11158 A < X <= MAX. */
11159
11160 if (!TREE_SIDE_EFFECTS (arg0)
11161 && !TREE_SIDE_EFFECTS (arg1))
11162 {
11163 tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
70a9e64b 11164 if (tem && !operand_equal_p (tem, arg0, 0))
7f20a5b7 11165 return fold_build2 (code, type, tem, arg1);
0aee4751
KH
11166
11167 tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
70a9e64b 11168 if (tem && !operand_equal_p (tem, arg1, 0))
7f20a5b7 11169 return fold_build2 (code, type, arg0, tem);
0aee4751
KH
11170 }
11171
11172 truth_andor:
11173 /* We only do these simplifications if we are optimizing. */
11174 if (!optimize)
62ab45cc 11175 return NULL_TREE;
0aee4751
KH
11176
11177 /* Check for things like (A || B) && (A || C). We can convert this
11178 to A || (B && C). Note that either operator can be any of the four
11179 truth and/or operations and the transformation will still be
11180 valid. Also note that we only care about order for the
11181 ANDIF and ORIF operators. If B contains side effects, this
11182 might change the truth-value of A. */
11183 if (TREE_CODE (arg0) == TREE_CODE (arg1)
11184 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11185 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11186 || TREE_CODE (arg0) == TRUTH_AND_EXPR
11187 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11188 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11189 {
11190 tree a00 = TREE_OPERAND (arg0, 0);
11191 tree a01 = TREE_OPERAND (arg0, 1);
11192 tree a10 = TREE_OPERAND (arg1, 0);
11193 tree a11 = TREE_OPERAND (arg1, 1);
11194 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11195 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11196 && (code == TRUTH_AND_EXPR
11197 || code == TRUTH_OR_EXPR));
11198
11199 if (operand_equal_p (a00, a10, 0))
7f20a5b7
KH
11200 return fold_build2 (TREE_CODE (arg0), type, a00,
11201 fold_build2 (code, type, a01, a11));
0aee4751 11202 else if (commutative && operand_equal_p (a00, a11, 0))
7f20a5b7
KH
11203 return fold_build2 (TREE_CODE (arg0), type, a00,
11204 fold_build2 (code, type, a01, a10));
0aee4751 11205 else if (commutative && operand_equal_p (a01, a10, 0))
7f20a5b7
KH
11206 return fold_build2 (TREE_CODE (arg0), type, a01,
11207 fold_build2 (code, type, a00, a11));
0aee4751
KH
11208
11209 /* This case if tricky because we must either have commutative
11210 operators or else A10 must not have side-effects. */
11211
11212 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
11213 && operand_equal_p (a01, a11, 0))
7f20a5b7
KH
11214 return fold_build2 (TREE_CODE (arg0), type,
11215 fold_build2 (code, type, a00, a10),
11216 a01);
0aee4751
KH
11217 }
11218
11219 /* See if we can build a range comparison. */
e1f04615 11220 if (0 != (tem = fold_range_test (code, type, op0, op1)))
0aee4751
KH
11221 return tem;
11222
11223 /* Check for the possibility of merging component references. If our
11224 lhs is another similar operation, try to merge its rhs with our
11225 rhs. Then try to merge our lhs and rhs. */
11226 if (TREE_CODE (arg0) == code
11227 && 0 != (tem = fold_truthop (code, type,
11228 TREE_OPERAND (arg0, 1), arg1)))
7f20a5b7 11229 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
0aee4751
KH
11230
11231 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
11232 return tem;
11233
62ab45cc 11234 return NULL_TREE;
0aee4751
KH
11235
11236 case TRUTH_ORIF_EXPR:
11237 /* Note that the operands of this must be ints
11238 and their values must be 0 or true.
11239 ("true" is a fixed value perhaps depending on the language.) */
11240 /* If first arg is constant true, return it. */
11241 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11242 return fold_convert (type, arg0);
11243 case TRUTH_OR_EXPR:
11244 /* If either arg is constant zero, drop it. */
11245 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
11246 return non_lvalue (fold_convert (type, arg1));
11247 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
11248 /* Preserve sequence points. */
11249 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11250 return non_lvalue (fold_convert (type, arg0));
11251 /* If second arg is constant true, result is true, but we must
11252 evaluate first arg. */
11253 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
11254 return omit_one_operand (type, arg1, arg0);
11255 /* Likewise for first arg, but note this only occurs here for
11256 TRUTH_OR_EXPR. */
11257 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11258 return omit_one_operand (type, arg0, arg1);
11259
11260 /* !X || X is always true. */
11261 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11262 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11263 return omit_one_operand (type, integer_one_node, arg1);
11264 /* X || !X is always true. */
11265 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11266 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11267 return omit_one_operand (type, integer_one_node, arg0);
11268
11269 goto truth_andor;
11270
11271 case TRUTH_XOR_EXPR:
11272 /* If the second arg is constant zero, drop it. */
11273 if (integer_zerop (arg1))
11274 return non_lvalue (fold_convert (type, arg0));
11275 /* If the second arg is constant true, this is a logical inversion. */
11276 if (integer_onep (arg1))
90ec750d
RS
11277 {
11278 /* Only call invert_truthvalue if operand is a truth value. */
11279 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
7f20a5b7 11280 tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
90ec750d
RS
11281 else
11282 tem = invert_truthvalue (arg0);
11283 return non_lvalue (fold_convert (type, tem));
11284 }
0aee4751
KH
11285 /* Identical arguments cancel to zero. */
11286 if (operand_equal_p (arg0, arg1, 0))
11287 return omit_one_operand (type, integer_zero_node, arg0);
11288
11289 /* !X ^ X is always true. */
11290 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11291 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11292 return omit_one_operand (type, integer_one_node, arg1);
11293
11294 /* X ^ !X is always true. */
11295 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11296 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11297 return omit_one_operand (type, integer_one_node, arg0);
11298
62ab45cc 11299 return NULL_TREE;
0aee4751
KH
11300
11301 case EQ_EXPR:
11302 case NE_EXPR:
e26ec0bb
RS
11303 tem = fold_comparison (code, type, op0, op1);
11304 if (tem != NULL_TREE)
11305 return tem;
210dfe6e 11306
a7e1c928
AP
11307 /* bool_var != 0 becomes bool_var. */
11308 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11309 && code == NE_EXPR)
11310 return non_lvalue (fold_convert (type, arg0));
e26ec0bb 11311
a7e1c928
AP
11312 /* bool_var == 1 becomes bool_var. */
11313 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11314 && code == EQ_EXPR)
11315 return non_lvalue (fold_convert (type, arg0));
0aee4751 11316
7934558d
AP
11317 /* bool_var != 1 becomes !bool_var. */
11318 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11319 && code == NE_EXPR)
11320 return fold_build1 (TRUTH_NOT_EXPR, type, arg0);
11321
11322 /* bool_var == 0 becomes !bool_var. */
11323 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11324 && code == EQ_EXPR)
11325 return fold_build1 (TRUTH_NOT_EXPR, type, arg0);
11326
0aee4751
KH
11327 /* If this is an equality comparison of the address of two non-weak,
11328 unaliased symbols neither of which are extern (since we do not
11329 have access to attributes for externs), then we know the result. */
e26ec0bb 11330 if (TREE_CODE (arg0) == ADDR_EXPR
820cc88f 11331 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
0aee4751
KH
11332 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
11333 && ! lookup_attribute ("alias",
11334 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
11335 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
11336 && TREE_CODE (arg1) == ADDR_EXPR
820cc88f 11337 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
0aee4751
KH
11338 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
11339 && ! lookup_attribute ("alias",
11340 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
11341 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
59f7a202
JL
11342 {
11343 /* We know that we're looking at the address of two
11344 non-weak, unaliased, static _DECL nodes.
11345
11346 It is both wasteful and incorrect to call operand_equal_p
11347 to compare the two ADDR_EXPR nodes. It is wasteful in that
11348 all we need to do is test pointer equality for the arguments
11349 to the two ADDR_EXPR nodes. It is incorrect to use
11350 operand_equal_p as that function is NOT equivalent to a
11351 C equality test. It can in fact return false for two
11352 objects which would test as equal using the C equality
11353 operator. */
11354 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
11355 return constant_boolean_node (equal
11356 ? code == EQ_EXPR : code != EQ_EXPR,
11357 type);
11358 }
0aee4751 11359
e26ec0bb
RS
11360 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
11361 a MINUS_EXPR of a constant, we can convert it into a comparison with
11362 a revised constant as long as no overflow occurs. */
11363 if (TREE_CODE (arg1) == INTEGER_CST
11364 && (TREE_CODE (arg0) == PLUS_EXPR
11365 || TREE_CODE (arg0) == MINUS_EXPR)
11366 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11367 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
11368 ? MINUS_EXPR : PLUS_EXPR,
45988118
RG
11369 fold_convert (TREE_TYPE (arg0), arg1),
11370 TREE_OPERAND (arg0, 1), 0))
455f14dd 11371 && !TREE_OVERFLOW (tem))
e26ec0bb 11372 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
0eeb03e6 11373
e26ec0bb
RS
11374 /* Similarly for a NEGATE_EXPR. */
11375 if (TREE_CODE (arg0) == NEGATE_EXPR
11376 && TREE_CODE (arg1) == INTEGER_CST
11377 && 0 != (tem = negate_expr (arg1))
11378 && TREE_CODE (tem) == INTEGER_CST
455f14dd 11379 && !TREE_OVERFLOW (tem))
e26ec0bb 11380 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
0eeb03e6 11381
cf06e5c1
RS
11382 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
11383 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11384 && TREE_CODE (arg1) == INTEGER_CST
11385 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11386 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11387 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
11388 fold_convert (TREE_TYPE (arg0), arg1),
11389 TREE_OPERAND (arg0, 1)));
11390
a31498d2
RG
11391 /* Transform comparisons of the form X +- C CMP X. */
11392 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11393 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11394 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11395 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11396 || POINTER_TYPE_P (TREE_TYPE (arg0))))
11397 {
11398 tree cst = TREE_OPERAND (arg0, 1);
11399
11400 if (code == EQ_EXPR
11401 && !integer_zerop (cst))
11402 return omit_two_operands (type, boolean_false_node,
11403 TREE_OPERAND (arg0, 0), arg1);
11404 else
11405 return omit_two_operands (type, boolean_true_node,
11406 TREE_OPERAND (arg0, 0), arg1);
11407 }
11408
e26ec0bb
RS
11409 /* If we have X - Y == 0, we can convert that to X == Y and similarly
11410 for !=. Don't do this for ordered comparisons due to overflow. */
11411 if (TREE_CODE (arg0) == MINUS_EXPR
11412 && integer_zerop (arg1))
11413 return fold_build2 (code, type,
11414 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
0eeb03e6 11415
e26ec0bb
RS
11416 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
11417 if (TREE_CODE (arg0) == ABS_EXPR
11418 && (integer_zerop (arg1) || real_zerop (arg1)))
11419 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
0eeb03e6 11420
e26ec0bb
RS
11421 /* If this is an EQ or NE comparison with zero and ARG0 is
11422 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
11423 two operations, but the latter can be done in one less insn
11424 on machines that have only two-operand insns or on which a
11425 constant cannot be the first operand. */
11426 if (TREE_CODE (arg0) == BIT_AND_EXPR
11427 && integer_zerop (arg1))
11428 {
11429 tree arg00 = TREE_OPERAND (arg0, 0);
11430 tree arg01 = TREE_OPERAND (arg0, 1);
11431 if (TREE_CODE (arg00) == LSHIFT_EXPR
11432 && integer_onep (TREE_OPERAND (arg00, 0)))
11433 return
11434 fold_build2 (code, type,
11435 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11436 build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
11437 arg01, TREE_OPERAND (arg00, 1)),
11438 fold_convert (TREE_TYPE (arg0),
11439 integer_one_node)),
11440 arg1);
11441 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
11442 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
11443 return
11444 fold_build2 (code, type,
11445 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11446 build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
11447 arg00, TREE_OPERAND (arg01, 1)),
11448 fold_convert (TREE_TYPE (arg0),
11449 integer_one_node)),
11450 arg1);
11451 }
11452
11453 /* If this is an NE or EQ comparison of zero against the result of a
11454 signed MOD operation whose second operand is a power of 2, make
11455 the MOD operation unsigned since it is simpler and equivalent. */
11456 if (integer_zerop (arg1)
11457 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
11458 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
11459 || TREE_CODE (arg0) == CEIL_MOD_EXPR
11460 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
11461 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
11462 && integer_pow2p (TREE_OPERAND (arg0, 1)))
11463 {
ca5ba2a3 11464 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
e26ec0bb
RS
11465 tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
11466 fold_convert (newtype,
11467 TREE_OPERAND (arg0, 0)),
11468 fold_convert (newtype,
11469 TREE_OPERAND (arg0, 1)));
11470
11471 return fold_build2 (code, type, newmod,
11472 fold_convert (newtype, arg1));
11473 }
11474
a861485c
RS
11475 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
11476 C1 is a valid shift constant, and C2 is a power of two, i.e.
11477 a single bit. */
11478 if (TREE_CODE (arg0) == BIT_AND_EXPR
11479 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
11480 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
11481 == INTEGER_CST
11482 && integer_pow2p (TREE_OPERAND (arg0, 1))
11483 && integer_zerop (arg1))
11484 {
11485 tree itype = TREE_TYPE (arg0);
11486 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
11487 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
11488
11489 /* Check for a valid shift count. */
11490 if (TREE_INT_CST_HIGH (arg001) == 0
11491 && TREE_INT_CST_LOW (arg001) < prec)
11492 {
11493 tree arg01 = TREE_OPERAND (arg0, 1);
11494 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
11495 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
11496 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
11497 can be rewritten as (X & (C2 << C1)) != 0. */
0ad12cd3 11498 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
a861485c
RS
11499 {
11500 tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
11501 tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
11502 return fold_build2 (code, type, tem, arg1);
11503 }
11504 /* Otherwise, for signed (arithmetic) shifts,
11505 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
11506 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
11507 else if (!TYPE_UNSIGNED (itype))
11508 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
11509 arg000, build_int_cst (itype, 0));
11510 /* Otherwise, of unsigned (logical) shifts,
11511 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
11512 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
11513 else
11514 return omit_one_operand (type,
11515 code == EQ_EXPR ? integer_one_node
11516 : integer_zero_node,
11517 arg000);
11518 }
11519 }
11520
e26ec0bb
RS
11521 /* If this is an NE comparison of zero with an AND of one, remove the
11522 comparison since the AND will give the correct value. */
11523 if (code == NE_EXPR
11524 && integer_zerop (arg1)
11525 && TREE_CODE (arg0) == BIT_AND_EXPR
11526 && integer_onep (TREE_OPERAND (arg0, 1)))
11527 return fold_convert (type, arg0);
11528
11529 /* If we have (A & C) == C where C is a power of 2, convert this into
11530 (A & C) != 0. Similarly for NE_EXPR. */
11531 if (TREE_CODE (arg0) == BIT_AND_EXPR
11532 && integer_pow2p (TREE_OPERAND (arg0, 1))
11533 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11534 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11535 arg0, fold_convert (TREE_TYPE (arg0),
11536 integer_zero_node));
11537
11538 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
11539 bit, then fold the expression into A < 0 or A >= 0. */
11540 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
11541 if (tem)
11542 return tem;
11543
11544 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
11545 Similarly for NE_EXPR. */
11546 if (TREE_CODE (arg0) == BIT_AND_EXPR
11547 && TREE_CODE (arg1) == INTEGER_CST
11548 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11549 {
11550 tree notc = fold_build1 (BIT_NOT_EXPR,
11551 TREE_TYPE (TREE_OPERAND (arg0, 1)),
11552 TREE_OPERAND (arg0, 1));
11553 tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11554 arg1, notc);
11555 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
11556 if (integer_nonzerop (dandnotc))
11557 return omit_one_operand (type, rslt, arg0);
11558 }
11559
11560 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
11561 Similarly for NE_EXPR. */
11562 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11563 && TREE_CODE (arg1) == INTEGER_CST
11564 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11565 {
11566 tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
11567 tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11568 TREE_OPERAND (arg0, 1), notd);
11569 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
11570 if (integer_nonzerop (candnotd))
11571 return omit_one_operand (type, rslt, arg0);
11572 }
11573
11574 /* If this is a comparison of a field, we may be able to simplify it. */
08f0e79e 11575 if ((TREE_CODE (arg0) == COMPONENT_REF
e26ec0bb
RS
11576 || TREE_CODE (arg0) == BIT_FIELD_REF)
11577 /* Handle the constant case even without -O
11578 to make sure the warnings are given. */
11579 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
11580 {
11581 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
11582 if (t1)
11583 return t1;
11584 }
11585
11586 /* Optimize comparisons of strlen vs zero to a compare of the
11587 first character of the string vs zero. To wit,
11588 strlen(ptr) == 0 => *ptr == 0
11589 strlen(ptr) != 0 => *ptr != 0
11590 Other cases should reduce to one of these two (or a constant)
11591 due to the return value of strlen being unsigned. */
11592 if (TREE_CODE (arg0) == CALL_EXPR
11593 && integer_zerop (arg1))
11594 {
11595 tree fndecl = get_callee_fndecl (arg0);
e26ec0bb
RS
11596
11597 if (fndecl
11598 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
11599 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
5039610b
SL
11600 && call_expr_nargs (arg0) == 1
11601 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
e26ec0bb 11602 {
5039610b 11603 tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
e26ec0bb
RS
11604 return fold_build2 (code, type, iref,
11605 build_int_cst (TREE_TYPE (iref), 0));
11606 }
11607 }
11608
11609 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
11610 of X. Similarly fold (X >> C) == 0 into X >= 0. */
11611 if (TREE_CODE (arg0) == RSHIFT_EXPR
11612 && integer_zerop (arg1)
11613 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11614 {
11615 tree arg00 = TREE_OPERAND (arg0, 0);
11616 tree arg01 = TREE_OPERAND (arg0, 1);
11617 tree itype = TREE_TYPE (arg00);
11618 if (TREE_INT_CST_HIGH (arg01) == 0
11619 && TREE_INT_CST_LOW (arg01)
11620 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
11621 {
11622 if (TYPE_UNSIGNED (itype))
11623 {
12753674 11624 itype = signed_type_for (itype);
e26ec0bb
RS
11625 arg00 = fold_convert (itype, arg00);
11626 }
11627 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
11628 type, arg00, build_int_cst (itype, 0));
11629 }
11630 }
11631
eb8dffe0
RS
11632 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
11633 if (integer_zerop (arg1)
11634 && TREE_CODE (arg0) == BIT_XOR_EXPR)
11635 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11636 TREE_OPERAND (arg0, 1));
11637
11638 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
11639 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11640 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11641 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11642 build_int_cst (TREE_TYPE (arg1), 0));
11643 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
11644 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11645 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11646 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11647 return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
11648 build_int_cst (TREE_TYPE (arg1), 0));
11649
11650 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
11651 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11652 && TREE_CODE (arg1) == INTEGER_CST
11653 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11654 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11655 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
11656 TREE_OPERAND (arg0, 1), arg1));
11657
5881ad5d
RS
11658 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
11659 (X & C) == 0 when C is a single bit. */
11660 if (TREE_CODE (arg0) == BIT_AND_EXPR
11661 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
11662 && integer_zerop (arg1)
11663 && integer_pow2p (TREE_OPERAND (arg0, 1)))
11664 {
11665 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11666 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
11667 TREE_OPERAND (arg0, 1));
11668 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
11669 type, tem, arg1);
11670 }
11671
11672 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
11673 constant C is a power of two, i.e. a single bit. */
11674 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11675 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11676 && integer_zerop (arg1)
11677 && integer_pow2p (TREE_OPERAND (arg0, 1))
11678 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11679 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
11680 {
11681 tree arg00 = TREE_OPERAND (arg0, 0);
11682 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11683 arg00, build_int_cst (TREE_TYPE (arg00), 0));
11684 }
11685
11686 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
11687 when is C is a power of two, i.e. a single bit. */
11688 if (TREE_CODE (arg0) == BIT_AND_EXPR
11689 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
11690 && integer_zerop (arg1)
11691 && integer_pow2p (TREE_OPERAND (arg0, 1))
11692 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11693 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
11694 {
11695 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
11696 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
11697 arg000, TREE_OPERAND (arg0, 1));
11698 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11699 tem, build_int_cst (TREE_TYPE (tem), 0));
11700 }
11701
e26ec0bb
RS
11702 if (integer_zerop (arg1)
11703 && tree_expr_nonzero_p (arg0))
11704 {
11705 tree res = constant_boolean_node (code==NE_EXPR, type);
11706 return omit_one_operand (type, res, arg0);
11707 }
c159ffe7
RS
11708
11709 /* Fold -X op -Y as X op Y, where op is eq/ne. */
11710 if (TREE_CODE (arg0) == NEGATE_EXPR
11711 && TREE_CODE (arg1) == NEGATE_EXPR)
11712 return fold_build2 (code, type,
11713 TREE_OPERAND (arg0, 0),
11714 TREE_OPERAND (arg1, 0));
11715
015e23f4
RS
11716 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
11717 if (TREE_CODE (arg0) == BIT_AND_EXPR
11718 && TREE_CODE (arg1) == BIT_AND_EXPR)
11719 {
11720 tree arg00 = TREE_OPERAND (arg0, 0);
11721 tree arg01 = TREE_OPERAND (arg0, 1);
11722 tree arg10 = TREE_OPERAND (arg1, 0);
11723 tree arg11 = TREE_OPERAND (arg1, 1);
11724 tree itype = TREE_TYPE (arg0);
11725
11726 if (operand_equal_p (arg01, arg11, 0))
11727 return fold_build2 (code, type,
11728 fold_build2 (BIT_AND_EXPR, itype,
11729 fold_build2 (BIT_XOR_EXPR, itype,
11730 arg00, arg10),
11731 arg01),
11732 build_int_cst (itype, 0));
11733
11734 if (operand_equal_p (arg01, arg10, 0))
11735 return fold_build2 (code, type,
11736 fold_build2 (BIT_AND_EXPR, itype,
11737 fold_build2 (BIT_XOR_EXPR, itype,
11738 arg00, arg11),
11739 arg01),
11740 build_int_cst (itype, 0));
11741
11742 if (operand_equal_p (arg00, arg11, 0))
11743 return fold_build2 (code, type,
11744 fold_build2 (BIT_AND_EXPR, itype,
11745 fold_build2 (BIT_XOR_EXPR, itype,
11746 arg01, arg10),
11747 arg00),
11748 build_int_cst (itype, 0));
11749
11750 if (operand_equal_p (arg00, arg10, 0))
11751 return fold_build2 (code, type,
11752 fold_build2 (BIT_AND_EXPR, itype,
11753 fold_build2 (BIT_XOR_EXPR, itype,
11754 arg01, arg11),
11755 arg00),
11756 build_int_cst (itype, 0));
11757 }
11758
cf06e5c1
RS
11759 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11760 && TREE_CODE (arg1) == BIT_XOR_EXPR)
11761 {
11762 tree arg00 = TREE_OPERAND (arg0, 0);
11763 tree arg01 = TREE_OPERAND (arg0, 1);
11764 tree arg10 = TREE_OPERAND (arg1, 0);
11765 tree arg11 = TREE_OPERAND (arg1, 1);
11766 tree itype = TREE_TYPE (arg0);
11767
11768 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
11769 operand_equal_p guarantees no side-effects so we don't need
11770 to use omit_one_operand on Z. */
11771 if (operand_equal_p (arg01, arg11, 0))
11772 return fold_build2 (code, type, arg00, arg10);
11773 if (operand_equal_p (arg01, arg10, 0))
11774 return fold_build2 (code, type, arg00, arg11);
11775 if (operand_equal_p (arg00, arg11, 0))
11776 return fold_build2 (code, type, arg01, arg10);
11777 if (operand_equal_p (arg00, arg10, 0))
11778 return fold_build2 (code, type, arg01, arg11);
11779
11780 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
11781 if (TREE_CODE (arg01) == INTEGER_CST
11782 && TREE_CODE (arg11) == INTEGER_CST)
11783 return fold_build2 (code, type,
11784 fold_build2 (BIT_XOR_EXPR, itype, arg00,
11785 fold_build2 (BIT_XOR_EXPR, itype,
11786 arg01, arg11)),
11787 arg10);
11788 }
23b9463b
RS
11789
11790 /* Attempt to simplify equality/inequality comparisons of complex
11791 values. Only lower the comparison if the result is known or
11792 can be simplified to a single scalar comparison. */
11793 if ((TREE_CODE (arg0) == COMPLEX_EXPR
11794 || TREE_CODE (arg0) == COMPLEX_CST)
11795 && (TREE_CODE (arg1) == COMPLEX_EXPR
11796 || TREE_CODE (arg1) == COMPLEX_CST))
11797 {
11798 tree real0, imag0, real1, imag1;
11799 tree rcond, icond;
11800
11801 if (TREE_CODE (arg0) == COMPLEX_EXPR)
11802 {
11803 real0 = TREE_OPERAND (arg0, 0);
11804 imag0 = TREE_OPERAND (arg0, 1);
11805 }
11806 else
11807 {
11808 real0 = TREE_REALPART (arg0);
11809 imag0 = TREE_IMAGPART (arg0);
11810 }
11811
11812 if (TREE_CODE (arg1) == COMPLEX_EXPR)
11813 {
11814 real1 = TREE_OPERAND (arg1, 0);
11815 imag1 = TREE_OPERAND (arg1, 1);
11816 }
11817 else
11818 {
11819 real1 = TREE_REALPART (arg1);
11820 imag1 = TREE_IMAGPART (arg1);
11821 }
11822
11823 rcond = fold_binary (code, type, real0, real1);
11824 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
11825 {
11826 if (integer_zerop (rcond))
11827 {
11828 if (code == EQ_EXPR)
11829 return omit_two_operands (type, boolean_false_node,
11830 imag0, imag1);
11831 return fold_build2 (NE_EXPR, type, imag0, imag1);
11832 }
11833 else
11834 {
11835 if (code == NE_EXPR)
11836 return omit_two_operands (type, boolean_true_node,
11837 imag0, imag1);
11838 return fold_build2 (EQ_EXPR, type, imag0, imag1);
11839 }
11840 }
11841
11842 icond = fold_binary (code, type, imag0, imag1);
11843 if (icond && TREE_CODE (icond) == INTEGER_CST)
11844 {
11845 if (integer_zerop (icond))
11846 {
11847 if (code == EQ_EXPR)
11848 return omit_two_operands (type, boolean_false_node,
11849 real0, real1);
11850 return fold_build2 (NE_EXPR, type, real0, real1);
11851 }
11852 else
11853 {
11854 if (code == NE_EXPR)
11855 return omit_two_operands (type, boolean_true_node,
11856 real0, real1);
11857 return fold_build2 (EQ_EXPR, type, real0, real1);
11858 }
11859 }
11860 }
11861
e26ec0bb
RS
11862 return NULL_TREE;
11863
11864 case LT_EXPR:
11865 case GT_EXPR:
11866 case LE_EXPR:
11867 case GE_EXPR:
11868 tem = fold_comparison (code, type, op0, op1);
11869 if (tem != NULL_TREE)
11870 return tem;
11871
11872 /* Transform comparisons of the form X +- C CMP X. */
11873 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11874 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11875 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11876 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
11877 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
eeef0e45 11878 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
e26ec0bb
RS
11879 {
11880 tree arg01 = TREE_OPERAND (arg0, 1);
11881 enum tree_code code0 = TREE_CODE (arg0);
11882 int is_positive;
11883
11884 if (TREE_CODE (arg01) == REAL_CST)
11885 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11886 else
11887 is_positive = tree_int_cst_sgn (arg01);
11888
11889 /* (X - c) > X becomes false. */
11890 if (code == GT_EXPR
11891 && ((code0 == MINUS_EXPR && is_positive >= 0)
11892 || (code0 == PLUS_EXPR && is_positive <= 0)))
6ac01510
ILT
11893 {
11894 if (TREE_CODE (arg01) == INTEGER_CST
11895 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11896 fold_overflow_warning (("assuming signed overflow does not "
11897 "occur when assuming that (X - c) > X "
11898 "is always false"),
11899 WARN_STRICT_OVERFLOW_ALL);
11900 return constant_boolean_node (0, type);
11901 }
e26ec0bb
RS
11902
11903 /* Likewise (X + c) < X becomes false. */
11904 if (code == LT_EXPR
11905 && ((code0 == PLUS_EXPR && is_positive >= 0)
11906 || (code0 == MINUS_EXPR && is_positive <= 0)))
6ac01510
ILT
11907 {
11908 if (TREE_CODE (arg01) == INTEGER_CST
11909 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11910 fold_overflow_warning (("assuming signed overflow does not "
11911 "occur when assuming that "
11912 "(X + c) < X is always false"),
11913 WARN_STRICT_OVERFLOW_ALL);
11914 return constant_boolean_node (0, type);
11915 }
e26ec0bb
RS
11916
11917 /* Convert (X - c) <= X to true. */
11918 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
11919 && code == LE_EXPR
0eeb03e6
JM
11920 && ((code0 == MINUS_EXPR && is_positive >= 0)
11921 || (code0 == PLUS_EXPR && is_positive <= 0)))
6ac01510
ILT
11922 {
11923 if (TREE_CODE (arg01) == INTEGER_CST
11924 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11925 fold_overflow_warning (("assuming signed overflow does not "
11926 "occur when assuming that "
11927 "(X - c) <= X is always true"),
11928 WARN_STRICT_OVERFLOW_ALL);
11929 return constant_boolean_node (1, type);
11930 }
0eeb03e6
JM
11931
11932 /* Convert (X + c) >= X to true. */
11933 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
11934 && code == GE_EXPR
11935 && ((code0 == PLUS_EXPR && is_positive >= 0)
11936 || (code0 == MINUS_EXPR && is_positive <= 0)))
6ac01510
ILT
11937 {
11938 if (TREE_CODE (arg01) == INTEGER_CST
11939 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11940 fold_overflow_warning (("assuming signed overflow does not "
11941 "occur when assuming that "
11942 "(X + c) >= X is always true"),
11943 WARN_STRICT_OVERFLOW_ALL);
11944 return constant_boolean_node (1, type);
11945 }
0eeb03e6
JM
11946
11947 if (TREE_CODE (arg01) == INTEGER_CST)
11948 {
11949 /* Convert X + c > X and X - c < X to true for integers. */
11950 if (code == GT_EXPR
11951 && ((code0 == PLUS_EXPR && is_positive > 0)
11952 || (code0 == MINUS_EXPR && is_positive < 0)))
6ac01510
ILT
11953 {
11954 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11955 fold_overflow_warning (("assuming signed overflow does "
11956 "not occur when assuming that "
11957 "(X + c) > X is always true"),
11958 WARN_STRICT_OVERFLOW_ALL);
11959 return constant_boolean_node (1, type);
11960 }
0eeb03e6
JM
11961
11962 if (code == LT_EXPR
11963 && ((code0 == MINUS_EXPR && is_positive > 0)
11964 || (code0 == PLUS_EXPR && is_positive < 0)))
6ac01510
ILT
11965 {
11966 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11967 fold_overflow_warning (("assuming signed overflow does "
11968 "not occur when assuming that "
11969 "(X - c) < X is always true"),
11970 WARN_STRICT_OVERFLOW_ALL);
11971 return constant_boolean_node (1, type);
11972 }
0eeb03e6
JM
11973
11974 /* Convert X + c <= X and X - c >= X to false for integers. */
11975 if (code == LE_EXPR
11976 && ((code0 == PLUS_EXPR && is_positive > 0)
11977 || (code0 == MINUS_EXPR && is_positive < 0)))
6ac01510
ILT
11978 {
11979 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11980 fold_overflow_warning (("assuming signed overflow does "
11981 "not occur when assuming that "
11982 "(X + c) <= X is always false"),
11983 WARN_STRICT_OVERFLOW_ALL);
11984 return constant_boolean_node (0, type);
11985 }
0eeb03e6
JM
11986
11987 if (code == GE_EXPR
11988 && ((code0 == MINUS_EXPR && is_positive > 0)
11989 || (code0 == PLUS_EXPR && is_positive < 0)))
6ac01510
ILT
11990 {
11991 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11992 fold_overflow_warning (("assuming signed overflow does "
11993 "not occur when assuming that "
f870ab63 11994 "(X - c) >= X is always false"),
6ac01510
ILT
11995 WARN_STRICT_OVERFLOW_ALL);
11996 return constant_boolean_node (0, type);
11997 }
0eeb03e6
JM
11998 }
11999 }
12000
0aee4751
KH
12001 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
12002 This transformation affects the cases which are handled in later
12003 optimizations involving comparisons with non-negative constants. */
12004 if (TREE_CODE (arg1) == INTEGER_CST
12005 && TREE_CODE (arg0) != INTEGER_CST
12006 && tree_int_cst_sgn (arg1) > 0)
12007 {
e26ec0bb 12008 if (code == GE_EXPR)
0aee4751 12009 {
548e34cd
RG
12010 arg1 = const_binop (MINUS_EXPR, arg1,
12011 build_int_cst (TREE_TYPE (arg1), 1), 0);
12012 return fold_build2 (GT_EXPR, type, arg0,
12013 fold_convert (TREE_TYPE (arg0), arg1));
e26ec0bb
RS
12014 }
12015 if (code == LT_EXPR)
12016 {
548e34cd
RG
12017 arg1 = const_binop (MINUS_EXPR, arg1,
12018 build_int_cst (TREE_TYPE (arg1), 1), 0);
12019 return fold_build2 (LE_EXPR, type, arg0,
12020 fold_convert (TREE_TYPE (arg0), arg1));
0aee4751
KH
12021 }
12022 }
12023
12024 /* Comparisons with the highest or lowest possible integer of
f0dbdfbb 12025 the specified precision will have known values. */
0aee4751 12026 {
f0dbdfbb
EB
12027 tree arg1_type = TREE_TYPE (arg1);
12028 unsigned int width = TYPE_PRECISION (arg1_type);
0aee4751
KH
12029
12030 if (TREE_CODE (arg1) == INTEGER_CST
455f14dd 12031 && !TREE_OVERFLOW (arg1)
0aee4751 12032 && width <= 2 * HOST_BITS_PER_WIDE_INT
f0dbdfbb 12033 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
0aee4751
KH
12034 {
12035 HOST_WIDE_INT signed_max_hi;
12036 unsigned HOST_WIDE_INT signed_max_lo;
12037 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12038
12039 if (width <= HOST_BITS_PER_WIDE_INT)
12040 {
12041 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12042 - 1;
12043 signed_max_hi = 0;
12044 max_hi = 0;
12045
f0dbdfbb 12046 if (TYPE_UNSIGNED (arg1_type))
0aee4751
KH
12047 {
12048 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12049 min_lo = 0;
12050 min_hi = 0;
12051 }
12052 else
12053 {
12054 max_lo = signed_max_lo;
12055 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12056 min_hi = -1;
12057 }
12058 }
12059 else
12060 {
12061 width -= HOST_BITS_PER_WIDE_INT;
12062 signed_max_lo = -1;
12063 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12064 - 1;
12065 max_lo = -1;
12066 min_lo = 0;
12067
f0dbdfbb 12068 if (TYPE_UNSIGNED (arg1_type))
0aee4751
KH
12069 {
12070 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12071 min_hi = 0;
12072 }
12073 else
12074 {
12075 max_hi = signed_max_hi;
12076 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12077 }
12078 }
12079
12080 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12081 && TREE_INT_CST_LOW (arg1) == max_lo)
12082 switch (code)
12083 {
12084 case GT_EXPR:
12085 return omit_one_operand (type, integer_zero_node, arg0);
12086
12087 case GE_EXPR:
86122f72 12088 return fold_build2 (EQ_EXPR, type, op0, op1);
0aee4751
KH
12089
12090 case LE_EXPR:
12091 return omit_one_operand (type, integer_one_node, arg0);
12092
12093 case LT_EXPR:
86122f72 12094 return fold_build2 (NE_EXPR, type, op0, op1);
0aee4751
KH
12095
12096 /* The GE_EXPR and LT_EXPR cases above are not normally
12097 reached because of previous transformations. */
12098
12099 default:
12100 break;
12101 }
12102 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12103 == max_hi
12104 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12105 switch (code)
12106 {
12107 case GT_EXPR:
000d8d44
RS
12108 arg1 = const_binop (PLUS_EXPR, arg1,
12109 build_int_cst (TREE_TYPE (arg1), 1), 0);
86122f72
JJ
12110 return fold_build2 (EQ_EXPR, type,
12111 fold_convert (TREE_TYPE (arg1), arg0),
12112 arg1);
0aee4751 12113 case LE_EXPR:
000d8d44
RS
12114 arg1 = const_binop (PLUS_EXPR, arg1,
12115 build_int_cst (TREE_TYPE (arg1), 1), 0);
86122f72
JJ
12116 return fold_build2 (NE_EXPR, type,
12117 fold_convert (TREE_TYPE (arg1), arg0),
12118 arg1);
0aee4751
KH
12119 default:
12120 break;
12121 }
12122 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12123 == min_hi
12124 && TREE_INT_CST_LOW (arg1) == min_lo)
12125 switch (code)
12126 {
12127 case LT_EXPR:
12128 return omit_one_operand (type, integer_zero_node, arg0);
12129
12130 case LE_EXPR:
86122f72 12131 return fold_build2 (EQ_EXPR, type, op0, op1);
0aee4751
KH
12132
12133 case GE_EXPR:
12134 return omit_one_operand (type, integer_one_node, arg0);
12135
12136 case GT_EXPR:
3f1dfb41 12137 return fold_build2 (NE_EXPR, type, op0, op1);
0aee4751
KH
12138
12139 default:
12140 break;
12141 }
12142 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12143 == min_hi
12144 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12145 switch (code)
12146 {
12147 case GE_EXPR:
12148 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
86122f72
JJ
12149 return fold_build2 (NE_EXPR, type,
12150 fold_convert (TREE_TYPE (arg1), arg0),
12151 arg1);
0aee4751
KH
12152 case LT_EXPR:
12153 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
86122f72
JJ
12154 return fold_build2 (EQ_EXPR, type,
12155 fold_convert (TREE_TYPE (arg1), arg0),
12156 arg1);
0aee4751
KH
12157 default:
12158 break;
12159 }
12160
5cdc4a26 12161 else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
0aee4751 12162 && TREE_INT_CST_LOW (arg1) == signed_max_lo
f0dbdfbb
EB
12163 && TYPE_UNSIGNED (arg1_type)
12164 /* We will flip the signedness of the comparison operator
12165 associated with the mode of arg1, so the sign bit is
12166 specified by this mode. Check that arg1 is the signed
12167 max associated with this sign bit. */
12168 && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
0aee4751 12169 /* signed_type does not work on pointer types. */
f0dbdfbb 12170 && INTEGRAL_TYPE_P (arg1_type))
0aee4751
KH
12171 {
12172 /* The following case also applies to X < signed_max+1
12173 and X >= signed_max+1 because previous transformations. */
12174 if (code == LE_EXPR || code == GT_EXPR)
12175 {
86122f72 12176 tree st;
12753674 12177 st = signed_type_for (TREE_TYPE (arg1));
86122f72
JJ
12178 return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
12179 type, fold_convert (st, arg0),
12180 build_int_cst (st, 0));
0aee4751
KH
12181 }
12182 }
12183 }
12184 }
12185
0aee4751
KH
12186 /* If we are comparing an ABS_EXPR with a constant, we can
12187 convert all the cases into explicit comparisons, but they may
12188 well not be faster than doing the ABS and one comparison.
12189 But ABS (X) <= C is a range comparison, which becomes a subtraction
12190 and a comparison, and is probably faster. */
e26ec0bb
RS
12191 if (code == LE_EXPR
12192 && TREE_CODE (arg1) == INTEGER_CST
12193 && TREE_CODE (arg0) == ABS_EXPR
12194 && ! TREE_SIDE_EFFECTS (arg0)
12195 && (0 != (tem = negate_expr (arg1)))
12196 && TREE_CODE (tem) == INTEGER_CST
455f14dd 12197 && !TREE_OVERFLOW (tem))
7f20a5b7
KH
12198 return fold_build2 (TRUTH_ANDIF_EXPR, type,
12199 build2 (GE_EXPR, type,
12200 TREE_OPERAND (arg0, 0), tem),
12201 build2 (LE_EXPR, type,
12202 TREE_OPERAND (arg0, 0), arg1));
0aee4751
KH
12203
12204 /* Convert ABS_EXPR<x> >= 0 to true. */
6ac01510 12205 strict_overflow_p = false;
e26ec0bb 12206 if (code == GE_EXPR
e26ec0bb
RS
12207 && (integer_zerop (arg1)
12208 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
6ac01510
ILT
12209 && real_zerop (arg1)))
12210 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12211 {
12212 if (strict_overflow_p)
12213 fold_overflow_warning (("assuming signed overflow does not occur "
12214 "when simplifying comparison of "
12215 "absolute value and zero"),
12216 WARN_STRICT_OVERFLOW_CONDITIONAL);
12217 return omit_one_operand (type, integer_one_node, arg0);
12218 }
0aee4751
KH
12219
12220 /* Convert ABS_EXPR<x> < 0 to false. */
6ac01510 12221 strict_overflow_p = false;
e26ec0bb 12222 if (code == LT_EXPR
6ac01510
ILT
12223 && (integer_zerop (arg1) || real_zerop (arg1))
12224 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12225 {
12226 if (strict_overflow_p)
12227 fold_overflow_warning (("assuming signed overflow does not occur "
12228 "when simplifying comparison of "
12229 "absolute value and zero"),
12230 WARN_STRICT_OVERFLOW_CONDITIONAL);
12231 return omit_one_operand (type, integer_zero_node, arg0);
12232 }
0aee4751 12233
0aee4751
KH
12234 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12235 and similarly for >= into !=. */
12236 if ((code == LT_EXPR || code == GE_EXPR)
12237 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12238 && TREE_CODE (arg1) == LSHIFT_EXPR
12239 && integer_onep (TREE_OPERAND (arg1, 0)))
12240 return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12241 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12242 TREE_OPERAND (arg1, 1)),
57decb7e 12243 build_int_cst (TREE_TYPE (arg0), 0));
0aee4751 12244
e26ec0bb
RS
12245 if ((code == LT_EXPR || code == GE_EXPR)
12246 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12247 && (TREE_CODE (arg1) == NOP_EXPR
12248 || TREE_CODE (arg1) == CONVERT_EXPR)
12249 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12250 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
0aee4751
KH
12251 return
12252 build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12253 fold_convert (TREE_TYPE (arg0),
12254 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12255 TREE_OPERAND (TREE_OPERAND (arg1, 0),
12256 1))),
57decb7e 12257 build_int_cst (TREE_TYPE (arg0), 0));
0aee4751 12258
e26ec0bb 12259 return NULL_TREE;
0aee4751
KH
12260
12261 case UNORDERED_EXPR:
12262 case ORDERED_EXPR:
12263 case UNLT_EXPR:
12264 case UNLE_EXPR:
12265 case UNGT_EXPR:
12266 case UNGE_EXPR:
12267 case UNEQ_EXPR:
12268 case LTGT_EXPR:
12269 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
12270 {
12271 t1 = fold_relational_const (code, type, arg0, arg1);
12272 if (t1 != NULL_TREE)
12273 return t1;
12274 }
12275
12276 /* If the first operand is NaN, the result is constant. */
12277 if (TREE_CODE (arg0) == REAL_CST
12278 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
12279 && (code != LTGT_EXPR || ! flag_trapping_math))
12280 {
12281 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12282 ? integer_zero_node
12283 : integer_one_node;
12284 return omit_one_operand (type, t1, arg1);
12285 }
12286
12287 /* If the second operand is NaN, the result is constant. */
12288 if (TREE_CODE (arg1) == REAL_CST
12289 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
12290 && (code != LTGT_EXPR || ! flag_trapping_math))
12291 {
12292 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12293 ? integer_zero_node
12294 : integer_one_node;
12295 return omit_one_operand (type, t1, arg0);
12296 }
12297
12298 /* Simplify unordered comparison of something with itself. */
12299 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
12300 && operand_equal_p (arg0, arg1, 0))
12301 return constant_boolean_node (1, type);
12302
12303 if (code == LTGT_EXPR
12304 && !flag_trapping_math
12305 && operand_equal_p (arg0, arg1, 0))
12306 return constant_boolean_node (0, type);
12307
12308 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12309 {
12310 tree targ0 = strip_float_extensions (arg0);
12311 tree targ1 = strip_float_extensions (arg1);
12312 tree newtype = TREE_TYPE (targ0);
12313
12314 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
12315 newtype = TREE_TYPE (targ1);
12316
12317 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
7f20a5b7
KH
12318 return fold_build2 (code, type, fold_convert (newtype, targ0),
12319 fold_convert (newtype, targ1));
0aee4751
KH
12320 }
12321
62ab45cc 12322 return NULL_TREE;
0aee4751
KH
12323
12324 case COMPOUND_EXPR:
12325 /* When pedantic, a compound expression can be neither an lvalue
12326 nor an integer constant expression. */
12327 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
62ab45cc 12328 return NULL_TREE;
0aee4751
KH
12329 /* Don't let (0, 0) be null pointer constant. */
12330 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
12331 : fold_convert (type, arg1);
12332 return pedantic_non_lvalue (tem);
12333
12334 case COMPLEX_EXPR:
fd6c76f4
RS
12335 if ((TREE_CODE (arg0) == REAL_CST
12336 && TREE_CODE (arg1) == REAL_CST)
12337 || (TREE_CODE (arg0) == INTEGER_CST
12338 && TREE_CODE (arg1) == INTEGER_CST))
0aee4751 12339 return build_complex (type, arg0, arg1);
62ab45cc 12340 return NULL_TREE;
0aee4751 12341
cb4819f0
KH
12342 case ASSERT_EXPR:
12343 /* An ASSERT_EXPR should never be passed to fold_binary. */
12344 gcc_unreachable ();
12345
0aee4751 12346 default:
62ab45cc 12347 return NULL_TREE;
0aee4751
KH
12348 } /* switch (code) */
12349}
12350
8c900457
GL
12351/* Callback for walk_tree, looking for LABEL_EXPR.
12352 Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
12353 Do not check the sub-tree of GOTO_EXPR. */
12354
12355static tree
12356contains_label_1 (tree *tp,
12357 int *walk_subtrees,
12358 void *data ATTRIBUTE_UNUSED)
12359{
12360 switch (TREE_CODE (*tp))
12361 {
12362 case LABEL_EXPR:
12363 return *tp;
12364 case GOTO_EXPR:
12365 *walk_subtrees = 0;
12366 /* no break */
12367 default:
12368 return NULL_TREE;
12369 }
12370}
12371
9d6aab7e 12372/* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
8c900457
GL
12373 accessible from outside the sub-tree. Returns NULL_TREE if no
12374 addressable label is found. */
12375
12376static bool
12377contains_label_p (tree st)
12378{
12379 return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
12380}
12381
7cf57259
KH
12382/* Fold a ternary expression of code CODE and type TYPE with operands
12383 OP0, OP1, and OP2. Return the folded expression if folding is
12384 successful. Otherwise, return NULL_TREE. */
9bdae6af 12385
721425b6 12386tree
7cf57259 12387fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
9bdae6af 12388{
9bdae6af
KH
12389 tree tem;
12390 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
9bdae6af 12391 enum tree_code_class kind = TREE_CODE_CLASS (code);
9bdae6af
KH
12392
12393 gcc_assert (IS_EXPR_CODE_CLASS (kind)
12394 && TREE_CODE_LENGTH (code) == 3);
12395
3ea2c264
KH
12396 /* Strip any conversions that don't change the mode. This is safe
12397 for every expression, except for a comparison expression because
12398 its signedness is derived from its operands. So, in the latter
12399 case, only strip conversions that don't change the signedness.
9bdae6af 12400
3ea2c264
KH
12401 Note that this is done as an internal manipulation within the
12402 constant folder, in order to find the simplest representation of
12403 the arguments so that their form can be studied. In any cases,
12404 the appropriate type conversions should be put back in the tree
12405 that will get out of the constant folder. */
12406 if (op0)
12407 {
12408 arg0 = op0;
12409 STRIP_NOPS (arg0);
12410 }
9bdae6af 12411
3ea2c264
KH
12412 if (op1)
12413 {
12414 arg1 = op1;
12415 STRIP_NOPS (arg1);
9bdae6af
KH
12416 }
12417
12418 switch (code)
12419 {
12420 case COMPONENT_REF:
12421 if (TREE_CODE (arg0) == CONSTRUCTOR
12422 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12423 {
4038c495
GB
12424 unsigned HOST_WIDE_INT idx;
12425 tree field, value;
12426 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12427 if (field == arg1)
12428 return value;
9bdae6af 12429 }
62ab45cc 12430 return NULL_TREE;
9bdae6af
KH
12431
12432 case COND_EXPR:
12433 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12434 so all simple results must be passed through pedantic_non_lvalue. */
12435 if (TREE_CODE (arg0) == INTEGER_CST)
12436 {
8c900457 12437 tree unused_op = integer_zerop (arg0) ? op1 : op2;
3ea2c264 12438 tem = integer_zerop (arg0) ? op2 : op1;
9bdae6af
KH
12439 /* Only optimize constant conditions when the selected branch
12440 has the same type as the COND_EXPR. This avoids optimizing
8c900457
GL
12441 away "c ? x : throw", where the throw has a void type.
12442 Avoid throwing away that operand which contains label. */
12443 if ((!TREE_SIDE_EFFECTS (unused_op)
12444 || !contains_label_p (unused_op))
12445 && (! VOID_TYPE_P (TREE_TYPE (tem))
12446 || VOID_TYPE_P (type)))
9bdae6af 12447 return pedantic_non_lvalue (tem);
62ab45cc 12448 return NULL_TREE;
9bdae6af 12449 }
3ea2c264 12450 if (operand_equal_p (arg1, op2, 0))
9bdae6af
KH
12451 return pedantic_omit_one_operand (type, arg1, arg0);
12452
12453 /* If we have A op B ? A : C, we may be able to convert this to a
12454 simpler expression, depending on the operation and the values
12455 of B and C. Signed zeros prevent all of these transformations,
12456 for reasons given above each one.
12457
12458 Also try swapping the arguments and inverting the conditional. */
12459 if (COMPARISON_CLASS_P (arg0)
12460 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12461 arg1, TREE_OPERAND (arg0, 1))
12462 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
12463 {
3ea2c264 12464 tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
9bdae6af
KH
12465 if (tem)
12466 return tem;
12467 }
12468
12469 if (COMPARISON_CLASS_P (arg0)
12470 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
3ea2c264 12471 op2,
9bdae6af 12472 TREE_OPERAND (arg0, 1))
3ea2c264 12473 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
9bdae6af 12474 {
d817ed3b
RG
12475 tem = fold_truth_not_expr (arg0);
12476 if (tem && COMPARISON_CLASS_P (tem))
9bdae6af 12477 {
3ea2c264 12478 tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
9bdae6af
KH
12479 if (tem)
12480 return tem;
12481 }
12482 }
12483
12484 /* If the second operand is simpler than the third, swap them
12485 since that produces better jump optimization results. */
3dac16bd
RG
12486 if (truth_value_p (TREE_CODE (arg0))
12487 && tree_swap_operands_p (op1, op2, false))
9bdae6af
KH
12488 {
12489 /* See if this can be inverted. If it can't, possibly because
12490 it was a floating-point inequality comparison, don't do
12491 anything. */
d817ed3b
RG
12492 tem = fold_truth_not_expr (arg0);
12493 if (tem)
7f20a5b7 12494 return fold_build3 (code, type, tem, op2, op1);
9bdae6af
KH
12495 }
12496
12497 /* Convert A ? 1 : 0 to simply A. */
3ea2c264
KH
12498 if (integer_onep (op1)
12499 && integer_zerop (op2)
12500 /* If we try to convert OP0 to our type, the
9bdae6af
KH
12501 call to fold will try to move the conversion inside
12502 a COND, which will recurse. In that case, the COND_EXPR
12503 is probably the best choice, so leave it alone. */
12504 && type == TREE_TYPE (arg0))
12505 return pedantic_non_lvalue (arg0);
12506
12507 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12508 over COND_EXPR in cases such as floating point comparisons. */
3ea2c264
KH
12509 if (integer_zerop (op1)
12510 && integer_onep (op2)
9bdae6af
KH
12511 && truth_value_p (TREE_CODE (arg0)))
12512 return pedantic_non_lvalue (fold_convert (type,
12513 invert_truthvalue (arg0)));
12514
12515 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12516 if (TREE_CODE (arg0) == LT_EXPR
789e604d
JJ
12517 && integer_zerop (TREE_OPERAND (arg0, 1))
12518 && integer_zerop (op2)
12519 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12520 {
12521 /* sign_bit_p only checks ARG1 bits within A's precision.
12522 If <sign bit of A> has wider type than A, bits outside
12523 of A's precision in <sign bit of A> need to be checked.
12524 If they are all 0, this optimization needs to be done
12525 in unsigned A's type, if they are all 1 in signed A's type,
12526 otherwise this can't be done. */
12527 if (TYPE_PRECISION (TREE_TYPE (tem))
12528 < TYPE_PRECISION (TREE_TYPE (arg1))
12529 && TYPE_PRECISION (TREE_TYPE (tem))
12530 < TYPE_PRECISION (type))
12531 {
12532 unsigned HOST_WIDE_INT mask_lo;
12533 HOST_WIDE_INT mask_hi;
12534 int inner_width, outer_width;
12535 tree tem_type;
12536
12537 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12538 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12539 if (outer_width > TYPE_PRECISION (type))
12540 outer_width = TYPE_PRECISION (type);
12541
12542 if (outer_width > HOST_BITS_PER_WIDE_INT)
12543 {
12544 mask_hi = ((unsigned HOST_WIDE_INT) -1
12545 >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
12546 mask_lo = -1;
12547 }
12548 else
12549 {
12550 mask_hi = 0;
12551 mask_lo = ((unsigned HOST_WIDE_INT) -1
12552 >> (HOST_BITS_PER_WIDE_INT - outer_width));
12553 }
12554 if (inner_width > HOST_BITS_PER_WIDE_INT)
12555 {
12556 mask_hi &= ~((unsigned HOST_WIDE_INT) -1
12557 >> (HOST_BITS_PER_WIDE_INT - inner_width));
12558 mask_lo = 0;
12559 }
12560 else
12561 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
12562 >> (HOST_BITS_PER_WIDE_INT - inner_width));
12563
12564 if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
12565 && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
12566 {
12753674 12567 tem_type = signed_type_for (TREE_TYPE (tem));
789e604d
JJ
12568 tem = fold_convert (tem_type, tem);
12569 }
12570 else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
12571 && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
12572 {
ca5ba2a3 12573 tem_type = unsigned_type_for (TREE_TYPE (tem));
789e604d
JJ
12574 tem = fold_convert (tem_type, tem);
12575 }
12576 else
12577 tem = NULL;
12578 }
12579
12580 if (tem)
12581 return fold_convert (type,
12582 fold_build2 (BIT_AND_EXPR,
12583 TREE_TYPE (tem), tem,
12584 fold_convert (TREE_TYPE (tem),
12585 arg1)));
12586 }
9bdae6af
KH
12587
12588 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
12589 already handled above. */
12590 if (TREE_CODE (arg0) == BIT_AND_EXPR
12591 && integer_onep (TREE_OPERAND (arg0, 1))
3ea2c264 12592 && integer_zerop (op2)
9bdae6af
KH
12593 && integer_pow2p (arg1))
12594 {
12595 tree tem = TREE_OPERAND (arg0, 0);
12596 STRIP_NOPS (tem);
12597 if (TREE_CODE (tem) == RSHIFT_EXPR
12598 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
12599 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
12600 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
7f20a5b7
KH
12601 return fold_build2 (BIT_AND_EXPR, type,
12602 TREE_OPERAND (tem, 0), arg1);
9bdae6af
KH
12603 }
12604
12605 /* A & N ? N : 0 is simply A & N if N is a power of two. This
12606 is probably obsolete because the first operand should be a
12607 truth value (that's why we have the two cases above), but let's
12608 leave it in until we can confirm this for all front-ends. */
3ea2c264 12609 if (integer_zerop (op2)
9bdae6af
KH
12610 && TREE_CODE (arg0) == NE_EXPR
12611 && integer_zerop (TREE_OPERAND (arg0, 1))
12612 && integer_pow2p (arg1)
12613 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12614 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12615 arg1, OEP_ONLY_CONST))
12616 return pedantic_non_lvalue (fold_convert (type,
12617 TREE_OPERAND (arg0, 0)));
12618
12619 /* Convert A ? B : 0 into A && B if A and B are truth values. */
3ea2c264 12620 if (integer_zerop (op2)
9bdae6af
KH
12621 && truth_value_p (TREE_CODE (arg0))
12622 && truth_value_p (TREE_CODE (arg1)))
726ac11e
RS
12623 return fold_build2 (TRUTH_ANDIF_EXPR, type,
12624 fold_convert (type, arg0),
12625 arg1);
9bdae6af
KH
12626
12627 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
3ea2c264 12628 if (integer_onep (op2)
9bdae6af
KH
12629 && truth_value_p (TREE_CODE (arg0))
12630 && truth_value_p (TREE_CODE (arg1)))
12631 {
12632 /* Only perform transformation if ARG0 is easily inverted. */
d817ed3b
RG
12633 tem = fold_truth_not_expr (arg0);
12634 if (tem)
726ac11e
RS
12635 return fold_build2 (TRUTH_ORIF_EXPR, type,
12636 fold_convert (type, tem),
12637 arg1);
9bdae6af
KH
12638 }
12639
12640 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
12641 if (integer_zerop (arg1)
12642 && truth_value_p (TREE_CODE (arg0))
3ea2c264 12643 && truth_value_p (TREE_CODE (op2)))
9bdae6af
KH
12644 {
12645 /* Only perform transformation if ARG0 is easily inverted. */
d817ed3b
RG
12646 tem = fold_truth_not_expr (arg0);
12647 if (tem)
726ac11e
RS
12648 return fold_build2 (TRUTH_ANDIF_EXPR, type,
12649 fold_convert (type, tem),
12650 op2);
9bdae6af
KH
12651 }
12652
12653 /* Convert A ? 1 : B into A || B if A and B are truth values. */
12654 if (integer_onep (arg1)
12655 && truth_value_p (TREE_CODE (arg0))
3ea2c264 12656 && truth_value_p (TREE_CODE (op2)))
726ac11e
RS
12657 return fold_build2 (TRUTH_ORIF_EXPR, type,
12658 fold_convert (type, arg0),
12659 op2);
9bdae6af 12660
62ab45cc 12661 return NULL_TREE;
9bdae6af
KH
12662
12663 case CALL_EXPR:
5039610b
SL
12664 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
12665 of fold_ternary on them. */
12666 gcc_unreachable ();
9bdae6af 12667
dcd25113 12668 case BIT_FIELD_REF:
5773afc5
DN
12669 if ((TREE_CODE (arg0) == VECTOR_CST
12670 || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
dcd25113
JJ
12671 && type == TREE_TYPE (TREE_TYPE (arg0))
12672 && host_integerp (arg1, 1)
12673 && host_integerp (op2, 1))
12674 {
12675 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
12676 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
12677
12678 if (width != 0
12679 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
12680 && (idx % width) == 0
12681 && (idx = idx / width)
12682 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
12683 {
5773afc5
DN
12684 tree elements = NULL_TREE;
12685
12686 if (TREE_CODE (arg0) == VECTOR_CST)
12687 elements = TREE_VECTOR_CST_ELTS (arg0);
12688 else
12689 {
12690 unsigned HOST_WIDE_INT idx;
12691 tree value;
12692
12693 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
12694 elements = tree_cons (NULL_TREE, value, elements);
12695 }
40182dbf 12696 while (idx-- > 0 && elements)
dcd25113 12697 elements = TREE_CHAIN (elements);
40182dbf
JJ
12698 if (elements)
12699 return TREE_VALUE (elements);
12700 else
12701 return fold_convert (type, integer_zero_node);
dcd25113
JJ
12702 }
12703 }
12704 return NULL_TREE;
12705
9bdae6af 12706 default:
62ab45cc 12707 return NULL_TREE;
9bdae6af
KH
12708 } /* switch (code) */
12709}
12710
6d716ca8
RS
12711/* Perform constant folding and related simplification of EXPR.
12712 The related simplifications include x*1 => x, x*0 => 0, etc.,
12713 and application of the associative law.
12714 NOP_EXPR conversions may be removed freely (as long as we
af5bdf6a 12715 are careful not to change the type of the overall expression).
6d716ca8
RS
12716 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
12717 but we can constant-fold them if they have constant operands. */
12718
5dfa45d0
JJ
12719#ifdef ENABLE_FOLD_CHECKING
12720# define fold(x) fold_1 (x)
12721static tree fold_1 (tree);
12722static
12723#endif
6d716ca8 12724tree
fa8db1f7 12725fold (tree expr)
6d716ca8 12726{
ea993805 12727 const tree t = expr;
b3694847 12728 enum tree_code code = TREE_CODE (t);
6615c446 12729 enum tree_code_class kind = TREE_CODE_CLASS (code);
62ab45cc 12730 tree tem;
6de9cd9a 12731
1796dff4 12732 /* Return right away if a constant. */
6615c446 12733 if (kind == tcc_constant)
1796dff4 12734 return t;
b6cc0a72 12735
5039610b
SL
12736 /* CALL_EXPR-like objects with variable numbers of operands are
12737 treated specially. */
12738 if (kind == tcc_vl_exp)
12739 {
12740 if (code == CALL_EXPR)
12741 {
12742 tem = fold_call_expr (expr, false);
12743 return tem ? tem : expr;
12744 }
12745 return expr;
12746 }
12747
07beea0d
AH
12748 if (IS_EXPR_CODE_CLASS (kind)
12749 || IS_GIMPLE_STMT_CODE_CLASS (kind))
659d8efa 12750 {
fbaa905c 12751 tree type = TREE_TYPE (t);
7cf57259 12752 tree op0, op1, op2;
fbaa905c 12753
659d8efa
KH
12754 switch (TREE_CODE_LENGTH (code))
12755 {
12756 case 1:
fbaa905c
KH
12757 op0 = TREE_OPERAND (t, 0);
12758 tem = fold_unary (code, type, op0);
62ab45cc 12759 return tem ? tem : expr;
0aee4751 12760 case 2:
fbaa905c
KH
12761 op0 = TREE_OPERAND (t, 0);
12762 op1 = TREE_OPERAND (t, 1);
12763 tem = fold_binary (code, type, op0, op1);
62ab45cc 12764 return tem ? tem : expr;
9bdae6af 12765 case 3:
7cf57259
KH
12766 op0 = TREE_OPERAND (t, 0);
12767 op1 = TREE_OPERAND (t, 1);
12768 op2 = TREE_OPERAND (t, 2);
12769 tem = fold_ternary (code, type, op0, op1, op2);
62ab45cc 12770 return tem ? tem : expr;
659d8efa
KH
12771 default:
12772 break;
12773 }
12774 }
12775
6d716ca8
RS
12776 switch (code)
12777 {
6d716ca8
RS
12778 case CONST_DECL:
12779 return fold (DECL_INITIAL (t));
12780
6d716ca8
RS
12781 default:
12782 return t;
12783 } /* switch (code) */
12784}
39dfb55a 12785
5dfa45d0
JJ
12786#ifdef ENABLE_FOLD_CHECKING
12787#undef fold
12788
12789static void fold_checksum_tree (tree, struct md5_ctx *, htab_t);
12790static void fold_check_failed (tree, tree);
12791void print_fold_checksum (tree);
12792
12793/* When --enable-checking=fold, compute a digest of expr before
12794 and after actual fold call to see if fold did not accidentally
12795 change original expr. */
12796
12797tree
12798fold (tree expr)
12799{
12800 tree ret;
12801 struct md5_ctx ctx;
12802 unsigned char checksum_before[16], checksum_after[16];
12803 htab_t ht;
12804
12805 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
12806 md5_init_ctx (&ctx);
12807 fold_checksum_tree (expr, &ctx, ht);
12808 md5_finish_ctx (&ctx, checksum_before);
12809 htab_empty (ht);
12810
12811 ret = fold_1 (expr);
12812
12813 md5_init_ctx (&ctx);
12814 fold_checksum_tree (expr, &ctx, ht);
12815 md5_finish_ctx (&ctx, checksum_after);
12816 htab_delete (ht);
12817
12818 if (memcmp (checksum_before, checksum_after, 16))
12819 fold_check_failed (expr, ret);
12820
12821 return ret;
12822}
12823
12824void
12825print_fold_checksum (tree expr)
12826{
12827 struct md5_ctx ctx;
12828 unsigned char checksum[16], cnt;
12829 htab_t ht;
12830
12831 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
12832 md5_init_ctx (&ctx);
12833 fold_checksum_tree (expr, &ctx, ht);
12834 md5_finish_ctx (&ctx, checksum);
12835 htab_delete (ht);
12836 for (cnt = 0; cnt < 16; ++cnt)
12837 fprintf (stderr, "%02x", checksum[cnt]);
12838 putc ('\n', stderr);
12839}
12840
12841static void
12842fold_check_failed (tree expr ATTRIBUTE_UNUSED, tree ret ATTRIBUTE_UNUSED)
12843{
12844 internal_error ("fold check: original tree changed by fold");
12845}
12846
12847static void
12848fold_checksum_tree (tree expr, struct md5_ctx *ctx, htab_t ht)
12849{
12850 void **slot;
12851 enum tree_code code;
3f7f53c7 12852 struct tree_function_decl buf;
5dfa45d0 12853 int i, len;
d763bb10
AP
12854
12855recursive_label:
5dfa45d0 12856
0bccc606 12857 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
46c5394b
DB
12858 <= sizeof (struct tree_function_decl))
12859 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
5dfa45d0
JJ
12860 if (expr == NULL)
12861 return;
12862 slot = htab_find_slot (ht, expr, INSERT);
12863 if (*slot != NULL)
12864 return;
12865 *slot = expr;
12866 code = TREE_CODE (expr);
6615c446
JO
12867 if (TREE_CODE_CLASS (code) == tcc_declaration
12868 && DECL_ASSEMBLER_NAME_SET_P (expr))
5dfa45d0
JJ
12869 {
12870 /* Allow DECL_ASSEMBLER_NAME to be modified. */
3f7f53c7
SE
12871 memcpy ((char *) &buf, expr, tree_size (expr));
12872 expr = (tree) &buf;
5dfa45d0
JJ
12873 SET_DECL_ASSEMBLER_NAME (expr, NULL);
12874 }
6615c446 12875 else if (TREE_CODE_CLASS (code) == tcc_type
b9193259 12876 && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)
d763bb10
AP
12877 || TYPE_CACHED_VALUES_P (expr)
12878 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)))
5dfa45d0 12879 {
b9193259 12880 /* Allow these fields to be modified. */
3f7f53c7
SE
12881 memcpy ((char *) &buf, expr, tree_size (expr));
12882 expr = (tree) &buf;
d763bb10 12883 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr) = 0;
5dfa45d0
JJ
12884 TYPE_POINTER_TO (expr) = NULL;
12885 TYPE_REFERENCE_TO (expr) = NULL;
0ebfd2c9
RS
12886 if (TYPE_CACHED_VALUES_P (expr))
12887 {
12888 TYPE_CACHED_VALUES_P (expr) = 0;
12889 TYPE_CACHED_VALUES (expr) = NULL;
12890 }
5dfa45d0
JJ
12891 }
12892 md5_process_bytes (expr, tree_size (expr), ctx);
12893 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
6615c446 12894 if (TREE_CODE_CLASS (code) != tcc_type
d763bb10 12895 && TREE_CODE_CLASS (code) != tcc_declaration
70826cbb
SP
12896 && code != TREE_LIST
12897 && code != SSA_NAME)
5dfa45d0 12898 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
5dfa45d0
JJ
12899 switch (TREE_CODE_CLASS (code))
12900 {
6615c446 12901 case tcc_constant:
5dfa45d0
JJ
12902 switch (code)
12903 {
12904 case STRING_CST:
12905 md5_process_bytes (TREE_STRING_POINTER (expr),
12906 TREE_STRING_LENGTH (expr), ctx);
12907 break;
12908 case COMPLEX_CST:
12909 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12910 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12911 break;
12912 case VECTOR_CST:
12913 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
12914 break;
12915 default:
12916 break;
12917 }
12918 break;
6615c446 12919 case tcc_exceptional:
5dfa45d0
JJ
12920 switch (code)
12921 {
12922 case TREE_LIST:
12923 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12924 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
d763bb10
AP
12925 expr = TREE_CHAIN (expr);
12926 goto recursive_label;
5dfa45d0
JJ
12927 break;
12928 case TREE_VEC:
12929 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12930 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12931 break;
12932 default:
12933 break;
12934 }
12935 break;
6615c446
JO
12936 case tcc_expression:
12937 case tcc_reference:
12938 case tcc_comparison:
12939 case tcc_unary:
12940 case tcc_binary:
12941 case tcc_statement:
5039610b
SL
12942 case tcc_vl_exp:
12943 len = TREE_OPERAND_LENGTH (expr);
5dfa45d0
JJ
12944 for (i = 0; i < len; ++i)
12945 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12946 break;
6615c446 12947 case tcc_declaration:
5dfa45d0
JJ
12948 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12949 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
3eb04608
DB
12950 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12951 {
12952 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12953 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12954 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12955 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12956 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12957 }
46c5394b
DB
12958 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
12959 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
12960
12961 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12962 {
12963 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12964 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12965 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
12966 }
5dfa45d0 12967 break;
6615c446 12968 case tcc_type:
a40de696
AP
12969 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12970 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
5dfa45d0
JJ
12971 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12972 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12973 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12974 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
a40de696
AP
12975 if (INTEGRAL_TYPE_P (expr)
12976 || SCALAR_FLOAT_TYPE_P (expr))
12977 {
12978 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12979 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12980 }
5dfa45d0 12981 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
b9193259
DJ
12982 if (TREE_CODE (expr) == RECORD_TYPE
12983 || TREE_CODE (expr) == UNION_TYPE
12984 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12985 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
5dfa45d0
JJ
12986 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12987 break;
12988 default:
12989 break;
12990 }
12991}
12992
f1b42630
AN
12993/* Helper function for outputting the checksum of a tree T. When
12994 debugging with gdb, you can "define mynext" to be "next" followed
12995 by "call debug_fold_checksum (op0)", then just trace down till the
12996 outputs differ. */
12997
12998void
12999debug_fold_checksum (tree t)
13000{
13001 int i;
13002 unsigned char checksum[16];
13003 struct md5_ctx ctx;
13004 htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13005
13006 md5_init_ctx (&ctx);
13007 fold_checksum_tree (t, &ctx, ht);
13008 md5_finish_ctx (&ctx, checksum);
13009 htab_empty (ht);
13010
13011 for (i = 0; i < 16; i++)
13012 fprintf (stderr, "%d ", checksum[i]);
13013
13014 fprintf (stderr, "\n");
13015}
13016
5dfa45d0
JJ
13017#endif
13018
ba199a53 13019/* Fold a unary tree expression with code CODE of type TYPE with an
830113fd 13020 operand OP0. Return a folded expression if successful. Otherwise,
ba199a53
KH
13021 return a tree expression with code CODE of type TYPE with an
13022 operand OP0. */
13023
13024tree
5808968e 13025fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
ba199a53 13026{
e2fe73f6
AP
13027 tree tem;
13028#ifdef ENABLE_FOLD_CHECKING
13029 unsigned char checksum_before[16], checksum_after[16];
13030 struct md5_ctx ctx;
13031 htab_t ht;
13032
13033 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13034 md5_init_ctx (&ctx);
13035 fold_checksum_tree (op0, &ctx, ht);
13036 md5_finish_ctx (&ctx, checksum_before);
13037 htab_empty (ht);
13038#endif
13039
13040 tem = fold_unary (code, type, op0);
13041 if (!tem)
5808968e 13042 tem = build1_stat (code, type, op0 PASS_MEM_STAT);
e2fe73f6
AP
13043
13044#ifdef ENABLE_FOLD_CHECKING
13045 md5_init_ctx (&ctx);
13046 fold_checksum_tree (op0, &ctx, ht);
13047 md5_finish_ctx (&ctx, checksum_after);
13048 htab_delete (ht);
ba199a53 13049
e2fe73f6
AP
13050 if (memcmp (checksum_before, checksum_after, 16))
13051 fold_check_failed (op0, tem);
13052#endif
13053 return tem;
ba199a53
KH
13054}
13055
13056/* Fold a binary tree expression with code CODE of type TYPE with
830113fd 13057 operands OP0 and OP1. Return a folded expression if successful.
ba199a53
KH
13058 Otherwise, return a tree expression with code CODE of type TYPE
13059 with operands OP0 and OP1. */
13060
13061tree
5808968e
AP
13062fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13063 MEM_STAT_DECL)
ba199a53 13064{
e2fe73f6
AP
13065 tree tem;
13066#ifdef ENABLE_FOLD_CHECKING
13067 unsigned char checksum_before_op0[16],
13068 checksum_before_op1[16],
13069 checksum_after_op0[16],
13070 checksum_after_op1[16];
13071 struct md5_ctx ctx;
13072 htab_t ht;
13073
13074 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13075 md5_init_ctx (&ctx);
13076 fold_checksum_tree (op0, &ctx, ht);
13077 md5_finish_ctx (&ctx, checksum_before_op0);
13078 htab_empty (ht);
13079
13080 md5_init_ctx (&ctx);
13081 fold_checksum_tree (op1, &ctx, ht);
13082 md5_finish_ctx (&ctx, checksum_before_op1);
13083 htab_empty (ht);
13084#endif
13085
13086 tem = fold_binary (code, type, op0, op1);
13087 if (!tem)
5808968e 13088 tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
e2fe73f6
AP
13089
13090#ifdef ENABLE_FOLD_CHECKING
13091 md5_init_ctx (&ctx);
13092 fold_checksum_tree (op0, &ctx, ht);
13093 md5_finish_ctx (&ctx, checksum_after_op0);
13094 htab_empty (ht);
13095
13096 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13097 fold_check_failed (op0, tem);
13098
13099 md5_init_ctx (&ctx);
13100 fold_checksum_tree (op1, &ctx, ht);
13101 md5_finish_ctx (&ctx, checksum_after_op1);
13102 htab_delete (ht);
ba199a53 13103
e2fe73f6
AP
13104 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13105 fold_check_failed (op1, tem);
13106#endif
13107 return tem;
ba199a53
KH
13108}
13109
13110/* Fold a ternary tree expression with code CODE of type TYPE with
830113fd 13111 operands OP0, OP1, and OP2. Return a folded expression if
ba199a53
KH
13112 successful. Otherwise, return a tree expression with code CODE of
13113 type TYPE with operands OP0, OP1, and OP2. */
13114
13115tree
5808968e
AP
13116fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
13117 MEM_STAT_DECL)
13118{
13119 tree tem;
e2fe73f6
AP
13120#ifdef ENABLE_FOLD_CHECKING
13121 unsigned char checksum_before_op0[16],
13122 checksum_before_op1[16],
13123 checksum_before_op2[16],
13124 checksum_after_op0[16],
13125 checksum_after_op1[16],
13126 checksum_after_op2[16];
13127 struct md5_ctx ctx;
13128 htab_t ht;
13129
13130 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13131 md5_init_ctx (&ctx);
13132 fold_checksum_tree (op0, &ctx, ht);
13133 md5_finish_ctx (&ctx, checksum_before_op0);
13134 htab_empty (ht);
ba199a53 13135
e2fe73f6
AP
13136 md5_init_ctx (&ctx);
13137 fold_checksum_tree (op1, &ctx, ht);
13138 md5_finish_ctx (&ctx, checksum_before_op1);
13139 htab_empty (ht);
13140
13141 md5_init_ctx (&ctx);
13142 fold_checksum_tree (op2, &ctx, ht);
13143 md5_finish_ctx (&ctx, checksum_before_op2);
13144 htab_empty (ht);
13145#endif
5039610b
SL
13146
13147 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
e2fe73f6
AP
13148 tem = fold_ternary (code, type, op0, op1, op2);
13149 if (!tem)
5808968e 13150 tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
e2fe73f6
AP
13151
13152#ifdef ENABLE_FOLD_CHECKING
13153 md5_init_ctx (&ctx);
13154 fold_checksum_tree (op0, &ctx, ht);
13155 md5_finish_ctx (&ctx, checksum_after_op0);
13156 htab_empty (ht);
13157
13158 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13159 fold_check_failed (op0, tem);
13160
13161 md5_init_ctx (&ctx);
13162 fold_checksum_tree (op1, &ctx, ht);
13163 md5_finish_ctx (&ctx, checksum_after_op1);
13164 htab_empty (ht);
13165
13166 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13167 fold_check_failed (op1, tem);
13168
13169 md5_init_ctx (&ctx);
13170 fold_checksum_tree (op2, &ctx, ht);
13171 md5_finish_ctx (&ctx, checksum_after_op2);
13172 htab_delete (ht);
13173
13174 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13175 fold_check_failed (op2, tem);
13176#endif
13177 return tem;
ba199a53
KH
13178}
13179
94a0dd7b
SL
13180/* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13181 arguments in ARGARRAY, and a null static chain.
5039610b 13182 Return a folded expression if successful. Otherwise, return a CALL_EXPR
94a0dd7b 13183 of type TYPE from the given operands as constructed by build_call_array. */
5039610b
SL
13184
13185tree
94a0dd7b 13186fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
5039610b
SL
13187{
13188 tree tem;
13189#ifdef ENABLE_FOLD_CHECKING
13190 unsigned char checksum_before_fn[16],
13191 checksum_before_arglist[16],
13192 checksum_after_fn[16],
13193 checksum_after_arglist[16];
13194 struct md5_ctx ctx;
13195 htab_t ht;
94a0dd7b 13196 int i;
5039610b
SL
13197
13198 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13199 md5_init_ctx (&ctx);
13200 fold_checksum_tree (fn, &ctx, ht);
13201 md5_finish_ctx (&ctx, checksum_before_fn);
13202 htab_empty (ht);
13203
13204 md5_init_ctx (&ctx);
94a0dd7b
SL
13205 for (i = 0; i < nargs; i++)
13206 fold_checksum_tree (argarray[i], &ctx, ht);
5039610b
SL
13207 md5_finish_ctx (&ctx, checksum_before_arglist);
13208 htab_empty (ht);
13209#endif
13210
94a0dd7b 13211 tem = fold_builtin_call_array (type, fn, nargs, argarray);
5039610b
SL
13212
13213#ifdef ENABLE_FOLD_CHECKING
13214 md5_init_ctx (&ctx);
13215 fold_checksum_tree (fn, &ctx, ht);
13216 md5_finish_ctx (&ctx, checksum_after_fn);
13217 htab_empty (ht);
13218
13219 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13220 fold_check_failed (fn, tem);
13221
13222 md5_init_ctx (&ctx);
94a0dd7b
SL
13223 for (i = 0; i < nargs; i++)
13224 fold_checksum_tree (argarray[i], &ctx, ht);
5039610b
SL
13225 md5_finish_ctx (&ctx, checksum_after_arglist);
13226 htab_delete (ht);
13227
13228 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
94a0dd7b 13229 fold_check_failed (NULL_TREE, tem);
5039610b
SL
13230#endif
13231 return tem;
13232}
13233
a98ebe2e 13234/* Perform constant folding and related simplification of initializer
00d1b1d6 13235 expression EXPR. These behave identically to "fold_buildN" but ignore
3e4093b6
RS
13236 potential run-time traps and exceptions that fold must preserve. */
13237
00d1b1d6
JM
13238#define START_FOLD_INIT \
13239 int saved_signaling_nans = flag_signaling_nans;\
13240 int saved_trapping_math = flag_trapping_math;\
13241 int saved_rounding_math = flag_rounding_math;\
13242 int saved_trapv = flag_trapv;\
63b48197 13243 int saved_folding_initializer = folding_initializer;\
00d1b1d6
JM
13244 flag_signaling_nans = 0;\
13245 flag_trapping_math = 0;\
13246 flag_rounding_math = 0;\
63b48197
MS
13247 flag_trapv = 0;\
13248 folding_initializer = 1;
00d1b1d6
JM
13249
13250#define END_FOLD_INIT \
13251 flag_signaling_nans = saved_signaling_nans;\
13252 flag_trapping_math = saved_trapping_math;\
13253 flag_rounding_math = saved_rounding_math;\
63b48197
MS
13254 flag_trapv = saved_trapv;\
13255 folding_initializer = saved_folding_initializer;
00d1b1d6
JM
13256
13257tree
13258fold_build1_initializer (enum tree_code code, tree type, tree op)
13259{
13260 tree result;
13261 START_FOLD_INIT;
13262
13263 result = fold_build1 (code, type, op);
13264
13265 END_FOLD_INIT;
13266 return result;
13267}
13268
3e4093b6 13269tree
00d1b1d6 13270fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
3e4093b6 13271{
3e4093b6 13272 tree result;
00d1b1d6
JM
13273 START_FOLD_INIT;
13274
13275 result = fold_build2 (code, type, op0, op1);
3e4093b6 13276
00d1b1d6
JM
13277 END_FOLD_INIT;
13278 return result;
13279}
3e4093b6 13280
00d1b1d6
JM
13281tree
13282fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
13283 tree op2)
13284{
13285 tree result;
13286 START_FOLD_INIT;
3e4093b6 13287
00d1b1d6 13288 result = fold_build3 (code, type, op0, op1, op2);
3e4093b6 13289
00d1b1d6 13290 END_FOLD_INIT;
3e4093b6
RS
13291 return result;
13292}
13293
5039610b 13294tree
94a0dd7b
SL
13295fold_build_call_array_initializer (tree type, tree fn,
13296 int nargs, tree *argarray)
5039610b
SL
13297{
13298 tree result;
13299 START_FOLD_INIT;
13300
94a0dd7b 13301 result = fold_build_call_array (type, fn, nargs, argarray);
5039610b
SL
13302
13303 END_FOLD_INIT;
13304 return result;
13305}
13306
00d1b1d6
JM
13307#undef START_FOLD_INIT
13308#undef END_FOLD_INIT
13309
c5c76735
JL
13310/* Determine if first argument is a multiple of second argument. Return 0 if
13311 it is not, or we cannot easily determined it to be.
39dfb55a 13312
c5c76735
JL
13313 An example of the sort of thing we care about (at this point; this routine
13314 could surely be made more general, and expanded to do what the *_DIV_EXPR's
13315 fold cases do now) is discovering that
39dfb55a
JL
13316
13317 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13318
13319 is a multiple of
13320
13321 SAVE_EXPR (J * 8)
13322
c5c76735 13323 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
39dfb55a
JL
13324
13325 This code also handles discovering that
13326
13327 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13328
c5c76735 13329 is a multiple of 8 so we don't have to worry about dealing with a
39dfb55a
JL
13330 possible remainder.
13331
c5c76735
JL
13332 Note that we *look* inside a SAVE_EXPR only to determine how it was
13333 calculated; it is not safe for fold to do much of anything else with the
13334 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
13335 at run time. For example, the latter example above *cannot* be implemented
13336 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
13337 evaluation time of the original SAVE_EXPR is not necessarily the same at
13338 the time the new expression is evaluated. The only optimization of this
39dfb55a
JL
13339 sort that would be valid is changing
13340
13341 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
39dfb55a 13342
c5c76735 13343 divided by 8 to
39dfb55a
JL
13344
13345 SAVE_EXPR (I) * SAVE_EXPR (J)
13346
13347 (where the same SAVE_EXPR (J) is used in the original and the
13348 transformed version). */
13349
d4e70294 13350int
fa8db1f7 13351multiple_of_p (tree type, tree top, tree bottom)
39dfb55a
JL
13352{
13353 if (operand_equal_p (top, bottom, 0))
13354 return 1;
13355
13356 if (TREE_CODE (type) != INTEGER_TYPE)
13357 return 0;
13358
13359 switch (TREE_CODE (top))
13360 {
29317008
RH
13361 case BIT_AND_EXPR:
13362 /* Bitwise and provides a power of two multiple. If the mask is
13363 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
13364 if (!integer_pow2p (bottom))
13365 return 0;
13366 /* FALLTHRU */
13367
39dfb55a
JL
13368 case MULT_EXPR:
13369 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13370 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13371
13372 case PLUS_EXPR:
13373 case MINUS_EXPR:
13374 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13375 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13376
fba2c0cd
JJ
13377 case LSHIFT_EXPR:
13378 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
13379 {
13380 tree op1, t1;
13381
13382 op1 = TREE_OPERAND (top, 1);
13383 /* const_binop may not detect overflow correctly,
13384 so check for it explicitly here. */
13385 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
13386 > TREE_INT_CST_LOW (op1)
13387 && TREE_INT_CST_HIGH (op1) == 0
088414c1
RS
13388 && 0 != (t1 = fold_convert (type,
13389 const_binop (LSHIFT_EXPR,
13390 size_one_node,
13391 op1, 0)))
455f14dd 13392 && !TREE_OVERFLOW (t1))
fba2c0cd
JJ
13393 return multiple_of_p (type, t1, bottom);
13394 }
13395 return 0;
13396
39dfb55a 13397 case NOP_EXPR:
c5c76735 13398 /* Can't handle conversions from non-integral or wider integral type. */
39dfb55a
JL
13399 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
13400 || (TYPE_PRECISION (type)
13401 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
13402 return 0;
c5c76735 13403
30f7a378 13404 /* .. fall through ... */
c5c76735 13405
39dfb55a
JL
13406 case SAVE_EXPR:
13407 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
13408
13409 case INTEGER_CST:
fba2c0cd 13410 if (TREE_CODE (bottom) != INTEGER_CST
81737468 13411 || integer_zerop (bottom)
8df83eae 13412 || (TYPE_UNSIGNED (type)
fba2c0cd
JJ
13413 && (tree_int_cst_sgn (top) < 0
13414 || tree_int_cst_sgn (bottom) < 0)))
39dfb55a 13415 return 0;
b73a6056
RS
13416 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
13417 top, bottom, 0));
39dfb55a
JL
13418
13419 default:
13420 return 0;
13421 }
13422}
a36556a8 13423
6ac01510
ILT
13424/* Return true if `t' is known to be non-negative. If the return
13425 value is based on the assumption that signed overflow is undefined,
13426 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13427 *STRICT_OVERFLOW_P. */
a36556a8 13428
682d0395 13429bool
6ac01510 13430tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
a36556a8 13431{
81e61fb4 13432 if (t == error_mark_node)
682d0395 13433 return false;
81e61fb4 13434
b49ceb45 13435 if (TYPE_UNSIGNED (TREE_TYPE (t)))
682d0395 13436 return true;
b49ceb45 13437
a36556a8
ZW
13438 switch (TREE_CODE (t))
13439 {
b16caf72
JL
13440 case SSA_NAME:
13441 /* Query VRP to see if it has recorded any information about
13442 the range of this object. */
13443 return ssa_name_nonnegative_p (t);
13444
88e3805d 13445 case ABS_EXPR:
1ade5842
JM
13446 /* We can't return 1 if flag_wrapv is set because
13447 ABS_EXPR<INT_MIN> = INT_MIN. */
eeef0e45
ILT
13448 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13449 return true;
13450 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
6ac01510
ILT
13451 {
13452 *strict_overflow_p = true;
13453 return true;
13454 }
1ade5842 13455 break;
7dba8395 13456
a36556a8
ZW
13457 case INTEGER_CST:
13458 return tree_int_cst_sgn (t) >= 0;
f7df23be
RS
13459
13460 case REAL_CST:
13461 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
13462
5be014d5 13463 case POINTER_PLUS_EXPR:
f7df23be 13464 case PLUS_EXPR:
96f26e41 13465 if (FLOAT_TYPE_P (TREE_TYPE (t)))
6ac01510
ILT
13466 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13467 strict_overflow_p)
13468 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13469 strict_overflow_p));
96f26e41 13470
e15bb5c6 13471 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
e2cca9be 13472 both unsigned and at least 2 bits shorter than the result. */
96f26e41
RS
13473 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
13474 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
13475 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
13476 {
13477 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
13478 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
8df83eae
RK
13479 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
13480 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
96f26e41
RS
13481 {
13482 unsigned int prec = MAX (TYPE_PRECISION (inner1),
13483 TYPE_PRECISION (inner2)) + 1;
13484 return prec < TYPE_PRECISION (TREE_TYPE (t));
13485 }
13486 }
13487 break;
f7df23be
RS
13488
13489 case MULT_EXPR:
13490 if (FLOAT_TYPE_P (TREE_TYPE (t)))
13491 {
13492 /* x * x for floating point x is always non-negative. */
13493 if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
682d0395 13494 return true;
6ac01510
ILT
13495 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13496 strict_overflow_p)
13497 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13498 strict_overflow_p));
f7df23be 13499 }
96f26e41 13500
e15bb5c6 13501 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
96f26e41
RS
13502 both unsigned and their total bits is shorter than the result. */
13503 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
13504 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
13505 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
13506 {
13507 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
13508 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
8df83eae
RK
13509 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
13510 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
96f26e41
RS
13511 return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
13512 < TYPE_PRECISION (TREE_TYPE (t));
13513 }
682d0395 13514 return false;
f7df23be 13515
196f5a8d
VR
13516 case BIT_AND_EXPR:
13517 case MAX_EXPR:
6ac01510
ILT
13518 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13519 strict_overflow_p)
13520 || tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13521 strict_overflow_p));
196f5a8d
VR
13522
13523 case BIT_IOR_EXPR:
13524 case BIT_XOR_EXPR:
13525 case MIN_EXPR:
13526 case RDIV_EXPR:
ada11335
KG
13527 case TRUNC_DIV_EXPR:
13528 case CEIL_DIV_EXPR:
13529 case FLOOR_DIV_EXPR:
13530 case ROUND_DIV_EXPR:
6ac01510
ILT
13531 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13532 strict_overflow_p)
13533 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13534 strict_overflow_p));
96f26e41 13535
ada11335
KG
13536 case TRUNC_MOD_EXPR:
13537 case CEIL_MOD_EXPR:
13538 case FLOOR_MOD_EXPR:
13539 case ROUND_MOD_EXPR:
196f5a8d
VR
13540 case SAVE_EXPR:
13541 case NON_LVALUE_EXPR:
13542 case FLOAT_EXPR:
aeabd15d 13543 case FIX_TRUNC_EXPR:
6ac01510
ILT
13544 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13545 strict_overflow_p);
96f26e41 13546
196f5a8d
VR
13547 case COMPOUND_EXPR:
13548 case MODIFY_EXPR:
07beea0d 13549 case GIMPLE_MODIFY_STMT:
6ac01510
ILT
13550 return tree_expr_nonnegative_warnv_p (GENERIC_TREE_OPERAND (t, 1),
13551 strict_overflow_p);
96f26e41 13552
196f5a8d 13553 case BIND_EXPR:
6ac01510
ILT
13554 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
13555 strict_overflow_p);
196f5a8d
VR
13556
13557 case COND_EXPR:
6ac01510
ILT
13558 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13559 strict_overflow_p)
13560 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
13561 strict_overflow_p));
b1500d00 13562
96f26e41
RS
13563 case NOP_EXPR:
13564 {
13565 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
13566 tree outer_type = TREE_TYPE (t);
13567
13568 if (TREE_CODE (outer_type) == REAL_TYPE)
13569 {
13570 if (TREE_CODE (inner_type) == REAL_TYPE)
6ac01510
ILT
13571 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13572 strict_overflow_p);
96f26e41
RS
13573 if (TREE_CODE (inner_type) == INTEGER_TYPE)
13574 {
8df83eae 13575 if (TYPE_UNSIGNED (inner_type))
682d0395 13576 return true;
6ac01510
ILT
13577 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13578 strict_overflow_p);
96f26e41
RS
13579 }
13580 }
13581 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
13582 {
13583 if (TREE_CODE (inner_type) == REAL_TYPE)
6ac01510
ILT
13584 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t,0),
13585 strict_overflow_p);
96f26e41
RS
13586 if (TREE_CODE (inner_type) == INTEGER_TYPE)
13587 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
8df83eae 13588 && TYPE_UNSIGNED (inner_type);
96f26e41
RS
13589 }
13590 }
13591 break;
13592
3a5b9284
RH
13593 case TARGET_EXPR:
13594 {
13595 tree temp = TARGET_EXPR_SLOT (t);
13596 t = TARGET_EXPR_INITIAL (t);
13597
13598 /* If the initializer is non-void, then it's a normal expression
13599 that will be assigned to the slot. */
13600 if (!VOID_TYPE_P (t))
6ac01510 13601 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
3a5b9284
RH
13602
13603 /* Otherwise, the initializer sets the slot in some way. One common
13604 way is an assignment statement at the end of the initializer. */
13605 while (1)
13606 {
13607 if (TREE_CODE (t) == BIND_EXPR)
13608 t = expr_last (BIND_EXPR_BODY (t));
13609 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13610 || TREE_CODE (t) == TRY_CATCH_EXPR)
13611 t = expr_last (TREE_OPERAND (t, 0));
13612 else if (TREE_CODE (t) == STATEMENT_LIST)
13613 t = expr_last (t);
13614 else
13615 break;
13616 }
07beea0d
AH
13617 if ((TREE_CODE (t) == MODIFY_EXPR
13618 || TREE_CODE (t) == GIMPLE_MODIFY_STMT)
13619 && GENERIC_TREE_OPERAND (t, 0) == temp)
6ac01510
ILT
13620 return tree_expr_nonnegative_warnv_p (GENERIC_TREE_OPERAND (t, 1),
13621 strict_overflow_p);
3a5b9284 13622
682d0395 13623 return false;
3a5b9284
RH
13624 }
13625
07bae5ad 13626 case CALL_EXPR:
2f503025
JM
13627 {
13628 tree fndecl = get_callee_fndecl (t);
8c96cd51 13629 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2f503025
JM
13630 switch (DECL_FUNCTION_CODE (fndecl))
13631 {
ea6a6627
VR
13632 CASE_FLT_FN (BUILT_IN_ACOS):
13633 CASE_FLT_FN (BUILT_IN_ACOSH):
13634 CASE_FLT_FN (BUILT_IN_CABS):
13635 CASE_FLT_FN (BUILT_IN_COSH):
13636 CASE_FLT_FN (BUILT_IN_ERFC):
13637 CASE_FLT_FN (BUILT_IN_EXP):
13638 CASE_FLT_FN (BUILT_IN_EXP10):
13639 CASE_FLT_FN (BUILT_IN_EXP2):
13640 CASE_FLT_FN (BUILT_IN_FABS):
13641 CASE_FLT_FN (BUILT_IN_FDIM):
13642 CASE_FLT_FN (BUILT_IN_HYPOT):
13643 CASE_FLT_FN (BUILT_IN_POW10):
13644 CASE_INT_FN (BUILT_IN_FFS):
13645 CASE_INT_FN (BUILT_IN_PARITY):
13646 CASE_INT_FN (BUILT_IN_POPCOUNT):
167fa32c
EC
13647 case BUILT_IN_BSWAP32:
13648 case BUILT_IN_BSWAP64:
b45d3a36 13649 /* Always true. */
682d0395 13650 return true;
2f503025 13651
ea6a6627 13652 CASE_FLT_FN (BUILT_IN_SQRT):
67057c53
RS
13653 /* sqrt(-0.0) is -0.0. */
13654 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (t))))
682d0395 13655 return true;
5039610b 13656 return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
6ac01510 13657 strict_overflow_p);
67057c53 13658
ea6a6627
VR
13659 CASE_FLT_FN (BUILT_IN_ASINH):
13660 CASE_FLT_FN (BUILT_IN_ATAN):
13661 CASE_FLT_FN (BUILT_IN_ATANH):
13662 CASE_FLT_FN (BUILT_IN_CBRT):
13663 CASE_FLT_FN (BUILT_IN_CEIL):
13664 CASE_FLT_FN (BUILT_IN_ERF):
13665 CASE_FLT_FN (BUILT_IN_EXPM1):
13666 CASE_FLT_FN (BUILT_IN_FLOOR):
13667 CASE_FLT_FN (BUILT_IN_FMOD):
13668 CASE_FLT_FN (BUILT_IN_FREXP):
13669 CASE_FLT_FN (BUILT_IN_LCEIL):
13670 CASE_FLT_FN (BUILT_IN_LDEXP):
13671 CASE_FLT_FN (BUILT_IN_LFLOOR):
13672 CASE_FLT_FN (BUILT_IN_LLCEIL):
13673 CASE_FLT_FN (BUILT_IN_LLFLOOR):
13674 CASE_FLT_FN (BUILT_IN_LLRINT):
13675 CASE_FLT_FN (BUILT_IN_LLROUND):
13676 CASE_FLT_FN (BUILT_IN_LRINT):
13677 CASE_FLT_FN (BUILT_IN_LROUND):
13678 CASE_FLT_FN (BUILT_IN_MODF):
13679 CASE_FLT_FN (BUILT_IN_NEARBYINT):
ea6a6627
VR
13680 CASE_FLT_FN (BUILT_IN_RINT):
13681 CASE_FLT_FN (BUILT_IN_ROUND):
8df79ac5
KG
13682 CASE_FLT_FN (BUILT_IN_SCALB):
13683 CASE_FLT_FN (BUILT_IN_SCALBLN):
13684 CASE_FLT_FN (BUILT_IN_SCALBN):
ea6a6627 13685 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6351a719 13686 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
ea6a6627
VR
13687 CASE_FLT_FN (BUILT_IN_SINH):
13688 CASE_FLT_FN (BUILT_IN_TANH):
13689 CASE_FLT_FN (BUILT_IN_TRUNC):
b45d3a36 13690 /* True if the 1st argument is nonnegative. */
5039610b 13691 return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
6ac01510 13692 strict_overflow_p);
07bae5ad 13693
ea6a6627 13694 CASE_FLT_FN (BUILT_IN_FMAX):
b45d3a36 13695 /* True if the 1st OR 2nd arguments are nonnegative. */
5039610b 13696 return (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
6ac01510 13697 strict_overflow_p)
5039610b
SL
13698 || (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 1),
13699 strict_overflow_p)));
b45d3a36 13700
ea6a6627 13701 CASE_FLT_FN (BUILT_IN_FMIN):
b45d3a36 13702 /* True if the 1st AND 2nd arguments are nonnegative. */
5039610b 13703 return (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
6ac01510 13704 strict_overflow_p)
5039610b
SL
13705 && (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 1),
13706 strict_overflow_p)));
b45d3a36 13707
ea6a6627 13708 CASE_FLT_FN (BUILT_IN_COPYSIGN):
b45d3a36 13709 /* True if the 2nd argument is nonnegative. */
5039610b
SL
13710 return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 1),
13711 strict_overflow_p);
b45d3a36 13712
682d0395
RS
13713 CASE_FLT_FN (BUILT_IN_POWI):
13714 /* True if the 1st argument is nonnegative or the second
13715 argument is an even integer. */
5039610b 13716 if (TREE_CODE (CALL_EXPR_ARG (t, 1)) == INTEGER_CST)
682d0395 13717 {
5039610b 13718 tree arg1 = CALL_EXPR_ARG (t, 1);
682d0395
RS
13719 if ((TREE_INT_CST_LOW (arg1) & 1) == 0)
13720 return true;
13721 }
5039610b 13722 return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
6ac01510 13723 strict_overflow_p);
682d0395
RS
13724
13725 CASE_FLT_FN (BUILT_IN_POW):
13726 /* True if the 1st argument is nonnegative or the second
13727 argument is an even integer valued real. */
5039610b 13728 if (TREE_CODE (CALL_EXPR_ARG (t, 1)) == REAL_CST)
682d0395
RS
13729 {
13730 REAL_VALUE_TYPE c;
13731 HOST_WIDE_INT n;
13732
5039610b 13733 c = TREE_REAL_CST (CALL_EXPR_ARG (t, 1));
682d0395
RS
13734 n = real_to_integer (&c);
13735 if ((n & 1) == 0)
13736 {
13737 REAL_VALUE_TYPE cint;
13738 real_from_integer (&cint, VOIDmode, n,
13739 n < 0 ? -1 : 0, 0);
13740 if (real_identical (&c, &cint))
13741 return true;
13742 }
13743 }
5039610b 13744 return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
6ac01510 13745 strict_overflow_p);
682d0395 13746
2f503025
JM
13747 default:
13748 break;
13749 }
13750 }
07bae5ad 13751
71c0e7fc 13752 /* ... fall through ... */
07bae5ad 13753
a36556a8 13754 default:
3613c7ab
AP
13755 {
13756 tree type = TREE_TYPE (t);
13757 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
13758 && truth_value_p (TREE_CODE (t)))
13759 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
13760 have a signed:1 type (where the value is -1 and 0). */
13761 return true;
13762 }
a36556a8 13763 }
96f26e41
RS
13764
13765 /* We don't know sign of `t', so be conservative and return false. */
682d0395 13766 return false;
a36556a8
ZW
13767}
13768
6ac01510
ILT
13769/* Return true if `t' is known to be non-negative. Handle warnings
13770 about undefined signed overflow. */
13771
13772bool
13773tree_expr_nonnegative_p (tree t)
13774{
13775 bool ret, strict_overflow_p;
13776
13777 strict_overflow_p = false;
13778 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13779 if (strict_overflow_p)
13780 fold_overflow_warning (("assuming signed overflow does not occur when "
13781 "determining that expression is always "
13782 "non-negative"),
13783 WARN_STRICT_OVERFLOW_MISC);
13784 return ret;
13785}
13786
8e7b3a43
KH
13787/* Return true when T is an address and is known to be nonzero.
13788 For floating point we further ensure that T is not denormal.
6ac01510
ILT
13789 Similar logic is present in nonzero_address in rtlanal.h.
13790
13791 If the return value is based on the assumption that signed overflow
13792 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13793 change *STRICT_OVERFLOW_P. */
8e7b3a43 13794
4db8040c 13795bool
6ac01510 13796tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
8e7b3a43
KH
13797{
13798 tree type = TREE_TYPE (t);
6ac01510 13799 bool sub_strict_overflow_p;
8e7b3a43 13800
1ae58c30 13801 /* Doing something useful for floating point would need more work. */
8e7b3a43
KH
13802 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13803 return false;
13804
13805 switch (TREE_CODE (t))
13806 {
b16caf72
JL
13807 case SSA_NAME:
13808 /* Query VRP to see if it has recorded any information about
13809 the range of this object. */
13810 return ssa_name_nonzero_p (t);
13811
8e7b3a43 13812 case ABS_EXPR:
6ac01510
ILT
13813 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
13814 strict_overflow_p);
8e7b3a43
KH
13815
13816 case INTEGER_CST:
455f14dd 13817 return !integer_zerop (t);
8e7b3a43 13818
5be014d5 13819 case POINTER_PLUS_EXPR:
8e7b3a43 13820 case PLUS_EXPR:
eeef0e45 13821 if (TYPE_OVERFLOW_UNDEFINED (type))
8e7b3a43
KH
13822 {
13823 /* With the presence of negative values it is hard
13824 to say something. */
6ac01510
ILT
13825 sub_strict_overflow_p = false;
13826 if (!tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13827 &sub_strict_overflow_p)
13828 || !tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13829 &sub_strict_overflow_p))
8e7b3a43
KH
13830 return false;
13831 /* One of operands must be positive and the other non-negative. */
6ac01510
ILT
13832 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13833 overflows, on a twos-complement machine the sum of two
13834 nonnegative numbers can never be zero. */
13835 return (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
13836 strict_overflow_p)
13837 || tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13838 strict_overflow_p));
8e7b3a43
KH
13839 }
13840 break;
13841
13842 case MULT_EXPR:
eeef0e45 13843 if (TYPE_OVERFLOW_UNDEFINED (type))
8e7b3a43 13844 {
6ac01510
ILT
13845 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
13846 strict_overflow_p)
13847 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13848 strict_overflow_p))
13849 {
13850 *strict_overflow_p = true;
13851 return true;
13852 }
8e7b3a43
KH
13853 }
13854 break;
13855
13856 case NOP_EXPR:
13857 {
13858 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
13859 tree outer_type = TREE_TYPE (t);
13860
284cbef5 13861 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
6ac01510
ILT
13862 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
13863 strict_overflow_p));
8e7b3a43
KH
13864 }
13865 break;
13866
13867 case ADDR_EXPR:
88f19756
RH
13868 {
13869 tree base = get_base_address (TREE_OPERAND (t, 0));
13870
13871 if (!base)
13872 return false;
13873
13874 /* Weak declarations may link to NULL. */
820cc88f 13875 if (VAR_OR_FUNCTION_DECL_P (base))
88f19756
RH
13876 return !DECL_WEAK (base);
13877
13878 /* Constants are never weak. */
6615c446 13879 if (CONSTANT_CLASS_P (base))
88f19756
RH
13880 return true;
13881
13882 return false;
13883 }
8e7b3a43
KH
13884
13885 case COND_EXPR:
6ac01510
ILT
13886 sub_strict_overflow_p = false;
13887 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13888 &sub_strict_overflow_p)
13889 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13890 &sub_strict_overflow_p))
13891 {
13892 if (sub_strict_overflow_p)
13893 *strict_overflow_p = true;
13894 return true;
13895 }
13896 break;
8e7b3a43
KH
13897
13898 case MIN_EXPR:
6ac01510
ILT
13899 sub_strict_overflow_p = false;
13900 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
13901 &sub_strict_overflow_p)
13902 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13903 &sub_strict_overflow_p))
13904 {
13905 if (sub_strict_overflow_p)
13906 *strict_overflow_p = true;
13907 }
13908 break;
8e7b3a43
KH
13909
13910 case MAX_EXPR:
6ac01510
ILT
13911 sub_strict_overflow_p = false;
13912 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
13913 &sub_strict_overflow_p))
8e7b3a43 13914 {
6ac01510
ILT
13915 if (sub_strict_overflow_p)
13916 *strict_overflow_p = true;
13917
8e7b3a43 13918 /* When both operands are nonzero, then MAX must be too. */
6ac01510
ILT
13919 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13920 strict_overflow_p))
8e7b3a43
KH
13921 return true;
13922
13923 /* MAX where operand 0 is positive is positive. */
6ac01510
ILT
13924 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13925 strict_overflow_p);
8e7b3a43
KH
13926 }
13927 /* MAX where operand 1 is positive is positive. */
6ac01510
ILT
13928 else if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13929 &sub_strict_overflow_p)
13930 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13931 &sub_strict_overflow_p))
13932 {
13933 if (sub_strict_overflow_p)
13934 *strict_overflow_p = true;
13935 return true;
13936 }
8e7b3a43
KH
13937 break;
13938
13939 case COMPOUND_EXPR:
13940 case MODIFY_EXPR:
07beea0d 13941 case GIMPLE_MODIFY_STMT:
8e7b3a43 13942 case BIND_EXPR:
6ac01510
ILT
13943 return tree_expr_nonzero_warnv_p (GENERIC_TREE_OPERAND (t, 1),
13944 strict_overflow_p);
8e7b3a43
KH
13945
13946 case SAVE_EXPR:
13947 case NON_LVALUE_EXPR:
6ac01510
ILT
13948 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
13949 strict_overflow_p);
8e7b3a43 13950
b1500d00 13951 case BIT_IOR_EXPR:
6ac01510
ILT
13952 return (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13953 strict_overflow_p)
13954 || tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
13955 strict_overflow_p));
b1500d00 13956
4db8040c
JM
13957 case CALL_EXPR:
13958 return alloca_call_p (t);
13959
8e7b3a43
KH
13960 default:
13961 break;
13962 }
13963 return false;
13964}
13965
6ac01510
ILT
13966/* Return true when T is an address and is known to be nonzero.
13967 Handle warnings about undefined signed overflow. */
13968
13969bool
13970tree_expr_nonzero_p (tree t)
13971{
13972 bool ret, strict_overflow_p;
13973
13974 strict_overflow_p = false;
13975 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
13976 if (strict_overflow_p)
13977 fold_overflow_warning (("assuming signed overflow does not occur when "
13978 "determining that expression is always "
13979 "non-zero"),
13980 WARN_STRICT_OVERFLOW_MISC);
13981 return ret;
13982}
13983
6de9cd9a
DN
13984/* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13985 attempt to fold the expression to a constant without modifying TYPE,
13986 OP0 or OP1.
13987
13988 If the expression could be simplified to a constant, then return
13989 the constant. If the expression would not be simplified to a
41704a38 13990 constant, then return NULL_TREE. */
6de9cd9a
DN
13991
13992tree
b52d5eaa 13993fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
6de9cd9a 13994{
054632e8
RS
13995 tree tem = fold_binary (code, type, op0, op1);
13996 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
6de9cd9a
DN
13997}
13998
13999/* Given the components of a unary expression CODE, TYPE and OP0,
14000 attempt to fold the expression to a constant without modifying
d1822754 14001 TYPE or OP0.
6de9cd9a
DN
14002
14003 If the expression could be simplified to a constant, then return
14004 the constant. If the expression would not be simplified to a
41704a38 14005 constant, then return NULL_TREE. */
6de9cd9a
DN
14006
14007tree
b52d5eaa 14008fold_unary_to_constant (enum tree_code code, tree type, tree op0)
6de9cd9a 14009{
054632e8
RS
14010 tree tem = fold_unary (code, type, op0);
14011 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
6de9cd9a
DN
14012}
14013
14014/* If EXP represents referencing an element in a constant string
14015 (either via pointer arithmetic or array indexing), return the
14016 tree representing the value accessed, otherwise return NULL. */
14017
14018tree
14019fold_read_from_constant_string (tree exp)
14020{
8e3dc7a3
RG
14021 if ((TREE_CODE (exp) == INDIRECT_REF
14022 || TREE_CODE (exp) == ARRAY_REF)
14023 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
6de9cd9a
DN
14024 {
14025 tree exp1 = TREE_OPERAND (exp, 0);
14026 tree index;
14027 tree string;
14028
14029 if (TREE_CODE (exp) == INDIRECT_REF)
44de5aeb 14030 string = string_constant (exp1, &index);
6de9cd9a
DN
14031 else
14032 {
44de5aeb 14033 tree low_bound = array_ref_low_bound (exp);
b953ebd6 14034 index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
d1822754 14035
6de9cd9a
DN
14036 /* Optimize the special-case of a zero lower bound.
14037
14038 We convert the low_bound to sizetype to avoid some problems
14039 with constant folding. (E.g. suppose the lower bound is 1,
14040 and its mode is QI. Without the conversion,l (ARRAY
14041 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
14042 +INDEX), which becomes (ARRAY+255+INDEX). Opps!) */
14043 if (! integer_zerop (low_bound))
b953ebd6 14044 index = size_diffop (index, fold_convert (sizetype, low_bound));
6de9cd9a
DN
14045
14046 string = exp1;
14047 }
14048
14049 if (string
f9c3744b 14050 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
6de9cd9a
DN
14051 && TREE_CODE (string) == STRING_CST
14052 && TREE_CODE (index) == INTEGER_CST
14053 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
14054 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
14055 == MODE_INT)
14056 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
12fea1f9 14057 return fold_convert (TREE_TYPE (exp),
4a90aeeb
NS
14058 build_int_cst (NULL_TREE,
14059 (TREE_STRING_POINTER (string)
7d60be94 14060 [TREE_INT_CST_LOW (index)])));
6de9cd9a
DN
14061 }
14062 return NULL;
14063}
14064
33d13fac
KH
14065/* Return the tree for neg (ARG0) when ARG0 is known to be either
14066 an integer constant or real constant.
14067
14068 TYPE is the type of the result. */
14069
14070static tree
14071fold_negate_const (tree arg0, tree type)
14072{
14073 tree t = NULL_TREE;
14074
0bccc606 14075 switch (TREE_CODE (arg0))
33d13fac 14076 {
0bccc606
NS
14077 case INTEGER_CST:
14078 {
14079 unsigned HOST_WIDE_INT low;
14080 HOST_WIDE_INT high;
14081 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14082 TREE_INT_CST_HIGH (arg0),
14083 &low, &high);
b8fca551
RG
14084 t = force_fit_type_double (type, low, high, 1,
14085 (overflow | TREE_OVERFLOW (arg0))
d95787e6 14086 && !TYPE_UNSIGNED (type));
0bccc606
NS
14087 break;
14088 }
3e6688a7 14089
0bccc606
NS
14090 case REAL_CST:
14091 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
14092 break;
d1822754 14093
0bccc606
NS
14094 default:
14095 gcc_unreachable ();
14096 }
3e6688a7 14097
33d13fac
KH
14098 return t;
14099}
14100
73c4ab99
KH
14101/* Return the tree for abs (ARG0) when ARG0 is known to be either
14102 an integer constant or real constant.
14103
14104 TYPE is the type of the result. */
14105
9655d83b 14106tree
73c4ab99
KH
14107fold_abs_const (tree arg0, tree type)
14108{
14109 tree t = NULL_TREE;
14110
0bccc606 14111 switch (TREE_CODE (arg0))
73c4ab99 14112 {
0bccc606 14113 case INTEGER_CST:
73c4ab99
KH
14114 /* If the value is unsigned, then the absolute value is
14115 the same as the ordinary value. */
8df83eae 14116 if (TYPE_UNSIGNED (type))
0bccc606 14117 t = arg0;
73c4ab99
KH
14118 /* Similarly, if the value is non-negative. */
14119 else if (INT_CST_LT (integer_minus_one_node, arg0))
0bccc606 14120 t = arg0;
73c4ab99
KH
14121 /* If the value is negative, then the absolute value is
14122 its negation. */
14123 else
14124 {
14125 unsigned HOST_WIDE_INT low;
14126 HOST_WIDE_INT high;
14127 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14128 TREE_INT_CST_HIGH (arg0),
14129 &low, &high);
b8fca551 14130 t = force_fit_type_double (type, low, high, -1,
d95787e6 14131 overflow | TREE_OVERFLOW (arg0));
73c4ab99 14132 }
0bccc606 14133 break;
3e6688a7 14134
0bccc606 14135 case REAL_CST:
73c4ab99 14136 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
0bccc606 14137 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
73c4ab99 14138 else
0bccc606
NS
14139 t = arg0;
14140 break;
3e6688a7 14141
0bccc606
NS
14142 default:
14143 gcc_unreachable ();
73c4ab99 14144 }
3e6688a7 14145
73c4ab99
KH
14146 return t;
14147}
14148
a653e758
RS
14149/* Return the tree for not (ARG0) when ARG0 is known to be an integer
14150 constant. TYPE is the type of the result. */
14151
14152static tree
14153fold_not_const (tree arg0, tree type)
14154{
14155 tree t = NULL_TREE;
14156
0bccc606 14157 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
3e6688a7 14158
b8fca551
RG
14159 t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
14160 ~TREE_INT_CST_HIGH (arg0), 0,
d95787e6 14161 TREE_OVERFLOW (arg0));
3e6688a7 14162
a653e758
RS
14163 return t;
14164}
14165
8e7b3a43
KH
14166/* Given CODE, a relational operator, the target type, TYPE and two
14167 constant operands OP0 and OP1, return the result of the
14168 relational operation. If the result is not a compile time
14169 constant, then return NULL_TREE. */
14170
14171static tree
14172fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
14173{
1382f0f0 14174 int result, invert;
8e7b3a43
KH
14175
14176 /* From here on, the only cases we handle are when the result is
ee8db92b
RS
14177 known to be a constant. */
14178
14179 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
14180 {
adb8e07e
RS
14181 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
14182 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
14183
ee8db92b 14184 /* Handle the cases where either operand is a NaN. */
adb8e07e 14185 if (real_isnan (c0) || real_isnan (c1))
ee8db92b
RS
14186 {
14187 switch (code)
14188 {
14189 case EQ_EXPR:
14190 case ORDERED_EXPR:
14191 result = 0;
14192 break;
14193
14194 case NE_EXPR:
14195 case UNORDERED_EXPR:
14196 case UNLT_EXPR:
14197 case UNLE_EXPR:
14198 case UNGT_EXPR:
14199 case UNGE_EXPR:
14200 case UNEQ_EXPR:
14201 result = 1;
14202 break;
14203
14204 case LT_EXPR:
14205 case LE_EXPR:
14206 case GT_EXPR:
14207 case GE_EXPR:
14208 case LTGT_EXPR:
14209 if (flag_trapping_math)
14210 return NULL_TREE;
14211 result = 0;
14212 break;
14213
14214 default:
0bccc606 14215 gcc_unreachable ();
ee8db92b
RS
14216 }
14217
14218 return constant_boolean_node (result, type);
14219 }
14220
adb8e07e 14221 return constant_boolean_node (real_compare (code, c0, c1), type);
ee8db92b
RS
14222 }
14223
23b9463b
RS
14224 /* Handle equality/inequality of complex constants. */
14225 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
14226 {
14227 tree rcond = fold_relational_const (code, type,
14228 TREE_REALPART (op0),
14229 TREE_REALPART (op1));
14230 tree icond = fold_relational_const (code, type,
14231 TREE_IMAGPART (op0),
14232 TREE_IMAGPART (op1));
14233 if (code == EQ_EXPR)
14234 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
14235 else if (code == NE_EXPR)
14236 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
14237 else
14238 return NULL_TREE;
14239 }
14240
ee8db92b 14241 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
8e7b3a43
KH
14242
14243 To compute GT, swap the arguments and do LT.
14244 To compute GE, do LT and invert the result.
14245 To compute LE, swap the arguments, do LT and invert the result.
14246 To compute NE, do EQ and invert the result.
14247
14248 Therefore, the code below must handle only EQ and LT. */
14249
14250 if (code == LE_EXPR || code == GT_EXPR)
14251 {
1382f0f0
RS
14252 tree tem = op0;
14253 op0 = op1;
14254 op1 = tem;
8e7b3a43
KH
14255 code = swap_tree_comparison (code);
14256 }
14257
14258 /* Note that it is safe to invert for real values here because we
ee8db92b 14259 have already handled the one case that it matters. */
8e7b3a43 14260
8e7b3a43
KH
14261 invert = 0;
14262 if (code == NE_EXPR || code == GE_EXPR)
14263 {
14264 invert = 1;
d1a7edaf 14265 code = invert_tree_comparison (code, false);
8e7b3a43
KH
14266 }
14267
14268 /* Compute a result for LT or EQ if args permit;
14269 Otherwise return T. */
14270 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14271 {
14272 if (code == EQ_EXPR)
1382f0f0
RS
14273 result = tree_int_cst_equal (op0, op1);
14274 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
14275 result = INT_CST_LT_UNSIGNED (op0, op1);
8e7b3a43 14276 else
1382f0f0 14277 result = INT_CST_LT (op0, op1);
8e7b3a43 14278 }
1382f0f0 14279 else
8e7b3a43
KH
14280 return NULL_TREE;
14281
14282 if (invert)
1382f0f0
RS
14283 result ^= 1;
14284 return constant_boolean_node (result, type);
8e7b3a43
KH
14285}
14286
3a687f8b
MM
14287/* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14288 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14289 itself. */
0ad28dde
AP
14290
14291tree
14292fold_build_cleanup_point_expr (tree type, tree expr)
14293{
14294 /* If the expression does not have side effects then we don't have to wrap
14295 it with a cleanup point expression. */
14296 if (!TREE_SIDE_EFFECTS (expr))
14297 return expr;
0e256a82
AP
14298
14299 /* If the expression is a return, check to see if the expression inside the
14300 return has no side effects or the right hand side of the modify expression
14301 inside the return. If either don't have side effects set we don't need to
14302 wrap the expression in a cleanup point expression. Note we don't check the
14303 left hand side of the modify because it should always be a return decl. */
14304 if (TREE_CODE (expr) == RETURN_EXPR)
14305 {
14306 tree op = TREE_OPERAND (expr, 0);
14307 if (!op || !TREE_SIDE_EFFECTS (op))
14308 return expr;
14309 op = TREE_OPERAND (op, 1);
14310 if (!TREE_SIDE_EFFECTS (op))
14311 return expr;
14312 }
0ad28dde
AP
14313
14314 return build1 (CLEANUP_POINT_EXPR, type, expr);
14315}
14316
30d2e943
RG
14317/* Given a pointer value OP0 and a type TYPE, return a simplified version
14318 of an indirection through OP0, or NULL_TREE if no simplification is
14319 possible. */
cd3ce9b4 14320
095ecc24 14321tree
30d2e943 14322fold_indirect_ref_1 (tree type, tree op0)
cd3ce9b4 14323{
30d2e943 14324 tree sub = op0;
cd3ce9b4
JM
14325 tree subtype;
14326
6033ae2a 14327 STRIP_NOPS (sub);
6a720599
JM
14328 subtype = TREE_TYPE (sub);
14329 if (!POINTER_TYPE_P (subtype))
14330 return NULL_TREE;
14331
cd3ce9b4
JM
14332 if (TREE_CODE (sub) == ADDR_EXPR)
14333 {
14334 tree op = TREE_OPERAND (sub, 0);
14335 tree optype = TREE_TYPE (op);
f9f63ff2
AP
14336 /* *&CONST_DECL -> to the value of the const decl. */
14337 if (TREE_CODE (op) == CONST_DECL)
14338 return DECL_INITIAL (op);
41b9109a 14339 /* *&p => p; make sure to handle *&"str"[cst] here. */
30d2e943 14340 if (type == optype)
41b9109a
RG
14341 {
14342 tree fop = fold_read_from_constant_string (op);
14343 if (fop)
14344 return fop;
14345 else
14346 return op;
14347 }
cd3ce9b4
JM
14348 /* *(foo *)&fooarray => fooarray[0] */
14349 else if (TREE_CODE (optype) == ARRAY_TYPE
30d2e943 14350 && type == TREE_TYPE (optype))
0d56ab33
AP
14351 {
14352 tree type_domain = TYPE_DOMAIN (optype);
14353 tree min_val = size_zero_node;
14354 if (type_domain && TYPE_MIN_VALUE (type_domain))
14355 min_val = TYPE_MIN_VALUE (type_domain);
14356 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
14357 }
4853940c
AP
14358 /* *(foo *)&complexfoo => __real__ complexfoo */
14359 else if (TREE_CODE (optype) == COMPLEX_TYPE
14360 && type == TREE_TYPE (optype))
14361 return fold_build1 (REALPART_EXPR, type, op);
0890b981
AP
14362 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14363 else if (TREE_CODE (optype) == VECTOR_TYPE
14364 && type == TREE_TYPE (optype))
14365 {
14366 tree part_width = TYPE_SIZE (type);
14367 tree index = bitsize_int (0);
14368 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
14369 }
cd3ce9b4
JM
14370 }
14371
4853940c 14372 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
5be014d5 14373 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
4853940c
AP
14374 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14375 {
14376 tree op00 = TREE_OPERAND (sub, 0);
14377 tree op01 = TREE_OPERAND (sub, 1);
14378 tree op00type;
14379
14380 STRIP_NOPS (op00);
14381 op00type = TREE_TYPE (op00);
14382 if (TREE_CODE (op00) == ADDR_EXPR
14383 && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
14384 && type == TREE_TYPE (TREE_TYPE (op00type)))
14385 {
14386 tree size = TYPE_SIZE_UNIT (type);
14387 if (tree_int_cst_equal (size, op01))
14388 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
14389 }
14390 }
14391
cd3ce9b4 14392 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
cd3ce9b4 14393 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
30d2e943 14394 && type == TREE_TYPE (TREE_TYPE (subtype)))
cd3ce9b4 14395 {
0d56ab33
AP
14396 tree type_domain;
14397 tree min_val = size_zero_node;
cd3ce9b4 14398 sub = build_fold_indirect_ref (sub);
0d56ab33
AP
14399 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14400 if (type_domain && TYPE_MIN_VALUE (type_domain))
14401 min_val = TYPE_MIN_VALUE (type_domain);
14402 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
cd3ce9b4
JM
14403 }
14404
6a720599
JM
14405 return NULL_TREE;
14406}
14407
14408/* Builds an expression for an indirection through T, simplifying some
14409 cases. */
14410
14411tree
14412build_fold_indirect_ref (tree t)
14413{
30d2e943
RG
14414 tree type = TREE_TYPE (TREE_TYPE (t));
14415 tree sub = fold_indirect_ref_1 (type, t);
6a720599
JM
14416
14417 if (sub)
14418 return sub;
14419 else
30d2e943 14420 return build1 (INDIRECT_REF, type, t);
6a720599
JM
14421}
14422
14423/* Given an INDIRECT_REF T, return either T or a simplified version. */
14424
14425tree
14426fold_indirect_ref (tree t)
14427{
30d2e943 14428 tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
6a720599
JM
14429
14430 if (sub)
14431 return sub;
14432 else
14433 return t;
cd3ce9b4
JM
14434}
14435
9675412f
RS
14436/* Strip non-trapping, non-side-effecting tree nodes from an expression
14437 whose result is ignored. The type of the returned tree need not be
14438 the same as the original expression. */
14439
14440tree
14441fold_ignored_result (tree t)
14442{
14443 if (!TREE_SIDE_EFFECTS (t))
14444 return integer_zero_node;
14445
14446 for (;;)
14447 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14448 {
6615c446 14449 case tcc_unary:
9675412f
RS
14450 t = TREE_OPERAND (t, 0);
14451 break;
14452
6615c446
JO
14453 case tcc_binary:
14454 case tcc_comparison:
9675412f
RS
14455 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14456 t = TREE_OPERAND (t, 0);
14457 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14458 t = TREE_OPERAND (t, 1);
14459 else
14460 return t;
14461 break;
14462
6615c446 14463 case tcc_expression:
9675412f
RS
14464 switch (TREE_CODE (t))
14465 {
14466 case COMPOUND_EXPR:
14467 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14468 return t;
14469 t = TREE_OPERAND (t, 0);
14470 break;
14471
14472 case COND_EXPR:
14473 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14474 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14475 return t;
14476 t = TREE_OPERAND (t, 0);
14477 break;
14478
14479 default:
14480 return t;
14481 }
14482 break;
14483
14484 default:
14485 return t;
14486 }
14487}
14488
15931954
RH
14489/* Return the value of VALUE, rounded up to a multiple of DIVISOR.
14490 This can only be applied to objects of a sizetype. */
14491
14492tree
14493round_up (tree value, int divisor)
14494{
0a936b12 14495 tree div = NULL_TREE;
15931954 14496
0bccc606 14497 gcc_assert (divisor > 0);
15931954
RH
14498 if (divisor == 1)
14499 return value;
14500
15931954 14501 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
0a936b12
NS
14502 have to do anything. Only do this when we are not given a const,
14503 because in that case, this check is more expensive than just
8c27b7d4 14504 doing it. */
0a936b12
NS
14505 if (TREE_CODE (value) != INTEGER_CST)
14506 {
ce552f75 14507 div = build_int_cst (TREE_TYPE (value), divisor);
0a936b12
NS
14508
14509 if (multiple_of_p (TREE_TYPE (value), value, div))
14510 return value;
14511 }
15931954
RH
14512
14513 /* If divisor is a power of two, simplify this to bit manipulation. */
14514 if (divisor == (divisor & -divisor))
14515 {
74890d7b
RS
14516 if (TREE_CODE (value) == INTEGER_CST)
14517 {
14518 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
bcf52d7b
RS
14519 unsigned HOST_WIDE_INT high;
14520 bool overflow_p;
74890d7b
RS
14521
14522 if ((low & (divisor - 1)) == 0)
14523 return value;
14524
bcf52d7b 14525 overflow_p = TREE_OVERFLOW (value);
74890d7b
RS
14526 high = TREE_INT_CST_HIGH (value);
14527 low &= ~(divisor - 1);
14528 low += divisor;
14529 if (low == 0)
74890d7b 14530 {
bcf52d7b
RS
14531 high++;
14532 if (high == 0)
14533 overflow_p = true;
74890d7b 14534 }
bcf52d7b
RS
14535
14536 return force_fit_type_double (TREE_TYPE (value), low, high,
14537 -1, overflow_p);
74890d7b
RS
14538 }
14539 else
14540 {
bcf52d7b
RS
14541 tree t;
14542
74890d7b
RS
14543 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14544 value = size_binop (PLUS_EXPR, value, t);
14545 t = build_int_cst (TREE_TYPE (value), -divisor);
14546 value = size_binop (BIT_AND_EXPR, value, t);
14547 }
15931954
RH
14548 }
14549 else
14550 {
0a936b12 14551 if (!div)
ce552f75 14552 div = build_int_cst (TREE_TYPE (value), divisor);
15931954
RH
14553 value = size_binop (CEIL_DIV_EXPR, value, div);
14554 value = size_binop (MULT_EXPR, value, div);
14555 }
14556
14557 return value;
14558}
14559
14560/* Likewise, but round down. */
14561
14562tree
14563round_down (tree value, int divisor)
14564{
0a936b12 14565 tree div = NULL_TREE;
15931954 14566
0bccc606 14567 gcc_assert (divisor > 0);
15931954
RH
14568 if (divisor == 1)
14569 return value;
14570
15931954 14571 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
0a936b12
NS
14572 have to do anything. Only do this when we are not given a const,
14573 because in that case, this check is more expensive than just
8c27b7d4 14574 doing it. */
0a936b12
NS
14575 if (TREE_CODE (value) != INTEGER_CST)
14576 {
ce552f75 14577 div = build_int_cst (TREE_TYPE (value), divisor);
0a936b12
NS
14578
14579 if (multiple_of_p (TREE_TYPE (value), value, div))
14580 return value;
14581 }
15931954
RH
14582
14583 /* If divisor is a power of two, simplify this to bit manipulation. */
14584 if (divisor == (divisor & -divisor))
14585 {
0a936b12 14586 tree t;
3e6688a7 14587
7d60be94 14588 t = build_int_cst (TREE_TYPE (value), -divisor);
15931954
RH
14589 value = size_binop (BIT_AND_EXPR, value, t);
14590 }
14591 else
14592 {
0a936b12 14593 if (!div)
ce552f75 14594 div = build_int_cst (TREE_TYPE (value), divisor);
15931954
RH
14595 value = size_binop (FLOOR_DIV_EXPR, value, div);
14596 value = size_binop (MULT_EXPR, value, div);
14597 }
14598
14599 return value;
14600}
2f4675b4 14601
7299dbfb
ZD
14602/* Returns the pointer to the base of the object addressed by EXP and
14603 extracts the information about the offset of the access, storing it
14604 to PBITPOS and POFFSET. */
14605
14606static tree
14607split_address_to_core_and_offset (tree exp,
14608 HOST_WIDE_INT *pbitpos, tree *poffset)
14609{
14610 tree core;
14611 enum machine_mode mode;
14612 int unsignedp, volatilep;
14613 HOST_WIDE_INT bitsize;
14614
14615 if (TREE_CODE (exp) == ADDR_EXPR)
14616 {
14617 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
2614034e
EB
14618 poffset, &mode, &unsignedp, &volatilep,
14619 false);
70826cbb 14620 core = fold_addr_expr (core);
7299dbfb
ZD
14621 }
14622 else
14623 {
14624 core = exp;
14625 *pbitpos = 0;
14626 *poffset = NULL_TREE;
14627 }
14628
14629 return core;
14630}
14631
2f4675b4 14632/* Returns true if addresses of E1 and E2 differ by a constant, false
7299dbfb 14633 otherwise. If they do, E1 - E2 is stored in *DIFF. */
2f4675b4
ZD
14634
14635bool
14636ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14637{
14638 tree core1, core2;
2f4675b4
ZD
14639 HOST_WIDE_INT bitpos1, bitpos2;
14640 tree toffset1, toffset2, tdiff, type;
3e6688a7 14641
7299dbfb
ZD
14642 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14643 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
2f4675b4
ZD
14644
14645 if (bitpos1 % BITS_PER_UNIT != 0
14646 || bitpos2 % BITS_PER_UNIT != 0
14647 || !operand_equal_p (core1, core2, 0))
14648 return false;
14649
14650 if (toffset1 && toffset2)
14651 {
14652 type = TREE_TYPE (toffset1);
14653 if (type != TREE_TYPE (toffset2))
14654 toffset2 = fold_convert (type, toffset2);
14655
7f20a5b7 14656 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
87de2376 14657 if (!cst_and_fits_in_hwi (tdiff))
2f4675b4
ZD
14658 return false;
14659
87de2376 14660 *diff = int_cst_value (tdiff);
2f4675b4
ZD
14661 }
14662 else if (toffset1 || toffset2)
14663 {
14664 /* If only one of the offsets is non-constant, the difference cannot
14665 be a constant. */
14666 return false;
14667 }
14668 else
14669 *diff = 0;
14670
14671 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14672 return true;
14673}
e3bb43c0
RS
14674
14675/* Simplify the floating point expression EXP when the sign of the
14676 result is not significant. Return NULL_TREE if no simplification
14677 is possible. */
14678
14679tree
14680fold_strip_sign_ops (tree exp)
14681{
14682 tree arg0, arg1;
14683
14684 switch (TREE_CODE (exp))
14685 {
14686 case ABS_EXPR:
14687 case NEGATE_EXPR:
14688 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
14689 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
14690
14691 case MULT_EXPR:
14692 case RDIV_EXPR:
14693 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
14694 return NULL_TREE;
14695 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
14696 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
14697 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
7f20a5b7
KH
14698 return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
14699 arg0 ? arg0 : TREE_OPERAND (exp, 0),
14700 arg1 ? arg1 : TREE_OPERAND (exp, 1));
e3bb43c0
RS
14701 break;
14702
b7e85170
KG
14703 case COMPOUND_EXPR:
14704 arg0 = TREE_OPERAND (exp, 0);
14705 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
14706 if (arg1)
14707 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
14708 break;
14709
14710 case COND_EXPR:
14711 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
14712 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
14713 if (arg0 || arg1)
14714 return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
14715 arg0 ? arg0 : TREE_OPERAND (exp, 1),
14716 arg1 ? arg1 : TREE_OPERAND (exp, 2));
14717 break;
14718
b81e7144 14719 case CALL_EXPR:
6af46feb
KG
14720 {
14721 const enum built_in_function fcode = builtin_mathfn_code (exp);
14722 switch (fcode)
14723 {
14724 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14725 /* Strip copysign function call, return the 1st argument. */
5039610b
SL
14726 arg0 = CALL_EXPR_ARG (exp, 0);
14727 arg1 = CALL_EXPR_ARG (exp, 1);
6af46feb
KG
14728 return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
14729
14730 default:
14731 /* Strip sign ops from the argument of "odd" math functions. */
14732 if (negate_mathfn_p (fcode))
14733 {
5039610b 14734 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
6af46feb 14735 if (arg0)
5039610b 14736 return build_call_expr (get_callee_fndecl (exp), 1, arg0);
6af46feb
KG
14737 }
14738 break;
b81e7144 14739 }
6af46feb 14740 }
b81e7144
KG
14741 break;
14742
e3bb43c0
RS
14743 default:
14744 break;
14745 }
14746 return NULL_TREE;
14747}