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