OSDN Git Service

* exp_pakd.adb (Create_Packed_Array_Type): Always use a modular type
[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, 2010
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 NEGATE_EXPR:
1133       return true;
1134
1135     case REAL_CST:
1136       /* We want to canonicalize to positive real constants.  Pretend
1137          that only negative ones can be easily negated.  */
1138       return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
1139
1140     case COMPLEX_CST:
1141       return negate_expr_p (TREE_REALPART (t))
1142              && negate_expr_p (TREE_IMAGPART (t));
1143
1144     case COMPLEX_EXPR:
1145       return negate_expr_p (TREE_OPERAND (t, 0))
1146              && negate_expr_p (TREE_OPERAND (t, 1));
1147
1148     case CONJ_EXPR:
1149       return negate_expr_p (TREE_OPERAND (t, 0));
1150
1151     case PLUS_EXPR:
1152       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1153           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1154         return false;
1155       /* -(A + B) -> (-B) - A.  */
1156       if (negate_expr_p (TREE_OPERAND (t, 1))
1157           && reorder_operands_p (TREE_OPERAND (t, 0),
1158                                  TREE_OPERAND (t, 1)))
1159         return true;
1160       /* -(A + B) -> (-A) - B.  */
1161       return negate_expr_p (TREE_OPERAND (t, 0));
1162
1163     case MINUS_EXPR:
1164       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
1165       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1166              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1167              && reorder_operands_p (TREE_OPERAND (t, 0),
1168                                     TREE_OPERAND (t, 1));
1169
1170     case MULT_EXPR:
1171       if (TYPE_UNSIGNED (TREE_TYPE (t)))
1172         break;
1173
1174       /* Fall through.  */
1175
1176     case RDIV_EXPR:
1177       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1178         return negate_expr_p (TREE_OPERAND (t, 1))
1179                || negate_expr_p (TREE_OPERAND (t, 0));
1180       break;
1181
1182     case TRUNC_DIV_EXPR:
1183     case ROUND_DIV_EXPR:
1184     case FLOOR_DIV_EXPR:
1185     case CEIL_DIV_EXPR:
1186     case EXACT_DIV_EXPR:
1187       /* In general we can't negate A / B, because if A is INT_MIN and
1188          B is 1, we may turn this into INT_MIN / -1 which is undefined
1189          and actually traps on some architectures.  But if overflow is
1190          undefined, we can negate, because - (INT_MIN / 1) is an
1191          overflow.  */
1192       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1193           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1194         break;
1195       return negate_expr_p (TREE_OPERAND (t, 1))
1196              || negate_expr_p (TREE_OPERAND (t, 0));
1197
1198     case NOP_EXPR:
1199       /* Negate -((double)float) as (double)(-float).  */
1200       if (TREE_CODE (type) == REAL_TYPE)
1201         {
1202           tree tem = strip_float_extensions (t);
1203           if (tem != t)
1204             return negate_expr_p (tem);
1205         }
1206       break;
1207
1208     case CALL_EXPR:
1209       /* Negate -f(x) as f(-x).  */
1210       if (negate_mathfn_p (builtin_mathfn_code (t)))
1211         return negate_expr_p (CALL_EXPR_ARG (t, 0));
1212       break;
1213
1214     case RSHIFT_EXPR:
1215       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1216       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1217         {
1218           tree op1 = TREE_OPERAND (t, 1);
1219           if (TREE_INT_CST_HIGH (op1) == 0
1220               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1221                  == TREE_INT_CST_LOW (op1))
1222             return true;
1223         }
1224       break;
1225
1226     default:
1227       break;
1228     }
1229   return false;
1230 }
1231
1232 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1233    simplification is possible.
1234    If negate_expr_p would return true for T, NULL_TREE will never be
1235    returned.  */
1236
1237 static tree
1238 fold_negate_expr (location_t loc, tree t)
1239 {
1240   tree type = TREE_TYPE (t);
1241   tree tem;
1242
1243   switch (TREE_CODE (t))
1244     {
1245     /* Convert - (~A) to A + 1.  */
1246     case BIT_NOT_EXPR:
1247       if (INTEGRAL_TYPE_P (type))
1248         return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
1249                             build_int_cst (type, 1));
1250       break;
1251
1252     case INTEGER_CST:
1253       tem = fold_negate_const (t, type);
1254       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1255           || !TYPE_OVERFLOW_TRAPS (type))
1256         return tem;
1257       break;
1258
1259     case REAL_CST:
1260       tem = fold_negate_const (t, type);
1261       /* Two's complement FP formats, such as c4x, may overflow.  */
1262       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1263         return tem;
1264       break;
1265
1266     case FIXED_CST:
1267       tem = fold_negate_const (t, type);
1268       return tem;
1269
1270     case COMPLEX_CST:
1271       {
1272         tree rpart = negate_expr (TREE_REALPART (t));
1273         tree ipart = negate_expr (TREE_IMAGPART (t));
1274
1275         if ((TREE_CODE (rpart) == REAL_CST
1276              && TREE_CODE (ipart) == REAL_CST)
1277             || (TREE_CODE (rpart) == INTEGER_CST
1278                 && TREE_CODE (ipart) == INTEGER_CST))
1279           return build_complex (type, rpart, ipart);
1280       }
1281       break;
1282
1283     case COMPLEX_EXPR:
1284       if (negate_expr_p (t))
1285         return fold_build2_loc (loc, COMPLEX_EXPR, type,
1286                             fold_negate_expr (loc, TREE_OPERAND (t, 0)),
1287                             fold_negate_expr (loc, TREE_OPERAND (t, 1)));
1288       break;
1289
1290     case CONJ_EXPR:
1291       if (negate_expr_p (t))
1292         return fold_build1_loc (loc, CONJ_EXPR, type,
1293                             fold_negate_expr (loc, TREE_OPERAND (t, 0)));
1294       break;
1295
1296     case NEGATE_EXPR:
1297       return TREE_OPERAND (t, 0);
1298
1299     case PLUS_EXPR:
1300       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1301           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1302         {
1303           /* -(A + B) -> (-B) - A.  */
1304           if (negate_expr_p (TREE_OPERAND (t, 1))
1305               && reorder_operands_p (TREE_OPERAND (t, 0),
1306                                      TREE_OPERAND (t, 1)))
1307             {
1308               tem = negate_expr (TREE_OPERAND (t, 1));
1309               return fold_build2_loc (loc, MINUS_EXPR, type,
1310                                   tem, TREE_OPERAND (t, 0));
1311             }
1312
1313           /* -(A + B) -> (-A) - B.  */
1314           if (negate_expr_p (TREE_OPERAND (t, 0)))
1315             {
1316               tem = negate_expr (TREE_OPERAND (t, 0));
1317               return fold_build2_loc (loc, MINUS_EXPR, type,
1318                                   tem, TREE_OPERAND (t, 1));
1319             }
1320         }
1321       break;
1322
1323     case MINUS_EXPR:
1324       /* - (A - B) -> B - A  */
1325       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1326           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1327           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1328         return fold_build2_loc (loc, MINUS_EXPR, type,
1329                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1330       break;
1331
1332     case MULT_EXPR:
1333       if (TYPE_UNSIGNED (type))
1334         break;
1335
1336       /* Fall through.  */
1337
1338     case RDIV_EXPR:
1339       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1340         {
1341           tem = TREE_OPERAND (t, 1);
1342           if (negate_expr_p (tem))
1343             return fold_build2_loc (loc, TREE_CODE (t), type,
1344                                 TREE_OPERAND (t, 0), negate_expr (tem));
1345           tem = TREE_OPERAND (t, 0);
1346           if (negate_expr_p (tem))
1347             return fold_build2_loc (loc, TREE_CODE (t), type,
1348                                 negate_expr (tem), TREE_OPERAND (t, 1));
1349         }
1350       break;
1351
1352     case TRUNC_DIV_EXPR:
1353     case ROUND_DIV_EXPR:
1354     case FLOOR_DIV_EXPR:
1355     case CEIL_DIV_EXPR:
1356     case EXACT_DIV_EXPR:
1357       /* In general we can't negate A / B, because if A is INT_MIN and
1358          B is 1, we may turn this into INT_MIN / -1 which is undefined
1359          and actually traps on some architectures.  But if overflow is
1360          undefined, we can negate, because - (INT_MIN / 1) is an
1361          overflow.  */
1362       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1363         {
1364           const char * const warnmsg = G_("assuming signed overflow does not "
1365                                           "occur when negating a division");
1366           tem = TREE_OPERAND (t, 1);
1367           if (negate_expr_p (tem))
1368             {
1369               if (INTEGRAL_TYPE_P (type)
1370                   && (TREE_CODE (tem) != INTEGER_CST
1371                       || integer_onep (tem)))
1372                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1373               return fold_build2_loc (loc, TREE_CODE (t), type,
1374                                   TREE_OPERAND (t, 0), negate_expr (tem));
1375             }
1376           tem = TREE_OPERAND (t, 0);
1377           if (negate_expr_p (tem))
1378             {
1379               if (INTEGRAL_TYPE_P (type)
1380                   && (TREE_CODE (tem) != INTEGER_CST
1381                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1382                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1383               return fold_build2_loc (loc, TREE_CODE (t), type,
1384                                   negate_expr (tem), TREE_OPERAND (t, 1));
1385             }
1386         }
1387       break;
1388
1389     case NOP_EXPR:
1390       /* Convert -((double)float) into (double)(-float).  */
1391       if (TREE_CODE (type) == REAL_TYPE)
1392         {
1393           tem = strip_float_extensions (t);
1394           if (tem != t && negate_expr_p (tem))
1395             return fold_convert_loc (loc, type, negate_expr (tem));
1396         }
1397       break;
1398
1399     case CALL_EXPR:
1400       /* Negate -f(x) as f(-x).  */
1401       if (negate_mathfn_p (builtin_mathfn_code (t))
1402           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1403         {
1404           tree fndecl, arg;
1405
1406           fndecl = get_callee_fndecl (t);
1407           arg = negate_expr (CALL_EXPR_ARG (t, 0));
1408           return build_call_expr_loc (loc, fndecl, 1, arg);
1409         }
1410       break;
1411
1412     case RSHIFT_EXPR:
1413       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1414       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1415         {
1416           tree op1 = TREE_OPERAND (t, 1);
1417           if (TREE_INT_CST_HIGH (op1) == 0
1418               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1419                  == TREE_INT_CST_LOW (op1))
1420             {
1421               tree ntype = TYPE_UNSIGNED (type)
1422                            ? signed_type_for (type)
1423                            : unsigned_type_for (type);
1424               tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
1425               temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
1426               return fold_convert_loc (loc, type, temp);
1427             }
1428         }
1429       break;
1430
1431     default:
1432       break;
1433     }
1434
1435   return NULL_TREE;
1436 }
1437
1438 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1439    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
1440    return NULL_TREE. */
1441
1442 static tree
1443 negate_expr (tree t)
1444 {
1445   tree type, tem;
1446   location_t loc;
1447
1448   if (t == NULL_TREE)
1449     return NULL_TREE;
1450
1451   loc = EXPR_LOCATION (t);
1452   type = TREE_TYPE (t);
1453   STRIP_SIGN_NOPS (t);
1454
1455   tem = fold_negate_expr (loc, t);
1456   if (!tem)
1457     {
1458       tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1459       SET_EXPR_LOCATION (tem, loc);
1460     }
1461   return fold_convert_loc (loc, type, tem);
1462 }
1463 \f
1464 /* Split a tree IN into a constant, literal and variable parts that could be
1465    combined with CODE to make IN.  "constant" means an expression with
1466    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1467    commutative arithmetic operation.  Store the constant part into *CONP,
1468    the literal in *LITP and return the variable part.  If a part isn't
1469    present, set it to null.  If the tree does not decompose in this way,
1470    return the entire tree as the variable part and the other parts as null.
1471
1472    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1473    case, we negate an operand that was subtracted.  Except if it is a
1474    literal for which we use *MINUS_LITP instead.
1475
1476    If NEGATE_P is true, we are negating all of IN, again except a literal
1477    for which we use *MINUS_LITP instead.
1478
1479    If IN is itself a literal or constant, return it as appropriate.
1480
1481    Note that we do not guarantee that any of the three values will be the
1482    same type as IN, but they will have the same signedness and mode.  */
1483
1484 static tree
1485 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1486             tree *minus_litp, int negate_p)
1487 {
1488   tree var = 0;
1489
1490   *conp = 0;
1491   *litp = 0;
1492   *minus_litp = 0;
1493
1494   /* Strip any conversions that don't change the machine mode or signedness.  */
1495   STRIP_SIGN_NOPS (in);
1496
1497   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1498       || TREE_CODE (in) == FIXED_CST)
1499     *litp = in;
1500   else if (TREE_CODE (in) == code
1501            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1502                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1503                /* We can associate addition and subtraction together (even
1504                   though the C standard doesn't say so) for integers because
1505                   the value is not affected.  For reals, the value might be
1506                   affected, so we can't.  */
1507                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1508                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1509     {
1510       tree op0 = TREE_OPERAND (in, 0);
1511       tree op1 = TREE_OPERAND (in, 1);
1512       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1513       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1514
1515       /* First see if either of the operands is a literal, then a constant.  */
1516       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1517           || TREE_CODE (op0) == FIXED_CST)
1518         *litp = op0, op0 = 0;
1519       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1520                || TREE_CODE (op1) == FIXED_CST)
1521         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1522
1523       if (op0 != 0 && TREE_CONSTANT (op0))
1524         *conp = op0, op0 = 0;
1525       else if (op1 != 0 && TREE_CONSTANT (op1))
1526         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1527
1528       /* If we haven't dealt with either operand, this is not a case we can
1529          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1530       if (op0 != 0 && op1 != 0)
1531         var = in;
1532       else if (op0 != 0)
1533         var = op0;
1534       else
1535         var = op1, neg_var_p = neg1_p;
1536
1537       /* Now do any needed negations.  */
1538       if (neg_litp_p)
1539         *minus_litp = *litp, *litp = 0;
1540       if (neg_conp_p)
1541         *conp = negate_expr (*conp);
1542       if (neg_var_p)
1543         var = negate_expr (var);
1544     }
1545   else if (TREE_CONSTANT (in))
1546     *conp = in;
1547   else
1548     var = in;
1549
1550   if (negate_p)
1551     {
1552       if (*litp)
1553         *minus_litp = *litp, *litp = 0;
1554       else if (*minus_litp)
1555         *litp = *minus_litp, *minus_litp = 0;
1556       *conp = negate_expr (*conp);
1557       var = negate_expr (var);
1558     }
1559
1560   return var;
1561 }
1562
1563 /* Re-associate trees split by the above function.  T1 and T2 are
1564    either expressions to associate or null.  Return the new
1565    expression, if any.  LOC is the location of the new expression.  If
1566    we build an operation, do it in TYPE and with CODE.  */
1567
1568 static tree
1569 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
1570 {
1571   tree tem;
1572
1573   if (t1 == 0)
1574     return t2;
1575   else if (t2 == 0)
1576     return t1;
1577
1578   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1579      try to fold this since we will have infinite recursion.  But do
1580      deal with any NEGATE_EXPRs.  */
1581   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1582       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1583     {
1584       if (code == PLUS_EXPR)
1585         {
1586           if (TREE_CODE (t1) == NEGATE_EXPR)
1587             tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t2),
1588                           fold_convert_loc (loc, type, TREE_OPERAND (t1, 0)));
1589           else if (TREE_CODE (t2) == NEGATE_EXPR)
1590             tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t1),
1591                           fold_convert_loc (loc, type, TREE_OPERAND (t2, 0)));
1592           else if (integer_zerop (t2))
1593             return fold_convert_loc (loc, type, t1);
1594         }
1595       else if (code == MINUS_EXPR)
1596         {
1597           if (integer_zerop (t2))
1598             return fold_convert_loc (loc, type, t1);
1599         }
1600
1601       tem = build2 (code, type, fold_convert_loc (loc, type, t1),
1602                     fold_convert_loc (loc, type, t2));
1603       goto associate_trees_exit;
1604     }
1605
1606   return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1607                       fold_convert_loc (loc, type, t2));
1608  associate_trees_exit:
1609   protected_set_expr_location (tem, loc);
1610   return tem;
1611 }
1612 \f
1613 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1614    for use in int_const_binop, size_binop and size_diffop.  */
1615
1616 static bool
1617 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1618 {
1619   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1620     return false;
1621   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1622     return false;
1623
1624   switch (code)
1625     {
1626     case LSHIFT_EXPR:
1627     case RSHIFT_EXPR:
1628     case LROTATE_EXPR:
1629     case RROTATE_EXPR:
1630       return true;
1631
1632     default:
1633       break;
1634     }
1635
1636   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1637          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1638          && TYPE_MODE (type1) == TYPE_MODE (type2);
1639 }
1640
1641
1642 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1643    to produce a new constant.  Return NULL_TREE if we don't know how
1644    to evaluate CODE at compile-time.
1645
1646    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1647
1648 tree
1649 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1650 {
1651   unsigned HOST_WIDE_INT int1l, int2l;
1652   HOST_WIDE_INT int1h, int2h;
1653   unsigned HOST_WIDE_INT low;
1654   HOST_WIDE_INT hi;
1655   unsigned HOST_WIDE_INT garbagel;
1656   HOST_WIDE_INT garbageh;
1657   tree t;
1658   tree type = TREE_TYPE (arg1);
1659   int uns = TYPE_UNSIGNED (type);
1660   int is_sizetype
1661     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1662   int overflow = 0;
1663
1664   int1l = TREE_INT_CST_LOW (arg1);
1665   int1h = TREE_INT_CST_HIGH (arg1);
1666   int2l = TREE_INT_CST_LOW (arg2);
1667   int2h = TREE_INT_CST_HIGH (arg2);
1668
1669   switch (code)
1670     {
1671     case BIT_IOR_EXPR:
1672       low = int1l | int2l, hi = int1h | int2h;
1673       break;
1674
1675     case BIT_XOR_EXPR:
1676       low = int1l ^ int2l, hi = int1h ^ int2h;
1677       break;
1678
1679     case BIT_AND_EXPR:
1680       low = int1l & int2l, hi = int1h & int2h;
1681       break;
1682
1683     case RSHIFT_EXPR:
1684       int2l = -int2l;
1685     case LSHIFT_EXPR:
1686       /* It's unclear from the C standard whether shifts can overflow.
1687          The following code ignores overflow; perhaps a C standard
1688          interpretation ruling is needed.  */
1689       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1690                      &low, &hi, !uns);
1691       break;
1692
1693     case RROTATE_EXPR:
1694       int2l = - int2l;
1695     case LROTATE_EXPR:
1696       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1697                       &low, &hi);
1698       break;
1699
1700     case PLUS_EXPR:
1701       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1702       break;
1703
1704     case MINUS_EXPR:
1705       neg_double (int2l, int2h, &low, &hi);
1706       add_double (int1l, int1h, low, hi, &low, &hi);
1707       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1708       break;
1709
1710     case MULT_EXPR:
1711       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1712       break;
1713
1714     case TRUNC_DIV_EXPR:
1715     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1716     case EXACT_DIV_EXPR:
1717       /* This is a shortcut for a common special case.  */
1718       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1719           && !TREE_OVERFLOW (arg1)
1720           && !TREE_OVERFLOW (arg2)
1721           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1722         {
1723           if (code == CEIL_DIV_EXPR)
1724             int1l += int2l - 1;
1725
1726           low = int1l / int2l, hi = 0;
1727           break;
1728         }
1729
1730       /* ... fall through ...  */
1731
1732     case ROUND_DIV_EXPR:
1733       if (int2h == 0 && int2l == 0)
1734         return NULL_TREE;
1735       if (int2h == 0 && int2l == 1)
1736         {
1737           low = int1l, hi = int1h;
1738           break;
1739         }
1740       if (int1l == int2l && int1h == int2h
1741           && ! (int1l == 0 && int1h == 0))
1742         {
1743           low = 1, hi = 0;
1744           break;
1745         }
1746       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1747                                        &low, &hi, &garbagel, &garbageh);
1748       break;
1749
1750     case TRUNC_MOD_EXPR:
1751     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1752       /* This is a shortcut for a common special case.  */
1753       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1754           && !TREE_OVERFLOW (arg1)
1755           && !TREE_OVERFLOW (arg2)
1756           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1757         {
1758           if (code == CEIL_MOD_EXPR)
1759             int1l += int2l - 1;
1760           low = int1l % int2l, hi = 0;
1761           break;
1762         }
1763
1764       /* ... fall through ...  */
1765
1766     case ROUND_MOD_EXPR:
1767       if (int2h == 0 && int2l == 0)
1768         return NULL_TREE;
1769       overflow = div_and_round_double (code, uns,
1770                                        int1l, int1h, int2l, int2h,
1771                                        &garbagel, &garbageh, &low, &hi);
1772       break;
1773
1774     case MIN_EXPR:
1775     case MAX_EXPR:
1776       if (uns)
1777         low = (((unsigned HOST_WIDE_INT) int1h
1778                 < (unsigned HOST_WIDE_INT) int2h)
1779                || (((unsigned HOST_WIDE_INT) int1h
1780                     == (unsigned HOST_WIDE_INT) int2h)
1781                    && int1l < int2l));
1782       else
1783         low = (int1h < int2h
1784                || (int1h == int2h && int1l < int2l));
1785
1786       if (low == (code == MIN_EXPR))
1787         low = int1l, hi = int1h;
1788       else
1789         low = int2l, hi = int2h;
1790       break;
1791
1792     default:
1793       return NULL_TREE;
1794     }
1795
1796   if (notrunc)
1797     {
1798       t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1799
1800       /* Propagate overflow flags ourselves.  */
1801       if (((!uns || is_sizetype) && overflow)
1802           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1803         {
1804           t = copy_node (t);
1805           TREE_OVERFLOW (t) = 1;
1806         }
1807     }
1808   else
1809     t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1810                                ((!uns || is_sizetype) && overflow)
1811                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1812
1813   return t;
1814 }
1815
1816 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1817    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1818    are the same kind of constant and the same machine mode.  Return zero if
1819    combining the constants is not allowed in the current operating mode.
1820
1821    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1822
1823 static tree
1824 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1825 {
1826   /* Sanity check for the recursive cases.  */
1827   if (!arg1 || !arg2)
1828     return NULL_TREE;
1829
1830   STRIP_NOPS (arg1);
1831   STRIP_NOPS (arg2);
1832
1833   if (TREE_CODE (arg1) == INTEGER_CST)
1834     return int_const_binop (code, arg1, arg2, notrunc);
1835
1836   if (TREE_CODE (arg1) == REAL_CST)
1837     {
1838       enum machine_mode mode;
1839       REAL_VALUE_TYPE d1;
1840       REAL_VALUE_TYPE d2;
1841       REAL_VALUE_TYPE value;
1842       REAL_VALUE_TYPE result;
1843       bool inexact;
1844       tree t, type;
1845
1846       /* The following codes are handled by real_arithmetic.  */
1847       switch (code)
1848         {
1849         case PLUS_EXPR:
1850         case MINUS_EXPR:
1851         case MULT_EXPR:
1852         case RDIV_EXPR:
1853         case MIN_EXPR:
1854         case MAX_EXPR:
1855           break;
1856
1857         default:
1858           return NULL_TREE;
1859         }
1860
1861       d1 = TREE_REAL_CST (arg1);
1862       d2 = TREE_REAL_CST (arg2);
1863
1864       type = TREE_TYPE (arg1);
1865       mode = TYPE_MODE (type);
1866
1867       /* Don't perform operation if we honor signaling NaNs and
1868          either operand is a NaN.  */
1869       if (HONOR_SNANS (mode)
1870           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1871         return NULL_TREE;
1872
1873       /* Don't perform operation if it would raise a division
1874          by zero exception.  */
1875       if (code == RDIV_EXPR
1876           && REAL_VALUES_EQUAL (d2, dconst0)
1877           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1878         return NULL_TREE;
1879
1880       /* If either operand is a NaN, just return it.  Otherwise, set up
1881          for floating-point trap; we return an overflow.  */
1882       if (REAL_VALUE_ISNAN (d1))
1883         return arg1;
1884       else if (REAL_VALUE_ISNAN (d2))
1885         return arg2;
1886
1887       inexact = real_arithmetic (&value, code, &d1, &d2);
1888       real_convert (&result, mode, &value);
1889
1890       /* Don't constant fold this floating point operation if
1891          the result has overflowed and flag_trapping_math.  */
1892       if (flag_trapping_math
1893           && MODE_HAS_INFINITIES (mode)
1894           && REAL_VALUE_ISINF (result)
1895           && !REAL_VALUE_ISINF (d1)
1896           && !REAL_VALUE_ISINF (d2))
1897         return NULL_TREE;
1898
1899       /* Don't constant fold this floating point operation if the
1900          result may dependent upon the run-time rounding mode and
1901          flag_rounding_math is set, or if GCC's software emulation
1902          is unable to accurately represent the result.  */
1903       if ((flag_rounding_math
1904            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1905           && (inexact || !real_identical (&result, &value)))
1906         return NULL_TREE;
1907
1908       t = build_real (type, result);
1909
1910       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1911       return t;
1912     }
1913
1914   if (TREE_CODE (arg1) == FIXED_CST)
1915     {
1916       FIXED_VALUE_TYPE f1;
1917       FIXED_VALUE_TYPE f2;
1918       FIXED_VALUE_TYPE result;
1919       tree t, type;
1920       int sat_p;
1921       bool overflow_p;
1922
1923       /* The following codes are handled by fixed_arithmetic.  */
1924       switch (code)
1925         {
1926         case PLUS_EXPR:
1927         case MINUS_EXPR:
1928         case MULT_EXPR:
1929         case TRUNC_DIV_EXPR:
1930           f2 = TREE_FIXED_CST (arg2);
1931           break;
1932
1933         case LSHIFT_EXPR:
1934         case RSHIFT_EXPR:
1935           f2.data.high = TREE_INT_CST_HIGH (arg2);
1936           f2.data.low = TREE_INT_CST_LOW (arg2);
1937           f2.mode = SImode;
1938           break;
1939
1940         default:
1941           return NULL_TREE;
1942         }
1943
1944       f1 = TREE_FIXED_CST (arg1);
1945       type = TREE_TYPE (arg1);
1946       sat_p = TYPE_SATURATING (type);
1947       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1948       t = build_fixed (type, result);
1949       /* Propagate overflow flags.  */
1950       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1951         TREE_OVERFLOW (t) = 1;
1952       return t;
1953     }
1954
1955   if (TREE_CODE (arg1) == COMPLEX_CST)
1956     {
1957       tree type = TREE_TYPE (arg1);
1958       tree r1 = TREE_REALPART (arg1);
1959       tree i1 = TREE_IMAGPART (arg1);
1960       tree r2 = TREE_REALPART (arg2);
1961       tree i2 = TREE_IMAGPART (arg2);
1962       tree real, imag;
1963
1964       switch (code)
1965         {
1966         case PLUS_EXPR:
1967         case MINUS_EXPR:
1968           real = const_binop (code, r1, r2, notrunc);
1969           imag = const_binop (code, i1, i2, notrunc);
1970           break;
1971
1972         case MULT_EXPR:
1973           if (COMPLEX_FLOAT_TYPE_P (type))
1974             return do_mpc_arg2 (arg1, arg2, type,
1975                                 /* do_nonfinite= */ folding_initializer,
1976                                 mpc_mul);
1977
1978           real = const_binop (MINUS_EXPR,
1979                               const_binop (MULT_EXPR, r1, r2, notrunc),
1980                               const_binop (MULT_EXPR, i1, i2, notrunc),
1981                               notrunc);
1982           imag = const_binop (PLUS_EXPR,
1983                               const_binop (MULT_EXPR, r1, i2, notrunc),
1984                               const_binop (MULT_EXPR, i1, r2, notrunc),
1985                               notrunc);
1986           break;
1987
1988         case RDIV_EXPR:
1989           if (COMPLEX_FLOAT_TYPE_P (type))
1990             return do_mpc_arg2 (arg1, arg2, type,
1991                                 /* do_nonfinite= */ folding_initializer,
1992                                 mpc_div);
1993           /* Fallthru ... */
1994         case TRUNC_DIV_EXPR:
1995         case CEIL_DIV_EXPR:
1996         case FLOOR_DIV_EXPR:
1997         case ROUND_DIV_EXPR:
1998           if (flag_complex_method == 0)
1999           {
2000             /* Keep this algorithm in sync with
2001                tree-complex.c:expand_complex_div_straight().
2002
2003                Expand complex division to scalars, straightforward algorithm.
2004                a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
2005                t = br*br + bi*bi
2006             */
2007             tree magsquared
2008               = const_binop (PLUS_EXPR,
2009                              const_binop (MULT_EXPR, r2, r2, notrunc),
2010                              const_binop (MULT_EXPR, i2, i2, notrunc),
2011                              notrunc);
2012             tree t1
2013               = const_binop (PLUS_EXPR,
2014                              const_binop (MULT_EXPR, r1, r2, notrunc),
2015                              const_binop (MULT_EXPR, i1, i2, notrunc),
2016                              notrunc);
2017             tree t2
2018               = const_binop (MINUS_EXPR,
2019                              const_binop (MULT_EXPR, i1, r2, notrunc),
2020                              const_binop (MULT_EXPR, r1, i2, notrunc),
2021                              notrunc);
2022
2023             real = const_binop (code, t1, magsquared, notrunc);
2024             imag = const_binop (code, t2, magsquared, notrunc);
2025           }
2026           else
2027           {
2028             /* Keep this algorithm in sync with
2029                tree-complex.c:expand_complex_div_wide().
2030
2031                Expand complex division to scalars, modified algorithm to minimize
2032                overflow with wide input ranges.  */
2033             tree compare = fold_build2 (LT_EXPR, boolean_type_node,
2034                                         fold_abs_const (r2, TREE_TYPE (type)),
2035                                         fold_abs_const (i2, TREE_TYPE (type)));
2036
2037             if (integer_nonzerop (compare))
2038               {
2039                 /* In the TRUE branch, we compute
2040                    ratio = br/bi;
2041                    div = (br * ratio) + bi;
2042                    tr = (ar * ratio) + ai;
2043                    ti = (ai * ratio) - ar;
2044                    tr = tr / div;
2045                    ti = ti / div;  */
2046                 tree ratio = const_binop (code, r2, i2, notrunc);
2047                 tree div = const_binop (PLUS_EXPR, i2,
2048                                         const_binop (MULT_EXPR, r2, ratio,
2049                                                      notrunc),
2050                                         notrunc);
2051                 real = const_binop (MULT_EXPR, r1, ratio, notrunc);
2052                 real = const_binop (PLUS_EXPR, real, i1, notrunc);
2053                 real = const_binop (code, real, div, notrunc);
2054
2055                 imag = const_binop (MULT_EXPR, i1, ratio, notrunc);
2056                 imag = const_binop (MINUS_EXPR, imag, r1, notrunc);
2057                 imag = const_binop (code, imag, div, notrunc);
2058               }
2059             else
2060               {
2061                 /* In the FALSE branch, we compute
2062                    ratio = d/c;
2063                    divisor = (d * ratio) + c;
2064                    tr = (b * ratio) + a;
2065                    ti = b - (a * ratio);
2066                    tr = tr / div;
2067                    ti = ti / div;  */
2068                 tree ratio = const_binop (code, i2, r2, notrunc);
2069                 tree div = const_binop (PLUS_EXPR, r2,
2070                                         const_binop (MULT_EXPR, i2, ratio,
2071                                                      notrunc),
2072                                         notrunc);
2073
2074                 real = const_binop (MULT_EXPR, i1, ratio, notrunc);
2075                 real = const_binop (PLUS_EXPR, real, r1, notrunc);
2076                 real = const_binop (code, real, div, notrunc);
2077
2078                 imag = const_binop (MULT_EXPR, r1, ratio, notrunc);
2079                 imag = const_binop (MINUS_EXPR, i1, imag, notrunc);
2080                 imag = const_binop (code, imag, div, notrunc);
2081               }
2082           }
2083           break;
2084
2085         default:
2086           return NULL_TREE;
2087         }
2088
2089       if (real && imag)
2090         return build_complex (type, real, imag);
2091     }
2092
2093   if (TREE_CODE (arg1) == VECTOR_CST)
2094     {
2095       tree type = TREE_TYPE(arg1);
2096       int count = TYPE_VECTOR_SUBPARTS (type), i;
2097       tree elements1, elements2, list = NULL_TREE;
2098
2099       if(TREE_CODE(arg2) != VECTOR_CST)
2100         return NULL_TREE;
2101
2102       elements1 = TREE_VECTOR_CST_ELTS (arg1);
2103       elements2 = TREE_VECTOR_CST_ELTS (arg2);
2104
2105       for (i = 0; i < count; i++)
2106         {
2107           tree elem1, elem2, elem;
2108
2109           /* The trailing elements can be empty and should be treated as 0 */
2110           if(!elements1)
2111             elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2112           else
2113             {
2114               elem1 = TREE_VALUE(elements1);
2115               elements1 = TREE_CHAIN (elements1);
2116             }
2117
2118           if(!elements2)
2119             elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2120           else
2121             {
2122               elem2 = TREE_VALUE(elements2);
2123               elements2 = TREE_CHAIN (elements2);
2124             }
2125
2126           elem = const_binop (code, elem1, elem2, notrunc);
2127
2128           /* It is possible that const_binop cannot handle the given
2129             code and return NULL_TREE */
2130           if(elem == NULL_TREE)
2131             return NULL_TREE;
2132
2133           list = tree_cons (NULL_TREE, elem, list);
2134         }
2135       return build_vector(type, nreverse(list));
2136     }
2137   return NULL_TREE;
2138 }
2139
2140 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
2141    indicates which particular sizetype to create.  */
2142
2143 tree
2144 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2145 {
2146   return build_int_cst (sizetype_tab[(int) kind], number);
2147 }
2148 \f
2149 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
2150    is a tree code.  The type of the result is taken from the operands.
2151    Both must be equivalent integer types, ala int_binop_types_match_p.
2152    If the operands are constant, so is the result.  */
2153
2154 tree
2155 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2156 {
2157   tree type = TREE_TYPE (arg0);
2158
2159   if (arg0 == error_mark_node || arg1 == error_mark_node)
2160     return error_mark_node;
2161
2162   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2163                                        TREE_TYPE (arg1)));
2164
2165   /* Handle the special case of two integer constants faster.  */
2166   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2167     {
2168       /* And some specific cases even faster than that.  */
2169       if (code == PLUS_EXPR)
2170         {
2171           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2172             return arg1;
2173           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2174             return arg0;
2175         }
2176       else if (code == MINUS_EXPR)
2177         {
2178           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2179             return arg0;
2180         }
2181       else if (code == MULT_EXPR)
2182         {
2183           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2184             return arg1;
2185         }
2186
2187       /* Handle general case of two integer constants.  */
2188       return int_const_binop (code, arg0, arg1, 0);
2189     }
2190
2191   return fold_build2_loc (loc, code, type, arg0, arg1);
2192 }
2193
2194 /* Given two values, either both of sizetype or both of bitsizetype,
2195    compute the difference between the two values.  Return the value
2196    in signed type corresponding to the type of the operands.  */
2197
2198 tree
2199 size_diffop_loc (location_t loc, tree arg0, tree arg1)
2200 {
2201   tree type = TREE_TYPE (arg0);
2202   tree ctype;
2203
2204   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2205                                        TREE_TYPE (arg1)));
2206
2207   /* If the type is already signed, just do the simple thing.  */
2208   if (!TYPE_UNSIGNED (type))
2209     return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2210
2211   if (type == sizetype)
2212     ctype = ssizetype;
2213   else if (type == bitsizetype)
2214     ctype = sbitsizetype;
2215   else
2216     ctype = signed_type_for (type);
2217
2218   /* If either operand is not a constant, do the conversions to the signed
2219      type and subtract.  The hardware will do the right thing with any
2220      overflow in the subtraction.  */
2221   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2222     return size_binop_loc (loc, MINUS_EXPR,
2223                            fold_convert_loc (loc, ctype, arg0),
2224                            fold_convert_loc (loc, ctype, arg1));
2225
2226   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2227      Otherwise, subtract the other way, convert to CTYPE (we know that can't
2228      overflow) and negate (which can't either).  Special-case a result
2229      of zero while we're here.  */
2230   if (tree_int_cst_equal (arg0, arg1))
2231     return build_int_cst (ctype, 0);
2232   else if (tree_int_cst_lt (arg1, arg0))
2233     return fold_convert_loc (loc, ctype,
2234                              size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2235   else
2236     return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2237                            fold_convert_loc (loc, ctype,
2238                                              size_binop_loc (loc,
2239                                                              MINUS_EXPR,
2240                                                              arg1, arg0)));
2241 }
2242 \f
2243 /* A subroutine of fold_convert_const handling conversions of an
2244    INTEGER_CST to another integer type.  */
2245
2246 static tree
2247 fold_convert_const_int_from_int (tree type, const_tree arg1)
2248 {
2249   tree t;
2250
2251   /* Given an integer constant, make new constant with new type,
2252      appropriately sign-extended or truncated.  */
2253   t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2254                              TREE_INT_CST_HIGH (arg1),
2255                              /* Don't set the overflow when
2256                                 converting from a pointer,  */
2257                              !POINTER_TYPE_P (TREE_TYPE (arg1))
2258                              /* or to a sizetype with same signedness
2259                                 and the precision is unchanged.
2260                                 ???  sizetype is always sign-extended,
2261                                 but its signedness depends on the
2262                                 frontend.  Thus we see spurious overflows
2263                                 here if we do not check this.  */
2264                              && !((TYPE_PRECISION (TREE_TYPE (arg1))
2265                                    == TYPE_PRECISION (type))
2266                                   && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2267                                       == TYPE_UNSIGNED (type))
2268                                   && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2269                                        && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2270                                       || (TREE_CODE (type) == INTEGER_TYPE
2271                                           && TYPE_IS_SIZETYPE (type)))),
2272                              (TREE_INT_CST_HIGH (arg1) < 0
2273                               && (TYPE_UNSIGNED (type)
2274                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2275                              | TREE_OVERFLOW (arg1));
2276
2277   return t;
2278 }
2279
2280 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2281    to an integer type.  */
2282
2283 static tree
2284 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2285 {
2286   int overflow = 0;
2287   tree t;
2288
2289   /* The following code implements the floating point to integer
2290      conversion rules required by the Java Language Specification,
2291      that IEEE NaNs are mapped to zero and values that overflow
2292      the target precision saturate, i.e. values greater than
2293      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2294      are mapped to INT_MIN.  These semantics are allowed by the
2295      C and C++ standards that simply state that the behavior of
2296      FP-to-integer conversion is unspecified upon overflow.  */
2297
2298   HOST_WIDE_INT high, low;
2299   REAL_VALUE_TYPE r;
2300   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2301
2302   switch (code)
2303     {
2304     case FIX_TRUNC_EXPR:
2305       real_trunc (&r, VOIDmode, &x);
2306       break;
2307
2308     default:
2309       gcc_unreachable ();
2310     }
2311
2312   /* If R is NaN, return zero and show we have an overflow.  */
2313   if (REAL_VALUE_ISNAN (r))
2314     {
2315       overflow = 1;
2316       high = 0;
2317       low = 0;
2318     }
2319
2320   /* See if R is less than the lower bound or greater than the
2321      upper bound.  */
2322
2323   if (! overflow)
2324     {
2325       tree lt = TYPE_MIN_VALUE (type);
2326       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2327       if (REAL_VALUES_LESS (r, l))
2328         {
2329           overflow = 1;
2330           high = TREE_INT_CST_HIGH (lt);
2331           low = TREE_INT_CST_LOW (lt);
2332         }
2333     }
2334
2335   if (! overflow)
2336     {
2337       tree ut = TYPE_MAX_VALUE (type);
2338       if (ut)
2339         {
2340           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2341           if (REAL_VALUES_LESS (u, r))
2342             {
2343               overflow = 1;
2344               high = TREE_INT_CST_HIGH (ut);
2345               low = TREE_INT_CST_LOW (ut);
2346             }
2347         }
2348     }
2349
2350   if (! overflow)
2351     REAL_VALUE_TO_INT (&low, &high, r);
2352
2353   t = force_fit_type_double (type, low, high, -1,
2354                              overflow | TREE_OVERFLOW (arg1));
2355   return t;
2356 }
2357
2358 /* A subroutine of fold_convert_const handling conversions of a
2359    FIXED_CST to an integer type.  */
2360
2361 static tree
2362 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2363 {
2364   tree t;
2365   double_int temp, temp_trunc;
2366   unsigned int mode;
2367
2368   /* Right shift FIXED_CST to temp by fbit.  */
2369   temp = TREE_FIXED_CST (arg1).data;
2370   mode = TREE_FIXED_CST (arg1).mode;
2371   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2372     {
2373       lshift_double (temp.low, temp.high,
2374                      - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2375                      &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2376
2377       /* Left shift temp to temp_trunc by fbit.  */
2378       lshift_double (temp.low, temp.high,
2379                      GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2380                      &temp_trunc.low, &temp_trunc.high,
2381                      SIGNED_FIXED_POINT_MODE_P (mode));
2382     }
2383   else
2384     {
2385       temp.low = 0;
2386       temp.high = 0;
2387       temp_trunc.low = 0;
2388       temp_trunc.high = 0;
2389     }
2390
2391   /* If FIXED_CST is negative, we need to round the value toward 0.
2392      By checking if the fractional bits are not zero to add 1 to temp.  */
2393   if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2394       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2395     {
2396       double_int one;
2397       one.low = 1;
2398       one.high = 0;
2399       temp = double_int_add (temp, one);
2400     }
2401
2402   /* Given a fixed-point constant, make new constant with new type,
2403      appropriately sign-extended or truncated.  */
2404   t = force_fit_type_double (type, temp.low, temp.high, -1,
2405                              (temp.high < 0
2406                               && (TYPE_UNSIGNED (type)
2407                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2408                              | TREE_OVERFLOW (arg1));
2409
2410   return t;
2411 }
2412
2413 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2414    to another floating point type.  */
2415
2416 static tree
2417 fold_convert_const_real_from_real (tree type, const_tree arg1)
2418 {
2419   REAL_VALUE_TYPE value;
2420   tree t;
2421
2422   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2423   t = build_real (type, value);
2424
2425   /* If converting an infinity or NAN to a representation that doesn't
2426      have one, set the overflow bit so that we can produce some kind of
2427      error message at the appropriate point if necessary.  It's not the
2428      most user-friendly message, but it's better than nothing.  */
2429   if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2430       && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2431     TREE_OVERFLOW (t) = 1;
2432   else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2433            && !MODE_HAS_NANS (TYPE_MODE (type)))
2434     TREE_OVERFLOW (t) = 1;
2435   /* Regular overflow, conversion produced an infinity in a mode that
2436      can't represent them.  */
2437   else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2438            && REAL_VALUE_ISINF (value)
2439            && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2440     TREE_OVERFLOW (t) = 1;
2441   else
2442     TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2443   return t;
2444 }
2445
2446 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2447    to a floating point type.  */
2448
2449 static tree
2450 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2451 {
2452   REAL_VALUE_TYPE value;
2453   tree t;
2454
2455   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2456   t = build_real (type, value);
2457
2458   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2459   return t;
2460 }
2461
2462 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2463    to another fixed-point type.  */
2464
2465 static tree
2466 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2467 {
2468   FIXED_VALUE_TYPE value;
2469   tree t;
2470   bool overflow_p;
2471
2472   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2473                               TYPE_SATURATING (type));
2474   t = build_fixed (type, value);
2475
2476   /* Propagate overflow flags.  */
2477   if (overflow_p | TREE_OVERFLOW (arg1))
2478     TREE_OVERFLOW (t) = 1;
2479   return t;
2480 }
2481
2482 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2483    to a fixed-point type.  */
2484
2485 static tree
2486 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2487 {
2488   FIXED_VALUE_TYPE value;
2489   tree t;
2490   bool overflow_p;
2491
2492   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2493                                        TREE_INT_CST (arg1),
2494                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
2495                                        TYPE_SATURATING (type));
2496   t = build_fixed (type, value);
2497
2498   /* Propagate overflow flags.  */
2499   if (overflow_p | TREE_OVERFLOW (arg1))
2500     TREE_OVERFLOW (t) = 1;
2501   return t;
2502 }
2503
2504 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2505    to a fixed-point type.  */
2506
2507 static tree
2508 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2509 {
2510   FIXED_VALUE_TYPE value;
2511   tree t;
2512   bool overflow_p;
2513
2514   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2515                                         &TREE_REAL_CST (arg1),
2516                                         TYPE_SATURATING (type));
2517   t = build_fixed (type, value);
2518
2519   /* Propagate overflow flags.  */
2520   if (overflow_p | TREE_OVERFLOW (arg1))
2521     TREE_OVERFLOW (t) = 1;
2522   return t;
2523 }
2524
2525 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2526    type TYPE.  If no simplification can be done return NULL_TREE.  */
2527
2528 static tree
2529 fold_convert_const (enum tree_code code, tree type, tree arg1)
2530 {
2531   if (TREE_TYPE (arg1) == type)
2532     return arg1;
2533
2534   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2535       || TREE_CODE (type) == OFFSET_TYPE)
2536     {
2537       if (TREE_CODE (arg1) == INTEGER_CST)
2538         return fold_convert_const_int_from_int (type, arg1);
2539       else if (TREE_CODE (arg1) == REAL_CST)
2540         return fold_convert_const_int_from_real (code, type, arg1);
2541       else if (TREE_CODE (arg1) == FIXED_CST)
2542         return fold_convert_const_int_from_fixed (type, arg1);
2543     }
2544   else if (TREE_CODE (type) == REAL_TYPE)
2545     {
2546       if (TREE_CODE (arg1) == INTEGER_CST)
2547         return build_real_from_int_cst (type, arg1);
2548       else if (TREE_CODE (arg1) == REAL_CST)
2549         return fold_convert_const_real_from_real (type, arg1);
2550       else if (TREE_CODE (arg1) == FIXED_CST)
2551         return fold_convert_const_real_from_fixed (type, arg1);
2552     }
2553   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2554     {
2555       if (TREE_CODE (arg1) == FIXED_CST)
2556         return fold_convert_const_fixed_from_fixed (type, arg1);
2557       else if (TREE_CODE (arg1) == INTEGER_CST)
2558         return fold_convert_const_fixed_from_int (type, arg1);
2559       else if (TREE_CODE (arg1) == REAL_CST)
2560         return fold_convert_const_fixed_from_real (type, arg1);
2561     }
2562   return NULL_TREE;
2563 }
2564
2565 /* Construct a vector of zero elements of vector type TYPE.  */
2566
2567 static tree
2568 build_zero_vector (tree type)
2569 {
2570   tree elem, list;
2571   int i, units;
2572
2573   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2574   units = TYPE_VECTOR_SUBPARTS (type);
2575
2576   list = NULL_TREE;
2577   for (i = 0; i < units; i++)
2578     list = tree_cons (NULL_TREE, elem, list);
2579   return build_vector (type, list);
2580 }
2581
2582 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
2583
2584 bool
2585 fold_convertible_p (const_tree type, const_tree arg)
2586 {
2587   tree orig = TREE_TYPE (arg);
2588
2589   if (type == orig)
2590     return true;
2591
2592   if (TREE_CODE (arg) == ERROR_MARK
2593       || TREE_CODE (type) == ERROR_MARK
2594       || TREE_CODE (orig) == ERROR_MARK)
2595     return false;
2596
2597   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2598     return true;
2599
2600   switch (TREE_CODE (type))
2601     {
2602     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2603     case POINTER_TYPE: case REFERENCE_TYPE:
2604     case OFFSET_TYPE:
2605       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2606           || TREE_CODE (orig) == OFFSET_TYPE)
2607         return true;
2608       return (TREE_CODE (orig) == VECTOR_TYPE
2609               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2610
2611     case REAL_TYPE:
2612     case FIXED_POINT_TYPE:
2613     case COMPLEX_TYPE:
2614     case VECTOR_TYPE:
2615     case VOID_TYPE:
2616       return TREE_CODE (type) == TREE_CODE (orig);
2617
2618     default:
2619       return false;
2620     }
2621 }
2622
2623 /* Convert expression ARG to type TYPE.  Used by the middle-end for
2624    simple conversions in preference to calling the front-end's convert.  */
2625
2626 tree
2627 fold_convert_loc (location_t loc, tree type, tree arg)
2628 {
2629   tree orig = TREE_TYPE (arg);
2630   tree tem;
2631
2632   if (type == orig)
2633     return arg;
2634
2635   if (TREE_CODE (arg) == ERROR_MARK
2636       || TREE_CODE (type) == ERROR_MARK
2637       || TREE_CODE (orig) == ERROR_MARK)
2638     return error_mark_node;
2639
2640   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2641     return fold_build1_loc (loc, NOP_EXPR, type, arg);
2642
2643   switch (TREE_CODE (type))
2644     {
2645     case POINTER_TYPE:
2646     case REFERENCE_TYPE:
2647       /* Handle conversions between pointers to different address spaces.  */
2648       if (POINTER_TYPE_P (orig)
2649           && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2650               != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2651         return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2652       /* fall through */
2653
2654     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2655     case OFFSET_TYPE:
2656       if (TREE_CODE (arg) == INTEGER_CST)
2657         {
2658           tem = fold_convert_const (NOP_EXPR, type, arg);
2659           if (tem != NULL_TREE)
2660             return tem;
2661         }
2662       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2663           || TREE_CODE (orig) == OFFSET_TYPE)
2664         return fold_build1_loc (loc, NOP_EXPR, type, arg);
2665       if (TREE_CODE (orig) == COMPLEX_TYPE)
2666         return fold_convert_loc (loc, type,
2667                              fold_build1_loc (loc, REALPART_EXPR,
2668                                           TREE_TYPE (orig), arg));
2669       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2670                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2671       return fold_build1_loc (loc, NOP_EXPR, type, arg);
2672
2673     case REAL_TYPE:
2674       if (TREE_CODE (arg) == INTEGER_CST)
2675         {
2676           tem = fold_convert_const (FLOAT_EXPR, type, arg);
2677           if (tem != NULL_TREE)
2678             return tem;
2679         }
2680       else if (TREE_CODE (arg) == REAL_CST)
2681         {
2682           tem = fold_convert_const (NOP_EXPR, type, arg);
2683           if (tem != NULL_TREE)
2684             return tem;
2685         }
2686       else if (TREE_CODE (arg) == FIXED_CST)
2687         {
2688           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2689           if (tem != NULL_TREE)
2690             return tem;
2691         }
2692
2693       switch (TREE_CODE (orig))
2694         {
2695         case INTEGER_TYPE:
2696         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2697         case POINTER_TYPE: case REFERENCE_TYPE:
2698           return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2699
2700         case REAL_TYPE:
2701           return fold_build1_loc (loc, NOP_EXPR, type, arg);
2702
2703         case FIXED_POINT_TYPE:
2704           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2705
2706         case COMPLEX_TYPE:
2707           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2708           return fold_convert_loc (loc, type, tem);
2709
2710         default:
2711           gcc_unreachable ();
2712         }
2713
2714     case FIXED_POINT_TYPE:
2715       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2716           || TREE_CODE (arg) == REAL_CST)
2717         {
2718           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2719           if (tem != NULL_TREE)
2720             goto fold_convert_exit;
2721         }
2722
2723       switch (TREE_CODE (orig))
2724         {
2725         case FIXED_POINT_TYPE:
2726         case INTEGER_TYPE:
2727         case ENUMERAL_TYPE:
2728         case BOOLEAN_TYPE:
2729         case REAL_TYPE:
2730           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2731
2732         case COMPLEX_TYPE:
2733           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2734           return fold_convert_loc (loc, type, tem);
2735
2736         default:
2737           gcc_unreachable ();
2738         }
2739
2740     case COMPLEX_TYPE:
2741       switch (TREE_CODE (orig))
2742         {
2743         case INTEGER_TYPE:
2744         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2745         case POINTER_TYPE: case REFERENCE_TYPE:
2746         case REAL_TYPE:
2747         case FIXED_POINT_TYPE:
2748           return fold_build2_loc (loc, COMPLEX_EXPR, type,
2749                               fold_convert_loc (loc, TREE_TYPE (type), arg),
2750                               fold_convert_loc (loc, TREE_TYPE (type),
2751                                             integer_zero_node));
2752         case COMPLEX_TYPE:
2753           {
2754             tree rpart, ipart;
2755
2756             if (TREE_CODE (arg) == COMPLEX_EXPR)
2757               {
2758                 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2759                                       TREE_OPERAND (arg, 0));
2760                 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2761                                       TREE_OPERAND (arg, 1));
2762                 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2763               }
2764
2765             arg = save_expr (arg);
2766             rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2767             ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2768             rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2769             ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2770             return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2771           }
2772
2773         default:
2774           gcc_unreachable ();
2775         }
2776
2777     case VECTOR_TYPE:
2778       if (integer_zerop (arg))
2779         return build_zero_vector (type);
2780       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2781       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2782                   || TREE_CODE (orig) == VECTOR_TYPE);
2783       return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2784
2785     case VOID_TYPE:
2786       tem = fold_ignored_result (arg);
2787       if (TREE_CODE (tem) == MODIFY_EXPR)
2788         goto fold_convert_exit;
2789       return fold_build1_loc (loc, NOP_EXPR, type, tem);
2790
2791     default:
2792       gcc_unreachable ();
2793     }
2794  fold_convert_exit:
2795   protected_set_expr_location (tem, loc);
2796   return tem;
2797 }
2798 \f
2799 /* Return false if expr can be assumed not to be an lvalue, true
2800    otherwise.  */
2801
2802 static bool
2803 maybe_lvalue_p (const_tree x)
2804 {
2805   /* We only need to wrap lvalue tree codes.  */
2806   switch (TREE_CODE (x))
2807   {
2808   case VAR_DECL:
2809   case PARM_DECL:
2810   case RESULT_DECL:
2811   case LABEL_DECL:
2812   case FUNCTION_DECL:
2813   case SSA_NAME:
2814
2815   case COMPONENT_REF:
2816   case INDIRECT_REF:
2817   case ALIGN_INDIRECT_REF:
2818   case MISALIGNED_INDIRECT_REF:
2819   case ARRAY_REF:
2820   case ARRAY_RANGE_REF:
2821   case BIT_FIELD_REF:
2822   case OBJ_TYPE_REF:
2823
2824   case REALPART_EXPR:
2825   case IMAGPART_EXPR:
2826   case PREINCREMENT_EXPR:
2827   case PREDECREMENT_EXPR:
2828   case SAVE_EXPR:
2829   case TRY_CATCH_EXPR:
2830   case WITH_CLEANUP_EXPR:
2831   case COMPOUND_EXPR:
2832   case MODIFY_EXPR:
2833   case TARGET_EXPR:
2834   case COND_EXPR:
2835   case BIND_EXPR:
2836     break;
2837
2838   default:
2839     /* Assume the worst for front-end tree codes.  */
2840     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2841       break;
2842     return false;
2843   }
2844
2845   return true;
2846 }
2847
2848 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2849
2850 tree
2851 non_lvalue_loc (location_t loc, tree x)
2852 {
2853   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2854      us.  */
2855   if (in_gimple_form)
2856     return x;
2857
2858   if (! maybe_lvalue_p (x))
2859     return x;
2860   x = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2861   SET_EXPR_LOCATION (x, loc);
2862   return x;
2863 }
2864
2865 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2866    Zero means allow extended lvalues.  */
2867
2868 int pedantic_lvalues;
2869
2870 /* When pedantic, return an expr equal to X but certainly not valid as a
2871    pedantic lvalue.  Otherwise, return X.  */
2872
2873 static tree
2874 pedantic_non_lvalue_loc (location_t loc, tree x)
2875 {
2876   if (pedantic_lvalues)
2877     return non_lvalue_loc (loc, x);
2878   protected_set_expr_location (x, loc);
2879   return x;
2880 }
2881 \f
2882 /* Given a tree comparison code, return the code that is the logical inverse
2883    of the given code.  It is not safe to do this for floating-point
2884    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2885    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2886
2887 enum tree_code
2888 invert_tree_comparison (enum tree_code code, bool honor_nans)
2889 {
2890   if (honor_nans && flag_trapping_math)
2891     return ERROR_MARK;
2892
2893   switch (code)
2894     {
2895     case EQ_EXPR:
2896       return NE_EXPR;
2897     case NE_EXPR:
2898       return EQ_EXPR;
2899     case GT_EXPR:
2900       return honor_nans ? UNLE_EXPR : LE_EXPR;
2901     case GE_EXPR:
2902       return honor_nans ? UNLT_EXPR : LT_EXPR;
2903     case LT_EXPR:
2904       return honor_nans ? UNGE_EXPR : GE_EXPR;
2905     case LE_EXPR:
2906       return honor_nans ? UNGT_EXPR : GT_EXPR;
2907     case LTGT_EXPR:
2908       return UNEQ_EXPR;
2909     case UNEQ_EXPR:
2910       return LTGT_EXPR;
2911     case UNGT_EXPR:
2912       return LE_EXPR;
2913     case UNGE_EXPR:
2914       return LT_EXPR;
2915     case UNLT_EXPR:
2916       return GE_EXPR;
2917     case UNLE_EXPR:
2918       return GT_EXPR;
2919     case ORDERED_EXPR:
2920       return UNORDERED_EXPR;
2921     case UNORDERED_EXPR:
2922       return ORDERED_EXPR;
2923     default:
2924       gcc_unreachable ();
2925     }
2926 }
2927
2928 /* Similar, but return the comparison that results if the operands are
2929    swapped.  This is safe for floating-point.  */
2930
2931 enum tree_code
2932 swap_tree_comparison (enum tree_code code)
2933 {
2934   switch (code)
2935     {
2936     case EQ_EXPR:
2937     case NE_EXPR:
2938     case ORDERED_EXPR:
2939     case UNORDERED_EXPR:
2940     case LTGT_EXPR:
2941     case UNEQ_EXPR:
2942       return code;
2943     case GT_EXPR:
2944       return LT_EXPR;
2945     case GE_EXPR:
2946       return LE_EXPR;
2947     case LT_EXPR:
2948       return GT_EXPR;
2949     case LE_EXPR:
2950       return GE_EXPR;
2951     case UNGT_EXPR:
2952       return UNLT_EXPR;
2953     case UNGE_EXPR:
2954       return UNLE_EXPR;
2955     case UNLT_EXPR:
2956       return UNGT_EXPR;
2957     case UNLE_EXPR:
2958       return UNGE_EXPR;
2959     default:
2960       gcc_unreachable ();
2961     }
2962 }
2963
2964
2965 /* Convert a comparison tree code from an enum tree_code representation
2966    into a compcode bit-based encoding.  This function is the inverse of
2967    compcode_to_comparison.  */
2968
2969 static enum comparison_code
2970 comparison_to_compcode (enum tree_code code)
2971 {
2972   switch (code)
2973     {
2974     case LT_EXPR:
2975       return COMPCODE_LT;
2976     case EQ_EXPR:
2977       return COMPCODE_EQ;
2978     case LE_EXPR:
2979       return COMPCODE_LE;
2980     case GT_EXPR:
2981       return COMPCODE_GT;
2982     case NE_EXPR:
2983       return COMPCODE_NE;
2984     case GE_EXPR:
2985       return COMPCODE_GE;
2986     case ORDERED_EXPR:
2987       return COMPCODE_ORD;
2988     case UNORDERED_EXPR:
2989       return COMPCODE_UNORD;
2990     case UNLT_EXPR:
2991       return COMPCODE_UNLT;
2992     case UNEQ_EXPR:
2993       return COMPCODE_UNEQ;
2994     case UNLE_EXPR:
2995       return COMPCODE_UNLE;
2996     case UNGT_EXPR:
2997       return COMPCODE_UNGT;
2998     case LTGT_EXPR:
2999       return COMPCODE_LTGT;
3000     case UNGE_EXPR:
3001       return COMPCODE_UNGE;
3002     default:
3003       gcc_unreachable ();
3004     }
3005 }
3006
3007 /* Convert a compcode bit-based encoding of a comparison operator back
3008    to GCC's enum tree_code representation.  This function is the
3009    inverse of comparison_to_compcode.  */
3010
3011 static enum tree_code
3012 compcode_to_comparison (enum comparison_code code)
3013 {
3014   switch (code)
3015     {
3016     case COMPCODE_LT:
3017       return LT_EXPR;
3018     case COMPCODE_EQ:
3019       return EQ_EXPR;
3020     case COMPCODE_LE:
3021       return LE_EXPR;
3022     case COMPCODE_GT:
3023       return GT_EXPR;
3024     case COMPCODE_NE:
3025       return NE_EXPR;
3026     case COMPCODE_GE:
3027       return GE_EXPR;
3028     case COMPCODE_ORD:
3029       return ORDERED_EXPR;
3030     case COMPCODE_UNORD:
3031       return UNORDERED_EXPR;
3032     case COMPCODE_UNLT:
3033       return UNLT_EXPR;
3034     case COMPCODE_UNEQ:
3035       return UNEQ_EXPR;
3036     case COMPCODE_UNLE:
3037       return UNLE_EXPR;
3038     case COMPCODE_UNGT:
3039       return UNGT_EXPR;
3040     case COMPCODE_LTGT:
3041       return LTGT_EXPR;
3042     case COMPCODE_UNGE:
3043       return UNGE_EXPR;
3044     default:
3045       gcc_unreachable ();
3046     }
3047 }
3048
3049 /* Return a tree for the comparison which is the combination of
3050    doing the AND or OR (depending on CODE) of the two operations LCODE
3051    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
3052    the possibility of trapping if the mode has NaNs, and return NULL_TREE
3053    if this makes the transformation invalid.  */
3054
3055 tree
3056 combine_comparisons (location_t loc,
3057                      enum tree_code code, enum tree_code lcode,
3058                      enum tree_code rcode, tree truth_type,
3059                      tree ll_arg, tree lr_arg)
3060 {
3061   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
3062   enum comparison_code lcompcode = comparison_to_compcode (lcode);
3063   enum comparison_code rcompcode = comparison_to_compcode (rcode);
3064   int compcode;
3065
3066   switch (code)
3067     {
3068     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3069       compcode = lcompcode & rcompcode;
3070       break;
3071
3072     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3073       compcode = lcompcode | rcompcode;
3074       break;
3075
3076     default:
3077       return NULL_TREE;
3078     }
3079
3080   if (!honor_nans)
3081     {
3082       /* Eliminate unordered comparisons, as well as LTGT and ORD
3083          which are not used unless the mode has NaNs.  */
3084       compcode &= ~COMPCODE_UNORD;
3085       if (compcode == COMPCODE_LTGT)
3086         compcode = COMPCODE_NE;
3087       else if (compcode == COMPCODE_ORD)
3088         compcode = COMPCODE_TRUE;
3089     }
3090    else if (flag_trapping_math)
3091      {
3092         /* Check that the original operation and the optimized ones will trap
3093            under the same condition.  */
3094         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
3095                      && (lcompcode != COMPCODE_EQ)
3096                      && (lcompcode != COMPCODE_ORD);
3097         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
3098                      && (rcompcode != COMPCODE_EQ)
3099                      && (rcompcode != COMPCODE_ORD);
3100         bool trap = (compcode & COMPCODE_UNORD) == 0
3101                     && (compcode != COMPCODE_EQ)
3102                     && (compcode != COMPCODE_ORD);
3103
3104         /* In a short-circuited boolean expression the LHS might be
3105            such that the RHS, if evaluated, will never trap.  For
3106            example, in ORD (x, y) && (x < y), we evaluate the RHS only
3107            if neither x nor y is NaN.  (This is a mixed blessing: for
3108            example, the expression above will never trap, hence
3109            optimizing it to x < y would be invalid).  */
3110         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3111             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3112           rtrap = false;
3113
3114         /* If the comparison was short-circuited, and only the RHS
3115            trapped, we may now generate a spurious trap.  */
3116         if (rtrap && !ltrap
3117             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3118           return NULL_TREE;
3119
3120         /* If we changed the conditions that cause a trap, we lose.  */
3121         if ((ltrap || rtrap) != trap)
3122           return NULL_TREE;
3123       }
3124
3125   if (compcode == COMPCODE_TRUE)
3126     return constant_boolean_node (true, truth_type);
3127   else if (compcode == COMPCODE_FALSE)
3128     return constant_boolean_node (false, truth_type);
3129   else
3130     {
3131       enum tree_code tcode;
3132
3133       tcode = compcode_to_comparison ((enum comparison_code) compcode);
3134       return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
3135     }
3136 }
3137 \f
3138 /* Return nonzero if two operands (typically of the same tree node)
3139    are necessarily equal.  If either argument has side-effects this
3140    function returns zero.  FLAGS modifies behavior as follows:
3141
3142    If OEP_ONLY_CONST is set, only return nonzero for constants.
3143    This function tests whether the operands are indistinguishable;
3144    it does not test whether they are equal using C's == operation.
3145    The distinction is important for IEEE floating point, because
3146    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3147    (2) two NaNs may be indistinguishable, but NaN!=NaN.
3148
3149    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3150    even though it may hold multiple values during a function.
3151    This is because a GCC tree node guarantees that nothing else is
3152    executed between the evaluation of its "operands" (which may often
3153    be evaluated in arbitrary order).  Hence if the operands themselves
3154    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3155    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
3156    unset means assuming isochronic (or instantaneous) tree equivalence.
3157    Unless comparing arbitrary expression trees, such as from different
3158    statements, this flag can usually be left unset.
3159
3160    If OEP_PURE_SAME is set, then pure functions with identical arguments
3161    are considered the same.  It is used when the caller has other ways
3162    to ensure that global memory is unchanged in between.  */
3163
3164 int
3165 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3166 {
3167   /* If either is ERROR_MARK, they aren't equal.  */
3168   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3169       || TREE_TYPE (arg0) == error_mark_node
3170       || TREE_TYPE (arg1) == error_mark_node)
3171     return 0;
3172
3173   /* Similar, if either does not have a type (like a released SSA name), 
3174      they aren't equal.  */
3175   if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
3176     return 0;
3177
3178   /* Check equality of integer constants before bailing out due to
3179      precision differences.  */
3180   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3181     return tree_int_cst_equal (arg0, arg1);
3182
3183   /* If both types don't have the same signedness, then we can't consider
3184      them equal.  We must check this before the STRIP_NOPS calls
3185      because they may change the signedness of the arguments.  As pointers
3186      strictly don't have a signedness, require either two pointers or
3187      two non-pointers as well.  */
3188   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3189       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
3190     return 0;
3191
3192   /* We cannot consider pointers to different address space equal.  */
3193   if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
3194       && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
3195           != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
3196     return 0;
3197
3198   /* If both types don't have the same precision, then it is not safe
3199      to strip NOPs.  */
3200   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3201     return 0;
3202
3203   STRIP_NOPS (arg0);
3204   STRIP_NOPS (arg1);
3205
3206   /* In case both args are comparisons but with different comparison
3207      code, try to swap the comparison operands of one arg to produce
3208      a match and compare that variant.  */
3209   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3210       && COMPARISON_CLASS_P (arg0)
3211       && COMPARISON_CLASS_P (arg1))
3212     {
3213       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3214
3215       if (TREE_CODE (arg0) == swap_code)
3216         return operand_equal_p (TREE_OPERAND (arg0, 0),
3217                                 TREE_OPERAND (arg1, 1), flags)
3218                && operand_equal_p (TREE_OPERAND (arg0, 1),
3219                                    TREE_OPERAND (arg1, 0), flags);
3220     }
3221
3222   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3223       /* This is needed for conversions and for COMPONENT_REF.
3224          Might as well play it safe and always test this.  */
3225       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3226       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3227       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3228     return 0;
3229
3230   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3231      We don't care about side effects in that case because the SAVE_EXPR
3232      takes care of that for us. In all other cases, two expressions are
3233      equal if they have no side effects.  If we have two identical
3234      expressions with side effects that should be treated the same due
3235      to the only side effects being identical SAVE_EXPR's, that will
3236      be detected in the recursive calls below.  */
3237   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3238       && (TREE_CODE (arg0) == SAVE_EXPR
3239           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3240     return 1;
3241
3242   /* Next handle constant cases, those for which we can return 1 even
3243      if ONLY_CONST is set.  */
3244   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3245     switch (TREE_CODE (arg0))
3246       {
3247       case INTEGER_CST:
3248         return tree_int_cst_equal (arg0, arg1);
3249
3250       case FIXED_CST:
3251         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3252                                        TREE_FIXED_CST (arg1));
3253
3254       case REAL_CST:
3255         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3256                                    TREE_REAL_CST (arg1)))
3257           return 1;
3258
3259
3260         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3261           {
3262             /* If we do not distinguish between signed and unsigned zero,
3263                consider them equal.  */
3264             if (real_zerop (arg0) && real_zerop (arg1))
3265               return 1;
3266           }
3267         return 0;
3268
3269       case VECTOR_CST:
3270         {
3271           tree v1, v2;
3272
3273           v1 = TREE_VECTOR_CST_ELTS (arg0);
3274           v2 = TREE_VECTOR_CST_ELTS (arg1);
3275           while (v1 && v2)
3276             {
3277               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3278                                     flags))
3279                 return 0;
3280               v1 = TREE_CHAIN (v1);
3281               v2 = TREE_CHAIN (v2);
3282             }
3283
3284           return v1 == v2;
3285         }
3286
3287       case COMPLEX_CST:
3288         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3289                                  flags)
3290                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3291                                     flags));
3292
3293       case STRING_CST:
3294         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3295                 && ! memcmp (TREE_STRING_POINTER (arg0),
3296                               TREE_STRING_POINTER (arg1),
3297                               TREE_STRING_LENGTH (arg0)));
3298
3299       case ADDR_EXPR:
3300         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3301                                 0);
3302       default:
3303         break;
3304       }
3305
3306   if (flags & OEP_ONLY_CONST)
3307     return 0;
3308
3309 /* Define macros to test an operand from arg0 and arg1 for equality and a
3310    variant that allows null and views null as being different from any
3311    non-null value.  In the latter case, if either is null, the both
3312    must be; otherwise, do the normal comparison.  */
3313 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
3314                                     TREE_OPERAND (arg1, N), flags)
3315
3316 #define OP_SAME_WITH_NULL(N)                            \
3317   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3318    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3319
3320   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3321     {
3322     case tcc_unary:
3323       /* Two conversions are equal only if signedness and modes match.  */
3324       switch (TREE_CODE (arg0))
3325         {
3326         CASE_CONVERT:
3327         case FIX_TRUNC_EXPR:
3328           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3329               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3330             return 0;
3331           break;
3332         default:
3333           break;
3334         }
3335
3336       return OP_SAME (0);
3337
3338
3339     case tcc_comparison:
3340     case tcc_binary:
3341       if (OP_SAME (0) && OP_SAME (1))
3342         return 1;
3343
3344       /* For commutative ops, allow the other order.  */
3345       return (commutative_tree_code (TREE_CODE (arg0))
3346               && operand_equal_p (TREE_OPERAND (arg0, 0),
3347                                   TREE_OPERAND (arg1, 1), flags)
3348               && operand_equal_p (TREE_OPERAND (arg0, 1),
3349                                   TREE_OPERAND (arg1, 0), flags));
3350
3351     case tcc_reference:
3352       /* If either of the pointer (or reference) expressions we are
3353          dereferencing contain a side effect, these cannot be equal.  */
3354       if (TREE_SIDE_EFFECTS (arg0)
3355           || TREE_SIDE_EFFECTS (arg1))
3356         return 0;
3357
3358       switch (TREE_CODE (arg0))
3359         {
3360         case INDIRECT_REF:
3361         case ALIGN_INDIRECT_REF:
3362         case MISALIGNED_INDIRECT_REF:
3363         case REALPART_EXPR:
3364         case IMAGPART_EXPR:
3365           return OP_SAME (0);
3366
3367         case ARRAY_REF:
3368         case ARRAY_RANGE_REF:
3369           /* Operands 2 and 3 may be null.
3370              Compare the array index by value if it is constant first as we
3371              may have different types but same value here.  */
3372           return (OP_SAME (0)
3373                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3374                                           TREE_OPERAND (arg1, 1))
3375                       || OP_SAME (1))
3376                   && OP_SAME_WITH_NULL (2)
3377                   && OP_SAME_WITH_NULL (3));
3378
3379         case COMPONENT_REF:
3380           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
3381              may be NULL when we're called to compare MEM_EXPRs.  */
3382           return OP_SAME_WITH_NULL (0)
3383                  && OP_SAME (1)
3384                  && OP_SAME_WITH_NULL (2);
3385
3386         case BIT_FIELD_REF:
3387           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3388
3389         default:
3390           return 0;
3391         }
3392
3393     case tcc_expression:
3394       switch (TREE_CODE (arg0))
3395         {
3396         case ADDR_EXPR:
3397         case TRUTH_NOT_EXPR:
3398           return OP_SAME (0);
3399
3400         case TRUTH_ANDIF_EXPR:
3401         case TRUTH_ORIF_EXPR:
3402           return OP_SAME (0) && OP_SAME (1);
3403
3404         case TRUTH_AND_EXPR:
3405         case TRUTH_OR_EXPR:
3406         case TRUTH_XOR_EXPR:
3407           if (OP_SAME (0) && OP_SAME (1))
3408             return 1;
3409
3410           /* Otherwise take into account this is a commutative operation.  */
3411           return (operand_equal_p (TREE_OPERAND (arg0, 0),
3412                                    TREE_OPERAND (arg1, 1), flags)
3413                   && operand_equal_p (TREE_OPERAND (arg0, 1),
3414                                       TREE_OPERAND (arg1, 0), flags));
3415
3416         case COND_EXPR:
3417           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3418
3419         default:
3420           return 0;
3421         }
3422
3423     case tcc_vl_exp:
3424       switch (TREE_CODE (arg0))
3425         {
3426         case CALL_EXPR:
3427           /* If the CALL_EXPRs call different functions, then they
3428              clearly can not be equal.  */
3429           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3430                                  flags))
3431             return 0;
3432
3433           {
3434             unsigned int cef = call_expr_flags (arg0);
3435             if (flags & OEP_PURE_SAME)
3436               cef &= ECF_CONST | ECF_PURE;
3437             else
3438               cef &= ECF_CONST;
3439             if (!cef)
3440               return 0;
3441           }
3442
3443           /* Now see if all the arguments are the same.  */
3444           {
3445             const_call_expr_arg_iterator iter0, iter1;
3446             const_tree a0, a1;
3447             for (a0 = first_const_call_expr_arg (arg0, &iter0),
3448                    a1 = first_const_call_expr_arg (arg1, &iter1);
3449                  a0 && a1;
3450                  a0 = next_const_call_expr_arg (&iter0),
3451                    a1 = next_const_call_expr_arg (&iter1))
3452               if (! operand_equal_p (a0, a1, flags))
3453                 return 0;
3454
3455             /* If we get here and both argument lists are exhausted
3456                then the CALL_EXPRs are equal.  */
3457             return ! (a0 || a1);
3458           }
3459         default:
3460           return 0;
3461         }
3462
3463     case tcc_declaration:
3464       /* Consider __builtin_sqrt equal to sqrt.  */
3465       return (TREE_CODE (arg0) == FUNCTION_DECL
3466               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3467               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3468               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3469
3470     default:
3471       return 0;
3472     }
3473
3474 #undef OP_SAME
3475 #undef OP_SAME_WITH_NULL
3476 }
3477 \f
3478 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3479    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3480
3481    When in doubt, return 0.  */
3482
3483 static int
3484 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3485 {
3486   int unsignedp1, unsignedpo;
3487   tree primarg0, primarg1, primother;
3488   unsigned int correct_width;
3489
3490   if (operand_equal_p (arg0, arg1, 0))
3491     return 1;
3492
3493   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3494       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3495     return 0;
3496
3497   /* Discard any conversions that don't change the modes of ARG0 and ARG1
3498      and see if the inner values are the same.  This removes any
3499      signedness comparison, which doesn't matter here.  */
3500   primarg0 = arg0, primarg1 = arg1;
3501   STRIP_NOPS (primarg0);
3502   STRIP_NOPS (primarg1);
3503   if (operand_equal_p (primarg0, primarg1, 0))
3504     return 1;
3505
3506   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3507      actual comparison operand, ARG0.
3508
3509      First throw away any conversions to wider types
3510      already present in the operands.  */
3511
3512   primarg1 = get_narrower (arg1, &unsignedp1);
3513   primother = get_narrower (other, &unsignedpo);
3514
3515   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3516   if (unsignedp1 == unsignedpo
3517       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3518       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3519     {
3520       tree type = TREE_TYPE (arg0);
3521
3522       /* Make sure shorter operand is extended the right way
3523          to match the longer operand.  */
3524       primarg1 = fold_convert (signed_or_unsigned_type_for
3525                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3526
3527       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3528         return 1;
3529     }
3530
3531   return 0;
3532 }
3533 \f
3534 /* See if ARG is an expression that is either a comparison or is performing
3535    arithmetic on comparisons.  The comparisons must only be comparing
3536    two different values, which will be stored in *CVAL1 and *CVAL2; if
3537    they are nonzero it means that some operands have already been found.
3538    No variables may be used anywhere else in the expression except in the
3539    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
3540    the expression and save_expr needs to be called with CVAL1 and CVAL2.
3541
3542    If this is true, return 1.  Otherwise, return zero.  */
3543
3544 static int
3545 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3546 {
3547   enum tree_code code = TREE_CODE (arg);
3548   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3549
3550   /* We can handle some of the tcc_expression cases here.  */
3551   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3552     tclass = tcc_unary;
3553   else if (tclass == tcc_expression
3554            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3555                || code == COMPOUND_EXPR))
3556     tclass = tcc_binary;
3557
3558   else if (tclass == tcc_expression && code == SAVE_EXPR
3559            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3560     {
3561       /* If we've already found a CVAL1 or CVAL2, this expression is
3562          two complex to handle.  */
3563       if (*cval1 || *cval2)
3564         return 0;
3565
3566       tclass = tcc_unary;
3567       *save_p = 1;
3568     }
3569
3570   switch (tclass)
3571     {
3572     case tcc_unary:
3573       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3574
3575     case tcc_binary:
3576       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3577               && twoval_comparison_p (TREE_OPERAND (arg, 1),
3578                                       cval1, cval2, save_p));
3579
3580     case tcc_constant:
3581       return 1;
3582
3583     case tcc_expression:
3584       if (code == COND_EXPR)
3585         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3586                                      cval1, cval2, save_p)
3587                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3588                                         cval1, cval2, save_p)
3589                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3590                                         cval1, cval2, save_p));
3591       return 0;
3592
3593     case tcc_comparison:
3594       /* First see if we can handle the first operand, then the second.  For
3595          the second operand, we know *CVAL1 can't be zero.  It must be that
3596          one side of the comparison is each of the values; test for the
3597          case where this isn't true by failing if the two operands
3598          are the same.  */
3599
3600       if (operand_equal_p (TREE_OPERAND (arg, 0),
3601                            TREE_OPERAND (arg, 1), 0))
3602         return 0;
3603
3604       if (*cval1 == 0)
3605         *cval1 = TREE_OPERAND (arg, 0);
3606       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3607         ;
3608       else if (*cval2 == 0)
3609         *cval2 = TREE_OPERAND (arg, 0);
3610       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3611         ;
3612       else
3613         return 0;
3614
3615       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3616         ;
3617       else if (*cval2 == 0)
3618         *cval2 = TREE_OPERAND (arg, 1);
3619       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3620         ;
3621       else
3622         return 0;
3623
3624       return 1;
3625
3626     default:
3627       return 0;
3628     }
3629 }
3630 \f
3631 /* ARG is a tree that is known to contain just arithmetic operations and
3632    comparisons.  Evaluate the operations in the tree substituting NEW0 for
3633    any occurrence of OLD0 as an operand of a comparison and likewise for
3634    NEW1 and OLD1.  */
3635
3636 static tree
3637 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3638             tree old1, tree new1)
3639 {
3640   tree type = TREE_TYPE (arg);
3641   enum tree_code code = TREE_CODE (arg);
3642   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3643
3644   /* We can handle some of the tcc_expression cases here.  */
3645   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3646     tclass = tcc_unary;
3647   else if (tclass == tcc_expression
3648            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3649     tclass = tcc_binary;
3650
3651   switch (tclass)
3652     {
3653     case tcc_unary:
3654       return fold_build1_loc (loc, code, type,
3655                           eval_subst (loc, TREE_OPERAND (arg, 0),
3656                                       old0, new0, old1, new1));
3657
3658     case tcc_binary:
3659       return fold_build2_loc (loc, code, type,
3660                           eval_subst (loc, TREE_OPERAND (arg, 0),
3661                                       old0, new0, old1, new1),
3662                           eval_subst (loc, TREE_OPERAND (arg, 1),
3663                                       old0, new0, old1, new1));
3664
3665     case tcc_expression:
3666       switch (code)
3667         {
3668         case SAVE_EXPR:
3669           return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3670                              old1, new1);
3671
3672         case COMPOUND_EXPR:
3673           return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3674                              old1, new1);
3675
3676         case COND_EXPR:
3677           return fold_build3_loc (loc, code, type,
3678                               eval_subst (loc, TREE_OPERAND (arg, 0),
3679                                           old0, new0, old1, new1),
3680                               eval_subst (loc, TREE_OPERAND (arg, 1),
3681                                           old0, new0, old1, new1),
3682                               eval_subst (loc, TREE_OPERAND (arg, 2),
3683                                           old0, new0, old1, new1));
3684         default:
3685           break;
3686         }
3687       /* Fall through - ???  */
3688
3689     case tcc_comparison:
3690       {
3691         tree arg0 = TREE_OPERAND (arg, 0);
3692         tree arg1 = TREE_OPERAND (arg, 1);
3693
3694         /* We need to check both for exact equality and tree equality.  The
3695            former will be true if the operand has a side-effect.  In that
3696            case, we know the operand occurred exactly once.  */
3697
3698         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3699           arg0 = new0;
3700         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3701           arg0 = new1;
3702
3703         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3704           arg1 = new0;
3705         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3706           arg1 = new1;
3707
3708         return fold_build2_loc (loc, code, type, arg0, arg1);
3709       }
3710
3711     default:
3712       return arg;
3713     }
3714 }
3715 \f
3716 /* Return a tree for the case when the result of an expression is RESULT
3717    converted to TYPE and OMITTED was previously an operand of the expression
3718    but is now not needed (e.g., we folded OMITTED * 0).
3719
3720    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
3721    the conversion of RESULT to TYPE.  */
3722
3723 tree
3724 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3725 {
3726   tree t = fold_convert_loc (loc, type, result);
3727
3728   /* If the resulting operand is an empty statement, just return the omitted
3729      statement casted to void. */
3730   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3731     {
3732       t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3733       goto omit_one_operand_exit;
3734     }
3735
3736   if (TREE_SIDE_EFFECTS (omitted))
3737     {
3738       t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3739       goto omit_one_operand_exit;
3740     }
3741
3742   return non_lvalue_loc (loc, t);
3743
3744  omit_one_operand_exit:
3745   protected_set_expr_location (t, loc);
3746   return t;
3747 }
3748
3749 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3750
3751 static tree
3752 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
3753                                tree omitted)
3754 {
3755   tree t = fold_convert_loc (loc, type, result);
3756
3757   /* If the resulting operand is an empty statement, just return the omitted
3758      statement casted to void. */
3759   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3760     {
3761       t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3762       goto pedantic_omit_one_operand_exit;
3763     }
3764
3765   if (TREE_SIDE_EFFECTS (omitted))
3766     {
3767       t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3768       goto pedantic_omit_one_operand_exit;
3769     }
3770
3771   return pedantic_non_lvalue_loc (loc, t);
3772
3773  pedantic_omit_one_operand_exit:
3774   protected_set_expr_location (t, loc);
3775   return t;
3776 }
3777
3778 /* Return a tree for the case when the result of an expression is RESULT
3779    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3780    of the expression but are now not needed.
3781
3782    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3783    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3784    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3785    just do the conversion of RESULT to TYPE.  */
3786
3787 tree
3788 omit_two_operands_loc (location_t loc, tree type, tree result,
3789                    tree omitted1, tree omitted2)
3790 {
3791   tree t = fold_convert_loc (loc, type, result);
3792
3793   if (TREE_SIDE_EFFECTS (omitted2))
3794     {
3795       t = build2 (COMPOUND_EXPR, type, omitted2, t);
3796       SET_EXPR_LOCATION (t, loc);
3797     }
3798   if (TREE_SIDE_EFFECTS (omitted1))
3799     {
3800       t = build2 (COMPOUND_EXPR, type, omitted1, t);
3801       SET_EXPR_LOCATION (t, loc);
3802     }
3803
3804   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3805 }
3806
3807 \f
3808 /* Return a simplified tree node for the truth-negation of ARG.  This
3809    never alters ARG itself.  We assume that ARG is an operation that
3810    returns a truth value (0 or 1).
3811
3812    FIXME: one would think we would fold the result, but it causes
3813    problems with the dominator optimizer.  */
3814
3815 tree
3816 fold_truth_not_expr (location_t loc, tree arg)
3817 {
3818   tree t, type = TREE_TYPE (arg);
3819   enum tree_code code = TREE_CODE (arg);
3820   location_t loc1, loc2;
3821
3822   /* If this is a comparison, we can simply invert it, except for
3823      floating-point non-equality comparisons, in which case we just
3824      enclose a TRUTH_NOT_EXPR around what we have.  */
3825
3826   if (TREE_CODE_CLASS (code) == tcc_comparison)
3827     {
3828       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3829       if (FLOAT_TYPE_P (op_type)
3830           && flag_trapping_math
3831           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3832           && code != NE_EXPR && code != EQ_EXPR)
3833         return NULL_TREE;
3834
3835       code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3836       if (code == ERROR_MARK)
3837         return NULL_TREE;
3838
3839       t = build2 (code, type, TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3840       SET_EXPR_LOCATION (t, loc);
3841       return t;
3842     }
3843
3844   switch (code)
3845     {
3846     case INTEGER_CST:
3847       return constant_boolean_node (integer_zerop (arg), type);
3848
3849     case TRUTH_AND_EXPR:
3850       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3851       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3852       if (loc1 == UNKNOWN_LOCATION)
3853         loc1 = loc;
3854       if (loc2 == UNKNOWN_LOCATION)
3855         loc2 = loc;
3856       t = build2 (TRUTH_OR_EXPR, type,
3857                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3858                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3859       break;
3860
3861     case TRUTH_OR_EXPR:
3862       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3863       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3864       if (loc1 == UNKNOWN_LOCATION)
3865         loc1 = loc;
3866       if (loc2 == UNKNOWN_LOCATION)
3867         loc2 = loc;
3868       t = build2 (TRUTH_AND_EXPR, type,
3869                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3870                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3871       break;
3872
3873     case TRUTH_XOR_EXPR:
3874       /* Here we can invert either operand.  We invert the first operand
3875          unless the second operand is a TRUTH_NOT_EXPR in which case our
3876          result is the XOR of the first operand with the inside of the
3877          negation of the second operand.  */
3878
3879       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3880         t = build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3881                     TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3882       else
3883         t = build2 (TRUTH_XOR_EXPR, type,
3884                     invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3885                     TREE_OPERAND (arg, 1));
3886       break;
3887
3888     case TRUTH_ANDIF_EXPR:
3889       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3890       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3891       if (loc1 == UNKNOWN_LOCATION)
3892         loc1 = loc;
3893       if (loc2 == UNKNOWN_LOCATION)
3894         loc2 = loc;
3895       t = build2 (TRUTH_ORIF_EXPR, type,
3896                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3897                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3898       break;
3899
3900     case TRUTH_ORIF_EXPR:
3901       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3902       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3903       if (loc1 == UNKNOWN_LOCATION)
3904         loc1 = loc;
3905       if (loc2 == UNKNOWN_LOCATION)
3906         loc2 = loc;
3907       t = build2 (TRUTH_ANDIF_EXPR, type,
3908                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3909                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3910       break;
3911
3912     case TRUTH_NOT_EXPR:
3913       return TREE_OPERAND (arg, 0);
3914
3915     case COND_EXPR:
3916       {
3917         tree arg1 = TREE_OPERAND (arg, 1);
3918         tree arg2 = TREE_OPERAND (arg, 2);
3919
3920         loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3921         loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 2));
3922         if (loc1 == UNKNOWN_LOCATION)
3923           loc1 = loc;
3924         if (loc2 == UNKNOWN_LOCATION)
3925           loc2 = loc;
3926
3927         /* A COND_EXPR may have a throw as one operand, which
3928            then has void type.  Just leave void operands
3929            as they are.  */
3930         t = build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3931                     VOID_TYPE_P (TREE_TYPE (arg1))
3932                     ? arg1 : invert_truthvalue_loc (loc1, arg1),
3933                     VOID_TYPE_P (TREE_TYPE (arg2))
3934                     ? arg2 : invert_truthvalue_loc (loc2, arg2));
3935         break;
3936       }
3937
3938     case COMPOUND_EXPR:
3939       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3940       if (loc1 == UNKNOWN_LOCATION)
3941         loc1 = loc;
3942       t = build2 (COMPOUND_EXPR, type,
3943                   TREE_OPERAND (arg, 0),
3944                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3945       break;
3946
3947     case NON_LVALUE_EXPR:
3948       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3949       if (loc1 == UNKNOWN_LOCATION)
3950         loc1 = loc;
3951       return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3952
3953     CASE_CONVERT:
3954       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3955         {
3956           t = build1 (TRUTH_NOT_EXPR, type, arg);
3957           break;
3958         }
3959
3960       /* ... fall through ...  */
3961
3962     case FLOAT_EXPR:
3963       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3964       if (loc1 == UNKNOWN_LOCATION)
3965         loc1 = loc;
3966       t = build1 (TREE_CODE (arg), type,
3967                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3968       break;
3969
3970     case BIT_AND_EXPR:
3971       if (!integer_onep (TREE_OPERAND (arg, 1)))
3972         return NULL_TREE;
3973       t = build2 (EQ_EXPR, type, arg, build_int_cst (type, 0));
3974       break;
3975
3976     case SAVE_EXPR:
3977       t = build1 (TRUTH_NOT_EXPR, type, arg);
3978       break;
3979
3980     case CLEANUP_POINT_EXPR:
3981       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3982       if (loc1 == UNKNOWN_LOCATION)
3983         loc1 = loc;
3984       t = build1 (CLEANUP_POINT_EXPR, type,
3985                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3986       break;
3987
3988     default:
3989       t = NULL_TREE;
3990       break;
3991     }
3992
3993   if (t)
3994     SET_EXPR_LOCATION (t, loc);
3995
3996   return t;
3997 }
3998
3999 /* Return a simplified tree node for the truth-negation of ARG.  This
4000    never alters ARG itself.  We assume that ARG is an operation that
4001    returns a truth value (0 or 1).
4002
4003    FIXME: one would think we would fold the result, but it causes
4004    problems with the dominator optimizer.  */
4005
4006 tree
4007 invert_truthvalue_loc (location_t loc, tree arg)
4008 {
4009   tree tem;
4010
4011   if (TREE_CODE (arg) == ERROR_MARK)
4012     return arg;
4013
4014   tem = fold_truth_not_expr (loc, arg);
4015   if (!tem)
4016     {
4017       tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
4018       SET_EXPR_LOCATION (tem, loc);
4019     }
4020
4021   return tem;
4022 }
4023
4024 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
4025    operands are another bit-wise operation with a common input.  If so,
4026    distribute the bit operations to save an operation and possibly two if
4027    constants are involved.  For example, convert
4028         (A | B) & (A | C) into A | (B & C)
4029    Further simplification will occur if B and C are constants.
4030
4031    If this optimization cannot be done, 0 will be returned.  */
4032
4033 static tree
4034 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
4035                      tree arg0, tree arg1)
4036 {
4037   tree common;
4038   tree left, right;
4039
4040   if (TREE_CODE (arg0) != TREE_CODE (arg1)
4041       || TREE_CODE (arg0) == code
4042       || (TREE_CODE (arg0) != BIT_AND_EXPR
4043           && TREE_CODE (arg0) != BIT_IOR_EXPR))
4044     return 0;
4045
4046   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
4047     {
4048       common = TREE_OPERAND (arg0, 0);
4049       left = TREE_OPERAND (arg0, 1);
4050       right = TREE_OPERAND (arg1, 1);
4051     }
4052   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
4053     {
4054       common = TREE_OPERAND (arg0, 0);
4055       left = TREE_OPERAND (arg0, 1);
4056       right = TREE_OPERAND (arg1, 0);
4057     }
4058   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
4059     {
4060       common = TREE_OPERAND (arg0, 1);
4061       left = TREE_OPERAND (arg0, 0);
4062       right = TREE_OPERAND (arg1, 1);
4063     }
4064   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
4065     {
4066       common = TREE_OPERAND (arg0, 1);
4067       left = TREE_OPERAND (arg0, 0);
4068       right = TREE_OPERAND (arg1, 0);
4069     }
4070   else
4071     return 0;
4072
4073   common = fold_convert_loc (loc, type, common);
4074   left = fold_convert_loc (loc, type, left);
4075   right = fold_convert_loc (loc, type, right);
4076   return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
4077                       fold_build2_loc (loc, code, type, left, right));
4078 }
4079
4080 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
4081    with code CODE.  This optimization is unsafe.  */
4082 static tree
4083 distribute_real_division (location_t loc, enum tree_code code, tree type,
4084                           tree arg0, tree arg1)
4085 {
4086   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
4087   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
4088
4089   /* (A / C) +- (B / C) -> (A +- B) / C.  */
4090   if (mul0 == mul1
4091       && operand_equal_p (TREE_OPERAND (arg0, 1),
4092                        TREE_OPERAND (arg1, 1), 0))
4093     return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
4094                         fold_build2_loc (loc, code, type,
4095                                      TREE_OPERAND (arg0, 0),
4096                                      TREE_OPERAND (arg1, 0)),
4097                         TREE_OPERAND (arg0, 1));
4098
4099   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
4100   if (operand_equal_p (TREE_OPERAND (arg0, 0),
4101                        TREE_OPERAND (arg1, 0), 0)
4102       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
4103       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
4104     {
4105       REAL_VALUE_TYPE r0, r1;
4106       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
4107       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
4108       if (!mul0)
4109         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
4110       if (!mul1)
4111         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
4112       real_arithmetic (&r0, code, &r0, &r1);
4113       return fold_build2_loc (loc, MULT_EXPR, type,
4114                           TREE_OPERAND (arg0, 0),
4115                           build_real (type, r0));
4116     }
4117
4118   return NULL_TREE;
4119 }
4120 \f
4121 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4122    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
4123
4124 static tree
4125 make_bit_field_ref (location_t loc, tree inner, tree type,
4126                     HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
4127 {
4128   tree result, bftype;
4129
4130   if (bitpos == 0)
4131     {
4132       tree size = TYPE_SIZE (TREE_TYPE (inner));
4133       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4134            || POINTER_TYPE_P (TREE_TYPE (inner)))
4135           && host_integerp (size, 0)
4136           && tree_low_cst (size, 0) == bitsize)
4137         return fold_convert_loc (loc, type, inner);
4138     }
4139
4140   bftype = type;
4141   if (TYPE_PRECISION (bftype) != bitsize
4142       || TYPE_UNSIGNED (bftype) == !unsignedp)
4143     bftype = build_nonstandard_integer_type (bitsize, 0);
4144
4145   result = build3 (BIT_FIELD_REF, bftype, inner,
4146                    size_int (bitsize), bitsize_int (bitpos));
4147   SET_EXPR_LOCATION (result, loc);
4148
4149   if (bftype != type)
4150     result = fold_convert_loc (loc, type, result);
4151
4152   return result;
4153 }
4154
4155 /* Optimize a bit-field compare.
4156
4157    There are two cases:  First is a compare against a constant and the
4158    second is a comparison of two items where the fields are at the same
4159    bit position relative to the start of a chunk (byte, halfword, word)
4160    large enough to contain it.  In these cases we can avoid the shift
4161    implicit in bitfield extractions.
4162
4163    For constants, we emit a compare of the shifted constant with the
4164    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4165    compared.  For two fields at the same position, we do the ANDs with the
4166    similar mask and compare the result of the ANDs.
4167
4168    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4169    COMPARE_TYPE is the type of the comparison, and LHS and RHS
4170    are the left and right operands of the comparison, respectively.
4171
4172    If the optimization described above can be done, we return the resulting
4173    tree.  Otherwise we return zero.  */
4174
4175 static tree
4176 optimize_bit_field_compare (location_t loc, enum tree_code code,
4177                             tree compare_type, tree lhs, tree rhs)
4178 {
4179   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
4180   tree type = TREE_TYPE (lhs);
4181   tree signed_type, unsigned_type;
4182   int const_p = TREE_CODE (rhs) == INTEGER_CST;
4183   enum machine_mode lmode, rmode, nmode;
4184   int lunsignedp, runsignedp;
4185   int lvolatilep = 0, rvolatilep = 0;
4186   tree linner, rinner = NULL_TREE;
4187   tree mask;
4188   tree offset;
4189
4190   /* Get all the information about the extractions being done.  If the bit size
4191      if the same as the size of the underlying object, we aren't doing an
4192      extraction at all and so can do nothing.  We also don't want to
4193      do anything if the inner expression is a PLACEHOLDER_EXPR since we
4194      then will no longer be able to replace it.  */
4195   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
4196                                 &lunsignedp, &lvolatilep, false);
4197   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
4198       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
4199     return 0;
4200
4201  if (!const_p)
4202    {
4203      /* If this is not a constant, we can only do something if bit positions,
4204         sizes, and signedness are the same.  */
4205      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4206                                    &runsignedp, &rvolatilep, false);
4207
4208      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
4209          || lunsignedp != runsignedp || offset != 0
4210          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
4211        return 0;
4212    }
4213
4214   /* See if we can find a mode to refer to this field.  We should be able to,
4215      but fail if we can't.  */
4216   nmode = get_best_mode (lbitsize, lbitpos,
4217                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4218                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4219                                 TYPE_ALIGN (TREE_TYPE (rinner))),
4220                          word_mode, lvolatilep || rvolatilep);
4221   if (nmode == VOIDmode)
4222     return 0;
4223
4224   /* Set signed and unsigned types of the precision of this mode for the
4225      shifts below.  */
4226   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
4227   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4228
4229   /* Compute the bit position and size for the new reference and our offset
4230      within it. If the new reference is the same size as the original, we
4231      won't optimize anything, so return zero.  */
4232   nbitsize = GET_MODE_BITSIZE (nmode);
4233   nbitpos = lbitpos & ~ (nbitsize - 1);
4234   lbitpos -= nbitpos;
4235   if (nbitsize == lbitsize)
4236     return 0;
4237
4238   if (BYTES_BIG_ENDIAN)
4239     lbitpos = nbitsize - lbitsize - lbitpos;
4240
4241   /* Make the mask to be used against the extracted field.  */
4242   mask = build_int_cst_type (unsigned_type, -1);
4243   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
4244   mask = const_binop (RSHIFT_EXPR, mask,
4245                       size_int (nbitsize - lbitsize - lbitpos), 0);
4246
4247   if (! const_p)
4248     /* If not comparing with constant, just rework the comparison
4249        and return.  */
4250     return fold_build2_loc (loc, code, compare_type,
4251                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4252                                      make_bit_field_ref (loc, linner,
4253                                                          unsigned_type,
4254                                                          nbitsize, nbitpos,
4255                                                          1),
4256                                      mask),
4257                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4258                                      make_bit_field_ref (loc, rinner,
4259                                                          unsigned_type,
4260                                                          nbitsize, nbitpos,
4261                                                          1),
4262                                      mask));
4263
4264   /* Otherwise, we are handling the constant case. See if the constant is too
4265      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
4266      this not only for its own sake, but to avoid having to test for this
4267      error case below.  If we didn't, we might generate wrong code.
4268
4269      For unsigned fields, the constant shifted right by the field length should
4270      be all zero.  For signed fields, the high-order bits should agree with
4271      the sign bit.  */
4272
4273   if (lunsignedp)
4274     {
4275       if (! integer_zerop (const_binop (RSHIFT_EXPR,
4276                                         fold_convert_loc (loc,
4277                                                           unsigned_type, rhs),
4278                                         size_int (lbitsize), 0)))
4279         {
4280           warning (0, "comparison is always %d due to width of bit-field",
4281                    code == NE_EXPR);
4282           return constant_boolean_node (code == NE_EXPR, compare_type);
4283         }
4284     }
4285   else
4286     {
4287       tree tem = const_binop (RSHIFT_EXPR,
4288                               fold_convert_loc (loc, signed_type, rhs),
4289                               size_int (lbitsize - 1), 0);
4290       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
4291         {
4292           warning (0, "comparison is always %d due to width of bit-field",
4293                    code == NE_EXPR);
4294           return constant_boolean_node (code == NE_EXPR, compare_type);
4295         }
4296     }
4297
4298   /* Single-bit compares should always be against zero.  */
4299   if (lbitsize == 1 && ! integer_zerop (rhs))
4300     {
4301       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4302       rhs = build_int_cst (type, 0);
4303     }
4304
4305   /* Make a new bitfield reference, shift the constant over the
4306      appropriate number of bits and mask it with the computed mask
4307      (in case this was a signed field).  If we changed it, make a new one.  */
4308   lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
4309   if (lvolatilep)
4310     {
4311       TREE_SIDE_EFFECTS (lhs) = 1;
4312       TREE_THIS_VOLATILE (lhs) = 1;
4313     }
4314
4315   rhs = const_binop (BIT_AND_EXPR,
4316                      const_binop (LSHIFT_EXPR,
4317                                   fold_convert_loc (loc, unsigned_type, rhs),
4318                                   size_int (lbitpos), 0),
4319                      mask, 0);
4320
4321   lhs = build2 (code, compare_type,
4322                 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4323                 rhs);
4324   SET_EXPR_LOCATION (lhs, loc);
4325   return lhs;
4326 }
4327 \f
4328 /* Subroutine for fold_truthop: decode a field reference.
4329
4330    If EXP is a comparison reference, we return the innermost reference.
4331
4332    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4333    set to the starting bit number.
4334
4335    If the innermost field can be completely contained in a mode-sized
4336    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
4337
4338    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4339    otherwise it is not changed.
4340
4341    *PUNSIGNEDP is set to the signedness of the field.
4342
4343    *PMASK is set to the mask used.  This is either contained in a
4344    BIT_AND_EXPR or derived from the width of the field.
4345
4346    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4347
4348    Return 0 if this is not a component reference or is one that we can't
4349    do anything with.  */
4350
4351 static tree
4352 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
4353                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4354                         int *punsignedp, int *pvolatilep,
4355                         tree *pmask, tree *pand_mask)
4356 {
4357   tree outer_type = 0;
4358   tree and_mask = 0;
4359   tree mask, inner, offset;
4360   tree unsigned_type;
4361   unsigned int precision;
4362
4363   /* All the optimizations using this function assume integer fields.
4364      There are problems with FP fields since the type_for_size call
4365      below can fail for, e.g., XFmode.  */
4366   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4367     return 0;
4368
4369   /* We are interested in the bare arrangement of bits, so strip everything
4370      that doesn't affect the machine mode.  However, record the type of the
4371      outermost expression if it may matter below.  */
4372   if (CONVERT_EXPR_P (exp)
4373       || TREE_CODE (exp) == NON_LVALUE_EXPR)
4374     outer_type = TREE_TYPE (exp);
4375   STRIP_NOPS (exp);
4376
4377   if (TREE_CODE (exp) == BIT_AND_EXPR)
4378     {
4379       and_mask = TREE_OPERAND (exp, 1);
4380       exp = TREE_OPERAND (exp, 0);
4381       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4382       if (TREE_CODE (and_mask) != INTEGER_CST)
4383         return 0;
4384     }
4385
4386   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4387                                punsignedp, pvolatilep, false);
4388   if ((inner == exp && and_mask == 0)
4389       || *pbitsize < 0 || offset != 0
4390       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4391     return 0;
4392
4393   /* If the number of bits in the reference is the same as the bitsize of
4394      the outer type, then the outer type gives the signedness. Otherwise
4395      (in case of a small bitfield) the signedness is unchanged.  */
4396   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4397     *punsignedp = TYPE_UNSIGNED (outer_type);
4398
4399   /* Compute the mask to access the bitfield.  */
4400   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4401   precision = TYPE_PRECISION (unsigned_type);
4402
4403   mask = build_int_cst_type (unsigned_type, -1);
4404
4405   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4406   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4407
4408   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
4409   if (and_mask != 0)
4410     mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4411                         fold_convert_loc (loc, unsigned_type, and_mask), mask);
4412
4413   *pmask = mask;
4414   *pand_mask = and_mask;
4415   return inner;
4416 }
4417
4418 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4419    bit positions.  */
4420
4421 static int
4422 all_ones_mask_p (const_tree mask, int size)
4423 {
4424   tree type = TREE_TYPE (mask);
4425   unsigned int precision = TYPE_PRECISION (type);
4426   tree tmask;
4427
4428   tmask = build_int_cst_type (signed_type_for (type), -1);
4429
4430   return
4431     tree_int_cst_equal (mask,
4432                         const_binop (RSHIFT_EXPR,
4433                                      const_binop (LSHIFT_EXPR, tmask,
4434                                                   size_int (precision - size),
4435                                                   0),
4436                                      size_int (precision - size), 0));
4437 }
4438
4439 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4440    represents the sign bit of EXP's type.  If EXP represents a sign
4441    or zero extension, also test VAL against the unextended type.
4442    The return value is the (sub)expression whose sign bit is VAL,
4443    or NULL_TREE otherwise.  */
4444
4445 static tree
4446 sign_bit_p (tree exp, const_tree val)
4447 {
4448   unsigned HOST_WIDE_INT mask_lo, lo;
4449   HOST_WIDE_INT mask_hi, hi;
4450   int width;
4451   tree t;
4452
4453   /* Tree EXP must have an integral type.  */
4454   t = TREE_TYPE (exp);
4455   if (! INTEGRAL_TYPE_P (t))
4456     return NULL_TREE;
4457
4458   /* Tree VAL must be an integer constant.  */
4459   if (TREE_CODE (val) != INTEGER_CST
4460       || TREE_OVERFLOW (val))
4461     return NULL_TREE;
4462
4463   width = TYPE_PRECISION (t);
4464   if (width > HOST_BITS_PER_WIDE_INT)
4465     {
4466       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4467       lo = 0;
4468
4469       mask_hi = ((unsigned HOST_WIDE_INT) -1
4470                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
4471       mask_lo = -1;
4472     }
4473   else
4474     {
4475       hi = 0;
4476       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4477
4478       mask_hi = 0;
4479       mask_lo = ((unsigned HOST_WIDE_INT) -1
4480                  >> (HOST_BITS_PER_WIDE_INT - width));
4481     }
4482
4483   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4484      treat VAL as if it were unsigned.  */
4485   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4486       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4487     return exp;
4488
4489   /* Handle extension from a narrower type.  */
4490   if (TREE_CODE (exp) == NOP_EXPR
4491       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4492     return sign_bit_p (TREE_OPERAND (exp, 0), val);
4493
4494   return NULL_TREE;
4495 }
4496
4497 /* Subroutine for fold_truthop: determine if an operand is simple enough
4498    to be evaluated unconditionally.  */
4499
4500 static int
4501 simple_operand_p (const_tree exp)
4502 {
4503   /* Strip any conversions that don't change the machine mode.  */
4504   STRIP_NOPS (exp);
4505
4506   return (CONSTANT_CLASS_P (exp)
4507           || TREE_CODE (exp) == SSA_NAME
4508           || (DECL_P (exp)
4509               && ! TREE_ADDRESSABLE (exp)
4510               && ! TREE_THIS_VOLATILE (exp)
4511               && ! DECL_NONLOCAL (exp)
4512               /* Don't regard global variables as simple.  They may be
4513                  allocated in ways unknown to the compiler (shared memory,
4514                  #pragma weak, etc).  */
4515               && ! TREE_PUBLIC (exp)
4516               && ! DECL_EXTERNAL (exp)
4517               /* Loading a static variable is unduly expensive, but global
4518                  registers aren't expensive.  */
4519               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4520 }
4521 \f
4522 /* The following functions are subroutines to fold_range_test and allow it to
4523    try to change a logical combination of comparisons into a range test.
4524
4525    For example, both
4526         X == 2 || X == 3 || X == 4 || X == 5
4527    and
4528         X >= 2 && X <= 5
4529    are converted to
4530         (unsigned) (X - 2) <= 3
4531
4532    We describe each set of comparisons as being either inside or outside
4533    a range, using a variable named like IN_P, and then describe the
4534    range with a lower and upper bound.  If one of the bounds is omitted,
4535    it represents either the highest or lowest value of the type.
4536
4537    In the comments below, we represent a range by two numbers in brackets
4538    preceded by a "+" to designate being inside that range, or a "-" to
4539    designate being outside that range, so the condition can be inverted by
4540    flipping the prefix.  An omitted bound is represented by a "-".  For
4541    example, "- [-, 10]" means being outside the range starting at the lowest
4542    possible value and ending at 10, in other words, being greater than 10.
4543    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4544    always false.
4545
4546    We set up things so that the missing bounds are handled in a consistent
4547    manner so neither a missing bound nor "true" and "false" need to be
4548    handled using a special case.  */
4549
4550 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4551    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4552    and UPPER1_P are nonzero if the respective argument is an upper bound
4553    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
4554    must be specified for a comparison.  ARG1 will be converted to ARG0's
4555    type if both are specified.  */
4556
4557 static tree
4558 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4559              tree arg1, int upper1_p)
4560 {
4561   tree tem;
4562   int result;
4563   int sgn0, sgn1;
4564
4565   /* If neither arg represents infinity, do the normal operation.
4566      Else, if not a comparison, return infinity.  Else handle the special
4567      comparison rules. Note that most of the cases below won't occur, but
4568      are handled for consistency.  */
4569
4570   if (arg0 != 0 && arg1 != 0)
4571     {
4572       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4573                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
4574       STRIP_NOPS (tem);
4575       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4576     }
4577
4578   if (TREE_CODE_CLASS (code) != tcc_comparison)
4579     return 0;
4580
4581   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4582      for neither.  In real maths, we cannot assume open ended ranges are
4583      the same. But, this is computer arithmetic, where numbers are finite.
4584      We can therefore make the transformation of any unbounded range with
4585      the value Z, Z being greater than any representable number. This permits
4586      us to treat unbounded ranges as equal.  */
4587   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4588   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4589   switch (code)
4590     {
4591     case EQ_EXPR:
4592       result = sgn0 == sgn1;
4593       break;
4594     case NE_EXPR:
4595       result = sgn0 != sgn1;
4596       break;
4597     case LT_EXPR:
4598       result = sgn0 < sgn1;
4599       break;
4600     case LE_EXPR:
4601       result = sgn0 <= sgn1;
4602       break;
4603     case GT_EXPR:
4604       result = sgn0 > sgn1;
4605       break;
4606     case GE_EXPR:
4607       result = sgn0 >= sgn1;
4608       break;
4609     default:
4610       gcc_unreachable ();
4611     }
4612
4613   return constant_boolean_node (result, type);
4614 }
4615 \f
4616 /* Given EXP, a logical expression, set the range it is testing into
4617    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4618    actually being tested.  *PLOW and *PHIGH will be made of the same
4619    type as the returned expression.  If EXP is not a comparison, we
4620    will most likely not be returning a useful value and range.  Set
4621    *STRICT_OVERFLOW_P to true if the return value is only valid
4622    because signed overflow is undefined; otherwise, do not change
4623    *STRICT_OVERFLOW_P.  */
4624
4625 tree
4626 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4627             bool *strict_overflow_p)
4628 {
4629   enum tree_code code;
4630   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4631   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4632   int in_p, n_in_p;
4633   tree low, high, n_low, n_high;
4634   location_t loc = EXPR_LOCATION (exp);
4635
4636   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4637      and see if we can refine the range.  Some of the cases below may not
4638      happen, but it doesn't seem worth worrying about this.  We "continue"
4639      the outer loop when we've changed something; otherwise we "break"
4640      the switch, which will "break" the while.  */
4641
4642   in_p = 0;
4643   low = high = build_int_cst (TREE_TYPE (exp), 0);
4644
4645   while (1)
4646     {
4647       code = TREE_CODE (exp);
4648       exp_type = TREE_TYPE (exp);
4649
4650       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4651         {
4652           if (TREE_OPERAND_LENGTH (exp) > 0)
4653             arg0 = TREE_OPERAND (exp, 0);
4654           if (TREE_CODE_CLASS (code) == tcc_comparison
4655               || TREE_CODE_CLASS (code) == tcc_unary
4656               || TREE_CODE_CLASS (code) == tcc_binary)
4657             arg0_type = TREE_TYPE (arg0);
4658           if (TREE_CODE_CLASS (code) == tcc_binary
4659               || TREE_CODE_CLASS (code) == tcc_comparison
4660               || (TREE_CODE_CLASS (code) == tcc_expression
4661                   && TREE_OPERAND_LENGTH (exp) > 1))
4662             arg1 = TREE_OPERAND (exp, 1);
4663         }
4664
4665       switch (code)
4666         {
4667         case TRUTH_NOT_EXPR:
4668           in_p = ! in_p, exp = arg0;
4669           continue;
4670
4671         case EQ_EXPR: case NE_EXPR:
4672         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4673           /* We can only do something if the range is testing for zero
4674              and if the second operand is an integer constant.  Note that
4675              saying something is "in" the range we make is done by
4676              complementing IN_P since it will set in the initial case of
4677              being not equal to zero; "out" is leaving it alone.  */
4678           if (low == 0 || high == 0
4679               || ! integer_zerop (low) || ! integer_zerop (high)
4680               || TREE_CODE (arg1) != INTEGER_CST)
4681             break;
4682
4683           switch (code)
4684             {
4685             case NE_EXPR:  /* - [c, c]  */
4686               low = high = arg1;
4687               break;
4688             case EQ_EXPR:  /* + [c, c]  */
4689               in_p = ! in_p, low = high = arg1;
4690               break;
4691             case GT_EXPR:  /* - [-, c] */
4692               low = 0, high = arg1;
4693               break;
4694             case GE_EXPR:  /* + [c, -] */
4695               in_p = ! in_p, low = arg1, high = 0;
4696               break;
4697             case LT_EXPR:  /* - [c, -] */
4698               low = arg1, high = 0;
4699               break;
4700             case LE_EXPR:  /* + [-, c] */
4701               in_p = ! in_p, low = 0, high = arg1;
4702               break;
4703             default:
4704               gcc_unreachable ();
4705             }
4706
4707           /* If this is an unsigned comparison, we also know that EXP is
4708              greater than or equal to zero.  We base the range tests we make
4709              on that fact, so we record it here so we can parse existing
4710              range tests.  We test arg0_type since often the return type
4711              of, e.g. EQ_EXPR, is boolean.  */
4712           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4713             {
4714               if (! merge_ranges (&n_in_p, &n_low, &n_high,
4715                                   in_p, low, high, 1,
4716                                   build_int_cst (arg0_type, 0),
4717                                   NULL_TREE))
4718                 break;
4719
4720               in_p = n_in_p, low = n_low, high = n_high;
4721
4722               /* If the high bound is missing, but we have a nonzero low
4723                  bound, reverse the range so it goes from zero to the low bound
4724                  minus 1.  */
4725               if (high == 0 && low && ! integer_zerop (low))
4726                 {
4727                   in_p = ! in_p;
4728                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4729                                       integer_one_node, 0);
4730                   low = build_int_cst (arg0_type, 0);
4731                 }
4732             }
4733
4734           exp = arg0;
4735           continue;
4736
4737         case NEGATE_EXPR:
4738           /* (-x) IN [a,b] -> x in [-b, -a]  */
4739           n_low = range_binop (MINUS_EXPR, exp_type,
4740                                build_int_cst (exp_type, 0),
4741                                0, high, 1);
4742           n_high = range_binop (MINUS_EXPR, exp_type,
4743                                 build_int_cst (exp_type, 0),
4744                                 0, low, 0);
4745           low = n_low, high = n_high;
4746           exp = arg0;
4747           continue;
4748
4749         case BIT_NOT_EXPR:
4750           /* ~ X -> -X - 1  */
4751           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4752                         build_int_cst (exp_type, 1));
4753           SET_EXPR_LOCATION (exp, loc);
4754           continue;
4755
4756         case PLUS_EXPR:  case MINUS_EXPR:
4757           if (TREE_CODE (arg1) != INTEGER_CST)
4758             break;
4759
4760           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4761              move a constant to the other side.  */
4762           if (!TYPE_UNSIGNED (arg0_type)
4763               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4764             break;
4765
4766           /* If EXP is signed, any overflow in the computation is undefined,
4767              so we don't worry about it so long as our computations on
4768              the bounds don't overflow.  For unsigned, overflow is defined
4769              and this is exactly the right thing.  */
4770           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4771                                arg0_type, low, 0, arg1, 0);
4772           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4773                                 arg0_type, high, 1, arg1, 0);
4774           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4775               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4776             break;
4777
4778           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4779             *strict_overflow_p = true;
4780
4781           /* Check for an unsigned range which has wrapped around the maximum
4782              value thus making n_high < n_low, and normalize it.  */
4783           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4784             {
4785               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4786                                  integer_one_node, 0);
4787               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4788                                   integer_one_node, 0);
4789
4790               /* If the range is of the form +/- [ x+1, x ], we won't
4791                  be able to normalize it.  But then, it represents the
4792                  whole range or the empty set, so make it
4793                  +/- [ -, - ].  */
4794               if (tree_int_cst_equal (n_low, low)
4795                   && tree_int_cst_equal (n_high, high))
4796                 low = high = 0;
4797               else
4798                 in_p = ! in_p;
4799             }
4800           else
4801             low = n_low, high = n_high;
4802
4803           exp = arg0;
4804           continue;
4805
4806         CASE_CONVERT: case NON_LVALUE_EXPR:
4807           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4808             break;
4809
4810           if (! INTEGRAL_TYPE_P (arg0_type)
4811               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4812               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4813             break;
4814
4815           n_low = low, n_high = high;
4816
4817           if (n_low != 0)
4818             n_low = fold_convert_loc (loc, arg0_type, n_low);
4819
4820           if (n_high != 0)
4821             n_high = fold_convert_loc (loc, arg0_type, n_high);
4822
4823
4824           /* If we're converting arg0 from an unsigned type, to exp,
4825              a signed type,  we will be doing the comparison as unsigned.
4826              The tests above have already verified that LOW and HIGH
4827              are both positive.
4828
4829              So we have to ensure that we will handle large unsigned
4830              values the same way that the current signed bounds treat
4831              negative values.  */
4832
4833           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4834             {
4835               tree high_positive;
4836               tree equiv_type;
4837               /* For fixed-point modes, we need to pass the saturating flag
4838                  as the 2nd parameter.  */
4839               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4840                 equiv_type = lang_hooks.types.type_for_mode
4841                              (TYPE_MODE (arg0_type),
4842                               TYPE_SATURATING (arg0_type));
4843               else
4844                 equiv_type = lang_hooks.types.type_for_mode
4845                              (TYPE_MODE (arg0_type), 1);
4846
4847               /* A range without an upper bound is, naturally, unbounded.
4848                  Since convert would have cropped a very large value, use
4849                  the max value for the destination type.  */
4850               high_positive
4851                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4852                 : TYPE_MAX_VALUE (arg0_type);
4853
4854               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4855                 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4856                                              fold_convert_loc (loc, arg0_type,
4857                                                                high_positive),
4858                                              build_int_cst (arg0_type, 1));
4859
4860               /* If the low bound is specified, "and" the range with the
4861                  range for which the original unsigned value will be
4862                  positive.  */
4863               if (low != 0)
4864                 {
4865                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4866                                       1, n_low, n_high, 1,
4867                                       fold_convert_loc (loc, arg0_type,
4868                                                         integer_zero_node),
4869                                       high_positive))
4870                     break;
4871
4872                   in_p = (n_in_p == in_p);
4873                 }
4874               else
4875                 {
4876                   /* Otherwise, "or" the range with the range of the input
4877                      that will be interpreted as negative.  */
4878                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4879                                       0, n_low, n_high, 1,
4880                                       fold_convert_loc (loc, arg0_type,
4881                                                         integer_zero_node),
4882                                       high_positive))
4883                     break;
4884
4885                   in_p = (in_p != n_in_p);
4886                 }
4887             }
4888
4889           exp = arg0;
4890           low = n_low, high = n_high;
4891           continue;
4892
4893         default:
4894           break;
4895         }
4896
4897       break;
4898     }
4899
4900   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4901   if (TREE_CODE (exp) == INTEGER_CST)
4902     {
4903       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4904                                                  exp, 0, low, 0))
4905                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4906                                                     exp, 1, high, 1)));
4907       low = high = 0;
4908       exp = 0;
4909     }
4910
4911   *pin_p = in_p, *plow = low, *phigh = high;
4912   return exp;
4913 }
4914 \f
4915 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4916    type, TYPE, return an expression to test if EXP is in (or out of, depending
4917    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4918
4919 tree
4920 build_range_check (location_t loc, tree type, tree exp, int in_p,
4921                    tree low, tree high)
4922 {
4923   tree etype = TREE_TYPE (exp), value;
4924
4925 #ifdef HAVE_canonicalize_funcptr_for_compare
4926   /* Disable this optimization for function pointer expressions
4927      on targets that require function pointer canonicalization.  */
4928   if (HAVE_canonicalize_funcptr_for_compare
4929       && TREE_CODE (etype) == POINTER_TYPE
4930       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4931     return NULL_TREE;
4932 #endif
4933
4934   if (! in_p)
4935     {
4936       value = build_range_check (loc, type, exp, 1, low, high);
4937       if (value != 0)
4938         return invert_truthvalue_loc (loc, value);
4939
4940       return 0;
4941     }
4942
4943   if (low == 0 && high == 0)
4944     return build_int_cst (type, 1);
4945
4946   if (low == 0)
4947     return fold_build2_loc (loc, LE_EXPR, type, exp,
4948                         fold_convert_loc (loc, etype, high));
4949
4950   if (high == 0)
4951     return fold_build2_loc (loc, GE_EXPR, type, exp,
4952                         fold_convert_loc (loc, etype, low));
4953
4954   if (operand_equal_p (low, high, 0))
4955     return fold_build2_loc (loc, EQ_EXPR, type, exp,
4956                         fold_convert_loc (loc, etype, low));
4957
4958   if (integer_zerop (low))
4959     {
4960       if (! TYPE_UNSIGNED (etype))
4961         {
4962           etype = unsigned_type_for (etype);
4963           high = fold_convert_loc (loc, etype, high);
4964           exp = fold_convert_loc (loc, etype, exp);
4965         }
4966       return build_range_check (loc, type, exp, 1, 0, high);
4967     }
4968
4969   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4970   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4971     {
4972       unsigned HOST_WIDE_INT lo;
4973       HOST_WIDE_INT hi;
4974       int prec;
4975
4976       prec = TYPE_PRECISION (etype);
4977       if (prec <= HOST_BITS_PER_WIDE_INT)
4978         {
4979           hi = 0;
4980           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4981         }
4982       else
4983         {
4984           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4985           lo = (unsigned HOST_WIDE_INT) -1;
4986         }
4987
4988       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4989         {
4990           if (TYPE_UNSIGNED (etype))
4991             {
4992               tree signed_etype = signed_type_for (etype);
4993               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4994                 etype
4995                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4996               else
4997                 etype = signed_etype;
4998               exp = fold_convert_loc (loc, etype, exp);
4999             }
5000           return fold_build2_loc (loc, GT_EXPR, type, exp,
5001                               build_int_cst (etype, 0));
5002         }
5003     }
5004
5005   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5006      This requires wrap-around arithmetics for the type of the expression.
5007      First make sure that arithmetics in this type is valid, then make sure
5008      that it wraps around.  */
5009   if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5010     etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
5011                                             TYPE_UNSIGNED (etype));
5012
5013   if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
5014     {
5015       tree utype, minv, maxv;
5016
5017       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5018          for the type in question, as we rely on this here.  */
5019       utype = unsigned_type_for (etype);
5020       maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
5021       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5022                           integer_one_node, 1);
5023       minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
5024
5025       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5026                                       minv, 1, maxv, 1)))
5027         etype = utype;
5028       else
5029         return 0;
5030     }
5031
5032   high = fold_convert_loc (loc, etype, high);
5033   low = fold_convert_loc (loc, etype, low);
5034   exp = fold_convert_loc (loc, etype, exp);
5035
5036   value = const_binop (MINUS_EXPR, high, low, 0);
5037
5038
5039   if (POINTER_TYPE_P (etype))
5040     {
5041       if (value != 0 && !TREE_OVERFLOW (value))
5042         {
5043           low = fold_convert_loc (loc, sizetype, low);
5044           low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low);
5045           return build_range_check (loc, type,
5046                                     fold_build2_loc (loc, POINTER_PLUS_EXPR,
5047                                                  etype, exp, low),
5048                                     1, build_int_cst (etype, 0), value);
5049         }
5050       return 0;
5051     }
5052
5053   if (value != 0 && !TREE_OVERFLOW (value))
5054     return build_range_check (loc, type,
5055                               fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5056                               1, build_int_cst (etype, 0), value);
5057
5058   return 0;
5059 }
5060 \f
5061 /* Return the predecessor of VAL in its type, handling the infinite case.  */
5062
5063 static tree
5064 range_predecessor (tree val)
5065 {
5066   tree type = TREE_TYPE (val);
5067
5068   if (INTEGRAL_TYPE_P (type)
5069       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5070     return 0;
5071   else
5072     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
5073 }
5074
5075 /* Return the successor of VAL in its type, handling the infinite case.  */
5076
5077 static tree
5078 range_successor (tree val)
5079 {
5080   tree type = TREE_TYPE (val);
5081
5082   if (INTEGRAL_TYPE_P (type)
5083       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5084     return 0;
5085   else
5086     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
5087 }
5088
5089 /* Given two ranges, see if we can merge them into one.  Return 1 if we
5090    can, 0 if we can't.  Set the output range into the specified parameters.  */
5091
5092 bool
5093 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5094               tree high0, int in1_p, tree low1, tree high1)
5095 {
5096   int no_overlap;
5097   int subset;
5098   int temp;
5099   tree tem;
5100   int in_p;
5101   tree low, high;
5102   int lowequal = ((low0 == 0 && low1 == 0)
5103                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5104                                                 low0, 0, low1, 0)));
5105   int highequal = ((high0 == 0 && high1 == 0)
5106                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5107                                                  high0, 1, high1, 1)));
5108
5109   /* Make range 0 be the range that starts first, or ends last if they
5110      start at the same value.  Swap them if it isn't.  */
5111   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5112                                  low0, 0, low1, 0))
5113       || (lowequal
5114           && integer_onep (range_binop (GT_EXPR, integer_type_node,
5115                                         high1, 1, high0, 1))))
5116     {
5117       temp = in0_p, in0_p = in1_p, in1_p = temp;
5118       tem = low0, low0 = low1, low1 = tem;
5119       tem = high0, high0 = high1, high1 = tem;
5120     }
5121
5122   /* Now flag two cases, whether the ranges are disjoint or whether the
5123      second range is totally subsumed in the first.  Note that the tests
5124      below are simplified by the ones above.  */
5125   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5126                                           high0, 1, low1, 0));
5127   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5128                                       high1, 1, high0, 1));
5129
5130   /* We now have four cases, depending on whether we are including or
5131      excluding the two ranges.  */
5132   if (in0_p && in1_p)
5133     {
5134       /* If they don't overlap, the result is false.  If the second range
5135          is a subset it is the result.  Otherwise, the range is from the start
5136          of the second to the end of the first.  */
5137       if (no_overlap)
5138         in_p = 0, low = high = 0;
5139       else if (subset)
5140         in_p = 1, low = low1, high = high1;
5141       else
5142         in_p = 1, low = low1, high = high0;
5143     }
5144
5145   else if (in0_p && ! in1_p)
5146     {
5147       /* If they don't overlap, the result is the first range.  If they are
5148          equal, the result is false.  If the second range is a subset of the
5149          first, and the ranges begin at the same place, we go from just after
5150          the end of the second range to the end of the first.  If the second
5151          range is not a subset of the first, or if it is a subset and both
5152          ranges end at the same place, the range starts at the start of the
5153          first range and ends just before the second range.
5154          Otherwise, we can't describe this as a single range.  */
5155       if (no_overlap)
5156         in_p = 1, low = low0, high = high0;
5157       else if (lowequal && highequal)
5158         in_p = 0, low = high = 0;
5159       else if (subset && lowequal)
5160         {
5161           low = range_successor (high1);
5162           high = high0;
5163           in_p = 1;
5164           if (low == 0)
5165             {
5166               /* We are in the weird situation where high0 > high1 but
5167                  high1 has no successor.  Punt.  */
5168               return 0;
5169             }
5170         }
5171       else if (! subset || highequal)
5172         {
5173           low = low0;
5174           high = range_predecessor (low1);
5175           in_p = 1;
5176           if (high == 0)
5177             {
5178               /* low0 < low1 but low1 has no predecessor.  Punt.  */
5179               return 0;
5180             }
5181         }
5182       else
5183         return 0;
5184     }
5185
5186   else if (! in0_p && in1_p)
5187     {
5188       /* If they don't overlap, the result is the second range.  If the second
5189          is a subset of the first, the result is false.  Otherwise,
5190          the range starts just after the first range and ends at the
5191          end of the second.  */
5192       if (no_overlap)
5193         in_p = 1, low = low1, high = high1;
5194       else if (subset || highequal)
5195         in_p = 0, low = high = 0;
5196       else
5197         {
5198           low = range_successor (high0);
5199           high = high1;
5200           in_p = 1;
5201           if (low == 0)
5202             {
5203               /* high1 > high0 but high0 has no successor.  Punt.  */
5204               return 0;
5205             }
5206         }
5207     }
5208
5209   else
5210     {
5211       /* The case where we are excluding both ranges.  Here the complex case
5212          is if they don't overlap.  In that case, the only time we have a
5213          range is if they are adjacent.  If the second is a subset of the
5214          first, the result is the first.  Otherwise, the range to exclude
5215          starts at the beginning of the first range and ends at the end of the
5216          second.  */
5217       if (no_overlap)
5218         {
5219           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5220                                          range_successor (high0),
5221                                          1, low1, 0)))
5222             in_p = 0, low = low0, high = high1;
5223           else
5224             {
5225               /* Canonicalize - [min, x] into - [-, x].  */
5226               if (low0 && TREE_CODE (low0) == INTEGER_CST)
5227                 switch (TREE_CODE (TREE_TYPE (low0)))
5228                   {
5229                   case ENUMERAL_TYPE:
5230                     if (TYPE_PRECISION (TREE_TYPE (low0))
5231                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
5232                       break;
5233                     /* FALLTHROUGH */
5234                   case INTEGER_TYPE:
5235                     if (tree_int_cst_equal (low0,
5236                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
5237                       low0 = 0;
5238                     break;
5239                   case POINTER_TYPE:
5240                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
5241                         && integer_zerop (low0))
5242                       low0 = 0;
5243                     break;
5244                   default:
5245                     break;
5246                   }
5247
5248               /* Canonicalize - [x, max] into - [x, -].  */
5249               if (high1 && TREE_CODE (high1) == INTEGER_CST)
5250                 switch (TREE_CODE (TREE_TYPE (high1)))
5251                   {
5252                   case ENUMERAL_TYPE:
5253                     if (TYPE_PRECISION (TREE_TYPE (high1))
5254                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5255                       break;
5256                     /* FALLTHROUGH */
5257                   case INTEGER_TYPE:
5258                     if (tree_int_cst_equal (high1,
5259                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
5260                       high1 = 0;
5261                     break;
5262                   case POINTER_TYPE:
5263                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
5264                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5265                                                        high1, 1,
5266                                                        integer_one_node, 1)))
5267                       high1 = 0;
5268                     break;
5269                   default:
5270                     break;
5271                   }
5272
5273               /* The ranges might be also adjacent between the maximum and
5274                  minimum values of the given type.  For
5275                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5276                  return + [x + 1, y - 1].  */
5277               if (low0 == 0 && high1 == 0)
5278                 {
5279                   low = range_successor (high0);
5280                   high = range_predecessor (low1);
5281                   if (low == 0 || high == 0)
5282                     return 0;
5283
5284                   in_p = 1;
5285                 }
5286               else
5287                 return 0;
5288             }
5289         }
5290       else if (subset)
5291         in_p = 0, low = low0, high = high0;
5292       else
5293         in_p = 0, low = low0, high = high1;
5294     }
5295
5296   *pin_p = in_p, *plow = low, *phigh = high;
5297   return 1;
5298 }
5299 \f
5300
5301 /* Subroutine of fold, looking inside expressions of the form
5302    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5303    of the COND_EXPR.  This function is being used also to optimize
5304    A op B ? C : A, by reversing the comparison first.
5305
5306    Return a folded expression whose code is not a COND_EXPR
5307    anymore, or NULL_TREE if no folding opportunity is found.  */
5308
5309 static tree
5310 fold_cond_expr_with_comparison (location_t loc, tree type,
5311                                 tree arg0, tree arg1, tree arg2)
5312 {
5313   enum tree_code comp_code = TREE_CODE (arg0);
5314   tree arg00 = TREE_OPERAND (arg0, 0);
5315   tree arg01 = TREE_OPERAND (arg0, 1);
5316   tree arg1_type = TREE_TYPE (arg1);
5317   tree tem;
5318
5319   STRIP_NOPS (arg1);
5320   STRIP_NOPS (arg2);
5321
5322   /* If we have A op 0 ? A : -A, consider applying the following
5323      transformations:
5324
5325      A == 0? A : -A    same as -A
5326      A != 0? A : -A    same as A
5327      A >= 0? A : -A    same as abs (A)
5328      A > 0?  A : -A    same as abs (A)
5329      A <= 0? A : -A    same as -abs (A)
5330      A < 0?  A : -A    same as -abs (A)
5331
5332      None of these transformations work for modes with signed
5333      zeros.  If A is +/-0, the first two transformations will
5334      change the sign of the result (from +0 to -0, or vice
5335      versa).  The last four will fix the sign of the result,
5336      even though the original expressions could be positive or
5337      negative, depending on the sign of A.
5338
5339      Note that all these transformations are correct if A is
5340      NaN, since the two alternatives (A and -A) are also NaNs.  */
5341   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5342       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5343           ? real_zerop (arg01)
5344           : integer_zerop (arg01))
5345       && ((TREE_CODE (arg2) == NEGATE_EXPR
5346            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5347              /* In the case that A is of the form X-Y, '-A' (arg2) may
5348                 have already been folded to Y-X, check for that. */
5349           || (TREE_CODE (arg1) == MINUS_EXPR
5350               && TREE_CODE (arg2) == MINUS_EXPR
5351               && operand_equal_p (TREE_OPERAND (arg1, 0),
5352                                   TREE_OPERAND (arg2, 1), 0)
5353               && operand_equal_p (TREE_OPERAND (arg1, 1),
5354                                   TREE_OPERAND (arg2, 0), 0))))
5355     switch (comp_code)
5356       {
5357       case EQ_EXPR:
5358       case UNEQ_EXPR:
5359         tem = fold_convert_loc (loc, arg1_type, arg1);
5360         return pedantic_non_lvalue_loc (loc,
5361                                     fold_convert_loc (loc, type,
5362                                                   negate_expr (tem)));
5363       case NE_EXPR:
5364       case LTGT_EXPR:
5365         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5366       case UNGE_EXPR:
5367       case UNGT_EXPR:
5368         if (flag_trapping_math)
5369           break;
5370         /* Fall through.  */
5371       case GE_EXPR:
5372       case GT_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 pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5378       case UNLE_EXPR:
5379       case UNLT_EXPR:
5380         if (flag_trapping_math)
5381           break;
5382       case LE_EXPR:
5383       case LT_EXPR:
5384         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5385           arg1 = fold_convert_loc (loc, signed_type_for
5386                                (TREE_TYPE (arg1)), arg1);
5387         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5388         return negate_expr (fold_convert_loc (loc, type, tem));
5389       default:
5390         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5391         break;
5392       }
5393
5394   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
5395      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
5396      both transformations are correct when A is NaN: A != 0
5397      is then true, and A == 0 is false.  */
5398
5399   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5400       && integer_zerop (arg01) && integer_zerop (arg2))
5401     {
5402       if (comp_code == NE_EXPR)
5403         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5404       else if (comp_code == EQ_EXPR)
5405         return build_int_cst (type, 0);
5406     }
5407
5408   /* Try some transformations of A op B ? A : B.
5409
5410      A == B? A : B    same as B
5411      A != B? A : B    same as A
5412      A >= B? A : B    same as max (A, B)
5413      A > B?  A : B    same as max (B, A)
5414      A <= B? A : B    same as min (A, B)
5415      A < B?  A : B    same as min (B, A)
5416
5417      As above, these transformations don't work in the presence
5418      of signed zeros.  For example, if A and B are zeros of
5419      opposite sign, the first two transformations will change
5420      the sign of the result.  In the last four, the original
5421      expressions give different results for (A=+0, B=-0) and
5422      (A=-0, B=+0), but the transformed expressions do not.
5423
5424      The first two transformations are correct if either A or B
5425      is a NaN.  In the first transformation, the condition will
5426      be false, and B will indeed be chosen.  In the case of the
5427      second transformation, the condition A != B will be true,
5428      and A will be chosen.
5429
5430      The conversions to max() and min() are not correct if B is
5431      a number and A is not.  The conditions in the original
5432      expressions will be false, so all four give B.  The min()
5433      and max() versions would give a NaN instead.  */
5434   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5435       && operand_equal_for_comparison_p (arg01, arg2, arg00)
5436       /* Avoid these transformations if the COND_EXPR may be used
5437          as an lvalue in the C++ front-end.  PR c++/19199.  */
5438       && (in_gimple_form
5439           || (strcmp (lang_hooks.name, "GNU C++") != 0
5440               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5441           || ! maybe_lvalue_p (arg1)
5442           || ! maybe_lvalue_p (arg2)))
5443     {
5444       tree comp_op0 = arg00;
5445       tree comp_op1 = arg01;
5446       tree comp_type = TREE_TYPE (comp_op0);
5447
5448       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
5449       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5450         {
5451           comp_type = type;
5452           comp_op0 = arg1;
5453           comp_op1 = arg2;
5454         }
5455
5456       switch (comp_code)
5457         {
5458         case EQ_EXPR:
5459           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
5460         case NE_EXPR:
5461           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5462         case LE_EXPR:
5463         case LT_EXPR:
5464         case UNLE_EXPR:
5465         case UNLT_EXPR:
5466           /* In C++ a ?: expression can be an lvalue, so put the
5467              operand which will be used if they are equal first
5468              so that we can convert this back to the
5469              corresponding COND_EXPR.  */
5470           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5471             {
5472               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5473               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5474               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5475                     ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5476                     : fold_build2_loc (loc, MIN_EXPR, comp_type,
5477                                    comp_op1, comp_op0);
5478               return pedantic_non_lvalue_loc (loc,
5479                                           fold_convert_loc (loc, type, tem));
5480             }
5481           break;
5482         case GE_EXPR:
5483         case GT_EXPR:
5484         case UNGE_EXPR:
5485         case UNGT_EXPR:
5486           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5487             {
5488               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5489               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5490               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5491                     ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5492                     : fold_build2_loc (loc, MAX_EXPR, comp_type,
5493                                    comp_op1, comp_op0);
5494               return pedantic_non_lvalue_loc (loc,
5495                                           fold_convert_loc (loc, type, tem));
5496             }
5497           break;
5498         case UNEQ_EXPR:
5499           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5500             return pedantic_non_lvalue_loc (loc,
5501                                         fold_convert_loc (loc, type, arg2));
5502           break;
5503         case LTGT_EXPR:
5504           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5505             return pedantic_non_lvalue_loc (loc,
5506                                         fold_convert_loc (loc, type, arg1));
5507           break;
5508         default:
5509           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5510           break;
5511         }
5512     }
5513
5514   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5515      we might still be able to simplify this.  For example,
5516      if C1 is one less or one more than C2, this might have started
5517      out as a MIN or MAX and been transformed by this function.
5518      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
5519
5520   if (INTEGRAL_TYPE_P (type)
5521       && TREE_CODE (arg01) == INTEGER_CST
5522       && TREE_CODE (arg2) == INTEGER_CST)
5523     switch (comp_code)
5524       {
5525       case EQ_EXPR:
5526         if (TREE_CODE (arg1) == INTEGER_CST)
5527           break;
5528         /* We can replace A with C1 in this case.  */
5529         arg1 = fold_convert_loc (loc, type, arg01);
5530         return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5531
5532       case LT_EXPR:
5533         /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5534            MIN_EXPR, to preserve the signedness of the comparison.  */
5535         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5536                                OEP_ONLY_CONST)
5537             && operand_equal_p (arg01,
5538                                 const_binop (PLUS_EXPR, arg2,
5539                                              build_int_cst (type, 1), 0),
5540                                 OEP_ONLY_CONST))
5541           {
5542             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5543                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5544                                                      arg2));
5545             return pedantic_non_lvalue_loc (loc,
5546                                             fold_convert_loc (loc, type, tem));
5547           }
5548         break;
5549
5550       case LE_EXPR:
5551         /* If C1 is C2 - 1, this is min(A, C2), with the same care
5552            as above.  */
5553         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5554                                OEP_ONLY_CONST)
5555             && operand_equal_p (arg01,
5556                                 const_binop (MINUS_EXPR, arg2,
5557                                              build_int_cst (type, 1), 0),
5558                                 OEP_ONLY_CONST))
5559           {
5560             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5561                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5562                                                      arg2));
5563             return pedantic_non_lvalue_loc (loc,
5564                                             fold_convert_loc (loc, type, tem));
5565           }
5566         break;
5567
5568       case GT_EXPR:
5569         /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5570            MAX_EXPR, to preserve the signedness of the comparison.  */
5571         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5572                                OEP_ONLY_CONST)
5573             && operand_equal_p (arg01,
5574                                 const_binop (MINUS_EXPR, arg2,
5575                                              build_int_cst (type, 1), 0),
5576                                 OEP_ONLY_CONST))
5577           {
5578             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5579                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5580                                                      arg2));
5581             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5582           }
5583         break;
5584
5585       case GE_EXPR:
5586         /* If C1 is C2 + 1, this is max(A, C2), with the same care as above.  */
5587         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5588                                OEP_ONLY_CONST)
5589             && operand_equal_p (arg01,
5590                                 const_binop (PLUS_EXPR, arg2,
5591                                              build_int_cst (type, 1), 0),
5592                                 OEP_ONLY_CONST))
5593           {
5594             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5595                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5596                                                      arg2));
5597             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5598           }
5599         break;
5600       case NE_EXPR:
5601         break;
5602       default:
5603         gcc_unreachable ();
5604       }
5605
5606   return NULL_TREE;
5607 }
5608
5609
5610 \f
5611 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5612 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5613   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5614                 false) >= 2)
5615 #endif
5616
5617 /* EXP is some logical combination of boolean tests.  See if we can
5618    merge it into some range test.  Return the new tree if so.  */
5619
5620 static tree
5621 fold_range_test (location_t loc, enum tree_code code, tree type,
5622                  tree op0, tree op1)
5623 {
5624   int or_op = (code == TRUTH_ORIF_EXPR
5625                || code == TRUTH_OR_EXPR);
5626   int in0_p, in1_p, in_p;
5627   tree low0, low1, low, high0, high1, high;
5628   bool strict_overflow_p = false;
5629   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5630   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5631   tree tem;
5632   const char * const warnmsg = G_("assuming signed overflow does not occur "
5633                                   "when simplifying range test");
5634
5635   /* If this is an OR operation, invert both sides; we will invert
5636      again at the end.  */
5637   if (or_op)
5638     in0_p = ! in0_p, in1_p = ! in1_p;
5639
5640   /* If both expressions are the same, if we can merge the ranges, and we
5641      can build the range test, return it or it inverted.  If one of the
5642      ranges is always true or always false, consider it to be the same
5643      expression as the other.  */
5644   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5645       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5646                        in1_p, low1, high1)
5647       && 0 != (tem = (build_range_check (UNKNOWN_LOCATION, type,
5648                                          lhs != 0 ? lhs
5649                                          : rhs != 0 ? rhs : integer_zero_node,
5650                                          in_p, low, high))))
5651     {
5652       if (strict_overflow_p)
5653         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5654       return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5655     }
5656
5657   /* On machines where the branch cost is expensive, if this is a
5658      short-circuited branch and the underlying object on both sides
5659      is the same, make a non-short-circuit operation.  */
5660   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5661            && lhs != 0 && rhs != 0
5662            && (code == TRUTH_ANDIF_EXPR
5663                || code == TRUTH_ORIF_EXPR)
5664            && operand_equal_p (lhs, rhs, 0))
5665     {
5666       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
5667          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5668          which cases we can't do this.  */
5669       if (simple_operand_p (lhs))
5670         {
5671           tem = build2 (code == TRUTH_ANDIF_EXPR
5672                         ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5673                         type, op0, op1);
5674           SET_EXPR_LOCATION (tem, loc);
5675           return tem;
5676         }
5677
5678       else if (lang_hooks.decls.global_bindings_p () == 0
5679                && ! CONTAINS_PLACEHOLDER_P (lhs))
5680         {
5681           tree common = save_expr (lhs);
5682
5683           if (0 != (lhs = build_range_check (loc, type, common,
5684                                              or_op ? ! in0_p : in0_p,
5685                                              low0, high0))
5686               && (0 != (rhs = build_range_check (loc, type, common,
5687                                                  or_op ? ! in1_p : in1_p,
5688                                                  low1, high1))))
5689             {
5690               if (strict_overflow_p)
5691                 fold_overflow_warning (warnmsg,
5692                                        WARN_STRICT_OVERFLOW_COMPARISON);
5693               tem = build2 (code == TRUTH_ANDIF_EXPR
5694                             ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5695                             type, lhs, rhs);
5696               SET_EXPR_LOCATION (tem, loc);
5697               return tem;
5698             }
5699         }
5700     }
5701
5702   return 0;
5703 }
5704 \f
5705 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5706    bit value.  Arrange things so the extra bits will be set to zero if and
5707    only if C is signed-extended to its full width.  If MASK is nonzero,
5708    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
5709
5710 static tree
5711 unextend (tree c, int p, int unsignedp, tree mask)
5712 {
5713   tree type = TREE_TYPE (c);
5714   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5715   tree temp;
5716
5717   if (p == modesize || unsignedp)
5718     return c;
5719
5720   /* We work by getting just the sign bit into the low-order bit, then
5721      into the high-order bit, then sign-extend.  We then XOR that value
5722      with C.  */
5723   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5724   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5725
5726   /* We must use a signed type in order to get an arithmetic right shift.
5727      However, we must also avoid introducing accidental overflows, so that
5728      a subsequent call to integer_zerop will work.  Hence we must
5729      do the type conversion here.  At this point, the constant is either
5730      zero or one, and the conversion to a signed type can never overflow.
5731      We could get an overflow if this conversion is done anywhere else.  */
5732   if (TYPE_UNSIGNED (type))
5733     temp = fold_convert (signed_type_for (type), temp);
5734
5735   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5736   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5737   if (mask != 0)
5738     temp = const_binop (BIT_AND_EXPR, temp,
5739                         fold_convert (TREE_TYPE (c), mask),
5740                         0);
5741   /* If necessary, convert the type back to match the type of C.  */
5742   if (TYPE_UNSIGNED (type))
5743     temp = fold_convert (type, temp);
5744
5745   return fold_convert (type,
5746                        const_binop (BIT_XOR_EXPR, c, temp, 0));
5747 }
5748 \f
5749 /* Find ways of folding logical expressions of LHS and RHS:
5750    Try to merge two comparisons to the same innermost item.
5751    Look for range tests like "ch >= '0' && ch <= '9'".
5752    Look for combinations of simple terms on machines with expensive branches
5753    and evaluate the RHS unconditionally.
5754
5755    For example, if we have p->a == 2 && p->b == 4 and we can make an
5756    object large enough to span both A and B, we can do this with a comparison
5757    against the object ANDed with the a mask.
5758
5759    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5760    operations to do this with one comparison.
5761
5762    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5763    function and the one above.
5764
5765    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5766    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5767
5768    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5769    two operands.
5770
5771    We return the simplified tree or 0 if no optimization is possible.  */
5772
5773 static tree
5774 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5775               tree lhs, tree rhs)
5776 {
5777   /* If this is the "or" of two comparisons, we can do something if
5778      the comparisons are NE_EXPR.  If this is the "and", we can do something
5779      if the comparisons are EQ_EXPR.  I.e.,
5780         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5781
5782      WANTED_CODE is this operation code.  For single bit fields, we can
5783      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5784      comparison for one-bit fields.  */
5785
5786   enum tree_code wanted_code;
5787   enum tree_code lcode, rcode;
5788   tree ll_arg, lr_arg, rl_arg, rr_arg;
5789   tree ll_inner, lr_inner, rl_inner, rr_inner;
5790   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5791   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5792   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5793   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5794   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5795   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5796   enum machine_mode lnmode, rnmode;
5797   tree ll_mask, lr_mask, rl_mask, rr_mask;
5798   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5799   tree l_const, r_const;
5800   tree lntype, rntype, result;
5801   HOST_WIDE_INT first_bit, end_bit;
5802   int volatilep;
5803   tree orig_lhs = lhs, orig_rhs = rhs;
5804   enum tree_code orig_code = code;
5805
5806   /* Start by getting the comparison codes.  Fail if anything is volatile.
5807      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5808      it were surrounded with a NE_EXPR.  */
5809
5810   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5811     return 0;
5812
5813   lcode = TREE_CODE (lhs);
5814   rcode = TREE_CODE (rhs);
5815
5816   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5817     {
5818       lhs = build2 (NE_EXPR, truth_type, lhs,
5819                     build_int_cst (TREE_TYPE (lhs), 0));
5820       lcode = NE_EXPR;
5821     }
5822
5823   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5824     {
5825       rhs = build2 (NE_EXPR, truth_type, rhs,
5826                     build_int_cst (TREE_TYPE (rhs), 0));
5827       rcode = NE_EXPR;
5828     }
5829
5830   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5831       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5832     return 0;
5833
5834   ll_arg = TREE_OPERAND (lhs, 0);
5835   lr_arg = TREE_OPERAND (lhs, 1);
5836   rl_arg = TREE_OPERAND (rhs, 0);
5837   rr_arg = TREE_OPERAND (rhs, 1);
5838
5839   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5840   if (simple_operand_p (ll_arg)
5841       && simple_operand_p (lr_arg))
5842     {
5843       tree result;
5844       if (operand_equal_p (ll_arg, rl_arg, 0)
5845           && operand_equal_p (lr_arg, rr_arg, 0))
5846         {
5847           result = combine_comparisons (loc, code, lcode, rcode,
5848                                         truth_type, ll_arg, lr_arg);
5849           if (result)
5850             return result;
5851         }
5852       else if (operand_equal_p (ll_arg, rr_arg, 0)
5853                && operand_equal_p (lr_arg, rl_arg, 0))
5854         {
5855           result = combine_comparisons (loc, code, lcode,
5856                                         swap_tree_comparison (rcode),
5857                                         truth_type, ll_arg, lr_arg);
5858           if (result)
5859             return result;
5860         }
5861     }
5862
5863   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5864           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5865
5866   /* If the RHS can be evaluated unconditionally and its operands are
5867      simple, it wins to evaluate the RHS unconditionally on machines
5868      with expensive branches.  In this case, this isn't a comparison
5869      that can be merged.  Avoid doing this if the RHS is a floating-point
5870      comparison since those can trap.  */
5871
5872   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5873                    false) >= 2
5874       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5875       && simple_operand_p (rl_arg)
5876       && simple_operand_p (rr_arg))
5877     {
5878       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5879       if (code == TRUTH_OR_EXPR
5880           && lcode == NE_EXPR && integer_zerop (lr_arg)
5881           && rcode == NE_EXPR && integer_zerop (rr_arg)
5882           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5883           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5884         {
5885           result = build2 (NE_EXPR, truth_type,
5886                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5887                                    ll_arg, rl_arg),
5888                            build_int_cst (TREE_TYPE (ll_arg), 0));
5889           goto fold_truthop_exit;
5890         }
5891
5892       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5893       if (code == TRUTH_AND_EXPR
5894           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5895           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5896           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5897           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5898         {
5899           result = build2 (EQ_EXPR, truth_type,
5900                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5901                                    ll_arg, rl_arg),
5902                            build_int_cst (TREE_TYPE (ll_arg), 0));
5903           goto fold_truthop_exit;
5904         }
5905
5906       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5907         {
5908           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5909             {
5910               result = build2 (code, truth_type, lhs, rhs);
5911               goto fold_truthop_exit;
5912             }
5913           return NULL_TREE;
5914         }
5915     }
5916
5917   /* See if the comparisons can be merged.  Then get all the parameters for
5918      each side.  */
5919
5920   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5921       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5922     return 0;
5923
5924   volatilep = 0;
5925   ll_inner = decode_field_reference (loc, ll_arg,
5926                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5927                                      &ll_unsignedp, &volatilep, &ll_mask,
5928                                      &ll_and_mask);
5929   lr_inner = decode_field_reference (loc, lr_arg,
5930                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5931                                      &lr_unsignedp, &volatilep, &lr_mask,
5932                                      &lr_and_mask);
5933   rl_inner = decode_field_reference (loc, rl_arg,
5934                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5935                                      &rl_unsignedp, &volatilep, &rl_mask,
5936                                      &rl_and_mask);
5937   rr_inner = decode_field_reference (loc, rr_arg,
5938                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5939                                      &rr_unsignedp, &volatilep, &rr_mask,
5940                                      &rr_and_mask);
5941
5942   /* It must be true that the inner operation on the lhs of each
5943      comparison must be the same if we are to be able to do anything.
5944      Then see if we have constants.  If not, the same must be true for
5945      the rhs's.  */
5946   if (volatilep || ll_inner == 0 || rl_inner == 0
5947       || ! operand_equal_p (ll_inner, rl_inner, 0))
5948     return 0;
5949
5950   if (TREE_CODE (lr_arg) == INTEGER_CST
5951       && TREE_CODE (rr_arg) == INTEGER_CST)
5952     l_const = lr_arg, r_const = rr_arg;
5953   else if (lr_inner == 0 || rr_inner == 0
5954            || ! operand_equal_p (lr_inner, rr_inner, 0))
5955     return 0;
5956   else
5957     l_const = r_const = 0;
5958
5959   /* If either comparison code is not correct for our logical operation,
5960      fail.  However, we can convert a one-bit comparison against zero into
5961      the opposite comparison against that bit being set in the field.  */
5962
5963   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5964   if (lcode != wanted_code)
5965     {
5966       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5967         {
5968           /* Make the left operand unsigned, since we are only interested
5969              in the value of one bit.  Otherwise we are doing the wrong
5970              thing below.  */
5971           ll_unsignedp = 1;
5972           l_const = ll_mask;
5973         }
5974       else
5975         return 0;
5976     }
5977
5978   /* This is analogous to the code for l_const above.  */
5979   if (rcode != wanted_code)
5980     {
5981       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5982         {
5983           rl_unsignedp = 1;
5984           r_const = rl_mask;
5985         }
5986       else
5987         return 0;
5988     }
5989
5990   /* See if we can find a mode that contains both fields being compared on
5991      the left.  If we can't, fail.  Otherwise, update all constants and masks
5992      to be relative to a field of that size.  */
5993   first_bit = MIN (ll_bitpos, rl_bitpos);
5994   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5995   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5996                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5997                           volatilep);
5998   if (lnmode == VOIDmode)
5999     return 0;
6000
6001   lnbitsize = GET_MODE_BITSIZE (lnmode);
6002   lnbitpos = first_bit & ~ (lnbitsize - 1);
6003   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
6004   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
6005
6006   if (BYTES_BIG_ENDIAN)
6007     {
6008       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
6009       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
6010     }
6011
6012   ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
6013                          size_int (xll_bitpos), 0);
6014   rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
6015                          size_int (xrl_bitpos), 0);
6016
6017   if (l_const)
6018     {
6019       l_const = fold_convert_loc (loc, lntype, l_const);
6020       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
6021       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
6022       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
6023                                         fold_build1_loc (loc, BIT_NOT_EXPR,
6024                                                      lntype, ll_mask),
6025                                         0)))
6026         {
6027           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6028
6029           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6030         }
6031     }
6032   if (r_const)
6033     {
6034       r_const = fold_convert_loc (loc, lntype, r_const);
6035       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
6036       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
6037       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
6038                                         fold_build1_loc (loc, BIT_NOT_EXPR,
6039                                                      lntype, rl_mask),
6040                                         0)))
6041         {
6042           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6043
6044           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6045         }
6046     }
6047
6048   /* If the right sides are not constant, do the same for it.  Also,
6049      disallow this optimization if a size or signedness mismatch occurs
6050      between the left and right sides.  */
6051   if (l_const == 0)
6052     {
6053       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
6054           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
6055           /* Make sure the two fields on the right
6056              correspond to the left without being swapped.  */
6057           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
6058         return 0;
6059
6060       first_bit = MIN (lr_bitpos, rr_bitpos);
6061       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
6062       rnmode = get_best_mode (end_bit - first_bit, first_bit,
6063                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
6064                               volatilep);
6065       if (rnmode == VOIDmode)
6066         return 0;
6067
6068       rnbitsize = GET_MODE_BITSIZE (rnmode);
6069       rnbitpos = first_bit & ~ (rnbitsize - 1);
6070       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
6071       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
6072
6073       if (BYTES_BIG_ENDIAN)
6074         {
6075           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
6076           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
6077         }
6078
6079       lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6080                                                             rntype, lr_mask),
6081                              size_int (xlr_bitpos), 0);
6082       rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6083                                                             rntype, rr_mask),
6084                              size_int (xrr_bitpos), 0);
6085
6086       /* Make a mask that corresponds to both fields being compared.
6087          Do this for both items being compared.  If the operands are the
6088          same size and the bits being compared are in the same position
6089          then we can do this by masking both and comparing the masked
6090          results.  */
6091       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
6092       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
6093       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
6094         {
6095           lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
6096                                     ll_unsignedp || rl_unsignedp);
6097           if (! all_ones_mask_p (ll_mask, lnbitsize))
6098             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6099
6100           rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
6101                                     lr_unsignedp || rr_unsignedp);
6102           if (! all_ones_mask_p (lr_mask, rnbitsize))
6103             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6104
6105           result = build2 (wanted_code, truth_type, lhs, rhs);
6106           goto fold_truthop_exit;
6107         }
6108
6109       /* There is still another way we can do something:  If both pairs of
6110          fields being compared are adjacent, we may be able to make a wider
6111          field containing them both.
6112
6113          Note that we still must mask the lhs/rhs expressions.  Furthermore,
6114          the mask must be shifted to account for the shift done by
6115          make_bit_field_ref.  */
6116       if ((ll_bitsize + ll_bitpos == rl_bitpos
6117            && lr_bitsize + lr_bitpos == rr_bitpos)
6118           || (ll_bitpos == rl_bitpos + rl_bitsize
6119               && lr_bitpos == rr_bitpos + rr_bitsize))
6120         {
6121           tree type;
6122
6123           lhs = make_bit_field_ref (loc, ll_inner, lntype,
6124                                     ll_bitsize + rl_bitsize,
6125                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
6126           rhs = make_bit_field_ref (loc, lr_inner, rntype,
6127                                     lr_bitsize + rr_bitsize,
6128                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
6129
6130           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6131                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
6132           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6133                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
6134
6135           /* Convert to the smaller type before masking out unwanted bits.  */
6136           type = lntype;
6137           if (lntype != rntype)
6138             {
6139               if (lnbitsize > rnbitsize)
6140                 {
6141                   lhs = fold_convert_loc (loc, rntype, lhs);
6142                   ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6143                   type = rntype;
6144                 }
6145               else if (lnbitsize < rnbitsize)
6146                 {
6147                   rhs = fold_convert_loc (loc, lntype, rhs);
6148                   lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6149                   type = lntype;
6150                 }
6151             }
6152
6153           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6154             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6155
6156           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6157             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6158
6159           result = build2 (wanted_code, truth_type, lhs, rhs);
6160           goto fold_truthop_exit;
6161         }
6162
6163       return 0;
6164     }
6165
6166   /* Handle the case of comparisons with constants.  If there is something in
6167      common between the masks, those bits of the constants must be the same.
6168      If not, the condition is always false.  Test for this to avoid generating
6169      incorrect code below.  */
6170   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
6171   if (! integer_zerop (result)
6172       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
6173                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
6174     {
6175       if (wanted_code == NE_EXPR)
6176         {
6177           warning (0, "%<or%> of unmatched not-equal tests is always 1");
6178           return constant_boolean_node (true, truth_type);
6179         }
6180       else
6181         {
6182           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6183           return constant_boolean_node (false, truth_type);
6184         }
6185     }
6186
6187   /* Construct the expression we will return.  First get the component
6188      reference we will make.  Unless the mask is all ones the width of
6189      that field, perform the mask operation.  Then compare with the
6190      merged constant.  */
6191   result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
6192                                ll_unsignedp || rl_unsignedp);
6193
6194   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
6195   if (! all_ones_mask_p (ll_mask, lnbitsize))
6196     {
6197       result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
6198       SET_EXPR_LOCATION (result, loc);
6199     }
6200
6201   result = build2 (wanted_code, truth_type, result,
6202                    const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
6203
6204  fold_truthop_exit:
6205   SET_EXPR_LOCATION (result, loc);
6206   return result;
6207 }
6208 \f
6209 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
6210    constant.  */
6211
6212 static tree
6213 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
6214                             tree op0, tree op1)
6215 {
6216   tree arg0 = op0;
6217   enum tree_code op_code;
6218   tree comp_const;
6219   tree minmax_const;
6220   int consts_equal, consts_lt;
6221   tree inner;
6222
6223   STRIP_SIGN_NOPS (arg0);
6224
6225   op_code = TREE_CODE (arg0);
6226   minmax_const = TREE_OPERAND (arg0, 1);
6227   comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
6228   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
6229   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
6230   inner = TREE_OPERAND (arg0, 0);
6231
6232   /* If something does not permit us to optimize, return the original tree.  */
6233   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
6234       || TREE_CODE (comp_const) != INTEGER_CST
6235       || TREE_OVERFLOW (comp_const)
6236       || TREE_CODE (minmax_const) != INTEGER_CST
6237       || TREE_OVERFLOW (minmax_const))
6238     return NULL_TREE;
6239
6240   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
6241      and GT_EXPR, doing the rest with recursive calls using logical
6242      simplifications.  */
6243   switch (code)
6244     {
6245     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
6246       {
6247         tree tem
6248           = optimize_minmax_comparison (loc,
6249                                         invert_tree_comparison (code, false),
6250                                         type, op0, op1);
6251         if (tem)
6252           return invert_truthvalue_loc (loc, tem);
6253         return NULL_TREE;
6254       }
6255
6256     case GE_EXPR:
6257       return
6258         fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
6259                      optimize_minmax_comparison
6260                      (loc, EQ_EXPR, type, arg0, comp_const),
6261                      optimize_minmax_comparison
6262                      (loc, GT_EXPR, type, arg0, comp_const));
6263
6264     case EQ_EXPR:
6265       if (op_code == MAX_EXPR && consts_equal)
6266         /* MAX (X, 0) == 0  ->  X <= 0  */
6267         return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
6268
6269       else if (op_code == MAX_EXPR && consts_lt)
6270         /* MAX (X, 0) == 5  ->  X == 5   */
6271         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
6272
6273       else if (op_code == MAX_EXPR)
6274         /* MAX (X, 0) == -1  ->  false  */
6275         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6276
6277       else if (consts_equal)
6278         /* MIN (X, 0) == 0  ->  X >= 0  */
6279         return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
6280
6281       else if (consts_lt)
6282         /* MIN (X, 0) == 5  ->  false  */
6283         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6284
6285       else
6286         /* MIN (X, 0) == -1  ->  X == -1  */
6287         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
6288
6289     case GT_EXPR:
6290       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
6291         /* MAX (X, 0) > 0  ->  X > 0
6292            MAX (X, 0) > 5  ->  X > 5  */
6293         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
6294
6295       else if (op_code == MAX_EXPR)
6296         /* MAX (X, 0) > -1  ->  true  */
6297         return omit_one_operand_loc (loc, type, integer_one_node, inner);
6298
6299       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
6300         /* MIN (X, 0) > 0  ->  false
6301            MIN (X, 0) > 5  ->  false  */
6302         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6303
6304       else
6305         /* MIN (X, 0) > -1  ->  X > -1  */
6306         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
6307
6308     default:
6309       return NULL_TREE;
6310     }
6311 }
6312 \f
6313 /* T is an integer expression that is being multiplied, divided, or taken a
6314    modulus (CODE says which and what kind of divide or modulus) by a
6315    constant C.  See if we can eliminate that operation by folding it with
6316    other operations already in T.  WIDE_TYPE, if non-null, is a type that
6317    should be used for the computation if wider than our type.
6318
6319    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6320    (X * 2) + (Y * 4).  We must, however, be assured that either the original
6321    expression would not overflow or that overflow is undefined for the type
6322    in the language in question.
6323
6324    If we return a non-null expression, it is an equivalent form of the
6325    original computation, but need not be in the original type.
6326
6327    We set *STRICT_OVERFLOW_P to true if the return values depends on
6328    signed overflow being undefined.  Otherwise we do not change
6329    *STRICT_OVERFLOW_P.  */
6330
6331 static tree
6332 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6333                 bool *strict_overflow_p)
6334 {
6335   /* To avoid exponential search depth, refuse to allow recursion past
6336      three levels.  Beyond that (1) it's highly unlikely that we'll find
6337      something interesting and (2) we've probably processed it before
6338      when we built the inner expression.  */
6339
6340   static int depth;
6341   tree ret;
6342
6343   if (depth > 3)
6344     return NULL;
6345
6346   depth++;
6347   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6348   depth--;
6349
6350   return ret;
6351 }
6352
6353 static tree
6354 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6355                   bool *strict_overflow_p)
6356 {
6357   tree type = TREE_TYPE (t);
6358   enum tree_code tcode = TREE_CODE (t);
6359   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6360                                    > GET_MODE_SIZE (TYPE_MODE (type)))
6361                 ? wide_type : type);
6362   tree t1, t2;
6363   int same_p = tcode == code;
6364   tree op0 = NULL_TREE, op1 = NULL_TREE;
6365   bool sub_strict_overflow_p;
6366
6367   /* Don't deal with constants of zero here; they confuse the code below.  */
6368   if (integer_zerop (c))
6369     return NULL_TREE;
6370
6371   if (TREE_CODE_CLASS (tcode) == tcc_unary)
6372     op0 = TREE_OPERAND (t, 0);
6373
6374   if (TREE_CODE_CLASS (tcode) == tcc_binary)
6375     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6376
6377   /* Note that we need not handle conditional operations here since fold
6378      already handles those cases.  So just do arithmetic here.  */
6379   switch (tcode)
6380     {
6381     case INTEGER_CST:
6382       /* For a constant, we can always simplify if we are a multiply
6383          or (for divide and modulus) if it is a multiple of our constant.  */
6384       if (code == MULT_EXPR
6385           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6386         return const_binop (code, fold_convert (ctype, t),
6387                             fold_convert (ctype, c), 0);
6388       break;
6389
6390     CASE_CONVERT: case NON_LVALUE_EXPR:
6391       /* If op0 is an expression ...  */
6392       if ((COMPARISON_CLASS_P (op0)
6393            || UNARY_CLASS_P (op0)
6394            || BINARY_CLASS_P (op0)
6395            || VL_EXP_CLASS_P (op0)
6396            || EXPRESSION_CLASS_P (op0))
6397           /* ... and has wrapping overflow, and its type is smaller
6398              than ctype, then we cannot pass through as widening.  */
6399           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6400                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6401                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6402                && (TYPE_PRECISION (ctype)
6403                    > TYPE_PRECISION (TREE_TYPE (op0))))
6404               /* ... or this is a truncation (t is narrower than op0),
6405                  then we cannot pass through this narrowing.  */
6406               || (TYPE_PRECISION (type)
6407                   < TYPE_PRECISION (TREE_TYPE (op0)))
6408               /* ... or signedness changes for division or modulus,
6409                  then we cannot pass through this conversion.  */
6410               || (code != MULT_EXPR
6411                   && (TYPE_UNSIGNED (ctype)
6412                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
6413               /* ... or has undefined overflow while the converted to
6414                  type has not, we cannot do the operation in the inner type
6415                  as that would introduce undefined overflow.  */
6416               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6417                   && !TYPE_OVERFLOW_UNDEFINED (type))))
6418         break;
6419
6420       /* Pass the constant down and see if we can make a simplification.  If
6421          we can, replace this expression with the inner simplification for
6422          possible later conversion to our or some other type.  */
6423       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6424           && TREE_CODE (t2) == INTEGER_CST
6425           && !TREE_OVERFLOW (t2)
6426           && (0 != (t1 = extract_muldiv (op0, t2, code,
6427                                          code == MULT_EXPR
6428                                          ? ctype : NULL_TREE,
6429                                          strict_overflow_p))))
6430         return t1;
6431       break;
6432
6433     case ABS_EXPR:
6434       /* If widening the type changes it from signed to unsigned, then we
6435          must avoid building ABS_EXPR itself as unsigned.  */
6436       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6437         {
6438           tree cstype = (*signed_type_for) (ctype);
6439           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6440               != 0)
6441             {
6442               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6443               return fold_convert (ctype, t1);
6444             }
6445           break;
6446         }
6447       /* If the constant is negative, we cannot simplify this.  */
6448       if (tree_int_cst_sgn (c) == -1)
6449         break;
6450       /* FALLTHROUGH */
6451     case NEGATE_EXPR:
6452       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6453           != 0)
6454         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6455       break;
6456
6457     case MIN_EXPR:  case MAX_EXPR:
6458       /* If widening the type changes the signedness, then we can't perform
6459          this optimization as that changes the result.  */
6460       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6461         break;
6462
6463       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
6464       sub_strict_overflow_p = false;
6465       if ((t1 = extract_muldiv (op0, c, code, wide_type,
6466                                 &sub_strict_overflow_p)) != 0
6467           && (t2 = extract_muldiv (op1, c, code, wide_type,
6468                                    &sub_strict_overflow_p)) != 0)
6469         {
6470           if (tree_int_cst_sgn (c) < 0)
6471             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6472           if (sub_strict_overflow_p)
6473             *strict_overflow_p = true;
6474           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6475                               fold_convert (ctype, t2));
6476         }
6477       break;
6478
6479     case LSHIFT_EXPR:  case RSHIFT_EXPR:
6480       /* If the second operand is constant, this is a multiplication
6481          or floor division, by a power of two, so we can treat it that
6482          way unless the multiplier or divisor overflows.  Signed
6483          left-shift overflow is implementation-defined rather than
6484          undefined in C90, so do not convert signed left shift into
6485          multiplication.  */
6486       if (TREE_CODE (op1) == INTEGER_CST
6487           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6488           /* const_binop may not detect overflow correctly,
6489              so check for it explicitly here.  */
6490           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6491           && TREE_INT_CST_HIGH (op1) == 0
6492           && 0 != (t1 = fold_convert (ctype,
6493                                       const_binop (LSHIFT_EXPR,
6494                                                    size_one_node,
6495                                                    op1, 0)))
6496           && !TREE_OVERFLOW (t1))
6497         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6498                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
6499                                        ctype,
6500                                        fold_convert (ctype, op0),
6501                                        t1),
6502                                c, code, wide_type, strict_overflow_p);
6503       break;
6504
6505     case PLUS_EXPR:  case MINUS_EXPR:
6506       /* See if we can eliminate the operation on both sides.  If we can, we
6507          can return a new PLUS or MINUS.  If we can't, the only remaining
6508          cases where we can do anything are if the second operand is a
6509          constant.  */
6510       sub_strict_overflow_p = false;
6511       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6512       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6513       if (t1 != 0 && t2 != 0
6514           && (code == MULT_EXPR
6515               /* If not multiplication, we can only do this if both operands
6516                  are divisible by c.  */
6517               || (multiple_of_p (ctype, op0, c)
6518                   && multiple_of_p (ctype, op1, c))))
6519         {
6520           if (sub_strict_overflow_p)
6521             *strict_overflow_p = true;
6522           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6523                               fold_convert (ctype, t2));
6524         }
6525
6526       /* If this was a subtraction, negate OP1 and set it to be an addition.
6527          This simplifies the logic below.  */
6528       if (tcode == MINUS_EXPR)
6529         {
6530           tcode = PLUS_EXPR, op1 = negate_expr (op1);
6531           /* If OP1 was not easily negatable, the constant may be OP0.  */
6532           if (TREE_CODE (op0) == INTEGER_CST)
6533             {
6534               tree tem = op0;
6535               op0 = op1;
6536               op1 = tem;
6537               tem = t1;
6538               t1 = t2;
6539               t2 = tem;
6540             }
6541         }
6542
6543       if (TREE_CODE (op1) != INTEGER_CST)
6544         break;
6545
6546       /* If either OP1 or C are negative, this optimization is not safe for
6547          some of the division and remainder types while for others we need
6548          to change the code.  */
6549       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6550         {
6551           if (code == CEIL_DIV_EXPR)
6552             code = FLOOR_DIV_EXPR;
6553           else if (code == FLOOR_DIV_EXPR)
6554             code = CEIL_DIV_EXPR;
6555           else if (code != MULT_EXPR
6556                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6557             break;
6558         }
6559
6560       /* If it's a multiply or a division/modulus operation of a multiple
6561          of our constant, do the operation and verify it doesn't overflow.  */
6562       if (code == MULT_EXPR
6563           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6564         {
6565           op1 = const_binop (code, fold_convert (ctype, op1),
6566                              fold_convert (ctype, c), 0);
6567           /* We allow the constant to overflow with wrapping semantics.  */
6568           if (op1 == 0
6569               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6570             break;
6571         }
6572       else
6573         break;
6574
6575       /* If we have an unsigned type is not a sizetype, we cannot widen
6576          the operation since it will change the result if the original
6577          computation overflowed.  */
6578       if (TYPE_UNSIGNED (ctype)
6579           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6580           && ctype != type)
6581         break;
6582
6583       /* If we were able to eliminate our operation from the first side,
6584          apply our operation to the second side and reform the PLUS.  */
6585       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6586         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6587
6588       /* The last case is if we are a multiply.  In that case, we can
6589          apply the distributive law to commute the multiply and addition
6590          if the multiplication of the constants doesn't overflow.  */
6591       if (code == MULT_EXPR)
6592         return fold_build2 (tcode, ctype,
6593                             fold_build2 (code, ctype,
6594                                          fold_convert (ctype, op0),
6595                                          fold_convert (ctype, c)),
6596                             op1);
6597
6598       break;
6599
6600     case MULT_EXPR:
6601       /* We have a special case here if we are doing something like
6602          (C * 8) % 4 since we know that's zero.  */
6603       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6604            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6605           /* If the multiplication can overflow we cannot optimize this.
6606              ???  Until we can properly mark individual operations as
6607              not overflowing we need to treat sizetype special here as
6608              stor-layout relies on this opimization to make
6609              DECL_FIELD_BIT_OFFSET always a constant.  */
6610           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6611               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6612                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
6613           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6614           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6615         {
6616           *strict_overflow_p = true;
6617           return omit_one_operand (type, integer_zero_node, op0);
6618         }
6619
6620       /* ... fall through ...  */
6621
6622     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
6623     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
6624       /* If we can extract our operation from the LHS, do so and return a
6625          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
6626          do something only if the second operand is a constant.  */
6627       if (same_p
6628           && (t1 = extract_muldiv (op0, c, code, wide_type,
6629                                    strict_overflow_p)) != 0)
6630         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6631                             fold_convert (ctype, op1));
6632       else if (tcode == MULT_EXPR && code == MULT_EXPR
6633                && (t1 = extract_muldiv (op1, c, code, wide_type,
6634                                         strict_overflow_p)) != 0)
6635         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6636                             fold_convert (ctype, t1));
6637       else if (TREE_CODE (op1) != INTEGER_CST)
6638         return 0;
6639
6640       /* If these are the same operation types, we can associate them
6641          assuming no overflow.  */
6642       if (tcode == code
6643           && 0 != (t1 = int_const_binop (MULT_EXPR,
6644                                          fold_convert (ctype, op1),
6645                                          fold_convert (ctype, c), 1))
6646           && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
6647                                                TREE_INT_CST_HIGH (t1),
6648                                                (TYPE_UNSIGNED (ctype)
6649                                                 && tcode != MULT_EXPR) ? -1 : 1,
6650                                                TREE_OVERFLOW (t1)))
6651           && !TREE_OVERFLOW (t1))
6652         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6653
6654       /* If these operations "cancel" each other, we have the main
6655          optimizations of this pass, which occur when either constant is a
6656          multiple of the other, in which case we replace this with either an
6657          operation or CODE or TCODE.
6658
6659          If we have an unsigned type that is not a sizetype, we cannot do
6660          this since it will change the result if the original computation
6661          overflowed.  */
6662       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6663            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6664           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6665               || (tcode == MULT_EXPR
6666                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6667                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6668                   && code != MULT_EXPR)))
6669         {
6670           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6671             {
6672               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6673                 *strict_overflow_p = true;
6674               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6675                                   fold_convert (ctype,
6676                                                 const_binop (TRUNC_DIV_EXPR,
6677                                                              op1, c, 0)));
6678             }
6679           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6680             {
6681               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6682                 *strict_overflow_p = true;
6683               return fold_build2 (code, ctype, fold_convert (ctype, op0),
6684                                   fold_convert (ctype,
6685                                                 const_binop (TRUNC_DIV_EXPR,
6686                                                              c, op1, 0)));
6687             }
6688         }
6689       break;
6690
6691     default:
6692       break;
6693     }
6694
6695   return 0;
6696 }
6697 \f
6698 /* Return a node which has the indicated constant VALUE (either 0 or
6699    1), and is of the indicated TYPE.  */
6700
6701 tree
6702 constant_boolean_node (int value, tree type)
6703 {
6704   if (type == integer_type_node)
6705     return value ? integer_one_node : integer_zero_node;
6706   else if (type == boolean_type_node)
6707     return value ? boolean_true_node : boolean_false_node;
6708   else
6709     return build_int_cst (type, value);
6710 }
6711
6712
6713 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6714    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6715    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6716    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6717    COND is the first argument to CODE; otherwise (as in the example
6718    given here), it is the second argument.  TYPE is the type of the
6719    original expression.  Return NULL_TREE if no simplification is
6720    possible.  */
6721
6722 static tree
6723 fold_binary_op_with_conditional_arg (location_t loc,
6724                                      enum tree_code code,
6725                                      tree type, tree op0, tree op1,
6726                                      tree cond, tree arg, int cond_first_p)
6727 {
6728   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6729   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6730   tree test, true_value, false_value;
6731   tree lhs = NULL_TREE;
6732   tree rhs = NULL_TREE;
6733
6734   /* This transformation is only worthwhile if we don't have to wrap
6735      arg in a SAVE_EXPR, and the operation can be simplified on at least
6736      one of the branches once its pushed inside the COND_EXPR.  */
6737   if (!TREE_CONSTANT (arg))
6738     return NULL_TREE;
6739
6740   if (TREE_CODE (cond) == COND_EXPR)
6741     {
6742       test = TREE_OPERAND (cond, 0);
6743       true_value = TREE_OPERAND (cond, 1);
6744       false_value = TREE_OPERAND (cond, 2);
6745       /* If this operand throws an expression, then it does not make
6746          sense to try to perform a logical or arithmetic operation
6747          involving it.  */
6748       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6749         lhs = true_value;
6750       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6751         rhs = false_value;
6752     }
6753   else
6754     {
6755       tree testtype = TREE_TYPE (cond);
6756       test = cond;
6757       true_value = constant_boolean_node (true, testtype);
6758       false_value = constant_boolean_node (false, testtype);
6759     }
6760
6761   arg = fold_convert_loc (loc, arg_type, arg);
6762   if (lhs == 0)
6763     {
6764       true_value = fold_convert_loc (loc, cond_type, true_value);
6765       if (cond_first_p)
6766         lhs = fold_build2_loc (loc, code, type, true_value, arg);
6767       else
6768         lhs = fold_build2_loc (loc, code, type, arg, true_value);
6769     }
6770   if (rhs == 0)
6771     {
6772       false_value = fold_convert_loc (loc, cond_type, false_value);
6773       if (cond_first_p)
6774         rhs = fold_build2_loc (loc, code, type, false_value, arg);
6775       else
6776         rhs = fold_build2_loc (loc, code, type, arg, false_value);
6777     }
6778
6779   test = fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6780   return fold_convert_loc (loc, type, test);
6781 }
6782
6783 \f
6784 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6785
6786    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6787    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6788    ADDEND is the same as X.
6789
6790    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6791    and finite.  The problematic cases are when X is zero, and its mode
6792    has signed zeros.  In the case of rounding towards -infinity,
6793    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6794    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6795
6796 bool
6797 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6798 {
6799   if (!real_zerop (addend))
6800     return false;
6801
6802   /* Don't allow the fold with -fsignaling-nans.  */
6803   if (HONOR_SNANS (TYPE_MODE (type)))
6804     return false;
6805
6806   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6807   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6808     return true;
6809
6810   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6811   if (TREE_CODE (addend) == REAL_CST
6812       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6813     negate = !negate;
6814
6815   /* The mode has signed zeros, and we have to honor their sign.
6816      In this situation, there is only one case we can return true for.
6817      X - 0 is the same as X unless rounding towards -infinity is
6818      supported.  */
6819   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6820 }
6821
6822 /* Subroutine of fold() that checks comparisons of built-in math
6823    functions against real constants.
6824
6825    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6826    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6827    is the type of the result and ARG0 and ARG1 are the operands of the
6828    comparison.  ARG1 must be a TREE_REAL_CST.
6829
6830    The function returns the constant folded tree if a simplification
6831    can be made, and NULL_TREE otherwise.  */
6832
6833 static tree
6834 fold_mathfn_compare (location_t loc,
6835                      enum built_in_function fcode, enum tree_code code,
6836                      tree type, tree arg0, tree arg1)
6837 {
6838   REAL_VALUE_TYPE c;
6839
6840   if (BUILTIN_SQRT_P (fcode))
6841     {
6842       tree arg = CALL_EXPR_ARG (arg0, 0);
6843       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6844
6845       c = TREE_REAL_CST (arg1);
6846       if (REAL_VALUE_NEGATIVE (c))
6847         {
6848           /* sqrt(x) < y is always false, if y is negative.  */
6849           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6850             return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6851
6852           /* sqrt(x) > y is always true, if y is negative and we
6853              don't care about NaNs, i.e. negative values of x.  */
6854           if (code == NE_EXPR || !HONOR_NANS (mode))
6855             return omit_one_operand_loc (loc, type, integer_one_node, arg);
6856
6857           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6858           return fold_build2_loc (loc, GE_EXPR, type, arg,
6859                               build_real (TREE_TYPE (arg), dconst0));
6860         }
6861       else if (code == GT_EXPR || code == GE_EXPR)
6862         {
6863           REAL_VALUE_TYPE c2;
6864
6865           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6866           real_convert (&c2, mode, &c2);
6867
6868           if (REAL_VALUE_ISINF (c2))
6869             {
6870               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6871               if (HONOR_INFINITIES (mode))
6872                 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6873                                     build_real (TREE_TYPE (arg), c2));
6874
6875               /* sqrt(x) > y is always false, when y is very large
6876                  and we don't care about infinities.  */
6877               return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6878             }
6879
6880           /* sqrt(x) > c is the same as x > c*c.  */
6881           return fold_build2_loc (loc, code, type, arg,
6882                               build_real (TREE_TYPE (arg), c2));
6883         }
6884       else if (code == LT_EXPR || code == LE_EXPR)
6885         {
6886           REAL_VALUE_TYPE c2;
6887
6888           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6889           real_convert (&c2, mode, &c2);
6890
6891           if (REAL_VALUE_ISINF (c2))
6892             {
6893               /* sqrt(x) < y is always true, when y is a very large
6894                  value and we don't care about NaNs or Infinities.  */
6895               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6896                 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6897
6898               /* sqrt(x) < y is x != +Inf when y is very large and we
6899                  don't care about NaNs.  */
6900               if (! HONOR_NANS (mode))
6901                 return fold_build2_loc (loc, NE_EXPR, type, arg,
6902                                     build_real (TREE_TYPE (arg), c2));
6903
6904               /* sqrt(x) < y is x >= 0 when y is very large and we
6905                  don't care about Infinities.  */
6906               if (! HONOR_INFINITIES (mode))
6907                 return fold_build2_loc (loc, GE_EXPR, type, arg,
6908                                     build_real (TREE_TYPE (arg), dconst0));
6909
6910               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6911               if (lang_hooks.decls.global_bindings_p () != 0
6912                   || CONTAINS_PLACEHOLDER_P (arg))
6913                 return NULL_TREE;
6914
6915               arg = save_expr (arg);
6916               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6917                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6918                                                build_real (TREE_TYPE (arg),
6919                                                            dconst0)),
6920                                   fold_build2_loc (loc, NE_EXPR, type, arg,
6921                                                build_real (TREE_TYPE (arg),
6922                                                            c2)));
6923             }
6924
6925           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6926           if (! HONOR_NANS (mode))
6927             return fold_build2_loc (loc, code, type, arg,
6928                                 build_real (TREE_TYPE (arg), c2));
6929
6930           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6931           if (lang_hooks.decls.global_bindings_p () == 0
6932               && ! CONTAINS_PLACEHOLDER_P (arg))
6933             {
6934               arg = save_expr (arg);
6935               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6936                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6937                                                build_real (TREE_TYPE (arg),
6938                                                            dconst0)),
6939                                   fold_build2_loc (loc, code, type, arg,
6940                                                build_real (TREE_TYPE (arg),
6941                                                            c2)));
6942             }
6943         }
6944     }
6945
6946   return NULL_TREE;
6947 }
6948
6949 /* Subroutine of fold() that optimizes comparisons against Infinities,
6950    either +Inf or -Inf.
6951
6952    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6953    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6954    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6955
6956    The function returns the constant folded tree if a simplification
6957    can be made, and NULL_TREE otherwise.  */
6958
6959 static tree
6960 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6961                   tree arg0, tree arg1)
6962 {
6963   enum machine_mode mode;
6964   REAL_VALUE_TYPE max;
6965   tree temp;
6966   bool neg;
6967
6968   mode = TYPE_MODE (TREE_TYPE (arg0));
6969
6970   /* For negative infinity swap the sense of the comparison.  */
6971   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6972   if (neg)
6973     code = swap_tree_comparison (code);
6974
6975   switch (code)
6976     {
6977     case GT_EXPR:
6978       /* x > +Inf is always false, if with ignore sNANs.  */
6979       if (HONOR_SNANS (mode))
6980         return NULL_TREE;
6981       return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6982
6983     case LE_EXPR:
6984       /* x <= +Inf is always true, if we don't case about NaNs.  */
6985       if (! HONOR_NANS (mode))
6986         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6987
6988       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6989       if (lang_hooks.decls.global_bindings_p () == 0
6990           && ! CONTAINS_PLACEHOLDER_P (arg0))
6991         {
6992           arg0 = save_expr (arg0);
6993           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6994         }
6995       break;
6996
6997     case EQ_EXPR:
6998     case GE_EXPR:
6999       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
7000       real_maxval (&max, neg, mode);
7001       return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
7002                           arg0, build_real (TREE_TYPE (arg0), max));
7003
7004     case LT_EXPR:
7005       /* x < +Inf is always equal to x <= DBL_MAX.  */
7006       real_maxval (&max, neg, mode);
7007       return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
7008                           arg0, build_real (TREE_TYPE (arg0), max));
7009
7010     case NE_EXPR:
7011       /* x != +Inf is always equal to !(x > DBL_MAX).  */
7012       real_maxval (&max, neg, mode);
7013       if (! HONOR_NANS (mode))
7014         return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
7015                             arg0, build_real (TREE_TYPE (arg0), max));
7016
7017       temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
7018                           arg0, build_real (TREE_TYPE (arg0), max));
7019       return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
7020
7021     default:
7022       break;
7023     }
7024
7025   return NULL_TREE;
7026 }
7027
7028 /* Subroutine of fold() that optimizes comparisons of a division by
7029    a nonzero integer constant against an integer constant, i.e.
7030    X/C1 op C2.
7031
7032    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7033    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
7034    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
7035
7036    The function returns the constant folded tree if a simplification
7037    can be made, and NULL_TREE otherwise.  */
7038
7039 static tree
7040 fold_div_compare (location_t loc,
7041                   enum tree_code code, tree type, tree arg0, tree arg1)
7042 {
7043   tree prod, tmp, hi, lo;
7044   tree arg00 = TREE_OPERAND (arg0, 0);
7045   tree arg01 = TREE_OPERAND (arg0, 1);
7046   unsigned HOST_WIDE_INT lpart;
7047   HOST_WIDE_INT hpart;
7048   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
7049   bool neg_overflow;
7050   int overflow;
7051
7052   /* We have to do this the hard way to detect unsigned overflow.
7053      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
7054   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
7055                                    TREE_INT_CST_HIGH (arg01),
7056                                    TREE_INT_CST_LOW (arg1),
7057                                    TREE_INT_CST_HIGH (arg1),
7058                                    &lpart, &hpart, unsigned_p);
7059   prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
7060                                 -1, overflow);
7061   neg_overflow = false;
7062
7063   if (unsigned_p)
7064     {
7065       tmp = int_const_binop (MINUS_EXPR, arg01,
7066                              build_int_cst (TREE_TYPE (arg01), 1), 0);
7067       lo = prod;
7068
7069       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
7070       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
7071                                        TREE_INT_CST_HIGH (prod),
7072                                        TREE_INT_CST_LOW (tmp),
7073                                        TREE_INT_CST_HIGH (tmp),
7074                                        &lpart, &hpart, unsigned_p);
7075       hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
7076                                   -1, overflow | TREE_OVERFLOW (prod));
7077     }
7078   else if (tree_int_cst_sgn (arg01) >= 0)
7079     {
7080       tmp = int_const_binop (MINUS_EXPR, arg01,
7081                              build_int_cst (TREE_TYPE (arg01), 1), 0);
7082       switch (tree_int_cst_sgn (arg1))
7083         {
7084         case -1:
7085           neg_overflow = true;
7086           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
7087           hi = prod;
7088           break;
7089
7090         case  0:
7091           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
7092           hi = tmp;
7093           break;
7094
7095         case  1:
7096           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
7097           lo = prod;
7098           break;
7099
7100         default:
7101           gcc_unreachable ();
7102         }
7103     }
7104   else
7105     {
7106       /* A negative divisor reverses the relational operators.  */
7107       code = swap_tree_comparison (code);
7108
7109       tmp = int_const_binop (PLUS_EXPR, arg01,
7110                              build_int_cst (TREE_TYPE (arg01), 1), 0);
7111       switch (tree_int_cst_sgn (arg1))
7112         {
7113         case -1:
7114           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
7115           lo = prod;
7116           break;
7117
7118         case  0:
7119           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
7120           lo = tmp;
7121           break;
7122
7123         case  1:
7124           neg_overflow = true;
7125           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
7126           hi = prod;
7127           break;
7128
7129         default:
7130           gcc_unreachable ();
7131         }
7132     }
7133
7134   switch (code)
7135     {
7136     case EQ_EXPR:
7137       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
7138         return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
7139       if (TREE_OVERFLOW (hi))
7140         return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
7141       if (TREE_OVERFLOW (lo))
7142         return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
7143       return build_range_check (loc, type, arg00, 1, lo, hi);
7144
7145     case NE_EXPR:
7146       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
7147         return omit_one_operand_loc (loc, type, integer_one_node, arg00);
7148       if (TREE_OVERFLOW (hi))
7149         return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
7150       if (TREE_OVERFLOW (lo))
7151         return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
7152       return build_range_check (loc, type, arg00, 0, lo, hi);
7153
7154     case LT_EXPR:
7155       if (TREE_OVERFLOW (lo))
7156         {
7157           tmp = neg_overflow ? integer_zero_node : integer_one_node;
7158           return omit_one_operand_loc (loc, type, tmp, arg00);
7159         }
7160       return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
7161
7162     case LE_EXPR:
7163       if (TREE_OVERFLOW (hi))
7164         {
7165           tmp = neg_overflow ? integer_zero_node : integer_one_node;
7166           return omit_one_operand_loc (loc, type, tmp, arg00);
7167         }
7168       return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
7169
7170     case GT_EXPR:
7171       if (TREE_OVERFLOW (hi))
7172         {
7173           tmp = neg_overflow ? integer_one_node : integer_zero_node;
7174           return omit_one_operand_loc (loc, type, tmp, arg00);
7175         }
7176       return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
7177
7178     case GE_EXPR:
7179       if (TREE_OVERFLOW (lo))
7180         {
7181           tmp = neg_overflow ? integer_one_node : integer_zero_node;
7182           return omit_one_operand_loc (loc, type, tmp, arg00);
7183         }
7184       return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
7185
7186     default:
7187       break;
7188     }
7189
7190   return NULL_TREE;
7191 }
7192
7193
7194 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7195    equality/inequality test, then return a simplified form of the test
7196    using a sign testing.  Otherwise return NULL.  TYPE is the desired
7197    result type.  */
7198
7199 static tree
7200 fold_single_bit_test_into_sign_test (location_t loc,
7201                                      enum tree_code code, tree arg0, tree arg1,
7202                                      tree result_type)
7203 {
7204   /* If this is testing a single bit, we can optimize the test.  */
7205   if ((code == NE_EXPR || code == EQ_EXPR)
7206       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
7207       && integer_pow2p (TREE_OPERAND (arg0, 1)))
7208     {
7209       /* If we have (A & C) != 0 where C is the sign bit of A, convert
7210          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
7211       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
7212
7213       if (arg00 != NULL_TREE
7214           /* This is only a win if casting to a signed type is cheap,
7215              i.e. when arg00's type is not a partial mode.  */
7216           && TYPE_PRECISION (TREE_TYPE (arg00))
7217              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
7218         {
7219           tree stype = signed_type_for (TREE_TYPE (arg00));
7220           return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
7221                               result_type,
7222                               fold_convert_loc (loc, stype, arg00),
7223                               build_int_cst (stype, 0));
7224         }
7225     }
7226
7227   return NULL_TREE;
7228 }
7229
7230 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7231    equality/inequality test, then return a simplified form of
7232    the test using shifts and logical operations.  Otherwise return
7233    NULL.  TYPE is the desired result type.  */
7234
7235 tree
7236 fold_single_bit_test (location_t loc, enum tree_code code,
7237                       tree arg0, tree arg1, tree result_type)
7238 {
7239   /* If this is testing a single bit, we can optimize the test.  */
7240   if ((code == NE_EXPR || code == EQ_EXPR)
7241       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
7242       && integer_pow2p (TREE_OPERAND (arg0, 1)))
7243     {
7244       tree inner = TREE_OPERAND (arg0, 0);
7245       tree type = TREE_TYPE (arg0);
7246       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
7247       enum machine_mode operand_mode = TYPE_MODE (type);
7248       int ops_unsigned;
7249       tree signed_type, unsigned_type, intermediate_type;
7250       tree tem, one;
7251
7252       /* First, see if we can fold the single bit test into a sign-bit
7253          test.  */
7254       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
7255                                                  result_type);
7256       if (tem)
7257         return tem;
7258
7259       /* Otherwise we have (A & C) != 0 where C is a single bit,
7260          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
7261          Similarly for (A & C) == 0.  */
7262
7263       /* If INNER is a right shift of a constant and it plus BITNUM does
7264          not overflow, adjust BITNUM and INNER.  */
7265       if (TREE_CODE (inner) == RSHIFT_EXPR
7266           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
7267           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
7268           && bitnum < TYPE_PRECISION (type)
7269           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
7270                                    bitnum - TYPE_PRECISION (type)))
7271         {
7272           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
7273           inner = TREE_OPERAND (inner, 0);
7274         }
7275
7276       /* If we are going to be able to omit the AND below, we must do our
7277          operations as unsigned.  If we must use the AND, we have a choice.
7278          Normally unsigned is faster, but for some machines signed is.  */
7279 #ifdef LOAD_EXTEND_OP
7280       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
7281                       && !flag_syntax_only) ? 0 : 1;
7282 #else
7283       ops_unsigned = 1;
7284 #endif
7285
7286       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
7287       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
7288       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
7289       inner = fold_convert_loc (loc, intermediate_type, inner);
7290
7291       if (bitnum != 0)
7292         inner = build2 (RSHIFT_EXPR, intermediate_type,
7293                         inner, size_int (bitnum));
7294
7295       one = build_int_cst (intermediate_type, 1);
7296
7297       if (code == EQ_EXPR)
7298         inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
7299
7300       /* Put the AND last so it can combine with more things.  */
7301       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
7302
7303       /* Make sure to return the proper type.  */
7304       inner = fold_convert_loc (loc, result_type, inner);
7305
7306       return inner;
7307     }
7308   return NULL_TREE;
7309 }
7310
7311 /* Check whether we are allowed to reorder operands arg0 and arg1,
7312    such that the evaluation of arg1 occurs before arg0.  */
7313
7314 static bool
7315 reorder_operands_p (const_tree arg0, const_tree arg1)
7316 {
7317   if (! flag_evaluation_order)
7318       return true;
7319   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
7320     return true;
7321   return ! TREE_SIDE_EFFECTS (arg0)
7322          && ! TREE_SIDE_EFFECTS (arg1);
7323 }
7324
7325 /* Test whether it is preferable two swap two operands, ARG0 and
7326    ARG1, for example because ARG0 is an integer constant and ARG1
7327    isn't.  If REORDER is true, only recommend swapping if we can
7328    evaluate the operands in reverse order.  */
7329
7330 bool
7331 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
7332 {
7333   STRIP_SIGN_NOPS (arg0);
7334   STRIP_SIGN_NOPS (arg1);
7335
7336   if (TREE_CODE (arg1) == INTEGER_CST)
7337     return 0;
7338   if (TREE_CODE (arg0) == INTEGER_CST)
7339     return 1;
7340
7341   if (TREE_CODE (arg1) == REAL_CST)
7342     return 0;
7343   if (TREE_CODE (arg0) == REAL_CST)
7344     return 1;
7345
7346   if (TREE_CODE (arg1) == FIXED_CST)
7347     return 0;
7348   if (TREE_CODE (arg0) == FIXED_CST)
7349     return 1;
7350
7351   if (TREE_CODE (arg1) == COMPLEX_CST)
7352     return 0;
7353   if (TREE_CODE (arg0) == COMPLEX_CST)
7354     return 1;
7355
7356   if (TREE_CONSTANT (arg1))
7357     return 0;
7358   if (TREE_CONSTANT (arg0))
7359     return 1;
7360
7361   if (optimize_function_for_size_p (cfun))
7362     return 0;
7363
7364   if (reorder && flag_evaluation_order
7365       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7366     return 0;
7367
7368   /* It is preferable to swap two SSA_NAME to ensure a canonical form
7369      for commutative and comparison operators.  Ensuring a canonical
7370      form allows the optimizers to find additional redundancies without
7371      having to explicitly check for both orderings.  */
7372   if (TREE_CODE (arg0) == SSA_NAME
7373       && TREE_CODE (arg1) == SSA_NAME
7374       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7375     return 1;
7376
7377   /* Put SSA_NAMEs last.  */
7378   if (TREE_CODE (arg1) == SSA_NAME)
7379     return 0;
7380   if (TREE_CODE (arg0) == SSA_NAME)
7381     return 1;
7382
7383   /* Put variables last.  */
7384   if (DECL_P (arg1))
7385     return 0;
7386   if (DECL_P (arg0))
7387     return 1;
7388
7389   return 0;
7390 }
7391
7392 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7393    ARG0 is extended to a wider type.  */
7394
7395 static tree
7396 fold_widened_comparison (location_t loc, enum tree_code code,
7397                          tree type, tree arg0, tree arg1)
7398 {
7399   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7400   tree arg1_unw;
7401   tree shorter_type, outer_type;
7402   tree min, max;
7403   bool above, below;
7404
7405   if (arg0_unw == arg0)
7406     return NULL_TREE;
7407   shorter_type = TREE_TYPE (arg0_unw);
7408
7409 #ifdef HAVE_canonicalize_funcptr_for_compare
7410   /* Disable this optimization if we're casting a function pointer
7411      type on targets that require function pointer canonicalization.  */
7412   if (HAVE_canonicalize_funcptr_for_compare
7413       && TREE_CODE (shorter_type) == POINTER_TYPE
7414       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7415     return NULL_TREE;
7416 #endif
7417
7418   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7419     return NULL_TREE;
7420
7421   arg1_unw = get_unwidened (arg1, NULL_TREE);
7422
7423   /* If possible, express the comparison in the shorter mode.  */
7424   if ((code == EQ_EXPR || code == NE_EXPR
7425        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7426       && (TREE_TYPE (arg1_unw) == shorter_type
7427           || ((TYPE_PRECISION (shorter_type)
7428                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
7429               && (TYPE_UNSIGNED (shorter_type)
7430                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
7431           || (TREE_CODE (arg1_unw) == INTEGER_CST
7432               && (TREE_CODE (shorter_type) == INTEGER_TYPE
7433                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7434               && int_fits_type_p (arg1_unw, shorter_type))))
7435     return fold_build2_loc (loc, code, type, arg0_unw,
7436                         fold_convert_loc (loc, shorter_type, arg1_unw));
7437
7438   if (TREE_CODE (arg1_unw) != INTEGER_CST
7439       || TREE_CODE (shorter_type) != INTEGER_TYPE
7440       || !int_fits_type_p (arg1_unw, shorter_type))
7441     return NULL_TREE;
7442
7443   /* If we are comparing with the integer that does not fit into the range
7444      of the shorter type, the result is known.  */
7445   outer_type = TREE_TYPE (arg1_unw);
7446   min = lower_bound_in_type (outer_type, shorter_type);
7447   max = upper_bound_in_type (outer_type, shorter_type);
7448
7449   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7450                                                    max, arg1_unw));
7451   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7452                                                    arg1_unw, min));
7453
7454   switch (code)
7455     {
7456     case EQ_EXPR:
7457       if (above || below)
7458         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7459       break;
7460
7461     case NE_EXPR:
7462       if (above || below)
7463         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7464       break;
7465
7466     case LT_EXPR:
7467     case LE_EXPR:
7468       if (above)
7469         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7470       else if (below)
7471         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7472
7473     case GT_EXPR:
7474     case GE_EXPR:
7475       if (above)
7476         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7477       else if (below)
7478         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7479
7480     default:
7481       break;
7482     }
7483
7484   return NULL_TREE;
7485 }
7486
7487 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7488    ARG0 just the signedness is changed.  */
7489
7490 static tree
7491 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
7492                               tree arg0, tree arg1)
7493 {
7494   tree arg0_inner;
7495   tree inner_type, outer_type;
7496
7497   if (!CONVERT_EXPR_P (arg0))
7498     return NULL_TREE;
7499
7500   outer_type = TREE_TYPE (arg0);
7501   arg0_inner = TREE_OPERAND (arg0, 0);
7502   inner_type = TREE_TYPE (arg0_inner);
7503
7504 #ifdef HAVE_canonicalize_funcptr_for_compare
7505   /* Disable this optimization if we're casting a function pointer
7506      type on targets that require function pointer canonicalization.  */
7507   if (HAVE_canonicalize_funcptr_for_compare
7508       && TREE_CODE (inner_type) == POINTER_TYPE
7509       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7510     return NULL_TREE;
7511 #endif
7512
7513   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7514     return NULL_TREE;
7515
7516   if (TREE_CODE (arg1) != INTEGER_CST
7517       && !(CONVERT_EXPR_P (arg1)
7518            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7519     return NULL_TREE;
7520
7521   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7522        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7523       && code != NE_EXPR
7524       && code != EQ_EXPR)
7525     return NULL_TREE;
7526
7527   if (TREE_CODE (arg1) == INTEGER_CST)
7528     arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7529                                   TREE_INT_CST_HIGH (arg1), 0,
7530                                   TREE_OVERFLOW (arg1));
7531   else
7532     arg1 = fold_convert_loc (loc, inner_type, arg1);
7533
7534   return fold_build2_loc (loc, code, type, arg0_inner, arg1);
7535 }
7536
7537 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7538    step of the array.  Reconstructs s and delta in the case of s *
7539    delta being an integer constant (and thus already folded).  ADDR is
7540    the address. MULT is the multiplicative expression.  If the
7541    function succeeds, the new address expression is returned.
7542    Otherwise NULL_TREE is returned.  LOC is the location of the
7543    resulting expression.  */
7544
7545 static tree
7546 try_move_mult_to_index (location_t loc, tree addr, tree op1)
7547 {
7548   tree s, delta, step;
7549   tree ref = TREE_OPERAND (addr, 0), pref;
7550   tree ret, pos;
7551   tree itype;
7552   bool mdim = false;
7553
7554   /*  Strip the nops that might be added when converting op1 to sizetype. */
7555   STRIP_NOPS (op1);
7556
7557   /* Canonicalize op1 into a possibly non-constant delta
7558      and an INTEGER_CST s.  */
7559   if (TREE_CODE (op1) == MULT_EXPR)
7560     {
7561       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7562
7563       STRIP_NOPS (arg0);
7564       STRIP_NOPS (arg1);
7565
7566       if (TREE_CODE (arg0) == INTEGER_CST)
7567         {
7568           s = arg0;
7569           delta = arg1;
7570         }
7571       else if (TREE_CODE (arg1) == INTEGER_CST)
7572         {
7573           s = arg1;
7574           delta = arg0;
7575         }
7576       else
7577         return NULL_TREE;
7578     }
7579   else if (TREE_CODE (op1) == INTEGER_CST)
7580     {
7581       delta = op1;
7582       s = NULL_TREE;
7583     }
7584   else
7585     {
7586       /* Simulate we are delta * 1.  */
7587       delta = op1;
7588       s = integer_one_node;
7589     }
7590
7591   for (;; ref = TREE_OPERAND (ref, 0))
7592     {
7593       if (TREE_CODE (ref) == ARRAY_REF)
7594         {
7595           tree domain;
7596
7597           /* Remember if this was a multi-dimensional array.  */
7598           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7599             mdim = true;
7600
7601           domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7602           if (! domain)
7603             continue;
7604           itype = TREE_TYPE (domain);
7605
7606           step = array_ref_element_size (ref);
7607           if (TREE_CODE (step) != INTEGER_CST)
7608             continue;
7609
7610           if (s)
7611             {
7612               if (! tree_int_cst_equal (step, s))
7613                 continue;
7614             }
7615           else
7616             {
7617               /* Try if delta is a multiple of step.  */
7618               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
7619               if (! tmp)
7620                 continue;
7621               delta = tmp;
7622             }
7623
7624           /* Only fold here if we can verify we do not overflow one
7625              dimension of a multi-dimensional array.  */
7626           if (mdim)
7627             {
7628               tree tmp;
7629
7630               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7631                   || !TYPE_MAX_VALUE (domain)
7632                   || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
7633                 continue;
7634
7635               tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
7636                                      fold_convert_loc (loc, itype,
7637                                                        TREE_OPERAND (ref, 1)),
7638                                      fold_convert_loc (loc, itype, delta));
7639               if (!tmp
7640                   || TREE_CODE (tmp) != INTEGER_CST
7641                   || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
7642                 continue;
7643             }
7644
7645           break;
7646         }
7647       else
7648         mdim = false;
7649
7650       if (!handled_component_p (ref))
7651         return NULL_TREE;
7652     }
7653
7654   /* We found the suitable array reference.  So copy everything up to it,
7655      and replace the index.  */
7656
7657   pref = TREE_OPERAND (addr, 0);
7658   ret = copy_node (pref);
7659   SET_EXPR_LOCATION (ret, loc);
7660   pos = ret;
7661
7662   while (pref != ref)
7663     {
7664       pref = TREE_OPERAND (pref, 0);
7665       TREE_OPERAND (pos, 0) = copy_node (pref);
7666       pos = TREE_OPERAND (pos, 0);
7667     }
7668
7669   TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype,
7670                                        fold_convert_loc (loc, itype,
7671                                                          TREE_OPERAND (pos, 1)),
7672                                        fold_convert_loc (loc, itype, delta));
7673
7674   return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
7675 }
7676
7677
7678 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
7679    means A >= Y && A != MAX, but in this case we know that
7680    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
7681
7682 static tree
7683 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7684 {
7685   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7686
7687   if (TREE_CODE (bound) == LT_EXPR)
7688     a = TREE_OPERAND (bound, 0);
7689   else if (TREE_CODE (bound) == GT_EXPR)
7690     a = TREE_OPERAND (bound, 1);
7691   else
7692     return NULL_TREE;
7693
7694   typea = TREE_TYPE (a);
7695   if (!INTEGRAL_TYPE_P (typea)
7696       && !POINTER_TYPE_P (typea))
7697     return NULL_TREE;
7698
7699   if (TREE_CODE (ineq) == LT_EXPR)
7700     {
7701       a1 = TREE_OPERAND (ineq, 1);
7702       y = TREE_OPERAND (ineq, 0);
7703     }
7704   else if (TREE_CODE (ineq) == GT_EXPR)
7705     {
7706       a1 = TREE_OPERAND (ineq, 0);
7707       y = TREE_OPERAND (ineq, 1);
7708     }
7709   else
7710     return NULL_TREE;
7711
7712   if (TREE_TYPE (a1) != typea)
7713     return NULL_TREE;
7714
7715   if (POINTER_TYPE_P (typea))
7716     {
7717       /* Convert the pointer types into integer before taking the difference.  */
7718       tree ta = fold_convert_loc (loc, ssizetype, a);
7719       tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7720       diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7721     }
7722   else
7723     diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7724
7725   if (!diff || !integer_onep (diff))
7726    return NULL_TREE;
7727
7728   return fold_build2_loc (loc, GE_EXPR, type, a, y);
7729 }
7730
7731 /* Fold a sum or difference of at least one multiplication.
7732    Returns the folded tree or NULL if no simplification could be made.  */
7733
7734 static tree
7735 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7736                           tree arg0, tree arg1)
7737 {
7738   tree arg00, arg01, arg10, arg11;
7739   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7740
7741   /* (A * C) +- (B * C) -> (A+-B) * C.
7742      (A * C) +- A -> A * (C+-1).
7743      We are most concerned about the case where C is a constant,
7744      but other combinations show up during loop reduction.  Since
7745      it is not difficult, try all four possibilities.  */
7746
7747   if (TREE_CODE (arg0) == MULT_EXPR)
7748     {
7749       arg00 = TREE_OPERAND (arg0, 0);
7750       arg01 = TREE_OPERAND (arg0, 1);
7751     }
7752   else if (TREE_CODE (arg0) == INTEGER_CST)
7753     {
7754       arg00 = build_one_cst (type);
7755       arg01 = arg0;
7756     }
7757   else
7758     {
7759       /* We cannot generate constant 1 for fract.  */
7760       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7761         return NULL_TREE;
7762       arg00 = arg0;
7763       arg01 = build_one_cst (type);
7764     }
7765   if (TREE_CODE (arg1) == MULT_EXPR)
7766     {
7767       arg10 = TREE_OPERAND (arg1, 0);
7768       arg11 = TREE_OPERAND (arg1, 1);
7769     }
7770   else if (TREE_CODE (arg1) == INTEGER_CST)
7771     {
7772       arg10 = build_one_cst (type);
7773       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7774          the purpose of this canonicalization.  */
7775       if (TREE_INT_CST_HIGH (arg1) == -1
7776           && negate_expr_p (arg1)
7777           && code == PLUS_EXPR)
7778         {
7779           arg11 = negate_expr (arg1);
7780           code = MINUS_EXPR;
7781         }
7782       else
7783         arg11 = arg1;
7784     }
7785   else
7786     {
7787       /* We cannot generate constant 1 for fract.  */
7788       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7789         return NULL_TREE;
7790       arg10 = arg1;
7791       arg11 = build_one_cst (type);
7792     }
7793   same = NULL_TREE;
7794
7795   if (operand_equal_p (arg01, arg11, 0))
7796     same = arg01, alt0 = arg00, alt1 = arg10;
7797   else if (operand_equal_p (arg00, arg10, 0))
7798     same = arg00, alt0 = arg01, alt1 = arg11;
7799   else if (operand_equal_p (arg00, arg11, 0))
7800     same = arg00, alt0 = arg01, alt1 = arg10;
7801   else if (operand_equal_p (arg01, arg10, 0))
7802     same = arg01, alt0 = arg00, alt1 = arg11;
7803
7804   /* No identical multiplicands; see if we can find a common
7805      power-of-two factor in non-power-of-two multiplies.  This
7806      can help in multi-dimensional array access.  */
7807   else if (host_integerp (arg01, 0)
7808            && host_integerp (arg11, 0))
7809     {
7810       HOST_WIDE_INT int01, int11, tmp;
7811       bool swap = false;
7812       tree maybe_same;
7813       int01 = TREE_INT_CST_LOW (arg01);
7814       int11 = TREE_INT_CST_LOW (arg11);
7815
7816       /* Move min of absolute values to int11.  */
7817       if ((int01 >= 0 ? int01 : -int01)
7818           < (int11 >= 0 ? int11 : -int11))
7819         {
7820           tmp = int01, int01 = int11, int11 = tmp;
7821           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7822           maybe_same = arg01;
7823           swap = true;
7824         }
7825       else
7826         maybe_same = arg11;
7827
7828       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7829           /* The remainder should not be a constant, otherwise we
7830              end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7831              increased the number of multiplications necessary.  */
7832           && TREE_CODE (arg10) != INTEGER_CST)
7833         {
7834           alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7835                               build_int_cst (TREE_TYPE (arg00),
7836                                              int01 / int11));
7837           alt1 = arg10;
7838           same = maybe_same;
7839           if (swap)
7840             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7841         }
7842     }
7843
7844   if (same)
7845     return fold_build2_loc (loc, MULT_EXPR, type,
7846                         fold_build2_loc (loc, code, type,
7847                                      fold_convert_loc (loc, type, alt0),
7848                                      fold_convert_loc (loc, type, alt1)),
7849                         fold_convert_loc (loc, type, same));
7850
7851   return NULL_TREE;
7852 }
7853
7854 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7855    specified by EXPR into the buffer PTR of length LEN bytes.
7856    Return the number of bytes placed in the buffer, or zero
7857    upon failure.  */
7858
7859 static int
7860 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7861 {
7862   tree type = TREE_TYPE (expr);
7863   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7864   int byte, offset, word, words;
7865   unsigned char value;
7866
7867   if (total_bytes > len)
7868     return 0;
7869   words = total_bytes / UNITS_PER_WORD;
7870
7871   for (byte = 0; byte < total_bytes; byte++)
7872     {
7873       int bitpos = byte * BITS_PER_UNIT;
7874       if (bitpos < HOST_BITS_PER_WIDE_INT)
7875         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7876       else
7877         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7878                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7879
7880       if (total_bytes > UNITS_PER_WORD)
7881         {
7882           word = byte / UNITS_PER_WORD;
7883           if (WORDS_BIG_ENDIAN)
7884             word = (words - 1) - word;
7885           offset = word * UNITS_PER_WORD;
7886           if (BYTES_BIG_ENDIAN)
7887             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7888           else
7889             offset += byte % UNITS_PER_WORD;
7890         }
7891       else
7892         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7893       ptr[offset] = value;
7894     }
7895   return total_bytes;
7896 }
7897
7898
7899 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7900    specified by EXPR into the buffer PTR of length LEN bytes.
7901    Return the number of bytes placed in the buffer, or zero
7902    upon failure.  */
7903
7904 static int
7905 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7906 {
7907   tree type = TREE_TYPE (expr);
7908   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7909   int byte, offset, word, words, bitpos;
7910   unsigned char value;
7911
7912   /* There are always 32 bits in each long, no matter the size of
7913      the hosts long.  We handle floating point representations with
7914      up to 192 bits.  */
7915   long tmp[6];
7916
7917   if (total_bytes > len)
7918     return 0;
7919   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7920
7921   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7922
7923   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7924        bitpos += BITS_PER_UNIT)
7925     {
7926       byte = (bitpos / BITS_PER_UNIT) & 3;
7927       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7928
7929       if (UNITS_PER_WORD < 4)
7930         {
7931           word = byte / UNITS_PER_WORD;
7932           if (WORDS_BIG_ENDIAN)
7933             word = (words - 1) - word;
7934           offset = word * UNITS_PER_WORD;
7935           if (BYTES_BIG_ENDIAN)
7936             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7937           else
7938             offset += byte % UNITS_PER_WORD;
7939         }
7940       else
7941         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7942       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7943     }
7944   return total_bytes;
7945 }
7946
7947 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7948    specified by EXPR into the buffer PTR of length LEN bytes.
7949    Return the number of bytes placed in the buffer, or zero
7950    upon failure.  */
7951
7952 static int
7953 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7954 {
7955   int rsize, isize;
7956   tree part;
7957
7958   part = TREE_REALPART (expr);
7959   rsize = native_encode_expr (part, ptr, len);
7960   if (rsize == 0)
7961     return 0;
7962   part = TREE_IMAGPART (expr);
7963   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7964   if (isize != rsize)
7965     return 0;
7966   return rsize + isize;
7967 }
7968
7969
7970 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7971    specified by EXPR into the buffer PTR of length LEN bytes.
7972    Return the number of bytes placed in the buffer, or zero
7973    upon failure.  */
7974
7975 static int
7976 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7977 {
7978   int i, size, offset, count;
7979   tree itype, elem, elements;
7980
7981   offset = 0;
7982   elements = TREE_VECTOR_CST_ELTS (expr);
7983   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7984   itype = TREE_TYPE (TREE_TYPE (expr));
7985   size = GET_MODE_SIZE (TYPE_MODE (itype));
7986   for (i = 0; i < count; i++)
7987     {
7988       if (elements)
7989         {
7990           elem = TREE_VALUE (elements);
7991           elements = TREE_CHAIN (elements);
7992         }
7993       else
7994         elem = NULL_TREE;
7995
7996       if (elem)
7997         {
7998           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7999             return 0;
8000         }
8001       else
8002         {
8003           if (offset + size > len)
8004             return 0;
8005           memset (ptr+offset, 0, size);
8006         }
8007       offset += size;
8008     }
8009   return offset;
8010 }
8011
8012
8013 /* Subroutine of native_encode_expr.  Encode the STRING_CST
8014    specified by EXPR into the buffer PTR of length LEN bytes.
8015    Return the number of bytes placed in the buffer, or zero
8016    upon failure.  */
8017
8018 static int
8019 native_encode_string (const_tree expr, unsigned char *ptr, int len)
8020 {
8021   tree type = TREE_TYPE (expr);
8022   HOST_WIDE_INT total_bytes;
8023
8024   if (TREE_CODE (type) != ARRAY_TYPE
8025       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
8026       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
8027       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
8028     return 0;
8029   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
8030   if (total_bytes > len)
8031     return 0;
8032   if (TREE_STRING_LENGTH (expr) < total_bytes)
8033     {
8034       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
8035       memset (ptr + TREE_STRING_LENGTH (expr), 0,
8036               total_bytes - TREE_STRING_LENGTH (expr));
8037     }
8038   else
8039     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
8040   return total_bytes;
8041 }
8042
8043
8044 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
8045    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
8046    buffer PTR of length LEN bytes.  Return the number of bytes
8047    placed in the buffer, or zero upon failure.  */
8048
8049 int
8050 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
8051 {
8052   switch (TREE_CODE (expr))
8053     {
8054     case INTEGER_CST:
8055       return native_encode_int (expr, ptr, len);
8056
8057     case REAL_CST:
8058       return native_encode_real (expr, ptr, len);
8059
8060     case COMPLEX_CST:
8061       return native_encode_complex (expr, ptr, len);
8062
8063     case VECTOR_CST:
8064       return native_encode_vector (expr, ptr, len);
8065
8066     case STRING_CST:
8067       return native_encode_string (expr, ptr, len);
8068
8069     default:
8070       return 0;
8071     }
8072 }
8073
8074
8075 /* Subroutine of native_interpret_expr.  Interpret the contents of
8076    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8077    If the buffer cannot be interpreted, return NULL_TREE.  */
8078
8079 static tree
8080 native_interpret_int (tree type, const unsigned char *ptr, int len)
8081 {
8082   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
8083   int byte, offset, word, words;
8084   unsigned char value;
8085   unsigned int HOST_WIDE_INT lo = 0;
8086   HOST_WIDE_INT hi = 0;
8087
8088   if (total_bytes > len)
8089     return NULL_TREE;
8090   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
8091     return NULL_TREE;
8092   words = total_bytes / UNITS_PER_WORD;
8093
8094   for (byte = 0; byte < total_bytes; byte++)
8095     {
8096       int bitpos = byte * BITS_PER_UNIT;
8097       if (total_bytes > UNITS_PER_WORD)
8098         {
8099           word = byte / UNITS_PER_WORD;
8100           if (WORDS_BIG_ENDIAN)
8101             word = (words - 1) - word;
8102           offset = word * UNITS_PER_WORD;
8103           if (BYTES_BIG_ENDIAN)
8104             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8105           else
8106             offset += byte % UNITS_PER_WORD;
8107         }
8108       else
8109         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
8110       value = ptr[offset];
8111
8112       if (bitpos < HOST_BITS_PER_WIDE_INT)
8113         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
8114       else
8115         hi |= (unsigned HOST_WIDE_INT) value
8116               << (bitpos - HOST_BITS_PER_WIDE_INT);
8117     }
8118
8119   return build_int_cst_wide_type (type, lo, hi);
8120 }
8121
8122
8123 /* Subroutine of native_interpret_expr.  Interpret the contents of
8124    the buffer PTR of length LEN as a REAL_CST of type TYPE.
8125    If the buffer cannot be interpreted, return NULL_TREE.  */
8126
8127 static tree
8128 native_interpret_real (tree type, const unsigned char *ptr, int len)
8129 {
8130   enum machine_mode mode = TYPE_MODE (type);
8131   int total_bytes = GET_MODE_SIZE (mode);
8132   int byte, offset, word, words, bitpos;
8133   unsigned char value;
8134   /* There are always 32 bits in each long, no matter the size of
8135      the hosts long.  We handle floating point representations with
8136      up to 192 bits.  */
8137   REAL_VALUE_TYPE r;
8138   long tmp[6];
8139
8140   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
8141   if (total_bytes > len || total_bytes > 24)
8142     return NULL_TREE;
8143   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8144
8145   memset (tmp, 0, sizeof (tmp));
8146   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8147        bitpos += BITS_PER_UNIT)
8148     {
8149       byte = (bitpos / BITS_PER_UNIT) & 3;
8150       if (UNITS_PER_WORD < 4)
8151         {
8152           word = byte / UNITS_PER_WORD;
8153           if (WORDS_BIG_ENDIAN)
8154             word = (words - 1) - word;
8155           offset = word * UNITS_PER_WORD;
8156           if (BYTES_BIG_ENDIAN)
8157             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8158           else
8159             offset += byte % UNITS_PER_WORD;
8160         }
8161       else
8162         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
8163       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8164
8165       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8166     }
8167
8168   real_from_target (&r, tmp, mode);
8169   return build_real (type, r);
8170 }
8171
8172
8173 /* Subroutine of native_interpret_expr.  Interpret the contents of
8174    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8175    If the buffer cannot be interpreted, return NULL_TREE.  */
8176
8177 static tree
8178 native_interpret_complex (tree type, const unsigned char *ptr, int len)
8179 {
8180   tree etype, rpart, ipart;
8181   int size;
8182
8183   etype = TREE_TYPE (type);
8184   size = GET_MODE_SIZE (TYPE_MODE (etype));
8185   if (size * 2 > len)
8186     return NULL_TREE;
8187   rpart = native_interpret_expr (etype, ptr, size);
8188   if (!rpart)
8189     return NULL_TREE;
8190   ipart = native_interpret_expr (etype, ptr+size, size);
8191   if (!ipart)
8192     return NULL_TREE;
8193   return build_complex (type, rpart, ipart);
8194 }
8195
8196
8197 /* Subroutine of native_interpret_expr.  Interpret the contents of
8198    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8199    If the buffer cannot be interpreted, return NULL_TREE.  */
8200
8201 static tree
8202 native_interpret_vector (tree type, const unsigned char *ptr, int len)
8203 {
8204   tree etype, elem, elements;
8205   int i, size, count;
8206
8207   etype = TREE_TYPE (type);
8208   size = GET_MODE_SIZE (TYPE_MODE (etype));
8209   count = TYPE_VECTOR_SUBPARTS (type);
8210   if (size * count > len)
8211     return NULL_TREE;
8212
8213   elements = NULL_TREE;
8214   for (i = count - 1; i >= 0; i--)
8215     {
8216       elem = native_interpret_expr (etype, ptr+(i*size), size);
8217       if (!elem)
8218         return NULL_TREE;
8219       elements = tree_cons (NULL_TREE, elem, elements);
8220     }
8221   return build_vector (type, elements);
8222 }
8223
8224
8225 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
8226    the buffer PTR of length LEN as a constant of type TYPE.  For
8227    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8228    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
8229    return NULL_TREE.  */
8230
8231 tree
8232 native_interpret_expr (tree type, const unsigned char *ptr, int len)
8233 {
8234   switch (TREE_CODE (type))
8235     {
8236     case INTEGER_TYPE:
8237     case ENUMERAL_TYPE:
8238     case BOOLEAN_TYPE:
8239       return native_interpret_int (type, ptr, len);
8240
8241     case REAL_TYPE:
8242       return native_interpret_real (type, ptr, len);
8243
8244     case COMPLEX_TYPE:
8245       return native_interpret_complex (type, ptr, len);
8246
8247     case VECTOR_TYPE:
8248       return native_interpret_vector (type, ptr, len);
8249
8250     default:
8251       return NULL_TREE;
8252     }
8253 }
8254
8255
8256 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
8257    TYPE at compile-time.  If we're unable to perform the conversion
8258    return NULL_TREE.  */
8259
8260 static tree
8261 fold_view_convert_expr (tree type, tree expr)
8262 {
8263   /* We support up to 512-bit values (for V8DFmode).  */
8264   unsigned char buffer[64];
8265   int len;
8266
8267   /* Check that the host and target are sane.  */
8268   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
8269     return NULL_TREE;
8270
8271   len = native_encode_expr (expr, buffer, sizeof (buffer));
8272   if (len == 0)
8273     return NULL_TREE;
8274
8275   return native_interpret_expr (type, buffer, len);
8276 }
8277
8278 /* Build an expression for the address of T.  Folds away INDIRECT_REF
8279    to avoid confusing the gimplify process.  */
8280
8281 tree
8282 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
8283 {
8284   /* The size of the object is not relevant when talking about its address.  */
8285   if (TREE_CODE (t) == WITH_SIZE_EXPR)
8286     t = TREE_OPERAND (t, 0);
8287
8288   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
8289   if (TREE_CODE (t) == INDIRECT_REF
8290       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
8291     {
8292       t = TREE_OPERAND (t, 0);
8293
8294       if (TREE_TYPE (t) != ptrtype)
8295         {
8296           t = build1 (NOP_EXPR, ptrtype, t);
8297           SET_EXPR_LOCATION (t, loc);
8298         }
8299     }
8300   else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
8301     {
8302       t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
8303
8304       if (TREE_TYPE (t) != ptrtype)
8305         t = fold_convert_loc (loc, ptrtype, t);
8306     }
8307   else
8308     {
8309       t = build1 (ADDR_EXPR, ptrtype, t);
8310       SET_EXPR_LOCATION (t, loc);
8311     }
8312
8313   return t;
8314 }
8315
8316 /* Build an expression for the address of T.  */
8317
8318 tree
8319 build_fold_addr_expr_loc (location_t loc, tree t)
8320 {
8321   tree ptrtype = build_pointer_type (TREE_TYPE (t));
8322
8323   return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
8324 }
8325
8326 /* Fold a unary expression of code CODE and type TYPE with operand
8327    OP0.  Return the folded expression if folding is successful.
8328    Otherwise, return NULL_TREE.  */
8329
8330 tree
8331 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
8332 {
8333   tree tem;
8334   tree arg0;
8335   enum tree_code_class kind = TREE_CODE_CLASS (code);
8336
8337   gcc_assert (IS_EXPR_CODE_CLASS (kind)
8338               && TREE_CODE_LENGTH (code) == 1);
8339
8340   arg0 = op0;
8341   if (arg0)
8342     {
8343       if (CONVERT_EXPR_CODE_P (code)
8344           || code == FLOAT_EXPR || code == ABS_EXPR)
8345         {
8346           /* Don't use STRIP_NOPS, because signedness of argument type
8347              matters.  */
8348           STRIP_SIGN_NOPS (arg0);
8349         }
8350       else
8351         {
8352           /* Strip any conversions that don't change the mode.  This
8353              is safe for every expression, except for a comparison
8354              expression because its signedness is derived from its
8355              operands.
8356
8357              Note that this is done as an internal manipulation within
8358              the constant folder, in order to find the simplest
8359              representation of the arguments so that their form can be
8360              studied.  In any cases, the appropriate type conversions
8361              should be put back in the tree that will get out of the
8362              constant folder.  */
8363           STRIP_NOPS (arg0);
8364         }
8365     }
8366
8367   if (TREE_CODE_CLASS (code) == tcc_unary)
8368     {
8369       if (TREE_CODE (arg0) == COMPOUND_EXPR)
8370         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
8371                        fold_build1_loc (loc, code, type,
8372                                     fold_convert_loc (loc, TREE_TYPE (op0),
8373                                                       TREE_OPERAND (arg0, 1))));
8374       else if (TREE_CODE (arg0) == COND_EXPR)
8375         {
8376           tree arg01 = TREE_OPERAND (arg0, 1);
8377           tree arg02 = TREE_OPERAND (arg0, 2);
8378           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8379             arg01 = fold_build1_loc (loc, code, type,
8380                                  fold_convert_loc (loc,
8381                                                    TREE_TYPE (op0), arg01));
8382           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8383             arg02 = fold_build1_loc (loc, code, type,
8384                                  fold_convert_loc (loc,
8385                                                    TREE_TYPE (op0), arg02));
8386           tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
8387                              arg01, arg02);
8388
8389           /* If this was a conversion, and all we did was to move into
8390              inside the COND_EXPR, bring it back out.  But leave it if
8391              it is a conversion from integer to integer and the
8392              result precision is no wider than a word since such a
8393              conversion is cheap and may be optimized away by combine,
8394              while it couldn't if it were outside the COND_EXPR.  Then return
8395              so we don't get into an infinite recursion loop taking the
8396              conversion out and then back in.  */
8397
8398           if ((CONVERT_EXPR_CODE_P (code)
8399                || code == NON_LVALUE_EXPR)
8400               && TREE_CODE (tem) == COND_EXPR
8401               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8402               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8403               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8404               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8405               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8406                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8407               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8408                      && (INTEGRAL_TYPE_P
8409                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8410                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8411                   || flag_syntax_only))
8412             {
8413               tem = build1 (code, type,
8414                             build3 (COND_EXPR,
8415                                     TREE_TYPE (TREE_OPERAND
8416                                                (TREE_OPERAND (tem, 1), 0)),
8417                                     TREE_OPERAND (tem, 0),
8418                                     TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8419                                     TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8420               SET_EXPR_LOCATION (tem, loc);
8421             }
8422           return tem;
8423         }
8424       else if (COMPARISON_CLASS_P (arg0))
8425         {
8426           if (TREE_CODE (type) == BOOLEAN_TYPE)
8427             {
8428               arg0 = copy_node (arg0);
8429               TREE_TYPE (arg0) = type;
8430               return arg0;
8431             }
8432           else if (TREE_CODE (type) != INTEGER_TYPE)
8433             return fold_build3_loc (loc, COND_EXPR, type, arg0,
8434                                 fold_build1_loc (loc, code, type,
8435                                              integer_one_node),
8436                                 fold_build1_loc (loc, code, type,
8437                                              integer_zero_node));
8438         }
8439    }
8440
8441   switch (code)
8442     {
8443     case PAREN_EXPR:
8444       /* Re-association barriers around constants and other re-association
8445          barriers can be removed.  */
8446       if (CONSTANT_CLASS_P (op0)
8447           || TREE_CODE (op0) == PAREN_EXPR)
8448         return fold_convert_loc (loc, type, op0);
8449       return NULL_TREE;
8450
8451     CASE_CONVERT:
8452     case FLOAT_EXPR:
8453     case FIX_TRUNC_EXPR:
8454       if (TREE_TYPE (op0) == type)
8455         return op0;
8456
8457       /* If we have (type) (a CMP b) and type is an integral type, return
8458          new expression involving the new type.  */
8459       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8460         return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8461                             TREE_OPERAND (op0, 1));
8462
8463       /* Handle cases of two conversions in a row.  */
8464       if (CONVERT_EXPR_P (op0))
8465         {
8466           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8467           tree inter_type = TREE_TYPE (op0);
8468           int inside_int = INTEGRAL_TYPE_P (inside_type);
8469           int inside_ptr = POINTER_TYPE_P (inside_type);
8470           int inside_float = FLOAT_TYPE_P (inside_type);
8471           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8472           unsigned int inside_prec = TYPE_PRECISION (inside_type);
8473           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8474           int inter_int = INTEGRAL_TYPE_P (inter_type);
8475           int inter_ptr = POINTER_TYPE_P (inter_type);
8476           int inter_float = FLOAT_TYPE_P (inter_type);
8477           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8478           unsigned int inter_prec = TYPE_PRECISION (inter_type);
8479           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8480           int final_int = INTEGRAL_TYPE_P (type);
8481           int final_ptr = POINTER_TYPE_P (type);
8482           int final_float = FLOAT_TYPE_P (type);
8483           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8484           unsigned int final_prec = TYPE_PRECISION (type);
8485           int final_unsignedp = TYPE_UNSIGNED (type);
8486
8487           /* In addition to the cases of two conversions in a row
8488              handled below, if we are converting something to its own
8489              type via an object of identical or wider precision, neither
8490              conversion is needed.  */
8491           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8492               && (((inter_int || inter_ptr) && final_int)
8493                   || (inter_float && final_float))
8494               && inter_prec >= final_prec)
8495             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8496
8497           /* Likewise, if the intermediate and initial types are either both
8498              float or both integer, we don't need the middle conversion if the
8499              former is wider than the latter and doesn't change the signedness
8500              (for integers).  Avoid this if the final type is a pointer since
8501              then we sometimes need the middle conversion.  Likewise if the
8502              final type has a precision not equal to the size of its mode.  */
8503           if (((inter_int && inside_int)
8504                || (inter_float && inside_float)
8505                || (inter_vec && inside_vec))
8506               && inter_prec >= inside_prec
8507               && (inter_float || inter_vec
8508                   || inter_unsignedp == inside_unsignedp)
8509               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8510                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
8511               && ! final_ptr
8512               && (! final_vec || inter_prec == inside_prec))
8513             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8514
8515           /* If we have a sign-extension of a zero-extended value, we can
8516              replace that by a single zero-extension.  */
8517           if (inside_int && inter_int && final_int
8518               && inside_prec < inter_prec && inter_prec < final_prec
8519               && inside_unsignedp && !inter_unsignedp)
8520             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8521
8522           /* Two conversions in a row are not needed unless:
8523              - some conversion is floating-point (overstrict for now), or
8524              - some conversion is a vector (overstrict for now), or
8525              - the intermediate type is narrower than both initial and
8526                final, or
8527              - the intermediate type and innermost type differ in signedness,
8528                and the outermost type is wider than the intermediate, or
8529              - the initial type is a pointer type and the precisions of the
8530                intermediate and final types differ, or
8531              - the final type is a pointer type and the precisions of the
8532                initial and intermediate types differ.  */
8533           if (! inside_float && ! inter_float && ! final_float
8534               && ! inside_vec && ! inter_vec && ! final_vec
8535               && (inter_prec >= inside_prec || inter_prec >= final_prec)
8536               && ! (inside_int && inter_int
8537                     && inter_unsignedp != inside_unsignedp
8538                     && inter_prec < final_prec)
8539               && ((inter_unsignedp && inter_prec > inside_prec)
8540                   == (final_unsignedp && final_prec > inter_prec))
8541               && ! (inside_ptr && inter_prec != final_prec)
8542               && ! (final_ptr && inside_prec != inter_prec)
8543               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8544                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
8545             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8546         }
8547
8548       /* Handle (T *)&A.B.C for A being of type T and B and C
8549          living at offset zero.  This occurs frequently in
8550          C++ upcasting and then accessing the base.  */
8551       if (TREE_CODE (op0) == ADDR_EXPR
8552           && POINTER_TYPE_P (type)
8553           && handled_component_p (TREE_OPERAND (op0, 0)))
8554         {
8555           HOST_WIDE_INT bitsize, bitpos;
8556           tree offset;
8557           enum machine_mode mode;
8558           int unsignedp, volatilep;
8559           tree base = TREE_OPERAND (op0, 0);
8560           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8561                                       &mode, &unsignedp, &volatilep, false);
8562           /* If the reference was to a (constant) zero offset, we can use
8563              the address of the base if it has the same base type
8564              as the result type.  */
8565           if (! offset && bitpos == 0
8566               && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8567                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8568             return fold_convert_loc (loc, type,
8569                                      build_fold_addr_expr_loc (loc, base));
8570         }
8571
8572       if (TREE_CODE (op0) == MODIFY_EXPR
8573           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
8574           /* Detect assigning a bitfield.  */
8575           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
8576                && DECL_BIT_FIELD
8577                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
8578         {
8579           /* Don't leave an assignment inside a conversion
8580              unless assigning a bitfield.  */
8581           tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
8582           /* First do the assignment, then return converted constant.  */
8583           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8584           TREE_NO_WARNING (tem) = 1;
8585           TREE_USED (tem) = 1;
8586           SET_EXPR_LOCATION (tem, loc);
8587           return tem;
8588         }
8589
8590       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8591          constants (if x has signed type, the sign bit cannot be set
8592          in c).  This folds extension into the BIT_AND_EXPR.
8593          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8594          very likely don't have maximal range for their precision and this
8595          transformation effectively doesn't preserve non-maximal ranges.  */
8596       if (TREE_CODE (type) == INTEGER_TYPE
8597           && TREE_CODE (op0) == BIT_AND_EXPR
8598           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
8599         {
8600           tree and_expr = op0;
8601           tree and0 = TREE_OPERAND (and_expr, 0);
8602           tree and1 = TREE_OPERAND (and_expr, 1);
8603           int change = 0;
8604
8605           if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
8606               || (TYPE_PRECISION (type)
8607                   <= TYPE_PRECISION (TREE_TYPE (and_expr))))
8608             change = 1;
8609           else if (TYPE_PRECISION (TREE_TYPE (and1))
8610                    <= HOST_BITS_PER_WIDE_INT
8611                    && host_integerp (and1, 1))
8612             {
8613               unsigned HOST_WIDE_INT cst;
8614
8615               cst = tree_low_cst (and1, 1);
8616               cst &= (HOST_WIDE_INT) -1
8617                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8618               change = (cst == 0);
8619 #ifdef LOAD_EXTEND_OP
8620               if (change
8621                   && !flag_syntax_only
8622                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8623                       == ZERO_EXTEND))
8624                 {
8625                   tree uns = unsigned_type_for (TREE_TYPE (and0));
8626                   and0 = fold_convert_loc (loc, uns, and0);
8627                   and1 = fold_convert_loc (loc, uns, and1);
8628                 }
8629 #endif
8630             }
8631           if (change)
8632             {
8633               tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8634                                            TREE_INT_CST_HIGH (and1), 0,
8635                                            TREE_OVERFLOW (and1));
8636               return fold_build2_loc (loc, BIT_AND_EXPR, type,
8637                                   fold_convert_loc (loc, type, and0), tem);
8638             }
8639         }
8640
8641       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8642          when one of the new casts will fold away. Conservatively we assume
8643          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8644       if (POINTER_TYPE_P (type)
8645           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8646           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8647               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8648               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8649         {
8650           tree arg00 = TREE_OPERAND (arg0, 0);
8651           tree arg01 = TREE_OPERAND (arg0, 1);
8652
8653           return fold_build2_loc (loc,
8654                               TREE_CODE (arg0), type,
8655                               fold_convert_loc (loc, type, arg00),
8656                               fold_convert_loc (loc, sizetype, arg01));
8657         }
8658
8659       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8660          of the same precision, and X is an integer type not narrower than
8661          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
8662       if (INTEGRAL_TYPE_P (type)
8663           && TREE_CODE (op0) == BIT_NOT_EXPR
8664           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8665           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8666           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8667         {
8668           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8669           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8670               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8671             return fold_build1_loc (loc, BIT_NOT_EXPR, type,
8672                                 fold_convert_loc (loc, type, tem));
8673         }
8674
8675       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8676          type of X and Y (integer types only).  */
8677       if (INTEGRAL_TYPE_P (type)
8678           && TREE_CODE (op0) == MULT_EXPR
8679           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8680           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8681         {
8682           /* Be careful not to introduce new overflows.  */
8683           tree mult_type;
8684           if (TYPE_OVERFLOW_WRAPS (type))
8685             mult_type = type;
8686           else
8687             mult_type = unsigned_type_for (type);
8688
8689           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8690             {
8691               tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8692                                  fold_convert_loc (loc, mult_type,
8693                                                    TREE_OPERAND (op0, 0)),
8694                                  fold_convert_loc (loc, mult_type,
8695                                                    TREE_OPERAND (op0, 1)));
8696               return fold_convert_loc (loc, type, tem);
8697             }
8698         }
8699
8700       tem = fold_convert_const (code, type, op0);
8701       return tem ? tem : NULL_TREE;
8702
8703     case ADDR_SPACE_CONVERT_EXPR:
8704       if (integer_zerop (arg0))
8705         return fold_convert_const (code, type, arg0);
8706       return NULL_TREE;
8707
8708     case FIXED_CONVERT_EXPR:
8709       tem = fold_convert_const (code, type, arg0);
8710       return tem ? tem : NULL_TREE;
8711
8712     case VIEW_CONVERT_EXPR:
8713       if (TREE_TYPE (op0) == type)
8714         return op0;
8715       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8716         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8717                             type, TREE_OPERAND (op0, 0));
8718
8719       /* For integral conversions with the same precision or pointer
8720          conversions use a NOP_EXPR instead.  */
8721       if ((INTEGRAL_TYPE_P (type)
8722            || POINTER_TYPE_P (type))
8723           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8724               || POINTER_TYPE_P (TREE_TYPE (op0)))
8725           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8726         return fold_convert_loc (loc, type, op0);
8727
8728       /* Strip inner integral conversions that do not change the precision.  */
8729       if (CONVERT_EXPR_P (op0)
8730           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8731               || POINTER_TYPE_P (TREE_TYPE (op0)))
8732           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8733               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8734           && (TYPE_PRECISION (TREE_TYPE (op0))
8735               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8736         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8737                             type, TREE_OPERAND (op0, 0));
8738
8739       return fold_view_convert_expr (type, op0);
8740
8741     case NEGATE_EXPR:
8742       tem = fold_negate_expr (loc, arg0);
8743       if (tem)
8744         return fold_convert_loc (loc, type, tem);
8745       return NULL_TREE;
8746
8747     case ABS_EXPR:
8748       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8749         return fold_abs_const (arg0, type);
8750       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8751         return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8752       /* Convert fabs((double)float) into (double)fabsf(float).  */
8753       else if (TREE_CODE (arg0) == NOP_EXPR
8754                && TREE_CODE (type) == REAL_TYPE)
8755         {
8756           tree targ0 = strip_float_extensions (arg0);
8757           if (targ0 != arg0)
8758             return fold_convert_loc (loc, type,
8759                                      fold_build1_loc (loc, ABS_EXPR,
8760                                                   TREE_TYPE (targ0),
8761                                                   targ0));
8762         }
8763       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8764       else if (TREE_CODE (arg0) == ABS_EXPR)
8765         return arg0;
8766       else if (tree_expr_nonnegative_p (arg0))
8767         return arg0;
8768
8769       /* Strip sign ops from argument.  */
8770       if (TREE_CODE (type) == REAL_TYPE)
8771         {
8772           tem = fold_strip_sign_ops (arg0);
8773           if (tem)
8774             return fold_build1_loc (loc, ABS_EXPR, type,
8775                                 fold_convert_loc (loc, type, tem));
8776         }
8777       return NULL_TREE;
8778
8779     case CONJ_EXPR:
8780       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8781         return fold_convert_loc (loc, type, arg0);
8782       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8783         {
8784           tree itype = TREE_TYPE (type);
8785           tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8786           tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8787           return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8788                               negate_expr (ipart));
8789         }
8790       if (TREE_CODE (arg0) == COMPLEX_CST)
8791         {
8792           tree itype = TREE_TYPE (type);
8793           tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8794           tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8795           return build_complex (type, rpart, negate_expr (ipart));
8796         }
8797       if (TREE_CODE (arg0) == CONJ_EXPR)
8798         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8799       return NULL_TREE;
8800
8801     case BIT_NOT_EXPR:
8802       if (TREE_CODE (arg0) == INTEGER_CST)
8803         return fold_not_const (arg0, type);
8804       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8805         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8806       /* Convert ~ (-A) to A - 1.  */
8807       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8808         return fold_build2_loc (loc, MINUS_EXPR, type,
8809                             fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8810                             build_int_cst (type, 1));
8811       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8812       else if (INTEGRAL_TYPE_P (type)
8813                && ((TREE_CODE (arg0) == MINUS_EXPR
8814                     && integer_onep (TREE_OPERAND (arg0, 1)))
8815                    || (TREE_CODE (arg0) == PLUS_EXPR
8816                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8817         return fold_build1_loc (loc, NEGATE_EXPR, type,
8818                             fold_convert_loc (loc, type,
8819                                               TREE_OPERAND (arg0, 0)));
8820       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8821       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8822                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8823                                      fold_convert_loc (loc, type,
8824                                                        TREE_OPERAND (arg0, 0)))))
8825         return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8826                             fold_convert_loc (loc, type,
8827                                               TREE_OPERAND (arg0, 1)));
8828       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8829                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8830                                      fold_convert_loc (loc, type,
8831                                                        TREE_OPERAND (arg0, 1)))))
8832         return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8833                             fold_convert_loc (loc, type,
8834                                               TREE_OPERAND (arg0, 0)), tem);
8835       /* Perform BIT_NOT_EXPR on each element individually.  */
8836       else if (TREE_CODE (arg0) == VECTOR_CST)
8837         {
8838           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8839           int count = TYPE_VECTOR_SUBPARTS (type), i;
8840
8841           for (i = 0; i < count; i++)
8842             {
8843               if (elements)
8844                 {
8845                   elem = TREE_VALUE (elements);
8846                   elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8847                   if (elem == NULL_TREE)
8848                     break;
8849                   elements = TREE_CHAIN (elements);
8850                 }
8851               else
8852                 elem = build_int_cst (TREE_TYPE (type), -1);
8853               list = tree_cons (NULL_TREE, elem, list);
8854             }
8855           if (i == count)
8856             return build_vector (type, nreverse (list));
8857         }
8858
8859       return NULL_TREE;
8860
8861     case TRUTH_NOT_EXPR:
8862       /* The argument to invert_truthvalue must have Boolean type.  */
8863       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8864           arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8865
8866       /* Note that the operand of this must be an int
8867          and its values must be 0 or 1.
8868          ("true" is a fixed value perhaps depending on the language,
8869          but we don't handle values other than 1 correctly yet.)  */
8870       tem = fold_truth_not_expr (loc, arg0);
8871       if (!tem)
8872         return NULL_TREE;
8873       return fold_convert_loc (loc, type, tem);
8874
8875     case REALPART_EXPR:
8876       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8877         return fold_convert_loc (loc, type, arg0);
8878       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8879         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8880                                  TREE_OPERAND (arg0, 1));
8881       if (TREE_CODE (arg0) == COMPLEX_CST)
8882         return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8883       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8884         {
8885           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8886           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8887                              fold_build1_loc (loc, REALPART_EXPR, itype,
8888                                           TREE_OPERAND (arg0, 0)),
8889                              fold_build1_loc (loc, REALPART_EXPR, itype,
8890                                           TREE_OPERAND (arg0, 1)));
8891           return fold_convert_loc (loc, type, tem);
8892         }
8893       if (TREE_CODE (arg0) == CONJ_EXPR)
8894         {
8895           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8896           tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8897                              TREE_OPERAND (arg0, 0));
8898           return fold_convert_loc (loc, type, tem);
8899         }
8900       if (TREE_CODE (arg0) == CALL_EXPR)
8901         {
8902           tree fn = get_callee_fndecl (arg0);
8903           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8904             switch (DECL_FUNCTION_CODE (fn))
8905               {
8906               CASE_FLT_FN (BUILT_IN_CEXPI):
8907                 fn = mathfn_built_in (type, BUILT_IN_COS);
8908                 if (fn)
8909                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8910                 break;
8911
8912               default:
8913                 break;
8914               }
8915         }
8916       return NULL_TREE;
8917
8918     case IMAGPART_EXPR:
8919       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8920         return fold_convert_loc (loc, type, integer_zero_node);
8921       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8922         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8923                                  TREE_OPERAND (arg0, 0));
8924       if (TREE_CODE (arg0) == COMPLEX_CST)
8925         return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8926       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8927         {
8928           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8929           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8930                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8931                                           TREE_OPERAND (arg0, 0)),
8932                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8933                                           TREE_OPERAND (arg0, 1)));
8934           return fold_convert_loc (loc, type, tem);
8935         }
8936       if (TREE_CODE (arg0) == CONJ_EXPR)
8937         {
8938           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8939           tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8940           return fold_convert_loc (loc, type, negate_expr (tem));
8941         }
8942       if (TREE_CODE (arg0) == CALL_EXPR)
8943         {
8944           tree fn = get_callee_fndecl (arg0);
8945           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8946             switch (DECL_FUNCTION_CODE (fn))
8947               {
8948               CASE_FLT_FN (BUILT_IN_CEXPI):
8949                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8950                 if (fn)
8951                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8952                 break;
8953
8954               default:
8955                 break;
8956               }
8957         }
8958       return NULL_TREE;
8959
8960     case INDIRECT_REF:
8961       /* Fold *&X to X if X is an lvalue.  */
8962       if (TREE_CODE (op0) == ADDR_EXPR)
8963         {
8964           tree op00 = TREE_OPERAND (op0, 0);
8965           if ((TREE_CODE (op00) == VAR_DECL
8966                || TREE_CODE (op00) == PARM_DECL
8967                || TREE_CODE (op00) == RESULT_DECL)
8968               && !TREE_READONLY (op00))
8969             return op00;
8970         }
8971       return NULL_TREE;
8972
8973     default:
8974       return NULL_TREE;
8975     } /* switch (code) */
8976 }
8977
8978
8979 /* If the operation was a conversion do _not_ mark a resulting constant
8980    with TREE_OVERFLOW if the original constant was not.  These conversions
8981    have implementation defined behavior and retaining the TREE_OVERFLOW
8982    flag here would confuse later passes such as VRP.  */
8983 tree
8984 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8985                                 tree type, tree op0)
8986 {
8987   tree res = fold_unary_loc (loc, code, type, op0);
8988   if (res
8989       && TREE_CODE (res) == INTEGER_CST
8990       && TREE_CODE (op0) == INTEGER_CST
8991       && CONVERT_EXPR_CODE_P (code))
8992     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8993
8994   return res;
8995 }
8996
8997 /* Fold a binary expression of code CODE and type TYPE with operands
8998    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8999    Return the folded expression if folding is successful.  Otherwise,
9000    return NULL_TREE.  */
9001
9002 static tree
9003 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
9004 {
9005   enum tree_code compl_code;
9006
9007   if (code == MIN_EXPR)
9008     compl_code = MAX_EXPR;
9009   else if (code == MAX_EXPR)
9010     compl_code = MIN_EXPR;
9011   else
9012     gcc_unreachable ();
9013
9014   /* MIN (MAX (a, b), b) == b.  */
9015   if (TREE_CODE (op0) == compl_code
9016       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
9017     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
9018
9019   /* MIN (MAX (b, a), b) == b.  */
9020   if (TREE_CODE (op0) == compl_code
9021       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
9022       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
9023     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
9024
9025   /* MIN (a, MAX (a, b)) == a.  */
9026   if (TREE_CODE (op1) == compl_code
9027       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
9028       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
9029     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
9030
9031   /* MIN (a, MAX (b, a)) == a.  */
9032   if (TREE_CODE (op1) == compl_code
9033       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
9034       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
9035     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
9036
9037   return NULL_TREE;
9038 }
9039
9040 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9041    by changing CODE to reduce the magnitude of constants involved in
9042    ARG0 of the comparison.
9043    Returns a canonicalized comparison tree if a simplification was
9044    possible, otherwise returns NULL_TREE.
9045    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9046    valid if signed overflow is undefined.  */
9047
9048 static tree
9049 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9050                                  tree arg0, tree arg1,
9051                                  bool *strict_overflow_p)
9052 {
9053   enum tree_code code0 = TREE_CODE (arg0);
9054   tree t, cst0 = NULL_TREE;
9055   int sgn0;
9056   bool swap = false;
9057
9058   /* Match A +- CST code arg1 and CST code arg1.  We can change the
9059      first form only if overflow is undefined.  */
9060   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9061          /* In principle pointers also have undefined overflow behavior,
9062             but that causes problems elsewhere.  */
9063          && !POINTER_TYPE_P (TREE_TYPE (arg0))
9064          && (code0 == MINUS_EXPR
9065              || code0 == PLUS_EXPR)
9066          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9067         || code0 == INTEGER_CST))
9068     return NULL_TREE;
9069
9070   /* Identify the constant in arg0 and its sign.  */
9071   if (code0 == INTEGER_CST)
9072     cst0 = arg0;
9073   else
9074     cst0 = TREE_OPERAND (arg0, 1);
9075   sgn0 = tree_int_cst_sgn (cst0);
9076
9077   /* Overflowed constants and zero will cause problems.  */
9078   if (integer_zerop (cst0)
9079       || TREE_OVERFLOW (cst0))
9080     return NULL_TREE;
9081
9082   /* See if we can reduce the magnitude of the constant in
9083      arg0 by changing the comparison code.  */
9084   if (code0 == INTEGER_CST)
9085     {
9086       /* CST <= arg1  ->  CST-1 < arg1.  */
9087       if (code == LE_EXPR && sgn0 == 1)
9088         code = LT_EXPR;
9089       /* -CST < arg1  ->  -CST-1 <= arg1.  */
9090       else if (code == LT_EXPR && sgn0 == -1)
9091         code = LE_EXPR;
9092       /* CST > arg1  ->  CST-1 >= arg1.  */
9093       else if (code == GT_EXPR && sgn0 == 1)
9094         code = GE_EXPR;
9095       /* -CST >= arg1  ->  -CST-1 > arg1.  */
9096       else if (code == GE_EXPR && sgn0 == -1)
9097         code = GT_EXPR;
9098       else
9099         return NULL_TREE;
9100       /* arg1 code' CST' might be more canonical.  */
9101       swap = true;
9102     }
9103   else
9104     {
9105       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
9106       if (code == LT_EXPR
9107           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9108         code = LE_EXPR;
9109       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
9110       else if (code == GT_EXPR
9111                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9112         code = GE_EXPR;
9113       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
9114       else if (code == LE_EXPR
9115                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9116         code = LT_EXPR;
9117       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
9118       else if (code == GE_EXPR
9119                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9120         code = GT_EXPR;
9121       else
9122         return NULL_TREE;
9123       *strict_overflow_p = true;
9124     }
9125
9126   /* Now build the constant reduced in magnitude.  But not if that
9127      would produce one outside of its types range.  */
9128   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9129       && ((sgn0 == 1
9130            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9131            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9132           || (sgn0 == -1
9133               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9134               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9135     /* We cannot swap the comparison here as that would cause us to
9136        endlessly recurse.  */
9137     return NULL_TREE;
9138
9139   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9140                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
9141   if (code0 != INTEGER_CST)
9142     t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9143
9144   /* If swapping might yield to a more canonical form, do so.  */
9145   if (swap)
9146     return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
9147   else
9148     return fold_build2_loc (loc, code, type, t, arg1);
9149 }
9150
9151 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9152    overflow further.  Try to decrease the magnitude of constants involved
9153    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9154    and put sole constants at the second argument position.
9155    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
9156
9157 static tree
9158 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9159                                tree arg0, tree arg1)
9160 {
9161   tree t;
9162   bool strict_overflow_p;
9163   const char * const warnmsg = G_("assuming signed overflow does not occur "
9164                                   "when reducing constant in comparison");
9165
9166   /* Try canonicalization by simplifying arg0.  */
9167   strict_overflow_p = false;
9168   t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
9169                                        &strict_overflow_p);
9170   if (t)
9171     {
9172       if (strict_overflow_p)
9173         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9174       return t;
9175     }
9176
9177   /* Try canonicalization by simplifying arg1 using the swapped
9178      comparison.  */
9179   code = swap_tree_comparison (code);
9180   strict_overflow_p = false;
9181   t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
9182                                        &strict_overflow_p);
9183   if (t && strict_overflow_p)
9184     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9185   return t;
9186 }
9187
9188 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
9189    space.  This is used to avoid issuing overflow warnings for
9190    expressions like &p->x which can not wrap.  */
9191
9192 static bool
9193 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
9194 {
9195   unsigned HOST_WIDE_INT offset_low, total_low;
9196   HOST_WIDE_INT size, offset_high, total_high;
9197
9198   if (!POINTER_TYPE_P (TREE_TYPE (base)))
9199     return true;
9200
9201   if (bitpos < 0)
9202     return true;
9203
9204   if (offset == NULL_TREE)
9205     {
9206       offset_low = 0;
9207       offset_high = 0;
9208     }
9209   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
9210     return true;
9211   else
9212     {
9213       offset_low = TREE_INT_CST_LOW (offset);
9214       offset_high = TREE_INT_CST_HIGH (offset);
9215     }
9216
9217   if (add_double_with_sign (offset_low, offset_high,
9218                             bitpos / BITS_PER_UNIT, 0,
9219                             &total_low, &total_high,
9220                             true))
9221     return true;
9222
9223   if (total_high != 0)
9224     return true;
9225
9226   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
9227   if (size <= 0)
9228     return true;
9229
9230   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
9231      array.  */
9232   if (TREE_CODE (base) == ADDR_EXPR)
9233     {
9234       HOST_WIDE_INT base_size;
9235
9236       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
9237       if (base_size > 0 && size < base_size)
9238         size = base_size;
9239     }
9240
9241   return total_low > (unsigned HOST_WIDE_INT) size;
9242 }
9243
9244 /* Subroutine of fold_binary.  This routine performs all of the
9245    transformations that are common to the equality/inequality
9246    operators (EQ_EXPR and NE_EXPR) and the ordering operators
9247    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
9248    fold_binary should call fold_binary.  Fold a comparison with
9249    tree code CODE and type TYPE with operands OP0 and OP1.  Return
9250    the folded comparison or NULL_TREE.  */
9251
9252 static tree
9253 fold_comparison (location_t loc, enum tree_code code, tree type,
9254                  tree op0, tree op1)
9255 {
9256   tree arg0, arg1, tem;
9257
9258   arg0 = op0;
9259   arg1 = op1;
9260
9261   STRIP_SIGN_NOPS (arg0);
9262   STRIP_SIGN_NOPS (arg1);
9263
9264   tem = fold_relational_const (code, type, arg0, arg1);
9265   if (tem != NULL_TREE)
9266     return tem;
9267
9268   /* If one arg is a real or integer constant, put it last.  */
9269   if (tree_swap_operands_p (arg0, arg1, true))
9270     return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9271
9272   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
9273   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9274       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9275           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9276           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
9277       && (TREE_CODE (arg1) == INTEGER_CST
9278           && !TREE_OVERFLOW (arg1)))
9279     {
9280       tree const1 = TREE_OPERAND (arg0, 1);
9281       tree const2 = arg1;
9282       tree variable = TREE_OPERAND (arg0, 0);
9283       tree lhs;
9284       int lhs_add;
9285       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
9286
9287       lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
9288                          TREE_TYPE (arg1), const2, const1);
9289
9290       /* If the constant operation overflowed this can be
9291          simplified as a comparison against INT_MAX/INT_MIN.  */
9292       if (TREE_CODE (lhs) == INTEGER_CST
9293           && TREE_OVERFLOW (lhs))
9294         {
9295           int const1_sgn = tree_int_cst_sgn (const1);
9296           enum tree_code code2 = code;
9297
9298           /* Get the sign of the constant on the lhs if the
9299              operation were VARIABLE + CONST1.  */
9300           if (TREE_CODE (arg0) == MINUS_EXPR)
9301             const1_sgn = -const1_sgn;
9302
9303           /* The sign of the constant determines if we overflowed
9304              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
9305              Canonicalize to the INT_MIN overflow by swapping the comparison
9306              if necessary.  */
9307           if (const1_sgn == -1)
9308             code2 = swap_tree_comparison (code);
9309
9310           /* We now can look at the canonicalized case
9311                VARIABLE + 1  CODE2  INT_MIN
9312              and decide on the result.  */
9313           if (code2 == LT_EXPR
9314               || code2 == LE_EXPR
9315               || code2 == EQ_EXPR)
9316             return omit_one_operand_loc (loc, type, boolean_false_node, variable);
9317           else if (code2 == NE_EXPR
9318                    || code2 == GE_EXPR
9319                    || code2 == GT_EXPR)
9320             return omit_one_operand_loc (loc, type, boolean_true_node, variable);
9321         }
9322
9323       if (TREE_CODE (lhs) == TREE_CODE (arg1)
9324           && (TREE_CODE (lhs) != INTEGER_CST
9325               || !TREE_OVERFLOW (lhs)))
9326         {
9327           fold_overflow_warning (("assuming signed overflow does not occur "
9328                                   "when changing X +- C1 cmp C2 to "
9329                                   "X cmp C1 +- C2"),
9330                                  WARN_STRICT_OVERFLOW_COMPARISON);
9331           return fold_build2_loc (loc, code, type, variable, lhs);
9332         }
9333     }
9334
9335   /* For comparisons of pointers we can decompose it to a compile time
9336      comparison of the base objects and the offsets into the object.
9337      This requires at least one operand being an ADDR_EXPR or a
9338      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
9339   if (POINTER_TYPE_P (TREE_TYPE (arg0))
9340       && (TREE_CODE (arg0) == ADDR_EXPR
9341           || TREE_CODE (arg1) == ADDR_EXPR
9342           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9343           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9344     {
9345       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9346       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
9347       enum machine_mode mode;
9348       int volatilep, unsignedp;
9349       bool indirect_base0 = false, indirect_base1 = false;
9350
9351       /* Get base and offset for the access.  Strip ADDR_EXPR for
9352          get_inner_reference, but put it back by stripping INDIRECT_REF
9353          off the base object if possible.  indirect_baseN will be true
9354          if baseN is not an address but refers to the object itself.  */
9355       base0 = arg0;
9356       if (TREE_CODE (arg0) == ADDR_EXPR)
9357         {
9358           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
9359                                        &bitsize, &bitpos0, &offset0, &mode,
9360                                        &unsignedp, &volatilep, false);
9361           if (TREE_CODE (base0) == INDIRECT_REF)
9362             base0 = TREE_OPERAND (base0, 0);
9363           else
9364             indirect_base0 = true;
9365         }
9366       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9367         {
9368           base0 = TREE_OPERAND (arg0, 0);
9369           offset0 = TREE_OPERAND (arg0, 1);
9370         }
9371
9372       base1 = arg1;
9373       if (TREE_CODE (arg1) == ADDR_EXPR)
9374         {
9375           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
9376                                        &bitsize, &bitpos1, &offset1, &mode,
9377                                        &unsignedp, &volatilep, false);
9378           if (TREE_CODE (base1) == INDIRECT_REF)
9379             base1 = TREE_OPERAND (base1, 0);
9380           else
9381             indirect_base1 = true;
9382         }
9383       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9384         {
9385           base1 = TREE_OPERAND (arg1, 0);
9386           offset1 = TREE_OPERAND (arg1, 1);
9387         }
9388
9389       /* If we have equivalent bases we might be able to simplify.  */
9390       if (indirect_base0 == indirect_base1
9391           && operand_equal_p (base0, base1, 0))
9392         {
9393           /* We can fold this expression to a constant if the non-constant
9394              offset parts are equal.  */
9395           if ((offset0 == offset1
9396                || (offset0 && offset1
9397                    && operand_equal_p (offset0, offset1, 0)))
9398               && (code == EQ_EXPR
9399                   || code == NE_EXPR
9400                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
9401
9402             {
9403               if (code != EQ_EXPR
9404                   && code != NE_EXPR
9405                   && bitpos0 != bitpos1
9406                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9407                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9408                 fold_overflow_warning (("assuming pointer wraparound does not "
9409                                         "occur when comparing P +- C1 with "
9410                                         "P +- C2"),
9411                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
9412
9413               switch (code)
9414                 {
9415                 case EQ_EXPR:
9416                   return constant_boolean_node (bitpos0 == bitpos1, type);
9417                 case NE_EXPR:
9418                   return constant_boolean_node (bitpos0 != bitpos1, type);
9419                 case LT_EXPR:
9420                   return constant_boolean_node (bitpos0 < bitpos1, type);
9421                 case LE_EXPR:
9422                   return constant_boolean_node (bitpos0 <= bitpos1, type);
9423                 case GE_EXPR:
9424                   return constant_boolean_node (bitpos0 >= bitpos1, type);
9425                 case GT_EXPR:
9426                   return constant_boolean_node (bitpos0 > bitpos1, type);
9427                 default:;
9428                 }
9429             }
9430           /* We can simplify the comparison to a comparison of the variable
9431              offset parts if the constant offset parts are equal.
9432              Be careful to use signed size type here because otherwise we
9433              mess with array offsets in the wrong way.  This is possible
9434              because pointer arithmetic is restricted to retain within an
9435              object and overflow on pointer differences is undefined as of
9436              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
9437           else if (bitpos0 == bitpos1
9438                    && ((code == EQ_EXPR || code == NE_EXPR)
9439                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
9440             {
9441               tree signed_size_type_node;
9442               signed_size_type_node = signed_type_for (size_type_node);
9443
9444               /* By converting to signed size type we cover middle-end pointer
9445                  arithmetic which operates on unsigned pointer types of size
9446                  type size and ARRAY_REF offsets which are properly sign or
9447                  zero extended from their type in case it is narrower than
9448                  size type.  */
9449               if (offset0 == NULL_TREE)
9450                 offset0 = build_int_cst (signed_size_type_node, 0);
9451               else
9452                 offset0 = fold_convert_loc (loc, signed_size_type_node,
9453                                             offset0);
9454               if (offset1 == NULL_TREE)
9455                 offset1 = build_int_cst (signed_size_type_node, 0);
9456               else
9457                 offset1 = fold_convert_loc (loc, signed_size_type_node,
9458                                             offset1);
9459
9460               if (code != EQ_EXPR
9461                   && code != NE_EXPR
9462                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9463                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9464                 fold_overflow_warning (("assuming pointer wraparound does not "
9465                                         "occur when comparing P +- C1 with "
9466                                         "P +- C2"),
9467                                        WARN_STRICT_OVERFLOW_COMPARISON);
9468
9469               return fold_build2_loc (loc, code, type, offset0, offset1);
9470             }
9471         }
9472       /* For non-equal bases we can simplify if they are addresses
9473          of local binding decls or constants.  */
9474       else if (indirect_base0 && indirect_base1
9475                /* We know that !operand_equal_p (base0, base1, 0)
9476                   because the if condition was false.  But make
9477                   sure two decls are not the same.  */
9478                && base0 != base1
9479                && TREE_CODE (arg0) == ADDR_EXPR
9480                && TREE_CODE (arg1) == ADDR_EXPR
9481                && (((TREE_CODE (base0) == VAR_DECL
9482                      || TREE_CODE (base0) == PARM_DECL)
9483                     && (targetm.binds_local_p (base0)
9484                         || CONSTANT_CLASS_P (base1)))
9485                    || CONSTANT_CLASS_P (base0))
9486                && (((TREE_CODE (base1) == VAR_DECL
9487                      || TREE_CODE (base1) == PARM_DECL)
9488                     && (targetm.binds_local_p (base1)
9489                         || CONSTANT_CLASS_P (base0)))
9490                    || CONSTANT_CLASS_P (base1)))
9491         {
9492           if (code == EQ_EXPR)
9493             return omit_two_operands_loc (loc, type, boolean_false_node,
9494                                       arg0, arg1);
9495           else if (code == NE_EXPR)
9496             return omit_two_operands_loc (loc, type, boolean_true_node,
9497                                       arg0, arg1);
9498         }
9499       /* For equal offsets we can simplify to a comparison of the
9500          base addresses.  */
9501       else if (bitpos0 == bitpos1
9502                && (indirect_base0
9503                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9504                && (indirect_base1
9505                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9506                && ((offset0 == offset1)
9507                    || (offset0 && offset1
9508                        && operand_equal_p (offset0, offset1, 0))))
9509         {
9510           if (indirect_base0)
9511             base0 = build_fold_addr_expr_loc (loc, base0);
9512           if (indirect_base1)
9513             base1 = build_fold_addr_expr_loc (loc, base1);
9514           return fold_build2_loc (loc, code, type, base0, base1);
9515         }
9516     }
9517
9518   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9519      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
9520      the resulting offset is smaller in absolute value than the
9521      original one.  */
9522   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9523       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9524       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9525           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9526       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9527       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9528           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9529     {
9530       tree const1 = TREE_OPERAND (arg0, 1);
9531       tree const2 = TREE_OPERAND (arg1, 1);
9532       tree variable1 = TREE_OPERAND (arg0, 0);
9533       tree variable2 = TREE_OPERAND (arg1, 0);
9534       tree cst;
9535       const char * const warnmsg = G_("assuming signed overflow does not "
9536                                       "occur when combining constants around "
9537                                       "a comparison");
9538
9539       /* Put the constant on the side where it doesn't overflow and is
9540          of lower absolute value than before.  */
9541       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9542                              ? MINUS_EXPR : PLUS_EXPR,
9543                              const2, const1, 0);
9544       if (!TREE_OVERFLOW (cst)
9545           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9546         {
9547           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9548           return fold_build2_loc (loc, code, type,
9549                               variable1,
9550                               fold_build2_loc (loc,
9551                                            TREE_CODE (arg1), TREE_TYPE (arg1),
9552                                            variable2, cst));
9553         }
9554
9555       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9556                              ? MINUS_EXPR : PLUS_EXPR,
9557                              const1, const2, 0);
9558       if (!TREE_OVERFLOW (cst)
9559           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9560         {
9561           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9562           return fold_build2_loc (loc, code, type,
9563                               fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
9564                                            variable1, cst),
9565                               variable2);
9566         }
9567     }
9568
9569   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9570      signed arithmetic case.  That form is created by the compiler
9571      often enough for folding it to be of value.  One example is in
9572      computing loop trip counts after Operator Strength Reduction.  */
9573   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9574       && TREE_CODE (arg0) == MULT_EXPR
9575       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9576           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9577       && integer_zerop (arg1))
9578     {
9579       tree const1 = TREE_OPERAND (arg0, 1);
9580       tree const2 = arg1;                       /* zero */
9581       tree variable1 = TREE_OPERAND (arg0, 0);
9582       enum tree_code cmp_code = code;
9583
9584       /* Handle unfolded multiplication by zero.  */
9585       if (integer_zerop (const1))
9586         return fold_build2_loc (loc, cmp_code, type, const1, const2);
9587
9588       fold_overflow_warning (("assuming signed overflow does not occur when "
9589                               "eliminating multiplication in comparison "
9590                               "with zero"),
9591                              WARN_STRICT_OVERFLOW_COMPARISON);
9592
9593       /* If const1 is negative we swap the sense of the comparison.  */
9594       if (tree_int_cst_sgn (const1) < 0)
9595         cmp_code = swap_tree_comparison (cmp_code);
9596
9597       return fold_build2_loc (loc, cmp_code, type, variable1, const2);
9598     }
9599
9600   tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
9601   if (tem)
9602     return tem;
9603
9604   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9605     {
9606       tree targ0 = strip_float_extensions (arg0);
9607       tree targ1 = strip_float_extensions (arg1);
9608       tree newtype = TREE_TYPE (targ0);
9609
9610       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9611         newtype = TREE_TYPE (targ1);
9612
9613       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9614       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9615         return fold_build2_loc (loc, code, type,
9616                             fold_convert_loc (loc, newtype, targ0),
9617                             fold_convert_loc (loc, newtype, targ1));
9618
9619       /* (-a) CMP (-b) -> b CMP a  */
9620       if (TREE_CODE (arg0) == NEGATE_EXPR
9621           && TREE_CODE (arg1) == NEGATE_EXPR)
9622         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
9623                             TREE_OPERAND (arg0, 0));
9624
9625       if (TREE_CODE (arg1) == REAL_CST)
9626         {
9627           REAL_VALUE_TYPE cst;
9628           cst = TREE_REAL_CST (arg1);
9629
9630           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
9631           if (TREE_CODE (arg0) == NEGATE_EXPR)
9632             return fold_build2_loc (loc, swap_tree_comparison (code), type,
9633                                 TREE_OPERAND (arg0, 0),
9634                                 build_real (TREE_TYPE (arg1),
9635                                             REAL_VALUE_NEGATE (cst)));
9636
9637           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
9638           /* a CMP (-0) -> a CMP 0  */
9639           if (REAL_VALUE_MINUS_ZERO (cst))
9640             return fold_build2_loc (loc, code, type, arg0,
9641                                 build_real (TREE_TYPE (arg1), dconst0));
9642
9643           /* x != NaN is always true, other ops are always false.  */
9644           if (REAL_VALUE_ISNAN (cst)
9645               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9646             {
9647               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9648               return omit_one_operand_loc (loc, type, tem, arg0);
9649             }
9650
9651           /* Fold comparisons against infinity.  */
9652           if (REAL_VALUE_ISINF (cst)
9653               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9654             {
9655               tem = fold_inf_compare (loc, code, type, arg0, arg1);
9656               if (tem != NULL_TREE)
9657                 return tem;
9658             }
9659         }
9660
9661       /* If this is a comparison of a real constant with a PLUS_EXPR
9662          or a MINUS_EXPR of a real constant, we can convert it into a
9663          comparison with a revised real constant as long as no overflow
9664          occurs when unsafe_math_optimizations are enabled.  */
9665       if (flag_unsafe_math_optimizations
9666           && TREE_CODE (arg1) == REAL_CST
9667           && (TREE_CODE (arg0) == PLUS_EXPR
9668               || TREE_CODE (arg0) == MINUS_EXPR)
9669           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9670           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9671                                       ? MINUS_EXPR : PLUS_EXPR,
9672                                       arg1, TREE_OPERAND (arg0, 1), 0))
9673           && !TREE_OVERFLOW (tem))
9674         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9675
9676       /* Likewise, we can simplify a comparison of a real constant with
9677          a MINUS_EXPR whose first operand is also a real constant, i.e.
9678          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
9679          floating-point types only if -fassociative-math is set.  */
9680       if (flag_associative_math
9681           && TREE_CODE (arg1) == REAL_CST
9682           && TREE_CODE (arg0) == MINUS_EXPR
9683           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9684           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9685                                       arg1, 0))
9686           && !TREE_OVERFLOW (tem))
9687         return fold_build2_loc (loc, swap_tree_comparison (code), type,
9688                             TREE_OPERAND (arg0, 1), tem);
9689
9690       /* Fold comparisons against built-in math functions.  */
9691       if (TREE_CODE (arg1) == REAL_CST
9692           && flag_unsafe_math_optimizations
9693           && ! flag_errno_math)
9694         {
9695           enum built_in_function fcode = builtin_mathfn_code (arg0);
9696
9697           if (fcode != END_BUILTINS)
9698             {
9699               tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9700               if (tem != NULL_TREE)
9701                 return tem;
9702             }
9703         }
9704     }
9705
9706   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9707       && CONVERT_EXPR_P (arg0))
9708     {
9709       /* If we are widening one operand of an integer comparison,
9710          see if the other operand is similarly being widened.  Perhaps we
9711          can do the comparison in the narrower type.  */
9712       tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9713       if (tem)
9714         return tem;
9715
9716       /* Or if we are changing signedness.  */
9717       tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9718       if (tem)
9719         return tem;
9720     }
9721
9722   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9723      constant, we can simplify it.  */
9724   if (TREE_CODE (arg1) == INTEGER_CST
9725       && (TREE_CODE (arg0) == MIN_EXPR
9726           || TREE_CODE (arg0) == MAX_EXPR)
9727       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9728     {
9729       tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9730       if (tem)
9731         return tem;
9732     }
9733
9734   /* Simplify comparison of something with itself.  (For IEEE
9735      floating-point, we can only do some of these simplifications.)  */
9736   if (operand_equal_p (arg0, arg1, 0))
9737     {
9738       switch (code)
9739         {
9740         case EQ_EXPR:
9741           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9742               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9743             return constant_boolean_node (1, type);
9744           break;
9745
9746         case GE_EXPR:
9747         case LE_EXPR:
9748           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9749               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9750             return constant_boolean_node (1, type);
9751           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9752
9753         case NE_EXPR:
9754           /* For NE, we can only do this simplification if integer
9755              or we don't honor IEEE floating point NaNs.  */
9756           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9757               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9758             break;
9759           /* ... fall through ...  */
9760         case GT_EXPR:
9761         case LT_EXPR:
9762           return constant_boolean_node (0, type);
9763         default:
9764           gcc_unreachable ();
9765         }
9766     }
9767
9768   /* If we are comparing an expression that just has comparisons
9769      of two integer values, arithmetic expressions of those comparisons,
9770      and constants, we can simplify it.  There are only three cases
9771      to check: the two values can either be equal, the first can be
9772      greater, or the second can be greater.  Fold the expression for
9773      those three values.  Since each value must be 0 or 1, we have
9774      eight possibilities, each of which corresponds to the constant 0
9775      or 1 or one of the six possible comparisons.
9776
9777      This handles common cases like (a > b) == 0 but also handles
9778      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9779      occur in macroized code.  */
9780
9781   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9782     {
9783       tree cval1 = 0, cval2 = 0;
9784       int save_p = 0;
9785
9786       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9787           /* Don't handle degenerate cases here; they should already
9788              have been handled anyway.  */
9789           && cval1 != 0 && cval2 != 0
9790           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9791           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9792           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9793           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9794           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9795           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9796                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9797         {
9798           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9799           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9800
9801           /* We can't just pass T to eval_subst in case cval1 or cval2
9802              was the same as ARG1.  */
9803
9804           tree high_result
9805                 = fold_build2_loc (loc, code, type,
9806                                eval_subst (loc, arg0, cval1, maxval,
9807                                            cval2, minval),
9808                                arg1);
9809           tree equal_result
9810                 = fold_build2_loc (loc, code, type,
9811                                eval_subst (loc, arg0, cval1, maxval,
9812                                            cval2, maxval),
9813                                arg1);
9814           tree low_result
9815                 = fold_build2_loc (loc, code, type,
9816                                eval_subst (loc, arg0, cval1, minval,
9817                                            cval2, maxval),
9818                                arg1);
9819
9820           /* All three of these results should be 0 or 1.  Confirm they are.
9821              Then use those values to select the proper code to use.  */
9822
9823           if (TREE_CODE (high_result) == INTEGER_CST
9824               && TREE_CODE (equal_result) == INTEGER_CST
9825               && TREE_CODE (low_result) == INTEGER_CST)
9826             {
9827               /* Make a 3-bit mask with the high-order bit being the
9828                  value for `>', the next for '=', and the low for '<'.  */
9829               switch ((integer_onep (high_result) * 4)
9830                       + (integer_onep (equal_result) * 2)
9831                       + integer_onep (low_result))
9832                 {
9833                 case 0:
9834                   /* Always false.  */
9835                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9836                 case 1:
9837                   code = LT_EXPR;
9838                   break;
9839                 case 2:
9840                   code = EQ_EXPR;
9841                   break;
9842                 case 3:
9843                   code = LE_EXPR;
9844                   break;
9845                 case 4:
9846                   code = GT_EXPR;
9847                   break;
9848                 case 5:
9849                   code = NE_EXPR;
9850                   break;
9851                 case 6:
9852                   code = GE_EXPR;
9853                   break;
9854                 case 7:
9855                   /* Always true.  */
9856                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9857                 }
9858
9859               if (save_p)
9860                 {
9861                   tem = save_expr (build2 (code, type, cval1, cval2));
9862                   SET_EXPR_LOCATION (tem, loc);
9863                   return tem;
9864                 }
9865               return fold_build2_loc (loc, code, type, cval1, cval2);
9866             }
9867         }
9868     }
9869
9870   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9871      into a single range test.  */
9872   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9873        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9874       && TREE_CODE (arg1) == INTEGER_CST
9875       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9876       && !integer_zerop (TREE_OPERAND (arg0, 1))
9877       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9878       && !TREE_OVERFLOW (arg1))
9879     {
9880       tem = fold_div_compare (loc, code, type, arg0, arg1);
9881       if (tem != NULL_TREE)
9882         return tem;
9883     }
9884
9885   /* Fold ~X op ~Y as Y op X.  */
9886   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9887       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9888     {
9889       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9890       return fold_build2_loc (loc, code, type,
9891                           fold_convert_loc (loc, cmp_type,
9892                                             TREE_OPERAND (arg1, 0)),
9893                           TREE_OPERAND (arg0, 0));
9894     }
9895
9896   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9897   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9898       && TREE_CODE (arg1) == INTEGER_CST)
9899     {
9900       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9901       return fold_build2_loc (loc, swap_tree_comparison (code), type,
9902                           TREE_OPERAND (arg0, 0),
9903                           fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9904                                        fold_convert_loc (loc, cmp_type, arg1)));
9905     }
9906
9907   return NULL_TREE;
9908 }
9909
9910
9911 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9912    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9913    argument EXPR represents the expression "z" of type TYPE.  */
9914
9915 static tree
9916 fold_mult_zconjz (location_t loc, tree type, tree expr)
9917 {
9918   tree itype = TREE_TYPE (type);
9919   tree rpart, ipart, tem;
9920
9921   if (TREE_CODE (expr) == COMPLEX_EXPR)
9922     {
9923       rpart = TREE_OPERAND (expr, 0);
9924       ipart = TREE_OPERAND (expr, 1);
9925     }
9926   else if (TREE_CODE (expr) == COMPLEX_CST)
9927     {
9928       rpart = TREE_REALPART (expr);
9929       ipart = TREE_IMAGPART (expr);
9930     }
9931   else
9932     {
9933       expr = save_expr (expr);
9934       rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9935       ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9936     }
9937
9938   rpart = save_expr (rpart);
9939   ipart = save_expr (ipart);
9940   tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9941                      fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9942                      fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9943   return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9944                       fold_convert_loc (loc, itype, integer_zero_node));
9945 }
9946
9947
9948 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9949    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9950    guarantees that P and N have the same least significant log2(M) bits.
9951    N is not otherwise constrained.  In particular, N is not normalized to
9952    0 <= N < M as is common.  In general, the precise value of P is unknown.
9953    M is chosen as large as possible such that constant N can be determined.
9954
9955    Returns M and sets *RESIDUE to N.
9956
9957    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9958    account.  This is not always possible due to PR 35705.
9959  */
9960
9961 static unsigned HOST_WIDE_INT
9962 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9963                                  bool allow_func_align)
9964 {
9965   enum tree_code code;
9966
9967   *residue = 0;
9968
9969   code = TREE_CODE (expr);
9970   if (code == ADDR_EXPR)
9971     {
9972       expr = TREE_OPERAND (expr, 0);
9973       if (handled_component_p (expr))
9974         {
9975           HOST_WIDE_INT bitsize, bitpos;
9976           tree offset;
9977           enum machine_mode mode;
9978           int unsignedp, volatilep;
9979
9980           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9981                                       &mode, &unsignedp, &volatilep, false);
9982           *residue = bitpos / BITS_PER_UNIT;
9983           if (offset)
9984             {
9985               if (TREE_CODE (offset) == INTEGER_CST)
9986                 *residue += TREE_INT_CST_LOW (offset);
9987               else
9988                 /* We don't handle more complicated offset expressions.  */
9989                 return 1;
9990             }
9991         }
9992
9993       if (DECL_P (expr)
9994           && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9995         return DECL_ALIGN_UNIT (expr);
9996     }
9997   else if (code == POINTER_PLUS_EXPR)
9998     {
9999       tree op0, op1;
10000       unsigned HOST_WIDE_INT modulus;
10001       enum tree_code inner_code;
10002
10003       op0 = TREE_OPERAND (expr, 0);
10004       STRIP_NOPS (op0);
10005       modulus = get_pointer_modulus_and_residue (op0, residue,
10006                                                  allow_func_align);
10007
10008       op1 = TREE_OPERAND (expr, 1);
10009       STRIP_NOPS (op1);
10010       inner_code = TREE_CODE (op1);
10011       if (inner_code == INTEGER_CST)
10012         {
10013           *residue += TREE_INT_CST_LOW (op1);
10014           return modulus;
10015         }
10016       else if (inner_code == MULT_EXPR)
10017         {
10018           op1 = TREE_OPERAND (op1, 1);
10019           if (TREE_CODE (op1) == INTEGER_CST)
10020             {
10021               unsigned HOST_WIDE_INT align;
10022
10023               /* Compute the greatest power-of-2 divisor of op1.  */
10024               align = TREE_INT_CST_LOW (op1);
10025               align &= -align;
10026
10027               /* If align is non-zero and less than *modulus, replace
10028                  *modulus with align., If align is 0, then either op1 is 0
10029                  or the greatest power-of-2 divisor of op1 doesn't fit in an
10030                  unsigned HOST_WIDE_INT.  In either case, no additional
10031                  constraint is imposed.  */
10032               if (align)
10033                 modulus = MIN (modulus, align);
10034
10035               return modulus;
10036             }
10037         }
10038     }
10039
10040     /* If we get here, we were unable to determine anything useful about the
10041        expression.  */
10042     return 1;
10043 }
10044
10045
10046 /* Fold a binary expression of code CODE and type TYPE with operands
10047    OP0 and OP1.  LOC is the location of the resulting expression.
10048    Return the folded expression if folding is successful.  Otherwise,
10049    return NULL_TREE.  */
10050
10051 tree
10052 fold_binary_loc (location_t loc,
10053              enum tree_code code, tree type, tree op0, tree op1)
10054 {
10055   enum tree_code_class kind = TREE_CODE_CLASS (code);
10056   tree arg0, arg1, tem;
10057   tree t1 = NULL_TREE;
10058   bool strict_overflow_p;
10059
10060   gcc_assert (IS_EXPR_CODE_CLASS (kind)
10061               && TREE_CODE_LENGTH (code) == 2
10062               && op0 != NULL_TREE
10063               && op1 != NULL_TREE);
10064
10065   arg0 = op0;
10066   arg1 = op1;
10067
10068   /* Strip any conversions that don't change the mode.  This is
10069      safe for every expression, except for a comparison expression
10070      because its signedness is derived from its operands.  So, in
10071      the latter case, only strip conversions that don't change the
10072      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
10073      preserved.
10074
10075      Note that this is done as an internal manipulation within the
10076      constant folder, in order to find the simplest representation
10077      of the arguments so that their form can be studied.  In any
10078      cases, the appropriate type conversions should be put back in
10079      the tree that will get out of the constant folder.  */
10080
10081   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10082     {
10083       STRIP_SIGN_NOPS (arg0);
10084       STRIP_SIGN_NOPS (arg1);
10085     }
10086   else
10087     {
10088       STRIP_NOPS (arg0);
10089       STRIP_NOPS (arg1);
10090     }
10091
10092   /* Note that TREE_CONSTANT isn't enough: static var addresses are
10093      constant but we can't do arithmetic on them.  */
10094   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10095       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
10096       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
10097       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
10098       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
10099       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
10100     {
10101       if (kind == tcc_binary)
10102         {
10103           /* Make sure type and arg0 have the same saturating flag.  */
10104           gcc_assert (TYPE_SATURATING (type)
10105                       == TYPE_SATURATING (TREE_TYPE (arg0)));
10106           tem = const_binop (code, arg0, arg1, 0);
10107         }
10108       else if (kind == tcc_comparison)
10109         tem = fold_relational_const (code, type, arg0, arg1);
10110       else
10111         tem = NULL_TREE;
10112
10113       if (tem != NULL_TREE)
10114         {
10115           if (TREE_TYPE (tem) != type)
10116             tem = fold_convert_loc (loc, type, tem);
10117           return tem;
10118         }
10119     }
10120
10121   /* If this is a commutative operation, and ARG0 is a constant, move it
10122      to ARG1 to reduce the number of tests below.  */
10123   if (commutative_tree_code (code)
10124       && tree_swap_operands_p (arg0, arg1, true))
10125     return fold_build2_loc (loc, code, type, op1, op0);
10126
10127   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10128
10129      First check for cases where an arithmetic operation is applied to a
10130      compound, conditional, or comparison operation.  Push the arithmetic
10131      operation inside the compound or conditional to see if any folding
10132      can then be done.  Convert comparison to conditional for this purpose.
10133      The also optimizes non-constant cases that used to be done in
10134      expand_expr.
10135
10136      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10137      one of the operands is a comparison and the other is a comparison, a
10138      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
10139      code below would make the expression more complex.  Change it to a
10140      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
10141      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
10142
10143   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
10144        || code == EQ_EXPR || code == NE_EXPR)
10145       && ((truth_value_p (TREE_CODE (arg0))
10146            && (truth_value_p (TREE_CODE (arg1))
10147                || (TREE_CODE (arg1) == BIT_AND_EXPR
10148                    && integer_onep (TREE_OPERAND (arg1, 1)))))
10149           || (truth_value_p (TREE_CODE (arg1))
10150               && (truth_value_p (TREE_CODE (arg0))
10151                   || (TREE_CODE (arg0) == BIT_AND_EXPR
10152                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
10153     {
10154       tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
10155                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
10156                          : TRUTH_XOR_EXPR,
10157                          boolean_type_node,
10158                          fold_convert_loc (loc, boolean_type_node, arg0),
10159                          fold_convert_loc (loc, boolean_type_node, arg1));
10160
10161       if (code == EQ_EXPR)
10162         tem = invert_truthvalue_loc (loc, tem);
10163
10164       return fold_convert_loc (loc, type, tem);
10165     }
10166
10167   if (TREE_CODE_CLASS (code) == tcc_binary
10168       || TREE_CODE_CLASS (code) == tcc_comparison)
10169     {
10170       if (TREE_CODE (arg0) == COMPOUND_EXPR)
10171         {
10172           tem = fold_build2_loc (loc, code, type,
10173                              fold_convert_loc (loc, TREE_TYPE (op0),
10174                                                TREE_OPERAND (arg0, 1)), op1);
10175           tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), tem);
10176           goto fold_binary_exit;
10177         }
10178       if (TREE_CODE (arg1) == COMPOUND_EXPR
10179           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10180         {
10181           tem = fold_build2_loc (loc, code, type, op0,
10182                              fold_convert_loc (loc, TREE_TYPE (op1),
10183                                                TREE_OPERAND (arg1, 1)));
10184           tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), tem);
10185           goto fold_binary_exit;
10186         }
10187
10188       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
10189         {
10190           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10191                                                      arg0, arg1,
10192                                                      /*cond_first_p=*/1);
10193           if (tem != NULL_TREE)
10194             return tem;
10195         }
10196
10197       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
10198         {
10199           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10200                                                      arg1, arg0,
10201                                                      /*cond_first_p=*/0);
10202           if (tem != NULL_TREE)
10203             return tem;
10204         }
10205     }
10206
10207   switch (code)
10208     {
10209     case POINTER_PLUS_EXPR:
10210       /* 0 +p index -> (type)index */
10211       if (integer_zerop (arg0))
10212         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10213
10214       /* PTR +p 0 -> PTR */
10215       if (integer_zerop (arg1))
10216         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10217
10218       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
10219       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10220            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
10221         return fold_convert_loc (loc, type,
10222                                  fold_build2_loc (loc, PLUS_EXPR, sizetype,
10223                                               fold_convert_loc (loc, sizetype,
10224                                                                 arg1),
10225                                               fold_convert_loc (loc, sizetype,
10226                                                                 arg0)));
10227
10228       /* index +p PTR -> PTR +p index */
10229       if (POINTER_TYPE_P (TREE_TYPE (arg1))
10230           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
10231         return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
10232                             fold_convert_loc (loc, type, arg1),
10233                             fold_convert_loc (loc, sizetype, arg0));
10234
10235       /* (PTR +p B) +p A -> PTR +p (B + A) */
10236       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10237         {
10238           tree inner;
10239           tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
10240           tree arg00 = TREE_OPERAND (arg0, 0);
10241           inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
10242                                arg01, fold_convert_loc (loc, sizetype, arg1));
10243           return fold_convert_loc (loc, type,
10244                                    fold_build2_loc (loc, POINTER_PLUS_EXPR,
10245                                                 TREE_TYPE (arg00),
10246                                                 arg00, inner));
10247         }
10248
10249       /* PTR_CST +p CST -> CST1 */
10250       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10251         return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
10252                             fold_convert_loc (loc, type, arg1));
10253
10254      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
10255         of the array.  Loop optimizer sometimes produce this type of
10256         expressions.  */
10257       if (TREE_CODE (arg0) == ADDR_EXPR)
10258         {
10259           tem = try_move_mult_to_index (loc, arg0,
10260                                         fold_convert_loc (loc, sizetype, arg1));
10261           if (tem)
10262             return fold_convert_loc (loc, type, tem);
10263         }
10264
10265       return NULL_TREE;
10266
10267     case PLUS_EXPR:
10268       /* A + (-B) -> A - B */
10269       if (TREE_CODE (arg1) == NEGATE_EXPR)
10270         return fold_build2_loc (loc, MINUS_EXPR, type,
10271                             fold_convert_loc (loc, type, arg0),
10272                             fold_convert_loc (loc, type,
10273                                               TREE_OPERAND (arg1, 0)));
10274       /* (-A) + B -> B - A */
10275       if (TREE_CODE (arg0) == NEGATE_EXPR
10276           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
10277         return fold_build2_loc (loc, MINUS_EXPR, type,
10278                             fold_convert_loc (loc, type, arg1),
10279                             fold_convert_loc (loc, type,
10280                                               TREE_OPERAND (arg0, 0)));
10281
10282       if (INTEGRAL_TYPE_P (type))
10283         {
10284           /* Convert ~A + 1 to -A.  */
10285           if (TREE_CODE (arg0) == BIT_NOT_EXPR
10286               && integer_onep (arg1))
10287             return fold_build1_loc (loc, NEGATE_EXPR, type,
10288                                 fold_convert_loc (loc, type,
10289                                                   TREE_OPERAND (arg0, 0)));
10290
10291           /* ~X + X is -1.  */
10292           if (TREE_CODE (arg0) == BIT_NOT_EXPR
10293               && !TYPE_OVERFLOW_TRAPS (type))
10294             {
10295               tree tem = TREE_OPERAND (arg0, 0);
10296
10297               STRIP_NOPS (tem);
10298               if (operand_equal_p (tem, arg1, 0))
10299                 {
10300                   t1 = build_int_cst_type (type, -1);
10301                   return omit_one_operand_loc (loc, type, t1, arg1);
10302                 }
10303             }
10304
10305           /* X + ~X is -1.  */
10306           if (TREE_CODE (arg1) == BIT_NOT_EXPR
10307               && !TYPE_OVERFLOW_TRAPS (type))
10308             {
10309               tree tem = TREE_OPERAND (arg1, 0);
10310
10311               STRIP_NOPS (tem);
10312               if (operand_equal_p (arg0, tem, 0))
10313                 {
10314                   t1 = build_int_cst_type (type, -1);
10315                   return omit_one_operand_loc (loc, type, t1, arg0);
10316                 }
10317             }
10318
10319           /* X + (X / CST) * -CST is X % CST.  */
10320           if (TREE_CODE (arg1) == MULT_EXPR
10321               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10322               && operand_equal_p (arg0,
10323                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
10324             {
10325               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
10326               tree cst1 = TREE_OPERAND (arg1, 1);
10327               tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
10328                                       cst1, cst0);
10329               if (sum && integer_zerop (sum))
10330                 return fold_convert_loc (loc, type,
10331                                          fold_build2_loc (loc, TRUNC_MOD_EXPR,
10332                                                       TREE_TYPE (arg0), arg0,
10333                                                       cst0));
10334             }
10335         }
10336
10337       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
10338          same or one.  Make sure type is not saturating.
10339          fold_plusminus_mult_expr will re-associate.  */
10340       if ((TREE_CODE (arg0) == MULT_EXPR
10341            || TREE_CODE (arg1) == MULT_EXPR)
10342           && !TYPE_SATURATING (type)
10343           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10344         {
10345           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10346           if (tem)
10347             return tem;
10348         }
10349
10350       if (! FLOAT_TYPE_P (type))
10351         {
10352           if (integer_zerop (arg1))
10353             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10354
10355           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
10356              with a constant, and the two constants have no bits in common,
10357              we should treat this as a BIT_IOR_EXPR since this may produce more
10358              simplifications.  */
10359           if (TREE_CODE (arg0) == BIT_AND_EXPR
10360               && TREE_CODE (arg1) == BIT_AND_EXPR
10361               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10362               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10363               && integer_zerop (const_binop (BIT_AND_EXPR,
10364                                              TREE_OPERAND (arg0, 1),
10365                                              TREE_OPERAND (arg1, 1), 0)))
10366             {
10367               code = BIT_IOR_EXPR;
10368               goto bit_ior;
10369             }
10370
10371           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10372              (plus (plus (mult) (mult)) (foo)) so that we can
10373              take advantage of the factoring cases below.  */
10374           if (((TREE_CODE (arg0) == PLUS_EXPR
10375                 || TREE_CODE (arg0) == MINUS_EXPR)
10376                && TREE_CODE (arg1) == MULT_EXPR)
10377               || ((TREE_CODE (arg1) == PLUS_EXPR
10378                    || TREE_CODE (arg1) == MINUS_EXPR)
10379                   && TREE_CODE (arg0) == MULT_EXPR))
10380             {
10381               tree parg0, parg1, parg, marg;
10382               enum tree_code pcode;
10383
10384               if (TREE_CODE (arg1) == MULT_EXPR)
10385                 parg = arg0, marg = arg1;
10386               else
10387                 parg = arg1, marg = arg0;
10388               pcode = TREE_CODE (parg);
10389               parg0 = TREE_OPERAND (parg, 0);
10390               parg1 = TREE_OPERAND (parg, 1);
10391               STRIP_NOPS (parg0);
10392               STRIP_NOPS (parg1);
10393
10394               if (TREE_CODE (parg0) == MULT_EXPR
10395                   && TREE_CODE (parg1) != MULT_EXPR)
10396                 return fold_build2_loc (loc, pcode, type,
10397                                     fold_build2_loc (loc, PLUS_EXPR, type,
10398                                                  fold_convert_loc (loc, type,
10399                                                                    parg0),
10400                                                  fold_convert_loc (loc, type,
10401                                                                    marg)),
10402                                     fold_convert_loc (loc, type, parg1));
10403               if (TREE_CODE (parg0) != MULT_EXPR
10404                   && TREE_CODE (parg1) == MULT_EXPR)
10405                 return
10406                   fold_build2_loc (loc, PLUS_EXPR, type,
10407                                fold_convert_loc (loc, type, parg0),
10408                                fold_build2_loc (loc, pcode, type,
10409                                             fold_convert_loc (loc, type, marg),
10410                                             fold_convert_loc (loc, type,
10411                                                               parg1)));
10412             }
10413         }
10414       else
10415         {
10416           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
10417           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10418             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10419
10420           /* Likewise if the operands are reversed.  */
10421           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10422             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10423
10424           /* Convert X + -C into X - C.  */
10425           if (TREE_CODE (arg1) == REAL_CST
10426               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10427             {
10428               tem = fold_negate_const (arg1, type);
10429               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10430                 return fold_build2_loc (loc, MINUS_EXPR, type,
10431                                     fold_convert_loc (loc, type, arg0),
10432                                     fold_convert_loc (loc, type, tem));
10433             }
10434
10435           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10436              to __complex__ ( x, y ).  This is not the same for SNaNs or
10437              if signed zeros are involved.  */
10438           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10439               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10440               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10441             {
10442               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10443               tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10444               tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10445               bool arg0rz = false, arg0iz = false;
10446               if ((arg0r && (arg0rz = real_zerop (arg0r)))
10447                   || (arg0i && (arg0iz = real_zerop (arg0i))))
10448                 {
10449                   tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10450                   tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10451                   if (arg0rz && arg1i && real_zerop (arg1i))
10452                     {
10453                       tree rp = arg1r ? arg1r
10454                                   : build1 (REALPART_EXPR, rtype, arg1);
10455                       tree ip = arg0i ? arg0i
10456                                   : build1 (IMAGPART_EXPR, rtype, arg0);
10457                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10458                     }
10459                   else if (arg0iz && arg1r && real_zerop (arg1r))
10460                     {
10461                       tree rp = arg0r ? arg0r
10462                                   : build1 (REALPART_EXPR, rtype, arg0);
10463                       tree ip = arg1i ? arg1i
10464                                   : build1 (IMAGPART_EXPR, rtype, arg1);
10465                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10466                     }
10467                 }
10468             }
10469
10470           if (flag_unsafe_math_optimizations
10471               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10472               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10473               && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10474             return tem;
10475
10476           /* Convert x+x into x*2.0.  */
10477           if (operand_equal_p (arg0, arg1, 0)
10478               && SCALAR_FLOAT_TYPE_P (type))
10479             return fold_build2_loc (loc, MULT_EXPR, type, arg0,
10480                                 build_real (type, dconst2));
10481
10482           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10483              We associate floats only if the user has specified
10484              -fassociative-math.  */
10485           if (flag_associative_math
10486               && TREE_CODE (arg1) == PLUS_EXPR
10487               && TREE_CODE (arg0) != MULT_EXPR)
10488             {
10489               tree tree10 = TREE_OPERAND (arg1, 0);
10490               tree tree11 = TREE_OPERAND (arg1, 1);
10491               if (TREE_CODE (tree11) == MULT_EXPR
10492                   && TREE_CODE (tree10) == MULT_EXPR)
10493                 {
10494                   tree tree0;
10495                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
10496                   return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
10497                 }
10498             }
10499           /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10500              We associate floats only if the user has specified
10501              -fassociative-math.  */
10502           if (flag_associative_math
10503               && TREE_CODE (arg0) == PLUS_EXPR
10504               && TREE_CODE (arg1) != MULT_EXPR)
10505             {
10506               tree tree00 = TREE_OPERAND (arg0, 0);
10507               tree tree01 = TREE_OPERAND (arg0, 1);
10508               if (TREE_CODE (tree01) == MULT_EXPR
10509                   && TREE_CODE (tree00) == MULT_EXPR)
10510                 {
10511                   tree tree0;
10512                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
10513                   return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
10514                 }
10515             }
10516         }
10517
10518      bit_rotate:
10519       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10520          is a rotate of A by C1 bits.  */
10521       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10522          is a rotate of A by B bits.  */
10523       {
10524         enum tree_code code0, code1;
10525         tree rtype;
10526         code0 = TREE_CODE (arg0);
10527         code1 = TREE_CODE (arg1);
10528         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10529              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10530             && operand_equal_p (TREE_OPERAND (arg0, 0),
10531                                 TREE_OPERAND (arg1, 0), 0)
10532             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10533                 TYPE_UNSIGNED (rtype))
10534             /* Only create rotates in complete modes.  Other cases are not
10535                expanded properly.  */
10536             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10537           {
10538             tree tree01, tree11;
10539             enum tree_code code01, code11;
10540
10541             tree01 = TREE_OPERAND (arg0, 1);
10542             tree11 = TREE_OPERAND (arg1, 1);
10543             STRIP_NOPS (tree01);
10544             STRIP_NOPS (tree11);
10545             code01 = TREE_CODE (tree01);
10546             code11 = TREE_CODE (tree11);
10547             if (code01 == INTEGER_CST
10548                 && code11 == INTEGER_CST
10549                 && TREE_INT_CST_HIGH (tree01) == 0
10550                 && TREE_INT_CST_HIGH (tree11) == 0
10551                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10552                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10553               {
10554                 tem = build2 (LROTATE_EXPR,
10555                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
10556                               TREE_OPERAND (arg0, 0),
10557                               code0 == LSHIFT_EXPR
10558                               ? tree01 : tree11);
10559                 SET_EXPR_LOCATION (tem, loc);
10560                 return fold_convert_loc (loc, type, tem);
10561               }
10562             else if (code11 == MINUS_EXPR)
10563               {
10564                 tree tree110, tree111;
10565                 tree110 = TREE_OPERAND (tree11, 0);
10566                 tree111 = TREE_OPERAND (tree11, 1);
10567                 STRIP_NOPS (tree110);
10568                 STRIP_NOPS (tree111);
10569                 if (TREE_CODE (tree110) == INTEGER_CST
10570                     && 0 == compare_tree_int (tree110,
10571                                               TYPE_PRECISION
10572                                               (TREE_TYPE (TREE_OPERAND
10573                                                           (arg0, 0))))
10574                     && operand_equal_p (tree01, tree111, 0))
10575                   return
10576                     fold_convert_loc (loc, type,
10577                                       build2 ((code0 == LSHIFT_EXPR
10578                                                ? LROTATE_EXPR
10579                                                : RROTATE_EXPR),
10580                                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
10581                                               TREE_OPERAND (arg0, 0), tree01));
10582               }
10583             else if (code01 == MINUS_EXPR)
10584               {
10585                 tree tree010, tree011;
10586                 tree010 = TREE_OPERAND (tree01, 0);
10587                 tree011 = TREE_OPERAND (tree01, 1);
10588                 STRIP_NOPS (tree010);
10589                 STRIP_NOPS (tree011);
10590                 if (TREE_CODE (tree010) == INTEGER_CST
10591                     && 0 == compare_tree_int (tree010,
10592                                               TYPE_PRECISION
10593                                               (TREE_TYPE (TREE_OPERAND
10594                                                           (arg0, 0))))
10595                     && operand_equal_p (tree11, tree011, 0))
10596                     return fold_convert_loc
10597                       (loc, type,
10598                        build2 ((code0 != LSHIFT_EXPR
10599                                 ? LROTATE_EXPR
10600                                 : RROTATE_EXPR),
10601                                TREE_TYPE (TREE_OPERAND (arg0, 0)),
10602                                TREE_OPERAND (arg0, 0), tree11));
10603               }
10604           }
10605       }
10606
10607     associate:
10608       /* In most languages, can't associate operations on floats through
10609          parentheses.  Rather than remember where the parentheses were, we
10610          don't associate floats at all, unless the user has specified
10611          -fassociative-math.
10612          And, we need to make sure type is not saturating.  */
10613
10614       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10615           && !TYPE_SATURATING (type))
10616         {
10617           tree var0, con0, lit0, minus_lit0;
10618           tree var1, con1, lit1, minus_lit1;
10619           bool ok = true;
10620
10621           /* Split both trees into variables, constants, and literals.  Then
10622              associate each group together, the constants with literals,
10623              then the result with variables.  This increases the chances of
10624              literals being recombined later and of generating relocatable
10625              expressions for the sum of a constant and literal.  */
10626           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10627           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10628                              code == MINUS_EXPR);
10629
10630           /* With undefined overflow we can only associate constants
10631              with one variable.  */
10632           if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10633                || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10634               && var0 && var1)
10635             {
10636               tree tmp0 = var0;
10637               tree tmp1 = var1;
10638
10639               if (TREE_CODE (tmp0) == NEGATE_EXPR)
10640                 tmp0 = TREE_OPERAND (tmp0, 0);
10641               if (TREE_CODE (tmp1) == NEGATE_EXPR)
10642                 tmp1 = TREE_OPERAND (tmp1, 0);
10643               /* The only case we can still associate with two variables
10644                  is if they are the same, modulo negation.  */
10645               if (!operand_equal_p (tmp0, tmp1, 0))
10646                 ok = false;
10647             }
10648
10649           /* Only do something if we found more than two objects.  Otherwise,
10650              nothing has changed and we risk infinite recursion.  */
10651           if (ok
10652               && (2 < ((var0 != 0) + (var1 != 0)
10653                        + (con0 != 0) + (con1 != 0)
10654                        + (lit0 != 0) + (lit1 != 0)
10655                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10656             {
10657               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
10658               if (code == MINUS_EXPR)
10659                 code = PLUS_EXPR;
10660
10661               var0 = associate_trees (loc, var0, var1, code, type);
10662               con0 = associate_trees (loc, con0, con1, code, type);
10663               lit0 = associate_trees (loc, lit0, lit1, code, type);
10664               minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
10665
10666               /* Preserve the MINUS_EXPR if the negative part of the literal is
10667                  greater than the positive part.  Otherwise, the multiplicative
10668                  folding code (i.e extract_muldiv) may be fooled in case
10669                  unsigned constants are subtracted, like in the following
10670                  example: ((X*2 + 4) - 8U)/2.  */
10671               if (minus_lit0 && lit0)
10672                 {
10673                   if (TREE_CODE (lit0) == INTEGER_CST
10674                       && TREE_CODE (minus_lit0) == INTEGER_CST
10675                       && tree_int_cst_lt (lit0, minus_lit0))
10676                     {
10677                       minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10678                                                     MINUS_EXPR, type);
10679                       lit0 = 0;
10680                     }
10681                   else
10682                     {
10683                       lit0 = associate_trees (loc, lit0, minus_lit0,
10684                                               MINUS_EXPR, type);
10685                       minus_lit0 = 0;
10686                     }
10687                 }
10688               if (minus_lit0)
10689                 {
10690                   if (con0 == 0)
10691                     return
10692                       fold_convert_loc (loc, type,
10693                                         associate_trees (loc, var0, minus_lit0,
10694                                                          MINUS_EXPR, type));
10695                   else
10696                     {
10697                       con0 = associate_trees (loc, con0, minus_lit0,
10698                                               MINUS_EXPR, type);
10699                       return
10700                         fold_convert_loc (loc, type,
10701                                           associate_trees (loc, var0, con0,
10702                                                            PLUS_EXPR, type));
10703                     }
10704                 }
10705
10706               con0 = associate_trees (loc, con0, lit0, code, type);
10707               return
10708                 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10709                                                               code, type));
10710             }
10711         }
10712
10713       return NULL_TREE;
10714
10715     case MINUS_EXPR:
10716       /* Pointer simplifications for subtraction, simple reassociations. */
10717       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10718         {
10719           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10720           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10721               && TREE_CODE (arg1) == 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 arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10726               tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10727               return fold_build2_loc (loc, PLUS_EXPR, type,
10728                                   fold_build2_loc (loc, MINUS_EXPR, type,
10729                                                arg00, arg10),
10730                                   fold_build2_loc (loc, MINUS_EXPR, type,
10731                                                arg01, arg11));
10732             }
10733           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10734           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10735             {
10736               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10737               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10738               tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10739                                       fold_convert_loc (loc, type, arg1));
10740               if (tmp)
10741                 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10742             }
10743         }
10744       /* A - (-B) -> A + B */
10745       if (TREE_CODE (arg1) == NEGATE_EXPR)
10746         return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10747                             fold_convert_loc (loc, type,
10748                                               TREE_OPERAND (arg1, 0)));
10749       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10750       if (TREE_CODE (arg0) == NEGATE_EXPR
10751           && (FLOAT_TYPE_P (type)
10752               || INTEGRAL_TYPE_P (type))
10753           && negate_expr_p (arg1)
10754           && reorder_operands_p (arg0, arg1))
10755         return fold_build2_loc (loc, MINUS_EXPR, type,
10756                             fold_convert_loc (loc, type,
10757                                               negate_expr (arg1)),
10758                             fold_convert_loc (loc, type,
10759                                               TREE_OPERAND (arg0, 0)));
10760       /* Convert -A - 1 to ~A.  */
10761       if (INTEGRAL_TYPE_P (type)
10762           && TREE_CODE (arg0) == NEGATE_EXPR
10763           && integer_onep (arg1)
10764           && !TYPE_OVERFLOW_TRAPS (type))
10765         return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10766                             fold_convert_loc (loc, type,
10767                                               TREE_OPERAND (arg0, 0)));
10768
10769       /* Convert -1 - A to ~A.  */
10770       if (INTEGRAL_TYPE_P (type)
10771           && integer_all_onesp (arg0))
10772         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10773
10774
10775       /* X - (X / CST) * CST is X % CST.  */
10776       if (INTEGRAL_TYPE_P (type)
10777           && TREE_CODE (arg1) == MULT_EXPR
10778           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10779           && operand_equal_p (arg0,
10780                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10781           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10782                               TREE_OPERAND (arg1, 1), 0))
10783         return
10784           fold_convert_loc (loc, type,
10785                             fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10786                                          arg0, TREE_OPERAND (arg1, 1)));
10787
10788       if (! FLOAT_TYPE_P (type))
10789         {
10790           if (integer_zerop (arg0))
10791             return negate_expr (fold_convert_loc (loc, type, arg1));
10792           if (integer_zerop (arg1))
10793             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10794
10795           /* Fold A - (A & B) into ~B & A.  */
10796           if (!TREE_SIDE_EFFECTS (arg0)
10797               && TREE_CODE (arg1) == BIT_AND_EXPR)
10798             {
10799               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10800                 {
10801                   tree arg10 = fold_convert_loc (loc, type,
10802                                                  TREE_OPERAND (arg1, 0));
10803                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10804                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10805                                                    type, arg10),
10806                                       fold_convert_loc (loc, type, arg0));
10807                 }
10808               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10809                 {
10810                   tree arg11 = fold_convert_loc (loc,
10811                                                  type, TREE_OPERAND (arg1, 1));
10812                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10813                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10814                                                    type, arg11),
10815                                       fold_convert_loc (loc, type, arg0));
10816                 }
10817             }
10818
10819           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10820              any power of 2 minus 1.  */
10821           if (TREE_CODE (arg0) == BIT_AND_EXPR
10822               && TREE_CODE (arg1) == BIT_AND_EXPR
10823               && operand_equal_p (TREE_OPERAND (arg0, 0),
10824                                   TREE_OPERAND (arg1, 0), 0))
10825             {
10826               tree mask0 = TREE_OPERAND (arg0, 1);
10827               tree mask1 = TREE_OPERAND (arg1, 1);
10828               tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10829
10830               if (operand_equal_p (tem, mask1, 0))
10831                 {
10832                   tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10833                                      TREE_OPERAND (arg0, 0), mask1);
10834                   return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10835                 }
10836             }
10837         }
10838
10839       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10840       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10841         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10842
10843       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10844          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10845          (-ARG1 + ARG0) reduces to -ARG1.  */
10846       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10847         return negate_expr (fold_convert_loc (loc, type, arg1));
10848
10849       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10850          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10851          signed zeros are involved.  */
10852       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10853           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10854           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10855         {
10856           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10857           tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10858           tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10859           bool arg0rz = false, arg0iz = false;
10860           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10861               || (arg0i && (arg0iz = real_zerop (arg0i))))
10862             {
10863               tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10864               tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10865               if (arg0rz && arg1i && real_zerop (arg1i))
10866                 {
10867                   tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10868                                          arg1r ? arg1r
10869                                          : build1 (REALPART_EXPR, rtype, arg1));
10870                   tree ip = arg0i ? arg0i
10871                     : build1 (IMAGPART_EXPR, rtype, arg0);
10872                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10873                 }
10874               else if (arg0iz && arg1r && real_zerop (arg1r))
10875                 {
10876                   tree rp = arg0r ? arg0r
10877                     : build1 (REALPART_EXPR, rtype, arg0);
10878                   tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10879                                          arg1i ? arg1i
10880                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10881                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10882                 }
10883             }
10884         }
10885
10886       /* Fold &x - &x.  This can happen from &x.foo - &x.
10887          This is unsafe for certain floats even in non-IEEE formats.
10888          In IEEE, it is unsafe because it does wrong for NaNs.
10889          Also note that operand_equal_p is always false if an operand
10890          is volatile.  */
10891
10892       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10893           && operand_equal_p (arg0, arg1, 0))
10894         return fold_convert_loc (loc, type, integer_zero_node);
10895
10896       /* A - B -> A + (-B) if B is easily negatable.  */
10897       if (negate_expr_p (arg1)
10898           && ((FLOAT_TYPE_P (type)
10899                /* Avoid this transformation if B is a positive REAL_CST.  */
10900                && (TREE_CODE (arg1) != REAL_CST
10901                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10902               || INTEGRAL_TYPE_P (type)))
10903         return fold_build2_loc (loc, PLUS_EXPR, type,
10904                             fold_convert_loc (loc, type, arg0),
10905                             fold_convert_loc (loc, type,
10906                                               negate_expr (arg1)));
10907
10908       /* Try folding difference of addresses.  */
10909       {
10910         HOST_WIDE_INT diff;
10911
10912         if ((TREE_CODE (arg0) == ADDR_EXPR
10913              || TREE_CODE (arg1) == ADDR_EXPR)
10914             && ptr_difference_const (arg0, arg1, &diff))
10915           return build_int_cst_type (type, diff);
10916       }
10917
10918       /* Fold &a[i] - &a[j] to i-j.  */
10919       if (TREE_CODE (arg0) == ADDR_EXPR
10920           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10921           && TREE_CODE (arg1) == ADDR_EXPR
10922           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10923         {
10924           tree aref0 = TREE_OPERAND (arg0, 0);
10925           tree aref1 = TREE_OPERAND (arg1, 0);
10926           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10927                                TREE_OPERAND (aref1, 0), 0))
10928             {
10929               tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10930               tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10931               tree esz = array_ref_element_size (aref0);
10932               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10933               return fold_build2_loc (loc, MULT_EXPR, type, diff,
10934                                   fold_convert_loc (loc, type, esz));
10935
10936             }
10937         }
10938
10939       if (FLOAT_TYPE_P (type)
10940           && flag_unsafe_math_optimizations
10941           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10942           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10943           && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10944         return tem;
10945
10946       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10947          same or one.  Make sure type is not saturating.
10948          fold_plusminus_mult_expr will re-associate.  */
10949       if ((TREE_CODE (arg0) == MULT_EXPR
10950            || TREE_CODE (arg1) == MULT_EXPR)
10951           && !TYPE_SATURATING (type)
10952           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10953         {
10954           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10955           if (tem)
10956             return tem;
10957         }
10958
10959       goto associate;
10960
10961     case MULT_EXPR:
10962       /* (-A) * (-B) -> A * B  */
10963       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10964         return fold_build2_loc (loc, MULT_EXPR, type,
10965                             fold_convert_loc (loc, type,
10966                                               TREE_OPERAND (arg0, 0)),
10967                             fold_convert_loc (loc, type,
10968                                               negate_expr (arg1)));
10969       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10970         return fold_build2_loc (loc, MULT_EXPR, type,
10971                             fold_convert_loc (loc, type,
10972                                               negate_expr (arg0)),
10973                             fold_convert_loc (loc, type,
10974                                               TREE_OPERAND (arg1, 0)));
10975
10976       if (! FLOAT_TYPE_P (type))
10977         {
10978           if (integer_zerop (arg1))
10979             return omit_one_operand_loc (loc, type, arg1, arg0);
10980           if (integer_onep (arg1))
10981             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10982           /* Transform x * -1 into -x.  Make sure to do the negation
10983              on the original operand with conversions not stripped
10984              because we can only strip non-sign-changing conversions.  */
10985           if (integer_all_onesp (arg1))
10986             return fold_convert_loc (loc, type, negate_expr (op0));
10987           /* Transform x * -C into -x * C if x is easily negatable.  */
10988           if (TREE_CODE (arg1) == INTEGER_CST
10989               && tree_int_cst_sgn (arg1) == -1
10990               && negate_expr_p (arg0)
10991               && (tem = negate_expr (arg1)) != arg1
10992               && !TREE_OVERFLOW (tem))
10993             return fold_build2_loc (loc, MULT_EXPR, type,
10994                                 fold_convert_loc (loc, type,
10995                                                   negate_expr (arg0)),
10996                                 tem);
10997
10998           /* (a * (1 << b)) is (a << b)  */
10999           if (TREE_CODE (arg1) == LSHIFT_EXPR
11000               && integer_onep (TREE_OPERAND (arg1, 0)))
11001             return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
11002                                 TREE_OPERAND (arg1, 1));
11003           if (TREE_CODE (arg0) == LSHIFT_EXPR
11004               && integer_onep (TREE_OPERAND (arg0, 0)))
11005             return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
11006                                 TREE_OPERAND (arg0, 1));
11007
11008           /* (A + A) * C -> A * 2 * C  */
11009           if (TREE_CODE (arg0) == PLUS_EXPR
11010               && TREE_CODE (arg1) == INTEGER_CST
11011               && operand_equal_p (TREE_OPERAND (arg0, 0),
11012                                   TREE_OPERAND (arg0, 1), 0))
11013             return fold_build2_loc (loc, MULT_EXPR, type,
11014                                 omit_one_operand_loc (loc, type,
11015                                                   TREE_OPERAND (arg0, 0),
11016                                                   TREE_OPERAND (arg0, 1)),
11017                                 fold_build2_loc (loc, MULT_EXPR, type,
11018                                              build_int_cst (type, 2) , arg1));
11019
11020           strict_overflow_p = false;
11021           if (TREE_CODE (arg1) == INTEGER_CST
11022               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11023                                              &strict_overflow_p)))
11024             {
11025               if (strict_overflow_p)
11026                 fold_overflow_warning (("assuming signed overflow does not "
11027                                         "occur when simplifying "
11028                                         "multiplication"),
11029                                        WARN_STRICT_OVERFLOW_MISC);
11030               return fold_convert_loc (loc, type, tem);
11031             }
11032
11033           /* Optimize z * conj(z) for integer complex numbers.  */
11034           if (TREE_CODE (arg0) == CONJ_EXPR
11035               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11036             return fold_mult_zconjz (loc, type, arg1);
11037           if (TREE_CODE (arg1) == CONJ_EXPR
11038               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11039             return fold_mult_zconjz (loc, type, arg0);
11040         }
11041       else
11042         {
11043           /* Maybe fold x * 0 to 0.  The expressions aren't the same
11044              when x is NaN, since x * 0 is also NaN.  Nor are they the
11045              same in modes with signed zeros, since multiplying a
11046              negative value by 0 gives -0, not +0.  */
11047           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11048               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11049               && real_zerop (arg1))
11050             return omit_one_operand_loc (loc, type, arg1, arg0);
11051           /* In IEEE floating point, x*1 is not equivalent to x for snans.
11052              Likewise for complex arithmetic with signed zeros.  */
11053           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11054               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11055                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11056               && real_onep (arg1))
11057             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11058
11059           /* Transform x * -1.0 into -x.  */
11060           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11061               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11062                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11063               && real_minus_onep (arg1))
11064             return fold_convert_loc (loc, type, negate_expr (arg0));
11065
11066           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
11067              the result for floating point types due to rounding so it is applied
11068              only if -fassociative-math was specify.  */
11069           if (flag_associative_math
11070               && TREE_CODE (arg0) == RDIV_EXPR
11071               && TREE_CODE (arg1) == REAL_CST
11072               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
11073             {
11074               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
11075                                       arg1, 0);
11076               if (tem)
11077                 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11078                                     TREE_OPERAND (arg0, 1));
11079             }
11080
11081           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
11082           if (operand_equal_p (arg0, arg1, 0))
11083             {
11084               tree tem = fold_strip_sign_ops (arg0);
11085               if (tem != NULL_TREE)
11086                 {
11087                   tem = fold_convert_loc (loc, type, tem);
11088                   return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
11089                 }
11090             }
11091
11092           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11093              This is not the same for NaNs or if signed zeros are
11094              involved.  */
11095           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11096               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11097               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11098               && TREE_CODE (arg1) == COMPLEX_CST
11099               && real_zerop (TREE_REALPART (arg1)))
11100             {
11101               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11102               if (real_onep (TREE_IMAGPART (arg1)))
11103                 return
11104                   fold_build2_loc (loc, COMPLEX_EXPR, type,
11105                                negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
11106                                                              rtype, arg0)),
11107                                fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
11108               else if (real_minus_onep (TREE_IMAGPART (arg1)))
11109                 return
11110                   fold_build2_loc (loc, COMPLEX_EXPR, type,
11111                                fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
11112                                negate_expr (fold_build1_loc (loc, REALPART_EXPR,
11113                                                              rtype, arg0)));
11114             }
11115
11116           /* Optimize z * conj(z) for floating point complex numbers.
11117              Guarded by flag_unsafe_math_optimizations as non-finite
11118              imaginary components don't produce scalar results.  */
11119           if (flag_unsafe_math_optimizations
11120               && TREE_CODE (arg0) == CONJ_EXPR
11121               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11122             return fold_mult_zconjz (loc, type, arg1);
11123           if (flag_unsafe_math_optimizations
11124               && TREE_CODE (arg1) == CONJ_EXPR
11125               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11126             return fold_mult_zconjz (loc, type, arg0);
11127
11128           if (flag_unsafe_math_optimizations)
11129             {
11130               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11131               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11132
11133               /* Optimizations of root(...)*root(...).  */
11134               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
11135                 {
11136                   tree rootfn, arg;
11137                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
11138                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
11139
11140                   /* Optimize sqrt(x)*sqrt(x) as x.  */
11141                   if (BUILTIN_SQRT_P (fcode0)
11142                       && operand_equal_p (arg00, arg10, 0)
11143                       && ! HONOR_SNANS (TYPE_MODE (type)))
11144                     return arg00;
11145
11146                   /* Optimize root(x)*root(y) as root(x*y).  */
11147                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11148                   arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
11149                   return build_call_expr_loc (loc, rootfn, 1, arg);
11150                 }
11151
11152               /* Optimize expN(x)*expN(y) as expN(x+y).  */
11153               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
11154                 {
11155                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11156                   tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
11157                                           CALL_EXPR_ARG (arg0, 0),
11158                                           CALL_EXPR_ARG (arg1, 0));
11159                   return build_call_expr_loc (loc, expfn, 1, arg);
11160                 }
11161
11162               /* Optimizations of pow(...)*pow(...).  */
11163               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
11164                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
11165                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
11166                 {
11167                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
11168                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
11169                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
11170                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
11171
11172                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
11173                   if (operand_equal_p (arg01, arg11, 0))
11174                     {
11175                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11176                       tree arg = fold_build2_loc (loc, MULT_EXPR, type,
11177                                               arg00, arg10);
11178                       return build_call_expr_loc (loc, powfn, 2, arg, arg01);
11179                     }
11180
11181                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
11182                   if (operand_equal_p (arg00, arg10, 0))
11183                     {
11184                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11185                       tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
11186                                               arg01, arg11);
11187                       return build_call_expr_loc (loc, powfn, 2, arg00, arg);
11188                     }
11189                 }
11190
11191               /* Optimize tan(x)*cos(x) as sin(x).  */
11192               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
11193                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
11194                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
11195                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
11196                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
11197                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
11198                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11199                                       CALL_EXPR_ARG (arg1, 0), 0))
11200                 {
11201                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
11202
11203                   if (sinfn != NULL_TREE)
11204                     return build_call_expr_loc (loc, sinfn, 1,
11205                                             CALL_EXPR_ARG (arg0, 0));
11206                 }
11207
11208               /* Optimize x*pow(x,c) as pow(x,c+1).  */
11209               if (fcode1 == BUILT_IN_POW
11210                   || fcode1 == BUILT_IN_POWF
11211                   || fcode1 == BUILT_IN_POWL)
11212                 {
11213                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
11214                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
11215                   if (TREE_CODE (arg11) == REAL_CST
11216                       && !TREE_OVERFLOW (arg11)
11217                       && operand_equal_p (arg0, arg10, 0))
11218                     {
11219                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11220                       REAL_VALUE_TYPE c;
11221                       tree arg;
11222
11223                       c = TREE_REAL_CST (arg11);
11224                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
11225                       arg = build_real (type, c);
11226                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
11227                     }
11228                 }
11229
11230               /* Optimize pow(x,c)*x as pow(x,c+1).  */
11231               if (fcode0 == BUILT_IN_POW
11232                   || fcode0 == BUILT_IN_POWF
11233                   || fcode0 == BUILT_IN_POWL)
11234                 {
11235                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
11236                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
11237                   if (TREE_CODE (arg01) == REAL_CST
11238                       && !TREE_OVERFLOW (arg01)
11239                       && operand_equal_p (arg1, arg00, 0))
11240                     {
11241                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11242                       REAL_VALUE_TYPE c;
11243                       tree arg;
11244
11245                       c = TREE_REAL_CST (arg01);
11246                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
11247                       arg = build_real (type, c);
11248                       return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11249                     }
11250                 }
11251
11252               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
11253               if (optimize_function_for_speed_p (cfun)
11254                   && operand_equal_p (arg0, arg1, 0))
11255                 {
11256                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
11257
11258                   if (powfn)
11259                     {
11260                       tree arg = build_real (type, dconst2);
11261                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
11262                     }
11263                 }
11264             }
11265         }
11266       goto associate;
11267
11268     case BIT_IOR_EXPR:
11269     bit_ior:
11270       if (integer_all_onesp (arg1))
11271         return omit_one_operand_loc (loc, type, arg1, arg0);
11272       if (integer_zerop (arg1))
11273         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11274       if (operand_equal_p (arg0, arg1, 0))
11275         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11276
11277       /* ~X | X is -1.  */
11278       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11279           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11280         {
11281           t1 = fold_convert_loc (loc, type, integer_zero_node);
11282           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11283           return omit_one_operand_loc (loc, type, t1, arg1);
11284         }
11285
11286       /* X | ~X is -1.  */
11287       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11288           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11289         {
11290           t1 = fold_convert_loc (loc, type, integer_zero_node);
11291           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11292           return omit_one_operand_loc (loc, type, t1, arg0);
11293         }
11294
11295       /* Canonicalize (X & C1) | C2.  */
11296       if (TREE_CODE (arg0) == BIT_AND_EXPR
11297           && TREE_CODE (arg1) == INTEGER_CST
11298           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11299         {
11300           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
11301           int width = TYPE_PRECISION (type), w;
11302           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
11303           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11304           hi2 = TREE_INT_CST_HIGH (arg1);
11305           lo2 = TREE_INT_CST_LOW (arg1);
11306
11307           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
11308           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
11309             return omit_one_operand_loc (loc, type, arg1,
11310                                      TREE_OPERAND (arg0, 0));
11311
11312           if (width > HOST_BITS_PER_WIDE_INT)
11313             {
11314               mhi = (unsigned HOST_WIDE_INT) -1
11315                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
11316               mlo = -1;
11317             }
11318           else
11319             {
11320               mhi = 0;
11321               mlo = (unsigned HOST_WIDE_INT) -1
11322                     >> (HOST_BITS_PER_WIDE_INT - width);
11323             }
11324
11325           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
11326           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
11327             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11328                                 TREE_OPERAND (arg0, 0), arg1);
11329
11330           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11331              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11332              mode which allows further optimizations.  */
11333           hi1 &= mhi;
11334           lo1 &= mlo;
11335           hi2 &= mhi;
11336           lo2 &= mlo;
11337           hi3 = hi1 & ~hi2;
11338           lo3 = lo1 & ~lo2;
11339           for (w = BITS_PER_UNIT;
11340                w <= width && w <= HOST_BITS_PER_WIDE_INT;
11341                w <<= 1)
11342             {
11343               unsigned HOST_WIDE_INT mask
11344                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
11345               if (((lo1 | lo2) & mask) == mask
11346                   && (lo1 & ~mask) == 0 && hi1 == 0)
11347                 {
11348                   hi3 = 0;
11349                   lo3 = mask;
11350                   break;
11351                 }
11352             }
11353           if (hi3 != hi1 || lo3 != lo1)
11354             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11355                                 fold_build2_loc (loc, BIT_AND_EXPR, type,
11356                                              TREE_OPERAND (arg0, 0),
11357                                              build_int_cst_wide (type,
11358                                                                  lo3, hi3)),
11359                                 arg1);
11360         }
11361
11362       /* (X & Y) | Y is (X, Y).  */
11363       if (TREE_CODE (arg0) == BIT_AND_EXPR
11364           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11365         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11366       /* (X & Y) | X is (Y, X).  */
11367       if (TREE_CODE (arg0) == BIT_AND_EXPR
11368           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11369           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11370         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11371       /* X | (X & Y) is (Y, X).  */
11372       if (TREE_CODE (arg1) == BIT_AND_EXPR
11373           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11374           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11375         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11376       /* X | (Y & X) is (Y, X).  */
11377       if (TREE_CODE (arg1) == BIT_AND_EXPR
11378           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11379           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11380         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11381
11382       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11383       if (t1 != NULL_TREE)
11384         return t1;
11385
11386       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
11387
11388          This results in more efficient code for machines without a NAND
11389          instruction.  Combine will canonicalize to the first form
11390          which will allow use of NAND instructions provided by the
11391          backend if they exist.  */
11392       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11393           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11394         {
11395           return
11396             fold_build1_loc (loc, BIT_NOT_EXPR, type,
11397                          build2 (BIT_AND_EXPR, type,
11398                                  fold_convert_loc (loc, type,
11399                                                    TREE_OPERAND (arg0, 0)),
11400                                  fold_convert_loc (loc, type,
11401                                                    TREE_OPERAND (arg1, 0))));
11402         }
11403
11404       /* See if this can be simplified into a rotate first.  If that
11405          is unsuccessful continue in the association code.  */
11406       goto bit_rotate;
11407
11408     case BIT_XOR_EXPR:
11409       if (integer_zerop (arg1))
11410         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11411       if (integer_all_onesp (arg1))
11412         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
11413       if (operand_equal_p (arg0, arg1, 0))
11414         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11415
11416       /* ~X ^ X is -1.  */
11417       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11418           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11419         {
11420           t1 = fold_convert_loc (loc, type, integer_zero_node);
11421           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11422           return omit_one_operand_loc (loc, type, t1, arg1);
11423         }
11424
11425       /* X ^ ~X is -1.  */
11426       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11427           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11428         {
11429           t1 = fold_convert_loc (loc, type, integer_zero_node);
11430           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11431           return omit_one_operand_loc (loc, type, t1, arg0);
11432         }
11433
11434       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11435          with a constant, and the two constants have no bits in common,
11436          we should treat this as a BIT_IOR_EXPR since this may produce more
11437          simplifications.  */
11438       if (TREE_CODE (arg0) == BIT_AND_EXPR
11439           && TREE_CODE (arg1) == BIT_AND_EXPR
11440           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11441           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11442           && integer_zerop (const_binop (BIT_AND_EXPR,
11443                                          TREE_OPERAND (arg0, 1),
11444                                          TREE_OPERAND (arg1, 1), 0)))
11445         {
11446           code = BIT_IOR_EXPR;
11447           goto bit_ior;
11448         }
11449
11450       /* (X | Y) ^ X -> Y & ~ X*/
11451       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11452           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11453         {
11454           tree t2 = TREE_OPERAND (arg0, 1);
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       /* (Y | X) ^ X -> Y & ~ X*/
11464       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11465           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11466         {
11467           tree t2 = TREE_OPERAND (arg0, 0);
11468           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11469                             arg1);
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 ^ (X | Y) -> Y & ~ X*/
11477       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11478           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11479         {
11480           tree t2 = TREE_OPERAND (arg1, 1);
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       /* X ^ (Y | X) -> Y & ~ X*/
11490       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11491           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11492         {
11493           tree t2 = TREE_OPERAND (arg1, 0);
11494           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11495                             arg0);
11496           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11497                             fold_convert_loc (loc, type, t2),
11498                             fold_convert_loc (loc, type, t1));
11499           return t1;
11500         }
11501
11502       /* Convert ~X ^ ~Y to X ^ Y.  */
11503       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11504           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11505         return fold_build2_loc (loc, code, type,
11506                             fold_convert_loc (loc, type,
11507                                               TREE_OPERAND (arg0, 0)),
11508                             fold_convert_loc (loc, type,
11509                                               TREE_OPERAND (arg1, 0)));
11510
11511       /* Convert ~X ^ C to X ^ ~C.  */
11512       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11513           && TREE_CODE (arg1) == INTEGER_CST)
11514         return fold_build2_loc (loc, code, type,
11515                             fold_convert_loc (loc, type,
11516                                               TREE_OPERAND (arg0, 0)),
11517                             fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
11518
11519       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
11520       if (TREE_CODE (arg0) == BIT_AND_EXPR
11521           && integer_onep (TREE_OPERAND (arg0, 1))
11522           && integer_onep (arg1))
11523         return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11524                             build_int_cst (TREE_TYPE (arg0), 0));
11525
11526       /* Fold (X & Y) ^ Y as ~X & Y.  */
11527       if (TREE_CODE (arg0) == BIT_AND_EXPR
11528           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11529         {
11530           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11531           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11532                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11533                               fold_convert_loc (loc, type, arg1));
11534         }
11535       /* Fold (X & Y) ^ X as ~Y & X.  */
11536       if (TREE_CODE (arg0) == BIT_AND_EXPR
11537           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11538           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11539         {
11540           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11541           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11542                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11543                               fold_convert_loc (loc, type, arg1));
11544         }
11545       /* Fold X ^ (X & Y) as X & ~Y.  */
11546       if (TREE_CODE (arg1) == BIT_AND_EXPR
11547           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11548         {
11549           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11550           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11551                               fold_convert_loc (loc, type, arg0),
11552                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11553         }
11554       /* Fold X ^ (Y & X) as ~Y & X.  */
11555       if (TREE_CODE (arg1) == BIT_AND_EXPR
11556           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11557           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11558         {
11559           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11560           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11561                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11562                               fold_convert_loc (loc, type, arg0));
11563         }
11564
11565       /* See if this can be simplified into a rotate first.  If that
11566          is unsuccessful continue in the association code.  */
11567       goto bit_rotate;
11568
11569     case BIT_AND_EXPR:
11570       if (integer_all_onesp (arg1))
11571         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11572       if (integer_zerop (arg1))
11573         return omit_one_operand_loc (loc, type, arg1, arg0);
11574       if (operand_equal_p (arg0, arg1, 0))
11575         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11576
11577       /* ~X & X is always zero.  */
11578       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11579           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11580         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11581
11582       /* X & ~X is always zero.  */
11583       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11584           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11585         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11586
11587       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
11588       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11589           && TREE_CODE (arg1) == INTEGER_CST
11590           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11591         {
11592           tree tmp1 = fold_convert_loc (loc, type, arg1);
11593           tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11594           tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11595           tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
11596           tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
11597           return
11598             fold_convert_loc (loc, type,
11599                               fold_build2_loc (loc, BIT_IOR_EXPR,
11600                                            type, tmp2, tmp3));
11601         }
11602
11603       /* (X | Y) & Y is (X, Y).  */
11604       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11605           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11606         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11607       /* (X | Y) & X is (Y, X).  */
11608       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11609           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11610           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11611         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11612       /* X & (X | Y) is (Y, X).  */
11613       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11614           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11615           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11616         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11617       /* X & (Y | X) is (Y, X).  */
11618       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11619           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11620           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11621         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11622
11623       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11624       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11625           && integer_onep (TREE_OPERAND (arg0, 1))
11626           && integer_onep (arg1))
11627         {
11628           tem = TREE_OPERAND (arg0, 0);
11629           return fold_build2_loc (loc, EQ_EXPR, type,
11630                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11631                                            build_int_cst (TREE_TYPE (tem), 1)),
11632                               build_int_cst (TREE_TYPE (tem), 0));
11633         }
11634       /* Fold ~X & 1 as (X & 1) == 0.  */
11635       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11636           && integer_onep (arg1))
11637         {
11638           tem = TREE_OPERAND (arg0, 0);
11639           return fold_build2_loc (loc, EQ_EXPR, type,
11640                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11641                                            build_int_cst (TREE_TYPE (tem), 1)),
11642                               build_int_cst (TREE_TYPE (tem), 0));
11643         }
11644
11645       /* Fold (X ^ Y) & Y as ~X & Y.  */
11646       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11647           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11648         {
11649           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11650           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11651                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11652                               fold_convert_loc (loc, type, arg1));
11653         }
11654       /* Fold (X ^ Y) & X as ~Y & X.  */
11655       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11656           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11657           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11658         {
11659           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11660           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11661                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11662                               fold_convert_loc (loc, type, arg1));
11663         }
11664       /* Fold X & (X ^ Y) as X & ~Y.  */
11665       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11666           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11667         {
11668           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11669           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11670                               fold_convert_loc (loc, type, arg0),
11671                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11672         }
11673       /* Fold X & (Y ^ X) as ~Y & X.  */
11674       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11675           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11676           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11677         {
11678           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11679           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11680                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11681                               fold_convert_loc (loc, type, arg0));
11682         }
11683
11684       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11685       if (t1 != NULL_TREE)
11686         return t1;
11687       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11688       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11689           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11690         {
11691           unsigned int prec
11692             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11693
11694           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11695               && (~TREE_INT_CST_LOW (arg1)
11696                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11697             return
11698               fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11699         }
11700
11701       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11702
11703          This results in more efficient code for machines without a NOR
11704          instruction.  Combine will canonicalize to the first form
11705          which will allow use of NOR instructions provided by the
11706          backend if they exist.  */
11707       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11708           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11709         {
11710           return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11711                               build2 (BIT_IOR_EXPR, type,
11712                                       fold_convert_loc (loc, type,
11713                                                         TREE_OPERAND (arg0, 0)),
11714                                       fold_convert_loc (loc, type,
11715                                                         TREE_OPERAND (arg1, 0))));
11716         }
11717
11718       /* If arg0 is derived from the address of an object or function, we may
11719          be able to fold this expression using the object or function's
11720          alignment.  */
11721       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11722         {
11723           unsigned HOST_WIDE_INT modulus, residue;
11724           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11725
11726           modulus = get_pointer_modulus_and_residue (arg0, &residue,
11727                                                      integer_onep (arg1));
11728
11729           /* This works because modulus is a power of 2.  If this weren't the
11730              case, we'd have to replace it by its greatest power-of-2
11731              divisor: modulus & -modulus.  */
11732           if (low < modulus)
11733             return build_int_cst (type, residue & low);
11734         }
11735
11736       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11737               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11738          if the new mask might be further optimized.  */
11739       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11740            || TREE_CODE (arg0) == RSHIFT_EXPR)
11741           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11742           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11743           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11744              < TYPE_PRECISION (TREE_TYPE (arg0))
11745           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11746           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11747         {
11748           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11749           unsigned HOST_WIDE_INT mask
11750             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11751           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11752           tree shift_type = TREE_TYPE (arg0);
11753
11754           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11755             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11756           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11757                    && TYPE_PRECISION (TREE_TYPE (arg0))
11758                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11759             {
11760               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11761               tree arg00 = TREE_OPERAND (arg0, 0);
11762               /* See if more bits can be proven as zero because of
11763                  zero extension.  */
11764               if (TREE_CODE (arg00) == NOP_EXPR
11765                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11766                 {
11767                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11768                   if (TYPE_PRECISION (inner_type)
11769                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11770                       && TYPE_PRECISION (inner_type) < prec)
11771                     {
11772                       prec = TYPE_PRECISION (inner_type);
11773                       /* See if we can shorten the right shift.  */
11774                       if (shiftc < prec)
11775                         shift_type = inner_type;
11776                     }
11777                 }
11778               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11779               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11780               zerobits <<= prec - shiftc;
11781               /* For arithmetic shift if sign bit could be set, zerobits
11782                  can contain actually sign bits, so no transformation is
11783                  possible, unless MASK masks them all away.  In that
11784                  case the shift needs to be converted into logical shift.  */
11785               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11786                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11787                 {
11788                   if ((mask & zerobits) == 0)
11789                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11790                   else
11791                     zerobits = 0;
11792                 }
11793             }
11794
11795           /* ((X << 16) & 0xff00) is (X, 0).  */
11796           if ((mask & zerobits) == mask)
11797             return omit_one_operand_loc (loc, type,
11798                                      build_int_cst (type, 0), arg0);
11799
11800           newmask = mask | zerobits;
11801           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11802             {
11803               unsigned int prec;
11804
11805               /* Only do the transformation if NEWMASK is some integer
11806                  mode's mask.  */
11807               for (prec = BITS_PER_UNIT;
11808                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11809                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11810                   break;
11811               if (prec < HOST_BITS_PER_WIDE_INT
11812                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11813                 {
11814                   tree newmaskt;
11815
11816                   if (shift_type != TREE_TYPE (arg0))
11817                     {
11818                       tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11819                                          fold_convert_loc (loc, shift_type,
11820                                                            TREE_OPERAND (arg0, 0)),
11821                                          TREE_OPERAND (arg0, 1));
11822                       tem = fold_convert_loc (loc, type, tem);
11823                     }
11824                   else
11825                     tem = op0;
11826                   newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11827                   if (!tree_int_cst_equal (newmaskt, arg1))
11828                     return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11829                 }
11830             }
11831         }
11832
11833       goto associate;
11834
11835     case RDIV_EXPR:
11836       /* Don't touch a floating-point divide by zero unless the mode
11837          of the constant can represent infinity.  */
11838       if (TREE_CODE (arg1) == REAL_CST
11839           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11840           && real_zerop (arg1))
11841         return NULL_TREE;
11842
11843       /* Optimize A / A to 1.0 if we don't care about
11844          NaNs or Infinities.  Skip the transformation
11845          for non-real operands.  */
11846       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11847           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11848           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11849           && operand_equal_p (arg0, arg1, 0))
11850         {
11851           tree r = build_real (TREE_TYPE (arg0), dconst1);
11852
11853           return omit_two_operands_loc (loc, type, r, arg0, arg1);
11854         }
11855
11856       /* The complex version of the above A / A optimization.  */
11857       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11858           && operand_equal_p (arg0, arg1, 0))
11859         {
11860           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11861           if (! HONOR_NANS (TYPE_MODE (elem_type))
11862               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11863             {
11864               tree r = build_real (elem_type, dconst1);
11865               /* omit_two_operands will call fold_convert for us.  */
11866               return omit_two_operands_loc (loc, type, r, arg0, arg1);
11867             }
11868         }
11869
11870       /* (-A) / (-B) -> A / B  */
11871       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11872         return fold_build2_loc (loc, RDIV_EXPR, type,
11873                             TREE_OPERAND (arg0, 0),
11874                             negate_expr (arg1));
11875       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11876         return fold_build2_loc (loc, RDIV_EXPR, type,
11877                             negate_expr (arg0),
11878                             TREE_OPERAND (arg1, 0));
11879
11880       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11881       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11882           && real_onep (arg1))
11883         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11884
11885       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11886       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11887           && real_minus_onep (arg1))
11888         return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11889                                                   negate_expr (arg0)));
11890
11891       /* If ARG1 is a constant, we can convert this to a multiply by the
11892          reciprocal.  This does not have the same rounding properties,
11893          so only do this if -freciprocal-math.  We can actually
11894          always safely do it if ARG1 is a power of two, but it's hard to
11895          tell if it is or not in a portable manner.  */
11896       if (TREE_CODE (arg1) == REAL_CST)
11897         {
11898           if (flag_reciprocal_math
11899               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11900                                           arg1, 0)))
11901             return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11902           /* Find the reciprocal if optimizing and the result is exact.  */
11903           if (optimize)
11904             {
11905               REAL_VALUE_TYPE r;
11906               r = TREE_REAL_CST (arg1);
11907               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11908                 {
11909                   tem = build_real (type, r);
11910                   return fold_build2_loc (loc, MULT_EXPR, type,
11911                                       fold_convert_loc (loc, type, arg0), tem);
11912                 }
11913             }
11914         }
11915       /* Convert A/B/C to A/(B*C).  */
11916       if (flag_reciprocal_math
11917           && TREE_CODE (arg0) == RDIV_EXPR)
11918         return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11919                             fold_build2_loc (loc, MULT_EXPR, type,
11920                                          TREE_OPERAND (arg0, 1), arg1));
11921
11922       /* Convert A/(B/C) to (A/B)*C.  */
11923       if (flag_reciprocal_math
11924           && TREE_CODE (arg1) == RDIV_EXPR)
11925         return fold_build2_loc (loc, MULT_EXPR, type,
11926                             fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11927                                          TREE_OPERAND (arg1, 0)),
11928                             TREE_OPERAND (arg1, 1));
11929
11930       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11931       if (flag_reciprocal_math
11932           && TREE_CODE (arg1) == MULT_EXPR
11933           && TREE_CODE (arg0) == REAL_CST
11934           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11935         {
11936           tree tem = const_binop (RDIV_EXPR, arg0,
11937                                   TREE_OPERAND (arg1, 1), 0);
11938           if (tem)
11939             return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11940                                 TREE_OPERAND (arg1, 0));
11941         }
11942
11943       if (flag_unsafe_math_optimizations)
11944         {
11945           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11946           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11947
11948           /* Optimize sin(x)/cos(x) as tan(x).  */
11949           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11950                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11951                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
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                 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11959             }
11960
11961           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11962           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11963                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11964                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11965               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11966                                   CALL_EXPR_ARG (arg1, 0), 0))
11967             {
11968               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11969
11970               if (tanfn != NULL_TREE)
11971                 {
11972                   tree tmp = build_call_expr_loc (loc, tanfn, 1,
11973                                               CALL_EXPR_ARG (arg0, 0));
11974                   return fold_build2_loc (loc, RDIV_EXPR, type,
11975                                       build_real (type, dconst1), tmp);
11976                 }
11977             }
11978
11979           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11980              NaNs or Infinities.  */
11981           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11982                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11983                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11984             {
11985               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11986               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11987
11988               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11989                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11990                   && operand_equal_p (arg00, arg01, 0))
11991                 {
11992                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11993
11994                   if (cosfn != NULL_TREE)
11995                     return build_call_expr_loc (loc, cosfn, 1, arg00);
11996                 }
11997             }
11998
11999           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
12000              NaNs or Infinities.  */
12001           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
12002                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
12003                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
12004             {
12005               tree arg00 = CALL_EXPR_ARG (arg0, 0);
12006               tree arg01 = CALL_EXPR_ARG (arg1, 0);
12007
12008               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
12009                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
12010                   && operand_equal_p (arg00, arg01, 0))
12011                 {
12012                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
12013
12014                   if (cosfn != NULL_TREE)
12015                     {
12016                       tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
12017                       return fold_build2_loc (loc, RDIV_EXPR, type,
12018                                           build_real (type, dconst1),
12019                                           tmp);
12020                     }
12021                 }
12022             }
12023
12024           /* Optimize pow(x,c)/x as pow(x,c-1).  */
12025           if (fcode0 == BUILT_IN_POW
12026               || fcode0 == BUILT_IN_POWF
12027               || fcode0 == BUILT_IN_POWL)
12028             {
12029               tree arg00 = CALL_EXPR_ARG (arg0, 0);
12030               tree arg01 = CALL_EXPR_ARG (arg0, 1);
12031               if (TREE_CODE (arg01) == REAL_CST
12032                   && !TREE_OVERFLOW (arg01)
12033                   && operand_equal_p (arg1, arg00, 0))
12034                 {
12035                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
12036                   REAL_VALUE_TYPE c;
12037                   tree arg;
12038
12039                   c = TREE_REAL_CST (arg01);
12040                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
12041                   arg = build_real (type, c);
12042                   return build_call_expr_loc (loc, powfn, 2, arg1, arg);
12043                 }
12044             }
12045
12046           /* Optimize a/root(b/c) into a*root(c/b).  */
12047           if (BUILTIN_ROOT_P (fcode1))
12048             {
12049               tree rootarg = CALL_EXPR_ARG (arg1, 0);
12050
12051               if (TREE_CODE (rootarg) == RDIV_EXPR)
12052                 {
12053                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12054                   tree b = TREE_OPERAND (rootarg, 0);
12055                   tree c = TREE_OPERAND (rootarg, 1);
12056
12057                   tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
12058
12059                   tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
12060                   return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
12061                 }
12062             }
12063
12064           /* Optimize x/expN(y) into x*expN(-y).  */
12065           if (BUILTIN_EXPONENT_P (fcode1))
12066             {
12067               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12068               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
12069               arg1 = build_call_expr_loc (loc,
12070                                       expfn, 1,
12071                                       fold_convert_loc (loc, type, arg));
12072               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
12073             }
12074
12075           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
12076           if (fcode1 == BUILT_IN_POW
12077               || fcode1 == BUILT_IN_POWF
12078               || fcode1 == BUILT_IN_POWL)
12079             {
12080               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12081               tree arg10 = CALL_EXPR_ARG (arg1, 0);
12082               tree arg11 = CALL_EXPR_ARG (arg1, 1);
12083               tree neg11 = fold_convert_loc (loc, type,
12084                                              negate_expr (arg11));
12085               arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
12086               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
12087             }
12088         }
12089       return NULL_TREE;
12090
12091     case TRUNC_DIV_EXPR:
12092     case FLOOR_DIV_EXPR:
12093       /* Simplify A / (B << N) where A and B are positive and B is
12094          a power of 2, to A >> (N + log2(B)).  */
12095       strict_overflow_p = false;
12096       if (TREE_CODE (arg1) == LSHIFT_EXPR
12097           && (TYPE_UNSIGNED (type)
12098               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12099         {
12100           tree sval = TREE_OPERAND (arg1, 0);
12101           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
12102             {
12103               tree sh_cnt = TREE_OPERAND (arg1, 1);
12104               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
12105
12106               if (strict_overflow_p)
12107                 fold_overflow_warning (("assuming signed overflow does not "
12108                                         "occur when simplifying A / (B << N)"),
12109                                        WARN_STRICT_OVERFLOW_MISC);
12110
12111               sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
12112                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
12113               return fold_build2_loc (loc, RSHIFT_EXPR, type,
12114                                   fold_convert_loc (loc, type, arg0), sh_cnt);
12115             }
12116         }
12117
12118       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
12119          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
12120       if (INTEGRAL_TYPE_P (type)
12121           && TYPE_UNSIGNED (type)
12122           && code == FLOOR_DIV_EXPR)
12123         return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
12124
12125       /* Fall thru */
12126
12127     case ROUND_DIV_EXPR:
12128     case CEIL_DIV_EXPR:
12129     case EXACT_DIV_EXPR:
12130       if (integer_onep (arg1))
12131         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12132       if (integer_zerop (arg1))
12133         return NULL_TREE;
12134       /* X / -1 is -X.  */
12135       if (!TYPE_UNSIGNED (type)
12136           && TREE_CODE (arg1) == INTEGER_CST
12137           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
12138           && TREE_INT_CST_HIGH (arg1) == -1)
12139         return fold_convert_loc (loc, type, negate_expr (arg0));
12140
12141       /* Convert -A / -B to A / B when the type is signed and overflow is
12142          undefined.  */
12143       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12144           && TREE_CODE (arg0) == NEGATE_EXPR
12145           && negate_expr_p (arg1))
12146         {
12147           if (INTEGRAL_TYPE_P (type))
12148             fold_overflow_warning (("assuming signed overflow does not occur "
12149                                     "when distributing negation across "
12150                                     "division"),
12151                                    WARN_STRICT_OVERFLOW_MISC);
12152           return fold_build2_loc (loc, code, type,
12153                               fold_convert_loc (loc, type,
12154                                                 TREE_OPERAND (arg0, 0)),
12155                               fold_convert_loc (loc, type,
12156                                                 negate_expr (arg1)));
12157         }
12158       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12159           && TREE_CODE (arg1) == NEGATE_EXPR
12160           && negate_expr_p (arg0))
12161         {
12162           if (INTEGRAL_TYPE_P (type))
12163             fold_overflow_warning (("assuming signed overflow does not occur "
12164                                     "when distributing negation across "
12165                                     "division"),
12166                                    WARN_STRICT_OVERFLOW_MISC);
12167           return fold_build2_loc (loc, code, type,
12168                               fold_convert_loc (loc, type,
12169                                                 negate_expr (arg0)),
12170                               fold_convert_loc (loc, type,
12171                                                 TREE_OPERAND (arg1, 0)));
12172         }
12173
12174       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12175          operation, EXACT_DIV_EXPR.
12176
12177          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12178          At one time others generated faster code, it's not clear if they do
12179          after the last round to changes to the DIV code in expmed.c.  */
12180       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12181           && multiple_of_p (type, arg0, arg1))
12182         return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
12183
12184       strict_overflow_p = false;
12185       if (TREE_CODE (arg1) == INTEGER_CST
12186           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12187                                          &strict_overflow_p)))
12188         {
12189           if (strict_overflow_p)
12190             fold_overflow_warning (("assuming signed overflow does not occur "
12191                                     "when simplifying division"),
12192                                    WARN_STRICT_OVERFLOW_MISC);
12193           return fold_convert_loc (loc, type, tem);
12194         }
12195
12196       return NULL_TREE;
12197
12198     case CEIL_MOD_EXPR:
12199     case FLOOR_MOD_EXPR:
12200     case ROUND_MOD_EXPR:
12201     case TRUNC_MOD_EXPR:
12202       /* X % 1 is always zero, but be sure to preserve any side
12203          effects in X.  */
12204       if (integer_onep (arg1))
12205         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12206
12207       /* X % 0, return X % 0 unchanged so that we can get the
12208          proper warnings and errors.  */
12209       if (integer_zerop (arg1))
12210         return NULL_TREE;
12211
12212       /* 0 % X is always zero, but be sure to preserve any side
12213          effects in X.  Place this after checking for X == 0.  */
12214       if (integer_zerop (arg0))
12215         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12216
12217       /* X % -1 is zero.  */
12218       if (!TYPE_UNSIGNED (type)
12219           && TREE_CODE (arg1) == INTEGER_CST
12220           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
12221           && TREE_INT_CST_HIGH (arg1) == -1)
12222         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12223
12224       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
12225          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
12226       strict_overflow_p = false;
12227       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
12228           && (TYPE_UNSIGNED (type)
12229               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12230         {
12231           tree c = arg1;
12232           /* Also optimize A % (C << N)  where C is a power of 2,
12233              to A & ((C << N) - 1).  */
12234           if (TREE_CODE (arg1) == LSHIFT_EXPR)
12235             c = TREE_OPERAND (arg1, 0);
12236
12237           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
12238             {
12239               tree mask = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
12240                                        build_int_cst (TREE_TYPE (arg1), 1));
12241               if (strict_overflow_p)
12242                 fold_overflow_warning (("assuming signed overflow does not "
12243                                         "occur when simplifying "
12244                                         "X % (power of two)"),
12245                                        WARN_STRICT_OVERFLOW_MISC);
12246               return fold_build2_loc (loc, BIT_AND_EXPR, type,
12247                                   fold_convert_loc (loc, type, arg0),
12248                                   fold_convert_loc (loc, type, mask));
12249             }
12250         }
12251
12252       /* X % -C is the same as X % C.  */
12253       if (code == TRUNC_MOD_EXPR
12254           && !TYPE_UNSIGNED (type)
12255           && TREE_CODE (arg1) == INTEGER_CST
12256           && !TREE_OVERFLOW (arg1)
12257           && TREE_INT_CST_HIGH (arg1) < 0
12258           && !TYPE_OVERFLOW_TRAPS (type)
12259           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
12260           && !sign_bit_p (arg1, arg1))
12261         return fold_build2_loc (loc, code, type,
12262                             fold_convert_loc (loc, type, arg0),
12263                             fold_convert_loc (loc, type,
12264                                               negate_expr (arg1)));
12265
12266       /* X % -Y is the same as X % Y.  */
12267       if (code == TRUNC_MOD_EXPR
12268           && !TYPE_UNSIGNED (type)
12269           && TREE_CODE (arg1) == NEGATE_EXPR
12270           && !TYPE_OVERFLOW_TRAPS (type))
12271         return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
12272                             fold_convert_loc (loc, type,
12273                                               TREE_OPERAND (arg1, 0)));
12274
12275       if (TREE_CODE (arg1) == INTEGER_CST
12276           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12277                                          &strict_overflow_p)))
12278         {
12279           if (strict_overflow_p)
12280             fold_overflow_warning (("assuming signed overflow does not occur "
12281                                     "when simplifying modulus"),
12282                                    WARN_STRICT_OVERFLOW_MISC);
12283           return fold_convert_loc (loc, type, tem);
12284         }
12285
12286       return NULL_TREE;
12287
12288     case LROTATE_EXPR:
12289     case RROTATE_EXPR:
12290       if (integer_all_onesp (arg0))
12291         return omit_one_operand_loc (loc, type, arg0, arg1);
12292       goto shift;
12293
12294     case RSHIFT_EXPR:
12295       /* Optimize -1 >> x for arithmetic right shifts.  */
12296       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
12297           && tree_expr_nonnegative_p (arg1))
12298         return omit_one_operand_loc (loc, type, arg0, arg1);
12299       /* ... fall through ...  */
12300
12301     case LSHIFT_EXPR:
12302     shift:
12303       if (integer_zerop (arg1))
12304         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12305       if (integer_zerop (arg0))
12306         return omit_one_operand_loc (loc, type, arg0, arg1);
12307
12308       /* Since negative shift count is not well-defined,
12309          don't try to compute it in the compiler.  */
12310       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12311         return NULL_TREE;
12312
12313       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
12314       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
12315           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
12316           && host_integerp (TREE_OPERAND (arg0, 1), false)
12317           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
12318         {
12319           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
12320                                + TREE_INT_CST_LOW (arg1));
12321
12322           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
12323              being well defined.  */
12324           if (low >= TYPE_PRECISION (type))
12325             {
12326               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
12327                 low = low % TYPE_PRECISION (type);
12328               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
12329                 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
12330                                          TREE_OPERAND (arg0, 0));
12331               else
12332                 low = TYPE_PRECISION (type) - 1;
12333             }
12334
12335           return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12336                               build_int_cst (type, low));
12337         }
12338
12339       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
12340          into x & ((unsigned)-1 >> c) for unsigned types.  */
12341       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
12342            || (TYPE_UNSIGNED (type)
12343                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
12344           && host_integerp (arg1, false)
12345           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
12346           && host_integerp (TREE_OPERAND (arg0, 1), false)
12347           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
12348         {
12349           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
12350           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
12351           tree lshift;
12352           tree arg00;
12353
12354           if (low0 == low1)
12355             {
12356               arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12357
12358               lshift = build_int_cst (type, -1);
12359               lshift = int_const_binop (code, lshift, arg1, 0);
12360
12361               return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
12362             }
12363         }
12364
12365       /* Rewrite an LROTATE_EXPR by a constant into an
12366          RROTATE_EXPR by a new constant.  */
12367       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
12368         {
12369           tree tem = build_int_cst (TREE_TYPE (arg1),
12370                                     TYPE_PRECISION (type));
12371           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
12372           return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
12373         }
12374
12375       /* If we have a rotate of a bit operation with the rotate count and
12376          the second operand of the bit operation both constant,
12377          permute the two operations.  */
12378       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12379           && (TREE_CODE (arg0) == BIT_AND_EXPR
12380               || TREE_CODE (arg0) == BIT_IOR_EXPR
12381               || TREE_CODE (arg0) == BIT_XOR_EXPR)
12382           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12383         return fold_build2_loc (loc, TREE_CODE (arg0), type,
12384                             fold_build2_loc (loc, code, type,
12385                                          TREE_OPERAND (arg0, 0), arg1),
12386                             fold_build2_loc (loc, code, type,
12387                                          TREE_OPERAND (arg0, 1), arg1));
12388
12389       /* Two consecutive rotates adding up to the precision of the
12390          type can be ignored.  */
12391       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12392           && TREE_CODE (arg0) == RROTATE_EXPR
12393           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12394           && TREE_INT_CST_HIGH (arg1) == 0
12395           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
12396           && ((TREE_INT_CST_LOW (arg1)
12397                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
12398               == (unsigned int) TYPE_PRECISION (type)))
12399         return TREE_OPERAND (arg0, 0);
12400
12401       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
12402               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
12403          if the latter can be further optimized.  */
12404       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
12405           && TREE_CODE (arg0) == BIT_AND_EXPR
12406           && TREE_CODE (arg1) == INTEGER_CST
12407           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12408         {
12409           tree mask = fold_build2_loc (loc, code, type,
12410                                    fold_convert_loc (loc, type,
12411                                                      TREE_OPERAND (arg0, 1)),
12412                                    arg1);
12413           tree shift = fold_build2_loc (loc, code, type,
12414                                     fold_convert_loc (loc, type,
12415                                                       TREE_OPERAND (arg0, 0)),
12416                                     arg1);
12417           tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
12418           if (tem)
12419             return tem;
12420         }
12421
12422       return NULL_TREE;
12423
12424     case MIN_EXPR:
12425       if (operand_equal_p (arg0, arg1, 0))
12426         return omit_one_operand_loc (loc, type, arg0, arg1);
12427       if (INTEGRAL_TYPE_P (type)
12428           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
12429         return omit_one_operand_loc (loc, type, arg1, arg0);
12430       tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
12431       if (tem)
12432         return tem;
12433       goto associate;
12434
12435     case MAX_EXPR:
12436       if (operand_equal_p (arg0, arg1, 0))
12437         return omit_one_operand_loc (loc, type, arg0, arg1);
12438       if (INTEGRAL_TYPE_P (type)
12439           && TYPE_MAX_VALUE (type)
12440           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
12441         return omit_one_operand_loc (loc, type, arg1, arg0);
12442       tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
12443       if (tem)
12444         return tem;
12445       goto associate;
12446
12447     case TRUTH_ANDIF_EXPR:
12448       /* Note that the operands of this must be ints
12449          and their values must be 0 or 1.
12450          ("true" is a fixed value perhaps depending on the language.)  */
12451       /* If first arg is constant zero, return it.  */
12452       if (integer_zerop (arg0))
12453         return fold_convert_loc (loc, type, arg0);
12454     case TRUTH_AND_EXPR:
12455       /* If either arg is constant true, drop it.  */
12456       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12457         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12458       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12459           /* Preserve sequence points.  */
12460           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12461         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12462       /* If second arg is constant zero, result is zero, but first arg
12463          must be evaluated.  */
12464       if (integer_zerop (arg1))
12465         return omit_one_operand_loc (loc, type, arg1, arg0);
12466       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12467          case will be handled here.  */
12468       if (integer_zerop (arg0))
12469         return omit_one_operand_loc (loc, type, arg0, arg1);
12470
12471       /* !X && X is always false.  */
12472       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12473           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12474         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12475       /* X && !X is always false.  */
12476       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12477           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12478         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12479
12480       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
12481          means A >= Y && A != MAX, but in this case we know that
12482          A < X <= MAX.  */
12483
12484       if (!TREE_SIDE_EFFECTS (arg0)
12485           && !TREE_SIDE_EFFECTS (arg1))
12486         {
12487           tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12488           if (tem && !operand_equal_p (tem, arg0, 0))
12489             return fold_build2_loc (loc, code, type, tem, arg1);
12490
12491           tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12492           if (tem && !operand_equal_p (tem, arg1, 0))
12493             return fold_build2_loc (loc, code, type, arg0, tem);
12494         }
12495
12496     truth_andor:
12497       /* We only do these simplifications if we are optimizing.  */
12498       if (!optimize)
12499         return NULL_TREE;
12500
12501       /* Check for things like (A || B) && (A || C).  We can convert this
12502          to A || (B && C).  Note that either operator can be any of the four
12503          truth and/or operations and the transformation will still be
12504          valid.   Also note that we only care about order for the
12505          ANDIF and ORIF operators.  If B contains side effects, this
12506          might change the truth-value of A.  */
12507       if (TREE_CODE (arg0) == TREE_CODE (arg1)
12508           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12509               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12510               || TREE_CODE (arg0) == TRUTH_AND_EXPR
12511               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12512           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12513         {
12514           tree a00 = TREE_OPERAND (arg0, 0);
12515           tree a01 = TREE_OPERAND (arg0, 1);
12516           tree a10 = TREE_OPERAND (arg1, 0);
12517           tree a11 = TREE_OPERAND (arg1, 1);
12518           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12519                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12520                              && (code == TRUTH_AND_EXPR
12521                                  || code == TRUTH_OR_EXPR));
12522
12523           if (operand_equal_p (a00, a10, 0))
12524             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12525                                 fold_build2_loc (loc, code, type, a01, a11));
12526           else if (commutative && operand_equal_p (a00, a11, 0))
12527             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12528                                 fold_build2_loc (loc, code, type, a01, a10));
12529           else if (commutative && operand_equal_p (a01, a10, 0))
12530             return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
12531                                 fold_build2_loc (loc, code, type, a00, a11));
12532
12533           /* This case if tricky because we must either have commutative
12534              operators or else A10 must not have side-effects.  */
12535
12536           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12537                    && operand_equal_p (a01, a11, 0))
12538             return fold_build2_loc (loc, TREE_CODE (arg0), type,
12539                                 fold_build2_loc (loc, code, type, a00, a10),
12540                                 a01);
12541         }
12542
12543       /* See if we can build a range comparison.  */
12544       if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
12545         return tem;
12546
12547       /* Check for the possibility of merging component references.  If our
12548          lhs is another similar operation, try to merge its rhs with our
12549          rhs.  Then try to merge our lhs and rhs.  */
12550       if (TREE_CODE (arg0) == code
12551           && 0 != (tem = fold_truthop (loc, code, type,
12552                                        TREE_OPERAND (arg0, 1), arg1)))
12553         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12554
12555       if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
12556         return tem;
12557
12558       return NULL_TREE;
12559
12560     case TRUTH_ORIF_EXPR:
12561       /* Note that the operands of this must be ints
12562          and their values must be 0 or true.
12563          ("true" is a fixed value perhaps depending on the language.)  */
12564       /* If first arg is constant true, return it.  */
12565       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12566         return fold_convert_loc (loc, type, arg0);
12567     case TRUTH_OR_EXPR:
12568       /* If either arg is constant zero, drop it.  */
12569       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12570         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12571       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12572           /* Preserve sequence points.  */
12573           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12574         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12575       /* If second arg is constant true, result is true, but we must
12576          evaluate first arg.  */
12577       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12578         return omit_one_operand_loc (loc, type, arg1, arg0);
12579       /* Likewise for first arg, but note this only occurs here for
12580          TRUTH_OR_EXPR.  */
12581       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12582         return omit_one_operand_loc (loc, type, arg0, arg1);
12583
12584       /* !X || X is always true.  */
12585       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12586           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12587         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12588       /* X || !X is always true.  */
12589       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12590           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12591         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12592
12593       goto truth_andor;
12594
12595     case TRUTH_XOR_EXPR:
12596       /* If the second arg is constant zero, drop it.  */
12597       if (integer_zerop (arg1))
12598         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12599       /* If the second arg is constant true, this is a logical inversion.  */
12600       if (integer_onep (arg1))
12601         {
12602           /* Only call invert_truthvalue if operand is a truth value.  */
12603           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12604             tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12605           else
12606             tem = invert_truthvalue_loc (loc, arg0);
12607           return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12608         }
12609       /* Identical arguments cancel to zero.  */
12610       if (operand_equal_p (arg0, arg1, 0))
12611         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12612
12613       /* !X ^ X is always true.  */
12614       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12615           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12616         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12617
12618       /* X ^ !X is always true.  */
12619       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12620           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12621         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12622
12623       return NULL_TREE;
12624
12625     case EQ_EXPR:
12626     case NE_EXPR:
12627       tem = fold_comparison (loc, code, type, op0, op1);
12628       if (tem != NULL_TREE)
12629         return tem;
12630
12631       /* bool_var != 0 becomes bool_var. */
12632       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12633           && code == NE_EXPR)
12634         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12635
12636       /* bool_var == 1 becomes bool_var. */
12637       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12638           && code == EQ_EXPR)
12639         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12640
12641       /* bool_var != 1 becomes !bool_var. */
12642       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12643           && code == NE_EXPR)
12644         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12645                             fold_convert_loc (loc, type, arg0));
12646
12647       /* bool_var == 0 becomes !bool_var. */
12648       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12649           && code == EQ_EXPR)
12650         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12651                             fold_convert_loc (loc, type, arg0));
12652
12653       /* !exp != 0 becomes !exp */
12654       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12655           && code == NE_EXPR)
12656         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12657
12658       /* If this is an equality comparison of the address of two non-weak,
12659          unaliased symbols neither of which are extern (since we do not
12660          have access to attributes for externs), then we know the result.  */
12661       if (TREE_CODE (arg0) == ADDR_EXPR
12662           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12663           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12664           && ! lookup_attribute ("alias",
12665                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12666           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12667           && TREE_CODE (arg1) == ADDR_EXPR
12668           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12669           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12670           && ! lookup_attribute ("alias",
12671                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12672           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12673         {
12674           /* We know that we're looking at the address of two
12675              non-weak, unaliased, static _DECL nodes.
12676
12677              It is both wasteful and incorrect to call operand_equal_p
12678              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12679              all we need to do is test pointer equality for the arguments
12680              to the two ADDR_EXPR nodes.  It is incorrect to use
12681              operand_equal_p as that function is NOT equivalent to a
12682              C equality test.  It can in fact return false for two
12683              objects which would test as equal using the C equality
12684              operator.  */
12685           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12686           return constant_boolean_node (equal
12687                                         ? code == EQ_EXPR : code != EQ_EXPR,
12688                                         type);
12689         }
12690
12691       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12692          a MINUS_EXPR of a constant, we can convert it into a comparison with
12693          a revised constant as long as no overflow occurs.  */
12694       if (TREE_CODE (arg1) == INTEGER_CST
12695           && (TREE_CODE (arg0) == PLUS_EXPR
12696               || TREE_CODE (arg0) == MINUS_EXPR)
12697           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12698           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12699                                       ? MINUS_EXPR : PLUS_EXPR,
12700                                       fold_convert_loc (loc, TREE_TYPE (arg0),
12701                                                         arg1),
12702                                       TREE_OPERAND (arg0, 1), 0))
12703           && !TREE_OVERFLOW (tem))
12704         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12705
12706       /* Similarly for a NEGATE_EXPR.  */
12707       if (TREE_CODE (arg0) == NEGATE_EXPR
12708           && TREE_CODE (arg1) == INTEGER_CST
12709           && 0 != (tem = negate_expr (arg1))
12710           && TREE_CODE (tem) == INTEGER_CST
12711           && !TREE_OVERFLOW (tem))
12712         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12713
12714       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12715       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12716           && TREE_CODE (arg1) == INTEGER_CST
12717           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12718         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12719                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12720                                          fold_convert_loc (loc,
12721                                                            TREE_TYPE (arg0),
12722                                                            arg1),
12723                                          TREE_OPERAND (arg0, 1)));
12724
12725       /* Transform comparisons of the form X +- Y CMP X to Y CMP 0.  */
12726       if ((TREE_CODE (arg0) == PLUS_EXPR
12727            || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12728            || TREE_CODE (arg0) == MINUS_EXPR)
12729           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12730           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12731               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12732         {
12733           tree val = TREE_OPERAND (arg0, 1);
12734           return omit_two_operands_loc (loc, type,
12735                                     fold_build2_loc (loc, code, type,
12736                                                  val,
12737                                                  build_int_cst (TREE_TYPE (val),
12738                                                                 0)),
12739                                     TREE_OPERAND (arg0, 0), arg1);
12740         }
12741
12742       /* Transform comparisons of the form C - X CMP X if C % 2 == 1.  */
12743       if (TREE_CODE (arg0) == MINUS_EXPR
12744           && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12745           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
12746           && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12747         {
12748           return omit_two_operands_loc (loc, type,
12749                                     code == NE_EXPR
12750                                     ? boolean_true_node : boolean_false_node,
12751                                     TREE_OPERAND (arg0, 1), arg1);
12752         }
12753
12754       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12755          for !=.  Don't do this for ordered comparisons due to overflow.  */
12756       if (TREE_CODE (arg0) == MINUS_EXPR
12757           && integer_zerop (arg1))
12758         return fold_build2_loc (loc, code, type,
12759                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12760
12761       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12762       if (TREE_CODE (arg0) == ABS_EXPR
12763           && (integer_zerop (arg1) || real_zerop (arg1)))
12764         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12765
12766       /* If this is an EQ or NE comparison with zero and ARG0 is
12767          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12768          two operations, but the latter can be done in one less insn
12769          on machines that have only two-operand insns or on which a
12770          constant cannot be the first operand.  */
12771       if (TREE_CODE (arg0) == BIT_AND_EXPR
12772           && integer_zerop (arg1))
12773         {
12774           tree arg00 = TREE_OPERAND (arg0, 0);
12775           tree arg01 = TREE_OPERAND (arg0, 1);
12776           if (TREE_CODE (arg00) == LSHIFT_EXPR
12777               && integer_onep (TREE_OPERAND (arg00, 0)))
12778             {
12779               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12780                                       arg01, TREE_OPERAND (arg00, 1));
12781               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12782                                  build_int_cst (TREE_TYPE (arg0), 1));
12783               return fold_build2_loc (loc, code, type,
12784                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12785                                   arg1);
12786             }
12787           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12788                    && integer_onep (TREE_OPERAND (arg01, 0)))
12789             {
12790               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12791                                       arg00, TREE_OPERAND (arg01, 1));
12792               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12793                                  build_int_cst (TREE_TYPE (arg0), 1));
12794               return fold_build2_loc (loc, code, type,
12795                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12796                                   arg1);
12797             }
12798         }
12799
12800       /* If this is an NE or EQ comparison of zero against the result of a
12801          signed MOD operation whose second operand is a power of 2, make
12802          the MOD operation unsigned since it is simpler and equivalent.  */
12803       if (integer_zerop (arg1)
12804           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12805           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12806               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12807               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12808               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12809           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12810         {
12811           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12812           tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12813                                      fold_convert_loc (loc, newtype,
12814                                                        TREE_OPERAND (arg0, 0)),
12815                                      fold_convert_loc (loc, newtype,
12816                                                        TREE_OPERAND (arg0, 1)));
12817
12818           return fold_build2_loc (loc, code, type, newmod,
12819                               fold_convert_loc (loc, newtype, arg1));
12820         }
12821
12822       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12823          C1 is a valid shift constant, and C2 is a power of two, i.e.
12824          a single bit.  */
12825       if (TREE_CODE (arg0) == BIT_AND_EXPR
12826           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12827           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12828              == INTEGER_CST
12829           && integer_pow2p (TREE_OPERAND (arg0, 1))
12830           && integer_zerop (arg1))
12831         {
12832           tree itype = TREE_TYPE (arg0);
12833           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12834           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12835
12836           /* Check for a valid shift count.  */
12837           if (TREE_INT_CST_HIGH (arg001) == 0
12838               && TREE_INT_CST_LOW (arg001) < prec)
12839             {
12840               tree arg01 = TREE_OPERAND (arg0, 1);
12841               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12842               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12843               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12844                  can be rewritten as (X & (C2 << C1)) != 0.  */
12845               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12846                 {
12847                   tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12848                   tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12849                   return fold_build2_loc (loc, code, type, tem, arg1);
12850                 }
12851               /* Otherwise, for signed (arithmetic) shifts,
12852                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12853                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12854               else if (!TYPE_UNSIGNED (itype))
12855                 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12856                                     arg000, build_int_cst (itype, 0));
12857               /* Otherwise, of unsigned (logical) shifts,
12858                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12859                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12860               else
12861                 return omit_one_operand_loc (loc, type,
12862                                          code == EQ_EXPR ? integer_one_node
12863                                                          : integer_zero_node,
12864                                          arg000);
12865             }
12866         }
12867
12868       /* If this is an NE comparison of zero with an AND of one, remove the
12869          comparison since the AND will give the correct value.  */
12870       if (code == NE_EXPR
12871           && integer_zerop (arg1)
12872           && TREE_CODE (arg0) == BIT_AND_EXPR
12873           && integer_onep (TREE_OPERAND (arg0, 1)))
12874         return fold_convert_loc (loc, type, arg0);
12875
12876       /* If we have (A & C) == C where C is a power of 2, convert this into
12877          (A & C) != 0.  Similarly for NE_EXPR.  */
12878       if (TREE_CODE (arg0) == BIT_AND_EXPR
12879           && integer_pow2p (TREE_OPERAND (arg0, 1))
12880           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12881         return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12882                             arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12883                                                     integer_zero_node));
12884
12885       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12886          bit, then fold the expression into A < 0 or A >= 0.  */
12887       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12888       if (tem)
12889         return tem;
12890
12891       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12892          Similarly for NE_EXPR.  */
12893       if (TREE_CODE (arg0) == BIT_AND_EXPR
12894           && TREE_CODE (arg1) == INTEGER_CST
12895           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12896         {
12897           tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12898                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12899                                    TREE_OPERAND (arg0, 1));
12900           tree dandnotc = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12901                                        arg1, notc);
12902           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12903           if (integer_nonzerop (dandnotc))
12904             return omit_one_operand_loc (loc, type, rslt, arg0);
12905         }
12906
12907       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12908          Similarly for NE_EXPR.  */
12909       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12910           && TREE_CODE (arg1) == INTEGER_CST
12911           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12912         {
12913           tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12914           tree candnotd = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12915                                        TREE_OPERAND (arg0, 1), notd);
12916           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12917           if (integer_nonzerop (candnotd))
12918             return omit_one_operand_loc (loc, type, rslt, arg0);
12919         }
12920
12921       /* If this is a comparison of a field, we may be able to simplify it.  */
12922       if ((TREE_CODE (arg0) == COMPONENT_REF
12923            || TREE_CODE (arg0) == BIT_FIELD_REF)
12924           /* Handle the constant case even without -O
12925              to make sure the warnings are given.  */
12926           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12927         {
12928           t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12929           if (t1)
12930             return t1;
12931         }
12932
12933       /* Optimize comparisons of strlen vs zero to a compare of the
12934          first character of the string vs zero.  To wit,
12935                 strlen(ptr) == 0   =>  *ptr == 0
12936                 strlen(ptr) != 0   =>  *ptr != 0
12937          Other cases should reduce to one of these two (or a constant)
12938          due to the return value of strlen being unsigned.  */
12939       if (TREE_CODE (arg0) == CALL_EXPR
12940           && integer_zerop (arg1))
12941         {
12942           tree fndecl = get_callee_fndecl (arg0);
12943
12944           if (fndecl
12945               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12946               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12947               && call_expr_nargs (arg0) == 1
12948               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12949             {
12950               tree iref = build_fold_indirect_ref_loc (loc,
12951                                                    CALL_EXPR_ARG (arg0, 0));
12952               return fold_build2_loc (loc, code, type, iref,
12953                                   build_int_cst (TREE_TYPE (iref), 0));
12954             }
12955         }
12956
12957       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12958          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12959       if (TREE_CODE (arg0) == RSHIFT_EXPR
12960           && integer_zerop (arg1)
12961           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12962         {
12963           tree arg00 = TREE_OPERAND (arg0, 0);
12964           tree arg01 = TREE_OPERAND (arg0, 1);
12965           tree itype = TREE_TYPE (arg00);
12966           if (TREE_INT_CST_HIGH (arg01) == 0
12967               && TREE_INT_CST_LOW (arg01)
12968                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12969             {
12970               if (TYPE_UNSIGNED (itype))
12971                 {
12972                   itype = signed_type_for (itype);
12973                   arg00 = fold_convert_loc (loc, itype, arg00);
12974                 }
12975               return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12976                                   type, arg00, build_int_cst (itype, 0));
12977             }
12978         }
12979
12980       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12981       if (integer_zerop (arg1)
12982           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12983         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12984                             TREE_OPERAND (arg0, 1));
12985
12986       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12987       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12988           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12989         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12990                             build_int_cst (TREE_TYPE (arg1), 0));
12991       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12992       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12993           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12994           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12995         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12996                             build_int_cst (TREE_TYPE (arg1), 0));
12997
12998       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12999       if (TREE_CODE (arg0) == BIT_XOR_EXPR
13000           && TREE_CODE (arg1) == INTEGER_CST
13001           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
13002         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
13003                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
13004                                          TREE_OPERAND (arg0, 1), arg1));
13005
13006       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
13007          (X & C) == 0 when C is a single bit.  */
13008       if (TREE_CODE (arg0) == BIT_AND_EXPR
13009           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
13010           && integer_zerop (arg1)
13011           && integer_pow2p (TREE_OPERAND (arg0, 1)))
13012         {
13013           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
13014                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
13015                              TREE_OPERAND (arg0, 1));
13016           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
13017                               type, tem, arg1);
13018         }
13019
13020       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
13021          constant C is a power of two, i.e. a single bit.  */
13022       if (TREE_CODE (arg0) == BIT_XOR_EXPR
13023           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13024           && integer_zerop (arg1)
13025           && integer_pow2p (TREE_OPERAND (arg0, 1))
13026           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13027                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
13028         {
13029           tree arg00 = TREE_OPERAND (arg0, 0);
13030           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
13031                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
13032         }
13033
13034       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
13035          when is C is a power of two, i.e. a single bit.  */
13036       if (TREE_CODE (arg0) == BIT_AND_EXPR
13037           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
13038           && integer_zerop (arg1)
13039           && integer_pow2p (TREE_OPERAND (arg0, 1))
13040           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13041                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
13042         {
13043           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
13044           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
13045                              arg000, TREE_OPERAND (arg0, 1));
13046           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
13047                               tem, build_int_cst (TREE_TYPE (tem), 0));
13048         }
13049
13050       if (integer_zerop (arg1)
13051           && tree_expr_nonzero_p (arg0))
13052         {
13053           tree res = constant_boolean_node (code==NE_EXPR, type);
13054           return omit_one_operand_loc (loc, type, res, arg0);
13055         }
13056
13057       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
13058       if (TREE_CODE (arg0) == NEGATE_EXPR
13059           && TREE_CODE (arg1) == NEGATE_EXPR)
13060         return fold_build2_loc (loc, code, type,
13061                             TREE_OPERAND (arg0, 0),
13062                             TREE_OPERAND (arg1, 0));
13063
13064       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
13065       if (TREE_CODE (arg0) == BIT_AND_EXPR
13066           && TREE_CODE (arg1) == BIT_AND_EXPR)
13067         {
13068           tree arg00 = TREE_OPERAND (arg0, 0);
13069           tree arg01 = TREE_OPERAND (arg0, 1);
13070           tree arg10 = TREE_OPERAND (arg1, 0);
13071           tree arg11 = TREE_OPERAND (arg1, 1);
13072           tree itype = TREE_TYPE (arg0);
13073
13074           if (operand_equal_p (arg01, arg11, 0))
13075             return fold_build2_loc (loc, code, type,
13076                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13077                                              fold_build2_loc (loc,
13078                                                           BIT_XOR_EXPR, itype,
13079                                                           arg00, arg10),
13080                                              arg01),
13081                                 build_int_cst (itype, 0));
13082
13083           if (operand_equal_p (arg01, arg10, 0))
13084             return fold_build2_loc (loc, code, type,
13085                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13086                                              fold_build2_loc (loc,
13087                                                           BIT_XOR_EXPR, itype,
13088                                                           arg00, arg11),
13089                                              arg01),
13090                                 build_int_cst (itype, 0));
13091
13092           if (operand_equal_p (arg00, arg11, 0))
13093             return fold_build2_loc (loc, code, type,
13094                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13095                                              fold_build2_loc (loc,
13096                                                           BIT_XOR_EXPR, itype,
13097                                                           arg01, arg10),
13098                                              arg00),
13099                                 build_int_cst (itype, 0));
13100
13101           if (operand_equal_p (arg00, arg10, 0))
13102             return fold_build2_loc (loc, code, type,
13103                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13104                                              fold_build2_loc (loc,
13105                                                           BIT_XOR_EXPR, itype,
13106                                                           arg01, arg11),
13107                                              arg00),
13108                                 build_int_cst (itype, 0));
13109         }
13110
13111       if (TREE_CODE (arg0) == BIT_XOR_EXPR
13112           && TREE_CODE (arg1) == BIT_XOR_EXPR)
13113         {
13114           tree arg00 = TREE_OPERAND (arg0, 0);
13115           tree arg01 = TREE_OPERAND (arg0, 1);
13116           tree arg10 = TREE_OPERAND (arg1, 0);
13117           tree arg11 = TREE_OPERAND (arg1, 1);
13118           tree itype = TREE_TYPE (arg0);
13119
13120           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
13121              operand_equal_p guarantees no side-effects so we don't need
13122              to use omit_one_operand on Z.  */
13123           if (operand_equal_p (arg01, arg11, 0))
13124             return fold_build2_loc (loc, code, type, arg00, arg10);
13125           if (operand_equal_p (arg01, arg10, 0))
13126             return fold_build2_loc (loc, code, type, arg00, arg11);
13127           if (operand_equal_p (arg00, arg11, 0))
13128             return fold_build2_loc (loc, code, type, arg01, arg10);
13129           if (operand_equal_p (arg00, arg10, 0))
13130             return fold_build2_loc (loc, code, type, arg01, arg11);
13131
13132           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
13133           if (TREE_CODE (arg01) == INTEGER_CST
13134               && TREE_CODE (arg11) == INTEGER_CST)
13135             return fold_build2_loc (loc, code, type,
13136                                 fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00,
13137                                              fold_build2_loc (loc,
13138                                                           BIT_XOR_EXPR, itype,
13139                                                           arg01, arg11)),
13140                                 arg10);
13141         }
13142
13143       /* Attempt to simplify equality/inequality comparisons of complex
13144          values.  Only lower the comparison if the result is known or
13145          can be simplified to a single scalar comparison.  */
13146       if ((TREE_CODE (arg0) == COMPLEX_EXPR
13147            || TREE_CODE (arg0) == COMPLEX_CST)
13148           && (TREE_CODE (arg1) == COMPLEX_EXPR
13149               || TREE_CODE (arg1) == COMPLEX_CST))
13150         {
13151           tree real0, imag0, real1, imag1;
13152           tree rcond, icond;
13153
13154           if (TREE_CODE (arg0) == COMPLEX_EXPR)
13155             {
13156               real0 = TREE_OPERAND (arg0, 0);
13157               imag0 = TREE_OPERAND (arg0, 1);
13158             }
13159           else
13160             {
13161               real0 = TREE_REALPART (arg0);
13162               imag0 = TREE_IMAGPART (arg0);
13163             }
13164
13165           if (TREE_CODE (arg1) == COMPLEX_EXPR)
13166             {
13167               real1 = TREE_OPERAND (arg1, 0);
13168               imag1 = TREE_OPERAND (arg1, 1);
13169             }
13170           else
13171             {
13172               real1 = TREE_REALPART (arg1);
13173               imag1 = TREE_IMAGPART (arg1);
13174             }
13175
13176           rcond = fold_binary_loc (loc, code, type, real0, real1);
13177           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
13178             {
13179               if (integer_zerop (rcond))
13180                 {
13181                   if (code == EQ_EXPR)
13182                     return omit_two_operands_loc (loc, type, boolean_false_node,
13183                                               imag0, imag1);
13184                   return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
13185                 }
13186               else
13187                 {
13188                   if (code == NE_EXPR)
13189                     return omit_two_operands_loc (loc, type, boolean_true_node,
13190                                               imag0, imag1);
13191                   return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
13192                 }
13193             }
13194
13195           icond = fold_binary_loc (loc, code, type, imag0, imag1);
13196           if (icond && TREE_CODE (icond) == INTEGER_CST)
13197             {
13198               if (integer_zerop (icond))
13199                 {
13200                   if (code == EQ_EXPR)
13201                     return omit_two_operands_loc (loc, type, boolean_false_node,
13202                                               real0, real1);
13203                   return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
13204                 }
13205               else
13206                 {
13207                   if (code == NE_EXPR)
13208                     return omit_two_operands_loc (loc, type, boolean_true_node,
13209                                               real0, real1);
13210                   return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
13211                 }
13212             }
13213         }
13214
13215       return NULL_TREE;
13216
13217     case LT_EXPR:
13218     case GT_EXPR:
13219     case LE_EXPR:
13220     case GE_EXPR:
13221       tem = fold_comparison (loc, code, type, op0, op1);
13222       if (tem != NULL_TREE)
13223         return tem;
13224
13225       /* Transform comparisons of the form X +- C CMP X.  */
13226       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
13227           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
13228           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
13229                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
13230               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
13231                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
13232         {
13233           tree arg01 = TREE_OPERAND (arg0, 1);
13234           enum tree_code code0 = TREE_CODE (arg0);
13235           int is_positive;
13236
13237           if (TREE_CODE (arg01) == REAL_CST)
13238             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
13239           else
13240             is_positive = tree_int_cst_sgn (arg01);
13241
13242           /* (X - c) > X becomes false.  */
13243           if (code == GT_EXPR
13244               && ((code0 == MINUS_EXPR && is_positive >= 0)
13245                   || (code0 == PLUS_EXPR && is_positive <= 0)))
13246             {
13247               if (TREE_CODE (arg01) == INTEGER_CST
13248                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13249                 fold_overflow_warning (("assuming signed overflow does not "
13250                                         "occur when assuming that (X - c) > X "
13251                                         "is always false"),
13252                                        WARN_STRICT_OVERFLOW_ALL);
13253               return constant_boolean_node (0, type);
13254             }
13255
13256           /* Likewise (X + c) < X becomes false.  */
13257           if (code == LT_EXPR
13258               && ((code0 == PLUS_EXPR && is_positive >= 0)
13259                   || (code0 == MINUS_EXPR && is_positive <= 0)))
13260             {
13261               if (TREE_CODE (arg01) == INTEGER_CST
13262                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13263                 fold_overflow_warning (("assuming signed overflow does not "
13264                                         "occur when assuming that "
13265                                         "(X + c) < X is always false"),
13266                                        WARN_STRICT_OVERFLOW_ALL);
13267               return constant_boolean_node (0, type);
13268             }
13269
13270           /* Convert (X - c) <= X to true.  */
13271           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13272               && code == LE_EXPR
13273               && ((code0 == MINUS_EXPR && is_positive >= 0)
13274                   || (code0 == PLUS_EXPR && is_positive <= 0)))
13275             {
13276               if (TREE_CODE (arg01) == INTEGER_CST
13277                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13278                 fold_overflow_warning (("assuming signed overflow does not "
13279                                         "occur when assuming that "
13280                                         "(X - c) <= X is always true"),
13281                                        WARN_STRICT_OVERFLOW_ALL);
13282               return constant_boolean_node (1, type);
13283             }
13284
13285           /* Convert (X + c) >= X to true.  */
13286           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13287               && code == GE_EXPR
13288               && ((code0 == PLUS_EXPR && is_positive >= 0)
13289                   || (code0 == MINUS_EXPR && is_positive <= 0)))
13290             {
13291               if (TREE_CODE (arg01) == INTEGER_CST
13292                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13293                 fold_overflow_warning (("assuming signed overflow does not "
13294                                         "occur when assuming that "
13295                                         "(X + c) >= X is always true"),
13296                                        WARN_STRICT_OVERFLOW_ALL);
13297               return constant_boolean_node (1, type);
13298             }
13299
13300           if (TREE_CODE (arg01) == INTEGER_CST)
13301             {
13302               /* Convert X + c > X and X - c < X to true for integers.  */
13303               if (code == GT_EXPR
13304                   && ((code0 == PLUS_EXPR && is_positive > 0)
13305                       || (code0 == MINUS_EXPR && is_positive < 0)))
13306                 {
13307                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13308                     fold_overflow_warning (("assuming signed overflow does "
13309                                             "not occur when assuming that "
13310                                             "(X + c) > X is always true"),
13311                                            WARN_STRICT_OVERFLOW_ALL);
13312                   return constant_boolean_node (1, type);
13313                 }
13314
13315               if (code == LT_EXPR
13316                   && ((code0 == MINUS_EXPR && is_positive > 0)
13317                       || (code0 == PLUS_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 true"),
13323                                            WARN_STRICT_OVERFLOW_ALL);
13324                   return constant_boolean_node (1, type);
13325                 }
13326
13327               /* Convert X + c <= X and X - c >= X to false for integers.  */
13328               if (code == LE_EXPR
13329                   && ((code0 == PLUS_EXPR && is_positive > 0)
13330                       || (code0 == MINUS_EXPR && is_positive < 0)))
13331                 {
13332                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13333                     fold_overflow_warning (("assuming signed overflow does "
13334                                             "not occur when assuming that "
13335                                             "(X + c) <= X is always false"),
13336                                            WARN_STRICT_OVERFLOW_ALL);
13337                   return constant_boolean_node (0, type);
13338                 }
13339
13340               if (code == GE_EXPR
13341                   && ((code0 == MINUS_EXPR && is_positive > 0)
13342                       || (code0 == PLUS_EXPR && is_positive < 0)))
13343                 {
13344                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13345                     fold_overflow_warning (("assuming signed overflow does "
13346                                             "not occur when assuming that "
13347                                             "(X - c) >= X is always false"),
13348                                            WARN_STRICT_OVERFLOW_ALL);
13349                   return constant_boolean_node (0, type);
13350                 }
13351             }
13352         }
13353
13354       /* Comparisons with the highest or lowest possible integer of
13355          the specified precision will have known values.  */
13356       {
13357         tree arg1_type = TREE_TYPE (arg1);
13358         unsigned int width = TYPE_PRECISION (arg1_type);
13359
13360         if (TREE_CODE (arg1) == INTEGER_CST
13361             && width <= 2 * HOST_BITS_PER_WIDE_INT
13362             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
13363           {
13364             HOST_WIDE_INT signed_max_hi;
13365             unsigned HOST_WIDE_INT signed_max_lo;
13366             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
13367
13368             if (width <= HOST_BITS_PER_WIDE_INT)
13369               {
13370                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13371                                 - 1;
13372                 signed_max_hi = 0;
13373                 max_hi = 0;
13374
13375                 if (TYPE_UNSIGNED (arg1_type))
13376                   {
13377                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13378                     min_lo = 0;
13379                     min_hi = 0;
13380                   }
13381                 else
13382                   {
13383                     max_lo = signed_max_lo;
13384                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
13385                     min_hi = -1;
13386                   }
13387               }
13388             else
13389               {
13390                 width -= HOST_BITS_PER_WIDE_INT;
13391                 signed_max_lo = -1;
13392                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13393                                 - 1;
13394                 max_lo = -1;
13395                 min_lo = 0;
13396
13397                 if (TYPE_UNSIGNED (arg1_type))
13398                   {
13399                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13400                     min_hi = 0;
13401                   }
13402                 else
13403                   {
13404                     max_hi = signed_max_hi;
13405                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
13406                   }
13407               }
13408
13409             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
13410                 && TREE_INT_CST_LOW (arg1) == max_lo)
13411               switch (code)
13412                 {
13413                 case GT_EXPR:
13414                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13415
13416                 case GE_EXPR:
13417                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13418
13419                 case LE_EXPR:
13420                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13421
13422                 case LT_EXPR:
13423                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13424
13425                 /* The GE_EXPR and LT_EXPR cases above are not normally
13426                    reached because of previous transformations.  */
13427
13428                 default:
13429                   break;
13430                 }
13431             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13432                      == max_hi
13433                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
13434               switch (code)
13435                 {
13436                 case GT_EXPR:
13437                   arg1 = const_binop (PLUS_EXPR, arg1,
13438                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
13439                   return fold_build2_loc (loc, EQ_EXPR, type,
13440                                       fold_convert_loc (loc,
13441                                                         TREE_TYPE (arg1), arg0),
13442                                       arg1);
13443                 case LE_EXPR:
13444                   arg1 = const_binop (PLUS_EXPR, arg1,
13445                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
13446                   return fold_build2_loc (loc, NE_EXPR, type,
13447                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13448                                                         arg0),
13449                                       arg1);
13450                 default:
13451                   break;
13452                 }
13453             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13454                      == min_hi
13455                      && TREE_INT_CST_LOW (arg1) == min_lo)
13456               switch (code)
13457                 {
13458                 case LT_EXPR:
13459                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13460
13461                 case LE_EXPR:
13462                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13463
13464                 case GE_EXPR:
13465                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13466
13467                 case GT_EXPR:
13468                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13469
13470                 default:
13471                   break;
13472                 }
13473             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13474                      == min_hi
13475                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
13476               switch (code)
13477                 {
13478                 case GE_EXPR:
13479                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13480                   return fold_build2_loc (loc, NE_EXPR, type,
13481                                       fold_convert_loc (loc,
13482                                                         TREE_TYPE (arg1), arg0),
13483                                       arg1);
13484                 case LT_EXPR:
13485                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13486                   return fold_build2_loc (loc, EQ_EXPR, type,
13487                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13488                                                         arg0),
13489                                       arg1);
13490                 default:
13491                   break;
13492                 }
13493
13494             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13495                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
13496                      && TYPE_UNSIGNED (arg1_type)
13497                      /* We will flip the signedness of the comparison operator
13498                         associated with the mode of arg1, so the sign bit is
13499                         specified by this mode.  Check that arg1 is the signed
13500                         max associated with this sign bit.  */
13501                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13502                      /* signed_type does not work on pointer types.  */
13503                      && INTEGRAL_TYPE_P (arg1_type))
13504               {
13505                 /* The following case also applies to X < signed_max+1
13506                    and X >= signed_max+1 because previous transformations.  */
13507                 if (code == LE_EXPR || code == GT_EXPR)
13508                   {
13509                     tree st;
13510                     st = signed_type_for (TREE_TYPE (arg1));
13511                     return fold_build2_loc (loc,
13512                                         code == LE_EXPR ? GE_EXPR : LT_EXPR,
13513                                         type, fold_convert_loc (loc, st, arg0),
13514                                         build_int_cst (st, 0));
13515                   }
13516               }
13517           }
13518       }
13519
13520       /* If we are comparing an ABS_EXPR with a constant, we can
13521          convert all the cases into explicit comparisons, but they may
13522          well not be faster than doing the ABS and one comparison.
13523          But ABS (X) <= C is a range comparison, which becomes a subtraction
13524          and a comparison, and is probably faster.  */
13525       if (code == LE_EXPR
13526           && TREE_CODE (arg1) == INTEGER_CST
13527           && TREE_CODE (arg0) == ABS_EXPR
13528           && ! TREE_SIDE_EFFECTS (arg0)
13529           && (0 != (tem = negate_expr (arg1)))
13530           && TREE_CODE (tem) == INTEGER_CST
13531           && !TREE_OVERFLOW (tem))
13532         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13533                             build2 (GE_EXPR, type,
13534                                     TREE_OPERAND (arg0, 0), tem),
13535                             build2 (LE_EXPR, type,
13536                                     TREE_OPERAND (arg0, 0), arg1));
13537
13538       /* Convert ABS_EXPR<x> >= 0 to true.  */
13539       strict_overflow_p = false;
13540       if (code == GE_EXPR
13541           && (integer_zerop (arg1)
13542               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13543                   && real_zerop (arg1)))
13544           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13545         {
13546           if (strict_overflow_p)
13547             fold_overflow_warning (("assuming signed overflow does not occur "
13548                                     "when simplifying comparison of "
13549                                     "absolute value and zero"),
13550                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13551           return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13552         }
13553
13554       /* Convert ABS_EXPR<x> < 0 to false.  */
13555       strict_overflow_p = false;
13556       if (code == LT_EXPR
13557           && (integer_zerop (arg1) || real_zerop (arg1))
13558           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13559         {
13560           if (strict_overflow_p)
13561             fold_overflow_warning (("assuming signed overflow does not occur "
13562                                     "when simplifying comparison of "
13563                                     "absolute value and zero"),
13564                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13565           return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13566         }
13567
13568       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13569          and similarly for >= into !=.  */
13570       if ((code == LT_EXPR || code == GE_EXPR)
13571           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13572           && TREE_CODE (arg1) == LSHIFT_EXPR
13573           && integer_onep (TREE_OPERAND (arg1, 0)))
13574         {
13575           tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13576                         build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13577                                 TREE_OPERAND (arg1, 1)),
13578                         build_int_cst (TREE_TYPE (arg0), 0));
13579           goto fold_binary_exit;
13580         }
13581
13582       if ((code == LT_EXPR || code == GE_EXPR)
13583           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13584           && CONVERT_EXPR_P (arg1)
13585           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13586           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13587         {
13588           tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13589                         fold_convert_loc (loc, TREE_TYPE (arg0),
13590                                           build2 (RSHIFT_EXPR,
13591                                                   TREE_TYPE (arg0), arg0,
13592                                                   TREE_OPERAND (TREE_OPERAND (arg1, 0),
13593                                                                 1))),
13594                         build_int_cst (TREE_TYPE (arg0), 0));
13595           goto fold_binary_exit;
13596         }
13597
13598       return NULL_TREE;
13599
13600     case UNORDERED_EXPR:
13601     case ORDERED_EXPR:
13602     case UNLT_EXPR:
13603     case UNLE_EXPR:
13604     case UNGT_EXPR:
13605     case UNGE_EXPR:
13606     case UNEQ_EXPR:
13607     case LTGT_EXPR:
13608       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13609         {
13610           t1 = fold_relational_const (code, type, arg0, arg1);
13611           if (t1 != NULL_TREE)
13612             return t1;
13613         }
13614
13615       /* If the first operand is NaN, the result is constant.  */
13616       if (TREE_CODE (arg0) == REAL_CST
13617           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13618           && (code != LTGT_EXPR || ! flag_trapping_math))
13619         {
13620           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13621                ? integer_zero_node
13622                : integer_one_node;
13623           return omit_one_operand_loc (loc, type, t1, arg1);
13624         }
13625
13626       /* If the second operand is NaN, the result is constant.  */
13627       if (TREE_CODE (arg1) == REAL_CST
13628           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13629           && (code != LTGT_EXPR || ! flag_trapping_math))
13630         {
13631           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13632                ? integer_zero_node
13633                : integer_one_node;
13634           return omit_one_operand_loc (loc, type, t1, arg0);
13635         }
13636
13637       /* Simplify unordered comparison of something with itself.  */
13638       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13639           && operand_equal_p (arg0, arg1, 0))
13640         return constant_boolean_node (1, type);
13641
13642       if (code == LTGT_EXPR
13643           && !flag_trapping_math
13644           && operand_equal_p (arg0, arg1, 0))
13645         return constant_boolean_node (0, type);
13646
13647       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13648       {
13649         tree targ0 = strip_float_extensions (arg0);
13650         tree targ1 = strip_float_extensions (arg1);
13651         tree newtype = TREE_TYPE (targ0);
13652
13653         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13654           newtype = TREE_TYPE (targ1);
13655
13656         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13657           return fold_build2_loc (loc, code, type,
13658                               fold_convert_loc (loc, newtype, targ0),
13659                               fold_convert_loc (loc, newtype, targ1));
13660       }
13661
13662       return NULL_TREE;
13663
13664     case COMPOUND_EXPR:
13665       /* When pedantic, a compound expression can be neither an lvalue
13666          nor an integer constant expression.  */
13667       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13668         return NULL_TREE;
13669       /* Don't let (0, 0) be null pointer constant.  */
13670       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13671                                  : fold_convert_loc (loc, type, arg1);
13672       return pedantic_non_lvalue_loc (loc, tem);
13673
13674     case COMPLEX_EXPR:
13675       if ((TREE_CODE (arg0) == REAL_CST
13676            && TREE_CODE (arg1) == REAL_CST)
13677           || (TREE_CODE (arg0) == INTEGER_CST
13678               && TREE_CODE (arg1) == INTEGER_CST))
13679         return build_complex (type, arg0, arg1);
13680       return NULL_TREE;
13681
13682     case ASSERT_EXPR:
13683       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13684       gcc_unreachable ();
13685
13686     default:
13687       return NULL_TREE;
13688     } /* switch (code) */
13689  fold_binary_exit:
13690   protected_set_expr_location (tem, loc);
13691   return tem;
13692 }
13693
13694 /* Callback for walk_tree, looking for LABEL_EXPR.  Return *TP if it is
13695    a LABEL_EXPR; otherwise return NULL_TREE.  Do not check the subtrees
13696    of GOTO_EXPR.  */
13697
13698 static tree
13699 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13700 {
13701   switch (TREE_CODE (*tp))
13702     {
13703     case LABEL_EXPR:
13704       return *tp;
13705
13706     case GOTO_EXPR:
13707       *walk_subtrees = 0;
13708
13709       /* ... fall through ...  */
13710
13711     default:
13712       return NULL_TREE;
13713     }
13714 }
13715
13716 /* Return whether the sub-tree ST contains a label which is accessible from
13717    outside the sub-tree.  */
13718
13719 static bool
13720 contains_label_p (tree st)
13721 {
13722   return
13723    (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13724 }
13725
13726 /* Fold a ternary expression of code CODE and type TYPE with operands
13727    OP0, OP1, and OP2.  Return the folded expression if folding is
13728    successful.  Otherwise, return NULL_TREE.  */
13729
13730 tree
13731 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13732               tree op0, tree op1, tree op2)
13733 {
13734   tree tem;
13735   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13736   enum tree_code_class kind = TREE_CODE_CLASS (code);
13737
13738   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13739               && TREE_CODE_LENGTH (code) == 3);
13740
13741   /* Strip any conversions that don't change the mode.  This is safe
13742      for every expression, except for a comparison expression because
13743      its signedness is derived from its operands.  So, in the latter
13744      case, only strip conversions that don't change the signedness.
13745
13746      Note that this is done as an internal manipulation within the
13747      constant folder, in order to find the simplest representation of
13748      the arguments so that their form can be studied.  In any cases,
13749      the appropriate type conversions should be put back in the tree
13750      that will get out of the constant folder.  */
13751   if (op0)
13752     {
13753       arg0 = op0;
13754       STRIP_NOPS (arg0);
13755     }
13756
13757   if (op1)
13758     {
13759       arg1 = op1;
13760       STRIP_NOPS (arg1);
13761     }
13762
13763   switch (code)
13764     {
13765     case COMPONENT_REF:
13766       if (TREE_CODE (arg0) == CONSTRUCTOR
13767           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13768         {
13769           unsigned HOST_WIDE_INT idx;
13770           tree field, value;
13771           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13772             if (field == arg1)
13773               return value;
13774         }
13775       return NULL_TREE;
13776
13777     case COND_EXPR:
13778       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13779          so all simple results must be passed through pedantic_non_lvalue.  */
13780       if (TREE_CODE (arg0) == INTEGER_CST)
13781         {
13782           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13783           tem = integer_zerop (arg0) ? op2 : op1;
13784           /* Only optimize constant conditions when the selected branch
13785              has the same type as the COND_EXPR.  This avoids optimizing
13786              away "c ? x : throw", where the throw has a void type.
13787              Avoid throwing away that operand which contains label.  */
13788           if ((!TREE_SIDE_EFFECTS (unused_op)
13789                || !contains_label_p (unused_op))
13790               && (! VOID_TYPE_P (TREE_TYPE (tem))
13791                   || VOID_TYPE_P (type)))
13792             return pedantic_non_lvalue_loc (loc, tem);
13793           return NULL_TREE;
13794         }
13795       if (operand_equal_p (arg1, op2, 0))
13796         return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13797
13798       /* If we have A op B ? A : C, we may be able to convert this to a
13799          simpler expression, depending on the operation and the values
13800          of B and C.  Signed zeros prevent all of these transformations,
13801          for reasons given above each one.
13802
13803          Also try swapping the arguments and inverting the conditional.  */
13804       if (COMPARISON_CLASS_P (arg0)
13805           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13806                                              arg1, TREE_OPERAND (arg0, 1))
13807           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13808         {
13809           tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13810           if (tem)
13811             return tem;
13812         }
13813
13814       if (COMPARISON_CLASS_P (arg0)
13815           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13816                                              op2,
13817                                              TREE_OPERAND (arg0, 1))
13818           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13819         {
13820           tem = fold_truth_not_expr (loc, arg0);
13821           if (tem && COMPARISON_CLASS_P (tem))
13822             {
13823               tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13824               if (tem)
13825                 return tem;
13826             }
13827         }
13828
13829       /* If the second operand is simpler than the third, swap them
13830          since that produces better jump optimization results.  */
13831       if (truth_value_p (TREE_CODE (arg0))
13832           && tree_swap_operands_p (op1, op2, false))
13833         {
13834           /* See if this can be inverted.  If it can't, possibly because
13835              it was a floating-point inequality comparison, don't do
13836              anything.  */
13837           tem = fold_truth_not_expr (loc, arg0);
13838           if (tem)
13839             return fold_build3_loc (loc, code, type, tem, op2, op1);
13840         }
13841
13842       /* Convert A ? 1 : 0 to simply A.  */
13843       if (integer_onep (op1)
13844           && integer_zerop (op2)
13845           /* If we try to convert OP0 to our type, the
13846              call to fold will try to move the conversion inside
13847              a COND, which will recurse.  In that case, the COND_EXPR
13848              is probably the best choice, so leave it alone.  */
13849           && type == TREE_TYPE (arg0))
13850         return pedantic_non_lvalue_loc (loc, arg0);
13851
13852       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13853          over COND_EXPR in cases such as floating point comparisons.  */
13854       if (integer_zerop (op1)
13855           && integer_onep (op2)
13856           && truth_value_p (TREE_CODE (arg0)))
13857         return pedantic_non_lvalue_loc (loc,
13858                                     fold_convert_loc (loc, type,
13859                                               invert_truthvalue_loc (loc,
13860                                                                      arg0)));
13861
13862       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13863       if (TREE_CODE (arg0) == LT_EXPR
13864           && integer_zerop (TREE_OPERAND (arg0, 1))
13865           && integer_zerop (op2)
13866           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13867         {
13868           /* sign_bit_p only checks ARG1 bits within A's precision.
13869              If <sign bit of A> has wider type than A, bits outside
13870              of A's precision in <sign bit of A> need to be checked.
13871              If they are all 0, this optimization needs to be done
13872              in unsigned A's type, if they are all 1 in signed A's type,
13873              otherwise this can't be done.  */
13874           if (TYPE_PRECISION (TREE_TYPE (tem))
13875               < TYPE_PRECISION (TREE_TYPE (arg1))
13876               && TYPE_PRECISION (TREE_TYPE (tem))
13877                  < TYPE_PRECISION (type))
13878             {
13879               unsigned HOST_WIDE_INT mask_lo;
13880               HOST_WIDE_INT mask_hi;
13881               int inner_width, outer_width;
13882               tree tem_type;
13883
13884               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13885               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13886               if (outer_width > TYPE_PRECISION (type))
13887                 outer_width = TYPE_PRECISION (type);
13888
13889               if (outer_width > HOST_BITS_PER_WIDE_INT)
13890                 {
13891                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13892                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13893                   mask_lo = -1;
13894                 }
13895               else
13896                 {
13897                   mask_hi = 0;
13898                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13899                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13900                 }
13901               if (inner_width > HOST_BITS_PER_WIDE_INT)
13902                 {
13903                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13904                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13905                   mask_lo = 0;
13906                 }
13907               else
13908                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13909                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13910
13911               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13912                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13913                 {
13914                   tem_type = signed_type_for (TREE_TYPE (tem));
13915                   tem = fold_convert_loc (loc, tem_type, tem);
13916                 }
13917               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13918                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13919                 {
13920                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13921                   tem = fold_convert_loc (loc, tem_type, tem);
13922                 }
13923               else
13924                 tem = NULL;
13925             }
13926
13927           if (tem)
13928             return
13929               fold_convert_loc (loc, type,
13930                                 fold_build2_loc (loc, BIT_AND_EXPR,
13931                                              TREE_TYPE (tem), tem,
13932                                              fold_convert_loc (loc,
13933                                                                TREE_TYPE (tem),
13934                                                                arg1)));
13935         }
13936
13937       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13938          already handled above.  */
13939       if (TREE_CODE (arg0) == BIT_AND_EXPR
13940           && integer_onep (TREE_OPERAND (arg0, 1))
13941           && integer_zerop (op2)
13942           && integer_pow2p (arg1))
13943         {
13944           tree tem = TREE_OPERAND (arg0, 0);
13945           STRIP_NOPS (tem);
13946           if (TREE_CODE (tem) == RSHIFT_EXPR
13947               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13948               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13949                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13950             return fold_build2_loc (loc, BIT_AND_EXPR, type,
13951                                 TREE_OPERAND (tem, 0), arg1);
13952         }
13953
13954       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13955          is probably obsolete because the first operand should be a
13956          truth value (that's why we have the two cases above), but let's
13957          leave it in until we can confirm this for all front-ends.  */
13958       if (integer_zerop (op2)
13959           && TREE_CODE (arg0) == NE_EXPR
13960           && integer_zerop (TREE_OPERAND (arg0, 1))
13961           && integer_pow2p (arg1)
13962           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13963           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13964                               arg1, OEP_ONLY_CONST))
13965         return pedantic_non_lvalue_loc (loc,
13966                                     fold_convert_loc (loc, type,
13967                                                       TREE_OPERAND (arg0, 0)));
13968
13969       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13970       if (integer_zerop (op2)
13971           && truth_value_p (TREE_CODE (arg0))
13972           && truth_value_p (TREE_CODE (arg1)))
13973         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13974                             fold_convert_loc (loc, type, arg0),
13975                             arg1);
13976
13977       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13978       if (integer_onep (op2)
13979           && truth_value_p (TREE_CODE (arg0))
13980           && truth_value_p (TREE_CODE (arg1)))
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_ORIF_EXPR, type,
13986                                 fold_convert_loc (loc, type, tem),
13987                                 arg1);
13988         }
13989
13990       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13991       if (integer_zerop (arg1)
13992           && truth_value_p (TREE_CODE (arg0))
13993           && truth_value_p (TREE_CODE (op2)))
13994         {
13995           /* Only perform transformation if ARG0 is easily inverted.  */
13996           tem = fold_truth_not_expr (loc, arg0);
13997           if (tem)
13998             return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13999                                 fold_convert_loc (loc, type, tem),
14000                                 op2);
14001         }
14002
14003       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
14004       if (integer_onep (arg1)
14005           && truth_value_p (TREE_CODE (arg0))
14006           && truth_value_p (TREE_CODE (op2)))
14007         return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
14008                             fold_convert_loc (loc, type, arg0),
14009                             op2);
14010
14011       return NULL_TREE;
14012
14013     case CALL_EXPR:
14014       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
14015          of fold_ternary on them.  */
14016       gcc_unreachable ();
14017
14018     case BIT_FIELD_REF:
14019       if ((TREE_CODE (arg0) == VECTOR_CST
14020            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
14021           && type == TREE_TYPE (TREE_TYPE (arg0)))
14022         {
14023           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
14024           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
14025
14026           if (width != 0
14027               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
14028               && (idx % width) == 0
14029               && (idx = idx / width)
14030                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
14031             {
14032               tree elements = NULL_TREE;
14033
14034               if (TREE_CODE (arg0) == VECTOR_CST)
14035                 elements = TREE_VECTOR_CST_ELTS (arg0);
14036               else
14037                 {
14038                   unsigned HOST_WIDE_INT idx;
14039                   tree value;
14040
14041                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
14042                     elements = tree_cons (NULL_TREE, value, elements);
14043                 }
14044               while (idx-- > 0 && elements)
14045                 elements = TREE_CHAIN (elements);
14046               if (elements)
14047                 return TREE_VALUE (elements);
14048               else
14049                 return fold_convert_loc (loc, type, integer_zero_node);
14050             }
14051         }
14052
14053       /* A bit-field-ref that referenced the full argument can be stripped.  */
14054       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
14055           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
14056           && integer_zerop (op2))
14057         return fold_convert_loc (loc, type, arg0);
14058
14059       return NULL_TREE;
14060
14061     default:
14062       return NULL_TREE;
14063     } /* switch (code) */
14064 }
14065
14066 /* Perform constant folding and related simplification of EXPR.
14067    The related simplifications include x*1 => x, x*0 => 0, etc.,
14068    and application of the associative law.
14069    NOP_EXPR conversions may be removed freely (as long as we
14070    are careful not to change the type of the overall expression).
14071    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
14072    but we can constant-fold them if they have constant operands.  */
14073
14074 #ifdef ENABLE_FOLD_CHECKING
14075 # define fold(x) fold_1 (x)
14076 static tree fold_1 (tree);
14077 static
14078 #endif
14079 tree
14080 fold (tree expr)
14081 {
14082   const tree t = expr;
14083   enum tree_code code = TREE_CODE (t);
14084   enum tree_code_class kind = TREE_CODE_CLASS (code);
14085   tree tem;
14086   location_t loc = EXPR_LOCATION (expr);
14087
14088   /* Return right away if a constant.  */
14089   if (kind == tcc_constant)
14090     return t;
14091
14092   /* CALL_EXPR-like objects with variable numbers of operands are
14093      treated specially.  */
14094   if (kind == tcc_vl_exp)
14095     {
14096       if (code == CALL_EXPR)
14097         {
14098           tem = fold_call_expr (loc, expr, false);
14099           return tem ? tem : expr;
14100         }
14101       return expr;
14102     }
14103
14104   if (IS_EXPR_CODE_CLASS (kind))
14105     {
14106       tree type = TREE_TYPE (t);
14107       tree op0, op1, op2;
14108
14109       switch (TREE_CODE_LENGTH (code))
14110         {
14111         case 1:
14112           op0 = TREE_OPERAND (t, 0);
14113           tem = fold_unary_loc (loc, code, type, op0);
14114           return tem ? tem : expr;
14115         case 2:
14116           op0 = TREE_OPERAND (t, 0);
14117           op1 = TREE_OPERAND (t, 1);
14118           tem = fold_binary_loc (loc, code, type, op0, op1);
14119           return tem ? tem : expr;
14120         case 3:
14121           op0 = TREE_OPERAND (t, 0);
14122           op1 = TREE_OPERAND (t, 1);
14123           op2 = TREE_OPERAND (t, 2);
14124           tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14125           return tem ? tem : expr;
14126         default:
14127           break;
14128         }
14129     }
14130
14131   switch (code)
14132     {
14133     case ARRAY_REF:
14134       {
14135         tree op0 = TREE_OPERAND (t, 0);
14136         tree op1 = TREE_OPERAND (t, 1);
14137
14138         if (TREE_CODE (op1) == INTEGER_CST
14139             && TREE_CODE (op0) == CONSTRUCTOR
14140             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
14141           {
14142             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
14143             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
14144             unsigned HOST_WIDE_INT begin = 0;
14145
14146             /* Find a matching index by means of a binary search.  */
14147             while (begin != end)
14148               {
14149                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
14150                 tree index = VEC_index (constructor_elt, elts, middle)->index;
14151
14152                 if (TREE_CODE (index) == INTEGER_CST
14153                     && tree_int_cst_lt (index, op1))
14154                   begin = middle + 1;
14155                 else if (TREE_CODE (index) == INTEGER_CST
14156                          && tree_int_cst_lt (op1, index))
14157                   end = middle;
14158                 else if (TREE_CODE (index) == RANGE_EXPR
14159                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
14160                   begin = middle + 1;
14161                 else if (TREE_CODE (index) == RANGE_EXPR
14162                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
14163                   end = middle;
14164                 else
14165                   return VEC_index (constructor_elt, elts, middle)->value;
14166               }
14167           }
14168
14169         return t;
14170       }
14171
14172     case CONST_DECL:
14173       return fold (DECL_INITIAL (t));
14174
14175     default:
14176       return t;
14177     } /* switch (code) */
14178 }
14179
14180 #ifdef ENABLE_FOLD_CHECKING
14181 #undef fold
14182
14183 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
14184 static void fold_check_failed (const_tree, const_tree);
14185 void print_fold_checksum (const_tree);
14186
14187 /* When --enable-checking=fold, compute a digest of expr before
14188    and after actual fold call to see if fold did not accidentally
14189    change original expr.  */
14190
14191 tree
14192 fold (tree expr)
14193 {
14194   tree ret;
14195   struct md5_ctx ctx;
14196   unsigned char checksum_before[16], checksum_after[16];
14197   htab_t ht;
14198
14199   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14200   md5_init_ctx (&ctx);
14201   fold_checksum_tree (expr, &ctx, ht);
14202   md5_finish_ctx (&ctx, checksum_before);
14203   htab_empty (ht);
14204
14205   ret = fold_1 (expr);
14206
14207   md5_init_ctx (&ctx);
14208   fold_checksum_tree (expr, &ctx, ht);
14209   md5_finish_ctx (&ctx, checksum_after);
14210   htab_delete (ht);
14211
14212   if (memcmp (checksum_before, checksum_after, 16))
14213     fold_check_failed (expr, ret);
14214
14215   return ret;
14216 }
14217
14218 void
14219 print_fold_checksum (const_tree expr)
14220 {
14221   struct md5_ctx ctx;
14222   unsigned char checksum[16], cnt;
14223   htab_t ht;
14224
14225   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14226   md5_init_ctx (&ctx);
14227   fold_checksum_tree (expr, &ctx, ht);
14228   md5_finish_ctx (&ctx, checksum);
14229   htab_delete (ht);
14230   for (cnt = 0; cnt < 16; ++cnt)
14231     fprintf (stderr, "%02x", checksum[cnt]);
14232   putc ('\n', stderr);
14233 }
14234
14235 static void
14236 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
14237 {
14238   internal_error ("fold check: original tree changed by fold");
14239 }
14240
14241 static void
14242 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
14243 {
14244   const void **slot;
14245   enum tree_code code;
14246   union tree_node buf;
14247   int i, len;
14248
14249 recursive_label:
14250
14251   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
14252                <= sizeof (struct tree_function_decl))
14253               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
14254   if (expr == NULL)
14255     return;
14256   slot = (const void **) htab_find_slot (ht, expr, INSERT);
14257   if (*slot != NULL)
14258     return;
14259   *slot = expr;
14260   code = TREE_CODE (expr);
14261   if (TREE_CODE_CLASS (code) == tcc_declaration
14262       && DECL_ASSEMBLER_NAME_SET_P (expr))
14263     {
14264       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
14265       memcpy ((char *) &buf, expr, tree_size (expr));
14266       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
14267       expr = (tree) &buf;
14268     }
14269   else if (TREE_CODE_CLASS (code) == tcc_type
14270            && (TYPE_POINTER_TO (expr)
14271                || TYPE_REFERENCE_TO (expr)
14272                || TYPE_CACHED_VALUES_P (expr)
14273                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
14274                || TYPE_NEXT_VARIANT (expr)))
14275     {
14276       /* Allow these fields to be modified.  */
14277       tree tmp;
14278       memcpy ((char *) &buf, expr, tree_size (expr));
14279       expr = tmp = (tree) &buf;
14280       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
14281       TYPE_POINTER_TO (tmp) = NULL;
14282       TYPE_REFERENCE_TO (tmp) = NULL;
14283       TYPE_NEXT_VARIANT (tmp) = NULL;
14284       if (TYPE_CACHED_VALUES_P (tmp))
14285         {
14286           TYPE_CACHED_VALUES_P (tmp) = 0;
14287           TYPE_CACHED_VALUES (tmp) = NULL;
14288         }
14289     }
14290   md5_process_bytes (expr, tree_size (expr), ctx);
14291   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
14292   if (TREE_CODE_CLASS (code) != tcc_type
14293       && TREE_CODE_CLASS (code) != tcc_declaration
14294       && code != TREE_LIST
14295       && code != SSA_NAME)
14296     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
14297   switch (TREE_CODE_CLASS (code))
14298     {
14299     case tcc_constant:
14300       switch (code)
14301         {
14302         case STRING_CST:
14303           md5_process_bytes (TREE_STRING_POINTER (expr),
14304                              TREE_STRING_LENGTH (expr), ctx);
14305           break;
14306         case COMPLEX_CST:
14307           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
14308           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
14309           break;
14310         case VECTOR_CST:
14311           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
14312           break;
14313         default:
14314           break;
14315         }
14316       break;
14317     case tcc_exceptional:
14318       switch (code)
14319         {
14320         case TREE_LIST:
14321           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
14322           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
14323           expr = TREE_CHAIN (expr);
14324           goto recursive_label;
14325           break;
14326         case TREE_VEC:
14327           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
14328             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
14329           break;
14330         default:
14331           break;
14332         }
14333       break;
14334     case tcc_expression:
14335     case tcc_reference:
14336     case tcc_comparison:
14337     case tcc_unary:
14338     case tcc_binary:
14339     case tcc_statement:
14340     case tcc_vl_exp:
14341       len = TREE_OPERAND_LENGTH (expr);
14342       for (i = 0; i < len; ++i)
14343         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
14344       break;
14345     case tcc_declaration:
14346       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
14347       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
14348       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
14349         {
14350           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
14351           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
14352           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
14353           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
14354           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
14355         }
14356       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
14357         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
14358
14359       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
14360         {
14361           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
14362           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
14363           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
14364         }
14365       break;
14366     case tcc_type:
14367       if (TREE_CODE (expr) == ENUMERAL_TYPE)
14368         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
14369       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
14370       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
14371       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
14372       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
14373       if (INTEGRAL_TYPE_P (expr)
14374           || SCALAR_FLOAT_TYPE_P (expr))
14375         {
14376           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
14377           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
14378         }
14379       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
14380       if (TREE_CODE (expr) == RECORD_TYPE
14381           || TREE_CODE (expr) == UNION_TYPE
14382           || TREE_CODE (expr) == QUAL_UNION_TYPE)
14383         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
14384       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
14385       break;
14386     default:
14387       break;
14388     }
14389 }
14390
14391 /* Helper function for outputting the checksum of a tree T.  When
14392    debugging with gdb, you can "define mynext" to be "next" followed
14393    by "call debug_fold_checksum (op0)", then just trace down till the
14394    outputs differ.  */
14395
14396 void
14397 debug_fold_checksum (const_tree t)
14398 {
14399   int i;
14400   unsigned char checksum[16];
14401   struct md5_ctx ctx;
14402   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14403
14404   md5_init_ctx (&ctx);
14405   fold_checksum_tree (t, &ctx, ht);
14406   md5_finish_ctx (&ctx, checksum);
14407   htab_empty (ht);
14408
14409   for (i = 0; i < 16; i++)
14410     fprintf (stderr, "%d ", checksum[i]);
14411
14412   fprintf (stderr, "\n");
14413 }
14414
14415 #endif
14416
14417 /* Fold a unary tree expression with code CODE of type TYPE with an
14418    operand OP0.  LOC is the location of the resulting expression.
14419    Return a folded expression if successful.  Otherwise, return a tree
14420    expression with code CODE of type TYPE with an operand OP0.  */
14421
14422 tree
14423 fold_build1_stat_loc (location_t loc,
14424                       enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
14425 {
14426   tree tem;
14427 #ifdef ENABLE_FOLD_CHECKING
14428   unsigned char checksum_before[16], checksum_after[16];
14429   struct md5_ctx ctx;
14430   htab_t ht;
14431
14432   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14433   md5_init_ctx (&ctx);
14434   fold_checksum_tree (op0, &ctx, ht);
14435   md5_finish_ctx (&ctx, checksum_before);
14436   htab_empty (ht);
14437 #endif
14438
14439   tem = fold_unary_loc (loc, code, type, op0);
14440   if (!tem)
14441     {
14442       tem = build1_stat (code, type, op0 PASS_MEM_STAT);
14443       SET_EXPR_LOCATION (tem, loc);
14444     }
14445
14446 #ifdef ENABLE_FOLD_CHECKING
14447   md5_init_ctx (&ctx);
14448   fold_checksum_tree (op0, &ctx, ht);
14449   md5_finish_ctx (&ctx, checksum_after);
14450   htab_delete (ht);
14451
14452   if (memcmp (checksum_before, checksum_after, 16))
14453     fold_check_failed (op0, tem);
14454 #endif
14455   return tem;
14456 }
14457
14458 /* Fold a binary tree expression with code CODE of type TYPE with
14459    operands OP0 and OP1.  LOC is the location of the resulting
14460    expression.  Return a folded expression if successful.  Otherwise,
14461    return a tree expression with code CODE of type TYPE with operands
14462    OP0 and OP1.  */
14463
14464 tree
14465 fold_build2_stat_loc (location_t loc,
14466                       enum tree_code code, tree type, tree op0, tree op1
14467                       MEM_STAT_DECL)
14468 {
14469   tree tem;
14470 #ifdef ENABLE_FOLD_CHECKING
14471   unsigned char checksum_before_op0[16],
14472                 checksum_before_op1[16],
14473                 checksum_after_op0[16],
14474                 checksum_after_op1[16];
14475   struct md5_ctx ctx;
14476   htab_t ht;
14477
14478   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14479   md5_init_ctx (&ctx);
14480   fold_checksum_tree (op0, &ctx, ht);
14481   md5_finish_ctx (&ctx, checksum_before_op0);
14482   htab_empty (ht);
14483
14484   md5_init_ctx (&ctx);
14485   fold_checksum_tree (op1, &ctx, ht);
14486   md5_finish_ctx (&ctx, checksum_before_op1);
14487   htab_empty (ht);
14488 #endif
14489
14490   tem = fold_binary_loc (loc, code, type, op0, op1);
14491   if (!tem)
14492     {
14493       tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
14494       SET_EXPR_LOCATION (tem, loc);
14495     }
14496
14497 #ifdef ENABLE_FOLD_CHECKING
14498   md5_init_ctx (&ctx);
14499   fold_checksum_tree (op0, &ctx, ht);
14500   md5_finish_ctx (&ctx, checksum_after_op0);
14501   htab_empty (ht);
14502
14503   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14504     fold_check_failed (op0, tem);
14505
14506   md5_init_ctx (&ctx);
14507   fold_checksum_tree (op1, &ctx, ht);
14508   md5_finish_ctx (&ctx, checksum_after_op1);
14509   htab_delete (ht);
14510
14511   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14512     fold_check_failed (op1, tem);
14513 #endif
14514   return tem;
14515 }
14516
14517 /* Fold a ternary tree expression with code CODE of type TYPE with
14518    operands OP0, OP1, and OP2.  Return a folded expression if
14519    successful.  Otherwise, return a tree expression with code CODE of
14520    type TYPE with operands OP0, OP1, and OP2.  */
14521
14522 tree
14523 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14524                       tree op0, tree op1, tree op2 MEM_STAT_DECL)
14525 {
14526   tree tem;
14527 #ifdef ENABLE_FOLD_CHECKING
14528   unsigned char checksum_before_op0[16],
14529                 checksum_before_op1[16],
14530                 checksum_before_op2[16],
14531                 checksum_after_op0[16],
14532                 checksum_after_op1[16],
14533                 checksum_after_op2[16];
14534   struct md5_ctx ctx;
14535   htab_t ht;
14536
14537   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14538   md5_init_ctx (&ctx);
14539   fold_checksum_tree (op0, &ctx, ht);
14540   md5_finish_ctx (&ctx, checksum_before_op0);
14541   htab_empty (ht);
14542
14543   md5_init_ctx (&ctx);
14544   fold_checksum_tree (op1, &ctx, ht);
14545   md5_finish_ctx (&ctx, checksum_before_op1);
14546   htab_empty (ht);
14547
14548   md5_init_ctx (&ctx);
14549   fold_checksum_tree (op2, &ctx, ht);
14550   md5_finish_ctx (&ctx, checksum_before_op2);
14551   htab_empty (ht);
14552 #endif
14553
14554   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14555   tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14556   if (!tem)
14557     {
14558       tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
14559       SET_EXPR_LOCATION (tem, loc);
14560     }
14561
14562 #ifdef ENABLE_FOLD_CHECKING
14563   md5_init_ctx (&ctx);
14564   fold_checksum_tree (op0, &ctx, ht);
14565   md5_finish_ctx (&ctx, checksum_after_op0);
14566   htab_empty (ht);
14567
14568   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14569     fold_check_failed (op0, tem);
14570
14571   md5_init_ctx (&ctx);
14572   fold_checksum_tree (op1, &ctx, ht);
14573   md5_finish_ctx (&ctx, checksum_after_op1);
14574   htab_empty (ht);
14575
14576   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14577     fold_check_failed (op1, tem);
14578
14579   md5_init_ctx (&ctx);
14580   fold_checksum_tree (op2, &ctx, ht);
14581   md5_finish_ctx (&ctx, checksum_after_op2);
14582   htab_delete (ht);
14583
14584   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14585     fold_check_failed (op2, tem);
14586 #endif
14587   return tem;
14588 }
14589
14590 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14591    arguments in ARGARRAY, and a null static chain.
14592    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14593    of type TYPE from the given operands as constructed by build_call_array.  */
14594
14595 tree
14596 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14597                            int nargs, tree *argarray)
14598 {
14599   tree tem;
14600 #ifdef ENABLE_FOLD_CHECKING
14601   unsigned char checksum_before_fn[16],
14602                 checksum_before_arglist[16],
14603                 checksum_after_fn[16],
14604                 checksum_after_arglist[16];
14605   struct md5_ctx ctx;
14606   htab_t ht;
14607   int i;
14608
14609   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14610   md5_init_ctx (&ctx);
14611   fold_checksum_tree (fn, &ctx, ht);
14612   md5_finish_ctx (&ctx, checksum_before_fn);
14613   htab_empty (ht);
14614
14615   md5_init_ctx (&ctx);
14616   for (i = 0; i < nargs; i++)
14617     fold_checksum_tree (argarray[i], &ctx, ht);
14618   md5_finish_ctx (&ctx, checksum_before_arglist);
14619   htab_empty (ht);
14620 #endif
14621
14622   tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14623
14624 #ifdef ENABLE_FOLD_CHECKING
14625   md5_init_ctx (&ctx);
14626   fold_checksum_tree (fn, &ctx, ht);
14627   md5_finish_ctx (&ctx, checksum_after_fn);
14628   htab_empty (ht);
14629
14630   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14631     fold_check_failed (fn, tem);
14632
14633   md5_init_ctx (&ctx);
14634   for (i = 0; i < nargs; i++)
14635     fold_checksum_tree (argarray[i], &ctx, ht);
14636   md5_finish_ctx (&ctx, checksum_after_arglist);
14637   htab_delete (ht);
14638
14639   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14640     fold_check_failed (NULL_TREE, tem);
14641 #endif
14642   return tem;
14643 }
14644
14645 /* Perform constant folding and related simplification of initializer
14646    expression EXPR.  These behave identically to "fold_buildN" but ignore
14647    potential run-time traps and exceptions that fold must preserve.  */
14648
14649 #define START_FOLD_INIT \
14650   int saved_signaling_nans = flag_signaling_nans;\
14651   int saved_trapping_math = flag_trapping_math;\
14652   int saved_rounding_math = flag_rounding_math;\
14653   int saved_trapv = flag_trapv;\
14654   int saved_folding_initializer = folding_initializer;\
14655   flag_signaling_nans = 0;\
14656   flag_trapping_math = 0;\
14657   flag_rounding_math = 0;\
14658   flag_trapv = 0;\
14659   folding_initializer = 1;
14660
14661 #define END_FOLD_INIT \
14662   flag_signaling_nans = saved_signaling_nans;\
14663   flag_trapping_math = saved_trapping_math;\
14664   flag_rounding_math = saved_rounding_math;\
14665   flag_trapv = saved_trapv;\
14666   folding_initializer = saved_folding_initializer;
14667
14668 tree
14669 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14670                              tree type, tree op)
14671 {
14672   tree result;
14673   START_FOLD_INIT;
14674
14675   result = fold_build1_loc (loc, code, type, op);
14676
14677   END_FOLD_INIT;
14678   return result;
14679 }
14680
14681 tree
14682 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14683                              tree type, tree op0, tree op1)
14684 {
14685   tree result;
14686   START_FOLD_INIT;
14687
14688   result = fold_build2_loc (loc, code, type, op0, op1);
14689
14690   END_FOLD_INIT;
14691   return result;
14692 }
14693
14694 tree
14695 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14696                              tree type, tree op0, tree op1, tree op2)
14697 {
14698   tree result;
14699   START_FOLD_INIT;
14700
14701   result = fold_build3_loc (loc, code, type, op0, op1, op2);
14702
14703   END_FOLD_INIT;
14704   return result;
14705 }
14706
14707 tree
14708 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14709                                        int nargs, tree *argarray)
14710 {
14711   tree result;
14712   START_FOLD_INIT;
14713
14714   result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14715
14716   END_FOLD_INIT;
14717   return result;
14718 }
14719
14720 #undef START_FOLD_INIT
14721 #undef END_FOLD_INIT
14722
14723 /* Determine if first argument is a multiple of second argument.  Return 0 if
14724    it is not, or we cannot easily determined it to be.
14725
14726    An example of the sort of thing we care about (at this point; this routine
14727    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14728    fold cases do now) is discovering that
14729
14730      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14731
14732    is a multiple of
14733
14734      SAVE_EXPR (J * 8)
14735
14736    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14737
14738    This code also handles discovering that
14739
14740      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14741
14742    is a multiple of 8 so we don't have to worry about dealing with a
14743    possible remainder.
14744
14745    Note that we *look* inside a SAVE_EXPR only to determine how it was
14746    calculated; it is not safe for fold to do much of anything else with the
14747    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14748    at run time.  For example, the latter example above *cannot* be implemented
14749    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14750    evaluation time of the original SAVE_EXPR is not necessarily the same at
14751    the time the new expression is evaluated.  The only optimization of this
14752    sort that would be valid is changing
14753
14754      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14755
14756    divided by 8 to
14757
14758      SAVE_EXPR (I) * SAVE_EXPR (J)
14759
14760    (where the same SAVE_EXPR (J) is used in the original and the
14761    transformed version).  */
14762
14763 int
14764 multiple_of_p (tree type, const_tree top, const_tree bottom)
14765 {
14766   if (operand_equal_p (top, bottom, 0))
14767     return 1;
14768
14769   if (TREE_CODE (type) != INTEGER_TYPE)
14770     return 0;
14771
14772   switch (TREE_CODE (top))
14773     {
14774     case BIT_AND_EXPR:
14775       /* Bitwise and provides a power of two multiple.  If the mask is
14776          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14777       if (!integer_pow2p (bottom))
14778         return 0;
14779       /* FALLTHRU */
14780
14781     case MULT_EXPR:
14782       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14783               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14784
14785     case PLUS_EXPR:
14786     case MINUS_EXPR:
14787       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14788               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14789
14790     case LSHIFT_EXPR:
14791       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14792         {
14793           tree op1, t1;
14794
14795           op1 = TREE_OPERAND (top, 1);
14796           /* const_binop may not detect overflow correctly,
14797              so check for it explicitly here.  */
14798           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14799               > TREE_INT_CST_LOW (op1)
14800               && TREE_INT_CST_HIGH (op1) == 0
14801               && 0 != (t1 = fold_convert (type,
14802                                           const_binop (LSHIFT_EXPR,
14803                                                        size_one_node,
14804                                                        op1, 0)))
14805               && !TREE_OVERFLOW (t1))
14806             return multiple_of_p (type, t1, bottom);
14807         }
14808       return 0;
14809
14810     case NOP_EXPR:
14811       /* Can't handle conversions from non-integral or wider integral type.  */
14812       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14813           || (TYPE_PRECISION (type)
14814               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14815         return 0;
14816
14817       /* .. fall through ...  */
14818
14819     case SAVE_EXPR:
14820       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14821
14822     case INTEGER_CST:
14823       if (TREE_CODE (bottom) != INTEGER_CST
14824           || integer_zerop (bottom)
14825           || (TYPE_UNSIGNED (type)
14826               && (tree_int_cst_sgn (top) < 0
14827                   || tree_int_cst_sgn (bottom) < 0)))
14828         return 0;
14829       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14830                                              top, bottom, 0));
14831
14832     default:
14833       return 0;
14834     }
14835 }
14836
14837 /* Return true if CODE or TYPE is known to be non-negative. */
14838
14839 static bool
14840 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14841 {
14842   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14843       && truth_value_p (code))
14844     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14845        have a signed:1 type (where the value is -1 and 0).  */
14846     return true;
14847   return false;
14848 }
14849
14850 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14851    value is based on the assumption that signed overflow is undefined,
14852    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14853    *STRICT_OVERFLOW_P.  */
14854
14855 bool
14856 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14857                                 bool *strict_overflow_p)
14858 {
14859   if (TYPE_UNSIGNED (type))
14860     return true;
14861
14862   switch (code)
14863     {
14864     case ABS_EXPR:
14865       /* We can't return 1 if flag_wrapv is set because
14866          ABS_EXPR<INT_MIN> = INT_MIN.  */
14867       if (!INTEGRAL_TYPE_P (type))
14868         return true;
14869       if (TYPE_OVERFLOW_UNDEFINED (type))
14870         {
14871           *strict_overflow_p = true;
14872           return true;
14873         }
14874       break;
14875
14876     case NON_LVALUE_EXPR:
14877     case FLOAT_EXPR:
14878     case FIX_TRUNC_EXPR:
14879       return tree_expr_nonnegative_warnv_p (op0,
14880                                             strict_overflow_p);
14881
14882     case NOP_EXPR:
14883       {
14884         tree inner_type = TREE_TYPE (op0);
14885         tree outer_type = type;
14886
14887         if (TREE_CODE (outer_type) == REAL_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               {
14894                 if (TYPE_UNSIGNED (inner_type))
14895                   return true;
14896                 return tree_expr_nonnegative_warnv_p (op0,
14897                                                       strict_overflow_p);
14898               }
14899           }
14900         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14901           {
14902             if (TREE_CODE (inner_type) == REAL_TYPE)
14903               return tree_expr_nonnegative_warnv_p (op0,
14904                                                     strict_overflow_p);
14905             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14906               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14907                       && TYPE_UNSIGNED (inner_type);
14908           }
14909       }
14910       break;
14911
14912     default:
14913       return tree_simple_nonnegative_warnv_p (code, type);
14914     }
14915
14916   /* We don't know sign of `t', so be conservative and return false.  */
14917   return false;
14918 }
14919
14920 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14921    value is based on the assumption that signed overflow is undefined,
14922    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14923    *STRICT_OVERFLOW_P.  */
14924
14925 bool
14926 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14927                                       tree op1, bool *strict_overflow_p)
14928 {
14929   if (TYPE_UNSIGNED (type))
14930     return true;
14931
14932   switch (code)
14933     {
14934     case POINTER_PLUS_EXPR:
14935     case PLUS_EXPR:
14936       if (FLOAT_TYPE_P (type))
14937         return (tree_expr_nonnegative_warnv_p (op0,
14938                                                strict_overflow_p)
14939                 && tree_expr_nonnegative_warnv_p (op1,
14940                                                   strict_overflow_p));
14941
14942       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14943          both unsigned and at least 2 bits shorter than the result.  */
14944       if (TREE_CODE (type) == INTEGER_TYPE
14945           && TREE_CODE (op0) == NOP_EXPR
14946           && TREE_CODE (op1) == NOP_EXPR)
14947         {
14948           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14949           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14950           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14951               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14952             {
14953               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14954                                        TYPE_PRECISION (inner2)) + 1;
14955               return prec < TYPE_PRECISION (type);
14956             }
14957         }
14958       break;
14959
14960     case MULT_EXPR:
14961       if (FLOAT_TYPE_P (type))
14962         {
14963           /* x * x for floating point x is always non-negative.  */
14964           if (operand_equal_p (op0, op1, 0))
14965             return true;
14966           return (tree_expr_nonnegative_warnv_p (op0,
14967                                                  strict_overflow_p)
14968                   && tree_expr_nonnegative_warnv_p (op1,
14969                                                     strict_overflow_p));
14970         }
14971
14972       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14973          both unsigned and their total bits is shorter than the result.  */
14974       if (TREE_CODE (type) == INTEGER_TYPE
14975           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14976           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14977         {
14978           tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14979             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14980             : TREE_TYPE (op0);
14981           tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14982             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14983             : TREE_TYPE (op1);
14984
14985           bool unsigned0 = TYPE_UNSIGNED (inner0);
14986           bool unsigned1 = TYPE_UNSIGNED (inner1);
14987
14988           if (TREE_CODE (op0) == INTEGER_CST)
14989             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14990
14991           if (TREE_CODE (op1) == INTEGER_CST)
14992             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14993
14994           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14995               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14996             {
14997               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14998                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14999                 : TYPE_PRECISION (inner0);
15000
15001               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
15002                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
15003                 : TYPE_PRECISION (inner1);
15004
15005               return precision0 + precision1 < TYPE_PRECISION (type);
15006             }
15007         }
15008       return false;
15009
15010     case BIT_AND_EXPR:
15011     case MAX_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 BIT_IOR_EXPR:
15018     case BIT_XOR_EXPR:
15019     case MIN_EXPR:
15020     case RDIV_EXPR:
15021     case TRUNC_DIV_EXPR:
15022     case CEIL_DIV_EXPR:
15023     case FLOOR_DIV_EXPR:
15024     case ROUND_DIV_EXPR:
15025       return (tree_expr_nonnegative_warnv_p (op0,
15026                                              strict_overflow_p)
15027               && tree_expr_nonnegative_warnv_p (op1,
15028                                                 strict_overflow_p));
15029
15030     case TRUNC_MOD_EXPR:
15031     case CEIL_MOD_EXPR:
15032     case FLOOR_MOD_EXPR:
15033     case ROUND_MOD_EXPR:
15034       return tree_expr_nonnegative_warnv_p (op0,
15035                                             strict_overflow_p);
15036     default:
15037       return tree_simple_nonnegative_warnv_p (code, type);
15038     }
15039
15040   /* We don't know sign of `t', so be conservative and return false.  */
15041   return false;
15042 }
15043
15044 /* Return true if T is known to be non-negative.  If the return
15045    value is based on the assumption that signed overflow is undefined,
15046    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15047    *STRICT_OVERFLOW_P.  */
15048
15049 bool
15050 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15051 {
15052   if (TYPE_UNSIGNED (TREE_TYPE (t)))
15053     return true;
15054
15055   switch (TREE_CODE (t))
15056     {
15057     case INTEGER_CST:
15058       return tree_int_cst_sgn (t) >= 0;
15059
15060     case REAL_CST:
15061       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
15062
15063     case FIXED_CST:
15064       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
15065
15066     case COND_EXPR:
15067       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15068                                              strict_overflow_p)
15069               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
15070                                                 strict_overflow_p));
15071     default:
15072       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15073                                                    TREE_TYPE (t));
15074     }
15075   /* We don't know sign of `t', so be conservative and return false.  */
15076   return false;
15077 }
15078
15079 /* Return true if T is known to be non-negative.  If the return
15080    value is based on the assumption that signed overflow is undefined,
15081    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15082    *STRICT_OVERFLOW_P.  */
15083
15084 bool
15085 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
15086                                tree arg0, tree arg1, bool *strict_overflow_p)
15087 {
15088   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
15089     switch (DECL_FUNCTION_CODE (fndecl))
15090       {
15091         CASE_FLT_FN (BUILT_IN_ACOS):
15092         CASE_FLT_FN (BUILT_IN_ACOSH):
15093         CASE_FLT_FN (BUILT_IN_CABS):
15094         CASE_FLT_FN (BUILT_IN_COSH):
15095         CASE_FLT_FN (BUILT_IN_ERFC):
15096         CASE_FLT_FN (BUILT_IN_EXP):
15097         CASE_FLT_FN (BUILT_IN_EXP10):
15098         CASE_FLT_FN (BUILT_IN_EXP2):
15099         CASE_FLT_FN (BUILT_IN_FABS):
15100         CASE_FLT_FN (BUILT_IN_FDIM):
15101         CASE_FLT_FN (BUILT_IN_HYPOT):
15102         CASE_FLT_FN (BUILT_IN_POW10):
15103         CASE_INT_FN (BUILT_IN_FFS):
15104         CASE_INT_FN (BUILT_IN_PARITY):
15105         CASE_INT_FN (BUILT_IN_POPCOUNT):
15106       case BUILT_IN_BSWAP32:
15107       case BUILT_IN_BSWAP64:
15108         /* Always true.  */
15109         return true;
15110
15111         CASE_FLT_FN (BUILT_IN_SQRT):
15112         /* sqrt(-0.0) is -0.0.  */
15113         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
15114           return true;
15115         return tree_expr_nonnegative_warnv_p (arg0,
15116                                               strict_overflow_p);
15117
15118         CASE_FLT_FN (BUILT_IN_ASINH):
15119         CASE_FLT_FN (BUILT_IN_ATAN):
15120         CASE_FLT_FN (BUILT_IN_ATANH):
15121         CASE_FLT_FN (BUILT_IN_CBRT):
15122         CASE_FLT_FN (BUILT_IN_CEIL):
15123         CASE_FLT_FN (BUILT_IN_ERF):
15124         CASE_FLT_FN (BUILT_IN_EXPM1):
15125         CASE_FLT_FN (BUILT_IN_FLOOR):
15126         CASE_FLT_FN (BUILT_IN_FMOD):
15127         CASE_FLT_FN (BUILT_IN_FREXP):
15128         CASE_FLT_FN (BUILT_IN_LCEIL):
15129         CASE_FLT_FN (BUILT_IN_LDEXP):
15130         CASE_FLT_FN (BUILT_IN_LFLOOR):
15131         CASE_FLT_FN (BUILT_IN_LLCEIL):
15132         CASE_FLT_FN (BUILT_IN_LLFLOOR):
15133         CASE_FLT_FN (BUILT_IN_LLRINT):
15134         CASE_FLT_FN (BUILT_IN_LLROUND):
15135         CASE_FLT_FN (BUILT_IN_LRINT):
15136         CASE_FLT_FN (BUILT_IN_LROUND):
15137         CASE_FLT_FN (BUILT_IN_MODF):
15138         CASE_FLT_FN (BUILT_IN_NEARBYINT):
15139         CASE_FLT_FN (BUILT_IN_RINT):
15140         CASE_FLT_FN (BUILT_IN_ROUND):
15141         CASE_FLT_FN (BUILT_IN_SCALB):
15142         CASE_FLT_FN (BUILT_IN_SCALBLN):
15143         CASE_FLT_FN (BUILT_IN_SCALBN):
15144         CASE_FLT_FN (BUILT_IN_SIGNBIT):
15145         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
15146         CASE_FLT_FN (BUILT_IN_SINH):
15147         CASE_FLT_FN (BUILT_IN_TANH):
15148         CASE_FLT_FN (BUILT_IN_TRUNC):
15149         /* True if the 1st argument is nonnegative.  */
15150         return tree_expr_nonnegative_warnv_p (arg0,
15151                                               strict_overflow_p);
15152
15153         CASE_FLT_FN (BUILT_IN_FMAX):
15154         /* True if the 1st OR 2nd arguments are nonnegative.  */
15155         return (tree_expr_nonnegative_warnv_p (arg0,
15156                                                strict_overflow_p)
15157                 || (tree_expr_nonnegative_warnv_p (arg1,
15158                                                    strict_overflow_p)));
15159
15160         CASE_FLT_FN (BUILT_IN_FMIN):
15161         /* True if the 1st AND 2nd arguments are nonnegative.  */
15162         return (tree_expr_nonnegative_warnv_p (arg0,
15163                                                strict_overflow_p)
15164                 && (tree_expr_nonnegative_warnv_p (arg1,
15165                                                    strict_overflow_p)));
15166
15167         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15168         /* True if the 2nd argument is nonnegative.  */
15169         return tree_expr_nonnegative_warnv_p (arg1,
15170                                               strict_overflow_p);
15171
15172         CASE_FLT_FN (BUILT_IN_POWI):
15173         /* True if the 1st argument is nonnegative or the second
15174            argument is an even integer.  */
15175         if (TREE_CODE (arg1) == INTEGER_CST
15176             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15177           return true;
15178         return tree_expr_nonnegative_warnv_p (arg0,
15179                                               strict_overflow_p);
15180
15181         CASE_FLT_FN (BUILT_IN_POW):
15182         /* True if the 1st argument is nonnegative or the second
15183            argument is an even integer valued real.  */
15184         if (TREE_CODE (arg1) == REAL_CST)
15185           {
15186             REAL_VALUE_TYPE c;
15187             HOST_WIDE_INT n;
15188
15189             c = TREE_REAL_CST (arg1);
15190             n = real_to_integer (&c);
15191             if ((n & 1) == 0)
15192               {
15193                 REAL_VALUE_TYPE cint;
15194                 real_from_integer (&cint, VOIDmode, n,
15195                                    n < 0 ? -1 : 0, 0);
15196                 if (real_identical (&c, &cint))
15197                   return true;
15198               }
15199           }
15200         return tree_expr_nonnegative_warnv_p (arg0,
15201                                               strict_overflow_p);
15202
15203       default:
15204         break;
15205       }
15206   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
15207                                           type);
15208 }
15209
15210 /* Return true if T is known to be non-negative.  If the return
15211    value is based on the assumption that signed overflow is undefined,
15212    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15213    *STRICT_OVERFLOW_P.  */
15214
15215 bool
15216 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15217 {
15218   enum tree_code code = TREE_CODE (t);
15219   if (TYPE_UNSIGNED (TREE_TYPE (t)))
15220     return true;
15221
15222   switch (code)
15223     {
15224     case TARGET_EXPR:
15225       {
15226         tree temp = TARGET_EXPR_SLOT (t);
15227         t = TARGET_EXPR_INITIAL (t);
15228
15229         /* If the initializer is non-void, then it's a normal expression
15230            that will be assigned to the slot.  */
15231         if (!VOID_TYPE_P (t))
15232           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
15233
15234         /* Otherwise, the initializer sets the slot in some way.  One common
15235            way is an assignment statement at the end of the initializer.  */
15236         while (1)
15237           {
15238             if (TREE_CODE (t) == BIND_EXPR)
15239               t = expr_last (BIND_EXPR_BODY (t));
15240             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15241                      || TREE_CODE (t) == TRY_CATCH_EXPR)
15242               t = expr_last (TREE_OPERAND (t, 0));
15243             else if (TREE_CODE (t) == STATEMENT_LIST)
15244               t = expr_last (t);
15245             else
15246               break;
15247           }
15248         if (TREE_CODE (t) == MODIFY_EXPR
15249             && TREE_OPERAND (t, 0) == temp)
15250           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15251                                                 strict_overflow_p);
15252
15253         return false;
15254       }
15255
15256     case CALL_EXPR:
15257       {
15258         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
15259         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
15260
15261         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15262                                               get_callee_fndecl (t),
15263                                               arg0,
15264                                               arg1,
15265                                               strict_overflow_p);
15266       }
15267     case COMPOUND_EXPR:
15268     case MODIFY_EXPR:
15269       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15270                                             strict_overflow_p);
15271     case BIND_EXPR:
15272       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
15273                                             strict_overflow_p);
15274     case SAVE_EXPR:
15275       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
15276                                             strict_overflow_p);
15277
15278     default:
15279       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15280                                                    TREE_TYPE (t));
15281     }
15282
15283   /* We don't know sign of `t', so be conservative and return false.  */
15284   return false;
15285 }
15286
15287 /* Return true if T is known to be non-negative.  If the return
15288    value is based on the assumption that signed overflow is undefined,
15289    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15290    *STRICT_OVERFLOW_P.  */
15291
15292 bool
15293 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15294 {
15295   enum tree_code code;
15296   if (t == error_mark_node)
15297     return false;
15298
15299   code = TREE_CODE (t);
15300   switch (TREE_CODE_CLASS (code))
15301     {
15302     case tcc_binary:
15303     case tcc_comparison:
15304       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15305                                               TREE_TYPE (t),
15306                                               TREE_OPERAND (t, 0),
15307                                               TREE_OPERAND (t, 1),
15308                                               strict_overflow_p);
15309
15310     case tcc_unary:
15311       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15312                                              TREE_TYPE (t),
15313                                              TREE_OPERAND (t, 0),
15314                                              strict_overflow_p);
15315
15316     case tcc_constant:
15317     case tcc_declaration:
15318     case tcc_reference:
15319       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15320
15321     default:
15322       break;
15323     }
15324
15325   switch (code)
15326     {
15327     case TRUTH_AND_EXPR:
15328     case TRUTH_OR_EXPR:
15329     case TRUTH_XOR_EXPR:
15330       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15331                                               TREE_TYPE (t),
15332                                               TREE_OPERAND (t, 0),
15333                                               TREE_OPERAND (t, 1),
15334                                               strict_overflow_p);
15335     case TRUTH_NOT_EXPR:
15336       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15337                                              TREE_TYPE (t),
15338                                              TREE_OPERAND (t, 0),
15339                                              strict_overflow_p);
15340
15341     case COND_EXPR:
15342     case CONSTRUCTOR:
15343     case OBJ_TYPE_REF:
15344     case ASSERT_EXPR:
15345     case ADDR_EXPR:
15346     case WITH_SIZE_EXPR:
15347     case SSA_NAME:
15348       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15349
15350     default:
15351       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
15352     }
15353 }
15354
15355 /* Return true if `t' is known to be non-negative.  Handle warnings
15356    about undefined signed overflow.  */
15357
15358 bool
15359 tree_expr_nonnegative_p (tree t)
15360 {
15361   bool ret, strict_overflow_p;
15362
15363   strict_overflow_p = false;
15364   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15365   if (strict_overflow_p)
15366     fold_overflow_warning (("assuming signed overflow does not occur when "
15367                             "determining that expression is always "
15368                             "non-negative"),
15369                            WARN_STRICT_OVERFLOW_MISC);
15370   return ret;
15371 }
15372
15373
15374 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15375    For floating point we further ensure that T is not denormal.
15376    Similar logic is present in nonzero_address in rtlanal.h.
15377
15378    If the return value is based on the assumption that signed overflow
15379    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15380    change *STRICT_OVERFLOW_P.  */
15381
15382 bool
15383 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15384                                  bool *strict_overflow_p)
15385 {
15386   switch (code)
15387     {
15388     case ABS_EXPR:
15389       return tree_expr_nonzero_warnv_p (op0,
15390                                         strict_overflow_p);
15391
15392     case NOP_EXPR:
15393       {
15394         tree inner_type = TREE_TYPE (op0);
15395         tree outer_type = type;
15396
15397         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15398                 && tree_expr_nonzero_warnv_p (op0,
15399                                               strict_overflow_p));
15400       }
15401       break;
15402
15403     case NON_LVALUE_EXPR:
15404       return tree_expr_nonzero_warnv_p (op0,
15405                                         strict_overflow_p);
15406
15407     default:
15408       break;
15409   }
15410
15411   return false;
15412 }
15413
15414 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15415    For floating point we further ensure that T is not denormal.
15416    Similar logic is present in nonzero_address in rtlanal.h.
15417
15418    If the return value is based on the assumption that signed overflow
15419    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15420    change *STRICT_OVERFLOW_P.  */
15421
15422 bool
15423 tree_binary_nonzero_warnv_p (enum tree_code code,
15424                              tree type,
15425                              tree op0,
15426                              tree op1, bool *strict_overflow_p)
15427 {
15428   bool sub_strict_overflow_p;
15429   switch (code)
15430     {
15431     case POINTER_PLUS_EXPR:
15432     case PLUS_EXPR:
15433       if (TYPE_OVERFLOW_UNDEFINED (type))
15434         {
15435           /* With the presence of negative values it is hard
15436              to say something.  */
15437           sub_strict_overflow_p = false;
15438           if (!tree_expr_nonnegative_warnv_p (op0,
15439                                               &sub_strict_overflow_p)
15440               || !tree_expr_nonnegative_warnv_p (op1,
15441                                                  &sub_strict_overflow_p))
15442             return false;
15443           /* One of operands must be positive and the other non-negative.  */
15444           /* We don't set *STRICT_OVERFLOW_P here: even if this value
15445              overflows, on a twos-complement machine the sum of two
15446              nonnegative numbers can never be zero.  */
15447           return (tree_expr_nonzero_warnv_p (op0,
15448                                              strict_overflow_p)
15449                   || tree_expr_nonzero_warnv_p (op1,
15450                                                 strict_overflow_p));
15451         }
15452       break;
15453
15454     case MULT_EXPR:
15455       if (TYPE_OVERFLOW_UNDEFINED (type))
15456         {
15457           if (tree_expr_nonzero_warnv_p (op0,
15458                                          strict_overflow_p)
15459               && tree_expr_nonzero_warnv_p (op1,
15460                                             strict_overflow_p))
15461             {
15462               *strict_overflow_p = true;
15463               return true;
15464             }
15465         }
15466       break;
15467
15468     case MIN_EXPR:
15469       sub_strict_overflow_p = false;
15470       if (tree_expr_nonzero_warnv_p (op0,
15471                                      &sub_strict_overflow_p)
15472           && tree_expr_nonzero_warnv_p (op1,
15473                                         &sub_strict_overflow_p))
15474         {
15475           if (sub_strict_overflow_p)
15476             *strict_overflow_p = true;
15477         }
15478       break;
15479
15480     case MAX_EXPR:
15481       sub_strict_overflow_p = false;
15482       if (tree_expr_nonzero_warnv_p (op0,
15483                                      &sub_strict_overflow_p))
15484         {
15485           if (sub_strict_overflow_p)
15486             *strict_overflow_p = true;
15487
15488           /* When both operands are nonzero, then MAX must be too.  */
15489           if (tree_expr_nonzero_warnv_p (op1,
15490                                          strict_overflow_p))
15491             return true;
15492
15493           /* MAX where operand 0 is positive is positive.  */
15494           return tree_expr_nonnegative_warnv_p (op0,
15495                                                strict_overflow_p);
15496         }
15497       /* MAX where operand 1 is positive is positive.  */
15498       else if (tree_expr_nonzero_warnv_p (op1,
15499                                           &sub_strict_overflow_p)
15500                && tree_expr_nonnegative_warnv_p (op1,
15501                                                  &sub_strict_overflow_p))
15502         {
15503           if (sub_strict_overflow_p)
15504             *strict_overflow_p = true;
15505           return true;
15506         }
15507       break;
15508
15509     case BIT_IOR_EXPR:
15510       return (tree_expr_nonzero_warnv_p (op1,
15511                                          strict_overflow_p)
15512               || tree_expr_nonzero_warnv_p (op0,
15513                                             strict_overflow_p));
15514
15515     default:
15516       break;
15517   }
15518
15519   return false;
15520 }
15521
15522 /* Return true when T is an address and is known to be nonzero.
15523    For floating point we further ensure that T is not denormal.
15524    Similar logic is present in nonzero_address in rtlanal.h.
15525
15526    If the return value is based on the assumption that signed overflow
15527    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15528    change *STRICT_OVERFLOW_P.  */
15529
15530 bool
15531 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15532 {
15533   bool sub_strict_overflow_p;
15534   switch (TREE_CODE (t))
15535     {
15536     case INTEGER_CST:
15537       return !integer_zerop (t);
15538
15539     case ADDR_EXPR:
15540       {
15541         tree base = get_base_address (TREE_OPERAND (t, 0));
15542
15543         if (!base)
15544           return false;
15545
15546         /* Weak declarations may link to NULL.  Other things may also be NULL
15547            so protect with -fdelete-null-pointer-checks; but not variables
15548            allocated on the stack.  */
15549         if (DECL_P (base)
15550             && (flag_delete_null_pointer_checks
15551                 || (TREE_CODE (base) == VAR_DECL && !TREE_STATIC (base))))
15552           return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15553
15554         /* Constants are never weak.  */
15555         if (CONSTANT_CLASS_P (base))
15556           return true;
15557
15558         return false;
15559       }
15560
15561     case COND_EXPR:
15562       sub_strict_overflow_p = false;
15563       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15564                                      &sub_strict_overflow_p)
15565           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15566                                         &sub_strict_overflow_p))
15567         {
15568           if (sub_strict_overflow_p)
15569             *strict_overflow_p = true;
15570           return true;
15571         }
15572       break;
15573
15574     default:
15575       break;
15576     }
15577   return false;
15578 }
15579
15580 /* Return true when T is an address and is known to be nonzero.
15581    For floating point we further ensure that T is not denormal.
15582    Similar logic is present in nonzero_address in rtlanal.h.
15583
15584    If the return value is based on the assumption that signed overflow
15585    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15586    change *STRICT_OVERFLOW_P.  */
15587
15588 bool
15589 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15590 {
15591   tree type = TREE_TYPE (t);
15592   enum tree_code code;
15593
15594   /* Doing something useful for floating point would need more work.  */
15595   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15596     return false;
15597
15598   code = TREE_CODE (t);
15599   switch (TREE_CODE_CLASS (code))
15600     {
15601     case tcc_unary:
15602       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15603                                               strict_overflow_p);
15604     case tcc_binary:
15605     case tcc_comparison:
15606       return tree_binary_nonzero_warnv_p (code, type,
15607                                                TREE_OPERAND (t, 0),
15608                                                TREE_OPERAND (t, 1),
15609                                                strict_overflow_p);
15610     case tcc_constant:
15611     case tcc_declaration:
15612     case tcc_reference:
15613       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15614
15615     default:
15616       break;
15617     }
15618
15619   switch (code)
15620     {
15621     case TRUTH_NOT_EXPR:
15622       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15623                                               strict_overflow_p);
15624
15625     case TRUTH_AND_EXPR:
15626     case TRUTH_OR_EXPR:
15627     case TRUTH_XOR_EXPR:
15628       return tree_binary_nonzero_warnv_p (code, type,
15629                                                TREE_OPERAND (t, 0),
15630                                                TREE_OPERAND (t, 1),
15631                                                strict_overflow_p);
15632
15633     case COND_EXPR:
15634     case CONSTRUCTOR:
15635     case OBJ_TYPE_REF:
15636     case ASSERT_EXPR:
15637     case ADDR_EXPR:
15638     case WITH_SIZE_EXPR:
15639     case SSA_NAME:
15640       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15641
15642     case COMPOUND_EXPR:
15643     case MODIFY_EXPR:
15644     case BIND_EXPR:
15645       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15646                                         strict_overflow_p);
15647
15648     case SAVE_EXPR:
15649       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15650                                         strict_overflow_p);
15651
15652     case CALL_EXPR:
15653       return alloca_call_p (t);
15654
15655     default:
15656       break;
15657     }
15658   return false;
15659 }
15660
15661 /* Return true when T is an address and is known to be nonzero.
15662    Handle warnings about undefined signed overflow.  */
15663
15664 bool
15665 tree_expr_nonzero_p (tree t)
15666 {
15667   bool ret, strict_overflow_p;
15668
15669   strict_overflow_p = false;
15670   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15671   if (strict_overflow_p)
15672     fold_overflow_warning (("assuming signed overflow does not occur when "
15673                             "determining that expression is always "
15674                             "non-zero"),
15675                            WARN_STRICT_OVERFLOW_MISC);
15676   return ret;
15677 }
15678
15679 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15680    attempt to fold the expression to a constant without modifying TYPE,
15681    OP0 or OP1.
15682
15683    If the expression could be simplified to a constant, then return
15684    the constant.  If the expression would not be simplified to a
15685    constant, then return NULL_TREE.  */
15686
15687 tree
15688 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15689 {
15690   tree tem = fold_binary (code, type, op0, op1);
15691   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15692 }
15693
15694 /* Given the components of a unary expression CODE, TYPE and OP0,
15695    attempt to fold the expression to a constant without modifying
15696    TYPE or OP0.
15697
15698    If the expression could be simplified to a constant, then return
15699    the constant.  If the expression would not be simplified to a
15700    constant, then return NULL_TREE.  */
15701
15702 tree
15703 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15704 {
15705   tree tem = fold_unary (code, type, op0);
15706   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15707 }
15708
15709 /* If EXP represents referencing an element in a constant string
15710    (either via pointer arithmetic or array indexing), return the
15711    tree representing the value accessed, otherwise return NULL.  */
15712
15713 tree
15714 fold_read_from_constant_string (tree exp)
15715 {
15716   if ((TREE_CODE (exp) == INDIRECT_REF
15717        || TREE_CODE (exp) == ARRAY_REF)
15718       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15719     {
15720       tree exp1 = TREE_OPERAND (exp, 0);
15721       tree index;
15722       tree string;
15723       location_t loc = EXPR_LOCATION (exp);
15724
15725       if (TREE_CODE (exp) == INDIRECT_REF)
15726         string = string_constant (exp1, &index);
15727       else
15728         {
15729           tree low_bound = array_ref_low_bound (exp);
15730           index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15731
15732           /* Optimize the special-case of a zero lower bound.
15733
15734              We convert the low_bound to sizetype to avoid some problems
15735              with constant folding.  (E.g. suppose the lower bound is 1,
15736              and its mode is QI.  Without the conversion,l (ARRAY
15737              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15738              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15739           if (! integer_zerop (low_bound))
15740             index = size_diffop_loc (loc, index,
15741                                  fold_convert_loc (loc, sizetype, low_bound));
15742
15743           string = exp1;
15744         }
15745
15746       if (string
15747           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15748           && TREE_CODE (string) == STRING_CST
15749           && TREE_CODE (index) == INTEGER_CST
15750           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15751           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15752               == MODE_INT)
15753           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15754         return build_int_cst_type (TREE_TYPE (exp),
15755                                    (TREE_STRING_POINTER (string)
15756                                     [TREE_INT_CST_LOW (index)]));
15757     }
15758   return NULL;
15759 }
15760
15761 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15762    an integer constant, real, or fixed-point constant.
15763
15764    TYPE is the type of the result.  */
15765
15766 static tree
15767 fold_negate_const (tree arg0, tree type)
15768 {
15769   tree t = NULL_TREE;
15770
15771   switch (TREE_CODE (arg0))
15772     {
15773     case INTEGER_CST:
15774       {
15775         unsigned HOST_WIDE_INT low;
15776         HOST_WIDE_INT high;
15777         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15778                                    TREE_INT_CST_HIGH (arg0),
15779                                    &low, &high);
15780         t = force_fit_type_double (type, low, high, 1,
15781                                    (overflow | TREE_OVERFLOW (arg0))
15782                                    && !TYPE_UNSIGNED (type));
15783         break;
15784       }
15785
15786     case REAL_CST:
15787       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15788       break;
15789
15790     case FIXED_CST:
15791       {
15792         FIXED_VALUE_TYPE f;
15793         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15794                                             &(TREE_FIXED_CST (arg0)), NULL,
15795                                             TYPE_SATURATING (type));
15796         t = build_fixed (type, f);
15797         /* Propagate overflow flags.  */
15798         if (overflow_p | TREE_OVERFLOW (arg0))
15799           TREE_OVERFLOW (t) = 1;
15800         break;
15801       }
15802
15803     default:
15804       gcc_unreachable ();
15805     }
15806
15807   return t;
15808 }
15809
15810 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15811    an integer constant or real constant.
15812
15813    TYPE is the type of the result.  */
15814
15815 tree
15816 fold_abs_const (tree arg0, tree type)
15817 {
15818   tree t = NULL_TREE;
15819
15820   switch (TREE_CODE (arg0))
15821     {
15822     case INTEGER_CST:
15823       /* If the value is unsigned, then the absolute value is
15824          the same as the ordinary value.  */
15825       if (TYPE_UNSIGNED (type))
15826         t = arg0;
15827       /* Similarly, if the value is non-negative.  */
15828       else if (INT_CST_LT (integer_minus_one_node, arg0))
15829         t = arg0;
15830       /* If the value is negative, then the absolute value is
15831          its negation.  */
15832       else
15833         {
15834           unsigned HOST_WIDE_INT low;
15835           HOST_WIDE_INT high;
15836           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15837                                      TREE_INT_CST_HIGH (arg0),
15838                                      &low, &high);
15839           t = force_fit_type_double (type, low, high, -1,
15840                                      overflow | TREE_OVERFLOW (arg0));
15841         }
15842       break;
15843
15844     case REAL_CST:
15845       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15846         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15847       else
15848         t =  arg0;
15849       break;
15850
15851     default:
15852       gcc_unreachable ();
15853     }
15854
15855   return t;
15856 }
15857
15858 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15859    constant.  TYPE is the type of the result.  */
15860
15861 static tree
15862 fold_not_const (tree arg0, tree type)
15863 {
15864   tree t = NULL_TREE;
15865
15866   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15867
15868   t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15869                              ~TREE_INT_CST_HIGH (arg0), 0,
15870                              TREE_OVERFLOW (arg0));
15871
15872   return t;
15873 }
15874
15875 /* Given CODE, a relational operator, the target type, TYPE and two
15876    constant operands OP0 and OP1, return the result of the
15877    relational operation.  If the result is not a compile time
15878    constant, then return NULL_TREE.  */
15879
15880 static tree
15881 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15882 {
15883   int result, invert;
15884
15885   /* From here on, the only cases we handle are when the result is
15886      known to be a constant.  */
15887
15888   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15889     {
15890       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15891       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15892
15893       /* Handle the cases where either operand is a NaN.  */
15894       if (real_isnan (c0) || real_isnan (c1))
15895         {
15896           switch (code)
15897             {
15898             case EQ_EXPR:
15899             case ORDERED_EXPR:
15900               result = 0;
15901               break;
15902
15903             case NE_EXPR:
15904             case UNORDERED_EXPR:
15905             case UNLT_EXPR:
15906             case UNLE_EXPR:
15907             case UNGT_EXPR:
15908             case UNGE_EXPR:
15909             case UNEQ_EXPR:
15910               result = 1;
15911               break;
15912
15913             case LT_EXPR:
15914             case LE_EXPR:
15915             case GT_EXPR:
15916             case GE_EXPR:
15917             case LTGT_EXPR:
15918               if (flag_trapping_math)
15919                 return NULL_TREE;
15920               result = 0;
15921               break;
15922
15923             default:
15924               gcc_unreachable ();
15925             }
15926
15927           return constant_boolean_node (result, type);
15928         }
15929
15930       return constant_boolean_node (real_compare (code, c0, c1), type);
15931     }
15932
15933   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15934     {
15935       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15936       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15937       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15938     }
15939
15940   /* Handle equality/inequality of complex constants.  */
15941   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15942     {
15943       tree rcond = fold_relational_const (code, type,
15944                                           TREE_REALPART (op0),
15945                                           TREE_REALPART (op1));
15946       tree icond = fold_relational_const (code, type,
15947                                           TREE_IMAGPART (op0),
15948                                           TREE_IMAGPART (op1));
15949       if (code == EQ_EXPR)
15950         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15951       else if (code == NE_EXPR)
15952         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15953       else
15954         return NULL_TREE;
15955     }
15956
15957   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15958
15959      To compute GT, swap the arguments and do LT.
15960      To compute GE, do LT and invert the result.
15961      To compute LE, swap the arguments, do LT and invert the result.
15962      To compute NE, do EQ and invert the result.
15963
15964      Therefore, the code below must handle only EQ and LT.  */
15965
15966   if (code == LE_EXPR || code == GT_EXPR)
15967     {
15968       tree tem = op0;
15969       op0 = op1;
15970       op1 = tem;
15971       code = swap_tree_comparison (code);
15972     }
15973
15974   /* Note that it is safe to invert for real values here because we
15975      have already handled the one case that it matters.  */
15976
15977   invert = 0;
15978   if (code == NE_EXPR || code == GE_EXPR)
15979     {
15980       invert = 1;
15981       code = invert_tree_comparison (code, false);
15982     }
15983
15984   /* Compute a result for LT or EQ if args permit;
15985      Otherwise return T.  */
15986   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15987     {
15988       if (code == EQ_EXPR)
15989         result = tree_int_cst_equal (op0, op1);
15990       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15991         result = INT_CST_LT_UNSIGNED (op0, op1);
15992       else
15993         result = INT_CST_LT (op0, op1);
15994     }
15995   else
15996     return NULL_TREE;
15997
15998   if (invert)
15999     result ^= 1;
16000   return constant_boolean_node (result, type);
16001 }
16002
16003 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
16004    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
16005    itself.  */
16006
16007 tree
16008 fold_build_cleanup_point_expr (tree type, tree expr)
16009 {
16010   /* If the expression does not have side effects then we don't have to wrap
16011      it with a cleanup point expression.  */
16012   if (!TREE_SIDE_EFFECTS (expr))
16013     return expr;
16014
16015   /* If the expression is a return, check to see if the expression inside the
16016      return has no side effects or the right hand side of the modify expression
16017      inside the return. If either don't have side effects set we don't need to
16018      wrap the expression in a cleanup point expression.  Note we don't check the
16019      left hand side of the modify because it should always be a return decl.  */
16020   if (TREE_CODE (expr) == RETURN_EXPR)
16021     {
16022       tree op = TREE_OPERAND (expr, 0);
16023       if (!op || !TREE_SIDE_EFFECTS (op))
16024         return expr;
16025       op = TREE_OPERAND (op, 1);
16026       if (!TREE_SIDE_EFFECTS (op))
16027         return expr;
16028     }
16029
16030   return build1 (CLEANUP_POINT_EXPR, type, expr);
16031 }
16032
16033 /* Given a pointer value OP0 and a type TYPE, return a simplified version
16034    of an indirection through OP0, or NULL_TREE if no simplification is
16035    possible.  */
16036
16037 tree
16038 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16039 {
16040   tree sub = op0;
16041   tree subtype;
16042
16043   STRIP_NOPS (sub);
16044   subtype = TREE_TYPE (sub);
16045   if (!POINTER_TYPE_P (subtype))
16046     return NULL_TREE;
16047
16048   if (TREE_CODE (sub) == ADDR_EXPR)
16049     {
16050       tree op = TREE_OPERAND (sub, 0);
16051       tree optype = TREE_TYPE (op);
16052       /* *&CONST_DECL -> to the value of the const decl.  */
16053       if (TREE_CODE (op) == CONST_DECL)
16054         return DECL_INITIAL (op);
16055       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
16056       if (type == optype)
16057         {
16058           tree fop = fold_read_from_constant_string (op);
16059           if (fop)
16060             return fop;
16061           else
16062             return op;
16063         }
16064       /* *(foo *)&fooarray => fooarray[0] */
16065       else if (TREE_CODE (optype) == ARRAY_TYPE
16066                && type == TREE_TYPE (optype))
16067         {
16068           tree type_domain = TYPE_DOMAIN (optype);
16069           tree min_val = size_zero_node;
16070           if (type_domain && TYPE_MIN_VALUE (type_domain))
16071             min_val = TYPE_MIN_VALUE (type_domain);
16072           op0 = build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
16073           SET_EXPR_LOCATION (op0, loc);
16074           return op0;
16075         }
16076       /* *(foo *)&complexfoo => __real__ complexfoo */
16077       else if (TREE_CODE (optype) == COMPLEX_TYPE
16078                && type == TREE_TYPE (optype))
16079         return fold_build1_loc (loc, REALPART_EXPR, type, op);
16080       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16081       else if (TREE_CODE (optype) == VECTOR_TYPE
16082                && type == TREE_TYPE (optype))
16083         {
16084           tree part_width = TYPE_SIZE (type);
16085           tree index = bitsize_int (0);
16086           return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
16087         }
16088     }
16089
16090   /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16091   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16092       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
16093     {
16094       tree op00 = TREE_OPERAND (sub, 0);
16095       tree op01 = TREE_OPERAND (sub, 1);
16096       tree op00type;
16097
16098       STRIP_NOPS (op00);
16099       op00type = TREE_TYPE (op00);
16100       if (TREE_CODE (op00) == ADDR_EXPR
16101           && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
16102           && type == TREE_TYPE (TREE_TYPE (op00type)))
16103         {
16104           HOST_WIDE_INT offset = tree_low_cst (op01, 0);
16105           tree part_width = TYPE_SIZE (type);
16106           unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
16107           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
16108           tree index = bitsize_int (indexi);
16109
16110           if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
16111             return fold_build3_loc (loc,
16112                                 BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
16113                                 part_width, index);
16114
16115         }
16116     }
16117
16118
16119   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16120   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16121       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
16122     {
16123       tree op00 = TREE_OPERAND (sub, 0);
16124       tree op01 = TREE_OPERAND (sub, 1);
16125       tree op00type;
16126
16127       STRIP_NOPS (op00);
16128       op00type = TREE_TYPE (op00);
16129       if (TREE_CODE (op00) == ADDR_EXPR
16130           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
16131           && type == TREE_TYPE (TREE_TYPE (op00type)))
16132         {
16133           tree size = TYPE_SIZE_UNIT (type);
16134           if (tree_int_cst_equal (size, op01))
16135             return fold_build1_loc (loc, IMAGPART_EXPR, type,
16136                                 TREE_OPERAND (op00, 0));
16137         }
16138     }
16139
16140   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16141   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16142       && type == TREE_TYPE (TREE_TYPE (subtype)))
16143     {
16144       tree type_domain;
16145       tree min_val = size_zero_node;
16146       sub = build_fold_indirect_ref_loc (loc, sub);
16147       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16148       if (type_domain && TYPE_MIN_VALUE (type_domain))
16149         min_val = TYPE_MIN_VALUE (type_domain);
16150       op0 = build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
16151       SET_EXPR_LOCATION (op0, loc);
16152       return op0;
16153     }
16154
16155   return NULL_TREE;
16156 }
16157
16158 /* Builds an expression for an indirection through T, simplifying some
16159    cases.  */
16160
16161 tree
16162 build_fold_indirect_ref_loc (location_t loc, tree t)
16163 {
16164   tree type = TREE_TYPE (TREE_TYPE (t));
16165   tree sub = fold_indirect_ref_1 (loc, type, t);
16166
16167   if (sub)
16168     return sub;
16169
16170   t = build1 (INDIRECT_REF, type, t);
16171   SET_EXPR_LOCATION (t, loc);
16172   return t;
16173 }
16174
16175 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
16176
16177 tree
16178 fold_indirect_ref_loc (location_t loc, tree t)
16179 {
16180   tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16181
16182   if (sub)
16183     return sub;
16184   else
16185     return t;
16186 }
16187
16188 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16189    whose result is ignored.  The type of the returned tree need not be
16190    the same as the original expression.  */
16191
16192 tree
16193 fold_ignored_result (tree t)
16194 {
16195   if (!TREE_SIDE_EFFECTS (t))
16196     return integer_zero_node;
16197
16198   for (;;)
16199     switch (TREE_CODE_CLASS (TREE_CODE (t)))
16200       {
16201       case tcc_unary:
16202         t = TREE_OPERAND (t, 0);
16203         break;
16204
16205       case tcc_binary:
16206       case tcc_comparison:
16207         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16208           t = TREE_OPERAND (t, 0);
16209         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16210           t = TREE_OPERAND (t, 1);
16211         else
16212           return t;
16213         break;
16214
16215       case tcc_expression:
16216         switch (TREE_CODE (t))
16217           {
16218           case COMPOUND_EXPR:
16219             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16220               return t;
16221             t = TREE_OPERAND (t, 0);
16222             break;
16223
16224           case COND_EXPR:
16225             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16226                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16227               return t;
16228             t = TREE_OPERAND (t, 0);
16229             break;
16230
16231           default:
16232             return t;
16233           }
16234         break;
16235
16236       default:
16237         return t;
16238       }
16239 }
16240
16241 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
16242    This can only be applied to objects of a sizetype.  */
16243
16244 tree
16245 round_up_loc (location_t loc, tree value, int divisor)
16246 {
16247   tree div = NULL_TREE;
16248
16249   gcc_assert (divisor > 0);
16250   if (divisor == 1)
16251     return value;
16252
16253   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
16254      have to do anything.  Only do this when we are not given a const,
16255      because in that case, this check is more expensive than just
16256      doing it.  */
16257   if (TREE_CODE (value) != INTEGER_CST)
16258     {
16259       div = build_int_cst (TREE_TYPE (value), divisor);
16260
16261       if (multiple_of_p (TREE_TYPE (value), value, div))
16262         return value;
16263     }
16264
16265   /* If divisor is a power of two, simplify this to bit manipulation.  */
16266   if (divisor == (divisor & -divisor))
16267     {
16268       if (TREE_CODE (value) == INTEGER_CST)
16269         {
16270           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
16271           unsigned HOST_WIDE_INT high;
16272           bool overflow_p;
16273
16274           if ((low & (divisor - 1)) == 0)
16275             return value;
16276
16277           overflow_p = TREE_OVERFLOW (value);
16278           high = TREE_INT_CST_HIGH (value);
16279           low &= ~(divisor - 1);
16280           low += divisor;
16281           if (low == 0)
16282             {
16283               high++;
16284               if (high == 0)
16285                 overflow_p = true;
16286             }
16287
16288           return force_fit_type_double (TREE_TYPE (value), low, high,
16289                                         -1, overflow_p);
16290         }
16291       else
16292         {
16293           tree t;
16294
16295           t = build_int_cst (TREE_TYPE (value), divisor - 1);
16296           value = size_binop_loc (loc, PLUS_EXPR, value, t);
16297           t = build_int_cst (TREE_TYPE (value), -divisor);
16298           value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16299         }
16300     }
16301   else
16302     {
16303       if (!div)
16304         div = build_int_cst (TREE_TYPE (value), divisor);
16305       value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16306       value = size_binop_loc (loc, MULT_EXPR, value, div);
16307     }
16308
16309   return value;
16310 }
16311
16312 /* Likewise, but round down.  */
16313
16314 tree
16315 round_down_loc (location_t loc, tree value, int divisor)
16316 {
16317   tree div = NULL_TREE;
16318
16319   gcc_assert (divisor > 0);
16320   if (divisor == 1)
16321     return value;
16322
16323   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
16324      have to do anything.  Only do this when we are not given a const,
16325      because in that case, this check is more expensive than just
16326      doing it.  */
16327   if (TREE_CODE (value) != INTEGER_CST)
16328     {
16329       div = build_int_cst (TREE_TYPE (value), divisor);
16330
16331       if (multiple_of_p (TREE_TYPE (value), value, div))
16332         return value;
16333     }
16334
16335   /* If divisor is a power of two, simplify this to bit manipulation.  */
16336   if (divisor == (divisor & -divisor))
16337     {
16338       tree t;
16339
16340       t = build_int_cst (TREE_TYPE (value), -divisor);
16341       value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16342     }
16343   else
16344     {
16345       if (!div)
16346         div = build_int_cst (TREE_TYPE (value), divisor);
16347       value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16348       value = size_binop_loc (loc, MULT_EXPR, value, div);
16349     }
16350
16351   return value;
16352 }
16353
16354 /* Returns the pointer to the base of the object addressed by EXP and
16355    extracts the information about the offset of the access, storing it
16356    to PBITPOS and POFFSET.  */
16357
16358 static tree
16359 split_address_to_core_and_offset (tree exp,
16360                                   HOST_WIDE_INT *pbitpos, tree *poffset)
16361 {
16362   tree core;
16363   enum machine_mode mode;
16364   int unsignedp, volatilep;
16365   HOST_WIDE_INT bitsize;
16366   location_t loc = EXPR_LOCATION (exp);
16367
16368   if (TREE_CODE (exp) == ADDR_EXPR)
16369     {
16370       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16371                                   poffset, &mode, &unsignedp, &volatilep,
16372                                   false);
16373       core = build_fold_addr_expr_loc (loc, core);
16374     }
16375   else
16376     {
16377       core = exp;
16378       *pbitpos = 0;
16379       *poffset = NULL_TREE;
16380     }
16381
16382   return core;
16383 }
16384
16385 /* Returns true if addresses of E1 and E2 differ by a constant, false
16386    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
16387
16388 bool
16389 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
16390 {
16391   tree core1, core2;
16392   HOST_WIDE_INT bitpos1, bitpos2;
16393   tree toffset1, toffset2, tdiff, type;
16394
16395   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16396   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16397
16398   if (bitpos1 % BITS_PER_UNIT != 0
16399       || bitpos2 % BITS_PER_UNIT != 0
16400       || !operand_equal_p (core1, core2, 0))
16401     return false;
16402
16403   if (toffset1 && toffset2)
16404     {
16405       type = TREE_TYPE (toffset1);
16406       if (type != TREE_TYPE (toffset2))
16407         toffset2 = fold_convert (type, toffset2);
16408
16409       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16410       if (!cst_and_fits_in_hwi (tdiff))
16411         return false;
16412
16413       *diff = int_cst_value (tdiff);
16414     }
16415   else if (toffset1 || toffset2)
16416     {
16417       /* If only one of the offsets is non-constant, the difference cannot
16418          be a constant.  */
16419       return false;
16420     }
16421   else
16422     *diff = 0;
16423
16424   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
16425   return true;
16426 }
16427
16428 /* Simplify the floating point expression EXP when the sign of the
16429    result is not significant.  Return NULL_TREE if no simplification
16430    is possible.  */
16431
16432 tree
16433 fold_strip_sign_ops (tree exp)
16434 {
16435   tree arg0, arg1;
16436   location_t loc = EXPR_LOCATION (exp);
16437
16438   switch (TREE_CODE (exp))
16439     {
16440     case ABS_EXPR:
16441     case NEGATE_EXPR:
16442       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16443       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
16444
16445     case MULT_EXPR:
16446     case RDIV_EXPR:
16447       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
16448         return NULL_TREE;
16449       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16450       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16451       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
16452         return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
16453                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
16454                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
16455       break;
16456
16457     case COMPOUND_EXPR:
16458       arg0 = TREE_OPERAND (exp, 0);
16459       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16460       if (arg1)
16461         return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16462       break;
16463
16464     case COND_EXPR:
16465       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16466       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16467       if (arg0 || arg1)
16468         return fold_build3_loc (loc,
16469                             COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16470                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
16471                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
16472       break;
16473
16474     case CALL_EXPR:
16475       {
16476         const enum built_in_function fcode = builtin_mathfn_code (exp);
16477         switch (fcode)
16478         {
16479         CASE_FLT_FN (BUILT_IN_COPYSIGN):
16480           /* Strip copysign function call, return the 1st argument. */
16481           arg0 = CALL_EXPR_ARG (exp, 0);
16482           arg1 = CALL_EXPR_ARG (exp, 1);
16483           return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16484
16485         default:
16486           /* Strip sign ops from the argument of "odd" math functions.  */
16487           if (negate_mathfn_p (fcode))
16488             {
16489               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16490               if (arg0)
16491                 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
16492             }
16493           break;
16494         }
16495       }
16496       break;
16497
16498     default:
16499       break;
16500     }
16501   return NULL_TREE;
16502 }