]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fold-const.c
Makefile.in, [...]: replace "GNU CC" with "GCC".
[thirdparty/gcc.git] / gcc / fold-const.c
CommitLineData
6d716ca8 1/* Fold a constant sub-tree into a single node for C-compiler
06ceef4e 2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
32812a40 3 1999, 2000, 2001 Free Software Foundation, Inc.
6d716ca8 4
1322177d 5This file is part of GCC.
6d716ca8 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
6d716ca8 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
6d716ca8
RS
16
17You should have received a copy of the GNU General Public License
1322177d
LB
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
6d716ca8 21
6dc42e49 22/*@@ This file should be rewritten to use an arbitrary precision
6d716ca8
RS
23 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25 @@ The routines that translate from the ap rep should
26 @@ warn if precision et. al. is lost.
27 @@ This would also make life easier when this technology is used
28 @@ for cross-compilers. */
29
f8dac6eb 30/* The entry points in this file are fold, size_int_wide, size_binop
0da6f3db 31 and force_fit_type.
6d716ca8
RS
32
33 fold takes a tree as argument and returns a simplified tree.
34
35 size_binop takes a tree code for an arithmetic operation
36 and two operands that are trees, and produces a tree for the
37 result, assuming the type comes from `sizetype'.
38
39 size_int takes an integer value, and creates a tree constant
0da6f3db
DE
40 with type from `sizetype'.
41
42 force_fit_type takes a constant and prior overflow indicator, and
43 forces the value to fit the type. It returns an overflow indicator. */
44
e9a25f70 45#include "config.h"
2fde567e 46#include "system.h"
6d716ca8
RS
47#include "flags.h"
48#include "tree.h"
efe3eb65 49#include "rtl.h"
0e9295cf 50#include "expr.h"
6baf1cc8 51#include "tm_p.h"
10f0ad3d 52#include "toplev.h"
a3770a81 53#include "ggc.h"
6d716ca8 54
711d877c 55static void encode PARAMS ((HOST_WIDE_INT *,
05bccae2
RK
56 unsigned HOST_WIDE_INT,
57 HOST_WIDE_INT));
711d877c 58static void decode PARAMS ((HOST_WIDE_INT *,
05bccae2 59 unsigned HOST_WIDE_INT *,
711d877c 60 HOST_WIDE_INT *));
94aca342
ZW
61#ifndef REAL_ARITHMETIC
62static void exact_real_inverse_1 PARAMS ((PTR));
63#endif
711d877c
KG
64static tree negate_expr PARAMS ((tree));
65static tree split_tree PARAMS ((tree, enum tree_code, tree *, tree *,
66 int));
67static tree associate_trees PARAMS ((tree, tree, enum tree_code, tree));
68static tree int_const_binop PARAMS ((enum tree_code, tree, tree, int, int));
69static void const_binop_1 PARAMS ((PTR));
70static tree const_binop PARAMS ((enum tree_code, tree, tree, int));
71static void fold_convert_1 PARAMS ((PTR));
72static tree fold_convert PARAMS ((tree, tree));
73static enum tree_code invert_tree_comparison PARAMS ((enum tree_code));
74static enum tree_code swap_tree_comparison PARAMS ((enum tree_code));
75static int truth_value_p PARAMS ((enum tree_code));
76static int operand_equal_for_comparison_p PARAMS ((tree, tree, tree));
77static int twoval_comparison_p PARAMS ((tree, tree *, tree *, int *));
78static tree eval_subst PARAMS ((tree, tree, tree, tree, tree));
79static tree omit_one_operand PARAMS ((tree, tree, tree));
80static tree pedantic_omit_one_operand PARAMS ((tree, tree, tree));
81static tree distribute_bit_expr PARAMS ((enum tree_code, tree, tree, tree));
82static tree make_bit_field_ref PARAMS ((tree, tree, int, int, int));
83static tree optimize_bit_field_compare PARAMS ((enum tree_code, tree,
84 tree, tree));
770ae6cc
RK
85static tree decode_field_reference PARAMS ((tree, HOST_WIDE_INT *,
86 HOST_WIDE_INT *,
711d877c
KG
87 enum machine_mode *, int *,
88 int *, tree *, tree *));
89static int all_ones_mask_p PARAMS ((tree, int));
90static int simple_operand_p PARAMS ((tree));
91static tree range_binop PARAMS ((enum tree_code, tree, tree, int,
92 tree, int));
93static tree make_range PARAMS ((tree, int *, tree *, tree *));
94static tree build_range_check PARAMS ((tree, tree, int, tree, tree));
95static int merge_ranges PARAMS ((int *, tree *, tree *, int, tree, tree,
ebde8a27 96 int, tree, tree));
711d877c
KG
97static tree fold_range_test PARAMS ((tree));
98static tree unextend PARAMS ((tree, int, int, tree));
99static tree fold_truthop PARAMS ((enum tree_code, tree, tree, tree));
100static tree optimize_minmax_comparison PARAMS ((tree));
101static tree extract_muldiv PARAMS ((tree, tree, enum tree_code, tree));
102static tree strip_compound_expr PARAMS ((tree, tree));
103static int multiple_of_p PARAMS ((tree, tree, tree));
104static tree constant_boolean_node PARAMS ((int, tree));
105static int count_cond PARAMS ((tree, int));
68626d4f
MM
106static tree fold_binary_op_with_conditional_arg
107 PARAMS ((enum tree_code, tree, tree, tree, int));
108
d35357ed
RS
109#ifndef BRANCH_COST
110#define BRANCH_COST 1
111#endif
fe3e8e40 112
5f6d3823
DP
113#if defined(HOST_EBCDIC)
114/* bit 8 is significant in EBCDIC */
115#define CHARMASK 0xff
116#else
117#define CHARMASK 0x7f
118#endif
119
d4b60170
RK
120/* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
121 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
122 and SUM1. Then this yields nonzero if overflow occurred during the
123 addition.
124
125 Overflow occurs if A and B have the same sign, but A and SUM differ in
126 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
127 sign. */
128#define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
6d716ca8 129\f
906c4e36 130/* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
37bdb7e3 131 We do that by representing the two-word integer in 4 words, with only
d4b60170
RK
132 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
133 number. The value of the word is LOWPART + HIGHPART * BASE. */
37bdb7e3
TG
134
135#define LOWPART(x) \
d4b60170 136 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
37bdb7e3 137#define HIGHPART(x) \
d4b60170
RK
138 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
139#define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
6d716ca8 140
37bdb7e3 141/* Unpack a two-word integer into 4 words.
906c4e36 142 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
37bdb7e3 143 WORDS points to the array of HOST_WIDE_INTs. */
6d716ca8
RS
144
145static void
37bdb7e3
TG
146encode (words, low, hi)
147 HOST_WIDE_INT *words;
05bccae2
RK
148 unsigned HOST_WIDE_INT low;
149 HOST_WIDE_INT hi;
6d716ca8 150{
37bdb7e3
TG
151 words[0] = LOWPART (low);
152 words[1] = HIGHPART (low);
153 words[2] = LOWPART (hi);
154 words[3] = HIGHPART (hi);
6d716ca8
RS
155}
156
37bdb7e3
TG
157/* Pack an array of 4 words into a two-word integer.
158 WORDS points to the array of words.
906c4e36 159 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
6d716ca8
RS
160
161static void
37bdb7e3
TG
162decode (words, low, hi)
163 HOST_WIDE_INT *words;
05bccae2
RK
164 unsigned HOST_WIDE_INT *low;
165 HOST_WIDE_INT *hi;
6d716ca8 166{
d4b60170
RK
167 *low = words[0] + words[1] * BASE;
168 *hi = words[2] + words[3] * BASE;
6d716ca8
RS
169}
170\f
d4b60170
RK
171/* Make the integer constant T valid for its type by setting to 0 or 1 all
172 the bits in the constant that don't belong in the type.
173
174 Return 1 if a signed overflow occurs, 0 otherwise. If OVERFLOW is
175 nonzero, a signed overflow has already occurred in calculating T, so
176 propagate it.
649ff3b4
RK
177
178 Make the real constant T valid for its type by calling CHECK_FLOAT_VALUE,
179 if it exists. */
6d716ca8 180
e0f776fb
RS
181int
182force_fit_type (t, overflow)
6d716ca8 183 tree t;
e0f776fb 184 int overflow;
6d716ca8 185{
05bccae2
RK
186 unsigned HOST_WIDE_INT low;
187 HOST_WIDE_INT high;
188 unsigned int prec;
6d716ca8 189
649ff3b4
RK
190 if (TREE_CODE (t) == REAL_CST)
191 {
192#ifdef CHECK_FLOAT_VALUE
193 CHECK_FLOAT_VALUE (TYPE_MODE (TREE_TYPE (t)), TREE_REAL_CST (t),
194 overflow);
195#endif
196 return overflow;
197 }
198
199 else if (TREE_CODE (t) != INTEGER_CST)
ef2bf0c0
RS
200 return overflow;
201
202 low = TREE_INT_CST_LOW (t);
203 high = TREE_INT_CST_HIGH (t);
42f769c1 204
e5e809f4 205 if (POINTER_TYPE_P (TREE_TYPE (t)))
6d716ca8 206 prec = POINTER_SIZE;
ef2bf0c0
RS
207 else
208 prec = TYPE_PRECISION (TREE_TYPE (t));
6d716ca8
RS
209
210 /* First clear all bits that are beyond the type's precision. */
211
906c4e36 212 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
6d716ca8 213 ;
906c4e36 214 else if (prec > HOST_BITS_PER_WIDE_INT)
d4b60170
RK
215 TREE_INT_CST_HIGH (t)
216 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
6d716ca8
RS
217 else
218 {
219 TREE_INT_CST_HIGH (t) = 0;
906c4e36 220 if (prec < HOST_BITS_PER_WIDE_INT)
05bccae2 221 TREE_INT_CST_LOW (t) &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
6d716ca8
RS
222 }
223
44475138
RK
224 /* Unsigned types do not suffer sign extension or overflow unless they
225 are a sizetype. */
226 if (TREE_UNSIGNED (TREE_TYPE (t))
227 && ! (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
228 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
a7a05640 229 return overflow;
6d716ca8 230
e0f776fb
RS
231 /* If the value's sign bit is set, extend the sign. */
232 if (prec != 2 * HOST_BITS_PER_WIDE_INT
906c4e36 233 && (prec > HOST_BITS_PER_WIDE_INT
05bccae2
RK
234 ? 0 != (TREE_INT_CST_HIGH (t)
235 & ((HOST_WIDE_INT) 1
236 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
237 : 0 != (TREE_INT_CST_LOW (t)
238 & ((unsigned HOST_WIDE_INT) 1 << (prec - 1)))))
6d716ca8
RS
239 {
240 /* Value is negative:
241 set to 1 all the bits that are outside this type's precision. */
906c4e36 242 if (prec > HOST_BITS_PER_WIDE_INT)
d4b60170
RK
243 TREE_INT_CST_HIGH (t)
244 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
6d716ca8
RS
245 else
246 {
247 TREE_INT_CST_HIGH (t) = -1;
906c4e36 248 if (prec < HOST_BITS_PER_WIDE_INT)
05bccae2 249 TREE_INT_CST_LOW (t) |= ((unsigned HOST_WIDE_INT) (-1) << prec);
6d716ca8
RS
250 }
251 }
e0f776fb 252
d4b60170 253 /* Return nonzero if signed overflow occurred. */
e0f776fb
RS
254 return
255 ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
256 != 0);
6d716ca8
RS
257}
258\f
906c4e36
RK
259/* Add two doubleword integers with doubleword result.
260 Each argument is given as two `HOST_WIDE_INT' pieces.
6d716ca8 261 One argument is L1 and H1; the other, L2 and H2.
37bdb7e3 262 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8 263
fe3e8e40 264int
6d716ca8 265add_double (l1, h1, l2, h2, lv, hv)
05bccae2
RK
266 unsigned HOST_WIDE_INT l1, l2;
267 HOST_WIDE_INT h1, h2;
268 unsigned HOST_WIDE_INT *lv;
269 HOST_WIDE_INT *hv;
6d716ca8 270{
05bccae2
RK
271 unsigned HOST_WIDE_INT l;
272 HOST_WIDE_INT h;
6d716ca8 273
37bdb7e3 274 l = l1 + l2;
05bccae2 275 h = h1 + h2 + (l < l1);
6d716ca8 276
37bdb7e3
TG
277 *lv = l;
278 *hv = h;
d4b60170 279 return OVERFLOW_SUM_SIGN (h1, h2, h);
6d716ca8
RS
280}
281
906c4e36 282/* Negate a doubleword integer with doubleword result.
fe3e8e40 283 Return nonzero if the operation overflows, assuming it's signed.
906c4e36 284 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
37bdb7e3 285 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8 286
fe3e8e40 287int
6d716ca8 288neg_double (l1, h1, lv, hv)
05bccae2
RK
289 unsigned HOST_WIDE_INT l1;
290 HOST_WIDE_INT h1;
291 unsigned HOST_WIDE_INT *lv;
292 HOST_WIDE_INT *hv;
6d716ca8
RS
293{
294 if (l1 == 0)
295 {
296 *lv = 0;
297 *hv = - h1;
e0f776fb 298 return (*hv & h1) < 0;
6d716ca8
RS
299 }
300 else
301 {
b6cc0a72
KH
302 *lv = -l1;
303 *hv = ~h1;
fe3e8e40 304 return 0;
6d716ca8
RS
305 }
306}
307\f
906c4e36 308/* Multiply two doubleword integers with doubleword result.
fe3e8e40 309 Return nonzero if the operation overflows, assuming it's signed.
906c4e36 310 Each argument is given as two `HOST_WIDE_INT' pieces.
6d716ca8 311 One argument is L1 and H1; the other, L2 and H2.
37bdb7e3 312 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8 313
fe3e8e40 314int
6d716ca8 315mul_double (l1, h1, l2, h2, lv, hv)
05bccae2
RK
316 unsigned HOST_WIDE_INT l1, l2;
317 HOST_WIDE_INT h1, h2;
318 unsigned HOST_WIDE_INT *lv;
319 HOST_WIDE_INT *hv;
6d716ca8 320{
37bdb7e3
TG
321 HOST_WIDE_INT arg1[4];
322 HOST_WIDE_INT arg2[4];
323 HOST_WIDE_INT prod[4 * 2];
324 register unsigned HOST_WIDE_INT carry;
6d716ca8 325 register int i, j, k;
05bccae2
RK
326 unsigned HOST_WIDE_INT toplow, neglow;
327 HOST_WIDE_INT tophigh, neghigh;
6d716ca8 328
6d716ca8
RS
329 encode (arg1, l1, h1);
330 encode (arg2, l2, h2);
331
961192e1 332 memset ((char *) prod, 0, sizeof prod);
6d716ca8 333
37bdb7e3
TG
334 for (i = 0; i < 4; i++)
335 {
336 carry = 0;
337 for (j = 0; j < 4; j++)
338 {
339 k = i + j;
340 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
341 carry += arg1[i] * arg2[j];
342 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
343 carry += prod[k];
344 prod[k] = LOWPART (carry);
345 carry = HIGHPART (carry);
346 }
347 prod[i + 4] = carry;
348 }
6d716ca8 349
37bdb7e3 350 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
fe3e8e40
RS
351
352 /* Check for overflow by calculating the top half of the answer in full;
353 it should agree with the low half's sign bit. */
b6cc0a72 354 decode (prod + 4, &toplow, &tophigh);
fe3e8e40
RS
355 if (h1 < 0)
356 {
357 neg_double (l2, h2, &neglow, &neghigh);
358 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
359 }
360 if (h2 < 0)
361 {
362 neg_double (l1, h1, &neglow, &neghigh);
363 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
364 }
365 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
6d716ca8
RS
366}
367\f
906c4e36 368/* Shift the doubleword integer in L1, H1 left by COUNT places
6d716ca8
RS
369 keeping only PREC bits of result.
370 Shift right if COUNT is negative.
371 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
906c4e36 372 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8 373
e0f776fb 374void
6d716ca8 375lshift_double (l1, h1, count, prec, lv, hv, arith)
05bccae2
RK
376 unsigned HOST_WIDE_INT l1;
377 HOST_WIDE_INT h1, count;
378 unsigned int prec;
379 unsigned HOST_WIDE_INT *lv;
380 HOST_WIDE_INT *hv;
6d716ca8
RS
381 int arith;
382{
6d716ca8
RS
383 if (count < 0)
384 {
b6cc0a72 385 rshift_double (l1, h1, -count, prec, lv, hv, arith);
e0f776fb 386 return;
6d716ca8 387 }
b6cc0a72 388
3d1877b1
RK
389#ifdef SHIFT_COUNT_TRUNCATED
390 if (SHIFT_COUNT_TRUNCATED)
391 count %= prec;
392#endif
6d716ca8 393
cb0a34c4
JW
394 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
395 {
396 /* Shifting by the host word size is undefined according to the
397 ANSI standard, so we must handle this as a special case. */
398 *hv = 0;
399 *lv = 0;
400 }
401 else if (count >= HOST_BITS_PER_WIDE_INT)
6d716ca8 402 {
05bccae2 403 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
37bdb7e3
TG
404 *lv = 0;
405 }
406 else
407 {
408 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
05bccae2
RK
409 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
410 *lv = l1 << count;
6d716ca8 411 }
6d716ca8
RS
412}
413
906c4e36 414/* Shift the doubleword integer in L1, H1 right by COUNT places
6d716ca8
RS
415 keeping only PREC bits of result. COUNT must be positive.
416 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
906c4e36 417 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8
RS
418
419void
420rshift_double (l1, h1, count, prec, lv, hv, arith)
05bccae2
RK
421 unsigned HOST_WIDE_INT l1;
422 HOST_WIDE_INT h1, count;
423 unsigned int prec ATTRIBUTE_UNUSED;
424 unsigned HOST_WIDE_INT *lv;
425 HOST_WIDE_INT *hv;
6d716ca8
RS
426 int arith;
427{
37bdb7e3 428 unsigned HOST_WIDE_INT signmask;
05bccae2 429
37bdb7e3
TG
430 signmask = (arith
431 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
432 : 0);
6d716ca8 433
3d1877b1
RK
434#ifdef SHIFT_COUNT_TRUNCATED
435 if (SHIFT_COUNT_TRUNCATED)
436 count %= prec;
437#endif
6d716ca8 438
cb0a34c4
JW
439 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
440 {
441 /* Shifting by the host word size is undefined according to the
442 ANSI standard, so we must handle this as a special case. */
443 *hv = signmask;
444 *lv = signmask;
445 }
446 else if (count >= HOST_BITS_PER_WIDE_INT)
6d716ca8 447 {
37bdb7e3 448 *hv = signmask;
2fde567e
KG
449 *lv = ((signmask << (2 * HOST_BITS_PER_WIDE_INT - count - 1) << 1)
450 | ((unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT)));
37bdb7e3
TG
451 }
452 else
453 {
05bccae2 454 *lv = ((l1 >> count)
2fde567e
KG
455 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
456 *hv = ((signmask << (HOST_BITS_PER_WIDE_INT - count))
37bdb7e3 457 | ((unsigned HOST_WIDE_INT) h1 >> count));
6d716ca8 458 }
6d716ca8
RS
459}
460\f
37bdb7e3 461/* Rotate the doubleword integer in L1, H1 left by COUNT places
6d716ca8
RS
462 keeping only PREC bits of result.
463 Rotate right if COUNT is negative.
906c4e36 464 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8
RS
465
466void
467lrotate_double (l1, h1, count, prec, lv, hv)
05bccae2
RK
468 unsigned HOST_WIDE_INT l1;
469 HOST_WIDE_INT h1, count;
470 unsigned int prec;
471 unsigned HOST_WIDE_INT *lv;
472 HOST_WIDE_INT *hv;
6d716ca8 473{
05bccae2
RK
474 unsigned HOST_WIDE_INT s1l, s2l;
475 HOST_WIDE_INT s1h, s2h;
6d716ca8 476
4d39710e 477 count %= prec;
6d716ca8 478 if (count < 0)
4d39710e 479 count += prec;
6d716ca8 480
4d39710e
RK
481 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
482 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
483 *lv = s1l | s2l;
484 *hv = s1h | s2h;
6d716ca8
RS
485}
486
906c4e36 487/* Rotate the doubleword integer in L1, H1 left by COUNT places
6d716ca8 488 keeping only PREC bits of result. COUNT must be positive.
906c4e36 489 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8
RS
490
491void
492rrotate_double (l1, h1, count, prec, lv, hv)
05bccae2
RK
493 unsigned HOST_WIDE_INT l1;
494 HOST_WIDE_INT h1, count;
495 unsigned int prec;
496 unsigned HOST_WIDE_INT *lv;
497 HOST_WIDE_INT *hv;
6d716ca8 498{
05bccae2
RK
499 unsigned HOST_WIDE_INT s1l, s2l;
500 HOST_WIDE_INT s1h, s2h;
6d716ca8 501
4d39710e
RK
502 count %= prec;
503 if (count < 0)
504 count += prec;
6d716ca8 505
4d39710e
RK
506 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
507 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
508 *lv = s1l | s2l;
509 *hv = s1h | s2h;
6d716ca8
RS
510}
511\f
906c4e36 512/* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
6d716ca8
RS
513 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
514 CODE is a tree code for a kind of division, one of
515 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
516 or EXACT_DIV_EXPR
fe3e8e40
RS
517 It controls how the quotient is rounded to a integer.
518 Return nonzero if the operation overflows.
6d716ca8
RS
519 UNS nonzero says do unsigned division. */
520
dbb5b3ce 521int
6d716ca8
RS
522div_and_round_double (code, uns,
523 lnum_orig, hnum_orig, lden_orig, hden_orig,
524 lquo, hquo, lrem, hrem)
525 enum tree_code code;
526 int uns;
05bccae2
RK
527 unsigned HOST_WIDE_INT lnum_orig; /* num == numerator == dividend */
528 HOST_WIDE_INT hnum_orig;
529 unsigned HOST_WIDE_INT lden_orig; /* den == denominator == divisor */
530 HOST_WIDE_INT hden_orig;
531 unsigned HOST_WIDE_INT *lquo, *lrem;
532 HOST_WIDE_INT *hquo, *hrem;
6d716ca8
RS
533{
534 int quo_neg = 0;
37bdb7e3
TG
535 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
536 HOST_WIDE_INT den[4], quo[4];
537 register int i, j;
538 unsigned HOST_WIDE_INT work;
05bccae2
RK
539 unsigned HOST_WIDE_INT carry = 0;
540 unsigned HOST_WIDE_INT lnum = lnum_orig;
b8eb43a2 541 HOST_WIDE_INT hnum = hnum_orig;
05bccae2 542 unsigned HOST_WIDE_INT lden = lden_orig;
b8eb43a2 543 HOST_WIDE_INT hden = hden_orig;
fe3e8e40 544 int overflow = 0;
6d716ca8 545
05bccae2 546 if (hden == 0 && lden == 0)
956d6950 547 overflow = 1, lden = 1;
6d716ca8
RS
548
549 /* calculate quotient sign and convert operands to unsigned. */
b6cc0a72 550 if (!uns)
6d716ca8 551 {
fe3e8e40 552 if (hnum < 0)
6d716ca8
RS
553 {
554 quo_neg = ~ quo_neg;
fe3e8e40 555 /* (minimum integer) / (-1) is the only overflow case. */
05bccae2
RK
556 if (neg_double (lnum, hnum, &lnum, &hnum)
557 && ((HOST_WIDE_INT) lden & hden) == -1)
fe3e8e40 558 overflow = 1;
6d716ca8 559 }
b6cc0a72 560 if (hden < 0)
6d716ca8
RS
561 {
562 quo_neg = ~ quo_neg;
fe3e8e40 563 neg_double (lden, hden, &lden, &hden);
6d716ca8
RS
564 }
565 }
566
567 if (hnum == 0 && hden == 0)
568 { /* single precision */
569 *hquo = *hrem = 0;
88ee2651 570 /* This unsigned division rounds toward zero. */
05bccae2 571 *lquo = lnum / lden;
6d716ca8
RS
572 goto finish_up;
573 }
574
575 if (hnum == 0)
576 { /* trivial case: dividend < divisor */
577 /* hden != 0 already checked. */
578 *hquo = *lquo = 0;
579 *hrem = hnum;
580 *lrem = lnum;
581 goto finish_up;
582 }
583
961192e1 584 memset ((char *) quo, 0, sizeof quo);
6d716ca8 585
961192e1
JM
586 memset ((char *) num, 0, sizeof num); /* to zero 9th element */
587 memset ((char *) den, 0, sizeof den);
6d716ca8 588
b6cc0a72 589 encode (num, lnum, hnum);
6d716ca8
RS
590 encode (den, lden, hden);
591
37bdb7e3 592 /* Special code for when the divisor < BASE. */
05bccae2 593 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
37bdb7e3 594 {
6d716ca8 595 /* hnum != 0 already checked. */
37bdb7e3 596 for (i = 4 - 1; i >= 0; i--)
6d716ca8 597 {
37bdb7e3 598 work = num[i] + carry * BASE;
05bccae2
RK
599 quo[i] = work / lden;
600 carry = work % lden;
6d716ca8
RS
601 }
602 }
37bdb7e3
TG
603 else
604 {
605 /* Full double precision division,
606 with thanks to Don Knuth's "Seminumerical Algorithms". */
05bccae2
RK
607 int num_hi_sig, den_hi_sig;
608 unsigned HOST_WIDE_INT quo_est, scale;
6d716ca8 609
05bccae2 610 /* Find the highest non-zero divisor digit. */
b6cc0a72
KH
611 for (i = 4 - 1;; i--)
612 if (den[i] != 0)
613 {
614 den_hi_sig = i;
615 break;
616 }
37bdb7e3 617
05bccae2
RK
618 /* Insure that the first digit of the divisor is at least BASE/2.
619 This is required by the quotient digit estimation algorithm. */
6d716ca8 620
05bccae2
RK
621 scale = BASE / (den[den_hi_sig] + 1);
622 if (scale > 1)
623 { /* scale divisor and dividend */
624 carry = 0;
625 for (i = 0; i <= 4 - 1; i++)
626 {
627 work = (num[i] * scale) + carry;
628 num[i] = LOWPART (work);
629 carry = HIGHPART (work);
630 }
6d716ca8 631
05bccae2
RK
632 num[4] = carry;
633 carry = 0;
634 for (i = 0; i <= 4 - 1; i++)
635 {
636 work = (den[i] * scale) + carry;
637 den[i] = LOWPART (work);
638 carry = HIGHPART (work);
639 if (den[i] != 0) den_hi_sig = i;
640 }
641 }
6d716ca8 642
05bccae2 643 num_hi_sig = 4;
6d716ca8 644
05bccae2
RK
645 /* Main loop */
646 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
6d716ca8 647 {
05bccae2
RK
648 /* Guess the next quotient digit, quo_est, by dividing the first
649 two remaining dividend digits by the high order quotient digit.
650 quo_est is never low and is at most 2 high. */
651 unsigned HOST_WIDE_INT tmp;
652
653 num_hi_sig = i + den_hi_sig + 1;
654 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
655 if (num[num_hi_sig] != den[den_hi_sig])
656 quo_est = work / den[den_hi_sig];
657 else
658 quo_est = BASE - 1;
6d716ca8 659
05bccae2
RK
660 /* Refine quo_est so it's usually correct, and at most one high. */
661 tmp = work - quo_est * den[den_hi_sig];
662 if (tmp < BASE
663 && (den[den_hi_sig - 1] * quo_est
664 > (tmp * BASE + num[num_hi_sig - 2])))
665 quo_est--;
6d716ca8 666
05bccae2
RK
667 /* Try QUO_EST as the quotient digit, by multiplying the
668 divisor by QUO_EST and subtracting from the remaining dividend.
669 Keep in mind that QUO_EST is the I - 1st digit. */
670
671 carry = 0;
6d716ca8
RS
672 for (j = 0; j <= den_hi_sig; j++)
673 {
05bccae2 674 work = quo_est * den[j] + carry;
37bdb7e3 675 carry = HIGHPART (work);
05bccae2 676 work = num[i + j] - LOWPART (work);
37bdb7e3 677 num[i + j] = LOWPART (work);
05bccae2 678 carry += HIGHPART (work) != 0;
6d716ca8 679 }
6d716ca8 680
05bccae2
RK
681 /* If quo_est was high by one, then num[i] went negative and
682 we need to correct things. */
683 if (num[num_hi_sig] < carry)
684 {
685 quo_est--;
686 carry = 0; /* add divisor back in */
687 for (j = 0; j <= den_hi_sig; j++)
688 {
689 work = num[i + j] + den[j] + carry;
690 carry = HIGHPART (work);
691 num[i + j] = LOWPART (work);
692 }
693
694 num [num_hi_sig] += carry;
695 }
696
697 /* Store the quotient digit. */
698 quo[i] = quo_est;
699 }
6d716ca8 700 }
6d716ca8
RS
701
702 decode (quo, lquo, hquo);
703
704 finish_up:
705 /* if result is negative, make it so. */
706 if (quo_neg)
707 neg_double (*lquo, *hquo, lquo, hquo);
708
709 /* compute trial remainder: rem = num - (quo * den) */
710 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
711 neg_double (*lrem, *hrem, lrem, hrem);
712 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
713
714 switch (code)
715 {
716 case TRUNC_DIV_EXPR:
717 case TRUNC_MOD_EXPR: /* round toward zero */
718 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
fe3e8e40 719 return overflow;
6d716ca8
RS
720
721 case FLOOR_DIV_EXPR:
722 case FLOOR_MOD_EXPR: /* round toward negative infinity */
723 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
724 {
725 /* quo = quo - 1; */
906c4e36
RK
726 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
727 lquo, hquo);
6d716ca8 728 }
05bccae2
RK
729 else
730 return overflow;
6d716ca8
RS
731 break;
732
733 case CEIL_DIV_EXPR:
734 case CEIL_MOD_EXPR: /* round toward positive infinity */
735 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
736 {
906c4e36
RK
737 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
738 lquo, hquo);
6d716ca8 739 }
05bccae2
RK
740 else
741 return overflow;
6d716ca8 742 break;
b6cc0a72 743
6d716ca8
RS
744 case ROUND_DIV_EXPR:
745 case ROUND_MOD_EXPR: /* round to closest integer */
746 {
05bccae2
RK
747 unsigned HOST_WIDE_INT labs_rem = *lrem;
748 HOST_WIDE_INT habs_rem = *hrem;
749 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
750 HOST_WIDE_INT habs_den = hden, htwice;
751
752 /* Get absolute values */
753 if (*hrem < 0)
754 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
755 if (hden < 0)
756 neg_double (lden, hden, &labs_den, &habs_den);
757
758 /* If (2 * abs (lrem) >= abs (lden)) */
906c4e36
RK
759 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
760 labs_rem, habs_rem, &ltwice, &htwice);
05bccae2 761
906c4e36
RK
762 if (((unsigned HOST_WIDE_INT) habs_den
763 < (unsigned HOST_WIDE_INT) htwice)
764 || (((unsigned HOST_WIDE_INT) habs_den
765 == (unsigned HOST_WIDE_INT) htwice)
05bccae2 766 && (labs_den < ltwice)))
6d716ca8
RS
767 {
768 if (*hquo < 0)
769 /* quo = quo - 1; */
906c4e36
RK
770 add_double (*lquo, *hquo,
771 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
6d716ca8
RS
772 else
773 /* quo = quo + 1; */
906c4e36
RK
774 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
775 lquo, hquo);
6d716ca8 776 }
05bccae2
RK
777 else
778 return overflow;
6d716ca8
RS
779 }
780 break;
781
782 default:
783 abort ();
784 }
785
786 /* compute true remainder: rem = num - (quo * den) */
787 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
788 neg_double (*lrem, *hrem, lrem, hrem);
789 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
fe3e8e40 790 return overflow;
6d716ca8
RS
791}
792\f
5008b8ae 793#ifndef REAL_ARITHMETIC
649ff3b4
RK
794/* Effectively truncate a real value to represent the nearest possible value
795 in a narrower mode. The result is actually represented in the same data
796 type as the argument, but its value is usually different.
797
798 A trap may occur during the FP operations and it is the responsibility
799 of the calling function to have a handler established. */
5352b11a
RS
800
801REAL_VALUE_TYPE
802real_value_truncate (mode, arg)
803 enum machine_mode mode;
804 REAL_VALUE_TYPE arg;
805{
649ff3b4 806 return REAL_VALUE_TRUNCATE (mode, arg);
5352b11a
RS
807}
808
6d716ca8
RS
809#if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
810
811/* Check for infinity in an IEEE double precision number. */
812
813int
814target_isinf (x)
815 REAL_VALUE_TYPE x;
816{
817 /* The IEEE 64-bit double format. */
818 union {
819 REAL_VALUE_TYPE d;
820 struct {
821 unsigned sign : 1;
822 unsigned exponent : 11;
823 unsigned mantissa1 : 20;
824 unsigned mantissa2;
825 } little_endian;
826 struct {
827 unsigned mantissa2;
828 unsigned mantissa1 : 20;
829 unsigned exponent : 11;
830 unsigned sign : 1;
b6cc0a72 831 } big_endian;
6d716ca8
RS
832 } u;
833
834 u.d = dconstm1;
835 if (u.big_endian.sign == 1)
836 {
837 u.d = x;
838 return (u.big_endian.exponent == 2047
839 && u.big_endian.mantissa1 == 0
840 && u.big_endian.mantissa2 == 0);
841 }
842 else
843 {
844 u.d = x;
845 return (u.little_endian.exponent == 2047
846 && u.little_endian.mantissa1 == 0
847 && u.little_endian.mantissa2 == 0);
848 }
849}
850
7d4d4d22
RS
851/* Check whether an IEEE double precision number is a NaN. */
852
853int
854target_isnan (x)
855 REAL_VALUE_TYPE x;
856{
857 /* The IEEE 64-bit double format. */
858 union {
859 REAL_VALUE_TYPE d;
860 struct {
861 unsigned sign : 1;
862 unsigned exponent : 11;
863 unsigned mantissa1 : 20;
864 unsigned mantissa2;
865 } little_endian;
866 struct {
867 unsigned mantissa2;
868 unsigned mantissa1 : 20;
869 unsigned exponent : 11;
870 unsigned sign : 1;
b6cc0a72 871 } big_endian;
7d4d4d22
RS
872 } u;
873
874 u.d = dconstm1;
875 if (u.big_endian.sign == 1)
876 {
877 u.d = x;
878 return (u.big_endian.exponent == 2047
879 && (u.big_endian.mantissa1 != 0
880 || u.big_endian.mantissa2 != 0));
881 }
882 else
883 {
884 u.d = x;
885 return (u.little_endian.exponent == 2047
886 && (u.little_endian.mantissa1 != 0
887 || u.little_endian.mantissa2 != 0));
888 }
889}
890
c05a9b68 891/* Check for a negative IEEE double precision number. */
6d716ca8
RS
892
893int
c05a9b68 894target_negative (x)
6d716ca8
RS
895 REAL_VALUE_TYPE x;
896{
c05a9b68
RS
897 /* The IEEE 64-bit double format. */
898 union {
899 REAL_VALUE_TYPE d;
900 struct {
901 unsigned sign : 1;
902 unsigned exponent : 11;
903 unsigned mantissa1 : 20;
904 unsigned mantissa2;
905 } little_endian;
906 struct {
907 unsigned mantissa2;
908 unsigned mantissa1 : 20;
909 unsigned exponent : 11;
910 unsigned sign : 1;
b6cc0a72 911 } big_endian;
c05a9b68 912 } u;
6d716ca8 913
c05a9b68
RS
914 u.d = dconstm1;
915 if (u.big_endian.sign == 1)
916 {
917 u.d = x;
918 return u.big_endian.sign;
919 }
920 else
921 {
922 u.d = x;
923 return u.little_endian.sign;
924 }
6d716ca8
RS
925}
926#else /* Target not IEEE */
927
928/* Let's assume other float formats don't have infinity.
929 (This can be overridden by redefining REAL_VALUE_ISINF.) */
930
7762d54e 931int
6d716ca8 932target_isinf (x)
962f1324 933 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
6d716ca8
RS
934{
935 return 0;
936}
937
7d4d4d22
RS
938/* Let's assume other float formats don't have NaNs.
939 (This can be overridden by redefining REAL_VALUE_ISNAN.) */
940
7762d54e 941int
7d4d4d22 942target_isnan (x)
962f1324 943 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
7d4d4d22
RS
944{
945 return 0;
946}
947
6d716ca8 948/* Let's assume other float formats don't have minus zero.
c05a9b68 949 (This can be overridden by redefining REAL_VALUE_NEGATIVE.) */
6d716ca8 950
7762d54e 951int
c05a9b68 952target_negative (x)
6d716ca8
RS
953 REAL_VALUE_TYPE x;
954{
c05a9b68 955 return x < 0;
6d716ca8
RS
956}
957#endif /* Target not IEEE */
39647dcb
RK
958
959/* Try to change R into its exact multiplicative inverse in machine mode
960 MODE. Return nonzero function value if successful. */
94aca342
ZW
961struct exact_real_inverse_args
962{
963 REAL_VALUE_TYPE *r;
964 enum machine_mode mode;
965 int success;
966};
39647dcb 967
94aca342
ZW
968static void
969exact_real_inverse_1 (p)
970 PTR p;
39647dcb 971{
94aca342
ZW
972 struct exact_real_inverse_args *args =
973 (struct exact_real_inverse_args *) p;
974
975 enum machine_mode mode = args->mode;
976 REAL_VALUE_TYPE *r = args->r;
977
39647dcb 978 union
94aca342
ZW
979 {
980 double d;
981 unsigned short i[4];
982 }
983 x, t, y;
7bdb32b9 984#ifdef CHECK_FLOAT_VALUE
39647dcb 985 int i;
7bdb32b9 986#endif
39647dcb 987
39647dcb 988 /* Set array index to the less significant bits in the unions, depending
94aca342
ZW
989 on the endian-ness of the host doubles. */
990#if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT \
991 || HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
992# define K 2
39647dcb 993#else
94aca342 994# define K (2 * HOST_FLOAT_WORDS_BIG_ENDIAN)
39647dcb
RK
995#endif
996
39647dcb
RK
997 /* Domain check the argument. */
998 x.d = *r;
999 if (x.d == 0.0)
1000 goto fail;
1001
1002#ifdef REAL_INFINITY
1003 if (REAL_VALUE_ISINF (x.d) || REAL_VALUE_ISNAN (x.d))
1004 goto fail;
1005#endif
1006
1007 /* Compute the reciprocal and check for numerical exactness.
1008 It is unnecessary to check all the significand bits to determine
1009 whether X is a power of 2. If X is not, then it is impossible for
1010 the bottom half significand of both X and 1/X to be all zero bits.
1011 Hence we ignore the data structure of the top half and examine only
1012 the low order bits of the two significands. */
1013 t.d = 1.0 / x.d;
1014 if (x.i[K] != 0 || x.i[K + 1] != 0 || t.i[K] != 0 || t.i[K + 1] != 0)
1015 goto fail;
1016
1017 /* Truncate to the required mode and range-check the result. */
1018 y.d = REAL_VALUE_TRUNCATE (mode, t.d);
1019#ifdef CHECK_FLOAT_VALUE
1020 i = 0;
1021 if (CHECK_FLOAT_VALUE (mode, y.d, i))
1022 goto fail;
1023#endif
1024
1025 /* Fail if truncation changed the value. */
1026 if (y.d != t.d || y.d == 0.0)
1027 goto fail;
1028
1029#ifdef REAL_INFINITY
1030 if (REAL_VALUE_ISINF (y.d) || REAL_VALUE_ISNAN (y.d))
1031 goto fail;
1032#endif
1033
1034 /* Output the reciprocal and return success flag. */
39647dcb 1035 *r = y.d;
94aca342
ZW
1036 args->success = 1;
1037 return;
1038
1039 fail:
1040 args->success = 0;
1041 return;
1042
1043#undef K
1044}
1045
1046
1047int
1048exact_real_inverse (mode, r)
1049 enum machine_mode mode;
1050 REAL_VALUE_TYPE *r;
1051{
1052 struct exact_real_inverse_args args;
1053
1054 /* Disable if insufficient information on the data structure. */
1055#if HOST_FLOAT_FORMAT == UNKNOWN_FLOAT_FORMAT
1056 return 0;
1057#endif
1058
1059 /* Usually disable if bounds checks are not reliable. */
1060 if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT) && !flag_pretend_float)
1061 return 0;
1062
1063 args.mode = mode;
1064 args.r = r;
1065
1066 if (do_float_handler (exact_real_inverse_1, (PTR) &args))
1067 return args.success;
1068 return 0;
39647dcb 1069}
6f4d7222 1070
526aba28 1071/* Convert C99 hexadecimal floating point string constant S. Return
6f4d7222 1072 real value type in mode MODE. This function uses the host computer's
d4b60170 1073 floating point arithmetic when there is no REAL_ARITHMETIC. */
6f4d7222
UD
1074
1075REAL_VALUE_TYPE
1076real_hex_to_f (s, mode)
08c148a8 1077 const char *s;
6f4d7222
UD
1078 enum machine_mode mode;
1079{
b6cc0a72 1080 REAL_VALUE_TYPE ip;
08c148a8 1081 const char *p = s;
b6cc0a72
KH
1082 unsigned HOST_WIDE_INT low, high;
1083 int shcount, nrmcount, k;
1084 int sign, expsign, isfloat;
1085 int lost = 0;/* Nonzero low order bits shifted out and discarded. */
1086 int frexpon = 0; /* Bits after the decimal point. */
1087 int expon = 0; /* Value of exponent. */
1088 int decpt = 0; /* How many decimal points. */
1089 int gotp = 0; /* How many P's. */
1090 char c;
1091
1092 isfloat = 0;
1093 expsign = 1;
1094 ip = 0.0;
1095
1096 while (*p == ' ' || *p == '\t')
1097 ++p;
1098
1099 /* Sign, if any, comes first. */
1100 sign = 1;
1101 if (*p == '-')
1102 {
1103 sign = -1;
1104 ++p;
1105 }
1106
1107 /* The string is supposed to start with 0x or 0X . */
1108 if (*p == '0')
1109 {
1110 ++p;
1111 if (*p == 'x' || *p == 'X')
1112 ++p;
1113 else
1114 abort ();
1115 }
1116 else
1117 abort ();
1118
1119 while (*p == '0')
1120 ++p;
1121
1122 high = 0;
1123 low = 0;
1124 shcount = 0;
1125 while ((c = *p) != '\0')
1126 {
1127 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')
1128 || (c >= 'a' && c <= 'f'))
1129 {
1130 k = c & CHARMASK;
1131 if (k >= 'a' && k <= 'f')
1132 k = k - 'a' + 10;
1133 else if (k >= 'A')
1134 k = k - 'A' + 10;
1135 else
1136 k = k - '0';
1137
1138 if ((high & 0xf0000000) == 0)
1139 {
1140 high = (high << 4) + ((low >> 28) & 15);
1141 low = (low << 4) + k;
1142 shcount += 4;
1143 if (decpt)
1144 frexpon += 4;
1145 }
1146 else
1147 {
1148 /* Record nonzero lost bits. */
1149 lost |= k;
1150 if (! decpt)
1151 frexpon -= 4;
1152 }
1153 ++p;
1154 }
1155 else if (c == '.')
1156 {
1157 ++decpt;
1158 ++p;
1159 }
1160
1161 else if (c == 'p' || c == 'P')
1162 {
1163 ++gotp;
1164 ++p;
1165 /* Sign of exponent. */
1166 if (*p == '-')
1167 {
1168 expsign = -1;
1169 ++p;
1170 }
1171
1172 /* Value of exponent.
1173 The exponent field is a decimal integer. */
1174 while (ISDIGIT (*p))
1175 {
1176 k = (*p++ & CHARMASK) - '0';
1177 expon = 10 * expon + k;
1178 }
1179
1180 expon *= expsign;
1181 /* F suffix is ambiguous in the significand part
1182 so it must appear after the decimal exponent field. */
1183 if (*p == 'f' || *p == 'F')
1184 {
1185 isfloat = 1;
1186 ++p;
1187 break;
1188 }
1189 }
1190
1191 else if (c == 'l' || c == 'L')
1192 {
1193 ++p;
1194 break;
1195 }
1196 else
1197 break;
1198 }
1199
1200 /* Abort if last character read was not legitimate. */
1201 c = *p;
1202 if ((c != '\0' && c != ' ' && c != '\n' && c != '\r') || (decpt > 1))
1203 abort ();
1204
1205 /* There must be either one decimal point or one p. */
1206 if (decpt == 0 && gotp == 0)
1207 abort ();
1208
1209 shcount -= 4;
1210 if (high == 0 && low == 0)
1211 return dconst0;
1212
1213 /* Normalize. */
1214 nrmcount = 0;
1215 if (high == 0)
1216 {
1217 high = low;
1218 low = 0;
1219 nrmcount += 32;
1220 }
1221
1222 /* Leave a high guard bit for carry-out. */
1223 if ((high & 0x80000000) != 0)
1224 {
1225 lost |= low & 1;
1226 low = (low >> 1) | (high << 31);
1227 high = high >> 1;
1228 nrmcount -= 1;
1229 }
1230
1231 if ((high & 0xffff8000) == 0)
1232 {
1233 high = (high << 16) + ((low >> 16) & 0xffff);
1234 low = low << 16;
1235 nrmcount += 16;
1236 }
1237
1238 while ((high & 0xc0000000) == 0)
1239 {
1240 high = (high << 1) + ((low >> 31) & 1);
1241 low = low << 1;
1242 nrmcount += 1;
1243 }
1244
1245 if (isfloat || GET_MODE_SIZE (mode) == UNITS_PER_WORD)
1246 {
1247 /* Keep 24 bits precision, bits 0x7fffff80.
1248 Rounding bit is 0x40. */
1249 lost = lost | low | (high & 0x3f);
1250 low = 0;
1251 if (high & 0x40)
1252 {
1253 if ((high & 0x80) || lost)
1254 high += 0x40;
1255 }
1256 high &= 0xffffff80;
1257 }
1258 else
1259 {
1260 /* We need real.c to do long double formats, so here default
1261 to double precision. */
6f4d7222 1262#if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
b6cc0a72
KH
1263 /* IEEE double.
1264 Keep 53 bits precision, bits 0x7fffffff fffffc00.
1265 Rounding bit is low word 0x200. */
1266 lost = lost | (low & 0x1ff);
1267 if (low & 0x200)
1268 {
1269 if ((low & 0x400) || lost)
1270 {
1271 low = (low + 0x200) & 0xfffffc00;
1272 if (low == 0)
1273 high += 1;
1274 }
1275 }
1276 low &= 0xfffffc00;
6f4d7222 1277#else
b6cc0a72
KH
1278 /* Assume it's a VAX with 56-bit significand,
1279 bits 0x7fffffff ffffff80. */
1280 lost = lost | (low & 0x7f);
1281 if (low & 0x40)
1282 {
1283 if ((low & 0x80) || lost)
1284 {
1285 low = (low + 0x40) & 0xffffff80;
1286 if (low == 0)
1287 high += 1;
1288 }
1289 }
1290 low &= 0xffffff80;
6f4d7222 1291#endif
b6cc0a72 1292 }
d4b60170 1293
b6cc0a72
KH
1294 ip = (double) high;
1295 ip = REAL_VALUE_LDEXP (ip, 32) + (double) low;
1296 /* Apply shifts and exponent value as power of 2. */
1297 ip = REAL_VALUE_LDEXP (ip, expon - (nrmcount + frexpon));
6f4d7222 1298
b6cc0a72
KH
1299 if (sign < 0)
1300 ip = -ip;
1301 return ip;
6f4d7222
UD
1302}
1303
5008b8ae 1304#endif /* no REAL_ARITHMETIC */
6d716ca8 1305\f
1baa375f
RK
1306/* Given T, an expression, return the negation of T. Allow for T to be
1307 null, in which case return null. */
6d716ca8 1308
1baa375f
RK
1309static tree
1310negate_expr (t)
1311 tree t;
1312{
1313 tree type;
1314 tree tem;
1315
1316 if (t == 0)
1317 return 0;
1318
1319 type = TREE_TYPE (t);
1320 STRIP_SIGN_NOPS (t);
1321
1322 switch (TREE_CODE (t))
1323 {
1324 case INTEGER_CST:
1325 case REAL_CST:
1326 if (! TREE_UNSIGNED (type)
1327 && 0 != (tem = fold (build1 (NEGATE_EXPR, type, t)))
1328 && ! TREE_OVERFLOW (tem))
1329 return tem;
1330 break;
1331
1332 case NEGATE_EXPR:
1333 return convert (type, TREE_OPERAND (t, 0));
1334
1335 case MINUS_EXPR:
1336 /* - (A - B) -> B - A */
de6c5979 1337 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1baa375f
RK
1338 return convert (type,
1339 fold (build (MINUS_EXPR, TREE_TYPE (t),
1340 TREE_OPERAND (t, 1),
1341 TREE_OPERAND (t, 0))));
1342 break;
1343
1344 default:
1345 break;
1346 }
1347
1348 return convert (type, build1 (NEGATE_EXPR, TREE_TYPE (t), t));
1349}
1350\f
1351/* Split a tree IN into a constant, literal and variable parts that could be
1352 combined with CODE to make IN. "constant" means an expression with
1353 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1354 commutative arithmetic operation. Store the constant part into *CONP,
1355 the literal in &LITP and return the variable part. If a part isn't
1356 present, set it to null. If the tree does not decompose in this way,
1357 return the entire tree as the variable part and the other parts as null.
1358
1359 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1360 case, we negate an operand that was subtracted. If NEGATE_P is true, we
1361 are negating all of IN.
1362
1363 If IN is itself a literal or constant, return it as appropriate.
1364
1365 Note that we do not guarantee that any of the three values will be the
1366 same type as IN, but they will have the same signedness and mode. */
1367
1368static tree
1369split_tree (in, code, conp, litp, negate_p)
6d716ca8
RS
1370 tree in;
1371 enum tree_code code;
1baa375f
RK
1372 tree *conp, *litp;
1373 int negate_p;
6d716ca8 1374{
1baa375f
RK
1375 tree var = 0;
1376
6d716ca8 1377 *conp = 0;
1baa375f
RK
1378 *litp = 0;
1379
30f7a378 1380 /* Strip any conversions that don't change the machine mode or signedness. */
1baa375f
RK
1381 STRIP_SIGN_NOPS (in);
1382
1383 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1384 *litp = in;
1baa375f
RK
1385 else if (TREE_CODE (in) == code
1386 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1387 /* We can associate addition and subtraction together (even
1388 though the C standard doesn't say so) for integers because
1389 the value is not affected. For reals, the value might be
1390 affected, so we can't. */
1391 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1392 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1393 {
1394 tree op0 = TREE_OPERAND (in, 0);
1395 tree op1 = TREE_OPERAND (in, 1);
1396 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1397 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1398
1399 /* First see if either of the operands is a literal, then a constant. */
1400 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1401 *litp = op0, op0 = 0;
1402 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1403 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1404
1405 if (op0 != 0 && TREE_CONSTANT (op0))
1406 *conp = op0, op0 = 0;
1407 else if (op1 != 0 && TREE_CONSTANT (op1))
1408 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1409
1410 /* If we haven't dealt with either operand, this is not a case we can
30f7a378 1411 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1baa375f
RK
1412 if (op0 != 0 && op1 != 0)
1413 var = in;
1414 else if (op0 != 0)
1415 var = op0;
1416 else
1417 var = op1, neg_var_p = neg1_p;
6d716ca8 1418
1baa375f
RK
1419 /* Now do any needed negations. */
1420 if (neg_litp_p) *litp = negate_expr (*litp);
1421 if (neg_conp_p) *conp = negate_expr (*conp);
1422 if (neg_var_p) var = negate_expr (var);
1423 }
1796dff4
RH
1424 else if (TREE_CONSTANT (in))
1425 *conp = in;
1baa375f
RK
1426 else
1427 var = in;
1428
1429 if (negate_p)
6d716ca8 1430 {
1baa375f
RK
1431 var = negate_expr (var);
1432 *conp = negate_expr (*conp);
1433 *litp = negate_expr (*litp);
6d716ca8 1434 }
1baa375f
RK
1435
1436 return var;
1437}
1438
1439/* Re-associate trees split by the above function. T1 and T2 are either
1440 expressions to associate or null. Return the new expression, if any. If
1441 we build an operation, do it in TYPE and with CODE, except if CODE is a
1442 MINUS_EXPR, in which case we use PLUS_EXPR since split_tree will already
1443 have taken care of the negations. */
1444
1445static tree
1446associate_trees (t1, t2, code, type)
1447 tree t1, t2;
1448 enum tree_code code;
1449 tree type;
1450{
1baa375f
RK
1451 if (t1 == 0)
1452 return t2;
1453 else if (t2 == 0)
1454 return t1;
1455
1456 if (code == MINUS_EXPR)
1457 code = PLUS_EXPR;
1458
1459 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1460 try to fold this since we will have infinite recursion. But do
1461 deal with any NEGATE_EXPRs. */
1462 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1463 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1464 {
1465 if (TREE_CODE (t1) == NEGATE_EXPR)
1466 return build (MINUS_EXPR, type, convert (type, t2),
1467 convert (type, TREE_OPERAND (t1, 0)));
1468 else if (TREE_CODE (t2) == NEGATE_EXPR)
1469 return build (MINUS_EXPR, type, convert (type, t1),
1470 convert (type, TREE_OPERAND (t2, 0)));
1471 else
1472 return build (code, type, convert (type, t1), convert (type, t2));
1473 }
1474
1475 return fold (build (code, type, convert (type, t1), convert (type, t2)));
6d716ca8
RS
1476}
1477\f
e9a25f70 1478/* Combine two integer constants ARG1 and ARG2 under operation CODE
6d716ca8 1479 to produce a new constant.
91d33e36 1480
e9a25f70
JL
1481 If NOTRUNC is nonzero, do not truncate the result to fit the data type.
1482 If FORSIZE is nonzero, compute overflow for unsigned types. */
6d716ca8 1483
6d716ca8 1484static tree
e9a25f70 1485int_const_binop (code, arg1, arg2, notrunc, forsize)
6d716ca8
RS
1486 enum tree_code code;
1487 register tree arg1, arg2;
e9a25f70 1488 int notrunc, forsize;
6d716ca8 1489{
05bccae2
RK
1490 unsigned HOST_WIDE_INT int1l, int2l;
1491 HOST_WIDE_INT int1h, int2h;
1492 unsigned HOST_WIDE_INT low;
1493 HOST_WIDE_INT hi;
1494 unsigned HOST_WIDE_INT garbagel;
1495 HOST_WIDE_INT garbageh;
e9a25f70
JL
1496 register tree t;
1497 int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
1498 int overflow = 0;
1499 int no_overflow = 0;
3dedc65a 1500
e9a25f70
JL
1501 int1l = TREE_INT_CST_LOW (arg1);
1502 int1h = TREE_INT_CST_HIGH (arg1);
1503 int2l = TREE_INT_CST_LOW (arg2);
1504 int2h = TREE_INT_CST_HIGH (arg2);
1505
1506 switch (code)
6d716ca8 1507 {
e9a25f70
JL
1508 case BIT_IOR_EXPR:
1509 low = int1l | int2l, hi = int1h | int2h;
1510 break;
6d716ca8 1511
e9a25f70
JL
1512 case BIT_XOR_EXPR:
1513 low = int1l ^ int2l, hi = int1h ^ int2h;
1514 break;
6d716ca8 1515
e9a25f70
JL
1516 case BIT_AND_EXPR:
1517 low = int1l & int2l, hi = int1h & int2h;
1518 break;
6d716ca8 1519
e9a25f70
JL
1520 case BIT_ANDTC_EXPR:
1521 low = int1l & ~int2l, hi = int1h & ~int2h;
1522 break;
6d716ca8 1523
e9a25f70 1524 case RSHIFT_EXPR:
b6cc0a72 1525 int2l = -int2l;
e9a25f70
JL
1526 case LSHIFT_EXPR:
1527 /* It's unclear from the C standard whether shifts can overflow.
1528 The following code ignores overflow; perhaps a C standard
1529 interpretation ruling is needed. */
770ae6cc
RK
1530 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (TREE_TYPE (arg1)),
1531 &low, &hi, !uns);
e9a25f70
JL
1532 no_overflow = 1;
1533 break;
6d716ca8 1534
e9a25f70
JL
1535 case RROTATE_EXPR:
1536 int2l = - int2l;
1537 case LROTATE_EXPR:
770ae6cc 1538 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (TREE_TYPE (arg1)),
e9a25f70
JL
1539 &low, &hi);
1540 break;
6d716ca8 1541
e9a25f70
JL
1542 case PLUS_EXPR:
1543 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1544 break;
6d716ca8 1545
e9a25f70
JL
1546 case MINUS_EXPR:
1547 neg_double (int2l, int2h, &low, &hi);
1548 add_double (int1l, int1h, low, hi, &low, &hi);
d4b60170 1549 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
e9a25f70 1550 break;
6d716ca8 1551
e9a25f70
JL
1552 case MULT_EXPR:
1553 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1554 break;
6d716ca8 1555
e9a25f70
JL
1556 case TRUNC_DIV_EXPR:
1557 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1558 case EXACT_DIV_EXPR:
1559 /* This is a shortcut for a common special case. */
05bccae2 1560 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
e9a25f70
JL
1561 && ! TREE_CONSTANT_OVERFLOW (arg1)
1562 && ! TREE_CONSTANT_OVERFLOW (arg2)
05bccae2 1563 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
e9a25f70
JL
1564 {
1565 if (code == CEIL_DIV_EXPR)
1566 int1l += int2l - 1;
05bccae2 1567
e9a25f70 1568 low = int1l / int2l, hi = 0;
6d716ca8 1569 break;
e9a25f70 1570 }
6d716ca8 1571
30f7a378 1572 /* ... fall through ... */
6d716ca8 1573
b6cc0a72 1574 case ROUND_DIV_EXPR:
e9a25f70
JL
1575 if (int2h == 0 && int2l == 1)
1576 {
1577 low = int1l, hi = int1h;
6d716ca8 1578 break;
e9a25f70
JL
1579 }
1580 if (int1l == int2l && int1h == int2h
1581 && ! (int1l == 0 && int1h == 0))
1582 {
1583 low = 1, hi = 0;
63e7fe9b 1584 break;
e9a25f70
JL
1585 }
1586 overflow = div_and_round_double (code, uns,
1587 int1l, int1h, int2l, int2h,
1588 &low, &hi, &garbagel, &garbageh);
1589 break;
63e7fe9b 1590
e9a25f70
JL
1591 case TRUNC_MOD_EXPR:
1592 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1593 /* This is a shortcut for a common special case. */
05bccae2 1594 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
e9a25f70
JL
1595 && ! TREE_CONSTANT_OVERFLOW (arg1)
1596 && ! TREE_CONSTANT_OVERFLOW (arg2)
05bccae2 1597 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
e9a25f70
JL
1598 {
1599 if (code == CEIL_MOD_EXPR)
1600 int1l += int2l - 1;
1601 low = int1l % int2l, hi = 0;
63e7fe9b 1602 break;
e9a25f70 1603 }
63e7fe9b 1604
30f7a378 1605 /* ... fall through ... */
e9a25f70 1606
b6cc0a72 1607 case ROUND_MOD_EXPR:
e9a25f70
JL
1608 overflow = div_and_round_double (code, uns,
1609 int1l, int1h, int2l, int2h,
1610 &garbagel, &garbageh, &low, &hi);
1611 break;
1612
1613 case MIN_EXPR:
1614 case MAX_EXPR:
1615 if (uns)
d4b60170
RK
1616 low = (((unsigned HOST_WIDE_INT) int1h
1617 < (unsigned HOST_WIDE_INT) int2h)
1618 || (((unsigned HOST_WIDE_INT) int1h
1619 == (unsigned HOST_WIDE_INT) int2h)
05bccae2 1620 && int1l < int2l));
380ff34a 1621 else
05bccae2
RK
1622 low = (int1h < int2h
1623 || (int1h == int2h && int1l < int2l));
d4b60170 1624
e9a25f70
JL
1625 if (low == (code == MIN_EXPR))
1626 low = int1l, hi = int1h;
1627 else
1628 low = int2l, hi = int2h;
1629 break;
3dedc65a 1630
e9a25f70
JL
1631 default:
1632 abort ();
3dedc65a 1633 }
e9a25f70 1634
44475138
RK
1635 if (forsize && hi == 0 && low < 10000
1636 && overflow == 0 && ! TREE_OVERFLOW (arg1) && ! TREE_OVERFLOW (arg2))
fed3cef0 1637 return size_int_type_wide (low, TREE_TYPE (arg1));
e9a25f70
JL
1638 else
1639 {
1640 t = build_int_2 (low, hi);
1641 TREE_TYPE (t) = TREE_TYPE (arg1);
1642 }
1643
1644 TREE_OVERFLOW (t)
1645 = ((notrunc ? (!uns || forsize) && overflow
1646 : force_fit_type (t, (!uns || forsize) && overflow) && ! no_overflow)
1647 | TREE_OVERFLOW (arg1)
1648 | TREE_OVERFLOW (arg2));
d4b60170 1649
e9a25f70
JL
1650 /* If we're doing a size calculation, unsigned arithmetic does overflow.
1651 So check if force_fit_type truncated the value. */
1652 if (forsize
1653 && ! TREE_OVERFLOW (t)
1654 && (TREE_INT_CST_HIGH (t) != hi
1655 || TREE_INT_CST_LOW (t) != low))
1656 TREE_OVERFLOW (t) = 1;
d4b60170 1657
e9a25f70
JL
1658 TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1659 | TREE_CONSTANT_OVERFLOW (arg1)
1660 | TREE_CONSTANT_OVERFLOW (arg2));
1661 return t;
1662}
1663
d4b60170 1664/* Define input and output argument for const_binop_1. */
a4d3481d
KG
1665struct cb_args
1666{
b6cc0a72
KH
1667 enum tree_code code; /* Input: tree code for operation. */
1668 tree type; /* Input: tree type for operation. */
1669 REAL_VALUE_TYPE d1, d2; /* Input: floating point operands. */
1670 tree t; /* Output: constant for result. */
a4d3481d
KG
1671};
1672
d4b60170
RK
1673/* Do the real arithmetic for const_binop while protected by a
1674 float overflow handler. */
1675
a4d3481d
KG
1676static void
1677const_binop_1 (data)
b6cc0a72 1678 PTR data;
a4d3481d 1679{
d4b60170 1680 struct cb_args *args = (struct cb_args *) data;
a4d3481d
KG
1681 REAL_VALUE_TYPE value;
1682
1683#ifdef REAL_ARITHMETIC
1684 REAL_ARITHMETIC (value, args->code, args->d1, args->d2);
1685#else
1686 switch (args->code)
1687 {
1688 case PLUS_EXPR:
1689 value = args->d1 + args->d2;
1690 break;
b6cc0a72 1691
a4d3481d
KG
1692 case MINUS_EXPR:
1693 value = args->d1 - args->d2;
1694 break;
b6cc0a72 1695
a4d3481d
KG
1696 case MULT_EXPR:
1697 value = args->d1 * args->d2;
1698 break;
b6cc0a72 1699
a4d3481d
KG
1700 case RDIV_EXPR:
1701#ifndef REAL_INFINITY
1702 if (args->d2 == 0)
1703 abort ();
1704#endif
b6cc0a72 1705
a4d3481d
KG
1706 value = args->d1 / args->d2;
1707 break;
b6cc0a72 1708
a4d3481d
KG
1709 case MIN_EXPR:
1710 value = MIN (args->d1, args->d2);
1711 break;
b6cc0a72 1712
a4d3481d
KG
1713 case MAX_EXPR:
1714 value = MAX (args->d1, args->d2);
1715 break;
b6cc0a72 1716
a4d3481d
KG
1717 default:
1718 abort ();
1719 }
1720#endif /* no REAL_ARITHMETIC */
d4b60170
RK
1721
1722 args->t
1723 = build_real (args->type,
1724 real_value_truncate (TYPE_MODE (args->type), value));
a4d3481d
KG
1725}
1726
d4b60170
RK
1727/* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1728 constant. We assume ARG1 and ARG2 have the same data type, or at least
1729 are the same kind of constant and the same machine mode.
e9a25f70
JL
1730
1731 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1732
1733static tree
1734const_binop (code, arg1, arg2, notrunc)
1735 enum tree_code code;
1736 register tree arg1, arg2;
1737 int notrunc;
1738{
b6cc0a72
KH
1739 STRIP_NOPS (arg1);
1740 STRIP_NOPS (arg2);
e9a25f70
JL
1741
1742 if (TREE_CODE (arg1) == INTEGER_CST)
1743 return int_const_binop (code, arg1, arg2, notrunc, 0);
1744
6d716ca8
RS
1745#if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1746 if (TREE_CODE (arg1) == REAL_CST)
1747 {
79c844cd
RK
1748 REAL_VALUE_TYPE d1;
1749 REAL_VALUE_TYPE d2;
649ff3b4 1750 int overflow = 0;
7c7b029d 1751 tree t;
a4d3481d 1752 struct cb_args args;
6d716ca8 1753
79c844cd
RK
1754 d1 = TREE_REAL_CST (arg1);
1755 d2 = TREE_REAL_CST (arg2);
5f610074
RK
1756
1757 /* If either operand is a NaN, just return it. Otherwise, set up
1758 for floating-point trap; we return an overflow. */
1759 if (REAL_VALUE_ISNAN (d1))
1760 return arg1;
1761 else if (REAL_VALUE_ISNAN (d2))
1762 return arg2;
a4d3481d
KG
1763
1764 /* Setup input for const_binop_1() */
d4b60170 1765 args.type = TREE_TYPE (arg1);
a4d3481d
KG
1766 args.d1 = d1;
1767 args.d2 = d2;
1768 args.code = code;
b6cc0a72 1769
a4d3481d 1770 if (do_float_handler (const_binop_1, (PTR) &args))
30f7a378 1771 /* Receive output from const_binop_1. */
d4b60170 1772 t = args.t;
a4d3481d 1773 else
6d716ca8 1774 {
30f7a378 1775 /* We got an exception from const_binop_1. */
a4d3481d
KG
1776 t = copy_node (arg1);
1777 overflow = 1;
6d716ca8 1778 }
649ff3b4
RK
1779
1780 TREE_OVERFLOW (t)
1781 = (force_fit_type (t, overflow)
1782 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1783 TREE_CONSTANT_OVERFLOW (t)
1784 = TREE_OVERFLOW (t)
1785 | TREE_CONSTANT_OVERFLOW (arg1)
1786 | TREE_CONSTANT_OVERFLOW (arg2);
7c7b029d 1787 return t;
6d716ca8
RS
1788 }
1789#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1790 if (TREE_CODE (arg1) == COMPLEX_CST)
1791 {
214d5b84 1792 register tree type = TREE_TYPE (arg1);
6d716ca8
RS
1793 register tree r1 = TREE_REALPART (arg1);
1794 register tree i1 = TREE_IMAGPART (arg1);
1795 register tree r2 = TREE_REALPART (arg2);
1796 register tree i2 = TREE_IMAGPART (arg2);
1797 register tree t;
1798
1799 switch (code)
1800 {
1801 case PLUS_EXPR:
214d5b84
RK
1802 t = build_complex (type,
1803 const_binop (PLUS_EXPR, r1, r2, notrunc),
91d33e36 1804 const_binop (PLUS_EXPR, i1, i2, notrunc));
6d716ca8
RS
1805 break;
1806
1807 case MINUS_EXPR:
214d5b84
RK
1808 t = build_complex (type,
1809 const_binop (MINUS_EXPR, r1, r2, notrunc),
91d33e36 1810 const_binop (MINUS_EXPR, i1, i2, notrunc));
6d716ca8
RS
1811 break;
1812
1813 case MULT_EXPR:
214d5b84
RK
1814 t = build_complex (type,
1815 const_binop (MINUS_EXPR,
91d33e36
RS
1816 const_binop (MULT_EXPR,
1817 r1, r2, notrunc),
1818 const_binop (MULT_EXPR,
1819 i1, i2, notrunc),
1820 notrunc),
6d716ca8 1821 const_binop (PLUS_EXPR,
91d33e36
RS
1822 const_binop (MULT_EXPR,
1823 r1, i2, notrunc),
1824 const_binop (MULT_EXPR,
1825 i1, r2, notrunc),
1826 notrunc));
6d716ca8
RS
1827 break;
1828
1829 case RDIV_EXPR:
1830 {
1831 register tree magsquared
1832 = const_binop (PLUS_EXPR,
91d33e36
RS
1833 const_binop (MULT_EXPR, r2, r2, notrunc),
1834 const_binop (MULT_EXPR, i2, i2, notrunc),
1835 notrunc);
58633be8 1836
214d5b84
RK
1837 t = build_complex (type,
1838 const_binop
1839 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1840 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1841 const_binop (PLUS_EXPR,
1842 const_binop (MULT_EXPR, r1, r2,
1843 notrunc),
1844 const_binop (MULT_EXPR, i1, i2,
1845 notrunc),
1846 notrunc),
1847 magsquared, notrunc),
1848 const_binop
1849 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1850 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1851 const_binop (MINUS_EXPR,
1852 const_binop (MULT_EXPR, i1, r2,
1853 notrunc),
1854 const_binop (MULT_EXPR, r1, i2,
1855 notrunc),
1856 notrunc),
1857 magsquared, notrunc));
6d716ca8
RS
1858 }
1859 break;
1860
1861 default:
1862 abort ();
1863 }
6d716ca8
RS
1864 return t;
1865 }
1866 return 0;
1867}
1868\f
06ceef4e 1869/* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
fed3cef0 1870 bits are given by NUMBER and of the sizetype represented by KIND. */
d4b60170 1871
fed3cef0
RK
1872tree
1873size_int_wide (number, kind)
1874 HOST_WIDE_INT number;
1875 enum size_type_kind kind;
1876{
1877 return size_int_type_wide (number, sizetype_tab[(int) kind]);
1878}
1879
1880/* Likewise, but the desired type is specified explicitly. */
6d716ca8
RS
1881
1882tree
fed3cef0 1883size_int_type_wide (number, type)
06ceef4e 1884 HOST_WIDE_INT number;
fed3cef0 1885 tree type;
6d716ca8 1886{
d4b60170 1887 /* Type-size nodes already made for small sizes. */
770ae6cc 1888 static tree size_table[2048 + 1];
d4b60170 1889 static int init_p = 0;
a3770a81 1890 tree t;
b6cc0a72 1891
1f8f4a0b 1892 if (! init_p)
6d716ca8 1893 {
d4b60170
RK
1894 ggc_add_tree_root ((tree *) size_table,
1895 sizeof size_table / sizeof (tree));
1896 init_p = 1;
1897 }
1898
06ceef4e
RK
1899 /* If this is a positive number that fits in the table we use to hold
1900 cached entries, see if it is already in the table and put it there
1901 if not. */
b6a1cbae 1902 if (number >= 0 && number < (int) ARRAY_SIZE (size_table))
d4b60170 1903 {
fed3cef0
RK
1904 if (size_table[number] != 0)
1905 for (t = size_table[number]; t != 0; t = TREE_CHAIN (t))
1906 if (TREE_TYPE (t) == type)
1907 return t;
06ceef4e 1908
d4b60170 1909 t = build_int_2 (number, 0);
fed3cef0
RK
1910 TREE_TYPE (t) = type;
1911 TREE_CHAIN (t) = size_table[number];
1912 size_table[number] = t;
d4b60170 1913
d4b60170 1914 return t;
6d716ca8 1915 }
a3770a81 1916
fed3cef0
RK
1917 t = build_int_2 (number, number < 0 ? -1 : 0);
1918 TREE_TYPE (t) = type;
a3770a81 1919 TREE_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (t) = force_fit_type (t, 0);
6d716ca8
RS
1920 return t;
1921}
1922
fed3cef0
RK
1923/* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1924 is a tree code. The type of the result is taken from the operands.
1925 Both must be the same type integer type and it must be a size type.
6d716ca8
RS
1926 If the operands are constant, so is the result. */
1927
1928tree
1929size_binop (code, arg0, arg1)
1930 enum tree_code code;
1931 tree arg0, arg1;
1932{
fed3cef0
RK
1933 tree type = TREE_TYPE (arg0);
1934
21318741
RK
1935 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1936 || type != TREE_TYPE (arg1))
fed3cef0
RK
1937 abort ();
1938
6d716ca8
RS
1939 /* Handle the special case of two integer constants faster. */
1940 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1941 {
1942 /* And some specific cases even faster than that. */
9898deac 1943 if (code == PLUS_EXPR && integer_zerop (arg0))
6d716ca8 1944 return arg1;
9898deac
RK
1945 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1946 && integer_zerop (arg1))
6d716ca8 1947 return arg0;
9898deac 1948 else if (code == MULT_EXPR && integer_onep (arg0))
6d716ca8 1949 return arg1;
9898deac 1950
6d716ca8 1951 /* Handle general case of two integer constants. */
e9a25f70 1952 return int_const_binop (code, arg0, arg1, 0, 1);
6d716ca8
RS
1953 }
1954
1955 if (arg0 == error_mark_node || arg1 == error_mark_node)
1956 return error_mark_node;
1957
fed3cef0 1958 return fold (build (code, type, arg0, arg1));
6d716ca8 1959}
697073d9 1960
fed3cef0
RK
1961/* Given two values, either both of sizetype or both of bitsizetype,
1962 compute the difference between the two values. Return the value
1963 in signed type corresponding to the type of the operands. */
697073d9
JM
1964
1965tree
fed3cef0 1966size_diffop (arg0, arg1)
697073d9
JM
1967 tree arg0, arg1;
1968{
fed3cef0
RK
1969 tree type = TREE_TYPE (arg0);
1970 tree ctype;
697073d9 1971
21318741
RK
1972 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1973 || type != TREE_TYPE (arg1))
fed3cef0 1974 abort ();
697073d9 1975
fed3cef0
RK
1976 /* If the type is already signed, just do the simple thing. */
1977 if (! TREE_UNSIGNED (type))
1978 return size_binop (MINUS_EXPR, arg0, arg1);
1979
1980 ctype = (type == bitsizetype || type == ubitsizetype
1981 ? sbitsizetype : ssizetype);
1982
1983 /* If either operand is not a constant, do the conversions to the signed
1984 type and subtract. The hardware will do the right thing with any
1985 overflow in the subtraction. */
1986 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1987 return size_binop (MINUS_EXPR, convert (ctype, arg0),
1988 convert (ctype, arg1));
1989
1990 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1991 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1992 overflow) and negate (which can't either). Special-case a result
1993 of zero while we're here. */
1994 if (tree_int_cst_equal (arg0, arg1))
1995 return convert (ctype, integer_zero_node);
1996 else if (tree_int_cst_lt (arg1, arg0))
1997 return convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1998 else
1999 return size_binop (MINUS_EXPR, convert (ctype, integer_zero_node),
2000 convert (ctype, size_binop (MINUS_EXPR, arg1, arg0)));
697073d9 2001}
6d716ca8 2002\f
d4b60170 2003/* This structure is used to communicate arguments to fold_convert_1. */
a4d3481d
KG
2004struct fc_args
2005{
30f7a378
KH
2006 tree arg1; /* Input: value to convert. */
2007 tree type; /* Input: type to convert value to. */
2008 tree t; /* Ouput: result of conversion. */
a4d3481d
KG
2009};
2010
d4b60170
RK
2011/* Function to convert floating-point constants, protected by floating
2012 point exception handler. */
2013
a4d3481d
KG
2014static void
2015fold_convert_1 (data)
b6cc0a72 2016 PTR data;
a4d3481d 2017{
b6cc0a72 2018 struct fc_args *args = (struct fc_args *) data;
a4d3481d
KG
2019
2020 args->t = build_real (args->type,
2021 real_value_truncate (TYPE_MODE (args->type),
2022 TREE_REAL_CST (args->arg1)));
2023}
2024
6d716ca8
RS
2025/* Given T, a tree representing type conversion of ARG1, a constant,
2026 return a constant tree representing the result of conversion. */
2027
2028static tree
2029fold_convert (t, arg1)
2030 register tree t;
2031 register tree arg1;
2032{
2033 register tree type = TREE_TYPE (t);
649ff3b4 2034 int overflow = 0;
6d716ca8 2035
e5e809f4 2036 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
6d716ca8
RS
2037 {
2038 if (TREE_CODE (arg1) == INTEGER_CST)
2039 {
e3374525
RK
2040 /* If we would build a constant wider than GCC supports,
2041 leave the conversion unfolded. */
2042 if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
2043 return t;
2044
fed3cef0
RK
2045 /* If we are trying to make a sizetype for a small integer, use
2046 size_int to pick up cached types to reduce duplicate nodes. */
55560b9d 2047 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
ac50176d 2048 && !TREE_CONSTANT_OVERFLOW (arg1)
770ae6cc 2049 && compare_tree_int (arg1, 10000) < 0)
fed3cef0
RK
2050 return size_int_type_wide (TREE_INT_CST_LOW (arg1), type);
2051
6d716ca8
RS
2052 /* Given an integer constant, make new constant with new type,
2053 appropriately sign-extended or truncated. */
2054 t = build_int_2 (TREE_INT_CST_LOW (arg1),
2055 TREE_INT_CST_HIGH (arg1));
2056 TREE_TYPE (t) = type;
e0f776fb 2057 /* Indicate an overflow if (1) ARG1 already overflowed,
dc3907c5
PE
2058 or (2) force_fit_type indicates an overflow.
2059 Tell force_fit_type that an overflow has already occurred
b472527b
JL
2060 if ARG1 is a too-large unsigned value and T is signed.
2061 But don't indicate an overflow if converting a pointer. */
dc3907c5 2062 TREE_OVERFLOW (t)
e5e809f4
JL
2063 = ((force_fit_type (t,
2064 (TREE_INT_CST_HIGH (arg1) < 0
4f242db3 2065 && (TREE_UNSIGNED (type)
e5e809f4
JL
2066 < TREE_UNSIGNED (TREE_TYPE (arg1)))))
2067 && ! POINTER_TYPE_P (TREE_TYPE (arg1)))
2068 || TREE_OVERFLOW (arg1));
dc3907c5
PE
2069 TREE_CONSTANT_OVERFLOW (t)
2070 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
6d716ca8
RS
2071 }
2072#if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2073 else if (TREE_CODE (arg1) == REAL_CST)
2074 {
4b8a0062
RK
2075 /* Don't initialize these, use assignments.
2076 Initialized local aggregates don't work on old compilers. */
2077 REAL_VALUE_TYPE x;
2078 REAL_VALUE_TYPE l;
2079 REAL_VALUE_TYPE u;
7cb6a121 2080 tree type1 = TREE_TYPE (arg1);
e1ee5cdc 2081 int no_upper_bound;
4b8a0062
RK
2082
2083 x = TREE_REAL_CST (arg1);
7cb6a121 2084 l = real_value_from_int_cst (type1, TYPE_MIN_VALUE (type));
e1ee5cdc
RH
2085
2086 no_upper_bound = (TYPE_MAX_VALUE (type) == NULL);
2087 if (!no_upper_bound)
2088 u = real_value_from_int_cst (type1, TYPE_MAX_VALUE (type));
2089
4b3d5ea0
RS
2090 /* See if X will be in range after truncation towards 0.
2091 To compensate for truncation, move the bounds away from 0,
2092 but reject if X exactly equals the adjusted bounds. */
2093#ifdef REAL_ARITHMETIC
2094 REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
e1ee5cdc
RH
2095 if (!no_upper_bound)
2096 REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
4b3d5ea0
RS
2097#else
2098 l--;
e1ee5cdc
RH
2099 if (!no_upper_bound)
2100 u++;
4b3d5ea0 2101#endif
5f610074
RK
2102 /* If X is a NaN, use zero instead and show we have an overflow.
2103 Otherwise, range check. */
2104 if (REAL_VALUE_ISNAN (x))
2105 overflow = 1, x = dconst0;
e1ee5cdc
RH
2106 else if (! (REAL_VALUES_LESS (l, x)
2107 && !no_upper_bound
2108 && REAL_VALUES_LESS (x, u)))
649ff3b4
RK
2109 overflow = 1;
2110
6d716ca8
RS
2111#ifndef REAL_ARITHMETIC
2112 {
906c4e36
RK
2113 HOST_WIDE_INT low, high;
2114 HOST_WIDE_INT half_word
2115 = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
6d716ca8 2116
5f610074
RK
2117 if (x < 0)
2118 x = -x;
6d716ca8 2119
5f610074
RK
2120 high = (HOST_WIDE_INT) (x / half_word / half_word);
2121 x -= (REAL_VALUE_TYPE) high * half_word * half_word;
2122 if (x >= (REAL_VALUE_TYPE) half_word * half_word / 2)
b6b19f41 2123 {
5f610074 2124 low = x - (REAL_VALUE_TYPE) half_word * half_word / 2;
e0f776fb 2125 low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1);
b6b19f41
RS
2126 }
2127 else
5f610074 2128 low = (HOST_WIDE_INT) x;
6d716ca8
RS
2129 if (TREE_REAL_CST (arg1) < 0)
2130 neg_double (low, high, &low, &high);
2131 t = build_int_2 (low, high);
2132 }
2133#else
2134 {
906c4e36 2135 HOST_WIDE_INT low, high;
5f610074 2136 REAL_VALUE_TO_INT (&low, &high, x);
6d716ca8
RS
2137 t = build_int_2 (low, high);
2138 }
2139#endif
2140 TREE_TYPE (t) = type;
649ff3b4
RK
2141 TREE_OVERFLOW (t)
2142 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
2143 TREE_CONSTANT_OVERFLOW (t)
2144 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
6d716ca8
RS
2145 }
2146#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2147 TREE_TYPE (t) = type;
2148 }
2149 else if (TREE_CODE (type) == REAL_TYPE)
2150 {
2151#if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2152 if (TREE_CODE (arg1) == INTEGER_CST)
2153 return build_real_from_int_cst (type, arg1);
2154#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2155 if (TREE_CODE (arg1) == REAL_CST)
7c7b029d 2156 {
a4d3481d 2157 struct fc_args args;
b6cc0a72 2158
5f610074 2159 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
a377ff85
RK
2160 {
2161 t = arg1;
2162 TREE_TYPE (arg1) = type;
2163 return t;
2164 }
a4d3481d
KG
2165
2166 /* Setup input for fold_convert_1() */
2167 args.arg1 = arg1;
2168 args.type = type;
b6cc0a72 2169
a4d3481d
KG
2170 if (do_float_handler (fold_convert_1, (PTR) &args))
2171 {
2172 /* Receive output from fold_convert_1() */
2173 t = args.t;
2174 }
2175 else
7c7b029d 2176 {
a4d3481d 2177 /* We got an exception from fold_convert_1() */
649ff3b4
RK
2178 overflow = 1;
2179 t = copy_node (arg1);
7c7b029d 2180 }
649ff3b4 2181
649ff3b4
RK
2182 TREE_OVERFLOW (t)
2183 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
2184 TREE_CONSTANT_OVERFLOW (t)
2185 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
7c7b029d
RS
2186 return t;
2187 }
6d716ca8
RS
2188 }
2189 TREE_CONSTANT (t) = 1;
2190 return t;
2191}
2192\f
ff59bfe6 2193/* Return an expr equal to X but certainly not valid as an lvalue. */
6d716ca8
RS
2194
2195tree
2196non_lvalue (x)
2197 tree x;
2198{
2199 tree result;
2200
2201 /* These things are certainly not lvalues. */
2202 if (TREE_CODE (x) == NON_LVALUE_EXPR
2203 || TREE_CODE (x) == INTEGER_CST
2204 || TREE_CODE (x) == REAL_CST
2205 || TREE_CODE (x) == STRING_CST
2206 || TREE_CODE (x) == ADDR_EXPR)
ff59bfe6 2207 return x;
6d716ca8
RS
2208
2209 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2210 TREE_CONSTANT (result) = TREE_CONSTANT (x);
2211 return result;
2212}
a5e9b124 2213
e9866da3
JM
2214/* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2215 Zero means allow extended lvalues. */
2216
2217int pedantic_lvalues;
2218
a5e9b124
JW
2219/* When pedantic, return an expr equal to X but certainly not valid as a
2220 pedantic lvalue. Otherwise, return X. */
2221
2222tree
2223pedantic_non_lvalue (x)
2224 tree x;
2225{
e9866da3 2226 if (pedantic_lvalues)
a5e9b124
JW
2227 return non_lvalue (x);
2228 else
2229 return x;
2230}
c05a9b68
RS
2231\f
2232/* Given a tree comparison code, return the code that is the logical inverse
2233 of the given code. It is not safe to do this for floating-point
2234 comparisons, except for NE_EXPR and EQ_EXPR. */
6d716ca8 2235
c05a9b68
RS
2236static enum tree_code
2237invert_tree_comparison (code)
2238 enum tree_code code;
2239{
2240 switch (code)
2241 {
2242 case EQ_EXPR:
2243 return NE_EXPR;
2244 case NE_EXPR:
2245 return EQ_EXPR;
2246 case GT_EXPR:
2247 return LE_EXPR;
2248 case GE_EXPR:
2249 return LT_EXPR;
2250 case LT_EXPR:
2251 return GE_EXPR;
2252 case LE_EXPR:
2253 return GT_EXPR;
2254 default:
2255 abort ();
2256 }
2257}
2258
2259/* Similar, but return the comparison that results if the operands are
2260 swapped. This is safe for floating-point. */
2261
2262static enum tree_code
2263swap_tree_comparison (code)
2264 enum tree_code code;
2265{
2266 switch (code)
2267 {
2268 case EQ_EXPR:
2269 case NE_EXPR:
2270 return code;
2271 case GT_EXPR:
2272 return LT_EXPR;
2273 case GE_EXPR:
2274 return LE_EXPR;
2275 case LT_EXPR:
2276 return GT_EXPR;
2277 case LE_EXPR:
2278 return GE_EXPR;
2279 default:
2280 abort ();
2281 }
2282}
61f275ff
RK
2283
2284/* Return nonzero if CODE is a tree code that represents a truth value. */
2285
2286static int
2287truth_value_p (code)
2288 enum tree_code code;
2289{
2290 return (TREE_CODE_CLASS (code) == '<'
2291 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2292 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2293 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2294}
c05a9b68 2295\f
6a1746af
RS
2296/* Return nonzero if two operands are necessarily equal.
2297 If ONLY_CONST is non-zero, only return non-zero for constants.
2298 This function tests whether the operands are indistinguishable;
2299 it does not test whether they are equal using C's == operation.
2300 The distinction is important for IEEE floating point, because
2301 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2302 (2) two NaNs may be indistinguishable, but NaN!=NaN. */
6d716ca8
RS
2303
2304int
2305operand_equal_p (arg0, arg1, only_const)
2306 tree arg0, arg1;
2307 int only_const;
2308{
2309 /* If both types don't have the same signedness, then we can't consider
2310 them equal. We must check this before the STRIP_NOPS calls
2311 because they may change the signedness of the arguments. */
2312 if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
2313 return 0;
2314
2315 STRIP_NOPS (arg0);
2316 STRIP_NOPS (arg1);
2317
c7cfe938
RK
2318 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2319 /* This is needed for conversions and for COMPONENT_REF.
2320 Might as well play it safe and always test this. */
e89a9554
ZW
2321 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2322 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
c7cfe938 2323 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
6d716ca8
RS
2324 return 0;
2325
c7cfe938
RK
2326 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2327 We don't care about side effects in that case because the SAVE_EXPR
2328 takes care of that for us. In all other cases, two expressions are
2329 equal if they have no side effects. If we have two identical
2330 expressions with side effects that should be treated the same due
2331 to the only side effects being identical SAVE_EXPR's, that will
2332 be detected in the recursive calls below. */
2333 if (arg0 == arg1 && ! only_const
2334 && (TREE_CODE (arg0) == SAVE_EXPR
2335 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
6d716ca8
RS
2336 return 1;
2337
c7cfe938
RK
2338 /* Next handle constant cases, those for which we can return 1 even
2339 if ONLY_CONST is set. */
2340 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2341 switch (TREE_CODE (arg0))
2342 {
2343 case INTEGER_CST:
3c9b0091
RK
2344 return (! TREE_CONSTANT_OVERFLOW (arg0)
2345 && ! TREE_CONSTANT_OVERFLOW (arg1)
05bccae2 2346 && tree_int_cst_equal (arg0, arg1));
c7cfe938
RK
2347
2348 case REAL_CST:
3c9b0091
RK
2349 return (! TREE_CONSTANT_OVERFLOW (arg0)
2350 && ! TREE_CONSTANT_OVERFLOW (arg1)
41c9120b
PE
2351 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2352 TREE_REAL_CST (arg1)));
c7cfe938
RK
2353
2354 case COMPLEX_CST:
2355 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2356 only_const)
2357 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2358 only_const));
2359
2360 case STRING_CST:
2361 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
71145810 2362 && ! memcmp (TREE_STRING_POINTER (arg0),
c7cfe938
RK
2363 TREE_STRING_POINTER (arg1),
2364 TREE_STRING_LENGTH (arg0)));
2365
2366 case ADDR_EXPR:
2367 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2368 0);
e9a25f70
JL
2369 default:
2370 break;
c7cfe938 2371 }
6d716ca8
RS
2372
2373 if (only_const)
2374 return 0;
2375
6d716ca8
RS
2376 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2377 {
2378 case '1':
2379 /* Two conversions are equal only if signedness and modes match. */
2380 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
2381 && (TREE_UNSIGNED (TREE_TYPE (arg0))
2382 != TREE_UNSIGNED (TREE_TYPE (arg1))))
2383 return 0;
2384
2385 return operand_equal_p (TREE_OPERAND (arg0, 0),
2386 TREE_OPERAND (arg1, 0), 0);
2387
2388 case '<':
2389 case '2':
c7cfe938
RK
2390 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)
2391 && operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1),
2392 0))
2393 return 1;
2394
2395 /* For commutative ops, allow the other order. */
2396 return ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MULT_EXPR
2397 || TREE_CODE (arg0) == MIN_EXPR || TREE_CODE (arg0) == MAX_EXPR
2398 || TREE_CODE (arg0) == BIT_IOR_EXPR
2399 || TREE_CODE (arg0) == BIT_XOR_EXPR
2400 || TREE_CODE (arg0) == BIT_AND_EXPR
2401 || TREE_CODE (arg0) == NE_EXPR || TREE_CODE (arg0) == EQ_EXPR)
2402 && operand_equal_p (TREE_OPERAND (arg0, 0),
2403 TREE_OPERAND (arg1, 1), 0)
6d716ca8 2404 && operand_equal_p (TREE_OPERAND (arg0, 1),
c7cfe938 2405 TREE_OPERAND (arg1, 0), 0));
6d716ca8
RS
2406
2407 case 'r':
05ca5990 2408 /* If either of the pointer (or reference) expressions we are dereferencing
30f7a378 2409 contain a side effect, these cannot be equal. */
05ca5990
GRK
2410 if (TREE_SIDE_EFFECTS (arg0)
2411 || TREE_SIDE_EFFECTS (arg1))
2412 return 0;
2413
6d716ca8
RS
2414 switch (TREE_CODE (arg0))
2415 {
2416 case INDIRECT_REF:
2417 return operand_equal_p (TREE_OPERAND (arg0, 0),
2418 TREE_OPERAND (arg1, 0), 0);
2419
2420 case COMPONENT_REF:
2421 case ARRAY_REF:
b4e3fabb 2422 case ARRAY_RANGE_REF:
6d716ca8
RS
2423 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2424 TREE_OPERAND (arg1, 0), 0)
2425 && operand_equal_p (TREE_OPERAND (arg0, 1),
2426 TREE_OPERAND (arg1, 1), 0));
2427
2428 case BIT_FIELD_REF:
2429 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2430 TREE_OPERAND (arg1, 0), 0)
2431 && operand_equal_p (TREE_OPERAND (arg0, 1),
2432 TREE_OPERAND (arg1, 1), 0)
2433 && operand_equal_p (TREE_OPERAND (arg0, 2),
2434 TREE_OPERAND (arg1, 2), 0));
e9a25f70
JL
2435 default:
2436 return 0;
6d716ca8 2437 }
45f97e2e
RH
2438
2439 case 'e':
2440 if (TREE_CODE (arg0) == RTL_EXPR)
2441 return rtx_equal_p (RTL_EXPR_RTL (arg0), RTL_EXPR_RTL (arg1));
2442 return 0;
b6cc0a72 2443
e9a25f70
JL
2444 default:
2445 return 0;
6d716ca8 2446 }
6d716ca8 2447}
c05a9b68
RS
2448\f
2449/* Similar to operand_equal_p, but see if ARG0 might have been made by
b6cc0a72 2450 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
6d716ca8 2451
6d716ca8
RS
2452 When in doubt, return 0. */
2453
b6cc0a72 2454static int
c05a9b68
RS
2455operand_equal_for_comparison_p (arg0, arg1, other)
2456 tree arg0, arg1;
2457 tree other;
6d716ca8 2458{
c05a9b68 2459 int unsignedp1, unsignedpo;
52de9b6c 2460 tree primarg0, primarg1, primother;
770ae6cc 2461 unsigned int correct_width;
6d716ca8 2462
c05a9b68 2463 if (operand_equal_p (arg0, arg1, 0))
6d716ca8
RS
2464 return 1;
2465
0982a4b8
JM
2466 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2467 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
6d716ca8
RS
2468 return 0;
2469
52de9b6c
RK
2470 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2471 and see if the inner values are the same. This removes any
2472 signedness comparison, which doesn't matter here. */
2473 primarg0 = arg0, primarg1 = arg1;
b6cc0a72
KH
2474 STRIP_NOPS (primarg0);
2475 STRIP_NOPS (primarg1);
52de9b6c
RK
2476 if (operand_equal_p (primarg0, primarg1, 0))
2477 return 1;
2478
c05a9b68
RS
2479 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2480 actual comparison operand, ARG0.
6d716ca8 2481
c05a9b68 2482 First throw away any conversions to wider types
6d716ca8 2483 already present in the operands. */
6d716ca8 2484
c05a9b68
RS
2485 primarg1 = get_narrower (arg1, &unsignedp1);
2486 primother = get_narrower (other, &unsignedpo);
2487
2488 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2489 if (unsignedp1 == unsignedpo
2490 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2491 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
6d716ca8 2492 {
c05a9b68 2493 tree type = TREE_TYPE (arg0);
6d716ca8
RS
2494
2495 /* Make sure shorter operand is extended the right way
2496 to match the longer operand. */
c05a9b68 2497 primarg1 = convert (signed_or_unsigned_type (unsignedp1,
b6cc0a72
KH
2498 TREE_TYPE (primarg1)),
2499 primarg1);
6d716ca8 2500
c05a9b68 2501 if (operand_equal_p (arg0, convert (type, primarg1), 0))
6d716ca8
RS
2502 return 1;
2503 }
2504
2505 return 0;
2506}
2507\f
f72aed24 2508/* See if ARG is an expression that is either a comparison or is performing
c05a9b68
RS
2509 arithmetic on comparisons. The comparisons must only be comparing
2510 two different values, which will be stored in *CVAL1 and *CVAL2; if
2511 they are non-zero it means that some operands have already been found.
2512 No variables may be used anywhere else in the expression except in the
35e66bd1
RK
2513 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2514 the expression and save_expr needs to be called with CVAL1 and CVAL2.
c05a9b68
RS
2515
2516 If this is true, return 1. Otherwise, return zero. */
2517
2518static int
35e66bd1 2519twoval_comparison_p (arg, cval1, cval2, save_p)
c05a9b68
RS
2520 tree arg;
2521 tree *cval1, *cval2;
35e66bd1 2522 int *save_p;
c05a9b68
RS
2523{
2524 enum tree_code code = TREE_CODE (arg);
2525 char class = TREE_CODE_CLASS (code);
2526
2527 /* We can handle some of the 'e' cases here. */
35e66bd1 2528 if (class == 'e' && code == TRUTH_NOT_EXPR)
c05a9b68
RS
2529 class = '1';
2530 else if (class == 'e'
2531 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2532 || code == COMPOUND_EXPR))
2533 class = '2';
2315a5db 2534
d4b60170
RK
2535 else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0
2536 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
35e66bd1
RK
2537 {
2538 /* If we've already found a CVAL1 or CVAL2, this expression is
2539 two complex to handle. */
2540 if (*cval1 || *cval2)
2541 return 0;
2542
2543 class = '1';
2544 *save_p = 1;
2545 }
c05a9b68
RS
2546
2547 switch (class)
2548 {
2549 case '1':
35e66bd1 2550 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
c05a9b68
RS
2551
2552 case '2':
35e66bd1
RK
2553 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2554 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2555 cval1, cval2, save_p));
c05a9b68
RS
2556
2557 case 'c':
2558 return 1;
2559
2560 case 'e':
2561 if (code == COND_EXPR)
35e66bd1
RK
2562 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2563 cval1, cval2, save_p)
2564 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2565 cval1, cval2, save_p)
c05a9b68 2566 && twoval_comparison_p (TREE_OPERAND (arg, 2),
35e66bd1 2567 cval1, cval2, save_p));
c05a9b68 2568 return 0;
b6cc0a72 2569
c05a9b68
RS
2570 case '<':
2571 /* First see if we can handle the first operand, then the second. For
2572 the second operand, we know *CVAL1 can't be zero. It must be that
2573 one side of the comparison is each of the values; test for the
2574 case where this isn't true by failing if the two operands
2575 are the same. */
2576
2577 if (operand_equal_p (TREE_OPERAND (arg, 0),
2578 TREE_OPERAND (arg, 1), 0))
2579 return 0;
2580
2581 if (*cval1 == 0)
2582 *cval1 = TREE_OPERAND (arg, 0);
2583 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2584 ;
2585 else if (*cval2 == 0)
2586 *cval2 = TREE_OPERAND (arg, 0);
2587 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2588 ;
2589 else
2590 return 0;
2591
2592 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2593 ;
2594 else if (*cval2 == 0)
2595 *cval2 = TREE_OPERAND (arg, 1);
2596 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2597 ;
2598 else
2599 return 0;
2600
2601 return 1;
c05a9b68 2602
e9a25f70
JL
2603 default:
2604 return 0;
2605 }
c05a9b68
RS
2606}
2607\f
2608/* ARG is a tree that is known to contain just arithmetic operations and
2609 comparisons. Evaluate the operations in the tree substituting NEW0 for
f72aed24 2610 any occurrence of OLD0 as an operand of a comparison and likewise for
c05a9b68
RS
2611 NEW1 and OLD1. */
2612
2613static tree
2614eval_subst (arg, old0, new0, old1, new1)
2615 tree arg;
2616 tree old0, new0, old1, new1;
2617{
2618 tree type = TREE_TYPE (arg);
2619 enum tree_code code = TREE_CODE (arg);
2620 char class = TREE_CODE_CLASS (code);
2621
2622 /* We can handle some of the 'e' cases here. */
2623 if (class == 'e' && code == TRUTH_NOT_EXPR)
2624 class = '1';
2625 else if (class == 'e'
2626 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2627 class = '2';
2628
2629 switch (class)
2630 {
2631 case '1':
2632 return fold (build1 (code, type,
2633 eval_subst (TREE_OPERAND (arg, 0),
2634 old0, new0, old1, new1)));
2635
2636 case '2':
2637 return fold (build (code, type,
2638 eval_subst (TREE_OPERAND (arg, 0),
2639 old0, new0, old1, new1),
2640 eval_subst (TREE_OPERAND (arg, 1),
2641 old0, new0, old1, new1)));
2642
2643 case 'e':
2644 switch (code)
2645 {
2646 case SAVE_EXPR:
2647 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2648
2649 case COMPOUND_EXPR:
2650 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2651
2652 case COND_EXPR:
2653 return fold (build (code, type,
2654 eval_subst (TREE_OPERAND (arg, 0),
2655 old0, new0, old1, new1),
2656 eval_subst (TREE_OPERAND (arg, 1),
2657 old0, new0, old1, new1),
2658 eval_subst (TREE_OPERAND (arg, 2),
2659 old0, new0, old1, new1)));
e9a25f70
JL
2660 default:
2661 break;
c05a9b68 2662 }
e45ed4de 2663 /* fall through - ??? */
c05a9b68
RS
2664
2665 case '<':
2666 {
2667 tree arg0 = TREE_OPERAND (arg, 0);
2668 tree arg1 = TREE_OPERAND (arg, 1);
2669
2670 /* We need to check both for exact equality and tree equality. The
2671 former will be true if the operand has a side-effect. In that
2672 case, we know the operand occurred exactly once. */
2673
2674 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2675 arg0 = new0;
2676 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2677 arg0 = new1;
2678
2679 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2680 arg1 = new0;
2681 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2682 arg1 = new1;
2683
2684 return fold (build (code, type, arg0, arg1));
2685 }
c05a9b68 2686
e9a25f70
JL
2687 default:
2688 return arg;
2689 }
c05a9b68
RS
2690}
2691\f
6d716ca8
RS
2692/* Return a tree for the case when the result of an expression is RESULT
2693 converted to TYPE and OMITTED was previously an operand of the expression
2694 but is now not needed (e.g., we folded OMITTED * 0).
2695
2696 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2697 the conversion of RESULT to TYPE. */
2698
2699static tree
2700omit_one_operand (type, result, omitted)
2701 tree type, result, omitted;
2702{
2703 tree t = convert (type, result);
2704
2705 if (TREE_SIDE_EFFECTS (omitted))
2706 return build (COMPOUND_EXPR, type, omitted, t);
2707
d023bff9 2708 return non_lvalue (t);
6d716ca8 2709}
4ab3cb65
RK
2710
2711/* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2712
2713static tree
2714pedantic_omit_one_operand (type, result, omitted)
2715 tree type, result, omitted;
2716{
2717 tree t = convert (type, result);
2718
2719 if (TREE_SIDE_EFFECTS (omitted))
2720 return build (COMPOUND_EXPR, type, omitted, t);
2721
2722 return pedantic_non_lvalue (t);
2723}
6d716ca8 2724\f
3f783329
RS
2725/* Return a simplified tree node for the truth-negation of ARG. This
2726 never alters ARG itself. We assume that ARG is an operation that
6d716ca8
RS
2727 returns a truth value (0 or 1). */
2728
2729tree
2730invert_truthvalue (arg)
2731 tree arg;
2732{
2733 tree type = TREE_TYPE (arg);
c05a9b68 2734 enum tree_code code = TREE_CODE (arg);
6d716ca8 2735
8ac1abdf
JW
2736 if (code == ERROR_MARK)
2737 return arg;
2738
c05a9b68
RS
2739 /* If this is a comparison, we can simply invert it, except for
2740 floating-point non-equality comparisons, in which case we just
2741 enclose a TRUTH_NOT_EXPR around what we have. */
6d716ca8 2742
c05a9b68 2743 if (TREE_CODE_CLASS (code) == '<')
6d716ca8 2744 {
7178e3af 2745 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
de6c5979
BL
2746 && !flag_unsafe_math_optimizations
2747 && code != NE_EXPR
2748 && code != EQ_EXPR)
c05a9b68
RS
2749 return build1 (TRUTH_NOT_EXPR, type, arg);
2750 else
ca46db87 2751 return build (invert_tree_comparison (code), type,
3f783329 2752 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
c05a9b68 2753 }
6d716ca8 2754
c05a9b68
RS
2755 switch (code)
2756 {
6d716ca8 2757 case INTEGER_CST:
05bccae2 2758 return convert (type, build_int_2 (integer_zerop (arg), 0));
6d716ca8
RS
2759
2760 case TRUTH_AND_EXPR:
2761 return build (TRUTH_OR_EXPR, type,
2762 invert_truthvalue (TREE_OPERAND (arg, 0)),
2763 invert_truthvalue (TREE_OPERAND (arg, 1)));
2764
2765 case TRUTH_OR_EXPR:
2766 return build (TRUTH_AND_EXPR, type,
2767 invert_truthvalue (TREE_OPERAND (arg, 0)),
2768 invert_truthvalue (TREE_OPERAND (arg, 1)));
2769
772447c5
RK
2770 case TRUTH_XOR_EXPR:
2771 /* Here we can invert either operand. We invert the first operand
2772 unless the second operand is a TRUTH_NOT_EXPR in which case our
2773 result is the XOR of the first operand with the inside of the
2774 negation of the second operand. */
2775
2776 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2777 return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2778 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2779 else
2780 return build (TRUTH_XOR_EXPR, type,
2781 invert_truthvalue (TREE_OPERAND (arg, 0)),
2782 TREE_OPERAND (arg, 1));
2783
6d716ca8
RS
2784 case TRUTH_ANDIF_EXPR:
2785 return build (TRUTH_ORIF_EXPR, type,
2786 invert_truthvalue (TREE_OPERAND (arg, 0)),
2787 invert_truthvalue (TREE_OPERAND (arg, 1)));
2788
2789 case TRUTH_ORIF_EXPR:
2790 return build (TRUTH_ANDIF_EXPR, type,
2791 invert_truthvalue (TREE_OPERAND (arg, 0)),
2792 invert_truthvalue (TREE_OPERAND (arg, 1)));
2793
2794 case TRUTH_NOT_EXPR:
2795 return TREE_OPERAND (arg, 0);
2796
2797 case COND_EXPR:
2798 return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2799 invert_truthvalue (TREE_OPERAND (arg, 1)),
2800 invert_truthvalue (TREE_OPERAND (arg, 2)));
2801
ef9fe0da
RK
2802 case COMPOUND_EXPR:
2803 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2804 invert_truthvalue (TREE_OPERAND (arg, 1)));
2805
14a774a9
RK
2806 case WITH_RECORD_EXPR:
2807 return build (WITH_RECORD_EXPR, type,
2808 invert_truthvalue (TREE_OPERAND (arg, 0)),
2809 TREE_OPERAND (arg, 1));
2810
6d716ca8
RS
2811 case NON_LVALUE_EXPR:
2812 return invert_truthvalue (TREE_OPERAND (arg, 0));
2813
2814 case NOP_EXPR:
2815 case CONVERT_EXPR:
2816 case FLOAT_EXPR:
2817 return build1 (TREE_CODE (arg), type,
2818 invert_truthvalue (TREE_OPERAND (arg, 0)));
2819
2820 case BIT_AND_EXPR:
efc1a4d9
PB
2821 if (!integer_onep (TREE_OPERAND (arg, 1)))
2822 break;
6d716ca8 2823 return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
6d716ca8 2824
dfa90b42
RS
2825 case SAVE_EXPR:
2826 return build1 (TRUTH_NOT_EXPR, type, arg);
a25ee332
RK
2827
2828 case CLEANUP_POINT_EXPR:
2829 return build1 (CLEANUP_POINT_EXPR, type,
2830 invert_truthvalue (TREE_OPERAND (arg, 0)));
e9a25f70
JL
2831
2832 default:
2833 break;
efc1a4d9
PB
2834 }
2835 if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
dfa90b42 2836 abort ();
efc1a4d9 2837 return build1 (TRUTH_NOT_EXPR, type, arg);
6d716ca8
RS
2838}
2839
2840/* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2841 operands are another bit-wise operation with a common input. If so,
2842 distribute the bit operations to save an operation and possibly two if
2843 constants are involved. For example, convert
2844 (A | B) & (A | C) into A | (B & C)
2845 Further simplification will occur if B and C are constants.
2846
2847 If this optimization cannot be done, 0 will be returned. */
2848
2849static tree
2850distribute_bit_expr (code, type, arg0, arg1)
2851 enum tree_code code;
2852 tree type;
2853 tree arg0, arg1;
2854{
2855 tree common;
2856 tree left, right;
2857
2858 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2859 || TREE_CODE (arg0) == code
fced8ba3
RS
2860 || (TREE_CODE (arg0) != BIT_AND_EXPR
2861 && TREE_CODE (arg0) != BIT_IOR_EXPR))
6d716ca8
RS
2862 return 0;
2863
2864 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2865 {
2866 common = TREE_OPERAND (arg0, 0);
2867 left = TREE_OPERAND (arg0, 1);
2868 right = TREE_OPERAND (arg1, 1);
2869 }
2870 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2871 {
2872 common = TREE_OPERAND (arg0, 0);
2873 left = TREE_OPERAND (arg0, 1);
2874 right = TREE_OPERAND (arg1, 0);
2875 }
2876 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2877 {
2878 common = TREE_OPERAND (arg0, 1);
2879 left = TREE_OPERAND (arg0, 0);
2880 right = TREE_OPERAND (arg1, 1);
2881 }
2882 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2883 {
2884 common = TREE_OPERAND (arg0, 1);
2885 left = TREE_OPERAND (arg0, 0);
2886 right = TREE_OPERAND (arg1, 0);
2887 }
2888 else
2889 return 0;
2890
2891 return fold (build (TREE_CODE (arg0), type, common,
2892 fold (build (code, type, left, right))));
2893}
2894\f
2895/* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2896 starting at BITPOS. The field is unsigned if UNSIGNEDP is non-zero. */
2897
2898static tree
2899make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
2900 tree inner;
2901 tree type;
2902 int bitsize, bitpos;
2903 int unsignedp;
2904{
2905 tree result = build (BIT_FIELD_REF, type, inner,
06ceef4e 2906 size_int (bitsize), bitsize_int (bitpos));
6d716ca8
RS
2907
2908 TREE_UNSIGNED (result) = unsignedp;
2909
2910 return result;
2911}
2912
2913/* Optimize a bit-field compare.
2914
2915 There are two cases: First is a compare against a constant and the
2916 second is a comparison of two items where the fields are at the same
2917 bit position relative to the start of a chunk (byte, halfword, word)
2918 large enough to contain it. In these cases we can avoid the shift
2919 implicit in bitfield extractions.
2920
2921 For constants, we emit a compare of the shifted constant with the
2922 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2923 compared. For two fields at the same position, we do the ANDs with the
2924 similar mask and compare the result of the ANDs.
2925
2926 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2927 COMPARE_TYPE is the type of the comparison, and LHS and RHS
2928 are the left and right operands of the comparison, respectively.
2929
6dc42e49 2930 If the optimization described above can be done, we return the resulting
6d716ca8
RS
2931 tree. Otherwise we return zero. */
2932
2933static tree
2934optimize_bit_field_compare (code, compare_type, lhs, rhs)
2935 enum tree_code code;
2936 tree compare_type;
2937 tree lhs, rhs;
2938{
770ae6cc 2939 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
6d716ca8
RS
2940 tree type = TREE_TYPE (lhs);
2941 tree signed_type, unsigned_type;
2942 int const_p = TREE_CODE (rhs) == INTEGER_CST;
5826cacf 2943 enum machine_mode lmode, rmode, nmode;
6d716ca8
RS
2944 int lunsignedp, runsignedp;
2945 int lvolatilep = 0, rvolatilep = 0;
770ae6cc 2946 unsigned int alignment;
4e86caed 2947 tree linner, rinner = NULL_TREE;
6d716ca8 2948 tree mask;
f1e60ec6 2949 tree offset;
6d716ca8
RS
2950
2951 /* Get all the information about the extractions being done. If the bit size
2952 if the same as the size of the underlying object, we aren't doing an
14a774a9
RK
2953 extraction at all and so can do nothing. We also don't want to
2954 do anything if the inner expression is a PLACEHOLDER_EXPR since we
2955 then will no longer be able to replace it. */
f1e60ec6 2956 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
23bd99ae 2957 &lunsignedp, &lvolatilep, &alignment);
9f5e873c 2958 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
14a774a9 2959 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
6d716ca8
RS
2960 return 0;
2961
2962 if (!const_p)
2963 {
2964 /* If this is not a constant, we can only do something if bit positions,
2965 sizes, and signedness are the same. */
23bd99ae
RK
2966 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
2967 &runsignedp, &rvolatilep, &alignment);
6d716ca8 2968
9f5e873c 2969 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
14a774a9
RK
2970 || lunsignedp != runsignedp || offset != 0
2971 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
6d716ca8
RS
2972 return 0;
2973 }
2974
2975 /* See if we can find a mode to refer to this field. We should be able to,
2976 but fail if we can't. */
5826cacf
RK
2977 nmode = get_best_mode (lbitsize, lbitpos,
2978 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
2979 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
2980 TYPE_ALIGN (TREE_TYPE (rinner))),
2981 word_mode, lvolatilep || rvolatilep);
2982 if (nmode == VOIDmode)
6d716ca8
RS
2983 return 0;
2984
2985 /* Set signed and unsigned types of the precision of this mode for the
2986 shifts below. */
5826cacf
RK
2987 signed_type = type_for_mode (nmode, 0);
2988 unsigned_type = type_for_mode (nmode, 1);
6d716ca8 2989
6d716ca8
RS
2990 /* Compute the bit position and size for the new reference and our offset
2991 within it. If the new reference is the same size as the original, we
2992 won't optimize anything, so return zero. */
5826cacf
RK
2993 nbitsize = GET_MODE_BITSIZE (nmode);
2994 nbitpos = lbitpos & ~ (nbitsize - 1);
2995 lbitpos -= nbitpos;
2996 if (nbitsize == lbitsize)
6d716ca8
RS
2997 return 0;
2998
f76b9db2 2999 if (BYTES_BIG_ENDIAN)
5826cacf 3000 lbitpos = nbitsize - lbitsize - lbitpos;
6d716ca8
RS
3001
3002 /* Make the mask to be used against the extracted field. */
13af526d
RS
3003 mask = build_int_2 (~0, ~0);
3004 TREE_TYPE (mask) = unsigned_type;
aa830baf 3005 force_fit_type (mask, 0);
13af526d 3006 mask = convert (unsigned_type, mask);
5826cacf 3007 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
6d716ca8 3008 mask = const_binop (RSHIFT_EXPR, mask,
5826cacf 3009 size_int (nbitsize - lbitsize - lbitpos), 0);
6d716ca8
RS
3010
3011 if (! const_p)
3012 /* If not comparing with constant, just rework the comparison
3013 and return. */
3014 return build (code, compare_type,
c0b9d4c8
RK
3015 build (BIT_AND_EXPR, unsigned_type,
3016 make_bit_field_ref (linner, unsigned_type,
5826cacf 3017 nbitsize, nbitpos, 1),
6d716ca8 3018 mask),
c0b9d4c8
RK
3019 build (BIT_AND_EXPR, unsigned_type,
3020 make_bit_field_ref (rinner, unsigned_type,
5826cacf 3021 nbitsize, nbitpos, 1),
6d716ca8
RS
3022 mask));
3023
3024 /* Otherwise, we are handling the constant case. See if the constant is too
3025 big for the field. Warn and return a tree of for 0 (false) if so. We do
3026 this not only for its own sake, but to avoid having to test for this
3027 error case below. If we didn't, we might generate wrong code.
3028
3029 For unsigned fields, the constant shifted right by the field length should
b6cc0a72 3030 be all zero. For signed fields, the high-order bits should agree with
6d716ca8
RS
3031 the sign bit. */
3032
3033 if (lunsignedp)
3034 {
3035 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3036 convert (unsigned_type, rhs),
91d33e36 3037 size_int (lbitsize), 0)))
6d716ca8 3038 {
ab87f8c8
JL
3039 warning ("comparison is always %d due to width of bitfield",
3040 code == NE_EXPR);
6d716ca8
RS
3041 return convert (compare_type,
3042 (code == NE_EXPR
3043 ? integer_one_node : integer_zero_node));
3044 }
3045 }
3046 else
3047 {
3048 tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
91d33e36 3049 size_int (lbitsize - 1), 0);
6d716ca8
RS
3050 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3051 {
ab87f8c8
JL
3052 warning ("comparison is always %d due to width of bitfield",
3053 code == NE_EXPR);
6d716ca8
RS
3054 return convert (compare_type,
3055 (code == NE_EXPR
3056 ? integer_one_node : integer_zero_node));
3057 }
3058 }
3059
3060 /* Single-bit compares should always be against zero. */
3061 if (lbitsize == 1 && ! integer_zerop (rhs))
3062 {
3063 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3064 rhs = convert (type, integer_zero_node);
3065 }
3066
3067 /* Make a new bitfield reference, shift the constant over the
3068 appropriate number of bits and mask it with the computed mask
3069 (in case this was a signed field). If we changed it, make a new one. */
5826cacf 3070 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
9db73acb
RK
3071 if (lvolatilep)
3072 {
3073 TREE_SIDE_EFFECTS (lhs) = 1;
3074 TREE_THIS_VOLATILE (lhs) = 1;
3075 }
6d716ca8 3076
c0b9d4c8
RK
3077 rhs = fold (const_binop (BIT_AND_EXPR,
3078 const_binop (LSHIFT_EXPR,
3079 convert (unsigned_type, rhs),
194c082f 3080 size_int (lbitpos), 0),
91d33e36 3081 mask, 0));
6d716ca8
RS
3082
3083 return build (code, compare_type,
c0b9d4c8 3084 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
6d716ca8
RS
3085 rhs);
3086}
3087\f
b2215d83 3088/* Subroutine for fold_truthop: decode a field reference.
6d716ca8
RS
3089
3090 If EXP is a comparison reference, we return the innermost reference.
3091
3092 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3093 set to the starting bit number.
3094
3095 If the innermost field can be completely contained in a mode-sized
3096 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3097
3098 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3099 otherwise it is not changed.
3100
3101 *PUNSIGNEDP is set to the signedness of the field.
3102
3103 *PMASK is set to the mask used. This is either contained in a
3104 BIT_AND_EXPR or derived from the width of the field.
3105
38e01259 3106 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
d4453ee5 3107
6d716ca8
RS
3108 Return 0 if this is not a component reference or is one that we can't
3109 do anything with. */
3110
3111static tree
3112decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
d4453ee5 3113 pvolatilep, pmask, pand_mask)
6d716ca8 3114 tree exp;
770ae6cc 3115 HOST_WIDE_INT *pbitsize, *pbitpos;
6d716ca8
RS
3116 enum machine_mode *pmode;
3117 int *punsignedp, *pvolatilep;
3118 tree *pmask;
d4453ee5 3119 tree *pand_mask;
6d716ca8 3120{
6d9f1f5f
RK
3121 tree and_mask = 0;
3122 tree mask, inner, offset;
3123 tree unsigned_type;
770ae6cc
RK
3124 unsigned int precision;
3125 unsigned int alignment;
6d716ca8 3126
b6cc0a72 3127 /* All the optimizations using this function assume integer fields.
772ae9f0
RK
3128 There are problems with FP fields since the type_for_size call
3129 below can fail for, e.g., XFmode. */
3130 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3131 return 0;
3132
6d716ca8
RS
3133 STRIP_NOPS (exp);
3134
3135 if (TREE_CODE (exp) == BIT_AND_EXPR)
3136 {
6d9f1f5f 3137 and_mask = TREE_OPERAND (exp, 1);
6d716ca8 3138 exp = TREE_OPERAND (exp, 0);
6d9f1f5f
RK
3139 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3140 if (TREE_CODE (and_mask) != INTEGER_CST)
6d716ca8
RS
3141 return 0;
3142 }
3143
f1e60ec6 3144 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
23bd99ae 3145 punsignedp, pvolatilep, &alignment);
02103577 3146 if ((inner == exp && and_mask == 0)
14a774a9
RK
3147 || *pbitsize < 0 || offset != 0
3148 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
c05a9b68 3149 return 0;
b6cc0a72 3150
6d9f1f5f
RK
3151 /* Compute the mask to access the bitfield. */
3152 unsigned_type = type_for_size (*pbitsize, 1);
3153 precision = TYPE_PRECISION (unsigned_type);
3154
3155 mask = build_int_2 (~0, ~0);
3156 TREE_TYPE (mask) = unsigned_type;
3157 force_fit_type (mask, 0);
3158 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3159 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3160
3161 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3162 if (and_mask != 0)
3163 mask = fold (build (BIT_AND_EXPR, unsigned_type,
3164 convert (unsigned_type, and_mask), mask));
6d716ca8
RS
3165
3166 *pmask = mask;
d4453ee5 3167 *pand_mask = and_mask;
6d716ca8
RS
3168 return inner;
3169}
3170
6dc42e49 3171/* Return non-zero if MASK represents a mask of SIZE ones in the low-order
6d716ca8
RS
3172 bit positions. */
3173
3174static int
3175all_ones_mask_p (mask, size)
3176 tree mask;
3177 int size;
3178{
3179 tree type = TREE_TYPE (mask);
770ae6cc 3180 unsigned int precision = TYPE_PRECISION (type);
13af526d 3181 tree tmask;
6d716ca8 3182
13af526d
RS
3183 tmask = build_int_2 (~0, ~0);
3184 TREE_TYPE (tmask) = signed_type (type);
aa830baf 3185 force_fit_type (tmask, 0);
6d716ca8 3186 return
b6cc0a72 3187 tree_int_cst_equal (mask,
02103577
RK
3188 const_binop (RSHIFT_EXPR,
3189 const_binop (LSHIFT_EXPR, tmask,
3190 size_int (precision - size),
3191 0),
3192 size_int (precision - size), 0));
6d716ca8 3193}
b2215d83
TW
3194
3195/* Subroutine for fold_truthop: determine if an operand is simple enough
3196 to be evaluated unconditionally. */
3197
b6cc0a72 3198static int
b2215d83
TW
3199simple_operand_p (exp)
3200 tree exp;
3201{
3202 /* Strip any conversions that don't change the machine mode. */
3203 while ((TREE_CODE (exp) == NOP_EXPR
3204 || TREE_CODE (exp) == CONVERT_EXPR)
3205 && (TYPE_MODE (TREE_TYPE (exp))
3206 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
3207 exp = TREE_OPERAND (exp, 0);
3208
3209 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
2f939d94 3210 || (DECL_P (exp)
b2215d83
TW
3211 && ! TREE_ADDRESSABLE (exp)
3212 && ! TREE_THIS_VOLATILE (exp)
8227896c
TW
3213 && ! DECL_NONLOCAL (exp)
3214 /* Don't regard global variables as simple. They may be
3215 allocated in ways unknown to the compiler (shared memory,
3216 #pragma weak, etc). */
3217 && ! TREE_PUBLIC (exp)
3218 && ! DECL_EXTERNAL (exp)
3219 /* Loading a static variable is unduly expensive, but global
3220 registers aren't expensive. */
3221 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
b2215d83 3222}
6d716ca8 3223\f
ebde8a27
RK
3224/* The following functions are subroutines to fold_range_test and allow it to
3225 try to change a logical combination of comparisons into a range test.
3226
3227 For example, both
f42ef510 3228 X == 2 || X == 3 || X == 4 || X == 5
ebde8a27
RK
3229 and
3230 X >= 2 && X <= 5
3231 are converted to
3232 (unsigned) (X - 2) <= 3
3233
956d6950 3234 We describe each set of comparisons as being either inside or outside
ebde8a27
RK
3235 a range, using a variable named like IN_P, and then describe the
3236 range with a lower and upper bound. If one of the bounds is omitted,
3237 it represents either the highest or lowest value of the type.
3238
3239 In the comments below, we represent a range by two numbers in brackets
956d6950 3240 preceded by a "+" to designate being inside that range, or a "-" to
ebde8a27
RK
3241 designate being outside that range, so the condition can be inverted by
3242 flipping the prefix. An omitted bound is represented by a "-". For
3243 example, "- [-, 10]" means being outside the range starting at the lowest
3244 possible value and ending at 10, in other words, being greater than 10.
3245 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3246 always false.
3247
3248 We set up things so that the missing bounds are handled in a consistent
3249 manner so neither a missing bound nor "true" and "false" need to be
3250 handled using a special case. */
3251
3252/* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3253 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3254 and UPPER1_P are nonzero if the respective argument is an upper bound
3255 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3256 must be specified for a comparison. ARG1 will be converted to ARG0's
3257 type if both are specified. */
ef659ec0 3258
ebde8a27
RK
3259static tree
3260range_binop (code, type, arg0, upper0_p, arg1, upper1_p)
3261 enum tree_code code;
3262 tree type;
3263 tree arg0, arg1;
3264 int upper0_p, upper1_p;
3265{
27bae8e5 3266 tree tem;
ebde8a27
RK
3267 int result;
3268 int sgn0, sgn1;
ef659ec0 3269
ebde8a27
RK
3270 /* If neither arg represents infinity, do the normal operation.
3271 Else, if not a comparison, return infinity. Else handle the special
3272 comparison rules. Note that most of the cases below won't occur, but
3273 are handled for consistency. */
ef659ec0 3274
ebde8a27 3275 if (arg0 != 0 && arg1 != 0)
27bae8e5
RK
3276 {
3277 tem = fold (build (code, type != 0 ? type : TREE_TYPE (arg0),
3278 arg0, convert (TREE_TYPE (arg0), arg1)));
3279 STRIP_NOPS (tem);
3280 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3281 }
ef659ec0 3282
ebde8a27
RK
3283 if (TREE_CODE_CLASS (code) != '<')
3284 return 0;
3285
3286 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
d7b3ea38
NS
3287 for neither. In real maths, we cannot assume open ended ranges are
3288 the same. But, this is computer arithmetic, where numbers are finite.
3289 We can therefore make the transformation of any unbounded range with
3290 the value Z, Z being greater than any representable number. This permits
30f7a378 3291 us to treat unbounded ranges as equal. */
ebde8a27 3292 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4e644c93 3293 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
ebde8a27
RK
3294 switch (code)
3295 {
d7b3ea38
NS
3296 case EQ_EXPR:
3297 result = sgn0 == sgn1;
3298 break;
3299 case NE_EXPR:
3300 result = sgn0 != sgn1;
ebde8a27 3301 break;
d7b3ea38 3302 case LT_EXPR:
ebde8a27
RK
3303 result = sgn0 < sgn1;
3304 break;
d7b3ea38
NS
3305 case LE_EXPR:
3306 result = sgn0 <= sgn1;
3307 break;
3308 case GT_EXPR:
ebde8a27
RK
3309 result = sgn0 > sgn1;
3310 break;
d7b3ea38
NS
3311 case GE_EXPR:
3312 result = sgn0 >= sgn1;
3313 break;
e9a25f70
JL
3314 default:
3315 abort ();
ebde8a27
RK
3316 }
3317
3318 return convert (type, result ? integer_one_node : integer_zero_node);
3319}
b6cc0a72 3320\f
ebde8a27
RK
3321/* Given EXP, a logical expression, set the range it is testing into
3322 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
f42ef510 3323 actually being tested. *PLOW and *PHIGH will be made of the same type
ebde8a27
RK
3324 as the returned expression. If EXP is not a comparison, we will most
3325 likely not be returning a useful value and range. */
ef659ec0 3326
6dc7571d 3327static tree
ebde8a27
RK
3328make_range (exp, pin_p, plow, phigh)
3329 tree exp;
3330 int *pin_p;
3331 tree *plow, *phigh;
ef659ec0 3332{
ebde8a27 3333 enum tree_code code;
07444f1d 3334 tree arg0 = NULL_TREE, arg1 = NULL_TREE, type = NULL_TREE;
7d12cee1 3335 tree orig_type = NULL_TREE;
ebde8a27
RK
3336 int in_p, n_in_p;
3337 tree low, high, n_low, n_high;
ef659ec0 3338
ebde8a27
RK
3339 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3340 and see if we can refine the range. Some of the cases below may not
3341 happen, but it doesn't seem worth worrying about this. We "continue"
3342 the outer loop when we've changed something; otherwise we "break"
3343 the switch, which will "break" the while. */
ef659ec0 3344
ebde8a27
RK
3345 in_p = 0, low = high = convert (TREE_TYPE (exp), integer_zero_node);
3346
3347 while (1)
ef659ec0 3348 {
ebde8a27 3349 code = TREE_CODE (exp);
30d68b86
MM
3350
3351 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3352 {
3353 arg0 = TREE_OPERAND (exp, 0);
b6cc0a72 3354 if (TREE_CODE_CLASS (code) == '<'
30d68b86
MM
3355 || TREE_CODE_CLASS (code) == '1'
3356 || TREE_CODE_CLASS (code) == '2')
3357 type = TREE_TYPE (arg0);
b6cc0a72 3358 if (TREE_CODE_CLASS (code) == '2'
30d68b86 3359 || TREE_CODE_CLASS (code) == '<'
b6cc0a72 3360 || (TREE_CODE_CLASS (code) == 'e'
efd58783 3361 && TREE_CODE_LENGTH (code) > 1))
30d68b86
MM
3362 arg1 = TREE_OPERAND (exp, 1);
3363 }
ef659ec0 3364
cc33944a
RE
3365 /* Set ORIG_TYPE as soon as TYPE is non-null so that we do not
3366 lose a cast by accident. */
3367 if (type != NULL_TREE && orig_type == NULL_TREE)
3368 orig_type = type;
3369
ebde8a27
RK
3370 switch (code)
3371 {
3372 case TRUTH_NOT_EXPR:
3373 in_p = ! in_p, exp = arg0;
3374 continue;
3375
3376 case EQ_EXPR: case NE_EXPR:
3377 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3378 /* We can only do something if the range is testing for zero
3379 and if the second operand is an integer constant. Note that
3380 saying something is "in" the range we make is done by
3381 complementing IN_P since it will set in the initial case of
3382 being not equal to zero; "out" is leaving it alone. */
3383 if (low == 0 || high == 0
3384 || ! integer_zerop (low) || ! integer_zerop (high)
3385 || TREE_CODE (arg1) != INTEGER_CST)
3386 break;
ef659ec0 3387
ebde8a27
RK
3388 switch (code)
3389 {
3390 case NE_EXPR: /* - [c, c] */
3391 low = high = arg1;
3392 break;
3393 case EQ_EXPR: /* + [c, c] */
3394 in_p = ! in_p, low = high = arg1;
3395 break;
3396 case GT_EXPR: /* - [-, c] */
3397 low = 0, high = arg1;
3398 break;
3399 case GE_EXPR: /* + [c, -] */
3400 in_p = ! in_p, low = arg1, high = 0;
3401 break;
3402 case LT_EXPR: /* - [c, -] */
3403 low = arg1, high = 0;
3404 break;
3405 case LE_EXPR: /* + [-, c] */
3406 in_p = ! in_p, low = 0, high = arg1;
3407 break;
e9a25f70
JL
3408 default:
3409 abort ();
ebde8a27 3410 }
ef659ec0 3411
ebde8a27 3412 exp = arg0;
ef659ec0 3413
7f423031 3414 /* If this is an unsigned comparison, we also know that EXP is
0e1c7fc7
RK
3415 greater than or equal to zero. We base the range tests we make
3416 on that fact, so we record it here so we can parse existing
3417 range tests. */
7f423031 3418 if (TREE_UNSIGNED (type) && (low == 0 || high == 0))
ebde8a27
RK
3419 {
3420 if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
3421 1, convert (type, integer_zero_node),
0e1c7fc7 3422 NULL_TREE))
ebde8a27 3423 break;
ef659ec0 3424
ebde8a27 3425 in_p = n_in_p, low = n_low, high = n_high;
0e1c7fc7 3426
c565a6b7
R
3427 /* If the high bound is missing, but we
3428 have a low bound, reverse the range so
3429 it goes from zero to the low bound minus 1. */
3430 if (high == 0 && low)
0e1c7fc7
RK
3431 {
3432 in_p = ! in_p;
3433 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3434 integer_one_node, 0);
3435 low = convert (type, integer_zero_node);
3436 }
ebde8a27
RK
3437 }
3438 continue;
3439
3440 case NEGATE_EXPR:
3441 /* (-x) IN [a,b] -> x in [-b, -a] */
3442 n_low = range_binop (MINUS_EXPR, type,
3443 convert (type, integer_zero_node), 0, high, 1);
3444 n_high = range_binop (MINUS_EXPR, type,
3445 convert (type, integer_zero_node), 0, low, 0);
3446 low = n_low, high = n_high;
3447 exp = arg0;
3448 continue;
3449
3450 case BIT_NOT_EXPR:
3451 /* ~ X -> -X - 1 */
1baa375f 3452 exp = build (MINUS_EXPR, type, negate_expr (arg0),
27bae8e5 3453 convert (type, integer_one_node));
ebde8a27
RK
3454 continue;
3455
3456 case PLUS_EXPR: case MINUS_EXPR:
3457 if (TREE_CODE (arg1) != INTEGER_CST)
3458 break;
3459
3460 /* If EXP is signed, any overflow in the computation is undefined,
3461 so we don't worry about it so long as our computations on
3462 the bounds don't overflow. For unsigned, overflow is defined
3463 and this is exactly the right thing. */
3464 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3465 type, low, 0, arg1, 0);
3466 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3467 type, high, 1, arg1, 0);
3468 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3469 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3470 break;
3471
3c00684e
JL
3472 /* Check for an unsigned range which has wrapped around the maximum
3473 value thus making n_high < n_low, and normalize it. */
5a9d82a6 3474 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3c00684e
JL
3475 {
3476 low = range_binop (PLUS_EXPR, type, n_high, 0,
0e1c7fc7 3477 integer_one_node, 0);
3c00684e 3478 high = range_binop (MINUS_EXPR, type, n_low, 0,
c2b63960
AO
3479 integer_one_node, 0);
3480
3481 /* If the range is of the form +/- [ x+1, x ], we won't
3482 be able to normalize it. But then, it represents the
3483 whole range or the empty set, so make it
3484 +/- [ -, - ]. */
3485 if (tree_int_cst_equal (n_low, low)
3486 && tree_int_cst_equal (n_high, high))
3487 low = high = 0;
3488 else
3489 in_p = ! in_p;
3c00684e 3490 }
5a9d82a6
JW
3491 else
3492 low = n_low, high = n_high;
27bae8e5 3493
ebde8a27
RK
3494 exp = arg0;
3495 continue;
3496
3497 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
7d12cee1
JL
3498 if (TYPE_PRECISION (type) > TYPE_PRECISION (orig_type))
3499 break;
3500
ebde8a27
RK
3501 if (! INTEGRAL_TYPE_P (type)
3502 || (low != 0 && ! int_fits_type_p (low, type))
3503 || (high != 0 && ! int_fits_type_p (high, type)))
3504 break;
3505
ce2157a1 3506 n_low = low, n_high = high;
ebde8a27 3507
ce2157a1
JL
3508 if (n_low != 0)
3509 n_low = convert (type, n_low);
3510
3511 if (n_high != 0)
3512 n_high = convert (type, n_high);
3513
3514 /* If we're converting from an unsigned to a signed type,
3515 we will be doing the comparison as unsigned. The tests above
3516 have already verified that LOW and HIGH are both positive.
3517
3518 So we have to make sure that the original unsigned value will
3519 be interpreted as positive. */
3520 if (TREE_UNSIGNED (type) && ! TREE_UNSIGNED (TREE_TYPE (exp)))
3521 {
3522 tree equiv_type = type_for_mode (TYPE_MODE (type), 1);
e1ee5cdc
RH
3523 tree high_positive;
3524
3525 /* A range without an upper bound is, naturally, unbounded.
3526 Since convert would have cropped a very large value, use
14a774a9
RK
3527 the max value for the destination type. */
3528 high_positive
3529 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3530 : TYPE_MAX_VALUE (type);
e1ee5cdc 3531
e1ee5cdc
RH
3532 high_positive = fold (build (RSHIFT_EXPR, type,
3533 convert (type, high_positive),
3534 convert (type, integer_one_node)));
b6cc0a72 3535
ce2157a1
JL
3536 /* If the low bound is specified, "and" the range with the
3537 range for which the original unsigned value will be
3538 positive. */
3539 if (low != 0)
3540 {
3541 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3542 1, n_low, n_high,
3543 1, convert (type, integer_zero_node),
3544 high_positive))
3545 break;
3546
3547 in_p = (n_in_p == in_p);
3548 }
3549 else
3550 {
3551 /* Otherwise, "or" the range with the range of the input
3552 that will be interpreted as negative. */
3553 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3554 0, n_low, n_high,
3555 1, convert (type, integer_zero_node),
3556 high_positive))
3557 break;
3558
3559 in_p = (in_p != n_in_p);
3560 }
3561 }
ebde8a27
RK
3562
3563 exp = arg0;
ce2157a1 3564 low = n_low, high = n_high;
ebde8a27 3565 continue;
ce2157a1
JL
3566
3567 default:
3568 break;
ef659ec0 3569 }
ebde8a27
RK
3570
3571 break;
ef659ec0 3572 }
ebde8a27 3573
80906567
RK
3574 /* If EXP is a constant, we can evaluate whether this is true or false. */
3575 if (TREE_CODE (exp) == INTEGER_CST)
3576 {
3577 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3578 exp, 0, low, 0))
3579 && integer_onep (range_binop (LE_EXPR, integer_type_node,
3580 exp, 1, high, 1)));
3581 low = high = 0;
3582 exp = 0;
3583 }
3584
ebde8a27
RK
3585 *pin_p = in_p, *plow = low, *phigh = high;
3586 return exp;
3587}
3588\f
3589/* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3590 type, TYPE, return an expression to test if EXP is in (or out of, depending
3591 on IN_P) the range. */
3592
3593static tree
3594build_range_check (type, exp, in_p, low, high)
3595 tree type;
3596 tree exp;
3597 int in_p;
3598 tree low, high;
3599{
3600 tree etype = TREE_TYPE (exp);
3601 tree utype, value;
3602
3603 if (! in_p
3604 && (0 != (value = build_range_check (type, exp, 1, low, high))))
3605 return invert_truthvalue (value);
3606
3607 else if (low == 0 && high == 0)
3608 return convert (type, integer_one_node);
3609
3610 else if (low == 0)
3611 return fold (build (LE_EXPR, type, exp, high));
3612
3613 else if (high == 0)
3614 return fold (build (GE_EXPR, type, exp, low));
3615
3616 else if (operand_equal_p (low, high, 0))
3617 return fold (build (EQ_EXPR, type, exp, low));
3618
3619 else if (TREE_UNSIGNED (etype) && integer_zerop (low))
3620 return build_range_check (type, exp, 1, 0, high);
3621
3622 else if (integer_zerop (low))
ef659ec0 3623 {
ebde8a27
RK
3624 utype = unsigned_type (etype);
3625 return build_range_check (type, convert (utype, exp), 1, 0,
3626 convert (utype, high));
3627 }
ef659ec0 3628
ebde8a27
RK
3629 else if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
3630 && ! TREE_OVERFLOW (value))
3631 return build_range_check (type,
3632 fold (build (MINUS_EXPR, etype, exp, low)),
3633 1, convert (etype, integer_zero_node), value);
3634 else
3635 return 0;
3636}
3637\f
b6cc0a72 3638/* Given two ranges, see if we can merge them into one. Return 1 if we
ebde8a27 3639 can, 0 if we can't. Set the output range into the specified parameters. */
ef659ec0 3640
ebde8a27
RK
3641static int
3642merge_ranges (pin_p, plow, phigh, in0_p, low0, high0, in1_p, low1, high1)
3643 int *pin_p;
3644 tree *plow, *phigh;
3645 int in0_p, in1_p;
3646 tree low0, high0, low1, high1;
3647{
3648 int no_overlap;
3649 int subset;
3650 int temp;
3651 tree tem;
3652 int in_p;
3653 tree low, high;
ce2157a1
JL
3654 int lowequal = ((low0 == 0 && low1 == 0)
3655 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3656 low0, 0, low1, 0)));
3657 int highequal = ((high0 == 0 && high1 == 0)
3658 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3659 high0, 1, high1, 1)));
3660
3661 /* Make range 0 be the range that starts first, or ends last if they
3662 start at the same value. Swap them if it isn't. */
b6cc0a72 3663 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
ebde8a27 3664 low0, 0, low1, 0))
ce2157a1 3665 || (lowequal
ebde8a27 3666 && integer_onep (range_binop (GT_EXPR, integer_type_node,
ce2157a1 3667 high1, 1, high0, 1))))
ebde8a27
RK
3668 {
3669 temp = in0_p, in0_p = in1_p, in1_p = temp;
3670 tem = low0, low0 = low1, low1 = tem;
3671 tem = high0, high0 = high1, high1 = tem;
3672 }
ef659ec0 3673
ebde8a27
RK
3674 /* Now flag two cases, whether the ranges are disjoint or whether the
3675 second range is totally subsumed in the first. Note that the tests
3676 below are simplified by the ones above. */
3677 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3678 high0, 1, low1, 0));
5df8a1f2 3679 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
ebde8a27
RK
3680 high1, 1, high0, 1));
3681
3682 /* We now have four cases, depending on whether we are including or
3683 excluding the two ranges. */
3684 if (in0_p && in1_p)
3685 {
3686 /* If they don't overlap, the result is false. If the second range
3687 is a subset it is the result. Otherwise, the range is from the start
3688 of the second to the end of the first. */
3689 if (no_overlap)
3690 in_p = 0, low = high = 0;
3691 else if (subset)
3692 in_p = 1, low = low1, high = high1;
3693 else
3694 in_p = 1, low = low1, high = high0;
3695 }
ef659ec0 3696
ebde8a27
RK
3697 else if (in0_p && ! in1_p)
3698 {
ce2157a1
JL
3699 /* If they don't overlap, the result is the first range. If they are
3700 equal, the result is false. If the second range is a subset of the
3701 first, and the ranges begin at the same place, we go from just after
3702 the end of the first range to the end of the second. If the second
3703 range is not a subset of the first, or if it is a subset and both
3704 ranges end at the same place, the range starts at the start of the
3705 first range and ends just before the second range.
3706 Otherwise, we can't describe this as a single range. */
ebde8a27
RK
3707 if (no_overlap)
3708 in_p = 1, low = low0, high = high0;
ce2157a1 3709 else if (lowequal && highequal)
405862dd 3710 in_p = 0, low = high = 0;
ce2157a1
JL
3711 else if (subset && lowequal)
3712 {
3713 in_p = 1, high = high0;
3714 low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
b6cc0a72 3715 integer_one_node, 0);
ce2157a1
JL
3716 }
3717 else if (! subset || highequal)
ebde8a27
RK
3718 {
3719 in_p = 1, low = low0;
3720 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
0e1c7fc7 3721 integer_one_node, 0);
ebde8a27 3722 }
ce2157a1
JL
3723 else
3724 return 0;
ebde8a27 3725 }
ef659ec0 3726
ebde8a27
RK
3727 else if (! in0_p && in1_p)
3728 {
3729 /* If they don't overlap, the result is the second range. If the second
3730 is a subset of the first, the result is false. Otherwise,
3731 the range starts just after the first range and ends at the
3732 end of the second. */
3733 if (no_overlap)
3734 in_p = 1, low = low1, high = high1;
14a774a9 3735 else if (subset || highequal)
ebde8a27
RK
3736 in_p = 0, low = high = 0;
3737 else
3738 {
3739 in_p = 1, high = high1;
3740 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3741 integer_one_node, 0);
ef659ec0
TW
3742 }
3743 }
3744
ebde8a27
RK
3745 else
3746 {
3747 /* The case where we are excluding both ranges. Here the complex case
3748 is if they don't overlap. In that case, the only time we have a
3749 range is if they are adjacent. If the second is a subset of the
3750 first, the result is the first. Otherwise, the range to exclude
3751 starts at the beginning of the first range and ends at the end of the
3752 second. */
3753 if (no_overlap)
3754 {
3755 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3756 range_binop (PLUS_EXPR, NULL_TREE,
3757 high0, 1,
3758 integer_one_node, 1),
3759 1, low1, 0)))
3760 in_p = 0, low = low0, high = high1;
3761 else
3762 return 0;
3763 }
3764 else if (subset)
3765 in_p = 0, low = low0, high = high0;
3766 else
3767 in_p = 0, low = low0, high = high1;
3768 }
f5902869 3769
ebde8a27
RK
3770 *pin_p = in_p, *plow = low, *phigh = high;
3771 return 1;
3772}
3773\f
3774/* EXP is some logical combination of boolean tests. See if we can
3775 merge it into some range test. Return the new tree if so. */
ef659ec0 3776
ebde8a27
RK
3777static tree
3778fold_range_test (exp)
3779 tree exp;
3780{
3781 int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3782 || TREE_CODE (exp) == TRUTH_OR_EXPR);
3783 int in0_p, in1_p, in_p;
3784 tree low0, low1, low, high0, high1, high;
3785 tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
3786 tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
3787 tree tem;
ef659ec0 3788
ebde8a27
RK
3789 /* If this is an OR operation, invert both sides; we will invert
3790 again at the end. */
3791 if (or_op)
3792 in0_p = ! in0_p, in1_p = ! in1_p;
3793
3794 /* If both expressions are the same, if we can merge the ranges, and we
80906567
RK
3795 can build the range test, return it or it inverted. If one of the
3796 ranges is always true or always false, consider it to be the same
3797 expression as the other. */
3798 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
ebde8a27
RK
3799 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
3800 in1_p, low1, high1)
80906567
RK
3801 && 0 != (tem = (build_range_check (TREE_TYPE (exp),
3802 lhs != 0 ? lhs
3803 : rhs != 0 ? rhs : integer_zero_node,
ebde8a27
RK
3804 in_p, low, high))))
3805 return or_op ? invert_truthvalue (tem) : tem;
3806
3807 /* On machines where the branch cost is expensive, if this is a
3808 short-circuited branch and the underlying object on both sides
3809 is the same, make a non-short-circuit operation. */
3810 else if (BRANCH_COST >= 2
7cf5c9e1 3811 && lhs != 0 && rhs != 0
ebde8a27
RK
3812 && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3813 || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
3814 && operand_equal_p (lhs, rhs, 0))
ef659ec0 3815 {
f0eebf28 3816 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
9ec36da5
JL
3817 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
3818 which cases we can't do this. */
ebde8a27
RK
3819 if (simple_operand_p (lhs))
3820 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3821 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3822 TREE_TYPE (exp), TREE_OPERAND (exp, 0),
3823 TREE_OPERAND (exp, 1));
f0eebf28 3824
c5c76735 3825 else if (global_bindings_p () == 0
9ec36da5 3826 && ! contains_placeholder_p (lhs))
ebde8a27
RK
3827 {
3828 tree common = save_expr (lhs);
3829
3830 if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
3831 or_op ? ! in0_p : in0_p,
3832 low0, high0))
3833 && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
3834 or_op ? ! in1_p : in1_p,
3835 low1, high1))))
3836 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3837 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3838 TREE_TYPE (exp), lhs, rhs);
3839 }
ef659ec0 3840 }
de153e82 3841
de153e82 3842 return 0;
ef659ec0
TW
3843}
3844\f
02103577 3845/* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
25216284 3846 bit value. Arrange things so the extra bits will be set to zero if and
d4453ee5
RK
3847 only if C is signed-extended to its full width. If MASK is nonzero,
3848 it is an INTEGER_CST that should be AND'ed with the extra bits. */
02103577
RK
3849
3850static tree
d4453ee5 3851unextend (c, p, unsignedp, mask)
02103577
RK
3852 tree c;
3853 int p;
3854 int unsignedp;
d4453ee5 3855 tree mask;
02103577
RK
3856{
3857 tree type = TREE_TYPE (c);
3858 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
3859 tree temp;
3860
3861 if (p == modesize || unsignedp)
3862 return c;
3863
02103577 3864 /* We work by getting just the sign bit into the low-order bit, then
9faa82d8 3865 into the high-order bit, then sign-extend. We then XOR that value
02103577
RK
3866 with C. */
3867 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
3868 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
cf85c69b
JW
3869
3870 /* We must use a signed type in order to get an arithmetic right shift.
3871 However, we must also avoid introducing accidental overflows, so that
b6cc0a72 3872 a subsequent call to integer_zerop will work. Hence we must
cf85c69b
JW
3873 do the type conversion here. At this point, the constant is either
3874 zero or one, and the conversion to a signed type can never overflow.
3875 We could get an overflow if this conversion is done anywhere else. */
3876 if (TREE_UNSIGNED (type))
3877 temp = convert (signed_type (type), temp);
3878
02103577
RK
3879 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
3880 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
d4453ee5
RK
3881 if (mask != 0)
3882 temp = const_binop (BIT_AND_EXPR, temp, convert (TREE_TYPE (c), mask), 0);
cf85c69b
JW
3883 /* If necessary, convert the type back to match the type of C. */
3884 if (TREE_UNSIGNED (type))
3885 temp = convert (type, temp);
d4453ee5 3886
02103577
RK
3887 return convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
3888}
3889\f
b2215d83
TW
3890/* Find ways of folding logical expressions of LHS and RHS:
3891 Try to merge two comparisons to the same innermost item.
3892 Look for range tests like "ch >= '0' && ch <= '9'".
3893 Look for combinations of simple terms on machines with expensive branches
3894 and evaluate the RHS unconditionally.
6d716ca8
RS
3895
3896 For example, if we have p->a == 2 && p->b == 4 and we can make an
3897 object large enough to span both A and B, we can do this with a comparison
3898 against the object ANDed with the a mask.
3899
3900 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
3901 operations to do this with one comparison.
3902
3903 We check for both normal comparisons and the BIT_AND_EXPRs made this by
3904 function and the one above.
3905
3906 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
3907 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
3908
3909 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
3910 two operands.
3911
3912 We return the simplified tree or 0 if no optimization is possible. */
3913
3914static tree
b2215d83 3915fold_truthop (code, truth_type, lhs, rhs)
6d716ca8
RS
3916 enum tree_code code;
3917 tree truth_type, lhs, rhs;
3918{
f42ef510 3919 /* If this is the "or" of two comparisons, we can do something if
6d716ca8 3920 the comparisons are NE_EXPR. If this is the "and", we can do something
b6cc0a72 3921 if the comparisons are EQ_EXPR. I.e.,
6d716ca8
RS
3922 (a->b == 2 && a->c == 4) can become (a->new == NEW).
3923
3924 WANTED_CODE is this operation code. For single bit fields, we can
3925 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
3926 comparison for one-bit fields. */
3927
b2215d83 3928 enum tree_code wanted_code;
6d716ca8 3929 enum tree_code lcode, rcode;
b2215d83 3930 tree ll_arg, lr_arg, rl_arg, rr_arg;
6d716ca8 3931 tree ll_inner, lr_inner, rl_inner, rr_inner;
770ae6cc
RK
3932 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
3933 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
3934 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
3935 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
6d716ca8
RS
3936 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
3937 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
3938 enum machine_mode lnmode, rnmode;
3939 tree ll_mask, lr_mask, rl_mask, rr_mask;
d4453ee5 3940 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
b2215d83 3941 tree l_const, r_const;
bd910dcf 3942 tree lntype, rntype, result;
6d716ca8 3943 int first_bit, end_bit;
b2215d83 3944 int volatilep;
6d716ca8 3945
ebde8a27
RK
3946 /* Start by getting the comparison codes. Fail if anything is volatile.
3947 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
3948 it were surrounded with a NE_EXPR. */
6d716ca8 3949
ebde8a27 3950 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
b2215d83
TW
3951 return 0;
3952
6d716ca8
RS
3953 lcode = TREE_CODE (lhs);
3954 rcode = TREE_CODE (rhs);
ef659ec0 3955
96d4cf0a
RK
3956 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
3957 lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
3958
3959 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
3960 rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
3961
ebde8a27 3962 if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
ef659ec0
TW
3963 return 0;
3964
b2215d83 3965 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
9c0ae98b 3966 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
b2215d83
TW
3967
3968 ll_arg = TREE_OPERAND (lhs, 0);
3969 lr_arg = TREE_OPERAND (lhs, 1);
3970 rl_arg = TREE_OPERAND (rhs, 0);
3971 rr_arg = TREE_OPERAND (rhs, 1);
b6cc0a72 3972
8227896c 3973 /* If the RHS can be evaluated unconditionally and its operands are
b2215d83
TW
3974 simple, it wins to evaluate the RHS unconditionally on machines
3975 with expensive branches. In this case, this isn't a comparison
1d691c53
RK
3976 that can be merged. Avoid doing this if the RHS is a floating-point
3977 comparison since those can trap. */
b2215d83
TW
3978
3979 if (BRANCH_COST >= 2
1d691c53 3980 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
b2215d83 3981 && simple_operand_p (rl_arg)
8227896c 3982 && simple_operand_p (rr_arg))
b2215d83
TW
3983 return build (code, truth_type, lhs, rhs);
3984
ef659ec0
TW
3985 /* See if the comparisons can be merged. Then get all the parameters for
3986 each side. */
3987
6d716ca8 3988 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
ef659ec0 3989 || (rcode != EQ_EXPR && rcode != NE_EXPR))
6d716ca8
RS
3990 return 0;
3991
b2215d83
TW
3992 volatilep = 0;
3993 ll_inner = decode_field_reference (ll_arg,
6d716ca8 3994 &ll_bitsize, &ll_bitpos, &ll_mode,
d4453ee5
RK
3995 &ll_unsignedp, &volatilep, &ll_mask,
3996 &ll_and_mask);
b2215d83 3997 lr_inner = decode_field_reference (lr_arg,
6d716ca8 3998 &lr_bitsize, &lr_bitpos, &lr_mode,
d4453ee5
RK
3999 &lr_unsignedp, &volatilep, &lr_mask,
4000 &lr_and_mask);
b2215d83 4001 rl_inner = decode_field_reference (rl_arg,
6d716ca8 4002 &rl_bitsize, &rl_bitpos, &rl_mode,
d4453ee5
RK
4003 &rl_unsignedp, &volatilep, &rl_mask,
4004 &rl_and_mask);
b2215d83 4005 rr_inner = decode_field_reference (rr_arg,
6d716ca8 4006 &rr_bitsize, &rr_bitpos, &rr_mode,
d4453ee5
RK
4007 &rr_unsignedp, &volatilep, &rr_mask,
4008 &rr_and_mask);
6d716ca8
RS
4009
4010 /* It must be true that the inner operation on the lhs of each
4011 comparison must be the same if we are to be able to do anything.
4012 Then see if we have constants. If not, the same must be true for
4013 the rhs's. */
4014 if (volatilep || ll_inner == 0 || rl_inner == 0
4015 || ! operand_equal_p (ll_inner, rl_inner, 0))
4016 return 0;
4017
b2215d83
TW
4018 if (TREE_CODE (lr_arg) == INTEGER_CST
4019 && TREE_CODE (rr_arg) == INTEGER_CST)
4020 l_const = lr_arg, r_const = rr_arg;
6d716ca8
RS
4021 else if (lr_inner == 0 || rr_inner == 0
4022 || ! operand_equal_p (lr_inner, rr_inner, 0))
4023 return 0;
b2215d83
TW
4024 else
4025 l_const = r_const = 0;
6d716ca8
RS
4026
4027 /* If either comparison code is not correct for our logical operation,
4028 fail. However, we can convert a one-bit comparison against zero into
4029 the opposite comparison against that bit being set in the field. */
b2215d83 4030
9c0ae98b 4031 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
6d716ca8
RS
4032 if (lcode != wanted_code)
4033 {
4034 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5a6b3365 4035 {
2bd21a02
AS
4036 /* Make the left operand unsigned, since we are only interested
4037 in the value of one bit. Otherwise we are doing the wrong
4038 thing below. */
4039 ll_unsignedp = 1;
71a874cd 4040 l_const = ll_mask;
5a6b3365 4041 }
6d716ca8
RS
4042 else
4043 return 0;
4044 }
4045
71a874cd 4046 /* This is analogous to the code for l_const above. */
6d716ca8
RS
4047 if (rcode != wanted_code)
4048 {
4049 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5a6b3365 4050 {
2bd21a02 4051 rl_unsignedp = 1;
71a874cd 4052 r_const = rl_mask;
5a6b3365 4053 }
6d716ca8
RS
4054 else
4055 return 0;
4056 }
4057
4058 /* See if we can find a mode that contains both fields being compared on
4059 the left. If we can't, fail. Otherwise, update all constants and masks
4060 to be relative to a field of that size. */
4061 first_bit = MIN (ll_bitpos, rl_bitpos);
4062 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4063 lnmode = get_best_mode (end_bit - first_bit, first_bit,
4064 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4065 volatilep);
4066 if (lnmode == VOIDmode)
4067 return 0;
4068
4069 lnbitsize = GET_MODE_BITSIZE (lnmode);
4070 lnbitpos = first_bit & ~ (lnbitsize - 1);
bd910dcf 4071 lntype = type_for_size (lnbitsize, 1);
6d716ca8
RS
4072 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
4073
f76b9db2
ILT
4074 if (BYTES_BIG_ENDIAN)
4075 {
4076 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
4077 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
4078 }
6d716ca8 4079
bd910dcf 4080 ll_mask = const_binop (LSHIFT_EXPR, convert (lntype, ll_mask),
91d33e36 4081 size_int (xll_bitpos), 0);
bd910dcf 4082 rl_mask = const_binop (LSHIFT_EXPR, convert (lntype, rl_mask),
91d33e36 4083 size_int (xrl_bitpos), 0);
6d716ca8 4084
6d716ca8
RS
4085 if (l_const)
4086 {
bd910dcf 4087 l_const = convert (lntype, l_const);
b6cc0a72 4088 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
02103577
RK
4089 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
4090 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
4091 fold (build1 (BIT_NOT_EXPR,
bd910dcf 4092 lntype, ll_mask)),
02103577
RK
4093 0)))
4094 {
ab87f8c8 4095 warning ("comparison is always %d", wanted_code == NE_EXPR);
b6cc0a72 4096
02103577
RK
4097 return convert (truth_type,
4098 wanted_code == NE_EXPR
4099 ? integer_one_node : integer_zero_node);
4100 }
6d716ca8
RS
4101 }
4102 if (r_const)
4103 {
bd910dcf 4104 r_const = convert (lntype, r_const);
d4453ee5 4105 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
02103577
RK
4106 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
4107 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4108 fold (build1 (BIT_NOT_EXPR,
bd910dcf 4109 lntype, rl_mask)),
02103577
RK
4110 0)))
4111 {
ab87f8c8
JL
4112 warning ("comparison is always %d", wanted_code == NE_EXPR);
4113
02103577
RK
4114 return convert (truth_type,
4115 wanted_code == NE_EXPR
4116 ? integer_one_node : integer_zero_node);
4117 }
6d716ca8
RS
4118 }
4119
4120 /* If the right sides are not constant, do the same for it. Also,
4121 disallow this optimization if a size or signedness mismatch occurs
4122 between the left and right sides. */
4123 if (l_const == 0)
4124 {
4125 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
e6a28f26
RS
4126 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4127 /* Make sure the two fields on the right
4128 correspond to the left without being swapped. */
4129 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
6d716ca8
RS
4130 return 0;
4131
4132 first_bit = MIN (lr_bitpos, rr_bitpos);
4133 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4134 rnmode = get_best_mode (end_bit - first_bit, first_bit,
4135 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4136 volatilep);
4137 if (rnmode == VOIDmode)
4138 return 0;
4139
4140 rnbitsize = GET_MODE_BITSIZE (rnmode);
4141 rnbitpos = first_bit & ~ (rnbitsize - 1);
bd910dcf 4142 rntype = type_for_size (rnbitsize, 1);
6d716ca8
RS
4143 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4144
f76b9db2
ILT
4145 if (BYTES_BIG_ENDIAN)
4146 {
4147 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4148 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4149 }
6d716ca8 4150
bd910dcf 4151 lr_mask = const_binop (LSHIFT_EXPR, convert (rntype, lr_mask),
91d33e36 4152 size_int (xlr_bitpos), 0);
bd910dcf 4153 rr_mask = const_binop (LSHIFT_EXPR, convert (rntype, rr_mask),
91d33e36 4154 size_int (xrr_bitpos), 0);
6d716ca8
RS
4155
4156 /* Make a mask that corresponds to both fields being compared.
11a86c56
CH
4157 Do this for both items being compared. If the operands are the
4158 same size and the bits being compared are in the same position
4159 then we can do this by masking both and comparing the masked
4160 results. */
91d33e36
RS
4161 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4162 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
11a86c56 4163 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
6d716ca8 4164 {
bd910dcf 4165 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
6d716ca8 4166 ll_unsignedp || rl_unsignedp);
11a86c56
CH
4167 if (! all_ones_mask_p (ll_mask, lnbitsize))
4168 lhs = build (BIT_AND_EXPR, lntype, lhs, ll_mask);
4169
bd910dcf 4170 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
6d716ca8 4171 lr_unsignedp || rr_unsignedp);
11a86c56
CH
4172 if (! all_ones_mask_p (lr_mask, rnbitsize))
4173 rhs = build (BIT_AND_EXPR, rntype, rhs, lr_mask);
4174
6d716ca8
RS
4175 return build (wanted_code, truth_type, lhs, rhs);
4176 }
4177
4178 /* There is still another way we can do something: If both pairs of
4179 fields being compared are adjacent, we may be able to make a wider
97ea7176
CH
4180 field containing them both.
4181
4182 Note that we still must mask the lhs/rhs expressions. Furthermore,
b6cc0a72 4183 the mask must be shifted to account for the shift done by
97ea7176 4184 make_bit_field_ref. */
6d716ca8
RS
4185 if ((ll_bitsize + ll_bitpos == rl_bitpos
4186 && lr_bitsize + lr_bitpos == rr_bitpos)
4187 || (ll_bitpos == rl_bitpos + rl_bitsize
4188 && lr_bitpos == rr_bitpos + rr_bitsize))
97ea7176 4189 {
bd910dcf
CH
4190 tree type;
4191
4192 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
97ea7176 4193 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
bd910dcf
CH
4194 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4195 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4196
97ea7176
CH
4197 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4198 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
bd910dcf
CH
4199 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4200 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4201
4202 /* Convert to the smaller type before masking out unwanted bits. */
4203 type = lntype;
4204 if (lntype != rntype)
4205 {
4206 if (lnbitsize > rnbitsize)
4207 {
4208 lhs = convert (rntype, lhs);
4209 ll_mask = convert (rntype, ll_mask);
4210 type = rntype;
4211 }
4212 else if (lnbitsize < rnbitsize)
4213 {
4214 rhs = convert (lntype, rhs);
4215 lr_mask = convert (lntype, lr_mask);
4216 type = lntype;
4217 }
4218 }
4219
97ea7176
CH
4220 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
4221 lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
4222
97ea7176
CH
4223 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
4224 rhs = build (BIT_AND_EXPR, type, rhs, lr_mask);
4225
4226 return build (wanted_code, truth_type, lhs, rhs);
4227 }
6d716ca8
RS
4228
4229 return 0;
4230 }
4231
4232 /* Handle the case of comparisons with constants. If there is something in
4233 common between the masks, those bits of the constants must be the same.
4234 If not, the condition is always false. Test for this to avoid generating
4235 incorrect code below. */
91d33e36 4236 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
6d716ca8 4237 if (! integer_zerop (result)
91d33e36
RS
4238 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
4239 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
6d716ca8
RS
4240 {
4241 if (wanted_code == NE_EXPR)
4242 {
4243 warning ("`or' of unmatched not-equal tests is always 1");
4244 return convert (truth_type, integer_one_node);
4245 }
4246 else
4247 {
ab87f8c8 4248 warning ("`and' of mutually exclusive equal-tests is always 0");
6d716ca8
RS
4249 return convert (truth_type, integer_zero_node);
4250 }
4251 }
4252
4253 /* Construct the expression we will return. First get the component
4254 reference we will make. Unless the mask is all ones the width of
4255 that field, perform the mask operation. Then compare with the
4256 merged constant. */
bd910dcf 4257 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
6d716ca8
RS
4258 ll_unsignedp || rl_unsignedp);
4259
91d33e36 4260 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
6d716ca8 4261 if (! all_ones_mask_p (ll_mask, lnbitsize))
bd910dcf 4262 result = build (BIT_AND_EXPR, lntype, result, ll_mask);
6d716ca8
RS
4263
4264 return build (wanted_code, truth_type, result,
91d33e36 4265 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
6d716ca8
RS
4266}
4267\f
b6cc0a72 4268/* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
14a774a9
RK
4269 constant. */
4270
4271static tree
4272optimize_minmax_comparison (t)
4273 tree t;
4274{
4275 tree type = TREE_TYPE (t);
4276 tree arg0 = TREE_OPERAND (t, 0);
4277 enum tree_code op_code;
4278 tree comp_const = TREE_OPERAND (t, 1);
4279 tree minmax_const;
4280 int consts_equal, consts_lt;
4281 tree inner;
4282
4283 STRIP_SIGN_NOPS (arg0);
4284
4285 op_code = TREE_CODE (arg0);
4286 minmax_const = TREE_OPERAND (arg0, 1);
4287 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
4288 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4289 inner = TREE_OPERAND (arg0, 0);
4290
4291 /* If something does not permit us to optimize, return the original tree. */
4292 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4293 || TREE_CODE (comp_const) != INTEGER_CST
4294 || TREE_CONSTANT_OVERFLOW (comp_const)
4295 || TREE_CODE (minmax_const) != INTEGER_CST
4296 || TREE_CONSTANT_OVERFLOW (minmax_const))
4297 return t;
4298
4299 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4300 and GT_EXPR, doing the rest with recursive calls using logical
4301 simplifications. */
4302 switch (TREE_CODE (t))
4303 {
4304 case NE_EXPR: case LT_EXPR: case LE_EXPR:
4305 return
4306 invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t)));
4307
4308 case GE_EXPR:
4309 return
4310 fold (build (TRUTH_ORIF_EXPR, type,
4311 optimize_minmax_comparison
4312 (build (EQ_EXPR, type, arg0, comp_const)),
4313 optimize_minmax_comparison
4314 (build (GT_EXPR, type, arg0, comp_const))));
4315
4316 case EQ_EXPR:
4317 if (op_code == MAX_EXPR && consts_equal)
4318 /* MAX (X, 0) == 0 -> X <= 0 */
4319 return fold (build (LE_EXPR, type, inner, comp_const));
4320
4321 else if (op_code == MAX_EXPR && consts_lt)
4322 /* MAX (X, 0) == 5 -> X == 5 */
4323 return fold (build (EQ_EXPR, type, inner, comp_const));
4324
4325 else if (op_code == MAX_EXPR)
4326 /* MAX (X, 0) == -1 -> false */
4327 return omit_one_operand (type, integer_zero_node, inner);
4328
4329 else if (consts_equal)
4330 /* MIN (X, 0) == 0 -> X >= 0 */
4331 return fold (build (GE_EXPR, type, inner, comp_const));
4332
4333 else if (consts_lt)
4334 /* MIN (X, 0) == 5 -> false */
4335 return omit_one_operand (type, integer_zero_node, inner);
4336
4337 else
4338 /* MIN (X, 0) == -1 -> X == -1 */
4339 return fold (build (EQ_EXPR, type, inner, comp_const));
4340
4341 case GT_EXPR:
4342 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
4343 /* MAX (X, 0) > 0 -> X > 0
4344 MAX (X, 0) > 5 -> X > 5 */
4345 return fold (build (GT_EXPR, type, inner, comp_const));
4346
4347 else if (op_code == MAX_EXPR)
4348 /* MAX (X, 0) > -1 -> true */
4349 return omit_one_operand (type, integer_one_node, inner);
4350
4351 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
4352 /* MIN (X, 0) > 0 -> false
4353 MIN (X, 0) > 5 -> false */
4354 return omit_one_operand (type, integer_zero_node, inner);
4355
4356 else
4357 /* MIN (X, 0) > -1 -> X > -1 */
4358 return fold (build (GT_EXPR, type, inner, comp_const));
4359
4360 default:
4361 return t;
4362 }
4363}
4364\f
1baa375f
RK
4365/* T is an integer expression that is being multiplied, divided, or taken a
4366 modulus (CODE says which and what kind of divide or modulus) by a
4367 constant C. See if we can eliminate that operation by folding it with
4368 other operations already in T. WIDE_TYPE, if non-null, is a type that
4369 should be used for the computation if wider than our type.
4370
4371 For example, if we are dividing (X * 8) + (Y + 16) by 4, we can return
8e1ca098
RH
4372 (X * 2) + (Y + 4). We must, however, be assured that either the original
4373 expression would not overflow or that overflow is undefined for the type
4374 in the language in question.
4375
4376 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
4377 the machine has a multiply-accumulate insn or that this is part of an
4378 addressing calculation.
1baa375f
RK
4379
4380 If we return a non-null expression, it is an equivalent form of the
4381 original computation, but need not be in the original type. */
4382
4383static tree
4384extract_muldiv (t, c, code, wide_type)
4385 tree t;
4386 tree c;
4387 enum tree_code code;
4388 tree wide_type;
4389{
4390 tree type = TREE_TYPE (t);
4391 enum tree_code tcode = TREE_CODE (t);
b6cc0a72 4392 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
1baa375f
RK
4393 > GET_MODE_SIZE (TYPE_MODE (type)))
4394 ? wide_type : type);
4395 tree t1, t2;
4396 int same_p = tcode == code;
9d0878fd 4397 tree op0 = NULL_TREE, op1 = NULL_TREE;
1baa375f
RK
4398
4399 /* Don't deal with constants of zero here; they confuse the code below. */
4400 if (integer_zerop (c))
8e1ca098 4401 return NULL_TREE;
1baa375f
RK
4402
4403 if (TREE_CODE_CLASS (tcode) == '1')
4404 op0 = TREE_OPERAND (t, 0);
4405
4406 if (TREE_CODE_CLASS (tcode) == '2')
4407 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
4408
4409 /* Note that we need not handle conditional operations here since fold
4410 already handles those cases. So just do arithmetic here. */
4411 switch (tcode)
4412 {
4413 case INTEGER_CST:
4414 /* For a constant, we can always simplify if we are a multiply
4415 or (for divide and modulus) if it is a multiple of our constant. */
4416 if (code == MULT_EXPR
4417 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
4418 return const_binop (code, convert (ctype, t), convert (ctype, c), 0);
4419 break;
4420
4421 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
eff9c80d
RH
4422 /* If op0 is an expression, and is unsigned, and the type is
4423 smaller than ctype, then we cannot widen the expression. */
4424 if ((TREE_CODE_CLASS (TREE_CODE (op0)) == '<'
4425 || TREE_CODE_CLASS (TREE_CODE (op0)) == '1'
4426 || TREE_CODE_CLASS (TREE_CODE (op0)) == '2'
4427 || TREE_CODE_CLASS (TREE_CODE (op0)) == 'e')
4428 && TREE_UNSIGNED (TREE_TYPE (op0))
7393c642
RK
4429 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
4430 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
eff9c80d
RH
4431 && (GET_MODE_SIZE (TYPE_MODE (ctype))
4432 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
4433 break;
4434
1baa375f 4435 /* Pass the constant down and see if we can make a simplification. If
59adecfa
RK
4436 we can, replace this expression with the inner simplification for
4437 possible later conversion to our or some other type. */
1baa375f
RK
4438 if (0 != (t1 = extract_muldiv (op0, convert (TREE_TYPE (op0), c), code,
4439 code == MULT_EXPR ? ctype : NULL_TREE)))
4440 return t1;
4441 break;
4442
4443 case NEGATE_EXPR: case ABS_EXPR:
4444 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4445 return fold (build1 (tcode, ctype, convert (ctype, t1)));
4446 break;
4447
4448 case MIN_EXPR: case MAX_EXPR:
13393c8a
JW
4449 /* If widening the type changes the signedness, then we can't perform
4450 this optimization as that changes the result. */
eff9c80d 4451 if (TREE_UNSIGNED (ctype) != TREE_UNSIGNED (type))
13393c8a
JW
4452 break;
4453
1baa375f
RK
4454 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
4455 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
4456 && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
59adecfa
RK
4457 {
4458 if (tree_int_cst_sgn (c) < 0)
4459 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
4460
4461 return fold (build (tcode, ctype, convert (ctype, t1),
4462 convert (ctype, t2)));
4463 }
1baa375f
RK
4464 break;
4465
4466 case WITH_RECORD_EXPR:
4467 if ((t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code, wide_type)) != 0)
4468 return build (WITH_RECORD_EXPR, TREE_TYPE (t1), t1,
4469 TREE_OPERAND (t, 1));
4470 break;
4471
4472 case SAVE_EXPR:
59adecfa
RK
4473 /* If this has not been evaluated and the operand has no side effects,
4474 we can see if we can do something inside it and make a new one.
4475 Note that this test is overly conservative since we can do this
4476 if the only reason it had side effects is that it was another
4477 similar SAVE_EXPR, but that isn't worth bothering with. */
4478 if (SAVE_EXPR_RTL (t) == 0 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0))
1baa375f
RK
4479 && 0 != (t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code,
4480 wide_type)))
fe375cf1
JJ
4481 {
4482 t1 = save_expr (t1);
4483 if (SAVE_EXPR_PERSISTENT_P (t) && TREE_CODE (t1) == SAVE_EXPR)
4484 SAVE_EXPR_PERSISTENT_P (t1) = 1;
4485 if (is_pending_size (t))
4486 put_pending_size (t1);
4487 return t1;
4488 }
1baa375f
RK
4489 break;
4490
4491 case LSHIFT_EXPR: case RSHIFT_EXPR:
4492 /* If the second operand is constant, this is a multiplication
4493 or floor division, by a power of two, so we can treat it that
4494 way unless the multiplier or divisor overflows. */
4495 if (TREE_CODE (op1) == INTEGER_CST
d08230fe
NC
4496 /* const_binop may not detect overflow correctly,
4497 so check for it explicitly here. */
4498 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
4499 && TREE_INT_CST_HIGH (op1) == 0
1baa375f
RK
4500 && 0 != (t1 = convert (ctype,
4501 const_binop (LSHIFT_EXPR, size_one_node,
4502 op1, 0)))
4503 && ! TREE_OVERFLOW (t1))
4504 return extract_muldiv (build (tcode == LSHIFT_EXPR
4505 ? MULT_EXPR : FLOOR_DIV_EXPR,
4506 ctype, convert (ctype, op0), t1),
4507 c, code, wide_type);
4508 break;
4509
4510 case PLUS_EXPR: case MINUS_EXPR:
4511 /* See if we can eliminate the operation on both sides. If we can, we
4512 can return a new PLUS or MINUS. If we can't, the only remaining
4513 cases where we can do anything are if the second operand is a
4514 constant. */
4515 t1 = extract_muldiv (op0, c, code, wide_type);
4516 t2 = extract_muldiv (op1, c, code, wide_type);
fba2c0cd
JJ
4517 if (t1 != 0 && t2 != 0
4518 && (code == MULT_EXPR
4519 /* If not multiplication, we can only do this if either operand
4520 is divisible by c. */
4521 || multiple_of_p (ctype, op0, c)
4522 || multiple_of_p (ctype, op1, c)))
1baa375f
RK
4523 return fold (build (tcode, ctype, convert (ctype, t1),
4524 convert (ctype, t2)));
1baa375f 4525
59adecfa
RK
4526 /* If this was a subtraction, negate OP1 and set it to be an addition.
4527 This simplifies the logic below. */
4528 if (tcode == MINUS_EXPR)
4529 tcode = PLUS_EXPR, op1 = negate_expr (op1);
4530
f9011d04
RK
4531 if (TREE_CODE (op1) != INTEGER_CST)
4532 break;
4533
59adecfa
RK
4534 /* If either OP1 or C are negative, this optimization is not safe for
4535 some of the division and remainder types while for others we need
4536 to change the code. */
4537 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
4538 {
4539 if (code == CEIL_DIV_EXPR)
4540 code = FLOOR_DIV_EXPR;
4541 else if (code == CEIL_MOD_EXPR)
4542 code = FLOOR_MOD_EXPR;
4543 else if (code == FLOOR_DIV_EXPR)
4544 code = CEIL_DIV_EXPR;
4545 else if (code == FLOOR_MOD_EXPR)
4546 code = CEIL_MOD_EXPR;
4547 else if (code != MULT_EXPR)
4548 break;
4549 }
4550
12644a9a
TM
4551 /* If it's a multiply or a division/modulus operation of a multiple
4552 of our constant, do the operation and verify it doesn't overflow. */
4553 if (code == MULT_EXPR
4554 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4555 {
4556 op1 = const_binop (code, convert (ctype, op1), convert (ctype, c), 0);
4557 if (op1 == 0 || TREE_OVERFLOW (op1))
4558 break;
4559 }
4560 else
4561 break;
59adecfa 4562
23cdce68
RH
4563 /* If we have an unsigned type is not a sizetype, we cannot widen
4564 the operation since it will change the result if the original
4565 computation overflowed. */
4566 if (TREE_UNSIGNED (ctype)
7393c642 4567 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
23cdce68
RH
4568 && ctype != type)
4569 break;
4570
1baa375f 4571 /* If we were able to eliminate our operation from the first side,
59adecfa
RK
4572 apply our operation to the second side and reform the PLUS. */
4573 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
4574 return fold (build (tcode, ctype, convert (ctype, t1), op1));
1baa375f
RK
4575
4576 /* The last case is if we are a multiply. In that case, we can
4577 apply the distributive law to commute the multiply and addition
30f7a378 4578 if the multiplication of the constants doesn't overflow. */
59adecfa 4579 if (code == MULT_EXPR)
1baa375f
RK
4580 return fold (build (tcode, ctype, fold (build (code, ctype,
4581 convert (ctype, op0),
4582 convert (ctype, c))),
59adecfa 4583 op1));
1baa375f
RK
4584
4585 break;
4586
4587 case MULT_EXPR:
4588 /* We have a special case here if we are doing something like
4589 (C * 8) % 4 since we know that's zero. */
4590 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
4591 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
4592 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
4593 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4594 return omit_one_operand (type, integer_zero_node, op0);
4595
30f7a378 4596 /* ... fall through ... */
1baa375f
RK
4597
4598 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
4599 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
4600 /* If we can extract our operation from the LHS, do so and return a
4601 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
4602 do something only if the second operand is a constant. */
4603 if (same_p
4604 && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4605 return fold (build (tcode, ctype, convert (ctype, t1),
4606 convert (ctype, op1)));
4607 else if (tcode == MULT_EXPR && code == MULT_EXPR
4608 && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
4609 return fold (build (tcode, ctype, convert (ctype, op0),
4610 convert (ctype, t1)));
4611 else if (TREE_CODE (op1) != INTEGER_CST)
4612 return 0;
4613
4614 /* If these are the same operation types, we can associate them
4615 assuming no overflow. */
4616 if (tcode == code
4617 && 0 != (t1 = const_binop (MULT_EXPR, convert (ctype, op1),
4618 convert (ctype, c), 0))
4619 && ! TREE_OVERFLOW (t1))
4620 return fold (build (tcode, ctype, convert (ctype, op0), t1));
4621
4622 /* If these operations "cancel" each other, we have the main
4623 optimizations of this pass, which occur when either constant is a
4624 multiple of the other, in which case we replace this with either an
b6cc0a72 4625 operation or CODE or TCODE.
8e1ca098
RH
4626
4627 If we have an unsigned type that is not a sizetype, we canot do
4628 this since it will change the result if the original computation
4629 overflowed. */
4630 if ((! TREE_UNSIGNED (ctype)
7393c642 4631 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
8e1ca098
RH
4632 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
4633 || (tcode == MULT_EXPR
4634 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
4635 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
1baa375f
RK
4636 {
4637 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4638 return fold (build (tcode, ctype, convert (ctype, op0),
4639 convert (ctype,
4640 const_binop (TRUNC_DIV_EXPR,
4641 op1, c, 0))));
4642 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
4643 return fold (build (code, ctype, convert (ctype, op0),
4644 convert (ctype,
4645 const_binop (TRUNC_DIV_EXPR,
4646 c, op1, 0))));
4647 }
4648 break;
4649
4650 default:
4651 break;
4652 }
4653
4654 return 0;
4655}
4656\f
b5f3b6b6
RK
4657/* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
4658 S, a SAVE_EXPR, return the expression actually being evaluated. Note
4659 that we may sometimes modify the tree. */
4660
4661static tree
4662strip_compound_expr (t, s)
4663 tree t;
4664 tree s;
4665{
b5f3b6b6
RK
4666 enum tree_code code = TREE_CODE (t);
4667
4668 /* See if this is the COMPOUND_EXPR we want to eliminate. */
4669 if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
4670 && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
4671 return TREE_OPERAND (t, 1);
4672
4673 /* See if this is a COND_EXPR or a simple arithmetic operator. We
4674 don't bother handling any other types. */
4675 else if (code == COND_EXPR)
4676 {
4677 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4678 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4679 TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
4680 }
4681 else if (TREE_CODE_CLASS (code) == '1')
4682 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4683 else if (TREE_CODE_CLASS (code) == '<'
4684 || TREE_CODE_CLASS (code) == '2')
4685 {
4686 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4687 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4688 }
4689
4690 return t;
4691}
4692\f
f628873f
MM
4693/* Return a node which has the indicated constant VALUE (either 0 or
4694 1), and is of the indicated TYPE. */
4695
83e0821b 4696static tree
f628873f
MM
4697constant_boolean_node (value, type)
4698 int value;
4699 tree type;
4700{
4701 if (type == integer_type_node)
4702 return value ? integer_one_node : integer_zero_node;
4703 else if (TREE_CODE (type) == BOOLEAN_TYPE)
4704 return truthvalue_conversion (value ? integer_one_node :
b6cc0a72
KH
4705 integer_zero_node);
4706 else
f628873f
MM
4707 {
4708 tree t = build_int_2 (value, 0);
d4b60170 4709
f628873f
MM
4710 TREE_TYPE (t) = type;
4711 return t;
4712 }
4713}
4714
ab87f8c8
JL
4715/* Utility function for the following routine, to see how complex a nesting of
4716 COND_EXPRs can be. EXPR is the expression and LIMIT is a count beyond which
4717 we don't care (to avoid spending too much time on complex expressions.). */
4718
4719static int
4720count_cond (expr, lim)
4721 tree expr;
4722 int lim;
4723{
d6edb99e 4724 int ctrue, cfalse;
ab87f8c8
JL
4725
4726 if (TREE_CODE (expr) != COND_EXPR)
4727 return 0;
4728 else if (lim <= 0)
4729 return 0;
4730
d6edb99e
ZW
4731 ctrue = count_cond (TREE_OPERAND (expr, 1), lim - 1);
4732 cfalse = count_cond (TREE_OPERAND (expr, 2), lim - 1 - ctrue);
4733 return MIN (lim, 1 + ctrue + cfalse);
ab87f8c8 4734}
68626d4f
MM
4735
4736/* Transform `a + (b ? x : y)' into `x ? (a + b) : (a + y)'.
4737 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
4738 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
4739 expression, and ARG to `a'. If COND_FIRST_P is non-zero, then the
4740 COND is the first argument to CODE; otherwise (as in the example
4741 given here), it is the second argument. TYPE is the type of the
4742 original expression. */
4743
4744static tree
4745fold_binary_op_with_conditional_arg (code, type, cond, arg, cond_first_p)
4746 enum tree_code code;
4747 tree type;
4748 tree cond;
4749 tree arg;
4750 int cond_first_p;
4751{
4752 tree test, true_value, false_value;
4753 tree lhs = NULL_TREE;
4754 tree rhs = NULL_TREE;
4755 /* In the end, we'll produce a COND_EXPR. Both arms of the
4756 conditional expression will be binary operations. The left-hand
4757 side of the expression to be executed if the condition is true
4758 will be pointed to by TRUE_LHS. Similarly, the right-hand side
4759 of the expression to be executed if the condition is true will be
4760 pointed to by TRUE_RHS. FALSE_LHS and FALSE_RHS are analagous --
4761 but apply to the expression to be executed if the conditional is
4762 false. */
4763 tree *true_lhs;
4764 tree *true_rhs;
4765 tree *false_lhs;
4766 tree *false_rhs;
4767 /* These are the codes to use for the left-hand side and right-hand
4768 side of the COND_EXPR. Normally, they are the same as CODE. */
4769 enum tree_code lhs_code = code;
4770 enum tree_code rhs_code = code;
4771 /* And these are the types of the expressions. */
4772 tree lhs_type = type;
4773 tree rhs_type = type;
4774
4775 if (cond_first_p)
4776 {
4777 true_rhs = false_rhs = &arg;
4778 true_lhs = &true_value;
4779 false_lhs = &false_value;
4780 }
4781 else
4782 {
4783 true_lhs = false_lhs = &arg;
4784 true_rhs = &true_value;
4785 false_rhs = &false_value;
4786 }
4787
4788 if (TREE_CODE (cond) == COND_EXPR)
4789 {
4790 test = TREE_OPERAND (cond, 0);
4791 true_value = TREE_OPERAND (cond, 1);
4792 false_value = TREE_OPERAND (cond, 2);
4793 /* If this operand throws an expression, then it does not make
4794 sense to try to perform a logical or arithmetic operation
4795 involving it. Instead of building `a + throw 3' for example,
4796 we simply build `a, throw 3'. */
4797 if (VOID_TYPE_P (TREE_TYPE (true_value)))
4798 {
4799 lhs_code = COMPOUND_EXPR;
4800 if (!cond_first_p)
4801 lhs_type = void_type_node;
4802 }
4803 if (VOID_TYPE_P (TREE_TYPE (false_value)))
4804 {
4805 rhs_code = COMPOUND_EXPR;
4806 if (!cond_first_p)
4807 rhs_type = void_type_node;
4808 }
4809 }
4810 else
4811 {
4812 tree testtype = TREE_TYPE (cond);
4813 test = cond;
4814 true_value = convert (testtype, integer_one_node);
4815 false_value = convert (testtype, integer_zero_node);
4816 }
4817
4818 /* If ARG is complex we want to make sure we only evaluate
4819 it once. Though this is only required if it is volatile, it
4820 might be more efficient even if it is not. However, if we
4821 succeed in folding one part to a constant, we do not need
4822 to make this SAVE_EXPR. Since we do this optimization
4823 primarily to see if we do end up with constant and this
4824 SAVE_EXPR interferes with later optimizations, suppressing
4825 it when we can is important.
4826
4827 If we are not in a function, we can't make a SAVE_EXPR, so don't
4828 try to do so. Don't try to see if the result is a constant
4829 if an arm is a COND_EXPR since we get exponential behavior
4830 in that case. */
4831
4832 if (TREE_CODE (arg) != SAVE_EXPR && ! TREE_CONSTANT (arg)
4833 && global_bindings_p () == 0
4834 && ((TREE_CODE (arg) != VAR_DECL
4835 && TREE_CODE (arg) != PARM_DECL)
4836 || TREE_SIDE_EFFECTS (arg)))
4837 {
4838 if (TREE_CODE (true_value) != COND_EXPR)
4839 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4840
4841 if (TREE_CODE (false_value) != COND_EXPR)
4842 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4843
4844 if ((lhs == 0 || ! TREE_CONSTANT (lhs))
4845 && (rhs == 0 || !TREE_CONSTANT (rhs)))
4846 arg = save_expr (arg), lhs = rhs = 0;
4847 }
4848
4849 if (lhs == 0)
4850 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4851 if (rhs == 0)
4852 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4853
4854 test = fold (build (COND_EXPR, type, test, lhs, rhs));
4855
4856 if (TREE_CODE (arg) == SAVE_EXPR)
4857 return build (COMPOUND_EXPR, type,
4858 convert (void_type_node, arg),
4859 strip_compound_expr (test, arg));
4860 else
4861 return convert (type, test);
4862}
4863
ab87f8c8 4864\f
6d716ca8
RS
4865/* Perform constant folding and related simplification of EXPR.
4866 The related simplifications include x*1 => x, x*0 => 0, etc.,
4867 and application of the associative law.
4868 NOP_EXPR conversions may be removed freely (as long as we
4869 are careful not to change the C type of the overall expression)
4870 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
4871 but we can constant-fold them if they have constant operands. */
4872
4873tree
b6cc0a72 4874fold (expr)
6d716ca8
RS
4875 tree expr;
4876{
4877 register tree t = expr;
4878 tree t1 = NULL_TREE;
c05a9b68 4879 tree tem;
6d716ca8 4880 tree type = TREE_TYPE (expr);
4e86caed 4881 register tree arg0 = NULL_TREE, arg1 = NULL_TREE;
6d716ca8 4882 register enum tree_code code = TREE_CODE (t);
1796dff4 4883 register int kind = TREE_CODE_CLASS (code);
c05a9b68 4884 int invert;
6d716ca8
RS
4885 /* WINS will be nonzero when the switch is done
4886 if all operands are constant. */
6d716ca8
RS
4887 int wins = 1;
4888
b6cc0a72 4889 /* Don't try to process an RTL_EXPR since its operands aren't trees.
5fd7f37d 4890 Likewise for a SAVE_EXPR that's already been evaluated. */
dea8be3e 4891 if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t) != 0))
ac27d589
RK
4892 return t;
4893
1796dff4
RH
4894 /* Return right away if a constant. */
4895 if (kind == 'c')
4896 return t;
b6cc0a72 4897
dbecbbe4
JL
4898#ifdef MAX_INTEGER_COMPUTATION_MODE
4899 check_max_integer_computation_mode (expr);
4900#endif
4901
1b81aa14
RS
4902 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
4903 {
1cc1b11a
RS
4904 tree subop;
4905
1b81aa14
RS
4906 /* Special case for conversion ops that can have fixed point args. */
4907 arg0 = TREE_OPERAND (t, 0);
4908
4909 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
4910 if (arg0 != 0)
14a774a9 4911 STRIP_SIGN_NOPS (arg0);
1b81aa14 4912
1cc1b11a
RS
4913 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
4914 subop = TREE_REALPART (arg0);
4915 else
4916 subop = arg0;
4917
4918 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
1b81aa14 4919#if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1cc1b11a 4920 && TREE_CODE (subop) != REAL_CST
1b81aa14
RS
4921#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4922 )
4923 /* Note that TREE_CONSTANT isn't enough:
4924 static var addresses are constant but we can't
4925 do arithmetic on them. */
4926 wins = 0;
4927 }
efd58783 4928 else if (IS_EXPR_CODE_CLASS (kind) || kind == 'r')
6d716ca8 4929 {
dea8be3e 4930 register int len = first_rtl_op (code);
6d716ca8
RS
4931 register int i;
4932 for (i = 0; i < len; i++)
4933 {
4934 tree op = TREE_OPERAND (t, i);
1cc1b11a 4935 tree subop;
6d716ca8
RS
4936
4937 if (op == 0)
4938 continue; /* Valid for CALL_EXPR, at least. */
4939
b8a91430
RK
4940 if (kind == '<' || code == RSHIFT_EXPR)
4941 {
4942 /* Signedness matters here. Perhaps we can refine this
4943 later. */
14a774a9 4944 STRIP_SIGN_NOPS (op);
b8a91430
RK
4945 }
4946 else
13eb1f7f
RK
4947 /* Strip any conversions that don't change the mode. */
4948 STRIP_NOPS (op);
b6cc0a72 4949
1cc1b11a
RS
4950 if (TREE_CODE (op) == COMPLEX_CST)
4951 subop = TREE_REALPART (op);
4952 else
4953 subop = op;
4954
4955 if (TREE_CODE (subop) != INTEGER_CST
6d716ca8 4956#if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1cc1b11a 4957 && TREE_CODE (subop) != REAL_CST
6d716ca8
RS
4958#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4959 )
4960 /* Note that TREE_CONSTANT isn't enough:
4961 static var addresses are constant but we can't
4962 do arithmetic on them. */
4963 wins = 0;
4964
4965 if (i == 0)
4966 arg0 = op;
4967 else if (i == 1)
4968 arg1 = op;
4969 }
4970 }
4971
4972 /* If this is a commutative operation, and ARG0 is a constant, move it
4973 to ARG1 to reduce the number of tests below. */
4974 if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
4975 || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
4976 || code == BIT_AND_EXPR)
4977 && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
4978 {
c05a9b68 4979 tem = arg0; arg0 = arg1; arg1 = tem;
6d716ca8 4980
c05a9b68
RS
4981 tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
4982 TREE_OPERAND (t, 1) = tem;
6d716ca8
RS
4983 }
4984
4985 /* Now WINS is set as described above,
4986 ARG0 is the first operand of EXPR,
4987 and ARG1 is the second operand (if it has more than one operand).
4988
4989 First check for cases where an arithmetic operation is applied to a
4990 compound, conditional, or comparison operation. Push the arithmetic
4991 operation inside the compound or conditional to see if any folding
4992 can then be done. Convert comparison to conditional for this purpose.
4993 The also optimizes non-constant cases that used to be done in
96d4cf0a
RK
4994 expand_expr.
4995
fba5638f 4996 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
61f275ff
RK
4997 one of the operands is a comparison and the other is a comparison, a
4998 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
4999 code below would make the expression more complex. Change it to a
b6cc0a72 5000 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
2df46b06 5001 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
96d4cf0a 5002
2df46b06
RK
5003 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
5004 || code == EQ_EXPR || code == NE_EXPR)
61f275ff
RK
5005 && ((truth_value_p (TREE_CODE (arg0))
5006 && (truth_value_p (TREE_CODE (arg1))
96d4cf0a
RK
5007 || (TREE_CODE (arg1) == BIT_AND_EXPR
5008 && integer_onep (TREE_OPERAND (arg1, 1)))))
61f275ff
RK
5009 || (truth_value_p (TREE_CODE (arg1))
5010 && (truth_value_p (TREE_CODE (arg0))
96d4cf0a
RK
5011 || (TREE_CODE (arg0) == BIT_AND_EXPR
5012 && integer_onep (TREE_OPERAND (arg0, 1)))))))
2df46b06
RK
5013 {
5014 t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
5015 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
5016 : TRUTH_XOR_EXPR,
5017 type, arg0, arg1));
5018
5019 if (code == EQ_EXPR)
5020 t = invert_truthvalue (t);
5021
5022 return t;
5023 }
96d4cf0a 5024
6d716ca8
RS
5025 if (TREE_CODE_CLASS (code) == '1')
5026 {
5027 if (TREE_CODE (arg0) == COMPOUND_EXPR)
5028 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5029 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
5030 else if (TREE_CODE (arg0) == COND_EXPR)
b8eb43a2
RK
5031 {
5032 t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
5033 fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
5034 fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
5035
5036 /* If this was a conversion, and all we did was to move into
e1f56f62
RK
5037 inside the COND_EXPR, bring it back out. But leave it if
5038 it is a conversion from integer to integer and the
5039 result precision is no wider than a word since such a
5040 conversion is cheap and may be optimized away by combine,
5041 while it couldn't if it were outside the COND_EXPR. Then return
5042 so we don't get into an infinite recursion loop taking the
5043 conversion out and then back in. */
b8eb43a2
RK
5044
5045 if ((code == NOP_EXPR || code == CONVERT_EXPR
5046 || code == NON_LVALUE_EXPR)
5047 && TREE_CODE (t) == COND_EXPR
5048 && TREE_CODE (TREE_OPERAND (t, 1)) == code
459a2653
RK
5049 && TREE_CODE (TREE_OPERAND (t, 2)) == code
5050 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
e1f56f62
RK
5051 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0)))
5052 && ! (INTEGRAL_TYPE_P (TREE_TYPE (t))
d4b60170
RK
5053 && (INTEGRAL_TYPE_P
5054 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))))
e1f56f62 5055 && TYPE_PRECISION (TREE_TYPE (t)) <= BITS_PER_WORD))
b8eb43a2
RK
5056 t = build1 (code, type,
5057 build (COND_EXPR,
d4b60170
RK
5058 TREE_TYPE (TREE_OPERAND
5059 (TREE_OPERAND (t, 1), 0)),
b8eb43a2
RK
5060 TREE_OPERAND (t, 0),
5061 TREE_OPERAND (TREE_OPERAND (t, 1), 0),
5062 TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
5063 return t;
5064 }
b6cc0a72 5065 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
6d716ca8
RS
5066 return fold (build (COND_EXPR, type, arg0,
5067 fold (build1 (code, type, integer_one_node)),
5068 fold (build1 (code, type, integer_zero_node))));
5069 }
96d4cf0a
RK
5070 else if (TREE_CODE_CLASS (code) == '2'
5071 || TREE_CODE_CLASS (code) == '<')
6d716ca8
RS
5072 {
5073 if (TREE_CODE (arg1) == COMPOUND_EXPR)
5074 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
96d4cf0a
RK
5075 fold (build (code, type,
5076 arg0, TREE_OPERAND (arg1, 1))));
f0eebf28
RK
5077 else if ((TREE_CODE (arg1) == COND_EXPR
5078 || (TREE_CODE_CLASS (TREE_CODE (arg1)) == '<'
5079 && TREE_CODE_CLASS (code) != '<'))
ab87f8c8
JL
5080 && (TREE_CODE (arg0) != COND_EXPR
5081 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
9ec36da5 5082 && (! TREE_SIDE_EFFECTS (arg0)
c5c76735 5083 || (global_bindings_p () == 0
9ec36da5 5084 && ! contains_placeholder_p (arg0))))
68626d4f
MM
5085 return
5086 fold_binary_op_with_conditional_arg (code, type, arg1, arg0,
5087 /*cond_first_p=*/0);
6d716ca8
RS
5088 else if (TREE_CODE (arg0) == COMPOUND_EXPR)
5089 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5090 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
f0eebf28
RK
5091 else if ((TREE_CODE (arg0) == COND_EXPR
5092 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
5093 && TREE_CODE_CLASS (code) != '<'))
ab87f8c8
JL
5094 && (TREE_CODE (arg1) != COND_EXPR
5095 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
9ec36da5 5096 && (! TREE_SIDE_EFFECTS (arg1)
c5c76735 5097 || (global_bindings_p () == 0
9ec36da5 5098 && ! contains_placeholder_p (arg1))))
68626d4f
MM
5099 return
5100 fold_binary_op_with_conditional_arg (code, type, arg0, arg1,
5101 /*cond_first_p=*/1);
6d716ca8 5102 }
c05a9b68
RS
5103 else if (TREE_CODE_CLASS (code) == '<'
5104 && TREE_CODE (arg0) == COMPOUND_EXPR)
5105 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5106 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5107 else if (TREE_CODE_CLASS (code) == '<'
5108 && TREE_CODE (arg1) == COMPOUND_EXPR)
5109 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5110 fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
b6cc0a72 5111
6d716ca8
RS
5112 switch (code)
5113 {
5114 case INTEGER_CST:
5115 case REAL_CST:
5116 case STRING_CST:
5117 case COMPLEX_CST:
5118 case CONSTRUCTOR:
5119 return t;
5120
5121 case CONST_DECL:
5122 return fold (DECL_INITIAL (t));
5123
5124 case NOP_EXPR:
5125 case FLOAT_EXPR:
5126 case CONVERT_EXPR:
5127 case FIX_TRUNC_EXPR:
5128 /* Other kinds of FIX are not handled properly by fold_convert. */
33558beb 5129
e1f56f62
RK
5130 if (TREE_TYPE (TREE_OPERAND (t, 0)) == TREE_TYPE (t))
5131 return TREE_OPERAND (t, 0);
5132
7e139075
RK
5133 /* Handle cases of two conversions in a row. */
5134 if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
5135 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
5136 {
5137 tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5138 tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
5139 tree final_type = TREE_TYPE (t);
5140 int inside_int = INTEGRAL_TYPE_P (inside_type);
7f0b305c 5141 int inside_ptr = POINTER_TYPE_P (inside_type);
7e139075 5142 int inside_float = FLOAT_TYPE_P (inside_type);
770ae6cc 5143 unsigned int inside_prec = TYPE_PRECISION (inside_type);
7e139075
RK
5144 int inside_unsignedp = TREE_UNSIGNED (inside_type);
5145 int inter_int = INTEGRAL_TYPE_P (inter_type);
7f0b305c 5146 int inter_ptr = POINTER_TYPE_P (inter_type);
7e139075 5147 int inter_float = FLOAT_TYPE_P (inter_type);
770ae6cc 5148 unsigned int inter_prec = TYPE_PRECISION (inter_type);
7e139075
RK
5149 int inter_unsignedp = TREE_UNSIGNED (inter_type);
5150 int final_int = INTEGRAL_TYPE_P (final_type);
7f0b305c 5151 int final_ptr = POINTER_TYPE_P (final_type);
7e139075 5152 int final_float = FLOAT_TYPE_P (final_type);
770ae6cc 5153 unsigned int final_prec = TYPE_PRECISION (final_type);
7e139075
RK
5154 int final_unsignedp = TREE_UNSIGNED (final_type);
5155
b6cc0a72 5156 /* In addition to the cases of two conversions in a row
7e139075
RK
5157 handled below, if we are converting something to its own
5158 type via an object of identical or wider precision, neither
5159 conversion is needed. */
30355eaa 5160 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (final_type)
7e139075
RK
5161 && ((inter_int && final_int) || (inter_float && final_float))
5162 && inter_prec >= final_prec)
72bd1f9e 5163 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
7e139075
RK
5164
5165 /* Likewise, if the intermediate and final types are either both
5166 float or both integer, we don't need the middle conversion if
5167 it is wider than the final type and doesn't change the signedness
7f0b305c 5168 (for integers). Avoid this if the final type is a pointer
410d3f5d
RK
5169 since then we sometimes need the inner conversion. Likewise if
5170 the outer has a precision not equal to the size of its mode. */
7e139075
RK
5171 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
5172 || (inter_float && inside_float))
5173 && inter_prec >= inside_prec
7f0b305c 5174 && (inter_float || inter_unsignedp == inside_unsignedp)
410d3f5d
RK
5175 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5176 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
7f0b305c 5177 && ! final_ptr)
7e139075
RK
5178 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5179
ba3ad5e0
PDM
5180 /* If we have a sign-extension of a zero-extended value, we can
5181 replace that by a single zero-extension. */
5182 if (inside_int && inter_int && final_int
5183 && inside_prec < inter_prec && inter_prec < final_prec
5184 && inside_unsignedp && !inter_unsignedp)
5185 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5186
7e139075
RK
5187 /* Two conversions in a row are not needed unless:
5188 - some conversion is floating-point (overstrict for now), or
5189 - the intermediate type is narrower than both initial and
5190 final, or
5191 - the intermediate type and innermost type differ in signedness,
5192 and the outermost type is wider than the intermediate, or
5193 - the initial type is a pointer type and the precisions of the
5194 intermediate and final types differ, or
b6cc0a72 5195 - the final type is a pointer type and the precisions of the
7e139075
RK
5196 initial and intermediate types differ. */
5197 if (! inside_float && ! inter_float && ! final_float
5198 && (inter_prec > inside_prec || inter_prec > final_prec)
5199 && ! (inside_int && inter_int
5200 && inter_unsignedp != inside_unsignedp
5201 && inter_prec < final_prec)
5202 && ((inter_unsignedp && inter_prec > inside_prec)
5203 == (final_unsignedp && final_prec > inter_prec))
5204 && ! (inside_ptr && inter_prec != final_prec)
410d3f5d
RK
5205 && ! (final_ptr && inside_prec != inter_prec)
5206 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5207 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5208 && ! final_ptr)
7e139075
RK
5209 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5210 }
6d716ca8
RS
5211
5212 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
d8f6dbb9
RS
5213 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
5214 /* Detect assigning a bitfield. */
5215 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
5216 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
6d716ca8 5217 {
d8f6dbb9 5218 /* Don't leave an assignment inside a conversion
f72aed24 5219 unless assigning a bitfield. */
6d716ca8
RS
5220 tree prev = TREE_OPERAND (t, 0);
5221 TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
5222 /* First do the assignment, then return converted constant. */
5223 t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
5224 TREE_USED (t) = 1;
5225 return t;
5226 }
5227 if (!wins)
5228 {
5229 TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
5230 return t;
5231 }
5232 return fold_convert (t, arg0);
5233
5234#if 0 /* This loses on &"foo"[0]. */
5235 case ARRAY_REF:
5236 {
5237 int i;
5238
5239 /* Fold an expression like: "foo"[2] */
5240 if (TREE_CODE (arg0) == STRING_CST
5241 && TREE_CODE (arg1) == INTEGER_CST
05bccae2 5242 && compare_tree_int (arg1, TREE_STRING_LENGTH (arg0)) < 0)
6d716ca8 5243 {
05bccae2 5244 t = build_int_2 (TREE_STRING_POINTER (arg0)[TREE_INT_CST_LOW (arg))], 0);
6d716ca8 5245 TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
ef2bf0c0 5246 force_fit_type (t, 0);
6d716ca8
RS
5247 }
5248 }
5249 return t;
5250#endif /* 0 */
5251
e082358b
JM
5252 case COMPONENT_REF:
5253 if (TREE_CODE (arg0) == CONSTRUCTOR)
3ac4f0e6
JM
5254 {
5255 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
5256 if (m)
5257 t = TREE_VALUE (m);
5258 }
e082358b
JM
5259 return t;
5260
6d716ca8
RS
5261 case RANGE_EXPR:
5262 TREE_CONSTANT (t) = wins;
5263 return t;
5264
5265 case NEGATE_EXPR:
5266 if (wins)
5267 {
5268 if (TREE_CODE (arg0) == INTEGER_CST)
5269 {
f9e158c3
JM
5270 unsigned HOST_WIDE_INT low;
5271 HOST_WIDE_INT high;
fe3e8e40
RS
5272 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5273 TREE_INT_CST_HIGH (arg0),
5274 &low, &high);
5275 t = build_int_2 (low, high);
6d716ca8 5276 TREE_TYPE (t) = type;
dc3907c5
PE
5277 TREE_OVERFLOW (t)
5278 = (TREE_OVERFLOW (arg0)
0c9cabe7 5279 | force_fit_type (t, overflow && !TREE_UNSIGNED (type)));
dc3907c5
PE
5280 TREE_CONSTANT_OVERFLOW (t)
5281 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
6d716ca8
RS
5282 }
5283 else if (TREE_CODE (arg0) == REAL_CST)
5284 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
6d716ca8
RS
5285 }
5286 else if (TREE_CODE (arg0) == NEGATE_EXPR)
5287 return TREE_OPERAND (arg0, 0);
5288
5289 /* Convert - (a - b) to (b - a) for non-floating-point. */
1baa375f 5290 else if (TREE_CODE (arg0) == MINUS_EXPR
de6c5979 5291 && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
6d716ca8
RS
5292 return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
5293 TREE_OPERAND (arg0, 0));
5294
5295 return t;
5296
5297 case ABS_EXPR:
5298 if (wins)
5299 {
5300 if (TREE_CODE (arg0) == INTEGER_CST)
5301 {
68e568c2
MM
5302 /* If the value is unsigned, then the absolute value is
5303 the same as the ordinary value. */
5304 if (TREE_UNSIGNED (type))
5305 return arg0;
5306 /* Similarly, if the value is non-negative. */
5307 else if (INT_CST_LT (integer_minus_one_node, arg0))
5308 return arg0;
5309 /* If the value is negative, then the absolute value is
5310 its negation. */
5311 else
6d716ca8 5312 {
f9e158c3
JM
5313 unsigned HOST_WIDE_INT low;
5314 HOST_WIDE_INT high;
2a23183e
RS
5315 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5316 TREE_INT_CST_HIGH (arg0),
5317 &low, &high);
5318 t = build_int_2 (low, high);
5319 TREE_TYPE (t) = type;
dc3907c5
PE
5320 TREE_OVERFLOW (t)
5321 = (TREE_OVERFLOW (arg0)
e0f776fb 5322 | force_fit_type (t, overflow));
dc3907c5
PE
5323 TREE_CONSTANT_OVERFLOW (t)
5324 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
6d716ca8
RS
5325 }
5326 }
5327 else if (TREE_CODE (arg0) == REAL_CST)
5328 {
c05a9b68 5329 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
6d716ca8
RS
5330 t = build_real (type,
5331 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5332 }
6d716ca8
RS
5333 }
5334 else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
5335 return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
5336 return t;
5337
551064b1
RS
5338 case CONJ_EXPR:
5339 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
13eb1f7f 5340 return convert (type, arg0);
551064b1 5341 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
c9869b75 5342 return build (COMPLEX_EXPR, type,
551064b1 5343 TREE_OPERAND (arg0, 0),
1baa375f 5344 negate_expr (TREE_OPERAND (arg0, 1)));
551064b1 5345 else if (TREE_CODE (arg0) == COMPLEX_CST)
32812a40
JM
5346 return build_complex (type, TREE_REALPART (arg0),
5347 negate_expr (TREE_IMAGPART (arg0)));
551064b1
RS
5348 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
5349 return fold (build (TREE_CODE (arg0), type,
5350 fold (build1 (CONJ_EXPR, type,
5351 TREE_OPERAND (arg0, 0))),
5352 fold (build1 (CONJ_EXPR,
5353 type, TREE_OPERAND (arg0, 1)))));
5354 else if (TREE_CODE (arg0) == CONJ_EXPR)
5355 return TREE_OPERAND (arg0, 0);
5356 return t;
5357
6d716ca8
RS
5358 case BIT_NOT_EXPR:
5359 if (wins)
5360 {
380ff34a
RK
5361 t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
5362 ~ TREE_INT_CST_HIGH (arg0));
6d716ca8 5363 TREE_TYPE (t) = type;
e0f776fb 5364 force_fit_type (t, 0);
dc3907c5 5365 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
fe3e8e40 5366 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
6d716ca8
RS
5367 }
5368 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
5369 return TREE_OPERAND (arg0, 0);
5370 return t;
5371
5372 case PLUS_EXPR:
5373 /* A + (-B) -> A - B */
5374 if (TREE_CODE (arg1) == NEGATE_EXPR)
5375 return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
10414089
JL
5376 /* (-A) + B -> B - A */
5377 if (TREE_CODE (arg0) == NEGATE_EXPR)
5378 return fold (build (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
7178e3af 5379 else if (! FLOAT_TYPE_P (type))
6d716ca8
RS
5380 {
5381 if (integer_zerop (arg1))
5382 return non_lvalue (convert (type, arg0));
5383
5384 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
5385 with a constant, and the two constants have no bits in common,
5386 we should treat this as a BIT_IOR_EXPR since this may produce more
5387 simplifications. */
5388 if (TREE_CODE (arg0) == BIT_AND_EXPR
5389 && TREE_CODE (arg1) == BIT_AND_EXPR
5390 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5391 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5392 && integer_zerop (const_binop (BIT_AND_EXPR,
5393 TREE_OPERAND (arg0, 1),
91d33e36 5394 TREE_OPERAND (arg1, 1), 0)))
6d716ca8
RS
5395 {
5396 code = BIT_IOR_EXPR;
5397 goto bit_ior;
5398 }
6a96fcb4 5399
abe4f192 5400 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
b6cc0a72 5401 (plus (plus (mult) (mult)) (foo)) so that we can
abe4f192
RH
5402 take advantage of the factoring cases below. */
5403 if ((TREE_CODE (arg0) == PLUS_EXPR
5404 && TREE_CODE (arg1) == MULT_EXPR)
5405 || (TREE_CODE (arg1) == PLUS_EXPR
b6cc0a72 5406 && TREE_CODE (arg0) == MULT_EXPR))
abe4f192
RH
5407 {
5408 tree parg0, parg1, parg, marg;
5409
5410 if (TREE_CODE (arg0) == PLUS_EXPR)
5411 parg = arg0, marg = arg1;
5412 else
5413 parg = arg1, marg = arg0;
5414 parg0 = TREE_OPERAND (parg, 0);
5415 parg1 = TREE_OPERAND (parg, 1);
5416 STRIP_NOPS (parg0);
5417 STRIP_NOPS (parg1);
5418
5419 if (TREE_CODE (parg0) == MULT_EXPR
5420 && TREE_CODE (parg1) != MULT_EXPR)
5421 return fold (build (PLUS_EXPR, type,
5422 fold (build (PLUS_EXPR, type, parg0, marg)),
5423 parg1));
5424 if (TREE_CODE (parg0) != MULT_EXPR
5425 && TREE_CODE (parg1) == MULT_EXPR)
5426 return fold (build (PLUS_EXPR, type,
5427 fold (build (PLUS_EXPR, type, parg1, marg)),
5428 parg0));
5429 }
5430
45f97e2e
RH
5431 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
5432 {
5433 tree arg00, arg01, arg10, arg11;
07444f1d 5434 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
45f97e2e
RH
5435
5436 /* (A * C) + (B * C) -> (A+B) * C.
5437 We are most concerned about the case where C is a constant,
5438 but other combinations show up during loop reduction. Since
5439 it is not difficult, try all four possibilities. */
5440
5441 arg00 = TREE_OPERAND (arg0, 0);
5442 arg01 = TREE_OPERAND (arg0, 1);
5443 arg10 = TREE_OPERAND (arg1, 0);
5444 arg11 = TREE_OPERAND (arg1, 1);
5445 same = NULL_TREE;
5446
5447 if (operand_equal_p (arg01, arg11, 0))
5448 same = arg01, alt0 = arg00, alt1 = arg10;
5449 else if (operand_equal_p (arg00, arg10, 0))
5450 same = arg00, alt0 = arg01, alt1 = arg11;
5451 else if (operand_equal_p (arg00, arg11, 0))
5452 same = arg00, alt0 = arg01, alt1 = arg10;
5453 else if (operand_equal_p (arg01, arg10, 0))
5454 same = arg01, alt0 = arg00, alt1 = arg11;
5455
abe4f192
RH
5456 /* No identical multiplicands; see if we can find a common
5457 power-of-two factor in non-power-of-two multiplies. This
5458 can help in multi-dimensional array access. */
5459 else if (TREE_CODE (arg01) == INTEGER_CST
5460 && TREE_CODE (arg11) == INTEGER_CST
5461 && TREE_INT_CST_HIGH (arg01) == 0
5462 && TREE_INT_CST_HIGH (arg11) == 0)
5463 {
5464 HOST_WIDE_INT int01, int11, tmp;
5465 int01 = TREE_INT_CST_LOW (arg01);
5466 int11 = TREE_INT_CST_LOW (arg11);
5467
5468 /* Move min of absolute values to int11. */
5469 if ((int01 >= 0 ? int01 : -int01)
5470 < (int11 >= 0 ? int11 : -int11))
5471 {
5472 tmp = int01, int01 = int11, int11 = tmp;
5473 alt0 = arg00, arg00 = arg10, arg10 = alt0;
5474 alt0 = arg01, arg01 = arg11, arg11 = alt0;
5475 }
5476
5477 if (exact_log2 (int11) > 0 && int01 % int11 == 0)
5478 {
5479 alt0 = fold (build (MULT_EXPR, type, arg00,
5480 build_int_2 (int01 / int11, 0)));
5481 alt1 = arg10;
5482 same = arg11;
5483 }
5484 }
5485
45f97e2e 5486 if (same)
b6cc0a72 5487 return fold (build (MULT_EXPR, type,
45f97e2e
RH
5488 fold (build (PLUS_EXPR, type, alt0, alt1)),
5489 same));
5490 }
6d716ca8
RS
5491 }
5492 /* In IEEE floating point, x+0 may not equal x. */
996c63d3 5493 else if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
de6c5979 5494 || flag_unsafe_math_optimizations)
6d716ca8
RS
5495 && real_zerop (arg1))
5496 return non_lvalue (convert (type, arg0));
10414089 5497 /* x+(-0) equals x, even for IEEE. */
2af3e5fb
AB
5498 else if (TREE_CODE (arg1) == REAL_CST
5499 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1)))
10414089
JL
5500 return non_lvalue (convert (type, arg0));
5501
79e8185c
JH
5502 bit_rotate:
5503 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
5504 is a rotate of A by C1 bits. */
5505 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
5506 is a rotate of A by B bits. */
5507 {
b6cc0a72
KH
5508 register enum tree_code code0, code1;
5509 code0 = TREE_CODE (arg0);
5510 code1 = TREE_CODE (arg1);
5511 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
5512 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
79e8185c 5513 && operand_equal_p (TREE_OPERAND (arg0, 0),
b6cc0a72 5514 TREE_OPERAND (arg1, 0), 0)
79e8185c
JH
5515 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5516 {
5517 register tree tree01, tree11;
5518 register enum tree_code code01, code11;
5519
5520 tree01 = TREE_OPERAND (arg0, 1);
5521 tree11 = TREE_OPERAND (arg1, 1);
5522 STRIP_NOPS (tree01);
5523 STRIP_NOPS (tree11);
5524 code01 = TREE_CODE (tree01);
5525 code11 = TREE_CODE (tree11);
5526 if (code01 == INTEGER_CST
b6cc0a72
KH
5527 && code11 == INTEGER_CST
5528 && TREE_INT_CST_HIGH (tree01) == 0
5529 && TREE_INT_CST_HIGH (tree11) == 0
5530 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
5531 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
79e8185c 5532 return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
b6cc0a72 5533 code0 == LSHIFT_EXPR ? tree01 : tree11);
79e8185c
JH
5534 else if (code11 == MINUS_EXPR)
5535 {
b6cc0a72
KH
5536 tree tree110, tree111;
5537 tree110 = TREE_OPERAND (tree11, 0);
5538 tree111 = TREE_OPERAND (tree11, 1);
5539 STRIP_NOPS (tree110);
5540 STRIP_NOPS (tree111);
5541 if (TREE_CODE (tree110) == INTEGER_CST
05bccae2
RK
5542 && 0 == compare_tree_int (tree110,
5543 TYPE_PRECISION
5544 (TREE_TYPE (TREE_OPERAND
5545 (arg0, 0))))
79e8185c 5546 && operand_equal_p (tree01, tree111, 0))
b6cc0a72
KH
5547 return build ((code0 == LSHIFT_EXPR
5548 ? LROTATE_EXPR
5549 : RROTATE_EXPR),
5550 type, TREE_OPERAND (arg0, 0), tree01);
79e8185c
JH
5551 }
5552 else if (code01 == MINUS_EXPR)
5553 {
b6cc0a72
KH
5554 tree tree010, tree011;
5555 tree010 = TREE_OPERAND (tree01, 0);
5556 tree011 = TREE_OPERAND (tree01, 1);
5557 STRIP_NOPS (tree010);
5558 STRIP_NOPS (tree011);
5559 if (TREE_CODE (tree010) == INTEGER_CST
05bccae2
RK
5560 && 0 == compare_tree_int (tree010,
5561 TYPE_PRECISION
5562 (TREE_TYPE (TREE_OPERAND
5563 (arg0, 0))))
79e8185c 5564 && operand_equal_p (tree11, tree011, 0))
b6cc0a72
KH
5565 return build ((code0 != LSHIFT_EXPR
5566 ? LROTATE_EXPR
5567 : RROTATE_EXPR),
5568 type, TREE_OPERAND (arg0, 0), tree11);
79e8185c
JH
5569 }
5570 }
5571 }
10414089 5572
6d716ca8 5573 associate:
1baa375f
RK
5574 /* In most languages, can't associate operations on floats through
5575 parentheses. Rather than remember where the parentheses were, we
5576 don't associate floats at all. It shouldn't matter much. However,
5577 associating multiplications is only very slightly inaccurate, so do
de6c5979 5578 that if -funsafe-math-optimizations is specified. */
1baa375f
RK
5579
5580 if (! wins
5581 && (! FLOAT_TYPE_P (type)
de6c5979 5582 || (flag_unsafe_math_optimizations && code == MULT_EXPR)))
6d716ca8 5583 {
1baa375f
RK
5584 tree var0, con0, lit0, var1, con1, lit1;
5585
5586 /* Split both trees into variables, constants, and literals. Then
5587 associate each group together, the constants with literals,
5588 then the result with variables. This increases the chances of
5589 literals being recombined later and of generating relocatable
30f7a378 5590 expressions for the sum of a constant and literal. */
1baa375f
RK
5591 var0 = split_tree (arg0, code, &con0, &lit0, 0);
5592 var1 = split_tree (arg1, code, &con1, &lit1, code == MINUS_EXPR);
5593
5594 /* Only do something if we found more than two objects. Otherwise,
5595 nothing has changed and we risk infinite recursion. */
5596 if (2 < ((var0 != 0) + (var1 != 0) + (con0 != 0) + (con1 != 0)
5597 + (lit0 != 0) + (lit1 != 0)))
6d716ca8 5598 {
1baa375f
RK
5599 var0 = associate_trees (var0, var1, code, type);
5600 con0 = associate_trees (con0, con1, code, type);
5601 lit0 = associate_trees (lit0, lit1, code, type);
5602 con0 = associate_trees (con0, lit0, code, type);
5603 return convert (type, associate_trees (var0, con0, code, type));
6d716ca8
RS
5604 }
5605 }
1baa375f 5606
6d716ca8
RS
5607 binary:
5608#if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
5609 if (TREE_CODE (arg1) == REAL_CST)
5610 return t;
5611#endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
5612 if (wins)
91d33e36 5613 t1 = const_binop (code, arg0, arg1, 0);
6d716ca8
RS
5614 if (t1 != NULL_TREE)
5615 {
5616 /* The return value should always have
5617 the same type as the original expression. */
380ff34a
RK
5618 if (TREE_TYPE (t1) != TREE_TYPE (t))
5619 t1 = convert (TREE_TYPE (t), t1);
5620
6d716ca8
RS
5621 return t1;
5622 }
5623 return t;
5624
5625 case MINUS_EXPR:
10414089
JL
5626 /* A - (-B) -> A + B */
5627 if (TREE_CODE (arg1) == NEGATE_EXPR)
5628 return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5629 /* (-A) - CST -> (-CST) - A for floating point (what about ints ?) */
5630 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == REAL_CST)
5631 return
b6cc0a72 5632 fold (build (MINUS_EXPR, type,
10414089
JL
5633 build_real (TREE_TYPE (arg1),
5634 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1))),
5635 TREE_OPERAND (arg0, 0)));
5636
7178e3af 5637 if (! FLOAT_TYPE_P (type))
6d716ca8
RS
5638 {
5639 if (! wins && integer_zerop (arg0))
b8fbe62c 5640 return negate_expr (convert (type, arg1));
6d716ca8
RS
5641 if (integer_zerop (arg1))
5642 return non_lvalue (convert (type, arg0));
6a96fcb4
RK
5643
5644 /* (A * C) - (B * C) -> (A-B) * C. Since we are most concerned
5645 about the case where C is a constant, just try one of the
5646 four possibilities. */
5647
5648 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
5649 && operand_equal_p (TREE_OPERAND (arg0, 1),
5650 TREE_OPERAND (arg1, 1), 0))
5651 return fold (build (MULT_EXPR, type,
5652 fold (build (MINUS_EXPR, type,
5653 TREE_OPERAND (arg0, 0),
5654 TREE_OPERAND (arg1, 0))),
5655 TREE_OPERAND (arg0, 1)));
6d716ca8 5656 }
fab446b8
RK
5657
5658 else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
de6c5979 5659 || flag_unsafe_math_optimizations)
6d716ca8 5660 {
c05a9b68 5661 /* Except with IEEE floating point, 0-x equals -x. */
6d716ca8 5662 if (! wins && real_zerop (arg0))
b8fbe62c 5663 return negate_expr (convert (type, arg1));
c05a9b68
RS
5664 /* Except with IEEE floating point, x-0 equals x. */
5665 if (real_zerop (arg1))
6d716ca8 5666 return non_lvalue (convert (type, arg0));
fab446b8 5667 }
a6acbe15 5668
b6cc0a72 5669 /* Fold &x - &x. This can happen from &x.foo - &x.
fab446b8
RK
5670 This is unsafe for certain floats even in non-IEEE formats.
5671 In IEEE, it is unsafe because it does wrong for NaNs.
5672 Also note that operand_equal_p is always false if an operand
5673 is volatile. */
5674
de6c5979 5675 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
59d90212 5676 && operand_equal_p (arg0, arg1, 0))
fab446b8 5677 return convert (type, integer_zero_node);
a6acbe15 5678
6d716ca8
RS
5679 goto associate;
5680
5681 case MULT_EXPR:
10414089
JL
5682 /* (-A) * (-B) -> A * B */
5683 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
5684 return fold (build (MULT_EXPR, type, TREE_OPERAND (arg0, 0),
b6cc0a72 5685 TREE_OPERAND (arg1, 0)));
10414089 5686
7178e3af 5687 if (! FLOAT_TYPE_P (type))
6d716ca8
RS
5688 {
5689 if (integer_zerop (arg1))
5690 return omit_one_operand (type, arg1, arg0);
5691 if (integer_onep (arg1))
5692 return non_lvalue (convert (type, arg0));
5693
5694 /* (a * (1 << b)) is (a << b) */
5695 if (TREE_CODE (arg1) == LSHIFT_EXPR
5696 && integer_onep (TREE_OPERAND (arg1, 0)))
5697 return fold (build (LSHIFT_EXPR, type, arg0,
5698 TREE_OPERAND (arg1, 1)));
5699 if (TREE_CODE (arg0) == LSHIFT_EXPR
5700 && integer_onep (TREE_OPERAND (arg0, 0)))
5701 return fold (build (LSHIFT_EXPR, type, arg1,
5702 TREE_OPERAND (arg0, 1)));
1baa375f
RK
5703
5704 if (TREE_CODE (arg1) == INTEGER_CST
5705 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5706 code, NULL_TREE)))
5707 return convert (type, tem);
5708
6d716ca8 5709 }
6d716ca8
RS
5710 else
5711 {
c05a9b68 5712 /* x*0 is 0, except for IEEE floating point. */
fab446b8 5713 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
de6c5979 5714 || flag_unsafe_math_optimizations)
6d716ca8
RS
5715 && real_zerop (arg1))
5716 return omit_one_operand (type, arg1, arg0);
c05a9b68 5717 /* In IEEE floating point, x*1 is not equivalent to x for snans.
6d716ca8
RS
5718 However, ANSI says we can drop signals,
5719 so we can do this anyway. */
5720 if (real_onep (arg1))
5721 return non_lvalue (convert (type, arg0));
5722 /* x*2 is x+x */
c5c76735 5723 if (! wins && real_twop (arg1) && global_bindings_p () == 0
9ec36da5 5724 && ! contains_placeholder_p (arg0))
6d716ca8
RS
5725 {
5726 tree arg = save_expr (arg0);
5727 return build (PLUS_EXPR, type, arg, arg);
5728 }
5729 }
5730 goto associate;
5731
5732 case BIT_IOR_EXPR:
5733 bit_ior:
5734 if (integer_all_onesp (arg1))
5735 return omit_one_operand (type, arg1, arg0);
5736 if (integer_zerop (arg1))
5737 return non_lvalue (convert (type, arg0));
5738 t1 = distribute_bit_expr (code, type, arg0, arg1);
5739 if (t1 != NULL_TREE)
5740 return t1;
85d2e16c 5741
ccc5fd95
JH
5742 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
5743
b6cc0a72 5744 This results in more efficient code for machines without a NAND
ccc5fd95
JH
5745 instruction. Combine will canonicalize to the first form
5746 which will allow use of NAND instructions provided by the
5747 backend if they exist. */
5748 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5749 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5750 {
5751 return fold (build1 (BIT_NOT_EXPR, type,
5752 build (BIT_AND_EXPR, type,
5753 TREE_OPERAND (arg0, 0),
5754 TREE_OPERAND (arg1, 0))));
5755 }
5756
79e8185c
JH
5757 /* See if this can be simplified into a rotate first. If that
5758 is unsuccessful continue in the association code. */
5759 goto bit_rotate;
6d716ca8
RS
5760
5761 case BIT_XOR_EXPR:
5762 if (integer_zerop (arg1))
5763 return non_lvalue (convert (type, arg0));
5764 if (integer_all_onesp (arg1))
5765 return fold (build1 (BIT_NOT_EXPR, type, arg0));
79e8185c
JH
5766
5767 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
5768 with a constant, and the two constants have no bits in common,
5769 we should treat this as a BIT_IOR_EXPR since this may produce more
5770 simplifications. */
5771 if (TREE_CODE (arg0) == BIT_AND_EXPR
5772 && TREE_CODE (arg1) == BIT_AND_EXPR
5773 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5774 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5775 && integer_zerop (const_binop (BIT_AND_EXPR,
5776 TREE_OPERAND (arg0, 1),
5777 TREE_OPERAND (arg1, 1), 0)))
b6cc0a72
KH
5778 {
5779 code = BIT_IOR_EXPR;
5780 goto bit_ior;
5781 }
79e8185c 5782
03e0a65f 5783 /* See if this can be simplified into a rotate first. If that
79e8185c 5784 is unsuccessful continue in the association code. */
03e0a65f 5785 goto bit_rotate;
6d716ca8
RS
5786
5787 case BIT_AND_EXPR:
5788 bit_and:
5789 if (integer_all_onesp (arg1))
5790 return non_lvalue (convert (type, arg0));
5791 if (integer_zerop (arg1))
5792 return omit_one_operand (type, arg1, arg0);
5793 t1 = distribute_bit_expr (code, type, arg0, arg1);
5794 if (t1 != NULL_TREE)
5795 return t1;
5796 /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */
5797 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
5798 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
5799 {
770ae6cc
RK
5800 unsigned int prec
5801 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
5802
906c4e36
RK
5803 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5804 && (~TREE_INT_CST_LOW (arg0)
5805 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
6d716ca8
RS
5806 return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
5807 }
5808 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
5809 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5810 {
770ae6cc
RK
5811 unsigned int prec
5812 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
5813
906c4e36
RK
5814 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5815 && (~TREE_INT_CST_LOW (arg1)
5816 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
6d716ca8
RS
5817 return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
5818 }
ccc5fd95 5819
a36335da 5820 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
ccc5fd95 5821
b6cc0a72 5822 This results in more efficient code for machines without a NOR
ccc5fd95
JH
5823 instruction. Combine will canonicalize to the first form
5824 which will allow use of NOR instructions provided by the
5825 backend if they exist. */
5826 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5827 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5828 {
5829 return fold (build1 (BIT_NOT_EXPR, type,
5830 build (BIT_IOR_EXPR, type,
5831 TREE_OPERAND (arg0, 0),
5832 TREE_OPERAND (arg1, 0))));
5833 }
5834
6d716ca8
RS
5835 goto associate;
5836
5837 case BIT_ANDTC_EXPR:
5838 if (integer_all_onesp (arg0))
5839 return non_lvalue (convert (type, arg1));
5840 if (integer_zerop (arg0))
5841 return omit_one_operand (type, arg0, arg1);
5842 if (TREE_CODE (arg1) == INTEGER_CST)
5843 {
5844 arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
5845 code = BIT_AND_EXPR;
5846 goto bit_and;
5847 }
5848 goto binary;
5849
e9aaa5a5
RK
5850 case RDIV_EXPR:
5851 /* In most cases, do nothing with a divide by zero. */
5852#if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
5853#ifndef REAL_INFINITY
5854 if (TREE_CODE (arg1) == REAL_CST && real_zerop (arg1))
5855 return t;
5856#endif
5857#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
5858
10414089
JL
5859 /* (-A) / (-B) -> A / B */
5860 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
5861 return fold (build (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
5862 TREE_OPERAND (arg1, 0)));
5863
e9aaa5a5
RK
5864 /* In IEEE floating point, x/1 is not equivalent to x for snans.
5865 However, ANSI says we can drop signals, so we can do this anyway. */
5866 if (real_onep (arg1))
5867 return non_lvalue (convert (type, arg0));
5868
5869 /* If ARG1 is a constant, we can convert this to a multiply by the
5870 reciprocal. This does not have the same rounding properties,
de6c5979
BL
5871 so only do this if -funsafe-math-optimizations. We can actually
5872 always safely do it if ARG1 is a power of two, but it's hard to
5873 tell if it is or not in a portable manner. */
39647dcb
RK
5874 if (TREE_CODE (arg1) == REAL_CST)
5875 {
de6c5979 5876 if (flag_unsafe_math_optimizations
39647dcb
RK
5877 && 0 != (tem = const_binop (code, build_real (type, dconst1),
5878 arg1, 0)))
5879 return fold (build (MULT_EXPR, type, arg0, tem));
30f7a378 5880 /* Find the reciprocal if optimizing and the result is exact. */
39647dcb
RK
5881 else if (optimize)
5882 {
5883 REAL_VALUE_TYPE r;
5884 r = TREE_REAL_CST (arg1);
5885 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
b6cc0a72
KH
5886 {
5887 tem = build_real (type, r);
5888 return fold (build (MULT_EXPR, type, arg0, tem));
5889 }
39647dcb
RK
5890 }
5891 }
bc8d3f91
JH
5892 /* Convert A/B/C to A/(B*C). */
5893 if (flag_unsafe_math_optimizations
5894 && TREE_CODE (arg0) == RDIV_EXPR)
5895 {
5896 return fold (build (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
5897 build (MULT_EXPR, type, TREE_OPERAND (arg0, 1),
5898 arg1)));
5899 }
5900 /* Convert A/(B/C) to (A/B)*C. */
5901 if (flag_unsafe_math_optimizations
5902 && TREE_CODE (arg1) == RDIV_EXPR)
5903 {
5904 return fold (build (MULT_EXPR, type,
5905 build (RDIV_EXPR, type, arg0,
5906 TREE_OPERAND (arg1, 0)),
5907 TREE_OPERAND (arg1, 1)));
5908 }
e9aaa5a5
RK
5909 goto binary;
5910
6d716ca8
RS
5911 case TRUNC_DIV_EXPR:
5912 case ROUND_DIV_EXPR:
5913 case FLOOR_DIV_EXPR:
5914 case CEIL_DIV_EXPR:
5915 case EXACT_DIV_EXPR:
6d716ca8
RS
5916 if (integer_onep (arg1))
5917 return non_lvalue (convert (type, arg0));
5918 if (integer_zerop (arg1))
5919 return t;
3b998c11 5920
39dfb55a
JL
5921 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
5922 operation, EXACT_DIV_EXPR.
5923
91585c63
TM
5924 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
5925 At one time others generated faster code, it's not clear if they do
5926 after the last round to changes to the DIV code in expmed.c. */
5927 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
39dfb55a
JL
5928 && multiple_of_p (type, arg0, arg1))
5929 return fold (build (EXACT_DIV_EXPR, type, arg0, arg1));
5930
b6cc0a72 5931 if (TREE_CODE (arg1) == INTEGER_CST
1baa375f
RK
5932 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5933 code, NULL_TREE)))
5934 return convert (type, tem);
3b998c11 5935
6d716ca8
RS
5936 goto binary;
5937
5938 case CEIL_MOD_EXPR:
5939 case FLOOR_MOD_EXPR:
5940 case ROUND_MOD_EXPR:
5941 case TRUNC_MOD_EXPR:
5942 if (integer_onep (arg1))
5943 return omit_one_operand (type, integer_zero_node, arg0);
5944 if (integer_zerop (arg1))
5945 return t;
6a96fcb4 5946
6a96fcb4 5947 if (TREE_CODE (arg1) == INTEGER_CST
1baa375f
RK
5948 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5949 code, NULL_TREE)))
5950 return convert (type, tem);
6a96fcb4 5951
6d716ca8
RS
5952 goto binary;
5953
5954 case LSHIFT_EXPR:
5955 case RSHIFT_EXPR:
5956 case LROTATE_EXPR:
5957 case RROTATE_EXPR:
5958 if (integer_zerop (arg1))
5959 return non_lvalue (convert (type, arg0));
5960 /* Since negative shift count is not well-defined,
5961 don't try to compute it in the compiler. */
4d39710e 5962 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
6d716ca8 5963 return t;
4d39710e
RK
5964 /* Rewrite an LROTATE_EXPR by a constant into an
5965 RROTATE_EXPR by a new constant. */
5966 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
5967 {
5968 TREE_SET_CODE (t, RROTATE_EXPR);
5969 code = RROTATE_EXPR;
5970 TREE_OPERAND (t, 1) = arg1
5971 = const_binop
5972 (MINUS_EXPR,
5973 convert (TREE_TYPE (arg1),
5974 build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0)),
5975 arg1, 0);
5976 if (tree_int_cst_sgn (arg1) < 0)
5977 return t;
5978 }
5979
5980 /* If we have a rotate of a bit operation with the rotate count and
5981 the second operand of the bit operation both constant,
5982 permute the two operations. */
5983 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
5984 && (TREE_CODE (arg0) == BIT_AND_EXPR
5985 || TREE_CODE (arg0) == BIT_ANDTC_EXPR
5986 || TREE_CODE (arg0) == BIT_IOR_EXPR
5987 || TREE_CODE (arg0) == BIT_XOR_EXPR)
5988 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
5989 return fold (build (TREE_CODE (arg0), type,
5990 fold (build (code, type,
5991 TREE_OPERAND (arg0, 0), arg1)),
5992 fold (build (code, type,
5993 TREE_OPERAND (arg0, 1), arg1))));
5994
5995 /* Two consecutive rotates adding up to the width of the mode can
5996 be ignored. */
5997 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
5998 && TREE_CODE (arg0) == RROTATE_EXPR
5999 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6000 && TREE_INT_CST_HIGH (arg1) == 0
6001 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
6002 && ((TREE_INT_CST_LOW (arg1)
6003 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
05bccae2 6004 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
4d39710e
RK
6005 return TREE_OPERAND (arg0, 0);
6006
6d716ca8
RS
6007 goto binary;
6008
6009 case MIN_EXPR:
6010 if (operand_equal_p (arg0, arg1, 0))
13eb1f7f 6011 return omit_one_operand (type, arg0, arg1);
7178e3af 6012 if (INTEGRAL_TYPE_P (type)
6d716ca8
RS
6013 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
6014 return omit_one_operand (type, arg1, arg0);
6015 goto associate;
6016
6017 case MAX_EXPR:
6018 if (operand_equal_p (arg0, arg1, 0))
13eb1f7f 6019 return omit_one_operand (type, arg0, arg1);
7178e3af 6020 if (INTEGRAL_TYPE_P (type)
e1ee5cdc 6021 && TYPE_MAX_VALUE (type)
6d716ca8
RS
6022 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
6023 return omit_one_operand (type, arg1, arg0);
6024 goto associate;
6025
6026 case TRUTH_NOT_EXPR:
6027 /* Note that the operand of this must be an int
6028 and its values must be 0 or 1.
6029 ("true" is a fixed value perhaps depending on the language,
6030 but we don't handle values other than 1 correctly yet.) */
1180eb10
JM
6031 tem = invert_truthvalue (arg0);
6032 /* Avoid infinite recursion. */
6033 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
6034 return t;
6035 return convert (type, tem);
6d716ca8
RS
6036
6037 case TRUTH_ANDIF_EXPR:
6038 /* Note that the operands of this must be ints
6039 and their values must be 0 or 1.
6040 ("true" is a fixed value perhaps depending on the language.) */
6041 /* If first arg is constant zero, return it. */
772447c5 6042 if (integer_zerop (arg0))
13eb1f7f 6043 return convert (type, arg0);
6d716ca8
RS
6044 case TRUTH_AND_EXPR:
6045 /* If either arg is constant true, drop it. */
6046 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
13eb1f7f 6047 return non_lvalue (convert (type, arg1));
235cfbc4
BS
6048 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
6049 /* Preserve sequence points. */
6050 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
13eb1f7f 6051 return non_lvalue (convert (type, arg0));
772447c5
RK
6052 /* If second arg is constant zero, result is zero, but first arg
6053 must be evaluated. */
6054 if (integer_zerop (arg1))
6055 return omit_one_operand (type, arg1, arg0);
80906567
RK
6056 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
6057 case will be handled here. */
6058 if (integer_zerop (arg0))
6059 return omit_one_operand (type, arg0, arg1);
6d716ca8
RS
6060
6061 truth_andor:
e9b5e15f
RK
6062 /* We only do these simplifications if we are optimizing. */
6063 if (!optimize)
6064 return t;
6065
6066 /* Check for things like (A || B) && (A || C). We can convert this
6067 to A || (B && C). Note that either operator can be any of the four
6068 truth and/or operations and the transformation will still be
6069 valid. Also note that we only care about order for the
96f06cb6 6070 ANDIF and ORIF operators. If B contains side effects, this
30f7a378 6071 might change the truth-value of A. */
e9b5e15f
RK
6072 if (TREE_CODE (arg0) == TREE_CODE (arg1)
6073 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
6074 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
6075 || TREE_CODE (arg0) == TRUTH_AND_EXPR
96f06cb6
RK
6076 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
6077 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
e9b5e15f
RK
6078 {
6079 tree a00 = TREE_OPERAND (arg0, 0);
6080 tree a01 = TREE_OPERAND (arg0, 1);
6081 tree a10 = TREE_OPERAND (arg1, 0);
6082 tree a11 = TREE_OPERAND (arg1, 1);
6083 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
6084 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
6085 && (code == TRUTH_AND_EXPR
6086 || code == TRUTH_OR_EXPR));
6087
6088 if (operand_equal_p (a00, a10, 0))
6089 return fold (build (TREE_CODE (arg0), type, a00,
6090 fold (build (code, type, a01, a11))));
6091 else if (commutative && operand_equal_p (a00, a11, 0))
6092 return fold (build (TREE_CODE (arg0), type, a00,
6093 fold (build (code, type, a01, a10))));
6094 else if (commutative && operand_equal_p (a01, a10, 0))
6095 return fold (build (TREE_CODE (arg0), type, a01,
6096 fold (build (code, type, a00, a11))));
6097
6098 /* This case if tricky because we must either have commutative
6099 operators or else A10 must not have side-effects. */
6100
6101 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
6102 && operand_equal_p (a01, a11, 0))
6103 return fold (build (TREE_CODE (arg0), type,
6104 fold (build (code, type, a00, a10)),
6105 a01));
6106 }
6107
ebde8a27
RK
6108 /* See if we can build a range comparison. */
6109 if (0 != (tem = fold_range_test (t)))
6110 return tem;
6111
6d716ca8
RS
6112 /* Check for the possibility of merging component references. If our
6113 lhs is another similar operation, try to merge its rhs with our
6114 rhs. Then try to merge our lhs and rhs. */
e9b5e15f
RK
6115 if (TREE_CODE (arg0) == code
6116 && 0 != (tem = fold_truthop (code, type,
6117 TREE_OPERAND (arg0, 1), arg1)))
6118 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6d716ca8 6119
e9b5e15f
RK
6120 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
6121 return tem;
61f275ff 6122
6d716ca8
RS
6123 return t;
6124
6125 case TRUTH_ORIF_EXPR:
6126 /* Note that the operands of this must be ints
6127 and their values must be 0 or true.
6128 ("true" is a fixed value perhaps depending on the language.) */
6129 /* If first arg is constant true, return it. */
6130 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
13eb1f7f 6131 return convert (type, arg0);
6d716ca8
RS
6132 case TRUTH_OR_EXPR:
6133 /* If either arg is constant zero, drop it. */
6134 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
13eb1f7f 6135 return non_lvalue (convert (type, arg1));
235cfbc4
BS
6136 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
6137 /* Preserve sequence points. */
6138 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
13eb1f7f 6139 return non_lvalue (convert (type, arg0));
772447c5
RK
6140 /* If second arg is constant true, result is true, but we must
6141 evaluate first arg. */
6142 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
6143 return omit_one_operand (type, arg1, arg0);
80906567
RK
6144 /* Likewise for first arg, but note this only occurs here for
6145 TRUTH_OR_EXPR. */
6146 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6147 return omit_one_operand (type, arg0, arg1);
6d716ca8
RS
6148 goto truth_andor;
6149
772447c5
RK
6150 case TRUTH_XOR_EXPR:
6151 /* If either arg is constant zero, drop it. */
6152 if (integer_zerop (arg0))
13eb1f7f 6153 return non_lvalue (convert (type, arg1));
772447c5 6154 if (integer_zerop (arg1))
13eb1f7f 6155 return non_lvalue (convert (type, arg0));
772447c5
RK
6156 /* If either arg is constant true, this is a logical inversion. */
6157 if (integer_onep (arg0))
13eb1f7f 6158 return non_lvalue (convert (type, invert_truthvalue (arg1)));
772447c5 6159 if (integer_onep (arg1))
13eb1f7f 6160 return non_lvalue (convert (type, invert_truthvalue (arg0)));
62d8b51e 6161 return t;
772447c5 6162
6d716ca8
RS
6163 case EQ_EXPR:
6164 case NE_EXPR:
6165 case LT_EXPR:
6166 case GT_EXPR:
6167 case LE_EXPR:
6168 case GE_EXPR:
10414089
JL
6169 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
6170 {
6171 /* (-a) CMP (-b) -> b CMP a */
6172 if (TREE_CODE (arg0) == NEGATE_EXPR
6173 && TREE_CODE (arg1) == NEGATE_EXPR)
6174 return fold (build (code, type, TREE_OPERAND (arg1, 0),
6175 TREE_OPERAND (arg0, 0)));
6176 /* (-a) CMP CST -> a swap(CMP) (-CST) */
6177 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == REAL_CST)
6178 return
6179 fold (build
b6cc0a72
KH
6180 (swap_tree_comparison (code), type,
6181 TREE_OPERAND (arg0, 0),
6182 build_real (TREE_TYPE (arg1),
6183 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1)))));
10414089
JL
6184 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6185 /* a CMP (-0) -> a CMP 0 */
0d19c2a1
JL
6186 if (TREE_CODE (arg1) == REAL_CST
6187 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1)))
10414089
JL
6188 return fold (build (code, type, arg0,
6189 build_real (TREE_TYPE (arg1), dconst0)));
6190 }
6191
6d716ca8
RS
6192 /* If one arg is a constant integer, put it last. */
6193 if (TREE_CODE (arg0) == INTEGER_CST
6194 && TREE_CODE (arg1) != INTEGER_CST)
6195 {
6196 TREE_OPERAND (t, 0) = arg1;
6197 TREE_OPERAND (t, 1) = arg0;
6198 arg0 = TREE_OPERAND (t, 0);
6199 arg1 = TREE_OPERAND (t, 1);
c05a9b68 6200 code = swap_tree_comparison (code);
6d716ca8
RS
6201 TREE_SET_CODE (t, code);
6202 }
6203
6204 /* Convert foo++ == CONST into ++foo == CONST + INCR.
6205 First, see if one arg is constant; find the constant arg
6206 and the other one. */
6207 {
4e86caed 6208 tree constop = 0, varop = NULL_TREE;
cd7ece66 6209 int constopnum = -1;
6d716ca8
RS
6210
6211 if (TREE_CONSTANT (arg1))
cd7ece66 6212 constopnum = 1, constop = arg1, varop = arg0;
6d716ca8 6213 if (TREE_CONSTANT (arg0))
cd7ece66 6214 constopnum = 0, constop = arg0, varop = arg1;
6d716ca8
RS
6215
6216 if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
6217 {
6d716ca8
RS
6218 /* This optimization is invalid for ordered comparisons
6219 if CONST+INCR overflows or if foo+incr might overflow.
c05a9b68 6220 This optimization is invalid for floating point due to rounding.
6d716ca8 6221 For pointer types we assume overflow doesn't happen. */
e5e809f4 6222 if (POINTER_TYPE_P (TREE_TYPE (varop))
7178e3af 6223 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
c05a9b68 6224 && (code == EQ_EXPR || code == NE_EXPR)))
6d716ca8 6225 {
c05a9b68
RS
6226 tree newconst
6227 = fold (build (PLUS_EXPR, TREE_TYPE (varop),
6228 constop, TREE_OPERAND (varop, 1)));
4d06bcc5
MM
6229
6230 /* Do not overwrite the current varop to be a preincrement,
6231 create a new node so that we won't confuse our caller who
6232 might create trees and throw them away, reusing the
6233 arguments that they passed to build. This shows up in
6234 the THEN or ELSE parts of ?: being postincrements. */
6235 varop = build (PREINCREMENT_EXPR, TREE_TYPE (varop),
6236 TREE_OPERAND (varop, 0),
6237 TREE_OPERAND (varop, 1));
cd7ece66 6238
30eca391
RK
6239 /* If VAROP is a reference to a bitfield, we must mask
6240 the constant by the width of the field. */
6241 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6242 && DECL_BIT_FIELD(TREE_OPERAND
6243 (TREE_OPERAND (varop, 0), 1)))
6244 {
6245 int size
6246 = TREE_INT_CST_LOW (DECL_SIZE
6247 (TREE_OPERAND
6248 (TREE_OPERAND (varop, 0), 1)));
df0e526f 6249 tree mask, unsigned_type;
770ae6cc 6250 unsigned int precision;
df0e526f
AS
6251 tree folded_compare;
6252
6253 /* First check whether the comparison would come out
6254 always the same. If we don't do that we would
6255 change the meaning with the masking. */
6256 if (constopnum == 0)
6257 folded_compare = fold (build (code, type, constop,
6258 TREE_OPERAND (varop, 0)));
6259 else
6260 folded_compare = fold (build (code, type,
6261 TREE_OPERAND (varop, 0),
6262 constop));
6263 if (integer_zerop (folded_compare)
6264 || integer_onep (folded_compare))
6265 return omit_one_operand (type, folded_compare, varop);
6266
6267 unsigned_type = type_for_size (size, 1);
6268 precision = TYPE_PRECISION (unsigned_type);
6269 mask = build_int_2 (~0, ~0);
6270 TREE_TYPE (mask) = unsigned_type;
6271 force_fit_type (mask, 0);
6272 mask = const_binop (RSHIFT_EXPR, mask,
6273 size_int (precision - size), 0);
30eca391
RK
6274 newconst = fold (build (BIT_AND_EXPR,
6275 TREE_TYPE (varop), newconst,
6276 convert (TREE_TYPE (varop),
df0e526f 6277 mask)));
30eca391 6278 }
30eca391 6279
4d06bcc5
MM
6280 t = build (code, type,
6281 (constopnum == 0) ? newconst : varop,
6282 (constopnum == 1) ? newconst : varop);
c05a9b68 6283 return t;
6d716ca8
RS
6284 }
6285 }
6286 else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
6287 {
e5e809f4 6288 if (POINTER_TYPE_P (TREE_TYPE (varop))
7178e3af 6289 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
c05a9b68 6290 && (code == EQ_EXPR || code == NE_EXPR)))
6d716ca8 6291 {
c05a9b68
RS
6292 tree newconst
6293 = fold (build (MINUS_EXPR, TREE_TYPE (varop),
6294 constop, TREE_OPERAND (varop, 1)));
4d06bcc5
MM
6295
6296 /* Do not overwrite the current varop to be a predecrement,
6297 create a new node so that we won't confuse our caller who
6298 might create trees and throw them away, reusing the
6299 arguments that they passed to build. This shows up in
6300 the THEN or ELSE parts of ?: being postdecrements. */
6301 varop = build (PREDECREMENT_EXPR, TREE_TYPE (varop),
6302 TREE_OPERAND (varop, 0),
6303 TREE_OPERAND (varop, 1));
30eca391
RK
6304
6305 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6306 && DECL_BIT_FIELD(TREE_OPERAND
6307 (TREE_OPERAND (varop, 0), 1)))
6308 {
6309 int size
6310 = TREE_INT_CST_LOW (DECL_SIZE
6311 (TREE_OPERAND
6312 (TREE_OPERAND (varop, 0), 1)));
df0e526f 6313 tree mask, unsigned_type;
770ae6cc 6314 unsigned int precision;
df0e526f
AS
6315 tree folded_compare;
6316
6317 if (constopnum == 0)
6318 folded_compare = fold (build (code, type, constop,
6319 TREE_OPERAND (varop, 0)));
6320 else
6321 folded_compare = fold (build (code, type,
6322 TREE_OPERAND (varop, 0),
6323 constop));
6324 if (integer_zerop (folded_compare)
6325 || integer_onep (folded_compare))
6326 return omit_one_operand (type, folded_compare, varop);
6327
6328 unsigned_type = type_for_size (size, 1);
6329 precision = TYPE_PRECISION (unsigned_type);
6330 mask = build_int_2 (~0, ~0);
6331 TREE_TYPE (mask) = TREE_TYPE (varop);
6332 force_fit_type (mask, 0);
6333 mask = const_binop (RSHIFT_EXPR, mask,
6334 size_int (precision - size), 0);
30eca391
RK
6335 newconst = fold (build (BIT_AND_EXPR,
6336 TREE_TYPE (varop), newconst,
6337 convert (TREE_TYPE (varop),
df0e526f 6338 mask)));
30eca391 6339 }
30eca391 6340
4d06bcc5
MM
6341 t = build (code, type,
6342 (constopnum == 0) ? newconst : varop,
6343 (constopnum == 1) ? newconst : varop);
c05a9b68 6344 return t;
6d716ca8
RS
6345 }
6346 }
6347 }
6348
6349 /* Change X >= CST to X > (CST - 1) if CST is positive. */
6350 if (TREE_CODE (arg1) == INTEGER_CST
6351 && TREE_CODE (arg0) != INTEGER_CST
d0cb4c65 6352 && tree_int_cst_sgn (arg1) > 0)
6d716ca8
RS
6353 {
6354 switch (TREE_CODE (t))
6355 {
6356 case GE_EXPR:
6357 code = GT_EXPR;
91d33e36 6358 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
cd7ece66 6359 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6d716ca8
RS
6360 break;
6361
6362 case LT_EXPR:
6363 code = LE_EXPR;
91d33e36 6364 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
cd7ece66
RK
6365 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6366 break;
e9a25f70
JL
6367
6368 default:
6369 break;
6d716ca8
RS
6370 }
6371 }
6372
14a774a9
RK
6373 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
6374 a MINUS_EXPR of a constant, we can convert it into a comparison with
6375 a revised constant as long as no overflow occurs. */
6376 if ((code == EQ_EXPR || code == NE_EXPR)
6377 && TREE_CODE (arg1) == INTEGER_CST
6378 && (TREE_CODE (arg0) == PLUS_EXPR
6379 || TREE_CODE (arg0) == MINUS_EXPR)
6380 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6381 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
6382 ? MINUS_EXPR : PLUS_EXPR,
6383 arg1, TREE_OPERAND (arg0, 1), 0))
6384 && ! TREE_CONSTANT_OVERFLOW (tem))
6385 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6386
6387 /* Similarly for a NEGATE_EXPR. */
6388 else if ((code == EQ_EXPR || code == NE_EXPR)
6389 && TREE_CODE (arg0) == NEGATE_EXPR
6390 && TREE_CODE (arg1) == INTEGER_CST
1baa375f 6391 && 0 != (tem = negate_expr (arg1))
14a774a9
RK
6392 && TREE_CODE (tem) == INTEGER_CST
6393 && ! TREE_CONSTANT_OVERFLOW (tem))
6394 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6395
6396 /* If we have X - Y == 0, we can convert that to X == Y and similarly
6397 for !=. Don't do this for ordered comparisons due to overflow. */
6398 else if ((code == NE_EXPR || code == EQ_EXPR)
6399 && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
6400 return fold (build (code, type,
6401 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
6402
6403 /* If we are widening one operand of an integer comparison,
6404 see if the other operand is similarly being widened. Perhaps we
6405 can do the comparison in the narrower type. */
6406 else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
6407 && TREE_CODE (arg0) == NOP_EXPR
6408 && (tem = get_unwidened (arg0, NULL_TREE)) != arg0
6409 && (t1 = get_unwidened (arg1, TREE_TYPE (tem))) != 0
6410 && (TREE_TYPE (t1) == TREE_TYPE (tem)
6411 || (TREE_CODE (t1) == INTEGER_CST
6412 && int_fits_type_p (t1, TREE_TYPE (tem)))))
6413 return fold (build (code, type, tem, convert (TREE_TYPE (tem), t1)));
b6cc0a72 6414
14a774a9
RK
6415 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
6416 constant, we can simplify it. */
6417 else if (TREE_CODE (arg1) == INTEGER_CST
6418 && (TREE_CODE (arg0) == MIN_EXPR
6419 || TREE_CODE (arg0) == MAX_EXPR)
6420 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
6421 return optimize_minmax_comparison (t);
6422
6423 /* If we are comparing an ABS_EXPR with a constant, we can
6424 convert all the cases into explicit comparisons, but they may
6425 well not be faster than doing the ABS and one comparison.
6426 But ABS (X) <= C is a range comparison, which becomes a subtraction
6427 and a comparison, and is probably faster. */
6428 else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6429 && TREE_CODE (arg0) == ABS_EXPR
1baa375f
RK
6430 && ! TREE_SIDE_EFFECTS (arg0)
6431 && (0 != (tem = negate_expr (arg1)))
6432 && TREE_CODE (tem) == INTEGER_CST
6433 && ! TREE_CONSTANT_OVERFLOW (tem))
6434 return fold (build (TRUTH_ANDIF_EXPR, type,
6435 build (GE_EXPR, type, TREE_OPERAND (arg0, 0), tem),
6436 build (LE_EXPR, type,
6437 TREE_OPERAND (arg0, 0), arg1)));
b6cc0a72 6438
6d716ca8
RS
6439 /* If this is an EQ or NE comparison with zero and ARG0 is
6440 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
6441 two operations, but the latter can be done in one less insn
e9a25f70 6442 on machines that have only two-operand insns or on which a
6d716ca8
RS
6443 constant cannot be the first operand. */
6444 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
6445 && TREE_CODE (arg0) == BIT_AND_EXPR)
6446 {
6447 if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
6448 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
6449 return
6450 fold (build (code, type,
6451 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6452 build (RSHIFT_EXPR,
6453 TREE_TYPE (TREE_OPERAND (arg0, 0)),
6454 TREE_OPERAND (arg0, 1),
6455 TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
6456 convert (TREE_TYPE (arg0),
6457 integer_one_node)),
6458 arg1));
6459 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
6460 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
6461 return
6462 fold (build (code, type,
6463 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6464 build (RSHIFT_EXPR,
6465 TREE_TYPE (TREE_OPERAND (arg0, 1)),
6466 TREE_OPERAND (arg0, 0),
6467 TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
6468 convert (TREE_TYPE (arg0),
6469 integer_one_node)),
6470 arg1));
6471 }
6472
05a0d5ea 6473 /* If this is an NE or EQ comparison of zero against the result of a
79bf94d3
RK
6474 signed MOD operation whose second operand is a power of 2, make
6475 the MOD operation unsigned since it is simpler and equivalent. */
05a0d5ea
RK
6476 if ((code == NE_EXPR || code == EQ_EXPR)
6477 && integer_zerop (arg1)
6478 && ! TREE_UNSIGNED (TREE_TYPE (arg0))
6479 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
6480 || TREE_CODE (arg0) == CEIL_MOD_EXPR
6481 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
79bf94d3
RK
6482 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
6483 && integer_pow2p (TREE_OPERAND (arg0, 1)))
05a0d5ea
RK
6484 {
6485 tree newtype = unsigned_type (TREE_TYPE (arg0));
6486 tree newmod = build (TREE_CODE (arg0), newtype,
6487 convert (newtype, TREE_OPERAND (arg0, 0)),
6488 convert (newtype, TREE_OPERAND (arg0, 1)));
6489
6490 return build (code, type, newmod, convert (newtype, arg1));
6491 }
6492
6d716ca8
RS
6493 /* If this is an NE comparison of zero with an AND of one, remove the
6494 comparison since the AND will give the correct value. */
6495 if (code == NE_EXPR && integer_zerop (arg1)
6496 && TREE_CODE (arg0) == BIT_AND_EXPR
6497 && integer_onep (TREE_OPERAND (arg0, 1)))
6498 return convert (type, arg0);
6499
6500 /* If we have (A & C) == C where C is a power of 2, convert this into
6501 (A & C) != 0. Similarly for NE_EXPR. */
6502 if ((code == EQ_EXPR || code == NE_EXPR)
6503 && TREE_CODE (arg0) == BIT_AND_EXPR
6504 && integer_pow2p (TREE_OPERAND (arg0, 1))
6505 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
6506 return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
6507 arg0, integer_zero_node);
6508
e92d3048 6509 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
34b1b41f 6510 and similarly for >= into !=. */
e92d3048
RK
6511 if ((code == LT_EXPR || code == GE_EXPR)
6512 && TREE_UNSIGNED (TREE_TYPE (arg0))
6513 && TREE_CODE (arg1) == LSHIFT_EXPR
6514 && integer_onep (TREE_OPERAND (arg1, 0)))
b6cc0a72 6515 return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
e92d3048
RK
6516 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6517 TREE_OPERAND (arg1, 1)),
6518 convert (TREE_TYPE (arg0), integer_zero_node));
6519
6520 else if ((code == LT_EXPR || code == GE_EXPR)
6521 && TREE_UNSIGNED (TREE_TYPE (arg0))
6522 && (TREE_CODE (arg1) == NOP_EXPR
6523 || TREE_CODE (arg1) == CONVERT_EXPR)
6524 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
6525 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
6526 return
6527 build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
6528 convert (TREE_TYPE (arg0),
6529 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6530 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))),
6531 convert (TREE_TYPE (arg0), integer_zero_node));
6532
c05a9b68
RS
6533 /* Simplify comparison of something with itself. (For IEEE
6534 floating-point, we can only do some of these simplifications.) */
6535 if (operand_equal_p (arg0, arg1, 0))
6d716ca8
RS
6536 {
6537 switch (code)
6538 {
6539 case EQ_EXPR:
6540 case GE_EXPR:
6541 case LE_EXPR:
1f2f8b29 6542 if (! FLOAT_TYPE_P (TREE_TYPE (arg0)))
f628873f 6543 return constant_boolean_node (1, type);
c05a9b68
RS
6544 code = EQ_EXPR;
6545 TREE_SET_CODE (t, code);
6546 break;
6547
6d716ca8 6548 case NE_EXPR:
c05a9b68 6549 /* For NE, we can only do this simplification if integer. */
1f2f8b29 6550 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
c05a9b68 6551 break;
0f41302f 6552 /* ... fall through ... */
6d716ca8
RS
6553 case GT_EXPR:
6554 case LT_EXPR:
f628873f 6555 return constant_boolean_node (0, type);
e9a25f70
JL
6556 default:
6557 abort ();
6d716ca8
RS
6558 }
6559 }
6560
6561 /* An unsigned comparison against 0 can be simplified. */
6562 if (integer_zerop (arg1)
7178e3af 6563 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
e5e809f4 6564 || POINTER_TYPE_P (TREE_TYPE (arg1)))
6d716ca8
RS
6565 && TREE_UNSIGNED (TREE_TYPE (arg1)))
6566 {
6567 switch (TREE_CODE (t))
6568 {
6569 case GT_EXPR:
c05a9b68 6570 code = NE_EXPR;
6d716ca8
RS
6571 TREE_SET_CODE (t, NE_EXPR);
6572 break;
6573 case LE_EXPR:
c05a9b68 6574 code = EQ_EXPR;
6d716ca8
RS
6575 TREE_SET_CODE (t, EQ_EXPR);
6576 break;
6577 case GE_EXPR:
56f8e5e6
RK
6578 return omit_one_operand (type,
6579 convert (type, integer_one_node),
6580 arg0);
6d716ca8 6581 case LT_EXPR:
56f8e5e6
RK
6582 return omit_one_operand (type,
6583 convert (type, integer_zero_node),
6584 arg0);
e9a25f70
JL
6585 default:
6586 break;
6d716ca8
RS
6587 }
6588 }
6589
14a774a9
RK
6590 /* Comparisons with the highest or lowest possible integer of
6591 the specified size will have known values and an unsigned
6592 <= 0x7fffffff can be simplified. */
e9a25f70 6593 {
14a774a9
RK
6594 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
6595
e9a25f70
JL
6596 if (TREE_CODE (arg1) == INTEGER_CST
6597 && ! TREE_CONSTANT_OVERFLOW (arg1)
6598 && width <= HOST_BITS_PER_WIDE_INT
e9a25f70 6599 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
14a774a9 6600 || POINTER_TYPE_P (TREE_TYPE (arg1))))
e9a25f70 6601 {
14a774a9
RK
6602 if (TREE_INT_CST_HIGH (arg1) == 0
6603 && (TREE_INT_CST_LOW (arg1)
05bccae2 6604 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1)
14a774a9
RK
6605 && ! TREE_UNSIGNED (TREE_TYPE (arg1)))
6606 switch (TREE_CODE (t))
6607 {
6608 case GT_EXPR:
6609 return omit_one_operand (type,
6610 convert (type, integer_zero_node),
6611 arg0);
6612 case GE_EXPR:
6613 TREE_SET_CODE (t, EQ_EXPR);
6614 break;
6615
6616 case LE_EXPR:
6617 return omit_one_operand (type,
6618 convert (type, integer_one_node),
6619 arg0);
6620 case LT_EXPR:
6621 TREE_SET_CODE (t, NE_EXPR);
6622 break;
6623
6624 default:
6625 break;
6626 }
6627
6628 else if (TREE_INT_CST_HIGH (arg1) == -1
6629 && (- TREE_INT_CST_LOW (arg1)
05bccae2 6630 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)))
14a774a9
RK
6631 && ! TREE_UNSIGNED (TREE_TYPE (arg1)))
6632 switch (TREE_CODE (t))
6633 {
6634 case LT_EXPR:
6635 return omit_one_operand (type,
6636 convert (type, integer_zero_node),
6637 arg0);
6638 case LE_EXPR:
6639 TREE_SET_CODE (t, EQ_EXPR);
6640 break;
6641
6642 case GE_EXPR:
6643 return omit_one_operand (type,
6644 convert (type, integer_one_node),
6645 arg0);
6646 case GT_EXPR:
6647 TREE_SET_CODE (t, NE_EXPR);
6648 break;
6649
6650 default:
6651 break;
6652 }
6653
6654 else if (TREE_INT_CST_HIGH (arg1) == 0
6655 && (TREE_INT_CST_LOW (arg1)
05bccae2 6656 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1)
d791ea1e
JJ
6657 && TREE_UNSIGNED (TREE_TYPE (arg1))
6658 /* signed_type does not work on pointer types. */
6659 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
b6cc0a72 6660
14a774a9
RK
6661 switch (TREE_CODE (t))
6662 {
6663 case LE_EXPR:
6664 return fold (build (GE_EXPR, type,
6665 convert (signed_type (TREE_TYPE (arg0)),
6666 arg0),
6667 convert (signed_type (TREE_TYPE (arg1)),
6668 integer_zero_node)));
6669 case GT_EXPR:
6670 return fold (build (LT_EXPR, type,
6671 convert (signed_type (TREE_TYPE (arg0)),
6672 arg0),
6673 convert (signed_type (TREE_TYPE (arg1)),
6674 integer_zero_node)));
6675
6676 default:
6677 break;
6678 }
e9a25f70
JL
6679 }
6680 }
6681
c05a9b68
RS
6682 /* If we are comparing an expression that just has comparisons
6683 of two integer values, arithmetic expressions of those comparisons,
6684 and constants, we can simplify it. There are only three cases
6685 to check: the two values can either be equal, the first can be
6686 greater, or the second can be greater. Fold the expression for
6687 those three values. Since each value must be 0 or 1, we have
6688 eight possibilities, each of which corresponds to the constant 0
6689 or 1 or one of the six possible comparisons.
6690
6691 This handles common cases like (a > b) == 0 but also handles
6692 expressions like ((x > y) - (y > x)) > 0, which supposedly
6693 occur in macroized code. */
6694
6695 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
6696 {
6697 tree cval1 = 0, cval2 = 0;
35e66bd1 6698 int save_p = 0;
c05a9b68 6699
35e66bd1 6700 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
c05a9b68
RS
6701 /* Don't handle degenerate cases here; they should already
6702 have been handled anyway. */
6703 && cval1 != 0 && cval2 != 0
6704 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
6705 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
7178e3af 6706 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
e1ee5cdc
RH
6707 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
6708 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
c05a9b68
RS
6709 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
6710 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
6711 {
6712 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
6713 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
6714
6715 /* We can't just pass T to eval_subst in case cval1 or cval2
6716 was the same as ARG1. */
6717
6718 tree high_result
6719 = fold (build (code, type,
6720 eval_subst (arg0, cval1, maxval, cval2, minval),
6721 arg1));
6722 tree equal_result
6723 = fold (build (code, type,
6724 eval_subst (arg0, cval1, maxval, cval2, maxval),
6725 arg1));
6726 tree low_result
6727 = fold (build (code, type,
6728 eval_subst (arg0, cval1, minval, cval2, maxval),
6729 arg1));
6730
6731 /* All three of these results should be 0 or 1. Confirm they
6732 are. Then use those values to select the proper code
6733 to use. */
6734
6735 if ((integer_zerop (high_result)
6736 || integer_onep (high_result))
6737 && (integer_zerop (equal_result)
6738 || integer_onep (equal_result))
6739 && (integer_zerop (low_result)
6740 || integer_onep (low_result)))
6741 {
6742 /* Make a 3-bit mask with the high-order bit being the
6743 value for `>', the next for '=', and the low for '<'. */
6744 switch ((integer_onep (high_result) * 4)
6745 + (integer_onep (equal_result) * 2)
6746 + integer_onep (low_result))
6747 {
6748 case 0:
6749 /* Always false. */
13837058 6750 return omit_one_operand (type, integer_zero_node, arg0);
c05a9b68
RS
6751 case 1:
6752 code = LT_EXPR;
6753 break;
6754 case 2:
6755 code = EQ_EXPR;
6756 break;
6757 case 3:
6758 code = LE_EXPR;
6759 break;
6760 case 4:
6761 code = GT_EXPR;
6762 break;
6763 case 5:
6764 code = NE_EXPR;
6765 break;
6766 case 6:
6767 code = GE_EXPR;
6768 break;
6769 case 7:
6770 /* Always true. */
13837058 6771 return omit_one_operand (type, integer_one_node, arg0);
c05a9b68
RS
6772 }
6773
35e66bd1
RK
6774 t = build (code, type, cval1, cval2);
6775 if (save_p)
6776 return save_expr (t);
6777 else
6778 return fold (t);
c05a9b68
RS
6779 }
6780 }
6781 }
6782
6783 /* If this is a comparison of a field, we may be able to simplify it. */
9c922ec7
MM
6784 if ((TREE_CODE (arg0) == COMPONENT_REF
6785 || TREE_CODE (arg0) == BIT_FIELD_REF)
6786 && (code == EQ_EXPR || code == NE_EXPR)
6787 /* Handle the constant case even without -O
6788 to make sure the warnings are given. */
6789 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
6790 {
6791 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
6792 return t1 ? t1 : t;
6793 }
c05a9b68 6794
9ec36da5
JL
6795 /* If this is a comparison of complex values and either or both sides
6796 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
6797 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
6798 This may prevent needless evaluations. */
95aa28ae
RK
6799 if ((code == EQ_EXPR || code == NE_EXPR)
6800 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
6801 && (TREE_CODE (arg0) == COMPLEX_EXPR
9ec36da5
JL
6802 || TREE_CODE (arg1) == COMPLEX_EXPR
6803 || TREE_CODE (arg0) == COMPLEX_CST
6804 || TREE_CODE (arg1) == COMPLEX_CST))
95aa28ae
RK
6805 {
6806 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
f0b8d9aa
AS
6807 tree real0, imag0, real1, imag1;
6808
6809 arg0 = save_expr (arg0);
6810 arg1 = save_expr (arg1);
6811 real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
6812 imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
6813 real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
6814 imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
95aa28ae
RK
6815
6816 return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
6817 : TRUTH_ORIF_EXPR),
6818 type,
6819 fold (build (code, type, real0, real1)),
6820 fold (build (code, type, imag0, imag1))));
6821 }
6822
c05a9b68
RS
6823 /* From here on, the only cases we handle are when the result is
6824 known to be a constant.
6825
6826 To compute GT, swap the arguments and do LT.
6d716ca8
RS
6827 To compute GE, do LT and invert the result.
6828 To compute LE, swap the arguments, do LT and invert the result.
c05a9b68
RS
6829 To compute NE, do EQ and invert the result.
6830
6831 Therefore, the code below must handle only EQ and LT. */
6832
6d716ca8
RS
6833 if (code == LE_EXPR || code == GT_EXPR)
6834 {
c05a9b68
RS
6835 tem = arg0, arg0 = arg1, arg1 = tem;
6836 code = swap_tree_comparison (code);
6837 }
6838
6839 /* Note that it is safe to invert for real values here because we
6840 will check below in the one case that it matters. */
6841
14a774a9 6842 t1 = NULL_TREE;
c05a9b68
RS
6843 invert = 0;
6844 if (code == NE_EXPR || code == GE_EXPR)
6845 {
6846 invert = 1;
6847 code = invert_tree_comparison (code);
6d716ca8
RS
6848 }
6849
6850 /* Compute a result for LT or EQ if args permit;
6851 otherwise return T. */
c05a9b68 6852 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
6d716ca8 6853 {
c05a9b68 6854 if (code == EQ_EXPR)
05bccae2 6855 t1 = build_int_2 (tree_int_cst_equal (arg0, arg1), 0);
6d716ca8 6856 else
c05a9b68
RS
6857 t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
6858 ? INT_CST_LT_UNSIGNED (arg0, arg1)
6859 : INT_CST_LT (arg0, arg1)),
6860 0);
6d716ca8 6861 }
c05a9b68 6862
e80c9ccb 6863#if 0 /* This is no longer useful, but breaks some real code. */
6d716ca8
RS
6864 /* Assume a nonexplicit constant cannot equal an explicit one,
6865 since such code would be undefined anyway.
6866 Exception: on sysvr4, using #pragma weak,
6867 a label can come out as 0. */
6868 else if (TREE_CODE (arg1) == INTEGER_CST
6869 && !integer_zerop (arg1)
6870 && TREE_CONSTANT (arg0)
6871 && TREE_CODE (arg0) == ADDR_EXPR
c05a9b68
RS
6872 && code == EQ_EXPR)
6873 t1 = build_int_2 (0, 0);
e80c9ccb 6874#endif
6d716ca8 6875 /* Two real constants can be compared explicitly. */
c05a9b68 6876 else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
6d716ca8 6877 {
c05a9b68
RS
6878 /* If either operand is a NaN, the result is false with two
6879 exceptions: First, an NE_EXPR is true on NaNs, but that case
6880 is already handled correctly since we will be inverting the
6881 result for NE_EXPR. Second, if we had inverted a LE_EXPR
6882 or a GE_EXPR into a LT_EXPR, we must return true so that it
6883 will be inverted into false. */
6884
6885 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
6886 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
6887 t1 = build_int_2 (invert && code == LT_EXPR, 0);
6888
6889 else if (code == EQ_EXPR)
6890 t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
6891 TREE_REAL_CST (arg1)),
6892 0);
6d716ca8 6893 else
c05a9b68
RS
6894 t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
6895 TREE_REAL_CST (arg1)),
6896 0);
6d716ca8
RS
6897 }
6898
c05a9b68
RS
6899 if (t1 == NULL_TREE)
6900 return t;
6901
6902 if (invert)
6903 TREE_INT_CST_LOW (t1) ^= 1;
6904
6905 TREE_TYPE (t1) = type;
c651e1e0
MM
6906 if (TREE_CODE (type) == BOOLEAN_TYPE)
6907 return truthvalue_conversion (t1);
c05a9b68 6908 return t1;
6d716ca8
RS
6909
6910 case COND_EXPR:
a5e9b124
JW
6911 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
6912 so all simple results must be passed through pedantic_non_lvalue. */
6d716ca8 6913 if (TREE_CODE (arg0) == INTEGER_CST)
a5e9b124
JW
6914 return pedantic_non_lvalue
6915 (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
6d716ca8 6916 else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
4ab3cb65 6917 return pedantic_omit_one_operand (type, arg1, arg0);
6d716ca8 6918
c05a9b68
RS
6919 /* If the second operand is zero, invert the comparison and swap
6920 the second and third operands. Likewise if the second operand
6921 is constant and the third is not or if the third operand is
6922 equivalent to the first operand of the comparison. */
6d716ca8 6923
c05a9b68
RS
6924 if (integer_zerop (arg1)
6925 || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
6926 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
6927 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
6928 TREE_OPERAND (t, 2),
6929 TREE_OPERAND (arg0, 1))))
6930 {
6931 /* See if this can be inverted. If it can't, possibly because
6932 it was a floating-point inequality comparison, don't do
6933 anything. */
6934 tem = invert_truthvalue (arg0);
6d716ca8 6935
c05a9b68
RS
6936 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
6937 {
cd7ece66
RK
6938 t = build (code, type, tem,
6939 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
6940 arg0 = tem;
a58bd508
MT
6941 /* arg1 should be the first argument of the new T. */
6942 arg1 = TREE_OPERAND (t, 1);
3a96b5eb 6943 STRIP_NOPS (arg1);
c05a9b68
RS
6944 }
6945 }
6d716ca8 6946
c05a9b68
RS
6947 /* If we have A op B ? A : C, we may be able to convert this to a
6948 simpler expression, depending on the operation and the values
b5c52586
RS
6949 of B and C. IEEE floating point prevents this though,
6950 because A or B might be -0.0 or a NaN. */
6d716ca8 6951
c05a9b68 6952 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
b5c52586 6953 && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
fab446b8 6954 || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
de6c5979 6955 || flag_unsafe_math_optimizations)
c05a9b68
RS
6956 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
6957 arg1, TREE_OPERAND (arg0, 1)))
6d716ca8 6958 {
c05a9b68
RS
6959 tree arg2 = TREE_OPERAND (t, 2);
6960 enum tree_code comp_code = TREE_CODE (arg0);
6961
3a96b5eb
RK
6962 STRIP_NOPS (arg2);
6963
c05a9b68
RS
6964 /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
6965 depending on the comparison operation. */
68c6b3a9
DE
6966 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
6967 ? real_zerop (TREE_OPERAND (arg0, 1))
6968 : integer_zerop (TREE_OPERAND (arg0, 1)))
c05a9b68
RS
6969 && TREE_CODE (arg2) == NEGATE_EXPR
6970 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6971 switch (comp_code)
6972 {
6973 case EQ_EXPR:
c9869b75 6974 return
b8fbe62c
RK
6975 pedantic_non_lvalue
6976 (convert (type,
6977 negate_expr
6978 (convert (TREE_TYPE (TREE_OPERAND (t, 1)),
6979 arg1))));
6980
c05a9b68 6981 case NE_EXPR:
a5e9b124 6982 return pedantic_non_lvalue (convert (type, arg1));
c05a9b68
RS
6983 case GE_EXPR:
6984 case GT_EXPR:
899f1ed6
RH
6985 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
6986 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
a5e9b124 6987 return pedantic_non_lvalue
21403f14
RK
6988 (convert (type, fold (build1 (ABS_EXPR,
6989 TREE_TYPE (arg1), arg1))));
c05a9b68
RS
6990 case LE_EXPR:
6991 case LT_EXPR:
899f1ed6
RH
6992 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
6993 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
a5e9b124 6994 return pedantic_non_lvalue
1baa375f
RK
6995 (negate_expr (convert (type,
6996 fold (build1 (ABS_EXPR,
6997 TREE_TYPE (arg1),
6998 arg1)))));
e9a25f70
JL
6999 default:
7000 abort ();
c05a9b68 7001 }
6d716ca8 7002
c05a9b68
RS
7003 /* If this is A != 0 ? A : 0, this is simply A. For ==, it is
7004 always zero. */
6d716ca8 7005
29ebe69a 7006 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
c05a9b68
RS
7007 {
7008 if (comp_code == NE_EXPR)
a5e9b124 7009 return pedantic_non_lvalue (convert (type, arg1));
c05a9b68 7010 else if (comp_code == EQ_EXPR)
a5e9b124 7011 return pedantic_non_lvalue (convert (type, integer_zero_node));
c05a9b68
RS
7012 }
7013
7014 /* If this is A op B ? A : B, this is either A, B, min (A, B),
7015 or max (A, B), depending on the operation. */
7016
7017 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
7018 arg2, TREE_OPERAND (arg0, 0)))
3a96b5eb
RK
7019 {
7020 tree comp_op0 = TREE_OPERAND (arg0, 0);
7021 tree comp_op1 = TREE_OPERAND (arg0, 1);
7022 tree comp_type = TREE_TYPE (comp_op0);
7023
a7833bec
JM
7024 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
7025 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
7026 comp_type = type;
7027
3a96b5eb
RK
7028 switch (comp_code)
7029 {
7030 case EQ_EXPR:
7031 return pedantic_non_lvalue (convert (type, arg2));
7032 case NE_EXPR:
7033 return pedantic_non_lvalue (convert (type, arg1));
7034 case LE_EXPR:
7035 case LT_EXPR:
e9a25f70
JL
7036 /* In C++ a ?: expression can be an lvalue, so put the
7037 operand which will be used if they are equal first
b6cc0a72 7038 so that we can convert this back to the
e9a25f70
JL
7039 corresponding COND_EXPR. */
7040 return pedantic_non_lvalue
792a328b
JM
7041 (convert (type, fold (build (MIN_EXPR, comp_type,
7042 (comp_code == LE_EXPR
7043 ? comp_op0 : comp_op1),
7044 (comp_code == LE_EXPR
7045 ? comp_op1 : comp_op0)))));
88a5cdb8 7046 break;
3a96b5eb
RK
7047 case GE_EXPR:
7048 case GT_EXPR:
e9a25f70
JL
7049 return pedantic_non_lvalue
7050 (convert (type, fold (build (MAX_EXPR, comp_type,
7051 (comp_code == GE_EXPR
7052 ? comp_op0 : comp_op1),
7053 (comp_code == GE_EXPR
7054 ? comp_op1 : comp_op0)))));
88a5cdb8 7055 break;
e9a25f70
JL
7056 default:
7057 abort ();
3a96b5eb
RK
7058 }
7059 }
c05a9b68
RS
7060
7061 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
7062 we might still be able to simplify this. For example,
7063 if C1 is one less or one more than C2, this might have started
53d2fb4f 7064 out as a MIN or MAX and been transformed by this function.
7178e3af 7065 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
c05a9b68 7066
7178e3af 7067 if (INTEGRAL_TYPE_P (type)
53d2fb4f 7068 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
c05a9b68
RS
7069 && TREE_CODE (arg2) == INTEGER_CST)
7070 switch (comp_code)
7071 {
7072 case EQ_EXPR:
7073 /* We can replace A with C1 in this case. */
cd7ece66
RK
7074 arg1 = convert (type, TREE_OPERAND (arg0, 1));
7075 t = build (code, type, TREE_OPERAND (t, 0), arg1,
7076 TREE_OPERAND (t, 2));
c05a9b68
RS
7077 break;
7078
7079 case LT_EXPR:
7080 /* If C1 is C2 + 1, this is min(A, C2). */
7081 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
7082 && operand_equal_p (TREE_OPERAND (arg0, 1),
7083 const_binop (PLUS_EXPR, arg2,
91d33e36 7084 integer_one_node, 0), 1))
a5e9b124
JW
7085 return pedantic_non_lvalue
7086 (fold (build (MIN_EXPR, type, arg1, arg2)));
c05a9b68
RS
7087 break;
7088
7089 case LE_EXPR:
7090 /* If C1 is C2 - 1, this is min(A, C2). */
7091 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
7092 && operand_equal_p (TREE_OPERAND (arg0, 1),
7093 const_binop (MINUS_EXPR, arg2,
91d33e36 7094 integer_one_node, 0), 1))
a5e9b124
JW
7095 return pedantic_non_lvalue
7096 (fold (build (MIN_EXPR, type, arg1, arg2)));
c05a9b68
RS
7097 break;
7098
7099 case GT_EXPR:
7100 /* If C1 is C2 - 1, this is max(A, C2). */
7101 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
7102 && operand_equal_p (TREE_OPERAND (arg0, 1),
7103 const_binop (MINUS_EXPR, arg2,
91d33e36 7104 integer_one_node, 0), 1))
a5e9b124
JW
7105 return pedantic_non_lvalue
7106 (fold (build (MAX_EXPR, type, arg1, arg2)));
c05a9b68
RS
7107 break;
7108
7109 case GE_EXPR:
7110 /* If C1 is C2 + 1, this is max(A, C2). */
7111 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
7112 && operand_equal_p (TREE_OPERAND (arg0, 1),
7113 const_binop (PLUS_EXPR, arg2,
91d33e36 7114 integer_one_node, 0), 1))
a5e9b124
JW
7115 return pedantic_non_lvalue
7116 (fold (build (MAX_EXPR, type, arg1, arg2)));
c05a9b68 7117 break;
e9a25f70
JL
7118 case NE_EXPR:
7119 break;
7120 default:
7121 abort ();
c05a9b68 7122 }
6d716ca8
RS
7123 }
7124
e1f56f62
RK
7125 /* If the second operand is simpler than the third, swap them
7126 since that produces better jump optimization results. */
2f939d94 7127 if ((TREE_CONSTANT (arg1) || DECL_P (arg1)
e1f56f62
RK
7128 || TREE_CODE (arg1) == SAVE_EXPR)
7129 && ! (TREE_CONSTANT (TREE_OPERAND (t, 2))
2f939d94 7130 || DECL_P (TREE_OPERAND (t, 2))
e1f56f62
RK
7131 || TREE_CODE (TREE_OPERAND (t, 2)) == SAVE_EXPR))
7132 {
7133 /* See if this can be inverted. If it can't, possibly because
7134 it was a floating-point inequality comparison, don't do
7135 anything. */
7136 tem = invert_truthvalue (arg0);
7137
7138 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
7139 {
cd7ece66
RK
7140 t = build (code, type, tem,
7141 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
7142 arg0 = tem;
c479c2ac
JL
7143 /* arg1 should be the first argument of the new T. */
7144 arg1 = TREE_OPERAND (t, 1);
3a96b5eb 7145 STRIP_NOPS (arg1);
e1f56f62
RK
7146 }
7147 }
7148
c05a9b68
RS
7149 /* Convert A ? 1 : 0 to simply A. */
7150 if (integer_onep (TREE_OPERAND (t, 1))
7151 && integer_zerop (TREE_OPERAND (t, 2))
7152 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
b6cc0a72 7153 call to fold will try to move the conversion inside
c05a9b68
RS
7154 a COND, which will recurse. In that case, the COND_EXPR
7155 is probably the best choice, so leave it alone. */
7156 && type == TREE_TYPE (arg0))
a5e9b124 7157 return pedantic_non_lvalue (arg0);
6d716ca8 7158
c05a9b68
RS
7159 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
7160 operation is simply A & 2. */
6d716ca8
RS
7161
7162 if (integer_zerop (TREE_OPERAND (t, 2))
7163 && TREE_CODE (arg0) == NE_EXPR
7164 && integer_zerop (TREE_OPERAND (arg0, 1))
c05a9b68
RS
7165 && integer_pow2p (arg1)
7166 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
7167 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
7168 arg1, 1))
a5e9b124 7169 return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
6d716ca8 7170
6d716ca8
RS
7171 return t;
7172
7173 case COMPOUND_EXPR:
b7647895
JW
7174 /* When pedantic, a compound expression can be neither an lvalue
7175 nor an integer constant expression. */
7176 if (TREE_SIDE_EFFECTS (arg0) || pedantic)
d023bff9
RS
7177 return t;
7178 /* Don't let (0, 0) be null pointer constant. */
7179 if (integer_zerop (arg1))
13eb1f7f
RK
7180 return build1 (NOP_EXPR, type, arg1);
7181 return convert (type, arg1);
6d716ca8 7182
1cc1b11a
RS
7183 case COMPLEX_EXPR:
7184 if (wins)
214d5b84 7185 return build_complex (type, arg0, arg1);
1cc1b11a
RS
7186 return t;
7187
7188 case REALPART_EXPR:
a333b79f 7189 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
1cc1b11a
RS
7190 return t;
7191 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7192 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
7193 TREE_OPERAND (arg0, 1));
7194 else if (TREE_CODE (arg0) == COMPLEX_CST)
7195 return TREE_REALPART (arg0);
7196 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
a3279355
RK
7197 return fold (build (TREE_CODE (arg0), type,
7198 fold (build1 (REALPART_EXPR, type,
7199 TREE_OPERAND (arg0, 0))),
7200 fold (build1 (REALPART_EXPR,
7201 type, TREE_OPERAND (arg0, 1)))));
1cc1b11a
RS
7202 return t;
7203
7204 case IMAGPART_EXPR:
a333b79f 7205 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
1cc1b11a
RS
7206 return convert (type, integer_zero_node);
7207 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7208 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
7209 TREE_OPERAND (arg0, 0));
7210 else if (TREE_CODE (arg0) == COMPLEX_CST)
7211 return TREE_IMAGPART (arg0);
7212 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
a3279355
RK
7213 return fold (build (TREE_CODE (arg0), type,
7214 fold (build1 (IMAGPART_EXPR, type,
7215 TREE_OPERAND (arg0, 0))),
7216 fold (build1 (IMAGPART_EXPR, type,
7217 TREE_OPERAND (arg0, 1)))));
1cc1b11a
RS
7218 return t;
7219
56c98e5b
JM
7220 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
7221 appropriate. */
7222 case CLEANUP_POINT_EXPR:
b7f6588d 7223 if (! has_cleanups (arg0))
3ffcb234 7224 return TREE_OPERAND (t, 0);
56c98e5b
JM
7225
7226 {
7227 enum tree_code code0 = TREE_CODE (arg0);
7228 int kind0 = TREE_CODE_CLASS (code0);
7229 tree arg00 = TREE_OPERAND (arg0, 0);
7230 tree arg01;
7231
0982a4b8 7232 if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
b6cc0a72 7233 return fold (build1 (code0, type,
56c98e5b
JM
7234 fold (build1 (CLEANUP_POINT_EXPR,
7235 TREE_TYPE (arg00), arg00))));
0982a4b8
JM
7236
7237 if (kind0 == '<' || kind0 == '2'
7238 || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
7239 || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR
7240 || code0 == TRUTH_XOR_EXPR)
7241 {
7242 arg01 = TREE_OPERAND (arg0, 1);
7243
b7f6588d
JM
7244 if (TREE_CONSTANT (arg00)
7245 || ((code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR)
7246 && ! has_cleanups (arg00)))
0982a4b8
JM
7247 return fold (build (code0, type, arg00,
7248 fold (build1 (CLEANUP_POINT_EXPR,
7249 TREE_TYPE (arg01), arg01))));
7250
b7f6588d 7251 if (TREE_CONSTANT (arg01))
0982a4b8
JM
7252 return fold (build (code0, type,
7253 fold (build1 (CLEANUP_POINT_EXPR,
7254 TREE_TYPE (arg00), arg00)),
7255 arg01));
7256 }
56c98e5b
JM
7257
7258 return t;
7259 }
7260
b0b3afb2
BS
7261 case CALL_EXPR:
7262 /* Check for a built-in function. */
7263 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR
7264 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (expr, 0), 0))
7265 == FUNCTION_DECL)
7266 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (expr, 0), 0)))
7267 {
7268 tree tmp = fold_builtin (expr);
7269 if (tmp)
7270 return tmp;
7271 }
7272 return t;
7273
6d716ca8
RS
7274 default:
7275 return t;
7276 } /* switch (code) */
7277}
39dfb55a 7278
c5c76735
JL
7279/* Determine if first argument is a multiple of second argument. Return 0 if
7280 it is not, or we cannot easily determined it to be.
39dfb55a 7281
c5c76735
JL
7282 An example of the sort of thing we care about (at this point; this routine
7283 could surely be made more general, and expanded to do what the *_DIV_EXPR's
7284 fold cases do now) is discovering that
39dfb55a
JL
7285
7286 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7287
7288 is a multiple of
7289
7290 SAVE_EXPR (J * 8)
7291
c5c76735 7292 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
39dfb55a
JL
7293
7294 This code also handles discovering that
7295
7296 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7297
c5c76735 7298 is a multiple of 8 so we don't have to worry about dealing with a
39dfb55a
JL
7299 possible remainder.
7300
c5c76735
JL
7301 Note that we *look* inside a SAVE_EXPR only to determine how it was
7302 calculated; it is not safe for fold to do much of anything else with the
7303 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
7304 at run time. For example, the latter example above *cannot* be implemented
7305 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
7306 evaluation time of the original SAVE_EXPR is not necessarily the same at
7307 the time the new expression is evaluated. The only optimization of this
39dfb55a
JL
7308 sort that would be valid is changing
7309
7310 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
39dfb55a 7311
c5c76735 7312 divided by 8 to
39dfb55a
JL
7313
7314 SAVE_EXPR (I) * SAVE_EXPR (J)
7315
7316 (where the same SAVE_EXPR (J) is used in the original and the
7317 transformed version). */
7318
7319static int
7320multiple_of_p (type, top, bottom)
7321 tree type;
7322 tree top;
7323 tree bottom;
7324{
7325 if (operand_equal_p (top, bottom, 0))
7326 return 1;
7327
7328 if (TREE_CODE (type) != INTEGER_TYPE)
7329 return 0;
7330
7331 switch (TREE_CODE (top))
7332 {
7333 case MULT_EXPR:
7334 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7335 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7336
7337 case PLUS_EXPR:
7338 case MINUS_EXPR:
7339 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7340 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7341
fba2c0cd
JJ
7342 case LSHIFT_EXPR:
7343 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
7344 {
7345 tree op1, t1;
7346
7347 op1 = TREE_OPERAND (top, 1);
7348 /* const_binop may not detect overflow correctly,
7349 so check for it explicitly here. */
7350 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
7351 > TREE_INT_CST_LOW (op1)
7352 && TREE_INT_CST_HIGH (op1) == 0
7353 && 0 != (t1 = convert (type,
7354 const_binop (LSHIFT_EXPR, size_one_node,
7355 op1, 0)))
7356 && ! TREE_OVERFLOW (t1))
7357 return multiple_of_p (type, t1, bottom);
7358 }
7359 return 0;
7360
39dfb55a 7361 case NOP_EXPR:
c5c76735 7362 /* Can't handle conversions from non-integral or wider integral type. */
39dfb55a
JL
7363 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
7364 || (TYPE_PRECISION (type)
7365 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
7366 return 0;
c5c76735 7367
30f7a378 7368 /* .. fall through ... */
c5c76735 7369
39dfb55a
JL
7370 case SAVE_EXPR:
7371 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
7372
7373 case INTEGER_CST:
fba2c0cd
JJ
7374 if (TREE_CODE (bottom) != INTEGER_CST
7375 || (TREE_UNSIGNED (type)
7376 && (tree_int_cst_sgn (top) < 0
7377 || tree_int_cst_sgn (bottom) < 0)))
39dfb55a
JL
7378 return 0;
7379 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
7380 top, bottom, 0));
7381
7382 default:
7383 return 0;
7384 }
7385}
a36556a8
ZW
7386
7387/* Return true if `t' is known to be non-negative. */
7388
7389int
7390tree_expr_nonnegative_p (t)
7391 tree t;
7392{
7393 switch (TREE_CODE (t))
7394 {
88e3805d
KG
7395 case ABS_EXPR:
7396 case FFS_EXPR:
7397 return 1;
a36556a8
ZW
7398 case INTEGER_CST:
7399 return tree_int_cst_sgn (t) >= 0;
7400 case COND_EXPR:
7401 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
7402 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
88e3805d
KG
7403 case COMPOUND_EXPR:
7404 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7405 case MIN_EXPR:
7406 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
7407 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7408 case MAX_EXPR:
7409 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
7410 || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7411 case MODIFY_EXPR:
7412 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
a36556a8
ZW
7413 case BIND_EXPR:
7414 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7415 case RTL_EXPR:
7416 return rtl_expr_nonnegative_p (RTL_EXPR_RTL (t));
7417
7418 default:
9d2dc7da
KG
7419 if (truth_value_p (TREE_CODE (t)))
7420 /* Truth values evaluate to 0 or 1, which is nonnegative. */
7421 return 1;
7422 else
7423 /* We don't know sign of `t', so be conservative and return false. */
7424 return 0;
a36556a8
ZW
7425 }
7426}
7427
7428/* Return true if `r' is known to be non-negative.
7429 Only handles constants at the moment. */
7430
7431int
7432rtl_expr_nonnegative_p (r)
7433 rtx r;
7434{
7435 switch (GET_CODE (r))
7436 {
7437 case CONST_INT:
7438 return INTVAL (r) >= 0;
7439
7440 case CONST_DOUBLE:
7441 if (GET_MODE (r) == VOIDmode)
7442 return CONST_DOUBLE_HIGH (r) >= 0;
7443 return 0;
7444
7445 case SYMBOL_REF:
7446 case LABEL_REF:
7447 /* These are always nonnegative. */
7448 return 1;
7449
7450 default:
7451 return 0;
7452 }
7453}