OSDN Git Service

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