OSDN Git Service

PR c/42721
[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, 2009
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 (location_t, 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 int operand_equal_for_comparison_p (tree, tree, tree);
107 static int twoval_comparison_p (tree, tree *, tree *, int *);
108 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
109 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
110 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
111 static tree make_bit_field_ref (location_t, tree, tree,
112                                 HOST_WIDE_INT, HOST_WIDE_INT, int);
113 static tree optimize_bit_field_compare (location_t, enum tree_code,
114                                         tree, tree, tree);
115 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
116                                     HOST_WIDE_INT *,
117                                     enum machine_mode *, int *, int *,
118                                     tree *, tree *);
119 static int all_ones_mask_p (const_tree, int);
120 static tree sign_bit_p (tree, const_tree);
121 static int simple_operand_p (const_tree);
122 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
123 static tree range_predecessor (tree);
124 static tree range_successor (tree);
125 extern tree make_range (tree, int *, tree *, tree *, bool *);
126 extern bool merge_ranges (int *, tree *, tree *, int, tree, tree, int,
127                           tree, tree);
128 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
129 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
130 static tree unextend (tree, int, int, tree);
131 static tree fold_truthop (location_t, enum tree_code, tree, tree, tree);
132 static tree optimize_minmax_comparison (location_t, enum tree_code,
133                                         tree, tree, tree);
134 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
135 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
136 static tree fold_binary_op_with_conditional_arg (location_t,
137                                                  enum tree_code, tree,
138                                                  tree, tree,
139                                                  tree, tree, int);
140 static tree fold_mathfn_compare (location_t,
141                                  enum built_in_function, enum tree_code,
142                                  tree, tree, tree);
143 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
144 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
145 static bool reorder_operands_p (const_tree, const_tree);
146 static tree fold_negate_const (tree, tree);
147 static tree fold_not_const (tree, tree);
148 static tree fold_relational_const (enum tree_code, tree, tree, tree);
149 static tree fold_convert_const (enum tree_code, tree, tree);
150
151
152 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
153    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
154    and SUM1.  Then this yields nonzero if overflow occurred during the
155    addition.
156
157    Overflow occurs if A and B have the same sign, but A and SUM differ in
158    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
159    sign.  */
160 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
161 \f
162 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
163    We do that by representing the two-word integer in 4 words, with only
164    HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
165    number.  The value of the word is LOWPART + HIGHPART * BASE.  */
166
167 #define LOWPART(x) \
168   ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
169 #define HIGHPART(x) \
170   ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
171 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
172
173 /* Unpack a two-word integer into 4 words.
174    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
175    WORDS points to the array of HOST_WIDE_INTs.  */
176
177 static void
178 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
179 {
180   words[0] = LOWPART (low);
181   words[1] = HIGHPART (low);
182   words[2] = LOWPART (hi);
183   words[3] = HIGHPART (hi);
184 }
185
186 /* Pack an array of 4 words into a two-word integer.
187    WORDS points to the array of words.
188    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
189
190 static void
191 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
192         HOST_WIDE_INT *hi)
193 {
194   *low = words[0] + words[1] * BASE;
195   *hi = words[2] + words[3] * BASE;
196 }
197 \f
198 /* Force the double-word integer L1, H1 to be within the range of the
199    integer type TYPE.  Stores the properly truncated and sign-extended
200    double-word integer in *LV, *HV.  Returns true if the operation
201    overflows, that is, argument and result are different.  */
202
203 int
204 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
205                  unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
206 {
207   unsigned HOST_WIDE_INT low0 = l1;
208   HOST_WIDE_INT high0 = h1;
209   unsigned int prec = TYPE_PRECISION (type);
210   int sign_extended_type;
211
212   /* Size types *are* sign extended.  */
213   sign_extended_type = (!TYPE_UNSIGNED (type)
214                         || (TREE_CODE (type) == INTEGER_TYPE
215                             && TYPE_IS_SIZETYPE (type)));
216
217   /* First clear all bits that are beyond the type's precision.  */
218   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
219     ;
220   else if (prec > HOST_BITS_PER_WIDE_INT)
221     h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
222   else
223     {
224       h1 = 0;
225       if (prec < HOST_BITS_PER_WIDE_INT)
226         l1 &= ~((HOST_WIDE_INT) (-1) << prec);
227     }
228
229   /* Then do sign extension if necessary.  */
230   if (!sign_extended_type)
231     /* No sign extension */;
232   else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
233     /* Correct width already.  */;
234   else if (prec > HOST_BITS_PER_WIDE_INT)
235     {
236       /* Sign extend top half? */
237       if (h1 & ((unsigned HOST_WIDE_INT)1
238                 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
239         h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
240     }
241   else if (prec == HOST_BITS_PER_WIDE_INT)
242     {
243       if ((HOST_WIDE_INT)l1 < 0)
244         h1 = -1;
245     }
246   else
247     {
248       /* Sign extend bottom half? */
249       if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
250         {
251           h1 = -1;
252           l1 |= (HOST_WIDE_INT)(-1) << prec;
253         }
254     }
255
256   *lv = l1;
257   *hv = h1;
258
259   /* If the value didn't fit, signal overflow.  */
260   return l1 != low0 || h1 != high0;
261 }
262
263 /* We force the double-int HIGH:LOW to the range of the type TYPE by
264    sign or zero extending it.
265    OVERFLOWABLE indicates if we are interested
266    in overflow of the value, when >0 we are only interested in signed
267    overflow, for <0 we are interested in any overflow.  OVERFLOWED
268    indicates whether overflow has already occurred.  CONST_OVERFLOWED
269    indicates whether constant overflow has already occurred.  We force
270    T's value to be within range of T's type (by setting to 0 or 1 all
271    the bits outside the type's range).  We set TREE_OVERFLOWED if,
272         OVERFLOWED is nonzero,
273         or OVERFLOWABLE is >0 and signed overflow occurs
274         or OVERFLOWABLE is <0 and any overflow occurs
275    We return a new tree node for the extended double-int.  The node
276    is shared if no overflow flags are set.  */
277
278 tree
279 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
280                        HOST_WIDE_INT high, int overflowable,
281                        bool overflowed)
282 {
283   int sign_extended_type;
284   bool overflow;
285
286   /* Size types *are* sign extended.  */
287   sign_extended_type = (!TYPE_UNSIGNED (type)
288                         || (TREE_CODE (type) == INTEGER_TYPE
289                             && TYPE_IS_SIZETYPE (type)));
290
291   overflow = fit_double_type (low, high, &low, &high, type);
292
293   /* If we need to set overflow flags, return a new unshared node.  */
294   if (overflowed || overflow)
295     {
296       if (overflowed
297           || overflowable < 0
298           || (overflowable > 0 && sign_extended_type))
299         {
300           tree t = make_node (INTEGER_CST);
301           TREE_INT_CST_LOW (t) = low;
302           TREE_INT_CST_HIGH (t) = high;
303           TREE_TYPE (t) = type;
304           TREE_OVERFLOW (t) = 1;
305           return t;
306         }
307     }
308
309   /* Else build a shared node.  */
310   return build_int_cst_wide (type, low, high);
311 }
312 \f
313 /* Add two doubleword integers with doubleword result.
314    Return nonzero if the operation overflows according to UNSIGNED_P.
315    Each argument is given as two `HOST_WIDE_INT' pieces.
316    One argument is L1 and H1; the other, L2 and H2.
317    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
318
319 int
320 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
321                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
322                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
323                       bool unsigned_p)
324 {
325   unsigned HOST_WIDE_INT l;
326   HOST_WIDE_INT h;
327
328   l = l1 + l2;
329   h = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) h1
330                        + (unsigned HOST_WIDE_INT) h2
331                        + (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             || (h == h1
339                 && l < l1));
340   else
341     return OVERFLOW_SUM_SIGN (h1, h2, h);
342 }
343
344 /* Negate a doubleword integer with doubleword result.
345    Return nonzero if the operation overflows, assuming it's signed.
346    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
347    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
348
349 int
350 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
351             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
352 {
353   if (l1 == 0)
354     {
355       *lv = 0;
356       *hv = - h1;
357       return (*hv & h1) < 0;
358     }
359   else
360     {
361       *lv = -l1;
362       *hv = ~h1;
363       return 0;
364     }
365 }
366 \f
367 /* Multiply two doubleword integers with doubleword result.
368    Return nonzero if the operation overflows according to UNSIGNED_P.
369    Each argument is given as two `HOST_WIDE_INT' pieces.
370    One argument is L1 and H1; the other, L2 and H2.
371    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
372
373 int
374 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
375                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
376                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
377                       bool unsigned_p)
378 {
379   HOST_WIDE_INT arg1[4];
380   HOST_WIDE_INT arg2[4];
381   HOST_WIDE_INT prod[4 * 2];
382   unsigned HOST_WIDE_INT carry;
383   int i, j, k;
384   unsigned HOST_WIDE_INT toplow, neglow;
385   HOST_WIDE_INT tophigh, neghigh;
386
387   encode (arg1, l1, h1);
388   encode (arg2, l2, h2);
389
390   memset (prod, 0, sizeof prod);
391
392   for (i = 0; i < 4; i++)
393     {
394       carry = 0;
395       for (j = 0; j < 4; j++)
396         {
397           k = i + j;
398           /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
399           carry += arg1[i] * arg2[j];
400           /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
401           carry += prod[k];
402           prod[k] = LOWPART (carry);
403           carry = HIGHPART (carry);
404         }
405       prod[i + 4] = carry;
406     }
407
408   decode (prod, lv, hv);
409   decode (prod + 4, &toplow, &tophigh);
410
411   /* Unsigned overflow is immediate.  */
412   if (unsigned_p)
413     return (toplow | tophigh) != 0;
414
415   /* Check for signed overflow by calculating the signed representation of the
416      top half of the result; it should agree with the low half's sign bit.  */
417   if (h1 < 0)
418     {
419       neg_double (l2, h2, &neglow, &neghigh);
420       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
421     }
422   if (h2 < 0)
423     {
424       neg_double (l1, h1, &neglow, &neghigh);
425       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
426     }
427   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
428 }
429 \f
430 /* Shift the doubleword integer in L1, H1 left by COUNT places
431    keeping only PREC bits of result.
432    Shift right if COUNT is negative.
433    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
434    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
435
436 void
437 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
438                HOST_WIDE_INT count, unsigned int prec,
439                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
440 {
441   unsigned HOST_WIDE_INT signmask;
442
443   if (count < 0)
444     {
445       rshift_double (l1, h1, -count, prec, lv, hv, arith);
446       return;
447     }
448
449   if (SHIFT_COUNT_TRUNCATED)
450     count %= prec;
451
452   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
453     {
454       /* Shifting by the host word size is undefined according to the
455          ANSI standard, so we must handle this as a special case.  */
456       *hv = 0;
457       *lv = 0;
458     }
459   else if (count >= HOST_BITS_PER_WIDE_INT)
460     {
461       *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
462       *lv = 0;
463     }
464   else
465     {
466       *hv = (((unsigned HOST_WIDE_INT) h1 << count)
467              | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
468       *lv = l1 << count;
469     }
470
471   /* Sign extend all bits that are beyond the precision.  */
472
473   signmask = -((prec > HOST_BITS_PER_WIDE_INT
474                 ? ((unsigned HOST_WIDE_INT) *hv
475                    >> (prec - HOST_BITS_PER_WIDE_INT - 1))
476                 : (*lv >> (prec - 1))) & 1);
477
478   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
479     ;
480   else if (prec >= HOST_BITS_PER_WIDE_INT)
481     {
482       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
483       *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
484     }
485   else
486     {
487       *hv = signmask;
488       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
489       *lv |= signmask << prec;
490     }
491 }
492
493 /* Shift the doubleword integer in L1, H1 right by COUNT places
494    keeping only PREC bits of result.  COUNT must be positive.
495    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
496    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
497
498 void
499 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
500                HOST_WIDE_INT count, unsigned int prec,
501                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
502                int arith)
503 {
504   unsigned HOST_WIDE_INT signmask;
505
506   signmask = (arith
507               ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
508               : 0);
509
510   if (SHIFT_COUNT_TRUNCATED)
511     count %= prec;
512
513   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
514     {
515       /* Shifting by the host word size is undefined according to the
516          ANSI standard, so we must handle this as a special case.  */
517       *hv = 0;
518       *lv = 0;
519     }
520   else if (count >= HOST_BITS_PER_WIDE_INT)
521     {
522       *hv = 0;
523       *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
524     }
525   else
526     {
527       *hv = (unsigned HOST_WIDE_INT) h1 >> count;
528       *lv = ((l1 >> count)
529              | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
530     }
531
532   /* Zero / sign extend all bits that are beyond the precision.  */
533
534   if (count >= (HOST_WIDE_INT)prec)
535     {
536       *hv = signmask;
537       *lv = signmask;
538     }
539   else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
540     ;
541   else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
542     {
543       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
544       *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
545     }
546   else
547     {
548       *hv = signmask;
549       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
550       *lv |= signmask << (prec - count);
551     }
552 }
553 \f
554 /* Rotate the doubleword integer in L1, H1 left by COUNT places
555    keeping only PREC bits of result.
556    Rotate right if COUNT is negative.
557    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
558
559 void
560 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
561                 HOST_WIDE_INT count, unsigned int prec,
562                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
563 {
564   unsigned HOST_WIDE_INT s1l, s2l;
565   HOST_WIDE_INT s1h, s2h;
566
567   count %= prec;
568   if (count < 0)
569     count += prec;
570
571   lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
572   rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
573   *lv = s1l | s2l;
574   *hv = s1h | s2h;
575 }
576
577 /* Rotate the doubleword integer in L1, H1 left by COUNT places
578    keeping only PREC bits of result.  COUNT must be positive.
579    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
580
581 void
582 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
583                 HOST_WIDE_INT count, unsigned int prec,
584                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
585 {
586   unsigned HOST_WIDE_INT s1l, s2l;
587   HOST_WIDE_INT s1h, s2h;
588
589   count %= prec;
590   if (count < 0)
591     count += prec;
592
593   rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
594   lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
595   *lv = s1l | s2l;
596   *hv = s1h | s2h;
597 }
598 \f
599 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
600    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
601    CODE is a tree code for a kind of division, one of
602    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
603    or EXACT_DIV_EXPR
604    It controls how the quotient is rounded to an integer.
605    Return nonzero if the operation overflows.
606    UNS nonzero says do unsigned division.  */
607
608 int
609 div_and_round_double (enum tree_code code, int uns,
610                       unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
611                       HOST_WIDE_INT hnum_orig,
612                       unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
613                       HOST_WIDE_INT hden_orig,
614                       unsigned HOST_WIDE_INT *lquo,
615                       HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
616                       HOST_WIDE_INT *hrem)
617 {
618   int quo_neg = 0;
619   HOST_WIDE_INT num[4 + 1];     /* extra element for scaling.  */
620   HOST_WIDE_INT den[4], quo[4];
621   int i, j;
622   unsigned HOST_WIDE_INT work;
623   unsigned HOST_WIDE_INT carry = 0;
624   unsigned HOST_WIDE_INT lnum = lnum_orig;
625   HOST_WIDE_INT hnum = hnum_orig;
626   unsigned HOST_WIDE_INT lden = lden_orig;
627   HOST_WIDE_INT hden = hden_orig;
628   int overflow = 0;
629
630   if (hden == 0 && lden == 0)
631     overflow = 1, lden = 1;
632
633   /* Calculate quotient sign and convert operands to unsigned.  */
634   if (!uns)
635     {
636       if (hnum < 0)
637         {
638           quo_neg = ~ quo_neg;
639           /* (minimum integer) / (-1) is the only overflow case.  */
640           if (neg_double (lnum, hnum, &lnum, &hnum)
641               && ((HOST_WIDE_INT) lden & hden) == -1)
642             overflow = 1;
643         }
644       if (hden < 0)
645         {
646           quo_neg = ~ quo_neg;
647           neg_double (lden, hden, &lden, &hden);
648         }
649     }
650
651   if (hnum == 0 && hden == 0)
652     {                           /* single precision */
653       *hquo = *hrem = 0;
654       /* This unsigned division rounds toward zero.  */
655       *lquo = lnum / lden;
656       goto finish_up;
657     }
658
659   if (hnum == 0)
660     {                           /* trivial case: dividend < divisor */
661       /* hden != 0 already checked.  */
662       *hquo = *lquo = 0;
663       *hrem = hnum;
664       *lrem = lnum;
665       goto finish_up;
666     }
667
668   memset (quo, 0, sizeof quo);
669
670   memset (num, 0, sizeof num);  /* to zero 9th element */
671   memset (den, 0, sizeof den);
672
673   encode (num, lnum, hnum);
674   encode (den, lden, hden);
675
676   /* Special code for when the divisor < BASE.  */
677   if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
678     {
679       /* hnum != 0 already checked.  */
680       for (i = 4 - 1; i >= 0; i--)
681         {
682           work = num[i] + carry * BASE;
683           quo[i] = work / lden;
684           carry = work % lden;
685         }
686     }
687   else
688     {
689       /* Full double precision division,
690          with thanks to Don Knuth's "Seminumerical Algorithms".  */
691       int num_hi_sig, den_hi_sig;
692       unsigned HOST_WIDE_INT quo_est, scale;
693
694       /* Find the highest nonzero divisor digit.  */
695       for (i = 4 - 1;; i--)
696         if (den[i] != 0)
697           {
698             den_hi_sig = i;
699             break;
700           }
701
702       /* Insure that the first digit of the divisor is at least BASE/2.
703          This is required by the quotient digit estimation algorithm.  */
704
705       scale = BASE / (den[den_hi_sig] + 1);
706       if (scale > 1)
707         {               /* scale divisor and dividend */
708           carry = 0;
709           for (i = 0; i <= 4 - 1; i++)
710             {
711               work = (num[i] * scale) + carry;
712               num[i] = LOWPART (work);
713               carry = HIGHPART (work);
714             }
715
716           num[4] = carry;
717           carry = 0;
718           for (i = 0; i <= 4 - 1; i++)
719             {
720               work = (den[i] * scale) + carry;
721               den[i] = LOWPART (work);
722               carry = HIGHPART (work);
723               if (den[i] != 0) den_hi_sig = i;
724             }
725         }
726
727       num_hi_sig = 4;
728
729       /* Main loop */
730       for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
731         {
732           /* Guess the next quotient digit, quo_est, by dividing the first
733              two remaining dividend digits by the high order quotient digit.
734              quo_est is never low and is at most 2 high.  */
735           unsigned HOST_WIDE_INT tmp;
736
737           num_hi_sig = i + den_hi_sig + 1;
738           work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
739           if (num[num_hi_sig] != den[den_hi_sig])
740             quo_est = work / den[den_hi_sig];
741           else
742             quo_est = BASE - 1;
743
744           /* Refine quo_est so it's usually correct, and at most one high.  */
745           tmp = work - quo_est * den[den_hi_sig];
746           if (tmp < BASE
747               && (den[den_hi_sig - 1] * quo_est
748                   > (tmp * BASE + num[num_hi_sig - 2])))
749             quo_est--;
750
751           /* Try QUO_EST as the quotient digit, by multiplying the
752              divisor by QUO_EST and subtracting from the remaining dividend.
753              Keep in mind that QUO_EST is the I - 1st digit.  */
754
755           carry = 0;
756           for (j = 0; j <= den_hi_sig; j++)
757             {
758               work = quo_est * den[j] + carry;
759               carry = HIGHPART (work);
760               work = num[i + j] - LOWPART (work);
761               num[i + j] = LOWPART (work);
762               carry += HIGHPART (work) != 0;
763             }
764
765           /* If quo_est was high by one, then num[i] went negative and
766              we need to correct things.  */
767           if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
768             {
769               quo_est--;
770               carry = 0;                /* add divisor back in */
771               for (j = 0; j <= den_hi_sig; j++)
772                 {
773                   work = num[i + j] + den[j] + carry;
774                   carry = HIGHPART (work);
775                   num[i + j] = LOWPART (work);
776                 }
777
778               num [num_hi_sig] += carry;
779             }
780
781           /* Store the quotient digit.  */
782           quo[i] = quo_est;
783         }
784     }
785
786   decode (quo, lquo, hquo);
787
788  finish_up:
789   /* If result is negative, make it so.  */
790   if (quo_neg)
791     neg_double (*lquo, *hquo, lquo, hquo);
792
793   /* Compute trial remainder:  rem = num - (quo * den)  */
794   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
795   neg_double (*lrem, *hrem, lrem, hrem);
796   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
797
798   switch (code)
799     {
800     case TRUNC_DIV_EXPR:
801     case TRUNC_MOD_EXPR:        /* round toward zero */
802     case EXACT_DIV_EXPR:        /* for this one, it shouldn't matter */
803       return overflow;
804
805     case FLOOR_DIV_EXPR:
806     case FLOOR_MOD_EXPR:        /* round toward negative infinity */
807       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
808         {
809           /* quo = quo - 1;  */
810           add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
811                       lquo, hquo);
812         }
813       else
814         return overflow;
815       break;
816
817     case CEIL_DIV_EXPR:
818     case CEIL_MOD_EXPR:         /* round toward positive infinity */
819       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
820         {
821           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
822                       lquo, hquo);
823         }
824       else
825         return overflow;
826       break;
827
828     case ROUND_DIV_EXPR:
829     case ROUND_MOD_EXPR:        /* round to closest integer */
830       {
831         unsigned HOST_WIDE_INT labs_rem = *lrem;
832         HOST_WIDE_INT habs_rem = *hrem;
833         unsigned HOST_WIDE_INT labs_den = lden, ltwice;
834         HOST_WIDE_INT habs_den = hden, htwice;
835
836         /* Get absolute values.  */
837         if (*hrem < 0)
838           neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
839         if (hden < 0)
840           neg_double (lden, hden, &labs_den, &habs_den);
841
842         /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient.  */
843         mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
844                     labs_rem, habs_rem, &ltwice, &htwice);
845
846         if (((unsigned HOST_WIDE_INT) habs_den
847              < (unsigned HOST_WIDE_INT) htwice)
848             || (((unsigned HOST_WIDE_INT) habs_den
849                  == (unsigned HOST_WIDE_INT) htwice)
850                 && (labs_den <= ltwice)))
851           {
852             if (*hquo < 0)
853               /* quo = quo - 1;  */
854               add_double (*lquo, *hquo,
855                           (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
856             else
857               /* quo = quo + 1; */
858               add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
859                           lquo, hquo);
860           }
861         else
862           return overflow;
863       }
864       break;
865
866     default:
867       gcc_unreachable ();
868     }
869
870   /* Compute true remainder:  rem = num - (quo * den)  */
871   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
872   neg_double (*lrem, *hrem, lrem, hrem);
873   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
874   return overflow;
875 }
876
877 /* If ARG2 divides ARG1 with zero remainder, carries out the division
878    of type CODE and returns the quotient.
879    Otherwise returns NULL_TREE.  */
880
881 tree
882 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
883 {
884   unsigned HOST_WIDE_INT int1l, int2l;
885   HOST_WIDE_INT int1h, int2h;
886   unsigned HOST_WIDE_INT quol, reml;
887   HOST_WIDE_INT quoh, remh;
888   int uns;
889
890   /* The sign of the division is according to operand two, that
891      does the correct thing for POINTER_PLUS_EXPR where we want
892      a signed division.  */
893   uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
894   if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE
895       && TYPE_IS_SIZETYPE (TREE_TYPE (arg2)))
896     uns = false;
897
898   int1l = TREE_INT_CST_LOW (arg1);
899   int1h = TREE_INT_CST_HIGH (arg1);
900   int2l = TREE_INT_CST_LOW (arg2);
901   int2h = TREE_INT_CST_HIGH (arg2);
902
903   div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
904                         &quol, &quoh, &reml, &remh);
905   if (remh != 0 || reml != 0)
906     return NULL_TREE;
907
908   return build_int_cst_wide (TREE_TYPE (arg1), quol, quoh);
909 }
910 \f
911 /* This is nonzero if we should defer warnings about undefined
912    overflow.  This facility exists because these warnings are a
913    special case.  The code to estimate loop iterations does not want
914    to issue any warnings, since it works with expressions which do not
915    occur in user code.  Various bits of cleanup code call fold(), but
916    only use the result if it has certain characteristics (e.g., is a
917    constant); that code only wants to issue a warning if the result is
918    used.  */
919
920 static int fold_deferring_overflow_warnings;
921
922 /* If a warning about undefined overflow is deferred, this is the
923    warning.  Note that this may cause us to turn two warnings into
924    one, but that is fine since it is sufficient to only give one
925    warning per expression.  */
926
927 static const char* fold_deferred_overflow_warning;
928
929 /* If a warning about undefined overflow is deferred, this is the
930    level at which the warning should be emitted.  */
931
932 static enum warn_strict_overflow_code fold_deferred_overflow_code;
933
934 /* Start deferring overflow warnings.  We could use a stack here to
935    permit nested calls, but at present it is not necessary.  */
936
937 void
938 fold_defer_overflow_warnings (void)
939 {
940   ++fold_deferring_overflow_warnings;
941 }
942
943 /* Stop deferring overflow warnings.  If there is a pending warning,
944    and ISSUE is true, then issue the warning if appropriate.  STMT is
945    the statement with which the warning should be associated (used for
946    location information); STMT may be NULL.  CODE is the level of the
947    warning--a warn_strict_overflow_code value.  This function will use
948    the smaller of CODE and the deferred code when deciding whether to
949    issue the warning.  CODE may be zero to mean to always use the
950    deferred code.  */
951
952 void
953 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
954 {
955   const char *warnmsg;
956   location_t locus;
957
958   gcc_assert (fold_deferring_overflow_warnings > 0);
959   --fold_deferring_overflow_warnings;
960   if (fold_deferring_overflow_warnings > 0)
961     {
962       if (fold_deferred_overflow_warning != NULL
963           && code != 0
964           && code < (int) fold_deferred_overflow_code)
965         fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
966       return;
967     }
968
969   warnmsg = fold_deferred_overflow_warning;
970   fold_deferred_overflow_warning = NULL;
971
972   if (!issue || warnmsg == NULL)
973     return;
974
975   if (gimple_no_warning_p (stmt))
976     return;
977
978   /* Use the smallest code level when deciding to issue the
979      warning.  */
980   if (code == 0 || code > (int) fold_deferred_overflow_code)
981     code = fold_deferred_overflow_code;
982
983   if (!issue_strict_overflow_warning (code))
984     return;
985
986   if (stmt == NULL)
987     locus = input_location;
988   else
989     locus = gimple_location (stmt);
990   warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
991 }
992
993 /* Stop deferring overflow warnings, ignoring any deferred
994    warnings.  */
995
996 void
997 fold_undefer_and_ignore_overflow_warnings (void)
998 {
999   fold_undefer_overflow_warnings (false, NULL, 0);
1000 }
1001
1002 /* Whether we are deferring overflow warnings.  */
1003
1004 bool
1005 fold_deferring_overflow_warnings_p (void)
1006 {
1007   return fold_deferring_overflow_warnings > 0;
1008 }
1009
1010 /* This is called when we fold something based on the fact that signed
1011    overflow is undefined.  */
1012
1013 static void
1014 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1015 {
1016   if (fold_deferring_overflow_warnings > 0)
1017     {
1018       if (fold_deferred_overflow_warning == NULL
1019           || wc < fold_deferred_overflow_code)
1020         {
1021           fold_deferred_overflow_warning = gmsgid;
1022           fold_deferred_overflow_code = wc;
1023         }
1024     }
1025   else if (issue_strict_overflow_warning (wc))
1026     warning (OPT_Wstrict_overflow, gmsgid);
1027 }
1028 \f
1029 /* Return true if the built-in mathematical function specified by CODE
1030    is odd, i.e. -f(x) == f(-x).  */
1031
1032 static bool
1033 negate_mathfn_p (enum built_in_function code)
1034 {
1035   switch (code)
1036     {
1037     CASE_FLT_FN (BUILT_IN_ASIN):
1038     CASE_FLT_FN (BUILT_IN_ASINH):
1039     CASE_FLT_FN (BUILT_IN_ATAN):
1040     CASE_FLT_FN (BUILT_IN_ATANH):
1041     CASE_FLT_FN (BUILT_IN_CASIN):
1042     CASE_FLT_FN (BUILT_IN_CASINH):
1043     CASE_FLT_FN (BUILT_IN_CATAN):
1044     CASE_FLT_FN (BUILT_IN_CATANH):
1045     CASE_FLT_FN (BUILT_IN_CBRT):
1046     CASE_FLT_FN (BUILT_IN_CPROJ):
1047     CASE_FLT_FN (BUILT_IN_CSIN):
1048     CASE_FLT_FN (BUILT_IN_CSINH):
1049     CASE_FLT_FN (BUILT_IN_CTAN):
1050     CASE_FLT_FN (BUILT_IN_CTANH):
1051     CASE_FLT_FN (BUILT_IN_ERF):
1052     CASE_FLT_FN (BUILT_IN_LLROUND):
1053     CASE_FLT_FN (BUILT_IN_LROUND):
1054     CASE_FLT_FN (BUILT_IN_ROUND):
1055     CASE_FLT_FN (BUILT_IN_SIN):
1056     CASE_FLT_FN (BUILT_IN_SINH):
1057     CASE_FLT_FN (BUILT_IN_TAN):
1058     CASE_FLT_FN (BUILT_IN_TANH):
1059     CASE_FLT_FN (BUILT_IN_TRUNC):
1060       return true;
1061
1062     CASE_FLT_FN (BUILT_IN_LLRINT):
1063     CASE_FLT_FN (BUILT_IN_LRINT):
1064     CASE_FLT_FN (BUILT_IN_NEARBYINT):
1065     CASE_FLT_FN (BUILT_IN_RINT):
1066       return !flag_rounding_math;
1067
1068     default:
1069       break;
1070     }
1071   return false;
1072 }
1073
1074 /* Check whether we may negate an integer constant T without causing
1075    overflow.  */
1076
1077 bool
1078 may_negate_without_overflow_p (const_tree t)
1079 {
1080   unsigned HOST_WIDE_INT val;
1081   unsigned int prec;
1082   tree type;
1083
1084   gcc_assert (TREE_CODE (t) == INTEGER_CST);
1085
1086   type = TREE_TYPE (t);
1087   if (TYPE_UNSIGNED (type))
1088     return false;
1089
1090   prec = TYPE_PRECISION (type);
1091   if (prec > HOST_BITS_PER_WIDE_INT)
1092     {
1093       if (TREE_INT_CST_LOW (t) != 0)
1094         return true;
1095       prec -= HOST_BITS_PER_WIDE_INT;
1096       val = TREE_INT_CST_HIGH (t);
1097     }
1098   else
1099     val = TREE_INT_CST_LOW (t);
1100   if (prec < HOST_BITS_PER_WIDE_INT)
1101     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1102   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1103 }
1104
1105 /* Determine whether an expression T can be cheaply negated using
1106    the function negate_expr without introducing undefined overflow.  */
1107
1108 static bool
1109 negate_expr_p (tree t)
1110 {
1111   tree type;
1112
1113   if (t == 0)
1114     return false;
1115
1116   type = TREE_TYPE (t);
1117
1118   STRIP_SIGN_NOPS (t);
1119   switch (TREE_CODE (t))
1120     {
1121     case INTEGER_CST:
1122       if (TYPE_OVERFLOW_WRAPS (type))
1123         return true;
1124
1125       /* Check that -CST will not overflow type.  */
1126       return may_negate_without_overflow_p (t);
1127     case BIT_NOT_EXPR:
1128       return (INTEGRAL_TYPE_P (type)
1129               && TYPE_OVERFLOW_WRAPS (type));
1130
1131     case FIXED_CST:
1132     case REAL_CST:
1133     case NEGATE_EXPR:
1134       return true;
1135
1136     case COMPLEX_CST:
1137       return negate_expr_p (TREE_REALPART (t))
1138              && negate_expr_p (TREE_IMAGPART (t));
1139
1140     case COMPLEX_EXPR:
1141       return negate_expr_p (TREE_OPERAND (t, 0))
1142              && negate_expr_p (TREE_OPERAND (t, 1));
1143
1144     case CONJ_EXPR:
1145       return negate_expr_p (TREE_OPERAND (t, 0));
1146
1147     case PLUS_EXPR:
1148       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1149           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1150         return false;
1151       /* -(A + B) -> (-B) - A.  */
1152       if (negate_expr_p (TREE_OPERAND (t, 1))
1153           && reorder_operands_p (TREE_OPERAND (t, 0),
1154                                  TREE_OPERAND (t, 1)))
1155         return true;
1156       /* -(A + B) -> (-A) - B.  */
1157       return negate_expr_p (TREE_OPERAND (t, 0));
1158
1159     case MINUS_EXPR:
1160       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
1161       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1162              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1163              && reorder_operands_p (TREE_OPERAND (t, 0),
1164                                     TREE_OPERAND (t, 1));
1165
1166     case MULT_EXPR:
1167       if (TYPE_UNSIGNED (TREE_TYPE (t)))
1168         break;
1169
1170       /* Fall through.  */
1171
1172     case RDIV_EXPR:
1173       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1174         return negate_expr_p (TREE_OPERAND (t, 1))
1175                || negate_expr_p (TREE_OPERAND (t, 0));
1176       break;
1177
1178     case TRUNC_DIV_EXPR:
1179     case ROUND_DIV_EXPR:
1180     case FLOOR_DIV_EXPR:
1181     case CEIL_DIV_EXPR:
1182     case EXACT_DIV_EXPR:
1183       /* In general we can't negate A / B, because if A is INT_MIN and
1184          B is 1, we may turn this into INT_MIN / -1 which is undefined
1185          and actually traps on some architectures.  But if overflow is
1186          undefined, we can negate, because - (INT_MIN / 1) is an
1187          overflow.  */
1188       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1189           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1190         break;
1191       return negate_expr_p (TREE_OPERAND (t, 1))
1192              || negate_expr_p (TREE_OPERAND (t, 0));
1193
1194     case NOP_EXPR:
1195       /* Negate -((double)float) as (double)(-float).  */
1196       if (TREE_CODE (type) == REAL_TYPE)
1197         {
1198           tree tem = strip_float_extensions (t);
1199           if (tem != t)
1200             return negate_expr_p (tem);
1201         }
1202       break;
1203
1204     case CALL_EXPR:
1205       /* Negate -f(x) as f(-x).  */
1206       if (negate_mathfn_p (builtin_mathfn_code (t)))
1207         return negate_expr_p (CALL_EXPR_ARG (t, 0));
1208       break;
1209
1210     case RSHIFT_EXPR:
1211       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1212       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1213         {
1214           tree op1 = TREE_OPERAND (t, 1);
1215           if (TREE_INT_CST_HIGH (op1) == 0
1216               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1217                  == TREE_INT_CST_LOW (op1))
1218             return true;
1219         }
1220       break;
1221
1222     default:
1223       break;
1224     }
1225   return false;
1226 }
1227
1228 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1229    simplification is possible.
1230    If negate_expr_p would return true for T, NULL_TREE will never be
1231    returned.  */
1232
1233 static tree
1234 fold_negate_expr (location_t loc, tree t)
1235 {
1236   tree type = TREE_TYPE (t);
1237   tree tem;
1238
1239   switch (TREE_CODE (t))
1240     {
1241     /* Convert - (~A) to A + 1.  */
1242     case BIT_NOT_EXPR:
1243       if (INTEGRAL_TYPE_P (type))
1244         return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
1245                             build_int_cst (type, 1));
1246       break;
1247
1248     case INTEGER_CST:
1249       tem = fold_negate_const (t, type);
1250       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1251           || !TYPE_OVERFLOW_TRAPS (type))
1252         return tem;
1253       break;
1254
1255     case REAL_CST:
1256       tem = fold_negate_const (t, type);
1257       /* Two's complement FP formats, such as c4x, may overflow.  */
1258       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1259         return tem;
1260       break;
1261
1262     case FIXED_CST:
1263       tem = fold_negate_const (t, type);
1264       return tem;
1265
1266     case COMPLEX_CST:
1267       {
1268         tree rpart = negate_expr (TREE_REALPART (t));
1269         tree ipart = negate_expr (TREE_IMAGPART (t));
1270
1271         if ((TREE_CODE (rpart) == REAL_CST
1272              && TREE_CODE (ipart) == REAL_CST)
1273             || (TREE_CODE (rpart) == INTEGER_CST
1274                 && TREE_CODE (ipart) == INTEGER_CST))
1275           return build_complex (type, rpart, ipart);
1276       }
1277       break;
1278
1279     case COMPLEX_EXPR:
1280       if (negate_expr_p (t))
1281         return fold_build2_loc (loc, COMPLEX_EXPR, type,
1282                             fold_negate_expr (loc, TREE_OPERAND (t, 0)),
1283                             fold_negate_expr (loc, TREE_OPERAND (t, 1)));
1284       break;
1285
1286     case CONJ_EXPR:
1287       if (negate_expr_p (t))
1288         return fold_build1_loc (loc, CONJ_EXPR, type,
1289                             fold_negate_expr (loc, TREE_OPERAND (t, 0)));
1290       break;
1291
1292     case NEGATE_EXPR:
1293       return TREE_OPERAND (t, 0);
1294
1295     case PLUS_EXPR:
1296       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1297           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1298         {
1299           /* -(A + B) -> (-B) - A.  */
1300           if (negate_expr_p (TREE_OPERAND (t, 1))
1301               && reorder_operands_p (TREE_OPERAND (t, 0),
1302                                      TREE_OPERAND (t, 1)))
1303             {
1304               tem = negate_expr (TREE_OPERAND (t, 1));
1305               return fold_build2_loc (loc, MINUS_EXPR, type,
1306                                   tem, TREE_OPERAND (t, 0));
1307             }
1308
1309           /* -(A + B) -> (-A) - B.  */
1310           if (negate_expr_p (TREE_OPERAND (t, 0)))
1311             {
1312               tem = negate_expr (TREE_OPERAND (t, 0));
1313               return fold_build2_loc (loc, MINUS_EXPR, type,
1314                                   tem, TREE_OPERAND (t, 1));
1315             }
1316         }
1317       break;
1318
1319     case MINUS_EXPR:
1320       /* - (A - B) -> B - A  */
1321       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1322           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1323           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1324         return fold_build2_loc (loc, MINUS_EXPR, type,
1325                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1326       break;
1327
1328     case MULT_EXPR:
1329       if (TYPE_UNSIGNED (type))
1330         break;
1331
1332       /* Fall through.  */
1333
1334     case RDIV_EXPR:
1335       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1336         {
1337           tem = TREE_OPERAND (t, 1);
1338           if (negate_expr_p (tem))
1339             return fold_build2_loc (loc, TREE_CODE (t), type,
1340                                 TREE_OPERAND (t, 0), negate_expr (tem));
1341           tem = TREE_OPERAND (t, 0);
1342           if (negate_expr_p (tem))
1343             return fold_build2_loc (loc, TREE_CODE (t), type,
1344                                 negate_expr (tem), TREE_OPERAND (t, 1));
1345         }
1346       break;
1347
1348     case TRUNC_DIV_EXPR:
1349     case ROUND_DIV_EXPR:
1350     case FLOOR_DIV_EXPR:
1351     case CEIL_DIV_EXPR:
1352     case EXACT_DIV_EXPR:
1353       /* In general we can't negate A / B, because if A is INT_MIN and
1354          B is 1, we may turn this into INT_MIN / -1 which is undefined
1355          and actually traps on some architectures.  But if overflow is
1356          undefined, we can negate, because - (INT_MIN / 1) is an
1357          overflow.  */
1358       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1359         {
1360           const char * const warnmsg = G_("assuming signed overflow does not "
1361                                           "occur when negating a division");
1362           tem = TREE_OPERAND (t, 1);
1363           if (negate_expr_p (tem))
1364             {
1365               if (INTEGRAL_TYPE_P (type)
1366                   && (TREE_CODE (tem) != INTEGER_CST
1367                       || integer_onep (tem)))
1368                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1369               return fold_build2_loc (loc, TREE_CODE (t), type,
1370                                   TREE_OPERAND (t, 0), negate_expr (tem));
1371             }
1372           tem = TREE_OPERAND (t, 0);
1373           if (negate_expr_p (tem))
1374             {
1375               if (INTEGRAL_TYPE_P (type)
1376                   && (TREE_CODE (tem) != INTEGER_CST
1377                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1378                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1379               return fold_build2_loc (loc, TREE_CODE (t), type,
1380                                   negate_expr (tem), TREE_OPERAND (t, 1));
1381             }
1382         }
1383       break;
1384
1385     case NOP_EXPR:
1386       /* Convert -((double)float) into (double)(-float).  */
1387       if (TREE_CODE (type) == REAL_TYPE)
1388         {
1389           tem = strip_float_extensions (t);
1390           if (tem != t && negate_expr_p (tem))
1391             return fold_convert_loc (loc, type, negate_expr (tem));
1392         }
1393       break;
1394
1395     case CALL_EXPR:
1396       /* Negate -f(x) as f(-x).  */
1397       if (negate_mathfn_p (builtin_mathfn_code (t))
1398           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1399         {
1400           tree fndecl, arg;
1401
1402           fndecl = get_callee_fndecl (t);
1403           arg = negate_expr (CALL_EXPR_ARG (t, 0));
1404           return build_call_expr_loc (loc, fndecl, 1, arg);
1405         }
1406       break;
1407
1408     case RSHIFT_EXPR:
1409       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1410       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1411         {
1412           tree op1 = TREE_OPERAND (t, 1);
1413           if (TREE_INT_CST_HIGH (op1) == 0
1414               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1415                  == TREE_INT_CST_LOW (op1))
1416             {
1417               tree ntype = TYPE_UNSIGNED (type)
1418                            ? signed_type_for (type)
1419                            : unsigned_type_for (type);
1420               tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
1421               temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
1422               return fold_convert_loc (loc, type, temp);
1423             }
1424         }
1425       break;
1426
1427     default:
1428       break;
1429     }
1430
1431   return NULL_TREE;
1432 }
1433
1434 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1435    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
1436    return NULL_TREE. */
1437
1438 static tree
1439 negate_expr (tree t)
1440 {
1441   tree type, tem;
1442   location_t loc;
1443
1444   if (t == NULL_TREE)
1445     return NULL_TREE;
1446
1447   loc = EXPR_LOCATION (t);
1448   type = TREE_TYPE (t);
1449   STRIP_SIGN_NOPS (t);
1450
1451   tem = fold_negate_expr (loc, t);
1452   if (!tem)
1453     {
1454       tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1455       SET_EXPR_LOCATION (tem, loc);
1456     }
1457   return fold_convert_loc (loc, type, tem);
1458 }
1459 \f
1460 /* Split a tree IN into a constant, literal and variable parts that could be
1461    combined with CODE to make IN.  "constant" means an expression with
1462    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1463    commutative arithmetic operation.  Store the constant part into *CONP,
1464    the literal in *LITP and return the variable part.  If a part isn't
1465    present, set it to null.  If the tree does not decompose in this way,
1466    return the entire tree as the variable part and the other parts as null.
1467
1468    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1469    case, we negate an operand that was subtracted.  Except if it is a
1470    literal for which we use *MINUS_LITP instead.
1471
1472    If NEGATE_P is true, we are negating all of IN, again except a literal
1473    for which we use *MINUS_LITP instead.
1474
1475    If IN is itself a literal or constant, return it as appropriate.
1476
1477    Note that we do not guarantee that any of the three values will be the
1478    same type as IN, but they will have the same signedness and mode.  */
1479
1480 static tree
1481 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1482             tree *minus_litp, int negate_p)
1483 {
1484   tree var = 0;
1485
1486   *conp = 0;
1487   *litp = 0;
1488   *minus_litp = 0;
1489
1490   /* Strip any conversions that don't change the machine mode or signedness.  */
1491   STRIP_SIGN_NOPS (in);
1492
1493   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1494       || TREE_CODE (in) == FIXED_CST)
1495     *litp = in;
1496   else if (TREE_CODE (in) == code
1497            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1498                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1499                /* We can associate addition and subtraction together (even
1500                   though the C standard doesn't say so) for integers because
1501                   the value is not affected.  For reals, the value might be
1502                   affected, so we can't.  */
1503                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1504                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1505     {
1506       tree op0 = TREE_OPERAND (in, 0);
1507       tree op1 = TREE_OPERAND (in, 1);
1508       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1509       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1510
1511       /* First see if either of the operands is a literal, then a constant.  */
1512       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1513           || TREE_CODE (op0) == FIXED_CST)
1514         *litp = op0, op0 = 0;
1515       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1516                || TREE_CODE (op1) == FIXED_CST)
1517         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1518
1519       if (op0 != 0 && TREE_CONSTANT (op0))
1520         *conp = op0, op0 = 0;
1521       else if (op1 != 0 && TREE_CONSTANT (op1))
1522         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1523
1524       /* If we haven't dealt with either operand, this is not a case we can
1525          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1526       if (op0 != 0 && op1 != 0)
1527         var = in;
1528       else if (op0 != 0)
1529         var = op0;
1530       else
1531         var = op1, neg_var_p = neg1_p;
1532
1533       /* Now do any needed negations.  */
1534       if (neg_litp_p)
1535         *minus_litp = *litp, *litp = 0;
1536       if (neg_conp_p)
1537         *conp = negate_expr (*conp);
1538       if (neg_var_p)
1539         var = negate_expr (var);
1540     }
1541   else if (TREE_CONSTANT (in))
1542     *conp = in;
1543   else
1544     var = in;
1545
1546   if (negate_p)
1547     {
1548       if (*litp)
1549         *minus_litp = *litp, *litp = 0;
1550       else if (*minus_litp)
1551         *litp = *minus_litp, *minus_litp = 0;
1552       *conp = negate_expr (*conp);
1553       var = negate_expr (var);
1554     }
1555
1556   return var;
1557 }
1558
1559 /* Re-associate trees split by the above function.  T1 and T2 are
1560    either expressions to associate or null.  Return the new
1561    expression, if any.  LOC is the location of the new expression.  If
1562    we build an operation, do it in TYPE and with CODE.  */
1563
1564 static tree
1565 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
1566 {
1567   tree tem;
1568
1569   if (t1 == 0)
1570     return t2;
1571   else if (t2 == 0)
1572     return t1;
1573
1574   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1575      try to fold this since we will have infinite recursion.  But do
1576      deal with any NEGATE_EXPRs.  */
1577   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1578       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1579     {
1580       if (code == PLUS_EXPR)
1581         {
1582           if (TREE_CODE (t1) == NEGATE_EXPR)
1583             tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t2),
1584                           fold_convert_loc (loc, type, TREE_OPERAND (t1, 0)));
1585           else if (TREE_CODE (t2) == NEGATE_EXPR)
1586             tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t1),
1587                           fold_convert_loc (loc, type, TREE_OPERAND (t2, 0)));
1588           else if (integer_zerop (t2))
1589             return fold_convert_loc (loc, type, t1);
1590         }
1591       else if (code == MINUS_EXPR)
1592         {
1593           if (integer_zerop (t2))
1594             return fold_convert_loc (loc, type, t1);
1595         }
1596
1597       tem = build2 (code, type, fold_convert_loc (loc, type, t1),
1598                     fold_convert_loc (loc, type, t2));
1599       goto associate_trees_exit;
1600     }
1601
1602   return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1603                       fold_convert_loc (loc, type, t2));
1604  associate_trees_exit:
1605   protected_set_expr_location (tem, loc);
1606   return tem;
1607 }
1608 \f
1609 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1610    for use in int_const_binop, size_binop and size_diffop.  */
1611
1612 static bool
1613 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1614 {
1615   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1616     return false;
1617   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1618     return false;
1619
1620   switch (code)
1621     {
1622     case LSHIFT_EXPR:
1623     case RSHIFT_EXPR:
1624     case LROTATE_EXPR:
1625     case RROTATE_EXPR:
1626       return true;
1627
1628     default:
1629       break;
1630     }
1631
1632   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1633          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1634          && TYPE_MODE (type1) == TYPE_MODE (type2);
1635 }
1636
1637
1638 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1639    to produce a new constant.  Return NULL_TREE if we don't know how
1640    to evaluate CODE at compile-time.
1641
1642    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1643
1644 tree
1645 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1646 {
1647   unsigned HOST_WIDE_INT int1l, int2l;
1648   HOST_WIDE_INT int1h, int2h;
1649   unsigned HOST_WIDE_INT low;
1650   HOST_WIDE_INT hi;
1651   unsigned HOST_WIDE_INT garbagel;
1652   HOST_WIDE_INT garbageh;
1653   tree t;
1654   tree type = TREE_TYPE (arg1);
1655   int uns = TYPE_UNSIGNED (type);
1656   int is_sizetype
1657     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1658   int overflow = 0;
1659
1660   int1l = TREE_INT_CST_LOW (arg1);
1661   int1h = TREE_INT_CST_HIGH (arg1);
1662   int2l = TREE_INT_CST_LOW (arg2);
1663   int2h = TREE_INT_CST_HIGH (arg2);
1664
1665   switch (code)
1666     {
1667     case BIT_IOR_EXPR:
1668       low = int1l | int2l, hi = int1h | int2h;
1669       break;
1670
1671     case BIT_XOR_EXPR:
1672       low = int1l ^ int2l, hi = int1h ^ int2h;
1673       break;
1674
1675     case BIT_AND_EXPR:
1676       low = int1l & int2l, hi = int1h & int2h;
1677       break;
1678
1679     case RSHIFT_EXPR:
1680       int2l = -int2l;
1681     case LSHIFT_EXPR:
1682       /* It's unclear from the C standard whether shifts can overflow.
1683          The following code ignores overflow; perhaps a C standard
1684          interpretation ruling is needed.  */
1685       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1686                      &low, &hi, !uns);
1687       break;
1688
1689     case RROTATE_EXPR:
1690       int2l = - int2l;
1691     case LROTATE_EXPR:
1692       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1693                       &low, &hi);
1694       break;
1695
1696     case PLUS_EXPR:
1697       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1698       break;
1699
1700     case MINUS_EXPR:
1701       neg_double (int2l, int2h, &low, &hi);
1702       add_double (int1l, int1h, low, hi, &low, &hi);
1703       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1704       break;
1705
1706     case MULT_EXPR:
1707       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1708       break;
1709
1710     case TRUNC_DIV_EXPR:
1711     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1712     case EXACT_DIV_EXPR:
1713       /* This is a shortcut for a common special case.  */
1714       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1715           && !TREE_OVERFLOW (arg1)
1716           && !TREE_OVERFLOW (arg2)
1717           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1718         {
1719           if (code == CEIL_DIV_EXPR)
1720             int1l += int2l - 1;
1721
1722           low = int1l / int2l, hi = 0;
1723           break;
1724         }
1725
1726       /* ... fall through ...  */
1727
1728     case ROUND_DIV_EXPR:
1729       if (int2h == 0 && int2l == 0)
1730         return NULL_TREE;
1731       if (int2h == 0 && int2l == 1)
1732         {
1733           low = int1l, hi = int1h;
1734           break;
1735         }
1736       if (int1l == int2l && int1h == int2h
1737           && ! (int1l == 0 && int1h == 0))
1738         {
1739           low = 1, hi = 0;
1740           break;
1741         }
1742       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1743                                        &low, &hi, &garbagel, &garbageh);
1744       break;
1745
1746     case TRUNC_MOD_EXPR:
1747     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1748       /* This is a shortcut for a common special case.  */
1749       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1750           && !TREE_OVERFLOW (arg1)
1751           && !TREE_OVERFLOW (arg2)
1752           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1753         {
1754           if (code == CEIL_MOD_EXPR)
1755             int1l += int2l - 1;
1756           low = int1l % int2l, hi = 0;
1757           break;
1758         }
1759
1760       /* ... fall through ...  */
1761
1762     case ROUND_MOD_EXPR:
1763       if (int2h == 0 && int2l == 0)
1764         return NULL_TREE;
1765       overflow = div_and_round_double (code, uns,
1766                                        int1l, int1h, int2l, int2h,
1767                                        &garbagel, &garbageh, &low, &hi);
1768       break;
1769
1770     case MIN_EXPR:
1771     case MAX_EXPR:
1772       if (uns)
1773         low = (((unsigned HOST_WIDE_INT) int1h
1774                 < (unsigned HOST_WIDE_INT) int2h)
1775                || (((unsigned HOST_WIDE_INT) int1h
1776                     == (unsigned HOST_WIDE_INT) int2h)
1777                    && int1l < int2l));
1778       else
1779         low = (int1h < int2h
1780                || (int1h == int2h && int1l < int2l));
1781
1782       if (low == (code == MIN_EXPR))
1783         low = int1l, hi = int1h;
1784       else
1785         low = int2l, hi = int2h;
1786       break;
1787
1788     default:
1789       return NULL_TREE;
1790     }
1791
1792   if (notrunc)
1793     {
1794       t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1795
1796       /* Propagate overflow flags ourselves.  */
1797       if (((!uns || is_sizetype) && overflow)
1798           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1799         {
1800           t = copy_node (t);
1801           TREE_OVERFLOW (t) = 1;
1802         }
1803     }
1804   else
1805     t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1806                                ((!uns || is_sizetype) && overflow)
1807                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1808
1809   return t;
1810 }
1811
1812 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1813    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1814    are the same kind of constant and the same machine mode.  Return zero if
1815    combining the constants is not allowed in the current operating mode.
1816
1817    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1818
1819 static tree
1820 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1821 {
1822   /* Sanity check for the recursive cases.  */
1823   if (!arg1 || !arg2)
1824     return NULL_TREE;
1825
1826   STRIP_NOPS (arg1);
1827   STRIP_NOPS (arg2);
1828
1829   if (TREE_CODE (arg1) == INTEGER_CST)
1830     return int_const_binop (code, arg1, arg2, notrunc);
1831
1832   if (TREE_CODE (arg1) == REAL_CST)
1833     {
1834       enum machine_mode mode;
1835       REAL_VALUE_TYPE d1;
1836       REAL_VALUE_TYPE d2;
1837       REAL_VALUE_TYPE value;
1838       REAL_VALUE_TYPE result;
1839       bool inexact;
1840       tree t, type;
1841
1842       /* The following codes are handled by real_arithmetic.  */
1843       switch (code)
1844         {
1845         case PLUS_EXPR:
1846         case MINUS_EXPR:
1847         case MULT_EXPR:
1848         case RDIV_EXPR:
1849         case MIN_EXPR:
1850         case MAX_EXPR:
1851           break;
1852
1853         default:
1854           return NULL_TREE;
1855         }
1856
1857       d1 = TREE_REAL_CST (arg1);
1858       d2 = TREE_REAL_CST (arg2);
1859
1860       type = TREE_TYPE (arg1);
1861       mode = TYPE_MODE (type);
1862
1863       /* Don't perform operation if we honor signaling NaNs and
1864          either operand is a NaN.  */
1865       if (HONOR_SNANS (mode)
1866           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1867         return NULL_TREE;
1868
1869       /* Don't perform operation if it would raise a division
1870          by zero exception.  */
1871       if (code == RDIV_EXPR
1872           && REAL_VALUES_EQUAL (d2, dconst0)
1873           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1874         return NULL_TREE;
1875
1876       /* If either operand is a NaN, just return it.  Otherwise, set up
1877          for floating-point trap; we return an overflow.  */
1878       if (REAL_VALUE_ISNAN (d1))
1879         return arg1;
1880       else if (REAL_VALUE_ISNAN (d2))
1881         return arg2;
1882
1883       inexact = real_arithmetic (&value, code, &d1, &d2);
1884       real_convert (&result, mode, &value);
1885
1886       /* Don't constant fold this floating point operation if
1887          the result has overflowed and flag_trapping_math.  */
1888       if (flag_trapping_math
1889           && MODE_HAS_INFINITIES (mode)
1890           && REAL_VALUE_ISINF (result)
1891           && !REAL_VALUE_ISINF (d1)
1892           && !REAL_VALUE_ISINF (d2))
1893         return NULL_TREE;
1894
1895       /* Don't constant fold this floating point operation if the
1896          result may dependent upon the run-time rounding mode and
1897          flag_rounding_math is set, or if GCC's software emulation
1898          is unable to accurately represent the result.  */
1899       if ((flag_rounding_math
1900            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1901           && (inexact || !real_identical (&result, &value)))
1902         return NULL_TREE;
1903
1904       t = build_real (type, result);
1905
1906       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1907       return t;
1908     }
1909
1910   if (TREE_CODE (arg1) == FIXED_CST)
1911     {
1912       FIXED_VALUE_TYPE f1;
1913       FIXED_VALUE_TYPE f2;
1914       FIXED_VALUE_TYPE result;
1915       tree t, type;
1916       int sat_p;
1917       bool overflow_p;
1918
1919       /* The following codes are handled by fixed_arithmetic.  */
1920       switch (code)
1921         {
1922         case PLUS_EXPR:
1923         case MINUS_EXPR:
1924         case MULT_EXPR:
1925         case TRUNC_DIV_EXPR:
1926           f2 = TREE_FIXED_CST (arg2);
1927           break;
1928
1929         case LSHIFT_EXPR:
1930         case RSHIFT_EXPR:
1931           f2.data.high = TREE_INT_CST_HIGH (arg2);
1932           f2.data.low = TREE_INT_CST_LOW (arg2);
1933           f2.mode = SImode;
1934           break;
1935
1936         default:
1937           return NULL_TREE;
1938         }
1939
1940       f1 = TREE_FIXED_CST (arg1);
1941       type = TREE_TYPE (arg1);
1942       sat_p = TYPE_SATURATING (type);
1943       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1944       t = build_fixed (type, result);
1945       /* Propagate overflow flags.  */
1946       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1947         TREE_OVERFLOW (t) = 1;
1948       return t;
1949     }
1950
1951   if (TREE_CODE (arg1) == COMPLEX_CST)
1952     {
1953       tree type = TREE_TYPE (arg1);
1954       tree r1 = TREE_REALPART (arg1);
1955       tree i1 = TREE_IMAGPART (arg1);
1956       tree r2 = TREE_REALPART (arg2);
1957       tree i2 = TREE_IMAGPART (arg2);
1958       tree real, imag;
1959
1960       switch (code)
1961         {
1962         case PLUS_EXPR:
1963         case MINUS_EXPR:
1964           real = const_binop (code, r1, r2, notrunc);
1965           imag = const_binop (code, i1, i2, notrunc);
1966           break;
1967
1968         case MULT_EXPR:
1969           if (COMPLEX_FLOAT_TYPE_P (type))
1970             return do_mpc_arg2 (arg1, arg2, type,
1971                                 /* do_nonfinite= */ folding_initializer,
1972                                 mpc_mul);
1973
1974           real = const_binop (MINUS_EXPR,
1975                               const_binop (MULT_EXPR, r1, r2, notrunc),
1976                               const_binop (MULT_EXPR, i1, i2, notrunc),
1977                               notrunc);
1978           imag = const_binop (PLUS_EXPR,
1979                               const_binop (MULT_EXPR, r1, i2, notrunc),
1980                               const_binop (MULT_EXPR, i1, r2, notrunc),
1981                               notrunc);
1982           break;
1983
1984         case RDIV_EXPR:
1985           if (COMPLEX_FLOAT_TYPE_P (type))
1986             return do_mpc_arg2 (arg1, arg2, type,
1987                                 /* do_nonfinite= */ folding_initializer,
1988                                 mpc_div);
1989           /* Fallthru ... */
1990         case TRUNC_DIV_EXPR:
1991         case CEIL_DIV_EXPR:
1992         case FLOOR_DIV_EXPR:
1993         case ROUND_DIV_EXPR:
1994           if (flag_complex_method == 0)
1995           {
1996             /* Keep this algorithm in sync with
1997                tree-complex.c:expand_complex_div_straight().
1998
1999                Expand complex division to scalars, straightforward algorithm.
2000                a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
2001                t = br*br + bi*bi
2002             */
2003             tree magsquared
2004               = const_binop (PLUS_EXPR,
2005                              const_binop (MULT_EXPR, r2, r2, notrunc),
2006                              const_binop (MULT_EXPR, i2, i2, notrunc),
2007                              notrunc);
2008             tree t1
2009               = const_binop (PLUS_EXPR,
2010                              const_binop (MULT_EXPR, r1, r2, notrunc),
2011                              const_binop (MULT_EXPR, i1, i2, notrunc),
2012                              notrunc);
2013             tree t2
2014               = const_binop (MINUS_EXPR,
2015                              const_binop (MULT_EXPR, i1, r2, notrunc),
2016                              const_binop (MULT_EXPR, r1, i2, notrunc),
2017                              notrunc);
2018
2019             real = const_binop (code, t1, magsquared, notrunc);
2020             imag = const_binop (code, t2, magsquared, notrunc);
2021           }
2022           else
2023           {
2024             /* Keep this algorithm in sync with
2025                tree-complex.c:expand_complex_div_wide().
2026
2027                Expand complex division to scalars, modified algorithm to minimize
2028                overflow with wide input ranges.  */
2029             tree compare = fold_build2 (LT_EXPR, boolean_type_node,
2030                                         fold_abs_const (r2, TREE_TYPE (type)),
2031                                         fold_abs_const (i2, TREE_TYPE (type)));
2032
2033             if (integer_nonzerop (compare))
2034               {
2035                 /* In the TRUE branch, we compute
2036                    ratio = br/bi;
2037                    div = (br * ratio) + bi;
2038                    tr = (ar * ratio) + ai;
2039                    ti = (ai * ratio) - ar;
2040                    tr = tr / div;
2041                    ti = ti / div;  */
2042                 tree ratio = const_binop (code, r2, i2, notrunc);
2043                 tree div = const_binop (PLUS_EXPR, i2,
2044                                         const_binop (MULT_EXPR, r2, ratio,
2045                                                      notrunc),
2046                                         notrunc);
2047                 real = const_binop (MULT_EXPR, r1, ratio, notrunc);
2048                 real = const_binop (PLUS_EXPR, real, i1, notrunc);
2049                 real = const_binop (code, real, div, notrunc);
2050
2051                 imag = const_binop (MULT_EXPR, i1, ratio, notrunc);
2052                 imag = const_binop (MINUS_EXPR, imag, r1, notrunc);
2053                 imag = const_binop (code, imag, div, notrunc);
2054               }
2055             else
2056               {
2057                 /* In the FALSE branch, we compute
2058                    ratio = d/c;
2059                    divisor = (d * ratio) + c;
2060                    tr = (b * ratio) + a;
2061                    ti = b - (a * ratio);
2062                    tr = tr / div;
2063                    ti = ti / div;  */
2064                 tree ratio = const_binop (code, i2, r2, notrunc);
2065                 tree div = const_binop (PLUS_EXPR, r2,
2066                                         const_binop (MULT_EXPR, i2, ratio,
2067                                                      notrunc),
2068                                         notrunc);
2069
2070                 real = const_binop (MULT_EXPR, i1, ratio, notrunc);
2071                 real = const_binop (PLUS_EXPR, real, r1, notrunc);
2072                 real = const_binop (code, real, div, notrunc);
2073
2074                 imag = const_binop (MULT_EXPR, r1, ratio, notrunc);
2075                 imag = const_binop (MINUS_EXPR, i1, imag, notrunc);
2076                 imag = const_binop (code, imag, div, notrunc);
2077               }
2078           }
2079           break;
2080
2081         default:
2082           return NULL_TREE;
2083         }
2084
2085       if (real && imag)
2086         return build_complex (type, real, imag);
2087     }
2088
2089   if (TREE_CODE (arg1) == VECTOR_CST)
2090     {
2091       tree type = TREE_TYPE(arg1);
2092       int count = TYPE_VECTOR_SUBPARTS (type), i;
2093       tree elements1, elements2, list = NULL_TREE;
2094
2095       if(TREE_CODE(arg2) != VECTOR_CST)
2096         return NULL_TREE;
2097
2098       elements1 = TREE_VECTOR_CST_ELTS (arg1);
2099       elements2 = TREE_VECTOR_CST_ELTS (arg2);
2100
2101       for (i = 0; i < count; i++)
2102         {
2103           tree elem1, elem2, elem;
2104
2105           /* The trailing elements can be empty and should be treated as 0 */
2106           if(!elements1)
2107             elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2108           else
2109             {
2110               elem1 = TREE_VALUE(elements1);
2111               elements1 = TREE_CHAIN (elements1);
2112             }
2113
2114           if(!elements2)
2115             elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2116           else
2117             {
2118               elem2 = TREE_VALUE(elements2);
2119               elements2 = TREE_CHAIN (elements2);
2120             }
2121
2122           elem = const_binop (code, elem1, elem2, notrunc);
2123
2124           /* It is possible that const_binop cannot handle the given
2125             code and return NULL_TREE */
2126           if(elem == NULL_TREE)
2127             return NULL_TREE;
2128
2129           list = tree_cons (NULL_TREE, elem, list);
2130         }
2131       return build_vector(type, nreverse(list));
2132     }
2133   return NULL_TREE;
2134 }
2135
2136 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
2137    indicates which particular sizetype to create.  */
2138
2139 tree
2140 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2141 {
2142   return build_int_cst (sizetype_tab[(int) kind], number);
2143 }
2144 \f
2145 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
2146    is a tree code.  The type of the result is taken from the operands.
2147    Both must be equivalent integer types, ala int_binop_types_match_p.
2148    If the operands are constant, so is the result.  */
2149
2150 tree
2151 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2152 {
2153   tree type = TREE_TYPE (arg0);
2154
2155   if (arg0 == error_mark_node || arg1 == error_mark_node)
2156     return error_mark_node;
2157
2158   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2159                                        TREE_TYPE (arg1)));
2160
2161   /* Handle the special case of two integer constants faster.  */
2162   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2163     {
2164       /* And some specific cases even faster than that.  */
2165       if (code == PLUS_EXPR)
2166         {
2167           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2168             return arg1;
2169           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2170             return arg0;
2171         }
2172       else if (code == MINUS_EXPR)
2173         {
2174           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2175             return arg0;
2176         }
2177       else if (code == MULT_EXPR)
2178         {
2179           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2180             return arg1;
2181         }
2182
2183       /* Handle general case of two integer constants.  */
2184       return int_const_binop (code, arg0, arg1, 0);
2185     }
2186
2187   return fold_build2_loc (loc, code, type, arg0, arg1);
2188 }
2189
2190 /* Given two values, either both of sizetype or both of bitsizetype,
2191    compute the difference between the two values.  Return the value
2192    in signed type corresponding to the type of the operands.  */
2193
2194 tree
2195 size_diffop_loc (location_t loc, tree arg0, tree arg1)
2196 {
2197   tree type = TREE_TYPE (arg0);
2198   tree ctype;
2199
2200   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2201                                        TREE_TYPE (arg1)));
2202
2203   /* If the type is already signed, just do the simple thing.  */
2204   if (!TYPE_UNSIGNED (type))
2205     return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2206
2207   if (type == sizetype)
2208     ctype = ssizetype;
2209   else if (type == bitsizetype)
2210     ctype = sbitsizetype;
2211   else
2212     ctype = signed_type_for (type);
2213
2214   /* If either operand is not a constant, do the conversions to the signed
2215      type and subtract.  The hardware will do the right thing with any
2216      overflow in the subtraction.  */
2217   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2218     return size_binop_loc (loc, MINUS_EXPR,
2219                            fold_convert_loc (loc, ctype, arg0),
2220                            fold_convert_loc (loc, ctype, arg1));
2221
2222   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2223      Otherwise, subtract the other way, convert to CTYPE (we know that can't
2224      overflow) and negate (which can't either).  Special-case a result
2225      of zero while we're here.  */
2226   if (tree_int_cst_equal (arg0, arg1))
2227     return build_int_cst (ctype, 0);
2228   else if (tree_int_cst_lt (arg1, arg0))
2229     return fold_convert_loc (loc, ctype,
2230                              size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2231   else
2232     return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2233                            fold_convert_loc (loc, ctype,
2234                                              size_binop_loc (loc,
2235                                                              MINUS_EXPR,
2236                                                              arg1, arg0)));
2237 }
2238 \f
2239 /* A subroutine of fold_convert_const handling conversions of an
2240    INTEGER_CST to another integer type.  */
2241
2242 static tree
2243 fold_convert_const_int_from_int (tree type, const_tree arg1)
2244 {
2245   tree t;
2246
2247   /* Given an integer constant, make new constant with new type,
2248      appropriately sign-extended or truncated.  */
2249   t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2250                              TREE_INT_CST_HIGH (arg1),
2251                              /* Don't set the overflow when
2252                                 converting from a pointer,  */
2253                              !POINTER_TYPE_P (TREE_TYPE (arg1))
2254                              /* or to a sizetype with same signedness
2255                                 and the precision is unchanged.
2256                                 ???  sizetype is always sign-extended,
2257                                 but its signedness depends on the
2258                                 frontend.  Thus we see spurious overflows
2259                                 here if we do not check this.  */
2260                              && !((TYPE_PRECISION (TREE_TYPE (arg1))
2261                                    == TYPE_PRECISION (type))
2262                                   && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2263                                       == TYPE_UNSIGNED (type))
2264                                   && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2265                                        && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2266                                       || (TREE_CODE (type) == INTEGER_TYPE
2267                                           && TYPE_IS_SIZETYPE (type)))),
2268                              (TREE_INT_CST_HIGH (arg1) < 0
2269                               && (TYPE_UNSIGNED (type)
2270                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2271                              | TREE_OVERFLOW (arg1));
2272
2273   return t;
2274 }
2275
2276 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2277    to an integer type.  */
2278
2279 static tree
2280 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2281 {
2282   int overflow = 0;
2283   tree t;
2284
2285   /* The following code implements the floating point to integer
2286      conversion rules required by the Java Language Specification,
2287      that IEEE NaNs are mapped to zero and values that overflow
2288      the target precision saturate, i.e. values greater than
2289      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2290      are mapped to INT_MIN.  These semantics are allowed by the
2291      C and C++ standards that simply state that the behavior of
2292      FP-to-integer conversion is unspecified upon overflow.  */
2293
2294   HOST_WIDE_INT high, low;
2295   REAL_VALUE_TYPE r;
2296   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2297
2298   switch (code)
2299     {
2300     case FIX_TRUNC_EXPR:
2301       real_trunc (&r, VOIDmode, &x);
2302       break;
2303
2304     default:
2305       gcc_unreachable ();
2306     }
2307
2308   /* If R is NaN, return zero and show we have an overflow.  */
2309   if (REAL_VALUE_ISNAN (r))
2310     {
2311       overflow = 1;
2312       high = 0;
2313       low = 0;
2314     }
2315
2316   /* See if R is less than the lower bound or greater than the
2317      upper bound.  */
2318
2319   if (! overflow)
2320     {
2321       tree lt = TYPE_MIN_VALUE (type);
2322       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2323       if (REAL_VALUES_LESS (r, l))
2324         {
2325           overflow = 1;
2326           high = TREE_INT_CST_HIGH (lt);
2327           low = TREE_INT_CST_LOW (lt);
2328         }
2329     }
2330
2331   if (! overflow)
2332     {
2333       tree ut = TYPE_MAX_VALUE (type);
2334       if (ut)
2335         {
2336           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2337           if (REAL_VALUES_LESS (u, r))
2338             {
2339               overflow = 1;
2340               high = TREE_INT_CST_HIGH (ut);
2341               low = TREE_INT_CST_LOW (ut);
2342             }
2343         }
2344     }
2345
2346   if (! overflow)
2347     REAL_VALUE_TO_INT (&low, &high, r);
2348
2349   t = force_fit_type_double (type, low, high, -1,
2350                              overflow | TREE_OVERFLOW (arg1));
2351   return t;
2352 }
2353
2354 /* A subroutine of fold_convert_const handling conversions of a
2355    FIXED_CST to an integer type.  */
2356
2357 static tree
2358 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2359 {
2360   tree t;
2361   double_int temp, temp_trunc;
2362   unsigned int mode;
2363
2364   /* Right shift FIXED_CST to temp by fbit.  */
2365   temp = TREE_FIXED_CST (arg1).data;
2366   mode = TREE_FIXED_CST (arg1).mode;
2367   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2368     {
2369       lshift_double (temp.low, temp.high,
2370                      - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2371                      &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2372
2373       /* Left shift temp to temp_trunc by fbit.  */
2374       lshift_double (temp.low, temp.high,
2375                      GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2376                      &temp_trunc.low, &temp_trunc.high,
2377                      SIGNED_FIXED_POINT_MODE_P (mode));
2378     }
2379   else
2380     {
2381       temp.low = 0;
2382       temp.high = 0;
2383       temp_trunc.low = 0;
2384       temp_trunc.high = 0;
2385     }
2386
2387   /* If FIXED_CST is negative, we need to round the value toward 0.
2388      By checking if the fractional bits are not zero to add 1 to temp.  */
2389   if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2390       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2391     {
2392       double_int one;
2393       one.low = 1;
2394       one.high = 0;
2395       temp = double_int_add (temp, one);
2396     }
2397
2398   /* Given a fixed-point constant, make new constant with new type,
2399      appropriately sign-extended or truncated.  */
2400   t = force_fit_type_double (type, temp.low, temp.high, -1,
2401                              (temp.high < 0
2402                               && (TYPE_UNSIGNED (type)
2403                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2404                              | TREE_OVERFLOW (arg1));
2405
2406   return t;
2407 }
2408
2409 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2410    to another floating point type.  */
2411
2412 static tree
2413 fold_convert_const_real_from_real (tree type, const_tree arg1)
2414 {
2415   REAL_VALUE_TYPE value;
2416   tree t;
2417
2418   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2419   t = build_real (type, value);
2420
2421   /* If converting an infinity or NAN to a representation that doesn't
2422      have one, set the overflow bit so that we can produce some kind of
2423      error message at the appropriate point if necessary.  It's not the
2424      most user-friendly message, but it's better than nothing.  */
2425   if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2426       && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2427     TREE_OVERFLOW (t) = 1;
2428   else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2429            && !MODE_HAS_NANS (TYPE_MODE (type)))
2430     TREE_OVERFLOW (t) = 1;
2431   /* Regular overflow, conversion produced an infinity in a mode that
2432      can't represent them.  */
2433   else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2434            && REAL_VALUE_ISINF (value)
2435            && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2436     TREE_OVERFLOW (t) = 1;
2437   else
2438     TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2439   return t;
2440 }
2441
2442 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2443    to a floating point type.  */
2444
2445 static tree
2446 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2447 {
2448   REAL_VALUE_TYPE value;
2449   tree t;
2450
2451   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2452   t = build_real (type, value);
2453
2454   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2455   return t;
2456 }
2457
2458 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2459    to another fixed-point type.  */
2460
2461 static tree
2462 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2463 {
2464   FIXED_VALUE_TYPE value;
2465   tree t;
2466   bool overflow_p;
2467
2468   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2469                               TYPE_SATURATING (type));
2470   t = build_fixed (type, value);
2471
2472   /* Propagate overflow flags.  */
2473   if (overflow_p | TREE_OVERFLOW (arg1))
2474     TREE_OVERFLOW (t) = 1;
2475   return t;
2476 }
2477
2478 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2479    to a fixed-point type.  */
2480
2481 static tree
2482 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2483 {
2484   FIXED_VALUE_TYPE value;
2485   tree t;
2486   bool overflow_p;
2487
2488   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2489                                        TREE_INT_CST (arg1),
2490                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
2491                                        TYPE_SATURATING (type));
2492   t = build_fixed (type, value);
2493
2494   /* Propagate overflow flags.  */
2495   if (overflow_p | TREE_OVERFLOW (arg1))
2496     TREE_OVERFLOW (t) = 1;
2497   return t;
2498 }
2499
2500 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2501    to a fixed-point type.  */
2502
2503 static tree
2504 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2505 {
2506   FIXED_VALUE_TYPE value;
2507   tree t;
2508   bool overflow_p;
2509
2510   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2511                                         &TREE_REAL_CST (arg1),
2512                                         TYPE_SATURATING (type));
2513   t = build_fixed (type, value);
2514
2515   /* Propagate overflow flags.  */
2516   if (overflow_p | TREE_OVERFLOW (arg1))
2517     TREE_OVERFLOW (t) = 1;
2518   return t;
2519 }
2520
2521 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2522    type TYPE.  If no simplification can be done return NULL_TREE.  */
2523
2524 static tree
2525 fold_convert_const (enum tree_code code, tree type, tree arg1)
2526 {
2527   if (TREE_TYPE (arg1) == type)
2528     return arg1;
2529
2530   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2531       || TREE_CODE (type) == OFFSET_TYPE)
2532     {
2533       if (TREE_CODE (arg1) == INTEGER_CST)
2534         return fold_convert_const_int_from_int (type, arg1);
2535       else if (TREE_CODE (arg1) == REAL_CST)
2536         return fold_convert_const_int_from_real (code, type, arg1);
2537       else if (TREE_CODE (arg1) == FIXED_CST)
2538         return fold_convert_const_int_from_fixed (type, arg1);
2539     }
2540   else if (TREE_CODE (type) == REAL_TYPE)
2541     {
2542       if (TREE_CODE (arg1) == INTEGER_CST)
2543         return build_real_from_int_cst (type, arg1);
2544       else if (TREE_CODE (arg1) == REAL_CST)
2545         return fold_convert_const_real_from_real (type, arg1);
2546       else if (TREE_CODE (arg1) == FIXED_CST)
2547         return fold_convert_const_real_from_fixed (type, arg1);
2548     }
2549   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2550     {
2551       if (TREE_CODE (arg1) == FIXED_CST)
2552         return fold_convert_const_fixed_from_fixed (type, arg1);
2553       else if (TREE_CODE (arg1) == INTEGER_CST)
2554         return fold_convert_const_fixed_from_int (type, arg1);
2555       else if (TREE_CODE (arg1) == REAL_CST)
2556         return fold_convert_const_fixed_from_real (type, arg1);
2557     }
2558   return NULL_TREE;
2559 }
2560
2561 /* Construct a vector of zero elements of vector type TYPE.  */
2562
2563 static tree
2564 build_zero_vector (tree type)
2565 {
2566   tree elem, list;
2567   int i, units;
2568
2569   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2570   units = TYPE_VECTOR_SUBPARTS (type);
2571
2572   list = NULL_TREE;
2573   for (i = 0; i < units; i++)
2574     list = tree_cons (NULL_TREE, elem, list);
2575   return build_vector (type, list);
2576 }
2577
2578 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
2579
2580 bool
2581 fold_convertible_p (const_tree type, const_tree arg)
2582 {
2583   tree orig = TREE_TYPE (arg);
2584
2585   if (type == orig)
2586     return true;
2587
2588   if (TREE_CODE (arg) == ERROR_MARK
2589       || TREE_CODE (type) == ERROR_MARK
2590       || TREE_CODE (orig) == ERROR_MARK)
2591     return false;
2592
2593   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2594     return true;
2595
2596   switch (TREE_CODE (type))
2597     {
2598     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2599     case POINTER_TYPE: case REFERENCE_TYPE:
2600     case OFFSET_TYPE:
2601       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2602           || TREE_CODE (orig) == OFFSET_TYPE)
2603         return true;
2604       return (TREE_CODE (orig) == VECTOR_TYPE
2605               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2606
2607     case REAL_TYPE:
2608     case FIXED_POINT_TYPE:
2609     case COMPLEX_TYPE:
2610     case VECTOR_TYPE:
2611     case VOID_TYPE:
2612       return TREE_CODE (type) == TREE_CODE (orig);
2613
2614     default:
2615       return false;
2616     }
2617 }
2618
2619 /* Convert expression ARG to type TYPE.  Used by the middle-end for
2620    simple conversions in preference to calling the front-end's convert.  */
2621
2622 tree
2623 fold_convert_loc (location_t loc, tree type, tree arg)
2624 {
2625   tree orig = TREE_TYPE (arg);
2626   tree tem;
2627
2628   if (type == orig)
2629     return arg;
2630
2631   if (TREE_CODE (arg) == ERROR_MARK
2632       || TREE_CODE (type) == ERROR_MARK
2633       || TREE_CODE (orig) == ERROR_MARK)
2634     return error_mark_node;
2635
2636   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2637     return fold_build1_loc (loc, NOP_EXPR, type, arg);
2638
2639   switch (TREE_CODE (type))
2640     {
2641     case POINTER_TYPE:
2642     case REFERENCE_TYPE:
2643       /* Handle conversions between pointers to different address spaces.  */
2644       if (POINTER_TYPE_P (orig)
2645           && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2646               != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2647         return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2648       /* fall through */
2649
2650     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2651     case OFFSET_TYPE:
2652       if (TREE_CODE (arg) == INTEGER_CST)
2653         {
2654           tem = fold_convert_const (NOP_EXPR, type, arg);
2655           if (tem != NULL_TREE)
2656             return tem;
2657         }
2658       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2659           || TREE_CODE (orig) == OFFSET_TYPE)
2660         return fold_build1_loc (loc, NOP_EXPR, type, arg);
2661       if (TREE_CODE (orig) == COMPLEX_TYPE)
2662         return fold_convert_loc (loc, type,
2663                              fold_build1_loc (loc, REALPART_EXPR,
2664                                           TREE_TYPE (orig), arg));
2665       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2666                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2667       return fold_build1_loc (loc, NOP_EXPR, type, arg);
2668
2669     case REAL_TYPE:
2670       if (TREE_CODE (arg) == INTEGER_CST)
2671         {
2672           tem = fold_convert_const (FLOAT_EXPR, type, arg);
2673           if (tem != NULL_TREE)
2674             return tem;
2675         }
2676       else if (TREE_CODE (arg) == REAL_CST)
2677         {
2678           tem = fold_convert_const (NOP_EXPR, type, arg);
2679           if (tem != NULL_TREE)
2680             return tem;
2681         }
2682       else if (TREE_CODE (arg) == FIXED_CST)
2683         {
2684           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2685           if (tem != NULL_TREE)
2686             return tem;
2687         }
2688
2689       switch (TREE_CODE (orig))
2690         {
2691         case INTEGER_TYPE:
2692         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2693         case POINTER_TYPE: case REFERENCE_TYPE:
2694           return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2695
2696         case REAL_TYPE:
2697           return fold_build1_loc (loc, NOP_EXPR, type, arg);
2698
2699         case FIXED_POINT_TYPE:
2700           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2701
2702         case COMPLEX_TYPE:
2703           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2704           return fold_convert_loc (loc, type, tem);
2705
2706         default:
2707           gcc_unreachable ();
2708         }
2709
2710     case FIXED_POINT_TYPE:
2711       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2712           || TREE_CODE (arg) == REAL_CST)
2713         {
2714           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2715           if (tem != NULL_TREE)
2716             goto fold_convert_exit;
2717         }
2718
2719       switch (TREE_CODE (orig))
2720         {
2721         case FIXED_POINT_TYPE:
2722         case INTEGER_TYPE:
2723         case ENUMERAL_TYPE:
2724         case BOOLEAN_TYPE:
2725         case REAL_TYPE:
2726           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2727
2728         case COMPLEX_TYPE:
2729           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2730           return fold_convert_loc (loc, type, tem);
2731
2732         default:
2733           gcc_unreachable ();
2734         }
2735
2736     case COMPLEX_TYPE:
2737       switch (TREE_CODE (orig))
2738         {
2739         case INTEGER_TYPE:
2740         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2741         case POINTER_TYPE: case REFERENCE_TYPE:
2742         case REAL_TYPE:
2743         case FIXED_POINT_TYPE:
2744           return fold_build2_loc (loc, COMPLEX_EXPR, type,
2745                               fold_convert_loc (loc, TREE_TYPE (type), arg),
2746                               fold_convert_loc (loc, TREE_TYPE (type),
2747                                             integer_zero_node));
2748         case COMPLEX_TYPE:
2749           {
2750             tree rpart, ipart;
2751
2752             if (TREE_CODE (arg) == COMPLEX_EXPR)
2753               {
2754                 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2755                                       TREE_OPERAND (arg, 0));
2756                 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2757                                       TREE_OPERAND (arg, 1));
2758                 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2759               }
2760
2761             arg = save_expr (arg);
2762             rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2763             ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2764             rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2765             ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2766             return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2767           }
2768
2769         default:
2770           gcc_unreachable ();
2771         }
2772
2773     case VECTOR_TYPE:
2774       if (integer_zerop (arg))
2775         return build_zero_vector (type);
2776       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2777       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2778                   || TREE_CODE (orig) == VECTOR_TYPE);
2779       return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2780
2781     case VOID_TYPE:
2782       tem = fold_ignored_result (arg);
2783       if (TREE_CODE (tem) == MODIFY_EXPR)
2784         goto fold_convert_exit;
2785       return fold_build1_loc (loc, NOP_EXPR, type, tem);
2786
2787     default:
2788       gcc_unreachable ();
2789     }
2790  fold_convert_exit:
2791   protected_set_expr_location (tem, loc);
2792   return tem;
2793 }
2794 \f
2795 /* Return false if expr can be assumed not to be an lvalue, true
2796    otherwise.  */
2797
2798 static bool
2799 maybe_lvalue_p (const_tree x)
2800 {
2801   /* We only need to wrap lvalue tree codes.  */
2802   switch (TREE_CODE (x))
2803   {
2804   case VAR_DECL:
2805   case PARM_DECL:
2806   case RESULT_DECL:
2807   case LABEL_DECL:
2808   case FUNCTION_DECL:
2809   case SSA_NAME:
2810
2811   case COMPONENT_REF:
2812   case INDIRECT_REF:
2813   case ALIGN_INDIRECT_REF:
2814   case MISALIGNED_INDIRECT_REF:
2815   case ARRAY_REF:
2816   case ARRAY_RANGE_REF:
2817   case BIT_FIELD_REF:
2818   case OBJ_TYPE_REF:
2819
2820   case REALPART_EXPR:
2821   case IMAGPART_EXPR:
2822   case PREINCREMENT_EXPR:
2823   case PREDECREMENT_EXPR:
2824   case SAVE_EXPR:
2825   case TRY_CATCH_EXPR:
2826   case WITH_CLEANUP_EXPR:
2827   case COMPOUND_EXPR:
2828   case MODIFY_EXPR:
2829   case TARGET_EXPR:
2830   case COND_EXPR:
2831   case BIND_EXPR:
2832     break;
2833
2834   default:
2835     /* Assume the worst for front-end tree codes.  */
2836     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2837       break;
2838     return false;
2839   }
2840
2841   return true;
2842 }
2843
2844 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2845
2846 tree
2847 non_lvalue_loc (location_t loc, tree x)
2848 {
2849   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2850      us.  */
2851   if (in_gimple_form)
2852     return x;
2853
2854   if (! maybe_lvalue_p (x))
2855     return x;
2856   x = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2857   SET_EXPR_LOCATION (x, loc);
2858   return x;
2859 }
2860
2861 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2862    Zero means allow extended lvalues.  */
2863
2864 int pedantic_lvalues;
2865
2866 /* When pedantic, return an expr equal to X but certainly not valid as a
2867    pedantic lvalue.  Otherwise, return X.  */
2868
2869 static tree
2870 pedantic_non_lvalue_loc (location_t loc, tree x)
2871 {
2872   if (pedantic_lvalues)
2873     return non_lvalue_loc (loc, x);
2874   protected_set_expr_location (x, loc);
2875   return x;
2876 }
2877 \f
2878 /* Given a tree comparison code, return the code that is the logical inverse
2879    of the given code.  It is not safe to do this for floating-point
2880    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2881    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2882
2883 enum tree_code
2884 invert_tree_comparison (enum tree_code code, bool honor_nans)
2885 {
2886   if (honor_nans && flag_trapping_math)
2887     return ERROR_MARK;
2888
2889   switch (code)
2890     {
2891     case EQ_EXPR:
2892       return NE_EXPR;
2893     case NE_EXPR:
2894       return EQ_EXPR;
2895     case GT_EXPR:
2896       return honor_nans ? UNLE_EXPR : LE_EXPR;
2897     case GE_EXPR:
2898       return honor_nans ? UNLT_EXPR : LT_EXPR;
2899     case LT_EXPR:
2900       return honor_nans ? UNGE_EXPR : GE_EXPR;
2901     case LE_EXPR:
2902       return honor_nans ? UNGT_EXPR : GT_EXPR;
2903     case LTGT_EXPR:
2904       return UNEQ_EXPR;
2905     case UNEQ_EXPR:
2906       return LTGT_EXPR;
2907     case UNGT_EXPR:
2908       return LE_EXPR;
2909     case UNGE_EXPR:
2910       return LT_EXPR;
2911     case UNLT_EXPR:
2912       return GE_EXPR;
2913     case UNLE_EXPR:
2914       return GT_EXPR;
2915     case ORDERED_EXPR:
2916       return UNORDERED_EXPR;
2917     case UNORDERED_EXPR:
2918       return ORDERED_EXPR;
2919     default:
2920       gcc_unreachable ();
2921     }
2922 }
2923
2924 /* Similar, but return the comparison that results if the operands are
2925    swapped.  This is safe for floating-point.  */
2926
2927 enum tree_code
2928 swap_tree_comparison (enum tree_code code)
2929 {
2930   switch (code)
2931     {
2932     case EQ_EXPR:
2933     case NE_EXPR:
2934     case ORDERED_EXPR:
2935     case UNORDERED_EXPR:
2936     case LTGT_EXPR:
2937     case UNEQ_EXPR:
2938       return code;
2939     case GT_EXPR:
2940       return LT_EXPR;
2941     case GE_EXPR:
2942       return LE_EXPR;
2943     case LT_EXPR:
2944       return GT_EXPR;
2945     case LE_EXPR:
2946       return GE_EXPR;
2947     case UNGT_EXPR:
2948       return UNLT_EXPR;
2949     case UNGE_EXPR:
2950       return UNLE_EXPR;
2951     case UNLT_EXPR:
2952       return UNGT_EXPR;
2953     case UNLE_EXPR:
2954       return UNGE_EXPR;
2955     default:
2956       gcc_unreachable ();
2957     }
2958 }
2959
2960
2961 /* Convert a comparison tree code from an enum tree_code representation
2962    into a compcode bit-based encoding.  This function is the inverse of
2963    compcode_to_comparison.  */
2964
2965 static enum comparison_code
2966 comparison_to_compcode (enum tree_code code)
2967 {
2968   switch (code)
2969     {
2970     case LT_EXPR:
2971       return COMPCODE_LT;
2972     case EQ_EXPR:
2973       return COMPCODE_EQ;
2974     case LE_EXPR:
2975       return COMPCODE_LE;
2976     case GT_EXPR:
2977       return COMPCODE_GT;
2978     case NE_EXPR:
2979       return COMPCODE_NE;
2980     case GE_EXPR:
2981       return COMPCODE_GE;
2982     case ORDERED_EXPR:
2983       return COMPCODE_ORD;
2984     case UNORDERED_EXPR:
2985       return COMPCODE_UNORD;
2986     case UNLT_EXPR:
2987       return COMPCODE_UNLT;
2988     case UNEQ_EXPR:
2989       return COMPCODE_UNEQ;
2990     case UNLE_EXPR:
2991       return COMPCODE_UNLE;
2992     case UNGT_EXPR:
2993       return COMPCODE_UNGT;
2994     case LTGT_EXPR:
2995       return COMPCODE_LTGT;
2996     case UNGE_EXPR:
2997       return COMPCODE_UNGE;
2998     default:
2999       gcc_unreachable ();
3000     }
3001 }
3002
3003 /* Convert a compcode bit-based encoding of a comparison operator back
3004    to GCC's enum tree_code representation.  This function is the
3005    inverse of comparison_to_compcode.  */
3006
3007 static enum tree_code
3008 compcode_to_comparison (enum comparison_code code)
3009 {
3010   switch (code)
3011     {
3012     case COMPCODE_LT:
3013       return LT_EXPR;
3014     case COMPCODE_EQ:
3015       return EQ_EXPR;
3016     case COMPCODE_LE:
3017       return LE_EXPR;
3018     case COMPCODE_GT:
3019       return GT_EXPR;
3020     case COMPCODE_NE:
3021       return NE_EXPR;
3022     case COMPCODE_GE:
3023       return GE_EXPR;
3024     case COMPCODE_ORD:
3025       return ORDERED_EXPR;
3026     case COMPCODE_UNORD:
3027       return UNORDERED_EXPR;
3028     case COMPCODE_UNLT:
3029       return UNLT_EXPR;
3030     case COMPCODE_UNEQ:
3031       return UNEQ_EXPR;
3032     case COMPCODE_UNLE:
3033       return UNLE_EXPR;
3034     case COMPCODE_UNGT:
3035       return UNGT_EXPR;
3036     case COMPCODE_LTGT:
3037       return LTGT_EXPR;
3038     case COMPCODE_UNGE:
3039       return UNGE_EXPR;
3040     default:
3041       gcc_unreachable ();
3042     }
3043 }
3044
3045 /* Return a tree for the comparison which is the combination of
3046    doing the AND or OR (depending on CODE) of the two operations LCODE
3047    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
3048    the possibility of trapping if the mode has NaNs, and return NULL_TREE
3049    if this makes the transformation invalid.  */
3050
3051 tree
3052 combine_comparisons (location_t loc,
3053                      enum tree_code code, enum tree_code lcode,
3054                      enum tree_code rcode, tree truth_type,
3055                      tree ll_arg, tree lr_arg)
3056 {
3057   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
3058   enum comparison_code lcompcode = comparison_to_compcode (lcode);
3059   enum comparison_code rcompcode = comparison_to_compcode (rcode);
3060   int compcode;
3061
3062   switch (code)
3063     {
3064     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3065       compcode = lcompcode & rcompcode;
3066       break;
3067
3068     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3069       compcode = lcompcode | rcompcode;
3070       break;
3071
3072     default:
3073       return NULL_TREE;
3074     }
3075
3076   if (!honor_nans)
3077     {
3078       /* Eliminate unordered comparisons, as well as LTGT and ORD
3079          which are not used unless the mode has NaNs.  */
3080       compcode &= ~COMPCODE_UNORD;
3081       if (compcode == COMPCODE_LTGT)
3082         compcode = COMPCODE_NE;
3083       else if (compcode == COMPCODE_ORD)
3084         compcode = COMPCODE_TRUE;
3085     }
3086    else if (flag_trapping_math)
3087      {
3088         /* Check that the original operation and the optimized ones will trap
3089            under the same condition.  */
3090         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
3091                      && (lcompcode != COMPCODE_EQ)
3092                      && (lcompcode != COMPCODE_ORD);
3093         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
3094                      && (rcompcode != COMPCODE_EQ)
3095                      && (rcompcode != COMPCODE_ORD);
3096         bool trap = (compcode & COMPCODE_UNORD) == 0
3097                     && (compcode != COMPCODE_EQ)
3098                     && (compcode != COMPCODE_ORD);
3099
3100         /* In a short-circuited boolean expression the LHS might be
3101            such that the RHS, if evaluated, will never trap.  For
3102            example, in ORD (x, y) && (x < y), we evaluate the RHS only
3103            if neither x nor y is NaN.  (This is a mixed blessing: for
3104            example, the expression above will never trap, hence
3105            optimizing it to x < y would be invalid).  */
3106         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3107             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3108           rtrap = false;
3109
3110         /* If the comparison was short-circuited, and only the RHS
3111            trapped, we may now generate a spurious trap.  */
3112         if (rtrap && !ltrap
3113             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3114           return NULL_TREE;
3115
3116         /* If we changed the conditions that cause a trap, we lose.  */
3117         if ((ltrap || rtrap) != trap)
3118           return NULL_TREE;
3119       }
3120
3121   if (compcode == COMPCODE_TRUE)
3122     return constant_boolean_node (true, truth_type);
3123   else if (compcode == COMPCODE_FALSE)
3124     return constant_boolean_node (false, truth_type);
3125   else
3126     {
3127       enum tree_code tcode;
3128
3129       tcode = compcode_to_comparison ((enum comparison_code) compcode);
3130       return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
3131     }
3132 }
3133 \f
3134 /* Return nonzero if two operands (typically of the same tree node)
3135    are necessarily equal.  If either argument has side-effects this
3136    function returns zero.  FLAGS modifies behavior as follows:
3137
3138    If OEP_ONLY_CONST is set, only return nonzero for constants.
3139    This function tests whether the operands are indistinguishable;
3140    it does not test whether they are equal using C's == operation.
3141    The distinction is important for IEEE floating point, because
3142    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3143    (2) two NaNs may be indistinguishable, but NaN!=NaN.
3144
3145    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3146    even though it may hold multiple values during a function.
3147    This is because a GCC tree node guarantees that nothing else is
3148    executed between the evaluation of its "operands" (which may often
3149    be evaluated in arbitrary order).  Hence if the operands themselves
3150    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3151    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
3152    unset means assuming isochronic (or instantaneous) tree equivalence.
3153    Unless comparing arbitrary expression trees, such as from different
3154    statements, this flag can usually be left unset.
3155
3156    If OEP_PURE_SAME is set, then pure functions with identical arguments
3157    are considered the same.  It is used when the caller has other ways
3158    to ensure that global memory is unchanged in between.  */
3159
3160 int
3161 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3162 {
3163   /* If either is ERROR_MARK, they aren't equal.  */
3164   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3165     return 0;
3166
3167   /* Check equality of integer constants before bailing out due to
3168      precision differences.  */
3169   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3170     return tree_int_cst_equal (arg0, arg1);
3171
3172   /* If both types don't have the same signedness, then we can't consider
3173      them equal.  We must check this before the STRIP_NOPS calls
3174      because they may change the signedness of the arguments.  As pointers
3175      strictly don't have a signedness, require either two pointers or
3176      two non-pointers as well.  */
3177   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3178       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
3179     return 0;
3180
3181   /* We cannot consider pointers to different address space equal.  */
3182   if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
3183       && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
3184           != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
3185     return 0;
3186
3187   /* If both types don't have the same precision, then it is not safe
3188      to strip NOPs.  */
3189   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3190     return 0;
3191
3192   STRIP_NOPS (arg0);
3193   STRIP_NOPS (arg1);
3194
3195   /* In case both args are comparisons but with different comparison
3196      code, try to swap the comparison operands of one arg to produce
3197      a match and compare that variant.  */
3198   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3199       && COMPARISON_CLASS_P (arg0)
3200       && COMPARISON_CLASS_P (arg1))
3201     {
3202       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3203
3204       if (TREE_CODE (arg0) == swap_code)
3205         return operand_equal_p (TREE_OPERAND (arg0, 0),
3206                                 TREE_OPERAND (arg1, 1), flags)
3207                && operand_equal_p (TREE_OPERAND (arg0, 1),
3208                                    TREE_OPERAND (arg1, 0), flags);
3209     }
3210
3211   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3212       /* This is needed for conversions and for COMPONENT_REF.
3213          Might as well play it safe and always test this.  */
3214       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3215       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3216       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3217     return 0;
3218
3219   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3220      We don't care about side effects in that case because the SAVE_EXPR
3221      takes care of that for us. In all other cases, two expressions are
3222      equal if they have no side effects.  If we have two identical
3223      expressions with side effects that should be treated the same due
3224      to the only side effects being identical SAVE_EXPR's, that will
3225      be detected in the recursive calls below.  */
3226   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3227       && (TREE_CODE (arg0) == SAVE_EXPR
3228           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3229     return 1;
3230
3231   /* Next handle constant cases, those for which we can return 1 even
3232      if ONLY_CONST is set.  */
3233   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3234     switch (TREE_CODE (arg0))
3235       {
3236       case INTEGER_CST:
3237         return tree_int_cst_equal (arg0, arg1);
3238
3239       case FIXED_CST:
3240         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3241                                        TREE_FIXED_CST (arg1));
3242
3243       case REAL_CST:
3244         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3245                                    TREE_REAL_CST (arg1)))
3246           return 1;
3247
3248
3249         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3250           {
3251             /* If we do not distinguish between signed and unsigned zero,
3252                consider them equal.  */
3253             if (real_zerop (arg0) && real_zerop (arg1))
3254               return 1;
3255           }
3256         return 0;
3257
3258       case VECTOR_CST:
3259         {
3260           tree v1, v2;
3261
3262           v1 = TREE_VECTOR_CST_ELTS (arg0);
3263           v2 = TREE_VECTOR_CST_ELTS (arg1);
3264           while (v1 && v2)
3265             {
3266               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3267                                     flags))
3268                 return 0;
3269               v1 = TREE_CHAIN (v1);
3270               v2 = TREE_CHAIN (v2);
3271             }
3272
3273           return v1 == v2;
3274         }
3275
3276       case COMPLEX_CST:
3277         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3278                                  flags)
3279                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3280                                     flags));
3281
3282       case STRING_CST:
3283         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3284                 && ! memcmp (TREE_STRING_POINTER (arg0),
3285                               TREE_STRING_POINTER (arg1),
3286                               TREE_STRING_LENGTH (arg0)));
3287
3288       case ADDR_EXPR:
3289         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3290                                 0);
3291       default:
3292         break;
3293       }
3294
3295   if (flags & OEP_ONLY_CONST)
3296     return 0;
3297
3298 /* Define macros to test an operand from arg0 and arg1 for equality and a
3299    variant that allows null and views null as being different from any
3300    non-null value.  In the latter case, if either is null, the both
3301    must be; otherwise, do the normal comparison.  */
3302 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
3303                                     TREE_OPERAND (arg1, N), flags)
3304
3305 #define OP_SAME_WITH_NULL(N)                            \
3306   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3307    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3308
3309   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3310     {
3311     case tcc_unary:
3312       /* Two conversions are equal only if signedness and modes match.  */
3313       switch (TREE_CODE (arg0))
3314         {
3315         CASE_CONVERT:
3316         case FIX_TRUNC_EXPR:
3317           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3318               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3319             return 0;
3320           break;
3321         default:
3322           break;
3323         }
3324
3325       return OP_SAME (0);
3326
3327
3328     case tcc_comparison:
3329     case tcc_binary:
3330       if (OP_SAME (0) && OP_SAME (1))
3331         return 1;
3332
3333       /* For commutative ops, allow the other order.  */
3334       return (commutative_tree_code (TREE_CODE (arg0))
3335               && operand_equal_p (TREE_OPERAND (arg0, 0),
3336                                   TREE_OPERAND (arg1, 1), flags)
3337               && operand_equal_p (TREE_OPERAND (arg0, 1),
3338                                   TREE_OPERAND (arg1, 0), flags));
3339
3340     case tcc_reference:
3341       /* If either of the pointer (or reference) expressions we are
3342          dereferencing contain a side effect, these cannot be equal.  */
3343       if (TREE_SIDE_EFFECTS (arg0)
3344           || TREE_SIDE_EFFECTS (arg1))
3345         return 0;
3346
3347       switch (TREE_CODE (arg0))
3348         {
3349         case INDIRECT_REF:
3350         case ALIGN_INDIRECT_REF:
3351         case MISALIGNED_INDIRECT_REF:
3352         case REALPART_EXPR:
3353         case IMAGPART_EXPR:
3354           return OP_SAME (0);
3355
3356         case ARRAY_REF:
3357         case ARRAY_RANGE_REF:
3358           /* Operands 2 and 3 may be null.
3359              Compare the array index by value if it is constant first as we
3360              may have different types but same value here.  */
3361           return (OP_SAME (0)
3362                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3363                                           TREE_OPERAND (arg1, 1))
3364                       || OP_SAME (1))
3365                   && OP_SAME_WITH_NULL (2)
3366                   && OP_SAME_WITH_NULL (3));
3367
3368         case COMPONENT_REF:
3369           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
3370              may be NULL when we're called to compare MEM_EXPRs.  */
3371           return OP_SAME_WITH_NULL (0)
3372                  && OP_SAME (1)
3373                  && OP_SAME_WITH_NULL (2);
3374
3375         case BIT_FIELD_REF:
3376           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3377
3378         default:
3379           return 0;
3380         }
3381
3382     case tcc_expression:
3383       switch (TREE_CODE (arg0))
3384         {
3385         case ADDR_EXPR:
3386         case TRUTH_NOT_EXPR:
3387           return OP_SAME (0);
3388
3389         case TRUTH_ANDIF_EXPR:
3390         case TRUTH_ORIF_EXPR:
3391           return OP_SAME (0) && OP_SAME (1);
3392
3393         case TRUTH_AND_EXPR:
3394         case TRUTH_OR_EXPR:
3395         case TRUTH_XOR_EXPR:
3396           if (OP_SAME (0) && OP_SAME (1))
3397             return 1;
3398
3399           /* Otherwise take into account this is a commutative operation.  */
3400           return (operand_equal_p (TREE_OPERAND (arg0, 0),
3401                                    TREE_OPERAND (arg1, 1), flags)
3402                   && operand_equal_p (TREE_OPERAND (arg0, 1),
3403                                       TREE_OPERAND (arg1, 0), flags));
3404
3405         case COND_EXPR:
3406           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3407
3408         default:
3409           return 0;
3410         }
3411
3412     case tcc_vl_exp:
3413       switch (TREE_CODE (arg0))
3414         {
3415         case CALL_EXPR:
3416           /* If the CALL_EXPRs call different functions, then they
3417              clearly can not be equal.  */
3418           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3419                                  flags))
3420             return 0;
3421
3422           {
3423             unsigned int cef = call_expr_flags (arg0);
3424             if (flags & OEP_PURE_SAME)
3425               cef &= ECF_CONST | ECF_PURE;
3426             else
3427               cef &= ECF_CONST;
3428             if (!cef)
3429               return 0;
3430           }
3431
3432           /* Now see if all the arguments are the same.  */
3433           {
3434             const_call_expr_arg_iterator iter0, iter1;
3435             const_tree a0, a1;
3436             for (a0 = first_const_call_expr_arg (arg0, &iter0),
3437                    a1 = first_const_call_expr_arg (arg1, &iter1);
3438                  a0 && a1;
3439                  a0 = next_const_call_expr_arg (&iter0),
3440                    a1 = next_const_call_expr_arg (&iter1))
3441               if (! operand_equal_p (a0, a1, flags))
3442                 return 0;
3443
3444             /* If we get here and both argument lists are exhausted
3445                then the CALL_EXPRs are equal.  */
3446             return ! (a0 || a1);
3447           }
3448         default:
3449           return 0;
3450         }
3451
3452     case tcc_declaration:
3453       /* Consider __builtin_sqrt equal to sqrt.  */
3454       return (TREE_CODE (arg0) == FUNCTION_DECL
3455               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3456               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3457               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3458
3459     default:
3460       return 0;
3461     }
3462
3463 #undef OP_SAME
3464 #undef OP_SAME_WITH_NULL
3465 }
3466 \f
3467 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3468    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3469
3470    When in doubt, return 0.  */
3471
3472 static int
3473 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3474 {
3475   int unsignedp1, unsignedpo;
3476   tree primarg0, primarg1, primother;
3477   unsigned int correct_width;
3478
3479   if (operand_equal_p (arg0, arg1, 0))
3480     return 1;
3481
3482   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3483       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3484     return 0;
3485
3486   /* Discard any conversions that don't change the modes of ARG0 and ARG1
3487      and see if the inner values are the same.  This removes any
3488      signedness comparison, which doesn't matter here.  */
3489   primarg0 = arg0, primarg1 = arg1;
3490   STRIP_NOPS (primarg0);
3491   STRIP_NOPS (primarg1);
3492   if (operand_equal_p (primarg0, primarg1, 0))
3493     return 1;
3494
3495   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3496      actual comparison operand, ARG0.
3497
3498      First throw away any conversions to wider types
3499      already present in the operands.  */
3500
3501   primarg1 = get_narrower (arg1, &unsignedp1);
3502   primother = get_narrower (other, &unsignedpo);
3503
3504   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3505   if (unsignedp1 == unsignedpo
3506       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3507       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3508     {
3509       tree type = TREE_TYPE (arg0);
3510
3511       /* Make sure shorter operand is extended the right way
3512          to match the longer operand.  */
3513       primarg1 = fold_convert (signed_or_unsigned_type_for
3514                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3515
3516       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3517         return 1;
3518     }
3519
3520   return 0;
3521 }
3522 \f
3523 /* See if ARG is an expression that is either a comparison or is performing
3524    arithmetic on comparisons.  The comparisons must only be comparing
3525    two different values, which will be stored in *CVAL1 and *CVAL2; if
3526    they are nonzero it means that some operands have already been found.
3527    No variables may be used anywhere else in the expression except in the
3528    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
3529    the expression and save_expr needs to be called with CVAL1 and CVAL2.
3530
3531    If this is true, return 1.  Otherwise, return zero.  */
3532
3533 static int
3534 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3535 {
3536   enum tree_code code = TREE_CODE (arg);
3537   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3538
3539   /* We can handle some of the tcc_expression cases here.  */
3540   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3541     tclass = tcc_unary;
3542   else if (tclass == tcc_expression
3543            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3544                || code == COMPOUND_EXPR))
3545     tclass = tcc_binary;
3546
3547   else if (tclass == tcc_expression && code == SAVE_EXPR
3548            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3549     {
3550       /* If we've already found a CVAL1 or CVAL2, this expression is
3551          two complex to handle.  */
3552       if (*cval1 || *cval2)
3553         return 0;
3554
3555       tclass = tcc_unary;
3556       *save_p = 1;
3557     }
3558
3559   switch (tclass)
3560     {
3561     case tcc_unary:
3562       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3563
3564     case tcc_binary:
3565       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3566               && twoval_comparison_p (TREE_OPERAND (arg, 1),
3567                                       cval1, cval2, save_p));
3568
3569     case tcc_constant:
3570       return 1;
3571
3572     case tcc_expression:
3573       if (code == COND_EXPR)
3574         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3575                                      cval1, cval2, save_p)
3576                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3577                                         cval1, cval2, save_p)
3578                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3579                                         cval1, cval2, save_p));
3580       return 0;
3581
3582     case tcc_comparison:
3583       /* First see if we can handle the first operand, then the second.  For
3584          the second operand, we know *CVAL1 can't be zero.  It must be that
3585          one side of the comparison is each of the values; test for the
3586          case where this isn't true by failing if the two operands
3587          are the same.  */
3588
3589       if (operand_equal_p (TREE_OPERAND (arg, 0),
3590                            TREE_OPERAND (arg, 1), 0))
3591         return 0;
3592
3593       if (*cval1 == 0)
3594         *cval1 = TREE_OPERAND (arg, 0);
3595       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3596         ;
3597       else if (*cval2 == 0)
3598         *cval2 = TREE_OPERAND (arg, 0);
3599       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3600         ;
3601       else
3602         return 0;
3603
3604       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3605         ;
3606       else if (*cval2 == 0)
3607         *cval2 = TREE_OPERAND (arg, 1);
3608       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3609         ;
3610       else
3611         return 0;
3612
3613       return 1;
3614
3615     default:
3616       return 0;
3617     }
3618 }
3619 \f
3620 /* ARG is a tree that is known to contain just arithmetic operations and
3621    comparisons.  Evaluate the operations in the tree substituting NEW0 for
3622    any occurrence of OLD0 as an operand of a comparison and likewise for
3623    NEW1 and OLD1.  */
3624
3625 static tree
3626 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3627             tree old1, tree new1)
3628 {
3629   tree type = TREE_TYPE (arg);
3630   enum tree_code code = TREE_CODE (arg);
3631   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3632
3633   /* We can handle some of the tcc_expression cases here.  */
3634   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3635     tclass = tcc_unary;
3636   else if (tclass == tcc_expression
3637            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3638     tclass = tcc_binary;
3639
3640   switch (tclass)
3641     {
3642     case tcc_unary:
3643       return fold_build1_loc (loc, code, type,
3644                           eval_subst (loc, TREE_OPERAND (arg, 0),
3645                                       old0, new0, old1, new1));
3646
3647     case tcc_binary:
3648       return fold_build2_loc (loc, code, type,
3649                           eval_subst (loc, TREE_OPERAND (arg, 0),
3650                                       old0, new0, old1, new1),
3651                           eval_subst (loc, TREE_OPERAND (arg, 1),
3652                                       old0, new0, old1, new1));
3653
3654     case tcc_expression:
3655       switch (code)
3656         {
3657         case SAVE_EXPR:
3658           return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3659                              old1, new1);
3660
3661         case COMPOUND_EXPR:
3662           return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3663                              old1, new1);
3664
3665         case COND_EXPR:
3666           return fold_build3_loc (loc, code, type,
3667                               eval_subst (loc, TREE_OPERAND (arg, 0),
3668                                           old0, new0, old1, new1),
3669                               eval_subst (loc, TREE_OPERAND (arg, 1),
3670                                           old0, new0, old1, new1),
3671                               eval_subst (loc, TREE_OPERAND (arg, 2),
3672                                           old0, new0, old1, new1));
3673         default:
3674           break;
3675         }
3676       /* Fall through - ???  */
3677
3678     case tcc_comparison:
3679       {
3680         tree arg0 = TREE_OPERAND (arg, 0);
3681         tree arg1 = TREE_OPERAND (arg, 1);
3682
3683         /* We need to check both for exact equality and tree equality.  The
3684            former will be true if the operand has a side-effect.  In that
3685            case, we know the operand occurred exactly once.  */
3686
3687         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3688           arg0 = new0;
3689         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3690           arg0 = new1;
3691
3692         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3693           arg1 = new0;
3694         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3695           arg1 = new1;
3696
3697         return fold_build2_loc (loc, code, type, arg0, arg1);
3698       }
3699
3700     default:
3701       return arg;
3702     }
3703 }
3704 \f
3705 /* Return a tree for the case when the result of an expression is RESULT
3706    converted to TYPE and OMITTED was previously an operand of the expression
3707    but is now not needed (e.g., we folded OMITTED * 0).
3708
3709    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
3710    the conversion of RESULT to TYPE.  */
3711
3712 tree
3713 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3714 {
3715   tree t = fold_convert_loc (loc, type, result);
3716
3717   /* If the resulting operand is an empty statement, just return the omitted
3718      statement casted to void. */
3719   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3720     {
3721       t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3722       goto omit_one_operand_exit;
3723     }
3724
3725   if (TREE_SIDE_EFFECTS (omitted))
3726     {
3727       t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3728       goto omit_one_operand_exit;
3729     }
3730
3731   return non_lvalue_loc (loc, t);
3732
3733  omit_one_operand_exit:
3734   protected_set_expr_location (t, loc);
3735   return t;
3736 }
3737
3738 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3739
3740 static tree
3741 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
3742                                tree omitted)
3743 {
3744   tree t = fold_convert_loc (loc, type, result);
3745
3746   /* If the resulting operand is an empty statement, just return the omitted
3747      statement casted to void. */
3748   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3749     {
3750       t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3751       goto pedantic_omit_one_operand_exit;
3752     }
3753
3754   if (TREE_SIDE_EFFECTS (omitted))
3755     {
3756       t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3757       goto pedantic_omit_one_operand_exit;
3758     }
3759
3760   return pedantic_non_lvalue_loc (loc, t);
3761
3762  pedantic_omit_one_operand_exit:
3763   protected_set_expr_location (t, loc);
3764   return t;
3765 }
3766
3767 /* Return a tree for the case when the result of an expression is RESULT
3768    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3769    of the expression but are now not needed.
3770
3771    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3772    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3773    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3774    just do the conversion of RESULT to TYPE.  */
3775
3776 tree
3777 omit_two_operands_loc (location_t loc, tree type, tree result,
3778                    tree omitted1, tree omitted2)
3779 {
3780   tree t = fold_convert_loc (loc, type, result);
3781
3782   if (TREE_SIDE_EFFECTS (omitted2))
3783     {
3784       t = build2 (COMPOUND_EXPR, type, omitted2, t);
3785       SET_EXPR_LOCATION (t, loc);
3786     }
3787   if (TREE_SIDE_EFFECTS (omitted1))
3788     {
3789       t = build2 (COMPOUND_EXPR, type, omitted1, t);
3790       SET_EXPR_LOCATION (t, loc);
3791     }
3792
3793   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3794 }
3795
3796 \f
3797 /* Return a simplified tree node for the truth-negation of ARG.  This
3798    never alters ARG itself.  We assume that ARG is an operation that
3799    returns a truth value (0 or 1).
3800
3801    FIXME: one would think we would fold the result, but it causes
3802    problems with the dominator optimizer.  */
3803
3804 tree
3805 fold_truth_not_expr (location_t loc, tree arg)
3806 {
3807   tree t, type = TREE_TYPE (arg);
3808   enum tree_code code = TREE_CODE (arg);
3809   location_t loc1, loc2;
3810
3811   /* If this is a comparison, we can simply invert it, except for
3812      floating-point non-equality comparisons, in which case we just
3813      enclose a TRUTH_NOT_EXPR around what we have.  */
3814
3815   if (TREE_CODE_CLASS (code) == tcc_comparison)
3816     {
3817       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3818       if (FLOAT_TYPE_P (op_type)
3819           && flag_trapping_math
3820           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3821           && code != NE_EXPR && code != EQ_EXPR)
3822         return NULL_TREE;
3823
3824       code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3825       if (code == ERROR_MARK)
3826         return NULL_TREE;
3827
3828       t = build2 (code, type, TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3829       SET_EXPR_LOCATION (t, loc);
3830       return t;
3831     }
3832
3833   switch (code)
3834     {
3835     case INTEGER_CST:
3836       return constant_boolean_node (integer_zerop (arg), type);
3837
3838     case TRUTH_AND_EXPR:
3839       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3840       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3841       if (loc1 == UNKNOWN_LOCATION)
3842         loc1 = loc;
3843       if (loc2 == UNKNOWN_LOCATION)
3844         loc2 = loc;
3845       t = build2 (TRUTH_OR_EXPR, type,
3846                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3847                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3848       break;
3849
3850     case TRUTH_OR_EXPR:
3851       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3852       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3853       if (loc1 == UNKNOWN_LOCATION)
3854         loc1 = loc;
3855       if (loc2 == UNKNOWN_LOCATION)
3856         loc2 = loc;
3857       t = build2 (TRUTH_AND_EXPR, type,
3858                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3859                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3860       break;
3861
3862     case TRUTH_XOR_EXPR:
3863       /* Here we can invert either operand.  We invert the first operand
3864          unless the second operand is a TRUTH_NOT_EXPR in which case our
3865          result is the XOR of the first operand with the inside of the
3866          negation of the second operand.  */
3867
3868       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3869         t = build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3870                     TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3871       else
3872         t = build2 (TRUTH_XOR_EXPR, type,
3873                     invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3874                     TREE_OPERAND (arg, 1));
3875       break;
3876
3877     case TRUTH_ANDIF_EXPR:
3878       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3879       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3880       if (loc1 == UNKNOWN_LOCATION)
3881         loc1 = loc;
3882       if (loc2 == UNKNOWN_LOCATION)
3883         loc2 = loc;
3884       t = build2 (TRUTH_ORIF_EXPR, type,
3885                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3886                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3887       break;
3888
3889     case TRUTH_ORIF_EXPR:
3890       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3891       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3892       if (loc1 == UNKNOWN_LOCATION)
3893         loc1 = loc;
3894       if (loc2 == UNKNOWN_LOCATION)
3895         loc2 = loc;
3896       t = build2 (TRUTH_ANDIF_EXPR, type,
3897                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3898                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3899       break;
3900
3901     case TRUTH_NOT_EXPR:
3902       return TREE_OPERAND (arg, 0);
3903
3904     case COND_EXPR:
3905       {
3906         tree arg1 = TREE_OPERAND (arg, 1);
3907         tree arg2 = TREE_OPERAND (arg, 2);
3908
3909         loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3910         loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 2));
3911         if (loc1 == UNKNOWN_LOCATION)
3912           loc1 = loc;
3913         if (loc2 == UNKNOWN_LOCATION)
3914           loc2 = loc;
3915
3916         /* A COND_EXPR may have a throw as one operand, which
3917            then has void type.  Just leave void operands
3918            as they are.  */
3919         t = build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3920                     VOID_TYPE_P (TREE_TYPE (arg1))
3921                     ? arg1 : invert_truthvalue_loc (loc1, arg1),
3922                     VOID_TYPE_P (TREE_TYPE (arg2))
3923                     ? arg2 : invert_truthvalue_loc (loc2, arg2));
3924         break;
3925       }
3926
3927     case COMPOUND_EXPR:
3928       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3929       if (loc1 == UNKNOWN_LOCATION)
3930         loc1 = loc;
3931       t = build2 (COMPOUND_EXPR, type,
3932                   TREE_OPERAND (arg, 0),
3933                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3934       break;
3935
3936     case NON_LVALUE_EXPR:
3937       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3938       if (loc1 == UNKNOWN_LOCATION)
3939         loc1 = loc;
3940       return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3941
3942     CASE_CONVERT:
3943       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3944         {
3945           t = build1 (TRUTH_NOT_EXPR, type, arg);
3946           break;
3947         }
3948
3949       /* ... fall through ...  */
3950
3951     case FLOAT_EXPR:
3952       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3953       if (loc1 == UNKNOWN_LOCATION)
3954         loc1 = loc;
3955       t = build1 (TREE_CODE (arg), type,
3956                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3957       break;
3958
3959     case BIT_AND_EXPR:
3960       if (!integer_onep (TREE_OPERAND (arg, 1)))
3961         return NULL_TREE;
3962       t = build2 (EQ_EXPR, type, arg, build_int_cst (type, 0));
3963       break;
3964
3965     case SAVE_EXPR:
3966       t = build1 (TRUTH_NOT_EXPR, type, arg);
3967       break;
3968
3969     case CLEANUP_POINT_EXPR:
3970       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3971       if (loc1 == UNKNOWN_LOCATION)
3972         loc1 = loc;
3973       t = build1 (CLEANUP_POINT_EXPR, type,
3974                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3975       break;
3976
3977     default:
3978       t = NULL_TREE;
3979       break;
3980     }
3981
3982   if (t)
3983     SET_EXPR_LOCATION (t, loc);
3984
3985   return t;
3986 }
3987
3988 /* Return a simplified tree node for the truth-negation of ARG.  This
3989    never alters ARG itself.  We assume that ARG is an operation that
3990    returns a truth value (0 or 1).
3991
3992    FIXME: one would think we would fold the result, but it causes
3993    problems with the dominator optimizer.  */
3994
3995 tree
3996 invert_truthvalue_loc (location_t loc, tree arg)
3997 {
3998   tree tem;
3999
4000   if (TREE_CODE (arg) == ERROR_MARK)
4001     return arg;
4002
4003   tem = fold_truth_not_expr (loc, arg);
4004   if (!tem)
4005     {
4006       tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
4007       SET_EXPR_LOCATION (tem, loc);
4008     }
4009
4010   return tem;
4011 }
4012
4013 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
4014    operands are another bit-wise operation with a common input.  If so,
4015    distribute the bit operations to save an operation and possibly two if
4016    constants are involved.  For example, convert
4017         (A | B) & (A | C) into A | (B & C)
4018    Further simplification will occur if B and C are constants.
4019
4020    If this optimization cannot be done, 0 will be returned.  */
4021
4022 static tree
4023 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
4024                      tree arg0, tree arg1)
4025 {
4026   tree common;
4027   tree left, right;
4028
4029   if (TREE_CODE (arg0) != TREE_CODE (arg1)
4030       || TREE_CODE (arg0) == code
4031       || (TREE_CODE (arg0) != BIT_AND_EXPR
4032           && TREE_CODE (arg0) != BIT_IOR_EXPR))
4033     return 0;
4034
4035   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
4036     {
4037       common = TREE_OPERAND (arg0, 0);
4038       left = TREE_OPERAND (arg0, 1);
4039       right = TREE_OPERAND (arg1, 1);
4040     }
4041   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
4042     {
4043       common = TREE_OPERAND (arg0, 0);
4044       left = TREE_OPERAND (arg0, 1);
4045       right = TREE_OPERAND (arg1, 0);
4046     }
4047   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
4048     {
4049       common = TREE_OPERAND (arg0, 1);
4050       left = TREE_OPERAND (arg0, 0);
4051       right = TREE_OPERAND (arg1, 1);
4052     }
4053   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
4054     {
4055       common = TREE_OPERAND (arg0, 1);
4056       left = TREE_OPERAND (arg0, 0);
4057       right = TREE_OPERAND (arg1, 0);
4058     }
4059   else
4060     return 0;
4061
4062   common = fold_convert_loc (loc, type, common);
4063   left = fold_convert_loc (loc, type, left);
4064   right = fold_convert_loc (loc, type, right);
4065   return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
4066                       fold_build2_loc (loc, code, type, left, right));
4067 }
4068
4069 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
4070    with code CODE.  This optimization is unsafe.  */
4071 static tree
4072 distribute_real_division (location_t loc, enum tree_code code, tree type,
4073                           tree arg0, tree arg1)
4074 {
4075   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
4076   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
4077
4078   /* (A / C) +- (B / C) -> (A +- B) / C.  */
4079   if (mul0 == mul1
4080       && operand_equal_p (TREE_OPERAND (arg0, 1),
4081                        TREE_OPERAND (arg1, 1), 0))
4082     return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
4083                         fold_build2_loc (loc, code, type,
4084                                      TREE_OPERAND (arg0, 0),
4085                                      TREE_OPERAND (arg1, 0)),
4086                         TREE_OPERAND (arg0, 1));
4087
4088   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
4089   if (operand_equal_p (TREE_OPERAND (arg0, 0),
4090                        TREE_OPERAND (arg1, 0), 0)
4091       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
4092       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
4093     {
4094       REAL_VALUE_TYPE r0, r1;
4095       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
4096       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
4097       if (!mul0)
4098         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
4099       if (!mul1)
4100         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
4101       real_arithmetic (&r0, code, &r0, &r1);
4102       return fold_build2_loc (loc, MULT_EXPR, type,
4103                           TREE_OPERAND (arg0, 0),
4104                           build_real (type, r0));
4105     }
4106
4107   return NULL_TREE;
4108 }
4109 \f
4110 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4111    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
4112
4113 static tree
4114 make_bit_field_ref (location_t loc, tree inner, tree type,
4115                     HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
4116 {
4117   tree result, bftype;
4118
4119   if (bitpos == 0)
4120     {
4121       tree size = TYPE_SIZE (TREE_TYPE (inner));
4122       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4123            || POINTER_TYPE_P (TREE_TYPE (inner)))
4124           && host_integerp (size, 0)
4125           && tree_low_cst (size, 0) == bitsize)
4126         return fold_convert_loc (loc, type, inner);
4127     }
4128
4129   bftype = type;
4130   if (TYPE_PRECISION (bftype) != bitsize
4131       || TYPE_UNSIGNED (bftype) == !unsignedp)
4132     bftype = build_nonstandard_integer_type (bitsize, 0);
4133
4134   result = build3 (BIT_FIELD_REF, bftype, inner,
4135                    size_int (bitsize), bitsize_int (bitpos));
4136   SET_EXPR_LOCATION (result, loc);
4137
4138   if (bftype != type)
4139     result = fold_convert_loc (loc, type, result);
4140
4141   return result;
4142 }
4143
4144 /* Optimize a bit-field compare.
4145
4146    There are two cases:  First is a compare against a constant and the
4147    second is a comparison of two items where the fields are at the same
4148    bit position relative to the start of a chunk (byte, halfword, word)
4149    large enough to contain it.  In these cases we can avoid the shift
4150    implicit in bitfield extractions.
4151
4152    For constants, we emit a compare of the shifted constant with the
4153    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4154    compared.  For two fields at the same position, we do the ANDs with the
4155    similar mask and compare the result of the ANDs.
4156
4157    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4158    COMPARE_TYPE is the type of the comparison, and LHS and RHS
4159    are the left and right operands of the comparison, respectively.
4160
4161    If the optimization described above can be done, we return the resulting
4162    tree.  Otherwise we return zero.  */
4163
4164 static tree
4165 optimize_bit_field_compare (location_t loc, enum tree_code code,
4166                             tree compare_type, tree lhs, tree rhs)
4167 {
4168   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
4169   tree type = TREE_TYPE (lhs);
4170   tree signed_type, unsigned_type;
4171   int const_p = TREE_CODE (rhs) == INTEGER_CST;
4172   enum machine_mode lmode, rmode, nmode;
4173   int lunsignedp, runsignedp;
4174   int lvolatilep = 0, rvolatilep = 0;
4175   tree linner, rinner = NULL_TREE;
4176   tree mask;
4177   tree offset;
4178
4179   /* Get all the information about the extractions being done.  If the bit size
4180      if the same as the size of the underlying object, we aren't doing an
4181      extraction at all and so can do nothing.  We also don't want to
4182      do anything if the inner expression is a PLACEHOLDER_EXPR since we
4183      then will no longer be able to replace it.  */
4184   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
4185                                 &lunsignedp, &lvolatilep, false);
4186   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
4187       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
4188     return 0;
4189
4190  if (!const_p)
4191    {
4192      /* If this is not a constant, we can only do something if bit positions,
4193         sizes, and signedness are the same.  */
4194      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4195                                    &runsignedp, &rvolatilep, false);
4196
4197      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
4198          || lunsignedp != runsignedp || offset != 0
4199          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
4200        return 0;
4201    }
4202
4203   /* See if we can find a mode to refer to this field.  We should be able to,
4204      but fail if we can't.  */
4205   nmode = get_best_mode (lbitsize, lbitpos,
4206                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4207                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4208                                 TYPE_ALIGN (TREE_TYPE (rinner))),
4209                          word_mode, lvolatilep || rvolatilep);
4210   if (nmode == VOIDmode)
4211     return 0;
4212
4213   /* Set signed and unsigned types of the precision of this mode for the
4214      shifts below.  */
4215   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
4216   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4217
4218   /* Compute the bit position and size for the new reference and our offset
4219      within it. If the new reference is the same size as the original, we
4220      won't optimize anything, so return zero.  */
4221   nbitsize = GET_MODE_BITSIZE (nmode);
4222   nbitpos = lbitpos & ~ (nbitsize - 1);
4223   lbitpos -= nbitpos;
4224   if (nbitsize == lbitsize)
4225     return 0;
4226
4227   if (BYTES_BIG_ENDIAN)
4228     lbitpos = nbitsize - lbitsize - lbitpos;
4229
4230   /* Make the mask to be used against the extracted field.  */
4231   mask = build_int_cst_type (unsigned_type, -1);
4232   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
4233   mask = const_binop (RSHIFT_EXPR, mask,
4234                       size_int (nbitsize - lbitsize - lbitpos), 0);
4235
4236   if (! const_p)
4237     /* If not comparing with constant, just rework the comparison
4238        and return.  */
4239     return fold_build2_loc (loc, code, compare_type,
4240                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4241                                      make_bit_field_ref (loc, linner,
4242                                                          unsigned_type,
4243                                                          nbitsize, nbitpos,
4244                                                          1),
4245                                      mask),
4246                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4247                                      make_bit_field_ref (loc, rinner,
4248                                                          unsigned_type,
4249                                                          nbitsize, nbitpos,
4250                                                          1),
4251                                      mask));
4252
4253   /* Otherwise, we are handling the constant case. See if the constant is too
4254      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
4255      this not only for its own sake, but to avoid having to test for this
4256      error case below.  If we didn't, we might generate wrong code.
4257
4258      For unsigned fields, the constant shifted right by the field length should
4259      be all zero.  For signed fields, the high-order bits should agree with
4260      the sign bit.  */
4261
4262   if (lunsignedp)
4263     {
4264       if (! integer_zerop (const_binop (RSHIFT_EXPR,
4265                                         fold_convert_loc (loc,
4266                                                           unsigned_type, rhs),
4267                                         size_int (lbitsize), 0)))
4268         {
4269           warning (0, "comparison is always %d due to width of bit-field",
4270                    code == NE_EXPR);
4271           return constant_boolean_node (code == NE_EXPR, compare_type);
4272         }
4273     }
4274   else
4275     {
4276       tree tem = const_binop (RSHIFT_EXPR,
4277                               fold_convert_loc (loc, signed_type, rhs),
4278                               size_int (lbitsize - 1), 0);
4279       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
4280         {
4281           warning (0, "comparison is always %d due to width of bit-field",
4282                    code == NE_EXPR);
4283           return constant_boolean_node (code == NE_EXPR, compare_type);
4284         }
4285     }
4286
4287   /* Single-bit compares should always be against zero.  */
4288   if (lbitsize == 1 && ! integer_zerop (rhs))
4289     {
4290       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4291       rhs = build_int_cst (type, 0);
4292     }
4293
4294   /* Make a new bitfield reference, shift the constant over the
4295      appropriate number of bits and mask it with the computed mask
4296      (in case this was a signed field).  If we changed it, make a new one.  */
4297   lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
4298   if (lvolatilep)
4299     {
4300       TREE_SIDE_EFFECTS (lhs) = 1;
4301       TREE_THIS_VOLATILE (lhs) = 1;
4302     }
4303
4304   rhs = const_binop (BIT_AND_EXPR,
4305                      const_binop (LSHIFT_EXPR,
4306                                   fold_convert_loc (loc, unsigned_type, rhs),
4307                                   size_int (lbitpos), 0),
4308                      mask, 0);
4309
4310   lhs = build2 (code, compare_type,
4311                 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4312                 rhs);
4313   SET_EXPR_LOCATION (lhs, loc);
4314   return lhs;
4315 }
4316 \f
4317 /* Subroutine for fold_truthop: decode a field reference.
4318
4319    If EXP is a comparison reference, we return the innermost reference.
4320
4321    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4322    set to the starting bit number.
4323
4324    If the innermost field can be completely contained in a mode-sized
4325    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
4326
4327    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4328    otherwise it is not changed.
4329
4330    *PUNSIGNEDP is set to the signedness of the field.
4331
4332    *PMASK is set to the mask used.  This is either contained in a
4333    BIT_AND_EXPR or derived from the width of the field.
4334
4335    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4336
4337    Return 0 if this is not a component reference or is one that we can't
4338    do anything with.  */
4339
4340 static tree
4341 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
4342                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4343                         int *punsignedp, int *pvolatilep,
4344                         tree *pmask, tree *pand_mask)
4345 {
4346   tree outer_type = 0;
4347   tree and_mask = 0;
4348   tree mask, inner, offset;
4349   tree unsigned_type;
4350   unsigned int precision;
4351
4352   /* All the optimizations using this function assume integer fields.
4353      There are problems with FP fields since the type_for_size call
4354      below can fail for, e.g., XFmode.  */
4355   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4356     return 0;
4357
4358   /* We are interested in the bare arrangement of bits, so strip everything
4359      that doesn't affect the machine mode.  However, record the type of the
4360      outermost expression if it may matter below.  */
4361   if (CONVERT_EXPR_P (exp)
4362       || TREE_CODE (exp) == NON_LVALUE_EXPR)
4363     outer_type = TREE_TYPE (exp);
4364   STRIP_NOPS (exp);
4365
4366   if (TREE_CODE (exp) == BIT_AND_EXPR)
4367     {
4368       and_mask = TREE_OPERAND (exp, 1);
4369       exp = TREE_OPERAND (exp, 0);
4370       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4371       if (TREE_CODE (and_mask) != INTEGER_CST)
4372         return 0;
4373     }
4374
4375   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4376                                punsignedp, pvolatilep, false);
4377   if ((inner == exp && and_mask == 0)
4378       || *pbitsize < 0 || offset != 0
4379       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4380     return 0;
4381
4382   /* If the number of bits in the reference is the same as the bitsize of
4383      the outer type, then the outer type gives the signedness. Otherwise
4384      (in case of a small bitfield) the signedness is unchanged.  */
4385   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4386     *punsignedp = TYPE_UNSIGNED (outer_type);
4387
4388   /* Compute the mask to access the bitfield.  */
4389   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4390   precision = TYPE_PRECISION (unsigned_type);
4391
4392   mask = build_int_cst_type (unsigned_type, -1);
4393
4394   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4395   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4396
4397   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
4398   if (and_mask != 0)
4399     mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4400                         fold_convert_loc (loc, unsigned_type, and_mask), mask);
4401
4402   *pmask = mask;
4403   *pand_mask = and_mask;
4404   return inner;
4405 }
4406
4407 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4408    bit positions.  */
4409
4410 static int
4411 all_ones_mask_p (const_tree mask, int size)
4412 {
4413   tree type = TREE_TYPE (mask);
4414   unsigned int precision = TYPE_PRECISION (type);
4415   tree tmask;
4416
4417   tmask = build_int_cst_type (signed_type_for (type), -1);
4418
4419   return
4420     tree_int_cst_equal (mask,
4421                         const_binop (RSHIFT_EXPR,
4422                                      const_binop (LSHIFT_EXPR, tmask,
4423                                                   size_int (precision - size),
4424                                                   0),
4425                                      size_int (precision - size), 0));
4426 }
4427
4428 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4429    represents the sign bit of EXP's type.  If EXP represents a sign
4430    or zero extension, also test VAL against the unextended type.
4431    The return value is the (sub)expression whose sign bit is VAL,
4432    or NULL_TREE otherwise.  */
4433
4434 static tree
4435 sign_bit_p (tree exp, const_tree val)
4436 {
4437   unsigned HOST_WIDE_INT mask_lo, lo;
4438   HOST_WIDE_INT mask_hi, hi;
4439   int width;
4440   tree t;
4441
4442   /* Tree EXP must have an integral type.  */
4443   t = TREE_TYPE (exp);
4444   if (! INTEGRAL_TYPE_P (t))
4445     return NULL_TREE;
4446
4447   /* Tree VAL must be an integer constant.  */
4448   if (TREE_CODE (val) != INTEGER_CST
4449       || TREE_OVERFLOW (val))
4450     return NULL_TREE;
4451
4452   width = TYPE_PRECISION (t);
4453   if (width > HOST_BITS_PER_WIDE_INT)
4454     {
4455       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4456       lo = 0;
4457
4458       mask_hi = ((unsigned HOST_WIDE_INT) -1
4459                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
4460       mask_lo = -1;
4461     }
4462   else
4463     {
4464       hi = 0;
4465       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4466
4467       mask_hi = 0;
4468       mask_lo = ((unsigned HOST_WIDE_INT) -1
4469                  >> (HOST_BITS_PER_WIDE_INT - width));
4470     }
4471
4472   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4473      treat VAL as if it were unsigned.  */
4474   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4475       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4476     return exp;
4477
4478   /* Handle extension from a narrower type.  */
4479   if (TREE_CODE (exp) == NOP_EXPR
4480       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4481     return sign_bit_p (TREE_OPERAND (exp, 0), val);
4482
4483   return NULL_TREE;
4484 }
4485
4486 /* Subroutine for fold_truthop: determine if an operand is simple enough
4487    to be evaluated unconditionally.  */
4488
4489 static int
4490 simple_operand_p (const_tree exp)
4491 {
4492   /* Strip any conversions that don't change the machine mode.  */
4493   STRIP_NOPS (exp);
4494
4495   return (CONSTANT_CLASS_P (exp)
4496           || TREE_CODE (exp) == SSA_NAME
4497           || (DECL_P (exp)
4498               && ! TREE_ADDRESSABLE (exp)
4499               && ! TREE_THIS_VOLATILE (exp)
4500               && ! DECL_NONLOCAL (exp)
4501               /* Don't regard global variables as simple.  They may be
4502                  allocated in ways unknown to the compiler (shared memory,
4503                  #pragma weak, etc).  */
4504               && ! TREE_PUBLIC (exp)
4505               && ! DECL_EXTERNAL (exp)
4506               /* Loading a static variable is unduly expensive, but global
4507                  registers aren't expensive.  */
4508               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4509 }
4510 \f
4511 /* The following functions are subroutines to fold_range_test and allow it to
4512    try to change a logical combination of comparisons into a range test.
4513
4514    For example, both
4515         X == 2 || X == 3 || X == 4 || X == 5
4516    and
4517         X >= 2 && X <= 5
4518    are converted to
4519         (unsigned) (X - 2) <= 3
4520
4521    We describe each set of comparisons as being either inside or outside
4522    a range, using a variable named like IN_P, and then describe the
4523    range with a lower and upper bound.  If one of the bounds is omitted,
4524    it represents either the highest or lowest value of the type.
4525
4526    In the comments below, we represent a range by two numbers in brackets
4527    preceded by a "+" to designate being inside that range, or a "-" to
4528    designate being outside that range, so the condition can be inverted by
4529    flipping the prefix.  An omitted bound is represented by a "-".  For
4530    example, "- [-, 10]" means being outside the range starting at the lowest
4531    possible value and ending at 10, in other words, being greater than 10.
4532    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4533    always false.
4534
4535    We set up things so that the missing bounds are handled in a consistent
4536    manner so neither a missing bound nor "true" and "false" need to be
4537    handled using a special case.  */
4538
4539 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4540    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4541    and UPPER1_P are nonzero if the respective argument is an upper bound
4542    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
4543    must be specified for a comparison.  ARG1 will be converted to ARG0's
4544    type if both are specified.  */
4545
4546 static tree
4547 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4548              tree arg1, int upper1_p)
4549 {
4550   tree tem;
4551   int result;
4552   int sgn0, sgn1;
4553
4554   /* If neither arg represents infinity, do the normal operation.
4555      Else, if not a comparison, return infinity.  Else handle the special
4556      comparison rules. Note that most of the cases below won't occur, but
4557      are handled for consistency.  */
4558
4559   if (arg0 != 0 && arg1 != 0)
4560     {
4561       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4562                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
4563       STRIP_NOPS (tem);
4564       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4565     }
4566
4567   if (TREE_CODE_CLASS (code) != tcc_comparison)
4568     return 0;
4569
4570   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4571      for neither.  In real maths, we cannot assume open ended ranges are
4572      the same. But, this is computer arithmetic, where numbers are finite.
4573      We can therefore make the transformation of any unbounded range with
4574      the value Z, Z being greater than any representable number. This permits
4575      us to treat unbounded ranges as equal.  */
4576   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4577   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4578   switch (code)
4579     {
4580     case EQ_EXPR:
4581       result = sgn0 == sgn1;
4582       break;
4583     case NE_EXPR:
4584       result = sgn0 != sgn1;
4585       break;
4586     case LT_EXPR:
4587       result = sgn0 < sgn1;
4588       break;
4589     case LE_EXPR:
4590       result = sgn0 <= sgn1;
4591       break;
4592     case GT_EXPR:
4593       result = sgn0 > sgn1;
4594       break;
4595     case GE_EXPR:
4596       result = sgn0 >= sgn1;
4597       break;
4598     default:
4599       gcc_unreachable ();
4600     }
4601
4602   return constant_boolean_node (result, type);
4603 }
4604 \f
4605 /* Given EXP, a logical expression, set the range it is testing into
4606    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4607    actually being tested.  *PLOW and *PHIGH will be made of the same
4608    type as the returned expression.  If EXP is not a comparison, we
4609    will most likely not be returning a useful value and range.  Set
4610    *STRICT_OVERFLOW_P to true if the return value is only valid
4611    because signed overflow is undefined; otherwise, do not change
4612    *STRICT_OVERFLOW_P.  */
4613
4614 tree
4615 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4616             bool *strict_overflow_p)
4617 {
4618   enum tree_code code;
4619   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4620   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4621   int in_p, n_in_p;
4622   tree low, high, n_low, n_high;
4623   location_t loc = EXPR_LOCATION (exp);
4624
4625   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4626      and see if we can refine the range.  Some of the cases below may not
4627      happen, but it doesn't seem worth worrying about this.  We "continue"
4628      the outer loop when we've changed something; otherwise we "break"
4629      the switch, which will "break" the while.  */
4630
4631   in_p = 0;
4632   low = high = build_int_cst (TREE_TYPE (exp), 0);
4633
4634   while (1)
4635     {
4636       code = TREE_CODE (exp);
4637       exp_type = TREE_TYPE (exp);
4638
4639       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4640         {
4641           if (TREE_OPERAND_LENGTH (exp) > 0)
4642             arg0 = TREE_OPERAND (exp, 0);
4643           if (TREE_CODE_CLASS (code) == tcc_comparison
4644               || TREE_CODE_CLASS (code) == tcc_unary
4645               || TREE_CODE_CLASS (code) == tcc_binary)
4646             arg0_type = TREE_TYPE (arg0);
4647           if (TREE_CODE_CLASS (code) == tcc_binary
4648               || TREE_CODE_CLASS (code) == tcc_comparison
4649               || (TREE_CODE_CLASS (code) == tcc_expression
4650                   && TREE_OPERAND_LENGTH (exp) > 1))
4651             arg1 = TREE_OPERAND (exp, 1);
4652         }
4653
4654       switch (code)
4655         {
4656         case TRUTH_NOT_EXPR:
4657           in_p = ! in_p, exp = arg0;
4658           continue;
4659
4660         case EQ_EXPR: case NE_EXPR:
4661         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4662           /* We can only do something if the range is testing for zero
4663              and if the second operand is an integer constant.  Note that
4664              saying something is "in" the range we make is done by
4665              complementing IN_P since it will set in the initial case of
4666              being not equal to zero; "out" is leaving it alone.  */
4667           if (low == 0 || high == 0
4668               || ! integer_zerop (low) || ! integer_zerop (high)
4669               || TREE_CODE (arg1) != INTEGER_CST)
4670             break;
4671
4672           switch (code)
4673             {
4674             case NE_EXPR:  /* - [c, c]  */
4675               low = high = arg1;
4676               break;
4677             case EQ_EXPR:  /* + [c, c]  */
4678               in_p = ! in_p, low = high = arg1;
4679               break;
4680             case GT_EXPR:  /* - [-, c] */
4681               low = 0, high = arg1;
4682               break;
4683             case GE_EXPR:  /* + [c, -] */
4684               in_p = ! in_p, low = arg1, high = 0;
4685               break;
4686             case LT_EXPR:  /* - [c, -] */
4687               low = arg1, high = 0;
4688               break;
4689             case LE_EXPR:  /* + [-, c] */
4690               in_p = ! in_p, low = 0, high = arg1;
4691               break;
4692             default:
4693               gcc_unreachable ();
4694             }
4695
4696           /* If this is an unsigned comparison, we also know that EXP is
4697              greater than or equal to zero.  We base the range tests we make
4698              on that fact, so we record it here so we can parse existing
4699              range tests.  We test arg0_type since often the return type
4700              of, e.g. EQ_EXPR, is boolean.  */
4701           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4702             {
4703               if (! merge_ranges (&n_in_p, &n_low, &n_high,
4704                                   in_p, low, high, 1,
4705                                   build_int_cst (arg0_type, 0),
4706                                   NULL_TREE))
4707                 break;
4708
4709               in_p = n_in_p, low = n_low, high = n_high;
4710
4711               /* If the high bound is missing, but we have a nonzero low
4712                  bound, reverse the range so it goes from zero to the low bound
4713                  minus 1.  */
4714               if (high == 0 && low && ! integer_zerop (low))
4715                 {
4716                   in_p = ! in_p;
4717                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4718                                       integer_one_node, 0);
4719                   low = build_int_cst (arg0_type, 0);
4720                 }
4721             }
4722
4723           exp = arg0;
4724           continue;
4725
4726         case NEGATE_EXPR:
4727           /* (-x) IN [a,b] -> x in [-b, -a]  */
4728           n_low = range_binop (MINUS_EXPR, exp_type,
4729                                build_int_cst (exp_type, 0),
4730                                0, high, 1);
4731           n_high = range_binop (MINUS_EXPR, exp_type,
4732                                 build_int_cst (exp_type, 0),
4733                                 0, low, 0);
4734           low = n_low, high = n_high;
4735           exp = arg0;
4736           continue;
4737
4738         case BIT_NOT_EXPR:
4739           /* ~ X -> -X - 1  */
4740           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4741                         build_int_cst (exp_type, 1));
4742           SET_EXPR_LOCATION (exp, loc);
4743           continue;
4744
4745         case PLUS_EXPR:  case MINUS_EXPR:
4746           if (TREE_CODE (arg1) != INTEGER_CST)
4747             break;
4748
4749           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4750              move a constant to the other side.  */
4751           if (!TYPE_UNSIGNED (arg0_type)
4752               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4753             break;
4754
4755           /* If EXP is signed, any overflow in the computation is undefined,
4756              so we don't worry about it so long as our computations on
4757              the bounds don't overflow.  For unsigned, overflow is defined
4758              and this is exactly the right thing.  */
4759           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4760                                arg0_type, low, 0, arg1, 0);
4761           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4762                                 arg0_type, high, 1, arg1, 0);
4763           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4764               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4765             break;
4766
4767           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4768             *strict_overflow_p = true;
4769
4770           /* Check for an unsigned range which has wrapped around the maximum
4771              value thus making n_high < n_low, and normalize it.  */
4772           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4773             {
4774               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4775                                  integer_one_node, 0);
4776               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4777                                   integer_one_node, 0);
4778
4779               /* If the range is of the form +/- [ x+1, x ], we won't
4780                  be able to normalize it.  But then, it represents the
4781                  whole range or the empty set, so make it
4782                  +/- [ -, - ].  */
4783               if (tree_int_cst_equal (n_low, low)
4784                   && tree_int_cst_equal (n_high, high))
4785                 low = high = 0;
4786               else
4787                 in_p = ! in_p;
4788             }
4789           else
4790             low = n_low, high = n_high;
4791
4792           exp = arg0;
4793           continue;
4794
4795         CASE_CONVERT: case NON_LVALUE_EXPR:
4796           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4797             break;
4798
4799           if (! INTEGRAL_TYPE_P (arg0_type)
4800               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4801               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4802             break;
4803
4804           n_low = low, n_high = high;
4805
4806           if (n_low != 0)
4807             n_low = fold_convert_loc (loc, arg0_type, n_low);
4808
4809           if (n_high != 0)
4810             n_high = fold_convert_loc (loc, arg0_type, n_high);
4811
4812
4813           /* If we're converting arg0 from an unsigned type, to exp,
4814              a signed type,  we will be doing the comparison as unsigned.
4815              The tests above have already verified that LOW and HIGH
4816              are both positive.
4817
4818              So we have to ensure that we will handle large unsigned
4819              values the same way that the current signed bounds treat
4820              negative values.  */
4821
4822           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4823             {
4824               tree high_positive;
4825               tree equiv_type;
4826               /* For fixed-point modes, we need to pass the saturating flag
4827                  as the 2nd parameter.  */
4828               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4829                 equiv_type = lang_hooks.types.type_for_mode
4830                              (TYPE_MODE (arg0_type),
4831                               TYPE_SATURATING (arg0_type));
4832               else
4833                 equiv_type = lang_hooks.types.type_for_mode
4834                              (TYPE_MODE (arg0_type), 1);
4835
4836               /* A range without an upper bound is, naturally, unbounded.
4837                  Since convert would have cropped a very large value, use
4838                  the max value for the destination type.  */
4839               high_positive
4840                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4841                 : TYPE_MAX_VALUE (arg0_type);
4842
4843               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4844                 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4845                                              fold_convert_loc (loc, arg0_type,
4846                                                                high_positive),
4847                                              build_int_cst (arg0_type, 1));
4848
4849               /* If the low bound is specified, "and" the range with the
4850                  range for which the original unsigned value will be
4851                  positive.  */
4852               if (low != 0)
4853                 {
4854                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4855                                       1, n_low, n_high, 1,
4856                                       fold_convert_loc (loc, arg0_type,
4857                                                         integer_zero_node),
4858                                       high_positive))
4859                     break;
4860
4861                   in_p = (n_in_p == in_p);
4862                 }
4863               else
4864                 {
4865                   /* Otherwise, "or" the range with the range of the input
4866                      that will be interpreted as negative.  */
4867                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4868                                       0, n_low, n_high, 1,
4869                                       fold_convert_loc (loc, arg0_type,
4870                                                         integer_zero_node),
4871                                       high_positive))
4872                     break;
4873
4874                   in_p = (in_p != n_in_p);
4875                 }
4876             }
4877
4878           exp = arg0;
4879           low = n_low, high = n_high;
4880           continue;
4881
4882         default:
4883           break;
4884         }
4885
4886       break;
4887     }
4888
4889   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4890   if (TREE_CODE (exp) == INTEGER_CST)
4891     {
4892       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4893                                                  exp, 0, low, 0))
4894                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4895                                                     exp, 1, high, 1)));
4896       low = high = 0;
4897       exp = 0;
4898     }
4899
4900   *pin_p = in_p, *plow = low, *phigh = high;
4901   return exp;
4902 }
4903 \f
4904 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4905    type, TYPE, return an expression to test if EXP is in (or out of, depending
4906    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4907
4908 tree
4909 build_range_check (location_t loc, tree type, tree exp, int in_p,
4910                    tree low, tree high)
4911 {
4912   tree etype = TREE_TYPE (exp), value;
4913
4914 #ifdef HAVE_canonicalize_funcptr_for_compare
4915   /* Disable this optimization for function pointer expressions
4916      on targets that require function pointer canonicalization.  */
4917   if (HAVE_canonicalize_funcptr_for_compare
4918       && TREE_CODE (etype) == POINTER_TYPE
4919       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4920     return NULL_TREE;
4921 #endif
4922
4923   if (! in_p)
4924     {
4925       value = build_range_check (loc, type, exp, 1, low, high);
4926       if (value != 0)
4927         return invert_truthvalue_loc (loc, value);
4928
4929       return 0;
4930     }
4931
4932   if (low == 0 && high == 0)
4933     return build_int_cst (type, 1);
4934
4935   if (low == 0)
4936     return fold_build2_loc (loc, LE_EXPR, type, exp,
4937                         fold_convert_loc (loc, etype, high));
4938
4939   if (high == 0)
4940     return fold_build2_loc (loc, GE_EXPR, type, exp,
4941                         fold_convert_loc (loc, etype, low));
4942
4943   if (operand_equal_p (low, high, 0))
4944     return fold_build2_loc (loc, EQ_EXPR, type, exp,
4945                         fold_convert_loc (loc, etype, low));
4946
4947   if (integer_zerop (low))
4948     {
4949       if (! TYPE_UNSIGNED (etype))
4950         {
4951           etype = unsigned_type_for (etype);
4952           high = fold_convert_loc (loc, etype, high);
4953           exp = fold_convert_loc (loc, etype, exp);
4954         }
4955       return build_range_check (loc, type, exp, 1, 0, high);
4956     }
4957
4958   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4959   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4960     {
4961       unsigned HOST_WIDE_INT lo;
4962       HOST_WIDE_INT hi;
4963       int prec;
4964
4965       prec = TYPE_PRECISION (etype);
4966       if (prec <= HOST_BITS_PER_WIDE_INT)
4967         {
4968           hi = 0;
4969           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4970         }
4971       else
4972         {
4973           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4974           lo = (unsigned HOST_WIDE_INT) -1;
4975         }
4976
4977       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4978         {
4979           if (TYPE_UNSIGNED (etype))
4980             {
4981               tree signed_etype = signed_type_for (etype);
4982               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4983                 etype
4984                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4985               else
4986                 etype = signed_etype;
4987               exp = fold_convert_loc (loc, etype, exp);
4988             }
4989           return fold_build2_loc (loc, GT_EXPR, type, exp,
4990                               build_int_cst (etype, 0));
4991         }
4992     }
4993
4994   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4995      This requires wrap-around arithmetics for the type of the expression.
4996      First make sure that arithmetics in this type is valid, then make sure
4997      that it wraps around.  */
4998   if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4999     etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
5000                                             TYPE_UNSIGNED (etype));
5001
5002   if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
5003     {
5004       tree utype, minv, maxv;
5005
5006       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5007          for the type in question, as we rely on this here.  */
5008       utype = unsigned_type_for (etype);
5009       maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
5010       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5011                           integer_one_node, 1);
5012       minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
5013
5014       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5015                                       minv, 1, maxv, 1)))
5016         etype = utype;
5017       else
5018         return 0;
5019     }
5020
5021   high = fold_convert_loc (loc, etype, high);
5022   low = fold_convert_loc (loc, etype, low);
5023   exp = fold_convert_loc (loc, etype, exp);
5024
5025   value = const_binop (MINUS_EXPR, high, low, 0);
5026
5027
5028   if (POINTER_TYPE_P (etype))
5029     {
5030       if (value != 0 && !TREE_OVERFLOW (value))
5031         {
5032           low = fold_convert_loc (loc, sizetype, low);
5033           low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low);
5034           return build_range_check (loc, type,
5035                                     fold_build2_loc (loc, POINTER_PLUS_EXPR,
5036                                                  etype, exp, low),
5037                                     1, build_int_cst (etype, 0), value);
5038         }
5039       return 0;
5040     }
5041
5042   if (value != 0 && !TREE_OVERFLOW (value))
5043     return build_range_check (loc, type,
5044                               fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5045                               1, build_int_cst (etype, 0), value);
5046
5047   return 0;
5048 }
5049 \f
5050 /* Return the predecessor of VAL in its type, handling the infinite case.  */
5051
5052 static tree
5053 range_predecessor (tree val)
5054 {
5055   tree type = TREE_TYPE (val);
5056
5057   if (INTEGRAL_TYPE_P (type)
5058       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5059     return 0;
5060   else
5061     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
5062 }
5063
5064 /* Return the successor of VAL in its type, handling the infinite case.  */
5065
5066 static tree
5067 range_successor (tree val)
5068 {
5069   tree type = TREE_TYPE (val);
5070
5071   if (INTEGRAL_TYPE_P (type)
5072       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5073     return 0;
5074   else
5075     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
5076 }
5077
5078 /* Given two ranges, see if we can merge them into one.  Return 1 if we
5079    can, 0 if we can't.  Set the output range into the specified parameters.  */
5080
5081 bool
5082 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5083               tree high0, int in1_p, tree low1, tree high1)
5084 {
5085   int no_overlap;
5086   int subset;
5087   int temp;
5088   tree tem;
5089   int in_p;
5090   tree low, high;
5091   int lowequal = ((low0 == 0 && low1 == 0)
5092                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5093                                                 low0, 0, low1, 0)));
5094   int highequal = ((high0 == 0 && high1 == 0)
5095                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5096                                                  high0, 1, high1, 1)));
5097
5098   /* Make range 0 be the range that starts first, or ends last if they
5099      start at the same value.  Swap them if it isn't.  */
5100   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5101                                  low0, 0, low1, 0))
5102       || (lowequal
5103           && integer_onep (range_binop (GT_EXPR, integer_type_node,
5104                                         high1, 1, high0, 1))))
5105     {
5106       temp = in0_p, in0_p = in1_p, in1_p = temp;
5107       tem = low0, low0 = low1, low1 = tem;
5108       tem = high0, high0 = high1, high1 = tem;
5109     }
5110
5111   /* Now flag two cases, whether the ranges are disjoint or whether the
5112      second range is totally subsumed in the first.  Note that the tests
5113      below are simplified by the ones above.  */
5114   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5115                                           high0, 1, low1, 0));
5116   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5117                                       high1, 1, high0, 1));
5118
5119   /* We now have four cases, depending on whether we are including or
5120      excluding the two ranges.  */
5121   if (in0_p && in1_p)
5122     {
5123       /* If they don't overlap, the result is false.  If the second range
5124          is a subset it is the result.  Otherwise, the range is from the start
5125          of the second to the end of the first.  */
5126       if (no_overlap)
5127         in_p = 0, low = high = 0;
5128       else if (subset)
5129         in_p = 1, low = low1, high = high1;
5130       else
5131         in_p = 1, low = low1, high = high0;
5132     }
5133
5134   else if (in0_p && ! in1_p)
5135     {
5136       /* If they don't overlap, the result is the first range.  If they are
5137          equal, the result is false.  If the second range is a subset of the
5138          first, and the ranges begin at the same place, we go from just after
5139          the end of the second range to the end of the first.  If the second
5140          range is not a subset of the first, or if it is a subset and both
5141          ranges end at the same place, the range starts at the start of the
5142          first range and ends just before the second range.
5143          Otherwise, we can't describe this as a single range.  */
5144       if (no_overlap)
5145         in_p = 1, low = low0, high = high0;
5146       else if (lowequal && highequal)
5147         in_p = 0, low = high = 0;
5148       else if (subset && lowequal)
5149         {
5150           low = range_successor (high1);
5151           high = high0;
5152           in_p = 1;
5153           if (low == 0)
5154             {
5155               /* We are in the weird situation where high0 > high1 but
5156                  high1 has no successor.  Punt.  */
5157               return 0;
5158             }
5159         }
5160       else if (! subset || highequal)
5161         {
5162           low = low0;
5163           high = range_predecessor (low1);
5164           in_p = 1;
5165           if (high == 0)
5166             {
5167               /* low0 < low1 but low1 has no predecessor.  Punt.  */
5168               return 0;
5169             }
5170         }
5171       else
5172         return 0;
5173     }
5174
5175   else if (! in0_p && in1_p)
5176     {
5177       /* If they don't overlap, the result is the second range.  If the second
5178          is a subset of the first, the result is false.  Otherwise,
5179          the range starts just after the first range and ends at the
5180          end of the second.  */
5181       if (no_overlap)
5182         in_p = 1, low = low1, high = high1;
5183       else if (subset || highequal)
5184         in_p = 0, low = high = 0;
5185       else
5186         {
5187           low = range_successor (high0);
5188           high = high1;
5189           in_p = 1;
5190           if (low == 0)
5191             {
5192               /* high1 > high0 but high0 has no successor.  Punt.  */
5193               return 0;
5194             }
5195         }
5196     }
5197
5198   else
5199     {
5200       /* The case where we are excluding both ranges.  Here the complex case
5201          is if they don't overlap.  In that case, the only time we have a
5202          range is if they are adjacent.  If the second is a subset of the
5203          first, the result is the first.  Otherwise, the range to exclude
5204          starts at the beginning of the first range and ends at the end of the
5205          second.  */
5206       if (no_overlap)
5207         {
5208           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5209                                          range_successor (high0),
5210                                          1, low1, 0)))
5211             in_p = 0, low = low0, high = high1;
5212           else
5213             {
5214               /* Canonicalize - [min, x] into - [-, x].  */
5215               if (low0 && TREE_CODE (low0) == INTEGER_CST)
5216                 switch (TREE_CODE (TREE_TYPE (low0)))
5217                   {
5218                   case ENUMERAL_TYPE:
5219                     if (TYPE_PRECISION (TREE_TYPE (low0))
5220                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
5221                       break;
5222                     /* FALLTHROUGH */
5223                   case INTEGER_TYPE:
5224                     if (tree_int_cst_equal (low0,
5225                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
5226                       low0 = 0;
5227                     break;
5228                   case POINTER_TYPE:
5229                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
5230                         && integer_zerop (low0))
5231                       low0 = 0;
5232                     break;
5233                   default:
5234                     break;
5235                   }
5236
5237               /* Canonicalize - [x, max] into - [x, -].  */
5238               if (high1 && TREE_CODE (high1) == INTEGER_CST)
5239                 switch (TREE_CODE (TREE_TYPE (high1)))
5240                   {
5241                   case ENUMERAL_TYPE:
5242                     if (TYPE_PRECISION (TREE_TYPE (high1))
5243                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5244                       break;
5245                     /* FALLTHROUGH */
5246                   case INTEGER_TYPE:
5247                     if (tree_int_cst_equal (high1,
5248                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
5249                       high1 = 0;
5250                     break;
5251                   case POINTER_TYPE:
5252                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
5253                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5254                                                        high1, 1,
5255                                                        integer_one_node, 1)))
5256                       high1 = 0;
5257                     break;
5258                   default:
5259                     break;
5260                   }
5261
5262               /* The ranges might be also adjacent between the maximum and
5263                  minimum values of the given type.  For
5264                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5265                  return + [x + 1, y - 1].  */
5266               if (low0 == 0 && high1 == 0)
5267                 {
5268                   low = range_successor (high0);
5269                   high = range_predecessor (low1);
5270                   if (low == 0 || high == 0)
5271                     return 0;
5272
5273                   in_p = 1;
5274                 }
5275               else
5276                 return 0;
5277             }
5278         }
5279       else if (subset)
5280         in_p = 0, low = low0, high = high0;
5281       else
5282         in_p = 0, low = low0, high = high1;
5283     }
5284
5285   *pin_p = in_p, *plow = low, *phigh = high;
5286   return 1;
5287 }
5288 \f
5289
5290 /* Subroutine of fold, looking inside expressions of the form
5291    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5292    of the COND_EXPR.  This function is being used also to optimize
5293    A op B ? C : A, by reversing the comparison first.
5294
5295    Return a folded expression whose code is not a COND_EXPR
5296    anymore, or NULL_TREE if no folding opportunity is found.  */
5297
5298 static tree
5299 fold_cond_expr_with_comparison (location_t loc, tree type,
5300                                 tree arg0, tree arg1, tree arg2)
5301 {
5302   enum tree_code comp_code = TREE_CODE (arg0);
5303   tree arg00 = TREE_OPERAND (arg0, 0);
5304   tree arg01 = TREE_OPERAND (arg0, 1);
5305   tree arg1_type = TREE_TYPE (arg1);
5306   tree tem;
5307
5308   STRIP_NOPS (arg1);
5309   STRIP_NOPS (arg2);
5310
5311   /* If we have A op 0 ? A : -A, consider applying the following
5312      transformations:
5313
5314      A == 0? A : -A    same as -A
5315      A != 0? A : -A    same as A
5316      A >= 0? A : -A    same as abs (A)
5317      A > 0?  A : -A    same as abs (A)
5318      A <= 0? A : -A    same as -abs (A)
5319      A < 0?  A : -A    same as -abs (A)
5320
5321      None of these transformations work for modes with signed
5322      zeros.  If A is +/-0, the first two transformations will
5323      change the sign of the result (from +0 to -0, or vice
5324      versa).  The last four will fix the sign of the result,
5325      even though the original expressions could be positive or
5326      negative, depending on the sign of A.
5327
5328      Note that all these transformations are correct if A is
5329      NaN, since the two alternatives (A and -A) are also NaNs.  */
5330   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5331       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5332           ? real_zerop (arg01)
5333           : integer_zerop (arg01))
5334       && ((TREE_CODE (arg2) == NEGATE_EXPR
5335            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5336              /* In the case that A is of the form X-Y, '-A' (arg2) may
5337                 have already been folded to Y-X, check for that. */
5338           || (TREE_CODE (arg1) == MINUS_EXPR
5339               && TREE_CODE (arg2) == MINUS_EXPR
5340               && operand_equal_p (TREE_OPERAND (arg1, 0),
5341                                   TREE_OPERAND (arg2, 1), 0)
5342               && operand_equal_p (TREE_OPERAND (arg1, 1),
5343                                   TREE_OPERAND (arg2, 0), 0))))
5344     switch (comp_code)
5345       {
5346       case EQ_EXPR:
5347       case UNEQ_EXPR:
5348         tem = fold_convert_loc (loc, arg1_type, arg1);
5349         return pedantic_non_lvalue_loc (loc,
5350                                     fold_convert_loc (loc, type,
5351                                                   negate_expr (tem)));
5352       case NE_EXPR:
5353       case LTGT_EXPR:
5354         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5355       case UNGE_EXPR:
5356       case UNGT_EXPR:
5357         if (flag_trapping_math)
5358           break;
5359         /* Fall through.  */
5360       case GE_EXPR:
5361       case GT_EXPR:
5362         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5363           arg1 = fold_convert_loc (loc, signed_type_for
5364                                (TREE_TYPE (arg1)), arg1);
5365         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5366         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5367       case UNLE_EXPR:
5368       case UNLT_EXPR:
5369         if (flag_trapping_math)
5370           break;
5371       case LE_EXPR:
5372       case LT_EXPR:
5373         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5374           arg1 = fold_convert_loc (loc, signed_type_for
5375                                (TREE_TYPE (arg1)), arg1);
5376         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5377         return negate_expr (fold_convert_loc (loc, type, tem));
5378       default:
5379         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5380         break;
5381       }
5382
5383   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
5384      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
5385      both transformations are correct when A is NaN: A != 0
5386      is then true, and A == 0 is false.  */
5387
5388   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5389       && integer_zerop (arg01) && integer_zerop (arg2))
5390     {
5391       if (comp_code == NE_EXPR)
5392         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5393       else if (comp_code == EQ_EXPR)
5394         return build_int_cst (type, 0);
5395     }
5396
5397   /* Try some transformations of A op B ? A : B.
5398
5399      A == B? A : B    same as B
5400      A != B? A : B    same as A
5401      A >= B? A : B    same as max (A, B)
5402      A > B?  A : B    same as max (B, A)
5403      A <= B? A : B    same as min (A, B)
5404      A < B?  A : B    same as min (B, A)
5405
5406      As above, these transformations don't work in the presence
5407      of signed zeros.  For example, if A and B are zeros of
5408      opposite sign, the first two transformations will change
5409      the sign of the result.  In the last four, the original
5410      expressions give different results for (A=+0, B=-0) and
5411      (A=-0, B=+0), but the transformed expressions do not.
5412
5413      The first two transformations are correct if either A or B
5414      is a NaN.  In the first transformation, the condition will
5415      be false, and B will indeed be chosen.  In the case of the
5416      second transformation, the condition A != B will be true,
5417      and A will be chosen.
5418
5419      The conversions to max() and min() are not correct if B is
5420      a number and A is not.  The conditions in the original
5421      expressions will be false, so all four give B.  The min()
5422      and max() versions would give a NaN instead.  */
5423   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5424       && operand_equal_for_comparison_p (arg01, arg2, arg00)
5425       /* Avoid these transformations if the COND_EXPR may be used
5426          as an lvalue in the C++ front-end.  PR c++/19199.  */
5427       && (in_gimple_form
5428           || (strcmp (lang_hooks.name, "GNU C++") != 0
5429               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5430           || ! maybe_lvalue_p (arg1)
5431           || ! maybe_lvalue_p (arg2)))
5432     {
5433       tree comp_op0 = arg00;
5434       tree comp_op1 = arg01;
5435       tree comp_type = TREE_TYPE (comp_op0);
5436
5437       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
5438       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5439         {
5440           comp_type = type;
5441           comp_op0 = arg1;
5442           comp_op1 = arg2;
5443         }
5444
5445       switch (comp_code)
5446         {
5447         case EQ_EXPR:
5448           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
5449         case NE_EXPR:
5450           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5451         case LE_EXPR:
5452         case LT_EXPR:
5453         case UNLE_EXPR:
5454         case UNLT_EXPR:
5455           /* In C++ a ?: expression can be an lvalue, so put the
5456              operand which will be used if they are equal first
5457              so that we can convert this back to the
5458              corresponding COND_EXPR.  */
5459           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5460             {
5461               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5462               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5463               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5464                     ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5465                     : fold_build2_loc (loc, MIN_EXPR, comp_type,
5466                                    comp_op1, comp_op0);
5467               return pedantic_non_lvalue_loc (loc,
5468                                           fold_convert_loc (loc, type, tem));
5469             }
5470           break;
5471         case GE_EXPR:
5472         case GT_EXPR:
5473         case UNGE_EXPR:
5474         case UNGT_EXPR:
5475           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5476             {
5477               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5478               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5479               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5480                     ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5481                     : fold_build2_loc (loc, MAX_EXPR, comp_type,
5482                                    comp_op1, comp_op0);
5483               return pedantic_non_lvalue_loc (loc,
5484                                           fold_convert_loc (loc, type, tem));
5485             }
5486           break;
5487         case UNEQ_EXPR:
5488           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5489             return pedantic_non_lvalue_loc (loc,
5490                                         fold_convert_loc (loc, type, arg2));
5491           break;
5492         case LTGT_EXPR:
5493           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5494             return pedantic_non_lvalue_loc (loc,
5495                                         fold_convert_loc (loc, type, arg1));
5496           break;
5497         default:
5498           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5499           break;
5500         }
5501     }
5502
5503   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5504      we might still be able to simplify this.  For example,
5505      if C1 is one less or one more than C2, this might have started
5506      out as a MIN or MAX and been transformed by this function.
5507      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
5508
5509   if (INTEGRAL_TYPE_P (type)
5510       && TREE_CODE (arg01) == INTEGER_CST
5511       && TREE_CODE (arg2) == INTEGER_CST)
5512     switch (comp_code)
5513       {
5514       case EQ_EXPR:
5515         if (TREE_CODE (arg1) == INTEGER_CST)
5516           break;
5517         /* We can replace A with C1 in this case.  */
5518         arg1 = fold_convert_loc (loc, type, arg01);
5519         return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5520
5521       case LT_EXPR:
5522         /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5523            MIN_EXPR, to preserve the signedness of the comparison.  */
5524         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5525                                OEP_ONLY_CONST)
5526             && operand_equal_p (arg01,
5527                                 const_binop (PLUS_EXPR, arg2,
5528                                              build_int_cst (type, 1), 0),
5529                                 OEP_ONLY_CONST))
5530           {
5531             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5532                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5533                                                      arg2));
5534             return pedantic_non_lvalue_loc (loc,
5535                                             fold_convert_loc (loc, type, tem));
5536           }
5537         break;
5538
5539       case LE_EXPR:
5540         /* If C1 is C2 - 1, this is min(A, C2), with the same care
5541            as above.  */
5542         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5543                                OEP_ONLY_CONST)
5544             && operand_equal_p (arg01,
5545                                 const_binop (MINUS_EXPR, arg2,
5546                                              build_int_cst (type, 1), 0),
5547                                 OEP_ONLY_CONST))
5548           {
5549             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5550                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5551                                                      arg2));
5552             return pedantic_non_lvalue_loc (loc,
5553                                             fold_convert_loc (loc, type, tem));
5554           }
5555         break;
5556
5557       case GT_EXPR:
5558         /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5559            MAX_EXPR, to preserve the signedness of the comparison.  */
5560         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5561                                OEP_ONLY_CONST)
5562             && operand_equal_p (arg01,
5563                                 const_binop (MINUS_EXPR, arg2,
5564                                              build_int_cst (type, 1), 0),
5565                                 OEP_ONLY_CONST))
5566           {
5567             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5568                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5569                                                      arg2));
5570             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5571           }
5572         break;
5573
5574       case GE_EXPR:
5575         /* If C1 is C2 + 1, this is max(A, C2), with the same care as above.  */
5576         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5577                                OEP_ONLY_CONST)
5578             && operand_equal_p (arg01,
5579                                 const_binop (PLUS_EXPR, arg2,
5580                                              build_int_cst (type, 1), 0),
5581                                 OEP_ONLY_CONST))
5582           {
5583             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5584                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5585                                                      arg2));
5586             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5587           }
5588         break;
5589       case NE_EXPR:
5590         break;
5591       default:
5592         gcc_unreachable ();
5593       }
5594
5595   return NULL_TREE;
5596 }
5597
5598
5599 \f
5600 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5601 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5602   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5603                 false) >= 2)
5604 #endif
5605
5606 /* EXP is some logical combination of boolean tests.  See if we can
5607    merge it into some range test.  Return the new tree if so.  */
5608
5609 static tree
5610 fold_range_test (location_t loc, enum tree_code code, tree type,
5611                  tree op0, tree op1)
5612 {
5613   int or_op = (code == TRUTH_ORIF_EXPR
5614                || code == TRUTH_OR_EXPR);
5615   int in0_p, in1_p, in_p;
5616   tree low0, low1, low, high0, high1, high;
5617   bool strict_overflow_p = false;
5618   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5619   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5620   tree tem;
5621   const char * const warnmsg = G_("assuming signed overflow does not occur "
5622                                   "when simplifying range test");
5623
5624   /* If this is an OR operation, invert both sides; we will invert
5625      again at the end.  */
5626   if (or_op)
5627     in0_p = ! in0_p, in1_p = ! in1_p;
5628
5629   /* If both expressions are the same, if we can merge the ranges, and we
5630      can build the range test, return it or it inverted.  If one of the
5631      ranges is always true or always false, consider it to be the same
5632      expression as the other.  */
5633   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5634       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5635                        in1_p, low1, high1)
5636       && 0 != (tem = (build_range_check (UNKNOWN_LOCATION, type,
5637                                          lhs != 0 ? lhs
5638                                          : rhs != 0 ? rhs : integer_zero_node,
5639                                          in_p, low, high))))
5640     {
5641       if (strict_overflow_p)
5642         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5643       return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5644     }
5645
5646   /* On machines where the branch cost is expensive, if this is a
5647      short-circuited branch and the underlying object on both sides
5648      is the same, make a non-short-circuit operation.  */
5649   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5650            && lhs != 0 && rhs != 0
5651            && (code == TRUTH_ANDIF_EXPR
5652                || code == TRUTH_ORIF_EXPR)
5653            && operand_equal_p (lhs, rhs, 0))
5654     {
5655       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
5656          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5657          which cases we can't do this.  */
5658       if (simple_operand_p (lhs))
5659         {
5660           tem = build2 (code == TRUTH_ANDIF_EXPR
5661                         ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5662                         type, op0, op1);
5663           SET_EXPR_LOCATION (tem, loc);
5664           return tem;
5665         }
5666
5667       else if (lang_hooks.decls.global_bindings_p () == 0
5668                && ! CONTAINS_PLACEHOLDER_P (lhs))
5669         {
5670           tree common = save_expr (lhs);
5671
5672           if (0 != (lhs = build_range_check (loc, type, common,
5673                                              or_op ? ! in0_p : in0_p,
5674                                              low0, high0))
5675               && (0 != (rhs = build_range_check (loc, type, common,
5676                                                  or_op ? ! in1_p : in1_p,
5677                                                  low1, high1))))
5678             {
5679               if (strict_overflow_p)
5680                 fold_overflow_warning (warnmsg,
5681                                        WARN_STRICT_OVERFLOW_COMPARISON);
5682               tem = build2 (code == TRUTH_ANDIF_EXPR
5683                             ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5684                             type, lhs, rhs);
5685               SET_EXPR_LOCATION (tem, loc);
5686               return tem;
5687             }
5688         }
5689     }
5690
5691   return 0;
5692 }
5693 \f
5694 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5695    bit value.  Arrange things so the extra bits will be set to zero if and
5696    only if C is signed-extended to its full width.  If MASK is nonzero,
5697    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
5698
5699 static tree
5700 unextend (tree c, int p, int unsignedp, tree mask)
5701 {
5702   tree type = TREE_TYPE (c);
5703   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5704   tree temp;
5705
5706   if (p == modesize || unsignedp)
5707     return c;
5708
5709   /* We work by getting just the sign bit into the low-order bit, then
5710      into the high-order bit, then sign-extend.  We then XOR that value
5711      with C.  */
5712   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5713   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5714
5715   /* We must use a signed type in order to get an arithmetic right shift.
5716      However, we must also avoid introducing accidental overflows, so that
5717      a subsequent call to integer_zerop will work.  Hence we must
5718      do the type conversion here.  At this point, the constant is either
5719      zero or one, and the conversion to a signed type can never overflow.
5720      We could get an overflow if this conversion is done anywhere else.  */
5721   if (TYPE_UNSIGNED (type))
5722     temp = fold_convert (signed_type_for (type), temp);
5723
5724   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5725   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5726   if (mask != 0)
5727     temp = const_binop (BIT_AND_EXPR, temp,
5728                         fold_convert (TREE_TYPE (c), mask),
5729                         0);
5730   /* If necessary, convert the type back to match the type of C.  */
5731   if (TYPE_UNSIGNED (type))
5732     temp = fold_convert (type, temp);
5733
5734   return fold_convert (type,
5735                        const_binop (BIT_XOR_EXPR, c, temp, 0));
5736 }
5737 \f
5738 /* Find ways of folding logical expressions of LHS and RHS:
5739    Try to merge two comparisons to the same innermost item.
5740    Look for range tests like "ch >= '0' && ch <= '9'".
5741    Look for combinations of simple terms on machines with expensive branches
5742    and evaluate the RHS unconditionally.
5743
5744    For example, if we have p->a == 2 && p->b == 4 and we can make an
5745    object large enough to span both A and B, we can do this with a comparison
5746    against the object ANDed with the a mask.
5747
5748    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5749    operations to do this with one comparison.
5750
5751    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5752    function and the one above.
5753
5754    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5755    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5756
5757    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5758    two operands.
5759
5760    We return the simplified tree or 0 if no optimization is possible.  */
5761
5762 static tree
5763 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5764               tree lhs, tree rhs)
5765 {
5766   /* If this is the "or" of two comparisons, we can do something if
5767      the comparisons are NE_EXPR.  If this is the "and", we can do something
5768      if the comparisons are EQ_EXPR.  I.e.,
5769         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5770
5771      WANTED_CODE is this operation code.  For single bit fields, we can
5772      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5773      comparison for one-bit fields.  */
5774
5775   enum tree_code wanted_code;
5776   enum tree_code lcode, rcode;
5777   tree ll_arg, lr_arg, rl_arg, rr_arg;
5778   tree ll_inner, lr_inner, rl_inner, rr_inner;
5779   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5780   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5781   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5782   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5783   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5784   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5785   enum machine_mode lnmode, rnmode;
5786   tree ll_mask, lr_mask, rl_mask, rr_mask;
5787   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5788   tree l_const, r_const;
5789   tree lntype, rntype, result;
5790   HOST_WIDE_INT first_bit, end_bit;
5791   int volatilep;
5792   tree orig_lhs = lhs, orig_rhs = rhs;
5793   enum tree_code orig_code = code;
5794
5795   /* Start by getting the comparison codes.  Fail if anything is volatile.
5796      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5797      it were surrounded with a NE_EXPR.  */
5798
5799   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5800     return 0;
5801
5802   lcode = TREE_CODE (lhs);
5803   rcode = TREE_CODE (rhs);
5804
5805   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5806     {
5807       lhs = build2 (NE_EXPR, truth_type, lhs,
5808                     build_int_cst (TREE_TYPE (lhs), 0));
5809       lcode = NE_EXPR;
5810     }
5811
5812   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5813     {
5814       rhs = build2 (NE_EXPR, truth_type, rhs,
5815                     build_int_cst (TREE_TYPE (rhs), 0));
5816       rcode = NE_EXPR;
5817     }
5818
5819   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5820       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5821     return 0;
5822
5823   ll_arg = TREE_OPERAND (lhs, 0);
5824   lr_arg = TREE_OPERAND (lhs, 1);
5825   rl_arg = TREE_OPERAND (rhs, 0);
5826   rr_arg = TREE_OPERAND (rhs, 1);
5827
5828   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5829   if (simple_operand_p (ll_arg)
5830       && simple_operand_p (lr_arg))
5831     {
5832       tree result;
5833       if (operand_equal_p (ll_arg, rl_arg, 0)
5834           && operand_equal_p (lr_arg, rr_arg, 0))
5835         {
5836           result = combine_comparisons (loc, code, lcode, rcode,
5837                                         truth_type, ll_arg, lr_arg);
5838           if (result)
5839             return result;
5840         }
5841       else if (operand_equal_p (ll_arg, rr_arg, 0)
5842                && operand_equal_p (lr_arg, rl_arg, 0))
5843         {
5844           result = combine_comparisons (loc, code, lcode,
5845                                         swap_tree_comparison (rcode),
5846                                         truth_type, ll_arg, lr_arg);
5847           if (result)
5848             return result;
5849         }
5850     }
5851
5852   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5853           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5854
5855   /* If the RHS can be evaluated unconditionally and its operands are
5856      simple, it wins to evaluate the RHS unconditionally on machines
5857      with expensive branches.  In this case, this isn't a comparison
5858      that can be merged.  Avoid doing this if the RHS is a floating-point
5859      comparison since those can trap.  */
5860
5861   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5862                    false) >= 2
5863       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5864       && simple_operand_p (rl_arg)
5865       && simple_operand_p (rr_arg))
5866     {
5867       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5868       if (code == TRUTH_OR_EXPR
5869           && lcode == NE_EXPR && integer_zerop (lr_arg)
5870           && rcode == NE_EXPR && integer_zerop (rr_arg)
5871           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5872           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5873         {
5874           result = build2 (NE_EXPR, truth_type,
5875                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5876                                    ll_arg, rl_arg),
5877                            build_int_cst (TREE_TYPE (ll_arg), 0));
5878           goto fold_truthop_exit;
5879         }
5880
5881       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5882       if (code == TRUTH_AND_EXPR
5883           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5884           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5885           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5886           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5887         {
5888           result = build2 (EQ_EXPR, truth_type,
5889                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5890                                    ll_arg, rl_arg),
5891                            build_int_cst (TREE_TYPE (ll_arg), 0));
5892           goto fold_truthop_exit;
5893         }
5894
5895       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5896         {
5897           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5898             {
5899               result = build2 (code, truth_type, lhs, rhs);
5900               goto fold_truthop_exit;
5901             }
5902           return NULL_TREE;
5903         }
5904     }
5905
5906   /* See if the comparisons can be merged.  Then get all the parameters for
5907      each side.  */
5908
5909   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5910       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5911     return 0;
5912
5913   volatilep = 0;
5914   ll_inner = decode_field_reference (loc, ll_arg,
5915                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5916                                      &ll_unsignedp, &volatilep, &ll_mask,
5917                                      &ll_and_mask);
5918   lr_inner = decode_field_reference (loc, lr_arg,
5919                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5920                                      &lr_unsignedp, &volatilep, &lr_mask,
5921                                      &lr_and_mask);
5922   rl_inner = decode_field_reference (loc, rl_arg,
5923                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5924                                      &rl_unsignedp, &volatilep, &rl_mask,
5925                                      &rl_and_mask);
5926   rr_inner = decode_field_reference (loc, rr_arg,
5927                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5928                                      &rr_unsignedp, &volatilep, &rr_mask,
5929                                      &rr_and_mask);
5930
5931   /* It must be true that the inner operation on the lhs of each
5932      comparison must be the same if we are to be able to do anything.
5933      Then see if we have constants.  If not, the same must be true for
5934      the rhs's.  */
5935   if (volatilep || ll_inner == 0 || rl_inner == 0
5936       || ! operand_equal_p (ll_inner, rl_inner, 0))
5937     return 0;
5938
5939   if (TREE_CODE (lr_arg) == INTEGER_CST
5940       && TREE_CODE (rr_arg) == INTEGER_CST)
5941     l_const = lr_arg, r_const = rr_arg;
5942   else if (lr_inner == 0 || rr_inner == 0
5943            || ! operand_equal_p (lr_inner, rr_inner, 0))
5944     return 0;
5945   else
5946     l_const = r_const = 0;
5947
5948   /* If either comparison code is not correct for our logical operation,
5949      fail.  However, we can convert a one-bit comparison against zero into
5950      the opposite comparison against that bit being set in the field.  */
5951
5952   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5953   if (lcode != wanted_code)
5954     {
5955       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5956         {
5957           /* Make the left operand unsigned, since we are only interested
5958              in the value of one bit.  Otherwise we are doing the wrong
5959              thing below.  */
5960           ll_unsignedp = 1;
5961           l_const = ll_mask;
5962         }
5963       else
5964         return 0;
5965     }
5966
5967   /* This is analogous to the code for l_const above.  */
5968   if (rcode != wanted_code)
5969     {
5970       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5971         {
5972           rl_unsignedp = 1;
5973           r_const = rl_mask;
5974         }
5975       else
5976         return 0;
5977     }
5978
5979   /* See if we can find a mode that contains both fields being compared on
5980      the left.  If we can't, fail.  Otherwise, update all constants and masks
5981      to be relative to a field of that size.  */
5982   first_bit = MIN (ll_bitpos, rl_bitpos);
5983   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5984   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5985                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5986                           volatilep);
5987   if (lnmode == VOIDmode)
5988     return 0;
5989
5990   lnbitsize = GET_MODE_BITSIZE (lnmode);
5991   lnbitpos = first_bit & ~ (lnbitsize - 1);
5992   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5993   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5994
5995   if (BYTES_BIG_ENDIAN)
5996     {
5997       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5998       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5999     }
6000
6001   ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
6002                          size_int (xll_bitpos), 0);
6003   rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
6004                          size_int (xrl_bitpos), 0);
6005
6006   if (l_const)
6007     {
6008       l_const = fold_convert_loc (loc, lntype, l_const);
6009       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
6010       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
6011       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
6012                                         fold_build1_loc (loc, BIT_NOT_EXPR,
6013                                                      lntype, ll_mask),
6014                                         0)))
6015         {
6016           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6017
6018           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6019         }
6020     }
6021   if (r_const)
6022     {
6023       r_const = fold_convert_loc (loc, lntype, r_const);
6024       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
6025       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
6026       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
6027                                         fold_build1_loc (loc, BIT_NOT_EXPR,
6028                                                      lntype, rl_mask),
6029                                         0)))
6030         {
6031           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6032
6033           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6034         }
6035     }
6036
6037   /* If the right sides are not constant, do the same for it.  Also,
6038      disallow this optimization if a size or signedness mismatch occurs
6039      between the left and right sides.  */
6040   if (l_const == 0)
6041     {
6042       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
6043           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
6044           /* Make sure the two fields on the right
6045              correspond to the left without being swapped.  */
6046           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
6047         return 0;
6048
6049       first_bit = MIN (lr_bitpos, rr_bitpos);
6050       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
6051       rnmode = get_best_mode (end_bit - first_bit, first_bit,
6052                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
6053                               volatilep);
6054       if (rnmode == VOIDmode)
6055         return 0;
6056
6057       rnbitsize = GET_MODE_BITSIZE (rnmode);
6058       rnbitpos = first_bit & ~ (rnbitsize - 1);
6059       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
6060       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
6061
6062       if (BYTES_BIG_ENDIAN)
6063         {
6064           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
6065           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
6066         }
6067
6068       lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6069                                                             rntype, lr_mask),
6070                              size_int (xlr_bitpos), 0);
6071       rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6072                                                             rntype, rr_mask),
6073                              size_int (xrr_bitpos), 0);
6074
6075       /* Make a mask that corresponds to both fields being compared.
6076          Do this for both items being compared.  If the operands are the
6077          same size and the bits being compared are in the same position
6078          then we can do this by masking both and comparing the masked
6079          results.  */
6080       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
6081       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
6082       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
6083         {
6084           lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
6085                                     ll_unsignedp || rl_unsignedp);
6086           if (! all_ones_mask_p (ll_mask, lnbitsize))
6087             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6088
6089           rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
6090                                     lr_unsignedp || rr_unsignedp);
6091           if (! all_ones_mask_p (lr_mask, rnbitsize))
6092             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6093
6094           result = build2 (wanted_code, truth_type, lhs, rhs);
6095           goto fold_truthop_exit;
6096         }
6097
6098       /* There is still another way we can do something:  If both pairs of
6099          fields being compared are adjacent, we may be able to make a wider
6100          field containing them both.
6101
6102          Note that we still must mask the lhs/rhs expressions.  Furthermore,
6103          the mask must be shifted to account for the shift done by
6104          make_bit_field_ref.  */
6105       if ((ll_bitsize + ll_bitpos == rl_bitpos
6106            && lr_bitsize + lr_bitpos == rr_bitpos)
6107           || (ll_bitpos == rl_bitpos + rl_bitsize
6108               && lr_bitpos == rr_bitpos + rr_bitsize))
6109         {
6110           tree type;
6111
6112           lhs = make_bit_field_ref (loc, ll_inner, lntype,
6113                                     ll_bitsize + rl_bitsize,
6114                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
6115           rhs = make_bit_field_ref (loc, lr_inner, rntype,
6116                                     lr_bitsize + rr_bitsize,
6117                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
6118
6119           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6120                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
6121           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6122                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
6123
6124           /* Convert to the smaller type before masking out unwanted bits.  */
6125           type = lntype;
6126           if (lntype != rntype)
6127             {
6128               if (lnbitsize > rnbitsize)
6129                 {
6130                   lhs = fold_convert_loc (loc, rntype, lhs);
6131                   ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6132                   type = rntype;
6133                 }
6134               else if (lnbitsize < rnbitsize)
6135                 {
6136                   rhs = fold_convert_loc (loc, lntype, rhs);
6137                   lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6138                   type = lntype;
6139                 }
6140             }
6141
6142           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6143             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6144
6145           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6146             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6147
6148           result = build2 (wanted_code, truth_type, lhs, rhs);
6149           goto fold_truthop_exit;
6150         }
6151
6152       return 0;
6153     }
6154
6155   /* Handle the case of comparisons with constants.  If there is something in
6156      common between the masks, those bits of the constants must be the same.
6157      If not, the condition is always false.  Test for this to avoid generating
6158      incorrect code below.  */
6159   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
6160   if (! integer_zerop (result)
6161       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
6162                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
6163     {
6164       if (wanted_code == NE_EXPR)
6165         {
6166           warning (0, "%<or%> of unmatched not-equal tests is always 1");
6167           return constant_boolean_node (true, truth_type);
6168         }
6169       else
6170         {
6171           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6172           return constant_boolean_node (false, truth_type);
6173         }
6174     }
6175
6176   /* Construct the expression we will return.  First get the component
6177      reference we will make.  Unless the mask is all ones the width of
6178      that field, perform the mask operation.  Then compare with the
6179      merged constant.  */
6180   result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
6181                                ll_unsignedp || rl_unsignedp);
6182
6183   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
6184   if (! all_ones_mask_p (ll_mask, lnbitsize))
6185     {
6186       result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
6187       SET_EXPR_LOCATION (result, loc);
6188     }
6189
6190   result = build2 (wanted_code, truth_type, result,
6191                    const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
6192
6193  fold_truthop_exit:
6194   SET_EXPR_LOCATION (result, loc);
6195   return result;
6196 }
6197 \f
6198 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
6199    constant.  */
6200
6201 static tree
6202 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
6203                             tree op0, tree op1)
6204 {
6205   tree arg0 = op0;
6206   enum tree_code op_code;
6207   tree comp_const;
6208   tree minmax_const;
6209   int consts_equal, consts_lt;
6210   tree inner;
6211
6212   STRIP_SIGN_NOPS (arg0);
6213
6214   op_code = TREE_CODE (arg0);
6215   minmax_const = TREE_OPERAND (arg0, 1);
6216   comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
6217   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
6218   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
6219   inner = TREE_OPERAND (arg0, 0);
6220
6221   /* If something does not permit us to optimize, return the original tree.  */
6222   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
6223       || TREE_CODE (comp_const) != INTEGER_CST
6224       || TREE_OVERFLOW (comp_const)
6225       || TREE_CODE (minmax_const) != INTEGER_CST
6226       || TREE_OVERFLOW (minmax_const))
6227     return NULL_TREE;
6228
6229   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
6230      and GT_EXPR, doing the rest with recursive calls using logical
6231      simplifications.  */
6232   switch (code)
6233     {
6234     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
6235       {
6236         tree tem
6237           = optimize_minmax_comparison (loc,
6238                                         invert_tree_comparison (code, false),
6239                                         type, op0, op1);
6240         if (tem)
6241           return invert_truthvalue_loc (loc, tem);
6242         return NULL_TREE;
6243       }
6244
6245     case GE_EXPR:
6246       return
6247         fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
6248                      optimize_minmax_comparison
6249                      (loc, EQ_EXPR, type, arg0, comp_const),
6250                      optimize_minmax_comparison
6251                      (loc, GT_EXPR, type, arg0, comp_const));
6252
6253     case EQ_EXPR:
6254       if (op_code == MAX_EXPR && consts_equal)
6255         /* MAX (X, 0) == 0  ->  X <= 0  */
6256         return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
6257
6258       else if (op_code == MAX_EXPR && consts_lt)
6259         /* MAX (X, 0) == 5  ->  X == 5   */
6260         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
6261
6262       else if (op_code == MAX_EXPR)
6263         /* MAX (X, 0) == -1  ->  false  */
6264         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6265
6266       else if (consts_equal)
6267         /* MIN (X, 0) == 0  ->  X >= 0  */
6268         return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
6269
6270       else if (consts_lt)
6271         /* MIN (X, 0) == 5  ->  false  */
6272         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6273
6274       else
6275         /* MIN (X, 0) == -1  ->  X == -1  */
6276         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
6277
6278     case GT_EXPR:
6279       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
6280         /* MAX (X, 0) > 0  ->  X > 0
6281            MAX (X, 0) > 5  ->  X > 5  */
6282         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
6283
6284       else if (op_code == MAX_EXPR)
6285         /* MAX (X, 0) > -1  ->  true  */
6286         return omit_one_operand_loc (loc, type, integer_one_node, inner);
6287
6288       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
6289         /* MIN (X, 0) > 0  ->  false
6290            MIN (X, 0) > 5  ->  false  */
6291         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6292
6293       else
6294         /* MIN (X, 0) > -1  ->  X > -1  */
6295         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
6296
6297     default:
6298       return NULL_TREE;
6299     }
6300 }
6301 \f
6302 /* T is an integer expression that is being multiplied, divided, or taken a
6303    modulus (CODE says which and what kind of divide or modulus) by a
6304    constant C.  See if we can eliminate that operation by folding it with
6305    other operations already in T.  WIDE_TYPE, if non-null, is a type that
6306    should be used for the computation if wider than our type.
6307
6308    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6309    (X * 2) + (Y * 4).  We must, however, be assured that either the original
6310    expression would not overflow or that overflow is undefined for the type
6311    in the language in question.
6312
6313    If we return a non-null expression, it is an equivalent form of the
6314    original computation, but need not be in the original type.
6315
6316    We set *STRICT_OVERFLOW_P to true if the return values depends on
6317    signed overflow being undefined.  Otherwise we do not change
6318    *STRICT_OVERFLOW_P.  */
6319
6320 static tree
6321 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6322                 bool *strict_overflow_p)
6323 {
6324   /* To avoid exponential search depth, refuse to allow recursion past
6325      three levels.  Beyond that (1) it's highly unlikely that we'll find
6326      something interesting and (2) we've probably processed it before
6327      when we built the inner expression.  */
6328
6329   static int depth;
6330   tree ret;
6331
6332   if (depth > 3)
6333     return NULL;
6334
6335   depth++;
6336   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6337   depth--;
6338
6339   return ret;
6340 }
6341
6342 static tree
6343 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6344                   bool *strict_overflow_p)
6345 {
6346   tree type = TREE_TYPE (t);
6347   enum tree_code tcode = TREE_CODE (t);
6348   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6349                                    > GET_MODE_SIZE (TYPE_MODE (type)))
6350                 ? wide_type : type);
6351   tree t1, t2;
6352   int same_p = tcode == code;
6353   tree op0 = NULL_TREE, op1 = NULL_TREE;
6354   bool sub_strict_overflow_p;
6355
6356   /* Don't deal with constants of zero here; they confuse the code below.  */
6357   if (integer_zerop (c))
6358     return NULL_TREE;
6359
6360   if (TREE_CODE_CLASS (tcode) == tcc_unary)
6361     op0 = TREE_OPERAND (t, 0);
6362
6363   if (TREE_CODE_CLASS (tcode) == tcc_binary)
6364     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6365
6366   /* Note that we need not handle conditional operations here since fold
6367      already handles those cases.  So just do arithmetic here.  */
6368   switch (tcode)
6369     {
6370     case INTEGER_CST:
6371       /* For a constant, we can always simplify if we are a multiply
6372          or (for divide and modulus) if it is a multiple of our constant.  */
6373       if (code == MULT_EXPR
6374           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6375         return const_binop (code, fold_convert (ctype, t),
6376                             fold_convert (ctype, c), 0);
6377       break;
6378
6379     CASE_CONVERT: case NON_LVALUE_EXPR:
6380       /* If op0 is an expression ...  */
6381       if ((COMPARISON_CLASS_P (op0)
6382            || UNARY_CLASS_P (op0)
6383            || BINARY_CLASS_P (op0)
6384            || VL_EXP_CLASS_P (op0)
6385            || EXPRESSION_CLASS_P (op0))
6386           /* ... and has wrapping overflow, and its type is smaller
6387              than ctype, then we cannot pass through as widening.  */
6388           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6389                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6390                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6391                && (TYPE_PRECISION (ctype)
6392                    > TYPE_PRECISION (TREE_TYPE (op0))))
6393               /* ... or this is a truncation (t is narrower than op0),
6394                  then we cannot pass through this narrowing.  */
6395               || (TYPE_PRECISION (type)
6396                   < TYPE_PRECISION (TREE_TYPE (op0)))
6397               /* ... or signedness changes for division or modulus,
6398                  then we cannot pass through this conversion.  */
6399               || (code != MULT_EXPR
6400                   && (TYPE_UNSIGNED (ctype)
6401                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
6402               /* ... or has undefined overflow while the converted to
6403                  type has not, we cannot do the operation in the inner type
6404                  as that would introduce undefined overflow.  */
6405               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6406                   && !TYPE_OVERFLOW_UNDEFINED (type))))
6407         break;
6408
6409       /* Pass the constant down and see if we can make a simplification.  If
6410          we can, replace this expression with the inner simplification for
6411          possible later conversion to our or some other type.  */
6412       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6413           && TREE_CODE (t2) == INTEGER_CST
6414           && !TREE_OVERFLOW (t2)
6415           && (0 != (t1 = extract_muldiv (op0, t2, code,
6416                                          code == MULT_EXPR
6417                                          ? ctype : NULL_TREE,
6418                                          strict_overflow_p))))
6419         return t1;
6420       break;
6421
6422     case ABS_EXPR:
6423       /* If widening the type changes it from signed to unsigned, then we
6424          must avoid building ABS_EXPR itself as unsigned.  */
6425       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6426         {
6427           tree cstype = (*signed_type_for) (ctype);
6428           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6429               != 0)
6430             {
6431               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6432               return fold_convert (ctype, t1);
6433             }
6434           break;
6435         }
6436       /* If the constant is negative, we cannot simplify this.  */
6437       if (tree_int_cst_sgn (c) == -1)
6438         break;
6439       /* FALLTHROUGH */
6440     case NEGATE_EXPR:
6441       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6442           != 0)
6443         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6444       break;
6445
6446     case MIN_EXPR:  case MAX_EXPR:
6447       /* If widening the type changes the signedness, then we can't perform
6448          this optimization as that changes the result.  */
6449       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6450         break;
6451
6452       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
6453       sub_strict_overflow_p = false;
6454       if ((t1 = extract_muldiv (op0, c, code, wide_type,
6455                                 &sub_strict_overflow_p)) != 0
6456           && (t2 = extract_muldiv (op1, c, code, wide_type,
6457                                    &sub_strict_overflow_p)) != 0)
6458         {
6459           if (tree_int_cst_sgn (c) < 0)
6460             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6461           if (sub_strict_overflow_p)
6462             *strict_overflow_p = true;
6463           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6464                               fold_convert (ctype, t2));
6465         }
6466       break;
6467
6468     case LSHIFT_EXPR:  case RSHIFT_EXPR:
6469       /* If the second operand is constant, this is a multiplication
6470          or floor division, by a power of two, so we can treat it that
6471          way unless the multiplier or divisor overflows.  Signed
6472          left-shift overflow is implementation-defined rather than
6473          undefined in C90, so do not convert signed left shift into
6474          multiplication.  */
6475       if (TREE_CODE (op1) == INTEGER_CST
6476           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6477           /* const_binop may not detect overflow correctly,
6478              so check for it explicitly here.  */
6479           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6480           && TREE_INT_CST_HIGH (op1) == 0
6481           && 0 != (t1 = fold_convert (ctype,
6482                                       const_binop (LSHIFT_EXPR,
6483                                                    size_one_node,
6484                                                    op1, 0)))
6485           && !TREE_OVERFLOW (t1))
6486         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6487                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
6488                                        ctype,
6489                                        fold_convert (ctype, op0),
6490                                        t1),
6491                                c, code, wide_type, strict_overflow_p);
6492       break;
6493
6494     case PLUS_EXPR:  case MINUS_EXPR:
6495       /* See if we can eliminate the operation on both sides.  If we can, we
6496          can return a new PLUS or MINUS.  If we can't, the only remaining
6497          cases where we can do anything are if the second operand is a
6498          constant.  */
6499       sub_strict_overflow_p = false;
6500       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6501       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6502       if (t1 != 0 && t2 != 0
6503           && (code == MULT_EXPR
6504               /* If not multiplication, we can only do this if both operands
6505                  are divisible by c.  */
6506               || (multiple_of_p (ctype, op0, c)
6507                   && multiple_of_p (ctype, op1, c))))
6508         {
6509           if (sub_strict_overflow_p)
6510             *strict_overflow_p = true;
6511           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6512                               fold_convert (ctype, t2));
6513         }
6514
6515       /* If this was a subtraction, negate OP1 and set it to be an addition.
6516          This simplifies the logic below.  */
6517       if (tcode == MINUS_EXPR)
6518         {
6519           tcode = PLUS_EXPR, op1 = negate_expr (op1);
6520           /* If OP1 was not easily negatable, the constant may be OP0.  */
6521           if (TREE_CODE (op0) == INTEGER_CST)
6522             {
6523               tree tem = op0;
6524               op0 = op1;
6525               op1 = tem;
6526               tem = t1;
6527               t1 = t2;
6528               t2 = tem;
6529             }
6530         }
6531
6532       if (TREE_CODE (op1) != INTEGER_CST)
6533         break;
6534
6535       /* If either OP1 or C are negative, this optimization is not safe for
6536          some of the division and remainder types while for others we need
6537          to change the code.  */
6538       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6539         {
6540           if (code == CEIL_DIV_EXPR)
6541             code = FLOOR_DIV_EXPR;
6542           else if (code == FLOOR_DIV_EXPR)
6543             code = CEIL_DIV_EXPR;
6544           else if (code != MULT_EXPR
6545                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6546             break;
6547         }
6548
6549       /* If it's a multiply or a division/modulus operation of a multiple
6550          of our constant, do the operation and verify it doesn't overflow.  */
6551       if (code == MULT_EXPR
6552           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6553         {
6554           op1 = const_binop (code, fold_convert (ctype, op1),
6555                              fold_convert (ctype, c), 0);
6556           /* We allow the constant to overflow with wrapping semantics.  */
6557           if (op1 == 0
6558               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6559             break;
6560         }
6561       else
6562         break;
6563
6564       /* If we have an unsigned type is not a sizetype, we cannot widen
6565          the operation since it will change the result if the original
6566          computation overflowed.  */
6567       if (TYPE_UNSIGNED (ctype)
6568           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6569           && ctype != type)
6570         break;
6571
6572       /* If we were able to eliminate our operation from the first side,
6573          apply our operation to the second side and reform the PLUS.  */
6574       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6575         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6576
6577       /* The last case is if we are a multiply.  In that case, we can
6578          apply the distributive law to commute the multiply and addition
6579          if the multiplication of the constants doesn't overflow.  */
6580       if (code == MULT_EXPR)
6581         return fold_build2 (tcode, ctype,
6582                             fold_build2 (code, ctype,
6583                                          fold_convert (ctype, op0),
6584                                          fold_convert (ctype, c)),
6585                             op1);
6586
6587       break;
6588
6589     case MULT_EXPR:
6590       /* We have a special case here if we are doing something like
6591          (C * 8) % 4 since we know that's zero.  */
6592       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6593            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6594           /* If the multiplication can overflow we cannot optimize this.
6595              ???  Until we can properly mark individual operations as
6596              not overflowing we need to treat sizetype special here as
6597              stor-layout relies on this opimization to make
6598              DECL_FIELD_BIT_OFFSET always a constant.  */
6599           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6600               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6601                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
6602           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6603           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6604         {
6605           *strict_overflow_p = true;
6606           return omit_one_operand (type, integer_zero_node, op0);
6607         }
6608
6609       /* ... fall through ...  */
6610
6611     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
6612     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
6613       /* If we can extract our operation from the LHS, do so and return a
6614          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
6615          do something only if the second operand is a constant.  */
6616       if (same_p
6617           && (t1 = extract_muldiv (op0, c, code, wide_type,
6618                                    strict_overflow_p)) != 0)
6619         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6620                             fold_convert (ctype, op1));
6621       else if (tcode == MULT_EXPR && code == MULT_EXPR
6622                && (t1 = extract_muldiv (op1, c, code, wide_type,
6623                                         strict_overflow_p)) != 0)
6624         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6625                             fold_convert (ctype, t1));
6626       else if (TREE_CODE (op1) != INTEGER_CST)
6627         return 0;
6628
6629       /* If these are the same operation types, we can associate them
6630          assuming no overflow.  */
6631       if (tcode == code
6632           && 0 != (t1 = int_const_binop (MULT_EXPR,
6633                                          fold_convert (ctype, op1),
6634                                          fold_convert (ctype, c), 1))
6635           && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
6636                                                TREE_INT_CST_HIGH (t1),
6637                                                (TYPE_UNSIGNED (ctype)
6638                                                 && tcode != MULT_EXPR) ? -1 : 1,
6639                                                TREE_OVERFLOW (t1)))
6640           && !TREE_OVERFLOW (t1))
6641         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6642
6643       /* If these operations "cancel" each other, we have the main
6644          optimizations of this pass, which occur when either constant is a
6645          multiple of the other, in which case we replace this with either an
6646          operation or CODE or TCODE.
6647
6648          If we have an unsigned type that is not a sizetype, we cannot do
6649          this since it will change the result if the original computation
6650          overflowed.  */
6651       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6652            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6653           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6654               || (tcode == MULT_EXPR
6655                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6656                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6657                   && code != MULT_EXPR)))
6658         {
6659           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6660             {
6661               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6662                 *strict_overflow_p = true;
6663               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6664                                   fold_convert (ctype,
6665                                                 const_binop (TRUNC_DIV_EXPR,
6666                                                              op1, c, 0)));
6667             }
6668           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6669             {
6670               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6671                 *strict_overflow_p = true;
6672               return fold_build2 (code, ctype, fold_convert (ctype, op0),
6673                                   fold_convert (ctype,
6674                                                 const_binop (TRUNC_DIV_EXPR,
6675                                                              c, op1, 0)));
6676             }
6677         }
6678       break;
6679
6680     default:
6681       break;
6682     }
6683
6684   return 0;
6685 }
6686 \f
6687 /* Return a node which has the indicated constant VALUE (either 0 or
6688    1), and is of the indicated TYPE.  */
6689
6690 tree
6691 constant_boolean_node (int value, tree type)
6692 {
6693   if (type == integer_type_node)
6694     return value ? integer_one_node : integer_zero_node;
6695   else if (type == boolean_type_node)
6696     return value ? boolean_true_node : boolean_false_node;
6697   else
6698     return build_int_cst (type, value);
6699 }
6700
6701
6702 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6703    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6704    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6705    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6706    COND is the first argument to CODE; otherwise (as in the example
6707    given here), it is the second argument.  TYPE is the type of the
6708    original expression.  Return NULL_TREE if no simplification is
6709    possible.  */
6710
6711 static tree
6712 fold_binary_op_with_conditional_arg (location_t loc,
6713                                      enum tree_code code,
6714                                      tree type, tree op0, tree op1,
6715                                      tree cond, tree arg, int cond_first_p)
6716 {
6717   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6718   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6719   tree test, true_value, false_value;
6720   tree lhs = NULL_TREE;
6721   tree rhs = NULL_TREE;
6722
6723   /* This transformation is only worthwhile if we don't have to wrap
6724      arg in a SAVE_EXPR, and the operation can be simplified on at least
6725      one of the branches once its pushed inside the COND_EXPR.  */
6726   if (!TREE_CONSTANT (arg))
6727     return NULL_TREE;
6728
6729   if (TREE_CODE (cond) == COND_EXPR)
6730     {
6731       test = TREE_OPERAND (cond, 0);
6732       true_value = TREE_OPERAND (cond, 1);
6733       false_value = TREE_OPERAND (cond, 2);
6734       /* If this operand throws an expression, then it does not make
6735          sense to try to perform a logical or arithmetic operation
6736          involving it.  */
6737       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6738         lhs = true_value;
6739       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6740         rhs = false_value;
6741     }
6742   else
6743     {
6744       tree testtype = TREE_TYPE (cond);
6745       test = cond;
6746       true_value = constant_boolean_node (true, testtype);
6747       false_value = constant_boolean_node (false, testtype);
6748     }
6749
6750   arg = fold_convert_loc (loc, arg_type, arg);
6751   if (lhs == 0)
6752     {
6753       true_value = fold_convert_loc (loc, cond_type, true_value);
6754       if (cond_first_p)
6755         lhs = fold_build2_loc (loc, code, type, true_value, arg);
6756       else
6757         lhs = fold_build2_loc (loc, code, type, arg, true_value);
6758     }
6759   if (rhs == 0)
6760     {
6761       false_value = fold_convert_loc (loc, cond_type, false_value);
6762       if (cond_first_p)
6763         rhs = fold_build2_loc (loc, code, type, false_value, arg);
6764       else
6765         rhs = fold_build2_loc (loc, code, type, arg, false_value);
6766     }
6767
6768   test = fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6769   return fold_convert_loc (loc, type, test);
6770 }
6771
6772 \f
6773 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6774
6775    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6776    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6777    ADDEND is the same as X.
6778
6779    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6780    and finite.  The problematic cases are when X is zero, and its mode
6781    has signed zeros.  In the case of rounding towards -infinity,
6782    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6783    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6784
6785 bool
6786 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6787 {
6788   if (!real_zerop (addend))
6789     return false;
6790
6791   /* Don't allow the fold with -fsignaling-nans.  */
6792   if (HONOR_SNANS (TYPE_MODE (type)))
6793     return false;
6794
6795   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6796   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6797     return true;
6798
6799   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6800   if (TREE_CODE (addend) == REAL_CST
6801       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6802     negate = !negate;
6803
6804   /* The mode has signed zeros, and we have to honor their sign.
6805      In this situation, there is only one case we can return true for.
6806      X - 0 is the same as X unless rounding towards -infinity is
6807      supported.  */
6808   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6809 }
6810
6811 /* Subroutine of fold() that checks comparisons of built-in math
6812    functions against real constants.
6813
6814    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6815    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6816    is the type of the result and ARG0 and ARG1 are the operands of the
6817    comparison.  ARG1 must be a TREE_REAL_CST.
6818
6819    The function returns the constant folded tree if a simplification
6820    can be made, and NULL_TREE otherwise.  */
6821
6822 static tree
6823 fold_mathfn_compare (location_t loc,
6824                      enum built_in_function fcode, enum tree_code code,
6825                      tree type, tree arg0, tree arg1)
6826 {
6827   REAL_VALUE_TYPE c;
6828
6829   if (BUILTIN_SQRT_P (fcode))
6830     {
6831       tree arg = CALL_EXPR_ARG (arg0, 0);
6832       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6833
6834       c = TREE_REAL_CST (arg1);
6835       if (REAL_VALUE_NEGATIVE (c))
6836         {
6837           /* sqrt(x) < y is always false, if y is negative.  */
6838           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6839             return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6840
6841           /* sqrt(x) > y is always true, if y is negative and we
6842              don't care about NaNs, i.e. negative values of x.  */
6843           if (code == NE_EXPR || !HONOR_NANS (mode))
6844             return omit_one_operand_loc (loc, type, integer_one_node, arg);
6845
6846           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6847           return fold_build2_loc (loc, GE_EXPR, type, arg,
6848                               build_real (TREE_TYPE (arg), dconst0));
6849         }
6850       else if (code == GT_EXPR || code == GE_EXPR)
6851         {
6852           REAL_VALUE_TYPE c2;
6853
6854           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6855           real_convert (&c2, mode, &c2);
6856
6857           if (REAL_VALUE_ISINF (c2))
6858             {
6859               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6860               if (HONOR_INFINITIES (mode))
6861                 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6862                                     build_real (TREE_TYPE (arg), c2));
6863
6864               /* sqrt(x) > y is always false, when y is very large
6865                  and we don't care about infinities.  */
6866               return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6867             }
6868
6869           /* sqrt(x) > c is the same as x > c*c.  */
6870           return fold_build2_loc (loc, code, type, arg,
6871                               build_real (TREE_TYPE (arg), c2));
6872         }
6873       else if (code == LT_EXPR || code == LE_EXPR)
6874         {
6875           REAL_VALUE_TYPE c2;
6876
6877           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6878           real_convert (&c2, mode, &c2);
6879
6880           if (REAL_VALUE_ISINF (c2))
6881             {
6882               /* sqrt(x) < y is always true, when y is a very large
6883                  value and we don't care about NaNs or Infinities.  */
6884               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6885                 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6886
6887               /* sqrt(x) < y is x != +Inf when y is very large and we
6888                  don't care about NaNs.  */
6889               if (! HONOR_NANS (mode))
6890                 return fold_build2_loc (loc, NE_EXPR, type, arg,
6891                                     build_real (TREE_TYPE (arg), c2));
6892
6893               /* sqrt(x) < y is x >= 0 when y is very large and we
6894                  don't care about Infinities.  */
6895               if (! HONOR_INFINITIES (mode))
6896                 return fold_build2_loc (loc, GE_EXPR, type, arg,
6897                                     build_real (TREE_TYPE (arg), dconst0));
6898
6899               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6900               if (lang_hooks.decls.global_bindings_p () != 0
6901                   || CONTAINS_PLACEHOLDER_P (arg))
6902                 return NULL_TREE;
6903
6904               arg = save_expr (arg);
6905               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6906                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6907                                                build_real (TREE_TYPE (arg),
6908                                                            dconst0)),
6909                                   fold_build2_loc (loc, NE_EXPR, type, arg,
6910                                                build_real (TREE_TYPE (arg),
6911                                                            c2)));
6912             }
6913
6914           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6915           if (! HONOR_NANS (mode))
6916             return fold_build2_loc (loc, code, type, arg,
6917                                 build_real (TREE_TYPE (arg), c2));
6918
6919           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6920           if (lang_hooks.decls.global_bindings_p () == 0
6921               && ! CONTAINS_PLACEHOLDER_P (arg))
6922             {
6923               arg = save_expr (arg);
6924               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6925                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6926                                                build_real (TREE_TYPE (arg),
6927                                                            dconst0)),
6928                                   fold_build2_loc (loc, code, type, arg,
6929                                                build_real (TREE_TYPE (arg),
6930                                                            c2)));
6931             }
6932         }
6933     }
6934
6935   return NULL_TREE;
6936 }
6937
6938 /* Subroutine of fold() that optimizes comparisons against Infinities,
6939    either +Inf or -Inf.
6940
6941    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6942    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6943    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6944
6945    The function returns the constant folded tree if a simplification
6946    can be made, and NULL_TREE otherwise.  */
6947
6948 static tree
6949 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6950                   tree arg0, tree arg1)
6951 {
6952   enum machine_mode mode;
6953   REAL_VALUE_TYPE max;
6954   tree temp;
6955   bool neg;
6956
6957   mode = TYPE_MODE (TREE_TYPE (arg0));
6958
6959   /* For negative infinity swap the sense of the comparison.  */
6960   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6961   if (neg)
6962     code = swap_tree_comparison (code);
6963
6964   switch (code)
6965     {
6966     case GT_EXPR:
6967       /* x > +Inf is always false, if with ignore sNANs.  */
6968       if (HONOR_SNANS (mode))
6969         return NULL_TREE;
6970       return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6971
6972     case LE_EXPR:
6973       /* x <= +Inf is always true, if we don't case about NaNs.  */
6974       if (! HONOR_NANS (mode))
6975         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6976
6977       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6978       if (lang_hooks.decls.global_bindings_p () == 0
6979           && ! CONTAINS_PLACEHOLDER_P (arg0))
6980         {
6981           arg0 = save_expr (arg0);
6982           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6983         }
6984       break;
6985
6986     case EQ_EXPR:
6987     case GE_EXPR:
6988       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6989       real_maxval (&max, neg, mode);
6990       return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6991                           arg0, build_real (TREE_TYPE (arg0), max));
6992
6993     case LT_EXPR:
6994       /* x < +Inf is always equal to x <= DBL_MAX.  */
6995       real_maxval (&max, neg, mode);
6996       return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6997                           arg0, build_real (TREE_TYPE (arg0), max));
6998
6999     case NE_EXPR:
7000       /* x != +Inf is always equal to !(x > DBL_MAX).  */
7001       real_maxval (&max, neg, mode);
7002       if (! HONOR_NANS (mode))
7003         return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
7004                             arg0, build_real (TREE_TYPE (arg0), max));
7005
7006       temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
7007                           arg0, build_real (TREE_TYPE (arg0), max));
7008       return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
7009
7010     default:
7011       break;
7012     }
7013
7014   return NULL_TREE;
7015 }
7016
7017 /* Subroutine of fold() that optimizes comparisons of a division by
7018    a nonzero integer constant against an integer constant, i.e.
7019    X/C1 op C2.
7020
7021    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7022    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
7023    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
7024
7025    The function returns the constant folded tree if a simplification
7026    can be made, and NULL_TREE otherwise.  */
7027
7028 static tree
7029 fold_div_compare (location_t loc,
7030                   enum tree_code code, tree type, tree arg0, tree arg1)
7031 {
7032   tree prod, tmp, hi, lo;
7033   tree arg00 = TREE_OPERAND (arg0, 0);
7034   tree arg01 = TREE_OPERAND (arg0, 1);
7035   unsigned HOST_WIDE_INT lpart;
7036   HOST_WIDE_INT hpart;
7037   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
7038   bool neg_overflow;
7039   int overflow;
7040
7041   /* We have to do this the hard way to detect unsigned overflow.
7042      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
7043   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
7044                                    TREE_INT_CST_HIGH (arg01),
7045                                    TREE_INT_CST_LOW (arg1),
7046                                    TREE_INT_CST_HIGH (arg1),
7047                                    &lpart, &hpart, unsigned_p);
7048   prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
7049                                 -1, overflow);
7050   neg_overflow = false;
7051
7052   if (unsigned_p)
7053     {
7054       tmp = int_const_binop (MINUS_EXPR, arg01,
7055                              build_int_cst (TREE_TYPE (arg01), 1), 0);
7056       lo = prod;
7057
7058       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
7059       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
7060                                        TREE_INT_CST_HIGH (prod),
7061                                        TREE_INT_CST_LOW (tmp),
7062                                        TREE_INT_CST_HIGH (tmp),
7063                                        &lpart, &hpart, unsigned_p);
7064       hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
7065                                   -1, overflow | TREE_OVERFLOW (prod));
7066     }
7067   else if (tree_int_cst_sgn (arg01) >= 0)
7068     {
7069       tmp = int_const_binop (MINUS_EXPR, arg01,
7070                              build_int_cst (TREE_TYPE (arg01), 1), 0);
7071       switch (tree_int_cst_sgn (arg1))
7072         {
7073         case -1:
7074           neg_overflow = true;
7075           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
7076           hi = prod;
7077           break;
7078
7079         case  0:
7080           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
7081           hi = tmp;
7082           break;
7083
7084         case  1:
7085           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
7086           lo = prod;
7087           break;
7088
7089         default:
7090           gcc_unreachable ();
7091         }
7092     }
7093   else
7094     {
7095       /* A negative divisor reverses the relational operators.  */
7096       code = swap_tree_comparison (code);
7097
7098       tmp = int_const_binop (PLUS_EXPR, arg01,
7099                              build_int_cst (TREE_TYPE (arg01), 1), 0);
7100       switch (tree_int_cst_sgn (arg1))
7101         {
7102         case -1:
7103           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
7104           lo = prod;
7105           break;
7106
7107         case  0:
7108           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
7109           lo = tmp;
7110           break;
7111
7112         case  1:
7113           neg_overflow = true;
7114           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
7115           hi = prod;
7116           break;
7117
7118         default:
7119           gcc_unreachable ();
7120         }
7121     }
7122
7123   switch (code)
7124     {
7125     case EQ_EXPR:
7126       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
7127         return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
7128       if (TREE_OVERFLOW (hi))
7129         return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
7130       if (TREE_OVERFLOW (lo))
7131         return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
7132       return build_range_check (loc, type, arg00, 1, lo, hi);
7133
7134     case NE_EXPR:
7135       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
7136         return omit_one_operand_loc (loc, type, integer_one_node, arg00);
7137       if (TREE_OVERFLOW (hi))
7138         return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
7139       if (TREE_OVERFLOW (lo))
7140         return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
7141       return build_range_check (loc, type, arg00, 0, lo, hi);
7142
7143     case LT_EXPR:
7144       if (TREE_OVERFLOW (lo))
7145         {
7146           tmp = neg_overflow ? integer_zero_node : integer_one_node;
7147           return omit_one_operand_loc (loc, type, tmp, arg00);
7148         }
7149       return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
7150
7151     case LE_EXPR:
7152       if (TREE_OVERFLOW (hi))
7153         {
7154           tmp = neg_overflow ? integer_zero_node : integer_one_node;
7155           return omit_one_operand_loc (loc, type, tmp, arg00);
7156         }
7157       return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
7158
7159     case GT_EXPR:
7160       if (TREE_OVERFLOW (hi))
7161         {
7162           tmp = neg_overflow ? integer_one_node : integer_zero_node;
7163           return omit_one_operand_loc (loc, type, tmp, arg00);
7164         }
7165       return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
7166
7167     case GE_EXPR:
7168       if (TREE_OVERFLOW (lo))
7169         {
7170           tmp = neg_overflow ? integer_one_node : integer_zero_node;
7171           return omit_one_operand_loc (loc, type, tmp, arg00);
7172         }
7173       return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
7174
7175     default:
7176       break;
7177     }
7178
7179   return NULL_TREE;
7180 }
7181
7182
7183 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7184    equality/inequality test, then return a simplified form of the test
7185    using a sign testing.  Otherwise return NULL.  TYPE is the desired
7186    result type.  */
7187
7188 static tree
7189 fold_single_bit_test_into_sign_test (location_t loc,
7190                                      enum tree_code code, tree arg0, tree arg1,
7191                                      tree result_type)
7192 {
7193   /* If this is testing a single bit, we can optimize the test.  */
7194   if ((code == NE_EXPR || code == EQ_EXPR)
7195       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
7196       && integer_pow2p (TREE_OPERAND (arg0, 1)))
7197     {
7198       /* If we have (A & C) != 0 where C is the sign bit of A, convert
7199          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
7200       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
7201
7202       if (arg00 != NULL_TREE
7203           /* This is only a win if casting to a signed type is cheap,
7204              i.e. when arg00's type is not a partial mode.  */
7205           && TYPE_PRECISION (TREE_TYPE (arg00))
7206              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
7207         {
7208           tree stype = signed_type_for (TREE_TYPE (arg00));
7209           return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
7210                               result_type,
7211                               fold_convert_loc (loc, stype, arg00),
7212                               build_int_cst (stype, 0));
7213         }
7214     }
7215
7216   return NULL_TREE;
7217 }
7218
7219 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7220    equality/inequality test, then return a simplified form of
7221    the test using shifts and logical operations.  Otherwise return
7222    NULL.  TYPE is the desired result type.  */
7223
7224 tree
7225 fold_single_bit_test (location_t loc, enum tree_code code,
7226                       tree arg0, tree arg1, tree result_type)
7227 {
7228   /* If this is testing a single bit, we can optimize the test.  */
7229   if ((code == NE_EXPR || code == EQ_EXPR)
7230       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
7231       && integer_pow2p (TREE_OPERAND (arg0, 1)))
7232     {
7233       tree inner = TREE_OPERAND (arg0, 0);
7234       tree type = TREE_TYPE (arg0);
7235       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
7236       enum machine_mode operand_mode = TYPE_MODE (type);
7237       int ops_unsigned;
7238       tree signed_type, unsigned_type, intermediate_type;
7239       tree tem, one;
7240
7241       /* First, see if we can fold the single bit test into a sign-bit
7242          test.  */
7243       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
7244                                                  result_type);
7245       if (tem)
7246         return tem;
7247
7248       /* Otherwise we have (A & C) != 0 where C is a single bit,
7249          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
7250          Similarly for (A & C) == 0.  */
7251
7252       /* If INNER is a right shift of a constant and it plus BITNUM does
7253          not overflow, adjust BITNUM and INNER.  */
7254       if (TREE_CODE (inner) == RSHIFT_EXPR
7255           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
7256           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
7257           && bitnum < TYPE_PRECISION (type)
7258           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
7259                                    bitnum - TYPE_PRECISION (type)))
7260         {
7261           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
7262           inner = TREE_OPERAND (inner, 0);
7263         }
7264
7265       /* If we are going to be able to omit the AND below, we must do our
7266          operations as unsigned.  If we must use the AND, we have a choice.
7267          Normally unsigned is faster, but for some machines signed is.  */
7268 #ifdef LOAD_EXTEND_OP
7269       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
7270                       && !flag_syntax_only) ? 0 : 1;
7271 #else
7272       ops_unsigned = 1;
7273 #endif
7274
7275       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
7276       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
7277       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
7278       inner = fold_convert_loc (loc, intermediate_type, inner);
7279
7280       if (bitnum != 0)
7281         inner = build2 (RSHIFT_EXPR, intermediate_type,
7282                         inner, size_int (bitnum));
7283
7284       one = build_int_cst (intermediate_type, 1);
7285
7286       if (code == EQ_EXPR)
7287         inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
7288
7289       /* Put the AND last so it can combine with more things.  */
7290       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
7291
7292       /* Make sure to return the proper type.  */
7293       inner = fold_convert_loc (loc, result_type, inner);
7294
7295       return inner;
7296     }
7297   return NULL_TREE;
7298 }
7299
7300 /* Check whether we are allowed to reorder operands arg0 and arg1,
7301    such that the evaluation of arg1 occurs before arg0.  */
7302
7303 static bool
7304 reorder_operands_p (const_tree arg0, const_tree arg1)
7305 {
7306   if (! flag_evaluation_order)
7307       return true;
7308   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
7309     return true;
7310   return ! TREE_SIDE_EFFECTS (arg0)
7311          && ! TREE_SIDE_EFFECTS (arg1);
7312 }
7313
7314 /* Test whether it is preferable two swap two operands, ARG0 and
7315    ARG1, for example because ARG0 is an integer constant and ARG1
7316    isn't.  If REORDER is true, only recommend swapping if we can
7317    evaluate the operands in reverse order.  */
7318
7319 bool
7320 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
7321 {
7322   STRIP_SIGN_NOPS (arg0);
7323   STRIP_SIGN_NOPS (arg1);
7324
7325   if (TREE_CODE (arg1) == INTEGER_CST)
7326     return 0;
7327   if (TREE_CODE (arg0) == INTEGER_CST)
7328     return 1;
7329
7330   if (TREE_CODE (arg1) == REAL_CST)
7331     return 0;
7332   if (TREE_CODE (arg0) == REAL_CST)
7333     return 1;
7334
7335   if (TREE_CODE (arg1) == FIXED_CST)
7336     return 0;
7337   if (TREE_CODE (arg0) == FIXED_CST)
7338     return 1;
7339
7340   if (TREE_CODE (arg1) == COMPLEX_CST)
7341     return 0;
7342   if (TREE_CODE (arg0) == COMPLEX_CST)
7343     return 1;
7344
7345   if (TREE_CONSTANT (arg1))
7346     return 0;
7347   if (TREE_CONSTANT (arg0))
7348     return 1;
7349
7350   if (optimize_function_for_size_p (cfun))
7351     return 0;
7352
7353   if (reorder && flag_evaluation_order
7354       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7355     return 0;
7356
7357   /* It is preferable to swap two SSA_NAME to ensure a canonical form
7358      for commutative and comparison operators.  Ensuring a canonical
7359      form allows the optimizers to find additional redundancies without
7360      having to explicitly check for both orderings.  */
7361   if (TREE_CODE (arg0) == SSA_NAME
7362       && TREE_CODE (arg1) == SSA_NAME
7363       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7364     return 1;
7365
7366   /* Put SSA_NAMEs last.  */
7367   if (TREE_CODE (arg1) == SSA_NAME)
7368     return 0;
7369   if (TREE_CODE (arg0) == SSA_NAME)
7370     return 1;
7371
7372   /* Put variables last.  */
7373   if (DECL_P (arg1))
7374     return 0;
7375   if (DECL_P (arg0))
7376     return 1;
7377
7378   return 0;
7379 }
7380
7381 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7382    ARG0 is extended to a wider type.  */
7383
7384 static tree
7385 fold_widened_comparison (location_t loc, enum tree_code code,
7386                          tree type, tree arg0, tree arg1)
7387 {
7388   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7389   tree arg1_unw;
7390   tree shorter_type, outer_type;
7391   tree min, max;
7392   bool above, below;
7393
7394   if (arg0_unw == arg0)
7395     return NULL_TREE;
7396   shorter_type = TREE_TYPE (arg0_unw);
7397
7398 #ifdef HAVE_canonicalize_funcptr_for_compare
7399   /* Disable this optimization if we're casting a function pointer
7400      type on targets that require function pointer canonicalization.  */
7401   if (HAVE_canonicalize_funcptr_for_compare
7402       && TREE_CODE (shorter_type) == POINTER_TYPE
7403       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7404     return NULL_TREE;
7405 #endif
7406
7407   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7408     return NULL_TREE;
7409
7410   arg1_unw = get_unwidened (arg1, NULL_TREE);
7411
7412   /* If possible, express the comparison in the shorter mode.  */
7413   if ((code == EQ_EXPR || code == NE_EXPR
7414        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7415       && (TREE_TYPE (arg1_unw) == shorter_type
7416           || ((TYPE_PRECISION (shorter_type)
7417                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
7418               && (TYPE_UNSIGNED (shorter_type)
7419                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
7420           || (TREE_CODE (arg1_unw) == INTEGER_CST
7421               && (TREE_CODE (shorter_type) == INTEGER_TYPE
7422                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7423               && int_fits_type_p (arg1_unw, shorter_type))))
7424     return fold_build2_loc (loc, code, type, arg0_unw,
7425                         fold_convert_loc (loc, shorter_type, arg1_unw));
7426
7427   if (TREE_CODE (arg1_unw) != INTEGER_CST
7428       || TREE_CODE (shorter_type) != INTEGER_TYPE
7429       || !int_fits_type_p (arg1_unw, shorter_type))
7430     return NULL_TREE;
7431
7432   /* If we are comparing with the integer that does not fit into the range
7433      of the shorter type, the result is known.  */
7434   outer_type = TREE_TYPE (arg1_unw);
7435   min = lower_bound_in_type (outer_type, shorter_type);
7436   max = upper_bound_in_type (outer_type, shorter_type);
7437
7438   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7439                                                    max, arg1_unw));
7440   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7441                                                    arg1_unw, min));
7442
7443   switch (code)
7444     {
7445     case EQ_EXPR:
7446       if (above || below)
7447         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7448       break;
7449
7450     case NE_EXPR:
7451       if (above || below)
7452         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7453       break;
7454
7455     case LT_EXPR:
7456     case LE_EXPR:
7457       if (above)
7458         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7459       else if (below)
7460         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7461
7462     case GT_EXPR:
7463     case GE_EXPR:
7464       if (above)
7465         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7466       else if (below)
7467         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7468
7469     default:
7470       break;
7471     }
7472
7473   return NULL_TREE;
7474 }
7475
7476 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7477    ARG0 just the signedness is changed.  */
7478
7479 static tree
7480 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
7481                               tree arg0, tree arg1)
7482 {
7483   tree arg0_inner;
7484   tree inner_type, outer_type;
7485
7486   if (!CONVERT_EXPR_P (arg0))
7487     return NULL_TREE;
7488
7489   outer_type = TREE_TYPE (arg0);
7490   arg0_inner = TREE_OPERAND (arg0, 0);
7491   inner_type = TREE_TYPE (arg0_inner);
7492
7493 #ifdef HAVE_canonicalize_funcptr_for_compare
7494   /* Disable this optimization if we're casting a function pointer
7495      type on targets that require function pointer canonicalization.  */
7496   if (HAVE_canonicalize_funcptr_for_compare
7497       && TREE_CODE (inner_type) == POINTER_TYPE
7498       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7499     return NULL_TREE;
7500 #endif
7501
7502   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7503     return NULL_TREE;
7504
7505   if (TREE_CODE (arg1) != INTEGER_CST
7506       && !(CONVERT_EXPR_P (arg1)
7507            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7508     return NULL_TREE;
7509
7510   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7511        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7512       && code != NE_EXPR
7513       && code != EQ_EXPR)
7514     return NULL_TREE;
7515
7516   if (TREE_CODE (arg1) == INTEGER_CST)
7517     arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7518                                   TREE_INT_CST_HIGH (arg1), 0,
7519                                   TREE_OVERFLOW (arg1));
7520   else
7521     arg1 = fold_convert_loc (loc, inner_type, arg1);
7522
7523   return fold_build2_loc (loc, code, type, arg0_inner, arg1);
7524 }
7525
7526 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7527    step of the array.  Reconstructs s and delta in the case of s *
7528    delta being an integer constant (and thus already folded).  ADDR is
7529    the address. MULT is the multiplicative expression.  If the
7530    function succeeds, the new address expression is returned.
7531    Otherwise NULL_TREE is returned.  LOC is the location of the
7532    resulting expression.  */
7533
7534 static tree
7535 try_move_mult_to_index (location_t loc, tree addr, tree op1)
7536 {
7537   tree s, delta, step;
7538   tree ref = TREE_OPERAND (addr, 0), pref;
7539   tree ret, pos;
7540   tree itype;
7541   bool mdim = false;
7542
7543   /*  Strip the nops that might be added when converting op1 to sizetype. */
7544   STRIP_NOPS (op1);
7545
7546   /* Canonicalize op1 into a possibly non-constant delta
7547      and an INTEGER_CST s.  */
7548   if (TREE_CODE (op1) == MULT_EXPR)
7549     {
7550       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7551
7552       STRIP_NOPS (arg0);
7553       STRIP_NOPS (arg1);
7554
7555       if (TREE_CODE (arg0) == INTEGER_CST)
7556         {
7557           s = arg0;
7558           delta = arg1;
7559         }
7560       else if (TREE_CODE (arg1) == INTEGER_CST)
7561         {
7562           s = arg1;
7563           delta = arg0;
7564         }
7565       else
7566         return NULL_TREE;
7567     }
7568   else if (TREE_CODE (op1) == INTEGER_CST)
7569     {
7570       delta = op1;
7571       s = NULL_TREE;
7572     }
7573   else
7574     {
7575       /* Simulate we are delta * 1.  */
7576       delta = op1;
7577       s = integer_one_node;
7578     }
7579
7580   for (;; ref = TREE_OPERAND (ref, 0))
7581     {
7582       if (TREE_CODE (ref) == ARRAY_REF)
7583         {
7584           tree domain;
7585
7586           /* Remember if this was a multi-dimensional array.  */
7587           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7588             mdim = true;
7589
7590           domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7591           if (! domain)
7592             continue;
7593           itype = TREE_TYPE (domain);
7594
7595           step = array_ref_element_size (ref);
7596           if (TREE_CODE (step) != INTEGER_CST)
7597             continue;
7598
7599           if (s)
7600             {
7601               if (! tree_int_cst_equal (step, s))
7602                 continue;
7603             }
7604           else
7605             {
7606               /* Try if delta is a multiple of step.  */
7607               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
7608               if (! tmp)
7609                 continue;
7610               delta = tmp;
7611             }
7612
7613           /* Only fold here if we can verify we do not overflow one
7614              dimension of a multi-dimensional array.  */
7615           if (mdim)
7616             {
7617               tree tmp;
7618
7619               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7620                   || !TYPE_MAX_VALUE (domain)
7621                   || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
7622                 continue;
7623
7624               tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
7625                                      fold_convert_loc (loc, itype,
7626                                                        TREE_OPERAND (ref, 1)),
7627                                      fold_convert_loc (loc, itype, delta));
7628               if (!tmp
7629                   || TREE_CODE (tmp) != INTEGER_CST
7630                   || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
7631                 continue;
7632             }
7633
7634           break;
7635         }
7636       else
7637         mdim = false;
7638
7639       if (!handled_component_p (ref))
7640         return NULL_TREE;
7641     }
7642
7643   /* We found the suitable array reference.  So copy everything up to it,
7644      and replace the index.  */
7645
7646   pref = TREE_OPERAND (addr, 0);
7647   ret = copy_node (pref);
7648   SET_EXPR_LOCATION (ret, loc);
7649   pos = ret;
7650
7651   while (pref != ref)
7652     {
7653       pref = TREE_OPERAND (pref, 0);
7654       TREE_OPERAND (pos, 0) = copy_node (pref);
7655       pos = TREE_OPERAND (pos, 0);
7656     }
7657
7658   TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype,
7659                                        fold_convert_loc (loc, itype,
7660                                                          TREE_OPERAND (pos, 1)),
7661                                        fold_convert_loc (loc, itype, delta));
7662
7663   return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
7664 }
7665
7666
7667 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
7668    means A >= Y && A != MAX, but in this case we know that
7669    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
7670
7671 static tree
7672 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7673 {
7674   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7675
7676   if (TREE_CODE (bound) == LT_EXPR)
7677     a = TREE_OPERAND (bound, 0);
7678   else if (TREE_CODE (bound) == GT_EXPR)
7679     a = TREE_OPERAND (bound, 1);
7680   else
7681     return NULL_TREE;
7682
7683   typea = TREE_TYPE (a);
7684   if (!INTEGRAL_TYPE_P (typea)
7685       && !POINTER_TYPE_P (typea))
7686     return NULL_TREE;
7687
7688   if (TREE_CODE (ineq) == LT_EXPR)
7689     {
7690       a1 = TREE_OPERAND (ineq, 1);
7691       y = TREE_OPERAND (ineq, 0);
7692     }
7693   else if (TREE_CODE (ineq) == GT_EXPR)
7694     {
7695       a1 = TREE_OPERAND (ineq, 0);
7696       y = TREE_OPERAND (ineq, 1);
7697     }
7698   else
7699     return NULL_TREE;
7700
7701   if (TREE_TYPE (a1) != typea)
7702     return NULL_TREE;
7703
7704   if (POINTER_TYPE_P (typea))
7705     {
7706       /* Convert the pointer types into integer before taking the difference.  */
7707       tree ta = fold_convert_loc (loc, ssizetype, a);
7708       tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7709       diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7710     }
7711   else
7712     diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7713
7714   if (!diff || !integer_onep (diff))
7715    return NULL_TREE;
7716
7717   return fold_build2_loc (loc, GE_EXPR, type, a, y);
7718 }
7719
7720 /* Fold a sum or difference of at least one multiplication.
7721    Returns the folded tree or NULL if no simplification could be made.  */
7722
7723 static tree
7724 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7725                           tree arg0, tree arg1)
7726 {
7727   tree arg00, arg01, arg10, arg11;
7728   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7729
7730   /* (A * C) +- (B * C) -> (A+-B) * C.
7731      (A * C) +- A -> A * (C+-1).
7732      We are most concerned about the case where C is a constant,
7733      but other combinations show up during loop reduction.  Since
7734      it is not difficult, try all four possibilities.  */
7735
7736   if (TREE_CODE (arg0) == MULT_EXPR)
7737     {
7738       arg00 = TREE_OPERAND (arg0, 0);
7739       arg01 = TREE_OPERAND (arg0, 1);
7740     }
7741   else if (TREE_CODE (arg0) == INTEGER_CST)
7742     {
7743       arg00 = build_one_cst (type);
7744       arg01 = arg0;
7745     }
7746   else
7747     {
7748       /* We cannot generate constant 1 for fract.  */
7749       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7750         return NULL_TREE;
7751       arg00 = arg0;
7752       arg01 = build_one_cst (type);
7753     }
7754   if (TREE_CODE (arg1) == MULT_EXPR)
7755     {
7756       arg10 = TREE_OPERAND (arg1, 0);
7757       arg11 = TREE_OPERAND (arg1, 1);
7758     }
7759   else if (TREE_CODE (arg1) == INTEGER_CST)
7760     {
7761       arg10 = build_one_cst (type);
7762       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7763          the purpose of this canonicalization.  */
7764       if (TREE_INT_CST_HIGH (arg1) == -1
7765           && negate_expr_p (arg1)
7766           && code == PLUS_EXPR)
7767         {
7768           arg11 = negate_expr (arg1);
7769           code = MINUS_EXPR;
7770         }
7771       else
7772         arg11 = arg1;
7773     }
7774   else
7775     {
7776       /* We cannot generate constant 1 for fract.  */
7777       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7778         return NULL_TREE;
7779       arg10 = arg1;
7780       arg11 = build_one_cst (type);
7781     }
7782   same = NULL_TREE;
7783
7784   if (operand_equal_p (arg01, arg11, 0))
7785     same = arg01, alt0 = arg00, alt1 = arg10;
7786   else if (operand_equal_p (arg00, arg10, 0))
7787     same = arg00, alt0 = arg01, alt1 = arg11;
7788   else if (operand_equal_p (arg00, arg11, 0))
7789     same = arg00, alt0 = arg01, alt1 = arg10;
7790   else if (operand_equal_p (arg01, arg10, 0))
7791     same = arg01, alt0 = arg00, alt1 = arg11;
7792
7793   /* No identical multiplicands; see if we can find a common
7794      power-of-two factor in non-power-of-two multiplies.  This
7795      can help in multi-dimensional array access.  */
7796   else if (host_integerp (arg01, 0)
7797            && host_integerp (arg11, 0))
7798     {
7799       HOST_WIDE_INT int01, int11, tmp;
7800       bool swap = false;
7801       tree maybe_same;
7802       int01 = TREE_INT_CST_LOW (arg01);
7803       int11 = TREE_INT_CST_LOW (arg11);
7804
7805       /* Move min of absolute values to int11.  */
7806       if ((int01 >= 0 ? int01 : -int01)
7807           < (int11 >= 0 ? int11 : -int11))
7808         {
7809           tmp = int01, int01 = int11, int11 = tmp;
7810           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7811           maybe_same = arg01;
7812           swap = true;
7813         }
7814       else
7815         maybe_same = arg11;
7816
7817       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7818           /* The remainder should not be a constant, otherwise we
7819              end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7820              increased the number of multiplications necessary.  */
7821           && TREE_CODE (arg10) != INTEGER_CST)
7822         {
7823           alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7824                               build_int_cst (TREE_TYPE (arg00),
7825                                              int01 / int11));
7826           alt1 = arg10;
7827           same = maybe_same;
7828           if (swap)
7829             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7830         }
7831     }
7832
7833   if (same)
7834     return fold_build2_loc (loc, MULT_EXPR, type,
7835                         fold_build2_loc (loc, code, type,
7836                                      fold_convert_loc (loc, type, alt0),
7837                                      fold_convert_loc (loc, type, alt1)),
7838                         fold_convert_loc (loc, type, same));
7839
7840   return NULL_TREE;
7841 }
7842
7843 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7844    specified by EXPR into the buffer PTR of length LEN bytes.
7845    Return the number of bytes placed in the buffer, or zero
7846    upon failure.  */
7847
7848 static int
7849 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7850 {
7851   tree type = TREE_TYPE (expr);
7852   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7853   int byte, offset, word, words;
7854   unsigned char value;
7855
7856   if (total_bytes > len)
7857     return 0;
7858   words = total_bytes / UNITS_PER_WORD;
7859
7860   for (byte = 0; byte < total_bytes; byte++)
7861     {
7862       int bitpos = byte * BITS_PER_UNIT;
7863       if (bitpos < HOST_BITS_PER_WIDE_INT)
7864         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7865       else
7866         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7867                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7868
7869       if (total_bytes > UNITS_PER_WORD)
7870         {
7871           word = byte / UNITS_PER_WORD;
7872           if (WORDS_BIG_ENDIAN)
7873             word = (words - 1) - word;
7874           offset = word * UNITS_PER_WORD;
7875           if (BYTES_BIG_ENDIAN)
7876             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7877           else
7878             offset += byte % UNITS_PER_WORD;
7879         }
7880       else
7881         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7882       ptr[offset] = value;
7883     }
7884   return total_bytes;
7885 }
7886
7887
7888 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7889    specified by EXPR into the buffer PTR of length LEN bytes.
7890    Return the number of bytes placed in the buffer, or zero
7891    upon failure.  */
7892
7893 static int
7894 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7895 {
7896   tree type = TREE_TYPE (expr);
7897   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7898   int byte, offset, word, words, bitpos;
7899   unsigned char value;
7900
7901   /* There are always 32 bits in each long, no matter the size of
7902      the hosts long.  We handle floating point representations with
7903      up to 192 bits.  */
7904   long tmp[6];
7905
7906   if (total_bytes > len)
7907     return 0;
7908   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7909
7910   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7911
7912   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7913        bitpos += BITS_PER_UNIT)
7914     {
7915       byte = (bitpos / BITS_PER_UNIT) & 3;
7916       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7917
7918       if (UNITS_PER_WORD < 4)
7919         {
7920           word = byte / UNITS_PER_WORD;
7921           if (WORDS_BIG_ENDIAN)
7922             word = (words - 1) - word;
7923           offset = word * UNITS_PER_WORD;
7924           if (BYTES_BIG_ENDIAN)
7925             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7926           else
7927             offset += byte % UNITS_PER_WORD;
7928         }
7929       else
7930         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7931       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7932     }
7933   return total_bytes;
7934 }
7935
7936 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7937    specified by EXPR into the buffer PTR of length LEN bytes.
7938    Return the number of bytes placed in the buffer, or zero
7939    upon failure.  */
7940
7941 static int
7942 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7943 {
7944   int rsize, isize;
7945   tree part;
7946
7947   part = TREE_REALPART (expr);
7948   rsize = native_encode_expr (part, ptr, len);
7949   if (rsize == 0)
7950     return 0;
7951   part = TREE_IMAGPART (expr);
7952   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7953   if (isize != rsize)
7954     return 0;
7955   return rsize + isize;
7956 }
7957
7958
7959 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7960    specified by EXPR into the buffer PTR of length LEN bytes.
7961    Return the number of bytes placed in the buffer, or zero
7962    upon failure.  */
7963
7964 static int
7965 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7966 {
7967   int i, size, offset, count;
7968   tree itype, elem, elements;
7969
7970   offset = 0;
7971   elements = TREE_VECTOR_CST_ELTS (expr);
7972   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7973   itype = TREE_TYPE (TREE_TYPE (expr));
7974   size = GET_MODE_SIZE (TYPE_MODE (itype));
7975   for (i = 0; i < count; i++)
7976     {
7977       if (elements)
7978         {
7979           elem = TREE_VALUE (elements);
7980           elements = TREE_CHAIN (elements);
7981         }
7982       else
7983         elem = NULL_TREE;
7984
7985       if (elem)
7986         {
7987           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7988             return 0;
7989         }
7990       else
7991         {
7992           if (offset + size > len)
7993             return 0;
7994           memset (ptr+offset, 0, size);
7995         }
7996       offset += size;
7997     }
7998   return offset;
7999 }
8000
8001
8002 /* Subroutine of native_encode_expr.  Encode the STRING_CST
8003    specified by EXPR into the buffer PTR of length LEN bytes.
8004    Return the number of bytes placed in the buffer, or zero
8005    upon failure.  */
8006
8007 static int
8008 native_encode_string (const_tree expr, unsigned char *ptr, int len)
8009 {
8010   tree type = TREE_TYPE (expr);
8011   HOST_WIDE_INT total_bytes;
8012
8013   if (TREE_CODE (type) != ARRAY_TYPE
8014       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
8015       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
8016       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
8017     return 0;
8018   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
8019   if (total_bytes > len)
8020     return 0;
8021   if (TREE_STRING_LENGTH (expr) < total_bytes)
8022     {
8023       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
8024       memset (ptr + TREE_STRING_LENGTH (expr), 0,
8025               total_bytes - TREE_STRING_LENGTH (expr));
8026     }
8027   else
8028     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
8029   return total_bytes;
8030 }
8031
8032
8033 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
8034    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
8035    buffer PTR of length LEN bytes.  Return the number of bytes
8036    placed in the buffer, or zero upon failure.  */
8037
8038 int
8039 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
8040 {
8041   switch (TREE_CODE (expr))
8042     {
8043     case INTEGER_CST:
8044       return native_encode_int (expr, ptr, len);
8045
8046     case REAL_CST:
8047       return native_encode_real (expr, ptr, len);
8048
8049     case COMPLEX_CST:
8050       return native_encode_complex (expr, ptr, len);
8051
8052     case VECTOR_CST:
8053       return native_encode_vector (expr, ptr, len);
8054
8055     case STRING_CST:
8056       return native_encode_string (expr, ptr, len);
8057
8058     default:
8059       return 0;
8060     }
8061 }
8062
8063
8064 /* Subroutine of native_interpret_expr.  Interpret the contents of
8065    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8066    If the buffer cannot be interpreted, return NULL_TREE.  */
8067
8068 static tree
8069 native_interpret_int (tree type, const unsigned char *ptr, int len)
8070 {
8071   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
8072   int byte, offset, word, words;
8073   unsigned char value;
8074   unsigned int HOST_WIDE_INT lo = 0;
8075   HOST_WIDE_INT hi = 0;
8076
8077   if (total_bytes > len)
8078     return NULL_TREE;
8079   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
8080     return NULL_TREE;
8081   words = total_bytes / UNITS_PER_WORD;
8082
8083   for (byte = 0; byte < total_bytes; byte++)
8084     {
8085       int bitpos = byte * BITS_PER_UNIT;
8086       if (total_bytes > UNITS_PER_WORD)
8087         {
8088           word = byte / UNITS_PER_WORD;
8089           if (WORDS_BIG_ENDIAN)
8090             word = (words - 1) - word;
8091           offset = word * UNITS_PER_WORD;
8092           if (BYTES_BIG_ENDIAN)
8093             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8094           else
8095             offset += byte % UNITS_PER_WORD;
8096         }
8097       else
8098         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
8099       value = ptr[offset];
8100
8101       if (bitpos < HOST_BITS_PER_WIDE_INT)
8102         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
8103       else
8104         hi |= (unsigned HOST_WIDE_INT) value
8105               << (bitpos - HOST_BITS_PER_WIDE_INT);
8106     }
8107
8108   return build_int_cst_wide_type (type, lo, hi);
8109 }
8110
8111
8112 /* Subroutine of native_interpret_expr.  Interpret the contents of
8113    the buffer PTR of length LEN as a REAL_CST of type TYPE.
8114    If the buffer cannot be interpreted, return NULL_TREE.  */
8115
8116 static tree
8117 native_interpret_real (tree type, const unsigned char *ptr, int len)
8118 {
8119   enum machine_mode mode = TYPE_MODE (type);
8120   int total_bytes = GET_MODE_SIZE (mode);
8121   int byte, offset, word, words, bitpos;
8122   unsigned char value;
8123   /* There are always 32 bits in each long, no matter the size of
8124      the hosts long.  We handle floating point representations with
8125      up to 192 bits.  */
8126   REAL_VALUE_TYPE r;
8127   long tmp[6];
8128
8129   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
8130   if (total_bytes > len || total_bytes > 24)
8131     return NULL_TREE;
8132   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8133
8134   memset (tmp, 0, sizeof (tmp));
8135   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8136        bitpos += BITS_PER_UNIT)
8137     {
8138       byte = (bitpos / BITS_PER_UNIT) & 3;
8139       if (UNITS_PER_WORD < 4)
8140         {
8141           word = byte / UNITS_PER_WORD;
8142           if (WORDS_BIG_ENDIAN)
8143             word = (words - 1) - word;
8144           offset = word * UNITS_PER_WORD;
8145           if (BYTES_BIG_ENDIAN)
8146             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8147           else
8148             offset += byte % UNITS_PER_WORD;
8149         }
8150       else
8151         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
8152       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8153
8154       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8155     }
8156
8157   real_from_target (&r, tmp, mode);
8158   return build_real (type, r);
8159 }
8160
8161
8162 /* Subroutine of native_interpret_expr.  Interpret the contents of
8163    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8164    If the buffer cannot be interpreted, return NULL_TREE.  */
8165
8166 static tree
8167 native_interpret_complex (tree type, const unsigned char *ptr, int len)
8168 {
8169   tree etype, rpart, ipart;
8170   int size;
8171
8172   etype = TREE_TYPE (type);
8173   size = GET_MODE_SIZE (TYPE_MODE (etype));
8174   if (size * 2 > len)
8175     return NULL_TREE;
8176   rpart = native_interpret_expr (etype, ptr, size);
8177   if (!rpart)
8178     return NULL_TREE;
8179   ipart = native_interpret_expr (etype, ptr+size, size);
8180   if (!ipart)
8181     return NULL_TREE;
8182   return build_complex (type, rpart, ipart);
8183 }
8184
8185
8186 /* Subroutine of native_interpret_expr.  Interpret the contents of
8187    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8188    If the buffer cannot be interpreted, return NULL_TREE.  */
8189
8190 static tree
8191 native_interpret_vector (tree type, const unsigned char *ptr, int len)
8192 {
8193   tree etype, elem, elements;
8194   int i, size, count;
8195
8196   etype = TREE_TYPE (type);
8197   size = GET_MODE_SIZE (TYPE_MODE (etype));
8198   count = TYPE_VECTOR_SUBPARTS (type);
8199   if (size * count > len)
8200     return NULL_TREE;
8201
8202   elements = NULL_TREE;
8203   for (i = count - 1; i >= 0; i--)
8204     {
8205       elem = native_interpret_expr (etype, ptr+(i*size), size);
8206       if (!elem)
8207         return NULL_TREE;
8208       elements = tree_cons (NULL_TREE, elem, elements);
8209     }
8210   return build_vector (type, elements);
8211 }
8212
8213
8214 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
8215    the buffer PTR of length LEN as a constant of type TYPE.  For
8216    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8217    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
8218    return NULL_TREE.  */
8219
8220 tree
8221 native_interpret_expr (tree type, const unsigned char *ptr, int len)
8222 {
8223   switch (TREE_CODE (type))
8224     {
8225     case INTEGER_TYPE:
8226     case ENUMERAL_TYPE:
8227     case BOOLEAN_TYPE:
8228       return native_interpret_int (type, ptr, len);
8229
8230     case REAL_TYPE:
8231       return native_interpret_real (type, ptr, len);
8232
8233     case COMPLEX_TYPE:
8234       return native_interpret_complex (type, ptr, len);
8235
8236     case VECTOR_TYPE:
8237       return native_interpret_vector (type, ptr, len);
8238
8239     default:
8240       return NULL_TREE;
8241     }
8242 }
8243
8244
8245 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
8246    TYPE at compile-time.  If we're unable to perform the conversion
8247    return NULL_TREE.  */
8248
8249 static tree
8250 fold_view_convert_expr (tree type, tree expr)
8251 {
8252   /* We support up to 512-bit values (for V8DFmode).  */
8253   unsigned char buffer[64];
8254   int len;
8255
8256   /* Check that the host and target are sane.  */
8257   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
8258     return NULL_TREE;
8259
8260   len = native_encode_expr (expr, buffer, sizeof (buffer));
8261   if (len == 0)
8262     return NULL_TREE;
8263
8264   return native_interpret_expr (type, buffer, len);
8265 }
8266
8267 /* Build an expression for the address of T.  Folds away INDIRECT_REF
8268    to avoid confusing the gimplify process.  */
8269
8270 tree
8271 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
8272 {
8273   /* The size of the object is not relevant when talking about its address.  */
8274   if (TREE_CODE (t) == WITH_SIZE_EXPR)
8275     t = TREE_OPERAND (t, 0);
8276
8277   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
8278   if (TREE_CODE (t) == INDIRECT_REF
8279       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
8280     {
8281       t = TREE_OPERAND (t, 0);
8282
8283       if (TREE_TYPE (t) != ptrtype)
8284         {
8285           t = build1 (NOP_EXPR, ptrtype, t);
8286           SET_EXPR_LOCATION (t, loc);
8287         }
8288     }
8289   else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
8290     {
8291       t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
8292
8293       if (TREE_TYPE (t) != ptrtype)
8294         t = fold_convert_loc (loc, ptrtype, t);
8295     }
8296   else
8297     {
8298       t = build1 (ADDR_EXPR, ptrtype, t);
8299       SET_EXPR_LOCATION (t, loc);
8300     }
8301
8302   return t;
8303 }
8304
8305 /* Build an expression for the address of T.  */
8306
8307 tree
8308 build_fold_addr_expr_loc (location_t loc, tree t)
8309 {
8310   tree ptrtype = build_pointer_type (TREE_TYPE (t));
8311
8312   return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
8313 }
8314
8315 /* Fold a unary expression of code CODE and type TYPE with operand
8316    OP0.  Return the folded expression if folding is successful.
8317    Otherwise, return NULL_TREE.  */
8318
8319 tree
8320 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
8321 {
8322   tree tem;
8323   tree arg0;
8324   enum tree_code_class kind = TREE_CODE_CLASS (code);
8325
8326   gcc_assert (IS_EXPR_CODE_CLASS (kind)
8327               && TREE_CODE_LENGTH (code) == 1);
8328
8329   arg0 = op0;
8330   if (arg0)
8331     {
8332       if (CONVERT_EXPR_CODE_P (code)
8333           || code == FLOAT_EXPR || code == ABS_EXPR)
8334         {
8335           /* Don't use STRIP_NOPS, because signedness of argument type
8336              matters.  */
8337           STRIP_SIGN_NOPS (arg0);
8338         }
8339       else
8340         {
8341           /* Strip any conversions that don't change the mode.  This
8342              is safe for every expression, except for a comparison
8343              expression because its signedness is derived from its
8344              operands.
8345
8346              Note that this is done as an internal manipulation within
8347              the constant folder, in order to find the simplest
8348              representation of the arguments so that their form can be
8349              studied.  In any cases, the appropriate type conversions
8350              should be put back in the tree that will get out of the
8351              constant folder.  */
8352           STRIP_NOPS (arg0);
8353         }
8354     }
8355
8356   if (TREE_CODE_CLASS (code) == tcc_unary)
8357     {
8358       if (TREE_CODE (arg0) == COMPOUND_EXPR)
8359         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
8360                        fold_build1_loc (loc, code, type,
8361                                     fold_convert_loc (loc, TREE_TYPE (op0),
8362                                                       TREE_OPERAND (arg0, 1))));
8363       else if (TREE_CODE (arg0) == COND_EXPR)
8364         {
8365           tree arg01 = TREE_OPERAND (arg0, 1);
8366           tree arg02 = TREE_OPERAND (arg0, 2);
8367           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8368             arg01 = fold_build1_loc (loc, code, type,
8369                                  fold_convert_loc (loc,
8370                                                    TREE_TYPE (op0), arg01));
8371           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8372             arg02 = fold_build1_loc (loc, code, type,
8373                                  fold_convert_loc (loc,
8374                                                    TREE_TYPE (op0), arg02));
8375           tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
8376                              arg01, arg02);
8377
8378           /* If this was a conversion, and all we did was to move into
8379              inside the COND_EXPR, bring it back out.  But leave it if
8380              it is a conversion from integer to integer and the
8381              result precision is no wider than a word since such a
8382              conversion is cheap and may be optimized away by combine,
8383              while it couldn't if it were outside the COND_EXPR.  Then return
8384              so we don't get into an infinite recursion loop taking the
8385              conversion out and then back in.  */
8386
8387           if ((CONVERT_EXPR_CODE_P (code)
8388                || code == NON_LVALUE_EXPR)
8389               && TREE_CODE (tem) == COND_EXPR
8390               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8391               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8392               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8393               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8394               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8395                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8396               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8397                      && (INTEGRAL_TYPE_P
8398                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8399                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8400                   || flag_syntax_only))
8401             {
8402               tem = build1 (code, type,
8403                             build3 (COND_EXPR,
8404                                     TREE_TYPE (TREE_OPERAND
8405                                                (TREE_OPERAND (tem, 1), 0)),
8406                                     TREE_OPERAND (tem, 0),
8407                                     TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8408                                     TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8409               SET_EXPR_LOCATION (tem, loc);
8410             }
8411           return tem;
8412         }
8413       else if (COMPARISON_CLASS_P (arg0))
8414         {
8415           if (TREE_CODE (type) == BOOLEAN_TYPE)
8416             {
8417               arg0 = copy_node (arg0);
8418               TREE_TYPE (arg0) = type;
8419               return arg0;
8420             }
8421           else if (TREE_CODE (type) != INTEGER_TYPE)
8422             return fold_build3_loc (loc, COND_EXPR, type, arg0,
8423                                 fold_build1_loc (loc, code, type,
8424                                              integer_one_node),
8425                                 fold_build1_loc (loc, code, type,
8426                                              integer_zero_node));
8427         }
8428    }
8429
8430   switch (code)
8431     {
8432     case PAREN_EXPR:
8433       /* Re-association barriers around constants and other re-association
8434          barriers can be removed.  */
8435       if (CONSTANT_CLASS_P (op0)
8436           || TREE_CODE (op0) == PAREN_EXPR)
8437         return fold_convert_loc (loc, type, op0);
8438       return NULL_TREE;
8439
8440     CASE_CONVERT:
8441     case FLOAT_EXPR:
8442     case FIX_TRUNC_EXPR:
8443       if (TREE_TYPE (op0) == type)
8444         return op0;
8445
8446       /* If we have (type) (a CMP b) and type is an integral type, return
8447          new expression involving the new type.  */
8448       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8449         return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8450                             TREE_OPERAND (op0, 1));
8451
8452       /* Handle cases of two conversions in a row.  */
8453       if (CONVERT_EXPR_P (op0))
8454         {
8455           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8456           tree inter_type = TREE_TYPE (op0);
8457           int inside_int = INTEGRAL_TYPE_P (inside_type);
8458           int inside_ptr = POINTER_TYPE_P (inside_type);
8459           int inside_float = FLOAT_TYPE_P (inside_type);
8460           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8461           unsigned int inside_prec = TYPE_PRECISION (inside_type);
8462           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8463           int inter_int = INTEGRAL_TYPE_P (inter_type);
8464           int inter_ptr = POINTER_TYPE_P (inter_type);
8465           int inter_float = FLOAT_TYPE_P (inter_type);
8466           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8467           unsigned int inter_prec = TYPE_PRECISION (inter_type);
8468           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8469           int final_int = INTEGRAL_TYPE_P (type);
8470           int final_ptr = POINTER_TYPE_P (type);
8471           int final_float = FLOAT_TYPE_P (type);
8472           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8473           unsigned int final_prec = TYPE_PRECISION (type);
8474           int final_unsignedp = TYPE_UNSIGNED (type);
8475
8476           /* In addition to the cases of two conversions in a row
8477              handled below, if we are converting something to its own
8478              type via an object of identical or wider precision, neither
8479              conversion is needed.  */
8480           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8481               && (((inter_int || inter_ptr) && final_int)
8482                   || (inter_float && final_float))
8483               && inter_prec >= final_prec)
8484             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8485
8486           /* Likewise, if the intermediate and initial types are either both
8487              float or both integer, we don't need the middle conversion if the
8488              former is wider than the latter and doesn't change the signedness
8489              (for integers).  Avoid this if the final type is a pointer since
8490              then we sometimes need the middle conversion.  Likewise if the
8491              final type has a precision not equal to the size of its mode.  */
8492           if (((inter_int && inside_int)
8493                || (inter_float && inside_float)
8494                || (inter_vec && inside_vec))
8495               && inter_prec >= inside_prec
8496               && (inter_float || inter_vec
8497                   || inter_unsignedp == inside_unsignedp)
8498               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8499                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
8500               && ! final_ptr
8501               && (! final_vec || inter_prec == inside_prec))
8502             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8503
8504           /* If we have a sign-extension of a zero-extended value, we can
8505              replace that by a single zero-extension.  */
8506           if (inside_int && inter_int && final_int
8507               && inside_prec < inter_prec && inter_prec < final_prec
8508               && inside_unsignedp && !inter_unsignedp)
8509             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8510
8511           /* Two conversions in a row are not needed unless:
8512              - some conversion is floating-point (overstrict for now), or
8513              - some conversion is a vector (overstrict for now), or
8514              - the intermediate type is narrower than both initial and
8515                final, or
8516              - the intermediate type and innermost type differ in signedness,
8517                and the outermost type is wider than the intermediate, or
8518              - the initial type is a pointer type and the precisions of the
8519                intermediate and final types differ, or
8520              - the final type is a pointer type and the precisions of the
8521                initial and intermediate types differ.  */
8522           if (! inside_float && ! inter_float && ! final_float
8523               && ! inside_vec && ! inter_vec && ! final_vec
8524               && (inter_prec >= inside_prec || inter_prec >= final_prec)
8525               && ! (inside_int && inter_int
8526                     && inter_unsignedp != inside_unsignedp
8527                     && inter_prec < final_prec)
8528               && ((inter_unsignedp && inter_prec > inside_prec)
8529                   == (final_unsignedp && final_prec > inter_prec))
8530               && ! (inside_ptr && inter_prec != final_prec)
8531               && ! (final_ptr && inside_prec != inter_prec)
8532               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8533                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
8534             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8535         }
8536
8537       /* Handle (T *)&A.B.C for A being of type T and B and C
8538          living at offset zero.  This occurs frequently in
8539          C++ upcasting and then accessing the base.  */
8540       if (TREE_CODE (op0) == ADDR_EXPR
8541           && POINTER_TYPE_P (type)
8542           && handled_component_p (TREE_OPERAND (op0, 0)))
8543         {
8544           HOST_WIDE_INT bitsize, bitpos;
8545           tree offset;
8546           enum machine_mode mode;
8547           int unsignedp, volatilep;
8548           tree base = TREE_OPERAND (op0, 0);
8549           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8550                                       &mode, &unsignedp, &volatilep, false);
8551           /* If the reference was to a (constant) zero offset, we can use
8552              the address of the base if it has the same base type
8553              as the result type.  */
8554           if (! offset && bitpos == 0
8555               && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8556                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8557             return fold_convert_loc (loc, type,
8558                                      build_fold_addr_expr_loc (loc, base));
8559         }
8560
8561       if (TREE_CODE (op0) == MODIFY_EXPR
8562           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
8563           /* Detect assigning a bitfield.  */
8564           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
8565                && DECL_BIT_FIELD
8566                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
8567         {
8568           /* Don't leave an assignment inside a conversion
8569              unless assigning a bitfield.  */
8570           tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
8571           /* First do the assignment, then return converted constant.  */
8572           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8573           TREE_NO_WARNING (tem) = 1;
8574           TREE_USED (tem) = 1;
8575           SET_EXPR_LOCATION (tem, loc);
8576           return tem;
8577         }
8578
8579       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8580          constants (if x has signed type, the sign bit cannot be set
8581          in c).  This folds extension into the BIT_AND_EXPR.
8582          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8583          very likely don't have maximal range for their precision and this
8584          transformation effectively doesn't preserve non-maximal ranges.  */
8585       if (TREE_CODE (type) == INTEGER_TYPE
8586           && TREE_CODE (op0) == BIT_AND_EXPR
8587           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
8588         {
8589           tree and_expr = op0;
8590           tree and0 = TREE_OPERAND (and_expr, 0);
8591           tree and1 = TREE_OPERAND (and_expr, 1);
8592           int change = 0;
8593
8594           if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
8595               || (TYPE_PRECISION (type)
8596                   <= TYPE_PRECISION (TREE_TYPE (and_expr))))
8597             change = 1;
8598           else if (TYPE_PRECISION (TREE_TYPE (and1))
8599                    <= HOST_BITS_PER_WIDE_INT
8600                    && host_integerp (and1, 1))
8601             {
8602               unsigned HOST_WIDE_INT cst;
8603
8604               cst = tree_low_cst (and1, 1);
8605               cst &= (HOST_WIDE_INT) -1
8606                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8607               change = (cst == 0);
8608 #ifdef LOAD_EXTEND_OP
8609               if (change
8610                   && !flag_syntax_only
8611                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8612                       == ZERO_EXTEND))
8613                 {
8614                   tree uns = unsigned_type_for (TREE_TYPE (and0));
8615                   and0 = fold_convert_loc (loc, uns, and0);
8616                   and1 = fold_convert_loc (loc, uns, and1);
8617                 }
8618 #endif
8619             }
8620           if (change)
8621             {
8622               tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8623                                            TREE_INT_CST_HIGH (and1), 0,
8624                                            TREE_OVERFLOW (and1));
8625               return fold_build2_loc (loc, BIT_AND_EXPR, type,
8626                                   fold_convert_loc (loc, type, and0), tem);
8627             }
8628         }
8629
8630       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8631          when one of the new casts will fold away. Conservatively we assume
8632          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8633       if (POINTER_TYPE_P (type)
8634           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8635           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8636               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8637               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8638         {
8639           tree arg00 = TREE_OPERAND (arg0, 0);
8640           tree arg01 = TREE_OPERAND (arg0, 1);
8641
8642           return fold_build2_loc (loc,
8643                               TREE_CODE (arg0), type,
8644                               fold_convert_loc (loc, type, arg00),
8645                               fold_convert_loc (loc, sizetype, arg01));
8646         }
8647
8648       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8649          of the same precision, and X is an integer type not narrower than
8650          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
8651       if (INTEGRAL_TYPE_P (type)
8652           && TREE_CODE (op0) == BIT_NOT_EXPR
8653           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8654           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8655           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8656         {
8657           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8658           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8659               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8660             return fold_build1_loc (loc, BIT_NOT_EXPR, type,
8661                                 fold_convert_loc (loc, type, tem));
8662         }
8663
8664       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8665          type of X and Y (integer types only).  */
8666       if (INTEGRAL_TYPE_P (type)
8667           && TREE_CODE (op0) == MULT_EXPR
8668           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8669           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8670         {
8671           /* Be careful not to introduce new overflows.  */
8672           tree mult_type;
8673           if (TYPE_OVERFLOW_WRAPS (type))
8674             mult_type = type;
8675           else
8676             mult_type = unsigned_type_for (type);
8677
8678           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8679             {
8680               tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8681                                  fold_convert_loc (loc, mult_type,
8682                                                    TREE_OPERAND (op0, 0)),
8683                                  fold_convert_loc (loc, mult_type,
8684                                                    TREE_OPERAND (op0, 1)));
8685               return fold_convert_loc (loc, type, tem);
8686             }
8687         }
8688
8689       tem = fold_convert_const (code, type, op0);
8690       return tem ? tem : NULL_TREE;
8691
8692     case ADDR_SPACE_CONVERT_EXPR:
8693       if (integer_zerop (arg0))
8694         return fold_convert_const (code, type, arg0);
8695       return NULL_TREE;
8696
8697     case FIXED_CONVERT_EXPR:
8698       tem = fold_convert_const (code, type, arg0);
8699       return tem ? tem : NULL_TREE;
8700
8701     case VIEW_CONVERT_EXPR:
8702       if (TREE_TYPE (op0) == type)
8703         return op0;
8704       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8705         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8706                             type, TREE_OPERAND (op0, 0));
8707
8708       /* For integral conversions with the same precision or pointer
8709          conversions use a NOP_EXPR instead.  */
8710       if ((INTEGRAL_TYPE_P (type)
8711            || POINTER_TYPE_P (type))
8712           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8713               || POINTER_TYPE_P (TREE_TYPE (op0)))
8714           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8715         return fold_convert_loc (loc, type, op0);
8716
8717       /* Strip inner integral conversions that do not change the precision.  */
8718       if (CONVERT_EXPR_P (op0)
8719           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8720               || POINTER_TYPE_P (TREE_TYPE (op0)))
8721           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8722               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8723           && (TYPE_PRECISION (TREE_TYPE (op0))
8724               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8725         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8726                             type, TREE_OPERAND (op0, 0));
8727
8728       return fold_view_convert_expr (type, op0);
8729
8730     case NEGATE_EXPR:
8731       tem = fold_negate_expr (loc, arg0);
8732       if (tem)
8733         return fold_convert_loc (loc, type, tem);
8734       return NULL_TREE;
8735
8736     case ABS_EXPR:
8737       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8738         return fold_abs_const (arg0, type);
8739       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8740         return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8741       /* Convert fabs((double)float) into (double)fabsf(float).  */
8742       else if (TREE_CODE (arg0) == NOP_EXPR
8743                && TREE_CODE (type) == REAL_TYPE)
8744         {
8745           tree targ0 = strip_float_extensions (arg0);
8746           if (targ0 != arg0)
8747             return fold_convert_loc (loc, type,
8748                                      fold_build1_loc (loc, ABS_EXPR,
8749                                                   TREE_TYPE (targ0),
8750                                                   targ0));
8751         }
8752       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8753       else if (TREE_CODE (arg0) == ABS_EXPR)
8754         return arg0;
8755       else if (tree_expr_nonnegative_p (arg0))
8756         return arg0;
8757
8758       /* Strip sign ops from argument.  */
8759       if (TREE_CODE (type) == REAL_TYPE)
8760         {
8761           tem = fold_strip_sign_ops (arg0);
8762           if (tem)
8763             return fold_build1_loc (loc, ABS_EXPR, type,
8764                                 fold_convert_loc (loc, type, tem));
8765         }
8766       return NULL_TREE;
8767
8768     case CONJ_EXPR:
8769       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8770         return fold_convert_loc (loc, type, arg0);
8771       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8772         {
8773           tree itype = TREE_TYPE (type);
8774           tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8775           tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8776           return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8777                               negate_expr (ipart));
8778         }
8779       if (TREE_CODE (arg0) == COMPLEX_CST)
8780         {
8781           tree itype = TREE_TYPE (type);
8782           tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8783           tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8784           return build_complex (type, rpart, negate_expr (ipart));
8785         }
8786       if (TREE_CODE (arg0) == CONJ_EXPR)
8787         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8788       return NULL_TREE;
8789
8790     case BIT_NOT_EXPR:
8791       if (TREE_CODE (arg0) == INTEGER_CST)
8792         return fold_not_const (arg0, type);
8793       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8794         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8795       /* Convert ~ (-A) to A - 1.  */
8796       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8797         return fold_build2_loc (loc, MINUS_EXPR, type,
8798                             fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8799                             build_int_cst (type, 1));
8800       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8801       else if (INTEGRAL_TYPE_P (type)
8802                && ((TREE_CODE (arg0) == MINUS_EXPR
8803                     && integer_onep (TREE_OPERAND (arg0, 1)))
8804                    || (TREE_CODE (arg0) == PLUS_EXPR
8805                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8806         return fold_build1_loc (loc, NEGATE_EXPR, type,
8807                             fold_convert_loc (loc, type,
8808                                               TREE_OPERAND (arg0, 0)));
8809       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8810       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8811                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8812                                      fold_convert_loc (loc, type,
8813                                                        TREE_OPERAND (arg0, 0)))))
8814         return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8815                             fold_convert_loc (loc, type,
8816                                               TREE_OPERAND (arg0, 1)));
8817       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8818                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8819                                      fold_convert_loc (loc, type,
8820                                                        TREE_OPERAND (arg0, 1)))))
8821         return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8822                             fold_convert_loc (loc, type,
8823                                               TREE_OPERAND (arg0, 0)), tem);
8824       /* Perform BIT_NOT_EXPR on each element individually.  */
8825       else if (TREE_CODE (arg0) == VECTOR_CST)
8826         {
8827           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8828           int count = TYPE_VECTOR_SUBPARTS (type), i;
8829
8830           for (i = 0; i < count; i++)
8831             {
8832               if (elements)
8833                 {
8834                   elem = TREE_VALUE (elements);
8835                   elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8836                   if (elem == NULL_TREE)
8837                     break;
8838                   elements = TREE_CHAIN (elements);
8839                 }
8840               else
8841                 elem = build_int_cst (TREE_TYPE (type), -1);
8842               list = tree_cons (NULL_TREE, elem, list);
8843             }
8844           if (i == count)
8845             return build_vector (type, nreverse (list));
8846         }
8847
8848       return NULL_TREE;
8849
8850     case TRUTH_NOT_EXPR:
8851       /* The argument to invert_truthvalue must have Boolean type.  */
8852       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8853           arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8854
8855       /* Note that the operand of this must be an int
8856          and its values must be 0 or 1.
8857          ("true" is a fixed value perhaps depending on the language,
8858          but we don't handle values other than 1 correctly yet.)  */
8859       tem = fold_truth_not_expr (loc, arg0);
8860       if (!tem)
8861         return NULL_TREE;
8862       return fold_convert_loc (loc, type, tem);
8863
8864     case REALPART_EXPR:
8865       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8866         return fold_convert_loc (loc, type, arg0);
8867       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8868         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8869                                  TREE_OPERAND (arg0, 1));
8870       if (TREE_CODE (arg0) == COMPLEX_CST)
8871         return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8872       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8873         {
8874           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8875           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8876                              fold_build1_loc (loc, REALPART_EXPR, itype,
8877                                           TREE_OPERAND (arg0, 0)),
8878                              fold_build1_loc (loc, REALPART_EXPR, itype,
8879                                           TREE_OPERAND (arg0, 1)));
8880           return fold_convert_loc (loc, type, tem);
8881         }
8882       if (TREE_CODE (arg0) == CONJ_EXPR)
8883         {
8884           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8885           tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8886                              TREE_OPERAND (arg0, 0));
8887           return fold_convert_loc (loc, type, tem);
8888         }
8889       if (TREE_CODE (arg0) == CALL_EXPR)
8890         {
8891           tree fn = get_callee_fndecl (arg0);
8892           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8893             switch (DECL_FUNCTION_CODE (fn))
8894               {
8895               CASE_FLT_FN (BUILT_IN_CEXPI):
8896                 fn = mathfn_built_in (type, BUILT_IN_COS);
8897                 if (fn)
8898                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8899                 break;
8900
8901               default:
8902                 break;
8903               }
8904         }
8905       return NULL_TREE;
8906
8907     case IMAGPART_EXPR:
8908       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8909         return fold_convert_loc (loc, type, integer_zero_node);
8910       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8911         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8912                                  TREE_OPERAND (arg0, 0));
8913       if (TREE_CODE (arg0) == COMPLEX_CST)
8914         return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8915       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8916         {
8917           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8918           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8919                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8920                                           TREE_OPERAND (arg0, 0)),
8921                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8922                                           TREE_OPERAND (arg0, 1)));
8923           return fold_convert_loc (loc, type, tem);
8924         }
8925       if (TREE_CODE (arg0) == CONJ_EXPR)
8926         {
8927           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8928           tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8929           return fold_convert_loc (loc, type, negate_expr (tem));
8930         }
8931       if (TREE_CODE (arg0) == CALL_EXPR)
8932         {
8933           tree fn = get_callee_fndecl (arg0);
8934           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8935             switch (DECL_FUNCTION_CODE (fn))
8936               {
8937               CASE_FLT_FN (BUILT_IN_CEXPI):
8938                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8939                 if (fn)
8940                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8941                 break;
8942
8943               default:
8944                 break;
8945               }
8946         }
8947       return NULL_TREE;
8948
8949     case INDIRECT_REF:
8950       /* Fold *&X to X if X is an lvalue.  */
8951       if (TREE_CODE (op0) == ADDR_EXPR)
8952         {
8953           tree op00 = TREE_OPERAND (op0, 0);
8954           if ((TREE_CODE (op00) == VAR_DECL
8955                || TREE_CODE (op00) == PARM_DECL
8956                || TREE_CODE (op00) == RESULT_DECL)
8957               && !TREE_READONLY (op00))
8958             return op00;
8959         }
8960       return NULL_TREE;
8961
8962     default:
8963       return NULL_TREE;
8964     } /* switch (code) */
8965 }
8966
8967
8968 /* If the operation was a conversion do _not_ mark a resulting constant
8969    with TREE_OVERFLOW if the original constant was not.  These conversions
8970    have implementation defined behavior and retaining the TREE_OVERFLOW
8971    flag here would confuse later passes such as VRP.  */
8972 tree
8973 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8974                                 tree type, tree op0)
8975 {
8976   tree res = fold_unary_loc (loc, code, type, op0);
8977   if (res
8978       && TREE_CODE (res) == INTEGER_CST
8979       && TREE_CODE (op0) == INTEGER_CST
8980       && CONVERT_EXPR_CODE_P (code))
8981     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8982
8983   return res;
8984 }
8985
8986 /* Fold a binary expression of code CODE and type TYPE with operands
8987    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8988    Return the folded expression if folding is successful.  Otherwise,
8989    return NULL_TREE.  */
8990
8991 static tree
8992 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8993 {
8994   enum tree_code compl_code;
8995
8996   if (code == MIN_EXPR)
8997     compl_code = MAX_EXPR;
8998   else if (code == MAX_EXPR)
8999     compl_code = MIN_EXPR;
9000   else
9001     gcc_unreachable ();
9002
9003   /* MIN (MAX (a, b), b) == b.  */
9004   if (TREE_CODE (op0) == compl_code
9005       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
9006     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
9007
9008   /* MIN (MAX (b, a), b) == b.  */
9009   if (TREE_CODE (op0) == compl_code
9010       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
9011       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
9012     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
9013
9014   /* MIN (a, MAX (a, b)) == a.  */
9015   if (TREE_CODE (op1) == compl_code
9016       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
9017       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
9018     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
9019
9020   /* MIN (a, MAX (b, a)) == a.  */
9021   if (TREE_CODE (op1) == compl_code
9022       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
9023       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
9024     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
9025
9026   return NULL_TREE;
9027 }
9028
9029 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9030    by changing CODE to reduce the magnitude of constants involved in
9031    ARG0 of the comparison.
9032    Returns a canonicalized comparison tree if a simplification was
9033    possible, otherwise returns NULL_TREE.
9034    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9035    valid if signed overflow is undefined.  */
9036
9037 static tree
9038 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9039                                  tree arg0, tree arg1,
9040                                  bool *strict_overflow_p)
9041 {
9042   enum tree_code code0 = TREE_CODE (arg0);
9043   tree t, cst0 = NULL_TREE;
9044   int sgn0;
9045   bool swap = false;
9046
9047   /* Match A +- CST code arg1 and CST code arg1.  We can change the
9048      first form only if overflow is undefined.  */
9049   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9050          /* In principle pointers also have undefined overflow behavior,
9051             but that causes problems elsewhere.  */
9052          && !POINTER_TYPE_P (TREE_TYPE (arg0))
9053          && (code0 == MINUS_EXPR
9054              || code0 == PLUS_EXPR)
9055          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9056         || code0 == INTEGER_CST))
9057     return NULL_TREE;
9058
9059   /* Identify the constant in arg0 and its sign.  */
9060   if (code0 == INTEGER_CST)
9061     cst0 = arg0;
9062   else
9063     cst0 = TREE_OPERAND (arg0, 1);
9064   sgn0 = tree_int_cst_sgn (cst0);
9065
9066   /* Overflowed constants and zero will cause problems.  */
9067   if (integer_zerop (cst0)
9068       || TREE_OVERFLOW (cst0))
9069     return NULL_TREE;
9070
9071   /* See if we can reduce the magnitude of the constant in
9072      arg0 by changing the comparison code.  */
9073   if (code0 == INTEGER_CST)
9074     {
9075       /* CST <= arg1  ->  CST-1 < arg1.  */
9076       if (code == LE_EXPR && sgn0 == 1)
9077         code = LT_EXPR;
9078       /* -CST < arg1  ->  -CST-1 <= arg1.  */
9079       else if (code == LT_EXPR && sgn0 == -1)
9080         code = LE_EXPR;
9081       /* CST > arg1  ->  CST-1 >= arg1.  */
9082       else if (code == GT_EXPR && sgn0 == 1)
9083         code = GE_EXPR;
9084       /* -CST >= arg1  ->  -CST-1 > arg1.  */
9085       else if (code == GE_EXPR && sgn0 == -1)
9086         code = GT_EXPR;
9087       else
9088         return NULL_TREE;
9089       /* arg1 code' CST' might be more canonical.  */
9090       swap = true;
9091     }
9092   else
9093     {
9094       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
9095       if (code == LT_EXPR
9096           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9097         code = LE_EXPR;
9098       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
9099       else if (code == GT_EXPR
9100                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9101         code = GE_EXPR;
9102       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
9103       else if (code == LE_EXPR
9104                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9105         code = LT_EXPR;
9106       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
9107       else if (code == GE_EXPR
9108                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9109         code = GT_EXPR;
9110       else
9111         return NULL_TREE;
9112       *strict_overflow_p = true;
9113     }
9114
9115   /* Now build the constant reduced in magnitude.  But not if that
9116      would produce one outside of its types range.  */
9117   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9118       && ((sgn0 == 1
9119            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9120            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9121           || (sgn0 == -1
9122               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9123               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9124     /* We cannot swap the comparison here as that would cause us to
9125        endlessly recurse.  */
9126     return NULL_TREE;
9127
9128   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9129                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
9130   if (code0 != INTEGER_CST)
9131     t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9132
9133   /* If swapping might yield to a more canonical form, do so.  */
9134   if (swap)
9135     return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
9136   else
9137     return fold_build2_loc (loc, code, type, t, arg1);
9138 }
9139
9140 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9141    overflow further.  Try to decrease the magnitude of constants involved
9142    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9143    and put sole constants at the second argument position.
9144    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
9145
9146 static tree
9147 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9148                                tree arg0, tree arg1)
9149 {
9150   tree t;
9151   bool strict_overflow_p;
9152   const char * const warnmsg = G_("assuming signed overflow does not occur "
9153                                   "when reducing constant in comparison");
9154
9155   /* Try canonicalization by simplifying arg0.  */
9156   strict_overflow_p = false;
9157   t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
9158                                        &strict_overflow_p);
9159   if (t)
9160     {
9161       if (strict_overflow_p)
9162         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9163       return t;
9164     }
9165
9166   /* Try canonicalization by simplifying arg1 using the swapped
9167      comparison.  */
9168   code = swap_tree_comparison (code);
9169   strict_overflow_p = false;
9170   t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
9171                                        &strict_overflow_p);
9172   if (t && strict_overflow_p)
9173     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9174   return t;
9175 }
9176
9177 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
9178    space.  This is used to avoid issuing overflow warnings for
9179    expressions like &p->x which can not wrap.  */
9180
9181 static bool
9182 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
9183 {
9184   unsigned HOST_WIDE_INT offset_low, total_low;
9185   HOST_WIDE_INT size, offset_high, total_high;
9186
9187   if (!POINTER_TYPE_P (TREE_TYPE (base)))
9188     return true;
9189
9190   if (bitpos < 0)
9191     return true;
9192
9193   if (offset == NULL_TREE)
9194     {
9195       offset_low = 0;
9196       offset_high = 0;
9197     }
9198   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
9199     return true;
9200   else
9201     {
9202       offset_low = TREE_INT_CST_LOW (offset);
9203       offset_high = TREE_INT_CST_HIGH (offset);
9204     }
9205
9206   if (add_double_with_sign (offset_low, offset_high,
9207                             bitpos / BITS_PER_UNIT, 0,
9208                             &total_low, &total_high,
9209                             true))
9210     return true;
9211
9212   if (total_high != 0)
9213     return true;
9214
9215   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
9216   if (size <= 0)
9217     return true;
9218
9219   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
9220      array.  */
9221   if (TREE_CODE (base) == ADDR_EXPR)
9222     {
9223       HOST_WIDE_INT base_size;
9224
9225       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
9226       if (base_size > 0 && size < base_size)
9227         size = base_size;
9228     }
9229
9230   return total_low > (unsigned HOST_WIDE_INT) size;
9231 }
9232
9233 /* Subroutine of fold_binary.  This routine performs all of the
9234    transformations that are common to the equality/inequality
9235    operators (EQ_EXPR and NE_EXPR) and the ordering operators
9236    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
9237    fold_binary should call fold_binary.  Fold a comparison with
9238    tree code CODE and type TYPE with operands OP0 and OP1.  Return
9239    the folded comparison or NULL_TREE.  */
9240
9241 static tree
9242 fold_comparison (location_t loc, enum tree_code code, tree type,
9243                  tree op0, tree op1)
9244 {
9245   tree arg0, arg1, tem;
9246
9247   arg0 = op0;
9248   arg1 = op1;
9249
9250   STRIP_SIGN_NOPS (arg0);
9251   STRIP_SIGN_NOPS (arg1);
9252
9253   tem = fold_relational_const (code, type, arg0, arg1);
9254   if (tem != NULL_TREE)
9255     return tem;
9256
9257   /* If one arg is a real or integer constant, put it last.  */
9258   if (tree_swap_operands_p (arg0, arg1, true))
9259     return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9260
9261   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
9262   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9263       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9264           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9265           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
9266       && (TREE_CODE (arg1) == INTEGER_CST
9267           && !TREE_OVERFLOW (arg1)))
9268     {
9269       tree const1 = TREE_OPERAND (arg0, 1);
9270       tree const2 = arg1;
9271       tree variable = TREE_OPERAND (arg0, 0);
9272       tree lhs;
9273       int lhs_add;
9274       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
9275
9276       lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
9277                          TREE_TYPE (arg1), const2, const1);
9278
9279       /* If the constant operation overflowed this can be
9280          simplified as a comparison against INT_MAX/INT_MIN.  */
9281       if (TREE_CODE (lhs) == INTEGER_CST
9282           && TREE_OVERFLOW (lhs))
9283         {
9284           int const1_sgn = tree_int_cst_sgn (const1);
9285           enum tree_code code2 = code;
9286
9287           /* Get the sign of the constant on the lhs if the
9288              operation were VARIABLE + CONST1.  */
9289           if (TREE_CODE (arg0) == MINUS_EXPR)
9290             const1_sgn = -const1_sgn;
9291
9292           /* The sign of the constant determines if we overflowed
9293              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
9294              Canonicalize to the INT_MIN overflow by swapping the comparison
9295              if necessary.  */
9296           if (const1_sgn == -1)
9297             code2 = swap_tree_comparison (code);
9298
9299           /* We now can look at the canonicalized case
9300                VARIABLE + 1  CODE2  INT_MIN
9301              and decide on the result.  */
9302           if (code2 == LT_EXPR
9303               || code2 == LE_EXPR
9304               || code2 == EQ_EXPR)
9305             return omit_one_operand_loc (loc, type, boolean_false_node, variable);
9306           else if (code2 == NE_EXPR
9307                    || code2 == GE_EXPR
9308                    || code2 == GT_EXPR)
9309             return omit_one_operand_loc (loc, type, boolean_true_node, variable);
9310         }
9311
9312       if (TREE_CODE (lhs) == TREE_CODE (arg1)
9313           && (TREE_CODE (lhs) != INTEGER_CST
9314               || !TREE_OVERFLOW (lhs)))
9315         {
9316           fold_overflow_warning (("assuming signed overflow does not occur "
9317                                   "when changing X +- C1 cmp C2 to "
9318                                   "X cmp C1 +- C2"),
9319                                  WARN_STRICT_OVERFLOW_COMPARISON);
9320           return fold_build2_loc (loc, code, type, variable, lhs);
9321         }
9322     }
9323
9324   /* For comparisons of pointers we can decompose it to a compile time
9325      comparison of the base objects and the offsets into the object.
9326      This requires at least one operand being an ADDR_EXPR or a
9327      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
9328   if (POINTER_TYPE_P (TREE_TYPE (arg0))
9329       && (TREE_CODE (arg0) == ADDR_EXPR
9330           || TREE_CODE (arg1) == ADDR_EXPR
9331           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9332           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9333     {
9334       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9335       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
9336       enum machine_mode mode;
9337       int volatilep, unsignedp;
9338       bool indirect_base0 = false, indirect_base1 = false;
9339
9340       /* Get base and offset for the access.  Strip ADDR_EXPR for
9341          get_inner_reference, but put it back by stripping INDIRECT_REF
9342          off the base object if possible.  indirect_baseN will be true
9343          if baseN is not an address but refers to the object itself.  */
9344       base0 = arg0;
9345       if (TREE_CODE (arg0) == ADDR_EXPR)
9346         {
9347           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
9348                                        &bitsize, &bitpos0, &offset0, &mode,
9349                                        &unsignedp, &volatilep, false);
9350           if (TREE_CODE (base0) == INDIRECT_REF)
9351             base0 = TREE_OPERAND (base0, 0);
9352           else
9353             indirect_base0 = true;
9354         }
9355       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9356         {
9357           base0 = TREE_OPERAND (arg0, 0);
9358           offset0 = TREE_OPERAND (arg0, 1);
9359         }
9360
9361       base1 = arg1;
9362       if (TREE_CODE (arg1) == ADDR_EXPR)
9363         {
9364           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
9365                                        &bitsize, &bitpos1, &offset1, &mode,
9366                                        &unsignedp, &volatilep, false);
9367           if (TREE_CODE (base1) == INDIRECT_REF)
9368             base1 = TREE_OPERAND (base1, 0);
9369           else
9370             indirect_base1 = true;
9371         }
9372       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9373         {
9374           base1 = TREE_OPERAND (arg1, 0);
9375           offset1 = TREE_OPERAND (arg1, 1);
9376         }
9377
9378       /* If we have equivalent bases we might be able to simplify.  */
9379       if (indirect_base0 == indirect_base1
9380           && operand_equal_p (base0, base1, 0))
9381         {
9382           /* We can fold this expression to a constant if the non-constant
9383              offset parts are equal.  */
9384           if ((offset0 == offset1
9385                || (offset0 && offset1
9386                    && operand_equal_p (offset0, offset1, 0)))
9387               && (code == EQ_EXPR
9388                   || code == NE_EXPR
9389                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
9390
9391             {
9392               if (code != EQ_EXPR
9393                   && code != NE_EXPR
9394                   && bitpos0 != bitpos1
9395                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9396                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9397                 fold_overflow_warning (("assuming pointer wraparound does not "
9398                                         "occur when comparing P +- C1 with "
9399                                         "P +- C2"),
9400                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
9401
9402               switch (code)
9403                 {
9404                 case EQ_EXPR:
9405                   return constant_boolean_node (bitpos0 == bitpos1, type);
9406                 case NE_EXPR:
9407                   return constant_boolean_node (bitpos0 != bitpos1, type);
9408                 case LT_EXPR:
9409                   return constant_boolean_node (bitpos0 < bitpos1, type);
9410                 case LE_EXPR:
9411                   return constant_boolean_node (bitpos0 <= bitpos1, type);
9412                 case GE_EXPR:
9413                   return constant_boolean_node (bitpos0 >= bitpos1, type);
9414                 case GT_EXPR:
9415                   return constant_boolean_node (bitpos0 > bitpos1, type);
9416                 default:;
9417                 }
9418             }
9419           /* We can simplify the comparison to a comparison of the variable
9420              offset parts if the constant offset parts are equal.
9421              Be careful to use signed size type here because otherwise we
9422              mess with array offsets in the wrong way.  This is possible
9423              because pointer arithmetic is restricted to retain within an
9424              object and overflow on pointer differences is undefined as of
9425              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
9426           else if (bitpos0 == bitpos1
9427                    && ((code == EQ_EXPR || code == NE_EXPR)
9428                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
9429             {
9430               tree signed_size_type_node;
9431               signed_size_type_node = signed_type_for (size_type_node);
9432
9433               /* By converting to signed size type we cover middle-end pointer
9434                  arithmetic which operates on unsigned pointer types of size
9435                  type size and ARRAY_REF offsets which are properly sign or
9436                  zero extended from their type in case it is narrower than
9437                  size type.  */
9438               if (offset0 == NULL_TREE)
9439                 offset0 = build_int_cst (signed_size_type_node, 0);
9440               else
9441                 offset0 = fold_convert_loc (loc, signed_size_type_node,
9442                                             offset0);
9443               if (offset1 == NULL_TREE)
9444                 offset1 = build_int_cst (signed_size_type_node, 0);
9445               else
9446                 offset1 = fold_convert_loc (loc, signed_size_type_node,
9447                                             offset1);
9448
9449               if (code != EQ_EXPR
9450                   && code != NE_EXPR
9451                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9452                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9453                 fold_overflow_warning (("assuming pointer wraparound does not "
9454                                         "occur when comparing P +- C1 with "
9455                                         "P +- C2"),
9456                                        WARN_STRICT_OVERFLOW_COMPARISON);
9457
9458               return fold_build2_loc (loc, code, type, offset0, offset1);
9459             }
9460         }
9461       /* For non-equal bases we can simplify if they are addresses
9462          of local binding decls or constants.  */
9463       else if (indirect_base0 && indirect_base1
9464                /* We know that !operand_equal_p (base0, base1, 0)
9465                   because the if condition was false.  But make
9466                   sure two decls are not the same.  */
9467                && base0 != base1
9468                && TREE_CODE (arg0) == ADDR_EXPR
9469                && TREE_CODE (arg1) == ADDR_EXPR
9470                && (((TREE_CODE (base0) == VAR_DECL
9471                      || TREE_CODE (base0) == PARM_DECL)
9472                     && (targetm.binds_local_p (base0)
9473                         || CONSTANT_CLASS_P (base1)))
9474                    || CONSTANT_CLASS_P (base0))
9475                && (((TREE_CODE (base1) == VAR_DECL
9476                      || TREE_CODE (base1) == PARM_DECL)
9477                     && (targetm.binds_local_p (base1)
9478                         || CONSTANT_CLASS_P (base0)))
9479                    || CONSTANT_CLASS_P (base1)))
9480         {
9481           if (code == EQ_EXPR)
9482             return omit_two_operands_loc (loc, type, boolean_false_node,
9483                                       arg0, arg1);
9484           else if (code == NE_EXPR)
9485             return omit_two_operands_loc (loc, type, boolean_true_node,
9486                                       arg0, arg1);
9487         }
9488       /* For equal offsets we can simplify to a comparison of the
9489          base addresses.  */
9490       else if (bitpos0 == bitpos1
9491                && (indirect_base0
9492                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9493                && (indirect_base1
9494                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9495                && ((offset0 == offset1)
9496                    || (offset0 && offset1
9497                        && operand_equal_p (offset0, offset1, 0))))
9498         {
9499           if (indirect_base0)
9500             base0 = build_fold_addr_expr_loc (loc, base0);
9501           if (indirect_base1)
9502             base1 = build_fold_addr_expr_loc (loc, base1);
9503           return fold_build2_loc (loc, code, type, base0, base1);
9504         }
9505     }
9506
9507   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9508      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
9509      the resulting offset is smaller in absolute value than the
9510      original one.  */
9511   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9512       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9513       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9514           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9515       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9516       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9517           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9518     {
9519       tree const1 = TREE_OPERAND (arg0, 1);
9520       tree const2 = TREE_OPERAND (arg1, 1);
9521       tree variable1 = TREE_OPERAND (arg0, 0);
9522       tree variable2 = TREE_OPERAND (arg1, 0);
9523       tree cst;
9524       const char * const warnmsg = G_("assuming signed overflow does not "
9525                                       "occur when combining constants around "
9526                                       "a comparison");
9527
9528       /* Put the constant on the side where it doesn't overflow and is
9529          of lower absolute value than before.  */
9530       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9531                              ? MINUS_EXPR : PLUS_EXPR,
9532                              const2, const1, 0);
9533       if (!TREE_OVERFLOW (cst)
9534           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9535         {
9536           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9537           return fold_build2_loc (loc, code, type,
9538                               variable1,
9539                               fold_build2_loc (loc,
9540                                            TREE_CODE (arg1), TREE_TYPE (arg1),
9541                                            variable2, cst));
9542         }
9543
9544       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9545                              ? MINUS_EXPR : PLUS_EXPR,
9546                              const1, const2, 0);
9547       if (!TREE_OVERFLOW (cst)
9548           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9549         {
9550           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9551           return fold_build2_loc (loc, code, type,
9552                               fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
9553                                            variable1, cst),
9554                               variable2);
9555         }
9556     }
9557
9558   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9559      signed arithmetic case.  That form is created by the compiler
9560      often enough for folding it to be of value.  One example is in
9561      computing loop trip counts after Operator Strength Reduction.  */
9562   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9563       && TREE_CODE (arg0) == MULT_EXPR
9564       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9565           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9566       && integer_zerop (arg1))
9567     {
9568       tree const1 = TREE_OPERAND (arg0, 1);
9569       tree const2 = arg1;                       /* zero */
9570       tree variable1 = TREE_OPERAND (arg0, 0);
9571       enum tree_code cmp_code = code;
9572
9573       gcc_assert (!integer_zerop (const1));
9574
9575       fold_overflow_warning (("assuming signed overflow does not occur when "
9576                               "eliminating multiplication in comparison "
9577                               "with zero"),
9578                              WARN_STRICT_OVERFLOW_COMPARISON);
9579
9580       /* If const1 is negative we swap the sense of the comparison.  */
9581       if (tree_int_cst_sgn (const1) < 0)
9582         cmp_code = swap_tree_comparison (cmp_code);
9583
9584       return fold_build2_loc (loc, cmp_code, type, variable1, const2);
9585     }
9586
9587   tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
9588   if (tem)
9589     return tem;
9590
9591   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9592     {
9593       tree targ0 = strip_float_extensions (arg0);
9594       tree targ1 = strip_float_extensions (arg1);
9595       tree newtype = TREE_TYPE (targ0);
9596
9597       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9598         newtype = TREE_TYPE (targ1);
9599
9600       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9601       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9602         return fold_build2_loc (loc, code, type,
9603                             fold_convert_loc (loc, newtype, targ0),
9604                             fold_convert_loc (loc, newtype, targ1));
9605
9606       /* (-a) CMP (-b) -> b CMP a  */
9607       if (TREE_CODE (arg0) == NEGATE_EXPR
9608           && TREE_CODE (arg1) == NEGATE_EXPR)
9609         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
9610                             TREE_OPERAND (arg0, 0));
9611
9612       if (TREE_CODE (arg1) == REAL_CST)
9613         {
9614           REAL_VALUE_TYPE cst;
9615           cst = TREE_REAL_CST (arg1);
9616
9617           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
9618           if (TREE_CODE (arg0) == NEGATE_EXPR)
9619             return fold_build2_loc (loc, swap_tree_comparison (code), type,
9620                                 TREE_OPERAND (arg0, 0),
9621                                 build_real (TREE_TYPE (arg1),
9622                                             REAL_VALUE_NEGATE (cst)));
9623
9624           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
9625           /* a CMP (-0) -> a CMP 0  */
9626           if (REAL_VALUE_MINUS_ZERO (cst))
9627             return fold_build2_loc (loc, code, type, arg0,
9628                                 build_real (TREE_TYPE (arg1), dconst0));
9629
9630           /* x != NaN is always true, other ops are always false.  */
9631           if (REAL_VALUE_ISNAN (cst)
9632               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9633             {
9634               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9635               return omit_one_operand_loc (loc, type, tem, arg0);
9636             }
9637
9638           /* Fold comparisons against infinity.  */
9639           if (REAL_VALUE_ISINF (cst)
9640               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9641             {
9642               tem = fold_inf_compare (loc, code, type, arg0, arg1);
9643               if (tem != NULL_TREE)
9644                 return tem;
9645             }
9646         }
9647
9648       /* If this is a comparison of a real constant with a PLUS_EXPR
9649          or a MINUS_EXPR of a real constant, we can convert it into a
9650          comparison with a revised real constant as long as no overflow
9651          occurs when unsafe_math_optimizations are enabled.  */
9652       if (flag_unsafe_math_optimizations
9653           && TREE_CODE (arg1) == REAL_CST
9654           && (TREE_CODE (arg0) == PLUS_EXPR
9655               || TREE_CODE (arg0) == MINUS_EXPR)
9656           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9657           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9658                                       ? MINUS_EXPR : PLUS_EXPR,
9659                                       arg1, TREE_OPERAND (arg0, 1), 0))
9660           && !TREE_OVERFLOW (tem))
9661         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9662
9663       /* Likewise, we can simplify a comparison of a real constant with
9664          a MINUS_EXPR whose first operand is also a real constant, i.e.
9665          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
9666          floating-point types only if -fassociative-math is set.  */
9667       if (flag_associative_math
9668           && TREE_CODE (arg1) == REAL_CST
9669           && TREE_CODE (arg0) == MINUS_EXPR
9670           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9671           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9672                                       arg1, 0))
9673           && !TREE_OVERFLOW (tem))
9674         return fold_build2_loc (loc, swap_tree_comparison (code), type,
9675                             TREE_OPERAND (arg0, 1), tem);
9676
9677       /* Fold comparisons against built-in math functions.  */
9678       if (TREE_CODE (arg1) == REAL_CST
9679           && flag_unsafe_math_optimizations
9680           && ! flag_errno_math)
9681         {
9682           enum built_in_function fcode = builtin_mathfn_code (arg0);
9683
9684           if (fcode != END_BUILTINS)
9685             {
9686               tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9687               if (tem != NULL_TREE)
9688                 return tem;
9689             }
9690         }
9691     }
9692
9693   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9694       && CONVERT_EXPR_P (arg0))
9695     {
9696       /* If we are widening one operand of an integer comparison,
9697          see if the other operand is similarly being widened.  Perhaps we
9698          can do the comparison in the narrower type.  */
9699       tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9700       if (tem)
9701         return tem;
9702
9703       /* Or if we are changing signedness.  */
9704       tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9705       if (tem)
9706         return tem;
9707     }
9708
9709   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9710      constant, we can simplify it.  */
9711   if (TREE_CODE (arg1) == INTEGER_CST
9712       && (TREE_CODE (arg0) == MIN_EXPR
9713           || TREE_CODE (arg0) == MAX_EXPR)
9714       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9715     {
9716       tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9717       if (tem)
9718         return tem;
9719     }
9720
9721   /* Simplify comparison of something with itself.  (For IEEE
9722      floating-point, we can only do some of these simplifications.)  */
9723   if (operand_equal_p (arg0, arg1, 0))
9724     {
9725       switch (code)
9726         {
9727         case EQ_EXPR:
9728           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9729               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9730             return constant_boolean_node (1, type);
9731           break;
9732
9733         case GE_EXPR:
9734         case LE_EXPR:
9735           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9736               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9737             return constant_boolean_node (1, type);
9738           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9739
9740         case NE_EXPR:
9741           /* For NE, we can only do this simplification if integer
9742              or we don't honor IEEE floating point NaNs.  */
9743           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9744               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9745             break;
9746           /* ... fall through ...  */
9747         case GT_EXPR:
9748         case LT_EXPR:
9749           return constant_boolean_node (0, type);
9750         default:
9751           gcc_unreachable ();
9752         }
9753     }
9754
9755   /* If we are comparing an expression that just has comparisons
9756      of two integer values, arithmetic expressions of those comparisons,
9757      and constants, we can simplify it.  There are only three cases
9758      to check: the two values can either be equal, the first can be
9759      greater, or the second can be greater.  Fold the expression for
9760      those three values.  Since each value must be 0 or 1, we have
9761      eight possibilities, each of which corresponds to the constant 0
9762      or 1 or one of the six possible comparisons.
9763
9764      This handles common cases like (a > b) == 0 but also handles
9765      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9766      occur in macroized code.  */
9767
9768   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9769     {
9770       tree cval1 = 0, cval2 = 0;
9771       int save_p = 0;
9772
9773       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9774           /* Don't handle degenerate cases here; they should already
9775              have been handled anyway.  */
9776           && cval1 != 0 && cval2 != 0
9777           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9778           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9779           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9780           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9781           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9782           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9783                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9784         {
9785           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9786           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9787
9788           /* We can't just pass T to eval_subst in case cval1 or cval2
9789              was the same as ARG1.  */
9790
9791           tree high_result
9792                 = fold_build2_loc (loc, code, type,
9793                                eval_subst (loc, arg0, cval1, maxval,
9794                                            cval2, minval),
9795                                arg1);
9796           tree equal_result
9797                 = fold_build2_loc (loc, code, type,
9798                                eval_subst (loc, arg0, cval1, maxval,
9799                                            cval2, maxval),
9800                                arg1);
9801           tree low_result
9802                 = fold_build2_loc (loc, code, type,
9803                                eval_subst (loc, arg0, cval1, minval,
9804                                            cval2, maxval),
9805                                arg1);
9806
9807           /* All three of these results should be 0 or 1.  Confirm they are.
9808              Then use those values to select the proper code to use.  */
9809
9810           if (TREE_CODE (high_result) == INTEGER_CST
9811               && TREE_CODE (equal_result) == INTEGER_CST
9812               && TREE_CODE (low_result) == INTEGER_CST)
9813             {
9814               /* Make a 3-bit mask with the high-order bit being the
9815                  value for `>', the next for '=', and the low for '<'.  */
9816               switch ((integer_onep (high_result) * 4)
9817                       + (integer_onep (equal_result) * 2)
9818                       + integer_onep (low_result))
9819                 {
9820                 case 0:
9821                   /* Always false.  */
9822                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9823                 case 1:
9824                   code = LT_EXPR;
9825                   break;
9826                 case 2:
9827                   code = EQ_EXPR;
9828                   break;
9829                 case 3:
9830                   code = LE_EXPR;
9831                   break;
9832                 case 4:
9833                   code = GT_EXPR;
9834                   break;
9835                 case 5:
9836                   code = NE_EXPR;
9837                   break;
9838                 case 6:
9839                   code = GE_EXPR;
9840                   break;
9841                 case 7:
9842                   /* Always true.  */
9843                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9844                 }
9845
9846               if (save_p)
9847                 {
9848                   tem = save_expr (build2 (code, type, cval1, cval2));
9849                   SET_EXPR_LOCATION (tem, loc);
9850                   return tem;
9851                 }
9852               return fold_build2_loc (loc, code, type, cval1, cval2);
9853             }
9854         }
9855     }
9856
9857   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9858      into a single range test.  */
9859   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9860        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9861       && TREE_CODE (arg1) == INTEGER_CST
9862       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9863       && !integer_zerop (TREE_OPERAND (arg0, 1))
9864       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9865       && !TREE_OVERFLOW (arg1))
9866     {
9867       tem = fold_div_compare (loc, code, type, arg0, arg1);
9868       if (tem != NULL_TREE)
9869         return tem;
9870     }
9871
9872   /* Fold ~X op ~Y as Y op X.  */
9873   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9874       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9875     {
9876       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9877       return fold_build2_loc (loc, code, type,
9878                           fold_convert_loc (loc, cmp_type,
9879                                             TREE_OPERAND (arg1, 0)),
9880                           TREE_OPERAND (arg0, 0));
9881     }
9882
9883   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9884   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9885       && TREE_CODE (arg1) == INTEGER_CST)
9886     {
9887       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9888       return fold_build2_loc (loc, swap_tree_comparison (code), type,
9889                           TREE_OPERAND (arg0, 0),
9890                           fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9891                                        fold_convert_loc (loc, cmp_type, arg1)));
9892     }
9893
9894   return NULL_TREE;
9895 }
9896
9897
9898 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9899    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9900    argument EXPR represents the expression "z" of type TYPE.  */
9901
9902 static tree
9903 fold_mult_zconjz (location_t loc, tree type, tree expr)
9904 {
9905   tree itype = TREE_TYPE (type);
9906   tree rpart, ipart, tem;
9907
9908   if (TREE_CODE (expr) == COMPLEX_EXPR)
9909     {
9910       rpart = TREE_OPERAND (expr, 0);
9911       ipart = TREE_OPERAND (expr, 1);
9912     }
9913   else if (TREE_CODE (expr) == COMPLEX_CST)
9914     {
9915       rpart = TREE_REALPART (expr);
9916       ipart = TREE_IMAGPART (expr);
9917     }
9918   else
9919     {
9920       expr = save_expr (expr);
9921       rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9922       ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9923     }
9924
9925   rpart = save_expr (rpart);
9926   ipart = save_expr (ipart);
9927   tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9928                      fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9929                      fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9930   return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9931                       fold_convert_loc (loc, itype, integer_zero_node));
9932 }
9933
9934
9935 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9936    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9937    guarantees that P and N have the same least significant log2(M) bits.
9938    N is not otherwise constrained.  In particular, N is not normalized to
9939    0 <= N < M as is common.  In general, the precise value of P is unknown.
9940    M is chosen as large as possible such that constant N can be determined.
9941
9942    Returns M and sets *RESIDUE to N.
9943
9944    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9945    account.  This is not always possible due to PR 35705.
9946  */
9947
9948 static unsigned HOST_WIDE_INT
9949 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9950                                  bool allow_func_align)
9951 {
9952   enum tree_code code;
9953
9954   *residue = 0;
9955
9956   code = TREE_CODE (expr);
9957   if (code == ADDR_EXPR)
9958     {
9959       expr = TREE_OPERAND (expr, 0);
9960       if (handled_component_p (expr))
9961         {
9962           HOST_WIDE_INT bitsize, bitpos;
9963           tree offset;
9964           enum machine_mode mode;
9965           int unsignedp, volatilep;
9966
9967           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9968                                       &mode, &unsignedp, &volatilep, false);
9969           *residue = bitpos / BITS_PER_UNIT;
9970           if (offset)
9971             {
9972               if (TREE_CODE (offset) == INTEGER_CST)
9973                 *residue += TREE_INT_CST_LOW (offset);
9974               else
9975                 /* We don't handle more complicated offset expressions.  */
9976                 return 1;
9977             }
9978         }
9979
9980       if (DECL_P (expr)
9981           && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9982         return DECL_ALIGN_UNIT (expr);
9983     }
9984   else if (code == POINTER_PLUS_EXPR)
9985     {
9986       tree op0, op1;
9987       unsigned HOST_WIDE_INT modulus;
9988       enum tree_code inner_code;
9989
9990       op0 = TREE_OPERAND (expr, 0);
9991       STRIP_NOPS (op0);
9992       modulus = get_pointer_modulus_and_residue (op0, residue,
9993                                                  allow_func_align);
9994
9995       op1 = TREE_OPERAND (expr, 1);
9996       STRIP_NOPS (op1);
9997       inner_code = TREE_CODE (op1);
9998       if (inner_code == INTEGER_CST)
9999         {
10000           *residue += TREE_INT_CST_LOW (op1);
10001           return modulus;
10002         }
10003       else if (inner_code == MULT_EXPR)
10004         {
10005           op1 = TREE_OPERAND (op1, 1);
10006           if (TREE_CODE (op1) == INTEGER_CST)
10007             {
10008               unsigned HOST_WIDE_INT align;
10009
10010               /* Compute the greatest power-of-2 divisor of op1.  */
10011               align = TREE_INT_CST_LOW (op1);
10012               align &= -align;
10013
10014               /* If align is non-zero and less than *modulus, replace
10015                  *modulus with align., If align is 0, then either op1 is 0
10016                  or the greatest power-of-2 divisor of op1 doesn't fit in an
10017                  unsigned HOST_WIDE_INT.  In either case, no additional
10018                  constraint is imposed.  */
10019               if (align)
10020                 modulus = MIN (modulus, align);
10021
10022               return modulus;
10023             }
10024         }
10025     }
10026
10027     /* If we get here, we were unable to determine anything useful about the
10028        expression.  */
10029     return 1;
10030 }
10031
10032
10033 /* Fold a binary expression of code CODE and type TYPE with operands
10034    OP0 and OP1.  LOC is the location of the resulting expression.
10035    Return the folded expression if folding is successful.  Otherwise,
10036    return NULL_TREE.  */
10037
10038 tree
10039 fold_binary_loc (location_t loc,
10040              enum tree_code code, tree type, tree op0, tree op1)
10041 {
10042   enum tree_code_class kind = TREE_CODE_CLASS (code);
10043   tree arg0, arg1, tem;
10044   tree t1 = NULL_TREE;
10045   bool strict_overflow_p;
10046
10047   gcc_assert (IS_EXPR_CODE_CLASS (kind)
10048               && TREE_CODE_LENGTH (code) == 2
10049               && op0 != NULL_TREE
10050               && op1 != NULL_TREE);
10051
10052   arg0 = op0;
10053   arg1 = op1;
10054
10055   /* Strip any conversions that don't change the mode.  This is
10056      safe for every expression, except for a comparison expression
10057      because its signedness is derived from its operands.  So, in
10058      the latter case, only strip conversions that don't change the
10059      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
10060      preserved.
10061
10062      Note that this is done as an internal manipulation within the
10063      constant folder, in order to find the simplest representation
10064      of the arguments so that their form can be studied.  In any
10065      cases, the appropriate type conversions should be put back in
10066      the tree that will get out of the constant folder.  */
10067
10068   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10069     {
10070       STRIP_SIGN_NOPS (arg0);
10071       STRIP_SIGN_NOPS (arg1);
10072     }
10073   else
10074     {
10075       STRIP_NOPS (arg0);
10076       STRIP_NOPS (arg1);
10077     }
10078
10079   /* Note that TREE_CONSTANT isn't enough: static var addresses are
10080      constant but we can't do arithmetic on them.  */
10081   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10082       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
10083       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
10084       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
10085       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
10086       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
10087     {
10088       if (kind == tcc_binary)
10089         {
10090           /* Make sure type and arg0 have the same saturating flag.  */
10091           gcc_assert (TYPE_SATURATING (type)
10092                       == TYPE_SATURATING (TREE_TYPE (arg0)));
10093           tem = const_binop (code, arg0, arg1, 0);
10094         }
10095       else if (kind == tcc_comparison)
10096         tem = fold_relational_const (code, type, arg0, arg1);
10097       else
10098         tem = NULL_TREE;
10099
10100       if (tem != NULL_TREE)
10101         {
10102           if (TREE_TYPE (tem) != type)
10103             tem = fold_convert_loc (loc, type, tem);
10104           return tem;
10105         }
10106     }
10107
10108   /* If this is a commutative operation, and ARG0 is a constant, move it
10109      to ARG1 to reduce the number of tests below.  */
10110   if (commutative_tree_code (code)
10111       && tree_swap_operands_p (arg0, arg1, true))
10112     return fold_build2_loc (loc, code, type, op1, op0);
10113
10114   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10115
10116      First check for cases where an arithmetic operation is applied to a
10117      compound, conditional, or comparison operation.  Push the arithmetic
10118      operation inside the compound or conditional to see if any folding
10119      can then be done.  Convert comparison to conditional for this purpose.
10120      The also optimizes non-constant cases that used to be done in
10121      expand_expr.
10122
10123      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10124      one of the operands is a comparison and the other is a comparison, a
10125      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
10126      code below would make the expression more complex.  Change it to a
10127      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
10128      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
10129
10130   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
10131        || code == EQ_EXPR || code == NE_EXPR)
10132       && ((truth_value_p (TREE_CODE (arg0))
10133            && (truth_value_p (TREE_CODE (arg1))
10134                || (TREE_CODE (arg1) == BIT_AND_EXPR
10135                    && integer_onep (TREE_OPERAND (arg1, 1)))))
10136           || (truth_value_p (TREE_CODE (arg1))
10137               && (truth_value_p (TREE_CODE (arg0))
10138                   || (TREE_CODE (arg0) == BIT_AND_EXPR
10139                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
10140     {
10141       tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
10142                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
10143                          : TRUTH_XOR_EXPR,
10144                          boolean_type_node,
10145                          fold_convert_loc (loc, boolean_type_node, arg0),
10146                          fold_convert_loc (loc, boolean_type_node, arg1));
10147
10148       if (code == EQ_EXPR)
10149         tem = invert_truthvalue_loc (loc, tem);
10150
10151       return fold_convert_loc (loc, type, tem);
10152     }
10153
10154   if (TREE_CODE_CLASS (code) == tcc_binary
10155       || TREE_CODE_CLASS (code) == tcc_comparison)
10156     {
10157       if (TREE_CODE (arg0) == COMPOUND_EXPR)
10158         {
10159           tem = fold_build2_loc (loc, code, type,
10160                              fold_convert_loc (loc, TREE_TYPE (op0),
10161                                                TREE_OPERAND (arg0, 1)), op1);
10162           tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), tem);
10163           goto fold_binary_exit;
10164         }
10165       if (TREE_CODE (arg1) == COMPOUND_EXPR
10166           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10167         {
10168           tem = fold_build2_loc (loc, code, type, op0,
10169                              fold_convert_loc (loc, TREE_TYPE (op1),
10170                                                TREE_OPERAND (arg1, 1)));
10171           tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), tem);
10172           goto fold_binary_exit;
10173         }
10174
10175       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
10176         {
10177           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10178                                                      arg0, arg1,
10179                                                      /*cond_first_p=*/1);
10180           if (tem != NULL_TREE)
10181             return tem;
10182         }
10183
10184       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
10185         {
10186           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10187                                                      arg1, arg0,
10188                                                      /*cond_first_p=*/0);
10189           if (tem != NULL_TREE)
10190             return tem;
10191         }
10192     }
10193
10194   switch (code)
10195     {
10196     case POINTER_PLUS_EXPR:
10197       /* 0 +p index -> (type)index */
10198       if (integer_zerop (arg0))
10199         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10200
10201       /* PTR +p 0 -> PTR */
10202       if (integer_zerop (arg1))
10203         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10204
10205       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
10206       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10207            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
10208         return fold_convert_loc (loc, type,
10209                                  fold_build2_loc (loc, PLUS_EXPR, sizetype,
10210                                               fold_convert_loc (loc, sizetype,
10211                                                                 arg1),
10212                                               fold_convert_loc (loc, sizetype,
10213                                                                 arg0)));
10214
10215       /* index +p PTR -> PTR +p index */
10216       if (POINTER_TYPE_P (TREE_TYPE (arg1))
10217           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
10218         return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
10219                             fold_convert_loc (loc, type, arg1),
10220                             fold_convert_loc (loc, sizetype, arg0));
10221
10222       /* (PTR +p B) +p A -> PTR +p (B + A) */
10223       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10224         {
10225           tree inner;
10226           tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
10227           tree arg00 = TREE_OPERAND (arg0, 0);
10228           inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
10229                                arg01, fold_convert_loc (loc, sizetype, arg1));
10230           return fold_convert_loc (loc, type,
10231                                    fold_build2_loc (loc, POINTER_PLUS_EXPR,
10232                                                 TREE_TYPE (arg00),
10233                                                 arg00, inner));
10234         }
10235
10236       /* PTR_CST +p CST -> CST1 */
10237       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10238         return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
10239                             fold_convert_loc (loc, type, arg1));
10240
10241      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
10242         of the array.  Loop optimizer sometimes produce this type of
10243         expressions.  */
10244       if (TREE_CODE (arg0) == ADDR_EXPR)
10245         {
10246           tem = try_move_mult_to_index (loc, arg0,
10247                                         fold_convert_loc (loc, sizetype, arg1));
10248           if (tem)
10249             return fold_convert_loc (loc, type, tem);
10250         }
10251
10252       return NULL_TREE;
10253
10254     case PLUS_EXPR:
10255       /* A + (-B) -> A - B */
10256       if (TREE_CODE (arg1) == NEGATE_EXPR)
10257         return fold_build2_loc (loc, MINUS_EXPR, type,
10258                             fold_convert_loc (loc, type, arg0),
10259                             fold_convert_loc (loc, type,
10260                                               TREE_OPERAND (arg1, 0)));
10261       /* (-A) + B -> B - A */
10262       if (TREE_CODE (arg0) == NEGATE_EXPR
10263           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
10264         return fold_build2_loc (loc, MINUS_EXPR, type,
10265                             fold_convert_loc (loc, type, arg1),
10266                             fold_convert_loc (loc, type,
10267                                               TREE_OPERAND (arg0, 0)));
10268
10269       if (INTEGRAL_TYPE_P (type))
10270         {
10271           /* Convert ~A + 1 to -A.  */
10272           if (TREE_CODE (arg0) == BIT_NOT_EXPR
10273               && integer_onep (arg1))
10274             return fold_build1_loc (loc, NEGATE_EXPR, type,
10275                                 fold_convert_loc (loc, type,
10276                                                   TREE_OPERAND (arg0, 0)));
10277
10278           /* ~X + X is -1.  */
10279           if (TREE_CODE (arg0) == BIT_NOT_EXPR
10280               && !TYPE_OVERFLOW_TRAPS (type))
10281             {
10282               tree tem = TREE_OPERAND (arg0, 0);
10283
10284               STRIP_NOPS (tem);
10285               if (operand_equal_p (tem, arg1, 0))
10286                 {
10287                   t1 = build_int_cst_type (type, -1);
10288                   return omit_one_operand_loc (loc, type, t1, arg1);
10289                 }
10290             }
10291
10292           /* X + ~X is -1.  */
10293           if (TREE_CODE (arg1) == BIT_NOT_EXPR
10294               && !TYPE_OVERFLOW_TRAPS (type))
10295             {
10296               tree tem = TREE_OPERAND (arg1, 0);
10297
10298               STRIP_NOPS (tem);
10299               if (operand_equal_p (arg0, tem, 0))
10300                 {
10301                   t1 = build_int_cst_type (type, -1);
10302                   return omit_one_operand_loc (loc, type, t1, arg0);
10303                 }
10304             }
10305
10306           /* X + (X / CST) * -CST is X % CST.  */
10307           if (TREE_CODE (arg1) == MULT_EXPR
10308               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10309               && operand_equal_p (arg0,
10310                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
10311             {
10312               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
10313               tree cst1 = TREE_OPERAND (arg1, 1);
10314               tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
10315                                       cst1, cst0);
10316               if (sum && integer_zerop (sum))
10317                 return fold_convert_loc (loc, type,
10318                                          fold_build2_loc (loc, TRUNC_MOD_EXPR,
10319                                                       TREE_TYPE (arg0), arg0,
10320                                                       cst0));
10321             }
10322         }
10323
10324       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
10325          same or one.  Make sure type is not saturating.
10326          fold_plusminus_mult_expr will re-associate.  */
10327       if ((TREE_CODE (arg0) == MULT_EXPR
10328            || TREE_CODE (arg1) == MULT_EXPR)
10329           && !TYPE_SATURATING (type)
10330           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10331         {
10332           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10333           if (tem)
10334             return tem;
10335         }
10336
10337       if (! FLOAT_TYPE_P (type))
10338         {
10339           if (integer_zerop (arg1))
10340             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10341
10342           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
10343              with a constant, and the two constants have no bits in common,
10344              we should treat this as a BIT_IOR_EXPR since this may produce more
10345              simplifications.  */
10346           if (TREE_CODE (arg0) == BIT_AND_EXPR
10347               && TREE_CODE (arg1) == BIT_AND_EXPR
10348               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10349               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10350               && integer_zerop (const_binop (BIT_AND_EXPR,
10351                                              TREE_OPERAND (arg0, 1),
10352                                              TREE_OPERAND (arg1, 1), 0)))
10353             {
10354               code = BIT_IOR_EXPR;
10355               goto bit_ior;
10356             }
10357
10358           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10359              (plus (plus (mult) (mult)) (foo)) so that we can
10360              take advantage of the factoring cases below.  */
10361           if (((TREE_CODE (arg0) == PLUS_EXPR
10362                 || TREE_CODE (arg0) == MINUS_EXPR)
10363                && TREE_CODE (arg1) == MULT_EXPR)
10364               || ((TREE_CODE (arg1) == PLUS_EXPR
10365                    || TREE_CODE (arg1) == MINUS_EXPR)
10366                   && TREE_CODE (arg0) == MULT_EXPR))
10367             {
10368               tree parg0, parg1, parg, marg;
10369               enum tree_code pcode;
10370
10371               if (TREE_CODE (arg1) == MULT_EXPR)
10372                 parg = arg0, marg = arg1;
10373               else
10374                 parg = arg1, marg = arg0;
10375               pcode = TREE_CODE (parg);
10376               parg0 = TREE_OPERAND (parg, 0);
10377               parg1 = TREE_OPERAND (parg, 1);
10378               STRIP_NOPS (parg0);
10379               STRIP_NOPS (parg1);
10380
10381               if (TREE_CODE (parg0) == MULT_EXPR
10382                   && TREE_CODE (parg1) != MULT_EXPR)
10383                 return fold_build2_loc (loc, pcode, type,
10384                                     fold_build2_loc (loc, PLUS_EXPR, type,
10385                                                  fold_convert_loc (loc, type,
10386                                                                    parg0),
10387                                                  fold_convert_loc (loc, type,
10388                                                                    marg)),
10389                                     fold_convert_loc (loc, type, parg1));
10390               if (TREE_CODE (parg0) != MULT_EXPR
10391                   && TREE_CODE (parg1) == MULT_EXPR)
10392                 return
10393                   fold_build2_loc (loc, PLUS_EXPR, type,
10394                                fold_convert_loc (loc, type, parg0),
10395                                fold_build2_loc (loc, pcode, type,
10396                                             fold_convert_loc (loc, type, marg),
10397                                             fold_convert_loc (loc, type,
10398                                                               parg1)));
10399             }
10400         }
10401       else
10402         {
10403           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
10404           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10405             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10406
10407           /* Likewise if the operands are reversed.  */
10408           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10409             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10410
10411           /* Convert X + -C into X - C.  */
10412           if (TREE_CODE (arg1) == REAL_CST
10413               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10414             {
10415               tem = fold_negate_const (arg1, type);
10416               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10417                 return fold_build2_loc (loc, MINUS_EXPR, type,
10418                                     fold_convert_loc (loc, type, arg0),
10419                                     fold_convert_loc (loc, type, tem));
10420             }
10421
10422           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10423              to __complex__ ( x, y ).  This is not the same for SNaNs or
10424              if signed zeros are involved.  */
10425           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10426               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10427               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10428             {
10429               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10430               tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10431               tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10432               bool arg0rz = false, arg0iz = false;
10433               if ((arg0r && (arg0rz = real_zerop (arg0r)))
10434                   || (arg0i && (arg0iz = real_zerop (arg0i))))
10435                 {
10436                   tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10437                   tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10438                   if (arg0rz && arg1i && real_zerop (arg1i))
10439                     {
10440                       tree rp = arg1r ? arg1r
10441                                   : build1 (REALPART_EXPR, rtype, arg1);
10442                       tree ip = arg0i ? arg0i
10443                                   : build1 (IMAGPART_EXPR, rtype, arg0);
10444                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10445                     }
10446                   else if (arg0iz && arg1r && real_zerop (arg1r))
10447                     {
10448                       tree rp = arg0r ? arg0r
10449                                   : build1 (REALPART_EXPR, rtype, arg0);
10450                       tree ip = arg1i ? arg1i
10451                                   : build1 (IMAGPART_EXPR, rtype, arg1);
10452                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10453                     }
10454                 }
10455             }
10456
10457           if (flag_unsafe_math_optimizations
10458               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10459               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10460               && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10461             return tem;
10462
10463           /* Convert x+x into x*2.0.  */
10464           if (operand_equal_p (arg0, arg1, 0)
10465               && SCALAR_FLOAT_TYPE_P (type))
10466             return fold_build2_loc (loc, MULT_EXPR, type, arg0,
10467                                 build_real (type, dconst2));
10468
10469           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10470              We associate floats only if the user has specified
10471              -fassociative-math.  */
10472           if (flag_associative_math
10473               && TREE_CODE (arg1) == PLUS_EXPR
10474               && TREE_CODE (arg0) != MULT_EXPR)
10475             {
10476               tree tree10 = TREE_OPERAND (arg1, 0);
10477               tree tree11 = TREE_OPERAND (arg1, 1);
10478               if (TREE_CODE (tree11) == MULT_EXPR
10479                   && TREE_CODE (tree10) == MULT_EXPR)
10480                 {
10481                   tree tree0;
10482                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
10483                   return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
10484                 }
10485             }
10486           /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10487              We associate floats only if the user has specified
10488              -fassociative-math.  */
10489           if (flag_associative_math
10490               && TREE_CODE (arg0) == PLUS_EXPR
10491               && TREE_CODE (arg1) != MULT_EXPR)
10492             {
10493               tree tree00 = TREE_OPERAND (arg0, 0);
10494               tree tree01 = TREE_OPERAND (arg0, 1);
10495               if (TREE_CODE (tree01) == MULT_EXPR
10496                   && TREE_CODE (tree00) == MULT_EXPR)
10497                 {
10498                   tree tree0;
10499                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
10500                   return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
10501                 }
10502             }
10503         }
10504
10505      bit_rotate:
10506       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10507          is a rotate of A by C1 bits.  */
10508       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10509          is a rotate of A by B bits.  */
10510       {
10511         enum tree_code code0, code1;
10512         tree rtype;
10513         code0 = TREE_CODE (arg0);
10514         code1 = TREE_CODE (arg1);
10515         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10516              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10517             && operand_equal_p (TREE_OPERAND (arg0, 0),
10518                                 TREE_OPERAND (arg1, 0), 0)
10519             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10520                 TYPE_UNSIGNED (rtype))
10521             /* Only create rotates in complete modes.  Other cases are not
10522                expanded properly.  */
10523             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10524           {
10525             tree tree01, tree11;
10526             enum tree_code code01, code11;
10527
10528             tree01 = TREE_OPERAND (arg0, 1);
10529             tree11 = TREE_OPERAND (arg1, 1);
10530             STRIP_NOPS (tree01);
10531             STRIP_NOPS (tree11);
10532             code01 = TREE_CODE (tree01);
10533             code11 = TREE_CODE (tree11);
10534             if (code01 == INTEGER_CST
10535                 && code11 == INTEGER_CST
10536                 && TREE_INT_CST_HIGH (tree01) == 0
10537                 && TREE_INT_CST_HIGH (tree11) == 0
10538                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10539                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10540               {
10541                 tem = build2 (LROTATE_EXPR,
10542                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
10543                               TREE_OPERAND (arg0, 0),
10544                               code0 == LSHIFT_EXPR
10545                               ? tree01 : tree11);
10546                 SET_EXPR_LOCATION (tem, loc);
10547                 return fold_convert_loc (loc, type, tem);
10548               }
10549             else if (code11 == MINUS_EXPR)
10550               {
10551                 tree tree110, tree111;
10552                 tree110 = TREE_OPERAND (tree11, 0);
10553                 tree111 = TREE_OPERAND (tree11, 1);
10554                 STRIP_NOPS (tree110);
10555                 STRIP_NOPS (tree111);
10556                 if (TREE_CODE (tree110) == INTEGER_CST
10557                     && 0 == compare_tree_int (tree110,
10558                                               TYPE_PRECISION
10559                                               (TREE_TYPE (TREE_OPERAND
10560                                                           (arg0, 0))))
10561                     && operand_equal_p (tree01, tree111, 0))
10562                   return
10563                     fold_convert_loc (loc, type,
10564                                       build2 ((code0 == LSHIFT_EXPR
10565                                                ? LROTATE_EXPR
10566                                                : RROTATE_EXPR),
10567                                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
10568                                               TREE_OPERAND (arg0, 0), tree01));
10569               }
10570             else if (code01 == MINUS_EXPR)
10571               {
10572                 tree tree010, tree011;
10573                 tree010 = TREE_OPERAND (tree01, 0);
10574                 tree011 = TREE_OPERAND (tree01, 1);
10575                 STRIP_NOPS (tree010);
10576                 STRIP_NOPS (tree011);
10577                 if (TREE_CODE (tree010) == INTEGER_CST
10578                     && 0 == compare_tree_int (tree010,
10579                                               TYPE_PRECISION
10580                                               (TREE_TYPE (TREE_OPERAND
10581                                                           (arg0, 0))))
10582                     && operand_equal_p (tree11, tree011, 0))
10583                     return fold_convert_loc
10584                       (loc, type,
10585                        build2 ((code0 != LSHIFT_EXPR
10586                                 ? LROTATE_EXPR
10587                                 : RROTATE_EXPR),
10588                                TREE_TYPE (TREE_OPERAND (arg0, 0)),
10589                                TREE_OPERAND (arg0, 0), tree11));
10590               }
10591           }
10592       }
10593
10594     associate:
10595       /* In most languages, can't associate operations on floats through
10596          parentheses.  Rather than remember where the parentheses were, we
10597          don't associate floats at all, unless the user has specified
10598          -fassociative-math.
10599          And, we need to make sure type is not saturating.  */
10600
10601       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10602           && !TYPE_SATURATING (type))
10603         {
10604           tree var0, con0, lit0, minus_lit0;
10605           tree var1, con1, lit1, minus_lit1;
10606           bool ok = true;
10607
10608           /* Split both trees into variables, constants, and literals.  Then
10609              associate each group together, the constants with literals,
10610              then the result with variables.  This increases the chances of
10611              literals being recombined later and of generating relocatable
10612              expressions for the sum of a constant and literal.  */
10613           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10614           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10615                              code == MINUS_EXPR);
10616
10617           /* With undefined overflow we can only associate constants
10618              with one variable.  */
10619           if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10620                || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10621               && var0 && var1)
10622             {
10623               tree tmp0 = var0;
10624               tree tmp1 = var1;
10625
10626               if (TREE_CODE (tmp0) == NEGATE_EXPR)
10627                 tmp0 = TREE_OPERAND (tmp0, 0);
10628               if (TREE_CODE (tmp1) == NEGATE_EXPR)
10629                 tmp1 = TREE_OPERAND (tmp1, 0);
10630               /* The only case we can still associate with two variables
10631                  is if they are the same, modulo negation.  */
10632               if (!operand_equal_p (tmp0, tmp1, 0))
10633                 ok = false;
10634             }
10635
10636           /* Only do something if we found more than two objects.  Otherwise,
10637              nothing has changed and we risk infinite recursion.  */
10638           if (ok
10639               && (2 < ((var0 != 0) + (var1 != 0)
10640                        + (con0 != 0) + (con1 != 0)
10641                        + (lit0 != 0) + (lit1 != 0)
10642                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10643             {
10644               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
10645               if (code == MINUS_EXPR)
10646                 code = PLUS_EXPR;
10647
10648               var0 = associate_trees (loc, var0, var1, code, type);
10649               con0 = associate_trees (loc, con0, con1, code, type);
10650               lit0 = associate_trees (loc, lit0, lit1, code, type);
10651               minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
10652
10653               /* Preserve the MINUS_EXPR if the negative part of the literal is
10654                  greater than the positive part.  Otherwise, the multiplicative
10655                  folding code (i.e extract_muldiv) may be fooled in case
10656                  unsigned constants are subtracted, like in the following
10657                  example: ((X*2 + 4) - 8U)/2.  */
10658               if (minus_lit0 && lit0)
10659                 {
10660                   if (TREE_CODE (lit0) == INTEGER_CST
10661                       && TREE_CODE (minus_lit0) == INTEGER_CST
10662                       && tree_int_cst_lt (lit0, minus_lit0))
10663                     {
10664                       minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10665                                                     MINUS_EXPR, type);
10666                       lit0 = 0;
10667                     }
10668                   else
10669                     {
10670                       lit0 = associate_trees (loc, lit0, minus_lit0,
10671                                               MINUS_EXPR, type);
10672                       minus_lit0 = 0;
10673                     }
10674                 }
10675               if (minus_lit0)
10676                 {
10677                   if (con0 == 0)
10678                     return
10679                       fold_convert_loc (loc, type,
10680                                         associate_trees (loc, var0, minus_lit0,
10681                                                          MINUS_EXPR, type));
10682                   else
10683                     {
10684                       con0 = associate_trees (loc, con0, minus_lit0,
10685                                               MINUS_EXPR, type);
10686                       return
10687                         fold_convert_loc (loc, type,
10688                                           associate_trees (loc, var0, con0,
10689                                                            PLUS_EXPR, type));
10690                     }
10691                 }
10692
10693               con0 = associate_trees (loc, con0, lit0, code, type);
10694               return
10695                 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10696                                                               code, type));
10697             }
10698         }
10699
10700       return NULL_TREE;
10701
10702     case MINUS_EXPR:
10703       /* Pointer simplifications for subtraction, simple reassociations. */
10704       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10705         {
10706           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10707           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10708               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10709             {
10710               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10711               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10712               tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10713               tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10714               return fold_build2_loc (loc, PLUS_EXPR, type,
10715                                   fold_build2_loc (loc, MINUS_EXPR, type,
10716                                                arg00, arg10),
10717                                   fold_build2_loc (loc, MINUS_EXPR, type,
10718                                                arg01, arg11));
10719             }
10720           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10721           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10722             {
10723               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10724               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10725               tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10726                                       fold_convert_loc (loc, type, arg1));
10727               if (tmp)
10728                 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10729             }
10730         }
10731       /* A - (-B) -> A + B */
10732       if (TREE_CODE (arg1) == NEGATE_EXPR)
10733         return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10734                             fold_convert_loc (loc, type,
10735                                               TREE_OPERAND (arg1, 0)));
10736       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10737       if (TREE_CODE (arg0) == NEGATE_EXPR
10738           && (FLOAT_TYPE_P (type)
10739               || INTEGRAL_TYPE_P (type))
10740           && negate_expr_p (arg1)
10741           && reorder_operands_p (arg0, arg1))
10742         return fold_build2_loc (loc, MINUS_EXPR, type,
10743                             fold_convert_loc (loc, type,
10744                                               negate_expr (arg1)),
10745                             fold_convert_loc (loc, type,
10746                                               TREE_OPERAND (arg0, 0)));
10747       /* Convert -A - 1 to ~A.  */
10748       if (INTEGRAL_TYPE_P (type)
10749           && TREE_CODE (arg0) == NEGATE_EXPR
10750           && integer_onep (arg1)
10751           && !TYPE_OVERFLOW_TRAPS (type))
10752         return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10753                             fold_convert_loc (loc, type,
10754                                               TREE_OPERAND (arg0, 0)));
10755
10756       /* Convert -1 - A to ~A.  */
10757       if (INTEGRAL_TYPE_P (type)
10758           && integer_all_onesp (arg0))
10759         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10760
10761
10762       /* X - (X / CST) * CST is X % CST.  */
10763       if (INTEGRAL_TYPE_P (type)
10764           && TREE_CODE (arg1) == MULT_EXPR
10765           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10766           && operand_equal_p (arg0,
10767                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10768           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10769                               TREE_OPERAND (arg1, 1), 0))
10770         return
10771           fold_convert_loc (loc, type,
10772                             fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10773                                          arg0, TREE_OPERAND (arg1, 1)));
10774
10775       if (! FLOAT_TYPE_P (type))
10776         {
10777           if (integer_zerop (arg0))
10778             return negate_expr (fold_convert_loc (loc, type, arg1));
10779           if (integer_zerop (arg1))
10780             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10781
10782           /* Fold A - (A & B) into ~B & A.  */
10783           if (!TREE_SIDE_EFFECTS (arg0)
10784               && TREE_CODE (arg1) == BIT_AND_EXPR)
10785             {
10786               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10787                 {
10788                   tree arg10 = fold_convert_loc (loc, type,
10789                                                  TREE_OPERAND (arg1, 0));
10790                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10791                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10792                                                    type, arg10),
10793                                       fold_convert_loc (loc, type, arg0));
10794                 }
10795               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10796                 {
10797                   tree arg11 = fold_convert_loc (loc,
10798                                                  type, TREE_OPERAND (arg1, 1));
10799                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10800                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10801                                                    type, arg11),
10802                                       fold_convert_loc (loc, type, arg0));
10803                 }
10804             }
10805
10806           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10807              any power of 2 minus 1.  */
10808           if (TREE_CODE (arg0) == BIT_AND_EXPR
10809               && TREE_CODE (arg1) == BIT_AND_EXPR
10810               && operand_equal_p (TREE_OPERAND (arg0, 0),
10811                                   TREE_OPERAND (arg1, 0), 0))
10812             {
10813               tree mask0 = TREE_OPERAND (arg0, 1);
10814               tree mask1 = TREE_OPERAND (arg1, 1);
10815               tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10816
10817               if (operand_equal_p (tem, mask1, 0))
10818                 {
10819                   tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10820                                      TREE_OPERAND (arg0, 0), mask1);
10821                   return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10822                 }
10823             }
10824         }
10825
10826       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10827       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10828         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10829
10830       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10831          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10832          (-ARG1 + ARG0) reduces to -ARG1.  */
10833       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10834         return negate_expr (fold_convert_loc (loc, type, arg1));
10835
10836       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10837          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10838          signed zeros are involved.  */
10839       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10840           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10841           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10842         {
10843           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10844           tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10845           tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10846           bool arg0rz = false, arg0iz = false;
10847           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10848               || (arg0i && (arg0iz = real_zerop (arg0i))))
10849             {
10850               tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10851               tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10852               if (arg0rz && arg1i && real_zerop (arg1i))
10853                 {
10854                   tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10855                                          arg1r ? arg1r
10856                                          : build1 (REALPART_EXPR, rtype, arg1));
10857                   tree ip = arg0i ? arg0i
10858                     : build1 (IMAGPART_EXPR, rtype, arg0);
10859                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10860                 }
10861               else if (arg0iz && arg1r && real_zerop (arg1r))
10862                 {
10863                   tree rp = arg0r ? arg0r
10864                     : build1 (REALPART_EXPR, rtype, arg0);
10865                   tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10866                                          arg1i ? arg1i
10867                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10868                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10869                 }
10870             }
10871         }
10872
10873       /* Fold &x - &x.  This can happen from &x.foo - &x.
10874          This is unsafe for certain floats even in non-IEEE formats.
10875          In IEEE, it is unsafe because it does wrong for NaNs.
10876          Also note that operand_equal_p is always false if an operand
10877          is volatile.  */
10878
10879       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10880           && operand_equal_p (arg0, arg1, 0))
10881         return fold_convert_loc (loc, type, integer_zero_node);
10882
10883       /* A - B -> A + (-B) if B is easily negatable.  */
10884       if (negate_expr_p (arg1)
10885           && ((FLOAT_TYPE_P (type)
10886                /* Avoid this transformation if B is a positive REAL_CST.  */
10887                && (TREE_CODE (arg1) != REAL_CST
10888                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10889               || INTEGRAL_TYPE_P (type)))
10890         return fold_build2_loc (loc, PLUS_EXPR, type,
10891                             fold_convert_loc (loc, type, arg0),
10892                             fold_convert_loc (loc, type,
10893                                               negate_expr (arg1)));
10894
10895       /* Try folding difference of addresses.  */
10896       {
10897         HOST_WIDE_INT diff;
10898
10899         if ((TREE_CODE (arg0) == ADDR_EXPR
10900              || TREE_CODE (arg1) == ADDR_EXPR)
10901             && ptr_difference_const (arg0, arg1, &diff))
10902           return build_int_cst_type (type, diff);
10903       }
10904
10905       /* Fold &a[i] - &a[j] to i-j.  */
10906       if (TREE_CODE (arg0) == ADDR_EXPR
10907           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10908           && TREE_CODE (arg1) == ADDR_EXPR
10909           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10910         {
10911           tree aref0 = TREE_OPERAND (arg0, 0);
10912           tree aref1 = TREE_OPERAND (arg1, 0);
10913           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10914                                TREE_OPERAND (aref1, 0), 0))
10915             {
10916               tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10917               tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10918               tree esz = array_ref_element_size (aref0);
10919               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10920               return fold_build2_loc (loc, MULT_EXPR, type, diff,
10921                                   fold_convert_loc (loc, type, esz));
10922
10923             }
10924         }
10925
10926       if (FLOAT_TYPE_P (type)
10927           && flag_unsafe_math_optimizations
10928           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10929           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10930           && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10931         return tem;
10932
10933       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10934          same or one.  Make sure type is not saturating.
10935          fold_plusminus_mult_expr will re-associate.  */
10936       if ((TREE_CODE (arg0) == MULT_EXPR
10937            || TREE_CODE (arg1) == MULT_EXPR)
10938           && !TYPE_SATURATING (type)
10939           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10940         {
10941           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10942           if (tem)
10943             return tem;
10944         }
10945
10946       goto associate;
10947
10948     case MULT_EXPR:
10949       /* (-A) * (-B) -> A * B  */
10950       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10951         return fold_build2_loc (loc, MULT_EXPR, type,
10952                             fold_convert_loc (loc, type,
10953                                               TREE_OPERAND (arg0, 0)),
10954                             fold_convert_loc (loc, type,
10955                                               negate_expr (arg1)));
10956       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10957         return fold_build2_loc (loc, MULT_EXPR, type,
10958                             fold_convert_loc (loc, type,
10959                                               negate_expr (arg0)),
10960                             fold_convert_loc (loc, type,
10961                                               TREE_OPERAND (arg1, 0)));
10962
10963       if (! FLOAT_TYPE_P (type))
10964         {
10965           if (integer_zerop (arg1))
10966             return omit_one_operand_loc (loc, type, arg1, arg0);
10967           if (integer_onep (arg1))
10968             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10969           /* Transform x * -1 into -x.  Make sure to do the negation
10970              on the original operand with conversions not stripped
10971              because we can only strip non-sign-changing conversions.  */
10972           if (integer_all_onesp (arg1))
10973             return fold_convert_loc (loc, type, negate_expr (op0));
10974           /* Transform x * -C into -x * C if x is easily negatable.  */
10975           if (TREE_CODE (arg1) == INTEGER_CST
10976               && tree_int_cst_sgn (arg1) == -1
10977               && negate_expr_p (arg0)
10978               && (tem = negate_expr (arg1)) != arg1
10979               && !TREE_OVERFLOW (tem))
10980             return fold_build2_loc (loc, MULT_EXPR, type,
10981                                 fold_convert_loc (loc, type,
10982                                                   negate_expr (arg0)),
10983                                 tem);
10984
10985           /* (a * (1 << b)) is (a << b)  */
10986           if (TREE_CODE (arg1) == LSHIFT_EXPR
10987               && integer_onep (TREE_OPERAND (arg1, 0)))
10988             return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10989                                 TREE_OPERAND (arg1, 1));
10990           if (TREE_CODE (arg0) == LSHIFT_EXPR
10991               && integer_onep (TREE_OPERAND (arg0, 0)))
10992             return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10993                                 TREE_OPERAND (arg0, 1));
10994
10995           /* (A + A) * C -> A * 2 * C  */
10996           if (TREE_CODE (arg0) == PLUS_EXPR
10997               && TREE_CODE (arg1) == INTEGER_CST
10998               && operand_equal_p (TREE_OPERAND (arg0, 0),
10999                                   TREE_OPERAND (arg0, 1), 0))
11000             return fold_build2_loc (loc, MULT_EXPR, type,
11001                                 omit_one_operand_loc (loc, type,
11002                                                   TREE_OPERAND (arg0, 0),
11003                                                   TREE_OPERAND (arg0, 1)),
11004                                 fold_build2_loc (loc, MULT_EXPR, type,
11005                                              build_int_cst (type, 2) , arg1));
11006
11007           strict_overflow_p = false;
11008           if (TREE_CODE (arg1) == INTEGER_CST
11009               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11010                                              &strict_overflow_p)))
11011             {
11012               if (strict_overflow_p)
11013                 fold_overflow_warning (("assuming signed overflow does not "
11014                                         "occur when simplifying "
11015                                         "multiplication"),
11016                                        WARN_STRICT_OVERFLOW_MISC);
11017               return fold_convert_loc (loc, type, tem);
11018             }
11019
11020           /* Optimize z * conj(z) for integer complex numbers.  */
11021           if (TREE_CODE (arg0) == CONJ_EXPR
11022               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11023             return fold_mult_zconjz (loc, type, arg1);
11024           if (TREE_CODE (arg1) == CONJ_EXPR
11025               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11026             return fold_mult_zconjz (loc, type, arg0);
11027         }
11028       else
11029         {
11030           /* Maybe fold x * 0 to 0.  The expressions aren't the same
11031              when x is NaN, since x * 0 is also NaN.  Nor are they the
11032              same in modes with signed zeros, since multiplying a
11033              negative value by 0 gives -0, not +0.  */
11034           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11035               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11036               && real_zerop (arg1))
11037             return omit_one_operand_loc (loc, type, arg1, arg0);
11038           /* In IEEE floating point, x*1 is not equivalent to x for snans.
11039              Likewise for complex arithmetic with signed zeros.  */
11040           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11041               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11042                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11043               && real_onep (arg1))
11044             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11045
11046           /* Transform x * -1.0 into -x.  */
11047           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11048               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11049                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11050               && real_minus_onep (arg1))
11051             return fold_convert_loc (loc, type, negate_expr (arg0));
11052
11053           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
11054              the result for floating point types due to rounding so it is applied
11055              only if -fassociative-math was specify.  */
11056           if (flag_associative_math
11057               && TREE_CODE (arg0) == RDIV_EXPR
11058               && TREE_CODE (arg1) == REAL_CST
11059               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
11060             {
11061               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
11062                                       arg1, 0);
11063               if (tem)
11064                 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11065                                     TREE_OPERAND (arg0, 1));
11066             }
11067
11068           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
11069           if (operand_equal_p (arg0, arg1, 0))
11070             {
11071               tree tem = fold_strip_sign_ops (arg0);
11072               if (tem != NULL_TREE)
11073                 {
11074                   tem = fold_convert_loc (loc, type, tem);
11075                   return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
11076                 }
11077             }
11078
11079           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11080              This is not the same for NaNs or if signed zeros are
11081              involved.  */
11082           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11083               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11084               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11085               && TREE_CODE (arg1) == COMPLEX_CST
11086               && real_zerop (TREE_REALPART (arg1)))
11087             {
11088               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11089               if (real_onep (TREE_IMAGPART (arg1)))
11090                 return
11091                   fold_build2_loc (loc, COMPLEX_EXPR, type,
11092                                negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
11093                                                              rtype, arg0)),
11094                                fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
11095               else if (real_minus_onep (TREE_IMAGPART (arg1)))
11096                 return
11097                   fold_build2_loc (loc, COMPLEX_EXPR, type,
11098                                fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
11099                                negate_expr (fold_build1_loc (loc, REALPART_EXPR,
11100                                                              rtype, arg0)));
11101             }
11102
11103           /* Optimize z * conj(z) for floating point complex numbers.
11104              Guarded by flag_unsafe_math_optimizations as non-finite
11105              imaginary components don't produce scalar results.  */
11106           if (flag_unsafe_math_optimizations
11107               && TREE_CODE (arg0) == CONJ_EXPR
11108               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11109             return fold_mult_zconjz (loc, type, arg1);
11110           if (flag_unsafe_math_optimizations
11111               && TREE_CODE (arg1) == CONJ_EXPR
11112               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11113             return fold_mult_zconjz (loc, type, arg0);
11114
11115           if (flag_unsafe_math_optimizations)
11116             {
11117               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11118               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11119
11120               /* Optimizations of root(...)*root(...).  */
11121               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
11122                 {
11123                   tree rootfn, arg;
11124                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
11125                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
11126
11127                   /* Optimize sqrt(x)*sqrt(x) as x.  */
11128                   if (BUILTIN_SQRT_P (fcode0)
11129                       && operand_equal_p (arg00, arg10, 0)
11130                       && ! HONOR_SNANS (TYPE_MODE (type)))
11131                     return arg00;
11132
11133                   /* Optimize root(x)*root(y) as root(x*y).  */
11134                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11135                   arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
11136                   return build_call_expr_loc (loc, rootfn, 1, arg);
11137                 }
11138
11139               /* Optimize expN(x)*expN(y) as expN(x+y).  */
11140               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
11141                 {
11142                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11143                   tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
11144                                           CALL_EXPR_ARG (arg0, 0),
11145                                           CALL_EXPR_ARG (arg1, 0));
11146                   return build_call_expr_loc (loc, expfn, 1, arg);
11147                 }
11148
11149               /* Optimizations of pow(...)*pow(...).  */
11150               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
11151                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
11152                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
11153                 {
11154                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
11155                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
11156                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
11157                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
11158
11159                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
11160                   if (operand_equal_p (arg01, arg11, 0))
11161                     {
11162                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11163                       tree arg = fold_build2_loc (loc, MULT_EXPR, type,
11164                                               arg00, arg10);
11165                       return build_call_expr_loc (loc, powfn, 2, arg, arg01);
11166                     }
11167
11168                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
11169                   if (operand_equal_p (arg00, arg10, 0))
11170                     {
11171                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11172                       tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
11173                                               arg01, arg11);
11174                       return build_call_expr_loc (loc, powfn, 2, arg00, arg);
11175                     }
11176                 }
11177
11178               /* Optimize tan(x)*cos(x) as sin(x).  */
11179               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
11180                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
11181                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
11182                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
11183                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
11184                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
11185                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11186                                       CALL_EXPR_ARG (arg1, 0), 0))
11187                 {
11188                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
11189
11190                   if (sinfn != NULL_TREE)
11191                     return build_call_expr_loc (loc, sinfn, 1,
11192                                             CALL_EXPR_ARG (arg0, 0));
11193                 }
11194
11195               /* Optimize x*pow(x,c) as pow(x,c+1).  */
11196               if (fcode1 == BUILT_IN_POW
11197                   || fcode1 == BUILT_IN_POWF
11198                   || fcode1 == BUILT_IN_POWL)
11199                 {
11200                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
11201                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
11202                   if (TREE_CODE (arg11) == REAL_CST
11203                       && !TREE_OVERFLOW (arg11)
11204                       && operand_equal_p (arg0, arg10, 0))
11205                     {
11206                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11207                       REAL_VALUE_TYPE c;
11208                       tree arg;
11209
11210                       c = TREE_REAL_CST (arg11);
11211                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
11212                       arg = build_real (type, c);
11213                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
11214                     }
11215                 }
11216
11217               /* Optimize pow(x,c)*x as pow(x,c+1).  */
11218               if (fcode0 == BUILT_IN_POW
11219                   || fcode0 == BUILT_IN_POWF
11220                   || fcode0 == BUILT_IN_POWL)
11221                 {
11222                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
11223                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
11224                   if (TREE_CODE (arg01) == REAL_CST
11225                       && !TREE_OVERFLOW (arg01)
11226                       && operand_equal_p (arg1, arg00, 0))
11227                     {
11228                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11229                       REAL_VALUE_TYPE c;
11230                       tree arg;
11231
11232                       c = TREE_REAL_CST (arg01);
11233                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
11234                       arg = build_real (type, c);
11235                       return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11236                     }
11237                 }
11238
11239               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
11240               if (optimize_function_for_speed_p (cfun)
11241                   && operand_equal_p (arg0, arg1, 0))
11242                 {
11243                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
11244
11245                   if (powfn)
11246                     {
11247                       tree arg = build_real (type, dconst2);
11248                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
11249                     }
11250                 }
11251             }
11252         }
11253       goto associate;
11254
11255     case BIT_IOR_EXPR:
11256     bit_ior:
11257       if (integer_all_onesp (arg1))
11258         return omit_one_operand_loc (loc, type, arg1, arg0);
11259       if (integer_zerop (arg1))
11260         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11261       if (operand_equal_p (arg0, arg1, 0))
11262         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11263
11264       /* ~X | X is -1.  */
11265       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11266           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11267         {
11268           t1 = fold_convert_loc (loc, type, integer_zero_node);
11269           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11270           return omit_one_operand_loc (loc, type, t1, arg1);
11271         }
11272
11273       /* X | ~X is -1.  */
11274       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11275           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11276         {
11277           t1 = fold_convert_loc (loc, type, integer_zero_node);
11278           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11279           return omit_one_operand_loc (loc, type, t1, arg0);
11280         }
11281
11282       /* Canonicalize (X & C1) | C2.  */
11283       if (TREE_CODE (arg0) == BIT_AND_EXPR
11284           && TREE_CODE (arg1) == INTEGER_CST
11285           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11286         {
11287           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
11288           int width = TYPE_PRECISION (type), w;
11289           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
11290           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11291           hi2 = TREE_INT_CST_HIGH (arg1);
11292           lo2 = TREE_INT_CST_LOW (arg1);
11293
11294           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
11295           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
11296             return omit_one_operand_loc (loc, type, arg1,
11297                                      TREE_OPERAND (arg0, 0));
11298
11299           if (width > HOST_BITS_PER_WIDE_INT)
11300             {
11301               mhi = (unsigned HOST_WIDE_INT) -1
11302                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
11303               mlo = -1;
11304             }
11305           else
11306             {
11307               mhi = 0;
11308               mlo = (unsigned HOST_WIDE_INT) -1
11309                     >> (HOST_BITS_PER_WIDE_INT - width);
11310             }
11311
11312           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
11313           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
11314             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11315                                 TREE_OPERAND (arg0, 0), arg1);
11316
11317           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11318              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11319              mode which allows further optimizations.  */
11320           hi1 &= mhi;
11321           lo1 &= mlo;
11322           hi2 &= mhi;
11323           lo2 &= mlo;
11324           hi3 = hi1 & ~hi2;
11325           lo3 = lo1 & ~lo2;
11326           for (w = BITS_PER_UNIT;
11327                w <= width && w <= HOST_BITS_PER_WIDE_INT;
11328                w <<= 1)
11329             {
11330               unsigned HOST_WIDE_INT mask
11331                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
11332               if (((lo1 | lo2) & mask) == mask
11333                   && (lo1 & ~mask) == 0 && hi1 == 0)
11334                 {
11335                   hi3 = 0;
11336                   lo3 = mask;
11337                   break;
11338                 }
11339             }
11340           if (hi3 != hi1 || lo3 != lo1)
11341             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11342                                 fold_build2_loc (loc, BIT_AND_EXPR, type,
11343                                              TREE_OPERAND (arg0, 0),
11344                                              build_int_cst_wide (type,
11345                                                                  lo3, hi3)),
11346                                 arg1);
11347         }
11348
11349       /* (X & Y) | Y is (X, Y).  */
11350       if (TREE_CODE (arg0) == BIT_AND_EXPR
11351           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11352         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11353       /* (X & Y) | X is (Y, X).  */
11354       if (TREE_CODE (arg0) == BIT_AND_EXPR
11355           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11356           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11357         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11358       /* X | (X & Y) is (Y, X).  */
11359       if (TREE_CODE (arg1) == BIT_AND_EXPR
11360           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11361           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11362         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11363       /* X | (Y & X) is (Y, X).  */
11364       if (TREE_CODE (arg1) == BIT_AND_EXPR
11365           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11366           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11367         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11368
11369       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11370       if (t1 != NULL_TREE)
11371         return t1;
11372
11373       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
11374
11375          This results in more efficient code for machines without a NAND
11376          instruction.  Combine will canonicalize to the first form
11377          which will allow use of NAND instructions provided by the
11378          backend if they exist.  */
11379       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11380           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11381         {
11382           return
11383             fold_build1_loc (loc, BIT_NOT_EXPR, type,
11384                          build2 (BIT_AND_EXPR, type,
11385                                  fold_convert_loc (loc, type,
11386                                                    TREE_OPERAND (arg0, 0)),
11387                                  fold_convert_loc (loc, type,
11388                                                    TREE_OPERAND (arg1, 0))));
11389         }
11390
11391       /* See if this can be simplified into a rotate first.  If that
11392          is unsuccessful continue in the association code.  */
11393       goto bit_rotate;
11394
11395     case BIT_XOR_EXPR:
11396       if (integer_zerop (arg1))
11397         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11398       if (integer_all_onesp (arg1))
11399         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
11400       if (operand_equal_p (arg0, arg1, 0))
11401         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11402
11403       /* ~X ^ X is -1.  */
11404       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11405           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11406         {
11407           t1 = fold_convert_loc (loc, type, integer_zero_node);
11408           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11409           return omit_one_operand_loc (loc, type, t1, arg1);
11410         }
11411
11412       /* X ^ ~X is -1.  */
11413       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11414           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11415         {
11416           t1 = fold_convert_loc (loc, type, integer_zero_node);
11417           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11418           return omit_one_operand_loc (loc, type, t1, arg0);
11419         }
11420
11421       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11422          with a constant, and the two constants have no bits in common,
11423          we should treat this as a BIT_IOR_EXPR since this may produce more
11424          simplifications.  */
11425       if (TREE_CODE (arg0) == BIT_AND_EXPR
11426           && TREE_CODE (arg1) == BIT_AND_EXPR
11427           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11428           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11429           && integer_zerop (const_binop (BIT_AND_EXPR,
11430                                          TREE_OPERAND (arg0, 1),
11431                                          TREE_OPERAND (arg1, 1), 0)))
11432         {
11433           code = BIT_IOR_EXPR;
11434           goto bit_ior;
11435         }
11436
11437       /* (X | Y) ^ X -> Y & ~ X*/
11438       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11439           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11440         {
11441           tree t2 = TREE_OPERAND (arg0, 1);
11442           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11443                             arg1);
11444           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11445                             fold_convert_loc (loc, type, t2),
11446                             fold_convert_loc (loc, type, t1));
11447           return t1;
11448         }
11449
11450       /* (Y | X) ^ X -> Y & ~ X*/
11451       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11452           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11453         {
11454           tree t2 = TREE_OPERAND (arg0, 0);
11455           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11456                             arg1);
11457           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11458                             fold_convert_loc (loc, type, t2),
11459                             fold_convert_loc (loc, type, t1));
11460           return t1;
11461         }
11462
11463       /* X ^ (X | Y) -> Y & ~ X*/
11464       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11465           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11466         {
11467           tree t2 = TREE_OPERAND (arg1, 1);
11468           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11469                             arg0);
11470           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11471                             fold_convert_loc (loc, type, t2),
11472                             fold_convert_loc (loc, type, t1));
11473           return t1;
11474         }
11475
11476       /* X ^ (Y | X) -> Y & ~ X*/
11477       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11478           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11479         {
11480           tree t2 = TREE_OPERAND (arg1, 0);
11481           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11482                             arg0);
11483           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11484                             fold_convert_loc (loc, type, t2),
11485                             fold_convert_loc (loc, type, t1));
11486           return t1;
11487         }
11488
11489       /* Convert ~X ^ ~Y to X ^ Y.  */
11490       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11491           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11492         return fold_build2_loc (loc, code, type,
11493                             fold_convert_loc (loc, type,
11494                                               TREE_OPERAND (arg0, 0)),
11495                             fold_convert_loc (loc, type,
11496                                               TREE_OPERAND (arg1, 0)));
11497
11498       /* Convert ~X ^ C to X ^ ~C.  */
11499       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11500           && TREE_CODE (arg1) == INTEGER_CST)
11501         return fold_build2_loc (loc, code, type,
11502                             fold_convert_loc (loc, type,
11503                                               TREE_OPERAND (arg0, 0)),
11504                             fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
11505
11506       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
11507       if (TREE_CODE (arg0) == BIT_AND_EXPR
11508           && integer_onep (TREE_OPERAND (arg0, 1))
11509           && integer_onep (arg1))
11510         return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11511                             build_int_cst (TREE_TYPE (arg0), 0));
11512
11513       /* Fold (X & Y) ^ Y as ~X & Y.  */
11514       if (TREE_CODE (arg0) == BIT_AND_EXPR
11515           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11516         {
11517           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11518           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11519                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11520                               fold_convert_loc (loc, type, arg1));
11521         }
11522       /* Fold (X & Y) ^ X as ~Y & X.  */
11523       if (TREE_CODE (arg0) == BIT_AND_EXPR
11524           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11525           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11526         {
11527           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11528           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11529                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11530                               fold_convert_loc (loc, type, arg1));
11531         }
11532       /* Fold X ^ (X & Y) as X & ~Y.  */
11533       if (TREE_CODE (arg1) == BIT_AND_EXPR
11534           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11535         {
11536           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11537           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11538                               fold_convert_loc (loc, type, arg0),
11539                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11540         }
11541       /* Fold X ^ (Y & X) as ~Y & X.  */
11542       if (TREE_CODE (arg1) == BIT_AND_EXPR
11543           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11544           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11545         {
11546           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11547           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11548                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11549                               fold_convert_loc (loc, type, arg0));
11550         }
11551
11552       /* See if this can be simplified into a rotate first.  If that
11553          is unsuccessful continue in the association code.  */
11554       goto bit_rotate;
11555
11556     case BIT_AND_EXPR:
11557       if (integer_all_onesp (arg1))
11558         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11559       if (integer_zerop (arg1))
11560         return omit_one_operand_loc (loc, type, arg1, arg0);
11561       if (operand_equal_p (arg0, arg1, 0))
11562         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11563
11564       /* ~X & X is always zero.  */
11565       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11566           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11567         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11568
11569       /* X & ~X is always zero.  */
11570       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11571           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11572         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11573
11574       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
11575       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11576           && TREE_CODE (arg1) == INTEGER_CST
11577           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11578         {
11579           tree tmp1 = fold_convert_loc (loc, type, arg1);
11580           tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11581           tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11582           tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
11583           tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
11584           return
11585             fold_convert_loc (loc, type,
11586                               fold_build2_loc (loc, BIT_IOR_EXPR,
11587                                            type, tmp2, tmp3));
11588         }
11589
11590       /* (X | Y) & Y is (X, Y).  */
11591       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11592           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11593         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11594       /* (X | Y) & X is (Y, X).  */
11595       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11596           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11597           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11598         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11599       /* X & (X | Y) is (Y, X).  */
11600       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11601           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11602           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11603         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11604       /* X & (Y | X) is (Y, X).  */
11605       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11606           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11607           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11608         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11609
11610       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11611       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11612           && integer_onep (TREE_OPERAND (arg0, 1))
11613           && integer_onep (arg1))
11614         {
11615           tem = TREE_OPERAND (arg0, 0);
11616           return fold_build2_loc (loc, EQ_EXPR, type,
11617                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11618                                            build_int_cst (TREE_TYPE (tem), 1)),
11619                               build_int_cst (TREE_TYPE (tem), 0));
11620         }
11621       /* Fold ~X & 1 as (X & 1) == 0.  */
11622       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11623           && integer_onep (arg1))
11624         {
11625           tem = TREE_OPERAND (arg0, 0);
11626           return fold_build2_loc (loc, EQ_EXPR, type,
11627                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11628                                            build_int_cst (TREE_TYPE (tem), 1)),
11629                               build_int_cst (TREE_TYPE (tem), 0));
11630         }
11631
11632       /* Fold (X ^ Y) & Y as ~X & Y.  */
11633       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11634           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11635         {
11636           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11637           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11638                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11639                               fold_convert_loc (loc, type, arg1));
11640         }
11641       /* Fold (X ^ Y) & X as ~Y & X.  */
11642       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11643           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11644           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11645         {
11646           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11647           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11648                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11649                               fold_convert_loc (loc, type, arg1));
11650         }
11651       /* Fold X & (X ^ Y) as X & ~Y.  */
11652       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11653           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11654         {
11655           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11656           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11657                               fold_convert_loc (loc, type, arg0),
11658                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11659         }
11660       /* Fold X & (Y ^ X) as ~Y & X.  */
11661       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11662           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11663           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11664         {
11665           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11666           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11667                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11668                               fold_convert_loc (loc, type, arg0));
11669         }
11670
11671       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11672       if (t1 != NULL_TREE)
11673         return t1;
11674       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11675       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11676           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11677         {
11678           unsigned int prec
11679             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11680
11681           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11682               && (~TREE_INT_CST_LOW (arg1)
11683                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11684             return
11685               fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11686         }
11687
11688       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11689
11690          This results in more efficient code for machines without a NOR
11691          instruction.  Combine will canonicalize to the first form
11692          which will allow use of NOR instructions provided by the
11693          backend if they exist.  */
11694       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11695           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11696         {
11697           return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11698                               build2 (BIT_IOR_EXPR, type,
11699                                       fold_convert_loc (loc, type,
11700                                                         TREE_OPERAND (arg0, 0)),
11701                                       fold_convert_loc (loc, type,
11702                                                         TREE_OPERAND (arg1, 0))));
11703         }
11704
11705       /* If arg0 is derived from the address of an object or function, we may
11706          be able to fold this expression using the object or function's
11707          alignment.  */
11708       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11709         {
11710           unsigned HOST_WIDE_INT modulus, residue;
11711           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11712
11713           modulus = get_pointer_modulus_and_residue (arg0, &residue,
11714                                                      integer_onep (arg1));
11715
11716           /* This works because modulus is a power of 2.  If this weren't the
11717              case, we'd have to replace it by its greatest power-of-2
11718              divisor: modulus & -modulus.  */
11719           if (low < modulus)
11720             return build_int_cst (type, residue & low);
11721         }
11722
11723       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11724               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11725          if the new mask might be further optimized.  */
11726       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11727            || TREE_CODE (arg0) == RSHIFT_EXPR)
11728           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11729           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11730           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11731              < TYPE_PRECISION (TREE_TYPE (arg0))
11732           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11733           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11734         {
11735           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11736           unsigned HOST_WIDE_INT mask
11737             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11738           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11739           tree shift_type = TREE_TYPE (arg0);
11740
11741           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11742             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11743           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11744                    && TYPE_PRECISION (TREE_TYPE (arg0))
11745                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11746             {
11747               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11748               tree arg00 = TREE_OPERAND (arg0, 0);
11749               /* See if more bits can be proven as zero because of
11750                  zero extension.  */
11751               if (TREE_CODE (arg00) == NOP_EXPR
11752                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11753                 {
11754                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11755                   if (TYPE_PRECISION (inner_type)
11756                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11757                       && TYPE_PRECISION (inner_type) < prec)
11758                     {
11759                       prec = TYPE_PRECISION (inner_type);
11760                       /* See if we can shorten the right shift.  */
11761                       if (shiftc < prec)
11762                         shift_type = inner_type;
11763                     }
11764                 }
11765               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11766               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11767               zerobits <<= prec - shiftc;
11768               /* For arithmetic shift if sign bit could be set, zerobits
11769                  can contain actually sign bits, so no transformation is
11770                  possible, unless MASK masks them all away.  In that
11771                  case the shift needs to be converted into logical shift.  */
11772               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11773                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11774                 {
11775                   if ((mask & zerobits) == 0)
11776                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11777                   else
11778                     zerobits = 0;
11779                 }
11780             }
11781
11782           /* ((X << 16) & 0xff00) is (X, 0).  */
11783           if ((mask & zerobits) == mask)
11784             return omit_one_operand_loc (loc, type,
11785                                      build_int_cst (type, 0), arg0);
11786
11787           newmask = mask | zerobits;
11788           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11789             {
11790               unsigned int prec;
11791
11792               /* Only do the transformation if NEWMASK is some integer
11793                  mode's mask.  */
11794               for (prec = BITS_PER_UNIT;
11795                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11796                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11797                   break;
11798               if (prec < HOST_BITS_PER_WIDE_INT
11799                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11800                 {
11801                   tree newmaskt;
11802
11803                   if (shift_type != TREE_TYPE (arg0))
11804                     {
11805                       tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11806                                          fold_convert_loc (loc, shift_type,
11807                                                            TREE_OPERAND (arg0, 0)),
11808                                          TREE_OPERAND (arg0, 1));
11809                       tem = fold_convert_loc (loc, type, tem);
11810                     }
11811                   else
11812                     tem = op0;
11813                   newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11814                   if (!tree_int_cst_equal (newmaskt, arg1))
11815                     return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11816                 }
11817             }
11818         }
11819
11820       goto associate;
11821
11822     case RDIV_EXPR:
11823       /* Don't touch a floating-point divide by zero unless the mode
11824          of the constant can represent infinity.  */
11825       if (TREE_CODE (arg1) == REAL_CST
11826           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11827           && real_zerop (arg1))
11828         return NULL_TREE;
11829
11830       /* Optimize A / A to 1.0 if we don't care about
11831          NaNs or Infinities.  Skip the transformation
11832          for non-real operands.  */
11833       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11834           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11835           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11836           && operand_equal_p (arg0, arg1, 0))
11837         {
11838           tree r = build_real (TREE_TYPE (arg0), dconst1);
11839
11840           return omit_two_operands_loc (loc, type, r, arg0, arg1);
11841         }
11842
11843       /* The complex version of the above A / A optimization.  */
11844       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11845           && operand_equal_p (arg0, arg1, 0))
11846         {
11847           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11848           if (! HONOR_NANS (TYPE_MODE (elem_type))
11849               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11850             {
11851               tree r = build_real (elem_type, dconst1);
11852               /* omit_two_operands will call fold_convert for us.  */
11853               return omit_two_operands_loc (loc, type, r, arg0, arg1);
11854             }
11855         }
11856
11857       /* (-A) / (-B) -> A / B  */
11858       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11859         return fold_build2_loc (loc, RDIV_EXPR, type,
11860                             TREE_OPERAND (arg0, 0),
11861                             negate_expr (arg1));
11862       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11863         return fold_build2_loc (loc, RDIV_EXPR, type,
11864                             negate_expr (arg0),
11865                             TREE_OPERAND (arg1, 0));
11866
11867       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11868       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11869           && real_onep (arg1))
11870         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11871
11872       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11873       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11874           && real_minus_onep (arg1))
11875         return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11876                                                   negate_expr (arg0)));
11877
11878       /* If ARG1 is a constant, we can convert this to a multiply by the
11879          reciprocal.  This does not have the same rounding properties,
11880          so only do this if -freciprocal-math.  We can actually
11881          always safely do it if ARG1 is a power of two, but it's hard to
11882          tell if it is or not in a portable manner.  */
11883       if (TREE_CODE (arg1) == REAL_CST)
11884         {
11885           if (flag_reciprocal_math
11886               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11887                                           arg1, 0)))
11888             return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11889           /* Find the reciprocal if optimizing and the result is exact.  */
11890           if (optimize)
11891             {
11892               REAL_VALUE_TYPE r;
11893               r = TREE_REAL_CST (arg1);
11894               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11895                 {
11896                   tem = build_real (type, r);
11897                   return fold_build2_loc (loc, MULT_EXPR, type,
11898                                       fold_convert_loc (loc, type, arg0), tem);
11899                 }
11900             }
11901         }
11902       /* Convert A/B/C to A/(B*C).  */
11903       if (flag_reciprocal_math
11904           && TREE_CODE (arg0) == RDIV_EXPR)
11905         return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11906                             fold_build2_loc (loc, MULT_EXPR, type,
11907                                          TREE_OPERAND (arg0, 1), arg1));
11908
11909       /* Convert A/(B/C) to (A/B)*C.  */
11910       if (flag_reciprocal_math
11911           && TREE_CODE (arg1) == RDIV_EXPR)
11912         return fold_build2_loc (loc, MULT_EXPR, type,
11913                             fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11914                                          TREE_OPERAND (arg1, 0)),
11915                             TREE_OPERAND (arg1, 1));
11916
11917       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11918       if (flag_reciprocal_math
11919           && TREE_CODE (arg1) == MULT_EXPR
11920           && TREE_CODE (arg0) == REAL_CST
11921           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11922         {
11923           tree tem = const_binop (RDIV_EXPR, arg0,
11924                                   TREE_OPERAND (arg1, 1), 0);
11925           if (tem)
11926             return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11927                                 TREE_OPERAND (arg1, 0));
11928         }
11929
11930       if (flag_unsafe_math_optimizations)
11931         {
11932           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11933           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11934
11935           /* Optimize sin(x)/cos(x) as tan(x).  */
11936           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11937                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11938                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11939               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11940                                   CALL_EXPR_ARG (arg1, 0), 0))
11941             {
11942               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11943
11944               if (tanfn != NULL_TREE)
11945                 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11946             }
11947
11948           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11949           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11950                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11951                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11952               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11953                                   CALL_EXPR_ARG (arg1, 0), 0))
11954             {
11955               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11956
11957               if (tanfn != NULL_TREE)
11958                 {
11959                   tree tmp = build_call_expr_loc (loc, tanfn, 1,
11960                                               CALL_EXPR_ARG (arg0, 0));
11961                   return fold_build2_loc (loc, RDIV_EXPR, type,
11962                                       build_real (type, dconst1), tmp);
11963                 }
11964             }
11965
11966           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11967              NaNs or Infinities.  */
11968           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11969                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11970                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11971             {
11972               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11973               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11974
11975               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11976                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11977                   && operand_equal_p (arg00, arg01, 0))
11978                 {
11979                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11980
11981                   if (cosfn != NULL_TREE)
11982                     return build_call_expr_loc (loc, cosfn, 1, arg00);
11983                 }
11984             }
11985
11986           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11987              NaNs or Infinities.  */
11988           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11989                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11990                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11991             {
11992               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11993               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11994
11995               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11996                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11997                   && operand_equal_p (arg00, arg01, 0))
11998                 {
11999                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
12000
12001                   if (cosfn != NULL_TREE)
12002                     {
12003                       tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
12004                       return fold_build2_loc (loc, RDIV_EXPR, type,
12005                                           build_real (type, dconst1),
12006                                           tmp);
12007                     }
12008                 }
12009             }
12010
12011           /* Optimize pow(x,c)/x as pow(x,c-1).  */
12012           if (fcode0 == BUILT_IN_POW
12013               || fcode0 == BUILT_IN_POWF
12014               || fcode0 == BUILT_IN_POWL)
12015             {
12016               tree arg00 = CALL_EXPR_ARG (arg0, 0);
12017               tree arg01 = CALL_EXPR_ARG (arg0, 1);
12018               if (TREE_CODE (arg01) == REAL_CST
12019                   && !TREE_OVERFLOW (arg01)
12020                   && operand_equal_p (arg1, arg00, 0))
12021                 {
12022                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
12023                   REAL_VALUE_TYPE c;
12024                   tree arg;
12025
12026                   c = TREE_REAL_CST (arg01);
12027                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
12028                   arg = build_real (type, c);
12029                   return build_call_expr_loc (loc, powfn, 2, arg1, arg);
12030                 }
12031             }
12032
12033           /* Optimize a/root(b/c) into a*root(c/b).  */
12034           if (BUILTIN_ROOT_P (fcode1))
12035             {
12036               tree rootarg = CALL_EXPR_ARG (arg1, 0);
12037
12038               if (TREE_CODE (rootarg) == RDIV_EXPR)
12039                 {
12040                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12041                   tree b = TREE_OPERAND (rootarg, 0);
12042                   tree c = TREE_OPERAND (rootarg, 1);
12043
12044                   tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
12045
12046                   tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
12047                   return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
12048                 }
12049             }
12050
12051           /* Optimize x/expN(y) into x*expN(-y).  */
12052           if (BUILTIN_EXPONENT_P (fcode1))
12053             {
12054               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12055               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
12056               arg1 = build_call_expr_loc (loc,
12057                                       expfn, 1,
12058                                       fold_convert_loc (loc, type, arg));
12059               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
12060             }
12061
12062           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
12063           if (fcode1 == BUILT_IN_POW
12064               || fcode1 == BUILT_IN_POWF
12065               || fcode1 == BUILT_IN_POWL)
12066             {
12067               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12068               tree arg10 = CALL_EXPR_ARG (arg1, 0);
12069               tree arg11 = CALL_EXPR_ARG (arg1, 1);
12070               tree neg11 = fold_convert_loc (loc, type,
12071                                              negate_expr (arg11));
12072               arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
12073               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
12074             }
12075         }
12076       return NULL_TREE;
12077
12078     case TRUNC_DIV_EXPR:
12079     case FLOOR_DIV_EXPR:
12080       /* Simplify A / (B << N) where A and B are positive and B is
12081          a power of 2, to A >> (N + log2(B)).  */
12082       strict_overflow_p = false;
12083       if (TREE_CODE (arg1) == LSHIFT_EXPR
12084           && (TYPE_UNSIGNED (type)
12085               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12086         {
12087           tree sval = TREE_OPERAND (arg1, 0);
12088           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
12089             {
12090               tree sh_cnt = TREE_OPERAND (arg1, 1);
12091               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
12092
12093               if (strict_overflow_p)
12094                 fold_overflow_warning (("assuming signed overflow does not "
12095                                         "occur when simplifying A / (B << N)"),
12096                                        WARN_STRICT_OVERFLOW_MISC);
12097
12098               sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
12099                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
12100               return fold_build2_loc (loc, RSHIFT_EXPR, type,
12101                                   fold_convert_loc (loc, type, arg0), sh_cnt);
12102             }
12103         }
12104
12105       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
12106          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
12107       if (INTEGRAL_TYPE_P (type)
12108           && TYPE_UNSIGNED (type)
12109           && code == FLOOR_DIV_EXPR)
12110         return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
12111
12112       /* Fall thru */
12113
12114     case ROUND_DIV_EXPR:
12115     case CEIL_DIV_EXPR:
12116     case EXACT_DIV_EXPR:
12117       if (integer_onep (arg1))
12118         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12119       if (integer_zerop (arg1))
12120         return NULL_TREE;
12121       /* X / -1 is -X.  */
12122       if (!TYPE_UNSIGNED (type)
12123           && TREE_CODE (arg1) == INTEGER_CST
12124           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
12125           && TREE_INT_CST_HIGH (arg1) == -1)
12126         return fold_convert_loc (loc, type, negate_expr (arg0));
12127
12128       /* Convert -A / -B to A / B when the type is signed and overflow is
12129          undefined.  */
12130       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12131           && TREE_CODE (arg0) == NEGATE_EXPR
12132           && negate_expr_p (arg1))
12133         {
12134           if (INTEGRAL_TYPE_P (type))
12135             fold_overflow_warning (("assuming signed overflow does not occur "
12136                                     "when distributing negation across "
12137                                     "division"),
12138                                    WARN_STRICT_OVERFLOW_MISC);
12139           return fold_build2_loc (loc, code, type,
12140                               fold_convert_loc (loc, type,
12141                                                 TREE_OPERAND (arg0, 0)),
12142                               fold_convert_loc (loc, type,
12143                                                 negate_expr (arg1)));
12144         }
12145       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12146           && TREE_CODE (arg1) == NEGATE_EXPR
12147           && negate_expr_p (arg0))
12148         {
12149           if (INTEGRAL_TYPE_P (type))
12150             fold_overflow_warning (("assuming signed overflow does not occur "
12151                                     "when distributing negation across "
12152                                     "division"),
12153                                    WARN_STRICT_OVERFLOW_MISC);
12154           return fold_build2_loc (loc, code, type,
12155                               fold_convert_loc (loc, type,
12156                                                 negate_expr (arg0)),
12157                               fold_convert_loc (loc, type,
12158                                                 TREE_OPERAND (arg1, 0)));
12159         }
12160
12161       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12162          operation, EXACT_DIV_EXPR.
12163
12164          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12165          At one time others generated faster code, it's not clear if they do
12166          after the last round to changes to the DIV code in expmed.c.  */
12167       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12168           && multiple_of_p (type, arg0, arg1))
12169         return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
12170
12171       strict_overflow_p = false;
12172       if (TREE_CODE (arg1) == INTEGER_CST
12173           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12174                                          &strict_overflow_p)))
12175         {
12176           if (strict_overflow_p)
12177             fold_overflow_warning (("assuming signed overflow does not occur "
12178                                     "when simplifying division"),
12179                                    WARN_STRICT_OVERFLOW_MISC);
12180           return fold_convert_loc (loc, type, tem);
12181         }
12182
12183       return NULL_TREE;
12184
12185     case CEIL_MOD_EXPR:
12186     case FLOOR_MOD_EXPR:
12187     case ROUND_MOD_EXPR:
12188     case TRUNC_MOD_EXPR:
12189       /* X % 1 is always zero, but be sure to preserve any side
12190          effects in X.  */
12191       if (integer_onep (arg1))
12192         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12193
12194       /* X % 0, return X % 0 unchanged so that we can get the
12195          proper warnings and errors.  */
12196       if (integer_zerop (arg1))
12197         return NULL_TREE;
12198
12199       /* 0 % X is always zero, but be sure to preserve any side
12200          effects in X.  Place this after checking for X == 0.  */
12201       if (integer_zerop (arg0))
12202         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12203
12204       /* X % -1 is zero.  */
12205       if (!TYPE_UNSIGNED (type)
12206           && TREE_CODE (arg1) == INTEGER_CST
12207           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
12208           && TREE_INT_CST_HIGH (arg1) == -1)
12209         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12210
12211       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
12212          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
12213       strict_overflow_p = false;
12214       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
12215           && (TYPE_UNSIGNED (type)
12216               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12217         {
12218           tree c = arg1;
12219           /* Also optimize A % (C << N)  where C is a power of 2,
12220              to A & ((C << N) - 1).  */
12221           if (TREE_CODE (arg1) == LSHIFT_EXPR)
12222             c = TREE_OPERAND (arg1, 0);
12223
12224           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
12225             {
12226               tree mask = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
12227                                        build_int_cst (TREE_TYPE (arg1), 1));
12228               if (strict_overflow_p)
12229                 fold_overflow_warning (("assuming signed overflow does not "
12230                                         "occur when simplifying "
12231                                         "X % (power of two)"),
12232                                        WARN_STRICT_OVERFLOW_MISC);
12233               return fold_build2_loc (loc, BIT_AND_EXPR, type,
12234                                   fold_convert_loc (loc, type, arg0),
12235                                   fold_convert_loc (loc, type, mask));
12236             }
12237         }
12238
12239       /* X % -C is the same as X % C.  */
12240       if (code == TRUNC_MOD_EXPR
12241           && !TYPE_UNSIGNED (type)
12242           && TREE_CODE (arg1) == INTEGER_CST
12243           && !TREE_OVERFLOW (arg1)
12244           && TREE_INT_CST_HIGH (arg1) < 0
12245           && !TYPE_OVERFLOW_TRAPS (type)
12246           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
12247           && !sign_bit_p (arg1, arg1))
12248         return fold_build2_loc (loc, code, type,
12249                             fold_convert_loc (loc, type, arg0),
12250                             fold_convert_loc (loc, type,
12251                                               negate_expr (arg1)));
12252
12253       /* X % -Y is the same as X % Y.  */
12254       if (code == TRUNC_MOD_EXPR
12255           && !TYPE_UNSIGNED (type)
12256           && TREE_CODE (arg1) == NEGATE_EXPR
12257           && !TYPE_OVERFLOW_TRAPS (type))
12258         return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
12259                             fold_convert_loc (loc, type,
12260                                               TREE_OPERAND (arg1, 0)));
12261
12262       if (TREE_CODE (arg1) == INTEGER_CST
12263           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12264                                          &strict_overflow_p)))
12265         {
12266           if (strict_overflow_p)
12267             fold_overflow_warning (("assuming signed overflow does not occur "
12268                                     "when simplifying modulus"),
12269                                    WARN_STRICT_OVERFLOW_MISC);
12270           return fold_convert_loc (loc, type, tem);
12271         }
12272
12273       return NULL_TREE;
12274
12275     case LROTATE_EXPR:
12276     case RROTATE_EXPR:
12277       if (integer_all_onesp (arg0))
12278         return omit_one_operand_loc (loc, type, arg0, arg1);
12279       goto shift;
12280
12281     case RSHIFT_EXPR:
12282       /* Optimize -1 >> x for arithmetic right shifts.  */
12283       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
12284           && tree_expr_nonnegative_p (arg1))
12285         return omit_one_operand_loc (loc, type, arg0, arg1);
12286       /* ... fall through ...  */
12287
12288     case LSHIFT_EXPR:
12289     shift:
12290       if (integer_zerop (arg1))
12291         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12292       if (integer_zerop (arg0))
12293         return omit_one_operand_loc (loc, type, arg0, arg1);
12294
12295       /* Since negative shift count is not well-defined,
12296          don't try to compute it in the compiler.  */
12297       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12298         return NULL_TREE;
12299
12300       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
12301       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
12302           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
12303           && host_integerp (TREE_OPERAND (arg0, 1), false)
12304           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
12305         {
12306           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
12307                                + TREE_INT_CST_LOW (arg1));
12308
12309           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
12310              being well defined.  */
12311           if (low >= TYPE_PRECISION (type))
12312             {
12313               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
12314                 low = low % TYPE_PRECISION (type);
12315               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
12316                 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
12317                                          TREE_OPERAND (arg0, 0));
12318               else
12319                 low = TYPE_PRECISION (type) - 1;
12320             }
12321
12322           return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12323                               build_int_cst (type, low));
12324         }
12325
12326       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
12327          into x & ((unsigned)-1 >> c) for unsigned types.  */
12328       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
12329            || (TYPE_UNSIGNED (type)
12330                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
12331           && host_integerp (arg1, false)
12332           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
12333           && host_integerp (TREE_OPERAND (arg0, 1), false)
12334           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
12335         {
12336           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
12337           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
12338           tree lshift;
12339           tree arg00;
12340
12341           if (low0 == low1)
12342             {
12343               arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12344
12345               lshift = build_int_cst (type, -1);
12346               lshift = int_const_binop (code, lshift, arg1, 0);
12347
12348               return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
12349             }
12350         }
12351
12352       /* Rewrite an LROTATE_EXPR by a constant into an
12353          RROTATE_EXPR by a new constant.  */
12354       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
12355         {
12356           tree tem = build_int_cst (TREE_TYPE (arg1),
12357                                     TYPE_PRECISION (type));
12358           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
12359           return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
12360         }
12361
12362       /* If we have a rotate of a bit operation with the rotate count and
12363          the second operand of the bit operation both constant,
12364          permute the two operations.  */
12365       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12366           && (TREE_CODE (arg0) == BIT_AND_EXPR
12367               || TREE_CODE (arg0) == BIT_IOR_EXPR
12368               || TREE_CODE (arg0) == BIT_XOR_EXPR)
12369           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12370         return fold_build2_loc (loc, TREE_CODE (arg0), type,
12371                             fold_build2_loc (loc, code, type,
12372                                          TREE_OPERAND (arg0, 0), arg1),
12373                             fold_build2_loc (loc, code, type,
12374                                          TREE_OPERAND (arg0, 1), arg1));
12375
12376       /* Two consecutive rotates adding up to the precision of the
12377          type can be ignored.  */
12378       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12379           && TREE_CODE (arg0) == RROTATE_EXPR
12380           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12381           && TREE_INT_CST_HIGH (arg1) == 0
12382           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
12383           && ((TREE_INT_CST_LOW (arg1)
12384                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
12385               == (unsigned int) TYPE_PRECISION (type)))
12386         return TREE_OPERAND (arg0, 0);
12387
12388       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
12389               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
12390          if the latter can be further optimized.  */
12391       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
12392           && TREE_CODE (arg0) == BIT_AND_EXPR
12393           && TREE_CODE (arg1) == INTEGER_CST
12394           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12395         {
12396           tree mask = fold_build2_loc (loc, code, type,
12397                                    fold_convert_loc (loc, type,
12398                                                      TREE_OPERAND (arg0, 1)),
12399                                    arg1);
12400           tree shift = fold_build2_loc (loc, code, type,
12401                                     fold_convert_loc (loc, type,
12402                                                       TREE_OPERAND (arg0, 0)),
12403                                     arg1);
12404           tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
12405           if (tem)
12406             return tem;
12407         }
12408
12409       return NULL_TREE;
12410
12411     case MIN_EXPR:
12412       if (operand_equal_p (arg0, arg1, 0))
12413         return omit_one_operand_loc (loc, type, arg0, arg1);
12414       if (INTEGRAL_TYPE_P (type)
12415           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
12416         return omit_one_operand_loc (loc, type, arg1, arg0);
12417       tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
12418       if (tem)
12419         return tem;
12420       goto associate;
12421
12422     case MAX_EXPR:
12423       if (operand_equal_p (arg0, arg1, 0))
12424         return omit_one_operand_loc (loc, type, arg0, arg1);
12425       if (INTEGRAL_TYPE_P (type)
12426           && TYPE_MAX_VALUE (type)
12427           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
12428         return omit_one_operand_loc (loc, type, arg1, arg0);
12429       tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
12430       if (tem)
12431         return tem;
12432       goto associate;
12433
12434     case TRUTH_ANDIF_EXPR:
12435       /* Note that the operands of this must be ints
12436          and their values must be 0 or 1.
12437          ("true" is a fixed value perhaps depending on the language.)  */
12438       /* If first arg is constant zero, return it.  */
12439       if (integer_zerop (arg0))
12440         return fold_convert_loc (loc, type, arg0);
12441     case TRUTH_AND_EXPR:
12442       /* If either arg is constant true, drop it.  */
12443       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12444         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12445       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12446           /* Preserve sequence points.  */
12447           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12448         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12449       /* If second arg is constant zero, result is zero, but first arg
12450          must be evaluated.  */
12451       if (integer_zerop (arg1))
12452         return omit_one_operand_loc (loc, type, arg1, arg0);
12453       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12454          case will be handled here.  */
12455       if (integer_zerop (arg0))
12456         return omit_one_operand_loc (loc, type, arg0, arg1);
12457
12458       /* !X && X is always false.  */
12459       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12460           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12461         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12462       /* X && !X is always false.  */
12463       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12464           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12465         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12466
12467       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
12468          means A >= Y && A != MAX, but in this case we know that
12469          A < X <= MAX.  */
12470
12471       if (!TREE_SIDE_EFFECTS (arg0)
12472           && !TREE_SIDE_EFFECTS (arg1))
12473         {
12474           tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12475           if (tem && !operand_equal_p (tem, arg0, 0))
12476             return fold_build2_loc (loc, code, type, tem, arg1);
12477
12478           tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12479           if (tem && !operand_equal_p (tem, arg1, 0))
12480             return fold_build2_loc (loc, code, type, arg0, tem);
12481         }
12482
12483     truth_andor:
12484       /* We only do these simplifications if we are optimizing.  */
12485       if (!optimize)
12486         return NULL_TREE;
12487
12488       /* Check for things like (A || B) && (A || C).  We can convert this
12489          to A || (B && C).  Note that either operator can be any of the four
12490          truth and/or operations and the transformation will still be
12491          valid.   Also note that we only care about order for the
12492          ANDIF and ORIF operators.  If B contains side effects, this
12493          might change the truth-value of A.  */
12494       if (TREE_CODE (arg0) == TREE_CODE (arg1)
12495           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12496               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12497               || TREE_CODE (arg0) == TRUTH_AND_EXPR
12498               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12499           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12500         {
12501           tree a00 = TREE_OPERAND (arg0, 0);
12502           tree a01 = TREE_OPERAND (arg0, 1);
12503           tree a10 = TREE_OPERAND (arg1, 0);
12504           tree a11 = TREE_OPERAND (arg1, 1);
12505           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12506                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12507                              && (code == TRUTH_AND_EXPR
12508                                  || code == TRUTH_OR_EXPR));
12509
12510           if (operand_equal_p (a00, a10, 0))
12511             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12512                                 fold_build2_loc (loc, code, type, a01, a11));
12513           else if (commutative && operand_equal_p (a00, a11, 0))
12514             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12515                                 fold_build2_loc (loc, code, type, a01, a10));
12516           else if (commutative && operand_equal_p (a01, a10, 0))
12517             return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
12518                                 fold_build2_loc (loc, code, type, a00, a11));
12519
12520           /* This case if tricky because we must either have commutative
12521              operators or else A10 must not have side-effects.  */
12522
12523           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12524                    && operand_equal_p (a01, a11, 0))
12525             return fold_build2_loc (loc, TREE_CODE (arg0), type,
12526                                 fold_build2_loc (loc, code, type, a00, a10),
12527                                 a01);
12528         }
12529
12530       /* See if we can build a range comparison.  */
12531       if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
12532         return tem;
12533
12534       /* Check for the possibility of merging component references.  If our
12535          lhs is another similar operation, try to merge its rhs with our
12536          rhs.  Then try to merge our lhs and rhs.  */
12537       if (TREE_CODE (arg0) == code
12538           && 0 != (tem = fold_truthop (loc, code, type,
12539                                        TREE_OPERAND (arg0, 1), arg1)))
12540         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12541
12542       if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
12543         return tem;
12544
12545       return NULL_TREE;
12546
12547     case TRUTH_ORIF_EXPR:
12548       /* Note that the operands of this must be ints
12549          and their values must be 0 or true.
12550          ("true" is a fixed value perhaps depending on the language.)  */
12551       /* If first arg is constant true, return it.  */
12552       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12553         return fold_convert_loc (loc, type, arg0);
12554     case TRUTH_OR_EXPR:
12555       /* If either arg is constant zero, drop it.  */
12556       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12557         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12558       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12559           /* Preserve sequence points.  */
12560           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12561         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12562       /* If second arg is constant true, result is true, but we must
12563          evaluate first arg.  */
12564       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12565         return omit_one_operand_loc (loc, type, arg1, arg0);
12566       /* Likewise for first arg, but note this only occurs here for
12567          TRUTH_OR_EXPR.  */
12568       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12569         return omit_one_operand_loc (loc, type, arg0, arg1);
12570
12571       /* !X || X is always true.  */
12572       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12573           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12574         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12575       /* X || !X is always true.  */
12576       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12577           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12578         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12579
12580       goto truth_andor;
12581
12582     case TRUTH_XOR_EXPR:
12583       /* If the second arg is constant zero, drop it.  */
12584       if (integer_zerop (arg1))
12585         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12586       /* If the second arg is constant true, this is a logical inversion.  */
12587       if (integer_onep (arg1))
12588         {
12589           /* Only call invert_truthvalue if operand is a truth value.  */
12590           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12591             tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12592           else
12593             tem = invert_truthvalue_loc (loc, arg0);
12594           return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12595         }
12596       /* Identical arguments cancel to zero.  */
12597       if (operand_equal_p (arg0, arg1, 0))
12598         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12599
12600       /* !X ^ X is always true.  */
12601       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12602           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12603         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12604
12605       /* X ^ !X is always true.  */
12606       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12607           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12608         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12609
12610       return NULL_TREE;
12611
12612     case EQ_EXPR:
12613     case NE_EXPR:
12614       tem = fold_comparison (loc, code, type, op0, op1);
12615       if (tem != NULL_TREE)
12616         return tem;
12617
12618       /* bool_var != 0 becomes bool_var. */
12619       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12620           && code == NE_EXPR)
12621         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12622
12623       /* bool_var == 1 becomes bool_var. */
12624       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12625           && code == EQ_EXPR)
12626         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12627
12628       /* bool_var != 1 becomes !bool_var. */
12629       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12630           && code == NE_EXPR)
12631         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12632                             fold_convert_loc (loc, type, arg0));
12633
12634       /* bool_var == 0 becomes !bool_var. */
12635       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12636           && code == EQ_EXPR)
12637         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12638                             fold_convert_loc (loc, type, arg0));
12639
12640       /* !exp != 0 becomes !exp */
12641       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12642           && code == NE_EXPR)
12643         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12644
12645       /* If this is an equality comparison of the address of two non-weak,
12646          unaliased symbols neither of which are extern (since we do not
12647          have access to attributes for externs), then we know the result.  */
12648       if (TREE_CODE (arg0) == ADDR_EXPR
12649           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12650           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12651           && ! lookup_attribute ("alias",
12652                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12653           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12654           && TREE_CODE (arg1) == ADDR_EXPR
12655           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12656           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12657           && ! lookup_attribute ("alias",
12658                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12659           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12660         {
12661           /* We know that we're looking at the address of two
12662              non-weak, unaliased, static _DECL nodes.
12663
12664              It is both wasteful and incorrect to call operand_equal_p
12665              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12666              all we need to do is test pointer equality for the arguments
12667              to the two ADDR_EXPR nodes.  It is incorrect to use
12668              operand_equal_p as that function is NOT equivalent to a
12669              C equality test.  It can in fact return false for two
12670              objects which would test as equal using the C equality
12671              operator.  */
12672           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12673           return constant_boolean_node (equal
12674                                         ? code == EQ_EXPR : code != EQ_EXPR,
12675                                         type);
12676         }
12677
12678       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12679          a MINUS_EXPR of a constant, we can convert it into a comparison with
12680          a revised constant as long as no overflow occurs.  */
12681       if (TREE_CODE (arg1) == INTEGER_CST
12682           && (TREE_CODE (arg0) == PLUS_EXPR
12683               || TREE_CODE (arg0) == MINUS_EXPR)
12684           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12685           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12686                                       ? MINUS_EXPR : PLUS_EXPR,
12687                                       fold_convert_loc (loc, TREE_TYPE (arg0),
12688                                                         arg1),
12689                                       TREE_OPERAND (arg0, 1), 0))
12690           && !TREE_OVERFLOW (tem))
12691         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12692
12693       /* Similarly for a NEGATE_EXPR.  */
12694       if (TREE_CODE (arg0) == NEGATE_EXPR
12695           && TREE_CODE (arg1) == INTEGER_CST
12696           && 0 != (tem = negate_expr (arg1))
12697           && TREE_CODE (tem) == INTEGER_CST
12698           && !TREE_OVERFLOW (tem))
12699         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12700
12701       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12702       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12703           && TREE_CODE (arg1) == INTEGER_CST
12704           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12705         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12706                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12707                                          fold_convert_loc (loc,
12708                                                            TREE_TYPE (arg0),
12709                                                            arg1),
12710                                          TREE_OPERAND (arg0, 1)));
12711
12712       /* Transform comparisons of the form X +- Y CMP X to Y CMP 0.  */
12713       if ((TREE_CODE (arg0) == PLUS_EXPR
12714            || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12715            || TREE_CODE (arg0) == MINUS_EXPR)
12716           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12717           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12718               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12719         {
12720           tree val = TREE_OPERAND (arg0, 1);
12721           return omit_two_operands_loc (loc, type,
12722                                     fold_build2_loc (loc, code, type,
12723                                                  val,
12724                                                  build_int_cst (TREE_TYPE (val),
12725                                                                 0)),
12726                                     TREE_OPERAND (arg0, 0), arg1);
12727         }
12728
12729       /* Transform comparisons of the form C - X CMP X if C % 2 == 1.  */
12730       if (TREE_CODE (arg0) == MINUS_EXPR
12731           && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12732           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
12733           && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12734         {
12735           return omit_two_operands_loc (loc, type,
12736                                     code == NE_EXPR
12737                                     ? boolean_true_node : boolean_false_node,
12738                                     TREE_OPERAND (arg0, 1), arg1);
12739         }
12740
12741       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12742          for !=.  Don't do this for ordered comparisons due to overflow.  */
12743       if (TREE_CODE (arg0) == MINUS_EXPR
12744           && integer_zerop (arg1))
12745         return fold_build2_loc (loc, code, type,
12746                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12747
12748       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12749       if (TREE_CODE (arg0) == ABS_EXPR
12750           && (integer_zerop (arg1) || real_zerop (arg1)))
12751         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12752
12753       /* If this is an EQ or NE comparison with zero and ARG0 is
12754          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12755          two operations, but the latter can be done in one less insn
12756          on machines that have only two-operand insns or on which a
12757          constant cannot be the first operand.  */
12758       if (TREE_CODE (arg0) == BIT_AND_EXPR
12759           && integer_zerop (arg1))
12760         {
12761           tree arg00 = TREE_OPERAND (arg0, 0);
12762           tree arg01 = TREE_OPERAND (arg0, 1);
12763           if (TREE_CODE (arg00) == LSHIFT_EXPR
12764               && integer_onep (TREE_OPERAND (arg00, 0)))
12765             {
12766               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12767                                       arg01, TREE_OPERAND (arg00, 1));
12768               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12769                                  build_int_cst (TREE_TYPE (arg0), 1));
12770               return fold_build2_loc (loc, code, type,
12771                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12772                                   arg1);
12773             }
12774           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12775                    && integer_onep (TREE_OPERAND (arg01, 0)))
12776             {
12777               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12778                                       arg00, TREE_OPERAND (arg01, 1));
12779               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12780                                  build_int_cst (TREE_TYPE (arg0), 1));
12781               return fold_build2_loc (loc, code, type,
12782                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12783                                   arg1);
12784             }
12785         }
12786
12787       /* If this is an NE or EQ comparison of zero against the result of a
12788          signed MOD operation whose second operand is a power of 2, make
12789          the MOD operation unsigned since it is simpler and equivalent.  */
12790       if (integer_zerop (arg1)
12791           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12792           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12793               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12794               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12795               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12796           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12797         {
12798           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12799           tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12800                                      fold_convert_loc (loc, newtype,
12801                                                        TREE_OPERAND (arg0, 0)),
12802                                      fold_convert_loc (loc, newtype,
12803                                                        TREE_OPERAND (arg0, 1)));
12804
12805           return fold_build2_loc (loc, code, type, newmod,
12806                               fold_convert_loc (loc, newtype, arg1));
12807         }
12808
12809       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12810          C1 is a valid shift constant, and C2 is a power of two, i.e.
12811          a single bit.  */
12812       if (TREE_CODE (arg0) == BIT_AND_EXPR
12813           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12814           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12815              == INTEGER_CST
12816           && integer_pow2p (TREE_OPERAND (arg0, 1))
12817           && integer_zerop (arg1))
12818         {
12819           tree itype = TREE_TYPE (arg0);
12820           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12821           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12822
12823           /* Check for a valid shift count.  */
12824           if (TREE_INT_CST_HIGH (arg001) == 0
12825               && TREE_INT_CST_LOW (arg001) < prec)
12826             {
12827               tree arg01 = TREE_OPERAND (arg0, 1);
12828               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12829               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12830               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12831                  can be rewritten as (X & (C2 << C1)) != 0.  */
12832               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12833                 {
12834                   tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12835                   tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12836                   return fold_build2_loc (loc, code, type, tem, arg1);
12837                 }
12838               /* Otherwise, for signed (arithmetic) shifts,
12839                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12840                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12841               else if (!TYPE_UNSIGNED (itype))
12842                 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12843                                     arg000, build_int_cst (itype, 0));
12844               /* Otherwise, of unsigned (logical) shifts,
12845                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12846                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12847               else
12848                 return omit_one_operand_loc (loc, type,
12849                                          code == EQ_EXPR ? integer_one_node
12850                                                          : integer_zero_node,
12851                                          arg000);
12852             }
12853         }
12854
12855       /* If this is an NE comparison of zero with an AND of one, remove the
12856          comparison since the AND will give the correct value.  */
12857       if (code == NE_EXPR
12858           && integer_zerop (arg1)
12859           && TREE_CODE (arg0) == BIT_AND_EXPR
12860           && integer_onep (TREE_OPERAND (arg0, 1)))
12861         return fold_convert_loc (loc, type, arg0);
12862
12863       /* If we have (A & C) == C where C is a power of 2, convert this into
12864          (A & C) != 0.  Similarly for NE_EXPR.  */
12865       if (TREE_CODE (arg0) == BIT_AND_EXPR
12866           && integer_pow2p (TREE_OPERAND (arg0, 1))
12867           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12868         return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12869                             arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12870                                                     integer_zero_node));
12871
12872       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12873          bit, then fold the expression into A < 0 or A >= 0.  */
12874       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12875       if (tem)
12876         return tem;
12877
12878       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12879          Similarly for NE_EXPR.  */
12880       if (TREE_CODE (arg0) == BIT_AND_EXPR
12881           && TREE_CODE (arg1) == INTEGER_CST
12882           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12883         {
12884           tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12885                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12886                                    TREE_OPERAND (arg0, 1));
12887           tree dandnotc = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12888                                        arg1, notc);
12889           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12890           if (integer_nonzerop (dandnotc))
12891             return omit_one_operand_loc (loc, type, rslt, arg0);
12892         }
12893
12894       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12895          Similarly for NE_EXPR.  */
12896       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12897           && TREE_CODE (arg1) == INTEGER_CST
12898           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12899         {
12900           tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12901           tree candnotd = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12902                                        TREE_OPERAND (arg0, 1), notd);
12903           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12904           if (integer_nonzerop (candnotd))
12905             return omit_one_operand_loc (loc, type, rslt, arg0);
12906         }
12907
12908       /* If this is a comparison of a field, we may be able to simplify it.  */
12909       if ((TREE_CODE (arg0) == COMPONENT_REF
12910            || TREE_CODE (arg0) == BIT_FIELD_REF)
12911           /* Handle the constant case even without -O
12912              to make sure the warnings are given.  */
12913           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12914         {
12915           t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12916           if (t1)
12917             return t1;
12918         }
12919
12920       /* Optimize comparisons of strlen vs zero to a compare of the
12921          first character of the string vs zero.  To wit,
12922                 strlen(ptr) == 0   =>  *ptr == 0
12923                 strlen(ptr) != 0   =>  *ptr != 0
12924          Other cases should reduce to one of these two (or a constant)
12925          due to the return value of strlen being unsigned.  */
12926       if (TREE_CODE (arg0) == CALL_EXPR
12927           && integer_zerop (arg1))
12928         {
12929           tree fndecl = get_callee_fndecl (arg0);
12930
12931           if (fndecl
12932               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12933               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12934               && call_expr_nargs (arg0) == 1
12935               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12936             {
12937               tree iref = build_fold_indirect_ref_loc (loc,
12938                                                    CALL_EXPR_ARG (arg0, 0));
12939               return fold_build2_loc (loc, code, type, iref,
12940                                   build_int_cst (TREE_TYPE (iref), 0));
12941             }
12942         }
12943
12944       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12945          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12946       if (TREE_CODE (arg0) == RSHIFT_EXPR
12947           && integer_zerop (arg1)
12948           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12949         {
12950           tree arg00 = TREE_OPERAND (arg0, 0);
12951           tree arg01 = TREE_OPERAND (arg0, 1);
12952           tree itype = TREE_TYPE (arg00);
12953           if (TREE_INT_CST_HIGH (arg01) == 0
12954               && TREE_INT_CST_LOW (arg01)
12955                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12956             {
12957               if (TYPE_UNSIGNED (itype))
12958                 {
12959                   itype = signed_type_for (itype);
12960                   arg00 = fold_convert_loc (loc, itype, arg00);
12961                 }
12962               return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12963                                   type, arg00, build_int_cst (itype, 0));
12964             }
12965         }
12966
12967       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12968       if (integer_zerop (arg1)
12969           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12970         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12971                             TREE_OPERAND (arg0, 1));
12972
12973       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12974       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12975           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12976         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12977                             build_int_cst (TREE_TYPE (arg1), 0));
12978       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12979       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12980           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12981           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12982         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12983                             build_int_cst (TREE_TYPE (arg1), 0));
12984
12985       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12986       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12987           && TREE_CODE (arg1) == INTEGER_CST
12988           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12989         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12990                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12991                                          TREE_OPERAND (arg0, 1), arg1));
12992
12993       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12994          (X & C) == 0 when C is a single bit.  */
12995       if (TREE_CODE (arg0) == BIT_AND_EXPR
12996           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12997           && integer_zerop (arg1)
12998           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12999         {
13000           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
13001                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
13002                              TREE_OPERAND (arg0, 1));
13003           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
13004                               type, tem, arg1);
13005         }
13006
13007       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
13008          constant C is a power of two, i.e. a single bit.  */
13009       if (TREE_CODE (arg0) == BIT_XOR_EXPR
13010           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13011           && integer_zerop (arg1)
13012           && integer_pow2p (TREE_OPERAND (arg0, 1))
13013           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13014                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
13015         {
13016           tree arg00 = TREE_OPERAND (arg0, 0);
13017           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
13018                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
13019         }
13020
13021       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
13022          when is C is a power of two, i.e. a single bit.  */
13023       if (TREE_CODE (arg0) == BIT_AND_EXPR
13024           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
13025           && integer_zerop (arg1)
13026           && integer_pow2p (TREE_OPERAND (arg0, 1))
13027           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13028                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
13029         {
13030           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
13031           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
13032                              arg000, TREE_OPERAND (arg0, 1));
13033           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
13034                               tem, build_int_cst (TREE_TYPE (tem), 0));
13035         }
13036
13037       if (integer_zerop (arg1)
13038           && tree_expr_nonzero_p (arg0))
13039         {
13040           tree res = constant_boolean_node (code==NE_EXPR, type);
13041           return omit_one_operand_loc (loc, type, res, arg0);
13042         }
13043
13044       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
13045       if (TREE_CODE (arg0) == NEGATE_EXPR
13046           && TREE_CODE (arg1) == NEGATE_EXPR)
13047         return fold_build2_loc (loc, code, type,
13048                             TREE_OPERAND (arg0, 0),
13049                             TREE_OPERAND (arg1, 0));
13050
13051       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
13052       if (TREE_CODE (arg0) == BIT_AND_EXPR
13053           && TREE_CODE (arg1) == BIT_AND_EXPR)
13054         {
13055           tree arg00 = TREE_OPERAND (arg0, 0);
13056           tree arg01 = TREE_OPERAND (arg0, 1);
13057           tree arg10 = TREE_OPERAND (arg1, 0);
13058           tree arg11 = TREE_OPERAND (arg1, 1);
13059           tree itype = TREE_TYPE (arg0);
13060
13061           if (operand_equal_p (arg01, arg11, 0))
13062             return fold_build2_loc (loc, code, type,
13063                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13064                                              fold_build2_loc (loc,
13065                                                           BIT_XOR_EXPR, itype,
13066                                                           arg00, arg10),
13067                                              arg01),
13068                                 build_int_cst (itype, 0));
13069
13070           if (operand_equal_p (arg01, arg10, 0))
13071             return fold_build2_loc (loc, code, type,
13072                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13073                                              fold_build2_loc (loc,
13074                                                           BIT_XOR_EXPR, itype,
13075                                                           arg00, arg11),
13076                                              arg01),
13077                                 build_int_cst (itype, 0));
13078
13079           if (operand_equal_p (arg00, arg11, 0))
13080             return fold_build2_loc (loc, code, type,
13081                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13082                                              fold_build2_loc (loc,
13083                                                           BIT_XOR_EXPR, itype,
13084                                                           arg01, arg10),
13085                                              arg00),
13086                                 build_int_cst (itype, 0));
13087
13088           if (operand_equal_p (arg00, arg10, 0))
13089             return fold_build2_loc (loc, code, type,
13090                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13091                                              fold_build2_loc (loc,
13092                                                           BIT_XOR_EXPR, itype,
13093                                                           arg01, arg11),
13094                                              arg00),
13095                                 build_int_cst (itype, 0));
13096         }
13097
13098       if (TREE_CODE (arg0) == BIT_XOR_EXPR
13099           && TREE_CODE (arg1) == BIT_XOR_EXPR)
13100         {
13101           tree arg00 = TREE_OPERAND (arg0, 0);
13102           tree arg01 = TREE_OPERAND (arg0, 1);
13103           tree arg10 = TREE_OPERAND (arg1, 0);
13104           tree arg11 = TREE_OPERAND (arg1, 1);
13105           tree itype = TREE_TYPE (arg0);
13106
13107           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
13108              operand_equal_p guarantees no side-effects so we don't need
13109              to use omit_one_operand on Z.  */
13110           if (operand_equal_p (arg01, arg11, 0))
13111             return fold_build2_loc (loc, code, type, arg00, arg10);
13112           if (operand_equal_p (arg01, arg10, 0))
13113             return fold_build2_loc (loc, code, type, arg00, arg11);
13114           if (operand_equal_p (arg00, arg11, 0))
13115             return fold_build2_loc (loc, code, type, arg01, arg10);
13116           if (operand_equal_p (arg00, arg10, 0))
13117             return fold_build2_loc (loc, code, type, arg01, arg11);
13118
13119           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
13120           if (TREE_CODE (arg01) == INTEGER_CST
13121               && TREE_CODE (arg11) == INTEGER_CST)
13122             return fold_build2_loc (loc, code, type,
13123                                 fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00,
13124                                              fold_build2_loc (loc,
13125                                                           BIT_XOR_EXPR, itype,
13126                                                           arg01, arg11)),
13127                                 arg10);
13128         }
13129
13130       /* Attempt to simplify equality/inequality comparisons of complex
13131          values.  Only lower the comparison if the result is known or
13132          can be simplified to a single scalar comparison.  */
13133       if ((TREE_CODE (arg0) == COMPLEX_EXPR
13134            || TREE_CODE (arg0) == COMPLEX_CST)
13135           && (TREE_CODE (arg1) == COMPLEX_EXPR
13136               || TREE_CODE (arg1) == COMPLEX_CST))
13137         {
13138           tree real0, imag0, real1, imag1;
13139           tree rcond, icond;
13140
13141           if (TREE_CODE (arg0) == COMPLEX_EXPR)
13142             {
13143               real0 = TREE_OPERAND (arg0, 0);
13144               imag0 = TREE_OPERAND (arg0, 1);
13145             }
13146           else
13147             {
13148               real0 = TREE_REALPART (arg0);
13149               imag0 = TREE_IMAGPART (arg0);
13150             }
13151
13152           if (TREE_CODE (arg1) == COMPLEX_EXPR)
13153             {
13154               real1 = TREE_OPERAND (arg1, 0);
13155               imag1 = TREE_OPERAND (arg1, 1);
13156             }
13157           else
13158             {
13159               real1 = TREE_REALPART (arg1);
13160               imag1 = TREE_IMAGPART (arg1);
13161             }
13162
13163           rcond = fold_binary_loc (loc, code, type, real0, real1);
13164           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
13165             {
13166               if (integer_zerop (rcond))
13167                 {
13168                   if (code == EQ_EXPR)
13169                     return omit_two_operands_loc (loc, type, boolean_false_node,
13170                                               imag0, imag1);
13171                   return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
13172                 }
13173               else
13174                 {
13175                   if (code == NE_EXPR)
13176                     return omit_two_operands_loc (loc, type, boolean_true_node,
13177                                               imag0, imag1);
13178                   return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
13179                 }
13180             }
13181
13182           icond = fold_binary_loc (loc, code, type, imag0, imag1);
13183           if (icond && TREE_CODE (icond) == INTEGER_CST)
13184             {
13185               if (integer_zerop (icond))
13186                 {
13187                   if (code == EQ_EXPR)
13188                     return omit_two_operands_loc (loc, type, boolean_false_node,
13189                                               real0, real1);
13190                   return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
13191                 }
13192               else
13193                 {
13194                   if (code == NE_EXPR)
13195                     return omit_two_operands_loc (loc, type, boolean_true_node,
13196                                               real0, real1);
13197                   return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
13198                 }
13199             }
13200         }
13201
13202       return NULL_TREE;
13203
13204     case LT_EXPR:
13205     case GT_EXPR:
13206     case LE_EXPR:
13207     case GE_EXPR:
13208       tem = fold_comparison (loc, code, type, op0, op1);
13209       if (tem != NULL_TREE)
13210         return tem;
13211
13212       /* Transform comparisons of the form X +- C CMP X.  */
13213       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
13214           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
13215           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
13216                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
13217               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
13218                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
13219         {
13220           tree arg01 = TREE_OPERAND (arg0, 1);
13221           enum tree_code code0 = TREE_CODE (arg0);
13222           int is_positive;
13223
13224           if (TREE_CODE (arg01) == REAL_CST)
13225             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
13226           else
13227             is_positive = tree_int_cst_sgn (arg01);
13228
13229           /* (X - c) > X becomes false.  */
13230           if (code == GT_EXPR
13231               && ((code0 == MINUS_EXPR && is_positive >= 0)
13232                   || (code0 == PLUS_EXPR && is_positive <= 0)))
13233             {
13234               if (TREE_CODE (arg01) == INTEGER_CST
13235                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13236                 fold_overflow_warning (("assuming signed overflow does not "
13237                                         "occur when assuming that (X - c) > X "
13238                                         "is always false"),
13239                                        WARN_STRICT_OVERFLOW_ALL);
13240               return constant_boolean_node (0, type);
13241             }
13242
13243           /* Likewise (X + c) < X becomes false.  */
13244           if (code == LT_EXPR
13245               && ((code0 == PLUS_EXPR && is_positive >= 0)
13246                   || (code0 == MINUS_EXPR && is_positive <= 0)))
13247             {
13248               if (TREE_CODE (arg01) == INTEGER_CST
13249                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13250                 fold_overflow_warning (("assuming signed overflow does not "
13251                                         "occur when assuming that "
13252                                         "(X + c) < X is always false"),
13253                                        WARN_STRICT_OVERFLOW_ALL);
13254               return constant_boolean_node (0, type);
13255             }
13256
13257           /* Convert (X - c) <= X to true.  */
13258           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13259               && code == LE_EXPR
13260               && ((code0 == MINUS_EXPR && is_positive >= 0)
13261                   || (code0 == PLUS_EXPR && is_positive <= 0)))
13262             {
13263               if (TREE_CODE (arg01) == INTEGER_CST
13264                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13265                 fold_overflow_warning (("assuming signed overflow does not "
13266                                         "occur when assuming that "
13267                                         "(X - c) <= X is always true"),
13268                                        WARN_STRICT_OVERFLOW_ALL);
13269               return constant_boolean_node (1, type);
13270             }
13271
13272           /* Convert (X + c) >= X to true.  */
13273           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13274               && code == GE_EXPR
13275               && ((code0 == PLUS_EXPR && is_positive >= 0)
13276                   || (code0 == MINUS_EXPR && is_positive <= 0)))
13277             {
13278               if (TREE_CODE (arg01) == INTEGER_CST
13279                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13280                 fold_overflow_warning (("assuming signed overflow does not "
13281                                         "occur when assuming that "
13282                                         "(X + c) >= X is always true"),
13283                                        WARN_STRICT_OVERFLOW_ALL);
13284               return constant_boolean_node (1, type);
13285             }
13286
13287           if (TREE_CODE (arg01) == INTEGER_CST)
13288             {
13289               /* Convert X + c > X and X - c < X to true for integers.  */
13290               if (code == GT_EXPR
13291                   && ((code0 == PLUS_EXPR && is_positive > 0)
13292                       || (code0 == MINUS_EXPR && is_positive < 0)))
13293                 {
13294                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13295                     fold_overflow_warning (("assuming signed overflow does "
13296                                             "not occur when assuming that "
13297                                             "(X + c) > X is always true"),
13298                                            WARN_STRICT_OVERFLOW_ALL);
13299                   return constant_boolean_node (1, type);
13300                 }
13301
13302               if (code == LT_EXPR
13303                   && ((code0 == MINUS_EXPR && is_positive > 0)
13304                       || (code0 == PLUS_EXPR && is_positive < 0)))
13305                 {
13306                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13307                     fold_overflow_warning (("assuming signed overflow does "
13308                                             "not occur when assuming that "
13309                                             "(X - c) < X is always true"),
13310                                            WARN_STRICT_OVERFLOW_ALL);
13311                   return constant_boolean_node (1, type);
13312                 }
13313
13314               /* Convert X + c <= X and X - c >= X to false for integers.  */
13315               if (code == LE_EXPR
13316                   && ((code0 == PLUS_EXPR && is_positive > 0)
13317                       || (code0 == MINUS_EXPR && is_positive < 0)))
13318                 {
13319                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13320                     fold_overflow_warning (("assuming signed overflow does "
13321                                             "not occur when assuming that "
13322                                             "(X + c) <= X is always false"),
13323                                            WARN_STRICT_OVERFLOW_ALL);
13324                   return constant_boolean_node (0, type);
13325                 }
13326
13327               if (code == GE_EXPR
13328                   && ((code0 == MINUS_EXPR && is_positive > 0)
13329                       || (code0 == PLUS_EXPR && is_positive < 0)))
13330                 {
13331                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13332                     fold_overflow_warning (("assuming signed overflow does "
13333                                             "not occur when assuming that "
13334                                             "(X - c) >= X is always false"),
13335                                            WARN_STRICT_OVERFLOW_ALL);
13336                   return constant_boolean_node (0, type);
13337                 }
13338             }
13339         }
13340
13341       /* Comparisons with the highest or lowest possible integer of
13342          the specified precision will have known values.  */
13343       {
13344         tree arg1_type = TREE_TYPE (arg1);
13345         unsigned int width = TYPE_PRECISION (arg1_type);
13346
13347         if (TREE_CODE (arg1) == INTEGER_CST
13348             && width <= 2 * HOST_BITS_PER_WIDE_INT
13349             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
13350           {
13351             HOST_WIDE_INT signed_max_hi;
13352             unsigned HOST_WIDE_INT signed_max_lo;
13353             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
13354
13355             if (width <= HOST_BITS_PER_WIDE_INT)
13356               {
13357                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13358                                 - 1;
13359                 signed_max_hi = 0;
13360                 max_hi = 0;
13361
13362                 if (TYPE_UNSIGNED (arg1_type))
13363                   {
13364                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13365                     min_lo = 0;
13366                     min_hi = 0;
13367                   }
13368                 else
13369                   {
13370                     max_lo = signed_max_lo;
13371                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
13372                     min_hi = -1;
13373                   }
13374               }
13375             else
13376               {
13377                 width -= HOST_BITS_PER_WIDE_INT;
13378                 signed_max_lo = -1;
13379                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13380                                 - 1;
13381                 max_lo = -1;
13382                 min_lo = 0;
13383
13384                 if (TYPE_UNSIGNED (arg1_type))
13385                   {
13386                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13387                     min_hi = 0;
13388                   }
13389                 else
13390                   {
13391                     max_hi = signed_max_hi;
13392                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
13393                   }
13394               }
13395
13396             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
13397                 && TREE_INT_CST_LOW (arg1) == max_lo)
13398               switch (code)
13399                 {
13400                 case GT_EXPR:
13401                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13402
13403                 case GE_EXPR:
13404                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13405
13406                 case LE_EXPR:
13407                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13408
13409                 case LT_EXPR:
13410                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13411
13412                 /* The GE_EXPR and LT_EXPR cases above are not normally
13413                    reached because of previous transformations.  */
13414
13415                 default:
13416                   break;
13417                 }
13418             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13419                      == max_hi
13420                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
13421               switch (code)
13422                 {
13423                 case GT_EXPR:
13424                   arg1 = const_binop (PLUS_EXPR, arg1,
13425                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
13426                   return fold_build2_loc (loc, EQ_EXPR, type,
13427                                       fold_convert_loc (loc,
13428                                                         TREE_TYPE (arg1), arg0),
13429                                       arg1);
13430                 case LE_EXPR:
13431                   arg1 = const_binop (PLUS_EXPR, arg1,
13432                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
13433                   return fold_build2_loc (loc, NE_EXPR, type,
13434                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13435                                                         arg0),
13436                                       arg1);
13437                 default:
13438                   break;
13439                 }
13440             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13441                      == min_hi
13442                      && TREE_INT_CST_LOW (arg1) == min_lo)
13443               switch (code)
13444                 {
13445                 case LT_EXPR:
13446                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13447
13448                 case LE_EXPR:
13449                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13450
13451                 case GE_EXPR:
13452                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13453
13454                 case GT_EXPR:
13455                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13456
13457                 default:
13458                   break;
13459                 }
13460             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13461                      == min_hi
13462                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
13463               switch (code)
13464                 {
13465                 case GE_EXPR:
13466                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13467                   return fold_build2_loc (loc, NE_EXPR, type,
13468                                       fold_convert_loc (loc,
13469                                                         TREE_TYPE (arg1), arg0),
13470                                       arg1);
13471                 case LT_EXPR:
13472                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13473                   return fold_build2_loc (loc, EQ_EXPR, type,
13474                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13475                                                         arg0),
13476                                       arg1);
13477                 default:
13478                   break;
13479                 }
13480
13481             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13482                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
13483                      && TYPE_UNSIGNED (arg1_type)
13484                      /* We will flip the signedness of the comparison operator
13485                         associated with the mode of arg1, so the sign bit is
13486                         specified by this mode.  Check that arg1 is the signed
13487                         max associated with this sign bit.  */
13488                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13489                      /* signed_type does not work on pointer types.  */
13490                      && INTEGRAL_TYPE_P (arg1_type))
13491               {
13492                 /* The following case also applies to X < signed_max+1
13493                    and X >= signed_max+1 because previous transformations.  */
13494                 if (code == LE_EXPR || code == GT_EXPR)
13495                   {
13496                     tree st;
13497                     st = signed_type_for (TREE_TYPE (arg1));
13498                     return fold_build2_loc (loc,
13499                                         code == LE_EXPR ? GE_EXPR : LT_EXPR,
13500                                         type, fold_convert_loc (loc, st, arg0),
13501                                         build_int_cst (st, 0));
13502                   }
13503               }
13504           }
13505       }
13506
13507       /* If we are comparing an ABS_EXPR with a constant, we can
13508          convert all the cases into explicit comparisons, but they may
13509          well not be faster than doing the ABS and one comparison.
13510          But ABS (X) <= C is a range comparison, which becomes a subtraction
13511          and a comparison, and is probably faster.  */
13512       if (code == LE_EXPR
13513           && TREE_CODE (arg1) == INTEGER_CST
13514           && TREE_CODE (arg0) == ABS_EXPR
13515           && ! TREE_SIDE_EFFECTS (arg0)
13516           && (0 != (tem = negate_expr (arg1)))
13517           && TREE_CODE (tem) == INTEGER_CST
13518           && !TREE_OVERFLOW (tem))
13519         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13520                             build2 (GE_EXPR, type,
13521                                     TREE_OPERAND (arg0, 0), tem),
13522                             build2 (LE_EXPR, type,
13523                                     TREE_OPERAND (arg0, 0), arg1));
13524
13525       /* Convert ABS_EXPR<x> >= 0 to true.  */
13526       strict_overflow_p = false;
13527       if (code == GE_EXPR
13528           && (integer_zerop (arg1)
13529               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13530                   && real_zerop (arg1)))
13531           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13532         {
13533           if (strict_overflow_p)
13534             fold_overflow_warning (("assuming signed overflow does not occur "
13535                                     "when simplifying comparison of "
13536                                     "absolute value and zero"),
13537                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13538           return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13539         }
13540
13541       /* Convert ABS_EXPR<x> < 0 to false.  */
13542       strict_overflow_p = false;
13543       if (code == LT_EXPR
13544           && (integer_zerop (arg1) || real_zerop (arg1))
13545           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13546         {
13547           if (strict_overflow_p)
13548             fold_overflow_warning (("assuming signed overflow does not occur "
13549                                     "when simplifying comparison of "
13550                                     "absolute value and zero"),
13551                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13552           return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13553         }
13554
13555       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13556          and similarly for >= into !=.  */
13557       if ((code == LT_EXPR || code == GE_EXPR)
13558           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13559           && TREE_CODE (arg1) == LSHIFT_EXPR
13560           && integer_onep (TREE_OPERAND (arg1, 0)))
13561         {
13562           tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13563                         build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13564                                 TREE_OPERAND (arg1, 1)),
13565                         build_int_cst (TREE_TYPE (arg0), 0));
13566           goto fold_binary_exit;
13567         }
13568
13569       if ((code == LT_EXPR || code == GE_EXPR)
13570           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13571           && CONVERT_EXPR_P (arg1)
13572           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13573           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13574         {
13575           tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13576                         fold_convert_loc (loc, TREE_TYPE (arg0),
13577                                           build2 (RSHIFT_EXPR,
13578                                                   TREE_TYPE (arg0), arg0,
13579                                                   TREE_OPERAND (TREE_OPERAND (arg1, 0),
13580                                                                 1))),
13581                         build_int_cst (TREE_TYPE (arg0), 0));
13582           goto fold_binary_exit;
13583         }
13584
13585       return NULL_TREE;
13586
13587     case UNORDERED_EXPR:
13588     case ORDERED_EXPR:
13589     case UNLT_EXPR:
13590     case UNLE_EXPR:
13591     case UNGT_EXPR:
13592     case UNGE_EXPR:
13593     case UNEQ_EXPR:
13594     case LTGT_EXPR:
13595       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13596         {
13597           t1 = fold_relational_const (code, type, arg0, arg1);
13598           if (t1 != NULL_TREE)
13599             return t1;
13600         }
13601
13602       /* If the first operand is NaN, the result is constant.  */
13603       if (TREE_CODE (arg0) == REAL_CST
13604           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13605           && (code != LTGT_EXPR || ! flag_trapping_math))
13606         {
13607           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13608                ? integer_zero_node
13609                : integer_one_node;
13610           return omit_one_operand_loc (loc, type, t1, arg1);
13611         }
13612
13613       /* If the second operand is NaN, the result is constant.  */
13614       if (TREE_CODE (arg1) == REAL_CST
13615           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13616           && (code != LTGT_EXPR || ! flag_trapping_math))
13617         {
13618           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13619                ? integer_zero_node
13620                : integer_one_node;
13621           return omit_one_operand_loc (loc, type, t1, arg0);
13622         }
13623
13624       /* Simplify unordered comparison of something with itself.  */
13625       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13626           && operand_equal_p (arg0, arg1, 0))
13627         return constant_boolean_node (1, type);
13628
13629       if (code == LTGT_EXPR
13630           && !flag_trapping_math
13631           && operand_equal_p (arg0, arg1, 0))
13632         return constant_boolean_node (0, type);
13633
13634       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13635       {
13636         tree targ0 = strip_float_extensions (arg0);
13637         tree targ1 = strip_float_extensions (arg1);
13638         tree newtype = TREE_TYPE (targ0);
13639
13640         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13641           newtype = TREE_TYPE (targ1);
13642
13643         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13644           return fold_build2_loc (loc, code, type,
13645                               fold_convert_loc (loc, newtype, targ0),
13646                               fold_convert_loc (loc, newtype, targ1));
13647       }
13648
13649       return NULL_TREE;
13650
13651     case COMPOUND_EXPR:
13652       /* When pedantic, a compound expression can be neither an lvalue
13653          nor an integer constant expression.  */
13654       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13655         return NULL_TREE;
13656       /* Don't let (0, 0) be null pointer constant.  */
13657       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13658                                  : fold_convert_loc (loc, type, arg1);
13659       return pedantic_non_lvalue_loc (loc, tem);
13660
13661     case COMPLEX_EXPR:
13662       if ((TREE_CODE (arg0) == REAL_CST
13663            && TREE_CODE (arg1) == REAL_CST)
13664           || (TREE_CODE (arg0) == INTEGER_CST
13665               && TREE_CODE (arg1) == INTEGER_CST))
13666         return build_complex (type, arg0, arg1);
13667       return NULL_TREE;
13668
13669     case ASSERT_EXPR:
13670       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13671       gcc_unreachable ();
13672
13673     default:
13674       return NULL_TREE;
13675     } /* switch (code) */
13676  fold_binary_exit:
13677   protected_set_expr_location (tem, loc);
13678   return tem;
13679 }
13680
13681 /* Callback for walk_tree, looking for LABEL_EXPR.  Return *TP if it is
13682    a LABEL_EXPR; otherwise return NULL_TREE.  Do not check the subtrees
13683    of GOTO_EXPR.  */
13684
13685 static tree
13686 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13687 {
13688   switch (TREE_CODE (*tp))
13689     {
13690     case LABEL_EXPR:
13691       return *tp;
13692
13693     case GOTO_EXPR:
13694       *walk_subtrees = 0;
13695
13696       /* ... fall through ...  */
13697
13698     default:
13699       return NULL_TREE;
13700     }
13701 }
13702
13703 /* Return whether the sub-tree ST contains a label which is accessible from
13704    outside the sub-tree.  */
13705
13706 static bool
13707 contains_label_p (tree st)
13708 {
13709   return
13710    (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13711 }
13712
13713 /* Fold a ternary expression of code CODE and type TYPE with operands
13714    OP0, OP1, and OP2.  Return the folded expression if folding is
13715    successful.  Otherwise, return NULL_TREE.  */
13716
13717 tree
13718 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13719               tree op0, tree op1, tree op2)
13720 {
13721   tree tem;
13722   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13723   enum tree_code_class kind = TREE_CODE_CLASS (code);
13724
13725   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13726               && TREE_CODE_LENGTH (code) == 3);
13727
13728   /* Strip any conversions that don't change the mode.  This is safe
13729      for every expression, except for a comparison expression because
13730      its signedness is derived from its operands.  So, in the latter
13731      case, only strip conversions that don't change the signedness.
13732
13733      Note that this is done as an internal manipulation within the
13734      constant folder, in order to find the simplest representation of
13735      the arguments so that their form can be studied.  In any cases,
13736      the appropriate type conversions should be put back in the tree
13737      that will get out of the constant folder.  */
13738   if (op0)
13739     {
13740       arg0 = op0;
13741       STRIP_NOPS (arg0);
13742     }
13743
13744   if (op1)
13745     {
13746       arg1 = op1;
13747       STRIP_NOPS (arg1);
13748     }
13749
13750   switch (code)
13751     {
13752     case COMPONENT_REF:
13753       if (TREE_CODE (arg0) == CONSTRUCTOR
13754           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13755         {
13756           unsigned HOST_WIDE_INT idx;
13757           tree field, value;
13758           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13759             if (field == arg1)
13760               return value;
13761         }
13762       return NULL_TREE;
13763
13764     case COND_EXPR:
13765       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13766          so all simple results must be passed through pedantic_non_lvalue.  */
13767       if (TREE_CODE (arg0) == INTEGER_CST)
13768         {
13769           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13770           tem = integer_zerop (arg0) ? op2 : op1;
13771           /* Only optimize constant conditions when the selected branch
13772              has the same type as the COND_EXPR.  This avoids optimizing
13773              away "c ? x : throw", where the throw has a void type.
13774              Avoid throwing away that operand which contains label.  */
13775           if ((!TREE_SIDE_EFFECTS (unused_op)
13776                || !contains_label_p (unused_op))
13777               && (! VOID_TYPE_P (TREE_TYPE (tem))
13778                   || VOID_TYPE_P (type)))
13779             return pedantic_non_lvalue_loc (loc, tem);
13780           return NULL_TREE;
13781         }
13782       if (operand_equal_p (arg1, op2, 0))
13783         return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13784
13785       /* If we have A op B ? A : C, we may be able to convert this to a
13786          simpler expression, depending on the operation and the values
13787          of B and C.  Signed zeros prevent all of these transformations,
13788          for reasons given above each one.
13789
13790          Also try swapping the arguments and inverting the conditional.  */
13791       if (COMPARISON_CLASS_P (arg0)
13792           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13793                                              arg1, TREE_OPERAND (arg0, 1))
13794           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13795         {
13796           tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13797           if (tem)
13798             return tem;
13799         }
13800
13801       if (COMPARISON_CLASS_P (arg0)
13802           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13803                                              op2,
13804                                              TREE_OPERAND (arg0, 1))
13805           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13806         {
13807           tem = fold_truth_not_expr (loc, arg0);
13808           if (tem && COMPARISON_CLASS_P (tem))
13809             {
13810               tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13811               if (tem)
13812                 return tem;
13813             }
13814         }
13815
13816       /* If the second operand is simpler than the third, swap them
13817          since that produces better jump optimization results.  */
13818       if (truth_value_p (TREE_CODE (arg0))
13819           && tree_swap_operands_p (op1, op2, false))
13820         {
13821           /* See if this can be inverted.  If it can't, possibly because
13822              it was a floating-point inequality comparison, don't do
13823              anything.  */
13824           tem = fold_truth_not_expr (loc, arg0);
13825           if (tem)
13826             return fold_build3_loc (loc, code, type, tem, op2, op1);
13827         }
13828
13829       /* Convert A ? 1 : 0 to simply A.  */
13830       if (integer_onep (op1)
13831           && integer_zerop (op2)
13832           /* If we try to convert OP0 to our type, the
13833              call to fold will try to move the conversion inside
13834              a COND, which will recurse.  In that case, the COND_EXPR
13835              is probably the best choice, so leave it alone.  */
13836           && type == TREE_TYPE (arg0))
13837         return pedantic_non_lvalue_loc (loc, arg0);
13838
13839       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13840          over COND_EXPR in cases such as floating point comparisons.  */
13841       if (integer_zerop (op1)
13842           && integer_onep (op2)
13843           && truth_value_p (TREE_CODE (arg0)))
13844         return pedantic_non_lvalue_loc (loc,
13845                                     fold_convert_loc (loc, type,
13846                                               invert_truthvalue_loc (loc,
13847                                                                      arg0)));
13848
13849       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13850       if (TREE_CODE (arg0) == LT_EXPR
13851           && integer_zerop (TREE_OPERAND (arg0, 1))
13852           && integer_zerop (op2)
13853           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13854         {
13855           /* sign_bit_p only checks ARG1 bits within A's precision.
13856              If <sign bit of A> has wider type than A, bits outside
13857              of A's precision in <sign bit of A> need to be checked.
13858              If they are all 0, this optimization needs to be done
13859              in unsigned A's type, if they are all 1 in signed A's type,
13860              otherwise this can't be done.  */
13861           if (TYPE_PRECISION (TREE_TYPE (tem))
13862               < TYPE_PRECISION (TREE_TYPE (arg1))
13863               && TYPE_PRECISION (TREE_TYPE (tem))
13864                  < TYPE_PRECISION (type))
13865             {
13866               unsigned HOST_WIDE_INT mask_lo;
13867               HOST_WIDE_INT mask_hi;
13868               int inner_width, outer_width;
13869               tree tem_type;
13870
13871               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13872               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13873               if (outer_width > TYPE_PRECISION (type))
13874                 outer_width = TYPE_PRECISION (type);
13875
13876               if (outer_width > HOST_BITS_PER_WIDE_INT)
13877                 {
13878                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13879                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13880                   mask_lo = -1;
13881                 }
13882               else
13883                 {
13884                   mask_hi = 0;
13885                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13886                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13887                 }
13888               if (inner_width > HOST_BITS_PER_WIDE_INT)
13889                 {
13890                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13891                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13892                   mask_lo = 0;
13893                 }
13894               else
13895                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13896                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13897
13898               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13899                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13900                 {
13901                   tem_type = signed_type_for (TREE_TYPE (tem));
13902                   tem = fold_convert_loc (loc, tem_type, tem);
13903                 }
13904               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13905                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13906                 {
13907                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13908                   tem = fold_convert_loc (loc, tem_type, tem);
13909                 }
13910               else
13911                 tem = NULL;
13912             }
13913
13914           if (tem)
13915             return
13916               fold_convert_loc (loc, type,
13917                                 fold_build2_loc (loc, BIT_AND_EXPR,
13918                                              TREE_TYPE (tem), tem,
13919                                              fold_convert_loc (loc,
13920                                                                TREE_TYPE (tem),
13921                                                                arg1)));
13922         }
13923
13924       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13925          already handled above.  */
13926       if (TREE_CODE (arg0) == BIT_AND_EXPR
13927           && integer_onep (TREE_OPERAND (arg0, 1))
13928           && integer_zerop (op2)
13929           && integer_pow2p (arg1))
13930         {
13931           tree tem = TREE_OPERAND (arg0, 0);
13932           STRIP_NOPS (tem);
13933           if (TREE_CODE (tem) == RSHIFT_EXPR
13934               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13935               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13936                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13937             return fold_build2_loc (loc, BIT_AND_EXPR, type,
13938                                 TREE_OPERAND (tem, 0), arg1);
13939         }
13940
13941       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13942          is probably obsolete because the first operand should be a
13943          truth value (that's why we have the two cases above), but let's
13944          leave it in until we can confirm this for all front-ends.  */
13945       if (integer_zerop (op2)
13946           && TREE_CODE (arg0) == NE_EXPR
13947           && integer_zerop (TREE_OPERAND (arg0, 1))
13948           && integer_pow2p (arg1)
13949           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13950           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13951                               arg1, OEP_ONLY_CONST))
13952         return pedantic_non_lvalue_loc (loc,
13953                                     fold_convert_loc (loc, type,
13954                                                       TREE_OPERAND (arg0, 0)));
13955
13956       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13957       if (integer_zerop (op2)
13958           && truth_value_p (TREE_CODE (arg0))
13959           && truth_value_p (TREE_CODE (arg1)))
13960         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13961                             fold_convert_loc (loc, type, arg0),
13962                             arg1);
13963
13964       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13965       if (integer_onep (op2)
13966           && truth_value_p (TREE_CODE (arg0))
13967           && truth_value_p (TREE_CODE (arg1)))
13968         {
13969           /* Only perform transformation if ARG0 is easily inverted.  */
13970           tem = fold_truth_not_expr (loc, arg0);
13971           if (tem)
13972             return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13973                                 fold_convert_loc (loc, type, tem),
13974                                 arg1);
13975         }
13976
13977       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13978       if (integer_zerop (arg1)
13979           && truth_value_p (TREE_CODE (arg0))
13980           && truth_value_p (TREE_CODE (op2)))
13981         {
13982           /* Only perform transformation if ARG0 is easily inverted.  */
13983           tem = fold_truth_not_expr (loc, arg0);
13984           if (tem)
13985             return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13986                                 fold_convert_loc (loc, type, tem),
13987                                 op2);
13988         }
13989
13990       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13991       if (integer_onep (arg1)
13992           && truth_value_p (TREE_CODE (arg0))
13993           && truth_value_p (TREE_CODE (op2)))
13994         return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13995                             fold_convert_loc (loc, type, arg0),
13996                             op2);
13997
13998       return NULL_TREE;
13999
14000     case CALL_EXPR:
14001       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
14002          of fold_ternary on them.  */
14003       gcc_unreachable ();
14004
14005     case BIT_FIELD_REF:
14006       if ((TREE_CODE (arg0) == VECTOR_CST
14007            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
14008           && type == TREE_TYPE (TREE_TYPE (arg0)))
14009         {
14010           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
14011           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
14012
14013           if (width != 0
14014               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
14015               && (idx % width) == 0
14016               && (idx = idx / width)
14017                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
14018             {
14019               tree elements = NULL_TREE;
14020
14021               if (TREE_CODE (arg0) == VECTOR_CST)
14022                 elements = TREE_VECTOR_CST_ELTS (arg0);
14023               else
14024                 {
14025                   unsigned HOST_WIDE_INT idx;
14026                   tree value;
14027
14028                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
14029                     elements = tree_cons (NULL_TREE, value, elements);
14030                 }
14031               while (idx-- > 0 && elements)
14032                 elements = TREE_CHAIN (elements);
14033               if (elements)
14034                 return TREE_VALUE (elements);
14035               else
14036                 return fold_convert_loc (loc, type, integer_zero_node);
14037             }
14038         }
14039
14040       /* A bit-field-ref that referenced the full argument can be stripped.  */
14041       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
14042           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
14043           && integer_zerop (op2))
14044         return fold_convert_loc (loc, type, arg0);
14045
14046       return NULL_TREE;
14047
14048     default:
14049       return NULL_TREE;
14050     } /* switch (code) */
14051 }
14052
14053 /* Perform constant folding and related simplification of EXPR.
14054    The related simplifications include x*1 => x, x*0 => 0, etc.,
14055    and application of the associative law.
14056    NOP_EXPR conversions may be removed freely (as long as we
14057    are careful not to change the type of the overall expression).
14058    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
14059    but we can constant-fold them if they have constant operands.  */
14060
14061 #ifdef ENABLE_FOLD_CHECKING
14062 # define fold(x) fold_1 (x)
14063 static tree fold_1 (tree);
14064 static
14065 #endif
14066 tree
14067 fold (tree expr)
14068 {
14069   const tree t = expr;
14070   enum tree_code code = TREE_CODE (t);
14071   enum tree_code_class kind = TREE_CODE_CLASS (code);
14072   tree tem;
14073   location_t loc = EXPR_LOCATION (expr);
14074
14075   /* Return right away if a constant.  */
14076   if (kind == tcc_constant)
14077     return t;
14078
14079   /* CALL_EXPR-like objects with variable numbers of operands are
14080      treated specially.  */
14081   if (kind == tcc_vl_exp)
14082     {
14083       if (code == CALL_EXPR)
14084         {
14085           tem = fold_call_expr (loc, expr, false);
14086           return tem ? tem : expr;
14087         }
14088       return expr;
14089     }
14090
14091   if (IS_EXPR_CODE_CLASS (kind))
14092     {
14093       tree type = TREE_TYPE (t);
14094       tree op0, op1, op2;
14095
14096       switch (TREE_CODE_LENGTH (code))
14097         {
14098         case 1:
14099           op0 = TREE_OPERAND (t, 0);
14100           tem = fold_unary_loc (loc, code, type, op0);
14101           return tem ? tem : expr;
14102         case 2:
14103           op0 = TREE_OPERAND (t, 0);
14104           op1 = TREE_OPERAND (t, 1);
14105           tem = fold_binary_loc (loc, code, type, op0, op1);
14106           return tem ? tem : expr;
14107         case 3:
14108           op0 = TREE_OPERAND (t, 0);
14109           op1 = TREE_OPERAND (t, 1);
14110           op2 = TREE_OPERAND (t, 2);
14111           tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14112           return tem ? tem : expr;
14113         default:
14114           break;
14115         }
14116     }
14117
14118   switch (code)
14119     {
14120     case ARRAY_REF:
14121       {
14122         tree op0 = TREE_OPERAND (t, 0);
14123         tree op1 = TREE_OPERAND (t, 1);
14124
14125         if (TREE_CODE (op1) == INTEGER_CST
14126             && TREE_CODE (op0) == CONSTRUCTOR
14127             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
14128           {
14129             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
14130             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
14131             unsigned HOST_WIDE_INT begin = 0;
14132
14133             /* Find a matching index by means of a binary search.  */
14134             while (begin != end)
14135               {
14136                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
14137                 tree index = VEC_index (constructor_elt, elts, middle)->index;
14138
14139                 if (TREE_CODE (index) == INTEGER_CST
14140                     && tree_int_cst_lt (index, op1))
14141                   begin = middle + 1;
14142                 else if (TREE_CODE (index) == INTEGER_CST
14143                          && tree_int_cst_lt (op1, index))
14144                   end = middle;
14145                 else if (TREE_CODE (index) == RANGE_EXPR
14146                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
14147                   begin = middle + 1;
14148                 else if (TREE_CODE (index) == RANGE_EXPR
14149                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
14150                   end = middle;
14151                 else
14152                   return VEC_index (constructor_elt, elts, middle)->value;
14153               }
14154           }
14155
14156         return t;
14157       }
14158
14159     case CONST_DECL:
14160       return fold (DECL_INITIAL (t));
14161
14162     default:
14163       return t;
14164     } /* switch (code) */
14165 }
14166
14167 #ifdef ENABLE_FOLD_CHECKING
14168 #undef fold
14169
14170 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
14171 static void fold_check_failed (const_tree, const_tree);
14172 void print_fold_checksum (const_tree);
14173
14174 /* When --enable-checking=fold, compute a digest of expr before
14175    and after actual fold call to see if fold did not accidentally
14176    change original expr.  */
14177
14178 tree
14179 fold (tree expr)
14180 {
14181   tree ret;
14182   struct md5_ctx ctx;
14183   unsigned char checksum_before[16], checksum_after[16];
14184   htab_t ht;
14185
14186   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14187   md5_init_ctx (&ctx);
14188   fold_checksum_tree (expr, &ctx, ht);
14189   md5_finish_ctx (&ctx, checksum_before);
14190   htab_empty (ht);
14191
14192   ret = fold_1 (expr);
14193
14194   md5_init_ctx (&ctx);
14195   fold_checksum_tree (expr, &ctx, ht);
14196   md5_finish_ctx (&ctx, checksum_after);
14197   htab_delete (ht);
14198
14199   if (memcmp (checksum_before, checksum_after, 16))
14200     fold_check_failed (expr, ret);
14201
14202   return ret;
14203 }
14204
14205 void
14206 print_fold_checksum (const_tree expr)
14207 {
14208   struct md5_ctx ctx;
14209   unsigned char checksum[16], cnt;
14210   htab_t ht;
14211
14212   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14213   md5_init_ctx (&ctx);
14214   fold_checksum_tree (expr, &ctx, ht);
14215   md5_finish_ctx (&ctx, checksum);
14216   htab_delete (ht);
14217   for (cnt = 0; cnt < 16; ++cnt)
14218     fprintf (stderr, "%02x", checksum[cnt]);
14219   putc ('\n', stderr);
14220 }
14221
14222 static void
14223 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
14224 {
14225   internal_error ("fold check: original tree changed by fold");
14226 }
14227
14228 static void
14229 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
14230 {
14231   const void **slot;
14232   enum tree_code code;
14233   union tree_node buf;
14234   int i, len;
14235
14236 recursive_label:
14237
14238   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
14239                <= sizeof (struct tree_function_decl))
14240               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
14241   if (expr == NULL)
14242     return;
14243   slot = (const void **) htab_find_slot (ht, expr, INSERT);
14244   if (*slot != NULL)
14245     return;
14246   *slot = expr;
14247   code = TREE_CODE (expr);
14248   if (TREE_CODE_CLASS (code) == tcc_declaration
14249       && DECL_ASSEMBLER_NAME_SET_P (expr))
14250     {
14251       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
14252       memcpy ((char *) &buf, expr, tree_size (expr));
14253       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
14254       expr = (tree) &buf;
14255     }
14256   else if (TREE_CODE_CLASS (code) == tcc_type
14257            && (TYPE_POINTER_TO (expr)
14258                || TYPE_REFERENCE_TO (expr)
14259                || TYPE_CACHED_VALUES_P (expr)
14260                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
14261                || TYPE_NEXT_VARIANT (expr)))
14262     {
14263       /* Allow these fields to be modified.  */
14264       tree tmp;
14265       memcpy ((char *) &buf, expr, tree_size (expr));
14266       expr = tmp = (tree) &buf;
14267       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
14268       TYPE_POINTER_TO (tmp) = NULL;
14269       TYPE_REFERENCE_TO (tmp) = NULL;
14270       TYPE_NEXT_VARIANT (tmp) = NULL;
14271       if (TYPE_CACHED_VALUES_P (tmp))
14272         {
14273           TYPE_CACHED_VALUES_P (tmp) = 0;
14274           TYPE_CACHED_VALUES (tmp) = NULL;
14275         }
14276     }
14277   md5_process_bytes (expr, tree_size (expr), ctx);
14278   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
14279   if (TREE_CODE_CLASS (code) != tcc_type
14280       && TREE_CODE_CLASS (code) != tcc_declaration
14281       && code != TREE_LIST
14282       && code != SSA_NAME)
14283     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
14284   switch (TREE_CODE_CLASS (code))
14285     {
14286     case tcc_constant:
14287       switch (code)
14288         {
14289         case STRING_CST:
14290           md5_process_bytes (TREE_STRING_POINTER (expr),
14291                              TREE_STRING_LENGTH (expr), ctx);
14292           break;
14293         case COMPLEX_CST:
14294           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
14295           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
14296           break;
14297         case VECTOR_CST:
14298           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
14299           break;
14300         default:
14301           break;
14302         }
14303       break;
14304     case tcc_exceptional:
14305       switch (code)
14306         {
14307         case TREE_LIST:
14308           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
14309           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
14310           expr = TREE_CHAIN (expr);
14311           goto recursive_label;
14312           break;
14313         case TREE_VEC:
14314           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
14315             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
14316           break;
14317         default:
14318           break;
14319         }
14320       break;
14321     case tcc_expression:
14322     case tcc_reference:
14323     case tcc_comparison:
14324     case tcc_unary:
14325     case tcc_binary:
14326     case tcc_statement:
14327     case tcc_vl_exp:
14328       len = TREE_OPERAND_LENGTH (expr);
14329       for (i = 0; i < len; ++i)
14330         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
14331       break;
14332     case tcc_declaration:
14333       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
14334       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
14335       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
14336         {
14337           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
14338           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
14339           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
14340           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
14341           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
14342         }
14343       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
14344         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
14345
14346       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
14347         {
14348           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
14349           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
14350           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
14351         }
14352       break;
14353     case tcc_type:
14354       if (TREE_CODE (expr) == ENUMERAL_TYPE)
14355         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
14356       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
14357       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
14358       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
14359       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
14360       if (INTEGRAL_TYPE_P (expr)
14361           || SCALAR_FLOAT_TYPE_P (expr))
14362         {
14363           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
14364           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
14365         }
14366       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
14367       if (TREE_CODE (expr) == RECORD_TYPE
14368           || TREE_CODE (expr) == UNION_TYPE
14369           || TREE_CODE (expr) == QUAL_UNION_TYPE)
14370         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
14371       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
14372       break;
14373     default:
14374       break;
14375     }
14376 }
14377
14378 /* Helper function for outputting the checksum of a tree T.  When
14379    debugging with gdb, you can "define mynext" to be "next" followed
14380    by "call debug_fold_checksum (op0)", then just trace down till the
14381    outputs differ.  */
14382
14383 void
14384 debug_fold_checksum (const_tree t)
14385 {
14386   int i;
14387   unsigned char checksum[16];
14388   struct md5_ctx ctx;
14389   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14390
14391   md5_init_ctx (&ctx);
14392   fold_checksum_tree (t, &ctx, ht);
14393   md5_finish_ctx (&ctx, checksum);
14394   htab_empty (ht);
14395
14396   for (i = 0; i < 16; i++)
14397     fprintf (stderr, "%d ", checksum[i]);
14398
14399   fprintf (stderr, "\n");
14400 }
14401
14402 #endif
14403
14404 /* Fold a unary tree expression with code CODE of type TYPE with an
14405    operand OP0.  LOC is the location of the resulting expression.
14406    Return a folded expression if successful.  Otherwise, return a tree
14407    expression with code CODE of type TYPE with an operand OP0.  */
14408
14409 tree
14410 fold_build1_stat_loc (location_t loc,
14411                       enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
14412 {
14413   tree tem;
14414 #ifdef ENABLE_FOLD_CHECKING
14415   unsigned char checksum_before[16], checksum_after[16];
14416   struct md5_ctx ctx;
14417   htab_t ht;
14418
14419   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14420   md5_init_ctx (&ctx);
14421   fold_checksum_tree (op0, &ctx, ht);
14422   md5_finish_ctx (&ctx, checksum_before);
14423   htab_empty (ht);
14424 #endif
14425
14426   tem = fold_unary_loc (loc, code, type, op0);
14427   if (!tem)
14428     {
14429       tem = build1_stat (code, type, op0 PASS_MEM_STAT);
14430       SET_EXPR_LOCATION (tem, loc);
14431     }
14432
14433 #ifdef ENABLE_FOLD_CHECKING
14434   md5_init_ctx (&ctx);
14435   fold_checksum_tree (op0, &ctx, ht);
14436   md5_finish_ctx (&ctx, checksum_after);
14437   htab_delete (ht);
14438
14439   if (memcmp (checksum_before, checksum_after, 16))
14440     fold_check_failed (op0, tem);
14441 #endif
14442   return tem;
14443 }
14444
14445 /* Fold a binary tree expression with code CODE of type TYPE with
14446    operands OP0 and OP1.  LOC is the location of the resulting
14447    expression.  Return a folded expression if successful.  Otherwise,
14448    return a tree expression with code CODE of type TYPE with operands
14449    OP0 and OP1.  */
14450
14451 tree
14452 fold_build2_stat_loc (location_t loc,
14453                       enum tree_code code, tree type, tree op0, tree op1
14454                       MEM_STAT_DECL)
14455 {
14456   tree tem;
14457 #ifdef ENABLE_FOLD_CHECKING
14458   unsigned char checksum_before_op0[16],
14459                 checksum_before_op1[16],
14460                 checksum_after_op0[16],
14461                 checksum_after_op1[16];
14462   struct md5_ctx ctx;
14463   htab_t ht;
14464
14465   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14466   md5_init_ctx (&ctx);
14467   fold_checksum_tree (op0, &ctx, ht);
14468   md5_finish_ctx (&ctx, checksum_before_op0);
14469   htab_empty (ht);
14470
14471   md5_init_ctx (&ctx);
14472   fold_checksum_tree (op1, &ctx, ht);
14473   md5_finish_ctx (&ctx, checksum_before_op1);
14474   htab_empty (ht);
14475 #endif
14476
14477   tem = fold_binary_loc (loc, code, type, op0, op1);
14478   if (!tem)
14479     {
14480       tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
14481       SET_EXPR_LOCATION (tem, loc);
14482     }
14483
14484 #ifdef ENABLE_FOLD_CHECKING
14485   md5_init_ctx (&ctx);
14486   fold_checksum_tree (op0, &ctx, ht);
14487   md5_finish_ctx (&ctx, checksum_after_op0);
14488   htab_empty (ht);
14489
14490   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14491     fold_check_failed (op0, tem);
14492
14493   md5_init_ctx (&ctx);
14494   fold_checksum_tree (op1, &ctx, ht);
14495   md5_finish_ctx (&ctx, checksum_after_op1);
14496   htab_delete (ht);
14497
14498   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14499     fold_check_failed (op1, tem);
14500 #endif
14501   return tem;
14502 }
14503
14504 /* Fold a ternary tree expression with code CODE of type TYPE with
14505    operands OP0, OP1, and OP2.  Return a folded expression if
14506    successful.  Otherwise, return a tree expression with code CODE of
14507    type TYPE with operands OP0, OP1, and OP2.  */
14508
14509 tree
14510 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14511                       tree op0, tree op1, tree op2 MEM_STAT_DECL)
14512 {
14513   tree tem;
14514 #ifdef ENABLE_FOLD_CHECKING
14515   unsigned char checksum_before_op0[16],
14516                 checksum_before_op1[16],
14517                 checksum_before_op2[16],
14518                 checksum_after_op0[16],
14519                 checksum_after_op1[16],
14520                 checksum_after_op2[16];
14521   struct md5_ctx ctx;
14522   htab_t ht;
14523
14524   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14525   md5_init_ctx (&ctx);
14526   fold_checksum_tree (op0, &ctx, ht);
14527   md5_finish_ctx (&ctx, checksum_before_op0);
14528   htab_empty (ht);
14529
14530   md5_init_ctx (&ctx);
14531   fold_checksum_tree (op1, &ctx, ht);
14532   md5_finish_ctx (&ctx, checksum_before_op1);
14533   htab_empty (ht);
14534
14535   md5_init_ctx (&ctx);
14536   fold_checksum_tree (op2, &ctx, ht);
14537   md5_finish_ctx (&ctx, checksum_before_op2);
14538   htab_empty (ht);
14539 #endif
14540
14541   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14542   tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14543   if (!tem)
14544     {
14545       tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
14546       SET_EXPR_LOCATION (tem, loc);
14547     }
14548
14549 #ifdef ENABLE_FOLD_CHECKING
14550   md5_init_ctx (&ctx);
14551   fold_checksum_tree (op0, &ctx, ht);
14552   md5_finish_ctx (&ctx, checksum_after_op0);
14553   htab_empty (ht);
14554
14555   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14556     fold_check_failed (op0, tem);
14557
14558   md5_init_ctx (&ctx);
14559   fold_checksum_tree (op1, &ctx, ht);
14560   md5_finish_ctx (&ctx, checksum_after_op1);
14561   htab_empty (ht);
14562
14563   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14564     fold_check_failed (op1, tem);
14565
14566   md5_init_ctx (&ctx);
14567   fold_checksum_tree (op2, &ctx, ht);
14568   md5_finish_ctx (&ctx, checksum_after_op2);
14569   htab_delete (ht);
14570
14571   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14572     fold_check_failed (op2, tem);
14573 #endif
14574   return tem;
14575 }
14576
14577 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14578    arguments in ARGARRAY, and a null static chain.
14579    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14580    of type TYPE from the given operands as constructed by build_call_array.  */
14581
14582 tree
14583 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14584                            int nargs, tree *argarray)
14585 {
14586   tree tem;
14587 #ifdef ENABLE_FOLD_CHECKING
14588   unsigned char checksum_before_fn[16],
14589                 checksum_before_arglist[16],
14590                 checksum_after_fn[16],
14591                 checksum_after_arglist[16];
14592   struct md5_ctx ctx;
14593   htab_t ht;
14594   int i;
14595
14596   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14597   md5_init_ctx (&ctx);
14598   fold_checksum_tree (fn, &ctx, ht);
14599   md5_finish_ctx (&ctx, checksum_before_fn);
14600   htab_empty (ht);
14601
14602   md5_init_ctx (&ctx);
14603   for (i = 0; i < nargs; i++)
14604     fold_checksum_tree (argarray[i], &ctx, ht);
14605   md5_finish_ctx (&ctx, checksum_before_arglist);
14606   htab_empty (ht);
14607 #endif
14608
14609   tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14610
14611 #ifdef ENABLE_FOLD_CHECKING
14612   md5_init_ctx (&ctx);
14613   fold_checksum_tree (fn, &ctx, ht);
14614   md5_finish_ctx (&ctx, checksum_after_fn);
14615   htab_empty (ht);
14616
14617   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14618     fold_check_failed (fn, tem);
14619
14620   md5_init_ctx (&ctx);
14621   for (i = 0; i < nargs; i++)
14622     fold_checksum_tree (argarray[i], &ctx, ht);
14623   md5_finish_ctx (&ctx, checksum_after_arglist);
14624   htab_delete (ht);
14625
14626   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14627     fold_check_failed (NULL_TREE, tem);
14628 #endif
14629   return tem;
14630 }
14631
14632 /* Perform constant folding and related simplification of initializer
14633    expression EXPR.  These behave identically to "fold_buildN" but ignore
14634    potential run-time traps and exceptions that fold must preserve.  */
14635
14636 #define START_FOLD_INIT \
14637   int saved_signaling_nans = flag_signaling_nans;\
14638   int saved_trapping_math = flag_trapping_math;\
14639   int saved_rounding_math = flag_rounding_math;\
14640   int saved_trapv = flag_trapv;\
14641   int saved_folding_initializer = folding_initializer;\
14642   flag_signaling_nans = 0;\
14643   flag_trapping_math = 0;\
14644   flag_rounding_math = 0;\
14645   flag_trapv = 0;\
14646   folding_initializer = 1;
14647
14648 #define END_FOLD_INIT \
14649   flag_signaling_nans = saved_signaling_nans;\
14650   flag_trapping_math = saved_trapping_math;\
14651   flag_rounding_math = saved_rounding_math;\
14652   flag_trapv = saved_trapv;\
14653   folding_initializer = saved_folding_initializer;
14654
14655 tree
14656 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14657                              tree type, tree op)
14658 {
14659   tree result;
14660   START_FOLD_INIT;
14661
14662   result = fold_build1_loc (loc, code, type, op);
14663
14664   END_FOLD_INIT;
14665   return result;
14666 }
14667
14668 tree
14669 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14670                              tree type, tree op0, tree op1)
14671 {
14672   tree result;
14673   START_FOLD_INIT;
14674
14675   result = fold_build2_loc (loc, code, type, op0, op1);
14676
14677   END_FOLD_INIT;
14678   return result;
14679 }
14680
14681 tree
14682 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14683                              tree type, tree op0, tree op1, tree op2)
14684 {
14685   tree result;
14686   START_FOLD_INIT;
14687
14688   result = fold_build3_loc (loc, code, type, op0, op1, op2);
14689
14690   END_FOLD_INIT;
14691   return result;
14692 }
14693
14694 tree
14695 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14696                                        int nargs, tree *argarray)
14697 {
14698   tree result;
14699   START_FOLD_INIT;
14700
14701   result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14702
14703   END_FOLD_INIT;
14704   return result;
14705 }
14706
14707 #undef START_FOLD_INIT
14708 #undef END_FOLD_INIT
14709
14710 /* Determine if first argument is a multiple of second argument.  Return 0 if
14711    it is not, or we cannot easily determined it to be.
14712
14713    An example of the sort of thing we care about (at this point; this routine
14714    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14715    fold cases do now) is discovering that
14716
14717      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14718
14719    is a multiple of
14720
14721      SAVE_EXPR (J * 8)
14722
14723    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14724
14725    This code also handles discovering that
14726
14727      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14728
14729    is a multiple of 8 so we don't have to worry about dealing with a
14730    possible remainder.
14731
14732    Note that we *look* inside a SAVE_EXPR only to determine how it was
14733    calculated; it is not safe for fold to do much of anything else with the
14734    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14735    at run time.  For example, the latter example above *cannot* be implemented
14736    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14737    evaluation time of the original SAVE_EXPR is not necessarily the same at
14738    the time the new expression is evaluated.  The only optimization of this
14739    sort that would be valid is changing
14740
14741      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14742
14743    divided by 8 to
14744
14745      SAVE_EXPR (I) * SAVE_EXPR (J)
14746
14747    (where the same SAVE_EXPR (J) is used in the original and the
14748    transformed version).  */
14749
14750 int
14751 multiple_of_p (tree type, const_tree top, const_tree bottom)
14752 {
14753   if (operand_equal_p (top, bottom, 0))
14754     return 1;
14755
14756   if (TREE_CODE (type) != INTEGER_TYPE)
14757     return 0;
14758
14759   switch (TREE_CODE (top))
14760     {
14761     case BIT_AND_EXPR:
14762       /* Bitwise and provides a power of two multiple.  If the mask is
14763          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14764       if (!integer_pow2p (bottom))
14765         return 0;
14766       /* FALLTHRU */
14767
14768     case MULT_EXPR:
14769       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14770               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14771
14772     case PLUS_EXPR:
14773     case MINUS_EXPR:
14774       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14775               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14776
14777     case LSHIFT_EXPR:
14778       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14779         {
14780           tree op1, t1;
14781
14782           op1 = TREE_OPERAND (top, 1);
14783           /* const_binop may not detect overflow correctly,
14784              so check for it explicitly here.  */
14785           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14786               > TREE_INT_CST_LOW (op1)
14787               && TREE_INT_CST_HIGH (op1) == 0
14788               && 0 != (t1 = fold_convert (type,
14789                                           const_binop (LSHIFT_EXPR,
14790                                                        size_one_node,
14791                                                        op1, 0)))
14792               && !TREE_OVERFLOW (t1))
14793             return multiple_of_p (type, t1, bottom);
14794         }
14795       return 0;
14796
14797     case NOP_EXPR:
14798       /* Can't handle conversions from non-integral or wider integral type.  */
14799       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14800           || (TYPE_PRECISION (type)
14801               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14802         return 0;
14803
14804       /* .. fall through ...  */
14805
14806     case SAVE_EXPR:
14807       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14808
14809     case INTEGER_CST:
14810       if (TREE_CODE (bottom) != INTEGER_CST
14811           || integer_zerop (bottom)
14812           || (TYPE_UNSIGNED (type)
14813               && (tree_int_cst_sgn (top) < 0
14814                   || tree_int_cst_sgn (bottom) < 0)))
14815         return 0;
14816       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14817                                              top, bottom, 0));
14818
14819     default:
14820       return 0;
14821     }
14822 }
14823
14824 /* Return true if CODE or TYPE is known to be non-negative. */
14825
14826 static bool
14827 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14828 {
14829   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14830       && truth_value_p (code))
14831     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14832        have a signed:1 type (where the value is -1 and 0).  */
14833     return true;
14834   return false;
14835 }
14836
14837 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14838    value is based on the assumption that signed overflow is undefined,
14839    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14840    *STRICT_OVERFLOW_P.  */
14841
14842 bool
14843 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14844                                 bool *strict_overflow_p)
14845 {
14846   if (TYPE_UNSIGNED (type))
14847     return true;
14848
14849   switch (code)
14850     {
14851     case ABS_EXPR:
14852       /* We can't return 1 if flag_wrapv is set because
14853          ABS_EXPR<INT_MIN> = INT_MIN.  */
14854       if (!INTEGRAL_TYPE_P (type))
14855         return true;
14856       if (TYPE_OVERFLOW_UNDEFINED (type))
14857         {
14858           *strict_overflow_p = true;
14859           return true;
14860         }
14861       break;
14862
14863     case NON_LVALUE_EXPR:
14864     case FLOAT_EXPR:
14865     case FIX_TRUNC_EXPR:
14866       return tree_expr_nonnegative_warnv_p (op0,
14867                                             strict_overflow_p);
14868
14869     case NOP_EXPR:
14870       {
14871         tree inner_type = TREE_TYPE (op0);
14872         tree outer_type = type;
14873
14874         if (TREE_CODE (outer_type) == REAL_TYPE)
14875           {
14876             if (TREE_CODE (inner_type) == REAL_TYPE)
14877               return tree_expr_nonnegative_warnv_p (op0,
14878                                                     strict_overflow_p);
14879             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14880               {
14881                 if (TYPE_UNSIGNED (inner_type))
14882                   return true;
14883                 return tree_expr_nonnegative_warnv_p (op0,
14884                                                       strict_overflow_p);
14885               }
14886           }
14887         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14888           {
14889             if (TREE_CODE (inner_type) == REAL_TYPE)
14890               return tree_expr_nonnegative_warnv_p (op0,
14891                                                     strict_overflow_p);
14892             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14893               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14894                       && TYPE_UNSIGNED (inner_type);
14895           }
14896       }
14897       break;
14898
14899     default:
14900       return tree_simple_nonnegative_warnv_p (code, type);
14901     }
14902
14903   /* We don't know sign of `t', so be conservative and return false.  */
14904   return false;
14905 }
14906
14907 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14908    value is based on the assumption that signed overflow is undefined,
14909    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14910    *STRICT_OVERFLOW_P.  */
14911
14912 bool
14913 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14914                                       tree op1, bool *strict_overflow_p)
14915 {
14916   if (TYPE_UNSIGNED (type))
14917     return true;
14918
14919   switch (code)
14920     {
14921     case POINTER_PLUS_EXPR:
14922     case PLUS_EXPR:
14923       if (FLOAT_TYPE_P (type))
14924         return (tree_expr_nonnegative_warnv_p (op0,
14925                                                strict_overflow_p)
14926                 && tree_expr_nonnegative_warnv_p (op1,
14927                                                   strict_overflow_p));
14928
14929       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14930          both unsigned and at least 2 bits shorter than the result.  */
14931       if (TREE_CODE (type) == INTEGER_TYPE
14932           && TREE_CODE (op0) == NOP_EXPR
14933           && TREE_CODE (op1) == NOP_EXPR)
14934         {
14935           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14936           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14937           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14938               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14939             {
14940               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14941                                        TYPE_PRECISION (inner2)) + 1;
14942               return prec < TYPE_PRECISION (type);
14943             }
14944         }
14945       break;
14946
14947     case MULT_EXPR:
14948       if (FLOAT_TYPE_P (type))
14949         {
14950           /* x * x for floating point x is always non-negative.  */
14951           if (operand_equal_p (op0, op1, 0))
14952             return true;
14953           return (tree_expr_nonnegative_warnv_p (op0,
14954                                                  strict_overflow_p)
14955                   && tree_expr_nonnegative_warnv_p (op1,
14956                                                     strict_overflow_p));
14957         }
14958
14959       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14960          both unsigned and their total bits is shorter than the result.  */
14961       if (TREE_CODE (type) == INTEGER_TYPE
14962           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14963           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14964         {
14965           tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14966             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14967             : TREE_TYPE (op0);
14968           tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14969             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14970             : TREE_TYPE (op1);
14971
14972           bool unsigned0 = TYPE_UNSIGNED (inner0);
14973           bool unsigned1 = TYPE_UNSIGNED (inner1);
14974
14975           if (TREE_CODE (op0) == INTEGER_CST)
14976             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14977
14978           if (TREE_CODE (op1) == INTEGER_CST)
14979             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14980
14981           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14982               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14983             {
14984               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14985                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14986                 : TYPE_PRECISION (inner0);
14987
14988               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14989                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14990                 : TYPE_PRECISION (inner1);
14991
14992               return precision0 + precision1 < TYPE_PRECISION (type);
14993             }
14994         }
14995       return false;
14996
14997     case BIT_AND_EXPR:
14998     case MAX_EXPR:
14999       return (tree_expr_nonnegative_warnv_p (op0,
15000                                              strict_overflow_p)
15001               || tree_expr_nonnegative_warnv_p (op1,
15002                                                 strict_overflow_p));
15003
15004     case BIT_IOR_EXPR:
15005     case BIT_XOR_EXPR:
15006     case MIN_EXPR:
15007     case RDIV_EXPR:
15008     case TRUNC_DIV_EXPR:
15009     case CEIL_DIV_EXPR:
15010     case FLOOR_DIV_EXPR:
15011     case ROUND_DIV_EXPR:
15012       return (tree_expr_nonnegative_warnv_p (op0,
15013                                              strict_overflow_p)
15014               && tree_expr_nonnegative_warnv_p (op1,
15015                                                 strict_overflow_p));
15016
15017     case TRUNC_MOD_EXPR:
15018     case CEIL_MOD_EXPR:
15019     case FLOOR_MOD_EXPR:
15020     case ROUND_MOD_EXPR:
15021       return tree_expr_nonnegative_warnv_p (op0,
15022                                             strict_overflow_p);
15023     default:
15024       return tree_simple_nonnegative_warnv_p (code, type);
15025     }
15026
15027   /* We don't know sign of `t', so be conservative and return false.  */
15028   return false;
15029 }
15030
15031 /* Return true if T is known to be non-negative.  If the return
15032    value is based on the assumption that signed overflow is undefined,
15033    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15034    *STRICT_OVERFLOW_P.  */
15035
15036 bool
15037 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15038 {
15039   if (TYPE_UNSIGNED (TREE_TYPE (t)))
15040     return true;
15041
15042   switch (TREE_CODE (t))
15043     {
15044     case INTEGER_CST:
15045       return tree_int_cst_sgn (t) >= 0;
15046
15047     case REAL_CST:
15048       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
15049
15050     case FIXED_CST:
15051       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
15052
15053     case COND_EXPR:
15054       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15055                                              strict_overflow_p)
15056               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
15057                                                 strict_overflow_p));
15058     default:
15059       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15060                                                    TREE_TYPE (t));
15061     }
15062   /* We don't know sign of `t', so be conservative and return false.  */
15063   return false;
15064 }
15065
15066 /* Return true if T is known to be non-negative.  If the return
15067    value is based on the assumption that signed overflow is undefined,
15068    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15069    *STRICT_OVERFLOW_P.  */
15070
15071 bool
15072 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
15073                                tree arg0, tree arg1, bool *strict_overflow_p)
15074 {
15075   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
15076     switch (DECL_FUNCTION_CODE (fndecl))
15077       {
15078         CASE_FLT_FN (BUILT_IN_ACOS):
15079         CASE_FLT_FN (BUILT_IN_ACOSH):
15080         CASE_FLT_FN (BUILT_IN_CABS):
15081         CASE_FLT_FN (BUILT_IN_COSH):
15082         CASE_FLT_FN (BUILT_IN_ERFC):
15083         CASE_FLT_FN (BUILT_IN_EXP):
15084         CASE_FLT_FN (BUILT_IN_EXP10):
15085         CASE_FLT_FN (BUILT_IN_EXP2):
15086         CASE_FLT_FN (BUILT_IN_FABS):
15087         CASE_FLT_FN (BUILT_IN_FDIM):
15088         CASE_FLT_FN (BUILT_IN_HYPOT):
15089         CASE_FLT_FN (BUILT_IN_POW10):
15090         CASE_INT_FN (BUILT_IN_FFS):
15091         CASE_INT_FN (BUILT_IN_PARITY):
15092         CASE_INT_FN (BUILT_IN_POPCOUNT):
15093       case BUILT_IN_BSWAP32:
15094       case BUILT_IN_BSWAP64:
15095         /* Always true.  */
15096         return true;
15097
15098         CASE_FLT_FN (BUILT_IN_SQRT):
15099         /* sqrt(-0.0) is -0.0.  */
15100         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
15101           return true;
15102         return tree_expr_nonnegative_warnv_p (arg0,
15103                                               strict_overflow_p);
15104
15105         CASE_FLT_FN (BUILT_IN_ASINH):
15106         CASE_FLT_FN (BUILT_IN_ATAN):
15107         CASE_FLT_FN (BUILT_IN_ATANH):
15108         CASE_FLT_FN (BUILT_IN_CBRT):
15109         CASE_FLT_FN (BUILT_IN_CEIL):
15110         CASE_FLT_FN (BUILT_IN_ERF):
15111         CASE_FLT_FN (BUILT_IN_EXPM1):
15112         CASE_FLT_FN (BUILT_IN_FLOOR):
15113         CASE_FLT_FN (BUILT_IN_FMOD):
15114         CASE_FLT_FN (BUILT_IN_FREXP):
15115         CASE_FLT_FN (BUILT_IN_LCEIL):
15116         CASE_FLT_FN (BUILT_IN_LDEXP):
15117         CASE_FLT_FN (BUILT_IN_LFLOOR):
15118         CASE_FLT_FN (BUILT_IN_LLCEIL):
15119         CASE_FLT_FN (BUILT_IN_LLFLOOR):
15120         CASE_FLT_FN (BUILT_IN_LLRINT):
15121         CASE_FLT_FN (BUILT_IN_LLROUND):
15122         CASE_FLT_FN (BUILT_IN_LRINT):
15123         CASE_FLT_FN (BUILT_IN_LROUND):
15124         CASE_FLT_FN (BUILT_IN_MODF):
15125         CASE_FLT_FN (BUILT_IN_NEARBYINT):
15126         CASE_FLT_FN (BUILT_IN_RINT):
15127         CASE_FLT_FN (BUILT_IN_ROUND):
15128         CASE_FLT_FN (BUILT_IN_SCALB):
15129         CASE_FLT_FN (BUILT_IN_SCALBLN):
15130         CASE_FLT_FN (BUILT_IN_SCALBN):
15131         CASE_FLT_FN (BUILT_IN_SIGNBIT):
15132         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
15133         CASE_FLT_FN (BUILT_IN_SINH):
15134         CASE_FLT_FN (BUILT_IN_TANH):
15135         CASE_FLT_FN (BUILT_IN_TRUNC):
15136         /* True if the 1st argument is nonnegative.  */
15137         return tree_expr_nonnegative_warnv_p (arg0,
15138                                               strict_overflow_p);
15139
15140         CASE_FLT_FN (BUILT_IN_FMAX):
15141         /* True if the 1st OR 2nd arguments are nonnegative.  */
15142         return (tree_expr_nonnegative_warnv_p (arg0,
15143                                                strict_overflow_p)
15144                 || (tree_expr_nonnegative_warnv_p (arg1,
15145                                                    strict_overflow_p)));
15146
15147         CASE_FLT_FN (BUILT_IN_FMIN):
15148         /* True if the 1st AND 2nd arguments are nonnegative.  */
15149         return (tree_expr_nonnegative_warnv_p (arg0,
15150                                                strict_overflow_p)
15151                 && (tree_expr_nonnegative_warnv_p (arg1,
15152                                                    strict_overflow_p)));
15153
15154         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15155         /* True if the 2nd argument is nonnegative.  */
15156         return tree_expr_nonnegative_warnv_p (arg1,
15157                                               strict_overflow_p);
15158
15159         CASE_FLT_FN (BUILT_IN_POWI):
15160         /* True if the 1st argument is nonnegative or the second
15161            argument is an even integer.  */
15162         if (TREE_CODE (arg1) == INTEGER_CST
15163             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15164           return true;
15165         return tree_expr_nonnegative_warnv_p (arg0,
15166                                               strict_overflow_p);
15167
15168         CASE_FLT_FN (BUILT_IN_POW):
15169         /* True if the 1st argument is nonnegative or the second
15170            argument is an even integer valued real.  */
15171         if (TREE_CODE (arg1) == REAL_CST)
15172           {
15173             REAL_VALUE_TYPE c;
15174             HOST_WIDE_INT n;
15175
15176             c = TREE_REAL_CST (arg1);
15177             n = real_to_integer (&c);
15178             if ((n & 1) == 0)
15179               {
15180                 REAL_VALUE_TYPE cint;
15181                 real_from_integer (&cint, VOIDmode, n,
15182                                    n < 0 ? -1 : 0, 0);
15183                 if (real_identical (&c, &cint))
15184                   return true;
15185               }
15186           }
15187         return tree_expr_nonnegative_warnv_p (arg0,
15188                                               strict_overflow_p);
15189
15190       default:
15191         break;
15192       }
15193   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
15194                                           type);
15195 }
15196
15197 /* Return true if T is known to be non-negative.  If the return
15198    value is based on the assumption that signed overflow is undefined,
15199    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15200    *STRICT_OVERFLOW_P.  */
15201
15202 bool
15203 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15204 {
15205   enum tree_code code = TREE_CODE (t);
15206   if (TYPE_UNSIGNED (TREE_TYPE (t)))
15207     return true;
15208
15209   switch (code)
15210     {
15211     case TARGET_EXPR:
15212       {
15213         tree temp = TARGET_EXPR_SLOT (t);
15214         t = TARGET_EXPR_INITIAL (t);
15215
15216         /* If the initializer is non-void, then it's a normal expression
15217            that will be assigned to the slot.  */
15218         if (!VOID_TYPE_P (t))
15219           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
15220
15221         /* Otherwise, the initializer sets the slot in some way.  One common
15222            way is an assignment statement at the end of the initializer.  */
15223         while (1)
15224           {
15225             if (TREE_CODE (t) == BIND_EXPR)
15226               t = expr_last (BIND_EXPR_BODY (t));
15227             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15228                      || TREE_CODE (t) == TRY_CATCH_EXPR)
15229               t = expr_last (TREE_OPERAND (t, 0));
15230             else if (TREE_CODE (t) == STATEMENT_LIST)
15231               t = expr_last (t);
15232             else
15233               break;
15234           }
15235         if (TREE_CODE (t) == MODIFY_EXPR
15236             && TREE_OPERAND (t, 0) == temp)
15237           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15238                                                 strict_overflow_p);
15239
15240         return false;
15241       }
15242
15243     case CALL_EXPR:
15244       {
15245         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
15246         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
15247
15248         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15249                                               get_callee_fndecl (t),
15250                                               arg0,
15251                                               arg1,
15252                                               strict_overflow_p);
15253       }
15254     case COMPOUND_EXPR:
15255     case MODIFY_EXPR:
15256       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15257                                             strict_overflow_p);
15258     case BIND_EXPR:
15259       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
15260                                             strict_overflow_p);
15261     case SAVE_EXPR:
15262       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
15263                                             strict_overflow_p);
15264
15265     default:
15266       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15267                                                    TREE_TYPE (t));
15268     }
15269
15270   /* We don't know sign of `t', so be conservative and return false.  */
15271   return false;
15272 }
15273
15274 /* Return true if T is known to be non-negative.  If the return
15275    value is based on the assumption that signed overflow is undefined,
15276    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15277    *STRICT_OVERFLOW_P.  */
15278
15279 bool
15280 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15281 {
15282   enum tree_code code;
15283   if (t == error_mark_node)
15284     return false;
15285
15286   code = TREE_CODE (t);
15287   switch (TREE_CODE_CLASS (code))
15288     {
15289     case tcc_binary:
15290     case tcc_comparison:
15291       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15292                                               TREE_TYPE (t),
15293                                               TREE_OPERAND (t, 0),
15294                                               TREE_OPERAND (t, 1),
15295                                               strict_overflow_p);
15296
15297     case tcc_unary:
15298       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15299                                              TREE_TYPE (t),
15300                                              TREE_OPERAND (t, 0),
15301                                              strict_overflow_p);
15302
15303     case tcc_constant:
15304     case tcc_declaration:
15305     case tcc_reference:
15306       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15307
15308     default:
15309       break;
15310     }
15311
15312   switch (code)
15313     {
15314     case TRUTH_AND_EXPR:
15315     case TRUTH_OR_EXPR:
15316     case TRUTH_XOR_EXPR:
15317       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15318                                               TREE_TYPE (t),
15319                                               TREE_OPERAND (t, 0),
15320                                               TREE_OPERAND (t, 1),
15321                                               strict_overflow_p);
15322     case TRUTH_NOT_EXPR:
15323       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15324                                              TREE_TYPE (t),
15325                                              TREE_OPERAND (t, 0),
15326                                              strict_overflow_p);
15327
15328     case COND_EXPR:
15329     case CONSTRUCTOR:
15330     case OBJ_TYPE_REF:
15331     case ASSERT_EXPR:
15332     case ADDR_EXPR:
15333     case WITH_SIZE_EXPR:
15334     case SSA_NAME:
15335       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15336
15337     default:
15338       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
15339     }
15340 }
15341
15342 /* Return true if `t' is known to be non-negative.  Handle warnings
15343    about undefined signed overflow.  */
15344
15345 bool
15346 tree_expr_nonnegative_p (tree t)
15347 {
15348   bool ret, strict_overflow_p;
15349
15350   strict_overflow_p = false;
15351   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15352   if (strict_overflow_p)
15353     fold_overflow_warning (("assuming signed overflow does not occur when "
15354                             "determining that expression is always "
15355                             "non-negative"),
15356                            WARN_STRICT_OVERFLOW_MISC);
15357   return ret;
15358 }
15359
15360
15361 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15362    For floating point we further ensure that T is not denormal.
15363    Similar logic is present in nonzero_address in rtlanal.h.
15364
15365    If the return value is based on the assumption that signed overflow
15366    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15367    change *STRICT_OVERFLOW_P.  */
15368
15369 bool
15370 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15371                                  bool *strict_overflow_p)
15372 {
15373   switch (code)
15374     {
15375     case ABS_EXPR:
15376       return tree_expr_nonzero_warnv_p (op0,
15377                                         strict_overflow_p);
15378
15379     case NOP_EXPR:
15380       {
15381         tree inner_type = TREE_TYPE (op0);
15382         tree outer_type = type;
15383
15384         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15385                 && tree_expr_nonzero_warnv_p (op0,
15386                                               strict_overflow_p));
15387       }
15388       break;
15389
15390     case NON_LVALUE_EXPR:
15391       return tree_expr_nonzero_warnv_p (op0,
15392                                         strict_overflow_p);
15393
15394     default:
15395       break;
15396   }
15397
15398   return false;
15399 }
15400
15401 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15402    For floating point we further ensure that T is not denormal.
15403    Similar logic is present in nonzero_address in rtlanal.h.
15404
15405    If the return value is based on the assumption that signed overflow
15406    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15407    change *STRICT_OVERFLOW_P.  */
15408
15409 bool
15410 tree_binary_nonzero_warnv_p (enum tree_code code,
15411                              tree type,
15412                              tree op0,
15413                              tree op1, bool *strict_overflow_p)
15414 {
15415   bool sub_strict_overflow_p;
15416   switch (code)
15417     {
15418     case POINTER_PLUS_EXPR:
15419     case PLUS_EXPR:
15420       if (TYPE_OVERFLOW_UNDEFINED (type))
15421         {
15422           /* With the presence of negative values it is hard
15423              to say something.  */
15424           sub_strict_overflow_p = false;
15425           if (!tree_expr_nonnegative_warnv_p (op0,
15426                                               &sub_strict_overflow_p)
15427               || !tree_expr_nonnegative_warnv_p (op1,
15428                                                  &sub_strict_overflow_p))
15429             return false;
15430           /* One of operands must be positive and the other non-negative.  */
15431           /* We don't set *STRICT_OVERFLOW_P here: even if this value
15432              overflows, on a twos-complement machine the sum of two
15433              nonnegative numbers can never be zero.  */
15434           return (tree_expr_nonzero_warnv_p (op0,
15435                                              strict_overflow_p)
15436                   || tree_expr_nonzero_warnv_p (op1,
15437                                                 strict_overflow_p));
15438         }
15439       break;
15440
15441     case MULT_EXPR:
15442       if (TYPE_OVERFLOW_UNDEFINED (type))
15443         {
15444           if (tree_expr_nonzero_warnv_p (op0,
15445                                          strict_overflow_p)
15446               && tree_expr_nonzero_warnv_p (op1,
15447                                             strict_overflow_p))
15448             {
15449               *strict_overflow_p = true;
15450               return true;
15451             }
15452         }
15453       break;
15454
15455     case MIN_EXPR:
15456       sub_strict_overflow_p = false;
15457       if (tree_expr_nonzero_warnv_p (op0,
15458                                      &sub_strict_overflow_p)
15459           && tree_expr_nonzero_warnv_p (op1,
15460                                         &sub_strict_overflow_p))
15461         {
15462           if (sub_strict_overflow_p)
15463             *strict_overflow_p = true;
15464         }
15465       break;
15466
15467     case MAX_EXPR:
15468       sub_strict_overflow_p = false;
15469       if (tree_expr_nonzero_warnv_p (op0,
15470                                      &sub_strict_overflow_p))
15471         {
15472           if (sub_strict_overflow_p)
15473             *strict_overflow_p = true;
15474
15475           /* When both operands are nonzero, then MAX must be too.  */
15476           if (tree_expr_nonzero_warnv_p (op1,
15477                                          strict_overflow_p))
15478             return true;
15479
15480           /* MAX where operand 0 is positive is positive.  */
15481           return tree_expr_nonnegative_warnv_p (op0,
15482                                                strict_overflow_p);
15483         }
15484       /* MAX where operand 1 is positive is positive.  */
15485       else if (tree_expr_nonzero_warnv_p (op1,
15486                                           &sub_strict_overflow_p)
15487                && tree_expr_nonnegative_warnv_p (op1,
15488                                                  &sub_strict_overflow_p))
15489         {
15490           if (sub_strict_overflow_p)
15491             *strict_overflow_p = true;
15492           return true;
15493         }
15494       break;
15495
15496     case BIT_IOR_EXPR:
15497       return (tree_expr_nonzero_warnv_p (op1,
15498                                          strict_overflow_p)
15499               || tree_expr_nonzero_warnv_p (op0,
15500                                             strict_overflow_p));
15501
15502     default:
15503       break;
15504   }
15505
15506   return false;
15507 }
15508
15509 /* Return true when T is an address and is known to be nonzero.
15510    For floating point we further ensure that T is not denormal.
15511    Similar logic is present in nonzero_address in rtlanal.h.
15512
15513    If the return value is based on the assumption that signed overflow
15514    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15515    change *STRICT_OVERFLOW_P.  */
15516
15517 bool
15518 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15519 {
15520   bool sub_strict_overflow_p;
15521   switch (TREE_CODE (t))
15522     {
15523     case INTEGER_CST:
15524       return !integer_zerop (t);
15525
15526     case ADDR_EXPR:
15527       {
15528         tree base = get_base_address (TREE_OPERAND (t, 0));
15529
15530         if (!base)
15531           return false;
15532
15533         /* Weak declarations may link to NULL.  Other things may also be NULL
15534            so protect with -fdelete-null-pointer-checks; but not variables
15535            allocated on the stack.  */
15536         if (DECL_P (base)
15537             && (flag_delete_null_pointer_checks
15538                 || (TREE_CODE (base) == VAR_DECL && !TREE_STATIC (base))))
15539           return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15540
15541         /* Constants are never weak.  */
15542         if (CONSTANT_CLASS_P (base))
15543           return true;
15544
15545         return false;
15546       }
15547
15548     case COND_EXPR:
15549       sub_strict_overflow_p = false;
15550       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15551                                      &sub_strict_overflow_p)
15552           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15553                                         &sub_strict_overflow_p))
15554         {
15555           if (sub_strict_overflow_p)
15556             *strict_overflow_p = true;
15557           return true;
15558         }
15559       break;
15560
15561     default:
15562       break;
15563     }
15564   return false;
15565 }
15566
15567 /* Return true when T is an address and is known to be nonzero.
15568    For floating point we further ensure that T is not denormal.
15569    Similar logic is present in nonzero_address in rtlanal.h.
15570
15571    If the return value is based on the assumption that signed overflow
15572    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15573    change *STRICT_OVERFLOW_P.  */
15574
15575 bool
15576 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15577 {
15578   tree type = TREE_TYPE (t);
15579   enum tree_code code;
15580
15581   /* Doing something useful for floating point would need more work.  */
15582   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15583     return false;
15584
15585   code = TREE_CODE (t);
15586   switch (TREE_CODE_CLASS (code))
15587     {
15588     case tcc_unary:
15589       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15590                                               strict_overflow_p);
15591     case tcc_binary:
15592     case tcc_comparison:
15593       return tree_binary_nonzero_warnv_p (code, type,
15594                                                TREE_OPERAND (t, 0),
15595                                                TREE_OPERAND (t, 1),
15596                                                strict_overflow_p);
15597     case tcc_constant:
15598     case tcc_declaration:
15599     case tcc_reference:
15600       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15601
15602     default:
15603       break;
15604     }
15605
15606   switch (code)
15607     {
15608     case TRUTH_NOT_EXPR:
15609       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15610                                               strict_overflow_p);
15611
15612     case TRUTH_AND_EXPR:
15613     case TRUTH_OR_EXPR:
15614     case TRUTH_XOR_EXPR:
15615       return tree_binary_nonzero_warnv_p (code, type,
15616                                                TREE_OPERAND (t, 0),
15617                                                TREE_OPERAND (t, 1),
15618                                                strict_overflow_p);
15619
15620     case COND_EXPR:
15621     case CONSTRUCTOR:
15622     case OBJ_TYPE_REF:
15623     case ASSERT_EXPR:
15624     case ADDR_EXPR:
15625     case WITH_SIZE_EXPR:
15626     case SSA_NAME:
15627       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15628
15629     case COMPOUND_EXPR:
15630     case MODIFY_EXPR:
15631     case BIND_EXPR:
15632       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15633                                         strict_overflow_p);
15634
15635     case SAVE_EXPR:
15636       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15637                                         strict_overflow_p);
15638
15639     case CALL_EXPR:
15640       return alloca_call_p (t);
15641
15642     default:
15643       break;
15644     }
15645   return false;
15646 }
15647
15648 /* Return true when T is an address and is known to be nonzero.
15649    Handle warnings about undefined signed overflow.  */
15650
15651 bool
15652 tree_expr_nonzero_p (tree t)
15653 {
15654   bool ret, strict_overflow_p;
15655
15656   strict_overflow_p = false;
15657   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15658   if (strict_overflow_p)
15659     fold_overflow_warning (("assuming signed overflow does not occur when "
15660                             "determining that expression is always "
15661                             "non-zero"),
15662                            WARN_STRICT_OVERFLOW_MISC);
15663   return ret;
15664 }
15665
15666 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15667    attempt to fold the expression to a constant without modifying TYPE,
15668    OP0 or OP1.
15669
15670    If the expression could be simplified to a constant, then return
15671    the constant.  If the expression would not be simplified to a
15672    constant, then return NULL_TREE.  */
15673
15674 tree
15675 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15676 {
15677   tree tem = fold_binary (code, type, op0, op1);
15678   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15679 }
15680
15681 /* Given the components of a unary expression CODE, TYPE and OP0,
15682    attempt to fold the expression to a constant without modifying
15683    TYPE or OP0.
15684
15685    If the expression could be simplified to a constant, then return
15686    the constant.  If the expression would not be simplified to a
15687    constant, then return NULL_TREE.  */
15688
15689 tree
15690 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15691 {
15692   tree tem = fold_unary (code, type, op0);
15693   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15694 }
15695
15696 /* If EXP represents referencing an element in a constant string
15697    (either via pointer arithmetic or array indexing), return the
15698    tree representing the value accessed, otherwise return NULL.  */
15699
15700 tree
15701 fold_read_from_constant_string (tree exp)
15702 {
15703   if ((TREE_CODE (exp) == INDIRECT_REF
15704        || TREE_CODE (exp) == ARRAY_REF)
15705       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15706     {
15707       tree exp1 = TREE_OPERAND (exp, 0);
15708       tree index;
15709       tree string;
15710       location_t loc = EXPR_LOCATION (exp);
15711
15712       if (TREE_CODE (exp) == INDIRECT_REF)
15713         string = string_constant (exp1, &index);
15714       else
15715         {
15716           tree low_bound = array_ref_low_bound (exp);
15717           index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15718
15719           /* Optimize the special-case of a zero lower bound.
15720
15721              We convert the low_bound to sizetype to avoid some problems
15722              with constant folding.  (E.g. suppose the lower bound is 1,
15723              and its mode is QI.  Without the conversion,l (ARRAY
15724              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15725              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15726           if (! integer_zerop (low_bound))
15727             index = size_diffop_loc (loc, index,
15728                                  fold_convert_loc (loc, sizetype, low_bound));
15729
15730           string = exp1;
15731         }
15732
15733       if (string
15734           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15735           && TREE_CODE (string) == STRING_CST
15736           && TREE_CODE (index) == INTEGER_CST
15737           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15738           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15739               == MODE_INT)
15740           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15741         return build_int_cst_type (TREE_TYPE (exp),
15742                                    (TREE_STRING_POINTER (string)
15743                                     [TREE_INT_CST_LOW (index)]));
15744     }
15745   return NULL;
15746 }
15747
15748 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15749    an integer constant, real, or fixed-point constant.
15750
15751    TYPE is the type of the result.  */
15752
15753 static tree
15754 fold_negate_const (tree arg0, tree type)
15755 {
15756   tree t = NULL_TREE;
15757
15758   switch (TREE_CODE (arg0))
15759     {
15760     case INTEGER_CST:
15761       {
15762         unsigned HOST_WIDE_INT low;
15763         HOST_WIDE_INT high;
15764         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15765                                    TREE_INT_CST_HIGH (arg0),
15766                                    &low, &high);
15767         t = force_fit_type_double (type, low, high, 1,
15768                                    (overflow | TREE_OVERFLOW (arg0))
15769                                    && !TYPE_UNSIGNED (type));
15770         break;
15771       }
15772
15773     case REAL_CST:
15774       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15775       break;
15776
15777     case FIXED_CST:
15778       {
15779         FIXED_VALUE_TYPE f;
15780         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15781                                             &(TREE_FIXED_CST (arg0)), NULL,
15782                                             TYPE_SATURATING (type));
15783         t = build_fixed (type, f);
15784         /* Propagate overflow flags.  */
15785         if (overflow_p | TREE_OVERFLOW (arg0))
15786           TREE_OVERFLOW (t) = 1;
15787         break;
15788       }
15789
15790     default:
15791       gcc_unreachable ();
15792     }
15793
15794   return t;
15795 }
15796
15797 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15798    an integer constant or real constant.
15799
15800    TYPE is the type of the result.  */
15801
15802 tree
15803 fold_abs_const (tree arg0, tree type)
15804 {
15805   tree t = NULL_TREE;
15806
15807   switch (TREE_CODE (arg0))
15808     {
15809     case INTEGER_CST:
15810       /* If the value is unsigned, then the absolute value is
15811          the same as the ordinary value.  */
15812       if (TYPE_UNSIGNED (type))
15813         t = arg0;
15814       /* Similarly, if the value is non-negative.  */
15815       else if (INT_CST_LT (integer_minus_one_node, arg0))
15816         t = arg0;
15817       /* If the value is negative, then the absolute value is
15818          its negation.  */
15819       else
15820         {
15821           unsigned HOST_WIDE_INT low;
15822           HOST_WIDE_INT high;
15823           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15824                                      TREE_INT_CST_HIGH (arg0),
15825                                      &low, &high);
15826           t = force_fit_type_double (type, low, high, -1,
15827                                      overflow | TREE_OVERFLOW (arg0));
15828         }
15829       break;
15830
15831     case REAL_CST:
15832       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15833         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15834       else
15835         t =  arg0;
15836       break;
15837
15838     default:
15839       gcc_unreachable ();
15840     }
15841
15842   return t;
15843 }
15844
15845 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15846    constant.  TYPE is the type of the result.  */
15847
15848 static tree
15849 fold_not_const (tree arg0, tree type)
15850 {
15851   tree t = NULL_TREE;
15852
15853   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15854
15855   t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15856                              ~TREE_INT_CST_HIGH (arg0), 0,
15857                              TREE_OVERFLOW (arg0));
15858
15859   return t;
15860 }
15861
15862 /* Given CODE, a relational operator, the target type, TYPE and two
15863    constant operands OP0 and OP1, return the result of the
15864    relational operation.  If the result is not a compile time
15865    constant, then return NULL_TREE.  */
15866
15867 static tree
15868 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15869 {
15870   int result, invert;
15871
15872   /* From here on, the only cases we handle are when the result is
15873      known to be a constant.  */
15874
15875   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15876     {
15877       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15878       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15879
15880       /* Handle the cases where either operand is a NaN.  */
15881       if (real_isnan (c0) || real_isnan (c1))
15882         {
15883           switch (code)
15884             {
15885             case EQ_EXPR:
15886             case ORDERED_EXPR:
15887               result = 0;
15888               break;
15889
15890             case NE_EXPR:
15891             case UNORDERED_EXPR:
15892             case UNLT_EXPR:
15893             case UNLE_EXPR:
15894             case UNGT_EXPR:
15895             case UNGE_EXPR:
15896             case UNEQ_EXPR:
15897               result = 1;
15898               break;
15899
15900             case LT_EXPR:
15901             case LE_EXPR:
15902             case GT_EXPR:
15903             case GE_EXPR:
15904             case LTGT_EXPR:
15905               if (flag_trapping_math)
15906                 return NULL_TREE;
15907               result = 0;
15908               break;
15909
15910             default:
15911               gcc_unreachable ();
15912             }
15913
15914           return constant_boolean_node (result, type);
15915         }
15916
15917       return constant_boolean_node (real_compare (code, c0, c1), type);
15918     }
15919
15920   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15921     {
15922       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15923       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15924       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15925     }
15926
15927   /* Handle equality/inequality of complex constants.  */
15928   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15929     {
15930       tree rcond = fold_relational_const (code, type,
15931                                           TREE_REALPART (op0),
15932                                           TREE_REALPART (op1));
15933       tree icond = fold_relational_const (code, type,
15934                                           TREE_IMAGPART (op0),
15935                                           TREE_IMAGPART (op1));
15936       if (code == EQ_EXPR)
15937         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15938       else if (code == NE_EXPR)
15939         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15940       else
15941         return NULL_TREE;
15942     }
15943
15944   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15945
15946      To compute GT, swap the arguments and do LT.
15947      To compute GE, do LT and invert the result.
15948      To compute LE, swap the arguments, do LT and invert the result.
15949      To compute NE, do EQ and invert the result.
15950
15951      Therefore, the code below must handle only EQ and LT.  */
15952
15953   if (code == LE_EXPR || code == GT_EXPR)
15954     {
15955       tree tem = op0;
15956       op0 = op1;
15957       op1 = tem;
15958       code = swap_tree_comparison (code);
15959     }
15960
15961   /* Note that it is safe to invert for real values here because we
15962      have already handled the one case that it matters.  */
15963
15964   invert = 0;
15965   if (code == NE_EXPR || code == GE_EXPR)
15966     {
15967       invert = 1;
15968       code = invert_tree_comparison (code, false);
15969     }
15970
15971   /* Compute a result for LT or EQ if args permit;
15972      Otherwise return T.  */
15973   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15974     {
15975       if (code == EQ_EXPR)
15976         result = tree_int_cst_equal (op0, op1);
15977       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15978         result = INT_CST_LT_UNSIGNED (op0, op1);
15979       else
15980         result = INT_CST_LT (op0, op1);
15981     }
15982   else
15983     return NULL_TREE;
15984
15985   if (invert)
15986     result ^= 1;
15987   return constant_boolean_node (result, type);
15988 }
15989
15990 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15991    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15992    itself.  */
15993
15994 tree
15995 fold_build_cleanup_point_expr (tree type, tree expr)
15996 {
15997   /* If the expression does not have side effects then we don't have to wrap
15998      it with a cleanup point expression.  */
15999   if (!TREE_SIDE_EFFECTS (expr))
16000     return expr;
16001
16002   /* If the expression is a return, check to see if the expression inside the
16003      return has no side effects or the right hand side of the modify expression
16004      inside the return. If either don't have side effects set we don't need to
16005      wrap the expression in a cleanup point expression.  Note we don't check the
16006      left hand side of the modify because it should always be a return decl.  */
16007   if (TREE_CODE (expr) == RETURN_EXPR)
16008     {
16009       tree op = TREE_OPERAND (expr, 0);
16010       if (!op || !TREE_SIDE_EFFECTS (op))
16011         return expr;
16012       op = TREE_OPERAND (op, 1);
16013       if (!TREE_SIDE_EFFECTS (op))
16014         return expr;
16015     }
16016
16017   return build1 (CLEANUP_POINT_EXPR, type, expr);
16018 }
16019
16020 /* Given a pointer value OP0 and a type TYPE, return a simplified version
16021    of an indirection through OP0, or NULL_TREE if no simplification is
16022    possible.  */
16023
16024 tree
16025 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16026 {
16027   tree sub = op0;
16028   tree subtype;
16029
16030   STRIP_NOPS (sub);
16031   subtype = TREE_TYPE (sub);
16032   if (!POINTER_TYPE_P (subtype))
16033     return NULL_TREE;
16034
16035   if (TREE_CODE (sub) == ADDR_EXPR)
16036     {
16037       tree op = TREE_OPERAND (sub, 0);
16038       tree optype = TREE_TYPE (op);
16039       /* *&CONST_DECL -> to the value of the const decl.  */
16040       if (TREE_CODE (op) == CONST_DECL)
16041         return DECL_INITIAL (op);
16042       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
16043       if (type == optype)
16044         {
16045           tree fop = fold_read_from_constant_string (op);
16046           if (fop)
16047             return fop;
16048           else
16049             return op;
16050         }
16051       /* *(foo *)&fooarray => fooarray[0] */
16052       else if (TREE_CODE (optype) == ARRAY_TYPE
16053                && type == TREE_TYPE (optype))
16054         {
16055           tree type_domain = TYPE_DOMAIN (optype);
16056           tree min_val = size_zero_node;
16057           if (type_domain && TYPE_MIN_VALUE (type_domain))
16058             min_val = TYPE_MIN_VALUE (type_domain);
16059           op0 = build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
16060           SET_EXPR_LOCATION (op0, loc);
16061           return op0;
16062         }
16063       /* *(foo *)&complexfoo => __real__ complexfoo */
16064       else if (TREE_CODE (optype) == COMPLEX_TYPE
16065                && type == TREE_TYPE (optype))
16066         return fold_build1_loc (loc, REALPART_EXPR, type, op);
16067       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16068       else if (TREE_CODE (optype) == VECTOR_TYPE
16069                && type == TREE_TYPE (optype))
16070         {
16071           tree part_width = TYPE_SIZE (type);
16072           tree index = bitsize_int (0);
16073           return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
16074         }
16075     }
16076
16077   /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16078   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16079       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
16080     {
16081       tree op00 = TREE_OPERAND (sub, 0);
16082       tree op01 = TREE_OPERAND (sub, 1);
16083       tree op00type;
16084
16085       STRIP_NOPS (op00);
16086       op00type = TREE_TYPE (op00);
16087       if (TREE_CODE (op00) == ADDR_EXPR
16088           && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
16089           && type == TREE_TYPE (TREE_TYPE (op00type)))
16090         {
16091           HOST_WIDE_INT offset = tree_low_cst (op01, 0);
16092           tree part_width = TYPE_SIZE (type);
16093           unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
16094           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
16095           tree index = bitsize_int (indexi);
16096
16097           if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
16098             return fold_build3_loc (loc,
16099                                 BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
16100                                 part_width, index);
16101
16102         }
16103     }
16104
16105
16106   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16107   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16108       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
16109     {
16110       tree op00 = TREE_OPERAND (sub, 0);
16111       tree op01 = TREE_OPERAND (sub, 1);
16112       tree op00type;
16113
16114       STRIP_NOPS (op00);
16115       op00type = TREE_TYPE (op00);
16116       if (TREE_CODE (op00) == ADDR_EXPR
16117           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
16118           && type == TREE_TYPE (TREE_TYPE (op00type)))
16119         {
16120           tree size = TYPE_SIZE_UNIT (type);
16121           if (tree_int_cst_equal (size, op01))
16122             return fold_build1_loc (loc, IMAGPART_EXPR, type,
16123                                 TREE_OPERAND (op00, 0));
16124         }
16125     }
16126
16127   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16128   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16129       && type == TREE_TYPE (TREE_TYPE (subtype)))
16130     {
16131       tree type_domain;
16132       tree min_val = size_zero_node;
16133       sub = build_fold_indirect_ref_loc (loc, sub);
16134       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16135       if (type_domain && TYPE_MIN_VALUE (type_domain))
16136         min_val = TYPE_MIN_VALUE (type_domain);
16137       op0 = build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
16138       SET_EXPR_LOCATION (op0, loc);
16139       return op0;
16140     }
16141
16142   return NULL_TREE;
16143 }
16144
16145 /* Builds an expression for an indirection through T, simplifying some
16146    cases.  */
16147
16148 tree
16149 build_fold_indirect_ref_loc (location_t loc, tree t)
16150 {
16151   tree type = TREE_TYPE (TREE_TYPE (t));
16152   tree sub = fold_indirect_ref_1 (loc, type, t);
16153
16154   if (sub)
16155     return sub;
16156
16157   t = build1 (INDIRECT_REF, type, t);
16158   SET_EXPR_LOCATION (t, loc);
16159   return t;
16160 }
16161
16162 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
16163
16164 tree
16165 fold_indirect_ref_loc (location_t loc, tree t)
16166 {
16167   tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16168
16169   if (sub)
16170     return sub;
16171   else
16172     return t;
16173 }
16174
16175 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16176    whose result is ignored.  The type of the returned tree need not be
16177    the same as the original expression.  */
16178
16179 tree
16180 fold_ignored_result (tree t)
16181 {
16182   if (!TREE_SIDE_EFFECTS (t))
16183     return integer_zero_node;
16184
16185   for (;;)
16186     switch (TREE_CODE_CLASS (TREE_CODE (t)))
16187       {
16188       case tcc_unary:
16189         t = TREE_OPERAND (t, 0);
16190         break;
16191
16192       case tcc_binary:
16193       case tcc_comparison:
16194         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16195           t = TREE_OPERAND (t, 0);
16196         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16197           t = TREE_OPERAND (t, 1);
16198         else
16199           return t;
16200         break;
16201
16202       case tcc_expression:
16203         switch (TREE_CODE (t))
16204           {
16205           case COMPOUND_EXPR:
16206             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16207               return t;
16208             t = TREE_OPERAND (t, 0);
16209             break;
16210
16211           case COND_EXPR:
16212             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16213                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16214               return t;
16215             t = TREE_OPERAND (t, 0);
16216             break;
16217
16218           default:
16219             return t;
16220           }
16221         break;
16222
16223       default:
16224         return t;
16225       }
16226 }
16227
16228 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
16229    This can only be applied to objects of a sizetype.  */
16230
16231 tree
16232 round_up_loc (location_t loc, tree value, int divisor)
16233 {
16234   tree div = NULL_TREE;
16235
16236   gcc_assert (divisor > 0);
16237   if (divisor == 1)
16238     return value;
16239
16240   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
16241      have to do anything.  Only do this when we are not given a const,
16242      because in that case, this check is more expensive than just
16243      doing it.  */
16244   if (TREE_CODE (value) != INTEGER_CST)
16245     {
16246       div = build_int_cst (TREE_TYPE (value), divisor);
16247
16248       if (multiple_of_p (TREE_TYPE (value), value, div))
16249         return value;
16250     }
16251
16252   /* If divisor is a power of two, simplify this to bit manipulation.  */
16253   if (divisor == (divisor & -divisor))
16254     {
16255       if (TREE_CODE (value) == INTEGER_CST)
16256         {
16257           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
16258           unsigned HOST_WIDE_INT high;
16259           bool overflow_p;
16260
16261           if ((low & (divisor - 1)) == 0)
16262             return value;
16263
16264           overflow_p = TREE_OVERFLOW (value);
16265           high = TREE_INT_CST_HIGH (value);
16266           low &= ~(divisor - 1);
16267           low += divisor;
16268           if (low == 0)
16269             {
16270               high++;
16271               if (high == 0)
16272                 overflow_p = true;
16273             }
16274
16275           return force_fit_type_double (TREE_TYPE (value), low, high,
16276                                         -1, overflow_p);
16277         }
16278       else
16279         {
16280           tree t;
16281
16282           t = build_int_cst (TREE_TYPE (value), divisor - 1);
16283           value = size_binop_loc (loc, PLUS_EXPR, value, t);
16284           t = build_int_cst (TREE_TYPE (value), -divisor);
16285           value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16286         }
16287     }
16288   else
16289     {
16290       if (!div)
16291         div = build_int_cst (TREE_TYPE (value), divisor);
16292       value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16293       value = size_binop_loc (loc, MULT_EXPR, value, div);
16294     }
16295
16296   return value;
16297 }
16298
16299 /* Likewise, but round down.  */
16300
16301 tree
16302 round_down_loc (location_t loc, tree value, int divisor)
16303 {
16304   tree div = NULL_TREE;
16305
16306   gcc_assert (divisor > 0);
16307   if (divisor == 1)
16308     return value;
16309
16310   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
16311      have to do anything.  Only do this when we are not given a const,
16312      because in that case, this check is more expensive than just
16313      doing it.  */
16314   if (TREE_CODE (value) != INTEGER_CST)
16315     {
16316       div = build_int_cst (TREE_TYPE (value), divisor);
16317
16318       if (multiple_of_p (TREE_TYPE (value), value, div))
16319         return value;
16320     }
16321
16322   /* If divisor is a power of two, simplify this to bit manipulation.  */
16323   if (divisor == (divisor & -divisor))
16324     {
16325       tree t;
16326
16327       t = build_int_cst (TREE_TYPE (value), -divisor);
16328       value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16329     }
16330   else
16331     {
16332       if (!div)
16333         div = build_int_cst (TREE_TYPE (value), divisor);
16334       value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16335       value = size_binop_loc (loc, MULT_EXPR, value, div);
16336     }
16337
16338   return value;
16339 }
16340
16341 /* Returns the pointer to the base of the object addressed by EXP and
16342    extracts the information about the offset of the access, storing it
16343    to PBITPOS and POFFSET.  */
16344
16345 static tree
16346 split_address_to_core_and_offset (tree exp,
16347                                   HOST_WIDE_INT *pbitpos, tree *poffset)
16348 {
16349   tree core;
16350   enum machine_mode mode;
16351   int unsignedp, volatilep;
16352   HOST_WIDE_INT bitsize;
16353   location_t loc = EXPR_LOCATION (exp);
16354
16355   if (TREE_CODE (exp) == ADDR_EXPR)
16356     {
16357       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16358                                   poffset, &mode, &unsignedp, &volatilep,
16359                                   false);
16360       core = build_fold_addr_expr_loc (loc, core);
16361     }
16362   else
16363     {
16364       core = exp;
16365       *pbitpos = 0;
16366       *poffset = NULL_TREE;
16367     }
16368
16369   return core;
16370 }
16371
16372 /* Returns true if addresses of E1 and E2 differ by a constant, false
16373    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
16374
16375 bool
16376 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
16377 {
16378   tree core1, core2;
16379   HOST_WIDE_INT bitpos1, bitpos2;
16380   tree toffset1, toffset2, tdiff, type;
16381
16382   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16383   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16384
16385   if (bitpos1 % BITS_PER_UNIT != 0
16386       || bitpos2 % BITS_PER_UNIT != 0
16387       || !operand_equal_p (core1, core2, 0))
16388     return false;
16389
16390   if (toffset1 && toffset2)
16391     {
16392       type = TREE_TYPE (toffset1);
16393       if (type != TREE_TYPE (toffset2))
16394         toffset2 = fold_convert (type, toffset2);
16395
16396       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16397       if (!cst_and_fits_in_hwi (tdiff))
16398         return false;
16399
16400       *diff = int_cst_value (tdiff);
16401     }
16402   else if (toffset1 || toffset2)
16403     {
16404       /* If only one of the offsets is non-constant, the difference cannot
16405          be a constant.  */
16406       return false;
16407     }
16408   else
16409     *diff = 0;
16410
16411   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
16412   return true;
16413 }
16414
16415 /* Simplify the floating point expression EXP when the sign of the
16416    result is not significant.  Return NULL_TREE if no simplification
16417    is possible.  */
16418
16419 tree
16420 fold_strip_sign_ops (tree exp)
16421 {
16422   tree arg0, arg1;
16423   location_t loc = EXPR_LOCATION (exp);
16424
16425   switch (TREE_CODE (exp))
16426     {
16427     case ABS_EXPR:
16428     case NEGATE_EXPR:
16429       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16430       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
16431
16432     case MULT_EXPR:
16433     case RDIV_EXPR:
16434       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
16435         return NULL_TREE;
16436       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16437       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16438       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
16439         return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
16440                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
16441                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
16442       break;
16443
16444     case COMPOUND_EXPR:
16445       arg0 = TREE_OPERAND (exp, 0);
16446       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16447       if (arg1)
16448         return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16449       break;
16450
16451     case COND_EXPR:
16452       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16453       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16454       if (arg0 || arg1)
16455         return fold_build3_loc (loc,
16456                             COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16457                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
16458                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
16459       break;
16460
16461     case CALL_EXPR:
16462       {
16463         const enum built_in_function fcode = builtin_mathfn_code (exp);
16464         switch (fcode)
16465         {
16466         CASE_FLT_FN (BUILT_IN_COPYSIGN):
16467           /* Strip copysign function call, return the 1st argument. */
16468           arg0 = CALL_EXPR_ARG (exp, 0);
16469           arg1 = CALL_EXPR_ARG (exp, 1);
16470           return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16471
16472         default:
16473           /* Strip sign ops from the argument of "odd" math functions.  */
16474           if (negate_mathfn_p (fcode))
16475             {
16476               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16477               if (arg0)
16478                 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
16479             }
16480           break;
16481         }
16482       }
16483       break;
16484
16485     default:
16486       break;
16487     }
16488   return NULL_TREE;
16489 }