OSDN Git Service

PR c++/35546
[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, 2006, 2007
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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_double.
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_double takes a constant, an overflowable flag and a
43    prior overflow indicator.  It forces the value to fit the type and
44    sets TREE_OVERFLOW.
45
46    Note: Since the folders get called on non-gimple code as well as
47    gimple code, we need to handle GIMPLE tuples as well as their
48    corresponding tree equivalents.  */
49
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "flags.h"
55 #include "tree.h"
56 #include "real.h"
57 #include "fixed-value.h"
58 #include "rtl.h"
59 #include "expr.h"
60 #include "tm_p.h"
61 #include "toplev.h"
62 #include "intl.h"
63 #include "ggc.h"
64 #include "hashtab.h"
65 #include "langhooks.h"
66 #include "md5.h"
67
68 /* Nonzero if we are folding constants inside an initializer; zero
69    otherwise.  */
70 int folding_initializer = 0;
71
72 /* The following constants represent a bit based encoding of GCC's
73    comparison operators.  This encoding simplifies transformations
74    on relational comparison operators, such as AND and OR.  */
75 enum comparison_code {
76   COMPCODE_FALSE = 0,
77   COMPCODE_LT = 1,
78   COMPCODE_EQ = 2,
79   COMPCODE_LE = 3,
80   COMPCODE_GT = 4,
81   COMPCODE_LTGT = 5,
82   COMPCODE_GE = 6,
83   COMPCODE_ORD = 7,
84   COMPCODE_UNORD = 8,
85   COMPCODE_UNLT = 9,
86   COMPCODE_UNEQ = 10,
87   COMPCODE_UNLE = 11,
88   COMPCODE_UNGT = 12,
89   COMPCODE_NE = 13,
90   COMPCODE_UNGE = 14,
91   COMPCODE_TRUE = 15
92 };
93
94 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
95 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
96 static bool negate_mathfn_p (enum built_in_function);
97 static bool negate_expr_p (tree);
98 static tree negate_expr (tree);
99 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
100 static tree associate_trees (tree, tree, enum tree_code, tree);
101 static tree const_binop (enum tree_code, tree, tree, int);
102 static enum comparison_code comparison_to_compcode (enum tree_code);
103 static enum tree_code compcode_to_comparison (enum comparison_code);
104 static tree combine_comparisons (enum tree_code, enum tree_code,
105                                  enum tree_code, tree, tree, tree);
106 static int truth_value_p (enum tree_code);
107 static int operand_equal_for_comparison_p (tree, tree, tree);
108 static int twoval_comparison_p (tree, tree *, tree *, int *);
109 static tree eval_subst (tree, tree, tree, tree, tree);
110 static tree pedantic_omit_one_operand (tree, tree, tree);
111 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
112 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
113                                     enum machine_mode *, int *, int *,
114                                     tree *, tree *);
115 static tree sign_bit_p (tree, const_tree);
116 static int simple_operand_p (const_tree);
117 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
118 static tree range_predecessor (tree);
119 static tree range_successor (tree);
120 static tree make_range (tree, int *, tree *, tree *, bool *);
121 static tree build_range_check (tree, tree, int, tree, tree);
122 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
123                          tree);
124 static tree fold_range_test (enum tree_code, tree, tree, tree);
125 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
126 static tree unextend (tree, int, int, tree);
127 static tree fold_truthop (enum tree_code, tree, tree, tree);
128 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
129 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
130 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
131 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
132                                                  tree, tree,
133                                                  tree, tree, int);
134 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
135                                  tree, tree, tree);
136 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
137 static tree fold_div_compare (enum tree_code, tree, tree, tree);
138 static bool reorder_operands_p (const_tree, const_tree);
139 static tree fold_negate_const (tree, tree);
140 static tree fold_not_const (tree, tree);
141 static tree fold_relational_const (enum tree_code, tree, tree, tree);
142
143
144 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
145    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
146    and SUM1.  Then this yields nonzero if overflow occurred during the
147    addition.
148
149    Overflow occurs if A and B have the same sign, but A and SUM differ in
150    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
151    sign.  */
152 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
153 \f
154 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
155    We do that by representing the two-word integer in 4 words, with only
156    HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
157    number.  The value of the word is LOWPART + HIGHPART * BASE.  */
158
159 #define LOWPART(x) \
160   ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
161 #define HIGHPART(x) \
162   ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
163 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
164
165 /* Unpack a two-word integer into 4 words.
166    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
167    WORDS points to the array of HOST_WIDE_INTs.  */
168
169 static void
170 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
171 {
172   words[0] = LOWPART (low);
173   words[1] = HIGHPART (low);
174   words[2] = LOWPART (hi);
175   words[3] = HIGHPART (hi);
176 }
177
178 /* Pack an array of 4 words into a two-word integer.
179    WORDS points to the array of words.
180    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
181
182 static void
183 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
184         HOST_WIDE_INT *hi)
185 {
186   *low = words[0] + words[1] * BASE;
187   *hi = words[2] + words[3] * BASE;
188 }
189 \f
190 /* Force the double-word integer L1, H1 to be within the range of the
191    integer type TYPE.  Stores the properly truncated and sign-extended
192    double-word integer in *LV, *HV.  Returns true if the operation
193    overflows, that is, argument and result are different.  */
194
195 int
196 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
197                  unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
198 {
199   unsigned HOST_WIDE_INT low0 = l1;
200   HOST_WIDE_INT high0 = h1;
201   unsigned int prec;
202   int sign_extended_type;
203
204   if (POINTER_TYPE_P (type)
205       || TREE_CODE (type) == OFFSET_TYPE)
206     prec = POINTER_SIZE;
207   else
208     prec = TYPE_PRECISION (type);
209
210   /* Size types *are* sign extended.  */
211   sign_extended_type = (!TYPE_UNSIGNED (type)
212                         || (TREE_CODE (type) == INTEGER_TYPE
213                             && TYPE_IS_SIZETYPE (type)));
214
215   /* First clear all bits that are beyond the type's precision.  */
216   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
217     ;
218   else if (prec > HOST_BITS_PER_WIDE_INT)
219     h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
220   else
221     {
222       h1 = 0;
223       if (prec < HOST_BITS_PER_WIDE_INT)
224         l1 &= ~((HOST_WIDE_INT) (-1) << prec);
225     }
226
227   /* Then do sign extension if necessary.  */
228   if (!sign_extended_type)
229     /* No sign extension */;
230   else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
231     /* Correct width already.  */;
232   else if (prec > HOST_BITS_PER_WIDE_INT)
233     {
234       /* Sign extend top half? */
235       if (h1 & ((unsigned HOST_WIDE_INT)1
236                 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
237         h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
238     }
239   else if (prec == HOST_BITS_PER_WIDE_INT)
240     {
241       if ((HOST_WIDE_INT)l1 < 0)
242         h1 = -1;
243     }
244   else
245     {
246       /* Sign extend bottom half? */
247       if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
248         {
249           h1 = -1;
250           l1 |= (HOST_WIDE_INT)(-1) << prec;
251         }
252     }
253
254   *lv = l1;
255   *hv = h1;
256
257   /* If the value didn't fit, signal overflow.  */
258   return l1 != low0 || h1 != high0;
259 }
260
261 /* We force the double-int HIGH:LOW to the range of the type TYPE by
262    sign or zero extending it.
263    OVERFLOWABLE indicates if we are interested
264    in overflow of the value, when >0 we are only interested in signed
265    overflow, for <0 we are interested in any overflow.  OVERFLOWED
266    indicates whether overflow has already occurred.  CONST_OVERFLOWED
267    indicates whether constant overflow has already occurred.  We force
268    T's value to be within range of T's type (by setting to 0 or 1 all
269    the bits outside the type's range).  We set TREE_OVERFLOWED if,
270         OVERFLOWED is nonzero,
271         or OVERFLOWABLE is >0 and signed overflow occurs
272         or OVERFLOWABLE is <0 and any overflow occurs
273    We return a new tree node for the extended double-int.  The node
274    is shared if no overflow flags are set.  */
275
276 tree
277 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
278                        HOST_WIDE_INT high, int overflowable,
279                        bool overflowed)
280 {
281   int sign_extended_type;
282   bool overflow;
283
284   /* Size types *are* sign extended.  */
285   sign_extended_type = (!TYPE_UNSIGNED (type)
286                         || (TREE_CODE (type) == INTEGER_TYPE
287                             && TYPE_IS_SIZETYPE (type)));
288
289   overflow = fit_double_type (low, high, &low, &high, type);
290
291   /* If we need to set overflow flags, return a new unshared node.  */
292   if (overflowed || overflow)
293     {
294       if (overflowed
295           || overflowable < 0
296           || (overflowable > 0 && sign_extended_type))
297         {
298           tree t = make_node (INTEGER_CST);
299           TREE_INT_CST_LOW (t) = low;
300           TREE_INT_CST_HIGH (t) = high;
301           TREE_TYPE (t) = type;
302           TREE_OVERFLOW (t) = 1;
303           return t;
304         }
305     }
306
307   /* Else build a shared node.  */
308   return build_int_cst_wide (type, low, high);
309 }
310 \f
311 /* Add two doubleword integers with doubleword result.
312    Return nonzero if the operation overflows according to UNSIGNED_P.
313    Each argument is given as two `HOST_WIDE_INT' pieces.
314    One argument is L1 and H1; the other, L2 and H2.
315    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
316
317 int
318 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
319                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
320                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
321                       bool unsigned_p)
322 {
323   unsigned HOST_WIDE_INT l;
324   HOST_WIDE_INT h;
325
326   l = l1 + l2;
327   h = h1 + h2 + (l < l1);
328
329   *lv = l;
330   *hv = h;
331
332   if (unsigned_p)
333     return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
334   else
335     return OVERFLOW_SUM_SIGN (h1, h2, h);
336 }
337
338 /* Negate a doubleword integer with doubleword result.
339    Return nonzero if the operation overflows, assuming it's signed.
340    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
341    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
342
343 int
344 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
345             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
346 {
347   if (l1 == 0)
348     {
349       *lv = 0;
350       *hv = - h1;
351       return (*hv & h1) < 0;
352     }
353   else
354     {
355       *lv = -l1;
356       *hv = ~h1;
357       return 0;
358     }
359 }
360 \f
361 /* Multiply two doubleword integers with doubleword result.
362    Return nonzero if the operation overflows according to UNSIGNED_P.
363    Each argument is given as two `HOST_WIDE_INT' pieces.
364    One argument is L1 and H1; the other, L2 and H2.
365    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
366
367 int
368 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
369                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
370                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
371                       bool unsigned_p)
372 {
373   HOST_WIDE_INT arg1[4];
374   HOST_WIDE_INT arg2[4];
375   HOST_WIDE_INT prod[4 * 2];
376   unsigned HOST_WIDE_INT carry;
377   int i, j, k;
378   unsigned HOST_WIDE_INT toplow, neglow;
379   HOST_WIDE_INT tophigh, neghigh;
380
381   encode (arg1, l1, h1);
382   encode (arg2, l2, h2);
383
384   memset (prod, 0, sizeof prod);
385
386   for (i = 0; i < 4; i++)
387     {
388       carry = 0;
389       for (j = 0; j < 4; j++)
390         {
391           k = i + j;
392           /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
393           carry += arg1[i] * arg2[j];
394           /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
395           carry += prod[k];
396           prod[k] = LOWPART (carry);
397           carry = HIGHPART (carry);
398         }
399       prod[i + 4] = carry;
400     }
401
402   decode (prod, lv, hv);
403   decode (prod + 4, &toplow, &tophigh);
404
405   /* Unsigned overflow is immediate.  */
406   if (unsigned_p)
407     return (toplow | tophigh) != 0;
408
409   /* Check for signed overflow by calculating the signed representation of the
410      top half of the result; it should agree with the low half's sign bit.  */
411   if (h1 < 0)
412     {
413       neg_double (l2, h2, &neglow, &neghigh);
414       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
415     }
416   if (h2 < 0)
417     {
418       neg_double (l1, h1, &neglow, &neghigh);
419       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
420     }
421   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
422 }
423 \f
424 /* Shift the doubleword integer in L1, H1 left by COUNT places
425    keeping only PREC bits of result.
426    Shift right if COUNT is negative.
427    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
428    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
429
430 void
431 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
432                HOST_WIDE_INT count, unsigned int prec,
433                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
434 {
435   unsigned HOST_WIDE_INT signmask;
436
437   if (count < 0)
438     {
439       rshift_double (l1, h1, -count, prec, lv, hv, arith);
440       return;
441     }
442
443   if (SHIFT_COUNT_TRUNCATED)
444     count %= prec;
445
446   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
447     {
448       /* Shifting by the host word size is undefined according to the
449          ANSI standard, so we must handle this as a special case.  */
450       *hv = 0;
451       *lv = 0;
452     }
453   else if (count >= HOST_BITS_PER_WIDE_INT)
454     {
455       *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
456       *lv = 0;
457     }
458   else
459     {
460       *hv = (((unsigned HOST_WIDE_INT) h1 << count)
461              | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
462       *lv = l1 << count;
463     }
464
465   /* Sign extend all bits that are beyond the precision.  */
466
467   signmask = -((prec > HOST_BITS_PER_WIDE_INT
468                 ? ((unsigned HOST_WIDE_INT) *hv
469                    >> (prec - HOST_BITS_PER_WIDE_INT - 1))
470                 : (*lv >> (prec - 1))) & 1);
471
472   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
473     ;
474   else if (prec >= HOST_BITS_PER_WIDE_INT)
475     {
476       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
477       *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
478     }
479   else
480     {
481       *hv = signmask;
482       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
483       *lv |= signmask << prec;
484     }
485 }
486
487 /* Shift the doubleword integer in L1, H1 right by COUNT places
488    keeping only PREC bits of result.  COUNT must be positive.
489    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
490    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
491
492 void
493 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
494                HOST_WIDE_INT count, unsigned int prec,
495                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
496                int arith)
497 {
498   unsigned HOST_WIDE_INT signmask;
499
500   signmask = (arith
501               ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
502               : 0);
503
504   if (SHIFT_COUNT_TRUNCATED)
505     count %= prec;
506
507   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
508     {
509       /* Shifting by the host word size is undefined according to the
510          ANSI standard, so we must handle this as a special case.  */
511       *hv = 0;
512       *lv = 0;
513     }
514   else if (count >= HOST_BITS_PER_WIDE_INT)
515     {
516       *hv = 0;
517       *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
518     }
519   else
520     {
521       *hv = (unsigned HOST_WIDE_INT) h1 >> count;
522       *lv = ((l1 >> count)
523              | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
524     }
525
526   /* Zero / sign extend all bits that are beyond the precision.  */
527
528   if (count >= (HOST_WIDE_INT)prec)
529     {
530       *hv = signmask;
531       *lv = signmask;
532     }
533   else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
534     ;
535   else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
536     {
537       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
538       *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
539     }
540   else
541     {
542       *hv = signmask;
543       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
544       *lv |= signmask << (prec - count);
545     }
546 }
547 \f
548 /* Rotate the doubleword integer in L1, H1 left by COUNT places
549    keeping only PREC bits of result.
550    Rotate right if COUNT is negative.
551    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
552
553 void
554 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
555                 HOST_WIDE_INT count, unsigned int prec,
556                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
557 {
558   unsigned HOST_WIDE_INT s1l, s2l;
559   HOST_WIDE_INT s1h, s2h;
560
561   count %= prec;
562   if (count < 0)
563     count += prec;
564
565   lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
566   rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
567   *lv = s1l | s2l;
568   *hv = s1h | s2h;
569 }
570
571 /* Rotate the doubleword integer in L1, H1 left by COUNT places
572    keeping only PREC bits of result.  COUNT must be positive.
573    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
574
575 void
576 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
577                 HOST_WIDE_INT count, unsigned int prec,
578                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
579 {
580   unsigned HOST_WIDE_INT s1l, s2l;
581   HOST_WIDE_INT s1h, s2h;
582
583   count %= prec;
584   if (count < 0)
585     count += prec;
586
587   rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
588   lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
589   *lv = s1l | s2l;
590   *hv = s1h | s2h;
591 }
592 \f
593 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
594    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
595    CODE is a tree code for a kind of division, one of
596    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
597    or EXACT_DIV_EXPR
598    It controls how the quotient is rounded to an integer.
599    Return nonzero if the operation overflows.
600    UNS nonzero says do unsigned division.  */
601
602 int
603 div_and_round_double (enum tree_code code, int uns,
604                       unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
605                       HOST_WIDE_INT hnum_orig,
606                       unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
607                       HOST_WIDE_INT hden_orig,
608                       unsigned HOST_WIDE_INT *lquo,
609                       HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
610                       HOST_WIDE_INT *hrem)
611 {
612   int quo_neg = 0;
613   HOST_WIDE_INT num[4 + 1];     /* extra element for scaling.  */
614   HOST_WIDE_INT den[4], quo[4];
615   int i, j;
616   unsigned HOST_WIDE_INT work;
617   unsigned HOST_WIDE_INT carry = 0;
618   unsigned HOST_WIDE_INT lnum = lnum_orig;
619   HOST_WIDE_INT hnum = hnum_orig;
620   unsigned HOST_WIDE_INT lden = lden_orig;
621   HOST_WIDE_INT hden = hden_orig;
622   int overflow = 0;
623
624   if (hden == 0 && lden == 0)
625     overflow = 1, lden = 1;
626
627   /* Calculate quotient sign and convert operands to unsigned.  */
628   if (!uns)
629     {
630       if (hnum < 0)
631         {
632           quo_neg = ~ quo_neg;
633           /* (minimum integer) / (-1) is the only overflow case.  */
634           if (neg_double (lnum, hnum, &lnum, &hnum)
635               && ((HOST_WIDE_INT) lden & hden) == -1)
636             overflow = 1;
637         }
638       if (hden < 0)
639         {
640           quo_neg = ~ quo_neg;
641           neg_double (lden, hden, &lden, &hden);
642         }
643     }
644
645   if (hnum == 0 && hden == 0)
646     {                           /* single precision */
647       *hquo = *hrem = 0;
648       /* This unsigned division rounds toward zero.  */
649       *lquo = lnum / lden;
650       goto finish_up;
651     }
652
653   if (hnum == 0)
654     {                           /* trivial case: dividend < divisor */
655       /* hden != 0 already checked.  */
656       *hquo = *lquo = 0;
657       *hrem = hnum;
658       *lrem = lnum;
659       goto finish_up;
660     }
661
662   memset (quo, 0, sizeof quo);
663
664   memset (num, 0, sizeof num);  /* to zero 9th element */
665   memset (den, 0, sizeof den);
666
667   encode (num, lnum, hnum);
668   encode (den, lden, hden);
669
670   /* Special code for when the divisor < BASE.  */
671   if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
672     {
673       /* hnum != 0 already checked.  */
674       for (i = 4 - 1; i >= 0; i--)
675         {
676           work = num[i] + carry * BASE;
677           quo[i] = work / lden;
678           carry = work % lden;
679         }
680     }
681   else
682     {
683       /* Full double precision division,
684          with thanks to Don Knuth's "Seminumerical Algorithms".  */
685       int num_hi_sig, den_hi_sig;
686       unsigned HOST_WIDE_INT quo_est, scale;
687
688       /* Find the highest nonzero divisor digit.  */
689       for (i = 4 - 1;; i--)
690         if (den[i] != 0)
691           {
692             den_hi_sig = i;
693             break;
694           }
695
696       /* Insure that the first digit of the divisor is at least BASE/2.
697          This is required by the quotient digit estimation algorithm.  */
698
699       scale = BASE / (den[den_hi_sig] + 1);
700       if (scale > 1)
701         {               /* scale divisor and dividend */
702           carry = 0;
703           for (i = 0; i <= 4 - 1; i++)
704             {
705               work = (num[i] * scale) + carry;
706               num[i] = LOWPART (work);
707               carry = HIGHPART (work);
708             }
709
710           num[4] = carry;
711           carry = 0;
712           for (i = 0; i <= 4 - 1; i++)
713             {
714               work = (den[i] * scale) + carry;
715               den[i] = LOWPART (work);
716               carry = HIGHPART (work);
717               if (den[i] != 0) den_hi_sig = i;
718             }
719         }
720
721       num_hi_sig = 4;
722
723       /* Main loop */
724       for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
725         {
726           /* Guess the next quotient digit, quo_est, by dividing the first
727              two remaining dividend digits by the high order quotient digit.
728              quo_est is never low and is at most 2 high.  */
729           unsigned HOST_WIDE_INT tmp;
730
731           num_hi_sig = i + den_hi_sig + 1;
732           work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
733           if (num[num_hi_sig] != den[den_hi_sig])
734             quo_est = work / den[den_hi_sig];
735           else
736             quo_est = BASE - 1;
737
738           /* Refine quo_est so it's usually correct, and at most one high.  */
739           tmp = work - quo_est * den[den_hi_sig];
740           if (tmp < BASE
741               && (den[den_hi_sig - 1] * quo_est
742                   > (tmp * BASE + num[num_hi_sig - 2])))
743             quo_est--;
744
745           /* Try QUO_EST as the quotient digit, by multiplying the
746              divisor by QUO_EST and subtracting from the remaining dividend.
747              Keep in mind that QUO_EST is the I - 1st digit.  */
748
749           carry = 0;
750           for (j = 0; j <= den_hi_sig; j++)
751             {
752               work = quo_est * den[j] + carry;
753               carry = HIGHPART (work);
754               work = num[i + j] - LOWPART (work);
755               num[i + j] = LOWPART (work);
756               carry += HIGHPART (work) != 0;
757             }
758
759           /* If quo_est was high by one, then num[i] went negative and
760              we need to correct things.  */
761           if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
762             {
763               quo_est--;
764               carry = 0;                /* add divisor back in */
765               for (j = 0; j <= den_hi_sig; j++)
766                 {
767                   work = num[i + j] + den[j] + carry;
768                   carry = HIGHPART (work);
769                   num[i + j] = LOWPART (work);
770                 }
771
772               num [num_hi_sig] += carry;
773             }
774
775           /* Store the quotient digit.  */
776           quo[i] = quo_est;
777         }
778     }
779
780   decode (quo, lquo, hquo);
781
782  finish_up:
783   /* If result is negative, make it so.  */
784   if (quo_neg)
785     neg_double (*lquo, *hquo, lquo, hquo);
786
787   /* Compute trial remainder:  rem = num - (quo * den)  */
788   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
789   neg_double (*lrem, *hrem, lrem, hrem);
790   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
791
792   switch (code)
793     {
794     case TRUNC_DIV_EXPR:
795     case TRUNC_MOD_EXPR:        /* round toward zero */
796     case EXACT_DIV_EXPR:        /* for this one, it shouldn't matter */
797       return overflow;
798
799     case FLOOR_DIV_EXPR:
800     case FLOOR_MOD_EXPR:        /* round toward negative infinity */
801       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
802         {
803           /* quo = quo - 1;  */
804           add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
805                       lquo, hquo);
806         }
807       else
808         return overflow;
809       break;
810
811     case CEIL_DIV_EXPR:
812     case CEIL_MOD_EXPR:         /* round toward positive infinity */
813       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
814         {
815           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
816                       lquo, hquo);
817         }
818       else
819         return overflow;
820       break;
821
822     case ROUND_DIV_EXPR:
823     case ROUND_MOD_EXPR:        /* round to closest integer */
824       {
825         unsigned HOST_WIDE_INT labs_rem = *lrem;
826         HOST_WIDE_INT habs_rem = *hrem;
827         unsigned HOST_WIDE_INT labs_den = lden, ltwice;
828         HOST_WIDE_INT habs_den = hden, htwice;
829
830         /* Get absolute values.  */
831         if (*hrem < 0)
832           neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
833         if (hden < 0)
834           neg_double (lden, hden, &labs_den, &habs_den);
835
836         /* If (2 * abs (lrem) >= abs (lden)) */
837         mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
838                     labs_rem, habs_rem, &ltwice, &htwice);
839
840         if (((unsigned HOST_WIDE_INT) habs_den
841              < (unsigned HOST_WIDE_INT) htwice)
842             || (((unsigned HOST_WIDE_INT) habs_den
843                  == (unsigned HOST_WIDE_INT) htwice)
844                 && (labs_den < ltwice)))
845           {
846             if (*hquo < 0)
847               /* quo = quo - 1;  */
848               add_double (*lquo, *hquo,
849                           (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
850             else
851               /* quo = quo + 1; */
852               add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
853                           lquo, hquo);
854           }
855         else
856           return overflow;
857       }
858       break;
859
860     default:
861       gcc_unreachable ();
862     }
863
864   /* Compute true remainder:  rem = num - (quo * den)  */
865   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
866   neg_double (*lrem, *hrem, lrem, hrem);
867   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
868   return overflow;
869 }
870
871 /* If ARG2 divides ARG1 with zero remainder, carries out the division
872    of type CODE and returns the quotient.
873    Otherwise returns NULL_TREE.  */
874
875 static tree
876 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
877 {
878   unsigned HOST_WIDE_INT int1l, int2l;
879   HOST_WIDE_INT int1h, int2h;
880   unsigned HOST_WIDE_INT quol, reml;
881   HOST_WIDE_INT quoh, remh;
882   tree type = TREE_TYPE (arg1);
883   int uns = TYPE_UNSIGNED (type);
884
885   int1l = TREE_INT_CST_LOW (arg1);
886   int1h = TREE_INT_CST_HIGH (arg1);
887   /* &obj[0] + -128 really should be compiled as &obj[-8] rather than
888      &obj[some_exotic_number].  */
889   if (POINTER_TYPE_P (type))
890     {
891       uns = false;
892       type = signed_type_for (type);
893       fit_double_type (int1l, int1h, &int1l, &int1h,
894                        type);
895     }
896   else
897     fit_double_type (int1l, int1h, &int1l, &int1h, type);
898   int2l = TREE_INT_CST_LOW (arg2);
899   int2h = TREE_INT_CST_HIGH (arg2);
900
901   div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
902                         &quol, &quoh, &reml, &remh);
903   if (remh != 0 || reml != 0)
904     return NULL_TREE;
905
906   return build_int_cst_wide (type, quol, quoh);
907 }
908 \f
909 /* This is nonzero if we should defer warnings about undefined
910    overflow.  This facility exists because these warnings are a
911    special case.  The code to estimate loop iterations does not want
912    to issue any warnings, since it works with expressions which do not
913    occur in user code.  Various bits of cleanup code call fold(), but
914    only use the result if it has certain characteristics (e.g., is a
915    constant); that code only wants to issue a warning if the result is
916    used.  */
917
918 static int fold_deferring_overflow_warnings;
919
920 /* If a warning about undefined overflow is deferred, this is the
921    warning.  Note that this may cause us to turn two warnings into
922    one, but that is fine since it is sufficient to only give one
923    warning per expression.  */
924
925 static const char* fold_deferred_overflow_warning;
926
927 /* If a warning about undefined overflow is deferred, this is the
928    level at which the warning should be emitted.  */
929
930 static enum warn_strict_overflow_code fold_deferred_overflow_code;
931
932 /* Start deferring overflow warnings.  We could use a stack here to
933    permit nested calls, but at present it is not necessary.  */
934
935 void
936 fold_defer_overflow_warnings (void)
937 {
938   ++fold_deferring_overflow_warnings;
939 }
940
941 /* Stop deferring overflow warnings.  If there is a pending warning,
942    and ISSUE is true, then issue the warning if appropriate.  STMT is
943    the statement with which the warning should be associated (used for
944    location information); STMT may be NULL.  CODE is the level of the
945    warning--a warn_strict_overflow_code value.  This function will use
946    the smaller of CODE and the deferred code when deciding whether to
947    issue the warning.  CODE may be zero to mean to always use the
948    deferred code.  */
949
950 void
951 fold_undefer_overflow_warnings (bool issue, const_tree stmt, int code)
952 {
953   const char *warnmsg;
954   location_t locus;
955
956   gcc_assert (fold_deferring_overflow_warnings > 0);
957   --fold_deferring_overflow_warnings;
958   if (fold_deferring_overflow_warnings > 0)
959     {
960       if (fold_deferred_overflow_warning != NULL
961           && code != 0
962           && code < (int) fold_deferred_overflow_code)
963         fold_deferred_overflow_code = code;
964       return;
965     }
966
967   warnmsg = fold_deferred_overflow_warning;
968   fold_deferred_overflow_warning = NULL;
969
970   if (!issue || warnmsg == NULL)
971     return;
972
973   if (stmt != NULL_TREE && TREE_NO_WARNING (stmt))
974     return;
975
976   /* Use the smallest code level when deciding to issue the
977      warning.  */
978   if (code == 0 || code > (int) fold_deferred_overflow_code)
979     code = fold_deferred_overflow_code;
980
981   if (!issue_strict_overflow_warning (code))
982     return;
983
984   if (stmt == NULL_TREE || !expr_has_location (stmt))
985     locus = input_location;
986   else
987     locus = expr_location (stmt);
988   warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
989 }
990
991 /* Stop deferring overflow warnings, ignoring any deferred
992    warnings.  */
993
994 void
995 fold_undefer_and_ignore_overflow_warnings (void)
996 {
997   fold_undefer_overflow_warnings (false, NULL_TREE, 0);
998 }
999
1000 /* Whether we are deferring overflow warnings.  */
1001
1002 bool
1003 fold_deferring_overflow_warnings_p (void)
1004 {
1005   return fold_deferring_overflow_warnings > 0;
1006 }
1007
1008 /* This is called when we fold something based on the fact that signed
1009    overflow is undefined.  */
1010
1011 static void
1012 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1013 {
1014   gcc_assert (!flag_wrapv && !flag_trapv);
1015   if (fold_deferring_overflow_warnings > 0)
1016     {
1017       if (fold_deferred_overflow_warning == NULL
1018           || wc < fold_deferred_overflow_code)
1019         {
1020           fold_deferred_overflow_warning = gmsgid;
1021           fold_deferred_overflow_code = wc;
1022         }
1023     }
1024   else if (issue_strict_overflow_warning (wc))
1025     warning (OPT_Wstrict_overflow, gmsgid);
1026 }
1027 \f
1028 /* Return true if the built-in mathematical function specified by CODE
1029    is odd, i.e. -f(x) == f(-x).  */
1030
1031 static bool
1032 negate_mathfn_p (enum built_in_function code)
1033 {
1034   switch (code)
1035     {
1036     CASE_FLT_FN (BUILT_IN_ASIN):
1037     CASE_FLT_FN (BUILT_IN_ASINH):
1038     CASE_FLT_FN (BUILT_IN_ATAN):
1039     CASE_FLT_FN (BUILT_IN_ATANH):
1040     CASE_FLT_FN (BUILT_IN_CASIN):
1041     CASE_FLT_FN (BUILT_IN_CASINH):
1042     CASE_FLT_FN (BUILT_IN_CATAN):
1043     CASE_FLT_FN (BUILT_IN_CATANH):
1044     CASE_FLT_FN (BUILT_IN_CBRT):
1045     CASE_FLT_FN (BUILT_IN_CPROJ):
1046     CASE_FLT_FN (BUILT_IN_CSIN):
1047     CASE_FLT_FN (BUILT_IN_CSINH):
1048     CASE_FLT_FN (BUILT_IN_CTAN):
1049     CASE_FLT_FN (BUILT_IN_CTANH):
1050     CASE_FLT_FN (BUILT_IN_ERF):
1051     CASE_FLT_FN (BUILT_IN_LLROUND):
1052     CASE_FLT_FN (BUILT_IN_LROUND):
1053     CASE_FLT_FN (BUILT_IN_ROUND):
1054     CASE_FLT_FN (BUILT_IN_SIN):
1055     CASE_FLT_FN (BUILT_IN_SINH):
1056     CASE_FLT_FN (BUILT_IN_TAN):
1057     CASE_FLT_FN (BUILT_IN_TANH):
1058     CASE_FLT_FN (BUILT_IN_TRUNC):
1059       return true;
1060
1061     CASE_FLT_FN (BUILT_IN_LLRINT):
1062     CASE_FLT_FN (BUILT_IN_LRINT):
1063     CASE_FLT_FN (BUILT_IN_NEARBYINT):
1064     CASE_FLT_FN (BUILT_IN_RINT):
1065       return !flag_rounding_math;
1066     
1067     default:
1068       break;
1069     }
1070   return false;
1071 }
1072
1073 /* Check whether we may negate an integer constant T without causing
1074    overflow.  */
1075
1076 bool
1077 may_negate_without_overflow_p (const_tree t)
1078 {
1079   unsigned HOST_WIDE_INT val;
1080   unsigned int prec;
1081   tree type;
1082
1083   gcc_assert (TREE_CODE (t) == INTEGER_CST);
1084
1085   type = TREE_TYPE (t);
1086   if (TYPE_UNSIGNED (type))
1087     return false;
1088
1089   prec = TYPE_PRECISION (type);
1090   if (prec > HOST_BITS_PER_WIDE_INT)
1091     {
1092       if (TREE_INT_CST_LOW (t) != 0)
1093         return true;
1094       prec -= HOST_BITS_PER_WIDE_INT;
1095       val = TREE_INT_CST_HIGH (t);
1096     }
1097   else
1098     val = TREE_INT_CST_LOW (t);
1099   if (prec < HOST_BITS_PER_WIDE_INT)
1100     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1101   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1102 }
1103
1104 /* Determine whether an expression T can be cheaply negated using
1105    the function negate_expr without introducing undefined overflow.  */
1106
1107 static bool
1108 negate_expr_p (tree t)
1109 {
1110   tree type;
1111
1112   if (t == 0)
1113     return false;
1114
1115   type = TREE_TYPE (t);
1116
1117   STRIP_SIGN_NOPS (t);
1118   switch (TREE_CODE (t))
1119     {
1120     case INTEGER_CST:
1121       if (TYPE_OVERFLOW_WRAPS (type))
1122         return true;
1123
1124       /* Check that -CST will not overflow type.  */
1125       return may_negate_without_overflow_p (t);
1126     case BIT_NOT_EXPR:
1127       return (INTEGRAL_TYPE_P (type)
1128               && TYPE_OVERFLOW_WRAPS (type));
1129
1130     case FIXED_CST:
1131     case REAL_CST:
1132     case NEGATE_EXPR:
1133       return true;
1134
1135     case COMPLEX_CST:
1136       return negate_expr_p (TREE_REALPART (t))
1137              && negate_expr_p (TREE_IMAGPART (t));
1138
1139     case COMPLEX_EXPR:
1140       return negate_expr_p (TREE_OPERAND (t, 0))
1141              && negate_expr_p (TREE_OPERAND (t, 1));
1142
1143     case CONJ_EXPR:
1144       return negate_expr_p (TREE_OPERAND (t, 0));
1145
1146     case PLUS_EXPR:
1147       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1148           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1149         return false;
1150       /* -(A + B) -> (-B) - A.  */
1151       if (negate_expr_p (TREE_OPERAND (t, 1))
1152           && reorder_operands_p (TREE_OPERAND (t, 0),
1153                                  TREE_OPERAND (t, 1)))
1154         return true;
1155       /* -(A + B) -> (-A) - B.  */
1156       return negate_expr_p (TREE_OPERAND (t, 0));
1157
1158     case MINUS_EXPR:
1159       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
1160       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1161              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1162              && reorder_operands_p (TREE_OPERAND (t, 0),
1163                                     TREE_OPERAND (t, 1));
1164
1165     case MULT_EXPR:
1166       if (TYPE_UNSIGNED (TREE_TYPE (t)))
1167         break;
1168
1169       /* Fall through.  */
1170
1171     case RDIV_EXPR:
1172       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1173         return negate_expr_p (TREE_OPERAND (t, 1))
1174                || negate_expr_p (TREE_OPERAND (t, 0));
1175       break;
1176
1177     case TRUNC_DIV_EXPR:
1178     case ROUND_DIV_EXPR:
1179     case FLOOR_DIV_EXPR:
1180     case CEIL_DIV_EXPR:
1181     case EXACT_DIV_EXPR:
1182       /* In general we can't negate A / B, because if A is INT_MIN and
1183          B is 1, we may turn this into INT_MIN / -1 which is undefined
1184          and actually traps on some architectures.  But if overflow is
1185          undefined, we can negate, because - (INT_MIN / 1) is an
1186          overflow.  */
1187       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1188           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1189         break;
1190       return negate_expr_p (TREE_OPERAND (t, 1))
1191              || negate_expr_p (TREE_OPERAND (t, 0));
1192
1193     case NOP_EXPR:
1194       /* Negate -((double)float) as (double)(-float).  */
1195       if (TREE_CODE (type) == REAL_TYPE)
1196         {
1197           tree tem = strip_float_extensions (t);
1198           if (tem != t)
1199             return negate_expr_p (tem);
1200         }
1201       break;
1202
1203     case CALL_EXPR:
1204       /* Negate -f(x) as f(-x).  */
1205       if (negate_mathfn_p (builtin_mathfn_code (t)))
1206         return negate_expr_p (CALL_EXPR_ARG (t, 0));
1207       break;
1208
1209     case RSHIFT_EXPR:
1210       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1211       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1212         {
1213           tree op1 = TREE_OPERAND (t, 1);
1214           if (TREE_INT_CST_HIGH (op1) == 0
1215               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1216                  == TREE_INT_CST_LOW (op1))
1217             return true;
1218         }
1219       break;
1220
1221     default:
1222       break;
1223     }
1224   return false;
1225 }
1226
1227 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1228    simplification is possible.
1229    If negate_expr_p would return true for T, NULL_TREE will never be
1230    returned.  */
1231
1232 static tree
1233 fold_negate_expr (tree t)
1234 {
1235   tree type = TREE_TYPE (t);
1236   tree tem;
1237
1238   switch (TREE_CODE (t))
1239     {
1240     /* Convert - (~A) to A + 1.  */
1241     case BIT_NOT_EXPR:
1242       if (INTEGRAL_TYPE_P (type))
1243         return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
1244                             build_int_cst (type, 1));
1245       break;
1246       
1247     case INTEGER_CST:
1248       tem = fold_negate_const (t, type);
1249       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1250           || !TYPE_OVERFLOW_TRAPS (type))
1251         return tem;
1252       break;
1253
1254     case REAL_CST:
1255       tem = fold_negate_const (t, type);
1256       /* Two's complement FP formats, such as c4x, may overflow.  */
1257       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1258         return tem;
1259       break;
1260
1261     case FIXED_CST:
1262       tem = fold_negate_const (t, type);
1263       return tem;
1264
1265     case COMPLEX_CST:
1266       {
1267         tree rpart = negate_expr (TREE_REALPART (t));
1268         tree ipart = negate_expr (TREE_IMAGPART (t));
1269
1270         if ((TREE_CODE (rpart) == REAL_CST
1271              && TREE_CODE (ipart) == REAL_CST)
1272             || (TREE_CODE (rpart) == INTEGER_CST
1273                 && TREE_CODE (ipart) == INTEGER_CST))
1274           return build_complex (type, rpart, ipart);
1275       }
1276       break;
1277
1278     case COMPLEX_EXPR:
1279       if (negate_expr_p (t))
1280         return fold_build2 (COMPLEX_EXPR, type,
1281                             fold_negate_expr (TREE_OPERAND (t, 0)),
1282                             fold_negate_expr (TREE_OPERAND (t, 1)));
1283       break;
1284       
1285     case CONJ_EXPR:
1286       if (negate_expr_p (t))
1287         return fold_build1 (CONJ_EXPR, type,
1288                             fold_negate_expr (TREE_OPERAND (t, 0)));
1289       break;
1290
1291     case NEGATE_EXPR:
1292       return TREE_OPERAND (t, 0);
1293
1294     case PLUS_EXPR:
1295       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1296           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1297         {
1298           /* -(A + B) -> (-B) - A.  */
1299           if (negate_expr_p (TREE_OPERAND (t, 1))
1300               && reorder_operands_p (TREE_OPERAND (t, 0),
1301                                      TREE_OPERAND (t, 1)))
1302             {
1303               tem = negate_expr (TREE_OPERAND (t, 1));
1304               return fold_build2 (MINUS_EXPR, type,
1305                                   tem, TREE_OPERAND (t, 0));
1306             }
1307
1308           /* -(A + B) -> (-A) - B.  */
1309           if (negate_expr_p (TREE_OPERAND (t, 0)))
1310             {
1311               tem = negate_expr (TREE_OPERAND (t, 0));
1312               return fold_build2 (MINUS_EXPR, type,
1313                                   tem, TREE_OPERAND (t, 1));
1314             }
1315         }
1316       break;
1317
1318     case MINUS_EXPR:
1319       /* - (A - B) -> B - A  */
1320       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1321           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1322           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1323         return fold_build2 (MINUS_EXPR, type,
1324                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1325       break;
1326
1327     case MULT_EXPR:
1328       if (TYPE_UNSIGNED (type))
1329         break;
1330
1331       /* Fall through.  */
1332
1333     case RDIV_EXPR:
1334       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1335         {
1336           tem = TREE_OPERAND (t, 1);
1337           if (negate_expr_p (tem))
1338             return fold_build2 (TREE_CODE (t), type,
1339                                 TREE_OPERAND (t, 0), negate_expr (tem));
1340           tem = TREE_OPERAND (t, 0);
1341           if (negate_expr_p (tem))
1342             return fold_build2 (TREE_CODE (t), type,
1343                                 negate_expr (tem), TREE_OPERAND (t, 1));
1344         }
1345       break;
1346
1347     case TRUNC_DIV_EXPR:
1348     case ROUND_DIV_EXPR:
1349     case FLOOR_DIV_EXPR:
1350     case CEIL_DIV_EXPR:
1351     case EXACT_DIV_EXPR:
1352       /* In general we can't negate A / B, because if A is INT_MIN and
1353          B is 1, we may turn this into INT_MIN / -1 which is undefined
1354          and actually traps on some architectures.  But if overflow is
1355          undefined, we can negate, because - (INT_MIN / 1) is an
1356          overflow.  */
1357       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1358         {
1359           const char * const warnmsg = G_("assuming signed overflow does not "
1360                                           "occur when negating a division");
1361           tem = TREE_OPERAND (t, 1);
1362           if (negate_expr_p (tem))
1363             {
1364               if (INTEGRAL_TYPE_P (type)
1365                   && (TREE_CODE (tem) != INTEGER_CST
1366                       || integer_onep (tem)))
1367                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1368               return fold_build2 (TREE_CODE (t), type,
1369                                   TREE_OPERAND (t, 0), negate_expr (tem));
1370             }
1371           tem = TREE_OPERAND (t, 0);
1372           if (negate_expr_p (tem))
1373             {
1374               if (INTEGRAL_TYPE_P (type)
1375                   && (TREE_CODE (tem) != INTEGER_CST
1376                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1377                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1378               return fold_build2 (TREE_CODE (t), type,
1379                                   negate_expr (tem), TREE_OPERAND (t, 1));
1380             }
1381         }
1382       break;
1383
1384     case NOP_EXPR:
1385       /* Convert -((double)float) into (double)(-float).  */
1386       if (TREE_CODE (type) == REAL_TYPE)
1387         {
1388           tem = strip_float_extensions (t);
1389           if (tem != t && negate_expr_p (tem))
1390             return fold_convert (type, negate_expr (tem));
1391         }
1392       break;
1393
1394     case CALL_EXPR:
1395       /* Negate -f(x) as f(-x).  */
1396       if (negate_mathfn_p (builtin_mathfn_code (t))
1397           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1398         {
1399           tree fndecl, arg;
1400
1401           fndecl = get_callee_fndecl (t);
1402           arg = negate_expr (CALL_EXPR_ARG (t, 0));
1403           return build_call_expr (fndecl, 1, arg);
1404         }
1405       break;
1406
1407     case RSHIFT_EXPR:
1408       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1409       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1410         {
1411           tree op1 = TREE_OPERAND (t, 1);
1412           if (TREE_INT_CST_HIGH (op1) == 0
1413               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1414                  == TREE_INT_CST_LOW (op1))
1415             {
1416               tree ntype = TYPE_UNSIGNED (type)
1417                            ? signed_type_for (type)
1418                            : unsigned_type_for (type);
1419               tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1420               temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1421               return fold_convert (type, temp);
1422             }
1423         }
1424       break;
1425
1426     default:
1427       break;
1428     }
1429
1430   return NULL_TREE;
1431 }
1432
1433 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1434    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
1435    return NULL_TREE. */
1436
1437 static tree
1438 negate_expr (tree t)
1439 {
1440   tree type, tem;
1441
1442   if (t == NULL_TREE)
1443     return NULL_TREE;
1444
1445   type = TREE_TYPE (t);
1446   STRIP_SIGN_NOPS (t);
1447
1448   tem = fold_negate_expr (t);
1449   if (!tem)
1450     tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1451   return fold_convert (type, tem);
1452 }
1453 \f
1454 /* Split a tree IN into a constant, literal and variable parts that could be
1455    combined with CODE to make IN.  "constant" means an expression with
1456    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1457    commutative arithmetic operation.  Store the constant part into *CONP,
1458    the literal in *LITP and return the variable part.  If a part isn't
1459    present, set it to null.  If the tree does not decompose in this way,
1460    return the entire tree as the variable part and the other parts as null.
1461
1462    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1463    case, we negate an operand that was subtracted.  Except if it is a
1464    literal for which we use *MINUS_LITP instead.
1465
1466    If NEGATE_P is true, we are negating all of IN, again except a literal
1467    for which we use *MINUS_LITP instead.
1468
1469    If IN is itself a literal or constant, return it as appropriate.
1470
1471    Note that we do not guarantee that any of the three values will be the
1472    same type as IN, but they will have the same signedness and mode.  */
1473
1474 static tree
1475 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1476             tree *minus_litp, int negate_p)
1477 {
1478   tree var = 0;
1479
1480   *conp = 0;
1481   *litp = 0;
1482   *minus_litp = 0;
1483
1484   /* Strip any conversions that don't change the machine mode or signedness.  */
1485   STRIP_SIGN_NOPS (in);
1486
1487   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1488       || TREE_CODE (in) == FIXED_CST)
1489     *litp = in;
1490   else if (TREE_CODE (in) == code
1491            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1492                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1493                /* We can associate addition and subtraction together (even
1494                   though the C standard doesn't say so) for integers because
1495                   the value is not affected.  For reals, the value might be
1496                   affected, so we can't.  */
1497                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1498                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1499     {
1500       tree op0 = TREE_OPERAND (in, 0);
1501       tree op1 = TREE_OPERAND (in, 1);
1502       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1503       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1504
1505       /* First see if either of the operands is a literal, then a constant.  */
1506       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1507           || TREE_CODE (op0) == FIXED_CST)
1508         *litp = op0, op0 = 0;
1509       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1510                || TREE_CODE (op1) == FIXED_CST)
1511         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1512
1513       if (op0 != 0 && TREE_CONSTANT (op0))
1514         *conp = op0, op0 = 0;
1515       else if (op1 != 0 && TREE_CONSTANT (op1))
1516         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1517
1518       /* If we haven't dealt with either operand, this is not a case we can
1519          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1520       if (op0 != 0 && op1 != 0)
1521         var = in;
1522       else if (op0 != 0)
1523         var = op0;
1524       else
1525         var = op1, neg_var_p = neg1_p;
1526
1527       /* Now do any needed negations.  */
1528       if (neg_litp_p)
1529         *minus_litp = *litp, *litp = 0;
1530       if (neg_conp_p)
1531         *conp = negate_expr (*conp);
1532       if (neg_var_p)
1533         var = negate_expr (var);
1534     }
1535   else if (TREE_CONSTANT (in))
1536     *conp = in;
1537   else
1538     var = in;
1539
1540   if (negate_p)
1541     {
1542       if (*litp)
1543         *minus_litp = *litp, *litp = 0;
1544       else if (*minus_litp)
1545         *litp = *minus_litp, *minus_litp = 0;
1546       *conp = negate_expr (*conp);
1547       var = negate_expr (var);
1548     }
1549
1550   return var;
1551 }
1552
1553 /* Re-associate trees split by the above function.  T1 and T2 are either
1554    expressions to associate or null.  Return the new expression, if any.  If
1555    we build an operation, do it in TYPE and with CODE.  */
1556
1557 static tree
1558 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1559 {
1560   if (t1 == 0)
1561     return t2;
1562   else if (t2 == 0)
1563     return t1;
1564
1565   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1566      try to fold this since we will have infinite recursion.  But do
1567      deal with any NEGATE_EXPRs.  */
1568   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1569       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1570     {
1571       if (code == PLUS_EXPR)
1572         {
1573           if (TREE_CODE (t1) == NEGATE_EXPR)
1574             return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1575                            fold_convert (type, TREE_OPERAND (t1, 0)));
1576           else if (TREE_CODE (t2) == NEGATE_EXPR)
1577             return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1578                            fold_convert (type, TREE_OPERAND (t2, 0)));
1579           else if (integer_zerop (t2))
1580             return fold_convert (type, t1);
1581         }
1582       else if (code == MINUS_EXPR)
1583         {
1584           if (integer_zerop (t2))
1585             return fold_convert (type, t1);
1586         }
1587
1588       return build2 (code, type, fold_convert (type, t1),
1589                      fold_convert (type, t2));
1590     }
1591
1592   return fold_build2 (code, type, fold_convert (type, t1),
1593                       fold_convert (type, t2));
1594 }
1595 \f
1596 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1597    for use in int_const_binop, size_binop and size_diffop.  */
1598
1599 static bool
1600 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1601 {
1602   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1603     return false;
1604   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1605     return false;
1606
1607   switch (code)
1608     {
1609     case LSHIFT_EXPR:
1610     case RSHIFT_EXPR:
1611     case LROTATE_EXPR:
1612     case RROTATE_EXPR:
1613       return true;
1614
1615     default:
1616       break;
1617     }
1618
1619   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1620          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1621          && TYPE_MODE (type1) == TYPE_MODE (type2);
1622 }
1623
1624
1625 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1626    to produce a new constant.  Return NULL_TREE if we don't know how
1627    to evaluate CODE at compile-time.
1628
1629    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1630
1631 tree
1632 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1633 {
1634   unsigned HOST_WIDE_INT int1l, int2l;
1635   HOST_WIDE_INT int1h, int2h;
1636   unsigned HOST_WIDE_INT low;
1637   HOST_WIDE_INT hi;
1638   unsigned HOST_WIDE_INT garbagel;
1639   HOST_WIDE_INT garbageh;
1640   tree t;
1641   tree type = TREE_TYPE (arg1);
1642   int uns = TYPE_UNSIGNED (type);
1643   int is_sizetype
1644     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1645   int overflow = 0;
1646
1647   int1l = TREE_INT_CST_LOW (arg1);
1648   int1h = TREE_INT_CST_HIGH (arg1);
1649   int2l = TREE_INT_CST_LOW (arg2);
1650   int2h = TREE_INT_CST_HIGH (arg2);
1651
1652   switch (code)
1653     {
1654     case BIT_IOR_EXPR:
1655       low = int1l | int2l, hi = int1h | int2h;
1656       break;
1657
1658     case BIT_XOR_EXPR:
1659       low = int1l ^ int2l, hi = int1h ^ int2h;
1660       break;
1661
1662     case BIT_AND_EXPR:
1663       low = int1l & int2l, hi = int1h & int2h;
1664       break;
1665
1666     case RSHIFT_EXPR:
1667       int2l = -int2l;
1668     case LSHIFT_EXPR:
1669       /* It's unclear from the C standard whether shifts can overflow.
1670          The following code ignores overflow; perhaps a C standard
1671          interpretation ruling is needed.  */
1672       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1673                      &low, &hi, !uns);
1674       break;
1675
1676     case RROTATE_EXPR:
1677       int2l = - int2l;
1678     case LROTATE_EXPR:
1679       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1680                       &low, &hi);
1681       break;
1682
1683     case PLUS_EXPR:
1684       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1685       break;
1686
1687     case MINUS_EXPR:
1688       neg_double (int2l, int2h, &low, &hi);
1689       add_double (int1l, int1h, low, hi, &low, &hi);
1690       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1691       break;
1692
1693     case MULT_EXPR:
1694       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1695       break;
1696
1697     case TRUNC_DIV_EXPR:
1698     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1699     case EXACT_DIV_EXPR:
1700       /* This is a shortcut for a common special case.  */
1701       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1702           && !TREE_OVERFLOW (arg1)
1703           && !TREE_OVERFLOW (arg2)
1704           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1705         {
1706           if (code == CEIL_DIV_EXPR)
1707             int1l += int2l - 1;
1708
1709           low = int1l / int2l, hi = 0;
1710           break;
1711         }
1712
1713       /* ... fall through ...  */
1714
1715     case ROUND_DIV_EXPR:
1716       if (int2h == 0 && int2l == 0)
1717         return NULL_TREE;
1718       if (int2h == 0 && int2l == 1)
1719         {
1720           low = int1l, hi = int1h;
1721           break;
1722         }
1723       if (int1l == int2l && int1h == int2h
1724           && ! (int1l == 0 && int1h == 0))
1725         {
1726           low = 1, hi = 0;
1727           break;
1728         }
1729       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1730                                        &low, &hi, &garbagel, &garbageh);
1731       break;
1732
1733     case TRUNC_MOD_EXPR:
1734     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1735       /* This is a shortcut for a common special case.  */
1736       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1737           && !TREE_OVERFLOW (arg1)
1738           && !TREE_OVERFLOW (arg2)
1739           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1740         {
1741           if (code == CEIL_MOD_EXPR)
1742             int1l += int2l - 1;
1743           low = int1l % int2l, hi = 0;
1744           break;
1745         }
1746
1747       /* ... fall through ...  */
1748
1749     case ROUND_MOD_EXPR:
1750       if (int2h == 0 && int2l == 0)
1751         return NULL_TREE;
1752       overflow = div_and_round_double (code, uns,
1753                                        int1l, int1h, int2l, int2h,
1754                                        &garbagel, &garbageh, &low, &hi);
1755       break;
1756
1757     case MIN_EXPR:
1758     case MAX_EXPR:
1759       if (uns)
1760         low = (((unsigned HOST_WIDE_INT) int1h
1761                 < (unsigned HOST_WIDE_INT) int2h)
1762                || (((unsigned HOST_WIDE_INT) int1h
1763                     == (unsigned HOST_WIDE_INT) int2h)
1764                    && int1l < int2l));
1765       else
1766         low = (int1h < int2h
1767                || (int1h == int2h && int1l < int2l));
1768
1769       if (low == (code == MIN_EXPR))
1770         low = int1l, hi = int1h;
1771       else
1772         low = int2l, hi = int2h;
1773       break;
1774
1775     default:
1776       return NULL_TREE;
1777     }
1778
1779   if (notrunc)
1780     {
1781       t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1782
1783       /* Propagate overflow flags ourselves.  */
1784       if (((!uns || is_sizetype) && overflow)
1785           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1786         {
1787           t = copy_node (t);
1788           TREE_OVERFLOW (t) = 1;
1789         }
1790     }
1791   else
1792     t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1793                                ((!uns || is_sizetype) && overflow)
1794                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1795
1796   return t;
1797 }
1798
1799 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1800    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1801    are the same kind of constant and the same machine mode.  Return zero if
1802    combining the constants is not allowed in the current operating mode.
1803
1804    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1805
1806 static tree
1807 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1808 {
1809   /* Sanity check for the recursive cases.  */
1810   if (!arg1 || !arg2)
1811     return NULL_TREE;
1812
1813   STRIP_NOPS (arg1);
1814   STRIP_NOPS (arg2);
1815
1816   if (TREE_CODE (arg1) == INTEGER_CST)
1817     return int_const_binop (code, arg1, arg2, notrunc);
1818
1819   if (TREE_CODE (arg1) == REAL_CST)
1820     {
1821       enum machine_mode mode;
1822       REAL_VALUE_TYPE d1;
1823       REAL_VALUE_TYPE d2;
1824       REAL_VALUE_TYPE value;
1825       REAL_VALUE_TYPE result;
1826       bool inexact;
1827       tree t, type;
1828
1829       /* The following codes are handled by real_arithmetic.  */
1830       switch (code)
1831         {
1832         case PLUS_EXPR:
1833         case MINUS_EXPR:
1834         case MULT_EXPR:
1835         case RDIV_EXPR:
1836         case MIN_EXPR:
1837         case MAX_EXPR:
1838           break;
1839
1840         default:
1841           return NULL_TREE;
1842         }
1843
1844       d1 = TREE_REAL_CST (arg1);
1845       d2 = TREE_REAL_CST (arg2);
1846
1847       type = TREE_TYPE (arg1);
1848       mode = TYPE_MODE (type);
1849
1850       /* Don't perform operation if we honor signaling NaNs and
1851          either operand is a NaN.  */
1852       if (HONOR_SNANS (mode)
1853           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1854         return NULL_TREE;
1855
1856       /* Don't perform operation if it would raise a division
1857          by zero exception.  */
1858       if (code == RDIV_EXPR
1859           && REAL_VALUES_EQUAL (d2, dconst0)
1860           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1861         return NULL_TREE;
1862
1863       /* If either operand is a NaN, just return it.  Otherwise, set up
1864          for floating-point trap; we return an overflow.  */
1865       if (REAL_VALUE_ISNAN (d1))
1866         return arg1;
1867       else if (REAL_VALUE_ISNAN (d2))
1868         return arg2;
1869
1870       inexact = real_arithmetic (&value, code, &d1, &d2);
1871       real_convert (&result, mode, &value);
1872
1873       /* Don't constant fold this floating point operation if
1874          the result has overflowed and flag_trapping_math.  */
1875       if (flag_trapping_math
1876           && MODE_HAS_INFINITIES (mode)
1877           && REAL_VALUE_ISINF (result)
1878           && !REAL_VALUE_ISINF (d1)
1879           && !REAL_VALUE_ISINF (d2))
1880         return NULL_TREE;
1881
1882       /* Don't constant fold this floating point operation if the
1883          result may dependent upon the run-time rounding mode and
1884          flag_rounding_math is set, or if GCC's software emulation
1885          is unable to accurately represent the result.  */
1886       if ((flag_rounding_math
1887            || (REAL_MODE_FORMAT_COMPOSITE_P (mode)
1888                && !flag_unsafe_math_optimizations))
1889           && (inexact || !real_identical (&result, &value)))
1890         return NULL_TREE;
1891
1892       t = build_real (type, result);
1893
1894       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1895       return t;
1896     }
1897
1898   if (TREE_CODE (arg1) == FIXED_CST)
1899     {
1900       FIXED_VALUE_TYPE f1;
1901       FIXED_VALUE_TYPE f2;
1902       FIXED_VALUE_TYPE result;
1903       tree t, type;
1904       int sat_p;
1905       bool overflow_p;
1906
1907       /* The following codes are handled by fixed_arithmetic.  */
1908       switch (code)
1909         {
1910         case PLUS_EXPR:
1911         case MINUS_EXPR:
1912         case MULT_EXPR:
1913         case TRUNC_DIV_EXPR:
1914           f2 = TREE_FIXED_CST (arg2);
1915           break;
1916
1917         case LSHIFT_EXPR:
1918         case RSHIFT_EXPR:
1919           f2.data.high = TREE_INT_CST_HIGH (arg2);
1920           f2.data.low = TREE_INT_CST_LOW (arg2);
1921           f2.mode = SImode;
1922           break;
1923
1924         default:
1925           return NULL_TREE;
1926         }
1927
1928       f1 = TREE_FIXED_CST (arg1);
1929       type = TREE_TYPE (arg1);
1930       sat_p = TYPE_SATURATING (type);
1931       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1932       t = build_fixed (type, result);
1933       /* Propagate overflow flags.  */
1934       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1935         {
1936           TREE_OVERFLOW (t) = 1;
1937           TREE_CONSTANT_OVERFLOW (t) = 1;
1938         }
1939       else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1940         TREE_CONSTANT_OVERFLOW (t) = 1;
1941       return t;
1942     }
1943
1944   if (TREE_CODE (arg1) == COMPLEX_CST)
1945     {
1946       tree type = TREE_TYPE (arg1);
1947       tree r1 = TREE_REALPART (arg1);
1948       tree i1 = TREE_IMAGPART (arg1);
1949       tree r2 = TREE_REALPART (arg2);
1950       tree i2 = TREE_IMAGPART (arg2);
1951       tree real, imag;
1952
1953       switch (code)
1954         {
1955         case PLUS_EXPR:
1956         case MINUS_EXPR:
1957           real = const_binop (code, r1, r2, notrunc);
1958           imag = const_binop (code, i1, i2, notrunc);
1959           break;
1960
1961         case MULT_EXPR:
1962           real = const_binop (MINUS_EXPR,
1963                               const_binop (MULT_EXPR, r1, r2, notrunc),
1964                               const_binop (MULT_EXPR, i1, i2, notrunc),
1965                               notrunc);
1966           imag = const_binop (PLUS_EXPR,
1967                               const_binop (MULT_EXPR, r1, i2, notrunc),
1968                               const_binop (MULT_EXPR, i1, r2, notrunc),
1969                               notrunc);
1970           break;
1971
1972         case RDIV_EXPR:
1973           {
1974             tree magsquared
1975               = const_binop (PLUS_EXPR,
1976                              const_binop (MULT_EXPR, r2, r2, notrunc),
1977                              const_binop (MULT_EXPR, i2, i2, notrunc),
1978                              notrunc);
1979             tree t1
1980               = const_binop (PLUS_EXPR,
1981                              const_binop (MULT_EXPR, r1, r2, notrunc),
1982                              const_binop (MULT_EXPR, i1, i2, notrunc),
1983                              notrunc);
1984             tree t2
1985               = const_binop (MINUS_EXPR,
1986                              const_binop (MULT_EXPR, i1, r2, notrunc),
1987                              const_binop (MULT_EXPR, r1, i2, notrunc),
1988                              notrunc);
1989
1990             if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1991               code = TRUNC_DIV_EXPR;
1992
1993             real = const_binop (code, t1, magsquared, notrunc);
1994             imag = const_binop (code, t2, magsquared, notrunc);
1995           }
1996           break;
1997
1998         default:
1999           return NULL_TREE;
2000         }
2001
2002       if (real && imag)
2003         return build_complex (type, real, imag);
2004     }
2005
2006   return NULL_TREE;
2007 }
2008
2009 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
2010    indicates which particular sizetype to create.  */
2011
2012 tree
2013 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2014 {
2015   return build_int_cst (sizetype_tab[(int) kind], number);
2016 }
2017 \f
2018 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
2019    is a tree code.  The type of the result is taken from the operands.
2020    Both must be equivalent integer types, ala int_binop_types_match_p.
2021    If the operands are constant, so is the result.  */
2022
2023 tree
2024 size_binop (enum tree_code code, tree arg0, tree arg1)
2025 {
2026   tree type = TREE_TYPE (arg0);
2027
2028   if (arg0 == error_mark_node || arg1 == error_mark_node)
2029     return error_mark_node;
2030
2031   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2032                                        TREE_TYPE (arg1)));
2033
2034   /* Handle the special case of two integer constants faster.  */
2035   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2036     {
2037       /* And some specific cases even faster than that.  */
2038       if (code == PLUS_EXPR)
2039         {
2040           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2041             return arg1;
2042           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2043             return arg0;
2044         }
2045       else if (code == MINUS_EXPR)
2046         {
2047           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2048             return arg0;
2049         }
2050       else if (code == MULT_EXPR)
2051         {
2052           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2053             return arg1;
2054         }
2055
2056       /* Handle general case of two integer constants.  */
2057       return int_const_binop (code, arg0, arg1, 0);
2058     }
2059
2060   return fold_build2 (code, type, arg0, arg1);
2061 }
2062
2063 /* Given two values, either both of sizetype or both of bitsizetype,
2064    compute the difference between the two values.  Return the value
2065    in signed type corresponding to the type of the operands.  */
2066
2067 tree
2068 size_diffop (tree arg0, tree arg1)
2069 {
2070   tree type = TREE_TYPE (arg0);
2071   tree ctype;
2072
2073   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2074                                        TREE_TYPE (arg1)));
2075
2076   /* If the type is already signed, just do the simple thing.  */
2077   if (!TYPE_UNSIGNED (type))
2078     return size_binop (MINUS_EXPR, arg0, arg1);
2079
2080   if (type == sizetype)
2081     ctype = ssizetype;
2082   else if (type == bitsizetype)
2083     ctype = sbitsizetype;
2084   else
2085     ctype = signed_type_for (type);
2086
2087   /* If either operand is not a constant, do the conversions to the signed
2088      type and subtract.  The hardware will do the right thing with any
2089      overflow in the subtraction.  */
2090   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2091     return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2092                        fold_convert (ctype, arg1));
2093
2094   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2095      Otherwise, subtract the other way, convert to CTYPE (we know that can't
2096      overflow) and negate (which can't either).  Special-case a result
2097      of zero while we're here.  */
2098   if (tree_int_cst_equal (arg0, arg1))
2099     return build_int_cst (ctype, 0);
2100   else if (tree_int_cst_lt (arg1, arg0))
2101     return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2102   else
2103     return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2104                        fold_convert (ctype, size_binop (MINUS_EXPR,
2105                                                         arg1, arg0)));
2106 }
2107 \f
2108 /* A subroutine of fold_convert_const handling conversions of an
2109    INTEGER_CST to another integer type.  */
2110
2111 static tree
2112 fold_convert_const_int_from_int (tree type, const_tree arg1)
2113 {
2114   tree t;
2115
2116   /* Given an integer constant, make new constant with new type,
2117      appropriately sign-extended or truncated.  */
2118   t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2119                              TREE_INT_CST_HIGH (arg1),
2120                              /* Don't set the overflow when
2121                                 converting from a pointer,  */
2122                              !POINTER_TYPE_P (TREE_TYPE (arg1))
2123                              /* or to a sizetype with same signedness
2124                                 and the precision is unchanged.
2125                                 ???  sizetype is always sign-extended,
2126                                 but its signedness depends on the
2127                                 frontend.  Thus we see spurious overflows
2128                                 here if we do not check this.  */
2129                              && !((TYPE_PRECISION (TREE_TYPE (arg1))
2130                                    == TYPE_PRECISION (type))
2131                                   && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2132                                       == TYPE_UNSIGNED (type))
2133                                   && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2134                                        && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2135                                       || (TREE_CODE (type) == INTEGER_TYPE
2136                                           && TYPE_IS_SIZETYPE (type)))),
2137                              (TREE_INT_CST_HIGH (arg1) < 0
2138                               && (TYPE_UNSIGNED (type)
2139                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2140                              | TREE_OVERFLOW (arg1));
2141
2142   return t;
2143 }
2144
2145 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2146    to an integer type.  */
2147
2148 static tree
2149 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2150 {
2151   int overflow = 0;
2152   tree t;
2153
2154   /* The following code implements the floating point to integer
2155      conversion rules required by the Java Language Specification,
2156      that IEEE NaNs are mapped to zero and values that overflow
2157      the target precision saturate, i.e. values greater than
2158      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2159      are mapped to INT_MIN.  These semantics are allowed by the
2160      C and C++ standards that simply state that the behavior of
2161      FP-to-integer conversion is unspecified upon overflow.  */
2162
2163   HOST_WIDE_INT high, low;
2164   REAL_VALUE_TYPE r;
2165   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2166
2167   switch (code)
2168     {
2169     case FIX_TRUNC_EXPR:
2170       real_trunc (&r, VOIDmode, &x);
2171       break;
2172
2173     default:
2174       gcc_unreachable ();
2175     }
2176
2177   /* If R is NaN, return zero and show we have an overflow.  */
2178   if (REAL_VALUE_ISNAN (r))
2179     {
2180       overflow = 1;
2181       high = 0;
2182       low = 0;
2183     }
2184
2185   /* See if R is less than the lower bound or greater than the
2186      upper bound.  */
2187
2188   if (! overflow)
2189     {
2190       tree lt = TYPE_MIN_VALUE (type);
2191       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2192       if (REAL_VALUES_LESS (r, l))
2193         {
2194           overflow = 1;
2195           high = TREE_INT_CST_HIGH (lt);
2196           low = TREE_INT_CST_LOW (lt);
2197         }
2198     }
2199
2200   if (! overflow)
2201     {
2202       tree ut = TYPE_MAX_VALUE (type);
2203       if (ut)
2204         {
2205           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2206           if (REAL_VALUES_LESS (u, r))
2207             {
2208               overflow = 1;
2209               high = TREE_INT_CST_HIGH (ut);
2210               low = TREE_INT_CST_LOW (ut);
2211             }
2212         }
2213     }
2214
2215   if (! overflow)
2216     REAL_VALUE_TO_INT (&low, &high, r);
2217
2218   t = force_fit_type_double (type, low, high, -1,
2219                              overflow | TREE_OVERFLOW (arg1));
2220   return t;
2221 }
2222
2223 /* A subroutine of fold_convert_const handling conversions of a
2224    FIXED_CST to an integer type.  */
2225
2226 static tree
2227 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2228 {
2229   tree t;
2230   double_int temp, temp_trunc;
2231   unsigned int mode;
2232
2233   /* Right shift FIXED_CST to temp by fbit.  */
2234   temp = TREE_FIXED_CST (arg1).data;
2235   mode = TREE_FIXED_CST (arg1).mode;
2236   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2237     {
2238       lshift_double (temp.low, temp.high,
2239                      - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2240                      &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2241
2242       /* Left shift temp to temp_trunc by fbit.  */
2243       lshift_double (temp.low, temp.high,
2244                      GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2245                      &temp_trunc.low, &temp_trunc.high,
2246                      SIGNED_FIXED_POINT_MODE_P (mode));
2247     }
2248   else
2249     {
2250       temp.low = 0;
2251       temp.high = 0;
2252       temp_trunc.low = 0;
2253       temp_trunc.high = 0;
2254     }
2255
2256   /* If FIXED_CST is negative, we need to round the value toward 0.
2257      By checking if the fractional bits are not zero to add 1 to temp.  */
2258   if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2259       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2260     {
2261       double_int one;
2262       one.low = 1;
2263       one.high = 0;
2264       temp = double_int_add (temp, one);
2265     }
2266
2267   /* Given a fixed-point constant, make new constant with new type,
2268      appropriately sign-extended or truncated.  */
2269   t = force_fit_type_double (type, temp.low, temp.high, -1,
2270                              (temp.high < 0
2271                               && (TYPE_UNSIGNED (type)
2272                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2273                              | TREE_OVERFLOW (arg1));
2274
2275   return t;
2276 }
2277
2278 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2279    to another floating point type.  */
2280
2281 static tree
2282 fold_convert_const_real_from_real (tree type, const_tree arg1)
2283 {
2284   REAL_VALUE_TYPE value;
2285   tree t;
2286
2287   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2288   t = build_real (type, value);
2289
2290   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2291   return t;
2292 }
2293
2294 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2295    to a floating point type.  */
2296
2297 static tree
2298 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2299 {
2300   REAL_VALUE_TYPE value;
2301   tree t;
2302
2303   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2304   t = build_real (type, value);
2305
2306   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2307   TREE_CONSTANT_OVERFLOW (t)
2308     = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2309   return t;
2310 }
2311
2312 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2313    to another fixed-point type.  */
2314
2315 static tree
2316 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2317 {
2318   FIXED_VALUE_TYPE value;
2319   tree t;
2320   bool overflow_p;
2321
2322   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2323                               TYPE_SATURATING (type));
2324   t = build_fixed (type, value);
2325
2326   /* Propagate overflow flags.  */
2327   if (overflow_p | TREE_OVERFLOW (arg1))
2328     {
2329       TREE_OVERFLOW (t) = 1;
2330       TREE_CONSTANT_OVERFLOW (t) = 1;
2331     }
2332   else if (TREE_CONSTANT_OVERFLOW (arg1))
2333     TREE_CONSTANT_OVERFLOW (t) = 1;
2334   return t;
2335 }
2336
2337 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2338    to a fixed-point type.  */
2339
2340 static tree
2341 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2342 {
2343   FIXED_VALUE_TYPE value;
2344   tree t;
2345   bool overflow_p;
2346
2347   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2348                                        TREE_INT_CST (arg1),
2349                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
2350                                        TYPE_SATURATING (type));
2351   t = build_fixed (type, value);
2352
2353   /* Propagate overflow flags.  */
2354   if (overflow_p | TREE_OVERFLOW (arg1))
2355     {
2356       TREE_OVERFLOW (t) = 1;
2357       TREE_CONSTANT_OVERFLOW (t) = 1;
2358     }
2359   else if (TREE_CONSTANT_OVERFLOW (arg1))
2360     TREE_CONSTANT_OVERFLOW (t) = 1;
2361   return t;
2362 }
2363
2364 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2365    to a fixed-point type.  */
2366
2367 static tree
2368 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2369 {
2370   FIXED_VALUE_TYPE value;
2371   tree t;
2372   bool overflow_p;
2373
2374   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2375                                         &TREE_REAL_CST (arg1),
2376                                         TYPE_SATURATING (type));
2377   t = build_fixed (type, value);
2378
2379   /* Propagate overflow flags.  */
2380   if (overflow_p | TREE_OVERFLOW (arg1))
2381     {
2382       TREE_OVERFLOW (t) = 1;
2383       TREE_CONSTANT_OVERFLOW (t) = 1;
2384     }
2385   else if (TREE_CONSTANT_OVERFLOW (arg1))
2386     TREE_CONSTANT_OVERFLOW (t) = 1;
2387   return t;
2388 }
2389
2390 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2391    type TYPE.  If no simplification can be done return NULL_TREE.  */
2392
2393 static tree
2394 fold_convert_const (enum tree_code code, tree type, tree arg1)
2395 {
2396   if (TREE_TYPE (arg1) == type)
2397     return arg1;
2398
2399   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2400     {
2401       if (TREE_CODE (arg1) == INTEGER_CST)
2402         return fold_convert_const_int_from_int (type, arg1);
2403       else if (TREE_CODE (arg1) == REAL_CST)
2404         return fold_convert_const_int_from_real (code, type, arg1);
2405       else if (TREE_CODE (arg1) == FIXED_CST)
2406         return fold_convert_const_int_from_fixed (type, arg1);
2407     }
2408   else if (TREE_CODE (type) == REAL_TYPE)
2409     {
2410       if (TREE_CODE (arg1) == INTEGER_CST)
2411         return build_real_from_int_cst (type, arg1);
2412       else if (TREE_CODE (arg1) == REAL_CST)
2413         return fold_convert_const_real_from_real (type, arg1);
2414       else if (TREE_CODE (arg1) == FIXED_CST)
2415         return fold_convert_const_real_from_fixed (type, arg1);
2416     }
2417   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2418     {
2419       if (TREE_CODE (arg1) == FIXED_CST)
2420         return fold_convert_const_fixed_from_fixed (type, arg1);
2421       else if (TREE_CODE (arg1) == INTEGER_CST)
2422         return fold_convert_const_fixed_from_int (type, arg1);
2423       else if (TREE_CODE (arg1) == REAL_CST)
2424         return fold_convert_const_fixed_from_real (type, arg1);
2425     }
2426   return NULL_TREE;
2427 }
2428
2429 /* Construct a vector of zero elements of vector type TYPE.  */
2430
2431 static tree
2432 build_zero_vector (tree type)
2433 {
2434   tree elem, list;
2435   int i, units;
2436
2437   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2438   units = TYPE_VECTOR_SUBPARTS (type);
2439   
2440   list = NULL_TREE;
2441   for (i = 0; i < units; i++)
2442     list = tree_cons (NULL_TREE, elem, list);
2443   return build_vector (type, list);
2444 }
2445
2446 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
2447
2448 bool
2449 fold_convertible_p (const_tree type, const_tree arg)
2450 {
2451   tree orig = TREE_TYPE (arg);
2452
2453   if (type == orig)
2454     return true;
2455
2456   if (TREE_CODE (arg) == ERROR_MARK
2457       || TREE_CODE (type) == ERROR_MARK
2458       || TREE_CODE (orig) == ERROR_MARK)
2459     return false;
2460
2461   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2462     return true;
2463
2464   switch (TREE_CODE (type))
2465     {
2466     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2467     case POINTER_TYPE: case REFERENCE_TYPE:
2468     case OFFSET_TYPE:
2469       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2470           || TREE_CODE (orig) == OFFSET_TYPE)
2471         return true;
2472       return (TREE_CODE (orig) == VECTOR_TYPE
2473               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2474
2475     case REAL_TYPE:
2476     case FIXED_POINT_TYPE:
2477     case COMPLEX_TYPE:
2478     case VECTOR_TYPE:
2479     case VOID_TYPE:
2480       return TREE_CODE (type) == TREE_CODE (orig);
2481
2482     default:
2483       return false;
2484     }
2485 }
2486
2487 /* Convert expression ARG to type TYPE.  Used by the middle-end for
2488    simple conversions in preference to calling the front-end's convert.  */
2489
2490 tree
2491 fold_convert (tree type, tree arg)
2492 {
2493   tree orig = TREE_TYPE (arg);
2494   tree tem;
2495
2496   if (type == orig)
2497     return arg;
2498
2499   if (TREE_CODE (arg) == ERROR_MARK
2500       || TREE_CODE (type) == ERROR_MARK
2501       || TREE_CODE (orig) == ERROR_MARK)
2502     return error_mark_node;
2503
2504   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2505     return fold_build1 (NOP_EXPR, type, arg);
2506
2507   switch (TREE_CODE (type))
2508     {
2509     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2510     case POINTER_TYPE: case REFERENCE_TYPE:
2511     case OFFSET_TYPE:
2512       if (TREE_CODE (arg) == INTEGER_CST)
2513         {
2514           tem = fold_convert_const (NOP_EXPR, type, arg);
2515           if (tem != NULL_TREE)
2516             return tem;
2517         }
2518       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2519           || TREE_CODE (orig) == OFFSET_TYPE)
2520         return fold_build1 (NOP_EXPR, type, arg);
2521       if (TREE_CODE (orig) == COMPLEX_TYPE)
2522         {
2523           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2524           return fold_convert (type, tem);
2525         }
2526       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2527                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2528       return fold_build1 (NOP_EXPR, type, arg);
2529
2530     case REAL_TYPE:
2531       if (TREE_CODE (arg) == INTEGER_CST)
2532         {
2533           tem = fold_convert_const (FLOAT_EXPR, type, arg);
2534           if (tem != NULL_TREE)
2535             return tem;
2536         }
2537       else if (TREE_CODE (arg) == REAL_CST)
2538         {
2539           tem = fold_convert_const (NOP_EXPR, type, arg);
2540           if (tem != NULL_TREE)
2541             return tem;
2542         }
2543       else if (TREE_CODE (arg) == FIXED_CST)
2544         {
2545           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2546           if (tem != NULL_TREE)
2547             return tem;
2548         }
2549
2550       switch (TREE_CODE (orig))
2551         {
2552         case INTEGER_TYPE:
2553         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2554         case POINTER_TYPE: case REFERENCE_TYPE:
2555           return fold_build1 (FLOAT_EXPR, type, arg);
2556
2557         case REAL_TYPE:
2558           return fold_build1 (NOP_EXPR, type, arg);
2559
2560         case FIXED_POINT_TYPE:
2561           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2562
2563         case COMPLEX_TYPE:
2564           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2565           return fold_convert (type, tem);
2566
2567         default:
2568           gcc_unreachable ();
2569         }
2570
2571     case FIXED_POINT_TYPE:
2572       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2573           || TREE_CODE (arg) == REAL_CST)
2574         {
2575           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2576           if (tem != NULL_TREE)
2577             return tem;
2578         }
2579
2580       switch (TREE_CODE (orig))
2581         {
2582         case FIXED_POINT_TYPE:
2583         case INTEGER_TYPE:
2584         case ENUMERAL_TYPE:
2585         case BOOLEAN_TYPE:
2586         case REAL_TYPE:
2587           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2588
2589         case COMPLEX_TYPE:
2590           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2591           return fold_convert (type, tem);
2592
2593         default:
2594           gcc_unreachable ();
2595         }
2596
2597     case COMPLEX_TYPE:
2598       switch (TREE_CODE (orig))
2599         {
2600         case INTEGER_TYPE:
2601         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2602         case POINTER_TYPE: case REFERENCE_TYPE:
2603         case REAL_TYPE:
2604         case FIXED_POINT_TYPE:
2605           return build2 (COMPLEX_EXPR, type,
2606                          fold_convert (TREE_TYPE (type), arg),
2607                          fold_convert (TREE_TYPE (type), integer_zero_node));
2608         case COMPLEX_TYPE:
2609           {
2610             tree rpart, ipart;
2611
2612             if (TREE_CODE (arg) == COMPLEX_EXPR)
2613               {
2614                 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2615                 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2616                 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2617               }
2618
2619             arg = save_expr (arg);
2620             rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2621             ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2622             rpart = fold_convert (TREE_TYPE (type), rpart);
2623             ipart = fold_convert (TREE_TYPE (type), ipart);
2624             return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2625           }
2626
2627         default:
2628           gcc_unreachable ();
2629         }
2630
2631     case VECTOR_TYPE:
2632       if (integer_zerop (arg))
2633         return build_zero_vector (type);
2634       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2635       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2636                   || TREE_CODE (orig) == VECTOR_TYPE);
2637       return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2638
2639     case VOID_TYPE:
2640       tem = fold_ignored_result (arg);
2641       if (TREE_CODE (tem) == GIMPLE_MODIFY_STMT)
2642         return tem;
2643       return fold_build1 (NOP_EXPR, type, tem);
2644
2645     default:
2646       gcc_unreachable ();
2647     }
2648 }
2649 \f
2650 /* Return false if expr can be assumed not to be an lvalue, true
2651    otherwise.  */
2652
2653 static bool
2654 maybe_lvalue_p (const_tree x)
2655 {
2656   /* We only need to wrap lvalue tree codes.  */
2657   switch (TREE_CODE (x))
2658   {
2659   case VAR_DECL:
2660   case PARM_DECL:
2661   case RESULT_DECL:
2662   case LABEL_DECL:
2663   case FUNCTION_DECL:
2664   case SSA_NAME:
2665
2666   case COMPONENT_REF:
2667   case INDIRECT_REF:
2668   case ALIGN_INDIRECT_REF:
2669   case MISALIGNED_INDIRECT_REF:
2670   case ARRAY_REF:
2671   case ARRAY_RANGE_REF:
2672   case BIT_FIELD_REF:
2673   case OBJ_TYPE_REF:
2674
2675   case REALPART_EXPR:
2676   case IMAGPART_EXPR:
2677   case PREINCREMENT_EXPR:
2678   case PREDECREMENT_EXPR:
2679   case SAVE_EXPR:
2680   case TRY_CATCH_EXPR:
2681   case WITH_CLEANUP_EXPR:
2682   case COMPOUND_EXPR:
2683   case MODIFY_EXPR:
2684   case GIMPLE_MODIFY_STMT:
2685   case TARGET_EXPR:
2686   case COND_EXPR:
2687   case BIND_EXPR:
2688   case MIN_EXPR:
2689   case MAX_EXPR:
2690     break;
2691
2692   default:
2693     /* Assume the worst for front-end tree codes.  */
2694     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2695       break;
2696     return false;
2697   }
2698
2699   return true;
2700 }
2701
2702 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2703
2704 tree
2705 non_lvalue (tree x)
2706 {
2707   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2708      us.  */
2709   if (in_gimple_form)
2710     return x;
2711
2712   if (! maybe_lvalue_p (x))
2713     return x;
2714   return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2715 }
2716
2717 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2718    Zero means allow extended lvalues.  */
2719
2720 int pedantic_lvalues;
2721
2722 /* When pedantic, return an expr equal to X but certainly not valid as a
2723    pedantic lvalue.  Otherwise, return X.  */
2724
2725 static tree
2726 pedantic_non_lvalue (tree x)
2727 {
2728   if (pedantic_lvalues)
2729     return non_lvalue (x);
2730   else
2731     return x;
2732 }
2733 \f
2734 /* Given a tree comparison code, return the code that is the logical inverse
2735    of the given code.  It is not safe to do this for floating-point
2736    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2737    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2738
2739 enum tree_code
2740 invert_tree_comparison (enum tree_code code, bool honor_nans)
2741 {
2742   if (honor_nans && flag_trapping_math)
2743     return ERROR_MARK;
2744
2745   switch (code)
2746     {
2747     case EQ_EXPR:
2748       return NE_EXPR;
2749     case NE_EXPR:
2750       return EQ_EXPR;
2751     case GT_EXPR:
2752       return honor_nans ? UNLE_EXPR : LE_EXPR;
2753     case GE_EXPR:
2754       return honor_nans ? UNLT_EXPR : LT_EXPR;
2755     case LT_EXPR:
2756       return honor_nans ? UNGE_EXPR : GE_EXPR;
2757     case LE_EXPR:
2758       return honor_nans ? UNGT_EXPR : GT_EXPR;
2759     case LTGT_EXPR:
2760       return UNEQ_EXPR;
2761     case UNEQ_EXPR:
2762       return LTGT_EXPR;
2763     case UNGT_EXPR:
2764       return LE_EXPR;
2765     case UNGE_EXPR:
2766       return LT_EXPR;
2767     case UNLT_EXPR:
2768       return GE_EXPR;
2769     case UNLE_EXPR:
2770       return GT_EXPR;
2771     case ORDERED_EXPR:
2772       return UNORDERED_EXPR;
2773     case UNORDERED_EXPR:
2774       return ORDERED_EXPR;
2775     default:
2776       gcc_unreachable ();
2777     }
2778 }
2779
2780 /* Similar, but return the comparison that results if the operands are
2781    swapped.  This is safe for floating-point.  */
2782
2783 enum tree_code
2784 swap_tree_comparison (enum tree_code code)
2785 {
2786   switch (code)
2787     {
2788     case EQ_EXPR:
2789     case NE_EXPR:
2790     case ORDERED_EXPR:
2791     case UNORDERED_EXPR:
2792     case LTGT_EXPR:
2793     case UNEQ_EXPR:
2794       return code;
2795     case GT_EXPR:
2796       return LT_EXPR;
2797     case GE_EXPR:
2798       return LE_EXPR;
2799     case LT_EXPR:
2800       return GT_EXPR;
2801     case LE_EXPR:
2802       return GE_EXPR;
2803     case UNGT_EXPR:
2804       return UNLT_EXPR;
2805     case UNGE_EXPR:
2806       return UNLE_EXPR;
2807     case UNLT_EXPR:
2808       return UNGT_EXPR;
2809     case UNLE_EXPR:
2810       return UNGE_EXPR;
2811     default:
2812       gcc_unreachable ();
2813     }
2814 }
2815
2816
2817 /* Convert a comparison tree code from an enum tree_code representation
2818    into a compcode bit-based encoding.  This function is the inverse of
2819    compcode_to_comparison.  */
2820
2821 static enum comparison_code
2822 comparison_to_compcode (enum tree_code code)
2823 {
2824   switch (code)
2825     {
2826     case LT_EXPR:
2827       return COMPCODE_LT;
2828     case EQ_EXPR:
2829       return COMPCODE_EQ;
2830     case LE_EXPR:
2831       return COMPCODE_LE;
2832     case GT_EXPR:
2833       return COMPCODE_GT;
2834     case NE_EXPR:
2835       return COMPCODE_NE;
2836     case GE_EXPR:
2837       return COMPCODE_GE;
2838     case ORDERED_EXPR:
2839       return COMPCODE_ORD;
2840     case UNORDERED_EXPR:
2841       return COMPCODE_UNORD;
2842     case UNLT_EXPR:
2843       return COMPCODE_UNLT;
2844     case UNEQ_EXPR:
2845       return COMPCODE_UNEQ;
2846     case UNLE_EXPR:
2847       return COMPCODE_UNLE;
2848     case UNGT_EXPR:
2849       return COMPCODE_UNGT;
2850     case LTGT_EXPR:
2851       return COMPCODE_LTGT;
2852     case UNGE_EXPR:
2853       return COMPCODE_UNGE;
2854     default:
2855       gcc_unreachable ();
2856     }
2857 }
2858
2859 /* Convert a compcode bit-based encoding of a comparison operator back
2860    to GCC's enum tree_code representation.  This function is the
2861    inverse of comparison_to_compcode.  */
2862
2863 static enum tree_code
2864 compcode_to_comparison (enum comparison_code code)
2865 {
2866   switch (code)
2867     {
2868     case COMPCODE_LT:
2869       return LT_EXPR;
2870     case COMPCODE_EQ:
2871       return EQ_EXPR;
2872     case COMPCODE_LE:
2873       return LE_EXPR;
2874     case COMPCODE_GT:
2875       return GT_EXPR;
2876     case COMPCODE_NE:
2877       return NE_EXPR;
2878     case COMPCODE_GE:
2879       return GE_EXPR;
2880     case COMPCODE_ORD:
2881       return ORDERED_EXPR;
2882     case COMPCODE_UNORD:
2883       return UNORDERED_EXPR;
2884     case COMPCODE_UNLT:
2885       return UNLT_EXPR;
2886     case COMPCODE_UNEQ:
2887       return UNEQ_EXPR;
2888     case COMPCODE_UNLE:
2889       return UNLE_EXPR;
2890     case COMPCODE_UNGT:
2891       return UNGT_EXPR;
2892     case COMPCODE_LTGT:
2893       return LTGT_EXPR;
2894     case COMPCODE_UNGE:
2895       return UNGE_EXPR;
2896     default:
2897       gcc_unreachable ();
2898     }
2899 }
2900
2901 /* Return a tree for the comparison which is the combination of
2902    doing the AND or OR (depending on CODE) of the two operations LCODE
2903    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2904    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2905    if this makes the transformation invalid.  */
2906
2907 tree
2908 combine_comparisons (enum tree_code code, enum tree_code lcode,
2909                      enum tree_code rcode, tree truth_type,
2910                      tree ll_arg, tree lr_arg)
2911 {
2912   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2913   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2914   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2915   enum comparison_code compcode;
2916
2917   switch (code)
2918     {
2919     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2920       compcode = lcompcode & rcompcode;
2921       break;
2922
2923     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2924       compcode = lcompcode | rcompcode;
2925       break;
2926
2927     default:
2928       return NULL_TREE;
2929     }
2930
2931   if (!honor_nans)
2932     {
2933       /* Eliminate unordered comparisons, as well as LTGT and ORD
2934          which are not used unless the mode has NaNs.  */
2935       compcode &= ~COMPCODE_UNORD;
2936       if (compcode == COMPCODE_LTGT)
2937         compcode = COMPCODE_NE;
2938       else if (compcode == COMPCODE_ORD)
2939         compcode = COMPCODE_TRUE;
2940     }
2941    else if (flag_trapping_math)
2942      {
2943         /* Check that the original operation and the optimized ones will trap
2944            under the same condition.  */
2945         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2946                      && (lcompcode != COMPCODE_EQ)
2947                      && (lcompcode != COMPCODE_ORD);
2948         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2949                      && (rcompcode != COMPCODE_EQ)
2950                      && (rcompcode != COMPCODE_ORD);
2951         bool trap = (compcode & COMPCODE_UNORD) == 0
2952                     && (compcode != COMPCODE_EQ)
2953                     && (compcode != COMPCODE_ORD);
2954
2955         /* In a short-circuited boolean expression the LHS might be
2956            such that the RHS, if evaluated, will never trap.  For
2957            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2958            if neither x nor y is NaN.  (This is a mixed blessing: for
2959            example, the expression above will never trap, hence
2960            optimizing it to x < y would be invalid).  */
2961         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2962             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2963           rtrap = false;
2964
2965         /* If the comparison was short-circuited, and only the RHS
2966            trapped, we may now generate a spurious trap.  */
2967         if (rtrap && !ltrap
2968             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2969           return NULL_TREE;
2970
2971         /* If we changed the conditions that cause a trap, we lose.  */
2972         if ((ltrap || rtrap) != trap)
2973           return NULL_TREE;
2974       }
2975
2976   if (compcode == COMPCODE_TRUE)
2977     return constant_boolean_node (true, truth_type);
2978   else if (compcode == COMPCODE_FALSE)
2979     return constant_boolean_node (false, truth_type);
2980   else
2981     return fold_build2 (compcode_to_comparison (compcode),
2982                         truth_type, ll_arg, lr_arg);
2983 }
2984
2985 /* Return nonzero if CODE is a tree code that represents a truth value.  */
2986
2987 static int
2988 truth_value_p (enum tree_code code)
2989 {
2990   return (TREE_CODE_CLASS (code) == tcc_comparison
2991           || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2992           || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2993           || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2994 }
2995 \f
2996 /* Return nonzero if two operands (typically of the same tree node)
2997    are necessarily equal.  If either argument has side-effects this
2998    function returns zero.  FLAGS modifies behavior as follows:
2999
3000    If OEP_ONLY_CONST is set, only return nonzero for constants.
3001    This function tests whether the operands are indistinguishable;
3002    it does not test whether they are equal using C's == operation.
3003    The distinction is important for IEEE floating point, because
3004    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3005    (2) two NaNs may be indistinguishable, but NaN!=NaN.
3006
3007    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3008    even though it may hold multiple values during a function.
3009    This is because a GCC tree node guarantees that nothing else is
3010    executed between the evaluation of its "operands" (which may often
3011    be evaluated in arbitrary order).  Hence if the operands themselves
3012    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3013    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
3014    unset means assuming isochronic (or instantaneous) tree equivalence.
3015    Unless comparing arbitrary expression trees, such as from different
3016    statements, this flag can usually be left unset.
3017
3018    If OEP_PURE_SAME is set, then pure functions with identical arguments
3019    are considered the same.  It is used when the caller has other ways
3020    to ensure that global memory is unchanged in between.  */
3021
3022 int
3023 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3024 {
3025   /* If either is ERROR_MARK, they aren't equal.  */
3026   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3027     return 0;
3028
3029   /* If both types don't have the same signedness, then we can't consider
3030      them equal.  We must check this before the STRIP_NOPS calls
3031      because they may change the signedness of the arguments.  */
3032   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3033     return 0;
3034
3035   /* If both types don't have the same precision, then it is not safe
3036      to strip NOPs.  */
3037   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3038     return 0;
3039
3040   STRIP_NOPS (arg0);
3041   STRIP_NOPS (arg1);
3042
3043   /* In case both args are comparisons but with different comparison
3044      code, try to swap the comparison operands of one arg to produce
3045      a match and compare that variant.  */
3046   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3047       && COMPARISON_CLASS_P (arg0)
3048       && COMPARISON_CLASS_P (arg1))
3049     {
3050       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3051
3052       if (TREE_CODE (arg0) == swap_code)
3053         return operand_equal_p (TREE_OPERAND (arg0, 0),
3054                                 TREE_OPERAND (arg1, 1), flags)
3055                && operand_equal_p (TREE_OPERAND (arg0, 1),
3056                                    TREE_OPERAND (arg1, 0), flags);
3057     }
3058
3059   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3060       /* This is needed for conversions and for COMPONENT_REF.
3061          Might as well play it safe and always test this.  */
3062       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3063       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3064       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3065     return 0;
3066
3067   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3068      We don't care about side effects in that case because the SAVE_EXPR
3069      takes care of that for us. In all other cases, two expressions are
3070      equal if they have no side effects.  If we have two identical
3071      expressions with side effects that should be treated the same due
3072      to the only side effects being identical SAVE_EXPR's, that will
3073      be detected in the recursive calls below.  */
3074   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3075       && (TREE_CODE (arg0) == SAVE_EXPR
3076           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3077     return 1;
3078
3079   /* Next handle constant cases, those for which we can return 1 even
3080      if ONLY_CONST is set.  */
3081   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3082     switch (TREE_CODE (arg0))
3083       {
3084       case INTEGER_CST:
3085         return tree_int_cst_equal (arg0, arg1);
3086
3087       case FIXED_CST:
3088         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3089                                        TREE_FIXED_CST (arg1));
3090
3091       case REAL_CST:
3092         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3093                                    TREE_REAL_CST (arg1)))
3094           return 1;
3095
3096         
3097         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3098           {
3099             /* If we do not distinguish between signed and unsigned zero,
3100                consider them equal.  */
3101             if (real_zerop (arg0) && real_zerop (arg1))
3102               return 1;
3103           }
3104         return 0;
3105
3106       case VECTOR_CST:
3107         {
3108           tree v1, v2;
3109
3110           v1 = TREE_VECTOR_CST_ELTS (arg0);
3111           v2 = TREE_VECTOR_CST_ELTS (arg1);
3112           while (v1 && v2)
3113             {
3114               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3115                                     flags))
3116                 return 0;
3117               v1 = TREE_CHAIN (v1);
3118               v2 = TREE_CHAIN (v2);
3119             }
3120
3121           return v1 == v2;
3122         }
3123
3124       case COMPLEX_CST:
3125         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3126                                  flags)
3127                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3128                                     flags));
3129
3130       case STRING_CST:
3131         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3132                 && ! memcmp (TREE_STRING_POINTER (arg0),
3133                               TREE_STRING_POINTER (arg1),
3134                               TREE_STRING_LENGTH (arg0)));
3135
3136       case ADDR_EXPR:
3137         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3138                                 0);
3139       default:
3140         break;
3141       }
3142
3143   if (flags & OEP_ONLY_CONST)
3144     return 0;
3145
3146 /* Define macros to test an operand from arg0 and arg1 for equality and a
3147    variant that allows null and views null as being different from any
3148    non-null value.  In the latter case, if either is null, the both
3149    must be; otherwise, do the normal comparison.  */
3150 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
3151                                     TREE_OPERAND (arg1, N), flags)
3152
3153 #define OP_SAME_WITH_NULL(N)                            \
3154   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3155    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3156
3157   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3158     {
3159     case tcc_unary:
3160       /* Two conversions are equal only if signedness and modes match.  */
3161       switch (TREE_CODE (arg0))
3162         {
3163         case NOP_EXPR:
3164         case CONVERT_EXPR:
3165         case FIX_TRUNC_EXPR:
3166           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3167               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3168             return 0;
3169           break;
3170         default:
3171           break;
3172         }
3173
3174       return OP_SAME (0);
3175
3176
3177     case tcc_comparison:
3178     case tcc_binary:
3179       if (OP_SAME (0) && OP_SAME (1))
3180         return 1;
3181
3182       /* For commutative ops, allow the other order.  */
3183       return (commutative_tree_code (TREE_CODE (arg0))
3184               && operand_equal_p (TREE_OPERAND (arg0, 0),
3185                                   TREE_OPERAND (arg1, 1), flags)
3186               && operand_equal_p (TREE_OPERAND (arg0, 1),
3187                                   TREE_OPERAND (arg1, 0), flags));
3188
3189     case tcc_reference:
3190       /* If either of the pointer (or reference) expressions we are
3191          dereferencing contain a side effect, these cannot be equal.  */
3192       if (TREE_SIDE_EFFECTS (arg0)
3193           || TREE_SIDE_EFFECTS (arg1))
3194         return 0;
3195
3196       switch (TREE_CODE (arg0))
3197         {
3198         case INDIRECT_REF:
3199         case ALIGN_INDIRECT_REF:
3200         case MISALIGNED_INDIRECT_REF:
3201         case REALPART_EXPR:
3202         case IMAGPART_EXPR:
3203           return OP_SAME (0);
3204
3205         case ARRAY_REF:
3206         case ARRAY_RANGE_REF:
3207           /* Operands 2 and 3 may be null.
3208              Compare the array index by value if it is constant first as we
3209              may have different types but same value here.  */
3210           return (OP_SAME (0)
3211                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3212                                           TREE_OPERAND (arg1, 1))
3213                       || OP_SAME (1))
3214                   && OP_SAME_WITH_NULL (2)
3215                   && OP_SAME_WITH_NULL (3));
3216
3217         case COMPONENT_REF:
3218           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
3219              may be NULL when we're called to compare MEM_EXPRs.  */
3220           return OP_SAME_WITH_NULL (0)
3221                  && OP_SAME (1)
3222                  && OP_SAME_WITH_NULL (2);
3223
3224         case BIT_FIELD_REF:
3225           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3226
3227         default:
3228           return 0;
3229         }
3230
3231     case tcc_expression:
3232       switch (TREE_CODE (arg0))
3233         {
3234         case ADDR_EXPR:
3235         case TRUTH_NOT_EXPR:
3236           return OP_SAME (0);
3237
3238         case TRUTH_ANDIF_EXPR:
3239         case TRUTH_ORIF_EXPR:
3240           return OP_SAME (0) && OP_SAME (1);
3241
3242         case TRUTH_AND_EXPR:
3243         case TRUTH_OR_EXPR:
3244         case TRUTH_XOR_EXPR:
3245           if (OP_SAME (0) && OP_SAME (1))
3246             return 1;
3247
3248           /* Otherwise take into account this is a commutative operation.  */
3249           return (operand_equal_p (TREE_OPERAND (arg0, 0),
3250                                    TREE_OPERAND (arg1, 1), flags)
3251                   && operand_equal_p (TREE_OPERAND (arg0, 1),
3252                                       TREE_OPERAND (arg1, 0), flags));
3253
3254         default:
3255           return 0;
3256         }
3257
3258     case tcc_vl_exp:
3259       switch (TREE_CODE (arg0))
3260         {
3261         case CALL_EXPR:
3262           /* If the CALL_EXPRs call different functions, then they
3263              clearly can not be equal.  */
3264           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3265                                  flags))
3266             return 0;
3267
3268           {
3269             unsigned int cef = call_expr_flags (arg0);
3270             if (flags & OEP_PURE_SAME)
3271               cef &= ECF_CONST | ECF_PURE;
3272             else
3273               cef &= ECF_CONST;
3274             if (!cef)
3275               return 0;
3276           }
3277
3278           /* Now see if all the arguments are the same.  */
3279           {
3280             const_call_expr_arg_iterator iter0, iter1;
3281             const_tree a0, a1;
3282             for (a0 = first_const_call_expr_arg (arg0, &iter0),
3283                    a1 = first_const_call_expr_arg (arg1, &iter1);
3284                  a0 && a1;
3285                  a0 = next_const_call_expr_arg (&iter0),
3286                    a1 = next_const_call_expr_arg (&iter1))
3287               if (! operand_equal_p (a0, a1, flags))
3288                 return 0;
3289
3290             /* If we get here and both argument lists are exhausted
3291                then the CALL_EXPRs are equal.  */
3292             return ! (a0 || a1);
3293           }
3294         default:
3295           return 0;
3296         }
3297
3298     case tcc_declaration:
3299       /* Consider __builtin_sqrt equal to sqrt.  */
3300       return (TREE_CODE (arg0) == FUNCTION_DECL
3301               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3302               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3303               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3304
3305     default:
3306       return 0;
3307     }
3308
3309 #undef OP_SAME
3310 #undef OP_SAME_WITH_NULL
3311 }
3312 \f
3313 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3314    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3315
3316    When in doubt, return 0.  */
3317
3318 static int
3319 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3320 {
3321   int unsignedp1, unsignedpo;
3322   tree primarg0, primarg1, primother;
3323   unsigned int correct_width;
3324
3325   if (operand_equal_p (arg0, arg1, 0))
3326     return 1;
3327
3328   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3329       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3330     return 0;
3331
3332   /* Discard any conversions that don't change the modes of ARG0 and ARG1
3333      and see if the inner values are the same.  This removes any
3334      signedness comparison, which doesn't matter here.  */
3335   primarg0 = arg0, primarg1 = arg1;
3336   STRIP_NOPS (primarg0);
3337   STRIP_NOPS (primarg1);
3338   if (operand_equal_p (primarg0, primarg1, 0))
3339     return 1;
3340
3341   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3342      actual comparison operand, ARG0.
3343
3344      First throw away any conversions to wider types
3345      already present in the operands.  */
3346
3347   primarg1 = get_narrower (arg1, &unsignedp1);
3348   primother = get_narrower (other, &unsignedpo);
3349
3350   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3351   if (unsignedp1 == unsignedpo
3352       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3353       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3354     {
3355       tree type = TREE_TYPE (arg0);
3356
3357       /* Make sure shorter operand is extended the right way
3358          to match the longer operand.  */
3359       primarg1 = fold_convert (signed_or_unsigned_type_for
3360                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3361
3362       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3363         return 1;
3364     }
3365
3366   return 0;
3367 }
3368 \f
3369 /* See if ARG is an expression that is either a comparison or is performing
3370    arithmetic on comparisons.  The comparisons must only be comparing
3371    two different values, which will be stored in *CVAL1 and *CVAL2; if
3372    they are nonzero it means that some operands have already been found.
3373    No variables may be used anywhere else in the expression except in the
3374    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
3375    the expression and save_expr needs to be called with CVAL1 and CVAL2.
3376
3377    If this is true, return 1.  Otherwise, return zero.  */
3378
3379 static int
3380 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3381 {
3382   enum tree_code code = TREE_CODE (arg);
3383   enum tree_code_class class = TREE_CODE_CLASS (code);
3384
3385   /* We can handle some of the tcc_expression cases here.  */
3386   if (class == tcc_expression && code == TRUTH_NOT_EXPR)
3387     class = tcc_unary;
3388   else if (class == tcc_expression
3389            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3390                || code == COMPOUND_EXPR))
3391     class = tcc_binary;
3392
3393   else if (class == tcc_expression && code == SAVE_EXPR
3394            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3395     {
3396       /* If we've already found a CVAL1 or CVAL2, this expression is
3397          two complex to handle.  */
3398       if (*cval1 || *cval2)
3399         return 0;
3400
3401       class = tcc_unary;
3402       *save_p = 1;
3403     }
3404
3405   switch (class)
3406     {
3407     case tcc_unary:
3408       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3409
3410     case tcc_binary:
3411       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3412               && twoval_comparison_p (TREE_OPERAND (arg, 1),
3413                                       cval1, cval2, save_p));
3414
3415     case tcc_constant:
3416       return 1;
3417
3418     case tcc_expression:
3419       if (code == COND_EXPR)
3420         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3421                                      cval1, cval2, save_p)
3422                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3423                                         cval1, cval2, save_p)
3424                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3425                                         cval1, cval2, save_p));
3426       return 0;
3427
3428     case tcc_comparison:
3429       /* First see if we can handle the first operand, then the second.  For
3430          the second operand, we know *CVAL1 can't be zero.  It must be that
3431          one side of the comparison is each of the values; test for the
3432          case where this isn't true by failing if the two operands
3433          are the same.  */
3434
3435       if (operand_equal_p (TREE_OPERAND (arg, 0),
3436                            TREE_OPERAND (arg, 1), 0))
3437         return 0;
3438
3439       if (*cval1 == 0)
3440         *cval1 = TREE_OPERAND (arg, 0);
3441       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3442         ;
3443       else if (*cval2 == 0)
3444         *cval2 = TREE_OPERAND (arg, 0);
3445       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3446         ;
3447       else
3448         return 0;
3449
3450       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3451         ;
3452       else if (*cval2 == 0)
3453         *cval2 = TREE_OPERAND (arg, 1);
3454       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3455         ;
3456       else
3457         return 0;
3458
3459       return 1;
3460
3461     default:
3462       return 0;
3463     }
3464 }
3465 \f
3466 /* ARG is a tree that is known to contain just arithmetic operations and
3467    comparisons.  Evaluate the operations in the tree substituting NEW0 for
3468    any occurrence of OLD0 as an operand of a comparison and likewise for
3469    NEW1 and OLD1.  */
3470
3471 static tree
3472 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3473 {
3474   tree type = TREE_TYPE (arg);
3475   enum tree_code code = TREE_CODE (arg);
3476   enum tree_code_class class = TREE_CODE_CLASS (code);
3477
3478   /* We can handle some of the tcc_expression cases here.  */
3479   if (class == tcc_expression && code == TRUTH_NOT_EXPR)
3480     class = tcc_unary;
3481   else if (class == tcc_expression
3482            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3483     class = tcc_binary;
3484
3485   switch (class)
3486     {
3487     case tcc_unary:
3488       return fold_build1 (code, type,
3489                           eval_subst (TREE_OPERAND (arg, 0),
3490                                       old0, new0, old1, new1));
3491
3492     case tcc_binary:
3493       return fold_build2 (code, type,
3494                           eval_subst (TREE_OPERAND (arg, 0),
3495                                       old0, new0, old1, new1),
3496                           eval_subst (TREE_OPERAND (arg, 1),
3497                                       old0, new0, old1, new1));
3498
3499     case tcc_expression:
3500       switch (code)
3501         {
3502         case SAVE_EXPR:
3503           return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3504
3505         case COMPOUND_EXPR:
3506           return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3507
3508         case COND_EXPR:
3509           return fold_build3 (code, type,
3510                               eval_subst (TREE_OPERAND (arg, 0),
3511                                           old0, new0, old1, new1),
3512                               eval_subst (TREE_OPERAND (arg, 1),
3513                                           old0, new0, old1, new1),
3514                               eval_subst (TREE_OPERAND (arg, 2),
3515                                           old0, new0, old1, new1));
3516         default:
3517           break;
3518         }
3519       /* Fall through - ???  */
3520
3521     case tcc_comparison:
3522       {
3523         tree arg0 = TREE_OPERAND (arg, 0);
3524         tree arg1 = TREE_OPERAND (arg, 1);
3525
3526         /* We need to check both for exact equality and tree equality.  The
3527            former will be true if the operand has a side-effect.  In that
3528            case, we know the operand occurred exactly once.  */
3529
3530         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3531           arg0 = new0;
3532         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3533           arg0 = new1;
3534
3535         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3536           arg1 = new0;
3537         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3538           arg1 = new1;
3539
3540         return fold_build2 (code, type, arg0, arg1);
3541       }
3542
3543     default:
3544       return arg;
3545     }
3546 }
3547 \f
3548 /* Return a tree for the case when the result of an expression is RESULT
3549    converted to TYPE and OMITTED was previously an operand of the expression
3550    but is now not needed (e.g., we folded OMITTED * 0).
3551
3552    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
3553    the conversion of RESULT to TYPE.  */
3554
3555 tree
3556 omit_one_operand (tree type, tree result, tree omitted)
3557 {
3558   tree t = fold_convert (type, result);
3559
3560   /* If the resulting operand is an empty statement, just return the omitted
3561      statement casted to void. */
3562   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3563     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3564
3565   if (TREE_SIDE_EFFECTS (omitted))
3566     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3567
3568   return non_lvalue (t);
3569 }
3570
3571 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3572
3573 static tree
3574 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3575 {
3576   tree t = fold_convert (type, result);
3577
3578   /* If the resulting operand is an empty statement, just return the omitted
3579      statement casted to void. */
3580   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3581     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3582
3583   if (TREE_SIDE_EFFECTS (omitted))
3584     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3585
3586   return pedantic_non_lvalue (t);
3587 }
3588
3589 /* Return a tree for the case when the result of an expression is RESULT
3590    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3591    of the expression but are now not needed.
3592
3593    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3594    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3595    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3596    just do the conversion of RESULT to TYPE.  */
3597
3598 tree
3599 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3600 {
3601   tree t = fold_convert (type, result);
3602
3603   if (TREE_SIDE_EFFECTS (omitted2))
3604     t = build2 (COMPOUND_EXPR, type, omitted2, t);
3605   if (TREE_SIDE_EFFECTS (omitted1))
3606     t = build2 (COMPOUND_EXPR, type, omitted1, t);
3607
3608   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3609 }
3610
3611 \f
3612 /* Return a simplified tree node for the truth-negation of ARG.  This
3613    never alters ARG itself.  We assume that ARG is an operation that
3614    returns a truth value (0 or 1).
3615
3616    FIXME: one would think we would fold the result, but it causes
3617    problems with the dominator optimizer.  */
3618
3619 tree
3620 fold_truth_not_expr (tree arg)
3621 {
3622   tree type = TREE_TYPE (arg);
3623   enum tree_code code = TREE_CODE (arg);
3624
3625   /* If this is a comparison, we can simply invert it, except for
3626      floating-point non-equality comparisons, in which case we just
3627      enclose a TRUTH_NOT_EXPR around what we have.  */
3628
3629   if (TREE_CODE_CLASS (code) == tcc_comparison)
3630     {
3631       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3632       if (FLOAT_TYPE_P (op_type)
3633           && flag_trapping_math
3634           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3635           && code != NE_EXPR && code != EQ_EXPR)
3636         return NULL_TREE;
3637       else
3638         {
3639           code = invert_tree_comparison (code,
3640                                          HONOR_NANS (TYPE_MODE (op_type)));
3641           if (code == ERROR_MARK)
3642             return NULL_TREE;
3643           else
3644             return build2 (code, type,
3645                            TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3646         }
3647     }
3648
3649   switch (code)
3650     {
3651     case INTEGER_CST:
3652       return constant_boolean_node (integer_zerop (arg), type);
3653
3654     case TRUTH_AND_EXPR:
3655       return build2 (TRUTH_OR_EXPR, type,
3656                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3657                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3658
3659     case TRUTH_OR_EXPR:
3660       return build2 (TRUTH_AND_EXPR, type,
3661                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3662                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3663
3664     case TRUTH_XOR_EXPR:
3665       /* Here we can invert either operand.  We invert the first operand
3666          unless the second operand is a TRUTH_NOT_EXPR in which case our
3667          result is the XOR of the first operand with the inside of the
3668          negation of the second operand.  */
3669
3670       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3671         return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3672                        TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3673       else
3674         return build2 (TRUTH_XOR_EXPR, type,
3675                        invert_truthvalue (TREE_OPERAND (arg, 0)),
3676                        TREE_OPERAND (arg, 1));
3677
3678     case TRUTH_ANDIF_EXPR:
3679       return build2 (TRUTH_ORIF_EXPR, type,
3680                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3681                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3682
3683     case TRUTH_ORIF_EXPR:
3684       return build2 (TRUTH_ANDIF_EXPR, type,
3685                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3686                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3687
3688     case TRUTH_NOT_EXPR:
3689       return TREE_OPERAND (arg, 0);
3690
3691     case COND_EXPR:
3692       {
3693         tree arg1 = TREE_OPERAND (arg, 1);
3694         tree arg2 = TREE_OPERAND (arg, 2);
3695         /* A COND_EXPR may have a throw as one operand, which
3696            then has void type.  Just leave void operands
3697            as they are.  */
3698         return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3699                        VOID_TYPE_P (TREE_TYPE (arg1))
3700                        ? arg1 : invert_truthvalue (arg1),
3701                        VOID_TYPE_P (TREE_TYPE (arg2))
3702                        ? arg2 : invert_truthvalue (arg2));
3703       }
3704
3705     case COMPOUND_EXPR:
3706       return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3707                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3708
3709     case NON_LVALUE_EXPR:
3710       return invert_truthvalue (TREE_OPERAND (arg, 0));
3711
3712     case NOP_EXPR:
3713       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3714         return build1 (TRUTH_NOT_EXPR, type, arg);
3715
3716     case CONVERT_EXPR:
3717     case FLOAT_EXPR:
3718       return build1 (TREE_CODE (arg), type,
3719                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3720
3721     case BIT_AND_EXPR:
3722       if (!integer_onep (TREE_OPERAND (arg, 1)))
3723         break;
3724       return build2 (EQ_EXPR, type, arg,
3725                      build_int_cst (type, 0));
3726
3727     case SAVE_EXPR:
3728       return build1 (TRUTH_NOT_EXPR, type, arg);
3729
3730     case CLEANUP_POINT_EXPR:
3731       return build1 (CLEANUP_POINT_EXPR, type,
3732                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3733
3734     default:
3735       break;
3736     }
3737
3738   return NULL_TREE;
3739 }
3740
3741 /* Return a simplified tree node for the truth-negation of ARG.  This
3742    never alters ARG itself.  We assume that ARG is an operation that
3743    returns a truth value (0 or 1).
3744
3745    FIXME: one would think we would fold the result, but it causes
3746    problems with the dominator optimizer.  */
3747
3748 tree
3749 invert_truthvalue (tree arg)
3750 {
3751   tree tem;
3752
3753   if (TREE_CODE (arg) == ERROR_MARK)
3754     return arg;
3755
3756   tem = fold_truth_not_expr (arg);
3757   if (!tem)
3758     tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3759
3760   return tem;
3761 }
3762
3763 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3764    operands are another bit-wise operation with a common input.  If so,
3765    distribute the bit operations to save an operation and possibly two if
3766    constants are involved.  For example, convert
3767         (A | B) & (A | C) into A | (B & C)
3768    Further simplification will occur if B and C are constants.
3769
3770    If this optimization cannot be done, 0 will be returned.  */
3771
3772 static tree
3773 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3774 {
3775   tree common;
3776   tree left, right;
3777
3778   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3779       || TREE_CODE (arg0) == code
3780       || (TREE_CODE (arg0) != BIT_AND_EXPR
3781           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3782     return 0;
3783
3784   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3785     {
3786       common = TREE_OPERAND (arg0, 0);
3787       left = TREE_OPERAND (arg0, 1);
3788       right = TREE_OPERAND (arg1, 1);
3789     }
3790   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3791     {
3792       common = TREE_OPERAND (arg0, 0);
3793       left = TREE_OPERAND (arg0, 1);
3794       right = TREE_OPERAND (arg1, 0);
3795     }
3796   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3797     {
3798       common = TREE_OPERAND (arg0, 1);
3799       left = TREE_OPERAND (arg0, 0);
3800       right = TREE_OPERAND (arg1, 1);
3801     }
3802   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3803     {
3804       common = TREE_OPERAND (arg0, 1);
3805       left = TREE_OPERAND (arg0, 0);
3806       right = TREE_OPERAND (arg1, 0);
3807     }
3808   else
3809     return 0;
3810
3811   return fold_build2 (TREE_CODE (arg0), type, common,
3812                       fold_build2 (code, type, left, right));
3813 }
3814
3815 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3816    with code CODE.  This optimization is unsafe.  */
3817 static tree
3818 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3819 {
3820   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3821   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3822
3823   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3824   if (mul0 == mul1
3825       && operand_equal_p (TREE_OPERAND (arg0, 1),
3826                        TREE_OPERAND (arg1, 1), 0))
3827     return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3828                         fold_build2 (code, type,
3829                                      TREE_OPERAND (arg0, 0),
3830                                      TREE_OPERAND (arg1, 0)),
3831                         TREE_OPERAND (arg0, 1));
3832
3833   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3834   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3835                        TREE_OPERAND (arg1, 0), 0)
3836       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3837       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3838     {
3839       REAL_VALUE_TYPE r0, r1;
3840       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3841       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3842       if (!mul0)
3843         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3844       if (!mul1)
3845         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3846       real_arithmetic (&r0, code, &r0, &r1);
3847       return fold_build2 (MULT_EXPR, type,
3848                           TREE_OPERAND (arg0, 0),
3849                           build_real (type, r0));
3850     }
3851
3852   return NULL_TREE;
3853 }
3854 \f
3855 /* Subroutine for fold_truthop: decode a field reference.
3856
3857    If EXP is a comparison reference, we return the innermost reference.
3858
3859    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3860    set to the starting bit number.
3861
3862    If the innermost field can be completely contained in a mode-sized
3863    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
3864
3865    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3866    otherwise it is not changed.
3867
3868    *PUNSIGNEDP is set to the signedness of the field.
3869
3870    *PMASK is set to the mask used.  This is either contained in a
3871    BIT_AND_EXPR or derived from the width of the field.
3872
3873    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3874
3875    Return 0 if this is not a component reference or is one that we can't
3876    do anything with.  */
3877
3878 static tree
3879 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3880                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3881                         int *punsignedp, int *pvolatilep,
3882                         tree *pmask, tree *pand_mask)
3883 {
3884   tree outer_type = 0;
3885   tree and_mask = 0;
3886   tree mask, inner, offset;
3887   tree unsigned_type;
3888   unsigned int precision;
3889
3890   /* All the optimizations using this function assume integer fields.
3891      There are problems with FP fields since the type_for_size call
3892      below can fail for, e.g., XFmode.  */
3893   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3894     return 0;
3895
3896   /* We are interested in the bare arrangement of bits, so strip everything
3897      that doesn't affect the machine mode.  However, record the type of the
3898      outermost expression if it may matter below.  */
3899   if (TREE_CODE (exp) == NOP_EXPR
3900       || TREE_CODE (exp) == CONVERT_EXPR
3901       || TREE_CODE (exp) == NON_LVALUE_EXPR)
3902     outer_type = TREE_TYPE (exp);
3903   STRIP_NOPS (exp);
3904
3905   if (TREE_CODE (exp) == BIT_AND_EXPR)
3906     {
3907       and_mask = TREE_OPERAND (exp, 1);
3908       exp = TREE_OPERAND (exp, 0);
3909       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3910       if (TREE_CODE (and_mask) != INTEGER_CST)
3911         return 0;
3912     }
3913
3914   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3915                                punsignedp, pvolatilep, false);
3916   if ((inner == exp && and_mask == 0)
3917       || *pbitsize < 0 || offset != 0
3918       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3919     return 0;
3920
3921   /* If the number of bits in the reference is the same as the bitsize of
3922      the outer type, then the outer type gives the signedness. Otherwise
3923      (in case of a small bitfield) the signedness is unchanged.  */
3924   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3925     *punsignedp = TYPE_UNSIGNED (outer_type);
3926
3927   /* Compute the mask to access the bitfield.  */
3928   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3929   precision = TYPE_PRECISION (unsigned_type);
3930
3931   mask = build_int_cst_type (unsigned_type, -1);
3932
3933   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3934   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3935
3936   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
3937   if (and_mask != 0)
3938     mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
3939                         fold_convert (unsigned_type, and_mask), mask);
3940
3941   *pmask = mask;
3942   *pand_mask = and_mask;
3943   return inner;
3944 }
3945
3946 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3947    represents the sign bit of EXP's type.  If EXP represents a sign
3948    or zero extension, also test VAL against the unextended type.
3949    The return value is the (sub)expression whose sign bit is VAL,
3950    or NULL_TREE otherwise.  */
3951
3952 static tree
3953 sign_bit_p (tree exp, const_tree val)
3954 {
3955   unsigned HOST_WIDE_INT mask_lo, lo;
3956   HOST_WIDE_INT mask_hi, hi;
3957   int width;
3958   tree t;
3959
3960   /* Tree EXP must have an integral type.  */
3961   t = TREE_TYPE (exp);
3962   if (! INTEGRAL_TYPE_P (t))
3963     return NULL_TREE;
3964
3965   /* Tree VAL must be an integer constant.  */
3966   if (TREE_CODE (val) != INTEGER_CST
3967       || TREE_OVERFLOW (val))
3968     return NULL_TREE;
3969
3970   width = TYPE_PRECISION (t);
3971   if (width > HOST_BITS_PER_WIDE_INT)
3972     {
3973       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3974       lo = 0;
3975
3976       mask_hi = ((unsigned HOST_WIDE_INT) -1
3977                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
3978       mask_lo = -1;
3979     }
3980   else
3981     {
3982       hi = 0;
3983       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3984
3985       mask_hi = 0;
3986       mask_lo = ((unsigned HOST_WIDE_INT) -1
3987                  >> (HOST_BITS_PER_WIDE_INT - width));
3988     }
3989
3990   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3991      treat VAL as if it were unsigned.  */
3992   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3993       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3994     return exp;
3995
3996   /* Handle extension from a narrower type.  */
3997   if (TREE_CODE (exp) == NOP_EXPR
3998       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3999     return sign_bit_p (TREE_OPERAND (exp, 0), val);
4000
4001   return NULL_TREE;
4002 }
4003
4004 /* Subroutine for fold_truthop: determine if an operand is simple enough
4005    to be evaluated unconditionally.  */
4006
4007 static int
4008 simple_operand_p (const_tree exp)
4009 {
4010   /* Strip any conversions that don't change the machine mode.  */
4011   STRIP_NOPS (exp);
4012
4013   return (CONSTANT_CLASS_P (exp)
4014           || TREE_CODE (exp) == SSA_NAME
4015           || (DECL_P (exp)
4016               && ! TREE_ADDRESSABLE (exp)
4017               && ! TREE_THIS_VOLATILE (exp)
4018               && ! DECL_NONLOCAL (exp)
4019               /* Don't regard global variables as simple.  They may be
4020                  allocated in ways unknown to the compiler (shared memory,
4021                  #pragma weak, etc).  */
4022               && ! TREE_PUBLIC (exp)
4023               && ! DECL_EXTERNAL (exp)
4024               /* Loading a static variable is unduly expensive, but global
4025                  registers aren't expensive.  */
4026               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4027 }
4028 \f
4029 /* The following functions are subroutines to fold_range_test and allow it to
4030    try to change a logical combination of comparisons into a range test.
4031
4032    For example, both
4033         X == 2 || X == 3 || X == 4 || X == 5
4034    and
4035         X >= 2 && X <= 5
4036    are converted to
4037         (unsigned) (X - 2) <= 3
4038
4039    We describe each set of comparisons as being either inside or outside
4040    a range, using a variable named like IN_P, and then describe the
4041    range with a lower and upper bound.  If one of the bounds is omitted,
4042    it represents either the highest or lowest value of the type.
4043
4044    In the comments below, we represent a range by two numbers in brackets
4045    preceded by a "+" to designate being inside that range, or a "-" to
4046    designate being outside that range, so the condition can be inverted by
4047    flipping the prefix.  An omitted bound is represented by a "-".  For
4048    example, "- [-, 10]" means being outside the range starting at the lowest
4049    possible value and ending at 10, in other words, being greater than 10.
4050    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4051    always false.
4052
4053    We set up things so that the missing bounds are handled in a consistent
4054    manner so neither a missing bound nor "true" and "false" need to be
4055    handled using a special case.  */
4056
4057 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4058    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4059    and UPPER1_P are nonzero if the respective argument is an upper bound
4060    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
4061    must be specified for a comparison.  ARG1 will be converted to ARG0's
4062    type if both are specified.  */
4063
4064 static tree
4065 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4066              tree arg1, int upper1_p)
4067 {
4068   tree tem;
4069   int result;
4070   int sgn0, sgn1;
4071
4072   /* If neither arg represents infinity, do the normal operation.
4073      Else, if not a comparison, return infinity.  Else handle the special
4074      comparison rules. Note that most of the cases below won't occur, but
4075      are handled for consistency.  */
4076
4077   if (arg0 != 0 && arg1 != 0)
4078     {
4079       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4080                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
4081       STRIP_NOPS (tem);
4082       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4083     }
4084
4085   if (TREE_CODE_CLASS (code) != tcc_comparison)
4086     return 0;
4087
4088   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4089      for neither.  In real maths, we cannot assume open ended ranges are
4090      the same. But, this is computer arithmetic, where numbers are finite.
4091      We can therefore make the transformation of any unbounded range with
4092      the value Z, Z being greater than any representable number. This permits
4093      us to treat unbounded ranges as equal.  */
4094   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4095   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4096   switch (code)
4097     {
4098     case EQ_EXPR:
4099       result = sgn0 == sgn1;
4100       break;
4101     case NE_EXPR:
4102       result = sgn0 != sgn1;
4103       break;
4104     case LT_EXPR:
4105       result = sgn0 < sgn1;
4106       break;
4107     case LE_EXPR:
4108       result = sgn0 <= sgn1;
4109       break;
4110     case GT_EXPR:
4111       result = sgn0 > sgn1;
4112       break;
4113     case GE_EXPR:
4114       result = sgn0 >= sgn1;
4115       break;
4116     default:
4117       gcc_unreachable ();
4118     }
4119
4120   return constant_boolean_node (result, type);
4121 }
4122 \f
4123 /* Given EXP, a logical expression, set the range it is testing into
4124    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4125    actually being tested.  *PLOW and *PHIGH will be made of the same
4126    type as the returned expression.  If EXP is not a comparison, we
4127    will most likely not be returning a useful value and range.  Set
4128    *STRICT_OVERFLOW_P to true if the return value is only valid
4129    because signed overflow is undefined; otherwise, do not change
4130    *STRICT_OVERFLOW_P.  */
4131
4132 static tree
4133 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4134             bool *strict_overflow_p)
4135 {
4136   enum tree_code code;
4137   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4138   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4139   int in_p, n_in_p;
4140   tree low, high, n_low, n_high;
4141
4142   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4143      and see if we can refine the range.  Some of the cases below may not
4144      happen, but it doesn't seem worth worrying about this.  We "continue"
4145      the outer loop when we've changed something; otherwise we "break"
4146      the switch, which will "break" the while.  */
4147
4148   in_p = 0;
4149   low = high = build_int_cst (TREE_TYPE (exp), 0);
4150
4151   while (1)
4152     {
4153       code = TREE_CODE (exp);
4154       exp_type = TREE_TYPE (exp);
4155
4156       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4157         {
4158           if (TREE_OPERAND_LENGTH (exp) > 0)
4159             arg0 = TREE_OPERAND (exp, 0);
4160           if (TREE_CODE_CLASS (code) == tcc_comparison
4161               || TREE_CODE_CLASS (code) == tcc_unary
4162               || TREE_CODE_CLASS (code) == tcc_binary)
4163             arg0_type = TREE_TYPE (arg0);
4164           if (TREE_CODE_CLASS (code) == tcc_binary
4165               || TREE_CODE_CLASS (code) == tcc_comparison
4166               || (TREE_CODE_CLASS (code) == tcc_expression
4167                   && TREE_OPERAND_LENGTH (exp) > 1))
4168             arg1 = TREE_OPERAND (exp, 1);
4169         }
4170
4171       switch (code)
4172         {
4173         case TRUTH_NOT_EXPR:
4174           in_p = ! in_p, exp = arg0;
4175           continue;
4176
4177         case EQ_EXPR: case NE_EXPR:
4178         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4179           /* We can only do something if the range is testing for zero
4180              and if the second operand is an integer constant.  Note that
4181              saying something is "in" the range we make is done by
4182              complementing IN_P since it will set in the initial case of
4183              being not equal to zero; "out" is leaving it alone.  */
4184           if (low == 0 || high == 0
4185               || ! integer_zerop (low) || ! integer_zerop (high)
4186               || TREE_CODE (arg1) != INTEGER_CST)
4187             break;
4188
4189           switch (code)
4190             {
4191             case NE_EXPR:  /* - [c, c]  */
4192               low = high = arg1;
4193               break;
4194             case EQ_EXPR:  /* + [c, c]  */
4195               in_p = ! in_p, low = high = arg1;
4196               break;
4197             case GT_EXPR:  /* - [-, c] */
4198               low = 0, high = arg1;
4199               break;
4200             case GE_EXPR:  /* + [c, -] */
4201               in_p = ! in_p, low = arg1, high = 0;
4202               break;
4203             case LT_EXPR:  /* - [c, -] */
4204               low = arg1, high = 0;
4205               break;
4206             case LE_EXPR:  /* + [-, c] */
4207               in_p = ! in_p, low = 0, high = arg1;
4208               break;
4209             default:
4210               gcc_unreachable ();
4211             }
4212
4213           /* If this is an unsigned comparison, we also know that EXP is
4214              greater than or equal to zero.  We base the range tests we make
4215              on that fact, so we record it here so we can parse existing
4216              range tests.  We test arg0_type since often the return type
4217              of, e.g. EQ_EXPR, is boolean.  */
4218           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4219             {
4220               if (! merge_ranges (&n_in_p, &n_low, &n_high,
4221                                   in_p, low, high, 1,
4222                                   build_int_cst (arg0_type, 0),
4223                                   NULL_TREE))
4224                 break;
4225
4226               in_p = n_in_p, low = n_low, high = n_high;
4227
4228               /* If the high bound is missing, but we have a nonzero low
4229                  bound, reverse the range so it goes from zero to the low bound
4230                  minus 1.  */
4231               if (high == 0 && low && ! integer_zerop (low))
4232                 {
4233                   in_p = ! in_p;
4234                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4235                                       integer_one_node, 0);
4236                   low = build_int_cst (arg0_type, 0);
4237                 }
4238             }
4239
4240           exp = arg0;
4241           continue;
4242
4243         case NEGATE_EXPR:
4244           /* (-x) IN [a,b] -> x in [-b, -a]  */
4245           n_low = range_binop (MINUS_EXPR, exp_type,
4246                                build_int_cst (exp_type, 0),
4247                                0, high, 1);
4248           n_high = range_binop (MINUS_EXPR, exp_type,
4249                                 build_int_cst (exp_type, 0),
4250                                 0, low, 0);
4251           low = n_low, high = n_high;
4252           exp = arg0;
4253           continue;
4254
4255         case BIT_NOT_EXPR:
4256           /* ~ X -> -X - 1  */
4257           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4258                         build_int_cst (exp_type, 1));
4259           continue;
4260
4261         case PLUS_EXPR:  case MINUS_EXPR:
4262           if (TREE_CODE (arg1) != INTEGER_CST)
4263             break;
4264
4265           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4266              move a constant to the other side.  */
4267           if (!TYPE_UNSIGNED (arg0_type)
4268               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4269             break;
4270
4271           /* If EXP is signed, any overflow in the computation is undefined,
4272              so we don't worry about it so long as our computations on
4273              the bounds don't overflow.  For unsigned, overflow is defined
4274              and this is exactly the right thing.  */
4275           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4276                                arg0_type, low, 0, arg1, 0);
4277           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4278                                 arg0_type, high, 1, arg1, 0);
4279           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4280               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4281             break;
4282
4283           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4284             *strict_overflow_p = true;
4285
4286           /* Check for an unsigned range which has wrapped around the maximum
4287              value thus making n_high < n_low, and normalize it.  */
4288           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4289             {
4290               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4291                                  integer_one_node, 0);
4292               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4293                                   integer_one_node, 0);
4294
4295               /* If the range is of the form +/- [ x+1, x ], we won't
4296                  be able to normalize it.  But then, it represents the
4297                  whole range or the empty set, so make it
4298                  +/- [ -, - ].  */
4299               if (tree_int_cst_equal (n_low, low)
4300                   && tree_int_cst_equal (n_high, high))
4301                 low = high = 0;
4302               else
4303                 in_p = ! in_p;
4304             }
4305           else
4306             low = n_low, high = n_high;
4307
4308           exp = arg0;
4309           continue;
4310
4311         case NOP_EXPR:  case NON_LVALUE_EXPR:  case CONVERT_EXPR:
4312           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4313             break;
4314
4315           if (! INTEGRAL_TYPE_P (arg0_type)
4316               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4317               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4318             break;
4319
4320           n_low = low, n_high = high;
4321
4322           if (n_low != 0)
4323             n_low = fold_convert (arg0_type, n_low);
4324
4325           if (n_high != 0)
4326             n_high = fold_convert (arg0_type, n_high);
4327
4328
4329           /* If we're converting arg0 from an unsigned type, to exp,
4330              a signed type,  we will be doing the comparison as unsigned.
4331              The tests above have already verified that LOW and HIGH
4332              are both positive.
4333
4334              So we have to ensure that we will handle large unsigned
4335              values the same way that the current signed bounds treat
4336              negative values.  */
4337
4338           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4339             {
4340               tree high_positive;
4341               tree equiv_type;
4342               /* For fixed-point modes, we need to pass the saturating flag
4343                  as the 2nd parameter.  */
4344               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4345                 equiv_type = lang_hooks.types.type_for_mode
4346                              (TYPE_MODE (arg0_type),
4347                               TYPE_SATURATING (arg0_type));
4348               else
4349                 equiv_type = lang_hooks.types.type_for_mode
4350                              (TYPE_MODE (arg0_type), 1);
4351
4352               /* A range without an upper bound is, naturally, unbounded.
4353                  Since convert would have cropped a very large value, use
4354                  the max value for the destination type.  */
4355               high_positive
4356                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4357                 : TYPE_MAX_VALUE (arg0_type);
4358
4359               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4360                 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4361                                              fold_convert (arg0_type,
4362                                                            high_positive),
4363                                              build_int_cst (arg0_type, 1));
4364
4365               /* If the low bound is specified, "and" the range with the
4366                  range for which the original unsigned value will be
4367                  positive.  */
4368               if (low != 0)
4369                 {
4370                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4371                                       1, n_low, n_high, 1,
4372                                       fold_convert (arg0_type,
4373                                                     integer_zero_node),
4374                                       high_positive))
4375                     break;
4376
4377                   in_p = (n_in_p == in_p);
4378                 }
4379               else
4380                 {
4381                   /* Otherwise, "or" the range with the range of the input
4382                      that will be interpreted as negative.  */
4383                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4384                                       0, n_low, n_high, 1,
4385                                       fold_convert (arg0_type,
4386                                                     integer_zero_node),
4387                                       high_positive))
4388                     break;
4389
4390                   in_p = (in_p != n_in_p);
4391                 }
4392             }
4393
4394           exp = arg0;
4395           low = n_low, high = n_high;
4396           continue;
4397
4398         default:
4399           break;
4400         }
4401
4402       break;
4403     }
4404
4405   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4406   if (TREE_CODE (exp) == INTEGER_CST)
4407     {
4408       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4409                                                  exp, 0, low, 0))
4410                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4411                                                     exp, 1, high, 1)));
4412       low = high = 0;
4413       exp = 0;
4414     }
4415
4416   *pin_p = in_p, *plow = low, *phigh = high;
4417   return exp;
4418 }
4419 \f
4420 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4421    type, TYPE, return an expression to test if EXP is in (or out of, depending
4422    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4423
4424 static tree
4425 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4426 {
4427   tree etype = TREE_TYPE (exp);
4428   tree value;
4429
4430 #ifdef HAVE_canonicalize_funcptr_for_compare
4431   /* Disable this optimization for function pointer expressions
4432      on targets that require function pointer canonicalization.  */
4433   if (HAVE_canonicalize_funcptr_for_compare
4434       && TREE_CODE (etype) == POINTER_TYPE
4435       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4436     return NULL_TREE;
4437 #endif
4438
4439   if (! in_p)
4440     {
4441       value = build_range_check (type, exp, 1, low, high);
4442       if (value != 0)
4443         return invert_truthvalue (value);
4444
4445       return 0;
4446     }
4447
4448   if (low == 0 && high == 0)
4449     return build_int_cst (type, 1);
4450
4451   if (low == 0)
4452     return fold_build2 (LE_EXPR, type, exp,
4453                         fold_convert (etype, high));
4454
4455   if (high == 0)
4456     return fold_build2 (GE_EXPR, type, exp,
4457                         fold_convert (etype, low));
4458
4459   if (operand_equal_p (low, high, 0))
4460     return fold_build2 (EQ_EXPR, type, exp,
4461                         fold_convert (etype, low));
4462
4463   if (integer_zerop (low))
4464     {
4465       if (! TYPE_UNSIGNED (etype))
4466         {
4467           etype = unsigned_type_for (etype);
4468           high = fold_convert (etype, high);
4469           exp = fold_convert (etype, exp);
4470         }
4471       return build_range_check (type, exp, 1, 0, high);
4472     }
4473
4474   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4475   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4476     {
4477       unsigned HOST_WIDE_INT lo;
4478       HOST_WIDE_INT hi;
4479       int prec;
4480
4481       prec = TYPE_PRECISION (etype);
4482       if (prec <= HOST_BITS_PER_WIDE_INT)
4483         {
4484           hi = 0;
4485           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4486         }
4487       else
4488         {
4489           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4490           lo = (unsigned HOST_WIDE_INT) -1;
4491         }
4492
4493       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4494         {
4495           if (TYPE_UNSIGNED (etype))
4496             {
4497               etype = signed_type_for (etype);
4498               exp = fold_convert (etype, exp);
4499             }
4500           return fold_build2 (GT_EXPR, type, exp,
4501                               build_int_cst (etype, 0));
4502         }
4503     }
4504
4505   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4506      This requires wrap-around arithmetics for the type of the expression.  */
4507   switch (TREE_CODE (etype))
4508     {
4509     case INTEGER_TYPE:
4510       /* There is no requirement that LOW be within the range of ETYPE
4511          if the latter is a subtype.  It must, however, be within the base
4512          type of ETYPE.  So be sure we do the subtraction in that type.  */
4513       if (TREE_TYPE (etype))
4514         etype = TREE_TYPE (etype);
4515       break;
4516
4517     case ENUMERAL_TYPE:
4518     case BOOLEAN_TYPE:
4519       etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4520                                               TYPE_UNSIGNED (etype));
4521       break;
4522
4523     default:
4524       break;
4525     }
4526
4527   /* If we don't have wrap-around arithmetics upfront, try to force it.  */
4528   if (TREE_CODE (etype) == INTEGER_TYPE
4529       && !TYPE_OVERFLOW_WRAPS (etype))
4530     {
4531       tree utype, minv, maxv;
4532
4533       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4534          for the type in question, as we rely on this here.  */
4535       utype = unsigned_type_for (etype);
4536       maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4537       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4538                           integer_one_node, 1);
4539       minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4540
4541       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4542                                       minv, 1, maxv, 1)))
4543         etype = utype;
4544       else
4545         return 0;
4546     }
4547
4548   high = fold_convert (etype, high);
4549   low = fold_convert (etype, low);
4550   exp = fold_convert (etype, exp);
4551
4552   value = const_binop (MINUS_EXPR, high, low, 0);
4553
4554
4555   if (POINTER_TYPE_P (etype))
4556     {
4557       if (value != 0 && !TREE_OVERFLOW (value))
4558         {
4559           low = fold_convert (sizetype, low);
4560           low = fold_build1 (NEGATE_EXPR, sizetype, low);
4561           return build_range_check (type,
4562                                     fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4563                                     1, build_int_cst (etype, 0), value);
4564         }
4565       return 0;
4566     }
4567
4568   if (value != 0 && !TREE_OVERFLOW (value))
4569     return build_range_check (type,
4570                               fold_build2 (MINUS_EXPR, etype, exp, low),
4571                               1, build_int_cst (etype, 0), value);
4572
4573   return 0;
4574 }
4575 \f
4576 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4577
4578 static tree
4579 range_predecessor (tree val)
4580 {
4581   tree type = TREE_TYPE (val);
4582
4583   if (INTEGRAL_TYPE_P (type)
4584       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4585     return 0;
4586   else
4587     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4588 }
4589
4590 /* Return the successor of VAL in its type, handling the infinite case.  */
4591
4592 static tree
4593 range_successor (tree val)
4594 {
4595   tree type = TREE_TYPE (val);
4596
4597   if (INTEGRAL_TYPE_P (type)
4598       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4599     return 0;
4600   else
4601     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4602 }
4603
4604 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4605    can, 0 if we can't.  Set the output range into the specified parameters.  */
4606
4607 static int
4608 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4609               tree high0, int in1_p, tree low1, tree high1)
4610 {
4611   int no_overlap;
4612   int subset;
4613   int temp;
4614   tree tem;
4615   int in_p;
4616   tree low, high;
4617   int lowequal = ((low0 == 0 && low1 == 0)
4618                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4619                                                 low0, 0, low1, 0)));
4620   int highequal = ((high0 == 0 && high1 == 0)
4621                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4622                                                  high0, 1, high1, 1)));
4623
4624   /* Make range 0 be the range that starts first, or ends last if they
4625      start at the same value.  Swap them if it isn't.  */
4626   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4627                                  low0, 0, low1, 0))
4628       || (lowequal
4629           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4630                                         high1, 1, high0, 1))))
4631     {
4632       temp = in0_p, in0_p = in1_p, in1_p = temp;
4633       tem = low0, low0 = low1, low1 = tem;
4634       tem = high0, high0 = high1, high1 = tem;
4635     }
4636
4637   /* Now flag two cases, whether the ranges are disjoint or whether the
4638      second range is totally subsumed in the first.  Note that the tests
4639      below are simplified by the ones above.  */
4640   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4641                                           high0, 1, low1, 0));
4642   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4643                                       high1, 1, high0, 1));
4644
4645   /* We now have four cases, depending on whether we are including or
4646      excluding the two ranges.  */
4647   if (in0_p && in1_p)
4648     {
4649       /* If they don't overlap, the result is false.  If the second range
4650          is a subset it is the result.  Otherwise, the range is from the start
4651          of the second to the end of the first.  */
4652       if (no_overlap)
4653         in_p = 0, low = high = 0;
4654       else if (subset)
4655         in_p = 1, low = low1, high = high1;
4656       else
4657         in_p = 1, low = low1, high = high0;
4658     }
4659
4660   else if (in0_p && ! in1_p)
4661     {
4662       /* If they don't overlap, the result is the first range.  If they are
4663          equal, the result is false.  If the second range is a subset of the
4664          first, and the ranges begin at the same place, we go from just after
4665          the end of the second range to the end of the first.  If the second
4666          range is not a subset of the first, or if it is a subset and both
4667          ranges end at the same place, the range starts at the start of the
4668          first range and ends just before the second range.
4669          Otherwise, we can't describe this as a single range.  */
4670       if (no_overlap)
4671         in_p = 1, low = low0, high = high0;
4672       else if (lowequal && highequal)
4673         in_p = 0, low = high = 0;
4674       else if (subset && lowequal)
4675         {
4676           low = range_successor (high1);
4677           high = high0;
4678           in_p = 1;
4679           if (low == 0)
4680             {
4681               /* We are in the weird situation where high0 > high1 but
4682                  high1 has no successor.  Punt.  */
4683               return 0;
4684             }
4685         }
4686       else if (! subset || highequal)
4687         {
4688           low = low0;
4689           high = range_predecessor (low1);
4690           in_p = 1;
4691           if (high == 0)
4692             {
4693               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4694               return 0;
4695             }
4696         }
4697       else
4698         return 0;
4699     }
4700
4701   else if (! in0_p && in1_p)
4702     {
4703       /* If they don't overlap, the result is the second range.  If the second
4704          is a subset of the first, the result is false.  Otherwise,
4705          the range starts just after the first range and ends at the
4706          end of the second.  */
4707       if (no_overlap)
4708         in_p = 1, low = low1, high = high1;
4709       else if (subset || highequal)
4710         in_p = 0, low = high = 0;
4711       else
4712         {
4713           low = range_successor (high0);
4714           high = high1;
4715           in_p = 1;
4716           if (low == 0)
4717             {
4718               /* high1 > high0 but high0 has no successor.  Punt.  */
4719               return 0;
4720             }
4721         }
4722     }
4723
4724   else
4725     {
4726       /* The case where we are excluding both ranges.  Here the complex case
4727          is if they don't overlap.  In that case, the only time we have a
4728          range is if they are adjacent.  If the second is a subset of the
4729          first, the result is the first.  Otherwise, the range to exclude
4730          starts at the beginning of the first range and ends at the end of the
4731          second.  */
4732       if (no_overlap)
4733         {
4734           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4735                                          range_successor (high0),
4736                                          1, low1, 0)))
4737             in_p = 0, low = low0, high = high1;
4738           else
4739             {
4740               /* Canonicalize - [min, x] into - [-, x].  */
4741               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4742                 switch (TREE_CODE (TREE_TYPE (low0)))
4743                   {
4744                   case ENUMERAL_TYPE:
4745                     if (TYPE_PRECISION (TREE_TYPE (low0))
4746                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4747                       break;
4748                     /* FALLTHROUGH */
4749                   case INTEGER_TYPE:
4750                     if (tree_int_cst_equal (low0,
4751                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4752                       low0 = 0;
4753                     break;
4754                   case POINTER_TYPE:
4755                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4756                         && integer_zerop (low0))
4757                       low0 = 0;
4758                     break;
4759                   default:
4760                     break;
4761                   }
4762
4763               /* Canonicalize - [x, max] into - [x, -].  */
4764               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4765                 switch (TREE_CODE (TREE_TYPE (high1)))
4766                   {
4767                   case ENUMERAL_TYPE:
4768                     if (TYPE_PRECISION (TREE_TYPE (high1))
4769                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4770                       break;
4771                     /* FALLTHROUGH */
4772                   case INTEGER_TYPE:
4773                     if (tree_int_cst_equal (high1,
4774                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4775                       high1 = 0;
4776                     break;
4777                   case POINTER_TYPE:
4778                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4779                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4780                                                        high1, 1,
4781                                                        integer_one_node, 1)))
4782                       high1 = 0;
4783                     break;
4784                   default:
4785                     break;
4786                   }
4787
4788               /* The ranges might be also adjacent between the maximum and
4789                  minimum values of the given type.  For
4790                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4791                  return + [x + 1, y - 1].  */
4792               if (low0 == 0 && high1 == 0)
4793                 {
4794                   low = range_successor (high0);
4795                   high = range_predecessor (low1);
4796                   if (low == 0 || high == 0)
4797                     return 0;
4798
4799                   in_p = 1;
4800                 }
4801               else
4802                 return 0;
4803             }
4804         }
4805       else if (subset)
4806         in_p = 0, low = low0, high = high0;
4807       else
4808         in_p = 0, low = low0, high = high1;
4809     }
4810
4811   *pin_p = in_p, *plow = low, *phigh = high;
4812   return 1;
4813 }
4814 \f
4815
4816 /* Subroutine of fold, looking inside expressions of the form
4817    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4818    of the COND_EXPR.  This function is being used also to optimize
4819    A op B ? C : A, by reversing the comparison first.
4820
4821    Return a folded expression whose code is not a COND_EXPR
4822    anymore, or NULL_TREE if no folding opportunity is found.  */
4823
4824 static tree
4825 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
4826 {
4827   enum tree_code comp_code = TREE_CODE (arg0);
4828   tree arg00 = TREE_OPERAND (arg0, 0);
4829   tree arg01 = TREE_OPERAND (arg0, 1);
4830   tree arg1_type = TREE_TYPE (arg1);
4831   tree tem;
4832
4833   STRIP_NOPS (arg1);
4834   STRIP_NOPS (arg2);
4835
4836   /* If we have A op 0 ? A : -A, consider applying the following
4837      transformations:
4838
4839      A == 0? A : -A    same as -A
4840      A != 0? A : -A    same as A
4841      A >= 0? A : -A    same as abs (A)
4842      A > 0?  A : -A    same as abs (A)
4843      A <= 0? A : -A    same as -abs (A)
4844      A < 0?  A : -A    same as -abs (A)
4845
4846      None of these transformations work for modes with signed
4847      zeros.  If A is +/-0, the first two transformations will
4848      change the sign of the result (from +0 to -0, or vice
4849      versa).  The last four will fix the sign of the result,
4850      even though the original expressions could be positive or
4851      negative, depending on the sign of A.
4852
4853      Note that all these transformations are correct if A is
4854      NaN, since the two alternatives (A and -A) are also NaNs.  */
4855   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4856       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4857           ? real_zerop (arg01)
4858           : integer_zerop (arg01))
4859       && ((TREE_CODE (arg2) == NEGATE_EXPR
4860            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4861              /* In the case that A is of the form X-Y, '-A' (arg2) may
4862                 have already been folded to Y-X, check for that. */
4863           || (TREE_CODE (arg1) == MINUS_EXPR
4864               && TREE_CODE (arg2) == MINUS_EXPR
4865               && operand_equal_p (TREE_OPERAND (arg1, 0),
4866                                   TREE_OPERAND (arg2, 1), 0)
4867               && operand_equal_p (TREE_OPERAND (arg1, 1),
4868                                   TREE_OPERAND (arg2, 0), 0))))
4869     switch (comp_code)
4870       {
4871       case EQ_EXPR:
4872       case UNEQ_EXPR:
4873         tem = fold_convert (arg1_type, arg1);
4874         return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
4875       case NE_EXPR:
4876       case LTGT_EXPR:
4877         return pedantic_non_lvalue (fold_convert (type, arg1));
4878       case UNGE_EXPR:
4879       case UNGT_EXPR:
4880         if (flag_trapping_math)
4881           break;
4882         /* Fall through.  */
4883       case GE_EXPR:
4884       case GT_EXPR:
4885         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4886           arg1 = fold_convert (signed_type_for
4887                                (TREE_TYPE (arg1)), arg1);
4888         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4889         return pedantic_non_lvalue (fold_convert (type, tem));
4890       case UNLE_EXPR:
4891       case UNLT_EXPR:
4892         if (flag_trapping_math)
4893           break;
4894       case LE_EXPR:
4895       case LT_EXPR:
4896         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4897           arg1 = fold_convert (signed_type_for
4898                                (TREE_TYPE (arg1)), arg1);
4899         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4900         return negate_expr (fold_convert (type, tem));
4901       default:
4902         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4903         break;
4904       }
4905
4906   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
4907      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
4908      both transformations are correct when A is NaN: A != 0
4909      is then true, and A == 0 is false.  */
4910
4911   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4912       && integer_zerop (arg01) && integer_zerop (arg2))
4913     {
4914       if (comp_code == NE_EXPR)
4915         return pedantic_non_lvalue (fold_convert (type, arg1));
4916       else if (comp_code == EQ_EXPR)
4917         return build_int_cst (type, 0);
4918     }
4919
4920   /* Try some transformations of A op B ? A : B.
4921
4922      A == B? A : B    same as B
4923      A != B? A : B    same as A
4924      A >= B? A : B    same as max (A, B)
4925      A > B?  A : B    same as max (B, A)
4926      A <= B? A : B    same as min (A, B)
4927      A < B?  A : B    same as min (B, A)
4928
4929      As above, these transformations don't work in the presence
4930      of signed zeros.  For example, if A and B are zeros of
4931      opposite sign, the first two transformations will change
4932      the sign of the result.  In the last four, the original
4933      expressions give different results for (A=+0, B=-0) and
4934      (A=-0, B=+0), but the transformed expressions do not.
4935
4936      The first two transformations are correct if either A or B
4937      is a NaN.  In the first transformation, the condition will
4938      be false, and B will indeed be chosen.  In the case of the
4939      second transformation, the condition A != B will be true,
4940      and A will be chosen.
4941
4942      The conversions to max() and min() are not correct if B is
4943      a number and A is not.  The conditions in the original
4944      expressions will be false, so all four give B.  The min()
4945      and max() versions would give a NaN instead.  */
4946   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4947       && operand_equal_for_comparison_p (arg01, arg2, arg00)
4948       /* Avoid these transformations if the COND_EXPR may be used
4949          as an lvalue in the C++ front-end.  PR c++/19199.  */
4950       && (in_gimple_form
4951           || (strcmp (lang_hooks.name, "GNU C++") != 0
4952               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4953           || ! maybe_lvalue_p (arg1)
4954           || ! maybe_lvalue_p (arg2)))
4955     {
4956       tree comp_op0 = arg00;
4957       tree comp_op1 = arg01;
4958       tree comp_type = TREE_TYPE (comp_op0);
4959
4960       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
4961       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4962         {
4963           comp_type = type;
4964           comp_op0 = arg1;
4965           comp_op1 = arg2;
4966         }
4967
4968       switch (comp_code)
4969         {
4970         case EQ_EXPR:
4971           return pedantic_non_lvalue (fold_convert (type, arg2));
4972         case NE_EXPR:
4973           return pedantic_non_lvalue (fold_convert (type, arg1));
4974         case LE_EXPR:
4975         case LT_EXPR:
4976         case UNLE_EXPR:
4977         case UNLT_EXPR:
4978           /* In C++ a ?: expression can be an lvalue, so put the
4979              operand which will be used if they are equal first
4980              so that we can convert this back to the
4981              corresponding COND_EXPR.  */
4982           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4983             {
4984               comp_op0 = fold_convert (comp_type, comp_op0);
4985               comp_op1 = fold_convert (comp_type, comp_op1);
4986               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4987                     ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
4988                     : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
4989               return pedantic_non_lvalue (fold_convert (type, tem));
4990             }
4991           break;
4992         case GE_EXPR:
4993         case GT_EXPR:
4994         case UNGE_EXPR:
4995         case UNGT_EXPR:
4996           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4997             {
4998               comp_op0 = fold_convert (comp_type, comp_op0);
4999               comp_op1 = fold_convert (comp_type, comp_op1);
5000               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5001                     ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5002                     : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5003               return pedantic_non_lvalue (fold_convert (type, tem));
5004             }
5005           break;
5006         case UNEQ_EXPR:
5007           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5008             return pedantic_non_lvalue (fold_convert (type, arg2));
5009           break;
5010         case LTGT_EXPR:
5011           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5012             return pedantic_non_lvalue (fold_convert (type, arg1));
5013           break;
5014         default:
5015           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5016           break;
5017         }
5018     }
5019
5020   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5021      we might still be able to simplify this.  For example,
5022      if C1 is one less or one more than C2, this might have started
5023      out as a MIN or MAX and been transformed by this function.
5024      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
5025
5026   if (INTEGRAL_TYPE_P (type)
5027       && TREE_CODE (arg01) == INTEGER_CST
5028       && TREE_CODE (arg2) == INTEGER_CST)
5029     switch (comp_code)
5030       {
5031       case EQ_EXPR:
5032         /* We can replace A with C1 in this case.  */
5033         arg1 = fold_convert (type, arg01);
5034         return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5035
5036       case LT_EXPR:
5037         /* If C1 is C2 + 1, this is min(A, C2).  */
5038         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5039                                OEP_ONLY_CONST)
5040             && operand_equal_p (arg01,
5041                                 const_binop (PLUS_EXPR, arg2,
5042                                              build_int_cst (type, 1), 0),
5043                                 OEP_ONLY_CONST))
5044           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5045                                                    type,
5046                                                    fold_convert (type, arg1),
5047                                                    arg2));
5048         break;
5049
5050       case LE_EXPR:
5051         /* If C1 is C2 - 1, this is min(A, C2).  */
5052         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5053                                OEP_ONLY_CONST)
5054             && operand_equal_p (arg01,
5055                                 const_binop (MINUS_EXPR, arg2,
5056                                              build_int_cst (type, 1), 0),
5057                                 OEP_ONLY_CONST))
5058           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5059                                                    type,
5060                                                    fold_convert (type, arg1),
5061                                                    arg2));
5062         break;
5063
5064       case GT_EXPR:
5065         /* If C1 is C2 - 1, this is max(A, C2).  */
5066         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5067                                OEP_ONLY_CONST)
5068             && operand_equal_p (arg01,
5069                                 const_binop (MINUS_EXPR, arg2,
5070                                              build_int_cst (type, 1), 0),
5071                                 OEP_ONLY_CONST))
5072           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5073                                                    type,
5074                                                    fold_convert (type, arg1),
5075                                                    arg2));
5076         break;
5077
5078       case GE_EXPR:
5079         /* If C1 is C2 + 1, this is max(A, C2).  */
5080         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5081                                OEP_ONLY_CONST)
5082             && operand_equal_p (arg01,
5083                                 const_binop (PLUS_EXPR, arg2,
5084                                              build_int_cst (type, 1), 0),
5085                                 OEP_ONLY_CONST))
5086           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5087                                                    type,
5088                                                    fold_convert (type, arg1),
5089                                                    arg2));
5090         break;
5091       case NE_EXPR:
5092         break;
5093       default:
5094         gcc_unreachable ();
5095       }
5096
5097   return NULL_TREE;
5098 }
5099
5100
5101 \f
5102 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5103 #define LOGICAL_OP_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
5104 #endif
5105
5106 /* EXP is some logical combination of boolean tests.  See if we can
5107    merge it into some range test.  Return the new tree if so.  */
5108
5109 static tree
5110 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5111 {
5112   int or_op = (code == TRUTH_ORIF_EXPR
5113                || code == TRUTH_OR_EXPR);
5114   int in0_p, in1_p, in_p;
5115   tree low0, low1, low, high0, high1, high;
5116   bool strict_overflow_p = false;
5117   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5118   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5119   tree tem;
5120   const char * const warnmsg = G_("assuming signed overflow does not occur "
5121                                   "when simplifying range test");
5122
5123   /* If this is an OR operation, invert both sides; we will invert
5124      again at the end.  */
5125   if (or_op)
5126     in0_p = ! in0_p, in1_p = ! in1_p;
5127
5128   /* If both expressions are the same, if we can merge the ranges, and we
5129      can build the range test, return it or it inverted.  If one of the
5130      ranges is always true or always false, consider it to be the same
5131      expression as the other.  */
5132   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5133       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5134                        in1_p, low1, high1)
5135       && 0 != (tem = (build_range_check (type,
5136                                          lhs != 0 ? lhs
5137                                          : rhs != 0 ? rhs : integer_zero_node,
5138                                          in_p, low, high))))
5139     {
5140       if (strict_overflow_p)
5141         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5142       return or_op ? invert_truthvalue (tem) : tem;
5143     }
5144
5145   /* On machines where the branch cost is expensive, if this is a
5146      short-circuited branch and the underlying object on both sides
5147      is the same, make a non-short-circuit operation.  */
5148   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5149            && lhs != 0 && rhs != 0
5150            && (code == TRUTH_ANDIF_EXPR
5151                || code == TRUTH_ORIF_EXPR)
5152            && operand_equal_p (lhs, rhs, 0))
5153     {
5154       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
5155          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5156          which cases we can't do this.  */
5157       if (simple_operand_p (lhs))
5158         return build2 (code == TRUTH_ANDIF_EXPR
5159                        ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5160                        type, op0, op1);
5161
5162       else if (lang_hooks.decls.global_bindings_p () == 0
5163                && ! CONTAINS_PLACEHOLDER_P (lhs))
5164         {
5165           tree common = save_expr (lhs);
5166
5167           if (0 != (lhs = build_range_check (type, common,
5168                                              or_op ? ! in0_p : in0_p,
5169                                              low0, high0))
5170               && (0 != (rhs = build_range_check (type, common,
5171                                                  or_op ? ! in1_p : in1_p,
5172                                                  low1, high1))))
5173             {
5174               if (strict_overflow_p)
5175                 fold_overflow_warning (warnmsg,
5176                                        WARN_STRICT_OVERFLOW_COMPARISON);
5177               return build2 (code == TRUTH_ANDIF_EXPR
5178                              ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5179                              type, lhs, rhs);
5180             }
5181         }
5182     }
5183
5184   return 0;
5185 }
5186 \f
5187 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5188    bit value.  Arrange things so the extra bits will be set to zero if and
5189    only if C is signed-extended to its full width.  If MASK is nonzero,
5190    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
5191
5192 static tree
5193 unextend (tree c, int p, int unsignedp, tree mask)
5194 {
5195   tree type = TREE_TYPE (c);
5196   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5197   tree temp;
5198
5199   if (p == modesize || unsignedp)
5200     return c;
5201
5202   /* We work by getting just the sign bit into the low-order bit, then
5203      into the high-order bit, then sign-extend.  We then XOR that value
5204      with C.  */
5205   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5206   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5207
5208   /* We must use a signed type in order to get an arithmetic right shift.
5209      However, we must also avoid introducing accidental overflows, so that
5210      a subsequent call to integer_zerop will work.  Hence we must
5211      do the type conversion here.  At this point, the constant is either
5212      zero or one, and the conversion to a signed type can never overflow.
5213      We could get an overflow if this conversion is done anywhere else.  */
5214   if (TYPE_UNSIGNED (type))
5215     temp = fold_convert (signed_type_for (type), temp);
5216
5217   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5218   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5219   if (mask != 0)
5220     temp = const_binop (BIT_AND_EXPR, temp,
5221                         fold_convert (TREE_TYPE (c), mask), 0);
5222   /* If necessary, convert the type back to match the type of C.  */
5223   if (TYPE_UNSIGNED (type))
5224     temp = fold_convert (type, temp);
5225
5226   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5227 }
5228 \f
5229 /* Find ways of folding logical expressions of LHS and RHS:
5230    Try to merge two comparisons to the same innermost item.
5231    Look for range tests like "ch >= '0' && ch <= '9'".
5232    Look for combinations of simple terms on machines with expensive branches
5233    and evaluate the RHS unconditionally.
5234
5235    For example, if we have p->a == 2 && p->b == 4 and we can make an
5236    object large enough to span both A and B, we can do this with a comparison
5237    against the object ANDed with the a mask.
5238
5239    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5240    operations to do this with one comparison.
5241
5242    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5243    function and the one above.
5244
5245    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5246    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5247
5248    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5249    two operands.
5250
5251    We return the simplified tree or 0 if no optimization is possible.  */
5252
5253 static tree
5254 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5255 {
5256   /* If this is the "or" of two comparisons, we can do something if
5257      the comparisons are NE_EXPR.  If this is the "and", we can do something
5258      if the comparisons are EQ_EXPR.  I.e.,
5259         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5260
5261      WANTED_CODE is this operation code.  For single bit fields, we can
5262      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5263      comparison for one-bit fields.  */
5264
5265   enum tree_code wanted_code;
5266   enum tree_code lcode, rcode;
5267   tree ll_arg, lr_arg, rl_arg, rr_arg;
5268   tree ll_inner, lr_inner, rl_inner, rr_inner;
5269   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5270   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5271   HOST_WIDE_INT xll_bitpos, xrl_bitpos;
5272   HOST_WIDE_INT lnbitsize, lnbitpos;
5273   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5274   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5275   enum machine_mode lnmode;
5276   tree ll_mask, lr_mask, rl_mask, rr_mask;
5277   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5278   tree l_const, r_const;
5279   tree lntype, result;
5280   int first_bit, end_bit;
5281   int volatilep;
5282   tree orig_lhs = lhs, orig_rhs = rhs;
5283   enum tree_code orig_code = code;
5284
5285   /* Start by getting the comparison codes.  Fail if anything is volatile.
5286      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5287      it were surrounded with a NE_EXPR.  */
5288
5289   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5290     return 0;
5291
5292   lcode = TREE_CODE (lhs);
5293   rcode = TREE_CODE (rhs);
5294
5295   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5296     {
5297       lhs = build2 (NE_EXPR, truth_type, lhs,
5298                     build_int_cst (TREE_TYPE (lhs), 0));
5299       lcode = NE_EXPR;
5300     }
5301
5302   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5303     {
5304       rhs = build2 (NE_EXPR, truth_type, rhs,
5305                     build_int_cst (TREE_TYPE (rhs), 0));
5306       rcode = NE_EXPR;
5307     }
5308
5309   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5310       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5311     return 0;
5312
5313   ll_arg = TREE_OPERAND (lhs, 0);
5314   lr_arg = TREE_OPERAND (lhs, 1);
5315   rl_arg = TREE_OPERAND (rhs, 0);
5316   rr_arg = TREE_OPERAND (rhs, 1);
5317
5318   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5319   if (simple_operand_p (ll_arg)
5320       && simple_operand_p (lr_arg))
5321     {
5322       tree result;
5323       if (operand_equal_p (ll_arg, rl_arg, 0)
5324           && operand_equal_p (lr_arg, rr_arg, 0))
5325         {
5326           result = combine_comparisons (code, lcode, rcode,
5327                                         truth_type, ll_arg, lr_arg);
5328           if (result)
5329             return result;
5330         }
5331       else if (operand_equal_p (ll_arg, rr_arg, 0)
5332                && operand_equal_p (lr_arg, rl_arg, 0))
5333         {
5334           result = combine_comparisons (code, lcode,
5335                                         swap_tree_comparison (rcode),
5336                                         truth_type, ll_arg, lr_arg);
5337           if (result)
5338             return result;
5339         }
5340     }
5341
5342   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5343           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5344
5345   /* If the RHS can be evaluated unconditionally and its operands are
5346      simple, it wins to evaluate the RHS unconditionally on machines
5347      with expensive branches.  In this case, this isn't a comparison
5348      that can be merged.  Avoid doing this if the RHS is a floating-point
5349      comparison since those can trap.  */
5350
5351   if (BRANCH_COST >= 2
5352       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5353       && simple_operand_p (rl_arg)
5354       && simple_operand_p (rr_arg))
5355     {
5356       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5357       if (code == TRUTH_OR_EXPR
5358           && lcode == NE_EXPR && integer_zerop (lr_arg)
5359           && rcode == NE_EXPR && integer_zerop (rr_arg)
5360           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
5361         return build2 (NE_EXPR, truth_type,
5362                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5363                                ll_arg, rl_arg),
5364                        build_int_cst (TREE_TYPE (ll_arg), 0));
5365
5366       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5367       if (code == TRUTH_AND_EXPR
5368           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5369           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5370           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
5371         return build2 (EQ_EXPR, truth_type,
5372                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5373                                ll_arg, rl_arg),
5374                        build_int_cst (TREE_TYPE (ll_arg), 0));
5375
5376       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5377         {
5378           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5379             return build2 (code, truth_type, lhs, rhs);
5380           return NULL_TREE;
5381         }
5382     }
5383
5384   /* See if the comparisons can be merged.  Then get all the parameters for
5385      each side.  */
5386
5387   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5388       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5389     return 0;
5390
5391   volatilep = 0;
5392   ll_inner = decode_field_reference (ll_arg,
5393                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5394                                      &ll_unsignedp, &volatilep, &ll_mask,
5395                                      &ll_and_mask);
5396   lr_inner = decode_field_reference (lr_arg,
5397                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5398                                      &lr_unsignedp, &volatilep, &lr_mask,
5399                                      &lr_and_mask);
5400   rl_inner = decode_field_reference (rl_arg,
5401                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5402                                      &rl_unsignedp, &volatilep, &rl_mask,
5403                                      &rl_and_mask);
5404   rr_inner = decode_field_reference (rr_arg,
5405                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5406                                      &rr_unsignedp, &volatilep, &rr_mask,
5407                                      &rr_and_mask);
5408
5409   /* It must be true that the inner operation on the lhs of each
5410      comparison must be the same if we are to be able to do anything.
5411      Then see if we have constants.  If not, the same must be true for
5412      the rhs's.  */
5413   if (volatilep || ll_inner == 0 || rl_inner == 0
5414       || ! operand_equal_p (ll_inner, rl_inner, 0))
5415     return 0;
5416
5417   if (TREE_CODE (lr_arg) == INTEGER_CST
5418       && TREE_CODE (rr_arg) == INTEGER_CST)
5419     l_const = lr_arg, r_const = rr_arg;
5420   else if (lr_inner == 0 || rr_inner == 0
5421            || ! operand_equal_p (lr_inner, rr_inner, 0))
5422     return 0;
5423   else
5424     l_const = r_const = 0;
5425
5426   /* If either comparison code is not correct for our logical operation,
5427      fail.  However, we can convert a one-bit comparison against zero into
5428      the opposite comparison against that bit being set in the field.  */
5429
5430   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5431   if (lcode != wanted_code)
5432     {
5433       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5434         {
5435           /* Make the left operand unsigned, since we are only interested
5436              in the value of one bit.  Otherwise we are doing the wrong
5437              thing below.  */
5438           ll_unsignedp = 1;
5439           l_const = ll_mask;
5440         }
5441       else
5442         return 0;
5443     }
5444
5445   /* This is analogous to the code for l_const above.  */
5446   if (rcode != wanted_code)
5447     {
5448       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5449         {
5450           rl_unsignedp = 1;
5451           r_const = rl_mask;
5452         }
5453       else
5454         return 0;
5455     }
5456
5457   /* See if we can find a mode that contains both fields being compared on
5458      the left.  If we can't, fail.  Otherwise, update all constants and masks
5459      to be relative to a field of that size.  */
5460   first_bit = MIN (ll_bitpos, rl_bitpos);
5461   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5462   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5463                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5464                           volatilep);
5465   if (lnmode == VOIDmode)
5466     return 0;
5467
5468   lnbitsize = GET_MODE_BITSIZE (lnmode);
5469   lnbitpos = first_bit & ~ (lnbitsize - 1);
5470   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5471   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5472
5473   if (BYTES_BIG_ENDIAN)
5474     {
5475       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5476       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5477     }
5478
5479   ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5480                          size_int (xll_bitpos), 0);
5481   rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5482                          size_int (xrl_bitpos), 0);
5483
5484   if (l_const)
5485     {
5486       l_const = fold_convert (lntype, l_const);
5487       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5488       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5489       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5490                                         fold_build1 (BIT_NOT_EXPR,
5491                                                      lntype, ll_mask),
5492                                         0)))
5493         {
5494           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5495
5496           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5497         }
5498     }
5499   if (r_const)
5500     {
5501       r_const = fold_convert (lntype, r_const);
5502       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5503       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5504       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5505                                         fold_build1 (BIT_NOT_EXPR,
5506                                                      lntype, rl_mask),
5507                                         0)))
5508         {
5509           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5510
5511           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5512         }
5513     }
5514
5515   /* Handle the case of comparisons with constants.  If there is something in
5516      common between the masks, those bits of the constants must be the same.
5517      If not, the condition is always false.  Test for this to avoid generating
5518      incorrect code below.  */
5519   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5520   if (! integer_zerop (result)
5521       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5522                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5523     {
5524       if (wanted_code == NE_EXPR)
5525         {
5526           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5527           return constant_boolean_node (true, truth_type);
5528         }
5529       else
5530         {
5531           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5532           return constant_boolean_node (false, truth_type);
5533         }
5534     }
5535
5536   return NULL_TREE;
5537 }
5538 \f
5539 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5540    constant.  */
5541
5542 static tree
5543 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5544 {
5545   tree arg0 = op0;
5546   enum tree_code op_code;
5547   tree comp_const = op1;
5548   tree minmax_const;
5549   int consts_equal, consts_lt;
5550   tree inner;
5551
5552   STRIP_SIGN_NOPS (arg0);
5553
5554   op_code = TREE_CODE (arg0);
5555   minmax_const = TREE_OPERAND (arg0, 1);
5556   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5557   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5558   inner = TREE_OPERAND (arg0, 0);
5559
5560   /* If something does not permit us to optimize, return the original tree.  */
5561   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5562       || TREE_CODE (comp_const) != INTEGER_CST
5563       || TREE_OVERFLOW (comp_const)
5564       || TREE_CODE (minmax_const) != INTEGER_CST
5565       || TREE_OVERFLOW (minmax_const))
5566     return NULL_TREE;
5567
5568   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5569      and GT_EXPR, doing the rest with recursive calls using logical
5570      simplifications.  */
5571   switch (code)
5572     {
5573     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5574       {
5575         tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5576                                           type, op0, op1);
5577         if (tem)
5578           return invert_truthvalue (tem);
5579         return NULL_TREE;
5580       }
5581
5582     case GE_EXPR:
5583       return
5584         fold_build2 (TRUTH_ORIF_EXPR, type,
5585                      optimize_minmax_comparison
5586                      (EQ_EXPR, type, arg0, comp_const),
5587                      optimize_minmax_comparison
5588                      (GT_EXPR, type, arg0, comp_const));
5589
5590     case EQ_EXPR:
5591       if (op_code == MAX_EXPR && consts_equal)
5592         /* MAX (X, 0) == 0  ->  X <= 0  */
5593         return fold_build2 (LE_EXPR, type, inner, comp_const);
5594
5595       else if (op_code == MAX_EXPR && consts_lt)
5596         /* MAX (X, 0) == 5  ->  X == 5   */
5597         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5598
5599       else if (op_code == MAX_EXPR)
5600         /* MAX (X, 0) == -1  ->  false  */
5601         return omit_one_operand (type, integer_zero_node, inner);
5602
5603       else if (consts_equal)
5604         /* MIN (X, 0) == 0  ->  X >= 0  */
5605         return fold_build2 (GE_EXPR, type, inner, comp_const);
5606
5607       else if (consts_lt)
5608         /* MIN (X, 0) == 5  ->  false  */
5609         return omit_one_operand (type, integer_zero_node, inner);
5610
5611       else
5612         /* MIN (X, 0) == -1  ->  X == -1  */
5613         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5614
5615     case GT_EXPR:
5616       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5617         /* MAX (X, 0) > 0  ->  X > 0
5618            MAX (X, 0) > 5  ->  X > 5  */
5619         return fold_build2 (GT_EXPR, type, inner, comp_const);
5620
5621       else if (op_code == MAX_EXPR)
5622         /* MAX (X, 0) > -1  ->  true  */
5623         return omit_one_operand (type, integer_one_node, inner);
5624
5625       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5626         /* MIN (X, 0) > 0  ->  false
5627            MIN (X, 0) > 5  ->  false  */
5628         return omit_one_operand (type, integer_zero_node, inner);
5629
5630       else
5631         /* MIN (X, 0) > -1  ->  X > -1  */
5632         return fold_build2 (GT_EXPR, type, inner, comp_const);
5633
5634     default:
5635       return NULL_TREE;
5636     }
5637 }
5638 \f
5639 /* T is an integer expression that is being multiplied, divided, or taken a
5640    modulus (CODE says which and what kind of divide or modulus) by a
5641    constant C.  See if we can eliminate that operation by folding it with
5642    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5643    should be used for the computation if wider than our type.
5644
5645    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5646    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5647    expression would not overflow or that overflow is undefined for the type
5648    in the language in question.
5649
5650    We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
5651    the machine has a multiply-accumulate insn or that this is part of an
5652    addressing calculation.
5653
5654    If we return a non-null expression, it is an equivalent form of the
5655    original computation, but need not be in the original type.
5656
5657    We set *STRICT_OVERFLOW_P to true if the return values depends on
5658    signed overflow being undefined.  Otherwise we do not change
5659    *STRICT_OVERFLOW_P.  */
5660
5661 static tree
5662 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5663                 bool *strict_overflow_p)
5664 {
5665   /* To avoid exponential search depth, refuse to allow recursion past
5666      three levels.  Beyond that (1) it's highly unlikely that we'll find
5667      something interesting and (2) we've probably processed it before
5668      when we built the inner expression.  */
5669
5670   static int depth;
5671   tree ret;
5672
5673   if (depth > 3)
5674     return NULL;
5675
5676   depth++;
5677   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5678   depth--;
5679
5680   return ret;
5681 }
5682
5683 static tree
5684 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5685                   bool *strict_overflow_p)
5686 {
5687   tree type = TREE_TYPE (t);
5688   enum tree_code tcode = TREE_CODE (t);
5689   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5690                                    > GET_MODE_SIZE (TYPE_MODE (type)))
5691                 ? wide_type : type);
5692   tree t1, t2;
5693   int same_p = tcode == code;
5694   tree op0 = NULL_TREE, op1 = NULL_TREE;
5695   bool sub_strict_overflow_p;
5696
5697   /* Don't deal with constants of zero here; they confuse the code below.  */
5698   if (integer_zerop (c))
5699     return NULL_TREE;
5700
5701   if (TREE_CODE_CLASS (tcode) == tcc_unary)
5702     op0 = TREE_OPERAND (t, 0);
5703
5704   if (TREE_CODE_CLASS (tcode) == tcc_binary)
5705     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5706
5707   /* Note that we need not handle conditional operations here since fold
5708      already handles those cases.  So just do arithmetic here.  */
5709   switch (tcode)
5710     {
5711     case INTEGER_CST:
5712       /* For a constant, we can always simplify if we are a multiply
5713          or (for divide and modulus) if it is a multiple of our constant.  */
5714       if (code == MULT_EXPR
5715           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
5716         return const_binop (code, fold_convert (ctype, t),
5717                             fold_convert (ctype, c), 0);
5718       break;
5719
5720     case CONVERT_EXPR:  case NON_LVALUE_EXPR:  case NOP_EXPR:
5721       /* If op0 is an expression ...  */
5722       if ((COMPARISON_CLASS_P (op0)
5723            || UNARY_CLASS_P (op0)
5724            || BINARY_CLASS_P (op0)
5725            || VL_EXP_CLASS_P (op0)
5726            || EXPRESSION_CLASS_P (op0))
5727           /* ... and is unsigned, and its type is smaller than ctype,
5728              then we cannot pass through as widening.  */
5729           && ((TYPE_UNSIGNED (TREE_TYPE (op0))
5730                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5731                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5732                && (GET_MODE_SIZE (TYPE_MODE (ctype))
5733                    > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
5734               /* ... or this is a truncation (t is narrower than op0),
5735                  then we cannot pass through this narrowing.  */
5736               || (GET_MODE_SIZE (TYPE_MODE (type))
5737                   < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
5738               /* ... or signedness changes for division or modulus,
5739                  then we cannot pass through this conversion.  */
5740               || (code != MULT_EXPR
5741                   && (TYPE_UNSIGNED (ctype)
5742                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
5743               /* ... or has undefined overflow while the converted to
5744                  type has not, we cannot do the operation in the inner type
5745                  as that would introduce undefined overflow.  */
5746               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5747                   && !TYPE_OVERFLOW_UNDEFINED (type))))
5748         break;
5749
5750       /* Pass the constant down and see if we can make a simplification.  If
5751          we can, replace this expression with the inner simplification for
5752          possible later conversion to our or some other type.  */
5753       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5754           && TREE_CODE (t2) == INTEGER_CST
5755           && !TREE_OVERFLOW (t2)
5756           && (0 != (t1 = extract_muldiv (op0, t2, code,
5757                                          code == MULT_EXPR
5758                                          ? ctype : NULL_TREE,
5759                                          strict_overflow_p))))
5760         return t1;
5761       break;
5762
5763     case ABS_EXPR:
5764       /* If widening the type changes it from signed to unsigned, then we
5765          must avoid building ABS_EXPR itself as unsigned.  */
5766       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5767         {
5768           tree cstype = (*signed_type_for) (ctype);
5769           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5770               != 0)
5771             {
5772               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5773               return fold_convert (ctype, t1);
5774             }
5775           break;
5776         }
5777       /* If the constant is negative, we cannot simplify this.  */
5778       if (tree_int_cst_sgn (c) == -1)
5779         break;
5780       /* FALLTHROUGH */
5781     case NEGATE_EXPR:
5782       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5783           != 0)
5784         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5785       break;
5786
5787     case MIN_EXPR:  case MAX_EXPR:
5788       /* If widening the type changes the signedness, then we can't perform
5789          this optimization as that changes the result.  */
5790       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5791         break;
5792
5793       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
5794       sub_strict_overflow_p = false;
5795       if ((t1 = extract_muldiv (op0, c, code, wide_type,
5796                                 &sub_strict_overflow_p)) != 0
5797           && (t2 = extract_muldiv (op1, c, code, wide_type,
5798                                    &sub_strict_overflow_p)) != 0)
5799         {
5800           if (tree_int_cst_sgn (c) < 0)
5801             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5802           if (sub_strict_overflow_p)
5803             *strict_overflow_p = true;
5804           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5805                               fold_convert (ctype, t2));
5806         }
5807       break;
5808
5809     case LSHIFT_EXPR:  case RSHIFT_EXPR:
5810       /* If the second operand is constant, this is a multiplication
5811          or floor division, by a power of two, so we can treat it that
5812          way unless the multiplier or divisor overflows.  Signed
5813          left-shift overflow is implementation-defined rather than
5814          undefined in C90, so do not convert signed left shift into
5815          multiplication.  */
5816       if (TREE_CODE (op1) == INTEGER_CST
5817           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5818           /* const_binop may not detect overflow correctly,
5819              so check for it explicitly here.  */
5820           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5821           && TREE_INT_CST_HIGH (op1) == 0
5822           && 0 != (t1 = fold_convert (ctype,
5823                                       const_binop (LSHIFT_EXPR,
5824                                                    size_one_node,
5825                                                    op1, 0)))
5826           && !TREE_OVERFLOW (t1))
5827         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5828                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
5829                                        ctype, fold_convert (ctype, op0), t1),
5830                                c, code, wide_type, strict_overflow_p);
5831       break;
5832
5833     case PLUS_EXPR:  case MINUS_EXPR:
5834       /* See if we can eliminate the operation on both sides.  If we can, we
5835          can return a new PLUS or MINUS.  If we can't, the only remaining
5836          cases where we can do anything are if the second operand is a
5837          constant.  */
5838       sub_strict_overflow_p = false;
5839       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5840       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5841       if (t1 != 0 && t2 != 0
5842           && (code == MULT_EXPR
5843               /* If not multiplication, we can only do this if both operands
5844                  are divisible by c.  */
5845               || (multiple_of_p (ctype, op0, c)
5846                   && multiple_of_p (ctype, op1, c))))
5847         {
5848           if (sub_strict_overflow_p)
5849             *strict_overflow_p = true;
5850           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5851                               fold_convert (ctype, t2));
5852         }
5853
5854       /* If this was a subtraction, negate OP1 and set it to be an addition.
5855          This simplifies the logic below.  */
5856       if (tcode == MINUS_EXPR)
5857         tcode = PLUS_EXPR, op1 = negate_expr (op1);
5858
5859       if (TREE_CODE (op1) != INTEGER_CST)
5860         break;
5861
5862       /* If either OP1 or C are negative, this optimization is not safe for
5863          some of the division and remainder types while for others we need
5864          to change the code.  */
5865       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5866         {
5867           if (code == CEIL_DIV_EXPR)
5868             code = FLOOR_DIV_EXPR;
5869           else if (code == FLOOR_DIV_EXPR)
5870             code = CEIL_DIV_EXPR;
5871           else if (code != MULT_EXPR
5872                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5873             break;
5874         }
5875
5876       /* If it's a multiply or a division/modulus operation of a multiple
5877          of our constant, do the operation and verify it doesn't overflow.  */
5878       if (code == MULT_EXPR
5879           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5880         {
5881           op1 = const_binop (code, fold_convert (ctype, op1),
5882                              fold_convert (ctype, c), 0);
5883           /* We allow the constant to overflow with wrapping semantics.  */
5884           if (op1 == 0
5885               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5886             break;
5887         }
5888       else
5889         break;
5890
5891       /* If we have an unsigned type is not a sizetype, we cannot widen
5892          the operation since it will change the result if the original
5893          computation overflowed.  */
5894       if (TYPE_UNSIGNED (ctype)
5895           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5896           && ctype != type)
5897         break;
5898
5899       /* If we were able to eliminate our operation from the first side,
5900          apply our operation to the second side and reform the PLUS.  */
5901       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5902         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5903
5904       /* The last case is if we are a multiply.  In that case, we can
5905          apply the distributive law to commute the multiply and addition
5906          if the multiplication of the constants doesn't overflow.  */
5907       if (code == MULT_EXPR)
5908         return fold_build2 (tcode, ctype,
5909                             fold_build2 (code, ctype,
5910                                          fold_convert (ctype, op0),
5911                                          fold_convert (ctype, c)),
5912                             op1);
5913
5914       break;
5915
5916     case MULT_EXPR:
5917       /* We have a special case here if we are doing something like
5918          (C * 8) % 4 since we know that's zero.  */
5919       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5920            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5921           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5922           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5923         return omit_one_operand (type, integer_zero_node, op0);
5924
5925       /* ... fall through ...  */
5926
5927     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
5928     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
5929       /* If we can extract our operation from the LHS, do so and return a
5930          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
5931          do something only if the second operand is a constant.  */
5932       if (same_p
5933           && (t1 = extract_muldiv (op0, c, code, wide_type,
5934                                    strict_overflow_p)) != 0)
5935         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5936                             fold_convert (ctype, op1));
5937       else if (tcode == MULT_EXPR && code == MULT_EXPR
5938                && (t1 = extract_muldiv (op1, c, code, wide_type,
5939                                         strict_overflow_p)) != 0)
5940         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5941                             fold_convert (ctype, t1));
5942       else if (TREE_CODE (op1) != INTEGER_CST)
5943         return 0;
5944
5945       /* If these are the same operation types, we can associate them
5946          assuming no overflow.  */
5947       if (tcode == code
5948           && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
5949                                      fold_convert (ctype, c), 0))
5950           && !TREE_OVERFLOW (t1))
5951         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5952
5953       /* If these operations "cancel" each other, we have the main
5954          optimizations of this pass, which occur when either constant is a
5955          multiple of the other, in which case we replace this with either an
5956          operation or CODE or TCODE.
5957
5958          If we have an unsigned type that is not a sizetype, we cannot do
5959          this since it will change the result if the original computation
5960          overflowed.  */
5961       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5962            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5963           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5964               || (tcode == MULT_EXPR
5965                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5966                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5967                   && code != MULT_EXPR)))
5968         {
5969           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5970             {
5971               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5972                 *strict_overflow_p = true;
5973               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5974                                   fold_convert (ctype,
5975                                                 const_binop (TRUNC_DIV_EXPR,
5976                                                              op1, c, 0)));
5977             }
5978           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
5979             {
5980               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5981                 *strict_overflow_p = true;
5982               return fold_build2 (code, ctype, fold_convert (ctype, op0),
5983                                   fold_convert (ctype,
5984                                                 const_binop (TRUNC_DIV_EXPR,
5985                                                              c, op1, 0)));
5986             }
5987         }
5988       break;
5989
5990     default:
5991       break;
5992     }
5993
5994   return 0;
5995 }
5996 \f
5997 /* Return a node which has the indicated constant VALUE (either 0 or
5998    1), and is of the indicated TYPE.  */
5999
6000 tree
6001 constant_boolean_node (int value, tree type)
6002 {
6003   if (type == integer_type_node)
6004     return value ? integer_one_node : integer_zero_node;
6005   else if (type == boolean_type_node)
6006     return value ? boolean_true_node : boolean_false_node;
6007   else
6008     return build_int_cst (type, value);
6009 }
6010
6011
6012 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6013    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6014    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6015    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6016    COND is the first argument to CODE; otherwise (as in the example
6017    given here), it is the second argument.  TYPE is the type of the
6018    original expression.  Return NULL_TREE if no simplification is
6019    possible.  */
6020
6021 static tree
6022 fold_binary_op_with_conditional_arg (enum tree_code code,
6023                                      tree type, tree op0, tree op1,
6024                                      tree cond, tree arg, int cond_first_p)
6025 {
6026   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6027   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6028   tree test, true_value, false_value;
6029   tree lhs = NULL_TREE;
6030   tree rhs = NULL_TREE;
6031
6032   /* This transformation is only worthwhile if we don't have to wrap
6033      arg in a SAVE_EXPR, and the operation can be simplified on at least
6034      one of the branches once its pushed inside the COND_EXPR.  */
6035   if (!TREE_CONSTANT (arg))
6036     return NULL_TREE;
6037
6038   if (TREE_CODE (cond) == COND_EXPR)
6039     {
6040       test = TREE_OPERAND (cond, 0);
6041       true_value = TREE_OPERAND (cond, 1);
6042       false_value = TREE_OPERAND (cond, 2);
6043       /* If this operand throws an expression, then it does not make
6044          sense to try to perform a logical or arithmetic operation
6045          involving it.  */
6046       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6047         lhs = true_value;
6048       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6049         rhs = false_value;
6050     }
6051   else
6052     {
6053       tree testtype = TREE_TYPE (cond);
6054       test = cond;
6055       true_value = constant_boolean_node (true, testtype);
6056       false_value = constant_boolean_node (false, testtype);
6057     }
6058
6059   arg = fold_convert (arg_type, arg);
6060   if (lhs == 0)
6061     {
6062       true_value = fold_convert (cond_type, true_value);
6063       if (cond_first_p)
6064         lhs = fold_build2 (code, type, true_value, arg);
6065       else
6066         lhs = fold_build2 (code, type, arg, true_value);
6067     }
6068   if (rhs == 0)
6069     {
6070       false_value = fold_convert (cond_type, false_value);
6071       if (cond_first_p)
6072         rhs = fold_build2 (code, type, false_value, arg);
6073       else
6074         rhs = fold_build2 (code, type, arg, false_value);
6075     }
6076
6077   test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6078   return fold_convert (type, test);
6079 }
6080
6081 \f
6082 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6083
6084    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6085    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6086    ADDEND is the same as X.
6087
6088    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6089    and finite.  The problematic cases are when X is zero, and its mode
6090    has signed zeros.  In the case of rounding towards -infinity,
6091    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6092    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6093
6094 bool
6095 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6096 {
6097   if (!real_zerop (addend))
6098     return false;
6099
6100   /* Don't allow the fold with -fsignaling-nans.  */
6101   if (HONOR_SNANS (TYPE_MODE (type)))
6102     return false;
6103
6104   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6105   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6106     return true;
6107
6108   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6109   if (TREE_CODE (addend) == REAL_CST
6110       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6111     negate = !negate;
6112
6113   /* The mode has signed zeros, and we have to honor their sign.
6114      In this situation, there is only one case we can return true for.
6115      X - 0 is the same as X unless rounding towards -infinity is
6116      supported.  */
6117   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6118 }
6119
6120 /* Subroutine of fold() that checks comparisons of built-in math
6121    functions against real constants.
6122
6123    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6124    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6125    is the type of the result and ARG0 and ARG1 are the operands of the
6126    comparison.  ARG1 must be a TREE_REAL_CST.
6127
6128    The function returns the constant folded tree if a simplification
6129    can be made, and NULL_TREE otherwise.  */
6130
6131 static tree
6132 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6133                      tree type, tree arg0, tree arg1)
6134 {
6135   REAL_VALUE_TYPE c;
6136
6137   if (BUILTIN_SQRT_P (fcode))
6138     {
6139       tree arg = CALL_EXPR_ARG (arg0, 0);
6140       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6141
6142       c = TREE_REAL_CST (arg1);
6143       if (REAL_VALUE_NEGATIVE (c))
6144         {
6145           /* sqrt(x) < y is always false, if y is negative.  */
6146           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6147             return omit_one_operand (type, integer_zero_node, arg);
6148
6149           /* sqrt(x) > y is always true, if y is negative and we
6150              don't care about NaNs, i.e. negative values of x.  */
6151           if (code == NE_EXPR || !HONOR_NANS (mode))
6152             return omit_one_operand (type, integer_one_node, arg);
6153
6154           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6155           return fold_build2 (GE_EXPR, type, arg,
6156                               build_real (TREE_TYPE (arg), dconst0));
6157         }
6158       else if (code == GT_EXPR || code == GE_EXPR)
6159         {
6160           REAL_VALUE_TYPE c2;
6161
6162           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6163           real_convert (&c2, mode, &c2);
6164
6165           if (REAL_VALUE_ISINF (c2))
6166             {
6167               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6168               if (HONOR_INFINITIES (mode))
6169                 return fold_build2 (EQ_EXPR, type, arg,
6170                                     build_real (TREE_TYPE (arg), c2));
6171
6172               /* sqrt(x) > y is always false, when y is very large
6173                  and we don't care about infinities.  */
6174               return omit_one_operand (type, integer_zero_node, arg);
6175             }
6176
6177           /* sqrt(x) > c is the same as x > c*c.  */
6178           return fold_build2 (code, type, arg,
6179                               build_real (TREE_TYPE (arg), c2));
6180         }
6181       else if (code == LT_EXPR || code == LE_EXPR)
6182         {
6183           REAL_VALUE_TYPE c2;
6184
6185           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6186           real_convert (&c2, mode, &c2);
6187
6188           if (REAL_VALUE_ISINF (c2))
6189             {
6190               /* sqrt(x) < y is always true, when y is a very large
6191                  value and we don't care about NaNs or Infinities.  */
6192               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6193                 return omit_one_operand (type, integer_one_node, arg);
6194
6195               /* sqrt(x) < y is x != +Inf when y is very large and we
6196                  don't care about NaNs.  */
6197               if (! HONOR_NANS (mode))
6198                 return fold_build2 (NE_EXPR, type, arg,
6199                                     build_real (TREE_TYPE (arg), c2));
6200
6201               /* sqrt(x) < y is x >= 0 when y is very large and we
6202                  don't care about Infinities.  */
6203               if (! HONOR_INFINITIES (mode))
6204                 return fold_build2 (GE_EXPR, type, arg,
6205                                     build_real (TREE_TYPE (arg), dconst0));
6206
6207               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6208               if (lang_hooks.decls.global_bindings_p () != 0
6209                   || CONTAINS_PLACEHOLDER_P (arg))
6210                 return NULL_TREE;
6211
6212               arg = save_expr (arg);
6213               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6214                                   fold_build2 (GE_EXPR, type, arg,
6215                                                build_real (TREE_TYPE (arg),
6216                                                            dconst0)),
6217                                   fold_build2 (NE_EXPR, type, arg,
6218                                                build_real (TREE_TYPE (arg),
6219                                                            c2)));
6220             }
6221
6222           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6223           if (! HONOR_NANS (mode))
6224             return fold_build2 (code, type, arg,
6225                                 build_real (TREE_TYPE (arg), c2));
6226
6227           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6228           if (lang_hooks.decls.global_bindings_p () == 0
6229               && ! CONTAINS_PLACEHOLDER_P (arg))
6230             {
6231               arg = save_expr (arg);
6232               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6233                                   fold_build2 (GE_EXPR, type, arg,
6234                                                build_real (TREE_TYPE (arg),
6235                                                            dconst0)),
6236                                   fold_build2 (code, type, arg,
6237                                                build_real (TREE_TYPE (arg),
6238                                                            c2)));
6239             }
6240         }
6241     }
6242
6243   return NULL_TREE;
6244 }
6245
6246 /* Subroutine of fold() that optimizes comparisons against Infinities,
6247    either +Inf or -Inf.
6248
6249    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6250    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6251    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6252
6253    The function returns the constant folded tree if a simplification
6254    can be made, and NULL_TREE otherwise.  */
6255
6256 static tree
6257 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6258 {
6259   enum machine_mode mode;
6260   REAL_VALUE_TYPE max;
6261   tree temp;
6262   bool neg;
6263
6264   mode = TYPE_MODE (TREE_TYPE (arg0));
6265
6266   /* For negative infinity swap the sense of the comparison.  */
6267   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6268   if (neg)
6269     code = swap_tree_comparison (code);
6270
6271   switch (code)
6272     {
6273     case GT_EXPR:
6274       /* x > +Inf is always false, if with ignore sNANs.  */
6275       if (HONOR_SNANS (mode))
6276         return NULL_TREE;
6277       return omit_one_operand (type, integer_zero_node, arg0);
6278
6279     case LE_EXPR:
6280       /* x <= +Inf is always true, if we don't case about NaNs.  */
6281       if (! HONOR_NANS (mode))
6282         return omit_one_operand (type, integer_one_node, arg0);
6283
6284       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6285       if (lang_hooks.decls.global_bindings_p () == 0
6286           && ! CONTAINS_PLACEHOLDER_P (arg0))
6287         {
6288           arg0 = save_expr (arg0);
6289           return fold_build2 (EQ_EXPR, type, arg0, arg0);
6290         }
6291       break;
6292
6293     case EQ_EXPR:
6294     case GE_EXPR:
6295       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6296       real_maxval (&max, neg, mode);
6297       return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6298                           arg0, build_real (TREE_TYPE (arg0), max));
6299
6300     case LT_EXPR:
6301       /* x < +Inf is always equal to x <= DBL_MAX.  */
6302       real_maxval (&max, neg, mode);
6303       return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6304                           arg0, build_real (TREE_TYPE (arg0), max));
6305
6306     case NE_EXPR:
6307       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6308       real_maxval (&max, neg, mode);
6309       if (! HONOR_NANS (mode))
6310         return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6311                             arg0, build_real (TREE_TYPE (arg0), max));
6312
6313       temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6314                           arg0, build_real (TREE_TYPE (arg0), max));
6315       return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6316
6317     default:
6318       break;
6319     }
6320
6321   return NULL_TREE;
6322 }
6323
6324 /* Subroutine of fold() that optimizes comparisons of a division by
6325    a nonzero integer constant against an integer constant, i.e.
6326    X/C1 op C2.
6327
6328    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6329    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6330    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6331
6332    The function returns the constant folded tree if a simplification
6333    can be made, and NULL_TREE otherwise.  */
6334
6335 static tree
6336 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6337 {
6338   tree prod, tmp, hi, lo;
6339   tree arg00 = TREE_OPERAND (arg0, 0);
6340   tree arg01 = TREE_OPERAND (arg0, 1);
6341   unsigned HOST_WIDE_INT lpart;
6342   HOST_WIDE_INT hpart;
6343   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6344   bool neg_overflow;
6345   int overflow;
6346
6347   /* We have to do this the hard way to detect unsigned overflow.
6348      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
6349   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6350                                    TREE_INT_CST_HIGH (arg01),
6351                                    TREE_INT_CST_LOW (arg1),
6352                                    TREE_INT_CST_HIGH (arg1),
6353                                    &lpart, &hpart, unsigned_p);
6354   prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6355                                 -1, overflow);
6356   neg_overflow = false;
6357
6358   if (unsigned_p)
6359     {
6360       tmp = int_const_binop (MINUS_EXPR, arg01,
6361                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6362       lo = prod;
6363
6364       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
6365       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6366                                        TREE_INT_CST_HIGH (prod),
6367                                        TREE_INT_CST_LOW (tmp),
6368                                        TREE_INT_CST_HIGH (tmp),
6369                                        &lpart, &hpart, unsigned_p);
6370       hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6371                                   -1, overflow | TREE_OVERFLOW (prod));
6372     }
6373   else if (tree_int_cst_sgn (arg01) >= 0)
6374     {
6375       tmp = int_const_binop (MINUS_EXPR, arg01,
6376                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6377       switch (tree_int_cst_sgn (arg1))
6378         {
6379         case -1:
6380           neg_overflow = true;
6381           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6382           hi = prod;
6383           break;
6384
6385         case  0:
6386           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6387           hi = tmp;
6388           break;
6389
6390         case  1:
6391           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6392           lo = prod;
6393           break;
6394
6395         default:
6396           gcc_unreachable ();
6397         }
6398     }
6399   else
6400     {
6401       /* A negative divisor reverses the relational operators.  */
6402       code = swap_tree_comparison (code);
6403
6404       tmp = int_const_binop (PLUS_EXPR, arg01,
6405                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6406       switch (tree_int_cst_sgn (arg1))
6407         {
6408         case -1:
6409           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6410           lo = prod;
6411           break;
6412
6413         case  0:
6414           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6415           lo = tmp;
6416           break;
6417
6418         case  1:
6419           neg_overflow = true;
6420           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6421           hi = prod;
6422           break;
6423
6424         default:
6425           gcc_unreachable ();
6426         }
6427     }
6428
6429   switch (code)
6430     {
6431     case EQ_EXPR:
6432       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6433         return omit_one_operand (type, integer_zero_node, arg00);
6434       if (TREE_OVERFLOW (hi))
6435         return fold_build2 (GE_EXPR, type, arg00, lo);
6436       if (TREE_OVERFLOW (lo))
6437         return fold_build2 (LE_EXPR, type, arg00, hi);
6438       return build_range_check (type, arg00, 1, lo, hi);
6439
6440     case NE_EXPR:
6441       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6442         return omit_one_operand (type, integer_one_node, arg00);
6443       if (TREE_OVERFLOW (hi))
6444         return fold_build2 (LT_EXPR, type, arg00, lo);
6445       if (TREE_OVERFLOW (lo))
6446         return fold_build2 (GT_EXPR, type, arg00, hi);
6447       return build_range_check (type, arg00, 0, lo, hi);
6448
6449     case LT_EXPR:
6450       if (TREE_OVERFLOW (lo))
6451         {
6452           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6453           return omit_one_operand (type, tmp, arg00);
6454         }
6455       return fold_build2 (LT_EXPR, type, arg00, lo);
6456
6457     case LE_EXPR:
6458       if (TREE_OVERFLOW (hi))
6459         {
6460           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6461           return omit_one_operand (type, tmp, arg00);
6462         }
6463       return fold_build2 (LE_EXPR, type, arg00, hi);
6464
6465     case GT_EXPR:
6466       if (TREE_OVERFLOW (hi))
6467         {
6468           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6469           return omit_one_operand (type, tmp, arg00);
6470         }
6471       return fold_build2 (GT_EXPR, type, arg00, hi);
6472
6473     case GE_EXPR:
6474       if (TREE_OVERFLOW (lo))
6475         {
6476           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6477           return omit_one_operand (type, tmp, arg00);
6478         }
6479       return fold_build2 (GE_EXPR, type, arg00, lo);
6480
6481     default:
6482       break;
6483     }
6484
6485   return NULL_TREE;
6486 }
6487
6488
6489 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6490    equality/inequality test, then return a simplified form of the test
6491    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6492    result type.  */
6493
6494 static tree
6495 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6496                                      tree result_type)
6497 {
6498   /* If this is testing a single bit, we can optimize the test.  */
6499   if ((code == NE_EXPR || code == EQ_EXPR)
6500       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6501       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6502     {
6503       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6504          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6505       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6506
6507       if (arg00 != NULL_TREE
6508           /* This is only a win if casting to a signed type is cheap,
6509              i.e. when arg00's type is not a partial mode.  */
6510           && TYPE_PRECISION (TREE_TYPE (arg00))
6511              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6512         {
6513           tree stype = signed_type_for (TREE_TYPE (arg00));
6514           return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6515                               result_type, fold_convert (stype, arg00),
6516                               build_int_cst (stype, 0));
6517         }
6518     }
6519
6520   return NULL_TREE;
6521 }
6522
6523 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6524    equality/inequality test, then return a simplified form of
6525    the test using shifts and logical operations.  Otherwise return
6526    NULL.  TYPE is the desired result type.  */
6527
6528 tree
6529 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6530                       tree result_type)
6531 {
6532   /* If this is testing a single bit, we can optimize the test.  */
6533   if ((code == NE_EXPR || code == EQ_EXPR)
6534       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6535       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6536     {
6537       tree inner = TREE_OPERAND (arg0, 0);
6538       tree type = TREE_TYPE (arg0);
6539       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6540       enum machine_mode operand_mode = TYPE_MODE (type);
6541       int ops_unsigned;
6542       tree signed_type, unsigned_type, intermediate_type;
6543       tree tem, one;
6544
6545       /* First, see if we can fold the single bit test into a sign-bit
6546          test.  */
6547       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6548                                                  result_type);
6549       if (tem)
6550         return tem;
6551
6552       /* Otherwise we have (A & C) != 0 where C is a single bit,
6553          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6554          Similarly for (A & C) == 0.  */
6555
6556       /* If INNER is a right shift of a constant and it plus BITNUM does
6557          not overflow, adjust BITNUM and INNER.  */
6558       if (TREE_CODE (inner) == RSHIFT_EXPR
6559           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6560           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6561           && bitnum < TYPE_PRECISION (type)
6562           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6563                                    bitnum - TYPE_PRECISION (type)))
6564         {
6565           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6566           inner = TREE_OPERAND (inner, 0);
6567         }
6568
6569       /* If we are going to be able to omit the AND below, we must do our
6570          operations as unsigned.  If we must use the AND, we have a choice.
6571          Normally unsigned is faster, but for some machines signed is.  */
6572 #ifdef LOAD_EXTEND_OP
6573       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND 
6574                       && !flag_syntax_only) ? 0 : 1;
6575 #else
6576       ops_unsigned = 1;
6577 #endif
6578
6579       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6580       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6581       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6582       inner = fold_convert (intermediate_type, inner);
6583
6584       if (bitnum != 0)
6585         inner = build2 (RSHIFT_EXPR, intermediate_type,
6586                         inner, size_int (bitnum));
6587
6588       one = build_int_cst (intermediate_type, 1);
6589
6590       if (code == EQ_EXPR)
6591         inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6592
6593       /* Put the AND last so it can combine with more things.  */
6594       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6595
6596       /* Make sure to return the proper type.  */
6597       inner = fold_convert (result_type, inner);
6598
6599       return inner;
6600     }
6601   return NULL_TREE;
6602 }
6603
6604 /* Check whether we are allowed to reorder operands arg0 and arg1,
6605    such that the evaluation of arg1 occurs before arg0.  */
6606
6607 static bool
6608 reorder_operands_p (const_tree arg0, const_tree arg1)
6609 {
6610   if (! flag_evaluation_order)
6611       return true;
6612   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6613     return true;
6614   return ! TREE_SIDE_EFFECTS (arg0)
6615          && ! TREE_SIDE_EFFECTS (arg1);
6616 }
6617
6618 /* Test whether it is preferable two swap two operands, ARG0 and
6619    ARG1, for example because ARG0 is an integer constant and ARG1
6620    isn't.  If REORDER is true, only recommend swapping if we can
6621    evaluate the operands in reverse order.  */
6622
6623 bool
6624 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6625 {
6626   STRIP_SIGN_NOPS (arg0);
6627   STRIP_SIGN_NOPS (arg1);
6628
6629   if (TREE_CODE (arg1) == INTEGER_CST)
6630     return 0;
6631   if (TREE_CODE (arg0) == INTEGER_CST)
6632     return 1;
6633
6634   if (TREE_CODE (arg1) == REAL_CST)
6635     return 0;
6636   if (TREE_CODE (arg0) == REAL_CST)
6637     return 1;
6638
6639   if (TREE_CODE (arg1) == FIXED_CST)
6640     return 0;
6641   if (TREE_CODE (arg0) == FIXED_CST)
6642     return 1;
6643
6644   if (TREE_CODE (arg1) == COMPLEX_CST)
6645     return 0;
6646   if (TREE_CODE (arg0) == COMPLEX_CST)
6647     return 1;
6648
6649   if (TREE_CONSTANT (arg1))
6650     return 0;
6651   if (TREE_CONSTANT (arg0))
6652     return 1;
6653
6654   if (optimize_size)
6655     return 0;
6656
6657   if (reorder && flag_evaluation_order
6658       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6659     return 0;
6660
6661   /* It is preferable to swap two SSA_NAME to ensure a canonical form
6662      for commutative and comparison operators.  Ensuring a canonical
6663      form allows the optimizers to find additional redundancies without
6664      having to explicitly check for both orderings.  */
6665   if (TREE_CODE (arg0) == SSA_NAME
6666       && TREE_CODE (arg1) == SSA_NAME
6667       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6668     return 1;
6669
6670   /* Put SSA_NAMEs last.  */
6671   if (TREE_CODE (arg1) == SSA_NAME)
6672     return 0;
6673   if (TREE_CODE (arg0) == SSA_NAME)
6674     return 1;
6675
6676   /* Put variables last.  */
6677   if (DECL_P (arg1))
6678     return 0;
6679   if (DECL_P (arg0))
6680     return 1;
6681
6682   return 0;
6683 }
6684
6685 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6686    ARG0 is extended to a wider type.  */
6687
6688 static tree
6689 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
6690 {
6691   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6692   tree arg1_unw;
6693   tree shorter_type, outer_type;
6694   tree min, max;
6695   bool above, below;
6696
6697   if (arg0_unw == arg0)
6698     return NULL_TREE;
6699   shorter_type = TREE_TYPE (arg0_unw);
6700
6701 #ifdef HAVE_canonicalize_funcptr_for_compare
6702   /* Disable this optimization if we're casting a function pointer
6703      type on targets that require function pointer canonicalization.  */
6704   if (HAVE_canonicalize_funcptr_for_compare
6705       && TREE_CODE (shorter_type) == POINTER_TYPE
6706       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6707     return NULL_TREE;
6708 #endif
6709
6710   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6711     return NULL_TREE;
6712
6713   arg1_unw = get_unwidened (arg1, NULL_TREE);
6714
6715   /* If possible, express the comparison in the shorter mode.  */
6716   if ((code == EQ_EXPR || code == NE_EXPR
6717        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6718       && (TREE_TYPE (arg1_unw) == shorter_type
6719           || (TYPE_PRECISION (shorter_type)
6720               >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6721           || (TREE_CODE (arg1_unw) == INTEGER_CST
6722               && (TREE_CODE (shorter_type) == INTEGER_TYPE
6723                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6724               && int_fits_type_p (arg1_unw, shorter_type))))
6725     return fold_build2 (code, type, arg0_unw,
6726                        fold_convert (shorter_type, arg1_unw));
6727
6728   if (TREE_CODE (arg1_unw) != INTEGER_CST
6729       || TREE_CODE (shorter_type) != INTEGER_TYPE
6730       || !int_fits_type_p (arg1_unw, shorter_type))
6731     return NULL_TREE;
6732
6733   /* If we are comparing with the integer that does not fit into the range
6734      of the shorter type, the result is known.  */
6735   outer_type = TREE_TYPE (arg1_unw);
6736   min = lower_bound_in_type (outer_type, shorter_type);
6737   max = upper_bound_in_type (outer_type, shorter_type);
6738
6739   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6740                                                    max, arg1_unw));
6741   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6742                                                    arg1_unw, min));
6743
6744   switch (code)
6745     {
6746     case EQ_EXPR:
6747       if (above || below)
6748         return omit_one_operand (type, integer_zero_node, arg0);
6749       break;
6750
6751     case NE_EXPR:
6752       if (above || below)
6753         return omit_one_operand (type, integer_one_node, arg0);
6754       break;
6755
6756     case LT_EXPR:
6757     case LE_EXPR:
6758       if (above)
6759         return omit_one_operand (type, integer_one_node, arg0);
6760       else if (below)
6761         return omit_one_operand (type, integer_zero_node, arg0);
6762
6763     case GT_EXPR:
6764     case GE_EXPR:
6765       if (above)
6766         return omit_one_operand (type, integer_zero_node, arg0);
6767       else if (below)
6768         return omit_one_operand (type, integer_one_node, arg0);
6769
6770     default:
6771       break;
6772     }
6773
6774   return NULL_TREE;
6775 }
6776
6777 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6778    ARG0 just the signedness is changed.  */
6779
6780 static tree
6781 fold_sign_changed_comparison (enum tree_code code, tree type,
6782                               tree arg0, tree arg1)
6783 {
6784   tree arg0_inner;
6785   tree inner_type, outer_type;
6786
6787   if (TREE_CODE (arg0) != NOP_EXPR
6788       && TREE_CODE (arg0) != CONVERT_EXPR)
6789     return NULL_TREE;
6790
6791   outer_type = TREE_TYPE (arg0);
6792   arg0_inner = TREE_OPERAND (arg0, 0);
6793   inner_type = TREE_TYPE (arg0_inner);
6794
6795 #ifdef HAVE_canonicalize_funcptr_for_compare
6796   /* Disable this optimization if we're casting a function pointer
6797      type on targets that require function pointer canonicalization.  */
6798   if (HAVE_canonicalize_funcptr_for_compare
6799       && TREE_CODE (inner_type) == POINTER_TYPE
6800       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6801     return NULL_TREE;
6802 #endif
6803
6804   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6805     return NULL_TREE;
6806
6807   if (TREE_CODE (arg1) != INTEGER_CST
6808       && !((TREE_CODE (arg1) == NOP_EXPR
6809             || TREE_CODE (arg1) == CONVERT_EXPR)
6810            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6811     return NULL_TREE;
6812
6813   if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6814       && code != NE_EXPR
6815       && code != EQ_EXPR)
6816     return NULL_TREE;
6817
6818   if (TREE_CODE (arg1) == INTEGER_CST)
6819     arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
6820                                   TREE_INT_CST_HIGH (arg1), 0,
6821                                   TREE_OVERFLOW (arg1));
6822   else
6823     arg1 = fold_convert (inner_type, arg1);
6824
6825   return fold_build2 (code, type, arg0_inner, arg1);
6826 }
6827
6828 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6829    step of the array.  Reconstructs s and delta in the case of s * delta
6830    being an integer constant (and thus already folded).
6831    ADDR is the address. MULT is the multiplicative expression.
6832    If the function succeeds, the new address expression is returned.  Otherwise
6833    NULL_TREE is returned.  */
6834
6835 static tree
6836 try_move_mult_to_index (tree addr, tree op1)
6837 {
6838   tree s, delta, step;
6839   tree ref = TREE_OPERAND (addr, 0), pref;
6840   tree ret, pos;
6841   tree itype;
6842   bool mdim = false;
6843
6844   /*  Strip the nops that might be added when converting op1 to sizetype. */
6845   STRIP_NOPS (op1);
6846
6847   /* Canonicalize op1 into a possibly non-constant delta
6848      and an INTEGER_CST s.  */
6849   if (TREE_CODE (op1) == MULT_EXPR)
6850     {
6851       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6852
6853       STRIP_NOPS (arg0);
6854       STRIP_NOPS (arg1);
6855   
6856       if (TREE_CODE (arg0) == INTEGER_CST)
6857         {
6858           s = arg0;
6859           delta = arg1;
6860         }
6861       else if (TREE_CODE (arg1) == INTEGER_CST)
6862         {
6863           s = arg1;
6864           delta = arg0;
6865         }
6866       else
6867         return NULL_TREE;
6868     }
6869   else if (TREE_CODE (op1) == INTEGER_CST)
6870     {
6871       delta = op1;
6872       s = NULL_TREE;
6873     }
6874   else
6875     {
6876       /* Simulate we are delta * 1.  */
6877       delta = op1;
6878       s = integer_one_node;
6879     }
6880
6881   for (;; ref = TREE_OPERAND (ref, 0))
6882     {
6883       if (TREE_CODE (ref) == ARRAY_REF)
6884         {
6885           /* Remember if this was a multi-dimensional array.  */
6886           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6887             mdim = true;
6888
6889           itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6890           if (! itype)
6891             continue;
6892
6893           step = array_ref_element_size (ref);
6894           if (TREE_CODE (step) != INTEGER_CST)
6895             continue;
6896
6897           if (s)
6898             {
6899               if (! tree_int_cst_equal (step, s))
6900                 continue;
6901             }
6902           else
6903             {
6904               /* Try if delta is a multiple of step.  */
6905               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, delta, step);
6906               if (! tmp)
6907                 continue;
6908               delta = tmp;
6909             }
6910
6911           /* Only fold here if we can verify we do not overflow one
6912              dimension of a multi-dimensional array.  */
6913           if (mdim)
6914             {
6915               tree tmp;
6916
6917               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6918                   || !INTEGRAL_TYPE_P (itype)
6919                   || !TYPE_MAX_VALUE (itype)
6920                   || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
6921                 continue;
6922
6923               tmp = fold_binary (PLUS_EXPR, itype,
6924                                  fold_convert (itype,
6925                                                TREE_OPERAND (ref, 1)),
6926                                  fold_convert (itype, delta));
6927               if (!tmp
6928                   || TREE_CODE (tmp) != INTEGER_CST
6929                   || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
6930                 continue;
6931             }
6932
6933           break;
6934         }
6935       else
6936         mdim = false;
6937
6938       if (!handled_component_p (ref))
6939         return NULL_TREE;
6940     }
6941
6942   /* We found the suitable array reference.  So copy everything up to it,
6943      and replace the index.  */
6944
6945   pref = TREE_OPERAND (addr, 0);
6946   ret = copy_node (pref);
6947   pos = ret;
6948
6949   while (pref != ref)
6950     {
6951       pref = TREE_OPERAND (pref, 0);
6952       TREE_OPERAND (pos, 0) = copy_node (pref);
6953       pos = TREE_OPERAND (pos, 0);
6954     }
6955
6956   TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
6957                                        fold_convert (itype,
6958                                                      TREE_OPERAND (pos, 1)),
6959                                        fold_convert (itype, delta));
6960
6961   return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
6962 }
6963
6964
6965 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
6966    means A >= Y && A != MAX, but in this case we know that
6967    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
6968
6969 static tree
6970 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
6971 {
6972   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6973
6974   if (TREE_CODE (bound) == LT_EXPR)
6975     a = TREE_OPERAND (bound, 0);
6976   else if (TREE_CODE (bound) == GT_EXPR)
6977     a = TREE_OPERAND (bound, 1);
6978   else
6979     return NULL_TREE;
6980
6981   typea = TREE_TYPE (a);
6982   if (!INTEGRAL_TYPE_P (typea)
6983       && !POINTER_TYPE_P (typea))
6984     return NULL_TREE;
6985
6986   if (TREE_CODE (ineq) == LT_EXPR)
6987     {
6988       a1 = TREE_OPERAND (ineq, 1);
6989       y = TREE_OPERAND (ineq, 0);
6990     }
6991   else if (TREE_CODE (ineq) == GT_EXPR)
6992     {
6993       a1 = TREE_OPERAND (ineq, 0);
6994       y = TREE_OPERAND (ineq, 1);
6995     }
6996   else
6997     return NULL_TREE;
6998
6999   if (TREE_TYPE (a1) != typea)
7000     return NULL_TREE;
7001
7002   if (POINTER_TYPE_P (typea))
7003     {
7004       /* Convert the pointer types into integer before taking the difference.  */
7005       tree ta = fold_convert (ssizetype, a);
7006       tree ta1 = fold_convert (ssizetype, a1);
7007       diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7008     }
7009   else
7010    diff = fold_binary (MINUS_EXPR, typea, a1, a);
7011
7012   if (!diff || !integer_onep (diff))
7013    return NULL_TREE;
7014
7015   return fold_build2 (GE_EXPR, type, a, y);
7016 }
7017
7018 /* Fold a sum or difference of at least one multiplication.
7019    Returns the folded tree or NULL if no simplification could be made.  */
7020
7021 static tree
7022 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7023 {
7024   tree arg00, arg01, arg10, arg11;
7025   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7026
7027   /* (A * C) +- (B * C) -> (A+-B) * C.
7028      (A * C) +- A -> A * (C+-1).
7029      We are most concerned about the case where C is a constant,
7030      but other combinations show up during loop reduction.  Since
7031      it is not difficult, try all four possibilities.  */
7032
7033   if (TREE_CODE (arg0) == MULT_EXPR)
7034     {
7035       arg00 = TREE_OPERAND (arg0, 0);
7036       arg01 = TREE_OPERAND (arg0, 1);
7037     }
7038   else if (TREE_CODE (arg0) == INTEGER_CST)
7039     {
7040       arg00 = build_one_cst (type);
7041       arg01 = arg0;
7042     }
7043   else
7044     {
7045       /* We cannot generate constant 1 for fract.  */
7046       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7047         return NULL_TREE;
7048       arg00 = arg0;
7049       arg01 = build_one_cst (type);
7050     }
7051   if (TREE_CODE (arg1) == MULT_EXPR)
7052     {
7053       arg10 = TREE_OPERAND (arg1, 0);
7054       arg11 = TREE_OPERAND (arg1, 1);
7055     }
7056   else if (TREE_CODE (arg1) == INTEGER_CST)
7057     {
7058       arg10 = build_one_cst (type);
7059       arg11 = arg1;
7060     }
7061   else
7062     {
7063       /* We cannot generate constant 1 for fract.  */
7064       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7065         return NULL_TREE;
7066       arg10 = arg1;
7067       arg11 = build_one_cst (type);
7068     }
7069   same = NULL_TREE;
7070
7071   if (operand_equal_p (arg01, arg11, 0))
7072     same = arg01, alt0 = arg00, alt1 = arg10;
7073   else if (operand_equal_p (arg00, arg10, 0))
7074     same = arg00, alt0 = arg01, alt1 = arg11;
7075   else if (operand_equal_p (arg00, arg11, 0))
7076     same = arg00, alt0 = arg01, alt1 = arg10;
7077   else if (operand_equal_p (arg01, arg10, 0))
7078     same = arg01, alt0 = arg00, alt1 = arg11;
7079
7080   /* No identical multiplicands; see if we can find a common
7081      power-of-two factor in non-power-of-two multiplies.  This
7082      can help in multi-dimensional array access.  */
7083   else if (host_integerp (arg01, 0)
7084            && host_integerp (arg11, 0))
7085     {
7086       HOST_WIDE_INT int01, int11, tmp;
7087       bool swap = false;
7088       tree maybe_same;
7089       int01 = TREE_INT_CST_LOW (arg01);
7090       int11 = TREE_INT_CST_LOW (arg11);
7091
7092       /* Move min of absolute values to int11.  */
7093       if ((int01 >= 0 ? int01 : -int01)
7094           < (int11 >= 0 ? int11 : -int11))
7095         {
7096           tmp = int01, int01 = int11, int11 = tmp;
7097           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7098           maybe_same = arg01;
7099           swap = true;
7100         }
7101       else
7102         maybe_same = arg11;
7103
7104       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
7105         {
7106           alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7107                               build_int_cst (TREE_TYPE (arg00),
7108                                              int01 / int11));
7109           alt1 = arg10;
7110           same = maybe_same;
7111           if (swap)
7112             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7113         }
7114     }
7115
7116   if (same)
7117     return fold_build2 (MULT_EXPR, type,
7118                         fold_build2 (code, type,
7119                                      fold_convert (type, alt0),
7120                                      fold_convert (type, alt1)),
7121                         fold_convert (type, same));
7122
7123   return NULL_TREE;
7124 }
7125
7126 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7127    specified by EXPR into the buffer PTR of length LEN bytes.
7128    Return the number of bytes placed in the buffer, or zero
7129    upon failure.  */
7130
7131 static int
7132 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7133 {
7134   tree type = TREE_TYPE (expr);
7135   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7136   int byte, offset, word, words;
7137   unsigned char value;
7138
7139   if (total_bytes > len)
7140     return 0;
7141   words = total_bytes / UNITS_PER_WORD;
7142
7143   for (byte = 0; byte < total_bytes; byte++)
7144     {
7145       int bitpos = byte * BITS_PER_UNIT;
7146       if (bitpos < HOST_BITS_PER_WIDE_INT)
7147         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7148       else
7149         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7150                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7151
7152       if (total_bytes > UNITS_PER_WORD)
7153         {
7154           word = byte / UNITS_PER_WORD;
7155           if (WORDS_BIG_ENDIAN)
7156             word = (words - 1) - word;
7157           offset = word * UNITS_PER_WORD;
7158           if (BYTES_BIG_ENDIAN)
7159             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7160           else
7161             offset += byte % UNITS_PER_WORD;
7162         }
7163       else
7164         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7165       ptr[offset] = value;
7166     }
7167   return total_bytes;
7168 }
7169
7170
7171 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7172    specified by EXPR into the buffer PTR of length LEN bytes.
7173    Return the number of bytes placed in the buffer, or zero
7174    upon failure.  */
7175
7176 static int
7177 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7178 {
7179   tree type = TREE_TYPE (expr);
7180   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7181   int byte, offset, word, words, bitpos;
7182   unsigned char value;
7183
7184   /* There are always 32 bits in each long, no matter the size of
7185      the hosts long.  We handle floating point representations with
7186      up to 192 bits.  */
7187   long tmp[6];
7188
7189   if (total_bytes > len)
7190     return 0;
7191   words = 32 / UNITS_PER_WORD;
7192
7193   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7194
7195   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7196        bitpos += BITS_PER_UNIT)
7197     {
7198       byte = (bitpos / BITS_PER_UNIT) & 3;
7199       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7200
7201       if (UNITS_PER_WORD < 4)
7202         {
7203           word = byte / UNITS_PER_WORD;
7204           if (WORDS_BIG_ENDIAN)
7205             word = (words - 1) - word;
7206           offset = word * UNITS_PER_WORD;
7207           if (BYTES_BIG_ENDIAN)
7208             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7209           else
7210             offset += byte % UNITS_PER_WORD;
7211         }
7212       else
7213         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7214       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7215     }
7216   return total_bytes;
7217 }
7218
7219 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7220    specified by EXPR into the buffer PTR of length LEN bytes.
7221    Return the number of bytes placed in the buffer, or zero
7222    upon failure.  */
7223
7224 static int
7225 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7226 {
7227   int rsize, isize;
7228   tree part;
7229
7230   part = TREE_REALPART (expr);
7231   rsize = native_encode_expr (part, ptr, len);
7232   if (rsize == 0)
7233     return 0;
7234   part = TREE_IMAGPART (expr);
7235   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7236   if (isize != rsize)
7237     return 0;
7238   return rsize + isize;
7239 }
7240
7241
7242 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7243    specified by EXPR into the buffer PTR of length LEN bytes.
7244    Return the number of bytes placed in the buffer, or zero
7245    upon failure.  */
7246
7247 static int
7248 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7249 {
7250   int i, size, offset, count;
7251   tree itype, elem, elements;
7252
7253   offset = 0;
7254   elements = TREE_VECTOR_CST_ELTS (expr);
7255   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7256   itype = TREE_TYPE (TREE_TYPE (expr));
7257   size = GET_MODE_SIZE (TYPE_MODE (itype));
7258   for (i = 0; i < count; i++)
7259     {
7260       if (elements)
7261         {
7262           elem = TREE_VALUE (elements);
7263           elements = TREE_CHAIN (elements);
7264         }
7265       else
7266         elem = NULL_TREE;
7267
7268       if (elem)
7269         {
7270           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7271             return 0;
7272         }
7273       else
7274         {
7275           if (offset + size > len)
7276             return 0;
7277           memset (ptr+offset, 0, size);
7278         }
7279       offset += size;
7280     }
7281   return offset;
7282 }
7283
7284
7285 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7286    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7287    buffer PTR of length LEN bytes.  Return the number of bytes
7288    placed in the buffer, or zero upon failure.  */
7289
7290 int
7291 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7292 {
7293   switch (TREE_CODE (expr))
7294     {
7295     case INTEGER_CST:
7296       return native_encode_int (expr, ptr, len);
7297
7298     case REAL_CST:
7299       return native_encode_real (expr, ptr, len);
7300
7301     case COMPLEX_CST:
7302       return native_encode_complex (expr, ptr, len);
7303
7304     case VECTOR_CST:
7305       return native_encode_vector (expr, ptr, len);
7306
7307     default:
7308       return 0;
7309     }
7310 }
7311
7312
7313 /* Subroutine of native_interpret_expr.  Interpret the contents of
7314    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7315    If the buffer cannot be interpreted, return NULL_TREE.  */
7316
7317 static tree
7318 native_interpret_int (tree type, const unsigned char *ptr, int len)
7319 {
7320   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7321   int byte, offset, word, words;
7322   unsigned char value;
7323   unsigned int HOST_WIDE_INT lo = 0;
7324   HOST_WIDE_INT hi = 0;
7325
7326   if (total_bytes > len)
7327     return NULL_TREE;
7328   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7329     return NULL_TREE;
7330   words = total_bytes / UNITS_PER_WORD;
7331
7332   for (byte = 0; byte < total_bytes; byte++)
7333     {
7334       int bitpos = byte * BITS_PER_UNIT;
7335       if (total_bytes > UNITS_PER_WORD)
7336         {
7337           word = byte / UNITS_PER_WORD;
7338           if (WORDS_BIG_ENDIAN)
7339             word = (words - 1) - word;
7340           offset = word * UNITS_PER_WORD;
7341           if (BYTES_BIG_ENDIAN)
7342             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7343           else
7344             offset += byte % UNITS_PER_WORD;
7345         }
7346       else
7347         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7348       value = ptr[offset];
7349
7350       if (bitpos < HOST_BITS_PER_WIDE_INT)
7351         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7352       else
7353         hi |= (unsigned HOST_WIDE_INT) value
7354               << (bitpos - HOST_BITS_PER_WIDE_INT);
7355     }
7356
7357   return build_int_cst_wide_type (type, lo, hi);
7358 }
7359
7360
7361 /* Subroutine of native_interpret_expr.  Interpret the contents of
7362    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7363    If the buffer cannot be interpreted, return NULL_TREE.  */
7364
7365 static tree
7366 native_interpret_real (tree type, const unsigned char *ptr, int len)
7367 {
7368   enum machine_mode mode = TYPE_MODE (type);
7369   int total_bytes = GET_MODE_SIZE (mode);
7370   int byte, offset, word, words, bitpos;
7371   unsigned char value;
7372   /* There are always 32 bits in each long, no matter the size of
7373      the hosts long.  We handle floating point representations with
7374      up to 192 bits.  */
7375   REAL_VALUE_TYPE r;
7376   long tmp[6];
7377
7378   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7379   if (total_bytes > len || total_bytes > 24)
7380     return NULL_TREE;
7381   words = 32 / UNITS_PER_WORD;
7382
7383   memset (tmp, 0, sizeof (tmp));
7384   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7385        bitpos += BITS_PER_UNIT)
7386     {
7387       byte = (bitpos / BITS_PER_UNIT) & 3;
7388       if (UNITS_PER_WORD < 4)
7389         {
7390           word = byte / UNITS_PER_WORD;
7391           if (WORDS_BIG_ENDIAN)
7392             word = (words - 1) - word;
7393           offset = word * UNITS_PER_WORD;
7394           if (BYTES_BIG_ENDIAN)
7395             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7396           else
7397             offset += byte % UNITS_PER_WORD;
7398         }
7399       else
7400         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7401       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7402
7403       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7404     }
7405
7406   real_from_target (&r, tmp, mode);
7407   return build_real (type, r);
7408 }
7409
7410
7411 /* Subroutine of native_interpret_expr.  Interpret the contents of
7412    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7413    If the buffer cannot be interpreted, return NULL_TREE.  */
7414
7415 static tree
7416 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7417 {
7418   tree etype, rpart, ipart;
7419   int size;
7420
7421   etype = TREE_TYPE (type);
7422   size = GET_MODE_SIZE (TYPE_MODE (etype));
7423   if (size * 2 > len)
7424     return NULL_TREE;
7425   rpart = native_interpret_expr (etype, ptr, size);
7426   if (!rpart)
7427     return NULL_TREE;
7428   ipart = native_interpret_expr (etype, ptr+size, size);
7429   if (!ipart)
7430     return NULL_TREE;
7431   return build_complex (type, rpart, ipart);
7432 }
7433
7434
7435 /* Subroutine of native_interpret_expr.  Interpret the contents of
7436    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7437    If the buffer cannot be interpreted, return NULL_TREE.  */
7438
7439 static tree
7440 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7441 {
7442   tree etype, elem, elements;
7443   int i, size, count;
7444
7445   etype = TREE_TYPE (type);
7446   size = GET_MODE_SIZE (TYPE_MODE (etype));
7447   count = TYPE_VECTOR_SUBPARTS (type);
7448   if (size * count > len)
7449     return NULL_TREE;
7450
7451   elements = NULL_TREE;
7452   for (i = count - 1; i >= 0; i--)
7453     {
7454       elem = native_interpret_expr (etype, ptr+(i*size), size);
7455       if (!elem)
7456         return NULL_TREE;
7457       elements = tree_cons (NULL_TREE, elem, elements);
7458     }
7459   return build_vector (type, elements);
7460 }
7461
7462
7463 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7464    the buffer PTR of length LEN as a constant of type TYPE.  For
7465    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7466    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7467    return NULL_TREE.  */
7468
7469 tree
7470 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7471 {
7472   switch (TREE_CODE (type))
7473     {
7474     case INTEGER_TYPE:
7475     case ENUMERAL_TYPE:
7476     case BOOLEAN_TYPE:
7477       return native_interpret_int (type, ptr, len);
7478
7479     case REAL_TYPE:
7480       return native_interpret_real (type, ptr, len);
7481
7482     case COMPLEX_TYPE:
7483       return native_interpret_complex (type, ptr, len);
7484
7485     case VECTOR_TYPE:
7486       return native_interpret_vector (type, ptr, len);
7487
7488     default:
7489       return NULL_TREE;
7490     }
7491 }
7492
7493
7494 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7495    TYPE at compile-time.  If we're unable to perform the conversion
7496    return NULL_TREE.  */
7497
7498 static tree
7499 fold_view_convert_expr (tree type, tree expr)
7500 {
7501   /* We support up to 512-bit values (for V8DFmode).  */
7502   unsigned char buffer[64];
7503   int len;
7504
7505   /* Check that the host and target are sane.  */
7506   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7507     return NULL_TREE;
7508
7509   len = native_encode_expr (expr, buffer, sizeof (buffer));
7510   if (len == 0)
7511     return NULL_TREE;
7512
7513   return native_interpret_expr (type, buffer, len);
7514 }
7515
7516 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7517    to avoid confusing the gimplify process.  When IN_FOLD is true
7518    avoid modifications of T.  */
7519
7520 static tree
7521 build_fold_addr_expr_with_type_1 (tree t, tree ptrtype, bool in_fold)
7522 {
7523   /* The size of the object is not relevant when talking about its address.  */
7524   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7525     t = TREE_OPERAND (t, 0);
7526
7527   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7528   if (TREE_CODE (t) == INDIRECT_REF
7529       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7530     {
7531       t = TREE_OPERAND (t, 0);
7532
7533       if (TREE_TYPE (t) != ptrtype)
7534         t = build1 (NOP_EXPR, ptrtype, t);
7535     }
7536   else if (!in_fold)
7537     {
7538       tree base = t;
7539
7540       while (handled_component_p (base))
7541         base = TREE_OPERAND (base, 0);
7542
7543       if (DECL_P (base))
7544         TREE_ADDRESSABLE (base) = 1;
7545
7546       t = build1 (ADDR_EXPR, ptrtype, t);
7547     }
7548   else
7549     t = build1 (ADDR_EXPR, ptrtype, t);
7550
7551   return t;
7552 }
7553
7554 /* Build an expression for the address of T with type PTRTYPE.  This
7555    function modifies the input parameter 'T' by sometimes setting the
7556    TREE_ADDRESSABLE flag.  */
7557
7558 tree
7559 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7560 {
7561   return build_fold_addr_expr_with_type_1 (t, ptrtype, false);
7562 }
7563
7564 /* Build an expression for the address of T.  This function modifies
7565    the input parameter 'T' by sometimes setting the TREE_ADDRESSABLE
7566    flag.  When called from fold functions, use fold_addr_expr instead.  */
7567
7568 tree
7569 build_fold_addr_expr (tree t)
7570 {
7571   return build_fold_addr_expr_with_type_1 (t, 
7572                                            build_pointer_type (TREE_TYPE (t)),
7573                                            false);
7574 }
7575
7576 /* Same as build_fold_addr_expr, builds an expression for the address
7577    of T, but avoids touching the input node 't'.  Fold functions
7578    should use this version.  */
7579
7580 static tree
7581 fold_addr_expr (tree t)
7582 {
7583   tree ptrtype = build_pointer_type (TREE_TYPE (t));
7584
7585   return build_fold_addr_expr_with_type_1 (t, ptrtype, true);
7586 }
7587
7588 /* Fold a unary expression of code CODE and type TYPE with operand
7589    OP0.  Return the folded expression if folding is successful.
7590    Otherwise, return NULL_TREE.  */
7591
7592 tree
7593 fold_unary (enum tree_code code, tree type, tree op0)
7594 {
7595   tree tem;
7596   tree arg0;
7597   enum tree_code_class kind = TREE_CODE_CLASS (code);
7598
7599   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7600               && TREE_CODE_LENGTH (code) == 1);
7601
7602   arg0 = op0;
7603   if (arg0)
7604     {
7605       if (code == NOP_EXPR || code == CONVERT_EXPR
7606           || code == FLOAT_EXPR || code == ABS_EXPR)
7607         {
7608           /* Don't use STRIP_NOPS, because signedness of argument type
7609              matters.  */
7610           STRIP_SIGN_NOPS (arg0);
7611         }
7612       else
7613         {
7614           /* Strip any conversions that don't change the mode.  This
7615              is safe for every expression, except for a comparison
7616              expression because its signedness is derived from its
7617              operands.
7618
7619              Note that this is done as an internal manipulation within
7620              the constant folder, in order to find the simplest
7621              representation of the arguments so that their form can be
7622              studied.  In any cases, the appropriate type conversions
7623              should be put back in the tree that will get out of the
7624              constant folder.  */
7625           STRIP_NOPS (arg0);
7626         }
7627     }
7628
7629   if (TREE_CODE_CLASS (code) == tcc_unary)
7630     {
7631       if (TREE_CODE (arg0) == COMPOUND_EXPR)
7632         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7633                        fold_build1 (code, type, TREE_OPERAND (arg0, 1)));
7634       else if (TREE_CODE (arg0) == COND_EXPR)
7635         {
7636           tree arg01 = TREE_OPERAND (arg0, 1);
7637           tree arg02 = TREE_OPERAND (arg0, 2);
7638           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7639             arg01 = fold_build1 (code, type, arg01);
7640           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7641             arg02 = fold_build1 (code, type, arg02);
7642           tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
7643                              arg01, arg02);
7644
7645           /* If this was a conversion, and all we did was to move into
7646              inside the COND_EXPR, bring it back out.  But leave it if
7647              it is a conversion from integer to integer and the
7648              result precision is no wider than a word since such a
7649              conversion is cheap and may be optimized away by combine,
7650              while it couldn't if it were outside the COND_EXPR.  Then return
7651              so we don't get into an infinite recursion loop taking the
7652              conversion out and then back in.  */
7653
7654           if ((code == NOP_EXPR || code == CONVERT_EXPR
7655                || code == NON_LVALUE_EXPR)
7656               && TREE_CODE (tem) == COND_EXPR
7657               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7658               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7659               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7660               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7661               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7662                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7663               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7664                      && (INTEGRAL_TYPE_P
7665                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7666                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7667                   || flag_syntax_only))
7668             tem = build1 (code, type,
7669                           build3 (COND_EXPR,
7670                                   TREE_TYPE (TREE_OPERAND
7671                                              (TREE_OPERAND (tem, 1), 0)),
7672                                   TREE_OPERAND (tem, 0),
7673                                   TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7674                                   TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
7675           return tem;
7676         }
7677       else if (COMPARISON_CLASS_P (arg0))
7678         {
7679           if (TREE_CODE (type) == BOOLEAN_TYPE)
7680             {
7681               arg0 = copy_node (arg0);
7682               TREE_TYPE (arg0) = type;
7683               return arg0;
7684             }
7685           else if (TREE_CODE (type) != INTEGER_TYPE)
7686             return fold_build3 (COND_EXPR, type, arg0,
7687                                 fold_build1 (code, type,
7688                                              integer_one_node),
7689                                 fold_build1 (code, type,
7690                                              integer_zero_node));
7691         }
7692    }
7693
7694   switch (code)
7695     {
7696     case PAREN_EXPR:
7697       /* Re-association barriers around constants and other re-association
7698          barriers can be removed.  */
7699       if (CONSTANT_CLASS_P (op0)
7700           || TREE_CODE (op0) == PAREN_EXPR)
7701         return fold_convert (type, op0);
7702       return NULL_TREE;
7703
7704     case NOP_EXPR:
7705     case FLOAT_EXPR:
7706     case CONVERT_EXPR:
7707     case FIX_TRUNC_EXPR:
7708       if (TREE_TYPE (op0) == type)
7709         return op0;
7710       
7711       /* If we have (type) (a CMP b) and type is an integral type, return
7712          new expression involving the new type.  */
7713       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7714         return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7715                             TREE_OPERAND (op0, 1));
7716
7717       /* Handle cases of two conversions in a row.  */
7718       if (TREE_CODE (op0) == NOP_EXPR
7719           || TREE_CODE (op0) == CONVERT_EXPR)
7720         {
7721           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7722           tree inter_type = TREE_TYPE (op0);
7723           int inside_int = INTEGRAL_TYPE_P (inside_type);
7724           int inside_ptr = POINTER_TYPE_P (inside_type);
7725           int inside_float = FLOAT_TYPE_P (inside_type);
7726           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7727           unsigned int inside_prec = TYPE_PRECISION (inside_type);
7728           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7729           int inter_int = INTEGRAL_TYPE_P (inter_type);
7730           int inter_ptr = POINTER_TYPE_P (inter_type);
7731           int inter_float = FLOAT_TYPE_P (inter_type);
7732           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7733           unsigned int inter_prec = TYPE_PRECISION (inter_type);
7734           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7735           int final_int = INTEGRAL_TYPE_P (type);
7736           int final_ptr = POINTER_TYPE_P (type);
7737           int final_float = FLOAT_TYPE_P (type);
7738           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7739           unsigned int final_prec = TYPE_PRECISION (type);
7740           int final_unsignedp = TYPE_UNSIGNED (type);
7741
7742           /* In addition to the cases of two conversions in a row
7743              handled below, if we are converting something to its own
7744              type via an object of identical or wider precision, neither
7745              conversion is needed.  */
7746           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7747               && (((inter_int || inter_ptr) && final_int)
7748                   || (inter_float && final_float))
7749               && inter_prec >= final_prec)
7750             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7751
7752           /* Likewise, if the intermediate and final types are either both
7753              float or both integer, we don't need the middle conversion if
7754              it is wider than the final type and doesn't change the signedness
7755              (for integers).  Avoid this if the final type is a pointer
7756              since then we sometimes need the inner conversion.  Likewise if
7757              the outer has a precision not equal to the size of its mode.  */
7758           if (((inter_int && inside_int)
7759                || (inter_float && inside_float)
7760                || (inter_vec && inside_vec))
7761               && inter_prec >= inside_prec
7762               && (inter_float || inter_vec
7763                   || inter_unsignedp == inside_unsignedp)
7764               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7765                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
7766               && ! final_ptr
7767               && (! final_vec || inter_prec == inside_prec))
7768             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7769
7770           /* If we have a sign-extension of a zero-extended value, we can
7771              replace that by a single zero-extension.  */
7772           if (inside_int && inter_int && final_int
7773               && inside_prec < inter_prec && inter_prec < final_prec
7774               && inside_unsignedp && !inter_unsignedp)
7775             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7776
7777           /* Two conversions in a row are not needed unless:
7778              - some conversion is floating-point (overstrict for now), or
7779              - some conversion is a vector (overstrict for now), or
7780              - the intermediate type is narrower than both initial and
7781                final, or
7782              - the intermediate type and innermost type differ in signedness,
7783                and the outermost type is wider than the intermediate, or
7784              - the initial type is a pointer type and the precisions of the
7785                intermediate and final types differ, or
7786              - the final type is a pointer type and the precisions of the
7787                initial and intermediate types differ.  */
7788           if (! inside_float && ! inter_float && ! final_float
7789               && ! inside_vec && ! inter_vec && ! final_vec
7790               && (inter_prec >= inside_prec || inter_prec >= final_prec)
7791               && ! (inside_int && inter_int
7792                     && inter_unsignedp != inside_unsignedp
7793                     && inter_prec < final_prec)
7794               && ((inter_unsignedp && inter_prec > inside_prec)
7795                   == (final_unsignedp && final_prec > inter_prec))
7796               && ! (inside_ptr && inter_prec != final_prec)
7797               && ! (final_ptr && inside_prec != inter_prec)
7798               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7799                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7800             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7801         }
7802
7803       /* Handle (T *)&A.B.C for A being of type T and B and C
7804          living at offset zero.  This occurs frequently in
7805          C++ upcasting and then accessing the base.  */
7806       if (TREE_CODE (op0) == ADDR_EXPR
7807           && POINTER_TYPE_P (type)
7808           && handled_component_p (TREE_OPERAND (op0, 0)))
7809         {
7810           HOST_WIDE_INT bitsize, bitpos;
7811           tree offset;
7812           enum machine_mode mode;
7813           int unsignedp, volatilep;
7814           tree base = TREE_OPERAND (op0, 0);
7815           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7816                                       &mode, &unsignedp, &volatilep, false);
7817           /* If the reference was to a (constant) zero offset, we can use
7818              the address of the base if it has the same base type
7819              as the result type.  */
7820           if (! offset && bitpos == 0
7821               && TYPE_MAIN_VARIANT (TREE_TYPE (type))
7822                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7823             return fold_convert (type, fold_addr_expr (base));
7824         }
7825
7826       if ((TREE_CODE (op0) == MODIFY_EXPR
7827            || TREE_CODE (op0) == GIMPLE_MODIFY_STMT)
7828           && TREE_CONSTANT (GENERIC_TREE_OPERAND (op0, 1))
7829           /* Detect assigning a bitfield.  */
7830           && !(TREE_CODE (GENERIC_TREE_OPERAND (op0, 0)) == COMPONENT_REF
7831                && DECL_BIT_FIELD
7832                (TREE_OPERAND (GENERIC_TREE_OPERAND (op0, 0), 1))))
7833         {
7834           /* Don't leave an assignment inside a conversion
7835              unless assigning a bitfield.  */
7836           tem = fold_build1 (code, type, GENERIC_TREE_OPERAND (op0, 1));
7837           /* First do the assignment, then return converted constant.  */
7838           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7839           TREE_NO_WARNING (tem) = 1;
7840           TREE_USED (tem) = 1;
7841           return tem;
7842         }
7843
7844       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7845          constants (if x has signed type, the sign bit cannot be set
7846          in c).  This folds extension into the BIT_AND_EXPR.  */
7847       if (INTEGRAL_TYPE_P (type)
7848           && TREE_CODE (type) != BOOLEAN_TYPE
7849           && TREE_CODE (op0) == BIT_AND_EXPR
7850           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7851         {
7852           tree and = op0;
7853           tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
7854           int change = 0;
7855
7856           if (TYPE_UNSIGNED (TREE_TYPE (and))
7857               || (TYPE_PRECISION (type)
7858                   <= TYPE_PRECISION (TREE_TYPE (and))))
7859             change = 1;
7860           else if (TYPE_PRECISION (TREE_TYPE (and1))
7861                    <= HOST_BITS_PER_WIDE_INT
7862                    && host_integerp (and1, 1))
7863             {
7864               unsigned HOST_WIDE_INT cst;
7865
7866               cst = tree_low_cst (and1, 1);
7867               cst &= (HOST_WIDE_INT) -1
7868                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7869               change = (cst == 0);
7870 #ifdef LOAD_EXTEND_OP
7871               if (change
7872                   && !flag_syntax_only
7873                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7874                       == ZERO_EXTEND))
7875                 {
7876                   tree uns = unsigned_type_for (TREE_TYPE (and0));
7877                   and0 = fold_convert (uns, and0);
7878                   and1 = fold_convert (uns, and1);
7879                 }
7880 #endif
7881             }
7882           if (change)
7883             {
7884               tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
7885                                            TREE_INT_CST_HIGH (and1), 0,
7886                                            TREE_OVERFLOW (and1));
7887               return fold_build2 (BIT_AND_EXPR, type,
7888                                   fold_convert (type, and0), tem);
7889             }
7890         }
7891
7892       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7893          when one of the new casts will fold away. Conservatively we assume
7894          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7895       if (POINTER_TYPE_P (type)
7896           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7897           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7898               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7899               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7900         {
7901           tree arg00 = TREE_OPERAND (arg0, 0);
7902           tree arg01 = TREE_OPERAND (arg0, 1);
7903
7904           return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
7905                               fold_convert (sizetype, arg01));
7906         }
7907
7908       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7909          of the same precision, and X is an integer type not narrower than
7910          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
7911       if (INTEGRAL_TYPE_P (type)
7912           && TREE_CODE (op0) == BIT_NOT_EXPR
7913           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7914           && (TREE_CODE (TREE_OPERAND (op0, 0)) == NOP_EXPR
7915               || TREE_CODE (TREE_OPERAND (op0, 0)) == CONVERT_EXPR)
7916           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7917         {
7918           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7919           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7920               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7921             return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
7922         }
7923
7924       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7925          type of X and Y (integer types only).  */
7926       if (INTEGRAL_TYPE_P (type)
7927           && TREE_CODE (op0) == MULT_EXPR
7928           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7929           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7930         {
7931           /* Be careful not to introduce new overflows.  */
7932           tree mult_type;
7933           if (TYPE_OVERFLOW_WRAPS (type))
7934             mult_type = type;
7935           else
7936             mult_type = unsigned_type_for (type);
7937           
7938           tem = fold_build2 (MULT_EXPR, mult_type,
7939                              fold_convert (mult_type, TREE_OPERAND (op0, 0)),
7940                              fold_convert (mult_type, TREE_OPERAND (op0, 1)));
7941           return fold_convert (type, tem);
7942         }
7943
7944       tem = fold_convert_const (code, type, op0);
7945       return tem ? tem : NULL_TREE;
7946
7947     case FIXED_CONVERT_EXPR:
7948       tem = fold_convert_const (code, type, arg0);
7949       return tem ? tem : NULL_TREE;
7950
7951     case VIEW_CONVERT_EXPR:
7952       if (TREE_TYPE (op0) == type)
7953         return op0;
7954       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
7955         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
7956
7957       /* For integral conversions with the same precision or pointer
7958          conversions use a NOP_EXPR instead.  */
7959       if ((INTEGRAL_TYPE_P (type)
7960            || POINTER_TYPE_P (type))
7961           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7962               || POINTER_TYPE_P (TREE_TYPE (op0)))
7963           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))
7964           /* Do not muck with VIEW_CONVERT_EXPRs that convert from
7965              a sub-type to its base type as generated by the Ada FE.  */
7966           && !(INTEGRAL_TYPE_P (TREE_TYPE (op0))
7967                && TREE_TYPE (TREE_TYPE (op0))))
7968         return fold_convert (type, op0);
7969
7970       /* Strip inner integral conversions that do not change the precision.  */
7971       if ((TREE_CODE (op0) == NOP_EXPR
7972            || TREE_CODE (op0) == CONVERT_EXPR)
7973           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7974               || POINTER_TYPE_P (TREE_TYPE (op0)))
7975           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
7976               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
7977           && (TYPE_PRECISION (TREE_TYPE (op0))
7978               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
7979         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
7980
7981       return fold_view_convert_expr (type, op0);
7982
7983     case NEGATE_EXPR:
7984       tem = fold_negate_expr (arg0);
7985       if (tem)
7986         return fold_convert (type, tem);
7987       return NULL_TREE;
7988
7989     case ABS_EXPR:
7990       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
7991         return fold_abs_const (arg0, type);
7992       else if (TREE_CODE (arg0) == NEGATE_EXPR)
7993         return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
7994       /* Convert fabs((double)float) into (double)fabsf(float).  */
7995       else if (TREE_CODE (arg0) == NOP_EXPR
7996                && TREE_CODE (type) == REAL_TYPE)
7997         {
7998           tree targ0 = strip_float_extensions (arg0);
7999           if (targ0 != arg0)
8000             return fold_convert (type, fold_build1 (ABS_EXPR,
8001                                                     TREE_TYPE (targ0),
8002                                                     targ0));
8003         }
8004       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8005       else if (TREE_CODE (arg0) == ABS_EXPR)
8006         return arg0;
8007       else if (tree_expr_nonnegative_p (arg0))
8008         return arg0;
8009
8010       /* Strip sign ops from argument.  */
8011       if (TREE_CODE (type) == REAL_TYPE)
8012         {
8013           tem = fold_strip_sign_ops (arg0);
8014           if (tem)
8015             return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8016         }
8017       return NULL_TREE;
8018
8019     case CONJ_EXPR:
8020       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8021         return fold_convert (type, arg0);
8022       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8023         {
8024           tree itype = TREE_TYPE (type);
8025           tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8026           tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8027           return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8028         }
8029       if (TREE_CODE (arg0) == COMPLEX_CST)
8030         {
8031           tree itype = TREE_TYPE (type);
8032           tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8033           tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8034           return build_complex (type, rpart, negate_expr (ipart));
8035         }
8036       if (TREE_CODE (arg0) == CONJ_EXPR)
8037         return fold_convert (type, TREE_OPERAND (arg0, 0));
8038       return NULL_TREE;
8039
8040     case BIT_NOT_EXPR:
8041       if (TREE_CODE (arg0) == INTEGER_CST)
8042         return fold_not_const (arg0, type);
8043       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8044         return fold_convert (type, TREE_OPERAND (arg0, 0));
8045       /* Convert ~ (-A) to A - 1.  */
8046       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8047         return fold_build2 (MINUS_EXPR, type,
8048                             fold_convert (type, TREE_OPERAND (arg0, 0)),
8049                             build_int_cst (type, 1));
8050       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8051       else if (INTEGRAL_TYPE_P (type)
8052                && ((TREE_CODE (arg0) == MINUS_EXPR
8053                     && integer_onep (TREE_OPERAND (arg0, 1)))
8054                    || (TREE_CODE (arg0) == PLUS_EXPR
8055                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8056         return fold_build1 (NEGATE_EXPR, type,
8057                             fold_convert (type, TREE_OPERAND (arg0, 0)));
8058       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8059       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8060                && (tem = fold_unary (BIT_NOT_EXPR, type,
8061                                      fold_convert (type,
8062                                                    TREE_OPERAND (arg0, 0)))))
8063         return fold_build2 (BIT_XOR_EXPR, type, tem,
8064                             fold_convert (type, TREE_OPERAND (arg0, 1)));
8065       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8066                && (tem = fold_unary (BIT_NOT_EXPR, type,
8067                                      fold_convert (type,
8068                                                    TREE_OPERAND (arg0, 1)))))
8069         return fold_build2 (BIT_XOR_EXPR, type,
8070                             fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8071       /* Perform BIT_NOT_EXPR on each element individually.  */
8072       else if (TREE_CODE (arg0) == VECTOR_CST)
8073         {
8074           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8075           int count = TYPE_VECTOR_SUBPARTS (type), i;
8076
8077           for (i = 0; i < count; i++)
8078             {
8079               if (elements)
8080                 {
8081                   elem = TREE_VALUE (elements);
8082                   elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8083                   if (elem == NULL_TREE)
8084                     break;
8085                   elements = TREE_CHAIN (elements);
8086                 }
8087               else
8088                 elem = build_int_cst (TREE_TYPE (type), -1);
8089               list = tree_cons (NULL_TREE, elem, list);
8090             }
8091           if (i == count)
8092             return build_vector (type, nreverse (list));
8093         }
8094
8095       return NULL_TREE;
8096
8097     case TRUTH_NOT_EXPR:
8098       /* The argument to invert_truthvalue must have Boolean type.  */
8099       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8100           arg0 = fold_convert (boolean_type_node, arg0);
8101
8102       /* Note that the operand of this must be an int
8103          and its values must be 0 or 1.
8104          ("true" is a fixed value perhaps depending on the language,
8105          but we don't handle values other than 1 correctly yet.)  */
8106       tem = fold_truth_not_expr (arg0);
8107       if (!tem)
8108         return NULL_TREE;
8109       return fold_convert (type, tem);
8110
8111     case REALPART_EXPR:
8112       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8113         return fold_convert (type, arg0);
8114       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8115         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8116                                  TREE_OPERAND (arg0, 1));
8117       if (TREE_CODE (arg0) == COMPLEX_CST)
8118         return fold_convert (type, TREE_REALPART (arg0));
8119       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8120         {
8121           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8122           tem = fold_build2 (TREE_CODE (arg0), itype,
8123                              fold_build1 (REALPART_EXPR, itype,
8124                                           TREE_OPERAND (arg0, 0)),
8125                              fold_build1 (REALPART_EXPR, itype,
8126                                           TREE_OPERAND (arg0, 1)));
8127           return fold_convert (type, tem);
8128         }
8129       if (TREE_CODE (arg0) == CONJ_EXPR)
8130         {
8131           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8132           tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8133           return fold_convert (type, tem);
8134         }
8135       if (TREE_CODE (arg0) == CALL_EXPR)
8136         {
8137           tree fn = get_callee_fndecl (arg0);
8138           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8139             switch (DECL_FUNCTION_CODE (fn))
8140               {
8141               CASE_FLT_FN (BUILT_IN_CEXPI):
8142                 fn = mathfn_built_in (type, BUILT_IN_COS);
8143                 if (fn)
8144                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8145                 break;
8146
8147               default:
8148                 break;
8149               }
8150         }
8151       return NULL_TREE;
8152
8153     case IMAGPART_EXPR:
8154       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8155         return fold_convert (type, integer_zero_node);
8156       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8157         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8158                                  TREE_OPERAND (arg0, 0));
8159       if (TREE_CODE (arg0) == COMPLEX_CST)
8160         return fold_convert (type, TREE_IMAGPART (arg0));
8161       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8162         {
8163           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8164           tem = fold_build2 (TREE_CODE (arg0), itype,
8165                              fold_build1 (IMAGPART_EXPR, itype,
8166                                           TREE_OPERAND (arg0, 0)),
8167                              fold_build1 (IMAGPART_EXPR, itype,
8168                                           TREE_OPERAND (arg0, 1)));
8169           return fold_convert (type, tem);
8170         }
8171       if (TREE_CODE (arg0) == CONJ_EXPR)
8172         {
8173           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8174           tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8175           return fold_convert (type, negate_expr (tem));
8176         }
8177       if (TREE_CODE (arg0) == CALL_EXPR)
8178         {
8179           tree fn = get_callee_fndecl (arg0);
8180           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8181             switch (DECL_FUNCTION_CODE (fn))
8182               {
8183               CASE_FLT_FN (BUILT_IN_CEXPI):
8184                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8185                 if (fn)
8186                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8187                 break;
8188
8189               default:
8190                 break;
8191               }
8192         }
8193       return NULL_TREE;
8194
8195     default:
8196       return NULL_TREE;
8197     } /* switch (code) */
8198 }
8199
8200 /* Fold a binary expression of code CODE and type TYPE with operands
8201    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8202    Return the folded expression if folding is successful.  Otherwise,
8203    return NULL_TREE.  */
8204
8205 static tree
8206 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8207 {
8208   enum tree_code compl_code;
8209
8210   if (code == MIN_EXPR)
8211     compl_code = MAX_EXPR;
8212   else if (code == MAX_EXPR)
8213     compl_code = MIN_EXPR;
8214   else
8215     gcc_unreachable ();
8216
8217   /* MIN (MAX (a, b), b) == b.  */
8218   if (TREE_CODE (op0) == compl_code
8219       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8220     return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8221
8222   /* MIN (MAX (b, a), b) == b.  */
8223   if (TREE_CODE (op0) == compl_code
8224       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8225       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8226     return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8227
8228   /* MIN (a, MAX (a, b)) == a.  */
8229   if (TREE_CODE (op1) == compl_code
8230       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8231       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8232     return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8233
8234   /* MIN (a, MAX (b, a)) == a.  */
8235   if (TREE_CODE (op1) == compl_code
8236       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8237       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8238     return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8239
8240   return NULL_TREE;
8241 }
8242
8243 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8244    by changing CODE to reduce the magnitude of constants involved in
8245    ARG0 of the comparison.
8246    Returns a canonicalized comparison tree if a simplification was
8247    possible, otherwise returns NULL_TREE.
8248    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8249    valid if signed overflow is undefined.  */
8250
8251 static tree
8252 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8253                                  tree arg0, tree arg1,
8254                                  bool *strict_overflow_p)
8255 {
8256   enum tree_code code0 = TREE_CODE (arg0);
8257   tree t, cst0 = NULL_TREE;
8258   int sgn0;
8259   bool swap = false;
8260
8261   /* Match A +- CST code arg1 and CST code arg1.  */
8262   if (!(((code0 == MINUS_EXPR
8263           || code0 == PLUS_EXPR)
8264          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8265         || code0 == INTEGER_CST))
8266     return NULL_TREE;
8267
8268   /* Identify the constant in arg0 and its sign.  */
8269   if (code0 == INTEGER_CST)
8270     cst0 = arg0;
8271   else
8272     cst0 = TREE_OPERAND (arg0, 1);
8273   sgn0 = tree_int_cst_sgn (cst0);
8274
8275   /* Overflowed constants and zero will cause problems.  */
8276   if (integer_zerop (cst0)
8277       || TREE_OVERFLOW (cst0))
8278     return NULL_TREE;
8279
8280   /* See if we can reduce the magnitude of the constant in
8281      arg0 by changing the comparison code.  */
8282   if (code0 == INTEGER_CST)
8283     {
8284       /* CST <= arg1  ->  CST-1 < arg1.  */
8285       if (code == LE_EXPR && sgn0 == 1)
8286         code = LT_EXPR;
8287       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8288       else if (code == LT_EXPR && sgn0 == -1)
8289         code = LE_EXPR;
8290       /* CST > arg1  ->  CST-1 >= arg1.  */
8291       else if (code == GT_EXPR && sgn0 == 1)
8292         code = GE_EXPR;
8293       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8294       else if (code == GE_EXPR && sgn0 == -1)
8295         code = GT_EXPR;
8296       else
8297         return NULL_TREE;
8298       /* arg1 code' CST' might be more canonical.  */
8299       swap = true;
8300     }
8301   else
8302     {
8303       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8304       if (code == LT_EXPR
8305           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8306         code = LE_EXPR;
8307       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8308       else if (code == GT_EXPR
8309                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8310         code = GE_EXPR;
8311       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8312       else if (code == LE_EXPR
8313                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8314         code = LT_EXPR;
8315       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8316       else if (code == GE_EXPR
8317                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8318         code = GT_EXPR;
8319       else
8320         return NULL_TREE;
8321       *strict_overflow_p = true;
8322     }
8323
8324   /* Now build the constant reduced in magnitude.  */
8325   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8326                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8327   if (code0 != INTEGER_CST)
8328     t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8329
8330   /* If swapping might yield to a more canonical form, do so.  */
8331   if (swap)
8332     return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8333   else
8334     return fold_build2 (code, type, t, arg1);
8335 }
8336
8337 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8338    overflow further.  Try to decrease the magnitude of constants involved
8339    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8340    and put sole constants at the second argument position.
8341    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8342
8343 static tree
8344 maybe_canonicalize_comparison (enum tree_code code, tree type,
8345                                tree arg0, tree arg1)
8346 {
8347   tree t;
8348   bool strict_overflow_p;
8349   const char * const warnmsg = G_("assuming signed overflow does not occur "
8350                                   "when reducing constant in comparison");
8351
8352   /* In principle pointers also have undefined overflow behavior,
8353      but that causes problems elsewhere.  */
8354   if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8355       || POINTER_TYPE_P (TREE_TYPE (arg0)))
8356     return NULL_TREE;
8357
8358   /* Try canonicalization by simplifying arg0.  */
8359   strict_overflow_p = false;
8360   t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8361                                        &strict_overflow_p);
8362   if (t)
8363     {
8364       if (strict_overflow_p)
8365         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8366       return t;
8367     }
8368
8369   /* Try canonicalization by simplifying arg1 using the swapped
8370      comparison.  */
8371   code = swap_tree_comparison (code);
8372   strict_overflow_p = false;
8373   t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8374                                        &strict_overflow_p);
8375   if (t && strict_overflow_p)
8376     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8377   return t;
8378 }
8379
8380 /* Subroutine of fold_binary.  This routine performs all of the
8381    transformations that are common to the equality/inequality
8382    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8383    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8384    fold_binary should call fold_binary.  Fold a comparison with
8385    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8386    the folded comparison or NULL_TREE.  */
8387
8388 static tree
8389 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8390 {
8391   tree arg0, arg1, tem;
8392
8393   arg0 = op0;
8394   arg1 = op1;
8395
8396   STRIP_SIGN_NOPS (arg0);
8397   STRIP_SIGN_NOPS (arg1);
8398
8399   tem = fold_relational_const (code, type, arg0, arg1);
8400   if (tem != NULL_TREE)
8401     return tem;
8402
8403   /* If one arg is a real or integer constant, put it last.  */
8404   if (tree_swap_operands_p (arg0, arg1, true))
8405     return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8406
8407   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8408   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8409       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8410           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8411           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8412       && (TREE_CODE (arg1) == INTEGER_CST
8413           && !TREE_OVERFLOW (arg1)))
8414     {
8415       tree const1 = TREE_OPERAND (arg0, 1);
8416       tree const2 = arg1;
8417       tree variable = TREE_OPERAND (arg0, 0);
8418       tree lhs;
8419       int lhs_add;
8420       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8421
8422       lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8423                          TREE_TYPE (arg1), const2, const1);
8424
8425       /* If the constant operation overflowed this can be
8426          simplified as a comparison against INT_MAX/INT_MIN.  */
8427       if (TREE_CODE (lhs) == INTEGER_CST
8428           && TREE_OVERFLOW (lhs))
8429         {
8430           int const1_sgn = tree_int_cst_sgn (const1);
8431           enum tree_code code2 = code;
8432
8433           /* Get the sign of the constant on the lhs if the
8434              operation were VARIABLE + CONST1.  */
8435           if (TREE_CODE (arg0) == MINUS_EXPR)
8436             const1_sgn = -const1_sgn;
8437
8438           /* The sign of the constant determines if we overflowed
8439              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8440              Canonicalize to the INT_MIN overflow by swapping the comparison
8441              if necessary.  */
8442           if (const1_sgn == -1)
8443             code2 = swap_tree_comparison (code);
8444
8445           /* We now can look at the canonicalized case
8446                VARIABLE + 1  CODE2  INT_MIN
8447              and decide on the result.  */
8448           if (code2 == LT_EXPR
8449               || code2 == LE_EXPR
8450               || code2 == EQ_EXPR)
8451             return omit_one_operand (type, boolean_false_node, variable);
8452           else if (code2 == NE_EXPR
8453                    || code2 == GE_EXPR
8454                    || code2 == GT_EXPR)
8455             return omit_one_operand (type, boolean_true_node, variable);
8456         }
8457
8458       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8459           && (TREE_CODE (lhs) != INTEGER_CST
8460               || !TREE_OVERFLOW (lhs)))
8461         {
8462           fold_overflow_warning (("assuming signed overflow does not occur "
8463                                   "when changing X +- C1 cmp C2 to "
8464                                   "X cmp C1 +- C2"),
8465                                  WARN_STRICT_OVERFLOW_COMPARISON);
8466           return fold_build2 (code, type, variable, lhs);
8467         }
8468     }
8469
8470   /* For comparisons of pointers we can decompose it to a compile time
8471      comparison of the base objects and the offsets into the object.
8472      This requires at least one operand being an ADDR_EXPR or a
8473      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
8474   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8475       && (TREE_CODE (arg0) == ADDR_EXPR
8476           || TREE_CODE (arg1) == ADDR_EXPR
8477           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8478           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8479     {
8480       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8481       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8482       enum machine_mode mode;
8483       int volatilep, unsignedp;
8484       bool indirect_base0 = false;
8485
8486       /* Get base and offset for the access.  Strip ADDR_EXPR for
8487          get_inner_reference, but put it back by stripping INDIRECT_REF
8488          off the base object if possible.  */
8489       base0 = arg0;
8490       if (TREE_CODE (arg0) == ADDR_EXPR)
8491         {
8492           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8493                                        &bitsize, &bitpos0, &offset0, &mode,
8494                                        &unsignedp, &volatilep, false);
8495           if (TREE_CODE (base0) == INDIRECT_REF)
8496             base0 = TREE_OPERAND (base0, 0);
8497           else
8498             indirect_base0 = true;
8499         }
8500       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8501         {
8502           base0 = TREE_OPERAND (arg0, 0);
8503           offset0 = TREE_OPERAND (arg0, 1);
8504         }
8505
8506       base1 = arg1;
8507       if (TREE_CODE (arg1) == ADDR_EXPR)
8508         {
8509           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8510                                        &bitsize, &bitpos1, &offset1, &mode,
8511                                        &unsignedp, &volatilep, false);
8512           /* We have to make sure to have an indirect/non-indirect base1
8513              just the same as we did for base0.  */
8514           if (TREE_CODE (base1) == INDIRECT_REF
8515               && !indirect_base0)
8516             base1 = TREE_OPERAND (base1, 0);
8517           else if (!indirect_base0)
8518             base1 = NULL_TREE;
8519         }
8520       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8521         {
8522           base1 = TREE_OPERAND (arg1, 0);
8523           offset1 = TREE_OPERAND (arg1, 1);
8524         }
8525       else if (indirect_base0)
8526         base1 = NULL_TREE;
8527
8528       /* If we have equivalent bases we might be able to simplify.  */
8529       if (base0 && base1
8530           && operand_equal_p (base0, base1, 0))
8531         {
8532           /* We can fold this expression to a constant if the non-constant
8533              offset parts are equal.  */
8534           if (offset0 == offset1
8535               || (offset0 && offset1
8536                   && operand_equal_p (offset0, offset1, 0)))
8537             {
8538               switch (code)
8539                 {
8540                 case EQ_EXPR:
8541                   return build_int_cst (boolean_type_node, bitpos0 == bitpos1);
8542                 case NE_EXPR:
8543                   return build_int_cst (boolean_type_node, bitpos0 != bitpos1);
8544                 case LT_EXPR:
8545                   return build_int_cst (boolean_type_node, bitpos0 < bitpos1);
8546                 case LE_EXPR:
8547                   return build_int_cst (boolean_type_node, bitpos0 <= bitpos1);
8548                 case GE_EXPR:
8549                   return build_int_cst (boolean_type_node, bitpos0 >= bitpos1);
8550                 case GT_EXPR:
8551                   return build_int_cst (boolean_type_node, bitpos0 > bitpos1);
8552                 default:;
8553                 }
8554             }
8555           /* We can simplify the comparison to a comparison of the variable
8556              offset parts if the constant offset parts are equal.
8557              Be careful to use signed size type here because otherwise we
8558              mess with array offsets in the wrong way.  This is possible
8559              because pointer arithmetic is restricted to retain within an
8560              object and overflow on pointer differences is undefined as of
8561              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
8562           else if (bitpos0 == bitpos1)
8563             {
8564               tree signed_size_type_node;
8565               signed_size_type_node = signed_type_for (size_type_node);
8566
8567               /* By converting to signed size type we cover middle-end pointer
8568                  arithmetic which operates on unsigned pointer types of size
8569                  type size and ARRAY_REF offsets which are properly sign or
8570                  zero extended from their type in case it is narrower than
8571                  size type.  */
8572               if (offset0 == NULL_TREE)
8573                 offset0 = build_int_cst (signed_size_type_node, 0);
8574               else
8575                 offset0 = fold_convert (signed_size_type_node, offset0);
8576               if (offset1 == NULL_TREE)
8577                 offset1 = build_int_cst (signed_size_type_node, 0);
8578               else
8579                 offset1 = fold_convert (signed_size_type_node, offset1);
8580
8581               return fold_build2 (code, type, offset0, offset1);
8582             }
8583         }
8584     }
8585
8586   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8587      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
8588      the resulting offset is smaller in absolute value than the
8589      original one.  */
8590   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8591       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8592       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8593           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8594       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8595       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8596           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8597     {
8598       tree const1 = TREE_OPERAND (arg0, 1);
8599       tree const2 = TREE_OPERAND (arg1, 1);
8600       tree variable1 = TREE_OPERAND (arg0, 0);
8601       tree variable2 = TREE_OPERAND (arg1, 0);
8602       tree cst;
8603       const char * const warnmsg = G_("assuming signed overflow does not "
8604                                       "occur when combining constants around "
8605                                       "a comparison");
8606
8607       /* Put the constant on the side where it doesn't overflow and is
8608          of lower absolute value than before.  */
8609       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8610                              ? MINUS_EXPR : PLUS_EXPR,
8611                              const2, const1, 0);
8612       if (!TREE_OVERFLOW (cst)
8613           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8614         {
8615           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8616           return fold_build2 (code, type,
8617                               variable1,
8618                               fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
8619                                            variable2, cst));
8620         }
8621
8622       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8623                              ? MINUS_EXPR : PLUS_EXPR,
8624                              const1, const2, 0);
8625       if (!TREE_OVERFLOW (cst)
8626           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8627         {
8628           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8629           return fold_build2 (code, type,
8630                               fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
8631                                            variable1, cst),
8632                               variable2);
8633         }
8634     }
8635
8636   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8637      signed arithmetic case.  That form is created by the compiler
8638      often enough for folding it to be of value.  One example is in
8639      computing loop trip counts after Operator Strength Reduction.  */
8640   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8641       && TREE_CODE (arg0) == MULT_EXPR
8642       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8643           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8644       && integer_zerop (arg1))
8645     {
8646       tree const1 = TREE_OPERAND (arg0, 1);
8647       tree const2 = arg1;                       /* zero */
8648       tree variable1 = TREE_OPERAND (arg0, 0);
8649       enum tree_code cmp_code = code;
8650
8651       gcc_assert (!integer_zerop (const1));
8652
8653       fold_overflow_warning (("assuming signed overflow does not occur when "
8654                               "eliminating multiplication in comparison "
8655                               "with zero"),
8656                              WARN_STRICT_OVERFLOW_COMPARISON);
8657
8658       /* If const1 is negative we swap the sense of the comparison.  */
8659       if (tree_int_cst_sgn (const1) < 0)
8660         cmp_code = swap_tree_comparison (cmp_code);
8661
8662       return fold_build2 (cmp_code, type, variable1, const2);
8663     }
8664
8665   tem = maybe_canonicalize_comparison (code, type, op0, op1);
8666   if (tem)
8667     return tem;
8668
8669   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8670     {
8671       tree targ0 = strip_float_extensions (arg0);
8672       tree targ1 = strip_float_extensions (arg1);
8673       tree newtype = TREE_TYPE (targ0);
8674
8675       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8676         newtype = TREE_TYPE (targ1);
8677
8678       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
8679       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8680         return fold_build2 (code, type, fold_convert (newtype, targ0),
8681                             fold_convert (newtype, targ1));
8682
8683       /* (-a) CMP (-b) -> b CMP a  */
8684       if (TREE_CODE (arg0) == NEGATE_EXPR
8685           && TREE_CODE (arg1) == NEGATE_EXPR)
8686         return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
8687                             TREE_OPERAND (arg0, 0));
8688
8689       if (TREE_CODE (arg1) == REAL_CST)
8690         {
8691           REAL_VALUE_TYPE cst;
8692           cst = TREE_REAL_CST (arg1);
8693
8694           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
8695           if (TREE_CODE (arg0) == NEGATE_EXPR)
8696             return fold_build2 (swap_tree_comparison (code), type,
8697                                 TREE_OPERAND (arg0, 0),
8698                                 build_real (TREE_TYPE (arg1),
8699                                             REAL_VALUE_NEGATE (cst)));
8700
8701           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
8702           /* a CMP (-0) -> a CMP 0  */
8703           if (REAL_VALUE_MINUS_ZERO (cst))
8704             return fold_build2 (code, type, arg0,
8705                                 build_real (TREE_TYPE (arg1), dconst0));
8706
8707           /* x != NaN is always true, other ops are always false.  */
8708           if (REAL_VALUE_ISNAN (cst)
8709               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8710             {
8711               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8712               return omit_one_operand (type, tem, arg0);
8713             }
8714
8715           /* Fold comparisons against infinity.  */
8716           if (REAL_VALUE_ISINF (cst))
8717             {
8718               tem = fold_inf_compare (code, type, arg0, arg1);
8719               if (tem != NULL_TREE)
8720                 return tem;
8721             }
8722         }
8723
8724       /* If this is a comparison of a real constant with a PLUS_EXPR
8725          or a MINUS_EXPR of a real constant, we can convert it into a
8726          comparison with a revised real constant as long as no overflow
8727          occurs when unsafe_math_optimizations are enabled.  */
8728       if (flag_unsafe_math_optimizations
8729           && TREE_CODE (arg1) == REAL_CST
8730           && (TREE_CODE (arg0) == PLUS_EXPR
8731               || TREE_CODE (arg0) == MINUS_EXPR)
8732           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
8733           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8734                                       ? MINUS_EXPR : PLUS_EXPR,
8735                                       arg1, TREE_OPERAND (arg0, 1), 0))
8736           && !TREE_OVERFLOW (tem))
8737         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
8738
8739       /* Likewise, we can simplify a comparison of a real constant with
8740          a MINUS_EXPR whose first operand is also a real constant, i.e.
8741          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on 
8742          floating-point types only if -fassociative-math is set.  */
8743       if (flag_associative_math
8744           && TREE_CODE (arg1) == REAL_CST
8745           && TREE_CODE (arg0) == MINUS_EXPR
8746           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
8747           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
8748                                       arg1, 0))
8749           && !TREE_OVERFLOW (tem))
8750         return fold_build2 (swap_tree_comparison (code), type,
8751                             TREE_OPERAND (arg0, 1), tem);
8752
8753       /* Fold comparisons against built-in math functions.  */
8754       if (TREE_CODE (arg1) == REAL_CST
8755           && flag_unsafe_math_optimizations
8756           && ! flag_errno_math)
8757         {
8758           enum built_in_function fcode = builtin_mathfn_code (arg0);
8759
8760           if (fcode != END_BUILTINS)
8761             {
8762               tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
8763               if (tem != NULL_TREE)
8764                 return tem;
8765             }
8766         }
8767     }
8768
8769   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
8770       && (TREE_CODE (arg0) == NOP_EXPR
8771           || TREE_CODE (arg0) == CONVERT_EXPR))
8772     {
8773       /* If we are widening one operand of an integer comparison,
8774          see if the other operand is similarly being widened.  Perhaps we
8775          can do the comparison in the narrower type.  */
8776       tem = fold_widened_comparison (code, type, arg0, arg1);
8777       if (tem)
8778         return tem;
8779
8780       /* Or if we are changing signedness.  */
8781       tem = fold_sign_changed_comparison (code, type, arg0, arg1);
8782       if (tem)
8783         return tem;
8784     }
8785
8786   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8787      constant, we can simplify it.  */
8788   if (TREE_CODE (arg1) == INTEGER_CST
8789       && (TREE_CODE (arg0) == MIN_EXPR
8790           || TREE_CODE (arg0) == MAX_EXPR)
8791       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8792     {
8793       tem = optimize_minmax_comparison (code, type, op0, op1);
8794       if (tem)
8795         return tem;
8796     }
8797
8798   /* Simplify comparison of something with itself.  (For IEEE
8799      floating-point, we can only do some of these simplifications.)  */
8800   if (operand_equal_p (arg0, arg1, 0))
8801     {
8802       switch (code)
8803         {
8804         case EQ_EXPR:
8805           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8806               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8807             return constant_boolean_node (1, type);
8808           break;
8809
8810         case GE_EXPR:
8811         case LE_EXPR:
8812           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8813               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8814             return constant_boolean_node (1, type);
8815           return fold_build2 (EQ_EXPR, type, arg0, arg1);
8816
8817         case NE_EXPR:
8818           /* For NE, we can only do this simplification if integer
8819              or we don't honor IEEE floating point NaNs.  */
8820           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
8821               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8822             break;
8823           /* ... fall through ...  */
8824         case GT_EXPR:
8825         case LT_EXPR:
8826           return constant_boolean_node (0, type);
8827         default:
8828           gcc_unreachable ();
8829         }
8830     }
8831
8832   /* If we are comparing an expression that just has comparisons
8833      of two integer values, arithmetic expressions of those comparisons,
8834      and constants, we can simplify it.  There are only three cases
8835      to check: the two values can either be equal, the first can be
8836      greater, or the second can be greater.  Fold the expression for
8837      those three values.  Since each value must be 0 or 1, we have
8838      eight possibilities, each of which corresponds to the constant 0
8839      or 1 or one of the six possible comparisons.
8840
8841      This handles common cases like (a > b) == 0 but also handles
8842      expressions like  ((x > y) - (y > x)) > 0, which supposedly
8843      occur in macroized code.  */
8844
8845   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8846     {
8847       tree cval1 = 0, cval2 = 0;
8848       int save_p = 0;
8849
8850       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8851           /* Don't handle degenerate cases here; they should already
8852              have been handled anyway.  */
8853           && cval1 != 0 && cval2 != 0
8854           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8855           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8856           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8857           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8858           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8859           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8860                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8861         {
8862           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8863           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8864
8865           /* We can't just pass T to eval_subst in case cval1 or cval2
8866              was the same as ARG1.  */
8867
8868           tree high_result
8869                 = fold_build2 (code, type,
8870                                eval_subst (arg0, cval1, maxval,
8871                                            cval2, minval),
8872                                arg1);
8873           tree equal_result
8874                 = fold_build2 (code, type,
8875                                eval_subst (arg0, cval1, maxval,
8876                                            cval2, maxval),
8877                                arg1);
8878           tree low_result
8879                 = fold_build2 (code, type,
8880                                eval_subst (arg0, cval1, minval,
8881                                            cval2, maxval),
8882                                arg1);
8883
8884           /* All three of these results should be 0 or 1.  Confirm they are.
8885              Then use those values to select the proper code to use.  */
8886
8887           if (TREE_CODE (high_result) == INTEGER_CST
8888               && TREE_CODE (equal_result) == INTEGER_CST
8889               && TREE_CODE (low_result) == INTEGER_CST)
8890             {
8891               /* Make a 3-bit mask with the high-order bit being the
8892                  value for `>', the next for '=', and the low for '<'.  */
8893               switch ((integer_onep (high_result) * 4)
8894                       + (integer_onep (equal_result) * 2)
8895                       + integer_onep (low_result))
8896                 {
8897                 case 0:
8898                   /* Always false.  */
8899                   return omit_one_operand (type, integer_zero_node, arg0);
8900                 case 1:
8901                   code = LT_EXPR;
8902                   break;
8903                 case 2:
8904                   code = EQ_EXPR;
8905                   break;
8906                 case 3:
8907                   code = LE_EXPR;
8908                   break;
8909                 case 4:
8910                   code = GT_EXPR;
8911                   break;
8912                 case 5:
8913                   code = NE_EXPR;
8914                   break;
8915                 case 6:
8916                   code = GE_EXPR;
8917                   break;
8918                 case 7:
8919                   /* Always true.  */
8920                   return omit_one_operand (type, integer_one_node, arg0);
8921                 }
8922
8923               if (save_p)
8924                 return save_expr (build2 (code, type, cval1, cval2));
8925               return fold_build2 (code, type, cval1, cval2);
8926             }
8927         }
8928     }
8929
8930   /* Fold a comparison of the address of COMPONENT_REFs with the same
8931      type and component to a comparison of the address of the base
8932      object.  In short, &x->a OP &y->a to x OP y and
8933      &x->a OP &y.a to x OP &y  */
8934   if (TREE_CODE (arg0) == ADDR_EXPR
8935       && TREE_CODE (TREE_OPERAND (arg0, 0)) == COMPONENT_REF
8936       && TREE_CODE (arg1) == ADDR_EXPR
8937       && TREE_CODE (TREE_OPERAND (arg1, 0)) == COMPONENT_REF)
8938     {
8939       tree cref0 = TREE_OPERAND (arg0, 0);
8940       tree cref1 = TREE_OPERAND (arg1, 0);
8941       if (TREE_OPERAND (cref0, 1) == TREE_OPERAND (cref1, 1))
8942         {
8943           tree op0 = TREE_OPERAND (cref0, 0);
8944           tree op1 = TREE_OPERAND (cref1, 0);
8945           return fold_build2 (code, type,
8946                               fold_addr_expr (op0),
8947                               fold_addr_expr (op1));
8948         }
8949     }
8950
8951   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8952      into a single range test.  */
8953   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8954        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8955       && TREE_CODE (arg1) == INTEGER_CST
8956       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8957       && !integer_zerop (TREE_OPERAND (arg0, 1))
8958       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8959       && !TREE_OVERFLOW (arg1))
8960     {
8961       tem = fold_div_compare (code, type, arg0, arg1);
8962       if (tem != NULL_TREE)
8963         return tem;
8964     }
8965
8966   /* Fold ~X op ~Y as Y op X.  */
8967   if (TREE_CODE (arg0) == BIT_NOT_EXPR
8968       && TREE_CODE (arg1) == BIT_NOT_EXPR)
8969     {
8970       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
8971       return fold_build2 (code, type,
8972                           fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
8973                           TREE_OPERAND (arg0, 0));
8974     }
8975
8976   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
8977   if (TREE_CODE (arg0) == BIT_NOT_EXPR
8978       && TREE_CODE (arg1) == INTEGER_CST)
8979     {
8980       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
8981       return fold_build2 (swap_tree_comparison (code), type,
8982                           TREE_OPERAND (arg0, 0),
8983                           fold_build1 (BIT_NOT_EXPR, cmp_type,
8984                                        fold_convert (cmp_type, arg1)));
8985     }
8986
8987   return NULL_TREE;
8988 }
8989
8990
8991 /* Subroutine of fold_binary.  Optimize complex multiplications of the
8992    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
8993    argument EXPR represents the expression "z" of type TYPE.  */
8994
8995 static tree
8996 fold_mult_zconjz (tree type, tree expr)
8997 {
8998   tree itype = TREE_TYPE (type);
8999   tree rpart, ipart, tem;
9000
9001   if (TREE_CODE (expr) == COMPLEX_EXPR)
9002     {
9003       rpart = TREE_OPERAND (expr, 0);
9004       ipart = TREE_OPERAND (expr, 1);
9005     }
9006   else if (TREE_CODE (expr) == COMPLEX_CST)
9007     {
9008       rpart = TREE_REALPART (expr);
9009       ipart = TREE_IMAGPART (expr);
9010     }
9011   else
9012     {
9013       expr = save_expr (expr);
9014       rpart = fold_build1 (REALPART_EXPR, itype, expr);
9015       ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9016     }
9017
9018   rpart = save_expr (rpart);
9019   ipart = save_expr (ipart);
9020   tem = fold_build2 (PLUS_EXPR, itype,
9021                      fold_build2 (MULT_EXPR, itype, rpart, rpart),
9022                      fold_build2 (MULT_EXPR, itype, ipart, ipart));
9023   return fold_build2 (COMPLEX_EXPR, type, tem,
9024                       fold_convert (itype, integer_zero_node));
9025 }
9026
9027
9028 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9029    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9030    guarantees that P and N have the same least significant log2(M) bits.
9031    N is not otherwise constrained.  In particular, N is not normalized to
9032    0 <= N < M as is common.  In general, the precise value of P is unknown.
9033    M is chosen as large as possible such that constant N can be determined.
9034
9035    Returns M and sets *RESIDUE to N.  */
9036
9037 static unsigned HOST_WIDE_INT
9038 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue)
9039 {
9040   enum tree_code code;
9041
9042   *residue = 0;
9043
9044   code = TREE_CODE (expr);
9045   if (code == ADDR_EXPR)
9046     {
9047       expr = TREE_OPERAND (expr, 0);
9048       if (handled_component_p (expr))
9049         {
9050           HOST_WIDE_INT bitsize, bitpos;
9051           tree offset;
9052           enum machine_mode mode;
9053           int unsignedp, volatilep;
9054
9055           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9056                                       &mode, &unsignedp, &volatilep, false);
9057           *residue = bitpos / BITS_PER_UNIT;
9058           if (offset)
9059             {
9060               if (TREE_CODE (offset) == INTEGER_CST)
9061                 *residue += TREE_INT_CST_LOW (offset);
9062               else
9063                 /* We don't handle more complicated offset expressions.  */
9064                 return 1;
9065             }
9066         }
9067
9068       if (DECL_P (expr))
9069         return DECL_ALIGN_UNIT (expr);
9070     }
9071   else if (code == POINTER_PLUS_EXPR)
9072     {
9073       tree op0, op1;
9074       unsigned HOST_WIDE_INT modulus;
9075       enum tree_code inner_code;
9076       
9077       op0 = TREE_OPERAND (expr, 0);
9078       STRIP_NOPS (op0);
9079       modulus = get_pointer_modulus_and_residue (op0, residue);
9080
9081       op1 = TREE_OPERAND (expr, 1);
9082       STRIP_NOPS (op1);
9083       inner_code = TREE_CODE (op1);
9084       if (inner_code == INTEGER_CST)
9085         {
9086           *residue += TREE_INT_CST_LOW (op1);
9087           return modulus;
9088         }
9089       else if (inner_code == MULT_EXPR)
9090         {
9091           op1 = TREE_OPERAND (op1, 1);
9092           if (TREE_CODE (op1) == INTEGER_CST)
9093             {
9094               unsigned HOST_WIDE_INT align;
9095               
9096               /* Compute the greatest power-of-2 divisor of op1.  */
9097               align = TREE_INT_CST_LOW (op1);
9098               align &= -align;
9099
9100               /* If align is non-zero and less than *modulus, replace
9101                  *modulus with align., If align is 0, then either op1 is 0
9102                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9103                  unsigned HOST_WIDE_INT.  In either case, no additional
9104                  constraint is imposed.  */
9105               if (align)
9106                 modulus = MIN (modulus, align);
9107
9108               return modulus;
9109             }
9110         }
9111     }
9112
9113     /* If we get here, we were unable to determine anything useful about the
9114        expression.  */
9115     return 1;
9116 }
9117
9118
9119 /* Fold a binary expression of code CODE and type TYPE with operands
9120    OP0 and OP1.  Return the folded expression if folding is
9121    successful.  Otherwise, return NULL_TREE.  */
9122
9123 tree
9124 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9125 {
9126   enum tree_code_class kind = TREE_CODE_CLASS (code);
9127   tree arg0, arg1, tem;
9128   tree t1 = NULL_TREE;
9129   bool strict_overflow_p;
9130
9131   gcc_assert ((IS_EXPR_CODE_CLASS (kind)
9132                || IS_GIMPLE_STMT_CODE_CLASS (kind))
9133               && TREE_CODE_LENGTH (code) == 2
9134               && op0 != NULL_TREE
9135               && op1 != NULL_TREE);
9136
9137   arg0 = op0;
9138   arg1 = op1;
9139
9140   /* Strip any conversions that don't change the mode.  This is
9141      safe for every expression, except for a comparison expression
9142      because its signedness is derived from its operands.  So, in
9143      the latter case, only strip conversions that don't change the
9144      signedness.
9145
9146      Note that this is done as an internal manipulation within the
9147      constant folder, in order to find the simplest representation
9148      of the arguments so that their form can be studied.  In any
9149      cases, the appropriate type conversions should be put back in
9150      the tree that will get out of the constant folder.  */
9151
9152   if (kind == tcc_comparison)
9153     {
9154       STRIP_SIGN_NOPS (arg0);
9155       STRIP_SIGN_NOPS (arg1);
9156     }
9157   else
9158     {
9159       STRIP_NOPS (arg0);
9160       STRIP_NOPS (arg1);
9161     }
9162
9163   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9164      constant but we can't do arithmetic on them.  */
9165   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9166       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9167       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9168       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9169       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9170       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9171     {
9172       if (kind == tcc_binary)
9173         {
9174           /* Make sure type and arg0 have the same saturating flag.  */
9175           gcc_assert (TYPE_SATURATING (type)
9176                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9177           tem = const_binop (code, arg0, arg1, 0);
9178         }
9179       else if (kind == tcc_comparison)
9180         tem = fold_relational_const (code, type, arg0, arg1);
9181       else
9182         tem = NULL_TREE;
9183
9184       if (tem != NULL_TREE)
9185         {
9186           if (TREE_TYPE (tem) != type)
9187             tem = fold_convert (type, tem);
9188           return tem;
9189         }
9190     }
9191
9192   /* If this is a commutative operation, and ARG0 is a constant, move it
9193      to ARG1 to reduce the number of tests below.  */
9194   if (commutative_tree_code (code)
9195       && tree_swap_operands_p (arg0, arg1, true))
9196     return fold_build2 (code, type, op1, op0);
9197
9198   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9199
9200      First check for cases where an arithmetic operation is applied to a
9201      compound, conditional, or comparison operation.  Push the arithmetic
9202      operation inside the compound or conditional to see if any folding
9203      can then be done.  Convert comparison to conditional for this purpose.
9204      The also optimizes non-constant cases that used to be done in
9205      expand_expr.
9206
9207      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9208      one of the operands is a comparison and the other is a comparison, a
9209      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9210      code below would make the expression more complex.  Change it to a
9211      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9212      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9213
9214   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9215        || code == EQ_EXPR || code == NE_EXPR)
9216       && ((truth_value_p (TREE_CODE (arg0))
9217            && (truth_value_p (TREE_CODE (arg1))
9218                || (TREE_CODE (arg1) == BIT_AND_EXPR
9219                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9220           || (truth_value_p (TREE_CODE (arg1))
9221               && (truth_value_p (TREE_CODE (arg0))
9222                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9223                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9224     {
9225       tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9226                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9227                          : TRUTH_XOR_EXPR,
9228                          boolean_type_node,
9229                          fold_convert (boolean_type_node, arg0),
9230                          fold_convert (boolean_type_node, arg1));
9231
9232       if (code == EQ_EXPR)
9233         tem = invert_truthvalue (tem);
9234
9235       return fold_convert (type, tem);
9236     }
9237
9238   if (TREE_CODE_CLASS (code) == tcc_binary
9239       || TREE_CODE_CLASS (code) == tcc_comparison)
9240     {
9241       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9242         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9243                        fold_build2 (code, type,
9244                                     fold_convert (TREE_TYPE (op0),
9245                                                   TREE_OPERAND (arg0, 1)),
9246                                     op1));
9247       if (TREE_CODE (arg1) == COMPOUND_EXPR
9248           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9249         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9250                        fold_build2 (code, type, op0,
9251                                     fold_convert (TREE_TYPE (op1),
9252                                                   TREE_OPERAND (arg1, 1))));
9253
9254       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9255         {
9256           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9257                                                      arg0, arg1, 
9258                                                      /*cond_first_p=*/1);
9259           if (tem != NULL_TREE)
9260             return tem;
9261         }
9262
9263       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9264         {
9265           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9266                                                      arg1, arg0, 
9267                                                      /*cond_first_p=*/0);
9268           if (tem != NULL_TREE)
9269             return tem;
9270         }
9271     }
9272
9273   switch (code)
9274     {
9275     case POINTER_PLUS_EXPR:
9276       /* 0 +p index -> (type)index */
9277       if (integer_zerop (arg0))
9278         return non_lvalue (fold_convert (type, arg1));
9279
9280       /* PTR +p 0 -> PTR */
9281       if (integer_zerop (arg1))
9282         return non_lvalue (fold_convert (type, arg0));
9283
9284       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9285       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9286            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9287         return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9288                                                 fold_convert (sizetype, arg1),
9289                                                 fold_convert (sizetype, arg0)));
9290
9291       /* index +p PTR -> PTR +p index */
9292       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9293           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9294         return fold_build2 (POINTER_PLUS_EXPR, type,
9295                             fold_convert (type, arg1),
9296                             fold_convert (sizetype, arg0));
9297
9298       /* (PTR +p B) +p A -> PTR +p (B + A) */
9299       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9300         {
9301           tree inner;
9302           tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9303           tree arg00 = TREE_OPERAND (arg0, 0);
9304           inner = fold_build2 (PLUS_EXPR, sizetype,
9305                                arg01, fold_convert (sizetype, arg1));
9306           return fold_convert (type,
9307                                fold_build2 (POINTER_PLUS_EXPR,
9308                                             TREE_TYPE (arg00), arg00, inner));
9309         }
9310
9311       /* PTR_CST +p CST -> CST1 */
9312       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9313         return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9314
9315      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9316         of the array.  Loop optimizer sometimes produce this type of
9317         expressions.  */
9318       if (TREE_CODE (arg0) == ADDR_EXPR)
9319         {
9320           tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9321           if (tem)
9322             return fold_convert (type, tem);
9323         }
9324
9325       return NULL_TREE;
9326
9327     case PLUS_EXPR:
9328       /* PTR + INT -> (INT)(PTR p+ INT) */
9329       if (POINTER_TYPE_P (TREE_TYPE (arg0))
9330           && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
9331         return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9332                                                 TREE_TYPE (arg0),
9333                                                 arg0,
9334                                                 fold_convert (sizetype, arg1)));
9335       /* INT + PTR -> (INT)(PTR p+ INT) */
9336       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9337           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9338         return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9339                                                 TREE_TYPE (arg1),
9340                                                 arg1,
9341                                                 fold_convert (sizetype, arg0)));
9342       /* A + (-B) -> A - B */
9343       if (TREE_CODE (arg1) == NEGATE_EXPR)
9344         return fold_build2 (MINUS_EXPR, type,
9345                             fold_convert (type, arg0),
9346                             fold_convert (type, TREE_OPERAND (arg1, 0)));
9347       /* (-A) + B -> B - A */
9348       if (TREE_CODE (arg0) == NEGATE_EXPR
9349           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9350         return fold_build2 (MINUS_EXPR, type,
9351                             fold_convert (type, arg1),
9352                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9353
9354       if (INTEGRAL_TYPE_P (type))
9355         {
9356           /* Convert ~A + 1 to -A.  */
9357           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9358               && integer_onep (arg1))
9359             return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
9360
9361           /* ~X + X is -1.  */
9362           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9363               && !TYPE_OVERFLOW_TRAPS (type))
9364             {
9365               tree tem = TREE_OPERAND (arg0, 0);
9366
9367               STRIP_NOPS (tem);
9368               if (operand_equal_p (tem, arg1, 0))
9369                 {
9370                   t1 = build_int_cst_type (type, -1);
9371                   return omit_one_operand (type, t1, arg1);
9372                 }
9373             }
9374
9375           /* X + ~X is -1.  */
9376           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9377               && !TYPE_OVERFLOW_TRAPS (type))
9378             {
9379               tree tem = TREE_OPERAND (arg1, 0);
9380
9381               STRIP_NOPS (tem);
9382               if (operand_equal_p (arg0, tem, 0))
9383                 {
9384                   t1 = build_int_cst_type (type, -1);
9385                   return omit_one_operand (type, t1, arg0);
9386                 }
9387             }
9388
9389           /* X + (X / CST) * -CST is X % CST.  */
9390           if (TREE_CODE (arg1) == MULT_EXPR
9391               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9392               && operand_equal_p (arg0,
9393                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9394             {
9395               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9396               tree cst1 = TREE_OPERAND (arg1, 1);
9397               tree sum = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1, cst0);
9398               if (sum && integer_zerop (sum))
9399                 return fold_convert (type,
9400                                      fold_build2 (TRUNC_MOD_EXPR,
9401                                                   TREE_TYPE (arg0), arg0, cst0));
9402             }
9403         }
9404
9405       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9406          same or one.  Make sure type is not saturating.
9407          fold_plusminus_mult_expr will re-associate.  */
9408       if ((TREE_CODE (arg0) == MULT_EXPR
9409            || TREE_CODE (arg1) == MULT_EXPR)
9410           && !TYPE_SATURATING (type)
9411           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9412         {
9413           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9414           if (tem)
9415             return tem;
9416         }
9417
9418       if (! FLOAT_TYPE_P (type))
9419         {
9420           if (integer_zerop (arg1))
9421             return non_lvalue (fold_convert (type, arg0));
9422
9423           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9424              with a constant, and the two constants have no bits in common,
9425              we should treat this as a BIT_IOR_EXPR since this may produce more
9426              simplifications.  */
9427           if (TREE_CODE (arg0) == BIT_AND_EXPR
9428               && TREE_CODE (arg1) == BIT_AND_EXPR
9429               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9430               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9431               && integer_zerop (const_binop (BIT_AND_EXPR,
9432                                              TREE_OPERAND (arg0, 1),
9433                                              TREE_OPERAND (arg1, 1), 0)))
9434             {
9435               code = BIT_IOR_EXPR;
9436               goto bit_ior;
9437             }
9438
9439           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9440              (plus (plus (mult) (mult)) (foo)) so that we can
9441              take advantage of the factoring cases below.  */
9442           if (((TREE_CODE (arg0) == PLUS_EXPR
9443                 || TREE_CODE (arg0) == MINUS_EXPR)
9444                && TREE_CODE (arg1) == MULT_EXPR)
9445               || ((TREE_CODE (arg1) == PLUS_EXPR
9446                    || TREE_CODE (arg1) == MINUS_EXPR)
9447                   && TREE_CODE (arg0) == MULT_EXPR))
9448             {
9449               tree parg0, parg1, parg, marg;
9450               enum tree_code pcode;
9451
9452               if (TREE_CODE (arg1) == MULT_EXPR)
9453                 parg = arg0, marg = arg1;
9454               else
9455                 parg = arg1, marg = arg0;
9456               pcode = TREE_CODE (parg);
9457               parg0 = TREE_OPERAND (parg, 0);
9458               parg1 = TREE_OPERAND (parg, 1);
9459               STRIP_NOPS (parg0);
9460               STRIP_NOPS (parg1);
9461
9462               if (TREE_CODE (parg0) == MULT_EXPR
9463                   && TREE_CODE (parg1) != MULT_EXPR)
9464                 return fold_build2 (pcode, type,
9465                                     fold_build2 (PLUS_EXPR, type,
9466                                                  fold_convert (type, parg0),
9467                                                  fold_convert (type, marg)),
9468                                     fold_convert (type, parg1));
9469               if (TREE_CODE (parg0) != MULT_EXPR
9470                   && TREE_CODE (parg1) == MULT_EXPR)
9471                 return fold_build2 (PLUS_EXPR, type,
9472                                     fold_convert (type, parg0),
9473                                     fold_build2 (pcode, type,
9474                                                  fold_convert (type, marg),
9475                                                  fold_convert (type,
9476                                                                parg1)));
9477             }
9478         }
9479       else
9480         {
9481           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
9482           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9483             return non_lvalue (fold_convert (type, arg0));
9484
9485           /* Likewise if the operands are reversed.  */
9486           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9487             return non_lvalue (fold_convert (type, arg1));
9488
9489           /* Convert X + -C into X - C.  */
9490           if (TREE_CODE (arg1) == REAL_CST
9491               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9492             {
9493               tem = fold_negate_const (arg1, type);
9494               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9495                 return fold_build2 (MINUS_EXPR, type,
9496                                     fold_convert (type, arg0),
9497                                     fold_convert (type, tem));
9498             }
9499
9500           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9501              to __complex__ ( x, y ).  This is not the same for SNaNs or
9502              if signed zeros are involved.  */
9503           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9504               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9505               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9506             {
9507               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9508               tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
9509               tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
9510               bool arg0rz = false, arg0iz = false;
9511               if ((arg0r && (arg0rz = real_zerop (arg0r)))
9512                   || (arg0i && (arg0iz = real_zerop (arg0i))))
9513                 {
9514                   tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
9515                   tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
9516                   if (arg0rz && arg1i && real_zerop (arg1i))
9517                     {
9518                       tree rp = arg1r ? arg1r
9519                                   : build1 (REALPART_EXPR, rtype, arg1);
9520                       tree ip = arg0i ? arg0i
9521                                   : build1 (IMAGPART_EXPR, rtype, arg0);
9522                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9523                     }
9524                   else if (arg0iz && arg1r && real_zerop (arg1r))
9525                     {
9526                       tree rp = arg0r ? arg0r
9527                                   : build1 (REALPART_EXPR, rtype, arg0);
9528                       tree ip = arg1i ? arg1i
9529                                   : build1 (IMAGPART_EXPR, rtype, arg1);
9530                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9531                     }
9532                 }
9533             }
9534
9535           if (flag_unsafe_math_optimizations
9536               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9537               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9538               && (tem = distribute_real_division (code, type, arg0, arg1)))
9539             return tem;
9540
9541           /* Convert x+x into x*2.0.  */
9542           if (operand_equal_p (arg0, arg1, 0)
9543               && SCALAR_FLOAT_TYPE_P (type))
9544             return fold_build2 (MULT_EXPR, type, arg0,
9545                                 build_real (type, dconst2));
9546
9547           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.  
9548              We associate floats only if the user has specified
9549              -fassociative-math.  */
9550           if (flag_associative_math
9551               && TREE_CODE (arg1) == PLUS_EXPR
9552               && TREE_CODE (arg0) != MULT_EXPR)
9553             {
9554               tree tree10 = TREE_OPERAND (arg1, 0);
9555               tree tree11 = TREE_OPERAND (arg1, 1);
9556               if (TREE_CODE (tree11) == MULT_EXPR
9557                   && TREE_CODE (tree10) == MULT_EXPR)
9558                 {
9559                   tree tree0;
9560                   tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
9561                   return fold_build2 (PLUS_EXPR, type, tree0, tree11);
9562                 }
9563             }
9564           /* Convert (b*c + d*e) + a into b*c + (d*e +a).  
9565              We associate floats only if the user has specified
9566              -fassociative-math.  */
9567           if (flag_associative_math
9568               && TREE_CODE (arg0) == PLUS_EXPR
9569               && TREE_CODE (arg1) != MULT_EXPR)
9570             {
9571               tree tree00 = TREE_OPERAND (arg0, 0);
9572               tree tree01 = TREE_OPERAND (arg0, 1);
9573               if (TREE_CODE (tree01) == MULT_EXPR
9574                   && TREE_CODE (tree00) == MULT_EXPR)
9575                 {
9576                   tree tree0;
9577                   tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
9578                   return fold_build2 (PLUS_EXPR, type, tree00, tree0);
9579                 }
9580             }
9581         }
9582
9583      bit_rotate:
9584       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9585          is a rotate of A by C1 bits.  */
9586       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9587          is a rotate of A by B bits.  */
9588       {
9589         enum tree_code code0, code1;
9590         tree rtype;
9591         code0 = TREE_CODE (arg0);
9592         code1 = TREE_CODE (arg1);
9593         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9594              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9595             && operand_equal_p (TREE_OPERAND (arg0, 0),
9596                                 TREE_OPERAND (arg1, 0), 0)
9597             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9598                 TYPE_UNSIGNED (rtype))
9599             /* Only create rotates in complete modes.  Other cases are not
9600                expanded properly.  */
9601             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9602           {
9603             tree tree01, tree11;
9604             enum tree_code code01, code11;
9605
9606             tree01 = TREE_OPERAND (arg0, 1);
9607             tree11 = TREE_OPERAND (arg1, 1);
9608             STRIP_NOPS (tree01);
9609             STRIP_NOPS (tree11);
9610             code01 = TREE_CODE (tree01);
9611             code11 = TREE_CODE (tree11);
9612             if (code01 == INTEGER_CST
9613                 && code11 == INTEGER_CST
9614                 && TREE_INT_CST_HIGH (tree01) == 0
9615                 && TREE_INT_CST_HIGH (tree11) == 0
9616                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9617                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9618               return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
9619                              code0 == LSHIFT_EXPR ? tree01 : tree11);
9620             else if (code11 == MINUS_EXPR)
9621               {
9622                 tree tree110, tree111;
9623                 tree110 = TREE_OPERAND (tree11, 0);
9624                 tree111 = TREE_OPERAND (tree11, 1);
9625                 STRIP_NOPS (tree110);
9626                 STRIP_NOPS (tree111);
9627                 if (TREE_CODE (tree110) == INTEGER_CST
9628                     && 0 == compare_tree_int (tree110,
9629                                               TYPE_PRECISION
9630                                               (TREE_TYPE (TREE_OPERAND
9631                                                           (arg0, 0))))
9632                     && operand_equal_p (tree01, tree111, 0))
9633                   return build2 ((code0 == LSHIFT_EXPR
9634                                   ? LROTATE_EXPR
9635                                   : RROTATE_EXPR),
9636                                  type, TREE_OPERAND (arg0, 0), tree01);
9637               }
9638             else if (code01 == MINUS_EXPR)
9639               {
9640                 tree tree010, tree011;
9641                 tree010 = TREE_OPERAND (tree01, 0);
9642                 tree011 = TREE_OPERAND (tree01, 1);
9643                 STRIP_NOPS (tree010);
9644                 STRIP_NOPS (tree011);
9645                 if (TREE_CODE (tree010) == INTEGER_CST
9646                     && 0 == compare_tree_int (tree010,
9647                                               TYPE_PRECISION
9648                                               (TREE_TYPE (TREE_OPERAND
9649                                                           (arg0, 0))))
9650                     && operand_equal_p (tree11, tree011, 0))
9651                   return build2 ((code0 != LSHIFT_EXPR
9652                                   ? LROTATE_EXPR
9653                                   : RROTATE_EXPR),
9654                                  type, TREE_OPERAND (arg0, 0), tree11);
9655               }
9656           }
9657       }
9658
9659     associate:
9660       /* In most languages, can't associate operations on floats through
9661          parentheses.  Rather than remember where the parentheses were, we
9662          don't associate floats at all, unless the user has specified
9663          -fassociative-math.
9664          And, we need to make sure type is not saturating.  */
9665
9666       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9667           && !TYPE_SATURATING (type))
9668         {
9669           tree var0, con0, lit0, minus_lit0;
9670           tree var1, con1, lit1, minus_lit1;
9671           bool ok = true;
9672
9673           /* Split both trees into variables, constants, and literals.  Then
9674              associate each group together, the constants with literals,
9675              then the result with variables.  This increases the chances of
9676              literals being recombined later and of generating relocatable
9677              expressions for the sum of a constant and literal.  */
9678           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9679           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9680                              code == MINUS_EXPR);
9681
9682           /* With undefined overflow we can only associate constants
9683              with one variable.  */
9684           if ((POINTER_TYPE_P (type)
9685                || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9686               && var0 && var1)
9687             {
9688               tree tmp0 = var0;
9689               tree tmp1 = var1;
9690
9691               if (TREE_CODE (tmp0) == NEGATE_EXPR)
9692                 tmp0 = TREE_OPERAND (tmp0, 0);
9693               if (TREE_CODE (tmp1) == NEGATE_EXPR)
9694                 tmp1 = TREE_OPERAND (tmp1, 0);
9695               /* The only case we can still associate with two variables
9696                  is if they are the same, modulo negation.  */
9697               if (!operand_equal_p (tmp0, tmp1, 0))
9698                 ok = false;
9699             }
9700
9701           /* Only do something if we found more than two objects.  Otherwise,
9702              nothing has changed and we risk infinite recursion.  */
9703           if (ok
9704               && (2 < ((var0 != 0) + (var1 != 0)
9705                        + (con0 != 0) + (con1 != 0)
9706                        + (lit0 != 0) + (lit1 != 0)
9707                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
9708             {
9709               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
9710               if (code == MINUS_EXPR)
9711                 code = PLUS_EXPR;
9712
9713               var0 = associate_trees (var0, var1, code, type);
9714               con0 = associate_trees (con0, con1, code, type);
9715               lit0 = associate_trees (lit0, lit1, code, type);
9716               minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
9717
9718               /* Preserve the MINUS_EXPR if the negative part of the literal is
9719                  greater than the positive part.  Otherwise, the multiplicative
9720                  folding code (i.e extract_muldiv) may be fooled in case
9721                  unsigned constants are subtracted, like in the following
9722                  example: ((X*2 + 4) - 8U)/2.  */
9723               if (minus_lit0 && lit0)
9724                 {
9725                   if (TREE_CODE (lit0) == INTEGER_CST
9726                       && TREE_CODE (minus_lit0) == INTEGER_CST
9727                       && tree_int_cst_lt (lit0, minus_lit0))
9728                     {
9729                       minus_lit0 = associate_trees (minus_lit0, lit0,
9730                                                     MINUS_EXPR, type);
9731                       lit0 = 0;
9732                     }
9733                   else
9734                     {
9735                       lit0 = associate_trees (lit0, minus_lit0,
9736                                               MINUS_EXPR, type);
9737                       minus_lit0 = 0;
9738                     }
9739                 }
9740               if (minus_lit0)
9741                 {
9742                   if (con0 == 0)
9743                     return fold_convert (type,
9744                                          associate_trees (var0, minus_lit0,
9745                                                           MINUS_EXPR, type));
9746                   else
9747                     {
9748                       con0 = associate_trees (con0, minus_lit0,
9749                                               MINUS_EXPR, type);
9750                       return fold_convert (type,
9751                                            associate_trees (var0, con0,
9752                                                             PLUS_EXPR, type));
9753                     }
9754                 }
9755
9756               con0 = associate_trees (con0, lit0, code, type);
9757               return fold_convert (type, associate_trees (var0, con0,
9758                                                           code, type));
9759             }
9760         }
9761
9762       return NULL_TREE;
9763
9764     case MINUS_EXPR:
9765       /* Pointer simplifications for subtraction, simple reassociations. */
9766       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
9767         {
9768           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
9769           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
9770               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9771             {
9772               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9773               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
9774               tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
9775               tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
9776               return fold_build2 (PLUS_EXPR, type,
9777                                   fold_build2 (MINUS_EXPR, type, arg00, arg10),
9778                                   fold_build2 (MINUS_EXPR, type, arg01, arg11));
9779             }
9780           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
9781           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9782             {
9783               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9784               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
9785               tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
9786               if (tmp)
9787                 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
9788             }
9789         }
9790       /* A - (-B) -> A + B */
9791       if (TREE_CODE (arg1) == NEGATE_EXPR)
9792         return fold_build2 (PLUS_EXPR, type, op0,
9793                             fold_convert (type, TREE_OPERAND (arg1, 0)));
9794       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
9795       if (TREE_CODE (arg0) == NEGATE_EXPR
9796           && (FLOAT_TYPE_P (type)
9797               || INTEGRAL_TYPE_P (type))
9798           && negate_expr_p (arg1)
9799           && reorder_operands_p (arg0, arg1))
9800         return fold_build2 (MINUS_EXPR, type,
9801                             fold_convert (type, negate_expr (arg1)),
9802                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9803       /* Convert -A - 1 to ~A.  */
9804       if (INTEGRAL_TYPE_P (type)
9805           && TREE_CODE (arg0) == NEGATE_EXPR
9806           && integer_onep (arg1)
9807           && !TYPE_OVERFLOW_TRAPS (type))
9808         return fold_build1 (BIT_NOT_EXPR, type,
9809                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9810
9811       /* Convert -1 - A to ~A.  */
9812       if (INTEGRAL_TYPE_P (type)
9813           && integer_all_onesp (arg0))
9814         return fold_build1 (BIT_NOT_EXPR, type, op1);
9815
9816
9817       /* X - (X / CST) * CST is X % CST.  */
9818       if (INTEGRAL_TYPE_P (type)
9819           && TREE_CODE (arg1) == MULT_EXPR
9820           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9821           && operand_equal_p (arg0,
9822                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
9823           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
9824                               TREE_OPERAND (arg1, 1), 0))
9825         return fold_convert (type,
9826                              fold_build2 (TRUNC_MOD_EXPR, TREE_TYPE (arg0),
9827                                           arg0, TREE_OPERAND (arg1, 1)));
9828
9829       if (! FLOAT_TYPE_P (type))
9830         {
9831           if (integer_zerop (arg0))
9832             return negate_expr (fold_convert (type, arg1));
9833           if (integer_zerop (arg1))
9834             return non_lvalue (fold_convert (type, arg0));
9835
9836           /* Fold A - (A & B) into ~B & A.  */
9837           if (!TREE_SIDE_EFFECTS (arg0)
9838               && TREE_CODE (arg1) == BIT_AND_EXPR)
9839             {
9840               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
9841                 {
9842                   tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
9843                   return fold_build2 (BIT_AND_EXPR, type,
9844                                       fold_build1 (BIT_NOT_EXPR, type, arg10),
9845                                       fold_convert (type, arg0));
9846                 }
9847               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9848                 {
9849                   tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
9850                   return fold_build2 (BIT_AND_EXPR, type,
9851                                       fold_build1 (BIT_NOT_EXPR, type, arg11),
9852                                       fold_convert (type, arg0));
9853                 }
9854             }
9855
9856           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
9857              any power of 2 minus 1.  */
9858           if (TREE_CODE (arg0) == BIT_AND_EXPR
9859               && TREE_CODE (arg1) == BIT_AND_EXPR
9860               && operand_equal_p (TREE_OPERAND (arg0, 0),
9861                                   TREE_OPERAND (arg1, 0), 0))
9862             {
9863               tree mask0 = TREE_OPERAND (arg0, 1);
9864               tree mask1 = TREE_OPERAND (arg1, 1);
9865               tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
9866
9867               if (operand_equal_p (tem, mask1, 0))
9868                 {
9869                   tem = fold_build2 (BIT_XOR_EXPR, type,
9870                                      TREE_OPERAND (arg0, 0), mask1);
9871                   return fold_build2 (MINUS_EXPR, type, tem, mask1);
9872                 }
9873             }
9874         }
9875
9876       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
9877       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
9878         return non_lvalue (fold_convert (type, arg0));
9879
9880       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
9881          ARG0 is zero and X + ARG0 reduces to X, since that would mean
9882          (-ARG1 + ARG0) reduces to -ARG1.  */
9883       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9884         return negate_expr (fold_convert (type, arg1));
9885
9886       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9887          __complex__ ( x, -y ).  This is not the same for SNaNs or if
9888          signed zeros are involved.  */
9889       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9890           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9891           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9892         {
9893           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9894           tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
9895           tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
9896           bool arg0rz = false, arg0iz = false;
9897           if ((arg0r && (arg0rz = real_zerop (arg0r)))
9898               || (arg0i && (arg0iz = real_zerop (arg0i))))
9899             {
9900               tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
9901               tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
9902               if (arg0rz && arg1i && real_zerop (arg1i))
9903                 {
9904                   tree rp = fold_build1 (NEGATE_EXPR, rtype,
9905                                          arg1r ? arg1r
9906                                          : build1 (REALPART_EXPR, rtype, arg1));
9907                   tree ip = arg0i ? arg0i
9908                     : build1 (IMAGPART_EXPR, rtype, arg0);
9909                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9910                 }
9911               else if (arg0iz && arg1r && real_zerop (arg1r))
9912                 {
9913                   tree rp = arg0r ? arg0r
9914                     : build1 (REALPART_EXPR, rtype, arg0);
9915                   tree ip = fold_build1 (NEGATE_EXPR, rtype,
9916                                          arg1i ? arg1i
9917                                          : build1 (IMAGPART_EXPR, rtype, arg1));
9918                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9919                 }
9920             }
9921         }
9922
9923       /* Fold &x - &x.  This can happen from &x.foo - &x.
9924          This is unsafe for certain floats even in non-IEEE formats.
9925          In IEEE, it is unsafe because it does wrong for NaNs.
9926          Also note that operand_equal_p is always false if an operand
9927          is volatile.  */
9928
9929       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
9930           && operand_equal_p (arg0, arg1, 0))
9931         return fold_convert (type, integer_zero_node);
9932
9933       /* A - B -> A + (-B) if B is easily negatable.  */
9934       if (negate_expr_p (arg1)
9935           && ((FLOAT_TYPE_P (type)
9936                /* Avoid this transformation if B is a positive REAL_CST.  */
9937                && (TREE_CODE (arg1) != REAL_CST
9938                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
9939               || INTEGRAL_TYPE_P (type)))
9940         return fold_build2 (PLUS_EXPR, type,
9941                             fold_convert (type, arg0),
9942                             fold_convert (type, negate_expr (arg1)));
9943
9944       /* Try folding difference of addresses.  */
9945       {
9946         HOST_WIDE_INT diff;
9947
9948         if ((TREE_CODE (arg0) == ADDR_EXPR
9949              || TREE_CODE (arg1) == ADDR_EXPR)
9950             && ptr_difference_const (arg0, arg1, &diff))
9951           return build_int_cst_type (type, diff);
9952       }
9953
9954       /* Fold &a[i] - &a[j] to i-j.  */
9955       if (TREE_CODE (arg0) == ADDR_EXPR
9956           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9957           && TREE_CODE (arg1) == ADDR_EXPR
9958           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9959         {
9960           tree aref0 = TREE_OPERAND (arg0, 0);
9961           tree aref1 = TREE_OPERAND (arg1, 0);
9962           if (operand_equal_p (TREE_OPERAND (aref0, 0),
9963                                TREE_OPERAND (aref1, 0), 0))
9964             {
9965               tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
9966               tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
9967               tree esz = array_ref_element_size (aref0);
9968               tree diff = build2 (MINUS_EXPR, type, op0, op1);
9969               return fold_build2 (MULT_EXPR, type, diff,
9970                                   fold_convert (type, esz));
9971                                   
9972             }
9973         }
9974
9975       if (flag_unsafe_math_optimizations
9976           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9977           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9978           && (tem = distribute_real_division (code, type, arg0, arg1)))
9979         return tem;
9980
9981       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
9982          same or one.  Make sure type is not saturating.
9983          fold_plusminus_mult_expr will re-associate.  */
9984       if ((TREE_CODE (arg0) == MULT_EXPR
9985            || TREE_CODE (arg1) == MULT_EXPR)
9986           && !TYPE_SATURATING (type)
9987           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9988         {
9989           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9990           if (tem)
9991             return tem;
9992         }
9993
9994       goto associate;
9995
9996     case MULT_EXPR:
9997       /* (-A) * (-B) -> A * B  */
9998       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
9999         return fold_build2 (MULT_EXPR, type,
10000                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10001                             fold_convert (type, negate_expr (arg1)));
10002       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10003         return fold_build2 (MULT_EXPR, type,
10004                             fold_convert (type, negate_expr (arg0)),
10005                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10006
10007       if (! FLOAT_TYPE_P (type))
10008         {
10009           if (integer_zerop (arg1))
10010             return omit_one_operand (type, arg1, arg0);
10011           if (integer_onep (arg1))
10012             return non_lvalue (fold_convert (type, arg0));
10013           /* Transform x * -1 into -x.  Make sure to do the negation
10014              on the original operand with conversions not stripped
10015              because we can only strip non-sign-changing conversions.  */
10016           if (integer_all_onesp (arg1))
10017             return fold_convert (type, negate_expr (op0));
10018           /* Transform x * -C into -x * C if x is easily negatable.  */
10019           if (TREE_CODE (arg1) == INTEGER_CST
10020               && tree_int_cst_sgn (arg1) == -1
10021               && negate_expr_p (arg0)
10022               && (tem = negate_expr (arg1)) != arg1
10023               && !TREE_OVERFLOW (tem))
10024             return fold_build2 (MULT_EXPR, type,
10025                                 fold_convert (type, negate_expr (arg0)), tem);
10026
10027           /* (a * (1 << b)) is (a << b)  */
10028           if (TREE_CODE (arg1) == LSHIFT_EXPR
10029               && integer_onep (TREE_OPERAND (arg1, 0)))
10030             return fold_build2 (LSHIFT_EXPR, type, op0,
10031                                 TREE_OPERAND (arg1, 1));
10032           if (TREE_CODE (arg0) == LSHIFT_EXPR
10033               && integer_onep (TREE_OPERAND (arg0, 0)))
10034             return fold_build2 (LSHIFT_EXPR, type, op1,
10035                                 TREE_OPERAND (arg0, 1));
10036
10037           strict_overflow_p = false;
10038           if (TREE_CODE (arg1) == INTEGER_CST
10039               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10040                                              &strict_overflow_p)))
10041             {
10042               if (strict_overflow_p)
10043                 fold_overflow_warning (("assuming signed overflow does not "
10044                                         "occur when simplifying "
10045                                         "multiplication"),
10046                                        WARN_STRICT_OVERFLOW_MISC);
10047               return fold_convert (type, tem);
10048             }
10049
10050           /* Optimize z * conj(z) for integer complex numbers.  */
10051           if (TREE_CODE (arg0) == CONJ_EXPR
10052               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10053             return fold_mult_zconjz (type, arg1);
10054           if (TREE_CODE (arg1) == CONJ_EXPR
10055               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10056             return fold_mult_zconjz (type, arg0);
10057         }
10058       else
10059         {
10060           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10061              when x is NaN, since x * 0 is also NaN.  Nor are they the
10062              same in modes with signed zeros, since multiplying a
10063              negative value by 0 gives -0, not +0.  */
10064           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10065               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10066               && real_zerop (arg1))
10067             return omit_one_operand (type, arg1, arg0);
10068           /* In IEEE floating point, x*1 is not equivalent to x for snans.  */
10069           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10070               && real_onep (arg1))
10071             return non_lvalue (fold_convert (type, arg0));
10072
10073           /* Transform x * -1.0 into -x.  */
10074           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10075               && real_minus_onep (arg1))
10076             return fold_convert (type, negate_expr (arg0));
10077
10078           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10079              the result for floating point types due to rounding so it is applied
10080              only if -fassociative-math was specify.  */
10081           if (flag_associative_math
10082               && TREE_CODE (arg0) == RDIV_EXPR
10083               && TREE_CODE (arg1) == REAL_CST
10084               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10085             {
10086               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10087                                       arg1, 0);
10088               if (tem)
10089                 return fold_build2 (RDIV_EXPR, type, tem,
10090                                     TREE_OPERAND (arg0, 1));
10091             }
10092
10093           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10094           if (operand_equal_p (arg0, arg1, 0))
10095             {
10096               tree tem = fold_strip_sign_ops (arg0);
10097               if (tem != NULL_TREE)
10098                 {
10099                   tem = fold_convert (type, tem);
10100                   return fold_build2 (MULT_EXPR, type, tem, tem);
10101                 }
10102             }
10103
10104           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10105              This is not the same for NaNs or if signed zeros are
10106              involved.  */
10107           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10108               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10109               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10110               && TREE_CODE (arg1) == COMPLEX_CST
10111               && real_zerop (TREE_REALPART (arg1)))
10112             {
10113               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10114               if (real_onep (TREE_IMAGPART (arg1)))
10115                 return fold_build2 (COMPLEX_EXPR, type,
10116                                     negate_expr (fold_build1 (IMAGPART_EXPR,
10117                                                               rtype, arg0)),
10118                                     fold_build1 (REALPART_EXPR, rtype, arg0));
10119               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10120                 return fold_build2 (COMPLEX_EXPR, type,
10121                                     fold_build1 (IMAGPART_EXPR, rtype, arg0),
10122                                     negate_expr (fold_build1 (REALPART_EXPR,
10123                                                               rtype, arg0)));
10124             }
10125
10126           /* Optimize z * conj(z) for floating point complex numbers.
10127              Guarded by flag_unsafe_math_optimizations as non-finite
10128              imaginary components don't produce scalar results.  */
10129           if (flag_unsafe_math_optimizations
10130               && TREE_CODE (arg0) == CONJ_EXPR
10131               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10132             return fold_mult_zconjz (type, arg1);
10133           if (flag_unsafe_math_optimizations
10134               && TREE_CODE (arg1) == CONJ_EXPR
10135               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10136             return fold_mult_zconjz (type, arg0);
10137
10138           if (flag_unsafe_math_optimizations)
10139             {
10140               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10141               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10142
10143               /* Optimizations of root(...)*root(...).  */
10144               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10145                 {
10146                   tree rootfn, arg;
10147                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10148                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10149
10150                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10151                   if (BUILTIN_SQRT_P (fcode0)
10152                       && operand_equal_p (arg00, arg10, 0)
10153                       && ! HONOR_SNANS (TYPE_MODE (type)))
10154                     return arg00;
10155
10156                   /* Optimize root(x)*root(y) as root(x*y).  */
10157                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10158                   arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10159                   return build_call_expr (rootfn, 1, arg);
10160                 }
10161
10162               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10163               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10164                 {
10165                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10166                   tree arg = fold_build2 (PLUS_EXPR, type,
10167                                           CALL_EXPR_ARG (arg0, 0),
10168                                           CALL_EXPR_ARG (arg1, 0));
10169                   return build_call_expr (expfn, 1, arg);
10170                 }
10171
10172               /* Optimizations of pow(...)*pow(...).  */
10173               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10174                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10175                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10176                 {
10177                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10178                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10179                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10180                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10181
10182                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10183                   if (operand_equal_p (arg01, arg11, 0))
10184                     {
10185                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10186                       tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10187                       return build_call_expr (powfn, 2, arg, arg01);
10188                     }
10189
10190                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10191                   if (operand_equal_p (arg00, arg10, 0))
10192                     {
10193                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10194                       tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10195                       return build_call_expr (powfn, 2, arg00, arg);
10196                     }
10197                 }
10198
10199               /* Optimize tan(x)*cos(x) as sin(x).  */
10200               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10201                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10202                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10203                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10204                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10205                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10206                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10207                                       CALL_EXPR_ARG (arg1, 0), 0))
10208                 {
10209                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10210
10211                   if (sinfn != NULL_TREE)
10212                     return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10213                 }
10214
10215               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10216               if (fcode1 == BUILT_IN_POW
10217                   || fcode1 == BUILT_IN_POWF
10218                   || fcode1 == BUILT_IN_POWL)
10219                 {
10220                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10221                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10222                   if (TREE_CODE (arg11) == REAL_CST
10223                       && !TREE_OVERFLOW (arg11)
10224                       && operand_equal_p (arg0, arg10, 0))
10225                     {
10226                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10227                       REAL_VALUE_TYPE c;
10228                       tree arg;
10229
10230                       c = TREE_REAL_CST (arg11);
10231                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10232                       arg = build_real (type, c);
10233                       return build_call_expr (powfn, 2, arg0, arg);
10234                     }
10235                 }
10236
10237               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10238               if (fcode0 == BUILT_IN_POW
10239                   || fcode0 == BUILT_IN_POWF
10240                   || fcode0 == BUILT_IN_POWL)
10241                 {
10242                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10243                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10244                   if (TREE_CODE (arg01) == REAL_CST
10245                       && !TREE_OVERFLOW (arg01)
10246                       && operand_equal_p (arg1, arg00, 0))
10247                     {
10248                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10249                       REAL_VALUE_TYPE c;
10250                       tree arg;
10251
10252                       c = TREE_REAL_CST (arg01);
10253                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10254                       arg = build_real (type, c);
10255                       return build_call_expr (powfn, 2, arg1, arg);
10256                     }
10257                 }
10258
10259               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10260               if (! optimize_size
10261                   && operand_equal_p (arg0, arg1, 0))
10262                 {
10263                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10264
10265                   if (powfn)
10266                     {
10267                       tree arg = build_real (type, dconst2);
10268                       return build_call_expr (powfn, 2, arg0, arg);
10269                     }
10270                 }
10271             }
10272         }
10273       goto associate;
10274
10275     case BIT_IOR_EXPR:
10276     bit_ior:
10277       if (integer_all_onesp (arg1))
10278         return omit_one_operand (type, arg1, arg0);
10279       if (integer_zerop (arg1))
10280         return non_lvalue (fold_convert (type, arg0));
10281       if (operand_equal_p (arg0, arg1, 0))
10282         return non_lvalue (fold_convert (type, arg0));
10283
10284       /* ~X | X is -1.  */
10285       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10286           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10287         {
10288           t1 = fold_convert (type, integer_zero_node);
10289           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10290           return omit_one_operand (type, t1, arg1);
10291         }
10292
10293       /* X | ~X is -1.  */
10294       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10295           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10296         {
10297           t1 = fold_convert (type, integer_zero_node);
10298           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10299           return omit_one_operand (type, t1, arg0);
10300         }
10301
10302       /* Canonicalize (X & C1) | C2.  */
10303       if (TREE_CODE (arg0) == BIT_AND_EXPR
10304           && TREE_CODE (arg1) == INTEGER_CST
10305           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10306         {
10307           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10308           int width = TYPE_PRECISION (type), w;
10309           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10310           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10311           hi2 = TREE_INT_CST_HIGH (arg1);
10312           lo2 = TREE_INT_CST_LOW (arg1);
10313
10314           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10315           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10316             return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10317
10318           if (width > HOST_BITS_PER_WIDE_INT)
10319             {
10320               mhi = (unsigned HOST_WIDE_INT) -1 
10321                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10322               mlo = -1;
10323             }
10324           else
10325             {
10326               mhi = 0;
10327               mlo = (unsigned HOST_WIDE_INT) -1
10328                     >> (HOST_BITS_PER_WIDE_INT - width);
10329             }
10330
10331           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10332           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10333             return fold_build2 (BIT_IOR_EXPR, type,
10334                                 TREE_OPERAND (arg0, 0), arg1);
10335
10336           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10337              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10338              mode which allows further optimizations.  */
10339           hi1 &= mhi;
10340           lo1 &= mlo;
10341           hi2 &= mhi;
10342           lo2 &= mlo;
10343           hi3 = hi1 & ~hi2;
10344           lo3 = lo1 & ~lo2;
10345           for (w = BITS_PER_UNIT;
10346                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10347                w <<= 1)
10348             {
10349               unsigned HOST_WIDE_INT mask
10350                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10351               if (((lo1 | lo2) & mask) == mask
10352                   && (lo1 & ~mask) == 0 && hi1 == 0)
10353                 {
10354                   hi3 = 0;
10355                   lo3 = mask;
10356                   break;
10357                 }
10358             }
10359           if (hi3 != hi1 || lo3 != lo1)
10360             return fold_build2 (BIT_IOR_EXPR, type,
10361                                 fold_build2 (BIT_AND_EXPR, type,
10362                                              TREE_OPERAND (arg0, 0),
10363                                              build_int_cst_wide (type,
10364                                                                  lo3, hi3)),
10365                                 arg1);
10366         }
10367
10368       /* (X & Y) | Y is (X, Y).  */
10369       if (TREE_CODE (arg0) == BIT_AND_EXPR
10370           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10371         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10372       /* (X & Y) | X is (Y, X).  */
10373       if (TREE_CODE (arg0) == BIT_AND_EXPR
10374           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10375           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10376         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10377       /* X | (X & Y) is (Y, X).  */
10378       if (TREE_CODE (arg1) == BIT_AND_EXPR
10379           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10380           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10381         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10382       /* X | (Y & X) is (Y, X).  */
10383       if (TREE_CODE (arg1) == BIT_AND_EXPR
10384           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10385           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10386         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10387
10388       t1 = distribute_bit_expr (code, type, arg0, arg1);
10389       if (t1 != NULL_TREE)
10390         return t1;
10391
10392       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10393
10394          This results in more efficient code for machines without a NAND
10395          instruction.  Combine will canonicalize to the first form
10396          which will allow use of NAND instructions provided by the
10397          backend if they exist.  */
10398       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10399           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10400         {
10401           return fold_build1 (BIT_NOT_EXPR, type,
10402                               build2 (BIT_AND_EXPR, type,
10403                                       TREE_OPERAND (arg0, 0),
10404                                       TREE_OPERAND (arg1, 0)));
10405         }
10406
10407       /* See if this can be simplified into a rotate first.  If that
10408          is unsuccessful continue in the association code.  */
10409       goto bit_rotate;
10410
10411     case BIT_XOR_EXPR:
10412       if (integer_zerop (arg1))
10413         return non_lvalue (fold_convert (type, arg0));
10414       if (integer_all_onesp (arg1))
10415         return fold_build1 (BIT_NOT_EXPR, type, op0);
10416       if (operand_equal_p (arg0, arg1, 0))
10417         return omit_one_operand (type, integer_zero_node, arg0);
10418
10419       /* ~X ^ X is -1.  */
10420       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10421           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10422         {
10423           t1 = fold_convert (type, integer_zero_node);
10424           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10425           return omit_one_operand (type, t1, arg1);
10426         }
10427
10428       /* X ^ ~X is -1.  */
10429       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10430           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10431         {
10432           t1 = fold_convert (type, integer_zero_node);
10433           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10434           return omit_one_operand (type, t1, arg0);
10435         }
10436
10437       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10438          with a constant, and the two constants have no bits in common,
10439          we should treat this as a BIT_IOR_EXPR since this may produce more
10440          simplifications.  */
10441       if (TREE_CODE (arg0) == BIT_AND_EXPR
10442           && TREE_CODE (arg1) == BIT_AND_EXPR
10443           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10444           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10445           && integer_zerop (const_binop (BIT_AND_EXPR,
10446                                          TREE_OPERAND (arg0, 1),
10447                                          TREE_OPERAND (arg1, 1), 0)))
10448         {
10449           code = BIT_IOR_EXPR;
10450           goto bit_ior;
10451         }
10452
10453       /* (X | Y) ^ X -> Y & ~ X*/
10454       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10455           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10456         {
10457           tree t2 = TREE_OPERAND (arg0, 1);
10458           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10459                             arg1);
10460           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10461                             fold_convert (type, t1));
10462           return t1;
10463         }
10464
10465       /* (Y | X) ^ X -> Y & ~ X*/
10466       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10467           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10468         {
10469           tree t2 = TREE_OPERAND (arg0, 0);
10470           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10471                             arg1);
10472           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10473                             fold_convert (type, t1));
10474           return t1;
10475         }
10476
10477       /* X ^ (X | Y) -> Y & ~ X*/
10478       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10479           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10480         {
10481           tree t2 = TREE_OPERAND (arg1, 1);
10482           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10483                             arg0);
10484           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10485                             fold_convert (type, t1));
10486           return t1;
10487         }
10488
10489       /* X ^ (Y | X) -> Y & ~ X*/
10490       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10491           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10492         {
10493           tree t2 = TREE_OPERAND (arg1, 0);
10494           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10495                             arg0);
10496           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10497                             fold_convert (type, t1));
10498           return t1;
10499         }
10500         
10501       /* Convert ~X ^ ~Y to X ^ Y.  */
10502       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10503           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10504         return fold_build2 (code, type,
10505                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10506                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10507
10508       /* Convert ~X ^ C to X ^ ~C.  */
10509       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10510           && TREE_CODE (arg1) == INTEGER_CST)
10511         return fold_build2 (code, type,
10512                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10513                             fold_build1 (BIT_NOT_EXPR, type, arg1));
10514
10515       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
10516       if (TREE_CODE (arg0) == BIT_AND_EXPR
10517           && integer_onep (TREE_OPERAND (arg0, 1))
10518           && integer_onep (arg1))
10519         return fold_build2 (EQ_EXPR, type, arg0,
10520                             build_int_cst (TREE_TYPE (arg0), 0));
10521
10522       /* Fold (X & Y) ^ Y as ~X & Y.  */
10523       if (TREE_CODE (arg0) == BIT_AND_EXPR
10524           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10525         {
10526           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10527           return fold_build2 (BIT_AND_EXPR, type, 
10528                               fold_build1 (BIT_NOT_EXPR, type, tem),
10529                               fold_convert (type, arg1));
10530         }
10531       /* Fold (X & Y) ^ X as ~Y & X.  */
10532       if (TREE_CODE (arg0) == BIT_AND_EXPR
10533           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10534           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10535         {
10536           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10537           return fold_build2 (BIT_AND_EXPR, type,
10538                               fold_build1 (BIT_NOT_EXPR, type, tem),
10539                               fold_convert (type, arg1));
10540         }
10541       /* Fold X ^ (X & Y) as X & ~Y.  */
10542       if (TREE_CODE (arg1) == BIT_AND_EXPR
10543           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10544         {
10545           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10546           return fold_build2 (BIT_AND_EXPR, type,
10547                               fold_convert (type, arg0),
10548                               fold_build1 (BIT_NOT_EXPR, type, tem));
10549         }
10550       /* Fold X ^ (Y & X) as ~Y & X.  */
10551       if (TREE_CODE (arg1) == BIT_AND_EXPR
10552           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10553           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10554         {
10555           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10556           return fold_build2 (BIT_AND_EXPR, type,
10557                               fold_build1 (BIT_NOT_EXPR, type, tem),
10558                               fold_convert (type, arg0));
10559         }
10560
10561       /* See if this can be simplified into a rotate first.  If that
10562          is unsuccessful continue in the association code.  */
10563       goto bit_rotate;
10564
10565     case BIT_AND_EXPR:
10566       if (integer_all_onesp (arg1))
10567         return non_lvalue (fold_convert (type, arg0));
10568       if (integer_zerop (arg1))
10569         return omit_one_operand (type, arg1, arg0);
10570       if (operand_equal_p (arg0, arg1, 0))
10571         return non_lvalue (fold_convert (type, arg0));
10572
10573       /* ~X & X is always zero.  */
10574       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10575           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10576         return omit_one_operand (type, integer_zero_node, arg1);
10577
10578       /* X & ~X is always zero.  */
10579       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10580           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10581         return omit_one_operand (type, integer_zero_node, arg0);
10582
10583       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
10584       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10585           && TREE_CODE (arg1) == INTEGER_CST
10586           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10587         {
10588           tree tmp1 = fold_convert (TREE_TYPE (arg0), arg1);
10589           tree tmp2 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
10590                                    TREE_OPERAND (arg0, 0), tmp1);
10591           tree tmp3 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
10592                                    TREE_OPERAND (arg0, 1), tmp1);
10593           return fold_convert (type,
10594                                fold_build2 (BIT_IOR_EXPR, TREE_TYPE (arg0),
10595                                             tmp2, tmp3));
10596         }
10597
10598       /* (X | Y) & Y is (X, Y).  */
10599       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10600           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10601         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10602       /* (X | Y) & X is (Y, X).  */
10603       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10604           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10605           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10606         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10607       /* X & (X | Y) is (Y, X).  */
10608       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10609           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10610           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10611         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10612       /* X & (Y | X) is (Y, X).  */
10613       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10614           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10615           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10616         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10617
10618       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
10619       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10620           && integer_onep (TREE_OPERAND (arg0, 1))
10621           && integer_onep (arg1))
10622         {
10623           tem = TREE_OPERAND (arg0, 0);
10624           return fold_build2 (EQ_EXPR, type,
10625                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10626                                            build_int_cst (TREE_TYPE (tem), 1)),
10627                               build_int_cst (TREE_TYPE (tem), 0));
10628         }
10629       /* Fold ~X & 1 as (X & 1) == 0.  */
10630       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10631           && integer_onep (arg1))
10632         {
10633           tem = TREE_OPERAND (arg0, 0);
10634           return fold_build2 (EQ_EXPR, type,
10635                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10636                                            build_int_cst (TREE_TYPE (tem), 1)),
10637                               build_int_cst (TREE_TYPE (tem), 0));
10638         }
10639
10640       /* Fold (X ^ Y) & Y as ~X & Y.  */
10641       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10642           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10643         {
10644           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10645           return fold_build2 (BIT_AND_EXPR, type, 
10646                               fold_build1 (BIT_NOT_EXPR, type, tem),
10647                               fold_convert (type, arg1));
10648         }
10649       /* Fold (X ^ Y) & X as ~Y & X.  */
10650       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10651           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10652           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10653         {
10654           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10655           return fold_build2 (BIT_AND_EXPR, type,
10656                               fold_build1 (BIT_NOT_EXPR, type, tem),
10657                               fold_convert (type, arg1));
10658         }
10659       /* Fold X & (X ^ Y) as X & ~Y.  */
10660       if (TREE_CODE (arg1) == BIT_XOR_EXPR
10661           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10662         {
10663           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10664           return fold_build2 (BIT_AND_EXPR, type,
10665                               fold_convert (type, arg0),
10666                               fold_build1 (BIT_NOT_EXPR, type, tem));
10667         }
10668       /* Fold X & (Y ^ X) as ~Y & X.  */
10669       if (TREE_CODE (arg1) == BIT_XOR_EXPR
10670           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10671           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10672         {
10673           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10674           return fold_build2 (BIT_AND_EXPR, type,
10675                               fold_build1 (BIT_NOT_EXPR, type, tem),
10676                               fold_convert (type, arg0));
10677         }
10678
10679       t1 = distribute_bit_expr (code, type, arg0, arg1);
10680       if (t1 != NULL_TREE)
10681         return t1;
10682       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
10683       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10684           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10685         {
10686           unsigned int prec
10687             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10688
10689           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
10690               && (~TREE_INT_CST_LOW (arg1)
10691                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
10692             return fold_convert (type, TREE_OPERAND (arg0, 0));
10693         }
10694
10695       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
10696
10697          This results in more efficient code for machines without a NOR
10698          instruction.  Combine will canonicalize to the first form
10699          which will allow use of NOR instructions provided by the
10700          backend if they exist.  */
10701       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10702           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10703         {
10704           return fold_build1 (BIT_NOT_EXPR, type,
10705                               build2 (BIT_IOR_EXPR, type,
10706                                       fold_convert (type,
10707                                                     TREE_OPERAND (arg0, 0)),
10708                                       fold_convert (type,
10709                                                     TREE_OPERAND (arg1, 0))));
10710         }
10711
10712       /* If arg0 is derived from the address of an object or function, we may
10713          be able to fold this expression using the object or function's
10714          alignment.  */
10715       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
10716         {
10717           unsigned HOST_WIDE_INT modulus, residue;
10718           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
10719
10720           modulus = get_pointer_modulus_and_residue (arg0, &residue);
10721
10722           /* This works because modulus is a power of 2.  If this weren't the
10723              case, we'd have to replace it by its greatest power-of-2
10724              divisor: modulus & -modulus.  */
10725           if (low < modulus)
10726             return build_int_cst (type, residue & low);
10727         }
10728
10729       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
10730               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
10731          if the new mask might be further optimized.  */
10732       if ((TREE_CODE (arg0) == LSHIFT_EXPR
10733            || TREE_CODE (arg0) == RSHIFT_EXPR)
10734           && host_integerp (TREE_OPERAND (arg0, 1), 1)
10735           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
10736           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
10737              < TYPE_PRECISION (TREE_TYPE (arg0))
10738           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
10739           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
10740         {
10741           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
10742           unsigned HOST_WIDE_INT mask
10743             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
10744           unsigned HOST_WIDE_INT newmask, zerobits = 0;
10745           tree shift_type = TREE_TYPE (arg0);
10746
10747           if (TREE_CODE (arg0) == LSHIFT_EXPR)
10748             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
10749           else if (TREE_CODE (arg0) == RSHIFT_EXPR
10750                    && TYPE_PRECISION (TREE_TYPE (arg0))
10751                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
10752             {
10753               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
10754               tree arg00 = TREE_OPERAND (arg0, 0);
10755               /* See if more bits can be proven as zero because of
10756                  zero extension.  */
10757               if (TREE_CODE (arg00) == NOP_EXPR
10758                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
10759                 {
10760                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
10761                   if (TYPE_PRECISION (inner_type)
10762                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
10763                       && TYPE_PRECISION (inner_type) < prec)
10764                     {
10765                       prec = TYPE_PRECISION (inner_type);
10766                       /* See if we can shorten the right shift.  */
10767                       if (shiftc < prec)
10768                         shift_type = inner_type;
10769                     }
10770                 }
10771               zerobits = ~(unsigned HOST_WIDE_INT) 0;
10772               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
10773               zerobits <<= prec - shiftc;
10774               /* For arithmetic shift if sign bit could be set, zerobits
10775                  can contain actually sign bits, so no transformation is
10776                  possible, unless MASK masks them all away.  In that
10777                  case the shift needs to be converted into logical shift.  */
10778               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
10779                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
10780                 {
10781                   if ((mask & zerobits) == 0)
10782                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
10783                   else
10784                     zerobits = 0;
10785                 }
10786             }
10787
10788           /* ((X << 16) & 0xff00) is (X, 0).  */
10789           if ((mask & zerobits) == mask)
10790             return omit_one_operand (type, build_int_cst (type, 0), arg0);
10791
10792           newmask = mask | zerobits;
10793           if (newmask != mask && (newmask & (newmask + 1)) == 0)
10794             {
10795               unsigned int prec;
10796
10797               /* Only do the transformation if NEWMASK is some integer
10798                  mode's mask.  */
10799               for (prec = BITS_PER_UNIT;
10800                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
10801                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
10802                   break;
10803               if (prec < HOST_BITS_PER_WIDE_INT
10804                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
10805                 {
10806                   if (shift_type != TREE_TYPE (arg0))
10807                     {
10808                       tem = fold_build2 (TREE_CODE (arg0), shift_type,
10809                                          fold_convert (shift_type,
10810                                                        TREE_OPERAND (arg0, 0)),
10811                                          TREE_OPERAND (arg0, 1));
10812                       tem = fold_convert (type, tem);
10813                     }
10814                   else
10815                     tem = op0;
10816                   return fold_build2 (BIT_AND_EXPR, type, tem,
10817                                       build_int_cst_type (TREE_TYPE (op1),
10818                                                           newmask));
10819                 }
10820             }
10821         }
10822
10823       goto associate;
10824
10825     case RDIV_EXPR:
10826       /* Don't touch a floating-point divide by zero unless the mode
10827          of the constant can represent infinity.  */
10828       if (TREE_CODE (arg1) == REAL_CST
10829           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10830           && real_zerop (arg1))
10831         return NULL_TREE;
10832
10833       /* Optimize A / A to 1.0 if we don't care about
10834          NaNs or Infinities.  Skip the transformation
10835          for non-real operands.  */
10836       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
10837           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10838           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
10839           && operand_equal_p (arg0, arg1, 0))
10840         {
10841           tree r = build_real (TREE_TYPE (arg0), dconst1);
10842
10843           return omit_two_operands (type, r, arg0, arg1);
10844         }
10845
10846       /* The complex version of the above A / A optimization.  */
10847       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10848           && operand_equal_p (arg0, arg1, 0))
10849         {
10850           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
10851           if (! HONOR_NANS (TYPE_MODE (elem_type))
10852               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
10853             {
10854               tree r = build_real (elem_type, dconst1);
10855               /* omit_two_operands will call fold_convert for us.  */
10856               return omit_two_operands (type, r, arg0, arg1);
10857             }
10858         }
10859
10860       /* (-A) / (-B) -> A / B  */
10861       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10862         return fold_build2 (RDIV_EXPR, type,
10863                             TREE_OPERAND (arg0, 0),
10864                             negate_expr (arg1));
10865       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10866         return fold_build2 (RDIV_EXPR, type,
10867                             negate_expr (arg0),
10868                             TREE_OPERAND (arg1, 0));
10869
10870       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
10871       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10872           && real_onep (arg1))
10873         return non_lvalue (fold_convert (type, arg0));
10874
10875       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
10876       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10877           && real_minus_onep (arg1))
10878         return non_lvalue (fold_convert (type, negate_expr (arg0)));
10879
10880       /* If ARG1 is a constant, we can convert this to a multiply by the
10881          reciprocal.  This does not have the same rounding properties,
10882          so only do this if -freciprocal-math.  We can actually
10883          always safely do it if ARG1 is a power of two, but it's hard to
10884          tell if it is or not in a portable manner.  */
10885       if (TREE_CODE (arg1) == REAL_CST)
10886         {
10887           if (flag_reciprocal_math
10888               && 0 != (tem = const_binop (code, build_real (type, dconst1),
10889                                           arg1, 0)))
10890             return fold_build2 (MULT_EXPR, type, arg0, tem);
10891           /* Find the reciprocal if optimizing and the result is exact.  */
10892           if (optimize)
10893             {
10894               REAL_VALUE_TYPE r;
10895               r = TREE_REAL_CST (arg1);
10896               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
10897                 {
10898                   tem = build_real (type, r);
10899                   return fold_build2 (MULT_EXPR, type,
10900                                       fold_convert (type, arg0), tem);
10901                 }
10902             }
10903         }
10904       /* Convert A/B/C to A/(B*C).  */ 
10905       if (flag_reciprocal_math
10906           && TREE_CODE (arg0) == RDIV_EXPR)
10907         return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
10908                             fold_build2 (MULT_EXPR, type,
10909                                          TREE_OPERAND (arg0, 1), arg1));
10910
10911       /* Convert A/(B/C) to (A/B)*C.  */
10912       if (flag_reciprocal_math
10913           && TREE_CODE (arg1) == RDIV_EXPR)
10914         return fold_build2 (MULT_EXPR, type,
10915                             fold_build2 (RDIV_EXPR, type, arg0,
10916                                          TREE_OPERAND (arg1, 0)),
10917                             TREE_OPERAND (arg1, 1));
10918
10919       /* Convert C1/(X*C2) into (C1/C2)/X.  */
10920       if (flag_reciprocal_math
10921           && TREE_CODE (arg1) == MULT_EXPR
10922           && TREE_CODE (arg0) == REAL_CST
10923           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
10924         {
10925           tree tem = const_binop (RDIV_EXPR, arg0,
10926                                   TREE_OPERAND (arg1, 1), 0);
10927           if (tem)
10928             return fold_build2 (RDIV_EXPR, type, tem,
10929                                 TREE_OPERAND (arg1, 0));
10930         }
10931
10932       if (flag_unsafe_math_optimizations)
10933         {
10934           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10935           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10936
10937           /* Optimize sin(x)/cos(x) as tan(x).  */
10938           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
10939                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
10940                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
10941               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10942                                   CALL_EXPR_ARG (arg1, 0), 0))
10943             {
10944               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
10945
10946               if (tanfn != NULL_TREE)
10947                 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
10948             }
10949
10950           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
10951           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
10952                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
10953                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
10954               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10955                                   CALL_EXPR_ARG (arg1, 0), 0))
10956             {
10957               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
10958
10959               if (tanfn != NULL_TREE)
10960                 {
10961                   tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
10962                   return fold_build2 (RDIV_EXPR, type,
10963                                       build_real (type, dconst1), tmp);
10964                 }
10965             }
10966
10967           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
10968              NaNs or Infinities.  */
10969           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
10970                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
10971                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
10972             {
10973               tree arg00 = CALL_EXPR_ARG (arg0, 0);
10974               tree arg01 = CALL_EXPR_ARG (arg1, 0);
10975
10976               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
10977                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
10978                   && operand_equal_p (arg00, arg01, 0))
10979                 {
10980                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
10981
10982                   if (cosfn != NULL_TREE)
10983                     return build_call_expr (cosfn, 1, arg00);
10984                 }
10985             }
10986
10987           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
10988              NaNs or Infinities.  */
10989           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
10990                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
10991                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
10992             {
10993               tree arg00 = CALL_EXPR_ARG (arg0, 0);
10994               tree arg01 = CALL_EXPR_ARG (arg1, 0);
10995
10996               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
10997                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
10998                   && operand_equal_p (arg00, arg01, 0))
10999                 {
11000                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11001
11002                   if (cosfn != NULL_TREE)
11003                     {
11004                       tree tmp = build_call_expr (cosfn, 1, arg00);
11005                       return fold_build2 (RDIV_EXPR, type,
11006                                           build_real (type, dconst1),
11007                                           tmp);
11008                     }
11009                 }
11010             }
11011
11012           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11013           if (fcode0 == BUILT_IN_POW
11014               || fcode0 == BUILT_IN_POWF
11015               || fcode0 == BUILT_IN_POWL)
11016             {
11017               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11018               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11019               if (TREE_CODE (arg01) == REAL_CST
11020                   && !TREE_OVERFLOW (arg01)
11021                   && operand_equal_p (arg1, arg00, 0))
11022                 {
11023                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11024                   REAL_VALUE_TYPE c;
11025                   tree arg;
11026
11027                   c = TREE_REAL_CST (arg01);
11028                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11029                   arg = build_real (type, c);
11030                   return build_call_expr (powfn, 2, arg1, arg);
11031                 }
11032             }
11033
11034           /* Optimize a/root(b/c) into a*root(c/b).  */
11035           if (BUILTIN_ROOT_P (fcode1))
11036             {
11037               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11038
11039               if (TREE_CODE (rootarg) == RDIV_EXPR)
11040                 {
11041                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11042                   tree b = TREE_OPERAND (rootarg, 0);
11043                   tree c = TREE_OPERAND (rootarg, 1);
11044
11045                   tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11046
11047                   tmp = build_call_expr (rootfn, 1, tmp);
11048                   return fold_build2 (MULT_EXPR, type, arg0, tmp);
11049                 }
11050             }
11051
11052           /* Optimize x/expN(y) into x*expN(-y).  */
11053           if (BUILTIN_EXPONENT_P (fcode1))
11054             {
11055               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11056               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11057               arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11058               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11059             }
11060
11061           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11062           if (fcode1 == BUILT_IN_POW
11063               || fcode1 == BUILT_IN_POWF
11064               || fcode1 == BUILT_IN_POWL)
11065             {
11066               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11067               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11068               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11069               tree neg11 = fold_convert (type, negate_expr (arg11));
11070               arg1 = build_call_expr (powfn, 2, arg10, neg11);
11071               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11072             }
11073         }
11074       return NULL_TREE;
11075
11076     case TRUNC_DIV_EXPR:
11077     case FLOOR_DIV_EXPR:
11078       /* Simplify A / (B << N) where A and B are positive and B is
11079          a power of 2, to A >> (N + log2(B)).  */
11080       strict_overflow_p = false;
11081       if (TREE_CODE (arg1) == LSHIFT_EXPR
11082           && (TYPE_UNSIGNED (type)
11083               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11084         {
11085           tree sval = TREE_OPERAND (arg1, 0);
11086           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11087             {
11088               tree sh_cnt = TREE_OPERAND (arg1, 1);
11089               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11090
11091               if (strict_overflow_p)
11092                 fold_overflow_warning (("assuming signed overflow does not "
11093                                         "occur when simplifying A / (B << N)"),
11094                                        WARN_STRICT_OVERFLOW_MISC);
11095
11096               sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11097                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
11098               return fold_build2 (RSHIFT_EXPR, type,
11099                                   fold_convert (type, arg0), sh_cnt);
11100             }
11101         }
11102
11103       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11104          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11105       if (INTEGRAL_TYPE_P (type)
11106           && TYPE_UNSIGNED (type)
11107           && code == FLOOR_DIV_EXPR)
11108         return fold_build2 (TRUNC_DIV_EXPR, type, op0, op1);
11109
11110       /* Fall thru */
11111
11112     case ROUND_DIV_EXPR:
11113     case CEIL_DIV_EXPR:
11114     case EXACT_DIV_EXPR:
11115       if (integer_onep (arg1))
11116         return non_lvalue (fold_convert (type, arg0));
11117       if (integer_zerop (arg1))
11118         return NULL_TREE;
11119       /* X / -1 is -X.  */
11120       if (!TYPE_UNSIGNED (type)
11121           && TREE_CODE (arg1) == INTEGER_CST
11122           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11123           && TREE_INT_CST_HIGH (arg1) == -1)
11124         return fold_convert (type, negate_expr (arg0));
11125
11126       /* Convert -A / -B to A / B when the type is signed and overflow is
11127          undefined.  */
11128       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11129           && TREE_CODE (arg0) == NEGATE_EXPR
11130           && negate_expr_p (arg1))
11131         {
11132           if (INTEGRAL_TYPE_P (type))
11133             fold_overflow_warning (("assuming signed overflow does not occur "
11134                                     "when distributing negation across "
11135                                     "division"),
11136                                    WARN_STRICT_OVERFLOW_MISC);
11137           return fold_build2 (code, type,
11138                               fold_convert (type, TREE_OPERAND (arg0, 0)),
11139                               negate_expr (arg1));
11140         }
11141       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11142           && TREE_CODE (arg1) == NEGATE_EXPR
11143           && negate_expr_p (arg0))
11144         {
11145           if (INTEGRAL_TYPE_P (type))
11146             fold_overflow_warning (("assuming signed overflow does not occur "
11147                                     "when distributing negation across "
11148                                     "division"),
11149                                    WARN_STRICT_OVERFLOW_MISC);
11150           return fold_build2 (code, type, negate_expr (arg0),
11151                               TREE_OPERAND (arg1, 0));
11152         }
11153
11154       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11155          operation, EXACT_DIV_EXPR.
11156
11157          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11158          At one time others generated faster code, it's not clear if they do
11159          after the last round to changes to the DIV code in expmed.c.  */
11160       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11161           && multiple_of_p (type, arg0, arg1))
11162         return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11163
11164       strict_overflow_p = false;
11165       if (TREE_CODE (arg1) == INTEGER_CST
11166           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11167                                          &strict_overflow_p)))
11168         {
11169           if (strict_overflow_p)
11170             fold_overflow_warning (("assuming signed overflow does not occur "
11171                                     "when simplifying division"),
11172                                    WARN_STRICT_OVERFLOW_MISC);
11173           return fold_convert (type, tem);
11174         }
11175
11176       return NULL_TREE;
11177
11178     case CEIL_MOD_EXPR:
11179     case FLOOR_MOD_EXPR:
11180     case ROUND_MOD_EXPR:
11181     case TRUNC_MOD_EXPR:
11182       /* X % 1 is always zero, but be sure to preserve any side
11183          effects in X.  */
11184       if (integer_onep (arg1))
11185         return omit_one_operand (type, integer_zero_node, arg0);
11186
11187       /* X % 0, return X % 0 unchanged so that we can get the
11188          proper warnings and errors.  */
11189       if (integer_zerop (arg1))
11190         return NULL_TREE;
11191
11192       /* 0 % X is always zero, but be sure to preserve any side
11193          effects in X.  Place this after checking for X == 0.  */
11194       if (integer_zerop (arg0))
11195         return omit_one_operand (type, integer_zero_node, arg1);
11196
11197       /* X % -1 is zero.  */
11198       if (!TYPE_UNSIGNED (type)
11199           && TREE_CODE (arg1) == INTEGER_CST
11200           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11201           && TREE_INT_CST_HIGH (arg1) == -1)
11202         return omit_one_operand (type, integer_zero_node, arg0);
11203
11204       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11205          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11206       strict_overflow_p = false;
11207       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11208           && (TYPE_UNSIGNED (type)
11209               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11210         {
11211           tree c = arg1;
11212           /* Also optimize A % (C << N)  where C is a power of 2,
11213              to A & ((C << N) - 1).  */
11214           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11215             c = TREE_OPERAND (arg1, 0);
11216
11217           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11218             {
11219               tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11220                                        build_int_cst (TREE_TYPE (arg1), 1));
11221               if (strict_overflow_p)
11222                 fold_overflow_warning (("assuming signed overflow does not "
11223                                         "occur when simplifying "
11224                                         "X % (power of two)"),
11225                                        WARN_STRICT_OVERFLOW_MISC);
11226               return fold_build2 (BIT_AND_EXPR, type,
11227                                   fold_convert (type, arg0),
11228                                   fold_convert (type, mask));
11229             }
11230         }
11231
11232       /* X % -C is the same as X % C.  */
11233       if (code == TRUNC_MOD_EXPR
11234           && !TYPE_UNSIGNED (type)
11235           && TREE_CODE (arg1) == INTEGER_CST
11236           && !TREE_OVERFLOW (arg1)
11237           && TREE_INT_CST_HIGH (arg1) < 0
11238           && !TYPE_OVERFLOW_TRAPS (type)
11239           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11240           && !sign_bit_p (arg1, arg1))
11241         return fold_build2 (code, type, fold_convert (type, arg0),
11242                             fold_convert (type, negate_expr (arg1)));
11243
11244       /* X % -Y is the same as X % Y.  */
11245       if (code == TRUNC_MOD_EXPR
11246           && !TYPE_UNSIGNED (type)
11247           && TREE_CODE (arg1) == NEGATE_EXPR
11248           && !TYPE_OVERFLOW_TRAPS (type))
11249         return fold_build2 (code, type, fold_convert (type, arg0),
11250                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11251
11252       if (TREE_CODE (arg1) == INTEGER_CST
11253           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11254                                          &strict_overflow_p)))
11255         {
11256           if (strict_overflow_p)
11257             fold_overflow_warning (("assuming signed overflow does not occur "
11258                                     "when simplifying modulos"),
11259                                    WARN_STRICT_OVERFLOW_MISC);
11260           return fold_convert (type, tem);
11261         }
11262
11263       return NULL_TREE;
11264
11265     case LROTATE_EXPR:
11266     case RROTATE_EXPR:
11267       if (integer_all_onesp (arg0))
11268         return omit_one_operand (type, arg0, arg1);
11269       goto shift;
11270
11271     case RSHIFT_EXPR:
11272       /* Optimize -1 >> x for arithmetic right shifts.  */
11273       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
11274         return omit_one_operand (type, arg0, arg1);
11275       /* ... fall through ...  */
11276
11277     case LSHIFT_EXPR:
11278     shift:
11279       if (integer_zerop (arg1))
11280         return non_lvalue (fold_convert (type, arg0));
11281       if (integer_zerop (arg0))
11282         return omit_one_operand (type, arg0, arg1);
11283
11284       /* Since negative shift count is not well-defined,
11285          don't try to compute it in the compiler.  */
11286       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11287         return NULL_TREE;
11288
11289       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11290       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11291           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11292           && host_integerp (TREE_OPERAND (arg0, 1), false)
11293           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11294         {
11295           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11296                                + TREE_INT_CST_LOW (arg1));
11297
11298           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11299              being well defined.  */
11300           if (low >= TYPE_PRECISION (type))
11301             {
11302               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11303                 low = low % TYPE_PRECISION (type);
11304               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11305                 return build_int_cst (type, 0);
11306               else
11307                 low = TYPE_PRECISION (type) - 1;
11308             }
11309
11310           return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11311                               build_int_cst (type, low));
11312         }
11313
11314       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11315          into x & ((unsigned)-1 >> c) for unsigned types.  */
11316       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11317            || (TYPE_UNSIGNED (type)
11318                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11319           && host_integerp (arg1, false)
11320           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11321           && host_integerp (TREE_OPERAND (arg0, 1), false)
11322           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11323         {
11324           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11325           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11326           tree lshift;
11327           tree arg00;
11328
11329           if (low0 == low1)
11330             {
11331               arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11332
11333               lshift = build_int_cst (type, -1);
11334               lshift = int_const_binop (code, lshift, arg1, 0);
11335
11336               return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11337             }
11338         }
11339
11340       /* Rewrite an LROTATE_EXPR by a constant into an
11341          RROTATE_EXPR by a new constant.  */
11342       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11343         {
11344           tree tem = build_int_cst (TREE_TYPE (arg1),
11345                                     TYPE_PRECISION (type));
11346           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11347           return fold_build2 (RROTATE_EXPR, type, op0, tem);
11348         }
11349
11350       /* If we have a rotate of a bit operation with the rotate count and
11351          the second operand of the bit operation both constant,
11352          permute the two operations.  */
11353       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11354           && (TREE_CODE (arg0) == BIT_AND_EXPR
11355               || TREE_CODE (arg0) == BIT_IOR_EXPR
11356               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11357           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11358         return fold_build2 (TREE_CODE (arg0), type,
11359                             fold_build2 (code, type,
11360                                          TREE_OPERAND (arg0, 0), arg1),
11361                             fold_build2 (code, type,
11362                                          TREE_OPERAND (arg0, 1), arg1));
11363
11364       /* Two consecutive rotates adding up to the precision of the
11365          type can be ignored.  */
11366       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11367           && TREE_CODE (arg0) == RROTATE_EXPR
11368           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11369           && TREE_INT_CST_HIGH (arg1) == 0
11370           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11371           && ((TREE_INT_CST_LOW (arg1)
11372                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11373               == (unsigned int) TYPE_PRECISION (type)))
11374         return TREE_OPERAND (arg0, 0);
11375
11376       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11377               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11378          if the latter can be further optimized.  */
11379       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11380           && TREE_CODE (arg0) == BIT_AND_EXPR
11381           && TREE_CODE (arg1) == INTEGER_CST
11382           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11383         {
11384           tree mask = fold_build2 (code, type,
11385                                    fold_convert (type, TREE_OPERAND (arg0, 1)),
11386                                    arg1);
11387           tree shift = fold_build2 (code, type,
11388                                     fold_convert (type, TREE_OPERAND (arg0, 0)),
11389                                     arg1);
11390           tem = fold_binary (BIT_AND_EXPR, type, shift, mask);
11391           if (tem)
11392             return tem;
11393         }
11394
11395       return NULL_TREE;
11396
11397     case MIN_EXPR:
11398       if (operand_equal_p (arg0, arg1, 0))
11399         return omit_one_operand (type, arg0, arg1);
11400       if (INTEGRAL_TYPE_P (type)
11401           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11402         return omit_one_operand (type, arg1, arg0);
11403       tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11404       if (tem)
11405         return tem;
11406       goto associate;
11407
11408     case MAX_EXPR:
11409       if (operand_equal_p (arg0, arg1, 0))
11410         return omit_one_operand (type, arg0, arg1);
11411       if (INTEGRAL_TYPE_P (type)
11412           && TYPE_MAX_VALUE (type)
11413           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11414         return omit_one_operand (type, arg1, arg0);
11415       tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11416       if (tem)
11417         return tem;
11418       goto associate;
11419
11420     case TRUTH_ANDIF_EXPR:
11421       /* Note that the operands of this must be ints
11422          and their values must be 0 or 1.
11423          ("true" is a fixed value perhaps depending on the language.)  */
11424       /* If first arg is constant zero, return it.  */
11425       if (integer_zerop (arg0))
11426         return fold_convert (type, arg0);
11427     case TRUTH_AND_EXPR:
11428       /* If either arg is constant true, drop it.  */
11429       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11430         return non_lvalue (fold_convert (type, arg1));
11431       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11432           /* Preserve sequence points.  */
11433           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11434         return non_lvalue (fold_convert (type, arg0));
11435       /* If second arg is constant zero, result is zero, but first arg
11436          must be evaluated.  */
11437       if (integer_zerop (arg1))
11438         return omit_one_operand (type, arg1, arg0);
11439       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11440          case will be handled here.  */
11441       if (integer_zerop (arg0))
11442         return omit_one_operand (type, arg0, arg1);
11443
11444       /* !X && X is always false.  */
11445       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11446           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11447         return omit_one_operand (type, integer_zero_node, arg1);
11448       /* X && !X is always false.  */
11449       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11450           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11451         return omit_one_operand (type, integer_zero_node, arg0);
11452
11453       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
11454          means A >= Y && A != MAX, but in this case we know that
11455          A < X <= MAX.  */
11456
11457       if (!TREE_SIDE_EFFECTS (arg0)
11458           && !TREE_SIDE_EFFECTS (arg1))
11459         {
11460           tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
11461           if (tem && !operand_equal_p (tem, arg0, 0))
11462             return fold_build2 (code, type, tem, arg1);
11463
11464           tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
11465           if (tem && !operand_equal_p (tem, arg1, 0))
11466             return fold_build2 (code, type, arg0, tem);
11467         }
11468
11469     truth_andor:
11470       /* We only do these simplifications if we are optimizing.  */
11471       if (!optimize)
11472         return NULL_TREE;
11473
11474       /* Check for things like (A || B) && (A || C).  We can convert this
11475          to A || (B && C).  Note that either operator can be any of the four
11476          truth and/or operations and the transformation will still be
11477          valid.   Also note that we only care about order for the
11478          ANDIF and ORIF operators.  If B contains side effects, this
11479          might change the truth-value of A.  */
11480       if (TREE_CODE (arg0) == TREE_CODE (arg1)
11481           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11482               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11483               || TREE_CODE (arg0) == TRUTH_AND_EXPR
11484               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11485           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11486         {
11487           tree a00 = TREE_OPERAND (arg0, 0);
11488           tree a01 = TREE_OPERAND (arg0, 1);
11489           tree a10 = TREE_OPERAND (arg1, 0);
11490           tree a11 = TREE_OPERAND (arg1, 1);
11491           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11492                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11493                              && (code == TRUTH_AND_EXPR
11494                                  || code == TRUTH_OR_EXPR));
11495
11496           if (operand_equal_p (a00, a10, 0))
11497             return fold_build2 (TREE_CODE (arg0), type, a00,
11498                                 fold_build2 (code, type, a01, a11));
11499           else if (commutative && operand_equal_p (a00, a11, 0))
11500             return fold_build2 (TREE_CODE (arg0), type, a00,
11501                                 fold_build2 (code, type, a01, a10));
11502           else if (commutative && operand_equal_p (a01, a10, 0))
11503             return fold_build2 (TREE_CODE (arg0), type, a01,
11504                                 fold_build2 (code, type, a00, a11));
11505
11506           /* This case if tricky because we must either have commutative
11507              operators or else A10 must not have side-effects.  */
11508
11509           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
11510                    && operand_equal_p (a01, a11, 0))
11511             return fold_build2 (TREE_CODE (arg0), type,
11512                                 fold_build2 (code, type, a00, a10),
11513                                 a01);
11514         }
11515
11516       /* See if we can build a range comparison.  */
11517       if (0 != (tem = fold_range_test (code, type, op0, op1)))
11518         return tem;
11519
11520       /* Check for the possibility of merging component references.  If our
11521          lhs is another similar operation, try to merge its rhs with our
11522          rhs.  Then try to merge our lhs and rhs.  */
11523       if (TREE_CODE (arg0) == code
11524           && 0 != (tem = fold_truthop (code, type,
11525                                        TREE_OPERAND (arg0, 1), arg1)))
11526         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11527
11528       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
11529         return tem;
11530
11531       return NULL_TREE;
11532
11533     case TRUTH_ORIF_EXPR:
11534       /* Note that the operands of this must be ints
11535          and their values must be 0 or true.
11536          ("true" is a fixed value perhaps depending on the language.)  */
11537       /* If first arg is constant true, return it.  */
11538       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11539         return fold_convert (type, arg0);
11540     case TRUTH_OR_EXPR:
11541       /* If either arg is constant zero, drop it.  */
11542       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
11543         return non_lvalue (fold_convert (type, arg1));
11544       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
11545           /* Preserve sequence points.  */
11546           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11547         return non_lvalue (fold_convert (type, arg0));
11548       /* If second arg is constant true, result is true, but we must
11549          evaluate first arg.  */
11550       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
11551         return omit_one_operand (type, arg1, arg0);
11552       /* Likewise for first arg, but note this only occurs here for
11553          TRUTH_OR_EXPR.  */
11554       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11555         return omit_one_operand (type, arg0, arg1);
11556
11557       /* !X || X is always true.  */
11558       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11559           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11560         return omit_one_operand (type, integer_one_node, arg1);
11561       /* X || !X is always true.  */
11562       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11563           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11564         return omit_one_operand (type, integer_one_node, arg0);
11565
11566       goto truth_andor;
11567
11568     case TRUTH_XOR_EXPR:
11569       /* If the second arg is constant zero, drop it.  */
11570       if (integer_zerop (arg1))
11571         return non_lvalue (fold_convert (type, arg0));
11572       /* If the second arg is constant true, this is a logical inversion.  */
11573       if (integer_onep (arg1))
11574         {
11575           /* Only call invert_truthvalue if operand is a truth value.  */
11576           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
11577             tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
11578           else
11579             tem = invert_truthvalue (arg0);
11580           return non_lvalue (fold_convert (type, tem));
11581         }
11582       /* Identical arguments cancel to zero.  */
11583       if (operand_equal_p (arg0, arg1, 0))
11584         return omit_one_operand (type, integer_zero_node, arg0);
11585
11586       /* !X ^ X is always true.  */
11587       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11588           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11589         return omit_one_operand (type, integer_one_node, arg1);
11590
11591       /* X ^ !X is always true.  */
11592       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11593           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11594         return omit_one_operand (type, integer_one_node, arg0);
11595
11596       return NULL_TREE;
11597
11598     case EQ_EXPR:
11599     case NE_EXPR:
11600       tem = fold_comparison (code, type, op0, op1);
11601       if (tem != NULL_TREE)
11602         return tem;
11603
11604       /* bool_var != 0 becomes bool_var. */
11605       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11606           && code == NE_EXPR)
11607         return non_lvalue (fold_convert (type, arg0));
11608
11609       /* bool_var == 1 becomes bool_var. */
11610       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11611           && code == EQ_EXPR)
11612         return non_lvalue (fold_convert (type, arg0));
11613
11614       /* bool_var != 1 becomes !bool_var. */
11615       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11616           && code == NE_EXPR)
11617         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
11618
11619       /* bool_var == 0 becomes !bool_var. */
11620       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11621           && code == EQ_EXPR)
11622         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
11623
11624       /* If this is an equality comparison of the address of two non-weak,
11625          unaliased symbols neither of which are extern (since we do not
11626          have access to attributes for externs), then we know the result.  */
11627       if (TREE_CODE (arg0) == ADDR_EXPR
11628           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
11629           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
11630           && ! lookup_attribute ("alias",
11631                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
11632           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
11633           && TREE_CODE (arg1) == ADDR_EXPR
11634           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
11635           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
11636           && ! lookup_attribute ("alias",
11637                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
11638           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
11639         {
11640           /* We know that we're looking at the address of two
11641              non-weak, unaliased, static _DECL nodes.
11642
11643              It is both wasteful and incorrect to call operand_equal_p
11644              to compare the two ADDR_EXPR nodes.  It is wasteful in that
11645              all we need to do is test pointer equality for the arguments
11646              to the two ADDR_EXPR nodes.  It is incorrect to use
11647              operand_equal_p as that function is NOT equivalent to a
11648              C equality test.  It can in fact return false for two
11649              objects which would test as equal using the C equality
11650              operator.  */
11651           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
11652           return constant_boolean_node (equal
11653                                         ? code == EQ_EXPR : code != EQ_EXPR,
11654                                         type);
11655         }
11656
11657       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
11658          a MINUS_EXPR of a constant, we can convert it into a comparison with
11659          a revised constant as long as no overflow occurs.  */
11660       if (TREE_CODE (arg1) == INTEGER_CST
11661           && (TREE_CODE (arg0) == PLUS_EXPR
11662               || TREE_CODE (arg0) == MINUS_EXPR)
11663           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11664           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
11665                                       ? MINUS_EXPR : PLUS_EXPR,
11666                                       fold_convert (TREE_TYPE (arg0), arg1),
11667                                       TREE_OPERAND (arg0, 1), 0))
11668           && !TREE_OVERFLOW (tem))
11669         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11670
11671       /* Similarly for a NEGATE_EXPR.  */
11672       if (TREE_CODE (arg0) == NEGATE_EXPR
11673           && TREE_CODE (arg1) == INTEGER_CST
11674           && 0 != (tem = negate_expr (arg1))
11675           && TREE_CODE (tem) == INTEGER_CST
11676           && !TREE_OVERFLOW (tem))
11677         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11678
11679       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
11680       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11681           && TREE_CODE (arg1) == INTEGER_CST
11682           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11683         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11684                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
11685                                          fold_convert (TREE_TYPE (arg0), arg1),
11686                                          TREE_OPERAND (arg0, 1)));
11687
11688       /* Transform comparisons of the form X +- C CMP X.  */
11689       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11690           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11691           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11692           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11693               || POINTER_TYPE_P (TREE_TYPE (arg0))))
11694         {
11695           tree cst = TREE_OPERAND (arg0, 1);
11696
11697           if (code == EQ_EXPR
11698               && !integer_zerop (cst))
11699             return omit_two_operands (type, boolean_false_node,
11700                                       TREE_OPERAND (arg0, 0), arg1);
11701           else
11702             return omit_two_operands (type, boolean_true_node,
11703                                       TREE_OPERAND (arg0, 0), arg1);
11704         }
11705
11706       /* If we have X - Y == 0, we can convert that to X == Y and similarly
11707          for !=.  Don't do this for ordered comparisons due to overflow.  */
11708       if (TREE_CODE (arg0) == MINUS_EXPR
11709           && integer_zerop (arg1))
11710         return fold_build2 (code, type,
11711                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
11712
11713       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
11714       if (TREE_CODE (arg0) == ABS_EXPR
11715           && (integer_zerop (arg1) || real_zerop (arg1)))
11716         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
11717
11718       /* If this is an EQ or NE comparison with zero and ARG0 is
11719          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
11720          two operations, but the latter can be done in one less insn
11721          on machines that have only two-operand insns or on which a
11722          constant cannot be the first operand.  */
11723       if (TREE_CODE (arg0) == BIT_AND_EXPR
11724           && integer_zerop (arg1))
11725         {
11726           tree arg00 = TREE_OPERAND (arg0, 0);
11727           tree arg01 = TREE_OPERAND (arg0, 1);
11728           if (TREE_CODE (arg00) == LSHIFT_EXPR
11729               && integer_onep (TREE_OPERAND (arg00, 0)))
11730             {
11731               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
11732                                       arg01, TREE_OPERAND (arg00, 1));
11733               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
11734                                  build_int_cst (TREE_TYPE (arg0), 1));
11735               return fold_build2 (code, type,
11736                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
11737             }
11738           else if (TREE_CODE (arg01) == LSHIFT_EXPR
11739                    && integer_onep (TREE_OPERAND (arg01, 0)))
11740             {
11741               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
11742                                       arg00, TREE_OPERAND (arg01, 1));
11743               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
11744                                  build_int_cst (TREE_TYPE (arg0), 1));
11745               return fold_build2 (code, type,
11746                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
11747             }
11748         }
11749
11750       /* If this is an NE or EQ comparison of zero against the result of a
11751          signed MOD operation whose second operand is a power of 2, make
11752          the MOD operation unsigned since it is simpler and equivalent.  */
11753       if (integer_zerop (arg1)
11754           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
11755           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
11756               || TREE_CODE (arg0) == CEIL_MOD_EXPR
11757               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
11758               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
11759           && integer_pow2p (TREE_OPERAND (arg0, 1)))
11760         {
11761           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
11762           tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
11763                                      fold_convert (newtype,
11764                                                    TREE_OPERAND (arg0, 0)),
11765                                      fold_convert (newtype,
11766                                                    TREE_OPERAND (arg0, 1)));
11767
11768           return fold_build2 (code, type, newmod,
11769                               fold_convert (newtype, arg1));
11770         }
11771
11772       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
11773          C1 is a valid shift constant, and C2 is a power of two, i.e.
11774          a single bit.  */
11775       if (TREE_CODE (arg0) == BIT_AND_EXPR
11776           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
11777           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
11778              == INTEGER_CST
11779           && integer_pow2p (TREE_OPERAND (arg0, 1))
11780           && integer_zerop (arg1))
11781         {
11782           tree itype = TREE_TYPE (arg0);
11783           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
11784           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
11785
11786           /* Check for a valid shift count.  */
11787           if (TREE_INT_CST_HIGH (arg001) == 0
11788               && TREE_INT_CST_LOW (arg001) < prec)
11789             {
11790               tree arg01 = TREE_OPERAND (arg0, 1);
11791               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
11792               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
11793               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
11794                  can be rewritten as (X & (C2 << C1)) != 0.  */
11795               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
11796                 {
11797                   tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
11798                   tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
11799                   return fold_build2 (code, type, tem, arg1);
11800                 }
11801               /* Otherwise, for signed (arithmetic) shifts,
11802                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
11803                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
11804               else if (!TYPE_UNSIGNED (itype))
11805                 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
11806                                     arg000, build_int_cst (itype, 0));
11807               /* Otherwise, of unsigned (logical) shifts,
11808                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
11809                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
11810               else
11811                 return omit_one_operand (type,
11812                                          code == EQ_EXPR ? integer_one_node
11813                                                          : integer_zero_node,
11814                                          arg000);
11815             }
11816         }
11817
11818       /* If this is an NE comparison of zero with an AND of one, remove the
11819          comparison since the AND will give the correct value.  */
11820       if (code == NE_EXPR
11821           && integer_zerop (arg1)
11822           && TREE_CODE (arg0) == BIT_AND_EXPR
11823           && integer_onep (TREE_OPERAND (arg0, 1)))
11824         return fold_convert (type, arg0);
11825
11826       /* If we have (A & C) == C where C is a power of 2, convert this into
11827          (A & C) != 0.  Similarly for NE_EXPR.  */
11828       if (TREE_CODE (arg0) == BIT_AND_EXPR
11829           && integer_pow2p (TREE_OPERAND (arg0, 1))
11830           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11831         return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11832                             arg0, fold_convert (TREE_TYPE (arg0),
11833                                                 integer_zero_node));
11834
11835       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
11836          bit, then fold the expression into A < 0 or A >= 0.  */
11837       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
11838       if (tem)
11839         return tem;
11840
11841       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
11842          Similarly for NE_EXPR.  */
11843       if (TREE_CODE (arg0) == BIT_AND_EXPR
11844           && TREE_CODE (arg1) == INTEGER_CST
11845           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11846         {
11847           tree notc = fold_build1 (BIT_NOT_EXPR,
11848                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
11849                                    TREE_OPERAND (arg0, 1));
11850           tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11851                                        arg1, notc);
11852           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
11853           if (integer_nonzerop (dandnotc))
11854             return omit_one_operand (type, rslt, arg0);
11855         }
11856
11857       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
11858          Similarly for NE_EXPR.  */
11859       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11860           && TREE_CODE (arg1) == INTEGER_CST
11861           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11862         {
11863           tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
11864           tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11865                                        TREE_OPERAND (arg0, 1), notd);
11866           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
11867           if (integer_nonzerop (candnotd))
11868             return omit_one_operand (type, rslt, arg0);
11869         }
11870
11871       /* Optimize comparisons of strlen vs zero to a compare of the
11872          first character of the string vs zero.  To wit,
11873                 strlen(ptr) == 0   =>  *ptr == 0
11874                 strlen(ptr) != 0   =>  *ptr != 0
11875          Other cases should reduce to one of these two (or a constant)
11876          due to the return value of strlen being unsigned.  */
11877       if (TREE_CODE (arg0) == CALL_EXPR
11878           && integer_zerop (arg1))
11879         {
11880           tree fndecl = get_callee_fndecl (arg0);
11881
11882           if (fndecl
11883               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
11884               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
11885               && call_expr_nargs (arg0) == 1
11886               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
11887             {
11888               tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
11889               return fold_build2 (code, type, iref,
11890                                   build_int_cst (TREE_TYPE (iref), 0));
11891             }
11892         }
11893
11894       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
11895          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
11896       if (TREE_CODE (arg0) == RSHIFT_EXPR
11897           && integer_zerop (arg1)
11898           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11899         {
11900           tree arg00 = TREE_OPERAND (arg0, 0);
11901           tree arg01 = TREE_OPERAND (arg0, 1);
11902           tree itype = TREE_TYPE (arg00);
11903           if (TREE_INT_CST_HIGH (arg01) == 0
11904               && TREE_INT_CST_LOW (arg01)
11905                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
11906             {
11907               if (TYPE_UNSIGNED (itype))
11908                 {
11909                   itype = signed_type_for (itype);
11910                   arg00 = fold_convert (itype, arg00);
11911                 }
11912               return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
11913                                   type, arg00, build_int_cst (itype, 0));
11914             }
11915         }
11916
11917       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
11918       if (integer_zerop (arg1)
11919           && TREE_CODE (arg0) == BIT_XOR_EXPR)
11920         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11921                             TREE_OPERAND (arg0, 1));
11922
11923       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
11924       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11925           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11926         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11927                             build_int_cst (TREE_TYPE (arg1), 0));
11928       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
11929       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11930           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11931           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11932         return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
11933                             build_int_cst (TREE_TYPE (arg1), 0));
11934
11935       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
11936       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11937           && TREE_CODE (arg1) == INTEGER_CST
11938           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11939         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11940                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
11941                                          TREE_OPERAND (arg0, 1), arg1));
11942
11943       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
11944          (X & C) == 0 when C is a single bit.  */
11945       if (TREE_CODE (arg0) == BIT_AND_EXPR
11946           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
11947           && integer_zerop (arg1)
11948           && integer_pow2p (TREE_OPERAND (arg0, 1)))
11949         {
11950           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11951                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
11952                              TREE_OPERAND (arg0, 1));
11953           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
11954                               type, tem, arg1);
11955         }
11956
11957       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
11958          constant C is a power of two, i.e. a single bit.  */
11959       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11960           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11961           && integer_zerop (arg1)
11962           && integer_pow2p (TREE_OPERAND (arg0, 1))
11963           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11964                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
11965         {
11966           tree arg00 = TREE_OPERAND (arg0, 0);
11967           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11968                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
11969         }
11970
11971       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
11972          when is C is a power of two, i.e. a single bit.  */
11973       if (TREE_CODE (arg0) == BIT_AND_EXPR
11974           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
11975           && integer_zerop (arg1)
11976           && integer_pow2p (TREE_OPERAND (arg0, 1))
11977           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11978                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
11979         {
11980           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
11981           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
11982                              arg000, TREE_OPERAND (arg0, 1));
11983           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11984                               tem, build_int_cst (TREE_TYPE (tem), 0));
11985         }
11986
11987       if (integer_zerop (arg1)
11988           && tree_expr_nonzero_p (arg0))
11989         {
11990           tree res = constant_boolean_node (code==NE_EXPR, type);
11991           return omit_one_operand (type, res, arg0);
11992         }
11993
11994       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
11995       if (TREE_CODE (arg0) == NEGATE_EXPR
11996           && TREE_CODE (arg1) == NEGATE_EXPR)
11997         return fold_build2 (code, type,
11998                             TREE_OPERAND (arg0, 0),
11999                             TREE_OPERAND (arg1, 0));
12000
12001       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12002       if (TREE_CODE (arg0) == BIT_AND_EXPR
12003           && TREE_CODE (arg1) == BIT_AND_EXPR)
12004         {
12005           tree arg00 = TREE_OPERAND (arg0, 0);
12006           tree arg01 = TREE_OPERAND (arg0, 1);
12007           tree arg10 = TREE_OPERAND (arg1, 0);
12008           tree arg11 = TREE_OPERAND (arg1, 1);
12009           tree itype = TREE_TYPE (arg0);
12010
12011           if (operand_equal_p (arg01, arg11, 0))
12012             return fold_build2 (code, type,
12013                                 fold_build2 (BIT_AND_EXPR, itype,
12014                                              fold_build2 (BIT_XOR_EXPR, itype,
12015                                                           arg00, arg10),
12016                                              arg01),
12017                                 build_int_cst (itype, 0));
12018
12019           if (operand_equal_p (arg01, arg10, 0))
12020             return fold_build2 (code, type,
12021                                 fold_build2 (BIT_AND_EXPR, itype,
12022                                              fold_build2 (BIT_XOR_EXPR, itype,
12023                                                           arg00, arg11),
12024                                              arg01),
12025                                 build_int_cst (itype, 0));
12026
12027           if (operand_equal_p (arg00, arg11, 0))
12028             return fold_build2 (code, type,
12029                                 fold_build2 (BIT_AND_EXPR, itype,
12030                                              fold_build2 (BIT_XOR_EXPR, itype,
12031                                                           arg01, arg10),
12032                                              arg00),
12033                                 build_int_cst (itype, 0));
12034
12035           if (operand_equal_p (arg00, arg10, 0))
12036             return fold_build2 (code, type,
12037                                 fold_build2 (BIT_AND_EXPR, itype,
12038                                              fold_build2 (BIT_XOR_EXPR, itype,
12039                                                           arg01, arg11),
12040                                              arg00),
12041                                 build_int_cst (itype, 0));
12042         }
12043
12044       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12045           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12046         {
12047           tree arg00 = TREE_OPERAND (arg0, 0);
12048           tree arg01 = TREE_OPERAND (arg0, 1);
12049           tree arg10 = TREE_OPERAND (arg1, 0);
12050           tree arg11 = TREE_OPERAND (arg1, 1);
12051           tree itype = TREE_TYPE (arg0);
12052
12053           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12054              operand_equal_p guarantees no side-effects so we don't need
12055              to use omit_one_operand on Z.  */
12056           if (operand_equal_p (arg01, arg11, 0))
12057             return fold_build2 (code, type, arg00, arg10);
12058           if (operand_equal_p (arg01, arg10, 0))
12059             return fold_build2 (code, type, arg00, arg11);
12060           if (operand_equal_p (arg00, arg11, 0))
12061             return fold_build2 (code, type, arg01, arg10);
12062           if (operand_equal_p (arg00, arg10, 0))
12063             return fold_build2 (code, type, arg01, arg11);
12064
12065           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12066           if (TREE_CODE (arg01) == INTEGER_CST
12067               && TREE_CODE (arg11) == INTEGER_CST)
12068             return fold_build2 (code, type,
12069                                 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12070                                              fold_build2 (BIT_XOR_EXPR, itype,
12071                                                           arg01, arg11)),
12072                                 arg10);
12073         }
12074
12075       /* Attempt to simplify equality/inequality comparisons of complex
12076          values.  Only lower the comparison if the result is known or
12077          can be simplified to a single scalar comparison.  */
12078       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12079            || TREE_CODE (arg0) == COMPLEX_CST)
12080           && (TREE_CODE (arg1) == COMPLEX_EXPR
12081               || TREE_CODE (arg1) == COMPLEX_CST))
12082         {
12083           tree real0, imag0, real1, imag1;
12084           tree rcond, icond;
12085
12086           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12087             {
12088               real0 = TREE_OPERAND (arg0, 0);
12089               imag0 = TREE_OPERAND (arg0, 1);
12090             }
12091           else
12092             {
12093               real0 = TREE_REALPART (arg0);
12094               imag0 = TREE_IMAGPART (arg0);
12095             }
12096
12097           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12098             {
12099               real1 = TREE_OPERAND (arg1, 0);
12100               imag1 = TREE_OPERAND (arg1, 1);
12101             }
12102           else
12103             {
12104               real1 = TREE_REALPART (arg1);
12105               imag1 = TREE_IMAGPART (arg1);
12106             }
12107
12108           rcond = fold_binary (code, type, real0, real1);
12109           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12110             {
12111               if (integer_zerop (rcond))
12112                 {
12113                   if (code == EQ_EXPR)
12114                     return omit_two_operands (type, boolean_false_node,
12115                                               imag0, imag1);
12116                   return fold_build2 (NE_EXPR, type, imag0, imag1);
12117                 }
12118               else
12119                 {
12120                   if (code == NE_EXPR)
12121                     return omit_two_operands (type, boolean_true_node,
12122                                               imag0, imag1);
12123                   return fold_build2 (EQ_EXPR, type, imag0, imag1);
12124                 }
12125             }
12126
12127           icond = fold_binary (code, type, imag0, imag1);
12128           if (icond && TREE_CODE (icond) == INTEGER_CST)
12129             {
12130               if (integer_zerop (icond))
12131                 {
12132                   if (code == EQ_EXPR)
12133                     return omit_two_operands (type, boolean_false_node,
12134                                               real0, real1);
12135                   return fold_build2 (NE_EXPR, type, real0, real1);
12136                 }
12137               else
12138                 {
12139                   if (code == NE_EXPR)
12140                     return omit_two_operands (type, boolean_true_node,
12141                                               real0, real1);
12142                   return fold_build2 (EQ_EXPR, type, real0, real1);
12143                 }
12144             }
12145         }
12146
12147       return NULL_TREE;
12148
12149     case LT_EXPR:
12150     case GT_EXPR:
12151     case LE_EXPR:
12152     case GE_EXPR:
12153       tem = fold_comparison (code, type, op0, op1);
12154       if (tem != NULL_TREE)
12155         return tem;
12156
12157       /* Transform comparisons of the form X +- C CMP X.  */
12158       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12159           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12160           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12161                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12162               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12163                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12164         {
12165           tree arg01 = TREE_OPERAND (arg0, 1);
12166           enum tree_code code0 = TREE_CODE (arg0);
12167           int is_positive;
12168
12169           if (TREE_CODE (arg01) == REAL_CST)
12170             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12171           else
12172             is_positive = tree_int_cst_sgn (arg01);
12173
12174           /* (X - c) > X becomes false.  */
12175           if (code == GT_EXPR
12176               && ((code0 == MINUS_EXPR && is_positive >= 0)
12177                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12178             {
12179               if (TREE_CODE (arg01) == INTEGER_CST
12180                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12181                 fold_overflow_warning (("assuming signed overflow does not "
12182                                         "occur when assuming that (X - c) > X "
12183                                         "is always false"),
12184                                        WARN_STRICT_OVERFLOW_ALL);
12185               return constant_boolean_node (0, type);
12186             }
12187
12188           /* Likewise (X + c) < X becomes false.  */
12189           if (code == LT_EXPR
12190               && ((code0 == PLUS_EXPR && is_positive >= 0)
12191                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12192             {
12193               if (TREE_CODE (arg01) == INTEGER_CST
12194                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12195                 fold_overflow_warning (("assuming signed overflow does not "
12196                                         "occur when assuming that "
12197                                         "(X + c) < X is always false"),
12198                                        WARN_STRICT_OVERFLOW_ALL);
12199               return constant_boolean_node (0, type);
12200             }
12201
12202           /* Convert (X - c) <= X to true.  */
12203           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12204               && code == LE_EXPR
12205               && ((code0 == MINUS_EXPR && is_positive >= 0)
12206                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12207             {
12208               if (TREE_CODE (arg01) == INTEGER_CST
12209                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12210                 fold_overflow_warning (("assuming signed overflow does not "
12211                                         "occur when assuming that "
12212                                         "(X - c) <= X is always true"),
12213                                        WARN_STRICT_OVERFLOW_ALL);
12214               return constant_boolean_node (1, type);
12215             }
12216
12217           /* Convert (X + c) >= X to true.  */
12218           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12219               && code == GE_EXPR
12220               && ((code0 == PLUS_EXPR && is_positive >= 0)
12221                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12222             {
12223               if (TREE_CODE (arg01) == INTEGER_CST
12224                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12225                 fold_overflow_warning (("assuming signed overflow does not "
12226                                         "occur when assuming that "
12227                                         "(X + c) >= X is always true"),
12228                                        WARN_STRICT_OVERFLOW_ALL);
12229               return constant_boolean_node (1, type);
12230             }
12231
12232           if (TREE_CODE (arg01) == INTEGER_CST)
12233             {
12234               /* Convert X + c > X and X - c < X to true for integers.  */
12235               if (code == GT_EXPR
12236                   && ((code0 == PLUS_EXPR && is_positive > 0)
12237                       || (code0 == MINUS_EXPR && is_positive < 0)))
12238                 {
12239                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12240                     fold_overflow_warning (("assuming signed overflow does "
12241                                             "not occur when assuming that "
12242                                             "(X + c) > X is always true"),
12243                                            WARN_STRICT_OVERFLOW_ALL);
12244                   return constant_boolean_node (1, type);
12245                 }
12246
12247               if (code == LT_EXPR
12248                   && ((code0 == MINUS_EXPR && is_positive > 0)
12249                       || (code0 == PLUS_EXPR && is_positive < 0)))
12250                 {
12251                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12252                     fold_overflow_warning (("assuming signed overflow does "
12253                                             "not occur when assuming that "
12254                                             "(X - c) < X is always true"),
12255                                            WARN_STRICT_OVERFLOW_ALL);
12256                   return constant_boolean_node (1, type);
12257                 }
12258
12259               /* Convert X + c <= X and X - c >= X to false for integers.  */
12260               if (code == LE_EXPR
12261                   && ((code0 == PLUS_EXPR && is_positive > 0)
12262                       || (code0 == MINUS_EXPR && is_positive < 0)))
12263                 {
12264                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12265                     fold_overflow_warning (("assuming signed overflow does "
12266                                             "not occur when assuming that "
12267                                             "(X + c) <= X is always false"),
12268                                            WARN_STRICT_OVERFLOW_ALL);
12269                   return constant_boolean_node (0, type);
12270                 }
12271
12272               if (code == GE_EXPR
12273                   && ((code0 == MINUS_EXPR && is_positive > 0)
12274                       || (code0 == PLUS_EXPR && is_positive < 0)))
12275                 {
12276                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12277                     fold_overflow_warning (("assuming signed overflow does "
12278                                             "not occur when assuming that "
12279                                             "(X - c) >= X is always false"),
12280                                            WARN_STRICT_OVERFLOW_ALL);
12281                   return constant_boolean_node (0, type);
12282                 }
12283             }
12284         }
12285
12286       /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
12287          This transformation affects the cases which are handled in later
12288          optimizations involving comparisons with non-negative constants.  */
12289       if (TREE_CODE (arg1) == INTEGER_CST
12290           && TREE_CODE (arg0) != INTEGER_CST
12291           && tree_int_cst_sgn (arg1) > 0)
12292         {
12293           if (code == GE_EXPR)
12294             {
12295               arg1 = const_binop (MINUS_EXPR, arg1,
12296                                   build_int_cst (TREE_TYPE (arg1), 1), 0);
12297               return fold_build2 (GT_EXPR, type, arg0,
12298                                   fold_convert (TREE_TYPE (arg0), arg1));
12299             }
12300           if (code == LT_EXPR)
12301             {
12302               arg1 = const_binop (MINUS_EXPR, arg1,
12303                                   build_int_cst (TREE_TYPE (arg1), 1), 0);
12304               return fold_build2 (LE_EXPR, type, arg0,
12305                                   fold_convert (TREE_TYPE (arg0), arg1));
12306             }
12307         }
12308
12309       /* Comparisons with the highest or lowest possible integer of
12310          the specified precision will have known values.  */
12311       {
12312         tree arg1_type = TREE_TYPE (arg1);
12313         unsigned int width = TYPE_PRECISION (arg1_type);
12314
12315         if (TREE_CODE (arg1) == INTEGER_CST
12316             && !TREE_OVERFLOW (arg1)
12317             && width <= 2 * HOST_BITS_PER_WIDE_INT
12318             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12319           {
12320             HOST_WIDE_INT signed_max_hi;
12321             unsigned HOST_WIDE_INT signed_max_lo;
12322             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12323
12324             if (width <= HOST_BITS_PER_WIDE_INT)
12325               {
12326                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12327                                 - 1;
12328                 signed_max_hi = 0;
12329                 max_hi = 0;
12330
12331                 if (TYPE_UNSIGNED (arg1_type))
12332                   {
12333                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12334                     min_lo = 0;
12335                     min_hi = 0;
12336                   }
12337                 else
12338                   {
12339                     max_lo = signed_max_lo;
12340                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12341                     min_hi = -1;
12342                   }
12343               }
12344             else
12345               {
12346                 width -= HOST_BITS_PER_WIDE_INT;
12347                 signed_max_lo = -1;
12348                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12349                                 - 1;
12350                 max_lo = -1;
12351                 min_lo = 0;
12352
12353                 if (TYPE_UNSIGNED (arg1_type))
12354                   {
12355                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12356                     min_hi = 0;
12357                   }
12358                 else
12359                   {
12360                     max_hi = signed_max_hi;
12361                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12362                   }
12363               }
12364
12365             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12366                 && TREE_INT_CST_LOW (arg1) == max_lo)
12367               switch (code)
12368                 {
12369                 case GT_EXPR:
12370                   return omit_one_operand (type, integer_zero_node, arg0);
12371
12372                 case GE_EXPR:
12373                   return fold_build2 (EQ_EXPR, type, op0, op1);
12374
12375                 case LE_EXPR:
12376                   return omit_one_operand (type, integer_one_node, arg0);
12377
12378                 case LT_EXPR:
12379                   return fold_build2 (NE_EXPR, type, op0, op1);
12380
12381                 /* The GE_EXPR and LT_EXPR cases above are not normally
12382                    reached because of previous transformations.  */
12383
12384                 default:
12385                   break;
12386                 }
12387             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12388                      == max_hi
12389                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12390               switch (code)
12391                 {
12392                 case GT_EXPR:
12393                   arg1 = const_binop (PLUS_EXPR, arg1,
12394                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12395                   return fold_build2 (EQ_EXPR, type,
12396                                       fold_convert (TREE_TYPE (arg1), arg0),
12397                                       arg1);
12398                 case LE_EXPR:
12399                   arg1 = const_binop (PLUS_EXPR, arg1,
12400                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12401                   return fold_build2 (NE_EXPR, type,
12402                                       fold_convert (TREE_TYPE (arg1), arg0),
12403                                       arg1);
12404                 default:
12405                   break;
12406                 }
12407             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12408                      == min_hi
12409                      && TREE_INT_CST_LOW (arg1) == min_lo)
12410               switch (code)
12411                 {
12412                 case LT_EXPR:
12413                   return omit_one_operand (type, integer_zero_node, arg0);
12414
12415                 case LE_EXPR:
12416                   return fold_build2 (EQ_EXPR, type, op0, op1);
12417
12418                 case GE_EXPR:
12419                   return omit_one_operand (type, integer_one_node, arg0);
12420
12421                 case GT_EXPR:
12422                   return fold_build2 (NE_EXPR, type, op0, op1);
12423
12424                 default:
12425                   break;
12426                 }
12427             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12428                      == min_hi
12429                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12430               switch (code)
12431                 {
12432                 case GE_EXPR:
12433                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12434                   return fold_build2 (NE_EXPR, type,
12435                                       fold_convert (TREE_TYPE (arg1), arg0),
12436                                       arg1);
12437                 case LT_EXPR:
12438                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12439                   return fold_build2 (EQ_EXPR, type,
12440                                       fold_convert (TREE_TYPE (arg1), arg0),
12441                                       arg1);
12442                 default:
12443                   break;
12444                 }
12445
12446             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12447                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
12448                      && TYPE_UNSIGNED (arg1_type)
12449                      /* We will flip the signedness of the comparison operator
12450                         associated with the mode of arg1, so the sign bit is
12451                         specified by this mode.  Check that arg1 is the signed
12452                         max associated with this sign bit.  */
12453                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12454                      /* signed_type does not work on pointer types.  */
12455                      && INTEGRAL_TYPE_P (arg1_type))
12456               {
12457                 /* The following case also applies to X < signed_max+1
12458                    and X >= signed_max+1 because previous transformations.  */
12459                 if (code == LE_EXPR || code == GT_EXPR)
12460                   {
12461                     tree st;
12462                     st = signed_type_for (TREE_TYPE (arg1));
12463                     return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
12464                                         type, fold_convert (st, arg0),
12465                                         build_int_cst (st, 0));
12466                   }
12467               }
12468           }
12469       }
12470
12471       /* If we are comparing an ABS_EXPR with a constant, we can
12472          convert all the cases into explicit comparisons, but they may
12473          well not be faster than doing the ABS and one comparison.
12474          But ABS (X) <= C is a range comparison, which becomes a subtraction
12475          and a comparison, and is probably faster.  */
12476       if (code == LE_EXPR
12477           && TREE_CODE (arg1) == INTEGER_CST
12478           && TREE_CODE (arg0) == ABS_EXPR
12479           && ! TREE_SIDE_EFFECTS (arg0)
12480           && (0 != (tem = negate_expr (arg1)))
12481           && TREE_CODE (tem) == INTEGER_CST
12482           && !TREE_OVERFLOW (tem))
12483         return fold_build2 (TRUTH_ANDIF_EXPR, type,
12484                             build2 (GE_EXPR, type,
12485                                     TREE_OPERAND (arg0, 0), tem),
12486                             build2 (LE_EXPR, type,
12487                                     TREE_OPERAND (arg0, 0), arg1));
12488
12489       /* Convert ABS_EXPR<x> >= 0 to true.  */
12490       strict_overflow_p = false;
12491       if (code == GE_EXPR
12492           && (integer_zerop (arg1)
12493               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
12494                   && real_zerop (arg1)))
12495           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12496         {
12497           if (strict_overflow_p)
12498             fold_overflow_warning (("assuming signed overflow does not occur "
12499                                     "when simplifying comparison of "
12500                                     "absolute value and zero"),
12501                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
12502           return omit_one_operand (type, integer_one_node, arg0);
12503         }
12504
12505       /* Convert ABS_EXPR<x> < 0 to false.  */
12506       strict_overflow_p = false;
12507       if (code == LT_EXPR
12508           && (integer_zerop (arg1) || real_zerop (arg1))
12509           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12510         {
12511           if (strict_overflow_p)
12512             fold_overflow_warning (("assuming signed overflow does not occur "
12513                                     "when simplifying comparison of "
12514                                     "absolute value and zero"),
12515                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
12516           return omit_one_operand (type, integer_zero_node, arg0);
12517         }
12518
12519       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12520          and similarly for >= into !=.  */
12521       if ((code == LT_EXPR || code == GE_EXPR)
12522           && TYPE_UNSIGNED (TREE_TYPE (arg0))
12523           && TREE_CODE (arg1) == LSHIFT_EXPR
12524           && integer_onep (TREE_OPERAND (arg1, 0)))
12525         return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12526                        build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12527                                TREE_OPERAND (arg1, 1)),
12528                        build_int_cst (TREE_TYPE (arg0), 0));
12529
12530       if ((code == LT_EXPR || code == GE_EXPR)
12531           && TYPE_UNSIGNED (TREE_TYPE (arg0))
12532           && (TREE_CODE (arg1) == NOP_EXPR
12533               || TREE_CODE (arg1) == CONVERT_EXPR)
12534           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12535           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12536         return
12537           build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12538                   fold_convert (TREE_TYPE (arg0),
12539                                 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12540                                         TREE_OPERAND (TREE_OPERAND (arg1, 0),
12541                                                       1))),
12542                   build_int_cst (TREE_TYPE (arg0), 0));
12543
12544       return NULL_TREE;
12545
12546     case UNORDERED_EXPR:
12547     case ORDERED_EXPR:
12548     case UNLT_EXPR:
12549     case UNLE_EXPR:
12550     case UNGT_EXPR:
12551     case UNGE_EXPR:
12552     case UNEQ_EXPR:
12553     case LTGT_EXPR:
12554       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
12555         {
12556           t1 = fold_relational_const (code, type, arg0, arg1);
12557           if (t1 != NULL_TREE)
12558             return t1;
12559         }
12560
12561       /* If the first operand is NaN, the result is constant.  */
12562       if (TREE_CODE (arg0) == REAL_CST
12563           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
12564           && (code != LTGT_EXPR || ! flag_trapping_math))
12565         {
12566           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12567                ? integer_zero_node
12568                : integer_one_node;
12569           return omit_one_operand (type, t1, arg1);
12570         }
12571
12572       /* If the second operand is NaN, the result is constant.  */
12573       if (TREE_CODE (arg1) == REAL_CST
12574           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
12575           && (code != LTGT_EXPR || ! flag_trapping_math))
12576         {
12577           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12578                ? integer_zero_node
12579                : integer_one_node;
12580           return omit_one_operand (type, t1, arg0);
12581         }
12582
12583       /* Simplify unordered comparison of something with itself.  */
12584       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
12585           && operand_equal_p (arg0, arg1, 0))
12586         return constant_boolean_node (1, type);
12587
12588       if (code == LTGT_EXPR
12589           && !flag_trapping_math
12590           && operand_equal_p (arg0, arg1, 0))
12591         return constant_boolean_node (0, type);
12592
12593       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
12594       {
12595         tree targ0 = strip_float_extensions (arg0);
12596         tree targ1 = strip_float_extensions (arg1);
12597         tree newtype = TREE_TYPE (targ0);
12598
12599         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
12600           newtype = TREE_TYPE (targ1);
12601
12602         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
12603           return fold_build2 (code, type, fold_convert (newtype, targ0),
12604                               fold_convert (newtype, targ1));
12605       }
12606
12607       return NULL_TREE;
12608
12609     case COMPOUND_EXPR:
12610       /* When pedantic, a compound expression can be neither an lvalue
12611          nor an integer constant expression.  */
12612       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12613         return NULL_TREE;
12614       /* Don't let (0, 0) be null pointer constant.  */
12615       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
12616                                  : fold_convert (type, arg1);
12617       return pedantic_non_lvalue (tem);
12618
12619     case COMPLEX_EXPR:
12620       if ((TREE_CODE (arg0) == REAL_CST
12621            && TREE_CODE (arg1) == REAL_CST)
12622           || (TREE_CODE (arg0) == INTEGER_CST
12623               && TREE_CODE (arg1) == INTEGER_CST))
12624         return build_complex (type, arg0, arg1);
12625       return NULL_TREE;
12626
12627     case ASSERT_EXPR:
12628       /* An ASSERT_EXPR should never be passed to fold_binary.  */
12629       gcc_unreachable ();
12630
12631     default:
12632       return NULL_TREE;
12633     } /* switch (code) */
12634 }
12635
12636 /* Callback for walk_tree, looking for LABEL_EXPR.
12637    Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
12638    Do not check the sub-tree of GOTO_EXPR.  */
12639
12640 static tree
12641 contains_label_1 (tree *tp,
12642                   int *walk_subtrees,
12643                   void *data ATTRIBUTE_UNUSED)
12644 {
12645   switch (TREE_CODE (*tp))
12646     {
12647     case LABEL_EXPR:
12648       return *tp;
12649     case GOTO_EXPR:
12650       *walk_subtrees = 0;
12651     /* no break */
12652     default:
12653       return NULL_TREE;
12654     }
12655 }
12656
12657 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
12658    accessible from outside the sub-tree. Returns NULL_TREE if no
12659    addressable label is found.  */
12660
12661 static bool
12662 contains_label_p (tree st)
12663 {
12664   return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
12665 }
12666
12667 /* Fold a ternary expression of code CODE and type TYPE with operands
12668    OP0, OP1, and OP2.  Return the folded expression if folding is
12669    successful.  Otherwise, return NULL_TREE.  */
12670
12671 tree
12672 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
12673 {
12674   tree tem;
12675   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
12676   enum tree_code_class kind = TREE_CODE_CLASS (code);
12677
12678   gcc_assert (IS_EXPR_CODE_CLASS (kind)
12679               && TREE_CODE_LENGTH (code) == 3);
12680
12681   /* Strip any conversions that don't change the mode.  This is safe
12682      for every expression, except for a comparison expression because
12683      its signedness is derived from its operands.  So, in the latter
12684      case, only strip conversions that don't change the signedness.
12685
12686      Note that this is done as an internal manipulation within the
12687      constant folder, in order to find the simplest representation of
12688      the arguments so that their form can be studied.  In any cases,
12689      the appropriate type conversions should be put back in the tree
12690      that will get out of the constant folder.  */
12691   if (op0)
12692     {
12693       arg0 = op0;
12694       STRIP_NOPS (arg0);
12695     }
12696
12697   if (op1)
12698     {
12699       arg1 = op1;
12700       STRIP_NOPS (arg1);
12701     }
12702
12703   switch (code)
12704     {
12705     case COMPONENT_REF:
12706       if (TREE_CODE (arg0) == CONSTRUCTOR
12707           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12708         {
12709           unsigned HOST_WIDE_INT idx;
12710           tree field, value;
12711           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12712             if (field == arg1)
12713               return value;
12714         }
12715       return NULL_TREE;
12716
12717     case COND_EXPR:
12718       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12719          so all simple results must be passed through pedantic_non_lvalue.  */
12720       if (TREE_CODE (arg0) == INTEGER_CST)
12721         {
12722           tree unused_op = integer_zerop (arg0) ? op1 : op2;
12723           tem = integer_zerop (arg0) ? op2 : op1;
12724           /* Only optimize constant conditions when the selected branch
12725              has the same type as the COND_EXPR.  This avoids optimizing
12726              away "c ? x : throw", where the throw has a void type.
12727              Avoid throwing away that operand which contains label.  */
12728           if ((!TREE_SIDE_EFFECTS (unused_op)
12729                || !contains_label_p (unused_op))
12730               && (! VOID_TYPE_P (TREE_TYPE (tem))
12731                   || VOID_TYPE_P (type)))
12732             return pedantic_non_lvalue (tem);
12733           return NULL_TREE;
12734         }
12735       if (operand_equal_p (arg1, op2, 0))
12736         return pedantic_omit_one_operand (type, arg1, arg0);
12737
12738       /* If we have A op B ? A : C, we may be able to convert this to a
12739          simpler expression, depending on the operation and the values
12740          of B and C.  Signed zeros prevent all of these transformations,
12741          for reasons given above each one.
12742
12743          Also try swapping the arguments and inverting the conditional.  */
12744       if (COMPARISON_CLASS_P (arg0)
12745           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12746                                              arg1, TREE_OPERAND (arg0, 1))
12747           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
12748         {
12749           tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
12750           if (tem)
12751             return tem;
12752         }
12753
12754       if (COMPARISON_CLASS_P (arg0)
12755           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12756                                              op2,
12757                                              TREE_OPERAND (arg0, 1))
12758           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
12759         {
12760           tem = fold_truth_not_expr (arg0);
12761           if (tem && COMPARISON_CLASS_P (tem))
12762             {
12763               tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
12764               if (tem)
12765                 return tem;
12766             }
12767         }
12768
12769       /* If the second operand is simpler than the third, swap them
12770          since that produces better jump optimization results.  */
12771       if (truth_value_p (TREE_CODE (arg0))
12772           && tree_swap_operands_p (op1, op2, false))
12773         {
12774           /* See if this can be inverted.  If it can't, possibly because
12775              it was a floating-point inequality comparison, don't do
12776              anything.  */
12777           tem = fold_truth_not_expr (arg0);
12778           if (tem)
12779             return fold_build3 (code, type, tem, op2, op1);
12780         }
12781
12782       /* Convert A ? 1 : 0 to simply A.  */
12783       if (integer_onep (op1)
12784           && integer_zerop (op2)
12785           /* If we try to convert OP0 to our type, the
12786              call to fold will try to move the conversion inside
12787              a COND, which will recurse.  In that case, the COND_EXPR
12788              is probably the best choice, so leave it alone.  */
12789           && type == TREE_TYPE (arg0))
12790         return pedantic_non_lvalue (arg0);
12791
12792       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
12793          over COND_EXPR in cases such as floating point comparisons.  */
12794       if (integer_zerop (op1)
12795           && integer_onep (op2)
12796           && truth_value_p (TREE_CODE (arg0)))
12797         return pedantic_non_lvalue (fold_convert (type,
12798                                                   invert_truthvalue (arg0)));
12799
12800       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
12801       if (TREE_CODE (arg0) == LT_EXPR
12802           && integer_zerop (TREE_OPERAND (arg0, 1))
12803           && integer_zerop (op2)
12804           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12805         {
12806           /* sign_bit_p only checks ARG1 bits within A's precision.
12807              If <sign bit of A> has wider type than A, bits outside
12808              of A's precision in <sign bit of A> need to be checked.
12809              If they are all 0, this optimization needs to be done
12810              in unsigned A's type, if they are all 1 in signed A's type,
12811              otherwise this can't be done.  */
12812           if (TYPE_PRECISION (TREE_TYPE (tem))
12813               < TYPE_PRECISION (TREE_TYPE (arg1))
12814               && TYPE_PRECISION (TREE_TYPE (tem))
12815                  < TYPE_PRECISION (type))
12816             {
12817               unsigned HOST_WIDE_INT mask_lo;
12818               HOST_WIDE_INT mask_hi;
12819               int inner_width, outer_width;
12820               tree tem_type;
12821
12822               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12823               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12824               if (outer_width > TYPE_PRECISION (type))
12825                 outer_width = TYPE_PRECISION (type);
12826
12827               if (outer_width > HOST_BITS_PER_WIDE_INT)
12828                 {
12829                   mask_hi = ((unsigned HOST_WIDE_INT) -1
12830                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
12831                   mask_lo = -1;
12832                 }
12833               else
12834                 {
12835                   mask_hi = 0;
12836                   mask_lo = ((unsigned HOST_WIDE_INT) -1
12837                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
12838                 }
12839               if (inner_width > HOST_BITS_PER_WIDE_INT)
12840                 {
12841                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
12842                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
12843                   mask_lo = 0;
12844                 }
12845               else
12846                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
12847                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
12848
12849               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
12850                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
12851                 {
12852                   tem_type = signed_type_for (TREE_TYPE (tem));
12853                   tem = fold_convert (tem_type, tem);
12854                 }
12855               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
12856                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
12857                 {
12858                   tem_type = unsigned_type_for (TREE_TYPE (tem));
12859                   tem = fold_convert (tem_type, tem);
12860                 }
12861               else
12862                 tem = NULL;
12863             }
12864
12865           if (tem)
12866             return fold_convert (type,
12867                                  fold_build2 (BIT_AND_EXPR,
12868                                               TREE_TYPE (tem), tem,
12869                                               fold_convert (TREE_TYPE (tem),
12870                                                             arg1)));
12871         }
12872
12873       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
12874          already handled above.  */
12875       if (TREE_CODE (arg0) == BIT_AND_EXPR
12876           && integer_onep (TREE_OPERAND (arg0, 1))
12877           && integer_zerop (op2)
12878           && integer_pow2p (arg1))
12879         {
12880           tree tem = TREE_OPERAND (arg0, 0);
12881           STRIP_NOPS (tem);
12882           if (TREE_CODE (tem) == RSHIFT_EXPR
12883               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
12884               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
12885                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
12886             return fold_build2 (BIT_AND_EXPR, type,
12887                                 TREE_OPERAND (tem, 0), arg1);
12888         }
12889
12890       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
12891          is probably obsolete because the first operand should be a
12892          truth value (that's why we have the two cases above), but let's
12893          leave it in until we can confirm this for all front-ends.  */
12894       if (integer_zerop (op2)
12895           && TREE_CODE (arg0) == NE_EXPR
12896           && integer_zerop (TREE_OPERAND (arg0, 1))
12897           && integer_pow2p (arg1)
12898           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12899           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12900                               arg1, OEP_ONLY_CONST))
12901         return pedantic_non_lvalue (fold_convert (type,
12902                                                   TREE_OPERAND (arg0, 0)));
12903
12904       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
12905       if (integer_zerop (op2)
12906           && truth_value_p (TREE_CODE (arg0))
12907           && truth_value_p (TREE_CODE (arg1)))
12908         return fold_build2 (TRUTH_ANDIF_EXPR, type,
12909                             fold_convert (type, arg0),
12910                             arg1);
12911
12912       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
12913       if (integer_onep (op2)
12914           && truth_value_p (TREE_CODE (arg0))
12915           && truth_value_p (TREE_CODE (arg1)))
12916         {
12917           /* Only perform transformation if ARG0 is easily inverted.  */
12918           tem = fold_truth_not_expr (arg0);
12919           if (tem)
12920             return fold_build2 (TRUTH_ORIF_EXPR, type,
12921                                 fold_convert (type, tem),
12922                                 arg1);
12923         }
12924
12925       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
12926       if (integer_zerop (arg1)
12927           && truth_value_p (TREE_CODE (arg0))
12928           && truth_value_p (TREE_CODE (op2)))
12929         {
12930           /* Only perform transformation if ARG0 is easily inverted.  */
12931           tem = fold_truth_not_expr (arg0);
12932           if (tem)
12933             return fold_build2 (TRUTH_ANDIF_EXPR, type,
12934                                 fold_convert (type, tem),
12935                                 op2);
12936         }
12937
12938       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
12939       if (integer_onep (arg1)
12940           && truth_value_p (TREE_CODE (arg0))
12941           && truth_value_p (TREE_CODE (op2)))
12942         return fold_build2 (TRUTH_ORIF_EXPR, type,
12943                             fold_convert (type, arg0),
12944                             op2);
12945
12946       return NULL_TREE;
12947
12948     case CALL_EXPR:
12949       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
12950          of fold_ternary on them.  */
12951       gcc_unreachable ();
12952
12953     case BIT_FIELD_REF:
12954       if ((TREE_CODE (arg0) == VECTOR_CST
12955            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
12956           && type == TREE_TYPE (TREE_TYPE (arg0)))
12957         {
12958           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
12959           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
12960
12961           if (width != 0
12962               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
12963               && (idx % width) == 0
12964               && (idx = idx / width)
12965                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
12966             {
12967               tree elements = NULL_TREE;
12968
12969               if (TREE_CODE (arg0) == VECTOR_CST)
12970                 elements = TREE_VECTOR_CST_ELTS (arg0);
12971               else
12972                 {
12973                   unsigned HOST_WIDE_INT idx;
12974                   tree value;
12975
12976                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
12977                     elements = tree_cons (NULL_TREE, value, elements);
12978                 }
12979               while (idx-- > 0 && elements)
12980                 elements = TREE_CHAIN (elements);
12981               if (elements)
12982                 return TREE_VALUE (elements);
12983               else
12984                 return fold_convert (type, integer_zero_node);
12985             }
12986         }
12987       return NULL_TREE;
12988
12989     default:
12990       return NULL_TREE;
12991     } /* switch (code) */
12992 }
12993
12994 /* Perform constant folding and related simplification of EXPR.
12995    The related simplifications include x*1 => x, x*0 => 0, etc.,
12996    and application of the associative law.
12997    NOP_EXPR conversions may be removed freely (as long as we
12998    are careful not to change the type of the overall expression).
12999    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13000    but we can constant-fold them if they have constant operands.  */
13001
13002 #ifdef ENABLE_FOLD_CHECKING
13003 # define fold(x) fold_1 (x)
13004 static tree fold_1 (tree);
13005 static
13006 #endif
13007 tree
13008 fold (tree expr)
13009 {
13010   const tree t = expr;
13011   enum tree_code code = TREE_CODE (t);
13012   enum tree_code_class kind = TREE_CODE_CLASS (code);
13013   tree tem;
13014
13015   /* Return right away if a constant.  */
13016   if (kind == tcc_constant)
13017     return t;
13018
13019   /* CALL_EXPR-like objects with variable numbers of operands are
13020      treated specially.  */
13021   if (kind == tcc_vl_exp)
13022     {
13023       if (code == CALL_EXPR)
13024         {
13025           tem = fold_call_expr (expr, false);
13026           return tem ? tem : expr;
13027         }
13028       return expr;
13029     }
13030
13031   if (IS_EXPR_CODE_CLASS (kind)
13032       || IS_GIMPLE_STMT_CODE_CLASS (kind))
13033     {
13034       tree type = TREE_TYPE (t);
13035       tree op0, op1, op2;
13036
13037       switch (TREE_CODE_LENGTH (code))
13038         {
13039         case 1:
13040           op0 = TREE_OPERAND (t, 0);
13041           tem = fold_unary (code, type, op0);
13042           return tem ? tem : expr;
13043         case 2:
13044           op0 = TREE_OPERAND (t, 0);
13045           op1 = TREE_OPERAND (t, 1);
13046           tem = fold_binary (code, type, op0, op1);
13047           return tem ? tem : expr;
13048         case 3:
13049           op0 = TREE_OPERAND (t, 0);
13050           op1 = TREE_OPERAND (t, 1);
13051           op2 = TREE_OPERAND (t, 2);
13052           tem = fold_ternary (code, type, op0, op1, op2);
13053           return tem ? tem : expr;
13054         default:
13055           break;
13056         }
13057     }
13058
13059   switch (code)
13060     {
13061     case CONST_DECL:
13062       return fold (DECL_INITIAL (t));
13063
13064     default:
13065       return t;
13066     } /* switch (code) */
13067 }
13068
13069 #ifdef ENABLE_FOLD_CHECKING
13070 #undef fold
13071
13072 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13073 static void fold_check_failed (const_tree, const_tree);
13074 void print_fold_checksum (const_tree);
13075
13076 /* When --enable-checking=fold, compute a digest of expr before
13077    and after actual fold call to see if fold did not accidentally
13078    change original expr.  */
13079
13080 tree
13081 fold (tree expr)
13082 {
13083   tree ret;
13084   struct md5_ctx ctx;
13085   unsigned char checksum_before[16], checksum_after[16];
13086   htab_t ht;
13087
13088   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13089   md5_init_ctx (&ctx);
13090   fold_checksum_tree (expr, &ctx, ht);
13091   md5_finish_ctx (&ctx, checksum_before);
13092   htab_empty (ht);
13093
13094   ret = fold_1 (expr);
13095
13096   md5_init_ctx (&ctx);
13097   fold_checksum_tree (expr, &ctx, ht);
13098   md5_finish_ctx (&ctx, checksum_after);
13099   htab_delete (ht);
13100
13101   if (memcmp (checksum_before, checksum_after, 16))
13102     fold_check_failed (expr, ret);
13103
13104   return ret;
13105 }
13106
13107 void
13108 print_fold_checksum (const_tree expr)
13109 {
13110   struct md5_ctx ctx;
13111   unsigned char checksum[16], cnt;
13112   htab_t ht;
13113
13114   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13115   md5_init_ctx (&ctx);
13116   fold_checksum_tree (expr, &ctx, ht);
13117   md5_finish_ctx (&ctx, checksum);
13118   htab_delete (ht);
13119   for (cnt = 0; cnt < 16; ++cnt)
13120     fprintf (stderr, "%02x", checksum[cnt]);
13121   putc ('\n', stderr);
13122 }
13123
13124 static void
13125 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13126 {
13127   internal_error ("fold check: original tree changed by fold");
13128 }
13129
13130 static void
13131 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13132 {
13133   const void **slot;
13134   enum tree_code code;
13135   struct tree_function_decl buf;
13136   int i, len;
13137   
13138 recursive_label:
13139
13140   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13141                <= sizeof (struct tree_function_decl))
13142               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13143   if (expr == NULL)
13144     return;
13145   slot = (const void **) htab_find_slot (ht, expr, INSERT);
13146   if (*slot != NULL)
13147     return;
13148   *slot = expr;
13149   code = TREE_CODE (expr);
13150   if (TREE_CODE_CLASS (code) == tcc_declaration
13151       && DECL_ASSEMBLER_NAME_SET_P (expr))
13152     {
13153       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13154       memcpy ((char *) &buf, expr, tree_size (expr));
13155       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13156       expr = (tree) &buf;
13157     }
13158   else if (TREE_CODE_CLASS (code) == tcc_type
13159            && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)
13160                || TYPE_CACHED_VALUES_P (expr)
13161                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)))
13162     {
13163       /* Allow these fields to be modified.  */
13164       tree tmp;
13165       memcpy ((char *) &buf, expr, tree_size (expr));
13166       expr = tmp = (tree) &buf;
13167       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13168       TYPE_POINTER_TO (tmp) = NULL;
13169       TYPE_REFERENCE_TO (tmp) = NULL;
13170       if (TYPE_CACHED_VALUES_P (tmp))
13171         {
13172           TYPE_CACHED_VALUES_P (tmp) = 0;
13173           TYPE_CACHED_VALUES (tmp) = NULL;
13174         }
13175     }
13176   md5_process_bytes (expr, tree_size (expr), ctx);
13177   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13178   if (TREE_CODE_CLASS (code) != tcc_type
13179       && TREE_CODE_CLASS (code) != tcc_declaration
13180       && code != TREE_LIST
13181       && code != SSA_NAME)
13182     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13183   switch (TREE_CODE_CLASS (code))
13184     {
13185     case tcc_constant:
13186       switch (code)
13187         {
13188         case STRING_CST:
13189           md5_process_bytes (TREE_STRING_POINTER (expr),
13190                              TREE_STRING_LENGTH (expr), ctx);
13191           break;
13192         case COMPLEX_CST:
13193           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13194           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13195           break;
13196         case VECTOR_CST:
13197           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13198           break;
13199         default:
13200           break;
13201         }
13202       break;
13203     case tcc_exceptional:
13204       switch (code)
13205         {
13206         case TREE_LIST:
13207           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13208           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13209           expr = TREE_CHAIN (expr);
13210           goto recursive_label;
13211           break;
13212         case TREE_VEC:
13213           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13214             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13215           break;
13216         default:
13217           break;
13218         }
13219       break;
13220     case tcc_expression:
13221     case tcc_reference:
13222     case tcc_comparison:
13223     case tcc_unary:
13224     case tcc_binary:
13225     case tcc_statement:
13226     case tcc_vl_exp:
13227       len = TREE_OPERAND_LENGTH (expr);
13228       for (i = 0; i < len; ++i)
13229         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13230       break;
13231     case tcc_declaration:
13232       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13233       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13234       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13235         {
13236           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13237           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13238           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13239           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13240           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13241         }
13242       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13243         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13244           
13245       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13246         {
13247           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13248           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13249           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13250         }
13251       break;
13252     case tcc_type:
13253       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13254         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13255       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13256       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13257       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13258       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13259       if (INTEGRAL_TYPE_P (expr)
13260           || SCALAR_FLOAT_TYPE_P (expr))
13261         {
13262           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13263           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13264         }
13265       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13266       if (TREE_CODE (expr) == RECORD_TYPE
13267           || TREE_CODE (expr) == UNION_TYPE
13268           || TREE_CODE (expr) == QUAL_UNION_TYPE)
13269         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13270       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13271       break;
13272     default:
13273       break;
13274     }
13275 }
13276
13277 /* Helper function for outputting the checksum of a tree T.  When
13278    debugging with gdb, you can "define mynext" to be "next" followed
13279    by "call debug_fold_checksum (op0)", then just trace down till the
13280    outputs differ.  */
13281
13282 void
13283 debug_fold_checksum (const_tree t)
13284 {
13285   int i;
13286   unsigned char checksum[16];
13287   struct md5_ctx ctx;
13288   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13289   
13290   md5_init_ctx (&ctx);
13291   fold_checksum_tree (t, &ctx, ht);
13292   md5_finish_ctx (&ctx, checksum);
13293   htab_empty (ht);
13294
13295   for (i = 0; i < 16; i++)
13296     fprintf (stderr, "%d ", checksum[i]);
13297
13298   fprintf (stderr, "\n");
13299 }
13300
13301 #endif
13302
13303 /* Fold a unary tree expression with code CODE of type TYPE with an
13304    operand OP0.  Return a folded expression if successful.  Otherwise,
13305    return a tree expression with code CODE of type TYPE with an
13306    operand OP0.  */
13307
13308 tree
13309 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13310 {
13311   tree tem;
13312 #ifdef ENABLE_FOLD_CHECKING
13313   unsigned char checksum_before[16], checksum_after[16];
13314   struct md5_ctx ctx;
13315   htab_t ht;
13316
13317   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13318   md5_init_ctx (&ctx);
13319   fold_checksum_tree (op0, &ctx, ht);
13320   md5_finish_ctx (&ctx, checksum_before);
13321   htab_empty (ht);
13322 #endif
13323   
13324   tem = fold_unary (code, type, op0);
13325   if (!tem)
13326     tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13327   
13328 #ifdef ENABLE_FOLD_CHECKING
13329   md5_init_ctx (&ctx);
13330   fold_checksum_tree (op0, &ctx, ht);
13331   md5_finish_ctx (&ctx, checksum_after);
13332   htab_delete (ht);
13333
13334   if (memcmp (checksum_before, checksum_after, 16))
13335     fold_check_failed (op0, tem);
13336 #endif
13337   return tem;
13338 }
13339
13340 /* Fold a binary tree expression with code CODE of type TYPE with
13341    operands OP0 and OP1.  Return a folded expression if successful.
13342    Otherwise, return a tree expression with code CODE of type TYPE
13343    with operands OP0 and OP1.  */
13344
13345 tree
13346 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13347                   MEM_STAT_DECL)
13348 {
13349   tree tem;
13350 #ifdef ENABLE_FOLD_CHECKING
13351   unsigned char checksum_before_op0[16],
13352                 checksum_before_op1[16],
13353                 checksum_after_op0[16],
13354                 checksum_after_op1[16];
13355   struct md5_ctx ctx;
13356   htab_t ht;
13357
13358   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13359   md5_init_ctx (&ctx);
13360   fold_checksum_tree (op0, &ctx, ht);
13361   md5_finish_ctx (&ctx, checksum_before_op0);
13362   htab_empty (ht);
13363
13364   md5_init_ctx (&ctx);
13365   fold_checksum_tree (op1, &ctx, ht);
13366   md5_finish_ctx (&ctx, checksum_before_op1);
13367   htab_empty (ht);
13368 #endif
13369
13370   tem = fold_binary (code, type, op0, op1);
13371   if (!tem)
13372     tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13373   
13374 #ifdef ENABLE_FOLD_CHECKING
13375   md5_init_ctx (&ctx);
13376   fold_checksum_tree (op0, &ctx, ht);
13377   md5_finish_ctx (&ctx, checksum_after_op0);
13378   htab_empty (ht);
13379
13380   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13381     fold_check_failed (op0, tem);
13382   
13383   md5_init_ctx (&ctx);
13384   fold_checksum_tree (op1, &ctx, ht);
13385   md5_finish_ctx (&ctx, checksum_after_op1);
13386   htab_delete (ht);
13387
13388   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13389     fold_check_failed (op1, tem);
13390 #endif
13391   return tem;
13392 }
13393
13394 /* Fold a ternary tree expression with code CODE of type TYPE with
13395    operands OP0, OP1, and OP2.  Return a folded expression if
13396    successful.  Otherwise, return a tree expression with code CODE of
13397    type TYPE with operands OP0, OP1, and OP2.  */
13398
13399 tree
13400 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
13401              MEM_STAT_DECL)
13402 {
13403   tree tem;
13404 #ifdef ENABLE_FOLD_CHECKING
13405   unsigned char checksum_before_op0[16],
13406                 checksum_before_op1[16],
13407                 checksum_before_op2[16],
13408                 checksum_after_op0[16],
13409                 checksum_after_op1[16],
13410                 checksum_after_op2[16];
13411   struct md5_ctx ctx;
13412   htab_t ht;
13413
13414   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13415   md5_init_ctx (&ctx);
13416   fold_checksum_tree (op0, &ctx, ht);
13417   md5_finish_ctx (&ctx, checksum_before_op0);
13418   htab_empty (ht);
13419
13420   md5_init_ctx (&ctx);
13421   fold_checksum_tree (op1, &ctx, ht);
13422   md5_finish_ctx (&ctx, checksum_before_op1);
13423   htab_empty (ht);
13424
13425   md5_init_ctx (&ctx);
13426   fold_checksum_tree (op2, &ctx, ht);
13427   md5_finish_ctx (&ctx, checksum_before_op2);
13428   htab_empty (ht);
13429 #endif
13430
13431   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13432   tem = fold_ternary (code, type, op0, op1, op2);
13433   if (!tem)
13434     tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
13435       
13436 #ifdef ENABLE_FOLD_CHECKING
13437   md5_init_ctx (&ctx);
13438   fold_checksum_tree (op0, &ctx, ht);
13439   md5_finish_ctx (&ctx, checksum_after_op0);
13440   htab_empty (ht);
13441
13442   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13443     fold_check_failed (op0, tem);
13444   
13445   md5_init_ctx (&ctx);
13446   fold_checksum_tree (op1, &ctx, ht);
13447   md5_finish_ctx (&ctx, checksum_after_op1);
13448   htab_empty (ht);
13449
13450   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13451     fold_check_failed (op1, tem);
13452   
13453   md5_init_ctx (&ctx);
13454   fold_checksum_tree (op2, &ctx, ht);
13455   md5_finish_ctx (&ctx, checksum_after_op2);
13456   htab_delete (ht);
13457
13458   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13459     fold_check_failed (op2, tem);
13460 #endif
13461   return tem;
13462 }
13463
13464 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13465    arguments in ARGARRAY, and a null static chain.
13466    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
13467    of type TYPE from the given operands as constructed by build_call_array.  */
13468
13469 tree
13470 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
13471 {
13472   tree tem;
13473 #ifdef ENABLE_FOLD_CHECKING
13474   unsigned char checksum_before_fn[16],
13475                 checksum_before_arglist[16],
13476                 checksum_after_fn[16],
13477                 checksum_after_arglist[16];
13478   struct md5_ctx ctx;
13479   htab_t ht;
13480   int i;
13481
13482   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13483   md5_init_ctx (&ctx);
13484   fold_checksum_tree (fn, &ctx, ht);
13485   md5_finish_ctx (&ctx, checksum_before_fn);
13486   htab_empty (ht);
13487
13488   md5_init_ctx (&ctx);
13489   for (i = 0; i < nargs; i++)
13490     fold_checksum_tree (argarray[i], &ctx, ht);
13491   md5_finish_ctx (&ctx, checksum_before_arglist);
13492   htab_empty (ht);
13493 #endif
13494
13495   tem = fold_builtin_call_array (type, fn, nargs, argarray);
13496       
13497 #ifdef ENABLE_FOLD_CHECKING
13498   md5_init_ctx (&ctx);
13499   fold_checksum_tree (fn, &ctx, ht);
13500   md5_finish_ctx (&ctx, checksum_after_fn);
13501   htab_empty (ht);
13502
13503   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13504     fold_check_failed (fn, tem);
13505   
13506   md5_init_ctx (&ctx);
13507   for (i = 0; i < nargs; i++)
13508     fold_checksum_tree (argarray[i], &ctx, ht);
13509   md5_finish_ctx (&ctx, checksum_after_arglist);
13510   htab_delete (ht);
13511
13512   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13513     fold_check_failed (NULL_TREE, tem);
13514 #endif
13515   return tem;
13516 }
13517
13518 /* Perform constant folding and related simplification of initializer
13519    expression EXPR.  These behave identically to "fold_buildN" but ignore
13520    potential run-time traps and exceptions that fold must preserve.  */
13521
13522 #define START_FOLD_INIT \
13523   int saved_signaling_nans = flag_signaling_nans;\
13524   int saved_trapping_math = flag_trapping_math;\
13525   int saved_rounding_math = flag_rounding_math;\
13526   int saved_trapv = flag_trapv;\
13527   int saved_folding_initializer = folding_initializer;\
13528   flag_signaling_nans = 0;\
13529   flag_trapping_math = 0;\
13530   flag_rounding_math = 0;\
13531   flag_trapv = 0;\
13532   folding_initializer = 1;
13533
13534 #define END_FOLD_INIT \
13535   flag_signaling_nans = saved_signaling_nans;\
13536   flag_trapping_math = saved_trapping_math;\
13537   flag_rounding_math = saved_rounding_math;\
13538   flag_trapv = saved_trapv;\
13539   folding_initializer = saved_folding_initializer;
13540
13541 tree
13542 fold_build1_initializer (enum tree_code code, tree type, tree op)
13543 {
13544   tree result;
13545   START_FOLD_INIT;
13546
13547   result = fold_build1 (code, type, op);
13548
13549   END_FOLD_INIT;
13550   return result;
13551 }
13552
13553 tree
13554 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
13555 {
13556   tree result;
13557   START_FOLD_INIT;
13558
13559   result = fold_build2 (code, type, op0, op1);
13560
13561   END_FOLD_INIT;
13562   return result;
13563 }
13564
13565 tree
13566 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
13567                          tree op2)
13568 {
13569   tree result;
13570   START_FOLD_INIT;
13571
13572   result = fold_build3 (code, type, op0, op1, op2);
13573
13574   END_FOLD_INIT;
13575   return result;
13576 }
13577
13578 tree
13579 fold_build_call_array_initializer (tree type, tree fn,
13580                                    int nargs, tree *argarray)
13581 {
13582   tree result;
13583   START_FOLD_INIT;
13584
13585   result = fold_build_call_array (type, fn, nargs, argarray);
13586
13587   END_FOLD_INIT;
13588   return result;
13589 }
13590
13591 #undef START_FOLD_INIT
13592 #undef END_FOLD_INIT
13593
13594 /* Determine if first argument is a multiple of second argument.  Return 0 if
13595    it is not, or we cannot easily determined it to be.
13596
13597    An example of the sort of thing we care about (at this point; this routine
13598    could surely be made more general, and expanded to do what the *_DIV_EXPR's
13599    fold cases do now) is discovering that
13600
13601      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13602
13603    is a multiple of
13604
13605      SAVE_EXPR (J * 8)
13606
13607    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
13608
13609    This code also handles discovering that
13610
13611      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13612
13613    is a multiple of 8 so we don't have to worry about dealing with a
13614    possible remainder.
13615
13616    Note that we *look* inside a SAVE_EXPR only to determine how it was
13617    calculated; it is not safe for fold to do much of anything else with the
13618    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
13619    at run time.  For example, the latter example above *cannot* be implemented
13620    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
13621    evaluation time of the original SAVE_EXPR is not necessarily the same at
13622    the time the new expression is evaluated.  The only optimization of this
13623    sort that would be valid is changing
13624
13625      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
13626
13627    divided by 8 to
13628
13629      SAVE_EXPR (I) * SAVE_EXPR (J)
13630
13631    (where the same SAVE_EXPR (J) is used in the original and the
13632    transformed version).  */
13633
13634 int
13635 multiple_of_p (tree type, const_tree top, const_tree bottom)
13636 {
13637   if (operand_equal_p (top, bottom, 0))
13638     return 1;
13639
13640   if (TREE_CODE (type) != INTEGER_TYPE)
13641     return 0;
13642
13643   switch (TREE_CODE (top))
13644     {
13645     case BIT_AND_EXPR:
13646       /* Bitwise and provides a power of two multiple.  If the mask is
13647          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
13648       if (!integer_pow2p (bottom))
13649         return 0;
13650       /* FALLTHRU */
13651
13652     case MULT_EXPR:
13653       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13654               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13655
13656     case PLUS_EXPR:
13657     case MINUS_EXPR:
13658       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13659               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13660
13661     case LSHIFT_EXPR:
13662       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
13663         {
13664           tree op1, t1;
13665
13666           op1 = TREE_OPERAND (top, 1);
13667           /* const_binop may not detect overflow correctly,
13668              so check for it explicitly here.  */
13669           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
13670               > TREE_INT_CST_LOW (op1)
13671               && TREE_INT_CST_HIGH (op1) == 0
13672               && 0 != (t1 = fold_convert (type,
13673                                           const_binop (LSHIFT_EXPR,
13674                                                        size_one_node,
13675                                                        op1, 0)))
13676               && !TREE_OVERFLOW (t1))
13677             return multiple_of_p (type, t1, bottom);
13678         }
13679       return 0;
13680
13681     case NOP_EXPR:
13682       /* Can't handle conversions from non-integral or wider integral type.  */
13683       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
13684           || (TYPE_PRECISION (type)
13685               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
13686         return 0;
13687
13688       /* .. fall through ...  */
13689
13690     case SAVE_EXPR:
13691       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
13692
13693     case INTEGER_CST:
13694       if (TREE_CODE (bottom) != INTEGER_CST
13695           || integer_zerop (bottom)
13696           || (TYPE_UNSIGNED (type)
13697               && (tree_int_cst_sgn (top) < 0
13698                   || tree_int_cst_sgn (bottom) < 0)))
13699         return 0;
13700       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
13701                                              top, bottom, 0));
13702
13703     default:
13704       return 0;
13705     }
13706 }
13707
13708 /* Return true if CODE or TYPE is known to be non-negative. */
13709
13710 static bool
13711 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
13712 {
13713   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
13714       && truth_value_p (code))
13715     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
13716        have a signed:1 type (where the value is -1 and 0).  */
13717     return true;
13718   return false;
13719 }
13720
13721 /* Return true if (CODE OP0) is known to be non-negative.  If the return
13722    value is based on the assumption that signed overflow is undefined,
13723    set *STRICT_OVERFLOW_P to true; otherwise, don't change
13724    *STRICT_OVERFLOW_P.  */
13725
13726 static bool
13727 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
13728                                 bool *strict_overflow_p)
13729 {
13730   if (TYPE_UNSIGNED (type))
13731     return true;
13732
13733   switch (code)
13734     {
13735     case ABS_EXPR:
13736       /* We can't return 1 if flag_wrapv is set because
13737          ABS_EXPR<INT_MIN> = INT_MIN.  */
13738       if (!INTEGRAL_TYPE_P (type))
13739         return true;
13740       if (TYPE_OVERFLOW_UNDEFINED (type))
13741         {
13742           *strict_overflow_p = true;
13743           return true;
13744         }
13745       break;
13746
13747     case NON_LVALUE_EXPR:
13748     case FLOAT_EXPR:
13749     case FIX_TRUNC_EXPR:
13750       return tree_expr_nonnegative_warnv_p (op0,
13751                                             strict_overflow_p);
13752
13753     case NOP_EXPR:
13754       {
13755         tree inner_type = TREE_TYPE (op0);
13756         tree outer_type = type;
13757
13758         if (TREE_CODE (outer_type) == REAL_TYPE)
13759           {
13760             if (TREE_CODE (inner_type) == REAL_TYPE)
13761               return tree_expr_nonnegative_warnv_p (op0,
13762                                                     strict_overflow_p);
13763             if (TREE_CODE (inner_type) == INTEGER_TYPE)
13764               {
13765                 if (TYPE_UNSIGNED (inner_type))
13766                   return true;
13767                 return tree_expr_nonnegative_warnv_p (op0,
13768                                                       strict_overflow_p);
13769               }
13770           }
13771         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
13772           {
13773             if (TREE_CODE (inner_type) == REAL_TYPE)
13774               return tree_expr_nonnegative_warnv_p (op0,
13775                                                     strict_overflow_p);
13776             if (TREE_CODE (inner_type) == INTEGER_TYPE)
13777               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
13778                       && TYPE_UNSIGNED (inner_type);
13779           }
13780       }
13781       break;
13782
13783     default:
13784       return tree_simple_nonnegative_warnv_p (code, type);
13785     }
13786
13787   /* We don't know sign of `t', so be conservative and return false.  */
13788   return false;
13789 }
13790
13791 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
13792    value is based on the assumption that signed overflow is undefined,
13793    set *STRICT_OVERFLOW_P to true; otherwise, don't change
13794    *STRICT_OVERFLOW_P.  */
13795
13796 static bool
13797 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
13798                                       tree op1, bool *strict_overflow_p)
13799 {
13800   if (TYPE_UNSIGNED (type))
13801     return true;
13802
13803   switch (code)
13804     {
13805     case POINTER_PLUS_EXPR:
13806     case PLUS_EXPR:
13807       if (FLOAT_TYPE_P (type))
13808         return (tree_expr_nonnegative_warnv_p (op0,
13809                                                strict_overflow_p)
13810                 && tree_expr_nonnegative_warnv_p (op1,
13811                                                   strict_overflow_p));
13812
13813       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
13814          both unsigned and at least 2 bits shorter than the result.  */
13815       if (TREE_CODE (type) == INTEGER_TYPE
13816           && TREE_CODE (op0) == NOP_EXPR
13817           && TREE_CODE (op1) == NOP_EXPR)
13818         {
13819           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
13820           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
13821           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
13822               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
13823             {
13824               unsigned int prec = MAX (TYPE_PRECISION (inner1),
13825                                        TYPE_PRECISION (inner2)) + 1;
13826               return prec < TYPE_PRECISION (type);
13827             }
13828         }
13829       break;
13830
13831     case MULT_EXPR:
13832       if (FLOAT_TYPE_P (type))
13833         {
13834           /* x * x for floating point x is always non-negative.  */
13835           if (operand_equal_p (op0, op1, 0))
13836             return true;
13837           return (tree_expr_nonnegative_warnv_p (op0,
13838                                                  strict_overflow_p)
13839                   && tree_expr_nonnegative_warnv_p (op1,
13840                                                     strict_overflow_p));
13841         }
13842
13843       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
13844          both unsigned and their total bits is shorter than the result.  */
13845       if (TREE_CODE (type) == INTEGER_TYPE
13846           && TREE_CODE (op0) == NOP_EXPR
13847           && TREE_CODE (op1) == NOP_EXPR)
13848         {
13849           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
13850           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
13851           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
13852               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
13853             return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
13854                    < TYPE_PRECISION (type);
13855         }
13856       return false;
13857
13858     case BIT_AND_EXPR:
13859     case MAX_EXPR:
13860       return (tree_expr_nonnegative_warnv_p (op0,
13861                                              strict_overflow_p)
13862               || tree_expr_nonnegative_warnv_p (op1,
13863                                                 strict_overflow_p));
13864
13865     case BIT_IOR_EXPR:
13866     case BIT_XOR_EXPR:
13867     case MIN_EXPR:
13868     case RDIV_EXPR:
13869     case TRUNC_DIV_EXPR:
13870     case CEIL_DIV_EXPR:
13871     case FLOOR_DIV_EXPR:
13872     case ROUND_DIV_EXPR:
13873       return (tree_expr_nonnegative_warnv_p (op0,
13874                                              strict_overflow_p)
13875               && tree_expr_nonnegative_warnv_p (op1,
13876                                                 strict_overflow_p));
13877
13878     case TRUNC_MOD_EXPR:
13879     case CEIL_MOD_EXPR:
13880     case FLOOR_MOD_EXPR:
13881     case ROUND_MOD_EXPR:
13882       return tree_expr_nonnegative_warnv_p (op0,
13883                                             strict_overflow_p);
13884     default:
13885       return tree_simple_nonnegative_warnv_p (code, type);
13886     }
13887
13888   /* We don't know sign of `t', so be conservative and return false.  */
13889   return false;
13890 }
13891
13892 /* Return true if T is known to be non-negative.  If the return
13893    value is based on the assumption that signed overflow is undefined,
13894    set *STRICT_OVERFLOW_P to true; otherwise, don't change
13895    *STRICT_OVERFLOW_P.  */
13896
13897 static bool
13898 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
13899 {
13900   if (TYPE_UNSIGNED (TREE_TYPE (t)))
13901     return true;
13902
13903   switch (TREE_CODE (t))
13904     {
13905     case SSA_NAME:
13906       /* Query VRP to see if it has recorded any information about
13907          the range of this object.  */
13908       return ssa_name_nonnegative_p (t);
13909
13910     case INTEGER_CST:
13911       return tree_int_cst_sgn (t) >= 0;
13912
13913     case REAL_CST:
13914       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
13915
13916     case FIXED_CST:
13917       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
13918
13919     case COND_EXPR:
13920       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13921                                              strict_overflow_p)
13922               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
13923                                                 strict_overflow_p));
13924     default:
13925       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
13926                                                    TREE_TYPE (t));
13927     }
13928   /* We don't know sign of `t', so be conservative and return false.  */
13929   return false;
13930 }
13931
13932 /* Return true if T is known to be non-negative.  If the return
13933    value is based on the assumption that signed overflow is undefined,
13934    set *STRICT_OVERFLOW_P to true; otherwise, don't change
13935    *STRICT_OVERFLOW_P.  */
13936
13937 static bool
13938 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
13939 {
13940   enum tree_code code = TREE_CODE (t);
13941   if (TYPE_UNSIGNED (TREE_TYPE (t)))
13942     return true;
13943
13944   switch (code)
13945     {
13946     case TARGET_EXPR:
13947       {
13948         tree temp = TARGET_EXPR_SLOT (t);
13949         t = TARGET_EXPR_INITIAL (t);
13950
13951         /* If the initializer is non-void, then it's a normal expression
13952            that will be assigned to the slot.  */
13953         if (!VOID_TYPE_P (t))
13954           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
13955
13956         /* Otherwise, the initializer sets the slot in some way.  One common
13957            way is an assignment statement at the end of the initializer.  */
13958         while (1)
13959           {
13960             if (TREE_CODE (t) == BIND_EXPR)
13961               t = expr_last (BIND_EXPR_BODY (t));
13962             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13963                      || TREE_CODE (t) == TRY_CATCH_EXPR)
13964               t = expr_last (TREE_OPERAND (t, 0));
13965             else if (TREE_CODE (t) == STATEMENT_LIST)
13966               t = expr_last (t);
13967             else
13968               break;
13969           }
13970         if ((TREE_CODE (t) == MODIFY_EXPR
13971              || TREE_CODE (t) == GIMPLE_MODIFY_STMT)
13972             && GENERIC_TREE_OPERAND (t, 0) == temp)
13973           return tree_expr_nonnegative_warnv_p (GENERIC_TREE_OPERAND (t, 1),
13974                                                 strict_overflow_p);
13975
13976         return false;
13977       }
13978
13979     case CALL_EXPR:
13980       {
13981         tree fndecl = get_callee_fndecl (t);
13982         if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
13983           switch (DECL_FUNCTION_CODE (fndecl))
13984             {
13985             CASE_FLT_FN (BUILT_IN_ACOS):
13986             CASE_FLT_FN (BUILT_IN_ACOSH):
13987             CASE_FLT_FN (BUILT_IN_CABS):
13988             CASE_FLT_FN (BUILT_IN_COSH):
13989             CASE_FLT_FN (BUILT_IN_ERFC):
13990             CASE_FLT_FN (BUILT_IN_EXP):
13991             CASE_FLT_FN (BUILT_IN_EXP10):
13992             CASE_FLT_FN (BUILT_IN_EXP2):
13993             CASE_FLT_FN (BUILT_IN_FABS):
13994             CASE_FLT_FN (BUILT_IN_FDIM):
13995             CASE_FLT_FN (BUILT_IN_HYPOT):
13996             CASE_FLT_FN (BUILT_IN_POW10):
13997             CASE_INT_FN (BUILT_IN_FFS):
13998             CASE_INT_FN (BUILT_IN_PARITY):
13999             CASE_INT_FN (BUILT_IN_POPCOUNT):
14000             case BUILT_IN_BSWAP32:
14001             case BUILT_IN_BSWAP64:
14002               /* Always true.  */
14003               return true;
14004
14005             CASE_FLT_FN (BUILT_IN_SQRT):
14006               /* sqrt(-0.0) is -0.0.  */
14007               if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (t))))
14008                 return true;
14009               return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14010                                                     strict_overflow_p);
14011
14012             CASE_FLT_FN (BUILT_IN_ASINH):
14013             CASE_FLT_FN (BUILT_IN_ATAN):
14014             CASE_FLT_FN (BUILT_IN_ATANH):
14015             CASE_FLT_FN (BUILT_IN_CBRT):
14016             CASE_FLT_FN (BUILT_IN_CEIL):
14017             CASE_FLT_FN (BUILT_IN_ERF):
14018             CASE_FLT_FN (BUILT_IN_EXPM1):
14019             CASE_FLT_FN (BUILT_IN_FLOOR):
14020             CASE_FLT_FN (BUILT_IN_FMOD):
14021             CASE_FLT_FN (BUILT_IN_FREXP):
14022             CASE_FLT_FN (BUILT_IN_LCEIL):
14023             CASE_FLT_FN (BUILT_IN_LDEXP):
14024             CASE_FLT_FN (BUILT_IN_LFLOOR):
14025             CASE_FLT_FN (BUILT_IN_LLCEIL):
14026             CASE_FLT_FN (BUILT_IN_LLFLOOR):
14027             CASE_FLT_FN (BUILT_IN_LLRINT):
14028             CASE_FLT_FN (BUILT_IN_LLROUND):
14029             CASE_FLT_FN (BUILT_IN_LRINT):
14030             CASE_FLT_FN (BUILT_IN_LROUND):
14031             CASE_FLT_FN (BUILT_IN_MODF):
14032             CASE_FLT_FN (BUILT_IN_NEARBYINT):
14033             CASE_FLT_FN (BUILT_IN_RINT):
14034             CASE_FLT_FN (BUILT_IN_ROUND):
14035             CASE_FLT_FN (BUILT_IN_SCALB):
14036             CASE_FLT_FN (BUILT_IN_SCALBLN):
14037             CASE_FLT_FN (BUILT_IN_SCALBN):
14038             CASE_FLT_FN (BUILT_IN_SIGNBIT):
14039             CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14040             CASE_FLT_FN (BUILT_IN_SINH):
14041             CASE_FLT_FN (BUILT_IN_TANH):
14042             CASE_FLT_FN (BUILT_IN_TRUNC):
14043               /* True if the 1st argument is nonnegative.  */
14044               return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14045                                                     strict_overflow_p);
14046
14047             CASE_FLT_FN (BUILT_IN_FMAX):
14048               /* True if the 1st OR 2nd arguments are nonnegative.  */
14049               return (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14050                                                      strict_overflow_p)
14051                       || (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 1),
14052                                                          strict_overflow_p)));
14053
14054             CASE_FLT_FN (BUILT_IN_FMIN):
14055               /* True if the 1st AND 2nd arguments are nonnegative.  */
14056               return (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14057                                                      strict_overflow_p)
14058                       && (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 1),
14059                                                          strict_overflow_p)));
14060
14061             CASE_FLT_FN (BUILT_IN_COPYSIGN):
14062               /* True if the 2nd argument is nonnegative.  */
14063               return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 1),
14064                                                     strict_overflow_p);
14065
14066             CASE_FLT_FN (BUILT_IN_POWI):
14067               /* True if the 1st argument is nonnegative or the second
14068                  argument is an even integer.  */
14069               if (TREE_CODE (CALL_EXPR_ARG (t, 1)) == INTEGER_CST)
14070                 {
14071                   tree arg1 = CALL_EXPR_ARG (t, 1);
14072                   if ((TREE_INT_CST_LOW (arg1) & 1) == 0)
14073                     return true;
14074                 }
14075               return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14076                                                     strict_overflow_p);
14077
14078             CASE_FLT_FN (BUILT_IN_POW):
14079               /* True if the 1st argument is nonnegative or the second
14080                  argument is an even integer valued real.  */
14081               if (TREE_CODE (CALL_EXPR_ARG (t, 1)) == REAL_CST)
14082                 {
14083                   REAL_VALUE_TYPE c;
14084                   HOST_WIDE_INT n;
14085
14086                   c = TREE_REAL_CST (CALL_EXPR_ARG (t, 1));
14087                   n = real_to_integer (&c);
14088                   if ((n & 1) == 0)
14089                     {
14090                       REAL_VALUE_TYPE cint;
14091                       real_from_integer (&cint, VOIDmode, n,
14092                                          n < 0 ? -1 : 0, 0);
14093                       if (real_identical (&c, &cint))
14094                         return true;
14095                     }
14096                 }
14097               return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14098                                                     strict_overflow_p);
14099
14100             default:
14101               break;
14102             }
14103         return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14104                                                      TREE_TYPE (t));
14105       }
14106       break;
14107
14108     case COMPOUND_EXPR:
14109     case MODIFY_EXPR:
14110     case GIMPLE_MODIFY_STMT:
14111       return tree_expr_nonnegative_warnv_p (GENERIC_TREE_OPERAND (t, 1),
14112                                             strict_overflow_p);
14113     case BIND_EXPR:
14114       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14115                                             strict_overflow_p);
14116     case SAVE_EXPR:
14117       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14118                                             strict_overflow_p);
14119
14120     default:
14121       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14122                                                    TREE_TYPE (t));
14123     }
14124
14125   /* We don't know sign of `t', so be conservative and return false.  */
14126   return false;
14127 }
14128
14129 /* Return true if T is known to be non-negative.  If the return
14130    value is based on the assumption that signed overflow is undefined,
14131    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14132    *STRICT_OVERFLOW_P.  */
14133
14134 bool
14135 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14136 {
14137   enum tree_code code;
14138   if (t == error_mark_node)
14139     return false;
14140
14141   code = TREE_CODE (t);
14142   switch (TREE_CODE_CLASS (code))
14143     {
14144     case tcc_binary:
14145     case tcc_comparison:
14146       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14147                                               TREE_TYPE (t),
14148                                               TREE_OPERAND (t, 0),
14149                                               TREE_OPERAND (t, 1),
14150                                               strict_overflow_p);
14151
14152     case tcc_unary:
14153       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14154                                              TREE_TYPE (t),
14155                                              TREE_OPERAND (t, 0),
14156                                              strict_overflow_p);
14157
14158     case tcc_constant:
14159     case tcc_declaration:
14160     case tcc_reference:
14161       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14162
14163     default:
14164       break;
14165     }
14166
14167   switch (code)
14168     {
14169     case TRUTH_AND_EXPR:
14170     case TRUTH_OR_EXPR:
14171     case TRUTH_XOR_EXPR:
14172       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14173                                               TREE_TYPE (t),
14174                                               TREE_OPERAND (t, 0),
14175                                               TREE_OPERAND (t, 1),
14176                                               strict_overflow_p);
14177     case TRUTH_NOT_EXPR:
14178       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14179                                              TREE_TYPE (t),
14180                                              TREE_OPERAND (t, 0),
14181                                              strict_overflow_p);
14182
14183     case COND_EXPR:
14184     case CONSTRUCTOR:
14185     case OBJ_TYPE_REF:
14186     case ASSERT_EXPR:
14187     case ADDR_EXPR:
14188     case WITH_SIZE_EXPR:
14189     case EXC_PTR_EXPR:
14190     case SSA_NAME:
14191     case FILTER_EXPR:
14192       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14193
14194     default:
14195       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14196     }
14197 }
14198
14199 /* Return true if `t' is known to be non-negative.  Handle warnings
14200    about undefined signed overflow.  */
14201
14202 bool
14203 tree_expr_nonnegative_p (tree t)
14204 {
14205   bool ret, strict_overflow_p;
14206
14207   strict_overflow_p = false;
14208   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14209   if (strict_overflow_p)
14210     fold_overflow_warning (("assuming signed overflow does not occur when "
14211                             "determining that expression is always "
14212                             "non-negative"),
14213                            WARN_STRICT_OVERFLOW_MISC);
14214   return ret;
14215 }
14216
14217
14218 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14219    For floating point we further ensure that T is not denormal.
14220    Similar logic is present in nonzero_address in rtlanal.h.
14221
14222    If the return value is based on the assumption that signed overflow
14223    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14224    change *STRICT_OVERFLOW_P.  */
14225
14226 static bool
14227 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14228                                  bool *strict_overflow_p)
14229 {
14230   switch (code)
14231     {
14232     case ABS_EXPR:
14233       return tree_expr_nonzero_warnv_p (op0,
14234                                         strict_overflow_p);
14235
14236     case NOP_EXPR:
14237       {
14238         tree inner_type = TREE_TYPE (op0);
14239         tree outer_type = type;
14240
14241         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14242                 && tree_expr_nonzero_warnv_p (op0,
14243                                               strict_overflow_p));
14244       }
14245       break;
14246
14247     case NON_LVALUE_EXPR:
14248       return tree_expr_nonzero_warnv_p (op0,
14249                                         strict_overflow_p);
14250
14251     default:
14252       break;
14253   }
14254
14255   return false;
14256 }
14257
14258 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14259    For floating point we further ensure that T is not denormal.
14260    Similar logic is present in nonzero_address in rtlanal.h.
14261
14262    If the return value is based on the assumption that signed overflow
14263    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14264    change *STRICT_OVERFLOW_P.  */
14265
14266 static bool
14267 tree_binary_nonzero_warnv_p (enum tree_code code,
14268                              tree type,
14269                              tree op0,
14270                              tree op1, bool *strict_overflow_p)
14271 {
14272   bool sub_strict_overflow_p;
14273   switch (code)
14274     {
14275     case POINTER_PLUS_EXPR:
14276     case PLUS_EXPR:
14277       if (TYPE_OVERFLOW_UNDEFINED (type))
14278         {
14279           /* With the presence of negative values it is hard
14280              to say something.  */
14281           sub_strict_overflow_p = false;
14282           if (!tree_expr_nonnegative_warnv_p (op0,
14283                                               &sub_strict_overflow_p)
14284               || !tree_expr_nonnegative_warnv_p (op1,
14285                                                  &sub_strict_overflow_p))
14286             return false;
14287           /* One of operands must be positive and the other non-negative.  */
14288           /* We don't set *STRICT_OVERFLOW_P here: even if this value
14289              overflows, on a twos-complement machine the sum of two
14290              nonnegative numbers can never be zero.  */
14291           return (tree_expr_nonzero_warnv_p (op0,
14292                                              strict_overflow_p)
14293                   || tree_expr_nonzero_warnv_p (op1,
14294                                                 strict_overflow_p));
14295         }
14296       break;
14297
14298     case MULT_EXPR:
14299       if (TYPE_OVERFLOW_UNDEFINED (type))
14300         {
14301           if (tree_expr_nonzero_warnv_p (op0,
14302                                          strict_overflow_p)
14303               && tree_expr_nonzero_warnv_p (op1,
14304                                             strict_overflow_p))
14305             {
14306               *strict_overflow_p = true;
14307               return true;
14308             }
14309         }
14310       break;
14311
14312     case MIN_EXPR:
14313       sub_strict_overflow_p = false;
14314       if (tree_expr_nonzero_warnv_p (op0,
14315                                      &sub_strict_overflow_p)
14316           && tree_expr_nonzero_warnv_p (op1,
14317                                         &sub_strict_overflow_p))
14318         {
14319           if (sub_strict_overflow_p)
14320             *strict_overflow_p = true;
14321         }
14322       break;
14323
14324     case MAX_EXPR:
14325       sub_strict_overflow_p = false;
14326       if (tree_expr_nonzero_warnv_p (op0,
14327                                      &sub_strict_overflow_p))
14328         {
14329           if (sub_strict_overflow_p)
14330             *strict_overflow_p = true;
14331
14332           /* When both operands are nonzero, then MAX must be too.  */
14333           if (tree_expr_nonzero_warnv_p (op1,
14334                                          strict_overflow_p))
14335             return true;
14336
14337           /* MAX where operand 0 is positive is positive.  */
14338           return tree_expr_nonnegative_warnv_p (op0,
14339                                                strict_overflow_p);
14340         }
14341       /* MAX where operand 1 is positive is positive.  */
14342       else if (tree_expr_nonzero_warnv_p (op1,
14343                                           &sub_strict_overflow_p)
14344                && tree_expr_nonnegative_warnv_p (op1,
14345                                                  &sub_strict_overflow_p))
14346         {
14347           if (sub_strict_overflow_p)
14348             *strict_overflow_p = true;
14349           return true;
14350         }
14351       break;
14352
14353     case BIT_IOR_EXPR:
14354       return (tree_expr_nonzero_warnv_p (op1,
14355                                          strict_overflow_p)
14356               || tree_expr_nonzero_warnv_p (op0,
14357                                             strict_overflow_p));
14358
14359     default:
14360       break;
14361   }
14362
14363   return false;
14364 }
14365
14366 /* Return true when T is an address and is known to be nonzero.
14367    For floating point we further ensure that T is not denormal.
14368    Similar logic is present in nonzero_address in rtlanal.h.
14369
14370    If the return value is based on the assumption that signed overflow
14371    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14372    change *STRICT_OVERFLOW_P.  */
14373
14374 static bool
14375 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14376 {
14377   bool sub_strict_overflow_p;
14378   switch (TREE_CODE (t))
14379     {
14380     case SSA_NAME:
14381       /* Query VRP to see if it has recorded any information about
14382          the range of this object.  */
14383       return ssa_name_nonzero_p (t);
14384
14385     case INTEGER_CST:
14386       return !integer_zerop (t);
14387
14388     case ADDR_EXPR:
14389       {
14390         tree base = get_base_address (TREE_OPERAND (t, 0));
14391
14392         if (!base)
14393           return false;
14394
14395         /* Weak declarations may link to NULL.  */
14396         if (VAR_OR_FUNCTION_DECL_P (base))
14397           return !DECL_WEAK (base);
14398
14399         /* Constants are never weak.  */
14400         if (CONSTANT_CLASS_P (base))
14401           return true;
14402
14403         return false;
14404       }
14405
14406     case COND_EXPR:
14407       sub_strict_overflow_p = false;
14408       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14409                                      &sub_strict_overflow_p)
14410           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
14411                                         &sub_strict_overflow_p))
14412         {
14413           if (sub_strict_overflow_p)
14414             *strict_overflow_p = true;
14415           return true;
14416         }
14417       break;
14418
14419     default:
14420       break;
14421     }
14422   return false;
14423 }
14424
14425 /* Return true when T is an address and is known to be nonzero.
14426    For floating point we further ensure that T is not denormal.
14427    Similar logic is present in nonzero_address in rtlanal.h.
14428
14429    If the return value is based on the assumption that signed overflow
14430    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14431    change *STRICT_OVERFLOW_P.  */
14432
14433 bool
14434 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14435 {
14436   tree type = TREE_TYPE (t);
14437   enum tree_code code;
14438
14439   /* Doing something useful for floating point would need more work.  */
14440   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14441     return false;
14442
14443   code = TREE_CODE (t);
14444   switch (TREE_CODE_CLASS (code))
14445     {
14446     case tcc_unary:
14447       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
14448                                               strict_overflow_p);
14449     case tcc_binary:
14450     case tcc_comparison:
14451       return tree_binary_nonzero_warnv_p (code, type,
14452                                                TREE_OPERAND (t, 0),
14453                                                TREE_OPERAND (t, 1),
14454                                                strict_overflow_p);
14455     case tcc_constant:
14456     case tcc_declaration:
14457     case tcc_reference:
14458       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
14459
14460     default:
14461       break;
14462     }
14463
14464   switch (code)
14465     {
14466     case TRUTH_NOT_EXPR:
14467       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
14468                                               strict_overflow_p);
14469
14470     case TRUTH_AND_EXPR:
14471     case TRUTH_OR_EXPR:
14472     case TRUTH_XOR_EXPR:
14473       return tree_binary_nonzero_warnv_p (code, type,
14474                                                TREE_OPERAND (t, 0),
14475                                                TREE_OPERAND (t, 1),
14476                                                strict_overflow_p);
14477
14478     case COND_EXPR:
14479     case CONSTRUCTOR:
14480     case OBJ_TYPE_REF:
14481     case ASSERT_EXPR:
14482     case ADDR_EXPR:
14483     case WITH_SIZE_EXPR:
14484     case EXC_PTR_EXPR:
14485     case SSA_NAME:
14486     case FILTER_EXPR:
14487       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
14488
14489     case COMPOUND_EXPR:
14490     case MODIFY_EXPR:
14491     case GIMPLE_MODIFY_STMT:
14492     case BIND_EXPR:
14493       return tree_expr_nonzero_warnv_p (GENERIC_TREE_OPERAND (t, 1),
14494                                         strict_overflow_p);
14495
14496     case SAVE_EXPR:
14497       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14498                                         strict_overflow_p);
14499
14500     case CALL_EXPR:
14501       return alloca_call_p (t);
14502
14503     default:
14504       break;
14505     }
14506   return false;
14507 }
14508
14509 /* Return true when T is an address and is known to be nonzero.
14510    Handle warnings about undefined signed overflow.  */
14511
14512 bool
14513 tree_expr_nonzero_p (tree t)
14514 {
14515   bool ret, strict_overflow_p;
14516
14517   strict_overflow_p = false;
14518   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
14519   if (strict_overflow_p)
14520     fold_overflow_warning (("assuming signed overflow does not occur when "
14521                             "determining that expression is always "
14522                             "non-zero"),
14523                            WARN_STRICT_OVERFLOW_MISC);
14524   return ret;
14525 }
14526
14527 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
14528    attempt to fold the expression to a constant without modifying TYPE,
14529    OP0 or OP1.
14530
14531    If the expression could be simplified to a constant, then return
14532    the constant.  If the expression would not be simplified to a
14533    constant, then return NULL_TREE.  */
14534
14535 tree
14536 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
14537 {
14538   tree tem = fold_binary (code, type, op0, op1);
14539   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14540 }
14541
14542 /* Given the components of a unary expression CODE, TYPE and OP0,
14543    attempt to fold the expression to a constant without modifying
14544    TYPE or OP0.
14545
14546    If the expression could be simplified to a constant, then return
14547    the constant.  If the expression would not be simplified to a
14548    constant, then return NULL_TREE.  */
14549
14550 tree
14551 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
14552 {
14553   tree tem = fold_unary (code, type, op0);
14554   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14555 }
14556
14557 /* If EXP represents referencing an element in a constant string
14558    (either via pointer arithmetic or array indexing), return the
14559    tree representing the value accessed, otherwise return NULL.  */
14560
14561 tree
14562 fold_read_from_constant_string (tree exp)
14563 {
14564   if ((TREE_CODE (exp) == INDIRECT_REF
14565        || TREE_CODE (exp) == ARRAY_REF)
14566       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
14567     {
14568       tree exp1 = TREE_OPERAND (exp, 0);
14569       tree index;
14570       tree string;
14571
14572       if (TREE_CODE (exp) == INDIRECT_REF)
14573         string = string_constant (exp1, &index);
14574       else
14575         {
14576           tree low_bound = array_ref_low_bound (exp);
14577           index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
14578
14579           /* Optimize the special-case of a zero lower bound.
14580
14581              We convert the low_bound to sizetype to avoid some problems
14582              with constant folding.  (E.g. suppose the lower bound is 1,
14583              and its mode is QI.  Without the conversion,l (ARRAY
14584              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
14585              +INDEX), which becomes (ARRAY+255+INDEX).  Opps!)  */
14586           if (! integer_zerop (low_bound))
14587             index = size_diffop (index, fold_convert (sizetype, low_bound));
14588
14589           string = exp1;
14590         }
14591
14592       if (string
14593           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
14594           && TREE_CODE (string) == STRING_CST
14595           && TREE_CODE (index) == INTEGER_CST
14596           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
14597           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
14598               == MODE_INT)
14599           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
14600         return build_int_cst_type (TREE_TYPE (exp),
14601                                    (TREE_STRING_POINTER (string)
14602                                     [TREE_INT_CST_LOW (index)]));
14603     }
14604   return NULL;
14605 }
14606
14607 /* Return the tree for neg (ARG0) when ARG0 is known to be either
14608    an integer constant, real, or fixed-point constant.
14609
14610    TYPE is the type of the result.  */
14611
14612 static tree
14613 fold_negate_const (tree arg0, tree type)
14614 {
14615   tree t = NULL_TREE;
14616
14617   switch (TREE_CODE (arg0))
14618     {
14619     case INTEGER_CST:
14620       {
14621         unsigned HOST_WIDE_INT low;
14622         HOST_WIDE_INT high;
14623         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14624                                    TREE_INT_CST_HIGH (arg0),
14625                                    &low, &high);
14626         t = force_fit_type_double (type, low, high, 1,
14627                                    (overflow | TREE_OVERFLOW (arg0))
14628                                    && !TYPE_UNSIGNED (type));
14629         break;
14630       }
14631
14632     case REAL_CST:
14633       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
14634       break;
14635
14636     case FIXED_CST:
14637       {
14638         FIXED_VALUE_TYPE f;
14639         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
14640                                             &(TREE_FIXED_CST (arg0)), NULL,
14641                                             TYPE_SATURATING (type));
14642         t = build_fixed (type, f);
14643         /* Propagate overflow flags.  */
14644         if (overflow_p | TREE_OVERFLOW (arg0))
14645           {
14646             TREE_OVERFLOW (t) = 1;
14647             TREE_CONSTANT_OVERFLOW (t) = 1;
14648           }
14649         else if (TREE_CONSTANT_OVERFLOW (arg0))
14650           TREE_CONSTANT_OVERFLOW (t) = 1;
14651         break;
14652       }
14653
14654     default:
14655       gcc_unreachable ();
14656     }
14657
14658   return t;
14659 }
14660
14661 /* Return the tree for abs (ARG0) when ARG0 is known to be either
14662    an integer constant or real constant.
14663
14664    TYPE is the type of the result.  */
14665
14666 tree
14667 fold_abs_const (tree arg0, tree type)
14668 {
14669   tree t = NULL_TREE;
14670
14671   switch (TREE_CODE (arg0))
14672     {
14673     case INTEGER_CST:
14674       /* If the value is unsigned, then the absolute value is
14675          the same as the ordinary value.  */
14676       if (TYPE_UNSIGNED (type))
14677         t = arg0;
14678       /* Similarly, if the value is non-negative.  */
14679       else if (INT_CST_LT (integer_minus_one_node, arg0))
14680         t = arg0;
14681       /* If the value is negative, then the absolute value is
14682          its negation.  */
14683       else
14684         {
14685           unsigned HOST_WIDE_INT low;
14686           HOST_WIDE_INT high;
14687           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14688                                      TREE_INT_CST_HIGH (arg0),
14689                                      &low, &high);
14690           t = force_fit_type_double (type, low, high, -1,
14691                                      overflow | TREE_OVERFLOW (arg0));
14692         }
14693       break;
14694
14695     case REAL_CST:
14696       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
14697         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
14698       else
14699         t =  arg0;
14700       break;
14701
14702     default:
14703       gcc_unreachable ();
14704     }
14705
14706   return t;
14707 }
14708
14709 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
14710    constant.  TYPE is the type of the result.  */
14711
14712 static tree
14713 fold_not_const (tree arg0, tree type)
14714 {
14715   tree t = NULL_TREE;
14716
14717   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
14718
14719   t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
14720                              ~TREE_INT_CST_HIGH (arg0), 0,
14721                              TREE_OVERFLOW (arg0));
14722
14723   return t;
14724 }
14725
14726 /* Given CODE, a relational operator, the target type, TYPE and two
14727    constant operands OP0 and OP1, return the result of the
14728    relational operation.  If the result is not a compile time
14729    constant, then return NULL_TREE.  */
14730
14731 static tree
14732 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
14733 {
14734   int result, invert;
14735
14736   /* From here on, the only cases we handle are when the result is
14737      known to be a constant.  */
14738
14739   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
14740     {
14741       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
14742       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
14743
14744       /* Handle the cases where either operand is a NaN.  */
14745       if (real_isnan (c0) || real_isnan (c1))
14746         {
14747           switch (code)
14748             {
14749             case EQ_EXPR:
14750             case ORDERED_EXPR:
14751               result = 0;
14752               break;
14753
14754             case NE_EXPR:
14755             case UNORDERED_EXPR:
14756             case UNLT_EXPR:
14757             case UNLE_EXPR:
14758             case UNGT_EXPR:
14759             case UNGE_EXPR:
14760             case UNEQ_EXPR:
14761               result = 1;
14762               break;
14763
14764             case LT_EXPR:
14765             case LE_EXPR:
14766             case GT_EXPR:
14767             case GE_EXPR:
14768             case LTGT_EXPR:
14769               if (flag_trapping_math)
14770                 return NULL_TREE;
14771               result = 0;
14772               break;
14773
14774             default:
14775               gcc_unreachable ();
14776             }
14777
14778           return constant_boolean_node (result, type);
14779         }
14780
14781       return constant_boolean_node (real_compare (code, c0, c1), type);
14782     }
14783
14784   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
14785     {
14786       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
14787       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
14788       return constant_boolean_node (fixed_compare (code, c0, c1), type);
14789     }
14790
14791   /* Handle equality/inequality of complex constants.  */
14792   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
14793     {
14794       tree rcond = fold_relational_const (code, type,
14795                                           TREE_REALPART (op0),
14796                                           TREE_REALPART (op1));
14797       tree icond = fold_relational_const (code, type,
14798                                           TREE_IMAGPART (op0),
14799                                           TREE_IMAGPART (op1));
14800       if (code == EQ_EXPR)
14801         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
14802       else if (code == NE_EXPR)
14803         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
14804       else
14805         return NULL_TREE;
14806     }
14807
14808   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14809
14810      To compute GT, swap the arguments and do LT.
14811      To compute GE, do LT and invert the result.
14812      To compute LE, swap the arguments, do LT and invert the result.
14813      To compute NE, do EQ and invert the result.
14814
14815      Therefore, the code below must handle only EQ and LT.  */
14816
14817   if (code == LE_EXPR || code == GT_EXPR)
14818     {
14819       tree tem = op0;
14820       op0 = op1;
14821       op1 = tem;
14822       code = swap_tree_comparison (code);
14823     }
14824
14825   /* Note that it is safe to invert for real values here because we
14826      have already handled the one case that it matters.  */
14827
14828   invert = 0;
14829   if (code == NE_EXPR || code == GE_EXPR)
14830     {
14831       invert = 1;
14832       code = invert_tree_comparison (code, false);
14833     }
14834
14835   /* Compute a result for LT or EQ if args permit;
14836      Otherwise return T.  */
14837   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14838     {
14839       if (code == EQ_EXPR)
14840         result = tree_int_cst_equal (op0, op1);
14841       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
14842         result = INT_CST_LT_UNSIGNED (op0, op1);
14843       else
14844         result = INT_CST_LT (op0, op1);
14845     }
14846   else
14847     return NULL_TREE;
14848
14849   if (invert)
14850     result ^= 1;
14851   return constant_boolean_node (result, type);
14852 }
14853
14854 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14855    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
14856    itself.  */
14857
14858 tree
14859 fold_build_cleanup_point_expr (tree type, tree expr)
14860 {
14861   /* If the expression does not have side effects then we don't have to wrap
14862      it with a cleanup point expression.  */
14863   if (!TREE_SIDE_EFFECTS (expr))
14864     return expr;
14865
14866   /* If the expression is a return, check to see if the expression inside the
14867      return has no side effects or the right hand side of the modify expression
14868      inside the return. If either don't have side effects set we don't need to
14869      wrap the expression in a cleanup point expression.  Note we don't check the
14870      left hand side of the modify because it should always be a return decl.  */
14871   if (TREE_CODE (expr) == RETURN_EXPR)
14872     {
14873       tree op = TREE_OPERAND (expr, 0);
14874       if (!op || !TREE_SIDE_EFFECTS (op))
14875         return expr;
14876       op = TREE_OPERAND (op, 1);
14877       if (!TREE_SIDE_EFFECTS (op))
14878         return expr;
14879     }
14880   
14881   return build1 (CLEANUP_POINT_EXPR, type, expr);
14882 }
14883
14884 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14885    of an indirection through OP0, or NULL_TREE if no simplification is
14886    possible.  */
14887
14888 tree
14889 fold_indirect_ref_1 (tree type, tree op0)
14890 {
14891   tree sub = op0;
14892   tree subtype;
14893
14894   STRIP_NOPS (sub);
14895   subtype = TREE_TYPE (sub);
14896   if (!POINTER_TYPE_P (subtype))
14897     return NULL_TREE;
14898
14899   if (TREE_CODE (sub) == ADDR_EXPR)
14900     {
14901       tree op = TREE_OPERAND (sub, 0);
14902       tree optype = TREE_TYPE (op);
14903       /* *&CONST_DECL -> to the value of the const decl.  */
14904       if (TREE_CODE (op) == CONST_DECL)
14905         return DECL_INITIAL (op);
14906       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
14907       if (type == optype)
14908         {
14909           tree fop = fold_read_from_constant_string (op);
14910           if (fop)
14911             return fop;
14912           else
14913             return op;
14914         }
14915       /* *(foo *)&fooarray => fooarray[0] */
14916       else if (TREE_CODE (optype) == ARRAY_TYPE
14917                && type == TREE_TYPE (optype))
14918         {
14919           tree type_domain = TYPE_DOMAIN (optype);
14920           tree min_val = size_zero_node;
14921           if (type_domain && TYPE_MIN_VALUE (type_domain))
14922             min_val = TYPE_MIN_VALUE (type_domain);
14923           return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
14924         }
14925       /* *(foo *)&complexfoo => __real__ complexfoo */
14926       else if (TREE_CODE (optype) == COMPLEX_TYPE
14927                && type == TREE_TYPE (optype))
14928         return fold_build1 (REALPART_EXPR, type, op);
14929       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14930       else if (TREE_CODE (optype) == VECTOR_TYPE
14931                && type == TREE_TYPE (optype))
14932         {
14933           tree part_width = TYPE_SIZE (type);
14934           tree index = bitsize_int (0);
14935           return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
14936         }
14937     }
14938
14939   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14940   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14941       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14942     {
14943       tree op00 = TREE_OPERAND (sub, 0);
14944       tree op01 = TREE_OPERAND (sub, 1);
14945       tree op00type;
14946
14947       STRIP_NOPS (op00);
14948       op00type = TREE_TYPE (op00);
14949       if (TREE_CODE (op00) == ADDR_EXPR
14950           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
14951           && type == TREE_TYPE (TREE_TYPE (op00type)))
14952         {
14953           tree size = TYPE_SIZE_UNIT (type);
14954           if (tree_int_cst_equal (size, op01))
14955             return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
14956         }
14957     }
14958   
14959   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14960   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14961       && type == TREE_TYPE (TREE_TYPE (subtype)))
14962     {
14963       tree type_domain;
14964       tree min_val = size_zero_node;
14965       sub = build_fold_indirect_ref (sub);
14966       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14967       if (type_domain && TYPE_MIN_VALUE (type_domain))
14968         min_val = TYPE_MIN_VALUE (type_domain);
14969       return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
14970     }
14971
14972   return NULL_TREE;
14973 }
14974
14975 /* Builds an expression for an indirection through T, simplifying some
14976    cases.  */
14977
14978 tree
14979 build_fold_indirect_ref (tree t)
14980 {
14981   tree type = TREE_TYPE (TREE_TYPE (t));
14982   tree sub = fold_indirect_ref_1 (type, t);
14983
14984   if (sub)
14985     return sub;
14986   else
14987     return build1 (INDIRECT_REF, type, t);
14988 }
14989
14990 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
14991
14992 tree
14993 fold_indirect_ref (tree t)
14994 {
14995   tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
14996
14997   if (sub)
14998     return sub;
14999   else
15000     return t;
15001 }
15002
15003 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15004    whose result is ignored.  The type of the returned tree need not be
15005    the same as the original expression.  */
15006
15007 tree
15008 fold_ignored_result (tree t)
15009 {
15010   if (!TREE_SIDE_EFFECTS (t))
15011     return integer_zero_node;
15012
15013   for (;;)
15014     switch (TREE_CODE_CLASS (TREE_CODE (t)))
15015       {
15016       case tcc_unary:
15017         t = TREE_OPERAND (t, 0);
15018         break;
15019
15020       case tcc_binary:
15021       case tcc_comparison:
15022         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15023           t = TREE_OPERAND (t, 0);
15024         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15025           t = TREE_OPERAND (t, 1);
15026         else
15027           return t;
15028         break;
15029
15030       case tcc_expression:
15031         switch (TREE_CODE (t))
15032           {
15033           case COMPOUND_EXPR:
15034             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15035               return t;
15036             t = TREE_OPERAND (t, 0);
15037             break;
15038
15039           case COND_EXPR:
15040             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15041                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15042               return t;
15043             t = TREE_OPERAND (t, 0);
15044             break;
15045
15046           default:
15047             return t;
15048           }
15049         break;
15050
15051       default:
15052         return t;
15053       }
15054 }
15055
15056 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15057    This can only be applied to objects of a sizetype.  */
15058
15059 tree
15060 round_up (tree value, int divisor)
15061 {
15062   tree div = NULL_TREE;
15063
15064   gcc_assert (divisor > 0);
15065   if (divisor == 1)
15066     return value;
15067
15068   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15069      have to do anything.  Only do this when we are not given a const,
15070      because in that case, this check is more expensive than just
15071      doing it.  */
15072   if (TREE_CODE (value) != INTEGER_CST)
15073     {
15074       div = build_int_cst (TREE_TYPE (value), divisor);
15075
15076       if (multiple_of_p (TREE_TYPE (value), value, div))
15077         return value;
15078     }
15079
15080   /* If divisor is a power of two, simplify this to bit manipulation.  */
15081   if (divisor == (divisor & -divisor))
15082     {
15083       if (TREE_CODE (value) == INTEGER_CST)
15084         {
15085           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15086           unsigned HOST_WIDE_INT high;
15087           bool overflow_p;
15088
15089           if ((low & (divisor - 1)) == 0)
15090             return value;
15091
15092           overflow_p = TREE_OVERFLOW (value);
15093           high = TREE_INT_CST_HIGH (value);
15094           low &= ~(divisor - 1);
15095           low += divisor;
15096           if (low == 0)
15097             {
15098               high++;
15099               if (high == 0)
15100                 overflow_p = true;
15101             }
15102
15103           return force_fit_type_double (TREE_TYPE (value), low, high,
15104                                         -1, overflow_p);
15105         }
15106       else
15107         {
15108           tree t;
15109
15110           t = build_int_cst (TREE_TYPE (value), divisor - 1);
15111           value = size_binop (PLUS_EXPR, value, t);
15112           t = build_int_cst (TREE_TYPE (value), -divisor);
15113           value = size_binop (BIT_AND_EXPR, value, t);
15114         }
15115     }
15116   else
15117     {
15118       if (!div)
15119         div = build_int_cst (TREE_TYPE (value), divisor);
15120       value = size_binop (CEIL_DIV_EXPR, value, div);
15121       value = size_binop (MULT_EXPR, value, div);
15122     }
15123
15124   return value;
15125 }
15126
15127 /* Likewise, but round down.  */
15128
15129 tree
15130 round_down (tree value, int divisor)
15131 {
15132   tree div = NULL_TREE;
15133
15134   gcc_assert (divisor > 0);
15135   if (divisor == 1)
15136     return value;
15137
15138   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15139      have to do anything.  Only do this when we are not given a const,
15140      because in that case, this check is more expensive than just
15141      doing it.  */
15142   if (TREE_CODE (value) != INTEGER_CST)
15143     {
15144       div = build_int_cst (TREE_TYPE (value), divisor);
15145
15146       if (multiple_of_p (TREE_TYPE (value), value, div))
15147         return value;
15148     }
15149
15150   /* If divisor is a power of two, simplify this to bit manipulation.  */
15151   if (divisor == (divisor & -divisor))
15152     {
15153       tree t;
15154
15155       t = build_int_cst (TREE_TYPE (value), -divisor);
15156       value = size_binop (BIT_AND_EXPR, value, t);
15157     }
15158   else
15159     {
15160       if (!div)
15161         div = build_int_cst (TREE_TYPE (value), divisor);
15162       value = size_binop (FLOOR_DIV_EXPR, value, div);
15163       value = size_binop (MULT_EXPR, value, div);
15164     }
15165
15166   return value;
15167 }
15168
15169 /* Returns the pointer to the base of the object addressed by EXP and
15170    extracts the information about the offset of the access, storing it
15171    to PBITPOS and POFFSET.  */
15172
15173 static tree
15174 split_address_to_core_and_offset (tree exp,
15175                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15176 {
15177   tree core;
15178   enum machine_mode mode;
15179   int unsignedp, volatilep;
15180   HOST_WIDE_INT bitsize;
15181
15182   if (TREE_CODE (exp) == ADDR_EXPR)
15183     {
15184       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15185                                   poffset, &mode, &unsignedp, &volatilep,
15186                                   false);
15187       core = fold_addr_expr (core);
15188     }
15189   else
15190     {
15191       core = exp;
15192       *pbitpos = 0;
15193       *poffset = NULL_TREE;
15194     }
15195
15196   return core;
15197 }
15198
15199 /* Returns true if addresses of E1 and E2 differ by a constant, false
15200    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
15201
15202 bool
15203 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15204 {
15205   tree core1, core2;
15206   HOST_WIDE_INT bitpos1, bitpos2;
15207   tree toffset1, toffset2, tdiff, type;
15208
15209   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15210   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15211
15212   if (bitpos1 % BITS_PER_UNIT != 0
15213       || bitpos2 % BITS_PER_UNIT != 0
15214       || !operand_equal_p (core1, core2, 0))
15215     return false;
15216
15217   if (toffset1 && toffset2)
15218     {
15219       type = TREE_TYPE (toffset1);
15220       if (type != TREE_TYPE (toffset2))
15221         toffset2 = fold_convert (type, toffset2);
15222
15223       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15224       if (!cst_and_fits_in_hwi (tdiff))
15225         return false;
15226
15227       *diff = int_cst_value (tdiff);
15228     }
15229   else if (toffset1 || toffset2)
15230     {
15231       /* If only one of the offsets is non-constant, the difference cannot
15232          be a constant.  */
15233       return false;
15234     }
15235   else
15236     *diff = 0;
15237
15238   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15239   return true;
15240 }
15241
15242 /* Simplify the floating point expression EXP when the sign of the
15243    result is not significant.  Return NULL_TREE if no simplification
15244    is possible.  */
15245
15246 tree
15247 fold_strip_sign_ops (tree exp)
15248 {
15249   tree arg0, arg1;
15250
15251   switch (TREE_CODE (exp))
15252     {
15253     case ABS_EXPR:
15254     case NEGATE_EXPR:
15255       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15256       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15257
15258     case MULT_EXPR:
15259     case RDIV_EXPR:
15260       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15261         return NULL_TREE;
15262       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15263       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15264       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15265         return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15266                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
15267                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
15268       break;
15269
15270     case COMPOUND_EXPR:
15271       arg0 = TREE_OPERAND (exp, 0);
15272       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15273       if (arg1)
15274         return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15275       break;
15276       
15277     case COND_EXPR:
15278       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15279       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15280       if (arg0 || arg1)
15281         return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15282                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
15283                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
15284       break;
15285       
15286     case CALL_EXPR:
15287       {
15288         const enum built_in_function fcode = builtin_mathfn_code (exp);
15289         switch (fcode)
15290         {
15291         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15292           /* Strip copysign function call, return the 1st argument. */
15293           arg0 = CALL_EXPR_ARG (exp, 0);
15294           arg1 = CALL_EXPR_ARG (exp, 1);
15295           return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15296
15297         default:
15298           /* Strip sign ops from the argument of "odd" math functions.  */
15299           if (negate_mathfn_p (fcode))
15300             {
15301               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15302               if (arg0)
15303                 return build_call_expr (get_callee_fndecl (exp), 1, arg0);
15304             }
15305           break;
15306         }
15307       }
15308       break;
15309
15310     default:
15311       break;
15312     }
15313   return NULL_TREE;
15314 }