OSDN Git Service

2008-12-09 Tobias Grosser <grosser@fim.uni-passau.de>
[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, 2008
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 "target.h"
62 #include "toplev.h"
63 #include "intl.h"
64 #include "ggc.h"
65 #include "hashtab.h"
66 #include "langhooks.h"
67 #include "md5.h"
68 #include "gimple.h"
69
70 /* Nonzero if we are folding constants inside an initializer; zero
71    otherwise.  */
72 int folding_initializer = 0;
73
74 /* The following constants represent a bit based encoding of GCC's
75    comparison operators.  This encoding simplifies transformations
76    on relational comparison operators, such as AND and OR.  */
77 enum comparison_code {
78   COMPCODE_FALSE = 0,
79   COMPCODE_LT = 1,
80   COMPCODE_EQ = 2,
81   COMPCODE_LE = 3,
82   COMPCODE_GT = 4,
83   COMPCODE_LTGT = 5,
84   COMPCODE_GE = 6,
85   COMPCODE_ORD = 7,
86   COMPCODE_UNORD = 8,
87   COMPCODE_UNLT = 9,
88   COMPCODE_UNEQ = 10,
89   COMPCODE_UNLE = 11,
90   COMPCODE_UNGT = 12,
91   COMPCODE_NE = 13,
92   COMPCODE_UNGE = 14,
93   COMPCODE_TRUE = 15
94 };
95
96 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
97 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
98 static bool negate_mathfn_p (enum built_in_function);
99 static bool negate_expr_p (tree);
100 static tree negate_expr (tree);
101 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
102 static tree associate_trees (tree, tree, enum tree_code, tree);
103 static tree const_binop (enum tree_code, tree, tree, int);
104 static enum comparison_code comparison_to_compcode (enum tree_code);
105 static enum tree_code compcode_to_comparison (enum comparison_code);
106 static tree combine_comparisons (enum tree_code, enum tree_code,
107                                  enum tree_code, tree, tree, tree);
108 static int operand_equal_for_comparison_p (tree, tree, tree);
109 static int twoval_comparison_p (tree, tree *, tree *, int *);
110 static tree eval_subst (tree, tree, tree, tree, tree);
111 static tree pedantic_omit_one_operand (tree, tree, tree);
112 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
113 static tree make_bit_field_ref (tree, tree, HOST_WIDE_INT, HOST_WIDE_INT, int);
114 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
115 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
116                                     enum machine_mode *, int *, int *,
117                                     tree *, tree *);
118 static int all_ones_mask_p (const_tree, int);
119 static tree sign_bit_p (tree, const_tree);
120 static int simple_operand_p (const_tree);
121 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
122 static tree range_predecessor (tree);
123 static tree range_successor (tree);
124 static tree make_range (tree, int *, tree *, tree *, bool *);
125 static tree build_range_check (tree, tree, int, tree, tree);
126 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
127                          tree);
128 static tree fold_range_test (enum tree_code, tree, tree, tree);
129 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
130 static tree unextend (tree, int, int, tree);
131 static tree fold_truthop (enum tree_code, tree, tree, tree);
132 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
133 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
134 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
135 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
136                                                  tree, tree,
137                                                  tree, 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)), adjust the quotient.  */
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_gimple 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   if (gimple_no_warning_p (stmt))
978     return;
979
980   /* Use the smallest code level when deciding to issue the
981      warning.  */
982   if (code == 0 || code > (int) fold_deferred_overflow_code)
983     code = fold_deferred_overflow_code;
984
985   if (!issue_strict_overflow_warning (code))
986     return;
987
988   if (stmt == NULL)
989     locus = input_location;
990   else
991     locus = gimple_location (stmt);
992   warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
993 }
994
995 /* Stop deferring overflow warnings, ignoring any deferred
996    warnings.  */
997
998 void
999 fold_undefer_and_ignore_overflow_warnings (void)
1000 {
1001   fold_undefer_overflow_warnings (false, NULL, 0);
1002 }
1003
1004 /* Whether we are deferring overflow warnings.  */
1005
1006 bool
1007 fold_deferring_overflow_warnings_p (void)
1008 {
1009   return fold_deferring_overflow_warnings > 0;
1010 }
1011
1012 /* This is called when we fold something based on the fact that signed
1013    overflow is undefined.  */
1014
1015 static void
1016 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1017 {
1018   if (fold_deferring_overflow_warnings > 0)
1019     {
1020       if (fold_deferred_overflow_warning == NULL
1021           || wc < fold_deferred_overflow_code)
1022         {
1023           fold_deferred_overflow_warning = gmsgid;
1024           fold_deferred_overflow_code = wc;
1025         }
1026     }
1027   else if (issue_strict_overflow_warning (wc))
1028     warning (OPT_Wstrict_overflow, gmsgid);
1029 }
1030 \f
1031 /* Return true if the built-in mathematical function specified by CODE
1032    is odd, i.e. -f(x) == f(-x).  */
1033
1034 static bool
1035 negate_mathfn_p (enum built_in_function code)
1036 {
1037   switch (code)
1038     {
1039     CASE_FLT_FN (BUILT_IN_ASIN):
1040     CASE_FLT_FN (BUILT_IN_ASINH):
1041     CASE_FLT_FN (BUILT_IN_ATAN):
1042     CASE_FLT_FN (BUILT_IN_ATANH):
1043     CASE_FLT_FN (BUILT_IN_CASIN):
1044     CASE_FLT_FN (BUILT_IN_CASINH):
1045     CASE_FLT_FN (BUILT_IN_CATAN):
1046     CASE_FLT_FN (BUILT_IN_CATANH):
1047     CASE_FLT_FN (BUILT_IN_CBRT):
1048     CASE_FLT_FN (BUILT_IN_CPROJ):
1049     CASE_FLT_FN (BUILT_IN_CSIN):
1050     CASE_FLT_FN (BUILT_IN_CSINH):
1051     CASE_FLT_FN (BUILT_IN_CTAN):
1052     CASE_FLT_FN (BUILT_IN_CTANH):
1053     CASE_FLT_FN (BUILT_IN_ERF):
1054     CASE_FLT_FN (BUILT_IN_LLROUND):
1055     CASE_FLT_FN (BUILT_IN_LROUND):
1056     CASE_FLT_FN (BUILT_IN_ROUND):
1057     CASE_FLT_FN (BUILT_IN_SIN):
1058     CASE_FLT_FN (BUILT_IN_SINH):
1059     CASE_FLT_FN (BUILT_IN_TAN):
1060     CASE_FLT_FN (BUILT_IN_TANH):
1061     CASE_FLT_FN (BUILT_IN_TRUNC):
1062       return true;
1063
1064     CASE_FLT_FN (BUILT_IN_LLRINT):
1065     CASE_FLT_FN (BUILT_IN_LRINT):
1066     CASE_FLT_FN (BUILT_IN_NEARBYINT):
1067     CASE_FLT_FN (BUILT_IN_RINT):
1068       return !flag_rounding_math;
1069     
1070     default:
1071       break;
1072     }
1073   return false;
1074 }
1075
1076 /* Check whether we may negate an integer constant T without causing
1077    overflow.  */
1078
1079 bool
1080 may_negate_without_overflow_p (const_tree t)
1081 {
1082   unsigned HOST_WIDE_INT val;
1083   unsigned int prec;
1084   tree type;
1085
1086   gcc_assert (TREE_CODE (t) == INTEGER_CST);
1087
1088   type = TREE_TYPE (t);
1089   if (TYPE_UNSIGNED (type))
1090     return false;
1091
1092   prec = TYPE_PRECISION (type);
1093   if (prec > HOST_BITS_PER_WIDE_INT)
1094     {
1095       if (TREE_INT_CST_LOW (t) != 0)
1096         return true;
1097       prec -= HOST_BITS_PER_WIDE_INT;
1098       val = TREE_INT_CST_HIGH (t);
1099     }
1100   else
1101     val = TREE_INT_CST_LOW (t);
1102   if (prec < HOST_BITS_PER_WIDE_INT)
1103     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1104   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1105 }
1106
1107 /* Determine whether an expression T can be cheaply negated using
1108    the function negate_expr without introducing undefined overflow.  */
1109
1110 static bool
1111 negate_expr_p (tree t)
1112 {
1113   tree type;
1114
1115   if (t == 0)
1116     return false;
1117
1118   type = TREE_TYPE (t);
1119
1120   STRIP_SIGN_NOPS (t);
1121   switch (TREE_CODE (t))
1122     {
1123     case INTEGER_CST:
1124       if (TYPE_OVERFLOW_WRAPS (type))
1125         return true;
1126
1127       /* Check that -CST will not overflow type.  */
1128       return may_negate_without_overflow_p (t);
1129     case BIT_NOT_EXPR:
1130       return (INTEGRAL_TYPE_P (type)
1131               && TYPE_OVERFLOW_WRAPS (type));
1132
1133     case FIXED_CST:
1134     case REAL_CST:
1135     case NEGATE_EXPR:
1136       return true;
1137
1138     case COMPLEX_CST:
1139       return negate_expr_p (TREE_REALPART (t))
1140              && negate_expr_p (TREE_IMAGPART (t));
1141
1142     case COMPLEX_EXPR:
1143       return negate_expr_p (TREE_OPERAND (t, 0))
1144              && negate_expr_p (TREE_OPERAND (t, 1));
1145
1146     case CONJ_EXPR:
1147       return negate_expr_p (TREE_OPERAND (t, 0));
1148
1149     case PLUS_EXPR:
1150       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1151           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1152         return false;
1153       /* -(A + B) -> (-B) - A.  */
1154       if (negate_expr_p (TREE_OPERAND (t, 1))
1155           && reorder_operands_p (TREE_OPERAND (t, 0),
1156                                  TREE_OPERAND (t, 1)))
1157         return true;
1158       /* -(A + B) -> (-A) - B.  */
1159       return negate_expr_p (TREE_OPERAND (t, 0));
1160
1161     case MINUS_EXPR:
1162       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
1163       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1164              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1165              && reorder_operands_p (TREE_OPERAND (t, 0),
1166                                     TREE_OPERAND (t, 1));
1167
1168     case MULT_EXPR:
1169       if (TYPE_UNSIGNED (TREE_TYPE (t)))
1170         break;
1171
1172       /* Fall through.  */
1173
1174     case RDIV_EXPR:
1175       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1176         return negate_expr_p (TREE_OPERAND (t, 1))
1177                || negate_expr_p (TREE_OPERAND (t, 0));
1178       break;
1179
1180     case TRUNC_DIV_EXPR:
1181     case ROUND_DIV_EXPR:
1182     case FLOOR_DIV_EXPR:
1183     case CEIL_DIV_EXPR:
1184     case EXACT_DIV_EXPR:
1185       /* In general we can't negate A / B, because if A is INT_MIN and
1186          B is 1, we may turn this into INT_MIN / -1 which is undefined
1187          and actually traps on some architectures.  But if overflow is
1188          undefined, we can negate, because - (INT_MIN / 1) is an
1189          overflow.  */
1190       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1191           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1192         break;
1193       return negate_expr_p (TREE_OPERAND (t, 1))
1194              || negate_expr_p (TREE_OPERAND (t, 0));
1195
1196     case NOP_EXPR:
1197       /* Negate -((double)float) as (double)(-float).  */
1198       if (TREE_CODE (type) == REAL_TYPE)
1199         {
1200           tree tem = strip_float_extensions (t);
1201           if (tem != t)
1202             return negate_expr_p (tem);
1203         }
1204       break;
1205
1206     case CALL_EXPR:
1207       /* Negate -f(x) as f(-x).  */
1208       if (negate_mathfn_p (builtin_mathfn_code (t)))
1209         return negate_expr_p (CALL_EXPR_ARG (t, 0));
1210       break;
1211
1212     case RSHIFT_EXPR:
1213       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1214       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1215         {
1216           tree op1 = TREE_OPERAND (t, 1);
1217           if (TREE_INT_CST_HIGH (op1) == 0
1218               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1219                  == TREE_INT_CST_LOW (op1))
1220             return true;
1221         }
1222       break;
1223
1224     default:
1225       break;
1226     }
1227   return false;
1228 }
1229
1230 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1231    simplification is possible.
1232    If negate_expr_p would return true for T, NULL_TREE will never be
1233    returned.  */
1234
1235 static tree
1236 fold_negate_expr (tree t)
1237 {
1238   tree type = TREE_TYPE (t);
1239   tree tem;
1240
1241   switch (TREE_CODE (t))
1242     {
1243     /* Convert - (~A) to A + 1.  */
1244     case BIT_NOT_EXPR:
1245       if (INTEGRAL_TYPE_P (type))
1246         return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
1247                             build_int_cst (type, 1));
1248       break;
1249       
1250     case INTEGER_CST:
1251       tem = fold_negate_const (t, type);
1252       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1253           || !TYPE_OVERFLOW_TRAPS (type))
1254         return tem;
1255       break;
1256
1257     case REAL_CST:
1258       tem = fold_negate_const (t, type);
1259       /* Two's complement FP formats, such as c4x, may overflow.  */
1260       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1261         return tem;
1262       break;
1263
1264     case FIXED_CST:
1265       tem = fold_negate_const (t, type);
1266       return tem;
1267
1268     case COMPLEX_CST:
1269       {
1270         tree rpart = negate_expr (TREE_REALPART (t));
1271         tree ipart = negate_expr (TREE_IMAGPART (t));
1272
1273         if ((TREE_CODE (rpart) == REAL_CST
1274              && TREE_CODE (ipart) == REAL_CST)
1275             || (TREE_CODE (rpart) == INTEGER_CST
1276                 && TREE_CODE (ipart) == INTEGER_CST))
1277           return build_complex (type, rpart, ipart);
1278       }
1279       break;
1280
1281     case COMPLEX_EXPR:
1282       if (negate_expr_p (t))
1283         return fold_build2 (COMPLEX_EXPR, type,
1284                             fold_negate_expr (TREE_OPERAND (t, 0)),
1285                             fold_negate_expr (TREE_OPERAND (t, 1)));
1286       break;
1287       
1288     case CONJ_EXPR:
1289       if (negate_expr_p (t))
1290         return fold_build1 (CONJ_EXPR, type,
1291                             fold_negate_expr (TREE_OPERAND (t, 0)));
1292       break;
1293
1294     case NEGATE_EXPR:
1295       return TREE_OPERAND (t, 0);
1296
1297     case PLUS_EXPR:
1298       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1299           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1300         {
1301           /* -(A + B) -> (-B) - A.  */
1302           if (negate_expr_p (TREE_OPERAND (t, 1))
1303               && reorder_operands_p (TREE_OPERAND (t, 0),
1304                                      TREE_OPERAND (t, 1)))
1305             {
1306               tem = negate_expr (TREE_OPERAND (t, 1));
1307               return fold_build2 (MINUS_EXPR, type,
1308                                   tem, TREE_OPERAND (t, 0));
1309             }
1310
1311           /* -(A + B) -> (-A) - B.  */
1312           if (negate_expr_p (TREE_OPERAND (t, 0)))
1313             {
1314               tem = negate_expr (TREE_OPERAND (t, 0));
1315               return fold_build2 (MINUS_EXPR, type,
1316                                   tem, TREE_OPERAND (t, 1));
1317             }
1318         }
1319       break;
1320
1321     case MINUS_EXPR:
1322       /* - (A - B) -> B - A  */
1323       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1324           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1325           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1326         return fold_build2 (MINUS_EXPR, type,
1327                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1328       break;
1329
1330     case MULT_EXPR:
1331       if (TYPE_UNSIGNED (type))
1332         break;
1333
1334       /* Fall through.  */
1335
1336     case RDIV_EXPR:
1337       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1338         {
1339           tem = TREE_OPERAND (t, 1);
1340           if (negate_expr_p (tem))
1341             return fold_build2 (TREE_CODE (t), type,
1342                                 TREE_OPERAND (t, 0), negate_expr (tem));
1343           tem = TREE_OPERAND (t, 0);
1344           if (negate_expr_p (tem))
1345             return fold_build2 (TREE_CODE (t), type,
1346                                 negate_expr (tem), TREE_OPERAND (t, 1));
1347         }
1348       break;
1349
1350     case TRUNC_DIV_EXPR:
1351     case ROUND_DIV_EXPR:
1352     case FLOOR_DIV_EXPR:
1353     case CEIL_DIV_EXPR:
1354     case EXACT_DIV_EXPR:
1355       /* In general we can't negate A / B, because if A is INT_MIN and
1356          B is 1, we may turn this into INT_MIN / -1 which is undefined
1357          and actually traps on some architectures.  But if overflow is
1358          undefined, we can negate, because - (INT_MIN / 1) is an
1359          overflow.  */
1360       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1361         {
1362           const char * const warnmsg = G_("assuming signed overflow does not "
1363                                           "occur when negating a division");
1364           tem = TREE_OPERAND (t, 1);
1365           if (negate_expr_p (tem))
1366             {
1367               if (INTEGRAL_TYPE_P (type)
1368                   && (TREE_CODE (tem) != INTEGER_CST
1369                       || integer_onep (tem)))
1370                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1371               return fold_build2 (TREE_CODE (t), type,
1372                                   TREE_OPERAND (t, 0), negate_expr (tem));
1373             }
1374           tem = TREE_OPERAND (t, 0);
1375           if (negate_expr_p (tem))
1376             {
1377               if (INTEGRAL_TYPE_P (type)
1378                   && (TREE_CODE (tem) != INTEGER_CST
1379                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1380                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1381               return fold_build2 (TREE_CODE (t), type,
1382                                   negate_expr (tem), TREE_OPERAND (t, 1));
1383             }
1384         }
1385       break;
1386
1387     case NOP_EXPR:
1388       /* Convert -((double)float) into (double)(-float).  */
1389       if (TREE_CODE (type) == REAL_TYPE)
1390         {
1391           tem = strip_float_extensions (t);
1392           if (tem != t && negate_expr_p (tem))
1393             return fold_convert (type, negate_expr (tem));
1394         }
1395       break;
1396
1397     case CALL_EXPR:
1398       /* Negate -f(x) as f(-x).  */
1399       if (negate_mathfn_p (builtin_mathfn_code (t))
1400           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1401         {
1402           tree fndecl, arg;
1403
1404           fndecl = get_callee_fndecl (t);
1405           arg = negate_expr (CALL_EXPR_ARG (t, 0));
1406           return build_call_expr (fndecl, 1, arg);
1407         }
1408       break;
1409
1410     case RSHIFT_EXPR:
1411       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1412       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1413         {
1414           tree op1 = TREE_OPERAND (t, 1);
1415           if (TREE_INT_CST_HIGH (op1) == 0
1416               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1417                  == TREE_INT_CST_LOW (op1))
1418             {
1419               tree ntype = TYPE_UNSIGNED (type)
1420                            ? signed_type_for (type)
1421                            : unsigned_type_for (type);
1422               tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1423               temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1424               return fold_convert (type, temp);
1425             }
1426         }
1427       break;
1428
1429     default:
1430       break;
1431     }
1432
1433   return NULL_TREE;
1434 }
1435
1436 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1437    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
1438    return NULL_TREE. */
1439
1440 static tree
1441 negate_expr (tree t)
1442 {
1443   tree type, tem;
1444
1445   if (t == NULL_TREE)
1446     return NULL_TREE;
1447
1448   type = TREE_TYPE (t);
1449   STRIP_SIGN_NOPS (t);
1450
1451   tem = fold_negate_expr (t);
1452   if (!tem)
1453     tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1454   return fold_convert (type, tem);
1455 }
1456 \f
1457 /* Split a tree IN into a constant, literal and variable parts that could be
1458    combined with CODE to make IN.  "constant" means an expression with
1459    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1460    commutative arithmetic operation.  Store the constant part into *CONP,
1461    the literal in *LITP and return the variable part.  If a part isn't
1462    present, set it to null.  If the tree does not decompose in this way,
1463    return the entire tree as the variable part and the other parts as null.
1464
1465    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1466    case, we negate an operand that was subtracted.  Except if it is a
1467    literal for which we use *MINUS_LITP instead.
1468
1469    If NEGATE_P is true, we are negating all of IN, again except a literal
1470    for which we use *MINUS_LITP instead.
1471
1472    If IN is itself a literal or constant, return it as appropriate.
1473
1474    Note that we do not guarantee that any of the three values will be the
1475    same type as IN, but they will have the same signedness and mode.  */
1476
1477 static tree
1478 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1479             tree *minus_litp, int negate_p)
1480 {
1481   tree var = 0;
1482
1483   *conp = 0;
1484   *litp = 0;
1485   *minus_litp = 0;
1486
1487   /* Strip any conversions that don't change the machine mode or signedness.  */
1488   STRIP_SIGN_NOPS (in);
1489
1490   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1491       || TREE_CODE (in) == FIXED_CST)
1492     *litp = in;
1493   else if (TREE_CODE (in) == code
1494            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1495                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1496                /* We can associate addition and subtraction together (even
1497                   though the C standard doesn't say so) for integers because
1498                   the value is not affected.  For reals, the value might be
1499                   affected, so we can't.  */
1500                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1501                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1502     {
1503       tree op0 = TREE_OPERAND (in, 0);
1504       tree op1 = TREE_OPERAND (in, 1);
1505       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1506       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1507
1508       /* First see if either of the operands is a literal, then a constant.  */
1509       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1510           || TREE_CODE (op0) == FIXED_CST)
1511         *litp = op0, op0 = 0;
1512       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1513                || TREE_CODE (op1) == FIXED_CST)
1514         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1515
1516       if (op0 != 0 && TREE_CONSTANT (op0))
1517         *conp = op0, op0 = 0;
1518       else if (op1 != 0 && TREE_CONSTANT (op1))
1519         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1520
1521       /* If we haven't dealt with either operand, this is not a case we can
1522          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1523       if (op0 != 0 && op1 != 0)
1524         var = in;
1525       else if (op0 != 0)
1526         var = op0;
1527       else
1528         var = op1, neg_var_p = neg1_p;
1529
1530       /* Now do any needed negations.  */
1531       if (neg_litp_p)
1532         *minus_litp = *litp, *litp = 0;
1533       if (neg_conp_p)
1534         *conp = negate_expr (*conp);
1535       if (neg_var_p)
1536         var = negate_expr (var);
1537     }
1538   else if (TREE_CONSTANT (in))
1539     *conp = in;
1540   else
1541     var = in;
1542
1543   if (negate_p)
1544     {
1545       if (*litp)
1546         *minus_litp = *litp, *litp = 0;
1547       else if (*minus_litp)
1548         *litp = *minus_litp, *minus_litp = 0;
1549       *conp = negate_expr (*conp);
1550       var = negate_expr (var);
1551     }
1552
1553   return var;
1554 }
1555
1556 /* Re-associate trees split by the above function.  T1 and T2 are either
1557    expressions to associate or null.  Return the new expression, if any.  If
1558    we build an operation, do it in TYPE and with CODE.  */
1559
1560 static tree
1561 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1562 {
1563   if (t1 == 0)
1564     return t2;
1565   else if (t2 == 0)
1566     return t1;
1567
1568   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1569      try to fold this since we will have infinite recursion.  But do
1570      deal with any NEGATE_EXPRs.  */
1571   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1572       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1573     {
1574       if (code == PLUS_EXPR)
1575         {
1576           if (TREE_CODE (t1) == NEGATE_EXPR)
1577             return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1578                            fold_convert (type, TREE_OPERAND (t1, 0)));
1579           else if (TREE_CODE (t2) == NEGATE_EXPR)
1580             return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1581                            fold_convert (type, TREE_OPERAND (t2, 0)));
1582           else if (integer_zerop (t2))
1583             return fold_convert (type, t1);
1584         }
1585       else if (code == MINUS_EXPR)
1586         {
1587           if (integer_zerop (t2))
1588             return fold_convert (type, t1);
1589         }
1590
1591       return build2 (code, type, fold_convert (type, t1),
1592                      fold_convert (type, t2));
1593     }
1594
1595   return fold_build2 (code, type, fold_convert (type, t1),
1596                       fold_convert (type, t2));
1597 }
1598 \f
1599 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1600    for use in int_const_binop, size_binop and size_diffop.  */
1601
1602 static bool
1603 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1604 {
1605   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1606     return false;
1607   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1608     return false;
1609
1610   switch (code)
1611     {
1612     case LSHIFT_EXPR:
1613     case RSHIFT_EXPR:
1614     case LROTATE_EXPR:
1615     case RROTATE_EXPR:
1616       return true;
1617
1618     default:
1619       break;
1620     }
1621
1622   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1623          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1624          && TYPE_MODE (type1) == TYPE_MODE (type2);
1625 }
1626
1627
1628 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1629    to produce a new constant.  Return NULL_TREE if we don't know how
1630    to evaluate CODE at compile-time.
1631
1632    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1633
1634 tree
1635 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1636 {
1637   unsigned HOST_WIDE_INT int1l, int2l;
1638   HOST_WIDE_INT int1h, int2h;
1639   unsigned HOST_WIDE_INT low;
1640   HOST_WIDE_INT hi;
1641   unsigned HOST_WIDE_INT garbagel;
1642   HOST_WIDE_INT garbageh;
1643   tree t;
1644   tree type = TREE_TYPE (arg1);
1645   int uns = TYPE_UNSIGNED (type);
1646   int is_sizetype
1647     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1648   int overflow = 0;
1649
1650   int1l = TREE_INT_CST_LOW (arg1);
1651   int1h = TREE_INT_CST_HIGH (arg1);
1652   int2l = TREE_INT_CST_LOW (arg2);
1653   int2h = TREE_INT_CST_HIGH (arg2);
1654
1655   switch (code)
1656     {
1657     case BIT_IOR_EXPR:
1658       low = int1l | int2l, hi = int1h | int2h;
1659       break;
1660
1661     case BIT_XOR_EXPR:
1662       low = int1l ^ int2l, hi = int1h ^ int2h;
1663       break;
1664
1665     case BIT_AND_EXPR:
1666       low = int1l & int2l, hi = int1h & int2h;
1667       break;
1668
1669     case RSHIFT_EXPR:
1670       int2l = -int2l;
1671     case LSHIFT_EXPR:
1672       /* It's unclear from the C standard whether shifts can overflow.
1673          The following code ignores overflow; perhaps a C standard
1674          interpretation ruling is needed.  */
1675       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1676                      &low, &hi, !uns);
1677       break;
1678
1679     case RROTATE_EXPR:
1680       int2l = - int2l;
1681     case LROTATE_EXPR:
1682       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1683                       &low, &hi);
1684       break;
1685
1686     case PLUS_EXPR:
1687       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1688       break;
1689
1690     case MINUS_EXPR:
1691       neg_double (int2l, int2h, &low, &hi);
1692       add_double (int1l, int1h, low, hi, &low, &hi);
1693       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1694       break;
1695
1696     case MULT_EXPR:
1697       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1698       break;
1699
1700     case TRUNC_DIV_EXPR:
1701     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1702     case EXACT_DIV_EXPR:
1703       /* This is a shortcut for a common special case.  */
1704       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1705           && !TREE_OVERFLOW (arg1)
1706           && !TREE_OVERFLOW (arg2)
1707           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1708         {
1709           if (code == CEIL_DIV_EXPR)
1710             int1l += int2l - 1;
1711
1712           low = int1l / int2l, hi = 0;
1713           break;
1714         }
1715
1716       /* ... fall through ...  */
1717
1718     case ROUND_DIV_EXPR:
1719       if (int2h == 0 && int2l == 0)
1720         return NULL_TREE;
1721       if (int2h == 0 && int2l == 1)
1722         {
1723           low = int1l, hi = int1h;
1724           break;
1725         }
1726       if (int1l == int2l && int1h == int2h
1727           && ! (int1l == 0 && int1h == 0))
1728         {
1729           low = 1, hi = 0;
1730           break;
1731         }
1732       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1733                                        &low, &hi, &garbagel, &garbageh);
1734       break;
1735
1736     case TRUNC_MOD_EXPR:
1737     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1738       /* This is a shortcut for a common special case.  */
1739       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1740           && !TREE_OVERFLOW (arg1)
1741           && !TREE_OVERFLOW (arg2)
1742           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1743         {
1744           if (code == CEIL_MOD_EXPR)
1745             int1l += int2l - 1;
1746           low = int1l % int2l, hi = 0;
1747           break;
1748         }
1749
1750       /* ... fall through ...  */
1751
1752     case ROUND_MOD_EXPR:
1753       if (int2h == 0 && int2l == 0)
1754         return NULL_TREE;
1755       overflow = div_and_round_double (code, uns,
1756                                        int1l, int1h, int2l, int2h,
1757                                        &garbagel, &garbageh, &low, &hi);
1758       break;
1759
1760     case MIN_EXPR:
1761     case MAX_EXPR:
1762       if (uns)
1763         low = (((unsigned HOST_WIDE_INT) int1h
1764                 < (unsigned HOST_WIDE_INT) int2h)
1765                || (((unsigned HOST_WIDE_INT) int1h
1766                     == (unsigned HOST_WIDE_INT) int2h)
1767                    && int1l < int2l));
1768       else
1769         low = (int1h < int2h
1770                || (int1h == int2h && int1l < int2l));
1771
1772       if (low == (code == MIN_EXPR))
1773         low = int1l, hi = int1h;
1774       else
1775         low = int2l, hi = int2h;
1776       break;
1777
1778     default:
1779       return NULL_TREE;
1780     }
1781
1782   if (notrunc)
1783     {
1784       t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1785
1786       /* Propagate overflow flags ourselves.  */
1787       if (((!uns || is_sizetype) && overflow)
1788           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1789         {
1790           t = copy_node (t);
1791           TREE_OVERFLOW (t) = 1;
1792         }
1793     }
1794   else
1795     t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1796                                ((!uns || is_sizetype) && overflow)
1797                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1798
1799   return t;
1800 }
1801
1802 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1803    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1804    are the same kind of constant and the same machine mode.  Return zero if
1805    combining the constants is not allowed in the current operating mode.
1806
1807    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1808
1809 static tree
1810 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1811 {
1812   /* Sanity check for the recursive cases.  */
1813   if (!arg1 || !arg2)
1814     return NULL_TREE;
1815
1816   STRIP_NOPS (arg1);
1817   STRIP_NOPS (arg2);
1818
1819   if (TREE_CODE (arg1) == INTEGER_CST)
1820     return int_const_binop (code, arg1, arg2, notrunc);
1821
1822   if (TREE_CODE (arg1) == REAL_CST)
1823     {
1824       enum machine_mode mode;
1825       REAL_VALUE_TYPE d1;
1826       REAL_VALUE_TYPE d2;
1827       REAL_VALUE_TYPE value;
1828       REAL_VALUE_TYPE result;
1829       bool inexact;
1830       tree t, type;
1831
1832       /* The following codes are handled by real_arithmetic.  */
1833       switch (code)
1834         {
1835         case PLUS_EXPR:
1836         case MINUS_EXPR:
1837         case MULT_EXPR:
1838         case RDIV_EXPR:
1839         case MIN_EXPR:
1840         case MAX_EXPR:
1841           break;
1842
1843         default:
1844           return NULL_TREE;
1845         }
1846
1847       d1 = TREE_REAL_CST (arg1);
1848       d2 = TREE_REAL_CST (arg2);
1849
1850       type = TREE_TYPE (arg1);
1851       mode = TYPE_MODE (type);
1852
1853       /* Don't perform operation if we honor signaling NaNs and
1854          either operand is a NaN.  */
1855       if (HONOR_SNANS (mode)
1856           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1857         return NULL_TREE;
1858
1859       /* Don't perform operation if it would raise a division
1860          by zero exception.  */
1861       if (code == RDIV_EXPR
1862           && REAL_VALUES_EQUAL (d2, dconst0)
1863           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1864         return NULL_TREE;
1865
1866       /* If either operand is a NaN, just return it.  Otherwise, set up
1867          for floating-point trap; we return an overflow.  */
1868       if (REAL_VALUE_ISNAN (d1))
1869         return arg1;
1870       else if (REAL_VALUE_ISNAN (d2))
1871         return arg2;
1872
1873       inexact = real_arithmetic (&value, code, &d1, &d2);
1874       real_convert (&result, mode, &value);
1875
1876       /* Don't constant fold this floating point operation if
1877          the result has overflowed and flag_trapping_math.  */
1878       if (flag_trapping_math
1879           && MODE_HAS_INFINITIES (mode)
1880           && REAL_VALUE_ISINF (result)
1881           && !REAL_VALUE_ISINF (d1)
1882           && !REAL_VALUE_ISINF (d2))
1883         return NULL_TREE;
1884
1885       /* Don't constant fold this floating point operation if the
1886          result may dependent upon the run-time rounding mode and
1887          flag_rounding_math is set, or if GCC's software emulation
1888          is unable to accurately represent the result.  */
1889       if ((flag_rounding_math
1890            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1891           && (inexact || !real_identical (&result, &value)))
1892         return NULL_TREE;
1893
1894       t = build_real (type, result);
1895
1896       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1897       return t;
1898     }
1899
1900   if (TREE_CODE (arg1) == FIXED_CST)
1901     {
1902       FIXED_VALUE_TYPE f1;
1903       FIXED_VALUE_TYPE f2;
1904       FIXED_VALUE_TYPE result;
1905       tree t, type;
1906       int sat_p;
1907       bool overflow_p;
1908
1909       /* The following codes are handled by fixed_arithmetic.  */
1910       switch (code)
1911         {
1912         case PLUS_EXPR:
1913         case MINUS_EXPR:
1914         case MULT_EXPR:
1915         case TRUNC_DIV_EXPR:
1916           f2 = TREE_FIXED_CST (arg2);
1917           break;
1918
1919         case LSHIFT_EXPR:
1920         case RSHIFT_EXPR:
1921           f2.data.high = TREE_INT_CST_HIGH (arg2);
1922           f2.data.low = TREE_INT_CST_LOW (arg2);
1923           f2.mode = SImode;
1924           break;
1925
1926         default:
1927           return NULL_TREE;
1928         }
1929
1930       f1 = TREE_FIXED_CST (arg1);
1931       type = TREE_TYPE (arg1);
1932       sat_p = TYPE_SATURATING (type);
1933       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1934       t = build_fixed (type, result);
1935       /* Propagate overflow flags.  */
1936       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1937         {
1938           TREE_OVERFLOW (t) = 1;
1939           TREE_CONSTANT_OVERFLOW (t) = 1;
1940         }
1941       else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1942         TREE_CONSTANT_OVERFLOW (t) = 1;
1943       return t;
1944     }
1945
1946   if (TREE_CODE (arg1) == COMPLEX_CST)
1947     {
1948       tree type = TREE_TYPE (arg1);
1949       tree r1 = TREE_REALPART (arg1);
1950       tree i1 = TREE_IMAGPART (arg1);
1951       tree r2 = TREE_REALPART (arg2);
1952       tree i2 = TREE_IMAGPART (arg2);
1953       tree real, imag;
1954
1955       switch (code)
1956         {
1957         case PLUS_EXPR:
1958         case MINUS_EXPR:
1959           real = const_binop (code, r1, r2, notrunc);
1960           imag = const_binop (code, i1, i2, notrunc);
1961           break;
1962
1963         case MULT_EXPR:
1964           real = const_binop (MINUS_EXPR,
1965                               const_binop (MULT_EXPR, r1, r2, notrunc),
1966                               const_binop (MULT_EXPR, i1, i2, notrunc),
1967                               notrunc);
1968           imag = const_binop (PLUS_EXPR,
1969                               const_binop (MULT_EXPR, r1, i2, notrunc),
1970                               const_binop (MULT_EXPR, i1, r2, notrunc),
1971                               notrunc);
1972           break;
1973
1974         case RDIV_EXPR:
1975           {
1976             tree magsquared
1977               = const_binop (PLUS_EXPR,
1978                              const_binop (MULT_EXPR, r2, r2, notrunc),
1979                              const_binop (MULT_EXPR, i2, i2, notrunc),
1980                              notrunc);
1981             tree t1
1982               = const_binop (PLUS_EXPR,
1983                              const_binop (MULT_EXPR, r1, r2, notrunc),
1984                              const_binop (MULT_EXPR, i1, i2, notrunc),
1985                              notrunc);
1986             tree t2
1987               = const_binop (MINUS_EXPR,
1988                              const_binop (MULT_EXPR, i1, r2, notrunc),
1989                              const_binop (MULT_EXPR, r1, i2, notrunc),
1990                              notrunc);
1991
1992             if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1993               code = TRUNC_DIV_EXPR;
1994
1995             real = const_binop (code, t1, magsquared, notrunc);
1996             imag = const_binop (code, t2, magsquared, notrunc);
1997           }
1998           break;
1999
2000         default:
2001           return NULL_TREE;
2002         }
2003
2004       if (real && imag)
2005         return build_complex (type, real, imag);
2006     }
2007
2008   return NULL_TREE;
2009 }
2010
2011 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
2012    indicates which particular sizetype to create.  */
2013
2014 tree
2015 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2016 {
2017   return build_int_cst (sizetype_tab[(int) kind], number);
2018 }
2019 \f
2020 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
2021    is a tree code.  The type of the result is taken from the operands.
2022    Both must be equivalent integer types, ala int_binop_types_match_p.
2023    If the operands are constant, so is the result.  */
2024
2025 tree
2026 size_binop (enum tree_code code, tree arg0, tree arg1)
2027 {
2028   tree type = TREE_TYPE (arg0);
2029
2030   if (arg0 == error_mark_node || arg1 == error_mark_node)
2031     return error_mark_node;
2032
2033   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2034                                        TREE_TYPE (arg1)));
2035
2036   /* Handle the special case of two integer constants faster.  */
2037   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2038     {
2039       /* And some specific cases even faster than that.  */
2040       if (code == PLUS_EXPR)
2041         {
2042           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2043             return arg1;
2044           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2045             return arg0;
2046         }
2047       else if (code == MINUS_EXPR)
2048         {
2049           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2050             return arg0;
2051         }
2052       else if (code == MULT_EXPR)
2053         {
2054           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2055             return arg1;
2056         }
2057
2058       /* Handle general case of two integer constants.  */
2059       return int_const_binop (code, arg0, arg1, 0);
2060     }
2061
2062   return fold_build2 (code, type, arg0, arg1);
2063 }
2064
2065 /* Given two values, either both of sizetype or both of bitsizetype,
2066    compute the difference between the two values.  Return the value
2067    in signed type corresponding to the type of the operands.  */
2068
2069 tree
2070 size_diffop (tree arg0, tree arg1)
2071 {
2072   tree type = TREE_TYPE (arg0);
2073   tree ctype;
2074
2075   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2076                                        TREE_TYPE (arg1)));
2077
2078   /* If the type is already signed, just do the simple thing.  */
2079   if (!TYPE_UNSIGNED (type))
2080     return size_binop (MINUS_EXPR, arg0, arg1);
2081
2082   if (type == sizetype)
2083     ctype = ssizetype;
2084   else if (type == bitsizetype)
2085     ctype = sbitsizetype;
2086   else
2087     ctype = signed_type_for (type);
2088
2089   /* If either operand is not a constant, do the conversions to the signed
2090      type and subtract.  The hardware will do the right thing with any
2091      overflow in the subtraction.  */
2092   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2093     return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2094                        fold_convert (ctype, arg1));
2095
2096   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2097      Otherwise, subtract the other way, convert to CTYPE (we know that can't
2098      overflow) and negate (which can't either).  Special-case a result
2099      of zero while we're here.  */
2100   if (tree_int_cst_equal (arg0, arg1))
2101     return build_int_cst (ctype, 0);
2102   else if (tree_int_cst_lt (arg1, arg0))
2103     return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2104   else
2105     return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2106                        fold_convert (ctype, size_binop (MINUS_EXPR,
2107                                                         arg1, arg0)));
2108 }
2109 \f
2110 /* A subroutine of fold_convert_const handling conversions of an
2111    INTEGER_CST to another integer type.  */
2112
2113 static tree
2114 fold_convert_const_int_from_int (tree type, const_tree arg1)
2115 {
2116   tree t;
2117
2118   /* Given an integer constant, make new constant with new type,
2119      appropriately sign-extended or truncated.  */
2120   t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2121                              TREE_INT_CST_HIGH (arg1),
2122                              /* Don't set the overflow when
2123                                 converting from a pointer,  */
2124                              !POINTER_TYPE_P (TREE_TYPE (arg1))
2125                              /* or to a sizetype with same signedness
2126                                 and the precision is unchanged.
2127                                 ???  sizetype is always sign-extended,
2128                                 but its signedness depends on the
2129                                 frontend.  Thus we see spurious overflows
2130                                 here if we do not check this.  */
2131                              && !((TYPE_PRECISION (TREE_TYPE (arg1))
2132                                    == TYPE_PRECISION (type))
2133                                   && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2134                                       == TYPE_UNSIGNED (type))
2135                                   && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2136                                        && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2137                                       || (TREE_CODE (type) == INTEGER_TYPE
2138                                           && TYPE_IS_SIZETYPE (type)))),
2139                              (TREE_INT_CST_HIGH (arg1) < 0
2140                               && (TYPE_UNSIGNED (type)
2141                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2142                              | TREE_OVERFLOW (arg1));
2143
2144   return t;
2145 }
2146
2147 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2148    to an integer type.  */
2149
2150 static tree
2151 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2152 {
2153   int overflow = 0;
2154   tree t;
2155
2156   /* The following code implements the floating point to integer
2157      conversion rules required by the Java Language Specification,
2158      that IEEE NaNs are mapped to zero and values that overflow
2159      the target precision saturate, i.e. values greater than
2160      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2161      are mapped to INT_MIN.  These semantics are allowed by the
2162      C and C++ standards that simply state that the behavior of
2163      FP-to-integer conversion is unspecified upon overflow.  */
2164
2165   HOST_WIDE_INT high, low;
2166   REAL_VALUE_TYPE r;
2167   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2168
2169   switch (code)
2170     {
2171     case FIX_TRUNC_EXPR:
2172       real_trunc (&r, VOIDmode, &x);
2173       break;
2174
2175     default:
2176       gcc_unreachable ();
2177     }
2178
2179   /* If R is NaN, return zero and show we have an overflow.  */
2180   if (REAL_VALUE_ISNAN (r))
2181     {
2182       overflow = 1;
2183       high = 0;
2184       low = 0;
2185     }
2186
2187   /* See if R is less than the lower bound or greater than the
2188      upper bound.  */
2189
2190   if (! overflow)
2191     {
2192       tree lt = TYPE_MIN_VALUE (type);
2193       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2194       if (REAL_VALUES_LESS (r, l))
2195         {
2196           overflow = 1;
2197           high = TREE_INT_CST_HIGH (lt);
2198           low = TREE_INT_CST_LOW (lt);
2199         }
2200     }
2201
2202   if (! overflow)
2203     {
2204       tree ut = TYPE_MAX_VALUE (type);
2205       if (ut)
2206         {
2207           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2208           if (REAL_VALUES_LESS (u, r))
2209             {
2210               overflow = 1;
2211               high = TREE_INT_CST_HIGH (ut);
2212               low = TREE_INT_CST_LOW (ut);
2213             }
2214         }
2215     }
2216
2217   if (! overflow)
2218     REAL_VALUE_TO_INT (&low, &high, r);
2219
2220   t = force_fit_type_double (type, low, high, -1,
2221                              overflow | TREE_OVERFLOW (arg1));
2222   return t;
2223 }
2224
2225 /* A subroutine of fold_convert_const handling conversions of a
2226    FIXED_CST to an integer type.  */
2227
2228 static tree
2229 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2230 {
2231   tree t;
2232   double_int temp, temp_trunc;
2233   unsigned int mode;
2234
2235   /* Right shift FIXED_CST to temp by fbit.  */
2236   temp = TREE_FIXED_CST (arg1).data;
2237   mode = TREE_FIXED_CST (arg1).mode;
2238   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2239     {
2240       lshift_double (temp.low, temp.high,
2241                      - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2242                      &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2243
2244       /* Left shift temp to temp_trunc by fbit.  */
2245       lshift_double (temp.low, temp.high,
2246                      GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2247                      &temp_trunc.low, &temp_trunc.high,
2248                      SIGNED_FIXED_POINT_MODE_P (mode));
2249     }
2250   else
2251     {
2252       temp.low = 0;
2253       temp.high = 0;
2254       temp_trunc.low = 0;
2255       temp_trunc.high = 0;
2256     }
2257
2258   /* If FIXED_CST is negative, we need to round the value toward 0.
2259      By checking if the fractional bits are not zero to add 1 to temp.  */
2260   if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2261       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2262     {
2263       double_int one;
2264       one.low = 1;
2265       one.high = 0;
2266       temp = double_int_add (temp, one);
2267     }
2268
2269   /* Given a fixed-point constant, make new constant with new type,
2270      appropriately sign-extended or truncated.  */
2271   t = force_fit_type_double (type, temp.low, temp.high, -1,
2272                              (temp.high < 0
2273                               && (TYPE_UNSIGNED (type)
2274                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2275                              | TREE_OVERFLOW (arg1));
2276
2277   return t;
2278 }
2279
2280 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2281    to another floating point type.  */
2282
2283 static tree
2284 fold_convert_const_real_from_real (tree type, const_tree arg1)
2285 {
2286   REAL_VALUE_TYPE value;
2287   tree t;
2288
2289   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2290   t = build_real (type, value);
2291
2292   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2293   return t;
2294 }
2295
2296 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2297    to a floating point type.  */
2298
2299 static tree
2300 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2301 {
2302   REAL_VALUE_TYPE value;
2303   tree t;
2304
2305   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2306   t = build_real (type, value);
2307
2308   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2309   TREE_CONSTANT_OVERFLOW (t)
2310     = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2311   return t;
2312 }
2313
2314 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2315    to another fixed-point type.  */
2316
2317 static tree
2318 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2319 {
2320   FIXED_VALUE_TYPE value;
2321   tree t;
2322   bool overflow_p;
2323
2324   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2325                               TYPE_SATURATING (type));
2326   t = build_fixed (type, value);
2327
2328   /* Propagate overflow flags.  */
2329   if (overflow_p | TREE_OVERFLOW (arg1))
2330     {
2331       TREE_OVERFLOW (t) = 1;
2332       TREE_CONSTANT_OVERFLOW (t) = 1;
2333     }
2334   else if (TREE_CONSTANT_OVERFLOW (arg1))
2335     TREE_CONSTANT_OVERFLOW (t) = 1;
2336   return t;
2337 }
2338
2339 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2340    to a fixed-point type.  */
2341
2342 static tree
2343 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2344 {
2345   FIXED_VALUE_TYPE value;
2346   tree t;
2347   bool overflow_p;
2348
2349   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2350                                        TREE_INT_CST (arg1),
2351                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
2352                                        TYPE_SATURATING (type));
2353   t = build_fixed (type, value);
2354
2355   /* Propagate overflow flags.  */
2356   if (overflow_p | TREE_OVERFLOW (arg1))
2357     {
2358       TREE_OVERFLOW (t) = 1;
2359       TREE_CONSTANT_OVERFLOW (t) = 1;
2360     }
2361   else if (TREE_CONSTANT_OVERFLOW (arg1))
2362     TREE_CONSTANT_OVERFLOW (t) = 1;
2363   return t;
2364 }
2365
2366 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2367    to a fixed-point type.  */
2368
2369 static tree
2370 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2371 {
2372   FIXED_VALUE_TYPE value;
2373   tree t;
2374   bool overflow_p;
2375
2376   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2377                                         &TREE_REAL_CST (arg1),
2378                                         TYPE_SATURATING (type));
2379   t = build_fixed (type, value);
2380
2381   /* Propagate overflow flags.  */
2382   if (overflow_p | TREE_OVERFLOW (arg1))
2383     {
2384       TREE_OVERFLOW (t) = 1;
2385       TREE_CONSTANT_OVERFLOW (t) = 1;
2386     }
2387   else if (TREE_CONSTANT_OVERFLOW (arg1))
2388     TREE_CONSTANT_OVERFLOW (t) = 1;
2389   return t;
2390 }
2391
2392 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2393    type TYPE.  If no simplification can be done return NULL_TREE.  */
2394
2395 static tree
2396 fold_convert_const (enum tree_code code, tree type, tree arg1)
2397 {
2398   if (TREE_TYPE (arg1) == type)
2399     return arg1;
2400
2401   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2402       || TREE_CODE (type) == OFFSET_TYPE)
2403     {
2404       if (TREE_CODE (arg1) == INTEGER_CST)
2405         return fold_convert_const_int_from_int (type, arg1);
2406       else if (TREE_CODE (arg1) == REAL_CST)
2407         return fold_convert_const_int_from_real (code, type, arg1);
2408       else if (TREE_CODE (arg1) == FIXED_CST)
2409         return fold_convert_const_int_from_fixed (type, arg1);
2410     }
2411   else if (TREE_CODE (type) == REAL_TYPE)
2412     {
2413       if (TREE_CODE (arg1) == INTEGER_CST)
2414         return build_real_from_int_cst (type, arg1);
2415       else if (TREE_CODE (arg1) == REAL_CST)
2416         return fold_convert_const_real_from_real (type, arg1);
2417       else if (TREE_CODE (arg1) == FIXED_CST)
2418         return fold_convert_const_real_from_fixed (type, arg1);
2419     }
2420   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2421     {
2422       if (TREE_CODE (arg1) == FIXED_CST)
2423         return fold_convert_const_fixed_from_fixed (type, arg1);
2424       else if (TREE_CODE (arg1) == INTEGER_CST)
2425         return fold_convert_const_fixed_from_int (type, arg1);
2426       else if (TREE_CODE (arg1) == REAL_CST)
2427         return fold_convert_const_fixed_from_real (type, arg1);
2428     }
2429   return NULL_TREE;
2430 }
2431
2432 /* Construct a vector of zero elements of vector type TYPE.  */
2433
2434 static tree
2435 build_zero_vector (tree type)
2436 {
2437   tree elem, list;
2438   int i, units;
2439
2440   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2441   units = TYPE_VECTOR_SUBPARTS (type);
2442   
2443   list = NULL_TREE;
2444   for (i = 0; i < units; i++)
2445     list = tree_cons (NULL_TREE, elem, list);
2446   return build_vector (type, list);
2447 }
2448
2449 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
2450
2451 bool
2452 fold_convertible_p (const_tree type, const_tree arg)
2453 {
2454   tree orig = TREE_TYPE (arg);
2455
2456   if (type == orig)
2457     return true;
2458
2459   if (TREE_CODE (arg) == ERROR_MARK
2460       || TREE_CODE (type) == ERROR_MARK
2461       || TREE_CODE (orig) == ERROR_MARK)
2462     return false;
2463
2464   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2465     return true;
2466
2467   switch (TREE_CODE (type))
2468     {
2469     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2470     case POINTER_TYPE: case REFERENCE_TYPE:
2471     case OFFSET_TYPE:
2472       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2473           || TREE_CODE (orig) == OFFSET_TYPE)
2474         return true;
2475       return (TREE_CODE (orig) == VECTOR_TYPE
2476               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2477
2478     case REAL_TYPE:
2479     case FIXED_POINT_TYPE:
2480     case COMPLEX_TYPE:
2481     case VECTOR_TYPE:
2482     case VOID_TYPE:
2483       return TREE_CODE (type) == TREE_CODE (orig);
2484
2485     default:
2486       return false;
2487     }
2488 }
2489
2490 /* Convert expression ARG to type TYPE.  Used by the middle-end for
2491    simple conversions in preference to calling the front-end's convert.  */
2492
2493 tree
2494 fold_convert (tree type, tree arg)
2495 {
2496   tree orig = TREE_TYPE (arg);
2497   tree tem;
2498
2499   if (type == orig)
2500     return arg;
2501
2502   if (TREE_CODE (arg) == ERROR_MARK
2503       || TREE_CODE (type) == ERROR_MARK
2504       || TREE_CODE (orig) == ERROR_MARK)
2505     return error_mark_node;
2506
2507   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2508     return fold_build1 (NOP_EXPR, type, arg);
2509
2510   switch (TREE_CODE (type))
2511     {
2512     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2513     case POINTER_TYPE: case REFERENCE_TYPE:
2514     case OFFSET_TYPE:
2515       if (TREE_CODE (arg) == INTEGER_CST)
2516         {
2517           tem = fold_convert_const (NOP_EXPR, type, arg);
2518           if (tem != NULL_TREE)
2519             return tem;
2520         }
2521       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2522           || TREE_CODE (orig) == OFFSET_TYPE)
2523         return fold_build1 (NOP_EXPR, type, arg);
2524       if (TREE_CODE (orig) == COMPLEX_TYPE)
2525         {
2526           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2527           return fold_convert (type, tem);
2528         }
2529       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2530                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2531       return fold_build1 (NOP_EXPR, type, arg);
2532
2533     case REAL_TYPE:
2534       if (TREE_CODE (arg) == INTEGER_CST)
2535         {
2536           tem = fold_convert_const (FLOAT_EXPR, type, arg);
2537           if (tem != NULL_TREE)
2538             return tem;
2539         }
2540       else if (TREE_CODE (arg) == REAL_CST)
2541         {
2542           tem = fold_convert_const (NOP_EXPR, type, arg);
2543           if (tem != NULL_TREE)
2544             return tem;
2545         }
2546       else if (TREE_CODE (arg) == FIXED_CST)
2547         {
2548           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2549           if (tem != NULL_TREE)
2550             return tem;
2551         }
2552
2553       switch (TREE_CODE (orig))
2554         {
2555         case INTEGER_TYPE:
2556         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2557         case POINTER_TYPE: case REFERENCE_TYPE:
2558           return fold_build1 (FLOAT_EXPR, type, arg);
2559
2560         case REAL_TYPE:
2561           return fold_build1 (NOP_EXPR, type, arg);
2562
2563         case FIXED_POINT_TYPE:
2564           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2565
2566         case COMPLEX_TYPE:
2567           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2568           return fold_convert (type, tem);
2569
2570         default:
2571           gcc_unreachable ();
2572         }
2573
2574     case FIXED_POINT_TYPE:
2575       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2576           || TREE_CODE (arg) == REAL_CST)
2577         {
2578           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2579           if (tem != NULL_TREE)
2580             return tem;
2581         }
2582
2583       switch (TREE_CODE (orig))
2584         {
2585         case FIXED_POINT_TYPE:
2586         case INTEGER_TYPE:
2587         case ENUMERAL_TYPE:
2588         case BOOLEAN_TYPE:
2589         case REAL_TYPE:
2590           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2591
2592         case COMPLEX_TYPE:
2593           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2594           return fold_convert (type, tem);
2595
2596         default:
2597           gcc_unreachable ();
2598         }
2599
2600     case COMPLEX_TYPE:
2601       switch (TREE_CODE (orig))
2602         {
2603         case INTEGER_TYPE:
2604         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2605         case POINTER_TYPE: case REFERENCE_TYPE:
2606         case REAL_TYPE:
2607         case FIXED_POINT_TYPE:
2608           return build2 (COMPLEX_EXPR, type,
2609                          fold_convert (TREE_TYPE (type), arg),
2610                          fold_convert (TREE_TYPE (type), integer_zero_node));
2611         case COMPLEX_TYPE:
2612           {
2613             tree rpart, ipart;
2614
2615             if (TREE_CODE (arg) == COMPLEX_EXPR)
2616               {
2617                 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2618                 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2619                 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2620               }
2621
2622             arg = save_expr (arg);
2623             rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2624             ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2625             rpart = fold_convert (TREE_TYPE (type), rpart);
2626             ipart = fold_convert (TREE_TYPE (type), ipart);
2627             return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2628           }
2629
2630         default:
2631           gcc_unreachable ();
2632         }
2633
2634     case VECTOR_TYPE:
2635       if (integer_zerop (arg))
2636         return build_zero_vector (type);
2637       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2638       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2639                   || TREE_CODE (orig) == VECTOR_TYPE);
2640       return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2641
2642     case VOID_TYPE:
2643       tem = fold_ignored_result (arg);
2644       if (TREE_CODE (tem) == MODIFY_EXPR)
2645         return tem;
2646       return fold_build1 (NOP_EXPR, type, tem);
2647
2648     default:
2649       gcc_unreachable ();
2650     }
2651 }
2652 \f
2653 /* Return false if expr can be assumed not to be an lvalue, true
2654    otherwise.  */
2655
2656 static bool
2657 maybe_lvalue_p (const_tree x)
2658 {
2659   /* We only need to wrap lvalue tree codes.  */
2660   switch (TREE_CODE (x))
2661   {
2662   case VAR_DECL:
2663   case PARM_DECL:
2664   case RESULT_DECL:
2665   case LABEL_DECL:
2666   case FUNCTION_DECL:
2667   case SSA_NAME:
2668
2669   case COMPONENT_REF:
2670   case INDIRECT_REF:
2671   case ALIGN_INDIRECT_REF:
2672   case MISALIGNED_INDIRECT_REF:
2673   case ARRAY_REF:
2674   case ARRAY_RANGE_REF:
2675   case BIT_FIELD_REF:
2676   case OBJ_TYPE_REF:
2677
2678   case REALPART_EXPR:
2679   case IMAGPART_EXPR:
2680   case PREINCREMENT_EXPR:
2681   case PREDECREMENT_EXPR:
2682   case SAVE_EXPR:
2683   case TRY_CATCH_EXPR:
2684   case WITH_CLEANUP_EXPR:
2685   case COMPOUND_EXPR:
2686   case MODIFY_EXPR:
2687   case TARGET_EXPR:
2688   case COND_EXPR:
2689   case BIND_EXPR:
2690   case MIN_EXPR:
2691   case MAX_EXPR:
2692     break;
2693
2694   default:
2695     /* Assume the worst for front-end tree codes.  */
2696     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2697       break;
2698     return false;
2699   }
2700
2701   return true;
2702 }
2703
2704 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2705
2706 tree
2707 non_lvalue (tree x)
2708 {
2709   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2710      us.  */
2711   if (in_gimple_form)
2712     return x;
2713
2714   if (! maybe_lvalue_p (x))
2715     return x;
2716   return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2717 }
2718
2719 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2720    Zero means allow extended lvalues.  */
2721
2722 int pedantic_lvalues;
2723
2724 /* When pedantic, return an expr equal to X but certainly not valid as a
2725    pedantic lvalue.  Otherwise, return X.  */
2726
2727 static tree
2728 pedantic_non_lvalue (tree x)
2729 {
2730   if (pedantic_lvalues)
2731     return non_lvalue (x);
2732   else
2733     return x;
2734 }
2735 \f
2736 /* Given a tree comparison code, return the code that is the logical inverse
2737    of the given code.  It is not safe to do this for floating-point
2738    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2739    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2740
2741 enum tree_code
2742 invert_tree_comparison (enum tree_code code, bool honor_nans)
2743 {
2744   if (honor_nans && flag_trapping_math)
2745     return ERROR_MARK;
2746
2747   switch (code)
2748     {
2749     case EQ_EXPR:
2750       return NE_EXPR;
2751     case NE_EXPR:
2752       return EQ_EXPR;
2753     case GT_EXPR:
2754       return honor_nans ? UNLE_EXPR : LE_EXPR;
2755     case GE_EXPR:
2756       return honor_nans ? UNLT_EXPR : LT_EXPR;
2757     case LT_EXPR:
2758       return honor_nans ? UNGE_EXPR : GE_EXPR;
2759     case LE_EXPR:
2760       return honor_nans ? UNGT_EXPR : GT_EXPR;
2761     case LTGT_EXPR:
2762       return UNEQ_EXPR;
2763     case UNEQ_EXPR:
2764       return LTGT_EXPR;
2765     case UNGT_EXPR:
2766       return LE_EXPR;
2767     case UNGE_EXPR:
2768       return LT_EXPR;
2769     case UNLT_EXPR:
2770       return GE_EXPR;
2771     case UNLE_EXPR:
2772       return GT_EXPR;
2773     case ORDERED_EXPR:
2774       return UNORDERED_EXPR;
2775     case UNORDERED_EXPR:
2776       return ORDERED_EXPR;
2777     default:
2778       gcc_unreachable ();
2779     }
2780 }
2781
2782 /* Similar, but return the comparison that results if the operands are
2783    swapped.  This is safe for floating-point.  */
2784
2785 enum tree_code
2786 swap_tree_comparison (enum tree_code code)
2787 {
2788   switch (code)
2789     {
2790     case EQ_EXPR:
2791     case NE_EXPR:
2792     case ORDERED_EXPR:
2793     case UNORDERED_EXPR:
2794     case LTGT_EXPR:
2795     case UNEQ_EXPR:
2796       return code;
2797     case GT_EXPR:
2798       return LT_EXPR;
2799     case GE_EXPR:
2800       return LE_EXPR;
2801     case LT_EXPR:
2802       return GT_EXPR;
2803     case LE_EXPR:
2804       return GE_EXPR;
2805     case UNGT_EXPR:
2806       return UNLT_EXPR;
2807     case UNGE_EXPR:
2808       return UNLE_EXPR;
2809     case UNLT_EXPR:
2810       return UNGT_EXPR;
2811     case UNLE_EXPR:
2812       return UNGE_EXPR;
2813     default:
2814       gcc_unreachable ();
2815     }
2816 }
2817
2818
2819 /* Convert a comparison tree code from an enum tree_code representation
2820    into a compcode bit-based encoding.  This function is the inverse of
2821    compcode_to_comparison.  */
2822
2823 static enum comparison_code
2824 comparison_to_compcode (enum tree_code code)
2825 {
2826   switch (code)
2827     {
2828     case LT_EXPR:
2829       return COMPCODE_LT;
2830     case EQ_EXPR:
2831       return COMPCODE_EQ;
2832     case LE_EXPR:
2833       return COMPCODE_LE;
2834     case GT_EXPR:
2835       return COMPCODE_GT;
2836     case NE_EXPR:
2837       return COMPCODE_NE;
2838     case GE_EXPR:
2839       return COMPCODE_GE;
2840     case ORDERED_EXPR:
2841       return COMPCODE_ORD;
2842     case UNORDERED_EXPR:
2843       return COMPCODE_UNORD;
2844     case UNLT_EXPR:
2845       return COMPCODE_UNLT;
2846     case UNEQ_EXPR:
2847       return COMPCODE_UNEQ;
2848     case UNLE_EXPR:
2849       return COMPCODE_UNLE;
2850     case UNGT_EXPR:
2851       return COMPCODE_UNGT;
2852     case LTGT_EXPR:
2853       return COMPCODE_LTGT;
2854     case UNGE_EXPR:
2855       return COMPCODE_UNGE;
2856     default:
2857       gcc_unreachable ();
2858     }
2859 }
2860
2861 /* Convert a compcode bit-based encoding of a comparison operator back
2862    to GCC's enum tree_code representation.  This function is the
2863    inverse of comparison_to_compcode.  */
2864
2865 static enum tree_code
2866 compcode_to_comparison (enum comparison_code code)
2867 {
2868   switch (code)
2869     {
2870     case COMPCODE_LT:
2871       return LT_EXPR;
2872     case COMPCODE_EQ:
2873       return EQ_EXPR;
2874     case COMPCODE_LE:
2875       return LE_EXPR;
2876     case COMPCODE_GT:
2877       return GT_EXPR;
2878     case COMPCODE_NE:
2879       return NE_EXPR;
2880     case COMPCODE_GE:
2881       return GE_EXPR;
2882     case COMPCODE_ORD:
2883       return ORDERED_EXPR;
2884     case COMPCODE_UNORD:
2885       return UNORDERED_EXPR;
2886     case COMPCODE_UNLT:
2887       return UNLT_EXPR;
2888     case COMPCODE_UNEQ:
2889       return UNEQ_EXPR;
2890     case COMPCODE_UNLE:
2891       return UNLE_EXPR;
2892     case COMPCODE_UNGT:
2893       return UNGT_EXPR;
2894     case COMPCODE_LTGT:
2895       return LTGT_EXPR;
2896     case COMPCODE_UNGE:
2897       return UNGE_EXPR;
2898     default:
2899       gcc_unreachable ();
2900     }
2901 }
2902
2903 /* Return a tree for the comparison which is the combination of
2904    doing the AND or OR (depending on CODE) of the two operations LCODE
2905    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2906    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2907    if this makes the transformation invalid.  */
2908
2909 tree
2910 combine_comparisons (enum tree_code code, enum tree_code lcode,
2911                      enum tree_code rcode, tree truth_type,
2912                      tree ll_arg, tree lr_arg)
2913 {
2914   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2915   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2916   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2917   enum comparison_code compcode;
2918
2919   switch (code)
2920     {
2921     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2922       compcode = lcompcode & rcompcode;
2923       break;
2924
2925     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2926       compcode = lcompcode | rcompcode;
2927       break;
2928
2929     default:
2930       return NULL_TREE;
2931     }
2932
2933   if (!honor_nans)
2934     {
2935       /* Eliminate unordered comparisons, as well as LTGT and ORD
2936          which are not used unless the mode has NaNs.  */
2937       compcode &= ~COMPCODE_UNORD;
2938       if (compcode == COMPCODE_LTGT)
2939         compcode = COMPCODE_NE;
2940       else if (compcode == COMPCODE_ORD)
2941         compcode = COMPCODE_TRUE;
2942     }
2943    else if (flag_trapping_math)
2944      {
2945         /* Check that the original operation and the optimized ones will trap
2946            under the same condition.  */
2947         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2948                      && (lcompcode != COMPCODE_EQ)
2949                      && (lcompcode != COMPCODE_ORD);
2950         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2951                      && (rcompcode != COMPCODE_EQ)
2952                      && (rcompcode != COMPCODE_ORD);
2953         bool trap = (compcode & COMPCODE_UNORD) == 0
2954                     && (compcode != COMPCODE_EQ)
2955                     && (compcode != COMPCODE_ORD);
2956
2957         /* In a short-circuited boolean expression the LHS might be
2958            such that the RHS, if evaluated, will never trap.  For
2959            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2960            if neither x nor y is NaN.  (This is a mixed blessing: for
2961            example, the expression above will never trap, hence
2962            optimizing it to x < y would be invalid).  */
2963         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2964             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2965           rtrap = false;
2966
2967         /* If the comparison was short-circuited, and only the RHS
2968            trapped, we may now generate a spurious trap.  */
2969         if (rtrap && !ltrap
2970             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2971           return NULL_TREE;
2972
2973         /* If we changed the conditions that cause a trap, we lose.  */
2974         if ((ltrap || rtrap) != trap)
2975           return NULL_TREE;
2976       }
2977
2978   if (compcode == COMPCODE_TRUE)
2979     return constant_boolean_node (true, truth_type);
2980   else if (compcode == COMPCODE_FALSE)
2981     return constant_boolean_node (false, truth_type);
2982   else
2983     return fold_build2 (compcode_to_comparison (compcode),
2984                         truth_type, ll_arg, lr_arg);
2985 }
2986 \f
2987 /* Return nonzero if two operands (typically of the same tree node)
2988    are necessarily equal.  If either argument has side-effects this
2989    function returns zero.  FLAGS modifies behavior as follows:
2990
2991    If OEP_ONLY_CONST is set, only return nonzero for constants.
2992    This function tests whether the operands are indistinguishable;
2993    it does not test whether they are equal using C's == operation.
2994    The distinction is important for IEEE floating point, because
2995    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2996    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2997
2998    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2999    even though it may hold multiple values during a function.
3000    This is because a GCC tree node guarantees that nothing else is
3001    executed between the evaluation of its "operands" (which may often
3002    be evaluated in arbitrary order).  Hence if the operands themselves
3003    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3004    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
3005    unset means assuming isochronic (or instantaneous) tree equivalence.
3006    Unless comparing arbitrary expression trees, such as from different
3007    statements, this flag can usually be left unset.
3008
3009    If OEP_PURE_SAME is set, then pure functions with identical arguments
3010    are considered the same.  It is used when the caller has other ways
3011    to ensure that global memory is unchanged in between.  */
3012
3013 int
3014 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3015 {
3016   /* If either is ERROR_MARK, they aren't equal.  */
3017   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3018     return 0;
3019
3020   /* Check equality of integer constants before bailing out due to
3021      precision differences.  */
3022   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3023     return tree_int_cst_equal (arg0, arg1);
3024
3025   /* If both types don't have the same signedness, then we can't consider
3026      them equal.  We must check this before the STRIP_NOPS calls
3027      because they may change the signedness of the arguments.  As pointers
3028      strictly don't have a signedness, require either two pointers or
3029      two non-pointers as well.  */
3030   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3031       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
3032     return 0;
3033
3034   /* If both types don't have the same precision, then it is not safe
3035      to strip NOPs.  */
3036   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3037     return 0;
3038
3039   STRIP_NOPS (arg0);
3040   STRIP_NOPS (arg1);
3041
3042   /* In case both args are comparisons but with different comparison
3043      code, try to swap the comparison operands of one arg to produce
3044      a match and compare that variant.  */
3045   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3046       && COMPARISON_CLASS_P (arg0)
3047       && COMPARISON_CLASS_P (arg1))
3048     {
3049       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3050
3051       if (TREE_CODE (arg0) == swap_code)
3052         return operand_equal_p (TREE_OPERAND (arg0, 0),
3053                                 TREE_OPERAND (arg1, 1), flags)
3054                && operand_equal_p (TREE_OPERAND (arg0, 1),
3055                                    TREE_OPERAND (arg1, 0), flags);
3056     }
3057
3058   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3059       /* This is needed for conversions and for COMPONENT_REF.
3060          Might as well play it safe and always test this.  */
3061       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3062       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3063       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3064     return 0;
3065
3066   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3067      We don't care about side effects in that case because the SAVE_EXPR
3068      takes care of that for us. In all other cases, two expressions are
3069      equal if they have no side effects.  If we have two identical
3070      expressions with side effects that should be treated the same due
3071      to the only side effects being identical SAVE_EXPR's, that will
3072      be detected in the recursive calls below.  */
3073   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3074       && (TREE_CODE (arg0) == SAVE_EXPR
3075           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3076     return 1;
3077
3078   /* Next handle constant cases, those for which we can return 1 even
3079      if ONLY_CONST is set.  */
3080   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3081     switch (TREE_CODE (arg0))
3082       {
3083       case INTEGER_CST:
3084         return tree_int_cst_equal (arg0, arg1);
3085
3086       case FIXED_CST:
3087         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3088                                        TREE_FIXED_CST (arg1));
3089
3090       case REAL_CST:
3091         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3092                                    TREE_REAL_CST (arg1)))
3093           return 1;
3094
3095         
3096         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3097           {
3098             /* If we do not distinguish between signed and unsigned zero,
3099                consider them equal.  */
3100             if (real_zerop (arg0) && real_zerop (arg1))
3101               return 1;
3102           }
3103         return 0;
3104
3105       case VECTOR_CST:
3106         {
3107           tree v1, v2;
3108
3109           v1 = TREE_VECTOR_CST_ELTS (arg0);
3110           v2 = TREE_VECTOR_CST_ELTS (arg1);
3111           while (v1 && v2)
3112             {
3113               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3114                                     flags))
3115                 return 0;
3116               v1 = TREE_CHAIN (v1);
3117               v2 = TREE_CHAIN (v2);
3118             }
3119
3120           return v1 == v2;
3121         }
3122
3123       case COMPLEX_CST:
3124         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3125                                  flags)
3126                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3127                                     flags));
3128
3129       case STRING_CST:
3130         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3131                 && ! memcmp (TREE_STRING_POINTER (arg0),
3132                               TREE_STRING_POINTER (arg1),
3133                               TREE_STRING_LENGTH (arg0)));
3134
3135       case ADDR_EXPR:
3136         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3137                                 0);
3138       default:
3139         break;
3140       }
3141
3142   if (flags & OEP_ONLY_CONST)
3143     return 0;
3144
3145 /* Define macros to test an operand from arg0 and arg1 for equality and a
3146    variant that allows null and views null as being different from any
3147    non-null value.  In the latter case, if either is null, the both
3148    must be; otherwise, do the normal comparison.  */
3149 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
3150                                     TREE_OPERAND (arg1, N), flags)
3151
3152 #define OP_SAME_WITH_NULL(N)                            \
3153   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3154    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3155
3156   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3157     {
3158     case tcc_unary:
3159       /* Two conversions are equal only if signedness and modes match.  */
3160       switch (TREE_CODE (arg0))
3161         {
3162         CASE_CONVERT:
3163         case FIX_TRUNC_EXPR:
3164           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3165               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3166             return 0;
3167           break;
3168         default:
3169           break;
3170         }
3171
3172       return OP_SAME (0);
3173
3174
3175     case tcc_comparison:
3176     case tcc_binary:
3177       if (OP_SAME (0) && OP_SAME (1))
3178         return 1;
3179
3180       /* For commutative ops, allow the other order.  */
3181       return (commutative_tree_code (TREE_CODE (arg0))
3182               && operand_equal_p (TREE_OPERAND (arg0, 0),
3183                                   TREE_OPERAND (arg1, 1), flags)
3184               && operand_equal_p (TREE_OPERAND (arg0, 1),
3185                                   TREE_OPERAND (arg1, 0), flags));
3186
3187     case tcc_reference:
3188       /* If either of the pointer (or reference) expressions we are
3189          dereferencing contain a side effect, these cannot be equal.  */
3190       if (TREE_SIDE_EFFECTS (arg0)
3191           || TREE_SIDE_EFFECTS (arg1))
3192         return 0;
3193
3194       switch (TREE_CODE (arg0))
3195         {
3196         case INDIRECT_REF:
3197         case ALIGN_INDIRECT_REF:
3198         case MISALIGNED_INDIRECT_REF:
3199         case REALPART_EXPR:
3200         case IMAGPART_EXPR:
3201           return OP_SAME (0);
3202
3203         case ARRAY_REF:
3204         case ARRAY_RANGE_REF:
3205           /* Operands 2 and 3 may be null.
3206              Compare the array index by value if it is constant first as we
3207              may have different types but same value here.  */
3208           return (OP_SAME (0)
3209                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3210                                           TREE_OPERAND (arg1, 1))
3211                       || OP_SAME (1))
3212                   && OP_SAME_WITH_NULL (2)
3213                   && OP_SAME_WITH_NULL (3));
3214
3215         case COMPONENT_REF:
3216           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
3217              may be NULL when we're called to compare MEM_EXPRs.  */
3218           return OP_SAME_WITH_NULL (0)
3219                  && OP_SAME (1)
3220                  && OP_SAME_WITH_NULL (2);
3221
3222         case BIT_FIELD_REF:
3223           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3224
3225         default:
3226           return 0;
3227         }
3228
3229     case tcc_expression:
3230       switch (TREE_CODE (arg0))
3231         {
3232         case ADDR_EXPR:
3233         case TRUTH_NOT_EXPR:
3234           return OP_SAME (0);
3235
3236         case TRUTH_ANDIF_EXPR:
3237         case TRUTH_ORIF_EXPR:
3238           return OP_SAME (0) && OP_SAME (1);
3239
3240         case TRUTH_AND_EXPR:
3241         case TRUTH_OR_EXPR:
3242         case TRUTH_XOR_EXPR:
3243           if (OP_SAME (0) && OP_SAME (1))
3244             return 1;
3245
3246           /* Otherwise take into account this is a commutative operation.  */
3247           return (operand_equal_p (TREE_OPERAND (arg0, 0),
3248                                    TREE_OPERAND (arg1, 1), flags)
3249                   && operand_equal_p (TREE_OPERAND (arg0, 1),
3250                                       TREE_OPERAND (arg1, 0), flags));
3251
3252         case COND_EXPR:
3253           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3254           
3255         default:
3256           return 0;
3257         }
3258
3259     case tcc_vl_exp:
3260       switch (TREE_CODE (arg0))
3261         {
3262         case CALL_EXPR:
3263           /* If the CALL_EXPRs call different functions, then they
3264              clearly can not be equal.  */
3265           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3266                                  flags))
3267             return 0;
3268
3269           {
3270             unsigned int cef = call_expr_flags (arg0);
3271             if (flags & OEP_PURE_SAME)
3272               cef &= ECF_CONST | ECF_PURE;
3273             else
3274               cef &= ECF_CONST;
3275             if (!cef)
3276               return 0;
3277           }
3278
3279           /* Now see if all the arguments are the same.  */
3280           {
3281             const_call_expr_arg_iterator iter0, iter1;
3282             const_tree a0, a1;
3283             for (a0 = first_const_call_expr_arg (arg0, &iter0),
3284                    a1 = first_const_call_expr_arg (arg1, &iter1);
3285                  a0 && a1;
3286                  a0 = next_const_call_expr_arg (&iter0),
3287                    a1 = next_const_call_expr_arg (&iter1))
3288               if (! operand_equal_p (a0, a1, flags))
3289                 return 0;
3290
3291             /* If we get here and both argument lists are exhausted
3292                then the CALL_EXPRs are equal.  */
3293             return ! (a0 || a1);
3294           }
3295         default:
3296           return 0;
3297         }
3298
3299     case tcc_declaration:
3300       /* Consider __builtin_sqrt equal to sqrt.  */
3301       return (TREE_CODE (arg0) == FUNCTION_DECL
3302               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3303               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3304               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3305
3306     default:
3307       return 0;
3308     }
3309
3310 #undef OP_SAME
3311 #undef OP_SAME_WITH_NULL
3312 }
3313 \f
3314 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3315    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3316
3317    When in doubt, return 0.  */
3318
3319 static int
3320 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3321 {
3322   int unsignedp1, unsignedpo;
3323   tree primarg0, primarg1, primother;
3324   unsigned int correct_width;
3325
3326   if (operand_equal_p (arg0, arg1, 0))
3327     return 1;
3328
3329   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3330       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3331     return 0;
3332
3333   /* Discard any conversions that don't change the modes of ARG0 and ARG1
3334      and see if the inner values are the same.  This removes any
3335      signedness comparison, which doesn't matter here.  */
3336   primarg0 = arg0, primarg1 = arg1;
3337   STRIP_NOPS (primarg0);
3338   STRIP_NOPS (primarg1);
3339   if (operand_equal_p (primarg0, primarg1, 0))
3340     return 1;
3341
3342   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3343      actual comparison operand, ARG0.
3344
3345      First throw away any conversions to wider types
3346      already present in the operands.  */
3347
3348   primarg1 = get_narrower (arg1, &unsignedp1);
3349   primother = get_narrower (other, &unsignedpo);
3350
3351   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3352   if (unsignedp1 == unsignedpo
3353       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3354       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3355     {
3356       tree type = TREE_TYPE (arg0);
3357
3358       /* Make sure shorter operand is extended the right way
3359          to match the longer operand.  */
3360       primarg1 = fold_convert (signed_or_unsigned_type_for
3361                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3362
3363       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3364         return 1;
3365     }
3366
3367   return 0;
3368 }
3369 \f
3370 /* See if ARG is an expression that is either a comparison or is performing
3371    arithmetic on comparisons.  The comparisons must only be comparing
3372    two different values, which will be stored in *CVAL1 and *CVAL2; if
3373    they are nonzero it means that some operands have already been found.
3374    No variables may be used anywhere else in the expression except in the
3375    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
3376    the expression and save_expr needs to be called with CVAL1 and CVAL2.
3377
3378    If this is true, return 1.  Otherwise, return zero.  */
3379
3380 static int
3381 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3382 {
3383   enum tree_code code = TREE_CODE (arg);
3384   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3385
3386   /* We can handle some of the tcc_expression cases here.  */
3387   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3388     tclass = tcc_unary;
3389   else if (tclass == tcc_expression
3390            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3391                || code == COMPOUND_EXPR))
3392     tclass = tcc_binary;
3393
3394   else if (tclass == tcc_expression && code == SAVE_EXPR
3395            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3396     {
3397       /* If we've already found a CVAL1 or CVAL2, this expression is
3398          two complex to handle.  */
3399       if (*cval1 || *cval2)
3400         return 0;
3401
3402       tclass = tcc_unary;
3403       *save_p = 1;
3404     }
3405
3406   switch (tclass)
3407     {
3408     case tcc_unary:
3409       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3410
3411     case tcc_binary:
3412       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3413               && twoval_comparison_p (TREE_OPERAND (arg, 1),
3414                                       cval1, cval2, save_p));
3415
3416     case tcc_constant:
3417       return 1;
3418
3419     case tcc_expression:
3420       if (code == COND_EXPR)
3421         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3422                                      cval1, cval2, save_p)
3423                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3424                                         cval1, cval2, save_p)
3425                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3426                                         cval1, cval2, save_p));
3427       return 0;
3428
3429     case tcc_comparison:
3430       /* First see if we can handle the first operand, then the second.  For
3431          the second operand, we know *CVAL1 can't be zero.  It must be that
3432          one side of the comparison is each of the values; test for the
3433          case where this isn't true by failing if the two operands
3434          are the same.  */
3435
3436       if (operand_equal_p (TREE_OPERAND (arg, 0),
3437                            TREE_OPERAND (arg, 1), 0))
3438         return 0;
3439
3440       if (*cval1 == 0)
3441         *cval1 = TREE_OPERAND (arg, 0);
3442       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3443         ;
3444       else if (*cval2 == 0)
3445         *cval2 = TREE_OPERAND (arg, 0);
3446       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3447         ;
3448       else
3449         return 0;
3450
3451       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3452         ;
3453       else if (*cval2 == 0)
3454         *cval2 = TREE_OPERAND (arg, 1);
3455       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3456         ;
3457       else
3458         return 0;
3459
3460       return 1;
3461
3462     default:
3463       return 0;
3464     }
3465 }
3466 \f
3467 /* ARG is a tree that is known to contain just arithmetic operations and
3468    comparisons.  Evaluate the operations in the tree substituting NEW0 for
3469    any occurrence of OLD0 as an operand of a comparison and likewise for
3470    NEW1 and OLD1.  */
3471
3472 static tree
3473 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3474 {
3475   tree type = TREE_TYPE (arg);
3476   enum tree_code code = TREE_CODE (arg);
3477   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3478
3479   /* We can handle some of the tcc_expression cases here.  */
3480   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3481     tclass = tcc_unary;
3482   else if (tclass == tcc_expression
3483            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3484     tclass = tcc_binary;
3485
3486   switch (tclass)
3487     {
3488     case tcc_unary:
3489       return fold_build1 (code, type,
3490                           eval_subst (TREE_OPERAND (arg, 0),
3491                                       old0, new0, old1, new1));
3492
3493     case tcc_binary:
3494       return fold_build2 (code, type,
3495                           eval_subst (TREE_OPERAND (arg, 0),
3496                                       old0, new0, old1, new1),
3497                           eval_subst (TREE_OPERAND (arg, 1),
3498                                       old0, new0, old1, new1));
3499
3500     case tcc_expression:
3501       switch (code)
3502         {
3503         case SAVE_EXPR:
3504           return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3505
3506         case COMPOUND_EXPR:
3507           return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3508
3509         case COND_EXPR:
3510           return fold_build3 (code, type,
3511                               eval_subst (TREE_OPERAND (arg, 0),
3512                                           old0, new0, old1, new1),
3513                               eval_subst (TREE_OPERAND (arg, 1),
3514                                           old0, new0, old1, new1),
3515                               eval_subst (TREE_OPERAND (arg, 2),
3516                                           old0, new0, old1, new1));
3517         default:
3518           break;
3519         }
3520       /* Fall through - ???  */
3521
3522     case tcc_comparison:
3523       {
3524         tree arg0 = TREE_OPERAND (arg, 0);
3525         tree arg1 = TREE_OPERAND (arg, 1);
3526
3527         /* We need to check both for exact equality and tree equality.  The
3528            former will be true if the operand has a side-effect.  In that
3529            case, we know the operand occurred exactly once.  */
3530
3531         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3532           arg0 = new0;
3533         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3534           arg0 = new1;
3535
3536         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3537           arg1 = new0;
3538         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3539           arg1 = new1;
3540
3541         return fold_build2 (code, type, arg0, arg1);
3542       }
3543
3544     default:
3545       return arg;
3546     }
3547 }
3548 \f
3549 /* Return a tree for the case when the result of an expression is RESULT
3550    converted to TYPE and OMITTED was previously an operand of the expression
3551    but is now not needed (e.g., we folded OMITTED * 0).
3552
3553    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
3554    the conversion of RESULT to TYPE.  */
3555
3556 tree
3557 omit_one_operand (tree type, tree result, tree omitted)
3558 {
3559   tree t = fold_convert (type, result);
3560
3561   /* If the resulting operand is an empty statement, just return the omitted
3562      statement casted to void. */
3563   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3564     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3565
3566   if (TREE_SIDE_EFFECTS (omitted))
3567     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3568
3569   return non_lvalue (t);
3570 }
3571
3572 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3573
3574 static tree
3575 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3576 {
3577   tree t = fold_convert (type, result);
3578
3579   /* If the resulting operand is an empty statement, just return the omitted
3580      statement casted to void. */
3581   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3582     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3583
3584   if (TREE_SIDE_EFFECTS (omitted))
3585     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3586
3587   return pedantic_non_lvalue (t);
3588 }
3589
3590 /* Return a tree for the case when the result of an expression is RESULT
3591    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3592    of the expression but are now not needed.
3593
3594    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3595    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3596    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3597    just do the conversion of RESULT to TYPE.  */
3598
3599 tree
3600 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3601 {
3602   tree t = fold_convert (type, result);
3603
3604   if (TREE_SIDE_EFFECTS (omitted2))
3605     t = build2 (COMPOUND_EXPR, type, omitted2, t);
3606   if (TREE_SIDE_EFFECTS (omitted1))
3607     t = build2 (COMPOUND_EXPR, type, omitted1, t);
3608
3609   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3610 }
3611
3612 \f
3613 /* Return a simplified tree node for the truth-negation of ARG.  This
3614    never alters ARG itself.  We assume that ARG is an operation that
3615    returns a truth value (0 or 1).
3616
3617    FIXME: one would think we would fold the result, but it causes
3618    problems with the dominator optimizer.  */
3619
3620 tree
3621 fold_truth_not_expr (tree arg)
3622 {
3623   tree type = TREE_TYPE (arg);
3624   enum tree_code code = TREE_CODE (arg);
3625
3626   /* If this is a comparison, we can simply invert it, except for
3627      floating-point non-equality comparisons, in which case we just
3628      enclose a TRUTH_NOT_EXPR around what we have.  */
3629
3630   if (TREE_CODE_CLASS (code) == tcc_comparison)
3631     {
3632       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3633       if (FLOAT_TYPE_P (op_type)
3634           && flag_trapping_math
3635           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3636           && code != NE_EXPR && code != EQ_EXPR)
3637         return NULL_TREE;
3638       else
3639         {
3640           code = invert_tree_comparison (code,
3641                                          HONOR_NANS (TYPE_MODE (op_type)));
3642           if (code == ERROR_MARK)
3643             return NULL_TREE;
3644           else
3645             return build2 (code, type,
3646                            TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3647         }
3648     }
3649
3650   switch (code)
3651     {
3652     case INTEGER_CST:
3653       return constant_boolean_node (integer_zerop (arg), type);
3654
3655     case TRUTH_AND_EXPR:
3656       return build2 (TRUTH_OR_EXPR, type,
3657                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3658                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3659
3660     case TRUTH_OR_EXPR:
3661       return build2 (TRUTH_AND_EXPR, type,
3662                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3663                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3664
3665     case TRUTH_XOR_EXPR:
3666       /* Here we can invert either operand.  We invert the first operand
3667          unless the second operand is a TRUTH_NOT_EXPR in which case our
3668          result is the XOR of the first operand with the inside of the
3669          negation of the second operand.  */
3670
3671       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3672         return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3673                        TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3674       else
3675         return build2 (TRUTH_XOR_EXPR, type,
3676                        invert_truthvalue (TREE_OPERAND (arg, 0)),
3677                        TREE_OPERAND (arg, 1));
3678
3679     case TRUTH_ANDIF_EXPR:
3680       return build2 (TRUTH_ORIF_EXPR, type,
3681                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3682                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3683
3684     case TRUTH_ORIF_EXPR:
3685       return build2 (TRUTH_ANDIF_EXPR, type,
3686                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3687                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3688
3689     case TRUTH_NOT_EXPR:
3690       return TREE_OPERAND (arg, 0);
3691
3692     case COND_EXPR:
3693       {
3694         tree arg1 = TREE_OPERAND (arg, 1);
3695         tree arg2 = TREE_OPERAND (arg, 2);
3696         /* A COND_EXPR may have a throw as one operand, which
3697            then has void type.  Just leave void operands
3698            as they are.  */
3699         return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3700                        VOID_TYPE_P (TREE_TYPE (arg1))
3701                        ? arg1 : invert_truthvalue (arg1),
3702                        VOID_TYPE_P (TREE_TYPE (arg2))
3703                        ? arg2 : invert_truthvalue (arg2));
3704       }
3705
3706     case COMPOUND_EXPR:
3707       return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3708                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3709
3710     case NON_LVALUE_EXPR:
3711       return invert_truthvalue (TREE_OPERAND (arg, 0));
3712
3713     case NOP_EXPR:
3714       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3715         return build1 (TRUTH_NOT_EXPR, type, arg);
3716
3717     case CONVERT_EXPR:
3718     case FLOAT_EXPR:
3719       return build1 (TREE_CODE (arg), type,
3720                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3721
3722     case BIT_AND_EXPR:
3723       if (!integer_onep (TREE_OPERAND (arg, 1)))
3724         break;
3725       return build2 (EQ_EXPR, type, arg,
3726                      build_int_cst (type, 0));
3727
3728     case SAVE_EXPR:
3729       return build1 (TRUTH_NOT_EXPR, type, arg);
3730
3731     case CLEANUP_POINT_EXPR:
3732       return build1 (CLEANUP_POINT_EXPR, type,
3733                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3734
3735     default:
3736       break;
3737     }
3738
3739   return NULL_TREE;
3740 }
3741
3742 /* Return a simplified tree node for the truth-negation of ARG.  This
3743    never alters ARG itself.  We assume that ARG is an operation that
3744    returns a truth value (0 or 1).
3745
3746    FIXME: one would think we would fold the result, but it causes
3747    problems with the dominator optimizer.  */
3748
3749 tree
3750 invert_truthvalue (tree arg)
3751 {
3752   tree tem;
3753
3754   if (TREE_CODE (arg) == ERROR_MARK)
3755     return arg;
3756
3757   tem = fold_truth_not_expr (arg);
3758   if (!tem)
3759     tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3760
3761   return tem;
3762 }
3763
3764 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3765    operands are another bit-wise operation with a common input.  If so,
3766    distribute the bit operations to save an operation and possibly two if
3767    constants are involved.  For example, convert
3768         (A | B) & (A | C) into A | (B & C)
3769    Further simplification will occur if B and C are constants.
3770
3771    If this optimization cannot be done, 0 will be returned.  */
3772
3773 static tree
3774 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3775 {
3776   tree common;
3777   tree left, right;
3778
3779   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3780       || TREE_CODE (arg0) == code
3781       || (TREE_CODE (arg0) != BIT_AND_EXPR
3782           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3783     return 0;
3784
3785   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3786     {
3787       common = TREE_OPERAND (arg0, 0);
3788       left = TREE_OPERAND (arg0, 1);
3789       right = TREE_OPERAND (arg1, 1);
3790     }
3791   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3792     {
3793       common = TREE_OPERAND (arg0, 0);
3794       left = TREE_OPERAND (arg0, 1);
3795       right = TREE_OPERAND (arg1, 0);
3796     }
3797   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3798     {
3799       common = TREE_OPERAND (arg0, 1);
3800       left = TREE_OPERAND (arg0, 0);
3801       right = TREE_OPERAND (arg1, 1);
3802     }
3803   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3804     {
3805       common = TREE_OPERAND (arg0, 1);
3806       left = TREE_OPERAND (arg0, 0);
3807       right = TREE_OPERAND (arg1, 0);
3808     }
3809   else
3810     return 0;
3811
3812   common = fold_convert (type, common);
3813   left = fold_convert (type, left);
3814   right = fold_convert (type, right);
3815   return fold_build2 (TREE_CODE (arg0), type, common,
3816                       fold_build2 (code, type, left, right));
3817 }
3818
3819 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3820    with code CODE.  This optimization is unsafe.  */
3821 static tree
3822 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3823 {
3824   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3825   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3826
3827   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3828   if (mul0 == mul1
3829       && operand_equal_p (TREE_OPERAND (arg0, 1),
3830                        TREE_OPERAND (arg1, 1), 0))
3831     return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3832                         fold_build2 (code, type,
3833                                      TREE_OPERAND (arg0, 0),
3834                                      TREE_OPERAND (arg1, 0)),
3835                         TREE_OPERAND (arg0, 1));
3836
3837   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3838   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3839                        TREE_OPERAND (arg1, 0), 0)
3840       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3841       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3842     {
3843       REAL_VALUE_TYPE r0, r1;
3844       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3845       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3846       if (!mul0)
3847         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3848       if (!mul1)
3849         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3850       real_arithmetic (&r0, code, &r0, &r1);
3851       return fold_build2 (MULT_EXPR, type,
3852                           TREE_OPERAND (arg0, 0),
3853                           build_real (type, r0));
3854     }
3855
3856   return NULL_TREE;
3857 }
3858 \f
3859 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3860    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3861
3862 static tree
3863 make_bit_field_ref (tree inner, tree type, HOST_WIDE_INT bitsize,
3864                     HOST_WIDE_INT bitpos, int unsignedp)
3865 {
3866   tree result, bftype;
3867
3868   if (bitpos == 0)
3869     {
3870       tree size = TYPE_SIZE (TREE_TYPE (inner));
3871       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3872            || POINTER_TYPE_P (TREE_TYPE (inner)))
3873           && host_integerp (size, 0) 
3874           && tree_low_cst (size, 0) == bitsize)
3875         return fold_convert (type, inner);
3876     }
3877
3878   bftype = type;
3879   if (TYPE_PRECISION (bftype) != bitsize
3880       || TYPE_UNSIGNED (bftype) == !unsignedp)
3881     bftype = build_nonstandard_integer_type (bitsize, 0);
3882
3883   result = build3 (BIT_FIELD_REF, bftype, inner,
3884                    size_int (bitsize), bitsize_int (bitpos));
3885
3886   if (bftype != type)
3887     result = fold_convert (type, result);
3888
3889   return result;
3890 }
3891
3892 /* Optimize a bit-field compare.
3893
3894    There are two cases:  First is a compare against a constant and the
3895    second is a comparison of two items where the fields are at the same
3896    bit position relative to the start of a chunk (byte, halfword, word)
3897    large enough to contain it.  In these cases we can avoid the shift
3898    implicit in bitfield extractions.
3899
3900    For constants, we emit a compare of the shifted constant with the
3901    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3902    compared.  For two fields at the same position, we do the ANDs with the
3903    similar mask and compare the result of the ANDs.
3904
3905    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3906    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3907    are the left and right operands of the comparison, respectively.
3908
3909    If the optimization described above can be done, we return the resulting
3910    tree.  Otherwise we return zero.  */
3911
3912 static tree
3913 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3914                             tree lhs, tree rhs)
3915 {
3916   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3917   tree type = TREE_TYPE (lhs);
3918   tree signed_type, unsigned_type;
3919   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3920   enum machine_mode lmode, rmode, nmode;
3921   int lunsignedp, runsignedp;
3922   int lvolatilep = 0, rvolatilep = 0;
3923   tree linner, rinner = NULL_TREE;
3924   tree mask;
3925   tree offset;
3926
3927   /* Get all the information about the extractions being done.  If the bit size
3928      if the same as the size of the underlying object, we aren't doing an
3929      extraction at all and so can do nothing.  We also don't want to
3930      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3931      then will no longer be able to replace it.  */
3932   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3933                                 &lunsignedp, &lvolatilep, false);
3934   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3935       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3936     return 0;
3937
3938  if (!const_p)
3939    {
3940      /* If this is not a constant, we can only do something if bit positions,
3941         sizes, and signedness are the same.  */
3942      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3943                                    &runsignedp, &rvolatilep, false);
3944
3945      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3946          || lunsignedp != runsignedp || offset != 0
3947          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3948        return 0;
3949    }
3950
3951   /* See if we can find a mode to refer to this field.  We should be able to,
3952      but fail if we can't.  */
3953   nmode = get_best_mode (lbitsize, lbitpos,
3954                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3955                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3956                                 TYPE_ALIGN (TREE_TYPE (rinner))),
3957                          word_mode, lvolatilep || rvolatilep);
3958   if (nmode == VOIDmode)
3959     return 0;
3960
3961   /* Set signed and unsigned types of the precision of this mode for the
3962      shifts below.  */
3963   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3964   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3965
3966   /* Compute the bit position and size for the new reference and our offset
3967      within it. If the new reference is the same size as the original, we
3968      won't optimize anything, so return zero.  */
3969   nbitsize = GET_MODE_BITSIZE (nmode);
3970   nbitpos = lbitpos & ~ (nbitsize - 1);
3971   lbitpos -= nbitpos;
3972   if (nbitsize == lbitsize)
3973     return 0;
3974
3975   if (BYTES_BIG_ENDIAN)
3976     lbitpos = nbitsize - lbitsize - lbitpos;
3977
3978   /* Make the mask to be used against the extracted field.  */
3979   mask = build_int_cst_type (unsigned_type, -1);
3980   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3981   mask = const_binop (RSHIFT_EXPR, mask,
3982                       size_int (nbitsize - lbitsize - lbitpos), 0);
3983
3984   if (! const_p)
3985     /* If not comparing with constant, just rework the comparison
3986        and return.  */
3987     return fold_build2 (code, compare_type,
3988                         fold_build2 (BIT_AND_EXPR, unsigned_type,
3989                                      make_bit_field_ref (linner,
3990                                                          unsigned_type,
3991                                                          nbitsize, nbitpos,
3992                                                          1),
3993                                      mask),
3994                         fold_build2 (BIT_AND_EXPR, unsigned_type,
3995                                      make_bit_field_ref (rinner,
3996                                                          unsigned_type,
3997                                                          nbitsize, nbitpos,
3998                                                          1),
3999                                      mask));
4000
4001   /* Otherwise, we are handling the constant case. See if the constant is too
4002      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
4003      this not only for its own sake, but to avoid having to test for this
4004      error case below.  If we didn't, we might generate wrong code.
4005
4006      For unsigned fields, the constant shifted right by the field length should
4007      be all zero.  For signed fields, the high-order bits should agree with
4008      the sign bit.  */
4009
4010   if (lunsignedp)
4011     {
4012       if (! integer_zerop (const_binop (RSHIFT_EXPR,
4013                                         fold_convert (unsigned_type, rhs),
4014                                         size_int (lbitsize), 0)))
4015         {
4016           warning (0, "comparison is always %d due to width of bit-field",
4017                    code == NE_EXPR);
4018           return constant_boolean_node (code == NE_EXPR, compare_type);
4019         }
4020     }
4021   else
4022     {
4023       tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
4024                               size_int (lbitsize - 1), 0);
4025       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
4026         {
4027           warning (0, "comparison is always %d due to width of bit-field",
4028                    code == NE_EXPR);
4029           return constant_boolean_node (code == NE_EXPR, compare_type);
4030         }
4031     }
4032
4033   /* Single-bit compares should always be against zero.  */
4034   if (lbitsize == 1 && ! integer_zerop (rhs))
4035     {
4036       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4037       rhs = build_int_cst (type, 0);
4038     }
4039
4040   /* Make a new bitfield reference, shift the constant over the
4041      appropriate number of bits and mask it with the computed mask
4042      (in case this was a signed field).  If we changed it, make a new one.  */
4043   lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
4044   if (lvolatilep)
4045     {
4046       TREE_SIDE_EFFECTS (lhs) = 1;
4047       TREE_THIS_VOLATILE (lhs) = 1;
4048     }
4049
4050   rhs = const_binop (BIT_AND_EXPR,
4051                      const_binop (LSHIFT_EXPR,
4052                                   fold_convert (unsigned_type, rhs),
4053                                   size_int (lbitpos), 0),
4054                      mask, 0);
4055
4056   return build2 (code, compare_type,
4057                  build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4058                  rhs);
4059 }
4060 \f
4061 /* Subroutine for fold_truthop: decode a field reference.
4062
4063    If EXP is a comparison reference, we return the innermost reference.
4064
4065    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4066    set to the starting bit number.
4067
4068    If the innermost field can be completely contained in a mode-sized
4069    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
4070
4071    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4072    otherwise it is not changed.
4073
4074    *PUNSIGNEDP is set to the signedness of the field.
4075
4076    *PMASK is set to the mask used.  This is either contained in a
4077    BIT_AND_EXPR or derived from the width of the field.
4078
4079    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4080
4081    Return 0 if this is not a component reference or is one that we can't
4082    do anything with.  */
4083
4084 static tree
4085 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
4086                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4087                         int *punsignedp, int *pvolatilep,
4088                         tree *pmask, tree *pand_mask)
4089 {
4090   tree outer_type = 0;
4091   tree and_mask = 0;
4092   tree mask, inner, offset;
4093   tree unsigned_type;
4094   unsigned int precision;
4095
4096   /* All the optimizations using this function assume integer fields.
4097      There are problems with FP fields since the type_for_size call
4098      below can fail for, e.g., XFmode.  */
4099   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4100     return 0;
4101
4102   /* We are interested in the bare arrangement of bits, so strip everything
4103      that doesn't affect the machine mode.  However, record the type of the
4104      outermost expression if it may matter below.  */
4105   if (CONVERT_EXPR_P (exp)
4106       || TREE_CODE (exp) == NON_LVALUE_EXPR)
4107     outer_type = TREE_TYPE (exp);
4108   STRIP_NOPS (exp);
4109
4110   if (TREE_CODE (exp) == BIT_AND_EXPR)
4111     {
4112       and_mask = TREE_OPERAND (exp, 1);
4113       exp = TREE_OPERAND (exp, 0);
4114       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4115       if (TREE_CODE (and_mask) != INTEGER_CST)
4116         return 0;
4117     }
4118
4119   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4120                                punsignedp, pvolatilep, false);
4121   if ((inner == exp && and_mask == 0)
4122       || *pbitsize < 0 || offset != 0
4123       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4124     return 0;
4125
4126   /* If the number of bits in the reference is the same as the bitsize of
4127      the outer type, then the outer type gives the signedness. Otherwise
4128      (in case of a small bitfield) the signedness is unchanged.  */
4129   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4130     *punsignedp = TYPE_UNSIGNED (outer_type);
4131
4132   /* Compute the mask to access the bitfield.  */
4133   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4134   precision = TYPE_PRECISION (unsigned_type);
4135
4136   mask = build_int_cst_type (unsigned_type, -1);
4137
4138   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4139   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4140
4141   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
4142   if (and_mask != 0)
4143     mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
4144                         fold_convert (unsigned_type, and_mask), mask);
4145
4146   *pmask = mask;
4147   *pand_mask = and_mask;
4148   return inner;
4149 }
4150
4151 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4152    bit positions.  */
4153
4154 static int
4155 all_ones_mask_p (const_tree mask, int size)
4156 {
4157   tree type = TREE_TYPE (mask);
4158   unsigned int precision = TYPE_PRECISION (type);
4159   tree tmask;
4160
4161   tmask = build_int_cst_type (signed_type_for (type), -1);
4162
4163   return
4164     tree_int_cst_equal (mask,
4165                         const_binop (RSHIFT_EXPR,
4166                                      const_binop (LSHIFT_EXPR, tmask,
4167                                                   size_int (precision - size),
4168                                                   0),
4169                                      size_int (precision - size), 0));
4170 }
4171
4172 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4173    represents the sign bit of EXP's type.  If EXP represents a sign
4174    or zero extension, also test VAL against the unextended type.
4175    The return value is the (sub)expression whose sign bit is VAL,
4176    or NULL_TREE otherwise.  */
4177
4178 static tree
4179 sign_bit_p (tree exp, const_tree val)
4180 {
4181   unsigned HOST_WIDE_INT mask_lo, lo;
4182   HOST_WIDE_INT mask_hi, hi;
4183   int width;
4184   tree t;
4185
4186   /* Tree EXP must have an integral type.  */
4187   t = TREE_TYPE (exp);
4188   if (! INTEGRAL_TYPE_P (t))
4189     return NULL_TREE;
4190
4191   /* Tree VAL must be an integer constant.  */
4192   if (TREE_CODE (val) != INTEGER_CST
4193       || TREE_OVERFLOW (val))
4194     return NULL_TREE;
4195
4196   width = TYPE_PRECISION (t);
4197   if (width > HOST_BITS_PER_WIDE_INT)
4198     {
4199       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4200       lo = 0;
4201
4202       mask_hi = ((unsigned HOST_WIDE_INT) -1
4203                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
4204       mask_lo = -1;
4205     }
4206   else
4207     {
4208       hi = 0;
4209       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4210
4211       mask_hi = 0;
4212       mask_lo = ((unsigned HOST_WIDE_INT) -1
4213                  >> (HOST_BITS_PER_WIDE_INT - width));
4214     }
4215
4216   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4217      treat VAL as if it were unsigned.  */
4218   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4219       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4220     return exp;
4221
4222   /* Handle extension from a narrower type.  */
4223   if (TREE_CODE (exp) == NOP_EXPR
4224       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4225     return sign_bit_p (TREE_OPERAND (exp, 0), val);
4226
4227   return NULL_TREE;
4228 }
4229
4230 /* Subroutine for fold_truthop: determine if an operand is simple enough
4231    to be evaluated unconditionally.  */
4232
4233 static int
4234 simple_operand_p (const_tree exp)
4235 {
4236   /* Strip any conversions that don't change the machine mode.  */
4237   STRIP_NOPS (exp);
4238
4239   return (CONSTANT_CLASS_P (exp)
4240           || TREE_CODE (exp) == SSA_NAME
4241           || (DECL_P (exp)
4242               && ! TREE_ADDRESSABLE (exp)
4243               && ! TREE_THIS_VOLATILE (exp)
4244               && ! DECL_NONLOCAL (exp)
4245               /* Don't regard global variables as simple.  They may be
4246                  allocated in ways unknown to the compiler (shared memory,
4247                  #pragma weak, etc).  */
4248               && ! TREE_PUBLIC (exp)
4249               && ! DECL_EXTERNAL (exp)
4250               /* Loading a static variable is unduly expensive, but global
4251                  registers aren't expensive.  */
4252               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4253 }
4254 \f
4255 /* The following functions are subroutines to fold_range_test and allow it to
4256    try to change a logical combination of comparisons into a range test.
4257
4258    For example, both
4259         X == 2 || X == 3 || X == 4 || X == 5
4260    and
4261         X >= 2 && X <= 5
4262    are converted to
4263         (unsigned) (X - 2) <= 3
4264
4265    We describe each set of comparisons as being either inside or outside
4266    a range, using a variable named like IN_P, and then describe the
4267    range with a lower and upper bound.  If one of the bounds is omitted,
4268    it represents either the highest or lowest value of the type.
4269
4270    In the comments below, we represent a range by two numbers in brackets
4271    preceded by a "+" to designate being inside that range, or a "-" to
4272    designate being outside that range, so the condition can be inverted by
4273    flipping the prefix.  An omitted bound is represented by a "-".  For
4274    example, "- [-, 10]" means being outside the range starting at the lowest
4275    possible value and ending at 10, in other words, being greater than 10.
4276    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4277    always false.
4278
4279    We set up things so that the missing bounds are handled in a consistent
4280    manner so neither a missing bound nor "true" and "false" need to be
4281    handled using a special case.  */
4282
4283 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4284    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4285    and UPPER1_P are nonzero if the respective argument is an upper bound
4286    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
4287    must be specified for a comparison.  ARG1 will be converted to ARG0's
4288    type if both are specified.  */
4289
4290 static tree
4291 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4292              tree arg1, int upper1_p)
4293 {
4294   tree tem;
4295   int result;
4296   int sgn0, sgn1;
4297
4298   /* If neither arg represents infinity, do the normal operation.
4299      Else, if not a comparison, return infinity.  Else handle the special
4300      comparison rules. Note that most of the cases below won't occur, but
4301      are handled for consistency.  */
4302
4303   if (arg0 != 0 && arg1 != 0)
4304     {
4305       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4306                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
4307       STRIP_NOPS (tem);
4308       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4309     }
4310
4311   if (TREE_CODE_CLASS (code) != tcc_comparison)
4312     return 0;
4313
4314   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4315      for neither.  In real maths, we cannot assume open ended ranges are
4316      the same. But, this is computer arithmetic, where numbers are finite.
4317      We can therefore make the transformation of any unbounded range with
4318      the value Z, Z being greater than any representable number. This permits
4319      us to treat unbounded ranges as equal.  */
4320   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4321   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4322   switch (code)
4323     {
4324     case EQ_EXPR:
4325       result = sgn0 == sgn1;
4326       break;
4327     case NE_EXPR:
4328       result = sgn0 != sgn1;
4329       break;
4330     case LT_EXPR:
4331       result = sgn0 < sgn1;
4332       break;
4333     case LE_EXPR:
4334       result = sgn0 <= sgn1;
4335       break;
4336     case GT_EXPR:
4337       result = sgn0 > sgn1;
4338       break;
4339     case GE_EXPR:
4340       result = sgn0 >= sgn1;
4341       break;
4342     default:
4343       gcc_unreachable ();
4344     }
4345
4346   return constant_boolean_node (result, type);
4347 }
4348 \f
4349 /* Given EXP, a logical expression, set the range it is testing into
4350    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4351    actually being tested.  *PLOW and *PHIGH will be made of the same
4352    type as the returned expression.  If EXP is not a comparison, we
4353    will most likely not be returning a useful value and range.  Set
4354    *STRICT_OVERFLOW_P to true if the return value is only valid
4355    because signed overflow is undefined; otherwise, do not change
4356    *STRICT_OVERFLOW_P.  */
4357
4358 static tree
4359 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4360             bool *strict_overflow_p)
4361 {
4362   enum tree_code code;
4363   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4364   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4365   int in_p, n_in_p;
4366   tree low, high, n_low, n_high;
4367
4368   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4369      and see if we can refine the range.  Some of the cases below may not
4370      happen, but it doesn't seem worth worrying about this.  We "continue"
4371      the outer loop when we've changed something; otherwise we "break"
4372      the switch, which will "break" the while.  */
4373
4374   in_p = 0;
4375   low = high = build_int_cst (TREE_TYPE (exp), 0);
4376
4377   while (1)
4378     {
4379       code = TREE_CODE (exp);
4380       exp_type = TREE_TYPE (exp);
4381
4382       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4383         {
4384           if (TREE_OPERAND_LENGTH (exp) > 0)
4385             arg0 = TREE_OPERAND (exp, 0);
4386           if (TREE_CODE_CLASS (code) == tcc_comparison
4387               || TREE_CODE_CLASS (code) == tcc_unary
4388               || TREE_CODE_CLASS (code) == tcc_binary)
4389             arg0_type = TREE_TYPE (arg0);
4390           if (TREE_CODE_CLASS (code) == tcc_binary
4391               || TREE_CODE_CLASS (code) == tcc_comparison
4392               || (TREE_CODE_CLASS (code) == tcc_expression
4393                   && TREE_OPERAND_LENGTH (exp) > 1))
4394             arg1 = TREE_OPERAND (exp, 1);
4395         }
4396
4397       switch (code)
4398         {
4399         case TRUTH_NOT_EXPR:
4400           in_p = ! in_p, exp = arg0;
4401           continue;
4402
4403         case EQ_EXPR: case NE_EXPR:
4404         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4405           /* We can only do something if the range is testing for zero
4406              and if the second operand is an integer constant.  Note that
4407              saying something is "in" the range we make is done by
4408              complementing IN_P since it will set in the initial case of
4409              being not equal to zero; "out" is leaving it alone.  */
4410           if (low == 0 || high == 0
4411               || ! integer_zerop (low) || ! integer_zerop (high)
4412               || TREE_CODE (arg1) != INTEGER_CST)
4413             break;
4414
4415           switch (code)
4416             {
4417             case NE_EXPR:  /* - [c, c]  */
4418               low = high = arg1;
4419               break;
4420             case EQ_EXPR:  /* + [c, c]  */
4421               in_p = ! in_p, low = high = arg1;
4422               break;
4423             case GT_EXPR:  /* - [-, c] */
4424               low = 0, high = arg1;
4425               break;
4426             case GE_EXPR:  /* + [c, -] */
4427               in_p = ! in_p, low = arg1, high = 0;
4428               break;
4429             case LT_EXPR:  /* - [c, -] */
4430               low = arg1, high = 0;
4431               break;
4432             case LE_EXPR:  /* + [-, c] */
4433               in_p = ! in_p, low = 0, high = arg1;
4434               break;
4435             default:
4436               gcc_unreachable ();
4437             }
4438
4439           /* If this is an unsigned comparison, we also know that EXP is
4440              greater than or equal to zero.  We base the range tests we make
4441              on that fact, so we record it here so we can parse existing
4442              range tests.  We test arg0_type since often the return type
4443              of, e.g. EQ_EXPR, is boolean.  */
4444           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4445             {
4446               if (! merge_ranges (&n_in_p, &n_low, &n_high,
4447                                   in_p, low, high, 1,
4448                                   build_int_cst (arg0_type, 0),
4449                                   NULL_TREE))
4450                 break;
4451
4452               in_p = n_in_p, low = n_low, high = n_high;
4453
4454               /* If the high bound is missing, but we have a nonzero low
4455                  bound, reverse the range so it goes from zero to the low bound
4456                  minus 1.  */
4457               if (high == 0 && low && ! integer_zerop (low))
4458                 {
4459                   in_p = ! in_p;
4460                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4461                                       integer_one_node, 0);
4462                   low = build_int_cst (arg0_type, 0);
4463                 }
4464             }
4465
4466           exp = arg0;
4467           continue;
4468
4469         case NEGATE_EXPR:
4470           /* (-x) IN [a,b] -> x in [-b, -a]  */
4471           n_low = range_binop (MINUS_EXPR, exp_type,
4472                                build_int_cst (exp_type, 0),
4473                                0, high, 1);
4474           n_high = range_binop (MINUS_EXPR, exp_type,
4475                                 build_int_cst (exp_type, 0),
4476                                 0, low, 0);
4477           low = n_low, high = n_high;
4478           exp = arg0;
4479           continue;
4480
4481         case BIT_NOT_EXPR:
4482           /* ~ X -> -X - 1  */
4483           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4484                         build_int_cst (exp_type, 1));
4485           continue;
4486
4487         case PLUS_EXPR:  case MINUS_EXPR:
4488           if (TREE_CODE (arg1) != INTEGER_CST)
4489             break;
4490
4491           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4492              move a constant to the other side.  */
4493           if (!TYPE_UNSIGNED (arg0_type)
4494               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4495             break;
4496
4497           /* If EXP is signed, any overflow in the computation is undefined,
4498              so we don't worry about it so long as our computations on
4499              the bounds don't overflow.  For unsigned, overflow is defined
4500              and this is exactly the right thing.  */
4501           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4502                                arg0_type, low, 0, arg1, 0);
4503           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4504                                 arg0_type, high, 1, arg1, 0);
4505           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4506               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4507             break;
4508
4509           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4510             *strict_overflow_p = true;
4511
4512           /* Check for an unsigned range which has wrapped around the maximum
4513              value thus making n_high < n_low, and normalize it.  */
4514           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4515             {
4516               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4517                                  integer_one_node, 0);
4518               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4519                                   integer_one_node, 0);
4520
4521               /* If the range is of the form +/- [ x+1, x ], we won't
4522                  be able to normalize it.  But then, it represents the
4523                  whole range or the empty set, so make it
4524                  +/- [ -, - ].  */
4525               if (tree_int_cst_equal (n_low, low)
4526                   && tree_int_cst_equal (n_high, high))
4527                 low = high = 0;
4528               else
4529                 in_p = ! in_p;
4530             }
4531           else
4532             low = n_low, high = n_high;
4533
4534           exp = arg0;
4535           continue;
4536
4537         CASE_CONVERT: case NON_LVALUE_EXPR:
4538           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4539             break;
4540
4541           if (! INTEGRAL_TYPE_P (arg0_type)
4542               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4543               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4544             break;
4545
4546           n_low = low, n_high = high;
4547
4548           if (n_low != 0)
4549             n_low = fold_convert (arg0_type, n_low);
4550
4551           if (n_high != 0)
4552             n_high = fold_convert (arg0_type, n_high);
4553
4554
4555           /* If we're converting arg0 from an unsigned type, to exp,
4556              a signed type,  we will be doing the comparison as unsigned.
4557              The tests above have already verified that LOW and HIGH
4558              are both positive.
4559
4560              So we have to ensure that we will handle large unsigned
4561              values the same way that the current signed bounds treat
4562              negative values.  */
4563
4564           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4565             {
4566               tree high_positive;
4567               tree equiv_type;
4568               /* For fixed-point modes, we need to pass the saturating flag
4569                  as the 2nd parameter.  */
4570               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4571                 equiv_type = lang_hooks.types.type_for_mode
4572                              (TYPE_MODE (arg0_type),
4573                               TYPE_SATURATING (arg0_type));
4574               else
4575                 equiv_type = lang_hooks.types.type_for_mode
4576                              (TYPE_MODE (arg0_type), 1);
4577
4578               /* A range without an upper bound is, naturally, unbounded.
4579                  Since convert would have cropped a very large value, use
4580                  the max value for the destination type.  */
4581               high_positive
4582                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4583                 : TYPE_MAX_VALUE (arg0_type);
4584
4585               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4586                 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4587                                              fold_convert (arg0_type,
4588                                                            high_positive),
4589                                              build_int_cst (arg0_type, 1));
4590
4591               /* If the low bound is specified, "and" the range with the
4592                  range for which the original unsigned value will be
4593                  positive.  */
4594               if (low != 0)
4595                 {
4596                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4597                                       1, n_low, n_high, 1,
4598                                       fold_convert (arg0_type,
4599                                                     integer_zero_node),
4600                                       high_positive))
4601                     break;
4602
4603                   in_p = (n_in_p == in_p);
4604                 }
4605               else
4606                 {
4607                   /* Otherwise, "or" the range with the range of the input
4608                      that will be interpreted as negative.  */
4609                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4610                                       0, n_low, n_high, 1,
4611                                       fold_convert (arg0_type,
4612                                                     integer_zero_node),
4613                                       high_positive))
4614                     break;
4615
4616                   in_p = (in_p != n_in_p);
4617                 }
4618             }
4619
4620           exp = arg0;
4621           low = n_low, high = n_high;
4622           continue;
4623
4624         default:
4625           break;
4626         }
4627
4628       break;
4629     }
4630
4631   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4632   if (TREE_CODE (exp) == INTEGER_CST)
4633     {
4634       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4635                                                  exp, 0, low, 0))
4636                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4637                                                     exp, 1, high, 1)));
4638       low = high = 0;
4639       exp = 0;
4640     }
4641
4642   *pin_p = in_p, *plow = low, *phigh = high;
4643   return exp;
4644 }
4645 \f
4646 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4647    type, TYPE, return an expression to test if EXP is in (or out of, depending
4648    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4649
4650 static tree
4651 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4652 {
4653   tree etype = TREE_TYPE (exp);
4654   tree value;
4655
4656 #ifdef HAVE_canonicalize_funcptr_for_compare
4657   /* Disable this optimization for function pointer expressions
4658      on targets that require function pointer canonicalization.  */
4659   if (HAVE_canonicalize_funcptr_for_compare
4660       && TREE_CODE (etype) == POINTER_TYPE
4661       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4662     return NULL_TREE;
4663 #endif
4664
4665   if (! in_p)
4666     {
4667       value = build_range_check (type, exp, 1, low, high);
4668       if (value != 0)
4669         return invert_truthvalue (value);
4670
4671       return 0;
4672     }
4673
4674   if (low == 0 && high == 0)
4675     return build_int_cst (type, 1);
4676
4677   if (low == 0)
4678     return fold_build2 (LE_EXPR, type, exp,
4679                         fold_convert (etype, high));
4680
4681   if (high == 0)
4682     return fold_build2 (GE_EXPR, type, exp,
4683                         fold_convert (etype, low));
4684
4685   if (operand_equal_p (low, high, 0))
4686     return fold_build2 (EQ_EXPR, type, exp,
4687                         fold_convert (etype, low));
4688
4689   if (integer_zerop (low))
4690     {
4691       if (! TYPE_UNSIGNED (etype))
4692         {
4693           etype = unsigned_type_for (etype);
4694           high = fold_convert (etype, high);
4695           exp = fold_convert (etype, exp);
4696         }
4697       return build_range_check (type, exp, 1, 0, high);
4698     }
4699
4700   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4701   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4702     {
4703       unsigned HOST_WIDE_INT lo;
4704       HOST_WIDE_INT hi;
4705       int prec;
4706
4707       prec = TYPE_PRECISION (etype);
4708       if (prec <= HOST_BITS_PER_WIDE_INT)
4709         {
4710           hi = 0;
4711           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4712         }
4713       else
4714         {
4715           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4716           lo = (unsigned HOST_WIDE_INT) -1;
4717         }
4718
4719       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4720         {
4721           if (TYPE_UNSIGNED (etype))
4722             {
4723               tree signed_etype = signed_type_for (etype);
4724               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4725                 etype
4726                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4727               else
4728                 etype = signed_etype;
4729               exp = fold_convert (etype, exp);
4730             }
4731           return fold_build2 (GT_EXPR, type, exp,
4732                               build_int_cst (etype, 0));
4733         }
4734     }
4735
4736   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4737      This requires wrap-around arithmetics for the type of the expression.  */
4738   switch (TREE_CODE (etype))
4739     {
4740     case INTEGER_TYPE:
4741       /* There is no requirement that LOW be within the range of ETYPE
4742          if the latter is a subtype.  It must, however, be within the base
4743          type of ETYPE.  So be sure we do the subtraction in that type.  */
4744       if (TREE_TYPE (etype))
4745         etype = TREE_TYPE (etype);
4746       break;
4747
4748     case ENUMERAL_TYPE:
4749     case BOOLEAN_TYPE:
4750       etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4751                                               TYPE_UNSIGNED (etype));
4752       break;
4753
4754     default:
4755       break;
4756     }
4757
4758   /* If we don't have wrap-around arithmetics upfront, try to force it.  */
4759   if (TREE_CODE (etype) == INTEGER_TYPE
4760       && !TYPE_OVERFLOW_WRAPS (etype))
4761     {
4762       tree utype, minv, maxv;
4763
4764       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4765          for the type in question, as we rely on this here.  */
4766       utype = unsigned_type_for (etype);
4767       maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4768       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4769                           integer_one_node, 1);
4770       minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4771
4772       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4773                                       minv, 1, maxv, 1)))
4774         etype = utype;
4775       else
4776         return 0;
4777     }
4778
4779   high = fold_convert (etype, high);
4780   low = fold_convert (etype, low);
4781   exp = fold_convert (etype, exp);
4782
4783   value = const_binop (MINUS_EXPR, high, low, 0);
4784
4785
4786   if (POINTER_TYPE_P (etype))
4787     {
4788       if (value != 0 && !TREE_OVERFLOW (value))
4789         {
4790           low = fold_convert (sizetype, low);
4791           low = fold_build1 (NEGATE_EXPR, sizetype, low);
4792           return build_range_check (type,
4793                                     fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4794                                     1, build_int_cst (etype, 0), value);
4795         }
4796       return 0;
4797     }
4798
4799   if (value != 0 && !TREE_OVERFLOW (value))
4800     return build_range_check (type,
4801                               fold_build2 (MINUS_EXPR, etype, exp, low),
4802                               1, build_int_cst (etype, 0), value);
4803
4804   return 0;
4805 }
4806 \f
4807 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4808
4809 static tree
4810 range_predecessor (tree val)
4811 {
4812   tree type = TREE_TYPE (val);
4813
4814   if (INTEGRAL_TYPE_P (type)
4815       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4816     return 0;
4817   else
4818     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4819 }
4820
4821 /* Return the successor of VAL in its type, handling the infinite case.  */
4822
4823 static tree
4824 range_successor (tree val)
4825 {
4826   tree type = TREE_TYPE (val);
4827
4828   if (INTEGRAL_TYPE_P (type)
4829       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4830     return 0;
4831   else
4832     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4833 }
4834
4835 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4836    can, 0 if we can't.  Set the output range into the specified parameters.  */
4837
4838 static int
4839 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4840               tree high0, int in1_p, tree low1, tree high1)
4841 {
4842   int no_overlap;
4843   int subset;
4844   int temp;
4845   tree tem;
4846   int in_p;
4847   tree low, high;
4848   int lowequal = ((low0 == 0 && low1 == 0)
4849                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4850                                                 low0, 0, low1, 0)));
4851   int highequal = ((high0 == 0 && high1 == 0)
4852                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4853                                                  high0, 1, high1, 1)));
4854
4855   /* Make range 0 be the range that starts first, or ends last if they
4856      start at the same value.  Swap them if it isn't.  */
4857   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4858                                  low0, 0, low1, 0))
4859       || (lowequal
4860           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4861                                         high1, 1, high0, 1))))
4862     {
4863       temp = in0_p, in0_p = in1_p, in1_p = temp;
4864       tem = low0, low0 = low1, low1 = tem;
4865       tem = high0, high0 = high1, high1 = tem;
4866     }
4867
4868   /* Now flag two cases, whether the ranges are disjoint or whether the
4869      second range is totally subsumed in the first.  Note that the tests
4870      below are simplified by the ones above.  */
4871   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4872                                           high0, 1, low1, 0));
4873   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4874                                       high1, 1, high0, 1));
4875
4876   /* We now have four cases, depending on whether we are including or
4877      excluding the two ranges.  */
4878   if (in0_p && in1_p)
4879     {
4880       /* If they don't overlap, the result is false.  If the second range
4881          is a subset it is the result.  Otherwise, the range is from the start
4882          of the second to the end of the first.  */
4883       if (no_overlap)
4884         in_p = 0, low = high = 0;
4885       else if (subset)
4886         in_p = 1, low = low1, high = high1;
4887       else
4888         in_p = 1, low = low1, high = high0;
4889     }
4890
4891   else if (in0_p && ! in1_p)
4892     {
4893       /* If they don't overlap, the result is the first range.  If they are
4894          equal, the result is false.  If the second range is a subset of the
4895          first, and the ranges begin at the same place, we go from just after
4896          the end of the second range to the end of the first.  If the second
4897          range is not a subset of the first, or if it is a subset and both
4898          ranges end at the same place, the range starts at the start of the
4899          first range and ends just before the second range.
4900          Otherwise, we can't describe this as a single range.  */
4901       if (no_overlap)
4902         in_p = 1, low = low0, high = high0;
4903       else if (lowequal && highequal)
4904         in_p = 0, low = high = 0;
4905       else if (subset && lowequal)
4906         {
4907           low = range_successor (high1);
4908           high = high0;
4909           in_p = 1;
4910           if (low == 0)
4911             {
4912               /* We are in the weird situation where high0 > high1 but
4913                  high1 has no successor.  Punt.  */
4914               return 0;
4915             }
4916         }
4917       else if (! subset || highequal)
4918         {
4919           low = low0;
4920           high = range_predecessor (low1);
4921           in_p = 1;
4922           if (high == 0)
4923             {
4924               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4925               return 0;
4926             }
4927         }
4928       else
4929         return 0;
4930     }
4931
4932   else if (! in0_p && in1_p)
4933     {
4934       /* If they don't overlap, the result is the second range.  If the second
4935          is a subset of the first, the result is false.  Otherwise,
4936          the range starts just after the first range and ends at the
4937          end of the second.  */
4938       if (no_overlap)
4939         in_p = 1, low = low1, high = high1;
4940       else if (subset || highequal)
4941         in_p = 0, low = high = 0;
4942       else
4943         {
4944           low = range_successor (high0);
4945           high = high1;
4946           in_p = 1;
4947           if (low == 0)
4948             {
4949               /* high1 > high0 but high0 has no successor.  Punt.  */
4950               return 0;
4951             }
4952         }
4953     }
4954
4955   else
4956     {
4957       /* The case where we are excluding both ranges.  Here the complex case
4958          is if they don't overlap.  In that case, the only time we have a
4959          range is if they are adjacent.  If the second is a subset of the
4960          first, the result is the first.  Otherwise, the range to exclude
4961          starts at the beginning of the first range and ends at the end of the
4962          second.  */
4963       if (no_overlap)
4964         {
4965           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4966                                          range_successor (high0),
4967                                          1, low1, 0)))
4968             in_p = 0, low = low0, high = high1;
4969           else
4970             {
4971               /* Canonicalize - [min, x] into - [-, x].  */
4972               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4973                 switch (TREE_CODE (TREE_TYPE (low0)))
4974                   {
4975                   case ENUMERAL_TYPE:
4976                     if (TYPE_PRECISION (TREE_TYPE (low0))
4977                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4978                       break;
4979                     /* FALLTHROUGH */
4980                   case INTEGER_TYPE:
4981                     if (tree_int_cst_equal (low0,
4982                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4983                       low0 = 0;
4984                     break;
4985                   case POINTER_TYPE:
4986                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4987                         && integer_zerop (low0))
4988                       low0 = 0;
4989                     break;
4990                   default:
4991                     break;
4992                   }
4993
4994               /* Canonicalize - [x, max] into - [x, -].  */
4995               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4996                 switch (TREE_CODE (TREE_TYPE (high1)))
4997                   {
4998                   case ENUMERAL_TYPE:
4999                     if (TYPE_PRECISION (TREE_TYPE (high1))
5000                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5001                       break;
5002                     /* FALLTHROUGH */
5003                   case INTEGER_TYPE:
5004                     if (tree_int_cst_equal (high1,
5005                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
5006                       high1 = 0;
5007                     break;
5008                   case POINTER_TYPE:
5009                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
5010                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5011                                                        high1, 1,
5012                                                        integer_one_node, 1)))
5013                       high1 = 0;
5014                     break;
5015                   default:
5016                     break;
5017                   }
5018
5019               /* The ranges might be also adjacent between the maximum and
5020                  minimum values of the given type.  For
5021                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5022                  return + [x + 1, y - 1].  */
5023               if (low0 == 0 && high1 == 0)
5024                 {
5025                   low = range_successor (high0);
5026                   high = range_predecessor (low1);
5027                   if (low == 0 || high == 0)
5028                     return 0;
5029
5030                   in_p = 1;
5031                 }
5032               else
5033                 return 0;
5034             }
5035         }
5036       else if (subset)
5037         in_p = 0, low = low0, high = high0;
5038       else
5039         in_p = 0, low = low0, high = high1;
5040     }
5041
5042   *pin_p = in_p, *plow = low, *phigh = high;
5043   return 1;
5044 }
5045 \f
5046
5047 /* Subroutine of fold, looking inside expressions of the form
5048    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5049    of the COND_EXPR.  This function is being used also to optimize
5050    A op B ? C : A, by reversing the comparison first.
5051
5052    Return a folded expression whose code is not a COND_EXPR
5053    anymore, or NULL_TREE if no folding opportunity is found.  */
5054
5055 static tree
5056 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
5057 {
5058   enum tree_code comp_code = TREE_CODE (arg0);
5059   tree arg00 = TREE_OPERAND (arg0, 0);
5060   tree arg01 = TREE_OPERAND (arg0, 1);
5061   tree arg1_type = TREE_TYPE (arg1);
5062   tree tem;
5063
5064   STRIP_NOPS (arg1);
5065   STRIP_NOPS (arg2);
5066
5067   /* If we have A op 0 ? A : -A, consider applying the following
5068      transformations:
5069
5070      A == 0? A : -A    same as -A
5071      A != 0? A : -A    same as A
5072      A >= 0? A : -A    same as abs (A)
5073      A > 0?  A : -A    same as abs (A)
5074      A <= 0? A : -A    same as -abs (A)
5075      A < 0?  A : -A    same as -abs (A)
5076
5077      None of these transformations work for modes with signed
5078      zeros.  If A is +/-0, the first two transformations will
5079      change the sign of the result (from +0 to -0, or vice
5080      versa).  The last four will fix the sign of the result,
5081      even though the original expressions could be positive or
5082      negative, depending on the sign of A.
5083
5084      Note that all these transformations are correct if A is
5085      NaN, since the two alternatives (A and -A) are also NaNs.  */
5086   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5087       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5088           ? real_zerop (arg01)
5089           : integer_zerop (arg01))
5090       && ((TREE_CODE (arg2) == NEGATE_EXPR
5091            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5092              /* In the case that A is of the form X-Y, '-A' (arg2) may
5093                 have already been folded to Y-X, check for that. */
5094           || (TREE_CODE (arg1) == MINUS_EXPR
5095               && TREE_CODE (arg2) == MINUS_EXPR
5096               && operand_equal_p (TREE_OPERAND (arg1, 0),
5097                                   TREE_OPERAND (arg2, 1), 0)
5098               && operand_equal_p (TREE_OPERAND (arg1, 1),
5099                                   TREE_OPERAND (arg2, 0), 0))))
5100     switch (comp_code)
5101       {
5102       case EQ_EXPR:
5103       case UNEQ_EXPR:
5104         tem = fold_convert (arg1_type, arg1);
5105         return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
5106       case NE_EXPR:
5107       case LTGT_EXPR:
5108         return pedantic_non_lvalue (fold_convert (type, arg1));
5109       case UNGE_EXPR:
5110       case UNGT_EXPR:
5111         if (flag_trapping_math)
5112           break;
5113         /* Fall through.  */
5114       case GE_EXPR:
5115       case GT_EXPR:
5116         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5117           arg1 = fold_convert (signed_type_for
5118                                (TREE_TYPE (arg1)), arg1);
5119         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5120         return pedantic_non_lvalue (fold_convert (type, tem));
5121       case UNLE_EXPR:
5122       case UNLT_EXPR:
5123         if (flag_trapping_math)
5124           break;
5125       case LE_EXPR:
5126       case LT_EXPR:
5127         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5128           arg1 = fold_convert (signed_type_for
5129                                (TREE_TYPE (arg1)), arg1);
5130         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5131         return negate_expr (fold_convert (type, tem));
5132       default:
5133         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5134         break;
5135       }
5136
5137   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
5138      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
5139      both transformations are correct when A is NaN: A != 0
5140      is then true, and A == 0 is false.  */
5141
5142   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5143       && integer_zerop (arg01) && integer_zerop (arg2))
5144     {
5145       if (comp_code == NE_EXPR)
5146         return pedantic_non_lvalue (fold_convert (type, arg1));
5147       else if (comp_code == EQ_EXPR)
5148         return build_int_cst (type, 0);
5149     }
5150
5151   /* Try some transformations of A op B ? A : B.
5152
5153      A == B? A : B    same as B
5154      A != B? A : B    same as A
5155      A >= B? A : B    same as max (A, B)
5156      A > B?  A : B    same as max (B, A)
5157      A <= B? A : B    same as min (A, B)
5158      A < B?  A : B    same as min (B, A)
5159
5160      As above, these transformations don't work in the presence
5161      of signed zeros.  For example, if A and B are zeros of
5162      opposite sign, the first two transformations will change
5163      the sign of the result.  In the last four, the original
5164      expressions give different results for (A=+0, B=-0) and
5165      (A=-0, B=+0), but the transformed expressions do not.
5166
5167      The first two transformations are correct if either A or B
5168      is a NaN.  In the first transformation, the condition will
5169      be false, and B will indeed be chosen.  In the case of the
5170      second transformation, the condition A != B will be true,
5171      and A will be chosen.
5172
5173      The conversions to max() and min() are not correct if B is
5174      a number and A is not.  The conditions in the original
5175      expressions will be false, so all four give B.  The min()
5176      and max() versions would give a NaN instead.  */
5177   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5178       && operand_equal_for_comparison_p (arg01, arg2, arg00)
5179       /* Avoid these transformations if the COND_EXPR may be used
5180          as an lvalue in the C++ front-end.  PR c++/19199.  */
5181       && (in_gimple_form
5182           || (strcmp (lang_hooks.name, "GNU C++") != 0
5183               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5184           || ! maybe_lvalue_p (arg1)
5185           || ! maybe_lvalue_p (arg2)))
5186     {
5187       tree comp_op0 = arg00;
5188       tree comp_op1 = arg01;
5189       tree comp_type = TREE_TYPE (comp_op0);
5190
5191       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
5192       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5193         {
5194           comp_type = type;
5195           comp_op0 = arg1;
5196           comp_op1 = arg2;
5197         }
5198
5199       switch (comp_code)
5200         {
5201         case EQ_EXPR:
5202           return pedantic_non_lvalue (fold_convert (type, arg2));
5203         case NE_EXPR:
5204           return pedantic_non_lvalue (fold_convert (type, arg1));
5205         case LE_EXPR:
5206         case LT_EXPR:
5207         case UNLE_EXPR:
5208         case UNLT_EXPR:
5209           /* In C++ a ?: expression can be an lvalue, so put the
5210              operand which will be used if they are equal first
5211              so that we can convert this back to the
5212              corresponding COND_EXPR.  */
5213           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5214             {
5215               comp_op0 = fold_convert (comp_type, comp_op0);
5216               comp_op1 = fold_convert (comp_type, comp_op1);
5217               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5218                     ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
5219                     : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
5220               return pedantic_non_lvalue (fold_convert (type, tem));
5221             }
5222           break;
5223         case GE_EXPR:
5224         case GT_EXPR:
5225         case UNGE_EXPR:
5226         case UNGT_EXPR:
5227           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5228             {
5229               comp_op0 = fold_convert (comp_type, comp_op0);
5230               comp_op1 = fold_convert (comp_type, comp_op1);
5231               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5232                     ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5233                     : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5234               return pedantic_non_lvalue (fold_convert (type, tem));
5235             }
5236           break;
5237         case UNEQ_EXPR:
5238           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5239             return pedantic_non_lvalue (fold_convert (type, arg2));
5240           break;
5241         case LTGT_EXPR:
5242           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5243             return pedantic_non_lvalue (fold_convert (type, arg1));
5244           break;
5245         default:
5246           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5247           break;
5248         }
5249     }
5250
5251   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5252      we might still be able to simplify this.  For example,
5253      if C1 is one less or one more than C2, this might have started
5254      out as a MIN or MAX and been transformed by this function.
5255      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
5256
5257   if (INTEGRAL_TYPE_P (type)
5258       && TREE_CODE (arg01) == INTEGER_CST
5259       && TREE_CODE (arg2) == INTEGER_CST)
5260     switch (comp_code)
5261       {
5262       case EQ_EXPR:
5263         /* We can replace A with C1 in this case.  */
5264         arg1 = fold_convert (type, arg01);
5265         return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5266
5267       case LT_EXPR:
5268         /* If C1 is C2 + 1, this is min(A, C2).  */
5269         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5270                                OEP_ONLY_CONST)
5271             && operand_equal_p (arg01,
5272                                 const_binop (PLUS_EXPR, arg2,
5273                                              build_int_cst (type, 1), 0),
5274                                 OEP_ONLY_CONST))
5275           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5276                                                    type,
5277                                                    fold_convert (type, arg1),
5278                                                    arg2));
5279         break;
5280
5281       case LE_EXPR:
5282         /* If C1 is C2 - 1, this is min(A, C2).  */
5283         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5284                                OEP_ONLY_CONST)
5285             && operand_equal_p (arg01,
5286                                 const_binop (MINUS_EXPR, arg2,
5287                                              build_int_cst (type, 1), 0),
5288                                 OEP_ONLY_CONST))
5289           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5290                                                    type,
5291                                                    fold_convert (type, arg1),
5292                                                    arg2));
5293         break;
5294
5295       case GT_EXPR:
5296         /* If C1 is C2 - 1, this is max(A, C2).  */
5297         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5298                                OEP_ONLY_CONST)
5299             && operand_equal_p (arg01,
5300                                 const_binop (MINUS_EXPR, arg2,
5301                                              build_int_cst (type, 1), 0),
5302                                 OEP_ONLY_CONST))
5303           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5304                                                    type,
5305                                                    fold_convert (type, arg1),
5306                                                    arg2));
5307         break;
5308
5309       case GE_EXPR:
5310         /* If C1 is C2 + 1, this is max(A, C2).  */
5311         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5312                                OEP_ONLY_CONST)
5313             && operand_equal_p (arg01,
5314                                 const_binop (PLUS_EXPR, arg2,
5315                                              build_int_cst (type, 1), 0),
5316                                 OEP_ONLY_CONST))
5317           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5318                                                    type,
5319                                                    fold_convert (type, arg1),
5320                                                    arg2));
5321         break;
5322       case NE_EXPR:
5323         break;
5324       default:
5325         gcc_unreachable ();
5326       }
5327
5328   return NULL_TREE;
5329 }
5330
5331
5332 \f
5333 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5334 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5335   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5336                 false) >= 2)
5337 #endif
5338
5339 /* EXP is some logical combination of boolean tests.  See if we can
5340    merge it into some range test.  Return the new tree if so.  */
5341
5342 static tree
5343 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5344 {
5345   int or_op = (code == TRUTH_ORIF_EXPR
5346                || code == TRUTH_OR_EXPR);
5347   int in0_p, in1_p, in_p;
5348   tree low0, low1, low, high0, high1, high;
5349   bool strict_overflow_p = false;
5350   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5351   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5352   tree tem;
5353   const char * const warnmsg = G_("assuming signed overflow does not occur "
5354                                   "when simplifying range test");
5355
5356   /* If this is an OR operation, invert both sides; we will invert
5357      again at the end.  */
5358   if (or_op)
5359     in0_p = ! in0_p, in1_p = ! in1_p;
5360
5361   /* If both expressions are the same, if we can merge the ranges, and we
5362      can build the range test, return it or it inverted.  If one of the
5363      ranges is always true or always false, consider it to be the same
5364      expression as the other.  */
5365   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5366       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5367                        in1_p, low1, high1)
5368       && 0 != (tem = (build_range_check (type,
5369                                          lhs != 0 ? lhs
5370                                          : rhs != 0 ? rhs : integer_zero_node,
5371                                          in_p, low, high))))
5372     {
5373       if (strict_overflow_p)
5374         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5375       return or_op ? invert_truthvalue (tem) : tem;
5376     }
5377
5378   /* On machines where the branch cost is expensive, if this is a
5379      short-circuited branch and the underlying object on both sides
5380      is the same, make a non-short-circuit operation.  */
5381   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5382            && lhs != 0 && rhs != 0
5383            && (code == TRUTH_ANDIF_EXPR
5384                || code == TRUTH_ORIF_EXPR)
5385            && operand_equal_p (lhs, rhs, 0))
5386     {
5387       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
5388          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5389          which cases we can't do this.  */
5390       if (simple_operand_p (lhs))
5391         return build2 (code == TRUTH_ANDIF_EXPR
5392                        ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5393                        type, op0, op1);
5394
5395       else if (lang_hooks.decls.global_bindings_p () == 0
5396                && ! CONTAINS_PLACEHOLDER_P (lhs))
5397         {
5398           tree common = save_expr (lhs);
5399
5400           if (0 != (lhs = build_range_check (type, common,
5401                                              or_op ? ! in0_p : in0_p,
5402                                              low0, high0))
5403               && (0 != (rhs = build_range_check (type, common,
5404                                                  or_op ? ! in1_p : in1_p,
5405                                                  low1, high1))))
5406             {
5407               if (strict_overflow_p)
5408                 fold_overflow_warning (warnmsg,
5409                                        WARN_STRICT_OVERFLOW_COMPARISON);
5410               return build2 (code == TRUTH_ANDIF_EXPR
5411                              ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5412                              type, lhs, rhs);
5413             }
5414         }
5415     }
5416
5417   return 0;
5418 }
5419 \f
5420 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5421    bit value.  Arrange things so the extra bits will be set to zero if and
5422    only if C is signed-extended to its full width.  If MASK is nonzero,
5423    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
5424
5425 static tree
5426 unextend (tree c, int p, int unsignedp, tree mask)
5427 {
5428   tree type = TREE_TYPE (c);
5429   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5430   tree temp;
5431
5432   if (p == modesize || unsignedp)
5433     return c;
5434
5435   /* We work by getting just the sign bit into the low-order bit, then
5436      into the high-order bit, then sign-extend.  We then XOR that value
5437      with C.  */
5438   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5439   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5440
5441   /* We must use a signed type in order to get an arithmetic right shift.
5442      However, we must also avoid introducing accidental overflows, so that
5443      a subsequent call to integer_zerop will work.  Hence we must
5444      do the type conversion here.  At this point, the constant is either
5445      zero or one, and the conversion to a signed type can never overflow.
5446      We could get an overflow if this conversion is done anywhere else.  */
5447   if (TYPE_UNSIGNED (type))
5448     temp = fold_convert (signed_type_for (type), temp);
5449
5450   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5451   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5452   if (mask != 0)
5453     temp = const_binop (BIT_AND_EXPR, temp,
5454                         fold_convert (TREE_TYPE (c), mask), 0);
5455   /* If necessary, convert the type back to match the type of C.  */
5456   if (TYPE_UNSIGNED (type))
5457     temp = fold_convert (type, temp);
5458
5459   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5460 }
5461 \f
5462 /* Find ways of folding logical expressions of LHS and RHS:
5463    Try to merge two comparisons to the same innermost item.
5464    Look for range tests like "ch >= '0' && ch <= '9'".
5465    Look for combinations of simple terms on machines with expensive branches
5466    and evaluate the RHS unconditionally.
5467
5468    For example, if we have p->a == 2 && p->b == 4 and we can make an
5469    object large enough to span both A and B, we can do this with a comparison
5470    against the object ANDed with the a mask.
5471
5472    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5473    operations to do this with one comparison.
5474
5475    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5476    function and the one above.
5477
5478    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5479    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5480
5481    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5482    two operands.
5483
5484    We return the simplified tree or 0 if no optimization is possible.  */
5485
5486 static tree
5487 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5488 {
5489   /* If this is the "or" of two comparisons, we can do something if
5490      the comparisons are NE_EXPR.  If this is the "and", we can do something
5491      if the comparisons are EQ_EXPR.  I.e.,
5492         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5493
5494      WANTED_CODE is this operation code.  For single bit fields, we can
5495      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5496      comparison for one-bit fields.  */
5497
5498   enum tree_code wanted_code;
5499   enum tree_code lcode, rcode;
5500   tree ll_arg, lr_arg, rl_arg, rr_arg;
5501   tree ll_inner, lr_inner, rl_inner, rr_inner;
5502   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5503   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5504   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5505   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5506   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5507   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5508   enum machine_mode lnmode, rnmode;
5509   tree ll_mask, lr_mask, rl_mask, rr_mask;
5510   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5511   tree l_const, r_const;
5512   tree lntype, rntype, result;
5513   HOST_WIDE_INT first_bit, end_bit;
5514   int volatilep;
5515   tree orig_lhs = lhs, orig_rhs = rhs;
5516   enum tree_code orig_code = code;
5517
5518   /* Start by getting the comparison codes.  Fail if anything is volatile.
5519      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5520      it were surrounded with a NE_EXPR.  */
5521
5522   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5523     return 0;
5524
5525   lcode = TREE_CODE (lhs);
5526   rcode = TREE_CODE (rhs);
5527
5528   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5529     {
5530       lhs = build2 (NE_EXPR, truth_type, lhs,
5531                     build_int_cst (TREE_TYPE (lhs), 0));
5532       lcode = NE_EXPR;
5533     }
5534
5535   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5536     {
5537       rhs = build2 (NE_EXPR, truth_type, rhs,
5538                     build_int_cst (TREE_TYPE (rhs), 0));
5539       rcode = NE_EXPR;
5540     }
5541
5542   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5543       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5544     return 0;
5545
5546   ll_arg = TREE_OPERAND (lhs, 0);
5547   lr_arg = TREE_OPERAND (lhs, 1);
5548   rl_arg = TREE_OPERAND (rhs, 0);
5549   rr_arg = TREE_OPERAND (rhs, 1);
5550
5551   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5552   if (simple_operand_p (ll_arg)
5553       && simple_operand_p (lr_arg))
5554     {
5555       tree result;
5556       if (operand_equal_p (ll_arg, rl_arg, 0)
5557           && operand_equal_p (lr_arg, rr_arg, 0))
5558         {
5559           result = combine_comparisons (code, lcode, rcode,
5560                                         truth_type, ll_arg, lr_arg);
5561           if (result)
5562             return result;
5563         }
5564       else if (operand_equal_p (ll_arg, rr_arg, 0)
5565                && operand_equal_p (lr_arg, rl_arg, 0))
5566         {
5567           result = combine_comparisons (code, lcode,
5568                                         swap_tree_comparison (rcode),
5569                                         truth_type, ll_arg, lr_arg);
5570           if (result)
5571             return result;
5572         }
5573     }
5574
5575   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5576           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5577
5578   /* If the RHS can be evaluated unconditionally and its operands are
5579      simple, it wins to evaluate the RHS unconditionally on machines
5580      with expensive branches.  In this case, this isn't a comparison
5581      that can be merged.  Avoid doing this if the RHS is a floating-point
5582      comparison since those can trap.  */
5583
5584   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5585                    false) >= 2
5586       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5587       && simple_operand_p (rl_arg)
5588       && simple_operand_p (rr_arg))
5589     {
5590       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5591       if (code == TRUTH_OR_EXPR
5592           && lcode == NE_EXPR && integer_zerop (lr_arg)
5593           && rcode == NE_EXPR && integer_zerop (rr_arg)
5594           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5595           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5596         return build2 (NE_EXPR, truth_type,
5597                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5598                                ll_arg, rl_arg),
5599                        build_int_cst (TREE_TYPE (ll_arg), 0));
5600
5601       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5602       if (code == TRUTH_AND_EXPR
5603           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5604           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5605           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5606           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5607         return build2 (EQ_EXPR, truth_type,
5608                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5609                                ll_arg, rl_arg),
5610                        build_int_cst (TREE_TYPE (ll_arg), 0));
5611
5612       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5613         {
5614           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5615             return build2 (code, truth_type, lhs, rhs);
5616           return NULL_TREE;
5617         }
5618     }
5619
5620   /* See if the comparisons can be merged.  Then get all the parameters for
5621      each side.  */
5622
5623   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5624       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5625     return 0;
5626
5627   volatilep = 0;
5628   ll_inner = decode_field_reference (ll_arg,
5629                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5630                                      &ll_unsignedp, &volatilep, &ll_mask,
5631                                      &ll_and_mask);
5632   lr_inner = decode_field_reference (lr_arg,
5633                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5634                                      &lr_unsignedp, &volatilep, &lr_mask,
5635                                      &lr_and_mask);
5636   rl_inner = decode_field_reference (rl_arg,
5637                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5638                                      &rl_unsignedp, &volatilep, &rl_mask,
5639                                      &rl_and_mask);
5640   rr_inner = decode_field_reference (rr_arg,
5641                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5642                                      &rr_unsignedp, &volatilep, &rr_mask,
5643                                      &rr_and_mask);
5644
5645   /* It must be true that the inner operation on the lhs of each
5646      comparison must be the same if we are to be able to do anything.
5647      Then see if we have constants.  If not, the same must be true for
5648      the rhs's.  */
5649   if (volatilep || ll_inner == 0 || rl_inner == 0
5650       || ! operand_equal_p (ll_inner, rl_inner, 0))
5651     return 0;
5652
5653   if (TREE_CODE (lr_arg) == INTEGER_CST
5654       && TREE_CODE (rr_arg) == INTEGER_CST)
5655     l_const = lr_arg, r_const = rr_arg;
5656   else if (lr_inner == 0 || rr_inner == 0
5657            || ! operand_equal_p (lr_inner, rr_inner, 0))
5658     return 0;
5659   else
5660     l_const = r_const = 0;
5661
5662   /* If either comparison code is not correct for our logical operation,
5663      fail.  However, we can convert a one-bit comparison against zero into
5664      the opposite comparison against that bit being set in the field.  */
5665
5666   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5667   if (lcode != wanted_code)
5668     {
5669       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5670         {
5671           /* Make the left operand unsigned, since we are only interested
5672              in the value of one bit.  Otherwise we are doing the wrong
5673              thing below.  */
5674           ll_unsignedp = 1;
5675           l_const = ll_mask;
5676         }
5677       else
5678         return 0;
5679     }
5680
5681   /* This is analogous to the code for l_const above.  */
5682   if (rcode != wanted_code)
5683     {
5684       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5685         {
5686           rl_unsignedp = 1;
5687           r_const = rl_mask;
5688         }
5689       else
5690         return 0;
5691     }
5692
5693   /* See if we can find a mode that contains both fields being compared on
5694      the left.  If we can't, fail.  Otherwise, update all constants and masks
5695      to be relative to a field of that size.  */
5696   first_bit = MIN (ll_bitpos, rl_bitpos);
5697   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5698   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5699                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5700                           volatilep);
5701   if (lnmode == VOIDmode)
5702     return 0;
5703
5704   lnbitsize = GET_MODE_BITSIZE (lnmode);
5705   lnbitpos = first_bit & ~ (lnbitsize - 1);
5706   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5707   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5708
5709   if (BYTES_BIG_ENDIAN)
5710     {
5711       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5712       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5713     }
5714
5715   ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5716                          size_int (xll_bitpos), 0);
5717   rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5718                          size_int (xrl_bitpos), 0);
5719
5720   if (l_const)
5721     {
5722       l_const = fold_convert (lntype, l_const);
5723       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5724       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5725       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5726                                         fold_build1 (BIT_NOT_EXPR,
5727                                                      lntype, ll_mask),
5728                                         0)))
5729         {
5730           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5731
5732           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5733         }
5734     }
5735   if (r_const)
5736     {
5737       r_const = fold_convert (lntype, r_const);
5738       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5739       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5740       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5741                                         fold_build1 (BIT_NOT_EXPR,
5742                                                      lntype, rl_mask),
5743                                         0)))
5744         {
5745           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5746
5747           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5748         }
5749     }
5750
5751   /* If the right sides are not constant, do the same for it.  Also,
5752      disallow this optimization if a size or signedness mismatch occurs
5753      between the left and right sides.  */
5754   if (l_const == 0)
5755     {
5756       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5757           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5758           /* Make sure the two fields on the right
5759              correspond to the left without being swapped.  */
5760           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5761         return 0;
5762
5763       first_bit = MIN (lr_bitpos, rr_bitpos);
5764       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5765       rnmode = get_best_mode (end_bit - first_bit, first_bit,
5766                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5767                               volatilep);
5768       if (rnmode == VOIDmode)
5769         return 0;
5770
5771       rnbitsize = GET_MODE_BITSIZE (rnmode);
5772       rnbitpos = first_bit & ~ (rnbitsize - 1);
5773       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5774       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5775
5776       if (BYTES_BIG_ENDIAN)
5777         {
5778           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5779           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5780         }
5781
5782       lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
5783                              size_int (xlr_bitpos), 0);
5784       rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
5785                              size_int (xrr_bitpos), 0);
5786
5787       /* Make a mask that corresponds to both fields being compared.
5788          Do this for both items being compared.  If the operands are the
5789          same size and the bits being compared are in the same position
5790          then we can do this by masking both and comparing the masked
5791          results.  */
5792       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5793       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5794       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5795         {
5796           lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5797                                     ll_unsignedp || rl_unsignedp);
5798           if (! all_ones_mask_p (ll_mask, lnbitsize))
5799             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5800
5801           rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
5802                                     lr_unsignedp || rr_unsignedp);
5803           if (! all_ones_mask_p (lr_mask, rnbitsize))
5804             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5805
5806           return build2 (wanted_code, truth_type, lhs, rhs);
5807         }
5808
5809       /* There is still another way we can do something:  If both pairs of
5810          fields being compared are adjacent, we may be able to make a wider
5811          field containing them both.
5812
5813          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5814          the mask must be shifted to account for the shift done by
5815          make_bit_field_ref.  */
5816       if ((ll_bitsize + ll_bitpos == rl_bitpos
5817            && lr_bitsize + lr_bitpos == rr_bitpos)
5818           || (ll_bitpos == rl_bitpos + rl_bitsize
5819               && lr_bitpos == rr_bitpos + rr_bitsize))
5820         {
5821           tree type;
5822
5823           lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
5824                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5825           rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
5826                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5827
5828           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5829                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5830           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5831                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5832
5833           /* Convert to the smaller type before masking out unwanted bits.  */
5834           type = lntype;
5835           if (lntype != rntype)
5836             {
5837               if (lnbitsize > rnbitsize)
5838                 {
5839                   lhs = fold_convert (rntype, lhs);
5840                   ll_mask = fold_convert (rntype, ll_mask);
5841                   type = rntype;
5842                 }
5843               else if (lnbitsize < rnbitsize)
5844                 {
5845                   rhs = fold_convert (lntype, rhs);
5846                   lr_mask = fold_convert (lntype, lr_mask);
5847                   type = lntype;
5848                 }
5849             }
5850
5851           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5852             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5853
5854           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5855             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5856
5857           return build2 (wanted_code, truth_type, lhs, rhs);
5858         }
5859
5860       return 0;
5861     }
5862
5863   /* Handle the case of comparisons with constants.  If there is something in
5864      common between the masks, those bits of the constants must be the same.
5865      If not, the condition is always false.  Test for this to avoid generating
5866      incorrect code below.  */
5867   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5868   if (! integer_zerop (result)
5869       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5870                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5871     {
5872       if (wanted_code == NE_EXPR)
5873         {
5874           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5875           return constant_boolean_node (true, truth_type);
5876         }
5877       else
5878         {
5879           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5880           return constant_boolean_node (false, truth_type);
5881         }
5882     }
5883
5884   /* Construct the expression we will return.  First get the component
5885      reference we will make.  Unless the mask is all ones the width of
5886      that field, perform the mask operation.  Then compare with the
5887      merged constant.  */
5888   result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5889                                ll_unsignedp || rl_unsignedp);
5890
5891   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5892   if (! all_ones_mask_p (ll_mask, lnbitsize))
5893     result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5894
5895   return build2 (wanted_code, truth_type, result,
5896                  const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5897 }
5898 \f
5899 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5900    constant.  */
5901
5902 static tree
5903 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5904 {
5905   tree arg0 = op0;
5906   enum tree_code op_code;
5907   tree comp_const;
5908   tree minmax_const;
5909   int consts_equal, consts_lt;
5910   tree inner;
5911
5912   STRIP_SIGN_NOPS (arg0);
5913
5914   op_code = TREE_CODE (arg0);
5915   minmax_const = TREE_OPERAND (arg0, 1);
5916   comp_const = fold_convert (TREE_TYPE (arg0), op1);
5917   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5918   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5919   inner = TREE_OPERAND (arg0, 0);
5920
5921   /* If something does not permit us to optimize, return the original tree.  */
5922   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5923       || TREE_CODE (comp_const) != INTEGER_CST
5924       || TREE_OVERFLOW (comp_const)
5925       || TREE_CODE (minmax_const) != INTEGER_CST
5926       || TREE_OVERFLOW (minmax_const))
5927     return NULL_TREE;
5928
5929   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5930      and GT_EXPR, doing the rest with recursive calls using logical
5931      simplifications.  */
5932   switch (code)
5933     {
5934     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5935       {
5936         tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5937                                           type, op0, op1);
5938         if (tem)
5939           return invert_truthvalue (tem);
5940         return NULL_TREE;
5941       }
5942
5943     case GE_EXPR:
5944       return
5945         fold_build2 (TRUTH_ORIF_EXPR, type,
5946                      optimize_minmax_comparison
5947                      (EQ_EXPR, type, arg0, comp_const),
5948                      optimize_minmax_comparison
5949                      (GT_EXPR, type, arg0, comp_const));
5950
5951     case EQ_EXPR:
5952       if (op_code == MAX_EXPR && consts_equal)
5953         /* MAX (X, 0) == 0  ->  X <= 0  */
5954         return fold_build2 (LE_EXPR, type, inner, comp_const);
5955
5956       else if (op_code == MAX_EXPR && consts_lt)
5957         /* MAX (X, 0) == 5  ->  X == 5   */
5958         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5959
5960       else if (op_code == MAX_EXPR)
5961         /* MAX (X, 0) == -1  ->  false  */
5962         return omit_one_operand (type, integer_zero_node, inner);
5963
5964       else if (consts_equal)
5965         /* MIN (X, 0) == 0  ->  X >= 0  */
5966         return fold_build2 (GE_EXPR, type, inner, comp_const);
5967
5968       else if (consts_lt)
5969         /* MIN (X, 0) == 5  ->  false  */
5970         return omit_one_operand (type, integer_zero_node, inner);
5971
5972       else
5973         /* MIN (X, 0) == -1  ->  X == -1  */
5974         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5975
5976     case GT_EXPR:
5977       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5978         /* MAX (X, 0) > 0  ->  X > 0
5979            MAX (X, 0) > 5  ->  X > 5  */
5980         return fold_build2 (GT_EXPR, type, inner, comp_const);
5981
5982       else if (op_code == MAX_EXPR)
5983         /* MAX (X, 0) > -1  ->  true  */
5984         return omit_one_operand (type, integer_one_node, inner);
5985
5986       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5987         /* MIN (X, 0) > 0  ->  false
5988            MIN (X, 0) > 5  ->  false  */
5989         return omit_one_operand (type, integer_zero_node, inner);
5990
5991       else
5992         /* MIN (X, 0) > -1  ->  X > -1  */
5993         return fold_build2 (GT_EXPR, type, inner, comp_const);
5994
5995     default:
5996       return NULL_TREE;
5997     }
5998 }
5999 \f
6000 /* T is an integer expression that is being multiplied, divided, or taken a
6001    modulus (CODE says which and what kind of divide or modulus) by a
6002    constant C.  See if we can eliminate that operation by folding it with
6003    other operations already in T.  WIDE_TYPE, if non-null, is a type that
6004    should be used for the computation if wider than our type.
6005
6006    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6007    (X * 2) + (Y * 4).  We must, however, be assured that either the original
6008    expression would not overflow or that overflow is undefined for the type
6009    in the language in question.
6010
6011    We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
6012    the machine has a multiply-accumulate insn or that this is part of an
6013    addressing calculation.
6014
6015    If we return a non-null expression, it is an equivalent form of the
6016    original computation, but need not be in the original type.
6017
6018    We set *STRICT_OVERFLOW_P to true if the return values depends on
6019    signed overflow being undefined.  Otherwise we do not change
6020    *STRICT_OVERFLOW_P.  */
6021
6022 static tree
6023 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6024                 bool *strict_overflow_p)
6025 {
6026   /* To avoid exponential search depth, refuse to allow recursion past
6027      three levels.  Beyond that (1) it's highly unlikely that we'll find
6028      something interesting and (2) we've probably processed it before
6029      when we built the inner expression.  */
6030
6031   static int depth;
6032   tree ret;
6033
6034   if (depth > 3)
6035     return NULL;
6036
6037   depth++;
6038   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6039   depth--;
6040
6041   return ret;
6042 }
6043
6044 static tree
6045 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6046                   bool *strict_overflow_p)
6047 {
6048   tree type = TREE_TYPE (t);
6049   enum tree_code tcode = TREE_CODE (t);
6050   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6051                                    > GET_MODE_SIZE (TYPE_MODE (type)))
6052                 ? wide_type : type);
6053   tree t1, t2;
6054   int same_p = tcode == code;
6055   tree op0 = NULL_TREE, op1 = NULL_TREE;
6056   bool sub_strict_overflow_p;
6057
6058   /* Don't deal with constants of zero here; they confuse the code below.  */
6059   if (integer_zerop (c))
6060     return NULL_TREE;
6061
6062   if (TREE_CODE_CLASS (tcode) == tcc_unary)
6063     op0 = TREE_OPERAND (t, 0);
6064
6065   if (TREE_CODE_CLASS (tcode) == tcc_binary)
6066     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6067
6068   /* Note that we need not handle conditional operations here since fold
6069      already handles those cases.  So just do arithmetic here.  */
6070   switch (tcode)
6071     {
6072     case INTEGER_CST:
6073       /* For a constant, we can always simplify if we are a multiply
6074          or (for divide and modulus) if it is a multiple of our constant.  */
6075       if (code == MULT_EXPR
6076           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6077         return const_binop (code, fold_convert (ctype, t),
6078                             fold_convert (ctype, c), 0);
6079       break;
6080
6081     CASE_CONVERT: case NON_LVALUE_EXPR:
6082       /* If op0 is an expression ...  */
6083       if ((COMPARISON_CLASS_P (op0)
6084            || UNARY_CLASS_P (op0)
6085            || BINARY_CLASS_P (op0)
6086            || VL_EXP_CLASS_P (op0)
6087            || EXPRESSION_CLASS_P (op0))
6088           /* ... and has wrapping overflow, and its type is smaller
6089              than ctype, then we cannot pass through as widening.  */
6090           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6091                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6092                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6093                && (TYPE_PRECISION (ctype)
6094                    > TYPE_PRECISION (TREE_TYPE (op0))))
6095               /* ... or this is a truncation (t is narrower than op0),
6096                  then we cannot pass through this narrowing.  */
6097               || (TYPE_PRECISION (type)
6098                   < TYPE_PRECISION (TREE_TYPE (op0)))
6099               /* ... or signedness changes for division or modulus,
6100                  then we cannot pass through this conversion.  */
6101               || (code != MULT_EXPR
6102                   && (TYPE_UNSIGNED (ctype)
6103                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
6104               /* ... or has undefined overflow while the converted to
6105                  type has not, we cannot do the operation in the inner type
6106                  as that would introduce undefined overflow.  */
6107               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6108                   && !TYPE_OVERFLOW_UNDEFINED (type))))
6109         break;
6110
6111       /* Pass the constant down and see if we can make a simplification.  If
6112          we can, replace this expression with the inner simplification for
6113          possible later conversion to our or some other type.  */
6114       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6115           && TREE_CODE (t2) == INTEGER_CST
6116           && !TREE_OVERFLOW (t2)
6117           && (0 != (t1 = extract_muldiv (op0, t2, code,
6118                                          code == MULT_EXPR
6119                                          ? ctype : NULL_TREE,
6120                                          strict_overflow_p))))
6121         return t1;
6122       break;
6123
6124     case ABS_EXPR:
6125       /* If widening the type changes it from signed to unsigned, then we
6126          must avoid building ABS_EXPR itself as unsigned.  */
6127       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6128         {
6129           tree cstype = (*signed_type_for) (ctype);
6130           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6131               != 0)
6132             {
6133               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6134               return fold_convert (ctype, t1);
6135             }
6136           break;
6137         }
6138       /* If the constant is negative, we cannot simplify this.  */
6139       if (tree_int_cst_sgn (c) == -1)
6140         break;
6141       /* FALLTHROUGH */
6142     case NEGATE_EXPR:
6143       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6144           != 0)
6145         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6146       break;
6147
6148     case MIN_EXPR:  case MAX_EXPR:
6149       /* If widening the type changes the signedness, then we can't perform
6150          this optimization as that changes the result.  */
6151       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6152         break;
6153
6154       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
6155       sub_strict_overflow_p = false;
6156       if ((t1 = extract_muldiv (op0, c, code, wide_type,
6157                                 &sub_strict_overflow_p)) != 0
6158           && (t2 = extract_muldiv (op1, c, code, wide_type,
6159                                    &sub_strict_overflow_p)) != 0)
6160         {
6161           if (tree_int_cst_sgn (c) < 0)
6162             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6163           if (sub_strict_overflow_p)
6164             *strict_overflow_p = true;
6165           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6166                               fold_convert (ctype, t2));
6167         }
6168       break;
6169
6170     case LSHIFT_EXPR:  case RSHIFT_EXPR:
6171       /* If the second operand is constant, this is a multiplication
6172          or floor division, by a power of two, so we can treat it that
6173          way unless the multiplier or divisor overflows.  Signed
6174          left-shift overflow is implementation-defined rather than
6175          undefined in C90, so do not convert signed left shift into
6176          multiplication.  */
6177       if (TREE_CODE (op1) == INTEGER_CST
6178           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6179           /* const_binop may not detect overflow correctly,
6180              so check for it explicitly here.  */
6181           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6182           && TREE_INT_CST_HIGH (op1) == 0
6183           && 0 != (t1 = fold_convert (ctype,
6184                                       const_binop (LSHIFT_EXPR,
6185                                                    size_one_node,
6186                                                    op1, 0)))
6187           && !TREE_OVERFLOW (t1))
6188         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6189                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
6190                                        ctype, fold_convert (ctype, op0), t1),
6191                                c, code, wide_type, strict_overflow_p);
6192       break;
6193
6194     case PLUS_EXPR:  case MINUS_EXPR:
6195       /* See if we can eliminate the operation on both sides.  If we can, we
6196          can return a new PLUS or MINUS.  If we can't, the only remaining
6197          cases where we can do anything are if the second operand is a
6198          constant.  */
6199       sub_strict_overflow_p = false;
6200       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6201       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6202       if (t1 != 0 && t2 != 0
6203           && (code == MULT_EXPR
6204               /* If not multiplication, we can only do this if both operands
6205                  are divisible by c.  */
6206               || (multiple_of_p (ctype, op0, c)
6207                   && multiple_of_p (ctype, op1, c))))
6208         {
6209           if (sub_strict_overflow_p)
6210             *strict_overflow_p = true;
6211           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6212                               fold_convert (ctype, t2));
6213         }
6214
6215       /* If this was a subtraction, negate OP1 and set it to be an addition.
6216          This simplifies the logic below.  */
6217       if (tcode == MINUS_EXPR)
6218         tcode = PLUS_EXPR, op1 = negate_expr (op1);
6219
6220       if (TREE_CODE (op1) != INTEGER_CST)
6221         break;
6222
6223       /* If either OP1 or C are negative, this optimization is not safe for
6224          some of the division and remainder types while for others we need
6225          to change the code.  */
6226       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6227         {
6228           if (code == CEIL_DIV_EXPR)
6229             code = FLOOR_DIV_EXPR;
6230           else if (code == FLOOR_DIV_EXPR)
6231             code = CEIL_DIV_EXPR;
6232           else if (code != MULT_EXPR
6233                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6234             break;
6235         }
6236
6237       /* If it's a multiply or a division/modulus operation of a multiple
6238          of our constant, do the operation and verify it doesn't overflow.  */
6239       if (code == MULT_EXPR
6240           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6241         {
6242           op1 = const_binop (code, fold_convert (ctype, op1),
6243                              fold_convert (ctype, c), 0);
6244           /* We allow the constant to overflow with wrapping semantics.  */
6245           if (op1 == 0
6246               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6247             break;
6248         }
6249       else
6250         break;
6251
6252       /* If we have an unsigned type is not a sizetype, we cannot widen
6253          the operation since it will change the result if the original
6254          computation overflowed.  */
6255       if (TYPE_UNSIGNED (ctype)
6256           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6257           && ctype != type)
6258         break;
6259
6260       /* If we were able to eliminate our operation from the first side,
6261          apply our operation to the second side and reform the PLUS.  */
6262       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6263         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6264
6265       /* The last case is if we are a multiply.  In that case, we can
6266          apply the distributive law to commute the multiply and addition
6267          if the multiplication of the constants doesn't overflow.  */
6268       if (code == MULT_EXPR)
6269         return fold_build2 (tcode, ctype,
6270                             fold_build2 (code, ctype,
6271                                          fold_convert (ctype, op0),
6272                                          fold_convert (ctype, c)),
6273                             op1);
6274
6275       break;
6276
6277     case MULT_EXPR:
6278       /* We have a special case here if we are doing something like
6279          (C * 8) % 4 since we know that's zero.  */
6280       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6281            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6282           /* If the multiplication can overflow we cannot optimize this.
6283              ???  Until we can properly mark individual operations as
6284              not overflowing we need to treat sizetype special here as
6285              stor-layout relies on this opimization to make
6286              DECL_FIELD_BIT_OFFSET always a constant.  */
6287           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6288               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6289                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
6290           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6291           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6292         {
6293           *strict_overflow_p = true;
6294           return omit_one_operand (type, integer_zero_node, op0);
6295         }
6296
6297       /* ... fall through ...  */
6298
6299     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
6300     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
6301       /* If we can extract our operation from the LHS, do so and return a
6302          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
6303          do something only if the second operand is a constant.  */
6304       if (same_p
6305           && (t1 = extract_muldiv (op0, c, code, wide_type,
6306                                    strict_overflow_p)) != 0)
6307         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6308                             fold_convert (ctype, op1));
6309       else if (tcode == MULT_EXPR && code == MULT_EXPR
6310                && (t1 = extract_muldiv (op1, c, code, wide_type,
6311                                         strict_overflow_p)) != 0)
6312         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6313                             fold_convert (ctype, t1));
6314       else if (TREE_CODE (op1) != INTEGER_CST)
6315         return 0;
6316
6317       /* If these are the same operation types, we can associate them
6318          assuming no overflow.  */
6319       if (tcode == code
6320           && 0 != (t1 = int_const_binop (MULT_EXPR, fold_convert (ctype, op1),
6321                                          fold_convert (ctype, c), 1))
6322           && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
6323                                                TREE_INT_CST_HIGH (t1),
6324                                                (TYPE_UNSIGNED (ctype)
6325                                                 && tcode != MULT_EXPR) ? -1 : 1,
6326                                                TREE_OVERFLOW (t1)))
6327           && !TREE_OVERFLOW (t1))
6328         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6329
6330       /* If these operations "cancel" each other, we have the main
6331          optimizations of this pass, which occur when either constant is a
6332          multiple of the other, in which case we replace this with either an
6333          operation or CODE or TCODE.
6334
6335          If we have an unsigned type that is not a sizetype, we cannot do
6336          this since it will change the result if the original computation
6337          overflowed.  */
6338       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6339            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6340           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6341               || (tcode == MULT_EXPR
6342                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6343                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6344                   && code != MULT_EXPR)))
6345         {
6346           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6347             {
6348               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6349                 *strict_overflow_p = true;
6350               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6351                                   fold_convert (ctype,
6352                                                 const_binop (TRUNC_DIV_EXPR,
6353                                                              op1, c, 0)));
6354             }
6355           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6356             {
6357               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6358                 *strict_overflow_p = true;
6359               return fold_build2 (code, ctype, fold_convert (ctype, op0),
6360                                   fold_convert (ctype,
6361                                                 const_binop (TRUNC_DIV_EXPR,
6362                                                              c, op1, 0)));
6363             }
6364         }
6365       break;
6366
6367     default:
6368       break;
6369     }
6370
6371   return 0;
6372 }
6373 \f
6374 /* Return a node which has the indicated constant VALUE (either 0 or
6375    1), and is of the indicated TYPE.  */
6376
6377 tree
6378 constant_boolean_node (int value, tree type)
6379 {
6380   if (type == integer_type_node)
6381     return value ? integer_one_node : integer_zero_node;
6382   else if (type == boolean_type_node)
6383     return value ? boolean_true_node : boolean_false_node;
6384   else
6385     return build_int_cst (type, value);
6386 }
6387
6388
6389 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6390    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6391    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6392    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6393    COND is the first argument to CODE; otherwise (as in the example
6394    given here), it is the second argument.  TYPE is the type of the
6395    original expression.  Return NULL_TREE if no simplification is
6396    possible.  */
6397
6398 static tree
6399 fold_binary_op_with_conditional_arg (enum tree_code code,
6400                                      tree type, tree op0, tree op1,
6401                                      tree cond, tree arg, int cond_first_p)
6402 {
6403   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6404   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6405   tree test, true_value, false_value;
6406   tree lhs = NULL_TREE;
6407   tree rhs = NULL_TREE;
6408
6409   /* This transformation is only worthwhile if we don't have to wrap
6410      arg in a SAVE_EXPR, and the operation can be simplified on at least
6411      one of the branches once its pushed inside the COND_EXPR.  */
6412   if (!TREE_CONSTANT (arg))
6413     return NULL_TREE;
6414
6415   if (TREE_CODE (cond) == COND_EXPR)
6416     {
6417       test = TREE_OPERAND (cond, 0);
6418       true_value = TREE_OPERAND (cond, 1);
6419       false_value = TREE_OPERAND (cond, 2);
6420       /* If this operand throws an expression, then it does not make
6421          sense to try to perform a logical or arithmetic operation
6422          involving it.  */
6423       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6424         lhs = true_value;
6425       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6426         rhs = false_value;
6427     }
6428   else
6429     {
6430       tree testtype = TREE_TYPE (cond);
6431       test = cond;
6432       true_value = constant_boolean_node (true, testtype);
6433       false_value = constant_boolean_node (false, testtype);
6434     }
6435
6436   arg = fold_convert (arg_type, arg);
6437   if (lhs == 0)
6438     {
6439       true_value = fold_convert (cond_type, true_value);
6440       if (cond_first_p)
6441         lhs = fold_build2 (code, type, true_value, arg);
6442       else
6443         lhs = fold_build2 (code, type, arg, true_value);
6444     }
6445   if (rhs == 0)
6446     {
6447       false_value = fold_convert (cond_type, false_value);
6448       if (cond_first_p)
6449         rhs = fold_build2 (code, type, false_value, arg);
6450       else
6451         rhs = fold_build2 (code, type, arg, false_value);
6452     }
6453
6454   test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6455   return fold_convert (type, test);
6456 }
6457
6458 \f
6459 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6460
6461    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6462    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6463    ADDEND is the same as X.
6464
6465    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6466    and finite.  The problematic cases are when X is zero, and its mode
6467    has signed zeros.  In the case of rounding towards -infinity,
6468    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6469    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6470
6471 bool
6472 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6473 {
6474   if (!real_zerop (addend))
6475     return false;
6476
6477   /* Don't allow the fold with -fsignaling-nans.  */
6478   if (HONOR_SNANS (TYPE_MODE (type)))
6479     return false;
6480
6481   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6482   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6483     return true;
6484
6485   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6486   if (TREE_CODE (addend) == REAL_CST
6487       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6488     negate = !negate;
6489
6490   /* The mode has signed zeros, and we have to honor their sign.
6491      In this situation, there is only one case we can return true for.
6492      X - 0 is the same as X unless rounding towards -infinity is
6493      supported.  */
6494   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6495 }
6496
6497 /* Subroutine of fold() that checks comparisons of built-in math
6498    functions against real constants.
6499
6500    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6501    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6502    is the type of the result and ARG0 and ARG1 are the operands of the
6503    comparison.  ARG1 must be a TREE_REAL_CST.
6504
6505    The function returns the constant folded tree if a simplification
6506    can be made, and NULL_TREE otherwise.  */
6507
6508 static tree
6509 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6510                      tree type, tree arg0, tree arg1)
6511 {
6512   REAL_VALUE_TYPE c;
6513
6514   if (BUILTIN_SQRT_P (fcode))
6515     {
6516       tree arg = CALL_EXPR_ARG (arg0, 0);
6517       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6518
6519       c = TREE_REAL_CST (arg1);
6520       if (REAL_VALUE_NEGATIVE (c))
6521         {
6522           /* sqrt(x) < y is always false, if y is negative.  */
6523           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6524             return omit_one_operand (type, integer_zero_node, arg);
6525
6526           /* sqrt(x) > y is always true, if y is negative and we
6527              don't care about NaNs, i.e. negative values of x.  */
6528           if (code == NE_EXPR || !HONOR_NANS (mode))
6529             return omit_one_operand (type, integer_one_node, arg);
6530
6531           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6532           return fold_build2 (GE_EXPR, type, arg,
6533                               build_real (TREE_TYPE (arg), dconst0));
6534         }
6535       else if (code == GT_EXPR || code == GE_EXPR)
6536         {
6537           REAL_VALUE_TYPE c2;
6538
6539           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6540           real_convert (&c2, mode, &c2);
6541
6542           if (REAL_VALUE_ISINF (c2))
6543             {
6544               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6545               if (HONOR_INFINITIES (mode))
6546                 return fold_build2 (EQ_EXPR, type, arg,
6547                                     build_real (TREE_TYPE (arg), c2));
6548
6549               /* sqrt(x) > y is always false, when y is very large
6550                  and we don't care about infinities.  */
6551               return omit_one_operand (type, integer_zero_node, arg);
6552             }
6553
6554           /* sqrt(x) > c is the same as x > c*c.  */
6555           return fold_build2 (code, type, arg,
6556                               build_real (TREE_TYPE (arg), c2));
6557         }
6558       else if (code == LT_EXPR || code == LE_EXPR)
6559         {
6560           REAL_VALUE_TYPE c2;
6561
6562           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6563           real_convert (&c2, mode, &c2);
6564
6565           if (REAL_VALUE_ISINF (c2))
6566             {
6567               /* sqrt(x) < y is always true, when y is a very large
6568                  value and we don't care about NaNs or Infinities.  */
6569               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6570                 return omit_one_operand (type, integer_one_node, arg);
6571
6572               /* sqrt(x) < y is x != +Inf when y is very large and we
6573                  don't care about NaNs.  */
6574               if (! HONOR_NANS (mode))
6575                 return fold_build2 (NE_EXPR, type, arg,
6576                                     build_real (TREE_TYPE (arg), c2));
6577
6578               /* sqrt(x) < y is x >= 0 when y is very large and we
6579                  don't care about Infinities.  */
6580               if (! HONOR_INFINITIES (mode))
6581                 return fold_build2 (GE_EXPR, type, arg,
6582                                     build_real (TREE_TYPE (arg), dconst0));
6583
6584               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6585               if (lang_hooks.decls.global_bindings_p () != 0
6586                   || CONTAINS_PLACEHOLDER_P (arg))
6587                 return NULL_TREE;
6588
6589               arg = save_expr (arg);
6590               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6591                                   fold_build2 (GE_EXPR, type, arg,
6592                                                build_real (TREE_TYPE (arg),
6593                                                            dconst0)),
6594                                   fold_build2 (NE_EXPR, type, arg,
6595                                                build_real (TREE_TYPE (arg),
6596                                                            c2)));
6597             }
6598
6599           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6600           if (! HONOR_NANS (mode))
6601             return fold_build2 (code, type, arg,
6602                                 build_real (TREE_TYPE (arg), c2));
6603
6604           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6605           if (lang_hooks.decls.global_bindings_p () == 0
6606               && ! CONTAINS_PLACEHOLDER_P (arg))
6607             {
6608               arg = save_expr (arg);
6609               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6610                                   fold_build2 (GE_EXPR, type, arg,
6611                                                build_real (TREE_TYPE (arg),
6612                                                            dconst0)),
6613                                   fold_build2 (code, type, arg,
6614                                                build_real (TREE_TYPE (arg),
6615                                                            c2)));
6616             }
6617         }
6618     }
6619
6620   return NULL_TREE;
6621 }
6622
6623 /* Subroutine of fold() that optimizes comparisons against Infinities,
6624    either +Inf or -Inf.
6625
6626    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6627    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6628    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6629
6630    The function returns the constant folded tree if a simplification
6631    can be made, and NULL_TREE otherwise.  */
6632
6633 static tree
6634 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6635 {
6636   enum machine_mode mode;
6637   REAL_VALUE_TYPE max;
6638   tree temp;
6639   bool neg;
6640
6641   mode = TYPE_MODE (TREE_TYPE (arg0));
6642
6643   /* For negative infinity swap the sense of the comparison.  */
6644   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6645   if (neg)
6646     code = swap_tree_comparison (code);
6647
6648   switch (code)
6649     {
6650     case GT_EXPR:
6651       /* x > +Inf is always false, if with ignore sNANs.  */
6652       if (HONOR_SNANS (mode))
6653         return NULL_TREE;
6654       return omit_one_operand (type, integer_zero_node, arg0);
6655
6656     case LE_EXPR:
6657       /* x <= +Inf is always true, if we don't case about NaNs.  */
6658       if (! HONOR_NANS (mode))
6659         return omit_one_operand (type, integer_one_node, arg0);
6660
6661       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6662       if (lang_hooks.decls.global_bindings_p () == 0
6663           && ! CONTAINS_PLACEHOLDER_P (arg0))
6664         {
6665           arg0 = save_expr (arg0);
6666           return fold_build2 (EQ_EXPR, type, arg0, arg0);
6667         }
6668       break;
6669
6670     case EQ_EXPR:
6671     case GE_EXPR:
6672       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6673       real_maxval (&max, neg, mode);
6674       return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6675                           arg0, build_real (TREE_TYPE (arg0), max));
6676
6677     case LT_EXPR:
6678       /* x < +Inf is always equal to x <= DBL_MAX.  */
6679       real_maxval (&max, neg, mode);
6680       return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6681                           arg0, build_real (TREE_TYPE (arg0), max));
6682
6683     case NE_EXPR:
6684       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6685       real_maxval (&max, neg, mode);
6686       if (! HONOR_NANS (mode))
6687         return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6688                             arg0, build_real (TREE_TYPE (arg0), max));
6689
6690       temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6691                           arg0, build_real (TREE_TYPE (arg0), max));
6692       return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6693
6694     default:
6695       break;
6696     }
6697
6698   return NULL_TREE;
6699 }
6700
6701 /* Subroutine of fold() that optimizes comparisons of a division by
6702    a nonzero integer constant against an integer constant, i.e.
6703    X/C1 op C2.
6704
6705    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6706    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6707    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6708
6709    The function returns the constant folded tree if a simplification
6710    can be made, and NULL_TREE otherwise.  */
6711
6712 static tree
6713 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6714 {
6715   tree prod, tmp, hi, lo;
6716   tree arg00 = TREE_OPERAND (arg0, 0);
6717   tree arg01 = TREE_OPERAND (arg0, 1);
6718   unsigned HOST_WIDE_INT lpart;
6719   HOST_WIDE_INT hpart;
6720   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6721   bool neg_overflow;
6722   int overflow;
6723
6724   /* We have to do this the hard way to detect unsigned overflow.
6725      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
6726   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6727                                    TREE_INT_CST_HIGH (arg01),
6728                                    TREE_INT_CST_LOW (arg1),
6729                                    TREE_INT_CST_HIGH (arg1),
6730                                    &lpart, &hpart, unsigned_p);
6731   prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6732                                 -1, overflow);
6733   neg_overflow = false;
6734
6735   if (unsigned_p)
6736     {
6737       tmp = int_const_binop (MINUS_EXPR, arg01,
6738                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6739       lo = prod;
6740
6741       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
6742       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6743                                        TREE_INT_CST_HIGH (prod),
6744                                        TREE_INT_CST_LOW (tmp),
6745                                        TREE_INT_CST_HIGH (tmp),
6746                                        &lpart, &hpart, unsigned_p);
6747       hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6748                                   -1, overflow | TREE_OVERFLOW (prod));
6749     }
6750   else if (tree_int_cst_sgn (arg01) >= 0)
6751     {
6752       tmp = int_const_binop (MINUS_EXPR, arg01,
6753                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6754       switch (tree_int_cst_sgn (arg1))
6755         {
6756         case -1:
6757           neg_overflow = true;
6758           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6759           hi = prod;
6760           break;
6761
6762         case  0:
6763           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6764           hi = tmp;
6765           break;
6766
6767         case  1:
6768           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6769           lo = prod;
6770           break;
6771
6772         default:
6773           gcc_unreachable ();
6774         }
6775     }
6776   else
6777     {
6778       /* A negative divisor reverses the relational operators.  */
6779       code = swap_tree_comparison (code);
6780
6781       tmp = int_const_binop (PLUS_EXPR, arg01,
6782                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6783       switch (tree_int_cst_sgn (arg1))
6784         {
6785         case -1:
6786           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6787           lo = prod;
6788           break;
6789
6790         case  0:
6791           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6792           lo = tmp;
6793           break;
6794
6795         case  1:
6796           neg_overflow = true;
6797           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6798           hi = prod;
6799           break;
6800
6801         default:
6802           gcc_unreachable ();
6803         }
6804     }
6805
6806   switch (code)
6807     {
6808     case EQ_EXPR:
6809       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6810         return omit_one_operand (type, integer_zero_node, arg00);
6811       if (TREE_OVERFLOW (hi))
6812         return fold_build2 (GE_EXPR, type, arg00, lo);
6813       if (TREE_OVERFLOW (lo))
6814         return fold_build2 (LE_EXPR, type, arg00, hi);
6815       return build_range_check (type, arg00, 1, lo, hi);
6816
6817     case NE_EXPR:
6818       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6819         return omit_one_operand (type, integer_one_node, arg00);
6820       if (TREE_OVERFLOW (hi))
6821         return fold_build2 (LT_EXPR, type, arg00, lo);
6822       if (TREE_OVERFLOW (lo))
6823         return fold_build2 (GT_EXPR, type, arg00, hi);
6824       return build_range_check (type, arg00, 0, lo, hi);
6825
6826     case LT_EXPR:
6827       if (TREE_OVERFLOW (lo))
6828         {
6829           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6830           return omit_one_operand (type, tmp, arg00);
6831         }
6832       return fold_build2 (LT_EXPR, type, arg00, lo);
6833
6834     case LE_EXPR:
6835       if (TREE_OVERFLOW (hi))
6836         {
6837           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6838           return omit_one_operand (type, tmp, arg00);
6839         }
6840       return fold_build2 (LE_EXPR, type, arg00, hi);
6841
6842     case GT_EXPR:
6843       if (TREE_OVERFLOW (hi))
6844         {
6845           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6846           return omit_one_operand (type, tmp, arg00);
6847         }
6848       return fold_build2 (GT_EXPR, type, arg00, hi);
6849
6850     case GE_EXPR:
6851       if (TREE_OVERFLOW (lo))
6852         {
6853           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6854           return omit_one_operand (type, tmp, arg00);
6855         }
6856       return fold_build2 (GE_EXPR, type, arg00, lo);
6857
6858     default:
6859       break;
6860     }
6861
6862   return NULL_TREE;
6863 }
6864
6865
6866 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6867    equality/inequality test, then return a simplified form of the test
6868    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6869    result type.  */
6870
6871 static tree
6872 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6873                                      tree result_type)
6874 {
6875   /* If this is testing a single bit, we can optimize the test.  */
6876   if ((code == NE_EXPR || code == EQ_EXPR)
6877       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6878       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6879     {
6880       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6881          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6882       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6883
6884       if (arg00 != NULL_TREE
6885           /* This is only a win if casting to a signed type is cheap,
6886              i.e. when arg00's type is not a partial mode.  */
6887           && TYPE_PRECISION (TREE_TYPE (arg00))
6888              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6889         {
6890           tree stype = signed_type_for (TREE_TYPE (arg00));
6891           return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6892                               result_type, fold_convert (stype, arg00),
6893                               build_int_cst (stype, 0));
6894         }
6895     }
6896
6897   return NULL_TREE;
6898 }
6899
6900 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6901    equality/inequality test, then return a simplified form of
6902    the test using shifts and logical operations.  Otherwise return
6903    NULL.  TYPE is the desired result type.  */
6904
6905 tree
6906 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6907                       tree result_type)
6908 {
6909   /* If this is testing a single bit, we can optimize the test.  */
6910   if ((code == NE_EXPR || code == EQ_EXPR)
6911       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6912       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6913     {
6914       tree inner = TREE_OPERAND (arg0, 0);
6915       tree type = TREE_TYPE (arg0);
6916       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6917       enum machine_mode operand_mode = TYPE_MODE (type);
6918       int ops_unsigned;
6919       tree signed_type, unsigned_type, intermediate_type;
6920       tree tem, one;
6921
6922       /* First, see if we can fold the single bit test into a sign-bit
6923          test.  */
6924       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6925                                                  result_type);
6926       if (tem)
6927         return tem;
6928
6929       /* Otherwise we have (A & C) != 0 where C is a single bit,
6930          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6931          Similarly for (A & C) == 0.  */
6932
6933       /* If INNER is a right shift of a constant and it plus BITNUM does
6934          not overflow, adjust BITNUM and INNER.  */
6935       if (TREE_CODE (inner) == RSHIFT_EXPR
6936           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6937           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6938           && bitnum < TYPE_PRECISION (type)
6939           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6940                                    bitnum - TYPE_PRECISION (type)))
6941         {
6942           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6943           inner = TREE_OPERAND (inner, 0);
6944         }
6945
6946       /* If we are going to be able to omit the AND below, we must do our
6947          operations as unsigned.  If we must use the AND, we have a choice.
6948          Normally unsigned is faster, but for some machines signed is.  */
6949 #ifdef LOAD_EXTEND_OP
6950       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND 
6951                       && !flag_syntax_only) ? 0 : 1;
6952 #else
6953       ops_unsigned = 1;
6954 #endif
6955
6956       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6957       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6958       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6959       inner = fold_convert (intermediate_type, inner);
6960
6961       if (bitnum != 0)
6962         inner = build2 (RSHIFT_EXPR, intermediate_type,
6963                         inner, size_int (bitnum));
6964
6965       one = build_int_cst (intermediate_type, 1);
6966
6967       if (code == EQ_EXPR)
6968         inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6969
6970       /* Put the AND last so it can combine with more things.  */
6971       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6972
6973       /* Make sure to return the proper type.  */
6974       inner = fold_convert (result_type, inner);
6975
6976       return inner;
6977     }
6978   return NULL_TREE;
6979 }
6980
6981 /* Check whether we are allowed to reorder operands arg0 and arg1,
6982    such that the evaluation of arg1 occurs before arg0.  */
6983
6984 static bool
6985 reorder_operands_p (const_tree arg0, const_tree arg1)
6986 {
6987   if (! flag_evaluation_order)
6988       return true;
6989   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6990     return true;
6991   return ! TREE_SIDE_EFFECTS (arg0)
6992          && ! TREE_SIDE_EFFECTS (arg1);
6993 }
6994
6995 /* Test whether it is preferable two swap two operands, ARG0 and
6996    ARG1, for example because ARG0 is an integer constant and ARG1
6997    isn't.  If REORDER is true, only recommend swapping if we can
6998    evaluate the operands in reverse order.  */
6999
7000 bool
7001 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
7002 {
7003   STRIP_SIGN_NOPS (arg0);
7004   STRIP_SIGN_NOPS (arg1);
7005
7006   if (TREE_CODE (arg1) == INTEGER_CST)
7007     return 0;
7008   if (TREE_CODE (arg0) == INTEGER_CST)
7009     return 1;
7010
7011   if (TREE_CODE (arg1) == REAL_CST)
7012     return 0;
7013   if (TREE_CODE (arg0) == REAL_CST)
7014     return 1;
7015
7016   if (TREE_CODE (arg1) == FIXED_CST)
7017     return 0;
7018   if (TREE_CODE (arg0) == FIXED_CST)
7019     return 1;
7020
7021   if (TREE_CODE (arg1) == COMPLEX_CST)
7022     return 0;
7023   if (TREE_CODE (arg0) == COMPLEX_CST)
7024     return 1;
7025
7026   if (TREE_CONSTANT (arg1))
7027     return 0;
7028   if (TREE_CONSTANT (arg0))
7029     return 1;
7030
7031   if (optimize_function_for_size_p (cfun))
7032     return 0;
7033
7034   if (reorder && flag_evaluation_order
7035       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7036     return 0;
7037
7038   /* It is preferable to swap two SSA_NAME to ensure a canonical form
7039      for commutative and comparison operators.  Ensuring a canonical
7040      form allows the optimizers to find additional redundancies without
7041      having to explicitly check for both orderings.  */
7042   if (TREE_CODE (arg0) == SSA_NAME
7043       && TREE_CODE (arg1) == SSA_NAME
7044       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7045     return 1;
7046
7047   /* Put SSA_NAMEs last.  */
7048   if (TREE_CODE (arg1) == SSA_NAME)
7049     return 0;
7050   if (TREE_CODE (arg0) == SSA_NAME)
7051     return 1;
7052
7053   /* Put variables last.  */
7054   if (DECL_P (arg1))
7055     return 0;
7056   if (DECL_P (arg0))
7057     return 1;
7058
7059   return 0;
7060 }
7061
7062 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7063    ARG0 is extended to a wider type.  */
7064
7065 static tree
7066 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
7067 {
7068   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7069   tree arg1_unw;
7070   tree shorter_type, outer_type;
7071   tree min, max;
7072   bool above, below;
7073
7074   if (arg0_unw == arg0)
7075     return NULL_TREE;
7076   shorter_type = TREE_TYPE (arg0_unw);
7077
7078 #ifdef HAVE_canonicalize_funcptr_for_compare
7079   /* Disable this optimization if we're casting a function pointer
7080      type on targets that require function pointer canonicalization.  */
7081   if (HAVE_canonicalize_funcptr_for_compare
7082       && TREE_CODE (shorter_type) == POINTER_TYPE
7083       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7084     return NULL_TREE;
7085 #endif
7086
7087   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7088     return NULL_TREE;
7089
7090   arg1_unw = get_unwidened (arg1, NULL_TREE);
7091
7092   /* If possible, express the comparison in the shorter mode.  */
7093   if ((code == EQ_EXPR || code == NE_EXPR
7094        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7095       && (TREE_TYPE (arg1_unw) == shorter_type
7096           || ((TYPE_PRECISION (shorter_type)
7097                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
7098               && (TYPE_UNSIGNED (shorter_type)
7099                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
7100           || (TREE_CODE (arg1_unw) == INTEGER_CST
7101               && (TREE_CODE (shorter_type) == INTEGER_TYPE
7102                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7103               && int_fits_type_p (arg1_unw, shorter_type))))
7104     return fold_build2 (code, type, arg0_unw,
7105                        fold_convert (shorter_type, arg1_unw));
7106
7107   if (TREE_CODE (arg1_unw) != INTEGER_CST
7108       || TREE_CODE (shorter_type) != INTEGER_TYPE
7109       || !int_fits_type_p (arg1_unw, shorter_type))
7110     return NULL_TREE;
7111
7112   /* If we are comparing with the integer that does not fit into the range
7113      of the shorter type, the result is known.  */
7114   outer_type = TREE_TYPE (arg1_unw);
7115   min = lower_bound_in_type (outer_type, shorter_type);
7116   max = upper_bound_in_type (outer_type, shorter_type);
7117
7118   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7119                                                    max, arg1_unw));
7120   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7121                                                    arg1_unw, min));
7122
7123   switch (code)
7124     {
7125     case EQ_EXPR:
7126       if (above || below)
7127         return omit_one_operand (type, integer_zero_node, arg0);
7128       break;
7129
7130     case NE_EXPR:
7131       if (above || below)
7132         return omit_one_operand (type, integer_one_node, arg0);
7133       break;
7134
7135     case LT_EXPR:
7136     case LE_EXPR:
7137       if (above)
7138         return omit_one_operand (type, integer_one_node, arg0);
7139       else if (below)
7140         return omit_one_operand (type, integer_zero_node, arg0);
7141
7142     case GT_EXPR:
7143     case GE_EXPR:
7144       if (above)
7145         return omit_one_operand (type, integer_zero_node, arg0);
7146       else if (below)
7147         return omit_one_operand (type, integer_one_node, arg0);
7148
7149     default:
7150       break;
7151     }
7152
7153   return NULL_TREE;
7154 }
7155
7156 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7157    ARG0 just the signedness is changed.  */
7158
7159 static tree
7160 fold_sign_changed_comparison (enum tree_code code, tree type,
7161                               tree arg0, tree arg1)
7162 {
7163   tree arg0_inner;
7164   tree inner_type, outer_type;
7165
7166   if (!CONVERT_EXPR_P (arg0))
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 the conversion is from an integral subtype to its basetype
7186      leave it alone.  */
7187   if (TREE_TYPE (inner_type) == outer_type)
7188     return NULL_TREE;
7189
7190   if (TREE_CODE (arg1) != INTEGER_CST
7191       && !(CONVERT_EXPR_P (arg1)
7192            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7193     return NULL_TREE;
7194
7195   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7196        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7197       && code != NE_EXPR
7198       && code != EQ_EXPR)
7199     return NULL_TREE;
7200
7201   if (TREE_CODE (arg1) == INTEGER_CST)
7202     arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7203                                   TREE_INT_CST_HIGH (arg1), 0,
7204                                   TREE_OVERFLOW (arg1));
7205   else
7206     arg1 = fold_convert (inner_type, arg1);
7207
7208   return fold_build2 (code, type, arg0_inner, arg1);
7209 }
7210
7211 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7212    step of the array.  Reconstructs s and delta in the case of s * delta
7213    being an integer constant (and thus already folded).
7214    ADDR is the address. MULT is the multiplicative expression.
7215    If the function succeeds, the new address expression is returned.  Otherwise
7216    NULL_TREE is returned.  */
7217
7218 static tree
7219 try_move_mult_to_index (tree addr, tree op1)
7220 {
7221   tree s, delta, step;
7222   tree ref = TREE_OPERAND (addr, 0), pref;
7223   tree ret, pos;
7224   tree itype;
7225   bool mdim = false;
7226
7227   /*  Strip the nops that might be added when converting op1 to sizetype. */
7228   STRIP_NOPS (op1);
7229
7230   /* Canonicalize op1 into a possibly non-constant delta
7231      and an INTEGER_CST s.  */
7232   if (TREE_CODE (op1) == MULT_EXPR)
7233     {
7234       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7235
7236       STRIP_NOPS (arg0);
7237       STRIP_NOPS (arg1);
7238   
7239       if (TREE_CODE (arg0) == INTEGER_CST)
7240         {
7241           s = arg0;
7242           delta = arg1;
7243         }
7244       else if (TREE_CODE (arg1) == INTEGER_CST)
7245         {
7246           s = arg1;
7247           delta = arg0;
7248         }
7249       else
7250         return NULL_TREE;
7251     }
7252   else if (TREE_CODE (op1) == INTEGER_CST)
7253     {
7254       delta = op1;
7255       s = NULL_TREE;
7256     }
7257   else
7258     {
7259       /* Simulate we are delta * 1.  */
7260       delta = op1;
7261       s = integer_one_node;
7262     }
7263
7264   for (;; ref = TREE_OPERAND (ref, 0))
7265     {
7266       if (TREE_CODE (ref) == ARRAY_REF)
7267         {
7268           /* Remember if this was a multi-dimensional array.  */
7269           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7270             mdim = true;
7271
7272           itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7273           if (! itype)
7274             continue;
7275
7276           step = array_ref_element_size (ref);
7277           if (TREE_CODE (step) != INTEGER_CST)
7278             continue;
7279
7280           if (s)
7281             {
7282               if (! tree_int_cst_equal (step, s))
7283                 continue;
7284             }
7285           else
7286             {
7287               /* Try if delta is a multiple of step.  */
7288               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
7289               if (! tmp)
7290                 continue;
7291               delta = tmp;
7292             }
7293
7294           /* Only fold here if we can verify we do not overflow one
7295              dimension of a multi-dimensional array.  */
7296           if (mdim)
7297             {
7298               tree tmp;
7299
7300               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7301                   || !INTEGRAL_TYPE_P (itype)
7302                   || !TYPE_MAX_VALUE (itype)
7303                   || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
7304                 continue;
7305
7306               tmp = fold_binary (PLUS_EXPR, itype,
7307                                  fold_convert (itype,
7308                                                TREE_OPERAND (ref, 1)),
7309                                  fold_convert (itype, delta));
7310               if (!tmp
7311                   || TREE_CODE (tmp) != INTEGER_CST
7312                   || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
7313                 continue;
7314             }
7315
7316           break;
7317         }
7318       else
7319         mdim = false;
7320
7321       if (!handled_component_p (ref))
7322         return NULL_TREE;
7323     }
7324
7325   /* We found the suitable array reference.  So copy everything up to it,
7326      and replace the index.  */
7327
7328   pref = TREE_OPERAND (addr, 0);
7329   ret = copy_node (pref);
7330   pos = ret;
7331
7332   while (pref != ref)
7333     {
7334       pref = TREE_OPERAND (pref, 0);
7335       TREE_OPERAND (pos, 0) = copy_node (pref);
7336       pos = TREE_OPERAND (pos, 0);
7337     }
7338
7339   TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
7340                                        fold_convert (itype,
7341                                                      TREE_OPERAND (pos, 1)),
7342                                        fold_convert (itype, delta));
7343
7344   return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
7345 }
7346
7347
7348 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
7349    means A >= Y && A != MAX, but in this case we know that
7350    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
7351
7352 static tree
7353 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
7354 {
7355   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7356
7357   if (TREE_CODE (bound) == LT_EXPR)
7358     a = TREE_OPERAND (bound, 0);
7359   else if (TREE_CODE (bound) == GT_EXPR)
7360     a = TREE_OPERAND (bound, 1);
7361   else
7362     return NULL_TREE;
7363
7364   typea = TREE_TYPE (a);
7365   if (!INTEGRAL_TYPE_P (typea)
7366       && !POINTER_TYPE_P (typea))
7367     return NULL_TREE;
7368
7369   if (TREE_CODE (ineq) == LT_EXPR)
7370     {
7371       a1 = TREE_OPERAND (ineq, 1);
7372       y = TREE_OPERAND (ineq, 0);
7373     }
7374   else if (TREE_CODE (ineq) == GT_EXPR)
7375     {
7376       a1 = TREE_OPERAND (ineq, 0);
7377       y = TREE_OPERAND (ineq, 1);
7378     }
7379   else
7380     return NULL_TREE;
7381
7382   if (TREE_TYPE (a1) != typea)
7383     return NULL_TREE;
7384
7385   if (POINTER_TYPE_P (typea))
7386     {
7387       /* Convert the pointer types into integer before taking the difference.  */
7388       tree ta = fold_convert (ssizetype, a);
7389       tree ta1 = fold_convert (ssizetype, a1);
7390       diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7391     }
7392   else
7393    diff = fold_binary (MINUS_EXPR, typea, a1, a);
7394
7395   if (!diff || !integer_onep (diff))
7396    return NULL_TREE;
7397
7398   return fold_build2 (GE_EXPR, type, a, y);
7399 }
7400
7401 /* Fold a sum or difference of at least one multiplication.
7402    Returns the folded tree or NULL if no simplification could be made.  */
7403
7404 static tree
7405 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7406 {
7407   tree arg00, arg01, arg10, arg11;
7408   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7409
7410   /* (A * C) +- (B * C) -> (A+-B) * C.
7411      (A * C) +- A -> A * (C+-1).
7412      We are most concerned about the case where C is a constant,
7413      but other combinations show up during loop reduction.  Since
7414      it is not difficult, try all four possibilities.  */
7415
7416   if (TREE_CODE (arg0) == MULT_EXPR)
7417     {
7418       arg00 = TREE_OPERAND (arg0, 0);
7419       arg01 = TREE_OPERAND (arg0, 1);
7420     }
7421   else if (TREE_CODE (arg0) == INTEGER_CST)
7422     {
7423       arg00 = build_one_cst (type);
7424       arg01 = arg0;
7425     }
7426   else
7427     {
7428       /* We cannot generate constant 1 for fract.  */
7429       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7430         return NULL_TREE;
7431       arg00 = arg0;
7432       arg01 = build_one_cst (type);
7433     }
7434   if (TREE_CODE (arg1) == MULT_EXPR)
7435     {
7436       arg10 = TREE_OPERAND (arg1, 0);
7437       arg11 = TREE_OPERAND (arg1, 1);
7438     }
7439   else if (TREE_CODE (arg1) == INTEGER_CST)
7440     {
7441       arg10 = build_one_cst (type);
7442       arg11 = arg1;
7443     }
7444   else
7445     {
7446       /* We cannot generate constant 1 for fract.  */
7447       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7448         return NULL_TREE;
7449       arg10 = arg1;
7450       arg11 = build_one_cst (type);
7451     }
7452   same = NULL_TREE;
7453
7454   if (operand_equal_p (arg01, arg11, 0))
7455     same = arg01, alt0 = arg00, alt1 = arg10;
7456   else if (operand_equal_p (arg00, arg10, 0))
7457     same = arg00, alt0 = arg01, alt1 = arg11;
7458   else if (operand_equal_p (arg00, arg11, 0))
7459     same = arg00, alt0 = arg01, alt1 = arg10;
7460   else if (operand_equal_p (arg01, arg10, 0))
7461     same = arg01, alt0 = arg00, alt1 = arg11;
7462
7463   /* No identical multiplicands; see if we can find a common
7464      power-of-two factor in non-power-of-two multiplies.  This
7465      can help in multi-dimensional array access.  */
7466   else if (host_integerp (arg01, 0)
7467            && host_integerp (arg11, 0))
7468     {
7469       HOST_WIDE_INT int01, int11, tmp;
7470       bool swap = false;
7471       tree maybe_same;
7472       int01 = TREE_INT_CST_LOW (arg01);
7473       int11 = TREE_INT_CST_LOW (arg11);
7474
7475       /* Move min of absolute values to int11.  */
7476       if ((int01 >= 0 ? int01 : -int01)
7477           < (int11 >= 0 ? int11 : -int11))
7478         {
7479           tmp = int01, int01 = int11, int11 = tmp;
7480           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7481           maybe_same = arg01;
7482           swap = true;
7483         }
7484       else
7485         maybe_same = arg11;
7486
7487       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
7488         {
7489           alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7490                               build_int_cst (TREE_TYPE (arg00),
7491                                              int01 / int11));
7492           alt1 = arg10;
7493           same = maybe_same;
7494           if (swap)
7495             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7496         }
7497     }
7498
7499   if (same)
7500     return fold_build2 (MULT_EXPR, type,
7501                         fold_build2 (code, type,
7502                                      fold_convert (type, alt0),
7503                                      fold_convert (type, alt1)),
7504                         fold_convert (type, same));
7505
7506   return NULL_TREE;
7507 }
7508
7509 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7510    specified by EXPR into the buffer PTR of length LEN bytes.
7511    Return the number of bytes placed in the buffer, or zero
7512    upon failure.  */
7513
7514 static int
7515 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7516 {
7517   tree type = TREE_TYPE (expr);
7518   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7519   int byte, offset, word, words;
7520   unsigned char value;
7521
7522   if (total_bytes > len)
7523     return 0;
7524   words = total_bytes / UNITS_PER_WORD;
7525
7526   for (byte = 0; byte < total_bytes; byte++)
7527     {
7528       int bitpos = byte * BITS_PER_UNIT;
7529       if (bitpos < HOST_BITS_PER_WIDE_INT)
7530         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7531       else
7532         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7533                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7534
7535       if (total_bytes > UNITS_PER_WORD)
7536         {
7537           word = byte / UNITS_PER_WORD;
7538           if (WORDS_BIG_ENDIAN)
7539             word = (words - 1) - word;
7540           offset = word * UNITS_PER_WORD;
7541           if (BYTES_BIG_ENDIAN)
7542             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7543           else
7544             offset += byte % UNITS_PER_WORD;
7545         }
7546       else
7547         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7548       ptr[offset] = value;
7549     }
7550   return total_bytes;
7551 }
7552
7553
7554 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7555    specified by EXPR into the buffer PTR of length LEN bytes.
7556    Return the number of bytes placed in the buffer, or zero
7557    upon failure.  */
7558
7559 static int
7560 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7561 {
7562   tree type = TREE_TYPE (expr);
7563   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7564   int byte, offset, word, words, bitpos;
7565   unsigned char value;
7566
7567   /* There are always 32 bits in each long, no matter the size of
7568      the hosts long.  We handle floating point representations with
7569      up to 192 bits.  */
7570   long tmp[6];
7571
7572   if (total_bytes > len)
7573     return 0;
7574   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7575
7576   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7577
7578   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7579        bitpos += BITS_PER_UNIT)
7580     {
7581       byte = (bitpos / BITS_PER_UNIT) & 3;
7582       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7583
7584       if (UNITS_PER_WORD < 4)
7585         {
7586           word = byte / UNITS_PER_WORD;
7587           if (WORDS_BIG_ENDIAN)
7588             word = (words - 1) - word;
7589           offset = word * UNITS_PER_WORD;
7590           if (BYTES_BIG_ENDIAN)
7591             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7592           else
7593             offset += byte % UNITS_PER_WORD;
7594         }
7595       else
7596         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7597       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7598     }
7599   return total_bytes;
7600 }
7601
7602 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7603    specified by EXPR into the buffer PTR of length LEN bytes.
7604    Return the number of bytes placed in the buffer, or zero
7605    upon failure.  */
7606
7607 static int
7608 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7609 {
7610   int rsize, isize;
7611   tree part;
7612
7613   part = TREE_REALPART (expr);
7614   rsize = native_encode_expr (part, ptr, len);
7615   if (rsize == 0)
7616     return 0;
7617   part = TREE_IMAGPART (expr);
7618   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7619   if (isize != rsize)
7620     return 0;
7621   return rsize + isize;
7622 }
7623
7624
7625 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7626    specified by EXPR into the buffer PTR of length LEN bytes.
7627    Return the number of bytes placed in the buffer, or zero
7628    upon failure.  */
7629
7630 static int
7631 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7632 {
7633   int i, size, offset, count;
7634   tree itype, elem, elements;
7635
7636   offset = 0;
7637   elements = TREE_VECTOR_CST_ELTS (expr);
7638   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7639   itype = TREE_TYPE (TREE_TYPE (expr));
7640   size = GET_MODE_SIZE (TYPE_MODE (itype));
7641   for (i = 0; i < count; i++)
7642     {
7643       if (elements)
7644         {
7645           elem = TREE_VALUE (elements);
7646           elements = TREE_CHAIN (elements);
7647         }
7648       else
7649         elem = NULL_TREE;
7650
7651       if (elem)
7652         {
7653           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7654             return 0;
7655         }
7656       else
7657         {
7658           if (offset + size > len)
7659             return 0;
7660           memset (ptr+offset, 0, size);
7661         }
7662       offset += size;
7663     }
7664   return offset;
7665 }
7666
7667
7668 /* Subroutine of native_encode_expr.  Encode the STRING_CST
7669    specified by EXPR into the buffer PTR of length LEN bytes.
7670    Return the number of bytes placed in the buffer, or zero
7671    upon failure.  */
7672
7673 static int
7674 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7675 {
7676   tree type = TREE_TYPE (expr);
7677   HOST_WIDE_INT total_bytes;
7678
7679   if (TREE_CODE (type) != ARRAY_TYPE
7680       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7681       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7682       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7683     return 0;
7684   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7685   if (total_bytes > len)
7686     return 0;
7687   if (TREE_STRING_LENGTH (expr) < total_bytes)
7688     {
7689       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7690       memset (ptr + TREE_STRING_LENGTH (expr), 0,
7691               total_bytes - TREE_STRING_LENGTH (expr));
7692     }
7693   else
7694     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7695   return total_bytes;
7696 }
7697
7698
7699 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7700    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7701    buffer PTR of length LEN bytes.  Return the number of bytes
7702    placed in the buffer, or zero upon failure.  */
7703
7704 int
7705 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7706 {
7707   switch (TREE_CODE (expr))
7708     {
7709     case INTEGER_CST:
7710       return native_encode_int (expr, ptr, len);
7711
7712     case REAL_CST:
7713       return native_encode_real (expr, ptr, len);
7714
7715     case COMPLEX_CST:
7716       return native_encode_complex (expr, ptr, len);
7717
7718     case VECTOR_CST:
7719       return native_encode_vector (expr, ptr, len);
7720
7721     case STRING_CST:
7722       return native_encode_string (expr, ptr, len);
7723
7724     default:
7725       return 0;
7726     }
7727 }
7728
7729
7730 /* Subroutine of native_interpret_expr.  Interpret the contents of
7731    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7732    If the buffer cannot be interpreted, return NULL_TREE.  */
7733
7734 static tree
7735 native_interpret_int (tree type, const unsigned char *ptr, int len)
7736 {
7737   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7738   int byte, offset, word, words;
7739   unsigned char value;
7740   unsigned int HOST_WIDE_INT lo = 0;
7741   HOST_WIDE_INT hi = 0;
7742
7743   if (total_bytes > len)
7744     return NULL_TREE;
7745   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7746     return NULL_TREE;
7747   words = total_bytes / UNITS_PER_WORD;
7748
7749   for (byte = 0; byte < total_bytes; byte++)
7750     {
7751       int bitpos = byte * BITS_PER_UNIT;
7752       if (total_bytes > UNITS_PER_WORD)
7753         {
7754           word = byte / UNITS_PER_WORD;
7755           if (WORDS_BIG_ENDIAN)
7756             word = (words - 1) - word;
7757           offset = word * UNITS_PER_WORD;
7758           if (BYTES_BIG_ENDIAN)
7759             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7760           else
7761             offset += byte % UNITS_PER_WORD;
7762         }
7763       else
7764         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7765       value = ptr[offset];
7766
7767       if (bitpos < HOST_BITS_PER_WIDE_INT)
7768         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7769       else
7770         hi |= (unsigned HOST_WIDE_INT) value
7771               << (bitpos - HOST_BITS_PER_WIDE_INT);
7772     }
7773
7774   return build_int_cst_wide_type (type, lo, hi);
7775 }
7776
7777
7778 /* Subroutine of native_interpret_expr.  Interpret the contents of
7779    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7780    If the buffer cannot be interpreted, return NULL_TREE.  */
7781
7782 static tree
7783 native_interpret_real (tree type, const unsigned char *ptr, int len)
7784 {
7785   enum machine_mode mode = TYPE_MODE (type);
7786   int total_bytes = GET_MODE_SIZE (mode);
7787   int byte, offset, word, words, bitpos;
7788   unsigned char value;
7789   /* There are always 32 bits in each long, no matter the size of
7790      the hosts long.  We handle floating point representations with
7791      up to 192 bits.  */
7792   REAL_VALUE_TYPE r;
7793   long tmp[6];
7794
7795   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7796   if (total_bytes > len || total_bytes > 24)
7797     return NULL_TREE;
7798   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7799
7800   memset (tmp, 0, sizeof (tmp));
7801   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7802        bitpos += BITS_PER_UNIT)
7803     {
7804       byte = (bitpos / BITS_PER_UNIT) & 3;
7805       if (UNITS_PER_WORD < 4)
7806         {
7807           word = byte / UNITS_PER_WORD;
7808           if (WORDS_BIG_ENDIAN)
7809             word = (words - 1) - word;
7810           offset = word * UNITS_PER_WORD;
7811           if (BYTES_BIG_ENDIAN)
7812             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7813           else
7814             offset += byte % UNITS_PER_WORD;
7815         }
7816       else
7817         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7818       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7819
7820       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7821     }
7822
7823   real_from_target (&r, tmp, mode);
7824   return build_real (type, r);
7825 }
7826
7827
7828 /* Subroutine of native_interpret_expr.  Interpret the contents of
7829    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7830    If the buffer cannot be interpreted, return NULL_TREE.  */
7831
7832 static tree
7833 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7834 {
7835   tree etype, rpart, ipart;
7836   int size;
7837
7838   etype = TREE_TYPE (type);
7839   size = GET_MODE_SIZE (TYPE_MODE (etype));
7840   if (size * 2 > len)
7841     return NULL_TREE;
7842   rpart = native_interpret_expr (etype, ptr, size);
7843   if (!rpart)
7844     return NULL_TREE;
7845   ipart = native_interpret_expr (etype, ptr+size, size);
7846   if (!ipart)
7847     return NULL_TREE;
7848   return build_complex (type, rpart, ipart);
7849 }
7850
7851
7852 /* Subroutine of native_interpret_expr.  Interpret the contents of
7853    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7854    If the buffer cannot be interpreted, return NULL_TREE.  */
7855
7856 static tree
7857 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7858 {
7859   tree etype, elem, elements;
7860   int i, size, count;
7861
7862   etype = TREE_TYPE (type);
7863   size = GET_MODE_SIZE (TYPE_MODE (etype));
7864   count = TYPE_VECTOR_SUBPARTS (type);
7865   if (size * count > len)
7866     return NULL_TREE;
7867
7868   elements = NULL_TREE;
7869   for (i = count - 1; i >= 0; i--)
7870     {
7871       elem = native_interpret_expr (etype, ptr+(i*size), size);
7872       if (!elem)
7873         return NULL_TREE;
7874       elements = tree_cons (NULL_TREE, elem, elements);
7875     }
7876   return build_vector (type, elements);
7877 }
7878
7879
7880 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7881    the buffer PTR of length LEN as a constant of type TYPE.  For
7882    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7883    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7884    return NULL_TREE.  */
7885
7886 tree
7887 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7888 {
7889   switch (TREE_CODE (type))
7890     {
7891     case INTEGER_TYPE:
7892     case ENUMERAL_TYPE:
7893     case BOOLEAN_TYPE:
7894       return native_interpret_int (type, ptr, len);
7895
7896     case REAL_TYPE:
7897       return native_interpret_real (type, ptr, len);
7898
7899     case COMPLEX_TYPE:
7900       return native_interpret_complex (type, ptr, len);
7901
7902     case VECTOR_TYPE:
7903       return native_interpret_vector (type, ptr, len);
7904
7905     default:
7906       return NULL_TREE;
7907     }
7908 }
7909
7910
7911 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7912    TYPE at compile-time.  If we're unable to perform the conversion
7913    return NULL_TREE.  */
7914
7915 static tree
7916 fold_view_convert_expr (tree type, tree expr)
7917 {
7918   /* We support up to 512-bit values (for V8DFmode).  */
7919   unsigned char buffer[64];
7920   int len;
7921
7922   /* Check that the host and target are sane.  */
7923   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7924     return NULL_TREE;
7925
7926   len = native_encode_expr (expr, buffer, sizeof (buffer));
7927   if (len == 0)
7928     return NULL_TREE;
7929
7930   return native_interpret_expr (type, buffer, len);
7931 }
7932
7933 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7934    to avoid confusing the gimplify process.  When IN_FOLD is true
7935    avoid modifications of T.  */
7936
7937 static tree
7938 build_fold_addr_expr_with_type_1 (tree t, tree ptrtype, bool in_fold)
7939 {
7940   /* The size of the object is not relevant when talking about its address.  */
7941   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7942     t = TREE_OPERAND (t, 0);
7943
7944   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7945   if (TREE_CODE (t) == INDIRECT_REF
7946       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7947     {
7948       t = TREE_OPERAND (t, 0);
7949
7950       if (TREE_TYPE (t) != ptrtype)
7951         t = build1 (NOP_EXPR, ptrtype, t);
7952     }
7953   else if (!in_fold)
7954     {
7955       tree base = t;
7956
7957       while (handled_component_p (base))
7958         base = TREE_OPERAND (base, 0);
7959
7960       if (DECL_P (base))
7961         TREE_ADDRESSABLE (base) = 1;
7962
7963       t = build1 (ADDR_EXPR, ptrtype, t);
7964     }
7965   else
7966     t = build1 (ADDR_EXPR, ptrtype, t);
7967
7968   return t;
7969 }
7970
7971 /* Build an expression for the address of T with type PTRTYPE.  This
7972    function modifies the input parameter 'T' by sometimes setting the
7973    TREE_ADDRESSABLE flag.  */
7974
7975 tree
7976 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7977 {
7978   return build_fold_addr_expr_with_type_1 (t, ptrtype, false);
7979 }
7980
7981 /* Build an expression for the address of T.  This function modifies
7982    the input parameter 'T' by sometimes setting the TREE_ADDRESSABLE
7983    flag.  When called from fold functions, use fold_addr_expr instead.  */
7984
7985 tree
7986 build_fold_addr_expr (tree t)
7987 {
7988   return build_fold_addr_expr_with_type_1 (t, 
7989                                            build_pointer_type (TREE_TYPE (t)),
7990                                            false);
7991 }
7992
7993 /* Same as build_fold_addr_expr, builds an expression for the address
7994    of T, but avoids touching the input node 't'.  Fold functions
7995    should use this version.  */
7996
7997 static tree
7998 fold_addr_expr (tree t)
7999 {
8000   tree ptrtype = build_pointer_type (TREE_TYPE (t));
8001
8002   return build_fold_addr_expr_with_type_1 (t, ptrtype, true);
8003 }
8004
8005 /* Fold a unary expression of code CODE and type TYPE with operand
8006    OP0.  Return the folded expression if folding is successful.
8007    Otherwise, return NULL_TREE.  */
8008
8009 tree
8010 fold_unary (enum tree_code code, tree type, tree op0)
8011 {
8012   tree tem;
8013   tree arg0;
8014   enum tree_code_class kind = TREE_CODE_CLASS (code);
8015
8016   gcc_assert (IS_EXPR_CODE_CLASS (kind)
8017               && TREE_CODE_LENGTH (code) == 1);
8018
8019   arg0 = op0;
8020   if (arg0)
8021     {
8022       if (CONVERT_EXPR_CODE_P (code)
8023           || code == FLOAT_EXPR || code == ABS_EXPR)
8024         {
8025           /* Don't use STRIP_NOPS, because signedness of argument type
8026              matters.  */
8027           STRIP_SIGN_NOPS (arg0);
8028         }
8029       else
8030         {
8031           /* Strip any conversions that don't change the mode.  This
8032              is safe for every expression, except for a comparison
8033              expression because its signedness is derived from its
8034              operands.
8035
8036              Note that this is done as an internal manipulation within
8037              the constant folder, in order to find the simplest
8038              representation of the arguments so that their form can be
8039              studied.  In any cases, the appropriate type conversions
8040              should be put back in the tree that will get out of the
8041              constant folder.  */
8042           STRIP_NOPS (arg0);
8043         }
8044     }
8045
8046   if (TREE_CODE_CLASS (code) == tcc_unary)
8047     {
8048       if (TREE_CODE (arg0) == COMPOUND_EXPR)
8049         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
8050                        fold_build1 (code, type, TREE_OPERAND (arg0, 1)));
8051       else if (TREE_CODE (arg0) == COND_EXPR)
8052         {
8053           tree arg01 = TREE_OPERAND (arg0, 1);
8054           tree arg02 = TREE_OPERAND (arg0, 2);
8055           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8056             arg01 = fold_build1 (code, type, arg01);
8057           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8058             arg02 = fold_build1 (code, type, arg02);
8059           tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
8060                              arg01, arg02);
8061
8062           /* If this was a conversion, and all we did was to move into
8063              inside the COND_EXPR, bring it back out.  But leave it if
8064              it is a conversion from integer to integer and the
8065              result precision is no wider than a word since such a
8066              conversion is cheap and may be optimized away by combine,
8067              while it couldn't if it were outside the COND_EXPR.  Then return
8068              so we don't get into an infinite recursion loop taking the
8069              conversion out and then back in.  */
8070
8071           if ((CONVERT_EXPR_CODE_P (code)
8072                || code == NON_LVALUE_EXPR)
8073               && TREE_CODE (tem) == COND_EXPR
8074               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8075               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8076               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8077               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8078               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8079                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8080               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8081                      && (INTEGRAL_TYPE_P
8082                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8083                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8084                   || flag_syntax_only))
8085             tem = build1 (code, type,
8086                           build3 (COND_EXPR,
8087                                   TREE_TYPE (TREE_OPERAND
8088                                              (TREE_OPERAND (tem, 1), 0)),
8089                                   TREE_OPERAND (tem, 0),
8090                                   TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8091                                   TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8092           return tem;
8093         }
8094       else if (COMPARISON_CLASS_P (arg0))
8095         {
8096           if (TREE_CODE (type) == BOOLEAN_TYPE)
8097             {
8098               arg0 = copy_node (arg0);
8099               TREE_TYPE (arg0) = type;
8100               return arg0;
8101             }
8102           else if (TREE_CODE (type) != INTEGER_TYPE)
8103             return fold_build3 (COND_EXPR, type, arg0,
8104                                 fold_build1 (code, type,
8105                                              integer_one_node),
8106                                 fold_build1 (code, type,
8107                                              integer_zero_node));
8108         }
8109    }
8110
8111   switch (code)
8112     {
8113     case PAREN_EXPR:
8114       /* Re-association barriers around constants and other re-association
8115          barriers can be removed.  */
8116       if (CONSTANT_CLASS_P (op0)
8117           || TREE_CODE (op0) == PAREN_EXPR)
8118         return fold_convert (type, op0);
8119       return NULL_TREE;
8120
8121     CASE_CONVERT:
8122     case FLOAT_EXPR:
8123     case FIX_TRUNC_EXPR:
8124       if (TREE_TYPE (op0) == type)
8125         return op0;
8126       
8127       /* If we have (type) (a CMP b) and type is an integral type, return
8128          new expression involving the new type.  */
8129       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8130         return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8131                             TREE_OPERAND (op0, 1));
8132
8133       /* Handle cases of two conversions in a row.  */
8134       if (CONVERT_EXPR_P (op0))
8135         {
8136           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8137           tree inter_type = TREE_TYPE (op0);
8138           int inside_int = INTEGRAL_TYPE_P (inside_type);
8139           int inside_ptr = POINTER_TYPE_P (inside_type);
8140           int inside_float = FLOAT_TYPE_P (inside_type);
8141           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8142           unsigned int inside_prec = TYPE_PRECISION (inside_type);
8143           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8144           int inter_int = INTEGRAL_TYPE_P (inter_type);
8145           int inter_ptr = POINTER_TYPE_P (inter_type);
8146           int inter_float = FLOAT_TYPE_P (inter_type);
8147           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8148           unsigned int inter_prec = TYPE_PRECISION (inter_type);
8149           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8150           int final_int = INTEGRAL_TYPE_P (type);
8151           int final_ptr = POINTER_TYPE_P (type);
8152           int final_float = FLOAT_TYPE_P (type);
8153           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8154           unsigned int final_prec = TYPE_PRECISION (type);
8155           int final_unsignedp = TYPE_UNSIGNED (type);
8156
8157           /* In addition to the cases of two conversions in a row
8158              handled below, if we are converting something to its own
8159              type via an object of identical or wider precision, neither
8160              conversion is needed.  */
8161           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8162               && (((inter_int || inter_ptr) && final_int)
8163                   || (inter_float && final_float))
8164               && inter_prec >= final_prec)
8165             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8166
8167           /* Likewise, if the intermediate and final types are either both
8168              float or both integer, we don't need the middle conversion if
8169              it is wider than the final type and doesn't change the signedness
8170              (for integers).  Avoid this if the final type is a pointer
8171              since then we sometimes need the inner conversion.  Likewise if
8172              the outer has a precision not equal to the size of its mode.  */
8173           if (((inter_int && inside_int)
8174                || (inter_float && inside_float)
8175                || (inter_vec && inside_vec))
8176               && inter_prec >= inside_prec
8177               && (inter_float || inter_vec
8178                   || inter_unsignedp == inside_unsignedp)
8179               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8180                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
8181               && ! final_ptr
8182               && (! final_vec || inter_prec == inside_prec))
8183             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8184
8185           /* If we have a sign-extension of a zero-extended value, we can
8186              replace that by a single zero-extension.  */
8187           if (inside_int && inter_int && final_int
8188               && inside_prec < inter_prec && inter_prec < final_prec
8189               && inside_unsignedp && !inter_unsignedp)
8190             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8191
8192           /* Two conversions in a row are not needed unless:
8193              - some conversion is floating-point (overstrict for now), or
8194              - some conversion is a vector (overstrict for now), or
8195              - the intermediate type is narrower than both initial and
8196                final, or
8197              - the intermediate type and innermost type differ in signedness,
8198                and the outermost type is wider than the intermediate, or
8199              - the initial type is a pointer type and the precisions of the
8200                intermediate and final types differ, or
8201              - the final type is a pointer type and the precisions of the
8202                initial and intermediate types differ.  */
8203           if (! inside_float && ! inter_float && ! final_float
8204               && ! inside_vec && ! inter_vec && ! final_vec
8205               && (inter_prec >= inside_prec || inter_prec >= final_prec)
8206               && ! (inside_int && inter_int
8207                     && inter_unsignedp != inside_unsignedp
8208                     && inter_prec < final_prec)
8209               && ((inter_unsignedp && inter_prec > inside_prec)
8210                   == (final_unsignedp && final_prec > inter_prec))
8211               && ! (inside_ptr && inter_prec != final_prec)
8212               && ! (final_ptr && inside_prec != inter_prec)
8213               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8214                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
8215             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8216         }
8217
8218       /* Handle (T *)&A.B.C for A being of type T and B and C
8219          living at offset zero.  This occurs frequently in
8220          C++ upcasting and then accessing the base.  */
8221       if (TREE_CODE (op0) == ADDR_EXPR
8222           && POINTER_TYPE_P (type)
8223           && handled_component_p (TREE_OPERAND (op0, 0)))
8224         {
8225           HOST_WIDE_INT bitsize, bitpos;
8226           tree offset;
8227           enum machine_mode mode;
8228           int unsignedp, volatilep;
8229           tree base = TREE_OPERAND (op0, 0);
8230           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8231                                       &mode, &unsignedp, &volatilep, false);
8232           /* If the reference was to a (constant) zero offset, we can use
8233              the address of the base if it has the same base type
8234              as the result type.  */
8235           if (! offset && bitpos == 0
8236               && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8237                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8238             return fold_convert (type, fold_addr_expr (base));
8239         }
8240
8241       if (TREE_CODE (op0) == MODIFY_EXPR
8242           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
8243           /* Detect assigning a bitfield.  */
8244           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
8245                && DECL_BIT_FIELD
8246                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
8247         {
8248           /* Don't leave an assignment inside a conversion
8249              unless assigning a bitfield.  */
8250           tem = fold_build1 (code, type, TREE_OPERAND (op0, 1));
8251           /* First do the assignment, then return converted constant.  */
8252           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8253           TREE_NO_WARNING (tem) = 1;
8254           TREE_USED (tem) = 1;
8255           return tem;
8256         }
8257
8258       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8259          constants (if x has signed type, the sign bit cannot be set
8260          in c).  This folds extension into the BIT_AND_EXPR.
8261          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8262          very likely don't have maximal range for their precision and this
8263          transformation effectively doesn't preserve non-maximal ranges.  */
8264       if (TREE_CODE (type) == INTEGER_TYPE
8265           && TREE_CODE (op0) == BIT_AND_EXPR
8266           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST
8267           /* Not if the conversion is to the sub-type.  */
8268           && TREE_TYPE (type) != TREE_TYPE (op0))
8269         {
8270           tree and = op0;
8271           tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
8272           int change = 0;
8273
8274           if (TYPE_UNSIGNED (TREE_TYPE (and))
8275               || (TYPE_PRECISION (type)
8276                   <= TYPE_PRECISION (TREE_TYPE (and))))
8277             change = 1;
8278           else if (TYPE_PRECISION (TREE_TYPE (and1))
8279                    <= HOST_BITS_PER_WIDE_INT
8280                    && host_integerp (and1, 1))
8281             {
8282               unsigned HOST_WIDE_INT cst;
8283
8284               cst = tree_low_cst (and1, 1);
8285               cst &= (HOST_WIDE_INT) -1
8286                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8287               change = (cst == 0);
8288 #ifdef LOAD_EXTEND_OP
8289               if (change
8290                   && !flag_syntax_only
8291                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8292                       == ZERO_EXTEND))
8293                 {
8294                   tree uns = unsigned_type_for (TREE_TYPE (and0));
8295                   and0 = fold_convert (uns, and0);
8296                   and1 = fold_convert (uns, and1);
8297                 }
8298 #endif
8299             }
8300           if (change)
8301             {
8302               tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8303                                            TREE_INT_CST_HIGH (and1), 0,
8304                                            TREE_OVERFLOW (and1));
8305               return fold_build2 (BIT_AND_EXPR, type,
8306                                   fold_convert (type, and0), tem);
8307             }
8308         }
8309
8310       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8311          when one of the new casts will fold away. Conservatively we assume
8312          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8313       if (POINTER_TYPE_P (type)
8314           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8315           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8316               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8317               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8318         {
8319           tree arg00 = TREE_OPERAND (arg0, 0);
8320           tree arg01 = TREE_OPERAND (arg0, 1);
8321
8322           return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
8323                               fold_convert (sizetype, arg01));
8324         }
8325
8326       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8327          of the same precision, and X is an integer type not narrower than
8328          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
8329       if (INTEGRAL_TYPE_P (type)
8330           && TREE_CODE (op0) == BIT_NOT_EXPR
8331           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8332           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8333           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8334         {
8335           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8336           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8337               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8338             return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
8339         }
8340
8341       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8342          type of X and Y (integer types only).  */
8343       if (INTEGRAL_TYPE_P (type)
8344           && TREE_CODE (op0) == MULT_EXPR
8345           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8346           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8347         {
8348           /* Be careful not to introduce new overflows.  */
8349           tree mult_type;
8350           if (TYPE_OVERFLOW_WRAPS (type))
8351             mult_type = type;
8352           else
8353             mult_type = unsigned_type_for (type);
8354
8355           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8356             {
8357               tem = fold_build2 (MULT_EXPR, mult_type,
8358                                  fold_convert (mult_type,
8359                                                TREE_OPERAND (op0, 0)),
8360                                  fold_convert (mult_type,
8361                                                TREE_OPERAND (op0, 1)));
8362               return fold_convert (type, tem);
8363             }
8364         }
8365
8366       tem = fold_convert_const (code, type, op0);
8367       return tem ? tem : NULL_TREE;
8368
8369     case FIXED_CONVERT_EXPR:
8370       tem = fold_convert_const (code, type, arg0);
8371       return tem ? tem : NULL_TREE;
8372
8373     case VIEW_CONVERT_EXPR:
8374       if (TREE_TYPE (op0) == type)
8375         return op0;
8376       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8377         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8378
8379       /* For integral conversions with the same precision or pointer
8380          conversions use a NOP_EXPR instead.  */
8381       if ((INTEGRAL_TYPE_P (type)
8382            || POINTER_TYPE_P (type))
8383           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8384               || POINTER_TYPE_P (TREE_TYPE (op0)))
8385           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))
8386           /* Do not muck with VIEW_CONVERT_EXPRs that convert from
8387              a sub-type to its base type as generated by the Ada FE.  */
8388           && !(INTEGRAL_TYPE_P (TREE_TYPE (op0))
8389                && TREE_TYPE (TREE_TYPE (op0))))
8390         return fold_convert (type, op0);
8391
8392       /* Strip inner integral conversions that do not change the precision.  */
8393       if (CONVERT_EXPR_P (op0)
8394           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8395               || POINTER_TYPE_P (TREE_TYPE (op0)))
8396           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8397               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8398           && (TYPE_PRECISION (TREE_TYPE (op0))
8399               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8400         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8401
8402       return fold_view_convert_expr (type, op0);
8403
8404     case NEGATE_EXPR:
8405       tem = fold_negate_expr (arg0);
8406       if (tem)
8407         return fold_convert (type, tem);
8408       return NULL_TREE;
8409
8410     case ABS_EXPR:
8411       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8412         return fold_abs_const (arg0, type);
8413       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8414         return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8415       /* Convert fabs((double)float) into (double)fabsf(float).  */
8416       else if (TREE_CODE (arg0) == NOP_EXPR
8417                && TREE_CODE (type) == REAL_TYPE)
8418         {
8419           tree targ0 = strip_float_extensions (arg0);
8420           if (targ0 != arg0)
8421             return fold_convert (type, fold_build1 (ABS_EXPR,
8422                                                     TREE_TYPE (targ0),
8423                                                     targ0));
8424         }
8425       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8426       else if (TREE_CODE (arg0) == ABS_EXPR)
8427         return arg0;
8428       else if (tree_expr_nonnegative_p (arg0))
8429         return arg0;
8430
8431       /* Strip sign ops from argument.  */
8432       if (TREE_CODE (type) == REAL_TYPE)
8433         {
8434           tem = fold_strip_sign_ops (arg0);
8435           if (tem)
8436             return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8437         }
8438       return NULL_TREE;
8439
8440     case CONJ_EXPR:
8441       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8442         return fold_convert (type, arg0);
8443       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8444         {
8445           tree itype = TREE_TYPE (type);
8446           tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8447           tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8448           return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8449         }
8450       if (TREE_CODE (arg0) == COMPLEX_CST)
8451         {
8452           tree itype = TREE_TYPE (type);
8453           tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8454           tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8455           return build_complex (type, rpart, negate_expr (ipart));
8456         }
8457       if (TREE_CODE (arg0) == CONJ_EXPR)
8458         return fold_convert (type, TREE_OPERAND (arg0, 0));
8459       return NULL_TREE;
8460
8461     case BIT_NOT_EXPR:
8462       if (TREE_CODE (arg0) == INTEGER_CST)
8463         return fold_not_const (arg0, type);
8464       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8465         return fold_convert (type, TREE_OPERAND (arg0, 0));
8466       /* Convert ~ (-A) to A - 1.  */
8467       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8468         return fold_build2 (MINUS_EXPR, type,
8469                             fold_convert (type, TREE_OPERAND (arg0, 0)),
8470                             build_int_cst (type, 1));
8471       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8472       else if (INTEGRAL_TYPE_P (type)
8473                && ((TREE_CODE (arg0) == MINUS_EXPR
8474                     && integer_onep (TREE_OPERAND (arg0, 1)))
8475                    || (TREE_CODE (arg0) == PLUS_EXPR
8476                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8477         return fold_build1 (NEGATE_EXPR, type,
8478                             fold_convert (type, TREE_OPERAND (arg0, 0)));
8479       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8480       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8481                && (tem = fold_unary (BIT_NOT_EXPR, type,
8482                                      fold_convert (type,
8483                                                    TREE_OPERAND (arg0, 0)))))
8484         return fold_build2 (BIT_XOR_EXPR, type, tem,
8485                             fold_convert (type, TREE_OPERAND (arg0, 1)));
8486       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8487                && (tem = fold_unary (BIT_NOT_EXPR, type,
8488                                      fold_convert (type,
8489                                                    TREE_OPERAND (arg0, 1)))))
8490         return fold_build2 (BIT_XOR_EXPR, type,
8491                             fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8492       /* Perform BIT_NOT_EXPR on each element individually.  */
8493       else if (TREE_CODE (arg0) == VECTOR_CST)
8494         {
8495           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8496           int count = TYPE_VECTOR_SUBPARTS (type), i;
8497
8498           for (i = 0; i < count; i++)
8499             {
8500               if (elements)
8501                 {
8502                   elem = TREE_VALUE (elements);
8503                   elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8504                   if (elem == NULL_TREE)
8505                     break;
8506                   elements = TREE_CHAIN (elements);
8507                 }
8508               else
8509                 elem = build_int_cst (TREE_TYPE (type), -1);
8510               list = tree_cons (NULL_TREE, elem, list);
8511             }
8512           if (i == count)
8513             return build_vector (type, nreverse (list));
8514         }
8515
8516       return NULL_TREE;
8517
8518     case TRUTH_NOT_EXPR:
8519       /* The argument to invert_truthvalue must have Boolean type.  */
8520       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8521           arg0 = fold_convert (boolean_type_node, arg0);
8522
8523       /* Note that the operand of this must be an int
8524          and its values must be 0 or 1.
8525          ("true" is a fixed value perhaps depending on the language,
8526          but we don't handle values other than 1 correctly yet.)  */
8527       tem = fold_truth_not_expr (arg0);
8528       if (!tem)
8529         return NULL_TREE;
8530       return fold_convert (type, tem);
8531
8532     case REALPART_EXPR:
8533       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8534         return fold_convert (type, arg0);
8535       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8536         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8537                                  TREE_OPERAND (arg0, 1));
8538       if (TREE_CODE (arg0) == COMPLEX_CST)
8539         return fold_convert (type, TREE_REALPART (arg0));
8540       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8541         {
8542           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8543           tem = fold_build2 (TREE_CODE (arg0), itype,
8544                              fold_build1 (REALPART_EXPR, itype,
8545                                           TREE_OPERAND (arg0, 0)),
8546                              fold_build1 (REALPART_EXPR, itype,
8547                                           TREE_OPERAND (arg0, 1)));
8548           return fold_convert (type, tem);
8549         }
8550       if (TREE_CODE (arg0) == CONJ_EXPR)
8551         {
8552           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8553           tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8554           return fold_convert (type, tem);
8555         }
8556       if (TREE_CODE (arg0) == CALL_EXPR)
8557         {
8558           tree fn = get_callee_fndecl (arg0);
8559           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8560             switch (DECL_FUNCTION_CODE (fn))
8561               {
8562               CASE_FLT_FN (BUILT_IN_CEXPI):
8563                 fn = mathfn_built_in (type, BUILT_IN_COS);
8564                 if (fn)
8565                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8566                 break;
8567
8568               default:
8569                 break;
8570               }
8571         }
8572       return NULL_TREE;
8573
8574     case IMAGPART_EXPR:
8575       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8576         return fold_convert (type, integer_zero_node);
8577       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8578         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8579                                  TREE_OPERAND (arg0, 0));
8580       if (TREE_CODE (arg0) == COMPLEX_CST)
8581         return fold_convert (type, TREE_IMAGPART (arg0));
8582       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8583         {
8584           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8585           tem = fold_build2 (TREE_CODE (arg0), itype,
8586                              fold_build1 (IMAGPART_EXPR, itype,
8587                                           TREE_OPERAND (arg0, 0)),
8588                              fold_build1 (IMAGPART_EXPR, itype,
8589                                           TREE_OPERAND (arg0, 1)));
8590           return fold_convert (type, tem);
8591         }
8592       if (TREE_CODE (arg0) == CONJ_EXPR)
8593         {
8594           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8595           tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8596           return fold_convert (type, negate_expr (tem));
8597         }
8598       if (TREE_CODE (arg0) == CALL_EXPR)
8599         {
8600           tree fn = get_callee_fndecl (arg0);
8601           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8602             switch (DECL_FUNCTION_CODE (fn))
8603               {
8604               CASE_FLT_FN (BUILT_IN_CEXPI):
8605                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8606                 if (fn)
8607                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8608                 break;
8609
8610               default:
8611                 break;
8612               }
8613         }
8614       return NULL_TREE;
8615
8616     default:
8617       return NULL_TREE;
8618     } /* switch (code) */
8619 }
8620
8621 /* Fold a binary expression of code CODE and type TYPE with operands
8622    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8623    Return the folded expression if folding is successful.  Otherwise,
8624    return NULL_TREE.  */
8625
8626 static tree
8627 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8628 {
8629   enum tree_code compl_code;
8630
8631   if (code == MIN_EXPR)
8632     compl_code = MAX_EXPR;
8633   else if (code == MAX_EXPR)
8634     compl_code = MIN_EXPR;
8635   else
8636     gcc_unreachable ();
8637
8638   /* MIN (MAX (a, b), b) == b.  */
8639   if (TREE_CODE (op0) == compl_code
8640       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8641     return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8642
8643   /* MIN (MAX (b, a), b) == b.  */
8644   if (TREE_CODE (op0) == compl_code
8645       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8646       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8647     return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8648
8649   /* MIN (a, MAX (a, b)) == a.  */
8650   if (TREE_CODE (op1) == compl_code
8651       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8652       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8653     return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8654
8655   /* MIN (a, MAX (b, a)) == a.  */
8656   if (TREE_CODE (op1) == compl_code
8657       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8658       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8659     return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8660
8661   return NULL_TREE;
8662 }
8663
8664 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8665    by changing CODE to reduce the magnitude of constants involved in
8666    ARG0 of the comparison.
8667    Returns a canonicalized comparison tree if a simplification was
8668    possible, otherwise returns NULL_TREE.
8669    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8670    valid if signed overflow is undefined.  */
8671
8672 static tree
8673 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8674                                  tree arg0, tree arg1,
8675                                  bool *strict_overflow_p)
8676 {
8677   enum tree_code code0 = TREE_CODE (arg0);
8678   tree t, cst0 = NULL_TREE;
8679   int sgn0;
8680   bool swap = false;
8681
8682   /* Match A +- CST code arg1 and CST code arg1.  We can change the
8683      first form only if overflow is undefined.  */
8684   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8685          /* In principle pointers also have undefined overflow behavior,
8686             but that causes problems elsewhere.  */
8687          && !POINTER_TYPE_P (TREE_TYPE (arg0))
8688          && (code0 == MINUS_EXPR
8689              || code0 == PLUS_EXPR)
8690          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8691         || code0 == INTEGER_CST))
8692     return NULL_TREE;
8693
8694   /* Identify the constant in arg0 and its sign.  */
8695   if (code0 == INTEGER_CST)
8696     cst0 = arg0;
8697   else
8698     cst0 = TREE_OPERAND (arg0, 1);
8699   sgn0 = tree_int_cst_sgn (cst0);
8700
8701   /* Overflowed constants and zero will cause problems.  */
8702   if (integer_zerop (cst0)
8703       || TREE_OVERFLOW (cst0))
8704     return NULL_TREE;
8705
8706   /* See if we can reduce the magnitude of the constant in
8707      arg0 by changing the comparison code.  */
8708   if (code0 == INTEGER_CST)
8709     {
8710       /* CST <= arg1  ->  CST-1 < arg1.  */
8711       if (code == LE_EXPR && sgn0 == 1)
8712         code = LT_EXPR;
8713       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8714       else if (code == LT_EXPR && sgn0 == -1)
8715         code = LE_EXPR;
8716       /* CST > arg1  ->  CST-1 >= arg1.  */
8717       else if (code == GT_EXPR && sgn0 == 1)
8718         code = GE_EXPR;
8719       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8720       else if (code == GE_EXPR && sgn0 == -1)
8721         code = GT_EXPR;
8722       else
8723         return NULL_TREE;
8724       /* arg1 code' CST' might be more canonical.  */
8725       swap = true;
8726     }
8727   else
8728     {
8729       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8730       if (code == LT_EXPR
8731           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8732         code = LE_EXPR;
8733       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8734       else if (code == GT_EXPR
8735                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8736         code = GE_EXPR;
8737       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8738       else if (code == LE_EXPR
8739                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8740         code = LT_EXPR;
8741       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8742       else if (code == GE_EXPR
8743                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8744         code = GT_EXPR;
8745       else
8746         return NULL_TREE;
8747       *strict_overflow_p = true;
8748     }
8749
8750   /* Now build the constant reduced in magnitude.  But not if that
8751      would produce one outside of its types range.  */
8752   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8753       && ((sgn0 == 1
8754            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8755            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8756           || (sgn0 == -1
8757               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8758               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8759     /* We cannot swap the comparison here as that would cause us to
8760        endlessly recurse.  */
8761     return NULL_TREE;
8762
8763   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8764                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8765   if (code0 != INTEGER_CST)
8766     t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8767
8768   /* If swapping might yield to a more canonical form, do so.  */
8769   if (swap)
8770     return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8771   else
8772     return fold_build2 (code, type, t, arg1);
8773 }
8774
8775 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8776    overflow further.  Try to decrease the magnitude of constants involved
8777    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8778    and put sole constants at the second argument position.
8779    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8780
8781 static tree
8782 maybe_canonicalize_comparison (enum tree_code code, tree type,
8783                                tree arg0, tree arg1)
8784 {
8785   tree t;
8786   bool strict_overflow_p;
8787   const char * const warnmsg = G_("assuming signed overflow does not occur "
8788                                   "when reducing constant in comparison");
8789
8790   /* Try canonicalization by simplifying arg0.  */
8791   strict_overflow_p = false;
8792   t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8793                                        &strict_overflow_p);
8794   if (t)
8795     {
8796       if (strict_overflow_p)
8797         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8798       return t;
8799     }
8800
8801   /* Try canonicalization by simplifying arg1 using the swapped
8802      comparison.  */
8803   code = swap_tree_comparison (code);
8804   strict_overflow_p = false;
8805   t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8806                                        &strict_overflow_p);
8807   if (t && strict_overflow_p)
8808     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8809   return t;
8810 }
8811
8812 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8813    space.  This is used to avoid issuing overflow warnings for
8814    expressions like &p->x which can not wrap.  */
8815
8816 static bool
8817 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8818 {
8819   unsigned HOST_WIDE_INT offset_low, total_low;
8820   HOST_WIDE_INT size, offset_high, total_high;
8821
8822   if (!POINTER_TYPE_P (TREE_TYPE (base)))
8823     return true;
8824
8825   if (bitpos < 0)
8826     return true;
8827
8828   if (offset == NULL_TREE)
8829     {
8830       offset_low = 0;
8831       offset_high = 0;
8832     }
8833   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8834     return true;
8835   else
8836     {
8837       offset_low = TREE_INT_CST_LOW (offset);
8838       offset_high = TREE_INT_CST_HIGH (offset);
8839     }
8840
8841   if (add_double_with_sign (offset_low, offset_high,
8842                             bitpos / BITS_PER_UNIT, 0,
8843                             &total_low, &total_high,
8844                             true))
8845     return true;
8846
8847   if (total_high != 0)
8848     return true;
8849
8850   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8851   if (size <= 0)
8852     return true;
8853
8854   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8855      array.  */
8856   if (TREE_CODE (base) == ADDR_EXPR)
8857     {
8858       HOST_WIDE_INT base_size;
8859
8860       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8861       if (base_size > 0 && size < base_size)
8862         size = base_size;
8863     }
8864
8865   return total_low > (unsigned HOST_WIDE_INT) size;
8866 }
8867
8868 /* Subroutine of fold_binary.  This routine performs all of the
8869    transformations that are common to the equality/inequality
8870    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8871    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8872    fold_binary should call fold_binary.  Fold a comparison with
8873    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8874    the folded comparison or NULL_TREE.  */
8875
8876 static tree
8877 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8878 {
8879   tree arg0, arg1, tem;
8880
8881   arg0 = op0;
8882   arg1 = op1;
8883
8884   STRIP_SIGN_NOPS (arg0);
8885   STRIP_SIGN_NOPS (arg1);
8886
8887   tem = fold_relational_const (code, type, arg0, arg1);
8888   if (tem != NULL_TREE)
8889     return tem;
8890
8891   /* If one arg is a real or integer constant, put it last.  */
8892   if (tree_swap_operands_p (arg0, arg1, true))
8893     return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8894
8895   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8896   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8897       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8898           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8899           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8900       && (TREE_CODE (arg1) == INTEGER_CST
8901           && !TREE_OVERFLOW (arg1)))
8902     {
8903       tree const1 = TREE_OPERAND (arg0, 1);
8904       tree const2 = arg1;
8905       tree variable = TREE_OPERAND (arg0, 0);
8906       tree lhs;
8907       int lhs_add;
8908       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8909
8910       lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8911                          TREE_TYPE (arg1), const2, const1);
8912
8913       /* If the constant operation overflowed this can be
8914          simplified as a comparison against INT_MAX/INT_MIN.  */
8915       if (TREE_CODE (lhs) == INTEGER_CST
8916           && TREE_OVERFLOW (lhs))
8917         {
8918           int const1_sgn = tree_int_cst_sgn (const1);
8919           enum tree_code code2 = code;
8920
8921           /* Get the sign of the constant on the lhs if the
8922              operation were VARIABLE + CONST1.  */
8923           if (TREE_CODE (arg0) == MINUS_EXPR)
8924             const1_sgn = -const1_sgn;
8925
8926           /* The sign of the constant determines if we overflowed
8927              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8928              Canonicalize to the INT_MIN overflow by swapping the comparison
8929              if necessary.  */
8930           if (const1_sgn == -1)
8931             code2 = swap_tree_comparison (code);
8932
8933           /* We now can look at the canonicalized case
8934                VARIABLE + 1  CODE2  INT_MIN
8935              and decide on the result.  */
8936           if (code2 == LT_EXPR
8937               || code2 == LE_EXPR
8938               || code2 == EQ_EXPR)
8939             return omit_one_operand (type, boolean_false_node, variable);
8940           else if (code2 == NE_EXPR
8941                    || code2 == GE_EXPR
8942                    || code2 == GT_EXPR)
8943             return omit_one_operand (type, boolean_true_node, variable);
8944         }
8945
8946       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8947           && (TREE_CODE (lhs) != INTEGER_CST
8948               || !TREE_OVERFLOW (lhs)))
8949         {
8950           fold_overflow_warning (("assuming signed overflow does not occur "
8951                                   "when changing X +- C1 cmp C2 to "
8952                                   "X cmp C1 +- C2"),
8953                                  WARN_STRICT_OVERFLOW_COMPARISON);
8954           return fold_build2 (code, type, variable, lhs);
8955         }
8956     }
8957
8958   /* For comparisons of pointers we can decompose it to a compile time
8959      comparison of the base objects and the offsets into the object.
8960      This requires at least one operand being an ADDR_EXPR or a
8961      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
8962   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8963       && (TREE_CODE (arg0) == ADDR_EXPR
8964           || TREE_CODE (arg1) == ADDR_EXPR
8965           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8966           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8967     {
8968       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8969       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8970       enum machine_mode mode;
8971       int volatilep, unsignedp;
8972       bool indirect_base0 = false, indirect_base1 = false;
8973
8974       /* Get base and offset for the access.  Strip ADDR_EXPR for
8975          get_inner_reference, but put it back by stripping INDIRECT_REF
8976          off the base object if possible.  indirect_baseN will be true
8977          if baseN is not an address but refers to the object itself.  */
8978       base0 = arg0;
8979       if (TREE_CODE (arg0) == ADDR_EXPR)
8980         {
8981           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8982                                        &bitsize, &bitpos0, &offset0, &mode,
8983                                        &unsignedp, &volatilep, false);
8984           if (TREE_CODE (base0) == INDIRECT_REF)
8985             base0 = TREE_OPERAND (base0, 0);
8986           else
8987             indirect_base0 = true;
8988         }
8989       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8990         {
8991           base0 = TREE_OPERAND (arg0, 0);
8992           offset0 = TREE_OPERAND (arg0, 1);
8993         }
8994
8995       base1 = arg1;
8996       if (TREE_CODE (arg1) == ADDR_EXPR)
8997         {
8998           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8999                                        &bitsize, &bitpos1, &offset1, &mode,
9000                                        &unsignedp, &volatilep, false);
9001           if (TREE_CODE (base1) == INDIRECT_REF)
9002             base1 = TREE_OPERAND (base1, 0);
9003           else
9004             indirect_base1 = true;
9005         }
9006       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9007         {
9008           base1 = TREE_OPERAND (arg1, 0);
9009           offset1 = TREE_OPERAND (arg1, 1);
9010         }
9011
9012       /* If we have equivalent bases we might be able to simplify.  */
9013       if (indirect_base0 == indirect_base1
9014           && operand_equal_p (base0, base1, 0))
9015         {
9016           /* We can fold this expression to a constant if the non-constant
9017              offset parts are equal.  */
9018           if ((offset0 == offset1
9019                || (offset0 && offset1
9020                    && operand_equal_p (offset0, offset1, 0)))
9021               && (code == EQ_EXPR
9022                   || code == NE_EXPR
9023                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
9024                 
9025             {
9026               if (code != EQ_EXPR
9027                   && code != NE_EXPR
9028                   && bitpos0 != bitpos1
9029                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9030                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9031                 fold_overflow_warning (("assuming pointer wraparound does not "
9032                                         "occur when comparing P +- C1 with "
9033                                         "P +- C2"),
9034                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
9035
9036               switch (code)
9037                 {
9038                 case EQ_EXPR:
9039                   return build_int_cst (boolean_type_node, bitpos0 == bitpos1);
9040                 case NE_EXPR:
9041                   return build_int_cst (boolean_type_node, bitpos0 != bitpos1);
9042                 case LT_EXPR:
9043                   return build_int_cst (boolean_type_node, bitpos0 < bitpos1);
9044                 case LE_EXPR:
9045                   return build_int_cst (boolean_type_node, bitpos0 <= bitpos1);
9046                 case GE_EXPR:
9047                   return build_int_cst (boolean_type_node, bitpos0 >= bitpos1);
9048                 case GT_EXPR:
9049                   return build_int_cst (boolean_type_node, bitpos0 > bitpos1);
9050                 default:;
9051                 }
9052             }
9053           /* We can simplify the comparison to a comparison of the variable
9054              offset parts if the constant offset parts are equal.
9055              Be careful to use signed size type here because otherwise we
9056              mess with array offsets in the wrong way.  This is possible
9057              because pointer arithmetic is restricted to retain within an
9058              object and overflow on pointer differences is undefined as of
9059              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
9060           else if (bitpos0 == bitpos1
9061                    && ((code == EQ_EXPR || code == NE_EXPR)
9062                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
9063             {
9064               tree signed_size_type_node;
9065               signed_size_type_node = signed_type_for (size_type_node);
9066
9067               /* By converting to signed size type we cover middle-end pointer
9068                  arithmetic which operates on unsigned pointer types of size
9069                  type size and ARRAY_REF offsets which are properly sign or
9070                  zero extended from their type in case it is narrower than
9071                  size type.  */
9072               if (offset0 == NULL_TREE)
9073                 offset0 = build_int_cst (signed_size_type_node, 0);
9074               else
9075                 offset0 = fold_convert (signed_size_type_node, offset0);
9076               if (offset1 == NULL_TREE)
9077                 offset1 = build_int_cst (signed_size_type_node, 0);
9078               else
9079                 offset1 = fold_convert (signed_size_type_node, offset1);
9080
9081               if (code != EQ_EXPR
9082                   && code != NE_EXPR
9083                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9084                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9085                 fold_overflow_warning (("assuming pointer wraparound does not "
9086                                         "occur when comparing P +- C1 with "
9087                                         "P +- C2"),
9088                                        WARN_STRICT_OVERFLOW_COMPARISON);
9089
9090               return fold_build2 (code, type, offset0, offset1);
9091             }
9092         }
9093       /* For non-equal bases we can simplify if they are addresses
9094          of local binding decls or constants.  */
9095       else if (indirect_base0 && indirect_base1
9096                /* We know that !operand_equal_p (base0, base1, 0)
9097                   because the if condition was false.  But make
9098                   sure two decls are not the same.  */
9099                && base0 != base1
9100                && TREE_CODE (arg0) == ADDR_EXPR
9101                && TREE_CODE (arg1) == ADDR_EXPR
9102                && (((TREE_CODE (base0) == VAR_DECL
9103                      || TREE_CODE (base0) == PARM_DECL)
9104                     && (targetm.binds_local_p (base0)
9105                         || CONSTANT_CLASS_P (base1)))
9106                    || CONSTANT_CLASS_P (base0))
9107                && (((TREE_CODE (base1) == VAR_DECL
9108                      || TREE_CODE (base1) == PARM_DECL)
9109                     && (targetm.binds_local_p (base1)
9110                         || CONSTANT_CLASS_P (base0)))
9111                    || CONSTANT_CLASS_P (base1)))
9112         {
9113           if (code == EQ_EXPR)
9114             return omit_two_operands (type, boolean_false_node, arg0, arg1);
9115           else if (code == NE_EXPR)
9116             return omit_two_operands (type, boolean_true_node, arg0, arg1);
9117         }
9118       /* For equal offsets we can simplify to a comparison of the
9119          base addresses.  */
9120       else if (bitpos0 == bitpos1
9121                && (indirect_base0
9122                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9123                && (indirect_base1
9124                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9125                && ((offset0 == offset1)
9126                    || (offset0 && offset1
9127                        && operand_equal_p (offset0, offset1, 0))))
9128         {
9129           if (indirect_base0)
9130             base0 = fold_addr_expr (base0);
9131           if (indirect_base1)
9132             base1 = fold_addr_expr (base1);
9133           return fold_build2 (code, type, base0, base1);
9134         }
9135     }
9136
9137   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9138      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
9139      the resulting offset is smaller in absolute value than the
9140      original one.  */
9141   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9142       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9143       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9144           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9145       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9146       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9147           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9148     {
9149       tree const1 = TREE_OPERAND (arg0, 1);
9150       tree const2 = TREE_OPERAND (arg1, 1);
9151       tree variable1 = TREE_OPERAND (arg0, 0);
9152       tree variable2 = TREE_OPERAND (arg1, 0);
9153       tree cst;
9154       const char * const warnmsg = G_("assuming signed overflow does not "
9155                                       "occur when combining constants around "
9156                                       "a comparison");
9157
9158       /* Put the constant on the side where it doesn't overflow and is
9159          of lower absolute value than before.  */
9160       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9161                              ? MINUS_EXPR : PLUS_EXPR,
9162                              const2, const1, 0);
9163       if (!TREE_OVERFLOW (cst)
9164           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9165         {
9166           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9167           return fold_build2 (code, type,
9168                               variable1,
9169                               fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
9170                                            variable2, cst));
9171         }
9172
9173       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9174                              ? MINUS_EXPR : PLUS_EXPR,
9175                              const1, const2, 0);
9176       if (!TREE_OVERFLOW (cst)
9177           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9178         {
9179           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9180           return fold_build2 (code, type,
9181                               fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
9182                                            variable1, cst),
9183                               variable2);
9184         }
9185     }
9186
9187   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9188      signed arithmetic case.  That form is created by the compiler
9189      often enough for folding it to be of value.  One example is in
9190      computing loop trip counts after Operator Strength Reduction.  */
9191   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9192       && TREE_CODE (arg0) == MULT_EXPR
9193       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9194           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9195       && integer_zerop (arg1))
9196     {
9197       tree const1 = TREE_OPERAND (arg0, 1);
9198       tree const2 = arg1;                       /* zero */
9199       tree variable1 = TREE_OPERAND (arg0, 0);
9200       enum tree_code cmp_code = code;
9201
9202       gcc_assert (!integer_zerop (const1));
9203
9204       fold_overflow_warning (("assuming signed overflow does not occur when "
9205                               "eliminating multiplication in comparison "
9206                               "with zero"),
9207                              WARN_STRICT_OVERFLOW_COMPARISON);
9208
9209       /* If const1 is negative we swap the sense of the comparison.  */
9210       if (tree_int_cst_sgn (const1) < 0)
9211         cmp_code = swap_tree_comparison (cmp_code);
9212
9213       return fold_build2 (cmp_code, type, variable1, const2);
9214     }
9215
9216   tem = maybe_canonicalize_comparison (code, type, op0, op1);
9217   if (tem)
9218     return tem;
9219
9220   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9221     {
9222       tree targ0 = strip_float_extensions (arg0);
9223       tree targ1 = strip_float_extensions (arg1);
9224       tree newtype = TREE_TYPE (targ0);
9225
9226       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9227         newtype = TREE_TYPE (targ1);
9228
9229       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9230       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9231         return fold_build2 (code, type, fold_convert (newtype, targ0),
9232                             fold_convert (newtype, targ1));
9233
9234       /* (-a) CMP (-b) -> b CMP a  */
9235       if (TREE_CODE (arg0) == NEGATE_EXPR
9236           && TREE_CODE (arg1) == NEGATE_EXPR)
9237         return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
9238                             TREE_OPERAND (arg0, 0));
9239
9240       if (TREE_CODE (arg1) == REAL_CST)
9241         {
9242           REAL_VALUE_TYPE cst;
9243           cst = TREE_REAL_CST (arg1);
9244
9245           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
9246           if (TREE_CODE (arg0) == NEGATE_EXPR)
9247             return fold_build2 (swap_tree_comparison (code), type,
9248                                 TREE_OPERAND (arg0, 0),
9249                                 build_real (TREE_TYPE (arg1),
9250                                             REAL_VALUE_NEGATE (cst)));
9251
9252           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
9253           /* a CMP (-0) -> a CMP 0  */
9254           if (REAL_VALUE_MINUS_ZERO (cst))
9255             return fold_build2 (code, type, arg0,
9256                                 build_real (TREE_TYPE (arg1), dconst0));
9257
9258           /* x != NaN is always true, other ops are always false.  */
9259           if (REAL_VALUE_ISNAN (cst)
9260               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9261             {
9262               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9263               return omit_one_operand (type, tem, arg0);
9264             }
9265
9266           /* Fold comparisons against infinity.  */
9267           if (REAL_VALUE_ISINF (cst))
9268             {
9269               tem = fold_inf_compare (code, type, arg0, arg1);
9270               if (tem != NULL_TREE)
9271                 return tem;
9272             }
9273         }
9274
9275       /* If this is a comparison of a real constant with a PLUS_EXPR
9276          or a MINUS_EXPR of a real constant, we can convert it into a
9277          comparison with a revised real constant as long as no overflow
9278          occurs when unsafe_math_optimizations are enabled.  */
9279       if (flag_unsafe_math_optimizations
9280           && TREE_CODE (arg1) == REAL_CST
9281           && (TREE_CODE (arg0) == PLUS_EXPR
9282               || TREE_CODE (arg0) == MINUS_EXPR)
9283           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9284           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9285                                       ? MINUS_EXPR : PLUS_EXPR,
9286                                       arg1, TREE_OPERAND (arg0, 1), 0))
9287           && !TREE_OVERFLOW (tem))
9288         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9289
9290       /* Likewise, we can simplify a comparison of a real constant with
9291          a MINUS_EXPR whose first operand is also a real constant, i.e.
9292          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on 
9293          floating-point types only if -fassociative-math is set.  */
9294       if (flag_associative_math
9295           && TREE_CODE (arg1) == REAL_CST
9296           && TREE_CODE (arg0) == MINUS_EXPR
9297           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9298           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9299                                       arg1, 0))
9300           && !TREE_OVERFLOW (tem))
9301         return fold_build2 (swap_tree_comparison (code), type,
9302                             TREE_OPERAND (arg0, 1), tem);
9303
9304       /* Fold comparisons against built-in math functions.  */
9305       if (TREE_CODE (arg1) == REAL_CST
9306           && flag_unsafe_math_optimizations
9307           && ! flag_errno_math)
9308         {
9309           enum built_in_function fcode = builtin_mathfn_code (arg0);
9310
9311           if (fcode != END_BUILTINS)
9312             {
9313               tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
9314               if (tem != NULL_TREE)
9315                 return tem;
9316             }
9317         }
9318     }
9319
9320   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9321       && CONVERT_EXPR_P (arg0))
9322     {
9323       /* If we are widening one operand of an integer comparison,
9324          see if the other operand is similarly being widened.  Perhaps we
9325          can do the comparison in the narrower type.  */
9326       tem = fold_widened_comparison (code, type, arg0, arg1);
9327       if (tem)
9328         return tem;
9329
9330       /* Or if we are changing signedness.  */
9331       tem = fold_sign_changed_comparison (code, type, arg0, arg1);
9332       if (tem)
9333         return tem;
9334     }
9335
9336   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9337      constant, we can simplify it.  */
9338   if (TREE_CODE (arg1) == INTEGER_CST
9339       && (TREE_CODE (arg0) == MIN_EXPR
9340           || TREE_CODE (arg0) == MAX_EXPR)
9341       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9342     {
9343       tem = optimize_minmax_comparison (code, type, op0, op1);
9344       if (tem)
9345         return tem;
9346     }
9347
9348   /* Simplify comparison of something with itself.  (For IEEE
9349      floating-point, we can only do some of these simplifications.)  */
9350   if (operand_equal_p (arg0, arg1, 0))
9351     {
9352       switch (code)
9353         {
9354         case EQ_EXPR:
9355           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9356               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9357             return constant_boolean_node (1, type);
9358           break;
9359
9360         case GE_EXPR:
9361         case LE_EXPR:
9362           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9363               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9364             return constant_boolean_node (1, type);
9365           return fold_build2 (EQ_EXPR, type, arg0, arg1);
9366
9367         case NE_EXPR:
9368           /* For NE, we can only do this simplification if integer
9369              or we don't honor IEEE floating point NaNs.  */
9370           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9371               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9372             break;
9373           /* ... fall through ...  */
9374         case GT_EXPR:
9375         case LT_EXPR:
9376           return constant_boolean_node (0, type);
9377         default:
9378           gcc_unreachable ();
9379         }
9380     }
9381
9382   /* If we are comparing an expression that just has comparisons
9383      of two integer values, arithmetic expressions of those comparisons,
9384      and constants, we can simplify it.  There are only three cases
9385      to check: the two values can either be equal, the first can be
9386      greater, or the second can be greater.  Fold the expression for
9387      those three values.  Since each value must be 0 or 1, we have
9388      eight possibilities, each of which corresponds to the constant 0
9389      or 1 or one of the six possible comparisons.
9390
9391      This handles common cases like (a > b) == 0 but also handles
9392      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9393      occur in macroized code.  */
9394
9395   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9396     {
9397       tree cval1 = 0, cval2 = 0;
9398       int save_p = 0;
9399
9400       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9401           /* Don't handle degenerate cases here; they should already
9402              have been handled anyway.  */
9403           && cval1 != 0 && cval2 != 0
9404           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9405           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9406           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9407           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9408           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9409           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9410                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9411         {
9412           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9413           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9414
9415           /* We can't just pass T to eval_subst in case cval1 or cval2
9416              was the same as ARG1.  */
9417
9418           tree high_result
9419                 = fold_build2 (code, type,
9420                                eval_subst (arg0, cval1, maxval,
9421                                            cval2, minval),
9422                                arg1);
9423           tree equal_result
9424                 = fold_build2 (code, type,
9425                                eval_subst (arg0, cval1, maxval,
9426                                            cval2, maxval),
9427                                arg1);
9428           tree low_result
9429                 = fold_build2 (code, type,
9430                                eval_subst (arg0, cval1, minval,
9431                                            cval2, maxval),
9432                                arg1);
9433
9434           /* All three of these results should be 0 or 1.  Confirm they are.
9435              Then use those values to select the proper code to use.  */
9436
9437           if (TREE_CODE (high_result) == INTEGER_CST
9438               && TREE_CODE (equal_result) == INTEGER_CST
9439               && TREE_CODE (low_result) == INTEGER_CST)
9440             {
9441               /* Make a 3-bit mask with the high-order bit being the
9442                  value for `>', the next for '=', and the low for '<'.  */
9443               switch ((integer_onep (high_result) * 4)
9444                       + (integer_onep (equal_result) * 2)
9445                       + integer_onep (low_result))
9446                 {
9447                 case 0:
9448                   /* Always false.  */
9449                   return omit_one_operand (type, integer_zero_node, arg0);
9450                 case 1:
9451                   code = LT_EXPR;
9452                   break;
9453                 case 2:
9454                   code = EQ_EXPR;
9455                   break;
9456                 case 3:
9457                   code = LE_EXPR;
9458                   break;
9459                 case 4:
9460                   code = GT_EXPR;
9461                   break;
9462                 case 5:
9463                   code = NE_EXPR;
9464                   break;
9465                 case 6:
9466                   code = GE_EXPR;
9467                   break;
9468                 case 7:
9469                   /* Always true.  */
9470                   return omit_one_operand (type, integer_one_node, arg0);
9471                 }
9472
9473               if (save_p)
9474                 return save_expr (build2 (code, type, cval1, cval2));
9475               return fold_build2 (code, type, cval1, cval2);
9476             }
9477         }
9478     }
9479
9480   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9481      into a single range test.  */
9482   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9483        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9484       && TREE_CODE (arg1) == INTEGER_CST
9485       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9486       && !integer_zerop (TREE_OPERAND (arg0, 1))
9487       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9488       && !TREE_OVERFLOW (arg1))
9489     {
9490       tem = fold_div_compare (code, type, arg0, arg1);
9491       if (tem != NULL_TREE)
9492         return tem;
9493     }
9494
9495   /* Fold ~X op ~Y as Y op X.  */
9496   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9497       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9498     {
9499       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9500       return fold_build2 (code, type,
9501                           fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9502                           TREE_OPERAND (arg0, 0));
9503     }
9504
9505   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9506   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9507       && TREE_CODE (arg1) == INTEGER_CST)
9508     {
9509       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9510       return fold_build2 (swap_tree_comparison (code), type,
9511                           TREE_OPERAND (arg0, 0),
9512                           fold_build1 (BIT_NOT_EXPR, cmp_type,
9513                                        fold_convert (cmp_type, arg1)));
9514     }
9515
9516   return NULL_TREE;
9517 }
9518
9519
9520 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9521    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9522    argument EXPR represents the expression "z" of type TYPE.  */
9523
9524 static tree
9525 fold_mult_zconjz (tree type, tree expr)
9526 {
9527   tree itype = TREE_TYPE (type);
9528   tree rpart, ipart, tem;
9529
9530   if (TREE_CODE (expr) == COMPLEX_EXPR)
9531     {
9532       rpart = TREE_OPERAND (expr, 0);
9533       ipart = TREE_OPERAND (expr, 1);
9534     }
9535   else if (TREE_CODE (expr) == COMPLEX_CST)
9536     {
9537       rpart = TREE_REALPART (expr);
9538       ipart = TREE_IMAGPART (expr);
9539     }
9540   else
9541     {
9542       expr = save_expr (expr);
9543       rpart = fold_build1 (REALPART_EXPR, itype, expr);
9544       ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9545     }
9546
9547   rpart = save_expr (rpart);
9548   ipart = save_expr (ipart);
9549   tem = fold_build2 (PLUS_EXPR, itype,
9550                      fold_build2 (MULT_EXPR, itype, rpart, rpart),
9551                      fold_build2 (MULT_EXPR, itype, ipart, ipart));
9552   return fold_build2 (COMPLEX_EXPR, type, tem,
9553                       fold_convert (itype, integer_zero_node));
9554 }
9555
9556
9557 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9558    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9559    guarantees that P and N have the same least significant log2(M) bits.
9560    N is not otherwise constrained.  In particular, N is not normalized to
9561    0 <= N < M as is common.  In general, the precise value of P is unknown.
9562    M is chosen as large as possible such that constant N can be determined.
9563
9564    Returns M and sets *RESIDUE to N.  */
9565
9566 static unsigned HOST_WIDE_INT
9567 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue)
9568 {
9569   enum tree_code code;
9570
9571   *residue = 0;
9572
9573   code = TREE_CODE (expr);
9574   if (code == ADDR_EXPR)
9575     {
9576       expr = TREE_OPERAND (expr, 0);
9577       if (handled_component_p (expr))
9578         {
9579           HOST_WIDE_INT bitsize, bitpos;
9580           tree offset;
9581           enum machine_mode mode;
9582           int unsignedp, volatilep;
9583
9584           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9585                                       &mode, &unsignedp, &volatilep, false);
9586           *residue = bitpos / BITS_PER_UNIT;
9587           if (offset)
9588             {
9589               if (TREE_CODE (offset) == INTEGER_CST)
9590                 *residue += TREE_INT_CST_LOW (offset);
9591               else
9592                 /* We don't handle more complicated offset expressions.  */
9593                 return 1;
9594             }
9595         }
9596
9597       if (DECL_P (expr) && TREE_CODE (expr) != FUNCTION_DECL)
9598         return DECL_ALIGN_UNIT (expr);
9599     }
9600   else if (code == POINTER_PLUS_EXPR)
9601     {
9602       tree op0, op1;
9603       unsigned HOST_WIDE_INT modulus;
9604       enum tree_code inner_code;
9605       
9606       op0 = TREE_OPERAND (expr, 0);
9607       STRIP_NOPS (op0);
9608       modulus = get_pointer_modulus_and_residue (op0, residue);
9609
9610       op1 = TREE_OPERAND (expr, 1);
9611       STRIP_NOPS (op1);
9612       inner_code = TREE_CODE (op1);
9613       if (inner_code == INTEGER_CST)
9614         {
9615           *residue += TREE_INT_CST_LOW (op1);
9616           return modulus;
9617         }
9618       else if (inner_code == MULT_EXPR)
9619         {
9620           op1 = TREE_OPERAND (op1, 1);
9621           if (TREE_CODE (op1) == INTEGER_CST)
9622             {
9623               unsigned HOST_WIDE_INT align;
9624               
9625               /* Compute the greatest power-of-2 divisor of op1.  */
9626               align = TREE_INT_CST_LOW (op1);
9627               align &= -align;
9628
9629               /* If align is non-zero and less than *modulus, replace
9630                  *modulus with align., If align is 0, then either op1 is 0
9631                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9632                  unsigned HOST_WIDE_INT.  In either case, no additional
9633                  constraint is imposed.  */
9634               if (align)
9635                 modulus = MIN (modulus, align);
9636
9637               return modulus;
9638             }
9639         }
9640     }
9641
9642     /* If we get here, we were unable to determine anything useful about the
9643        expression.  */
9644     return 1;
9645 }
9646
9647
9648 /* Fold a binary expression of code CODE and type TYPE with operands
9649    OP0 and OP1.  Return the folded expression if folding is
9650    successful.  Otherwise, return NULL_TREE.  */
9651
9652 tree
9653 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9654 {
9655   enum tree_code_class kind = TREE_CODE_CLASS (code);
9656   tree arg0, arg1, tem;
9657   tree t1 = NULL_TREE;
9658   bool strict_overflow_p;
9659
9660   gcc_assert (IS_EXPR_CODE_CLASS (kind)
9661               && TREE_CODE_LENGTH (code) == 2
9662               && op0 != NULL_TREE
9663               && op1 != NULL_TREE);
9664
9665   arg0 = op0;
9666   arg1 = op1;
9667
9668   /* Strip any conversions that don't change the mode.  This is
9669      safe for every expression, except for a comparison expression
9670      because its signedness is derived from its operands.  So, in
9671      the latter case, only strip conversions that don't change the
9672      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
9673      preserved.
9674
9675      Note that this is done as an internal manipulation within the
9676      constant folder, in order to find the simplest representation
9677      of the arguments so that their form can be studied.  In any
9678      cases, the appropriate type conversions should be put back in
9679      the tree that will get out of the constant folder.  */
9680
9681   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9682     {
9683       STRIP_SIGN_NOPS (arg0);
9684       STRIP_SIGN_NOPS (arg1);
9685     }
9686   else
9687     {
9688       STRIP_NOPS (arg0);
9689       STRIP_NOPS (arg1);
9690     }
9691
9692   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9693      constant but we can't do arithmetic on them.  */
9694   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9695       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9696       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9697       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9698       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9699       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9700     {
9701       if (kind == tcc_binary)
9702         {
9703           /* Make sure type and arg0 have the same saturating flag.  */
9704           gcc_assert (TYPE_SATURATING (type)
9705                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9706           tem = const_binop (code, arg0, arg1, 0);
9707         }
9708       else if (kind == tcc_comparison)
9709         tem = fold_relational_const (code, type, arg0, arg1);
9710       else
9711         tem = NULL_TREE;
9712
9713       if (tem != NULL_TREE)
9714         {
9715           if (TREE_TYPE (tem) != type)
9716             tem = fold_convert (type, tem);
9717           return tem;
9718         }
9719     }
9720
9721   /* If this is a commutative operation, and ARG0 is a constant, move it
9722      to ARG1 to reduce the number of tests below.  */
9723   if (commutative_tree_code (code)
9724       && tree_swap_operands_p (arg0, arg1, true))
9725     return fold_build2 (code, type, op1, op0);
9726
9727   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9728
9729      First check for cases where an arithmetic operation is applied to a
9730      compound, conditional, or comparison operation.  Push the arithmetic
9731      operation inside the compound or conditional to see if any folding
9732      can then be done.  Convert comparison to conditional for this purpose.
9733      The also optimizes non-constant cases that used to be done in
9734      expand_expr.
9735
9736      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9737      one of the operands is a comparison and the other is a comparison, a
9738      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9739      code below would make the expression more complex.  Change it to a
9740      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9741      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9742
9743   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9744        || code == EQ_EXPR || code == NE_EXPR)
9745       && ((truth_value_p (TREE_CODE (arg0))
9746            && (truth_value_p (TREE_CODE (arg1))
9747                || (TREE_CODE (arg1) == BIT_AND_EXPR
9748                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9749           || (truth_value_p (TREE_CODE (arg1))
9750               && (truth_value_p (TREE_CODE (arg0))
9751                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9752                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9753     {
9754       tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9755                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9756                          : TRUTH_XOR_EXPR,
9757                          boolean_type_node,
9758                          fold_convert (boolean_type_node, arg0),
9759                          fold_convert (boolean_type_node, arg1));
9760
9761       if (code == EQ_EXPR)
9762         tem = invert_truthvalue (tem);
9763
9764       return fold_convert (type, tem);
9765     }
9766
9767   if (TREE_CODE_CLASS (code) == tcc_binary
9768       || TREE_CODE_CLASS (code) == tcc_comparison)
9769     {
9770       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9771         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9772                        fold_build2 (code, type,
9773                                     fold_convert (TREE_TYPE (op0),
9774                                                   TREE_OPERAND (arg0, 1)),
9775                                     op1));
9776       if (TREE_CODE (arg1) == COMPOUND_EXPR
9777           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9778         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9779                        fold_build2 (code, type, op0,
9780                                     fold_convert (TREE_TYPE (op1),
9781                                                   TREE_OPERAND (arg1, 1))));
9782
9783       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9784         {
9785           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9786                                                      arg0, arg1, 
9787                                                      /*cond_first_p=*/1);
9788           if (tem != NULL_TREE)
9789             return tem;
9790         }
9791
9792       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9793         {
9794           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9795                                                      arg1, arg0, 
9796                                                      /*cond_first_p=*/0);
9797           if (tem != NULL_TREE)
9798             return tem;
9799         }
9800     }
9801
9802   switch (code)
9803     {
9804     case POINTER_PLUS_EXPR:
9805       /* 0 +p index -> (type)index */
9806       if (integer_zerop (arg0))
9807         return non_lvalue (fold_convert (type, arg1));
9808
9809       /* PTR +p 0 -> PTR */
9810       if (integer_zerop (arg1))
9811         return non_lvalue (fold_convert (type, arg0));
9812
9813       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9814       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9815            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9816         return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9817                                                 fold_convert (sizetype, arg1),
9818                                                 fold_convert (sizetype, arg0)));
9819
9820       /* index +p PTR -> PTR +p index */
9821       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9822           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9823         return fold_build2 (POINTER_PLUS_EXPR, type,
9824                             fold_convert (type, arg1),
9825                             fold_convert (sizetype, arg0));
9826
9827       /* (PTR +p B) +p A -> PTR +p (B + A) */
9828       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9829         {
9830           tree inner;
9831           tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9832           tree arg00 = TREE_OPERAND (arg0, 0);
9833           inner = fold_build2 (PLUS_EXPR, sizetype,
9834                                arg01, fold_convert (sizetype, arg1));
9835           return fold_convert (type,
9836                                fold_build2 (POINTER_PLUS_EXPR,
9837                                             TREE_TYPE (arg00), arg00, inner));
9838         }
9839
9840       /* PTR_CST +p CST -> CST1 */
9841       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9842         return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9843
9844      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9845         of the array.  Loop optimizer sometimes produce this type of
9846         expressions.  */
9847       if (TREE_CODE (arg0) == ADDR_EXPR)
9848         {
9849           tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9850           if (tem)
9851             return fold_convert (type, tem);
9852         }
9853
9854       return NULL_TREE;
9855
9856     case PLUS_EXPR:
9857       /* PTR + INT -> (INT)(PTR p+ INT) */
9858       if (POINTER_TYPE_P (TREE_TYPE (arg0))
9859           && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
9860         return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9861                                                 TREE_TYPE (arg0),
9862                                                 arg0,
9863                                                 fold_convert (sizetype, arg1)));
9864       /* INT + PTR -> (INT)(PTR p+ INT) */
9865       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9866           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9867         return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9868                                                 TREE_TYPE (arg1),
9869                                                 arg1,
9870                                                 fold_convert (sizetype, arg0)));
9871       /* A + (-B) -> A - B */
9872       if (TREE_CODE (arg1) == NEGATE_EXPR)
9873         return fold_build2 (MINUS_EXPR, type,
9874                             fold_convert (type, arg0),
9875                             fold_convert (type, TREE_OPERAND (arg1, 0)));
9876       /* (-A) + B -> B - A */
9877       if (TREE_CODE (arg0) == NEGATE_EXPR
9878           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9879         return fold_build2 (MINUS_EXPR, type,
9880                             fold_convert (type, arg1),
9881                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9882
9883       if (INTEGRAL_TYPE_P (type))
9884         {
9885           /* Convert ~A + 1 to -A.  */
9886           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9887               && integer_onep (arg1))
9888             return fold_build1 (NEGATE_EXPR, type,
9889                                 fold_convert (type, TREE_OPERAND (arg0, 0)));
9890
9891           /* ~X + X is -1.  */
9892           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9893               && !TYPE_OVERFLOW_TRAPS (type))
9894             {
9895               tree tem = TREE_OPERAND (arg0, 0);
9896
9897               STRIP_NOPS (tem);
9898               if (operand_equal_p (tem, arg1, 0))
9899                 {
9900                   t1 = build_int_cst_type (type, -1);
9901                   return omit_one_operand (type, t1, arg1);
9902                 }
9903             }
9904
9905           /* X + ~X is -1.  */
9906           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9907               && !TYPE_OVERFLOW_TRAPS (type))
9908             {
9909               tree tem = TREE_OPERAND (arg1, 0);
9910
9911               STRIP_NOPS (tem);
9912               if (operand_equal_p (arg0, tem, 0))
9913                 {
9914                   t1 = build_int_cst_type (type, -1);
9915                   return omit_one_operand (type, t1, arg0);
9916                 }
9917             }
9918
9919           /* X + (X / CST) * -CST is X % CST.  */
9920           if (TREE_CODE (arg1) == MULT_EXPR
9921               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9922               && operand_equal_p (arg0,
9923                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9924             {
9925               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9926               tree cst1 = TREE_OPERAND (arg1, 1);
9927               tree sum = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1, cst0);
9928               if (sum && integer_zerop (sum))
9929                 return fold_convert (type,
9930                                      fold_build2 (TRUNC_MOD_EXPR,
9931                                                   TREE_TYPE (arg0), arg0, cst0));
9932             }
9933         }
9934
9935       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9936          same or one.  Make sure type is not saturating.
9937          fold_plusminus_mult_expr will re-associate.  */
9938       if ((TREE_CODE (arg0) == MULT_EXPR
9939            || TREE_CODE (arg1) == MULT_EXPR)
9940           && !TYPE_SATURATING (type)
9941           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9942         {
9943           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9944           if (tem)
9945             return tem;
9946         }
9947
9948       if (! FLOAT_TYPE_P (type))
9949         {
9950           if (integer_zerop (arg1))
9951             return non_lvalue (fold_convert (type, arg0));
9952
9953           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9954              with a constant, and the two constants have no bits in common,
9955              we should treat this as a BIT_IOR_EXPR since this may produce more
9956              simplifications.  */
9957           if (TREE_CODE (arg0) == BIT_AND_EXPR
9958               && TREE_CODE (arg1) == BIT_AND_EXPR
9959               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9960               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9961               && integer_zerop (const_binop (BIT_AND_EXPR,
9962                                              TREE_OPERAND (arg0, 1),
9963                                              TREE_OPERAND (arg1, 1), 0)))
9964             {
9965               code = BIT_IOR_EXPR;
9966               goto bit_ior;
9967             }
9968
9969           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9970              (plus (plus (mult) (mult)) (foo)) so that we can
9971              take advantage of the factoring cases below.  */
9972           if (((TREE_CODE (arg0) == PLUS_EXPR
9973                 || TREE_CODE (arg0) == MINUS_EXPR)
9974                && TREE_CODE (arg1) == MULT_EXPR)
9975               || ((TREE_CODE (arg1) == PLUS_EXPR
9976                    || TREE_CODE (arg1) == MINUS_EXPR)
9977                   && TREE_CODE (arg0) == MULT_EXPR))
9978             {
9979               tree parg0, parg1, parg, marg;
9980               enum tree_code pcode;
9981
9982               if (TREE_CODE (arg1) == MULT_EXPR)
9983                 parg = arg0, marg = arg1;
9984               else
9985                 parg = arg1, marg = arg0;
9986               pcode = TREE_CODE (parg);
9987               parg0 = TREE_OPERAND (parg, 0);
9988               parg1 = TREE_OPERAND (parg, 1);
9989               STRIP_NOPS (parg0);
9990               STRIP_NOPS (parg1);
9991
9992               if (TREE_CODE (parg0) == MULT_EXPR
9993                   && TREE_CODE (parg1) != MULT_EXPR)
9994                 return fold_build2 (pcode, type,
9995                                     fold_build2 (PLUS_EXPR, type,
9996                                                  fold_convert (type, parg0),
9997                                                  fold_convert (type, marg)),
9998                                     fold_convert (type, parg1));
9999               if (TREE_CODE (parg0) != MULT_EXPR
10000                   && TREE_CODE (parg1) == MULT_EXPR)
10001                 return fold_build2 (PLUS_EXPR, type,
10002                                     fold_convert (type, parg0),
10003                                     fold_build2 (pcode, type,
10004                                                  fold_convert (type, marg),
10005                                                  fold_convert (type,
10006                                                                parg1)));
10007             }
10008         }
10009       else
10010         {
10011           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
10012           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10013             return non_lvalue (fold_convert (type, arg0));
10014
10015           /* Likewise if the operands are reversed.  */
10016           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10017             return non_lvalue (fold_convert (type, arg1));
10018
10019           /* Convert X + -C into X - C.  */
10020           if (TREE_CODE (arg1) == REAL_CST
10021               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10022             {
10023               tem = fold_negate_const (arg1, type);
10024               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10025                 return fold_build2 (MINUS_EXPR, type,
10026                                     fold_convert (type, arg0),
10027                                     fold_convert (type, tem));
10028             }
10029
10030           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10031              to __complex__ ( x, y ).  This is not the same for SNaNs or
10032              if signed zeros are involved.  */
10033           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10034               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10035               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10036             {
10037               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10038               tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10039               tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10040               bool arg0rz = false, arg0iz = false;
10041               if ((arg0r && (arg0rz = real_zerop (arg0r)))
10042                   || (arg0i && (arg0iz = real_zerop (arg0i))))
10043                 {
10044                   tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10045                   tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10046                   if (arg0rz && arg1i && real_zerop (arg1i))
10047                     {
10048                       tree rp = arg1r ? arg1r
10049                                   : build1 (REALPART_EXPR, rtype, arg1);
10050                       tree ip = arg0i ? arg0i
10051                                   : build1 (IMAGPART_EXPR, rtype, arg0);
10052                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10053                     }
10054                   else if (arg0iz && arg1r && real_zerop (arg1r))
10055                     {
10056                       tree rp = arg0r ? arg0r
10057                                   : build1 (REALPART_EXPR, rtype, arg0);
10058                       tree ip = arg1i ? arg1i
10059                                   : build1 (IMAGPART_EXPR, rtype, arg1);
10060                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10061                     }
10062                 }
10063             }
10064
10065           if (flag_unsafe_math_optimizations
10066               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10067               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10068               && (tem = distribute_real_division (code, type, arg0, arg1)))
10069             return tem;
10070
10071           /* Convert x+x into x*2.0.  */
10072           if (operand_equal_p (arg0, arg1, 0)
10073               && SCALAR_FLOAT_TYPE_P (type))
10074             return fold_build2 (MULT_EXPR, type, arg0,
10075                                 build_real (type, dconst2));
10076
10077           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.  
10078              We associate floats only if the user has specified
10079              -fassociative-math.  */
10080           if (flag_associative_math
10081               && TREE_CODE (arg1) == PLUS_EXPR
10082               && TREE_CODE (arg0) != MULT_EXPR)
10083             {
10084               tree tree10 = TREE_OPERAND (arg1, 0);
10085               tree tree11 = TREE_OPERAND (arg1, 1);
10086               if (TREE_CODE (tree11) == MULT_EXPR
10087                   && TREE_CODE (tree10) == MULT_EXPR)
10088                 {
10089                   tree tree0;
10090                   tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
10091                   return fold_build2 (PLUS_EXPR, type, tree0, tree11);
10092                 }
10093             }
10094           /* Convert (b*c + d*e) + a into b*c + (d*e +a).  
10095              We associate floats only if the user has specified
10096              -fassociative-math.  */
10097           if (flag_associative_math
10098               && TREE_CODE (arg0) == PLUS_EXPR
10099               && TREE_CODE (arg1) != MULT_EXPR)
10100             {
10101               tree tree00 = TREE_OPERAND (arg0, 0);
10102               tree tree01 = TREE_OPERAND (arg0, 1);
10103               if (TREE_CODE (tree01) == MULT_EXPR
10104                   && TREE_CODE (tree00) == MULT_EXPR)
10105                 {
10106                   tree tree0;
10107                   tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
10108                   return fold_build2 (PLUS_EXPR, type, tree00, tree0);
10109                 }
10110             }
10111         }
10112
10113      bit_rotate:
10114       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10115          is a rotate of A by C1 bits.  */
10116       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10117          is a rotate of A by B bits.  */
10118       {
10119         enum tree_code code0, code1;
10120         tree rtype;
10121         code0 = TREE_CODE (arg0);
10122         code1 = TREE_CODE (arg1);
10123         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10124              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10125             && operand_equal_p (TREE_OPERAND (arg0, 0),
10126                                 TREE_OPERAND (arg1, 0), 0)
10127             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10128                 TYPE_UNSIGNED (rtype))
10129             /* Only create rotates in complete modes.  Other cases are not
10130                expanded properly.  */
10131             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10132           {
10133             tree tree01, tree11;
10134             enum tree_code code01, code11;
10135
10136             tree01 = TREE_OPERAND (arg0, 1);
10137             tree11 = TREE_OPERAND (arg1, 1);
10138             STRIP_NOPS (tree01);
10139             STRIP_NOPS (tree11);
10140             code01 = TREE_CODE (tree01);
10141             code11 = TREE_CODE (tree11);
10142             if (code01 == INTEGER_CST
10143                 && code11 == INTEGER_CST
10144                 && TREE_INT_CST_HIGH (tree01) == 0
10145                 && TREE_INT_CST_HIGH (tree11) == 0
10146                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10147                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10148               return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
10149                              code0 == LSHIFT_EXPR ? tree01 : tree11);
10150             else if (code11 == MINUS_EXPR)
10151               {
10152                 tree tree110, tree111;
10153                 tree110 = TREE_OPERAND (tree11, 0);
10154                 tree111 = TREE_OPERAND (tree11, 1);
10155                 STRIP_NOPS (tree110);
10156                 STRIP_NOPS (tree111);
10157                 if (TREE_CODE (tree110) == INTEGER_CST
10158                     && 0 == compare_tree_int (tree110,
10159                                               TYPE_PRECISION
10160                                               (TREE_TYPE (TREE_OPERAND
10161                                                           (arg0, 0))))
10162                     && operand_equal_p (tree01, tree111, 0))
10163                   return build2 ((code0 == LSHIFT_EXPR
10164                                   ? LROTATE_EXPR
10165                                   : RROTATE_EXPR),
10166                                  type, TREE_OPERAND (arg0, 0), tree01);
10167               }
10168             else if (code01 == MINUS_EXPR)
10169               {
10170                 tree tree010, tree011;
10171                 tree010 = TREE_OPERAND (tree01, 0);
10172                 tree011 = TREE_OPERAND (tree01, 1);
10173                 STRIP_NOPS (tree010);
10174                 STRIP_NOPS (tree011);
10175                 if (TREE_CODE (tree010) == INTEGER_CST
10176                     && 0 == compare_tree_int (tree010,
10177                                               TYPE_PRECISION
10178                                               (TREE_TYPE (TREE_OPERAND
10179                                                           (arg0, 0))))
10180                     && operand_equal_p (tree11, tree011, 0))
10181                   return build2 ((code0 != LSHIFT_EXPR
10182                                   ? LROTATE_EXPR
10183                                   : RROTATE_EXPR),
10184                                  type, TREE_OPERAND (arg0, 0), tree11);
10185               }
10186           }
10187       }
10188
10189     associate:
10190       /* In most languages, can't associate operations on floats through
10191          parentheses.  Rather than remember where the parentheses were, we
10192          don't associate floats at all, unless the user has specified
10193          -fassociative-math.
10194          And, we need to make sure type is not saturating.  */
10195
10196       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10197           && !TYPE_SATURATING (type))
10198         {
10199           tree var0, con0, lit0, minus_lit0;
10200           tree var1, con1, lit1, minus_lit1;
10201           bool ok = true;
10202
10203           /* Split both trees into variables, constants, and literals.  Then
10204              associate each group together, the constants with literals,
10205              then the result with variables.  This increases the chances of
10206              literals being recombined later and of generating relocatable
10207              expressions for the sum of a constant and literal.  */
10208           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10209           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10210                              code == MINUS_EXPR);
10211
10212           /* With undefined overflow we can only associate constants
10213              with one variable.  */
10214           if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10215                || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10216               && var0 && var1)
10217             {
10218               tree tmp0 = var0;
10219               tree tmp1 = var1;
10220
10221               if (TREE_CODE (tmp0) == NEGATE_EXPR)
10222                 tmp0 = TREE_OPERAND (tmp0, 0);
10223               if (TREE_CODE (tmp1) == NEGATE_EXPR)
10224                 tmp1 = TREE_OPERAND (tmp1, 0);
10225               /* The only case we can still associate with two variables
10226                  is if they are the same, modulo negation.  */
10227               if (!operand_equal_p (tmp0, tmp1, 0))
10228                 ok = false;
10229             }
10230
10231           /* Only do something if we found more than two objects.  Otherwise,
10232              nothing has changed and we risk infinite recursion.  */
10233           if (ok
10234               && (2 < ((var0 != 0) + (var1 != 0)
10235                        + (con0 != 0) + (con1 != 0)
10236                        + (lit0 != 0) + (lit1 != 0)
10237                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10238             {
10239               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
10240               if (code == MINUS_EXPR)
10241                 code = PLUS_EXPR;
10242
10243               var0 = associate_trees (var0, var1, code, type);
10244               con0 = associate_trees (con0, con1, code, type);
10245               lit0 = associate_trees (lit0, lit1, code, type);
10246               minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
10247
10248               /* Preserve the MINUS_EXPR if the negative part of the literal is
10249                  greater than the positive part.  Otherwise, the multiplicative
10250                  folding code (i.e extract_muldiv) may be fooled in case
10251                  unsigned constants are subtracted, like in the following
10252                  example: ((X*2 + 4) - 8U)/2.  */
10253               if (minus_lit0 && lit0)
10254                 {
10255                   if (TREE_CODE (lit0) == INTEGER_CST
10256                       && TREE_CODE (minus_lit0) == INTEGER_CST
10257                       && tree_int_cst_lt (lit0, minus_lit0))
10258                     {
10259                       minus_lit0 = associate_trees (minus_lit0, lit0,
10260                                                     MINUS_EXPR, type);
10261                       lit0 = 0;
10262                     }
10263                   else
10264                     {
10265                       lit0 = associate_trees (lit0, minus_lit0,
10266                                               MINUS_EXPR, type);
10267                       minus_lit0 = 0;
10268                     }
10269                 }
10270               if (minus_lit0)
10271                 {
10272                   if (con0 == 0)
10273                     return fold_convert (type,
10274                                          associate_trees (var0, minus_lit0,
10275                                                           MINUS_EXPR, type));
10276                   else
10277                     {
10278                       con0 = associate_trees (con0, minus_lit0,
10279                                               MINUS_EXPR, type);
10280                       return fold_convert (type,
10281                                            associate_trees (var0, con0,
10282                                                             PLUS_EXPR, type));
10283                     }
10284                 }
10285
10286               con0 = associate_trees (con0, lit0, code, type);
10287               return fold_convert (type, associate_trees (var0, con0,
10288                                                           code, type));
10289             }
10290         }
10291
10292       return NULL_TREE;
10293
10294     case MINUS_EXPR:
10295       /* Pointer simplifications for subtraction, simple reassociations. */
10296       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10297         {
10298           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10299           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10300               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10301             {
10302               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10303               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10304               tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10305               tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10306               return fold_build2 (PLUS_EXPR, type,
10307                                   fold_build2 (MINUS_EXPR, type, arg00, arg10),
10308                                   fold_build2 (MINUS_EXPR, type, arg01, arg11));
10309             }
10310           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10311           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10312             {
10313               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10314               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10315               tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
10316               if (tmp)
10317                 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
10318             }
10319         }
10320       /* A - (-B) -> A + B */
10321       if (TREE_CODE (arg1) == NEGATE_EXPR)
10322         return fold_build2 (PLUS_EXPR, type, op0,
10323                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10324       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10325       if (TREE_CODE (arg0) == NEGATE_EXPR
10326           && (FLOAT_TYPE_P (type)
10327               || INTEGRAL_TYPE_P (type))
10328           && negate_expr_p (arg1)
10329           && reorder_operands_p (arg0, arg1))
10330         return fold_build2 (MINUS_EXPR, type,
10331                             fold_convert (type, negate_expr (arg1)),
10332                             fold_convert (type, TREE_OPERAND (arg0, 0)));
10333       /* Convert -A - 1 to ~A.  */
10334       if (INTEGRAL_TYPE_P (type)
10335           && TREE_CODE (arg0) == NEGATE_EXPR
10336           && integer_onep (arg1)
10337           && !TYPE_OVERFLOW_TRAPS (type))
10338         return fold_build1 (BIT_NOT_EXPR, type,
10339                             fold_convert (type, TREE_OPERAND (arg0, 0)));
10340
10341       /* Convert -1 - A to ~A.  */
10342       if (INTEGRAL_TYPE_P (type)
10343           && integer_all_onesp (arg0))
10344         return fold_build1 (BIT_NOT_EXPR, type, op1);
10345
10346
10347       /* X - (X / CST) * CST is X % CST.  */
10348       if (INTEGRAL_TYPE_P (type)
10349           && TREE_CODE (arg1) == MULT_EXPR
10350           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10351           && operand_equal_p (arg0,
10352                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10353           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10354                               TREE_OPERAND (arg1, 1), 0))
10355         return fold_convert (type,
10356                              fold_build2 (TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10357                                           arg0, TREE_OPERAND (arg1, 1)));
10358
10359       if (! FLOAT_TYPE_P (type))
10360         {
10361           if (integer_zerop (arg0))
10362             return negate_expr (fold_convert (type, arg1));
10363           if (integer_zerop (arg1))
10364             return non_lvalue (fold_convert (type, arg0));
10365
10366           /* Fold A - (A & B) into ~B & A.  */
10367           if (!TREE_SIDE_EFFECTS (arg0)
10368               && TREE_CODE (arg1) == BIT_AND_EXPR)
10369             {
10370               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10371                 {
10372                   tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10373                   return fold_build2 (BIT_AND_EXPR, type,
10374                                       fold_build1 (BIT_NOT_EXPR, type, arg10),
10375                                       fold_convert (type, arg0));
10376                 }
10377               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10378                 {
10379                   tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10380                   return fold_build2 (BIT_AND_EXPR, type,
10381                                       fold_build1 (BIT_NOT_EXPR, type, arg11),
10382                                       fold_convert (type, arg0));
10383                 }
10384             }
10385
10386           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10387              any power of 2 minus 1.  */
10388           if (TREE_CODE (arg0) == BIT_AND_EXPR
10389               && TREE_CODE (arg1) == BIT_AND_EXPR
10390               && operand_equal_p (TREE_OPERAND (arg0, 0),
10391                                   TREE_OPERAND (arg1, 0), 0))
10392             {
10393               tree mask0 = TREE_OPERAND (arg0, 1);
10394               tree mask1 = TREE_OPERAND (arg1, 1);
10395               tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
10396
10397               if (operand_equal_p (tem, mask1, 0))
10398                 {
10399                   tem = fold_build2 (BIT_XOR_EXPR, type,
10400                                      TREE_OPERAND (arg0, 0), mask1);
10401                   return fold_build2 (MINUS_EXPR, type, tem, mask1);
10402                 }
10403             }
10404         }
10405
10406       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10407       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10408         return non_lvalue (fold_convert (type, arg0));
10409
10410       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10411          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10412          (-ARG1 + ARG0) reduces to -ARG1.  */
10413       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10414         return negate_expr (fold_convert (type, arg1));
10415
10416       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10417          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10418          signed zeros are involved.  */
10419       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10420           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10421           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10422         {
10423           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10424           tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10425           tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10426           bool arg0rz = false, arg0iz = false;
10427           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10428               || (arg0i && (arg0iz = real_zerop (arg0i))))
10429             {
10430               tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10431               tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10432               if (arg0rz && arg1i && real_zerop (arg1i))
10433                 {
10434                   tree rp = fold_build1 (NEGATE_EXPR, rtype,
10435                                          arg1r ? arg1r
10436                                          : build1 (REALPART_EXPR, rtype, arg1));
10437                   tree ip = arg0i ? arg0i
10438                     : build1 (IMAGPART_EXPR, rtype, arg0);
10439                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10440                 }
10441               else if (arg0iz && arg1r && real_zerop (arg1r))
10442                 {
10443                   tree rp = arg0r ? arg0r
10444                     : build1 (REALPART_EXPR, rtype, arg0);
10445                   tree ip = fold_build1 (NEGATE_EXPR, rtype,
10446                                          arg1i ? arg1i
10447                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10448                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10449                 }
10450             }
10451         }
10452
10453       /* Fold &x - &x.  This can happen from &x.foo - &x.
10454          This is unsafe for certain floats even in non-IEEE formats.
10455          In IEEE, it is unsafe because it does wrong for NaNs.
10456          Also note that operand_equal_p is always false if an operand
10457          is volatile.  */
10458
10459       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10460           && operand_equal_p (arg0, arg1, 0))
10461         return fold_convert (type, integer_zero_node);
10462
10463       /* A - B -> A + (-B) if B is easily negatable.  */
10464       if (negate_expr_p (arg1)
10465           && ((FLOAT_TYPE_P (type)
10466                /* Avoid this transformation if B is a positive REAL_CST.  */
10467                && (TREE_CODE (arg1) != REAL_CST
10468                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10469               || INTEGRAL_TYPE_P (type)))
10470         return fold_build2 (PLUS_EXPR, type,
10471                             fold_convert (type, arg0),
10472                             fold_convert (type, negate_expr (arg1)));
10473
10474       /* Try folding difference of addresses.  */
10475       {
10476         HOST_WIDE_INT diff;
10477
10478         if ((TREE_CODE (arg0) == ADDR_EXPR
10479              || TREE_CODE (arg1) == ADDR_EXPR)
10480             && ptr_difference_const (arg0, arg1, &diff))
10481           return build_int_cst_type (type, diff);
10482       }
10483
10484       /* Fold &a[i] - &a[j] to i-j.  */
10485       if (TREE_CODE (arg0) == ADDR_EXPR
10486           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10487           && TREE_CODE (arg1) == ADDR_EXPR
10488           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10489         {
10490           tree aref0 = TREE_OPERAND (arg0, 0);
10491           tree aref1 = TREE_OPERAND (arg1, 0);
10492           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10493                                TREE_OPERAND (aref1, 0), 0))
10494             {
10495               tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10496               tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10497               tree esz = array_ref_element_size (aref0);
10498               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10499               return fold_build2 (MULT_EXPR, type, diff,
10500                                   fold_convert (type, esz));
10501                                   
10502             }
10503         }
10504
10505       if (flag_unsafe_math_optimizations
10506           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10507           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10508           && (tem = distribute_real_division (code, type, arg0, arg1)))
10509         return tem;
10510
10511       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10512          same or one.  Make sure type is not saturating.
10513          fold_plusminus_mult_expr will re-associate.  */
10514       if ((TREE_CODE (arg0) == MULT_EXPR
10515            || TREE_CODE (arg1) == MULT_EXPR)
10516           && !TYPE_SATURATING (type)
10517           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10518         {
10519           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10520           if (tem)
10521             return tem;
10522         }
10523
10524       goto associate;
10525
10526     case MULT_EXPR:
10527       /* (-A) * (-B) -> A * B  */
10528       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10529         return fold_build2 (MULT_EXPR, type,
10530                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10531                             fold_convert (type, negate_expr (arg1)));
10532       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10533         return fold_build2 (MULT_EXPR, type,
10534                             fold_convert (type, negate_expr (arg0)),
10535                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10536
10537       if (! FLOAT_TYPE_P (type))
10538         {
10539           if (integer_zerop (arg1))
10540             return omit_one_operand (type, arg1, arg0);
10541           if (integer_onep (arg1))
10542             return non_lvalue (fold_convert (type, arg0));
10543           /* Transform x * -1 into -x.  Make sure to do the negation
10544              on the original operand with conversions not stripped
10545              because we can only strip non-sign-changing conversions.  */
10546           if (integer_all_onesp (arg1))
10547             return fold_convert (type, negate_expr (op0));
10548           /* Transform x * -C into -x * C if x is easily negatable.  */
10549           if (TREE_CODE (arg1) == INTEGER_CST
10550               && tree_int_cst_sgn (arg1) == -1
10551               && negate_expr_p (arg0)
10552               && (tem = negate_expr (arg1)) != arg1
10553               && !TREE_OVERFLOW (tem))
10554             return fold_build2 (MULT_EXPR, type,
10555                                 fold_convert (type, negate_expr (arg0)), tem);
10556
10557           /* (a * (1 << b)) is (a << b)  */
10558           if (TREE_CODE (arg1) == LSHIFT_EXPR
10559               && integer_onep (TREE_OPERAND (arg1, 0)))
10560             return fold_build2 (LSHIFT_EXPR, type, op0,
10561                                 TREE_OPERAND (arg1, 1));
10562           if (TREE_CODE (arg0) == LSHIFT_EXPR
10563               && integer_onep (TREE_OPERAND (arg0, 0)))
10564             return fold_build2 (LSHIFT_EXPR, type, op1,
10565                                 TREE_OPERAND (arg0, 1));
10566
10567           /* (A + A) * C -> A * 2 * C  */
10568           if (TREE_CODE (arg0) == PLUS_EXPR
10569               && TREE_CODE (arg1) == INTEGER_CST
10570               && operand_equal_p (TREE_OPERAND (arg0, 0),
10571                                   TREE_OPERAND (arg0, 1), 0))
10572             return fold_build2 (MULT_EXPR, type,
10573                                 omit_one_operand (type, TREE_OPERAND (arg0, 0),
10574                                                   TREE_OPERAND (arg0, 1)),
10575                                 fold_build2 (MULT_EXPR, type,
10576                                              build_int_cst (type, 2) , arg1));
10577
10578           strict_overflow_p = false;
10579           if (TREE_CODE (arg1) == INTEGER_CST
10580               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10581                                              &strict_overflow_p)))
10582             {
10583               if (strict_overflow_p)
10584                 fold_overflow_warning (("assuming signed overflow does not "
10585                                         "occur when simplifying "
10586                                         "multiplication"),
10587                                        WARN_STRICT_OVERFLOW_MISC);
10588               return fold_convert (type, tem);
10589             }
10590
10591           /* Optimize z * conj(z) for integer complex numbers.  */
10592           if (TREE_CODE (arg0) == CONJ_EXPR
10593               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10594             return fold_mult_zconjz (type, arg1);
10595           if (TREE_CODE (arg1) == CONJ_EXPR
10596               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10597             return fold_mult_zconjz (type, arg0);
10598         }
10599       else
10600         {
10601           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10602              when x is NaN, since x * 0 is also NaN.  Nor are they the
10603              same in modes with signed zeros, since multiplying a
10604              negative value by 0 gives -0, not +0.  */
10605           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10606               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10607               && real_zerop (arg1))
10608             return omit_one_operand (type, arg1, arg0);
10609           /* In IEEE floating point, x*1 is not equivalent to x for snans.  */
10610           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10611               && real_onep (arg1))
10612             return non_lvalue (fold_convert (type, arg0));
10613
10614           /* Transform x * -1.0 into -x.  */
10615           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10616               && real_minus_onep (arg1))
10617             return fold_convert (type, negate_expr (arg0));
10618
10619           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10620              the result for floating point types due to rounding so it is applied
10621              only if -fassociative-math was specify.  */
10622           if (flag_associative_math
10623               && TREE_CODE (arg0) == RDIV_EXPR
10624               && TREE_CODE (arg1) == REAL_CST
10625               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10626             {
10627               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10628                                       arg1, 0);
10629               if (tem)
10630                 return fold_build2 (RDIV_EXPR, type, tem,
10631                                     TREE_OPERAND (arg0, 1));
10632             }
10633
10634           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10635           if (operand_equal_p (arg0, arg1, 0))
10636             {
10637               tree tem = fold_strip_sign_ops (arg0);
10638               if (tem != NULL_TREE)
10639                 {
10640                   tem = fold_convert (type, tem);
10641                   return fold_build2 (MULT_EXPR, type, tem, tem);
10642                 }
10643             }
10644
10645           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10646              This is not the same for NaNs or if signed zeros are
10647              involved.  */
10648           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10649               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10650               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10651               && TREE_CODE (arg1) == COMPLEX_CST
10652               && real_zerop (TREE_REALPART (arg1)))
10653             {
10654               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10655               if (real_onep (TREE_IMAGPART (arg1)))
10656                 return fold_build2 (COMPLEX_EXPR, type,
10657                                     negate_expr (fold_build1 (IMAGPART_EXPR,
10658                                                               rtype, arg0)),
10659                                     fold_build1 (REALPART_EXPR, rtype, arg0));
10660               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10661                 return fold_build2 (COMPLEX_EXPR, type,
10662                                     fold_build1 (IMAGPART_EXPR, rtype, arg0),
10663                                     negate_expr (fold_build1 (REALPART_EXPR,
10664                                                               rtype, arg0)));
10665             }
10666
10667           /* Optimize z * conj(z) for floating point complex numbers.
10668              Guarded by flag_unsafe_math_optimizations as non-finite
10669              imaginary components don't produce scalar results.  */
10670           if (flag_unsafe_math_optimizations
10671               && TREE_CODE (arg0) == CONJ_EXPR
10672               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10673             return fold_mult_zconjz (type, arg1);
10674           if (flag_unsafe_math_optimizations
10675               && TREE_CODE (arg1) == CONJ_EXPR
10676               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10677             return fold_mult_zconjz (type, arg0);
10678
10679           if (flag_unsafe_math_optimizations)
10680             {
10681               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10682               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10683
10684               /* Optimizations of root(...)*root(...).  */
10685               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10686                 {
10687                   tree rootfn, arg;
10688                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10689                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10690
10691                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10692                   if (BUILTIN_SQRT_P (fcode0)
10693                       && operand_equal_p (arg00, arg10, 0)
10694                       && ! HONOR_SNANS (TYPE_MODE (type)))
10695                     return arg00;
10696
10697                   /* Optimize root(x)*root(y) as root(x*y).  */
10698                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10699                   arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10700                   return build_call_expr (rootfn, 1, arg);
10701                 }
10702
10703               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10704               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10705                 {
10706                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10707                   tree arg = fold_build2 (PLUS_EXPR, type,
10708                                           CALL_EXPR_ARG (arg0, 0),
10709                                           CALL_EXPR_ARG (arg1, 0));
10710                   return build_call_expr (expfn, 1, arg);
10711                 }
10712
10713               /* Optimizations of pow(...)*pow(...).  */
10714               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10715                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10716                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10717                 {
10718                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10719                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10720                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10721                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10722
10723                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10724                   if (operand_equal_p (arg01, arg11, 0))
10725                     {
10726                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10727                       tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10728                       return build_call_expr (powfn, 2, arg, arg01);
10729                     }
10730
10731                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10732                   if (operand_equal_p (arg00, arg10, 0))
10733                     {
10734                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10735                       tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10736                       return build_call_expr (powfn, 2, arg00, arg);
10737                     }
10738                 }
10739
10740               /* Optimize tan(x)*cos(x) as sin(x).  */
10741               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10742                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10743                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10744                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10745                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10746                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10747                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10748                                       CALL_EXPR_ARG (arg1, 0), 0))
10749                 {
10750                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10751
10752                   if (sinfn != NULL_TREE)
10753                     return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10754                 }
10755
10756               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10757               if (fcode1 == BUILT_IN_POW
10758                   || fcode1 == BUILT_IN_POWF
10759                   || fcode1 == BUILT_IN_POWL)
10760                 {
10761                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10762                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10763                   if (TREE_CODE (arg11) == REAL_CST
10764                       && !TREE_OVERFLOW (arg11)
10765                       && operand_equal_p (arg0, arg10, 0))
10766                     {
10767                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10768                       REAL_VALUE_TYPE c;
10769                       tree arg;
10770
10771                       c = TREE_REAL_CST (arg11);
10772                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10773                       arg = build_real (type, c);
10774                       return build_call_expr (powfn, 2, arg0, arg);
10775                     }
10776                 }
10777
10778               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10779               if (fcode0 == BUILT_IN_POW
10780                   || fcode0 == BUILT_IN_POWF
10781                   || fcode0 == BUILT_IN_POWL)
10782                 {
10783                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10784                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10785                   if (TREE_CODE (arg01) == REAL_CST
10786                       && !TREE_OVERFLOW (arg01)
10787                       && operand_equal_p (arg1, arg00, 0))
10788                     {
10789                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10790                       REAL_VALUE_TYPE c;
10791                       tree arg;
10792
10793                       c = TREE_REAL_CST (arg01);
10794                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10795                       arg = build_real (type, c);
10796                       return build_call_expr (powfn, 2, arg1, arg);
10797                     }
10798                 }
10799
10800               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10801               if (optimize_function_for_speed_p (cfun)
10802                   && operand_equal_p (arg0, arg1, 0))
10803                 {
10804                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10805
10806                   if (powfn)
10807                     {
10808                       tree arg = build_real (type, dconst2);
10809                       return build_call_expr (powfn, 2, arg0, arg);
10810                     }
10811                 }
10812             }
10813         }
10814       goto associate;
10815
10816     case BIT_IOR_EXPR:
10817     bit_ior:
10818       if (integer_all_onesp (arg1))
10819         return omit_one_operand (type, arg1, arg0);
10820       if (integer_zerop (arg1))
10821         return non_lvalue (fold_convert (type, arg0));
10822       if (operand_equal_p (arg0, arg1, 0))
10823         return non_lvalue (fold_convert (type, arg0));
10824
10825       /* ~X | X is -1.  */
10826       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10827           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10828         {
10829           t1 = fold_convert (type, integer_zero_node);
10830           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10831           return omit_one_operand (type, t1, arg1);
10832         }
10833
10834       /* X | ~X is -1.  */
10835       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10836           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10837         {
10838           t1 = fold_convert (type, integer_zero_node);
10839           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10840           return omit_one_operand (type, t1, arg0);
10841         }
10842
10843       /* Canonicalize (X & C1) | C2.  */
10844       if (TREE_CODE (arg0) == BIT_AND_EXPR
10845           && TREE_CODE (arg1) == INTEGER_CST
10846           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10847         {
10848           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10849           int width = TYPE_PRECISION (type), w;
10850           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10851           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10852           hi2 = TREE_INT_CST_HIGH (arg1);
10853           lo2 = TREE_INT_CST_LOW (arg1);
10854
10855           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10856           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10857             return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10858
10859           if (width > HOST_BITS_PER_WIDE_INT)
10860             {
10861               mhi = (unsigned HOST_WIDE_INT) -1 
10862                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10863               mlo = -1;
10864             }
10865           else
10866             {
10867               mhi = 0;
10868               mlo = (unsigned HOST_WIDE_INT) -1
10869                     >> (HOST_BITS_PER_WIDE_INT - width);
10870             }
10871
10872           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10873           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10874             return fold_build2 (BIT_IOR_EXPR, type,
10875                                 TREE_OPERAND (arg0, 0), arg1);
10876
10877           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10878              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10879              mode which allows further optimizations.  */
10880           hi1 &= mhi;
10881           lo1 &= mlo;
10882           hi2 &= mhi;
10883           lo2 &= mlo;
10884           hi3 = hi1 & ~hi2;
10885           lo3 = lo1 & ~lo2;
10886           for (w = BITS_PER_UNIT;
10887                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10888                w <<= 1)
10889             {
10890               unsigned HOST_WIDE_INT mask
10891                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10892               if (((lo1 | lo2) & mask) == mask
10893                   && (lo1 & ~mask) == 0 && hi1 == 0)
10894                 {
10895                   hi3 = 0;
10896                   lo3 = mask;
10897                   break;
10898                 }
10899             }
10900           if (hi3 != hi1 || lo3 != lo1)
10901             return fold_build2 (BIT_IOR_EXPR, type,
10902                                 fold_build2 (BIT_AND_EXPR, type,
10903                                              TREE_OPERAND (arg0, 0),
10904                                              build_int_cst_wide (type,
10905                                                                  lo3, hi3)),
10906                                 arg1);
10907         }
10908
10909       /* (X & Y) | Y is (X, Y).  */
10910       if (TREE_CODE (arg0) == BIT_AND_EXPR
10911           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10912         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10913       /* (X & Y) | X is (Y, X).  */
10914       if (TREE_CODE (arg0) == BIT_AND_EXPR
10915           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10916           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10917         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10918       /* X | (X & Y) is (Y, X).  */
10919       if (TREE_CODE (arg1) == BIT_AND_EXPR
10920           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10921           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10922         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10923       /* X | (Y & X) is (Y, X).  */
10924       if (TREE_CODE (arg1) == BIT_AND_EXPR
10925           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10926           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10927         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10928
10929       t1 = distribute_bit_expr (code, type, arg0, arg1);
10930       if (t1 != NULL_TREE)
10931         return t1;
10932
10933       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10934
10935          This results in more efficient code for machines without a NAND
10936          instruction.  Combine will canonicalize to the first form
10937          which will allow use of NAND instructions provided by the
10938          backend if they exist.  */
10939       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10940           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10941         {
10942           return fold_build1 (BIT_NOT_EXPR, type,
10943                               build2 (BIT_AND_EXPR, type,
10944                                       fold_convert (type,
10945                                                     TREE_OPERAND (arg0, 0)),
10946                                       fold_convert (type,
10947                                                     TREE_OPERAND (arg1, 0))));
10948         }
10949
10950       /* See if this can be simplified into a rotate first.  If that
10951          is unsuccessful continue in the association code.  */
10952       goto bit_rotate;
10953
10954     case BIT_XOR_EXPR:
10955       if (integer_zerop (arg1))
10956         return non_lvalue (fold_convert (type, arg0));
10957       if (integer_all_onesp (arg1))
10958         return fold_build1 (BIT_NOT_EXPR, type, op0);
10959       if (operand_equal_p (arg0, arg1, 0))
10960         return omit_one_operand (type, integer_zero_node, arg0);
10961
10962       /* ~X ^ X is -1.  */
10963       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10964           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10965         {
10966           t1 = fold_convert (type, integer_zero_node);
10967           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10968           return omit_one_operand (type, t1, arg1);
10969         }
10970
10971       /* X ^ ~X is -1.  */
10972       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10973           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10974         {
10975           t1 = fold_convert (type, integer_zero_node);
10976           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10977           return omit_one_operand (type, t1, arg0);
10978         }
10979
10980       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10981          with a constant, and the two constants have no bits in common,
10982          we should treat this as a BIT_IOR_EXPR since this may produce more
10983          simplifications.  */
10984       if (TREE_CODE (arg0) == BIT_AND_EXPR
10985           && TREE_CODE (arg1) == BIT_AND_EXPR
10986           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10987           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10988           && integer_zerop (const_binop (BIT_AND_EXPR,
10989                                          TREE_OPERAND (arg0, 1),
10990                                          TREE_OPERAND (arg1, 1), 0)))
10991         {
10992           code = BIT_IOR_EXPR;
10993           goto bit_ior;
10994         }
10995
10996       /* (X | Y) ^ X -> Y & ~ X*/
10997       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10998           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10999         {
11000           tree t2 = TREE_OPERAND (arg0, 1);
11001           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
11002                             arg1);
11003           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11004                             fold_convert (type, t1));
11005           return t1;
11006         }
11007
11008       /* (Y | X) ^ X -> Y & ~ X*/
11009       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11010           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11011         {
11012           tree t2 = TREE_OPERAND (arg0, 0);
11013           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
11014                             arg1);
11015           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11016                             fold_convert (type, t1));
11017           return t1;
11018         }
11019
11020       /* X ^ (X | Y) -> Y & ~ X*/
11021       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11022           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11023         {
11024           tree t2 = TREE_OPERAND (arg1, 1);
11025           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11026                             arg0);
11027           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11028                             fold_convert (type, t1));
11029           return t1;
11030         }
11031
11032       /* X ^ (Y | X) -> Y & ~ X*/
11033       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11034           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11035         {
11036           tree t2 = TREE_OPERAND (arg1, 0);
11037           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11038                             arg0);
11039           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11040                             fold_convert (type, t1));
11041           return t1;
11042         }
11043         
11044       /* Convert ~X ^ ~Y to X ^ Y.  */
11045       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11046           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11047         return fold_build2 (code, type,
11048                             fold_convert (type, TREE_OPERAND (arg0, 0)),
11049                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11050
11051       /* Convert ~X ^ C to X ^ ~C.  */
11052       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11053           && TREE_CODE (arg1) == INTEGER_CST)
11054         return fold_build2 (code, type,
11055                             fold_convert (type, TREE_OPERAND (arg0, 0)),
11056                             fold_build1 (BIT_NOT_EXPR, type, arg1));
11057
11058       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
11059       if (TREE_CODE (arg0) == BIT_AND_EXPR
11060           && integer_onep (TREE_OPERAND (arg0, 1))
11061           && integer_onep (arg1))
11062         return fold_build2 (EQ_EXPR, type, arg0,
11063                             build_int_cst (TREE_TYPE (arg0), 0));
11064
11065       /* Fold (X & Y) ^ Y as ~X & Y.  */
11066       if (TREE_CODE (arg0) == BIT_AND_EXPR
11067           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11068         {
11069           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11070           return fold_build2 (BIT_AND_EXPR, type, 
11071                               fold_build1 (BIT_NOT_EXPR, type, tem),
11072                               fold_convert (type, arg1));
11073         }
11074       /* Fold (X & Y) ^ X as ~Y & X.  */
11075       if (TREE_CODE (arg0) == BIT_AND_EXPR
11076           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11077           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11078         {
11079           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11080           return fold_build2 (BIT_AND_EXPR, type,
11081                               fold_build1 (BIT_NOT_EXPR, type, tem),
11082                               fold_convert (type, arg1));
11083         }
11084       /* Fold X ^ (X & Y) as X & ~Y.  */
11085       if (TREE_CODE (arg1) == BIT_AND_EXPR
11086           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11087         {
11088           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11089           return fold_build2 (BIT_AND_EXPR, type,
11090                               fold_convert (type, arg0),
11091                               fold_build1 (BIT_NOT_EXPR, type, tem));
11092         }
11093       /* Fold X ^ (Y & X) as ~Y & X.  */
11094       if (TREE_CODE (arg1) == BIT_AND_EXPR
11095           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11096           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11097         {
11098           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11099           return fold_build2 (BIT_AND_EXPR, type,
11100                               fold_build1 (BIT_NOT_EXPR, type, tem),
11101                               fold_convert (type, arg0));
11102         }
11103
11104       /* See if this can be simplified into a rotate first.  If that
11105          is unsuccessful continue in the association code.  */
11106       goto bit_rotate;
11107
11108     case BIT_AND_EXPR:
11109       if (integer_all_onesp (arg1))
11110         return non_lvalue (fold_convert (type, arg0));
11111       if (integer_zerop (arg1))
11112         return omit_one_operand (type, arg1, arg0);
11113       if (operand_equal_p (arg0, arg1, 0))
11114         return non_lvalue (fold_convert (type, arg0));
11115
11116       /* ~X & X is always zero.  */
11117       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11118           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11119         return omit_one_operand (type, integer_zero_node, arg1);
11120
11121       /* X & ~X is always zero.  */
11122       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11123           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11124         return omit_one_operand (type, integer_zero_node, arg0);
11125
11126       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
11127       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11128           && TREE_CODE (arg1) == INTEGER_CST
11129           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11130         {
11131           tree tmp1 = fold_convert (type, arg1);
11132           tree tmp2 = fold_convert (type, TREE_OPERAND (arg0, 0));
11133           tree tmp3 = fold_convert (type, TREE_OPERAND (arg0, 1));
11134           tmp2 = fold_build2 (BIT_AND_EXPR, type, tmp2, tmp1);
11135           tmp3 = fold_build2 (BIT_AND_EXPR, type, tmp3, tmp1);
11136           return fold_convert (type,
11137                                fold_build2 (BIT_IOR_EXPR, type, tmp2, tmp3));
11138         }
11139
11140       /* (X | Y) & Y is (X, Y).  */
11141       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11142           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11143         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
11144       /* (X | Y) & X is (Y, X).  */
11145       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11146           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11147           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11148         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
11149       /* X & (X | Y) is (Y, X).  */
11150       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11151           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11152           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11153         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
11154       /* X & (Y | X) is (Y, X).  */
11155       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11156           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11157           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11158         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
11159
11160       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11161       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11162           && integer_onep (TREE_OPERAND (arg0, 1))
11163           && integer_onep (arg1))
11164         {
11165           tem = TREE_OPERAND (arg0, 0);
11166           return fold_build2 (EQ_EXPR, type,
11167                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11168                                            build_int_cst (TREE_TYPE (tem), 1)),
11169                               build_int_cst (TREE_TYPE (tem), 0));
11170         }
11171       /* Fold ~X & 1 as (X & 1) == 0.  */
11172       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11173           && integer_onep (arg1))
11174         {
11175           tem = TREE_OPERAND (arg0, 0);
11176           return fold_build2 (EQ_EXPR, type,
11177                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11178                                            build_int_cst (TREE_TYPE (tem), 1)),
11179                               build_int_cst (TREE_TYPE (tem), 0));
11180         }
11181
11182       /* Fold (X ^ Y) & Y as ~X & Y.  */
11183       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11184           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11185         {
11186           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11187           return fold_build2 (BIT_AND_EXPR, type, 
11188                               fold_build1 (BIT_NOT_EXPR, type, tem),
11189                               fold_convert (type, arg1));
11190         }
11191       /* Fold (X ^ Y) & X as ~Y & X.  */
11192       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11193           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11194           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11195         {
11196           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11197           return fold_build2 (BIT_AND_EXPR, type,
11198                               fold_build1 (BIT_NOT_EXPR, type, tem),
11199                               fold_convert (type, arg1));
11200         }
11201       /* Fold X & (X ^ Y) as X & ~Y.  */
11202       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11203           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11204         {
11205           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11206           return fold_build2 (BIT_AND_EXPR, type,
11207                               fold_convert (type, arg0),
11208                               fold_build1 (BIT_NOT_EXPR, type, tem));
11209         }
11210       /* Fold X & (Y ^ X) as ~Y & X.  */
11211       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11212           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11213           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11214         {
11215           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11216           return fold_build2 (BIT_AND_EXPR, type,
11217                               fold_build1 (BIT_NOT_EXPR, type, tem),
11218                               fold_convert (type, arg0));
11219         }
11220
11221       t1 = distribute_bit_expr (code, type, arg0, arg1);
11222       if (t1 != NULL_TREE)
11223         return t1;
11224       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11225       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11226           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11227         {
11228           unsigned int prec
11229             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11230
11231           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11232               && (~TREE_INT_CST_LOW (arg1)
11233                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11234             return fold_convert (type, TREE_OPERAND (arg0, 0));
11235         }
11236
11237       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11238
11239          This results in more efficient code for machines without a NOR
11240          instruction.  Combine will canonicalize to the first form
11241          which will allow use of NOR instructions provided by the
11242          backend if they exist.  */
11243       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11244           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11245         {
11246           return fold_build1 (BIT_NOT_EXPR, type,
11247                               build2 (BIT_IOR_EXPR, type,
11248                                       fold_convert (type,
11249                                                     TREE_OPERAND (arg0, 0)),
11250                                       fold_convert (type,
11251                                                     TREE_OPERAND (arg1, 0))));
11252         }
11253
11254       /* If arg0 is derived from the address of an object or function, we may
11255          be able to fold this expression using the object or function's
11256          alignment.  */
11257       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11258         {
11259           unsigned HOST_WIDE_INT modulus, residue;
11260           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11261
11262           modulus = get_pointer_modulus_and_residue (arg0, &residue);
11263
11264           /* This works because modulus is a power of 2.  If this weren't the
11265              case, we'd have to replace it by its greatest power-of-2
11266              divisor: modulus & -modulus.  */
11267           if (low < modulus)
11268             return build_int_cst (type, residue & low);
11269         }
11270
11271       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11272               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11273          if the new mask might be further optimized.  */
11274       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11275            || TREE_CODE (arg0) == RSHIFT_EXPR)
11276           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11277           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11278           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11279              < TYPE_PRECISION (TREE_TYPE (arg0))
11280           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11281           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11282         {
11283           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11284           unsigned HOST_WIDE_INT mask
11285             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11286           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11287           tree shift_type = TREE_TYPE (arg0);
11288
11289           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11290             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11291           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11292                    && TYPE_PRECISION (TREE_TYPE (arg0))
11293                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11294             {
11295               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11296               tree arg00 = TREE_OPERAND (arg0, 0);
11297               /* See if more bits can be proven as zero because of
11298                  zero extension.  */
11299               if (TREE_CODE (arg00) == NOP_EXPR
11300                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11301                 {
11302                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11303                   if (TYPE_PRECISION (inner_type)
11304                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11305                       && TYPE_PRECISION (inner_type) < prec)
11306                     {
11307                       prec = TYPE_PRECISION (inner_type);
11308                       /* See if we can shorten the right shift.  */
11309                       if (shiftc < prec)
11310                         shift_type = inner_type;
11311                     }
11312                 }
11313               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11314               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11315               zerobits <<= prec - shiftc;
11316               /* For arithmetic shift if sign bit could be set, zerobits
11317                  can contain actually sign bits, so no transformation is
11318                  possible, unless MASK masks them all away.  In that
11319                  case the shift needs to be converted into logical shift.  */
11320               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11321                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11322                 {
11323                   if ((mask & zerobits) == 0)
11324                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11325                   else
11326                     zerobits = 0;
11327                 }
11328             }
11329
11330           /* ((X << 16) & 0xff00) is (X, 0).  */
11331           if ((mask & zerobits) == mask)
11332             return omit_one_operand (type, build_int_cst (type, 0), arg0);
11333
11334           newmask = mask | zerobits;
11335           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11336             {
11337               unsigned int prec;
11338
11339               /* Only do the transformation if NEWMASK is some integer
11340                  mode's mask.  */
11341               for (prec = BITS_PER_UNIT;
11342                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11343                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11344                   break;
11345               if (prec < HOST_BITS_PER_WIDE_INT
11346                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11347                 {
11348                   if (shift_type != TREE_TYPE (arg0))
11349                     {
11350                       tem = fold_build2 (TREE_CODE (arg0), shift_type,
11351                                          fold_convert (shift_type,
11352                                                        TREE_OPERAND (arg0, 0)),
11353                                          TREE_OPERAND (arg0, 1));
11354                       tem = fold_convert (type, tem);
11355                     }
11356                   else
11357                     tem = op0;
11358                   return fold_build2 (BIT_AND_EXPR, type, tem,
11359                                       build_int_cst_type (TREE_TYPE (op1),
11360                                                           newmask));
11361                 }
11362             }
11363         }
11364
11365       goto associate;
11366
11367     case RDIV_EXPR:
11368       /* Don't touch a floating-point divide by zero unless the mode
11369          of the constant can represent infinity.  */
11370       if (TREE_CODE (arg1) == REAL_CST
11371           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11372           && real_zerop (arg1))
11373         return NULL_TREE;
11374
11375       /* Optimize A / A to 1.0 if we don't care about
11376          NaNs or Infinities.  Skip the transformation
11377          for non-real operands.  */
11378       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11379           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11380           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11381           && operand_equal_p (arg0, arg1, 0))
11382         {
11383           tree r = build_real (TREE_TYPE (arg0), dconst1);
11384
11385           return omit_two_operands (type, r, arg0, arg1);
11386         }
11387
11388       /* The complex version of the above A / A optimization.  */
11389       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11390           && operand_equal_p (arg0, arg1, 0))
11391         {
11392           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11393           if (! HONOR_NANS (TYPE_MODE (elem_type))
11394               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11395             {
11396               tree r = build_real (elem_type, dconst1);
11397               /* omit_two_operands will call fold_convert for us.  */
11398               return omit_two_operands (type, r, arg0, arg1);
11399             }
11400         }
11401
11402       /* (-A) / (-B) -> A / B  */
11403       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11404         return fold_build2 (RDIV_EXPR, type,
11405                             TREE_OPERAND (arg0, 0),
11406                             negate_expr (arg1));
11407       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11408         return fold_build2 (RDIV_EXPR, type,
11409                             negate_expr (arg0),
11410                             TREE_OPERAND (arg1, 0));
11411
11412       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11413       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11414           && real_onep (arg1))
11415         return non_lvalue (fold_convert (type, arg0));
11416
11417       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11418       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11419           && real_minus_onep (arg1))
11420         return non_lvalue (fold_convert (type, negate_expr (arg0)));
11421
11422       /* If ARG1 is a constant, we can convert this to a multiply by the
11423          reciprocal.  This does not have the same rounding properties,
11424          so only do this if -freciprocal-math.  We can actually
11425          always safely do it if ARG1 is a power of two, but it's hard to
11426          tell if it is or not in a portable manner.  */
11427       if (TREE_CODE (arg1) == REAL_CST)
11428         {
11429           if (flag_reciprocal_math
11430               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11431                                           arg1, 0)))
11432             return fold_build2 (MULT_EXPR, type, arg0, tem);
11433           /* Find the reciprocal if optimizing and the result is exact.  */
11434           if (optimize)
11435             {
11436               REAL_VALUE_TYPE r;
11437               r = TREE_REAL_CST (arg1);
11438               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11439                 {
11440                   tem = build_real (type, r);
11441                   return fold_build2 (MULT_EXPR, type,
11442                                       fold_convert (type, arg0), tem);
11443                 }
11444             }
11445         }
11446       /* Convert A/B/C to A/(B*C).  */ 
11447       if (flag_reciprocal_math
11448           && TREE_CODE (arg0) == RDIV_EXPR)
11449         return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11450                             fold_build2 (MULT_EXPR, type,
11451                                          TREE_OPERAND (arg0, 1), arg1));
11452
11453       /* Convert A/(B/C) to (A/B)*C.  */
11454       if (flag_reciprocal_math
11455           && TREE_CODE (arg1) == RDIV_EXPR)
11456         return fold_build2 (MULT_EXPR, type,
11457                             fold_build2 (RDIV_EXPR, type, arg0,
11458                                          TREE_OPERAND (arg1, 0)),
11459                             TREE_OPERAND (arg1, 1));
11460
11461       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11462       if (flag_reciprocal_math
11463           && TREE_CODE (arg1) == MULT_EXPR
11464           && TREE_CODE (arg0) == REAL_CST
11465           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11466         {
11467           tree tem = const_binop (RDIV_EXPR, arg0,
11468                                   TREE_OPERAND (arg1, 1), 0);
11469           if (tem)
11470             return fold_build2 (RDIV_EXPR, type, tem,
11471                                 TREE_OPERAND (arg1, 0));
11472         }
11473
11474       if (flag_unsafe_math_optimizations)
11475         {
11476           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11477           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11478
11479           /* Optimize sin(x)/cos(x) as tan(x).  */
11480           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11481                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11482                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11483               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11484                                   CALL_EXPR_ARG (arg1, 0), 0))
11485             {
11486               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11487
11488               if (tanfn != NULL_TREE)
11489                 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11490             }
11491
11492           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11493           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11494                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11495                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11496               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11497                                   CALL_EXPR_ARG (arg1, 0), 0))
11498             {
11499               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11500
11501               if (tanfn != NULL_TREE)
11502                 {
11503                   tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11504                   return fold_build2 (RDIV_EXPR, type,
11505                                       build_real (type, dconst1), tmp);
11506                 }
11507             }
11508
11509           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11510              NaNs or Infinities.  */
11511           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11512                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11513                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11514             {
11515               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11516               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11517
11518               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11519                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11520                   && operand_equal_p (arg00, arg01, 0))
11521                 {
11522                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11523
11524                   if (cosfn != NULL_TREE)
11525                     return build_call_expr (cosfn, 1, arg00);
11526                 }
11527             }
11528
11529           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11530              NaNs or Infinities.  */
11531           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11532                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11533                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11534             {
11535               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11536               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11537
11538               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11539                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11540                   && operand_equal_p (arg00, arg01, 0))
11541                 {
11542                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11543
11544                   if (cosfn != NULL_TREE)
11545                     {
11546                       tree tmp = build_call_expr (cosfn, 1, arg00);
11547                       return fold_build2 (RDIV_EXPR, type,
11548                                           build_real (type, dconst1),
11549                                           tmp);
11550                     }
11551                 }
11552             }
11553
11554           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11555           if (fcode0 == BUILT_IN_POW
11556               || fcode0 == BUILT_IN_POWF
11557               || fcode0 == BUILT_IN_POWL)
11558             {
11559               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11560               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11561               if (TREE_CODE (arg01) == REAL_CST
11562                   && !TREE_OVERFLOW (arg01)
11563                   && operand_equal_p (arg1, arg00, 0))
11564                 {
11565                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11566                   REAL_VALUE_TYPE c;
11567                   tree arg;
11568
11569                   c = TREE_REAL_CST (arg01);
11570                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11571                   arg = build_real (type, c);
11572                   return build_call_expr (powfn, 2, arg1, arg);
11573                 }
11574             }
11575
11576           /* Optimize a/root(b/c) into a*root(c/b).  */
11577           if (BUILTIN_ROOT_P (fcode1))
11578             {
11579               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11580
11581               if (TREE_CODE (rootarg) == RDIV_EXPR)
11582                 {
11583                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11584                   tree b = TREE_OPERAND (rootarg, 0);
11585                   tree c = TREE_OPERAND (rootarg, 1);
11586
11587                   tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11588
11589                   tmp = build_call_expr (rootfn, 1, tmp);
11590                   return fold_build2 (MULT_EXPR, type, arg0, tmp);
11591                 }
11592             }
11593
11594           /* Optimize x/expN(y) into x*expN(-y).  */
11595           if (BUILTIN_EXPONENT_P (fcode1))
11596             {
11597               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11598               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11599               arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11600               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11601             }
11602
11603           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11604           if (fcode1 == BUILT_IN_POW
11605               || fcode1 == BUILT_IN_POWF
11606               || fcode1 == BUILT_IN_POWL)
11607             {
11608               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11609               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11610               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11611               tree neg11 = fold_convert (type, negate_expr (arg11));
11612               arg1 = build_call_expr (powfn, 2, arg10, neg11);
11613               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11614             }
11615         }
11616       return NULL_TREE;
11617
11618     case TRUNC_DIV_EXPR:
11619     case FLOOR_DIV_EXPR:
11620       /* Simplify A / (B << N) where A and B are positive and B is
11621          a power of 2, to A >> (N + log2(B)).  */
11622       strict_overflow_p = false;
11623       if (TREE_CODE (arg1) == LSHIFT_EXPR
11624           && (TYPE_UNSIGNED (type)
11625               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11626         {
11627           tree sval = TREE_OPERAND (arg1, 0);
11628           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11629             {
11630               tree sh_cnt = TREE_OPERAND (arg1, 1);
11631               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11632
11633               if (strict_overflow_p)
11634                 fold_overflow_warning (("assuming signed overflow does not "
11635                                         "occur when simplifying A / (B << N)"),
11636                                        WARN_STRICT_OVERFLOW_MISC);
11637
11638               sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11639                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
11640               return fold_build2 (RSHIFT_EXPR, type,
11641                                   fold_convert (type, arg0), sh_cnt);
11642             }
11643         }
11644
11645       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11646          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11647       if (INTEGRAL_TYPE_P (type)
11648           && TYPE_UNSIGNED (type)
11649           && code == FLOOR_DIV_EXPR)
11650         return fold_build2 (TRUNC_DIV_EXPR, type, op0, op1);
11651
11652       /* Fall thru */
11653
11654     case ROUND_DIV_EXPR:
11655     case CEIL_DIV_EXPR:
11656     case EXACT_DIV_EXPR:
11657       if (integer_onep (arg1))
11658         return non_lvalue (fold_convert (type, arg0));
11659       if (integer_zerop (arg1))
11660         return NULL_TREE;
11661       /* X / -1 is -X.  */
11662       if (!TYPE_UNSIGNED (type)
11663           && TREE_CODE (arg1) == INTEGER_CST
11664           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11665           && TREE_INT_CST_HIGH (arg1) == -1)
11666         return fold_convert (type, negate_expr (arg0));
11667
11668       /* Convert -A / -B to A / B when the type is signed and overflow is
11669          undefined.  */
11670       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11671           && TREE_CODE (arg0) == NEGATE_EXPR
11672           && negate_expr_p (arg1))
11673         {
11674           if (INTEGRAL_TYPE_P (type))
11675             fold_overflow_warning (("assuming signed overflow does not occur "
11676                                     "when distributing negation across "
11677                                     "division"),
11678                                    WARN_STRICT_OVERFLOW_MISC);
11679           return fold_build2 (code, type,
11680                               fold_convert (type, TREE_OPERAND (arg0, 0)),
11681                               negate_expr (arg1));
11682         }
11683       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11684           && TREE_CODE (arg1) == NEGATE_EXPR
11685           && negate_expr_p (arg0))
11686         {
11687           if (INTEGRAL_TYPE_P (type))
11688             fold_overflow_warning (("assuming signed overflow does not occur "
11689                                     "when distributing negation across "
11690                                     "division"),
11691                                    WARN_STRICT_OVERFLOW_MISC);
11692           return fold_build2 (code, type, negate_expr (arg0),
11693                               TREE_OPERAND (arg1, 0));
11694         }
11695
11696       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11697          operation, EXACT_DIV_EXPR.
11698
11699          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11700          At one time others generated faster code, it's not clear if they do
11701          after the last round to changes to the DIV code in expmed.c.  */
11702       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11703           && multiple_of_p (type, arg0, arg1))
11704         return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11705
11706       strict_overflow_p = false;
11707       if (TREE_CODE (arg1) == INTEGER_CST
11708           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11709                                          &strict_overflow_p)))
11710         {
11711           if (strict_overflow_p)
11712             fold_overflow_warning (("assuming signed overflow does not occur "
11713                                     "when simplifying division"),
11714                                    WARN_STRICT_OVERFLOW_MISC);
11715           return fold_convert (type, tem);
11716         }
11717
11718       return NULL_TREE;
11719
11720     case CEIL_MOD_EXPR:
11721     case FLOOR_MOD_EXPR:
11722     case ROUND_MOD_EXPR:
11723     case TRUNC_MOD_EXPR:
11724       /* X % 1 is always zero, but be sure to preserve any side
11725          effects in X.  */
11726       if (integer_onep (arg1))
11727         return omit_one_operand (type, integer_zero_node, arg0);
11728
11729       /* X % 0, return X % 0 unchanged so that we can get the
11730          proper warnings and errors.  */
11731       if (integer_zerop (arg1))
11732         return NULL_TREE;
11733
11734       /* 0 % X is always zero, but be sure to preserve any side
11735          effects in X.  Place this after checking for X == 0.  */
11736       if (integer_zerop (arg0))
11737         return omit_one_operand (type, integer_zero_node, arg1);
11738
11739       /* X % -1 is zero.  */
11740       if (!TYPE_UNSIGNED (type)
11741           && TREE_CODE (arg1) == INTEGER_CST
11742           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11743           && TREE_INT_CST_HIGH (arg1) == -1)
11744         return omit_one_operand (type, integer_zero_node, arg0);
11745
11746       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11747          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11748       strict_overflow_p = false;
11749       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11750           && (TYPE_UNSIGNED (type)
11751               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11752         {
11753           tree c = arg1;
11754           /* Also optimize A % (C << N)  where C is a power of 2,
11755              to A & ((C << N) - 1).  */
11756           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11757             c = TREE_OPERAND (arg1, 0);
11758
11759           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11760             {
11761               tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11762                                        build_int_cst (TREE_TYPE (arg1), 1));
11763               if (strict_overflow_p)
11764                 fold_overflow_warning (("assuming signed overflow does not "
11765                                         "occur when simplifying "
11766                                         "X % (power of two)"),
11767                                        WARN_STRICT_OVERFLOW_MISC);
11768               return fold_build2 (BIT_AND_EXPR, type,
11769                                   fold_convert (type, arg0),
11770                                   fold_convert (type, mask));
11771             }
11772         }
11773
11774       /* X % -C is the same as X % C.  */
11775       if (code == TRUNC_MOD_EXPR
11776           && !TYPE_UNSIGNED (type)
11777           && TREE_CODE (arg1) == INTEGER_CST
11778           && !TREE_OVERFLOW (arg1)
11779           && TREE_INT_CST_HIGH (arg1) < 0
11780           && !TYPE_OVERFLOW_TRAPS (type)
11781           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11782           && !sign_bit_p (arg1, arg1))
11783         return fold_build2 (code, type, fold_convert (type, arg0),
11784                             fold_convert (type, negate_expr (arg1)));
11785
11786       /* X % -Y is the same as X % Y.  */
11787       if (code == TRUNC_MOD_EXPR
11788           && !TYPE_UNSIGNED (type)
11789           && TREE_CODE (arg1) == NEGATE_EXPR
11790           && !TYPE_OVERFLOW_TRAPS (type))
11791         return fold_build2 (code, type, fold_convert (type, arg0),
11792                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11793
11794       if (TREE_CODE (arg1) == INTEGER_CST
11795           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11796                                          &strict_overflow_p)))
11797         {
11798           if (strict_overflow_p)
11799             fold_overflow_warning (("assuming signed overflow does not occur "
11800                                     "when simplifying modulus"),
11801                                    WARN_STRICT_OVERFLOW_MISC);
11802           return fold_convert (type, tem);
11803         }
11804
11805       return NULL_TREE;
11806
11807     case LROTATE_EXPR:
11808     case RROTATE_EXPR:
11809       if (integer_all_onesp (arg0))
11810         return omit_one_operand (type, arg0, arg1);
11811       goto shift;
11812
11813     case RSHIFT_EXPR:
11814       /* Optimize -1 >> x for arithmetic right shifts.  */
11815       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11816           && tree_expr_nonnegative_p (arg1))
11817         return omit_one_operand (type, arg0, arg1);
11818       /* ... fall through ...  */
11819
11820     case LSHIFT_EXPR:
11821     shift:
11822       if (integer_zerop (arg1))
11823         return non_lvalue (fold_convert (type, arg0));
11824       if (integer_zerop (arg0))
11825         return omit_one_operand (type, arg0, arg1);
11826
11827       /* Since negative shift count is not well-defined,
11828          don't try to compute it in the compiler.  */
11829       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11830         return NULL_TREE;
11831
11832       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11833       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11834           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11835           && host_integerp (TREE_OPERAND (arg0, 1), false)
11836           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11837         {
11838           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11839                                + TREE_INT_CST_LOW (arg1));
11840
11841           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11842              being well defined.  */
11843           if (low >= TYPE_PRECISION (type))
11844             {
11845               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11846                 low = low % TYPE_PRECISION (type);
11847               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11848                 return build_int_cst (type, 0);
11849               else
11850                 low = TYPE_PRECISION (type) - 1;
11851             }
11852
11853           return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11854                               build_int_cst (type, low));
11855         }
11856
11857       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11858          into x & ((unsigned)-1 >> c) for unsigned types.  */
11859       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11860            || (TYPE_UNSIGNED (type)
11861                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11862           && host_integerp (arg1, false)
11863           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11864           && host_integerp (TREE_OPERAND (arg0, 1), false)
11865           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11866         {
11867           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11868           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11869           tree lshift;
11870           tree arg00;
11871
11872           if (low0 == low1)
11873             {
11874               arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11875
11876               lshift = build_int_cst (type, -1);
11877               lshift = int_const_binop (code, lshift, arg1, 0);
11878
11879               return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11880             }
11881         }
11882
11883       /* Rewrite an LROTATE_EXPR by a constant into an
11884          RROTATE_EXPR by a new constant.  */
11885       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11886         {
11887           tree tem = build_int_cst (TREE_TYPE (arg1),
11888                                     TYPE_PRECISION (type));
11889           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11890           return fold_build2 (RROTATE_EXPR, type, op0, tem);
11891         }
11892
11893       /* If we have a rotate of a bit operation with the rotate count and
11894          the second operand of the bit operation both constant,
11895          permute the two operations.  */
11896       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11897           && (TREE_CODE (arg0) == BIT_AND_EXPR
11898               || TREE_CODE (arg0) == BIT_IOR_EXPR
11899               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11900           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11901         return fold_build2 (TREE_CODE (arg0), type,
11902                             fold_build2 (code, type,
11903                                          TREE_OPERAND (arg0, 0), arg1),
11904                             fold_build2 (code, type,
11905                                          TREE_OPERAND (arg0, 1), arg1));
11906
11907       /* Two consecutive rotates adding up to the precision of the
11908          type can be ignored.  */
11909       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11910           && TREE_CODE (arg0) == RROTATE_EXPR
11911           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11912           && TREE_INT_CST_HIGH (arg1) == 0
11913           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11914           && ((TREE_INT_CST_LOW (arg1)
11915                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11916               == (unsigned int) TYPE_PRECISION (type)))
11917         return TREE_OPERAND (arg0, 0);
11918
11919       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11920               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11921          if the latter can be further optimized.  */
11922       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11923           && TREE_CODE (arg0) == BIT_AND_EXPR
11924           && TREE_CODE (arg1) == INTEGER_CST
11925           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11926         {
11927           tree mask = fold_build2 (code, type,
11928                                    fold_convert (type, TREE_OPERAND (arg0, 1)),
11929                                    arg1);
11930           tree shift = fold_build2 (code, type,
11931                                     fold_convert (type, TREE_OPERAND (arg0, 0)),
11932                                     arg1);
11933           tem = fold_binary (BIT_AND_EXPR, type, shift, mask);
11934           if (tem)
11935             return tem;
11936         }
11937
11938       return NULL_TREE;
11939
11940     case MIN_EXPR:
11941       if (operand_equal_p (arg0, arg1, 0))
11942         return omit_one_operand (type, arg0, arg1);
11943       if (INTEGRAL_TYPE_P (type)
11944           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11945         return omit_one_operand (type, arg1, arg0);
11946       tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11947       if (tem)
11948         return tem;
11949       goto associate;
11950
11951     case MAX_EXPR:
11952       if (operand_equal_p (arg0, arg1, 0))
11953         return omit_one_operand (type, arg0, arg1);
11954       if (INTEGRAL_TYPE_P (type)
11955           && TYPE_MAX_VALUE (type)
11956           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11957         return omit_one_operand (type, arg1, arg0);
11958       tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11959       if (tem)
11960         return tem;
11961       goto associate;
11962
11963     case TRUTH_ANDIF_EXPR:
11964       /* Note that the operands of this must be ints
11965          and their values must be 0 or 1.
11966          ("true" is a fixed value perhaps depending on the language.)  */
11967       /* If first arg is constant zero, return it.  */
11968       if (integer_zerop (arg0))
11969         return fold_convert (type, arg0);
11970     case TRUTH_AND_EXPR:
11971       /* If either arg is constant true, drop it.  */
11972       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11973         return non_lvalue (fold_convert (type, arg1));
11974       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11975           /* Preserve sequence points.  */
11976           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11977         return non_lvalue (fold_convert (type, arg0));
11978       /* If second arg is constant zero, result is zero, but first arg
11979          must be evaluated.  */
11980       if (integer_zerop (arg1))
11981         return omit_one_operand (type, arg1, arg0);
11982       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11983          case will be handled here.  */
11984       if (integer_zerop (arg0))
11985         return omit_one_operand (type, arg0, arg1);
11986
11987       /* !X && X is always false.  */
11988       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11989           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11990         return omit_one_operand (type, integer_zero_node, arg1);
11991       /* X && !X is always false.  */
11992       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11993           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11994         return omit_one_operand (type, integer_zero_node, arg0);
11995
11996       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
11997          means A >= Y && A != MAX, but in this case we know that
11998          A < X <= MAX.  */
11999
12000       if (!TREE_SIDE_EFFECTS (arg0)
12001           && !TREE_SIDE_EFFECTS (arg1))
12002         {
12003           tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
12004           if (tem && !operand_equal_p (tem, arg0, 0))
12005             return fold_build2 (code, type, tem, arg1);
12006
12007           tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
12008           if (tem && !operand_equal_p (tem, arg1, 0))
12009             return fold_build2 (code, type, arg0, tem);
12010         }
12011
12012     truth_andor:
12013       /* We only do these simplifications if we are optimizing.  */
12014       if (!optimize)
12015         return NULL_TREE;
12016
12017       /* Check for things like (A || B) && (A || C).  We can convert this
12018          to A || (B && C).  Note that either operator can be any of the four
12019          truth and/or operations and the transformation will still be
12020          valid.   Also note that we only care about order for the
12021          ANDIF and ORIF operators.  If B contains side effects, this
12022          might change the truth-value of A.  */
12023       if (TREE_CODE (arg0) == TREE_CODE (arg1)
12024           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12025               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12026               || TREE_CODE (arg0) == TRUTH_AND_EXPR
12027               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12028           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12029         {
12030           tree a00 = TREE_OPERAND (arg0, 0);
12031           tree a01 = TREE_OPERAND (arg0, 1);
12032           tree a10 = TREE_OPERAND (arg1, 0);
12033           tree a11 = TREE_OPERAND (arg1, 1);
12034           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12035                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12036                              && (code == TRUTH_AND_EXPR
12037                                  || code == TRUTH_OR_EXPR));
12038
12039           if (operand_equal_p (a00, a10, 0))
12040             return fold_build2 (TREE_CODE (arg0), type, a00,
12041                                 fold_build2 (code, type, a01, a11));
12042           else if (commutative && operand_equal_p (a00, a11, 0))
12043             return fold_build2 (TREE_CODE (arg0), type, a00,
12044                                 fold_build2 (code, type, a01, a10));
12045           else if (commutative && operand_equal_p (a01, a10, 0))
12046             return fold_build2 (TREE_CODE (arg0), type, a01,
12047                                 fold_build2 (code, type, a00, a11));
12048
12049           /* This case if tricky because we must either have commutative
12050              operators or else A10 must not have side-effects.  */
12051
12052           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12053                    && operand_equal_p (a01, a11, 0))
12054             return fold_build2 (TREE_CODE (arg0), type,
12055                                 fold_build2 (code, type, a00, a10),
12056                                 a01);
12057         }
12058
12059       /* See if we can build a range comparison.  */
12060       if (0 != (tem = fold_range_test (code, type, op0, op1)))
12061         return tem;
12062
12063       /* Check for the possibility of merging component references.  If our
12064          lhs is another similar operation, try to merge its rhs with our
12065          rhs.  Then try to merge our lhs and rhs.  */
12066       if (TREE_CODE (arg0) == code
12067           && 0 != (tem = fold_truthop (code, type,
12068                                        TREE_OPERAND (arg0, 1), arg1)))
12069         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12070
12071       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
12072         return tem;
12073
12074       return NULL_TREE;
12075
12076     case TRUTH_ORIF_EXPR:
12077       /* Note that the operands of this must be ints
12078          and their values must be 0 or true.
12079          ("true" is a fixed value perhaps depending on the language.)  */
12080       /* If first arg is constant true, return it.  */
12081       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12082         return fold_convert (type, arg0);
12083     case TRUTH_OR_EXPR:
12084       /* If either arg is constant zero, drop it.  */
12085       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12086         return non_lvalue (fold_convert (type, arg1));
12087       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12088           /* Preserve sequence points.  */
12089           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12090         return non_lvalue (fold_convert (type, arg0));
12091       /* If second arg is constant true, result is true, but we must
12092          evaluate first arg.  */
12093       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12094         return omit_one_operand (type, arg1, arg0);
12095       /* Likewise for first arg, but note this only occurs here for
12096          TRUTH_OR_EXPR.  */
12097       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12098         return omit_one_operand (type, arg0, arg1);
12099
12100       /* !X || X is always true.  */
12101       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12102           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12103         return omit_one_operand (type, integer_one_node, arg1);
12104       /* X || !X is always true.  */
12105       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12106           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12107         return omit_one_operand (type, integer_one_node, arg0);
12108
12109       goto truth_andor;
12110
12111     case TRUTH_XOR_EXPR:
12112       /* If the second arg is constant zero, drop it.  */
12113       if (integer_zerop (arg1))
12114         return non_lvalue (fold_convert (type, arg0));
12115       /* If the second arg is constant true, this is a logical inversion.  */
12116       if (integer_onep (arg1))
12117         {
12118           /* Only call invert_truthvalue if operand is a truth value.  */
12119           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12120             tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12121           else
12122             tem = invert_truthvalue (arg0);
12123           return non_lvalue (fold_convert (type, tem));
12124         }
12125       /* Identical arguments cancel to zero.  */
12126       if (operand_equal_p (arg0, arg1, 0))
12127         return omit_one_operand (type, integer_zero_node, arg0);
12128
12129       /* !X ^ X is always true.  */
12130       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12131           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12132         return omit_one_operand (type, integer_one_node, arg1);
12133
12134       /* X ^ !X is always true.  */
12135       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12136           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12137         return omit_one_operand (type, integer_one_node, arg0);
12138
12139       return NULL_TREE;
12140
12141     case EQ_EXPR:
12142     case NE_EXPR:
12143       tem = fold_comparison (code, type, op0, op1);
12144       if (tem != NULL_TREE)
12145         return tem;
12146
12147       /* bool_var != 0 becomes bool_var. */
12148       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12149           && code == NE_EXPR)
12150         return non_lvalue (fold_convert (type, arg0));
12151
12152       /* bool_var == 1 becomes bool_var. */
12153       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12154           && code == EQ_EXPR)
12155         return non_lvalue (fold_convert (type, arg0));
12156
12157       /* bool_var != 1 becomes !bool_var. */
12158       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12159           && code == NE_EXPR)
12160         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12161
12162       /* bool_var == 0 becomes !bool_var. */
12163       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12164           && code == EQ_EXPR)
12165         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12166
12167       /* If this is an equality comparison of the address of two non-weak,
12168          unaliased symbols neither of which are extern (since we do not
12169          have access to attributes for externs), then we know the result.  */
12170       if (TREE_CODE (arg0) == ADDR_EXPR
12171           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12172           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12173           && ! lookup_attribute ("alias",
12174                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12175           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12176           && TREE_CODE (arg1) == ADDR_EXPR
12177           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12178           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12179           && ! lookup_attribute ("alias",
12180                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12181           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12182         {
12183           /* We know that we're looking at the address of two
12184              non-weak, unaliased, static _DECL nodes.
12185
12186              It is both wasteful and incorrect to call operand_equal_p
12187              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12188              all we need to do is test pointer equality for the arguments
12189              to the two ADDR_EXPR nodes.  It is incorrect to use
12190              operand_equal_p as that function is NOT equivalent to a
12191              C equality test.  It can in fact return false for two
12192              objects which would test as equal using the C equality
12193              operator.  */
12194           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12195           return constant_boolean_node (equal
12196                                         ? code == EQ_EXPR : code != EQ_EXPR,
12197                                         type);
12198         }
12199
12200       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12201          a MINUS_EXPR of a constant, we can convert it into a comparison with
12202          a revised constant as long as no overflow occurs.  */
12203       if (TREE_CODE (arg1) == INTEGER_CST
12204           && (TREE_CODE (arg0) == PLUS_EXPR
12205               || TREE_CODE (arg0) == MINUS_EXPR)
12206           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12207           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12208                                       ? MINUS_EXPR : PLUS_EXPR,
12209                                       fold_convert (TREE_TYPE (arg0), arg1),
12210                                       TREE_OPERAND (arg0, 1), 0))
12211           && !TREE_OVERFLOW (tem))
12212         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12213
12214       /* Similarly for a NEGATE_EXPR.  */
12215       if (TREE_CODE (arg0) == NEGATE_EXPR
12216           && TREE_CODE (arg1) == INTEGER_CST
12217           && 0 != (tem = negate_expr (arg1))
12218           && TREE_CODE (tem) == INTEGER_CST
12219           && !TREE_OVERFLOW (tem))
12220         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12221
12222       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12223       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12224           && TREE_CODE (arg1) == INTEGER_CST
12225           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12226         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12227                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
12228                                          fold_convert (TREE_TYPE (arg0), arg1),
12229                                          TREE_OPERAND (arg0, 1)));
12230
12231       /* Transform comparisons of the form X +- C CMP X.  */
12232       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12233           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12234           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12235           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12236               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12237         {
12238           tree cst = TREE_OPERAND (arg0, 1);
12239
12240           if (code == EQ_EXPR
12241               && !integer_zerop (cst))
12242             return omit_two_operands (type, boolean_false_node,
12243                                       TREE_OPERAND (arg0, 0), arg1);
12244           else
12245             return omit_two_operands (type, boolean_true_node,
12246                                       TREE_OPERAND (arg0, 0), arg1);
12247         }
12248
12249       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12250          for !=.  Don't do this for ordered comparisons due to overflow.  */
12251       if (TREE_CODE (arg0) == MINUS_EXPR
12252           && integer_zerop (arg1))
12253         return fold_build2 (code, type,
12254                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12255
12256       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12257       if (TREE_CODE (arg0) == ABS_EXPR
12258           && (integer_zerop (arg1) || real_zerop (arg1)))
12259         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
12260
12261       /* If this is an EQ or NE comparison with zero and ARG0 is
12262          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12263          two operations, but the latter can be done in one less insn
12264          on machines that have only two-operand insns or on which a
12265          constant cannot be the first operand.  */
12266       if (TREE_CODE (arg0) == BIT_AND_EXPR
12267           && integer_zerop (arg1))
12268         {
12269           tree arg00 = TREE_OPERAND (arg0, 0);
12270           tree arg01 = TREE_OPERAND (arg0, 1);
12271           if (TREE_CODE (arg00) == LSHIFT_EXPR
12272               && integer_onep (TREE_OPERAND (arg00, 0)))
12273             {
12274               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
12275                                       arg01, TREE_OPERAND (arg00, 1));
12276               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12277                                  build_int_cst (TREE_TYPE (arg0), 1));
12278               return fold_build2 (code, type,
12279                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
12280             }
12281           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12282                    && integer_onep (TREE_OPERAND (arg01, 0)))
12283             {
12284               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
12285                                       arg00, TREE_OPERAND (arg01, 1));
12286               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12287                                  build_int_cst (TREE_TYPE (arg0), 1));
12288               return fold_build2 (code, type,
12289                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
12290             }
12291         }
12292
12293       /* If this is an NE or EQ comparison of zero against the result of a
12294          signed MOD operation whose second operand is a power of 2, make
12295          the MOD operation unsigned since it is simpler and equivalent.  */
12296       if (integer_zerop (arg1)
12297           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12298           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12299               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12300               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12301               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12302           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12303         {
12304           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12305           tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
12306                                      fold_convert (newtype,
12307                                                    TREE_OPERAND (arg0, 0)),
12308                                      fold_convert (newtype,
12309                                                    TREE_OPERAND (arg0, 1)));
12310
12311           return fold_build2 (code, type, newmod,
12312                               fold_convert (newtype, arg1));
12313         }
12314
12315       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12316          C1 is a valid shift constant, and C2 is a power of two, i.e.
12317          a single bit.  */
12318       if (TREE_CODE (arg0) == BIT_AND_EXPR
12319           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12320           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12321              == INTEGER_CST
12322           && integer_pow2p (TREE_OPERAND (arg0, 1))
12323           && integer_zerop (arg1))
12324         {
12325           tree itype = TREE_TYPE (arg0);
12326           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12327           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12328
12329           /* Check for a valid shift count.  */
12330           if (TREE_INT_CST_HIGH (arg001) == 0
12331               && TREE_INT_CST_LOW (arg001) < prec)
12332             {
12333               tree arg01 = TREE_OPERAND (arg0, 1);
12334               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12335               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12336               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12337                  can be rewritten as (X & (C2 << C1)) != 0.  */
12338               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12339                 {
12340                   tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
12341                   tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
12342                   return fold_build2 (code, type, tem, arg1);
12343                 }
12344               /* Otherwise, for signed (arithmetic) shifts,
12345                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12346                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12347               else if (!TYPE_UNSIGNED (itype))
12348                 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12349                                     arg000, build_int_cst (itype, 0));
12350               /* Otherwise, of unsigned (logical) shifts,
12351                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12352                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12353               else
12354                 return omit_one_operand (type,
12355                                          code == EQ_EXPR ? integer_one_node
12356                                                          : integer_zero_node,
12357                                          arg000);
12358             }
12359         }
12360
12361       /* If this is an NE comparison of zero with an AND of one, remove the
12362          comparison since the AND will give the correct value.  */
12363       if (code == NE_EXPR
12364           && integer_zerop (arg1)
12365           && TREE_CODE (arg0) == BIT_AND_EXPR
12366           && integer_onep (TREE_OPERAND (arg0, 1)))
12367         return fold_convert (type, arg0);
12368
12369       /* If we have (A & C) == C where C is a power of 2, convert this into
12370          (A & C) != 0.  Similarly for NE_EXPR.  */
12371       if (TREE_CODE (arg0) == BIT_AND_EXPR
12372           && integer_pow2p (TREE_OPERAND (arg0, 1))
12373           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12374         return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12375                             arg0, fold_convert (TREE_TYPE (arg0),
12376                                                 integer_zero_node));
12377
12378       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12379          bit, then fold the expression into A < 0 or A >= 0.  */
12380       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
12381       if (tem)
12382         return tem;
12383
12384       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12385          Similarly for NE_EXPR.  */
12386       if (TREE_CODE (arg0) == BIT_AND_EXPR
12387           && TREE_CODE (arg1) == INTEGER_CST
12388           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12389         {
12390           tree notc = fold_build1 (BIT_NOT_EXPR,
12391                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12392                                    TREE_OPERAND (arg0, 1));
12393           tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12394                                        arg1, notc);
12395           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12396           if (integer_nonzerop (dandnotc))
12397             return omit_one_operand (type, rslt, arg0);
12398         }
12399
12400       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12401          Similarly for NE_EXPR.  */
12402       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12403           && TREE_CODE (arg1) == INTEGER_CST
12404           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12405         {
12406           tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12407           tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12408                                        TREE_OPERAND (arg0, 1), notd);
12409           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12410           if (integer_nonzerop (candnotd))
12411             return omit_one_operand (type, rslt, arg0);
12412         }
12413
12414       /* If this is a comparison of a field, we may be able to simplify it.  */
12415       if ((TREE_CODE (arg0) == COMPONENT_REF
12416            || TREE_CODE (arg0) == BIT_FIELD_REF)
12417           /* Handle the constant case even without -O
12418              to make sure the warnings are given.  */
12419           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12420         {
12421           t1 = optimize_bit_field_compare (code, type, arg0, arg1);
12422           if (t1)
12423             return t1;
12424         }
12425
12426       /* Optimize comparisons of strlen vs zero to a compare of the
12427          first character of the string vs zero.  To wit,
12428                 strlen(ptr) == 0   =>  *ptr == 0
12429                 strlen(ptr) != 0   =>  *ptr != 0
12430          Other cases should reduce to one of these two (or a constant)
12431          due to the return value of strlen being unsigned.  */
12432       if (TREE_CODE (arg0) == CALL_EXPR
12433           && integer_zerop (arg1))
12434         {
12435           tree fndecl = get_callee_fndecl (arg0);
12436
12437           if (fndecl
12438               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12439               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12440               && call_expr_nargs (arg0) == 1
12441               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12442             {
12443               tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
12444               return fold_build2 (code, type, iref,
12445                                   build_int_cst (TREE_TYPE (iref), 0));
12446             }
12447         }
12448
12449       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12450          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12451       if (TREE_CODE (arg0) == RSHIFT_EXPR
12452           && integer_zerop (arg1)
12453           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12454         {
12455           tree arg00 = TREE_OPERAND (arg0, 0);
12456           tree arg01 = TREE_OPERAND (arg0, 1);
12457           tree itype = TREE_TYPE (arg00);
12458           if (TREE_INT_CST_HIGH (arg01) == 0
12459               && TREE_INT_CST_LOW (arg01)
12460                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12461             {
12462               if (TYPE_UNSIGNED (itype))
12463                 {
12464                   itype = signed_type_for (itype);
12465                   arg00 = fold_convert (itype, arg00);
12466                 }
12467               return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12468                                   type, arg00, build_int_cst (itype, 0));
12469             }
12470         }
12471
12472       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12473       if (integer_zerop (arg1)
12474           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12475         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12476                             TREE_OPERAND (arg0, 1));
12477
12478       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12479       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12480           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12481         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12482                             build_int_cst (TREE_TYPE (arg1), 0));
12483       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12484       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12485           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12486           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12487         return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
12488                             build_int_cst (TREE_TYPE (arg1), 0));
12489
12490       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12491       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12492           && TREE_CODE (arg1) == INTEGER_CST
12493           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12494         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12495                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
12496                                          TREE_OPERAND (arg0, 1), arg1));
12497
12498       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12499          (X & C) == 0 when C is a single bit.  */
12500       if (TREE_CODE (arg0) == BIT_AND_EXPR
12501           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12502           && integer_zerop (arg1)
12503           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12504         {
12505           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12506                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12507                              TREE_OPERAND (arg0, 1));
12508           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12509                               type, tem, arg1);
12510         }
12511
12512       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12513          constant C is a power of two, i.e. a single bit.  */
12514       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12515           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12516           && integer_zerop (arg1)
12517           && integer_pow2p (TREE_OPERAND (arg0, 1))
12518           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12519                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12520         {
12521           tree arg00 = TREE_OPERAND (arg0, 0);
12522           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12523                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12524         }
12525
12526       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12527          when is C is a power of two, i.e. a single bit.  */
12528       if (TREE_CODE (arg0) == BIT_AND_EXPR
12529           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12530           && integer_zerop (arg1)
12531           && integer_pow2p (TREE_OPERAND (arg0, 1))
12532           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12533                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12534         {
12535           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12536           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12537                              arg000, TREE_OPERAND (arg0, 1));
12538           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12539                               tem, build_int_cst (TREE_TYPE (tem), 0));
12540         }
12541
12542       if (integer_zerop (arg1)
12543           && tree_expr_nonzero_p (arg0))
12544         {
12545           tree res = constant_boolean_node (code==NE_EXPR, type);
12546           return omit_one_operand (type, res, arg0);
12547         }
12548
12549       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12550       if (TREE_CODE (arg0) == NEGATE_EXPR
12551           && TREE_CODE (arg1) == NEGATE_EXPR)
12552         return fold_build2 (code, type,
12553                             TREE_OPERAND (arg0, 0),
12554                             TREE_OPERAND (arg1, 0));
12555
12556       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12557       if (TREE_CODE (arg0) == BIT_AND_EXPR
12558           && TREE_CODE (arg1) == BIT_AND_EXPR)
12559         {
12560           tree arg00 = TREE_OPERAND (arg0, 0);
12561           tree arg01 = TREE_OPERAND (arg0, 1);
12562           tree arg10 = TREE_OPERAND (arg1, 0);
12563           tree arg11 = TREE_OPERAND (arg1, 1);
12564           tree itype = TREE_TYPE (arg0);
12565
12566           if (operand_equal_p (arg01, arg11, 0))
12567             return fold_build2 (code, type,
12568                                 fold_build2 (BIT_AND_EXPR, itype,
12569                                              fold_build2 (BIT_XOR_EXPR, itype,
12570                                                           arg00, arg10),
12571                                              arg01),
12572                                 build_int_cst (itype, 0));
12573
12574           if (operand_equal_p (arg01, arg10, 0))
12575             return fold_build2 (code, type,
12576                                 fold_build2 (BIT_AND_EXPR, itype,
12577                                              fold_build2 (BIT_XOR_EXPR, itype,
12578                                                           arg00, arg11),
12579                                              arg01),
12580                                 build_int_cst (itype, 0));
12581
12582           if (operand_equal_p (arg00, arg11, 0))
12583             return fold_build2 (code, type,
12584                                 fold_build2 (BIT_AND_EXPR, itype,
12585                                              fold_build2 (BIT_XOR_EXPR, itype,
12586                                                           arg01, arg10),
12587                                              arg00),
12588                                 build_int_cst (itype, 0));
12589
12590           if (operand_equal_p (arg00, arg10, 0))
12591             return fold_build2 (code, type,
12592                                 fold_build2 (BIT_AND_EXPR, itype,
12593                                              fold_build2 (BIT_XOR_EXPR, itype,
12594                                                           arg01, arg11),
12595                                              arg00),
12596                                 build_int_cst (itype, 0));
12597         }
12598
12599       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12600           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12601         {
12602           tree arg00 = TREE_OPERAND (arg0, 0);
12603           tree arg01 = TREE_OPERAND (arg0, 1);
12604           tree arg10 = TREE_OPERAND (arg1, 0);
12605           tree arg11 = TREE_OPERAND (arg1, 1);
12606           tree itype = TREE_TYPE (arg0);
12607
12608           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12609              operand_equal_p guarantees no side-effects so we don't need
12610              to use omit_one_operand on Z.  */
12611           if (operand_equal_p (arg01, arg11, 0))
12612             return fold_build2 (code, type, arg00, arg10);
12613           if (operand_equal_p (arg01, arg10, 0))
12614             return fold_build2 (code, type, arg00, arg11);
12615           if (operand_equal_p (arg00, arg11, 0))
12616             return fold_build2 (code, type, arg01, arg10);
12617           if (operand_equal_p (arg00, arg10, 0))
12618             return fold_build2 (code, type, arg01, arg11);
12619
12620           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12621           if (TREE_CODE (arg01) == INTEGER_CST
12622               && TREE_CODE (arg11) == INTEGER_CST)
12623             return fold_build2 (code, type,
12624                                 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12625                                              fold_build2 (BIT_XOR_EXPR, itype,
12626                                                           arg01, arg11)),
12627                                 arg10);
12628         }
12629
12630       /* Attempt to simplify equality/inequality comparisons of complex
12631          values.  Only lower the comparison if the result is known or
12632          can be simplified to a single scalar comparison.  */
12633       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12634            || TREE_CODE (arg0) == COMPLEX_CST)
12635           && (TREE_CODE (arg1) == COMPLEX_EXPR
12636               || TREE_CODE (arg1) == COMPLEX_CST))
12637         {
12638           tree real0, imag0, real1, imag1;
12639           tree rcond, icond;
12640
12641           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12642             {
12643               real0 = TREE_OPERAND (arg0, 0);
12644               imag0 = TREE_OPERAND (arg0, 1);
12645             }
12646           else
12647             {
12648               real0 = TREE_REALPART (arg0);
12649               imag0 = TREE_IMAGPART (arg0);
12650             }
12651
12652           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12653             {
12654               real1 = TREE_OPERAND (arg1, 0);
12655               imag1 = TREE_OPERAND (arg1, 1);
12656             }
12657           else
12658             {
12659               real1 = TREE_REALPART (arg1);
12660               imag1 = TREE_IMAGPART (arg1);
12661             }
12662
12663           rcond = fold_binary (code, type, real0, real1);
12664           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12665             {
12666               if (integer_zerop (rcond))
12667                 {
12668                   if (code == EQ_EXPR)
12669                     return omit_two_operands (type, boolean_false_node,
12670                                               imag0, imag1);
12671                   return fold_build2 (NE_EXPR, type, imag0, imag1);
12672                 }
12673               else
12674                 {
12675                   if (code == NE_EXPR)
12676                     return omit_two_operands (type, boolean_true_node,
12677                                               imag0, imag1);
12678                   return fold_build2 (EQ_EXPR, type, imag0, imag1);
12679                 }
12680             }
12681
12682           icond = fold_binary (code, type, imag0, imag1);
12683           if (icond && TREE_CODE (icond) == INTEGER_CST)
12684             {
12685               if (integer_zerop (icond))
12686                 {
12687                   if (code == EQ_EXPR)
12688                     return omit_two_operands (type, boolean_false_node,
12689                                               real0, real1);
12690                   return fold_build2 (NE_EXPR, type, real0, real1);
12691                 }
12692               else
12693                 {
12694                   if (code == NE_EXPR)
12695                     return omit_two_operands (type, boolean_true_node,
12696                                               real0, real1);
12697                   return fold_build2 (EQ_EXPR, type, real0, real1);
12698                 }
12699             }
12700         }
12701
12702       return NULL_TREE;
12703
12704     case LT_EXPR:
12705     case GT_EXPR:
12706     case LE_EXPR:
12707     case GE_EXPR:
12708       tem = fold_comparison (code, type, op0, op1);
12709       if (tem != NULL_TREE)
12710         return tem;
12711
12712       /* Transform comparisons of the form X +- C CMP X.  */
12713       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12714           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12715           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12716                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12717               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12718                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12719         {
12720           tree arg01 = TREE_OPERAND (arg0, 1);
12721           enum tree_code code0 = TREE_CODE (arg0);
12722           int is_positive;
12723
12724           if (TREE_CODE (arg01) == REAL_CST)
12725             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12726           else
12727             is_positive = tree_int_cst_sgn (arg01);
12728
12729           /* (X - c) > X becomes false.  */
12730           if (code == GT_EXPR
12731               && ((code0 == MINUS_EXPR && is_positive >= 0)
12732                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12733             {
12734               if (TREE_CODE (arg01) == INTEGER_CST
12735                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12736                 fold_overflow_warning (("assuming signed overflow does not "
12737                                         "occur when assuming that (X - c) > X "
12738                                         "is always false"),
12739                                        WARN_STRICT_OVERFLOW_ALL);
12740               return constant_boolean_node (0, type);
12741             }
12742
12743           /* Likewise (X + c) < X becomes false.  */
12744           if (code == LT_EXPR
12745               && ((code0 == PLUS_EXPR && is_positive >= 0)
12746                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12747             {
12748               if (TREE_CODE (arg01) == INTEGER_CST
12749                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12750                 fold_overflow_warning (("assuming signed overflow does not "
12751                                         "occur when assuming that "
12752                                         "(X + c) < X is always false"),
12753                                        WARN_STRICT_OVERFLOW_ALL);
12754               return constant_boolean_node (0, type);
12755             }
12756
12757           /* Convert (X - c) <= X to true.  */
12758           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12759               && code == LE_EXPR
12760               && ((code0 == MINUS_EXPR && is_positive >= 0)
12761                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12762             {
12763               if (TREE_CODE (arg01) == INTEGER_CST
12764                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12765                 fold_overflow_warning (("assuming signed overflow does not "
12766                                         "occur when assuming that "
12767                                         "(X - c) <= X is always true"),
12768                                        WARN_STRICT_OVERFLOW_ALL);
12769               return constant_boolean_node (1, type);
12770             }
12771
12772           /* Convert (X + c) >= X to true.  */
12773           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12774               && code == GE_EXPR
12775               && ((code0 == PLUS_EXPR && is_positive >= 0)
12776                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12777             {
12778               if (TREE_CODE (arg01) == INTEGER_CST
12779                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12780                 fold_overflow_warning (("assuming signed overflow does not "
12781                                         "occur when assuming that "
12782                                         "(X + c) >= X is always true"),
12783                                        WARN_STRICT_OVERFLOW_ALL);
12784               return constant_boolean_node (1, type);
12785             }
12786
12787           if (TREE_CODE (arg01) == INTEGER_CST)
12788             {
12789               /* Convert X + c > X and X - c < X to true for integers.  */
12790               if (code == GT_EXPR
12791                   && ((code0 == PLUS_EXPR && is_positive > 0)
12792                       || (code0 == MINUS_EXPR && is_positive < 0)))
12793                 {
12794                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12795                     fold_overflow_warning (("assuming signed overflow does "
12796                                             "not occur when assuming that "
12797                                             "(X + c) > X is always true"),
12798                                            WARN_STRICT_OVERFLOW_ALL);
12799                   return constant_boolean_node (1, type);
12800                 }
12801
12802               if (code == LT_EXPR
12803                   && ((code0 == MINUS_EXPR && is_positive > 0)
12804                       || (code0 == PLUS_EXPR && is_positive < 0)))
12805                 {
12806                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12807                     fold_overflow_warning (("assuming signed overflow does "
12808                                             "not occur when assuming that "
12809                                             "(X - c) < X is always true"),
12810                                            WARN_STRICT_OVERFLOW_ALL);
12811                   return constant_boolean_node (1, type);
12812                 }
12813
12814               /* Convert X + c <= X and X - c >= X to false for integers.  */
12815               if (code == LE_EXPR
12816                   && ((code0 == PLUS_EXPR && is_positive > 0)
12817                       || (code0 == MINUS_EXPR && is_positive < 0)))
12818                 {
12819                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12820                     fold_overflow_warning (("assuming signed overflow does "
12821                                             "not occur when assuming that "
12822                                             "(X + c) <= X is always false"),
12823                                            WARN_STRICT_OVERFLOW_ALL);
12824                   return constant_boolean_node (0, type);
12825                 }
12826
12827               if (code == GE_EXPR
12828                   && ((code0 == MINUS_EXPR && is_positive > 0)
12829                       || (code0 == PLUS_EXPR && is_positive < 0)))
12830                 {
12831                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12832                     fold_overflow_warning (("assuming signed overflow does "
12833                                             "not occur when assuming that "
12834                                             "(X - c) >= X is always false"),
12835                                            WARN_STRICT_OVERFLOW_ALL);
12836                   return constant_boolean_node (0, type);
12837                 }
12838             }
12839         }
12840
12841       /* Comparisons with the highest or lowest possible integer of
12842          the specified precision will have known values.  */
12843       {
12844         tree arg1_type = TREE_TYPE (arg1);
12845         unsigned int width = TYPE_PRECISION (arg1_type);
12846
12847         if (TREE_CODE (arg1) == INTEGER_CST
12848             && width <= 2 * HOST_BITS_PER_WIDE_INT
12849             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12850           {
12851             HOST_WIDE_INT signed_max_hi;
12852             unsigned HOST_WIDE_INT signed_max_lo;
12853             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12854
12855             if (width <= HOST_BITS_PER_WIDE_INT)
12856               {
12857                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12858                                 - 1;
12859                 signed_max_hi = 0;
12860                 max_hi = 0;
12861
12862                 if (TYPE_UNSIGNED (arg1_type))
12863                   {
12864                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12865                     min_lo = 0;
12866                     min_hi = 0;
12867                   }
12868                 else
12869                   {
12870                     max_lo = signed_max_lo;
12871                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12872                     min_hi = -1;
12873                   }
12874               }
12875             else
12876               {
12877                 width -= HOST_BITS_PER_WIDE_INT;
12878                 signed_max_lo = -1;
12879                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12880                                 - 1;
12881                 max_lo = -1;
12882                 min_lo = 0;
12883
12884                 if (TYPE_UNSIGNED (arg1_type))
12885                   {
12886                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12887                     min_hi = 0;
12888                   }
12889                 else
12890                   {
12891                     max_hi = signed_max_hi;
12892                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12893                   }
12894               }
12895
12896             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12897                 && TREE_INT_CST_LOW (arg1) == max_lo)
12898               switch (code)
12899                 {
12900                 case GT_EXPR:
12901                   return omit_one_operand (type, integer_zero_node, arg0);
12902
12903                 case GE_EXPR:
12904                   return fold_build2 (EQ_EXPR, type, op0, op1);
12905
12906                 case LE_EXPR:
12907                   return omit_one_operand (type, integer_one_node, arg0);
12908
12909                 case LT_EXPR:
12910                   return fold_build2 (NE_EXPR, type, op0, op1);
12911
12912                 /* The GE_EXPR and LT_EXPR cases above are not normally
12913                    reached because of previous transformations.  */
12914
12915                 default:
12916                   break;
12917                 }
12918             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12919                      == max_hi
12920                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12921               switch (code)
12922                 {
12923                 case GT_EXPR:
12924                   arg1 = const_binop (PLUS_EXPR, arg1,
12925                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12926                   return fold_build2 (EQ_EXPR, type,
12927                                       fold_convert (TREE_TYPE (arg1), arg0),
12928                                       arg1);
12929                 case LE_EXPR:
12930                   arg1 = const_binop (PLUS_EXPR, arg1,
12931                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12932                   return fold_build2 (NE_EXPR, type,
12933                                       fold_convert (TREE_TYPE (arg1), arg0),
12934                                       arg1);
12935                 default:
12936                   break;
12937                 }
12938             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12939                      == min_hi
12940                      && TREE_INT_CST_LOW (arg1) == min_lo)
12941               switch (code)
12942                 {
12943                 case LT_EXPR:
12944                   return omit_one_operand (type, integer_zero_node, arg0);
12945
12946                 case LE_EXPR:
12947                   return fold_build2 (EQ_EXPR, type, op0, op1);
12948
12949                 case GE_EXPR:
12950                   return omit_one_operand (type, integer_one_node, arg0);
12951
12952                 case GT_EXPR:
12953                   return fold_build2 (NE_EXPR, type, op0, op1);
12954
12955                 default:
12956                   break;
12957                 }
12958             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12959                      == min_hi
12960                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12961               switch (code)
12962                 {
12963                 case GE_EXPR:
12964                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12965                   return fold_build2 (NE_EXPR, type,
12966                                       fold_convert (TREE_TYPE (arg1), arg0),
12967                                       arg1);
12968                 case LT_EXPR:
12969                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12970                   return fold_build2 (EQ_EXPR, type,
12971                                       fold_convert (TREE_TYPE (arg1), arg0),
12972                                       arg1);
12973                 default:
12974                   break;
12975                 }
12976
12977             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12978                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
12979                      && TYPE_UNSIGNED (arg1_type)
12980                      /* We will flip the signedness of the comparison operator
12981                         associated with the mode of arg1, so the sign bit is
12982                         specified by this mode.  Check that arg1 is the signed
12983                         max associated with this sign bit.  */
12984                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12985                      /* signed_type does not work on pointer types.  */
12986                      && INTEGRAL_TYPE_P (arg1_type))
12987               {
12988                 /* The following case also applies to X < signed_max+1
12989                    and X >= signed_max+1 because previous transformations.  */
12990                 if (code == LE_EXPR || code == GT_EXPR)
12991                   {
12992                     tree st;
12993                     st = signed_type_for (TREE_TYPE (arg1));
12994                     return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
12995                                         type, fold_convert (st, arg0),
12996                                         build_int_cst (st, 0));
12997                   }
12998               }
12999           }
13000       }
13001
13002       /* If we are comparing an ABS_EXPR with a constant, we can
13003          convert all the cases into explicit comparisons, but they may
13004          well not be faster than doing the ABS and one comparison.
13005          But ABS (X) <= C is a range comparison, which becomes a subtraction
13006          and a comparison, and is probably faster.  */
13007       if (code == LE_EXPR
13008           && TREE_CODE (arg1) == INTEGER_CST
13009           && TREE_CODE (arg0) == ABS_EXPR
13010           && ! TREE_SIDE_EFFECTS (arg0)
13011           && (0 != (tem = negate_expr (arg1)))
13012           && TREE_CODE (tem) == INTEGER_CST
13013           && !TREE_OVERFLOW (tem))
13014         return fold_build2 (TRUTH_ANDIF_EXPR, type,
13015                             build2 (GE_EXPR, type,
13016                                     TREE_OPERAND (arg0, 0), tem),
13017                             build2 (LE_EXPR, type,
13018                                     TREE_OPERAND (arg0, 0), arg1));
13019
13020       /* Convert ABS_EXPR<x> >= 0 to true.  */
13021       strict_overflow_p = false;
13022       if (code == GE_EXPR
13023           && (integer_zerop (arg1)
13024               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13025                   && real_zerop (arg1)))
13026           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13027         {
13028           if (strict_overflow_p)
13029             fold_overflow_warning (("assuming signed overflow does not occur "
13030                                     "when simplifying comparison of "
13031                                     "absolute value and zero"),
13032                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13033           return omit_one_operand (type, integer_one_node, arg0);
13034         }
13035
13036       /* Convert ABS_EXPR<x> < 0 to false.  */
13037       strict_overflow_p = false;
13038       if (code == LT_EXPR
13039           && (integer_zerop (arg1) || real_zerop (arg1))
13040           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13041         {
13042           if (strict_overflow_p)
13043             fold_overflow_warning (("assuming signed overflow does not occur "
13044                                     "when simplifying comparison of "
13045                                     "absolute value and zero"),
13046                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13047           return omit_one_operand (type, integer_zero_node, arg0);
13048         }
13049
13050       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13051          and similarly for >= into !=.  */
13052       if ((code == LT_EXPR || code == GE_EXPR)
13053           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13054           && TREE_CODE (arg1) == LSHIFT_EXPR
13055           && integer_onep (TREE_OPERAND (arg1, 0)))
13056         return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13057                        build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13058                                TREE_OPERAND (arg1, 1)),
13059                        build_int_cst (TREE_TYPE (arg0), 0));
13060
13061       if ((code == LT_EXPR || code == GE_EXPR)
13062           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13063           && CONVERT_EXPR_P (arg1)
13064           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13065           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13066         return
13067           build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13068                   fold_convert (TREE_TYPE (arg0),
13069                                 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13070                                         TREE_OPERAND (TREE_OPERAND (arg1, 0),
13071                                                       1))),
13072                   build_int_cst (TREE_TYPE (arg0), 0));
13073
13074       return NULL_TREE;
13075
13076     case UNORDERED_EXPR:
13077     case ORDERED_EXPR:
13078     case UNLT_EXPR:
13079     case UNLE_EXPR:
13080     case UNGT_EXPR:
13081     case UNGE_EXPR:
13082     case UNEQ_EXPR:
13083     case LTGT_EXPR:
13084       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13085         {
13086           t1 = fold_relational_const (code, type, arg0, arg1);
13087           if (t1 != NULL_TREE)
13088             return t1;
13089         }
13090
13091       /* If the first operand is NaN, the result is constant.  */
13092       if (TREE_CODE (arg0) == REAL_CST
13093           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13094           && (code != LTGT_EXPR || ! flag_trapping_math))
13095         {
13096           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13097                ? integer_zero_node
13098                : integer_one_node;
13099           return omit_one_operand (type, t1, arg1);
13100         }
13101
13102       /* If the second operand is NaN, the result is constant.  */
13103       if (TREE_CODE (arg1) == REAL_CST
13104           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13105           && (code != LTGT_EXPR || ! flag_trapping_math))
13106         {
13107           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13108                ? integer_zero_node
13109                : integer_one_node;
13110           return omit_one_operand (type, t1, arg0);
13111         }
13112
13113       /* Simplify unordered comparison of something with itself.  */
13114       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13115           && operand_equal_p (arg0, arg1, 0))
13116         return constant_boolean_node (1, type);
13117
13118       if (code == LTGT_EXPR
13119           && !flag_trapping_math
13120           && operand_equal_p (arg0, arg1, 0))
13121         return constant_boolean_node (0, type);
13122
13123       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13124       {
13125         tree targ0 = strip_float_extensions (arg0);
13126         tree targ1 = strip_float_extensions (arg1);
13127         tree newtype = TREE_TYPE (targ0);
13128
13129         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13130           newtype = TREE_TYPE (targ1);
13131
13132         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13133           return fold_build2 (code, type, fold_convert (newtype, targ0),
13134                               fold_convert (newtype, targ1));
13135       }
13136
13137       return NULL_TREE;
13138
13139     case COMPOUND_EXPR:
13140       /* When pedantic, a compound expression can be neither an lvalue
13141          nor an integer constant expression.  */
13142       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13143         return NULL_TREE;
13144       /* Don't let (0, 0) be null pointer constant.  */
13145       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13146                                  : fold_convert (type, arg1);
13147       return pedantic_non_lvalue (tem);
13148
13149     case COMPLEX_EXPR:
13150       if ((TREE_CODE (arg0) == REAL_CST
13151            && TREE_CODE (arg1) == REAL_CST)
13152           || (TREE_CODE (arg0) == INTEGER_CST
13153               && TREE_CODE (arg1) == INTEGER_CST))
13154         return build_complex (type, arg0, arg1);
13155       return NULL_TREE;
13156
13157     case ASSERT_EXPR:
13158       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13159       gcc_unreachable ();
13160
13161     default:
13162       return NULL_TREE;
13163     } /* switch (code) */
13164 }
13165
13166 /* Callback for walk_tree, looking for LABEL_EXPR.
13167    Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
13168    Do not check the sub-tree of GOTO_EXPR.  */
13169
13170 static tree
13171 contains_label_1 (tree *tp,
13172                   int *walk_subtrees,
13173                   void *data ATTRIBUTE_UNUSED)
13174 {
13175   switch (TREE_CODE (*tp))
13176     {
13177     case LABEL_EXPR:
13178       return *tp;
13179     case GOTO_EXPR:
13180       *walk_subtrees = 0;
13181     /* no break */
13182     default:
13183       return NULL_TREE;
13184     }
13185 }
13186
13187 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
13188    accessible from outside the sub-tree. Returns NULL_TREE if no
13189    addressable label is found.  */
13190
13191 static bool
13192 contains_label_p (tree st)
13193 {
13194   return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
13195 }
13196
13197 /* Fold a ternary expression of code CODE and type TYPE with operands
13198    OP0, OP1, and OP2.  Return the folded expression if folding is
13199    successful.  Otherwise, return NULL_TREE.  */
13200
13201 tree
13202 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
13203 {
13204   tree tem;
13205   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13206   enum tree_code_class kind = TREE_CODE_CLASS (code);
13207
13208   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13209               && TREE_CODE_LENGTH (code) == 3);
13210
13211   /* Strip any conversions that don't change the mode.  This is safe
13212      for every expression, except for a comparison expression because
13213      its signedness is derived from its operands.  So, in the latter
13214      case, only strip conversions that don't change the signedness.
13215
13216      Note that this is done as an internal manipulation within the
13217      constant folder, in order to find the simplest representation of
13218      the arguments so that their form can be studied.  In any cases,
13219      the appropriate type conversions should be put back in the tree
13220      that will get out of the constant folder.  */
13221   if (op0)
13222     {
13223       arg0 = op0;
13224       STRIP_NOPS (arg0);
13225     }
13226
13227   if (op1)
13228     {
13229       arg1 = op1;
13230       STRIP_NOPS (arg1);
13231     }
13232
13233   switch (code)
13234     {
13235     case COMPONENT_REF:
13236       if (TREE_CODE (arg0) == CONSTRUCTOR
13237           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13238         {
13239           unsigned HOST_WIDE_INT idx;
13240           tree field, value;
13241           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13242             if (field == arg1)
13243               return value;
13244         }
13245       return NULL_TREE;
13246
13247     case COND_EXPR:
13248       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13249          so all simple results must be passed through pedantic_non_lvalue.  */
13250       if (TREE_CODE (arg0) == INTEGER_CST)
13251         {
13252           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13253           tem = integer_zerop (arg0) ? op2 : op1;
13254           /* Only optimize constant conditions when the selected branch
13255              has the same type as the COND_EXPR.  This avoids optimizing
13256              away "c ? x : throw", where the throw has a void type.
13257              Avoid throwing away that operand which contains label.  */
13258           if ((!TREE_SIDE_EFFECTS (unused_op)
13259                || !contains_label_p (unused_op))
13260               && (! VOID_TYPE_P (TREE_TYPE (tem))
13261                   || VOID_TYPE_P (type)))
13262             return pedantic_non_lvalue (tem);
13263           return NULL_TREE;
13264         }
13265       if (operand_equal_p (arg1, op2, 0))
13266         return pedantic_omit_one_operand (type, arg1, arg0);
13267
13268       /* If we have A op B ? A : C, we may be able to convert this to a
13269          simpler expression, depending on the operation and the values
13270          of B and C.  Signed zeros prevent all of these transformations,
13271          for reasons given above each one.
13272
13273          Also try swapping the arguments and inverting the conditional.  */
13274       if (COMPARISON_CLASS_P (arg0)
13275           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13276                                              arg1, TREE_OPERAND (arg0, 1))
13277           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13278         {
13279           tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
13280           if (tem)
13281             return tem;
13282         }
13283
13284       if (COMPARISON_CLASS_P (arg0)
13285           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13286                                              op2,
13287                                              TREE_OPERAND (arg0, 1))
13288           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13289         {
13290           tem = fold_truth_not_expr (arg0);
13291           if (tem && COMPARISON_CLASS_P (tem))
13292             {
13293               tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
13294               if (tem)
13295                 return tem;
13296             }
13297         }
13298
13299       /* If the second operand is simpler than the third, swap them
13300          since that produces better jump optimization results.  */
13301       if (truth_value_p (TREE_CODE (arg0))
13302           && tree_swap_operands_p (op1, op2, false))
13303         {
13304           /* See if this can be inverted.  If it can't, possibly because
13305              it was a floating-point inequality comparison, don't do
13306              anything.  */
13307           tem = fold_truth_not_expr (arg0);
13308           if (tem)
13309             return fold_build3 (code, type, tem, op2, op1);
13310         }
13311
13312       /* Convert A ? 1 : 0 to simply A.  */
13313       if (integer_onep (op1)
13314           && integer_zerop (op2)
13315           /* If we try to convert OP0 to our type, the
13316              call to fold will try to move the conversion inside
13317              a COND, which will recurse.  In that case, the COND_EXPR
13318              is probably the best choice, so leave it alone.  */
13319           && type == TREE_TYPE (arg0))
13320         return pedantic_non_lvalue (arg0);
13321
13322       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13323          over COND_EXPR in cases such as floating point comparisons.  */
13324       if (integer_zerop (op1)
13325           && integer_onep (op2)
13326           && truth_value_p (TREE_CODE (arg0)))
13327         return pedantic_non_lvalue (fold_convert (type,
13328                                                   invert_truthvalue (arg0)));
13329
13330       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13331       if (TREE_CODE (arg0) == LT_EXPR
13332           && integer_zerop (TREE_OPERAND (arg0, 1))
13333           && integer_zerop (op2)
13334           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13335         {
13336           /* sign_bit_p only checks ARG1 bits within A's precision.
13337              If <sign bit of A> has wider type than A, bits outside
13338              of A's precision in <sign bit of A> need to be checked.
13339              If they are all 0, this optimization needs to be done
13340              in unsigned A's type, if they are all 1 in signed A's type,
13341              otherwise this can't be done.  */
13342           if (TYPE_PRECISION (TREE_TYPE (tem))
13343               < TYPE_PRECISION (TREE_TYPE (arg1))
13344               && TYPE_PRECISION (TREE_TYPE (tem))
13345                  < TYPE_PRECISION (type))
13346             {
13347               unsigned HOST_WIDE_INT mask_lo;
13348               HOST_WIDE_INT mask_hi;
13349               int inner_width, outer_width;
13350               tree tem_type;
13351
13352               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13353               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13354               if (outer_width > TYPE_PRECISION (type))
13355                 outer_width = TYPE_PRECISION (type);
13356
13357               if (outer_width > HOST_BITS_PER_WIDE_INT)
13358                 {
13359                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13360                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13361                   mask_lo = -1;
13362                 }
13363               else
13364                 {
13365                   mask_hi = 0;
13366                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13367                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13368                 }
13369               if (inner_width > HOST_BITS_PER_WIDE_INT)
13370                 {
13371                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13372                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13373                   mask_lo = 0;
13374                 }
13375               else
13376                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13377                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13378
13379               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13380                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13381                 {
13382                   tem_type = signed_type_for (TREE_TYPE (tem));
13383                   tem = fold_convert (tem_type, tem);
13384                 }
13385               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13386                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13387                 {
13388                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13389                   tem = fold_convert (tem_type, tem);
13390                 }
13391               else
13392                 tem = NULL;
13393             }
13394
13395           if (tem)
13396             return fold_convert (type,
13397                                  fold_build2 (BIT_AND_EXPR,
13398                                               TREE_TYPE (tem), tem,
13399                                               fold_convert (TREE_TYPE (tem),
13400                                                             arg1)));
13401         }
13402
13403       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13404          already handled above.  */
13405       if (TREE_CODE (arg0) == BIT_AND_EXPR
13406           && integer_onep (TREE_OPERAND (arg0, 1))
13407           && integer_zerop (op2)
13408           && integer_pow2p (arg1))
13409         {
13410           tree tem = TREE_OPERAND (arg0, 0);
13411           STRIP_NOPS (tem);
13412           if (TREE_CODE (tem) == RSHIFT_EXPR
13413               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13414               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13415                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13416             return fold_build2 (BIT_AND_EXPR, type,
13417                                 TREE_OPERAND (tem, 0), arg1);
13418         }
13419
13420       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13421          is probably obsolete because the first operand should be a
13422          truth value (that's why we have the two cases above), but let's
13423          leave it in until we can confirm this for all front-ends.  */
13424       if (integer_zerop (op2)
13425           && TREE_CODE (arg0) == NE_EXPR
13426           && integer_zerop (TREE_OPERAND (arg0, 1))
13427           && integer_pow2p (arg1)
13428           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13429           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13430                               arg1, OEP_ONLY_CONST))
13431         return pedantic_non_lvalue (fold_convert (type,
13432                                                   TREE_OPERAND (arg0, 0)));
13433
13434       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13435       if (integer_zerop (op2)
13436           && truth_value_p (TREE_CODE (arg0))
13437           && truth_value_p (TREE_CODE (arg1)))
13438         return fold_build2 (TRUTH_ANDIF_EXPR, type,
13439                             fold_convert (type, arg0),
13440                             arg1);
13441
13442       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13443       if (integer_onep (op2)
13444           && truth_value_p (TREE_CODE (arg0))
13445           && truth_value_p (TREE_CODE (arg1)))
13446         {
13447           /* Only perform transformation if ARG0 is easily inverted.  */
13448           tem = fold_truth_not_expr (arg0);
13449           if (tem)
13450             return fold_build2 (TRUTH_ORIF_EXPR, type,
13451                                 fold_convert (type, tem),
13452                                 arg1);
13453         }
13454
13455       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13456       if (integer_zerop (arg1)
13457           && truth_value_p (TREE_CODE (arg0))
13458           && truth_value_p (TREE_CODE (op2)))
13459         {
13460           /* Only perform transformation if ARG0 is easily inverted.  */
13461           tem = fold_truth_not_expr (arg0);
13462           if (tem)
13463             return fold_build2 (TRUTH_ANDIF_EXPR, type,
13464                                 fold_convert (type, tem),
13465                                 op2);
13466         }
13467
13468       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13469       if (integer_onep (arg1)
13470           && truth_value_p (TREE_CODE (arg0))
13471           && truth_value_p (TREE_CODE (op2)))
13472         return fold_build2 (TRUTH_ORIF_EXPR, type,
13473                             fold_convert (type, arg0),
13474                             op2);
13475
13476       return NULL_TREE;
13477
13478     case CALL_EXPR:
13479       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13480          of fold_ternary on them.  */
13481       gcc_unreachable ();
13482
13483     case BIT_FIELD_REF:
13484       if ((TREE_CODE (arg0) == VECTOR_CST
13485            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13486           && type == TREE_TYPE (TREE_TYPE (arg0)))
13487         {
13488           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13489           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13490
13491           if (width != 0
13492               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13493               && (idx % width) == 0
13494               && (idx = idx / width)
13495                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13496             {
13497               tree elements = NULL_TREE;
13498
13499               if (TREE_CODE (arg0) == VECTOR_CST)
13500                 elements = TREE_VECTOR_CST_ELTS (arg0);
13501               else
13502                 {
13503                   unsigned HOST_WIDE_INT idx;
13504                   tree value;
13505
13506                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13507                     elements = tree_cons (NULL_TREE, value, elements);
13508                 }
13509               while (idx-- > 0 && elements)
13510                 elements = TREE_CHAIN (elements);
13511               if (elements)
13512                 return TREE_VALUE (elements);
13513               else
13514                 return fold_convert (type, integer_zero_node);
13515             }
13516         }
13517
13518       /* A bit-field-ref that referenced the full argument can be stripped.  */
13519       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13520           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13521           && integer_zerop (op2))
13522         return fold_convert (type, arg0);
13523
13524       return NULL_TREE;
13525
13526     default:
13527       return NULL_TREE;
13528     } /* switch (code) */
13529 }
13530
13531 /* Perform constant folding and related simplification of EXPR.
13532    The related simplifications include x*1 => x, x*0 => 0, etc.,
13533    and application of the associative law.
13534    NOP_EXPR conversions may be removed freely (as long as we
13535    are careful not to change the type of the overall expression).
13536    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13537    but we can constant-fold them if they have constant operands.  */
13538
13539 #ifdef ENABLE_FOLD_CHECKING
13540 # define fold(x) fold_1 (x)
13541 static tree fold_1 (tree);
13542 static
13543 #endif
13544 tree
13545 fold (tree expr)
13546 {
13547   const tree t = expr;
13548   enum tree_code code = TREE_CODE (t);
13549   enum tree_code_class kind = TREE_CODE_CLASS (code);
13550   tree tem;
13551
13552   /* Return right away if a constant.  */
13553   if (kind == tcc_constant)
13554     return t;
13555
13556   /* CALL_EXPR-like objects with variable numbers of operands are
13557      treated specially.  */
13558   if (kind == tcc_vl_exp)
13559     {
13560       if (code == CALL_EXPR)
13561         {
13562           tem = fold_call_expr (expr, false);
13563           return tem ? tem : expr;
13564         }
13565       return expr;
13566     }
13567
13568   if (IS_EXPR_CODE_CLASS (kind))
13569     {
13570       tree type = TREE_TYPE (t);
13571       tree op0, op1, op2;
13572
13573       switch (TREE_CODE_LENGTH (code))
13574         {
13575         case 1:
13576           op0 = TREE_OPERAND (t, 0);
13577           tem = fold_unary (code, type, op0);
13578           return tem ? tem : expr;
13579         case 2:
13580           op0 = TREE_OPERAND (t, 0);
13581           op1 = TREE_OPERAND (t, 1);
13582           tem = fold_binary (code, type, op0, op1);
13583           return tem ? tem : expr;
13584         case 3:
13585           op0 = TREE_OPERAND (t, 0);
13586           op1 = TREE_OPERAND (t, 1);
13587           op2 = TREE_OPERAND (t, 2);
13588           tem = fold_ternary (code, type, op0, op1, op2);
13589           return tem ? tem : expr;
13590         default:
13591           break;
13592         }
13593     }
13594
13595   switch (code)
13596     {
13597     case ARRAY_REF:
13598       {
13599         tree op0 = TREE_OPERAND (t, 0);
13600         tree op1 = TREE_OPERAND (t, 1);
13601
13602         if (TREE_CODE (op1) == INTEGER_CST
13603             && TREE_CODE (op0) == CONSTRUCTOR
13604             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13605           {
13606             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13607             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13608             unsigned HOST_WIDE_INT begin = 0;
13609
13610             /* Find a matching index by means of a binary search.  */
13611             while (begin != end)
13612               {
13613                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13614                 tree index = VEC_index (constructor_elt, elts, middle)->index;
13615
13616                 if (TREE_CODE (index) == INTEGER_CST
13617                     && tree_int_cst_lt (index, op1))
13618                   begin = middle + 1;
13619                 else if (TREE_CODE (index) == INTEGER_CST
13620                          && tree_int_cst_lt (op1, index))
13621                   end = middle;
13622                 else if (TREE_CODE (index) == RANGE_EXPR
13623                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13624                   begin = middle + 1;
13625                 else if (TREE_CODE (index) == RANGE_EXPR
13626                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13627                   end = middle;
13628                 else
13629                   return VEC_index (constructor_elt, elts, middle)->value;
13630               }
13631           }
13632
13633         return t;
13634       }
13635
13636     case CONST_DECL:
13637       return fold (DECL_INITIAL (t));
13638
13639     default:
13640       return t;
13641     } /* switch (code) */
13642 }
13643
13644 #ifdef ENABLE_FOLD_CHECKING
13645 #undef fold
13646
13647 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13648 static void fold_check_failed (const_tree, const_tree);
13649 void print_fold_checksum (const_tree);
13650
13651 /* When --enable-checking=fold, compute a digest of expr before
13652    and after actual fold call to see if fold did not accidentally
13653    change original expr.  */
13654
13655 tree
13656 fold (tree expr)
13657 {
13658   tree ret;
13659   struct md5_ctx ctx;
13660   unsigned char checksum_before[16], checksum_after[16];
13661   htab_t ht;
13662
13663   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13664   md5_init_ctx (&ctx);
13665   fold_checksum_tree (expr, &ctx, ht);
13666   md5_finish_ctx (&ctx, checksum_before);
13667   htab_empty (ht);
13668
13669   ret = fold_1 (expr);
13670
13671   md5_init_ctx (&ctx);
13672   fold_checksum_tree (expr, &ctx, ht);
13673   md5_finish_ctx (&ctx, checksum_after);
13674   htab_delete (ht);
13675
13676   if (memcmp (checksum_before, checksum_after, 16))
13677     fold_check_failed (expr, ret);
13678
13679   return ret;
13680 }
13681
13682 void
13683 print_fold_checksum (const_tree expr)
13684 {
13685   struct md5_ctx ctx;
13686   unsigned char checksum[16], cnt;
13687   htab_t ht;
13688
13689   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13690   md5_init_ctx (&ctx);
13691   fold_checksum_tree (expr, &ctx, ht);
13692   md5_finish_ctx (&ctx, checksum);
13693   htab_delete (ht);
13694   for (cnt = 0; cnt < 16; ++cnt)
13695     fprintf (stderr, "%02x", checksum[cnt]);
13696   putc ('\n', stderr);
13697 }
13698
13699 static void
13700 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13701 {
13702   internal_error ("fold check: original tree changed by fold");
13703 }
13704
13705 static void
13706 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13707 {
13708   const void **slot;
13709   enum tree_code code;
13710   union tree_node buf;
13711   int i, len;
13712   
13713 recursive_label:
13714
13715   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13716                <= sizeof (struct tree_function_decl))
13717               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13718   if (expr == NULL)
13719     return;
13720   slot = (const void **) htab_find_slot (ht, expr, INSERT);
13721   if (*slot != NULL)
13722     return;
13723   *slot = expr;
13724   code = TREE_CODE (expr);
13725   if (TREE_CODE_CLASS (code) == tcc_declaration
13726       && DECL_ASSEMBLER_NAME_SET_P (expr))
13727     {
13728       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13729       memcpy ((char *) &buf, expr, tree_size (expr));
13730       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13731       expr = (tree) &buf;
13732     }
13733   else if (TREE_CODE_CLASS (code) == tcc_type
13734            && (TYPE_POINTER_TO (expr)
13735                || TYPE_REFERENCE_TO (expr)
13736                || TYPE_CACHED_VALUES_P (expr)
13737                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13738                || TYPE_NEXT_VARIANT (expr)))
13739     {
13740       /* Allow these fields to be modified.  */
13741       tree tmp;
13742       memcpy ((char *) &buf, expr, tree_size (expr));
13743       expr = tmp = (tree) &buf;
13744       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13745       TYPE_POINTER_TO (tmp) = NULL;
13746       TYPE_REFERENCE_TO (tmp) = NULL;
13747       TYPE_NEXT_VARIANT (tmp) = NULL;
13748       if (TYPE_CACHED_VALUES_P (tmp))
13749         {
13750           TYPE_CACHED_VALUES_P (tmp) = 0;
13751           TYPE_CACHED_VALUES (tmp) = NULL;
13752         }
13753     }
13754   md5_process_bytes (expr, tree_size (expr), ctx);
13755   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13756   if (TREE_CODE_CLASS (code) != tcc_type
13757       && TREE_CODE_CLASS (code) != tcc_declaration
13758       && code != TREE_LIST
13759       && code != SSA_NAME)
13760     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13761   switch (TREE_CODE_CLASS (code))
13762     {
13763     case tcc_constant:
13764       switch (code)
13765         {
13766         case STRING_CST:
13767           md5_process_bytes (TREE_STRING_POINTER (expr),
13768                              TREE_STRING_LENGTH (expr), ctx);
13769           break;
13770         case COMPLEX_CST:
13771           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13772           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13773           break;
13774         case VECTOR_CST:
13775           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13776           break;
13777         default:
13778           break;
13779         }
13780       break;
13781     case tcc_exceptional:
13782       switch (code)
13783         {
13784         case TREE_LIST:
13785           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13786           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13787           expr = TREE_CHAIN (expr);
13788           goto recursive_label;
13789           break;
13790         case TREE_VEC:
13791           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13792             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13793           break;
13794         default:
13795           break;
13796         }
13797       break;
13798     case tcc_expression:
13799     case tcc_reference:
13800     case tcc_comparison:
13801     case tcc_unary:
13802     case tcc_binary:
13803     case tcc_statement:
13804     case tcc_vl_exp:
13805       len = TREE_OPERAND_LENGTH (expr);
13806       for (i = 0; i < len; ++i)
13807         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13808       break;
13809     case tcc_declaration:
13810       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13811       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13812       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13813         {
13814           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13815           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13816           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13817           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13818           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13819         }
13820       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13821         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13822           
13823       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13824         {
13825           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13826           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13827           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13828         }
13829       break;
13830     case tcc_type:
13831       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13832         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13833       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13834       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13835       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13836       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13837       if (INTEGRAL_TYPE_P (expr)
13838           || SCALAR_FLOAT_TYPE_P (expr))
13839         {
13840           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13841           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13842         }
13843       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13844       if (TREE_CODE (expr) == RECORD_TYPE
13845           || TREE_CODE (expr) == UNION_TYPE
13846           || TREE_CODE (expr) == QUAL_UNION_TYPE)
13847         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13848       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13849       break;
13850     default:
13851       break;
13852     }
13853 }
13854
13855 /* Helper function for outputting the checksum of a tree T.  When
13856    debugging with gdb, you can "define mynext" to be "next" followed
13857    by "call debug_fold_checksum (op0)", then just trace down till the
13858    outputs differ.  */
13859
13860 void
13861 debug_fold_checksum (const_tree t)
13862 {
13863   int i;
13864   unsigned char checksum[16];
13865   struct md5_ctx ctx;
13866   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13867   
13868   md5_init_ctx (&ctx);
13869   fold_checksum_tree (t, &ctx, ht);
13870   md5_finish_ctx (&ctx, checksum);
13871   htab_empty (ht);
13872
13873   for (i = 0; i < 16; i++)
13874     fprintf (stderr, "%d ", checksum[i]);
13875
13876   fprintf (stderr, "\n");
13877 }
13878
13879 #endif
13880
13881 /* Fold a unary tree expression with code CODE of type TYPE with an
13882    operand OP0.  Return a folded expression if successful.  Otherwise,
13883    return a tree expression with code CODE of type TYPE with an
13884    operand OP0.  */
13885
13886 tree
13887 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13888 {
13889   tree tem;
13890 #ifdef ENABLE_FOLD_CHECKING
13891   unsigned char checksum_before[16], checksum_after[16];
13892   struct md5_ctx ctx;
13893   htab_t ht;
13894
13895   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13896   md5_init_ctx (&ctx);
13897   fold_checksum_tree (op0, &ctx, ht);
13898   md5_finish_ctx (&ctx, checksum_before);
13899   htab_empty (ht);
13900 #endif
13901   
13902   tem = fold_unary (code, type, op0);
13903   if (!tem)
13904     tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13905   
13906 #ifdef ENABLE_FOLD_CHECKING
13907   md5_init_ctx (&ctx);
13908   fold_checksum_tree (op0, &ctx, ht);
13909   md5_finish_ctx (&ctx, checksum_after);
13910   htab_delete (ht);
13911
13912   if (memcmp (checksum_before, checksum_after, 16))
13913     fold_check_failed (op0, tem);
13914 #endif
13915   return tem;
13916 }
13917
13918 /* Fold a binary tree expression with code CODE of type TYPE with
13919    operands OP0 and OP1.  Return a folded expression if successful.
13920    Otherwise, return a tree expression with code CODE of type TYPE
13921    with operands OP0 and OP1.  */
13922
13923 tree
13924 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13925                   MEM_STAT_DECL)
13926 {
13927   tree tem;
13928 #ifdef ENABLE_FOLD_CHECKING
13929   unsigned char checksum_before_op0[16],
13930                 checksum_before_op1[16],
13931                 checksum_after_op0[16],
13932                 checksum_after_op1[16];
13933   struct md5_ctx ctx;
13934   htab_t ht;
13935
13936   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13937   md5_init_ctx (&ctx);
13938   fold_checksum_tree (op0, &ctx, ht);
13939   md5_finish_ctx (&ctx, checksum_before_op0);
13940   htab_empty (ht);
13941
13942   md5_init_ctx (&ctx);
13943   fold_checksum_tree (op1, &ctx, ht);
13944   md5_finish_ctx (&ctx, checksum_before_op1);
13945   htab_empty (ht);
13946 #endif
13947
13948   tem = fold_binary (code, type, op0, op1);
13949   if (!tem)
13950     tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13951   
13952 #ifdef ENABLE_FOLD_CHECKING
13953   md5_init_ctx (&ctx);
13954   fold_checksum_tree (op0, &ctx, ht);
13955   md5_finish_ctx (&ctx, checksum_after_op0);
13956   htab_empty (ht);
13957
13958   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13959     fold_check_failed (op0, tem);
13960   
13961   md5_init_ctx (&ctx);
13962   fold_checksum_tree (op1, &ctx, ht);
13963   md5_finish_ctx (&ctx, checksum_after_op1);
13964   htab_delete (ht);
13965
13966   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13967     fold_check_failed (op1, tem);
13968 #endif
13969   return tem;
13970 }
13971
13972 /* Fold a ternary tree expression with code CODE of type TYPE with
13973    operands OP0, OP1, and OP2.  Return a folded expression if
13974    successful.  Otherwise, return a tree expression with code CODE of
13975    type TYPE with operands OP0, OP1, and OP2.  */
13976
13977 tree
13978 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
13979              MEM_STAT_DECL)
13980 {
13981   tree tem;
13982 #ifdef ENABLE_FOLD_CHECKING
13983   unsigned char checksum_before_op0[16],
13984                 checksum_before_op1[16],
13985                 checksum_before_op2[16],
13986                 checksum_after_op0[16],
13987                 checksum_after_op1[16],
13988                 checksum_after_op2[16];
13989   struct md5_ctx ctx;
13990   htab_t ht;
13991
13992   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13993   md5_init_ctx (&ctx);
13994   fold_checksum_tree (op0, &ctx, ht);
13995   md5_finish_ctx (&ctx, checksum_before_op0);
13996   htab_empty (ht);
13997
13998   md5_init_ctx (&ctx);
13999   fold_checksum_tree (op1, &ctx, ht);
14000   md5_finish_ctx (&ctx, checksum_before_op1);
14001   htab_empty (ht);
14002
14003   md5_init_ctx (&ctx);
14004   fold_checksum_tree (op2, &ctx, ht);
14005   md5_finish_ctx (&ctx, checksum_before_op2);
14006   htab_empty (ht);
14007 #endif
14008
14009   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14010   tem = fold_ternary (code, type, op0, op1, op2);
14011   if (!tem)
14012     tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
14013       
14014 #ifdef ENABLE_FOLD_CHECKING
14015   md5_init_ctx (&ctx);
14016   fold_checksum_tree (op0, &ctx, ht);
14017   md5_finish_ctx (&ctx, checksum_after_op0);
14018   htab_empty (ht);
14019
14020   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14021     fold_check_failed (op0, tem);
14022   
14023   md5_init_ctx (&ctx);
14024   fold_checksum_tree (op1, &ctx, ht);
14025   md5_finish_ctx (&ctx, checksum_after_op1);
14026   htab_empty (ht);
14027
14028   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14029     fold_check_failed (op1, tem);
14030   
14031   md5_init_ctx (&ctx);
14032   fold_checksum_tree (op2, &ctx, ht);
14033   md5_finish_ctx (&ctx, checksum_after_op2);
14034   htab_delete (ht);
14035
14036   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14037     fold_check_failed (op2, tem);
14038 #endif
14039   return tem;
14040 }
14041
14042 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14043    arguments in ARGARRAY, and a null static chain.
14044    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14045    of type TYPE from the given operands as constructed by build_call_array.  */
14046
14047 tree
14048 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
14049 {
14050   tree tem;
14051 #ifdef ENABLE_FOLD_CHECKING
14052   unsigned char checksum_before_fn[16],
14053                 checksum_before_arglist[16],
14054                 checksum_after_fn[16],
14055                 checksum_after_arglist[16];
14056   struct md5_ctx ctx;
14057   htab_t ht;
14058   int i;
14059
14060   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14061   md5_init_ctx (&ctx);
14062   fold_checksum_tree (fn, &ctx, ht);
14063   md5_finish_ctx (&ctx, checksum_before_fn);
14064   htab_empty (ht);
14065
14066   md5_init_ctx (&ctx);
14067   for (i = 0; i < nargs; i++)
14068     fold_checksum_tree (argarray[i], &ctx, ht);
14069   md5_finish_ctx (&ctx, checksum_before_arglist);
14070   htab_empty (ht);
14071 #endif
14072
14073   tem = fold_builtin_call_array (type, fn, nargs, argarray);
14074       
14075 #ifdef ENABLE_FOLD_CHECKING
14076   md5_init_ctx (&ctx);
14077   fold_checksum_tree (fn, &ctx, ht);
14078   md5_finish_ctx (&ctx, checksum_after_fn);
14079   htab_empty (ht);
14080
14081   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14082     fold_check_failed (fn, tem);
14083   
14084   md5_init_ctx (&ctx);
14085   for (i = 0; i < nargs; i++)
14086     fold_checksum_tree (argarray[i], &ctx, ht);
14087   md5_finish_ctx (&ctx, checksum_after_arglist);
14088   htab_delete (ht);
14089
14090   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14091     fold_check_failed (NULL_TREE, tem);
14092 #endif
14093   return tem;
14094 }
14095
14096 /* Perform constant folding and related simplification of initializer
14097    expression EXPR.  These behave identically to "fold_buildN" but ignore
14098    potential run-time traps and exceptions that fold must preserve.  */
14099
14100 #define START_FOLD_INIT \
14101   int saved_signaling_nans = flag_signaling_nans;\
14102   int saved_trapping_math = flag_trapping_math;\
14103   int saved_rounding_math = flag_rounding_math;\
14104   int saved_trapv = flag_trapv;\
14105   int saved_folding_initializer = folding_initializer;\
14106   flag_signaling_nans = 0;\
14107   flag_trapping_math = 0;\
14108   flag_rounding_math = 0;\
14109   flag_trapv = 0;\
14110   folding_initializer = 1;
14111
14112 #define END_FOLD_INIT \
14113   flag_signaling_nans = saved_signaling_nans;\
14114   flag_trapping_math = saved_trapping_math;\
14115   flag_rounding_math = saved_rounding_math;\
14116   flag_trapv = saved_trapv;\
14117   folding_initializer = saved_folding_initializer;
14118
14119 tree
14120 fold_build1_initializer (enum tree_code code, tree type, tree op)
14121 {
14122   tree result;
14123   START_FOLD_INIT;
14124
14125   result = fold_build1 (code, type, op);
14126
14127   END_FOLD_INIT;
14128   return result;
14129 }
14130
14131 tree
14132 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
14133 {
14134   tree result;
14135   START_FOLD_INIT;
14136
14137   result = fold_build2 (code, type, op0, op1);
14138
14139   END_FOLD_INIT;
14140   return result;
14141 }
14142
14143 tree
14144 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
14145                          tree op2)
14146 {
14147   tree result;
14148   START_FOLD_INIT;
14149
14150   result = fold_build3 (code, type, op0, op1, op2);
14151
14152   END_FOLD_INIT;
14153   return result;
14154 }
14155
14156 tree
14157 fold_build_call_array_initializer (tree type, tree fn,
14158                                    int nargs, tree *argarray)
14159 {
14160   tree result;
14161   START_FOLD_INIT;
14162
14163   result = fold_build_call_array (type, fn, nargs, argarray);
14164
14165   END_FOLD_INIT;
14166   return result;
14167 }
14168
14169 #undef START_FOLD_INIT
14170 #undef END_FOLD_INIT
14171
14172 /* Determine if first argument is a multiple of second argument.  Return 0 if
14173    it is not, or we cannot easily determined it to be.
14174
14175    An example of the sort of thing we care about (at this point; this routine
14176    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14177    fold cases do now) is discovering that
14178
14179      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14180
14181    is a multiple of
14182
14183      SAVE_EXPR (J * 8)
14184
14185    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14186
14187    This code also handles discovering that
14188
14189      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14190
14191    is a multiple of 8 so we don't have to worry about dealing with a
14192    possible remainder.
14193
14194    Note that we *look* inside a SAVE_EXPR only to determine how it was
14195    calculated; it is not safe for fold to do much of anything else with the
14196    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14197    at run time.  For example, the latter example above *cannot* be implemented
14198    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14199    evaluation time of the original SAVE_EXPR is not necessarily the same at
14200    the time the new expression is evaluated.  The only optimization of this
14201    sort that would be valid is changing
14202
14203      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14204
14205    divided by 8 to
14206
14207      SAVE_EXPR (I) * SAVE_EXPR (J)
14208
14209    (where the same SAVE_EXPR (J) is used in the original and the
14210    transformed version).  */
14211
14212 int
14213 multiple_of_p (tree type, const_tree top, const_tree bottom)
14214 {
14215   if (operand_equal_p (top, bottom, 0))
14216     return 1;
14217
14218   if (TREE_CODE (type) != INTEGER_TYPE)
14219     return 0;
14220
14221   switch (TREE_CODE (top))
14222     {
14223     case BIT_AND_EXPR:
14224       /* Bitwise and provides a power of two multiple.  If the mask is
14225          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14226       if (!integer_pow2p (bottom))
14227         return 0;
14228       /* FALLTHRU */
14229
14230     case MULT_EXPR:
14231       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14232               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14233
14234     case PLUS_EXPR:
14235     case MINUS_EXPR:
14236       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14237               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14238
14239     case LSHIFT_EXPR:
14240       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14241         {
14242           tree op1, t1;
14243
14244           op1 = TREE_OPERAND (top, 1);
14245           /* const_binop may not detect overflow correctly,
14246              so check for it explicitly here.  */
14247           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14248               > TREE_INT_CST_LOW (op1)
14249               && TREE_INT_CST_HIGH (op1) == 0
14250               && 0 != (t1 = fold_convert (type,
14251                                           const_binop (LSHIFT_EXPR,
14252                                                        size_one_node,
14253                                                        op1, 0)))
14254               && !TREE_OVERFLOW (t1))
14255             return multiple_of_p (type, t1, bottom);
14256         }
14257       return 0;
14258
14259     case NOP_EXPR:
14260       /* Can't handle conversions from non-integral or wider integral type.  */
14261       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14262           || (TYPE_PRECISION (type)
14263               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14264         return 0;
14265
14266       /* .. fall through ...  */
14267
14268     case SAVE_EXPR:
14269       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14270
14271     case INTEGER_CST:
14272       if (TREE_CODE (bottom) != INTEGER_CST
14273           || integer_zerop (bottom)
14274           || (TYPE_UNSIGNED (type)
14275               && (tree_int_cst_sgn (top) < 0
14276                   || tree_int_cst_sgn (bottom) < 0)))
14277         return 0;
14278       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14279                                              top, bottom, 0));
14280
14281     default:
14282       return 0;
14283     }
14284 }
14285
14286 /* Return true if CODE or TYPE is known to be non-negative. */
14287
14288 static bool
14289 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14290 {
14291   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14292       && truth_value_p (code))
14293     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14294        have a signed:1 type (where the value is -1 and 0).  */
14295     return true;
14296   return false;
14297 }
14298
14299 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14300    value is based on the assumption that signed overflow is undefined,
14301    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14302    *STRICT_OVERFLOW_P.  */
14303
14304 bool
14305 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14306                                 bool *strict_overflow_p)
14307 {
14308   if (TYPE_UNSIGNED (type))
14309     return true;
14310
14311   switch (code)
14312     {
14313     case ABS_EXPR:
14314       /* We can't return 1 if flag_wrapv is set because
14315          ABS_EXPR<INT_MIN> = INT_MIN.  */
14316       if (!INTEGRAL_TYPE_P (type))
14317         return true;
14318       if (TYPE_OVERFLOW_UNDEFINED (type))
14319         {
14320           *strict_overflow_p = true;
14321           return true;
14322         }
14323       break;
14324
14325     case NON_LVALUE_EXPR:
14326     case FLOAT_EXPR:
14327     case FIX_TRUNC_EXPR:
14328       return tree_expr_nonnegative_warnv_p (op0,
14329                                             strict_overflow_p);
14330
14331     case NOP_EXPR:
14332       {
14333         tree inner_type = TREE_TYPE (op0);
14334         tree outer_type = type;
14335
14336         if (TREE_CODE (outer_type) == REAL_TYPE)
14337           {
14338             if (TREE_CODE (inner_type) == REAL_TYPE)
14339               return tree_expr_nonnegative_warnv_p (op0,
14340                                                     strict_overflow_p);
14341             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14342               {
14343                 if (TYPE_UNSIGNED (inner_type))
14344                   return true;
14345                 return tree_expr_nonnegative_warnv_p (op0,
14346                                                       strict_overflow_p);
14347               }
14348           }
14349         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14350           {
14351             if (TREE_CODE (inner_type) == REAL_TYPE)
14352               return tree_expr_nonnegative_warnv_p (op0,
14353                                                     strict_overflow_p);
14354             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14355               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14356                       && TYPE_UNSIGNED (inner_type);
14357           }
14358       }
14359       break;
14360
14361     default:
14362       return tree_simple_nonnegative_warnv_p (code, type);
14363     }
14364
14365   /* We don't know sign of `t', so be conservative and return false.  */
14366   return false;
14367 }
14368
14369 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14370    value is based on the assumption that signed overflow is undefined,
14371    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14372    *STRICT_OVERFLOW_P.  */
14373
14374 bool
14375 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14376                                       tree op1, bool *strict_overflow_p)
14377 {
14378   if (TYPE_UNSIGNED (type))
14379     return true;
14380
14381   switch (code)
14382     {
14383     case POINTER_PLUS_EXPR:
14384     case PLUS_EXPR:
14385       if (FLOAT_TYPE_P (type))
14386         return (tree_expr_nonnegative_warnv_p (op0,
14387                                                strict_overflow_p)
14388                 && tree_expr_nonnegative_warnv_p (op1,
14389                                                   strict_overflow_p));
14390
14391       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14392          both unsigned and at least 2 bits shorter than the result.  */
14393       if (TREE_CODE (type) == INTEGER_TYPE
14394           && TREE_CODE (op0) == NOP_EXPR
14395           && TREE_CODE (op1) == NOP_EXPR)
14396         {
14397           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14398           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14399           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14400               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14401             {
14402               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14403                                        TYPE_PRECISION (inner2)) + 1;
14404               return prec < TYPE_PRECISION (type);
14405             }
14406         }
14407       break;
14408
14409     case MULT_EXPR:
14410       if (FLOAT_TYPE_P (type))
14411         {
14412           /* x * x for floating point x is always non-negative.  */
14413           if (operand_equal_p (op0, op1, 0))
14414             return true;
14415           return (tree_expr_nonnegative_warnv_p (op0,
14416                                                  strict_overflow_p)
14417                   && tree_expr_nonnegative_warnv_p (op1,
14418                                                     strict_overflow_p));
14419         }
14420
14421       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14422          both unsigned and their total bits is shorter than the result.  */
14423       if (TREE_CODE (type) == INTEGER_TYPE
14424           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14425           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14426         {
14427           tree inner0 = (TREE_CODE (op0) == NOP_EXPR) 
14428             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14429             : TREE_TYPE (op0);
14430           tree inner1 = (TREE_CODE (op1) == NOP_EXPR) 
14431             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14432             : TREE_TYPE (op1);
14433
14434           bool unsigned0 = TYPE_UNSIGNED (inner0);
14435           bool unsigned1 = TYPE_UNSIGNED (inner1);
14436
14437           if (TREE_CODE (op0) == INTEGER_CST)
14438             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14439
14440           if (TREE_CODE (op1) == INTEGER_CST)
14441             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14442
14443           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14444               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14445             {
14446               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14447                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14448                 : TYPE_PRECISION (inner0);
14449
14450               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14451                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14452                 : TYPE_PRECISION (inner1);
14453
14454               return precision0 + precision1 < TYPE_PRECISION (type);
14455             }
14456         }
14457       return false;
14458
14459     case BIT_AND_EXPR:
14460     case MAX_EXPR:
14461       return (tree_expr_nonnegative_warnv_p (op0,
14462                                              strict_overflow_p)
14463               || tree_expr_nonnegative_warnv_p (op1,
14464                                                 strict_overflow_p));
14465
14466     case BIT_IOR_EXPR:
14467     case BIT_XOR_EXPR:
14468     case MIN_EXPR:
14469     case RDIV_EXPR:
14470     case TRUNC_DIV_EXPR:
14471     case CEIL_DIV_EXPR:
14472     case FLOOR_DIV_EXPR:
14473     case ROUND_DIV_EXPR:
14474       return (tree_expr_nonnegative_warnv_p (op0,
14475                                              strict_overflow_p)
14476               && tree_expr_nonnegative_warnv_p (op1,
14477                                                 strict_overflow_p));
14478
14479     case TRUNC_MOD_EXPR:
14480     case CEIL_MOD_EXPR:
14481     case FLOOR_MOD_EXPR:
14482     case ROUND_MOD_EXPR:
14483       return tree_expr_nonnegative_warnv_p (op0,
14484                                             strict_overflow_p);
14485     default:
14486       return tree_simple_nonnegative_warnv_p (code, type);
14487     }
14488
14489   /* We don't know sign of `t', so be conservative and return false.  */
14490   return false;
14491 }
14492
14493 /* Return true if T is known to be non-negative.  If the return
14494    value is based on the assumption that signed overflow is undefined,
14495    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14496    *STRICT_OVERFLOW_P.  */
14497
14498 bool
14499 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14500 {
14501   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14502     return true;
14503
14504   switch (TREE_CODE (t))
14505     {
14506     case INTEGER_CST:
14507       return tree_int_cst_sgn (t) >= 0;
14508
14509     case REAL_CST:
14510       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14511
14512     case FIXED_CST:
14513       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14514
14515     case COND_EXPR:
14516       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14517                                              strict_overflow_p)
14518               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14519                                                 strict_overflow_p));
14520     default:
14521       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14522                                                    TREE_TYPE (t));
14523     }
14524   /* We don't know sign of `t', so be conservative and return false.  */
14525   return false;
14526 }
14527
14528 /* Return true if T is known to be non-negative.  If the return
14529    value is based on the assumption that signed overflow is undefined,
14530    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14531    *STRICT_OVERFLOW_P.  */
14532
14533 bool
14534 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14535                                tree arg0, tree arg1, bool *strict_overflow_p)
14536 {
14537   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14538     switch (DECL_FUNCTION_CODE (fndecl))
14539       {
14540         CASE_FLT_FN (BUILT_IN_ACOS):
14541         CASE_FLT_FN (BUILT_IN_ACOSH):
14542         CASE_FLT_FN (BUILT_IN_CABS):
14543         CASE_FLT_FN (BUILT_IN_COSH):
14544         CASE_FLT_FN (BUILT_IN_ERFC):
14545         CASE_FLT_FN (BUILT_IN_EXP):
14546         CASE_FLT_FN (BUILT_IN_EXP10):
14547         CASE_FLT_FN (BUILT_IN_EXP2):
14548         CASE_FLT_FN (BUILT_IN_FABS):
14549         CASE_FLT_FN (BUILT_IN_FDIM):
14550         CASE_FLT_FN (BUILT_IN_HYPOT):
14551         CASE_FLT_FN (BUILT_IN_POW10):
14552         CASE_INT_FN (BUILT_IN_FFS):
14553         CASE_INT_FN (BUILT_IN_PARITY):
14554         CASE_INT_FN (BUILT_IN_POPCOUNT):
14555       case BUILT_IN_BSWAP32:
14556       case BUILT_IN_BSWAP64:
14557         /* Always true.  */
14558         return true;
14559
14560         CASE_FLT_FN (BUILT_IN_SQRT):
14561         /* sqrt(-0.0) is -0.0.  */
14562         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14563           return true;
14564         return tree_expr_nonnegative_warnv_p (arg0,
14565                                               strict_overflow_p);
14566
14567         CASE_FLT_FN (BUILT_IN_ASINH):
14568         CASE_FLT_FN (BUILT_IN_ATAN):
14569         CASE_FLT_FN (BUILT_IN_ATANH):
14570         CASE_FLT_FN (BUILT_IN_CBRT):
14571         CASE_FLT_FN (BUILT_IN_CEIL):
14572         CASE_FLT_FN (BUILT_IN_ERF):
14573         CASE_FLT_FN (BUILT_IN_EXPM1):
14574         CASE_FLT_FN (BUILT_IN_FLOOR):
14575         CASE_FLT_FN (BUILT_IN_FMOD):
14576         CASE_FLT_FN (BUILT_IN_FREXP):
14577         CASE_FLT_FN (BUILT_IN_LCEIL):
14578         CASE_FLT_FN (BUILT_IN_LDEXP):
14579         CASE_FLT_FN (BUILT_IN_LFLOOR):
14580         CASE_FLT_FN (BUILT_IN_LLCEIL):
14581         CASE_FLT_FN (BUILT_IN_LLFLOOR):
14582         CASE_FLT_FN (BUILT_IN_LLRINT):
14583         CASE_FLT_FN (BUILT_IN_LLROUND):
14584         CASE_FLT_FN (BUILT_IN_LRINT):
14585         CASE_FLT_FN (BUILT_IN_LROUND):
14586         CASE_FLT_FN (BUILT_IN_MODF):
14587         CASE_FLT_FN (BUILT_IN_NEARBYINT):
14588         CASE_FLT_FN (BUILT_IN_RINT):
14589         CASE_FLT_FN (BUILT_IN_ROUND):
14590         CASE_FLT_FN (BUILT_IN_SCALB):
14591         CASE_FLT_FN (BUILT_IN_SCALBLN):
14592         CASE_FLT_FN (BUILT_IN_SCALBN):
14593         CASE_FLT_FN (BUILT_IN_SIGNBIT):
14594         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14595         CASE_FLT_FN (BUILT_IN_SINH):
14596         CASE_FLT_FN (BUILT_IN_TANH):
14597         CASE_FLT_FN (BUILT_IN_TRUNC):
14598         /* True if the 1st argument is nonnegative.  */
14599         return tree_expr_nonnegative_warnv_p (arg0,
14600                                               strict_overflow_p);
14601
14602         CASE_FLT_FN (BUILT_IN_FMAX):
14603         /* True if the 1st OR 2nd arguments are nonnegative.  */
14604         return (tree_expr_nonnegative_warnv_p (arg0,
14605                                                strict_overflow_p)
14606                 || (tree_expr_nonnegative_warnv_p (arg1,
14607                                                    strict_overflow_p)));
14608
14609         CASE_FLT_FN (BUILT_IN_FMIN):
14610         /* True if the 1st AND 2nd arguments are nonnegative.  */
14611         return (tree_expr_nonnegative_warnv_p (arg0,
14612                                                strict_overflow_p)
14613                 && (tree_expr_nonnegative_warnv_p (arg1,
14614                                                    strict_overflow_p)));
14615
14616         CASE_FLT_FN (BUILT_IN_COPYSIGN):
14617         /* True if the 2nd argument is nonnegative.  */
14618         return tree_expr_nonnegative_warnv_p (arg1,
14619                                               strict_overflow_p);
14620
14621         CASE_FLT_FN (BUILT_IN_POWI):
14622         /* True if the 1st argument is nonnegative or the second
14623            argument is an even integer.  */
14624         if (TREE_CODE (arg1) == INTEGER_CST
14625             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14626           return true;
14627         return tree_expr_nonnegative_warnv_p (arg0,
14628                                               strict_overflow_p);
14629
14630         CASE_FLT_FN (BUILT_IN_POW):
14631         /* True if the 1st argument is nonnegative or the second
14632            argument is an even integer valued real.  */
14633         if (TREE_CODE (arg1) == REAL_CST)
14634           {
14635             REAL_VALUE_TYPE c;
14636             HOST_WIDE_INT n;
14637
14638             c = TREE_REAL_CST (arg1);
14639             n = real_to_integer (&c);
14640             if ((n & 1) == 0)
14641               {
14642                 REAL_VALUE_TYPE cint;
14643                 real_from_integer (&cint, VOIDmode, n,
14644                                    n < 0 ? -1 : 0, 0);
14645                 if (real_identical (&c, &cint))
14646                   return true;
14647               }
14648           }
14649         return tree_expr_nonnegative_warnv_p (arg0,
14650                                               strict_overflow_p);
14651
14652       default:
14653         break;
14654       }
14655   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14656                                           type);
14657 }
14658
14659 /* Return true if T is known to be non-negative.  If the return
14660    value is based on the assumption that signed overflow is undefined,
14661    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14662    *STRICT_OVERFLOW_P.  */
14663
14664 bool
14665 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14666 {
14667   enum tree_code code = TREE_CODE (t);
14668   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14669     return true;
14670
14671   switch (code)
14672     {
14673     case TARGET_EXPR:
14674       {
14675         tree temp = TARGET_EXPR_SLOT (t);
14676         t = TARGET_EXPR_INITIAL (t);
14677
14678         /* If the initializer is non-void, then it's a normal expression
14679            that will be assigned to the slot.  */
14680         if (!VOID_TYPE_P (t))
14681           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14682
14683         /* Otherwise, the initializer sets the slot in some way.  One common
14684            way is an assignment statement at the end of the initializer.  */
14685         while (1)
14686           {
14687             if (TREE_CODE (t) == BIND_EXPR)
14688               t = expr_last (BIND_EXPR_BODY (t));
14689             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14690                      || TREE_CODE (t) == TRY_CATCH_EXPR)
14691               t = expr_last (TREE_OPERAND (t, 0));
14692             else if (TREE_CODE (t) == STATEMENT_LIST)
14693               t = expr_last (t);
14694             else
14695               break;
14696           }
14697         if (TREE_CODE (t) == MODIFY_EXPR
14698             && TREE_OPERAND (t, 0) == temp)
14699           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14700                                                 strict_overflow_p);
14701
14702         return false;
14703       }
14704
14705     case CALL_EXPR:
14706       {
14707         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
14708         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
14709
14710         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14711                                               get_callee_fndecl (t),
14712                                               arg0,
14713                                               arg1,
14714                                               strict_overflow_p);
14715       }
14716     case COMPOUND_EXPR:
14717     case MODIFY_EXPR:
14718       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14719                                             strict_overflow_p);
14720     case BIND_EXPR:
14721       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14722                                             strict_overflow_p);
14723     case SAVE_EXPR:
14724       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14725                                             strict_overflow_p);
14726
14727     default:
14728       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14729                                                    TREE_TYPE (t));
14730     }
14731
14732   /* We don't know sign of `t', so be conservative and return false.  */
14733   return false;
14734 }
14735
14736 /* Return true if T is known to be non-negative.  If the return
14737    value is based on the assumption that signed overflow is undefined,
14738    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14739    *STRICT_OVERFLOW_P.  */
14740
14741 bool
14742 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14743 {
14744   enum tree_code code;
14745   if (t == error_mark_node)
14746     return false;
14747
14748   code = TREE_CODE (t);
14749   switch (TREE_CODE_CLASS (code))
14750     {
14751     case tcc_binary:
14752     case tcc_comparison:
14753       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14754                                               TREE_TYPE (t),
14755                                               TREE_OPERAND (t, 0),
14756                                               TREE_OPERAND (t, 1),
14757                                               strict_overflow_p);
14758
14759     case tcc_unary:
14760       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14761                                              TREE_TYPE (t),
14762                                              TREE_OPERAND (t, 0),
14763                                              strict_overflow_p);
14764
14765     case tcc_constant:
14766     case tcc_declaration:
14767     case tcc_reference:
14768       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14769
14770     default:
14771       break;
14772     }
14773
14774   switch (code)
14775     {
14776     case TRUTH_AND_EXPR:
14777     case TRUTH_OR_EXPR:
14778     case TRUTH_XOR_EXPR:
14779       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14780                                               TREE_TYPE (t),
14781                                               TREE_OPERAND (t, 0),
14782                                               TREE_OPERAND (t, 1),
14783                                               strict_overflow_p);
14784     case TRUTH_NOT_EXPR:
14785       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14786                                              TREE_TYPE (t),
14787                                              TREE_OPERAND (t, 0),
14788                                              strict_overflow_p);
14789
14790     case COND_EXPR:
14791     case CONSTRUCTOR:
14792     case OBJ_TYPE_REF:
14793     case ASSERT_EXPR:
14794     case ADDR_EXPR:
14795     case WITH_SIZE_EXPR:
14796     case EXC_PTR_EXPR:
14797     case SSA_NAME:
14798     case FILTER_EXPR:
14799       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14800
14801     default:
14802       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14803     }
14804 }
14805
14806 /* Return true if `t' is known to be non-negative.  Handle warnings
14807    about undefined signed overflow.  */
14808
14809 bool
14810 tree_expr_nonnegative_p (tree t)
14811 {
14812   bool ret, strict_overflow_p;
14813
14814   strict_overflow_p = false;
14815   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14816   if (strict_overflow_p)
14817     fold_overflow_warning (("assuming signed overflow does not occur when "
14818                             "determining that expression is always "
14819                             "non-negative"),
14820                            WARN_STRICT_OVERFLOW_MISC);
14821   return ret;
14822 }
14823
14824
14825 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14826    For floating point we further ensure that T is not denormal.
14827    Similar logic is present in nonzero_address in rtlanal.h.
14828
14829    If the return value is based on the assumption that signed overflow
14830    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14831    change *STRICT_OVERFLOW_P.  */
14832
14833 bool
14834 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14835                                  bool *strict_overflow_p)
14836 {
14837   switch (code)
14838     {
14839     case ABS_EXPR:
14840       return tree_expr_nonzero_warnv_p (op0,
14841                                         strict_overflow_p);
14842
14843     case NOP_EXPR:
14844       {
14845         tree inner_type = TREE_TYPE (op0);
14846         tree outer_type = type;
14847
14848         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14849                 && tree_expr_nonzero_warnv_p (op0,
14850                                               strict_overflow_p));
14851       }
14852       break;
14853
14854     case NON_LVALUE_EXPR:
14855       return tree_expr_nonzero_warnv_p (op0,
14856                                         strict_overflow_p);
14857
14858     default:
14859       break;
14860   }
14861
14862   return false;
14863 }
14864
14865 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14866    For floating point we further ensure that T is not denormal.
14867    Similar logic is present in nonzero_address in rtlanal.h.
14868
14869    If the return value is based on the assumption that signed overflow
14870    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14871    change *STRICT_OVERFLOW_P.  */
14872
14873 bool
14874 tree_binary_nonzero_warnv_p (enum tree_code code,
14875                              tree type,
14876                              tree op0,
14877                              tree op1, bool *strict_overflow_p)
14878 {
14879   bool sub_strict_overflow_p;
14880   switch (code)
14881     {
14882     case POINTER_PLUS_EXPR:
14883     case PLUS_EXPR:
14884       if (TYPE_OVERFLOW_UNDEFINED (type))
14885         {
14886           /* With the presence of negative values it is hard
14887              to say something.  */
14888           sub_strict_overflow_p = false;
14889           if (!tree_expr_nonnegative_warnv_p (op0,
14890                                               &sub_strict_overflow_p)
14891               || !tree_expr_nonnegative_warnv_p (op1,
14892                                                  &sub_strict_overflow_p))
14893             return false;
14894           /* One of operands must be positive and the other non-negative.  */
14895           /* We don't set *STRICT_OVERFLOW_P here: even if this value
14896              overflows, on a twos-complement machine the sum of two
14897              nonnegative numbers can never be zero.  */
14898           return (tree_expr_nonzero_warnv_p (op0,
14899                                              strict_overflow_p)
14900                   || tree_expr_nonzero_warnv_p (op1,
14901                                                 strict_overflow_p));
14902         }
14903       break;
14904
14905     case MULT_EXPR:
14906       if (TYPE_OVERFLOW_UNDEFINED (type))
14907         {
14908           if (tree_expr_nonzero_warnv_p (op0,
14909                                          strict_overflow_p)
14910               && tree_expr_nonzero_warnv_p (op1,
14911                                             strict_overflow_p))
14912             {
14913               *strict_overflow_p = true;
14914               return true;
14915             }
14916         }
14917       break;
14918
14919     case MIN_EXPR:
14920       sub_strict_overflow_p = false;
14921       if (tree_expr_nonzero_warnv_p (op0,
14922                                      &sub_strict_overflow_p)
14923           && tree_expr_nonzero_warnv_p (op1,
14924                                         &sub_strict_overflow_p))
14925         {
14926           if (sub_strict_overflow_p)
14927             *strict_overflow_p = true;
14928         }
14929       break;
14930
14931     case MAX_EXPR:
14932       sub_strict_overflow_p = false;
14933       if (tree_expr_nonzero_warnv_p (op0,
14934                                      &sub_strict_overflow_p))
14935         {
14936           if (sub_strict_overflow_p)
14937             *strict_overflow_p = true;
14938
14939           /* When both operands are nonzero, then MAX must be too.  */
14940           if (tree_expr_nonzero_warnv_p (op1,
14941                                          strict_overflow_p))
14942             return true;
14943
14944           /* MAX where operand 0 is positive is positive.  */
14945           return tree_expr_nonnegative_warnv_p (op0,
14946                                                strict_overflow_p);
14947         }
14948       /* MAX where operand 1 is positive is positive.  */
14949       else if (tree_expr_nonzero_warnv_p (op1,
14950                                           &sub_strict_overflow_p)
14951                && tree_expr_nonnegative_warnv_p (op1,
14952                                                  &sub_strict_overflow_p))
14953         {
14954           if (sub_strict_overflow_p)
14955             *strict_overflow_p = true;
14956           return true;
14957         }
14958       break;
14959
14960     case BIT_IOR_EXPR:
14961       return (tree_expr_nonzero_warnv_p (op1,
14962                                          strict_overflow_p)
14963               || tree_expr_nonzero_warnv_p (op0,
14964                                             strict_overflow_p));
14965
14966     default:
14967       break;
14968   }
14969
14970   return false;
14971 }
14972
14973 /* Return true when T is an address and is known to be nonzero.
14974    For floating point we further ensure that T is not denormal.
14975    Similar logic is present in nonzero_address in rtlanal.h.
14976
14977    If the return value is based on the assumption that signed overflow
14978    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14979    change *STRICT_OVERFLOW_P.  */
14980
14981 bool
14982 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14983 {
14984   bool sub_strict_overflow_p;
14985   switch (TREE_CODE (t))
14986     {
14987     case INTEGER_CST:
14988       return !integer_zerop (t);
14989
14990     case ADDR_EXPR:
14991       {
14992         tree base = get_base_address (TREE_OPERAND (t, 0));
14993
14994         if (!base)
14995           return false;
14996
14997         /* Weak declarations may link to NULL.  */
14998         if (VAR_OR_FUNCTION_DECL_P (base))
14999           return !DECL_WEAK (base);
15000
15001         /* Constants are never weak.  */
15002         if (CONSTANT_CLASS_P (base))
15003           return true;
15004
15005         return false;
15006       }
15007
15008     case COND_EXPR:
15009       sub_strict_overflow_p = false;
15010       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15011                                      &sub_strict_overflow_p)
15012           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15013                                         &sub_strict_overflow_p))
15014         {
15015           if (sub_strict_overflow_p)
15016             *strict_overflow_p = true;
15017           return true;
15018         }
15019       break;
15020
15021     default:
15022       break;
15023     }
15024   return false;
15025 }
15026
15027 /* Return true when T is an address and is known to be nonzero.
15028    For floating point we further ensure that T is not denormal.
15029    Similar logic is present in nonzero_address in rtlanal.h.
15030
15031    If the return value is based on the assumption that signed overflow
15032    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15033    change *STRICT_OVERFLOW_P.  */
15034
15035 bool
15036 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15037 {
15038   tree type = TREE_TYPE (t);
15039   enum tree_code code;
15040
15041   /* Doing something useful for floating point would need more work.  */
15042   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15043     return false;
15044
15045   code = TREE_CODE (t);
15046   switch (TREE_CODE_CLASS (code))
15047     {
15048     case tcc_unary:
15049       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15050                                               strict_overflow_p);
15051     case tcc_binary:
15052     case tcc_comparison:
15053       return tree_binary_nonzero_warnv_p (code, type,
15054                                                TREE_OPERAND (t, 0),
15055                                                TREE_OPERAND (t, 1),
15056                                                strict_overflow_p);
15057     case tcc_constant:
15058     case tcc_declaration:
15059     case tcc_reference:
15060       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15061
15062     default:
15063       break;
15064     }
15065
15066   switch (code)
15067     {
15068     case TRUTH_NOT_EXPR:
15069       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15070                                               strict_overflow_p);
15071
15072     case TRUTH_AND_EXPR:
15073     case TRUTH_OR_EXPR:
15074     case TRUTH_XOR_EXPR:
15075       return tree_binary_nonzero_warnv_p (code, type,
15076                                                TREE_OPERAND (t, 0),
15077                                                TREE_OPERAND (t, 1),
15078                                                strict_overflow_p);
15079
15080     case COND_EXPR:
15081     case CONSTRUCTOR:
15082     case OBJ_TYPE_REF:
15083     case ASSERT_EXPR:
15084     case ADDR_EXPR:
15085     case WITH_SIZE_EXPR:
15086     case EXC_PTR_EXPR:
15087     case SSA_NAME:
15088     case FILTER_EXPR:
15089       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15090
15091     case COMPOUND_EXPR:
15092     case MODIFY_EXPR:
15093     case BIND_EXPR:
15094       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15095                                         strict_overflow_p);
15096
15097     case SAVE_EXPR:
15098       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15099                                         strict_overflow_p);
15100
15101     case CALL_EXPR:
15102       return alloca_call_p (t);
15103
15104     default:
15105       break;
15106     }
15107   return false;
15108 }
15109
15110 /* Return true when T is an address and is known to be nonzero.
15111    Handle warnings about undefined signed overflow.  */
15112
15113 bool
15114 tree_expr_nonzero_p (tree t)
15115 {
15116   bool ret, strict_overflow_p;
15117
15118   strict_overflow_p = false;
15119   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15120   if (strict_overflow_p)
15121     fold_overflow_warning (("assuming signed overflow does not occur when "
15122                             "determining that expression is always "
15123                             "non-zero"),
15124                            WARN_STRICT_OVERFLOW_MISC);
15125   return ret;
15126 }
15127
15128 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15129    attempt to fold the expression to a constant without modifying TYPE,
15130    OP0 or OP1.
15131
15132    If the expression could be simplified to a constant, then return
15133    the constant.  If the expression would not be simplified to a
15134    constant, then return NULL_TREE.  */
15135
15136 tree
15137 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15138 {
15139   tree tem = fold_binary (code, type, op0, op1);
15140   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15141 }
15142
15143 /* Given the components of a unary expression CODE, TYPE and OP0,
15144    attempt to fold the expression to a constant without modifying
15145    TYPE or OP0.
15146
15147    If the expression could be simplified to a constant, then return
15148    the constant.  If the expression would not be simplified to a
15149    constant, then return NULL_TREE.  */
15150
15151 tree
15152 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15153 {
15154   tree tem = fold_unary (code, type, op0);
15155   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15156 }
15157
15158 /* If EXP represents referencing an element in a constant string
15159    (either via pointer arithmetic or array indexing), return the
15160    tree representing the value accessed, otherwise return NULL.  */
15161
15162 tree
15163 fold_read_from_constant_string (tree exp)
15164 {
15165   if ((TREE_CODE (exp) == INDIRECT_REF
15166        || TREE_CODE (exp) == ARRAY_REF)
15167       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15168     {
15169       tree exp1 = TREE_OPERAND (exp, 0);
15170       tree index;
15171       tree string;
15172
15173       if (TREE_CODE (exp) == INDIRECT_REF)
15174         string = string_constant (exp1, &index);
15175       else
15176         {
15177           tree low_bound = array_ref_low_bound (exp);
15178           index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
15179
15180           /* Optimize the special-case of a zero lower bound.
15181
15182              We convert the low_bound to sizetype to avoid some problems
15183              with constant folding.  (E.g. suppose the lower bound is 1,
15184              and its mode is QI.  Without the conversion,l (ARRAY
15185              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15186              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15187           if (! integer_zerop (low_bound))
15188             index = size_diffop (index, fold_convert (sizetype, low_bound));
15189
15190           string = exp1;
15191         }
15192
15193       if (string
15194           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15195           && TREE_CODE (string) == STRING_CST
15196           && TREE_CODE (index) == INTEGER_CST
15197           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15198           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15199               == MODE_INT)
15200           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15201         return build_int_cst_type (TREE_TYPE (exp),
15202                                    (TREE_STRING_POINTER (string)
15203                                     [TREE_INT_CST_LOW (index)]));
15204     }
15205   return NULL;
15206 }
15207
15208 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15209    an integer constant, real, or fixed-point constant.
15210
15211    TYPE is the type of the result.  */
15212
15213 static tree
15214 fold_negate_const (tree arg0, tree type)
15215 {
15216   tree t = NULL_TREE;
15217
15218   switch (TREE_CODE (arg0))
15219     {
15220     case INTEGER_CST:
15221       {
15222         unsigned HOST_WIDE_INT low;
15223         HOST_WIDE_INT high;
15224         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15225                                    TREE_INT_CST_HIGH (arg0),
15226                                    &low, &high);
15227         t = force_fit_type_double (type, low, high, 1,
15228                                    (overflow | TREE_OVERFLOW (arg0))
15229                                    && !TYPE_UNSIGNED (type));
15230         break;
15231       }
15232
15233     case REAL_CST:
15234       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15235       break;
15236
15237     case FIXED_CST:
15238       {
15239         FIXED_VALUE_TYPE f;
15240         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15241                                             &(TREE_FIXED_CST (arg0)), NULL,
15242                                             TYPE_SATURATING (type));
15243         t = build_fixed (type, f);
15244         /* Propagate overflow flags.  */
15245         if (overflow_p | TREE_OVERFLOW (arg0))
15246           {
15247             TREE_OVERFLOW (t) = 1;
15248             TREE_CONSTANT_OVERFLOW (t) = 1;
15249           }
15250         else if (TREE_CONSTANT_OVERFLOW (arg0))
15251           TREE_CONSTANT_OVERFLOW (t) = 1;
15252         break;
15253       }
15254
15255     default:
15256       gcc_unreachable ();
15257     }
15258
15259   return t;
15260 }
15261
15262 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15263    an integer constant or real constant.
15264
15265    TYPE is the type of the result.  */
15266
15267 tree
15268 fold_abs_const (tree arg0, tree type)
15269 {
15270   tree t = NULL_TREE;
15271
15272   switch (TREE_CODE (arg0))
15273     {
15274     case INTEGER_CST:
15275       /* If the value is unsigned, then the absolute value is
15276          the same as the ordinary value.  */
15277       if (TYPE_UNSIGNED (type))
15278         t = arg0;
15279       /* Similarly, if the value is non-negative.  */
15280       else if (INT_CST_LT (integer_minus_one_node, arg0))
15281         t = arg0;
15282       /* If the value is negative, then the absolute value is
15283          its negation.  */
15284       else
15285         {
15286           unsigned HOST_WIDE_INT low;
15287           HOST_WIDE_INT high;
15288           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15289                                      TREE_INT_CST_HIGH (arg0),
15290                                      &low, &high);
15291           t = force_fit_type_double (type, low, high, -1,
15292                                      overflow | TREE_OVERFLOW (arg0));
15293         }
15294       break;
15295
15296     case REAL_CST:
15297       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15298         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15299       else
15300         t =  arg0;
15301       break;
15302
15303     default:
15304       gcc_unreachable ();
15305     }
15306
15307   return t;
15308 }
15309
15310 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15311    constant.  TYPE is the type of the result.  */
15312
15313 static tree
15314 fold_not_const (tree arg0, tree type)
15315 {
15316   tree t = NULL_TREE;
15317
15318   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15319
15320   t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15321                              ~TREE_INT_CST_HIGH (arg0), 0,
15322                              TREE_OVERFLOW (arg0));
15323
15324   return t;
15325 }
15326
15327 /* Given CODE, a relational operator, the target type, TYPE and two
15328    constant operands OP0 and OP1, return the result of the
15329    relational operation.  If the result is not a compile time
15330    constant, then return NULL_TREE.  */
15331
15332 static tree
15333 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15334 {
15335   int result, invert;
15336
15337   /* From here on, the only cases we handle are when the result is
15338      known to be a constant.  */
15339
15340   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15341     {
15342       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15343       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15344
15345       /* Handle the cases where either operand is a NaN.  */
15346       if (real_isnan (c0) || real_isnan (c1))
15347         {
15348           switch (code)
15349             {
15350             case EQ_EXPR:
15351             case ORDERED_EXPR:
15352               result = 0;
15353               break;
15354
15355             case NE_EXPR:
15356             case UNORDERED_EXPR:
15357             case UNLT_EXPR:
15358             case UNLE_EXPR:
15359             case UNGT_EXPR:
15360             case UNGE_EXPR:
15361             case UNEQ_EXPR:
15362               result = 1;
15363               break;
15364
15365             case LT_EXPR:
15366             case LE_EXPR:
15367             case GT_EXPR:
15368             case GE_EXPR:
15369             case LTGT_EXPR:
15370               if (flag_trapping_math)
15371                 return NULL_TREE;
15372               result = 0;
15373               break;
15374
15375             default:
15376               gcc_unreachable ();
15377             }
15378
15379           return constant_boolean_node (result, type);
15380         }
15381
15382       return constant_boolean_node (real_compare (code, c0, c1), type);
15383     }
15384
15385   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15386     {
15387       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15388       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15389       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15390     }
15391
15392   /* Handle equality/inequality of complex constants.  */
15393   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15394     {
15395       tree rcond = fold_relational_const (code, type,
15396                                           TREE_REALPART (op0),
15397                                           TREE_REALPART (op1));
15398       tree icond = fold_relational_const (code, type,
15399                                           TREE_IMAGPART (op0),
15400                                           TREE_IMAGPART (op1));
15401       if (code == EQ_EXPR)
15402         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15403       else if (code == NE_EXPR)
15404         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15405       else
15406         return NULL_TREE;
15407     }
15408
15409   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15410
15411      To compute GT, swap the arguments and do LT.
15412      To compute GE, do LT and invert the result.
15413      To compute LE, swap the arguments, do LT and invert the result.
15414      To compute NE, do EQ and invert the result.
15415
15416      Therefore, the code below must handle only EQ and LT.  */
15417
15418   if (code == LE_EXPR || code == GT_EXPR)
15419     {
15420       tree tem = op0;
15421       op0 = op1;
15422       op1 = tem;
15423       code = swap_tree_comparison (code);
15424     }
15425
15426   /* Note that it is safe to invert for real values here because we
15427      have already handled the one case that it matters.  */
15428
15429   invert = 0;
15430   if (code == NE_EXPR || code == GE_EXPR)
15431     {
15432       invert = 1;
15433       code = invert_tree_comparison (code, false);
15434     }
15435
15436   /* Compute a result for LT or EQ if args permit;
15437      Otherwise return T.  */
15438   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15439     {
15440       if (code == EQ_EXPR)
15441         result = tree_int_cst_equal (op0, op1);
15442       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15443         result = INT_CST_LT_UNSIGNED (op0, op1);
15444       else
15445         result = INT_CST_LT (op0, op1);
15446     }
15447   else
15448     return NULL_TREE;
15449
15450   if (invert)
15451     result ^= 1;
15452   return constant_boolean_node (result, type);
15453 }
15454
15455 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15456    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15457    itself.  */
15458
15459 tree
15460 fold_build_cleanup_point_expr (tree type, tree expr)
15461 {
15462   /* If the expression does not have side effects then we don't have to wrap
15463      it with a cleanup point expression.  */
15464   if (!TREE_SIDE_EFFECTS (expr))
15465     return expr;
15466
15467   /* If the expression is a return, check to see if the expression inside the
15468      return has no side effects or the right hand side of the modify expression
15469      inside the return. If either don't have side effects set we don't need to
15470      wrap the expression in a cleanup point expression.  Note we don't check the
15471      left hand side of the modify because it should always be a return decl.  */
15472   if (TREE_CODE (expr) == RETURN_EXPR)
15473     {
15474       tree op = TREE_OPERAND (expr, 0);
15475       if (!op || !TREE_SIDE_EFFECTS (op))
15476         return expr;
15477       op = TREE_OPERAND (op, 1);
15478       if (!TREE_SIDE_EFFECTS (op))
15479         return expr;
15480     }
15481   
15482   return build1 (CLEANUP_POINT_EXPR, type, expr);
15483 }
15484
15485 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15486    of an indirection through OP0, or NULL_TREE if no simplification is
15487    possible.  */
15488
15489 tree
15490 fold_indirect_ref_1 (tree type, tree op0)
15491 {
15492   tree sub = op0;
15493   tree subtype;
15494
15495   STRIP_NOPS (sub);
15496   subtype = TREE_TYPE (sub);
15497   if (!POINTER_TYPE_P (subtype))
15498     return NULL_TREE;
15499
15500   if (TREE_CODE (sub) == ADDR_EXPR)
15501     {
15502       tree op = TREE_OPERAND (sub, 0);
15503       tree optype = TREE_TYPE (op);
15504       /* *&CONST_DECL -> to the value of the const decl.  */
15505       if (TREE_CODE (op) == CONST_DECL)
15506         return DECL_INITIAL (op);
15507       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
15508       if (type == optype)
15509         {
15510           tree fop = fold_read_from_constant_string (op);
15511           if (fop)
15512             return fop;
15513           else
15514             return op;
15515         }
15516       /* *(foo *)&fooarray => fooarray[0] */
15517       else if (TREE_CODE (optype) == ARRAY_TYPE
15518                && type == TREE_TYPE (optype))
15519         {
15520           tree type_domain = TYPE_DOMAIN (optype);
15521           tree min_val = size_zero_node;
15522           if (type_domain && TYPE_MIN_VALUE (type_domain))
15523             min_val = TYPE_MIN_VALUE (type_domain);
15524           return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15525         }
15526       /* *(foo *)&complexfoo => __real__ complexfoo */
15527       else if (TREE_CODE (optype) == COMPLEX_TYPE
15528                && type == TREE_TYPE (optype))
15529         return fold_build1 (REALPART_EXPR, type, op);
15530       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15531       else if (TREE_CODE (optype) == VECTOR_TYPE
15532                && type == TREE_TYPE (optype))
15533         {
15534           tree part_width = TYPE_SIZE (type);
15535           tree index = bitsize_int (0);
15536           return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
15537         }
15538     }
15539
15540   /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15541   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15542       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15543     { 
15544       tree op00 = TREE_OPERAND (sub, 0);
15545       tree op01 = TREE_OPERAND (sub, 1);
15546       tree op00type;
15547       
15548       STRIP_NOPS (op00);
15549       op00type = TREE_TYPE (op00);
15550       if (TREE_CODE (op00) == ADDR_EXPR
15551           && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15552           && type == TREE_TYPE (TREE_TYPE (op00type)))
15553         { 
15554           HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15555           tree part_width = TYPE_SIZE (type);
15556           unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15557           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15558           tree index = bitsize_int (indexi);
15559
15560           if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15561             return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15562                                 part_width, index);
15563         
15564         }
15565     }
15566
15567
15568   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15569   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15570       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15571     {
15572       tree op00 = TREE_OPERAND (sub, 0);
15573       tree op01 = TREE_OPERAND (sub, 1);
15574       tree op00type;
15575
15576       STRIP_NOPS (op00);
15577       op00type = TREE_TYPE (op00);
15578       if (TREE_CODE (op00) == ADDR_EXPR
15579           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15580           && type == TREE_TYPE (TREE_TYPE (op00type)))
15581         {
15582           tree size = TYPE_SIZE_UNIT (type);
15583           if (tree_int_cst_equal (size, op01))
15584             return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
15585         }
15586     }
15587   
15588   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15589   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15590       && type == TREE_TYPE (TREE_TYPE (subtype)))
15591     {
15592       tree type_domain;
15593       tree min_val = size_zero_node;
15594       sub = build_fold_indirect_ref (sub);
15595       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15596       if (type_domain && TYPE_MIN_VALUE (type_domain))
15597         min_val = TYPE_MIN_VALUE (type_domain);
15598       return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15599     }
15600
15601   return NULL_TREE;
15602 }
15603
15604 /* Builds an expression for an indirection through T, simplifying some
15605    cases.  */
15606
15607 tree
15608 build_fold_indirect_ref (tree t)
15609 {
15610   tree type = TREE_TYPE (TREE_TYPE (t));
15611   tree sub = fold_indirect_ref_1 (type, t);
15612
15613   if (sub)
15614     return sub;
15615   else
15616     return build1 (INDIRECT_REF, type, t);
15617 }
15618
15619 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
15620
15621 tree
15622 fold_indirect_ref (tree t)
15623 {
15624   tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
15625
15626   if (sub)
15627     return sub;
15628   else
15629     return t;
15630 }
15631
15632 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15633    whose result is ignored.  The type of the returned tree need not be
15634    the same as the original expression.  */
15635
15636 tree
15637 fold_ignored_result (tree t)
15638 {
15639   if (!TREE_SIDE_EFFECTS (t))
15640     return integer_zero_node;
15641
15642   for (;;)
15643     switch (TREE_CODE_CLASS (TREE_CODE (t)))
15644       {
15645       case tcc_unary:
15646         t = TREE_OPERAND (t, 0);
15647         break;
15648
15649       case tcc_binary:
15650       case tcc_comparison:
15651         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15652           t = TREE_OPERAND (t, 0);
15653         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15654           t = TREE_OPERAND (t, 1);
15655         else
15656           return t;
15657         break;
15658
15659       case tcc_expression:
15660         switch (TREE_CODE (t))
15661           {
15662           case COMPOUND_EXPR:
15663             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15664               return t;
15665             t = TREE_OPERAND (t, 0);
15666             break;
15667
15668           case COND_EXPR:
15669             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15670                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15671               return t;
15672             t = TREE_OPERAND (t, 0);
15673             break;
15674
15675           default:
15676             return t;
15677           }
15678         break;
15679
15680       default:
15681         return t;
15682       }
15683 }
15684
15685 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15686    This can only be applied to objects of a sizetype.  */
15687
15688 tree
15689 round_up (tree value, int divisor)
15690 {
15691   tree div = NULL_TREE;
15692
15693   gcc_assert (divisor > 0);
15694   if (divisor == 1)
15695     return value;
15696
15697   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15698      have to do anything.  Only do this when we are not given a const,
15699      because in that case, this check is more expensive than just
15700      doing it.  */
15701   if (TREE_CODE (value) != INTEGER_CST)
15702     {
15703       div = build_int_cst (TREE_TYPE (value), divisor);
15704
15705       if (multiple_of_p (TREE_TYPE (value), value, div))
15706         return value;
15707     }
15708
15709   /* If divisor is a power of two, simplify this to bit manipulation.  */
15710   if (divisor == (divisor & -divisor))
15711     {
15712       if (TREE_CODE (value) == INTEGER_CST)
15713         {
15714           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15715           unsigned HOST_WIDE_INT high;
15716           bool overflow_p;
15717
15718           if ((low & (divisor - 1)) == 0)
15719             return value;
15720
15721           overflow_p = TREE_OVERFLOW (value);
15722           high = TREE_INT_CST_HIGH (value);
15723           low &= ~(divisor - 1);
15724           low += divisor;
15725           if (low == 0)
15726             {
15727               high++;
15728               if (high == 0)
15729                 overflow_p = true;
15730             }
15731
15732           return force_fit_type_double (TREE_TYPE (value), low, high,
15733                                         -1, overflow_p);
15734         }
15735       else
15736         {
15737           tree t;
15738
15739           t = build_int_cst (TREE_TYPE (value), divisor - 1);
15740           value = size_binop (PLUS_EXPR, value, t);
15741           t = build_int_cst (TREE_TYPE (value), -divisor);
15742           value = size_binop (BIT_AND_EXPR, value, t);
15743         }
15744     }
15745   else
15746     {
15747       if (!div)
15748         div = build_int_cst (TREE_TYPE (value), divisor);
15749       value = size_binop (CEIL_DIV_EXPR, value, div);
15750       value = size_binop (MULT_EXPR, value, div);
15751     }
15752
15753   return value;
15754 }
15755
15756 /* Likewise, but round down.  */
15757
15758 tree
15759 round_down (tree value, int divisor)
15760 {
15761   tree div = NULL_TREE;
15762
15763   gcc_assert (divisor > 0);
15764   if (divisor == 1)
15765     return value;
15766
15767   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15768      have to do anything.  Only do this when we are not given a const,
15769      because in that case, this check is more expensive than just
15770      doing it.  */
15771   if (TREE_CODE (value) != INTEGER_CST)
15772     {
15773       div = build_int_cst (TREE_TYPE (value), divisor);
15774
15775       if (multiple_of_p (TREE_TYPE (value), value, div))
15776         return value;
15777     }
15778
15779   /* If divisor is a power of two, simplify this to bit manipulation.  */
15780   if (divisor == (divisor & -divisor))
15781     {
15782       tree t;
15783
15784       t = build_int_cst (TREE_TYPE (value), -divisor);
15785       value = size_binop (BIT_AND_EXPR, value, t);
15786     }
15787   else
15788     {
15789       if (!div)
15790         div = build_int_cst (TREE_TYPE (value), divisor);
15791       value = size_binop (FLOOR_DIV_EXPR, value, div);
15792       value = size_binop (MULT_EXPR, value, div);
15793     }
15794
15795   return value;
15796 }
15797
15798 /* Returns the pointer to the base of the object addressed by EXP and
15799    extracts the information about the offset of the access, storing it
15800    to PBITPOS and POFFSET.  */
15801
15802 static tree
15803 split_address_to_core_and_offset (tree exp,
15804                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15805 {
15806   tree core;
15807   enum machine_mode mode;
15808   int unsignedp, volatilep;
15809   HOST_WIDE_INT bitsize;
15810
15811   if (TREE_CODE (exp) == ADDR_EXPR)
15812     {
15813       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15814                                   poffset, &mode, &unsignedp, &volatilep,
15815                                   false);
15816       core = fold_addr_expr (core);
15817     }
15818   else
15819     {
15820       core = exp;
15821       *pbitpos = 0;
15822       *poffset = NULL_TREE;
15823     }
15824
15825   return core;
15826 }
15827
15828 /* Returns true if addresses of E1 and E2 differ by a constant, false
15829    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
15830
15831 bool
15832 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15833 {
15834   tree core1, core2;
15835   HOST_WIDE_INT bitpos1, bitpos2;
15836   tree toffset1, toffset2, tdiff, type;
15837
15838   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15839   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15840
15841   if (bitpos1 % BITS_PER_UNIT != 0
15842       || bitpos2 % BITS_PER_UNIT != 0
15843       || !operand_equal_p (core1, core2, 0))
15844     return false;
15845
15846   if (toffset1 && toffset2)
15847     {
15848       type = TREE_TYPE (toffset1);
15849       if (type != TREE_TYPE (toffset2))
15850         toffset2 = fold_convert (type, toffset2);
15851
15852       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15853       if (!cst_and_fits_in_hwi (tdiff))
15854         return false;
15855
15856       *diff = int_cst_value (tdiff);
15857     }
15858   else if (toffset1 || toffset2)
15859     {
15860       /* If only one of the offsets is non-constant, the difference cannot
15861          be a constant.  */
15862       return false;
15863     }
15864   else
15865     *diff = 0;
15866
15867   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15868   return true;
15869 }
15870
15871 /* Simplify the floating point expression EXP when the sign of the
15872    result is not significant.  Return NULL_TREE if no simplification
15873    is possible.  */
15874
15875 tree
15876 fold_strip_sign_ops (tree exp)
15877 {
15878   tree arg0, arg1;
15879
15880   switch (TREE_CODE (exp))
15881     {
15882     case ABS_EXPR:
15883     case NEGATE_EXPR:
15884       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15885       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15886
15887     case MULT_EXPR:
15888     case RDIV_EXPR:
15889       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15890         return NULL_TREE;
15891       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15892       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15893       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15894         return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15895                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
15896                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
15897       break;
15898
15899     case COMPOUND_EXPR:
15900       arg0 = TREE_OPERAND (exp, 0);
15901       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15902       if (arg1)
15903         return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15904       break;
15905       
15906     case COND_EXPR:
15907       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15908       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15909       if (arg0 || arg1)
15910         return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15911                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
15912                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
15913       break;
15914       
15915     case CALL_EXPR:
15916       {
15917         const enum built_in_function fcode = builtin_mathfn_code (exp);
15918         switch (fcode)
15919         {
15920         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15921           /* Strip copysign function call, return the 1st argument. */
15922           arg0 = CALL_EXPR_ARG (exp, 0);
15923           arg1 = CALL_EXPR_ARG (exp, 1);
15924           return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15925
15926         default:
15927           /* Strip sign ops from the argument of "odd" math functions.  */
15928           if (negate_mathfn_p (fcode))
15929             {
15930               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15931               if (arg0)
15932                 return build_call_expr (get_callee_fndecl (exp), 1, arg0);
15933             }
15934           break;
15935         }
15936       }
15937       break;
15938
15939     default:
15940       break;
15941     }
15942   return NULL_TREE;
15943 }