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