OSDN Git Service

2009-12-02 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / fold-const.c
1 /* Fold a constant sub-tree into a single node for C-compiler
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /*@@ This file should be rewritten to use an arbitrary precision
23   @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24   @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25   @@ The routines that translate from the ap rep should
26   @@ warn if precision et. al. is lost.
27   @@ This would also make life easier when this technology is used
28   @@ for cross-compilers.  */
29
30 /* The entry points in this file are fold, size_int_wide, size_binop
31    and force_fit_type_double.
32
33    fold takes a tree as argument and returns a simplified tree.
34
35    size_binop takes a tree code for an arithmetic operation
36    and two operands that are trees, and produces a tree for the
37    result, assuming the type comes from `sizetype'.
38
39    size_int takes an integer value, and creates a tree constant
40    with type from `sizetype'.
41
42    force_fit_type_double takes a constant, an overflowable flag and a
43    prior overflow indicator.  It forces the value to fit the type and
44    sets TREE_OVERFLOW.
45
46    Note: Since the folders get called on non-gimple code as well as
47    gimple code, we need to handle GIMPLE tuples as well as their
48    corresponding tree equivalents.  */
49
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "flags.h"
55 #include "tree.h"
56 #include "real.h"
57 #include "fixed-value.h"
58 #include "rtl.h"
59 #include "expr.h"
60 #include "tm_p.h"
61 #include "target.h"
62 #include "toplev.h"
63 #include "intl.h"
64 #include "ggc.h"
65 #include "hashtab.h"
66 #include "langhooks.h"
67 #include "md5.h"
68 #include "gimple.h"
69
70 /* Nonzero if we are folding constants inside an initializer; zero
71    otherwise.  */
72 int folding_initializer = 0;
73
74 /* The following constants represent a bit based encoding of GCC's
75    comparison operators.  This encoding simplifies transformations
76    on relational comparison operators, such as AND and OR.  */
77 enum comparison_code {
78   COMPCODE_FALSE = 0,
79   COMPCODE_LT = 1,
80   COMPCODE_EQ = 2,
81   COMPCODE_LE = 3,
82   COMPCODE_GT = 4,
83   COMPCODE_LTGT = 5,
84   COMPCODE_GE = 6,
85   COMPCODE_ORD = 7,
86   COMPCODE_UNORD = 8,
87   COMPCODE_UNLT = 9,
88   COMPCODE_UNEQ = 10,
89   COMPCODE_UNLE = 11,
90   COMPCODE_UNGT = 12,
91   COMPCODE_NE = 13,
92   COMPCODE_UNGE = 14,
93   COMPCODE_TRUE = 15
94 };
95
96 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
97 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
98 static bool negate_mathfn_p (enum built_in_function);
99 static bool negate_expr_p (tree);
100 static tree negate_expr (tree);
101 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
102 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
103 static tree const_binop (enum tree_code, tree, tree, int);
104 static enum comparison_code comparison_to_compcode (enum tree_code);
105 static enum tree_code compcode_to_comparison (enum comparison_code);
106 static int operand_equal_for_comparison_p (tree, tree, tree);
107 static int twoval_comparison_p (tree, tree *, tree *, int *);
108 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
109 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
110 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
111 static tree make_bit_field_ref (location_t, tree, tree,
112                                 HOST_WIDE_INT, HOST_WIDE_INT, int);
113 static tree optimize_bit_field_compare (location_t, enum tree_code,
114                                         tree, tree, tree);
115 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
116                                     HOST_WIDE_INT *,
117                                     enum machine_mode *, int *, int *,
118                                     tree *, tree *);
119 static int all_ones_mask_p (const_tree, int);
120 static tree sign_bit_p (tree, const_tree);
121 static int simple_operand_p (const_tree);
122 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
123 static tree range_predecessor (tree);
124 static tree range_successor (tree);
125 extern tree make_range (tree, int *, tree *, tree *, bool *);
126 extern bool merge_ranges (int *, tree *, tree *, int, tree, tree, int,
127                           tree, tree);
128 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
129 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
130 static tree unextend (tree, int, int, tree);
131 static tree fold_truthop (location_t, enum tree_code, tree, tree, tree);
132 static tree optimize_minmax_comparison (location_t, enum tree_code,
133                                         tree, tree, tree);
134 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
135 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
136 static tree fold_binary_op_with_conditional_arg (location_t,
137                                                  enum tree_code, tree,
138                                                  tree, tree,
139                                                  tree, tree, int);
140 static tree fold_mathfn_compare (location_t,
141                                  enum built_in_function, enum tree_code,
142                                  tree, tree, tree);
143 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
144 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
145 static bool reorder_operands_p (const_tree, const_tree);
146 static tree fold_negate_const (tree, tree);
147 static tree fold_not_const (tree, tree);
148 static tree fold_relational_const (enum tree_code, tree, tree, tree);
149 static tree fold_convert_const (enum tree_code, tree, tree);
150
151
152 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
153    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
154    and SUM1.  Then this yields nonzero if overflow occurred during the
155    addition.
156
157    Overflow occurs if A and B have the same sign, but A and SUM differ in
158    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
159    sign.  */
160 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
161 \f
162 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
163    We do that by representing the two-word integer in 4 words, with only
164    HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
165    number.  The value of the word is LOWPART + HIGHPART * BASE.  */
166
167 #define LOWPART(x) \
168   ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
169 #define HIGHPART(x) \
170   ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
171 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
172
173 /* Unpack a two-word integer into 4 words.
174    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
175    WORDS points to the array of HOST_WIDE_INTs.  */
176
177 static void
178 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
179 {
180   words[0] = LOWPART (low);
181   words[1] = HIGHPART (low);
182   words[2] = LOWPART (hi);
183   words[3] = HIGHPART (hi);
184 }
185
186 /* Pack an array of 4 words into a two-word integer.
187    WORDS points to the array of words.
188    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
189
190 static void
191 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
192         HOST_WIDE_INT *hi)
193 {
194   *low = words[0] + words[1] * BASE;
195   *hi = words[2] + words[3] * BASE;
196 }
197 \f
198 /* Force the double-word integer L1, H1 to be within the range of the
199    integer type TYPE.  Stores the properly truncated and sign-extended
200    double-word integer in *LV, *HV.  Returns true if the operation
201    overflows, that is, argument and result are different.  */
202
203 int
204 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
205                  unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
206 {
207   unsigned HOST_WIDE_INT low0 = l1;
208   HOST_WIDE_INT high0 = h1;
209   unsigned int prec = TYPE_PRECISION (type);
210   int sign_extended_type;
211
212   /* Size types *are* sign extended.  */
213   sign_extended_type = (!TYPE_UNSIGNED (type)
214                         || (TREE_CODE (type) == INTEGER_TYPE
215                             && TYPE_IS_SIZETYPE (type)));
216
217   /* First clear all bits that are beyond the type's precision.  */
218   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
219     ;
220   else if (prec > HOST_BITS_PER_WIDE_INT)
221     h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
222   else
223     {
224       h1 = 0;
225       if (prec < HOST_BITS_PER_WIDE_INT)
226         l1 &= ~((HOST_WIDE_INT) (-1) << prec);
227     }
228
229   /* Then do sign extension if necessary.  */
230   if (!sign_extended_type)
231     /* No sign extension */;
232   else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
233     /* Correct width already.  */;
234   else if (prec > HOST_BITS_PER_WIDE_INT)
235     {
236       /* Sign extend top half? */
237       if (h1 & ((unsigned HOST_WIDE_INT)1
238                 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
239         h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
240     }
241   else if (prec == HOST_BITS_PER_WIDE_INT)
242     {
243       if ((HOST_WIDE_INT)l1 < 0)
244         h1 = -1;
245     }
246   else
247     {
248       /* Sign extend bottom half? */
249       if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
250         {
251           h1 = -1;
252           l1 |= (HOST_WIDE_INT)(-1) << prec;
253         }
254     }
255
256   *lv = l1;
257   *hv = h1;
258
259   /* If the value didn't fit, signal overflow.  */
260   return l1 != low0 || h1 != high0;
261 }
262
263 /* We force the double-int HIGH:LOW to the range of the type TYPE by
264    sign or zero extending it.
265    OVERFLOWABLE indicates if we are interested
266    in overflow of the value, when >0 we are only interested in signed
267    overflow, for <0 we are interested in any overflow.  OVERFLOWED
268    indicates whether overflow has already occurred.  CONST_OVERFLOWED
269    indicates whether constant overflow has already occurred.  We force
270    T's value to be within range of T's type (by setting to 0 or 1 all
271    the bits outside the type's range).  We set TREE_OVERFLOWED if,
272         OVERFLOWED is nonzero,
273         or OVERFLOWABLE is >0 and signed overflow occurs
274         or OVERFLOWABLE is <0 and any overflow occurs
275    We return a new tree node for the extended double-int.  The node
276    is shared if no overflow flags are set.  */
277
278 tree
279 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
280                        HOST_WIDE_INT high, int overflowable,
281                        bool overflowed)
282 {
283   int sign_extended_type;
284   bool overflow;
285
286   /* Size types *are* sign extended.  */
287   sign_extended_type = (!TYPE_UNSIGNED (type)
288                         || (TREE_CODE (type) == INTEGER_TYPE
289                             && TYPE_IS_SIZETYPE (type)));
290
291   overflow = fit_double_type (low, high, &low, &high, type);
292
293   /* If we need to set overflow flags, return a new unshared node.  */
294   if (overflowed || overflow)
295     {
296       if (overflowed
297           || overflowable < 0
298           || (overflowable > 0 && sign_extended_type))
299         {
300           tree t = make_node (INTEGER_CST);
301           TREE_INT_CST_LOW (t) = low;
302           TREE_INT_CST_HIGH (t) = high;
303           TREE_TYPE (t) = type;
304           TREE_OVERFLOW (t) = 1;
305           return t;
306         }
307     }
308
309   /* Else build a shared node.  */
310   return build_int_cst_wide (type, low, high);
311 }
312 \f
313 /* Add two doubleword integers with doubleword result.
314    Return nonzero if the operation overflows according to UNSIGNED_P.
315    Each argument is given as two `HOST_WIDE_INT' pieces.
316    One argument is L1 and H1; the other, L2 and H2.
317    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
318
319 int
320 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
321                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
322                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
323                       bool unsigned_p)
324 {
325   unsigned HOST_WIDE_INT l;
326   HOST_WIDE_INT h;
327
328   l = l1 + l2;
329   h = h1 + h2 + (l < l1);
330
331   *lv = l;
332   *hv = h;
333
334   if (unsigned_p)
335     return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
336   else
337     return OVERFLOW_SUM_SIGN (h1, h2, h);
338 }
339
340 /* Negate a doubleword integer with doubleword result.
341    Return nonzero if the operation overflows, assuming it's signed.
342    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
343    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
344
345 int
346 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
347             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
348 {
349   if (l1 == 0)
350     {
351       *lv = 0;
352       *hv = - h1;
353       return (*hv & h1) < 0;
354     }
355   else
356     {
357       *lv = -l1;
358       *hv = ~h1;
359       return 0;
360     }
361 }
362 \f
363 /* Multiply two doubleword integers with doubleword result.
364    Return nonzero if the operation overflows according to UNSIGNED_P.
365    Each argument is given as two `HOST_WIDE_INT' pieces.
366    One argument is L1 and H1; the other, L2 and H2.
367    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
368
369 int
370 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
371                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
372                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
373                       bool unsigned_p)
374 {
375   HOST_WIDE_INT arg1[4];
376   HOST_WIDE_INT arg2[4];
377   HOST_WIDE_INT prod[4 * 2];
378   unsigned HOST_WIDE_INT carry;
379   int i, j, k;
380   unsigned HOST_WIDE_INT toplow, neglow;
381   HOST_WIDE_INT tophigh, neghigh;
382
383   encode (arg1, l1, h1);
384   encode (arg2, l2, h2);
385
386   memset (prod, 0, sizeof prod);
387
388   for (i = 0; i < 4; i++)
389     {
390       carry = 0;
391       for (j = 0; j < 4; j++)
392         {
393           k = i + j;
394           /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
395           carry += arg1[i] * arg2[j];
396           /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
397           carry += prod[k];
398           prod[k] = LOWPART (carry);
399           carry = HIGHPART (carry);
400         }
401       prod[i + 4] = carry;
402     }
403
404   decode (prod, lv, hv);
405   decode (prod + 4, &toplow, &tophigh);
406
407   /* Unsigned overflow is immediate.  */
408   if (unsigned_p)
409     return (toplow | tophigh) != 0;
410
411   /* Check for signed overflow by calculating the signed representation of the
412      top half of the result; it should agree with the low half's sign bit.  */
413   if (h1 < 0)
414     {
415       neg_double (l2, h2, &neglow, &neghigh);
416       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
417     }
418   if (h2 < 0)
419     {
420       neg_double (l1, h1, &neglow, &neghigh);
421       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
422     }
423   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
424 }
425 \f
426 /* Shift the doubleword integer in L1, H1 left by COUNT places
427    keeping only PREC bits of result.
428    Shift right if COUNT is negative.
429    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
430    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
431
432 void
433 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
434                HOST_WIDE_INT count, unsigned int prec,
435                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
436 {
437   unsigned HOST_WIDE_INT signmask;
438
439   if (count < 0)
440     {
441       rshift_double (l1, h1, -count, prec, lv, hv, arith);
442       return;
443     }
444
445   if (SHIFT_COUNT_TRUNCATED)
446     count %= prec;
447
448   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
449     {
450       /* Shifting by the host word size is undefined according to the
451          ANSI standard, so we must handle this as a special case.  */
452       *hv = 0;
453       *lv = 0;
454     }
455   else if (count >= HOST_BITS_PER_WIDE_INT)
456     {
457       *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
458       *lv = 0;
459     }
460   else
461     {
462       *hv = (((unsigned HOST_WIDE_INT) h1 << count)
463              | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
464       *lv = l1 << count;
465     }
466
467   /* Sign extend all bits that are beyond the precision.  */
468
469   signmask = -((prec > HOST_BITS_PER_WIDE_INT
470                 ? ((unsigned HOST_WIDE_INT) *hv
471                    >> (prec - HOST_BITS_PER_WIDE_INT - 1))
472                 : (*lv >> (prec - 1))) & 1);
473
474   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
475     ;
476   else if (prec >= HOST_BITS_PER_WIDE_INT)
477     {
478       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
479       *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
480     }
481   else
482     {
483       *hv = signmask;
484       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
485       *lv |= signmask << prec;
486     }
487 }
488
489 /* Shift the doubleword integer in L1, H1 right by COUNT places
490    keeping only PREC bits of result.  COUNT must be positive.
491    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
492    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
493
494 void
495 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
496                HOST_WIDE_INT count, unsigned int prec,
497                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
498                int arith)
499 {
500   unsigned HOST_WIDE_INT signmask;
501
502   signmask = (arith
503               ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
504               : 0);
505
506   if (SHIFT_COUNT_TRUNCATED)
507     count %= prec;
508
509   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
510     {
511       /* Shifting by the host word size is undefined according to the
512          ANSI standard, so we must handle this as a special case.  */
513       *hv = 0;
514       *lv = 0;
515     }
516   else if (count >= HOST_BITS_PER_WIDE_INT)
517     {
518       *hv = 0;
519       *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
520     }
521   else
522     {
523       *hv = (unsigned HOST_WIDE_INT) h1 >> count;
524       *lv = ((l1 >> count)
525              | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
526     }
527
528   /* Zero / sign extend all bits that are beyond the precision.  */
529
530   if (count >= (HOST_WIDE_INT)prec)
531     {
532       *hv = signmask;
533       *lv = signmask;
534     }
535   else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
536     ;
537   else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
538     {
539       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
540       *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
541     }
542   else
543     {
544       *hv = signmask;
545       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
546       *lv |= signmask << (prec - count);
547     }
548 }
549 \f
550 /* Rotate the doubleword integer in L1, H1 left by COUNT places
551    keeping only PREC bits of result.
552    Rotate right if COUNT is negative.
553    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
554
555 void
556 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
557                 HOST_WIDE_INT count, unsigned int prec,
558                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
559 {
560   unsigned HOST_WIDE_INT s1l, s2l;
561   HOST_WIDE_INT s1h, s2h;
562
563   count %= prec;
564   if (count < 0)
565     count += prec;
566
567   lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
568   rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
569   *lv = s1l | s2l;
570   *hv = s1h | s2h;
571 }
572
573 /* Rotate the doubleword integer in L1, H1 left by COUNT places
574    keeping only PREC bits of result.  COUNT must be positive.
575    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
576
577 void
578 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
579                 HOST_WIDE_INT count, unsigned int prec,
580                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
581 {
582   unsigned HOST_WIDE_INT s1l, s2l;
583   HOST_WIDE_INT s1h, s2h;
584
585   count %= prec;
586   if (count < 0)
587     count += prec;
588
589   rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
590   lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
591   *lv = s1l | s2l;
592   *hv = s1h | s2h;
593 }
594 \f
595 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
596    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
597    CODE is a tree code for a kind of division, one of
598    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
599    or EXACT_DIV_EXPR
600    It controls how the quotient is rounded to an integer.
601    Return nonzero if the operation overflows.
602    UNS nonzero says do unsigned division.  */
603
604 int
605 div_and_round_double (enum tree_code code, int uns,
606                       unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
607                       HOST_WIDE_INT hnum_orig,
608                       unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
609                       HOST_WIDE_INT hden_orig,
610                       unsigned HOST_WIDE_INT *lquo,
611                       HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
612                       HOST_WIDE_INT *hrem)
613 {
614   int quo_neg = 0;
615   HOST_WIDE_INT num[4 + 1];     /* extra element for scaling.  */
616   HOST_WIDE_INT den[4], quo[4];
617   int i, j;
618   unsigned HOST_WIDE_INT work;
619   unsigned HOST_WIDE_INT carry = 0;
620   unsigned HOST_WIDE_INT lnum = lnum_orig;
621   HOST_WIDE_INT hnum = hnum_orig;
622   unsigned HOST_WIDE_INT lden = lden_orig;
623   HOST_WIDE_INT hden = hden_orig;
624   int overflow = 0;
625
626   if (hden == 0 && lden == 0)
627     overflow = 1, lden = 1;
628
629   /* Calculate quotient sign and convert operands to unsigned.  */
630   if (!uns)
631     {
632       if (hnum < 0)
633         {
634           quo_neg = ~ quo_neg;
635           /* (minimum integer) / (-1) is the only overflow case.  */
636           if (neg_double (lnum, hnum, &lnum, &hnum)
637               && ((HOST_WIDE_INT) lden & hden) == -1)
638             overflow = 1;
639         }
640       if (hden < 0)
641         {
642           quo_neg = ~ quo_neg;
643           neg_double (lden, hden, &lden, &hden);
644         }
645     }
646
647   if (hnum == 0 && hden == 0)
648     {                           /* single precision */
649       *hquo = *hrem = 0;
650       /* This unsigned division rounds toward zero.  */
651       *lquo = lnum / lden;
652       goto finish_up;
653     }
654
655   if (hnum == 0)
656     {                           /* trivial case: dividend < divisor */
657       /* hden != 0 already checked.  */
658       *hquo = *lquo = 0;
659       *hrem = hnum;
660       *lrem = lnum;
661       goto finish_up;
662     }
663
664   memset (quo, 0, sizeof quo);
665
666   memset (num, 0, sizeof num);  /* to zero 9th element */
667   memset (den, 0, sizeof den);
668
669   encode (num, lnum, hnum);
670   encode (den, lden, hden);
671
672   /* Special code for when the divisor < BASE.  */
673   if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
674     {
675       /* hnum != 0 already checked.  */
676       for (i = 4 - 1; i >= 0; i--)
677         {
678           work = num[i] + carry * BASE;
679           quo[i] = work / lden;
680           carry = work % lden;
681         }
682     }
683   else
684     {
685       /* Full double precision division,
686          with thanks to Don Knuth's "Seminumerical Algorithms".  */
687       int num_hi_sig, den_hi_sig;
688       unsigned HOST_WIDE_INT quo_est, scale;
689
690       /* Find the highest nonzero divisor digit.  */
691       for (i = 4 - 1;; i--)
692         if (den[i] != 0)
693           {
694             den_hi_sig = i;
695             break;
696           }
697
698       /* Insure that the first digit of the divisor is at least BASE/2.
699          This is required by the quotient digit estimation algorithm.  */
700
701       scale = BASE / (den[den_hi_sig] + 1);
702       if (scale > 1)
703         {               /* scale divisor and dividend */
704           carry = 0;
705           for (i = 0; i <= 4 - 1; i++)
706             {
707               work = (num[i] * scale) + carry;
708               num[i] = LOWPART (work);
709               carry = HIGHPART (work);
710             }
711
712           num[4] = carry;
713           carry = 0;
714           for (i = 0; i <= 4 - 1; i++)
715             {
716               work = (den[i] * scale) + carry;
717               den[i] = LOWPART (work);
718               carry = HIGHPART (work);
719               if (den[i] != 0) den_hi_sig = i;
720             }
721         }
722
723       num_hi_sig = 4;
724
725       /* Main loop */
726       for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
727         {
728           /* Guess the next quotient digit, quo_est, by dividing the first
729              two remaining dividend digits by the high order quotient digit.
730              quo_est is never low and is at most 2 high.  */
731           unsigned HOST_WIDE_INT tmp;
732
733           num_hi_sig = i + den_hi_sig + 1;
734           work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
735           if (num[num_hi_sig] != den[den_hi_sig])
736             quo_est = work / den[den_hi_sig];
737           else
738             quo_est = BASE - 1;
739
740           /* Refine quo_est so it's usually correct, and at most one high.  */
741           tmp = work - quo_est * den[den_hi_sig];
742           if (tmp < BASE
743               && (den[den_hi_sig - 1] * quo_est
744                   > (tmp * BASE + num[num_hi_sig - 2])))
745             quo_est--;
746
747           /* Try QUO_EST as the quotient digit, by multiplying the
748              divisor by QUO_EST and subtracting from the remaining dividend.
749              Keep in mind that QUO_EST is the I - 1st digit.  */
750
751           carry = 0;
752           for (j = 0; j <= den_hi_sig; j++)
753             {
754               work = quo_est * den[j] + carry;
755               carry = HIGHPART (work);
756               work = num[i + j] - LOWPART (work);
757               num[i + j] = LOWPART (work);
758               carry += HIGHPART (work) != 0;
759             }
760
761           /* If quo_est was high by one, then num[i] went negative and
762              we need to correct things.  */
763           if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
764             {
765               quo_est--;
766               carry = 0;                /* add divisor back in */
767               for (j = 0; j <= den_hi_sig; j++)
768                 {
769                   work = num[i + j] + den[j] + carry;
770                   carry = HIGHPART (work);
771                   num[i + j] = LOWPART (work);
772                 }
773
774               num [num_hi_sig] += carry;
775             }
776
777           /* Store the quotient digit.  */
778           quo[i] = quo_est;
779         }
780     }
781
782   decode (quo, lquo, hquo);
783
784  finish_up:
785   /* If result is negative, make it so.  */
786   if (quo_neg)
787     neg_double (*lquo, *hquo, lquo, hquo);
788
789   /* Compute trial remainder:  rem = num - (quo * den)  */
790   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
791   neg_double (*lrem, *hrem, lrem, hrem);
792   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
793
794   switch (code)
795     {
796     case TRUNC_DIV_EXPR:
797     case TRUNC_MOD_EXPR:        /* round toward zero */
798     case EXACT_DIV_EXPR:        /* for this one, it shouldn't matter */
799       return overflow;
800
801     case FLOOR_DIV_EXPR:
802     case FLOOR_MOD_EXPR:        /* round toward negative infinity */
803       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
804         {
805           /* quo = quo - 1;  */
806           add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
807                       lquo, hquo);
808         }
809       else
810         return overflow;
811       break;
812
813     case CEIL_DIV_EXPR:
814     case CEIL_MOD_EXPR:         /* round toward positive infinity */
815       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
816         {
817           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
818                       lquo, hquo);
819         }
820       else
821         return overflow;
822       break;
823
824     case ROUND_DIV_EXPR:
825     case ROUND_MOD_EXPR:        /* round to closest integer */
826       {
827         unsigned HOST_WIDE_INT labs_rem = *lrem;
828         HOST_WIDE_INT habs_rem = *hrem;
829         unsigned HOST_WIDE_INT labs_den = lden, ltwice;
830         HOST_WIDE_INT habs_den = hden, htwice;
831
832         /* Get absolute values.  */
833         if (*hrem < 0)
834           neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
835         if (hden < 0)
836           neg_double (lden, hden, &labs_den, &habs_den);
837
838         /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient.  */
839         mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
840                     labs_rem, habs_rem, &ltwice, &htwice);
841
842         if (((unsigned HOST_WIDE_INT) habs_den
843              < (unsigned HOST_WIDE_INT) htwice)
844             || (((unsigned HOST_WIDE_INT) habs_den
845                  == (unsigned HOST_WIDE_INT) htwice)
846                 && (labs_den <= ltwice)))
847           {
848             if (*hquo < 0)
849               /* quo = quo - 1;  */
850               add_double (*lquo, *hquo,
851                           (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
852             else
853               /* quo = quo + 1; */
854               add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
855                           lquo, hquo);
856           }
857         else
858           return overflow;
859       }
860       break;
861
862     default:
863       gcc_unreachable ();
864     }
865
866   /* Compute true remainder:  rem = num - (quo * den)  */
867   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
868   neg_double (*lrem, *hrem, lrem, hrem);
869   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
870   return overflow;
871 }
872
873 /* If ARG2 divides ARG1 with zero remainder, carries out the division
874    of type CODE and returns the quotient.
875    Otherwise returns NULL_TREE.  */
876
877 tree
878 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
879 {
880   unsigned HOST_WIDE_INT int1l, int2l;
881   HOST_WIDE_INT int1h, int2h;
882   unsigned HOST_WIDE_INT quol, reml;
883   HOST_WIDE_INT quoh, remh;
884   int uns;
885
886   /* The sign of the division is according to operand two, that
887      does the correct thing for POINTER_PLUS_EXPR where we want
888      a signed division.  */
889   uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
890   if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE
891       && TYPE_IS_SIZETYPE (TREE_TYPE (arg2)))
892     uns = false;
893
894   int1l = TREE_INT_CST_LOW (arg1);
895   int1h = TREE_INT_CST_HIGH (arg1);
896   int2l = TREE_INT_CST_LOW (arg2);
897   int2h = TREE_INT_CST_HIGH (arg2);
898
899   div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
900                         &quol, &quoh, &reml, &remh);
901   if (remh != 0 || reml != 0)
902     return NULL_TREE;
903
904   return build_int_cst_wide (TREE_TYPE (arg1), quol, quoh);
905 }
906 \f
907 /* This is nonzero if we should defer warnings about undefined
908    overflow.  This facility exists because these warnings are a
909    special case.  The code to estimate loop iterations does not want
910    to issue any warnings, since it works with expressions which do not
911    occur in user code.  Various bits of cleanup code call fold(), but
912    only use the result if it has certain characteristics (e.g., is a
913    constant); that code only wants to issue a warning if the result is
914    used.  */
915
916 static int fold_deferring_overflow_warnings;
917
918 /* If a warning about undefined overflow is deferred, this is the
919    warning.  Note that this may cause us to turn two warnings into
920    one, but that is fine since it is sufficient to only give one
921    warning per expression.  */
922
923 static const char* fold_deferred_overflow_warning;
924
925 /* If a warning about undefined overflow is deferred, this is the
926    level at which the warning should be emitted.  */
927
928 static enum warn_strict_overflow_code fold_deferred_overflow_code;
929
930 /* Start deferring overflow warnings.  We could use a stack here to
931    permit nested calls, but at present it is not necessary.  */
932
933 void
934 fold_defer_overflow_warnings (void)
935 {
936   ++fold_deferring_overflow_warnings;
937 }
938
939 /* Stop deferring overflow warnings.  If there is a pending warning,
940    and ISSUE is true, then issue the warning if appropriate.  STMT is
941    the statement with which the warning should be associated (used for
942    location information); STMT may be NULL.  CODE is the level of the
943    warning--a warn_strict_overflow_code value.  This function will use
944    the smaller of CODE and the deferred code when deciding whether to
945    issue the warning.  CODE may be zero to mean to always use the
946    deferred code.  */
947
948 void
949 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
950 {
951   const char *warnmsg;
952   location_t locus;
953
954   gcc_assert (fold_deferring_overflow_warnings > 0);
955   --fold_deferring_overflow_warnings;
956   if (fold_deferring_overflow_warnings > 0)
957     {
958       if (fold_deferred_overflow_warning != NULL
959           && code != 0
960           && code < (int) fold_deferred_overflow_code)
961         fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
962       return;
963     }
964
965   warnmsg = fold_deferred_overflow_warning;
966   fold_deferred_overflow_warning = NULL;
967
968   if (!issue || warnmsg == NULL)
969     return;
970
971   if (gimple_no_warning_p (stmt))
972     return;
973
974   /* Use the smallest code level when deciding to issue the
975      warning.  */
976   if (code == 0 || code > (int) fold_deferred_overflow_code)
977     code = fold_deferred_overflow_code;
978
979   if (!issue_strict_overflow_warning (code))
980     return;
981
982   if (stmt == NULL)
983     locus = input_location;
984   else
985     locus = gimple_location (stmt);
986   warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
987 }
988
989 /* Stop deferring overflow warnings, ignoring any deferred
990    warnings.  */
991
992 void
993 fold_undefer_and_ignore_overflow_warnings (void)
994 {
995   fold_undefer_overflow_warnings (false, NULL, 0);
996 }
997
998 /* Whether we are deferring overflow warnings.  */
999
1000 bool
1001 fold_deferring_overflow_warnings_p (void)
1002 {
1003   return fold_deferring_overflow_warnings > 0;
1004 }
1005
1006 /* This is called when we fold something based on the fact that signed
1007    overflow is undefined.  */
1008
1009 static void
1010 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1011 {
1012   if (fold_deferring_overflow_warnings > 0)
1013     {
1014       if (fold_deferred_overflow_warning == NULL
1015           || wc < fold_deferred_overflow_code)
1016         {
1017           fold_deferred_overflow_warning = gmsgid;
1018           fold_deferred_overflow_code = wc;
1019         }
1020     }
1021   else if (issue_strict_overflow_warning (wc))
1022     warning (OPT_Wstrict_overflow, gmsgid);
1023 }
1024 \f
1025 /* Return true if the built-in mathematical function specified by CODE
1026    is odd, i.e. -f(x) == f(-x).  */
1027
1028 static bool
1029 negate_mathfn_p (enum built_in_function code)
1030 {
1031   switch (code)
1032     {
1033     CASE_FLT_FN (BUILT_IN_ASIN):
1034     CASE_FLT_FN (BUILT_IN_ASINH):
1035     CASE_FLT_FN (BUILT_IN_ATAN):
1036     CASE_FLT_FN (BUILT_IN_ATANH):
1037     CASE_FLT_FN (BUILT_IN_CASIN):
1038     CASE_FLT_FN (BUILT_IN_CASINH):
1039     CASE_FLT_FN (BUILT_IN_CATAN):
1040     CASE_FLT_FN (BUILT_IN_CATANH):
1041     CASE_FLT_FN (BUILT_IN_CBRT):
1042     CASE_FLT_FN (BUILT_IN_CPROJ):
1043     CASE_FLT_FN (BUILT_IN_CSIN):
1044     CASE_FLT_FN (BUILT_IN_CSINH):
1045     CASE_FLT_FN (BUILT_IN_CTAN):
1046     CASE_FLT_FN (BUILT_IN_CTANH):
1047     CASE_FLT_FN (BUILT_IN_ERF):
1048     CASE_FLT_FN (BUILT_IN_LLROUND):
1049     CASE_FLT_FN (BUILT_IN_LROUND):
1050     CASE_FLT_FN (BUILT_IN_ROUND):
1051     CASE_FLT_FN (BUILT_IN_SIN):
1052     CASE_FLT_FN (BUILT_IN_SINH):
1053     CASE_FLT_FN (BUILT_IN_TAN):
1054     CASE_FLT_FN (BUILT_IN_TANH):
1055     CASE_FLT_FN (BUILT_IN_TRUNC):
1056       return true;
1057
1058     CASE_FLT_FN (BUILT_IN_LLRINT):
1059     CASE_FLT_FN (BUILT_IN_LRINT):
1060     CASE_FLT_FN (BUILT_IN_NEARBYINT):
1061     CASE_FLT_FN (BUILT_IN_RINT):
1062       return !flag_rounding_math;
1063
1064     default:
1065       break;
1066     }
1067   return false;
1068 }
1069
1070 /* Check whether we may negate an integer constant T without causing
1071    overflow.  */
1072
1073 bool
1074 may_negate_without_overflow_p (const_tree t)
1075 {
1076   unsigned HOST_WIDE_INT val;
1077   unsigned int prec;
1078   tree type;
1079
1080   gcc_assert (TREE_CODE (t) == INTEGER_CST);
1081
1082   type = TREE_TYPE (t);
1083   if (TYPE_UNSIGNED (type))
1084     return false;
1085
1086   prec = TYPE_PRECISION (type);
1087   if (prec > HOST_BITS_PER_WIDE_INT)
1088     {
1089       if (TREE_INT_CST_LOW (t) != 0)
1090         return true;
1091       prec -= HOST_BITS_PER_WIDE_INT;
1092       val = TREE_INT_CST_HIGH (t);
1093     }
1094   else
1095     val = TREE_INT_CST_LOW (t);
1096   if (prec < HOST_BITS_PER_WIDE_INT)
1097     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1098   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1099 }
1100
1101 /* Determine whether an expression T can be cheaply negated using
1102    the function negate_expr without introducing undefined overflow.  */
1103
1104 static bool
1105 negate_expr_p (tree t)
1106 {
1107   tree type;
1108
1109   if (t == 0)
1110     return false;
1111
1112   type = TREE_TYPE (t);
1113
1114   STRIP_SIGN_NOPS (t);
1115   switch (TREE_CODE (t))
1116     {
1117     case INTEGER_CST:
1118       if (TYPE_OVERFLOW_WRAPS (type))
1119         return true;
1120
1121       /* Check that -CST will not overflow type.  */
1122       return may_negate_without_overflow_p (t);
1123     case BIT_NOT_EXPR:
1124       return (INTEGRAL_TYPE_P (type)
1125               && TYPE_OVERFLOW_WRAPS (type));
1126
1127     case FIXED_CST:
1128     case REAL_CST:
1129     case NEGATE_EXPR:
1130       return true;
1131
1132     case COMPLEX_CST:
1133       return negate_expr_p (TREE_REALPART (t))
1134              && negate_expr_p (TREE_IMAGPART (t));
1135
1136     case COMPLEX_EXPR:
1137       return negate_expr_p (TREE_OPERAND (t, 0))
1138              && negate_expr_p (TREE_OPERAND (t, 1));
1139
1140     case CONJ_EXPR:
1141       return negate_expr_p (TREE_OPERAND (t, 0));
1142
1143     case PLUS_EXPR:
1144       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1145           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1146         return false;
1147       /* -(A + B) -> (-B) - A.  */
1148       if (negate_expr_p (TREE_OPERAND (t, 1))
1149           && reorder_operands_p (TREE_OPERAND (t, 0),
1150                                  TREE_OPERAND (t, 1)))
1151         return true;
1152       /* -(A + B) -> (-A) - B.  */
1153       return negate_expr_p (TREE_OPERAND (t, 0));
1154
1155     case MINUS_EXPR:
1156       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
1157       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1158              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1159              && reorder_operands_p (TREE_OPERAND (t, 0),
1160                                     TREE_OPERAND (t, 1));
1161
1162     case MULT_EXPR:
1163       if (TYPE_UNSIGNED (TREE_TYPE (t)))
1164         break;
1165
1166       /* Fall through.  */
1167
1168     case RDIV_EXPR:
1169       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1170         return negate_expr_p (TREE_OPERAND (t, 1))
1171                || negate_expr_p (TREE_OPERAND (t, 0));
1172       break;
1173
1174     case TRUNC_DIV_EXPR:
1175     case ROUND_DIV_EXPR:
1176     case FLOOR_DIV_EXPR:
1177     case CEIL_DIV_EXPR:
1178     case EXACT_DIV_EXPR:
1179       /* In general we can't negate A / B, because if A is INT_MIN and
1180          B is 1, we may turn this into INT_MIN / -1 which is undefined
1181          and actually traps on some architectures.  But if overflow is
1182          undefined, we can negate, because - (INT_MIN / 1) is an
1183          overflow.  */
1184       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1185           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1186         break;
1187       return negate_expr_p (TREE_OPERAND (t, 1))
1188              || negate_expr_p (TREE_OPERAND (t, 0));
1189
1190     case NOP_EXPR:
1191       /* Negate -((double)float) as (double)(-float).  */
1192       if (TREE_CODE (type) == REAL_TYPE)
1193         {
1194           tree tem = strip_float_extensions (t);
1195           if (tem != t)
1196             return negate_expr_p (tem);
1197         }
1198       break;
1199
1200     case CALL_EXPR:
1201       /* Negate -f(x) as f(-x).  */
1202       if (negate_mathfn_p (builtin_mathfn_code (t)))
1203         return negate_expr_p (CALL_EXPR_ARG (t, 0));
1204       break;
1205
1206     case RSHIFT_EXPR:
1207       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1208       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1209         {
1210           tree op1 = TREE_OPERAND (t, 1);
1211           if (TREE_INT_CST_HIGH (op1) == 0
1212               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1213                  == TREE_INT_CST_LOW (op1))
1214             return true;
1215         }
1216       break;
1217
1218     default:
1219       break;
1220     }
1221   return false;
1222 }
1223
1224 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1225    simplification is possible.
1226    If negate_expr_p would return true for T, NULL_TREE will never be
1227    returned.  */
1228
1229 static tree
1230 fold_negate_expr (location_t loc, tree t)
1231 {
1232   tree type = TREE_TYPE (t);
1233   tree tem;
1234
1235   switch (TREE_CODE (t))
1236     {
1237     /* Convert - (~A) to A + 1.  */
1238     case BIT_NOT_EXPR:
1239       if (INTEGRAL_TYPE_P (type))
1240         return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
1241                             build_int_cst (type, 1));
1242       break;
1243
1244     case INTEGER_CST:
1245       tem = fold_negate_const (t, type);
1246       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1247           || !TYPE_OVERFLOW_TRAPS (type))
1248         return tem;
1249       break;
1250
1251     case REAL_CST:
1252       tem = fold_negate_const (t, type);
1253       /* Two's complement FP formats, such as c4x, may overflow.  */
1254       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1255         return tem;
1256       break;
1257
1258     case FIXED_CST:
1259       tem = fold_negate_const (t, type);
1260       return tem;
1261
1262     case COMPLEX_CST:
1263       {
1264         tree rpart = negate_expr (TREE_REALPART (t));
1265         tree ipart = negate_expr (TREE_IMAGPART (t));
1266
1267         if ((TREE_CODE (rpart) == REAL_CST
1268              && TREE_CODE (ipart) == REAL_CST)
1269             || (TREE_CODE (rpart) == INTEGER_CST
1270                 && TREE_CODE (ipart) == INTEGER_CST))
1271           return build_complex (type, rpart, ipart);
1272       }
1273       break;
1274
1275     case COMPLEX_EXPR:
1276       if (negate_expr_p (t))
1277         return fold_build2_loc (loc, COMPLEX_EXPR, type,
1278                             fold_negate_expr (loc, TREE_OPERAND (t, 0)),
1279                             fold_negate_expr (loc, TREE_OPERAND (t, 1)));
1280       break;
1281
1282     case CONJ_EXPR:
1283       if (negate_expr_p (t))
1284         return fold_build1_loc (loc, CONJ_EXPR, type,
1285                             fold_negate_expr (loc, TREE_OPERAND (t, 0)));
1286       break;
1287
1288     case NEGATE_EXPR:
1289       return TREE_OPERAND (t, 0);
1290
1291     case PLUS_EXPR:
1292       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1293           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1294         {
1295           /* -(A + B) -> (-B) - A.  */
1296           if (negate_expr_p (TREE_OPERAND (t, 1))
1297               && reorder_operands_p (TREE_OPERAND (t, 0),
1298                                      TREE_OPERAND (t, 1)))
1299             {
1300               tem = negate_expr (TREE_OPERAND (t, 1));
1301               return fold_build2_loc (loc, MINUS_EXPR, type,
1302                                   tem, TREE_OPERAND (t, 0));
1303             }
1304
1305           /* -(A + B) -> (-A) - B.  */
1306           if (negate_expr_p (TREE_OPERAND (t, 0)))
1307             {
1308               tem = negate_expr (TREE_OPERAND (t, 0));
1309               return fold_build2_loc (loc, MINUS_EXPR, type,
1310                                   tem, TREE_OPERAND (t, 1));
1311             }
1312         }
1313       break;
1314
1315     case MINUS_EXPR:
1316       /* - (A - B) -> B - A  */
1317       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1318           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1319           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1320         return fold_build2_loc (loc, MINUS_EXPR, type,
1321                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1322       break;
1323
1324     case MULT_EXPR:
1325       if (TYPE_UNSIGNED (type))
1326         break;
1327
1328       /* Fall through.  */
1329
1330     case RDIV_EXPR:
1331       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1332         {
1333           tem = TREE_OPERAND (t, 1);
1334           if (negate_expr_p (tem))
1335             return fold_build2_loc (loc, TREE_CODE (t), type,
1336                                 TREE_OPERAND (t, 0), negate_expr (tem));
1337           tem = TREE_OPERAND (t, 0);
1338           if (negate_expr_p (tem))
1339             return fold_build2_loc (loc, TREE_CODE (t), type,
1340                                 negate_expr (tem), TREE_OPERAND (t, 1));
1341         }
1342       break;
1343
1344     case TRUNC_DIV_EXPR:
1345     case ROUND_DIV_EXPR:
1346     case FLOOR_DIV_EXPR:
1347     case CEIL_DIV_EXPR:
1348     case EXACT_DIV_EXPR:
1349       /* In general we can't negate A / B, because if A is INT_MIN and
1350          B is 1, we may turn this into INT_MIN / -1 which is undefined
1351          and actually traps on some architectures.  But if overflow is
1352          undefined, we can negate, because - (INT_MIN / 1) is an
1353          overflow.  */
1354       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1355         {
1356           const char * const warnmsg = G_("assuming signed overflow does not "
1357                                           "occur when negating a division");
1358           tem = TREE_OPERAND (t, 1);
1359           if (negate_expr_p (tem))
1360             {
1361               if (INTEGRAL_TYPE_P (type)
1362                   && (TREE_CODE (tem) != INTEGER_CST
1363                       || integer_onep (tem)))
1364                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1365               return fold_build2_loc (loc, TREE_CODE (t), type,
1366                                   TREE_OPERAND (t, 0), negate_expr (tem));
1367             }
1368           tem = TREE_OPERAND (t, 0);
1369           if (negate_expr_p (tem))
1370             {
1371               if (INTEGRAL_TYPE_P (type)
1372                   && (TREE_CODE (tem) != INTEGER_CST
1373                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1374                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1375               return fold_build2_loc (loc, TREE_CODE (t), type,
1376                                   negate_expr (tem), TREE_OPERAND (t, 1));
1377             }
1378         }
1379       break;
1380
1381     case NOP_EXPR:
1382       /* Convert -((double)float) into (double)(-float).  */
1383       if (TREE_CODE (type) == REAL_TYPE)
1384         {
1385           tem = strip_float_extensions (t);
1386           if (tem != t && negate_expr_p (tem))
1387             return fold_convert_loc (loc, type, negate_expr (tem));
1388         }
1389       break;
1390
1391     case CALL_EXPR:
1392       /* Negate -f(x) as f(-x).  */
1393       if (negate_mathfn_p (builtin_mathfn_code (t))
1394           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1395         {
1396           tree fndecl, arg;
1397
1398           fndecl = get_callee_fndecl (t);
1399           arg = negate_expr (CALL_EXPR_ARG (t, 0));
1400           return build_call_expr_loc (loc, fndecl, 1, arg);
1401         }
1402       break;
1403
1404     case RSHIFT_EXPR:
1405       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1406       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1407         {
1408           tree op1 = TREE_OPERAND (t, 1);
1409           if (TREE_INT_CST_HIGH (op1) == 0
1410               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1411                  == TREE_INT_CST_LOW (op1))
1412             {
1413               tree ntype = TYPE_UNSIGNED (type)
1414                            ? signed_type_for (type)
1415                            : unsigned_type_for (type);
1416               tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
1417               temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
1418               return fold_convert_loc (loc, type, temp);
1419             }
1420         }
1421       break;
1422
1423     default:
1424       break;
1425     }
1426
1427   return NULL_TREE;
1428 }
1429
1430 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1431    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
1432    return NULL_TREE. */
1433
1434 static tree
1435 negate_expr (tree t)
1436 {
1437   tree type, tem;
1438   location_t loc;
1439
1440   if (t == NULL_TREE)
1441     return NULL_TREE;
1442
1443   loc = EXPR_LOCATION (t);
1444   type = TREE_TYPE (t);
1445   STRIP_SIGN_NOPS (t);
1446
1447   tem = fold_negate_expr (loc, t);
1448   if (!tem)
1449     {
1450       tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1451       SET_EXPR_LOCATION (tem, loc);
1452     }
1453   return fold_convert_loc (loc, type, tem);
1454 }
1455 \f
1456 /* Split a tree IN into a constant, literal and variable parts that could be
1457    combined with CODE to make IN.  "constant" means an expression with
1458    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1459    commutative arithmetic operation.  Store the constant part into *CONP,
1460    the literal in *LITP and return the variable part.  If a part isn't
1461    present, set it to null.  If the tree does not decompose in this way,
1462    return the entire tree as the variable part and the other parts as null.
1463
1464    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1465    case, we negate an operand that was subtracted.  Except if it is a
1466    literal for which we use *MINUS_LITP instead.
1467
1468    If NEGATE_P is true, we are negating all of IN, again except a literal
1469    for which we use *MINUS_LITP instead.
1470
1471    If IN is itself a literal or constant, return it as appropriate.
1472
1473    Note that we do not guarantee that any of the three values will be the
1474    same type as IN, but they will have the same signedness and mode.  */
1475
1476 static tree
1477 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1478             tree *minus_litp, int negate_p)
1479 {
1480   tree var = 0;
1481
1482   *conp = 0;
1483   *litp = 0;
1484   *minus_litp = 0;
1485
1486   /* Strip any conversions that don't change the machine mode or signedness.  */
1487   STRIP_SIGN_NOPS (in);
1488
1489   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1490       || TREE_CODE (in) == FIXED_CST)
1491     *litp = in;
1492   else if (TREE_CODE (in) == code
1493            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1494                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1495                /* We can associate addition and subtraction together (even
1496                   though the C standard doesn't say so) for integers because
1497                   the value is not affected.  For reals, the value might be
1498                   affected, so we can't.  */
1499                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1500                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1501     {
1502       tree op0 = TREE_OPERAND (in, 0);
1503       tree op1 = TREE_OPERAND (in, 1);
1504       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1505       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1506
1507       /* First see if either of the operands is a literal, then a constant.  */
1508       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1509           || TREE_CODE (op0) == FIXED_CST)
1510         *litp = op0, op0 = 0;
1511       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1512                || TREE_CODE (op1) == FIXED_CST)
1513         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1514
1515       if (op0 != 0 && TREE_CONSTANT (op0))
1516         *conp = op0, op0 = 0;
1517       else if (op1 != 0 && TREE_CONSTANT (op1))
1518         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1519
1520       /* If we haven't dealt with either operand, this is not a case we can
1521          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1522       if (op0 != 0 && op1 != 0)
1523         var = in;
1524       else if (op0 != 0)
1525         var = op0;
1526       else
1527         var = op1, neg_var_p = neg1_p;
1528
1529       /* Now do any needed negations.  */
1530       if (neg_litp_p)
1531         *minus_litp = *litp, *litp = 0;
1532       if (neg_conp_p)
1533         *conp = negate_expr (*conp);
1534       if (neg_var_p)
1535         var = negate_expr (var);
1536     }
1537   else if (TREE_CONSTANT (in))
1538     *conp = in;
1539   else
1540     var = in;
1541
1542   if (negate_p)
1543     {
1544       if (*litp)
1545         *minus_litp = *litp, *litp = 0;
1546       else if (*minus_litp)
1547         *litp = *minus_litp, *minus_litp = 0;
1548       *conp = negate_expr (*conp);
1549       var = negate_expr (var);
1550     }
1551
1552   return var;
1553 }
1554
1555 /* Re-associate trees split by the above function.  T1 and T2 are
1556    either expressions to associate or null.  Return the new
1557    expression, if any.  LOC is the location of the new expression.  If
1558    we build an operation, do it in TYPE and with CODE.  */
1559
1560 static tree
1561 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
1562 {
1563   tree tem;
1564
1565   if (t1 == 0)
1566     return t2;
1567   else if (t2 == 0)
1568     return t1;
1569
1570   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1571      try to fold this since we will have infinite recursion.  But do
1572      deal with any NEGATE_EXPRs.  */
1573   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1574       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1575     {
1576       if (code == PLUS_EXPR)
1577         {
1578           if (TREE_CODE (t1) == NEGATE_EXPR)
1579             tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t2),
1580                           fold_convert_loc (loc, type, TREE_OPERAND (t1, 0)));
1581           else if (TREE_CODE (t2) == NEGATE_EXPR)
1582             tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t1),
1583                           fold_convert_loc (loc, type, TREE_OPERAND (t2, 0)));
1584           else if (integer_zerop (t2))
1585             return fold_convert_loc (loc, type, t1);
1586         }
1587       else if (code == MINUS_EXPR)
1588         {
1589           if (integer_zerop (t2))
1590             return fold_convert_loc (loc, type, t1);
1591         }
1592
1593       tem = build2 (code, type, fold_convert_loc (loc, type, t1),
1594                     fold_convert_loc (loc, type, t2));
1595       goto associate_trees_exit;
1596     }
1597
1598   return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1599                       fold_convert_loc (loc, type, t2));
1600  associate_trees_exit:
1601   protected_set_expr_location (tem, loc);
1602   return tem;
1603 }
1604 \f
1605 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1606    for use in int_const_binop, size_binop and size_diffop.  */
1607
1608 static bool
1609 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1610 {
1611   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1612     return false;
1613   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1614     return false;
1615
1616   switch (code)
1617     {
1618     case LSHIFT_EXPR:
1619     case RSHIFT_EXPR:
1620     case LROTATE_EXPR:
1621     case RROTATE_EXPR:
1622       return true;
1623
1624     default:
1625       break;
1626     }
1627
1628   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1629          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1630          && TYPE_MODE (type1) == TYPE_MODE (type2);
1631 }
1632
1633
1634 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1635    to produce a new constant.  Return NULL_TREE if we don't know how
1636    to evaluate CODE at compile-time.
1637
1638    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1639
1640 tree
1641 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1642 {
1643   unsigned HOST_WIDE_INT int1l, int2l;
1644   HOST_WIDE_INT int1h, int2h;
1645   unsigned HOST_WIDE_INT low;
1646   HOST_WIDE_INT hi;
1647   unsigned HOST_WIDE_INT garbagel;
1648   HOST_WIDE_INT garbageh;
1649   tree t;
1650   tree type = TREE_TYPE (arg1);
1651   int uns = TYPE_UNSIGNED (type);
1652   int is_sizetype
1653     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1654   int overflow = 0;
1655
1656   int1l = TREE_INT_CST_LOW (arg1);
1657   int1h = TREE_INT_CST_HIGH (arg1);
1658   int2l = TREE_INT_CST_LOW (arg2);
1659   int2h = TREE_INT_CST_HIGH (arg2);
1660
1661   switch (code)
1662     {
1663     case BIT_IOR_EXPR:
1664       low = int1l | int2l, hi = int1h | int2h;
1665       break;
1666
1667     case BIT_XOR_EXPR:
1668       low = int1l ^ int2l, hi = int1h ^ int2h;
1669       break;
1670
1671     case BIT_AND_EXPR:
1672       low = int1l & int2l, hi = int1h & int2h;
1673       break;
1674
1675     case RSHIFT_EXPR:
1676       int2l = -int2l;
1677     case LSHIFT_EXPR:
1678       /* It's unclear from the C standard whether shifts can overflow.
1679          The following code ignores overflow; perhaps a C standard
1680          interpretation ruling is needed.  */
1681       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1682                      &low, &hi, !uns);
1683       break;
1684
1685     case RROTATE_EXPR:
1686       int2l = - int2l;
1687     case LROTATE_EXPR:
1688       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1689                       &low, &hi);
1690       break;
1691
1692     case PLUS_EXPR:
1693       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1694       break;
1695
1696     case MINUS_EXPR:
1697       neg_double (int2l, int2h, &low, &hi);
1698       add_double (int1l, int1h, low, hi, &low, &hi);
1699       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1700       break;
1701
1702     case MULT_EXPR:
1703       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1704       break;
1705
1706     case TRUNC_DIV_EXPR:
1707     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1708     case EXACT_DIV_EXPR:
1709       /* This is a shortcut for a common special case.  */
1710       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1711           && !TREE_OVERFLOW (arg1)
1712           && !TREE_OVERFLOW (arg2)
1713           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1714         {
1715           if (code == CEIL_DIV_EXPR)
1716             int1l += int2l - 1;
1717
1718           low = int1l / int2l, hi = 0;
1719           break;
1720         }
1721
1722       /* ... fall through ...  */
1723
1724     case ROUND_DIV_EXPR:
1725       if (int2h == 0 && int2l == 0)
1726         return NULL_TREE;
1727       if (int2h == 0 && int2l == 1)
1728         {
1729           low = int1l, hi = int1h;
1730           break;
1731         }
1732       if (int1l == int2l && int1h == int2h
1733           && ! (int1l == 0 && int1h == 0))
1734         {
1735           low = 1, hi = 0;
1736           break;
1737         }
1738       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1739                                        &low, &hi, &garbagel, &garbageh);
1740       break;
1741
1742     case TRUNC_MOD_EXPR:
1743     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1744       /* This is a shortcut for a common special case.  */
1745       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1746           && !TREE_OVERFLOW (arg1)
1747           && !TREE_OVERFLOW (arg2)
1748           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1749         {
1750           if (code == CEIL_MOD_EXPR)
1751             int1l += int2l - 1;
1752           low = int1l % int2l, hi = 0;
1753           break;
1754         }
1755
1756       /* ... fall through ...  */
1757
1758     case ROUND_MOD_EXPR:
1759       if (int2h == 0 && int2l == 0)
1760         return NULL_TREE;
1761       overflow = div_and_round_double (code, uns,
1762                                        int1l, int1h, int2l, int2h,
1763                                        &garbagel, &garbageh, &low, &hi);
1764       break;
1765
1766     case MIN_EXPR:
1767     case MAX_EXPR:
1768       if (uns)
1769         low = (((unsigned HOST_WIDE_INT) int1h
1770                 < (unsigned HOST_WIDE_INT) int2h)
1771                || (((unsigned HOST_WIDE_INT) int1h
1772                     == (unsigned HOST_WIDE_INT) int2h)
1773                    && int1l < int2l));
1774       else
1775         low = (int1h < int2h
1776                || (int1h == int2h && int1l < int2l));
1777
1778       if (low == (code == MIN_EXPR))
1779         low = int1l, hi = int1h;
1780       else
1781         low = int2l, hi = int2h;
1782       break;
1783
1784     default:
1785       return NULL_TREE;
1786     }
1787
1788   if (notrunc)
1789     {
1790       t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1791
1792       /* Propagate overflow flags ourselves.  */
1793       if (((!uns || is_sizetype) && overflow)
1794           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1795         {
1796           t = copy_node (t);
1797           TREE_OVERFLOW (t) = 1;
1798         }
1799     }
1800   else
1801     t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1802                                ((!uns || is_sizetype) && overflow)
1803                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1804
1805   return t;
1806 }
1807
1808 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1809    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1810    are the same kind of constant and the same machine mode.  Return zero if
1811    combining the constants is not allowed in the current operating mode.
1812
1813    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1814
1815 static tree
1816 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1817 {
1818   /* Sanity check for the recursive cases.  */
1819   if (!arg1 || !arg2)
1820     return NULL_TREE;
1821
1822   STRIP_NOPS (arg1);
1823   STRIP_NOPS (arg2);
1824
1825   if (TREE_CODE (arg1) == INTEGER_CST)
1826     return int_const_binop (code, arg1, arg2, notrunc);
1827
1828   if (TREE_CODE (arg1) == REAL_CST)
1829     {
1830       enum machine_mode mode;
1831       REAL_VALUE_TYPE d1;
1832       REAL_VALUE_TYPE d2;
1833       REAL_VALUE_TYPE value;
1834       REAL_VALUE_TYPE result;
1835       bool inexact;
1836       tree t, type;
1837
1838       /* The following codes are handled by real_arithmetic.  */
1839       switch (code)
1840         {
1841         case PLUS_EXPR:
1842         case MINUS_EXPR:
1843         case MULT_EXPR:
1844         case RDIV_EXPR:
1845         case MIN_EXPR:
1846         case MAX_EXPR:
1847           break;
1848
1849         default:
1850           return NULL_TREE;
1851         }
1852
1853       d1 = TREE_REAL_CST (arg1);
1854       d2 = TREE_REAL_CST (arg2);
1855
1856       type = TREE_TYPE (arg1);
1857       mode = TYPE_MODE (type);
1858
1859       /* Don't perform operation if we honor signaling NaNs and
1860          either operand is a NaN.  */
1861       if (HONOR_SNANS (mode)
1862           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1863         return NULL_TREE;
1864
1865       /* Don't perform operation if it would raise a division
1866          by zero exception.  */
1867       if (code == RDIV_EXPR
1868           && REAL_VALUES_EQUAL (d2, dconst0)
1869           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1870         return NULL_TREE;
1871
1872       /* If either operand is a NaN, just return it.  Otherwise, set up
1873          for floating-point trap; we return an overflow.  */
1874       if (REAL_VALUE_ISNAN (d1))
1875         return arg1;
1876       else if (REAL_VALUE_ISNAN (d2))
1877         return arg2;
1878
1879       inexact = real_arithmetic (&value, code, &d1, &d2);
1880       real_convert (&result, mode, &value);
1881
1882       /* Don't constant fold this floating point operation if
1883          the result has overflowed and flag_trapping_math.  */
1884       if (flag_trapping_math
1885           && MODE_HAS_INFINITIES (mode)
1886           && REAL_VALUE_ISINF (result)
1887           && !REAL_VALUE_ISINF (d1)
1888           && !REAL_VALUE_ISINF (d2))
1889         return NULL_TREE;
1890
1891       /* Don't constant fold this floating point operation if the
1892          result may dependent upon the run-time rounding mode and
1893          flag_rounding_math is set, or if GCC's software emulation
1894          is unable to accurately represent the result.  */
1895       if ((flag_rounding_math
1896            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1897           && (inexact || !real_identical (&result, &value)))
1898         return NULL_TREE;
1899
1900       t = build_real (type, result);
1901
1902       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1903       return t;
1904     }
1905
1906   if (TREE_CODE (arg1) == FIXED_CST)
1907     {
1908       FIXED_VALUE_TYPE f1;
1909       FIXED_VALUE_TYPE f2;
1910       FIXED_VALUE_TYPE result;
1911       tree t, type;
1912       int sat_p;
1913       bool overflow_p;
1914
1915       /* The following codes are handled by fixed_arithmetic.  */
1916       switch (code)
1917         {
1918         case PLUS_EXPR:
1919         case MINUS_EXPR:
1920         case MULT_EXPR:
1921         case TRUNC_DIV_EXPR:
1922           f2 = TREE_FIXED_CST (arg2);
1923           break;
1924
1925         case LSHIFT_EXPR:
1926         case RSHIFT_EXPR:
1927           f2.data.high = TREE_INT_CST_HIGH (arg2);
1928           f2.data.low = TREE_INT_CST_LOW (arg2);
1929           f2.mode = SImode;
1930           break;
1931
1932         default:
1933           return NULL_TREE;
1934         }
1935
1936       f1 = TREE_FIXED_CST (arg1);
1937       type = TREE_TYPE (arg1);
1938       sat_p = TYPE_SATURATING (type);
1939       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1940       t = build_fixed (type, result);
1941       /* Propagate overflow flags.  */
1942       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1943         TREE_OVERFLOW (t) = 1;
1944       return t;
1945     }
1946
1947   if (TREE_CODE (arg1) == COMPLEX_CST)
1948     {
1949       tree type = TREE_TYPE (arg1);
1950       tree r1 = TREE_REALPART (arg1);
1951       tree i1 = TREE_IMAGPART (arg1);
1952       tree r2 = TREE_REALPART (arg2);
1953       tree i2 = TREE_IMAGPART (arg2);
1954       tree real, imag;
1955
1956       switch (code)
1957         {
1958         case PLUS_EXPR:
1959         case MINUS_EXPR:
1960           real = const_binop (code, r1, r2, notrunc);
1961           imag = const_binop (code, i1, i2, notrunc);
1962           break;
1963
1964         case MULT_EXPR:
1965 #ifdef HAVE_mpc
1966           if (COMPLEX_FLOAT_TYPE_P (type))
1967             return do_mpc_arg2 (arg1, arg2, type,
1968                                 /* do_nonfinite= */ folding_initializer,
1969                                 mpc_mul);
1970 #endif
1971
1972           real = const_binop (MINUS_EXPR,
1973                               const_binop (MULT_EXPR, r1, r2, notrunc),
1974                               const_binop (MULT_EXPR, i1, i2, notrunc),
1975                               notrunc);
1976           imag = const_binop (PLUS_EXPR,
1977                               const_binop (MULT_EXPR, r1, i2, notrunc),
1978                               const_binop (MULT_EXPR, i1, r2, notrunc),
1979                               notrunc);
1980           break;
1981
1982         case RDIV_EXPR:
1983 #ifdef HAVE_mpc
1984           if (COMPLEX_FLOAT_TYPE_P (type))
1985             return do_mpc_arg2 (arg1, arg2, type,
1986                                 /* do_nonfinite= */ folding_initializer,
1987                                 mpc_div);
1988           /* Fallthru ... */
1989 #endif
1990
1991         case TRUNC_DIV_EXPR:
1992         case CEIL_DIV_EXPR:
1993         case FLOOR_DIV_EXPR:
1994         case ROUND_DIV_EXPR:
1995           if (flag_complex_method == 0)
1996           {
1997             /* Keep this algorithm in sync with
1998                tree-complex.c:expand_complex_div_straight().
1999
2000                Expand complex division to scalars, straightforward algorithm.
2001                a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
2002                t = br*br + bi*bi
2003             */
2004             tree magsquared
2005               = const_binop (PLUS_EXPR,
2006                              const_binop (MULT_EXPR, r2, r2, notrunc),
2007                              const_binop (MULT_EXPR, i2, i2, notrunc),
2008                              notrunc);
2009             tree t1
2010               = const_binop (PLUS_EXPR,
2011                              const_binop (MULT_EXPR, r1, r2, notrunc),
2012                              const_binop (MULT_EXPR, i1, i2, notrunc),
2013                              notrunc);
2014             tree t2
2015               = const_binop (MINUS_EXPR,
2016                              const_binop (MULT_EXPR, i1, r2, notrunc),
2017                              const_binop (MULT_EXPR, r1, i2, notrunc),
2018                              notrunc);
2019
2020             real = const_binop (code, t1, magsquared, notrunc);
2021             imag = const_binop (code, t2, magsquared, notrunc);
2022           }
2023           else
2024           {
2025             /* Keep this algorithm in sync with
2026                tree-complex.c:expand_complex_div_wide().
2027
2028                Expand complex division to scalars, modified algorithm to minimize
2029                overflow with wide input ranges.  */
2030             tree compare = fold_build2 (LT_EXPR, boolean_type_node,
2031                                         fold_abs_const (r2, TREE_TYPE (type)),
2032                                         fold_abs_const (i2, TREE_TYPE (type)));
2033
2034             if (integer_nonzerop (compare))
2035               {
2036                 /* In the TRUE branch, we compute
2037                    ratio = br/bi;
2038                    div = (br * ratio) + bi;
2039                    tr = (ar * ratio) + ai;
2040                    ti = (ai * ratio) - ar;
2041                    tr = tr / div;
2042                    ti = ti / div;  */
2043                 tree ratio = const_binop (code, r2, i2, notrunc);
2044                 tree div = const_binop (PLUS_EXPR, i2,
2045                                         const_binop (MULT_EXPR, r2, ratio,
2046                                                      notrunc),
2047                                         notrunc);
2048                 real = const_binop (MULT_EXPR, r1, ratio, notrunc);
2049                 real = const_binop (PLUS_EXPR, real, i1, notrunc);
2050                 real = const_binop (code, real, div, notrunc);
2051
2052                 imag = const_binop (MULT_EXPR, i1, ratio, notrunc);
2053                 imag = const_binop (MINUS_EXPR, imag, r1, notrunc);
2054                 imag = const_binop (code, imag, div, notrunc);
2055               }
2056             else
2057               {
2058                 /* In the FALSE branch, we compute
2059                    ratio = d/c;
2060                    divisor = (d * ratio) + c;
2061                    tr = (b * ratio) + a;
2062                    ti = b - (a * ratio);
2063                    tr = tr / div;
2064                    ti = ti / div;  */
2065                 tree ratio = const_binop (code, i2, r2, notrunc);
2066                 tree div = const_binop (PLUS_EXPR, r2,
2067                                         const_binop (MULT_EXPR, i2, ratio,
2068                                                      notrunc),
2069                                         notrunc);
2070
2071                 real = const_binop (MULT_EXPR, i1, ratio, notrunc);
2072                 real = const_binop (PLUS_EXPR, real, r1, notrunc);
2073                 real = const_binop (code, real, div, notrunc);
2074
2075                 imag = const_binop (MULT_EXPR, r1, ratio, notrunc);
2076                 imag = const_binop (MINUS_EXPR, i1, imag, notrunc);
2077                 imag = const_binop (code, imag, div, notrunc);
2078               }
2079           }
2080           break;
2081
2082         default:
2083           return NULL_TREE;
2084         }
2085
2086       if (real && imag)
2087         return build_complex (type, real, imag);
2088     }
2089
2090   if (TREE_CODE (arg1) == VECTOR_CST)
2091     {
2092       tree type = TREE_TYPE(arg1);
2093       int count = TYPE_VECTOR_SUBPARTS (type), i;
2094       tree elements1, elements2, list = NULL_TREE;
2095
2096       if(TREE_CODE(arg2) != VECTOR_CST)
2097         return NULL_TREE;
2098
2099       elements1 = TREE_VECTOR_CST_ELTS (arg1);
2100       elements2 = TREE_VECTOR_CST_ELTS (arg2);
2101
2102       for (i = 0; i < count; i++)
2103         {
2104           tree elem1, elem2, elem;
2105
2106           /* The trailing elements can be empty and should be treated as 0 */
2107           if(!elements1)
2108             elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2109           else
2110             {
2111               elem1 = TREE_VALUE(elements1);
2112               elements1 = TREE_CHAIN (elements1);
2113             }
2114
2115           if(!elements2)
2116             elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2117           else
2118             {
2119               elem2 = TREE_VALUE(elements2);
2120               elements2 = TREE_CHAIN (elements2);
2121             }
2122
2123           elem = const_binop (code, elem1, elem2, notrunc);
2124
2125           /* It is possible that const_binop cannot handle the given
2126             code and return NULL_TREE */
2127           if(elem == NULL_TREE)
2128             return NULL_TREE;
2129
2130           list = tree_cons (NULL_TREE, elem, list);
2131         }
2132       return build_vector(type, nreverse(list));
2133     }
2134   return NULL_TREE;
2135 }
2136
2137 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
2138    indicates which particular sizetype to create.  */
2139
2140 tree
2141 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2142 {
2143   return build_int_cst (sizetype_tab[(int) kind], number);
2144 }
2145 \f
2146 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
2147    is a tree code.  The type of the result is taken from the operands.
2148    Both must be equivalent integer types, ala int_binop_types_match_p.
2149    If the operands are constant, so is the result.  */
2150
2151 tree
2152 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2153 {
2154   tree type = TREE_TYPE (arg0);
2155
2156   if (arg0 == error_mark_node || arg1 == error_mark_node)
2157     return error_mark_node;
2158
2159   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2160                                        TREE_TYPE (arg1)));
2161
2162   /* Handle the special case of two integer constants faster.  */
2163   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2164     {
2165       /* And some specific cases even faster than that.  */
2166       if (code == PLUS_EXPR)
2167         {
2168           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2169             return arg1;
2170           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2171             return arg0;
2172         }
2173       else if (code == MINUS_EXPR)
2174         {
2175           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2176             return arg0;
2177         }
2178       else if (code == MULT_EXPR)
2179         {
2180           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2181             return arg1;
2182         }
2183
2184       /* Handle general case of two integer constants.  */
2185       return int_const_binop (code, arg0, arg1, 0);
2186     }
2187
2188   return fold_build2_loc (loc, code, type, arg0, arg1);
2189 }
2190
2191 /* Given two values, either both of sizetype or both of bitsizetype,
2192    compute the difference between the two values.  Return the value
2193    in signed type corresponding to the type of the operands.  */
2194
2195 tree
2196 size_diffop_loc (location_t loc, tree arg0, tree arg1)
2197 {
2198   tree type = TREE_TYPE (arg0);
2199   tree ctype;
2200
2201   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2202                                        TREE_TYPE (arg1)));
2203
2204   /* If the type is already signed, just do the simple thing.  */
2205   if (!TYPE_UNSIGNED (type))
2206     return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2207
2208   if (type == sizetype)
2209     ctype = ssizetype;
2210   else if (type == bitsizetype)
2211     ctype = sbitsizetype;
2212   else
2213     ctype = signed_type_for (type);
2214
2215   /* If either operand is not a constant, do the conversions to the signed
2216      type and subtract.  The hardware will do the right thing with any
2217      overflow in the subtraction.  */
2218   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2219     return size_binop_loc (loc, MINUS_EXPR,
2220                            fold_convert_loc (loc, ctype, arg0),
2221                            fold_convert_loc (loc, ctype, arg1));
2222
2223   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2224      Otherwise, subtract the other way, convert to CTYPE (we know that can't
2225      overflow) and negate (which can't either).  Special-case a result
2226      of zero while we're here.  */
2227   if (tree_int_cst_equal (arg0, arg1))
2228     return build_int_cst (ctype, 0);
2229   else if (tree_int_cst_lt (arg1, arg0))
2230     return fold_convert_loc (loc, ctype,
2231                              size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2232   else
2233     return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2234                            fold_convert_loc (loc, ctype,
2235                                              size_binop_loc (loc,
2236                                                              MINUS_EXPR,
2237                                                              arg1, arg0)));
2238 }
2239 \f
2240 /* A subroutine of fold_convert_const handling conversions of an
2241    INTEGER_CST to another integer type.  */
2242
2243 static tree
2244 fold_convert_const_int_from_int (tree type, const_tree arg1)
2245 {
2246   tree t;
2247
2248   /* Given an integer constant, make new constant with new type,
2249      appropriately sign-extended or truncated.  */
2250   t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2251                              TREE_INT_CST_HIGH (arg1),
2252                              /* Don't set the overflow when
2253                                 converting from a pointer,  */
2254                              !POINTER_TYPE_P (TREE_TYPE (arg1))
2255                              /* or to a sizetype with same signedness
2256                                 and the precision is unchanged.
2257                                 ???  sizetype is always sign-extended,
2258                                 but its signedness depends on the
2259                                 frontend.  Thus we see spurious overflows
2260                                 here if we do not check this.  */
2261                              && !((TYPE_PRECISION (TREE_TYPE (arg1))
2262                                    == TYPE_PRECISION (type))
2263                                   && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2264                                       == TYPE_UNSIGNED (type))
2265                                   && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2266                                        && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2267                                       || (TREE_CODE (type) == INTEGER_TYPE
2268                                           && TYPE_IS_SIZETYPE (type)))),
2269                              (TREE_INT_CST_HIGH (arg1) < 0
2270                               && (TYPE_UNSIGNED (type)
2271                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2272                              | TREE_OVERFLOW (arg1));
2273
2274   return t;
2275 }
2276
2277 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2278    to an integer type.  */
2279
2280 static tree
2281 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2282 {
2283   int overflow = 0;
2284   tree t;
2285
2286   /* The following code implements the floating point to integer
2287      conversion rules required by the Java Language Specification,
2288      that IEEE NaNs are mapped to zero and values that overflow
2289      the target precision saturate, i.e. values greater than
2290      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2291      are mapped to INT_MIN.  These semantics are allowed by the
2292      C and C++ standards that simply state that the behavior of
2293      FP-to-integer conversion is unspecified upon overflow.  */
2294
2295   HOST_WIDE_INT high, low;
2296   REAL_VALUE_TYPE r;
2297   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2298
2299   switch (code)
2300     {
2301     case FIX_TRUNC_EXPR:
2302       real_trunc (&r, VOIDmode, &x);
2303       break;
2304
2305     default:
2306       gcc_unreachable ();
2307     }
2308
2309   /* If R is NaN, return zero and show we have an overflow.  */
2310   if (REAL_VALUE_ISNAN (r))
2311     {
2312       overflow = 1;
2313       high = 0;
2314       low = 0;
2315     }
2316
2317   /* See if R is less than the lower bound or greater than the
2318      upper bound.  */
2319
2320   if (! overflow)
2321     {
2322       tree lt = TYPE_MIN_VALUE (type);
2323       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2324       if (REAL_VALUES_LESS (r, l))
2325         {
2326           overflow = 1;
2327           high = TREE_INT_CST_HIGH (lt);
2328           low = TREE_INT_CST_LOW (lt);
2329         }
2330     }
2331
2332   if (! overflow)
2333     {
2334       tree ut = TYPE_MAX_VALUE (type);
2335       if (ut)
2336         {
2337           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2338           if (REAL_VALUES_LESS (u, r))
2339             {
2340               overflow = 1;
2341               high = TREE_INT_CST_HIGH (ut);
2342               low = TREE_INT_CST_LOW (ut);
2343             }
2344         }
2345     }
2346
2347   if (! overflow)
2348     REAL_VALUE_TO_INT (&low, &high, r);
2349
2350   t = force_fit_type_double (type, low, high, -1,
2351                              overflow | TREE_OVERFLOW (arg1));
2352   return t;
2353 }
2354
2355 /* A subroutine of fold_convert_const handling conversions of a
2356    FIXED_CST to an integer type.  */
2357
2358 static tree
2359 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2360 {
2361   tree t;
2362   double_int temp, temp_trunc;
2363   unsigned int mode;
2364
2365   /* Right shift FIXED_CST to temp by fbit.  */
2366   temp = TREE_FIXED_CST (arg1).data;
2367   mode = TREE_FIXED_CST (arg1).mode;
2368   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2369     {
2370       lshift_double (temp.low, temp.high,
2371                      - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2372                      &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2373
2374       /* Left shift temp to temp_trunc by fbit.  */
2375       lshift_double (temp.low, temp.high,
2376                      GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2377                      &temp_trunc.low, &temp_trunc.high,
2378                      SIGNED_FIXED_POINT_MODE_P (mode));
2379     }
2380   else
2381     {
2382       temp.low = 0;
2383       temp.high = 0;
2384       temp_trunc.low = 0;
2385       temp_trunc.high = 0;
2386     }
2387
2388   /* If FIXED_CST is negative, we need to round the value toward 0.
2389      By checking if the fractional bits are not zero to add 1 to temp.  */
2390   if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2391       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2392     {
2393       double_int one;
2394       one.low = 1;
2395       one.high = 0;
2396       temp = double_int_add (temp, one);
2397     }
2398
2399   /* Given a fixed-point constant, make new constant with new type,
2400      appropriately sign-extended or truncated.  */
2401   t = force_fit_type_double (type, temp.low, temp.high, -1,
2402                              (temp.high < 0
2403                               && (TYPE_UNSIGNED (type)
2404                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2405                              | TREE_OVERFLOW (arg1));
2406
2407   return t;
2408 }
2409
2410 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2411    to another floating point type.  */
2412
2413 static tree
2414 fold_convert_const_real_from_real (tree type, const_tree arg1)
2415 {
2416   REAL_VALUE_TYPE value;
2417   tree t;
2418
2419   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2420   t = build_real (type, value);
2421
2422   /* If converting an infinity or NAN to a representation that doesn't
2423      have one, set the overflow bit so that we can produce some kind of
2424      error message at the appropriate point if necessary.  It's not the
2425      most user-friendly message, but it's better than nothing.  */
2426   if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2427       && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2428     TREE_OVERFLOW (t) = 1;
2429   else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2430            && !MODE_HAS_NANS (TYPE_MODE (type)))
2431     TREE_OVERFLOW (t) = 1;
2432   /* Regular overflow, conversion produced an infinity in a mode that
2433      can't represent them.  */
2434   else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2435            && REAL_VALUE_ISINF (value)
2436            && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2437     TREE_OVERFLOW (t) = 1;
2438   else
2439     TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2440   return t;
2441 }
2442
2443 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2444    to a floating point type.  */
2445
2446 static tree
2447 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2448 {
2449   REAL_VALUE_TYPE value;
2450   tree t;
2451
2452   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2453   t = build_real (type, value);
2454
2455   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2456   return t;
2457 }
2458
2459 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2460    to another fixed-point type.  */
2461
2462 static tree
2463 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2464 {
2465   FIXED_VALUE_TYPE value;
2466   tree t;
2467   bool overflow_p;
2468
2469   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2470                               TYPE_SATURATING (type));
2471   t = build_fixed (type, value);
2472
2473   /* Propagate overflow flags.  */
2474   if (overflow_p | TREE_OVERFLOW (arg1))
2475     TREE_OVERFLOW (t) = 1;
2476   return t;
2477 }
2478
2479 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2480    to a fixed-point type.  */
2481
2482 static tree
2483 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2484 {
2485   FIXED_VALUE_TYPE value;
2486   tree t;
2487   bool overflow_p;
2488
2489   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2490                                        TREE_INT_CST (arg1),
2491                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
2492                                        TYPE_SATURATING (type));
2493   t = build_fixed (type, value);
2494
2495   /* Propagate overflow flags.  */
2496   if (overflow_p | TREE_OVERFLOW (arg1))
2497     TREE_OVERFLOW (t) = 1;
2498   return t;
2499 }
2500
2501 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2502    to a fixed-point type.  */
2503
2504 static tree
2505 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2506 {
2507   FIXED_VALUE_TYPE value;
2508   tree t;
2509   bool overflow_p;
2510
2511   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2512                                         &TREE_REAL_CST (arg1),
2513                                         TYPE_SATURATING (type));
2514   t = build_fixed (type, value);
2515
2516   /* Propagate overflow flags.  */
2517   if (overflow_p | TREE_OVERFLOW (arg1))
2518     TREE_OVERFLOW (t) = 1;
2519   return t;
2520 }
2521
2522 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2523    type TYPE.  If no simplification can be done return NULL_TREE.  */
2524
2525 static tree
2526 fold_convert_const (enum tree_code code, tree type, tree arg1)
2527 {
2528   if (TREE_TYPE (arg1) == type)
2529     return arg1;
2530
2531   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2532       || TREE_CODE (type) == OFFSET_TYPE)
2533     {
2534       if (TREE_CODE (arg1) == INTEGER_CST)
2535         return fold_convert_const_int_from_int (type, arg1);
2536       else if (TREE_CODE (arg1) == REAL_CST)
2537         return fold_convert_const_int_from_real (code, type, arg1);
2538       else if (TREE_CODE (arg1) == FIXED_CST)
2539         return fold_convert_const_int_from_fixed (type, arg1);
2540     }
2541   else if (TREE_CODE (type) == REAL_TYPE)
2542     {
2543       if (TREE_CODE (arg1) == INTEGER_CST)
2544         return build_real_from_int_cst (type, arg1);
2545       else if (TREE_CODE (arg1) == REAL_CST)
2546         return fold_convert_const_real_from_real (type, arg1);
2547       else if (TREE_CODE (arg1) == FIXED_CST)
2548         return fold_convert_const_real_from_fixed (type, arg1);
2549     }
2550   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2551     {
2552       if (TREE_CODE (arg1) == FIXED_CST)
2553         return fold_convert_const_fixed_from_fixed (type, arg1);
2554       else if (TREE_CODE (arg1) == INTEGER_CST)
2555         return fold_convert_const_fixed_from_int (type, arg1);
2556       else if (TREE_CODE (arg1) == REAL_CST)
2557         return fold_convert_const_fixed_from_real (type, arg1);
2558     }
2559   return NULL_TREE;
2560 }
2561
2562 /* Construct a vector of zero elements of vector type TYPE.  */
2563
2564 static tree
2565 build_zero_vector (tree type)
2566 {
2567   tree elem, list;
2568   int i, units;
2569
2570   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2571   units = TYPE_VECTOR_SUBPARTS (type);
2572
2573   list = NULL_TREE;
2574   for (i = 0; i < units; i++)
2575     list = tree_cons (NULL_TREE, elem, list);
2576   return build_vector (type, list);
2577 }
2578
2579 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
2580
2581 bool
2582 fold_convertible_p (const_tree type, const_tree arg)
2583 {
2584   tree orig = TREE_TYPE (arg);
2585
2586   if (type == orig)
2587     return true;
2588
2589   if (TREE_CODE (arg) == ERROR_MARK
2590       || TREE_CODE (type) == ERROR_MARK
2591       || TREE_CODE (orig) == ERROR_MARK)
2592     return false;
2593
2594   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2595     return true;
2596
2597   switch (TREE_CODE (type))
2598     {
2599     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2600     case POINTER_TYPE: case REFERENCE_TYPE:
2601     case OFFSET_TYPE:
2602       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2603           || TREE_CODE (orig) == OFFSET_TYPE)
2604         return true;
2605       return (TREE_CODE (orig) == VECTOR_TYPE
2606               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2607
2608     case REAL_TYPE:
2609     case FIXED_POINT_TYPE:
2610     case COMPLEX_TYPE:
2611     case VECTOR_TYPE:
2612     case VOID_TYPE:
2613       return TREE_CODE (type) == TREE_CODE (orig);
2614
2615     default:
2616       return false;
2617     }
2618 }
2619
2620 /* Convert expression ARG to type TYPE.  Used by the middle-end for
2621    simple conversions in preference to calling the front-end's convert.  */
2622
2623 tree
2624 fold_convert_loc (location_t loc, tree type, tree arg)
2625 {
2626   tree orig = TREE_TYPE (arg);
2627   tree tem;
2628
2629   if (type == orig)
2630     return arg;
2631
2632   if (TREE_CODE (arg) == ERROR_MARK
2633       || TREE_CODE (type) == ERROR_MARK
2634       || TREE_CODE (orig) == ERROR_MARK)
2635     return error_mark_node;
2636
2637   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2638     return fold_build1_loc (loc, NOP_EXPR, type, arg);
2639
2640   switch (TREE_CODE (type))
2641     {
2642     case POINTER_TYPE:
2643     case REFERENCE_TYPE:
2644       /* Handle conversions between pointers to different address spaces.  */
2645       if (POINTER_TYPE_P (orig)
2646           && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2647               != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2648         return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2649       /* fall through */
2650
2651     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2652     case OFFSET_TYPE:
2653       if (TREE_CODE (arg) == INTEGER_CST)
2654         {
2655           tem = fold_convert_const (NOP_EXPR, type, arg);
2656           if (tem != NULL_TREE)
2657             return tem;
2658         }
2659       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2660           || TREE_CODE (orig) == OFFSET_TYPE)
2661         return fold_build1_loc (loc, NOP_EXPR, type, arg);
2662       if (TREE_CODE (orig) == COMPLEX_TYPE)
2663         return fold_convert_loc (loc, type,
2664                              fold_build1_loc (loc, REALPART_EXPR,
2665                                           TREE_TYPE (orig), arg));
2666       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2667                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2668       return fold_build1_loc (loc, NOP_EXPR, type, arg);
2669
2670     case REAL_TYPE:
2671       if (TREE_CODE (arg) == INTEGER_CST)
2672         {
2673           tem = fold_convert_const (FLOAT_EXPR, type, arg);
2674           if (tem != NULL_TREE)
2675             return tem;
2676         }
2677       else if (TREE_CODE (arg) == REAL_CST)
2678         {
2679           tem = fold_convert_const (NOP_EXPR, type, arg);
2680           if (tem != NULL_TREE)
2681             return tem;
2682         }
2683       else if (TREE_CODE (arg) == FIXED_CST)
2684         {
2685           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2686           if (tem != NULL_TREE)
2687             return tem;
2688         }
2689
2690       switch (TREE_CODE (orig))
2691         {
2692         case INTEGER_TYPE:
2693         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2694         case POINTER_TYPE: case REFERENCE_TYPE:
2695           return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2696
2697         case REAL_TYPE:
2698           return fold_build1_loc (loc, NOP_EXPR, type, arg);
2699
2700         case FIXED_POINT_TYPE:
2701           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2702
2703         case COMPLEX_TYPE:
2704           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2705           return fold_convert_loc (loc, type, tem);
2706
2707         default:
2708           gcc_unreachable ();
2709         }
2710
2711     case FIXED_POINT_TYPE:
2712       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2713           || TREE_CODE (arg) == REAL_CST)
2714         {
2715           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2716           if (tem != NULL_TREE)
2717             goto fold_convert_exit;
2718         }
2719
2720       switch (TREE_CODE (orig))
2721         {
2722         case FIXED_POINT_TYPE:
2723         case INTEGER_TYPE:
2724         case ENUMERAL_TYPE:
2725         case BOOLEAN_TYPE:
2726         case REAL_TYPE:
2727           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2728
2729         case COMPLEX_TYPE:
2730           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2731           return fold_convert_loc (loc, type, tem);
2732
2733         default:
2734           gcc_unreachable ();
2735         }
2736
2737     case COMPLEX_TYPE:
2738       switch (TREE_CODE (orig))
2739         {
2740         case INTEGER_TYPE:
2741         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2742         case POINTER_TYPE: case REFERENCE_TYPE:
2743         case REAL_TYPE:
2744         case FIXED_POINT_TYPE:
2745           return fold_build2_loc (loc, COMPLEX_EXPR, type,
2746                               fold_convert_loc (loc, TREE_TYPE (type), arg),
2747                               fold_convert_loc (loc, TREE_TYPE (type),
2748                                             integer_zero_node));
2749         case COMPLEX_TYPE:
2750           {
2751             tree rpart, ipart;
2752
2753             if (TREE_CODE (arg) == COMPLEX_EXPR)
2754               {
2755                 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2756                                       TREE_OPERAND (arg, 0));
2757                 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2758                                       TREE_OPERAND (arg, 1));
2759                 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2760               }
2761
2762             arg = save_expr (arg);
2763             rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2764             ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2765             rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2766             ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2767             return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2768           }
2769
2770         default:
2771           gcc_unreachable ();
2772         }
2773
2774     case VECTOR_TYPE:
2775       if (integer_zerop (arg))
2776         return build_zero_vector (type);
2777       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2778       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2779                   || TREE_CODE (orig) == VECTOR_TYPE);
2780       return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2781
2782     case VOID_TYPE:
2783       tem = fold_ignored_result (arg);
2784       if (TREE_CODE (tem) == MODIFY_EXPR)
2785         goto fold_convert_exit;
2786       return fold_build1_loc (loc, NOP_EXPR, type, tem);
2787
2788     default:
2789       gcc_unreachable ();
2790     }
2791  fold_convert_exit:
2792   protected_set_expr_location (tem, loc);
2793   return tem;
2794 }
2795 \f
2796 /* Return false if expr can be assumed not to be an lvalue, true
2797    otherwise.  */
2798
2799 static bool
2800 maybe_lvalue_p (const_tree x)
2801 {
2802   /* We only need to wrap lvalue tree codes.  */
2803   switch (TREE_CODE (x))
2804   {
2805   case VAR_DECL:
2806   case PARM_DECL:
2807   case RESULT_DECL:
2808   case LABEL_DECL:
2809   case FUNCTION_DECL:
2810   case SSA_NAME:
2811
2812   case COMPONENT_REF:
2813   case INDIRECT_REF:
2814   case ALIGN_INDIRECT_REF:
2815   case MISALIGNED_INDIRECT_REF:
2816   case ARRAY_REF:
2817   case ARRAY_RANGE_REF:
2818   case BIT_FIELD_REF:
2819   case OBJ_TYPE_REF:
2820
2821   case REALPART_EXPR:
2822   case IMAGPART_EXPR:
2823   case PREINCREMENT_EXPR:
2824   case PREDECREMENT_EXPR:
2825   case SAVE_EXPR:
2826   case TRY_CATCH_EXPR:
2827   case WITH_CLEANUP_EXPR:
2828   case COMPOUND_EXPR:
2829   case MODIFY_EXPR:
2830   case TARGET_EXPR:
2831   case COND_EXPR:
2832   case BIND_EXPR:
2833     break;
2834
2835   default:
2836     /* Assume the worst for front-end tree codes.  */
2837     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2838       break;
2839     return false;
2840   }
2841
2842   return true;
2843 }
2844
2845 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2846
2847 tree
2848 non_lvalue_loc (location_t loc, tree x)
2849 {
2850   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2851      us.  */
2852   if (in_gimple_form)
2853     return x;
2854
2855   if (! maybe_lvalue_p (x))
2856     return x;
2857   x = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2858   SET_EXPR_LOCATION (x, loc);
2859   return x;
2860 }
2861
2862 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2863    Zero means allow extended lvalues.  */
2864
2865 int pedantic_lvalues;
2866
2867 /* When pedantic, return an expr equal to X but certainly not valid as a
2868    pedantic lvalue.  Otherwise, return X.  */
2869
2870 static tree
2871 pedantic_non_lvalue_loc (location_t loc, tree x)
2872 {
2873   if (pedantic_lvalues)
2874     return non_lvalue_loc (loc, x);
2875   protected_set_expr_location (x, loc);
2876   return x;
2877 }
2878 \f
2879 /* Given a tree comparison code, return the code that is the logical inverse
2880    of the given code.  It is not safe to do this for floating-point
2881    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2882    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2883
2884 enum tree_code
2885 invert_tree_comparison (enum tree_code code, bool honor_nans)
2886 {
2887   if (honor_nans && flag_trapping_math)
2888     return ERROR_MARK;
2889
2890   switch (code)
2891     {
2892     case EQ_EXPR:
2893       return NE_EXPR;
2894     case NE_EXPR:
2895       return EQ_EXPR;
2896     case GT_EXPR:
2897       return honor_nans ? UNLE_EXPR : LE_EXPR;
2898     case GE_EXPR:
2899       return honor_nans ? UNLT_EXPR : LT_EXPR;
2900     case LT_EXPR:
2901       return honor_nans ? UNGE_EXPR : GE_EXPR;
2902     case LE_EXPR:
2903       return honor_nans ? UNGT_EXPR : GT_EXPR;
2904     case LTGT_EXPR:
2905       return UNEQ_EXPR;
2906     case UNEQ_EXPR:
2907       return LTGT_EXPR;
2908     case UNGT_EXPR:
2909       return LE_EXPR;
2910     case UNGE_EXPR:
2911       return LT_EXPR;
2912     case UNLT_EXPR:
2913       return GE_EXPR;
2914     case UNLE_EXPR:
2915       return GT_EXPR;
2916     case ORDERED_EXPR:
2917       return UNORDERED_EXPR;
2918     case UNORDERED_EXPR:
2919       return ORDERED_EXPR;
2920     default:
2921       gcc_unreachable ();
2922     }
2923 }
2924
2925 /* Similar, but return the comparison that results if the operands are
2926    swapped.  This is safe for floating-point.  */
2927
2928 enum tree_code
2929 swap_tree_comparison (enum tree_code code)
2930 {
2931   switch (code)
2932     {
2933     case EQ_EXPR:
2934     case NE_EXPR:
2935     case ORDERED_EXPR:
2936     case UNORDERED_EXPR:
2937     case LTGT_EXPR:
2938     case UNEQ_EXPR:
2939       return code;
2940     case GT_EXPR:
2941       return LT_EXPR;
2942     case GE_EXPR:
2943       return LE_EXPR;
2944     case LT_EXPR:
2945       return GT_EXPR;
2946     case LE_EXPR:
2947       return GE_EXPR;
2948     case UNGT_EXPR:
2949       return UNLT_EXPR;
2950     case UNGE_EXPR:
2951       return UNLE_EXPR;
2952     case UNLT_EXPR:
2953       return UNGT_EXPR;
2954     case UNLE_EXPR:
2955       return UNGE_EXPR;
2956     default:
2957       gcc_unreachable ();
2958     }
2959 }
2960
2961
2962 /* Convert a comparison tree code from an enum tree_code representation
2963    into a compcode bit-based encoding.  This function is the inverse of
2964    compcode_to_comparison.  */
2965
2966 static enum comparison_code
2967 comparison_to_compcode (enum tree_code code)
2968 {
2969   switch (code)
2970     {
2971     case LT_EXPR:
2972       return COMPCODE_LT;
2973     case EQ_EXPR:
2974       return COMPCODE_EQ;
2975     case LE_EXPR:
2976       return COMPCODE_LE;
2977     case GT_EXPR:
2978       return COMPCODE_GT;
2979     case NE_EXPR:
2980       return COMPCODE_NE;
2981     case GE_EXPR:
2982       return COMPCODE_GE;
2983     case ORDERED_EXPR:
2984       return COMPCODE_ORD;
2985     case UNORDERED_EXPR:
2986       return COMPCODE_UNORD;
2987     case UNLT_EXPR:
2988       return COMPCODE_UNLT;
2989     case UNEQ_EXPR:
2990       return COMPCODE_UNEQ;
2991     case UNLE_EXPR:
2992       return COMPCODE_UNLE;
2993     case UNGT_EXPR:
2994       return COMPCODE_UNGT;
2995     case LTGT_EXPR:
2996       return COMPCODE_LTGT;
2997     case UNGE_EXPR:
2998       return COMPCODE_UNGE;
2999     default:
3000       gcc_unreachable ();
3001     }
3002 }
3003
3004 /* Convert a compcode bit-based encoding of a comparison operator back
3005    to GCC's enum tree_code representation.  This function is the
3006    inverse of comparison_to_compcode.  */
3007
3008 static enum tree_code
3009 compcode_to_comparison (enum comparison_code code)
3010 {
3011   switch (code)
3012     {
3013     case COMPCODE_LT:
3014       return LT_EXPR;
3015     case COMPCODE_EQ:
3016       return EQ_EXPR;
3017     case COMPCODE_LE:
3018       return LE_EXPR;
3019     case COMPCODE_GT:
3020       return GT_EXPR;
3021     case COMPCODE_NE:
3022       return NE_EXPR;
3023     case COMPCODE_GE:
3024       return GE_EXPR;
3025     case COMPCODE_ORD:
3026       return ORDERED_EXPR;
3027     case COMPCODE_UNORD:
3028       return UNORDERED_EXPR;
3029     case COMPCODE_UNLT:
3030       return UNLT_EXPR;
3031     case COMPCODE_UNEQ:
3032       return UNEQ_EXPR;
3033     case COMPCODE_UNLE:
3034       return UNLE_EXPR;
3035     case COMPCODE_UNGT:
3036       return UNGT_EXPR;
3037     case COMPCODE_LTGT:
3038       return LTGT_EXPR;
3039     case COMPCODE_UNGE:
3040       return UNGE_EXPR;
3041     default:
3042       gcc_unreachable ();
3043     }
3044 }
3045
3046 /* Return a tree for the comparison which is the combination of
3047    doing the AND or OR (depending on CODE) of the two operations LCODE
3048    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
3049    the possibility of trapping if the mode has NaNs, and return NULL_TREE
3050    if this makes the transformation invalid.  */
3051
3052 tree
3053 combine_comparisons (location_t loc,
3054                      enum tree_code code, enum tree_code lcode,
3055                      enum tree_code rcode, tree truth_type,
3056                      tree ll_arg, tree lr_arg)
3057 {
3058   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
3059   enum comparison_code lcompcode = comparison_to_compcode (lcode);
3060   enum comparison_code rcompcode = comparison_to_compcode (rcode);
3061   int compcode;
3062
3063   switch (code)
3064     {
3065     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3066       compcode = lcompcode & rcompcode;
3067       break;
3068
3069     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3070       compcode = lcompcode | rcompcode;
3071       break;
3072
3073     default:
3074       return NULL_TREE;
3075     }
3076
3077   if (!honor_nans)
3078     {
3079       /* Eliminate unordered comparisons, as well as LTGT and ORD
3080          which are not used unless the mode has NaNs.  */
3081       compcode &= ~COMPCODE_UNORD;
3082       if (compcode == COMPCODE_LTGT)
3083         compcode = COMPCODE_NE;
3084       else if (compcode == COMPCODE_ORD)
3085         compcode = COMPCODE_TRUE;
3086     }
3087    else if (flag_trapping_math)
3088      {
3089         /* Check that the original operation and the optimized ones will trap
3090            under the same condition.  */
3091         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
3092                      && (lcompcode != COMPCODE_EQ)
3093                      && (lcompcode != COMPCODE_ORD);
3094         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
3095                      && (rcompcode != COMPCODE_EQ)
3096                      && (rcompcode != COMPCODE_ORD);
3097         bool trap = (compcode & COMPCODE_UNORD) == 0
3098                     && (compcode != COMPCODE_EQ)
3099                     && (compcode != COMPCODE_ORD);
3100
3101         /* In a short-circuited boolean expression the LHS might be
3102            such that the RHS, if evaluated, will never trap.  For
3103            example, in ORD (x, y) && (x < y), we evaluate the RHS only
3104            if neither x nor y is NaN.  (This is a mixed blessing: for
3105            example, the expression above will never trap, hence
3106            optimizing it to x < y would be invalid).  */
3107         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3108             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3109           rtrap = false;
3110
3111         /* If the comparison was short-circuited, and only the RHS
3112            trapped, we may now generate a spurious trap.  */
3113         if (rtrap && !ltrap
3114             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3115           return NULL_TREE;
3116
3117         /* If we changed the conditions that cause a trap, we lose.  */
3118         if ((ltrap || rtrap) != trap)
3119           return NULL_TREE;
3120       }
3121
3122   if (compcode == COMPCODE_TRUE)
3123     return constant_boolean_node (true, truth_type);
3124   else if (compcode == COMPCODE_FALSE)
3125     return constant_boolean_node (false, truth_type);
3126   else
3127     {
3128       enum tree_code tcode;
3129
3130       tcode = compcode_to_comparison ((enum comparison_code) compcode);
3131       return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
3132     }
3133 }
3134 \f
3135 /* Return nonzero if two operands (typically of the same tree node)
3136    are necessarily equal.  If either argument has side-effects this
3137    function returns zero.  FLAGS modifies behavior as follows:
3138
3139    If OEP_ONLY_CONST is set, only return nonzero for constants.
3140    This function tests whether the operands are indistinguishable;
3141    it does not test whether they are equal using C's == operation.
3142    The distinction is important for IEEE floating point, because
3143    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3144    (2) two NaNs may be indistinguishable, but NaN!=NaN.
3145
3146    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3147    even though it may hold multiple values during a function.
3148    This is because a GCC tree node guarantees that nothing else is
3149    executed between the evaluation of its "operands" (which may often
3150    be evaluated in arbitrary order).  Hence if the operands themselves
3151    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3152    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
3153    unset means assuming isochronic (or instantaneous) tree equivalence.
3154    Unless comparing arbitrary expression trees, such as from different
3155    statements, this flag can usually be left unset.
3156
3157    If OEP_PURE_SAME is set, then pure functions with identical arguments
3158    are considered the same.  It is used when the caller has other ways
3159    to ensure that global memory is unchanged in between.  */
3160
3161 int
3162 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3163 {
3164   /* If either is ERROR_MARK, they aren't equal.  */
3165   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3166     return 0;
3167
3168   /* Check equality of integer constants before bailing out due to
3169      precision differences.  */
3170   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3171     return tree_int_cst_equal (arg0, arg1);
3172
3173   /* If both types don't have the same signedness, then we can't consider
3174      them equal.  We must check this before the STRIP_NOPS calls
3175      because they may change the signedness of the arguments.  As pointers
3176      strictly don't have a signedness, require either two pointers or
3177      two non-pointers as well.  */
3178   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3179       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
3180     return 0;
3181
3182   /* We cannot consider pointers to different address space equal.  */
3183   if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
3184       && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
3185           != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
3186     return 0;
3187
3188   /* If both types don't have the same precision, then it is not safe
3189      to strip NOPs.  */
3190   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3191     return 0;
3192
3193   STRIP_NOPS (arg0);
3194   STRIP_NOPS (arg1);
3195
3196   /* In case both args are comparisons but with different comparison
3197      code, try to swap the comparison operands of one arg to produce
3198      a match and compare that variant.  */
3199   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3200       && COMPARISON_CLASS_P (arg0)
3201       && COMPARISON_CLASS_P (arg1))
3202     {
3203       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3204
3205       if (TREE_CODE (arg0) == swap_code)
3206         return operand_equal_p (TREE_OPERAND (arg0, 0),
3207                                 TREE_OPERAND (arg1, 1), flags)
3208                && operand_equal_p (TREE_OPERAND (arg0, 1),
3209                                    TREE_OPERAND (arg1, 0), flags);
3210     }
3211
3212   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3213       /* This is needed for conversions and for COMPONENT_REF.
3214          Might as well play it safe and always test this.  */
3215       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3216       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3217       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3218     return 0;
3219
3220   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3221      We don't care about side effects in that case because the SAVE_EXPR
3222      takes care of that for us. In all other cases, two expressions are
3223      equal if they have no side effects.  If we have two identical
3224      expressions with side effects that should be treated the same due
3225      to the only side effects being identical SAVE_EXPR's, that will
3226      be detected in the recursive calls below.  */
3227   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3228       && (TREE_CODE (arg0) == SAVE_EXPR
3229           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3230     return 1;
3231
3232   /* Next handle constant cases, those for which we can return 1 even
3233      if ONLY_CONST is set.  */
3234   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3235     switch (TREE_CODE (arg0))
3236       {
3237       case INTEGER_CST:
3238         return tree_int_cst_equal (arg0, arg1);
3239
3240       case FIXED_CST:
3241         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3242                                        TREE_FIXED_CST (arg1));
3243
3244       case REAL_CST:
3245         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3246                                    TREE_REAL_CST (arg1)))
3247           return 1;
3248
3249
3250         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3251           {
3252             /* If we do not distinguish between signed and unsigned zero,
3253                consider them equal.  */
3254             if (real_zerop (arg0) && real_zerop (arg1))
3255               return 1;
3256           }
3257         return 0;
3258
3259       case VECTOR_CST:
3260         {
3261           tree v1, v2;
3262
3263           v1 = TREE_VECTOR_CST_ELTS (arg0);
3264           v2 = TREE_VECTOR_CST_ELTS (arg1);
3265           while (v1 && v2)
3266             {
3267               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3268                                     flags))
3269                 return 0;
3270               v1 = TREE_CHAIN (v1);
3271               v2 = TREE_CHAIN (v2);
3272             }
3273
3274           return v1 == v2;
3275         }
3276
3277       case COMPLEX_CST:
3278         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3279                                  flags)
3280                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3281                                     flags));
3282
3283       case STRING_CST:
3284         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3285                 && ! memcmp (TREE_STRING_POINTER (arg0),
3286                               TREE_STRING_POINTER (arg1),
3287                               TREE_STRING_LENGTH (arg0)));
3288
3289       case ADDR_EXPR:
3290         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3291                                 0);
3292       default:
3293         break;
3294       }
3295
3296   if (flags & OEP_ONLY_CONST)
3297     return 0;
3298
3299 /* Define macros to test an operand from arg0 and arg1 for equality and a
3300    variant that allows null and views null as being different from any
3301    non-null value.  In the latter case, if either is null, the both
3302    must be; otherwise, do the normal comparison.  */
3303 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
3304                                     TREE_OPERAND (arg1, N), flags)
3305
3306 #define OP_SAME_WITH_NULL(N)                            \
3307   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3308    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3309
3310   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3311     {
3312     case tcc_unary:
3313       /* Two conversions are equal only if signedness and modes match.  */
3314       switch (TREE_CODE (arg0))
3315         {
3316         CASE_CONVERT:
3317         case FIX_TRUNC_EXPR:
3318           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3319               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3320             return 0;
3321           break;
3322         default:
3323           break;
3324         }
3325
3326       return OP_SAME (0);
3327
3328
3329     case tcc_comparison:
3330     case tcc_binary:
3331       if (OP_SAME (0) && OP_SAME (1))
3332         return 1;
3333
3334       /* For commutative ops, allow the other order.  */
3335       return (commutative_tree_code (TREE_CODE (arg0))
3336               && operand_equal_p (TREE_OPERAND (arg0, 0),
3337                                   TREE_OPERAND (arg1, 1), flags)
3338               && operand_equal_p (TREE_OPERAND (arg0, 1),
3339                                   TREE_OPERAND (arg1, 0), flags));
3340
3341     case tcc_reference:
3342       /* If either of the pointer (or reference) expressions we are
3343          dereferencing contain a side effect, these cannot be equal.  */
3344       if (TREE_SIDE_EFFECTS (arg0)
3345           || TREE_SIDE_EFFECTS (arg1))
3346         return 0;
3347
3348       switch (TREE_CODE (arg0))
3349         {
3350         case INDIRECT_REF:
3351         case ALIGN_INDIRECT_REF:
3352         case MISALIGNED_INDIRECT_REF:
3353         case REALPART_EXPR:
3354         case IMAGPART_EXPR:
3355           return OP_SAME (0);
3356
3357         case ARRAY_REF:
3358         case ARRAY_RANGE_REF:
3359           /* Operands 2 and 3 may be null.
3360              Compare the array index by value if it is constant first as we
3361              may have different types but same value here.  */
3362           return (OP_SAME (0)
3363                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3364                                           TREE_OPERAND (arg1, 1))
3365                       || OP_SAME (1))
3366                   && OP_SAME_WITH_NULL (2)
3367                   && OP_SAME_WITH_NULL (3));
3368
3369         case COMPONENT_REF:
3370           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
3371              may be NULL when we're called to compare MEM_EXPRs.  */
3372           return OP_SAME_WITH_NULL (0)
3373                  && OP_SAME (1)
3374                  && OP_SAME_WITH_NULL (2);
3375
3376         case BIT_FIELD_REF:
3377           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3378
3379         default:
3380           return 0;
3381         }
3382
3383     case tcc_expression:
3384       switch (TREE_CODE (arg0))
3385         {
3386         case ADDR_EXPR:
3387         case TRUTH_NOT_EXPR:
3388           return OP_SAME (0);
3389
3390         case TRUTH_ANDIF_EXPR:
3391         case TRUTH_ORIF_EXPR:
3392           return OP_SAME (0) && OP_SAME (1);
3393
3394         case TRUTH_AND_EXPR:
3395         case TRUTH_OR_EXPR:
3396         case TRUTH_XOR_EXPR:
3397           if (OP_SAME (0) && OP_SAME (1))
3398             return 1;
3399
3400           /* Otherwise take into account this is a commutative operation.  */
3401           return (operand_equal_p (TREE_OPERAND (arg0, 0),
3402                                    TREE_OPERAND (arg1, 1), flags)
3403                   && operand_equal_p (TREE_OPERAND (arg0, 1),
3404                                       TREE_OPERAND (arg1, 0), flags));
3405
3406         case COND_EXPR:
3407           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3408
3409         default:
3410           return 0;
3411         }
3412
3413     case tcc_vl_exp:
3414       switch (TREE_CODE (arg0))
3415         {
3416         case CALL_EXPR:
3417           /* If the CALL_EXPRs call different functions, then they
3418              clearly can not be equal.  */
3419           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3420                                  flags))
3421             return 0;
3422
3423           {
3424             unsigned int cef = call_expr_flags (arg0);
3425             if (flags & OEP_PURE_SAME)
3426               cef &= ECF_CONST | ECF_PURE;
3427             else
3428               cef &= ECF_CONST;
3429             if (!cef)
3430               return 0;
3431           }
3432
3433           /* Now see if all the arguments are the same.  */
3434           {
3435             const_call_expr_arg_iterator iter0, iter1;
3436             const_tree a0, a1;
3437             for (a0 = first_const_call_expr_arg (arg0, &iter0),
3438                    a1 = first_const_call_expr_arg (arg1, &iter1);
3439                  a0 && a1;
3440                  a0 = next_const_call_expr_arg (&iter0),
3441                    a1 = next_const_call_expr_arg (&iter1))
3442               if (! operand_equal_p (a0, a1, flags))
3443                 return 0;
3444
3445             /* If we get here and both argument lists are exhausted
3446                then the CALL_EXPRs are equal.  */
3447             return ! (a0 || a1);
3448           }
3449         default:
3450           return 0;
3451         }
3452
3453     case tcc_declaration:
3454       /* Consider __builtin_sqrt equal to sqrt.  */
3455       return (TREE_CODE (arg0) == FUNCTION_DECL
3456               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3457               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3458               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3459
3460     default:
3461       return 0;
3462     }
3463
3464 #undef OP_SAME
3465 #undef OP_SAME_WITH_NULL
3466 }
3467 \f
3468 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3469    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3470
3471    When in doubt, return 0.  */
3472
3473 static int
3474 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3475 {
3476   int unsignedp1, unsignedpo;
3477   tree primarg0, primarg1, primother;
3478   unsigned int correct_width;
3479
3480   if (operand_equal_p (arg0, arg1, 0))
3481     return 1;
3482
3483   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3484       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3485     return 0;
3486
3487   /* Discard any conversions that don't change the modes of ARG0 and ARG1
3488      and see if the inner values are the same.  This removes any
3489      signedness comparison, which doesn't matter here.  */
3490   primarg0 = arg0, primarg1 = arg1;
3491   STRIP_NOPS (primarg0);
3492   STRIP_NOPS (primarg1);
3493   if (operand_equal_p (primarg0, primarg1, 0))
3494     return 1;
3495
3496   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3497      actual comparison operand, ARG0.
3498
3499      First throw away any conversions to wider types
3500      already present in the operands.  */
3501
3502   primarg1 = get_narrower (arg1, &unsignedp1);
3503   primother = get_narrower (other, &unsignedpo);
3504
3505   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3506   if (unsignedp1 == unsignedpo
3507       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3508       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3509     {
3510       tree type = TREE_TYPE (arg0);
3511
3512       /* Make sure shorter operand is extended the right way
3513          to match the longer operand.  */
3514       primarg1 = fold_convert (signed_or_unsigned_type_for
3515                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3516
3517       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3518         return 1;
3519     }
3520
3521   return 0;
3522 }
3523 \f
3524 /* See if ARG is an expression that is either a comparison or is performing
3525    arithmetic on comparisons.  The comparisons must only be comparing
3526    two different values, which will be stored in *CVAL1 and *CVAL2; if
3527    they are nonzero it means that some operands have already been found.
3528    No variables may be used anywhere else in the expression except in the
3529    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
3530    the expression and save_expr needs to be called with CVAL1 and CVAL2.
3531
3532    If this is true, return 1.  Otherwise, return zero.  */
3533
3534 static int
3535 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3536 {
3537   enum tree_code code = TREE_CODE (arg);
3538   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3539
3540   /* We can handle some of the tcc_expression cases here.  */
3541   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3542     tclass = tcc_unary;
3543   else if (tclass == tcc_expression
3544            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3545                || code == COMPOUND_EXPR))
3546     tclass = tcc_binary;
3547
3548   else if (tclass == tcc_expression && code == SAVE_EXPR
3549            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3550     {
3551       /* If we've already found a CVAL1 or CVAL2, this expression is
3552          two complex to handle.  */
3553       if (*cval1 || *cval2)
3554         return 0;
3555
3556       tclass = tcc_unary;
3557       *save_p = 1;
3558     }
3559
3560   switch (tclass)
3561     {
3562     case tcc_unary:
3563       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3564
3565     case tcc_binary:
3566       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3567               && twoval_comparison_p (TREE_OPERAND (arg, 1),
3568                                       cval1, cval2, save_p));
3569
3570     case tcc_constant:
3571       return 1;
3572
3573     case tcc_expression:
3574       if (code == COND_EXPR)
3575         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3576                                      cval1, cval2, save_p)
3577                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3578                                         cval1, cval2, save_p)
3579                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3580                                         cval1, cval2, save_p));
3581       return 0;
3582
3583     case tcc_comparison:
3584       /* First see if we can handle the first operand, then the second.  For
3585          the second operand, we know *CVAL1 can't be zero.  It must be that
3586          one side of the comparison is each of the values; test for the
3587          case where this isn't true by failing if the two operands
3588          are the same.  */
3589
3590       if (operand_equal_p (TREE_OPERAND (arg, 0),
3591                            TREE_OPERAND (arg, 1), 0))
3592         return 0;
3593
3594       if (*cval1 == 0)
3595         *cval1 = TREE_OPERAND (arg, 0);
3596       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3597         ;
3598       else if (*cval2 == 0)
3599         *cval2 = TREE_OPERAND (arg, 0);
3600       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3601         ;
3602       else
3603         return 0;
3604
3605       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3606         ;
3607       else if (*cval2 == 0)
3608         *cval2 = TREE_OPERAND (arg, 1);
3609       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3610         ;
3611       else
3612         return 0;
3613
3614       return 1;
3615
3616     default:
3617       return 0;
3618     }
3619 }
3620 \f
3621 /* ARG is a tree that is known to contain just arithmetic operations and
3622    comparisons.  Evaluate the operations in the tree substituting NEW0 for
3623    any occurrence of OLD0 as an operand of a comparison and likewise for
3624    NEW1 and OLD1.  */
3625
3626 static tree
3627 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3628             tree old1, tree new1)
3629 {
3630   tree type = TREE_TYPE (arg);
3631   enum tree_code code = TREE_CODE (arg);
3632   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3633
3634   /* We can handle some of the tcc_expression cases here.  */
3635   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3636     tclass = tcc_unary;
3637   else if (tclass == tcc_expression
3638            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3639     tclass = tcc_binary;
3640
3641   switch (tclass)
3642     {
3643     case tcc_unary:
3644       return fold_build1_loc (loc, code, type,
3645                           eval_subst (loc, TREE_OPERAND (arg, 0),
3646                                       old0, new0, old1, new1));
3647
3648     case tcc_binary:
3649       return fold_build2_loc (loc, code, type,
3650                           eval_subst (loc, TREE_OPERAND (arg, 0),
3651                                       old0, new0, old1, new1),
3652                           eval_subst (loc, TREE_OPERAND (arg, 1),
3653                                       old0, new0, old1, new1));
3654
3655     case tcc_expression:
3656       switch (code)
3657         {
3658         case SAVE_EXPR:
3659           return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3660                              old1, new1);
3661
3662         case COMPOUND_EXPR:
3663           return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3664                              old1, new1);
3665
3666         case COND_EXPR:
3667           return fold_build3_loc (loc, code, type,
3668                               eval_subst (loc, TREE_OPERAND (arg, 0),
3669                                           old0, new0, old1, new1),
3670                               eval_subst (loc, TREE_OPERAND (arg, 1),
3671                                           old0, new0, old1, new1),
3672                               eval_subst (loc, TREE_OPERAND (arg, 2),
3673                                           old0, new0, old1, new1));
3674         default:
3675           break;
3676         }
3677       /* Fall through - ???  */
3678
3679     case tcc_comparison:
3680       {
3681         tree arg0 = TREE_OPERAND (arg, 0);
3682         tree arg1 = TREE_OPERAND (arg, 1);
3683
3684         /* We need to check both for exact equality and tree equality.  The
3685            former will be true if the operand has a side-effect.  In that
3686            case, we know the operand occurred exactly once.  */
3687
3688         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3689           arg0 = new0;
3690         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3691           arg0 = new1;
3692
3693         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3694           arg1 = new0;
3695         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3696           arg1 = new1;
3697
3698         return fold_build2_loc (loc, code, type, arg0, arg1);
3699       }
3700
3701     default:
3702       return arg;
3703     }
3704 }
3705 \f
3706 /* Return a tree for the case when the result of an expression is RESULT
3707    converted to TYPE and OMITTED was previously an operand of the expression
3708    but is now not needed (e.g., we folded OMITTED * 0).
3709
3710    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
3711    the conversion of RESULT to TYPE.  */
3712
3713 tree
3714 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3715 {
3716   tree t = fold_convert_loc (loc, type, result);
3717
3718   /* If the resulting operand is an empty statement, just return the omitted
3719      statement casted to void. */
3720   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3721     {
3722       t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3723       goto omit_one_operand_exit;
3724     }
3725
3726   if (TREE_SIDE_EFFECTS (omitted))
3727     {
3728       t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3729       goto omit_one_operand_exit;
3730     }
3731
3732   return non_lvalue_loc (loc, t);
3733
3734  omit_one_operand_exit:
3735   protected_set_expr_location (t, loc);
3736   return t;
3737 }
3738
3739 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3740
3741 static tree
3742 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
3743                                tree omitted)
3744 {
3745   tree t = fold_convert_loc (loc, type, result);
3746
3747   /* If the resulting operand is an empty statement, just return the omitted
3748      statement casted to void. */
3749   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3750     {
3751       t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3752       goto pedantic_omit_one_operand_exit;
3753     }
3754
3755   if (TREE_SIDE_EFFECTS (omitted))
3756     {
3757       t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3758       goto pedantic_omit_one_operand_exit;
3759     }
3760
3761   return pedantic_non_lvalue_loc (loc, t);
3762
3763  pedantic_omit_one_operand_exit:
3764   protected_set_expr_location (t, loc);
3765   return t;
3766 }
3767
3768 /* Return a tree for the case when the result of an expression is RESULT
3769    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3770    of the expression but are now not needed.
3771
3772    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3773    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3774    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3775    just do the conversion of RESULT to TYPE.  */
3776
3777 tree
3778 omit_two_operands_loc (location_t loc, tree type, tree result,
3779                    tree omitted1, tree omitted2)
3780 {
3781   tree t = fold_convert_loc (loc, type, result);
3782
3783   if (TREE_SIDE_EFFECTS (omitted2))
3784     {
3785       t = build2 (COMPOUND_EXPR, type, omitted2, t);
3786       SET_EXPR_LOCATION (t, loc);
3787     }
3788   if (TREE_SIDE_EFFECTS (omitted1))
3789     {
3790       t = build2 (COMPOUND_EXPR, type, omitted1, t);
3791       SET_EXPR_LOCATION (t, loc);
3792     }
3793
3794   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3795 }
3796
3797 \f
3798 /* Return a simplified tree node for the truth-negation of ARG.  This
3799    never alters ARG itself.  We assume that ARG is an operation that
3800    returns a truth value (0 or 1).
3801
3802    FIXME: one would think we would fold the result, but it causes
3803    problems with the dominator optimizer.  */
3804
3805 tree
3806 fold_truth_not_expr (location_t loc, tree arg)
3807 {
3808   tree t, type = TREE_TYPE (arg);
3809   enum tree_code code = TREE_CODE (arg);
3810   location_t loc1, loc2;
3811
3812   /* If this is a comparison, we can simply invert it, except for
3813      floating-point non-equality comparisons, in which case we just
3814      enclose a TRUTH_NOT_EXPR around what we have.  */
3815
3816   if (TREE_CODE_CLASS (code) == tcc_comparison)
3817     {
3818       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3819       if (FLOAT_TYPE_P (op_type)
3820           && flag_trapping_math
3821           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3822           && code != NE_EXPR && code != EQ_EXPR)
3823         return NULL_TREE;
3824
3825       code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3826       if (code == ERROR_MARK)
3827         return NULL_TREE;
3828
3829       t = build2 (code, type, TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3830       SET_EXPR_LOCATION (t, loc);
3831       return t;
3832     }
3833
3834   switch (code)
3835     {
3836     case INTEGER_CST:
3837       return constant_boolean_node (integer_zerop (arg), type);
3838
3839     case TRUTH_AND_EXPR:
3840       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3841       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3842       if (loc1 == UNKNOWN_LOCATION)
3843         loc1 = loc;
3844       if (loc2 == UNKNOWN_LOCATION)
3845         loc2 = loc;
3846       t = build2 (TRUTH_OR_EXPR, type,
3847                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3848                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3849       break;
3850
3851     case TRUTH_OR_EXPR:
3852       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3853       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3854       if (loc1 == UNKNOWN_LOCATION)
3855         loc1 = loc;
3856       if (loc2 == UNKNOWN_LOCATION)
3857         loc2 = loc;
3858       t = build2 (TRUTH_AND_EXPR, type,
3859                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3860                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3861       break;
3862
3863     case TRUTH_XOR_EXPR:
3864       /* Here we can invert either operand.  We invert the first operand
3865          unless the second operand is a TRUTH_NOT_EXPR in which case our
3866          result is the XOR of the first operand with the inside of the
3867          negation of the second operand.  */
3868
3869       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3870         t = build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3871                     TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3872       else
3873         t = build2 (TRUTH_XOR_EXPR, type,
3874                     invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3875                     TREE_OPERAND (arg, 1));
3876       break;
3877
3878     case TRUTH_ANDIF_EXPR:
3879       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3880       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3881       if (loc1 == UNKNOWN_LOCATION)
3882         loc1 = loc;
3883       if (loc2 == UNKNOWN_LOCATION)
3884         loc2 = loc;
3885       t = build2 (TRUTH_ORIF_EXPR, type,
3886                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3887                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3888       break;
3889
3890     case TRUTH_ORIF_EXPR:
3891       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3892       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3893       if (loc1 == UNKNOWN_LOCATION)
3894         loc1 = loc;
3895       if (loc2 == UNKNOWN_LOCATION)
3896         loc2 = loc;
3897       t = build2 (TRUTH_ANDIF_EXPR, type,
3898                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3899                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3900       break;
3901
3902     case TRUTH_NOT_EXPR:
3903       return TREE_OPERAND (arg, 0);
3904
3905     case COND_EXPR:
3906       {
3907         tree arg1 = TREE_OPERAND (arg, 1);
3908         tree arg2 = TREE_OPERAND (arg, 2);
3909
3910         loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3911         loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 2));
3912         if (loc1 == UNKNOWN_LOCATION)
3913           loc1 = loc;
3914         if (loc2 == UNKNOWN_LOCATION)
3915           loc2 = loc;
3916
3917         /* A COND_EXPR may have a throw as one operand, which
3918            then has void type.  Just leave void operands
3919            as they are.  */
3920         t = build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3921                     VOID_TYPE_P (TREE_TYPE (arg1))
3922                     ? arg1 : invert_truthvalue_loc (loc1, arg1),
3923                     VOID_TYPE_P (TREE_TYPE (arg2))
3924                     ? arg2 : invert_truthvalue_loc (loc2, arg2));
3925         break;
3926       }
3927
3928     case COMPOUND_EXPR:
3929       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3930       if (loc1 == UNKNOWN_LOCATION)
3931         loc1 = loc;
3932       t = build2 (COMPOUND_EXPR, type,
3933                   TREE_OPERAND (arg, 0),
3934                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3935       break;
3936
3937     case NON_LVALUE_EXPR:
3938       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3939       if (loc1 == UNKNOWN_LOCATION)
3940         loc1 = loc;
3941       return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3942
3943     CASE_CONVERT:
3944       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3945         {
3946           t = build1 (TRUTH_NOT_EXPR, type, arg);
3947           break;
3948         }
3949
3950       /* ... fall through ...  */
3951
3952     case FLOAT_EXPR:
3953       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3954       if (loc1 == UNKNOWN_LOCATION)
3955         loc1 = loc;
3956       t = build1 (TREE_CODE (arg), type,
3957                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3958       break;
3959
3960     case BIT_AND_EXPR:
3961       if (!integer_onep (TREE_OPERAND (arg, 1)))
3962         return NULL_TREE;
3963       t = build2 (EQ_EXPR, type, arg, build_int_cst (type, 0));
3964       break;
3965
3966     case SAVE_EXPR:
3967       t = build1 (TRUTH_NOT_EXPR, type, arg);
3968       break;
3969
3970     case CLEANUP_POINT_EXPR:
3971       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3972       if (loc1 == UNKNOWN_LOCATION)
3973         loc1 = loc;
3974       t = build1 (CLEANUP_POINT_EXPR, type,
3975                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3976       break;
3977
3978     default:
3979       t = NULL_TREE;
3980       break;
3981     }
3982
3983   if (t)
3984     SET_EXPR_LOCATION (t, loc);
3985
3986   return t;
3987 }
3988
3989 /* Return a simplified tree node for the truth-negation of ARG.  This
3990    never alters ARG itself.  We assume that ARG is an operation that
3991    returns a truth value (0 or 1).
3992
3993    FIXME: one would think we would fold the result, but it causes
3994    problems with the dominator optimizer.  */
3995
3996 tree
3997 invert_truthvalue_loc (location_t loc, tree arg)
3998 {
3999   tree tem;
4000
4001   if (TREE_CODE (arg) == ERROR_MARK)
4002     return arg;
4003
4004   tem = fold_truth_not_expr (loc, arg);
4005   if (!tem)
4006     {
4007       tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
4008       SET_EXPR_LOCATION (tem, loc);
4009     }
4010
4011   return tem;
4012 }
4013
4014 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
4015    operands are another bit-wise operation with a common input.  If so,
4016    distribute the bit operations to save an operation and possibly two if
4017    constants are involved.  For example, convert
4018         (A | B) & (A | C) into A | (B & C)
4019    Further simplification will occur if B and C are constants.
4020
4021    If this optimization cannot be done, 0 will be returned.  */
4022
4023 static tree
4024 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
4025                      tree arg0, tree arg1)
4026 {
4027   tree common;
4028   tree left, right;
4029
4030   if (TREE_CODE (arg0) != TREE_CODE (arg1)
4031       || TREE_CODE (arg0) == code
4032       || (TREE_CODE (arg0) != BIT_AND_EXPR
4033           && TREE_CODE (arg0) != BIT_IOR_EXPR))
4034     return 0;
4035
4036   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
4037     {
4038       common = TREE_OPERAND (arg0, 0);
4039       left = TREE_OPERAND (arg0, 1);
4040       right = TREE_OPERAND (arg1, 1);
4041     }
4042   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
4043     {
4044       common = TREE_OPERAND (arg0, 0);
4045       left = TREE_OPERAND (arg0, 1);
4046       right = TREE_OPERAND (arg1, 0);
4047     }
4048   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
4049     {
4050       common = TREE_OPERAND (arg0, 1);
4051       left = TREE_OPERAND (arg0, 0);
4052       right = TREE_OPERAND (arg1, 1);
4053     }
4054   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
4055     {
4056       common = TREE_OPERAND (arg0, 1);
4057       left = TREE_OPERAND (arg0, 0);
4058       right = TREE_OPERAND (arg1, 0);
4059     }
4060   else
4061     return 0;
4062
4063   common = fold_convert_loc (loc, type, common);
4064   left = fold_convert_loc (loc, type, left);
4065   right = fold_convert_loc (loc, type, right);
4066   return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
4067                       fold_build2_loc (loc, code, type, left, right));
4068 }
4069
4070 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
4071    with code CODE.  This optimization is unsafe.  */
4072 static tree
4073 distribute_real_division (location_t loc, enum tree_code code, tree type,
4074                           tree arg0, tree arg1)
4075 {
4076   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
4077   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
4078
4079   /* (A / C) +- (B / C) -> (A +- B) / C.  */
4080   if (mul0 == mul1
4081       && operand_equal_p (TREE_OPERAND (arg0, 1),
4082                        TREE_OPERAND (arg1, 1), 0))
4083     return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
4084                         fold_build2_loc (loc, code, type,
4085                                      TREE_OPERAND (arg0, 0),
4086                                      TREE_OPERAND (arg1, 0)),
4087                         TREE_OPERAND (arg0, 1));
4088
4089   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
4090   if (operand_equal_p (TREE_OPERAND (arg0, 0),
4091                        TREE_OPERAND (arg1, 0), 0)
4092       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
4093       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
4094     {
4095       REAL_VALUE_TYPE r0, r1;
4096       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
4097       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
4098       if (!mul0)
4099         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
4100       if (!mul1)
4101         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
4102       real_arithmetic (&r0, code, &r0, &r1);
4103       return fold_build2_loc (loc, MULT_EXPR, type,
4104                           TREE_OPERAND (arg0, 0),
4105                           build_real (type, r0));
4106     }
4107
4108   return NULL_TREE;
4109 }
4110 \f
4111 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4112    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
4113
4114 static tree
4115 make_bit_field_ref (location_t loc, tree inner, tree type,
4116                     HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
4117 {
4118   tree result, bftype;
4119
4120   if (bitpos == 0)
4121     {
4122       tree size = TYPE_SIZE (TREE_TYPE (inner));
4123       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4124            || POINTER_TYPE_P (TREE_TYPE (inner)))
4125           && host_integerp (size, 0)
4126           && tree_low_cst (size, 0) == bitsize)
4127         return fold_convert_loc (loc, type, inner);
4128     }
4129
4130   bftype = type;
4131   if (TYPE_PRECISION (bftype) != bitsize
4132       || TYPE_UNSIGNED (bftype) == !unsignedp)
4133     bftype = build_nonstandard_integer_type (bitsize, 0);
4134
4135   result = build3 (BIT_FIELD_REF, bftype, inner,
4136                    size_int (bitsize), bitsize_int (bitpos));
4137   SET_EXPR_LOCATION (result, loc);
4138
4139   if (bftype != type)
4140     result = fold_convert_loc (loc, type, result);
4141
4142   return result;
4143 }
4144
4145 /* Optimize a bit-field compare.
4146
4147    There are two cases:  First is a compare against a constant and the
4148    second is a comparison of two items where the fields are at the same
4149    bit position relative to the start of a chunk (byte, halfword, word)
4150    large enough to contain it.  In these cases we can avoid the shift
4151    implicit in bitfield extractions.
4152
4153    For constants, we emit a compare of the shifted constant with the
4154    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4155    compared.  For two fields at the same position, we do the ANDs with the
4156    similar mask and compare the result of the ANDs.
4157
4158    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4159    COMPARE_TYPE is the type of the comparison, and LHS and RHS
4160    are the left and right operands of the comparison, respectively.
4161
4162    If the optimization described above can be done, we return the resulting
4163    tree.  Otherwise we return zero.  */
4164
4165 static tree
4166 optimize_bit_field_compare (location_t loc, enum tree_code code,
4167                             tree compare_type, tree lhs, tree rhs)
4168 {
4169   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
4170   tree type = TREE_TYPE (lhs);
4171   tree signed_type, unsigned_type;
4172   int const_p = TREE_CODE (rhs) == INTEGER_CST;
4173   enum machine_mode lmode, rmode, nmode;
4174   int lunsignedp, runsignedp;
4175   int lvolatilep = 0, rvolatilep = 0;
4176   tree linner, rinner = NULL_TREE;
4177   tree mask;
4178   tree offset;
4179
4180   /* Get all the information about the extractions being done.  If the bit size
4181      if the same as the size of the underlying object, we aren't doing an
4182      extraction at all and so can do nothing.  We also don't want to
4183      do anything if the inner expression is a PLACEHOLDER_EXPR since we
4184      then will no longer be able to replace it.  */
4185   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
4186                                 &lunsignedp, &lvolatilep, false);
4187   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
4188       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
4189     return 0;
4190
4191  if (!const_p)
4192    {
4193      /* If this is not a constant, we can only do something if bit positions,
4194         sizes, and signedness are the same.  */
4195      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4196                                    &runsignedp, &rvolatilep, false);
4197
4198      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
4199          || lunsignedp != runsignedp || offset != 0
4200          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
4201        return 0;
4202    }
4203
4204   /* See if we can find a mode to refer to this field.  We should be able to,
4205      but fail if we can't.  */
4206   nmode = get_best_mode (lbitsize, lbitpos,
4207                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4208                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4209                                 TYPE_ALIGN (TREE_TYPE (rinner))),
4210                          word_mode, lvolatilep || rvolatilep);
4211   if (nmode == VOIDmode)
4212     return 0;
4213
4214   /* Set signed and unsigned types of the precision of this mode for the
4215      shifts below.  */
4216   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
4217   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4218
4219   /* Compute the bit position and size for the new reference and our offset
4220      within it. If the new reference is the same size as the original, we
4221      won't optimize anything, so return zero.  */
4222   nbitsize = GET_MODE_BITSIZE (nmode);
4223   nbitpos = lbitpos & ~ (nbitsize - 1);
4224   lbitpos -= nbitpos;
4225   if (nbitsize == lbitsize)
4226     return 0;
4227
4228   if (BYTES_BIG_ENDIAN)
4229     lbitpos = nbitsize - lbitsize - lbitpos;
4230
4231   /* Make the mask to be used against the extracted field.  */
4232   mask = build_int_cst_type (unsigned_type, -1);
4233   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
4234   mask = const_binop (RSHIFT_EXPR, mask,
4235                       size_int (nbitsize - lbitsize - lbitpos), 0);
4236
4237   if (! const_p)
4238     /* If not comparing with constant, just rework the comparison
4239        and return.  */
4240     return fold_build2_loc (loc, code, compare_type,
4241                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4242                                      make_bit_field_ref (loc, linner,
4243                                                          unsigned_type,
4244                                                          nbitsize, nbitpos,
4245                                                          1),
4246                                      mask),
4247                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4248                                      make_bit_field_ref (loc, rinner,
4249                                                          unsigned_type,
4250                                                          nbitsize, nbitpos,
4251                                                          1),
4252                                      mask));
4253
4254   /* Otherwise, we are handling the constant case. See if the constant is too
4255      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
4256      this not only for its own sake, but to avoid having to test for this
4257      error case below.  If we didn't, we might generate wrong code.
4258
4259      For unsigned fields, the constant shifted right by the field length should
4260      be all zero.  For signed fields, the high-order bits should agree with
4261      the sign bit.  */
4262
4263   if (lunsignedp)
4264     {
4265       if (! integer_zerop (const_binop (RSHIFT_EXPR,
4266                                         fold_convert_loc (loc,
4267                                                           unsigned_type, rhs),
4268                                         size_int (lbitsize), 0)))
4269         {
4270           warning (0, "comparison is always %d due to width of bit-field",
4271                    code == NE_EXPR);
4272           return constant_boolean_node (code == NE_EXPR, compare_type);
4273         }
4274     }
4275   else
4276     {
4277       tree tem = const_binop (RSHIFT_EXPR,
4278                               fold_convert_loc (loc, signed_type, rhs),
4279                               size_int (lbitsize - 1), 0);
4280       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
4281         {
4282           warning (0, "comparison is always %d due to width of bit-field",
4283                    code == NE_EXPR);
4284           return constant_boolean_node (code == NE_EXPR, compare_type);
4285         }
4286     }
4287
4288   /* Single-bit compares should always be against zero.  */
4289   if (lbitsize == 1 && ! integer_zerop (rhs))
4290     {
4291       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4292       rhs = build_int_cst (type, 0);
4293     }
4294
4295   /* Make a new bitfield reference, shift the constant over the
4296      appropriate number of bits and mask it with the computed mask
4297      (in case this was a signed field).  If we changed it, make a new one.  */
4298   lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
4299   if (lvolatilep)
4300     {
4301       TREE_SIDE_EFFECTS (lhs) = 1;
4302       TREE_THIS_VOLATILE (lhs) = 1;
4303     }
4304
4305   rhs = const_binop (BIT_AND_EXPR,
4306                      const_binop (LSHIFT_EXPR,
4307                                   fold_convert_loc (loc, unsigned_type, rhs),
4308                                   size_int (lbitpos), 0),
4309                      mask, 0);
4310
4311   lhs = build2 (code, compare_type,
4312                 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4313                 rhs);
4314   SET_EXPR_LOCATION (lhs, loc);
4315   return lhs;
4316 }
4317 \f
4318 /* Subroutine for fold_truthop: decode a field reference.
4319
4320    If EXP is a comparison reference, we return the innermost reference.
4321
4322    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4323    set to the starting bit number.
4324
4325    If the innermost field can be completely contained in a mode-sized
4326    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
4327
4328    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4329    otherwise it is not changed.
4330
4331    *PUNSIGNEDP is set to the signedness of the field.
4332
4333    *PMASK is set to the mask used.  This is either contained in a
4334    BIT_AND_EXPR or derived from the width of the field.
4335
4336    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4337
4338    Return 0 if this is not a component reference or is one that we can't
4339    do anything with.  */
4340
4341 static tree
4342 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
4343                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4344                         int *punsignedp, int *pvolatilep,
4345                         tree *pmask, tree *pand_mask)
4346 {
4347   tree outer_type = 0;
4348   tree and_mask = 0;
4349   tree mask, inner, offset;
4350   tree unsigned_type;
4351   unsigned int precision;
4352
4353   /* All the optimizations using this function assume integer fields.
4354      There are problems with FP fields since the type_for_size call
4355      below can fail for, e.g., XFmode.  */
4356   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4357     return 0;
4358
4359   /* We are interested in the bare arrangement of bits, so strip everything
4360      that doesn't affect the machine mode.  However, record the type of the
4361      outermost expression if it may matter below.  */
4362   if (CONVERT_EXPR_P (exp)
4363       || TREE_CODE (exp) == NON_LVALUE_EXPR)
4364     outer_type = TREE_TYPE (exp);
4365   STRIP_NOPS (exp);
4366
4367   if (TREE_CODE (exp) == BIT_AND_EXPR)
4368     {
4369       and_mask = TREE_OPERAND (exp, 1);
4370       exp = TREE_OPERAND (exp, 0);
4371       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4372       if (TREE_CODE (and_mask) != INTEGER_CST)
4373         return 0;
4374     }
4375
4376   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4377                                punsignedp, pvolatilep, false);
4378   if ((inner == exp && and_mask == 0)
4379       || *pbitsize < 0 || offset != 0
4380       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4381     return 0;
4382
4383   /* If the number of bits in the reference is the same as the bitsize of
4384      the outer type, then the outer type gives the signedness. Otherwise
4385      (in case of a small bitfield) the signedness is unchanged.  */
4386   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4387     *punsignedp = TYPE_UNSIGNED (outer_type);
4388
4389   /* Compute the mask to access the bitfield.  */
4390   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4391   precision = TYPE_PRECISION (unsigned_type);
4392
4393   mask = build_int_cst_type (unsigned_type, -1);
4394
4395   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4396   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4397
4398   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
4399   if (and_mask != 0)
4400     mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4401                         fold_convert_loc (loc, unsigned_type, and_mask), mask);
4402
4403   *pmask = mask;
4404   *pand_mask = and_mask;
4405   return inner;
4406 }
4407
4408 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4409    bit positions.  */
4410
4411 static int
4412 all_ones_mask_p (const_tree mask, int size)
4413 {
4414   tree type = TREE_TYPE (mask);
4415   unsigned int precision = TYPE_PRECISION (type);
4416   tree tmask;
4417
4418   tmask = build_int_cst_type (signed_type_for (type), -1);
4419
4420   return
4421     tree_int_cst_equal (mask,
4422                         const_binop (RSHIFT_EXPR,
4423                                      const_binop (LSHIFT_EXPR, tmask,
4424                                                   size_int (precision - size),
4425                                                   0),
4426                                      size_int (precision - size), 0));
4427 }
4428
4429 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4430    represents the sign bit of EXP's type.  If EXP represents a sign
4431    or zero extension, also test VAL against the unextended type.
4432    The return value is the (sub)expression whose sign bit is VAL,
4433    or NULL_TREE otherwise.  */
4434
4435 static tree
4436 sign_bit_p (tree exp, const_tree val)
4437 {
4438   unsigned HOST_WIDE_INT mask_lo, lo;
4439   HOST_WIDE_INT mask_hi, hi;
4440   int width;
4441   tree t;
4442
4443   /* Tree EXP must have an integral type.  */
4444   t = TREE_TYPE (exp);
4445   if (! INTEGRAL_TYPE_P (t))
4446     return NULL_TREE;
4447
4448   /* Tree VAL must be an integer constant.  */
4449   if (TREE_CODE (val) != INTEGER_CST
4450       || TREE_OVERFLOW (val))
4451     return NULL_TREE;
4452
4453   width = TYPE_PRECISION (t);
4454   if (width > HOST_BITS_PER_WIDE_INT)
4455     {
4456       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4457       lo = 0;
4458
4459       mask_hi = ((unsigned HOST_WIDE_INT) -1
4460                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
4461       mask_lo = -1;
4462     }
4463   else
4464     {
4465       hi = 0;
4466       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4467
4468       mask_hi = 0;
4469       mask_lo = ((unsigned HOST_WIDE_INT) -1
4470                  >> (HOST_BITS_PER_WIDE_INT - width));
4471     }
4472
4473   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4474      treat VAL as if it were unsigned.  */
4475   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4476       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4477     return exp;
4478
4479   /* Handle extension from a narrower type.  */
4480   if (TREE_CODE (exp) == NOP_EXPR
4481       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4482     return sign_bit_p (TREE_OPERAND (exp, 0), val);
4483
4484   return NULL_TREE;
4485 }
4486
4487 /* Subroutine for fold_truthop: determine if an operand is simple enough
4488    to be evaluated unconditionally.  */
4489
4490 static int
4491 simple_operand_p (const_tree exp)
4492 {
4493   /* Strip any conversions that don't change the machine mode.  */
4494   STRIP_NOPS (exp);
4495
4496   return (CONSTANT_CLASS_P (exp)
4497           || TREE_CODE (exp) == SSA_NAME
4498           || (DECL_P (exp)
4499               && ! TREE_ADDRESSABLE (exp)
4500               && ! TREE_THIS_VOLATILE (exp)
4501               && ! DECL_NONLOCAL (exp)
4502               /* Don't regard global variables as simple.  They may be
4503                  allocated in ways unknown to the compiler (shared memory,
4504                  #pragma weak, etc).  */
4505               && ! TREE_PUBLIC (exp)
4506               && ! DECL_EXTERNAL (exp)
4507               /* Loading a static variable is unduly expensive, but global
4508                  registers aren't expensive.  */
4509               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4510 }
4511 \f
4512 /* The following functions are subroutines to fold_range_test and allow it to
4513    try to change a logical combination of comparisons into a range test.
4514
4515    For example, both
4516         X == 2 || X == 3 || X == 4 || X == 5
4517    and
4518         X >= 2 && X <= 5
4519    are converted to
4520         (unsigned) (X - 2) <= 3
4521
4522    We describe each set of comparisons as being either inside or outside
4523    a range, using a variable named like IN_P, and then describe the
4524    range with a lower and upper bound.  If one of the bounds is omitted,
4525    it represents either the highest or lowest value of the type.
4526
4527    In the comments below, we represent a range by two numbers in brackets
4528    preceded by a "+" to designate being inside that range, or a "-" to
4529    designate being outside that range, so the condition can be inverted by
4530    flipping the prefix.  An omitted bound is represented by a "-".  For
4531    example, "- [-, 10]" means being outside the range starting at the lowest
4532    possible value and ending at 10, in other words, being greater than 10.
4533    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4534    always false.
4535
4536    We set up things so that the missing bounds are handled in a consistent
4537    manner so neither a missing bound nor "true" and "false" need to be
4538    handled using a special case.  */
4539
4540 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4541    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4542    and UPPER1_P are nonzero if the respective argument is an upper bound
4543    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
4544    must be specified for a comparison.  ARG1 will be converted to ARG0's
4545    type if both are specified.  */
4546
4547 static tree
4548 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4549              tree arg1, int upper1_p)
4550 {
4551   tree tem;
4552   int result;
4553   int sgn0, sgn1;
4554
4555   /* If neither arg represents infinity, do the normal operation.
4556      Else, if not a comparison, return infinity.  Else handle the special
4557      comparison rules. Note that most of the cases below won't occur, but
4558      are handled for consistency.  */
4559
4560   if (arg0 != 0 && arg1 != 0)
4561     {
4562       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4563                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
4564       STRIP_NOPS (tem);
4565       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4566     }
4567
4568   if (TREE_CODE_CLASS (code) != tcc_comparison)
4569     return 0;
4570
4571   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4572      for neither.  In real maths, we cannot assume open ended ranges are
4573      the same. But, this is computer arithmetic, where numbers are finite.
4574      We can therefore make the transformation of any unbounded range with
4575      the value Z, Z being greater than any representable number. This permits
4576      us to treat unbounded ranges as equal.  */
4577   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4578   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4579   switch (code)
4580     {
4581     case EQ_EXPR:
4582       result = sgn0 == sgn1;
4583       break;
4584     case NE_EXPR:
4585       result = sgn0 != sgn1;
4586       break;
4587     case LT_EXPR:
4588       result = sgn0 < sgn1;
4589       break;
4590     case LE_EXPR:
4591       result = sgn0 <= sgn1;
4592       break;
4593     case GT_EXPR:
4594       result = sgn0 > sgn1;
4595       break;
4596     case GE_EXPR:
4597       result = sgn0 >= sgn1;
4598       break;
4599     default:
4600       gcc_unreachable ();
4601     }
4602
4603   return constant_boolean_node (result, type);
4604 }
4605 \f
4606 /* Given EXP, a logical expression, set the range it is testing into
4607    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4608    actually being tested.  *PLOW and *PHIGH will be made of the same
4609    type as the returned expression.  If EXP is not a comparison, we
4610    will most likely not be returning a useful value and range.  Set
4611    *STRICT_OVERFLOW_P to true if the return value is only valid
4612    because signed overflow is undefined; otherwise, do not change
4613    *STRICT_OVERFLOW_P.  */
4614
4615 tree
4616 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4617             bool *strict_overflow_p)
4618 {
4619   enum tree_code code;
4620   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4621   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4622   int in_p, n_in_p;
4623   tree low, high, n_low, n_high;
4624   location_t loc = EXPR_LOCATION (exp);
4625
4626   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4627      and see if we can refine the range.  Some of the cases below may not
4628      happen, but it doesn't seem worth worrying about this.  We "continue"
4629      the outer loop when we've changed something; otherwise we "break"
4630      the switch, which will "break" the while.  */
4631
4632   in_p = 0;
4633   low = high = build_int_cst (TREE_TYPE (exp), 0);
4634
4635   while (1)
4636     {
4637       code = TREE_CODE (exp);
4638       exp_type = TREE_TYPE (exp);
4639
4640       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4641         {
4642           if (TREE_OPERAND_LENGTH (exp) > 0)
4643             arg0 = TREE_OPERAND (exp, 0);
4644           if (TREE_CODE_CLASS (code) == tcc_comparison
4645               || TREE_CODE_CLASS (code) == tcc_unary
4646               || TREE_CODE_CLASS (code) == tcc_binary)
4647             arg0_type = TREE_TYPE (arg0);
4648           if (TREE_CODE_CLASS (code) == tcc_binary
4649               || TREE_CODE_CLASS (code) == tcc_comparison
4650               || (TREE_CODE_CLASS (code) == tcc_expression
4651                   && TREE_OPERAND_LENGTH (exp) > 1))
4652             arg1 = TREE_OPERAND (exp, 1);
4653         }
4654
4655       switch (code)
4656         {
4657         case TRUTH_NOT_EXPR:
4658           in_p = ! in_p, exp = arg0;
4659           continue;
4660
4661         case EQ_EXPR: case NE_EXPR:
4662         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4663           /* We can only do something if the range is testing for zero
4664              and if the second operand is an integer constant.  Note that
4665              saying something is "in" the range we make is done by
4666              complementing IN_P since it will set in the initial case of
4667              being not equal to zero; "out" is leaving it alone.  */
4668           if (low == 0 || high == 0
4669               || ! integer_zerop (low) || ! integer_zerop (high)
4670               || TREE_CODE (arg1) != INTEGER_CST)
4671             break;
4672
4673           switch (code)
4674             {
4675             case NE_EXPR:  /* - [c, c]  */
4676               low = high = arg1;
4677               break;
4678             case EQ_EXPR:  /* + [c, c]  */
4679               in_p = ! in_p, low = high = arg1;
4680               break;
4681             case GT_EXPR:  /* - [-, c] */
4682               low = 0, high = arg1;
4683               break;
4684             case GE_EXPR:  /* + [c, -] */
4685               in_p = ! in_p, low = arg1, high = 0;
4686               break;
4687             case LT_EXPR:  /* - [c, -] */
4688               low = arg1, high = 0;
4689               break;
4690             case LE_EXPR:  /* + [-, c] */
4691               in_p = ! in_p, low = 0, high = arg1;
4692               break;
4693             default:
4694               gcc_unreachable ();
4695             }
4696
4697           /* If this is an unsigned comparison, we also know that EXP is
4698              greater than or equal to zero.  We base the range tests we make
4699              on that fact, so we record it here so we can parse existing
4700              range tests.  We test arg0_type since often the return type
4701              of, e.g. EQ_EXPR, is boolean.  */
4702           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4703             {
4704               if (! merge_ranges (&n_in_p, &n_low, &n_high,
4705                                   in_p, low, high, 1,
4706                                   build_int_cst (arg0_type, 0),
4707                                   NULL_TREE))
4708                 break;
4709
4710               in_p = n_in_p, low = n_low, high = n_high;
4711
4712               /* If the high bound is missing, but we have a nonzero low
4713                  bound, reverse the range so it goes from zero to the low bound
4714                  minus 1.  */
4715               if (high == 0 && low && ! integer_zerop (low))
4716                 {
4717                   in_p = ! in_p;
4718                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4719                                       integer_one_node, 0);
4720                   low = build_int_cst (arg0_type, 0);
4721                 }
4722             }
4723
4724           exp = arg0;
4725           continue;
4726
4727         case NEGATE_EXPR:
4728           /* (-x) IN [a,b] -> x in [-b, -a]  */
4729           n_low = range_binop (MINUS_EXPR, exp_type,
4730                                build_int_cst (exp_type, 0),
4731                                0, high, 1);
4732           n_high = range_binop (MINUS_EXPR, exp_type,
4733                                 build_int_cst (exp_type, 0),
4734                                 0, low, 0);
4735           low = n_low, high = n_high;
4736           exp = arg0;
4737           continue;
4738
4739         case BIT_NOT_EXPR:
4740           /* ~ X -> -X - 1  */
4741           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4742                         build_int_cst (exp_type, 1));
4743           SET_EXPR_LOCATION (exp, loc);
4744           continue;
4745
4746         case PLUS_EXPR:  case MINUS_EXPR:
4747           if (TREE_CODE (arg1) != INTEGER_CST)
4748             break;
4749
4750           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4751              move a constant to the other side.  */
4752           if (!TYPE_UNSIGNED (arg0_type)
4753               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4754             break;
4755
4756           /* If EXP is signed, any overflow in the computation is undefined,
4757              so we don't worry about it so long as our computations on
4758              the bounds don't overflow.  For unsigned, overflow is defined
4759              and this is exactly the right thing.  */
4760           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4761                                arg0_type, low, 0, arg1, 0);
4762           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4763                                 arg0_type, high, 1, arg1, 0);
4764           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4765               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4766             break;
4767
4768           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4769             *strict_overflow_p = true;
4770
4771           /* Check for an unsigned range which has wrapped around the maximum
4772              value thus making n_high < n_low, and normalize it.  */
4773           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4774             {
4775               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4776                                  integer_one_node, 0);
4777               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4778                                   integer_one_node, 0);
4779
4780               /* If the range is of the form +/- [ x+1, x ], we won't
4781                  be able to normalize it.  But then, it represents the
4782                  whole range or the empty set, so make it
4783                  +/- [ -, - ].  */
4784               if (tree_int_cst_equal (n_low, low)
4785                   && tree_int_cst_equal (n_high, high))
4786                 low = high = 0;
4787               else
4788                 in_p = ! in_p;
4789             }
4790           else
4791             low = n_low, high = n_high;
4792
4793           exp = arg0;
4794           continue;
4795
4796         CASE_CONVERT: case NON_LVALUE_EXPR:
4797           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4798             break;
4799
4800           if (! INTEGRAL_TYPE_P (arg0_type)
4801               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4802               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4803             break;
4804
4805           n_low = low, n_high = high;
4806
4807           if (n_low != 0)
4808             n_low = fold_convert_loc (loc, arg0_type, n_low);
4809
4810           if (n_high != 0)
4811             n_high = fold_convert_loc (loc, arg0_type, n_high);
4812
4813
4814           /* If we're converting arg0 from an unsigned type, to exp,
4815              a signed type,  we will be doing the comparison as unsigned.
4816              The tests above have already verified that LOW and HIGH
4817              are both positive.
4818
4819              So we have to ensure that we will handle large unsigned
4820              values the same way that the current signed bounds treat
4821              negative values.  */
4822
4823           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4824             {
4825               tree high_positive;
4826               tree equiv_type;
4827               /* For fixed-point modes, we need to pass the saturating flag
4828                  as the 2nd parameter.  */
4829               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4830                 equiv_type = lang_hooks.types.type_for_mode
4831                              (TYPE_MODE (arg0_type),
4832                               TYPE_SATURATING (arg0_type));
4833               else
4834                 equiv_type = lang_hooks.types.type_for_mode
4835                              (TYPE_MODE (arg0_type), 1);
4836
4837               /* A range without an upper bound is, naturally, unbounded.
4838                  Since convert would have cropped a very large value, use
4839                  the max value for the destination type.  */
4840               high_positive
4841                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4842                 : TYPE_MAX_VALUE (arg0_type);
4843
4844               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4845                 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4846                                              fold_convert_loc (loc, arg0_type,
4847                                                                high_positive),
4848                                              build_int_cst (arg0_type, 1));
4849
4850               /* If the low bound is specified, "and" the range with the
4851                  range for which the original unsigned value will be
4852                  positive.  */
4853               if (low != 0)
4854                 {
4855                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4856                                       1, n_low, n_high, 1,
4857                                       fold_convert_loc (loc, arg0_type,
4858                                                         integer_zero_node),
4859                                       high_positive))
4860                     break;
4861
4862                   in_p = (n_in_p == in_p);
4863                 }
4864               else
4865                 {
4866                   /* Otherwise, "or" the range with the range of the input
4867                      that will be interpreted as negative.  */
4868                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4869                                       0, n_low, n_high, 1,
4870                                       fold_convert_loc (loc, arg0_type,
4871                                                         integer_zero_node),
4872                                       high_positive))
4873                     break;
4874
4875                   in_p = (in_p != n_in_p);
4876                 }
4877             }
4878
4879           exp = arg0;
4880           low = n_low, high = n_high;
4881           continue;
4882
4883         default:
4884           break;
4885         }
4886
4887       break;
4888     }
4889
4890   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4891   if (TREE_CODE (exp) == INTEGER_CST)
4892     {
4893       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4894                                                  exp, 0, low, 0))
4895                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4896                                                     exp, 1, high, 1)));
4897       low = high = 0;
4898       exp = 0;
4899     }
4900
4901   *pin_p = in_p, *plow = low, *phigh = high;
4902   return exp;
4903 }
4904 \f
4905 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4906    type, TYPE, return an expression to test if EXP is in (or out of, depending
4907    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4908
4909 tree
4910 build_range_check (location_t loc, tree type, tree exp, int in_p,
4911                    tree low, tree high)
4912 {
4913   tree etype = TREE_TYPE (exp), value;
4914
4915 #ifdef HAVE_canonicalize_funcptr_for_compare
4916   /* Disable this optimization for function pointer expressions
4917      on targets that require function pointer canonicalization.  */
4918   if (HAVE_canonicalize_funcptr_for_compare
4919       && TREE_CODE (etype) == POINTER_TYPE
4920       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4921     return NULL_TREE;
4922 #endif
4923
4924   if (! in_p)
4925     {
4926       value = build_range_check (loc, type, exp, 1, low, high);
4927       if (value != 0)
4928         return invert_truthvalue_loc (loc, value);
4929
4930       return 0;
4931     }
4932
4933   if (low == 0 && high == 0)
4934     return build_int_cst (type, 1);
4935
4936   if (low == 0)
4937     return fold_build2_loc (loc, LE_EXPR, type, exp,
4938                         fold_convert_loc (loc, etype, high));
4939
4940   if (high == 0)
4941     return fold_build2_loc (loc, GE_EXPR, type, exp,
4942                         fold_convert_loc (loc, etype, low));
4943
4944   if (operand_equal_p (low, high, 0))
4945     return fold_build2_loc (loc, EQ_EXPR, type, exp,
4946                         fold_convert_loc (loc, etype, low));
4947
4948   if (integer_zerop (low))
4949     {
4950       if (! TYPE_UNSIGNED (etype))
4951         {
4952           etype = unsigned_type_for (etype);
4953           high = fold_convert_loc (loc, etype, high);
4954           exp = fold_convert_loc (loc, etype, exp);
4955         }
4956       return build_range_check (loc, type, exp, 1, 0, high);
4957     }
4958
4959   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4960   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4961     {
4962       unsigned HOST_WIDE_INT lo;
4963       HOST_WIDE_INT hi;
4964       int prec;
4965
4966       prec = TYPE_PRECISION (etype);
4967       if (prec <= HOST_BITS_PER_WIDE_INT)
4968         {
4969           hi = 0;
4970           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4971         }
4972       else
4973         {
4974           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4975           lo = (unsigned HOST_WIDE_INT) -1;
4976         }
4977
4978       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4979         {
4980           if (TYPE_UNSIGNED (etype))
4981             {
4982               tree signed_etype = signed_type_for (etype);
4983               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4984                 etype
4985                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4986               else
4987                 etype = signed_etype;
4988               exp = fold_convert_loc (loc, etype, exp);
4989             }
4990           return fold_build2_loc (loc, GT_EXPR, type, exp,
4991                               build_int_cst (etype, 0));
4992         }
4993     }
4994
4995   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4996      This requires wrap-around arithmetics for the type of the expression.
4997      First make sure that arithmetics in this type is valid, then make sure
4998      that it wraps around.  */
4999   if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5000     etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
5001                                             TYPE_UNSIGNED (etype));
5002
5003   if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
5004     {
5005       tree utype, minv, maxv;
5006
5007       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5008          for the type in question, as we rely on this here.  */
5009       utype = unsigned_type_for (etype);
5010       maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
5011       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5012                           integer_one_node, 1);
5013       minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
5014
5015       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5016                                       minv, 1, maxv, 1)))
5017         etype = utype;
5018       else
5019         return 0;
5020     }
5021
5022   high = fold_convert_loc (loc, etype, high);
5023   low = fold_convert_loc (loc, etype, low);
5024   exp = fold_convert_loc (loc, etype, exp);
5025
5026   value = const_binop (MINUS_EXPR, high, low, 0);
5027
5028
5029   if (POINTER_TYPE_P (etype))
5030     {
5031       if (value != 0 && !TREE_OVERFLOW (value))
5032         {
5033           low = fold_convert_loc (loc, sizetype, low);
5034           low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low);
5035           return build_range_check (loc, type,
5036                                     fold_build2_loc (loc, POINTER_PLUS_EXPR,
5037                                                  etype, exp, low),
5038                                     1, build_int_cst (etype, 0), value);
5039         }
5040       return 0;
5041     }
5042
5043   if (value != 0 && !TREE_OVERFLOW (value))
5044     return build_range_check (loc, type,
5045                               fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5046                               1, build_int_cst (etype, 0), value);
5047
5048   return 0;
5049 }
5050 \f
5051 /* Return the predecessor of VAL in its type, handling the infinite case.  */
5052
5053 static tree
5054 range_predecessor (tree val)
5055 {
5056   tree type = TREE_TYPE (val);
5057
5058   if (INTEGRAL_TYPE_P (type)
5059       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5060     return 0;
5061   else
5062     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
5063 }
5064
5065 /* Return the successor of VAL in its type, handling the infinite case.  */
5066
5067 static tree
5068 range_successor (tree val)
5069 {
5070   tree type = TREE_TYPE (val);
5071
5072   if (INTEGRAL_TYPE_P (type)
5073       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5074     return 0;
5075   else
5076     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
5077 }
5078
5079 /* Given two ranges, see if we can merge them into one.  Return 1 if we
5080    can, 0 if we can't.  Set the output range into the specified parameters.  */
5081
5082 bool
5083 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5084               tree high0, int in1_p, tree low1, tree high1)
5085 {
5086   int no_overlap;
5087   int subset;
5088   int temp;
5089   tree tem;
5090   int in_p;
5091   tree low, high;
5092   int lowequal = ((low0 == 0 && low1 == 0)
5093                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5094                                                 low0, 0, low1, 0)));
5095   int highequal = ((high0 == 0 && high1 == 0)
5096                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5097                                                  high0, 1, high1, 1)));
5098
5099   /* Make range 0 be the range that starts first, or ends last if they
5100      start at the same value.  Swap them if it isn't.  */
5101   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5102                                  low0, 0, low1, 0))
5103       || (lowequal
5104           && integer_onep (range_binop (GT_EXPR, integer_type_node,
5105                                         high1, 1, high0, 1))))
5106     {
5107       temp = in0_p, in0_p = in1_p, in1_p = temp;
5108       tem = low0, low0 = low1, low1 = tem;
5109       tem = high0, high0 = high1, high1 = tem;
5110     }
5111
5112   /* Now flag two cases, whether the ranges are disjoint or whether the
5113      second range is totally subsumed in the first.  Note that the tests
5114      below are simplified by the ones above.  */
5115   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5116                                           high0, 1, low1, 0));
5117   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5118                                       high1, 1, high0, 1));
5119
5120   /* We now have four cases, depending on whether we are including or
5121      excluding the two ranges.  */
5122   if (in0_p && in1_p)
5123     {
5124       /* If they don't overlap, the result is false.  If the second range
5125          is a subset it is the result.  Otherwise, the range is from the start
5126          of the second to the end of the first.  */
5127       if (no_overlap)
5128         in_p = 0, low = high = 0;
5129       else if (subset)
5130         in_p = 1, low = low1, high = high1;
5131       else
5132         in_p = 1, low = low1, high = high0;
5133     }
5134
5135   else if (in0_p && ! in1_p)
5136     {
5137       /* If they don't overlap, the result is the first range.  If they are
5138          equal, the result is false.  If the second range is a subset of the
5139          first, and the ranges begin at the same place, we go from just after
5140          the end of the second range to the end of the first.  If the second
5141          range is not a subset of the first, or if it is a subset and both
5142          ranges end at the same place, the range starts at the start of the
5143          first range and ends just before the second range.
5144          Otherwise, we can't describe this as a single range.  */
5145       if (no_overlap)
5146         in_p = 1, low = low0, high = high0;
5147       else if (lowequal && highequal)
5148         in_p = 0, low = high = 0;
5149       else if (subset && lowequal)
5150         {
5151           low = range_successor (high1);
5152           high = high0;
5153           in_p = 1;
5154           if (low == 0)
5155             {
5156               /* We are in the weird situation where high0 > high1 but
5157                  high1 has no successor.  Punt.  */
5158               return 0;
5159             }
5160         }
5161       else if (! subset || highequal)
5162         {
5163           low = low0;
5164           high = range_predecessor (low1);
5165           in_p = 1;
5166           if (high == 0)
5167             {
5168               /* low0 < low1 but low1 has no predecessor.  Punt.  */
5169               return 0;
5170             }
5171         }
5172       else
5173         return 0;
5174     }
5175
5176   else if (! in0_p && in1_p)
5177     {
5178       /* If they don't overlap, the result is the second range.  If the second
5179          is a subset of the first, the result is false.  Otherwise,
5180          the range starts just after the first range and ends at the
5181          end of the second.  */
5182       if (no_overlap)
5183         in_p = 1, low = low1, high = high1;
5184       else if (subset || highequal)
5185         in_p = 0, low = high = 0;
5186       else
5187         {
5188           low = range_successor (high0);
5189           high = high1;
5190           in_p = 1;
5191           if (low == 0)
5192             {
5193               /* high1 > high0 but high0 has no successor.  Punt.  */
5194               return 0;
5195             }
5196         }
5197     }
5198
5199   else
5200     {
5201       /* The case where we are excluding both ranges.  Here the complex case
5202          is if they don't overlap.  In that case, the only time we have a
5203          range is if they are adjacent.  If the second is a subset of the
5204          first, the result is the first.  Otherwise, the range to exclude
5205          starts at the beginning of the first range and ends at the end of the
5206          second.  */
5207       if (no_overlap)
5208         {
5209           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5210                                          range_successor (high0),
5211                                          1, low1, 0)))
5212             in_p = 0, low = low0, high = high1;
5213           else
5214             {
5215               /* Canonicalize - [min, x] into - [-, x].  */
5216               if (low0 && TREE_CODE (low0) == INTEGER_CST)
5217                 switch (TREE_CODE (TREE_TYPE (low0)))
5218                   {
5219                   case ENUMERAL_TYPE:
5220                     if (TYPE_PRECISION (TREE_TYPE (low0))
5221                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
5222                       break;
5223                     /* FALLTHROUGH */
5224                   case INTEGER_TYPE:
5225                     if (tree_int_cst_equal (low0,
5226                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
5227                       low0 = 0;
5228                     break;
5229                   case POINTER_TYPE:
5230                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
5231                         && integer_zerop (low0))
5232                       low0 = 0;
5233                     break;
5234                   default:
5235                     break;
5236                   }
5237
5238               /* Canonicalize - [x, max] into - [x, -].  */
5239               if (high1 && TREE_CODE (high1) == INTEGER_CST)
5240                 switch (TREE_CODE (TREE_TYPE (high1)))
5241                   {
5242                   case ENUMERAL_TYPE:
5243                     if (TYPE_PRECISION (TREE_TYPE (high1))
5244                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5245                       break;
5246                     /* FALLTHROUGH */
5247                   case INTEGER_TYPE:
5248                     if (tree_int_cst_equal (high1,
5249                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
5250                       high1 = 0;
5251                     break;
5252                   case POINTER_TYPE:
5253                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
5254                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5255                                                        high1, 1,
5256                                                        integer_one_node, 1)))
5257                       high1 = 0;
5258                     break;
5259                   default:
5260                     break;
5261                   }
5262
5263               /* The ranges might be also adjacent between the maximum and
5264                  minimum values of the given type.  For
5265                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5266                  return + [x + 1, y - 1].  */
5267               if (low0 == 0 && high1 == 0)
5268                 {
5269                   low = range_successor (high0);
5270                   high = range_predecessor (low1);
5271                   if (low == 0 || high == 0)
5272                     return 0;
5273
5274                   in_p = 1;
5275                 }
5276               else
5277                 return 0;
5278             }
5279         }
5280       else if (subset)
5281         in_p = 0, low = low0, high = high0;
5282       else
5283         in_p = 0, low = low0, high = high1;
5284     }
5285
5286   *pin_p = in_p, *plow = low, *phigh = high;
5287   return 1;
5288 }
5289 \f
5290
5291 /* Subroutine of fold, looking inside expressions of the form
5292    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5293    of the COND_EXPR.  This function is being used also to optimize
5294    A op B ? C : A, by reversing the comparison first.
5295
5296    Return a folded expression whose code is not a COND_EXPR
5297    anymore, or NULL_TREE if no folding opportunity is found.  */
5298
5299 static tree
5300 fold_cond_expr_with_comparison (location_t loc, tree type,
5301                                 tree arg0, tree arg1, tree arg2)
5302 {
5303   enum tree_code comp_code = TREE_CODE (arg0);
5304   tree arg00 = TREE_OPERAND (arg0, 0);
5305   tree arg01 = TREE_OPERAND (arg0, 1);
5306   tree arg1_type = TREE_TYPE (arg1);
5307   tree tem;
5308
5309   STRIP_NOPS (arg1);
5310   STRIP_NOPS (arg2);
5311
5312   /* If we have A op 0 ? A : -A, consider applying the following
5313      transformations:
5314
5315      A == 0? A : -A    same as -A
5316      A != 0? A : -A    same as A
5317      A >= 0? A : -A    same as abs (A)
5318      A > 0?  A : -A    same as abs (A)
5319      A <= 0? A : -A    same as -abs (A)
5320      A < 0?  A : -A    same as -abs (A)
5321
5322      None of these transformations work for modes with signed
5323      zeros.  If A is +/-0, the first two transformations will
5324      change the sign of the result (from +0 to -0, or vice
5325      versa).  The last four will fix the sign of the result,
5326      even though the original expressions could be positive or
5327      negative, depending on the sign of A.
5328
5329      Note that all these transformations are correct if A is
5330      NaN, since the two alternatives (A and -A) are also NaNs.  */
5331   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5332       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5333           ? real_zerop (arg01)
5334           : integer_zerop (arg01))
5335       && ((TREE_CODE (arg2) == NEGATE_EXPR
5336            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5337              /* In the case that A is of the form X-Y, '-A' (arg2) may
5338                 have already been folded to Y-X, check for that. */
5339           || (TREE_CODE (arg1) == MINUS_EXPR
5340               && TREE_CODE (arg2) == MINUS_EXPR
5341               && operand_equal_p (TREE_OPERAND (arg1, 0),
5342                                   TREE_OPERAND (arg2, 1), 0)
5343               && operand_equal_p (TREE_OPERAND (arg1, 1),
5344                                   TREE_OPERAND (arg2, 0), 0))))
5345     switch (comp_code)
5346       {
5347       case EQ_EXPR:
5348       case UNEQ_EXPR:
5349         tem = fold_convert_loc (loc, arg1_type, arg1);
5350         return pedantic_non_lvalue_loc (loc,
5351                                     fold_convert_loc (loc, type,
5352                                                   negate_expr (tem)));
5353       case NE_EXPR:
5354       case LTGT_EXPR:
5355         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5356       case UNGE_EXPR:
5357       case UNGT_EXPR:
5358         if (flag_trapping_math)
5359           break;
5360         /* Fall through.  */
5361       case GE_EXPR:
5362       case GT_EXPR:
5363         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5364           arg1 = fold_convert_loc (loc, signed_type_for
5365                                (TREE_TYPE (arg1)), arg1);
5366         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5367         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5368       case UNLE_EXPR:
5369       case UNLT_EXPR:
5370         if (flag_trapping_math)
5371           break;
5372       case LE_EXPR:
5373       case LT_EXPR:
5374         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5375           arg1 = fold_convert_loc (loc, signed_type_for
5376                                (TREE_TYPE (arg1)), arg1);
5377         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5378         return negate_expr (fold_convert_loc (loc, type, tem));
5379       default:
5380         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5381         break;
5382       }
5383
5384   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
5385      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
5386      both transformations are correct when A is NaN: A != 0
5387      is then true, and A == 0 is false.  */
5388
5389   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5390       && integer_zerop (arg01) && integer_zerop (arg2))
5391     {
5392       if (comp_code == NE_EXPR)
5393         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5394       else if (comp_code == EQ_EXPR)
5395         return build_int_cst (type, 0);
5396     }
5397
5398   /* Try some transformations of A op B ? A : B.
5399
5400      A == B? A : B    same as B
5401      A != B? A : B    same as A
5402      A >= B? A : B    same as max (A, B)
5403      A > B?  A : B    same as max (B, A)
5404      A <= B? A : B    same as min (A, B)
5405      A < B?  A : B    same as min (B, A)
5406
5407      As above, these transformations don't work in the presence
5408      of signed zeros.  For example, if A and B are zeros of
5409      opposite sign, the first two transformations will change
5410      the sign of the result.  In the last four, the original
5411      expressions give different results for (A=+0, B=-0) and
5412      (A=-0, B=+0), but the transformed expressions do not.
5413
5414      The first two transformations are correct if either A or B
5415      is a NaN.  In the first transformation, the condition will
5416      be false, and B will indeed be chosen.  In the case of the
5417      second transformation, the condition A != B will be true,
5418      and A will be chosen.
5419
5420      The conversions to max() and min() are not correct if B is
5421      a number and A is not.  The conditions in the original
5422      expressions will be false, so all four give B.  The min()
5423      and max() versions would give a NaN instead.  */
5424   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5425       && operand_equal_for_comparison_p (arg01, arg2, arg00)
5426       /* Avoid these transformations if the COND_EXPR may be used
5427          as an lvalue in the C++ front-end.  PR c++/19199.  */
5428       && (in_gimple_form
5429           || (strcmp (lang_hooks.name, "GNU C++") != 0
5430               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5431           || ! maybe_lvalue_p (arg1)
5432           || ! maybe_lvalue_p (arg2)))
5433     {
5434       tree comp_op0 = arg00;
5435       tree comp_op1 = arg01;
5436       tree comp_type = TREE_TYPE (comp_op0);
5437
5438       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
5439       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5440         {
5441           comp_type = type;
5442           comp_op0 = arg1;
5443           comp_op1 = arg2;
5444         }
5445
5446       switch (comp_code)
5447         {
5448         case EQ_EXPR:
5449           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
5450         case NE_EXPR:
5451           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5452         case LE_EXPR:
5453         case LT_EXPR:
5454         case UNLE_EXPR:
5455         case UNLT_EXPR:
5456           /* In C++ a ?: expression can be an lvalue, so put the
5457              operand which will be used if they are equal first
5458              so that we can convert this back to the
5459              corresponding COND_EXPR.  */
5460           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5461             {
5462               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5463               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5464               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5465                     ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5466                     : fold_build2_loc (loc, MIN_EXPR, comp_type,
5467                                    comp_op1, comp_op0);
5468               return pedantic_non_lvalue_loc (loc,
5469                                           fold_convert_loc (loc, type, tem));
5470             }
5471           break;
5472         case GE_EXPR:
5473         case GT_EXPR:
5474         case UNGE_EXPR:
5475         case UNGT_EXPR:
5476           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5477             {
5478               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5479               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5480               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5481                     ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5482                     : fold_build2_loc (loc, MAX_EXPR, comp_type,
5483                                    comp_op1, comp_op0);
5484               return pedantic_non_lvalue_loc (loc,
5485                                           fold_convert_loc (loc, type, tem));
5486             }
5487           break;
5488         case UNEQ_EXPR:
5489           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5490             return pedantic_non_lvalue_loc (loc,
5491                                         fold_convert_loc (loc, type, arg2));
5492           break;
5493         case LTGT_EXPR:
5494           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5495             return pedantic_non_lvalue_loc (loc,
5496                                         fold_convert_loc (loc, type, arg1));
5497           break;
5498         default:
5499           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5500           break;
5501         }
5502     }
5503
5504   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5505      we might still be able to simplify this.  For example,
5506      if C1 is one less or one more than C2, this might have started
5507      out as a MIN or MAX and been transformed by this function.
5508      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
5509
5510   if (INTEGRAL_TYPE_P (type)
5511       && TREE_CODE (arg01) == INTEGER_CST
5512       && TREE_CODE (arg2) == INTEGER_CST)
5513     switch (comp_code)
5514       {
5515       case EQ_EXPR:
5516         if (TREE_CODE (arg1) == INTEGER_CST)
5517           break;
5518         /* We can replace A with C1 in this case.  */
5519         arg1 = fold_convert_loc (loc, type, arg01);
5520         return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5521
5522       case LT_EXPR:
5523         /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5524            MIN_EXPR, to preserve the signedness of the comparison.  */
5525         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5526                                OEP_ONLY_CONST)
5527             && operand_equal_p (arg01,
5528                                 const_binop (PLUS_EXPR, arg2,
5529                                              build_int_cst (type, 1), 0),
5530                                 OEP_ONLY_CONST))
5531           {
5532             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5533                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5534                                                      arg2));
5535             return pedantic_non_lvalue_loc (loc,
5536                                             fold_convert_loc (loc, type, tem));
5537           }
5538         break;
5539
5540       case LE_EXPR:
5541         /* If C1 is C2 - 1, this is min(A, C2), with the same care
5542            as above.  */
5543         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5544                                OEP_ONLY_CONST)
5545             && operand_equal_p (arg01,
5546                                 const_binop (MINUS_EXPR, arg2,
5547                                              build_int_cst (type, 1), 0),
5548                                 OEP_ONLY_CONST))
5549           {
5550             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5551                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5552                                                      arg2));
5553             return pedantic_non_lvalue_loc (loc,
5554                                             fold_convert_loc (loc, type, tem));
5555           }
5556         break;
5557
5558       case GT_EXPR:
5559         /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5560            MAX_EXPR, to preserve the signedness of the comparison.  */
5561         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5562                                OEP_ONLY_CONST)
5563             && operand_equal_p (arg01,
5564                                 const_binop (MINUS_EXPR, arg2,
5565                                              build_int_cst (type, 1), 0),
5566                                 OEP_ONLY_CONST))
5567           {
5568             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5569                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5570                                                      arg2));
5571             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5572           }
5573         break;
5574
5575       case GE_EXPR:
5576         /* If C1 is C2 + 1, this is max(A, C2), with the same care as above.  */
5577         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5578                                OEP_ONLY_CONST)
5579             && operand_equal_p (arg01,
5580                                 const_binop (PLUS_EXPR, arg2,
5581                                              build_int_cst (type, 1), 0),
5582                                 OEP_ONLY_CONST))
5583           {
5584             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5585                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5586                                                      arg2));
5587             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5588           }
5589         break;
5590       case NE_EXPR:
5591         break;
5592       default:
5593         gcc_unreachable ();
5594       }
5595
5596   return NULL_TREE;
5597 }
5598
5599
5600 \f
5601 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5602 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5603   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5604                 false) >= 2)
5605 #endif
5606
5607 /* EXP is some logical combination of boolean tests.  See if we can
5608    merge it into some range test.  Return the new tree if so.  */
5609
5610 static tree
5611 fold_range_test (location_t loc, enum tree_code code, tree type,
5612                  tree op0, tree op1)
5613 {
5614   int or_op = (code == TRUTH_ORIF_EXPR
5615                || code == TRUTH_OR_EXPR);
5616   int in0_p, in1_p, in_p;
5617   tree low0, low1, low, high0, high1, high;
5618   bool strict_overflow_p = false;
5619   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5620   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5621   tree tem;
5622   const char * const warnmsg = G_("assuming signed overflow does not occur "
5623                                   "when simplifying range test");
5624
5625   /* If this is an OR operation, invert both sides; we will invert
5626      again at the end.  */
5627   if (or_op)
5628     in0_p = ! in0_p, in1_p = ! in1_p;
5629
5630   /* If both expressions are the same, if we can merge the ranges, and we
5631      can build the range test, return it or it inverted.  If one of the
5632      ranges is always true or always false, consider it to be the same
5633      expression as the other.  */
5634   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5635       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5636                        in1_p, low1, high1)
5637       && 0 != (tem = (build_range_check (UNKNOWN_LOCATION, type,
5638                                          lhs != 0 ? lhs
5639                                          : rhs != 0 ? rhs : integer_zero_node,
5640                                          in_p, low, high))))
5641     {
5642       if (strict_overflow_p)
5643         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5644       return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5645     }
5646
5647   /* On machines where the branch cost is expensive, if this is a
5648      short-circuited branch and the underlying object on both sides
5649      is the same, make a non-short-circuit operation.  */
5650   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5651            && lhs != 0 && rhs != 0
5652            && (code == TRUTH_ANDIF_EXPR
5653                || code == TRUTH_ORIF_EXPR)
5654            && operand_equal_p (lhs, rhs, 0))
5655     {
5656       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
5657          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5658          which cases we can't do this.  */
5659       if (simple_operand_p (lhs))
5660         {
5661           tem = build2 (code == TRUTH_ANDIF_EXPR
5662                         ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5663                         type, op0, op1);
5664           SET_EXPR_LOCATION (tem, loc);
5665           return tem;
5666         }
5667
5668       else if (lang_hooks.decls.global_bindings_p () == 0
5669                && ! CONTAINS_PLACEHOLDER_P (lhs))
5670         {
5671           tree common = save_expr (lhs);
5672
5673           if (0 != (lhs = build_range_check (loc, type, common,
5674                                              or_op ? ! in0_p : in0_p,
5675                                              low0, high0))
5676               && (0 != (rhs = build_range_check (loc, type, common,
5677                                                  or_op ? ! in1_p : in1_p,
5678                                                  low1, high1))))
5679             {
5680               if (strict_overflow_p)
5681                 fold_overflow_warning (warnmsg,
5682                                        WARN_STRICT_OVERFLOW_COMPARISON);
5683               tem = build2 (code == TRUTH_ANDIF_EXPR
5684                             ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5685                             type, lhs, rhs);
5686               SET_EXPR_LOCATION (tem, loc);
5687               return tem;
5688             }
5689         }
5690     }
5691
5692   return 0;
5693 }
5694 \f
5695 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5696    bit value.  Arrange things so the extra bits will be set to zero if and
5697    only if C is signed-extended to its full width.  If MASK is nonzero,
5698    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
5699
5700 static tree
5701 unextend (tree c, int p, int unsignedp, tree mask)
5702 {
5703   tree type = TREE_TYPE (c);
5704   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5705   tree temp;
5706
5707   if (p == modesize || unsignedp)
5708     return c;
5709
5710   /* We work by getting just the sign bit into the low-order bit, then
5711      into the high-order bit, then sign-extend.  We then XOR that value
5712      with C.  */
5713   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5714   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5715
5716   /* We must use a signed type in order to get an arithmetic right shift.
5717      However, we must also avoid introducing accidental overflows, so that
5718      a subsequent call to integer_zerop will work.  Hence we must
5719      do the type conversion here.  At this point, the constant is either
5720      zero or one, and the conversion to a signed type can never overflow.
5721      We could get an overflow if this conversion is done anywhere else.  */
5722   if (TYPE_UNSIGNED (type))
5723     temp = fold_convert (signed_type_for (type), temp);
5724
5725   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5726   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5727   if (mask != 0)
5728     temp = const_binop (BIT_AND_EXPR, temp,
5729                         fold_convert (TREE_TYPE (c), mask),
5730                         0);
5731   /* If necessary, convert the type back to match the type of C.  */
5732   if (TYPE_UNSIGNED (type))
5733     temp = fold_convert (type, temp);
5734
5735   return fold_convert (type,
5736                        const_binop (BIT_XOR_EXPR, c, temp, 0));
5737 }
5738 \f
5739 /* Find ways of folding logical expressions of LHS and RHS:
5740    Try to merge two comparisons to the same innermost item.
5741    Look for range tests like "ch >= '0' && ch <= '9'".
5742    Look for combinations of simple terms on machines with expensive branches
5743    and evaluate the RHS unconditionally.
5744
5745    For example, if we have p->a == 2 && p->b == 4 and we can make an
5746    object large enough to span both A and B, we can do this with a comparison
5747    against the object ANDed with the a mask.
5748
5749    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5750    operations to do this with one comparison.
5751
5752    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5753    function and the one above.
5754
5755    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5756    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5757
5758    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5759    two operands.
5760
5761    We return the simplified tree or 0 if no optimization is possible.  */
5762
5763 static tree
5764 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5765               tree lhs, tree rhs)
5766 {
5767   /* If this is the "or" of two comparisons, we can do something if
5768      the comparisons are NE_EXPR.  If this is the "and", we can do something
5769      if the comparisons are EQ_EXPR.  I.e.,
5770         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5771
5772      WANTED_CODE is this operation code.  For single bit fields, we can
5773      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5774      comparison for one-bit fields.  */
5775
5776   enum tree_code wanted_code;
5777   enum tree_code lcode, rcode;
5778   tree ll_arg, lr_arg, rl_arg, rr_arg;
5779   tree ll_inner, lr_inner, rl_inner, rr_inner;
5780   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5781   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5782   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5783   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5784   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5785   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5786   enum machine_mode lnmode, rnmode;
5787   tree ll_mask, lr_mask, rl_mask, rr_mask;
5788   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5789   tree l_const, r_const;
5790   tree lntype, rntype, result;
5791   HOST_WIDE_INT first_bit, end_bit;
5792   int volatilep;
5793   tree orig_lhs = lhs, orig_rhs = rhs;
5794   enum tree_code orig_code = code;
5795
5796   /* Start by getting the comparison codes.  Fail if anything is volatile.
5797      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5798      it were surrounded with a NE_EXPR.  */
5799
5800   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5801     return 0;
5802
5803   lcode = TREE_CODE (lhs);
5804   rcode = TREE_CODE (rhs);
5805
5806   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5807     {
5808       lhs = build2 (NE_EXPR, truth_type, lhs,
5809                     build_int_cst (TREE_TYPE (lhs), 0));
5810       lcode = NE_EXPR;
5811     }
5812
5813   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5814     {
5815       rhs = build2 (NE_EXPR, truth_type, rhs,
5816                     build_int_cst (TREE_TYPE (rhs), 0));
5817       rcode = NE_EXPR;
5818     }
5819
5820   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5821       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5822     return 0;
5823
5824   ll_arg = TREE_OPERAND (lhs, 0);
5825   lr_arg = TREE_OPERAND (lhs, 1);
5826   rl_arg = TREE_OPERAND (rhs, 0);
5827   rr_arg = TREE_OPERAND (rhs, 1);
5828
5829   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5830   if (simple_operand_p (ll_arg)
5831       && simple_operand_p (lr_arg))
5832     {
5833       tree result;
5834       if (operand_equal_p (ll_arg, rl_arg, 0)
5835           && operand_equal_p (lr_arg, rr_arg, 0))
5836         {
5837           result = combine_comparisons (loc, code, lcode, rcode,
5838                                         truth_type, ll_arg, lr_arg);
5839           if (result)
5840             return result;
5841         }
5842       else if (operand_equal_p (ll_arg, rr_arg, 0)
5843                && operand_equal_p (lr_arg, rl_arg, 0))
5844         {
5845           result = combine_comparisons (loc, code, lcode,
5846                                         swap_tree_comparison (rcode),
5847                                         truth_type, ll_arg, lr_arg);
5848           if (result)
5849             return result;
5850         }
5851     }
5852
5853   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5854           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5855
5856   /* If the RHS can be evaluated unconditionally and its operands are
5857      simple, it wins to evaluate the RHS unconditionally on machines
5858      with expensive branches.  In this case, this isn't a comparison
5859      that can be merged.  Avoid doing this if the RHS is a floating-point
5860      comparison since those can trap.  */
5861
5862   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5863                    false) >= 2
5864       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5865       && simple_operand_p (rl_arg)
5866       && simple_operand_p (rr_arg))
5867     {
5868       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5869       if (code == TRUTH_OR_EXPR
5870           && lcode == NE_EXPR && integer_zerop (lr_arg)
5871           && rcode == NE_EXPR && integer_zerop (rr_arg)
5872           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5873           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5874         {
5875           result = build2 (NE_EXPR, truth_type,
5876                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5877                                    ll_arg, rl_arg),
5878                            build_int_cst (TREE_TYPE (ll_arg), 0));
5879           goto fold_truthop_exit;
5880         }
5881
5882       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5883       if (code == TRUTH_AND_EXPR
5884           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5885           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5886           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5887           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5888         {
5889           result = build2 (EQ_EXPR, truth_type,
5890                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5891                                    ll_arg, rl_arg),
5892                            build_int_cst (TREE_TYPE (ll_arg), 0));
5893           goto fold_truthop_exit;
5894         }
5895
5896       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5897         {
5898           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5899             {
5900               result = build2 (code, truth_type, lhs, rhs);
5901               goto fold_truthop_exit;
5902             }
5903           return NULL_TREE;
5904         }
5905     }
5906
5907   /* See if the comparisons can be merged.  Then get all the parameters for
5908      each side.  */
5909
5910   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5911       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5912     return 0;
5913
5914   volatilep = 0;
5915   ll_inner = decode_field_reference (loc, ll_arg,
5916                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5917                                      &ll_unsignedp, &volatilep, &ll_mask,
5918                                      &ll_and_mask);
5919   lr_inner = decode_field_reference (loc, lr_arg,
5920                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5921                                      &lr_unsignedp, &volatilep, &lr_mask,
5922                                      &lr_and_mask);
5923   rl_inner = decode_field_reference (loc, rl_arg,
5924                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5925                                      &rl_unsignedp, &volatilep, &rl_mask,
5926                                      &rl_and_mask);
5927   rr_inner = decode_field_reference (loc, rr_arg,
5928                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5929                                      &rr_unsignedp, &volatilep, &rr_mask,
5930                                      &rr_and_mask);
5931
5932   /* It must be true that the inner operation on the lhs of each
5933      comparison must be the same if we are to be able to do anything.
5934      Then see if we have constants.  If not, the same must be true for
5935      the rhs's.  */
5936   if (volatilep || ll_inner == 0 || rl_inner == 0
5937       || ! operand_equal_p (ll_inner, rl_inner, 0))
5938     return 0;
5939
5940   if (TREE_CODE (lr_arg) == INTEGER_CST
5941       && TREE_CODE (rr_arg) == INTEGER_CST)
5942     l_const = lr_arg, r_const = rr_arg;
5943   else if (lr_inner == 0 || rr_inner == 0
5944            || ! operand_equal_p (lr_inner, rr_inner, 0))
5945     return 0;
5946   else
5947     l_const = r_const = 0;
5948
5949   /* If either comparison code is not correct for our logical operation,
5950      fail.  However, we can convert a one-bit comparison against zero into
5951      the opposite comparison against that bit being set in the field.  */
5952
5953   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5954   if (lcode != wanted_code)
5955     {
5956       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5957         {
5958           /* Make the left operand unsigned, since we are only interested
5959              in the value of one bit.  Otherwise we are doing the wrong
5960              thing below.  */
5961           ll_unsignedp = 1;
5962           l_const = ll_mask;
5963         }
5964       else
5965         return 0;
5966     }
5967
5968   /* This is analogous to the code for l_const above.  */
5969   if (rcode != wanted_code)
5970     {
5971       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5972         {
5973           rl_unsignedp = 1;
5974           r_const = rl_mask;
5975         }
5976       else
5977         return 0;
5978     }
5979
5980   /* See if we can find a mode that contains both fields being compared on
5981      the left.  If we can't, fail.  Otherwise, update all constants and masks
5982      to be relative to a field of that size.  */
5983   first_bit = MIN (ll_bitpos, rl_bitpos);
5984   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5985   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5986                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5987                           volatilep);
5988   if (lnmode == VOIDmode)
5989     return 0;
5990
5991   lnbitsize = GET_MODE_BITSIZE (lnmode);
5992   lnbitpos = first_bit & ~ (lnbitsize - 1);
5993   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5994   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5995
5996   if (BYTES_BIG_ENDIAN)
5997     {
5998       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5999       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
6000     }
6001
6002   ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
6003                          size_int (xll_bitpos), 0);
6004   rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
6005                          size_int (xrl_bitpos), 0);
6006
6007   if (l_const)
6008     {
6009       l_const = fold_convert_loc (loc, lntype, l_const);
6010       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
6011       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
6012       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
6013                                         fold_build1_loc (loc, BIT_NOT_EXPR,
6014                                                      lntype, ll_mask),
6015                                         0)))
6016         {
6017           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6018
6019           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6020         }
6021     }
6022   if (r_const)
6023     {
6024       r_const = fold_convert_loc (loc, lntype, r_const);
6025       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
6026       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
6027       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
6028                                         fold_build1_loc (loc, BIT_NOT_EXPR,
6029                                                      lntype, rl_mask),
6030                                         0)))
6031         {
6032           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6033
6034           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6035         }
6036     }
6037
6038   /* If the right sides are not constant, do the same for it.  Also,
6039      disallow this optimization if a size or signedness mismatch occurs
6040      between the left and right sides.  */
6041   if (l_const == 0)
6042     {
6043       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
6044           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
6045           /* Make sure the two fields on the right
6046              correspond to the left without being swapped.  */
6047           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
6048         return 0;
6049
6050       first_bit = MIN (lr_bitpos, rr_bitpos);
6051       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
6052       rnmode = get_best_mode (end_bit - first_bit, first_bit,
6053                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
6054                               volatilep);
6055       if (rnmode == VOIDmode)
6056         return 0;
6057
6058       rnbitsize = GET_MODE_BITSIZE (rnmode);
6059       rnbitpos = first_bit & ~ (rnbitsize - 1);
6060       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
6061       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
6062
6063       if (BYTES_BIG_ENDIAN)
6064         {
6065           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
6066           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
6067         }
6068
6069       lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6070                                                             rntype, lr_mask),
6071                              size_int (xlr_bitpos), 0);
6072       rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6073                                                             rntype, rr_mask),
6074                              size_int (xrr_bitpos), 0);
6075
6076       /* Make a mask that corresponds to both fields being compared.
6077          Do this for both items being compared.  If the operands are the
6078          same size and the bits being compared are in the same position
6079          then we can do this by masking both and comparing the masked
6080          results.  */
6081       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
6082       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
6083       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
6084         {
6085           lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
6086                                     ll_unsignedp || rl_unsignedp);
6087           if (! all_ones_mask_p (ll_mask, lnbitsize))
6088             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6089
6090           rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
6091                                     lr_unsignedp || rr_unsignedp);
6092           if (! all_ones_mask_p (lr_mask, rnbitsize))
6093             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6094
6095           result = build2 (wanted_code, truth_type, lhs, rhs);
6096           goto fold_truthop_exit;
6097         }
6098
6099       /* There is still another way we can do something:  If both pairs of
6100          fields being compared are adjacent, we may be able to make a wider
6101          field containing them both.
6102
6103          Note that we still must mask the lhs/rhs expressions.  Furthermore,
6104          the mask must be shifted to account for the shift done by
6105          make_bit_field_ref.  */
6106       if ((ll_bitsize + ll_bitpos == rl_bitpos
6107            && lr_bitsize + lr_bitpos == rr_bitpos)
6108           || (ll_bitpos == rl_bitpos + rl_bitsize
6109               && lr_bitpos == rr_bitpos + rr_bitsize))
6110         {
6111           tree type;
6112
6113           lhs = make_bit_field_ref (loc, ll_inner, lntype,
6114                                     ll_bitsize + rl_bitsize,
6115                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
6116           rhs = make_bit_field_ref (loc, lr_inner, rntype,
6117                                     lr_bitsize + rr_bitsize,
6118                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
6119
6120           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6121                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
6122           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6123                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
6124
6125           /* Convert to the smaller type before masking out unwanted bits.  */
6126           type = lntype;
6127           if (lntype != rntype)
6128             {
6129               if (lnbitsize > rnbitsize)
6130                 {
6131                   lhs = fold_convert_loc (loc, rntype, lhs);
6132                   ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6133                   type = rntype;
6134                 }
6135               else if (lnbitsize < rnbitsize)
6136                 {
6137                   rhs = fold_convert_loc (loc, lntype, rhs);
6138                   lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6139                   type = lntype;
6140                 }
6141             }
6142
6143           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6144             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6145
6146           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6147             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6148
6149           result = build2 (wanted_code, truth_type, lhs, rhs);
6150           goto fold_truthop_exit;
6151         }
6152
6153       return 0;
6154     }
6155
6156   /* Handle the case of comparisons with constants.  If there is something in
6157      common between the masks, those bits of the constants must be the same.
6158      If not, the condition is always false.  Test for this to avoid generating
6159      incorrect code below.  */
6160   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
6161   if (! integer_zerop (result)
6162       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
6163                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
6164     {
6165       if (wanted_code == NE_EXPR)
6166         {
6167           warning (0, "%<or%> of unmatched not-equal tests is always 1");
6168           return constant_boolean_node (true, truth_type);
6169         }
6170       else
6171         {
6172           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6173           return constant_boolean_node (false, truth_type);
6174         }
6175     }
6176
6177   /* Construct the expression we will return.  First get the component
6178      reference we will make.  Unless the mask is all ones the width of
6179      that field, perform the mask operation.  Then compare with the
6180      merged constant.  */
6181   result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
6182                                ll_unsignedp || rl_unsignedp);
6183
6184   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
6185   if (! all_ones_mask_p (ll_mask, lnbitsize))
6186     {
6187       result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
6188       SET_EXPR_LOCATION (result, loc);
6189     }
6190
6191   result = build2 (wanted_code, truth_type, result,
6192                    const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
6193
6194  fold_truthop_exit:
6195   SET_EXPR_LOCATION (result, loc);
6196   return result;
6197 }
6198 \f
6199 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
6200    constant.  */
6201
6202 static tree
6203 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
6204                             tree op0, tree op1)
6205 {
6206   tree arg0 = op0;
6207   enum tree_code op_code;
6208   tree comp_const;
6209   tree minmax_const;
6210   int consts_equal, consts_lt;
6211   tree inner;
6212
6213   STRIP_SIGN_NOPS (arg0);
6214
6215   op_code = TREE_CODE (arg0);
6216   minmax_const = TREE_OPERAND (arg0, 1);
6217   comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
6218   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
6219   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
6220   inner = TREE_OPERAND (arg0, 0);
6221
6222   /* If something does not permit us to optimize, return the original tree.  */
6223   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
6224       || TREE_CODE (comp_const) != INTEGER_CST
6225       || TREE_OVERFLOW (comp_const)
6226       || TREE_CODE (minmax_const) != INTEGER_CST
6227       || TREE_OVERFLOW (minmax_const))
6228     return NULL_TREE;
6229
6230   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
6231      and GT_EXPR, doing the rest with recursive calls using logical
6232      simplifications.  */
6233   switch (code)
6234     {
6235     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
6236       {
6237         tree tem
6238           = optimize_minmax_comparison (loc,
6239                                         invert_tree_comparison (code, false),
6240                                         type, op0, op1);
6241         if (tem)
6242           return invert_truthvalue_loc (loc, tem);
6243         return NULL_TREE;
6244       }
6245
6246     case GE_EXPR:
6247       return
6248         fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
6249                      optimize_minmax_comparison
6250                      (loc, EQ_EXPR, type, arg0, comp_const),
6251                      optimize_minmax_comparison
6252                      (loc, GT_EXPR, type, arg0, comp_const));
6253
6254     case EQ_EXPR:
6255       if (op_code == MAX_EXPR && consts_equal)
6256         /* MAX (X, 0) == 0  ->  X <= 0  */
6257         return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
6258
6259       else if (op_code == MAX_EXPR && consts_lt)
6260         /* MAX (X, 0) == 5  ->  X == 5   */
6261         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
6262
6263       else if (op_code == MAX_EXPR)
6264         /* MAX (X, 0) == -1  ->  false  */
6265         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6266
6267       else if (consts_equal)
6268         /* MIN (X, 0) == 0  ->  X >= 0  */
6269         return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
6270
6271       else if (consts_lt)
6272         /* MIN (X, 0) == 5  ->  false  */
6273         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6274
6275       else
6276         /* MIN (X, 0) == -1  ->  X == -1  */
6277         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
6278
6279     case GT_EXPR:
6280       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
6281         /* MAX (X, 0) > 0  ->  X > 0
6282            MAX (X, 0) > 5  ->  X > 5  */
6283         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
6284
6285       else if (op_code == MAX_EXPR)
6286         /* MAX (X, 0) > -1  ->  true  */
6287         return omit_one_operand_loc (loc, type, integer_one_node, inner);
6288
6289       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
6290         /* MIN (X, 0) > 0  ->  false
6291            MIN (X, 0) > 5  ->  false  */
6292         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6293
6294       else
6295         /* MIN (X, 0) > -1  ->  X > -1  */
6296         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
6297
6298     default:
6299       return NULL_TREE;
6300     }
6301 }
6302 \f
6303 /* T is an integer expression that is being multiplied, divided, or taken a
6304    modulus (CODE says which and what kind of divide or modulus) by a
6305    constant C.  See if we can eliminate that operation by folding it with
6306    other operations already in T.  WIDE_TYPE, if non-null, is a type that
6307    should be used for the computation if wider than our type.
6308
6309    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6310    (X * 2) + (Y * 4).  We must, however, be assured that either the original
6311    expression would not overflow or that overflow is undefined for the type
6312    in the language in question.
6313
6314    If we return a non-null expression, it is an equivalent form of the
6315    original computation, but need not be in the original type.
6316
6317    We set *STRICT_OVERFLOW_P to true if the return values depends on
6318    signed overflow being undefined.  Otherwise we do not change
6319    *STRICT_OVERFLOW_P.  */
6320
6321 static tree
6322 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6323                 bool *strict_overflow_p)
6324 {
6325   /* To avoid exponential search depth, refuse to allow recursion past
6326      three levels.  Beyond that (1) it's highly unlikely that we'll find
6327      something interesting and (2) we've probably processed it before
6328      when we built the inner expression.  */
6329
6330   static int depth;
6331   tree ret;
6332
6333   if (depth > 3)
6334     return NULL;
6335
6336   depth++;
6337   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6338   depth--;
6339
6340   return ret;
6341 }
6342
6343 static tree
6344 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6345                   bool *strict_overflow_p)
6346 {
6347   tree type = TREE_TYPE (t);
6348   enum tree_code tcode = TREE_CODE (t);
6349   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6350                                    > GET_MODE_SIZE (TYPE_MODE (type)))
6351                 ? wide_type : type);
6352   tree t1, t2;
6353   int same_p = tcode == code;
6354   tree op0 = NULL_TREE, op1 = NULL_TREE;
6355   bool sub_strict_overflow_p;
6356
6357   /* Don't deal with constants of zero here; they confuse the code below.  */
6358   if (integer_zerop (c))
6359     return NULL_TREE;
6360
6361   if (TREE_CODE_CLASS (tcode) == tcc_unary)
6362     op0 = TREE_OPERAND (t, 0);
6363
6364   if (TREE_CODE_CLASS (tcode) == tcc_binary)
6365     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6366
6367   /* Note that we need not handle conditional operations here since fold
6368      already handles those cases.  So just do arithmetic here.  */
6369   switch (tcode)
6370     {
6371     case INTEGER_CST:
6372       /* For a constant, we can always simplify if we are a multiply
6373          or (for divide and modulus) if it is a multiple of our constant.  */
6374       if (code == MULT_EXPR
6375           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6376         return const_binop (code, fold_convert (ctype, t),
6377                             fold_convert (ctype, c), 0);
6378       break;
6379
6380     CASE_CONVERT: case NON_LVALUE_EXPR:
6381       /* If op0 is an expression ...  */
6382       if ((COMPARISON_CLASS_P (op0)
6383            || UNARY_CLASS_P (op0)
6384            || BINARY_CLASS_P (op0)
6385            || VL_EXP_CLASS_P (op0)
6386            || EXPRESSION_CLASS_P (op0))
6387           /* ... and has wrapping overflow, and its type is smaller
6388              than ctype, then we cannot pass through as widening.  */
6389           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6390                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6391                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6392                && (TYPE_PRECISION (ctype)
6393                    > TYPE_PRECISION (TREE_TYPE (op0))))
6394               /* ... or this is a truncation (t is narrower than op0),
6395                  then we cannot pass through this narrowing.  */
6396               || (TYPE_PRECISION (type)
6397                   < TYPE_PRECISION (TREE_TYPE (op0)))
6398               /* ... or signedness changes for division or modulus,
6399                  then we cannot pass through this conversion.  */
6400               || (code != MULT_EXPR
6401                   && (TYPE_UNSIGNED (ctype)
6402                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
6403               /* ... or has undefined overflow while the converted to
6404                  type has not, we cannot do the operation in the inner type
6405                  as that would introduce undefined overflow.  */
6406               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6407                   && !TYPE_OVERFLOW_UNDEFINED (type))))
6408         break;
6409
6410       /* Pass the constant down and see if we can make a simplification.  If
6411          we can, replace this expression with the inner simplification for
6412          possible later conversion to our or some other type.  */
6413       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6414           && TREE_CODE (t2) == INTEGER_CST
6415           && !TREE_OVERFLOW (t2)
6416           && (0 != (t1 = extract_muldiv (op0, t2, code,
6417                                          code == MULT_EXPR
6418                                          ? ctype : NULL_TREE,
6419                                          strict_overflow_p))))
6420         return t1;
6421       break;
6422
6423     case ABS_EXPR:
6424       /* If widening the type changes it from signed to unsigned, then we
6425          must avoid building ABS_EXPR itself as unsigned.  */
6426       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6427         {
6428           tree cstype = (*signed_type_for) (ctype);
6429           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6430               != 0)
6431             {
6432               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6433               return fold_convert (ctype, t1);
6434             }
6435           break;
6436         }
6437       /* If the constant is negative, we cannot simplify this.  */
6438       if (tree_int_cst_sgn (c) == -1)
6439         break;
6440       /* FALLTHROUGH */
6441     case NEGATE_EXPR:
6442       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6443           != 0)
6444         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6445       break;
6446
6447     case MIN_EXPR:  case MAX_EXPR:
6448       /* If widening the type changes the signedness, then we can't perform
6449          this optimization as that changes the result.  */
6450       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6451         break;
6452
6453       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
6454       sub_strict_overflow_p = false;
6455       if ((t1 = extract_muldiv (op0, c, code, wide_type,
6456                                 &sub_strict_overflow_p)) != 0
6457           && (t2 = extract_muldiv (op1, c, code, wide_type,
6458                                    &sub_strict_overflow_p)) != 0)
6459         {
6460           if (tree_int_cst_sgn (c) < 0)
6461             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6462           if (sub_strict_overflow_p)
6463             *strict_overflow_p = true;
6464           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6465                               fold_convert (ctype, t2));
6466         }
6467       break;
6468
6469     case LSHIFT_EXPR:  case RSHIFT_EXPR:
6470       /* If the second operand is constant, this is a multiplication
6471          or floor division, by a power of two, so we can treat it that
6472          way unless the multiplier or divisor overflows.  Signed
6473          left-shift overflow is implementation-defined rather than
6474          undefined in C90, so do not convert signed left shift into
6475          multiplication.  */
6476       if (TREE_CODE (op1) == INTEGER_CST
6477           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6478           /* const_binop may not detect overflow correctly,
6479              so check for it explicitly here.  */
6480           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6481           && TREE_INT_CST_HIGH (op1) == 0
6482           && 0 != (t1 = fold_convert (ctype,
6483                                       const_binop (LSHIFT_EXPR,
6484                                                    size_one_node,
6485                                                    op1, 0)))
6486           && !TREE_OVERFLOW (t1))
6487         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6488                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
6489                                        ctype,
6490                                        fold_convert (ctype, op0),
6491                                        t1),
6492                                c, code, wide_type, strict_overflow_p);
6493       break;
6494
6495     case PLUS_EXPR:  case MINUS_EXPR:
6496       /* See if we can eliminate the operation on both sides.  If we can, we
6497          can return a new PLUS or MINUS.  If we can't, the only remaining
6498          cases where we can do anything are if the second operand is a
6499          constant.  */
6500       sub_strict_overflow_p = false;
6501       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6502       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6503       if (t1 != 0 && t2 != 0
6504           && (code == MULT_EXPR
6505               /* If not multiplication, we can only do this if both operands
6506                  are divisible by c.  */
6507               || (multiple_of_p (ctype, op0, c)
6508                   && multiple_of_p (ctype, op1, c))))
6509         {
6510           if (sub_strict_overflow_p)
6511             *strict_overflow_p = true;
6512           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6513                               fold_convert (ctype, t2));
6514         }
6515
6516       /* If this was a subtraction, negate OP1 and set it to be an addition.
6517          This simplifies the logic below.  */
6518       if (tcode == MINUS_EXPR)
6519         {
6520           tcode = PLUS_EXPR, op1 = negate_expr (op1);
6521           /* If OP1 was not easily negatable, the constant may be OP0.  */
6522           if (TREE_CODE (op0) == INTEGER_CST)
6523             {
6524               tree tem = op0;
6525               op0 = op1;
6526               op1 = tem;
6527               tem = t1;
6528               t1 = t2;
6529               t2 = tem;
6530             }
6531         }
6532
6533       if (TREE_CODE (op1) != INTEGER_CST)
6534         break;
6535
6536       /* If either OP1 or C are negative, this optimization is not safe for
6537          some of the division and remainder types while for others we need
6538          to change the code.  */
6539       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6540         {
6541           if (code == CEIL_DIV_EXPR)
6542             code = FLOOR_DIV_EXPR;
6543           else if (code == FLOOR_DIV_EXPR)
6544             code = CEIL_DIV_EXPR;
6545           else if (code != MULT_EXPR
6546                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6547             break;
6548         }
6549
6550       /* If it's a multiply or a division/modulus operation of a multiple
6551          of our constant, do the operation and verify it doesn't overflow.  */
6552       if (code == MULT_EXPR
6553           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6554         {
6555           op1 = const_binop (code, fold_convert (ctype, op1),
6556                              fold_convert (ctype, c), 0);
6557           /* We allow the constant to overflow with wrapping semantics.  */
6558           if (op1 == 0
6559               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6560             break;
6561         }
6562       else
6563         break;
6564
6565       /* If we have an unsigned type is not a sizetype, we cannot widen
6566          the operation since it will change the result if the original
6567          computation overflowed.  */
6568       if (TYPE_UNSIGNED (ctype)
6569           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6570           && ctype != type)
6571         break;
6572
6573       /* If we were able to eliminate our operation from the first side,
6574          apply our operation to the second side and reform the PLUS.  */
6575       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6576         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6577
6578       /* The last case is if we are a multiply.  In that case, we can
6579          apply the distributive law to commute the multiply and addition
6580          if the multiplication of the constants doesn't overflow.  */
6581       if (code == MULT_EXPR)
6582         return fold_build2 (tcode, ctype,
6583                             fold_build2 (code, ctype,
6584                                          fold_convert (ctype, op0),
6585                                          fold_convert (ctype, c)),
6586                             op1);
6587
6588       break;
6589
6590     case MULT_EXPR:
6591       /* We have a special case here if we are doing something like
6592          (C * 8) % 4 since we know that's zero.  */
6593       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6594            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6595           /* If the multiplication can overflow we cannot optimize this.
6596              ???  Until we can properly mark individual operations as
6597              not overflowing we need to treat sizetype special here as
6598              stor-layout relies on this opimization to make
6599              DECL_FIELD_BIT_OFFSET always a constant.  */
6600           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6601               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6602                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
6603           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6604           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6605         {
6606           *strict_overflow_p = true;
6607           return omit_one_operand (type, integer_zero_node, op0);
6608         }
6609
6610       /* ... fall through ...  */
6611
6612     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
6613     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
6614       /* If we can extract our operation from the LHS, do so and return a
6615          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
6616          do something only if the second operand is a constant.  */
6617       if (same_p
6618           && (t1 = extract_muldiv (op0, c, code, wide_type,
6619                                    strict_overflow_p)) != 0)
6620         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6621                             fold_convert (ctype, op1));
6622       else if (tcode == MULT_EXPR && code == MULT_EXPR
6623                && (t1 = extract_muldiv (op1, c, code, wide_type,
6624                                         strict_overflow_p)) != 0)
6625         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6626                             fold_convert (ctype, t1));
6627       else if (TREE_CODE (op1) != INTEGER_CST)
6628         return 0;
6629
6630       /* If these are the same operation types, we can associate them
6631          assuming no overflow.  */
6632       if (tcode == code
6633           && 0 != (t1 = int_const_binop (MULT_EXPR,
6634                                          fold_convert (ctype, op1),
6635                                          fold_convert (ctype, c), 1))
6636           && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
6637                                                TREE_INT_CST_HIGH (t1),
6638                                                (TYPE_UNSIGNED (ctype)
6639                                                 && tcode != MULT_EXPR) ? -1 : 1,
6640                                                TREE_OVERFLOW (t1)))
6641           && !TREE_OVERFLOW (t1))
6642         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6643
6644       /* If these operations "cancel" each other, we have the main
6645          optimizations of this pass, which occur when either constant is a
6646          multiple of the other, in which case we replace this with either an
6647          operation or CODE or TCODE.
6648
6649          If we have an unsigned type that is not a sizetype, we cannot do
6650          this since it will change the result if the original computation
6651          overflowed.  */
6652       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6653            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6654           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6655               || (tcode == MULT_EXPR
6656                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6657                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6658                   && code != MULT_EXPR)))
6659         {
6660           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6661             {
6662               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6663                 *strict_overflow_p = true;
6664               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6665                                   fold_convert (ctype,
6666                                                 const_binop (TRUNC_DIV_EXPR,
6667                                                              op1, c, 0)));
6668             }
6669           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6670             {
6671               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6672                 *strict_overflow_p = true;
6673               return fold_build2 (code, ctype, fold_convert (ctype, op0),
6674                                   fold_convert (ctype,
6675                                                 const_binop (TRUNC_DIV_EXPR,
6676                                                              c, op1, 0)));
6677             }
6678         }
6679       break;
6680
6681     default:
6682       break;
6683     }
6684
6685   return 0;
6686 }
6687 \f
6688 /* Return a node which has the indicated constant VALUE (either 0 or
6689    1), and is of the indicated TYPE.  */
6690
6691 tree
6692 constant_boolean_node (int value, tree type)
6693 {
6694   if (type == integer_type_node)
6695     return value ? integer_one_node : integer_zero_node;
6696   else if (type == boolean_type_node)
6697     return value ? boolean_true_node : boolean_false_node;
6698   else
6699     return build_int_cst (type, value);
6700 }
6701
6702
6703 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6704    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6705    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6706    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6707    COND is the first argument to CODE; otherwise (as in the example
6708    given here), it is the second argument.  TYPE is the type of the
6709    original expression.  Return NULL_TREE if no simplification is
6710    possible.  */
6711
6712 static tree
6713 fold_binary_op_with_conditional_arg (location_t loc,
6714                                      enum tree_code code,
6715                                      tree type, tree op0, tree op1,
6716                                      tree cond, tree arg, int cond_first_p)
6717 {
6718   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6719   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6720   tree test, true_value, false_value;
6721   tree lhs = NULL_TREE;
6722   tree rhs = NULL_TREE;
6723
6724   /* This transformation is only worthwhile if we don't have to wrap
6725      arg in a SAVE_EXPR, and the operation can be simplified on at least
6726      one of the branches once its pushed inside the COND_EXPR.  */
6727   if (!TREE_CONSTANT (arg))
6728     return NULL_TREE;
6729
6730   if (TREE_CODE (cond) == COND_EXPR)
6731     {
6732       test = TREE_OPERAND (cond, 0);
6733       true_value = TREE_OPERAND (cond, 1);
6734       false_value = TREE_OPERAND (cond, 2);
6735       /* If this operand throws an expression, then it does not make
6736          sense to try to perform a logical or arithmetic operation
6737          involving it.  */
6738       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6739         lhs = true_value;
6740       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6741         rhs = false_value;
6742     }
6743   else
6744     {
6745       tree testtype = TREE_TYPE (cond);
6746       test = cond;
6747       true_value = constant_boolean_node (true, testtype);
6748       false_value = constant_boolean_node (false, testtype);
6749     }
6750
6751   arg = fold_convert_loc (loc, arg_type, arg);
6752   if (lhs == 0)
6753     {
6754       true_value = fold_convert_loc (loc, cond_type, true_value);
6755       if (cond_first_p)
6756         lhs = fold_build2_loc (loc, code, type, true_value, arg);
6757       else
6758         lhs = fold_build2_loc (loc, code, type, arg, true_value);
6759     }
6760   if (rhs == 0)
6761     {
6762       false_value = fold_convert_loc (loc, cond_type, false_value);
6763       if (cond_first_p)
6764         rhs = fold_build2_loc (loc, code, type, false_value, arg);
6765       else
6766         rhs = fold_build2_loc (loc, code, type, arg, false_value);
6767     }
6768
6769   test = fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6770   return fold_convert_loc (loc, type, test);
6771 }
6772
6773 \f
6774 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6775
6776    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6777    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6778    ADDEND is the same as X.
6779
6780    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6781    and finite.  The problematic cases are when X is zero, and its mode
6782    has signed zeros.  In the case of rounding towards -infinity,
6783    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6784    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6785
6786 bool
6787 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6788 {
6789   if (!real_zerop (addend))
6790     return false;
6791
6792   /* Don't allow the fold with -fsignaling-nans.  */
6793   if (HONOR_SNANS (TYPE_MODE (type)))
6794     return false;
6795
6796   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6797   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6798     return true;
6799
6800   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6801   if (TREE_CODE (addend) == REAL_CST
6802       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6803     negate = !negate;
6804
6805   /* The mode has signed zeros, and we have to honor their sign.
6806      In this situation, there is only one case we can return true for.
6807      X - 0 is the same as X unless rounding towards -infinity is
6808      supported.  */
6809   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6810 }
6811
6812 /* Subroutine of fold() that checks comparisons of built-in math
6813    functions against real constants.
6814
6815    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6816    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6817    is the type of the result and ARG0 and ARG1 are the operands of the
6818    comparison.  ARG1 must be a TREE_REAL_CST.
6819
6820    The function returns the constant folded tree if a simplification
6821    can be made, and NULL_TREE otherwise.  */
6822
6823 static tree
6824 fold_mathfn_compare (location_t loc,
6825                      enum built_in_function fcode, enum tree_code code,
6826                      tree type, tree arg0, tree arg1)
6827 {
6828   REAL_VALUE_TYPE c;
6829
6830   if (BUILTIN_SQRT_P (fcode))
6831     {
6832       tree arg = CALL_EXPR_ARG (arg0, 0);
6833       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6834
6835       c = TREE_REAL_CST (arg1);
6836       if (REAL_VALUE_NEGATIVE (c))
6837         {
6838           /* sqrt(x) < y is always false, if y is negative.  */
6839           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6840             return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6841
6842           /* sqrt(x) > y is always true, if y is negative and we
6843              don't care about NaNs, i.e. negative values of x.  */
6844           if (code == NE_EXPR || !HONOR_NANS (mode))
6845             return omit_one_operand_loc (loc, type, integer_one_node, arg);
6846
6847           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6848           return fold_build2_loc (loc, GE_EXPR, type, arg,
6849                               build_real (TREE_TYPE (arg), dconst0));
6850         }
6851       else if (code == GT_EXPR || code == GE_EXPR)
6852         {
6853           REAL_VALUE_TYPE c2;
6854
6855           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6856           real_convert (&c2, mode, &c2);
6857
6858           if (REAL_VALUE_ISINF (c2))
6859             {
6860               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6861               if (HONOR_INFINITIES (mode))
6862                 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6863                                     build_real (TREE_TYPE (arg), c2));
6864
6865               /* sqrt(x) > y is always false, when y is very large
6866                  and we don't care about infinities.  */
6867               return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6868             }
6869
6870           /* sqrt(x) > c is the same as x > c*c.  */
6871           return fold_build2_loc (loc, code, type, arg,
6872                               build_real (TREE_TYPE (arg), c2));
6873         }
6874       else if (code == LT_EXPR || code == LE_EXPR)
6875         {
6876           REAL_VALUE_TYPE c2;
6877
6878           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6879           real_convert (&c2, mode, &c2);
6880
6881           if (REAL_VALUE_ISINF (c2))
6882             {
6883               /* sqrt(x) < y is always true, when y is a very large
6884                  value and we don't care about NaNs or Infinities.  */
6885               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6886                 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6887
6888               /* sqrt(x) < y is x != +Inf when y is very large and we
6889                  don't care about NaNs.  */
6890               if (! HONOR_NANS (mode))
6891                 return fold_build2_loc (loc, NE_EXPR, type, arg,
6892                                     build_real (TREE_TYPE (arg), c2));
6893
6894               /* sqrt(x) < y is x >= 0 when y is very large and we
6895                  don't care about Infinities.  */
6896               if (! HONOR_INFINITIES (mode))
6897                 return fold_build2_loc (loc, GE_EXPR, type, arg,
6898                                     build_real (TREE_TYPE (arg), dconst0));
6899
6900               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6901               if (lang_hooks.decls.global_bindings_p () != 0
6902                   || CONTAINS_PLACEHOLDER_P (arg))
6903                 return NULL_TREE;
6904
6905               arg = save_expr (arg);
6906               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6907                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6908                                                build_real (TREE_TYPE (arg),
6909                                                            dconst0)),
6910                                   fold_build2_loc (loc, NE_EXPR, type, arg,
6911                                                build_real (TREE_TYPE (arg),
6912                                                            c2)));
6913             }
6914
6915           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6916           if (! HONOR_NANS (mode))
6917             return fold_build2_loc (loc, code, type, arg,
6918                                 build_real (TREE_TYPE (arg), c2));
6919
6920           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6921           if (lang_hooks.decls.global_bindings_p () == 0
6922               && ! CONTAINS_PLACEHOLDER_P (arg))
6923             {
6924               arg = save_expr (arg);
6925               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6926                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6927                                                build_real (TREE_TYPE (arg),
6928                                                            dconst0)),
6929                                   fold_build2_loc (loc, code, type, arg,
6930                                                build_real (TREE_TYPE (arg),
6931                                                            c2)));
6932             }
6933         }
6934     }
6935
6936   return NULL_TREE;
6937 }
6938
6939 /* Subroutine of fold() that optimizes comparisons against Infinities,
6940    either +Inf or -Inf.
6941
6942    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6943    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6944    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6945
6946    The function returns the constant folded tree if a simplification
6947    can be made, and NULL_TREE otherwise.  */
6948
6949 static tree
6950 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6951                   tree arg0, tree arg1)
6952 {
6953   enum machine_mode mode;
6954   REAL_VALUE_TYPE max;
6955   tree temp;
6956   bool neg;
6957
6958   mode = TYPE_MODE (TREE_TYPE (arg0));
6959
6960   /* For negative infinity swap the sense of the comparison.  */
6961   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6962   if (neg)
6963     code = swap_tree_comparison (code);
6964
6965   switch (code)
6966     {
6967     case GT_EXPR:
6968       /* x > +Inf is always false, if with ignore sNANs.  */
6969       if (HONOR_SNANS (mode))
6970         return NULL_TREE;
6971       return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6972
6973     case LE_EXPR:
6974       /* x <= +Inf is always true, if we don't case about NaNs.  */
6975       if (! HONOR_NANS (mode))
6976         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6977
6978       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6979       if (lang_hooks.decls.global_bindings_p () == 0
6980           && ! CONTAINS_PLACEHOLDER_P (arg0))
6981         {
6982           arg0 = save_expr (arg0);
6983           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6984         }
6985       break;
6986
6987     case EQ_EXPR:
6988     case GE_EXPR:
6989       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6990       real_maxval (&max, neg, mode);
6991       return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6992                           arg0, build_real (TREE_TYPE (arg0), max));
6993
6994     case LT_EXPR:
6995       /* x < +Inf is always equal to x <= DBL_MAX.  */
6996       real_maxval (&max, neg, mode);
6997       return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6998                           arg0, build_real (TREE_TYPE (arg0), max));
6999
7000     case NE_EXPR:
7001       /* x != +Inf is always equal to !(x > DBL_MAX).  */
7002       real_maxval (&max, neg, mode);
7003       if (! HONOR_NANS (mode))
7004         return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
7005                             arg0, build_real (TREE_TYPE (arg0), max));
7006
7007       temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
7008                           arg0, build_real (TREE_TYPE (arg0), max));
7009       return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
7010
7011     default:
7012       break;
7013     }
7014
7015   return NULL_TREE;
7016 }
7017
7018 /* Subroutine of fold() that optimizes comparisons of a division by
7019    a nonzero integer constant against an integer constant, i.e.
7020    X/C1 op C2.
7021
7022    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7023    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
7024    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
7025
7026    The function returns the constant folded tree if a simplification
7027    can be made, and NULL_TREE otherwise.  */
7028
7029 static tree
7030 fold_div_compare (location_t loc,
7031                   enum tree_code code, tree type, tree arg0, tree arg1)
7032 {
7033   tree prod, tmp, hi, lo;
7034   tree arg00 = TREE_OPERAND (arg0, 0);
7035   tree arg01 = TREE_OPERAND (arg0, 1);
7036   unsigned HOST_WIDE_INT lpart;
7037   HOST_WIDE_INT hpart;
7038   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
7039   bool neg_overflow;
7040   int overflow;
7041
7042   /* We have to do this the hard way to detect unsigned overflow.
7043      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
7044   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
7045                                    TREE_INT_CST_HIGH (arg01),
7046                                    TREE_INT_CST_LOW (arg1),
7047                                    TREE_INT_CST_HIGH (arg1),
7048                                    &lpart, &hpart, unsigned_p);
7049   prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
7050                                 -1, overflow);
7051   neg_overflow = false;
7052
7053   if (unsigned_p)
7054     {
7055       tmp = int_const_binop (MINUS_EXPR, arg01,
7056                              build_int_cst (TREE_TYPE (arg01), 1), 0);
7057       lo = prod;
7058
7059       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
7060       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
7061                                        TREE_INT_CST_HIGH (prod),
7062                                        TREE_INT_CST_LOW (tmp),
7063                                        TREE_INT_CST_HIGH (tmp),
7064                                        &lpart, &hpart, unsigned_p);
7065       hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
7066                                   -1, overflow | TREE_OVERFLOW (prod));
7067     }
7068   else if (tree_int_cst_sgn (arg01) >= 0)
7069     {
7070       tmp = int_const_binop (MINUS_EXPR, arg01,
7071                              build_int_cst (TREE_TYPE (arg01), 1), 0);
7072       switch (tree_int_cst_sgn (arg1))
7073         {
7074         case -1:
7075           neg_overflow = true;
7076           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
7077           hi = prod;
7078           break;
7079
7080         case  0:
7081           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
7082           hi = tmp;
7083           break;
7084
7085         case  1:
7086           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
7087           lo = prod;
7088           break;
7089
7090         default:
7091           gcc_unreachable ();
7092         }
7093     }
7094   else
7095     {
7096       /* A negative divisor reverses the relational operators.  */
7097       code = swap_tree_comparison (code);
7098
7099       tmp = int_const_binop (PLUS_EXPR, arg01,
7100                              build_int_cst (TREE_TYPE (arg01), 1), 0);
7101       switch (tree_int_cst_sgn (arg1))
7102         {
7103         case -1:
7104           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
7105           lo = prod;
7106           break;
7107
7108         case  0:
7109           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
7110           lo = tmp;
7111           break;
7112
7113         case  1:
7114           neg_overflow = true;
7115           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
7116           hi = prod;
7117           break;
7118
7119         default:
7120           gcc_unreachable ();
7121         }
7122     }
7123
7124   switch (code)
7125     {
7126     case EQ_EXPR:
7127       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
7128         return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
7129       if (TREE_OVERFLOW (hi))
7130         return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
7131       if (TREE_OVERFLOW (lo))
7132         return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
7133       return build_range_check (loc, type, arg00, 1, lo, hi);
7134
7135     case NE_EXPR:
7136       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
7137         return omit_one_operand_loc (loc, type, integer_one_node, arg00);
7138       if (TREE_OVERFLOW (hi))
7139         return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
7140       if (TREE_OVERFLOW (lo))
7141         return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
7142       return build_range_check (loc, type, arg00, 0, lo, hi);
7143
7144     case LT_EXPR:
7145       if (TREE_OVERFLOW (lo))
7146         {
7147           tmp = neg_overflow ? integer_zero_node : integer_one_node;
7148           return omit_one_operand_loc (loc, type, tmp, arg00);
7149         }
7150       return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
7151
7152     case LE_EXPR:
7153       if (TREE_OVERFLOW (hi))
7154         {
7155           tmp = neg_overflow ? integer_zero_node : integer_one_node;
7156           return omit_one_operand_loc (loc, type, tmp, arg00);
7157         }
7158       return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
7159
7160     case GT_EXPR:
7161       if (TREE_OVERFLOW (hi))
7162         {
7163           tmp = neg_overflow ? integer_one_node : integer_zero_node;
7164           return omit_one_operand_loc (loc, type, tmp, arg00);
7165         }
7166       return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
7167
7168     case GE_EXPR:
7169       if (TREE_OVERFLOW (lo))
7170         {
7171           tmp = neg_overflow ? integer_one_node : integer_zero_node;
7172           return omit_one_operand_loc (loc, type, tmp, arg00);
7173         }
7174       return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
7175
7176     default:
7177       break;
7178     }
7179
7180   return NULL_TREE;
7181 }
7182
7183
7184 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7185    equality/inequality test, then return a simplified form of the test
7186    using a sign testing.  Otherwise return NULL.  TYPE is the desired
7187    result type.  */
7188
7189 static tree
7190 fold_single_bit_test_into_sign_test (location_t loc,
7191                                      enum tree_code code, tree arg0, tree arg1,
7192                                      tree result_type)
7193 {
7194   /* If this is testing a single bit, we can optimize the test.  */
7195   if ((code == NE_EXPR || code == EQ_EXPR)
7196       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
7197       && integer_pow2p (TREE_OPERAND (arg0, 1)))
7198     {
7199       /* If we have (A & C) != 0 where C is the sign bit of A, convert
7200          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
7201       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
7202
7203       if (arg00 != NULL_TREE
7204           /* This is only a win if casting to a signed type is cheap,
7205              i.e. when arg00's type is not a partial mode.  */
7206           && TYPE_PRECISION (TREE_TYPE (arg00))
7207              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
7208         {
7209           tree stype = signed_type_for (TREE_TYPE (arg00));
7210           return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
7211                               result_type,
7212                               fold_convert_loc (loc, stype, arg00),
7213                               build_int_cst (stype, 0));
7214         }
7215     }
7216
7217   return NULL_TREE;
7218 }
7219
7220 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7221    equality/inequality test, then return a simplified form of
7222    the test using shifts and logical operations.  Otherwise return
7223    NULL.  TYPE is the desired result type.  */
7224
7225 tree
7226 fold_single_bit_test (location_t loc, enum tree_code code,
7227                       tree arg0, tree arg1, tree result_type)
7228 {
7229   /* If this is testing a single bit, we can optimize the test.  */
7230   if ((code == NE_EXPR || code == EQ_EXPR)
7231       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
7232       && integer_pow2p (TREE_OPERAND (arg0, 1)))
7233     {
7234       tree inner = TREE_OPERAND (arg0, 0);
7235       tree type = TREE_TYPE (arg0);
7236       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
7237       enum machine_mode operand_mode = TYPE_MODE (type);
7238       int ops_unsigned;
7239       tree signed_type, unsigned_type, intermediate_type;
7240       tree tem, one;
7241
7242       /* First, see if we can fold the single bit test into a sign-bit
7243          test.  */
7244       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
7245                                                  result_type);
7246       if (tem)
7247         return tem;
7248
7249       /* Otherwise we have (A & C) != 0 where C is a single bit,
7250          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
7251          Similarly for (A & C) == 0.  */
7252
7253       /* If INNER is a right shift of a constant and it plus BITNUM does
7254          not overflow, adjust BITNUM and INNER.  */
7255       if (TREE_CODE (inner) == RSHIFT_EXPR
7256           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
7257           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
7258           && bitnum < TYPE_PRECISION (type)
7259           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
7260                                    bitnum - TYPE_PRECISION (type)))
7261         {
7262           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
7263           inner = TREE_OPERAND (inner, 0);
7264         }
7265
7266       /* If we are going to be able to omit the AND below, we must do our
7267          operations as unsigned.  If we must use the AND, we have a choice.
7268          Normally unsigned is faster, but for some machines signed is.  */
7269 #ifdef LOAD_EXTEND_OP
7270       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
7271                       && !flag_syntax_only) ? 0 : 1;
7272 #else
7273       ops_unsigned = 1;
7274 #endif
7275
7276       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
7277       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
7278       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
7279       inner = fold_convert_loc (loc, intermediate_type, inner);
7280
7281       if (bitnum != 0)
7282         inner = build2 (RSHIFT_EXPR, intermediate_type,
7283                         inner, size_int (bitnum));
7284
7285       one = build_int_cst (intermediate_type, 1);
7286
7287       if (code == EQ_EXPR)
7288         inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
7289
7290       /* Put the AND last so it can combine with more things.  */
7291       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
7292
7293       /* Make sure to return the proper type.  */
7294       inner = fold_convert_loc (loc, result_type, inner);
7295
7296       return inner;
7297     }
7298   return NULL_TREE;
7299 }
7300
7301 /* Check whether we are allowed to reorder operands arg0 and arg1,
7302    such that the evaluation of arg1 occurs before arg0.  */
7303
7304 static bool
7305 reorder_operands_p (const_tree arg0, const_tree arg1)
7306 {
7307   if (! flag_evaluation_order)
7308       return true;
7309   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
7310     return true;
7311   return ! TREE_SIDE_EFFECTS (arg0)
7312          && ! TREE_SIDE_EFFECTS (arg1);
7313 }
7314
7315 /* Test whether it is preferable two swap two operands, ARG0 and
7316    ARG1, for example because ARG0 is an integer constant and ARG1
7317    isn't.  If REORDER is true, only recommend swapping if we can
7318    evaluate the operands in reverse order.  */
7319
7320 bool
7321 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
7322 {
7323   STRIP_SIGN_NOPS (arg0);
7324   STRIP_SIGN_NOPS (arg1);
7325
7326   if (TREE_CODE (arg1) == INTEGER_CST)
7327     return 0;
7328   if (TREE_CODE (arg0) == INTEGER_CST)
7329     return 1;
7330
7331   if (TREE_CODE (arg1) == REAL_CST)
7332     return 0;
7333   if (TREE_CODE (arg0) == REAL_CST)
7334     return 1;
7335
7336   if (TREE_CODE (arg1) == FIXED_CST)
7337     return 0;
7338   if (TREE_CODE (arg0) == FIXED_CST)
7339     return 1;
7340
7341   if (TREE_CODE (arg1) == COMPLEX_CST)
7342     return 0;
7343   if (TREE_CODE (arg0) == COMPLEX_CST)
7344     return 1;
7345
7346   if (TREE_CONSTANT (arg1))
7347     return 0;
7348   if (TREE_CONSTANT (arg0))
7349     return 1;
7350
7351   if (optimize_function_for_size_p (cfun))
7352     return 0;
7353
7354   if (reorder && flag_evaluation_order
7355       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7356     return 0;
7357
7358   /* It is preferable to swap two SSA_NAME to ensure a canonical form
7359      for commutative and comparison operators.  Ensuring a canonical
7360      form allows the optimizers to find additional redundancies without
7361      having to explicitly check for both orderings.  */
7362   if (TREE_CODE (arg0) == SSA_NAME
7363       && TREE_CODE (arg1) == SSA_NAME
7364       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7365     return 1;
7366
7367   /* Put SSA_NAMEs last.  */
7368   if (TREE_CODE (arg1) == SSA_NAME)
7369     return 0;
7370   if (TREE_CODE (arg0) == SSA_NAME)
7371     return 1;
7372
7373   /* Put variables last.  */
7374   if (DECL_P (arg1))
7375     return 0;
7376   if (DECL_P (arg0))
7377     return 1;
7378
7379   return 0;
7380 }
7381
7382 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7383    ARG0 is extended to a wider type.  */
7384
7385 static tree
7386 fold_widened_comparison (location_t loc, enum tree_code code,
7387                          tree type, tree arg0, tree arg1)
7388 {
7389   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7390   tree arg1_unw;
7391   tree shorter_type, outer_type;
7392   tree min, max;
7393   bool above, below;
7394
7395   if (arg0_unw == arg0)
7396     return NULL_TREE;
7397   shorter_type = TREE_TYPE (arg0_unw);
7398
7399 #ifdef HAVE_canonicalize_funcptr_for_compare
7400   /* Disable this optimization if we're casting a function pointer
7401      type on targets that require function pointer canonicalization.  */
7402   if (HAVE_canonicalize_funcptr_for_compare
7403       && TREE_CODE (shorter_type) == POINTER_TYPE
7404       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7405     return NULL_TREE;
7406 #endif
7407
7408   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7409     return NULL_TREE;
7410
7411   arg1_unw = get_unwidened (arg1, NULL_TREE);
7412
7413   /* If possible, express the comparison in the shorter mode.  */
7414   if ((code == EQ_EXPR || code == NE_EXPR
7415        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7416       && (TREE_TYPE (arg1_unw) == shorter_type
7417           || ((TYPE_PRECISION (shorter_type)
7418                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
7419               && (TYPE_UNSIGNED (shorter_type)
7420                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
7421           || (TREE_CODE (arg1_unw) == INTEGER_CST
7422               && (TREE_CODE (shorter_type) == INTEGER_TYPE
7423                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7424               && int_fits_type_p (arg1_unw, shorter_type))))
7425     return fold_build2_loc (loc, code, type, arg0_unw,
7426                         fold_convert_loc (loc, shorter_type, arg1_unw));
7427
7428   if (TREE_CODE (arg1_unw) != INTEGER_CST
7429       || TREE_CODE (shorter_type) != INTEGER_TYPE
7430       || !int_fits_type_p (arg1_unw, shorter_type))
7431     return NULL_TREE;
7432
7433   /* If we are comparing with the integer that does not fit into the range
7434      of the shorter type, the result is known.  */
7435   outer_type = TREE_TYPE (arg1_unw);
7436   min = lower_bound_in_type (outer_type, shorter_type);
7437   max = upper_bound_in_type (outer_type, shorter_type);
7438
7439   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7440                                                    max, arg1_unw));
7441   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7442                                                    arg1_unw, min));
7443
7444   switch (code)
7445     {
7446     case EQ_EXPR:
7447       if (above || below)
7448         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7449       break;
7450
7451     case NE_EXPR:
7452       if (above || below)
7453         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7454       break;
7455
7456     case LT_EXPR:
7457     case LE_EXPR:
7458       if (above)
7459         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7460       else if (below)
7461         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7462
7463     case GT_EXPR:
7464     case GE_EXPR:
7465       if (above)
7466         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7467       else if (below)
7468         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7469
7470     default:
7471       break;
7472     }
7473
7474   return NULL_TREE;
7475 }
7476
7477 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7478    ARG0 just the signedness is changed.  */
7479
7480 static tree
7481 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
7482                               tree arg0, tree arg1)
7483 {
7484   tree arg0_inner;
7485   tree inner_type, outer_type;
7486
7487   if (!CONVERT_EXPR_P (arg0))
7488     return NULL_TREE;
7489
7490   outer_type = TREE_TYPE (arg0);
7491   arg0_inner = TREE_OPERAND (arg0, 0);
7492   inner_type = TREE_TYPE (arg0_inner);
7493
7494 #ifdef HAVE_canonicalize_funcptr_for_compare
7495   /* Disable this optimization if we're casting a function pointer
7496      type on targets that require function pointer canonicalization.  */
7497   if (HAVE_canonicalize_funcptr_for_compare
7498       && TREE_CODE (inner_type) == POINTER_TYPE
7499       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7500     return NULL_TREE;
7501 #endif
7502
7503   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7504     return NULL_TREE;
7505
7506   if (TREE_CODE (arg1) != INTEGER_CST
7507       && !(CONVERT_EXPR_P (arg1)
7508            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7509     return NULL_TREE;
7510
7511   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7512        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7513       && code != NE_EXPR
7514       && code != EQ_EXPR)
7515     return NULL_TREE;
7516
7517   if (TREE_CODE (arg1) == INTEGER_CST)
7518     arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7519                                   TREE_INT_CST_HIGH (arg1), 0,
7520                                   TREE_OVERFLOW (arg1));
7521   else
7522     arg1 = fold_convert_loc (loc, inner_type, arg1);
7523
7524   return fold_build2_loc (loc, code, type, arg0_inner, arg1);
7525 }
7526
7527 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7528    step of the array.  Reconstructs s and delta in the case of s *
7529    delta being an integer constant (and thus already folded).  ADDR is
7530    the address. MULT is the multiplicative expression.  If the
7531    function succeeds, the new address expression is returned.
7532    Otherwise NULL_TREE is returned.  LOC is the location of the
7533    resulting expression.  */
7534
7535 static tree
7536 try_move_mult_to_index (location_t loc, tree addr, tree op1)
7537 {
7538   tree s, delta, step;
7539   tree ref = TREE_OPERAND (addr, 0), pref;
7540   tree ret, pos;
7541   tree itype;
7542   bool mdim = false;
7543
7544   /*  Strip the nops that might be added when converting op1 to sizetype. */
7545   STRIP_NOPS (op1);
7546
7547   /* Canonicalize op1 into a possibly non-constant delta
7548      and an INTEGER_CST s.  */
7549   if (TREE_CODE (op1) == MULT_EXPR)
7550     {
7551       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7552
7553       STRIP_NOPS (arg0);
7554       STRIP_NOPS (arg1);
7555
7556       if (TREE_CODE (arg0) == INTEGER_CST)
7557         {
7558           s = arg0;
7559           delta = arg1;
7560         }
7561       else if (TREE_CODE (arg1) == INTEGER_CST)
7562         {
7563           s = arg1;
7564           delta = arg0;
7565         }
7566       else
7567         return NULL_TREE;
7568     }
7569   else if (TREE_CODE (op1) == INTEGER_CST)
7570     {
7571       delta = op1;
7572       s = NULL_TREE;
7573     }
7574   else
7575     {
7576       /* Simulate we are delta * 1.  */
7577       delta = op1;
7578       s = integer_one_node;
7579     }
7580
7581   for (;; ref = TREE_OPERAND (ref, 0))
7582     {
7583       if (TREE_CODE (ref) == ARRAY_REF)
7584         {
7585           tree domain;
7586
7587           /* Remember if this was a multi-dimensional array.  */
7588           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7589             mdim = true;
7590
7591           domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7592           if (! domain)
7593             continue;
7594           itype = TREE_TYPE (domain);
7595
7596           step = array_ref_element_size (ref);
7597           if (TREE_CODE (step) != INTEGER_CST)
7598             continue;
7599
7600           if (s)
7601             {
7602               if (! tree_int_cst_equal (step, s))
7603                 continue;
7604             }
7605           else
7606             {
7607               /* Try if delta is a multiple of step.  */
7608               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
7609               if (! tmp)
7610                 continue;
7611               delta = tmp;
7612             }
7613
7614           /* Only fold here if we can verify we do not overflow one
7615              dimension of a multi-dimensional array.  */
7616           if (mdim)
7617             {
7618               tree tmp;
7619
7620               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7621                   || !TYPE_MAX_VALUE (domain)
7622                   || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
7623                 continue;
7624
7625               tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
7626                                      fold_convert_loc (loc, itype,
7627                                                        TREE_OPERAND (ref, 1)),
7628                                      fold_convert_loc (loc, itype, delta));
7629               if (!tmp
7630                   || TREE_CODE (tmp) != INTEGER_CST
7631                   || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
7632                 continue;
7633             }
7634
7635           break;
7636         }
7637       else
7638         mdim = false;
7639
7640       if (!handled_component_p (ref))
7641         return NULL_TREE;
7642     }
7643
7644   /* We found the suitable array reference.  So copy everything up to it,
7645      and replace the index.  */
7646
7647   pref = TREE_OPERAND (addr, 0);
7648   ret = copy_node (pref);
7649   SET_EXPR_LOCATION (ret, loc);
7650   pos = ret;
7651
7652   while (pref != ref)
7653     {
7654       pref = TREE_OPERAND (pref, 0);
7655       TREE_OPERAND (pos, 0) = copy_node (pref);
7656       pos = TREE_OPERAND (pos, 0);
7657     }
7658
7659   TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype,
7660                                        fold_convert_loc (loc, itype,
7661                                                          TREE_OPERAND (pos, 1)),
7662                                        fold_convert_loc (loc, itype, delta));
7663
7664   return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
7665 }
7666
7667
7668 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
7669    means A >= Y && A != MAX, but in this case we know that
7670    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
7671
7672 static tree
7673 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7674 {
7675   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7676
7677   if (TREE_CODE (bound) == LT_EXPR)
7678     a = TREE_OPERAND (bound, 0);
7679   else if (TREE_CODE (bound) == GT_EXPR)
7680     a = TREE_OPERAND (bound, 1);
7681   else
7682     return NULL_TREE;
7683
7684   typea = TREE_TYPE (a);
7685   if (!INTEGRAL_TYPE_P (typea)
7686       && !POINTER_TYPE_P (typea))
7687     return NULL_TREE;
7688
7689   if (TREE_CODE (ineq) == LT_EXPR)
7690     {
7691       a1 = TREE_OPERAND (ineq, 1);
7692       y = TREE_OPERAND (ineq, 0);
7693     }
7694   else if (TREE_CODE (ineq) == GT_EXPR)
7695     {
7696       a1 = TREE_OPERAND (ineq, 0);
7697       y = TREE_OPERAND (ineq, 1);
7698     }
7699   else
7700     return NULL_TREE;
7701
7702   if (TREE_TYPE (a1) != typea)
7703     return NULL_TREE;
7704
7705   if (POINTER_TYPE_P (typea))
7706     {
7707       /* Convert the pointer types into integer before taking the difference.  */
7708       tree ta = fold_convert_loc (loc, ssizetype, a);
7709       tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7710       diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7711     }
7712   else
7713     diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7714
7715   if (!diff || !integer_onep (diff))
7716    return NULL_TREE;
7717
7718   return fold_build2_loc (loc, GE_EXPR, type, a, y);
7719 }
7720
7721 /* Fold a sum or difference of at least one multiplication.
7722    Returns the folded tree or NULL if no simplification could be made.  */
7723
7724 static tree
7725 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7726                           tree arg0, tree arg1)
7727 {
7728   tree arg00, arg01, arg10, arg11;
7729   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7730
7731   /* (A * C) +- (B * C) -> (A+-B) * C.
7732      (A * C) +- A -> A * (C+-1).
7733      We are most concerned about the case where C is a constant,
7734      but other combinations show up during loop reduction.  Since
7735      it is not difficult, try all four possibilities.  */
7736
7737   if (TREE_CODE (arg0) == MULT_EXPR)
7738     {
7739       arg00 = TREE_OPERAND (arg0, 0);
7740       arg01 = TREE_OPERAND (arg0, 1);
7741     }
7742   else if (TREE_CODE (arg0) == INTEGER_CST)
7743     {
7744       arg00 = build_one_cst (type);
7745       arg01 = arg0;
7746     }
7747   else
7748     {
7749       /* We cannot generate constant 1 for fract.  */
7750       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7751         return NULL_TREE;
7752       arg00 = arg0;
7753       arg01 = build_one_cst (type);
7754     }
7755   if (TREE_CODE (arg1) == MULT_EXPR)
7756     {
7757       arg10 = TREE_OPERAND (arg1, 0);
7758       arg11 = TREE_OPERAND (arg1, 1);
7759     }
7760   else if (TREE_CODE (arg1) == INTEGER_CST)
7761     {
7762       arg10 = build_one_cst (type);
7763       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7764          the purpose of this canonicalization.  */
7765       if (TREE_INT_CST_HIGH (arg1) == -1
7766           && negate_expr_p (arg1)
7767           && code == PLUS_EXPR)
7768         {
7769           arg11 = negate_expr (arg1);
7770           code = MINUS_EXPR;
7771         }
7772       else
7773         arg11 = arg1;
7774     }
7775   else
7776     {
7777       /* We cannot generate constant 1 for fract.  */
7778       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7779         return NULL_TREE;
7780       arg10 = arg1;
7781       arg11 = build_one_cst (type);
7782     }
7783   same = NULL_TREE;
7784
7785   if (operand_equal_p (arg01, arg11, 0))
7786     same = arg01, alt0 = arg00, alt1 = arg10;
7787   else if (operand_equal_p (arg00, arg10, 0))
7788     same = arg00, alt0 = arg01, alt1 = arg11;
7789   else if (operand_equal_p (arg00, arg11, 0))
7790     same = arg00, alt0 = arg01, alt1 = arg10;
7791   else if (operand_equal_p (arg01, arg10, 0))
7792     same = arg01, alt0 = arg00, alt1 = arg11;
7793
7794   /* No identical multiplicands; see if we can find a common
7795      power-of-two factor in non-power-of-two multiplies.  This
7796      can help in multi-dimensional array access.  */
7797   else if (host_integerp (arg01, 0)
7798            && host_integerp (arg11, 0))
7799     {
7800       HOST_WIDE_INT int01, int11, tmp;
7801       bool swap = false;
7802       tree maybe_same;
7803       int01 = TREE_INT_CST_LOW (arg01);
7804       int11 = TREE_INT_CST_LOW (arg11);
7805
7806       /* Move min of absolute values to int11.  */
7807       if ((int01 >= 0 ? int01 : -int01)
7808           < (int11 >= 0 ? int11 : -int11))
7809         {
7810           tmp = int01, int01 = int11, int11 = tmp;
7811           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7812           maybe_same = arg01;
7813           swap = true;
7814         }
7815       else
7816         maybe_same = arg11;
7817
7818       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7819           /* The remainder should not be a constant, otherwise we
7820              end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7821              increased the number of multiplications necessary.  */
7822           && TREE_CODE (arg10) != INTEGER_CST)
7823         {
7824           alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7825                               build_int_cst (TREE_TYPE (arg00),
7826                                              int01 / int11));
7827           alt1 = arg10;
7828           same = maybe_same;
7829           if (swap)
7830             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7831         }
7832     }
7833
7834   if (same)
7835     return fold_build2_loc (loc, MULT_EXPR, type,
7836                         fold_build2_loc (loc, code, type,
7837                                      fold_convert_loc (loc, type, alt0),
7838                                      fold_convert_loc (loc, type, alt1)),
7839                         fold_convert_loc (loc, type, same));
7840
7841   return NULL_TREE;
7842 }
7843
7844 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7845    specified by EXPR into the buffer PTR of length LEN bytes.
7846    Return the number of bytes placed in the buffer, or zero
7847    upon failure.  */
7848
7849 static int
7850 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7851 {
7852   tree type = TREE_TYPE (expr);
7853   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7854   int byte, offset, word, words;
7855   unsigned char value;
7856
7857   if (total_bytes > len)
7858     return 0;
7859   words = total_bytes / UNITS_PER_WORD;
7860
7861   for (byte = 0; byte < total_bytes; byte++)
7862     {
7863       int bitpos = byte * BITS_PER_UNIT;
7864       if (bitpos < HOST_BITS_PER_WIDE_INT)
7865         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7866       else
7867         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7868                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7869
7870       if (total_bytes > UNITS_PER_WORD)
7871         {
7872           word = byte / UNITS_PER_WORD;
7873           if (WORDS_BIG_ENDIAN)
7874             word = (words - 1) - word;
7875           offset = word * UNITS_PER_WORD;
7876           if (BYTES_BIG_ENDIAN)
7877             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7878           else
7879             offset += byte % UNITS_PER_WORD;
7880         }
7881       else
7882         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7883       ptr[offset] = value;
7884     }
7885   return total_bytes;
7886 }
7887
7888
7889 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7890    specified by EXPR into the buffer PTR of length LEN bytes.
7891    Return the number of bytes placed in the buffer, or zero
7892    upon failure.  */
7893
7894 static int
7895 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7896 {
7897   tree type = TREE_TYPE (expr);
7898   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7899   int byte, offset, word, words, bitpos;
7900   unsigned char value;
7901
7902   /* There are always 32 bits in each long, no matter the size of
7903      the hosts long.  We handle floating point representations with
7904      up to 192 bits.  */
7905   long tmp[6];
7906
7907   if (total_bytes > len)
7908     return 0;
7909   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7910
7911   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7912
7913   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7914        bitpos += BITS_PER_UNIT)
7915     {
7916       byte = (bitpos / BITS_PER_UNIT) & 3;
7917       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7918
7919       if (UNITS_PER_WORD < 4)
7920         {
7921           word = byte / UNITS_PER_WORD;
7922           if (WORDS_BIG_ENDIAN)
7923             word = (words - 1) - word;
7924           offset = word * UNITS_PER_WORD;
7925           if (BYTES_BIG_ENDIAN)
7926             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7927           else
7928             offset += byte % UNITS_PER_WORD;
7929         }
7930       else
7931         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7932       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7933     }
7934   return total_bytes;
7935 }
7936
7937 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7938    specified by EXPR into the buffer PTR of length LEN bytes.
7939    Return the number of bytes placed in the buffer, or zero
7940    upon failure.  */
7941
7942 static int
7943 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7944 {
7945   int rsize, isize;
7946   tree part;
7947
7948   part = TREE_REALPART (expr);
7949   rsize = native_encode_expr (part, ptr, len);
7950   if (rsize == 0)
7951     return 0;
7952   part = TREE_IMAGPART (expr);
7953   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7954   if (isize != rsize)
7955     return 0;
7956   return rsize + isize;
7957 }
7958
7959
7960 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7961    specified by EXPR into the buffer PTR of length LEN bytes.
7962    Return the number of bytes placed in the buffer, or zero
7963    upon failure.  */
7964
7965 static int
7966 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7967 {
7968   int i, size, offset, count;
7969   tree itype, elem, elements;
7970
7971   offset = 0;
7972   elements = TREE_VECTOR_CST_ELTS (expr);
7973   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7974   itype = TREE_TYPE (TREE_TYPE (expr));
7975   size = GET_MODE_SIZE (TYPE_MODE (itype));
7976   for (i = 0; i < count; i++)
7977     {
7978       if (elements)
7979         {
7980           elem = TREE_VALUE (elements);
7981           elements = TREE_CHAIN (elements);
7982         }
7983       else
7984         elem = NULL_TREE;
7985
7986       if (elem)
7987         {
7988           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7989             return 0;
7990         }
7991       else
7992         {
7993           if (offset + size > len)
7994             return 0;
7995           memset (ptr+offset, 0, size);
7996         }
7997       offset += size;
7998     }
7999   return offset;
8000 }
8001
8002
8003 /* Subroutine of native_encode_expr.  Encode the STRING_CST
8004    specified by EXPR into the buffer PTR of length LEN bytes.
8005    Return the number of bytes placed in the buffer, or zero
8006    upon failure.  */
8007
8008 static int
8009 native_encode_string (const_tree expr, unsigned char *ptr, int len)
8010 {
8011   tree type = TREE_TYPE (expr);
8012   HOST_WIDE_INT total_bytes;
8013
8014   if (TREE_CODE (type) != ARRAY_TYPE
8015       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
8016       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
8017       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
8018     return 0;
8019   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
8020   if (total_bytes > len)
8021     return 0;
8022   if (TREE_STRING_LENGTH (expr) < total_bytes)
8023     {
8024       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
8025       memset (ptr + TREE_STRING_LENGTH (expr), 0,
8026               total_bytes - TREE_STRING_LENGTH (expr));
8027     }
8028   else
8029     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
8030   return total_bytes;
8031 }
8032
8033
8034 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
8035    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
8036    buffer PTR of length LEN bytes.  Return the number of bytes
8037    placed in the buffer, or zero upon failure.  */
8038
8039 int
8040 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
8041 {
8042   switch (TREE_CODE (expr))
8043     {
8044     case INTEGER_CST:
8045       return native_encode_int (expr, ptr, len);
8046
8047     case REAL_CST:
8048       return native_encode_real (expr, ptr, len);
8049
8050     case COMPLEX_CST:
8051       return native_encode_complex (expr, ptr, len);
8052
8053     case VECTOR_CST:
8054       return native_encode_vector (expr, ptr, len);
8055
8056     case STRING_CST:
8057       return native_encode_string (expr, ptr, len);
8058
8059     default:
8060       return 0;
8061     }
8062 }
8063
8064
8065 /* Subroutine of native_interpret_expr.  Interpret the contents of
8066    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8067    If the buffer cannot be interpreted, return NULL_TREE.  */
8068
8069 static tree
8070 native_interpret_int (tree type, const unsigned char *ptr, int len)
8071 {
8072   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
8073   int byte, offset, word, words;
8074   unsigned char value;
8075   unsigned int HOST_WIDE_INT lo = 0;
8076   HOST_WIDE_INT hi = 0;
8077
8078   if (total_bytes > len)
8079     return NULL_TREE;
8080   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
8081     return NULL_TREE;
8082   words = total_bytes / UNITS_PER_WORD;
8083
8084   for (byte = 0; byte < total_bytes; byte++)
8085     {
8086       int bitpos = byte * BITS_PER_UNIT;
8087       if (total_bytes > UNITS_PER_WORD)
8088         {
8089           word = byte / UNITS_PER_WORD;
8090           if (WORDS_BIG_ENDIAN)
8091             word = (words - 1) - word;
8092           offset = word * UNITS_PER_WORD;
8093           if (BYTES_BIG_ENDIAN)
8094             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8095           else
8096             offset += byte % UNITS_PER_WORD;
8097         }
8098       else
8099         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
8100       value = ptr[offset];
8101
8102       if (bitpos < HOST_BITS_PER_WIDE_INT)
8103         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
8104       else
8105         hi |= (unsigned HOST_WIDE_INT) value
8106               << (bitpos - HOST_BITS_PER_WIDE_INT);
8107     }
8108
8109   return build_int_cst_wide_type (type, lo, hi);
8110 }
8111
8112
8113 /* Subroutine of native_interpret_expr.  Interpret the contents of
8114    the buffer PTR of length LEN as a REAL_CST of type TYPE.
8115    If the buffer cannot be interpreted, return NULL_TREE.  */
8116
8117 static tree
8118 native_interpret_real (tree type, const unsigned char *ptr, int len)
8119 {
8120   enum machine_mode mode = TYPE_MODE (type);
8121   int total_bytes = GET_MODE_SIZE (mode);
8122   int byte, offset, word, words, bitpos;
8123   unsigned char value;
8124   /* There are always 32 bits in each long, no matter the size of
8125      the hosts long.  We handle floating point representations with
8126      up to 192 bits.  */
8127   REAL_VALUE_TYPE r;
8128   long tmp[6];
8129
8130   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
8131   if (total_bytes > len || total_bytes > 24)
8132     return NULL_TREE;
8133   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8134
8135   memset (tmp, 0, sizeof (tmp));
8136   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8137        bitpos += BITS_PER_UNIT)
8138     {
8139       byte = (bitpos / BITS_PER_UNIT) & 3;
8140       if (UNITS_PER_WORD < 4)
8141         {
8142           word = byte / UNITS_PER_WORD;
8143           if (WORDS_BIG_ENDIAN)
8144             word = (words - 1) - word;
8145           offset = word * UNITS_PER_WORD;
8146           if (BYTES_BIG_ENDIAN)
8147             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8148           else
8149             offset += byte % UNITS_PER_WORD;
8150         }
8151       else
8152         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
8153       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8154
8155       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8156     }
8157
8158   real_from_target (&r, tmp, mode);
8159   return build_real (type, r);
8160 }
8161
8162
8163 /* Subroutine of native_interpret_expr.  Interpret the contents of
8164    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8165    If the buffer cannot be interpreted, return NULL_TREE.  */
8166
8167 static tree
8168 native_interpret_complex (tree type, const unsigned char *ptr, int len)
8169 {
8170   tree etype, rpart, ipart;
8171   int size;
8172
8173   etype = TREE_TYPE (type);
8174   size = GET_MODE_SIZE (TYPE_MODE (etype));
8175   if (size * 2 > len)
8176     return NULL_TREE;
8177   rpart = native_interpret_expr (etype, ptr, size);
8178   if (!rpart)
8179     return NULL_TREE;
8180   ipart = native_interpret_expr (etype, ptr+size, size);
8181   if (!ipart)
8182     return NULL_TREE;
8183   return build_complex (type, rpart, ipart);
8184 }
8185
8186
8187 /* Subroutine of native_interpret_expr.  Interpret the contents of
8188    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8189    If the buffer cannot be interpreted, return NULL_TREE.  */
8190
8191 static tree
8192 native_interpret_vector (tree type, const unsigned char *ptr, int len)
8193 {
8194   tree etype, elem, elements;
8195   int i, size, count;
8196
8197   etype = TREE_TYPE (type);
8198   size = GET_MODE_SIZE (TYPE_MODE (etype));
8199   count = TYPE_VECTOR_SUBPARTS (type);
8200   if (size * count > len)
8201     return NULL_TREE;
8202
8203   elements = NULL_TREE;
8204   for (i = count - 1; i >= 0; i--)
8205     {
8206       elem = native_interpret_expr (etype, ptr+(i*size), size);
8207       if (!elem)
8208         return NULL_TREE;
8209       elements = tree_cons (NULL_TREE, elem, elements);
8210     }
8211   return build_vector (type, elements);
8212 }
8213
8214
8215 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
8216    the buffer PTR of length LEN as a constant of type TYPE.  For
8217    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8218    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
8219    return NULL_TREE.  */
8220
8221 tree
8222 native_interpret_expr (tree type, const unsigned char *ptr, int len)
8223 {
8224   switch (TREE_CODE (type))
8225     {
8226     case INTEGER_TYPE:
8227     case ENUMERAL_TYPE:
8228     case BOOLEAN_TYPE:
8229       return native_interpret_int (type, ptr, len);
8230
8231     case REAL_TYPE:
8232       return native_interpret_real (type, ptr, len);
8233
8234     case COMPLEX_TYPE:
8235       return native_interpret_complex (type, ptr, len);
8236
8237     case VECTOR_TYPE:
8238       return native_interpret_vector (type, ptr, len);
8239
8240     default:
8241       return NULL_TREE;
8242     }
8243 }
8244
8245
8246 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
8247    TYPE at compile-time.  If we're unable to perform the conversion
8248    return NULL_TREE.  */
8249
8250 static tree
8251 fold_view_convert_expr (tree type, tree expr)
8252 {
8253   /* We support up to 512-bit values (for V8DFmode).  */
8254   unsigned char buffer[64];
8255   int len;
8256
8257   /* Check that the host and target are sane.  */
8258   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
8259     return NULL_TREE;
8260
8261   len = native_encode_expr (expr, buffer, sizeof (buffer));
8262   if (len == 0)
8263     return NULL_TREE;
8264
8265   return native_interpret_expr (type, buffer, len);
8266 }
8267
8268 /* Build an expression for the address of T.  Folds away INDIRECT_REF
8269    to avoid confusing the gimplify process.  */
8270
8271 tree
8272 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
8273 {
8274   /* The size of the object is not relevant when talking about its address.  */
8275   if (TREE_CODE (t) == WITH_SIZE_EXPR)
8276     t = TREE_OPERAND (t, 0);
8277
8278   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
8279   if (TREE_CODE (t) == INDIRECT_REF
8280       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
8281     {
8282       t = TREE_OPERAND (t, 0);
8283
8284       if (TREE_TYPE (t) != ptrtype)
8285         {
8286           t = build1 (NOP_EXPR, ptrtype, t);
8287           SET_EXPR_LOCATION (t, loc);
8288         }
8289     }
8290   else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
8291     {
8292       t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
8293
8294       if (TREE_TYPE (t) != ptrtype)
8295         t = fold_convert_loc (loc, ptrtype, t);
8296     }
8297   else
8298     {
8299       t = build1 (ADDR_EXPR, ptrtype, t);
8300       SET_EXPR_LOCATION (t, loc);
8301     }
8302
8303   return t;
8304 }
8305
8306 /* Build an expression for the address of T.  */
8307
8308 tree
8309 build_fold_addr_expr_loc (location_t loc, tree t)
8310 {
8311   tree ptrtype = build_pointer_type (TREE_TYPE (t));
8312
8313   return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
8314 }
8315
8316 /* Fold a unary expression of code CODE and type TYPE with operand
8317    OP0.  Return the folded expression if folding is successful.
8318    Otherwise, return NULL_TREE.  */
8319
8320 tree
8321 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
8322 {
8323   tree tem;
8324   tree arg0;
8325   enum tree_code_class kind = TREE_CODE_CLASS (code);
8326
8327   gcc_assert (IS_EXPR_CODE_CLASS (kind)
8328               && TREE_CODE_LENGTH (code) == 1);
8329
8330   arg0 = op0;
8331   if (arg0)
8332     {
8333       if (CONVERT_EXPR_CODE_P (code)
8334           || code == FLOAT_EXPR || code == ABS_EXPR)
8335         {
8336           /* Don't use STRIP_NOPS, because signedness of argument type
8337              matters.  */
8338           STRIP_SIGN_NOPS (arg0);
8339         }
8340       else
8341         {
8342           /* Strip any conversions that don't change the mode.  This
8343              is safe for every expression, except for a comparison
8344              expression because its signedness is derived from its
8345              operands.
8346
8347              Note that this is done as an internal manipulation within
8348              the constant folder, in order to find the simplest
8349              representation of the arguments so that their form can be
8350              studied.  In any cases, the appropriate type conversions
8351              should be put back in the tree that will get out of the
8352              constant folder.  */
8353           STRIP_NOPS (arg0);
8354         }
8355     }
8356
8357   if (TREE_CODE_CLASS (code) == tcc_unary)
8358     {
8359       if (TREE_CODE (arg0) == COMPOUND_EXPR)
8360         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
8361                        fold_build1_loc (loc, code, type,
8362                                     fold_convert_loc (loc, TREE_TYPE (op0),
8363                                                       TREE_OPERAND (arg0, 1))));
8364       else if (TREE_CODE (arg0) == COND_EXPR)
8365         {
8366           tree arg01 = TREE_OPERAND (arg0, 1);
8367           tree arg02 = TREE_OPERAND (arg0, 2);
8368           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8369             arg01 = fold_build1_loc (loc, code, type,
8370                                  fold_convert_loc (loc,
8371                                                    TREE_TYPE (op0), arg01));
8372           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8373             arg02 = fold_build1_loc (loc, code, type,
8374                                  fold_convert_loc (loc,
8375                                                    TREE_TYPE (op0), arg02));
8376           tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
8377                              arg01, arg02);
8378
8379           /* If this was a conversion, and all we did was to move into
8380              inside the COND_EXPR, bring it back out.  But leave it if
8381              it is a conversion from integer to integer and the
8382              result precision is no wider than a word since such a
8383              conversion is cheap and may be optimized away by combine,
8384              while it couldn't if it were outside the COND_EXPR.  Then return
8385              so we don't get into an infinite recursion loop taking the
8386              conversion out and then back in.  */
8387
8388           if ((CONVERT_EXPR_CODE_P (code)
8389                || code == NON_LVALUE_EXPR)
8390               && TREE_CODE (tem) == COND_EXPR
8391               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8392               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8393               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8394               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8395               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8396                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8397               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8398                      && (INTEGRAL_TYPE_P
8399                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8400                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8401                   || flag_syntax_only))
8402             {
8403               tem = build1 (code, type,
8404                             build3 (COND_EXPR,
8405                                     TREE_TYPE (TREE_OPERAND
8406                                                (TREE_OPERAND (tem, 1), 0)),
8407                                     TREE_OPERAND (tem, 0),
8408                                     TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8409                                     TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8410               SET_EXPR_LOCATION (tem, loc);
8411             }
8412           return tem;
8413         }
8414       else if (COMPARISON_CLASS_P (arg0))
8415         {
8416           if (TREE_CODE (type) == BOOLEAN_TYPE)
8417             {
8418               arg0 = copy_node (arg0);
8419               TREE_TYPE (arg0) = type;
8420               return arg0;
8421             }
8422           else if (TREE_CODE (type) != INTEGER_TYPE)
8423             return fold_build3_loc (loc, COND_EXPR, type, arg0,
8424                                 fold_build1_loc (loc, code, type,
8425                                              integer_one_node),
8426                                 fold_build1_loc (loc, code, type,
8427                                              integer_zero_node));
8428         }
8429    }
8430
8431   switch (code)
8432     {
8433     case PAREN_EXPR:
8434       /* Re-association barriers around constants and other re-association
8435          barriers can be removed.  */
8436       if (CONSTANT_CLASS_P (op0)
8437           || TREE_CODE (op0) == PAREN_EXPR)
8438         return fold_convert_loc (loc, type, op0);
8439       return NULL_TREE;
8440
8441     CASE_CONVERT:
8442     case FLOAT_EXPR:
8443     case FIX_TRUNC_EXPR:
8444       if (TREE_TYPE (op0) == type)
8445         return op0;
8446
8447       /* If we have (type) (a CMP b) and type is an integral type, return
8448          new expression involving the new type.  */
8449       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8450         return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8451                             TREE_OPERAND (op0, 1));
8452
8453       /* Handle cases of two conversions in a row.  */
8454       if (CONVERT_EXPR_P (op0))
8455         {
8456           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8457           tree inter_type = TREE_TYPE (op0);
8458           int inside_int = INTEGRAL_TYPE_P (inside_type);
8459           int inside_ptr = POINTER_TYPE_P (inside_type);
8460           int inside_float = FLOAT_TYPE_P (inside_type);
8461           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8462           unsigned int inside_prec = TYPE_PRECISION (inside_type);
8463           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8464           int inter_int = INTEGRAL_TYPE_P (inter_type);
8465           int inter_ptr = POINTER_TYPE_P (inter_type);
8466           int inter_float = FLOAT_TYPE_P (inter_type);
8467           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8468           unsigned int inter_prec = TYPE_PRECISION (inter_type);
8469           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8470           int final_int = INTEGRAL_TYPE_P (type);
8471           int final_ptr = POINTER_TYPE_P (type);
8472           int final_float = FLOAT_TYPE_P (type);
8473           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8474           unsigned int final_prec = TYPE_PRECISION (type);
8475           int final_unsignedp = TYPE_UNSIGNED (type);
8476
8477           /* In addition to the cases of two conversions in a row
8478              handled below, if we are converting something to its own
8479              type via an object of identical or wider precision, neither
8480              conversion is needed.  */
8481           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8482               && (((inter_int || inter_ptr) && final_int)
8483                   || (inter_float && final_float))
8484               && inter_prec >= final_prec)
8485             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8486
8487           /* Likewise, if the intermediate and initial types are either both
8488              float or both integer, we don't need the middle conversion if the
8489              former is wider than the latter and doesn't change the signedness
8490              (for integers).  Avoid this if the final type is a pointer since
8491              then we sometimes need the middle conversion.  Likewise if the
8492              final type has a precision not equal to the size of its mode.  */
8493           if (((inter_int && inside_int)
8494                || (inter_float && inside_float)
8495                || (inter_vec && inside_vec))
8496               && inter_prec >= inside_prec
8497               && (inter_float || inter_vec
8498                   || inter_unsignedp == inside_unsignedp)
8499               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8500                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
8501               && ! final_ptr
8502               && (! final_vec || inter_prec == inside_prec))
8503             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8504
8505           /* If we have a sign-extension of a zero-extended value, we can
8506              replace that by a single zero-extension.  */
8507           if (inside_int && inter_int && final_int
8508               && inside_prec < inter_prec && inter_prec < final_prec
8509               && inside_unsignedp && !inter_unsignedp)
8510             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8511
8512           /* Two conversions in a row are not needed unless:
8513              - some conversion is floating-point (overstrict for now), or
8514              - some conversion is a vector (overstrict for now), or
8515              - the intermediate type is narrower than both initial and
8516                final, or
8517              - the intermediate type and innermost type differ in signedness,
8518                and the outermost type is wider than the intermediate, or
8519              - the initial type is a pointer type and the precisions of the
8520                intermediate and final types differ, or
8521              - the final type is a pointer type and the precisions of the
8522                initial and intermediate types differ.  */
8523           if (! inside_float && ! inter_float && ! final_float
8524               && ! inside_vec && ! inter_vec && ! final_vec
8525               && (inter_prec >= inside_prec || inter_prec >= final_prec)
8526               && ! (inside_int && inter_int
8527                     && inter_unsignedp != inside_unsignedp
8528                     && inter_prec < final_prec)
8529               && ((inter_unsignedp && inter_prec > inside_prec)
8530                   == (final_unsignedp && final_prec > inter_prec))
8531               && ! (inside_ptr && inter_prec != final_prec)
8532               && ! (final_ptr && inside_prec != inter_prec)
8533               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8534                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
8535             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8536         }
8537
8538       /* Handle (T *)&A.B.C for A being of type T and B and C
8539          living at offset zero.  This occurs frequently in
8540          C++ upcasting and then accessing the base.  */
8541       if (TREE_CODE (op0) == ADDR_EXPR
8542           && POINTER_TYPE_P (type)
8543           && handled_component_p (TREE_OPERAND (op0, 0)))
8544         {
8545           HOST_WIDE_INT bitsize, bitpos;
8546           tree offset;
8547           enum machine_mode mode;
8548           int unsignedp, volatilep;
8549           tree base = TREE_OPERAND (op0, 0);
8550           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8551                                       &mode, &unsignedp, &volatilep, false);
8552           /* If the reference was to a (constant) zero offset, we can use
8553              the address of the base if it has the same base type
8554              as the result type.  */
8555           if (! offset && bitpos == 0
8556               && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8557                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8558             return fold_convert_loc (loc, type,
8559                                      build_fold_addr_expr_loc (loc, base));
8560         }
8561
8562       if (TREE_CODE (op0) == MODIFY_EXPR
8563           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
8564           /* Detect assigning a bitfield.  */
8565           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
8566                && DECL_BIT_FIELD
8567                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
8568         {
8569           /* Don't leave an assignment inside a conversion
8570              unless assigning a bitfield.  */
8571           tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
8572           /* First do the assignment, then return converted constant.  */
8573           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8574           TREE_NO_WARNING (tem) = 1;
8575           TREE_USED (tem) = 1;
8576           SET_EXPR_LOCATION (tem, loc);
8577           return tem;
8578         }
8579
8580       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8581          constants (if x has signed type, the sign bit cannot be set
8582          in c).  This folds extension into the BIT_AND_EXPR.
8583          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8584          very likely don't have maximal range for their precision and this
8585          transformation effectively doesn't preserve non-maximal ranges.  */
8586       if (TREE_CODE (type) == INTEGER_TYPE
8587           && TREE_CODE (op0) == BIT_AND_EXPR
8588           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
8589         {
8590           tree and_expr = op0;
8591           tree and0 = TREE_OPERAND (and_expr, 0);
8592           tree and1 = TREE_OPERAND (and_expr, 1);
8593           int change = 0;
8594
8595           if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
8596               || (TYPE_PRECISION (type)
8597                   <= TYPE_PRECISION (TREE_TYPE (and_expr))))
8598             change = 1;
8599           else if (TYPE_PRECISION (TREE_TYPE (and1))
8600                    <= HOST_BITS_PER_WIDE_INT
8601                    && host_integerp (and1, 1))
8602             {
8603               unsigned HOST_WIDE_INT cst;
8604
8605               cst = tree_low_cst (and1, 1);
8606               cst &= (HOST_WIDE_INT) -1
8607                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8608               change = (cst == 0);
8609 #ifdef LOAD_EXTEND_OP
8610               if (change
8611                   && !flag_syntax_only
8612                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8613                       == ZERO_EXTEND))
8614                 {
8615                   tree uns = unsigned_type_for (TREE_TYPE (and0));
8616                   and0 = fold_convert_loc (loc, uns, and0);
8617                   and1 = fold_convert_loc (loc, uns, and1);
8618                 }
8619 #endif
8620             }
8621           if (change)
8622             {
8623               tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8624                                            TREE_INT_CST_HIGH (and1), 0,
8625                                            TREE_OVERFLOW (and1));
8626               return fold_build2_loc (loc, BIT_AND_EXPR, type,
8627                                   fold_convert_loc (loc, type, and0), tem);
8628             }
8629         }
8630
8631       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8632          when one of the new casts will fold away. Conservatively we assume
8633          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8634       if (POINTER_TYPE_P (type)
8635           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8636           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8637               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8638               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8639         {
8640           tree arg00 = TREE_OPERAND (arg0, 0);
8641           tree arg01 = TREE_OPERAND (arg0, 1);
8642
8643           return fold_build2_loc (loc,
8644                               TREE_CODE (arg0), type,
8645                               fold_convert_loc (loc, type, arg00),
8646                               fold_convert_loc (loc, sizetype, arg01));
8647         }
8648
8649       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8650          of the same precision, and X is an integer type not narrower than
8651          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
8652       if (INTEGRAL_TYPE_P (type)
8653           && TREE_CODE (op0) == BIT_NOT_EXPR
8654           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8655           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8656           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8657         {
8658           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8659           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8660               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8661             return fold_build1_loc (loc, BIT_NOT_EXPR, type,
8662                                 fold_convert_loc (loc, type, tem));
8663         }
8664
8665       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8666          type of X and Y (integer types only).  */
8667       if (INTEGRAL_TYPE_P (type)
8668           && TREE_CODE (op0) == MULT_EXPR
8669           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8670           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8671         {
8672           /* Be careful not to introduce new overflows.  */
8673           tree mult_type;
8674           if (TYPE_OVERFLOW_WRAPS (type))
8675             mult_type = type;
8676           else
8677             mult_type = unsigned_type_for (type);
8678
8679           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8680             {
8681               tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8682                                  fold_convert_loc (loc, mult_type,
8683                                                    TREE_OPERAND (op0, 0)),
8684                                  fold_convert_loc (loc, mult_type,
8685                                                    TREE_OPERAND (op0, 1)));
8686               return fold_convert_loc (loc, type, tem);
8687             }
8688         }
8689
8690       tem = fold_convert_const (code, type, op0);
8691       return tem ? tem : NULL_TREE;
8692
8693     case ADDR_SPACE_CONVERT_EXPR:
8694       if (integer_zerop (arg0))
8695         return fold_convert_const (code, type, arg0);
8696       return NULL_TREE;
8697
8698     case FIXED_CONVERT_EXPR:
8699       tem = fold_convert_const (code, type, arg0);
8700       return tem ? tem : NULL_TREE;
8701
8702     case VIEW_CONVERT_EXPR:
8703       if (TREE_TYPE (op0) == type)
8704         return op0;
8705       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8706         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8707                             type, TREE_OPERAND (op0, 0));
8708
8709       /* For integral conversions with the same precision or pointer
8710          conversions use a NOP_EXPR instead.  */
8711       if ((INTEGRAL_TYPE_P (type)
8712            || POINTER_TYPE_P (type))
8713           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8714               || POINTER_TYPE_P (TREE_TYPE (op0)))
8715           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8716         return fold_convert_loc (loc, type, op0);
8717
8718       /* Strip inner integral conversions that do not change the precision.  */
8719       if (CONVERT_EXPR_P (op0)
8720           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8721               || POINTER_TYPE_P (TREE_TYPE (op0)))
8722           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8723               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8724           && (TYPE_PRECISION (TREE_TYPE (op0))
8725               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8726         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8727                             type, TREE_OPERAND (op0, 0));
8728
8729       return fold_view_convert_expr (type, op0);
8730
8731     case NEGATE_EXPR:
8732       tem = fold_negate_expr (loc, arg0);
8733       if (tem)
8734         return fold_convert_loc (loc, type, tem);
8735       return NULL_TREE;
8736
8737     case ABS_EXPR:
8738       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8739         return fold_abs_const (arg0, type);
8740       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8741         return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8742       /* Convert fabs((double)float) into (double)fabsf(float).  */
8743       else if (TREE_CODE (arg0) == NOP_EXPR
8744                && TREE_CODE (type) == REAL_TYPE)
8745         {
8746           tree targ0 = strip_float_extensions (arg0);
8747           if (targ0 != arg0)
8748             return fold_convert_loc (loc, type,
8749                                      fold_build1_loc (loc, ABS_EXPR,
8750                                                   TREE_TYPE (targ0),
8751                                                   targ0));
8752         }
8753       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8754       else if (TREE_CODE (arg0) == ABS_EXPR)
8755         return arg0;
8756       else if (tree_expr_nonnegative_p (arg0))
8757         return arg0;
8758
8759       /* Strip sign ops from argument.  */
8760       if (TREE_CODE (type) == REAL_TYPE)
8761         {
8762           tem = fold_strip_sign_ops (arg0);
8763           if (tem)
8764             return fold_build1_loc (loc, ABS_EXPR, type,
8765                                 fold_convert_loc (loc, type, tem));
8766         }
8767       return NULL_TREE;
8768
8769     case CONJ_EXPR:
8770       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8771         return fold_convert_loc (loc, type, arg0);
8772       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8773         {
8774           tree itype = TREE_TYPE (type);
8775           tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8776           tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8777           return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8778                               negate_expr (ipart));
8779         }
8780       if (TREE_CODE (arg0) == COMPLEX_CST)
8781         {
8782           tree itype = TREE_TYPE (type);
8783           tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8784           tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8785           return build_complex (type, rpart, negate_expr (ipart));
8786         }
8787       if (TREE_CODE (arg0) == CONJ_EXPR)
8788         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8789       return NULL_TREE;
8790
8791     case BIT_NOT_EXPR:
8792       if (TREE_CODE (arg0) == INTEGER_CST)
8793         return fold_not_const (arg0, type);
8794       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8795         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8796       /* Convert ~ (-A) to A - 1.  */
8797       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8798         return fold_build2_loc (loc, MINUS_EXPR, type,
8799                             fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8800                             build_int_cst (type, 1));
8801       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8802       else if (INTEGRAL_TYPE_P (type)
8803                && ((TREE_CODE (arg0) == MINUS_EXPR
8804                     && integer_onep (TREE_OPERAND (arg0, 1)))
8805                    || (TREE_CODE (arg0) == PLUS_EXPR
8806                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8807         return fold_build1_loc (loc, NEGATE_EXPR, type,
8808                             fold_convert_loc (loc, type,
8809                                               TREE_OPERAND (arg0, 0)));
8810       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8811       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8812                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8813                                      fold_convert_loc (loc, type,
8814                                                        TREE_OPERAND (arg0, 0)))))
8815         return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8816                             fold_convert_loc (loc, type,
8817                                               TREE_OPERAND (arg0, 1)));
8818       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8819                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8820                                      fold_convert_loc (loc, type,
8821                                                        TREE_OPERAND (arg0, 1)))))
8822         return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8823                             fold_convert_loc (loc, type,
8824                                               TREE_OPERAND (arg0, 0)), tem);
8825       /* Perform BIT_NOT_EXPR on each element individually.  */
8826       else if (TREE_CODE (arg0) == VECTOR_CST)
8827         {
8828           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8829           int count = TYPE_VECTOR_SUBPARTS (type), i;
8830
8831           for (i = 0; i < count; i++)
8832             {
8833               if (elements)
8834                 {
8835                   elem = TREE_VALUE (elements);
8836                   elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8837                   if (elem == NULL_TREE)
8838                     break;
8839                   elements = TREE_CHAIN (elements);
8840                 }
8841               else
8842                 elem = build_int_cst (TREE_TYPE (type), -1);
8843               list = tree_cons (NULL_TREE, elem, list);
8844             }
8845           if (i == count)
8846             return build_vector (type, nreverse (list));
8847         }
8848
8849       return NULL_TREE;
8850
8851     case TRUTH_NOT_EXPR:
8852       /* The argument to invert_truthvalue must have Boolean type.  */
8853       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8854           arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8855
8856       /* Note that the operand of this must be an int
8857          and its values must be 0 or 1.
8858          ("true" is a fixed value perhaps depending on the language,
8859          but we don't handle values other than 1 correctly yet.)  */
8860       tem = fold_truth_not_expr (loc, arg0);
8861       if (!tem)
8862         return NULL_TREE;
8863       return fold_convert_loc (loc, type, tem);
8864
8865     case REALPART_EXPR:
8866       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8867         return fold_convert_loc (loc, type, arg0);
8868       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8869         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8870                                  TREE_OPERAND (arg0, 1));
8871       if (TREE_CODE (arg0) == COMPLEX_CST)
8872         return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8873       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8874         {
8875           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8876           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8877                              fold_build1_loc (loc, REALPART_EXPR, itype,
8878                                           TREE_OPERAND (arg0, 0)),
8879                              fold_build1_loc (loc, REALPART_EXPR, itype,
8880                                           TREE_OPERAND (arg0, 1)));
8881           return fold_convert_loc (loc, type, tem);
8882         }
8883       if (TREE_CODE (arg0) == CONJ_EXPR)
8884         {
8885           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8886           tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8887                              TREE_OPERAND (arg0, 0));
8888           return fold_convert_loc (loc, type, tem);
8889         }
8890       if (TREE_CODE (arg0) == CALL_EXPR)
8891         {
8892           tree fn = get_callee_fndecl (arg0);
8893           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8894             switch (DECL_FUNCTION_CODE (fn))
8895               {
8896               CASE_FLT_FN (BUILT_IN_CEXPI):
8897                 fn = mathfn_built_in (type, BUILT_IN_COS);
8898                 if (fn)
8899                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8900                 break;
8901
8902               default:
8903                 break;
8904               }
8905         }
8906       return NULL_TREE;
8907
8908     case IMAGPART_EXPR:
8909       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8910         return fold_convert_loc (loc, type, integer_zero_node);
8911       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8912         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8913                                  TREE_OPERAND (arg0, 0));
8914       if (TREE_CODE (arg0) == COMPLEX_CST)
8915         return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8916       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8917         {
8918           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8919           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8920                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8921                                           TREE_OPERAND (arg0, 0)),
8922                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8923                                           TREE_OPERAND (arg0, 1)));
8924           return fold_convert_loc (loc, type, tem);
8925         }
8926       if (TREE_CODE (arg0) == CONJ_EXPR)
8927         {
8928           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8929           tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8930           return fold_convert_loc (loc, type, negate_expr (tem));
8931         }
8932       if (TREE_CODE (arg0) == CALL_EXPR)
8933         {
8934           tree fn = get_callee_fndecl (arg0);
8935           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8936             switch (DECL_FUNCTION_CODE (fn))
8937               {
8938               CASE_FLT_FN (BUILT_IN_CEXPI):
8939                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8940                 if (fn)
8941                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8942                 break;
8943
8944               default:
8945                 break;
8946               }
8947         }
8948       return NULL_TREE;
8949
8950     default:
8951       return NULL_TREE;
8952     } /* switch (code) */
8953 }
8954
8955
8956 /* If the operation was a conversion do _not_ mark a resulting constant
8957    with TREE_OVERFLOW if the original constant was not.  These conversions
8958    have implementation defined behavior and retaining the TREE_OVERFLOW
8959    flag here would confuse later passes such as VRP.  */
8960 tree
8961 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8962                                 tree type, tree op0)
8963 {
8964   tree res = fold_unary_loc (loc, code, type, op0);
8965   if (res
8966       && TREE_CODE (res) == INTEGER_CST
8967       && TREE_CODE (op0) == INTEGER_CST
8968       && CONVERT_EXPR_CODE_P (code))
8969     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8970
8971   return res;
8972 }
8973
8974 /* Fold a binary expression of code CODE and type TYPE with operands
8975    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8976    Return the folded expression if folding is successful.  Otherwise,
8977    return NULL_TREE.  */
8978
8979 static tree
8980 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8981 {
8982   enum tree_code compl_code;
8983
8984   if (code == MIN_EXPR)
8985     compl_code = MAX_EXPR;
8986   else if (code == MAX_EXPR)
8987     compl_code = MIN_EXPR;
8988   else
8989     gcc_unreachable ();
8990
8991   /* MIN (MAX (a, b), b) == b.  */
8992   if (TREE_CODE (op0) == compl_code
8993       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8994     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8995
8996   /* MIN (MAX (b, a), b) == b.  */
8997   if (TREE_CODE (op0) == compl_code
8998       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8999       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
9000     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
9001
9002   /* MIN (a, MAX (a, b)) == a.  */
9003   if (TREE_CODE (op1) == compl_code
9004       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
9005       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
9006     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
9007
9008   /* MIN (a, MAX (b, a)) == a.  */
9009   if (TREE_CODE (op1) == compl_code
9010       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
9011       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
9012     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
9013
9014   return NULL_TREE;
9015 }
9016
9017 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9018    by changing CODE to reduce the magnitude of constants involved in
9019    ARG0 of the comparison.
9020    Returns a canonicalized comparison tree if a simplification was
9021    possible, otherwise returns NULL_TREE.
9022    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9023    valid if signed overflow is undefined.  */
9024
9025 static tree
9026 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9027                                  tree arg0, tree arg1,
9028                                  bool *strict_overflow_p)
9029 {
9030   enum tree_code code0 = TREE_CODE (arg0);
9031   tree t, cst0 = NULL_TREE;
9032   int sgn0;
9033   bool swap = false;
9034
9035   /* Match A +- CST code arg1 and CST code arg1.  We can change the
9036      first form only if overflow is undefined.  */
9037   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9038          /* In principle pointers also have undefined overflow behavior,
9039             but that causes problems elsewhere.  */
9040          && !POINTER_TYPE_P (TREE_TYPE (arg0))
9041          && (code0 == MINUS_EXPR
9042              || code0 == PLUS_EXPR)
9043          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9044         || code0 == INTEGER_CST))
9045     return NULL_TREE;
9046
9047   /* Identify the constant in arg0 and its sign.  */
9048   if (code0 == INTEGER_CST)
9049     cst0 = arg0;
9050   else
9051     cst0 = TREE_OPERAND (arg0, 1);
9052   sgn0 = tree_int_cst_sgn (cst0);
9053
9054   /* Overflowed constants and zero will cause problems.  */
9055   if (integer_zerop (cst0)
9056       || TREE_OVERFLOW (cst0))
9057     return NULL_TREE;
9058
9059   /* See if we can reduce the magnitude of the constant in
9060      arg0 by changing the comparison code.  */
9061   if (code0 == INTEGER_CST)
9062     {
9063       /* CST <= arg1  ->  CST-1 < arg1.  */
9064       if (code == LE_EXPR && sgn0 == 1)
9065         code = LT_EXPR;
9066       /* -CST < arg1  ->  -CST-1 <= arg1.  */
9067       else if (code == LT_EXPR && sgn0 == -1)
9068         code = LE_EXPR;
9069       /* CST > arg1  ->  CST-1 >= arg1.  */
9070       else if (code == GT_EXPR && sgn0 == 1)
9071         code = GE_EXPR;
9072       /* -CST >= arg1  ->  -CST-1 > arg1.  */
9073       else if (code == GE_EXPR && sgn0 == -1)
9074         code = GT_EXPR;
9075       else
9076         return NULL_TREE;
9077       /* arg1 code' CST' might be more canonical.  */
9078       swap = true;
9079     }
9080   else
9081     {
9082       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
9083       if (code == LT_EXPR
9084           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9085         code = LE_EXPR;
9086       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
9087       else if (code == GT_EXPR
9088                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9089         code = GE_EXPR;
9090       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
9091       else if (code == LE_EXPR
9092                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9093         code = LT_EXPR;
9094       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
9095       else if (code == GE_EXPR
9096                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9097         code = GT_EXPR;
9098       else
9099         return NULL_TREE;
9100       *strict_overflow_p = true;
9101     }
9102
9103   /* Now build the constant reduced in magnitude.  But not if that
9104      would produce one outside of its types range.  */
9105   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9106       && ((sgn0 == 1
9107            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9108            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9109           || (sgn0 == -1
9110               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9111               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9112     /* We cannot swap the comparison here as that would cause us to
9113        endlessly recurse.  */
9114     return NULL_TREE;
9115
9116   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9117                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
9118   if (code0 != INTEGER_CST)
9119     t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9120
9121   /* If swapping might yield to a more canonical form, do so.  */
9122   if (swap)
9123     return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
9124   else
9125     return fold_build2_loc (loc, code, type, t, arg1);
9126 }
9127
9128 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9129    overflow further.  Try to decrease the magnitude of constants involved
9130    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9131    and put sole constants at the second argument position.
9132    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
9133
9134 static tree
9135 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9136                                tree arg0, tree arg1)
9137 {
9138   tree t;
9139   bool strict_overflow_p;
9140   const char * const warnmsg = G_("assuming signed overflow does not occur "
9141                                   "when reducing constant in comparison");
9142
9143   /* Try canonicalization by simplifying arg0.  */
9144   strict_overflow_p = false;
9145   t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
9146                                        &strict_overflow_p);
9147   if (t)
9148     {
9149       if (strict_overflow_p)
9150         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9151       return t;
9152     }
9153
9154   /* Try canonicalization by simplifying arg1 using the swapped
9155      comparison.  */
9156   code = swap_tree_comparison (code);
9157   strict_overflow_p = false;
9158   t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
9159                                        &strict_overflow_p);
9160   if (t && strict_overflow_p)
9161     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9162   return t;
9163 }
9164
9165 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
9166    space.  This is used to avoid issuing overflow warnings for
9167    expressions like &p->x which can not wrap.  */
9168
9169 static bool
9170 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
9171 {
9172   unsigned HOST_WIDE_INT offset_low, total_low;
9173   HOST_WIDE_INT size, offset_high, total_high;
9174
9175   if (!POINTER_TYPE_P (TREE_TYPE (base)))
9176     return true;
9177
9178   if (bitpos < 0)
9179     return true;
9180
9181   if (offset == NULL_TREE)
9182     {
9183       offset_low = 0;
9184       offset_high = 0;
9185     }
9186   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
9187     return true;
9188   else
9189     {
9190       offset_low = TREE_INT_CST_LOW (offset);
9191       offset_high = TREE_INT_CST_HIGH (offset);
9192     }
9193
9194   if (add_double_with_sign (offset_low, offset_high,
9195                             bitpos / BITS_PER_UNIT, 0,
9196                             &total_low, &total_high,
9197                             true))
9198     return true;
9199
9200   if (total_high != 0)
9201     return true;
9202
9203   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
9204   if (size <= 0)
9205     return true;
9206
9207   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
9208      array.  */
9209   if (TREE_CODE (base) == ADDR_EXPR)
9210     {
9211       HOST_WIDE_INT base_size;
9212
9213       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
9214       if (base_size > 0 && size < base_size)
9215         size = base_size;
9216     }
9217
9218   return total_low > (unsigned HOST_WIDE_INT) size;
9219 }
9220
9221 /* Subroutine of fold_binary.  This routine performs all of the
9222    transformations that are common to the equality/inequality
9223    operators (EQ_EXPR and NE_EXPR) and the ordering operators
9224    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
9225    fold_binary should call fold_binary.  Fold a comparison with
9226    tree code CODE and type TYPE with operands OP0 and OP1.  Return
9227    the folded comparison or NULL_TREE.  */
9228
9229 static tree
9230 fold_comparison (location_t loc, enum tree_code code, tree type,
9231                  tree op0, tree op1)
9232 {
9233   tree arg0, arg1, tem;
9234
9235   arg0 = op0;
9236   arg1 = op1;
9237
9238   STRIP_SIGN_NOPS (arg0);
9239   STRIP_SIGN_NOPS (arg1);
9240
9241   tem = fold_relational_const (code, type, arg0, arg1);
9242   if (tem != NULL_TREE)
9243     return tem;
9244
9245   /* If one arg is a real or integer constant, put it last.  */
9246   if (tree_swap_operands_p (arg0, arg1, true))
9247     return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9248
9249   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
9250   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9251       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9252           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9253           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
9254       && (TREE_CODE (arg1) == INTEGER_CST
9255           && !TREE_OVERFLOW (arg1)))
9256     {
9257       tree const1 = TREE_OPERAND (arg0, 1);
9258       tree const2 = arg1;
9259       tree variable = TREE_OPERAND (arg0, 0);
9260       tree lhs;
9261       int lhs_add;
9262       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
9263
9264       lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
9265                          TREE_TYPE (arg1), const2, const1);
9266
9267       /* If the constant operation overflowed this can be
9268          simplified as a comparison against INT_MAX/INT_MIN.  */
9269       if (TREE_CODE (lhs) == INTEGER_CST
9270           && TREE_OVERFLOW (lhs))
9271         {
9272           int const1_sgn = tree_int_cst_sgn (const1);
9273           enum tree_code code2 = code;
9274
9275           /* Get the sign of the constant on the lhs if the
9276              operation were VARIABLE + CONST1.  */
9277           if (TREE_CODE (arg0) == MINUS_EXPR)
9278             const1_sgn = -const1_sgn;
9279
9280           /* The sign of the constant determines if we overflowed
9281              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
9282              Canonicalize to the INT_MIN overflow by swapping the comparison
9283              if necessary.  */
9284           if (const1_sgn == -1)
9285             code2 = swap_tree_comparison (code);
9286
9287           /* We now can look at the canonicalized case
9288                VARIABLE + 1  CODE2  INT_MIN
9289              and decide on the result.  */
9290           if (code2 == LT_EXPR
9291               || code2 == LE_EXPR
9292               || code2 == EQ_EXPR)
9293             return omit_one_operand_loc (loc, type, boolean_false_node, variable);
9294           else if (code2 == NE_EXPR
9295                    || code2 == GE_EXPR
9296                    || code2 == GT_EXPR)
9297             return omit_one_operand_loc (loc, type, boolean_true_node, variable);
9298         }
9299
9300       if (TREE_CODE (lhs) == TREE_CODE (arg1)
9301           && (TREE_CODE (lhs) != INTEGER_CST
9302               || !TREE_OVERFLOW (lhs)))
9303         {
9304           fold_overflow_warning (("assuming signed overflow does not occur "
9305                                   "when changing X +- C1 cmp C2 to "
9306                                   "X cmp C1 +- C2"),
9307                                  WARN_STRICT_OVERFLOW_COMPARISON);
9308           return fold_build2_loc (loc, code, type, variable, lhs);
9309         }
9310     }
9311
9312   /* For comparisons of pointers we can decompose it to a compile time
9313      comparison of the base objects and the offsets into the object.
9314      This requires at least one operand being an ADDR_EXPR or a
9315      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
9316   if (POINTER_TYPE_P (TREE_TYPE (arg0))
9317       && (TREE_CODE (arg0) == ADDR_EXPR
9318           || TREE_CODE (arg1) == ADDR_EXPR
9319           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9320           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9321     {
9322       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9323       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
9324       enum machine_mode mode;
9325       int volatilep, unsignedp;
9326       bool indirect_base0 = false, indirect_base1 = false;
9327
9328       /* Get base and offset for the access.  Strip ADDR_EXPR for
9329          get_inner_reference, but put it back by stripping INDIRECT_REF
9330          off the base object if possible.  indirect_baseN will be true
9331          if baseN is not an address but refers to the object itself.  */
9332       base0 = arg0;
9333       if (TREE_CODE (arg0) == ADDR_EXPR)
9334         {
9335           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
9336                                        &bitsize, &bitpos0, &offset0, &mode,
9337                                        &unsignedp, &volatilep, false);
9338           if (TREE_CODE (base0) == INDIRECT_REF)
9339             base0 = TREE_OPERAND (base0, 0);
9340           else
9341             indirect_base0 = true;
9342         }
9343       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9344         {
9345           base0 = TREE_OPERAND (arg0, 0);
9346           offset0 = TREE_OPERAND (arg0, 1);
9347         }
9348
9349       base1 = arg1;
9350       if (TREE_CODE (arg1) == ADDR_EXPR)
9351         {
9352           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
9353                                        &bitsize, &bitpos1, &offset1, &mode,
9354                                        &unsignedp, &volatilep, false);
9355           if (TREE_CODE (base1) == INDIRECT_REF)
9356             base1 = TREE_OPERAND (base1, 0);
9357           else
9358             indirect_base1 = true;
9359         }
9360       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9361         {
9362           base1 = TREE_OPERAND (arg1, 0);
9363           offset1 = TREE_OPERAND (arg1, 1);
9364         }
9365
9366       /* If we have equivalent bases we might be able to simplify.  */
9367       if (indirect_base0 == indirect_base1
9368           && operand_equal_p (base0, base1, 0))
9369         {
9370           /* We can fold this expression to a constant if the non-constant
9371              offset parts are equal.  */
9372           if ((offset0 == offset1
9373                || (offset0 && offset1
9374                    && operand_equal_p (offset0, offset1, 0)))
9375               && (code == EQ_EXPR
9376                   || code == NE_EXPR
9377                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
9378
9379             {
9380               if (code != EQ_EXPR
9381                   && code != NE_EXPR
9382                   && bitpos0 != bitpos1
9383                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9384                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9385                 fold_overflow_warning (("assuming pointer wraparound does not "
9386                                         "occur when comparing P +- C1 with "
9387                                         "P +- C2"),
9388                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
9389
9390               switch (code)
9391                 {
9392                 case EQ_EXPR:
9393                   return constant_boolean_node (bitpos0 == bitpos1, type);
9394                 case NE_EXPR:
9395                   return constant_boolean_node (bitpos0 != bitpos1, type);
9396                 case LT_EXPR:
9397                   return constant_boolean_node (bitpos0 < bitpos1, type);
9398                 case LE_EXPR:
9399                   return constant_boolean_node (bitpos0 <= bitpos1, type);
9400                 case GE_EXPR:
9401                   return constant_boolean_node (bitpos0 >= bitpos1, type);
9402                 case GT_EXPR:
9403                   return constant_boolean_node (bitpos0 > bitpos1, type);
9404                 default:;
9405                 }
9406             }
9407           /* We can simplify the comparison to a comparison of the variable
9408              offset parts if the constant offset parts are equal.
9409              Be careful to use signed size type here because otherwise we
9410              mess with array offsets in the wrong way.  This is possible
9411              because pointer arithmetic is restricted to retain within an
9412              object and overflow on pointer differences is undefined as of
9413              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
9414           else if (bitpos0 == bitpos1
9415                    && ((code == EQ_EXPR || code == NE_EXPR)
9416                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
9417             {
9418               tree signed_size_type_node;
9419               signed_size_type_node = signed_type_for (size_type_node);
9420
9421               /* By converting to signed size type we cover middle-end pointer
9422                  arithmetic which operates on unsigned pointer types of size
9423                  type size and ARRAY_REF offsets which are properly sign or
9424                  zero extended from their type in case it is narrower than
9425                  size type.  */
9426               if (offset0 == NULL_TREE)
9427                 offset0 = build_int_cst (signed_size_type_node, 0);
9428               else
9429                 offset0 = fold_convert_loc (loc, signed_size_type_node,
9430                                             offset0);
9431               if (offset1 == NULL_TREE)
9432                 offset1 = build_int_cst (signed_size_type_node, 0);
9433               else
9434                 offset1 = fold_convert_loc (loc, signed_size_type_node,
9435                                             offset1);
9436
9437               if (code != EQ_EXPR
9438                   && code != NE_EXPR
9439                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9440                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9441                 fold_overflow_warning (("assuming pointer wraparound does not "
9442                                         "occur when comparing P +- C1 with "
9443                                         "P +- C2"),
9444                                        WARN_STRICT_OVERFLOW_COMPARISON);
9445
9446               return fold_build2_loc (loc, code, type, offset0, offset1);
9447             }
9448         }
9449       /* For non-equal bases we can simplify if they are addresses
9450          of local binding decls or constants.  */
9451       else if (indirect_base0 && indirect_base1
9452                /* We know that !operand_equal_p (base0, base1, 0)
9453                   because the if condition was false.  But make
9454                   sure two decls are not the same.  */
9455                && base0 != base1
9456                && TREE_CODE (arg0) == ADDR_EXPR
9457                && TREE_CODE (arg1) == ADDR_EXPR
9458                && (((TREE_CODE (base0) == VAR_DECL
9459                      || TREE_CODE (base0) == PARM_DECL)
9460                     && (targetm.binds_local_p (base0)
9461                         || CONSTANT_CLASS_P (base1)))
9462                    || CONSTANT_CLASS_P (base0))
9463                && (((TREE_CODE (base1) == VAR_DECL
9464                      || TREE_CODE (base1) == PARM_DECL)
9465                     && (targetm.binds_local_p (base1)
9466                         || CONSTANT_CLASS_P (base0)))
9467                    || CONSTANT_CLASS_P (base1)))
9468         {
9469           if (code == EQ_EXPR)
9470             return omit_two_operands_loc (loc, type, boolean_false_node,
9471                                       arg0, arg1);
9472           else if (code == NE_EXPR)
9473             return omit_two_operands_loc (loc, type, boolean_true_node,
9474                                       arg0, arg1);
9475         }
9476       /* For equal offsets we can simplify to a comparison of the
9477          base addresses.  */
9478       else if (bitpos0 == bitpos1
9479                && (indirect_base0
9480                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9481                && (indirect_base1
9482                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9483                && ((offset0 == offset1)
9484                    || (offset0 && offset1
9485                        && operand_equal_p (offset0, offset1, 0))))
9486         {
9487           if (indirect_base0)
9488             base0 = build_fold_addr_expr_loc (loc, base0);
9489           if (indirect_base1)
9490             base1 = build_fold_addr_expr_loc (loc, base1);
9491           return fold_build2_loc (loc, code, type, base0, base1);
9492         }
9493     }
9494
9495   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9496      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
9497      the resulting offset is smaller in absolute value than the
9498      original one.  */
9499   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9500       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9501       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9502           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9503       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9504       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9505           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9506     {
9507       tree const1 = TREE_OPERAND (arg0, 1);
9508       tree const2 = TREE_OPERAND (arg1, 1);
9509       tree variable1 = TREE_OPERAND (arg0, 0);
9510       tree variable2 = TREE_OPERAND (arg1, 0);
9511       tree cst;
9512       const char * const warnmsg = G_("assuming signed overflow does not "
9513                                       "occur when combining constants around "
9514                                       "a comparison");
9515
9516       /* Put the constant on the side where it doesn't overflow and is
9517          of lower absolute value than before.  */
9518       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9519                              ? MINUS_EXPR : PLUS_EXPR,
9520                              const2, const1, 0);
9521       if (!TREE_OVERFLOW (cst)
9522           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9523         {
9524           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9525           return fold_build2_loc (loc, code, type,
9526                               variable1,
9527                               fold_build2_loc (loc,
9528                                            TREE_CODE (arg1), TREE_TYPE (arg1),
9529                                            variable2, cst));
9530         }
9531
9532       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9533                              ? MINUS_EXPR : PLUS_EXPR,
9534                              const1, const2, 0);
9535       if (!TREE_OVERFLOW (cst)
9536           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9537         {
9538           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9539           return fold_build2_loc (loc, code, type,
9540                               fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
9541                                            variable1, cst),
9542                               variable2);
9543         }
9544     }
9545
9546   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9547      signed arithmetic case.  That form is created by the compiler
9548      often enough for folding it to be of value.  One example is in
9549      computing loop trip counts after Operator Strength Reduction.  */
9550   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9551       && TREE_CODE (arg0) == MULT_EXPR
9552       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9553           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9554       && integer_zerop (arg1))
9555     {
9556       tree const1 = TREE_OPERAND (arg0, 1);
9557       tree const2 = arg1;                       /* zero */
9558       tree variable1 = TREE_OPERAND (arg0, 0);
9559       enum tree_code cmp_code = code;
9560
9561       gcc_assert (!integer_zerop (const1));
9562
9563       fold_overflow_warning (("assuming signed overflow does not occur when "
9564                               "eliminating multiplication in comparison "
9565                               "with zero"),
9566                              WARN_STRICT_OVERFLOW_COMPARISON);
9567
9568       /* If const1 is negative we swap the sense of the comparison.  */
9569       if (tree_int_cst_sgn (const1) < 0)
9570         cmp_code = swap_tree_comparison (cmp_code);
9571
9572       return fold_build2_loc (loc, cmp_code, type, variable1, const2);
9573     }
9574
9575   tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
9576   if (tem)
9577     return tem;
9578
9579   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9580     {
9581       tree targ0 = strip_float_extensions (arg0);
9582       tree targ1 = strip_float_extensions (arg1);
9583       tree newtype = TREE_TYPE (targ0);
9584
9585       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9586         newtype = TREE_TYPE (targ1);
9587
9588       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9589       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9590         return fold_build2_loc (loc, code, type,
9591                             fold_convert_loc (loc, newtype, targ0),
9592                             fold_convert_loc (loc, newtype, targ1));
9593
9594       /* (-a) CMP (-b) -> b CMP a  */
9595       if (TREE_CODE (arg0) == NEGATE_EXPR
9596           && TREE_CODE (arg1) == NEGATE_EXPR)
9597         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
9598                             TREE_OPERAND (arg0, 0));
9599
9600       if (TREE_CODE (arg1) == REAL_CST)
9601         {
9602           REAL_VALUE_TYPE cst;
9603           cst = TREE_REAL_CST (arg1);
9604
9605           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
9606           if (TREE_CODE (arg0) == NEGATE_EXPR)
9607             return fold_build2_loc (loc, swap_tree_comparison (code), type,
9608                                 TREE_OPERAND (arg0, 0),
9609                                 build_real (TREE_TYPE (arg1),
9610                                             REAL_VALUE_NEGATE (cst)));
9611
9612           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
9613           /* a CMP (-0) -> a CMP 0  */
9614           if (REAL_VALUE_MINUS_ZERO (cst))
9615             return fold_build2_loc (loc, code, type, arg0,
9616                                 build_real (TREE_TYPE (arg1), dconst0));
9617
9618           /* x != NaN is always true, other ops are always false.  */
9619           if (REAL_VALUE_ISNAN (cst)
9620               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9621             {
9622               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9623               return omit_one_operand_loc (loc, type, tem, arg0);
9624             }
9625
9626           /* Fold comparisons against infinity.  */
9627           if (REAL_VALUE_ISINF (cst)
9628               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9629             {
9630               tem = fold_inf_compare (loc, code, type, arg0, arg1);
9631               if (tem != NULL_TREE)
9632                 return tem;
9633             }
9634         }
9635
9636       /* If this is a comparison of a real constant with a PLUS_EXPR
9637          or a MINUS_EXPR of a real constant, we can convert it into a
9638          comparison with a revised real constant as long as no overflow
9639          occurs when unsafe_math_optimizations are enabled.  */
9640       if (flag_unsafe_math_optimizations
9641           && TREE_CODE (arg1) == REAL_CST
9642           && (TREE_CODE (arg0) == PLUS_EXPR
9643               || TREE_CODE (arg0) == MINUS_EXPR)
9644           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9645           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9646                                       ? MINUS_EXPR : PLUS_EXPR,
9647                                       arg1, TREE_OPERAND (arg0, 1), 0))
9648           && !TREE_OVERFLOW (tem))
9649         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9650
9651       /* Likewise, we can simplify a comparison of a real constant with
9652          a MINUS_EXPR whose first operand is also a real constant, i.e.
9653          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
9654          floating-point types only if -fassociative-math is set.  */
9655       if (flag_associative_math
9656           && TREE_CODE (arg1) == REAL_CST
9657           && TREE_CODE (arg0) == MINUS_EXPR
9658           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9659           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9660                                       arg1, 0))
9661           && !TREE_OVERFLOW (tem))
9662         return fold_build2_loc (loc, swap_tree_comparison (code), type,
9663                             TREE_OPERAND (arg0, 1), tem);
9664
9665       /* Fold comparisons against built-in math functions.  */
9666       if (TREE_CODE (arg1) == REAL_CST
9667           && flag_unsafe_math_optimizations
9668           && ! flag_errno_math)
9669         {
9670           enum built_in_function fcode = builtin_mathfn_code (arg0);
9671
9672           if (fcode != END_BUILTINS)
9673             {
9674               tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9675               if (tem != NULL_TREE)
9676                 return tem;
9677             }
9678         }
9679     }
9680
9681   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9682       && CONVERT_EXPR_P (arg0))
9683     {
9684       /* If we are widening one operand of an integer comparison,
9685          see if the other operand is similarly being widened.  Perhaps we
9686          can do the comparison in the narrower type.  */
9687       tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9688       if (tem)
9689         return tem;
9690
9691       /* Or if we are changing signedness.  */
9692       tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9693       if (tem)
9694         return tem;
9695     }
9696
9697   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9698      constant, we can simplify it.  */
9699   if (TREE_CODE (arg1) == INTEGER_CST
9700       && (TREE_CODE (arg0) == MIN_EXPR
9701           || TREE_CODE (arg0) == MAX_EXPR)
9702       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9703     {
9704       tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9705       if (tem)
9706         return tem;
9707     }
9708
9709   /* Simplify comparison of something with itself.  (For IEEE
9710      floating-point, we can only do some of these simplifications.)  */
9711   if (operand_equal_p (arg0, arg1, 0))
9712     {
9713       switch (code)
9714         {
9715         case EQ_EXPR:
9716           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9717               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9718             return constant_boolean_node (1, type);
9719           break;
9720
9721         case GE_EXPR:
9722         case LE_EXPR:
9723           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9724               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9725             return constant_boolean_node (1, type);
9726           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9727
9728         case NE_EXPR:
9729           /* For NE, we can only do this simplification if integer
9730              or we don't honor IEEE floating point NaNs.  */
9731           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9732               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9733             break;
9734           /* ... fall through ...  */
9735         case GT_EXPR:
9736         case LT_EXPR:
9737           return constant_boolean_node (0, type);
9738         default:
9739           gcc_unreachable ();
9740         }
9741     }
9742
9743   /* If we are comparing an expression that just has comparisons
9744      of two integer values, arithmetic expressions of those comparisons,
9745      and constants, we can simplify it.  There are only three cases
9746      to check: the two values can either be equal, the first can be
9747      greater, or the second can be greater.  Fold the expression for
9748      those three values.  Since each value must be 0 or 1, we have
9749      eight possibilities, each of which corresponds to the constant 0
9750      or 1 or one of the six possible comparisons.
9751
9752      This handles common cases like (a > b) == 0 but also handles
9753      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9754      occur in macroized code.  */
9755
9756   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9757     {
9758       tree cval1 = 0, cval2 = 0;
9759       int save_p = 0;
9760
9761       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9762           /* Don't handle degenerate cases here; they should already
9763              have been handled anyway.  */
9764           && cval1 != 0 && cval2 != 0
9765           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9766           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9767           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9768           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9769           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9770           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9771                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9772         {
9773           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9774           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9775
9776           /* We can't just pass T to eval_subst in case cval1 or cval2
9777              was the same as ARG1.  */
9778
9779           tree high_result
9780                 = fold_build2_loc (loc, code, type,
9781                                eval_subst (loc, arg0, cval1, maxval,
9782                                            cval2, minval),
9783                                arg1);
9784           tree equal_result
9785                 = fold_build2_loc (loc, code, type,
9786                                eval_subst (loc, arg0, cval1, maxval,
9787                                            cval2, maxval),
9788                                arg1);
9789           tree low_result
9790                 = fold_build2_loc (loc, code, type,
9791                                eval_subst (loc, arg0, cval1, minval,
9792                                            cval2, maxval),
9793                                arg1);
9794
9795           /* All three of these results should be 0 or 1.  Confirm they are.
9796              Then use those values to select the proper code to use.  */
9797
9798           if (TREE_CODE (high_result) == INTEGER_CST
9799               && TREE_CODE (equal_result) == INTEGER_CST
9800               && TREE_CODE (low_result) == INTEGER_CST)
9801             {
9802               /* Make a 3-bit mask with the high-order bit being the
9803                  value for `>', the next for '=', and the low for '<'.  */
9804               switch ((integer_onep (high_result) * 4)
9805                       + (integer_onep (equal_result) * 2)
9806                       + integer_onep (low_result))
9807                 {
9808                 case 0:
9809                   /* Always false.  */
9810                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9811                 case 1:
9812                   code = LT_EXPR;
9813                   break;
9814                 case 2:
9815                   code = EQ_EXPR;
9816                   break;
9817                 case 3:
9818                   code = LE_EXPR;
9819                   break;
9820                 case 4:
9821                   code = GT_EXPR;
9822                   break;
9823                 case 5:
9824                   code = NE_EXPR;
9825                   break;
9826                 case 6:
9827                   code = GE_EXPR;
9828                   break;
9829                 case 7:
9830                   /* Always true.  */
9831                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9832                 }
9833
9834               if (save_p)
9835                 {
9836                   tem = save_expr (build2 (code, type, cval1, cval2));
9837                   SET_EXPR_LOCATION (tem, loc);
9838                   return tem;
9839                 }
9840               return fold_build2_loc (loc, code, type, cval1, cval2);
9841             }
9842         }
9843     }
9844
9845   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9846      into a single range test.  */
9847   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9848        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9849       && TREE_CODE (arg1) == INTEGER_CST
9850       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9851       && !integer_zerop (TREE_OPERAND (arg0, 1))
9852       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9853       && !TREE_OVERFLOW (arg1))
9854     {
9855       tem = fold_div_compare (loc, code, type, arg0, arg1);
9856       if (tem != NULL_TREE)
9857         return tem;
9858     }
9859
9860   /* Fold ~X op ~Y as Y op X.  */
9861   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9862       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9863     {
9864       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9865       return fold_build2_loc (loc, code, type,
9866                           fold_convert_loc (loc, cmp_type,
9867                                             TREE_OPERAND (arg1, 0)),
9868                           TREE_OPERAND (arg0, 0));
9869     }
9870
9871   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9872   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9873       && TREE_CODE (arg1) == INTEGER_CST)
9874     {
9875       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9876       return fold_build2_loc (loc, swap_tree_comparison (code), type,
9877                           TREE_OPERAND (arg0, 0),
9878                           fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9879                                        fold_convert_loc (loc, cmp_type, arg1)));
9880     }
9881
9882   return NULL_TREE;
9883 }
9884
9885
9886 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9887    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9888    argument EXPR represents the expression "z" of type TYPE.  */
9889
9890 static tree
9891 fold_mult_zconjz (location_t loc, tree type, tree expr)
9892 {
9893   tree itype = TREE_TYPE (type);
9894   tree rpart, ipart, tem;
9895
9896   if (TREE_CODE (expr) == COMPLEX_EXPR)
9897     {
9898       rpart = TREE_OPERAND (expr, 0);
9899       ipart = TREE_OPERAND (expr, 1);
9900     }
9901   else if (TREE_CODE (expr) == COMPLEX_CST)
9902     {
9903       rpart = TREE_REALPART (expr);
9904       ipart = TREE_IMAGPART (expr);
9905     }
9906   else
9907     {
9908       expr = save_expr (expr);
9909       rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9910       ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9911     }
9912
9913   rpart = save_expr (rpart);
9914   ipart = save_expr (ipart);
9915   tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9916                      fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9917                      fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9918   return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9919                       fold_convert_loc (loc, itype, integer_zero_node));
9920 }
9921
9922
9923 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9924    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9925    guarantees that P and N have the same least significant log2(M) bits.
9926    N is not otherwise constrained.  In particular, N is not normalized to
9927    0 <= N < M as is common.  In general, the precise value of P is unknown.
9928    M is chosen as large as possible such that constant N can be determined.
9929
9930    Returns M and sets *RESIDUE to N.
9931
9932    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9933    account.  This is not always possible due to PR 35705.
9934  */
9935
9936 static unsigned HOST_WIDE_INT
9937 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9938                                  bool allow_func_align)
9939 {
9940   enum tree_code code;
9941
9942   *residue = 0;
9943
9944   code = TREE_CODE (expr);
9945   if (code == ADDR_EXPR)
9946     {
9947       expr = TREE_OPERAND (expr, 0);
9948       if (handled_component_p (expr))
9949         {
9950           HOST_WIDE_INT bitsize, bitpos;
9951           tree offset;
9952           enum machine_mode mode;
9953           int unsignedp, volatilep;
9954
9955           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9956                                       &mode, &unsignedp, &volatilep, false);
9957           *residue = bitpos / BITS_PER_UNIT;
9958           if (offset)
9959             {
9960               if (TREE_CODE (offset) == INTEGER_CST)
9961                 *residue += TREE_INT_CST_LOW (offset);
9962               else
9963                 /* We don't handle more complicated offset expressions.  */
9964                 return 1;
9965             }
9966         }
9967
9968       if (DECL_P (expr)
9969           && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9970         return DECL_ALIGN_UNIT (expr);
9971     }
9972   else if (code == POINTER_PLUS_EXPR)
9973     {
9974       tree op0, op1;
9975       unsigned HOST_WIDE_INT modulus;
9976       enum tree_code inner_code;
9977
9978       op0 = TREE_OPERAND (expr, 0);
9979       STRIP_NOPS (op0);
9980       modulus = get_pointer_modulus_and_residue (op0, residue,
9981                                                  allow_func_align);
9982
9983       op1 = TREE_OPERAND (expr, 1);
9984       STRIP_NOPS (op1);
9985       inner_code = TREE_CODE (op1);
9986       if (inner_code == INTEGER_CST)
9987         {
9988           *residue += TREE_INT_CST_LOW (op1);
9989           return modulus;
9990         }
9991       else if (inner_code == MULT_EXPR)
9992         {
9993           op1 = TREE_OPERAND (op1, 1);
9994           if (TREE_CODE (op1) == INTEGER_CST)
9995             {
9996               unsigned HOST_WIDE_INT align;
9997
9998               /* Compute the greatest power-of-2 divisor of op1.  */
9999               align = TREE_INT_CST_LOW (op1);
10000               align &= -align;
10001
10002               /* If align is non-zero and less than *modulus, replace
10003                  *modulus with align., If align is 0, then either op1 is 0
10004                  or the greatest power-of-2 divisor of op1 doesn't fit in an
10005                  unsigned HOST_WIDE_INT.  In either case, no additional
10006                  constraint is imposed.  */
10007               if (align)
10008                 modulus = MIN (modulus, align);
10009
10010               return modulus;
10011             }
10012         }
10013     }
10014
10015     /* If we get here, we were unable to determine anything useful about the
10016        expression.  */
10017     return 1;
10018 }
10019
10020
10021 /* Fold a binary expression of code CODE and type TYPE with operands
10022    OP0 and OP1.  LOC is the location of the resulting expression.
10023    Return the folded expression if folding is successful.  Otherwise,
10024    return NULL_TREE.  */
10025
10026 tree
10027 fold_binary_loc (location_t loc,
10028              enum tree_code code, tree type, tree op0, tree op1)
10029 {
10030   enum tree_code_class kind = TREE_CODE_CLASS (code);
10031   tree arg0, arg1, tem;
10032   tree t1 = NULL_TREE;
10033   bool strict_overflow_p;
10034
10035   gcc_assert (IS_EXPR_CODE_CLASS (kind)
10036               && TREE_CODE_LENGTH (code) == 2
10037               && op0 != NULL_TREE
10038               && op1 != NULL_TREE);
10039
10040   arg0 = op0;
10041   arg1 = op1;
10042
10043   /* Strip any conversions that don't change the mode.  This is
10044      safe for every expression, except for a comparison expression
10045      because its signedness is derived from its operands.  So, in
10046      the latter case, only strip conversions that don't change the
10047      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
10048      preserved.
10049
10050      Note that this is done as an internal manipulation within the
10051      constant folder, in order to find the simplest representation
10052      of the arguments so that their form can be studied.  In any
10053      cases, the appropriate type conversions should be put back in
10054      the tree that will get out of the constant folder.  */
10055
10056   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10057     {
10058       STRIP_SIGN_NOPS (arg0);
10059       STRIP_SIGN_NOPS (arg1);
10060     }
10061   else
10062     {
10063       STRIP_NOPS (arg0);
10064       STRIP_NOPS (arg1);
10065     }
10066
10067   /* Note that TREE_CONSTANT isn't enough: static var addresses are
10068      constant but we can't do arithmetic on them.  */
10069   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10070       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
10071       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
10072       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
10073       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
10074       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
10075     {
10076       if (kind == tcc_binary)
10077         {
10078           /* Make sure type and arg0 have the same saturating flag.  */
10079           gcc_assert (TYPE_SATURATING (type)
10080                       == TYPE_SATURATING (TREE_TYPE (arg0)));
10081           tem = const_binop (code, arg0, arg1, 0);
10082         }
10083       else if (kind == tcc_comparison)
10084         tem = fold_relational_const (code, type, arg0, arg1);
10085       else
10086         tem = NULL_TREE;
10087
10088       if (tem != NULL_TREE)
10089         {
10090           if (TREE_TYPE (tem) != type)
10091             tem = fold_convert_loc (loc, type, tem);
10092           return tem;
10093         }
10094     }
10095
10096   /* If this is a commutative operation, and ARG0 is a constant, move it
10097      to ARG1 to reduce the number of tests below.  */
10098   if (commutative_tree_code (code)
10099       && tree_swap_operands_p (arg0, arg1, true))
10100     return fold_build2_loc (loc, code, type, op1, op0);
10101
10102   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10103
10104      First check for cases where an arithmetic operation is applied to a
10105      compound, conditional, or comparison operation.  Push the arithmetic
10106      operation inside the compound or conditional to see if any folding
10107      can then be done.  Convert comparison to conditional for this purpose.
10108      The also optimizes non-constant cases that used to be done in
10109      expand_expr.
10110
10111      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10112      one of the operands is a comparison and the other is a comparison, a
10113      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
10114      code below would make the expression more complex.  Change it to a
10115      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
10116      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
10117
10118   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
10119        || code == EQ_EXPR || code == NE_EXPR)
10120       && ((truth_value_p (TREE_CODE (arg0))
10121            && (truth_value_p (TREE_CODE (arg1))
10122                || (TREE_CODE (arg1) == BIT_AND_EXPR
10123                    && integer_onep (TREE_OPERAND (arg1, 1)))))
10124           || (truth_value_p (TREE_CODE (arg1))
10125               && (truth_value_p (TREE_CODE (arg0))
10126                   || (TREE_CODE (arg0) == BIT_AND_EXPR
10127                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
10128     {
10129       tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
10130                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
10131                          : TRUTH_XOR_EXPR,
10132                          boolean_type_node,
10133                          fold_convert_loc (loc, boolean_type_node, arg0),
10134                          fold_convert_loc (loc, boolean_type_node, arg1));
10135
10136       if (code == EQ_EXPR)
10137         tem = invert_truthvalue_loc (loc, tem);
10138
10139       return fold_convert_loc (loc, type, tem);
10140     }
10141
10142   if (TREE_CODE_CLASS (code) == tcc_binary
10143       || TREE_CODE_CLASS (code) == tcc_comparison)
10144     {
10145       if (TREE_CODE (arg0) == COMPOUND_EXPR)
10146         {
10147           tem = fold_build2_loc (loc, code, type,
10148                              fold_convert_loc (loc, TREE_TYPE (op0),
10149                                                TREE_OPERAND (arg0, 1)), op1);
10150           tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), tem);
10151           goto fold_binary_exit;
10152         }
10153       if (TREE_CODE (arg1) == COMPOUND_EXPR
10154           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10155         {
10156           tem = fold_build2_loc (loc, code, type, op0,
10157                              fold_convert_loc (loc, TREE_TYPE (op1),
10158                                                TREE_OPERAND (arg1, 1)));
10159           tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), tem);
10160           goto fold_binary_exit;
10161         }
10162
10163       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
10164         {
10165           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10166                                                      arg0, arg1,
10167                                                      /*cond_first_p=*/1);
10168           if (tem != NULL_TREE)
10169             return tem;
10170         }
10171
10172       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
10173         {
10174           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10175                                                      arg1, arg0,
10176                                                      /*cond_first_p=*/0);
10177           if (tem != NULL_TREE)
10178             return tem;
10179         }
10180     }
10181
10182   switch (code)
10183     {
10184     case POINTER_PLUS_EXPR:
10185       /* 0 +p index -> (type)index */
10186       if (integer_zerop (arg0))
10187         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10188
10189       /* PTR +p 0 -> PTR */
10190       if (integer_zerop (arg1))
10191         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10192
10193       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
10194       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10195            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
10196         return fold_convert_loc (loc, type,
10197                                  fold_build2_loc (loc, PLUS_EXPR, sizetype,
10198                                               fold_convert_loc (loc, sizetype,
10199                                                                 arg1),
10200                                               fold_convert_loc (loc, sizetype,
10201                                                                 arg0)));
10202
10203       /* index +p PTR -> PTR +p index */
10204       if (POINTER_TYPE_P (TREE_TYPE (arg1))
10205           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
10206         return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
10207                             fold_convert_loc (loc, type, arg1),
10208                             fold_convert_loc (loc, sizetype, arg0));
10209
10210       /* (PTR +p B) +p A -> PTR +p (B + A) */
10211       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10212         {
10213           tree inner;
10214           tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
10215           tree arg00 = TREE_OPERAND (arg0, 0);
10216           inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
10217                                arg01, fold_convert_loc (loc, sizetype, arg1));
10218           return fold_convert_loc (loc, type,
10219                                    fold_build2_loc (loc, POINTER_PLUS_EXPR,
10220                                                 TREE_TYPE (arg00),
10221                                                 arg00, inner));
10222         }
10223
10224       /* PTR_CST +p CST -> CST1 */
10225       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10226         return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
10227                             fold_convert_loc (loc, type, arg1));
10228
10229      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
10230         of the array.  Loop optimizer sometimes produce this type of
10231         expressions.  */
10232       if (TREE_CODE (arg0) == ADDR_EXPR)
10233         {
10234           tem = try_move_mult_to_index (loc, arg0,
10235                                         fold_convert_loc (loc, sizetype, arg1));
10236           if (tem)
10237             return fold_convert_loc (loc, type, tem);
10238         }
10239
10240       return NULL_TREE;
10241
10242     case PLUS_EXPR:
10243       /* A + (-B) -> A - B */
10244       if (TREE_CODE (arg1) == NEGATE_EXPR)
10245         return fold_build2_loc (loc, MINUS_EXPR, type,
10246                             fold_convert_loc (loc, type, arg0),
10247                             fold_convert_loc (loc, type,
10248                                               TREE_OPERAND (arg1, 0)));
10249       /* (-A) + B -> B - A */
10250       if (TREE_CODE (arg0) == NEGATE_EXPR
10251           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
10252         return fold_build2_loc (loc, MINUS_EXPR, type,
10253                             fold_convert_loc (loc, type, arg1),
10254                             fold_convert_loc (loc, type,
10255                                               TREE_OPERAND (arg0, 0)));
10256
10257       if (INTEGRAL_TYPE_P (type))
10258         {
10259           /* Convert ~A + 1 to -A.  */
10260           if (TREE_CODE (arg0) == BIT_NOT_EXPR
10261               && integer_onep (arg1))
10262             return fold_build1_loc (loc, NEGATE_EXPR, type,
10263                                 fold_convert_loc (loc, type,
10264                                                   TREE_OPERAND (arg0, 0)));
10265
10266           /* ~X + X is -1.  */
10267           if (TREE_CODE (arg0) == BIT_NOT_EXPR
10268               && !TYPE_OVERFLOW_TRAPS (type))
10269             {
10270               tree tem = TREE_OPERAND (arg0, 0);
10271
10272               STRIP_NOPS (tem);
10273               if (operand_equal_p (tem, arg1, 0))
10274                 {
10275                   t1 = build_int_cst_type (type, -1);
10276                   return omit_one_operand_loc (loc, type, t1, arg1);
10277                 }
10278             }
10279
10280           /* X + ~X is -1.  */
10281           if (TREE_CODE (arg1) == BIT_NOT_EXPR
10282               && !TYPE_OVERFLOW_TRAPS (type))
10283             {
10284               tree tem = TREE_OPERAND (arg1, 0);
10285
10286               STRIP_NOPS (tem);
10287               if (operand_equal_p (arg0, tem, 0))
10288                 {
10289                   t1 = build_int_cst_type (type, -1);
10290                   return omit_one_operand_loc (loc, type, t1, arg0);
10291                 }
10292             }
10293
10294           /* X + (X / CST) * -CST is X % CST.  */
10295           if (TREE_CODE (arg1) == MULT_EXPR
10296               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10297               && operand_equal_p (arg0,
10298                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
10299             {
10300               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
10301               tree cst1 = TREE_OPERAND (arg1, 1);
10302               tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
10303                                       cst1, cst0);
10304               if (sum && integer_zerop (sum))
10305                 return fold_convert_loc (loc, type,
10306                                          fold_build2_loc (loc, TRUNC_MOD_EXPR,
10307                                                       TREE_TYPE (arg0), arg0,
10308                                                       cst0));
10309             }
10310         }
10311
10312       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
10313          same or one.  Make sure type is not saturating.
10314          fold_plusminus_mult_expr will re-associate.  */
10315       if ((TREE_CODE (arg0) == MULT_EXPR
10316            || TREE_CODE (arg1) == MULT_EXPR)
10317           && !TYPE_SATURATING (type)
10318           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10319         {
10320           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10321           if (tem)
10322             return tem;
10323         }
10324
10325       if (! FLOAT_TYPE_P (type))
10326         {
10327           if (integer_zerop (arg1))
10328             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10329
10330           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
10331              with a constant, and the two constants have no bits in common,
10332              we should treat this as a BIT_IOR_EXPR since this may produce more
10333              simplifications.  */
10334           if (TREE_CODE (arg0) == BIT_AND_EXPR
10335               && TREE_CODE (arg1) == BIT_AND_EXPR
10336               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10337               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10338               && integer_zerop (const_binop (BIT_AND_EXPR,
10339                                              TREE_OPERAND (arg0, 1),
10340                                              TREE_OPERAND (arg1, 1), 0)))
10341             {
10342               code = BIT_IOR_EXPR;
10343               goto bit_ior;
10344             }
10345
10346           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10347              (plus (plus (mult) (mult)) (foo)) so that we can
10348              take advantage of the factoring cases below.  */
10349           if (((TREE_CODE (arg0) == PLUS_EXPR
10350                 || TREE_CODE (arg0) == MINUS_EXPR)
10351                && TREE_CODE (arg1) == MULT_EXPR)
10352               || ((TREE_CODE (arg1) == PLUS_EXPR
10353                    || TREE_CODE (arg1) == MINUS_EXPR)
10354                   && TREE_CODE (arg0) == MULT_EXPR))
10355             {
10356               tree parg0, parg1, parg, marg;
10357               enum tree_code pcode;
10358
10359               if (TREE_CODE (arg1) == MULT_EXPR)
10360                 parg = arg0, marg = arg1;
10361               else
10362                 parg = arg1, marg = arg0;
10363               pcode = TREE_CODE (parg);
10364               parg0 = TREE_OPERAND (parg, 0);
10365               parg1 = TREE_OPERAND (parg, 1);
10366               STRIP_NOPS (parg0);
10367               STRIP_NOPS (parg1);
10368
10369               if (TREE_CODE (parg0) == MULT_EXPR
10370                   && TREE_CODE (parg1) != MULT_EXPR)
10371                 return fold_build2_loc (loc, pcode, type,
10372                                     fold_build2_loc (loc, PLUS_EXPR, type,
10373                                                  fold_convert_loc (loc, type,
10374                                                                    parg0),
10375                                                  fold_convert_loc (loc, type,
10376                                                                    marg)),
10377                                     fold_convert_loc (loc, type, parg1));
10378               if (TREE_CODE (parg0) != MULT_EXPR
10379                   && TREE_CODE (parg1) == MULT_EXPR)
10380                 return
10381                   fold_build2_loc (loc, PLUS_EXPR, type,
10382                                fold_convert_loc (loc, type, parg0),
10383                                fold_build2_loc (loc, pcode, type,
10384                                             fold_convert_loc (loc, type, marg),
10385                                             fold_convert_loc (loc, type,
10386                                                               parg1)));
10387             }
10388         }
10389       else
10390         {
10391           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
10392           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10393             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10394
10395           /* Likewise if the operands are reversed.  */
10396           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10397             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10398
10399           /* Convert X + -C into X - C.  */
10400           if (TREE_CODE (arg1) == REAL_CST
10401               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10402             {
10403               tem = fold_negate_const (arg1, type);
10404               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10405                 return fold_build2_loc (loc, MINUS_EXPR, type,
10406                                     fold_convert_loc (loc, type, arg0),
10407                                     fold_convert_loc (loc, type, tem));
10408             }
10409
10410           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10411              to __complex__ ( x, y ).  This is not the same for SNaNs or
10412              if signed zeros are involved.  */
10413           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10414               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10415               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10416             {
10417               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10418               tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10419               tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10420               bool arg0rz = false, arg0iz = false;
10421               if ((arg0r && (arg0rz = real_zerop (arg0r)))
10422                   || (arg0i && (arg0iz = real_zerop (arg0i))))
10423                 {
10424                   tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10425                   tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10426                   if (arg0rz && arg1i && real_zerop (arg1i))
10427                     {
10428                       tree rp = arg1r ? arg1r
10429                                   : build1 (REALPART_EXPR, rtype, arg1);
10430                       tree ip = arg0i ? arg0i
10431                                   : build1 (IMAGPART_EXPR, rtype, arg0);
10432                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10433                     }
10434                   else if (arg0iz && arg1r && real_zerop (arg1r))
10435                     {
10436                       tree rp = arg0r ? arg0r
10437                                   : build1 (REALPART_EXPR, rtype, arg0);
10438                       tree ip = arg1i ? arg1i
10439                                   : build1 (IMAGPART_EXPR, rtype, arg1);
10440                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10441                     }
10442                 }
10443             }
10444
10445           if (flag_unsafe_math_optimizations
10446               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10447               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10448               && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10449             return tem;
10450
10451           /* Convert x+x into x*2.0.  */
10452           if (operand_equal_p (arg0, arg1, 0)
10453               && SCALAR_FLOAT_TYPE_P (type))
10454             return fold_build2_loc (loc, MULT_EXPR, type, arg0,
10455                                 build_real (type, dconst2));
10456
10457           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10458              We associate floats only if the user has specified
10459              -fassociative-math.  */
10460           if (flag_associative_math
10461               && TREE_CODE (arg1) == PLUS_EXPR
10462               && TREE_CODE (arg0) != MULT_EXPR)
10463             {
10464               tree tree10 = TREE_OPERAND (arg1, 0);
10465               tree tree11 = TREE_OPERAND (arg1, 1);
10466               if (TREE_CODE (tree11) == MULT_EXPR
10467                   && TREE_CODE (tree10) == MULT_EXPR)
10468                 {
10469                   tree tree0;
10470                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
10471                   return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
10472                 }
10473             }
10474           /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10475              We associate floats only if the user has specified
10476              -fassociative-math.  */
10477           if (flag_associative_math
10478               && TREE_CODE (arg0) == PLUS_EXPR
10479               && TREE_CODE (arg1) != MULT_EXPR)
10480             {
10481               tree tree00 = TREE_OPERAND (arg0, 0);
10482               tree tree01 = TREE_OPERAND (arg0, 1);
10483               if (TREE_CODE (tree01) == MULT_EXPR
10484                   && TREE_CODE (tree00) == MULT_EXPR)
10485                 {
10486                   tree tree0;
10487                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
10488                   return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
10489                 }
10490             }
10491         }
10492
10493      bit_rotate:
10494       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10495          is a rotate of A by C1 bits.  */
10496       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10497          is a rotate of A by B bits.  */
10498       {
10499         enum tree_code code0, code1;
10500         tree rtype;
10501         code0 = TREE_CODE (arg0);
10502         code1 = TREE_CODE (arg1);
10503         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10504              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10505             && operand_equal_p (TREE_OPERAND (arg0, 0),
10506                                 TREE_OPERAND (arg1, 0), 0)
10507             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10508                 TYPE_UNSIGNED (rtype))
10509             /* Only create rotates in complete modes.  Other cases are not
10510                expanded properly.  */
10511             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10512           {
10513             tree tree01, tree11;
10514             enum tree_code code01, code11;
10515
10516             tree01 = TREE_OPERAND (arg0, 1);
10517             tree11 = TREE_OPERAND (arg1, 1);
10518             STRIP_NOPS (tree01);
10519             STRIP_NOPS (tree11);
10520             code01 = TREE_CODE (tree01);
10521             code11 = TREE_CODE (tree11);
10522             if (code01 == INTEGER_CST
10523                 && code11 == INTEGER_CST
10524                 && TREE_INT_CST_HIGH (tree01) == 0
10525                 && TREE_INT_CST_HIGH (tree11) == 0
10526                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10527                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10528               {
10529                 tem = build2 (LROTATE_EXPR,
10530                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
10531                               TREE_OPERAND (arg0, 0),
10532                               code0 == LSHIFT_EXPR
10533                               ? tree01 : tree11);
10534                 SET_EXPR_LOCATION (tem, loc);
10535                 return fold_convert_loc (loc, type, tem);
10536               }
10537             else if (code11 == MINUS_EXPR)
10538               {
10539                 tree tree110, tree111;
10540                 tree110 = TREE_OPERAND (tree11, 0);
10541                 tree111 = TREE_OPERAND (tree11, 1);
10542                 STRIP_NOPS (tree110);
10543                 STRIP_NOPS (tree111);
10544                 if (TREE_CODE (tree110) == INTEGER_CST
10545                     && 0 == compare_tree_int (tree110,
10546                                               TYPE_PRECISION
10547                                               (TREE_TYPE (TREE_OPERAND
10548                                                           (arg0, 0))))
10549                     && operand_equal_p (tree01, tree111, 0))
10550                   return
10551                     fold_convert_loc (loc, type,
10552                                       build2 ((code0 == LSHIFT_EXPR
10553                                                ? LROTATE_EXPR
10554                                                : RROTATE_EXPR),
10555                                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
10556                                               TREE_OPERAND (arg0, 0), tree01));
10557               }
10558             else if (code01 == MINUS_EXPR)
10559               {
10560                 tree tree010, tree011;
10561                 tree010 = TREE_OPERAND (tree01, 0);
10562                 tree011 = TREE_OPERAND (tree01, 1);
10563                 STRIP_NOPS (tree010);
10564                 STRIP_NOPS (tree011);
10565                 if (TREE_CODE (tree010) == INTEGER_CST
10566                     && 0 == compare_tree_int (tree010,
10567                                               TYPE_PRECISION
10568                                               (TREE_TYPE (TREE_OPERAND
10569                                                           (arg0, 0))))
10570                     && operand_equal_p (tree11, tree011, 0))
10571                     return fold_convert_loc
10572                       (loc, type,
10573                        build2 ((code0 != LSHIFT_EXPR
10574                                 ? LROTATE_EXPR
10575                                 : RROTATE_EXPR),
10576                                TREE_TYPE (TREE_OPERAND (arg0, 0)),
10577                                TREE_OPERAND (arg0, 0), tree11));
10578               }
10579           }
10580       }
10581
10582     associate:
10583       /* In most languages, can't associate operations on floats through
10584          parentheses.  Rather than remember where the parentheses were, we
10585          don't associate floats at all, unless the user has specified
10586          -fassociative-math.
10587          And, we need to make sure type is not saturating.  */
10588
10589       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10590           && !TYPE_SATURATING (type))
10591         {
10592           tree var0, con0, lit0, minus_lit0;
10593           tree var1, con1, lit1, minus_lit1;
10594           bool ok = true;
10595
10596           /* Split both trees into variables, constants, and literals.  Then
10597              associate each group together, the constants with literals,
10598              then the result with variables.  This increases the chances of
10599              literals being recombined later and of generating relocatable
10600              expressions for the sum of a constant and literal.  */
10601           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10602           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10603                              code == MINUS_EXPR);
10604
10605           /* With undefined overflow we can only associate constants
10606              with one variable.  */
10607           if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10608                || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10609               && var0 && var1)
10610             {
10611               tree tmp0 = var0;
10612               tree tmp1 = var1;
10613
10614               if (TREE_CODE (tmp0) == NEGATE_EXPR)
10615                 tmp0 = TREE_OPERAND (tmp0, 0);
10616               if (TREE_CODE (tmp1) == NEGATE_EXPR)
10617                 tmp1 = TREE_OPERAND (tmp1, 0);
10618               /* The only case we can still associate with two variables
10619                  is if they are the same, modulo negation.  */
10620               if (!operand_equal_p (tmp0, tmp1, 0))
10621                 ok = false;
10622             }
10623
10624           /* Only do something if we found more than two objects.  Otherwise,
10625              nothing has changed and we risk infinite recursion.  */
10626           if (ok
10627               && (2 < ((var0 != 0) + (var1 != 0)
10628                        + (con0 != 0) + (con1 != 0)
10629                        + (lit0 != 0) + (lit1 != 0)
10630                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10631             {
10632               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
10633               if (code == MINUS_EXPR)
10634                 code = PLUS_EXPR;
10635
10636               var0 = associate_trees (loc, var0, var1, code, type);
10637               con0 = associate_trees (loc, con0, con1, code, type);
10638               lit0 = associate_trees (loc, lit0, lit1, code, type);
10639               minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
10640
10641               /* Preserve the MINUS_EXPR if the negative part of the literal is
10642                  greater than the positive part.  Otherwise, the multiplicative
10643                  folding code (i.e extract_muldiv) may be fooled in case
10644                  unsigned constants are subtracted, like in the following
10645                  example: ((X*2 + 4) - 8U)/2.  */
10646               if (minus_lit0 && lit0)
10647                 {
10648                   if (TREE_CODE (lit0) == INTEGER_CST
10649                       && TREE_CODE (minus_lit0) == INTEGER_CST
10650                       && tree_int_cst_lt (lit0, minus_lit0))
10651                     {
10652                       minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10653                                                     MINUS_EXPR, type);
10654                       lit0 = 0;
10655                     }
10656                   else
10657                     {
10658                       lit0 = associate_trees (loc, lit0, minus_lit0,
10659                                               MINUS_EXPR, type);
10660                       minus_lit0 = 0;
10661                     }
10662                 }
10663               if (minus_lit0)
10664                 {
10665                   if (con0 == 0)
10666                     return
10667                       fold_convert_loc (loc, type,
10668                                         associate_trees (loc, var0, minus_lit0,
10669                                                          MINUS_EXPR, type));
10670                   else
10671                     {
10672                       con0 = associate_trees (loc, con0, minus_lit0,
10673                                               MINUS_EXPR, type);
10674                       return
10675                         fold_convert_loc (loc, type,
10676                                           associate_trees (loc, var0, con0,
10677                                                            PLUS_EXPR, type));
10678                     }
10679                 }
10680
10681               con0 = associate_trees (loc, con0, lit0, code, type);
10682               return
10683                 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10684                                                               code, type));
10685             }
10686         }
10687
10688       return NULL_TREE;
10689
10690     case MINUS_EXPR:
10691       /* Pointer simplifications for subtraction, simple reassociations. */
10692       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10693         {
10694           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10695           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10696               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10697             {
10698               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10699               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10700               tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10701               tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10702               return fold_build2_loc (loc, PLUS_EXPR, type,
10703                                   fold_build2_loc (loc, MINUS_EXPR, type,
10704                                                arg00, arg10),
10705                                   fold_build2_loc (loc, MINUS_EXPR, type,
10706                                                arg01, arg11));
10707             }
10708           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10709           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10710             {
10711               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10712               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10713               tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10714                                       fold_convert_loc (loc, type, arg1));
10715               if (tmp)
10716                 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10717             }
10718         }
10719       /* A - (-B) -> A + B */
10720       if (TREE_CODE (arg1) == NEGATE_EXPR)
10721         return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10722                             fold_convert_loc (loc, type,
10723                                               TREE_OPERAND (arg1, 0)));
10724       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10725       if (TREE_CODE (arg0) == NEGATE_EXPR
10726           && (FLOAT_TYPE_P (type)
10727               || INTEGRAL_TYPE_P (type))
10728           && negate_expr_p (arg1)
10729           && reorder_operands_p (arg0, arg1))
10730         return fold_build2_loc (loc, MINUS_EXPR, type,
10731                             fold_convert_loc (loc, type,
10732                                               negate_expr (arg1)),
10733                             fold_convert_loc (loc, type,
10734                                               TREE_OPERAND (arg0, 0)));
10735       /* Convert -A - 1 to ~A.  */
10736       if (INTEGRAL_TYPE_P (type)
10737           && TREE_CODE (arg0) == NEGATE_EXPR
10738           && integer_onep (arg1)
10739           && !TYPE_OVERFLOW_TRAPS (type))
10740         return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10741                             fold_convert_loc (loc, type,
10742                                               TREE_OPERAND (arg0, 0)));
10743
10744       /* Convert -1 - A to ~A.  */
10745       if (INTEGRAL_TYPE_P (type)
10746           && integer_all_onesp (arg0))
10747         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10748
10749
10750       /* X - (X / CST) * CST is X % CST.  */
10751       if (INTEGRAL_TYPE_P (type)
10752           && TREE_CODE (arg1) == MULT_EXPR
10753           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10754           && operand_equal_p (arg0,
10755                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10756           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10757                               TREE_OPERAND (arg1, 1), 0))
10758         return
10759           fold_convert_loc (loc, type,
10760                             fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10761                                          arg0, TREE_OPERAND (arg1, 1)));
10762
10763       if (! FLOAT_TYPE_P (type))
10764         {
10765           if (integer_zerop (arg0))
10766             return negate_expr (fold_convert_loc (loc, type, arg1));
10767           if (integer_zerop (arg1))
10768             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10769
10770           /* Fold A - (A & B) into ~B & A.  */
10771           if (!TREE_SIDE_EFFECTS (arg0)
10772               && TREE_CODE (arg1) == BIT_AND_EXPR)
10773             {
10774               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10775                 {
10776                   tree arg10 = fold_convert_loc (loc, type,
10777                                                  TREE_OPERAND (arg1, 0));
10778                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10779                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10780                                                    type, arg10),
10781                                       fold_convert_loc (loc, type, arg0));
10782                 }
10783               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10784                 {
10785                   tree arg11 = fold_convert_loc (loc,
10786                                                  type, TREE_OPERAND (arg1, 1));
10787                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10788                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10789                                                    type, arg11),
10790                                       fold_convert_loc (loc, type, arg0));
10791                 }
10792             }
10793
10794           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10795              any power of 2 minus 1.  */
10796           if (TREE_CODE (arg0) == BIT_AND_EXPR
10797               && TREE_CODE (arg1) == BIT_AND_EXPR
10798               && operand_equal_p (TREE_OPERAND (arg0, 0),
10799                                   TREE_OPERAND (arg1, 0), 0))
10800             {
10801               tree mask0 = TREE_OPERAND (arg0, 1);
10802               tree mask1 = TREE_OPERAND (arg1, 1);
10803               tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10804
10805               if (operand_equal_p (tem, mask1, 0))
10806                 {
10807                   tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10808                                      TREE_OPERAND (arg0, 0), mask1);
10809                   return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10810                 }
10811             }
10812         }
10813
10814       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10815       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10816         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10817
10818       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10819          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10820          (-ARG1 + ARG0) reduces to -ARG1.  */
10821       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10822         return negate_expr (fold_convert_loc (loc, type, arg1));
10823
10824       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10825          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10826          signed zeros are involved.  */
10827       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10828           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10829           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10830         {
10831           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10832           tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10833           tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10834           bool arg0rz = false, arg0iz = false;
10835           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10836               || (arg0i && (arg0iz = real_zerop (arg0i))))
10837             {
10838               tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10839               tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10840               if (arg0rz && arg1i && real_zerop (arg1i))
10841                 {
10842                   tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10843                                          arg1r ? arg1r
10844                                          : build1 (REALPART_EXPR, rtype, arg1));
10845                   tree ip = arg0i ? arg0i
10846                     : build1 (IMAGPART_EXPR, rtype, arg0);
10847                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10848                 }
10849               else if (arg0iz && arg1r && real_zerop (arg1r))
10850                 {
10851                   tree rp = arg0r ? arg0r
10852                     : build1 (REALPART_EXPR, rtype, arg0);
10853                   tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10854                                          arg1i ? arg1i
10855                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10856                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10857                 }
10858             }
10859         }
10860
10861       /* Fold &x - &x.  This can happen from &x.foo - &x.
10862          This is unsafe for certain floats even in non-IEEE formats.
10863          In IEEE, it is unsafe because it does wrong for NaNs.
10864          Also note that operand_equal_p is always false if an operand
10865          is volatile.  */
10866
10867       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10868           && operand_equal_p (arg0, arg1, 0))
10869         return fold_convert_loc (loc, type, integer_zero_node);
10870
10871       /* A - B -> A + (-B) if B is easily negatable.  */
10872       if (negate_expr_p (arg1)
10873           && ((FLOAT_TYPE_P (type)
10874                /* Avoid this transformation if B is a positive REAL_CST.  */
10875                && (TREE_CODE (arg1) != REAL_CST
10876                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10877               || INTEGRAL_TYPE_P (type)))
10878         return fold_build2_loc (loc, PLUS_EXPR, type,
10879                             fold_convert_loc (loc, type, arg0),
10880                             fold_convert_loc (loc, type,
10881                                               negate_expr (arg1)));
10882
10883       /* Try folding difference of addresses.  */
10884       {
10885         HOST_WIDE_INT diff;
10886
10887         if ((TREE_CODE (arg0) == ADDR_EXPR
10888              || TREE_CODE (arg1) == ADDR_EXPR)
10889             && ptr_difference_const (arg0, arg1, &diff))
10890           return build_int_cst_type (type, diff);
10891       }
10892
10893       /* Fold &a[i] - &a[j] to i-j.  */
10894       if (TREE_CODE (arg0) == ADDR_EXPR
10895           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10896           && TREE_CODE (arg1) == ADDR_EXPR
10897           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10898         {
10899           tree aref0 = TREE_OPERAND (arg0, 0);
10900           tree aref1 = TREE_OPERAND (arg1, 0);
10901           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10902                                TREE_OPERAND (aref1, 0), 0))
10903             {
10904               tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10905               tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10906               tree esz = array_ref_element_size (aref0);
10907               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10908               return fold_build2_loc (loc, MULT_EXPR, type, diff,
10909                                   fold_convert_loc (loc, type, esz));
10910
10911             }
10912         }
10913
10914       if (FLOAT_TYPE_P (type)
10915           && flag_unsafe_math_optimizations
10916           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10917           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10918           && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10919         return tem;
10920
10921       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10922          same or one.  Make sure type is not saturating.
10923          fold_plusminus_mult_expr will re-associate.  */
10924       if ((TREE_CODE (arg0) == MULT_EXPR
10925            || TREE_CODE (arg1) == MULT_EXPR)
10926           && !TYPE_SATURATING (type)
10927           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10928         {
10929           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10930           if (tem)
10931             return tem;
10932         }
10933
10934       goto associate;
10935
10936     case MULT_EXPR:
10937       /* (-A) * (-B) -> A * B  */
10938       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10939         return fold_build2_loc (loc, MULT_EXPR, type,
10940                             fold_convert_loc (loc, type,
10941                                               TREE_OPERAND (arg0, 0)),
10942                             fold_convert_loc (loc, type,
10943                                               negate_expr (arg1)));
10944       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10945         return fold_build2_loc (loc, MULT_EXPR, type,
10946                             fold_convert_loc (loc, type,
10947                                               negate_expr (arg0)),
10948                             fold_convert_loc (loc, type,
10949                                               TREE_OPERAND (arg1, 0)));
10950
10951       if (! FLOAT_TYPE_P (type))
10952         {
10953           if (integer_zerop (arg1))
10954             return omit_one_operand_loc (loc, type, arg1, arg0);
10955           if (integer_onep (arg1))
10956             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10957           /* Transform x * -1 into -x.  Make sure to do the negation
10958              on the original operand with conversions not stripped
10959              because we can only strip non-sign-changing conversions.  */
10960           if (integer_all_onesp (arg1))
10961             return fold_convert_loc (loc, type, negate_expr (op0));
10962           /* Transform x * -C into -x * C if x is easily negatable.  */
10963           if (TREE_CODE (arg1) == INTEGER_CST
10964               && tree_int_cst_sgn (arg1) == -1
10965               && negate_expr_p (arg0)
10966               && (tem = negate_expr (arg1)) != arg1
10967               && !TREE_OVERFLOW (tem))
10968             return fold_build2_loc (loc, MULT_EXPR, type,
10969                                 fold_convert_loc (loc, type,
10970                                                   negate_expr (arg0)),
10971                                 tem);
10972
10973           /* (a * (1 << b)) is (a << b)  */
10974           if (TREE_CODE (arg1) == LSHIFT_EXPR
10975               && integer_onep (TREE_OPERAND (arg1, 0)))
10976             return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10977                                 TREE_OPERAND (arg1, 1));
10978           if (TREE_CODE (arg0) == LSHIFT_EXPR
10979               && integer_onep (TREE_OPERAND (arg0, 0)))
10980             return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10981                                 TREE_OPERAND (arg0, 1));
10982
10983           /* (A + A) * C -> A * 2 * C  */
10984           if (TREE_CODE (arg0) == PLUS_EXPR
10985               && TREE_CODE (arg1) == INTEGER_CST
10986               && operand_equal_p (TREE_OPERAND (arg0, 0),
10987                                   TREE_OPERAND (arg0, 1), 0))
10988             return fold_build2_loc (loc, MULT_EXPR, type,
10989                                 omit_one_operand_loc (loc, type,
10990                                                   TREE_OPERAND (arg0, 0),
10991                                                   TREE_OPERAND (arg0, 1)),
10992                                 fold_build2_loc (loc, MULT_EXPR, type,
10993                                              build_int_cst (type, 2) , arg1));
10994
10995           strict_overflow_p = false;
10996           if (TREE_CODE (arg1) == INTEGER_CST
10997               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10998                                              &strict_overflow_p)))
10999             {
11000               if (strict_overflow_p)
11001                 fold_overflow_warning (("assuming signed overflow does not "
11002                                         "occur when simplifying "
11003                                         "multiplication"),
11004                                        WARN_STRICT_OVERFLOW_MISC);
11005               return fold_convert_loc (loc, type, tem);
11006             }
11007
11008           /* Optimize z * conj(z) for integer complex numbers.  */
11009           if (TREE_CODE (arg0) == CONJ_EXPR
11010               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11011             return fold_mult_zconjz (loc, type, arg1);
11012           if (TREE_CODE (arg1) == CONJ_EXPR
11013               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11014             return fold_mult_zconjz (loc, type, arg0);
11015         }
11016       else
11017         {
11018           /* Maybe fold x * 0 to 0.  The expressions aren't the same
11019              when x is NaN, since x * 0 is also NaN.  Nor are they the
11020              same in modes with signed zeros, since multiplying a
11021              negative value by 0 gives -0, not +0.  */
11022           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11023               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11024               && real_zerop (arg1))
11025             return omit_one_operand_loc (loc, type, arg1, arg0);
11026           /* In IEEE floating point, x*1 is not equivalent to x for snans.
11027              Likewise for complex arithmetic with signed zeros.  */
11028           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11029               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11030                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11031               && real_onep (arg1))
11032             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11033
11034           /* Transform x * -1.0 into -x.  */
11035           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11036               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11037                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11038               && real_minus_onep (arg1))
11039             return fold_convert_loc (loc, type, negate_expr (arg0));
11040
11041           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
11042              the result for floating point types due to rounding so it is applied
11043              only if -fassociative-math was specify.  */
11044           if (flag_associative_math
11045               && TREE_CODE (arg0) == RDIV_EXPR
11046               && TREE_CODE (arg1) == REAL_CST
11047               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
11048             {
11049               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
11050                                       arg1, 0);
11051               if (tem)
11052                 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11053                                     TREE_OPERAND (arg0, 1));
11054             }
11055
11056           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
11057           if (operand_equal_p (arg0, arg1, 0))
11058             {
11059               tree tem = fold_strip_sign_ops (arg0);
11060               if (tem != NULL_TREE)
11061                 {
11062                   tem = fold_convert_loc (loc, type, tem);
11063                   return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
11064                 }
11065             }
11066
11067           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11068              This is not the same for NaNs or if signed zeros are
11069              involved.  */
11070           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11071               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11072               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11073               && TREE_CODE (arg1) == COMPLEX_CST
11074               && real_zerop (TREE_REALPART (arg1)))
11075             {
11076               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11077               if (real_onep (TREE_IMAGPART (arg1)))
11078                 return
11079                   fold_build2_loc (loc, COMPLEX_EXPR, type,
11080                                negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
11081                                                              rtype, arg0)),
11082                                fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
11083               else if (real_minus_onep (TREE_IMAGPART (arg1)))
11084                 return
11085                   fold_build2_loc (loc, COMPLEX_EXPR, type,
11086                                fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
11087                                negate_expr (fold_build1_loc (loc, REALPART_EXPR,
11088                                                              rtype, arg0)));
11089             }
11090
11091           /* Optimize z * conj(z) for floating point complex numbers.
11092              Guarded by flag_unsafe_math_optimizations as non-finite
11093              imaginary components don't produce scalar results.  */
11094           if (flag_unsafe_math_optimizations
11095               && TREE_CODE (arg0) == CONJ_EXPR
11096               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11097             return fold_mult_zconjz (loc, type, arg1);
11098           if (flag_unsafe_math_optimizations
11099               && TREE_CODE (arg1) == CONJ_EXPR
11100               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11101             return fold_mult_zconjz (loc, type, arg0);
11102
11103           if (flag_unsafe_math_optimizations)
11104             {
11105               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11106               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11107
11108               /* Optimizations of root(...)*root(...).  */
11109               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
11110                 {
11111                   tree rootfn, arg;
11112                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
11113                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
11114
11115                   /* Optimize sqrt(x)*sqrt(x) as x.  */
11116                   if (BUILTIN_SQRT_P (fcode0)
11117                       && operand_equal_p (arg00, arg10, 0)
11118                       && ! HONOR_SNANS (TYPE_MODE (type)))
11119                     return arg00;
11120
11121                   /* Optimize root(x)*root(y) as root(x*y).  */
11122                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11123                   arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
11124                   return build_call_expr_loc (loc, rootfn, 1, arg);
11125                 }
11126
11127               /* Optimize expN(x)*expN(y) as expN(x+y).  */
11128               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
11129                 {
11130                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11131                   tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
11132                                           CALL_EXPR_ARG (arg0, 0),
11133                                           CALL_EXPR_ARG (arg1, 0));
11134                   return build_call_expr_loc (loc, expfn, 1, arg);
11135                 }
11136
11137               /* Optimizations of pow(...)*pow(...).  */
11138               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
11139                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
11140                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
11141                 {
11142                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
11143                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
11144                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
11145                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
11146
11147                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
11148                   if (operand_equal_p (arg01, arg11, 0))
11149                     {
11150                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11151                       tree arg = fold_build2_loc (loc, MULT_EXPR, type,
11152                                               arg00, arg10);
11153                       return build_call_expr_loc (loc, powfn, 2, arg, arg01);
11154                     }
11155
11156                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
11157                   if (operand_equal_p (arg00, arg10, 0))
11158                     {
11159                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11160                       tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
11161                                               arg01, arg11);
11162                       return build_call_expr_loc (loc, powfn, 2, arg00, arg);
11163                     }
11164                 }
11165
11166               /* Optimize tan(x)*cos(x) as sin(x).  */
11167               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
11168                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
11169                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
11170                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
11171                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
11172                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
11173                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11174                                       CALL_EXPR_ARG (arg1, 0), 0))
11175                 {
11176                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
11177
11178                   if (sinfn != NULL_TREE)
11179                     return build_call_expr_loc (loc, sinfn, 1,
11180                                             CALL_EXPR_ARG (arg0, 0));
11181                 }
11182
11183               /* Optimize x*pow(x,c) as pow(x,c+1).  */
11184               if (fcode1 == BUILT_IN_POW
11185                   || fcode1 == BUILT_IN_POWF
11186                   || fcode1 == BUILT_IN_POWL)
11187                 {
11188                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
11189                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
11190                   if (TREE_CODE (arg11) == REAL_CST
11191                       && !TREE_OVERFLOW (arg11)
11192                       && operand_equal_p (arg0, arg10, 0))
11193                     {
11194                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11195                       REAL_VALUE_TYPE c;
11196                       tree arg;
11197
11198                       c = TREE_REAL_CST (arg11);
11199                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
11200                       arg = build_real (type, c);
11201                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
11202                     }
11203                 }
11204
11205               /* Optimize pow(x,c)*x as pow(x,c+1).  */
11206               if (fcode0 == BUILT_IN_POW
11207                   || fcode0 == BUILT_IN_POWF
11208                   || fcode0 == BUILT_IN_POWL)
11209                 {
11210                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
11211                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
11212                   if (TREE_CODE (arg01) == REAL_CST
11213                       && !TREE_OVERFLOW (arg01)
11214                       && operand_equal_p (arg1, arg00, 0))
11215                     {
11216                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11217                       REAL_VALUE_TYPE c;
11218                       tree arg;
11219
11220                       c = TREE_REAL_CST (arg01);
11221                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
11222                       arg = build_real (type, c);
11223                       return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11224                     }
11225                 }
11226
11227               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
11228               if (optimize_function_for_speed_p (cfun)
11229                   && operand_equal_p (arg0, arg1, 0))
11230                 {
11231                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
11232
11233                   if (powfn)
11234                     {
11235                       tree arg = build_real (type, dconst2);
11236                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
11237                     }
11238                 }
11239             }
11240         }
11241       goto associate;
11242
11243     case BIT_IOR_EXPR:
11244     bit_ior:
11245       if (integer_all_onesp (arg1))
11246         return omit_one_operand_loc (loc, type, arg1, arg0);
11247       if (integer_zerop (arg1))
11248         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11249       if (operand_equal_p (arg0, arg1, 0))
11250         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11251
11252       /* ~X | X is -1.  */
11253       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11254           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11255         {
11256           t1 = fold_convert_loc (loc, type, integer_zero_node);
11257           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11258           return omit_one_operand_loc (loc, type, t1, arg1);
11259         }
11260
11261       /* X | ~X is -1.  */
11262       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11263           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11264         {
11265           t1 = fold_convert_loc (loc, type, integer_zero_node);
11266           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11267           return omit_one_operand_loc (loc, type, t1, arg0);
11268         }
11269
11270       /* Canonicalize (X & C1) | C2.  */
11271       if (TREE_CODE (arg0) == BIT_AND_EXPR
11272           && TREE_CODE (arg1) == INTEGER_CST
11273           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11274         {
11275           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
11276           int width = TYPE_PRECISION (type), w;
11277           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
11278           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11279           hi2 = TREE_INT_CST_HIGH (arg1);
11280           lo2 = TREE_INT_CST_LOW (arg1);
11281
11282           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
11283           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
11284             return omit_one_operand_loc (loc, type, arg1,
11285                                      TREE_OPERAND (arg0, 0));
11286
11287           if (width > HOST_BITS_PER_WIDE_INT)
11288             {
11289               mhi = (unsigned HOST_WIDE_INT) -1
11290                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
11291               mlo = -1;
11292             }
11293           else
11294             {
11295               mhi = 0;
11296               mlo = (unsigned HOST_WIDE_INT) -1
11297                     >> (HOST_BITS_PER_WIDE_INT - width);
11298             }
11299
11300           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
11301           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
11302             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11303                                 TREE_OPERAND (arg0, 0), arg1);
11304
11305           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11306              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11307              mode which allows further optimizations.  */
11308           hi1 &= mhi;
11309           lo1 &= mlo;
11310           hi2 &= mhi;
11311           lo2 &= mlo;
11312           hi3 = hi1 & ~hi2;
11313           lo3 = lo1 & ~lo2;
11314           for (w = BITS_PER_UNIT;
11315                w <= width && w <= HOST_BITS_PER_WIDE_INT;
11316                w <<= 1)
11317             {
11318               unsigned HOST_WIDE_INT mask
11319                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
11320               if (((lo1 | lo2) & mask) == mask
11321                   && (lo1 & ~mask) == 0 && hi1 == 0)
11322                 {
11323                   hi3 = 0;
11324                   lo3 = mask;
11325                   break;
11326                 }
11327             }
11328           if (hi3 != hi1 || lo3 != lo1)
11329             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11330                                 fold_build2_loc (loc, BIT_AND_EXPR, type,
11331                                              TREE_OPERAND (arg0, 0),
11332                                              build_int_cst_wide (type,
11333                                                                  lo3, hi3)),
11334                                 arg1);
11335         }
11336
11337       /* (X & Y) | Y is (X, Y).  */
11338       if (TREE_CODE (arg0) == BIT_AND_EXPR
11339           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11340         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11341       /* (X & Y) | X is (Y, X).  */
11342       if (TREE_CODE (arg0) == BIT_AND_EXPR
11343           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11344           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11345         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11346       /* X | (X & Y) is (Y, X).  */
11347       if (TREE_CODE (arg1) == BIT_AND_EXPR
11348           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11349           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11350         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11351       /* X | (Y & X) is (Y, X).  */
11352       if (TREE_CODE (arg1) == BIT_AND_EXPR
11353           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11354           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11355         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11356
11357       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11358       if (t1 != NULL_TREE)
11359         return t1;
11360
11361       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
11362
11363          This results in more efficient code for machines without a NAND
11364          instruction.  Combine will canonicalize to the first form
11365          which will allow use of NAND instructions provided by the
11366          backend if they exist.  */
11367       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11368           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11369         {
11370           return
11371             fold_build1_loc (loc, BIT_NOT_EXPR, type,
11372                          build2 (BIT_AND_EXPR, type,
11373                                  fold_convert_loc (loc, type,
11374                                                    TREE_OPERAND (arg0, 0)),
11375                                  fold_convert_loc (loc, type,
11376                                                    TREE_OPERAND (arg1, 0))));
11377         }
11378
11379       /* See if this can be simplified into a rotate first.  If that
11380          is unsuccessful continue in the association code.  */
11381       goto bit_rotate;
11382
11383     case BIT_XOR_EXPR:
11384       if (integer_zerop (arg1))
11385         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11386       if (integer_all_onesp (arg1))
11387         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
11388       if (operand_equal_p (arg0, arg1, 0))
11389         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11390
11391       /* ~X ^ X is -1.  */
11392       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11393           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11394         {
11395           t1 = fold_convert_loc (loc, type, integer_zero_node);
11396           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11397           return omit_one_operand_loc (loc, type, t1, arg1);
11398         }
11399
11400       /* X ^ ~X is -1.  */
11401       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11402           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11403         {
11404           t1 = fold_convert_loc (loc, type, integer_zero_node);
11405           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11406           return omit_one_operand_loc (loc, type, t1, arg0);
11407         }
11408
11409       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11410          with a constant, and the two constants have no bits in common,
11411          we should treat this as a BIT_IOR_EXPR since this may produce more
11412          simplifications.  */
11413       if (TREE_CODE (arg0) == BIT_AND_EXPR
11414           && TREE_CODE (arg1) == BIT_AND_EXPR
11415           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11416           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11417           && integer_zerop (const_binop (BIT_AND_EXPR,
11418                                          TREE_OPERAND (arg0, 1),
11419                                          TREE_OPERAND (arg1, 1), 0)))
11420         {
11421           code = BIT_IOR_EXPR;
11422           goto bit_ior;
11423         }
11424
11425       /* (X | Y) ^ X -> Y & ~ X*/
11426       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11427           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11428         {
11429           tree t2 = TREE_OPERAND (arg0, 1);
11430           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11431                             arg1);
11432           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11433                             fold_convert_loc (loc, type, t2),
11434                             fold_convert_loc (loc, type, t1));
11435           return t1;
11436         }
11437
11438       /* (Y | X) ^ X -> Y & ~ X*/
11439       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11440           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11441         {
11442           tree t2 = TREE_OPERAND (arg0, 0);
11443           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11444                             arg1);
11445           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11446                             fold_convert_loc (loc, type, t2),
11447                             fold_convert_loc (loc, type, t1));
11448           return t1;
11449         }
11450
11451       /* X ^ (X | Y) -> Y & ~ X*/
11452       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11453           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11454         {
11455           tree t2 = TREE_OPERAND (arg1, 1);
11456           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11457                             arg0);
11458           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11459                             fold_convert_loc (loc, type, t2),
11460                             fold_convert_loc (loc, type, t1));
11461           return t1;
11462         }
11463
11464       /* X ^ (Y | X) -> Y & ~ X*/
11465       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11466           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11467         {
11468           tree t2 = TREE_OPERAND (arg1, 0);
11469           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11470                             arg0);
11471           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11472                             fold_convert_loc (loc, type, t2),
11473                             fold_convert_loc (loc, type, t1));
11474           return t1;
11475         }
11476
11477       /* Convert ~X ^ ~Y to X ^ Y.  */
11478       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11479           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11480         return fold_build2_loc (loc, code, type,
11481                             fold_convert_loc (loc, type,
11482                                               TREE_OPERAND (arg0, 0)),
11483                             fold_convert_loc (loc, type,
11484                                               TREE_OPERAND (arg1, 0)));
11485
11486       /* Convert ~X ^ C to X ^ ~C.  */
11487       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11488           && TREE_CODE (arg1) == INTEGER_CST)
11489         return fold_build2_loc (loc, code, type,
11490                             fold_convert_loc (loc, type,
11491                                               TREE_OPERAND (arg0, 0)),
11492                             fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
11493
11494       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
11495       if (TREE_CODE (arg0) == BIT_AND_EXPR
11496           && integer_onep (TREE_OPERAND (arg0, 1))
11497           && integer_onep (arg1))
11498         return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11499                             build_int_cst (TREE_TYPE (arg0), 0));
11500
11501       /* Fold (X & Y) ^ Y as ~X & Y.  */
11502       if (TREE_CODE (arg0) == BIT_AND_EXPR
11503           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11504         {
11505           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11506           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11507                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11508                               fold_convert_loc (loc, type, arg1));
11509         }
11510       /* Fold (X & Y) ^ X as ~Y & X.  */
11511       if (TREE_CODE (arg0) == BIT_AND_EXPR
11512           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11513           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11514         {
11515           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11516           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11517                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11518                               fold_convert_loc (loc, type, arg1));
11519         }
11520       /* Fold X ^ (X & Y) as X & ~Y.  */
11521       if (TREE_CODE (arg1) == BIT_AND_EXPR
11522           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11523         {
11524           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11525           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11526                               fold_convert_loc (loc, type, arg0),
11527                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11528         }
11529       /* Fold X ^ (Y & X) as ~Y & X.  */
11530       if (TREE_CODE (arg1) == BIT_AND_EXPR
11531           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11532           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11533         {
11534           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11535           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11536                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11537                               fold_convert_loc (loc, type, arg0));
11538         }
11539
11540       /* See if this can be simplified into a rotate first.  If that
11541          is unsuccessful continue in the association code.  */
11542       goto bit_rotate;
11543
11544     case BIT_AND_EXPR:
11545       if (integer_all_onesp (arg1))
11546         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11547       if (integer_zerop (arg1))
11548         return omit_one_operand_loc (loc, type, arg1, arg0);
11549       if (operand_equal_p (arg0, arg1, 0))
11550         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11551
11552       /* ~X & X is always zero.  */
11553       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11554           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11555         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11556
11557       /* X & ~X is always zero.  */
11558       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11559           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11560         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11561
11562       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
11563       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11564           && TREE_CODE (arg1) == INTEGER_CST
11565           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11566         {
11567           tree tmp1 = fold_convert_loc (loc, type, arg1);
11568           tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11569           tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11570           tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
11571           tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
11572           return
11573             fold_convert_loc (loc, type,
11574                               fold_build2_loc (loc, BIT_IOR_EXPR,
11575                                            type, tmp2, tmp3));
11576         }
11577
11578       /* (X | Y) & Y is (X, Y).  */
11579       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11580           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11581         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11582       /* (X | Y) & X is (Y, X).  */
11583       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11584           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11585           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11586         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11587       /* X & (X | Y) is (Y, X).  */
11588       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11589           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11590           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11591         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11592       /* X & (Y | X) is (Y, X).  */
11593       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11594           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11595           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11596         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11597
11598       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11599       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11600           && integer_onep (TREE_OPERAND (arg0, 1))
11601           && integer_onep (arg1))
11602         {
11603           tem = TREE_OPERAND (arg0, 0);
11604           return fold_build2_loc (loc, EQ_EXPR, type,
11605                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11606                                            build_int_cst (TREE_TYPE (tem), 1)),
11607                               build_int_cst (TREE_TYPE (tem), 0));
11608         }
11609       /* Fold ~X & 1 as (X & 1) == 0.  */
11610       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11611           && integer_onep (arg1))
11612         {
11613           tem = TREE_OPERAND (arg0, 0);
11614           return fold_build2_loc (loc, EQ_EXPR, type,
11615                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11616                                            build_int_cst (TREE_TYPE (tem), 1)),
11617                               build_int_cst (TREE_TYPE (tem), 0));
11618         }
11619
11620       /* Fold (X ^ Y) & Y as ~X & Y.  */
11621       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11622           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11623         {
11624           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11625           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11626                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11627                               fold_convert_loc (loc, type, arg1));
11628         }
11629       /* Fold (X ^ Y) & X as ~Y & X.  */
11630       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11631           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11632           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11633         {
11634           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11635           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11636                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11637                               fold_convert_loc (loc, type, arg1));
11638         }
11639       /* Fold X & (X ^ Y) as X & ~Y.  */
11640       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11641           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11642         {
11643           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11644           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11645                               fold_convert_loc (loc, type, arg0),
11646                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11647         }
11648       /* Fold X & (Y ^ X) as ~Y & X.  */
11649       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11650           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11651           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11652         {
11653           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11654           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11655                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11656                               fold_convert_loc (loc, type, arg0));
11657         }
11658
11659       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11660       if (t1 != NULL_TREE)
11661         return t1;
11662       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11663       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11664           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11665         {
11666           unsigned int prec
11667             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11668
11669           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11670               && (~TREE_INT_CST_LOW (arg1)
11671                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11672             return
11673               fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11674         }
11675
11676       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11677
11678          This results in more efficient code for machines without a NOR
11679          instruction.  Combine will canonicalize to the first form
11680          which will allow use of NOR instructions provided by the
11681          backend if they exist.  */
11682       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11683           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11684         {
11685           return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11686                               build2 (BIT_IOR_EXPR, type,
11687                                       fold_convert_loc (loc, type,
11688                                                         TREE_OPERAND (arg0, 0)),
11689                                       fold_convert_loc (loc, type,
11690                                                         TREE_OPERAND (arg1, 0))));
11691         }
11692
11693       /* If arg0 is derived from the address of an object or function, we may
11694          be able to fold this expression using the object or function's
11695          alignment.  */
11696       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11697         {
11698           unsigned HOST_WIDE_INT modulus, residue;
11699           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11700
11701           modulus = get_pointer_modulus_and_residue (arg0, &residue,
11702                                                      integer_onep (arg1));
11703
11704           /* This works because modulus is a power of 2.  If this weren't the
11705              case, we'd have to replace it by its greatest power-of-2
11706              divisor: modulus & -modulus.  */
11707           if (low < modulus)
11708             return build_int_cst (type, residue & low);
11709         }
11710
11711       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11712               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11713          if the new mask might be further optimized.  */
11714       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11715            || TREE_CODE (arg0) == RSHIFT_EXPR)
11716           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11717           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11718           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11719              < TYPE_PRECISION (TREE_TYPE (arg0))
11720           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11721           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11722         {
11723           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11724           unsigned HOST_WIDE_INT mask
11725             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11726           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11727           tree shift_type = TREE_TYPE (arg0);
11728
11729           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11730             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11731           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11732                    && TYPE_PRECISION (TREE_TYPE (arg0))
11733                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11734             {
11735               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11736               tree arg00 = TREE_OPERAND (arg0, 0);
11737               /* See if more bits can be proven as zero because of
11738                  zero extension.  */
11739               if (TREE_CODE (arg00) == NOP_EXPR
11740                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11741                 {
11742                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11743                   if (TYPE_PRECISION (inner_type)
11744                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11745                       && TYPE_PRECISION (inner_type) < prec)
11746                     {
11747                       prec = TYPE_PRECISION (inner_type);
11748                       /* See if we can shorten the right shift.  */
11749                       if (shiftc < prec)
11750                         shift_type = inner_type;
11751                     }
11752                 }
11753               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11754               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11755               zerobits <<= prec - shiftc;
11756               /* For arithmetic shift if sign bit could be set, zerobits
11757                  can contain actually sign bits, so no transformation is
11758                  possible, unless MASK masks them all away.  In that
11759                  case the shift needs to be converted into logical shift.  */
11760               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11761                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11762                 {
11763                   if ((mask & zerobits) == 0)
11764                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11765                   else
11766                     zerobits = 0;
11767                 }
11768             }
11769
11770           /* ((X << 16) & 0xff00) is (X, 0).  */
11771           if ((mask & zerobits) == mask)
11772             return omit_one_operand_loc (loc, type,
11773                                      build_int_cst (type, 0), arg0);
11774
11775           newmask = mask | zerobits;
11776           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11777             {
11778               unsigned int prec;
11779
11780               /* Only do the transformation if NEWMASK is some integer
11781                  mode's mask.  */
11782               for (prec = BITS_PER_UNIT;
11783                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11784                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11785                   break;
11786               if (prec < HOST_BITS_PER_WIDE_INT
11787                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11788                 {
11789                   tree newmaskt;
11790
11791                   if (shift_type != TREE_TYPE (arg0))
11792                     {
11793                       tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11794                                          fold_convert_loc (loc, shift_type,
11795                                                            TREE_OPERAND (arg0, 0)),
11796                                          TREE_OPERAND (arg0, 1));
11797                       tem = fold_convert_loc (loc, type, tem);
11798                     }
11799                   else
11800                     tem = op0;
11801                   newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11802                   if (!tree_int_cst_equal (newmaskt, arg1))
11803                     return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11804                 }
11805             }
11806         }
11807
11808       goto associate;
11809
11810     case RDIV_EXPR:
11811       /* Don't touch a floating-point divide by zero unless the mode
11812          of the constant can represent infinity.  */
11813       if (TREE_CODE (arg1) == REAL_CST
11814           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11815           && real_zerop (arg1))
11816         return NULL_TREE;
11817
11818       /* Optimize A / A to 1.0 if we don't care about
11819          NaNs or Infinities.  Skip the transformation
11820          for non-real operands.  */
11821       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11822           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11823           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11824           && operand_equal_p (arg0, arg1, 0))
11825         {
11826           tree r = build_real (TREE_TYPE (arg0), dconst1);
11827
11828           return omit_two_operands_loc (loc, type, r, arg0, arg1);
11829         }
11830
11831       /* The complex version of the above A / A optimization.  */
11832       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11833           && operand_equal_p (arg0, arg1, 0))
11834         {
11835           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11836           if (! HONOR_NANS (TYPE_MODE (elem_type))
11837               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11838             {
11839               tree r = build_real (elem_type, dconst1);
11840               /* omit_two_operands will call fold_convert for us.  */
11841               return omit_two_operands_loc (loc, type, r, arg0, arg1);
11842             }
11843         }
11844
11845       /* (-A) / (-B) -> A / B  */
11846       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11847         return fold_build2_loc (loc, RDIV_EXPR, type,
11848                             TREE_OPERAND (arg0, 0),
11849                             negate_expr (arg1));
11850       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11851         return fold_build2_loc (loc, RDIV_EXPR, type,
11852                             negate_expr (arg0),
11853                             TREE_OPERAND (arg1, 0));
11854
11855       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11856       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11857           && real_onep (arg1))
11858         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11859
11860       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11861       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11862           && real_minus_onep (arg1))
11863         return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11864                                                   negate_expr (arg0)));
11865
11866       /* If ARG1 is a constant, we can convert this to a multiply by the
11867          reciprocal.  This does not have the same rounding properties,
11868          so only do this if -freciprocal-math.  We can actually
11869          always safely do it if ARG1 is a power of two, but it's hard to
11870          tell if it is or not in a portable manner.  */
11871       if (TREE_CODE (arg1) == REAL_CST)
11872         {
11873           if (flag_reciprocal_math
11874               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11875                                           arg1, 0)))
11876             return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11877           /* Find the reciprocal if optimizing and the result is exact.  */
11878           if (optimize)
11879             {
11880               REAL_VALUE_TYPE r;
11881               r = TREE_REAL_CST (arg1);
11882               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11883                 {
11884                   tem = build_real (type, r);
11885                   return fold_build2_loc (loc, MULT_EXPR, type,
11886                                       fold_convert_loc (loc, type, arg0), tem);
11887                 }
11888             }
11889         }
11890       /* Convert A/B/C to A/(B*C).  */
11891       if (flag_reciprocal_math
11892           && TREE_CODE (arg0) == RDIV_EXPR)
11893         return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11894                             fold_build2_loc (loc, MULT_EXPR, type,
11895                                          TREE_OPERAND (arg0, 1), arg1));
11896
11897       /* Convert A/(B/C) to (A/B)*C.  */
11898       if (flag_reciprocal_math
11899           && TREE_CODE (arg1) == RDIV_EXPR)
11900         return fold_build2_loc (loc, MULT_EXPR, type,
11901                             fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11902                                          TREE_OPERAND (arg1, 0)),
11903                             TREE_OPERAND (arg1, 1));
11904
11905       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11906       if (flag_reciprocal_math
11907           && TREE_CODE (arg1) == MULT_EXPR
11908           && TREE_CODE (arg0) == REAL_CST
11909           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11910         {
11911           tree tem = const_binop (RDIV_EXPR, arg0,
11912                                   TREE_OPERAND (arg1, 1), 0);
11913           if (tem)
11914             return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11915                                 TREE_OPERAND (arg1, 0));
11916         }
11917
11918       if (flag_unsafe_math_optimizations)
11919         {
11920           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11921           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11922
11923           /* Optimize sin(x)/cos(x) as tan(x).  */
11924           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11925                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11926                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11927               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11928                                   CALL_EXPR_ARG (arg1, 0), 0))
11929             {
11930               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11931
11932               if (tanfn != NULL_TREE)
11933                 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11934             }
11935
11936           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11937           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11938                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11939                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11940               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11941                                   CALL_EXPR_ARG (arg1, 0), 0))
11942             {
11943               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11944
11945               if (tanfn != NULL_TREE)
11946                 {
11947                   tree tmp = build_call_expr_loc (loc, tanfn, 1,
11948                                               CALL_EXPR_ARG (arg0, 0));
11949                   return fold_build2_loc (loc, RDIV_EXPR, type,
11950                                       build_real (type, dconst1), tmp);
11951                 }
11952             }
11953
11954           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11955              NaNs or Infinities.  */
11956           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11957                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11958                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11959             {
11960               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11961               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11962
11963               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11964                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11965                   && operand_equal_p (arg00, arg01, 0))
11966                 {
11967                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11968
11969                   if (cosfn != NULL_TREE)
11970                     return build_call_expr_loc (loc, cosfn, 1, arg00);
11971                 }
11972             }
11973
11974           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11975              NaNs or Infinities.  */
11976           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11977                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11978                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11979             {
11980               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11981               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11982
11983               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11984                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11985                   && operand_equal_p (arg00, arg01, 0))
11986                 {
11987                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11988
11989                   if (cosfn != NULL_TREE)
11990                     {
11991                       tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11992                       return fold_build2_loc (loc, RDIV_EXPR, type,
11993                                           build_real (type, dconst1),
11994                                           tmp);
11995                     }
11996                 }
11997             }
11998
11999           /* Optimize pow(x,c)/x as pow(x,c-1).  */
12000           if (fcode0 == BUILT_IN_POW
12001               || fcode0 == BUILT_IN_POWF
12002               || fcode0 == BUILT_IN_POWL)
12003             {
12004               tree arg00 = CALL_EXPR_ARG (arg0, 0);
12005               tree arg01 = CALL_EXPR_ARG (arg0, 1);
12006               if (TREE_CODE (arg01) == REAL_CST
12007                   && !TREE_OVERFLOW (arg01)
12008                   && operand_equal_p (arg1, arg00, 0))
12009                 {
12010                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
12011                   REAL_VALUE_TYPE c;
12012                   tree arg;
12013
12014                   c = TREE_REAL_CST (arg01);
12015                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
12016                   arg = build_real (type, c);
12017                   return build_call_expr_loc (loc, powfn, 2, arg1, arg);
12018                 }
12019             }
12020
12021           /* Optimize a/root(b/c) into a*root(c/b).  */
12022           if (BUILTIN_ROOT_P (fcode1))
12023             {
12024               tree rootarg = CALL_EXPR_ARG (arg1, 0);
12025
12026               if (TREE_CODE (rootarg) == RDIV_EXPR)
12027                 {
12028                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12029                   tree b = TREE_OPERAND (rootarg, 0);
12030                   tree c = TREE_OPERAND (rootarg, 1);
12031
12032                   tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
12033
12034                   tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
12035                   return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
12036                 }
12037             }
12038
12039           /* Optimize x/expN(y) into x*expN(-y).  */
12040           if (BUILTIN_EXPONENT_P (fcode1))
12041             {
12042               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12043               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
12044               arg1 = build_call_expr_loc (loc,
12045                                       expfn, 1,
12046                                       fold_convert_loc (loc, type, arg));
12047               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
12048             }
12049
12050           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
12051           if (fcode1 == BUILT_IN_POW
12052               || fcode1 == BUILT_IN_POWF
12053               || fcode1 == BUILT_IN_POWL)
12054             {
12055               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12056               tree arg10 = CALL_EXPR_ARG (arg1, 0);
12057               tree arg11 = CALL_EXPR_ARG (arg1, 1);
12058               tree neg11 = fold_convert_loc (loc, type,
12059                                              negate_expr (arg11));
12060               arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
12061               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
12062             }
12063         }
12064       return NULL_TREE;
12065
12066     case TRUNC_DIV_EXPR:
12067     case FLOOR_DIV_EXPR:
12068       /* Simplify A / (B << N) where A and B are positive and B is
12069          a power of 2, to A >> (N + log2(B)).  */
12070       strict_overflow_p = false;
12071       if (TREE_CODE (arg1) == LSHIFT_EXPR
12072           && (TYPE_UNSIGNED (type)
12073               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12074         {
12075           tree sval = TREE_OPERAND (arg1, 0);
12076           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
12077             {
12078               tree sh_cnt = TREE_OPERAND (arg1, 1);
12079               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
12080
12081               if (strict_overflow_p)
12082                 fold_overflow_warning (("assuming signed overflow does not "
12083                                         "occur when simplifying A / (B << N)"),
12084                                        WARN_STRICT_OVERFLOW_MISC);
12085
12086               sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
12087                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
12088               return fold_build2_loc (loc, RSHIFT_EXPR, type,
12089                                   fold_convert_loc (loc, type, arg0), sh_cnt);
12090             }
12091         }
12092
12093       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
12094          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
12095       if (INTEGRAL_TYPE_P (type)
12096           && TYPE_UNSIGNED (type)
12097           && code == FLOOR_DIV_EXPR)
12098         return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
12099
12100       /* Fall thru */
12101
12102     case ROUND_DIV_EXPR:
12103     case CEIL_DIV_EXPR:
12104     case EXACT_DIV_EXPR:
12105       if (integer_onep (arg1))
12106         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12107       if (integer_zerop (arg1))
12108         return NULL_TREE;
12109       /* X / -1 is -X.  */
12110       if (!TYPE_UNSIGNED (type)
12111           && TREE_CODE (arg1) == INTEGER_CST
12112           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
12113           && TREE_INT_CST_HIGH (arg1) == -1)
12114         return fold_convert_loc (loc, type, negate_expr (arg0));
12115
12116       /* Convert -A / -B to A / B when the type is signed and overflow is
12117          undefined.  */
12118       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12119           && TREE_CODE (arg0) == NEGATE_EXPR
12120           && negate_expr_p (arg1))
12121         {
12122           if (INTEGRAL_TYPE_P (type))
12123             fold_overflow_warning (("assuming signed overflow does not occur "
12124                                     "when distributing negation across "
12125                                     "division"),
12126                                    WARN_STRICT_OVERFLOW_MISC);
12127           return fold_build2_loc (loc, code, type,
12128                               fold_convert_loc (loc, type,
12129                                                 TREE_OPERAND (arg0, 0)),
12130                               fold_convert_loc (loc, type,
12131                                                 negate_expr (arg1)));
12132         }
12133       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12134           && TREE_CODE (arg1) == NEGATE_EXPR
12135           && negate_expr_p (arg0))
12136         {
12137           if (INTEGRAL_TYPE_P (type))
12138             fold_overflow_warning (("assuming signed overflow does not occur "
12139                                     "when distributing negation across "
12140                                     "division"),
12141                                    WARN_STRICT_OVERFLOW_MISC);
12142           return fold_build2_loc (loc, code, type,
12143                               fold_convert_loc (loc, type,
12144                                                 negate_expr (arg0)),
12145                               fold_convert_loc (loc, type,
12146                                                 TREE_OPERAND (arg1, 0)));
12147         }
12148
12149       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12150          operation, EXACT_DIV_EXPR.
12151
12152          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12153          At one time others generated faster code, it's not clear if they do
12154          after the last round to changes to the DIV code in expmed.c.  */
12155       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12156           && multiple_of_p (type, arg0, arg1))
12157         return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
12158
12159       strict_overflow_p = false;
12160       if (TREE_CODE (arg1) == INTEGER_CST
12161           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12162                                          &strict_overflow_p)))
12163         {
12164           if (strict_overflow_p)
12165             fold_overflow_warning (("assuming signed overflow does not occur "
12166                                     "when simplifying division"),
12167                                    WARN_STRICT_OVERFLOW_MISC);
12168           return fold_convert_loc (loc, type, tem);
12169         }
12170
12171       return NULL_TREE;
12172
12173     case CEIL_MOD_EXPR:
12174     case FLOOR_MOD_EXPR:
12175     case ROUND_MOD_EXPR:
12176     case TRUNC_MOD_EXPR:
12177       /* X % 1 is always zero, but be sure to preserve any side
12178          effects in X.  */
12179       if (integer_onep (arg1))
12180         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12181
12182       /* X % 0, return X % 0 unchanged so that we can get the
12183          proper warnings and errors.  */
12184       if (integer_zerop (arg1))
12185         return NULL_TREE;
12186
12187       /* 0 % X is always zero, but be sure to preserve any side
12188          effects in X.  Place this after checking for X == 0.  */
12189       if (integer_zerop (arg0))
12190         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12191
12192       /* X % -1 is zero.  */
12193       if (!TYPE_UNSIGNED (type)
12194           && TREE_CODE (arg1) == INTEGER_CST
12195           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
12196           && TREE_INT_CST_HIGH (arg1) == -1)
12197         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12198
12199       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
12200          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
12201       strict_overflow_p = false;
12202       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
12203           && (TYPE_UNSIGNED (type)
12204               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12205         {
12206           tree c = arg1;
12207           /* Also optimize A % (C << N)  where C is a power of 2,
12208              to A & ((C << N) - 1).  */
12209           if (TREE_CODE (arg1) == LSHIFT_EXPR)
12210             c = TREE_OPERAND (arg1, 0);
12211
12212           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
12213             {
12214               tree mask = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
12215                                        build_int_cst (TREE_TYPE (arg1), 1));
12216               if (strict_overflow_p)
12217                 fold_overflow_warning (("assuming signed overflow does not "
12218                                         "occur when simplifying "
12219                                         "X % (power of two)"),
12220                                        WARN_STRICT_OVERFLOW_MISC);
12221               return fold_build2_loc (loc, BIT_AND_EXPR, type,
12222                                   fold_convert_loc (loc, type, arg0),
12223                                   fold_convert_loc (loc, type, mask));
12224             }
12225         }
12226
12227       /* X % -C is the same as X % C.  */
12228       if (code == TRUNC_MOD_EXPR
12229           && !TYPE_UNSIGNED (type)
12230           && TREE_CODE (arg1) == INTEGER_CST
12231           && !TREE_OVERFLOW (arg1)
12232           && TREE_INT_CST_HIGH (arg1) < 0
12233           && !TYPE_OVERFLOW_TRAPS (type)
12234           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
12235           && !sign_bit_p (arg1, arg1))
12236         return fold_build2_loc (loc, code, type,
12237                             fold_convert_loc (loc, type, arg0),
12238                             fold_convert_loc (loc, type,
12239                                               negate_expr (arg1)));
12240
12241       /* X % -Y is the same as X % Y.  */
12242       if (code == TRUNC_MOD_EXPR
12243           && !TYPE_UNSIGNED (type)
12244           && TREE_CODE (arg1) == NEGATE_EXPR
12245           && !TYPE_OVERFLOW_TRAPS (type))
12246         return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
12247                             fold_convert_loc (loc, type,
12248                                               TREE_OPERAND (arg1, 0)));
12249
12250       if (TREE_CODE (arg1) == INTEGER_CST
12251           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12252                                          &strict_overflow_p)))
12253         {
12254           if (strict_overflow_p)
12255             fold_overflow_warning (("assuming signed overflow does not occur "
12256                                     "when simplifying modulus"),
12257                                    WARN_STRICT_OVERFLOW_MISC);
12258           return fold_convert_loc (loc, type, tem);
12259         }
12260
12261       return NULL_TREE;
12262
12263     case LROTATE_EXPR:
12264     case RROTATE_EXPR:
12265       if (integer_all_onesp (arg0))
12266         return omit_one_operand_loc (loc, type, arg0, arg1);
12267       goto shift;
12268
12269     case RSHIFT_EXPR:
12270       /* Optimize -1 >> x for arithmetic right shifts.  */
12271       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
12272           && tree_expr_nonnegative_p (arg1))
12273         return omit_one_operand_loc (loc, type, arg0, arg1);
12274       /* ... fall through ...  */
12275
12276     case LSHIFT_EXPR:
12277     shift:
12278       if (integer_zerop (arg1))
12279         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12280       if (integer_zerop (arg0))
12281         return omit_one_operand_loc (loc, type, arg0, arg1);
12282
12283       /* Since negative shift count is not well-defined,
12284          don't try to compute it in the compiler.  */
12285       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12286         return NULL_TREE;
12287
12288       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
12289       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
12290           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
12291           && host_integerp (TREE_OPERAND (arg0, 1), false)
12292           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
12293         {
12294           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
12295                                + TREE_INT_CST_LOW (arg1));
12296
12297           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
12298              being well defined.  */
12299           if (low >= TYPE_PRECISION (type))
12300             {
12301               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
12302                 low = low % TYPE_PRECISION (type);
12303               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
12304                 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
12305                                          TREE_OPERAND (arg0, 0));
12306               else
12307                 low = TYPE_PRECISION (type) - 1;
12308             }
12309
12310           return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12311                               build_int_cst (type, low));
12312         }
12313
12314       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
12315          into x & ((unsigned)-1 >> c) for unsigned types.  */
12316       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
12317            || (TYPE_UNSIGNED (type)
12318                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
12319           && host_integerp (arg1, false)
12320           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
12321           && host_integerp (TREE_OPERAND (arg0, 1), false)
12322           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
12323         {
12324           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
12325           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
12326           tree lshift;
12327           tree arg00;
12328
12329           if (low0 == low1)
12330             {
12331               arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12332
12333               lshift = build_int_cst (type, -1);
12334               lshift = int_const_binop (code, lshift, arg1, 0);
12335
12336               return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
12337             }
12338         }
12339
12340       /* Rewrite an LROTATE_EXPR by a constant into an
12341          RROTATE_EXPR by a new constant.  */
12342       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
12343         {
12344           tree tem = build_int_cst (TREE_TYPE (arg1),
12345                                     TYPE_PRECISION (type));
12346           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
12347           return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
12348         }
12349
12350       /* If we have a rotate of a bit operation with the rotate count and
12351          the second operand of the bit operation both constant,
12352          permute the two operations.  */
12353       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12354           && (TREE_CODE (arg0) == BIT_AND_EXPR
12355               || TREE_CODE (arg0) == BIT_IOR_EXPR
12356               || TREE_CODE (arg0) == BIT_XOR_EXPR)
12357           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12358         return fold_build2_loc (loc, TREE_CODE (arg0), type,
12359                             fold_build2_loc (loc, code, type,
12360                                          TREE_OPERAND (arg0, 0), arg1),
12361                             fold_build2_loc (loc, code, type,
12362                                          TREE_OPERAND (arg0, 1), arg1));
12363
12364       /* Two consecutive rotates adding up to the precision of the
12365          type can be ignored.  */
12366       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12367           && TREE_CODE (arg0) == RROTATE_EXPR
12368           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12369           && TREE_INT_CST_HIGH (arg1) == 0
12370           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
12371           && ((TREE_INT_CST_LOW (arg1)
12372                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
12373               == (unsigned int) TYPE_PRECISION (type)))
12374         return TREE_OPERAND (arg0, 0);
12375
12376       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
12377               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
12378          if the latter can be further optimized.  */
12379       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
12380           && TREE_CODE (arg0) == BIT_AND_EXPR
12381           && TREE_CODE (arg1) == INTEGER_CST
12382           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12383         {
12384           tree mask = fold_build2_loc (loc, code, type,
12385                                    fold_convert_loc (loc, type,
12386                                                      TREE_OPERAND (arg0, 1)),
12387                                    arg1);
12388           tree shift = fold_build2_loc (loc, code, type,
12389                                     fold_convert_loc (loc, type,
12390                                                       TREE_OPERAND (arg0, 0)),
12391                                     arg1);
12392           tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
12393           if (tem)
12394             return tem;
12395         }
12396
12397       return NULL_TREE;
12398
12399     case MIN_EXPR:
12400       if (operand_equal_p (arg0, arg1, 0))
12401         return omit_one_operand_loc (loc, type, arg0, arg1);
12402       if (INTEGRAL_TYPE_P (type)
12403           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
12404         return omit_one_operand_loc (loc, type, arg1, arg0);
12405       tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
12406       if (tem)
12407         return tem;
12408       goto associate;
12409
12410     case MAX_EXPR:
12411       if (operand_equal_p (arg0, arg1, 0))
12412         return omit_one_operand_loc (loc, type, arg0, arg1);
12413       if (INTEGRAL_TYPE_P (type)
12414           && TYPE_MAX_VALUE (type)
12415           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
12416         return omit_one_operand_loc (loc, type, arg1, arg0);
12417       tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
12418       if (tem)
12419         return tem;
12420       goto associate;
12421
12422     case TRUTH_ANDIF_EXPR:
12423       /* Note that the operands of this must be ints
12424          and their values must be 0 or 1.
12425          ("true" is a fixed value perhaps depending on the language.)  */
12426       /* If first arg is constant zero, return it.  */
12427       if (integer_zerop (arg0))
12428         return fold_convert_loc (loc, type, arg0);
12429     case TRUTH_AND_EXPR:
12430       /* If either arg is constant true, drop it.  */
12431       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12432         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12433       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12434           /* Preserve sequence points.  */
12435           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12436         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12437       /* If second arg is constant zero, result is zero, but first arg
12438          must be evaluated.  */
12439       if (integer_zerop (arg1))
12440         return omit_one_operand_loc (loc, type, arg1, arg0);
12441       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12442          case will be handled here.  */
12443       if (integer_zerop (arg0))
12444         return omit_one_operand_loc (loc, type, arg0, arg1);
12445
12446       /* !X && X is always false.  */
12447       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12448           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12449         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12450       /* X && !X is always false.  */
12451       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12452           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12453         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12454
12455       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
12456          means A >= Y && A != MAX, but in this case we know that
12457          A < X <= MAX.  */
12458
12459       if (!TREE_SIDE_EFFECTS (arg0)
12460           && !TREE_SIDE_EFFECTS (arg1))
12461         {
12462           tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12463           if (tem && !operand_equal_p (tem, arg0, 0))
12464             return fold_build2_loc (loc, code, type, tem, arg1);
12465
12466           tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12467           if (tem && !operand_equal_p (tem, arg1, 0))
12468             return fold_build2_loc (loc, code, type, arg0, tem);
12469         }
12470
12471     truth_andor:
12472       /* We only do these simplifications if we are optimizing.  */
12473       if (!optimize)
12474         return NULL_TREE;
12475
12476       /* Check for things like (A || B) && (A || C).  We can convert this
12477          to A || (B && C).  Note that either operator can be any of the four
12478          truth and/or operations and the transformation will still be
12479          valid.   Also note that we only care about order for the
12480          ANDIF and ORIF operators.  If B contains side effects, this
12481          might change the truth-value of A.  */
12482       if (TREE_CODE (arg0) == TREE_CODE (arg1)
12483           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12484               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12485               || TREE_CODE (arg0) == TRUTH_AND_EXPR
12486               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12487           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12488         {
12489           tree a00 = TREE_OPERAND (arg0, 0);
12490           tree a01 = TREE_OPERAND (arg0, 1);
12491           tree a10 = TREE_OPERAND (arg1, 0);
12492           tree a11 = TREE_OPERAND (arg1, 1);
12493           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12494                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12495                              && (code == TRUTH_AND_EXPR
12496                                  || code == TRUTH_OR_EXPR));
12497
12498           if (operand_equal_p (a00, a10, 0))
12499             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12500                                 fold_build2_loc (loc, code, type, a01, a11));
12501           else if (commutative && operand_equal_p (a00, a11, 0))
12502             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12503                                 fold_build2_loc (loc, code, type, a01, a10));
12504           else if (commutative && operand_equal_p (a01, a10, 0))
12505             return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
12506                                 fold_build2_loc (loc, code, type, a00, a11));
12507
12508           /* This case if tricky because we must either have commutative
12509              operators or else A10 must not have side-effects.  */
12510
12511           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12512                    && operand_equal_p (a01, a11, 0))
12513             return fold_build2_loc (loc, TREE_CODE (arg0), type,
12514                                 fold_build2_loc (loc, code, type, a00, a10),
12515                                 a01);
12516         }
12517
12518       /* See if we can build a range comparison.  */
12519       if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
12520         return tem;
12521
12522       /* Check for the possibility of merging component references.  If our
12523          lhs is another similar operation, try to merge its rhs with our
12524          rhs.  Then try to merge our lhs and rhs.  */
12525       if (TREE_CODE (arg0) == code
12526           && 0 != (tem = fold_truthop (loc, code, type,
12527                                        TREE_OPERAND (arg0, 1), arg1)))
12528         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12529
12530       if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
12531         return tem;
12532
12533       return NULL_TREE;
12534
12535     case TRUTH_ORIF_EXPR:
12536       /* Note that the operands of this must be ints
12537          and their values must be 0 or true.
12538          ("true" is a fixed value perhaps depending on the language.)  */
12539       /* If first arg is constant true, return it.  */
12540       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12541         return fold_convert_loc (loc, type, arg0);
12542     case TRUTH_OR_EXPR:
12543       /* If either arg is constant zero, drop it.  */
12544       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12545         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12546       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12547           /* Preserve sequence points.  */
12548           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12549         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12550       /* If second arg is constant true, result is true, but we must
12551          evaluate first arg.  */
12552       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12553         return omit_one_operand_loc (loc, type, arg1, arg0);
12554       /* Likewise for first arg, but note this only occurs here for
12555          TRUTH_OR_EXPR.  */
12556       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12557         return omit_one_operand_loc (loc, type, arg0, arg1);
12558
12559       /* !X || X is always true.  */
12560       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12561           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12562         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12563       /* X || !X is always true.  */
12564       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12565           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12566         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12567
12568       goto truth_andor;
12569
12570     case TRUTH_XOR_EXPR:
12571       /* If the second arg is constant zero, drop it.  */
12572       if (integer_zerop (arg1))
12573         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12574       /* If the second arg is constant true, this is a logical inversion.  */
12575       if (integer_onep (arg1))
12576         {
12577           /* Only call invert_truthvalue if operand is a truth value.  */
12578           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12579             tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12580           else
12581             tem = invert_truthvalue_loc (loc, arg0);
12582           return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12583         }
12584       /* Identical arguments cancel to zero.  */
12585       if (operand_equal_p (arg0, arg1, 0))
12586         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12587
12588       /* !X ^ X is always true.  */
12589       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12590           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12591         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12592
12593       /* X ^ !X is always true.  */
12594       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12595           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12596         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12597
12598       return NULL_TREE;
12599
12600     case EQ_EXPR:
12601     case NE_EXPR:
12602       tem = fold_comparison (loc, code, type, op0, op1);
12603       if (tem != NULL_TREE)
12604         return tem;
12605
12606       /* bool_var != 0 becomes bool_var. */
12607       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12608           && code == NE_EXPR)
12609         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12610
12611       /* bool_var == 1 becomes bool_var. */
12612       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12613           && code == EQ_EXPR)
12614         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12615
12616       /* bool_var != 1 becomes !bool_var. */
12617       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12618           && code == NE_EXPR)
12619         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12620                             fold_convert_loc (loc, type, arg0));
12621
12622       /* bool_var == 0 becomes !bool_var. */
12623       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12624           && code == EQ_EXPR)
12625         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12626                             fold_convert_loc (loc, type, arg0));
12627
12628       /* !exp != 0 becomes !exp */
12629       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12630           && code == NE_EXPR)
12631         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12632
12633       /* If this is an equality comparison of the address of two non-weak,
12634          unaliased symbols neither of which are extern (since we do not
12635          have access to attributes for externs), then we know the result.  */
12636       if (TREE_CODE (arg0) == ADDR_EXPR
12637           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12638           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12639           && ! lookup_attribute ("alias",
12640                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12641           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12642           && TREE_CODE (arg1) == ADDR_EXPR
12643           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12644           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12645           && ! lookup_attribute ("alias",
12646                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12647           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12648         {
12649           /* We know that we're looking at the address of two
12650              non-weak, unaliased, static _DECL nodes.
12651
12652              It is both wasteful and incorrect to call operand_equal_p
12653              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12654              all we need to do is test pointer equality for the arguments
12655              to the two ADDR_EXPR nodes.  It is incorrect to use
12656              operand_equal_p as that function is NOT equivalent to a
12657              C equality test.  It can in fact return false for two
12658              objects which would test as equal using the C equality
12659              operator.  */
12660           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12661           return constant_boolean_node (equal
12662                                         ? code == EQ_EXPR : code != EQ_EXPR,
12663                                         type);
12664         }
12665
12666       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12667          a MINUS_EXPR of a constant, we can convert it into a comparison with
12668          a revised constant as long as no overflow occurs.  */
12669       if (TREE_CODE (arg1) == INTEGER_CST
12670           && (TREE_CODE (arg0) == PLUS_EXPR
12671               || TREE_CODE (arg0) == MINUS_EXPR)
12672           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12673           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12674                                       ? MINUS_EXPR : PLUS_EXPR,
12675                                       fold_convert_loc (loc, TREE_TYPE (arg0),
12676                                                         arg1),
12677                                       TREE_OPERAND (arg0, 1), 0))
12678           && !TREE_OVERFLOW (tem))
12679         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12680
12681       /* Similarly for a NEGATE_EXPR.  */
12682       if (TREE_CODE (arg0) == NEGATE_EXPR
12683           && TREE_CODE (arg1) == INTEGER_CST
12684           && 0 != (tem = negate_expr (arg1))
12685           && TREE_CODE (tem) == INTEGER_CST
12686           && !TREE_OVERFLOW (tem))
12687         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12688
12689       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12690       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12691           && TREE_CODE (arg1) == INTEGER_CST
12692           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12693         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12694                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12695                                          fold_convert_loc (loc,
12696                                                            TREE_TYPE (arg0),
12697                                                            arg1),
12698                                          TREE_OPERAND (arg0, 1)));
12699
12700       /* Transform comparisons of the form X +- Y CMP X to Y CMP 0.  */
12701       if ((TREE_CODE (arg0) == PLUS_EXPR
12702            || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12703            || TREE_CODE (arg0) == MINUS_EXPR)
12704           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12705           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12706               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12707         {
12708           tree val = TREE_OPERAND (arg0, 1);
12709           return omit_two_operands_loc (loc, type,
12710                                     fold_build2_loc (loc, code, type,
12711                                                  val,
12712                                                  build_int_cst (TREE_TYPE (val),
12713                                                                 0)),
12714                                     TREE_OPERAND (arg0, 0), arg1);
12715         }
12716
12717       /* Transform comparisons of the form C - X CMP X if C % 2 == 1.  */
12718       if (TREE_CODE (arg0) == MINUS_EXPR
12719           && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12720           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
12721           && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12722         {
12723           return omit_two_operands_loc (loc, type,
12724                                     code == NE_EXPR
12725                                     ? boolean_true_node : boolean_false_node,
12726                                     TREE_OPERAND (arg0, 1), arg1);
12727         }
12728
12729       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12730          for !=.  Don't do this for ordered comparisons due to overflow.  */
12731       if (TREE_CODE (arg0) == MINUS_EXPR
12732           && integer_zerop (arg1))
12733         return fold_build2_loc (loc, code, type,
12734                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12735
12736       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12737       if (TREE_CODE (arg0) == ABS_EXPR
12738           && (integer_zerop (arg1) || real_zerop (arg1)))
12739         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12740
12741       /* If this is an EQ or NE comparison with zero and ARG0 is
12742          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12743          two operations, but the latter can be done in one less insn
12744          on machines that have only two-operand insns or on which a
12745          constant cannot be the first operand.  */
12746       if (TREE_CODE (arg0) == BIT_AND_EXPR
12747           && integer_zerop (arg1))
12748         {
12749           tree arg00 = TREE_OPERAND (arg0, 0);
12750           tree arg01 = TREE_OPERAND (arg0, 1);
12751           if (TREE_CODE (arg00) == LSHIFT_EXPR
12752               && integer_onep (TREE_OPERAND (arg00, 0)))
12753             {
12754               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12755                                       arg01, TREE_OPERAND (arg00, 1));
12756               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12757                                  build_int_cst (TREE_TYPE (arg0), 1));
12758               return fold_build2_loc (loc, code, type,
12759                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12760                                   arg1);
12761             }
12762           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12763                    && integer_onep (TREE_OPERAND (arg01, 0)))
12764             {
12765               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12766                                       arg00, TREE_OPERAND (arg01, 1));
12767               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12768                                  build_int_cst (TREE_TYPE (arg0), 1));
12769               return fold_build2_loc (loc, code, type,
12770                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12771                                   arg1);
12772             }
12773         }
12774
12775       /* If this is an NE or EQ comparison of zero against the result of a
12776          signed MOD operation whose second operand is a power of 2, make
12777          the MOD operation unsigned since it is simpler and equivalent.  */
12778       if (integer_zerop (arg1)
12779           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12780           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12781               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12782               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12783               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12784           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12785         {
12786           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12787           tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12788                                      fold_convert_loc (loc, newtype,
12789                                                        TREE_OPERAND (arg0, 0)),
12790                                      fold_convert_loc (loc, newtype,
12791                                                        TREE_OPERAND (arg0, 1)));
12792
12793           return fold_build2_loc (loc, code, type, newmod,
12794                               fold_convert_loc (loc, newtype, arg1));
12795         }
12796
12797       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12798          C1 is a valid shift constant, and C2 is a power of two, i.e.
12799          a single bit.  */
12800       if (TREE_CODE (arg0) == BIT_AND_EXPR
12801           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12802           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12803              == INTEGER_CST
12804           && integer_pow2p (TREE_OPERAND (arg0, 1))
12805           && integer_zerop (arg1))
12806         {
12807           tree itype = TREE_TYPE (arg0);
12808           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12809           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12810
12811           /* Check for a valid shift count.  */
12812           if (TREE_INT_CST_HIGH (arg001) == 0
12813               && TREE_INT_CST_LOW (arg001) < prec)
12814             {
12815               tree arg01 = TREE_OPERAND (arg0, 1);
12816               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12817               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12818               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12819                  can be rewritten as (X & (C2 << C1)) != 0.  */
12820               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12821                 {
12822                   tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12823                   tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12824                   return fold_build2_loc (loc, code, type, tem, arg1);
12825                 }
12826               /* Otherwise, for signed (arithmetic) shifts,
12827                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12828                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12829               else if (!TYPE_UNSIGNED (itype))
12830                 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12831                                     arg000, build_int_cst (itype, 0));
12832               /* Otherwise, of unsigned (logical) shifts,
12833                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12834                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12835               else
12836                 return omit_one_operand_loc (loc, type,
12837                                          code == EQ_EXPR ? integer_one_node
12838                                                          : integer_zero_node,
12839                                          arg000);
12840             }
12841         }
12842
12843       /* If this is an NE comparison of zero with an AND of one, remove the
12844          comparison since the AND will give the correct value.  */
12845       if (code == NE_EXPR
12846           && integer_zerop (arg1)
12847           && TREE_CODE (arg0) == BIT_AND_EXPR
12848           && integer_onep (TREE_OPERAND (arg0, 1)))
12849         return fold_convert_loc (loc, type, arg0);
12850
12851       /* If we have (A & C) == C where C is a power of 2, convert this into
12852          (A & C) != 0.  Similarly for NE_EXPR.  */
12853       if (TREE_CODE (arg0) == BIT_AND_EXPR
12854           && integer_pow2p (TREE_OPERAND (arg0, 1))
12855           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12856         return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12857                             arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12858                                                     integer_zero_node));
12859
12860       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12861          bit, then fold the expression into A < 0 or A >= 0.  */
12862       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12863       if (tem)
12864         return tem;
12865
12866       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12867          Similarly for NE_EXPR.  */
12868       if (TREE_CODE (arg0) == BIT_AND_EXPR
12869           && TREE_CODE (arg1) == INTEGER_CST
12870           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12871         {
12872           tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12873                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12874                                    TREE_OPERAND (arg0, 1));
12875           tree dandnotc = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12876                                        arg1, notc);
12877           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12878           if (integer_nonzerop (dandnotc))
12879             return omit_one_operand_loc (loc, type, rslt, arg0);
12880         }
12881
12882       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12883          Similarly for NE_EXPR.  */
12884       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12885           && TREE_CODE (arg1) == INTEGER_CST
12886           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12887         {
12888           tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12889           tree candnotd = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12890                                        TREE_OPERAND (arg0, 1), notd);
12891           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12892           if (integer_nonzerop (candnotd))
12893             return omit_one_operand_loc (loc, type, rslt, arg0);
12894         }
12895
12896       /* If this is a comparison of a field, we may be able to simplify it.  */
12897       if ((TREE_CODE (arg0) == COMPONENT_REF
12898            || TREE_CODE (arg0) == BIT_FIELD_REF)
12899           /* Handle the constant case even without -O
12900              to make sure the warnings are given.  */
12901           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12902         {
12903           t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12904           if (t1)
12905             return t1;
12906         }
12907
12908       /* Optimize comparisons of strlen vs zero to a compare of the
12909          first character of the string vs zero.  To wit,
12910                 strlen(ptr) == 0   =>  *ptr == 0
12911                 strlen(ptr) != 0   =>  *ptr != 0
12912          Other cases should reduce to one of these two (or a constant)
12913          due to the return value of strlen being unsigned.  */
12914       if (TREE_CODE (arg0) == CALL_EXPR
12915           && integer_zerop (arg1))
12916         {
12917           tree fndecl = get_callee_fndecl (arg0);
12918
12919           if (fndecl
12920               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12921               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12922               && call_expr_nargs (arg0) == 1
12923               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12924             {
12925               tree iref = build_fold_indirect_ref_loc (loc,
12926                                                    CALL_EXPR_ARG (arg0, 0));
12927               return fold_build2_loc (loc, code, type, iref,
12928                                   build_int_cst (TREE_TYPE (iref), 0));
12929             }
12930         }
12931
12932       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12933          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12934       if (TREE_CODE (arg0) == RSHIFT_EXPR
12935           && integer_zerop (arg1)
12936           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12937         {
12938           tree arg00 = TREE_OPERAND (arg0, 0);
12939           tree arg01 = TREE_OPERAND (arg0, 1);
12940           tree itype = TREE_TYPE (arg00);
12941           if (TREE_INT_CST_HIGH (arg01) == 0
12942               && TREE_INT_CST_LOW (arg01)
12943                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12944             {
12945               if (TYPE_UNSIGNED (itype))
12946                 {
12947                   itype = signed_type_for (itype);
12948                   arg00 = fold_convert_loc (loc, itype, arg00);
12949                 }
12950               return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12951                                   type, arg00, build_int_cst (itype, 0));
12952             }
12953         }
12954
12955       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12956       if (integer_zerop (arg1)
12957           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12958         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12959                             TREE_OPERAND (arg0, 1));
12960
12961       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12962       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12963           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12964         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12965                             build_int_cst (TREE_TYPE (arg1), 0));
12966       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12967       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12968           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12969           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12970         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12971                             build_int_cst (TREE_TYPE (arg1), 0));
12972
12973       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12974       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12975           && TREE_CODE (arg1) == INTEGER_CST
12976           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12977         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12978                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12979                                          TREE_OPERAND (arg0, 1), arg1));
12980
12981       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12982          (X & C) == 0 when C is a single bit.  */
12983       if (TREE_CODE (arg0) == BIT_AND_EXPR
12984           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12985           && integer_zerop (arg1)
12986           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12987         {
12988           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12989                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12990                              TREE_OPERAND (arg0, 1));
12991           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12992                               type, tem, arg1);
12993         }
12994
12995       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12996          constant C is a power of two, i.e. a single bit.  */
12997       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12998           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12999           && integer_zerop (arg1)
13000           && integer_pow2p (TREE_OPERAND (arg0, 1))
13001           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13002                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
13003         {
13004           tree arg00 = TREE_OPERAND (arg0, 0);
13005           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
13006                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
13007         }
13008
13009       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
13010          when is C is a power of two, i.e. a single bit.  */
13011       if (TREE_CODE (arg0) == BIT_AND_EXPR
13012           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
13013           && integer_zerop (arg1)
13014           && integer_pow2p (TREE_OPERAND (arg0, 1))
13015           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13016                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
13017         {
13018           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
13019           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
13020                              arg000, TREE_OPERAND (arg0, 1));
13021           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
13022                               tem, build_int_cst (TREE_TYPE (tem), 0));
13023         }
13024
13025       if (integer_zerop (arg1)
13026           && tree_expr_nonzero_p (arg0))
13027         {
13028           tree res = constant_boolean_node (code==NE_EXPR, type);
13029           return omit_one_operand_loc (loc, type, res, arg0);
13030         }
13031
13032       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
13033       if (TREE_CODE (arg0) == NEGATE_EXPR
13034           && TREE_CODE (arg1) == NEGATE_EXPR)
13035         return fold_build2_loc (loc, code, type,
13036                             TREE_OPERAND (arg0, 0),
13037                             TREE_OPERAND (arg1, 0));
13038
13039       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
13040       if (TREE_CODE (arg0) == BIT_AND_EXPR
13041           && TREE_CODE (arg1) == BIT_AND_EXPR)
13042         {
13043           tree arg00 = TREE_OPERAND (arg0, 0);
13044           tree arg01 = TREE_OPERAND (arg0, 1);
13045           tree arg10 = TREE_OPERAND (arg1, 0);
13046           tree arg11 = TREE_OPERAND (arg1, 1);
13047           tree itype = TREE_TYPE (arg0);
13048
13049           if (operand_equal_p (arg01, arg11, 0))
13050             return fold_build2_loc (loc, code, type,
13051                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13052                                              fold_build2_loc (loc,
13053                                                           BIT_XOR_EXPR, itype,
13054                                                           arg00, arg10),
13055                                              arg01),
13056                                 build_int_cst (itype, 0));
13057
13058           if (operand_equal_p (arg01, arg10, 0))
13059             return fold_build2_loc (loc, code, type,
13060                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13061                                              fold_build2_loc (loc,
13062                                                           BIT_XOR_EXPR, itype,
13063                                                           arg00, arg11),
13064                                              arg01),
13065                                 build_int_cst (itype, 0));
13066
13067           if (operand_equal_p (arg00, arg11, 0))
13068             return fold_build2_loc (loc, code, type,
13069                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13070                                              fold_build2_loc (loc,
13071                                                           BIT_XOR_EXPR, itype,
13072                                                           arg01, arg10),
13073                                              arg00),
13074                                 build_int_cst (itype, 0));
13075
13076           if (operand_equal_p (arg00, arg10, 0))
13077             return fold_build2_loc (loc, code, type,
13078                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13079                                              fold_build2_loc (loc,
13080                                                           BIT_XOR_EXPR, itype,
13081                                                           arg01, arg11),
13082                                              arg00),
13083                                 build_int_cst (itype, 0));
13084         }
13085
13086       if (TREE_CODE (arg0) == BIT_XOR_EXPR
13087           && TREE_CODE (arg1) == BIT_XOR_EXPR)
13088         {
13089           tree arg00 = TREE_OPERAND (arg0, 0);
13090           tree arg01 = TREE_OPERAND (arg0, 1);
13091           tree arg10 = TREE_OPERAND (arg1, 0);
13092           tree arg11 = TREE_OPERAND (arg1, 1);
13093           tree itype = TREE_TYPE (arg0);
13094
13095           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
13096              operand_equal_p guarantees no side-effects so we don't need
13097              to use omit_one_operand on Z.  */
13098           if (operand_equal_p (arg01, arg11, 0))
13099             return fold_build2_loc (loc, code, type, arg00, arg10);
13100           if (operand_equal_p (arg01, arg10, 0))
13101             return fold_build2_loc (loc, code, type, arg00, arg11);
13102           if (operand_equal_p (arg00, arg11, 0))
13103             return fold_build2_loc (loc, code, type, arg01, arg10);
13104           if (operand_equal_p (arg00, arg10, 0))
13105             return fold_build2_loc (loc, code, type, arg01, arg11);
13106
13107           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
13108           if (TREE_CODE (arg01) == INTEGER_CST
13109               && TREE_CODE (arg11) == INTEGER_CST)
13110             return fold_build2_loc (loc, code, type,
13111                                 fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00,
13112                                              fold_build2_loc (loc,
13113                                                           BIT_XOR_EXPR, itype,
13114                                                           arg01, arg11)),
13115                                 arg10);
13116         }
13117
13118       /* Attempt to simplify equality/inequality comparisons of complex
13119          values.  Only lower the comparison if the result is known or
13120          can be simplified to a single scalar comparison.  */
13121       if ((TREE_CODE (arg0) == COMPLEX_EXPR
13122            || TREE_CODE (arg0) == COMPLEX_CST)
13123           && (TREE_CODE (arg1) == COMPLEX_EXPR
13124               || TREE_CODE (arg1) == COMPLEX_CST))
13125         {
13126           tree real0, imag0, real1, imag1;
13127           tree rcond, icond;
13128
13129           if (TREE_CODE (arg0) == COMPLEX_EXPR)
13130             {
13131               real0 = TREE_OPERAND (arg0, 0);
13132               imag0 = TREE_OPERAND (arg0, 1);
13133             }
13134           else
13135             {
13136               real0 = TREE_REALPART (arg0);
13137               imag0 = TREE_IMAGPART (arg0);
13138             }
13139
13140           if (TREE_CODE (arg1) == COMPLEX_EXPR)
13141             {
13142               real1 = TREE_OPERAND (arg1, 0);
13143               imag1 = TREE_OPERAND (arg1, 1);
13144             }
13145           else
13146             {
13147               real1 = TREE_REALPART (arg1);
13148               imag1 = TREE_IMAGPART (arg1);
13149             }
13150
13151           rcond = fold_binary_loc (loc, code, type, real0, real1);
13152           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
13153             {
13154               if (integer_zerop (rcond))
13155                 {
13156                   if (code == EQ_EXPR)
13157                     return omit_two_operands_loc (loc, type, boolean_false_node,
13158                                               imag0, imag1);
13159                   return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
13160                 }
13161               else
13162                 {
13163                   if (code == NE_EXPR)
13164                     return omit_two_operands_loc (loc, type, boolean_true_node,
13165                                               imag0, imag1);
13166                   return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
13167                 }
13168             }
13169
13170           icond = fold_binary_loc (loc, code, type, imag0, imag1);
13171           if (icond && TREE_CODE (icond) == INTEGER_CST)
13172             {
13173               if (integer_zerop (icond))
13174                 {
13175                   if (code == EQ_EXPR)
13176                     return omit_two_operands_loc (loc, type, boolean_false_node,
13177                                               real0, real1);
13178                   return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
13179                 }
13180               else
13181                 {
13182                   if (code == NE_EXPR)
13183                     return omit_two_operands_loc (loc, type, boolean_true_node,
13184                                               real0, real1);
13185                   return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
13186                 }
13187             }
13188         }
13189
13190       return NULL_TREE;
13191
13192     case LT_EXPR:
13193     case GT_EXPR:
13194     case LE_EXPR:
13195     case GE_EXPR:
13196       tem = fold_comparison (loc, code, type, op0, op1);
13197       if (tem != NULL_TREE)
13198         return tem;
13199
13200       /* Transform comparisons of the form X +- C CMP X.  */
13201       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
13202           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
13203           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
13204                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
13205               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
13206                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
13207         {
13208           tree arg01 = TREE_OPERAND (arg0, 1);
13209           enum tree_code code0 = TREE_CODE (arg0);
13210           int is_positive;
13211
13212           if (TREE_CODE (arg01) == REAL_CST)
13213             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
13214           else
13215             is_positive = tree_int_cst_sgn (arg01);
13216
13217           /* (X - c) > X becomes false.  */
13218           if (code == GT_EXPR
13219               && ((code0 == MINUS_EXPR && is_positive >= 0)
13220                   || (code0 == PLUS_EXPR && is_positive <= 0)))
13221             {
13222               if (TREE_CODE (arg01) == INTEGER_CST
13223                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13224                 fold_overflow_warning (("assuming signed overflow does not "
13225                                         "occur when assuming that (X - c) > X "
13226                                         "is always false"),
13227                                        WARN_STRICT_OVERFLOW_ALL);
13228               return constant_boolean_node (0, type);
13229             }
13230
13231           /* Likewise (X + c) < X becomes false.  */
13232           if (code == LT_EXPR
13233               && ((code0 == PLUS_EXPR && is_positive >= 0)
13234                   || (code0 == MINUS_EXPR && is_positive <= 0)))
13235             {
13236               if (TREE_CODE (arg01) == INTEGER_CST
13237                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13238                 fold_overflow_warning (("assuming signed overflow does not "
13239                                         "occur when assuming that "
13240                                         "(X + c) < X is always false"),
13241                                        WARN_STRICT_OVERFLOW_ALL);
13242               return constant_boolean_node (0, type);
13243             }
13244
13245           /* Convert (X - c) <= X to true.  */
13246           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13247               && code == LE_EXPR
13248               && ((code0 == MINUS_EXPR && is_positive >= 0)
13249                   || (code0 == PLUS_EXPR && is_positive <= 0)))
13250             {
13251               if (TREE_CODE (arg01) == INTEGER_CST
13252                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13253                 fold_overflow_warning (("assuming signed overflow does not "
13254                                         "occur when assuming that "
13255                                         "(X - c) <= X is always true"),
13256                                        WARN_STRICT_OVERFLOW_ALL);
13257               return constant_boolean_node (1, type);
13258             }
13259
13260           /* Convert (X + c) >= X to true.  */
13261           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13262               && code == GE_EXPR
13263               && ((code0 == PLUS_EXPR && is_positive >= 0)
13264                   || (code0 == MINUS_EXPR && is_positive <= 0)))
13265             {
13266               if (TREE_CODE (arg01) == INTEGER_CST
13267                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13268                 fold_overflow_warning (("assuming signed overflow does not "
13269                                         "occur when assuming that "
13270                                         "(X + c) >= X is always true"),
13271                                        WARN_STRICT_OVERFLOW_ALL);
13272               return constant_boolean_node (1, type);
13273             }
13274
13275           if (TREE_CODE (arg01) == INTEGER_CST)
13276             {
13277               /* Convert X + c > X and X - c < X to true for integers.  */
13278               if (code == GT_EXPR
13279                   && ((code0 == PLUS_EXPR && is_positive > 0)
13280                       || (code0 == MINUS_EXPR && is_positive < 0)))
13281                 {
13282                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13283                     fold_overflow_warning (("assuming signed overflow does "
13284                                             "not occur when assuming that "
13285                                             "(X + c) > X is always true"),
13286                                            WARN_STRICT_OVERFLOW_ALL);
13287                   return constant_boolean_node (1, type);
13288                 }
13289
13290               if (code == LT_EXPR
13291                   && ((code0 == MINUS_EXPR && is_positive > 0)
13292                       || (code0 == PLUS_EXPR && is_positive < 0)))
13293                 {
13294                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13295                     fold_overflow_warning (("assuming signed overflow does "
13296                                             "not occur when assuming that "
13297                                             "(X - c) < X is always true"),
13298                                            WARN_STRICT_OVERFLOW_ALL);
13299                   return constant_boolean_node (1, type);
13300                 }
13301
13302               /* Convert X + c <= X and X - c >= X to false for integers.  */
13303               if (code == LE_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 false"),
13311                                            WARN_STRICT_OVERFLOW_ALL);
13312                   return constant_boolean_node (0, type);
13313                 }
13314
13315               if (code == GE_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 false"),
13323                                            WARN_STRICT_OVERFLOW_ALL);
13324                   return constant_boolean_node (0, type);
13325                 }
13326             }
13327         }
13328
13329       /* Comparisons with the highest or lowest possible integer of
13330          the specified precision will have known values.  */
13331       {
13332         tree arg1_type = TREE_TYPE (arg1);
13333         unsigned int width = TYPE_PRECISION (arg1_type);
13334
13335         if (TREE_CODE (arg1) == INTEGER_CST
13336             && width <= 2 * HOST_BITS_PER_WIDE_INT
13337             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
13338           {
13339             HOST_WIDE_INT signed_max_hi;
13340             unsigned HOST_WIDE_INT signed_max_lo;
13341             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
13342
13343             if (width <= HOST_BITS_PER_WIDE_INT)
13344               {
13345                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13346                                 - 1;
13347                 signed_max_hi = 0;
13348                 max_hi = 0;
13349
13350                 if (TYPE_UNSIGNED (arg1_type))
13351                   {
13352                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13353                     min_lo = 0;
13354                     min_hi = 0;
13355                   }
13356                 else
13357                   {
13358                     max_lo = signed_max_lo;
13359                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
13360                     min_hi = -1;
13361                   }
13362               }
13363             else
13364               {
13365                 width -= HOST_BITS_PER_WIDE_INT;
13366                 signed_max_lo = -1;
13367                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13368                                 - 1;
13369                 max_lo = -1;
13370                 min_lo = 0;
13371
13372                 if (TYPE_UNSIGNED (arg1_type))
13373                   {
13374                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13375                     min_hi = 0;
13376                   }
13377                 else
13378                   {
13379                     max_hi = signed_max_hi;
13380                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
13381                   }
13382               }
13383
13384             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
13385                 && TREE_INT_CST_LOW (arg1) == max_lo)
13386               switch (code)
13387                 {
13388                 case GT_EXPR:
13389                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13390
13391                 case GE_EXPR:
13392                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13393
13394                 case LE_EXPR:
13395                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13396
13397                 case LT_EXPR:
13398                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13399
13400                 /* The GE_EXPR and LT_EXPR cases above are not normally
13401                    reached because of previous transformations.  */
13402
13403                 default:
13404                   break;
13405                 }
13406             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13407                      == max_hi
13408                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
13409               switch (code)
13410                 {
13411                 case GT_EXPR:
13412                   arg1 = const_binop (PLUS_EXPR, arg1,
13413                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
13414                   return fold_build2_loc (loc, EQ_EXPR, type,
13415                                       fold_convert_loc (loc,
13416                                                         TREE_TYPE (arg1), arg0),
13417                                       arg1);
13418                 case LE_EXPR:
13419                   arg1 = const_binop (PLUS_EXPR, arg1,
13420                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
13421                   return fold_build2_loc (loc, NE_EXPR, type,
13422                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13423                                                         arg0),
13424                                       arg1);
13425                 default:
13426                   break;
13427                 }
13428             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13429                      == min_hi
13430                      && TREE_INT_CST_LOW (arg1) == min_lo)
13431               switch (code)
13432                 {
13433                 case LT_EXPR:
13434                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13435
13436                 case LE_EXPR:
13437                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13438
13439                 case GE_EXPR:
13440                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13441
13442                 case GT_EXPR:
13443                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13444
13445                 default:
13446                   break;
13447                 }
13448             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13449                      == min_hi
13450                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
13451               switch (code)
13452                 {
13453                 case GE_EXPR:
13454                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13455                   return fold_build2_loc (loc, NE_EXPR, type,
13456                                       fold_convert_loc (loc,
13457                                                         TREE_TYPE (arg1), arg0),
13458                                       arg1);
13459                 case LT_EXPR:
13460                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13461                   return fold_build2_loc (loc, EQ_EXPR, type,
13462                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13463                                                         arg0),
13464                                       arg1);
13465                 default:
13466                   break;
13467                 }
13468
13469             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13470                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
13471                      && TYPE_UNSIGNED (arg1_type)
13472                      /* We will flip the signedness of the comparison operator
13473                         associated with the mode of arg1, so the sign bit is
13474                         specified by this mode.  Check that arg1 is the signed
13475                         max associated with this sign bit.  */
13476                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13477                      /* signed_type does not work on pointer types.  */
13478                      && INTEGRAL_TYPE_P (arg1_type))
13479               {
13480                 /* The following case also applies to X < signed_max+1
13481                    and X >= signed_max+1 because previous transformations.  */
13482                 if (code == LE_EXPR || code == GT_EXPR)
13483                   {
13484                     tree st;
13485                     st = signed_type_for (TREE_TYPE (arg1));
13486                     return fold_build2_loc (loc,
13487                                         code == LE_EXPR ? GE_EXPR : LT_EXPR,
13488                                         type, fold_convert_loc (loc, st, arg0),
13489                                         build_int_cst (st, 0));
13490                   }
13491               }
13492           }
13493       }
13494
13495       /* If we are comparing an ABS_EXPR with a constant, we can
13496          convert all the cases into explicit comparisons, but they may
13497          well not be faster than doing the ABS and one comparison.
13498          But ABS (X) <= C is a range comparison, which becomes a subtraction
13499          and a comparison, and is probably faster.  */
13500       if (code == LE_EXPR
13501           && TREE_CODE (arg1) == INTEGER_CST
13502           && TREE_CODE (arg0) == ABS_EXPR
13503           && ! TREE_SIDE_EFFECTS (arg0)
13504           && (0 != (tem = negate_expr (arg1)))
13505           && TREE_CODE (tem) == INTEGER_CST
13506           && !TREE_OVERFLOW (tem))
13507         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13508                             build2 (GE_EXPR, type,
13509                                     TREE_OPERAND (arg0, 0), tem),
13510                             build2 (LE_EXPR, type,
13511                                     TREE_OPERAND (arg0, 0), arg1));
13512
13513       /* Convert ABS_EXPR<x> >= 0 to true.  */
13514       strict_overflow_p = false;
13515       if (code == GE_EXPR
13516           && (integer_zerop (arg1)
13517               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13518                   && real_zerop (arg1)))
13519           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13520         {
13521           if (strict_overflow_p)
13522             fold_overflow_warning (("assuming signed overflow does not occur "
13523                                     "when simplifying comparison of "
13524                                     "absolute value and zero"),
13525                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13526           return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13527         }
13528
13529       /* Convert ABS_EXPR<x> < 0 to false.  */
13530       strict_overflow_p = false;
13531       if (code == LT_EXPR
13532           && (integer_zerop (arg1) || real_zerop (arg1))
13533           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13534         {
13535           if (strict_overflow_p)
13536             fold_overflow_warning (("assuming signed overflow does not occur "
13537                                     "when simplifying comparison of "
13538                                     "absolute value and zero"),
13539                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13540           return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13541         }
13542
13543       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13544          and similarly for >= into !=.  */
13545       if ((code == LT_EXPR || code == GE_EXPR)
13546           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13547           && TREE_CODE (arg1) == LSHIFT_EXPR
13548           && integer_onep (TREE_OPERAND (arg1, 0)))
13549         {
13550           tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13551                         build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13552                                 TREE_OPERAND (arg1, 1)),
13553                         build_int_cst (TREE_TYPE (arg0), 0));
13554           goto fold_binary_exit;
13555         }
13556
13557       if ((code == LT_EXPR || code == GE_EXPR)
13558           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13559           && CONVERT_EXPR_P (arg1)
13560           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13561           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13562         {
13563           tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13564                         fold_convert_loc (loc, TREE_TYPE (arg0),
13565                                           build2 (RSHIFT_EXPR,
13566                                                   TREE_TYPE (arg0), arg0,
13567                                                   TREE_OPERAND (TREE_OPERAND (arg1, 0),
13568                                                                 1))),
13569                         build_int_cst (TREE_TYPE (arg0), 0));
13570           goto fold_binary_exit;
13571         }
13572
13573       return NULL_TREE;
13574
13575     case UNORDERED_EXPR:
13576     case ORDERED_EXPR:
13577     case UNLT_EXPR:
13578     case UNLE_EXPR:
13579     case UNGT_EXPR:
13580     case UNGE_EXPR:
13581     case UNEQ_EXPR:
13582     case LTGT_EXPR:
13583       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13584         {
13585           t1 = fold_relational_const (code, type, arg0, arg1);
13586           if (t1 != NULL_TREE)
13587             return t1;
13588         }
13589
13590       /* If the first operand is NaN, the result is constant.  */
13591       if (TREE_CODE (arg0) == REAL_CST
13592           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13593           && (code != LTGT_EXPR || ! flag_trapping_math))
13594         {
13595           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13596                ? integer_zero_node
13597                : integer_one_node;
13598           return omit_one_operand_loc (loc, type, t1, arg1);
13599         }
13600
13601       /* If the second operand is NaN, the result is constant.  */
13602       if (TREE_CODE (arg1) == REAL_CST
13603           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13604           && (code != LTGT_EXPR || ! flag_trapping_math))
13605         {
13606           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13607                ? integer_zero_node
13608                : integer_one_node;
13609           return omit_one_operand_loc (loc, type, t1, arg0);
13610         }
13611
13612       /* Simplify unordered comparison of something with itself.  */
13613       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13614           && operand_equal_p (arg0, arg1, 0))
13615         return constant_boolean_node (1, type);
13616
13617       if (code == LTGT_EXPR
13618           && !flag_trapping_math
13619           && operand_equal_p (arg0, arg1, 0))
13620         return constant_boolean_node (0, type);
13621
13622       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13623       {
13624         tree targ0 = strip_float_extensions (arg0);
13625         tree targ1 = strip_float_extensions (arg1);
13626         tree newtype = TREE_TYPE (targ0);
13627
13628         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13629           newtype = TREE_TYPE (targ1);
13630
13631         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13632           return fold_build2_loc (loc, code, type,
13633                               fold_convert_loc (loc, newtype, targ0),
13634                               fold_convert_loc (loc, newtype, targ1));
13635       }
13636
13637       return NULL_TREE;
13638
13639     case COMPOUND_EXPR:
13640       /* When pedantic, a compound expression can be neither an lvalue
13641          nor an integer constant expression.  */
13642       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13643         return NULL_TREE;
13644       /* Don't let (0, 0) be null pointer constant.  */
13645       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13646                                  : fold_convert_loc (loc, type, arg1);
13647       return pedantic_non_lvalue_loc (loc, tem);
13648
13649     case COMPLEX_EXPR:
13650       if ((TREE_CODE (arg0) == REAL_CST
13651            && TREE_CODE (arg1) == REAL_CST)
13652           || (TREE_CODE (arg0) == INTEGER_CST
13653               && TREE_CODE (arg1) == INTEGER_CST))
13654         return build_complex (type, arg0, arg1);
13655       return NULL_TREE;
13656
13657     case ASSERT_EXPR:
13658       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13659       gcc_unreachable ();
13660
13661     default:
13662       return NULL_TREE;
13663     } /* switch (code) */
13664  fold_binary_exit:
13665   protected_set_expr_location (tem, loc);
13666   return tem;
13667 }
13668
13669 /* Callback for walk_tree, looking for LABEL_EXPR.  Return *TP if it is
13670    a LABEL_EXPR; otherwise return NULL_TREE.  Do not check the subtrees
13671    of GOTO_EXPR.  */
13672
13673 static tree
13674 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13675 {
13676   switch (TREE_CODE (*tp))
13677     {
13678     case LABEL_EXPR:
13679       return *tp;
13680
13681     case GOTO_EXPR:
13682       *walk_subtrees = 0;
13683
13684       /* ... fall through ...  */
13685
13686     default:
13687       return NULL_TREE;
13688     }
13689 }
13690
13691 /* Return whether the sub-tree ST contains a label which is accessible from
13692    outside the sub-tree.  */
13693
13694 static bool
13695 contains_label_p (tree st)
13696 {
13697   return
13698    (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13699 }
13700
13701 /* Fold a ternary expression of code CODE and type TYPE with operands
13702    OP0, OP1, and OP2.  Return the folded expression if folding is
13703    successful.  Otherwise, return NULL_TREE.  */
13704
13705 tree
13706 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13707               tree op0, tree op1, tree op2)
13708 {
13709   tree tem;
13710   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13711   enum tree_code_class kind = TREE_CODE_CLASS (code);
13712
13713   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13714               && TREE_CODE_LENGTH (code) == 3);
13715
13716   /* Strip any conversions that don't change the mode.  This is safe
13717      for every expression, except for a comparison expression because
13718      its signedness is derived from its operands.  So, in the latter
13719      case, only strip conversions that don't change the signedness.
13720
13721      Note that this is done as an internal manipulation within the
13722      constant folder, in order to find the simplest representation of
13723      the arguments so that their form can be studied.  In any cases,
13724      the appropriate type conversions should be put back in the tree
13725      that will get out of the constant folder.  */
13726   if (op0)
13727     {
13728       arg0 = op0;
13729       STRIP_NOPS (arg0);
13730     }
13731
13732   if (op1)
13733     {
13734       arg1 = op1;
13735       STRIP_NOPS (arg1);
13736     }
13737
13738   switch (code)
13739     {
13740     case COMPONENT_REF:
13741       if (TREE_CODE (arg0) == CONSTRUCTOR
13742           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13743         {
13744           unsigned HOST_WIDE_INT idx;
13745           tree field, value;
13746           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13747             if (field == arg1)
13748               return value;
13749         }
13750       return NULL_TREE;
13751
13752     case COND_EXPR:
13753       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13754          so all simple results must be passed through pedantic_non_lvalue.  */
13755       if (TREE_CODE (arg0) == INTEGER_CST)
13756         {
13757           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13758           tem = integer_zerop (arg0) ? op2 : op1;
13759           /* Only optimize constant conditions when the selected branch
13760              has the same type as the COND_EXPR.  This avoids optimizing
13761              away "c ? x : throw", where the throw has a void type.
13762              Avoid throwing away that operand which contains label.  */
13763           if ((!TREE_SIDE_EFFECTS (unused_op)
13764                || !contains_label_p (unused_op))
13765               && (! VOID_TYPE_P (TREE_TYPE (tem))
13766                   || VOID_TYPE_P (type)))
13767             return pedantic_non_lvalue_loc (loc, tem);
13768           return NULL_TREE;
13769         }
13770       if (operand_equal_p (arg1, op2, 0))
13771         return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13772
13773       /* If we have A op B ? A : C, we may be able to convert this to a
13774          simpler expression, depending on the operation and the values
13775          of B and C.  Signed zeros prevent all of these transformations,
13776          for reasons given above each one.
13777
13778          Also try swapping the arguments and inverting the conditional.  */
13779       if (COMPARISON_CLASS_P (arg0)
13780           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13781                                              arg1, TREE_OPERAND (arg0, 1))
13782           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13783         {
13784           tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13785           if (tem)
13786             return tem;
13787         }
13788
13789       if (COMPARISON_CLASS_P (arg0)
13790           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13791                                              op2,
13792                                              TREE_OPERAND (arg0, 1))
13793           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13794         {
13795           tem = fold_truth_not_expr (loc, arg0);
13796           if (tem && COMPARISON_CLASS_P (tem))
13797             {
13798               tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13799               if (tem)
13800                 return tem;
13801             }
13802         }
13803
13804       /* If the second operand is simpler than the third, swap them
13805          since that produces better jump optimization results.  */
13806       if (truth_value_p (TREE_CODE (arg0))
13807           && tree_swap_operands_p (op1, op2, false))
13808         {
13809           /* See if this can be inverted.  If it can't, possibly because
13810              it was a floating-point inequality comparison, don't do
13811              anything.  */
13812           tem = fold_truth_not_expr (loc, arg0);
13813           if (tem)
13814             return fold_build3_loc (loc, code, type, tem, op2, op1);
13815         }
13816
13817       /* Convert A ? 1 : 0 to simply A.  */
13818       if (integer_onep (op1)
13819           && integer_zerop (op2)
13820           /* If we try to convert OP0 to our type, the
13821              call to fold will try to move the conversion inside
13822              a COND, which will recurse.  In that case, the COND_EXPR
13823              is probably the best choice, so leave it alone.  */
13824           && type == TREE_TYPE (arg0))
13825         return pedantic_non_lvalue_loc (loc, arg0);
13826
13827       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13828          over COND_EXPR in cases such as floating point comparisons.  */
13829       if (integer_zerop (op1)
13830           && integer_onep (op2)
13831           && truth_value_p (TREE_CODE (arg0)))
13832         return pedantic_non_lvalue_loc (loc,
13833                                     fold_convert_loc (loc, type,
13834                                               invert_truthvalue_loc (loc,
13835                                                                      arg0)));
13836
13837       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13838       if (TREE_CODE (arg0) == LT_EXPR
13839           && integer_zerop (TREE_OPERAND (arg0, 1))
13840           && integer_zerop (op2)
13841           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13842         {
13843           /* sign_bit_p only checks ARG1 bits within A's precision.
13844              If <sign bit of A> has wider type than A, bits outside
13845              of A's precision in <sign bit of A> need to be checked.
13846              If they are all 0, this optimization needs to be done
13847              in unsigned A's type, if they are all 1 in signed A's type,
13848              otherwise this can't be done.  */
13849           if (TYPE_PRECISION (TREE_TYPE (tem))
13850               < TYPE_PRECISION (TREE_TYPE (arg1))
13851               && TYPE_PRECISION (TREE_TYPE (tem))
13852                  < TYPE_PRECISION (type))
13853             {
13854               unsigned HOST_WIDE_INT mask_lo;
13855               HOST_WIDE_INT mask_hi;
13856               int inner_width, outer_width;
13857               tree tem_type;
13858
13859               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13860               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13861               if (outer_width > TYPE_PRECISION (type))
13862                 outer_width = TYPE_PRECISION (type);
13863
13864               if (outer_width > HOST_BITS_PER_WIDE_INT)
13865                 {
13866                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13867                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13868                   mask_lo = -1;
13869                 }
13870               else
13871                 {
13872                   mask_hi = 0;
13873                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13874                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13875                 }
13876               if (inner_width > HOST_BITS_PER_WIDE_INT)
13877                 {
13878                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13879                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13880                   mask_lo = 0;
13881                 }
13882               else
13883                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13884                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13885
13886               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13887                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13888                 {
13889                   tem_type = signed_type_for (TREE_TYPE (tem));
13890                   tem = fold_convert_loc (loc, tem_type, tem);
13891                 }
13892               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13893                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13894                 {
13895                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13896                   tem = fold_convert_loc (loc, tem_type, tem);
13897                 }
13898               else
13899                 tem = NULL;
13900             }
13901
13902           if (tem)
13903             return
13904               fold_convert_loc (loc, type,
13905                                 fold_build2_loc (loc, BIT_AND_EXPR,
13906                                              TREE_TYPE (tem), tem,
13907                                              fold_convert_loc (loc,
13908                                                                TREE_TYPE (tem),
13909                                                                arg1)));
13910         }
13911
13912       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13913          already handled above.  */
13914       if (TREE_CODE (arg0) == BIT_AND_EXPR
13915           && integer_onep (TREE_OPERAND (arg0, 1))
13916           && integer_zerop (op2)
13917           && integer_pow2p (arg1))
13918         {
13919           tree tem = TREE_OPERAND (arg0, 0);
13920           STRIP_NOPS (tem);
13921           if (TREE_CODE (tem) == RSHIFT_EXPR
13922               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13923               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13924                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13925             return fold_build2_loc (loc, BIT_AND_EXPR, type,
13926                                 TREE_OPERAND (tem, 0), arg1);
13927         }
13928
13929       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13930          is probably obsolete because the first operand should be a
13931          truth value (that's why we have the two cases above), but let's
13932          leave it in until we can confirm this for all front-ends.  */
13933       if (integer_zerop (op2)
13934           && TREE_CODE (arg0) == NE_EXPR
13935           && integer_zerop (TREE_OPERAND (arg0, 1))
13936           && integer_pow2p (arg1)
13937           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13938           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13939                               arg1, OEP_ONLY_CONST))
13940         return pedantic_non_lvalue_loc (loc,
13941                                     fold_convert_loc (loc, type,
13942                                                       TREE_OPERAND (arg0, 0)));
13943
13944       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13945       if (integer_zerop (op2)
13946           && truth_value_p (TREE_CODE (arg0))
13947           && truth_value_p (TREE_CODE (arg1)))
13948         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13949                             fold_convert_loc (loc, type, arg0),
13950                             arg1);
13951
13952       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13953       if (integer_onep (op2)
13954           && truth_value_p (TREE_CODE (arg0))
13955           && truth_value_p (TREE_CODE (arg1)))
13956         {
13957           /* Only perform transformation if ARG0 is easily inverted.  */
13958           tem = fold_truth_not_expr (loc, arg0);
13959           if (tem)
13960             return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13961                                 fold_convert_loc (loc, type, tem),
13962                                 arg1);
13963         }
13964
13965       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13966       if (integer_zerop (arg1)
13967           && truth_value_p (TREE_CODE (arg0))
13968           && truth_value_p (TREE_CODE (op2)))
13969         {
13970           /* Only perform transformation if ARG0 is easily inverted.  */
13971           tem = fold_truth_not_expr (loc, arg0);
13972           if (tem)
13973             return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13974                                 fold_convert_loc (loc, type, tem),
13975                                 op2);
13976         }
13977
13978       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13979       if (integer_onep (arg1)
13980           && truth_value_p (TREE_CODE (arg0))
13981           && truth_value_p (TREE_CODE (op2)))
13982         return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13983                             fold_convert_loc (loc, type, arg0),
13984                             op2);
13985
13986       return NULL_TREE;
13987
13988     case CALL_EXPR:
13989       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13990          of fold_ternary on them.  */
13991       gcc_unreachable ();
13992
13993     case BIT_FIELD_REF:
13994       if ((TREE_CODE (arg0) == VECTOR_CST
13995            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13996           && type == TREE_TYPE (TREE_TYPE (arg0)))
13997         {
13998           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13999           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
14000
14001           if (width != 0
14002               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
14003               && (idx % width) == 0
14004               && (idx = idx / width)
14005                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
14006             {
14007               tree elements = NULL_TREE;
14008
14009               if (TREE_CODE (arg0) == VECTOR_CST)
14010                 elements = TREE_VECTOR_CST_ELTS (arg0);
14011               else
14012                 {
14013                   unsigned HOST_WIDE_INT idx;
14014                   tree value;
14015
14016                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
14017                     elements = tree_cons (NULL_TREE, value, elements);
14018                 }
14019               while (idx-- > 0 && elements)
14020                 elements = TREE_CHAIN (elements);
14021               if (elements)
14022                 return TREE_VALUE (elements);
14023               else
14024                 return fold_convert_loc (loc, type, integer_zero_node);
14025             }
14026         }
14027
14028       /* A bit-field-ref that referenced the full argument can be stripped.  */
14029       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
14030           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
14031           && integer_zerop (op2))
14032         return fold_convert_loc (loc, type, arg0);
14033
14034       return NULL_TREE;
14035
14036     default:
14037       return NULL_TREE;
14038     } /* switch (code) */
14039 }
14040
14041 /* Perform constant folding and related simplification of EXPR.
14042    The related simplifications include x*1 => x, x*0 => 0, etc.,
14043    and application of the associative law.
14044    NOP_EXPR conversions may be removed freely (as long as we
14045    are careful not to change the type of the overall expression).
14046    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
14047    but we can constant-fold them if they have constant operands.  */
14048
14049 #ifdef ENABLE_FOLD_CHECKING
14050 # define fold(x) fold_1 (x)
14051 static tree fold_1 (tree);
14052 static
14053 #endif
14054 tree
14055 fold (tree expr)
14056 {
14057   const tree t = expr;
14058   enum tree_code code = TREE_CODE (t);
14059   enum tree_code_class kind = TREE_CODE_CLASS (code);
14060   tree tem;
14061   location_t loc = EXPR_LOCATION (expr);
14062
14063   /* Return right away if a constant.  */
14064   if (kind == tcc_constant)
14065     return t;
14066
14067   /* CALL_EXPR-like objects with variable numbers of operands are
14068      treated specially.  */
14069   if (kind == tcc_vl_exp)
14070     {
14071       if (code == CALL_EXPR)
14072         {
14073           tem = fold_call_expr (loc, expr, false);
14074           return tem ? tem : expr;
14075         }
14076       return expr;
14077     }
14078
14079   if (IS_EXPR_CODE_CLASS (kind))
14080     {
14081       tree type = TREE_TYPE (t);
14082       tree op0, op1, op2;
14083
14084       switch (TREE_CODE_LENGTH (code))
14085         {
14086         case 1:
14087           op0 = TREE_OPERAND (t, 0);
14088           tem = fold_unary_loc (loc, code, type, op0);
14089           return tem ? tem : expr;
14090         case 2:
14091           op0 = TREE_OPERAND (t, 0);
14092           op1 = TREE_OPERAND (t, 1);
14093           tem = fold_binary_loc (loc, code, type, op0, op1);
14094           return tem ? tem : expr;
14095         case 3:
14096           op0 = TREE_OPERAND (t, 0);
14097           op1 = TREE_OPERAND (t, 1);
14098           op2 = TREE_OPERAND (t, 2);
14099           tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14100           return tem ? tem : expr;
14101         default:
14102           break;
14103         }
14104     }
14105
14106   switch (code)
14107     {
14108     case ARRAY_REF:
14109       {
14110         tree op0 = TREE_OPERAND (t, 0);
14111         tree op1 = TREE_OPERAND (t, 1);
14112
14113         if (TREE_CODE (op1) == INTEGER_CST
14114             && TREE_CODE (op0) == CONSTRUCTOR
14115             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
14116           {
14117             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
14118             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
14119             unsigned HOST_WIDE_INT begin = 0;
14120
14121             /* Find a matching index by means of a binary search.  */
14122             while (begin != end)
14123               {
14124                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
14125                 tree index = VEC_index (constructor_elt, elts, middle)->index;
14126
14127                 if (TREE_CODE (index) == INTEGER_CST
14128                     && tree_int_cst_lt (index, op1))
14129                   begin = middle + 1;
14130                 else if (TREE_CODE (index) == INTEGER_CST
14131                          && tree_int_cst_lt (op1, index))
14132                   end = middle;
14133                 else if (TREE_CODE (index) == RANGE_EXPR
14134                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
14135                   begin = middle + 1;
14136                 else if (TREE_CODE (index) == RANGE_EXPR
14137                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
14138                   end = middle;
14139                 else
14140                   return VEC_index (constructor_elt, elts, middle)->value;
14141               }
14142           }
14143
14144         return t;
14145       }
14146
14147     case CONST_DECL:
14148       return fold (DECL_INITIAL (t));
14149
14150     default:
14151       return t;
14152     } /* switch (code) */
14153 }
14154
14155 #ifdef ENABLE_FOLD_CHECKING
14156 #undef fold
14157
14158 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
14159 static void fold_check_failed (const_tree, const_tree);
14160 void print_fold_checksum (const_tree);
14161
14162 /* When --enable-checking=fold, compute a digest of expr before
14163    and after actual fold call to see if fold did not accidentally
14164    change original expr.  */
14165
14166 tree
14167 fold (tree expr)
14168 {
14169   tree ret;
14170   struct md5_ctx ctx;
14171   unsigned char checksum_before[16], checksum_after[16];
14172   htab_t ht;
14173
14174   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14175   md5_init_ctx (&ctx);
14176   fold_checksum_tree (expr, &ctx, ht);
14177   md5_finish_ctx (&ctx, checksum_before);
14178   htab_empty (ht);
14179
14180   ret = fold_1 (expr);
14181
14182   md5_init_ctx (&ctx);
14183   fold_checksum_tree (expr, &ctx, ht);
14184   md5_finish_ctx (&ctx, checksum_after);
14185   htab_delete (ht);
14186
14187   if (memcmp (checksum_before, checksum_after, 16))
14188     fold_check_failed (expr, ret);
14189
14190   return ret;
14191 }
14192
14193 void
14194 print_fold_checksum (const_tree expr)
14195 {
14196   struct md5_ctx ctx;
14197   unsigned char checksum[16], cnt;
14198   htab_t ht;
14199
14200   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14201   md5_init_ctx (&ctx);
14202   fold_checksum_tree (expr, &ctx, ht);
14203   md5_finish_ctx (&ctx, checksum);
14204   htab_delete (ht);
14205   for (cnt = 0; cnt < 16; ++cnt)
14206     fprintf (stderr, "%02x", checksum[cnt]);
14207   putc ('\n', stderr);
14208 }
14209
14210 static void
14211 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
14212 {
14213   internal_error ("fold check: original tree changed by fold");
14214 }
14215
14216 static void
14217 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
14218 {
14219   const void **slot;
14220   enum tree_code code;
14221   union tree_node buf;
14222   int i, len;
14223
14224 recursive_label:
14225
14226   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
14227                <= sizeof (struct tree_function_decl))
14228               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
14229   if (expr == NULL)
14230     return;
14231   slot = (const void **) htab_find_slot (ht, expr, INSERT);
14232   if (*slot != NULL)
14233     return;
14234   *slot = expr;
14235   code = TREE_CODE (expr);
14236   if (TREE_CODE_CLASS (code) == tcc_declaration
14237       && DECL_ASSEMBLER_NAME_SET_P (expr))
14238     {
14239       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
14240       memcpy ((char *) &buf, expr, tree_size (expr));
14241       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
14242       expr = (tree) &buf;
14243     }
14244   else if (TREE_CODE_CLASS (code) == tcc_type
14245            && (TYPE_POINTER_TO (expr)
14246                || TYPE_REFERENCE_TO (expr)
14247                || TYPE_CACHED_VALUES_P (expr)
14248                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
14249                || TYPE_NEXT_VARIANT (expr)))
14250     {
14251       /* Allow these fields to be modified.  */
14252       tree tmp;
14253       memcpy ((char *) &buf, expr, tree_size (expr));
14254       expr = tmp = (tree) &buf;
14255       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
14256       TYPE_POINTER_TO (tmp) = NULL;
14257       TYPE_REFERENCE_TO (tmp) = NULL;
14258       TYPE_NEXT_VARIANT (tmp) = NULL;
14259       if (TYPE_CACHED_VALUES_P (tmp))
14260         {
14261           TYPE_CACHED_VALUES_P (tmp) = 0;
14262           TYPE_CACHED_VALUES (tmp) = NULL;
14263         }
14264     }
14265   md5_process_bytes (expr, tree_size (expr), ctx);
14266   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
14267   if (TREE_CODE_CLASS (code) != tcc_type
14268       && TREE_CODE_CLASS (code) != tcc_declaration
14269       && code != TREE_LIST
14270       && code != SSA_NAME)
14271     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
14272   switch (TREE_CODE_CLASS (code))
14273     {
14274     case tcc_constant:
14275       switch (code)
14276         {
14277         case STRING_CST:
14278           md5_process_bytes (TREE_STRING_POINTER (expr),
14279                              TREE_STRING_LENGTH (expr), ctx);
14280           break;
14281         case COMPLEX_CST:
14282           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
14283           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
14284           break;
14285         case VECTOR_CST:
14286           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
14287           break;
14288         default:
14289           break;
14290         }
14291       break;
14292     case tcc_exceptional:
14293       switch (code)
14294         {
14295         case TREE_LIST:
14296           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
14297           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
14298           expr = TREE_CHAIN (expr);
14299           goto recursive_label;
14300           break;
14301         case TREE_VEC:
14302           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
14303             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
14304           break;
14305         default:
14306           break;
14307         }
14308       break;
14309     case tcc_expression:
14310     case tcc_reference:
14311     case tcc_comparison:
14312     case tcc_unary:
14313     case tcc_binary:
14314     case tcc_statement:
14315     case tcc_vl_exp:
14316       len = TREE_OPERAND_LENGTH (expr);
14317       for (i = 0; i < len; ++i)
14318         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
14319       break;
14320     case tcc_declaration:
14321       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
14322       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
14323       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
14324         {
14325           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
14326           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
14327           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
14328           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
14329           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
14330         }
14331       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
14332         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
14333
14334       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
14335         {
14336           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
14337           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
14338           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
14339         }
14340       break;
14341     case tcc_type:
14342       if (TREE_CODE (expr) == ENUMERAL_TYPE)
14343         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
14344       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
14345       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
14346       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
14347       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
14348       if (INTEGRAL_TYPE_P (expr)
14349           || SCALAR_FLOAT_TYPE_P (expr))
14350         {
14351           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
14352           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
14353         }
14354       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
14355       if (TREE_CODE (expr) == RECORD_TYPE
14356           || TREE_CODE (expr) == UNION_TYPE
14357           || TREE_CODE (expr) == QUAL_UNION_TYPE)
14358         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
14359       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
14360       break;
14361     default:
14362       break;
14363     }
14364 }
14365
14366 /* Helper function for outputting the checksum of a tree T.  When
14367    debugging with gdb, you can "define mynext" to be "next" followed
14368    by "call debug_fold_checksum (op0)", then just trace down till the
14369    outputs differ.  */
14370
14371 void
14372 debug_fold_checksum (const_tree t)
14373 {
14374   int i;
14375   unsigned char checksum[16];
14376   struct md5_ctx ctx;
14377   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14378
14379   md5_init_ctx (&ctx);
14380   fold_checksum_tree (t, &ctx, ht);
14381   md5_finish_ctx (&ctx, checksum);
14382   htab_empty (ht);
14383
14384   for (i = 0; i < 16; i++)
14385     fprintf (stderr, "%d ", checksum[i]);
14386
14387   fprintf (stderr, "\n");
14388 }
14389
14390 #endif
14391
14392 /* Fold a unary tree expression with code CODE of type TYPE with an
14393    operand OP0.  LOC is the location of the resulting expression.
14394    Return a folded expression if successful.  Otherwise, return a tree
14395    expression with code CODE of type TYPE with an operand OP0.  */
14396
14397 tree
14398 fold_build1_stat_loc (location_t loc,
14399                       enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
14400 {
14401   tree tem;
14402 #ifdef ENABLE_FOLD_CHECKING
14403   unsigned char checksum_before[16], checksum_after[16];
14404   struct md5_ctx ctx;
14405   htab_t ht;
14406
14407   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14408   md5_init_ctx (&ctx);
14409   fold_checksum_tree (op0, &ctx, ht);
14410   md5_finish_ctx (&ctx, checksum_before);
14411   htab_empty (ht);
14412 #endif
14413
14414   tem = fold_unary_loc (loc, code, type, op0);
14415   if (!tem)
14416     {
14417       tem = build1_stat (code, type, op0 PASS_MEM_STAT);
14418       SET_EXPR_LOCATION (tem, loc);
14419     }
14420
14421 #ifdef ENABLE_FOLD_CHECKING
14422   md5_init_ctx (&ctx);
14423   fold_checksum_tree (op0, &ctx, ht);
14424   md5_finish_ctx (&ctx, checksum_after);
14425   htab_delete (ht);
14426
14427   if (memcmp (checksum_before, checksum_after, 16))
14428     fold_check_failed (op0, tem);
14429 #endif
14430   return tem;
14431 }
14432
14433 /* Fold a binary tree expression with code CODE of type TYPE with
14434    operands OP0 and OP1.  LOC is the location of the resulting
14435    expression.  Return a folded expression if successful.  Otherwise,
14436    return a tree expression with code CODE of type TYPE with operands
14437    OP0 and OP1.  */
14438
14439 tree
14440 fold_build2_stat_loc (location_t loc,
14441                       enum tree_code code, tree type, tree op0, tree op1
14442                       MEM_STAT_DECL)
14443 {
14444   tree tem;
14445 #ifdef ENABLE_FOLD_CHECKING
14446   unsigned char checksum_before_op0[16],
14447                 checksum_before_op1[16],
14448                 checksum_after_op0[16],
14449                 checksum_after_op1[16];
14450   struct md5_ctx ctx;
14451   htab_t ht;
14452
14453   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14454   md5_init_ctx (&ctx);
14455   fold_checksum_tree (op0, &ctx, ht);
14456   md5_finish_ctx (&ctx, checksum_before_op0);
14457   htab_empty (ht);
14458
14459   md5_init_ctx (&ctx);
14460   fold_checksum_tree (op1, &ctx, ht);
14461   md5_finish_ctx (&ctx, checksum_before_op1);
14462   htab_empty (ht);
14463 #endif
14464
14465   tem = fold_binary_loc (loc, code, type, op0, op1);
14466   if (!tem)
14467     {
14468       tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
14469       SET_EXPR_LOCATION (tem, loc);
14470     }
14471
14472 #ifdef ENABLE_FOLD_CHECKING
14473   md5_init_ctx (&ctx);
14474   fold_checksum_tree (op0, &ctx, ht);
14475   md5_finish_ctx (&ctx, checksum_after_op0);
14476   htab_empty (ht);
14477
14478   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14479     fold_check_failed (op0, tem);
14480
14481   md5_init_ctx (&ctx);
14482   fold_checksum_tree (op1, &ctx, ht);
14483   md5_finish_ctx (&ctx, checksum_after_op1);
14484   htab_delete (ht);
14485
14486   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14487     fold_check_failed (op1, tem);
14488 #endif
14489   return tem;
14490 }
14491
14492 /* Fold a ternary tree expression with code CODE of type TYPE with
14493    operands OP0, OP1, and OP2.  Return a folded expression if
14494    successful.  Otherwise, return a tree expression with code CODE of
14495    type TYPE with operands OP0, OP1, and OP2.  */
14496
14497 tree
14498 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14499                       tree op0, tree op1, tree op2 MEM_STAT_DECL)
14500 {
14501   tree tem;
14502 #ifdef ENABLE_FOLD_CHECKING
14503   unsigned char checksum_before_op0[16],
14504                 checksum_before_op1[16],
14505                 checksum_before_op2[16],
14506                 checksum_after_op0[16],
14507                 checksum_after_op1[16],
14508                 checksum_after_op2[16];
14509   struct md5_ctx ctx;
14510   htab_t ht;
14511
14512   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14513   md5_init_ctx (&ctx);
14514   fold_checksum_tree (op0, &ctx, ht);
14515   md5_finish_ctx (&ctx, checksum_before_op0);
14516   htab_empty (ht);
14517
14518   md5_init_ctx (&ctx);
14519   fold_checksum_tree (op1, &ctx, ht);
14520   md5_finish_ctx (&ctx, checksum_before_op1);
14521   htab_empty (ht);
14522
14523   md5_init_ctx (&ctx);
14524   fold_checksum_tree (op2, &ctx, ht);
14525   md5_finish_ctx (&ctx, checksum_before_op2);
14526   htab_empty (ht);
14527 #endif
14528
14529   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14530   tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14531   if (!tem)
14532     {
14533       tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
14534       SET_EXPR_LOCATION (tem, loc);
14535     }
14536
14537 #ifdef ENABLE_FOLD_CHECKING
14538   md5_init_ctx (&ctx);
14539   fold_checksum_tree (op0, &ctx, ht);
14540   md5_finish_ctx (&ctx, checksum_after_op0);
14541   htab_empty (ht);
14542
14543   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14544     fold_check_failed (op0, tem);
14545
14546   md5_init_ctx (&ctx);
14547   fold_checksum_tree (op1, &ctx, ht);
14548   md5_finish_ctx (&ctx, checksum_after_op1);
14549   htab_empty (ht);
14550
14551   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14552     fold_check_failed (op1, tem);
14553
14554   md5_init_ctx (&ctx);
14555   fold_checksum_tree (op2, &ctx, ht);
14556   md5_finish_ctx (&ctx, checksum_after_op2);
14557   htab_delete (ht);
14558
14559   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14560     fold_check_failed (op2, tem);
14561 #endif
14562   return tem;
14563 }
14564
14565 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14566    arguments in ARGARRAY, and a null static chain.
14567    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14568    of type TYPE from the given operands as constructed by build_call_array.  */
14569
14570 tree
14571 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14572                            int nargs, tree *argarray)
14573 {
14574   tree tem;
14575 #ifdef ENABLE_FOLD_CHECKING
14576   unsigned char checksum_before_fn[16],
14577                 checksum_before_arglist[16],
14578                 checksum_after_fn[16],
14579                 checksum_after_arglist[16];
14580   struct md5_ctx ctx;
14581   htab_t ht;
14582   int i;
14583
14584   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14585   md5_init_ctx (&ctx);
14586   fold_checksum_tree (fn, &ctx, ht);
14587   md5_finish_ctx (&ctx, checksum_before_fn);
14588   htab_empty (ht);
14589
14590   md5_init_ctx (&ctx);
14591   for (i = 0; i < nargs; i++)
14592     fold_checksum_tree (argarray[i], &ctx, ht);
14593   md5_finish_ctx (&ctx, checksum_before_arglist);
14594   htab_empty (ht);
14595 #endif
14596
14597   tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14598
14599 #ifdef ENABLE_FOLD_CHECKING
14600   md5_init_ctx (&ctx);
14601   fold_checksum_tree (fn, &ctx, ht);
14602   md5_finish_ctx (&ctx, checksum_after_fn);
14603   htab_empty (ht);
14604
14605   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14606     fold_check_failed (fn, tem);
14607
14608   md5_init_ctx (&ctx);
14609   for (i = 0; i < nargs; i++)
14610     fold_checksum_tree (argarray[i], &ctx, ht);
14611   md5_finish_ctx (&ctx, checksum_after_arglist);
14612   htab_delete (ht);
14613
14614   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14615     fold_check_failed (NULL_TREE, tem);
14616 #endif
14617   return tem;
14618 }
14619
14620 /* Perform constant folding and related simplification of initializer
14621    expression EXPR.  These behave identically to "fold_buildN" but ignore
14622    potential run-time traps and exceptions that fold must preserve.  */
14623
14624 #define START_FOLD_INIT \
14625   int saved_signaling_nans = flag_signaling_nans;\
14626   int saved_trapping_math = flag_trapping_math;\
14627   int saved_rounding_math = flag_rounding_math;\
14628   int saved_trapv = flag_trapv;\
14629   int saved_folding_initializer = folding_initializer;\
14630   flag_signaling_nans = 0;\
14631   flag_trapping_math = 0;\
14632   flag_rounding_math = 0;\
14633   flag_trapv = 0;\
14634   folding_initializer = 1;
14635
14636 #define END_FOLD_INIT \
14637   flag_signaling_nans = saved_signaling_nans;\
14638   flag_trapping_math = saved_trapping_math;\
14639   flag_rounding_math = saved_rounding_math;\
14640   flag_trapv = saved_trapv;\
14641   folding_initializer = saved_folding_initializer;
14642
14643 tree
14644 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14645                              tree type, tree op)
14646 {
14647   tree result;
14648   START_FOLD_INIT;
14649
14650   result = fold_build1_loc (loc, code, type, op);
14651
14652   END_FOLD_INIT;
14653   return result;
14654 }
14655
14656 tree
14657 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14658                              tree type, tree op0, tree op1)
14659 {
14660   tree result;
14661   START_FOLD_INIT;
14662
14663   result = fold_build2_loc (loc, code, type, op0, op1);
14664
14665   END_FOLD_INIT;
14666   return result;
14667 }
14668
14669 tree
14670 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14671                              tree type, tree op0, tree op1, tree op2)
14672 {
14673   tree result;
14674   START_FOLD_INIT;
14675
14676   result = fold_build3_loc (loc, code, type, op0, op1, op2);
14677
14678   END_FOLD_INIT;
14679   return result;
14680 }
14681
14682 tree
14683 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14684                                        int nargs, tree *argarray)
14685 {
14686   tree result;
14687   START_FOLD_INIT;
14688
14689   result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14690
14691   END_FOLD_INIT;
14692   return result;
14693 }
14694
14695 #undef START_FOLD_INIT
14696 #undef END_FOLD_INIT
14697
14698 /* Determine if first argument is a multiple of second argument.  Return 0 if
14699    it is not, or we cannot easily determined it to be.
14700
14701    An example of the sort of thing we care about (at this point; this routine
14702    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14703    fold cases do now) is discovering that
14704
14705      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14706
14707    is a multiple of
14708
14709      SAVE_EXPR (J * 8)
14710
14711    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14712
14713    This code also handles discovering that
14714
14715      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14716
14717    is a multiple of 8 so we don't have to worry about dealing with a
14718    possible remainder.
14719
14720    Note that we *look* inside a SAVE_EXPR only to determine how it was
14721    calculated; it is not safe for fold to do much of anything else with the
14722    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14723    at run time.  For example, the latter example above *cannot* be implemented
14724    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14725    evaluation time of the original SAVE_EXPR is not necessarily the same at
14726    the time the new expression is evaluated.  The only optimization of this
14727    sort that would be valid is changing
14728
14729      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14730
14731    divided by 8 to
14732
14733      SAVE_EXPR (I) * SAVE_EXPR (J)
14734
14735    (where the same SAVE_EXPR (J) is used in the original and the
14736    transformed version).  */
14737
14738 int
14739 multiple_of_p (tree type, const_tree top, const_tree bottom)
14740 {
14741   if (operand_equal_p (top, bottom, 0))
14742     return 1;
14743
14744   if (TREE_CODE (type) != INTEGER_TYPE)
14745     return 0;
14746
14747   switch (TREE_CODE (top))
14748     {
14749     case BIT_AND_EXPR:
14750       /* Bitwise and provides a power of two multiple.  If the mask is
14751          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14752       if (!integer_pow2p (bottom))
14753         return 0;
14754       /* FALLTHRU */
14755
14756     case MULT_EXPR:
14757       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14758               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14759
14760     case PLUS_EXPR:
14761     case MINUS_EXPR:
14762       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14763               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14764
14765     case LSHIFT_EXPR:
14766       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14767         {
14768           tree op1, t1;
14769
14770           op1 = TREE_OPERAND (top, 1);
14771           /* const_binop may not detect overflow correctly,
14772              so check for it explicitly here.  */
14773           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14774               > TREE_INT_CST_LOW (op1)
14775               && TREE_INT_CST_HIGH (op1) == 0
14776               && 0 != (t1 = fold_convert (type,
14777                                           const_binop (LSHIFT_EXPR,
14778                                                        size_one_node,
14779                                                        op1, 0)))
14780               && !TREE_OVERFLOW (t1))
14781             return multiple_of_p (type, t1, bottom);
14782         }
14783       return 0;
14784
14785     case NOP_EXPR:
14786       /* Can't handle conversions from non-integral or wider integral type.  */
14787       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14788           || (TYPE_PRECISION (type)
14789               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14790         return 0;
14791
14792       /* .. fall through ...  */
14793
14794     case SAVE_EXPR:
14795       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14796
14797     case INTEGER_CST:
14798       if (TREE_CODE (bottom) != INTEGER_CST
14799           || integer_zerop (bottom)
14800           || (TYPE_UNSIGNED (type)
14801               && (tree_int_cst_sgn (top) < 0
14802                   || tree_int_cst_sgn (bottom) < 0)))
14803         return 0;
14804       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14805                                              top, bottom, 0));
14806
14807     default:
14808       return 0;
14809     }
14810 }
14811
14812 /* Return true if CODE or TYPE is known to be non-negative. */
14813
14814 static bool
14815 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14816 {
14817   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14818       && truth_value_p (code))
14819     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14820        have a signed:1 type (where the value is -1 and 0).  */
14821     return true;
14822   return false;
14823 }
14824
14825 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14826    value is based on the assumption that signed overflow is undefined,
14827    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14828    *STRICT_OVERFLOW_P.  */
14829
14830 bool
14831 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14832                                 bool *strict_overflow_p)
14833 {
14834   if (TYPE_UNSIGNED (type))
14835     return true;
14836
14837   switch (code)
14838     {
14839     case ABS_EXPR:
14840       /* We can't return 1 if flag_wrapv is set because
14841          ABS_EXPR<INT_MIN> = INT_MIN.  */
14842       if (!INTEGRAL_TYPE_P (type))
14843         return true;
14844       if (TYPE_OVERFLOW_UNDEFINED (type))
14845         {
14846           *strict_overflow_p = true;
14847           return true;
14848         }
14849       break;
14850
14851     case NON_LVALUE_EXPR:
14852     case FLOAT_EXPR:
14853     case FIX_TRUNC_EXPR:
14854       return tree_expr_nonnegative_warnv_p (op0,
14855                                             strict_overflow_p);
14856
14857     case NOP_EXPR:
14858       {
14859         tree inner_type = TREE_TYPE (op0);
14860         tree outer_type = type;
14861
14862         if (TREE_CODE (outer_type) == REAL_TYPE)
14863           {
14864             if (TREE_CODE (inner_type) == REAL_TYPE)
14865               return tree_expr_nonnegative_warnv_p (op0,
14866                                                     strict_overflow_p);
14867             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14868               {
14869                 if (TYPE_UNSIGNED (inner_type))
14870                   return true;
14871                 return tree_expr_nonnegative_warnv_p (op0,
14872                                                       strict_overflow_p);
14873               }
14874           }
14875         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14876           {
14877             if (TREE_CODE (inner_type) == REAL_TYPE)
14878               return tree_expr_nonnegative_warnv_p (op0,
14879                                                     strict_overflow_p);
14880             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14881               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14882                       && TYPE_UNSIGNED (inner_type);
14883           }
14884       }
14885       break;
14886
14887     default:
14888       return tree_simple_nonnegative_warnv_p (code, type);
14889     }
14890
14891   /* We don't know sign of `t', so be conservative and return false.  */
14892   return false;
14893 }
14894
14895 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14896    value is based on the assumption that signed overflow is undefined,
14897    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14898    *STRICT_OVERFLOW_P.  */
14899
14900 bool
14901 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14902                                       tree op1, bool *strict_overflow_p)
14903 {
14904   if (TYPE_UNSIGNED (type))
14905     return true;
14906
14907   switch (code)
14908     {
14909     case POINTER_PLUS_EXPR:
14910     case PLUS_EXPR:
14911       if (FLOAT_TYPE_P (type))
14912         return (tree_expr_nonnegative_warnv_p (op0,
14913                                                strict_overflow_p)
14914                 && tree_expr_nonnegative_warnv_p (op1,
14915                                                   strict_overflow_p));
14916
14917       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14918          both unsigned and at least 2 bits shorter than the result.  */
14919       if (TREE_CODE (type) == INTEGER_TYPE
14920           && TREE_CODE (op0) == NOP_EXPR
14921           && TREE_CODE (op1) == NOP_EXPR)
14922         {
14923           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14924           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14925           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14926               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14927             {
14928               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14929                                        TYPE_PRECISION (inner2)) + 1;
14930               return prec < TYPE_PRECISION (type);
14931             }
14932         }
14933       break;
14934
14935     case MULT_EXPR:
14936       if (FLOAT_TYPE_P (type))
14937         {
14938           /* x * x for floating point x is always non-negative.  */
14939           if (operand_equal_p (op0, op1, 0))
14940             return true;
14941           return (tree_expr_nonnegative_warnv_p (op0,
14942                                                  strict_overflow_p)
14943                   && tree_expr_nonnegative_warnv_p (op1,
14944                                                     strict_overflow_p));
14945         }
14946
14947       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14948          both unsigned and their total bits is shorter than the result.  */
14949       if (TREE_CODE (type) == INTEGER_TYPE
14950           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14951           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14952         {
14953           tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14954             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14955             : TREE_TYPE (op0);
14956           tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14957             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14958             : TREE_TYPE (op1);
14959
14960           bool unsigned0 = TYPE_UNSIGNED (inner0);
14961           bool unsigned1 = TYPE_UNSIGNED (inner1);
14962
14963           if (TREE_CODE (op0) == INTEGER_CST)
14964             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14965
14966           if (TREE_CODE (op1) == INTEGER_CST)
14967             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14968
14969           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14970               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14971             {
14972               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14973                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14974                 : TYPE_PRECISION (inner0);
14975
14976               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14977                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14978                 : TYPE_PRECISION (inner1);
14979
14980               return precision0 + precision1 < TYPE_PRECISION (type);
14981             }
14982         }
14983       return false;
14984
14985     case BIT_AND_EXPR:
14986     case MAX_EXPR:
14987       return (tree_expr_nonnegative_warnv_p (op0,
14988                                              strict_overflow_p)
14989               || tree_expr_nonnegative_warnv_p (op1,
14990                                                 strict_overflow_p));
14991
14992     case BIT_IOR_EXPR:
14993     case BIT_XOR_EXPR:
14994     case MIN_EXPR:
14995     case RDIV_EXPR:
14996     case TRUNC_DIV_EXPR:
14997     case CEIL_DIV_EXPR:
14998     case FLOOR_DIV_EXPR:
14999     case ROUND_DIV_EXPR:
15000       return (tree_expr_nonnegative_warnv_p (op0,
15001                                              strict_overflow_p)
15002               && tree_expr_nonnegative_warnv_p (op1,
15003                                                 strict_overflow_p));
15004
15005     case TRUNC_MOD_EXPR:
15006     case CEIL_MOD_EXPR:
15007     case FLOOR_MOD_EXPR:
15008     case ROUND_MOD_EXPR:
15009       return tree_expr_nonnegative_warnv_p (op0,
15010                                             strict_overflow_p);
15011     default:
15012       return tree_simple_nonnegative_warnv_p (code, type);
15013     }
15014
15015   /* We don't know sign of `t', so be conservative and return false.  */
15016   return false;
15017 }
15018
15019 /* Return true if T is known to be non-negative.  If the return
15020    value is based on the assumption that signed overflow is undefined,
15021    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15022    *STRICT_OVERFLOW_P.  */
15023
15024 bool
15025 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15026 {
15027   if (TYPE_UNSIGNED (TREE_TYPE (t)))
15028     return true;
15029
15030   switch (TREE_CODE (t))
15031     {
15032     case INTEGER_CST:
15033       return tree_int_cst_sgn (t) >= 0;
15034
15035     case REAL_CST:
15036       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
15037
15038     case FIXED_CST:
15039       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
15040
15041     case COND_EXPR:
15042       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15043                                              strict_overflow_p)
15044               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
15045                                                 strict_overflow_p));
15046     default:
15047       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15048                                                    TREE_TYPE (t));
15049     }
15050   /* We don't know sign of `t', so be conservative and return false.  */
15051   return false;
15052 }
15053
15054 /* Return true if T is known to be non-negative.  If the return
15055    value is based on the assumption that signed overflow is undefined,
15056    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15057    *STRICT_OVERFLOW_P.  */
15058
15059 bool
15060 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
15061                                tree arg0, tree arg1, bool *strict_overflow_p)
15062 {
15063   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
15064     switch (DECL_FUNCTION_CODE (fndecl))
15065       {
15066         CASE_FLT_FN (BUILT_IN_ACOS):
15067         CASE_FLT_FN (BUILT_IN_ACOSH):
15068         CASE_FLT_FN (BUILT_IN_CABS):
15069         CASE_FLT_FN (BUILT_IN_COSH):
15070         CASE_FLT_FN (BUILT_IN_ERFC):
15071         CASE_FLT_FN (BUILT_IN_EXP):
15072         CASE_FLT_FN (BUILT_IN_EXP10):
15073         CASE_FLT_FN (BUILT_IN_EXP2):
15074         CASE_FLT_FN (BUILT_IN_FABS):
15075         CASE_FLT_FN (BUILT_IN_FDIM):
15076         CASE_FLT_FN (BUILT_IN_HYPOT):
15077         CASE_FLT_FN (BUILT_IN_POW10):
15078         CASE_INT_FN (BUILT_IN_FFS):
15079         CASE_INT_FN (BUILT_IN_PARITY):
15080         CASE_INT_FN (BUILT_IN_POPCOUNT):
15081       case BUILT_IN_BSWAP32:
15082       case BUILT_IN_BSWAP64:
15083         /* Always true.  */
15084         return true;
15085
15086         CASE_FLT_FN (BUILT_IN_SQRT):
15087         /* sqrt(-0.0) is -0.0.  */
15088         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
15089           return true;
15090         return tree_expr_nonnegative_warnv_p (arg0,
15091                                               strict_overflow_p);
15092
15093         CASE_FLT_FN (BUILT_IN_ASINH):
15094         CASE_FLT_FN (BUILT_IN_ATAN):
15095         CASE_FLT_FN (BUILT_IN_ATANH):
15096         CASE_FLT_FN (BUILT_IN_CBRT):
15097         CASE_FLT_FN (BUILT_IN_CEIL):
15098         CASE_FLT_FN (BUILT_IN_ERF):
15099         CASE_FLT_FN (BUILT_IN_EXPM1):
15100         CASE_FLT_FN (BUILT_IN_FLOOR):
15101         CASE_FLT_FN (BUILT_IN_FMOD):
15102         CASE_FLT_FN (BUILT_IN_FREXP):
15103         CASE_FLT_FN (BUILT_IN_LCEIL):
15104         CASE_FLT_FN (BUILT_IN_LDEXP):
15105         CASE_FLT_FN (BUILT_IN_LFLOOR):
15106         CASE_FLT_FN (BUILT_IN_LLCEIL):
15107         CASE_FLT_FN (BUILT_IN_LLFLOOR):
15108         CASE_FLT_FN (BUILT_IN_LLRINT):
15109         CASE_FLT_FN (BUILT_IN_LLROUND):
15110         CASE_FLT_FN (BUILT_IN_LRINT):
15111         CASE_FLT_FN (BUILT_IN_LROUND):
15112         CASE_FLT_FN (BUILT_IN_MODF):
15113         CASE_FLT_FN (BUILT_IN_NEARBYINT):
15114         CASE_FLT_FN (BUILT_IN_RINT):
15115         CASE_FLT_FN (BUILT_IN_ROUND):
15116         CASE_FLT_FN (BUILT_IN_SCALB):
15117         CASE_FLT_FN (BUILT_IN_SCALBLN):
15118         CASE_FLT_FN (BUILT_IN_SCALBN):
15119         CASE_FLT_FN (BUILT_IN_SIGNBIT):
15120         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
15121         CASE_FLT_FN (BUILT_IN_SINH):
15122         CASE_FLT_FN (BUILT_IN_TANH):
15123         CASE_FLT_FN (BUILT_IN_TRUNC):
15124         /* True if the 1st argument is nonnegative.  */
15125         return tree_expr_nonnegative_warnv_p (arg0,
15126                                               strict_overflow_p);
15127
15128         CASE_FLT_FN (BUILT_IN_FMAX):
15129         /* True if the 1st OR 2nd arguments are nonnegative.  */
15130         return (tree_expr_nonnegative_warnv_p (arg0,
15131                                                strict_overflow_p)
15132                 || (tree_expr_nonnegative_warnv_p (arg1,
15133                                                    strict_overflow_p)));
15134
15135         CASE_FLT_FN (BUILT_IN_FMIN):
15136         /* True if the 1st AND 2nd arguments are nonnegative.  */
15137         return (tree_expr_nonnegative_warnv_p (arg0,
15138                                                strict_overflow_p)
15139                 && (tree_expr_nonnegative_warnv_p (arg1,
15140                                                    strict_overflow_p)));
15141
15142         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15143         /* True if the 2nd argument is nonnegative.  */
15144         return tree_expr_nonnegative_warnv_p (arg1,
15145                                               strict_overflow_p);
15146
15147         CASE_FLT_FN (BUILT_IN_POWI):
15148         /* True if the 1st argument is nonnegative or the second
15149            argument is an even integer.  */
15150         if (TREE_CODE (arg1) == INTEGER_CST
15151             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15152           return true;
15153         return tree_expr_nonnegative_warnv_p (arg0,
15154                                               strict_overflow_p);
15155
15156         CASE_FLT_FN (BUILT_IN_POW):
15157         /* True if the 1st argument is nonnegative or the second
15158            argument is an even integer valued real.  */
15159         if (TREE_CODE (arg1) == REAL_CST)
15160           {
15161             REAL_VALUE_TYPE c;
15162             HOST_WIDE_INT n;
15163
15164             c = TREE_REAL_CST (arg1);
15165             n = real_to_integer (&c);
15166             if ((n & 1) == 0)
15167               {
15168                 REAL_VALUE_TYPE cint;
15169                 real_from_integer (&cint, VOIDmode, n,
15170                                    n < 0 ? -1 : 0, 0);
15171                 if (real_identical (&c, &cint))
15172                   return true;
15173               }
15174           }
15175         return tree_expr_nonnegative_warnv_p (arg0,
15176                                               strict_overflow_p);
15177
15178       default:
15179         break;
15180       }
15181   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
15182                                           type);
15183 }
15184
15185 /* Return true if T is known to be non-negative.  If the return
15186    value is based on the assumption that signed overflow is undefined,
15187    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15188    *STRICT_OVERFLOW_P.  */
15189
15190 bool
15191 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15192 {
15193   enum tree_code code = TREE_CODE (t);
15194   if (TYPE_UNSIGNED (TREE_TYPE (t)))
15195     return true;
15196
15197   switch (code)
15198     {
15199     case TARGET_EXPR:
15200       {
15201         tree temp = TARGET_EXPR_SLOT (t);
15202         t = TARGET_EXPR_INITIAL (t);
15203
15204         /* If the initializer is non-void, then it's a normal expression
15205            that will be assigned to the slot.  */
15206         if (!VOID_TYPE_P (t))
15207           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
15208
15209         /* Otherwise, the initializer sets the slot in some way.  One common
15210            way is an assignment statement at the end of the initializer.  */
15211         while (1)
15212           {
15213             if (TREE_CODE (t) == BIND_EXPR)
15214               t = expr_last (BIND_EXPR_BODY (t));
15215             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15216                      || TREE_CODE (t) == TRY_CATCH_EXPR)
15217               t = expr_last (TREE_OPERAND (t, 0));
15218             else if (TREE_CODE (t) == STATEMENT_LIST)
15219               t = expr_last (t);
15220             else
15221               break;
15222           }
15223         if (TREE_CODE (t) == MODIFY_EXPR
15224             && TREE_OPERAND (t, 0) == temp)
15225           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15226                                                 strict_overflow_p);
15227
15228         return false;
15229       }
15230
15231     case CALL_EXPR:
15232       {
15233         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
15234         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
15235
15236         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15237                                               get_callee_fndecl (t),
15238                                               arg0,
15239                                               arg1,
15240                                               strict_overflow_p);
15241       }
15242     case COMPOUND_EXPR:
15243     case MODIFY_EXPR:
15244       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15245                                             strict_overflow_p);
15246     case BIND_EXPR:
15247       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
15248                                             strict_overflow_p);
15249     case SAVE_EXPR:
15250       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
15251                                             strict_overflow_p);
15252
15253     default:
15254       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15255                                                    TREE_TYPE (t));
15256     }
15257
15258   /* We don't know sign of `t', so be conservative and return false.  */
15259   return false;
15260 }
15261
15262 /* Return true if T is known to be non-negative.  If the return
15263    value is based on the assumption that signed overflow is undefined,
15264    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15265    *STRICT_OVERFLOW_P.  */
15266
15267 bool
15268 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15269 {
15270   enum tree_code code;
15271   if (t == error_mark_node)
15272     return false;
15273
15274   code = TREE_CODE (t);
15275   switch (TREE_CODE_CLASS (code))
15276     {
15277     case tcc_binary:
15278     case tcc_comparison:
15279       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15280                                               TREE_TYPE (t),
15281                                               TREE_OPERAND (t, 0),
15282                                               TREE_OPERAND (t, 1),
15283                                               strict_overflow_p);
15284
15285     case tcc_unary:
15286       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15287                                              TREE_TYPE (t),
15288                                              TREE_OPERAND (t, 0),
15289                                              strict_overflow_p);
15290
15291     case tcc_constant:
15292     case tcc_declaration:
15293     case tcc_reference:
15294       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15295
15296     default:
15297       break;
15298     }
15299
15300   switch (code)
15301     {
15302     case TRUTH_AND_EXPR:
15303     case TRUTH_OR_EXPR:
15304     case TRUTH_XOR_EXPR:
15305       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15306                                               TREE_TYPE (t),
15307                                               TREE_OPERAND (t, 0),
15308                                               TREE_OPERAND (t, 1),
15309                                               strict_overflow_p);
15310     case TRUTH_NOT_EXPR:
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 COND_EXPR:
15317     case CONSTRUCTOR:
15318     case OBJ_TYPE_REF:
15319     case ASSERT_EXPR:
15320     case ADDR_EXPR:
15321     case WITH_SIZE_EXPR:
15322     case SSA_NAME:
15323       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15324
15325     default:
15326       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
15327     }
15328 }
15329
15330 /* Return true if `t' is known to be non-negative.  Handle warnings
15331    about undefined signed overflow.  */
15332
15333 bool
15334 tree_expr_nonnegative_p (tree t)
15335 {
15336   bool ret, strict_overflow_p;
15337
15338   strict_overflow_p = false;
15339   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15340   if (strict_overflow_p)
15341     fold_overflow_warning (("assuming signed overflow does not occur when "
15342                             "determining that expression is always "
15343                             "non-negative"),
15344                            WARN_STRICT_OVERFLOW_MISC);
15345   return ret;
15346 }
15347
15348
15349 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15350    For floating point we further ensure that T is not denormal.
15351    Similar logic is present in nonzero_address in rtlanal.h.
15352
15353    If the return value is based on the assumption that signed overflow
15354    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15355    change *STRICT_OVERFLOW_P.  */
15356
15357 bool
15358 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15359                                  bool *strict_overflow_p)
15360 {
15361   switch (code)
15362     {
15363     case ABS_EXPR:
15364       return tree_expr_nonzero_warnv_p (op0,
15365                                         strict_overflow_p);
15366
15367     case NOP_EXPR:
15368       {
15369         tree inner_type = TREE_TYPE (op0);
15370         tree outer_type = type;
15371
15372         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15373                 && tree_expr_nonzero_warnv_p (op0,
15374                                               strict_overflow_p));
15375       }
15376       break;
15377
15378     case NON_LVALUE_EXPR:
15379       return tree_expr_nonzero_warnv_p (op0,
15380                                         strict_overflow_p);
15381
15382     default:
15383       break;
15384   }
15385
15386   return false;
15387 }
15388
15389 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15390    For floating point we further ensure that T is not denormal.
15391    Similar logic is present in nonzero_address in rtlanal.h.
15392
15393    If the return value is based on the assumption that signed overflow
15394    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15395    change *STRICT_OVERFLOW_P.  */
15396
15397 bool
15398 tree_binary_nonzero_warnv_p (enum tree_code code,
15399                              tree type,
15400                              tree op0,
15401                              tree op1, bool *strict_overflow_p)
15402 {
15403   bool sub_strict_overflow_p;
15404   switch (code)
15405     {
15406     case POINTER_PLUS_EXPR:
15407     case PLUS_EXPR:
15408       if (TYPE_OVERFLOW_UNDEFINED (type))
15409         {
15410           /* With the presence of negative values it is hard
15411              to say something.  */
15412           sub_strict_overflow_p = false;
15413           if (!tree_expr_nonnegative_warnv_p (op0,
15414                                               &sub_strict_overflow_p)
15415               || !tree_expr_nonnegative_warnv_p (op1,
15416                                                  &sub_strict_overflow_p))
15417             return false;
15418           /* One of operands must be positive and the other non-negative.  */
15419           /* We don't set *STRICT_OVERFLOW_P here: even if this value
15420              overflows, on a twos-complement machine the sum of two
15421              nonnegative numbers can never be zero.  */
15422           return (tree_expr_nonzero_warnv_p (op0,
15423                                              strict_overflow_p)
15424                   || tree_expr_nonzero_warnv_p (op1,
15425                                                 strict_overflow_p));
15426         }
15427       break;
15428
15429     case MULT_EXPR:
15430       if (TYPE_OVERFLOW_UNDEFINED (type))
15431         {
15432           if (tree_expr_nonzero_warnv_p (op0,
15433                                          strict_overflow_p)
15434               && tree_expr_nonzero_warnv_p (op1,
15435                                             strict_overflow_p))
15436             {
15437               *strict_overflow_p = true;
15438               return true;
15439             }
15440         }
15441       break;
15442
15443     case MIN_EXPR:
15444       sub_strict_overflow_p = false;
15445       if (tree_expr_nonzero_warnv_p (op0,
15446                                      &sub_strict_overflow_p)
15447           && tree_expr_nonzero_warnv_p (op1,
15448                                         &sub_strict_overflow_p))
15449         {
15450           if (sub_strict_overflow_p)
15451             *strict_overflow_p = true;
15452         }
15453       break;
15454
15455     case MAX_EXPR:
15456       sub_strict_overflow_p = false;
15457       if (tree_expr_nonzero_warnv_p (op0,
15458                                      &sub_strict_overflow_p))
15459         {
15460           if (sub_strict_overflow_p)
15461             *strict_overflow_p = true;
15462
15463           /* When both operands are nonzero, then MAX must be too.  */
15464           if (tree_expr_nonzero_warnv_p (op1,
15465                                          strict_overflow_p))
15466             return true;
15467
15468           /* MAX where operand 0 is positive is positive.  */
15469           return tree_expr_nonnegative_warnv_p (op0,
15470                                                strict_overflow_p);
15471         }
15472       /* MAX where operand 1 is positive is positive.  */
15473       else if (tree_expr_nonzero_warnv_p (op1,
15474                                           &sub_strict_overflow_p)
15475                && tree_expr_nonnegative_warnv_p (op1,
15476                                                  &sub_strict_overflow_p))
15477         {
15478           if (sub_strict_overflow_p)
15479             *strict_overflow_p = true;
15480           return true;
15481         }
15482       break;
15483
15484     case BIT_IOR_EXPR:
15485       return (tree_expr_nonzero_warnv_p (op1,
15486                                          strict_overflow_p)
15487               || tree_expr_nonzero_warnv_p (op0,
15488                                             strict_overflow_p));
15489
15490     default:
15491       break;
15492   }
15493
15494   return false;
15495 }
15496
15497 /* Return true when T is an address and is known to be nonzero.
15498    For floating point we further ensure that T is not denormal.
15499    Similar logic is present in nonzero_address in rtlanal.h.
15500
15501    If the return value is based on the assumption that signed overflow
15502    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15503    change *STRICT_OVERFLOW_P.  */
15504
15505 bool
15506 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15507 {
15508   bool sub_strict_overflow_p;
15509   switch (TREE_CODE (t))
15510     {
15511     case INTEGER_CST:
15512       return !integer_zerop (t);
15513
15514     case ADDR_EXPR:
15515       {
15516         tree base = get_base_address (TREE_OPERAND (t, 0));
15517
15518         if (!base)
15519           return false;
15520
15521         /* Weak declarations may link to NULL.  Other things may also be NULL
15522            so protect with -fdelete-null-pointer-checks; but not variables
15523            allocated on the stack.  */
15524         if (DECL_P (base)
15525             && (flag_delete_null_pointer_checks
15526                 || (TREE_CODE (base) == VAR_DECL && !TREE_STATIC (base))))
15527           return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15528
15529         /* Constants are never weak.  */
15530         if (CONSTANT_CLASS_P (base))
15531           return true;
15532
15533         return false;
15534       }
15535
15536     case COND_EXPR:
15537       sub_strict_overflow_p = false;
15538       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15539                                      &sub_strict_overflow_p)
15540           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15541                                         &sub_strict_overflow_p))
15542         {
15543           if (sub_strict_overflow_p)
15544             *strict_overflow_p = true;
15545           return true;
15546         }
15547       break;
15548
15549     default:
15550       break;
15551     }
15552   return false;
15553 }
15554
15555 /* Return true when T is an address and is known to be nonzero.
15556    For floating point we further ensure that T is not denormal.
15557    Similar logic is present in nonzero_address in rtlanal.h.
15558
15559    If the return value is based on the assumption that signed overflow
15560    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15561    change *STRICT_OVERFLOW_P.  */
15562
15563 bool
15564 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15565 {
15566   tree type = TREE_TYPE (t);
15567   enum tree_code code;
15568
15569   /* Doing something useful for floating point would need more work.  */
15570   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15571     return false;
15572
15573   code = TREE_CODE (t);
15574   switch (TREE_CODE_CLASS (code))
15575     {
15576     case tcc_unary:
15577       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15578                                               strict_overflow_p);
15579     case tcc_binary:
15580     case tcc_comparison:
15581       return tree_binary_nonzero_warnv_p (code, type,
15582                                                TREE_OPERAND (t, 0),
15583                                                TREE_OPERAND (t, 1),
15584                                                strict_overflow_p);
15585     case tcc_constant:
15586     case tcc_declaration:
15587     case tcc_reference:
15588       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15589
15590     default:
15591       break;
15592     }
15593
15594   switch (code)
15595     {
15596     case TRUTH_NOT_EXPR:
15597       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15598                                               strict_overflow_p);
15599
15600     case TRUTH_AND_EXPR:
15601     case TRUTH_OR_EXPR:
15602     case TRUTH_XOR_EXPR:
15603       return tree_binary_nonzero_warnv_p (code, type,
15604                                                TREE_OPERAND (t, 0),
15605                                                TREE_OPERAND (t, 1),
15606                                                strict_overflow_p);
15607
15608     case COND_EXPR:
15609     case CONSTRUCTOR:
15610     case OBJ_TYPE_REF:
15611     case ASSERT_EXPR:
15612     case ADDR_EXPR:
15613     case WITH_SIZE_EXPR:
15614     case SSA_NAME:
15615       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15616
15617     case COMPOUND_EXPR:
15618     case MODIFY_EXPR:
15619     case BIND_EXPR:
15620       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15621                                         strict_overflow_p);
15622
15623     case SAVE_EXPR:
15624       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15625                                         strict_overflow_p);
15626
15627     case CALL_EXPR:
15628       return alloca_call_p (t);
15629
15630     default:
15631       break;
15632     }
15633   return false;
15634 }
15635
15636 /* Return true when T is an address and is known to be nonzero.
15637    Handle warnings about undefined signed overflow.  */
15638
15639 bool
15640 tree_expr_nonzero_p (tree t)
15641 {
15642   bool ret, strict_overflow_p;
15643
15644   strict_overflow_p = false;
15645   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15646   if (strict_overflow_p)
15647     fold_overflow_warning (("assuming signed overflow does not occur when "
15648                             "determining that expression is always "
15649                             "non-zero"),
15650                            WARN_STRICT_OVERFLOW_MISC);
15651   return ret;
15652 }
15653
15654 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15655    attempt to fold the expression to a constant without modifying TYPE,
15656    OP0 or OP1.
15657
15658    If the expression could be simplified to a constant, then return
15659    the constant.  If the expression would not be simplified to a
15660    constant, then return NULL_TREE.  */
15661
15662 tree
15663 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15664 {
15665   tree tem = fold_binary (code, type, op0, op1);
15666   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15667 }
15668
15669 /* Given the components of a unary expression CODE, TYPE and OP0,
15670    attempt to fold the expression to a constant without modifying
15671    TYPE or OP0.
15672
15673    If the expression could be simplified to a constant, then return
15674    the constant.  If the expression would not be simplified to a
15675    constant, then return NULL_TREE.  */
15676
15677 tree
15678 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15679 {
15680   tree tem = fold_unary (code, type, op0);
15681   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15682 }
15683
15684 /* If EXP represents referencing an element in a constant string
15685    (either via pointer arithmetic or array indexing), return the
15686    tree representing the value accessed, otherwise return NULL.  */
15687
15688 tree
15689 fold_read_from_constant_string (tree exp)
15690 {
15691   if ((TREE_CODE (exp) == INDIRECT_REF
15692        || TREE_CODE (exp) == ARRAY_REF)
15693       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15694     {
15695       tree exp1 = TREE_OPERAND (exp, 0);
15696       tree index;
15697       tree string;
15698       location_t loc = EXPR_LOCATION (exp);
15699
15700       if (TREE_CODE (exp) == INDIRECT_REF)
15701         string = string_constant (exp1, &index);
15702       else
15703         {
15704           tree low_bound = array_ref_low_bound (exp);
15705           index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15706
15707           /* Optimize the special-case of a zero lower bound.
15708
15709              We convert the low_bound to sizetype to avoid some problems
15710              with constant folding.  (E.g. suppose the lower bound is 1,
15711              and its mode is QI.  Without the conversion,l (ARRAY
15712              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15713              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15714           if (! integer_zerop (low_bound))
15715             index = size_diffop_loc (loc, index,
15716                                  fold_convert_loc (loc, sizetype, low_bound));
15717
15718           string = exp1;
15719         }
15720
15721       if (string
15722           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15723           && TREE_CODE (string) == STRING_CST
15724           && TREE_CODE (index) == INTEGER_CST
15725           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15726           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15727               == MODE_INT)
15728           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15729         return build_int_cst_type (TREE_TYPE (exp),
15730                                    (TREE_STRING_POINTER (string)
15731                                     [TREE_INT_CST_LOW (index)]));
15732     }
15733   return NULL;
15734 }
15735
15736 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15737    an integer constant, real, or fixed-point constant.
15738
15739    TYPE is the type of the result.  */
15740
15741 static tree
15742 fold_negate_const (tree arg0, tree type)
15743 {
15744   tree t = NULL_TREE;
15745
15746   switch (TREE_CODE (arg0))
15747     {
15748     case INTEGER_CST:
15749       {
15750         unsigned HOST_WIDE_INT low;
15751         HOST_WIDE_INT high;
15752         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15753                                    TREE_INT_CST_HIGH (arg0),
15754                                    &low, &high);
15755         t = force_fit_type_double (type, low, high, 1,
15756                                    (overflow | TREE_OVERFLOW (arg0))
15757                                    && !TYPE_UNSIGNED (type));
15758         break;
15759       }
15760
15761     case REAL_CST:
15762       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15763       break;
15764
15765     case FIXED_CST:
15766       {
15767         FIXED_VALUE_TYPE f;
15768         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15769                                             &(TREE_FIXED_CST (arg0)), NULL,
15770                                             TYPE_SATURATING (type));
15771         t = build_fixed (type, f);
15772         /* Propagate overflow flags.  */
15773         if (overflow_p | TREE_OVERFLOW (arg0))
15774           TREE_OVERFLOW (t) = 1;
15775         break;
15776       }
15777
15778     default:
15779       gcc_unreachable ();
15780     }
15781
15782   return t;
15783 }
15784
15785 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15786    an integer constant or real constant.
15787
15788    TYPE is the type of the result.  */
15789
15790 tree
15791 fold_abs_const (tree arg0, tree type)
15792 {
15793   tree t = NULL_TREE;
15794
15795   switch (TREE_CODE (arg0))
15796     {
15797     case INTEGER_CST:
15798       /* If the value is unsigned, then the absolute value is
15799          the same as the ordinary value.  */
15800       if (TYPE_UNSIGNED (type))
15801         t = arg0;
15802       /* Similarly, if the value is non-negative.  */
15803       else if (INT_CST_LT (integer_minus_one_node, arg0))
15804         t = arg0;
15805       /* If the value is negative, then the absolute value is
15806          its negation.  */
15807       else
15808         {
15809           unsigned HOST_WIDE_INT low;
15810           HOST_WIDE_INT high;
15811           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15812                                      TREE_INT_CST_HIGH (arg0),
15813                                      &low, &high);
15814           t = force_fit_type_double (type, low, high, -1,
15815                                      overflow | TREE_OVERFLOW (arg0));
15816         }
15817       break;
15818
15819     case REAL_CST:
15820       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15821         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15822       else
15823         t =  arg0;
15824       break;
15825
15826     default:
15827       gcc_unreachable ();
15828     }
15829
15830   return t;
15831 }
15832
15833 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15834    constant.  TYPE is the type of the result.  */
15835
15836 static tree
15837 fold_not_const (tree arg0, tree type)
15838 {
15839   tree t = NULL_TREE;
15840
15841   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15842
15843   t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15844                              ~TREE_INT_CST_HIGH (arg0), 0,
15845                              TREE_OVERFLOW (arg0));
15846
15847   return t;
15848 }
15849
15850 /* Given CODE, a relational operator, the target type, TYPE and two
15851    constant operands OP0 and OP1, return the result of the
15852    relational operation.  If the result is not a compile time
15853    constant, then return NULL_TREE.  */
15854
15855 static tree
15856 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15857 {
15858   int result, invert;
15859
15860   /* From here on, the only cases we handle are when the result is
15861      known to be a constant.  */
15862
15863   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15864     {
15865       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15866       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15867
15868       /* Handle the cases where either operand is a NaN.  */
15869       if (real_isnan (c0) || real_isnan (c1))
15870         {
15871           switch (code)
15872             {
15873             case EQ_EXPR:
15874             case ORDERED_EXPR:
15875               result = 0;
15876               break;
15877
15878             case NE_EXPR:
15879             case UNORDERED_EXPR:
15880             case UNLT_EXPR:
15881             case UNLE_EXPR:
15882             case UNGT_EXPR:
15883             case UNGE_EXPR:
15884             case UNEQ_EXPR:
15885               result = 1;
15886               break;
15887
15888             case LT_EXPR:
15889             case LE_EXPR:
15890             case GT_EXPR:
15891             case GE_EXPR:
15892             case LTGT_EXPR:
15893               if (flag_trapping_math)
15894                 return NULL_TREE;
15895               result = 0;
15896               break;
15897
15898             default:
15899               gcc_unreachable ();
15900             }
15901
15902           return constant_boolean_node (result, type);
15903         }
15904
15905       return constant_boolean_node (real_compare (code, c0, c1), type);
15906     }
15907
15908   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15909     {
15910       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15911       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15912       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15913     }
15914
15915   /* Handle equality/inequality of complex constants.  */
15916   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15917     {
15918       tree rcond = fold_relational_const (code, type,
15919                                           TREE_REALPART (op0),
15920                                           TREE_REALPART (op1));
15921       tree icond = fold_relational_const (code, type,
15922                                           TREE_IMAGPART (op0),
15923                                           TREE_IMAGPART (op1));
15924       if (code == EQ_EXPR)
15925         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15926       else if (code == NE_EXPR)
15927         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15928       else
15929         return NULL_TREE;
15930     }
15931
15932   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15933
15934      To compute GT, swap the arguments and do LT.
15935      To compute GE, do LT and invert the result.
15936      To compute LE, swap the arguments, do LT and invert the result.
15937      To compute NE, do EQ and invert the result.
15938
15939      Therefore, the code below must handle only EQ and LT.  */
15940
15941   if (code == LE_EXPR || code == GT_EXPR)
15942     {
15943       tree tem = op0;
15944       op0 = op1;
15945       op1 = tem;
15946       code = swap_tree_comparison (code);
15947     }
15948
15949   /* Note that it is safe to invert for real values here because we
15950      have already handled the one case that it matters.  */
15951
15952   invert = 0;
15953   if (code == NE_EXPR || code == GE_EXPR)
15954     {
15955       invert = 1;
15956       code = invert_tree_comparison (code, false);
15957     }
15958
15959   /* Compute a result for LT or EQ if args permit;
15960      Otherwise return T.  */
15961   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15962     {
15963       if (code == EQ_EXPR)
15964         result = tree_int_cst_equal (op0, op1);
15965       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15966         result = INT_CST_LT_UNSIGNED (op0, op1);
15967       else
15968         result = INT_CST_LT (op0, op1);
15969     }
15970   else
15971     return NULL_TREE;
15972
15973   if (invert)
15974     result ^= 1;
15975   return constant_boolean_node (result, type);
15976 }
15977
15978 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15979    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15980    itself.  */
15981
15982 tree
15983 fold_build_cleanup_point_expr (tree type, tree expr)
15984 {
15985   /* If the expression does not have side effects then we don't have to wrap
15986      it with a cleanup point expression.  */
15987   if (!TREE_SIDE_EFFECTS (expr))
15988     return expr;
15989
15990   /* If the expression is a return, check to see if the expression inside the
15991      return has no side effects or the right hand side of the modify expression
15992      inside the return. If either don't have side effects set we don't need to
15993      wrap the expression in a cleanup point expression.  Note we don't check the
15994      left hand side of the modify because it should always be a return decl.  */
15995   if (TREE_CODE (expr) == RETURN_EXPR)
15996     {
15997       tree op = TREE_OPERAND (expr, 0);
15998       if (!op || !TREE_SIDE_EFFECTS (op))
15999         return expr;
16000       op = TREE_OPERAND (op, 1);
16001       if (!TREE_SIDE_EFFECTS (op))
16002         return expr;
16003     }
16004
16005   return build1 (CLEANUP_POINT_EXPR, type, expr);
16006 }
16007
16008 /* Given a pointer value OP0 and a type TYPE, return a simplified version
16009    of an indirection through OP0, or NULL_TREE if no simplification is
16010    possible.  */
16011
16012 tree
16013 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16014 {
16015   tree sub = op0;
16016   tree subtype;
16017
16018   STRIP_NOPS (sub);
16019   subtype = TREE_TYPE (sub);
16020   if (!POINTER_TYPE_P (subtype))
16021     return NULL_TREE;
16022
16023   if (TREE_CODE (sub) == ADDR_EXPR)
16024     {
16025       tree op = TREE_OPERAND (sub, 0);
16026       tree optype = TREE_TYPE (op);
16027       /* *&CONST_DECL -> to the value of the const decl.  */
16028       if (TREE_CODE (op) == CONST_DECL)
16029         return DECL_INITIAL (op);
16030       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
16031       if (type == optype)
16032         {
16033           tree fop = fold_read_from_constant_string (op);
16034           if (fop)
16035             return fop;
16036           else
16037             return op;
16038         }
16039       /* *(foo *)&fooarray => fooarray[0] */
16040       else if (TREE_CODE (optype) == ARRAY_TYPE
16041                && type == TREE_TYPE (optype))
16042         {
16043           tree type_domain = TYPE_DOMAIN (optype);
16044           tree min_val = size_zero_node;
16045           if (type_domain && TYPE_MIN_VALUE (type_domain))
16046             min_val = TYPE_MIN_VALUE (type_domain);
16047           op0 = build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
16048           SET_EXPR_LOCATION (op0, loc);
16049           return op0;
16050         }
16051       /* *(foo *)&complexfoo => __real__ complexfoo */
16052       else if (TREE_CODE (optype) == COMPLEX_TYPE
16053                && type == TREE_TYPE (optype))
16054         return fold_build1_loc (loc, REALPART_EXPR, type, op);
16055       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16056       else if (TREE_CODE (optype) == VECTOR_TYPE
16057                && type == TREE_TYPE (optype))
16058         {
16059           tree part_width = TYPE_SIZE (type);
16060           tree index = bitsize_int (0);
16061           return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
16062         }
16063     }
16064
16065   /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16066   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16067       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
16068     {
16069       tree op00 = TREE_OPERAND (sub, 0);
16070       tree op01 = TREE_OPERAND (sub, 1);
16071       tree op00type;
16072
16073       STRIP_NOPS (op00);
16074       op00type = TREE_TYPE (op00);
16075       if (TREE_CODE (op00) == ADDR_EXPR
16076           && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
16077           && type == TREE_TYPE (TREE_TYPE (op00type)))
16078         {
16079           HOST_WIDE_INT offset = tree_low_cst (op01, 0);
16080           tree part_width = TYPE_SIZE (type);
16081           unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
16082           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
16083           tree index = bitsize_int (indexi);
16084
16085           if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
16086             return fold_build3_loc (loc,
16087                                 BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
16088                                 part_width, index);
16089
16090         }
16091     }
16092
16093
16094   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16095   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16096       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
16097     {
16098       tree op00 = TREE_OPERAND (sub, 0);
16099       tree op01 = TREE_OPERAND (sub, 1);
16100       tree op00type;
16101
16102       STRIP_NOPS (op00);
16103       op00type = TREE_TYPE (op00);
16104       if (TREE_CODE (op00) == ADDR_EXPR
16105           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
16106           && type == TREE_TYPE (TREE_TYPE (op00type)))
16107         {
16108           tree size = TYPE_SIZE_UNIT (type);
16109           if (tree_int_cst_equal (size, op01))
16110             return fold_build1_loc (loc, IMAGPART_EXPR, type,
16111                                 TREE_OPERAND (op00, 0));
16112         }
16113     }
16114
16115   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16116   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16117       && type == TREE_TYPE (TREE_TYPE (subtype)))
16118     {
16119       tree type_domain;
16120       tree min_val = size_zero_node;
16121       sub = build_fold_indirect_ref_loc (loc, sub);
16122       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16123       if (type_domain && TYPE_MIN_VALUE (type_domain))
16124         min_val = TYPE_MIN_VALUE (type_domain);
16125       op0 = build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
16126       SET_EXPR_LOCATION (op0, loc);
16127       return op0;
16128     }
16129
16130   return NULL_TREE;
16131 }
16132
16133 /* Builds an expression for an indirection through T, simplifying some
16134    cases.  */
16135
16136 tree
16137 build_fold_indirect_ref_loc (location_t loc, tree t)
16138 {
16139   tree type = TREE_TYPE (TREE_TYPE (t));
16140   tree sub = fold_indirect_ref_1 (loc, type, t);
16141
16142   if (sub)
16143     return sub;
16144
16145   t = build1 (INDIRECT_REF, type, t);
16146   SET_EXPR_LOCATION (t, loc);
16147   return t;
16148 }
16149
16150 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
16151
16152 tree
16153 fold_indirect_ref_loc (location_t loc, tree t)
16154 {
16155   tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16156
16157   if (sub)
16158     return sub;
16159   else
16160     return t;
16161 }
16162
16163 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16164    whose result is ignored.  The type of the returned tree need not be
16165    the same as the original expression.  */
16166
16167 tree
16168 fold_ignored_result (tree t)
16169 {
16170   if (!TREE_SIDE_EFFECTS (t))
16171     return integer_zero_node;
16172
16173   for (;;)
16174     switch (TREE_CODE_CLASS (TREE_CODE (t)))
16175       {
16176       case tcc_unary:
16177         t = TREE_OPERAND (t, 0);
16178         break;
16179
16180       case tcc_binary:
16181       case tcc_comparison:
16182         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16183           t = TREE_OPERAND (t, 0);
16184         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16185           t = TREE_OPERAND (t, 1);
16186         else
16187           return t;
16188         break;
16189
16190       case tcc_expression:
16191         switch (TREE_CODE (t))
16192           {
16193           case COMPOUND_EXPR:
16194             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16195               return t;
16196             t = TREE_OPERAND (t, 0);
16197             break;
16198
16199           case COND_EXPR:
16200             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16201                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16202               return t;
16203             t = TREE_OPERAND (t, 0);
16204             break;
16205
16206           default:
16207             return t;
16208           }
16209         break;
16210
16211       default:
16212         return t;
16213       }
16214 }
16215
16216 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
16217    This can only be applied to objects of a sizetype.  */
16218
16219 tree
16220 round_up_loc (location_t loc, tree value, int divisor)
16221 {
16222   tree div = NULL_TREE;
16223
16224   gcc_assert (divisor > 0);
16225   if (divisor == 1)
16226     return value;
16227
16228   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
16229      have to do anything.  Only do this when we are not given a const,
16230      because in that case, this check is more expensive than just
16231      doing it.  */
16232   if (TREE_CODE (value) != INTEGER_CST)
16233     {
16234       div = build_int_cst (TREE_TYPE (value), divisor);
16235
16236       if (multiple_of_p (TREE_TYPE (value), value, div))
16237         return value;
16238     }
16239
16240   /* If divisor is a power of two, simplify this to bit manipulation.  */
16241   if (divisor == (divisor & -divisor))
16242     {
16243       if (TREE_CODE (value) == INTEGER_CST)
16244         {
16245           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
16246           unsigned HOST_WIDE_INT high;
16247           bool overflow_p;
16248
16249           if ((low & (divisor - 1)) == 0)
16250             return value;
16251
16252           overflow_p = TREE_OVERFLOW (value);
16253           high = TREE_INT_CST_HIGH (value);
16254           low &= ~(divisor - 1);
16255           low += divisor;
16256           if (low == 0)
16257             {
16258               high++;
16259               if (high == 0)
16260                 overflow_p = true;
16261             }
16262
16263           return force_fit_type_double (TREE_TYPE (value), low, high,
16264                                         -1, overflow_p);
16265         }
16266       else
16267         {
16268           tree t;
16269
16270           t = build_int_cst (TREE_TYPE (value), divisor - 1);
16271           value = size_binop_loc (loc, PLUS_EXPR, value, t);
16272           t = build_int_cst (TREE_TYPE (value), -divisor);
16273           value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16274         }
16275     }
16276   else
16277     {
16278       if (!div)
16279         div = build_int_cst (TREE_TYPE (value), divisor);
16280       value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16281       value = size_binop_loc (loc, MULT_EXPR, value, div);
16282     }
16283
16284   return value;
16285 }
16286
16287 /* Likewise, but round down.  */
16288
16289 tree
16290 round_down_loc (location_t loc, tree value, int divisor)
16291 {
16292   tree div = NULL_TREE;
16293
16294   gcc_assert (divisor > 0);
16295   if (divisor == 1)
16296     return value;
16297
16298   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
16299      have to do anything.  Only do this when we are not given a const,
16300      because in that case, this check is more expensive than just
16301      doing it.  */
16302   if (TREE_CODE (value) != INTEGER_CST)
16303     {
16304       div = build_int_cst (TREE_TYPE (value), divisor);
16305
16306       if (multiple_of_p (TREE_TYPE (value), value, div))
16307         return value;
16308     }
16309
16310   /* If divisor is a power of two, simplify this to bit manipulation.  */
16311   if (divisor == (divisor & -divisor))
16312     {
16313       tree t;
16314
16315       t = build_int_cst (TREE_TYPE (value), -divisor);
16316       value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16317     }
16318   else
16319     {
16320       if (!div)
16321         div = build_int_cst (TREE_TYPE (value), divisor);
16322       value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16323       value = size_binop_loc (loc, MULT_EXPR, value, div);
16324     }
16325
16326   return value;
16327 }
16328
16329 /* Returns the pointer to the base of the object addressed by EXP and
16330    extracts the information about the offset of the access, storing it
16331    to PBITPOS and POFFSET.  */
16332
16333 static tree
16334 split_address_to_core_and_offset (tree exp,
16335                                   HOST_WIDE_INT *pbitpos, tree *poffset)
16336 {
16337   tree core;
16338   enum machine_mode mode;
16339   int unsignedp, volatilep;
16340   HOST_WIDE_INT bitsize;
16341   location_t loc = EXPR_LOCATION (exp);
16342
16343   if (TREE_CODE (exp) == ADDR_EXPR)
16344     {
16345       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16346                                   poffset, &mode, &unsignedp, &volatilep,
16347                                   false);
16348       core = build_fold_addr_expr_loc (loc, core);
16349     }
16350   else
16351     {
16352       core = exp;
16353       *pbitpos = 0;
16354       *poffset = NULL_TREE;
16355     }
16356
16357   return core;
16358 }
16359
16360 /* Returns true if addresses of E1 and E2 differ by a constant, false
16361    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
16362
16363 bool
16364 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
16365 {
16366   tree core1, core2;
16367   HOST_WIDE_INT bitpos1, bitpos2;
16368   tree toffset1, toffset2, tdiff, type;
16369
16370   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16371   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16372
16373   if (bitpos1 % BITS_PER_UNIT != 0
16374       || bitpos2 % BITS_PER_UNIT != 0
16375       || !operand_equal_p (core1, core2, 0))
16376     return false;
16377
16378   if (toffset1 && toffset2)
16379     {
16380       type = TREE_TYPE (toffset1);
16381       if (type != TREE_TYPE (toffset2))
16382         toffset2 = fold_convert (type, toffset2);
16383
16384       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16385       if (!cst_and_fits_in_hwi (tdiff))
16386         return false;
16387
16388       *diff = int_cst_value (tdiff);
16389     }
16390   else if (toffset1 || toffset2)
16391     {
16392       /* If only one of the offsets is non-constant, the difference cannot
16393          be a constant.  */
16394       return false;
16395     }
16396   else
16397     *diff = 0;
16398
16399   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
16400   return true;
16401 }
16402
16403 /* Simplify the floating point expression EXP when the sign of the
16404    result is not significant.  Return NULL_TREE if no simplification
16405    is possible.  */
16406
16407 tree
16408 fold_strip_sign_ops (tree exp)
16409 {
16410   tree arg0, arg1;
16411   location_t loc = EXPR_LOCATION (exp);
16412
16413   switch (TREE_CODE (exp))
16414     {
16415     case ABS_EXPR:
16416     case NEGATE_EXPR:
16417       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16418       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
16419
16420     case MULT_EXPR:
16421     case RDIV_EXPR:
16422       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
16423         return NULL_TREE;
16424       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16425       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16426       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
16427         return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
16428                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
16429                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
16430       break;
16431
16432     case COMPOUND_EXPR:
16433       arg0 = TREE_OPERAND (exp, 0);
16434       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16435       if (arg1)
16436         return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16437       break;
16438
16439     case COND_EXPR:
16440       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16441       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16442       if (arg0 || arg1)
16443         return fold_build3_loc (loc,
16444                             COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16445                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
16446                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
16447       break;
16448
16449     case CALL_EXPR:
16450       {
16451         const enum built_in_function fcode = builtin_mathfn_code (exp);
16452         switch (fcode)
16453         {
16454         CASE_FLT_FN (BUILT_IN_COPYSIGN):
16455           /* Strip copysign function call, return the 1st argument. */
16456           arg0 = CALL_EXPR_ARG (exp, 0);
16457           arg1 = CALL_EXPR_ARG (exp, 1);
16458           return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16459
16460         default:
16461           /* Strip sign ops from the argument of "odd" math functions.  */
16462           if (negate_mathfn_p (fcode))
16463             {
16464               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16465               if (arg0)
16466                 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
16467             }
16468           break;
16469         }
16470       }
16471       break;
16472
16473     default:
16474       break;
16475     }
16476   return NULL_TREE;
16477 }