]> gcc.gnu.org Git - gcc.git/blame - gcc/fold-const.c
(left_shift_overflows, same_sign, overflow_sum_sign): New macros.
[gcc.git] / gcc / fold-const.c
CommitLineData
6d716ca8
RS
1/* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987, 1988, 1992 Free Software Foundation, Inc.
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20/*@@ Fix lossage on folding division of big integers. */
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
31/* The entry points in this file are fold, size_int and size_binop.
32
33 fold takes a tree as argument and returns a simplified tree.
34
35 size_binop takes a tree code for an arithmetic operation
36 and two operands that are trees, and produces a tree for the
37 result, assuming the type comes from `sizetype'.
38
39 size_int takes an integer value, and creates a tree constant
40 with type from `sizetype'. */
41
42#include <stdio.h>
43#include <setjmp.h>
44#include "config.h"
45#include "flags.h"
46#include "tree.h"
47
7c7b029d
RS
48/* Handle floating overflow for `const_binop'. */
49static jmp_buf float_error;
50
fe3e8e40 51int lshift_double ();
6d716ca8
RS
52void rshift_double ();
53void lrotate_double ();
54void rrotate_double ();
55static tree const_binop ();
d35357ed
RS
56
57#ifndef BRANCH_COST
58#define BRANCH_COST 1
59#endif
fe3e8e40
RS
60
61/* Yield nonzero if a signed left shift of A by B bits overflows. */
62#define left_shift_overflows(a, b) ((a) != ((a) << (b)) >> (b))
63
64/* Yield nonzero if A and B have the same sign. */
65#define same_sign(a, b) ((a) ^ (b) >= 0)
66
67/* Suppose A1 + B1 = SUM1, using 2's complement arithmetic ignoring overflow.
68 Suppose A, B and SUM have the same respective signs as A1, B1, and SUM1.
69 Then this yields nonzero if overflow occurred during the addition.
70 Overflow occurs if A and B have the same sign, but A and SUM differ in sign.
71 Use `^' to test whether signs differ, and `< 0' to isolate the sign. */
72#define overflow_sum_sign(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
6d716ca8 73\f
906c4e36
RK
74/* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
75 We do that by representing the two-word integer as MAX_SHORTS shorts,
6d716ca8
RS
76 with only 8 bits stored in each short, as a positive number. */
77
906c4e36
RK
78/* Unpack a two-word integer into MAX_SHORTS shorts.
79 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
6d716ca8
RS
80 SHORTS points to the array of shorts. */
81
82static void
83encode (shorts, low, hi)
84 short *shorts;
906c4e36 85 HOST_WIDE_INT low, hi;
6d716ca8 86{
906c4e36
RK
87 register int i;
88
89 for (i = 0; i < MAX_SHORTS / 2; i++)
90 {
91 shorts[i] = (low >> (i * 8)) & 0xff;
92 shorts[i + MAX_SHORTS / 2] = (hi >> (i * 8) & 0xff);
93 }
6d716ca8
RS
94}
95
906c4e36 96/* Pack an array of MAX_SHORTS shorts into a two-word integer.
6d716ca8 97 SHORTS points to the array of shorts.
906c4e36 98 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
6d716ca8
RS
99
100static void
101decode (shorts, low, hi)
102 short *shorts;
906c4e36 103 HOST_WIDE_INT *low, *hi;
6d716ca8 104{
906c4e36
RK
105 register int i;
106 HOST_WIDE_INT lv = 0, hv = 0;
107
108 for (i = 0; i < MAX_SHORTS / 2; i++)
109 {
110 lv |= (HOST_WIDE_INT) shorts[i] << (i * 8);
111 hv |= (HOST_WIDE_INT) shorts[i + MAX_SHORTS / 2] << (i * 8);
112 }
113
114 *low = lv, *hi = hv;
6d716ca8
RS
115}
116\f
117/* Make the integer constant T valid for its type
118 by setting to 0 or 1 all the bits in the constant
119 that don't belong in the type. */
120
121static void
122force_fit_type (t)
123 tree t;
124{
125 register int prec = TYPE_PRECISION (TREE_TYPE (t));
126
127 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
128 prec = POINTER_SIZE;
129
130 /* First clear all bits that are beyond the type's precision. */
131
906c4e36 132 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
6d716ca8 133 ;
906c4e36 134 else if (prec > HOST_BITS_PER_WIDE_INT)
6d716ca8
RS
135 {
136 TREE_INT_CST_HIGH (t)
906c4e36 137 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
6d716ca8
RS
138 }
139 else
140 {
141 TREE_INT_CST_HIGH (t) = 0;
906c4e36
RK
142 if (prec < HOST_BITS_PER_WIDE_INT)
143 TREE_INT_CST_LOW (t) &= ~((HOST_WIDE_INT) (-1) << prec);
6d716ca8
RS
144 }
145
146 /* If it's a signed type and value's sign bit is set, extend the sign. */
147
148 if (! TREE_UNSIGNED (TREE_TYPE (t))
906c4e36
RK
149 && prec != 2 * HOST_BITS_PER_WIDE_INT
150 && (prec > HOST_BITS_PER_WIDE_INT
151 ? (TREE_INT_CST_HIGH (t)
152 & ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
153 : TREE_INT_CST_LOW (t) & ((HOST_WIDE_INT) 1 << (prec - 1))))
6d716ca8
RS
154 {
155 /* Value is negative:
156 set to 1 all the bits that are outside this type's precision. */
906c4e36 157 if (prec > HOST_BITS_PER_WIDE_INT)
6d716ca8
RS
158 {
159 TREE_INT_CST_HIGH (t)
906c4e36 160 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
6d716ca8
RS
161 }
162 else
163 {
164 TREE_INT_CST_HIGH (t) = -1;
906c4e36
RK
165 if (prec < HOST_BITS_PER_WIDE_INT)
166 TREE_INT_CST_LOW (t) |= ((HOST_WIDE_INT) (-1) << prec);
6d716ca8
RS
167 }
168 }
169}
170\f
906c4e36
RK
171/* Add two doubleword integers with doubleword result.
172 Each argument is given as two `HOST_WIDE_INT' pieces.
6d716ca8 173 One argument is L1 and H1; the other, L2 and H2.
906c4e36 174 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.
6d716ca8
RS
175 We use the 8-shorts representation internally. */
176
fe3e8e40 177int
6d716ca8 178add_double (l1, h1, l2, h2, lv, hv)
906c4e36
RK
179 HOST_WIDE_INT l1, h1, l2, h2;
180 HOST_WIDE_INT *lv, *hv;
6d716ca8 181{
906c4e36
RK
182 short arg1[MAX_SHORTS];
183 short arg2[MAX_SHORTS];
6d716ca8
RS
184 register int carry = 0;
185 register int i;
186
187 encode (arg1, l1, h1);
188 encode (arg2, l2, h2);
189
906c4e36 190 for (i = 0; i < MAX_SHORTS; i++)
6d716ca8
RS
191 {
192 carry += arg1[i] + arg2[i];
193 arg1[i] = carry & 0xff;
194 carry >>= 8;
195 }
196
197 decode (arg1, lv, hv);
fe3e8e40 198 return overflow_sum_sign (h1, h2, *hv);
6d716ca8
RS
199}
200
906c4e36 201/* Negate a doubleword integer with doubleword result.
fe3e8e40 202 Return nonzero if the operation overflows, assuming it's signed.
906c4e36
RK
203 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
204 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.
6d716ca8
RS
205 We use the 8-shorts representation internally. */
206
fe3e8e40 207int
6d716ca8 208neg_double (l1, h1, lv, hv)
906c4e36
RK
209 HOST_WIDE_INT l1, h1;
210 HOST_WIDE_INT *lv, *hv;
6d716ca8
RS
211{
212 if (l1 == 0)
213 {
214 *lv = 0;
215 *hv = - h1;
fe3e8e40 216 return same_sign (h1, *hv);
6d716ca8
RS
217 }
218 else
219 {
220 *lv = - l1;
221 *hv = ~ h1;
fe3e8e40 222 return 0;
6d716ca8
RS
223 }
224}
225\f
906c4e36 226/* Multiply two doubleword integers with doubleword result.
fe3e8e40 227 Return nonzero if the operation overflows, assuming it's signed.
906c4e36 228 Each argument is given as two `HOST_WIDE_INT' pieces.
6d716ca8 229 One argument is L1 and H1; the other, L2 and H2.
906c4e36 230 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.
6d716ca8
RS
231 We use the 8-shorts representation internally. */
232
fe3e8e40 233int
6d716ca8 234mul_double (l1, h1, l2, h2, lv, hv)
906c4e36
RK
235 HOST_WIDE_INT l1, h1, l2, h2;
236 HOST_WIDE_INT *lv, *hv;
6d716ca8 237{
906c4e36
RK
238 short arg1[MAX_SHORTS];
239 short arg2[MAX_SHORTS];
240 short prod[MAX_SHORTS * 2];
6d716ca8
RS
241 register int carry = 0;
242 register int i, j, k;
fe3e8e40 243 HOST_WIDE_INT toplow, tophigh, neglow, neghigh;
6d716ca8 244
fe3e8e40 245 /* These cases are used extensively, arising from pointer combinations. */
6d716ca8
RS
246 if (h2 == 0)
247 {
248 if (l2 == 2)
249 {
fe3e8e40 250 int overflow = left_shift_overflows (h1, 1);
906c4e36 251 unsigned HOST_WIDE_INT temp = l1 + l1;
fe3e8e40 252 *hv = (h1 << 1) + (temp < l1);
6d716ca8 253 *lv = temp;
fe3e8e40 254 return overflow;
6d716ca8
RS
255 }
256 if (l2 == 4)
257 {
fe3e8e40 258 int overflow = left_shift_overflows (h1, 2);
906c4e36 259 unsigned HOST_WIDE_INT temp = l1 + l1;
fe3e8e40 260 h1 = (h1 << 2) + ((temp < l1) << 1);
6d716ca8
RS
261 l1 = temp;
262 temp += temp;
263 h1 += (temp < l1);
264 *lv = temp;
265 *hv = h1;
fe3e8e40 266 return overflow;
6d716ca8
RS
267 }
268 if (l2 == 8)
269 {
fe3e8e40 270 int overflow = left_shift_overflows (h1, 3);
906c4e36 271 unsigned HOST_WIDE_INT temp = l1 + l1;
fe3e8e40 272 h1 = (h1 << 3) + ((temp < l1) << 2);
6d716ca8
RS
273 l1 = temp;
274 temp += temp;
275 h1 += (temp < l1) << 1;
276 l1 = temp;
277 temp += temp;
278 h1 += (temp < l1);
279 *lv = temp;
280 *hv = h1;
fe3e8e40 281 return overflow;
6d716ca8
RS
282 }
283 }
284
285 encode (arg1, l1, h1);
286 encode (arg2, l2, h2);
287
288 bzero (prod, sizeof prod);
289
906c4e36
RK
290 for (i = 0; i < MAX_SHORTS; i++)
291 for (j = 0; j < MAX_SHORTS; j++)
6d716ca8
RS
292 {
293 k = i + j;
294 carry = arg1[i] * arg2[j];
295 while (carry)
296 {
297 carry += prod[k];
298 prod[k] = carry & 0xff;
299 carry >>= 8;
300 k++;
301 }
302 }
303
fe3e8e40 304 decode (prod, lv, hv); /* This ignores
906c4e36 305 prod[MAX_SHORTS] -> prod[MAX_SHORTS*2-1] */
fe3e8e40
RS
306
307 /* Check for overflow by calculating the top half of the answer in full;
308 it should agree with the low half's sign bit. */
309 decode (prod+MAX_SHORTS, &toplow, &tophigh);
310 if (h1 < 0)
311 {
312 neg_double (l2, h2, &neglow, &neghigh);
313 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
314 }
315 if (h2 < 0)
316 {
317 neg_double (l1, h1, &neglow, &neghigh);
318 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
319 }
320 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
6d716ca8
RS
321}
322\f
906c4e36 323/* Shift the doubleword integer in L1, H1 left by COUNT places
6d716ca8
RS
324 keeping only PREC bits of result.
325 Shift right if COUNT is negative.
326 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
fe3e8e40 327 Return nonzero if the arithmetic shift overflows, assuming it's signed.
906c4e36 328 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8 329
fe3e8e40 330int
6d716ca8 331lshift_double (l1, h1, count, prec, lv, hv, arith)
906c4e36
RK
332 HOST_WIDE_INT l1, h1;
333 int count, prec;
334 HOST_WIDE_INT *lv, *hv;
6d716ca8
RS
335 int arith;
336{
906c4e36 337 short arg1[MAX_SHORTS];
6d716ca8 338 register int i;
fe3e8e40 339 register int carry, overflow;
6d716ca8
RS
340
341 if (count < 0)
342 {
343 rshift_double (l1, h1, - count, prec, lv, hv, arith);
fe3e8e40 344 return 0;
6d716ca8
RS
345 }
346
347 encode (arg1, l1, h1);
348
349 if (count > prec)
350 count = prec;
351
fe3e8e40 352 overflow = 0;
6d716ca8
RS
353 while (count > 0)
354 {
355 carry = 0;
906c4e36 356 for (i = 0; i < MAX_SHORTS; i++)
6d716ca8
RS
357 {
358 carry += arg1[i] << 1;
359 arg1[i] = carry & 0xff;
360 carry >>= 8;
361 }
362 count--;
fe3e8e40 363 overflow |= carry ^ (arg1[7] >> 7);
6d716ca8
RS
364 }
365
366 decode (arg1, lv, hv);
fe3e8e40 367 return overflow;
6d716ca8
RS
368}
369
906c4e36 370/* Shift the doubleword integer in L1, H1 right by COUNT places
6d716ca8
RS
371 keeping only PREC bits of result. COUNT must be positive.
372 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
906c4e36 373 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8
RS
374
375void
376rshift_double (l1, h1, count, prec, lv, hv, arith)
906c4e36
RK
377 HOST_WIDE_INT l1, h1, count, prec;
378 HOST_WIDE_INT *lv, *hv;
6d716ca8
RS
379 int arith;
380{
906c4e36 381 short arg1[MAX_SHORTS];
6d716ca8
RS
382 register int i;
383 register int carry;
384
385 encode (arg1, l1, h1);
386
387 if (count > prec)
388 count = prec;
389
390 while (count > 0)
391 {
392 carry = arith && arg1[7] >> 7;
906c4e36 393 for (i = MAX_SHORTS - 1; i >= 0; i--)
6d716ca8
RS
394 {
395 carry <<= 8;
396 carry += arg1[i];
397 arg1[i] = (carry >> 1) & 0xff;
398 }
399 count--;
400 }
401
402 decode (arg1, lv, hv);
403}
404\f
906c4e36 405/* Rotate the doubldword integer in L1, H1 left by COUNT places
6d716ca8
RS
406 keeping only PREC bits of result.
407 Rotate right if COUNT is negative.
906c4e36 408 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8
RS
409
410void
411lrotate_double (l1, h1, count, prec, lv, hv)
906c4e36
RK
412 HOST_WIDE_INT l1, h1, count, prec;
413 HOST_WIDE_INT *lv, *hv;
6d716ca8 414{
906c4e36 415 short arg1[MAX_SHORTS];
6d716ca8
RS
416 register int i;
417 register int carry;
418
419 if (count < 0)
420 {
421 rrotate_double (l1, h1, - count, prec, lv, hv);
422 return;
423 }
424
425 encode (arg1, l1, h1);
426
427 if (count > prec)
428 count = prec;
429
906c4e36 430 carry = arg1[MAX_SHORTS - 1] >> 7;
6d716ca8
RS
431 while (count > 0)
432 {
906c4e36 433 for (i = 0; i < MAX_SHORTS; i++)
6d716ca8
RS
434 {
435 carry += arg1[i] << 1;
436 arg1[i] = carry & 0xff;
437 carry >>= 8;
438 }
439 count--;
440 }
441
442 decode (arg1, lv, hv);
443}
444
906c4e36 445/* Rotate the doubleword integer in L1, H1 left by COUNT places
6d716ca8 446 keeping only PREC bits of result. COUNT must be positive.
906c4e36 447 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
6d716ca8
RS
448
449void
450rrotate_double (l1, h1, count, prec, lv, hv)
906c4e36
RK
451 HOST_WIDE_INT l1, h1, count, prec;
452 HOST_WIDE_INT *lv, *hv;
6d716ca8 453{
906c4e36 454 short arg1[MAX_SHORTS];
6d716ca8
RS
455 register int i;
456 register int carry;
457
458 encode (arg1, l1, h1);
459
460 if (count > prec)
461 count = prec;
462
463 carry = arg1[0] & 1;
464 while (count > 0)
465 {
906c4e36 466 for (i = MAX_SHORTS - 1; i >= 0; i--)
6d716ca8
RS
467 {
468 carry <<= 8;
469 carry += arg1[i];
470 arg1[i] = (carry >> 1) & 0xff;
471 }
472 count--;
473 }
474
475 decode (arg1, lv, hv);
476}
477\f
906c4e36 478/* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
6d716ca8
RS
479 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
480 CODE is a tree code for a kind of division, one of
481 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
482 or EXACT_DIV_EXPR
fe3e8e40
RS
483 It controls how the quotient is rounded to a integer.
484 Return nonzero if the operation overflows.
6d716ca8
RS
485 UNS nonzero says do unsigned division. */
486
fe3e8e40 487static int
6d716ca8
RS
488div_and_round_double (code, uns,
489 lnum_orig, hnum_orig, lden_orig, hden_orig,
490 lquo, hquo, lrem, hrem)
491 enum tree_code code;
492 int uns;
906c4e36
RK
493 HOST_WIDE_INT lnum_orig, hnum_orig; /* num == numerator == dividend */
494 HOST_WIDE_INT lden_orig, hden_orig; /* den == denominator == divisor */
495 HOST_WIDE_INT *lquo, *hquo, *lrem, *hrem;
6d716ca8
RS
496{
497 int quo_neg = 0;
906c4e36
RK
498 short num[MAX_SHORTS + 1]; /* extra element for scaling. */
499 short den[MAX_SHORTS], quo[MAX_SHORTS];
6d716ca8
RS
500 register int i, j, work;
501 register int carry = 0;
b8eb43a2
RK
502 unsigned HOST_WIDE_INT lnum = lnum_orig;
503 HOST_WIDE_INT hnum = hnum_orig;
504 unsigned HOST_WIDE_INT lden = lden_orig;
505 HOST_WIDE_INT hden = hden_orig;
fe3e8e40 506 int overflow = 0;
6d716ca8
RS
507
508 if ((hden == 0) && (lden == 0))
509 abort ();
510
511 /* calculate quotient sign and convert operands to unsigned. */
512 if (!uns)
513 {
fe3e8e40 514 if (hnum < 0)
6d716ca8
RS
515 {
516 quo_neg = ~ quo_neg;
fe3e8e40
RS
517 /* (minimum integer) / (-1) is the only overflow case. */
518 if (neg_double (lnum, hnum, &lnum, &hnum) && (lden & hden) == -1)
519 overflow = 1;
6d716ca8 520 }
fe3e8e40 521 if (hden < 0)
6d716ca8
RS
522 {
523 quo_neg = ~ quo_neg;
fe3e8e40 524 neg_double (lden, hden, &lden, &hden);
6d716ca8
RS
525 }
526 }
527
528 if (hnum == 0 && hden == 0)
529 { /* single precision */
530 *hquo = *hrem = 0;
531 *lquo = lnum / lden; /* rounds toward zero since positive args */
532 goto finish_up;
533 }
534
535 if (hnum == 0)
536 { /* trivial case: dividend < divisor */
537 /* hden != 0 already checked. */
538 *hquo = *lquo = 0;
539 *hrem = hnum;
540 *lrem = lnum;
541 goto finish_up;
542 }
543
544 bzero (quo, sizeof quo);
545
546 bzero (num, sizeof num); /* to zero 9th element */
547 bzero (den, sizeof den);
548
549 encode (num, lnum, hnum);
550 encode (den, lden, hden);
551
552 /* This code requires more than just hden == 0.
553 We also have to require that we don't need more than three bytes
554 to hold CARRY. If we ever did need four bytes to hold it, we
555 would lose part of it when computing WORK on the next round. */
556 if (hden == 0 && ((lden << 8) >> 8) == lden)
557 { /* simpler algorithm */
558 /* hnum != 0 already checked. */
906c4e36 559 for (i = MAX_SHORTS - 1; i >= 0; i--)
6d716ca8
RS
560 {
561 work = num[i] + (carry << 8);
562 quo[i] = work / lden;
563 carry = work % lden;
564 }
565 }
566 else { /* full double precision,
567 with thanks to Don Knuth's
6dc42e49 568 "Seminumerical Algorithms". */
6d716ca8
RS
569#define BASE 256
570 int quo_est, scale, num_hi_sig, den_hi_sig, quo_hi_sig;
571
572 /* Find the highest non-zero divisor digit. */
906c4e36 573 for (i = MAX_SHORTS - 1; ; i--)
6d716ca8
RS
574 if (den[i] != 0) {
575 den_hi_sig = i;
576 break;
577 }
906c4e36 578 for (i = MAX_SHORTS - 1; ; i--)
6d716ca8
RS
579 if (num[i] != 0) {
580 num_hi_sig = i;
581 break;
582 }
583 quo_hi_sig = num_hi_sig - den_hi_sig + 1;
584
585 /* Insure that the first digit of the divisor is at least BASE/2.
586 This is required by the quotient digit estimation algorithm. */
587
588 scale = BASE / (den[den_hi_sig] + 1);
589 if (scale > 1) { /* scale divisor and dividend */
590 carry = 0;
906c4e36 591 for (i = 0; i <= MAX_SHORTS - 1; i++) {
6d716ca8
RS
592 work = (num[i] * scale) + carry;
593 num[i] = work & 0xff;
594 carry = work >> 8;
595 if (num[i] != 0) num_hi_sig = i;
596 }
597 carry = 0;
906c4e36 598 for (i = 0; i <= MAX_SHORTS - 1; i++) {
6d716ca8
RS
599 work = (den[i] * scale) + carry;
600 den[i] = work & 0xff;
601 carry = work >> 8;
602 if (den[i] != 0) den_hi_sig = i;
603 }
604 }
605
606 /* Main loop */
607 for (i = quo_hi_sig; i > 0; i--) {
6dc42e49 608 /* guess the next quotient digit, quo_est, by dividing the first
6d716ca8
RS
609 two remaining dividend digits by the high order quotient digit.
610 quo_est is never low and is at most 2 high. */
611
612 int num_hi; /* index of highest remaining dividend digit */
613
614 num_hi = i + den_hi_sig;
615
616 work = (num[num_hi] * BASE) + (num_hi > 0 ? num[num_hi - 1] : 0);
617 if (num[num_hi] != den[den_hi_sig]) {
618 quo_est = work / den[den_hi_sig];
619 }
620 else {
621 quo_est = BASE - 1;
622 }
623
624 /* refine quo_est so it's usually correct, and at most one high. */
625 while ((den[den_hi_sig - 1] * quo_est)
626 > (((work - (quo_est * den[den_hi_sig])) * BASE)
627 + ((num_hi - 1) > 0 ? num[num_hi - 2] : 0)))
628 quo_est--;
629
630 /* Try QUO_EST as the quotient digit, by multiplying the
631 divisor by QUO_EST and subtracting from the remaining dividend.
632 Keep in mind that QUO_EST is the I - 1st digit. */
633
634 carry = 0;
635
636 for (j = 0; j <= den_hi_sig; j++)
637 {
638 int digit;
639
640 work = num[i + j - 1] - (quo_est * den[j]) + carry;
641 digit = work & 0xff;
642 carry = work >> 8;
643 if (digit < 0)
644 {
645 digit += BASE;
646 carry--;
647 }
648 num[i + j - 1] = digit;
649 }
650
651 /* if quo_est was high by one, then num[i] went negative and
652 we need to correct things. */
653
654 if (num[num_hi] < 0)
655 {
656 quo_est--;
657 carry = 0; /* add divisor back in */
658 for (j = 0; j <= den_hi_sig; j++)
659 {
660 work = num[i + j - 1] + den[j] + carry;
661 if (work > BASE)
662 {
663 work -= BASE;
664 carry = 1;
665 }
666 else
667 {
668 carry = 0;
669 }
670 num[i + j - 1] = work;
671 }
672 num [num_hi] += carry;
673 }
674
675 /* store the quotient digit. */
676 quo[i - 1] = quo_est;
677 }
678 }
679
680 decode (quo, lquo, hquo);
681
682 finish_up:
683 /* if result is negative, make it so. */
684 if (quo_neg)
685 neg_double (*lquo, *hquo, lquo, hquo);
686
687 /* compute trial remainder: rem = num - (quo * den) */
688 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
689 neg_double (*lrem, *hrem, lrem, hrem);
690 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
691
692 switch (code)
693 {
694 case TRUNC_DIV_EXPR:
695 case TRUNC_MOD_EXPR: /* round toward zero */
696 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
fe3e8e40 697 return overflow;
6d716ca8
RS
698
699 case FLOOR_DIV_EXPR:
700 case FLOOR_MOD_EXPR: /* round toward negative infinity */
701 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
702 {
703 /* quo = quo - 1; */
906c4e36
RK
704 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
705 lquo, hquo);
6d716ca8 706 }
fe3e8e40 707 else return overflow;
6d716ca8
RS
708 break;
709
710 case CEIL_DIV_EXPR:
711 case CEIL_MOD_EXPR: /* round toward positive infinity */
712 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
713 {
906c4e36
RK
714 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
715 lquo, hquo);
6d716ca8 716 }
fe3e8e40 717 else return overflow;
6d716ca8
RS
718 break;
719
720 case ROUND_DIV_EXPR:
721 case ROUND_MOD_EXPR: /* round to closest integer */
722 {
906c4e36
RK
723 HOST_WIDE_INT labs_rem = *lrem, habs_rem = *hrem;
724 HOST_WIDE_INT labs_den = lden, habs_den = hden, ltwice, htwice;
6d716ca8
RS
725
726 /* get absolute values */
727 if (*hrem < 0) neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
728 if (hden < 0) neg_double (lden, hden, &labs_den, &habs_den);
729
730 /* if (2 * abs (lrem) >= abs (lden)) */
906c4e36
RK
731 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
732 labs_rem, habs_rem, &ltwice, &htwice);
733 if (((unsigned HOST_WIDE_INT) habs_den
734 < (unsigned HOST_WIDE_INT) htwice)
735 || (((unsigned HOST_WIDE_INT) habs_den
736 == (unsigned HOST_WIDE_INT) htwice)
737 && ((HOST_WIDE_INT unsigned) labs_den
738 < (unsigned HOST_WIDE_INT) ltwice)))
6d716ca8
RS
739 {
740 if (*hquo < 0)
741 /* quo = quo - 1; */
906c4e36
RK
742 add_double (*lquo, *hquo,
743 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
6d716ca8
RS
744 else
745 /* quo = quo + 1; */
906c4e36
RK
746 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
747 lquo, hquo);
6d716ca8 748 }
fe3e8e40 749 else return overflow;
6d716ca8
RS
750 }
751 break;
752
753 default:
754 abort ();
755 }
756
757 /* compute true remainder: rem = num - (quo * den) */
758 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
759 neg_double (*lrem, *hrem, lrem, hrem);
760 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
fe3e8e40 761 return overflow;
6d716ca8
RS
762}
763\f
5352b11a
RS
764/* Effectively truncate a real value to represent
765 the nearest possible value in a narrower mode.
766 The result is actually represented in the same data type as the argument,
767 but its value is usually different. */
768
769REAL_VALUE_TYPE
770real_value_truncate (mode, arg)
771 enum machine_mode mode;
772 REAL_VALUE_TYPE arg;
773{
774#ifdef __STDC__
775 /* Make sure the value is actually stored in memory before we turn off
776 the handler. */
777 volatile
778#endif
779 REAL_VALUE_TYPE value;
780 jmp_buf handler;
781
782 if (setjmp (handler))
783 {
784 error ("floating overflow");
785 return dconst0;
786 }
787 set_float_handler (handler);
788 value = REAL_VALUE_TRUNCATE (mode, arg);
906c4e36 789 set_float_handler (NULL_PTR);
5352b11a
RS
790 return value;
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
915target_isinf (x)
916 REAL_VALUE_TYPE x;
917{
918 return 0;
919}
920
7d4d4d22
RS
921/* Let's assume other float formats don't have NaNs.
922 (This can be overridden by redefining REAL_VALUE_ISNAN.) */
923
924target_isnan (x)
925 REAL_VALUE_TYPE x;
926{
927 return 0;
928}
929
6d716ca8 930/* Let's assume other float formats don't have minus zero.
c05a9b68 931 (This can be overridden by redefining REAL_VALUE_NEGATIVE.) */
6d716ca8 932
c05a9b68 933target_negative (x)
6d716ca8
RS
934 REAL_VALUE_TYPE x;
935{
c05a9b68 936 return x < 0;
6d716ca8
RS
937}
938#endif /* Target not IEEE */
939\f
940/* Split a tree IN into a constant and a variable part
941 that could be combined with CODE to make IN.
942 CODE must be a commutative arithmetic operation.
943 Store the constant part into *CONP and the variable in &VARP.
944 Return 1 if this was done; zero means the tree IN did not decompose
945 this way.
946
947 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.
948 Therefore, we must tell the caller whether the variable part
949 was subtracted. We do this by storing 1 or -1 into *VARSIGNP.
950 The value stored is the coefficient for the variable term.
951 The constant term we return should always be added;
952 we negate it if necessary. */
953
954static int
955split_tree (in, code, varp, conp, varsignp)
956 tree in;
957 enum tree_code code;
958 tree *varp, *conp;
959 int *varsignp;
960{
961 register tree outtype = TREE_TYPE (in);
962 *varp = 0;
963 *conp = 0;
964
965 /* Strip any conversions that don't change the machine mode. */
966 while ((TREE_CODE (in) == NOP_EXPR
967 || TREE_CODE (in) == CONVERT_EXPR)
968 && (TYPE_MODE (TREE_TYPE (in))
969 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (in, 0)))))
970 in = TREE_OPERAND (in, 0);
971
972 if (TREE_CODE (in) == code
973 || (TREE_CODE (TREE_TYPE (in)) != REAL_TYPE
974 /* We can associate addition and subtraction together
975 (even though the C standard doesn't say so)
976 for integers because the value is not affected.
977 For reals, the value might be affected, so we can't. */
978 &&
979 ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
980 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
981 {
982 enum tree_code code = TREE_CODE (TREE_OPERAND (in, 0));
983 if (code == INTEGER_CST)
984 {
985 *conp = TREE_OPERAND (in, 0);
986 *varp = TREE_OPERAND (in, 1);
987 if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
988 && TREE_TYPE (*varp) != outtype)
989 *varp = convert (outtype, *varp);
990 *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
991 return 1;
992 }
993 if (TREE_CONSTANT (TREE_OPERAND (in, 1)))
994 {
995 *conp = TREE_OPERAND (in, 1);
996 *varp = TREE_OPERAND (in, 0);
997 *varsignp = 1;
998 if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
999 && TREE_TYPE (*varp) != outtype)
1000 *varp = convert (outtype, *varp);
1001 if (TREE_CODE (in) == MINUS_EXPR)
1002 {
1003 /* If operation is subtraction and constant is second,
1004 must negate it to get an additive constant.
1005 And this cannot be done unless it is a manifest constant.
1006 It could also be the address of a static variable.
1007 We cannot negate that, so give up. */
1008 if (TREE_CODE (*conp) == INTEGER_CST)
1009 /* Subtracting from integer_zero_node loses for long long. */
1010 *conp = fold (build1 (NEGATE_EXPR, TREE_TYPE (*conp), *conp));
1011 else
1012 return 0;
1013 }
1014 return 1;
1015 }
1016 if (TREE_CONSTANT (TREE_OPERAND (in, 0)))
1017 {
1018 *conp = TREE_OPERAND (in, 0);
1019 *varp = TREE_OPERAND (in, 1);
1020 if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1021 && TREE_TYPE (*varp) != outtype)
1022 *varp = convert (outtype, *varp);
1023 *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
1024 return 1;
1025 }
1026 }
1027 return 0;
1028}
1029\f
1030/* Combine two constants NUM and ARG2 under operation CODE
1031 to produce a new constant.
1032 We assume ARG1 and ARG2 have the same data type,
1033 or at least are the same kind of constant and the same machine mode. */
1034
6d716ca8
RS
1035static tree
1036const_binop (code, arg1, arg2)
1037 enum tree_code code;
1038 register tree arg1, arg2;
1039{
1040 if (TREE_CODE (arg1) == INTEGER_CST)
1041 {
906c4e36
RK
1042 register HOST_WIDE_INT int1l = TREE_INT_CST_LOW (arg1);
1043 register HOST_WIDE_INT int1h = TREE_INT_CST_HIGH (arg1);
1044 HOST_WIDE_INT int2l = TREE_INT_CST_LOW (arg2);
1045 HOST_WIDE_INT int2h = TREE_INT_CST_HIGH (arg2);
1046 HOST_WIDE_INT low, hi;
1047 HOST_WIDE_INT garbagel, garbageh;
6d716ca8
RS
1048 register tree t;
1049 int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
fe3e8e40
RS
1050 /* Propagate overflow flags from operands; also record new overflow. */
1051 int overflow
1052 = TREE_CONSTANT_OVERFLOW (arg0) | TREE_CONSTANT_OVERFLOW (arg1);
6d716ca8
RS
1053
1054 switch (code)
1055 {
1056 case BIT_IOR_EXPR:
1057 t = build_int_2 (int1l | int2l, int1h | int2h);
1058 break;
1059
1060 case BIT_XOR_EXPR:
1061 t = build_int_2 (int1l ^ int2l, int1h ^ int2h);
1062 break;
1063
1064 case BIT_AND_EXPR:
1065 t = build_int_2 (int1l & int2l, int1h & int2h);
1066 break;
1067
1068 case BIT_ANDTC_EXPR:
1069 t = build_int_2 (int1l & ~int2l, int1h & ~int2h);
1070 break;
1071
1072 case RSHIFT_EXPR:
1073 int2l = - int2l;
1074 case LSHIFT_EXPR:
fe3e8e40
RS
1075 overflow = lshift_double (int1l, int1h, int2l,
1076 TYPE_PRECISION (TREE_TYPE (arg1)),
1077 &low, &hi,
1078 !uns);
6d716ca8
RS
1079 t = build_int_2 (low, hi);
1080 break;
1081
1082 case RROTATE_EXPR:
1083 int2l = - int2l;
1084 case LROTATE_EXPR:
1085 lrotate_double (int1l, int1h, int2l,
1086 TYPE_PRECISION (TREE_TYPE (arg1)),
1087 &low, &hi);
1088 t = build_int_2 (low, hi);
1089 break;
1090
1091 case PLUS_EXPR:
1092 if (int1h == 0)
1093 {
1094 int2l += int1l;
906c4e36 1095 if ((unsigned HOST_WIDE_INT) int2l < int1l)
fe3e8e40
RS
1096 {
1097 hi = int2h++;
1098 overflow = ! same_sign (hi, int2h);
1099 }
6d716ca8
RS
1100 t = build_int_2 (int2l, int2h);
1101 break;
1102 }
1103 if (int2h == 0)
1104 {
1105 int1l += int2l;
906c4e36 1106 if ((unsigned HOST_WIDE_INT) int1l < int2l)
fe3e8e40
RS
1107 {
1108 hi = int1h++;
1109 overflow = ! same_sign (hi, int1h);
1110 }
6d716ca8
RS
1111 t = build_int_2 (int1l, int1h);
1112 break;
1113 }
fe3e8e40 1114 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
6d716ca8
RS
1115 t = build_int_2 (low, hi);
1116 break;
1117
1118 case MINUS_EXPR:
1119 if (int2h == 0 && int2l == 0)
1120 {
1121 t = build_int_2 (int1l, int1h);
1122 break;
1123 }
fe3e8e40
RS
1124 neg_double (int2l, int2h, &low, &hi);
1125 add_double (int1l, int1h, low, hi, &low, &hi);
1126 overflow = overflow_sum_sign (hi, int2h, int1h);
6d716ca8
RS
1127 t = build_int_2 (low, hi);
1128 break;
1129
1130 case MULT_EXPR:
812dd8a3 1131 /* Optimize simple cases. */
6d716ca8
RS
1132 if (int1h == 0)
1133 {
906c4e36 1134 unsigned HOST_WIDE_INT temp;
6d716ca8
RS
1135
1136 switch (int1l)
1137 {
1138 case 0:
1139 t = build_int_2 (0, 0);
1140 goto got_it;
1141 case 1:
1142 t = build_int_2 (int2l, int2h);
1143 goto got_it;
1144 case 2:
fe3e8e40 1145 overflow = left_shift_overflows (int2h, 1);
6d716ca8 1146 temp = int2l + int2l;
fe3e8e40 1147 int2h = (int2h << 1) + (temp < int2l);
6d716ca8
RS
1148 t = build_int_2 (temp, int2h);
1149 goto got_it;
812dd8a3 1150#if 0 /* This code can lose carries. */
6d716ca8
RS
1151 case 3:
1152 temp = int2l + int2l + int2l;
1153 int2h = int2h * 3 + (temp < int2l);
1154 t = build_int_2 (temp, int2h);
1155 goto got_it;
812dd8a3 1156#endif
6d716ca8 1157 case 4:
fe3e8e40 1158 overflow = left_shift_overflows (int2h, 2);
6d716ca8 1159 temp = int2l + int2l;
fe3e8e40 1160 int2h = (int2h << 2) + ((temp < int2l) << 1);
6d716ca8
RS
1161 int2l = temp;
1162 temp += temp;
1163 int2h += (temp < int2l);
1164 t = build_int_2 (temp, int2h);
1165 goto got_it;
1166 case 8:
fe3e8e40 1167 overflow = left_shift_overflows (int2h, 3);
6d716ca8 1168 temp = int2l + int2l;
fe3e8e40 1169 int2h = (int2h << 3) + ((temp < int2l) << 2);
6d716ca8
RS
1170 int2l = temp;
1171 temp += temp;
1172 int2h += (temp < int2l) << 1;
1173 int2l = temp;
1174 temp += temp;
1175 int2h += (temp < int2l);
1176 t = build_int_2 (temp, int2h);
1177 goto got_it;
1178 default:
1179 break;
1180 }
1181 }
1182
1183 if (int2h == 0)
1184 {
1185 if (int2l == 0)
1186 {
1187 t = build_int_2 (0, 0);
1188 break;
1189 }
1190 if (int2l == 1)
1191 {
1192 t = build_int_2 (int1l, int1h);
1193 break;
1194 }
1195 }
1196
fe3e8e40 1197 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
6d716ca8
RS
1198 t = build_int_2 (low, hi);
1199 break;
1200
1201 case TRUNC_DIV_EXPR:
1202 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1203 case EXACT_DIV_EXPR:
1204 /* This is a shortcut for a common special case.
1205 It reduces the number of tree nodes generated
1206 and saves time. */
1207 if (int2h == 0 && int2l > 0
1208 && TREE_TYPE (arg1) == sizetype
1209 && int1h == 0 && int1l >= 0)
1210 {
1211 if (code == CEIL_DIV_EXPR)
1212 int1l += int2l-1;
1213 return size_int (int1l / int2l);
1214 }
1215 case ROUND_DIV_EXPR:
1216 if (int2h == 0 && int2l == 1)
1217 {
1218 t = build_int_2 (int1l, int1h);
1219 break;
1220 }
1221 if (int1l == int2l && int1h == int2h)
1222 {
1223 if ((int1l | int1h) == 0)
1224 abort ();
1225 t = build_int_2 (1, 0);
1226 break;
1227 }
fe3e8e40
RS
1228 overflow = div_and_round_double (code, uns,
1229 int1l, int1h, int2l, int2h,
1230 &low, &hi, &garbagel, &garbageh);
6d716ca8
RS
1231 t = build_int_2 (low, hi);
1232 break;
1233
1234 case TRUNC_MOD_EXPR: case ROUND_MOD_EXPR:
1235 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
fe3e8e40
RS
1236 overflow = div_and_round_double (code, uns,
1237 int1l, int1h, int2l, int2h,
1238 &garbagel, &garbageh, &low, &hi);
6d716ca8
RS
1239 t = build_int_2 (low, hi);
1240 break;
1241
1242 case MIN_EXPR:
1243 case MAX_EXPR:
1244 if (uns)
1245 {
906c4e36
RK
1246 low = (((unsigned HOST_WIDE_INT) int1h
1247 < (unsigned HOST_WIDE_INT) int2h)
1248 || (((unsigned HOST_WIDE_INT) int1h
1249 == (unsigned HOST_WIDE_INT) int2h)
1250 && ((unsigned HOST_WIDE_INT) int1l
1251 < (unsigned HOST_WIDE_INT) int2l)));
6d716ca8
RS
1252 }
1253 else
1254 {
1255 low = ((int1h < int2h)
1256 || ((int1h == int2h)
906c4e36
RK
1257 && ((unsigned HOST_WIDE_INT) int1l
1258 < (unsigned HOST_WIDE_INT) int2l)));
6d716ca8
RS
1259 }
1260 if (low == (code == MIN_EXPR))
1261 t = build_int_2 (int1l, int1h);
1262 else
1263 t = build_int_2 (int2l, int2h);
1264 break;
1265
1266 default:
1267 abort ();
1268 }
1269 got_it:
1270 TREE_TYPE (t) = TREE_TYPE (arg1);
1271 force_fit_type (t);
fe3e8e40 1272 TREE_CONSTANT_OVERFLOW (t) = overflow;
6d716ca8
RS
1273 return t;
1274 }
1275#if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1276 if (TREE_CODE (arg1) == REAL_CST)
1277 {
1278 register REAL_VALUE_TYPE d1;
1279 register REAL_VALUE_TYPE d2;
1280 register REAL_VALUE_TYPE value;
7c7b029d 1281 tree t;
6d716ca8
RS
1282
1283 d1 = TREE_REAL_CST (arg1);
1284 d2 = TREE_REAL_CST (arg2);
7c7b029d 1285 if (setjmp (float_error))
6d716ca8 1286 {
fe3e8e40 1287 pedwarn ("floating overflow in constant expression");
6d716ca8
RS
1288 return build (code, TREE_TYPE (arg1), arg1, arg2);
1289 }
7c7b029d 1290 set_float_handler (float_error);
6d716ca8
RS
1291
1292#ifdef REAL_ARITHMETIC
1293 REAL_ARITHMETIC (value, code, d1, d2);
1294#else
1295 switch (code)
1296 {
1297 case PLUS_EXPR:
1298 value = d1 + d2;
1299 break;
1300
1301 case MINUS_EXPR:
1302 value = d1 - d2;
1303 break;
1304
1305 case MULT_EXPR:
1306 value = d1 * d2;
1307 break;
1308
1309 case RDIV_EXPR:
1310#ifndef REAL_INFINITY
1311 if (d2 == 0)
1312 abort ();
1313#endif
1314
1315 value = d1 / d2;
1316 break;
1317
1318 case MIN_EXPR:
1319 value = MIN (d1, d2);
1320 break;
1321
1322 case MAX_EXPR:
1323 value = MAX (d1, d2);
1324 break;
1325
1326 default:
1327 abort ();
1328 }
1329#endif /* no REAL_ARITHMETIC */
7c7b029d 1330 t = build_real (TREE_TYPE (arg1),
5352b11a 1331 real_value_truncate (TYPE_MODE (TREE_TYPE (arg1)), value));
906c4e36 1332 set_float_handler (NULL_PTR);
7c7b029d 1333 return t;
6d716ca8
RS
1334 }
1335#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1336 if (TREE_CODE (arg1) == COMPLEX_CST)
1337 {
1338 register tree r1 = TREE_REALPART (arg1);
1339 register tree i1 = TREE_IMAGPART (arg1);
1340 register tree r2 = TREE_REALPART (arg2);
1341 register tree i2 = TREE_IMAGPART (arg2);
1342 register tree t;
1343
1344 switch (code)
1345 {
1346 case PLUS_EXPR:
1347 t = build_complex (const_binop (PLUS_EXPR, r1, r2),
1348 const_binop (PLUS_EXPR, i1, i2));
1349 break;
1350
1351 case MINUS_EXPR:
1352 t = build_complex (const_binop (MINUS_EXPR, r1, r2),
1353 const_binop (MINUS_EXPR, i1, i2));
1354 break;
1355
1356 case MULT_EXPR:
1357 t = build_complex (const_binop (MINUS_EXPR,
1358 const_binop (MULT_EXPR, r1, r2),
1359 const_binop (MULT_EXPR, i1, i2)),
1360 const_binop (PLUS_EXPR,
1361 const_binop (MULT_EXPR, r1, i2),
1362 const_binop (MULT_EXPR, i1, r2)));
1363 break;
1364
1365 case RDIV_EXPR:
1366 {
1367 register tree magsquared
1368 = const_binop (PLUS_EXPR,
1369 const_binop (MULT_EXPR, r2, r2),
1370 const_binop (MULT_EXPR, i2, i2));
1371 t = build_complex (const_binop (RDIV_EXPR,
1372 const_binop (PLUS_EXPR,
1373 const_binop (MULT_EXPR, r1, r2),
1374 const_binop (MULT_EXPR, i1, i2)),
1375 magsquared),
1376 const_binop (RDIV_EXPR,
1377 const_binop (MINUS_EXPR,
1378 const_binop (MULT_EXPR, i1, r2),
1379 const_binop (MULT_EXPR, r1, i2)),
1380 magsquared));
1381 }
1382 break;
1383
1384 default:
1385 abort ();
1386 }
1387 TREE_TYPE (t) = TREE_TYPE (arg1);
1388 return t;
1389 }
1390 return 0;
1391}
1392\f
1393/* Return an INTEGER_CST with value V and type from `sizetype'. */
1394
1395tree
1396size_int (number)
1397 unsigned int number;
1398{
1399 register tree t;
1400 /* Type-size nodes already made for small sizes. */
906c4e36 1401 static tree size_table[2*HOST_BITS_PER_WIDE_INT + 1];
6d716ca8 1402
906c4e36
RK
1403 if (number >= 0 && number < 2*HOST_BITS_PER_WIDE_INT + 1
1404 && size_table[number] != 0)
6d716ca8 1405 return size_table[number];
906c4e36 1406 if (number >= 0 && number < 2*HOST_BITS_PER_WIDE_INT + 1)
6d716ca8 1407 {
6d716ca8
RS
1408 push_obstacks_nochange ();
1409 /* Make this a permanent node. */
c05a9b68 1410 end_temporary_allocation ();
6d716ca8
RS
1411 t = build_int_2 (number, 0);
1412 TREE_TYPE (t) = sizetype;
1413 size_table[number] = t;
1414 pop_obstacks ();
1415 }
1416 else
1417 {
1418 t = build_int_2 (number, 0);
1419 TREE_TYPE (t) = sizetype;
1420 }
1421 return t;
1422}
1423
1424/* Combine operands OP1 and OP2 with arithmetic operation CODE.
1425 CODE is a tree code. Data type is taken from `sizetype',
1426 If the operands are constant, so is the result. */
1427
1428tree
1429size_binop (code, arg0, arg1)
1430 enum tree_code code;
1431 tree arg0, arg1;
1432{
1433 /* Handle the special case of two integer constants faster. */
1434 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1435 {
1436 /* And some specific cases even faster than that. */
1437 if (code == PLUS_EXPR
1438 && TREE_INT_CST_LOW (arg0) == 0
1439 && TREE_INT_CST_HIGH (arg0) == 0)
1440 return arg1;
1441 if (code == MINUS_EXPR
1442 && TREE_INT_CST_LOW (arg1) == 0
1443 && TREE_INT_CST_HIGH (arg1) == 0)
1444 return arg0;
1445 if (code == MULT_EXPR
1446 && TREE_INT_CST_LOW (arg0) == 1
1447 && TREE_INT_CST_HIGH (arg0) == 0)
1448 return arg1;
1449 /* Handle general case of two integer constants. */
1450 return const_binop (code, arg0, arg1);
1451 }
1452
1453 if (arg0 == error_mark_node || arg1 == error_mark_node)
1454 return error_mark_node;
1455
1456 return fold (build (code, sizetype, arg0, arg1));
1457}
1458\f
1459/* Given T, a tree representing type conversion of ARG1, a constant,
1460 return a constant tree representing the result of conversion. */
1461
1462static tree
1463fold_convert (t, arg1)
1464 register tree t;
1465 register tree arg1;
1466{
1467 register tree type = TREE_TYPE (t);
1468
1469 if (TREE_CODE (type) == POINTER_TYPE
1470 || TREE_CODE (type) == INTEGER_TYPE
1471 || TREE_CODE (type) == ENUMERAL_TYPE)
1472 {
1473 if (TREE_CODE (arg1) == INTEGER_CST)
1474 {
1475 /* Given an integer constant, make new constant with new type,
1476 appropriately sign-extended or truncated. */
1477 t = build_int_2 (TREE_INT_CST_LOW (arg1),
1478 TREE_INT_CST_HIGH (arg1));
fe3e8e40
RS
1479 /* Carry forward overflow indication unless truncating. */
1480 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (t)))
1481 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg1);
6d716ca8
RS
1482 TREE_TYPE (t) = type;
1483 force_fit_type (t);
1484 }
1485#if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1486 else if (TREE_CODE (arg1) == REAL_CST)
1487 {
c05a9b68
RS
1488 REAL_VALUE_TYPE
1489 l = real_value_from_int_cst (TYPE_MIN_VALUE (type)),
1490 x = TREE_REAL_CST (arg1),
1491 u = real_value_from_int_cst (TYPE_MAX_VALUE (type));
4b3d5ea0
RS
1492 /* See if X will be in range after truncation towards 0.
1493 To compensate for truncation, move the bounds away from 0,
1494 but reject if X exactly equals the adjusted bounds. */
1495#ifdef REAL_ARITHMETIC
1496 REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
1497 REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
1498#else
1499 l--;
1500 u++;
1501#endif
1502 if (! (REAL_VALUES_LESS (l, x) && REAL_VALUES_LESS (x, u)))
6d716ca8 1503 {
fe3e8e40 1504 pedwarn ("real constant out of range for integer conversion");
6d716ca8
RS
1505 return t;
1506 }
1507#ifndef REAL_ARITHMETIC
1508 {
1509 REAL_VALUE_TYPE d;
906c4e36
RK
1510 HOST_WIDE_INT low, high;
1511 HOST_WIDE_INT half_word
1512 = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
6d716ca8
RS
1513
1514 d = TREE_REAL_CST (arg1);
1515 if (d < 0)
1516 d = -d;
1517
906c4e36 1518 high = (HOST_WIDE_INT) (d / half_word / half_word);
6d716ca8 1519 d -= (REAL_VALUE_TYPE) high * half_word * half_word;
906c4e36 1520 low = (unsigned HOST_WIDE_INT) d;
6d716ca8
RS
1521 if (TREE_REAL_CST (arg1) < 0)
1522 neg_double (low, high, &low, &high);
1523 t = build_int_2 (low, high);
1524 }
1525#else
1526 {
906c4e36 1527 HOST_WIDE_INT low, high;
6d716ca8
RS
1528 REAL_VALUE_TO_INT (low, high, TREE_REAL_CST (arg1));
1529 t = build_int_2 (low, high);
1530 }
1531#endif
1532 TREE_TYPE (t) = type;
1533 force_fit_type (t);
1534 }
1535#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1536 TREE_TYPE (t) = type;
1537 }
1538 else if (TREE_CODE (type) == REAL_TYPE)
1539 {
1540#if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1541 if (TREE_CODE (arg1) == INTEGER_CST)
1542 return build_real_from_int_cst (type, arg1);
1543#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1544 if (TREE_CODE (arg1) == REAL_CST)
7c7b029d
RS
1545 {
1546 if (setjmp (float_error))
1547 {
fe3e8e40 1548 pedwarn ("floating overflow in constant expression");
7c7b029d
RS
1549 return t;
1550 }
1551 set_float_handler (float_error);
1552
5352b11a 1553 t = build_real (type, real_value_truncate (TYPE_MODE (type),
7c7b029d 1554 TREE_REAL_CST (arg1)));
906c4e36 1555 set_float_handler (NULL_PTR);
7c7b029d
RS
1556 return t;
1557 }
6d716ca8
RS
1558 }
1559 TREE_CONSTANT (t) = 1;
1560 return t;
1561}
1562\f
1563/* Return an expr equal to X but certainly not valid as an lvalue. */
1564
1565tree
1566non_lvalue (x)
1567 tree x;
1568{
1569 tree result;
1570
1571 /* These things are certainly not lvalues. */
1572 if (TREE_CODE (x) == NON_LVALUE_EXPR
1573 || TREE_CODE (x) == INTEGER_CST
1574 || TREE_CODE (x) == REAL_CST
1575 || TREE_CODE (x) == STRING_CST
1576 || TREE_CODE (x) == ADDR_EXPR)
1577 return x;
1578
1579 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
1580 TREE_CONSTANT (result) = TREE_CONSTANT (x);
1581 return result;
1582}
c05a9b68
RS
1583\f
1584/* Given a tree comparison code, return the code that is the logical inverse
1585 of the given code. It is not safe to do this for floating-point
1586 comparisons, except for NE_EXPR and EQ_EXPR. */
6d716ca8 1587
c05a9b68
RS
1588static enum tree_code
1589invert_tree_comparison (code)
1590 enum tree_code code;
1591{
1592 switch (code)
1593 {
1594 case EQ_EXPR:
1595 return NE_EXPR;
1596 case NE_EXPR:
1597 return EQ_EXPR;
1598 case GT_EXPR:
1599 return LE_EXPR;
1600 case GE_EXPR:
1601 return LT_EXPR;
1602 case LT_EXPR:
1603 return GE_EXPR;
1604 case LE_EXPR:
1605 return GT_EXPR;
1606 default:
1607 abort ();
1608 }
1609}
1610
1611/* Similar, but return the comparison that results if the operands are
1612 swapped. This is safe for floating-point. */
1613
1614static enum tree_code
1615swap_tree_comparison (code)
1616 enum tree_code code;
1617{
1618 switch (code)
1619 {
1620 case EQ_EXPR:
1621 case NE_EXPR:
1622 return code;
1623 case GT_EXPR:
1624 return LT_EXPR;
1625 case GE_EXPR:
1626 return LE_EXPR;
1627 case LT_EXPR:
1628 return GT_EXPR;
1629 case LE_EXPR:
1630 return GE_EXPR;
1631 default:
1632 abort ();
1633 }
1634}
1635\f
6a1746af
RS
1636/* Return nonzero if two operands are necessarily equal.
1637 If ONLY_CONST is non-zero, only return non-zero for constants.
1638 This function tests whether the operands are indistinguishable;
1639 it does not test whether they are equal using C's == operation.
1640 The distinction is important for IEEE floating point, because
1641 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
1642 (2) two NaNs may be indistinguishable, but NaN!=NaN. */
6d716ca8
RS
1643
1644int
1645operand_equal_p (arg0, arg1, only_const)
1646 tree arg0, arg1;
1647 int only_const;
1648{
1649 /* If both types don't have the same signedness, then we can't consider
1650 them equal. We must check this before the STRIP_NOPS calls
1651 because they may change the signedness of the arguments. */
1652 if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
1653 return 0;
1654
1655 STRIP_NOPS (arg0);
1656 STRIP_NOPS (arg1);
1657
1658 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
1659 We don't care about side effects in that case because the SAVE_EXPR
1660 takes care of that for us. */
1661 if (TREE_CODE (arg0) == SAVE_EXPR && arg0 == arg1)
1662 return ! only_const;
1663
1664 if (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1))
1665 return 0;
1666
1667 if (TREE_CODE (arg0) == TREE_CODE (arg1)
1668 && TREE_CODE (arg0) == ADDR_EXPR
1669 && TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0))
1670 return 1;
1671
1672 if (TREE_CODE (arg0) == TREE_CODE (arg1)
1673 && TREE_CODE (arg0) == INTEGER_CST
1674 && TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1)
1675 && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1))
1676 return 1;
1677
6a1746af 1678 /* Detect when real constants are equal. */
6d716ca8 1679 if (TREE_CODE (arg0) == TREE_CODE (arg1)
6a1746af
RS
1680 && TREE_CODE (arg0) == REAL_CST)
1681 return !bcmp (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1),
1682 sizeof (REAL_VALUE_TYPE));
6d716ca8
RS
1683
1684 if (only_const)
1685 return 0;
1686
1687 if (arg0 == arg1)
1688 return 1;
1689
1690 if (TREE_CODE (arg0) != TREE_CODE (arg1))
1691 return 0;
1692 /* This is needed for conversions and for COMPONENT_REF.
1693 Might as well play it safe and always test this. */
1694 if (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
1695 return 0;
1696
1697 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
1698 {
1699 case '1':
1700 /* Two conversions are equal only if signedness and modes match. */
1701 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
1702 && (TREE_UNSIGNED (TREE_TYPE (arg0))
1703 != TREE_UNSIGNED (TREE_TYPE (arg1))))
1704 return 0;
1705
1706 return operand_equal_p (TREE_OPERAND (arg0, 0),
1707 TREE_OPERAND (arg1, 0), 0);
1708
1709 case '<':
1710 case '2':
1711 return (operand_equal_p (TREE_OPERAND (arg0, 0),
1712 TREE_OPERAND (arg1, 0), 0)
1713 && operand_equal_p (TREE_OPERAND (arg0, 1),
1714 TREE_OPERAND (arg1, 1), 0));
1715
1716 case 'r':
1717 switch (TREE_CODE (arg0))
1718 {
1719 case INDIRECT_REF:
1720 return operand_equal_p (TREE_OPERAND (arg0, 0),
1721 TREE_OPERAND (arg1, 0), 0);
1722
1723 case COMPONENT_REF:
1724 case ARRAY_REF:
1725 return (operand_equal_p (TREE_OPERAND (arg0, 0),
1726 TREE_OPERAND (arg1, 0), 0)
1727 && operand_equal_p (TREE_OPERAND (arg0, 1),
1728 TREE_OPERAND (arg1, 1), 0));
1729
1730 case BIT_FIELD_REF:
1731 return (operand_equal_p (TREE_OPERAND (arg0, 0),
1732 TREE_OPERAND (arg1, 0), 0)
1733 && operand_equal_p (TREE_OPERAND (arg0, 1),
1734 TREE_OPERAND (arg1, 1), 0)
1735 && operand_equal_p (TREE_OPERAND (arg0, 2),
1736 TREE_OPERAND (arg1, 2), 0));
1737 }
1738 break;
1739 }
1740
1741 return 0;
1742}
c05a9b68
RS
1743\f
1744/* Similar to operand_equal_p, but see if ARG0 might have been made by
1745 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
6d716ca8 1746
6d716ca8
RS
1747 When in doubt, return 0. */
1748
1749static int
c05a9b68
RS
1750operand_equal_for_comparison_p (arg0, arg1, other)
1751 tree arg0, arg1;
1752 tree other;
6d716ca8 1753{
c05a9b68
RS
1754 int unsignedp1, unsignedpo;
1755 tree primarg1, primother;
6d716ca8
RS
1756 int correct_width;
1757
c05a9b68 1758 if (operand_equal_p (arg0, arg1, 0))
6d716ca8
RS
1759 return 1;
1760
c05a9b68 1761 if (TREE_CODE (TREE_TYPE (arg0)) != INTEGER_TYPE)
6d716ca8
RS
1762 return 0;
1763
c05a9b68
RS
1764 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
1765 actual comparison operand, ARG0.
6d716ca8 1766
c05a9b68 1767 First throw away any conversions to wider types
6d716ca8 1768 already present in the operands. */
6d716ca8 1769
c05a9b68
RS
1770 primarg1 = get_narrower (arg1, &unsignedp1);
1771 primother = get_narrower (other, &unsignedpo);
1772
1773 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
1774 if (unsignedp1 == unsignedpo
1775 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
1776 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
6d716ca8 1777 {
c05a9b68 1778 tree type = TREE_TYPE (arg0);
6d716ca8
RS
1779
1780 /* Make sure shorter operand is extended the right way
1781 to match the longer operand. */
c05a9b68
RS
1782 primarg1 = convert (signed_or_unsigned_type (unsignedp1,
1783 TREE_TYPE (primarg1)),
1784 primarg1);
6d716ca8 1785
c05a9b68 1786 if (operand_equal_p (arg0, convert (type, primarg1), 0))
6d716ca8
RS
1787 return 1;
1788 }
1789
1790 return 0;
1791}
1792\f
f72aed24 1793/* See if ARG is an expression that is either a comparison or is performing
c05a9b68
RS
1794 arithmetic on comparisons. The comparisons must only be comparing
1795 two different values, which will be stored in *CVAL1 and *CVAL2; if
1796 they are non-zero it means that some operands have already been found.
1797 No variables may be used anywhere else in the expression except in the
1798 comparisons.
1799
1800 If this is true, return 1. Otherwise, return zero. */
1801
1802static int
1803twoval_comparison_p (arg, cval1, cval2)
1804 tree arg;
1805 tree *cval1, *cval2;
1806{
1807 enum tree_code code = TREE_CODE (arg);
1808 char class = TREE_CODE_CLASS (code);
1809
1810 /* We can handle some of the 'e' cases here. */
1811 if (class == 'e'
1812 && (code == TRUTH_NOT_EXPR
1813 || (code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0)))
1814 class = '1';
1815 else if (class == 'e'
1816 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
1817 || code == COMPOUND_EXPR))
1818 class = '2';
1819
1820 switch (class)
1821 {
1822 case '1':
1823 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
1824
1825 case '2':
1826 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
1827 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
1828
1829 case 'c':
1830 return 1;
1831
1832 case 'e':
1833 if (code == COND_EXPR)
1834 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
1835 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
1836 && twoval_comparison_p (TREE_OPERAND (arg, 2),
1837 cval1, cval2));
1838 return 0;
1839
1840 case '<':
1841 /* First see if we can handle the first operand, then the second. For
1842 the second operand, we know *CVAL1 can't be zero. It must be that
1843 one side of the comparison is each of the values; test for the
1844 case where this isn't true by failing if the two operands
1845 are the same. */
1846
1847 if (operand_equal_p (TREE_OPERAND (arg, 0),
1848 TREE_OPERAND (arg, 1), 0))
1849 return 0;
1850
1851 if (*cval1 == 0)
1852 *cval1 = TREE_OPERAND (arg, 0);
1853 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
1854 ;
1855 else if (*cval2 == 0)
1856 *cval2 = TREE_OPERAND (arg, 0);
1857 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
1858 ;
1859 else
1860 return 0;
1861
1862 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
1863 ;
1864 else if (*cval2 == 0)
1865 *cval2 = TREE_OPERAND (arg, 1);
1866 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
1867 ;
1868 else
1869 return 0;
1870
1871 return 1;
1872 }
1873
1874 return 0;
1875}
1876\f
1877/* ARG is a tree that is known to contain just arithmetic operations and
1878 comparisons. Evaluate the operations in the tree substituting NEW0 for
f72aed24 1879 any occurrence of OLD0 as an operand of a comparison and likewise for
c05a9b68
RS
1880 NEW1 and OLD1. */
1881
1882static tree
1883eval_subst (arg, old0, new0, old1, new1)
1884 tree arg;
1885 tree old0, new0, old1, new1;
1886{
1887 tree type = TREE_TYPE (arg);
1888 enum tree_code code = TREE_CODE (arg);
1889 char class = TREE_CODE_CLASS (code);
1890
1891 /* We can handle some of the 'e' cases here. */
1892 if (class == 'e' && code == TRUTH_NOT_EXPR)
1893 class = '1';
1894 else if (class == 'e'
1895 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
1896 class = '2';
1897
1898 switch (class)
1899 {
1900 case '1':
1901 return fold (build1 (code, type,
1902 eval_subst (TREE_OPERAND (arg, 0),
1903 old0, new0, old1, new1)));
1904
1905 case '2':
1906 return fold (build (code, type,
1907 eval_subst (TREE_OPERAND (arg, 0),
1908 old0, new0, old1, new1),
1909 eval_subst (TREE_OPERAND (arg, 1),
1910 old0, new0, old1, new1)));
1911
1912 case 'e':
1913 switch (code)
1914 {
1915 case SAVE_EXPR:
1916 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
1917
1918 case COMPOUND_EXPR:
1919 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
1920
1921 case COND_EXPR:
1922 return fold (build (code, type,
1923 eval_subst (TREE_OPERAND (arg, 0),
1924 old0, new0, old1, new1),
1925 eval_subst (TREE_OPERAND (arg, 1),
1926 old0, new0, old1, new1),
1927 eval_subst (TREE_OPERAND (arg, 2),
1928 old0, new0, old1, new1)));
1929 }
1930
1931 case '<':
1932 {
1933 tree arg0 = TREE_OPERAND (arg, 0);
1934 tree arg1 = TREE_OPERAND (arg, 1);
1935
1936 /* We need to check both for exact equality and tree equality. The
1937 former will be true if the operand has a side-effect. In that
1938 case, we know the operand occurred exactly once. */
1939
1940 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
1941 arg0 = new0;
1942 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
1943 arg0 = new1;
1944
1945 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
1946 arg1 = new0;
1947 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
1948 arg1 = new1;
1949
1950 return fold (build (code, type, arg0, arg1));
1951 }
1952 }
1953
1954 return arg;
1955}
1956\f
6d716ca8
RS
1957/* Return a tree for the case when the result of an expression is RESULT
1958 converted to TYPE and OMITTED was previously an operand of the expression
1959 but is now not needed (e.g., we folded OMITTED * 0).
1960
1961 If OMITTED has side effects, we must evaluate it. Otherwise, just do
1962 the conversion of RESULT to TYPE. */
1963
1964static tree
1965omit_one_operand (type, result, omitted)
1966 tree type, result, omitted;
1967{
1968 tree t = convert (type, result);
1969
1970 if (TREE_SIDE_EFFECTS (omitted))
1971 return build (COMPOUND_EXPR, type, omitted, t);
1972
1973 return t;
1974}
1975\f
1976/* Return a simplified tree node for the truth-negation of ARG
1977 (perhaps by altering ARG). It is known that ARG is an operation that
1978 returns a truth value (0 or 1). */
1979
1980tree
1981invert_truthvalue (arg)
1982 tree arg;
1983{
1984 tree type = TREE_TYPE (arg);
c05a9b68 1985 enum tree_code code = TREE_CODE (arg);
6d716ca8 1986
c05a9b68
RS
1987 /* If this is a comparison, we can simply invert it, except for
1988 floating-point non-equality comparisons, in which case we just
1989 enclose a TRUTH_NOT_EXPR around what we have. */
6d716ca8 1990
c05a9b68 1991 if (TREE_CODE_CLASS (code) == '<')
6d716ca8 1992 {
c05a9b68
RS
1993 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REAL_TYPE
1994 && code != NE_EXPR && code != EQ_EXPR)
1995 return build1 (TRUTH_NOT_EXPR, type, arg);
1996 else
1997 {
1998 TREE_SET_CODE (arg, invert_tree_comparison (code));
1999 return arg;
2000 }
2001 }
6d716ca8 2002
c05a9b68
RS
2003 switch (code)
2004 {
6d716ca8
RS
2005 case INTEGER_CST:
2006 return convert (type, build_int_2 (TREE_INT_CST_LOW (arg) == 0
2007 && TREE_INT_CST_HIGH (arg) == 0, 0));
2008
2009 case TRUTH_AND_EXPR:
2010 return build (TRUTH_OR_EXPR, type,
2011 invert_truthvalue (TREE_OPERAND (arg, 0)),
2012 invert_truthvalue (TREE_OPERAND (arg, 1)));
2013
2014 case TRUTH_OR_EXPR:
2015 return build (TRUTH_AND_EXPR, type,
2016 invert_truthvalue (TREE_OPERAND (arg, 0)),
2017 invert_truthvalue (TREE_OPERAND (arg, 1)));
2018
2019 case TRUTH_ANDIF_EXPR:
2020 return build (TRUTH_ORIF_EXPR, type,
2021 invert_truthvalue (TREE_OPERAND (arg, 0)),
2022 invert_truthvalue (TREE_OPERAND (arg, 1)));
2023
2024 case TRUTH_ORIF_EXPR:
2025 return build (TRUTH_ANDIF_EXPR, type,
2026 invert_truthvalue (TREE_OPERAND (arg, 0)),
2027 invert_truthvalue (TREE_OPERAND (arg, 1)));
2028
2029 case TRUTH_NOT_EXPR:
2030 return TREE_OPERAND (arg, 0);
2031
2032 case COND_EXPR:
2033 return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2034 invert_truthvalue (TREE_OPERAND (arg, 1)),
2035 invert_truthvalue (TREE_OPERAND (arg, 2)));
2036
ef9fe0da
RK
2037 case COMPOUND_EXPR:
2038 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2039 invert_truthvalue (TREE_OPERAND (arg, 1)));
2040
6d716ca8
RS
2041 case NON_LVALUE_EXPR:
2042 return invert_truthvalue (TREE_OPERAND (arg, 0));
2043
2044 case NOP_EXPR:
2045 case CONVERT_EXPR:
2046 case FLOAT_EXPR:
2047 return build1 (TREE_CODE (arg), type,
2048 invert_truthvalue (TREE_OPERAND (arg, 0)));
2049
2050 case BIT_AND_EXPR:
2051 if (! integer_onep (TREE_OPERAND (arg, 1)))
2052 abort ();
2053 return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
2054 }
2055
2056 abort ();
2057}
2058
2059/* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2060 operands are another bit-wise operation with a common input. If so,
2061 distribute the bit operations to save an operation and possibly two if
2062 constants are involved. For example, convert
2063 (A | B) & (A | C) into A | (B & C)
2064 Further simplification will occur if B and C are constants.
2065
2066 If this optimization cannot be done, 0 will be returned. */
2067
2068static tree
2069distribute_bit_expr (code, type, arg0, arg1)
2070 enum tree_code code;
2071 tree type;
2072 tree arg0, arg1;
2073{
2074 tree common;
2075 tree left, right;
2076
2077 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2078 || TREE_CODE (arg0) == code
9c0ae98b
CH
2079 || (TREE_CODE (arg0) != TRUTH_AND_EXPR
2080 && TREE_CODE (arg0) != TRUTH_OR_EXPR))
6d716ca8
RS
2081 return 0;
2082
2083 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2084 {
2085 common = TREE_OPERAND (arg0, 0);
2086 left = TREE_OPERAND (arg0, 1);
2087 right = TREE_OPERAND (arg1, 1);
2088 }
2089 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2090 {
2091 common = TREE_OPERAND (arg0, 0);
2092 left = TREE_OPERAND (arg0, 1);
2093 right = TREE_OPERAND (arg1, 0);
2094 }
2095 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2096 {
2097 common = TREE_OPERAND (arg0, 1);
2098 left = TREE_OPERAND (arg0, 0);
2099 right = TREE_OPERAND (arg1, 1);
2100 }
2101 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2102 {
2103 common = TREE_OPERAND (arg0, 1);
2104 left = TREE_OPERAND (arg0, 0);
2105 right = TREE_OPERAND (arg1, 0);
2106 }
2107 else
2108 return 0;
2109
2110 return fold (build (TREE_CODE (arg0), type, common,
2111 fold (build (code, type, left, right))));
2112}
2113\f
2114/* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2115 starting at BITPOS. The field is unsigned if UNSIGNEDP is non-zero. */
2116
2117static tree
2118make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
2119 tree inner;
2120 tree type;
2121 int bitsize, bitpos;
2122 int unsignedp;
2123{
2124 tree result = build (BIT_FIELD_REF, type, inner,
2125 size_int (bitsize), size_int (bitpos));
2126
2127 TREE_UNSIGNED (result) = unsignedp;
2128
2129 return result;
2130}
2131
2132/* Optimize a bit-field compare.
2133
2134 There are two cases: First is a compare against a constant and the
2135 second is a comparison of two items where the fields are at the same
2136 bit position relative to the start of a chunk (byte, halfword, word)
2137 large enough to contain it. In these cases we can avoid the shift
2138 implicit in bitfield extractions.
2139
2140 For constants, we emit a compare of the shifted constant with the
2141 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2142 compared. For two fields at the same position, we do the ANDs with the
2143 similar mask and compare the result of the ANDs.
2144
2145 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2146 COMPARE_TYPE is the type of the comparison, and LHS and RHS
2147 are the left and right operands of the comparison, respectively.
2148
6dc42e49 2149 If the optimization described above can be done, we return the resulting
6d716ca8
RS
2150 tree. Otherwise we return zero. */
2151
2152static tree
2153optimize_bit_field_compare (code, compare_type, lhs, rhs)
2154 enum tree_code code;
2155 tree compare_type;
2156 tree lhs, rhs;
2157{
2158 int lbitpos, lbitsize, rbitpos, rbitsize;
2159 int lnbitpos, lnbitsize, rnbitpos, rnbitsize;
2160 tree type = TREE_TYPE (lhs);
2161 tree signed_type, unsigned_type;
2162 int const_p = TREE_CODE (rhs) == INTEGER_CST;
2163 enum machine_mode lmode, rmode, lnmode, rnmode;
2164 int lunsignedp, runsignedp;
2165 int lvolatilep = 0, rvolatilep = 0;
2166 tree linner, rinner;
2167 tree mask;
f1e60ec6 2168 tree offset;
6d716ca8
RS
2169
2170 /* Get all the information about the extractions being done. If the bit size
2171 if the same as the size of the underlying object, we aren't doing an
2172 extraction at all and so can do nothing. */
f1e60ec6 2173 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
6d716ca8 2174 &lunsignedp, &lvolatilep);
f1e60ec6
RS
2175 if (lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
2176 || offset != 0)
6d716ca8
RS
2177 return 0;
2178
2179 if (!const_p)
2180 {
2181 /* If this is not a constant, we can only do something if bit positions,
2182 sizes, and signedness are the same. */
f1e60ec6 2183 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset,
6d716ca8
RS
2184 &rmode, &runsignedp, &rvolatilep);
2185
2186 if (lbitpos != rbitpos || lbitsize != rbitsize
f1e60ec6 2187 || lunsignedp != runsignedp || offset != 0)
6d716ca8
RS
2188 return 0;
2189 }
2190
2191 /* See if we can find a mode to refer to this field. We should be able to,
2192 but fail if we can't. */
2193 lnmode = get_best_mode (lbitsize, lbitpos,
2194 TYPE_ALIGN (TREE_TYPE (linner)), word_mode,
2195 lvolatilep);
2196 if (lnmode == VOIDmode)
2197 return 0;
2198
2199 /* Set signed and unsigned types of the precision of this mode for the
2200 shifts below. */
2201 signed_type = type_for_mode (lnmode, 0);
2202 unsigned_type = type_for_mode (lnmode, 1);
2203
2204 if (! const_p)
2205 {
2206 rnmode = get_best_mode (rbitsize, rbitpos,
2207 TYPE_ALIGN (TREE_TYPE (rinner)), word_mode,
2208 rvolatilep);
2209 if (rnmode == VOIDmode)
2210 return 0;
2211 }
2212
2213 /* Compute the bit position and size for the new reference and our offset
2214 within it. If the new reference is the same size as the original, we
2215 won't optimize anything, so return zero. */
2216 lnbitsize = GET_MODE_BITSIZE (lnmode);
2217 lnbitpos = lbitpos & ~ (lnbitsize - 1);
2218 lbitpos -= lnbitpos;
2219 if (lnbitsize == lbitsize)
2220 return 0;
2221
2222 if (! const_p)
2223 {
2224 rnbitsize = GET_MODE_BITSIZE (rnmode);
2225 rnbitpos = rbitpos & ~ (rnbitsize - 1);
2226 rbitpos -= rnbitpos;
2227 if (rnbitsize == rbitsize)
2228 return 0;
2229 }
2230
2231#if BYTES_BIG_ENDIAN
2232 lbitpos = lnbitsize - lbitsize - lbitpos;
6d716ca8
RS
2233#endif
2234
2235 /* Make the mask to be used against the extracted field. */
2236 mask = convert (unsigned_type, build_int_2 (~0, ~0));
2237 mask = const_binop (LSHIFT_EXPR, mask, size_int (lnbitsize - lbitsize));
2238 mask = const_binop (RSHIFT_EXPR, mask,
2239 size_int (lnbitsize - lbitsize - lbitpos));
2240
2241 if (! const_p)
2242 /* If not comparing with constant, just rework the comparison
2243 and return. */
2244 return build (code, compare_type,
c0b9d4c8
RK
2245 build (BIT_AND_EXPR, unsigned_type,
2246 make_bit_field_ref (linner, unsigned_type,
2247 lnbitsize, lnbitpos, 1),
6d716ca8 2248 mask),
c0b9d4c8
RK
2249 build (BIT_AND_EXPR, unsigned_type,
2250 make_bit_field_ref (rinner, unsigned_type,
2251 rnbitsize, rnbitpos, 1),
6d716ca8
RS
2252 mask));
2253
2254 /* Otherwise, we are handling the constant case. See if the constant is too
2255 big for the field. Warn and return a tree of for 0 (false) if so. We do
2256 this not only for its own sake, but to avoid having to test for this
2257 error case below. If we didn't, we might generate wrong code.
2258
2259 For unsigned fields, the constant shifted right by the field length should
2260 be all zero. For signed fields, the high-order bits should agree with
2261 the sign bit. */
2262
2263 if (lunsignedp)
2264 {
2265 if (! integer_zerop (const_binop (RSHIFT_EXPR,
2266 convert (unsigned_type, rhs),
2267 size_int (lbitsize))))
2268 {
2269 warning ("comparison is always %s due to width of bitfield",
2270 code == NE_EXPR ? "one" : "zero");
2271 return convert (compare_type,
2272 (code == NE_EXPR
2273 ? integer_one_node : integer_zero_node));
2274 }
2275 }
2276 else
2277 {
2278 tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
2279 size_int (lbitsize - 1));
2280 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
2281 {
2282 warning ("comparison is always %s due to width of bitfield",
2283 code == NE_EXPR ? "one" : "zero");
2284 return convert (compare_type,
2285 (code == NE_EXPR
2286 ? integer_one_node : integer_zero_node));
2287 }
2288 }
2289
2290 /* Single-bit compares should always be against zero. */
2291 if (lbitsize == 1 && ! integer_zerop (rhs))
2292 {
2293 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
2294 rhs = convert (type, integer_zero_node);
2295 }
2296
2297 /* Make a new bitfield reference, shift the constant over the
2298 appropriate number of bits and mask it with the computed mask
2299 (in case this was a signed field). If we changed it, make a new one. */
c0b9d4c8 2300 lhs = make_bit_field_ref (linner, unsigned_type, lnbitsize, lnbitpos, 1);
6d716ca8 2301
c0b9d4c8
RK
2302 rhs = fold (const_binop (BIT_AND_EXPR,
2303 const_binop (LSHIFT_EXPR,
2304 convert (unsigned_type, rhs),
2305 size_int (lbitpos)),
2306 mask));
6d716ca8
RS
2307
2308 return build (code, compare_type,
c0b9d4c8 2309 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
6d716ca8
RS
2310 rhs);
2311}
2312\f
b2215d83 2313/* Subroutine for fold_truthop: decode a field reference.
6d716ca8
RS
2314
2315 If EXP is a comparison reference, we return the innermost reference.
2316
2317 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
2318 set to the starting bit number.
2319
2320 If the innermost field can be completely contained in a mode-sized
2321 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
2322
2323 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
2324 otherwise it is not changed.
2325
2326 *PUNSIGNEDP is set to the signedness of the field.
2327
2328 *PMASK is set to the mask used. This is either contained in a
2329 BIT_AND_EXPR or derived from the width of the field.
2330
2331 Return 0 if this is not a component reference or is one that we can't
2332 do anything with. */
2333
2334static tree
2335decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
2336 pvolatilep, pmask)
2337 tree exp;
2338 int *pbitsize, *pbitpos;
2339 enum machine_mode *pmode;
2340 int *punsignedp, *pvolatilep;
2341 tree *pmask;
2342{
2343 tree mask = 0;
2344 tree inner;
f1e60ec6 2345 tree offset;
6d716ca8
RS
2346
2347 STRIP_NOPS (exp);
2348
2349 if (TREE_CODE (exp) == BIT_AND_EXPR)
2350 {
2351 mask = TREE_OPERAND (exp, 1);
2352 exp = TREE_OPERAND (exp, 0);
2353 STRIP_NOPS (exp); STRIP_NOPS (mask);
2354 if (TREE_CODE (mask) != INTEGER_CST)
2355 return 0;
2356 }
2357
2358 if (TREE_CODE (exp) != COMPONENT_REF && TREE_CODE (exp) != ARRAY_REF
2359 && TREE_CODE (exp) != BIT_FIELD_REF)
2360 return 0;
2361
f1e60ec6 2362 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
6d716ca8 2363 punsignedp, pvolatilep);
f1e60ec6 2364 if (*pbitsize < 0 || offset != 0)
c05a9b68 2365 return 0;
6d716ca8
RS
2366
2367 if (mask == 0)
2368 {
2369 tree unsigned_type = type_for_size (*pbitsize, 1);
2370 int precision = TYPE_PRECISION (unsigned_type);
2371
2372 mask = convert (unsigned_type, build_int_2 (~0, ~0));
2373 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
2374 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
2375 }
2376
2377 *pmask = mask;
2378 return inner;
2379}
2380
6dc42e49 2381/* Return non-zero if MASK represents a mask of SIZE ones in the low-order
6d716ca8
RS
2382 bit positions. */
2383
2384static int
2385all_ones_mask_p (mask, size)
2386 tree mask;
2387 int size;
2388{
2389 tree type = TREE_TYPE (mask);
2390 int precision = TYPE_PRECISION (type);
2391
2392 return
2393 operand_equal_p (mask,
2394 const_binop (RSHIFT_EXPR,
2395 const_binop (LSHIFT_EXPR,
2396 convert (signed_type (type),
2397 build_int_2 (~0, ~0)),
2398 size_int (precision - size)),
2399 size_int (precision - size)), 0);
2400}
b2215d83
TW
2401
2402/* Subroutine for fold_truthop: determine if an operand is simple enough
2403 to be evaluated unconditionally. */
2404
2405#ifdef __GNUC__
2406__inline
2407#endif
2408static int
2409simple_operand_p (exp)
2410 tree exp;
2411{
2412 /* Strip any conversions that don't change the machine mode. */
2413 while ((TREE_CODE (exp) == NOP_EXPR
2414 || TREE_CODE (exp) == CONVERT_EXPR)
2415 && (TYPE_MODE (TREE_TYPE (exp))
2416 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
2417 exp = TREE_OPERAND (exp, 0);
2418
2419 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
2420 || (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd'
2421 && ! TREE_ADDRESSABLE (exp)
2422 && ! TREE_THIS_VOLATILE (exp)
0924ddef 2423 && ! DECL_NONLOCAL (exp)));
b2215d83 2424}
6d716ca8 2425\f
b2215d83 2426/* Subroutine for fold_truthop: try to optimize a range test.
ef659ec0
TW
2427
2428 For example, "i >= 2 && i =< 9" can be done as "(unsigned) (i - 2) <= 7".
2429
9c0ae98b
CH
2430 JCODE is the logical combination of the two terms. It is TRUTH_AND_EXPR
2431 (representing TRUTH_ANDIF_EXPR and TRUTH_AND_EXPR) or TRUTH_OR_EXPR
b2215d83
TW
2432 (representing TRUTH_ORIF_EXPR and TRUTH_OR_EXPR). TYPE is the type of
2433 the result.
ef659ec0
TW
2434
2435 VAR is the value being tested. LO_CODE and HI_CODE are the comparison
2436 operators comparing VAR to LO_CST and HI_CST. LO_CST is known to be no
2437 larger than HI_CST (they may be equal).
2438
2439 We return the simplified tree or 0 if no optimization is possible. */
2440
2441tree
2442range_test (jcode, type, lo_code, hi_code, var, lo_cst, hi_cst)
2443 enum tree_code jcode, lo_code, hi_code;
2444 tree type, var, lo_cst, hi_cst;
2445{
2446 tree utype;
2447 enum tree_code rcode;
2448
2449 /* See if this is a range test and normalize the constant terms. */
2450
9c0ae98b 2451 if (jcode == TRUTH_AND_EXPR)
ef659ec0
TW
2452 {
2453 switch (lo_code)
2454 {
2455 case NE_EXPR:
2456 /* See if we have VAR != CST && VAR != CST+1. */
2457 if (! (hi_code == NE_EXPR
2458 && TREE_INT_CST_LOW (hi_cst) - TREE_INT_CST_LOW (lo_cst) == 1
2459 && tree_int_cst_equal (integer_one_node,
2460 const_binop (MINUS_EXPR,
2461 hi_cst, lo_cst))))
2462 return 0;
2463
2464 rcode = GT_EXPR;
2465 break;
2466
2467 case GT_EXPR:
2468 case GE_EXPR:
2469 if (hi_code == LT_EXPR)
2470 hi_cst = const_binop (MINUS_EXPR, hi_cst, integer_one_node);
2471 else if (hi_code != LE_EXPR)
2472 return 0;
2473
2474 if (lo_code == GT_EXPR)
2475 lo_cst = const_binop (PLUS_EXPR, lo_cst, integer_one_node);
2476
2477 /* We now have VAR >= LO_CST && VAR <= HI_CST. */
2478 rcode = LE_EXPR;
2479 break;
2480
2481 default:
2482 return 0;
2483 }
2484 }
2485 else
2486 {
2487 switch (lo_code)
2488 {
2489 case EQ_EXPR:
2490 /* See if we have VAR == CST || VAR == CST+1. */
2491 if (! (hi_code == EQ_EXPR
2492 && TREE_INT_CST_LOW (hi_cst) - TREE_INT_CST_LOW (lo_cst) == 1
2493 && tree_int_cst_equal (integer_one_node,
2494 const_binop (MINUS_EXPR,
2495 hi_cst, lo_cst))))
2496 return 0;
2497
2498 rcode = LE_EXPR;
2499 break;
2500
2501 case LE_EXPR:
2502 case LT_EXPR:
2503 if (hi_code == GE_EXPR)
2504 hi_cst = const_binop (MINUS_EXPR, hi_cst, integer_one_node);
2505 else if (hi_code != GT_EXPR)
2506 return 0;
2507
2508 if (lo_code == LE_EXPR)
2509 lo_cst = const_binop (PLUS_EXPR, lo_cst, integer_one_node);
2510
2511 /* We now have VAR < LO_CST || VAR > HI_CST. */
2512 rcode = GT_EXPR;
2513 break;
2514
2515 default:
2516 return 0;
2517 }
2518 }
2519
2520 /* When normalizing, it is possible to both increment the smaller constant
2521 and decrement the larger constant. See if they are still ordered. */
f5902869
TW
2522 if (tree_int_cst_lt (hi_cst, lo_cst))
2523 return 0;
2524
2525 /* Fail if VAR isn't an integer. */
2526 utype = TREE_TYPE (var);
2527 if (TREE_CODE (utype) != INTEGER_TYPE
2528 && TREE_CODE (utype) != ENUMERAL_TYPE)
ef659ec0
TW
2529 return 0;
2530
2531 /* The range test is invalid if subtracting the two constants results
2532 in overflow. This can happen in traditional mode. */
2533 if (! int_fits_type_p (hi_cst, TREE_TYPE (var))
2534 || ! int_fits_type_p (lo_cst, TREE_TYPE (var)))
2535 return 0;
2536
ef659ec0
TW
2537 if (! TREE_UNSIGNED (utype))
2538 {
2539 utype = unsigned_type (utype);
2540 var = convert (utype, var);
2541 }
2542
2543 return fold (convert (type,
2544 build (rcode, utype,
2545 build (MINUS_EXPR, utype, var, lo_cst),
2546 const_binop (MINUS_EXPR, hi_cst, lo_cst))));
2547}
2548\f
b2215d83
TW
2549/* Find ways of folding logical expressions of LHS and RHS:
2550 Try to merge two comparisons to the same innermost item.
2551 Look for range tests like "ch >= '0' && ch <= '9'".
2552 Look for combinations of simple terms on machines with expensive branches
2553 and evaluate the RHS unconditionally.
6d716ca8
RS
2554
2555 For example, if we have p->a == 2 && p->b == 4 and we can make an
2556 object large enough to span both A and B, we can do this with a comparison
2557 against the object ANDed with the a mask.
2558
2559 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
2560 operations to do this with one comparison.
2561
2562 We check for both normal comparisons and the BIT_AND_EXPRs made this by
2563 function and the one above.
2564
2565 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
2566 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
2567
2568 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
2569 two operands.
2570
2571 We return the simplified tree or 0 if no optimization is possible. */
2572
2573static tree
b2215d83 2574fold_truthop (code, truth_type, lhs, rhs)
6d716ca8
RS
2575 enum tree_code code;
2576 tree truth_type, lhs, rhs;
2577{
2578 /* If this is the "or" of two comparisons, we can do something if we
2579 the comparisons are NE_EXPR. If this is the "and", we can do something
2580 if the comparisons are EQ_EXPR. I.e.,
2581 (a->b == 2 && a->c == 4) can become (a->new == NEW).
2582
2583 WANTED_CODE is this operation code. For single bit fields, we can
2584 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
2585 comparison for one-bit fields. */
2586
b2215d83 2587 enum tree_code wanted_code;
6d716ca8 2588 enum tree_code lcode, rcode;
b2215d83 2589 tree ll_arg, lr_arg, rl_arg, rr_arg;
6d716ca8
RS
2590 tree ll_inner, lr_inner, rl_inner, rr_inner;
2591 int ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
2592 int rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
2593 int xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
2594 int lnbitsize, lnbitpos, rnbitsize, rnbitpos;
2595 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
2596 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
2597 enum machine_mode lnmode, rnmode;
2598 tree ll_mask, lr_mask, rl_mask, rr_mask;
b2215d83 2599 tree l_const, r_const;
6d716ca8
RS
2600 tree type, result;
2601 int first_bit, end_bit;
b2215d83 2602 int volatilep;
6d716ca8 2603
ef659ec0
TW
2604 /* Start by getting the comparison codes and seeing if this looks like
2605 a range test. Fail if anything is volatile. */
6d716ca8 2606
b2215d83
TW
2607 if (TREE_SIDE_EFFECTS (lhs)
2608 || TREE_SIDE_EFFECTS (rhs))
2609 return 0;
2610
6d716ca8
RS
2611 lcode = TREE_CODE (lhs);
2612 rcode = TREE_CODE (rhs);
ef659ec0 2613
b2215d83 2614 if (TREE_CODE_CLASS (lcode) != '<'
ef659ec0
TW
2615 || TREE_CODE_CLASS (rcode) != '<')
2616 return 0;
2617
b2215d83 2618 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
9c0ae98b 2619 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
b2215d83
TW
2620
2621 ll_arg = TREE_OPERAND (lhs, 0);
2622 lr_arg = TREE_OPERAND (lhs, 1);
2623 rl_arg = TREE_OPERAND (rhs, 0);
2624 rr_arg = TREE_OPERAND (rhs, 1);
2625
2626 if (TREE_CODE (lr_arg) == INTEGER_CST
2627 && TREE_CODE (rr_arg) == INTEGER_CST
2628 && operand_equal_p (ll_arg, rl_arg, 0))
ef659ec0 2629 {
b2215d83 2630 if (tree_int_cst_lt (lr_arg, rr_arg))
ef659ec0 2631 result = range_test (code, truth_type, lcode, rcode,
b2215d83 2632 ll_arg, lr_arg, rr_arg);
ef659ec0
TW
2633 else
2634 result = range_test (code, truth_type, rcode, lcode,
b2215d83 2635 ll_arg, rr_arg, lr_arg);
ef659ec0
TW
2636
2637 /* If this isn't a range test, it also isn't a comparison that
b2215d83
TW
2638 can be merged. However, it wins to evaluate the RHS unconditionally
2639 on machines with expensive branches. */
2640
2641 if (result == 0 && BRANCH_COST >= 2)
2642 {
2643 if (TREE_CODE (ll_arg) != VAR_DECL
2644 && TREE_CODE (ll_arg) != PARM_DECL)
2645 {
2646 /* Avoid evaluating the variable part twice. */
2647 ll_arg = save_expr (ll_arg);
2648 lhs = build (lcode, TREE_TYPE (lhs), ll_arg, lr_arg);
2649 rhs = build (rcode, TREE_TYPE (rhs), ll_arg, rr_arg);
2650 }
2651 return build (code, truth_type, lhs, rhs);
2652 }
ef659ec0
TW
2653 return result;
2654 }
2655
b2215d83
TW
2656 /* If the RHS can be evaluated unconditionally and all operands are
2657 simple, it wins to evaluate the RHS unconditionally on machines
2658 with expensive branches. In this case, this isn't a comparison
2659 that can be merged. */
2660
2661 /* @@ I'm not sure it wins on the m88110 to do this if the comparisons
2662 are with zero (tmw). */
2663
2664 if (BRANCH_COST >= 2
2665 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
2666 && simple_operand_p (rl_arg)
2667 && simple_operand_p (ll_arg)
2668 && simple_operand_p (rr_arg)
2669 && simple_operand_p (lr_arg))
2670 return build (code, truth_type, lhs, rhs);
2671
ef659ec0
TW
2672 /* See if the comparisons can be merged. Then get all the parameters for
2673 each side. */
2674
6d716ca8 2675 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
ef659ec0 2676 || (rcode != EQ_EXPR && rcode != NE_EXPR))
6d716ca8
RS
2677 return 0;
2678
b2215d83
TW
2679 volatilep = 0;
2680 ll_inner = decode_field_reference (ll_arg,
6d716ca8
RS
2681 &ll_bitsize, &ll_bitpos, &ll_mode,
2682 &ll_unsignedp, &volatilep, &ll_mask);
b2215d83 2683 lr_inner = decode_field_reference (lr_arg,
6d716ca8
RS
2684 &lr_bitsize, &lr_bitpos, &lr_mode,
2685 &lr_unsignedp, &volatilep, &lr_mask);
b2215d83 2686 rl_inner = decode_field_reference (rl_arg,
6d716ca8
RS
2687 &rl_bitsize, &rl_bitpos, &rl_mode,
2688 &rl_unsignedp, &volatilep, &rl_mask);
b2215d83 2689 rr_inner = decode_field_reference (rr_arg,
6d716ca8
RS
2690 &rr_bitsize, &rr_bitpos, &rr_mode,
2691 &rr_unsignedp, &volatilep, &rr_mask);
2692
2693 /* It must be true that the inner operation on the lhs of each
2694 comparison must be the same if we are to be able to do anything.
2695 Then see if we have constants. If not, the same must be true for
2696 the rhs's. */
2697 if (volatilep || ll_inner == 0 || rl_inner == 0
2698 || ! operand_equal_p (ll_inner, rl_inner, 0))
2699 return 0;
2700
b2215d83
TW
2701 if (TREE_CODE (lr_arg) == INTEGER_CST
2702 && TREE_CODE (rr_arg) == INTEGER_CST)
2703 l_const = lr_arg, r_const = rr_arg;
6d716ca8
RS
2704 else if (lr_inner == 0 || rr_inner == 0
2705 || ! operand_equal_p (lr_inner, rr_inner, 0))
2706 return 0;
b2215d83
TW
2707 else
2708 l_const = r_const = 0;
6d716ca8
RS
2709
2710 /* If either comparison code is not correct for our logical operation,
2711 fail. However, we can convert a one-bit comparison against zero into
2712 the opposite comparison against that bit being set in the field. */
b2215d83 2713
9c0ae98b 2714 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
6d716ca8
RS
2715 if (lcode != wanted_code)
2716 {
2717 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
2718 l_const = ll_mask;
2719 else
2720 return 0;
2721 }
2722
2723 if (rcode != wanted_code)
2724 {
2725 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
2726 r_const = rl_mask;
2727 else
2728 return 0;
2729 }
2730
2731 /* See if we can find a mode that contains both fields being compared on
2732 the left. If we can't, fail. Otherwise, update all constants and masks
2733 to be relative to a field of that size. */
2734 first_bit = MIN (ll_bitpos, rl_bitpos);
2735 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
2736 lnmode = get_best_mode (end_bit - first_bit, first_bit,
2737 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
2738 volatilep);
2739 if (lnmode == VOIDmode)
2740 return 0;
2741
2742 lnbitsize = GET_MODE_BITSIZE (lnmode);
2743 lnbitpos = first_bit & ~ (lnbitsize - 1);
2744 type = type_for_size (lnbitsize, 1);
2745 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
2746
2747#if BYTES_BIG_ENDIAN
2748 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
2749 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
2750#endif
2751
2752 ll_mask = const_binop (LSHIFT_EXPR, convert (type, ll_mask),
2753 size_int (xll_bitpos));
2754 rl_mask = const_binop (LSHIFT_EXPR, convert (type, rl_mask),
2755 size_int (xrl_bitpos));
2756
2757 /* Make sure the constants are interpreted as unsigned, so we
2758 don't have sign bits outside the range of their type. */
2759
2760 if (l_const)
2761 {
2762 l_const = convert (unsigned_type (TREE_TYPE (l_const)), l_const);
2763 l_const = const_binop (LSHIFT_EXPR, convert (type, l_const),
2764 size_int (xll_bitpos));
2765 }
2766 if (r_const)
2767 {
2768 r_const = convert (unsigned_type (TREE_TYPE (r_const)), r_const);
2769 r_const = const_binop (LSHIFT_EXPR, convert (type, r_const),
2770 size_int (xrl_bitpos));
2771 }
2772
2773 /* If the right sides are not constant, do the same for it. Also,
2774 disallow this optimization if a size or signedness mismatch occurs
2775 between the left and right sides. */
2776 if (l_const == 0)
2777 {
2778 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
e6a28f26
RS
2779 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
2780 /* Make sure the two fields on the right
2781 correspond to the left without being swapped. */
2782 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
6d716ca8
RS
2783 return 0;
2784
2785 first_bit = MIN (lr_bitpos, rr_bitpos);
2786 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
2787 rnmode = get_best_mode (end_bit - first_bit, first_bit,
2788 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
2789 volatilep);
2790 if (rnmode == VOIDmode)
2791 return 0;
2792
2793 rnbitsize = GET_MODE_BITSIZE (rnmode);
2794 rnbitpos = first_bit & ~ (rnbitsize - 1);
2795 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
2796
2797#if BYTES_BIG_ENDIAN
2798 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
2799 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
2800#endif
2801
2802 lr_mask = const_binop (LSHIFT_EXPR, convert (type, lr_mask),
2803 size_int (xlr_bitpos));
2804 rr_mask = const_binop (LSHIFT_EXPR, convert (type, rr_mask),
2805 size_int (xrr_bitpos));
2806
2807 /* Make a mask that corresponds to both fields being compared.
2808 Do this for both items being compared. If the masks agree,
2809 we can do this by masking both and comparing the masked
2810 results. */
2811 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
2812 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
2813 if (operand_equal_p (ll_mask, lr_mask, 0) && lnbitsize == rnbitsize)
2814 {
2815 lhs = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
2816 ll_unsignedp || rl_unsignedp);
2817 rhs = make_bit_field_ref (lr_inner, type, rnbitsize, rnbitpos,
2818 lr_unsignedp || rr_unsignedp);
2819 if (! all_ones_mask_p (ll_mask, lnbitsize))
2820 {
2821 lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
2822 rhs = build (BIT_AND_EXPR, type, rhs, ll_mask);
2823 }
2824 return build (wanted_code, truth_type, lhs, rhs);
2825 }
2826
2827 /* There is still another way we can do something: If both pairs of
2828 fields being compared are adjacent, we may be able to make a wider
2829 field containing them both. */
2830 if ((ll_bitsize + ll_bitpos == rl_bitpos
2831 && lr_bitsize + lr_bitpos == rr_bitpos)
2832 || (ll_bitpos == rl_bitpos + rl_bitsize
2833 && lr_bitpos == rr_bitpos + rr_bitsize))
2834 return build (wanted_code, truth_type,
2835 make_bit_field_ref (ll_inner, type,
2836 ll_bitsize + rl_bitsize,
2837 MIN (ll_bitpos, rl_bitpos),
2838 ll_unsignedp),
2839 make_bit_field_ref (lr_inner, type,
2840 lr_bitsize + rr_bitsize,
2841 MIN (lr_bitpos, rr_bitpos),
2842 lr_unsignedp));
2843
2844 return 0;
2845 }
2846
2847 /* Handle the case of comparisons with constants. If there is something in
2848 common between the masks, those bits of the constants must be the same.
2849 If not, the condition is always false. Test for this to avoid generating
2850 incorrect code below. */
2851 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
2852 if (! integer_zerop (result)
2853 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
2854 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
2855 {
2856 if (wanted_code == NE_EXPR)
2857 {
2858 warning ("`or' of unmatched not-equal tests is always 1");
2859 return convert (truth_type, integer_one_node);
2860 }
2861 else
2862 {
2863 warning ("`and' of mutually exclusive equal-tests is always zero");
2864 return convert (truth_type, integer_zero_node);
2865 }
2866 }
2867
2868 /* Construct the expression we will return. First get the component
2869 reference we will make. Unless the mask is all ones the width of
2870 that field, perform the mask operation. Then compare with the
2871 merged constant. */
2872 result = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
2873 ll_unsignedp || rl_unsignedp);
2874
2875 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
2876 if (! all_ones_mask_p (ll_mask, lnbitsize))
2877 result = build (BIT_AND_EXPR, type, result, ll_mask);
2878
2879 return build (wanted_code, truth_type, result,
2880 const_binop (BIT_IOR_EXPR, l_const, r_const));
2881}
2882\f
2883/* Perform constant folding and related simplification of EXPR.
2884 The related simplifications include x*1 => x, x*0 => 0, etc.,
2885 and application of the associative law.
2886 NOP_EXPR conversions may be removed freely (as long as we
2887 are careful not to change the C type of the overall expression)
2888 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
2889 but we can constant-fold them if they have constant operands. */
2890
2891tree
2892fold (expr)
2893 tree expr;
2894{
2895 register tree t = expr;
2896 tree t1 = NULL_TREE;
c05a9b68 2897 tree tem;
6d716ca8
RS
2898 tree type = TREE_TYPE (expr);
2899 register tree arg0, arg1;
2900 register enum tree_code code = TREE_CODE (t);
2901 register int kind;
c05a9b68 2902 int invert;
6d716ca8
RS
2903
2904 /* WINS will be nonzero when the switch is done
2905 if all operands are constant. */
2906
2907 int wins = 1;
2908
2909 /* Return right away if already constant. */
2910 if (TREE_CONSTANT (t))
2911 {
2912 if (code == CONST_DECL)
2913 return DECL_INITIAL (t);
2914 return t;
2915 }
2916
2917 kind = TREE_CODE_CLASS (code);
1b81aa14
RS
2918 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
2919 {
2920 /* Special case for conversion ops that can have fixed point args. */
2921 arg0 = TREE_OPERAND (t, 0);
2922
2923 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
2924 if (arg0 != 0)
2925 STRIP_TYPE_NOPS (arg0);
2926
2927 if (arg0 != 0 && TREE_CODE (arg0) != INTEGER_CST
2928#if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2929 && TREE_CODE (arg0) != REAL_CST
2930#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2931 )
2932 /* Note that TREE_CONSTANT isn't enough:
2933 static var addresses are constant but we can't
2934 do arithmetic on them. */
2935 wins = 0;
2936 }
2937 else if (kind == 'e' || kind == '<'
2938 || kind == '1' || kind == '2' || kind == 'r')
6d716ca8
RS
2939 {
2940 register int len = tree_code_length[(int) code];
2941 register int i;
2942 for (i = 0; i < len; i++)
2943 {
2944 tree op = TREE_OPERAND (t, i);
2945
2946 if (op == 0)
2947 continue; /* Valid for CALL_EXPR, at least. */
2948
2949 /* Strip any conversions that don't change the mode. */
2950 STRIP_NOPS (op);
2951
2952 if (TREE_CODE (op) != INTEGER_CST
2953#if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2954 && TREE_CODE (op) != REAL_CST
2955#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2956 )
2957 /* Note that TREE_CONSTANT isn't enough:
2958 static var addresses are constant but we can't
2959 do arithmetic on them. */
2960 wins = 0;
2961
2962 if (i == 0)
2963 arg0 = op;
2964 else if (i == 1)
2965 arg1 = op;
2966 }
2967 }
2968
2969 /* If this is a commutative operation, and ARG0 is a constant, move it
2970 to ARG1 to reduce the number of tests below. */
2971 if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
2972 || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
2973 || code == BIT_AND_EXPR)
2974 && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
2975 {
c05a9b68 2976 tem = arg0; arg0 = arg1; arg1 = tem;
6d716ca8 2977
c05a9b68
RS
2978 tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
2979 TREE_OPERAND (t, 1) = tem;
6d716ca8
RS
2980 }
2981
2982 /* Now WINS is set as described above,
2983 ARG0 is the first operand of EXPR,
2984 and ARG1 is the second operand (if it has more than one operand).
2985
2986 First check for cases where an arithmetic operation is applied to a
2987 compound, conditional, or comparison operation. Push the arithmetic
2988 operation inside the compound or conditional to see if any folding
2989 can then be done. Convert comparison to conditional for this purpose.
2990 The also optimizes non-constant cases that used to be done in
2991 expand_expr. */
2992 if (TREE_CODE_CLASS (code) == '1')
2993 {
2994 if (TREE_CODE (arg0) == COMPOUND_EXPR)
2995 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
2996 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
2997 else if (TREE_CODE (arg0) == COND_EXPR)
b8eb43a2
RK
2998 {
2999 t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
3000 fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
3001 fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
3002
3003 /* If this was a conversion, and all we did was to move into
3004 inside the COND_EXPR, bring it back out. Then return so we
3005 don't get into an infinite recursion loop taking the conversion
3006 out and then back in. */
3007
3008 if ((code == NOP_EXPR || code == CONVERT_EXPR
3009 || code == NON_LVALUE_EXPR)
3010 && TREE_CODE (t) == COND_EXPR
3011 && TREE_CODE (TREE_OPERAND (t, 1)) == code
3012 && TREE_CODE (TREE_OPERAND (t, 2)) == code)
3013 t = build1 (code, type,
3014 build (COND_EXPR,
3015 TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)),
3016 TREE_OPERAND (t, 0),
3017 TREE_OPERAND (TREE_OPERAND (t, 1), 0),
3018 TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
3019 return t;
3020 }
6d716ca8
RS
3021 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
3022 return fold (build (COND_EXPR, type, arg0,
3023 fold (build1 (code, type, integer_one_node)),
3024 fold (build1 (code, type, integer_zero_node))));
3025 }
3026 else if (TREE_CODE_CLASS (code) == '2')
3027 {
3028 if (TREE_CODE (arg1) == COMPOUND_EXPR)
3029 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
3030 fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
3031 else if (TREE_CODE (arg1) == COND_EXPR
3032 || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<')
3033 {
3034 tree test, true_value, false_value;
3035
3036 if (TREE_CODE (arg1) == COND_EXPR)
3037 {
3038 test = TREE_OPERAND (arg1, 0);
3039 true_value = TREE_OPERAND (arg1, 1);
3040 false_value = TREE_OPERAND (arg1, 2);
3041 }
3042 else
3043 {
3044 test = arg1;
3045 true_value = integer_one_node;
3046 false_value = integer_zero_node;
3047 }
3048
3049 if (TREE_CODE (arg0) != VAR_DECL && TREE_CODE (arg0) != PARM_DECL)
3050 arg0 = save_expr (arg0);
3051 test = fold (build (COND_EXPR, type, test,
3052 fold (build (code, type, arg0, true_value)),
3053 fold (build (code, type, arg0, false_value))));
3054 if (TREE_CODE (arg0) == SAVE_EXPR)
3055 return build (COMPOUND_EXPR, type,
3056 convert (void_type_node, arg0), test);
3057 else
3058 return convert (type, test);
3059 }
3060
3061 else if (TREE_CODE (arg0) == COMPOUND_EXPR)
3062 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
3063 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
3064 else if (TREE_CODE (arg0) == COND_EXPR
3065 || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
3066 {
3067 tree test, true_value, false_value;
3068
3069 if (TREE_CODE (arg0) == COND_EXPR)
3070 {
3071 test = TREE_OPERAND (arg0, 0);
3072 true_value = TREE_OPERAND (arg0, 1);
3073 false_value = TREE_OPERAND (arg0, 2);
3074 }
3075 else
3076 {
3077 test = arg0;
3078 true_value = integer_one_node;
3079 false_value = integer_zero_node;
3080 }
3081
3082 if (TREE_CODE (arg1) != VAR_DECL && TREE_CODE (arg1) != PARM_DECL)
3083 arg1 = save_expr (arg1);
3084 test = fold (build (COND_EXPR, type, test,
3085 fold (build (code, type, true_value, arg1)),
3086 fold (build (code, type, false_value, arg1))));
3087 if (TREE_CODE (arg1) == SAVE_EXPR)
3088 return build (COMPOUND_EXPR, type,
3089 convert (void_type_node, arg1), test);
3090 else
3091 return convert (type, test);
3092 }
3093 }
c05a9b68
RS
3094 else if (TREE_CODE_CLASS (code) == '<'
3095 && TREE_CODE (arg0) == COMPOUND_EXPR)
3096 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
3097 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
3098 else if (TREE_CODE_CLASS (code) == '<'
3099 && TREE_CODE (arg1) == COMPOUND_EXPR)
3100 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
3101 fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
6d716ca8
RS
3102
3103 switch (code)
3104 {
3105 case INTEGER_CST:
3106 case REAL_CST:
3107 case STRING_CST:
3108 case COMPLEX_CST:
3109 case CONSTRUCTOR:
3110 return t;
3111
3112 case CONST_DECL:
3113 return fold (DECL_INITIAL (t));
3114
3115 case NOP_EXPR:
3116 case FLOAT_EXPR:
3117 case CONVERT_EXPR:
3118 case FIX_TRUNC_EXPR:
3119 /* Other kinds of FIX are not handled properly by fold_convert. */
3120 /* Two conversions in a row are not needed unless:
3121 - the intermediate type is narrower than both initial and final, or
68d88491
RS
3122 - the intermediate type and innermost type differ in signedness,
3123 and the outermost type is wider than the intermediate, or
6d716ca8
RS
3124 - the initial type is a pointer type and the precisions of the
3125 intermediate and final types differ, or
3126 - the final type is a pointer type and the precisions of the
3127 initial and intermediate types differ. */
3128 if ((TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
3129 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
3130 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3131 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3132 ||
3133 TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3134 > TYPE_PRECISION (TREE_TYPE (t)))
68d88491
RS
3135 && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3136 == INTEGER_TYPE)
3137 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
3138 == INTEGER_TYPE)
3139 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t, 0)))
3140 != TREE_UNSIGNED (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3141 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3142 < TYPE_PRECISION (TREE_TYPE (t))))
6d716ca8
RS
3143 && ((TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t, 0)))
3144 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3145 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))))
3146 ==
3147 (TREE_UNSIGNED (TREE_TYPE (t))
3148 && (TYPE_PRECISION (TREE_TYPE (t))
3149 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))))))
3150 && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3151 == POINTER_TYPE)
3152 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3153 != TYPE_PRECISION (TREE_TYPE (t))))
3154 && ! (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
3155 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3156 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))))))
3157 return convert (TREE_TYPE (t), TREE_OPERAND (TREE_OPERAND (t, 0), 0));
3158
3159 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
d8f6dbb9
RS
3160 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
3161 /* Detect assigning a bitfield. */
3162 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
3163 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
6d716ca8 3164 {
d8f6dbb9 3165 /* Don't leave an assignment inside a conversion
f72aed24 3166 unless assigning a bitfield. */
6d716ca8
RS
3167 tree prev = TREE_OPERAND (t, 0);
3168 TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
3169 /* First do the assignment, then return converted constant. */
3170 t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
3171 TREE_USED (t) = 1;
3172 return t;
3173 }
3174 if (!wins)
3175 {
3176 TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
3177 return t;
3178 }
3179 return fold_convert (t, arg0);
3180
3181#if 0 /* This loses on &"foo"[0]. */
3182 case ARRAY_REF:
3183 {
3184 int i;
3185
3186 /* Fold an expression like: "foo"[2] */
3187 if (TREE_CODE (arg0) == STRING_CST
3188 && TREE_CODE (arg1) == INTEGER_CST
3189 && !TREE_INT_CST_HIGH (arg1)
3190 && (i = TREE_INT_CST_LOW (arg1)) < TREE_STRING_LENGTH (arg0))
3191 {
3192 t = build_int_2 (TREE_STRING_POINTER (arg0)[i], 0);
3193 TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
3194 force_fit_type (t);
3195 }
3196 }
3197 return t;
3198#endif /* 0 */
3199
3200 case RANGE_EXPR:
3201 TREE_CONSTANT (t) = wins;
3202 return t;
3203
3204 case NEGATE_EXPR:
3205 if (wins)
3206 {
3207 if (TREE_CODE (arg0) == INTEGER_CST)
3208 {
fe3e8e40
RS
3209 HOST_WIDE_INT low, high;
3210 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
3211 TREE_INT_CST_HIGH (arg0),
3212 &low, &high);
3213 t = build_int_2 (low, high);
3214 TREE_CONSTANT_OVERFLOW (t)
3215 = overflow | TREE_CONSTANT_OVERFLOW (arg0);
6d716ca8
RS
3216 TREE_TYPE (t) = type;
3217 force_fit_type (t);
3218 }
3219 else if (TREE_CODE (arg0) == REAL_CST)
3220 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
3221 TREE_TYPE (t) = type;
3222 }
3223 else if (TREE_CODE (arg0) == NEGATE_EXPR)
3224 return TREE_OPERAND (arg0, 0);
3225
3226 /* Convert - (a - b) to (b - a) for non-floating-point. */
3227 else if (TREE_CODE (arg0) == MINUS_EXPR && TREE_CODE (type) != REAL_TYPE)
3228 return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
3229 TREE_OPERAND (arg0, 0));
3230
3231 return t;
3232
3233 case ABS_EXPR:
3234 if (wins)
3235 {
3236 if (TREE_CODE (arg0) == INTEGER_CST)
3237 {
3238 if (! TREE_UNSIGNED (type)
3239 && TREE_INT_CST_HIGH (arg0) < 0)
3240 {
3241 if (TREE_INT_CST_LOW (arg0) == 0)
3242 t = build_int_2 (0, - TREE_INT_CST_HIGH (arg0));
3243 else
3244 t = build_int_2 (- TREE_INT_CST_LOW (arg0),
3245 ~ TREE_INT_CST_HIGH (arg0));
3246 }
3247 }
3248 else if (TREE_CODE (arg0) == REAL_CST)
3249 {
c05a9b68 3250 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
6d716ca8
RS
3251 t = build_real (type,
3252 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
3253 }
3254 TREE_TYPE (t) = type;
3255 }
3256 else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
3257 return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
3258 return t;
3259
3260 case BIT_NOT_EXPR:
3261 if (wins)
3262 {
3263 if (TREE_CODE (arg0) == INTEGER_CST)
3264 t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
3265 ~ TREE_INT_CST_HIGH (arg0));
3266 TREE_TYPE (t) = type;
3267 force_fit_type (t);
fe3e8e40 3268 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
6d716ca8
RS
3269 }
3270 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
3271 return TREE_OPERAND (arg0, 0);
3272 return t;
3273
3274 case PLUS_EXPR:
3275 /* A + (-B) -> A - B */
3276 if (TREE_CODE (arg1) == NEGATE_EXPR)
3277 return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
3278 else if (TREE_CODE (type) != REAL_TYPE)
3279 {
3280 if (integer_zerop (arg1))
3281 return non_lvalue (convert (type, arg0));
3282
3283 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
3284 with a constant, and the two constants have no bits in common,
3285 we should treat this as a BIT_IOR_EXPR since this may produce more
3286 simplifications. */
3287 if (TREE_CODE (arg0) == BIT_AND_EXPR
3288 && TREE_CODE (arg1) == BIT_AND_EXPR
3289 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3290 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3291 && integer_zerop (const_binop (BIT_AND_EXPR,
3292 TREE_OPERAND (arg0, 1),
3293 TREE_OPERAND (arg1, 1))))
3294 {
3295 code = BIT_IOR_EXPR;
3296 goto bit_ior;
3297 }
3298 }
3299 /* In IEEE floating point, x+0 may not equal x. */
3300 else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3301 && real_zerop (arg1))
3302 return non_lvalue (convert (type, arg0));
3303 associate:
3304 /* In most languages, can't associate operations on floats
3305 through parentheses. Rather than remember where the parentheses
3306 were, we don't associate floats at all. It shouldn't matter much. */
3307 if (TREE_CODE (type) == REAL_TYPE)
3308 goto binary;
3309 /* The varsign == -1 cases happen only for addition and subtraction.
3310 It says that the arg that was split was really CON minus VAR.
3311 The rest of the code applies to all associative operations. */
3312 if (!wins)
3313 {
c05a9b68 3314 tree var, con;
6d716ca8
RS
3315 int varsign;
3316
3317 if (split_tree (arg0, code, &var, &con, &varsign))
3318 {
3319 if (varsign == -1)
3320 {
3321 /* EXPR is (CON-VAR) +- ARG1. */
3322 /* If it is + and VAR==ARG1, return just CONST. */
3323 if (code == PLUS_EXPR && operand_equal_p (var, arg1, 0))
3324 return convert (TREE_TYPE (t), con);
3325
3326 /* Otherwise return (CON +- ARG1) - VAR. */
3327 TREE_SET_CODE (t, MINUS_EXPR);
3328 TREE_OPERAND (t, 1) = var;
3329 TREE_OPERAND (t, 0)
3330 = fold (build (code, TREE_TYPE (t), con, arg1));
3331 }
3332 else
3333 {
3334 /* EXPR is (VAR+CON) +- ARG1. */
3335 /* If it is - and VAR==ARG1, return just CONST. */
3336 if (code == MINUS_EXPR && operand_equal_p (var, arg1, 0))
3337 return convert (TREE_TYPE (t), con);
3338
3339 /* Otherwise return VAR +- (ARG1 +- CON). */
3340 TREE_OPERAND (t, 1) = tem
3341 = fold (build (code, TREE_TYPE (t), arg1, con));
3342 TREE_OPERAND (t, 0) = var;
3343 if (integer_zerop (tem)
3344 && (code == PLUS_EXPR || code == MINUS_EXPR))
3345 return convert (type, var);
3346 /* If we have x +/- (c - d) [c an explicit integer]
3347 change it to x -/+ (d - c) since if d is relocatable
3348 then the latter can be a single immediate insn
3349 and the former cannot. */
3350 if (TREE_CODE (tem) == MINUS_EXPR
3351 && TREE_CODE (TREE_OPERAND (tem, 0)) == INTEGER_CST)
3352 {
3353 tree tem1 = TREE_OPERAND (tem, 1);
3354 TREE_OPERAND (tem, 1) = TREE_OPERAND (tem, 0);
3355 TREE_OPERAND (tem, 0) = tem1;
3356 TREE_SET_CODE (t,
3357 (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
3358 }
3359 }
3360 return t;
3361 }
3362
3363 if (split_tree (arg1, code, &var, &con, &varsign))
3364 {
3365 /* EXPR is ARG0 +- (CON +- VAR). */
3366 if (varsign == -1)
3367 TREE_SET_CODE (t,
3368 (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
3369 if (TREE_CODE (t) == MINUS_EXPR
3370 && operand_equal_p (var, arg0, 0))
3371 {
3372 /* If VAR and ARG0 cancel, return just CON or -CON. */
3373 if (code == PLUS_EXPR)
3374 return convert (TREE_TYPE (t), con);
3375 return fold (build1 (NEGATE_EXPR, TREE_TYPE (t),
3376 convert (TREE_TYPE (t), con)));
3377 }
3378 TREE_OPERAND (t, 0)
3379 = fold (build (code, TREE_TYPE (t), arg0, con));
3380 TREE_OPERAND (t, 1) = var;
3381 if (integer_zerop (TREE_OPERAND (t, 0))
3382 && TREE_CODE (t) == PLUS_EXPR)
3383 return convert (TREE_TYPE (t), var);
3384 return t;
3385 }
3386 }
3387 binary:
3388#if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
3389 if (TREE_CODE (arg1) == REAL_CST)
3390 return t;
3391#endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
3392 if (wins)
3393 t1 = const_binop (code, arg0, arg1);
3394 if (t1 != NULL_TREE)
3395 {
3396 /* The return value should always have
3397 the same type as the original expression. */
3398 TREE_TYPE (t1) = TREE_TYPE (t);
3399 return t1;
3400 }
3401 return t;
3402
3403 case MINUS_EXPR:
3404 if (TREE_CODE (type) != REAL_TYPE)
3405 {
3406 if (! wins && integer_zerop (arg0))
3407 return build1 (NEGATE_EXPR, type, arg1);
3408 if (integer_zerop (arg1))
3409 return non_lvalue (convert (type, arg0));
3410 }
3411 /* Convert A - (-B) to A + B. */
3412 else if (TREE_CODE (arg1) == NEGATE_EXPR)
3413 return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
c05a9b68 3414 else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
6d716ca8 3415 {
c05a9b68 3416 /* Except with IEEE floating point, 0-x equals -x. */
6d716ca8
RS
3417 if (! wins && real_zerop (arg0))
3418 return build1 (NEGATE_EXPR, type, arg1);
c05a9b68
RS
3419 /* Except with IEEE floating point, x-0 equals x. */
3420 if (real_zerop (arg1))
6d716ca8 3421 return non_lvalue (convert (type, arg0));
a6acbe15
RS
3422
3423 /* Fold &x - &x. This can happen from &x.foo - &x.
3424 This is unsafe for certain floats even in non-IEEE formats.
3425 In IEEE, it is unsafe because it does wrong for NaNs.
6a1746af 3426 Also note that operand_equal_p is always false if an operand
a6acbe15
RS
3427 is volatile. */
3428
3429 if (operand_equal_p (arg0, arg1,
3430 TREE_CODE (type) == REAL_TYPE))
3431 return convert (type, integer_zero_node);
6d716ca8 3432 }
6d716ca8
RS
3433 goto associate;
3434
3435 case MULT_EXPR:
3436 if (TREE_CODE (type) != REAL_TYPE)
3437 {
3438 if (integer_zerop (arg1))
3439 return omit_one_operand (type, arg1, arg0);
3440 if (integer_onep (arg1))
3441 return non_lvalue (convert (type, arg0));
3442
3443 /* (a * (1 << b)) is (a << b) */
3444 if (TREE_CODE (arg1) == LSHIFT_EXPR
3445 && integer_onep (TREE_OPERAND (arg1, 0)))
3446 return fold (build (LSHIFT_EXPR, type, arg0,
3447 TREE_OPERAND (arg1, 1)));
3448 if (TREE_CODE (arg0) == LSHIFT_EXPR
3449 && integer_onep (TREE_OPERAND (arg0, 0)))
3450 return fold (build (LSHIFT_EXPR, type, arg1,
3451 TREE_OPERAND (arg0, 1)));
3452 }
6d716ca8
RS
3453 else
3454 {
c05a9b68 3455 /* x*0 is 0, except for IEEE floating point. */
6d716ca8
RS
3456 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3457 && real_zerop (arg1))
3458 return omit_one_operand (type, arg1, arg0);
c05a9b68 3459 /* In IEEE floating point, x*1 is not equivalent to x for snans.
6d716ca8
RS
3460 However, ANSI says we can drop signals,
3461 so we can do this anyway. */
3462 if (real_onep (arg1))
3463 return non_lvalue (convert (type, arg0));
3464 /* x*2 is x+x */
3465 if (! wins && real_twop (arg1))
3466 {
3467 tree arg = save_expr (arg0);
3468 return build (PLUS_EXPR, type, arg, arg);
3469 }
3470 }
3471 goto associate;
3472
3473 case BIT_IOR_EXPR:
3474 bit_ior:
3475 if (integer_all_onesp (arg1))
3476 return omit_one_operand (type, arg1, arg0);
3477 if (integer_zerop (arg1))
3478 return non_lvalue (convert (type, arg0));
3479 t1 = distribute_bit_expr (code, type, arg0, arg1);
3480 if (t1 != NULL_TREE)
3481 return t1;
3482 goto associate;
3483
3484 case BIT_XOR_EXPR:
3485 if (integer_zerop (arg1))
3486 return non_lvalue (convert (type, arg0));
3487 if (integer_all_onesp (arg1))
3488 return fold (build1 (BIT_NOT_EXPR, type, arg0));
3489 goto associate;
3490
3491 case BIT_AND_EXPR:
3492 bit_and:
3493 if (integer_all_onesp (arg1))
3494 return non_lvalue (convert (type, arg0));
3495 if (integer_zerop (arg1))
3496 return omit_one_operand (type, arg1, arg0);
3497 t1 = distribute_bit_expr (code, type, arg0, arg1);
3498 if (t1 != NULL_TREE)
3499 return t1;
3500 /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */
3501 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
3502 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
3503 {
3504 int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
906c4e36
RK
3505 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
3506 && (~TREE_INT_CST_LOW (arg0)
3507 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
6d716ca8
RS
3508 return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
3509 }
3510 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
3511 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
3512 {
3513 int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
906c4e36
RK
3514 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
3515 && (~TREE_INT_CST_LOW (arg1)
3516 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
6d716ca8
RS
3517 return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
3518 }
3519 goto associate;
3520
3521 case BIT_ANDTC_EXPR:
3522 if (integer_all_onesp (arg0))
3523 return non_lvalue (convert (type, arg1));
3524 if (integer_zerop (arg0))
3525 return omit_one_operand (type, arg0, arg1);
3526 if (TREE_CODE (arg1) == INTEGER_CST)
3527 {
3528 arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
3529 code = BIT_AND_EXPR;
3530 goto bit_and;
3531 }
3532 goto binary;
3533
3534 case TRUNC_DIV_EXPR:
3535 case ROUND_DIV_EXPR:
3536 case FLOOR_DIV_EXPR:
3537 case CEIL_DIV_EXPR:
3538 case EXACT_DIV_EXPR:
3539 case RDIV_EXPR:
3540 if (integer_onep (arg1))
3541 return non_lvalue (convert (type, arg0));
3542 if (integer_zerop (arg1))
3543 return t;
3b998c11
RK
3544
3545 /* If we have ((a * C1) / C2) and C1 % C2 == 0, we can replace this with
3546 (a * (C1/C2). Also look for when we have a SAVE_EXPR in
3547 between. */
3548 if (TREE_CODE (arg1) == INTEGER_CST
3549 && TREE_INT_CST_LOW (arg1) > 0 && TREE_INT_CST_HIGH (arg1) == 0
3550 && TREE_CODE (arg0) == MULT_EXPR
3551 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3552 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) > 0
3553 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
3554 && 0 == (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
3555 % TREE_INT_CST_LOW (arg1)))
3556 {
3557 tree new_op
3558 = build_int_2 (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
906c4e36 3559 / TREE_INT_CST_LOW (arg1), 0);
3b998c11
RK
3560
3561 TREE_TYPE (new_op) = type;
3562 return build (MULT_EXPR, type, TREE_OPERAND (arg0, 0), new_op);
3563 }
3564
3565 else if (TREE_CODE (arg1) == INTEGER_CST
3566 && TREE_INT_CST_LOW (arg1) > 0 && TREE_INT_CST_HIGH (arg1) == 0
3567 && TREE_CODE (arg0) == SAVE_EXPR
3568 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MULT_EXPR
3569 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
3570 == INTEGER_CST)
3571 && (TREE_INT_CST_LOW (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
3572 > 0)
3573 && (TREE_INT_CST_HIGH (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
3574 == 0)
3575 && (TREE_INT_CST_LOW (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
3576 % TREE_INT_CST_LOW (arg1)) == 0)
3577 {
3578 tree new_op
3579 = build_int_2 (TREE_INT_CST_LOW (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
906c4e36 3580 / TREE_INT_CST_LOW (arg1), 0);
3b998c11
RK
3581
3582 TREE_TYPE (new_op) = type;
3583 return build (MULT_EXPR, type,
3584 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0), new_op);
3585 }
3586
6d716ca8
RS
3587#if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
3588#ifndef REAL_INFINITY
3589 if (TREE_CODE (arg1) == REAL_CST
3590 && real_zerop (arg1))
3591 return t;
3592#endif
3593#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
3594
3595 goto binary;
3596
3597 case CEIL_MOD_EXPR:
3598 case FLOOR_MOD_EXPR:
3599 case ROUND_MOD_EXPR:
3600 case TRUNC_MOD_EXPR:
3601 if (integer_onep (arg1))
3602 return omit_one_operand (type, integer_zero_node, arg0);
3603 if (integer_zerop (arg1))
3604 return t;
3605 goto binary;
3606
3607 case LSHIFT_EXPR:
3608 case RSHIFT_EXPR:
3609 case LROTATE_EXPR:
3610 case RROTATE_EXPR:
3611 if (integer_zerop (arg1))
3612 return non_lvalue (convert (type, arg0));
3613 /* Since negative shift count is not well-defined,
3614 don't try to compute it in the compiler. */
3615 if (tree_int_cst_lt (arg1, integer_zero_node))
3616 return t;
3617 goto binary;
3618
3619 case MIN_EXPR:
3620 if (operand_equal_p (arg0, arg1, 0))
3621 return arg0;
3622 if (TREE_CODE (type) == INTEGER_TYPE
3623 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
3624 return omit_one_operand (type, arg1, arg0);
3625 goto associate;
3626
3627 case MAX_EXPR:
3628 if (operand_equal_p (arg0, arg1, 0))
3629 return arg0;
3630 if (TREE_CODE (type) == INTEGER_TYPE
3631 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
3632 return omit_one_operand (type, arg1, arg0);
3633 goto associate;
3634
3635 case TRUTH_NOT_EXPR:
3636 /* Note that the operand of this must be an int
3637 and its values must be 0 or 1.
3638 ("true" is a fixed value perhaps depending on the language,
3639 but we don't handle values other than 1 correctly yet.) */
3640 return invert_truthvalue (arg0);
3641
3642 case TRUTH_ANDIF_EXPR:
3643 /* Note that the operands of this must be ints
3644 and their values must be 0 or 1.
3645 ("true" is a fixed value perhaps depending on the language.) */
3646 /* If first arg is constant zero, return it. */
3647 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
3648 return arg0;
3649 case TRUTH_AND_EXPR:
3650 /* If either arg is constant true, drop it. */
3651 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
3652 return non_lvalue (arg1);
3653 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
3654 return non_lvalue (arg0);
3655 /* Both known to be zero => return zero. */
3656 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3657 return arg0;
3658
3659 truth_andor:
3660 /* Check for the possibility of merging component references. If our
3661 lhs is another similar operation, try to merge its rhs with our
3662 rhs. Then try to merge our lhs and rhs. */
3663 if (optimize)
3664 {
6d716ca8
RS
3665 if (TREE_CODE (arg0) == code)
3666 {
b2215d83
TW
3667 tem = fold_truthop (code, type,
3668 TREE_OPERAND (arg0, 1), arg1);
6d716ca8
RS
3669 if (tem)
3670 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
3671 }
3672
b2215d83 3673 tem = fold_truthop (code, type, arg0, arg1);
6d716ca8
RS
3674 if (tem)
3675 return tem;
3676 }
3677 return t;
3678
3679 case TRUTH_ORIF_EXPR:
3680 /* Note that the operands of this must be ints
3681 and their values must be 0 or true.
3682 ("true" is a fixed value perhaps depending on the language.) */
3683 /* If first arg is constant true, return it. */
3684 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
3685 return arg0;
3686 case TRUTH_OR_EXPR:
3687 /* If either arg is constant zero, drop it. */
3688 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
3689 return non_lvalue (arg1);
3690 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1))
3691 return non_lvalue (arg0);
3692 /* Both known to be true => return true. */
3693 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3694 return arg0;
3695 goto truth_andor;
3696
3697 case EQ_EXPR:
3698 case NE_EXPR:
3699 case LT_EXPR:
3700 case GT_EXPR:
3701 case LE_EXPR:
3702 case GE_EXPR:
3703 /* If one arg is a constant integer, put it last. */
3704 if (TREE_CODE (arg0) == INTEGER_CST
3705 && TREE_CODE (arg1) != INTEGER_CST)
3706 {
3707 TREE_OPERAND (t, 0) = arg1;
3708 TREE_OPERAND (t, 1) = arg0;
3709 arg0 = TREE_OPERAND (t, 0);
3710 arg1 = TREE_OPERAND (t, 1);
c05a9b68 3711 code = swap_tree_comparison (code);
6d716ca8
RS
3712 TREE_SET_CODE (t, code);
3713 }
3714
3715 /* Convert foo++ == CONST into ++foo == CONST + INCR.
3716 First, see if one arg is constant; find the constant arg
3717 and the other one. */
3718 {
3719 tree constop = 0, varop;
3720 tree *constoploc;
3721
3722 if (TREE_CONSTANT (arg1))
3723 constoploc = &TREE_OPERAND (t, 1), constop = arg1, varop = arg0;
3724 if (TREE_CONSTANT (arg0))
3725 constoploc = &TREE_OPERAND (t, 0), constop = arg0, varop = arg1;
3726
3727 if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
3728 {
6d716ca8
RS
3729 /* This optimization is invalid for ordered comparisons
3730 if CONST+INCR overflows or if foo+incr might overflow.
c05a9b68 3731 This optimization is invalid for floating point due to rounding.
6d716ca8
RS
3732 For pointer types we assume overflow doesn't happen. */
3733 if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
c05a9b68
RS
3734 || (TREE_CODE (TREE_TYPE (varop)) != REAL_TYPE
3735 && (code == EQ_EXPR || code == NE_EXPR)))
6d716ca8 3736 {
c05a9b68
RS
3737 tree newconst
3738 = fold (build (PLUS_EXPR, TREE_TYPE (varop),
3739 constop, TREE_OPERAND (varop, 1)));
3740 TREE_SET_CODE (varop, PREINCREMENT_EXPR);
3741 *constoploc = newconst;
3742 return t;
6d716ca8
RS
3743 }
3744 }
3745 else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
3746 {
6d716ca8 3747 if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
c05a9b68
RS
3748 || (TREE_CODE (TREE_TYPE (varop)) != REAL_TYPE
3749 && (code == EQ_EXPR || code == NE_EXPR)))
6d716ca8 3750 {
c05a9b68
RS
3751 tree newconst
3752 = fold (build (MINUS_EXPR, TREE_TYPE (varop),
3753 constop, TREE_OPERAND (varop, 1)));
3754 TREE_SET_CODE (varop, PREDECREMENT_EXPR);
3755 *constoploc = newconst;
3756 return t;
6d716ca8
RS
3757 }
3758 }
3759 }
3760
3761 /* Change X >= CST to X > (CST - 1) if CST is positive. */
3762 if (TREE_CODE (arg1) == INTEGER_CST
3763 && TREE_CODE (arg0) != INTEGER_CST
3764 && ! tree_int_cst_lt (arg1, integer_one_node))
3765 {
3766 switch (TREE_CODE (t))
3767 {
3768 case GE_EXPR:
3769 code = GT_EXPR;
3770 TREE_SET_CODE (t, code);
3771 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
3772 TREE_OPERAND (t, 1) = arg1;
3773 break;
3774
3775 case LT_EXPR:
3776 code = LE_EXPR;
3777 TREE_SET_CODE (t, code);
3778 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
3779 TREE_OPERAND (t, 1) = arg1;
3780 }
3781 }
3782
6d716ca8
RS
3783 /* If this is an EQ or NE comparison with zero and ARG0 is
3784 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
3785 two operations, but the latter can be done in one less insn
3786 one machine that have only two-operand insns or on which a
3787 constant cannot be the first operand. */
3788 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
3789 && TREE_CODE (arg0) == BIT_AND_EXPR)
3790 {
3791 if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
3792 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
3793 return
3794 fold (build (code, type,
3795 build (BIT_AND_EXPR, TREE_TYPE (arg0),
3796 build (RSHIFT_EXPR,
3797 TREE_TYPE (TREE_OPERAND (arg0, 0)),
3798 TREE_OPERAND (arg0, 1),
3799 TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
3800 convert (TREE_TYPE (arg0),
3801 integer_one_node)),
3802 arg1));
3803 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
3804 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
3805 return
3806 fold (build (code, type,
3807 build (BIT_AND_EXPR, TREE_TYPE (arg0),
3808 build (RSHIFT_EXPR,
3809 TREE_TYPE (TREE_OPERAND (arg0, 1)),
3810 TREE_OPERAND (arg0, 0),
3811 TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
3812 convert (TREE_TYPE (arg0),
3813 integer_one_node)),
3814 arg1));
3815 }
3816
3817 /* If this is an NE comparison of zero with an AND of one, remove the
3818 comparison since the AND will give the correct value. */
3819 if (code == NE_EXPR && integer_zerop (arg1)
3820 && TREE_CODE (arg0) == BIT_AND_EXPR
3821 && integer_onep (TREE_OPERAND (arg0, 1)))
3822 return convert (type, arg0);
3823
3824 /* If we have (A & C) == C where C is a power of 2, convert this into
3825 (A & C) != 0. Similarly for NE_EXPR. */
3826 if ((code == EQ_EXPR || code == NE_EXPR)
3827 && TREE_CODE (arg0) == BIT_AND_EXPR
3828 && integer_pow2p (TREE_OPERAND (arg0, 1))
3829 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
3830 return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
3831 arg0, integer_zero_node);
3832
c05a9b68
RS
3833 /* Simplify comparison of something with itself. (For IEEE
3834 floating-point, we can only do some of these simplifications.) */
3835 if (operand_equal_p (arg0, arg1, 0))
6d716ca8
RS
3836 {
3837 switch (code)
3838 {
3839 case EQ_EXPR:
3840 case GE_EXPR:
3841 case LE_EXPR:
c05a9b68
RS
3842 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE)
3843 {
3844 t = build_int_2 (1, 0);
3845 TREE_TYPE (t) = type;
3846 return t;
3847 }
3848 code = EQ_EXPR;
3849 TREE_SET_CODE (t, code);
3850 break;
3851
6d716ca8 3852 case NE_EXPR:
c05a9b68
RS
3853 /* For NE, we can only do this simplification if integer. */
3854 if (TREE_CODE (TREE_TYPE (arg0)) != INTEGER_TYPE)
3855 break;
3856 /* ... fall through ... */
6d716ca8
RS
3857 case GT_EXPR:
3858 case LT_EXPR:
3859 t = build_int_2 (0, 0);
3860 TREE_TYPE (t) = type;
3861 return t;
3862 }
3863 }
3864
3865 /* An unsigned comparison against 0 can be simplified. */
3866 if (integer_zerop (arg1)
3867 && (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
3868 || TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE)
3869 && TREE_UNSIGNED (TREE_TYPE (arg1)))
3870 {
3871 switch (TREE_CODE (t))
3872 {
3873 case GT_EXPR:
c05a9b68 3874 code = NE_EXPR;
6d716ca8
RS
3875 TREE_SET_CODE (t, NE_EXPR);
3876 break;
3877 case LE_EXPR:
c05a9b68 3878 code = EQ_EXPR;
6d716ca8
RS
3879 TREE_SET_CODE (t, EQ_EXPR);
3880 break;
3881 case GE_EXPR:
3882 return omit_one_operand (integer_type_node,
3883 integer_one_node, arg0);
3884 case LT_EXPR:
3885 return omit_one_operand (integer_type_node,
3886 integer_zero_node, arg0);
3887 }
3888 }
3889
c05a9b68
RS
3890 /* If we are comparing an expression that just has comparisons
3891 of two integer values, arithmetic expressions of those comparisons,
3892 and constants, we can simplify it. There are only three cases
3893 to check: the two values can either be equal, the first can be
3894 greater, or the second can be greater. Fold the expression for
3895 those three values. Since each value must be 0 or 1, we have
3896 eight possibilities, each of which corresponds to the constant 0
3897 or 1 or one of the six possible comparisons.
3898
3899 This handles common cases like (a > b) == 0 but also handles
3900 expressions like ((x > y) - (y > x)) > 0, which supposedly
3901 occur in macroized code. */
3902
3903 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
3904 {
3905 tree cval1 = 0, cval2 = 0;
3906
3907 if (twoval_comparison_p (arg0, &cval1, &cval2)
3908 /* Don't handle degenerate cases here; they should already
3909 have been handled anyway. */
3910 && cval1 != 0 && cval2 != 0
3911 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
3912 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
3913 && TREE_CODE (TREE_TYPE (cval1)) == INTEGER_TYPE
3914 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
3915 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
3916 {
3917 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
3918 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
3919
3920 /* We can't just pass T to eval_subst in case cval1 or cval2
3921 was the same as ARG1. */
3922
3923 tree high_result
3924 = fold (build (code, type,
3925 eval_subst (arg0, cval1, maxval, cval2, minval),
3926 arg1));
3927 tree equal_result
3928 = fold (build (code, type,
3929 eval_subst (arg0, cval1, maxval, cval2, maxval),
3930 arg1));
3931 tree low_result
3932 = fold (build (code, type,
3933 eval_subst (arg0, cval1, minval, cval2, maxval),
3934 arg1));
3935
3936 /* All three of these results should be 0 or 1. Confirm they
3937 are. Then use those values to select the proper code
3938 to use. */
3939
3940 if ((integer_zerop (high_result)
3941 || integer_onep (high_result))
3942 && (integer_zerop (equal_result)
3943 || integer_onep (equal_result))
3944 && (integer_zerop (low_result)
3945 || integer_onep (low_result)))
3946 {
3947 /* Make a 3-bit mask with the high-order bit being the
3948 value for `>', the next for '=', and the low for '<'. */
3949 switch ((integer_onep (high_result) * 4)
3950 + (integer_onep (equal_result) * 2)
3951 + integer_onep (low_result))
3952 {
3953 case 0:
3954 /* Always false. */
13837058 3955 return omit_one_operand (type, integer_zero_node, arg0);
c05a9b68
RS
3956 case 1:
3957 code = LT_EXPR;
3958 break;
3959 case 2:
3960 code = EQ_EXPR;
3961 break;
3962 case 3:
3963 code = LE_EXPR;
3964 break;
3965 case 4:
3966 code = GT_EXPR;
3967 break;
3968 case 5:
3969 code = NE_EXPR;
3970 break;
3971 case 6:
3972 code = GE_EXPR;
3973 break;
3974 case 7:
3975 /* Always true. */
13837058 3976 return omit_one_operand (type, integer_one_node, arg0);
c05a9b68
RS
3977 }
3978
95ca4f96 3979 return fold (build (code, type, cval1, cval2));
c05a9b68
RS
3980 }
3981 }
3982 }
3983
3984 /* If this is a comparison of a field, we may be able to simplify it. */
3985 if ((TREE_CODE (arg0) == COMPONENT_REF
3986 || TREE_CODE (arg0) == BIT_FIELD_REF)
3987 && (code == EQ_EXPR || code == NE_EXPR)
3988 /* Handle the constant case even without -O
3989 to make sure the warnings are given. */
3990 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
3991 {
3992 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
3993 return t1 ? t1 : t;
3994 }
3995
3996 /* From here on, the only cases we handle are when the result is
3997 known to be a constant.
3998
3999 To compute GT, swap the arguments and do LT.
6d716ca8
RS
4000 To compute GE, do LT and invert the result.
4001 To compute LE, swap the arguments, do LT and invert the result.
c05a9b68
RS
4002 To compute NE, do EQ and invert the result.
4003
4004 Therefore, the code below must handle only EQ and LT. */
4005
6d716ca8
RS
4006 if (code == LE_EXPR || code == GT_EXPR)
4007 {
c05a9b68
RS
4008 tem = arg0, arg0 = arg1, arg1 = tem;
4009 code = swap_tree_comparison (code);
4010 }
4011
4012 /* Note that it is safe to invert for real values here because we
4013 will check below in the one case that it matters. */
4014
4015 invert = 0;
4016 if (code == NE_EXPR || code == GE_EXPR)
4017 {
4018 invert = 1;
4019 code = invert_tree_comparison (code);
6d716ca8
RS
4020 }
4021
4022 /* Compute a result for LT or EQ if args permit;
4023 otherwise return T. */
c05a9b68 4024 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
6d716ca8 4025 {
c05a9b68
RS
4026 if (code == EQ_EXPR)
4027 t1 = build_int_2 ((TREE_INT_CST_LOW (arg0)
4028 == TREE_INT_CST_LOW (arg1))
4029 && (TREE_INT_CST_HIGH (arg0)
4030 == TREE_INT_CST_HIGH (arg1)),
4031 0);
6d716ca8 4032 else
c05a9b68
RS
4033 t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
4034 ? INT_CST_LT_UNSIGNED (arg0, arg1)
4035 : INT_CST_LT (arg0, arg1)),
4036 0);
6d716ca8 4037 }
c05a9b68 4038
6d716ca8
RS
4039 /* Assume a nonexplicit constant cannot equal an explicit one,
4040 since such code would be undefined anyway.
4041 Exception: on sysvr4, using #pragma weak,
4042 a label can come out as 0. */
4043 else if (TREE_CODE (arg1) == INTEGER_CST
4044 && !integer_zerop (arg1)
4045 && TREE_CONSTANT (arg0)
4046 && TREE_CODE (arg0) == ADDR_EXPR
c05a9b68
RS
4047 && code == EQ_EXPR)
4048 t1 = build_int_2 (0, 0);
4049
6d716ca8 4050 /* Two real constants can be compared explicitly. */
c05a9b68 4051 else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
6d716ca8 4052 {
c05a9b68
RS
4053 /* If either operand is a NaN, the result is false with two
4054 exceptions: First, an NE_EXPR is true on NaNs, but that case
4055 is already handled correctly since we will be inverting the
4056 result for NE_EXPR. Second, if we had inverted a LE_EXPR
4057 or a GE_EXPR into a LT_EXPR, we must return true so that it
4058 will be inverted into false. */
4059
4060 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
4061 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
4062 t1 = build_int_2 (invert && code == LT_EXPR, 0);
4063
4064 else if (code == EQ_EXPR)
4065 t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
4066 TREE_REAL_CST (arg1)),
4067 0);
6d716ca8 4068 else
c05a9b68
RS
4069 t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
4070 TREE_REAL_CST (arg1)),
4071 0);
6d716ca8
RS
4072 }
4073
c05a9b68
RS
4074 if (t1 == NULL_TREE)
4075 return t;
4076
4077 if (invert)
4078 TREE_INT_CST_LOW (t1) ^= 1;
4079
4080 TREE_TYPE (t1) = type;
4081 return t1;
6d716ca8
RS
4082
4083 case COND_EXPR:
4084 if (TREE_CODE (arg0) == INTEGER_CST)
4085 return TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1));
4086 else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
4087 return omit_one_operand (type, arg1, arg0);
6d716ca8 4088
c05a9b68
RS
4089 /* If the second operand is zero, invert the comparison and swap
4090 the second and third operands. Likewise if the second operand
4091 is constant and the third is not or if the third operand is
4092 equivalent to the first operand of the comparison. */
6d716ca8 4093
c05a9b68
RS
4094 if (integer_zerop (arg1)
4095 || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
4096 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
4097 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
4098 TREE_OPERAND (t, 2),
4099 TREE_OPERAND (arg0, 1))))
4100 {
4101 /* See if this can be inverted. If it can't, possibly because
4102 it was a floating-point inequality comparison, don't do
4103 anything. */
4104 tem = invert_truthvalue (arg0);
6d716ca8 4105
c05a9b68
RS
4106 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
4107 {
4108 arg0 = TREE_OPERAND (t, 0) = tem;
4109 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 2);
4110 TREE_OPERAND (t, 2) = arg1;
4111 arg1 = TREE_OPERAND (t, 1);
4112 }
4113 }
6d716ca8 4114
c05a9b68
RS
4115 /* If we have A op B ? A : C, we may be able to convert this to a
4116 simpler expression, depending on the operation and the values
b5c52586
RS
4117 of B and C. IEEE floating point prevents this though,
4118 because A or B might be -0.0 or a NaN. */
6d716ca8 4119
c05a9b68 4120 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
b5c52586
RS
4121 && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4122 || TREE_CODE (TREE_TYPE (TREE_OPERAND (arg0, 0))) != REAL_TYPE)
c05a9b68
RS
4123 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
4124 arg1, TREE_OPERAND (arg0, 1)))
6d716ca8 4125 {
c05a9b68
RS
4126 tree arg2 = TREE_OPERAND (t, 2);
4127 enum tree_code comp_code = TREE_CODE (arg0);
4128
4129 /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
4130 depending on the comparison operation. */
4131 if (integer_zerop (TREE_OPERAND (arg0, 1))
4132 && TREE_CODE (arg2) == NEGATE_EXPR
4133 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4134 switch (comp_code)
4135 {
4136 case EQ_EXPR:
4137 return fold (build1 (NEGATE_EXPR, type, arg1));
4138 case NE_EXPR:
cdc54cc9 4139 return convert (type, arg1);
c05a9b68
RS
4140 case GE_EXPR:
4141 case GT_EXPR:
4142 return fold (build1 (ABS_EXPR, type, arg1));
4143 case LE_EXPR:
4144 case LT_EXPR:
4145 return fold (build1 (NEGATE_EXPR, type,
4146 fold (build1 (ABS_EXPR, type, arg1))));
4147 }
6d716ca8 4148
c05a9b68
RS
4149 /* If this is A != 0 ? A : 0, this is simply A. For ==, it is
4150 always zero. */
6d716ca8 4151
29ebe69a 4152 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
c05a9b68
RS
4153 {
4154 if (comp_code == NE_EXPR)
cdc54cc9 4155 return convert (type, arg1);
c05a9b68
RS
4156 else if (comp_code == EQ_EXPR)
4157 return convert (type, integer_zero_node);
4158 }
4159
4160 /* If this is A op B ? A : B, this is either A, B, min (A, B),
4161 or max (A, B), depending on the operation. */
4162
4163 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
4164 arg2, TREE_OPERAND (arg0, 0)))
4165 switch (comp_code)
4166 {
4167 case EQ_EXPR:
cdc54cc9 4168 return convert (type, arg2);
c05a9b68 4169 case NE_EXPR:
cdc54cc9 4170 return convert (type, arg1);
c05a9b68
RS
4171 case LE_EXPR:
4172 case LT_EXPR:
4173 return fold (build (MIN_EXPR, type, arg1, arg2));
4174 case GE_EXPR:
4175 case GT_EXPR:
4176 return fold (build (MAX_EXPR, type, arg1, arg2));
4177 }
4178
4179 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4180 we might still be able to simplify this. For example,
4181 if C1 is one less or one more than C2, this might have started
53d2fb4f
RS
4182 out as a MIN or MAX and been transformed by this function.
4183 Only good for INTEGER_TYPE, because we need TYPE_MAX_VALUE. */
c05a9b68 4184
53d2fb4f
RS
4185 if (TREE_CODE (type) == INTEGER_TYPE
4186 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
c05a9b68
RS
4187 && TREE_CODE (arg2) == INTEGER_CST)
4188 switch (comp_code)
4189 {
4190 case EQ_EXPR:
4191 /* We can replace A with C1 in this case. */
4192 arg1 = TREE_OPERAND (t, 1)
4193 = convert (type, TREE_OPERAND (arg0, 1));
4194 break;
4195
4196 case LT_EXPR:
4197 /* If C1 is C2 + 1, this is min(A, C2). */
4198 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
4199 && operand_equal_p (TREE_OPERAND (arg0, 1),
4200 const_binop (PLUS_EXPR, arg2,
4201 integer_one_node), 1))
4202 return fold (build (MIN_EXPR, type, arg1, arg2));
4203 break;
4204
4205 case LE_EXPR:
4206 /* If C1 is C2 - 1, this is min(A, C2). */
4207 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
4208 && operand_equal_p (TREE_OPERAND (arg0, 1),
4209 const_binop (MINUS_EXPR, arg2,
4210 integer_one_node), 1))
4211 return fold (build (MIN_EXPR, type, arg1, arg2));
4212 break;
4213
4214 case GT_EXPR:
4215 /* If C1 is C2 - 1, this is max(A, C2). */
4216 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
4217 && operand_equal_p (TREE_OPERAND (arg0, 1),
4218 const_binop (MINUS_EXPR, arg2,
4219 integer_one_node), 1))
4220 return fold (build (MAX_EXPR, type, arg1, arg2));
4221 break;
4222
4223 case GE_EXPR:
4224 /* If C1 is C2 + 1, this is max(A, C2). */
4225 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
4226 && operand_equal_p (TREE_OPERAND (arg0, 1),
4227 const_binop (PLUS_EXPR, arg2,
4228 integer_one_node), 1))
4229 return fold (build (MAX_EXPR, type, arg1, arg2));
4230 break;
4231 }
6d716ca8
RS
4232 }
4233
c05a9b68
RS
4234 /* Convert A ? 1 : 0 to simply A. */
4235 if (integer_onep (TREE_OPERAND (t, 1))
4236 && integer_zerop (TREE_OPERAND (t, 2))
4237 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
4238 call to fold will try to move the conversion inside
4239 a COND, which will recurse. In that case, the COND_EXPR
4240 is probably the best choice, so leave it alone. */
4241 && type == TREE_TYPE (arg0))
4242 return arg0;
6d716ca8 4243
6d716ca8 4244
c05a9b68
RS
4245 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
4246 operation is simply A & 2. */
6d716ca8
RS
4247
4248 if (integer_zerop (TREE_OPERAND (t, 2))
4249 && TREE_CODE (arg0) == NE_EXPR
4250 && integer_zerop (TREE_OPERAND (arg0, 1))
c05a9b68
RS
4251 && integer_pow2p (arg1)
4252 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
4253 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
4254 arg1, 1))
4255 return convert (type, TREE_OPERAND (arg0, 0));
6d716ca8 4256
6d716ca8
RS
4257 return t;
4258
4259 case COMPOUND_EXPR:
4260 if (!TREE_SIDE_EFFECTS (arg0))
4261 return arg1;
4262 return t;
4263
4264 default:
4265 return t;
4266 } /* switch (code) */
4267}
This page took 0.539433 seconds and 5 git commands to generate.