OSDN Git Service

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