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