OSDN Git Service

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