OSDN Git Service

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