OSDN Git Service

fb664fee8be138ea7b6c954107c5fcf7810c365b
[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 make_bit_field_ref (tree, tree, int, int, int);
113 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
114 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
115                                     enum machine_mode *, int *, int *,
116                                     tree *, tree *);
117 static int all_ones_mask_p (const_tree, int);
118 static tree sign_bit_p (tree, const_tree);
119 static int simple_operand_p (const_tree);
120 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
121 static tree range_predecessor (tree);
122 static tree range_successor (tree);
123 static tree make_range (tree, int *, tree *, tree *, bool *);
124 static tree build_range_check (tree, tree, int, tree, tree);
125 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
126                          tree);
127 static tree fold_range_test (enum tree_code, tree, tree, tree);
128 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
129 static tree unextend (tree, int, int, tree);
130 static tree fold_truthop (enum tree_code, tree, tree, tree);
131 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
132 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
133 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
134 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
135                                                  tree, tree,
136                                                  tree, tree, int);
137 static bool fold_real_zero_addition_p (const_tree, const_tree, int);
138 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
139                                  tree, tree, tree);
140 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
141 static tree fold_div_compare (enum tree_code, tree, tree, tree);
142 static bool reorder_operands_p (const_tree, const_tree);
143 static tree fold_negate_const (tree, tree);
144 static tree fold_not_const (tree, tree);
145 static tree fold_relational_const (enum tree_code, tree, tree, tree);
146
147
148 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
149    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
150    and SUM1.  Then this yields nonzero if overflow occurred during the
151    addition.
152
153    Overflow occurs if A and B have the same sign, but A and SUM differ in
154    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
155    sign.  */
156 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
157 \f
158 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
159    We do that by representing the two-word integer in 4 words, with only
160    HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
161    number.  The value of the word is LOWPART + HIGHPART * BASE.  */
162
163 #define LOWPART(x) \
164   ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
165 #define HIGHPART(x) \
166   ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
167 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
168
169 /* Unpack a two-word integer into 4 words.
170    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
171    WORDS points to the array of HOST_WIDE_INTs.  */
172
173 static void
174 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
175 {
176   words[0] = LOWPART (low);
177   words[1] = HIGHPART (low);
178   words[2] = LOWPART (hi);
179   words[3] = HIGHPART (hi);
180 }
181
182 /* Pack an array of 4 words into a two-word integer.
183    WORDS points to the array of words.
184    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
185
186 static void
187 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
188         HOST_WIDE_INT *hi)
189 {
190   *low = words[0] + words[1] * BASE;
191   *hi = words[2] + words[3] * BASE;
192 }
193 \f
194 /* Force the double-word integer L1, H1 to be within the range of the
195    integer type TYPE.  Stores the properly truncated and sign-extended
196    double-word integer in *LV, *HV.  Returns true if the operation
197    overflows, that is, argument and result are different.  */
198
199 int
200 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
201                  unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
202 {
203   unsigned HOST_WIDE_INT low0 = l1;
204   HOST_WIDE_INT high0 = h1;
205   unsigned int prec;
206   int sign_extended_type;
207
208   if (POINTER_TYPE_P (type)
209       || TREE_CODE (type) == OFFSET_TYPE)
210     prec = POINTER_SIZE;
211   else
212     prec = TYPE_PRECISION (type);
213
214   /* Size types *are* sign extended.  */
215   sign_extended_type = (!TYPE_UNSIGNED (type)
216                         || (TREE_CODE (type) == INTEGER_TYPE
217                             && TYPE_IS_SIZETYPE (type)));
218
219   /* First clear all bits that are beyond the type's precision.  */
220   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
221     ;
222   else if (prec > HOST_BITS_PER_WIDE_INT)
223     h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
224   else
225     {
226       h1 = 0;
227       if (prec < HOST_BITS_PER_WIDE_INT)
228         l1 &= ~((HOST_WIDE_INT) (-1) << prec);
229     }
230
231   /* Then do sign extension if necessary.  */
232   if (!sign_extended_type)
233     /* No sign extension */;
234   else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
235     /* Correct width already.  */;
236   else if (prec > HOST_BITS_PER_WIDE_INT)
237     {
238       /* Sign extend top half? */
239       if (h1 & ((unsigned HOST_WIDE_INT)1
240                 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
241         h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
242     }
243   else if (prec == HOST_BITS_PER_WIDE_INT)
244     {
245       if ((HOST_WIDE_INT)l1 < 0)
246         h1 = -1;
247     }
248   else
249     {
250       /* Sign extend bottom half? */
251       if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
252         {
253           h1 = -1;
254           l1 |= (HOST_WIDE_INT)(-1) << prec;
255         }
256     }
257
258   *lv = l1;
259   *hv = h1;
260
261   /* If the value didn't fit, signal overflow.  */
262   return l1 != low0 || h1 != high0;
263 }
264
265 /* We force the double-int HIGH:LOW to the range of the type TYPE by
266    sign or zero extending it.
267    OVERFLOWABLE indicates if we are interested
268    in overflow of the value, when >0 we are only interested in signed
269    overflow, for <0 we are interested in any overflow.  OVERFLOWED
270    indicates whether overflow has already occurred.  CONST_OVERFLOWED
271    indicates whether constant overflow has already occurred.  We force
272    T's value to be within range of T's type (by setting to 0 or 1 all
273    the bits outside the type's range).  We set TREE_OVERFLOWED if,
274         OVERFLOWED is nonzero,
275         or OVERFLOWABLE is >0 and signed overflow occurs
276         or OVERFLOWABLE is <0 and any overflow occurs
277    We return a new tree node for the extended double-int.  The node
278    is shared if no overflow flags are set.  */
279
280 tree
281 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
282                        HOST_WIDE_INT high, int overflowable,
283                        bool overflowed)
284 {
285   int sign_extended_type;
286   bool overflow;
287
288   /* Size types *are* sign extended.  */
289   sign_extended_type = (!TYPE_UNSIGNED (type)
290                         || (TREE_CODE (type) == INTEGER_TYPE
291                             && TYPE_IS_SIZETYPE (type)));
292
293   overflow = fit_double_type (low, high, &low, &high, type);
294
295   /* If we need to set overflow flags, return a new unshared node.  */
296   if (overflowed || overflow)
297     {
298       if (overflowed
299           || overflowable < 0
300           || (overflowable > 0 && sign_extended_type))
301         {
302           tree t = make_node (INTEGER_CST);
303           TREE_INT_CST_LOW (t) = low;
304           TREE_INT_CST_HIGH (t) = high;
305           TREE_TYPE (t) = type;
306           TREE_OVERFLOW (t) = 1;
307           return t;
308         }
309     }
310
311   /* Else build a shared node.  */
312   return build_int_cst_wide (type, low, high);
313 }
314 \f
315 /* Add two doubleword integers with doubleword result.
316    Return nonzero if the operation overflows according to UNSIGNED_P.
317    Each argument is given as two `HOST_WIDE_INT' pieces.
318    One argument is L1 and H1; the other, L2 and H2.
319    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
320
321 int
322 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
323                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
324                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
325                       bool unsigned_p)
326 {
327   unsigned HOST_WIDE_INT l;
328   HOST_WIDE_INT h;
329
330   l = l1 + l2;
331   h = h1 + h2 + (l < l1);
332
333   *lv = l;
334   *hv = h;
335
336   if (unsigned_p)
337     return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
338   else
339     return OVERFLOW_SUM_SIGN (h1, h2, h);
340 }
341
342 /* Negate a doubleword integer with doubleword result.
343    Return nonzero if the operation overflows, assuming it's signed.
344    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
345    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
346
347 int
348 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
349             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
350 {
351   if (l1 == 0)
352     {
353       *lv = 0;
354       *hv = - h1;
355       return (*hv & h1) < 0;
356     }
357   else
358     {
359       *lv = -l1;
360       *hv = ~h1;
361       return 0;
362     }
363 }
364 \f
365 /* Multiply two doubleword integers with doubleword result.
366    Return nonzero if the operation overflows according to UNSIGNED_P.
367    Each argument is given as two `HOST_WIDE_INT' pieces.
368    One argument is L1 and H1; the other, L2 and H2.
369    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
370
371 int
372 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
373                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
374                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
375                       bool unsigned_p)
376 {
377   HOST_WIDE_INT arg1[4];
378   HOST_WIDE_INT arg2[4];
379   HOST_WIDE_INT prod[4 * 2];
380   unsigned HOST_WIDE_INT carry;
381   int i, j, k;
382   unsigned HOST_WIDE_INT toplow, neglow;
383   HOST_WIDE_INT tophigh, neghigh;
384
385   encode (arg1, l1, h1);
386   encode (arg2, l2, h2);
387
388   memset (prod, 0, sizeof prod);
389
390   for (i = 0; i < 4; i++)
391     {
392       carry = 0;
393       for (j = 0; j < 4; j++)
394         {
395           k = i + j;
396           /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
397           carry += arg1[i] * arg2[j];
398           /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
399           carry += prod[k];
400           prod[k] = LOWPART (carry);
401           carry = HIGHPART (carry);
402         }
403       prod[i + 4] = carry;
404     }
405
406   decode (prod, lv, hv);
407   decode (prod + 4, &toplow, &tophigh);
408
409   /* Unsigned overflow is immediate.  */
410   if (unsigned_p)
411     return (toplow | tophigh) != 0;
412
413   /* Check for signed overflow by calculating the signed representation of the
414      top half of the result; it should agree with the low half's sign bit.  */
415   if (h1 < 0)
416     {
417       neg_double (l2, h2, &neglow, &neghigh);
418       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
419     }
420   if (h2 < 0)
421     {
422       neg_double (l1, h1, &neglow, &neghigh);
423       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
424     }
425   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
426 }
427 \f
428 /* Shift the doubleword integer in L1, H1 left by COUNT places
429    keeping only PREC bits of result.
430    Shift right if COUNT is negative.
431    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
432    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
433
434 void
435 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
436                HOST_WIDE_INT count, unsigned int prec,
437                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
438 {
439   unsigned HOST_WIDE_INT signmask;
440
441   if (count < 0)
442     {
443       rshift_double (l1, h1, -count, prec, lv, hv, arith);
444       return;
445     }
446
447   if (SHIFT_COUNT_TRUNCATED)
448     count %= prec;
449
450   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
451     {
452       /* Shifting by the host word size is undefined according to the
453          ANSI standard, so we must handle this as a special case.  */
454       *hv = 0;
455       *lv = 0;
456     }
457   else if (count >= HOST_BITS_PER_WIDE_INT)
458     {
459       *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
460       *lv = 0;
461     }
462   else
463     {
464       *hv = (((unsigned HOST_WIDE_INT) h1 << count)
465              | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
466       *lv = l1 << count;
467     }
468
469   /* Sign extend all bits that are beyond the precision.  */
470
471   signmask = -((prec > HOST_BITS_PER_WIDE_INT
472                 ? ((unsigned HOST_WIDE_INT) *hv
473                    >> (prec - HOST_BITS_PER_WIDE_INT - 1))
474                 : (*lv >> (prec - 1))) & 1);
475
476   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
477     ;
478   else if (prec >= HOST_BITS_PER_WIDE_INT)
479     {
480       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
481       *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
482     }
483   else
484     {
485       *hv = signmask;
486       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
487       *lv |= signmask << prec;
488     }
489 }
490
491 /* Shift the doubleword integer in L1, H1 right by COUNT places
492    keeping only PREC bits of result.  COUNT must be positive.
493    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
494    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
495
496 void
497 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
498                HOST_WIDE_INT count, unsigned int prec,
499                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
500                int arith)
501 {
502   unsigned HOST_WIDE_INT signmask;
503
504   signmask = (arith
505               ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
506               : 0);
507
508   if (SHIFT_COUNT_TRUNCATED)
509     count %= prec;
510
511   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
512     {
513       /* Shifting by the host word size is undefined according to the
514          ANSI standard, so we must handle this as a special case.  */
515       *hv = 0;
516       *lv = 0;
517     }
518   else if (count >= HOST_BITS_PER_WIDE_INT)
519     {
520       *hv = 0;
521       *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
522     }
523   else
524     {
525       *hv = (unsigned HOST_WIDE_INT) h1 >> count;
526       *lv = ((l1 >> count)
527              | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
528     }
529
530   /* Zero / sign extend all bits that are beyond the precision.  */
531
532   if (count >= (HOST_WIDE_INT)prec)
533     {
534       *hv = signmask;
535       *lv = signmask;
536     }
537   else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
538     ;
539   else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
540     {
541       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
542       *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
543     }
544   else
545     {
546       *hv = signmask;
547       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
548       *lv |= signmask << (prec - count);
549     }
550 }
551 \f
552 /* Rotate the doubleword integer in L1, H1 left by COUNT places
553    keeping only PREC bits of result.
554    Rotate right if COUNT is negative.
555    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
556
557 void
558 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
559                 HOST_WIDE_INT count, unsigned int prec,
560                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
561 {
562   unsigned HOST_WIDE_INT s1l, s2l;
563   HOST_WIDE_INT s1h, s2h;
564
565   count %= prec;
566   if (count < 0)
567     count += prec;
568
569   lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
570   rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
571   *lv = s1l | s2l;
572   *hv = s1h | s2h;
573 }
574
575 /* Rotate the doubleword integer in L1, H1 left by COUNT places
576    keeping only PREC bits of result.  COUNT must be positive.
577    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
578
579 void
580 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
581                 HOST_WIDE_INT count, unsigned int prec,
582                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
583 {
584   unsigned HOST_WIDE_INT s1l, s2l;
585   HOST_WIDE_INT s1h, s2h;
586
587   count %= prec;
588   if (count < 0)
589     count += prec;
590
591   rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
592   lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
593   *lv = s1l | s2l;
594   *hv = s1h | s2h;
595 }
596 \f
597 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
598    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
599    CODE is a tree code for a kind of division, one of
600    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
601    or EXACT_DIV_EXPR
602    It controls how the quotient is rounded to an integer.
603    Return nonzero if the operation overflows.
604    UNS nonzero says do unsigned division.  */
605
606 int
607 div_and_round_double (enum tree_code code, int uns,
608                       unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
609                       HOST_WIDE_INT hnum_orig,
610                       unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
611                       HOST_WIDE_INT hden_orig,
612                       unsigned HOST_WIDE_INT *lquo,
613                       HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
614                       HOST_WIDE_INT *hrem)
615 {
616   int quo_neg = 0;
617   HOST_WIDE_INT num[4 + 1];     /* extra element for scaling.  */
618   HOST_WIDE_INT den[4], quo[4];
619   int i, j;
620   unsigned HOST_WIDE_INT work;
621   unsigned HOST_WIDE_INT carry = 0;
622   unsigned HOST_WIDE_INT lnum = lnum_orig;
623   HOST_WIDE_INT hnum = hnum_orig;
624   unsigned HOST_WIDE_INT lden = lden_orig;
625   HOST_WIDE_INT hden = hden_orig;
626   int overflow = 0;
627
628   if (hden == 0 && lden == 0)
629     overflow = 1, lden = 1;
630
631   /* Calculate quotient sign and convert operands to unsigned.  */
632   if (!uns)
633     {
634       if (hnum < 0)
635         {
636           quo_neg = ~ quo_neg;
637           /* (minimum integer) / (-1) is the only overflow case.  */
638           if (neg_double (lnum, hnum, &lnum, &hnum)
639               && ((HOST_WIDE_INT) lden & hden) == -1)
640             overflow = 1;
641         }
642       if (hden < 0)
643         {
644           quo_neg = ~ quo_neg;
645           neg_double (lden, hden, &lden, &hden);
646         }
647     }
648
649   if (hnum == 0 && hden == 0)
650     {                           /* single precision */
651       *hquo = *hrem = 0;
652       /* This unsigned division rounds toward zero.  */
653       *lquo = lnum / lden;
654       goto finish_up;
655     }
656
657   if (hnum == 0)
658     {                           /* trivial case: dividend < divisor */
659       /* hden != 0 already checked.  */
660       *hquo = *lquo = 0;
661       *hrem = hnum;
662       *lrem = lnum;
663       goto finish_up;
664     }
665
666   memset (quo, 0, sizeof quo);
667
668   memset (num, 0, sizeof num);  /* to zero 9th element */
669   memset (den, 0, sizeof den);
670
671   encode (num, lnum, hnum);
672   encode (den, lden, hden);
673
674   /* Special code for when the divisor < BASE.  */
675   if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
676     {
677       /* hnum != 0 already checked.  */
678       for (i = 4 - 1; i >= 0; i--)
679         {
680           work = num[i] + carry * BASE;
681           quo[i] = work / lden;
682           carry = work % lden;
683         }
684     }
685   else
686     {
687       /* Full double precision division,
688          with thanks to Don Knuth's "Seminumerical Algorithms".  */
689       int num_hi_sig, den_hi_sig;
690       unsigned HOST_WIDE_INT quo_est, scale;
691
692       /* Find the highest nonzero divisor digit.  */
693       for (i = 4 - 1;; i--)
694         if (den[i] != 0)
695           {
696             den_hi_sig = i;
697             break;
698           }
699
700       /* Insure that the first digit of the divisor is at least BASE/2.
701          This is required by the quotient digit estimation algorithm.  */
702
703       scale = BASE / (den[den_hi_sig] + 1);
704       if (scale > 1)
705         {               /* scale divisor and dividend */
706           carry = 0;
707           for (i = 0; i <= 4 - 1; i++)
708             {
709               work = (num[i] * scale) + carry;
710               num[i] = LOWPART (work);
711               carry = HIGHPART (work);
712             }
713
714           num[4] = carry;
715           carry = 0;
716           for (i = 0; i <= 4 - 1; i++)
717             {
718               work = (den[i] * scale) + carry;
719               den[i] = LOWPART (work);
720               carry = HIGHPART (work);
721               if (den[i] != 0) den_hi_sig = i;
722             }
723         }
724
725       num_hi_sig = 4;
726
727       /* Main loop */
728       for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
729         {
730           /* Guess the next quotient digit, quo_est, by dividing the first
731              two remaining dividend digits by the high order quotient digit.
732              quo_est is never low and is at most 2 high.  */
733           unsigned HOST_WIDE_INT tmp;
734
735           num_hi_sig = i + den_hi_sig + 1;
736           work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
737           if (num[num_hi_sig] != den[den_hi_sig])
738             quo_est = work / den[den_hi_sig];
739           else
740             quo_est = BASE - 1;
741
742           /* Refine quo_est so it's usually correct, and at most one high.  */
743           tmp = work - quo_est * den[den_hi_sig];
744           if (tmp < BASE
745               && (den[den_hi_sig - 1] * quo_est
746                   > (tmp * BASE + num[num_hi_sig - 2])))
747             quo_est--;
748
749           /* Try QUO_EST as the quotient digit, by multiplying the
750              divisor by QUO_EST and subtracting from the remaining dividend.
751              Keep in mind that QUO_EST is the I - 1st digit.  */
752
753           carry = 0;
754           for (j = 0; j <= den_hi_sig; j++)
755             {
756               work = quo_est * den[j] + carry;
757               carry = HIGHPART (work);
758               work = num[i + j] - LOWPART (work);
759               num[i + j] = LOWPART (work);
760               carry += HIGHPART (work) != 0;
761             }
762
763           /* If quo_est was high by one, then num[i] went negative and
764              we need to correct things.  */
765           if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
766             {
767               quo_est--;
768               carry = 0;                /* add divisor back in */
769               for (j = 0; j <= den_hi_sig; j++)
770                 {
771                   work = num[i + j] + den[j] + carry;
772                   carry = HIGHPART (work);
773                   num[i + j] = LOWPART (work);
774                 }
775
776               num [num_hi_sig] += carry;
777             }
778
779           /* Store the quotient digit.  */
780           quo[i] = quo_est;
781         }
782     }
783
784   decode (quo, lquo, hquo);
785
786  finish_up:
787   /* If result is negative, make it so.  */
788   if (quo_neg)
789     neg_double (*lquo, *hquo, lquo, hquo);
790
791   /* Compute trial remainder:  rem = num - (quo * den)  */
792   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
793   neg_double (*lrem, *hrem, lrem, hrem);
794   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
795
796   switch (code)
797     {
798     case TRUNC_DIV_EXPR:
799     case TRUNC_MOD_EXPR:        /* round toward zero */
800     case EXACT_DIV_EXPR:        /* for this one, it shouldn't matter */
801       return overflow;
802
803     case FLOOR_DIV_EXPR:
804     case FLOOR_MOD_EXPR:        /* round toward negative infinity */
805       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
806         {
807           /* quo = quo - 1;  */
808           add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
809                       lquo, hquo);
810         }
811       else
812         return overflow;
813       break;
814
815     case CEIL_DIV_EXPR:
816     case CEIL_MOD_EXPR:         /* round toward positive infinity */
817       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
818         {
819           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
820                       lquo, hquo);
821         }
822       else
823         return overflow;
824       break;
825
826     case ROUND_DIV_EXPR:
827     case ROUND_MOD_EXPR:        /* round to closest integer */
828       {
829         unsigned HOST_WIDE_INT labs_rem = *lrem;
830         HOST_WIDE_INT habs_rem = *hrem;
831         unsigned HOST_WIDE_INT labs_den = lden, ltwice;
832         HOST_WIDE_INT habs_den = hden, htwice;
833
834         /* Get absolute values.  */
835         if (*hrem < 0)
836           neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
837         if (hden < 0)
838           neg_double (lden, hden, &labs_den, &habs_den);
839
840         /* If (2 * abs (lrem) >= abs (lden)) */
841         mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
842                     labs_rem, habs_rem, &ltwice, &htwice);
843
844         if (((unsigned HOST_WIDE_INT) habs_den
845              < (unsigned HOST_WIDE_INT) htwice)
846             || (((unsigned HOST_WIDE_INT) habs_den
847                  == (unsigned HOST_WIDE_INT) htwice)
848                 && (labs_den < ltwice)))
849           {
850             if (*hquo < 0)
851               /* quo = quo - 1;  */
852               add_double (*lquo, *hquo,
853                           (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
854             else
855               /* quo = quo + 1; */
856               add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
857                           lquo, hquo);
858           }
859         else
860           return overflow;
861       }
862       break;
863
864     default:
865       gcc_unreachable ();
866     }
867
868   /* Compute true remainder:  rem = num - (quo * den)  */
869   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
870   neg_double (*lrem, *hrem, lrem, hrem);
871   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
872   return overflow;
873 }
874
875 /* If ARG2 divides ARG1 with zero remainder, carries out the division
876    of type CODE and returns the quotient.
877    Otherwise returns NULL_TREE.  */
878
879 static tree
880 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
881 {
882   unsigned HOST_WIDE_INT int1l, int2l;
883   HOST_WIDE_INT int1h, int2h;
884   unsigned HOST_WIDE_INT quol, reml;
885   HOST_WIDE_INT quoh, remh;
886   tree type = TREE_TYPE (arg1);
887   int uns = TYPE_UNSIGNED (type);
888
889   int1l = TREE_INT_CST_LOW (arg1);
890   int1h = TREE_INT_CST_HIGH (arg1);
891   /* &obj[0] + -128 really should be compiled as &obj[-8] rather than
892      &obj[some_exotic_number].  */
893   if (POINTER_TYPE_P (type))
894     {
895       uns = false;
896       type = signed_type_for (type);
897       fit_double_type (int1l, int1h, &int1l, &int1h,
898                        type);
899     }
900   else
901     fit_double_type (int1l, int1h, &int1l, &int1h, type);
902   int2l = TREE_INT_CST_LOW (arg2);
903   int2h = TREE_INT_CST_HIGH (arg2);
904
905   div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
906                         &quol, &quoh, &reml, &remh);
907   if (remh != 0 || reml != 0)
908     return NULL_TREE;
909
910   return build_int_cst_wide (type, quol, quoh);
911 }
912 \f
913 /* This is nonzero if we should defer warnings about undefined
914    overflow.  This facility exists because these warnings are a
915    special case.  The code to estimate loop iterations does not want
916    to issue any warnings, since it works with expressions which do not
917    occur in user code.  Various bits of cleanup code call fold(), but
918    only use the result if it has certain characteristics (e.g., is a
919    constant); that code only wants to issue a warning if the result is
920    used.  */
921
922 static int fold_deferring_overflow_warnings;
923
924 /* If a warning about undefined overflow is deferred, this is the
925    warning.  Note that this may cause us to turn two warnings into
926    one, but that is fine since it is sufficient to only give one
927    warning per expression.  */
928
929 static const char* fold_deferred_overflow_warning;
930
931 /* If a warning about undefined overflow is deferred, this is the
932    level at which the warning should be emitted.  */
933
934 static enum warn_strict_overflow_code fold_deferred_overflow_code;
935
936 /* Start deferring overflow warnings.  We could use a stack here to
937    permit nested calls, but at present it is not necessary.  */
938
939 void
940 fold_defer_overflow_warnings (void)
941 {
942   ++fold_deferring_overflow_warnings;
943 }
944
945 /* Stop deferring overflow warnings.  If there is a pending warning,
946    and ISSUE is true, then issue the warning if appropriate.  STMT is
947    the statement with which the warning should be associated (used for
948    location information); STMT may be NULL.  CODE is the level of the
949    warning--a warn_strict_overflow_code value.  This function will use
950    the smaller of CODE and the deferred code when deciding whether to
951    issue the warning.  CODE may be zero to mean to always use the
952    deferred code.  */
953
954 void
955 fold_undefer_overflow_warnings (bool issue, const_tree stmt, int code)
956 {
957   const char *warnmsg;
958   location_t locus;
959
960   gcc_assert (fold_deferring_overflow_warnings > 0);
961   --fold_deferring_overflow_warnings;
962   if (fold_deferring_overflow_warnings > 0)
963     {
964       if (fold_deferred_overflow_warning != NULL
965           && code != 0
966           && code < (int) fold_deferred_overflow_code)
967         fold_deferred_overflow_code = code;
968       return;
969     }
970
971   warnmsg = fold_deferred_overflow_warning;
972   fold_deferred_overflow_warning = NULL;
973
974   if (!issue || warnmsg == NULL)
975     return;
976
977   /* Use the smallest code level when deciding to issue the
978      warning.  */
979   if (code == 0 || code > (int) fold_deferred_overflow_code)
980     code = fold_deferred_overflow_code;
981
982   if (!issue_strict_overflow_warning (code))
983     return;
984
985   if (stmt == NULL_TREE || !expr_has_location (stmt))
986     locus = input_location;
987   else
988     locus = expr_location (stmt);
989   warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
990 }
991
992 /* Stop deferring overflow warnings, ignoring any deferred
993    warnings.  */
994
995 void
996 fold_undefer_and_ignore_overflow_warnings (void)
997 {
998   fold_undefer_overflow_warnings (false, NULL_TREE, 0);
999 }
1000
1001 /* Whether we are deferring overflow warnings.  */
1002
1003 bool
1004 fold_deferring_overflow_warnings_p (void)
1005 {
1006   return fold_deferring_overflow_warnings > 0;
1007 }
1008
1009 /* This is called when we fold something based on the fact that signed
1010    overflow is undefined.  */
1011
1012 static void
1013 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1014 {
1015   gcc_assert (!flag_wrapv && !flag_trapv);
1016   if (fold_deferring_overflow_warnings > 0)
1017     {
1018       if (fold_deferred_overflow_warning == NULL
1019           || wc < fold_deferred_overflow_code)
1020         {
1021           fold_deferred_overflow_warning = gmsgid;
1022           fold_deferred_overflow_code = wc;
1023         }
1024     }
1025   else if (issue_strict_overflow_warning (wc))
1026     warning (OPT_Wstrict_overflow, gmsgid);
1027 }
1028 \f
1029 /* Return true if the built-in mathematical function specified by CODE
1030    is odd, i.e. -f(x) == f(-x).  */
1031
1032 static bool
1033 negate_mathfn_p (enum built_in_function code)
1034 {
1035   switch (code)
1036     {
1037     CASE_FLT_FN (BUILT_IN_ASIN):
1038     CASE_FLT_FN (BUILT_IN_ASINH):
1039     CASE_FLT_FN (BUILT_IN_ATAN):
1040     CASE_FLT_FN (BUILT_IN_ATANH):
1041     CASE_FLT_FN (BUILT_IN_CASIN):
1042     CASE_FLT_FN (BUILT_IN_CASINH):
1043     CASE_FLT_FN (BUILT_IN_CATAN):
1044     CASE_FLT_FN (BUILT_IN_CATANH):
1045     CASE_FLT_FN (BUILT_IN_CBRT):
1046     CASE_FLT_FN (BUILT_IN_CPROJ):
1047     CASE_FLT_FN (BUILT_IN_CSIN):
1048     CASE_FLT_FN (BUILT_IN_CSINH):
1049     CASE_FLT_FN (BUILT_IN_CTAN):
1050     CASE_FLT_FN (BUILT_IN_CTANH):
1051     CASE_FLT_FN (BUILT_IN_ERF):
1052     CASE_FLT_FN (BUILT_IN_LLROUND):
1053     CASE_FLT_FN (BUILT_IN_LROUND):
1054     CASE_FLT_FN (BUILT_IN_ROUND):
1055     CASE_FLT_FN (BUILT_IN_SIN):
1056     CASE_FLT_FN (BUILT_IN_SINH):
1057     CASE_FLT_FN (BUILT_IN_TAN):
1058     CASE_FLT_FN (BUILT_IN_TANH):
1059     CASE_FLT_FN (BUILT_IN_TRUNC):
1060       return true;
1061
1062     CASE_FLT_FN (BUILT_IN_LLRINT):
1063     CASE_FLT_FN (BUILT_IN_LRINT):
1064     CASE_FLT_FN (BUILT_IN_NEARBYINT):
1065     CASE_FLT_FN (BUILT_IN_RINT):
1066       return !flag_rounding_math;
1067     
1068     default:
1069       break;
1070     }
1071   return false;
1072 }
1073
1074 /* Check whether we may negate an integer constant T without causing
1075    overflow.  */
1076
1077 bool
1078 may_negate_without_overflow_p (const_tree t)
1079 {
1080   unsigned HOST_WIDE_INT val;
1081   unsigned int prec;
1082   tree type;
1083
1084   gcc_assert (TREE_CODE (t) == INTEGER_CST);
1085
1086   type = TREE_TYPE (t);
1087   if (TYPE_UNSIGNED (type))
1088     return false;
1089
1090   prec = TYPE_PRECISION (type);
1091   if (prec > HOST_BITS_PER_WIDE_INT)
1092     {
1093       if (TREE_INT_CST_LOW (t) != 0)
1094         return true;
1095       prec -= HOST_BITS_PER_WIDE_INT;
1096       val = TREE_INT_CST_HIGH (t);
1097     }
1098   else
1099     val = TREE_INT_CST_LOW (t);
1100   if (prec < HOST_BITS_PER_WIDE_INT)
1101     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1102   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1103 }
1104
1105 /* Determine whether an expression T can be cheaply negated using
1106    the function negate_expr without introducing undefined overflow.  */
1107
1108 static bool
1109 negate_expr_p (tree t)
1110 {
1111   tree type;
1112
1113   if (t == 0)
1114     return false;
1115
1116   type = TREE_TYPE (t);
1117
1118   STRIP_SIGN_NOPS (t);
1119   switch (TREE_CODE (t))
1120     {
1121     case INTEGER_CST:
1122       if (TYPE_OVERFLOW_WRAPS (type))
1123         return true;
1124
1125       /* Check that -CST will not overflow type.  */
1126       return may_negate_without_overflow_p (t);
1127     case BIT_NOT_EXPR:
1128       return (INTEGRAL_TYPE_P (type)
1129               && TYPE_OVERFLOW_WRAPS (type));
1130
1131     case FIXED_CST:
1132     case REAL_CST:
1133     case NEGATE_EXPR:
1134       return true;
1135
1136     case COMPLEX_CST:
1137       return negate_expr_p (TREE_REALPART (t))
1138              && negate_expr_p (TREE_IMAGPART (t));
1139
1140     case COMPLEX_EXPR:
1141       return negate_expr_p (TREE_OPERAND (t, 0))
1142              && negate_expr_p (TREE_OPERAND (t, 1));
1143
1144     case CONJ_EXPR:
1145       return negate_expr_p (TREE_OPERAND (t, 0));
1146
1147     case PLUS_EXPR:
1148       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1149           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1150         return false;
1151       /* -(A + B) -> (-B) - A.  */
1152       if (negate_expr_p (TREE_OPERAND (t, 1))
1153           && reorder_operands_p (TREE_OPERAND (t, 0),
1154                                  TREE_OPERAND (t, 1)))
1155         return true;
1156       /* -(A + B) -> (-A) - B.  */
1157       return negate_expr_p (TREE_OPERAND (t, 0));
1158
1159     case MINUS_EXPR:
1160       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
1161       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1162              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1163              && reorder_operands_p (TREE_OPERAND (t, 0),
1164                                     TREE_OPERAND (t, 1));
1165
1166     case MULT_EXPR:
1167       if (TYPE_UNSIGNED (TREE_TYPE (t)))
1168         break;
1169
1170       /* Fall through.  */
1171
1172     case RDIV_EXPR:
1173       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1174         return negate_expr_p (TREE_OPERAND (t, 1))
1175                || negate_expr_p (TREE_OPERAND (t, 0));
1176       break;
1177
1178     case TRUNC_DIV_EXPR:
1179     case ROUND_DIV_EXPR:
1180     case FLOOR_DIV_EXPR:
1181     case CEIL_DIV_EXPR:
1182     case EXACT_DIV_EXPR:
1183       /* In general we can't negate A / B, because if A is INT_MIN and
1184          B is 1, we may turn this into INT_MIN / -1 which is undefined
1185          and actually traps on some architectures.  But if overflow is
1186          undefined, we can negate, because - (INT_MIN / 1) is an
1187          overflow.  */
1188       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1189           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1190         break;
1191       return negate_expr_p (TREE_OPERAND (t, 1))
1192              || negate_expr_p (TREE_OPERAND (t, 0));
1193
1194     case NOP_EXPR:
1195       /* Negate -((double)float) as (double)(-float).  */
1196       if (TREE_CODE (type) == REAL_TYPE)
1197         {
1198           tree tem = strip_float_extensions (t);
1199           if (tem != t)
1200             return negate_expr_p (tem);
1201         }
1202       break;
1203
1204     case CALL_EXPR:
1205       /* Negate -f(x) as f(-x).  */
1206       if (negate_mathfn_p (builtin_mathfn_code (t)))
1207         return negate_expr_p (CALL_EXPR_ARG (t, 0));
1208       break;
1209
1210     case RSHIFT_EXPR:
1211       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1212       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1213         {
1214           tree op1 = TREE_OPERAND (t, 1);
1215           if (TREE_INT_CST_HIGH (op1) == 0
1216               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1217                  == TREE_INT_CST_LOW (op1))
1218             return true;
1219         }
1220       break;
1221
1222     default:
1223       break;
1224     }
1225   return false;
1226 }
1227
1228 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1229    simplification is possible.
1230    If negate_expr_p would return true for T, NULL_TREE will never be
1231    returned.  */
1232
1233 static tree
1234 fold_negate_expr (tree t)
1235 {
1236   tree type = TREE_TYPE (t);
1237   tree tem;
1238
1239   switch (TREE_CODE (t))
1240     {
1241     /* Convert - (~A) to A + 1.  */
1242     case BIT_NOT_EXPR:
1243       if (INTEGRAL_TYPE_P (type))
1244         return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
1245                             build_int_cst (type, 1));
1246       break;
1247       
1248     case INTEGER_CST:
1249       tem = fold_negate_const (t, type);
1250       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1251           || !TYPE_OVERFLOW_TRAPS (type))
1252         return tem;
1253       break;
1254
1255     case REAL_CST:
1256       tem = fold_negate_const (t, type);
1257       /* Two's complement FP formats, such as c4x, may overflow.  */
1258       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1259         return tem;
1260       break;
1261
1262     case FIXED_CST:
1263       tem = fold_negate_const (t, type);
1264       return tem;
1265
1266     case COMPLEX_CST:
1267       {
1268         tree rpart = negate_expr (TREE_REALPART (t));
1269         tree ipart = negate_expr (TREE_IMAGPART (t));
1270
1271         if ((TREE_CODE (rpart) == REAL_CST
1272              && TREE_CODE (ipart) == REAL_CST)
1273             || (TREE_CODE (rpart) == INTEGER_CST
1274                 && TREE_CODE (ipart) == INTEGER_CST))
1275           return build_complex (type, rpart, ipart);
1276       }
1277       break;
1278
1279     case COMPLEX_EXPR:
1280       if (negate_expr_p (t))
1281         return fold_build2 (COMPLEX_EXPR, type,
1282                             fold_negate_expr (TREE_OPERAND (t, 0)),
1283                             fold_negate_expr (TREE_OPERAND (t, 1)));
1284       break;
1285       
1286     case CONJ_EXPR:
1287       if (negate_expr_p (t))
1288         return fold_build1 (CONJ_EXPR, type,
1289                             fold_negate_expr (TREE_OPERAND (t, 0)));
1290       break;
1291
1292     case NEGATE_EXPR:
1293       return TREE_OPERAND (t, 0);
1294
1295     case PLUS_EXPR:
1296       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1297           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1298         {
1299           /* -(A + B) -> (-B) - A.  */
1300           if (negate_expr_p (TREE_OPERAND (t, 1))
1301               && reorder_operands_p (TREE_OPERAND (t, 0),
1302                                      TREE_OPERAND (t, 1)))
1303             {
1304               tem = negate_expr (TREE_OPERAND (t, 1));
1305               return fold_build2 (MINUS_EXPR, type,
1306                                   tem, TREE_OPERAND (t, 0));
1307             }
1308
1309           /* -(A + B) -> (-A) - B.  */
1310           if (negate_expr_p (TREE_OPERAND (t, 0)))
1311             {
1312               tem = negate_expr (TREE_OPERAND (t, 0));
1313               return fold_build2 (MINUS_EXPR, type,
1314                                   tem, TREE_OPERAND (t, 1));
1315             }
1316         }
1317       break;
1318
1319     case MINUS_EXPR:
1320       /* - (A - B) -> B - A  */
1321       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1322           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1323           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1324         return fold_build2 (MINUS_EXPR, type,
1325                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1326       break;
1327
1328     case MULT_EXPR:
1329       if (TYPE_UNSIGNED (type))
1330         break;
1331
1332       /* Fall through.  */
1333
1334     case RDIV_EXPR:
1335       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1336         {
1337           tem = TREE_OPERAND (t, 1);
1338           if (negate_expr_p (tem))
1339             return fold_build2 (TREE_CODE (t), type,
1340                                 TREE_OPERAND (t, 0), negate_expr (tem));
1341           tem = TREE_OPERAND (t, 0);
1342           if (negate_expr_p (tem))
1343             return fold_build2 (TREE_CODE (t), type,
1344                                 negate_expr (tem), TREE_OPERAND (t, 1));
1345         }
1346       break;
1347
1348     case TRUNC_DIV_EXPR:
1349     case ROUND_DIV_EXPR:
1350     case FLOOR_DIV_EXPR:
1351     case CEIL_DIV_EXPR:
1352     case EXACT_DIV_EXPR:
1353       /* In general we can't negate A / B, because if A is INT_MIN and
1354          B is 1, we may turn this into INT_MIN / -1 which is undefined
1355          and actually traps on some architectures.  But if overflow is
1356          undefined, we can negate, because - (INT_MIN / 1) is an
1357          overflow.  */
1358       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1359         {
1360           const char * const warnmsg = G_("assuming signed overflow does not "
1361                                           "occur when negating a division");
1362           tem = TREE_OPERAND (t, 1);
1363           if (negate_expr_p (tem))
1364             {
1365               if (INTEGRAL_TYPE_P (type)
1366                   && (TREE_CODE (tem) != INTEGER_CST
1367                       || integer_onep (tem)))
1368                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1369               return fold_build2 (TREE_CODE (t), type,
1370                                   TREE_OPERAND (t, 0), negate_expr (tem));
1371             }
1372           tem = TREE_OPERAND (t, 0);
1373           if (negate_expr_p (tem))
1374             {
1375               if (INTEGRAL_TYPE_P (type)
1376                   && (TREE_CODE (tem) != INTEGER_CST
1377                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1378                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1379               return fold_build2 (TREE_CODE (t), type,
1380                                   negate_expr (tem), TREE_OPERAND (t, 1));
1381             }
1382         }
1383       break;
1384
1385     case NOP_EXPR:
1386       /* Convert -((double)float) into (double)(-float).  */
1387       if (TREE_CODE (type) == REAL_TYPE)
1388         {
1389           tem = strip_float_extensions (t);
1390           if (tem != t && negate_expr_p (tem))
1391             return fold_convert (type, negate_expr (tem));
1392         }
1393       break;
1394
1395     case CALL_EXPR:
1396       /* Negate -f(x) as f(-x).  */
1397       if (negate_mathfn_p (builtin_mathfn_code (t))
1398           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1399         {
1400           tree fndecl, arg;
1401
1402           fndecl = get_callee_fndecl (t);
1403           arg = negate_expr (CALL_EXPR_ARG (t, 0));
1404           return build_call_expr (fndecl, 1, arg);
1405         }
1406       break;
1407
1408     case RSHIFT_EXPR:
1409       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1410       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1411         {
1412           tree op1 = TREE_OPERAND (t, 1);
1413           if (TREE_INT_CST_HIGH (op1) == 0
1414               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1415                  == TREE_INT_CST_LOW (op1))
1416             {
1417               tree ntype = TYPE_UNSIGNED (type)
1418                            ? signed_type_for (type)
1419                            : unsigned_type_for (type);
1420               tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1421               temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1422               return fold_convert (type, temp);
1423             }
1424         }
1425       break;
1426
1427     default:
1428       break;
1429     }
1430
1431   return NULL_TREE;
1432 }
1433
1434 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1435    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
1436    return NULL_TREE. */
1437
1438 static tree
1439 negate_expr (tree t)
1440 {
1441   tree type, tem;
1442
1443   if (t == NULL_TREE)
1444     return NULL_TREE;
1445
1446   type = TREE_TYPE (t);
1447   STRIP_SIGN_NOPS (t);
1448
1449   tem = fold_negate_expr (t);
1450   if (!tem)
1451     tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1452   return fold_convert (type, tem);
1453 }
1454 \f
1455 /* Split a tree IN into a constant, literal and variable parts that could be
1456    combined with CODE to make IN.  "constant" means an expression with
1457    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1458    commutative arithmetic operation.  Store the constant part into *CONP,
1459    the literal in *LITP and return the variable part.  If a part isn't
1460    present, set it to null.  If the tree does not decompose in this way,
1461    return the entire tree as the variable part and the other parts as null.
1462
1463    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1464    case, we negate an operand that was subtracted.  Except if it is a
1465    literal for which we use *MINUS_LITP instead.
1466
1467    If NEGATE_P is true, we are negating all of IN, again except a literal
1468    for which we use *MINUS_LITP instead.
1469
1470    If IN is itself a literal or constant, return it as appropriate.
1471
1472    Note that we do not guarantee that any of the three values will be the
1473    same type as IN, but they will have the same signedness and mode.  */
1474
1475 static tree
1476 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1477             tree *minus_litp, int negate_p)
1478 {
1479   tree var = 0;
1480
1481   *conp = 0;
1482   *litp = 0;
1483   *minus_litp = 0;
1484
1485   /* Strip any conversions that don't change the machine mode or signedness.  */
1486   STRIP_SIGN_NOPS (in);
1487
1488   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1489       || TREE_CODE (in) == FIXED_CST)
1490     *litp = in;
1491   else if (TREE_CODE (in) == code
1492            || (! FLOAT_TYPE_P (TREE_TYPE (in))
1493                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1494                /* We can associate addition and subtraction together (even
1495                   though the C standard doesn't say so) for integers because
1496                   the value is not affected.  For reals, the value might be
1497                   affected, so we can't.  */
1498                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1499                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1500     {
1501       tree op0 = TREE_OPERAND (in, 0);
1502       tree op1 = TREE_OPERAND (in, 1);
1503       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1504       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1505
1506       /* First see if either of the operands is a literal, then a constant.  */
1507       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1508           || TREE_CODE (op0) == FIXED_CST)
1509         *litp = op0, op0 = 0;
1510       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1511                || TREE_CODE (op1) == FIXED_CST)
1512         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1513
1514       if (op0 != 0 && TREE_CONSTANT (op0))
1515         *conp = op0, op0 = 0;
1516       else if (op1 != 0 && TREE_CONSTANT (op1))
1517         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1518
1519       /* If we haven't dealt with either operand, this is not a case we can
1520          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1521       if (op0 != 0 && op1 != 0)
1522         var = in;
1523       else if (op0 != 0)
1524         var = op0;
1525       else
1526         var = op1, neg_var_p = neg1_p;
1527
1528       /* Now do any needed negations.  */
1529       if (neg_litp_p)
1530         *minus_litp = *litp, *litp = 0;
1531       if (neg_conp_p)
1532         *conp = negate_expr (*conp);
1533       if (neg_var_p)
1534         var = negate_expr (var);
1535     }
1536   else if (TREE_CONSTANT (in))
1537     *conp = in;
1538   else
1539     var = in;
1540
1541   if (negate_p)
1542     {
1543       if (*litp)
1544         *minus_litp = *litp, *litp = 0;
1545       else if (*minus_litp)
1546         *litp = *minus_litp, *minus_litp = 0;
1547       *conp = negate_expr (*conp);
1548       var = negate_expr (var);
1549     }
1550
1551   return var;
1552 }
1553
1554 /* Re-associate trees split by the above function.  T1 and T2 are either
1555    expressions to associate or null.  Return the new expression, if any.  If
1556    we build an operation, do it in TYPE and with CODE.  */
1557
1558 static tree
1559 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1560 {
1561   if (t1 == 0)
1562     return t2;
1563   else if (t2 == 0)
1564     return t1;
1565
1566   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1567      try to fold this since we will have infinite recursion.  But do
1568      deal with any NEGATE_EXPRs.  */
1569   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1570       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1571     {
1572       if (code == PLUS_EXPR)
1573         {
1574           if (TREE_CODE (t1) == NEGATE_EXPR)
1575             return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1576                            fold_convert (type, TREE_OPERAND (t1, 0)));
1577           else if (TREE_CODE (t2) == NEGATE_EXPR)
1578             return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1579                            fold_convert (type, TREE_OPERAND (t2, 0)));
1580           else if (integer_zerop (t2))
1581             return fold_convert (type, t1);
1582         }
1583       else if (code == MINUS_EXPR)
1584         {
1585           if (integer_zerop (t2))
1586             return fold_convert (type, t1);
1587         }
1588
1589       return build2 (code, type, fold_convert (type, t1),
1590                      fold_convert (type, t2));
1591     }
1592
1593   return fold_build2 (code, type, fold_convert (type, t1),
1594                       fold_convert (type, t2));
1595 }
1596 \f
1597 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1598    for use in int_const_binop, size_binop and size_diffop.  */
1599
1600 static bool
1601 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1602 {
1603   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1604     return false;
1605   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1606     return false;
1607
1608   switch (code)
1609     {
1610     case LSHIFT_EXPR:
1611     case RSHIFT_EXPR:
1612     case LROTATE_EXPR:
1613     case RROTATE_EXPR:
1614       return true;
1615
1616     default:
1617       break;
1618     }
1619
1620   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1621          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1622          && TYPE_MODE (type1) == TYPE_MODE (type2);
1623 }
1624
1625
1626 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1627    to produce a new constant.  Return NULL_TREE if we don't know how
1628    to evaluate CODE at compile-time.
1629
1630    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1631
1632 tree
1633 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1634 {
1635   unsigned HOST_WIDE_INT int1l, int2l;
1636   HOST_WIDE_INT int1h, int2h;
1637   unsigned HOST_WIDE_INT low;
1638   HOST_WIDE_INT hi;
1639   unsigned HOST_WIDE_INT garbagel;
1640   HOST_WIDE_INT garbageh;
1641   tree t;
1642   tree type = TREE_TYPE (arg1);
1643   int uns = TYPE_UNSIGNED (type);
1644   int is_sizetype
1645     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1646   int overflow = 0;
1647
1648   int1l = TREE_INT_CST_LOW (arg1);
1649   int1h = TREE_INT_CST_HIGH (arg1);
1650   int2l = TREE_INT_CST_LOW (arg2);
1651   int2h = TREE_INT_CST_HIGH (arg2);
1652
1653   switch (code)
1654     {
1655     case BIT_IOR_EXPR:
1656       low = int1l | int2l, hi = int1h | int2h;
1657       break;
1658
1659     case BIT_XOR_EXPR:
1660       low = int1l ^ int2l, hi = int1h ^ int2h;
1661       break;
1662
1663     case BIT_AND_EXPR:
1664       low = int1l & int2l, hi = int1h & int2h;
1665       break;
1666
1667     case RSHIFT_EXPR:
1668       int2l = -int2l;
1669     case LSHIFT_EXPR:
1670       /* It's unclear from the C standard whether shifts can overflow.
1671          The following code ignores overflow; perhaps a C standard
1672          interpretation ruling is needed.  */
1673       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1674                      &low, &hi, !uns);
1675       break;
1676
1677     case RROTATE_EXPR:
1678       int2l = - int2l;
1679     case LROTATE_EXPR:
1680       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1681                       &low, &hi);
1682       break;
1683
1684     case PLUS_EXPR:
1685       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1686       break;
1687
1688     case MINUS_EXPR:
1689       neg_double (int2l, int2h, &low, &hi);
1690       add_double (int1l, int1h, low, hi, &low, &hi);
1691       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1692       break;
1693
1694     case MULT_EXPR:
1695       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1696       break;
1697
1698     case TRUNC_DIV_EXPR:
1699     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1700     case EXACT_DIV_EXPR:
1701       /* This is a shortcut for a common special case.  */
1702       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1703           && !TREE_OVERFLOW (arg1)
1704           && !TREE_OVERFLOW (arg2)
1705           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1706         {
1707           if (code == CEIL_DIV_EXPR)
1708             int1l += int2l - 1;
1709
1710           low = int1l / int2l, hi = 0;
1711           break;
1712         }
1713
1714       /* ... fall through ...  */
1715
1716     case ROUND_DIV_EXPR:
1717       if (int2h == 0 && int2l == 0)
1718         return NULL_TREE;
1719       if (int2h == 0 && int2l == 1)
1720         {
1721           low = int1l, hi = int1h;
1722           break;
1723         }
1724       if (int1l == int2l && int1h == int2h
1725           && ! (int1l == 0 && int1h == 0))
1726         {
1727           low = 1, hi = 0;
1728           break;
1729         }
1730       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1731                                        &low, &hi, &garbagel, &garbageh);
1732       break;
1733
1734     case TRUNC_MOD_EXPR:
1735     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1736       /* This is a shortcut for a common special case.  */
1737       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1738           && !TREE_OVERFLOW (arg1)
1739           && !TREE_OVERFLOW (arg2)
1740           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1741         {
1742           if (code == CEIL_MOD_EXPR)
1743             int1l += int2l - 1;
1744           low = int1l % int2l, hi = 0;
1745           break;
1746         }
1747
1748       /* ... fall through ...  */
1749
1750     case ROUND_MOD_EXPR:
1751       if (int2h == 0 && int2l == 0)
1752         return NULL_TREE;
1753       overflow = div_and_round_double (code, uns,
1754                                        int1l, int1h, int2l, int2h,
1755                                        &garbagel, &garbageh, &low, &hi);
1756       break;
1757
1758     case MIN_EXPR:
1759     case MAX_EXPR:
1760       if (uns)
1761         low = (((unsigned HOST_WIDE_INT) int1h
1762                 < (unsigned HOST_WIDE_INT) int2h)
1763                || (((unsigned HOST_WIDE_INT) int1h
1764                     == (unsigned HOST_WIDE_INT) int2h)
1765                    && int1l < int2l));
1766       else
1767         low = (int1h < int2h
1768                || (int1h == int2h && int1l < int2l));
1769
1770       if (low == (code == MIN_EXPR))
1771         low = int1l, hi = int1h;
1772       else
1773         low = int2l, hi = int2h;
1774       break;
1775
1776     default:
1777       return NULL_TREE;
1778     }
1779
1780   if (notrunc)
1781     {
1782       t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1783
1784       /* Propagate overflow flags ourselves.  */
1785       if (((!uns || is_sizetype) && overflow)
1786           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1787         {
1788           t = copy_node (t);
1789           TREE_OVERFLOW (t) = 1;
1790         }
1791     }
1792   else
1793     t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1794                                ((!uns || is_sizetype) && overflow)
1795                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1796
1797   return t;
1798 }
1799
1800 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1801    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1802    are the same kind of constant and the same machine mode.  Return zero if
1803    combining the constants is not allowed in the current operating mode.
1804
1805    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1806
1807 static tree
1808 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1809 {
1810   /* Sanity check for the recursive cases.  */
1811   if (!arg1 || !arg2)
1812     return NULL_TREE;
1813
1814   STRIP_NOPS (arg1);
1815   STRIP_NOPS (arg2);
1816
1817   if (TREE_CODE (arg1) == INTEGER_CST)
1818     return int_const_binop (code, arg1, arg2, notrunc);
1819
1820   if (TREE_CODE (arg1) == REAL_CST)
1821     {
1822       enum machine_mode mode;
1823       REAL_VALUE_TYPE d1;
1824       REAL_VALUE_TYPE d2;
1825       REAL_VALUE_TYPE value;
1826       REAL_VALUE_TYPE result;
1827       bool inexact;
1828       tree t, type;
1829
1830       /* The following codes are handled by real_arithmetic.  */
1831       switch (code)
1832         {
1833         case PLUS_EXPR:
1834         case MINUS_EXPR:
1835         case MULT_EXPR:
1836         case RDIV_EXPR:
1837         case MIN_EXPR:
1838         case MAX_EXPR:
1839           break;
1840
1841         default:
1842           return NULL_TREE;
1843         }
1844
1845       d1 = TREE_REAL_CST (arg1);
1846       d2 = TREE_REAL_CST (arg2);
1847
1848       type = TREE_TYPE (arg1);
1849       mode = TYPE_MODE (type);
1850
1851       /* Don't perform operation if we honor signaling NaNs and
1852          either operand is a NaN.  */
1853       if (HONOR_SNANS (mode)
1854           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1855         return NULL_TREE;
1856
1857       /* Don't perform operation if it would raise a division
1858          by zero exception.  */
1859       if (code == RDIV_EXPR
1860           && REAL_VALUES_EQUAL (d2, dconst0)
1861           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1862         return NULL_TREE;
1863
1864       /* If either operand is a NaN, just return it.  Otherwise, set up
1865          for floating-point trap; we return an overflow.  */
1866       if (REAL_VALUE_ISNAN (d1))
1867         return arg1;
1868       else if (REAL_VALUE_ISNAN (d2))
1869         return arg2;
1870
1871       inexact = real_arithmetic (&value, code, &d1, &d2);
1872       real_convert (&result, mode, &value);
1873
1874       /* Don't constant fold this floating point operation if
1875          the result has overflowed and flag_trapping_math.  */
1876       if (flag_trapping_math
1877           && MODE_HAS_INFINITIES (mode)
1878           && REAL_VALUE_ISINF (result)
1879           && !REAL_VALUE_ISINF (d1)
1880           && !REAL_VALUE_ISINF (d2))
1881         return NULL_TREE;
1882
1883       /* Don't constant fold this floating point operation if the
1884          result may dependent upon the run-time rounding mode and
1885          flag_rounding_math is set, or if GCC's software emulation
1886          is unable to accurately represent the result.  */
1887       if ((flag_rounding_math
1888            || (REAL_MODE_FORMAT_COMPOSITE_P (mode)
1889                && !flag_unsafe_math_optimizations))
1890           && (inexact || !real_identical (&result, &value)))
1891         return NULL_TREE;
1892
1893       t = build_real (type, result);
1894
1895       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1896       return t;
1897     }
1898
1899   if (TREE_CODE (arg1) == FIXED_CST)
1900     {
1901       FIXED_VALUE_TYPE f1;
1902       FIXED_VALUE_TYPE f2;
1903       FIXED_VALUE_TYPE result;
1904       tree t, type;
1905       int sat_p;
1906       bool overflow_p;
1907
1908       /* The following codes are handled by fixed_arithmetic.  */
1909       switch (code)
1910         {
1911         case PLUS_EXPR:
1912         case MINUS_EXPR:
1913         case MULT_EXPR:
1914         case TRUNC_DIV_EXPR:
1915           f2 = TREE_FIXED_CST (arg2);
1916           break;
1917
1918         case LSHIFT_EXPR:
1919         case RSHIFT_EXPR:
1920           f2.data.high = TREE_INT_CST_HIGH (arg2);
1921           f2.data.low = TREE_INT_CST_LOW (arg2);
1922           f2.mode = SImode;
1923           break;
1924
1925         default:
1926           return NULL_TREE;
1927         }
1928
1929       f1 = TREE_FIXED_CST (arg1);
1930       type = TREE_TYPE (arg1);
1931       sat_p = TYPE_SATURATING (type);
1932       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1933       t = build_fixed (type, result);
1934       /* Propagate overflow flags.  */
1935       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1936         {
1937           TREE_OVERFLOW (t) = 1;
1938           TREE_CONSTANT_OVERFLOW (t) = 1;
1939         }
1940       else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1941         TREE_CONSTANT_OVERFLOW (t) = 1;
1942       return t;
1943     }
1944
1945   if (TREE_CODE (arg1) == COMPLEX_CST)
1946     {
1947       tree type = TREE_TYPE (arg1);
1948       tree r1 = TREE_REALPART (arg1);
1949       tree i1 = TREE_IMAGPART (arg1);
1950       tree r2 = TREE_REALPART (arg2);
1951       tree i2 = TREE_IMAGPART (arg2);
1952       tree real, imag;
1953
1954       switch (code)
1955         {
1956         case PLUS_EXPR:
1957         case MINUS_EXPR:
1958           real = const_binop (code, r1, r2, notrunc);
1959           imag = const_binop (code, i1, i2, notrunc);
1960           break;
1961
1962         case MULT_EXPR:
1963           real = const_binop (MINUS_EXPR,
1964                               const_binop (MULT_EXPR, r1, r2, notrunc),
1965                               const_binop (MULT_EXPR, i1, i2, notrunc),
1966                               notrunc);
1967           imag = const_binop (PLUS_EXPR,
1968                               const_binop (MULT_EXPR, r1, i2, notrunc),
1969                               const_binop (MULT_EXPR, i1, r2, notrunc),
1970                               notrunc);
1971           break;
1972
1973         case RDIV_EXPR:
1974           {
1975             tree magsquared
1976               = const_binop (PLUS_EXPR,
1977                              const_binop (MULT_EXPR, r2, r2, notrunc),
1978                              const_binop (MULT_EXPR, i2, i2, notrunc),
1979                              notrunc);
1980             tree t1
1981               = const_binop (PLUS_EXPR,
1982                              const_binop (MULT_EXPR, r1, r2, notrunc),
1983                              const_binop (MULT_EXPR, i1, i2, notrunc),
1984                              notrunc);
1985             tree t2
1986               = const_binop (MINUS_EXPR,
1987                              const_binop (MULT_EXPR, i1, r2, notrunc),
1988                              const_binop (MULT_EXPR, r1, i2, notrunc),
1989                              notrunc);
1990
1991             if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1992               code = TRUNC_DIV_EXPR;
1993
1994             real = const_binop (code, t1, magsquared, notrunc);
1995             imag = const_binop (code, t2, magsquared, notrunc);
1996           }
1997           break;
1998
1999         default:
2000           return NULL_TREE;
2001         }
2002
2003       if (real && imag)
2004         return build_complex (type, real, imag);
2005     }
2006
2007   return NULL_TREE;
2008 }
2009
2010 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
2011    indicates which particular sizetype to create.  */
2012
2013 tree
2014 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2015 {
2016   return build_int_cst (sizetype_tab[(int) kind], number);
2017 }
2018 \f
2019 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
2020    is a tree code.  The type of the result is taken from the operands.
2021    Both must be equivalent integer types, ala int_binop_types_match_p.
2022    If the operands are constant, so is the result.  */
2023
2024 tree
2025 size_binop (enum tree_code code, tree arg0, tree arg1)
2026 {
2027   tree type = TREE_TYPE (arg0);
2028
2029   if (arg0 == error_mark_node || arg1 == error_mark_node)
2030     return error_mark_node;
2031
2032   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2033                                        TREE_TYPE (arg1)));
2034
2035   /* Handle the special case of two integer constants faster.  */
2036   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2037     {
2038       /* And some specific cases even faster than that.  */
2039       if (code == PLUS_EXPR)
2040         {
2041           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2042             return arg1;
2043           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2044             return arg0;
2045         }
2046       else if (code == MINUS_EXPR)
2047         {
2048           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2049             return arg0;
2050         }
2051       else if (code == MULT_EXPR)
2052         {
2053           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2054             return arg1;
2055         }
2056
2057       /* Handle general case of two integer constants.  */
2058       return int_const_binop (code, arg0, arg1, 0);
2059     }
2060
2061   return fold_build2 (code, type, arg0, arg1);
2062 }
2063
2064 /* Given two values, either both of sizetype or both of bitsizetype,
2065    compute the difference between the two values.  Return the value
2066    in signed type corresponding to the type of the operands.  */
2067
2068 tree
2069 size_diffop (tree arg0, tree arg1)
2070 {
2071   tree type = TREE_TYPE (arg0);
2072   tree ctype;
2073
2074   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2075                                        TREE_TYPE (arg1)));
2076
2077   /* If the type is already signed, just do the simple thing.  */
2078   if (!TYPE_UNSIGNED (type))
2079     return size_binop (MINUS_EXPR, arg0, arg1);
2080
2081   if (type == sizetype)
2082     ctype = ssizetype;
2083   else if (type == bitsizetype)
2084     ctype = sbitsizetype;
2085   else
2086     ctype = signed_type_for (type);
2087
2088   /* If either operand is not a constant, do the conversions to the signed
2089      type and subtract.  The hardware will do the right thing with any
2090      overflow in the subtraction.  */
2091   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2092     return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2093                        fold_convert (ctype, arg1));
2094
2095   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2096      Otherwise, subtract the other way, convert to CTYPE (we know that can't
2097      overflow) and negate (which can't either).  Special-case a result
2098      of zero while we're here.  */
2099   if (tree_int_cst_equal (arg0, arg1))
2100     return build_int_cst (ctype, 0);
2101   else if (tree_int_cst_lt (arg1, arg0))
2102     return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2103   else
2104     return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2105                        fold_convert (ctype, size_binop (MINUS_EXPR,
2106                                                         arg1, arg0)));
2107 }
2108 \f
2109 /* A subroutine of fold_convert_const handling conversions of an
2110    INTEGER_CST to another integer type.  */
2111
2112 static tree
2113 fold_convert_const_int_from_int (tree type, const_tree arg1)
2114 {
2115   tree t;
2116
2117   /* Given an integer constant, make new constant with new type,
2118      appropriately sign-extended or truncated.  */
2119   t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2120                              TREE_INT_CST_HIGH (arg1),
2121                              /* Don't set the overflow when
2122                                 converting a pointer  */
2123                              !POINTER_TYPE_P (TREE_TYPE (arg1)),
2124                              (TREE_INT_CST_HIGH (arg1) < 0
2125                               && (TYPE_UNSIGNED (type)
2126                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2127                              | TREE_OVERFLOW (arg1));
2128
2129   return t;
2130 }
2131
2132 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2133    to an integer type.  */
2134
2135 static tree
2136 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2137 {
2138   int overflow = 0;
2139   tree t;
2140
2141   /* The following code implements the floating point to integer
2142      conversion rules required by the Java Language Specification,
2143      that IEEE NaNs are mapped to zero and values that overflow
2144      the target precision saturate, i.e. values greater than
2145      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2146      are mapped to INT_MIN.  These semantics are allowed by the
2147      C and C++ standards that simply state that the behavior of
2148      FP-to-integer conversion is unspecified upon overflow.  */
2149
2150   HOST_WIDE_INT high, low;
2151   REAL_VALUE_TYPE r;
2152   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2153
2154   switch (code)
2155     {
2156     case FIX_TRUNC_EXPR:
2157       real_trunc (&r, VOIDmode, &x);
2158       break;
2159
2160     default:
2161       gcc_unreachable ();
2162     }
2163
2164   /* If R is NaN, return zero and show we have an overflow.  */
2165   if (REAL_VALUE_ISNAN (r))
2166     {
2167       overflow = 1;
2168       high = 0;
2169       low = 0;
2170     }
2171
2172   /* See if R is less than the lower bound or greater than the
2173      upper bound.  */
2174
2175   if (! overflow)
2176     {
2177       tree lt = TYPE_MIN_VALUE (type);
2178       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2179       if (REAL_VALUES_LESS (r, l))
2180         {
2181           overflow = 1;
2182           high = TREE_INT_CST_HIGH (lt);
2183           low = TREE_INT_CST_LOW (lt);
2184         }
2185     }
2186
2187   if (! overflow)
2188     {
2189       tree ut = TYPE_MAX_VALUE (type);
2190       if (ut)
2191         {
2192           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2193           if (REAL_VALUES_LESS (u, r))
2194             {
2195               overflow = 1;
2196               high = TREE_INT_CST_HIGH (ut);
2197               low = TREE_INT_CST_LOW (ut);
2198             }
2199         }
2200     }
2201
2202   if (! overflow)
2203     REAL_VALUE_TO_INT (&low, &high, r);
2204
2205   t = force_fit_type_double (type, low, high, -1,
2206                              overflow | TREE_OVERFLOW (arg1));
2207   return t;
2208 }
2209
2210 /* A subroutine of fold_convert_const handling conversions of a
2211    FIXED_CST to an integer type.  */
2212
2213 static tree
2214 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2215 {
2216   tree t;
2217   double_int temp, temp_trunc;
2218   unsigned int mode;
2219
2220   /* Right shift FIXED_CST to temp by fbit.  */
2221   temp = TREE_FIXED_CST (arg1).data;
2222   mode = TREE_FIXED_CST (arg1).mode;
2223   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2224     {
2225       lshift_double (temp.low, temp.high,
2226                      - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2227                      &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2228
2229       /* Left shift temp to temp_trunc by fbit.  */
2230       lshift_double (temp.low, temp.high,
2231                      GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2232                      &temp_trunc.low, &temp_trunc.high,
2233                      SIGNED_FIXED_POINT_MODE_P (mode));
2234     }
2235   else
2236     {
2237       temp.low = 0;
2238       temp.high = 0;
2239       temp_trunc.low = 0;
2240       temp_trunc.high = 0;
2241     }
2242
2243   /* If FIXED_CST is negative, we need to round the value toward 0.
2244      By checking if the fractional bits are not zero to add 1 to temp.  */
2245   if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2246       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2247     {
2248       double_int one;
2249       one.low = 1;
2250       one.high = 0;
2251       temp = double_int_add (temp, one);
2252     }
2253
2254   /* Given a fixed-point constant, make new constant with new type,
2255      appropriately sign-extended or truncated.  */
2256   t = force_fit_type_double (type, temp.low, temp.high, -1,
2257                              (temp.high < 0
2258                               && (TYPE_UNSIGNED (type)
2259                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2260                              | TREE_OVERFLOW (arg1));
2261
2262   return t;
2263 }
2264
2265 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2266    to another floating point type.  */
2267
2268 static tree
2269 fold_convert_const_real_from_real (tree type, const_tree arg1)
2270 {
2271   REAL_VALUE_TYPE value;
2272   tree t;
2273
2274   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2275   t = build_real (type, value);
2276
2277   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2278   return t;
2279 }
2280
2281 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2282    to a floating point type.  */
2283
2284 static tree
2285 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2286 {
2287   REAL_VALUE_TYPE value;
2288   tree t;
2289
2290   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2291   t = build_real (type, value);
2292
2293   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2294   TREE_CONSTANT_OVERFLOW (t)
2295     = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2296   return t;
2297 }
2298
2299 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2300    to another fixed-point type.  */
2301
2302 static tree
2303 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2304 {
2305   FIXED_VALUE_TYPE value;
2306   tree t;
2307   bool overflow_p;
2308
2309   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2310                               TYPE_SATURATING (type));
2311   t = build_fixed (type, value);
2312
2313   /* Propagate overflow flags.  */
2314   if (overflow_p | TREE_OVERFLOW (arg1))
2315     {
2316       TREE_OVERFLOW (t) = 1;
2317       TREE_CONSTANT_OVERFLOW (t) = 1;
2318     }
2319   else if (TREE_CONSTANT_OVERFLOW (arg1))
2320     TREE_CONSTANT_OVERFLOW (t) = 1;
2321   return t;
2322 }
2323
2324 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2325    to a fixed-point type.  */
2326
2327 static tree
2328 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2329 {
2330   FIXED_VALUE_TYPE value;
2331   tree t;
2332   bool overflow_p;
2333
2334   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2335                                        TREE_INT_CST (arg1),
2336                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
2337                                        TYPE_SATURATING (type));
2338   t = build_fixed (type, value);
2339
2340   /* Propagate overflow flags.  */
2341   if (overflow_p | TREE_OVERFLOW (arg1))
2342     {
2343       TREE_OVERFLOW (t) = 1;
2344       TREE_CONSTANT_OVERFLOW (t) = 1;
2345     }
2346   else if (TREE_CONSTANT_OVERFLOW (arg1))
2347     TREE_CONSTANT_OVERFLOW (t) = 1;
2348   return t;
2349 }
2350
2351 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2352    to a fixed-point type.  */
2353
2354 static tree
2355 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2356 {
2357   FIXED_VALUE_TYPE value;
2358   tree t;
2359   bool overflow_p;
2360
2361   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2362                                         &TREE_REAL_CST (arg1),
2363                                         TYPE_SATURATING (type));
2364   t = build_fixed (type, value);
2365
2366   /* Propagate overflow flags.  */
2367   if (overflow_p | TREE_OVERFLOW (arg1))
2368     {
2369       TREE_OVERFLOW (t) = 1;
2370       TREE_CONSTANT_OVERFLOW (t) = 1;
2371     }
2372   else if (TREE_CONSTANT_OVERFLOW (arg1))
2373     TREE_CONSTANT_OVERFLOW (t) = 1;
2374   return t;
2375 }
2376
2377 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2378    type TYPE.  If no simplification can be done return NULL_TREE.  */
2379
2380 static tree
2381 fold_convert_const (enum tree_code code, tree type, tree arg1)
2382 {
2383   if (TREE_TYPE (arg1) == type)
2384     return arg1;
2385
2386   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2387     {
2388       if (TREE_CODE (arg1) == INTEGER_CST)
2389         return fold_convert_const_int_from_int (type, arg1);
2390       else if (TREE_CODE (arg1) == REAL_CST)
2391         return fold_convert_const_int_from_real (code, type, arg1);
2392       else if (TREE_CODE (arg1) == FIXED_CST)
2393         return fold_convert_const_int_from_fixed (type, arg1);
2394     }
2395   else if (TREE_CODE (type) == REAL_TYPE)
2396     {
2397       if (TREE_CODE (arg1) == INTEGER_CST)
2398         return build_real_from_int_cst (type, arg1);
2399       else if (TREE_CODE (arg1) == REAL_CST)
2400         return fold_convert_const_real_from_real (type, arg1);
2401       else if (TREE_CODE (arg1) == FIXED_CST)
2402         return fold_convert_const_real_from_fixed (type, arg1);
2403     }
2404   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2405     {
2406       if (TREE_CODE (arg1) == FIXED_CST)
2407         return fold_convert_const_fixed_from_fixed (type, arg1);
2408       else if (TREE_CODE (arg1) == INTEGER_CST)
2409         return fold_convert_const_fixed_from_int (type, arg1);
2410       else if (TREE_CODE (arg1) == REAL_CST)
2411         return fold_convert_const_fixed_from_real (type, arg1);
2412     }
2413   return NULL_TREE;
2414 }
2415
2416 /* Construct a vector of zero elements of vector type TYPE.  */
2417
2418 static tree
2419 build_zero_vector (tree type)
2420 {
2421   tree elem, list;
2422   int i, units;
2423
2424   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2425   units = TYPE_VECTOR_SUBPARTS (type);
2426   
2427   list = NULL_TREE;
2428   for (i = 0; i < units; i++)
2429     list = tree_cons (NULL_TREE, elem, list);
2430   return build_vector (type, list);
2431 }
2432
2433 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
2434
2435 bool
2436 fold_convertible_p (const_tree type, const_tree arg)
2437 {
2438   tree orig = TREE_TYPE (arg);
2439
2440   if (type == orig)
2441     return true;
2442
2443   if (TREE_CODE (arg) == ERROR_MARK
2444       || TREE_CODE (type) == ERROR_MARK
2445       || TREE_CODE (orig) == ERROR_MARK)
2446     return false;
2447
2448   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2449     return true;
2450
2451   switch (TREE_CODE (type))
2452     {
2453     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2454     case POINTER_TYPE: case REFERENCE_TYPE:
2455     case OFFSET_TYPE:
2456       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2457           || TREE_CODE (orig) == OFFSET_TYPE)
2458         return true;
2459       return (TREE_CODE (orig) == VECTOR_TYPE
2460               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2461
2462     default:
2463       return TREE_CODE (type) == TREE_CODE (orig);
2464     }
2465 }
2466
2467 /* Convert expression ARG to type TYPE.  Used by the middle-end for
2468    simple conversions in preference to calling the front-end's convert.  */
2469
2470 tree
2471 fold_convert (tree type, tree arg)
2472 {
2473   tree orig = TREE_TYPE (arg);
2474   tree tem;
2475
2476   if (type == orig)
2477     return arg;
2478
2479   if (TREE_CODE (arg) == ERROR_MARK
2480       || TREE_CODE (type) == ERROR_MARK
2481       || TREE_CODE (orig) == ERROR_MARK)
2482     return error_mark_node;
2483
2484   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2485     return fold_build1 (NOP_EXPR, type, arg);
2486
2487   switch (TREE_CODE (type))
2488     {
2489     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2490     case POINTER_TYPE: case REFERENCE_TYPE:
2491     case OFFSET_TYPE:
2492       if (TREE_CODE (arg) == INTEGER_CST)
2493         {
2494           tem = fold_convert_const (NOP_EXPR, type, arg);
2495           if (tem != NULL_TREE)
2496             return tem;
2497         }
2498       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2499           || TREE_CODE (orig) == OFFSET_TYPE)
2500         return fold_build1 (NOP_EXPR, type, arg);
2501       if (TREE_CODE (orig) == COMPLEX_TYPE)
2502         {
2503           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2504           return fold_convert (type, tem);
2505         }
2506       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2507                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2508       return fold_build1 (NOP_EXPR, type, arg);
2509
2510     case REAL_TYPE:
2511       if (TREE_CODE (arg) == INTEGER_CST)
2512         {
2513           tem = fold_convert_const (FLOAT_EXPR, type, arg);
2514           if (tem != NULL_TREE)
2515             return tem;
2516         }
2517       else if (TREE_CODE (arg) == REAL_CST)
2518         {
2519           tem = fold_convert_const (NOP_EXPR, type, arg);
2520           if (tem != NULL_TREE)
2521             return tem;
2522         }
2523       else if (TREE_CODE (arg) == FIXED_CST)
2524         {
2525           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2526           if (tem != NULL_TREE)
2527             return tem;
2528         }
2529
2530       switch (TREE_CODE (orig))
2531         {
2532         case INTEGER_TYPE:
2533         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2534         case POINTER_TYPE: case REFERENCE_TYPE:
2535           return fold_build1 (FLOAT_EXPR, type, arg);
2536
2537         case REAL_TYPE:
2538           return fold_build1 (NOP_EXPR, type, arg);
2539
2540         case FIXED_POINT_TYPE:
2541           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2542
2543         case COMPLEX_TYPE:
2544           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2545           return fold_convert (type, tem);
2546
2547         default:
2548           gcc_unreachable ();
2549         }
2550
2551     case FIXED_POINT_TYPE:
2552       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2553           || TREE_CODE (arg) == REAL_CST)
2554         {
2555           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2556           if (tem != NULL_TREE)
2557             return tem;
2558         }
2559
2560       switch (TREE_CODE (orig))
2561         {
2562         case FIXED_POINT_TYPE:
2563         case INTEGER_TYPE:
2564         case ENUMERAL_TYPE:
2565         case BOOLEAN_TYPE:
2566         case REAL_TYPE:
2567           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2568
2569         case COMPLEX_TYPE:
2570           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2571           return fold_convert (type, tem);
2572
2573         default:
2574           gcc_unreachable ();
2575         }
2576
2577     case COMPLEX_TYPE:
2578       switch (TREE_CODE (orig))
2579         {
2580         case INTEGER_TYPE:
2581         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2582         case POINTER_TYPE: case REFERENCE_TYPE:
2583         case REAL_TYPE:
2584         case FIXED_POINT_TYPE:
2585           return build2 (COMPLEX_EXPR, type,
2586                          fold_convert (TREE_TYPE (type), arg),
2587                          fold_convert (TREE_TYPE (type), integer_zero_node));
2588         case COMPLEX_TYPE:
2589           {
2590             tree rpart, ipart;
2591
2592             if (TREE_CODE (arg) == COMPLEX_EXPR)
2593               {
2594                 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2595                 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2596                 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2597               }
2598
2599             arg = save_expr (arg);
2600             rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2601             ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2602             rpart = fold_convert (TREE_TYPE (type), rpart);
2603             ipart = fold_convert (TREE_TYPE (type), ipart);
2604             return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2605           }
2606
2607         default:
2608           gcc_unreachable ();
2609         }
2610
2611     case VECTOR_TYPE:
2612       if (integer_zerop (arg))
2613         return build_zero_vector (type);
2614       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2615       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2616                   || TREE_CODE (orig) == VECTOR_TYPE);
2617       return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2618
2619     case VOID_TYPE:
2620       tem = fold_ignored_result (arg);
2621       if (TREE_CODE (tem) == GIMPLE_MODIFY_STMT)
2622         return tem;
2623       return fold_build1 (NOP_EXPR, type, tem);
2624
2625     default:
2626       gcc_unreachable ();
2627     }
2628 }
2629 \f
2630 /* Return false if expr can be assumed not to be an lvalue, true
2631    otherwise.  */
2632
2633 static bool
2634 maybe_lvalue_p (const_tree x)
2635 {
2636   /* We only need to wrap lvalue tree codes.  */
2637   switch (TREE_CODE (x))
2638   {
2639   case VAR_DECL:
2640   case PARM_DECL:
2641   case RESULT_DECL:
2642   case LABEL_DECL:
2643   case FUNCTION_DECL:
2644   case SSA_NAME:
2645
2646   case COMPONENT_REF:
2647   case INDIRECT_REF:
2648   case ALIGN_INDIRECT_REF:
2649   case MISALIGNED_INDIRECT_REF:
2650   case ARRAY_REF:
2651   case ARRAY_RANGE_REF:
2652   case BIT_FIELD_REF:
2653   case OBJ_TYPE_REF:
2654
2655   case REALPART_EXPR:
2656   case IMAGPART_EXPR:
2657   case PREINCREMENT_EXPR:
2658   case PREDECREMENT_EXPR:
2659   case SAVE_EXPR:
2660   case TRY_CATCH_EXPR:
2661   case WITH_CLEANUP_EXPR:
2662   case COMPOUND_EXPR:
2663   case MODIFY_EXPR:
2664   case GIMPLE_MODIFY_STMT:
2665   case TARGET_EXPR:
2666   case COND_EXPR:
2667   case BIND_EXPR:
2668   case MIN_EXPR:
2669   case MAX_EXPR:
2670     break;
2671
2672   default:
2673     /* Assume the worst for front-end tree codes.  */
2674     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2675       break;
2676     return false;
2677   }
2678
2679   return true;
2680 }
2681
2682 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2683
2684 tree
2685 non_lvalue (tree x)
2686 {
2687   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2688      us.  */
2689   if (in_gimple_form)
2690     return x;
2691
2692   if (! maybe_lvalue_p (x))
2693     return x;
2694   return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2695 }
2696
2697 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2698    Zero means allow extended lvalues.  */
2699
2700 int pedantic_lvalues;
2701
2702 /* When pedantic, return an expr equal to X but certainly not valid as a
2703    pedantic lvalue.  Otherwise, return X.  */
2704
2705 static tree
2706 pedantic_non_lvalue (tree x)
2707 {
2708   if (pedantic_lvalues)
2709     return non_lvalue (x);
2710   else
2711     return x;
2712 }
2713 \f
2714 /* Given a tree comparison code, return the code that is the logical inverse
2715    of the given code.  It is not safe to do this for floating-point
2716    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2717    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2718
2719 enum tree_code
2720 invert_tree_comparison (enum tree_code code, bool honor_nans)
2721 {
2722   if (honor_nans && flag_trapping_math)
2723     return ERROR_MARK;
2724
2725   switch (code)
2726     {
2727     case EQ_EXPR:
2728       return NE_EXPR;
2729     case NE_EXPR:
2730       return EQ_EXPR;
2731     case GT_EXPR:
2732       return honor_nans ? UNLE_EXPR : LE_EXPR;
2733     case GE_EXPR:
2734       return honor_nans ? UNLT_EXPR : LT_EXPR;
2735     case LT_EXPR:
2736       return honor_nans ? UNGE_EXPR : GE_EXPR;
2737     case LE_EXPR:
2738       return honor_nans ? UNGT_EXPR : GT_EXPR;
2739     case LTGT_EXPR:
2740       return UNEQ_EXPR;
2741     case UNEQ_EXPR:
2742       return LTGT_EXPR;
2743     case UNGT_EXPR:
2744       return LE_EXPR;
2745     case UNGE_EXPR:
2746       return LT_EXPR;
2747     case UNLT_EXPR:
2748       return GE_EXPR;
2749     case UNLE_EXPR:
2750       return GT_EXPR;
2751     case ORDERED_EXPR:
2752       return UNORDERED_EXPR;
2753     case UNORDERED_EXPR:
2754       return ORDERED_EXPR;
2755     default:
2756       gcc_unreachable ();
2757     }
2758 }
2759
2760 /* Similar, but return the comparison that results if the operands are
2761    swapped.  This is safe for floating-point.  */
2762
2763 enum tree_code
2764 swap_tree_comparison (enum tree_code code)
2765 {
2766   switch (code)
2767     {
2768     case EQ_EXPR:
2769     case NE_EXPR:
2770     case ORDERED_EXPR:
2771     case UNORDERED_EXPR:
2772     case LTGT_EXPR:
2773     case UNEQ_EXPR:
2774       return code;
2775     case GT_EXPR:
2776       return LT_EXPR;
2777     case GE_EXPR:
2778       return LE_EXPR;
2779     case LT_EXPR:
2780       return GT_EXPR;
2781     case LE_EXPR:
2782       return GE_EXPR;
2783     case UNGT_EXPR:
2784       return UNLT_EXPR;
2785     case UNGE_EXPR:
2786       return UNLE_EXPR;
2787     case UNLT_EXPR:
2788       return UNGT_EXPR;
2789     case UNLE_EXPR:
2790       return UNGE_EXPR;
2791     default:
2792       gcc_unreachable ();
2793     }
2794 }
2795
2796
2797 /* Convert a comparison tree code from an enum tree_code representation
2798    into a compcode bit-based encoding.  This function is the inverse of
2799    compcode_to_comparison.  */
2800
2801 static enum comparison_code
2802 comparison_to_compcode (enum tree_code code)
2803 {
2804   switch (code)
2805     {
2806     case LT_EXPR:
2807       return COMPCODE_LT;
2808     case EQ_EXPR:
2809       return COMPCODE_EQ;
2810     case LE_EXPR:
2811       return COMPCODE_LE;
2812     case GT_EXPR:
2813       return COMPCODE_GT;
2814     case NE_EXPR:
2815       return COMPCODE_NE;
2816     case GE_EXPR:
2817       return COMPCODE_GE;
2818     case ORDERED_EXPR:
2819       return COMPCODE_ORD;
2820     case UNORDERED_EXPR:
2821       return COMPCODE_UNORD;
2822     case UNLT_EXPR:
2823       return COMPCODE_UNLT;
2824     case UNEQ_EXPR:
2825       return COMPCODE_UNEQ;
2826     case UNLE_EXPR:
2827       return COMPCODE_UNLE;
2828     case UNGT_EXPR:
2829       return COMPCODE_UNGT;
2830     case LTGT_EXPR:
2831       return COMPCODE_LTGT;
2832     case UNGE_EXPR:
2833       return COMPCODE_UNGE;
2834     default:
2835       gcc_unreachable ();
2836     }
2837 }
2838
2839 /* Convert a compcode bit-based encoding of a comparison operator back
2840    to GCC's enum tree_code representation.  This function is the
2841    inverse of comparison_to_compcode.  */
2842
2843 static enum tree_code
2844 compcode_to_comparison (enum comparison_code code)
2845 {
2846   switch (code)
2847     {
2848     case COMPCODE_LT:
2849       return LT_EXPR;
2850     case COMPCODE_EQ:
2851       return EQ_EXPR;
2852     case COMPCODE_LE:
2853       return LE_EXPR;
2854     case COMPCODE_GT:
2855       return GT_EXPR;
2856     case COMPCODE_NE:
2857       return NE_EXPR;
2858     case COMPCODE_GE:
2859       return GE_EXPR;
2860     case COMPCODE_ORD:
2861       return ORDERED_EXPR;
2862     case COMPCODE_UNORD:
2863       return UNORDERED_EXPR;
2864     case COMPCODE_UNLT:
2865       return UNLT_EXPR;
2866     case COMPCODE_UNEQ:
2867       return UNEQ_EXPR;
2868     case COMPCODE_UNLE:
2869       return UNLE_EXPR;
2870     case COMPCODE_UNGT:
2871       return UNGT_EXPR;
2872     case COMPCODE_LTGT:
2873       return LTGT_EXPR;
2874     case COMPCODE_UNGE:
2875       return UNGE_EXPR;
2876     default:
2877       gcc_unreachable ();
2878     }
2879 }
2880
2881 /* Return a tree for the comparison which is the combination of
2882    doing the AND or OR (depending on CODE) of the two operations LCODE
2883    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2884    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2885    if this makes the transformation invalid.  */
2886
2887 tree
2888 combine_comparisons (enum tree_code code, enum tree_code lcode,
2889                      enum tree_code rcode, tree truth_type,
2890                      tree ll_arg, tree lr_arg)
2891 {
2892   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2893   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2894   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2895   enum comparison_code compcode;
2896
2897   switch (code)
2898     {
2899     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2900       compcode = lcompcode & rcompcode;
2901       break;
2902
2903     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2904       compcode = lcompcode | rcompcode;
2905       break;
2906
2907     default:
2908       return NULL_TREE;
2909     }
2910
2911   if (!honor_nans)
2912     {
2913       /* Eliminate unordered comparisons, as well as LTGT and ORD
2914          which are not used unless the mode has NaNs.  */
2915       compcode &= ~COMPCODE_UNORD;
2916       if (compcode == COMPCODE_LTGT)
2917         compcode = COMPCODE_NE;
2918       else if (compcode == COMPCODE_ORD)
2919         compcode = COMPCODE_TRUE;
2920     }
2921    else if (flag_trapping_math)
2922      {
2923         /* Check that the original operation and the optimized ones will trap
2924            under the same condition.  */
2925         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2926                      && (lcompcode != COMPCODE_EQ)
2927                      && (lcompcode != COMPCODE_ORD);
2928         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2929                      && (rcompcode != COMPCODE_EQ)
2930                      && (rcompcode != COMPCODE_ORD);
2931         bool trap = (compcode & COMPCODE_UNORD) == 0
2932                     && (compcode != COMPCODE_EQ)
2933                     && (compcode != COMPCODE_ORD);
2934
2935         /* In a short-circuited boolean expression the LHS might be
2936            such that the RHS, if evaluated, will never trap.  For
2937            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2938            if neither x nor y is NaN.  (This is a mixed blessing: for
2939            example, the expression above will never trap, hence
2940            optimizing it to x < y would be invalid).  */
2941         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2942             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2943           rtrap = false;
2944
2945         /* If the comparison was short-circuited, and only the RHS
2946            trapped, we may now generate a spurious trap.  */
2947         if (rtrap && !ltrap
2948             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2949           return NULL_TREE;
2950
2951         /* If we changed the conditions that cause a trap, we lose.  */
2952         if ((ltrap || rtrap) != trap)
2953           return NULL_TREE;
2954       }
2955
2956   if (compcode == COMPCODE_TRUE)
2957     return constant_boolean_node (true, truth_type);
2958   else if (compcode == COMPCODE_FALSE)
2959     return constant_boolean_node (false, truth_type);
2960   else
2961     return fold_build2 (compcode_to_comparison (compcode),
2962                         truth_type, ll_arg, lr_arg);
2963 }
2964
2965 /* Return nonzero if CODE is a tree code that represents a truth value.  */
2966
2967 static int
2968 truth_value_p (enum tree_code code)
2969 {
2970   return (TREE_CODE_CLASS (code) == tcc_comparison
2971           || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2972           || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2973           || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2974 }
2975 \f
2976 /* Return nonzero if two operands (typically of the same tree node)
2977    are necessarily equal.  If either argument has side-effects this
2978    function returns zero.  FLAGS modifies behavior as follows:
2979
2980    If OEP_ONLY_CONST is set, only return nonzero for constants.
2981    This function tests whether the operands are indistinguishable;
2982    it does not test whether they are equal using C's == operation.
2983    The distinction is important for IEEE floating point, because
2984    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2985    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2986
2987    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2988    even though it may hold multiple values during a function.
2989    This is because a GCC tree node guarantees that nothing else is
2990    executed between the evaluation of its "operands" (which may often
2991    be evaluated in arbitrary order).  Hence if the operands themselves
2992    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2993    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
2994    unset means assuming isochronic (or instantaneous) tree equivalence.
2995    Unless comparing arbitrary expression trees, such as from different
2996    statements, this flag can usually be left unset.
2997
2998    If OEP_PURE_SAME is set, then pure functions with identical arguments
2999    are considered the same.  It is used when the caller has other ways
3000    to ensure that global memory is unchanged in between.  */
3001
3002 int
3003 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3004 {
3005   /* If either is ERROR_MARK, they aren't equal.  */
3006   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3007     return 0;
3008
3009   /* If both types don't have the same signedness, then we can't consider
3010      them equal.  We must check this before the STRIP_NOPS calls
3011      because they may change the signedness of the arguments.  */
3012   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3013     return 0;
3014
3015   /* If both types don't have the same precision, then it is not safe
3016      to strip NOPs.  */
3017   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3018     return 0;
3019
3020   STRIP_NOPS (arg0);
3021   STRIP_NOPS (arg1);
3022
3023   /* In case both args are comparisons but with different comparison
3024      code, try to swap the comparison operands of one arg to produce
3025      a match and compare that variant.  */
3026   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3027       && COMPARISON_CLASS_P (arg0)
3028       && COMPARISON_CLASS_P (arg1))
3029     {
3030       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3031
3032       if (TREE_CODE (arg0) == swap_code)
3033         return operand_equal_p (TREE_OPERAND (arg0, 0),
3034                                 TREE_OPERAND (arg1, 1), flags)
3035                && operand_equal_p (TREE_OPERAND (arg0, 1),
3036                                    TREE_OPERAND (arg1, 0), flags);
3037     }
3038
3039   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3040       /* This is needed for conversions and for COMPONENT_REF.
3041          Might as well play it safe and always test this.  */
3042       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3043       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3044       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3045     return 0;
3046
3047   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3048      We don't care about side effects in that case because the SAVE_EXPR
3049      takes care of that for us. In all other cases, two expressions are
3050      equal if they have no side effects.  If we have two identical
3051      expressions with side effects that should be treated the same due
3052      to the only side effects being identical SAVE_EXPR's, that will
3053      be detected in the recursive calls below.  */
3054   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3055       && (TREE_CODE (arg0) == SAVE_EXPR
3056           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3057     return 1;
3058
3059   /* Next handle constant cases, those for which we can return 1 even
3060      if ONLY_CONST is set.  */
3061   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3062     switch (TREE_CODE (arg0))
3063       {
3064       case INTEGER_CST:
3065         return tree_int_cst_equal (arg0, arg1);
3066
3067       case FIXED_CST:
3068         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3069                                        TREE_FIXED_CST (arg1));
3070
3071       case REAL_CST:
3072         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3073                                    TREE_REAL_CST (arg1)))
3074           return 1;
3075
3076         
3077         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3078           {
3079             /* If we do not distinguish between signed and unsigned zero,
3080                consider them equal.  */
3081             if (real_zerop (arg0) && real_zerop (arg1))
3082               return 1;
3083           }
3084         return 0;
3085
3086       case VECTOR_CST:
3087         {
3088           tree v1, v2;
3089
3090           v1 = TREE_VECTOR_CST_ELTS (arg0);
3091           v2 = TREE_VECTOR_CST_ELTS (arg1);
3092           while (v1 && v2)
3093             {
3094               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3095                                     flags))
3096                 return 0;
3097               v1 = TREE_CHAIN (v1);
3098               v2 = TREE_CHAIN (v2);
3099             }
3100
3101           return v1 == v2;
3102         }
3103
3104       case COMPLEX_CST:
3105         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3106                                  flags)
3107                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3108                                     flags));
3109
3110       case STRING_CST:
3111         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3112                 && ! memcmp (TREE_STRING_POINTER (arg0),
3113                               TREE_STRING_POINTER (arg1),
3114                               TREE_STRING_LENGTH (arg0)));
3115
3116       case ADDR_EXPR:
3117         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3118                                 0);
3119       default:
3120         break;
3121       }
3122
3123   if (flags & OEP_ONLY_CONST)
3124     return 0;
3125
3126 /* Define macros to test an operand from arg0 and arg1 for equality and a
3127    variant that allows null and views null as being different from any
3128    non-null value.  In the latter case, if either is null, the both
3129    must be; otherwise, do the normal comparison.  */
3130 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
3131                                     TREE_OPERAND (arg1, N), flags)
3132
3133 #define OP_SAME_WITH_NULL(N)                            \
3134   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3135    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3136
3137   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3138     {
3139     case tcc_unary:
3140       /* Two conversions are equal only if signedness and modes match.  */
3141       switch (TREE_CODE (arg0))
3142         {
3143         case NOP_EXPR:
3144         case CONVERT_EXPR:
3145         case FIX_TRUNC_EXPR:
3146           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3147               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3148             return 0;
3149           break;
3150         default:
3151           break;
3152         }
3153
3154       return OP_SAME (0);
3155
3156
3157     case tcc_comparison:
3158     case tcc_binary:
3159       if (OP_SAME (0) && OP_SAME (1))
3160         return 1;
3161
3162       /* For commutative ops, allow the other order.  */
3163       return (commutative_tree_code (TREE_CODE (arg0))
3164               && operand_equal_p (TREE_OPERAND (arg0, 0),
3165                                   TREE_OPERAND (arg1, 1), flags)
3166               && operand_equal_p (TREE_OPERAND (arg0, 1),
3167                                   TREE_OPERAND (arg1, 0), flags));
3168
3169     case tcc_reference:
3170       /* If either of the pointer (or reference) expressions we are
3171          dereferencing contain a side effect, these cannot be equal.  */
3172       if (TREE_SIDE_EFFECTS (arg0)
3173           || TREE_SIDE_EFFECTS (arg1))
3174         return 0;
3175
3176       switch (TREE_CODE (arg0))
3177         {
3178         case INDIRECT_REF:
3179         case ALIGN_INDIRECT_REF:
3180         case MISALIGNED_INDIRECT_REF:
3181         case REALPART_EXPR:
3182         case IMAGPART_EXPR:
3183           return OP_SAME (0);
3184
3185         case ARRAY_REF:
3186         case ARRAY_RANGE_REF:
3187           /* Operands 2 and 3 may be null.
3188              Compare the array index by value if it is constant first as we
3189              may have different types but same value here.  */
3190           return (OP_SAME (0)
3191                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3192                                           TREE_OPERAND (arg1, 1))
3193                       || OP_SAME (1))
3194                   && OP_SAME_WITH_NULL (2)
3195                   && OP_SAME_WITH_NULL (3));
3196
3197         case COMPONENT_REF:
3198           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
3199              may be NULL when we're called to compare MEM_EXPRs.  */
3200           return OP_SAME_WITH_NULL (0)
3201                  && OP_SAME (1)
3202                  && OP_SAME_WITH_NULL (2);
3203
3204         case BIT_FIELD_REF:
3205           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3206
3207         default:
3208           return 0;
3209         }
3210
3211     case tcc_expression:
3212       switch (TREE_CODE (arg0))
3213         {
3214         case ADDR_EXPR:
3215         case TRUTH_NOT_EXPR:
3216           return OP_SAME (0);
3217
3218         case TRUTH_ANDIF_EXPR:
3219         case TRUTH_ORIF_EXPR:
3220           return OP_SAME (0) && OP_SAME (1);
3221
3222         case TRUTH_AND_EXPR:
3223         case TRUTH_OR_EXPR:
3224         case TRUTH_XOR_EXPR:
3225           if (OP_SAME (0) && OP_SAME (1))
3226             return 1;
3227
3228           /* Otherwise take into account this is a commutative operation.  */
3229           return (operand_equal_p (TREE_OPERAND (arg0, 0),
3230                                    TREE_OPERAND (arg1, 1), flags)
3231                   && operand_equal_p (TREE_OPERAND (arg0, 1),
3232                                       TREE_OPERAND (arg1, 0), flags));
3233
3234         default:
3235           return 0;
3236         }
3237
3238     case tcc_vl_exp:
3239       switch (TREE_CODE (arg0))
3240         {
3241         case CALL_EXPR:
3242           /* If the CALL_EXPRs call different functions, then they
3243              clearly can not be equal.  */
3244           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3245                                  flags))
3246             return 0;
3247
3248           {
3249             unsigned int cef = call_expr_flags (arg0);
3250             if (flags & OEP_PURE_SAME)
3251               cef &= ECF_CONST | ECF_PURE;
3252             else
3253               cef &= ECF_CONST;
3254             if (!cef)
3255               return 0;
3256           }
3257
3258           /* Now see if all the arguments are the same.  */
3259           {
3260             const_call_expr_arg_iterator iter0, iter1;
3261             const_tree a0, a1;
3262             for (a0 = first_const_call_expr_arg (arg0, &iter0),
3263                    a1 = first_const_call_expr_arg (arg1, &iter1);
3264                  a0 && a1;
3265                  a0 = next_const_call_expr_arg (&iter0),
3266                    a1 = next_const_call_expr_arg (&iter1))
3267               if (! operand_equal_p (a0, a1, flags))
3268                 return 0;
3269
3270             /* If we get here and both argument lists are exhausted
3271                then the CALL_EXPRs are equal.  */
3272             return ! (a0 || a1);
3273           }
3274         default:
3275           return 0;
3276         }
3277
3278     case tcc_declaration:
3279       /* Consider __builtin_sqrt equal to sqrt.  */
3280       return (TREE_CODE (arg0) == FUNCTION_DECL
3281               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3282               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3283               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3284
3285     default:
3286       return 0;
3287     }
3288
3289 #undef OP_SAME
3290 #undef OP_SAME_WITH_NULL
3291 }
3292 \f
3293 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3294    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3295
3296    When in doubt, return 0.  */
3297
3298 static int
3299 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3300 {
3301   int unsignedp1, unsignedpo;
3302   tree primarg0, primarg1, primother;
3303   unsigned int correct_width;
3304
3305   if (operand_equal_p (arg0, arg1, 0))
3306     return 1;
3307
3308   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3309       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3310     return 0;
3311
3312   /* Discard any conversions that don't change the modes of ARG0 and ARG1
3313      and see if the inner values are the same.  This removes any
3314      signedness comparison, which doesn't matter here.  */
3315   primarg0 = arg0, primarg1 = arg1;
3316   STRIP_NOPS (primarg0);
3317   STRIP_NOPS (primarg1);
3318   if (operand_equal_p (primarg0, primarg1, 0))
3319     return 1;
3320
3321   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3322      actual comparison operand, ARG0.
3323
3324      First throw away any conversions to wider types
3325      already present in the operands.  */
3326
3327   primarg1 = get_narrower (arg1, &unsignedp1);
3328   primother = get_narrower (other, &unsignedpo);
3329
3330   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3331   if (unsignedp1 == unsignedpo
3332       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3333       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3334     {
3335       tree type = TREE_TYPE (arg0);
3336
3337       /* Make sure shorter operand is extended the right way
3338          to match the longer operand.  */
3339       primarg1 = fold_convert (signed_or_unsigned_type_for
3340                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3341
3342       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3343         return 1;
3344     }
3345
3346   return 0;
3347 }
3348 \f
3349 /* See if ARG is an expression that is either a comparison or is performing
3350    arithmetic on comparisons.  The comparisons must only be comparing
3351    two different values, which will be stored in *CVAL1 and *CVAL2; if
3352    they are nonzero it means that some operands have already been found.
3353    No variables may be used anywhere else in the expression except in the
3354    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
3355    the expression and save_expr needs to be called with CVAL1 and CVAL2.
3356
3357    If this is true, return 1.  Otherwise, return zero.  */
3358
3359 static int
3360 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3361 {
3362   enum tree_code code = TREE_CODE (arg);
3363   enum tree_code_class class = TREE_CODE_CLASS (code);
3364
3365   /* We can handle some of the tcc_expression cases here.  */
3366   if (class == tcc_expression && code == TRUTH_NOT_EXPR)
3367     class = tcc_unary;
3368   else if (class == tcc_expression
3369            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3370                || code == COMPOUND_EXPR))
3371     class = tcc_binary;
3372
3373   else if (class == tcc_expression && code == SAVE_EXPR
3374            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3375     {
3376       /* If we've already found a CVAL1 or CVAL2, this expression is
3377          two complex to handle.  */
3378       if (*cval1 || *cval2)
3379         return 0;
3380
3381       class = tcc_unary;
3382       *save_p = 1;
3383     }
3384
3385   switch (class)
3386     {
3387     case tcc_unary:
3388       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3389
3390     case tcc_binary:
3391       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3392               && twoval_comparison_p (TREE_OPERAND (arg, 1),
3393                                       cval1, cval2, save_p));
3394
3395     case tcc_constant:
3396       return 1;
3397
3398     case tcc_expression:
3399       if (code == COND_EXPR)
3400         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3401                                      cval1, cval2, save_p)
3402                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3403                                         cval1, cval2, save_p)
3404                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3405                                         cval1, cval2, save_p));
3406       return 0;
3407
3408     case tcc_comparison:
3409       /* First see if we can handle the first operand, then the second.  For
3410          the second operand, we know *CVAL1 can't be zero.  It must be that
3411          one side of the comparison is each of the values; test for the
3412          case where this isn't true by failing if the two operands
3413          are the same.  */
3414
3415       if (operand_equal_p (TREE_OPERAND (arg, 0),
3416                            TREE_OPERAND (arg, 1), 0))
3417         return 0;
3418
3419       if (*cval1 == 0)
3420         *cval1 = TREE_OPERAND (arg, 0);
3421       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3422         ;
3423       else if (*cval2 == 0)
3424         *cval2 = TREE_OPERAND (arg, 0);
3425       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3426         ;
3427       else
3428         return 0;
3429
3430       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3431         ;
3432       else if (*cval2 == 0)
3433         *cval2 = TREE_OPERAND (arg, 1);
3434       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3435         ;
3436       else
3437         return 0;
3438
3439       return 1;
3440
3441     default:
3442       return 0;
3443     }
3444 }
3445 \f
3446 /* ARG is a tree that is known to contain just arithmetic operations and
3447    comparisons.  Evaluate the operations in the tree substituting NEW0 for
3448    any occurrence of OLD0 as an operand of a comparison and likewise for
3449    NEW1 and OLD1.  */
3450
3451 static tree
3452 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3453 {
3454   tree type = TREE_TYPE (arg);
3455   enum tree_code code = TREE_CODE (arg);
3456   enum tree_code_class class = TREE_CODE_CLASS (code);
3457
3458   /* We can handle some of the tcc_expression cases here.  */
3459   if (class == tcc_expression && code == TRUTH_NOT_EXPR)
3460     class = tcc_unary;
3461   else if (class == tcc_expression
3462            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3463     class = tcc_binary;
3464
3465   switch (class)
3466     {
3467     case tcc_unary:
3468       return fold_build1 (code, type,
3469                           eval_subst (TREE_OPERAND (arg, 0),
3470                                       old0, new0, old1, new1));
3471
3472     case tcc_binary:
3473       return fold_build2 (code, type,
3474                           eval_subst (TREE_OPERAND (arg, 0),
3475                                       old0, new0, old1, new1),
3476                           eval_subst (TREE_OPERAND (arg, 1),
3477                                       old0, new0, old1, new1));
3478
3479     case tcc_expression:
3480       switch (code)
3481         {
3482         case SAVE_EXPR:
3483           return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3484
3485         case COMPOUND_EXPR:
3486           return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3487
3488         case COND_EXPR:
3489           return fold_build3 (code, type,
3490                               eval_subst (TREE_OPERAND (arg, 0),
3491                                           old0, new0, old1, new1),
3492                               eval_subst (TREE_OPERAND (arg, 1),
3493                                           old0, new0, old1, new1),
3494                               eval_subst (TREE_OPERAND (arg, 2),
3495                                           old0, new0, old1, new1));
3496         default:
3497           break;
3498         }
3499       /* Fall through - ???  */
3500
3501     case tcc_comparison:
3502       {
3503         tree arg0 = TREE_OPERAND (arg, 0);
3504         tree arg1 = TREE_OPERAND (arg, 1);
3505
3506         /* We need to check both for exact equality and tree equality.  The
3507            former will be true if the operand has a side-effect.  In that
3508            case, we know the operand occurred exactly once.  */
3509
3510         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3511           arg0 = new0;
3512         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3513           arg0 = new1;
3514
3515         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3516           arg1 = new0;
3517         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3518           arg1 = new1;
3519
3520         return fold_build2 (code, type, arg0, arg1);
3521       }
3522
3523     default:
3524       return arg;
3525     }
3526 }
3527 \f
3528 /* Return a tree for the case when the result of an expression is RESULT
3529    converted to TYPE and OMITTED was previously an operand of the expression
3530    but is now not needed (e.g., we folded OMITTED * 0).
3531
3532    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
3533    the conversion of RESULT to TYPE.  */
3534
3535 tree
3536 omit_one_operand (tree type, tree result, tree omitted)
3537 {
3538   tree t = fold_convert (type, result);
3539
3540   /* If the resulting operand is an empty statement, just return the omitted
3541      statement casted to void. */
3542   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3543     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3544
3545   if (TREE_SIDE_EFFECTS (omitted))
3546     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3547
3548   return non_lvalue (t);
3549 }
3550
3551 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3552
3553 static tree
3554 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3555 {
3556   tree t = fold_convert (type, result);
3557
3558   /* If the resulting operand is an empty statement, just return the omitted
3559      statement casted to void. */
3560   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3561     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3562
3563   if (TREE_SIDE_EFFECTS (omitted))
3564     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3565
3566   return pedantic_non_lvalue (t);
3567 }
3568
3569 /* Return a tree for the case when the result of an expression is RESULT
3570    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3571    of the expression but are now not needed.
3572
3573    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3574    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3575    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3576    just do the conversion of RESULT to TYPE.  */
3577
3578 tree
3579 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3580 {
3581   tree t = fold_convert (type, result);
3582
3583   if (TREE_SIDE_EFFECTS (omitted2))
3584     t = build2 (COMPOUND_EXPR, type, omitted2, t);
3585   if (TREE_SIDE_EFFECTS (omitted1))
3586     t = build2 (COMPOUND_EXPR, type, omitted1, t);
3587
3588   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3589 }
3590
3591 \f
3592 /* Return a simplified tree node for the truth-negation of ARG.  This
3593    never alters ARG itself.  We assume that ARG is an operation that
3594    returns a truth value (0 or 1).
3595
3596    FIXME: one would think we would fold the result, but it causes
3597    problems with the dominator optimizer.  */
3598
3599 tree
3600 fold_truth_not_expr (tree arg)
3601 {
3602   tree type = TREE_TYPE (arg);
3603   enum tree_code code = TREE_CODE (arg);
3604
3605   /* If this is a comparison, we can simply invert it, except for
3606      floating-point non-equality comparisons, in which case we just
3607      enclose a TRUTH_NOT_EXPR around what we have.  */
3608
3609   if (TREE_CODE_CLASS (code) == tcc_comparison)
3610     {
3611       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3612       if (FLOAT_TYPE_P (op_type)
3613           && flag_trapping_math
3614           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3615           && code != NE_EXPR && code != EQ_EXPR)
3616         return NULL_TREE;
3617       else
3618         {
3619           code = invert_tree_comparison (code,
3620                                          HONOR_NANS (TYPE_MODE (op_type)));
3621           if (code == ERROR_MARK)
3622             return NULL_TREE;
3623           else
3624             return build2 (code, type,
3625                            TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3626         }
3627     }
3628
3629   switch (code)
3630     {
3631     case INTEGER_CST:
3632       return constant_boolean_node (integer_zerop (arg), type);
3633
3634     case TRUTH_AND_EXPR:
3635       return build2 (TRUTH_OR_EXPR, type,
3636                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3637                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3638
3639     case TRUTH_OR_EXPR:
3640       return build2 (TRUTH_AND_EXPR, type,
3641                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3642                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3643
3644     case TRUTH_XOR_EXPR:
3645       /* Here we can invert either operand.  We invert the first operand
3646          unless the second operand is a TRUTH_NOT_EXPR in which case our
3647          result is the XOR of the first operand with the inside of the
3648          negation of the second operand.  */
3649
3650       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3651         return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3652                        TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3653       else
3654         return build2 (TRUTH_XOR_EXPR, type,
3655                        invert_truthvalue (TREE_OPERAND (arg, 0)),
3656                        TREE_OPERAND (arg, 1));
3657
3658     case TRUTH_ANDIF_EXPR:
3659       return build2 (TRUTH_ORIF_EXPR, type,
3660                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3661                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3662
3663     case TRUTH_ORIF_EXPR:
3664       return build2 (TRUTH_ANDIF_EXPR, type,
3665                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3666                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3667
3668     case TRUTH_NOT_EXPR:
3669       return TREE_OPERAND (arg, 0);
3670
3671     case COND_EXPR:
3672       {
3673         tree arg1 = TREE_OPERAND (arg, 1);
3674         tree arg2 = TREE_OPERAND (arg, 2);
3675         /* A COND_EXPR may have a throw as one operand, which
3676            then has void type.  Just leave void operands
3677            as they are.  */
3678         return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3679                        VOID_TYPE_P (TREE_TYPE (arg1))
3680                        ? arg1 : invert_truthvalue (arg1),
3681                        VOID_TYPE_P (TREE_TYPE (arg2))
3682                        ? arg2 : invert_truthvalue (arg2));
3683       }
3684
3685     case COMPOUND_EXPR:
3686       return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3687                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3688
3689     case NON_LVALUE_EXPR:
3690       return invert_truthvalue (TREE_OPERAND (arg, 0));
3691
3692     case NOP_EXPR:
3693       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3694         return build1 (TRUTH_NOT_EXPR, type, arg);
3695
3696     case CONVERT_EXPR:
3697     case FLOAT_EXPR:
3698       return build1 (TREE_CODE (arg), type,
3699                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3700
3701     case BIT_AND_EXPR:
3702       if (!integer_onep (TREE_OPERAND (arg, 1)))
3703         break;
3704       return build2 (EQ_EXPR, type, arg,
3705                      build_int_cst (type, 0));
3706
3707     case SAVE_EXPR:
3708       return build1 (TRUTH_NOT_EXPR, type, arg);
3709
3710     case CLEANUP_POINT_EXPR:
3711       return build1 (CLEANUP_POINT_EXPR, type,
3712                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3713
3714     default:
3715       break;
3716     }
3717
3718   return NULL_TREE;
3719 }
3720
3721 /* Return a simplified tree node for the truth-negation of ARG.  This
3722    never alters ARG itself.  We assume that ARG is an operation that
3723    returns a truth value (0 or 1).
3724
3725    FIXME: one would think we would fold the result, but it causes
3726    problems with the dominator optimizer.  */
3727
3728 tree
3729 invert_truthvalue (tree arg)
3730 {
3731   tree tem;
3732
3733   if (TREE_CODE (arg) == ERROR_MARK)
3734     return arg;
3735
3736   tem = fold_truth_not_expr (arg);
3737   if (!tem)
3738     tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3739
3740   return tem;
3741 }
3742
3743 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3744    operands are another bit-wise operation with a common input.  If so,
3745    distribute the bit operations to save an operation and possibly two if
3746    constants are involved.  For example, convert
3747         (A | B) & (A | C) into A | (B & C)
3748    Further simplification will occur if B and C are constants.
3749
3750    If this optimization cannot be done, 0 will be returned.  */
3751
3752 static tree
3753 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3754 {
3755   tree common;
3756   tree left, right;
3757
3758   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3759       || TREE_CODE (arg0) == code
3760       || (TREE_CODE (arg0) != BIT_AND_EXPR
3761           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3762     return 0;
3763
3764   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3765     {
3766       common = TREE_OPERAND (arg0, 0);
3767       left = TREE_OPERAND (arg0, 1);
3768       right = TREE_OPERAND (arg1, 1);
3769     }
3770   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3771     {
3772       common = TREE_OPERAND (arg0, 0);
3773       left = TREE_OPERAND (arg0, 1);
3774       right = TREE_OPERAND (arg1, 0);
3775     }
3776   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3777     {
3778       common = TREE_OPERAND (arg0, 1);
3779       left = TREE_OPERAND (arg0, 0);
3780       right = TREE_OPERAND (arg1, 1);
3781     }
3782   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3783     {
3784       common = TREE_OPERAND (arg0, 1);
3785       left = TREE_OPERAND (arg0, 0);
3786       right = TREE_OPERAND (arg1, 0);
3787     }
3788   else
3789     return 0;
3790
3791   return fold_build2 (TREE_CODE (arg0), type, common,
3792                       fold_build2 (code, type, left, right));
3793 }
3794
3795 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3796    with code CODE.  This optimization is unsafe.  */
3797 static tree
3798 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3799 {
3800   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3801   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3802
3803   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3804   if (mul0 == mul1
3805       && operand_equal_p (TREE_OPERAND (arg0, 1),
3806                        TREE_OPERAND (arg1, 1), 0))
3807     return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3808                         fold_build2 (code, type,
3809                                      TREE_OPERAND (arg0, 0),
3810                                      TREE_OPERAND (arg1, 0)),
3811                         TREE_OPERAND (arg0, 1));
3812
3813   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3814   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3815                        TREE_OPERAND (arg1, 0), 0)
3816       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3817       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3818     {
3819       REAL_VALUE_TYPE r0, r1;
3820       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3821       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3822       if (!mul0)
3823         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3824       if (!mul1)
3825         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3826       real_arithmetic (&r0, code, &r0, &r1);
3827       return fold_build2 (MULT_EXPR, type,
3828                           TREE_OPERAND (arg0, 0),
3829                           build_real (type, r0));
3830     }
3831
3832   return NULL_TREE;
3833 }
3834 \f
3835 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3836    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3837
3838 static tree
3839 make_bit_field_ref (tree inner, tree type, int bitsize, int bitpos,
3840                     int unsignedp)
3841 {
3842   tree result;
3843
3844   if (bitpos == 0)
3845     {
3846       tree size = TYPE_SIZE (TREE_TYPE (inner));
3847       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3848            || POINTER_TYPE_P (TREE_TYPE (inner)))
3849           && host_integerp (size, 0) 
3850           && tree_low_cst (size, 0) == bitsize)
3851         return fold_convert (type, inner);
3852     }
3853
3854   result = build3 (BIT_FIELD_REF, type, inner,
3855                    size_int (bitsize), bitsize_int (bitpos));
3856
3857   BIT_FIELD_REF_UNSIGNED (result) = unsignedp;
3858
3859   return result;
3860 }
3861
3862 /* Optimize a bit-field compare.
3863
3864    There are two cases:  First is a compare against a constant and the
3865    second is a comparison of two items where the fields are at the same
3866    bit position relative to the start of a chunk (byte, halfword, word)
3867    large enough to contain it.  In these cases we can avoid the shift
3868    implicit in bitfield extractions.
3869
3870    For constants, we emit a compare of the shifted constant with the
3871    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3872    compared.  For two fields at the same position, we do the ANDs with the
3873    similar mask and compare the result of the ANDs.
3874
3875    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3876    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3877    are the left and right operands of the comparison, respectively.
3878
3879    If the optimization described above can be done, we return the resulting
3880    tree.  Otherwise we return zero.  */
3881
3882 static tree
3883 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3884                             tree lhs, tree rhs)
3885 {
3886   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3887   tree type = TREE_TYPE (lhs);
3888   tree signed_type, unsigned_type;
3889   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3890   enum machine_mode lmode, rmode, nmode;
3891   int lunsignedp, runsignedp;
3892   int lvolatilep = 0, rvolatilep = 0;
3893   tree linner, rinner = NULL_TREE;
3894   tree mask;
3895   tree offset;
3896
3897   /* Get all the information about the extractions being done.  If the bit size
3898      if the same as the size of the underlying object, we aren't doing an
3899      extraction at all and so can do nothing.  We also don't want to
3900      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3901      then will no longer be able to replace it.  */
3902   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3903                                 &lunsignedp, &lvolatilep, false);
3904   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3905       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3906     return 0;
3907
3908  if (!const_p)
3909    {
3910      /* If this is not a constant, we can only do something if bit positions,
3911         sizes, and signedness are the same.  */
3912      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3913                                    &runsignedp, &rvolatilep, false);
3914
3915      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3916          || lunsignedp != runsignedp || offset != 0
3917          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3918        return 0;
3919    }
3920
3921   /* See if we can find a mode to refer to this field.  We should be able to,
3922      but fail if we can't.  */
3923   nmode = get_best_mode (lbitsize, lbitpos,
3924                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3925                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3926                                 TYPE_ALIGN (TREE_TYPE (rinner))),
3927                          word_mode, lvolatilep || rvolatilep);
3928   if (nmode == VOIDmode)
3929     return 0;
3930
3931   /* Set signed and unsigned types of the precision of this mode for the
3932      shifts below.  */
3933   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3934   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3935
3936   /* Compute the bit position and size for the new reference and our offset
3937      within it. If the new reference is the same size as the original, we
3938      won't optimize anything, so return zero.  */
3939   nbitsize = GET_MODE_BITSIZE (nmode);
3940   nbitpos = lbitpos & ~ (nbitsize - 1);
3941   lbitpos -= nbitpos;
3942   if (nbitsize == lbitsize)
3943     return 0;
3944
3945   if (BYTES_BIG_ENDIAN)
3946     lbitpos = nbitsize - lbitsize - lbitpos;
3947
3948   /* Make the mask to be used against the extracted field.  */
3949   mask = build_int_cst_type (unsigned_type, -1);
3950   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3951   mask = const_binop (RSHIFT_EXPR, mask,
3952                       size_int (nbitsize - lbitsize - lbitpos), 0);
3953
3954   if (! const_p)
3955     /* If not comparing with constant, just rework the comparison
3956        and return.  */
3957     return fold_build2 (code, compare_type,
3958                         fold_build2 (BIT_AND_EXPR, unsigned_type,
3959                                      make_bit_field_ref (linner,
3960                                                          unsigned_type,
3961                                                          nbitsize, nbitpos,
3962                                                          1),
3963                                      mask),
3964                         fold_build2 (BIT_AND_EXPR, unsigned_type,
3965                                      make_bit_field_ref (rinner,
3966                                                          unsigned_type,
3967                                                          nbitsize, nbitpos,
3968                                                          1),
3969                                      mask));
3970
3971   /* Otherwise, we are handling the constant case. See if the constant is too
3972      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
3973      this not only for its own sake, but to avoid having to test for this
3974      error case below.  If we didn't, we might generate wrong code.
3975
3976      For unsigned fields, the constant shifted right by the field length should
3977      be all zero.  For signed fields, the high-order bits should agree with
3978      the sign bit.  */
3979
3980   if (lunsignedp)
3981     {
3982       if (! integer_zerop (const_binop (RSHIFT_EXPR,
3983                                         fold_convert (unsigned_type, rhs),
3984                                         size_int (lbitsize), 0)))
3985         {
3986           warning (0, "comparison is always %d due to width of bit-field",
3987                    code == NE_EXPR);
3988           return constant_boolean_node (code == NE_EXPR, compare_type);
3989         }
3990     }
3991   else
3992     {
3993       tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
3994                               size_int (lbitsize - 1), 0);
3995       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3996         {
3997           warning (0, "comparison is always %d due to width of bit-field",
3998                    code == NE_EXPR);
3999           return constant_boolean_node (code == NE_EXPR, compare_type);
4000         }
4001     }
4002
4003   /* Single-bit compares should always be against zero.  */
4004   if (lbitsize == 1 && ! integer_zerop (rhs))
4005     {
4006       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4007       rhs = build_int_cst (type, 0);
4008     }
4009
4010   /* Make a new bitfield reference, shift the constant over the
4011      appropriate number of bits and mask it with the computed mask
4012      (in case this was a signed field).  If we changed it, make a new one.  */
4013   lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
4014   if (lvolatilep)
4015     {
4016       TREE_SIDE_EFFECTS (lhs) = 1;
4017       TREE_THIS_VOLATILE (lhs) = 1;
4018     }
4019
4020   rhs = const_binop (BIT_AND_EXPR,
4021                      const_binop (LSHIFT_EXPR,
4022                                   fold_convert (unsigned_type, rhs),
4023                                   size_int (lbitpos), 0),
4024                      mask, 0);
4025
4026   return build2 (code, compare_type,
4027                  build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4028                  rhs);
4029 }
4030 \f
4031 /* Subroutine for fold_truthop: decode a field reference.
4032
4033    If EXP is a comparison reference, we return the innermost reference.
4034
4035    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4036    set to the starting bit number.
4037
4038    If the innermost field can be completely contained in a mode-sized
4039    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
4040
4041    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4042    otherwise it is not changed.
4043
4044    *PUNSIGNEDP is set to the signedness of the field.
4045
4046    *PMASK is set to the mask used.  This is either contained in a
4047    BIT_AND_EXPR or derived from the width of the field.
4048
4049    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4050
4051    Return 0 if this is not a component reference or is one that we can't
4052    do anything with.  */
4053
4054 static tree
4055 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
4056                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4057                         int *punsignedp, int *pvolatilep,
4058                         tree *pmask, tree *pand_mask)
4059 {
4060   tree outer_type = 0;
4061   tree and_mask = 0;
4062   tree mask, inner, offset;
4063   tree unsigned_type;
4064   unsigned int precision;
4065
4066   /* All the optimizations using this function assume integer fields.
4067      There are problems with FP fields since the type_for_size call
4068      below can fail for, e.g., XFmode.  */
4069   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4070     return 0;
4071
4072   /* We are interested in the bare arrangement of bits, so strip everything
4073      that doesn't affect the machine mode.  However, record the type of the
4074      outermost expression if it may matter below.  */
4075   if (TREE_CODE (exp) == NOP_EXPR
4076       || TREE_CODE (exp) == CONVERT_EXPR
4077       || TREE_CODE (exp) == NON_LVALUE_EXPR)
4078     outer_type = TREE_TYPE (exp);
4079   STRIP_NOPS (exp);
4080
4081   if (TREE_CODE (exp) == BIT_AND_EXPR)
4082     {
4083       and_mask = TREE_OPERAND (exp, 1);
4084       exp = TREE_OPERAND (exp, 0);
4085       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4086       if (TREE_CODE (and_mask) != INTEGER_CST)
4087         return 0;
4088     }
4089
4090   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4091                                punsignedp, pvolatilep, false);
4092   if ((inner == exp && and_mask == 0)
4093       || *pbitsize < 0 || offset != 0
4094       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4095     return 0;
4096
4097   /* If the number of bits in the reference is the same as the bitsize of
4098      the outer type, then the outer type gives the signedness. Otherwise
4099      (in case of a small bitfield) the signedness is unchanged.  */
4100   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4101     *punsignedp = TYPE_UNSIGNED (outer_type);
4102
4103   /* Compute the mask to access the bitfield.  */
4104   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4105   precision = TYPE_PRECISION (unsigned_type);
4106
4107   mask = build_int_cst_type (unsigned_type, -1);
4108
4109   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4110   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4111
4112   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
4113   if (and_mask != 0)
4114     mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
4115                         fold_convert (unsigned_type, and_mask), mask);
4116
4117   *pmask = mask;
4118   *pand_mask = and_mask;
4119   return inner;
4120 }
4121
4122 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4123    bit positions.  */
4124
4125 static int
4126 all_ones_mask_p (const_tree mask, int size)
4127 {
4128   tree type = TREE_TYPE (mask);
4129   unsigned int precision = TYPE_PRECISION (type);
4130   tree tmask;
4131
4132   tmask = build_int_cst_type (signed_type_for (type), -1);
4133
4134   return
4135     tree_int_cst_equal (mask,
4136                         const_binop (RSHIFT_EXPR,
4137                                      const_binop (LSHIFT_EXPR, tmask,
4138                                                   size_int (precision - size),
4139                                                   0),
4140                                      size_int (precision - size), 0));
4141 }
4142
4143 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4144    represents the sign bit of EXP's type.  If EXP represents a sign
4145    or zero extension, also test VAL against the unextended type.
4146    The return value is the (sub)expression whose sign bit is VAL,
4147    or NULL_TREE otherwise.  */
4148
4149 static tree
4150 sign_bit_p (tree exp, const_tree val)
4151 {
4152   unsigned HOST_WIDE_INT mask_lo, lo;
4153   HOST_WIDE_INT mask_hi, hi;
4154   int width;
4155   tree t;
4156
4157   /* Tree EXP must have an integral type.  */
4158   t = TREE_TYPE (exp);
4159   if (! INTEGRAL_TYPE_P (t))
4160     return NULL_TREE;
4161
4162   /* Tree VAL must be an integer constant.  */
4163   if (TREE_CODE (val) != INTEGER_CST
4164       || TREE_OVERFLOW (val))
4165     return NULL_TREE;
4166
4167   width = TYPE_PRECISION (t);
4168   if (width > HOST_BITS_PER_WIDE_INT)
4169     {
4170       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4171       lo = 0;
4172
4173       mask_hi = ((unsigned HOST_WIDE_INT) -1
4174                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
4175       mask_lo = -1;
4176     }
4177   else
4178     {
4179       hi = 0;
4180       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4181
4182       mask_hi = 0;
4183       mask_lo = ((unsigned HOST_WIDE_INT) -1
4184                  >> (HOST_BITS_PER_WIDE_INT - width));
4185     }
4186
4187   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4188      treat VAL as if it were unsigned.  */
4189   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4190       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4191     return exp;
4192
4193   /* Handle extension from a narrower type.  */
4194   if (TREE_CODE (exp) == NOP_EXPR
4195       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4196     return sign_bit_p (TREE_OPERAND (exp, 0), val);
4197
4198   return NULL_TREE;
4199 }
4200
4201 /* Subroutine for fold_truthop: determine if an operand is simple enough
4202    to be evaluated unconditionally.  */
4203
4204 static int
4205 simple_operand_p (const_tree exp)
4206 {
4207   /* Strip any conversions that don't change the machine mode.  */
4208   STRIP_NOPS (exp);
4209
4210   return (CONSTANT_CLASS_P (exp)
4211           || TREE_CODE (exp) == SSA_NAME
4212           || (DECL_P (exp)
4213               && ! TREE_ADDRESSABLE (exp)
4214               && ! TREE_THIS_VOLATILE (exp)
4215               && ! DECL_NONLOCAL (exp)
4216               /* Don't regard global variables as simple.  They may be
4217                  allocated in ways unknown to the compiler (shared memory,
4218                  #pragma weak, etc).  */
4219               && ! TREE_PUBLIC (exp)
4220               && ! DECL_EXTERNAL (exp)
4221               /* Loading a static variable is unduly expensive, but global
4222                  registers aren't expensive.  */
4223               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4224 }
4225 \f
4226 /* The following functions are subroutines to fold_range_test and allow it to
4227    try to change a logical combination of comparisons into a range test.
4228
4229    For example, both
4230         X == 2 || X == 3 || X == 4 || X == 5
4231    and
4232         X >= 2 && X <= 5
4233    are converted to
4234         (unsigned) (X - 2) <= 3
4235
4236    We describe each set of comparisons as being either inside or outside
4237    a range, using a variable named like IN_P, and then describe the
4238    range with a lower and upper bound.  If one of the bounds is omitted,
4239    it represents either the highest or lowest value of the type.
4240
4241    In the comments below, we represent a range by two numbers in brackets
4242    preceded by a "+" to designate being inside that range, or a "-" to
4243    designate being outside that range, so the condition can be inverted by
4244    flipping the prefix.  An omitted bound is represented by a "-".  For
4245    example, "- [-, 10]" means being outside the range starting at the lowest
4246    possible value and ending at 10, in other words, being greater than 10.
4247    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4248    always false.
4249
4250    We set up things so that the missing bounds are handled in a consistent
4251    manner so neither a missing bound nor "true" and "false" need to be
4252    handled using a special case.  */
4253
4254 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4255    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4256    and UPPER1_P are nonzero if the respective argument is an upper bound
4257    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
4258    must be specified for a comparison.  ARG1 will be converted to ARG0's
4259    type if both are specified.  */
4260
4261 static tree
4262 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4263              tree arg1, int upper1_p)
4264 {
4265   tree tem;
4266   int result;
4267   int sgn0, sgn1;
4268
4269   /* If neither arg represents infinity, do the normal operation.
4270      Else, if not a comparison, return infinity.  Else handle the special
4271      comparison rules. Note that most of the cases below won't occur, but
4272      are handled for consistency.  */
4273
4274   if (arg0 != 0 && arg1 != 0)
4275     {
4276       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4277                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
4278       STRIP_NOPS (tem);
4279       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4280     }
4281
4282   if (TREE_CODE_CLASS (code) != tcc_comparison)
4283     return 0;
4284
4285   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4286      for neither.  In real maths, we cannot assume open ended ranges are
4287      the same. But, this is computer arithmetic, where numbers are finite.
4288      We can therefore make the transformation of any unbounded range with
4289      the value Z, Z being greater than any representable number. This permits
4290      us to treat unbounded ranges as equal.  */
4291   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4292   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4293   switch (code)
4294     {
4295     case EQ_EXPR:
4296       result = sgn0 == sgn1;
4297       break;
4298     case NE_EXPR:
4299       result = sgn0 != sgn1;
4300       break;
4301     case LT_EXPR:
4302       result = sgn0 < sgn1;
4303       break;
4304     case LE_EXPR:
4305       result = sgn0 <= sgn1;
4306       break;
4307     case GT_EXPR:
4308       result = sgn0 > sgn1;
4309       break;
4310     case GE_EXPR:
4311       result = sgn0 >= sgn1;
4312       break;
4313     default:
4314       gcc_unreachable ();
4315     }
4316
4317   return constant_boolean_node (result, type);
4318 }
4319 \f
4320 /* Given EXP, a logical expression, set the range it is testing into
4321    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4322    actually being tested.  *PLOW and *PHIGH will be made of the same
4323    type as the returned expression.  If EXP is not a comparison, we
4324    will most likely not be returning a useful value and range.  Set
4325    *STRICT_OVERFLOW_P to true if the return value is only valid
4326    because signed overflow is undefined; otherwise, do not change
4327    *STRICT_OVERFLOW_P.  */
4328
4329 static tree
4330 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4331             bool *strict_overflow_p)
4332 {
4333   enum tree_code code;
4334   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4335   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4336   int in_p, n_in_p;
4337   tree low, high, n_low, n_high;
4338
4339   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4340      and see if we can refine the range.  Some of the cases below may not
4341      happen, but it doesn't seem worth worrying about this.  We "continue"
4342      the outer loop when we've changed something; otherwise we "break"
4343      the switch, which will "break" the while.  */
4344
4345   in_p = 0;
4346   low = high = build_int_cst (TREE_TYPE (exp), 0);
4347
4348   while (1)
4349     {
4350       code = TREE_CODE (exp);
4351       exp_type = TREE_TYPE (exp);
4352
4353       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4354         {
4355           if (TREE_OPERAND_LENGTH (exp) > 0)
4356             arg0 = TREE_OPERAND (exp, 0);
4357           if (TREE_CODE_CLASS (code) == tcc_comparison
4358               || TREE_CODE_CLASS (code) == tcc_unary
4359               || TREE_CODE_CLASS (code) == tcc_binary)
4360             arg0_type = TREE_TYPE (arg0);
4361           if (TREE_CODE_CLASS (code) == tcc_binary
4362               || TREE_CODE_CLASS (code) == tcc_comparison
4363               || (TREE_CODE_CLASS (code) == tcc_expression
4364                   && TREE_OPERAND_LENGTH (exp) > 1))
4365             arg1 = TREE_OPERAND (exp, 1);
4366         }
4367
4368       switch (code)
4369         {
4370         case TRUTH_NOT_EXPR:
4371           in_p = ! in_p, exp = arg0;
4372           continue;
4373
4374         case EQ_EXPR: case NE_EXPR:
4375         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4376           /* We can only do something if the range is testing for zero
4377              and if the second operand is an integer constant.  Note that
4378              saying something is "in" the range we make is done by
4379              complementing IN_P since it will set in the initial case of
4380              being not equal to zero; "out" is leaving it alone.  */
4381           if (low == 0 || high == 0
4382               || ! integer_zerop (low) || ! integer_zerop (high)
4383               || TREE_CODE (arg1) != INTEGER_CST)
4384             break;
4385
4386           switch (code)
4387             {
4388             case NE_EXPR:  /* - [c, c]  */
4389               low = high = arg1;
4390               break;
4391             case EQ_EXPR:  /* + [c, c]  */
4392               in_p = ! in_p, low = high = arg1;
4393               break;
4394             case GT_EXPR:  /* - [-, c] */
4395               low = 0, high = arg1;
4396               break;
4397             case GE_EXPR:  /* + [c, -] */
4398               in_p = ! in_p, low = arg1, high = 0;
4399               break;
4400             case LT_EXPR:  /* - [c, -] */
4401               low = arg1, high = 0;
4402               break;
4403             case LE_EXPR:  /* + [-, c] */
4404               in_p = ! in_p, low = 0, high = arg1;
4405               break;
4406             default:
4407               gcc_unreachable ();
4408             }
4409
4410           /* If this is an unsigned comparison, we also know that EXP is
4411              greater than or equal to zero.  We base the range tests we make
4412              on that fact, so we record it here so we can parse existing
4413              range tests.  We test arg0_type since often the return type
4414              of, e.g. EQ_EXPR, is boolean.  */
4415           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4416             {
4417               if (! merge_ranges (&n_in_p, &n_low, &n_high,
4418                                   in_p, low, high, 1,
4419                                   build_int_cst (arg0_type, 0),
4420                                   NULL_TREE))
4421                 break;
4422
4423               in_p = n_in_p, low = n_low, high = n_high;
4424
4425               /* If the high bound is missing, but we have a nonzero low
4426                  bound, reverse the range so it goes from zero to the low bound
4427                  minus 1.  */
4428               if (high == 0 && low && ! integer_zerop (low))
4429                 {
4430                   in_p = ! in_p;
4431                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4432                                       integer_one_node, 0);
4433                   low = build_int_cst (arg0_type, 0);
4434                 }
4435             }
4436
4437           exp = arg0;
4438           continue;
4439
4440         case NEGATE_EXPR:
4441           /* (-x) IN [a,b] -> x in [-b, -a]  */
4442           n_low = range_binop (MINUS_EXPR, exp_type,
4443                                build_int_cst (exp_type, 0),
4444                                0, high, 1);
4445           n_high = range_binop (MINUS_EXPR, exp_type,
4446                                 build_int_cst (exp_type, 0),
4447                                 0, low, 0);
4448           low = n_low, high = n_high;
4449           exp = arg0;
4450           continue;
4451
4452         case BIT_NOT_EXPR:
4453           /* ~ X -> -X - 1  */
4454           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4455                         build_int_cst (exp_type, 1));
4456           continue;
4457
4458         case PLUS_EXPR:  case MINUS_EXPR:
4459           if (TREE_CODE (arg1) != INTEGER_CST)
4460             break;
4461
4462           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4463              move a constant to the other side.  */
4464           if (!TYPE_UNSIGNED (arg0_type)
4465               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4466             break;
4467
4468           /* If EXP is signed, any overflow in the computation is undefined,
4469              so we don't worry about it so long as our computations on
4470              the bounds don't overflow.  For unsigned, overflow is defined
4471              and this is exactly the right thing.  */
4472           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4473                                arg0_type, low, 0, arg1, 0);
4474           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4475                                 arg0_type, high, 1, arg1, 0);
4476           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4477               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4478             break;
4479
4480           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4481             *strict_overflow_p = true;
4482
4483           /* Check for an unsigned range which has wrapped around the maximum
4484              value thus making n_high < n_low, and normalize it.  */
4485           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4486             {
4487               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4488                                  integer_one_node, 0);
4489               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4490                                   integer_one_node, 0);
4491
4492               /* If the range is of the form +/- [ x+1, x ], we won't
4493                  be able to normalize it.  But then, it represents the
4494                  whole range or the empty set, so make it
4495                  +/- [ -, - ].  */
4496               if (tree_int_cst_equal (n_low, low)
4497                   && tree_int_cst_equal (n_high, high))
4498                 low = high = 0;
4499               else
4500                 in_p = ! in_p;
4501             }
4502           else
4503             low = n_low, high = n_high;
4504
4505           exp = arg0;
4506           continue;
4507
4508         case NOP_EXPR:  case NON_LVALUE_EXPR:  case CONVERT_EXPR:
4509           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4510             break;
4511
4512           if (! INTEGRAL_TYPE_P (arg0_type)
4513               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4514               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4515             break;
4516
4517           n_low = low, n_high = high;
4518
4519           if (n_low != 0)
4520             n_low = fold_convert (arg0_type, n_low);
4521
4522           if (n_high != 0)
4523             n_high = fold_convert (arg0_type, n_high);
4524
4525
4526           /* If we're converting arg0 from an unsigned type, to exp,
4527              a signed type,  we will be doing the comparison as unsigned.
4528              The tests above have already verified that LOW and HIGH
4529              are both positive.
4530
4531              So we have to ensure that we will handle large unsigned
4532              values the same way that the current signed bounds treat
4533              negative values.  */
4534
4535           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4536             {
4537               tree high_positive;
4538               tree equiv_type;
4539               /* For fixed-point modes, we need to pass the saturating flag
4540                  as the 2nd parameter.  */
4541               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4542                 equiv_type = lang_hooks.types.type_for_mode
4543                              (TYPE_MODE (arg0_type),
4544                               TYPE_SATURATING (arg0_type));
4545               else
4546                 equiv_type = lang_hooks.types.type_for_mode
4547                              (TYPE_MODE (arg0_type), 1);
4548
4549               /* A range without an upper bound is, naturally, unbounded.
4550                  Since convert would have cropped a very large value, use
4551                  the max value for the destination type.  */
4552               high_positive
4553                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4554                 : TYPE_MAX_VALUE (arg0_type);
4555
4556               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4557                 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4558                                              fold_convert (arg0_type,
4559                                                            high_positive),
4560                                              build_int_cst (arg0_type, 1));
4561
4562               /* If the low bound is specified, "and" the range with the
4563                  range for which the original unsigned value will be
4564                  positive.  */
4565               if (low != 0)
4566                 {
4567                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4568                                       1, n_low, n_high, 1,
4569                                       fold_convert (arg0_type,
4570                                                     integer_zero_node),
4571                                       high_positive))
4572                     break;
4573
4574                   in_p = (n_in_p == in_p);
4575                 }
4576               else
4577                 {
4578                   /* Otherwise, "or" the range with the range of the input
4579                      that will be interpreted as negative.  */
4580                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4581                                       0, n_low, n_high, 1,
4582                                       fold_convert (arg0_type,
4583                                                     integer_zero_node),
4584                                       high_positive))
4585                     break;
4586
4587                   in_p = (in_p != n_in_p);
4588                 }
4589             }
4590
4591           exp = arg0;
4592           low = n_low, high = n_high;
4593           continue;
4594
4595         default:
4596           break;
4597         }
4598
4599       break;
4600     }
4601
4602   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4603   if (TREE_CODE (exp) == INTEGER_CST)
4604     {
4605       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4606                                                  exp, 0, low, 0))
4607                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4608                                                     exp, 1, high, 1)));
4609       low = high = 0;
4610       exp = 0;
4611     }
4612
4613   *pin_p = in_p, *plow = low, *phigh = high;
4614   return exp;
4615 }
4616 \f
4617 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4618    type, TYPE, return an expression to test if EXP is in (or out of, depending
4619    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4620
4621 static tree
4622 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4623 {
4624   tree etype = TREE_TYPE (exp);
4625   tree value;
4626
4627 #ifdef HAVE_canonicalize_funcptr_for_compare
4628   /* Disable this optimization for function pointer expressions
4629      on targets that require function pointer canonicalization.  */
4630   if (HAVE_canonicalize_funcptr_for_compare
4631       && TREE_CODE (etype) == POINTER_TYPE
4632       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4633     return NULL_TREE;
4634 #endif
4635
4636   if (! in_p)
4637     {
4638       value = build_range_check (type, exp, 1, low, high);
4639       if (value != 0)
4640         return invert_truthvalue (value);
4641
4642       return 0;
4643     }
4644
4645   if (low == 0 && high == 0)
4646     return build_int_cst (type, 1);
4647
4648   if (low == 0)
4649     return fold_build2 (LE_EXPR, type, exp,
4650                         fold_convert (etype, high));
4651
4652   if (high == 0)
4653     return fold_build2 (GE_EXPR, type, exp,
4654                         fold_convert (etype, low));
4655
4656   if (operand_equal_p (low, high, 0))
4657     return fold_build2 (EQ_EXPR, type, exp,
4658                         fold_convert (etype, low));
4659
4660   if (integer_zerop (low))
4661     {
4662       if (! TYPE_UNSIGNED (etype))
4663         {
4664           etype = unsigned_type_for (etype);
4665           high = fold_convert (etype, high);
4666           exp = fold_convert (etype, exp);
4667         }
4668       return build_range_check (type, exp, 1, 0, high);
4669     }
4670
4671   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4672   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4673     {
4674       unsigned HOST_WIDE_INT lo;
4675       HOST_WIDE_INT hi;
4676       int prec;
4677
4678       prec = TYPE_PRECISION (etype);
4679       if (prec <= HOST_BITS_PER_WIDE_INT)
4680         {
4681           hi = 0;
4682           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4683         }
4684       else
4685         {
4686           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4687           lo = (unsigned HOST_WIDE_INT) -1;
4688         }
4689
4690       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4691         {
4692           if (TYPE_UNSIGNED (etype))
4693             {
4694               etype = signed_type_for (etype);
4695               exp = fold_convert (etype, exp);
4696             }
4697           return fold_build2 (GT_EXPR, type, exp,
4698                               build_int_cst (etype, 0));
4699         }
4700     }
4701
4702   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4703      This requires wrap-around arithmetics for the type of the expression.  */
4704   switch (TREE_CODE (etype))
4705     {
4706     case INTEGER_TYPE:
4707       /* There is no requirement that LOW be within the range of ETYPE
4708          if the latter is a subtype.  It must, however, be within the base
4709          type of ETYPE.  So be sure we do the subtraction in that type.  */
4710       if (TREE_TYPE (etype))
4711         etype = TREE_TYPE (etype);
4712       break;
4713
4714     case ENUMERAL_TYPE:
4715     case BOOLEAN_TYPE:
4716       etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4717                                               TYPE_UNSIGNED (etype));
4718       break;
4719
4720     default:
4721       break;
4722     }
4723
4724   /* If we don't have wrap-around arithmetics upfront, try to force it.  */
4725   if (TREE_CODE (etype) == INTEGER_TYPE
4726       && !TYPE_OVERFLOW_WRAPS (etype))
4727     {
4728       tree utype, minv, maxv;
4729
4730       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4731          for the type in question, as we rely on this here.  */
4732       utype = unsigned_type_for (etype);
4733       maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4734       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4735                           integer_one_node, 1);
4736       minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4737
4738       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4739                                       minv, 1, maxv, 1)))
4740         etype = utype;
4741       else
4742         return 0;
4743     }
4744
4745   high = fold_convert (etype, high);
4746   low = fold_convert (etype, low);
4747   exp = fold_convert (etype, exp);
4748
4749   value = const_binop (MINUS_EXPR, high, low, 0);
4750
4751
4752   if (POINTER_TYPE_P (etype))
4753     {
4754       if (value != 0 && !TREE_OVERFLOW (value))
4755         {
4756           low = fold_convert (sizetype, low);
4757           low = fold_build1 (NEGATE_EXPR, sizetype, low);
4758           return build_range_check (type,
4759                                     fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4760                                     1, build_int_cst (etype, 0), value);
4761         }
4762       return 0;
4763     }
4764
4765   if (value != 0 && !TREE_OVERFLOW (value))
4766     return build_range_check (type,
4767                               fold_build2 (MINUS_EXPR, etype, exp, low),
4768                               1, build_int_cst (etype, 0), value);
4769
4770   return 0;
4771 }
4772 \f
4773 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4774
4775 static tree
4776 range_predecessor (tree val)
4777 {
4778   tree type = TREE_TYPE (val);
4779
4780   if (INTEGRAL_TYPE_P (type)
4781       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4782     return 0;
4783   else
4784     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4785 }
4786
4787 /* Return the successor of VAL in its type, handling the infinite case.  */
4788
4789 static tree
4790 range_successor (tree val)
4791 {
4792   tree type = TREE_TYPE (val);
4793
4794   if (INTEGRAL_TYPE_P (type)
4795       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4796     return 0;
4797   else
4798     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4799 }
4800
4801 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4802    can, 0 if we can't.  Set the output range into the specified parameters.  */
4803
4804 static int
4805 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4806               tree high0, int in1_p, tree low1, tree high1)
4807 {
4808   int no_overlap;
4809   int subset;
4810   int temp;
4811   tree tem;
4812   int in_p;
4813   tree low, high;
4814   int lowequal = ((low0 == 0 && low1 == 0)
4815                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4816                                                 low0, 0, low1, 0)));
4817   int highequal = ((high0 == 0 && high1 == 0)
4818                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4819                                                  high0, 1, high1, 1)));
4820
4821   /* Make range 0 be the range that starts first, or ends last if they
4822      start at the same value.  Swap them if it isn't.  */
4823   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4824                                  low0, 0, low1, 0))
4825       || (lowequal
4826           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4827                                         high1, 1, high0, 1))))
4828     {
4829       temp = in0_p, in0_p = in1_p, in1_p = temp;
4830       tem = low0, low0 = low1, low1 = tem;
4831       tem = high0, high0 = high1, high1 = tem;
4832     }
4833
4834   /* Now flag two cases, whether the ranges are disjoint or whether the
4835      second range is totally subsumed in the first.  Note that the tests
4836      below are simplified by the ones above.  */
4837   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4838                                           high0, 1, low1, 0));
4839   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4840                                       high1, 1, high0, 1));
4841
4842   /* We now have four cases, depending on whether we are including or
4843      excluding the two ranges.  */
4844   if (in0_p && in1_p)
4845     {
4846       /* If they don't overlap, the result is false.  If the second range
4847          is a subset it is the result.  Otherwise, the range is from the start
4848          of the second to the end of the first.  */
4849       if (no_overlap)
4850         in_p = 0, low = high = 0;
4851       else if (subset)
4852         in_p = 1, low = low1, high = high1;
4853       else
4854         in_p = 1, low = low1, high = high0;
4855     }
4856
4857   else if (in0_p && ! in1_p)
4858     {
4859       /* If they don't overlap, the result is the first range.  If they are
4860          equal, the result is false.  If the second range is a subset of the
4861          first, and the ranges begin at the same place, we go from just after
4862          the end of the second range to the end of the first.  If the second
4863          range is not a subset of the first, or if it is a subset and both
4864          ranges end at the same place, the range starts at the start of the
4865          first range and ends just before the second range.
4866          Otherwise, we can't describe this as a single range.  */
4867       if (no_overlap)
4868         in_p = 1, low = low0, high = high0;
4869       else if (lowequal && highequal)
4870         in_p = 0, low = high = 0;
4871       else if (subset && lowequal)
4872         {
4873           low = range_successor (high1);
4874           high = high0;
4875           in_p = 1;
4876           if (low == 0)
4877             {
4878               /* We are in the weird situation where high0 > high1 but
4879                  high1 has no successor.  Punt.  */
4880               return 0;
4881             }
4882         }
4883       else if (! subset || highequal)
4884         {
4885           low = low0;
4886           high = range_predecessor (low1);
4887           in_p = 1;
4888           if (high == 0)
4889             {
4890               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4891               return 0;
4892             }
4893         }
4894       else
4895         return 0;
4896     }
4897
4898   else if (! in0_p && in1_p)
4899     {
4900       /* If they don't overlap, the result is the second range.  If the second
4901          is a subset of the first, the result is false.  Otherwise,
4902          the range starts just after the first range and ends at the
4903          end of the second.  */
4904       if (no_overlap)
4905         in_p = 1, low = low1, high = high1;
4906       else if (subset || highequal)
4907         in_p = 0, low = high = 0;
4908       else
4909         {
4910           low = range_successor (high0);
4911           high = high1;
4912           in_p = 1;
4913           if (low == 0)
4914             {
4915               /* high1 > high0 but high0 has no successor.  Punt.  */
4916               return 0;
4917             }
4918         }
4919     }
4920
4921   else
4922     {
4923       /* The case where we are excluding both ranges.  Here the complex case
4924          is if they don't overlap.  In that case, the only time we have a
4925          range is if they are adjacent.  If the second is a subset of the
4926          first, the result is the first.  Otherwise, the range to exclude
4927          starts at the beginning of the first range and ends at the end of the
4928          second.  */
4929       if (no_overlap)
4930         {
4931           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4932                                          range_successor (high0),
4933                                          1, low1, 0)))
4934             in_p = 0, low = low0, high = high1;
4935           else
4936             {
4937               /* Canonicalize - [min, x] into - [-, x].  */
4938               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4939                 switch (TREE_CODE (TREE_TYPE (low0)))
4940                   {
4941                   case ENUMERAL_TYPE:
4942                     if (TYPE_PRECISION (TREE_TYPE (low0))
4943                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4944                       break;
4945                     /* FALLTHROUGH */
4946                   case INTEGER_TYPE:
4947                     if (tree_int_cst_equal (low0,
4948                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4949                       low0 = 0;
4950                     break;
4951                   case POINTER_TYPE:
4952                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4953                         && integer_zerop (low0))
4954                       low0 = 0;
4955                     break;
4956                   default:
4957                     break;
4958                   }
4959
4960               /* Canonicalize - [x, max] into - [x, -].  */
4961               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4962                 switch (TREE_CODE (TREE_TYPE (high1)))
4963                   {
4964                   case ENUMERAL_TYPE:
4965                     if (TYPE_PRECISION (TREE_TYPE (high1))
4966                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4967                       break;
4968                     /* FALLTHROUGH */
4969                   case INTEGER_TYPE:
4970                     if (tree_int_cst_equal (high1,
4971                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4972                       high1 = 0;
4973                     break;
4974                   case POINTER_TYPE:
4975                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4976                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4977                                                        high1, 1,
4978                                                        integer_one_node, 1)))
4979                       high1 = 0;
4980                     break;
4981                   default:
4982                     break;
4983                   }
4984
4985               /* The ranges might be also adjacent between the maximum and
4986                  minimum values of the given type.  For
4987                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4988                  return + [x + 1, y - 1].  */
4989               if (low0 == 0 && high1 == 0)
4990                 {
4991                   low = range_successor (high0);
4992                   high = range_predecessor (low1);
4993                   if (low == 0 || high == 0)
4994                     return 0;
4995
4996                   in_p = 1;
4997                 }
4998               else
4999                 return 0;
5000             }
5001         }
5002       else if (subset)
5003         in_p = 0, low = low0, high = high0;
5004       else
5005         in_p = 0, low = low0, high = high1;
5006     }
5007
5008   *pin_p = in_p, *plow = low, *phigh = high;
5009   return 1;
5010 }
5011 \f
5012
5013 /* Subroutine of fold, looking inside expressions of the form
5014    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5015    of the COND_EXPR.  This function is being used also to optimize
5016    A op B ? C : A, by reversing the comparison first.
5017
5018    Return a folded expression whose code is not a COND_EXPR
5019    anymore, or NULL_TREE if no folding opportunity is found.  */
5020
5021 static tree
5022 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
5023 {
5024   enum tree_code comp_code = TREE_CODE (arg0);
5025   tree arg00 = TREE_OPERAND (arg0, 0);
5026   tree arg01 = TREE_OPERAND (arg0, 1);
5027   tree arg1_type = TREE_TYPE (arg1);
5028   tree tem;
5029
5030   STRIP_NOPS (arg1);
5031   STRIP_NOPS (arg2);
5032
5033   /* If we have A op 0 ? A : -A, consider applying the following
5034      transformations:
5035
5036      A == 0? A : -A    same as -A
5037      A != 0? A : -A    same as A
5038      A >= 0? A : -A    same as abs (A)
5039      A > 0?  A : -A    same as abs (A)
5040      A <= 0? A : -A    same as -abs (A)
5041      A < 0?  A : -A    same as -abs (A)
5042
5043      None of these transformations work for modes with signed
5044      zeros.  If A is +/-0, the first two transformations will
5045      change the sign of the result (from +0 to -0, or vice
5046      versa).  The last four will fix the sign of the result,
5047      even though the original expressions could be positive or
5048      negative, depending on the sign of A.
5049
5050      Note that all these transformations are correct if A is
5051      NaN, since the two alternatives (A and -A) are also NaNs.  */
5052   if ((FLOAT_TYPE_P (TREE_TYPE (arg01))
5053        ? real_zerop (arg01)
5054        : integer_zerop (arg01))
5055       && ((TREE_CODE (arg2) == NEGATE_EXPR
5056            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5057              /* In the case that A is of the form X-Y, '-A' (arg2) may
5058                 have already been folded to Y-X, check for that. */
5059           || (TREE_CODE (arg1) == MINUS_EXPR
5060               && TREE_CODE (arg2) == MINUS_EXPR
5061               && operand_equal_p (TREE_OPERAND (arg1, 0),
5062                                   TREE_OPERAND (arg2, 1), 0)
5063               && operand_equal_p (TREE_OPERAND (arg1, 1),
5064                                   TREE_OPERAND (arg2, 0), 0))))
5065     switch (comp_code)
5066       {
5067       case EQ_EXPR:
5068       case UNEQ_EXPR:
5069         tem = fold_convert (arg1_type, arg1);
5070         return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
5071       case NE_EXPR:
5072       case LTGT_EXPR:
5073         return pedantic_non_lvalue (fold_convert (type, arg1));
5074       case UNGE_EXPR:
5075       case UNGT_EXPR:
5076         if (flag_trapping_math)
5077           break;
5078         /* Fall through.  */
5079       case GE_EXPR:
5080       case GT_EXPR:
5081         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5082           arg1 = fold_convert (signed_type_for
5083                                (TREE_TYPE (arg1)), arg1);
5084         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5085         return pedantic_non_lvalue (fold_convert (type, tem));
5086       case UNLE_EXPR:
5087       case UNLT_EXPR:
5088         if (flag_trapping_math)
5089           break;
5090       case LE_EXPR:
5091       case LT_EXPR:
5092         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5093           arg1 = fold_convert (signed_type_for
5094                                (TREE_TYPE (arg1)), arg1);
5095         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5096         return negate_expr (fold_convert (type, tem));
5097       default:
5098         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5099         break;
5100       }
5101
5102   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
5103      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
5104      both transformations are correct when A is NaN: A != 0
5105      is then true, and A == 0 is false.  */
5106
5107   if (integer_zerop (arg01) && integer_zerop (arg2))
5108     {
5109       if (comp_code == NE_EXPR)
5110         return pedantic_non_lvalue (fold_convert (type, arg1));
5111       else if (comp_code == EQ_EXPR)
5112         return build_int_cst (type, 0);
5113     }
5114
5115   /* Try some transformations of A op B ? A : B.
5116
5117      A == B? A : B    same as B
5118      A != B? A : B    same as A
5119      A >= B? A : B    same as max (A, B)
5120      A > B?  A : B    same as max (B, A)
5121      A <= B? A : B    same as min (A, B)
5122      A < B?  A : B    same as min (B, A)
5123
5124      As above, these transformations don't work in the presence
5125      of signed zeros.  For example, if A and B are zeros of
5126      opposite sign, the first two transformations will change
5127      the sign of the result.  In the last four, the original
5128      expressions give different results for (A=+0, B=-0) and
5129      (A=-0, B=+0), but the transformed expressions do not.
5130
5131      The first two transformations are correct if either A or B
5132      is a NaN.  In the first transformation, the condition will
5133      be false, and B will indeed be chosen.  In the case of the
5134      second transformation, the condition A != B will be true,
5135      and A will be chosen.
5136
5137      The conversions to max() and min() are not correct if B is
5138      a number and A is not.  The conditions in the original
5139      expressions will be false, so all four give B.  The min()
5140      and max() versions would give a NaN instead.  */
5141   if (operand_equal_for_comparison_p (arg01, arg2, arg00)
5142       /* Avoid these transformations if the COND_EXPR may be used
5143          as an lvalue in the C++ front-end.  PR c++/19199.  */
5144       && (in_gimple_form
5145           || (strcmp (lang_hooks.name, "GNU C++") != 0
5146               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5147           || ! maybe_lvalue_p (arg1)
5148           || ! maybe_lvalue_p (arg2)))
5149     {
5150       tree comp_op0 = arg00;
5151       tree comp_op1 = arg01;
5152       tree comp_type = TREE_TYPE (comp_op0);
5153
5154       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
5155       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5156         {
5157           comp_type = type;
5158           comp_op0 = arg1;
5159           comp_op1 = arg2;
5160         }
5161
5162       switch (comp_code)
5163         {
5164         case EQ_EXPR:
5165           return pedantic_non_lvalue (fold_convert (type, arg2));
5166         case NE_EXPR:
5167           return pedantic_non_lvalue (fold_convert (type, arg1));
5168         case LE_EXPR:
5169         case LT_EXPR:
5170         case UNLE_EXPR:
5171         case UNLT_EXPR:
5172           /* In C++ a ?: expression can be an lvalue, so put the
5173              operand which will be used if they are equal first
5174              so that we can convert this back to the
5175              corresponding COND_EXPR.  */
5176           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5177             {
5178               comp_op0 = fold_convert (comp_type, comp_op0);
5179               comp_op1 = fold_convert (comp_type, comp_op1);
5180               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5181                     ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
5182                     : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
5183               return pedantic_non_lvalue (fold_convert (type, tem));
5184             }
5185           break;
5186         case GE_EXPR:
5187         case GT_EXPR:
5188         case UNGE_EXPR:
5189         case UNGT_EXPR:
5190           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5191             {
5192               comp_op0 = fold_convert (comp_type, comp_op0);
5193               comp_op1 = fold_convert (comp_type, comp_op1);
5194               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5195                     ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5196                     : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5197               return pedantic_non_lvalue (fold_convert (type, tem));
5198             }
5199           break;
5200         case UNEQ_EXPR:
5201           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5202             return pedantic_non_lvalue (fold_convert (type, arg2));
5203           break;
5204         case LTGT_EXPR:
5205           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5206             return pedantic_non_lvalue (fold_convert (type, arg1));
5207           break;
5208         default:
5209           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5210           break;
5211         }
5212     }
5213
5214   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5215      we might still be able to simplify this.  For example,
5216      if C1 is one less or one more than C2, this might have started
5217      out as a MIN or MAX and been transformed by this function.
5218      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
5219
5220   if (INTEGRAL_TYPE_P (type)
5221       && TREE_CODE (arg01) == INTEGER_CST
5222       && TREE_CODE (arg2) == INTEGER_CST)
5223     switch (comp_code)
5224       {
5225       case EQ_EXPR:
5226         /* We can replace A with C1 in this case.  */
5227         arg1 = fold_convert (type, arg01);
5228         return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5229
5230       case LT_EXPR:
5231         /* If C1 is C2 + 1, this is min(A, C2).  */
5232         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5233                                OEP_ONLY_CONST)
5234             && operand_equal_p (arg01,
5235                                 const_binop (PLUS_EXPR, arg2,
5236                                              build_int_cst (type, 1), 0),
5237                                 OEP_ONLY_CONST))
5238           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5239                                                    type,
5240                                                    fold_convert (type, arg1),
5241                                                    arg2));
5242         break;
5243
5244       case LE_EXPR:
5245         /* If C1 is C2 - 1, this is min(A, C2).  */
5246         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5247                                OEP_ONLY_CONST)
5248             && operand_equal_p (arg01,
5249                                 const_binop (MINUS_EXPR, arg2,
5250                                              build_int_cst (type, 1), 0),
5251                                 OEP_ONLY_CONST))
5252           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5253                                                    type,
5254                                                    fold_convert (type, arg1),
5255                                                    arg2));
5256         break;
5257
5258       case GT_EXPR:
5259         /* If C1 is C2 - 1, this is max(A, C2).  */
5260         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5261                                OEP_ONLY_CONST)
5262             && operand_equal_p (arg01,
5263                                 const_binop (MINUS_EXPR, arg2,
5264                                              build_int_cst (type, 1), 0),
5265                                 OEP_ONLY_CONST))
5266           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5267                                                    type,
5268                                                    fold_convert (type, arg1),
5269                                                    arg2));
5270         break;
5271
5272       case GE_EXPR:
5273         /* If C1 is C2 + 1, this is max(A, C2).  */
5274         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5275                                OEP_ONLY_CONST)
5276             && operand_equal_p (arg01,
5277                                 const_binop (PLUS_EXPR, arg2,
5278                                              build_int_cst (type, 1), 0),
5279                                 OEP_ONLY_CONST))
5280           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5281                                                    type,
5282                                                    fold_convert (type, arg1),
5283                                                    arg2));
5284         break;
5285       case NE_EXPR:
5286         break;
5287       default:
5288         gcc_unreachable ();
5289       }
5290
5291   return NULL_TREE;
5292 }
5293
5294
5295 \f
5296 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5297 #define LOGICAL_OP_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
5298 #endif
5299
5300 /* EXP is some logical combination of boolean tests.  See if we can
5301    merge it into some range test.  Return the new tree if so.  */
5302
5303 static tree
5304 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5305 {
5306   int or_op = (code == TRUTH_ORIF_EXPR
5307                || code == TRUTH_OR_EXPR);
5308   int in0_p, in1_p, in_p;
5309   tree low0, low1, low, high0, high1, high;
5310   bool strict_overflow_p = false;
5311   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5312   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5313   tree tem;
5314   const char * const warnmsg = G_("assuming signed overflow does not occur "
5315                                   "when simplifying range test");
5316
5317   /* If this is an OR operation, invert both sides; we will invert
5318      again at the end.  */
5319   if (or_op)
5320     in0_p = ! in0_p, in1_p = ! in1_p;
5321
5322   /* If both expressions are the same, if we can merge the ranges, and we
5323      can build the range test, return it or it inverted.  If one of the
5324      ranges is always true or always false, consider it to be the same
5325      expression as the other.  */
5326   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5327       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5328                        in1_p, low1, high1)
5329       && 0 != (tem = (build_range_check (type,
5330                                          lhs != 0 ? lhs
5331                                          : rhs != 0 ? rhs : integer_zero_node,
5332                                          in_p, low, high))))
5333     {
5334       if (strict_overflow_p)
5335         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5336       return or_op ? invert_truthvalue (tem) : tem;
5337     }
5338
5339   /* On machines where the branch cost is expensive, if this is a
5340      short-circuited branch and the underlying object on both sides
5341      is the same, make a non-short-circuit operation.  */
5342   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5343            && lhs != 0 && rhs != 0
5344            && (code == TRUTH_ANDIF_EXPR
5345                || code == TRUTH_ORIF_EXPR)
5346            && operand_equal_p (lhs, rhs, 0))
5347     {
5348       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
5349          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5350          which cases we can't do this.  */
5351       if (simple_operand_p (lhs))
5352         return build2 (code == TRUTH_ANDIF_EXPR
5353                        ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5354                        type, op0, op1);
5355
5356       else if (lang_hooks.decls.global_bindings_p () == 0
5357                && ! CONTAINS_PLACEHOLDER_P (lhs))
5358         {
5359           tree common = save_expr (lhs);
5360
5361           if (0 != (lhs = build_range_check (type, common,
5362                                              or_op ? ! in0_p : in0_p,
5363                                              low0, high0))
5364               && (0 != (rhs = build_range_check (type, common,
5365                                                  or_op ? ! in1_p : in1_p,
5366                                                  low1, high1))))
5367             {
5368               if (strict_overflow_p)
5369                 fold_overflow_warning (warnmsg,
5370                                        WARN_STRICT_OVERFLOW_COMPARISON);
5371               return build2 (code == TRUTH_ANDIF_EXPR
5372                              ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5373                              type, lhs, rhs);
5374             }
5375         }
5376     }
5377
5378   return 0;
5379 }
5380 \f
5381 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5382    bit value.  Arrange things so the extra bits will be set to zero if and
5383    only if C is signed-extended to its full width.  If MASK is nonzero,
5384    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
5385
5386 static tree
5387 unextend (tree c, int p, int unsignedp, tree mask)
5388 {
5389   tree type = TREE_TYPE (c);
5390   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5391   tree temp;
5392
5393   if (p == modesize || unsignedp)
5394     return c;
5395
5396   /* We work by getting just the sign bit into the low-order bit, then
5397      into the high-order bit, then sign-extend.  We then XOR that value
5398      with C.  */
5399   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5400   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5401
5402   /* We must use a signed type in order to get an arithmetic right shift.
5403      However, we must also avoid introducing accidental overflows, so that
5404      a subsequent call to integer_zerop will work.  Hence we must
5405      do the type conversion here.  At this point, the constant is either
5406      zero or one, and the conversion to a signed type can never overflow.
5407      We could get an overflow if this conversion is done anywhere else.  */
5408   if (TYPE_UNSIGNED (type))
5409     temp = fold_convert (signed_type_for (type), temp);
5410
5411   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5412   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5413   if (mask != 0)
5414     temp = const_binop (BIT_AND_EXPR, temp,
5415                         fold_convert (TREE_TYPE (c), mask), 0);
5416   /* If necessary, convert the type back to match the type of C.  */
5417   if (TYPE_UNSIGNED (type))
5418     temp = fold_convert (type, temp);
5419
5420   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5421 }
5422 \f
5423 /* Find ways of folding logical expressions of LHS and RHS:
5424    Try to merge two comparisons to the same innermost item.
5425    Look for range tests like "ch >= '0' && ch <= '9'".
5426    Look for combinations of simple terms on machines with expensive branches
5427    and evaluate the RHS unconditionally.
5428
5429    For example, if we have p->a == 2 && p->b == 4 and we can make an
5430    object large enough to span both A and B, we can do this with a comparison
5431    against the object ANDed with the a mask.
5432
5433    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5434    operations to do this with one comparison.
5435
5436    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5437    function and the one above.
5438
5439    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5440    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5441
5442    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5443    two operands.
5444
5445    We return the simplified tree or 0 if no optimization is possible.  */
5446
5447 static tree
5448 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5449 {
5450   /* If this is the "or" of two comparisons, we can do something if
5451      the comparisons are NE_EXPR.  If this is the "and", we can do something
5452      if the comparisons are EQ_EXPR.  I.e.,
5453         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5454
5455      WANTED_CODE is this operation code.  For single bit fields, we can
5456      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5457      comparison for one-bit fields.  */
5458
5459   enum tree_code wanted_code;
5460   enum tree_code lcode, rcode;
5461   tree ll_arg, lr_arg, rl_arg, rr_arg;
5462   tree ll_inner, lr_inner, rl_inner, rr_inner;
5463   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5464   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5465   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5466   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5467   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5468   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5469   enum machine_mode lnmode, rnmode;
5470   tree ll_mask, lr_mask, rl_mask, rr_mask;
5471   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5472   tree l_const, r_const;
5473   tree lntype, rntype, result;
5474   int first_bit, end_bit;
5475   int volatilep;
5476   tree orig_lhs = lhs, orig_rhs = rhs;
5477   enum tree_code orig_code = code;
5478
5479   /* Start by getting the comparison codes.  Fail if anything is volatile.
5480      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5481      it were surrounded with a NE_EXPR.  */
5482
5483   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5484     return 0;
5485
5486   lcode = TREE_CODE (lhs);
5487   rcode = TREE_CODE (rhs);
5488
5489   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5490     {
5491       lhs = build2 (NE_EXPR, truth_type, lhs,
5492                     build_int_cst (TREE_TYPE (lhs), 0));
5493       lcode = NE_EXPR;
5494     }
5495
5496   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5497     {
5498       rhs = build2 (NE_EXPR, truth_type, rhs,
5499                     build_int_cst (TREE_TYPE (rhs), 0));
5500       rcode = NE_EXPR;
5501     }
5502
5503   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5504       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5505     return 0;
5506
5507   ll_arg = TREE_OPERAND (lhs, 0);
5508   lr_arg = TREE_OPERAND (lhs, 1);
5509   rl_arg = TREE_OPERAND (rhs, 0);
5510   rr_arg = TREE_OPERAND (rhs, 1);
5511
5512   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5513   if (simple_operand_p (ll_arg)
5514       && simple_operand_p (lr_arg))
5515     {
5516       tree result;
5517       if (operand_equal_p (ll_arg, rl_arg, 0)
5518           && operand_equal_p (lr_arg, rr_arg, 0))
5519         {
5520           result = combine_comparisons (code, lcode, rcode,
5521                                         truth_type, ll_arg, lr_arg);
5522           if (result)
5523             return result;
5524         }
5525       else if (operand_equal_p (ll_arg, rr_arg, 0)
5526                && operand_equal_p (lr_arg, rl_arg, 0))
5527         {
5528           result = combine_comparisons (code, lcode,
5529                                         swap_tree_comparison (rcode),
5530                                         truth_type, ll_arg, lr_arg);
5531           if (result)
5532             return result;
5533         }
5534     }
5535
5536   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5537           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5538
5539   /* If the RHS can be evaluated unconditionally and its operands are
5540      simple, it wins to evaluate the RHS unconditionally on machines
5541      with expensive branches.  In this case, this isn't a comparison
5542      that can be merged.  Avoid doing this if the RHS is a floating-point
5543      comparison since those can trap.  */
5544
5545   if (BRANCH_COST >= 2
5546       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5547       && simple_operand_p (rl_arg)
5548       && simple_operand_p (rr_arg))
5549     {
5550       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5551       if (code == TRUTH_OR_EXPR
5552           && lcode == NE_EXPR && integer_zerop (lr_arg)
5553           && rcode == NE_EXPR && integer_zerop (rr_arg)
5554           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
5555         return build2 (NE_EXPR, truth_type,
5556                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5557                                ll_arg, rl_arg),
5558                        build_int_cst (TREE_TYPE (ll_arg), 0));
5559
5560       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5561       if (code == TRUTH_AND_EXPR
5562           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5563           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5564           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
5565         return build2 (EQ_EXPR, truth_type,
5566                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5567                                ll_arg, rl_arg),
5568                        build_int_cst (TREE_TYPE (ll_arg), 0));
5569
5570       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5571         {
5572           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5573             return build2 (code, truth_type, lhs, rhs);
5574           return NULL_TREE;
5575         }
5576     }
5577
5578   /* See if the comparisons can be merged.  Then get all the parameters for
5579      each side.  */
5580
5581   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5582       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5583     return 0;
5584
5585   volatilep = 0;
5586   ll_inner = decode_field_reference (ll_arg,
5587                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5588                                      &ll_unsignedp, &volatilep, &ll_mask,
5589                                      &ll_and_mask);
5590   lr_inner = decode_field_reference (lr_arg,
5591                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5592                                      &lr_unsignedp, &volatilep, &lr_mask,
5593                                      &lr_and_mask);
5594   rl_inner = decode_field_reference (rl_arg,
5595                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5596                                      &rl_unsignedp, &volatilep, &rl_mask,
5597                                      &rl_and_mask);
5598   rr_inner = decode_field_reference (rr_arg,
5599                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5600                                      &rr_unsignedp, &volatilep, &rr_mask,
5601                                      &rr_and_mask);
5602
5603   /* It must be true that the inner operation on the lhs of each
5604      comparison must be the same if we are to be able to do anything.
5605      Then see if we have constants.  If not, the same must be true for
5606      the rhs's.  */
5607   if (volatilep || ll_inner == 0 || rl_inner == 0
5608       || ! operand_equal_p (ll_inner, rl_inner, 0))
5609     return 0;
5610
5611   if (TREE_CODE (lr_arg) == INTEGER_CST
5612       && TREE_CODE (rr_arg) == INTEGER_CST)
5613     l_const = lr_arg, r_const = rr_arg;
5614   else if (lr_inner == 0 || rr_inner == 0
5615            || ! operand_equal_p (lr_inner, rr_inner, 0))
5616     return 0;
5617   else
5618     l_const = r_const = 0;
5619
5620   /* If either comparison code is not correct for our logical operation,
5621      fail.  However, we can convert a one-bit comparison against zero into
5622      the opposite comparison against that bit being set in the field.  */
5623
5624   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5625   if (lcode != wanted_code)
5626     {
5627       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5628         {
5629           /* Make the left operand unsigned, since we are only interested
5630              in the value of one bit.  Otherwise we are doing the wrong
5631              thing below.  */
5632           ll_unsignedp = 1;
5633           l_const = ll_mask;
5634         }
5635       else
5636         return 0;
5637     }
5638
5639   /* This is analogous to the code for l_const above.  */
5640   if (rcode != wanted_code)
5641     {
5642       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5643         {
5644           rl_unsignedp = 1;
5645           r_const = rl_mask;
5646         }
5647       else
5648         return 0;
5649     }
5650
5651   /* See if we can find a mode that contains both fields being compared on
5652      the left.  If we can't, fail.  Otherwise, update all constants and masks
5653      to be relative to a field of that size.  */
5654   first_bit = MIN (ll_bitpos, rl_bitpos);
5655   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5656   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5657                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5658                           volatilep);
5659   if (lnmode == VOIDmode)
5660     return 0;
5661
5662   lnbitsize = GET_MODE_BITSIZE (lnmode);
5663   lnbitpos = first_bit & ~ (lnbitsize - 1);
5664   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5665   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5666
5667   if (BYTES_BIG_ENDIAN)
5668     {
5669       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5670       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5671     }
5672
5673   ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5674                          size_int (xll_bitpos), 0);
5675   rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5676                          size_int (xrl_bitpos), 0);
5677
5678   if (l_const)
5679     {
5680       l_const = fold_convert (lntype, l_const);
5681       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5682       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5683       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5684                                         fold_build1 (BIT_NOT_EXPR,
5685                                                      lntype, ll_mask),
5686                                         0)))
5687         {
5688           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5689
5690           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5691         }
5692     }
5693   if (r_const)
5694     {
5695       r_const = fold_convert (lntype, r_const);
5696       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5697       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5698       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5699                                         fold_build1 (BIT_NOT_EXPR,
5700                                                      lntype, rl_mask),
5701                                         0)))
5702         {
5703           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5704
5705           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5706         }
5707     }
5708
5709   /* If the right sides are not constant, do the same for it.  Also,
5710      disallow this optimization if a size or signedness mismatch occurs
5711      between the left and right sides.  */
5712   if (l_const == 0)
5713     {
5714       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5715           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5716           /* Make sure the two fields on the right
5717              correspond to the left without being swapped.  */
5718           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5719         return 0;
5720
5721       first_bit = MIN (lr_bitpos, rr_bitpos);
5722       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5723       rnmode = get_best_mode (end_bit - first_bit, first_bit,
5724                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5725                               volatilep);
5726       if (rnmode == VOIDmode)
5727         return 0;
5728
5729       rnbitsize = GET_MODE_BITSIZE (rnmode);
5730       rnbitpos = first_bit & ~ (rnbitsize - 1);
5731       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5732       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5733
5734       if (BYTES_BIG_ENDIAN)
5735         {
5736           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5737           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5738         }
5739
5740       lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
5741                              size_int (xlr_bitpos), 0);
5742       rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
5743                              size_int (xrr_bitpos), 0);
5744
5745       /* Make a mask that corresponds to both fields being compared.
5746          Do this for both items being compared.  If the operands are the
5747          same size and the bits being compared are in the same position
5748          then we can do this by masking both and comparing the masked
5749          results.  */
5750       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5751       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5752       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5753         {
5754           lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5755                                     ll_unsignedp || rl_unsignedp);
5756           if (! all_ones_mask_p (ll_mask, lnbitsize))
5757             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5758
5759           rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
5760                                     lr_unsignedp || rr_unsignedp);
5761           if (! all_ones_mask_p (lr_mask, rnbitsize))
5762             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5763
5764           return build2 (wanted_code, truth_type, lhs, rhs);
5765         }
5766
5767       /* There is still another way we can do something:  If both pairs of
5768          fields being compared are adjacent, we may be able to make a wider
5769          field containing them both.
5770
5771          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5772          the mask must be shifted to account for the shift done by
5773          make_bit_field_ref.  */
5774       if ((ll_bitsize + ll_bitpos == rl_bitpos
5775            && lr_bitsize + lr_bitpos == rr_bitpos)
5776           || (ll_bitpos == rl_bitpos + rl_bitsize
5777               && lr_bitpos == rr_bitpos + rr_bitsize))
5778         {
5779           tree type;
5780
5781           lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
5782                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5783           rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
5784                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5785
5786           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5787                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5788           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5789                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5790
5791           /* Convert to the smaller type before masking out unwanted bits.  */
5792           type = lntype;
5793           if (lntype != rntype)
5794             {
5795               if (lnbitsize > rnbitsize)
5796                 {
5797                   lhs = fold_convert (rntype, lhs);
5798                   ll_mask = fold_convert (rntype, ll_mask);
5799                   type = rntype;
5800                 }
5801               else if (lnbitsize < rnbitsize)
5802                 {
5803                   rhs = fold_convert (lntype, rhs);
5804                   lr_mask = fold_convert (lntype, lr_mask);
5805                   type = lntype;
5806                 }
5807             }
5808
5809           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5810             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5811
5812           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5813             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5814
5815           return build2 (wanted_code, truth_type, lhs, rhs);
5816         }
5817
5818       return 0;
5819     }
5820
5821   /* Handle the case of comparisons with constants.  If there is something in
5822      common between the masks, those bits of the constants must be the same.
5823      If not, the condition is always false.  Test for this to avoid generating
5824      incorrect code below.  */
5825   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5826   if (! integer_zerop (result)
5827       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5828                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5829     {
5830       if (wanted_code == NE_EXPR)
5831         {
5832           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5833           return constant_boolean_node (true, truth_type);
5834         }
5835       else
5836         {
5837           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5838           return constant_boolean_node (false, truth_type);
5839         }
5840     }
5841
5842   /* Construct the expression we will return.  First get the component
5843      reference we will make.  Unless the mask is all ones the width of
5844      that field, perform the mask operation.  Then compare with the
5845      merged constant.  */
5846   result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5847                                ll_unsignedp || rl_unsignedp);
5848
5849   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5850   if (! all_ones_mask_p (ll_mask, lnbitsize))
5851     result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5852
5853   return build2 (wanted_code, truth_type, result,
5854                  const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5855 }
5856 \f
5857 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5858    constant.  */
5859
5860 static tree
5861 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5862 {
5863   tree arg0 = op0;
5864   enum tree_code op_code;
5865   tree comp_const = op1;
5866   tree minmax_const;
5867   int consts_equal, consts_lt;
5868   tree inner;
5869
5870   STRIP_SIGN_NOPS (arg0);
5871
5872   op_code = TREE_CODE (arg0);
5873   minmax_const = TREE_OPERAND (arg0, 1);
5874   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5875   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5876   inner = TREE_OPERAND (arg0, 0);
5877
5878   /* If something does not permit us to optimize, return the original tree.  */
5879   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5880       || TREE_CODE (comp_const) != INTEGER_CST
5881       || TREE_OVERFLOW (comp_const)
5882       || TREE_CODE (minmax_const) != INTEGER_CST
5883       || TREE_OVERFLOW (minmax_const))
5884     return NULL_TREE;
5885
5886   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5887      and GT_EXPR, doing the rest with recursive calls using logical
5888      simplifications.  */
5889   switch (code)
5890     {
5891     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5892       {
5893         tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5894                                           type, op0, op1);
5895         if (tem)
5896           return invert_truthvalue (tem);
5897         return NULL_TREE;
5898       }
5899
5900     case GE_EXPR:
5901       return
5902         fold_build2 (TRUTH_ORIF_EXPR, type,
5903                      optimize_minmax_comparison
5904                      (EQ_EXPR, type, arg0, comp_const),
5905                      optimize_minmax_comparison
5906                      (GT_EXPR, type, arg0, comp_const));
5907
5908     case EQ_EXPR:
5909       if (op_code == MAX_EXPR && consts_equal)
5910         /* MAX (X, 0) == 0  ->  X <= 0  */
5911         return fold_build2 (LE_EXPR, type, inner, comp_const);
5912
5913       else if (op_code == MAX_EXPR && consts_lt)
5914         /* MAX (X, 0) == 5  ->  X == 5   */
5915         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5916
5917       else if (op_code == MAX_EXPR)
5918         /* MAX (X, 0) == -1  ->  false  */
5919         return omit_one_operand (type, integer_zero_node, inner);
5920
5921       else if (consts_equal)
5922         /* MIN (X, 0) == 0  ->  X >= 0  */
5923         return fold_build2 (GE_EXPR, type, inner, comp_const);
5924
5925       else if (consts_lt)
5926         /* MIN (X, 0) == 5  ->  false  */
5927         return omit_one_operand (type, integer_zero_node, inner);
5928
5929       else
5930         /* MIN (X, 0) == -1  ->  X == -1  */
5931         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5932
5933     case GT_EXPR:
5934       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5935         /* MAX (X, 0) > 0  ->  X > 0
5936            MAX (X, 0) > 5  ->  X > 5  */
5937         return fold_build2 (GT_EXPR, type, inner, comp_const);
5938
5939       else if (op_code == MAX_EXPR)
5940         /* MAX (X, 0) > -1  ->  true  */
5941         return omit_one_operand (type, integer_one_node, inner);
5942
5943       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5944         /* MIN (X, 0) > 0  ->  false
5945            MIN (X, 0) > 5  ->  false  */
5946         return omit_one_operand (type, integer_zero_node, inner);
5947
5948       else
5949         /* MIN (X, 0) > -1  ->  X > -1  */
5950         return fold_build2 (GT_EXPR, type, inner, comp_const);
5951
5952     default:
5953       return NULL_TREE;
5954     }
5955 }
5956 \f
5957 /* T is an integer expression that is being multiplied, divided, or taken a
5958    modulus (CODE says which and what kind of divide or modulus) by a
5959    constant C.  See if we can eliminate that operation by folding it with
5960    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5961    should be used for the computation if wider than our type.
5962
5963    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5964    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5965    expression would not overflow or that overflow is undefined for the type
5966    in the language in question.
5967
5968    We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
5969    the machine has a multiply-accumulate insn or that this is part of an
5970    addressing calculation.
5971
5972    If we return a non-null expression, it is an equivalent form of the
5973    original computation, but need not be in the original type.
5974
5975    We set *STRICT_OVERFLOW_P to true if the return values depends on
5976    signed overflow being undefined.  Otherwise we do not change
5977    *STRICT_OVERFLOW_P.  */
5978
5979 static tree
5980 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5981                 bool *strict_overflow_p)
5982 {
5983   /* To avoid exponential search depth, refuse to allow recursion past
5984      three levels.  Beyond that (1) it's highly unlikely that we'll find
5985      something interesting and (2) we've probably processed it before
5986      when we built the inner expression.  */
5987
5988   static int depth;
5989   tree ret;
5990
5991   if (depth > 3)
5992     return NULL;
5993
5994   depth++;
5995   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5996   depth--;
5997
5998   return ret;
5999 }
6000
6001 static tree
6002 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6003                   bool *strict_overflow_p)
6004 {
6005   tree type = TREE_TYPE (t);
6006   enum tree_code tcode = TREE_CODE (t);
6007   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6008                                    > GET_MODE_SIZE (TYPE_MODE (type)))
6009                 ? wide_type : type);
6010   tree t1, t2;
6011   int same_p = tcode == code;
6012   tree op0 = NULL_TREE, op1 = NULL_TREE;
6013   bool sub_strict_overflow_p;
6014
6015   /* Don't deal with constants of zero here; they confuse the code below.  */
6016   if (integer_zerop (c))
6017     return NULL_TREE;
6018
6019   if (TREE_CODE_CLASS (tcode) == tcc_unary)
6020     op0 = TREE_OPERAND (t, 0);
6021
6022   if (TREE_CODE_CLASS (tcode) == tcc_binary)
6023     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6024
6025   /* Note that we need not handle conditional operations here since fold
6026      already handles those cases.  So just do arithmetic here.  */
6027   switch (tcode)
6028     {
6029     case INTEGER_CST:
6030       /* For a constant, we can always simplify if we are a multiply
6031          or (for divide and modulus) if it is a multiple of our constant.  */
6032       if (code == MULT_EXPR
6033           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6034         return const_binop (code, fold_convert (ctype, t),
6035                             fold_convert (ctype, c), 0);
6036       break;
6037
6038     case CONVERT_EXPR:  case NON_LVALUE_EXPR:  case NOP_EXPR:
6039       /* If op0 is an expression ...  */
6040       if ((COMPARISON_CLASS_P (op0)
6041            || UNARY_CLASS_P (op0)
6042            || BINARY_CLASS_P (op0)
6043            || VL_EXP_CLASS_P (op0)
6044            || EXPRESSION_CLASS_P (op0))
6045           /* ... and is unsigned, and its type is smaller than ctype,
6046              then we cannot pass through as widening.  */
6047           && ((TYPE_UNSIGNED (TREE_TYPE (op0))
6048                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6049                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6050                && (GET_MODE_SIZE (TYPE_MODE (ctype))
6051                    > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
6052               /* ... or this is a truncation (t is narrower than op0),
6053                  then we cannot pass through this narrowing.  */
6054               || (GET_MODE_SIZE (TYPE_MODE (type))
6055                   < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
6056               /* ... or signedness changes for division or modulus,
6057                  then we cannot pass through this conversion.  */
6058               || (code != MULT_EXPR
6059                   && (TYPE_UNSIGNED (ctype)
6060                       != TYPE_UNSIGNED (TREE_TYPE (op0))))))
6061         break;
6062
6063       /* Pass the constant down and see if we can make a simplification.  If
6064          we can, replace this expression with the inner simplification for
6065          possible later conversion to our or some other type.  */
6066       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6067           && TREE_CODE (t2) == INTEGER_CST
6068           && !TREE_OVERFLOW (t2)
6069           && (0 != (t1 = extract_muldiv (op0, t2, code,
6070                                          code == MULT_EXPR
6071                                          ? ctype : NULL_TREE,
6072                                          strict_overflow_p))))
6073         return t1;
6074       break;
6075
6076     case ABS_EXPR:
6077       /* If widening the type changes it from signed to unsigned, then we
6078          must avoid building ABS_EXPR itself as unsigned.  */
6079       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6080         {
6081           tree cstype = (*signed_type_for) (ctype);
6082           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6083               != 0)
6084             {
6085               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6086               return fold_convert (ctype, t1);
6087             }
6088           break;
6089         }
6090       /* FALLTHROUGH */
6091     case NEGATE_EXPR:
6092       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6093           != 0)
6094         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6095       break;
6096
6097     case MIN_EXPR:  case MAX_EXPR:
6098       /* If widening the type changes the signedness, then we can't perform
6099          this optimization as that changes the result.  */
6100       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6101         break;
6102
6103       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
6104       sub_strict_overflow_p = false;
6105       if ((t1 = extract_muldiv (op0, c, code, wide_type,
6106                                 &sub_strict_overflow_p)) != 0
6107           && (t2 = extract_muldiv (op1, c, code, wide_type,
6108                                    &sub_strict_overflow_p)) != 0)
6109         {
6110           if (tree_int_cst_sgn (c) < 0)
6111             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6112           if (sub_strict_overflow_p)
6113             *strict_overflow_p = true;
6114           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6115                               fold_convert (ctype, t2));
6116         }
6117       break;
6118
6119     case LSHIFT_EXPR:  case RSHIFT_EXPR:
6120       /* If the second operand is constant, this is a multiplication
6121          or floor division, by a power of two, so we can treat it that
6122          way unless the multiplier or divisor overflows.  Signed
6123          left-shift overflow is implementation-defined rather than
6124          undefined in C90, so do not convert signed left shift into
6125          multiplication.  */
6126       if (TREE_CODE (op1) == INTEGER_CST
6127           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6128           /* const_binop may not detect overflow correctly,
6129              so check for it explicitly here.  */
6130           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6131           && TREE_INT_CST_HIGH (op1) == 0
6132           && 0 != (t1 = fold_convert (ctype,
6133                                       const_binop (LSHIFT_EXPR,
6134                                                    size_one_node,
6135                                                    op1, 0)))
6136           && !TREE_OVERFLOW (t1))
6137         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6138                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
6139                                        ctype, fold_convert (ctype, op0), t1),
6140                                c, code, wide_type, strict_overflow_p);
6141       break;
6142
6143     case PLUS_EXPR:  case MINUS_EXPR:
6144       /* See if we can eliminate the operation on both sides.  If we can, we
6145          can return a new PLUS or MINUS.  If we can't, the only remaining
6146          cases where we can do anything are if the second operand is a
6147          constant.  */
6148       sub_strict_overflow_p = false;
6149       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6150       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6151       if (t1 != 0 && t2 != 0
6152           && (code == MULT_EXPR
6153               /* If not multiplication, we can only do this if both operands
6154                  are divisible by c.  */
6155               || (multiple_of_p (ctype, op0, c)
6156                   && multiple_of_p (ctype, op1, c))))
6157         {
6158           if (sub_strict_overflow_p)
6159             *strict_overflow_p = true;
6160           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6161                               fold_convert (ctype, t2));
6162         }
6163
6164       /* If this was a subtraction, negate OP1 and set it to be an addition.
6165          This simplifies the logic below.  */
6166       if (tcode == MINUS_EXPR)
6167         tcode = PLUS_EXPR, op1 = negate_expr (op1);
6168
6169       if (TREE_CODE (op1) != INTEGER_CST)
6170         break;
6171
6172       /* If either OP1 or C are negative, this optimization is not safe for
6173          some of the division and remainder types while for others we need
6174          to change the code.  */
6175       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6176         {
6177           if (code == CEIL_DIV_EXPR)
6178             code = FLOOR_DIV_EXPR;
6179           else if (code == FLOOR_DIV_EXPR)
6180             code = CEIL_DIV_EXPR;
6181           else if (code != MULT_EXPR
6182                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6183             break;
6184         }
6185
6186       /* If it's a multiply or a division/modulus operation of a multiple
6187          of our constant, do the operation and verify it doesn't overflow.  */
6188       if (code == MULT_EXPR
6189           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6190         {
6191           op1 = const_binop (code, fold_convert (ctype, op1),
6192                              fold_convert (ctype, c), 0);
6193           /* We allow the constant to overflow with wrapping semantics.  */
6194           if (op1 == 0
6195               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6196             break;
6197         }
6198       else
6199         break;
6200
6201       /* If we have an unsigned type is not a sizetype, we cannot widen
6202          the operation since it will change the result if the original
6203          computation overflowed.  */
6204       if (TYPE_UNSIGNED (ctype)
6205           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6206           && ctype != type)
6207         break;
6208
6209       /* If we were able to eliminate our operation from the first side,
6210          apply our operation to the second side and reform the PLUS.  */
6211       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6212         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6213
6214       /* The last case is if we are a multiply.  In that case, we can
6215          apply the distributive law to commute the multiply and addition
6216          if the multiplication of the constants doesn't overflow.  */
6217       if (code == MULT_EXPR)
6218         return fold_build2 (tcode, ctype,
6219                             fold_build2 (code, ctype,
6220                                          fold_convert (ctype, op0),
6221                                          fold_convert (ctype, c)),
6222                             op1);
6223
6224       break;
6225
6226     case MULT_EXPR:
6227       /* We have a special case here if we are doing something like
6228          (C * 8) % 4 since we know that's zero.  */
6229       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6230            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6231           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6232           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6233         return omit_one_operand (type, integer_zero_node, op0);
6234
6235       /* ... fall through ...  */
6236
6237     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
6238     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
6239       /* If we can extract our operation from the LHS, do so and return a
6240          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
6241          do something only if the second operand is a constant.  */
6242       if (same_p
6243           && (t1 = extract_muldiv (op0, c, code, wide_type,
6244                                    strict_overflow_p)) != 0)
6245         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6246                             fold_convert (ctype, op1));
6247       else if (tcode == MULT_EXPR && code == MULT_EXPR
6248                && (t1 = extract_muldiv (op1, c, code, wide_type,
6249                                         strict_overflow_p)) != 0)
6250         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6251                             fold_convert (ctype, t1));
6252       else if (TREE_CODE (op1) != INTEGER_CST)
6253         return 0;
6254
6255       /* If these are the same operation types, we can associate them
6256          assuming no overflow.  */
6257       if (tcode == code
6258           && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
6259                                      fold_convert (ctype, c), 0))
6260           && !TREE_OVERFLOW (t1))
6261         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6262
6263       /* If these operations "cancel" each other, we have the main
6264          optimizations of this pass, which occur when either constant is a
6265          multiple of the other, in which case we replace this with either an
6266          operation or CODE or TCODE.
6267
6268          If we have an unsigned type that is not a sizetype, we cannot do
6269          this since it will change the result if the original computation
6270          overflowed.  */
6271       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6272            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6273           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6274               || (tcode == MULT_EXPR
6275                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6276                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6277                   && code != MULT_EXPR)))
6278         {
6279           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6280             {
6281               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6282                 *strict_overflow_p = true;
6283               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6284                                   fold_convert (ctype,
6285                                                 const_binop (TRUNC_DIV_EXPR,
6286                                                              op1, c, 0)));
6287             }
6288           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6289             {
6290               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6291                 *strict_overflow_p = true;
6292               return fold_build2 (code, ctype, fold_convert (ctype, op0),
6293                                   fold_convert (ctype,
6294                                                 const_binop (TRUNC_DIV_EXPR,
6295                                                              c, op1, 0)));
6296             }
6297         }
6298       break;
6299
6300     default:
6301       break;
6302     }
6303
6304   return 0;
6305 }
6306 \f
6307 /* Return a node which has the indicated constant VALUE (either 0 or
6308    1), and is of the indicated TYPE.  */
6309
6310 tree
6311 constant_boolean_node (int value, tree type)
6312 {
6313   if (type == integer_type_node)
6314     return value ? integer_one_node : integer_zero_node;
6315   else if (type == boolean_type_node)
6316     return value ? boolean_true_node : boolean_false_node;
6317   else
6318     return build_int_cst (type, value);
6319 }
6320
6321
6322 /* Return true if expr looks like an ARRAY_REF and set base and
6323    offset to the appropriate trees.  If there is no offset,
6324    offset is set to NULL_TREE.  Base will be canonicalized to
6325    something you can get the element type from using
6326    TREE_TYPE (TREE_TYPE (base)).  Offset will be the offset
6327    in bytes to the base in sizetype.  */
6328
6329 static bool
6330 extract_array_ref (tree expr, tree *base, tree *offset)
6331 {
6332   /* One canonical form is a PLUS_EXPR with the first
6333      argument being an ADDR_EXPR with a possible NOP_EXPR
6334      attached.  */
6335   if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
6336     {
6337       tree op0 = TREE_OPERAND (expr, 0);
6338       tree inner_base, dummy1;
6339       /* Strip NOP_EXPRs here because the C frontends and/or
6340          folders present us (int *)&x.a p+ 4 possibly.  */
6341       STRIP_NOPS (op0);
6342       if (extract_array_ref (op0, &inner_base, &dummy1))
6343         {
6344           *base = inner_base;
6345           *offset = fold_convert (sizetype, TREE_OPERAND (expr, 1));
6346           if (dummy1 != NULL_TREE)
6347             *offset = fold_build2 (PLUS_EXPR, sizetype,
6348                                    dummy1, *offset);
6349           return true;
6350         }
6351     }
6352   /* Other canonical form is an ADDR_EXPR of an ARRAY_REF,
6353      which we transform into an ADDR_EXPR with appropriate
6354      offset.  For other arguments to the ADDR_EXPR we assume
6355      zero offset and as such do not care about the ADDR_EXPR
6356      type and strip possible nops from it.  */
6357   else if (TREE_CODE (expr) == ADDR_EXPR)
6358     {
6359       tree op0 = TREE_OPERAND (expr, 0);
6360       if (TREE_CODE (op0) == ARRAY_REF)
6361         {
6362           tree idx = TREE_OPERAND (op0, 1);
6363           *base = TREE_OPERAND (op0, 0);
6364           *offset = fold_build2 (MULT_EXPR, TREE_TYPE (idx), idx,
6365                                  array_ref_element_size (op0)); 
6366           *offset = fold_convert (sizetype, *offset);
6367         }
6368       else
6369         {
6370           /* Handle array-to-pointer decay as &a.  */
6371           if (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE)
6372             *base = TREE_OPERAND (expr, 0);
6373           else
6374             *base = expr;
6375           *offset = NULL_TREE;
6376         }
6377       return true;
6378     }
6379   /* The next canonical form is a VAR_DECL with POINTER_TYPE.  */
6380   else if (SSA_VAR_P (expr)
6381            && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
6382     {
6383       *base = expr;
6384       *offset = NULL_TREE;
6385       return true;
6386     }
6387
6388   return false;
6389 }
6390
6391
6392 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6393    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6394    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6395    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6396    COND is the first argument to CODE; otherwise (as in the example
6397    given here), it is the second argument.  TYPE is the type of the
6398    original expression.  Return NULL_TREE if no simplification is
6399    possible.  */
6400
6401 static tree
6402 fold_binary_op_with_conditional_arg (enum tree_code code,
6403                                      tree type, tree op0, tree op1,
6404                                      tree cond, tree arg, int cond_first_p)
6405 {
6406   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6407   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6408   tree test, true_value, false_value;
6409   tree lhs = NULL_TREE;
6410   tree rhs = NULL_TREE;
6411
6412   /* This transformation is only worthwhile if we don't have to wrap
6413      arg in a SAVE_EXPR, and the operation can be simplified on at least
6414      one of the branches once its pushed inside the COND_EXPR.  */
6415   if (!TREE_CONSTANT (arg))
6416     return NULL_TREE;
6417
6418   if (TREE_CODE (cond) == COND_EXPR)
6419     {
6420       test = TREE_OPERAND (cond, 0);
6421       true_value = TREE_OPERAND (cond, 1);
6422       false_value = TREE_OPERAND (cond, 2);
6423       /* If this operand throws an expression, then it does not make
6424          sense to try to perform a logical or arithmetic operation
6425          involving it.  */
6426       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6427         lhs = true_value;
6428       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6429         rhs = false_value;
6430     }
6431   else
6432     {
6433       tree testtype = TREE_TYPE (cond);
6434       test = cond;
6435       true_value = constant_boolean_node (true, testtype);
6436       false_value = constant_boolean_node (false, testtype);
6437     }
6438
6439   arg = fold_convert (arg_type, arg);
6440   if (lhs == 0)
6441     {
6442       true_value = fold_convert (cond_type, true_value);
6443       if (cond_first_p)
6444         lhs = fold_build2 (code, type, true_value, arg);
6445       else
6446         lhs = fold_build2 (code, type, arg, true_value);
6447     }
6448   if (rhs == 0)
6449     {
6450       false_value = fold_convert (cond_type, false_value);
6451       if (cond_first_p)
6452         rhs = fold_build2 (code, type, false_value, arg);
6453       else
6454         rhs = fold_build2 (code, type, arg, false_value);
6455     }
6456
6457   test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6458   return fold_convert (type, test);
6459 }
6460
6461 \f
6462 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6463
6464    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6465    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6466    ADDEND is the same as X.
6467
6468    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6469    and finite.  The problematic cases are when X is zero, and its mode
6470    has signed zeros.  In the case of rounding towards -infinity,
6471    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6472    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6473
6474 static bool
6475 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6476 {
6477   if (!real_zerop (addend))
6478     return false;
6479
6480   /* Don't allow the fold with -fsignaling-nans.  */
6481   if (HONOR_SNANS (TYPE_MODE (type)))
6482     return false;
6483
6484   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6485   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6486     return true;
6487
6488   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6489   if (TREE_CODE (addend) == REAL_CST
6490       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6491     negate = !negate;
6492
6493   /* The mode has signed zeros, and we have to honor their sign.
6494      In this situation, there is only one case we can return true for.
6495      X - 0 is the same as X unless rounding towards -infinity is
6496      supported.  */
6497   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6498 }
6499
6500 /* Subroutine of fold() that checks comparisons of built-in math
6501    functions against real constants.
6502
6503    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6504    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6505    is the type of the result and ARG0 and ARG1 are the operands of the
6506    comparison.  ARG1 must be a TREE_REAL_CST.
6507
6508    The function returns the constant folded tree if a simplification
6509    can be made, and NULL_TREE otherwise.  */
6510
6511 static tree
6512 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6513                      tree type, tree arg0, tree arg1)
6514 {
6515   REAL_VALUE_TYPE c;
6516
6517   if (BUILTIN_SQRT_P (fcode))
6518     {
6519       tree arg = CALL_EXPR_ARG (arg0, 0);
6520       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6521
6522       c = TREE_REAL_CST (arg1);
6523       if (REAL_VALUE_NEGATIVE (c))
6524         {
6525           /* sqrt(x) < y is always false, if y is negative.  */
6526           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6527             return omit_one_operand (type, integer_zero_node, arg);
6528
6529           /* sqrt(x) > y is always true, if y is negative and we
6530              don't care about NaNs, i.e. negative values of x.  */
6531           if (code == NE_EXPR || !HONOR_NANS (mode))
6532             return omit_one_operand (type, integer_one_node, arg);
6533
6534           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6535           return fold_build2 (GE_EXPR, type, arg,
6536                               build_real (TREE_TYPE (arg), dconst0));
6537         }
6538       else if (code == GT_EXPR || code == GE_EXPR)
6539         {
6540           REAL_VALUE_TYPE c2;
6541
6542           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6543           real_convert (&c2, mode, &c2);
6544
6545           if (REAL_VALUE_ISINF (c2))
6546             {
6547               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6548               if (HONOR_INFINITIES (mode))
6549                 return fold_build2 (EQ_EXPR, type, arg,
6550                                     build_real (TREE_TYPE (arg), c2));
6551
6552               /* sqrt(x) > y is always false, when y is very large
6553                  and we don't care about infinities.  */
6554               return omit_one_operand (type, integer_zero_node, arg);
6555             }
6556
6557           /* sqrt(x) > c is the same as x > c*c.  */
6558           return fold_build2 (code, type, arg,
6559                               build_real (TREE_TYPE (arg), c2));
6560         }
6561       else if (code == LT_EXPR || code == LE_EXPR)
6562         {
6563           REAL_VALUE_TYPE c2;
6564
6565           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6566           real_convert (&c2, mode, &c2);
6567
6568           if (REAL_VALUE_ISINF (c2))
6569             {
6570               /* sqrt(x) < y is always true, when y is a very large
6571                  value and we don't care about NaNs or Infinities.  */
6572               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6573                 return omit_one_operand (type, integer_one_node, arg);
6574
6575               /* sqrt(x) < y is x != +Inf when y is very large and we
6576                  don't care about NaNs.  */
6577               if (! HONOR_NANS (mode))
6578                 return fold_build2 (NE_EXPR, type, arg,
6579                                     build_real (TREE_TYPE (arg), c2));
6580
6581               /* sqrt(x) < y is x >= 0 when y is very large and we
6582                  don't care about Infinities.  */
6583               if (! HONOR_INFINITIES (mode))
6584                 return fold_build2 (GE_EXPR, type, arg,
6585                                     build_real (TREE_TYPE (arg), dconst0));
6586
6587               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6588               if (lang_hooks.decls.global_bindings_p () != 0
6589                   || CONTAINS_PLACEHOLDER_P (arg))
6590                 return NULL_TREE;
6591
6592               arg = save_expr (arg);
6593               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6594                                   fold_build2 (GE_EXPR, type, arg,
6595                                                build_real (TREE_TYPE (arg),
6596                                                            dconst0)),
6597                                   fold_build2 (NE_EXPR, type, arg,
6598                                                build_real (TREE_TYPE (arg),
6599                                                            c2)));
6600             }
6601
6602           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6603           if (! HONOR_NANS (mode))
6604             return fold_build2 (code, type, arg,
6605                                 build_real (TREE_TYPE (arg), c2));
6606
6607           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6608           if (lang_hooks.decls.global_bindings_p () == 0
6609               && ! CONTAINS_PLACEHOLDER_P (arg))
6610             {
6611               arg = save_expr (arg);
6612               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6613                                   fold_build2 (GE_EXPR, type, arg,
6614                                                build_real (TREE_TYPE (arg),
6615                                                            dconst0)),
6616                                   fold_build2 (code, type, arg,
6617                                                build_real (TREE_TYPE (arg),
6618                                                            c2)));
6619             }
6620         }
6621     }
6622
6623   return NULL_TREE;
6624 }
6625
6626 /* Subroutine of fold() that optimizes comparisons against Infinities,
6627    either +Inf or -Inf.
6628
6629    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6630    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6631    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6632
6633    The function returns the constant folded tree if a simplification
6634    can be made, and NULL_TREE otherwise.  */
6635
6636 static tree
6637 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6638 {
6639   enum machine_mode mode;
6640   REAL_VALUE_TYPE max;
6641   tree temp;
6642   bool neg;
6643
6644   mode = TYPE_MODE (TREE_TYPE (arg0));
6645
6646   /* For negative infinity swap the sense of the comparison.  */
6647   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6648   if (neg)
6649     code = swap_tree_comparison (code);
6650
6651   switch (code)
6652     {
6653     case GT_EXPR:
6654       /* x > +Inf is always false, if with ignore sNANs.  */
6655       if (HONOR_SNANS (mode))
6656         return NULL_TREE;
6657       return omit_one_operand (type, integer_zero_node, arg0);
6658
6659     case LE_EXPR:
6660       /* x <= +Inf is always true, if we don't case about NaNs.  */
6661       if (! HONOR_NANS (mode))
6662         return omit_one_operand (type, integer_one_node, arg0);
6663
6664       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6665       if (lang_hooks.decls.global_bindings_p () == 0
6666           && ! CONTAINS_PLACEHOLDER_P (arg0))
6667         {
6668           arg0 = save_expr (arg0);
6669           return fold_build2 (EQ_EXPR, type, arg0, arg0);
6670         }
6671       break;
6672
6673     case EQ_EXPR:
6674     case GE_EXPR:
6675       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6676       real_maxval (&max, neg, mode);
6677       return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6678                           arg0, build_real (TREE_TYPE (arg0), max));
6679
6680     case LT_EXPR:
6681       /* x < +Inf is always equal to x <= DBL_MAX.  */
6682       real_maxval (&max, neg, mode);
6683       return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6684                           arg0, build_real (TREE_TYPE (arg0), max));
6685
6686     case NE_EXPR:
6687       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6688       real_maxval (&max, neg, mode);
6689       if (! HONOR_NANS (mode))
6690         return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6691                             arg0, build_real (TREE_TYPE (arg0), max));
6692
6693       temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6694                           arg0, build_real (TREE_TYPE (arg0), max));
6695       return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6696
6697     default:
6698       break;
6699     }
6700
6701   return NULL_TREE;
6702 }
6703
6704 /* Subroutine of fold() that optimizes comparisons of a division by
6705    a nonzero integer constant against an integer constant, i.e.
6706    X/C1 op C2.
6707
6708    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6709    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6710    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6711
6712    The function returns the constant folded tree if a simplification
6713    can be made, and NULL_TREE otherwise.  */
6714
6715 static tree
6716 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6717 {
6718   tree prod, tmp, hi, lo;
6719   tree arg00 = TREE_OPERAND (arg0, 0);
6720   tree arg01 = TREE_OPERAND (arg0, 1);
6721   unsigned HOST_WIDE_INT lpart;
6722   HOST_WIDE_INT hpart;
6723   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6724   bool neg_overflow;
6725   int overflow;
6726
6727   /* We have to do this the hard way to detect unsigned overflow.
6728      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
6729   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6730                                    TREE_INT_CST_HIGH (arg01),
6731                                    TREE_INT_CST_LOW (arg1),
6732                                    TREE_INT_CST_HIGH (arg1),
6733                                    &lpart, &hpart, unsigned_p);
6734   prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6735                                 -1, overflow);
6736   neg_overflow = false;
6737
6738   if (unsigned_p)
6739     {
6740       tmp = int_const_binop (MINUS_EXPR, arg01,
6741                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6742       lo = prod;
6743
6744       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
6745       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6746                                        TREE_INT_CST_HIGH (prod),
6747                                        TREE_INT_CST_LOW (tmp),
6748                                        TREE_INT_CST_HIGH (tmp),
6749                                        &lpart, &hpart, unsigned_p);
6750       hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6751                                   -1, overflow | TREE_OVERFLOW (prod));
6752     }
6753   else if (tree_int_cst_sgn (arg01) >= 0)
6754     {
6755       tmp = int_const_binop (MINUS_EXPR, arg01,
6756                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6757       switch (tree_int_cst_sgn (arg1))
6758         {
6759         case -1:
6760           neg_overflow = true;
6761           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6762           hi = prod;
6763           break;
6764
6765         case  0:
6766           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6767           hi = tmp;
6768           break;
6769
6770         case  1:
6771           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6772           lo = prod;
6773           break;
6774
6775         default:
6776           gcc_unreachable ();
6777         }
6778     }
6779   else
6780     {
6781       /* A negative divisor reverses the relational operators.  */
6782       code = swap_tree_comparison (code);
6783
6784       tmp = int_const_binop (PLUS_EXPR, arg01,
6785                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6786       switch (tree_int_cst_sgn (arg1))
6787         {
6788         case -1:
6789           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6790           lo = prod;
6791           break;
6792
6793         case  0:
6794           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6795           lo = tmp;
6796           break;
6797
6798         case  1:
6799           neg_overflow = true;
6800           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6801           hi = prod;
6802           break;
6803
6804         default:
6805           gcc_unreachable ();
6806         }
6807     }
6808
6809   switch (code)
6810     {
6811     case EQ_EXPR:
6812       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6813         return omit_one_operand (type, integer_zero_node, arg00);
6814       if (TREE_OVERFLOW (hi))
6815         return fold_build2 (GE_EXPR, type, arg00, lo);
6816       if (TREE_OVERFLOW (lo))
6817         return fold_build2 (LE_EXPR, type, arg00, hi);
6818       return build_range_check (type, arg00, 1, lo, hi);
6819
6820     case NE_EXPR:
6821       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6822         return omit_one_operand (type, integer_one_node, arg00);
6823       if (TREE_OVERFLOW (hi))
6824         return fold_build2 (LT_EXPR, type, arg00, lo);
6825       if (TREE_OVERFLOW (lo))
6826         return fold_build2 (GT_EXPR, type, arg00, hi);
6827       return build_range_check (type, arg00, 0, lo, hi);
6828
6829     case LT_EXPR:
6830       if (TREE_OVERFLOW (lo))
6831         {
6832           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6833           return omit_one_operand (type, tmp, arg00);
6834         }
6835       return fold_build2 (LT_EXPR, type, arg00, lo);
6836
6837     case LE_EXPR:
6838       if (TREE_OVERFLOW (hi))
6839         {
6840           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6841           return omit_one_operand (type, tmp, arg00);
6842         }
6843       return fold_build2 (LE_EXPR, type, arg00, hi);
6844
6845     case GT_EXPR:
6846       if (TREE_OVERFLOW (hi))
6847         {
6848           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6849           return omit_one_operand (type, tmp, arg00);
6850         }
6851       return fold_build2 (GT_EXPR, type, arg00, hi);
6852
6853     case GE_EXPR:
6854       if (TREE_OVERFLOW (lo))
6855         {
6856           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6857           return omit_one_operand (type, tmp, arg00);
6858         }
6859       return fold_build2 (GE_EXPR, type, arg00, lo);
6860
6861     default:
6862       break;
6863     }
6864
6865   return NULL_TREE;
6866 }
6867
6868
6869 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6870    equality/inequality test, then return a simplified form of the test
6871    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6872    result type.  */
6873
6874 static tree
6875 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6876                                      tree result_type)
6877 {
6878   /* If this is testing a single bit, we can optimize the test.  */
6879   if ((code == NE_EXPR || code == EQ_EXPR)
6880       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6881       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6882     {
6883       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6884          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6885       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6886
6887       if (arg00 != NULL_TREE
6888           /* This is only a win if casting to a signed type is cheap,
6889              i.e. when arg00's type is not a partial mode.  */
6890           && TYPE_PRECISION (TREE_TYPE (arg00))
6891              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6892         {
6893           tree stype = signed_type_for (TREE_TYPE (arg00));
6894           return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6895                               result_type, fold_convert (stype, arg00),
6896                               build_int_cst (stype, 0));
6897         }
6898     }
6899
6900   return NULL_TREE;
6901 }
6902
6903 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6904    equality/inequality test, then return a simplified form of
6905    the test using shifts and logical operations.  Otherwise return
6906    NULL.  TYPE is the desired result type.  */
6907
6908 tree
6909 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6910                       tree result_type)
6911 {
6912   /* If this is testing a single bit, we can optimize the test.  */
6913   if ((code == NE_EXPR || code == EQ_EXPR)
6914       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6915       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6916     {
6917       tree inner = TREE_OPERAND (arg0, 0);
6918       tree type = TREE_TYPE (arg0);
6919       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6920       enum machine_mode operand_mode = TYPE_MODE (type);
6921       int ops_unsigned;
6922       tree signed_type, unsigned_type, intermediate_type;
6923       tree tem, one;
6924
6925       /* First, see if we can fold the single bit test into a sign-bit
6926          test.  */
6927       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6928                                                  result_type);
6929       if (tem)
6930         return tem;
6931
6932       /* Otherwise we have (A & C) != 0 where C is a single bit,
6933          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6934          Similarly for (A & C) == 0.  */
6935
6936       /* If INNER is a right shift of a constant and it plus BITNUM does
6937          not overflow, adjust BITNUM and INNER.  */
6938       if (TREE_CODE (inner) == RSHIFT_EXPR
6939           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6940           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6941           && bitnum < TYPE_PRECISION (type)
6942           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6943                                    bitnum - TYPE_PRECISION (type)))
6944         {
6945           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6946           inner = TREE_OPERAND (inner, 0);
6947         }
6948
6949       /* If we are going to be able to omit the AND below, we must do our
6950          operations as unsigned.  If we must use the AND, we have a choice.
6951          Normally unsigned is faster, but for some machines signed is.  */
6952 #ifdef LOAD_EXTEND_OP
6953       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND 
6954                       && !flag_syntax_only) ? 0 : 1;
6955 #else
6956       ops_unsigned = 1;
6957 #endif
6958
6959       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6960       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6961       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6962       inner = fold_convert (intermediate_type, inner);
6963
6964       if (bitnum != 0)
6965         inner = build2 (RSHIFT_EXPR, intermediate_type,
6966                         inner, size_int (bitnum));
6967
6968       one = build_int_cst (intermediate_type, 1);
6969
6970       if (code == EQ_EXPR)
6971         inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6972
6973       /* Put the AND last so it can combine with more things.  */
6974       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6975
6976       /* Make sure to return the proper type.  */
6977       inner = fold_convert (result_type, inner);
6978
6979       return inner;
6980     }
6981   return NULL_TREE;
6982 }
6983
6984 /* Check whether we are allowed to reorder operands arg0 and arg1,
6985    such that the evaluation of arg1 occurs before arg0.  */
6986
6987 static bool
6988 reorder_operands_p (const_tree arg0, const_tree arg1)
6989 {
6990   if (! flag_evaluation_order)
6991       return true;
6992   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6993     return true;
6994   return ! TREE_SIDE_EFFECTS (arg0)
6995          && ! TREE_SIDE_EFFECTS (arg1);
6996 }
6997
6998 /* Test whether it is preferable two swap two operands, ARG0 and
6999    ARG1, for example because ARG0 is an integer constant and ARG1
7000    isn't.  If REORDER is true, only recommend swapping if we can
7001    evaluate the operands in reverse order.  */
7002
7003 bool
7004 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
7005 {
7006   STRIP_SIGN_NOPS (arg0);
7007   STRIP_SIGN_NOPS (arg1);
7008
7009   if (TREE_CODE (arg1) == INTEGER_CST)
7010     return 0;
7011   if (TREE_CODE (arg0) == INTEGER_CST)
7012     return 1;
7013
7014   if (TREE_CODE (arg1) == REAL_CST)
7015     return 0;
7016   if (TREE_CODE (arg0) == REAL_CST)
7017     return 1;
7018
7019   if (TREE_CODE (arg1) == FIXED_CST)
7020     return 0;
7021   if (TREE_CODE (arg0) == FIXED_CST)
7022     return 1;
7023
7024   if (TREE_CODE (arg1) == COMPLEX_CST)
7025     return 0;
7026   if (TREE_CODE (arg0) == COMPLEX_CST)
7027     return 1;
7028
7029   if (TREE_CONSTANT (arg1))
7030     return 0;
7031   if (TREE_CONSTANT (arg0))
7032     return 1;
7033
7034   if (optimize_size)
7035     return 0;
7036
7037   if (reorder && flag_evaluation_order
7038       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7039     return 0;
7040
7041   /* It is preferable to swap two SSA_NAME to ensure a canonical form
7042      for commutative and comparison operators.  Ensuring a canonical
7043      form allows the optimizers to find additional redundancies without
7044      having to explicitly check for both orderings.  */
7045   if (TREE_CODE (arg0) == SSA_NAME
7046       && TREE_CODE (arg1) == SSA_NAME
7047       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7048     return 1;
7049
7050   /* Put SSA_NAMEs last.  */
7051   if (TREE_CODE (arg1) == SSA_NAME)
7052     return 0;
7053   if (TREE_CODE (arg0) == SSA_NAME)
7054     return 1;
7055
7056   /* Put variables last.  */
7057   if (DECL_P (arg1))
7058     return 0;
7059   if (DECL_P (arg0))
7060     return 1;
7061
7062   return 0;
7063 }
7064
7065 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7066    ARG0 is extended to a wider type.  */
7067
7068 static tree
7069 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
7070 {
7071   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7072   tree arg1_unw;
7073   tree shorter_type, outer_type;
7074   tree min, max;
7075   bool above, below;
7076
7077   if (arg0_unw == arg0)
7078     return NULL_TREE;
7079   shorter_type = TREE_TYPE (arg0_unw);
7080
7081 #ifdef HAVE_canonicalize_funcptr_for_compare
7082   /* Disable this optimization if we're casting a function pointer
7083      type on targets that require function pointer canonicalization.  */
7084   if (HAVE_canonicalize_funcptr_for_compare
7085       && TREE_CODE (shorter_type) == POINTER_TYPE
7086       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7087     return NULL_TREE;
7088 #endif
7089
7090   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7091     return NULL_TREE;
7092
7093   arg1_unw = get_unwidened (arg1, shorter_type);
7094
7095   /* If possible, express the comparison in the shorter mode.  */
7096   if ((code == EQ_EXPR || code == NE_EXPR
7097        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7098       && (TREE_TYPE (arg1_unw) == shorter_type
7099           || (TREE_CODE (arg1_unw) == INTEGER_CST
7100               && (TREE_CODE (shorter_type) == INTEGER_TYPE
7101                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7102               && int_fits_type_p (arg1_unw, shorter_type))))
7103     return fold_build2 (code, type, arg0_unw,
7104                        fold_convert (shorter_type, arg1_unw));
7105
7106   if (TREE_CODE (arg1_unw) != INTEGER_CST
7107       || TREE_CODE (shorter_type) != INTEGER_TYPE
7108       || !int_fits_type_p (arg1_unw, shorter_type))
7109     return NULL_TREE;
7110
7111   /* If we are comparing with the integer that does not fit into the range
7112      of the shorter type, the result is known.  */
7113   outer_type = TREE_TYPE (arg1_unw);
7114   min = lower_bound_in_type (outer_type, shorter_type);
7115   max = upper_bound_in_type (outer_type, shorter_type);
7116
7117   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7118                                                    max, arg1_unw));
7119   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7120                                                    arg1_unw, min));
7121
7122   switch (code)
7123     {
7124     case EQ_EXPR:
7125       if (above || below)
7126         return omit_one_operand (type, integer_zero_node, arg0);
7127       break;
7128
7129     case NE_EXPR:
7130       if (above || below)
7131         return omit_one_operand (type, integer_one_node, arg0);
7132       break;
7133
7134     case LT_EXPR:
7135     case LE_EXPR:
7136       if (above)
7137         return omit_one_operand (type, integer_one_node, arg0);
7138       else if (below)
7139         return omit_one_operand (type, integer_zero_node, arg0);
7140
7141     case GT_EXPR:
7142     case GE_EXPR:
7143       if (above)
7144         return omit_one_operand (type, integer_zero_node, arg0);
7145       else if (below)
7146         return omit_one_operand (type, integer_one_node, arg0);
7147
7148     default:
7149       break;
7150     }
7151
7152   return NULL_TREE;
7153 }
7154
7155 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7156    ARG0 just the signedness is changed.  */
7157
7158 static tree
7159 fold_sign_changed_comparison (enum tree_code code, tree type,
7160                               tree arg0, tree arg1)
7161 {
7162   tree arg0_inner;
7163   tree inner_type, outer_type;
7164
7165   if (TREE_CODE (arg0) != NOP_EXPR
7166       && TREE_CODE (arg0) != CONVERT_EXPR)
7167     return NULL_TREE;
7168
7169   outer_type = TREE_TYPE (arg0);
7170   arg0_inner = TREE_OPERAND (arg0, 0);
7171   inner_type = TREE_TYPE (arg0_inner);
7172
7173 #ifdef HAVE_canonicalize_funcptr_for_compare
7174   /* Disable this optimization if we're casting a function pointer
7175      type on targets that require function pointer canonicalization.  */
7176   if (HAVE_canonicalize_funcptr_for_compare
7177       && TREE_CODE (inner_type) == POINTER_TYPE
7178       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7179     return NULL_TREE;
7180 #endif
7181
7182   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7183     return NULL_TREE;
7184
7185   if (TREE_CODE (arg1) != INTEGER_CST
7186       && !((TREE_CODE (arg1) == NOP_EXPR
7187             || TREE_CODE (arg1) == CONVERT_EXPR)
7188            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7189     return NULL_TREE;
7190
7191   if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7192       && code != NE_EXPR
7193       && code != EQ_EXPR)
7194     return NULL_TREE;
7195
7196   if (TREE_CODE (arg1) == INTEGER_CST)
7197     arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7198                                   TREE_INT_CST_HIGH (arg1), 0,
7199                                   TREE_OVERFLOW (arg1));
7200   else
7201     arg1 = fold_convert (inner_type, arg1);
7202
7203   return fold_build2 (code, type, arg0_inner, arg1);
7204 }
7205
7206 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7207    step of the array.  Reconstructs s and delta in the case of s * delta
7208    being an integer constant (and thus already folded).
7209    ADDR is the address. MULT is the multiplicative expression.
7210    If the function succeeds, the new address expression is returned.  Otherwise
7211    NULL_TREE is returned.  */
7212
7213 static tree
7214 try_move_mult_to_index (tree addr, tree op1)
7215 {
7216   tree s, delta, step;
7217   tree ref = TREE_OPERAND (addr, 0), pref;
7218   tree ret, pos;
7219   tree itype;
7220   bool mdim = false;
7221
7222   /*  Strip the nops that might be added when converting op1 to sizetype. */
7223   STRIP_NOPS (op1);
7224
7225   /* Canonicalize op1 into a possibly non-constant delta
7226      and an INTEGER_CST s.  */
7227   if (TREE_CODE (op1) == MULT_EXPR)
7228     {
7229       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7230
7231       STRIP_NOPS (arg0);
7232       STRIP_NOPS (arg1);
7233   
7234       if (TREE_CODE (arg0) == INTEGER_CST)
7235         {
7236           s = arg0;
7237           delta = arg1;
7238         }
7239       else if (TREE_CODE (arg1) == INTEGER_CST)
7240         {
7241           s = arg1;
7242           delta = arg0;
7243         }
7244       else
7245         return NULL_TREE;
7246     }
7247   else if (TREE_CODE (op1) == INTEGER_CST)
7248     {
7249       delta = op1;
7250       s = NULL_TREE;
7251     }
7252   else
7253     {
7254       /* Simulate we are delta * 1.  */
7255       delta = op1;
7256       s = integer_one_node;
7257     }
7258
7259   for (;; ref = TREE_OPERAND (ref, 0))
7260     {
7261       if (TREE_CODE (ref) == ARRAY_REF)
7262         {
7263           /* Remember if this was a multi-dimensional array.  */
7264           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7265             mdim = true;
7266
7267           itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7268           if (! itype)
7269             continue;
7270
7271           step = array_ref_element_size (ref);
7272           if (TREE_CODE (step) != INTEGER_CST)
7273             continue;
7274
7275           if (s)
7276             {
7277               if (! tree_int_cst_equal (step, s))
7278                 continue;
7279             }
7280           else
7281             {
7282               /* Try if delta is a multiple of step.  */
7283               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, delta, step);
7284               if (! tmp)
7285                 continue;
7286               delta = tmp;
7287             }
7288
7289           /* Only fold here if we can verify we do not overflow one
7290              dimension of a multi-dimensional array.  */
7291           if (mdim)
7292             {
7293               tree tmp;
7294
7295               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7296                   || !INTEGRAL_TYPE_P (itype)
7297                   || !TYPE_MAX_VALUE (itype)
7298                   || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
7299                 continue;
7300
7301               tmp = fold_binary (PLUS_EXPR, itype,
7302                                  fold_convert (itype,
7303                                                TREE_OPERAND (ref, 1)),
7304                                  fold_convert (itype, delta));
7305               if (!tmp
7306                   || TREE_CODE (tmp) != INTEGER_CST
7307                   || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
7308                 continue;
7309             }
7310
7311           break;
7312         }
7313       else
7314         mdim = false;
7315
7316       if (!handled_component_p (ref))
7317         return NULL_TREE;
7318     }
7319
7320   /* We found the suitable array reference.  So copy everything up to it,
7321      and replace the index.  */
7322
7323   pref = TREE_OPERAND (addr, 0);
7324   ret = copy_node (pref);
7325   pos = ret;
7326
7327   while (pref != ref)
7328     {
7329       pref = TREE_OPERAND (pref, 0);
7330       TREE_OPERAND (pos, 0) = copy_node (pref);
7331       pos = TREE_OPERAND (pos, 0);
7332     }
7333
7334   TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
7335                                        fold_convert (itype,
7336                                                      TREE_OPERAND (pos, 1)),
7337                                        fold_convert (itype, delta));
7338
7339   return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
7340 }
7341
7342
7343 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
7344    means A >= Y && A != MAX, but in this case we know that
7345    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
7346
7347 static tree
7348 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
7349 {
7350   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7351
7352   if (TREE_CODE (bound) == LT_EXPR)
7353     a = TREE_OPERAND (bound, 0);
7354   else if (TREE_CODE (bound) == GT_EXPR)
7355     a = TREE_OPERAND (bound, 1);
7356   else
7357     return NULL_TREE;
7358
7359   typea = TREE_TYPE (a);
7360   if (!INTEGRAL_TYPE_P (typea)
7361       && !POINTER_TYPE_P (typea))
7362     return NULL_TREE;
7363
7364   if (TREE_CODE (ineq) == LT_EXPR)
7365     {
7366       a1 = TREE_OPERAND (ineq, 1);
7367       y = TREE_OPERAND (ineq, 0);
7368     }
7369   else if (TREE_CODE (ineq) == GT_EXPR)
7370     {
7371       a1 = TREE_OPERAND (ineq, 0);
7372       y = TREE_OPERAND (ineq, 1);
7373     }
7374   else
7375     return NULL_TREE;
7376
7377   if (TREE_TYPE (a1) != typea)
7378     return NULL_TREE;
7379
7380   if (POINTER_TYPE_P (typea))
7381     {
7382       /* Convert the pointer types into integer before taking the difference.  */
7383       tree ta = fold_convert (ssizetype, a);
7384       tree ta1 = fold_convert (ssizetype, a1);
7385       diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7386     }
7387   else
7388    diff = fold_binary (MINUS_EXPR, typea, a1, a);
7389
7390   if (!diff || !integer_onep (diff))
7391    return NULL_TREE;
7392
7393   return fold_build2 (GE_EXPR, type, a, y);
7394 }
7395
7396 /* Fold a sum or difference of at least one multiplication.
7397    Returns the folded tree or NULL if no simplification could be made.  */
7398
7399 static tree
7400 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7401 {
7402   tree arg00, arg01, arg10, arg11;
7403   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7404
7405   /* (A * C) +- (B * C) -> (A+-B) * C.
7406      (A * C) +- A -> A * (C+-1).
7407      We are most concerned about the case where C is a constant,
7408      but other combinations show up during loop reduction.  Since
7409      it is not difficult, try all four possibilities.  */
7410
7411   if (TREE_CODE (arg0) == MULT_EXPR)
7412     {
7413       arg00 = TREE_OPERAND (arg0, 0);
7414       arg01 = TREE_OPERAND (arg0, 1);
7415     }
7416   else if (TREE_CODE (arg0) == INTEGER_CST)
7417     {
7418       arg00 = build_one_cst (type);
7419       arg01 = arg0;
7420     }
7421   else
7422     {
7423       /* We cannot generate constant 1 for fract.  */
7424       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7425         return NULL_TREE;
7426       arg00 = arg0;
7427       arg01 = build_one_cst (type);
7428     }
7429   if (TREE_CODE (arg1) == MULT_EXPR)
7430     {
7431       arg10 = TREE_OPERAND (arg1, 0);
7432       arg11 = TREE_OPERAND (arg1, 1);
7433     }
7434   else if (TREE_CODE (arg1) == INTEGER_CST)
7435     {
7436       arg10 = build_one_cst (type);
7437       arg11 = arg1;
7438     }
7439   else
7440     {
7441       /* We cannot generate constant 1 for fract.  */
7442       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7443         return NULL_TREE;
7444       arg10 = arg1;
7445       arg11 = build_one_cst (type);
7446     }
7447   same = NULL_TREE;
7448
7449   if (operand_equal_p (arg01, arg11, 0))
7450     same = arg01, alt0 = arg00, alt1 = arg10;
7451   else if (operand_equal_p (arg00, arg10, 0))
7452     same = arg00, alt0 = arg01, alt1 = arg11;
7453   else if (operand_equal_p (arg00, arg11, 0))
7454     same = arg00, alt0 = arg01, alt1 = arg10;
7455   else if (operand_equal_p (arg01, arg10, 0))
7456     same = arg01, alt0 = arg00, alt1 = arg11;
7457
7458   /* No identical multiplicands; see if we can find a common
7459      power-of-two factor in non-power-of-two multiplies.  This
7460      can help in multi-dimensional array access.  */
7461   else if (host_integerp (arg01, 0)
7462            && host_integerp (arg11, 0))
7463     {
7464       HOST_WIDE_INT int01, int11, tmp;
7465       bool swap = false;
7466       tree maybe_same;
7467       int01 = TREE_INT_CST_LOW (arg01);
7468       int11 = TREE_INT_CST_LOW (arg11);
7469
7470       /* Move min of absolute values to int11.  */
7471       if ((int01 >= 0 ? int01 : -int01)
7472           < (int11 >= 0 ? int11 : -int11))
7473         {
7474           tmp = int01, int01 = int11, int11 = tmp;
7475           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7476           maybe_same = arg01;
7477           swap = true;
7478         }
7479       else
7480         maybe_same = arg11;
7481
7482       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
7483         {
7484           alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7485                               build_int_cst (TREE_TYPE (arg00),
7486                                              int01 / int11));
7487           alt1 = arg10;
7488           same = maybe_same;
7489           if (swap)
7490             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7491         }
7492     }
7493
7494   if (same)
7495     return fold_build2 (MULT_EXPR, type,
7496                         fold_build2 (code, type,
7497                                      fold_convert (type, alt0),
7498                                      fold_convert (type, alt1)),
7499                         fold_convert (type, same));
7500
7501   return NULL_TREE;
7502 }
7503
7504 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7505    specified by EXPR into the buffer PTR of length LEN bytes.
7506    Return the number of bytes placed in the buffer, or zero
7507    upon failure.  */
7508
7509 static int
7510 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7511 {
7512   tree type = TREE_TYPE (expr);
7513   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7514   int byte, offset, word, words;
7515   unsigned char value;
7516
7517   if (total_bytes > len)
7518     return 0;
7519   words = total_bytes / UNITS_PER_WORD;
7520
7521   for (byte = 0; byte < total_bytes; byte++)
7522     {
7523       int bitpos = byte * BITS_PER_UNIT;
7524       if (bitpos < HOST_BITS_PER_WIDE_INT)
7525         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7526       else
7527         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7528                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7529
7530       if (total_bytes > UNITS_PER_WORD)
7531         {
7532           word = byte / UNITS_PER_WORD;
7533           if (WORDS_BIG_ENDIAN)
7534             word = (words - 1) - word;
7535           offset = word * UNITS_PER_WORD;
7536           if (BYTES_BIG_ENDIAN)
7537             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7538           else
7539             offset += byte % UNITS_PER_WORD;
7540         }
7541       else
7542         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7543       ptr[offset] = value;
7544     }
7545   return total_bytes;
7546 }
7547
7548
7549 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7550    specified by EXPR into the buffer PTR of length LEN bytes.
7551    Return the number of bytes placed in the buffer, or zero
7552    upon failure.  */
7553
7554 static int
7555 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7556 {
7557   tree type = TREE_TYPE (expr);
7558   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7559   int byte, offset, word, words, bitpos;
7560   unsigned char value;
7561
7562   /* There are always 32 bits in each long, no matter the size of
7563      the hosts long.  We handle floating point representations with
7564      up to 192 bits.  */
7565   long tmp[6];
7566
7567   if (total_bytes > len)
7568     return 0;
7569   words = 32 / UNITS_PER_WORD;
7570
7571   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7572
7573   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7574        bitpos += BITS_PER_UNIT)
7575     {
7576       byte = (bitpos / BITS_PER_UNIT) & 3;
7577       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7578
7579       if (UNITS_PER_WORD < 4)
7580         {
7581           word = byte / UNITS_PER_WORD;
7582           if (WORDS_BIG_ENDIAN)
7583             word = (words - 1) - word;
7584           offset = word * UNITS_PER_WORD;
7585           if (BYTES_BIG_ENDIAN)
7586             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7587           else
7588             offset += byte % UNITS_PER_WORD;
7589         }
7590       else
7591         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7592       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7593     }
7594   return total_bytes;
7595 }
7596
7597 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7598    specified by EXPR into the buffer PTR of length LEN bytes.
7599    Return the number of bytes placed in the buffer, or zero
7600    upon failure.  */
7601
7602 static int
7603 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7604 {
7605   int rsize, isize;
7606   tree part;
7607
7608   part = TREE_REALPART (expr);
7609   rsize = native_encode_expr (part, ptr, len);
7610   if (rsize == 0)
7611     return 0;
7612   part = TREE_IMAGPART (expr);
7613   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7614   if (isize != rsize)
7615     return 0;
7616   return rsize + isize;
7617 }
7618
7619
7620 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7621    specified by EXPR into the buffer PTR of length LEN bytes.
7622    Return the number of bytes placed in the buffer, or zero
7623    upon failure.  */
7624
7625 static int
7626 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7627 {
7628   int i, size, offset, count;
7629   tree itype, elem, elements;
7630
7631   offset = 0;
7632   elements = TREE_VECTOR_CST_ELTS (expr);
7633   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7634   itype = TREE_TYPE (TREE_TYPE (expr));
7635   size = GET_MODE_SIZE (TYPE_MODE (itype));
7636   for (i = 0; i < count; i++)
7637     {
7638       if (elements)
7639         {
7640           elem = TREE_VALUE (elements);
7641           elements = TREE_CHAIN (elements);
7642         }
7643       else
7644         elem = NULL_TREE;
7645
7646       if (elem)
7647         {
7648           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7649             return 0;
7650         }
7651       else
7652         {
7653           if (offset + size > len)
7654             return 0;
7655           memset (ptr+offset, 0, size);
7656         }
7657       offset += size;
7658     }
7659   return offset;
7660 }
7661
7662
7663 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7664    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7665    buffer PTR of length LEN bytes.  Return the number of bytes
7666    placed in the buffer, or zero upon failure.  */
7667
7668 int
7669 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7670 {
7671   switch (TREE_CODE (expr))
7672     {
7673     case INTEGER_CST:
7674       return native_encode_int (expr, ptr, len);
7675
7676     case REAL_CST:
7677       return native_encode_real (expr, ptr, len);
7678
7679     case COMPLEX_CST:
7680       return native_encode_complex (expr, ptr, len);
7681
7682     case VECTOR_CST:
7683       return native_encode_vector (expr, ptr, len);
7684
7685     default:
7686       return 0;
7687     }
7688 }
7689
7690
7691 /* Subroutine of native_interpret_expr.  Interpret the contents of
7692    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7693    If the buffer cannot be interpreted, return NULL_TREE.  */
7694
7695 static tree
7696 native_interpret_int (tree type, const unsigned char *ptr, int len)
7697 {
7698   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7699   int byte, offset, word, words;
7700   unsigned char value;
7701   unsigned int HOST_WIDE_INT lo = 0;
7702   HOST_WIDE_INT hi = 0;
7703
7704   if (total_bytes > len)
7705     return NULL_TREE;
7706   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7707     return NULL_TREE;
7708   words = total_bytes / UNITS_PER_WORD;
7709
7710   for (byte = 0; byte < total_bytes; byte++)
7711     {
7712       int bitpos = byte * BITS_PER_UNIT;
7713       if (total_bytes > UNITS_PER_WORD)
7714         {
7715           word = byte / UNITS_PER_WORD;
7716           if (WORDS_BIG_ENDIAN)
7717             word = (words - 1) - word;
7718           offset = word * UNITS_PER_WORD;
7719           if (BYTES_BIG_ENDIAN)
7720             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7721           else
7722             offset += byte % UNITS_PER_WORD;
7723         }
7724       else
7725         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7726       value = ptr[offset];
7727
7728       if (bitpos < HOST_BITS_PER_WIDE_INT)
7729         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7730       else
7731         hi |= (unsigned HOST_WIDE_INT) value
7732               << (bitpos - HOST_BITS_PER_WIDE_INT);
7733     }
7734
7735   return build_int_cst_wide_type (type, lo, hi);
7736 }
7737
7738
7739 /* Subroutine of native_interpret_expr.  Interpret the contents of
7740    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7741    If the buffer cannot be interpreted, return NULL_TREE.  */
7742
7743 static tree
7744 native_interpret_real (tree type, const unsigned char *ptr, int len)
7745 {
7746   enum machine_mode mode = TYPE_MODE (type);
7747   int total_bytes = GET_MODE_SIZE (mode);
7748   int byte, offset, word, words, bitpos;
7749   unsigned char value;
7750   /* There are always 32 bits in each long, no matter the size of
7751      the hosts long.  We handle floating point representations with
7752      up to 192 bits.  */
7753   REAL_VALUE_TYPE r;
7754   long tmp[6];
7755
7756   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7757   if (total_bytes > len || total_bytes > 24)
7758     return NULL_TREE;
7759   words = 32 / UNITS_PER_WORD;
7760
7761   memset (tmp, 0, sizeof (tmp));
7762   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7763        bitpos += BITS_PER_UNIT)
7764     {
7765       byte = (bitpos / BITS_PER_UNIT) & 3;
7766       if (UNITS_PER_WORD < 4)
7767         {
7768           word = byte / UNITS_PER_WORD;
7769           if (WORDS_BIG_ENDIAN)
7770             word = (words - 1) - word;
7771           offset = word * UNITS_PER_WORD;
7772           if (BYTES_BIG_ENDIAN)
7773             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7774           else
7775             offset += byte % UNITS_PER_WORD;
7776         }
7777       else
7778         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7779       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7780
7781       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7782     }
7783
7784   real_from_target (&r, tmp, mode);
7785   return build_real (type, r);
7786 }
7787
7788
7789 /* Subroutine of native_interpret_expr.  Interpret the contents of
7790    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7791    If the buffer cannot be interpreted, return NULL_TREE.  */
7792
7793 static tree
7794 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7795 {
7796   tree etype, rpart, ipart;
7797   int size;
7798
7799   etype = TREE_TYPE (type);
7800   size = GET_MODE_SIZE (TYPE_MODE (etype));
7801   if (size * 2 > len)
7802     return NULL_TREE;
7803   rpart = native_interpret_expr (etype, ptr, size);
7804   if (!rpart)
7805     return NULL_TREE;
7806   ipart = native_interpret_expr (etype, ptr+size, size);
7807   if (!ipart)
7808     return NULL_TREE;
7809   return build_complex (type, rpart, ipart);
7810 }
7811
7812
7813 /* Subroutine of native_interpret_expr.  Interpret the contents of
7814    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7815    If the buffer cannot be interpreted, return NULL_TREE.  */
7816
7817 static tree
7818 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7819 {
7820   tree etype, elem, elements;
7821   int i, size, count;
7822
7823   etype = TREE_TYPE (type);
7824   size = GET_MODE_SIZE (TYPE_MODE (etype));
7825   count = TYPE_VECTOR_SUBPARTS (type);
7826   if (size * count > len)
7827     return NULL_TREE;
7828
7829   elements = NULL_TREE;
7830   for (i = count - 1; i >= 0; i--)
7831     {
7832       elem = native_interpret_expr (etype, ptr+(i*size), size);
7833       if (!elem)
7834         return NULL_TREE;
7835       elements = tree_cons (NULL_TREE, elem, elements);
7836     }
7837   return build_vector (type, elements);
7838 }
7839
7840
7841 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7842    the buffer PTR of length LEN as a constant of type TYPE.  For
7843    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7844    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7845    return NULL_TREE.  */
7846
7847 tree
7848 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7849 {
7850   switch (TREE_CODE (type))
7851     {
7852     case INTEGER_TYPE:
7853     case ENUMERAL_TYPE:
7854     case BOOLEAN_TYPE:
7855       return native_interpret_int (type, ptr, len);
7856
7857     case REAL_TYPE:
7858       return native_interpret_real (type, ptr, len);
7859
7860     case COMPLEX_TYPE:
7861       return native_interpret_complex (type, ptr, len);
7862
7863     case VECTOR_TYPE:
7864       return native_interpret_vector (type, ptr, len);
7865
7866     default:
7867       return NULL_TREE;
7868     }
7869 }
7870
7871
7872 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7873    TYPE at compile-time.  If we're unable to perform the conversion
7874    return NULL_TREE.  */
7875
7876 static tree
7877 fold_view_convert_expr (tree type, tree expr)
7878 {
7879   /* We support up to 512-bit values (for V8DFmode).  */
7880   unsigned char buffer[64];
7881   int len;
7882
7883   /* Check that the host and target are sane.  */
7884   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7885     return NULL_TREE;
7886
7887   len = native_encode_expr (expr, buffer, sizeof (buffer));
7888   if (len == 0)
7889     return NULL_TREE;
7890
7891   return native_interpret_expr (type, buffer, len);
7892 }
7893
7894 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7895    to avoid confusing the gimplify process.  When IN_FOLD is true
7896    avoid modifications of T.  */
7897
7898 static tree
7899 build_fold_addr_expr_with_type_1 (tree t, tree ptrtype, bool in_fold)
7900 {
7901   /* The size of the object is not relevant when talking about its address.  */
7902   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7903     t = TREE_OPERAND (t, 0);
7904
7905   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7906   if (TREE_CODE (t) == INDIRECT_REF
7907       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7908     {
7909       t = TREE_OPERAND (t, 0);
7910
7911       if (TREE_TYPE (t) != ptrtype)
7912         t = build1 (NOP_EXPR, ptrtype, t);
7913     }
7914   else if (!in_fold)
7915     {
7916       tree base = t;
7917
7918       while (handled_component_p (base))
7919         base = TREE_OPERAND (base, 0);
7920
7921       if (DECL_P (base))
7922         TREE_ADDRESSABLE (base) = 1;
7923
7924       t = build1 (ADDR_EXPR, ptrtype, t);
7925     }
7926   else
7927     t = build1 (ADDR_EXPR, ptrtype, t);
7928
7929   return t;
7930 }
7931
7932 /* Build an expression for the address of T with type PTRTYPE.  This
7933    function modifies the input parameter 'T' by sometimes setting the
7934    TREE_ADDRESSABLE flag.  */
7935
7936 tree
7937 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7938 {
7939   return build_fold_addr_expr_with_type_1 (t, ptrtype, false);
7940 }
7941
7942 /* Build an expression for the address of T.  This function modifies
7943    the input parameter 'T' by sometimes setting the TREE_ADDRESSABLE
7944    flag.  When called from fold functions, use fold_addr_expr instead.  */
7945
7946 tree
7947 build_fold_addr_expr (tree t)
7948 {
7949   return build_fold_addr_expr_with_type_1 (t, 
7950                                            build_pointer_type (TREE_TYPE (t)),
7951                                            false);
7952 }
7953
7954 /* Same as build_fold_addr_expr, builds an expression for the address
7955    of T, but avoids touching the input node 't'.  Fold functions
7956    should use this version.  */
7957
7958 static tree
7959 fold_addr_expr (tree t)
7960 {
7961   tree ptrtype = build_pointer_type (TREE_TYPE (t));
7962
7963   return build_fold_addr_expr_with_type_1 (t, ptrtype, true);
7964 }
7965
7966 /* Fold a unary expression of code CODE and type TYPE with operand
7967    OP0.  Return the folded expression if folding is successful.
7968    Otherwise, return NULL_TREE.  */
7969
7970 tree
7971 fold_unary (enum tree_code code, tree type, tree op0)
7972 {
7973   tree tem;
7974   tree arg0;
7975   enum tree_code_class kind = TREE_CODE_CLASS (code);
7976
7977   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7978               && TREE_CODE_LENGTH (code) == 1);
7979
7980   arg0 = op0;
7981   if (arg0)
7982     {
7983       if (code == NOP_EXPR || code == CONVERT_EXPR
7984           || code == FLOAT_EXPR || code == ABS_EXPR)
7985         {
7986           /* Don't use STRIP_NOPS, because signedness of argument type
7987              matters.  */
7988           STRIP_SIGN_NOPS (arg0);
7989         }
7990       else
7991         {
7992           /* Strip any conversions that don't change the mode.  This
7993              is safe for every expression, except for a comparison
7994              expression because its signedness is derived from its
7995              operands.
7996
7997              Note that this is done as an internal manipulation within
7998              the constant folder, in order to find the simplest
7999              representation of the arguments so that their form can be
8000              studied.  In any cases, the appropriate type conversions
8001              should be put back in the tree that will get out of the
8002              constant folder.  */
8003           STRIP_NOPS (arg0);
8004         }
8005     }
8006
8007   if (TREE_CODE_CLASS (code) == tcc_unary)
8008     {
8009       if (TREE_CODE (arg0) == COMPOUND_EXPR)
8010         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
8011                        fold_build1 (code, type, TREE_OPERAND (arg0, 1)));
8012       else if (TREE_CODE (arg0) == COND_EXPR)
8013         {
8014           tree arg01 = TREE_OPERAND (arg0, 1);
8015           tree arg02 = TREE_OPERAND (arg0, 2);
8016           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8017             arg01 = fold_build1 (code, type, arg01);
8018           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8019             arg02 = fold_build1 (code, type, arg02);
8020           tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
8021                              arg01, arg02);
8022
8023           /* If this was a conversion, and all we did was to move into
8024              inside the COND_EXPR, bring it back out.  But leave it if
8025              it is a conversion from integer to integer and the
8026              result precision is no wider than a word since such a
8027              conversion is cheap and may be optimized away by combine,
8028              while it couldn't if it were outside the COND_EXPR.  Then return
8029              so we don't get into an infinite recursion loop taking the
8030              conversion out and then back in.  */
8031
8032           if ((code == NOP_EXPR || code == CONVERT_EXPR
8033                || code == NON_LVALUE_EXPR)
8034               && TREE_CODE (tem) == COND_EXPR
8035               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8036               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8037               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8038               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8039               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8040                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8041               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8042                      && (INTEGRAL_TYPE_P
8043                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8044                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8045                   || flag_syntax_only))
8046             tem = build1 (code, type,
8047                           build3 (COND_EXPR,
8048                                   TREE_TYPE (TREE_OPERAND
8049                                              (TREE_OPERAND (tem, 1), 0)),
8050                                   TREE_OPERAND (tem, 0),
8051                                   TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8052                                   TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8053           return tem;
8054         }
8055       else if (COMPARISON_CLASS_P (arg0))
8056         {
8057           if (TREE_CODE (type) == BOOLEAN_TYPE)
8058             {
8059               arg0 = copy_node (arg0);
8060               TREE_TYPE (arg0) = type;
8061               return arg0;
8062             }
8063           else if (TREE_CODE (type) != INTEGER_TYPE)
8064             return fold_build3 (COND_EXPR, type, arg0,
8065                                 fold_build1 (code, type,
8066                                              integer_one_node),
8067                                 fold_build1 (code, type,
8068                                              integer_zero_node));
8069         }
8070    }
8071
8072   switch (code)
8073     {
8074     case NOP_EXPR:
8075     case FLOAT_EXPR:
8076     case CONVERT_EXPR:
8077     case FIX_TRUNC_EXPR:
8078       if (TREE_TYPE (op0) == type)
8079         return op0;
8080       
8081       /* If we have (type) (a CMP b) and type is an integral type, return
8082          new expression involving the new type.  */
8083       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8084         return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8085                             TREE_OPERAND (op0, 1));
8086
8087       /* Handle cases of two conversions in a row.  */
8088       if (TREE_CODE (op0) == NOP_EXPR
8089           || TREE_CODE (op0) == CONVERT_EXPR)
8090         {
8091           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8092           tree inter_type = TREE_TYPE (op0);
8093           int inside_int = INTEGRAL_TYPE_P (inside_type);
8094           int inside_ptr = POINTER_TYPE_P (inside_type);
8095           int inside_float = FLOAT_TYPE_P (inside_type);
8096           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8097           unsigned int inside_prec = TYPE_PRECISION (inside_type);
8098           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8099           int inter_int = INTEGRAL_TYPE_P (inter_type);
8100           int inter_ptr = POINTER_TYPE_P (inter_type);
8101           int inter_float = FLOAT_TYPE_P (inter_type);
8102           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8103           unsigned int inter_prec = TYPE_PRECISION (inter_type);
8104           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8105           int final_int = INTEGRAL_TYPE_P (type);
8106           int final_ptr = POINTER_TYPE_P (type);
8107           int final_float = FLOAT_TYPE_P (type);
8108           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8109           unsigned int final_prec = TYPE_PRECISION (type);
8110           int final_unsignedp = TYPE_UNSIGNED (type);
8111
8112           /* In addition to the cases of two conversions in a row
8113              handled below, if we are converting something to its own
8114              type via an object of identical or wider precision, neither
8115              conversion is needed.  */
8116           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8117               && (((inter_int || inter_ptr) && final_int)
8118                   || (inter_float && final_float))
8119               && inter_prec >= final_prec)
8120             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8121
8122           /* Likewise, if the intermediate and final types are either both
8123              float or both integer, we don't need the middle conversion if
8124              it is wider than the final type and doesn't change the signedness
8125              (for integers).  Avoid this if the final type is a pointer
8126              since then we sometimes need the inner conversion.  Likewise if
8127              the outer has a precision not equal to the size of its mode.  */
8128           if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
8129                || (inter_float && inside_float)
8130                || (inter_vec && inside_vec))
8131               && inter_prec >= inside_prec
8132               && (inter_float || inter_vec
8133                   || inter_unsignedp == inside_unsignedp)
8134               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8135                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
8136               && ! final_ptr
8137               && (! final_vec || inter_prec == inside_prec))
8138             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8139
8140           /* If we have a sign-extension of a zero-extended value, we can
8141              replace that by a single zero-extension.  */
8142           if (inside_int && inter_int && final_int
8143               && inside_prec < inter_prec && inter_prec < final_prec
8144               && inside_unsignedp && !inter_unsignedp)
8145             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8146
8147           /* Two conversions in a row are not needed unless:
8148              - some conversion is floating-point (overstrict for now), or
8149              - some conversion is a vector (overstrict for now), or
8150              - the intermediate type is narrower than both initial and
8151                final, or
8152              - the intermediate type and innermost type differ in signedness,
8153                and the outermost type is wider than the intermediate, or
8154              - the initial type is a pointer type and the precisions of the
8155                intermediate and final types differ, or
8156              - the final type is a pointer type and the precisions of the
8157                initial and intermediate types differ.
8158              - the final type is a pointer type and the initial type not
8159              - the initial type is a pointer to an array and the final type
8160                not.  */
8161           if (! inside_float && ! inter_float && ! final_float
8162               && ! inside_vec && ! inter_vec && ! final_vec
8163               && (inter_prec >= inside_prec || inter_prec >= final_prec)
8164               && ! (inside_int && inter_int
8165                     && inter_unsignedp != inside_unsignedp
8166                     && inter_prec < final_prec)
8167               && ((inter_unsignedp && inter_prec > inside_prec)
8168                   == (final_unsignedp && final_prec > inter_prec))
8169               && ! (inside_ptr && inter_prec != final_prec)
8170               && ! (final_ptr && inside_prec != inter_prec)
8171               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8172                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
8173               && final_ptr == inside_ptr
8174               && ! (inside_ptr
8175                     && TREE_CODE (TREE_TYPE (inside_type)) == ARRAY_TYPE
8176                     && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8177             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8178         }
8179
8180       /* Handle (T *)&A.B.C for A being of type T and B and C
8181          living at offset zero.  This occurs frequently in
8182          C++ upcasting and then accessing the base.  */
8183       if (TREE_CODE (op0) == ADDR_EXPR
8184           && POINTER_TYPE_P (type)
8185           && handled_component_p (TREE_OPERAND (op0, 0)))
8186         {
8187           HOST_WIDE_INT bitsize, bitpos;
8188           tree offset;
8189           enum machine_mode mode;
8190           int unsignedp, volatilep;
8191           tree base = TREE_OPERAND (op0, 0);
8192           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8193                                       &mode, &unsignedp, &volatilep, false);
8194           /* If the reference was to a (constant) zero offset, we can use
8195              the address of the base if it has the same base type
8196              as the result type.  */
8197           if (! offset && bitpos == 0
8198               && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8199                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8200             return fold_convert (type, fold_addr_expr (base));
8201         }
8202
8203       if ((TREE_CODE (op0) == MODIFY_EXPR
8204            || TREE_CODE (op0) == GIMPLE_MODIFY_STMT)
8205           && TREE_CONSTANT (GENERIC_TREE_OPERAND (op0, 1))
8206           /* Detect assigning a bitfield.  */
8207           && !(TREE_CODE (GENERIC_TREE_OPERAND (op0, 0)) == COMPONENT_REF
8208                && DECL_BIT_FIELD
8209                (TREE_OPERAND (GENERIC_TREE_OPERAND (op0, 0), 1))))
8210         {
8211           /* Don't leave an assignment inside a conversion
8212              unless assigning a bitfield.  */
8213           tem = fold_build1 (code, type, GENERIC_TREE_OPERAND (op0, 1));
8214           /* First do the assignment, then return converted constant.  */
8215           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8216           TREE_NO_WARNING (tem) = 1;
8217           TREE_USED (tem) = 1;
8218           return tem;
8219         }
8220
8221       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8222          constants (if x has signed type, the sign bit cannot be set
8223          in c).  This folds extension into the BIT_AND_EXPR.  */
8224       if (INTEGRAL_TYPE_P (type)
8225           && TREE_CODE (type) != BOOLEAN_TYPE
8226           && TREE_CODE (op0) == BIT_AND_EXPR
8227           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
8228         {
8229           tree and = op0;
8230           tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
8231           int change = 0;
8232
8233           if (TYPE_UNSIGNED (TREE_TYPE (and))
8234               || (TYPE_PRECISION (type)
8235                   <= TYPE_PRECISION (TREE_TYPE (and))))
8236             change = 1;
8237           else if (TYPE_PRECISION (TREE_TYPE (and1))
8238                    <= HOST_BITS_PER_WIDE_INT
8239                    && host_integerp (and1, 1))
8240             {
8241               unsigned HOST_WIDE_INT cst;
8242
8243               cst = tree_low_cst (and1, 1);
8244               cst &= (HOST_WIDE_INT) -1
8245                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8246               change = (cst == 0);
8247 #ifdef LOAD_EXTEND_OP
8248               if (change
8249                   && !flag_syntax_only
8250                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8251                       == ZERO_EXTEND))
8252                 {
8253                   tree uns = unsigned_type_for (TREE_TYPE (and0));
8254                   and0 = fold_convert (uns, and0);
8255                   and1 = fold_convert (uns, and1);
8256                 }
8257 #endif
8258             }
8259           if (change)
8260             {
8261               tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8262                                            TREE_INT_CST_HIGH (and1), 0,
8263                                            TREE_OVERFLOW (and1));
8264               return fold_build2 (BIT_AND_EXPR, type,
8265                                   fold_convert (type, and0), tem);
8266             }
8267         }
8268
8269       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8270          when one of the new casts will fold away. Conservatively we assume
8271          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8272       if (POINTER_TYPE_P (type)
8273           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8274           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8275               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8276               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8277         {
8278           tree arg00 = TREE_OPERAND (arg0, 0);
8279           tree arg01 = TREE_OPERAND (arg0, 1);
8280
8281           return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
8282                               fold_convert (sizetype, arg01));
8283         }
8284
8285       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8286          of the same precision, and X is an integer type not narrower than
8287          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
8288       if (INTEGRAL_TYPE_P (type)
8289           && TREE_CODE (op0) == BIT_NOT_EXPR
8290           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8291           && (TREE_CODE (TREE_OPERAND (op0, 0)) == NOP_EXPR
8292               || TREE_CODE (TREE_OPERAND (op0, 0)) == CONVERT_EXPR)
8293           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8294         {
8295           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8296           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8297               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8298             return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
8299         }
8300
8301       tem = fold_convert_const (code, type, op0);
8302       return tem ? tem : NULL_TREE;
8303
8304     case FIXED_CONVERT_EXPR:
8305       tem = fold_convert_const (code, type, arg0);
8306       return tem ? tem : NULL_TREE;
8307
8308     case VIEW_CONVERT_EXPR:
8309       if (TREE_TYPE (op0) == type)
8310         return op0;
8311       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8312         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8313       return fold_view_convert_expr (type, op0);
8314
8315     case NEGATE_EXPR:
8316       tem = fold_negate_expr (arg0);
8317       if (tem)
8318         return fold_convert (type, tem);
8319       return NULL_TREE;
8320
8321     case ABS_EXPR:
8322       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8323         return fold_abs_const (arg0, type);
8324       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8325         return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8326       /* Convert fabs((double)float) into (double)fabsf(float).  */
8327       else if (TREE_CODE (arg0) == NOP_EXPR
8328                && TREE_CODE (type) == REAL_TYPE)
8329         {
8330           tree targ0 = strip_float_extensions (arg0);
8331           if (targ0 != arg0)
8332             return fold_convert (type, fold_build1 (ABS_EXPR,
8333                                                     TREE_TYPE (targ0),
8334                                                     targ0));
8335         }
8336       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8337       else if (TREE_CODE (arg0) == ABS_EXPR)
8338         return arg0;
8339       else if (tree_expr_nonnegative_p (arg0))
8340         return arg0;
8341
8342       /* Strip sign ops from argument.  */
8343       if (TREE_CODE (type) == REAL_TYPE)
8344         {
8345           tem = fold_strip_sign_ops (arg0);
8346           if (tem)
8347             return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8348         }
8349       return NULL_TREE;
8350
8351     case CONJ_EXPR:
8352       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8353         return fold_convert (type, arg0);
8354       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8355         {
8356           tree itype = TREE_TYPE (type);
8357           tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8358           tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8359           return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8360         }
8361       if (TREE_CODE (arg0) == COMPLEX_CST)
8362         {
8363           tree itype = TREE_TYPE (type);
8364           tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8365           tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8366           return build_complex (type, rpart, negate_expr (ipart));
8367         }
8368       if (TREE_CODE (arg0) == CONJ_EXPR)
8369         return fold_convert (type, TREE_OPERAND (arg0, 0));
8370       return NULL_TREE;
8371
8372     case BIT_NOT_EXPR:
8373       if (TREE_CODE (arg0) == INTEGER_CST)
8374         return fold_not_const (arg0, type);
8375       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8376         return TREE_OPERAND (arg0, 0);
8377       /* Convert ~ (-A) to A - 1.  */
8378       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8379         return fold_build2 (MINUS_EXPR, type, TREE_OPERAND (arg0, 0),
8380                             build_int_cst (type, 1));
8381       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8382       else if (INTEGRAL_TYPE_P (type)
8383                && ((TREE_CODE (arg0) == MINUS_EXPR
8384                     && integer_onep (TREE_OPERAND (arg0, 1)))
8385                    || (TREE_CODE (arg0) == PLUS_EXPR
8386                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8387         return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
8388       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8389       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8390                && (tem = fold_unary (BIT_NOT_EXPR, type,
8391                                      fold_convert (type,
8392                                                    TREE_OPERAND (arg0, 0)))))
8393         return fold_build2 (BIT_XOR_EXPR, type, tem,
8394                             fold_convert (type, TREE_OPERAND (arg0, 1)));
8395       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8396                && (tem = fold_unary (BIT_NOT_EXPR, type,
8397                                      fold_convert (type,
8398                                                    TREE_OPERAND (arg0, 1)))))
8399         return fold_build2 (BIT_XOR_EXPR, type,
8400                             fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8401       /* Perform BIT_NOT_EXPR on each element individually.  */
8402       else if (TREE_CODE (arg0) == VECTOR_CST)
8403         {
8404           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8405           int count = TYPE_VECTOR_SUBPARTS (type), i;
8406
8407           for (i = 0; i < count; i++)
8408             {
8409               if (elements)
8410                 {
8411                   elem = TREE_VALUE (elements);
8412                   elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8413                   if (elem == NULL_TREE)
8414                     break;
8415                   elements = TREE_CHAIN (elements);
8416                 }
8417               else
8418                 elem = build_int_cst (TREE_TYPE (type), -1);
8419               list = tree_cons (NULL_TREE, elem, list);
8420             }
8421           if (i == count)
8422             return build_vector (type, nreverse (list));
8423         }
8424
8425       return NULL_TREE;
8426
8427     case TRUTH_NOT_EXPR:
8428       /* The argument to invert_truthvalue must have Boolean type.  */
8429       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8430           arg0 = fold_convert (boolean_type_node, arg0);
8431
8432       /* Note that the operand of this must be an int
8433          and its values must be 0 or 1.
8434          ("true" is a fixed value perhaps depending on the language,
8435          but we don't handle values other than 1 correctly yet.)  */
8436       tem = fold_truth_not_expr (arg0);
8437       if (!tem)
8438         return NULL_TREE;
8439       return fold_convert (type, tem);
8440
8441     case REALPART_EXPR:
8442       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8443         return fold_convert (type, arg0);
8444       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8445         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8446                                  TREE_OPERAND (arg0, 1));
8447       if (TREE_CODE (arg0) == COMPLEX_CST)
8448         return fold_convert (type, TREE_REALPART (arg0));
8449       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8450         {
8451           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8452           tem = fold_build2 (TREE_CODE (arg0), itype,
8453                              fold_build1 (REALPART_EXPR, itype,
8454                                           TREE_OPERAND (arg0, 0)),
8455                              fold_build1 (REALPART_EXPR, itype,
8456                                           TREE_OPERAND (arg0, 1)));
8457           return fold_convert (type, tem);
8458         }
8459       if (TREE_CODE (arg0) == CONJ_EXPR)
8460         {
8461           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8462           tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8463           return fold_convert (type, tem);
8464         }
8465       if (TREE_CODE (arg0) == CALL_EXPR)
8466         {
8467           tree fn = get_callee_fndecl (arg0);
8468           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8469             switch (DECL_FUNCTION_CODE (fn))
8470               {
8471               CASE_FLT_FN (BUILT_IN_CEXPI):
8472                 fn = mathfn_built_in (type, BUILT_IN_COS);
8473                 if (fn)
8474                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8475                 break;
8476
8477               default:
8478                 break;
8479               }
8480         }
8481       return NULL_TREE;
8482
8483     case IMAGPART_EXPR:
8484       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8485         return fold_convert (type, integer_zero_node);
8486       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8487         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8488                                  TREE_OPERAND (arg0, 0));
8489       if (TREE_CODE (arg0) == COMPLEX_CST)
8490         return fold_convert (type, TREE_IMAGPART (arg0));
8491       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8492         {
8493           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8494           tem = fold_build2 (TREE_CODE (arg0), itype,
8495                              fold_build1 (IMAGPART_EXPR, itype,
8496                                           TREE_OPERAND (arg0, 0)),
8497                              fold_build1 (IMAGPART_EXPR, itype,
8498                                           TREE_OPERAND (arg0, 1)));
8499           return fold_convert (type, tem);
8500         }
8501       if (TREE_CODE (arg0) == CONJ_EXPR)
8502         {
8503           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8504           tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8505           return fold_convert (type, negate_expr (tem));
8506         }
8507       if (TREE_CODE (arg0) == CALL_EXPR)
8508         {
8509           tree fn = get_callee_fndecl (arg0);
8510           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8511             switch (DECL_FUNCTION_CODE (fn))
8512               {
8513               CASE_FLT_FN (BUILT_IN_CEXPI):
8514                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8515                 if (fn)
8516                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8517                 break;
8518
8519               default:
8520                 break;
8521               }
8522         }
8523       return NULL_TREE;
8524
8525     default:
8526       return NULL_TREE;
8527     } /* switch (code) */
8528 }
8529
8530 /* Fold a binary expression of code CODE and type TYPE with operands
8531    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8532    Return the folded expression if folding is successful.  Otherwise,
8533    return NULL_TREE.  */
8534
8535 static tree
8536 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8537 {
8538   enum tree_code compl_code;
8539
8540   if (code == MIN_EXPR)
8541     compl_code = MAX_EXPR;
8542   else if (code == MAX_EXPR)
8543     compl_code = MIN_EXPR;
8544   else
8545     gcc_unreachable ();
8546
8547   /* MIN (MAX (a, b), b) == b.  */
8548   if (TREE_CODE (op0) == compl_code
8549       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8550     return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8551
8552   /* MIN (MAX (b, a), b) == b.  */
8553   if (TREE_CODE (op0) == compl_code
8554       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8555       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8556     return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8557
8558   /* MIN (a, MAX (a, b)) == a.  */
8559   if (TREE_CODE (op1) == compl_code
8560       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8561       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8562     return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8563
8564   /* MIN (a, MAX (b, a)) == a.  */
8565   if (TREE_CODE (op1) == compl_code
8566       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8567       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8568     return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8569
8570   return NULL_TREE;
8571 }
8572
8573 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8574    by changing CODE to reduce the magnitude of constants involved in
8575    ARG0 of the comparison.
8576    Returns a canonicalized comparison tree if a simplification was
8577    possible, otherwise returns NULL_TREE.
8578    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8579    valid if signed overflow is undefined.  */
8580
8581 static tree
8582 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8583                                  tree arg0, tree arg1,
8584                                  bool *strict_overflow_p)
8585 {
8586   enum tree_code code0 = TREE_CODE (arg0);
8587   tree t, cst0 = NULL_TREE;
8588   int sgn0;
8589   bool swap = false;
8590
8591   /* Match A +- CST code arg1 and CST code arg1.  */
8592   if (!(((code0 == MINUS_EXPR
8593           || code0 == PLUS_EXPR)
8594          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8595         || code0 == INTEGER_CST))
8596     return NULL_TREE;
8597
8598   /* Identify the constant in arg0 and its sign.  */
8599   if (code0 == INTEGER_CST)
8600     cst0 = arg0;
8601   else
8602     cst0 = TREE_OPERAND (arg0, 1);
8603   sgn0 = tree_int_cst_sgn (cst0);
8604
8605   /* Overflowed constants and zero will cause problems.  */
8606   if (integer_zerop (cst0)
8607       || TREE_OVERFLOW (cst0))
8608     return NULL_TREE;
8609
8610   /* See if we can reduce the magnitude of the constant in
8611      arg0 by changing the comparison code.  */
8612   if (code0 == INTEGER_CST)
8613     {
8614       /* CST <= arg1  ->  CST-1 < arg1.  */
8615       if (code == LE_EXPR && sgn0 == 1)
8616         code = LT_EXPR;
8617       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8618       else if (code == LT_EXPR && sgn0 == -1)
8619         code = LE_EXPR;
8620       /* CST > arg1  ->  CST-1 >= arg1.  */
8621       else if (code == GT_EXPR && sgn0 == 1)
8622         code = GE_EXPR;
8623       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8624       else if (code == GE_EXPR && sgn0 == -1)
8625         code = GT_EXPR;
8626       else
8627         return NULL_TREE;
8628       /* arg1 code' CST' might be more canonical.  */
8629       swap = true;
8630     }
8631   else
8632     {
8633       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8634       if (code == LT_EXPR
8635           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8636         code = LE_EXPR;
8637       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8638       else if (code == GT_EXPR
8639                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8640         code = GE_EXPR;
8641       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8642       else if (code == LE_EXPR
8643                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8644         code = LT_EXPR;
8645       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8646       else if (code == GE_EXPR
8647                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8648         code = GT_EXPR;
8649       else
8650         return NULL_TREE;
8651       *strict_overflow_p = true;
8652     }
8653
8654   /* Now build the constant reduced in magnitude.  */
8655   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8656                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8657   if (code0 != INTEGER_CST)
8658     t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8659
8660   /* If swapping might yield to a more canonical form, do so.  */
8661   if (swap)
8662     return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8663   else
8664     return fold_build2 (code, type, t, arg1);
8665 }
8666
8667 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8668    overflow further.  Try to decrease the magnitude of constants involved
8669    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8670    and put sole constants at the second argument position.
8671    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8672
8673 static tree
8674 maybe_canonicalize_comparison (enum tree_code code, tree type,
8675                                tree arg0, tree arg1)
8676 {
8677   tree t;
8678   bool strict_overflow_p;
8679   const char * const warnmsg = G_("assuming signed overflow does not occur "
8680                                   "when reducing constant in comparison");
8681
8682   /* In principle pointers also have undefined overflow behavior,
8683      but that causes problems elsewhere.  */
8684   if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8685       || POINTER_TYPE_P (TREE_TYPE (arg0)))
8686     return NULL_TREE;
8687
8688   /* Try canonicalization by simplifying arg0.  */
8689   strict_overflow_p = false;
8690   t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8691                                        &strict_overflow_p);
8692   if (t)
8693     {
8694       if (strict_overflow_p)
8695         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8696       return t;
8697     }
8698
8699   /* Try canonicalization by simplifying arg1 using the swapped
8700      comparison.  */
8701   code = swap_tree_comparison (code);
8702   strict_overflow_p = false;
8703   t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8704                                        &strict_overflow_p);
8705   if (t && strict_overflow_p)
8706     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8707   return t;
8708 }
8709
8710 /* Subroutine of fold_binary.  This routine performs all of the
8711    transformations that are common to the equality/inequality
8712    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8713    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8714    fold_binary should call fold_binary.  Fold a comparison with
8715    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8716    the folded comparison or NULL_TREE.  */
8717
8718 static tree
8719 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8720 {
8721   tree arg0, arg1, tem;
8722
8723   arg0 = op0;
8724   arg1 = op1;
8725
8726   STRIP_SIGN_NOPS (arg0);
8727   STRIP_SIGN_NOPS (arg1);
8728
8729   tem = fold_relational_const (code, type, arg0, arg1);
8730   if (tem != NULL_TREE)
8731     return tem;
8732
8733   /* If one arg is a real or integer constant, put it last.  */
8734   if (tree_swap_operands_p (arg0, arg1, true))
8735     return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8736
8737   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8738   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8739       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8740           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8741           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8742       && (TREE_CODE (arg1) == INTEGER_CST
8743           && !TREE_OVERFLOW (arg1)))
8744     {
8745       tree const1 = TREE_OPERAND (arg0, 1);
8746       tree const2 = arg1;
8747       tree variable = TREE_OPERAND (arg0, 0);
8748       tree lhs;
8749       int lhs_add;
8750       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8751
8752       lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8753                          TREE_TYPE (arg1), const2, const1);
8754
8755       /* If the constant operation overflowed this can be
8756          simplified as a comparison against INT_MAX/INT_MIN.  */
8757       if (TREE_CODE (lhs) == INTEGER_CST
8758           && TREE_OVERFLOW (lhs))
8759         {
8760           int const1_sgn = tree_int_cst_sgn (const1);
8761           enum tree_code code2 = code;
8762
8763           /* Get the sign of the constant on the lhs if the
8764              operation were VARIABLE + CONST1.  */
8765           if (TREE_CODE (arg0) == MINUS_EXPR)
8766             const1_sgn = -const1_sgn;
8767
8768           /* The sign of the constant determines if we overflowed
8769              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8770              Canonicalize to the INT_MIN overflow by swapping the comparison
8771              if necessary.  */
8772           if (const1_sgn == -1)
8773             code2 = swap_tree_comparison (code);
8774
8775           /* We now can look at the canonicalized case
8776                VARIABLE + 1  CODE2  INT_MIN
8777              and decide on the result.  */
8778           if (code2 == LT_EXPR
8779               || code2 == LE_EXPR
8780               || code2 == EQ_EXPR)
8781             return omit_one_operand (type, boolean_false_node, variable);
8782           else if (code2 == NE_EXPR
8783                    || code2 == GE_EXPR
8784                    || code2 == GT_EXPR)
8785             return omit_one_operand (type, boolean_true_node, variable);
8786         }
8787
8788       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8789           && (TREE_CODE (lhs) != INTEGER_CST
8790               || !TREE_OVERFLOW (lhs)))
8791         {
8792           fold_overflow_warning (("assuming signed overflow does not occur "
8793                                   "when changing X +- C1 cmp C2 to "
8794                                   "X cmp C1 +- C2"),
8795                                  WARN_STRICT_OVERFLOW_COMPARISON);
8796           return fold_build2 (code, type, variable, lhs);
8797         }
8798     }
8799
8800   /* For comparisons of pointers we can decompose it to a compile time
8801      comparison of the base objects and the offsets into the object.
8802      This requires at least one operand being an ADDR_EXPR to do more
8803      than the operand_equal_p test below.  */
8804   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8805       && (TREE_CODE (arg0) == ADDR_EXPR
8806           || TREE_CODE (arg1) == ADDR_EXPR))
8807     {
8808       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8809       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8810       enum machine_mode mode;
8811       int volatilep, unsignedp;
8812       bool indirect_base0 = false;
8813
8814       /* Get base and offset for the access.  Strip ADDR_EXPR for
8815          get_inner_reference, but put it back by stripping INDIRECT_REF
8816          off the base object if possible.  */
8817       base0 = arg0;
8818       if (TREE_CODE (arg0) == ADDR_EXPR)
8819         {
8820           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8821                                        &bitsize, &bitpos0, &offset0, &mode,
8822                                        &unsignedp, &volatilep, false);
8823           if (TREE_CODE (base0) == INDIRECT_REF)
8824             base0 = TREE_OPERAND (base0, 0);
8825           else
8826             indirect_base0 = true;
8827         }
8828
8829       base1 = arg1;
8830       if (TREE_CODE (arg1) == ADDR_EXPR)
8831         {
8832           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8833                                        &bitsize, &bitpos1, &offset1, &mode,
8834                                        &unsignedp, &volatilep, false);
8835           /* We have to make sure to have an indirect/non-indirect base1
8836              just the same as we did for base0.  */
8837           if (TREE_CODE (base1) == INDIRECT_REF
8838               && !indirect_base0)
8839             base1 = TREE_OPERAND (base1, 0);
8840           else if (!indirect_base0)
8841             base1 = NULL_TREE;
8842         }
8843       else if (indirect_base0)
8844         base1 = NULL_TREE;
8845
8846       /* If we have equivalent bases we might be able to simplify.  */
8847       if (base0 && base1
8848           && operand_equal_p (base0, base1, 0))
8849         {
8850           /* We can fold this expression to a constant if the non-constant
8851              offset parts are equal.  */
8852           if (offset0 == offset1
8853               || (offset0 && offset1
8854                   && operand_equal_p (offset0, offset1, 0)))
8855             {
8856               switch (code)
8857                 {
8858                 case EQ_EXPR:
8859                   return build_int_cst (boolean_type_node, bitpos0 == bitpos1);
8860                 case NE_EXPR:
8861                   return build_int_cst (boolean_type_node, bitpos0 != bitpos1);
8862                 case LT_EXPR:
8863                   return build_int_cst (boolean_type_node, bitpos0 < bitpos1);
8864                 case LE_EXPR:
8865                   return build_int_cst (boolean_type_node, bitpos0 <= bitpos1);
8866                 case GE_EXPR:
8867                   return build_int_cst (boolean_type_node, bitpos0 >= bitpos1);
8868                 case GT_EXPR:
8869                   return build_int_cst (boolean_type_node, bitpos0 > bitpos1);
8870                 default:;
8871                 }
8872             }
8873           /* We can simplify the comparison to a comparison of the variable
8874              offset parts if the constant offset parts are equal.
8875              Be careful to use signed size type here because otherwise we
8876              mess with array offsets in the wrong way.  This is possible
8877              because pointer arithmetic is restricted to retain within an
8878              object and overflow on pointer differences is undefined as of
8879              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
8880           else if (bitpos0 == bitpos1)
8881             {
8882               tree signed_size_type_node;
8883               signed_size_type_node = signed_type_for (size_type_node);
8884
8885               /* By converting to signed size type we cover middle-end pointer
8886                  arithmetic which operates on unsigned pointer types of size
8887                  type size and ARRAY_REF offsets which are properly sign or
8888                  zero extended from their type in case it is narrower than
8889                  size type.  */
8890               if (offset0 == NULL_TREE)
8891                 offset0 = build_int_cst (signed_size_type_node, 0);
8892               else
8893                 offset0 = fold_convert (signed_size_type_node, offset0);
8894               if (offset1 == NULL_TREE)
8895                 offset1 = build_int_cst (signed_size_type_node, 0);
8896               else
8897                 offset1 = fold_convert (signed_size_type_node, offset1);
8898
8899               return fold_build2 (code, type, offset0, offset1);
8900             }
8901         }
8902     }
8903
8904   /* If this is a comparison of two exprs that look like an ARRAY_REF of the
8905      same object, then we can fold this to a comparison of the two offsets in
8906      signed size type.  This is possible because pointer arithmetic is
8907      restricted to retain within an object and overflow on pointer differences
8908      is undefined as of 6.5.6/8 and /9 with respect to the signed ptrdiff_t.
8909
8910      We check flag_wrapv directly because pointers types are unsigned,
8911      and therefore TYPE_OVERFLOW_WRAPS returns true for them.  That is
8912      normally what we want to avoid certain odd overflow cases, but
8913      not here.  */
8914   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8915       && !flag_wrapv
8916       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (arg0)))
8917     {
8918       tree base0, offset0, base1, offset1;
8919
8920       if (extract_array_ref (arg0, &base0, &offset0)
8921           && extract_array_ref (arg1, &base1, &offset1)
8922           && operand_equal_p (base0, base1, 0))
8923         {
8924           tree signed_size_type_node;
8925           signed_size_type_node = signed_type_for (size_type_node);
8926
8927           /* By converting to signed size type we cover middle-end pointer
8928              arithmetic which operates on unsigned pointer types of size
8929              type size and ARRAY_REF offsets which are properly sign or
8930              zero extended from their type in case it is narrower than
8931              size type.  */
8932           if (offset0 == NULL_TREE)
8933             offset0 = build_int_cst (signed_size_type_node, 0);
8934           else
8935             offset0 = fold_convert (signed_size_type_node, offset0);
8936           if (offset1 == NULL_TREE)
8937             offset1 = build_int_cst (signed_size_type_node, 0);
8938           else
8939             offset1 = fold_convert (signed_size_type_node, offset1);
8940
8941           return fold_build2 (code, type, offset0, offset1);
8942         }
8943     }
8944
8945   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8946      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
8947      the resulting offset is smaller in absolute value than the
8948      original one.  */
8949   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8950       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8951       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8952           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8953       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8954       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8955           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8956     {
8957       tree const1 = TREE_OPERAND (arg0, 1);
8958       tree const2 = TREE_OPERAND (arg1, 1);
8959       tree variable1 = TREE_OPERAND (arg0, 0);
8960       tree variable2 = TREE_OPERAND (arg1, 0);
8961       tree cst;
8962       const char * const warnmsg = G_("assuming signed overflow does not "
8963                                       "occur when combining constants around "
8964                                       "a comparison");
8965
8966       /* Put the constant on the side where it doesn't overflow and is
8967          of lower absolute value than before.  */
8968       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8969                              ? MINUS_EXPR : PLUS_EXPR,
8970                              const2, const1, 0);
8971       if (!TREE_OVERFLOW (cst)
8972           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8973         {
8974           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8975           return fold_build2 (code, type,
8976                               variable1,
8977                               fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
8978                                            variable2, cst));
8979         }
8980
8981       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8982                              ? MINUS_EXPR : PLUS_EXPR,
8983                              const1, const2, 0);
8984       if (!TREE_OVERFLOW (cst)
8985           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8986         {
8987           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8988           return fold_build2 (code, type,
8989                               fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
8990                                            variable1, cst),
8991                               variable2);
8992         }
8993     }
8994
8995   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8996      signed arithmetic case.  That form is created by the compiler
8997      often enough for folding it to be of value.  One example is in
8998      computing loop trip counts after Operator Strength Reduction.  */
8999   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9000       && TREE_CODE (arg0) == MULT_EXPR
9001       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9002           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9003       && integer_zerop (arg1))
9004     {
9005       tree const1 = TREE_OPERAND (arg0, 1);
9006       tree const2 = arg1;                       /* zero */
9007       tree variable1 = TREE_OPERAND (arg0, 0);
9008       enum tree_code cmp_code = code;
9009
9010       gcc_assert (!integer_zerop (const1));
9011
9012       fold_overflow_warning (("assuming signed overflow does not occur when "
9013                               "eliminating multiplication in comparison "
9014                               "with zero"),
9015                              WARN_STRICT_OVERFLOW_COMPARISON);
9016
9017       /* If const1 is negative we swap the sense of the comparison.  */
9018       if (tree_int_cst_sgn (const1) < 0)
9019         cmp_code = swap_tree_comparison (cmp_code);
9020
9021       return fold_build2 (cmp_code, type, variable1, const2);
9022     }
9023
9024   tem = maybe_canonicalize_comparison (code, type, op0, op1);
9025   if (tem)
9026     return tem;
9027
9028   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9029     {
9030       tree targ0 = strip_float_extensions (arg0);
9031       tree targ1 = strip_float_extensions (arg1);
9032       tree newtype = TREE_TYPE (targ0);
9033
9034       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9035         newtype = TREE_TYPE (targ1);
9036
9037       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9038       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9039         return fold_build2 (code, type, fold_convert (newtype, targ0),
9040                             fold_convert (newtype, targ1));
9041
9042       /* (-a) CMP (-b) -> b CMP a  */
9043       if (TREE_CODE (arg0) == NEGATE_EXPR
9044           && TREE_CODE (arg1) == NEGATE_EXPR)
9045         return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
9046                             TREE_OPERAND (arg0, 0));
9047
9048       if (TREE_CODE (arg1) == REAL_CST)
9049         {
9050           REAL_VALUE_TYPE cst;
9051           cst = TREE_REAL_CST (arg1);
9052
9053           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
9054           if (TREE_CODE (arg0) == NEGATE_EXPR)
9055             return fold_build2 (swap_tree_comparison (code), type,
9056                                 TREE_OPERAND (arg0, 0),
9057                                 build_real (TREE_TYPE (arg1),
9058                                             REAL_VALUE_NEGATE (cst)));
9059
9060           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
9061           /* a CMP (-0) -> a CMP 0  */
9062           if (REAL_VALUE_MINUS_ZERO (cst))
9063             return fold_build2 (code, type, arg0,
9064                                 build_real (TREE_TYPE (arg1), dconst0));
9065
9066           /* x != NaN is always true, other ops are always false.  */
9067           if (REAL_VALUE_ISNAN (cst)
9068               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9069             {
9070               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9071               return omit_one_operand (type, tem, arg0);
9072             }
9073
9074           /* Fold comparisons against infinity.  */
9075           if (REAL_VALUE_ISINF (cst))
9076             {
9077               tem = fold_inf_compare (code, type, arg0, arg1);
9078               if (tem != NULL_TREE)
9079                 return tem;
9080             }
9081         }
9082
9083       /* If this is a comparison of a real constant with a PLUS_EXPR
9084          or a MINUS_EXPR of a real constant, we can convert it into a
9085          comparison with a revised real constant as long as no overflow
9086          occurs when unsafe_math_optimizations are enabled.  */
9087       if (flag_unsafe_math_optimizations
9088           && TREE_CODE (arg1) == REAL_CST
9089           && (TREE_CODE (arg0) == PLUS_EXPR
9090               || TREE_CODE (arg0) == MINUS_EXPR)
9091           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9092           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9093                                       ? MINUS_EXPR : PLUS_EXPR,
9094                                       arg1, TREE_OPERAND (arg0, 1), 0))
9095           && !TREE_OVERFLOW (tem))
9096         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9097
9098       /* Likewise, we can simplify a comparison of a real constant with
9099          a MINUS_EXPR whose first operand is also a real constant, i.e.
9100          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on 
9101          floating-point types only if -fassociative-math is set.  */
9102       if (flag_associative_math
9103           && TREE_CODE (arg1) == REAL_CST
9104           && TREE_CODE (arg0) == MINUS_EXPR
9105           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9106           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9107                                       arg1, 0))
9108           && !TREE_OVERFLOW (tem))
9109         return fold_build2 (swap_tree_comparison (code), type,
9110                             TREE_OPERAND (arg0, 1), tem);
9111
9112       /* Fold comparisons against built-in math functions.  */
9113       if (TREE_CODE (arg1) == REAL_CST
9114           && flag_unsafe_math_optimizations
9115           && ! flag_errno_math)
9116         {
9117           enum built_in_function fcode = builtin_mathfn_code (arg0);
9118
9119           if (fcode != END_BUILTINS)
9120             {
9121               tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
9122               if (tem != NULL_TREE)
9123                 return tem;
9124             }
9125         }
9126     }
9127
9128   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9129       && (TREE_CODE (arg0) == NOP_EXPR
9130           || TREE_CODE (arg0) == CONVERT_EXPR))
9131     {
9132       /* If we are widening one operand of an integer comparison,
9133          see if the other operand is similarly being widened.  Perhaps we
9134          can do the comparison in the narrower type.  */
9135       tem = fold_widened_comparison (code, type, arg0, arg1);
9136       if (tem)
9137         return tem;
9138
9139       /* Or if we are changing signedness.  */
9140       tem = fold_sign_changed_comparison (code, type, arg0, arg1);
9141       if (tem)
9142         return tem;
9143     }
9144
9145   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9146      constant, we can simplify it.  */
9147   if (TREE_CODE (arg1) == INTEGER_CST
9148       && (TREE_CODE (arg0) == MIN_EXPR
9149           || TREE_CODE (arg0) == MAX_EXPR)
9150       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9151     {
9152       tem = optimize_minmax_comparison (code, type, op0, op1);
9153       if (tem)
9154         return tem;
9155     }
9156
9157   /* Simplify comparison of something with itself.  (For IEEE
9158      floating-point, we can only do some of these simplifications.)  */
9159   if (operand_equal_p (arg0, arg1, 0))
9160     {
9161       switch (code)
9162         {
9163         case EQ_EXPR:
9164           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9165               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9166             return constant_boolean_node (1, type);
9167           break;
9168
9169         case GE_EXPR:
9170         case LE_EXPR:
9171           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9172               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9173             return constant_boolean_node (1, type);
9174           return fold_build2 (EQ_EXPR, type, arg0, arg1);
9175
9176         case NE_EXPR:
9177           /* For NE, we can only do this simplification if integer
9178              or we don't honor IEEE floating point NaNs.  */
9179           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9180               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9181             break;
9182           /* ... fall through ...  */
9183         case GT_EXPR:
9184         case LT_EXPR:
9185           return constant_boolean_node (0, type);
9186         default:
9187           gcc_unreachable ();
9188         }
9189     }
9190
9191   /* If we are comparing an expression that just has comparisons
9192      of two integer values, arithmetic expressions of those comparisons,
9193      and constants, we can simplify it.  There are only three cases
9194      to check: the two values can either be equal, the first can be
9195      greater, or the second can be greater.  Fold the expression for
9196      those three values.  Since each value must be 0 or 1, we have
9197      eight possibilities, each of which corresponds to the constant 0
9198      or 1 or one of the six possible comparisons.
9199
9200      This handles common cases like (a > b) == 0 but also handles
9201      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9202      occur in macroized code.  */
9203
9204   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9205     {
9206       tree cval1 = 0, cval2 = 0;
9207       int save_p = 0;
9208
9209       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9210           /* Don't handle degenerate cases here; they should already
9211              have been handled anyway.  */
9212           && cval1 != 0 && cval2 != 0
9213           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9214           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9215           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9216           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9217           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9218           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9219                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9220         {
9221           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9222           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9223
9224           /* We can't just pass T to eval_subst in case cval1 or cval2
9225              was the same as ARG1.  */
9226
9227           tree high_result
9228                 = fold_build2 (code, type,
9229                                eval_subst (arg0, cval1, maxval,
9230                                            cval2, minval),
9231                                arg1);
9232           tree equal_result
9233                 = fold_build2 (code, type,
9234                                eval_subst (arg0, cval1, maxval,
9235                                            cval2, maxval),
9236                                arg1);
9237           tree low_result
9238                 = fold_build2 (code, type,
9239                                eval_subst (arg0, cval1, minval,
9240                                            cval2, maxval),
9241                                arg1);
9242
9243           /* All three of these results should be 0 or 1.  Confirm they are.
9244              Then use those values to select the proper code to use.  */
9245
9246           if (TREE_CODE (high_result) == INTEGER_CST
9247               && TREE_CODE (equal_result) == INTEGER_CST
9248               && TREE_CODE (low_result) == INTEGER_CST)
9249             {
9250               /* Make a 3-bit mask with the high-order bit being the
9251                  value for `>', the next for '=', and the low for '<'.  */
9252               switch ((integer_onep (high_result) * 4)
9253                       + (integer_onep (equal_result) * 2)
9254                       + integer_onep (low_result))
9255                 {
9256                 case 0:
9257                   /* Always false.  */
9258                   return omit_one_operand (type, integer_zero_node, arg0);
9259                 case 1:
9260                   code = LT_EXPR;
9261                   break;
9262                 case 2:
9263                   code = EQ_EXPR;
9264                   break;
9265                 case 3:
9266                   code = LE_EXPR;
9267                   break;
9268                 case 4:
9269                   code = GT_EXPR;
9270                   break;
9271                 case 5:
9272                   code = NE_EXPR;
9273                   break;
9274                 case 6:
9275                   code = GE_EXPR;
9276                   break;
9277                 case 7:
9278                   /* Always true.  */
9279                   return omit_one_operand (type, integer_one_node, arg0);
9280                 }
9281
9282               if (save_p)
9283                 return save_expr (build2 (code, type, cval1, cval2));
9284               return fold_build2 (code, type, cval1, cval2);
9285             }
9286         }
9287     }
9288
9289   /* Fold a comparison of the address of COMPONENT_REFs with the same
9290      type and component to a comparison of the address of the base
9291      object.  In short, &x->a OP &y->a to x OP y and
9292      &x->a OP &y.a to x OP &y  */
9293   if (TREE_CODE (arg0) == ADDR_EXPR
9294       && TREE_CODE (TREE_OPERAND (arg0, 0)) == COMPONENT_REF
9295       && TREE_CODE (arg1) == ADDR_EXPR
9296       && TREE_CODE (TREE_OPERAND (arg1, 0)) == COMPONENT_REF)
9297     {
9298       tree cref0 = TREE_OPERAND (arg0, 0);
9299       tree cref1 = TREE_OPERAND (arg1, 0);
9300       if (TREE_OPERAND (cref0, 1) == TREE_OPERAND (cref1, 1))
9301         {
9302           tree op0 = TREE_OPERAND (cref0, 0);
9303           tree op1 = TREE_OPERAND (cref1, 0);
9304           return fold_build2 (code, type,
9305                               fold_addr_expr (op0),
9306                               fold_addr_expr (op1));
9307         }
9308     }
9309
9310   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9311      into a single range test.  */
9312   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9313        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9314       && TREE_CODE (arg1) == INTEGER_CST
9315       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9316       && !integer_zerop (TREE_OPERAND (arg0, 1))
9317       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9318       && !TREE_OVERFLOW (arg1))
9319     {
9320       tem = fold_div_compare (code, type, arg0, arg1);
9321       if (tem != NULL_TREE)
9322         return tem;
9323     }
9324
9325   /* Fold ~X op ~Y as Y op X.  */
9326   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9327       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9328     {
9329       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9330       return fold_build2 (code, type,
9331                           fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9332                           TREE_OPERAND (arg0, 0));
9333     }
9334
9335   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9336   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9337       && TREE_CODE (arg1) == INTEGER_CST)
9338     {
9339       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9340       return fold_build2 (swap_tree_comparison (code), type,
9341                           TREE_OPERAND (arg0, 0),
9342                           fold_build1 (BIT_NOT_EXPR, cmp_type,
9343                                        fold_convert (cmp_type, arg1)));
9344     }
9345
9346   return NULL_TREE;
9347 }
9348
9349
9350 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9351    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9352    argument EXPR represents the expression "z" of type TYPE.  */
9353
9354 static tree
9355 fold_mult_zconjz (tree type, tree expr)
9356 {
9357   tree itype = TREE_TYPE (type);
9358   tree rpart, ipart, tem;
9359
9360   if (TREE_CODE (expr) == COMPLEX_EXPR)
9361     {
9362       rpart = TREE_OPERAND (expr, 0);
9363       ipart = TREE_OPERAND (expr, 1);
9364     }
9365   else if (TREE_CODE (expr) == COMPLEX_CST)
9366     {
9367       rpart = TREE_REALPART (expr);
9368       ipart = TREE_IMAGPART (expr);
9369     }
9370   else
9371     {
9372       expr = save_expr (expr);
9373       rpart = fold_build1 (REALPART_EXPR, itype, expr);
9374       ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9375     }
9376
9377   rpart = save_expr (rpart);
9378   ipart = save_expr (ipart);
9379   tem = fold_build2 (PLUS_EXPR, itype,
9380                      fold_build2 (MULT_EXPR, itype, rpart, rpart),
9381                      fold_build2 (MULT_EXPR, itype, ipart, ipart));
9382   return fold_build2 (COMPLEX_EXPR, type, tem,
9383                       fold_convert (itype, integer_zero_node));
9384 }
9385
9386
9387 /* Fold a binary expression of code CODE and type TYPE with operands
9388    OP0 and OP1.  Return the folded expression if folding is
9389    successful.  Otherwise, return NULL_TREE.  */
9390
9391 tree
9392 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9393 {
9394   enum tree_code_class kind = TREE_CODE_CLASS (code);
9395   tree arg0, arg1, tem;
9396   tree t1 = NULL_TREE;
9397   bool strict_overflow_p;
9398
9399   gcc_assert ((IS_EXPR_CODE_CLASS (kind)
9400                || IS_GIMPLE_STMT_CODE_CLASS (kind))
9401               && TREE_CODE_LENGTH (code) == 2
9402               && op0 != NULL_TREE
9403               && op1 != NULL_TREE);
9404
9405   arg0 = op0;
9406   arg1 = op1;
9407
9408   /* Strip any conversions that don't change the mode.  This is
9409      safe for every expression, except for a comparison expression
9410      because its signedness is derived from its operands.  So, in
9411      the latter case, only strip conversions that don't change the
9412      signedness.
9413
9414      Note that this is done as an internal manipulation within the
9415      constant folder, in order to find the simplest representation
9416      of the arguments so that their form can be studied.  In any
9417      cases, the appropriate type conversions should be put back in
9418      the tree that will get out of the constant folder.  */
9419
9420   if (kind == tcc_comparison)
9421     {
9422       STRIP_SIGN_NOPS (arg0);
9423       STRIP_SIGN_NOPS (arg1);
9424     }
9425   else
9426     {
9427       STRIP_NOPS (arg0);
9428       STRIP_NOPS (arg1);
9429     }
9430
9431   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9432      constant but we can't do arithmetic on them.  */
9433   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9434       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9435       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9436       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9437       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9438       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9439     {
9440       if (kind == tcc_binary)
9441         {
9442           /* Make sure type and arg0 have the same saturating flag.  */
9443           gcc_assert (TYPE_SATURATING (type)
9444                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9445           tem = const_binop (code, arg0, arg1, 0);
9446         }
9447       else if (kind == tcc_comparison)
9448         tem = fold_relational_const (code, type, arg0, arg1);
9449       else
9450         tem = NULL_TREE;
9451
9452       if (tem != NULL_TREE)
9453         {
9454           if (TREE_TYPE (tem) != type)
9455             tem = fold_convert (type, tem);
9456           return tem;
9457         }
9458     }
9459
9460   /* If this is a commutative operation, and ARG0 is a constant, move it
9461      to ARG1 to reduce the number of tests below.  */
9462   if (commutative_tree_code (code)
9463       && tree_swap_operands_p (arg0, arg1, true))
9464     return fold_build2 (code, type, op1, op0);
9465
9466   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9467
9468      First check for cases where an arithmetic operation is applied to a
9469      compound, conditional, or comparison operation.  Push the arithmetic
9470      operation inside the compound or conditional to see if any folding
9471      can then be done.  Convert comparison to conditional for this purpose.
9472      The also optimizes non-constant cases that used to be done in
9473      expand_expr.
9474
9475      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9476      one of the operands is a comparison and the other is a comparison, a
9477      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9478      code below would make the expression more complex.  Change it to a
9479      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9480      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9481
9482   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9483        || code == EQ_EXPR || code == NE_EXPR)
9484       && ((truth_value_p (TREE_CODE (arg0))
9485            && (truth_value_p (TREE_CODE (arg1))
9486                || (TREE_CODE (arg1) == BIT_AND_EXPR
9487                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9488           || (truth_value_p (TREE_CODE (arg1))
9489               && (truth_value_p (TREE_CODE (arg0))
9490                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9491                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9492     {
9493       tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9494                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9495                          : TRUTH_XOR_EXPR,
9496                          boolean_type_node,
9497                          fold_convert (boolean_type_node, arg0),
9498                          fold_convert (boolean_type_node, arg1));
9499
9500       if (code == EQ_EXPR)
9501         tem = invert_truthvalue (tem);
9502
9503       return fold_convert (type, tem);
9504     }
9505
9506   if (TREE_CODE_CLASS (code) == tcc_binary
9507       || TREE_CODE_CLASS (code) == tcc_comparison)
9508     {
9509       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9510         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9511                        fold_build2 (code, type,
9512                                     TREE_OPERAND (arg0, 1), op1));
9513       if (TREE_CODE (arg1) == COMPOUND_EXPR
9514           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9515         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9516                        fold_build2 (code, type,
9517                                     op0, TREE_OPERAND (arg1, 1)));
9518
9519       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9520         {
9521           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9522                                                      arg0, arg1, 
9523                                                      /*cond_first_p=*/1);
9524           if (tem != NULL_TREE)
9525             return tem;
9526         }
9527
9528       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9529         {
9530           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9531                                                      arg1, arg0, 
9532                                                      /*cond_first_p=*/0);
9533           if (tem != NULL_TREE)
9534             return tem;
9535         }
9536     }
9537
9538   switch (code)
9539     {
9540     case POINTER_PLUS_EXPR:
9541       /* 0 +p index -> (type)index */
9542       if (integer_zerop (arg0))
9543         return non_lvalue (fold_convert (type, arg1));
9544
9545       /* PTR +p 0 -> PTR */
9546       if (integer_zerop (arg1))
9547         return non_lvalue (fold_convert (type, arg0));
9548
9549       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9550       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9551            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9552         return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9553                                                 fold_convert (sizetype, arg1),
9554                                                 fold_convert (sizetype, arg0)));
9555
9556       /* index +p PTR -> PTR +p index */
9557       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9558           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9559         return fold_build2 (POINTER_PLUS_EXPR, type,
9560                             fold_convert (type, arg1),
9561                             fold_convert (sizetype, arg0));
9562
9563       /* (PTR +p B) +p A -> PTR +p (B + A) */
9564       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9565         {
9566           tree inner;
9567           tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9568           tree arg00 = TREE_OPERAND (arg0, 0);
9569           inner = fold_build2 (PLUS_EXPR, sizetype,
9570                                arg01, fold_convert (sizetype, arg1));
9571           return fold_convert (type,
9572                                fold_build2 (POINTER_PLUS_EXPR,
9573                                             TREE_TYPE (arg00), arg00, inner));
9574         }
9575
9576       /* PTR_CST +p CST -> CST1 */
9577       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9578         return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9579
9580      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9581         of the array.  Loop optimizer sometimes produce this type of
9582         expressions.  */
9583       if (TREE_CODE (arg0) == ADDR_EXPR)
9584         {
9585           tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9586           if (tem)
9587             return fold_convert (type, tem);
9588         }
9589
9590       return NULL_TREE;
9591
9592     case PLUS_EXPR:
9593       /* PTR + INT -> (INT)(PTR p+ INT) */
9594       if (POINTER_TYPE_P (TREE_TYPE (arg0))
9595           && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
9596         return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9597                                                 TREE_TYPE (arg0),
9598                                                 arg0,
9599                                                 fold_convert (sizetype, arg1)));
9600       /* INT + PTR -> (INT)(PTR p+ INT) */
9601       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9602           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9603         return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9604                                                 TREE_TYPE (arg1),
9605                                                 arg1,
9606                                                 fold_convert (sizetype, arg0)));
9607       /* A + (-B) -> A - B */
9608       if (TREE_CODE (arg1) == NEGATE_EXPR)
9609         return fold_build2 (MINUS_EXPR, type,
9610                             fold_convert (type, arg0),
9611                             fold_convert (type, TREE_OPERAND (arg1, 0)));
9612       /* (-A) + B -> B - A */
9613       if (TREE_CODE (arg0) == NEGATE_EXPR
9614           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9615         return fold_build2 (MINUS_EXPR, type,
9616                             fold_convert (type, arg1),
9617                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9618
9619       if (INTEGRAL_TYPE_P (type))
9620         {
9621           /* Convert ~A + 1 to -A.  */
9622           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9623               && integer_onep (arg1))
9624             return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
9625
9626           /* ~X + X is -1.  */
9627           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9628               && !TYPE_OVERFLOW_TRAPS (type))
9629             {
9630               tree tem = TREE_OPERAND (arg0, 0);
9631
9632               STRIP_NOPS (tem);
9633               if (operand_equal_p (tem, arg1, 0))
9634                 {
9635                   t1 = build_int_cst_type (type, -1);
9636                   return omit_one_operand (type, t1, arg1);
9637                 }
9638             }
9639
9640           /* X + ~X is -1.  */
9641           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9642               && !TYPE_OVERFLOW_TRAPS (type))
9643             {
9644               tree tem = TREE_OPERAND (arg1, 0);
9645
9646               STRIP_NOPS (tem);
9647               if (operand_equal_p (arg0, tem, 0))
9648                 {
9649                   t1 = build_int_cst_type (type, -1);
9650                   return omit_one_operand (type, t1, arg0);
9651                 }
9652             }
9653         }
9654
9655       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9656          same or one.  Make sure type is not saturating.
9657          fold_plusminus_mult_expr will re-associate.  */
9658       if ((TREE_CODE (arg0) == MULT_EXPR
9659            || TREE_CODE (arg1) == MULT_EXPR)
9660           && !TYPE_SATURATING (type)
9661           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9662         {
9663           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9664           if (tem)
9665             return tem;
9666         }
9667
9668       if (! FLOAT_TYPE_P (type))
9669         {
9670           if (integer_zerop (arg1))
9671             return non_lvalue (fold_convert (type, arg0));
9672
9673           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9674              with a constant, and the two constants have no bits in common,
9675              we should treat this as a BIT_IOR_EXPR since this may produce more
9676              simplifications.  */
9677           if (TREE_CODE (arg0) == BIT_AND_EXPR
9678               && TREE_CODE (arg1) == BIT_AND_EXPR
9679               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9680               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9681               && integer_zerop (const_binop (BIT_AND_EXPR,
9682                                              TREE_OPERAND (arg0, 1),
9683                                              TREE_OPERAND (arg1, 1), 0)))
9684             {
9685               code = BIT_IOR_EXPR;
9686               goto bit_ior;
9687             }
9688
9689           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9690              (plus (plus (mult) (mult)) (foo)) so that we can
9691              take advantage of the factoring cases below.  */
9692           if (((TREE_CODE (arg0) == PLUS_EXPR
9693                 || TREE_CODE (arg0) == MINUS_EXPR)
9694                && TREE_CODE (arg1) == MULT_EXPR)
9695               || ((TREE_CODE (arg1) == PLUS_EXPR
9696                    || TREE_CODE (arg1) == MINUS_EXPR)
9697                   && TREE_CODE (arg0) == MULT_EXPR))
9698             {
9699               tree parg0, parg1, parg, marg;
9700               enum tree_code pcode;
9701
9702               if (TREE_CODE (arg1) == MULT_EXPR)
9703                 parg = arg0, marg = arg1;
9704               else
9705                 parg = arg1, marg = arg0;
9706               pcode = TREE_CODE (parg);
9707               parg0 = TREE_OPERAND (parg, 0);
9708               parg1 = TREE_OPERAND (parg, 1);
9709               STRIP_NOPS (parg0);
9710               STRIP_NOPS (parg1);
9711
9712               if (TREE_CODE (parg0) == MULT_EXPR
9713                   && TREE_CODE (parg1) != MULT_EXPR)
9714                 return fold_build2 (pcode, type,
9715                                     fold_build2 (PLUS_EXPR, type,
9716                                                  fold_convert (type, parg0),
9717                                                  fold_convert (type, marg)),
9718                                     fold_convert (type, parg1));
9719               if (TREE_CODE (parg0) != MULT_EXPR
9720                   && TREE_CODE (parg1) == MULT_EXPR)
9721                 return fold_build2 (PLUS_EXPR, type,
9722                                     fold_convert (type, parg0),
9723                                     fold_build2 (pcode, type,
9724                                                  fold_convert (type, marg),
9725                                                  fold_convert (type,
9726                                                                parg1)));
9727             }
9728         }
9729       else
9730         {
9731           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
9732           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9733             return non_lvalue (fold_convert (type, arg0));
9734
9735           /* Likewise if the operands are reversed.  */
9736           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9737             return non_lvalue (fold_convert (type, arg1));
9738
9739           /* Convert X + -C into X - C.  */
9740           if (TREE_CODE (arg1) == REAL_CST
9741               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9742             {
9743               tem = fold_negate_const (arg1, type);
9744               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9745                 return fold_build2 (MINUS_EXPR, type,
9746                                     fold_convert (type, arg0),
9747                                     fold_convert (type, tem));
9748             }
9749
9750           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9751              to __complex__ ( x, y ).  This is not the same for SNaNs or
9752              if signed zeros are involved.  */
9753           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9754               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9755               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9756             {
9757               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9758               tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
9759               tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
9760               bool arg0rz = false, arg0iz = false;
9761               if ((arg0r && (arg0rz = real_zerop (arg0r)))
9762                   || (arg0i && (arg0iz = real_zerop (arg0i))))
9763                 {
9764                   tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
9765                   tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
9766                   if (arg0rz && arg1i && real_zerop (arg1i))
9767                     {
9768                       tree rp = arg1r ? arg1r
9769                                   : build1 (REALPART_EXPR, rtype, arg1);
9770                       tree ip = arg0i ? arg0i
9771                                   : build1 (IMAGPART_EXPR, rtype, arg0);
9772                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9773                     }
9774                   else if (arg0iz && arg1r && real_zerop (arg1r))
9775                     {
9776                       tree rp = arg0r ? arg0r
9777                                   : build1 (REALPART_EXPR, rtype, arg0);
9778                       tree ip = arg1i ? arg1i
9779                                   : build1 (IMAGPART_EXPR, rtype, arg1);
9780                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9781                     }
9782                 }
9783             }
9784
9785           if (flag_unsafe_math_optimizations
9786               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9787               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9788               && (tem = distribute_real_division (code, type, arg0, arg1)))
9789             return tem;
9790
9791           /* Convert x+x into x*2.0.  */
9792           if (operand_equal_p (arg0, arg1, 0)
9793               && SCALAR_FLOAT_TYPE_P (type))
9794             return fold_build2 (MULT_EXPR, type, arg0,
9795                                 build_real (type, dconst2));
9796
9797           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.  
9798              We associate floats only if the user has specified
9799              -fassociative-math.  */
9800           if (flag_associative_math
9801               && TREE_CODE (arg1) == PLUS_EXPR
9802               && TREE_CODE (arg0) != MULT_EXPR)
9803             {
9804               tree tree10 = TREE_OPERAND (arg1, 0);
9805               tree tree11 = TREE_OPERAND (arg1, 1);
9806               if (TREE_CODE (tree11) == MULT_EXPR
9807                   && TREE_CODE (tree10) == MULT_EXPR)
9808                 {
9809                   tree tree0;
9810                   tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
9811                   return fold_build2 (PLUS_EXPR, type, tree0, tree11);
9812                 }
9813             }
9814           /* Convert (b*c + d*e) + a into b*c + (d*e +a).  
9815              We associate floats only if the user has specified
9816              -fassociative-math.  */
9817           if (flag_associative_math
9818               && TREE_CODE (arg0) == PLUS_EXPR
9819               && TREE_CODE (arg1) != MULT_EXPR)
9820             {
9821               tree tree00 = TREE_OPERAND (arg0, 0);
9822               tree tree01 = TREE_OPERAND (arg0, 1);
9823               if (TREE_CODE (tree01) == MULT_EXPR
9824                   && TREE_CODE (tree00) == MULT_EXPR)
9825                 {
9826                   tree tree0;
9827                   tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
9828                   return fold_build2 (PLUS_EXPR, type, tree00, tree0);
9829                 }
9830             }
9831         }
9832
9833      bit_rotate:
9834       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9835          is a rotate of A by C1 bits.  */
9836       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9837          is a rotate of A by B bits.  */
9838       {
9839         enum tree_code code0, code1;
9840         code0 = TREE_CODE (arg0);
9841         code1 = TREE_CODE (arg1);
9842         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9843              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9844             && operand_equal_p (TREE_OPERAND (arg0, 0),
9845                                 TREE_OPERAND (arg1, 0), 0)
9846             && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
9847           {
9848             tree tree01, tree11;
9849             enum tree_code code01, code11;
9850
9851             tree01 = TREE_OPERAND (arg0, 1);
9852             tree11 = TREE_OPERAND (arg1, 1);
9853             STRIP_NOPS (tree01);
9854             STRIP_NOPS (tree11);
9855             code01 = TREE_CODE (tree01);
9856             code11 = TREE_CODE (tree11);
9857             if (code01 == INTEGER_CST
9858                 && code11 == INTEGER_CST
9859                 && TREE_INT_CST_HIGH (tree01) == 0
9860                 && TREE_INT_CST_HIGH (tree11) == 0
9861                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9862                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9863               return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
9864                              code0 == LSHIFT_EXPR ? tree01 : tree11);
9865             else if (code11 == MINUS_EXPR)
9866               {
9867                 tree tree110, tree111;
9868                 tree110 = TREE_OPERAND (tree11, 0);
9869                 tree111 = TREE_OPERAND (tree11, 1);
9870                 STRIP_NOPS (tree110);
9871                 STRIP_NOPS (tree111);
9872                 if (TREE_CODE (tree110) == INTEGER_CST
9873                     && 0 == compare_tree_int (tree110,
9874                                               TYPE_PRECISION
9875                                               (TREE_TYPE (TREE_OPERAND
9876                                                           (arg0, 0))))
9877                     && operand_equal_p (tree01, tree111, 0))
9878                   return build2 ((code0 == LSHIFT_EXPR
9879                                   ? LROTATE_EXPR
9880                                   : RROTATE_EXPR),
9881                                  type, TREE_OPERAND (arg0, 0), tree01);
9882               }
9883             else if (code01 == MINUS_EXPR)
9884               {
9885                 tree tree010, tree011;
9886                 tree010 = TREE_OPERAND (tree01, 0);
9887                 tree011 = TREE_OPERAND (tree01, 1);
9888                 STRIP_NOPS (tree010);
9889                 STRIP_NOPS (tree011);
9890                 if (TREE_CODE (tree010) == INTEGER_CST
9891                     && 0 == compare_tree_int (tree010,
9892                                               TYPE_PRECISION
9893                                               (TREE_TYPE (TREE_OPERAND
9894                                                           (arg0, 0))))
9895                     && operand_equal_p (tree11, tree011, 0))
9896                   return build2 ((code0 != LSHIFT_EXPR
9897                                   ? LROTATE_EXPR
9898                                   : RROTATE_EXPR),
9899                                  type, TREE_OPERAND (arg0, 0), tree11);
9900               }
9901           }
9902       }
9903
9904     associate:
9905       /* In most languages, can't associate operations on floats through
9906          parentheses.  Rather than remember where the parentheses were, we
9907          don't associate floats at all, unless the user has specified
9908          -fassociative-math.
9909          And, we need to make sure type is not saturating.  */
9910
9911       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9912           && !TYPE_SATURATING (type))
9913         {
9914           tree var0, con0, lit0, minus_lit0;
9915           tree var1, con1, lit1, minus_lit1;
9916           bool ok = true;
9917
9918           /* Split both trees into variables, constants, and literals.  Then
9919              associate each group together, the constants with literals,
9920              then the result with variables.  This increases the chances of
9921              literals being recombined later and of generating relocatable
9922              expressions for the sum of a constant and literal.  */
9923           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9924           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9925                              code == MINUS_EXPR);
9926
9927           /* With undefined overflow we can only associate constants
9928              with one variable.  */
9929           if ((POINTER_TYPE_P (type)
9930                || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9931               && var0 && var1)
9932             {
9933               tree tmp0 = var0;
9934               tree tmp1 = var1;
9935
9936               if (TREE_CODE (tmp0) == NEGATE_EXPR)
9937                 tmp0 = TREE_OPERAND (tmp0, 0);
9938               if (TREE_CODE (tmp1) == NEGATE_EXPR)
9939                 tmp1 = TREE_OPERAND (tmp1, 0);
9940               /* The only case we can still associate with two variables
9941                  is if they are the same, modulo negation.  */
9942               if (!operand_equal_p (tmp0, tmp1, 0))
9943                 ok = false;
9944             }
9945
9946           /* Only do something if we found more than two objects.  Otherwise,
9947              nothing has changed and we risk infinite recursion.  */
9948           if (ok
9949               && (2 < ((var0 != 0) + (var1 != 0)
9950                        + (con0 != 0) + (con1 != 0)
9951                        + (lit0 != 0) + (lit1 != 0)
9952                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
9953             {
9954               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
9955               if (code == MINUS_EXPR)
9956                 code = PLUS_EXPR;
9957
9958               var0 = associate_trees (var0, var1, code, type);
9959               con0 = associate_trees (con0, con1, code, type);
9960               lit0 = associate_trees (lit0, lit1, code, type);
9961               minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
9962
9963               /* Preserve the MINUS_EXPR if the negative part of the literal is
9964                  greater than the positive part.  Otherwise, the multiplicative
9965                  folding code (i.e extract_muldiv) may be fooled in case
9966                  unsigned constants are subtracted, like in the following
9967                  example: ((X*2 + 4) - 8U)/2.  */
9968               if (minus_lit0 && lit0)
9969                 {
9970                   if (TREE_CODE (lit0) == INTEGER_CST
9971                       && TREE_CODE (minus_lit0) == INTEGER_CST
9972                       && tree_int_cst_lt (lit0, minus_lit0))
9973                     {
9974                       minus_lit0 = associate_trees (minus_lit0, lit0,
9975                                                     MINUS_EXPR, type);
9976                       lit0 = 0;
9977                     }
9978                   else
9979                     {
9980                       lit0 = associate_trees (lit0, minus_lit0,
9981                                               MINUS_EXPR, type);
9982                       minus_lit0 = 0;
9983                     }
9984                 }
9985               if (minus_lit0)
9986                 {
9987                   if (con0 == 0)
9988                     return fold_convert (type,
9989                                          associate_trees (var0, minus_lit0,
9990                                                           MINUS_EXPR, type));
9991                   else
9992                     {
9993                       con0 = associate_trees (con0, minus_lit0,
9994                                               MINUS_EXPR, type);
9995                       return fold_convert (type,
9996                                            associate_trees (var0, con0,
9997                                                             PLUS_EXPR, type));
9998                     }
9999                 }
10000
10001               con0 = associate_trees (con0, lit0, code, type);
10002               return fold_convert (type, associate_trees (var0, con0,
10003                                                           code, type));
10004             }
10005         }
10006
10007       return NULL_TREE;
10008
10009     case MINUS_EXPR:
10010       /* Pointer simplifications for subtraction, simple reassociations. */
10011       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10012         {
10013           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10014           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10015               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10016             {
10017               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10018               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10019               tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10020               tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10021               return fold_build2 (PLUS_EXPR, type,
10022                                   fold_build2 (MINUS_EXPR, type, arg00, arg10),
10023                                   fold_build2 (MINUS_EXPR, type, arg01, arg11));
10024             }
10025           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10026           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10027             {
10028               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10029               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10030               tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
10031               if (tmp)
10032                 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
10033             }
10034         }
10035       /* A - (-B) -> A + B */
10036       if (TREE_CODE (arg1) == NEGATE_EXPR)
10037         return fold_build2 (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0));
10038       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10039       if (TREE_CODE (arg0) == NEGATE_EXPR
10040           && (FLOAT_TYPE_P (type)
10041               || INTEGRAL_TYPE_P (type))
10042           && negate_expr_p (arg1)
10043           && reorder_operands_p (arg0, arg1))
10044         return fold_build2 (MINUS_EXPR, type, negate_expr (arg1),
10045                             TREE_OPERAND (arg0, 0));
10046       /* Convert -A - 1 to ~A.  */
10047       if (INTEGRAL_TYPE_P (type)
10048           && TREE_CODE (arg0) == NEGATE_EXPR
10049           && integer_onep (arg1)
10050           && !TYPE_OVERFLOW_TRAPS (type))
10051         return fold_build1 (BIT_NOT_EXPR, type,
10052                             fold_convert (type, TREE_OPERAND (arg0, 0)));
10053
10054       /* Convert -1 - A to ~A.  */
10055       if (INTEGRAL_TYPE_P (type)
10056           && integer_all_onesp (arg0))
10057         return fold_build1 (BIT_NOT_EXPR, type, op1);
10058
10059       if (! FLOAT_TYPE_P (type))
10060         {
10061           if (integer_zerop (arg0))
10062             return negate_expr (fold_convert (type, arg1));
10063           if (integer_zerop (arg1))
10064             return non_lvalue (fold_convert (type, arg0));
10065
10066           /* Fold A - (A & B) into ~B & A.  */
10067           if (!TREE_SIDE_EFFECTS (arg0)
10068               && TREE_CODE (arg1) == BIT_AND_EXPR)
10069             {
10070               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10071                 {
10072                   tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10073                   return fold_build2 (BIT_AND_EXPR, type,
10074                                       fold_build1 (BIT_NOT_EXPR, type, arg10),
10075                                       fold_convert (type, arg0));
10076                 }
10077               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10078                 {
10079                   tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10080                   return fold_build2 (BIT_AND_EXPR, type,
10081                                       fold_build1 (BIT_NOT_EXPR, type, arg11),
10082                                       fold_convert (type, arg0));
10083                 }
10084             }
10085
10086           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10087              any power of 2 minus 1.  */
10088           if (TREE_CODE (arg0) == BIT_AND_EXPR
10089               && TREE_CODE (arg1) == BIT_AND_EXPR
10090               && operand_equal_p (TREE_OPERAND (arg0, 0),
10091                                   TREE_OPERAND (arg1, 0), 0))
10092             {
10093               tree mask0 = TREE_OPERAND (arg0, 1);
10094               tree mask1 = TREE_OPERAND (arg1, 1);
10095               tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
10096
10097               if (operand_equal_p (tem, mask1, 0))
10098                 {
10099                   tem = fold_build2 (BIT_XOR_EXPR, type,
10100                                      TREE_OPERAND (arg0, 0), mask1);
10101                   return fold_build2 (MINUS_EXPR, type, tem, mask1);
10102                 }
10103             }
10104         }
10105
10106       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10107       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10108         return non_lvalue (fold_convert (type, arg0));
10109
10110       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10111          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10112          (-ARG1 + ARG0) reduces to -ARG1.  */
10113       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10114         return negate_expr (fold_convert (type, arg1));
10115
10116       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10117          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10118          signed zeros are involved.  */
10119       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10120           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10121           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10122         {
10123           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10124           tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10125           tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10126           bool arg0rz = false, arg0iz = false;
10127           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10128               || (arg0i && (arg0iz = real_zerop (arg0i))))
10129             {
10130               tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10131               tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10132               if (arg0rz && arg1i && real_zerop (arg1i))
10133                 {
10134                   tree rp = fold_build1 (NEGATE_EXPR, rtype,
10135                                          arg1r ? arg1r
10136                                          : build1 (REALPART_EXPR, rtype, arg1));
10137                   tree ip = arg0i ? arg0i
10138                     : build1 (IMAGPART_EXPR, rtype, arg0);
10139                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10140                 }
10141               else if (arg0iz && arg1r && real_zerop (arg1r))
10142                 {
10143                   tree rp = arg0r ? arg0r
10144                     : build1 (REALPART_EXPR, rtype, arg0);
10145                   tree ip = fold_build1 (NEGATE_EXPR, rtype,
10146                                          arg1i ? arg1i
10147                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10148                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10149                 }
10150             }
10151         }
10152
10153       /* Fold &x - &x.  This can happen from &x.foo - &x.
10154          This is unsafe for certain floats even in non-IEEE formats.
10155          In IEEE, it is unsafe because it does wrong for NaNs.
10156          Also note that operand_equal_p is always false if an operand
10157          is volatile.  */
10158
10159       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10160           && operand_equal_p (arg0, arg1, 0))
10161         return fold_convert (type, integer_zero_node);
10162
10163       /* A - B -> A + (-B) if B is easily negatable.  */
10164       if (negate_expr_p (arg1)
10165           && ((FLOAT_TYPE_P (type)
10166                /* Avoid this transformation if B is a positive REAL_CST.  */
10167                && (TREE_CODE (arg1) != REAL_CST
10168                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10169               || INTEGRAL_TYPE_P (type)))
10170         return fold_build2 (PLUS_EXPR, type,
10171                             fold_convert (type, arg0),
10172                             fold_convert (type, negate_expr (arg1)));
10173
10174       /* Try folding difference of addresses.  */
10175       {
10176         HOST_WIDE_INT diff;
10177
10178         if ((TREE_CODE (arg0) == ADDR_EXPR
10179              || TREE_CODE (arg1) == ADDR_EXPR)
10180             && ptr_difference_const (arg0, arg1, &diff))
10181           return build_int_cst_type (type, diff);
10182       }
10183
10184       /* Fold &a[i] - &a[j] to i-j.  */
10185       if (TREE_CODE (arg0) == ADDR_EXPR
10186           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10187           && TREE_CODE (arg1) == ADDR_EXPR
10188           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10189         {
10190           tree aref0 = TREE_OPERAND (arg0, 0);
10191           tree aref1 = TREE_OPERAND (arg1, 0);
10192           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10193                                TREE_OPERAND (aref1, 0), 0))
10194             {
10195               tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10196               tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10197               tree esz = array_ref_element_size (aref0);
10198               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10199               return fold_build2 (MULT_EXPR, type, diff,
10200                                   fold_convert (type, esz));
10201                                   
10202             }
10203         }
10204
10205       if (flag_unsafe_math_optimizations
10206           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10207           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10208           && (tem = distribute_real_division (code, type, arg0, arg1)))
10209         return tem;
10210
10211       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10212          same or one.  Make sure type is not saturating.
10213          fold_plusminus_mult_expr will re-associate.  */
10214       if ((TREE_CODE (arg0) == MULT_EXPR
10215            || TREE_CODE (arg1) == MULT_EXPR)
10216           && !TYPE_SATURATING (type)
10217           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10218         {
10219           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10220           if (tem)
10221             return tem;
10222         }
10223
10224       goto associate;
10225
10226     case MULT_EXPR:
10227       /* (-A) * (-B) -> A * B  */
10228       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10229         return fold_build2 (MULT_EXPR, type,
10230                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10231                             fold_convert (type, negate_expr (arg1)));
10232       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10233         return fold_build2 (MULT_EXPR, type,
10234                             fold_convert (type, negate_expr (arg0)),
10235                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10236
10237       if (! FLOAT_TYPE_P (type))
10238         {
10239           if (integer_zerop (arg1))
10240             return omit_one_operand (type, arg1, arg0);
10241           if (integer_onep (arg1))
10242             return non_lvalue (fold_convert (type, arg0));
10243           /* Transform x * -1 into -x.  */
10244           if (integer_all_onesp (arg1))
10245             return fold_convert (type, negate_expr (arg0));
10246           /* Transform x * -C into -x * C if x is easily negatable.  */
10247           if (TREE_CODE (arg1) == INTEGER_CST
10248               && tree_int_cst_sgn (arg1) == -1
10249               && negate_expr_p (arg0)
10250               && (tem = negate_expr (arg1)) != arg1
10251               && !TREE_OVERFLOW (tem))
10252             return fold_build2 (MULT_EXPR, type,
10253                                 negate_expr (arg0), tem);
10254
10255           /* (a * (1 << b)) is (a << b)  */
10256           if (TREE_CODE (arg1) == LSHIFT_EXPR
10257               && integer_onep (TREE_OPERAND (arg1, 0)))
10258             return fold_build2 (LSHIFT_EXPR, type, arg0,
10259                                 TREE_OPERAND (arg1, 1));
10260           if (TREE_CODE (arg0) == LSHIFT_EXPR
10261               && integer_onep (TREE_OPERAND (arg0, 0)))
10262             return fold_build2 (LSHIFT_EXPR, type, arg1,
10263                                 TREE_OPERAND (arg0, 1));
10264
10265           strict_overflow_p = false;
10266           if (TREE_CODE (arg1) == INTEGER_CST
10267               && 0 != (tem = extract_muldiv (op0,
10268                                              fold_convert (type, arg1),
10269                                              code, NULL_TREE,
10270                                              &strict_overflow_p)))
10271             {
10272               if (strict_overflow_p)
10273                 fold_overflow_warning (("assuming signed overflow does not "
10274                                         "occur when simplifying "
10275                                         "multiplication"),
10276                                        WARN_STRICT_OVERFLOW_MISC);
10277               return fold_convert (type, tem);
10278             }
10279
10280           /* Optimize z * conj(z) for integer complex numbers.  */
10281           if (TREE_CODE (arg0) == CONJ_EXPR
10282               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10283             return fold_mult_zconjz (type, arg1);
10284           if (TREE_CODE (arg1) == CONJ_EXPR
10285               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10286             return fold_mult_zconjz (type, arg0);
10287         }
10288       else
10289         {
10290           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10291              when x is NaN, since x * 0 is also NaN.  Nor are they the
10292              same in modes with signed zeros, since multiplying a
10293              negative value by 0 gives -0, not +0.  */
10294           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10295               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10296               && real_zerop (arg1))
10297             return omit_one_operand (type, arg1, arg0);
10298           /* In IEEE floating point, x*1 is not equivalent to x for snans.  */
10299           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10300               && real_onep (arg1))
10301             return non_lvalue (fold_convert (type, arg0));
10302
10303           /* Transform x * -1.0 into -x.  */
10304           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10305               && real_minus_onep (arg1))
10306             return fold_convert (type, negate_expr (arg0));
10307
10308           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10309              the result for floating point types due to rounding so it is applied
10310              only if -fassociative-math was specify.  */
10311           if (flag_associative_math
10312               && TREE_CODE (arg0) == RDIV_EXPR
10313               && TREE_CODE (arg1) == REAL_CST
10314               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10315             {
10316               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10317                                       arg1, 0);
10318               if (tem)
10319                 return fold_build2 (RDIV_EXPR, type, tem,
10320                                     TREE_OPERAND (arg0, 1));
10321             }
10322
10323           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10324           if (operand_equal_p (arg0, arg1, 0))
10325             {
10326               tree tem = fold_strip_sign_ops (arg0);
10327               if (tem != NULL_TREE)
10328                 {
10329                   tem = fold_convert (type, tem);
10330                   return fold_build2 (MULT_EXPR, type, tem, tem);
10331                 }
10332             }
10333
10334           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10335              This is not the same for NaNs or if signed zeros are
10336              involved.  */
10337           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10338               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10339               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10340               && TREE_CODE (arg1) == COMPLEX_CST
10341               && real_zerop (TREE_REALPART (arg1)))
10342             {
10343               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10344               if (real_onep (TREE_IMAGPART (arg1)))
10345                 return fold_build2 (COMPLEX_EXPR, type,
10346                                     negate_expr (fold_build1 (IMAGPART_EXPR,
10347                                                               rtype, arg0)),
10348                                     fold_build1 (REALPART_EXPR, rtype, arg0));
10349               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10350                 return fold_build2 (COMPLEX_EXPR, type,
10351                                     fold_build1 (IMAGPART_EXPR, rtype, arg0),
10352                                     negate_expr (fold_build1 (REALPART_EXPR,
10353                                                               rtype, arg0)));
10354             }
10355
10356           /* Optimize z * conj(z) for floating point complex numbers.
10357              Guarded by flag_unsafe_math_optimizations as non-finite
10358              imaginary components don't produce scalar results.  */
10359           if (flag_unsafe_math_optimizations
10360               && TREE_CODE (arg0) == CONJ_EXPR
10361               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10362             return fold_mult_zconjz (type, arg1);
10363           if (flag_unsafe_math_optimizations
10364               && TREE_CODE (arg1) == CONJ_EXPR
10365               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10366             return fold_mult_zconjz (type, arg0);
10367
10368           if (flag_unsafe_math_optimizations)
10369             {
10370               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10371               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10372
10373               /* Optimizations of root(...)*root(...).  */
10374               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10375                 {
10376                   tree rootfn, arg;
10377                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10378                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10379
10380                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10381                   if (BUILTIN_SQRT_P (fcode0)
10382                       && operand_equal_p (arg00, arg10, 0)
10383                       && ! HONOR_SNANS (TYPE_MODE (type)))
10384                     return arg00;
10385
10386                   /* Optimize root(x)*root(y) as root(x*y).  */
10387                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10388                   arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10389                   return build_call_expr (rootfn, 1, arg);
10390                 }
10391
10392               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10393               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10394                 {
10395                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10396                   tree arg = fold_build2 (PLUS_EXPR, type,
10397                                           CALL_EXPR_ARG (arg0, 0),
10398                                           CALL_EXPR_ARG (arg1, 0));
10399                   return build_call_expr (expfn, 1, arg);
10400                 }
10401
10402               /* Optimizations of pow(...)*pow(...).  */
10403               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10404                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10405                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10406                 {
10407                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10408                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10409                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10410                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10411
10412                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10413                   if (operand_equal_p (arg01, arg11, 0))
10414                     {
10415                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10416                       tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10417                       return build_call_expr (powfn, 2, arg, arg01);
10418                     }
10419
10420                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10421                   if (operand_equal_p (arg00, arg10, 0))
10422                     {
10423                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10424                       tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10425                       return build_call_expr (powfn, 2, arg00, arg);
10426                     }
10427                 }
10428
10429               /* Optimize tan(x)*cos(x) as sin(x).  */
10430               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10431                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10432                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10433                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10434                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10435                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10436                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10437                                       CALL_EXPR_ARG (arg1, 0), 0))
10438                 {
10439                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10440
10441                   if (sinfn != NULL_TREE)
10442                     return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10443                 }
10444
10445               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10446               if (fcode1 == BUILT_IN_POW
10447                   || fcode1 == BUILT_IN_POWF
10448                   || fcode1 == BUILT_IN_POWL)
10449                 {
10450                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10451                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10452                   if (TREE_CODE (arg11) == REAL_CST
10453                       && !TREE_OVERFLOW (arg11)
10454                       && operand_equal_p (arg0, arg10, 0))
10455                     {
10456                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10457                       REAL_VALUE_TYPE c;
10458                       tree arg;
10459
10460                       c = TREE_REAL_CST (arg11);
10461                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10462                       arg = build_real (type, c);
10463                       return build_call_expr (powfn, 2, arg0, arg);
10464                     }
10465                 }
10466
10467               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10468               if (fcode0 == BUILT_IN_POW
10469                   || fcode0 == BUILT_IN_POWF
10470                   || fcode0 == BUILT_IN_POWL)
10471                 {
10472                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10473                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10474                   if (TREE_CODE (arg01) == REAL_CST
10475                       && !TREE_OVERFLOW (arg01)
10476                       && operand_equal_p (arg1, arg00, 0))
10477                     {
10478                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10479                       REAL_VALUE_TYPE c;
10480                       tree arg;
10481
10482                       c = TREE_REAL_CST (arg01);
10483                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10484                       arg = build_real (type, c);
10485                       return build_call_expr (powfn, 2, arg1, arg);
10486                     }
10487                 }
10488
10489               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10490               if (! optimize_size
10491                   && operand_equal_p (arg0, arg1, 0))
10492                 {
10493                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10494
10495                   if (powfn)
10496                     {
10497                       tree arg = build_real (type, dconst2);
10498                       return build_call_expr (powfn, 2, arg0, arg);
10499                     }
10500                 }
10501             }
10502         }
10503       goto associate;
10504
10505     case BIT_IOR_EXPR:
10506     bit_ior:
10507       if (integer_all_onesp (arg1))
10508         return omit_one_operand (type, arg1, arg0);
10509       if (integer_zerop (arg1))
10510         return non_lvalue (fold_convert (type, arg0));
10511       if (operand_equal_p (arg0, arg1, 0))
10512         return non_lvalue (fold_convert (type, arg0));
10513
10514       /* ~X | X is -1.  */
10515       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10516           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10517         {
10518           t1 = fold_convert (type, integer_zero_node);
10519           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10520           return omit_one_operand (type, t1, arg1);
10521         }
10522
10523       /* X | ~X is -1.  */
10524       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10525           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10526         {
10527           t1 = fold_convert (type, integer_zero_node);
10528           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10529           return omit_one_operand (type, t1, arg0);
10530         }
10531
10532       /* Canonicalize (X & C1) | C2.  */
10533       if (TREE_CODE (arg0) == BIT_AND_EXPR
10534           && TREE_CODE (arg1) == INTEGER_CST
10535           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10536         {
10537           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, mlo, mhi;
10538           int width = TYPE_PRECISION (type);
10539           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10540           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10541           hi2 = TREE_INT_CST_HIGH (arg1);
10542           lo2 = TREE_INT_CST_LOW (arg1);
10543
10544           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10545           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10546             return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10547
10548           if (width > HOST_BITS_PER_WIDE_INT)
10549             {
10550               mhi = (unsigned HOST_WIDE_INT) -1 
10551                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10552               mlo = -1;
10553             }
10554           else
10555             {
10556               mhi = 0;
10557               mlo = (unsigned HOST_WIDE_INT) -1
10558                     >> (HOST_BITS_PER_WIDE_INT - width);
10559             }
10560
10561           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10562           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10563             return fold_build2 (BIT_IOR_EXPR, type,
10564                                 TREE_OPERAND (arg0, 0), arg1);
10565
10566           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2.  */
10567           hi1 &= mhi;
10568           lo1 &= mlo;
10569           if ((hi1 & ~hi2) != hi1 || (lo1 & ~lo2) != lo1)
10570             return fold_build2 (BIT_IOR_EXPR, type,
10571                                 fold_build2 (BIT_AND_EXPR, type,
10572                                              TREE_OPERAND (arg0, 0),
10573                                              build_int_cst_wide (type,
10574                                                                  lo1 & ~lo2,
10575                                                                  hi1 & ~hi2)),
10576                                 arg1);
10577         }
10578
10579       /* (X & Y) | Y is (X, Y).  */
10580       if (TREE_CODE (arg0) == BIT_AND_EXPR
10581           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10582         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10583       /* (X & Y) | X is (Y, X).  */
10584       if (TREE_CODE (arg0) == BIT_AND_EXPR
10585           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10586           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10587         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10588       /* X | (X & Y) is (Y, X).  */
10589       if (TREE_CODE (arg1) == BIT_AND_EXPR
10590           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10591           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10592         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10593       /* X | (Y & X) is (Y, X).  */
10594       if (TREE_CODE (arg1) == BIT_AND_EXPR
10595           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10596           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10597         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10598
10599       t1 = distribute_bit_expr (code, type, arg0, arg1);
10600       if (t1 != NULL_TREE)
10601         return t1;
10602
10603       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10604
10605          This results in more efficient code for machines without a NAND
10606          instruction.  Combine will canonicalize to the first form
10607          which will allow use of NAND instructions provided by the
10608          backend if they exist.  */
10609       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10610           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10611         {
10612           return fold_build1 (BIT_NOT_EXPR, type,
10613                               build2 (BIT_AND_EXPR, type,
10614                                       TREE_OPERAND (arg0, 0),
10615                                       TREE_OPERAND (arg1, 0)));
10616         }
10617
10618       /* See if this can be simplified into a rotate first.  If that
10619          is unsuccessful continue in the association code.  */
10620       goto bit_rotate;
10621
10622     case BIT_XOR_EXPR:
10623       if (integer_zerop (arg1))
10624         return non_lvalue (fold_convert (type, arg0));
10625       if (integer_all_onesp (arg1))
10626         return fold_build1 (BIT_NOT_EXPR, type, op0);
10627       if (operand_equal_p (arg0, arg1, 0))
10628         return omit_one_operand (type, integer_zero_node, arg0);
10629
10630       /* ~X ^ X is -1.  */
10631       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10632           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10633         {
10634           t1 = fold_convert (type, integer_zero_node);
10635           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10636           return omit_one_operand (type, t1, arg1);
10637         }
10638
10639       /* X ^ ~X is -1.  */
10640       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10641           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10642         {
10643           t1 = fold_convert (type, integer_zero_node);
10644           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10645           return omit_one_operand (type, t1, arg0);
10646         }
10647
10648       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10649          with a constant, and the two constants have no bits in common,
10650          we should treat this as a BIT_IOR_EXPR since this may produce more
10651          simplifications.  */
10652       if (TREE_CODE (arg0) == BIT_AND_EXPR
10653           && TREE_CODE (arg1) == BIT_AND_EXPR
10654           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10655           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10656           && integer_zerop (const_binop (BIT_AND_EXPR,
10657                                          TREE_OPERAND (arg0, 1),
10658                                          TREE_OPERAND (arg1, 1), 0)))
10659         {
10660           code = BIT_IOR_EXPR;
10661           goto bit_ior;
10662         }
10663
10664       /* (X | Y) ^ X -> Y & ~ X*/
10665       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10666           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10667         {
10668           tree t2 = TREE_OPERAND (arg0, 1);
10669           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10670                             arg1);
10671           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10672                             fold_convert (type, t1));
10673           return t1;
10674         }
10675
10676       /* (Y | X) ^ X -> Y & ~ X*/
10677       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10678           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10679         {
10680           tree t2 = TREE_OPERAND (arg0, 0);
10681           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10682                             arg1);
10683           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10684                             fold_convert (type, t1));
10685           return t1;
10686         }
10687
10688       /* X ^ (X | Y) -> Y & ~ X*/
10689       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10690           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10691         {
10692           tree t2 = TREE_OPERAND (arg1, 1);
10693           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10694                             arg0);
10695           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10696                             fold_convert (type, t1));
10697           return t1;
10698         }
10699
10700       /* X ^ (Y | X) -> Y & ~ X*/
10701       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10702           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10703         {
10704           tree t2 = TREE_OPERAND (arg1, 0);
10705           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10706                             arg0);
10707           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10708                             fold_convert (type, t1));
10709           return t1;
10710         }
10711         
10712       /* Convert ~X ^ ~Y to X ^ Y.  */
10713       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10714           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10715         return fold_build2 (code, type,
10716                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10717                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10718
10719       /* Convert ~X ^ C to X ^ ~C.  */
10720       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10721           && TREE_CODE (arg1) == INTEGER_CST)
10722         return fold_build2 (code, type,
10723                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10724                             fold_build1 (BIT_NOT_EXPR, type, arg1));
10725
10726       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
10727       if (TREE_CODE (arg0) == BIT_AND_EXPR
10728           && integer_onep (TREE_OPERAND (arg0, 1))
10729           && integer_onep (arg1))
10730         return fold_build2 (EQ_EXPR, type, arg0,
10731                             build_int_cst (TREE_TYPE (arg0), 0));
10732
10733       /* Fold (X & Y) ^ Y as ~X & Y.  */
10734       if (TREE_CODE (arg0) == BIT_AND_EXPR
10735           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10736         {
10737           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10738           return fold_build2 (BIT_AND_EXPR, type, 
10739                               fold_build1 (BIT_NOT_EXPR, type, tem),
10740                               fold_convert (type, arg1));
10741         }
10742       /* Fold (X & Y) ^ X as ~Y & X.  */
10743       if (TREE_CODE (arg0) == BIT_AND_EXPR
10744           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10745           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10746         {
10747           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10748           return fold_build2 (BIT_AND_EXPR, type,
10749                               fold_build1 (BIT_NOT_EXPR, type, tem),
10750                               fold_convert (type, arg1));
10751         }
10752       /* Fold X ^ (X & Y) as X & ~Y.  */
10753       if (TREE_CODE (arg1) == BIT_AND_EXPR
10754           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10755         {
10756           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10757           return fold_build2 (BIT_AND_EXPR, type,
10758                               fold_convert (type, arg0),
10759                               fold_build1 (BIT_NOT_EXPR, type, tem));
10760         }
10761       /* Fold X ^ (Y & X) as ~Y & X.  */
10762       if (TREE_CODE (arg1) == BIT_AND_EXPR
10763           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10764           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10765         {
10766           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10767           return fold_build2 (BIT_AND_EXPR, type,
10768                               fold_build1 (BIT_NOT_EXPR, type, tem),
10769                               fold_convert (type, arg0));
10770         }
10771
10772       /* See if this can be simplified into a rotate first.  If that
10773          is unsuccessful continue in the association code.  */
10774       goto bit_rotate;
10775
10776     case BIT_AND_EXPR:
10777       if (integer_all_onesp (arg1))
10778         return non_lvalue (fold_convert (type, arg0));
10779       if (integer_zerop (arg1))
10780         return omit_one_operand (type, arg1, arg0);
10781       if (operand_equal_p (arg0, arg1, 0))
10782         return non_lvalue (fold_convert (type, arg0));
10783
10784       /* ~X & X is always zero.  */
10785       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10786           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10787         return omit_one_operand (type, integer_zero_node, arg1);
10788
10789       /* X & ~X is always zero.  */
10790       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10791           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10792         return omit_one_operand (type, integer_zero_node, arg0);
10793
10794       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
10795       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10796           && TREE_CODE (arg1) == INTEGER_CST
10797           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10798         return fold_build2 (BIT_IOR_EXPR, type,
10799                             fold_build2 (BIT_AND_EXPR, type,
10800                                          TREE_OPERAND (arg0, 0), arg1),
10801                             fold_build2 (BIT_AND_EXPR, type,
10802                                          TREE_OPERAND (arg0, 1), arg1));
10803
10804       /* (X | Y) & Y is (X, Y).  */
10805       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10806           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10807         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10808       /* (X | Y) & X is (Y, X).  */
10809       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10810           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10811           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10812         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10813       /* X & (X | Y) is (Y, X).  */
10814       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10815           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10816           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10817         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10818       /* X & (Y | X) is (Y, X).  */
10819       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10820           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10821           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10822         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10823
10824       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
10825       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10826           && integer_onep (TREE_OPERAND (arg0, 1))
10827           && integer_onep (arg1))
10828         {
10829           tem = TREE_OPERAND (arg0, 0);
10830           return fold_build2 (EQ_EXPR, type,
10831                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10832                                            build_int_cst (TREE_TYPE (tem), 1)),
10833                               build_int_cst (TREE_TYPE (tem), 0));
10834         }
10835       /* Fold ~X & 1 as (X & 1) == 0.  */
10836       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10837           && integer_onep (arg1))
10838         {
10839           tem = TREE_OPERAND (arg0, 0);
10840           return fold_build2 (EQ_EXPR, type,
10841                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10842                                            build_int_cst (TREE_TYPE (tem), 1)),
10843                               build_int_cst (TREE_TYPE (tem), 0));
10844         }
10845
10846       /* Fold (X ^ Y) & Y as ~X & Y.  */
10847       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10848           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10849         {
10850           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10851           return fold_build2 (BIT_AND_EXPR, type, 
10852                               fold_build1 (BIT_NOT_EXPR, type, tem),
10853                               fold_convert (type, arg1));
10854         }
10855       /* Fold (X ^ Y) & X as ~Y & X.  */
10856       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10857           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10858           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10859         {
10860           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10861           return fold_build2 (BIT_AND_EXPR, type,
10862                               fold_build1 (BIT_NOT_EXPR, type, tem),
10863                               fold_convert (type, arg1));
10864         }
10865       /* Fold X & (X ^ Y) as X & ~Y.  */
10866       if (TREE_CODE (arg1) == BIT_XOR_EXPR
10867           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10868         {
10869           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10870           return fold_build2 (BIT_AND_EXPR, type,
10871                               fold_convert (type, arg0),
10872                               fold_build1 (BIT_NOT_EXPR, type, tem));
10873         }
10874       /* Fold X & (Y ^ X) as ~Y & X.  */
10875       if (TREE_CODE (arg1) == BIT_XOR_EXPR
10876           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10877           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10878         {
10879           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10880           return fold_build2 (BIT_AND_EXPR, type,
10881                               fold_build1 (BIT_NOT_EXPR, type, tem),
10882                               fold_convert (type, arg0));
10883         }
10884
10885       t1 = distribute_bit_expr (code, type, arg0, arg1);
10886       if (t1 != NULL_TREE)
10887         return t1;
10888       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
10889       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10890           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10891         {
10892           unsigned int prec
10893             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10894
10895           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
10896               && (~TREE_INT_CST_LOW (arg1)
10897                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
10898             return fold_convert (type, TREE_OPERAND (arg0, 0));
10899         }
10900
10901       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
10902
10903          This results in more efficient code for machines without a NOR
10904          instruction.  Combine will canonicalize to the first form
10905          which will allow use of NOR instructions provided by the
10906          backend if they exist.  */
10907       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10908           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10909         {
10910           return fold_build1 (BIT_NOT_EXPR, type,
10911                               build2 (BIT_IOR_EXPR, type,
10912                                       TREE_OPERAND (arg0, 0),
10913                                       TREE_OPERAND (arg1, 0)));
10914         }
10915
10916       goto associate;
10917
10918     case RDIV_EXPR:
10919       /* Don't touch a floating-point divide by zero unless the mode
10920          of the constant can represent infinity.  */
10921       if (TREE_CODE (arg1) == REAL_CST
10922           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10923           && real_zerop (arg1))
10924         return NULL_TREE;
10925
10926       /* Optimize A / A to 1.0 if we don't care about
10927          NaNs or Infinities.  Skip the transformation
10928          for non-real operands.  */
10929       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
10930           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10931           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
10932           && operand_equal_p (arg0, arg1, 0))
10933         {
10934           tree r = build_real (TREE_TYPE (arg0), dconst1);
10935
10936           return omit_two_operands (type, r, arg0, arg1);
10937         }
10938
10939       /* The complex version of the above A / A optimization.  */
10940       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10941           && operand_equal_p (arg0, arg1, 0))
10942         {
10943           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
10944           if (! HONOR_NANS (TYPE_MODE (elem_type))
10945               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
10946             {
10947               tree r = build_real (elem_type, dconst1);
10948               /* omit_two_operands will call fold_convert for us.  */
10949               return omit_two_operands (type, r, arg0, arg1);
10950             }
10951         }
10952
10953       /* (-A) / (-B) -> A / B  */
10954       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10955         return fold_build2 (RDIV_EXPR, type,
10956                             TREE_OPERAND (arg0, 0),
10957                             negate_expr (arg1));
10958       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10959         return fold_build2 (RDIV_EXPR, type,
10960                             negate_expr (arg0),
10961                             TREE_OPERAND (arg1, 0));
10962
10963       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
10964       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10965           && real_onep (arg1))
10966         return non_lvalue (fold_convert (type, arg0));
10967
10968       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
10969       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10970           && real_minus_onep (arg1))
10971         return non_lvalue (fold_convert (type, negate_expr (arg0)));
10972
10973       /* If ARG1 is a constant, we can convert this to a multiply by the
10974          reciprocal.  This does not have the same rounding properties,
10975          so only do this if -freciprocal-math.  We can actually
10976          always safely do it if ARG1 is a power of two, but it's hard to
10977          tell if it is or not in a portable manner.  */
10978       if (TREE_CODE (arg1) == REAL_CST)
10979         {
10980           if (flag_reciprocal_math
10981               && 0 != (tem = const_binop (code, build_real (type, dconst1),
10982                                           arg1, 0)))
10983             return fold_build2 (MULT_EXPR, type, arg0, tem);
10984           /* Find the reciprocal if optimizing and the result is exact.  */
10985           if (optimize)
10986             {
10987               REAL_VALUE_TYPE r;
10988               r = TREE_REAL_CST (arg1);
10989               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
10990                 {
10991                   tem = build_real (type, r);
10992                   return fold_build2 (MULT_EXPR, type,
10993                                       fold_convert (type, arg0), tem);
10994                 }
10995             }
10996         }
10997       /* Convert A/B/C to A/(B*C).  */ 
10998       if (flag_reciprocal_math
10999           && TREE_CODE (arg0) == RDIV_EXPR)
11000         return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11001                             fold_build2 (MULT_EXPR, type,
11002                                          TREE_OPERAND (arg0, 1), arg1));
11003
11004       /* Convert A/(B/C) to (A/B)*C.  */
11005       if (flag_reciprocal_math
11006           && TREE_CODE (arg1) == RDIV_EXPR)
11007         return fold_build2 (MULT_EXPR, type,
11008                             fold_build2 (RDIV_EXPR, type, arg0,
11009                                          TREE_OPERAND (arg1, 0)),
11010                             TREE_OPERAND (arg1, 1));
11011
11012       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11013       if (flag_reciprocal_math
11014           && TREE_CODE (arg1) == MULT_EXPR
11015           && TREE_CODE (arg0) == REAL_CST
11016           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11017         {
11018           tree tem = const_binop (RDIV_EXPR, arg0,
11019                                   TREE_OPERAND (arg1, 1), 0);
11020           if (tem)
11021             return fold_build2 (RDIV_EXPR, type, tem,
11022                                 TREE_OPERAND (arg1, 0));
11023         }
11024
11025       if (flag_unsafe_math_optimizations)
11026         {
11027           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11028           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11029
11030           /* Optimize sin(x)/cos(x) as tan(x).  */
11031           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11032                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11033                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11034               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11035                                   CALL_EXPR_ARG (arg1, 0), 0))
11036             {
11037               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11038
11039               if (tanfn != NULL_TREE)
11040                 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11041             }
11042
11043           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11044           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11045                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11046                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11047               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11048                                   CALL_EXPR_ARG (arg1, 0), 0))
11049             {
11050               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11051
11052               if (tanfn != NULL_TREE)
11053                 {
11054                   tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11055                   return fold_build2 (RDIV_EXPR, type,
11056                                       build_real (type, dconst1), tmp);
11057                 }
11058             }
11059
11060           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11061              NaNs or Infinities.  */
11062           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11063                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11064                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11065             {
11066               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11067               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11068
11069               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11070                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11071                   && operand_equal_p (arg00, arg01, 0))
11072                 {
11073                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11074
11075                   if (cosfn != NULL_TREE)
11076                     return build_call_expr (cosfn, 1, arg00);
11077                 }
11078             }
11079
11080           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11081              NaNs or Infinities.  */
11082           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11083                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11084                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11085             {
11086               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11087               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11088
11089               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11090                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11091                   && operand_equal_p (arg00, arg01, 0))
11092                 {
11093                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11094
11095                   if (cosfn != NULL_TREE)
11096                     {
11097                       tree tmp = build_call_expr (cosfn, 1, arg00);
11098                       return fold_build2 (RDIV_EXPR, type,
11099                                           build_real (type, dconst1),
11100                                           tmp);
11101                     }
11102                 }
11103             }
11104
11105           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11106           if (fcode0 == BUILT_IN_POW
11107               || fcode0 == BUILT_IN_POWF
11108               || fcode0 == BUILT_IN_POWL)
11109             {
11110               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11111               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11112               if (TREE_CODE (arg01) == REAL_CST
11113                   && !TREE_OVERFLOW (arg01)
11114                   && operand_equal_p (arg1, arg00, 0))
11115                 {
11116                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11117                   REAL_VALUE_TYPE c;
11118                   tree arg;
11119
11120                   c = TREE_REAL_CST (arg01);
11121                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11122                   arg = build_real (type, c);
11123                   return build_call_expr (powfn, 2, arg1, arg);
11124                 }
11125             }
11126
11127           /* Optimize a/root(b/c) into a*root(c/b).  */
11128           if (BUILTIN_ROOT_P (fcode1))
11129             {
11130               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11131
11132               if (TREE_CODE (rootarg) == RDIV_EXPR)
11133                 {
11134                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11135                   tree b = TREE_OPERAND (rootarg, 0);
11136                   tree c = TREE_OPERAND (rootarg, 1);
11137
11138                   tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11139
11140                   tmp = build_call_expr (rootfn, 1, tmp);
11141                   return fold_build2 (MULT_EXPR, type, arg0, tmp);
11142                 }
11143             }
11144
11145           /* Optimize x/expN(y) into x*expN(-y).  */
11146           if (BUILTIN_EXPONENT_P (fcode1))
11147             {
11148               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11149               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11150               arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11151               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11152             }
11153
11154           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11155           if (fcode1 == BUILT_IN_POW
11156               || fcode1 == BUILT_IN_POWF
11157               || fcode1 == BUILT_IN_POWL)
11158             {
11159               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11160               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11161               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11162               tree neg11 = fold_convert (type, negate_expr (arg11));
11163               arg1 = build_call_expr (powfn, 2, arg10, neg11);
11164               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11165             }
11166         }
11167       return NULL_TREE;
11168
11169     case TRUNC_DIV_EXPR:
11170     case FLOOR_DIV_EXPR:
11171       /* Simplify A / (B << N) where A and B are positive and B is
11172          a power of 2, to A >> (N + log2(B)).  */
11173       strict_overflow_p = false;
11174       if (TREE_CODE (arg1) == LSHIFT_EXPR
11175           && (TYPE_UNSIGNED (type)
11176               || tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)))
11177         {
11178           tree sval = TREE_OPERAND (arg1, 0);
11179           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11180             {
11181               tree sh_cnt = TREE_OPERAND (arg1, 1);
11182               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11183
11184               if (strict_overflow_p)
11185                 fold_overflow_warning (("assuming signed overflow does not "
11186                                         "occur when simplifying A / (B << N)"),
11187                                        WARN_STRICT_OVERFLOW_MISC);
11188
11189               sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11190                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
11191               return fold_build2 (RSHIFT_EXPR, type,
11192                                   fold_convert (type, arg0), sh_cnt);
11193             }
11194         }
11195       /* Fall thru */
11196
11197     case ROUND_DIV_EXPR:
11198     case CEIL_DIV_EXPR:
11199     case EXACT_DIV_EXPR:
11200       if (integer_onep (arg1))
11201         return non_lvalue (fold_convert (type, arg0));
11202       if (integer_zerop (arg1))
11203         return NULL_TREE;
11204       /* X / -1 is -X.  */
11205       if (!TYPE_UNSIGNED (type)
11206           && TREE_CODE (arg1) == INTEGER_CST
11207           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11208           && TREE_INT_CST_HIGH (arg1) == -1)
11209         return fold_convert (type, negate_expr (arg0));
11210
11211       /* Convert -A / -B to A / B when the type is signed and overflow is
11212          undefined.  */
11213       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11214           && TREE_CODE (arg0) == NEGATE_EXPR
11215           && negate_expr_p (arg1))
11216         {
11217           if (INTEGRAL_TYPE_P (type))
11218             fold_overflow_warning (("assuming signed overflow does not occur "
11219                                     "when distributing negation across "
11220                                     "division"),
11221                                    WARN_STRICT_OVERFLOW_MISC);
11222           return fold_build2 (code, type,
11223                               fold_convert (type, TREE_OPERAND (arg0, 0)),
11224                               negate_expr (arg1));
11225         }
11226       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11227           && TREE_CODE (arg1) == NEGATE_EXPR
11228           && negate_expr_p (arg0))
11229         {
11230           if (INTEGRAL_TYPE_P (type))
11231             fold_overflow_warning (("assuming signed overflow does not occur "
11232                                     "when distributing negation across "
11233                                     "division"),
11234                                    WARN_STRICT_OVERFLOW_MISC);
11235           return fold_build2 (code, type, negate_expr (arg0),
11236                               TREE_OPERAND (arg1, 0));
11237         }
11238
11239       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11240          operation, EXACT_DIV_EXPR.
11241
11242          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11243          At one time others generated faster code, it's not clear if they do
11244          after the last round to changes to the DIV code in expmed.c.  */
11245       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11246           && multiple_of_p (type, arg0, arg1))
11247         return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11248
11249       strict_overflow_p = false;
11250       if (TREE_CODE (arg1) == INTEGER_CST
11251           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11252                                          &strict_overflow_p)))
11253         {
11254           if (strict_overflow_p)
11255             fold_overflow_warning (("assuming signed overflow does not occur "
11256                                     "when simplifying division"),
11257                                    WARN_STRICT_OVERFLOW_MISC);
11258           return fold_convert (type, tem);
11259         }
11260
11261       return NULL_TREE;
11262
11263     case CEIL_MOD_EXPR:
11264     case FLOOR_MOD_EXPR:
11265     case ROUND_MOD_EXPR:
11266     case TRUNC_MOD_EXPR:
11267       /* X % 1 is always zero, but be sure to preserve any side
11268          effects in X.  */
11269       if (integer_onep (arg1))
11270         return omit_one_operand (type, integer_zero_node, arg0);
11271
11272       /* X % 0, return X % 0 unchanged so that we can get the
11273          proper warnings and errors.  */
11274       if (integer_zerop (arg1))
11275         return NULL_TREE;
11276
11277       /* 0 % X is always zero, but be sure to preserve any side
11278          effects in X.  Place this after checking for X == 0.  */
11279       if (integer_zerop (arg0))
11280         return omit_one_operand (type, integer_zero_node, arg1);
11281
11282       /* X % -1 is zero.  */
11283       if (!TYPE_UNSIGNED (type)
11284           && TREE_CODE (arg1) == INTEGER_CST
11285           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11286           && TREE_INT_CST_HIGH (arg1) == -1)
11287         return omit_one_operand (type, integer_zero_node, arg0);
11288
11289       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11290          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11291       strict_overflow_p = false;
11292       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11293           && (TYPE_UNSIGNED (type)
11294               || tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)))
11295         {
11296           tree c = arg1;
11297           /* Also optimize A % (C << N)  where C is a power of 2,
11298              to A & ((C << N) - 1).  */
11299           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11300             c = TREE_OPERAND (arg1, 0);
11301
11302           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11303             {
11304               tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11305                                        build_int_cst (TREE_TYPE (arg1), 1));
11306               if (strict_overflow_p)
11307                 fold_overflow_warning (("assuming signed overflow does not "
11308                                         "occur when simplifying "
11309                                         "X % (power of two)"),
11310                                        WARN_STRICT_OVERFLOW_MISC);
11311               return fold_build2 (BIT_AND_EXPR, type,
11312                                   fold_convert (type, arg0),
11313                                   fold_convert (type, mask));
11314             }
11315         }
11316
11317       /* X % -C is the same as X % C.  */
11318       if (code == TRUNC_MOD_EXPR
11319           && !TYPE_UNSIGNED (type)
11320           && TREE_CODE (arg1) == INTEGER_CST
11321           && !TREE_OVERFLOW (arg1)
11322           && TREE_INT_CST_HIGH (arg1) < 0
11323           && !TYPE_OVERFLOW_TRAPS (type)
11324           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11325           && !sign_bit_p (arg1, arg1))
11326         return fold_build2 (code, type, fold_convert (type, arg0),
11327                             fold_convert (type, negate_expr (arg1)));
11328
11329       /* X % -Y is the same as X % Y.  */
11330       if (code == TRUNC_MOD_EXPR
11331           && !TYPE_UNSIGNED (type)
11332           && TREE_CODE (arg1) == NEGATE_EXPR
11333           && !TYPE_OVERFLOW_TRAPS (type))
11334         return fold_build2 (code, type, fold_convert (type, arg0),
11335                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11336
11337       if (TREE_CODE (arg1) == INTEGER_CST
11338           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11339                                          &strict_overflow_p)))
11340         {
11341           if (strict_overflow_p)
11342             fold_overflow_warning (("assuming signed overflow does not occur "
11343                                     "when simplifying modulos"),
11344                                    WARN_STRICT_OVERFLOW_MISC);
11345           return fold_convert (type, tem);
11346         }
11347
11348       return NULL_TREE;
11349
11350     case LROTATE_EXPR:
11351     case RROTATE_EXPR:
11352       if (integer_all_onesp (arg0))
11353         return omit_one_operand (type, arg0, arg1);
11354       goto shift;
11355
11356     case RSHIFT_EXPR:
11357       /* Optimize -1 >> x for arithmetic right shifts.  */
11358       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
11359         return omit_one_operand (type, arg0, arg1);
11360       /* ... fall through ...  */
11361
11362     case LSHIFT_EXPR:
11363     shift:
11364       if (integer_zerop (arg1))
11365         return non_lvalue (fold_convert (type, arg0));
11366       if (integer_zerop (arg0))
11367         return omit_one_operand (type, arg0, arg1);
11368
11369       /* Since negative shift count is not well-defined,
11370          don't try to compute it in the compiler.  */
11371       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11372         return NULL_TREE;
11373
11374       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11375       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11376           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11377           && host_integerp (TREE_OPERAND (arg0, 1), false)
11378           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11379         {
11380           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11381                                + TREE_INT_CST_LOW (arg1));
11382
11383           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11384              being well defined.  */
11385           if (low >= TYPE_PRECISION (type))
11386             {
11387               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11388                 low = low % TYPE_PRECISION (type);
11389               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11390                 return build_int_cst (type, 0);
11391               else
11392                 low = TYPE_PRECISION (type) - 1;
11393             }
11394
11395           return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11396                               build_int_cst (type, low));
11397         }
11398
11399       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11400          into x & ((unsigned)-1 >> c) for unsigned types.  */
11401       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11402            || (TYPE_UNSIGNED (type)
11403                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11404           && host_integerp (arg1, false)
11405           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11406           && host_integerp (TREE_OPERAND (arg0, 1), false)
11407           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11408         {
11409           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11410           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11411           tree lshift;
11412           tree arg00;
11413
11414           if (low0 == low1)
11415             {
11416               arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11417
11418               lshift = build_int_cst (type, -1);
11419               lshift = int_const_binop (code, lshift, arg1, 0);
11420
11421               return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11422             }
11423         }
11424
11425       /* Rewrite an LROTATE_EXPR by a constant into an
11426          RROTATE_EXPR by a new constant.  */
11427       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11428         {
11429           tree tem = build_int_cst (TREE_TYPE (arg1),
11430                                     GET_MODE_BITSIZE (TYPE_MODE (type)));
11431           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11432           return fold_build2 (RROTATE_EXPR, type, arg0, tem);
11433         }
11434
11435       /* If we have a rotate of a bit operation with the rotate count and
11436          the second operand of the bit operation both constant,
11437          permute the two operations.  */
11438       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11439           && (TREE_CODE (arg0) == BIT_AND_EXPR
11440               || TREE_CODE (arg0) == BIT_IOR_EXPR
11441               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11442           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11443         return fold_build2 (TREE_CODE (arg0), type,
11444                             fold_build2 (code, type,
11445                                          TREE_OPERAND (arg0, 0), arg1),
11446                             fold_build2 (code, type,
11447                                          TREE_OPERAND (arg0, 1), arg1));
11448
11449       /* Two consecutive rotates adding up to the width of the mode can
11450          be ignored.  */
11451       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11452           && TREE_CODE (arg0) == RROTATE_EXPR
11453           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11454           && TREE_INT_CST_HIGH (arg1) == 0
11455           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11456           && ((TREE_INT_CST_LOW (arg1)
11457                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11458               == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
11459         return TREE_OPERAND (arg0, 0);
11460
11461       return NULL_TREE;
11462
11463     case MIN_EXPR:
11464       if (operand_equal_p (arg0, arg1, 0))
11465         return omit_one_operand (type, arg0, arg1);
11466       if (INTEGRAL_TYPE_P (type)
11467           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11468         return omit_one_operand (type, arg1, arg0);
11469       tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11470       if (tem)
11471         return tem;
11472       goto associate;
11473
11474     case MAX_EXPR:
11475       if (operand_equal_p (arg0, arg1, 0))
11476         return omit_one_operand (type, arg0, arg1);
11477       if (INTEGRAL_TYPE_P (type)
11478           && TYPE_MAX_VALUE (type)
11479           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11480         return omit_one_operand (type, arg1, arg0);
11481       tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11482       if (tem)
11483         return tem;
11484       goto associate;
11485
11486     case TRUTH_ANDIF_EXPR:
11487       /* Note that the operands of this must be ints
11488          and their values must be 0 or 1.
11489          ("true" is a fixed value perhaps depending on the language.)  */
11490       /* If first arg is constant zero, return it.  */
11491       if (integer_zerop (arg0))
11492         return fold_convert (type, arg0);
11493     case TRUTH_AND_EXPR:
11494       /* If either arg is constant true, drop it.  */
11495       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11496         return non_lvalue (fold_convert (type, arg1));
11497       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11498           /* Preserve sequence points.  */
11499           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11500         return non_lvalue (fold_convert (type, arg0));
11501       /* If second arg is constant zero, result is zero, but first arg
11502          must be evaluated.  */
11503       if (integer_zerop (arg1))
11504         return omit_one_operand (type, arg1, arg0);
11505       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11506          case will be handled here.  */
11507       if (integer_zerop (arg0))
11508         return omit_one_operand (type, arg0, arg1);
11509
11510       /* !X && X is always false.  */
11511       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11512           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11513         return omit_one_operand (type, integer_zero_node, arg1);
11514       /* X && !X is always false.  */
11515       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11516           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11517         return omit_one_operand (type, integer_zero_node, arg0);
11518
11519       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
11520          means A >= Y && A != MAX, but in this case we know that
11521          A < X <= MAX.  */
11522
11523       if (!TREE_SIDE_EFFECTS (arg0)
11524           && !TREE_SIDE_EFFECTS (arg1))
11525         {
11526           tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
11527           if (tem && !operand_equal_p (tem, arg0, 0))
11528             return fold_build2 (code, type, tem, arg1);
11529
11530           tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
11531           if (tem && !operand_equal_p (tem, arg1, 0))
11532             return fold_build2 (code, type, arg0, tem);
11533         }
11534
11535     truth_andor:
11536       /* We only do these simplifications if we are optimizing.  */
11537       if (!optimize)
11538         return NULL_TREE;
11539
11540       /* Check for things like (A || B) && (A || C).  We can convert this
11541          to A || (B && C).  Note that either operator can be any of the four
11542          truth and/or operations and the transformation will still be
11543          valid.   Also note that we only care about order for the
11544          ANDIF and ORIF operators.  If B contains side effects, this
11545          might change the truth-value of A.  */
11546       if (TREE_CODE (arg0) == TREE_CODE (arg1)
11547           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11548               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11549               || TREE_CODE (arg0) == TRUTH_AND_EXPR
11550               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11551           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11552         {
11553           tree a00 = TREE_OPERAND (arg0, 0);
11554           tree a01 = TREE_OPERAND (arg0, 1);
11555           tree a10 = TREE_OPERAND (arg1, 0);
11556           tree a11 = TREE_OPERAND (arg1, 1);
11557           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11558                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11559                              && (code == TRUTH_AND_EXPR
11560                                  || code == TRUTH_OR_EXPR));
11561
11562           if (operand_equal_p (a00, a10, 0))
11563             return fold_build2 (TREE_CODE (arg0), type, a00,
11564                                 fold_build2 (code, type, a01, a11));
11565           else if (commutative && operand_equal_p (a00, a11, 0))
11566             return fold_build2 (TREE_CODE (arg0), type, a00,
11567                                 fold_build2 (code, type, a01, a10));
11568           else if (commutative && operand_equal_p (a01, a10, 0))
11569             return fold_build2 (TREE_CODE (arg0), type, a01,
11570                                 fold_build2 (code, type, a00, a11));
11571
11572           /* This case if tricky because we must either have commutative
11573              operators or else A10 must not have side-effects.  */
11574
11575           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
11576                    && operand_equal_p (a01, a11, 0))
11577             return fold_build2 (TREE_CODE (arg0), type,
11578                                 fold_build2 (code, type, a00, a10),
11579                                 a01);
11580         }
11581
11582       /* See if we can build a range comparison.  */
11583       if (0 != (tem = fold_range_test (code, type, op0, op1)))
11584         return tem;
11585
11586       /* Check for the possibility of merging component references.  If our
11587          lhs is another similar operation, try to merge its rhs with our
11588          rhs.  Then try to merge our lhs and rhs.  */
11589       if (TREE_CODE (arg0) == code
11590           && 0 != (tem = fold_truthop (code, type,
11591                                        TREE_OPERAND (arg0, 1), arg1)))
11592         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11593
11594       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
11595         return tem;
11596
11597       return NULL_TREE;
11598
11599     case TRUTH_ORIF_EXPR:
11600       /* Note that the operands of this must be ints
11601          and their values must be 0 or true.
11602          ("true" is a fixed value perhaps depending on the language.)  */
11603       /* If first arg is constant true, return it.  */
11604       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11605         return fold_convert (type, arg0);
11606     case TRUTH_OR_EXPR:
11607       /* If either arg is constant zero, drop it.  */
11608       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
11609         return non_lvalue (fold_convert (type, arg1));
11610       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
11611           /* Preserve sequence points.  */
11612           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11613         return non_lvalue (fold_convert (type, arg0));
11614       /* If second arg is constant true, result is true, but we must
11615          evaluate first arg.  */
11616       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
11617         return omit_one_operand (type, arg1, arg0);
11618       /* Likewise for first arg, but note this only occurs here for
11619          TRUTH_OR_EXPR.  */
11620       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11621         return omit_one_operand (type, arg0, arg1);
11622
11623       /* !X || X is always true.  */
11624       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11625           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11626         return omit_one_operand (type, integer_one_node, arg1);
11627       /* X || !X is always true.  */
11628       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11629           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11630         return omit_one_operand (type, integer_one_node, arg0);
11631
11632       goto truth_andor;
11633
11634     case TRUTH_XOR_EXPR:
11635       /* If the second arg is constant zero, drop it.  */
11636       if (integer_zerop (arg1))
11637         return non_lvalue (fold_convert (type, arg0));
11638       /* If the second arg is constant true, this is a logical inversion.  */
11639       if (integer_onep (arg1))
11640         {
11641           /* Only call invert_truthvalue if operand is a truth value.  */
11642           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
11643             tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
11644           else
11645             tem = invert_truthvalue (arg0);
11646           return non_lvalue (fold_convert (type, tem));
11647         }
11648       /* Identical arguments cancel to zero.  */
11649       if (operand_equal_p (arg0, arg1, 0))
11650         return omit_one_operand (type, integer_zero_node, arg0);
11651
11652       /* !X ^ X is always true.  */
11653       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11654           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11655         return omit_one_operand (type, integer_one_node, arg1);
11656
11657       /* X ^ !X is always true.  */
11658       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11659           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11660         return omit_one_operand (type, integer_one_node, arg0);
11661
11662       return NULL_TREE;
11663
11664     case EQ_EXPR:
11665     case NE_EXPR:
11666       tem = fold_comparison (code, type, op0, op1);
11667       if (tem != NULL_TREE)
11668         return tem;
11669
11670       /* bool_var != 0 becomes bool_var. */
11671       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11672           && code == NE_EXPR)
11673         return non_lvalue (fold_convert (type, arg0));
11674
11675       /* bool_var == 1 becomes bool_var. */
11676       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11677           && code == EQ_EXPR)
11678         return non_lvalue (fold_convert (type, arg0));
11679
11680       /* bool_var != 1 becomes !bool_var. */
11681       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11682           && code == NE_EXPR)
11683         return fold_build1 (TRUTH_NOT_EXPR, type, arg0);
11684
11685       /* bool_var == 0 becomes !bool_var. */
11686       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11687           && code == EQ_EXPR)
11688         return fold_build1 (TRUTH_NOT_EXPR, type, arg0);
11689
11690       /* If this is an equality comparison of the address of two non-weak,
11691          unaliased symbols neither of which are extern (since we do not
11692          have access to attributes for externs), then we know the result.  */
11693       if (TREE_CODE (arg0) == ADDR_EXPR
11694           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
11695           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
11696           && ! lookup_attribute ("alias",
11697                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
11698           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
11699           && TREE_CODE (arg1) == ADDR_EXPR
11700           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
11701           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
11702           && ! lookup_attribute ("alias",
11703                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
11704           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
11705         {
11706           /* We know that we're looking at the address of two
11707              non-weak, unaliased, static _DECL nodes.
11708
11709              It is both wasteful and incorrect to call operand_equal_p
11710              to compare the two ADDR_EXPR nodes.  It is wasteful in that
11711              all we need to do is test pointer equality for the arguments
11712              to the two ADDR_EXPR nodes.  It is incorrect to use
11713              operand_equal_p as that function is NOT equivalent to a
11714              C equality test.  It can in fact return false for two
11715              objects which would test as equal using the C equality
11716              operator.  */
11717           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
11718           return constant_boolean_node (equal
11719                                         ? code == EQ_EXPR : code != EQ_EXPR,
11720                                         type);
11721         }
11722
11723       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
11724          a MINUS_EXPR of a constant, we can convert it into a comparison with
11725          a revised constant as long as no overflow occurs.  */
11726       if (TREE_CODE (arg1) == INTEGER_CST
11727           && (TREE_CODE (arg0) == PLUS_EXPR
11728               || TREE_CODE (arg0) == MINUS_EXPR)
11729           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11730           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
11731                                       ? MINUS_EXPR : PLUS_EXPR,
11732                                       fold_convert (TREE_TYPE (arg0), arg1),
11733                                       TREE_OPERAND (arg0, 1), 0))
11734           && !TREE_OVERFLOW (tem))
11735         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11736
11737       /* Similarly for a NEGATE_EXPR.  */
11738       if (TREE_CODE (arg0) == NEGATE_EXPR
11739           && TREE_CODE (arg1) == INTEGER_CST
11740           && 0 != (tem = negate_expr (arg1))
11741           && TREE_CODE (tem) == INTEGER_CST
11742           && !TREE_OVERFLOW (tem))
11743         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11744
11745       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
11746       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11747           && TREE_CODE (arg1) == INTEGER_CST
11748           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11749         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11750                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
11751                                          fold_convert (TREE_TYPE (arg0), arg1),
11752                                          TREE_OPERAND (arg0, 1)));
11753
11754       /* Transform comparisons of the form X +- C CMP X.  */
11755       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11756           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11757           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11758           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11759               || POINTER_TYPE_P (TREE_TYPE (arg0))))
11760         {
11761           tree cst = TREE_OPERAND (arg0, 1);
11762
11763           if (code == EQ_EXPR
11764               && !integer_zerop (cst))
11765             return omit_two_operands (type, boolean_false_node,
11766                                       TREE_OPERAND (arg0, 0), arg1);
11767           else
11768             return omit_two_operands (type, boolean_true_node,
11769                                       TREE_OPERAND (arg0, 0), arg1);
11770         }
11771
11772       /* If we have X - Y == 0, we can convert that to X == Y and similarly
11773          for !=.  Don't do this for ordered comparisons due to overflow.  */
11774       if (TREE_CODE (arg0) == MINUS_EXPR
11775           && integer_zerop (arg1))
11776         return fold_build2 (code, type,
11777                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
11778
11779       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
11780       if (TREE_CODE (arg0) == ABS_EXPR
11781           && (integer_zerop (arg1) || real_zerop (arg1)))
11782         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
11783
11784       /* If this is an EQ or NE comparison with zero and ARG0 is
11785          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
11786          two operations, but the latter can be done in one less insn
11787          on machines that have only two-operand insns or on which a
11788          constant cannot be the first operand.  */
11789       if (TREE_CODE (arg0) == BIT_AND_EXPR
11790           && integer_zerop (arg1))
11791         {
11792           tree arg00 = TREE_OPERAND (arg0, 0);
11793           tree arg01 = TREE_OPERAND (arg0, 1);
11794           if (TREE_CODE (arg00) == LSHIFT_EXPR
11795               && integer_onep (TREE_OPERAND (arg00, 0)))
11796             return
11797               fold_build2 (code, type,
11798                            build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11799                                    build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
11800                                            arg01, TREE_OPERAND (arg00, 1)),
11801                                    fold_convert (TREE_TYPE (arg0),
11802                                                  integer_one_node)),
11803                            arg1);
11804           else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
11805                    && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
11806             return
11807               fold_build2 (code, type,
11808                            build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11809                                    build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
11810                                            arg00, TREE_OPERAND (arg01, 1)),
11811                                    fold_convert (TREE_TYPE (arg0),
11812                                                  integer_one_node)),
11813                            arg1);
11814         }
11815
11816       /* If this is an NE or EQ comparison of zero against the result of a
11817          signed MOD operation whose second operand is a power of 2, make
11818          the MOD operation unsigned since it is simpler and equivalent.  */
11819       if (integer_zerop (arg1)
11820           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
11821           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
11822               || TREE_CODE (arg0) == CEIL_MOD_EXPR
11823               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
11824               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
11825           && integer_pow2p (TREE_OPERAND (arg0, 1)))
11826         {
11827           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
11828           tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
11829                                      fold_convert (newtype,
11830                                                    TREE_OPERAND (arg0, 0)),
11831                                      fold_convert (newtype,
11832                                                    TREE_OPERAND (arg0, 1)));
11833
11834           return fold_build2 (code, type, newmod,
11835                               fold_convert (newtype, arg1));
11836         }
11837
11838       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
11839          C1 is a valid shift constant, and C2 is a power of two, i.e.
11840          a single bit.  */
11841       if (TREE_CODE (arg0) == BIT_AND_EXPR
11842           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
11843           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
11844              == INTEGER_CST
11845           && integer_pow2p (TREE_OPERAND (arg0, 1))
11846           && integer_zerop (arg1))
11847         {
11848           tree itype = TREE_TYPE (arg0);
11849           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
11850           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
11851
11852           /* Check for a valid shift count.  */
11853           if (TREE_INT_CST_HIGH (arg001) == 0
11854               && TREE_INT_CST_LOW (arg001) < prec)
11855             {
11856               tree arg01 = TREE_OPERAND (arg0, 1);
11857               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
11858               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
11859               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
11860                  can be rewritten as (X & (C2 << C1)) != 0.  */
11861               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
11862                 {
11863                   tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
11864                   tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
11865                   return fold_build2 (code, type, tem, arg1);
11866                 }
11867               /* Otherwise, for signed (arithmetic) shifts,
11868                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
11869                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
11870               else if (!TYPE_UNSIGNED (itype))
11871                 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
11872                                     arg000, build_int_cst (itype, 0));
11873               /* Otherwise, of unsigned (logical) shifts,
11874                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
11875                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
11876               else
11877                 return omit_one_operand (type,
11878                                          code == EQ_EXPR ? integer_one_node
11879                                                          : integer_zero_node,
11880                                          arg000);
11881             }
11882         }
11883
11884       /* If this is an NE comparison of zero with an AND of one, remove the
11885          comparison since the AND will give the correct value.  */
11886       if (code == NE_EXPR
11887           && integer_zerop (arg1)
11888           && TREE_CODE (arg0) == BIT_AND_EXPR
11889           && integer_onep (TREE_OPERAND (arg0, 1)))
11890         return fold_convert (type, arg0);
11891
11892       /* If we have (A & C) == C where C is a power of 2, convert this into
11893          (A & C) != 0.  Similarly for NE_EXPR.  */
11894       if (TREE_CODE (arg0) == BIT_AND_EXPR
11895           && integer_pow2p (TREE_OPERAND (arg0, 1))
11896           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11897         return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11898                             arg0, fold_convert (TREE_TYPE (arg0),
11899                                                 integer_zero_node));
11900
11901       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
11902          bit, then fold the expression into A < 0 or A >= 0.  */
11903       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
11904       if (tem)
11905         return tem;
11906
11907       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
11908          Similarly for NE_EXPR.  */
11909       if (TREE_CODE (arg0) == BIT_AND_EXPR
11910           && TREE_CODE (arg1) == INTEGER_CST
11911           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11912         {
11913           tree notc = fold_build1 (BIT_NOT_EXPR,
11914                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
11915                                    TREE_OPERAND (arg0, 1));
11916           tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11917                                        arg1, notc);
11918           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
11919           if (integer_nonzerop (dandnotc))
11920             return omit_one_operand (type, rslt, arg0);
11921         }
11922
11923       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
11924          Similarly for NE_EXPR.  */
11925       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11926           && TREE_CODE (arg1) == INTEGER_CST
11927           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11928         {
11929           tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
11930           tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11931                                        TREE_OPERAND (arg0, 1), notd);
11932           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
11933           if (integer_nonzerop (candnotd))
11934             return omit_one_operand (type, rslt, arg0);
11935         }
11936
11937       /* If this is a comparison of a field, we may be able to simplify it.  */
11938       if ((TREE_CODE (arg0) == COMPONENT_REF
11939            || TREE_CODE (arg0) == BIT_FIELD_REF)
11940           /* Handle the constant case even without -O
11941              to make sure the warnings are given.  */
11942           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
11943         {
11944           t1 = optimize_bit_field_compare (code, type, arg0, arg1);
11945           if (t1)
11946             return t1;
11947         }
11948
11949       /* Optimize comparisons of strlen vs zero to a compare of the
11950          first character of the string vs zero.  To wit,
11951                 strlen(ptr) == 0   =>  *ptr == 0
11952                 strlen(ptr) != 0   =>  *ptr != 0
11953          Other cases should reduce to one of these two (or a constant)
11954          due to the return value of strlen being unsigned.  */
11955       if (TREE_CODE (arg0) == CALL_EXPR
11956           && integer_zerop (arg1))
11957         {
11958           tree fndecl = get_callee_fndecl (arg0);
11959
11960           if (fndecl
11961               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
11962               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
11963               && call_expr_nargs (arg0) == 1
11964               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
11965             {
11966               tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
11967               return fold_build2 (code, type, iref,
11968                                   build_int_cst (TREE_TYPE (iref), 0));
11969             }
11970         }
11971
11972       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
11973          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
11974       if (TREE_CODE (arg0) == RSHIFT_EXPR
11975           && integer_zerop (arg1)
11976           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11977         {
11978           tree arg00 = TREE_OPERAND (arg0, 0);
11979           tree arg01 = TREE_OPERAND (arg0, 1);
11980           tree itype = TREE_TYPE (arg00);
11981           if (TREE_INT_CST_HIGH (arg01) == 0
11982               && TREE_INT_CST_LOW (arg01)
11983                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
11984             {
11985               if (TYPE_UNSIGNED (itype))
11986                 {
11987                   itype = signed_type_for (itype);
11988                   arg00 = fold_convert (itype, arg00);
11989                 }
11990               return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
11991                                   type, arg00, build_int_cst (itype, 0));
11992             }
11993         }
11994
11995       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
11996       if (integer_zerop (arg1)
11997           && TREE_CODE (arg0) == BIT_XOR_EXPR)
11998         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11999                             TREE_OPERAND (arg0, 1));
12000
12001       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12002       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12003           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12004         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12005                             build_int_cst (TREE_TYPE (arg1), 0));
12006       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12007       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12008           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12009           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12010         return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
12011                             build_int_cst (TREE_TYPE (arg1), 0));
12012
12013       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12014       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12015           && TREE_CODE (arg1) == INTEGER_CST
12016           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12017         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12018                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
12019                                          TREE_OPERAND (arg0, 1), arg1));
12020
12021       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12022          (X & C) == 0 when C is a single bit.  */
12023       if (TREE_CODE (arg0) == BIT_AND_EXPR
12024           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12025           && integer_zerop (arg1)
12026           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12027         {
12028           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12029                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12030                              TREE_OPERAND (arg0, 1));
12031           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12032                               type, tem, arg1);
12033         }
12034
12035       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12036          constant C is a power of two, i.e. a single bit.  */
12037       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12038           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12039           && integer_zerop (arg1)
12040           && integer_pow2p (TREE_OPERAND (arg0, 1))
12041           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12042                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12043         {
12044           tree arg00 = TREE_OPERAND (arg0, 0);
12045           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12046                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12047         }
12048
12049       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12050          when is C is a power of two, i.e. a single bit.  */
12051       if (TREE_CODE (arg0) == BIT_AND_EXPR
12052           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12053           && integer_zerop (arg1)
12054           && integer_pow2p (TREE_OPERAND (arg0, 1))
12055           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12056                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12057         {
12058           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12059           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12060                              arg000, TREE_OPERAND (arg0, 1));
12061           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12062                               tem, build_int_cst (TREE_TYPE (tem), 0));
12063         }
12064
12065       if (integer_zerop (arg1)
12066           && tree_expr_nonzero_p (arg0))
12067         {
12068           tree res = constant_boolean_node (code==NE_EXPR, type);
12069           return omit_one_operand (type, res, arg0);
12070         }
12071
12072       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12073       if (TREE_CODE (arg0) == NEGATE_EXPR
12074           && TREE_CODE (arg1) == NEGATE_EXPR)
12075         return fold_build2 (code, type,
12076                             TREE_OPERAND (arg0, 0),
12077                             TREE_OPERAND (arg1, 0));
12078
12079       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12080       if (TREE_CODE (arg0) == BIT_AND_EXPR
12081           && TREE_CODE (arg1) == BIT_AND_EXPR)
12082         {
12083           tree arg00 = TREE_OPERAND (arg0, 0);
12084           tree arg01 = TREE_OPERAND (arg0, 1);
12085           tree arg10 = TREE_OPERAND (arg1, 0);
12086           tree arg11 = TREE_OPERAND (arg1, 1);
12087           tree itype = TREE_TYPE (arg0);
12088
12089           if (operand_equal_p (arg01, arg11, 0))
12090             return fold_build2 (code, type,
12091                                 fold_build2 (BIT_AND_EXPR, itype,
12092                                              fold_build2 (BIT_XOR_EXPR, itype,
12093                                                           arg00, arg10),
12094                                              arg01),
12095                                 build_int_cst (itype, 0));
12096
12097           if (operand_equal_p (arg01, arg10, 0))
12098             return fold_build2 (code, type,
12099                                 fold_build2 (BIT_AND_EXPR, itype,
12100                                              fold_build2 (BIT_XOR_EXPR, itype,
12101                                                           arg00, arg11),
12102                                              arg01),
12103                                 build_int_cst (itype, 0));
12104
12105           if (operand_equal_p (arg00, arg11, 0))
12106             return fold_build2 (code, type,
12107                                 fold_build2 (BIT_AND_EXPR, itype,
12108                                              fold_build2 (BIT_XOR_EXPR, itype,
12109                                                           arg01, arg10),
12110                                              arg00),
12111                                 build_int_cst (itype, 0));
12112
12113           if (operand_equal_p (arg00, arg10, 0))
12114             return fold_build2 (code, type,
12115                                 fold_build2 (BIT_AND_EXPR, itype,
12116                                              fold_build2 (BIT_XOR_EXPR, itype,
12117                                                           arg01, arg11),
12118                                              arg00),
12119                                 build_int_cst (itype, 0));
12120         }
12121
12122       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12123           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12124         {
12125           tree arg00 = TREE_OPERAND (arg0, 0);
12126           tree arg01 = TREE_OPERAND (arg0, 1);
12127           tree arg10 = TREE_OPERAND (arg1, 0);
12128           tree arg11 = TREE_OPERAND (arg1, 1);
12129           tree itype = TREE_TYPE (arg0);
12130
12131           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12132              operand_equal_p guarantees no side-effects so we don't need
12133              to use omit_one_operand on Z.  */
12134           if (operand_equal_p (arg01, arg11, 0))
12135             return fold_build2 (code, type, arg00, arg10);
12136           if (operand_equal_p (arg01, arg10, 0))
12137             return fold_build2 (code, type, arg00, arg11);
12138           if (operand_equal_p (arg00, arg11, 0))
12139             return fold_build2 (code, type, arg01, arg10);
12140           if (operand_equal_p (arg00, arg10, 0))
12141             return fold_build2 (code, type, arg01, arg11);
12142
12143           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12144           if (TREE_CODE (arg01) == INTEGER_CST
12145               && TREE_CODE (arg11) == INTEGER_CST)
12146             return fold_build2 (code, type,
12147                                 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12148                                              fold_build2 (BIT_XOR_EXPR, itype,
12149                                                           arg01, arg11)),
12150                                 arg10);
12151         }
12152
12153       /* Attempt to simplify equality/inequality comparisons of complex
12154          values.  Only lower the comparison if the result is known or
12155          can be simplified to a single scalar comparison.  */
12156       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12157            || TREE_CODE (arg0) == COMPLEX_CST)
12158           && (TREE_CODE (arg1) == COMPLEX_EXPR
12159               || TREE_CODE (arg1) == COMPLEX_CST))
12160         {
12161           tree real0, imag0, real1, imag1;
12162           tree rcond, icond;
12163
12164           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12165             {
12166               real0 = TREE_OPERAND (arg0, 0);
12167               imag0 = TREE_OPERAND (arg0, 1);
12168             }
12169           else
12170             {
12171               real0 = TREE_REALPART (arg0);
12172               imag0 = TREE_IMAGPART (arg0);
12173             }
12174
12175           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12176             {
12177               real1 = TREE_OPERAND (arg1, 0);
12178               imag1 = TREE_OPERAND (arg1, 1);
12179             }
12180           else
12181             {
12182               real1 = TREE_REALPART (arg1);
12183               imag1 = TREE_IMAGPART (arg1);
12184             }
12185
12186           rcond = fold_binary (code, type, real0, real1);
12187           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12188             {
12189               if (integer_zerop (rcond))
12190                 {
12191                   if (code == EQ_EXPR)
12192                     return omit_two_operands (type, boolean_false_node,
12193                                               imag0, imag1);
12194                   return fold_build2 (NE_EXPR, type, imag0, imag1);
12195                 }
12196               else
12197                 {
12198                   if (code == NE_EXPR)
12199                     return omit_two_operands (type, boolean_true_node,
12200                                               imag0, imag1);
12201                   return fold_build2 (EQ_EXPR, type, imag0, imag1);
12202                 }
12203             }
12204
12205           icond = fold_binary (code, type, imag0, imag1);
12206           if (icond && TREE_CODE (icond) == INTEGER_CST)
12207             {
12208               if (integer_zerop (icond))
12209                 {
12210                   if (code == EQ_EXPR)
12211                     return omit_two_operands (type, boolean_false_node,
12212                                               real0, real1);
12213                   return fold_build2 (NE_EXPR, type, real0, real1);
12214                 }
12215               else
12216                 {
12217                   if (code == NE_EXPR)
12218                     return omit_two_operands (type, boolean_true_node,
12219                                               real0, real1);
12220                   return fold_build2 (EQ_EXPR, type, real0, real1);
12221                 }
12222             }
12223         }
12224
12225       return NULL_TREE;
12226
12227     case LT_EXPR:
12228     case GT_EXPR:
12229     case LE_EXPR:
12230     case GE_EXPR:
12231       tem = fold_comparison (code, type, op0, op1);
12232       if (tem != NULL_TREE)
12233         return tem;
12234
12235       /* Transform comparisons of the form X +- C CMP X.  */
12236       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12237           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12238           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12239                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12240               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12241                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12242         {
12243           tree arg01 = TREE_OPERAND (arg0, 1);
12244           enum tree_code code0 = TREE_CODE (arg0);
12245           int is_positive;
12246
12247           if (TREE_CODE (arg01) == REAL_CST)
12248             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12249           else
12250             is_positive = tree_int_cst_sgn (arg01);
12251
12252           /* (X - c) > X becomes false.  */
12253           if (code == GT_EXPR
12254               && ((code0 == MINUS_EXPR && is_positive >= 0)
12255                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12256             {
12257               if (TREE_CODE (arg01) == INTEGER_CST
12258                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12259                 fold_overflow_warning (("assuming signed overflow does not "
12260                                         "occur when assuming that (X - c) > X "
12261                                         "is always false"),
12262                                        WARN_STRICT_OVERFLOW_ALL);
12263               return constant_boolean_node (0, type);
12264             }
12265
12266           /* Likewise (X + c) < X becomes false.  */
12267           if (code == LT_EXPR
12268               && ((code0 == PLUS_EXPR && is_positive >= 0)
12269                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12270             {
12271               if (TREE_CODE (arg01) == INTEGER_CST
12272                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12273                 fold_overflow_warning (("assuming signed overflow does not "
12274                                         "occur when assuming that "
12275                                         "(X + c) < X is always false"),
12276                                        WARN_STRICT_OVERFLOW_ALL);
12277               return constant_boolean_node (0, type);
12278             }
12279
12280           /* Convert (X - c) <= X to true.  */
12281           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12282               && code == LE_EXPR
12283               && ((code0 == MINUS_EXPR && is_positive >= 0)
12284                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12285             {
12286               if (TREE_CODE (arg01) == INTEGER_CST
12287                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12288                 fold_overflow_warning (("assuming signed overflow does not "
12289                                         "occur when assuming that "
12290                                         "(X - c) <= X is always true"),
12291                                        WARN_STRICT_OVERFLOW_ALL);
12292               return constant_boolean_node (1, type);
12293             }
12294
12295           /* Convert (X + c) >= X to true.  */
12296           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12297               && code == GE_EXPR
12298               && ((code0 == PLUS_EXPR && is_positive >= 0)
12299                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12300             {
12301               if (TREE_CODE (arg01) == INTEGER_CST
12302                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12303                 fold_overflow_warning (("assuming signed overflow does not "
12304                                         "occur when assuming that "
12305                                         "(X + c) >= X is always true"),
12306                                        WARN_STRICT_OVERFLOW_ALL);
12307               return constant_boolean_node (1, type);
12308             }
12309
12310           if (TREE_CODE (arg01) == INTEGER_CST)
12311             {
12312               /* Convert X + c > X and X - c < X to true for integers.  */
12313               if (code == GT_EXPR
12314                   && ((code0 == PLUS_EXPR && is_positive > 0)
12315                       || (code0 == MINUS_EXPR && is_positive < 0)))
12316                 {
12317                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12318                     fold_overflow_warning (("assuming signed overflow does "
12319                                             "not occur when assuming that "
12320                                             "(X + c) > X is always true"),
12321                                            WARN_STRICT_OVERFLOW_ALL);
12322                   return constant_boolean_node (1, type);
12323                 }
12324
12325               if (code == LT_EXPR
12326                   && ((code0 == MINUS_EXPR && is_positive > 0)
12327                       || (code0 == PLUS_EXPR && is_positive < 0)))
12328                 {
12329                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12330                     fold_overflow_warning (("assuming signed overflow does "
12331                                             "not occur when assuming that "
12332                                             "(X - c) < X is always true"),
12333                                            WARN_STRICT_OVERFLOW_ALL);
12334                   return constant_boolean_node (1, type);
12335                 }
12336
12337               /* Convert X + c <= X and X - c >= X to false for integers.  */
12338               if (code == LE_EXPR
12339                   && ((code0 == PLUS_EXPR && is_positive > 0)
12340                       || (code0 == MINUS_EXPR && is_positive < 0)))
12341                 {
12342                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12343                     fold_overflow_warning (("assuming signed overflow does "
12344                                             "not occur when assuming that "
12345                                             "(X + c) <= X is always false"),
12346                                            WARN_STRICT_OVERFLOW_ALL);
12347                   return constant_boolean_node (0, type);
12348                 }
12349
12350               if (code == GE_EXPR
12351                   && ((code0 == MINUS_EXPR && is_positive > 0)
12352                       || (code0 == PLUS_EXPR && is_positive < 0)))
12353                 {
12354                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12355                     fold_overflow_warning (("assuming signed overflow does "
12356                                             "not occur when assuming that "
12357                                             "(X - c) >= X is always false"),
12358                                            WARN_STRICT_OVERFLOW_ALL);
12359                   return constant_boolean_node (0, type);
12360                 }
12361             }
12362         }
12363
12364       /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
12365          This transformation affects the cases which are handled in later
12366          optimizations involving comparisons with non-negative constants.  */
12367       if (TREE_CODE (arg1) == INTEGER_CST
12368           && TREE_CODE (arg0) != INTEGER_CST
12369           && tree_int_cst_sgn (arg1) > 0)
12370         {
12371           if (code == GE_EXPR)
12372             {
12373               arg1 = const_binop (MINUS_EXPR, arg1,
12374                                   build_int_cst (TREE_TYPE (arg1), 1), 0);
12375               return fold_build2 (GT_EXPR, type, arg0,
12376                                   fold_convert (TREE_TYPE (arg0), arg1));
12377             }
12378           if (code == LT_EXPR)
12379             {
12380               arg1 = const_binop (MINUS_EXPR, arg1,
12381                                   build_int_cst (TREE_TYPE (arg1), 1), 0);
12382               return fold_build2 (LE_EXPR, type, arg0,
12383                                   fold_convert (TREE_TYPE (arg0), arg1));
12384             }
12385         }
12386
12387       /* Comparisons with the highest or lowest possible integer of
12388          the specified precision will have known values.  */
12389       {
12390         tree arg1_type = TREE_TYPE (arg1);
12391         unsigned int width = TYPE_PRECISION (arg1_type);
12392
12393         if (TREE_CODE (arg1) == INTEGER_CST
12394             && !TREE_OVERFLOW (arg1)
12395             && width <= 2 * HOST_BITS_PER_WIDE_INT
12396             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12397           {
12398             HOST_WIDE_INT signed_max_hi;
12399             unsigned HOST_WIDE_INT signed_max_lo;
12400             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12401
12402             if (width <= HOST_BITS_PER_WIDE_INT)
12403               {
12404                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12405                                 - 1;
12406                 signed_max_hi = 0;
12407                 max_hi = 0;
12408
12409                 if (TYPE_UNSIGNED (arg1_type))
12410                   {
12411                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12412                     min_lo = 0;
12413                     min_hi = 0;
12414                   }
12415                 else
12416                   {
12417                     max_lo = signed_max_lo;
12418                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12419                     min_hi = -1;
12420                   }
12421               }
12422             else
12423               {
12424                 width -= HOST_BITS_PER_WIDE_INT;
12425                 signed_max_lo = -1;
12426                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12427                                 - 1;
12428                 max_lo = -1;
12429                 min_lo = 0;
12430
12431                 if (TYPE_UNSIGNED (arg1_type))
12432                   {
12433                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12434                     min_hi = 0;
12435                   }
12436                 else
12437                   {
12438                     max_hi = signed_max_hi;
12439                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12440                   }
12441               }
12442
12443             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12444                 && TREE_INT_CST_LOW (arg1) == max_lo)
12445               switch (code)
12446                 {
12447                 case GT_EXPR:
12448                   return omit_one_operand (type, integer_zero_node, arg0);
12449
12450                 case GE_EXPR:
12451                   return fold_build2 (EQ_EXPR, type, op0, op1);
12452
12453                 case LE_EXPR:
12454                   return omit_one_operand (type, integer_one_node, arg0);
12455
12456                 case LT_EXPR:
12457                   return fold_build2 (NE_EXPR, type, op0, op1);
12458
12459                 /* The GE_EXPR and LT_EXPR cases above are not normally
12460                    reached because of previous transformations.  */
12461
12462                 default:
12463                   break;
12464                 }
12465             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12466                      == max_hi
12467                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12468               switch (code)
12469                 {
12470                 case GT_EXPR:
12471                   arg1 = const_binop (PLUS_EXPR, arg1,
12472                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12473                   return fold_build2 (EQ_EXPR, type,
12474                                       fold_convert (TREE_TYPE (arg1), arg0),
12475                                       arg1);
12476                 case LE_EXPR:
12477                   arg1 = const_binop (PLUS_EXPR, arg1,
12478                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12479                   return fold_build2 (NE_EXPR, type,
12480                                       fold_convert (TREE_TYPE (arg1), arg0),
12481                                       arg1);
12482                 default:
12483                   break;
12484                 }
12485             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12486                      == min_hi
12487                      && TREE_INT_CST_LOW (arg1) == min_lo)
12488               switch (code)
12489                 {
12490                 case LT_EXPR:
12491                   return omit_one_operand (type, integer_zero_node, arg0);
12492
12493                 case LE_EXPR:
12494                   return fold_build2 (EQ_EXPR, type, op0, op1);
12495
12496                 case GE_EXPR:
12497                   return omit_one_operand (type, integer_one_node, arg0);
12498
12499                 case GT_EXPR:
12500                   return fold_build2 (NE_EXPR, type, op0, op1);
12501
12502                 default:
12503                   break;
12504                 }
12505             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12506                      == min_hi
12507                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12508               switch (code)
12509                 {
12510                 case GE_EXPR:
12511                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12512                   return fold_build2 (NE_EXPR, type,
12513                                       fold_convert (TREE_TYPE (arg1), arg0),
12514                                       arg1);
12515                 case LT_EXPR:
12516                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12517                   return fold_build2 (EQ_EXPR, type,
12518                                       fold_convert (TREE_TYPE (arg1), arg0),
12519                                       arg1);
12520                 default:
12521                   break;
12522                 }
12523
12524             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12525                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
12526                      && TYPE_UNSIGNED (arg1_type)
12527                      /* We will flip the signedness of the comparison operator
12528                         associated with the mode of arg1, so the sign bit is
12529                         specified by this mode.  Check that arg1 is the signed
12530                         max associated with this sign bit.  */
12531                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12532                      /* signed_type does not work on pointer types.  */
12533                      && INTEGRAL_TYPE_P (arg1_type))
12534               {
12535                 /* The following case also applies to X < signed_max+1
12536                    and X >= signed_max+1 because previous transformations.  */
12537                 if (code == LE_EXPR || code == GT_EXPR)
12538                   {
12539                     tree st;
12540                     st = signed_type_for (TREE_TYPE (arg1));
12541                     return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
12542                                         type, fold_convert (st, arg0),
12543                                         build_int_cst (st, 0));
12544                   }
12545               }
12546           }
12547       }
12548
12549       /* If we are comparing an ABS_EXPR with a constant, we can
12550          convert all the cases into explicit comparisons, but they may
12551          well not be faster than doing the ABS and one comparison.
12552          But ABS (X) <= C is a range comparison, which becomes a subtraction
12553          and a comparison, and is probably faster.  */
12554       if (code == LE_EXPR
12555           && TREE_CODE (arg1) == INTEGER_CST
12556           && TREE_CODE (arg0) == ABS_EXPR
12557           && ! TREE_SIDE_EFFECTS (arg0)
12558           && (0 != (tem = negate_expr (arg1)))
12559           && TREE_CODE (tem) == INTEGER_CST
12560           && !TREE_OVERFLOW (tem))
12561         return fold_build2 (TRUTH_ANDIF_EXPR, type,
12562                             build2 (GE_EXPR, type,
12563                                     TREE_OPERAND (arg0, 0), tem),
12564                             build2 (LE_EXPR, type,
12565                                     TREE_OPERAND (arg0, 0), arg1));
12566
12567       /* Convert ABS_EXPR<x> >= 0 to true.  */
12568       strict_overflow_p = false;
12569       if (code == GE_EXPR
12570           && (integer_zerop (arg1)
12571               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
12572                   && real_zerop (arg1)))
12573           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12574         {
12575           if (strict_overflow_p)
12576             fold_overflow_warning (("assuming signed overflow does not occur "
12577                                     "when simplifying comparison of "
12578                                     "absolute value and zero"),
12579                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
12580           return omit_one_operand (type, integer_one_node, arg0);
12581         }
12582
12583       /* Convert ABS_EXPR<x> < 0 to false.  */
12584       strict_overflow_p = false;
12585       if (code == LT_EXPR
12586           && (integer_zerop (arg1) || real_zerop (arg1))
12587           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12588         {
12589           if (strict_overflow_p)
12590             fold_overflow_warning (("assuming signed overflow does not occur "
12591                                     "when simplifying comparison of "
12592                                     "absolute value and zero"),
12593                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
12594           return omit_one_operand (type, integer_zero_node, arg0);
12595         }
12596
12597       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12598          and similarly for >= into !=.  */
12599       if ((code == LT_EXPR || code == GE_EXPR)
12600           && TYPE_UNSIGNED (TREE_TYPE (arg0))
12601           && TREE_CODE (arg1) == LSHIFT_EXPR
12602           && integer_onep (TREE_OPERAND (arg1, 0)))
12603         return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12604                        build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12605                                TREE_OPERAND (arg1, 1)),
12606                        build_int_cst (TREE_TYPE (arg0), 0));
12607
12608       if ((code == LT_EXPR || code == GE_EXPR)
12609           && TYPE_UNSIGNED (TREE_TYPE (arg0))
12610           && (TREE_CODE (arg1) == NOP_EXPR
12611               || TREE_CODE (arg1) == CONVERT_EXPR)
12612           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12613           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12614         return
12615           build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12616                   fold_convert (TREE_TYPE (arg0),
12617                                 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12618                                         TREE_OPERAND (TREE_OPERAND (arg1, 0),
12619                                                       1))),
12620                   build_int_cst (TREE_TYPE (arg0), 0));
12621
12622       return NULL_TREE;
12623
12624     case UNORDERED_EXPR:
12625     case ORDERED_EXPR:
12626     case UNLT_EXPR:
12627     case UNLE_EXPR:
12628     case UNGT_EXPR:
12629     case UNGE_EXPR:
12630     case UNEQ_EXPR:
12631     case LTGT_EXPR:
12632       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
12633         {
12634           t1 = fold_relational_const (code, type, arg0, arg1);
12635           if (t1 != NULL_TREE)
12636             return t1;
12637         }
12638
12639       /* If the first operand is NaN, the result is constant.  */
12640       if (TREE_CODE (arg0) == REAL_CST
12641           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
12642           && (code != LTGT_EXPR || ! flag_trapping_math))
12643         {
12644           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12645                ? integer_zero_node
12646                : integer_one_node;
12647           return omit_one_operand (type, t1, arg1);
12648         }
12649
12650       /* If the second operand is NaN, the result is constant.  */
12651       if (TREE_CODE (arg1) == REAL_CST
12652           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
12653           && (code != LTGT_EXPR || ! flag_trapping_math))
12654         {
12655           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12656                ? integer_zero_node
12657                : integer_one_node;
12658           return omit_one_operand (type, t1, arg0);
12659         }
12660
12661       /* Simplify unordered comparison of something with itself.  */
12662       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
12663           && operand_equal_p (arg0, arg1, 0))
12664         return constant_boolean_node (1, type);
12665
12666       if (code == LTGT_EXPR
12667           && !flag_trapping_math
12668           && operand_equal_p (arg0, arg1, 0))
12669         return constant_boolean_node (0, type);
12670
12671       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
12672       {
12673         tree targ0 = strip_float_extensions (arg0);
12674         tree targ1 = strip_float_extensions (arg1);
12675         tree newtype = TREE_TYPE (targ0);
12676
12677         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
12678           newtype = TREE_TYPE (targ1);
12679
12680         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
12681           return fold_build2 (code, type, fold_convert (newtype, targ0),
12682                               fold_convert (newtype, targ1));
12683       }
12684
12685       return NULL_TREE;
12686
12687     case COMPOUND_EXPR:
12688       /* When pedantic, a compound expression can be neither an lvalue
12689          nor an integer constant expression.  */
12690       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12691         return NULL_TREE;
12692       /* Don't let (0, 0) be null pointer constant.  */
12693       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
12694                                  : fold_convert (type, arg1);
12695       return pedantic_non_lvalue (tem);
12696
12697     case COMPLEX_EXPR:
12698       if ((TREE_CODE (arg0) == REAL_CST
12699            && TREE_CODE (arg1) == REAL_CST)
12700           || (TREE_CODE (arg0) == INTEGER_CST
12701               && TREE_CODE (arg1) == INTEGER_CST))
12702         return build_complex (type, arg0, arg1);
12703       return NULL_TREE;
12704
12705     case ASSERT_EXPR:
12706       /* An ASSERT_EXPR should never be passed to fold_binary.  */
12707       gcc_unreachable ();
12708
12709     default:
12710       return NULL_TREE;
12711     } /* switch (code) */
12712 }
12713
12714 /* Callback for walk_tree, looking for LABEL_EXPR.
12715    Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
12716    Do not check the sub-tree of GOTO_EXPR.  */
12717
12718 static tree
12719 contains_label_1 (tree *tp,
12720                   int *walk_subtrees,
12721                   void *data ATTRIBUTE_UNUSED)
12722 {
12723   switch (TREE_CODE (*tp))
12724     {
12725     case LABEL_EXPR:
12726       return *tp;
12727     case GOTO_EXPR:
12728       *walk_subtrees = 0;
12729     /* no break */
12730     default:
12731       return NULL_TREE;
12732     }
12733 }
12734
12735 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
12736    accessible from outside the sub-tree. Returns NULL_TREE if no
12737    addressable label is found.  */
12738
12739 static bool
12740 contains_label_p (tree st)
12741 {
12742   return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
12743 }
12744
12745 /* Fold a ternary expression of code CODE and type TYPE with operands
12746    OP0, OP1, and OP2.  Return the folded expression if folding is
12747    successful.  Otherwise, return NULL_TREE.  */
12748
12749 tree
12750 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
12751 {
12752   tree tem;
12753   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
12754   enum tree_code_class kind = TREE_CODE_CLASS (code);
12755
12756   gcc_assert (IS_EXPR_CODE_CLASS (kind)
12757               && TREE_CODE_LENGTH (code) == 3);
12758
12759   /* Strip any conversions that don't change the mode.  This is safe
12760      for every expression, except for a comparison expression because
12761      its signedness is derived from its operands.  So, in the latter
12762      case, only strip conversions that don't change the signedness.
12763
12764      Note that this is done as an internal manipulation within the
12765      constant folder, in order to find the simplest representation of
12766      the arguments so that their form can be studied.  In any cases,
12767      the appropriate type conversions should be put back in the tree
12768      that will get out of the constant folder.  */
12769   if (op0)
12770     {
12771       arg0 = op0;
12772       STRIP_NOPS (arg0);
12773     }
12774
12775   if (op1)
12776     {
12777       arg1 = op1;
12778       STRIP_NOPS (arg1);
12779     }
12780
12781   switch (code)
12782     {
12783     case COMPONENT_REF:
12784       if (TREE_CODE (arg0) == CONSTRUCTOR
12785           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12786         {
12787           unsigned HOST_WIDE_INT idx;
12788           tree field, value;
12789           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12790             if (field == arg1)
12791               return value;
12792         }
12793       return NULL_TREE;
12794
12795     case COND_EXPR:
12796       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12797          so all simple results must be passed through pedantic_non_lvalue.  */
12798       if (TREE_CODE (arg0) == INTEGER_CST)
12799         {
12800           tree unused_op = integer_zerop (arg0) ? op1 : op2;
12801           tem = integer_zerop (arg0) ? op2 : op1;
12802           /* Only optimize constant conditions when the selected branch
12803              has the same type as the COND_EXPR.  This avoids optimizing
12804              away "c ? x : throw", where the throw has a void type.
12805              Avoid throwing away that operand which contains label.  */
12806           if ((!TREE_SIDE_EFFECTS (unused_op)
12807                || !contains_label_p (unused_op))
12808               && (! VOID_TYPE_P (TREE_TYPE (tem))
12809                   || VOID_TYPE_P (type)))
12810             return pedantic_non_lvalue (tem);
12811           return NULL_TREE;
12812         }
12813       if (operand_equal_p (arg1, op2, 0))
12814         return pedantic_omit_one_operand (type, arg1, arg0);
12815
12816       /* If we have A op B ? A : C, we may be able to convert this to a
12817          simpler expression, depending on the operation and the values
12818          of B and C.  Signed zeros prevent all of these transformations,
12819          for reasons given above each one.
12820
12821          Also try swapping the arguments and inverting the conditional.  */
12822       if (COMPARISON_CLASS_P (arg0)
12823           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12824                                              arg1, TREE_OPERAND (arg0, 1))
12825           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
12826         {
12827           tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
12828           if (tem)
12829             return tem;
12830         }
12831
12832       if (COMPARISON_CLASS_P (arg0)
12833           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12834                                              op2,
12835                                              TREE_OPERAND (arg0, 1))
12836           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
12837         {
12838           tem = fold_truth_not_expr (arg0);
12839           if (tem && COMPARISON_CLASS_P (tem))
12840             {
12841               tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
12842               if (tem)
12843                 return tem;
12844             }
12845         }
12846
12847       /* If the second operand is simpler than the third, swap them
12848          since that produces better jump optimization results.  */
12849       if (truth_value_p (TREE_CODE (arg0))
12850           && tree_swap_operands_p (op1, op2, false))
12851         {
12852           /* See if this can be inverted.  If it can't, possibly because
12853              it was a floating-point inequality comparison, don't do
12854              anything.  */
12855           tem = fold_truth_not_expr (arg0);
12856           if (tem)
12857             return fold_build3 (code, type, tem, op2, op1);
12858         }
12859
12860       /* Convert A ? 1 : 0 to simply A.  */
12861       if (integer_onep (op1)
12862           && integer_zerop (op2)
12863           /* If we try to convert OP0 to our type, the
12864              call to fold will try to move the conversion inside
12865              a COND, which will recurse.  In that case, the COND_EXPR
12866              is probably the best choice, so leave it alone.  */
12867           && type == TREE_TYPE (arg0))
12868         return pedantic_non_lvalue (arg0);
12869
12870       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
12871          over COND_EXPR in cases such as floating point comparisons.  */
12872       if (integer_zerop (op1)
12873           && integer_onep (op2)
12874           && truth_value_p (TREE_CODE (arg0)))
12875         return pedantic_non_lvalue (fold_convert (type,
12876                                                   invert_truthvalue (arg0)));
12877
12878       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
12879       if (TREE_CODE (arg0) == LT_EXPR
12880           && integer_zerop (TREE_OPERAND (arg0, 1))
12881           && integer_zerop (op2)
12882           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12883         {
12884           /* sign_bit_p only checks ARG1 bits within A's precision.
12885              If <sign bit of A> has wider type than A, bits outside
12886              of A's precision in <sign bit of A> need to be checked.
12887              If they are all 0, this optimization needs to be done
12888              in unsigned A's type, if they are all 1 in signed A's type,
12889              otherwise this can't be done.  */
12890           if (TYPE_PRECISION (TREE_TYPE (tem))
12891               < TYPE_PRECISION (TREE_TYPE (arg1))
12892               && TYPE_PRECISION (TREE_TYPE (tem))
12893                  < TYPE_PRECISION (type))
12894             {
12895               unsigned HOST_WIDE_INT mask_lo;
12896               HOST_WIDE_INT mask_hi;
12897               int inner_width, outer_width;
12898               tree tem_type;
12899
12900               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12901               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12902               if (outer_width > TYPE_PRECISION (type))
12903                 outer_width = TYPE_PRECISION (type);
12904
12905               if (outer_width > HOST_BITS_PER_WIDE_INT)
12906                 {
12907                   mask_hi = ((unsigned HOST_WIDE_INT) -1
12908                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
12909                   mask_lo = -1;
12910                 }
12911               else
12912                 {
12913                   mask_hi = 0;
12914                   mask_lo = ((unsigned HOST_WIDE_INT) -1
12915                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
12916                 }
12917               if (inner_width > HOST_BITS_PER_WIDE_INT)
12918                 {
12919                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
12920                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
12921                   mask_lo = 0;
12922                 }
12923               else
12924                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
12925                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
12926
12927               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
12928                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
12929                 {
12930                   tem_type = signed_type_for (TREE_TYPE (tem));
12931                   tem = fold_convert (tem_type, tem);
12932                 }
12933               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
12934                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
12935                 {
12936                   tem_type = unsigned_type_for (TREE_TYPE (tem));
12937                   tem = fold_convert (tem_type, tem);
12938                 }
12939               else
12940                 tem = NULL;
12941             }
12942
12943           if (tem)
12944             return fold_convert (type,
12945                                  fold_build2 (BIT_AND_EXPR,
12946                                               TREE_TYPE (tem), tem,
12947                                               fold_convert (TREE_TYPE (tem),
12948                                                             arg1)));
12949         }
12950
12951       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
12952          already handled above.  */
12953       if (TREE_CODE (arg0) == BIT_AND_EXPR
12954           && integer_onep (TREE_OPERAND (arg0, 1))
12955           && integer_zerop (op2)
12956           && integer_pow2p (arg1))
12957         {
12958           tree tem = TREE_OPERAND (arg0, 0);
12959           STRIP_NOPS (tem);
12960           if (TREE_CODE (tem) == RSHIFT_EXPR
12961               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
12962               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
12963                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
12964             return fold_build2 (BIT_AND_EXPR, type,
12965                                 TREE_OPERAND (tem, 0), arg1);
12966         }
12967
12968       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
12969          is probably obsolete because the first operand should be a
12970          truth value (that's why we have the two cases above), but let's
12971          leave it in until we can confirm this for all front-ends.  */
12972       if (integer_zerop (op2)
12973           && TREE_CODE (arg0) == NE_EXPR
12974           && integer_zerop (TREE_OPERAND (arg0, 1))
12975           && integer_pow2p (arg1)
12976           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12977           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12978                               arg1, OEP_ONLY_CONST))
12979         return pedantic_non_lvalue (fold_convert (type,
12980                                                   TREE_OPERAND (arg0, 0)));
12981
12982       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
12983       if (integer_zerop (op2)
12984           && truth_value_p (TREE_CODE (arg0))
12985           && truth_value_p (TREE_CODE (arg1)))
12986         return fold_build2 (TRUTH_ANDIF_EXPR, type,
12987                             fold_convert (type, arg0),
12988                             arg1);
12989
12990       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
12991       if (integer_onep (op2)
12992           && truth_value_p (TREE_CODE (arg0))
12993           && truth_value_p (TREE_CODE (arg1)))
12994         {
12995           /* Only perform transformation if ARG0 is easily inverted.  */
12996           tem = fold_truth_not_expr (arg0);
12997           if (tem)
12998             return fold_build2 (TRUTH_ORIF_EXPR, type,
12999                                 fold_convert (type, tem),
13000                                 arg1);
13001         }
13002
13003       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13004       if (integer_zerop (arg1)
13005           && truth_value_p (TREE_CODE (arg0))
13006           && truth_value_p (TREE_CODE (op2)))
13007         {
13008           /* Only perform transformation if ARG0 is easily inverted.  */
13009           tem = fold_truth_not_expr (arg0);
13010           if (tem)
13011             return fold_build2 (TRUTH_ANDIF_EXPR, type,
13012                                 fold_convert (type, tem),
13013                                 op2);
13014         }
13015
13016       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13017       if (integer_onep (arg1)
13018           && truth_value_p (TREE_CODE (arg0))
13019           && truth_value_p (TREE_CODE (op2)))
13020         return fold_build2 (TRUTH_ORIF_EXPR, type,
13021                             fold_convert (type, arg0),
13022                             op2);
13023
13024       return NULL_TREE;
13025
13026     case CALL_EXPR:
13027       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13028          of fold_ternary on them.  */
13029       gcc_unreachable ();
13030
13031     case BIT_FIELD_REF:
13032       if ((TREE_CODE (arg0) == VECTOR_CST
13033            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13034           && type == TREE_TYPE (TREE_TYPE (arg0))
13035           && host_integerp (arg1, 1)
13036           && host_integerp (op2, 1))
13037         {
13038           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13039           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13040
13041           if (width != 0
13042               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13043               && (idx % width) == 0
13044               && (idx = idx / width)
13045                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13046             {
13047               tree elements = NULL_TREE;
13048
13049               if (TREE_CODE (arg0) == VECTOR_CST)
13050                 elements = TREE_VECTOR_CST_ELTS (arg0);
13051               else
13052                 {
13053                   unsigned HOST_WIDE_INT idx;
13054                   tree value;
13055
13056                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13057                     elements = tree_cons (NULL_TREE, value, elements);
13058                 }
13059               while (idx-- > 0 && elements)
13060                 elements = TREE_CHAIN (elements);
13061               if (elements)
13062                 return TREE_VALUE (elements);
13063               else
13064                 return fold_convert (type, integer_zero_node);
13065             }
13066         }
13067       return NULL_TREE;
13068
13069     default:
13070       return NULL_TREE;
13071     } /* switch (code) */
13072 }
13073
13074 /* Perform constant folding and related simplification of EXPR.
13075    The related simplifications include x*1 => x, x*0 => 0, etc.,
13076    and application of the associative law.
13077    NOP_EXPR conversions may be removed freely (as long as we
13078    are careful not to change the type of the overall expression).
13079    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13080    but we can constant-fold them if they have constant operands.  */
13081
13082 #ifdef ENABLE_FOLD_CHECKING
13083 # define fold(x) fold_1 (x)
13084 static tree fold_1 (tree);
13085 static
13086 #endif
13087 tree
13088 fold (tree expr)
13089 {
13090   const tree t = expr;
13091   enum tree_code code = TREE_CODE (t);
13092   enum tree_code_class kind = TREE_CODE_CLASS (code);
13093   tree tem;
13094
13095   /* Return right away if a constant.  */
13096   if (kind == tcc_constant)
13097     return t;
13098
13099   /* CALL_EXPR-like objects with variable numbers of operands are
13100      treated specially.  */
13101   if (kind == tcc_vl_exp)
13102     {
13103       if (code == CALL_EXPR)
13104         {
13105           tem = fold_call_expr (expr, false);
13106           return tem ? tem : expr;
13107         }
13108       return expr;
13109     }
13110
13111   if (IS_EXPR_CODE_CLASS (kind)
13112       || IS_GIMPLE_STMT_CODE_CLASS (kind))
13113     {
13114       tree type = TREE_TYPE (t);
13115       tree op0, op1, op2;
13116
13117       switch (TREE_CODE_LENGTH (code))
13118         {
13119         case 1:
13120           op0 = TREE_OPERAND (t, 0);
13121           tem = fold_unary (code, type, op0);
13122           return tem ? tem : expr;
13123         case 2:
13124           op0 = TREE_OPERAND (t, 0);
13125           op1 = TREE_OPERAND (t, 1);
13126           tem = fold_binary (code, type, op0, op1);
13127           return tem ? tem : expr;
13128         case 3:
13129           op0 = TREE_OPERAND (t, 0);
13130           op1 = TREE_OPERAND (t, 1);
13131           op2 = TREE_OPERAND (t, 2);
13132           tem = fold_ternary (code, type, op0, op1, op2);
13133           return tem ? tem : expr;
13134         default:
13135           break;
13136         }
13137     }
13138
13139   switch (code)
13140     {
13141     case CONST_DECL:
13142       return fold (DECL_INITIAL (t));
13143
13144     default:
13145       return t;
13146     } /* switch (code) */
13147 }
13148
13149 #ifdef ENABLE_FOLD_CHECKING
13150 #undef fold
13151
13152 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13153 static void fold_check_failed (const_tree, const_tree);
13154 void print_fold_checksum (const_tree);
13155
13156 /* When --enable-checking=fold, compute a digest of expr before
13157    and after actual fold call to see if fold did not accidentally
13158    change original expr.  */
13159
13160 tree
13161 fold (tree expr)
13162 {
13163   tree ret;
13164   struct md5_ctx ctx;
13165   unsigned char checksum_before[16], checksum_after[16];
13166   htab_t ht;
13167
13168   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13169   md5_init_ctx (&ctx);
13170   fold_checksum_tree (expr, &ctx, ht);
13171   md5_finish_ctx (&ctx, checksum_before);
13172   htab_empty (ht);
13173
13174   ret = fold_1 (expr);
13175
13176   md5_init_ctx (&ctx);
13177   fold_checksum_tree (expr, &ctx, ht);
13178   md5_finish_ctx (&ctx, checksum_after);
13179   htab_delete (ht);
13180
13181   if (memcmp (checksum_before, checksum_after, 16))
13182     fold_check_failed (expr, ret);
13183
13184   return ret;
13185 }
13186
13187 void
13188 print_fold_checksum (const_tree expr)
13189 {
13190   struct md5_ctx ctx;
13191   unsigned char checksum[16], cnt;
13192   htab_t ht;
13193
13194   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13195   md5_init_ctx (&ctx);
13196   fold_checksum_tree (expr, &ctx, ht);
13197   md5_finish_ctx (&ctx, checksum);
13198   htab_delete (ht);
13199   for (cnt = 0; cnt < 16; ++cnt)
13200     fprintf (stderr, "%02x", checksum[cnt]);
13201   putc ('\n', stderr);
13202 }
13203
13204 static void
13205 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13206 {
13207   internal_error ("fold check: original tree changed by fold");
13208 }
13209
13210 static void
13211 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13212 {
13213   const void **slot;
13214   enum tree_code code;
13215   struct tree_function_decl buf;
13216   int i, len;
13217   
13218 recursive_label:
13219
13220   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13221                <= sizeof (struct tree_function_decl))
13222               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13223   if (expr == NULL)
13224     return;
13225   slot = (const void **) htab_find_slot (ht, expr, INSERT);
13226   if (*slot != NULL)
13227     return;
13228   *slot = expr;
13229   code = TREE_CODE (expr);
13230   if (TREE_CODE_CLASS (code) == tcc_declaration
13231       && DECL_ASSEMBLER_NAME_SET_P (expr))
13232     {
13233       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13234       memcpy ((char *) &buf, expr, tree_size (expr));
13235       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13236       expr = (tree) &buf;
13237     }
13238   else if (TREE_CODE_CLASS (code) == tcc_type
13239            && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)
13240                || TYPE_CACHED_VALUES_P (expr)
13241                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)))
13242     {
13243       /* Allow these fields to be modified.  */
13244       tree tmp;
13245       memcpy ((char *) &buf, expr, tree_size (expr));
13246       expr = tmp = (tree) &buf;
13247       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13248       TYPE_POINTER_TO (tmp) = NULL;
13249       TYPE_REFERENCE_TO (tmp) = NULL;
13250       if (TYPE_CACHED_VALUES_P (tmp))
13251         {
13252           TYPE_CACHED_VALUES_P (tmp) = 0;
13253           TYPE_CACHED_VALUES (tmp) = NULL;
13254         }
13255     }
13256   md5_process_bytes (expr, tree_size (expr), ctx);
13257   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13258   if (TREE_CODE_CLASS (code) != tcc_type
13259       && TREE_CODE_CLASS (code) != tcc_declaration
13260       && code != TREE_LIST
13261       && code != SSA_NAME)
13262     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13263   switch (TREE_CODE_CLASS (code))
13264     {
13265     case tcc_constant:
13266       switch (code)
13267         {
13268         case STRING_CST:
13269           md5_process_bytes (TREE_STRING_POINTER (expr),
13270                              TREE_STRING_LENGTH (expr), ctx);
13271           break;
13272         case COMPLEX_CST:
13273           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13274           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13275           break;
13276         case VECTOR_CST:
13277           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13278           break;
13279         default:
13280           break;
13281         }
13282       break;
13283     case tcc_exceptional:
13284       switch (code)
13285         {
13286         case TREE_LIST:
13287           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13288           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13289           expr = TREE_CHAIN (expr);
13290           goto recursive_label;
13291           break;
13292         case TREE_VEC:
13293           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13294             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13295           break;
13296         default:
13297           break;
13298         }
13299       break;
13300     case tcc_expression:
13301     case tcc_reference:
13302     case tcc_comparison:
13303     case tcc_unary:
13304     case tcc_binary:
13305     case tcc_statement:
13306     case tcc_vl_exp:
13307       len = TREE_OPERAND_LENGTH (expr);
13308       for (i = 0; i < len; ++i)
13309         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13310       break;
13311     case tcc_declaration:
13312       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13313       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13314       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13315         {
13316           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13317           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13318           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13319           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13320           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13321         }
13322       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13323         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13324           
13325       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13326         {
13327           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13328           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13329           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13330         }
13331       break;
13332     case tcc_type:
13333       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13334         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13335       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13336       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13337       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13338       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13339       if (INTEGRAL_TYPE_P (expr)
13340           || SCALAR_FLOAT_TYPE_P (expr))
13341         {
13342           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13343           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13344         }
13345       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13346       if (TREE_CODE (expr) == RECORD_TYPE
13347           || TREE_CODE (expr) == UNION_TYPE
13348           || TREE_CODE (expr) == QUAL_UNION_TYPE)
13349         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13350       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13351       break;
13352     default:
13353       break;
13354     }
13355 }
13356
13357 /* Helper function for outputting the checksum of a tree T.  When
13358    debugging with gdb, you can "define mynext" to be "next" followed
13359    by "call debug_fold_checksum (op0)", then just trace down till the
13360    outputs differ.  */
13361
13362 void
13363 debug_fold_checksum (const_tree t)
13364 {
13365   int i;
13366   unsigned char checksum[16];
13367   struct md5_ctx ctx;
13368   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13369   
13370   md5_init_ctx (&ctx);
13371   fold_checksum_tree (t, &ctx, ht);
13372   md5_finish_ctx (&ctx, checksum);
13373   htab_empty (ht);
13374
13375   for (i = 0; i < 16; i++)
13376     fprintf (stderr, "%d ", checksum[i]);
13377
13378   fprintf (stderr, "\n");
13379 }
13380
13381 #endif
13382
13383 /* Fold a unary tree expression with code CODE of type TYPE with an
13384    operand OP0.  Return a folded expression if successful.  Otherwise,
13385    return a tree expression with code CODE of type TYPE with an
13386    operand OP0.  */
13387
13388 tree
13389 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13390 {
13391   tree tem;
13392 #ifdef ENABLE_FOLD_CHECKING
13393   unsigned char checksum_before[16], checksum_after[16];
13394   struct md5_ctx ctx;
13395   htab_t ht;
13396
13397   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13398   md5_init_ctx (&ctx);
13399   fold_checksum_tree (op0, &ctx, ht);
13400   md5_finish_ctx (&ctx, checksum_before);
13401   htab_empty (ht);
13402 #endif
13403   
13404   tem = fold_unary (code, type, op0);
13405   if (!tem)
13406     tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13407   
13408 #ifdef ENABLE_FOLD_CHECKING
13409   md5_init_ctx (&ctx);
13410   fold_checksum_tree (op0, &ctx, ht);
13411   md5_finish_ctx (&ctx, checksum_after);
13412   htab_delete (ht);
13413
13414   if (memcmp (checksum_before, checksum_after, 16))
13415     fold_check_failed (op0, tem);
13416 #endif
13417   return tem;
13418 }
13419
13420 /* Fold a binary tree expression with code CODE of type TYPE with
13421    operands OP0 and OP1.  Return a folded expression if successful.
13422    Otherwise, return a tree expression with code CODE of type TYPE
13423    with operands OP0 and OP1.  */
13424
13425 tree
13426 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13427                   MEM_STAT_DECL)
13428 {
13429   tree tem;
13430 #ifdef ENABLE_FOLD_CHECKING
13431   unsigned char checksum_before_op0[16],
13432                 checksum_before_op1[16],
13433                 checksum_after_op0[16],
13434                 checksum_after_op1[16];
13435   struct md5_ctx ctx;
13436   htab_t ht;
13437
13438   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13439   md5_init_ctx (&ctx);
13440   fold_checksum_tree (op0, &ctx, ht);
13441   md5_finish_ctx (&ctx, checksum_before_op0);
13442   htab_empty (ht);
13443
13444   md5_init_ctx (&ctx);
13445   fold_checksum_tree (op1, &ctx, ht);
13446   md5_finish_ctx (&ctx, checksum_before_op1);
13447   htab_empty (ht);
13448 #endif
13449
13450   tem = fold_binary (code, type, op0, op1);
13451   if (!tem)
13452     tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13453   
13454 #ifdef ENABLE_FOLD_CHECKING
13455   md5_init_ctx (&ctx);
13456   fold_checksum_tree (op0, &ctx, ht);
13457   md5_finish_ctx (&ctx, checksum_after_op0);
13458   htab_empty (ht);
13459
13460   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13461     fold_check_failed (op0, tem);
13462   
13463   md5_init_ctx (&ctx);
13464   fold_checksum_tree (op1, &ctx, ht);
13465   md5_finish_ctx (&ctx, checksum_after_op1);
13466   htab_delete (ht);
13467
13468   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13469     fold_check_failed (op1, tem);
13470 #endif
13471   return tem;
13472 }
13473
13474 /* Fold a ternary tree expression with code CODE of type TYPE with
13475    operands OP0, OP1, and OP2.  Return a folded expression if
13476    successful.  Otherwise, return a tree expression with code CODE of
13477    type TYPE with operands OP0, OP1, and OP2.  */
13478
13479 tree
13480 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
13481              MEM_STAT_DECL)
13482 {
13483   tree tem;
13484 #ifdef ENABLE_FOLD_CHECKING
13485   unsigned char checksum_before_op0[16],
13486                 checksum_before_op1[16],
13487                 checksum_before_op2[16],
13488                 checksum_after_op0[16],
13489                 checksum_after_op1[16],
13490                 checksum_after_op2[16];
13491   struct md5_ctx ctx;
13492   htab_t ht;
13493
13494   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13495   md5_init_ctx (&ctx);
13496   fold_checksum_tree (op0, &ctx, ht);
13497   md5_finish_ctx (&ctx, checksum_before_op0);
13498   htab_empty (ht);
13499
13500   md5_init_ctx (&ctx);
13501   fold_checksum_tree (op1, &ctx, ht);
13502   md5_finish_ctx (&ctx, checksum_before_op1);
13503   htab_empty (ht);
13504
13505   md5_init_ctx (&ctx);
13506   fold_checksum_tree (op2, &ctx, ht);
13507   md5_finish_ctx (&ctx, checksum_before_op2);
13508   htab_empty (ht);
13509 #endif
13510
13511   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13512   tem = fold_ternary (code, type, op0, op1, op2);
13513   if (!tem)
13514     tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
13515       
13516 #ifdef ENABLE_FOLD_CHECKING
13517   md5_init_ctx (&ctx);
13518   fold_checksum_tree (op0, &ctx, ht);
13519   md5_finish_ctx (&ctx, checksum_after_op0);
13520   htab_empty (ht);
13521
13522   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13523     fold_check_failed (op0, tem);
13524   
13525   md5_init_ctx (&ctx);
13526   fold_checksum_tree (op1, &ctx, ht);
13527   md5_finish_ctx (&ctx, checksum_after_op1);
13528   htab_empty (ht);
13529
13530   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13531     fold_check_failed (op1, tem);
13532   
13533   md5_init_ctx (&ctx);
13534   fold_checksum_tree (op2, &ctx, ht);
13535   md5_finish_ctx (&ctx, checksum_after_op2);
13536   htab_delete (ht);
13537
13538   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13539     fold_check_failed (op2, tem);
13540 #endif
13541   return tem;
13542 }
13543
13544 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13545    arguments in ARGARRAY, and a null static chain.
13546    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
13547    of type TYPE from the given operands as constructed by build_call_array.  */
13548
13549 tree
13550 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
13551 {
13552   tree tem;
13553 #ifdef ENABLE_FOLD_CHECKING
13554   unsigned char checksum_before_fn[16],
13555                 checksum_before_arglist[16],
13556                 checksum_after_fn[16],
13557                 checksum_after_arglist[16];
13558   struct md5_ctx ctx;
13559   htab_t ht;
13560   int i;
13561
13562   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13563   md5_init_ctx (&ctx);
13564   fold_checksum_tree (fn, &ctx, ht);
13565   md5_finish_ctx (&ctx, checksum_before_fn);
13566   htab_empty (ht);
13567
13568   md5_init_ctx (&ctx);
13569   for (i = 0; i < nargs; i++)
13570     fold_checksum_tree (argarray[i], &ctx, ht);
13571   md5_finish_ctx (&ctx, checksum_before_arglist);
13572   htab_empty (ht);
13573 #endif
13574
13575   tem = fold_builtin_call_array (type, fn, nargs, argarray);
13576       
13577 #ifdef ENABLE_FOLD_CHECKING
13578   md5_init_ctx (&ctx);
13579   fold_checksum_tree (fn, &ctx, ht);
13580   md5_finish_ctx (&ctx, checksum_after_fn);
13581   htab_empty (ht);
13582
13583   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13584     fold_check_failed (fn, tem);
13585   
13586   md5_init_ctx (&ctx);
13587   for (i = 0; i < nargs; i++)
13588     fold_checksum_tree (argarray[i], &ctx, ht);
13589   md5_finish_ctx (&ctx, checksum_after_arglist);
13590   htab_delete (ht);
13591
13592   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13593     fold_check_failed (NULL_TREE, tem);
13594 #endif
13595   return tem;
13596 }
13597
13598 /* Perform constant folding and related simplification of initializer
13599    expression EXPR.  These behave identically to "fold_buildN" but ignore
13600    potential run-time traps and exceptions that fold must preserve.  */
13601
13602 #define START_FOLD_INIT \
13603   int saved_signaling_nans = flag_signaling_nans;\
13604   int saved_trapping_math = flag_trapping_math;\
13605   int saved_rounding_math = flag_rounding_math;\
13606   int saved_trapv = flag_trapv;\
13607   int saved_folding_initializer = folding_initializer;\
13608   flag_signaling_nans = 0;\
13609   flag_trapping_math = 0;\
13610   flag_rounding_math = 0;\
13611   flag_trapv = 0;\
13612   folding_initializer = 1;
13613
13614 #define END_FOLD_INIT \
13615   flag_signaling_nans = saved_signaling_nans;\
13616   flag_trapping_math = saved_trapping_math;\
13617   flag_rounding_math = saved_rounding_math;\
13618   flag_trapv = saved_trapv;\
13619   folding_initializer = saved_folding_initializer;
13620
13621 tree
13622 fold_build1_initializer (enum tree_code code, tree type, tree op)
13623 {
13624   tree result;
13625   START_FOLD_INIT;
13626
13627   result = fold_build1 (code, type, op);
13628
13629   END_FOLD_INIT;
13630   return result;
13631 }
13632
13633 tree
13634 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
13635 {
13636   tree result;
13637   START_FOLD_INIT;
13638
13639   result = fold_build2 (code, type, op0, op1);
13640
13641   END_FOLD_INIT;
13642   return result;
13643 }
13644
13645 tree
13646 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
13647                          tree op2)
13648 {
13649   tree result;
13650   START_FOLD_INIT;
13651
13652   result = fold_build3 (code, type, op0, op1, op2);
13653
13654   END_FOLD_INIT;
13655   return result;
13656 }
13657
13658 tree
13659 fold_build_call_array_initializer (tree type, tree fn,
13660                                    int nargs, tree *argarray)
13661 {
13662   tree result;
13663   START_FOLD_INIT;
13664
13665   result = fold_build_call_array (type, fn, nargs, argarray);
13666
13667   END_FOLD_INIT;
13668   return result;
13669 }
13670
13671 #undef START_FOLD_INIT
13672 #undef END_FOLD_INIT
13673
13674 /* Determine if first argument is a multiple of second argument.  Return 0 if
13675    it is not, or we cannot easily determined it to be.
13676
13677    An example of the sort of thing we care about (at this point; this routine
13678    could surely be made more general, and expanded to do what the *_DIV_EXPR's
13679    fold cases do now) is discovering that
13680
13681      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13682
13683    is a multiple of
13684
13685      SAVE_EXPR (J * 8)
13686
13687    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
13688
13689    This code also handles discovering that
13690
13691      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13692
13693    is a multiple of 8 so we don't have to worry about dealing with a
13694    possible remainder.
13695
13696    Note that we *look* inside a SAVE_EXPR only to determine how it was
13697    calculated; it is not safe for fold to do much of anything else with the
13698    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
13699    at run time.  For example, the latter example above *cannot* be implemented
13700    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
13701    evaluation time of the original SAVE_EXPR is not necessarily the same at
13702    the time the new expression is evaluated.  The only optimization of this
13703    sort that would be valid is changing
13704
13705      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
13706
13707    divided by 8 to
13708
13709      SAVE_EXPR (I) * SAVE_EXPR (J)
13710
13711    (where the same SAVE_EXPR (J) is used in the original and the
13712    transformed version).  */
13713
13714 int
13715 multiple_of_p (tree type, const_tree top, const_tree bottom)
13716 {
13717   if (operand_equal_p (top, bottom, 0))
13718     return 1;
13719
13720   if (TREE_CODE (type) != INTEGER_TYPE)
13721     return 0;
13722
13723   switch (TREE_CODE (top))
13724     {
13725     case BIT_AND_EXPR:
13726       /* Bitwise and provides a power of two multiple.  If the mask is
13727          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
13728       if (!integer_pow2p (bottom))
13729         return 0;
13730       /* FALLTHRU */
13731
13732     case MULT_EXPR:
13733       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13734               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13735
13736     case PLUS_EXPR:
13737     case MINUS_EXPR:
13738       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13739               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13740
13741     case LSHIFT_EXPR:
13742       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
13743         {
13744           tree op1, t1;
13745
13746           op1 = TREE_OPERAND (top, 1);
13747           /* const_binop may not detect overflow correctly,
13748              so check for it explicitly here.  */
13749           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
13750               > TREE_INT_CST_LOW (op1)
13751               && TREE_INT_CST_HIGH (op1) == 0
13752               && 0 != (t1 = fold_convert (type,
13753                                           const_binop (LSHIFT_EXPR,
13754                                                        size_one_node,
13755                                                        op1, 0)))
13756               && !TREE_OVERFLOW (t1))
13757             return multiple_of_p (type, t1, bottom);
13758         }
13759       return 0;
13760
13761     case NOP_EXPR:
13762       /* Can't handle conversions from non-integral or wider integral type.  */
13763       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
13764           || (TYPE_PRECISION (type)
13765               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
13766         return 0;
13767
13768       /* .. fall through ...  */
13769
13770     case SAVE_EXPR:
13771       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
13772
13773     case INTEGER_CST:
13774       if (TREE_CODE (bottom) != INTEGER_CST
13775           || integer_zerop (bottom)
13776           || (TYPE_UNSIGNED (type)
13777               && (tree_int_cst_sgn (top) < 0
13778                   || tree_int_cst_sgn (bottom) < 0)))
13779         return 0;
13780       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
13781                                              top, bottom, 0));
13782
13783     default:
13784       return 0;
13785     }
13786 }
13787
13788 /* Return true if `t' is known to be non-negative.  If the return
13789    value is based on the assumption that signed overflow is undefined,
13790    set *STRICT_OVERFLOW_P to true; otherwise, don't change
13791    *STRICT_OVERFLOW_P.  */
13792
13793 bool
13794 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
13795 {
13796   if (t == error_mark_node)
13797     return false;
13798
13799   if (TYPE_UNSIGNED (TREE_TYPE (t)))
13800     return true;
13801
13802   switch (TREE_CODE (t))
13803     {
13804     case SSA_NAME:
13805       /* Query VRP to see if it has recorded any information about
13806          the range of this object.  */
13807       return ssa_name_nonnegative_p (t);
13808
13809     case ABS_EXPR:
13810       /* We can't return 1 if flag_wrapv is set because
13811          ABS_EXPR<INT_MIN> = INT_MIN.  */
13812       if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13813         return true;
13814       if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
13815         {
13816           *strict_overflow_p = true;
13817           return true;
13818         }
13819       break;
13820
13821     case INTEGER_CST:
13822       return tree_int_cst_sgn (t) >= 0;
13823
13824     case REAL_CST:
13825       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
13826
13827     case FIXED_CST:
13828       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
13829
13830     case POINTER_PLUS_EXPR:
13831     case PLUS_EXPR:
13832       if (FLOAT_TYPE_P (TREE_TYPE (t)))
13833         return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13834                                                strict_overflow_p)
13835                 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13836                                                   strict_overflow_p));
13837
13838       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
13839          both unsigned and at least 2 bits shorter than the result.  */
13840       if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
13841           && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
13842           && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
13843         {
13844           tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
13845           tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
13846           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
13847               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
13848             {
13849               unsigned int prec = MAX (TYPE_PRECISION (inner1),
13850                                        TYPE_PRECISION (inner2)) + 1;
13851               return prec < TYPE_PRECISION (TREE_TYPE (t));
13852             }
13853         }
13854       break;
13855
13856     case MULT_EXPR:
13857       if (FLOAT_TYPE_P (TREE_TYPE (t)))
13858         {
13859           /* x * x for floating point x is always non-negative.  */
13860           if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
13861             return true;
13862           return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13863                                                  strict_overflow_p)
13864                   && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13865                                                     strict_overflow_p));
13866         }
13867
13868       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
13869          both unsigned and their total bits is shorter than the result.  */
13870       if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
13871           && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
13872           && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
13873         {
13874           tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
13875           tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
13876           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
13877               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
13878             return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
13879                    < TYPE_PRECISION (TREE_TYPE (t));
13880         }
13881       return false;
13882
13883     case BIT_AND_EXPR:
13884     case MAX_EXPR:
13885       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13886                                              strict_overflow_p)
13887               || tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13888                                                 strict_overflow_p));
13889
13890     case BIT_IOR_EXPR:
13891     case BIT_XOR_EXPR:
13892     case MIN_EXPR:
13893     case RDIV_EXPR:
13894     case TRUNC_DIV_EXPR:
13895     case CEIL_DIV_EXPR:
13896     case FLOOR_DIV_EXPR:
13897     case ROUND_DIV_EXPR:
13898       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13899                                              strict_overflow_p)
13900               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13901                                                 strict_overflow_p));
13902
13903     case TRUNC_MOD_EXPR:
13904     case CEIL_MOD_EXPR:
13905     case FLOOR_MOD_EXPR:
13906     case ROUND_MOD_EXPR:
13907     case SAVE_EXPR:
13908     case NON_LVALUE_EXPR:
13909     case FLOAT_EXPR:
13910     case FIX_TRUNC_EXPR:
13911       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13912                                             strict_overflow_p);
13913
13914     case COMPOUND_EXPR:
13915     case MODIFY_EXPR:
13916     case GIMPLE_MODIFY_STMT:
13917       return tree_expr_nonnegative_warnv_p (GENERIC_TREE_OPERAND (t, 1),
13918                                             strict_overflow_p);
13919
13920     case BIND_EXPR:
13921       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
13922                                             strict_overflow_p);
13923
13924     case COND_EXPR:
13925       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13926                                              strict_overflow_p)
13927               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
13928                                                 strict_overflow_p));
13929
13930     case NOP_EXPR:
13931       {
13932         tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
13933         tree outer_type = TREE_TYPE (t);
13934
13935         if (TREE_CODE (outer_type) == REAL_TYPE)
13936           {
13937             if (TREE_CODE (inner_type) == REAL_TYPE)
13938               return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13939                                                     strict_overflow_p);
13940             if (TREE_CODE (inner_type) == INTEGER_TYPE)
13941               {
13942                 if (TYPE_UNSIGNED (inner_type))
13943                   return true;
13944                 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13945                                                       strict_overflow_p);
13946               }
13947           }
13948         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
13949           {
13950             if (TREE_CODE (inner_type) == REAL_TYPE)
13951               return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t,0),
13952                                                     strict_overflow_p);
13953             if (TREE_CODE (inner_type) == INTEGER_TYPE)
13954               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
13955                       && TYPE_UNSIGNED (inner_type);
13956           }
13957       }
13958       break;
13959
13960     case TARGET_EXPR:
13961       {
13962         tree temp = TARGET_EXPR_SLOT (t);
13963         t = TARGET_EXPR_INITIAL (t);
13964
13965         /* If the initializer is non-void, then it's a normal expression
13966            that will be assigned to the slot.  */
13967         if (!VOID_TYPE_P (t))
13968           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
13969
13970         /* Otherwise, the initializer sets the slot in some way.  One common
13971            way is an assignment statement at the end of the initializer.  */
13972         while (1)
13973           {
13974             if (TREE_CODE (t) == BIND_EXPR)
13975               t = expr_last (BIND_EXPR_BODY (t));
13976             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13977                      || TREE_CODE (t) == TRY_CATCH_EXPR)
13978               t = expr_last (TREE_OPERAND (t, 0));
13979             else if (TREE_CODE (t) == STATEMENT_LIST)
13980               t = expr_last (t);
13981             else
13982               break;
13983           }
13984         if ((TREE_CODE (t) == MODIFY_EXPR
13985              || TREE_CODE (t) == GIMPLE_MODIFY_STMT)
13986             && GENERIC_TREE_OPERAND (t, 0) == temp)
13987           return tree_expr_nonnegative_warnv_p (GENERIC_TREE_OPERAND (t, 1),
13988                                                 strict_overflow_p);
13989
13990         return false;
13991       }
13992
13993     case CALL_EXPR:
13994       {
13995         tree fndecl = get_callee_fndecl (t);
13996         if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
13997           switch (DECL_FUNCTION_CODE (fndecl))
13998             {
13999             CASE_FLT_FN (BUILT_IN_ACOS):
14000             CASE_FLT_FN (BUILT_IN_ACOSH):
14001             CASE_FLT_FN (BUILT_IN_CABS):
14002             CASE_FLT_FN (BUILT_IN_COSH):
14003             CASE_FLT_FN (BUILT_IN_ERFC):
14004             CASE_FLT_FN (BUILT_IN_EXP):
14005             CASE_FLT_FN (BUILT_IN_EXP10):
14006             CASE_FLT_FN (BUILT_IN_EXP2):
14007             CASE_FLT_FN (BUILT_IN_FABS):
14008             CASE_FLT_FN (BUILT_IN_FDIM):
14009             CASE_FLT_FN (BUILT_IN_HYPOT):
14010             CASE_FLT_FN (BUILT_IN_POW10):
14011             CASE_INT_FN (BUILT_IN_FFS):
14012             CASE_INT_FN (BUILT_IN_PARITY):
14013             CASE_INT_FN (BUILT_IN_POPCOUNT):
14014             case BUILT_IN_BSWAP32:
14015             case BUILT_IN_BSWAP64:
14016               /* Always true.  */
14017               return true;
14018
14019             CASE_FLT_FN (BUILT_IN_SQRT):
14020               /* sqrt(-0.0) is -0.0.  */
14021               if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (t))))
14022                 return true;
14023               return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14024                                                     strict_overflow_p);
14025
14026             CASE_FLT_FN (BUILT_IN_ASINH):
14027             CASE_FLT_FN (BUILT_IN_ATAN):
14028             CASE_FLT_FN (BUILT_IN_ATANH):
14029             CASE_FLT_FN (BUILT_IN_CBRT):
14030             CASE_FLT_FN (BUILT_IN_CEIL):
14031             CASE_FLT_FN (BUILT_IN_ERF):
14032             CASE_FLT_FN (BUILT_IN_EXPM1):
14033             CASE_FLT_FN (BUILT_IN_FLOOR):
14034             CASE_FLT_FN (BUILT_IN_FMOD):
14035             CASE_FLT_FN (BUILT_IN_FREXP):
14036             CASE_FLT_FN (BUILT_IN_LCEIL):
14037             CASE_FLT_FN (BUILT_IN_LDEXP):
14038             CASE_FLT_FN (BUILT_IN_LFLOOR):
14039             CASE_FLT_FN (BUILT_IN_LLCEIL):
14040             CASE_FLT_FN (BUILT_IN_LLFLOOR):
14041             CASE_FLT_FN (BUILT_IN_LLRINT):
14042             CASE_FLT_FN (BUILT_IN_LLROUND):
14043             CASE_FLT_FN (BUILT_IN_LRINT):
14044             CASE_FLT_FN (BUILT_IN_LROUND):
14045             CASE_FLT_FN (BUILT_IN_MODF):
14046             CASE_FLT_FN (BUILT_IN_NEARBYINT):
14047             CASE_FLT_FN (BUILT_IN_RINT):
14048             CASE_FLT_FN (BUILT_IN_ROUND):
14049             CASE_FLT_FN (BUILT_IN_SCALB):
14050             CASE_FLT_FN (BUILT_IN_SCALBLN):
14051             CASE_FLT_FN (BUILT_IN_SCALBN):
14052             CASE_FLT_FN (BUILT_IN_SIGNBIT):
14053             CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14054             CASE_FLT_FN (BUILT_IN_SINH):
14055             CASE_FLT_FN (BUILT_IN_TANH):
14056             CASE_FLT_FN (BUILT_IN_TRUNC):
14057               /* True if the 1st argument is nonnegative.  */
14058               return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14059                                                     strict_overflow_p);
14060
14061             CASE_FLT_FN (BUILT_IN_FMAX):
14062               /* True if the 1st OR 2nd arguments are nonnegative.  */
14063               return (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14064                                                      strict_overflow_p)
14065                       || (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 1),
14066                                                          strict_overflow_p)));
14067
14068             CASE_FLT_FN (BUILT_IN_FMIN):
14069               /* True if the 1st AND 2nd arguments are nonnegative.  */
14070               return (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14071                                                      strict_overflow_p)
14072                       && (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 1),
14073                                                          strict_overflow_p)));
14074
14075             CASE_FLT_FN (BUILT_IN_COPYSIGN):
14076               /* True if the 2nd argument is nonnegative.  */
14077               return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 1),
14078                                                     strict_overflow_p);
14079
14080             CASE_FLT_FN (BUILT_IN_POWI):
14081               /* True if the 1st argument is nonnegative or the second
14082                  argument is an even integer.  */
14083               if (TREE_CODE (CALL_EXPR_ARG (t, 1)) == INTEGER_CST)
14084                 {
14085                   tree arg1 = CALL_EXPR_ARG (t, 1);
14086                   if ((TREE_INT_CST_LOW (arg1) & 1) == 0)
14087                     return true;
14088                 }
14089               return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14090                                                     strict_overflow_p);
14091
14092             CASE_FLT_FN (BUILT_IN_POW):
14093               /* True if the 1st argument is nonnegative or the second
14094                  argument is an even integer valued real.  */
14095               if (TREE_CODE (CALL_EXPR_ARG (t, 1)) == REAL_CST)
14096                 {
14097                   REAL_VALUE_TYPE c;
14098                   HOST_WIDE_INT n;
14099
14100                   c = TREE_REAL_CST (CALL_EXPR_ARG (t, 1));
14101                   n = real_to_integer (&c);
14102                   if ((n & 1) == 0)
14103                     {
14104                       REAL_VALUE_TYPE cint;
14105                       real_from_integer (&cint, VOIDmode, n,
14106                                          n < 0 ? -1 : 0, 0);
14107                       if (real_identical (&c, &cint))
14108                         return true;
14109                     }
14110                 }
14111               return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14112                                                     strict_overflow_p);
14113
14114             default:
14115               break;
14116             }
14117       }
14118
14119       /* ... fall through ...  */
14120
14121     default:
14122       {
14123         tree type = TREE_TYPE (t);
14124         if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14125             && truth_value_p (TREE_CODE (t)))
14126           /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14127              have a signed:1 type (where the value is -1 and 0).  */
14128           return true;
14129       }
14130     }
14131
14132   /* We don't know sign of `t', so be conservative and return false.  */
14133   return false;
14134 }
14135
14136 /* Return true if `t' is known to be non-negative.  Handle warnings
14137    about undefined signed overflow.  */
14138
14139 bool
14140 tree_expr_nonnegative_p (tree t)
14141 {
14142   bool ret, strict_overflow_p;
14143
14144   strict_overflow_p = false;
14145   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14146   if (strict_overflow_p)
14147     fold_overflow_warning (("assuming signed overflow does not occur when "
14148                             "determining that expression is always "
14149                             "non-negative"),
14150                            WARN_STRICT_OVERFLOW_MISC);
14151   return ret;
14152 }
14153
14154 /* Return true when T is an address and is known to be nonzero.
14155    For floating point we further ensure that T is not denormal.
14156    Similar logic is present in nonzero_address in rtlanal.h.
14157
14158    If the return value is based on the assumption that signed overflow
14159    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14160    change *STRICT_OVERFLOW_P.  */
14161
14162 bool
14163 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14164 {
14165   tree type = TREE_TYPE (t);
14166   bool sub_strict_overflow_p;
14167
14168   /* Doing something useful for floating point would need more work.  */
14169   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14170     return false;
14171
14172   switch (TREE_CODE (t))
14173     {
14174     case SSA_NAME:
14175       /* Query VRP to see if it has recorded any information about
14176          the range of this object.  */
14177       return ssa_name_nonzero_p (t);
14178
14179     case ABS_EXPR:
14180       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14181                                         strict_overflow_p);
14182
14183     case INTEGER_CST:
14184       return !integer_zerop (t);
14185
14186     case POINTER_PLUS_EXPR:
14187     case PLUS_EXPR:
14188       if (TYPE_OVERFLOW_UNDEFINED (type))
14189         {
14190           /* With the presence of negative values it is hard
14191              to say something.  */
14192           sub_strict_overflow_p = false;
14193           if (!tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14194                                               &sub_strict_overflow_p)
14195               || !tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14196                                                  &sub_strict_overflow_p))
14197             return false;
14198           /* One of operands must be positive and the other non-negative.  */
14199           /* We don't set *STRICT_OVERFLOW_P here: even if this value
14200              overflows, on a twos-complement machine the sum of two
14201              nonnegative numbers can never be zero.  */
14202           return (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14203                                              strict_overflow_p)
14204                   || tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14205                                                 strict_overflow_p));
14206         }
14207       break;
14208
14209     case MULT_EXPR:
14210       if (TYPE_OVERFLOW_UNDEFINED (type))
14211         {
14212           if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14213                                          strict_overflow_p)
14214               && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14215                                             strict_overflow_p))
14216             {
14217               *strict_overflow_p = true;
14218               return true;
14219             }
14220         }
14221       break;
14222
14223     case NOP_EXPR:
14224       {
14225         tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
14226         tree outer_type = TREE_TYPE (t);
14227
14228         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14229                 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14230                                               strict_overflow_p));
14231       }
14232       break;
14233
14234    case ADDR_EXPR:
14235       {
14236         tree base = get_base_address (TREE_OPERAND (t, 0));
14237
14238         if (!base)
14239           return false;
14240
14241         /* Weak declarations may link to NULL.  */
14242         if (VAR_OR_FUNCTION_DECL_P (base))
14243           return !DECL_WEAK (base);
14244
14245         /* Constants are never weak.  */
14246         if (CONSTANT_CLASS_P (base))
14247           return true;
14248
14249         return false;
14250       }
14251
14252     case COND_EXPR:
14253       sub_strict_overflow_p = false;
14254       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14255                                      &sub_strict_overflow_p)
14256           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
14257                                         &sub_strict_overflow_p))
14258         {
14259           if (sub_strict_overflow_p)
14260             *strict_overflow_p = true;
14261           return true;
14262         }
14263       break;
14264
14265     case MIN_EXPR:
14266       sub_strict_overflow_p = false;
14267       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14268                                      &sub_strict_overflow_p)
14269           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14270                                         &sub_strict_overflow_p))
14271         {
14272           if (sub_strict_overflow_p)
14273             *strict_overflow_p = true;
14274         }
14275       break;
14276
14277     case MAX_EXPR:
14278       sub_strict_overflow_p = false;
14279       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14280                                      &sub_strict_overflow_p))
14281         {
14282           if (sub_strict_overflow_p)
14283             *strict_overflow_p = true;
14284
14285           /* When both operands are nonzero, then MAX must be too.  */
14286           if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14287                                          strict_overflow_p))
14288             return true;
14289
14290           /* MAX where operand 0 is positive is positive.  */
14291           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14292                                                strict_overflow_p);
14293         }
14294       /* MAX where operand 1 is positive is positive.  */
14295       else if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14296                                           &sub_strict_overflow_p)
14297                && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14298                                                  &sub_strict_overflow_p))
14299         {
14300           if (sub_strict_overflow_p)
14301             *strict_overflow_p = true;
14302           return true;
14303         }
14304       break;
14305
14306     case COMPOUND_EXPR:
14307     case MODIFY_EXPR:
14308     case GIMPLE_MODIFY_STMT:
14309     case BIND_EXPR:
14310       return tree_expr_nonzero_warnv_p (GENERIC_TREE_OPERAND (t, 1),
14311                                         strict_overflow_p);
14312
14313     case SAVE_EXPR:
14314     case NON_LVALUE_EXPR:
14315       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14316                                         strict_overflow_p);
14317
14318     case BIT_IOR_EXPR:
14319       return (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14320                                         strict_overflow_p)
14321               || tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14322                                             strict_overflow_p));
14323
14324     case CALL_EXPR:
14325       return alloca_call_p (t);
14326
14327     default:
14328       break;
14329     }
14330   return false;
14331 }
14332
14333 /* Return true when T is an address and is known to be nonzero.
14334    Handle warnings about undefined signed overflow.  */
14335
14336 bool
14337 tree_expr_nonzero_p (tree t)
14338 {
14339   bool ret, strict_overflow_p;
14340
14341   strict_overflow_p = false;
14342   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
14343   if (strict_overflow_p)
14344     fold_overflow_warning (("assuming signed overflow does not occur when "
14345                             "determining that expression is always "
14346                             "non-zero"),
14347                            WARN_STRICT_OVERFLOW_MISC);
14348   return ret;
14349 }
14350
14351 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
14352    attempt to fold the expression to a constant without modifying TYPE,
14353    OP0 or OP1.
14354
14355    If the expression could be simplified to a constant, then return
14356    the constant.  If the expression would not be simplified to a
14357    constant, then return NULL_TREE.  */
14358
14359 tree
14360 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
14361 {
14362   tree tem = fold_binary (code, type, op0, op1);
14363   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14364 }
14365
14366 /* Given the components of a unary expression CODE, TYPE and OP0,
14367    attempt to fold the expression to a constant without modifying
14368    TYPE or OP0.
14369
14370    If the expression could be simplified to a constant, then return
14371    the constant.  If the expression would not be simplified to a
14372    constant, then return NULL_TREE.  */
14373
14374 tree
14375 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
14376 {
14377   tree tem = fold_unary (code, type, op0);
14378   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14379 }
14380
14381 /* If EXP represents referencing an element in a constant string
14382    (either via pointer arithmetic or array indexing), return the
14383    tree representing the value accessed, otherwise return NULL.  */
14384
14385 tree
14386 fold_read_from_constant_string (tree exp)
14387 {
14388   if ((TREE_CODE (exp) == INDIRECT_REF
14389        || TREE_CODE (exp) == ARRAY_REF)
14390       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
14391     {
14392       tree exp1 = TREE_OPERAND (exp, 0);
14393       tree index;
14394       tree string;
14395
14396       if (TREE_CODE (exp) == INDIRECT_REF)
14397         string = string_constant (exp1, &index);
14398       else
14399         {
14400           tree low_bound = array_ref_low_bound (exp);
14401           index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
14402
14403           /* Optimize the special-case of a zero lower bound.
14404
14405              We convert the low_bound to sizetype to avoid some problems
14406              with constant folding.  (E.g. suppose the lower bound is 1,
14407              and its mode is QI.  Without the conversion,l (ARRAY
14408              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
14409              +INDEX), which becomes (ARRAY+255+INDEX).  Opps!)  */
14410           if (! integer_zerop (low_bound))
14411             index = size_diffop (index, fold_convert (sizetype, low_bound));
14412
14413           string = exp1;
14414         }
14415
14416       if (string
14417           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
14418           && TREE_CODE (string) == STRING_CST
14419           && TREE_CODE (index) == INTEGER_CST
14420           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
14421           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
14422               == MODE_INT)
14423           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
14424         return build_int_cst_type (TREE_TYPE (exp),
14425                                    (TREE_STRING_POINTER (string)
14426                                     [TREE_INT_CST_LOW (index)]));
14427     }
14428   return NULL;
14429 }
14430
14431 /* Return the tree for neg (ARG0) when ARG0 is known to be either
14432    an integer constant, real, or fixed-point constant.
14433
14434    TYPE is the type of the result.  */
14435
14436 static tree
14437 fold_negate_const (tree arg0, tree type)
14438 {
14439   tree t = NULL_TREE;
14440
14441   switch (TREE_CODE (arg0))
14442     {
14443     case INTEGER_CST:
14444       {
14445         unsigned HOST_WIDE_INT low;
14446         HOST_WIDE_INT high;
14447         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14448                                    TREE_INT_CST_HIGH (arg0),
14449                                    &low, &high);
14450         t = force_fit_type_double (type, low, high, 1,
14451                                    (overflow | TREE_OVERFLOW (arg0))
14452                                    && !TYPE_UNSIGNED (type));
14453         break;
14454       }
14455
14456     case REAL_CST:
14457       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
14458       break;
14459
14460     case FIXED_CST:
14461       {
14462         FIXED_VALUE_TYPE f;
14463         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
14464                                             &(TREE_FIXED_CST (arg0)), NULL,
14465                                             TYPE_SATURATING (type));
14466         t = build_fixed (type, f);
14467         /* Propagate overflow flags.  */
14468         if (overflow_p | TREE_OVERFLOW (arg0))
14469           {
14470             TREE_OVERFLOW (t) = 1;
14471             TREE_CONSTANT_OVERFLOW (t) = 1;
14472           }
14473         else if (TREE_CONSTANT_OVERFLOW (arg0))
14474           TREE_CONSTANT_OVERFLOW (t) = 1;
14475         break;
14476       }
14477
14478     default:
14479       gcc_unreachable ();
14480     }
14481
14482   return t;
14483 }
14484
14485 /* Return the tree for abs (ARG0) when ARG0 is known to be either
14486    an integer constant or real constant.
14487
14488    TYPE is the type of the result.  */
14489
14490 tree
14491 fold_abs_const (tree arg0, tree type)
14492 {
14493   tree t = NULL_TREE;
14494
14495   switch (TREE_CODE (arg0))
14496     {
14497     case INTEGER_CST:
14498       /* If the value is unsigned, then the absolute value is
14499          the same as the ordinary value.  */
14500       if (TYPE_UNSIGNED (type))
14501         t = arg0;
14502       /* Similarly, if the value is non-negative.  */
14503       else if (INT_CST_LT (integer_minus_one_node, arg0))
14504         t = arg0;
14505       /* If the value is negative, then the absolute value is
14506          its negation.  */
14507       else
14508         {
14509           unsigned HOST_WIDE_INT low;
14510           HOST_WIDE_INT high;
14511           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14512                                      TREE_INT_CST_HIGH (arg0),
14513                                      &low, &high);
14514           t = force_fit_type_double (type, low, high, -1,
14515                                      overflow | TREE_OVERFLOW (arg0));
14516         }
14517       break;
14518
14519     case REAL_CST:
14520       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
14521         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
14522       else
14523         t =  arg0;
14524       break;
14525
14526     default:
14527       gcc_unreachable ();
14528     }
14529
14530   return t;
14531 }
14532
14533 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
14534    constant.  TYPE is the type of the result.  */
14535
14536 static tree
14537 fold_not_const (tree arg0, tree type)
14538 {
14539   tree t = NULL_TREE;
14540
14541   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
14542
14543   t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
14544                              ~TREE_INT_CST_HIGH (arg0), 0,
14545                              TREE_OVERFLOW (arg0));
14546
14547   return t;
14548 }
14549
14550 /* Given CODE, a relational operator, the target type, TYPE and two
14551    constant operands OP0 and OP1, return the result of the
14552    relational operation.  If the result is not a compile time
14553    constant, then return NULL_TREE.  */
14554
14555 static tree
14556 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
14557 {
14558   int result, invert;
14559
14560   /* From here on, the only cases we handle are when the result is
14561      known to be a constant.  */
14562
14563   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
14564     {
14565       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
14566       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
14567
14568       /* Handle the cases where either operand is a NaN.  */
14569       if (real_isnan (c0) || real_isnan (c1))
14570         {
14571           switch (code)
14572             {
14573             case EQ_EXPR:
14574             case ORDERED_EXPR:
14575               result = 0;
14576               break;
14577
14578             case NE_EXPR:
14579             case UNORDERED_EXPR:
14580             case UNLT_EXPR:
14581             case UNLE_EXPR:
14582             case UNGT_EXPR:
14583             case UNGE_EXPR:
14584             case UNEQ_EXPR:
14585               result = 1;
14586               break;
14587
14588             case LT_EXPR:
14589             case LE_EXPR:
14590             case GT_EXPR:
14591             case GE_EXPR:
14592             case LTGT_EXPR:
14593               if (flag_trapping_math)
14594                 return NULL_TREE;
14595               result = 0;
14596               break;
14597
14598             default:
14599               gcc_unreachable ();
14600             }
14601
14602           return constant_boolean_node (result, type);
14603         }
14604
14605       return constant_boolean_node (real_compare (code, c0, c1), type);
14606     }
14607
14608   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
14609     {
14610       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
14611       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
14612       return constant_boolean_node (fixed_compare (code, c0, c1), type);
14613     }
14614
14615   /* Handle equality/inequality of complex constants.  */
14616   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
14617     {
14618       tree rcond = fold_relational_const (code, type,
14619                                           TREE_REALPART (op0),
14620                                           TREE_REALPART (op1));
14621       tree icond = fold_relational_const (code, type,
14622                                           TREE_IMAGPART (op0),
14623                                           TREE_IMAGPART (op1));
14624       if (code == EQ_EXPR)
14625         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
14626       else if (code == NE_EXPR)
14627         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
14628       else
14629         return NULL_TREE;
14630     }
14631
14632   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14633
14634      To compute GT, swap the arguments and do LT.
14635      To compute GE, do LT and invert the result.
14636      To compute LE, swap the arguments, do LT and invert the result.
14637      To compute NE, do EQ and invert the result.
14638
14639      Therefore, the code below must handle only EQ and LT.  */
14640
14641   if (code == LE_EXPR || code == GT_EXPR)
14642     {
14643       tree tem = op0;
14644       op0 = op1;
14645       op1 = tem;
14646       code = swap_tree_comparison (code);
14647     }
14648
14649   /* Note that it is safe to invert for real values here because we
14650      have already handled the one case that it matters.  */
14651
14652   invert = 0;
14653   if (code == NE_EXPR || code == GE_EXPR)
14654     {
14655       invert = 1;
14656       code = invert_tree_comparison (code, false);
14657     }
14658
14659   /* Compute a result for LT or EQ if args permit;
14660      Otherwise return T.  */
14661   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14662     {
14663       if (code == EQ_EXPR)
14664         result = tree_int_cst_equal (op0, op1);
14665       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
14666         result = INT_CST_LT_UNSIGNED (op0, op1);
14667       else
14668         result = INT_CST_LT (op0, op1);
14669     }
14670   else
14671     return NULL_TREE;
14672
14673   if (invert)
14674     result ^= 1;
14675   return constant_boolean_node (result, type);
14676 }
14677
14678 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14679    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
14680    itself.  */
14681
14682 tree
14683 fold_build_cleanup_point_expr (tree type, tree expr)
14684 {
14685   /* If the expression does not have side effects then we don't have to wrap
14686      it with a cleanup point expression.  */
14687   if (!TREE_SIDE_EFFECTS (expr))
14688     return expr;
14689
14690   /* If the expression is a return, check to see if the expression inside the
14691      return has no side effects or the right hand side of the modify expression
14692      inside the return. If either don't have side effects set we don't need to
14693      wrap the expression in a cleanup point expression.  Note we don't check the
14694      left hand side of the modify because it should always be a return decl.  */
14695   if (TREE_CODE (expr) == RETURN_EXPR)
14696     {
14697       tree op = TREE_OPERAND (expr, 0);
14698       if (!op || !TREE_SIDE_EFFECTS (op))
14699         return expr;
14700       op = TREE_OPERAND (op, 1);
14701       if (!TREE_SIDE_EFFECTS (op))
14702         return expr;
14703     }
14704   
14705   return build1 (CLEANUP_POINT_EXPR, type, expr);
14706 }
14707
14708 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14709    of an indirection through OP0, or NULL_TREE if no simplification is
14710    possible.  */
14711
14712 tree
14713 fold_indirect_ref_1 (tree type, tree op0)
14714 {
14715   tree sub = op0;
14716   tree subtype;
14717
14718   STRIP_NOPS (sub);
14719   subtype = TREE_TYPE (sub);
14720   if (!POINTER_TYPE_P (subtype))
14721     return NULL_TREE;
14722
14723   if (TREE_CODE (sub) == ADDR_EXPR)
14724     {
14725       tree op = TREE_OPERAND (sub, 0);
14726       tree optype = TREE_TYPE (op);
14727       /* *&CONST_DECL -> to the value of the const decl.  */
14728       if (TREE_CODE (op) == CONST_DECL)
14729         return DECL_INITIAL (op);
14730       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
14731       if (type == optype)
14732         {
14733           tree fop = fold_read_from_constant_string (op);
14734           if (fop)
14735             return fop;
14736           else
14737             return op;
14738         }
14739       /* *(foo *)&fooarray => fooarray[0] */
14740       else if (TREE_CODE (optype) == ARRAY_TYPE
14741                && type == TREE_TYPE (optype))
14742         {
14743           tree type_domain = TYPE_DOMAIN (optype);
14744           tree min_val = size_zero_node;
14745           if (type_domain && TYPE_MIN_VALUE (type_domain))
14746             min_val = TYPE_MIN_VALUE (type_domain);
14747           return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
14748         }
14749       /* *(foo *)&complexfoo => __real__ complexfoo */
14750       else if (TREE_CODE (optype) == COMPLEX_TYPE
14751                && type == TREE_TYPE (optype))
14752         return fold_build1 (REALPART_EXPR, type, op);
14753       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14754       else if (TREE_CODE (optype) == VECTOR_TYPE
14755                && type == TREE_TYPE (optype))
14756         {
14757           tree part_width = TYPE_SIZE (type);
14758           tree index = bitsize_int (0);
14759           return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
14760         }
14761     }
14762
14763   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14764   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14765       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14766     {
14767       tree op00 = TREE_OPERAND (sub, 0);
14768       tree op01 = TREE_OPERAND (sub, 1);
14769       tree op00type;
14770
14771       STRIP_NOPS (op00);
14772       op00type = TREE_TYPE (op00);
14773       if (TREE_CODE (op00) == ADDR_EXPR
14774           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
14775           && type == TREE_TYPE (TREE_TYPE (op00type)))
14776         {
14777           tree size = TYPE_SIZE_UNIT (type);
14778           if (tree_int_cst_equal (size, op01))
14779             return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
14780         }
14781     }
14782   
14783   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14784   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14785       && type == TREE_TYPE (TREE_TYPE (subtype)))
14786     {
14787       tree type_domain;
14788       tree min_val = size_zero_node;
14789       sub = build_fold_indirect_ref (sub);
14790       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14791       if (type_domain && TYPE_MIN_VALUE (type_domain))
14792         min_val = TYPE_MIN_VALUE (type_domain);
14793       return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
14794     }
14795
14796   return NULL_TREE;
14797 }
14798
14799 /* Builds an expression for an indirection through T, simplifying some
14800    cases.  */
14801
14802 tree
14803 build_fold_indirect_ref (tree t)
14804 {
14805   tree type = TREE_TYPE (TREE_TYPE (t));
14806   tree sub = fold_indirect_ref_1 (type, t);
14807
14808   if (sub)
14809     return sub;
14810   else
14811     return build1 (INDIRECT_REF, type, t);
14812 }
14813
14814 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
14815
14816 tree
14817 fold_indirect_ref (tree t)
14818 {
14819   tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
14820
14821   if (sub)
14822     return sub;
14823   else
14824     return t;
14825 }
14826
14827 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14828    whose result is ignored.  The type of the returned tree need not be
14829    the same as the original expression.  */
14830
14831 tree
14832 fold_ignored_result (tree t)
14833 {
14834   if (!TREE_SIDE_EFFECTS (t))
14835     return integer_zero_node;
14836
14837   for (;;)
14838     switch (TREE_CODE_CLASS (TREE_CODE (t)))
14839       {
14840       case tcc_unary:
14841         t = TREE_OPERAND (t, 0);
14842         break;
14843
14844       case tcc_binary:
14845       case tcc_comparison:
14846         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14847           t = TREE_OPERAND (t, 0);
14848         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14849           t = TREE_OPERAND (t, 1);
14850         else
14851           return t;
14852         break;
14853
14854       case tcc_expression:
14855         switch (TREE_CODE (t))
14856           {
14857           case COMPOUND_EXPR:
14858             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14859               return t;
14860             t = TREE_OPERAND (t, 0);
14861             break;
14862
14863           case COND_EXPR:
14864             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14865                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14866               return t;
14867             t = TREE_OPERAND (t, 0);
14868             break;
14869
14870           default:
14871             return t;
14872           }
14873         break;
14874
14875       default:
14876         return t;
14877       }
14878 }
14879
14880 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
14881    This can only be applied to objects of a sizetype.  */
14882
14883 tree
14884 round_up (tree value, int divisor)
14885 {
14886   tree div = NULL_TREE;
14887
14888   gcc_assert (divisor > 0);
14889   if (divisor == 1)
14890     return value;
14891
14892   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
14893      have to do anything.  Only do this when we are not given a const,
14894      because in that case, this check is more expensive than just
14895      doing it.  */
14896   if (TREE_CODE (value) != INTEGER_CST)
14897     {
14898       div = build_int_cst (TREE_TYPE (value), divisor);
14899
14900       if (multiple_of_p (TREE_TYPE (value), value, div))
14901         return value;
14902     }
14903
14904   /* If divisor is a power of two, simplify this to bit manipulation.  */
14905   if (divisor == (divisor & -divisor))
14906     {
14907       if (TREE_CODE (value) == INTEGER_CST)
14908         {
14909           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
14910           unsigned HOST_WIDE_INT high;
14911           bool overflow_p;
14912
14913           if ((low & (divisor - 1)) == 0)
14914             return value;
14915
14916           overflow_p = TREE_OVERFLOW (value);
14917           high = TREE_INT_CST_HIGH (value);
14918           low &= ~(divisor - 1);
14919           low += divisor;
14920           if (low == 0)
14921             {
14922               high++;
14923               if (high == 0)
14924                 overflow_p = true;
14925             }
14926
14927           return force_fit_type_double (TREE_TYPE (value), low, high,
14928                                         -1, overflow_p);
14929         }
14930       else
14931         {
14932           tree t;
14933
14934           t = build_int_cst (TREE_TYPE (value), divisor - 1);
14935           value = size_binop (PLUS_EXPR, value, t);
14936           t = build_int_cst (TREE_TYPE (value), -divisor);
14937           value = size_binop (BIT_AND_EXPR, value, t);
14938         }
14939     }
14940   else
14941     {
14942       if (!div)
14943         div = build_int_cst (TREE_TYPE (value), divisor);
14944       value = size_binop (CEIL_DIV_EXPR, value, div);
14945       value = size_binop (MULT_EXPR, value, div);
14946     }
14947
14948   return value;
14949 }
14950
14951 /* Likewise, but round down.  */
14952
14953 tree
14954 round_down (tree value, int divisor)
14955 {
14956   tree div = NULL_TREE;
14957
14958   gcc_assert (divisor > 0);
14959   if (divisor == 1)
14960     return value;
14961
14962   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
14963      have to do anything.  Only do this when we are not given a const,
14964      because in that case, this check is more expensive than just
14965      doing it.  */
14966   if (TREE_CODE (value) != INTEGER_CST)
14967     {
14968       div = build_int_cst (TREE_TYPE (value), divisor);
14969
14970       if (multiple_of_p (TREE_TYPE (value), value, div))
14971         return value;
14972     }
14973
14974   /* If divisor is a power of two, simplify this to bit manipulation.  */
14975   if (divisor == (divisor & -divisor))
14976     {
14977       tree t;
14978
14979       t = build_int_cst (TREE_TYPE (value), -divisor);
14980       value = size_binop (BIT_AND_EXPR, value, t);
14981     }
14982   else
14983     {
14984       if (!div)
14985         div = build_int_cst (TREE_TYPE (value), divisor);
14986       value = size_binop (FLOOR_DIV_EXPR, value, div);
14987       value = size_binop (MULT_EXPR, value, div);
14988     }
14989
14990   return value;
14991 }
14992
14993 /* Returns the pointer to the base of the object addressed by EXP and
14994    extracts the information about the offset of the access, storing it
14995    to PBITPOS and POFFSET.  */
14996
14997 static tree
14998 split_address_to_core_and_offset (tree exp,
14999                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15000 {
15001   tree core;
15002   enum machine_mode mode;
15003   int unsignedp, volatilep;
15004   HOST_WIDE_INT bitsize;
15005
15006   if (TREE_CODE (exp) == ADDR_EXPR)
15007     {
15008       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15009                                   poffset, &mode, &unsignedp, &volatilep,
15010                                   false);
15011       core = fold_addr_expr (core);
15012     }
15013   else
15014     {
15015       core = exp;
15016       *pbitpos = 0;
15017       *poffset = NULL_TREE;
15018     }
15019
15020   return core;
15021 }
15022
15023 /* Returns true if addresses of E1 and E2 differ by a constant, false
15024    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
15025
15026 bool
15027 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15028 {
15029   tree core1, core2;
15030   HOST_WIDE_INT bitpos1, bitpos2;
15031   tree toffset1, toffset2, tdiff, type;
15032
15033   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15034   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15035
15036   if (bitpos1 % BITS_PER_UNIT != 0
15037       || bitpos2 % BITS_PER_UNIT != 0
15038       || !operand_equal_p (core1, core2, 0))
15039     return false;
15040
15041   if (toffset1 && toffset2)
15042     {
15043       type = TREE_TYPE (toffset1);
15044       if (type != TREE_TYPE (toffset2))
15045         toffset2 = fold_convert (type, toffset2);
15046
15047       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15048       if (!cst_and_fits_in_hwi (tdiff))
15049         return false;
15050
15051       *diff = int_cst_value (tdiff);
15052     }
15053   else if (toffset1 || toffset2)
15054     {
15055       /* If only one of the offsets is non-constant, the difference cannot
15056          be a constant.  */
15057       return false;
15058     }
15059   else
15060     *diff = 0;
15061
15062   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15063   return true;
15064 }
15065
15066 /* Simplify the floating point expression EXP when the sign of the
15067    result is not significant.  Return NULL_TREE if no simplification
15068    is possible.  */
15069
15070 tree
15071 fold_strip_sign_ops (tree exp)
15072 {
15073   tree arg0, arg1;
15074
15075   switch (TREE_CODE (exp))
15076     {
15077     case ABS_EXPR:
15078     case NEGATE_EXPR:
15079       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15080       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15081
15082     case MULT_EXPR:
15083     case RDIV_EXPR:
15084       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15085         return NULL_TREE;
15086       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15087       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15088       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15089         return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15090                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
15091                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
15092       break;
15093
15094     case COMPOUND_EXPR:
15095       arg0 = TREE_OPERAND (exp, 0);
15096       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15097       if (arg1)
15098         return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15099       break;
15100       
15101     case COND_EXPR:
15102       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15103       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15104       if (arg0 || arg1)
15105         return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15106                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
15107                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
15108       break;
15109       
15110     case CALL_EXPR:
15111       {
15112         const enum built_in_function fcode = builtin_mathfn_code (exp);
15113         switch (fcode)
15114         {
15115         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15116           /* Strip copysign function call, return the 1st argument. */
15117           arg0 = CALL_EXPR_ARG (exp, 0);
15118           arg1 = CALL_EXPR_ARG (exp, 1);
15119           return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15120
15121         default:
15122           /* Strip sign ops from the argument of "odd" math functions.  */
15123           if (negate_mathfn_p (fcode))
15124             {
15125               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15126               if (arg0)
15127                 return build_call_expr (get_callee_fndecl (exp), 1, arg0);
15128             }
15129           break;
15130         }
15131       }
15132       break;
15133
15134     default:
15135       break;
15136     }
15137   return NULL_TREE;
15138 }