OSDN Git Service

Fix misapplied patch.
[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;
210   int sign_extended_type;
211
212   if (POINTER_TYPE_P (type)
213       || TREE_CODE (type) == OFFSET_TYPE)
214     prec = POINTER_SIZE;
215   else
216     prec = TYPE_PRECISION (type);
217
218   /* Size types *are* sign extended.  */
219   sign_extended_type = (!TYPE_UNSIGNED (type)
220                         || (TREE_CODE (type) == INTEGER_TYPE
221                             && TYPE_IS_SIZETYPE (type)));
222
223   /* First clear all bits that are beyond the type's precision.  */
224   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
225     ;
226   else if (prec > HOST_BITS_PER_WIDE_INT)
227     h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
228   else
229     {
230       h1 = 0;
231       if (prec < HOST_BITS_PER_WIDE_INT)
232         l1 &= ~((HOST_WIDE_INT) (-1) << prec);
233     }
234
235   /* Then do sign extension if necessary.  */
236   if (!sign_extended_type)
237     /* No sign extension */;
238   else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
239     /* Correct width already.  */;
240   else if (prec > HOST_BITS_PER_WIDE_INT)
241     {
242       /* Sign extend top half? */
243       if (h1 & ((unsigned HOST_WIDE_INT)1
244                 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
245         h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
246     }
247   else if (prec == HOST_BITS_PER_WIDE_INT)
248     {
249       if ((HOST_WIDE_INT)l1 < 0)
250         h1 = -1;
251     }
252   else
253     {
254       /* Sign extend bottom half? */
255       if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
256         {
257           h1 = -1;
258           l1 |= (HOST_WIDE_INT)(-1) << prec;
259         }
260     }
261
262   *lv = l1;
263   *hv = h1;
264
265   /* If the value didn't fit, signal overflow.  */
266   return l1 != low0 || h1 != high0;
267 }
268
269 /* We force the double-int HIGH:LOW to the range of the type TYPE by
270    sign or zero extending it.
271    OVERFLOWABLE indicates if we are interested
272    in overflow of the value, when >0 we are only interested in signed
273    overflow, for <0 we are interested in any overflow.  OVERFLOWED
274    indicates whether overflow has already occurred.  CONST_OVERFLOWED
275    indicates whether constant overflow has already occurred.  We force
276    T's value to be within range of T's type (by setting to 0 or 1 all
277    the bits outside the type's range).  We set TREE_OVERFLOWED if,
278         OVERFLOWED is nonzero,
279         or OVERFLOWABLE is >0 and signed overflow occurs
280         or OVERFLOWABLE is <0 and any overflow occurs
281    We return a new tree node for the extended double-int.  The node
282    is shared if no overflow flags are set.  */
283
284 tree
285 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
286                        HOST_WIDE_INT high, int overflowable,
287                        bool overflowed)
288 {
289   int sign_extended_type;
290   bool overflow;
291
292   /* Size types *are* sign extended.  */
293   sign_extended_type = (!TYPE_UNSIGNED (type)
294                         || (TREE_CODE (type) == INTEGER_TYPE
295                             && TYPE_IS_SIZETYPE (type)));
296
297   overflow = fit_double_type (low, high, &low, &high, type);
298
299   /* If we need to set overflow flags, return a new unshared node.  */
300   if (overflowed || overflow)
301     {
302       if (overflowed
303           || overflowable < 0
304           || (overflowable > 0 && sign_extended_type))
305         {
306           tree t = make_node (INTEGER_CST);
307           TREE_INT_CST_LOW (t) = low;
308           TREE_INT_CST_HIGH (t) = high;
309           TREE_TYPE (t) = type;
310           TREE_OVERFLOW (t) = 1;
311           return t;
312         }
313     }
314
315   /* Else build a shared node.  */
316   return build_int_cst_wide (type, low, high);
317 }
318 \f
319 /* Add two doubleword integers with doubleword result.
320    Return nonzero if the operation overflows according to UNSIGNED_P.
321    Each argument is given as two `HOST_WIDE_INT' pieces.
322    One argument is L1 and H1; the other, L2 and H2.
323    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
324
325 int
326 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
327                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
328                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
329                       bool unsigned_p)
330 {
331   unsigned HOST_WIDE_INT l;
332   HOST_WIDE_INT h;
333
334   l = l1 + l2;
335   h = h1 + h2 + (l < l1);
336
337   *lv = l;
338   *hv = h;
339
340   if (unsigned_p)
341     return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
342   else
343     return OVERFLOW_SUM_SIGN (h1, h2, h);
344 }
345
346 /* Negate a doubleword integer with doubleword result.
347    Return nonzero if the operation overflows, assuming it's signed.
348    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
349    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
350
351 int
352 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
353             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
354 {
355   if (l1 == 0)
356     {
357       *lv = 0;
358       *hv = - h1;
359       return (*hv & h1) < 0;
360     }
361   else
362     {
363       *lv = -l1;
364       *hv = ~h1;
365       return 0;
366     }
367 }
368 \f
369 /* Multiply two doubleword integers with doubleword result.
370    Return nonzero if the operation overflows according to UNSIGNED_P.
371    Each argument is given as two `HOST_WIDE_INT' pieces.
372    One argument is L1 and H1; the other, L2 and H2.
373    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
374
375 int
376 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
377                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
378                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
379                       bool unsigned_p)
380 {
381   HOST_WIDE_INT arg1[4];
382   HOST_WIDE_INT arg2[4];
383   HOST_WIDE_INT prod[4 * 2];
384   unsigned HOST_WIDE_INT carry;
385   int i, j, k;
386   unsigned HOST_WIDE_INT toplow, neglow;
387   HOST_WIDE_INT tophigh, neghigh;
388
389   encode (arg1, l1, h1);
390   encode (arg2, l2, h2);
391
392   memset (prod, 0, sizeof prod);
393
394   for (i = 0; i < 4; i++)
395     {
396       carry = 0;
397       for (j = 0; j < 4; j++)
398         {
399           k = i + j;
400           /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
401           carry += arg1[i] * arg2[j];
402           /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
403           carry += prod[k];
404           prod[k] = LOWPART (carry);
405           carry = HIGHPART (carry);
406         }
407       prod[i + 4] = carry;
408     }
409
410   decode (prod, lv, hv);
411   decode (prod + 4, &toplow, &tophigh);
412
413   /* Unsigned overflow is immediate.  */
414   if (unsigned_p)
415     return (toplow | tophigh) != 0;
416
417   /* Check for signed overflow by calculating the signed representation of the
418      top half of the result; it should agree with the low half's sign bit.  */
419   if (h1 < 0)
420     {
421       neg_double (l2, h2, &neglow, &neghigh);
422       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
423     }
424   if (h2 < 0)
425     {
426       neg_double (l1, h1, &neglow, &neghigh);
427       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
428     }
429   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
430 }
431 \f
432 /* Shift the doubleword integer in L1, H1 left by COUNT places
433    keeping only PREC bits of result.
434    Shift right if COUNT is negative.
435    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
436    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
437
438 void
439 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
440                HOST_WIDE_INT count, unsigned int prec,
441                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
442 {
443   unsigned HOST_WIDE_INT signmask;
444
445   if (count < 0)
446     {
447       rshift_double (l1, h1, -count, prec, lv, hv, arith);
448       return;
449     }
450
451   if (SHIFT_COUNT_TRUNCATED)
452     count %= prec;
453
454   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
455     {
456       /* Shifting by the host word size is undefined according to the
457          ANSI standard, so we must handle this as a special case.  */
458       *hv = 0;
459       *lv = 0;
460     }
461   else if (count >= HOST_BITS_PER_WIDE_INT)
462     {
463       *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
464       *lv = 0;
465     }
466   else
467     {
468       *hv = (((unsigned HOST_WIDE_INT) h1 << count)
469              | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
470       *lv = l1 << count;
471     }
472
473   /* Sign extend all bits that are beyond the precision.  */
474
475   signmask = -((prec > HOST_BITS_PER_WIDE_INT
476                 ? ((unsigned HOST_WIDE_INT) *hv
477                    >> (prec - HOST_BITS_PER_WIDE_INT - 1))
478                 : (*lv >> (prec - 1))) & 1);
479
480   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
481     ;
482   else if (prec >= HOST_BITS_PER_WIDE_INT)
483     {
484       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
485       *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
486     }
487   else
488     {
489       *hv = signmask;
490       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
491       *lv |= signmask << prec;
492     }
493 }
494
495 /* Shift the doubleword integer in L1, H1 right by COUNT places
496    keeping only PREC bits of result.  COUNT must be positive.
497    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
498    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
499
500 void
501 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
502                HOST_WIDE_INT count, unsigned int prec,
503                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
504                int arith)
505 {
506   unsigned HOST_WIDE_INT signmask;
507
508   signmask = (arith
509               ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
510               : 0);
511
512   if (SHIFT_COUNT_TRUNCATED)
513     count %= prec;
514
515   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
516     {
517       /* Shifting by the host word size is undefined according to the
518          ANSI standard, so we must handle this as a special case.  */
519       *hv = 0;
520       *lv = 0;
521     }
522   else if (count >= HOST_BITS_PER_WIDE_INT)
523     {
524       *hv = 0;
525       *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
526     }
527   else
528     {
529       *hv = (unsigned HOST_WIDE_INT) h1 >> count;
530       *lv = ((l1 >> count)
531              | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
532     }
533
534   /* Zero / sign extend all bits that are beyond the precision.  */
535
536   if (count >= (HOST_WIDE_INT)prec)
537     {
538       *hv = signmask;
539       *lv = signmask;
540     }
541   else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
542     ;
543   else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
544     {
545       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
546       *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
547     }
548   else
549     {
550       *hv = signmask;
551       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
552       *lv |= signmask << (prec - count);
553     }
554 }
555 \f
556 /* Rotate the doubleword integer in L1, H1 left by COUNT places
557    keeping only PREC bits of result.
558    Rotate right if COUNT is negative.
559    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
560
561 void
562 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
563                 HOST_WIDE_INT count, unsigned int prec,
564                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
565 {
566   unsigned HOST_WIDE_INT s1l, s2l;
567   HOST_WIDE_INT s1h, s2h;
568
569   count %= prec;
570   if (count < 0)
571     count += prec;
572
573   lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
574   rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
575   *lv = s1l | s2l;
576   *hv = s1h | s2h;
577 }
578
579 /* Rotate the doubleword integer in L1, H1 left by COUNT places
580    keeping only PREC bits of result.  COUNT must be positive.
581    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
582
583 void
584 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
585                 HOST_WIDE_INT count, unsigned int prec,
586                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
587 {
588   unsigned HOST_WIDE_INT s1l, s2l;
589   HOST_WIDE_INT s1h, s2h;
590
591   count %= prec;
592   if (count < 0)
593     count += prec;
594
595   rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
596   lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
597   *lv = s1l | s2l;
598   *hv = s1h | s2h;
599 }
600 \f
601 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
602    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
603    CODE is a tree code for a kind of division, one of
604    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
605    or EXACT_DIV_EXPR
606    It controls how the quotient is rounded to an integer.
607    Return nonzero if the operation overflows.
608    UNS nonzero says do unsigned division.  */
609
610 int
611 div_and_round_double (enum tree_code code, int uns,
612                       unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
613                       HOST_WIDE_INT hnum_orig,
614                       unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
615                       HOST_WIDE_INT hden_orig,
616                       unsigned HOST_WIDE_INT *lquo,
617                       HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
618                       HOST_WIDE_INT *hrem)
619 {
620   int quo_neg = 0;
621   HOST_WIDE_INT num[4 + 1];     /* extra element for scaling.  */
622   HOST_WIDE_INT den[4], quo[4];
623   int i, j;
624   unsigned HOST_WIDE_INT work;
625   unsigned HOST_WIDE_INT carry = 0;
626   unsigned HOST_WIDE_INT lnum = lnum_orig;
627   HOST_WIDE_INT hnum = hnum_orig;
628   unsigned HOST_WIDE_INT lden = lden_orig;
629   HOST_WIDE_INT hden = hden_orig;
630   int overflow = 0;
631
632   if (hden == 0 && lden == 0)
633     overflow = 1, lden = 1;
634
635   /* Calculate quotient sign and convert operands to unsigned.  */
636   if (!uns)
637     {
638       if (hnum < 0)
639         {
640           quo_neg = ~ quo_neg;
641           /* (minimum integer) / (-1) is the only overflow case.  */
642           if (neg_double (lnum, hnum, &lnum, &hnum)
643               && ((HOST_WIDE_INT) lden & hden) == -1)
644             overflow = 1;
645         }
646       if (hden < 0)
647         {
648           quo_neg = ~ quo_neg;
649           neg_double (lden, hden, &lden, &hden);
650         }
651     }
652
653   if (hnum == 0 && hden == 0)
654     {                           /* single precision */
655       *hquo = *hrem = 0;
656       /* This unsigned division rounds toward zero.  */
657       *lquo = lnum / lden;
658       goto finish_up;
659     }
660
661   if (hnum == 0)
662     {                           /* trivial case: dividend < divisor */
663       /* hden != 0 already checked.  */
664       *hquo = *lquo = 0;
665       *hrem = hnum;
666       *lrem = lnum;
667       goto finish_up;
668     }
669
670   memset (quo, 0, sizeof quo);
671
672   memset (num, 0, sizeof num);  /* to zero 9th element */
673   memset (den, 0, sizeof den);
674
675   encode (num, lnum, hnum);
676   encode (den, lden, hden);
677
678   /* Special code for when the divisor < BASE.  */
679   if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
680     {
681       /* hnum != 0 already checked.  */
682       for (i = 4 - 1; i >= 0; i--)
683         {
684           work = num[i] + carry * BASE;
685           quo[i] = work / lden;
686           carry = work % lden;
687         }
688     }
689   else
690     {
691       /* Full double precision division,
692          with thanks to Don Knuth's "Seminumerical Algorithms".  */
693       int num_hi_sig, den_hi_sig;
694       unsigned HOST_WIDE_INT quo_est, scale;
695
696       /* Find the highest nonzero divisor digit.  */
697       for (i = 4 - 1;; i--)
698         if (den[i] != 0)
699           {
700             den_hi_sig = i;
701             break;
702           }
703
704       /* Insure that the first digit of the divisor is at least BASE/2.
705          This is required by the quotient digit estimation algorithm.  */
706
707       scale = BASE / (den[den_hi_sig] + 1);
708       if (scale > 1)
709         {               /* scale divisor and dividend */
710           carry = 0;
711           for (i = 0; i <= 4 - 1; i++)
712             {
713               work = (num[i] * scale) + carry;
714               num[i] = LOWPART (work);
715               carry = HIGHPART (work);
716             }
717
718           num[4] = carry;
719           carry = 0;
720           for (i = 0; i <= 4 - 1; i++)
721             {
722               work = (den[i] * scale) + carry;
723               den[i] = LOWPART (work);
724               carry = HIGHPART (work);
725               if (den[i] != 0) den_hi_sig = i;
726             }
727         }
728
729       num_hi_sig = 4;
730
731       /* Main loop */
732       for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
733         {
734           /* Guess the next quotient digit, quo_est, by dividing the first
735              two remaining dividend digits by the high order quotient digit.
736              quo_est is never low and is at most 2 high.  */
737           unsigned HOST_WIDE_INT tmp;
738
739           num_hi_sig = i + den_hi_sig + 1;
740           work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
741           if (num[num_hi_sig] != den[den_hi_sig])
742             quo_est = work / den[den_hi_sig];
743           else
744             quo_est = BASE - 1;
745
746           /* Refine quo_est so it's usually correct, and at most one high.  */
747           tmp = work - quo_est * den[den_hi_sig];
748           if (tmp < BASE
749               && (den[den_hi_sig - 1] * quo_est
750                   > (tmp * BASE + num[num_hi_sig - 2])))
751             quo_est--;
752
753           /* Try QUO_EST as the quotient digit, by multiplying the
754              divisor by QUO_EST and subtracting from the remaining dividend.
755              Keep in mind that QUO_EST is the I - 1st digit.  */
756
757           carry = 0;
758           for (j = 0; j <= den_hi_sig; j++)
759             {
760               work = quo_est * den[j] + carry;
761               carry = HIGHPART (work);
762               work = num[i + j] - LOWPART (work);
763               num[i + j] = LOWPART (work);
764               carry += HIGHPART (work) != 0;
765             }
766
767           /* If quo_est was high by one, then num[i] went negative and
768              we need to correct things.  */
769           if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
770             {
771               quo_est--;
772               carry = 0;                /* add divisor back in */
773               for (j = 0; j <= den_hi_sig; j++)
774                 {
775                   work = num[i + j] + den[j] + carry;
776                   carry = HIGHPART (work);
777                   num[i + j] = LOWPART (work);
778                 }
779
780               num [num_hi_sig] += carry;
781             }
782
783           /* Store the quotient digit.  */
784           quo[i] = quo_est;
785         }
786     }
787
788   decode (quo, lquo, hquo);
789
790  finish_up:
791   /* If result is negative, make it so.  */
792   if (quo_neg)
793     neg_double (*lquo, *hquo, lquo, hquo);
794
795   /* Compute trial remainder:  rem = num - (quo * den)  */
796   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
797   neg_double (*lrem, *hrem, lrem, hrem);
798   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
799
800   switch (code)
801     {
802     case TRUNC_DIV_EXPR:
803     case TRUNC_MOD_EXPR:        /* round toward zero */
804     case EXACT_DIV_EXPR:        /* for this one, it shouldn't matter */
805       return overflow;
806
807     case FLOOR_DIV_EXPR:
808     case FLOOR_MOD_EXPR:        /* round toward negative infinity */
809       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
810         {
811           /* quo = quo - 1;  */
812           add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
813                       lquo, hquo);
814         }
815       else
816         return overflow;
817       break;
818
819     case CEIL_DIV_EXPR:
820     case CEIL_MOD_EXPR:         /* round toward positive infinity */
821       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
822         {
823           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
824                       lquo, hquo);
825         }
826       else
827         return overflow;
828       break;
829
830     case ROUND_DIV_EXPR:
831     case ROUND_MOD_EXPR:        /* round to closest integer */
832       {
833         unsigned HOST_WIDE_INT labs_rem = *lrem;
834         HOST_WIDE_INT habs_rem = *hrem;
835         unsigned HOST_WIDE_INT labs_den = lden, ltwice;
836         HOST_WIDE_INT habs_den = hden, htwice;
837
838         /* Get absolute values.  */
839         if (*hrem < 0)
840           neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
841         if (hden < 0)
842           neg_double (lden, hden, &labs_den, &habs_den);
843
844         /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient.  */
845         mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
846                     labs_rem, habs_rem, &ltwice, &htwice);
847
848         if (((unsigned HOST_WIDE_INT) habs_den
849              < (unsigned HOST_WIDE_INT) htwice)
850             || (((unsigned HOST_WIDE_INT) habs_den
851                  == (unsigned HOST_WIDE_INT) htwice)
852                 && (labs_den <= ltwice)))
853           {
854             if (*hquo < 0)
855               /* quo = quo - 1;  */
856               add_double (*lquo, *hquo,
857                           (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
858             else
859               /* quo = quo + 1; */
860               add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
861                           lquo, hquo);
862           }
863         else
864           return overflow;
865       }
866       break;
867
868     default:
869       gcc_unreachable ();
870     }
871
872   /* Compute true remainder:  rem = num - (quo * den)  */
873   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
874   neg_double (*lrem, *hrem, lrem, hrem);
875   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
876   return overflow;
877 }
878
879 /* If ARG2 divides ARG1 with zero remainder, carries out the division
880    of type CODE and returns the quotient.
881    Otherwise returns NULL_TREE.  */
882
883 tree
884 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
885 {
886   unsigned HOST_WIDE_INT int1l, int2l;
887   HOST_WIDE_INT int1h, int2h;
888   unsigned HOST_WIDE_INT quol, reml;
889   HOST_WIDE_INT quoh, remh;
890   tree type = TREE_TYPE (arg1);
891   int uns = TYPE_UNSIGNED (type);
892
893   int1l = TREE_INT_CST_LOW (arg1);
894   int1h = TREE_INT_CST_HIGH (arg1);
895   /* &obj[0] + -128 really should be compiled as &obj[-8] rather than
896      &obj[some_exotic_number].  */
897   if (POINTER_TYPE_P (type))
898     {
899       uns = false;
900       type = signed_type_for (type);
901       fit_double_type (int1l, int1h, &int1l, &int1h,
902                        type);
903     }
904   else
905     fit_double_type (int1l, int1h, &int1l, &int1h, type);
906   int2l = TREE_INT_CST_LOW (arg2);
907   int2h = TREE_INT_CST_HIGH (arg2);
908
909   div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
910                         &quol, &quoh, &reml, &remh);
911   if (remh != 0 || reml != 0)
912     return NULL_TREE;
913
914   return build_int_cst_wide (type, quol, quoh);
915 }
916 \f
917 /* This is nonzero if we should defer warnings about undefined
918    overflow.  This facility exists because these warnings are a
919    special case.  The code to estimate loop iterations does not want
920    to issue any warnings, since it works with expressions which do not
921    occur in user code.  Various bits of cleanup code call fold(), but
922    only use the result if it has certain characteristics (e.g., is a
923    constant); that code only wants to issue a warning if the result is
924    used.  */
925
926 static int fold_deferring_overflow_warnings;
927
928 /* If a warning about undefined overflow is deferred, this is the
929    warning.  Note that this may cause us to turn two warnings into
930    one, but that is fine since it is sufficient to only give one
931    warning per expression.  */
932
933 static const char* fold_deferred_overflow_warning;
934
935 /* If a warning about undefined overflow is deferred, this is the
936    level at which the warning should be emitted.  */
937
938 static enum warn_strict_overflow_code fold_deferred_overflow_code;
939
940 /* Start deferring overflow warnings.  We could use a stack here to
941    permit nested calls, but at present it is not necessary.  */
942
943 void
944 fold_defer_overflow_warnings (void)
945 {
946   ++fold_deferring_overflow_warnings;
947 }
948
949 /* Stop deferring overflow warnings.  If there is a pending warning,
950    and ISSUE is true, then issue the warning if appropriate.  STMT is
951    the statement with which the warning should be associated (used for
952    location information); STMT may be NULL.  CODE is the level of the
953    warning--a warn_strict_overflow_code value.  This function will use
954    the smaller of CODE and the deferred code when deciding whether to
955    issue the warning.  CODE may be zero to mean to always use the
956    deferred code.  */
957
958 void
959 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
960 {
961   const char *warnmsg;
962   location_t locus;
963
964   gcc_assert (fold_deferring_overflow_warnings > 0);
965   --fold_deferring_overflow_warnings;
966   if (fold_deferring_overflow_warnings > 0)
967     {
968       if (fold_deferred_overflow_warning != NULL
969           && code != 0
970           && code < (int) fold_deferred_overflow_code)
971         fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
972       return;
973     }
974
975   warnmsg = fold_deferred_overflow_warning;
976   fold_deferred_overflow_warning = NULL;
977
978   if (!issue || warnmsg == NULL)
979     return;
980
981   if (gimple_no_warning_p (stmt))
982     return;
983
984   /* Use the smallest code level when deciding to issue the
985      warning.  */
986   if (code == 0 || code > (int) fold_deferred_overflow_code)
987     code = fold_deferred_overflow_code;
988
989   if (!issue_strict_overflow_warning (code))
990     return;
991
992   if (stmt == NULL)
993     locus = input_location;
994   else
995     locus = gimple_location (stmt);
996   warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
997 }
998
999 /* Stop deferring overflow warnings, ignoring any deferred
1000    warnings.  */
1001
1002 void
1003 fold_undefer_and_ignore_overflow_warnings (void)
1004 {
1005   fold_undefer_overflow_warnings (false, NULL, 0);
1006 }
1007
1008 /* Whether we are deferring overflow warnings.  */
1009
1010 bool
1011 fold_deferring_overflow_warnings_p (void)
1012 {
1013   return fold_deferring_overflow_warnings > 0;
1014 }
1015
1016 /* This is called when we fold something based on the fact that signed
1017    overflow is undefined.  */
1018
1019 static void
1020 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1021 {
1022   if (fold_deferring_overflow_warnings > 0)
1023     {
1024       if (fold_deferred_overflow_warning == NULL
1025           || wc < fold_deferred_overflow_code)
1026         {
1027           fold_deferred_overflow_warning = gmsgid;
1028           fold_deferred_overflow_code = wc;
1029         }
1030     }
1031   else if (issue_strict_overflow_warning (wc))
1032     warning (OPT_Wstrict_overflow, gmsgid);
1033 }
1034 \f
1035 /* Return true if the built-in mathematical function specified by CODE
1036    is odd, i.e. -f(x) == f(-x).  */
1037
1038 static bool
1039 negate_mathfn_p (enum built_in_function code)
1040 {
1041   switch (code)
1042     {
1043     CASE_FLT_FN (BUILT_IN_ASIN):
1044     CASE_FLT_FN (BUILT_IN_ASINH):
1045     CASE_FLT_FN (BUILT_IN_ATAN):
1046     CASE_FLT_FN (BUILT_IN_ATANH):
1047     CASE_FLT_FN (BUILT_IN_CASIN):
1048     CASE_FLT_FN (BUILT_IN_CASINH):
1049     CASE_FLT_FN (BUILT_IN_CATAN):
1050     CASE_FLT_FN (BUILT_IN_CATANH):
1051     CASE_FLT_FN (BUILT_IN_CBRT):
1052     CASE_FLT_FN (BUILT_IN_CPROJ):
1053     CASE_FLT_FN (BUILT_IN_CSIN):
1054     CASE_FLT_FN (BUILT_IN_CSINH):
1055     CASE_FLT_FN (BUILT_IN_CTAN):
1056     CASE_FLT_FN (BUILT_IN_CTANH):
1057     CASE_FLT_FN (BUILT_IN_ERF):
1058     CASE_FLT_FN (BUILT_IN_LLROUND):
1059     CASE_FLT_FN (BUILT_IN_LROUND):
1060     CASE_FLT_FN (BUILT_IN_ROUND):
1061     CASE_FLT_FN (BUILT_IN_SIN):
1062     CASE_FLT_FN (BUILT_IN_SINH):
1063     CASE_FLT_FN (BUILT_IN_TAN):
1064     CASE_FLT_FN (BUILT_IN_TANH):
1065     CASE_FLT_FN (BUILT_IN_TRUNC):
1066       return true;
1067
1068     CASE_FLT_FN (BUILT_IN_LLRINT):
1069     CASE_FLT_FN (BUILT_IN_LRINT):
1070     CASE_FLT_FN (BUILT_IN_NEARBYINT):
1071     CASE_FLT_FN (BUILT_IN_RINT):
1072       return !flag_rounding_math;
1073     
1074     default:
1075       break;
1076     }
1077   return false;
1078 }
1079
1080 /* Check whether we may negate an integer constant T without causing
1081    overflow.  */
1082
1083 bool
1084 may_negate_without_overflow_p (const_tree t)
1085 {
1086   unsigned HOST_WIDE_INT val;
1087   unsigned int prec;
1088   tree type;
1089
1090   gcc_assert (TREE_CODE (t) == INTEGER_CST);
1091
1092   type = TREE_TYPE (t);
1093   if (TYPE_UNSIGNED (type))
1094     return false;
1095
1096   prec = TYPE_PRECISION (type);
1097   if (prec > HOST_BITS_PER_WIDE_INT)
1098     {
1099       if (TREE_INT_CST_LOW (t) != 0)
1100         return true;
1101       prec -= HOST_BITS_PER_WIDE_INT;
1102       val = TREE_INT_CST_HIGH (t);
1103     }
1104   else
1105     val = TREE_INT_CST_LOW (t);
1106   if (prec < HOST_BITS_PER_WIDE_INT)
1107     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1108   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1109 }
1110
1111 /* Determine whether an expression T can be cheaply negated using
1112    the function negate_expr without introducing undefined overflow.  */
1113
1114 static bool
1115 negate_expr_p (tree t)
1116 {
1117   tree type;
1118
1119   if (t == 0)
1120     return false;
1121
1122   type = TREE_TYPE (t);
1123
1124   STRIP_SIGN_NOPS (t);
1125   switch (TREE_CODE (t))
1126     {
1127     case INTEGER_CST:
1128       if (TYPE_OVERFLOW_WRAPS (type))
1129         return true;
1130
1131       /* Check that -CST will not overflow type.  */
1132       return may_negate_without_overflow_p (t);
1133     case BIT_NOT_EXPR:
1134       return (INTEGRAL_TYPE_P (type)
1135               && TYPE_OVERFLOW_WRAPS (type));
1136
1137     case FIXED_CST:
1138     case REAL_CST:
1139     case NEGATE_EXPR:
1140       return true;
1141
1142     case COMPLEX_CST:
1143       return negate_expr_p (TREE_REALPART (t))
1144              && negate_expr_p (TREE_IMAGPART (t));
1145
1146     case COMPLEX_EXPR:
1147       return negate_expr_p (TREE_OPERAND (t, 0))
1148              && negate_expr_p (TREE_OPERAND (t, 1));
1149
1150     case CONJ_EXPR:
1151       return negate_expr_p (TREE_OPERAND (t, 0));
1152
1153     case PLUS_EXPR:
1154       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1155           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1156         return false;
1157       /* -(A + B) -> (-B) - A.  */
1158       if (negate_expr_p (TREE_OPERAND (t, 1))
1159           && reorder_operands_p (TREE_OPERAND (t, 0),
1160                                  TREE_OPERAND (t, 1)))
1161         return true;
1162       /* -(A + B) -> (-A) - B.  */
1163       return negate_expr_p (TREE_OPERAND (t, 0));
1164
1165     case MINUS_EXPR:
1166       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
1167       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1168              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1169              && reorder_operands_p (TREE_OPERAND (t, 0),
1170                                     TREE_OPERAND (t, 1));
1171
1172     case MULT_EXPR:
1173       if (TYPE_UNSIGNED (TREE_TYPE (t)))
1174         break;
1175
1176       /* Fall through.  */
1177
1178     case RDIV_EXPR:
1179       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1180         return negate_expr_p (TREE_OPERAND (t, 1))
1181                || negate_expr_p (TREE_OPERAND (t, 0));
1182       break;
1183
1184     case TRUNC_DIV_EXPR:
1185     case ROUND_DIV_EXPR:
1186     case FLOOR_DIV_EXPR:
1187     case CEIL_DIV_EXPR:
1188     case EXACT_DIV_EXPR:
1189       /* In general we can't negate A / B, because if A is INT_MIN and
1190          B is 1, we may turn this into INT_MIN / -1 which is undefined
1191          and actually traps on some architectures.  But if overflow is
1192          undefined, we can negate, because - (INT_MIN / 1) is an
1193          overflow.  */
1194       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1195           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1196         break;
1197       return negate_expr_p (TREE_OPERAND (t, 1))
1198              || negate_expr_p (TREE_OPERAND (t, 0));
1199
1200     case NOP_EXPR:
1201       /* Negate -((double)float) as (double)(-float).  */
1202       if (TREE_CODE (type) == REAL_TYPE)
1203         {
1204           tree tem = strip_float_extensions (t);
1205           if (tem != t)
1206             return negate_expr_p (tem);
1207         }
1208       break;
1209
1210     case CALL_EXPR:
1211       /* Negate -f(x) as f(-x).  */
1212       if (negate_mathfn_p (builtin_mathfn_code (t)))
1213         return negate_expr_p (CALL_EXPR_ARG (t, 0));
1214       break;
1215
1216     case RSHIFT_EXPR:
1217       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1218       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1219         {
1220           tree op1 = TREE_OPERAND (t, 1);
1221           if (TREE_INT_CST_HIGH (op1) == 0
1222               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1223                  == TREE_INT_CST_LOW (op1))
1224             return true;
1225         }
1226       break;
1227
1228     default:
1229       break;
1230     }
1231   return false;
1232 }
1233
1234 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1235    simplification is possible.
1236    If negate_expr_p would return true for T, NULL_TREE will never be
1237    returned.  */
1238
1239 static tree
1240 fold_negate_expr (location_t loc, tree t)
1241 {
1242   tree type = TREE_TYPE (t);
1243   tree tem;
1244
1245   switch (TREE_CODE (t))
1246     {
1247     /* Convert - (~A) to A + 1.  */
1248     case BIT_NOT_EXPR:
1249       if (INTEGRAL_TYPE_P (type))
1250         return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
1251                             build_int_cst (type, 1));
1252       break;
1253       
1254     case INTEGER_CST:
1255       tem = fold_negate_const (t, type);
1256       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1257           || !TYPE_OVERFLOW_TRAPS (type))
1258         return tem;
1259       break;
1260
1261     case REAL_CST:
1262       tem = fold_negate_const (t, type);
1263       /* Two's complement FP formats, such as c4x, may overflow.  */
1264       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1265         return tem;
1266       break;
1267
1268     case FIXED_CST:
1269       tem = fold_negate_const (t, type);
1270       return tem;
1271
1272     case COMPLEX_CST:
1273       {
1274         tree rpart = negate_expr (TREE_REALPART (t));
1275         tree ipart = negate_expr (TREE_IMAGPART (t));
1276
1277         if ((TREE_CODE (rpart) == REAL_CST
1278              && TREE_CODE (ipart) == REAL_CST)
1279             || (TREE_CODE (rpart) == INTEGER_CST
1280                 && TREE_CODE (ipart) == INTEGER_CST))
1281           return build_complex (type, rpart, ipart);
1282       }
1283       break;
1284
1285     case COMPLEX_EXPR:
1286       if (negate_expr_p (t))
1287         return fold_build2_loc (loc, COMPLEX_EXPR, type,
1288                             fold_negate_expr (loc, TREE_OPERAND (t, 0)),
1289                             fold_negate_expr (loc, TREE_OPERAND (t, 1)));
1290       break;
1291       
1292     case CONJ_EXPR:
1293       if (negate_expr_p (t))
1294         return fold_build1_loc (loc, CONJ_EXPR, type,
1295                             fold_negate_expr (loc, TREE_OPERAND (t, 0)));
1296       break;
1297
1298     case NEGATE_EXPR:
1299       return TREE_OPERAND (t, 0);
1300
1301     case PLUS_EXPR:
1302       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1303           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1304         {
1305           /* -(A + B) -> (-B) - A.  */
1306           if (negate_expr_p (TREE_OPERAND (t, 1))
1307               && reorder_operands_p (TREE_OPERAND (t, 0),
1308                                      TREE_OPERAND (t, 1)))
1309             {
1310               tem = negate_expr (TREE_OPERAND (t, 1));
1311               return fold_build2_loc (loc, MINUS_EXPR, type,
1312                                   tem, TREE_OPERAND (t, 0));
1313             }
1314
1315           /* -(A + B) -> (-A) - B.  */
1316           if (negate_expr_p (TREE_OPERAND (t, 0)))
1317             {
1318               tem = negate_expr (TREE_OPERAND (t, 0));
1319               return fold_build2_loc (loc, MINUS_EXPR, type,
1320                                   tem, TREE_OPERAND (t, 1));
1321             }
1322         }
1323       break;
1324
1325     case MINUS_EXPR:
1326       /* - (A - B) -> B - A  */
1327       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1328           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1329           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1330         return fold_build2_loc (loc, MINUS_EXPR, type,
1331                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1332       break;
1333
1334     case MULT_EXPR:
1335       if (TYPE_UNSIGNED (type))
1336         break;
1337
1338       /* Fall through.  */
1339
1340     case RDIV_EXPR:
1341       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1342         {
1343           tem = TREE_OPERAND (t, 1);
1344           if (negate_expr_p (tem))
1345             return fold_build2_loc (loc, TREE_CODE (t), type,
1346                                 TREE_OPERAND (t, 0), negate_expr (tem));
1347           tem = TREE_OPERAND (t, 0);
1348           if (negate_expr_p (tem))
1349             return fold_build2_loc (loc, TREE_CODE (t), type,
1350                                 negate_expr (tem), TREE_OPERAND (t, 1));
1351         }
1352       break;
1353
1354     case TRUNC_DIV_EXPR:
1355     case ROUND_DIV_EXPR:
1356     case FLOOR_DIV_EXPR:
1357     case CEIL_DIV_EXPR:
1358     case EXACT_DIV_EXPR:
1359       /* In general we can't negate A / B, because if A is INT_MIN and
1360          B is 1, we may turn this into INT_MIN / -1 which is undefined
1361          and actually traps on some architectures.  But if overflow is
1362          undefined, we can negate, because - (INT_MIN / 1) is an
1363          overflow.  */
1364       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1365         {
1366           const char * const warnmsg = G_("assuming signed overflow does not "
1367                                           "occur when negating a division");
1368           tem = TREE_OPERAND (t, 1);
1369           if (negate_expr_p (tem))
1370             {
1371               if (INTEGRAL_TYPE_P (type)
1372                   && (TREE_CODE (tem) != INTEGER_CST
1373                       || integer_onep (tem)))
1374                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1375               return fold_build2_loc (loc, TREE_CODE (t), type,
1376                                   TREE_OPERAND (t, 0), negate_expr (tem));
1377             }
1378           tem = TREE_OPERAND (t, 0);
1379           if (negate_expr_p (tem))
1380             {
1381               if (INTEGRAL_TYPE_P (type)
1382                   && (TREE_CODE (tem) != INTEGER_CST
1383                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1384                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1385               return fold_build2_loc (loc, TREE_CODE (t), type,
1386                                   negate_expr (tem), TREE_OPERAND (t, 1));
1387             }
1388         }
1389       break;
1390
1391     case NOP_EXPR:
1392       /* Convert -((double)float) into (double)(-float).  */
1393       if (TREE_CODE (type) == REAL_TYPE)
1394         {
1395           tem = strip_float_extensions (t);
1396           if (tem != t && negate_expr_p (tem))
1397             return fold_convert_loc (loc, type, negate_expr (tem));
1398         }
1399       break;
1400
1401     case CALL_EXPR:
1402       /* Negate -f(x) as f(-x).  */
1403       if (negate_mathfn_p (builtin_mathfn_code (t))
1404           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1405         {
1406           tree fndecl, arg;
1407
1408           fndecl = get_callee_fndecl (t);
1409           arg = negate_expr (CALL_EXPR_ARG (t, 0));
1410           return build_call_expr_loc (loc, fndecl, 1, arg);
1411         }
1412       break;
1413
1414     case RSHIFT_EXPR:
1415       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1416       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1417         {
1418           tree op1 = TREE_OPERAND (t, 1);
1419           if (TREE_INT_CST_HIGH (op1) == 0
1420               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1421                  == TREE_INT_CST_LOW (op1))
1422             {
1423               tree ntype = TYPE_UNSIGNED (type)
1424                            ? signed_type_for (type)
1425                            : unsigned_type_for (type);
1426               tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
1427               temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
1428               return fold_convert_loc (loc, type, temp);
1429             }
1430         }
1431       break;
1432
1433     default:
1434       break;
1435     }
1436
1437   return NULL_TREE;
1438 }
1439
1440 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1441    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
1442    return NULL_TREE. */
1443
1444 static tree
1445 negate_expr (tree t)
1446 {
1447   tree type, tem;
1448   location_t loc;
1449
1450   if (t == NULL_TREE)
1451     return NULL_TREE;
1452
1453   loc = EXPR_LOCATION (t);
1454   type = TREE_TYPE (t);
1455   STRIP_SIGN_NOPS (t);
1456
1457   tem = fold_negate_expr (loc, t);
1458   if (!tem)
1459     {
1460       tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1461       SET_EXPR_LOCATION (tem, loc);
1462     }
1463   return fold_convert_loc (loc, type, tem);
1464 }
1465 \f
1466 /* Split a tree IN into a constant, literal and variable parts that could be
1467    combined with CODE to make IN.  "constant" means an expression with
1468    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1469    commutative arithmetic operation.  Store the constant part into *CONP,
1470    the literal in *LITP and return the variable part.  If a part isn't
1471    present, set it to null.  If the tree does not decompose in this way,
1472    return the entire tree as the variable part and the other parts as null.
1473
1474    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1475    case, we negate an operand that was subtracted.  Except if it is a
1476    literal for which we use *MINUS_LITP instead.
1477
1478    If NEGATE_P is true, we are negating all of IN, again except a literal
1479    for which we use *MINUS_LITP instead.
1480
1481    If IN is itself a literal or constant, return it as appropriate.
1482
1483    Note that we do not guarantee that any of the three values will be the
1484    same type as IN, but they will have the same signedness and mode.  */
1485
1486 static tree
1487 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1488             tree *minus_litp, int negate_p)
1489 {
1490   tree var = 0;
1491
1492   *conp = 0;
1493   *litp = 0;
1494   *minus_litp = 0;
1495
1496   /* Strip any conversions that don't change the machine mode or signedness.  */
1497   STRIP_SIGN_NOPS (in);
1498
1499   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1500       || TREE_CODE (in) == FIXED_CST)
1501     *litp = in;
1502   else if (TREE_CODE (in) == code
1503            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1504                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1505                /* We can associate addition and subtraction together (even
1506                   though the C standard doesn't say so) for integers because
1507                   the value is not affected.  For reals, the value might be
1508                   affected, so we can't.  */
1509                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1510                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1511     {
1512       tree op0 = TREE_OPERAND (in, 0);
1513       tree op1 = TREE_OPERAND (in, 1);
1514       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1515       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1516
1517       /* First see if either of the operands is a literal, then a constant.  */
1518       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1519           || TREE_CODE (op0) == FIXED_CST)
1520         *litp = op0, op0 = 0;
1521       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1522                || TREE_CODE (op1) == FIXED_CST)
1523         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1524
1525       if (op0 != 0 && TREE_CONSTANT (op0))
1526         *conp = op0, op0 = 0;
1527       else if (op1 != 0 && TREE_CONSTANT (op1))
1528         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1529
1530       /* If we haven't dealt with either operand, this is not a case we can
1531          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1532       if (op0 != 0 && op1 != 0)
1533         var = in;
1534       else if (op0 != 0)
1535         var = op0;
1536       else
1537         var = op1, neg_var_p = neg1_p;
1538
1539       /* Now do any needed negations.  */
1540       if (neg_litp_p)
1541         *minus_litp = *litp, *litp = 0;
1542       if (neg_conp_p)
1543         *conp = negate_expr (*conp);
1544       if (neg_var_p)
1545         var = negate_expr (var);
1546     }
1547   else if (TREE_CONSTANT (in))
1548     *conp = in;
1549   else
1550     var = in;
1551
1552   if (negate_p)
1553     {
1554       if (*litp)
1555         *minus_litp = *litp, *litp = 0;
1556       else if (*minus_litp)
1557         *litp = *minus_litp, *minus_litp = 0;
1558       *conp = negate_expr (*conp);
1559       var = negate_expr (var);
1560     }
1561
1562   return var;
1563 }
1564
1565 /* Re-associate trees split by the above function.  T1 and T2 are
1566    either expressions to associate or null.  Return the new
1567    expression, if any.  LOC is the location of the new expression.  If
1568    we build an operation, do it in TYPE and with CODE.  */
1569
1570 static tree
1571 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
1572 {
1573   tree tem;
1574
1575   if (t1 == 0)
1576     return t2;
1577   else if (t2 == 0)
1578     return t1;
1579
1580   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1581      try to fold this since we will have infinite recursion.  But do
1582      deal with any NEGATE_EXPRs.  */
1583   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1584       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1585     {
1586       if (code == PLUS_EXPR)
1587         {
1588           if (TREE_CODE (t1) == NEGATE_EXPR)
1589             tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t2),
1590                           fold_convert_loc (loc, type, TREE_OPERAND (t1, 0)));
1591           else if (TREE_CODE (t2) == NEGATE_EXPR)
1592             tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t1),
1593                           fold_convert_loc (loc, type, TREE_OPERAND (t2, 0)));
1594           else if (integer_zerop (t2))
1595             return fold_convert_loc (loc, type, t1);
1596         }
1597       else if (code == MINUS_EXPR)
1598         {
1599           if (integer_zerop (t2))
1600             return fold_convert_loc (loc, type, t1);
1601         }
1602
1603       tem = build2 (code, type, fold_convert_loc (loc, type, t1),
1604                     fold_convert_loc (loc, type, t2));
1605       goto associate_trees_exit;
1606     }
1607
1608   return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1609                       fold_convert_loc (loc, type, t2));
1610  associate_trees_exit:
1611   protected_set_expr_location (tem, loc);
1612   return tem;
1613 }
1614 \f
1615 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1616    for use in int_const_binop, size_binop and size_diffop.  */
1617
1618 static bool
1619 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1620 {
1621   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1622     return false;
1623   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1624     return false;
1625
1626   switch (code)
1627     {
1628     case LSHIFT_EXPR:
1629     case RSHIFT_EXPR:
1630     case LROTATE_EXPR:
1631     case RROTATE_EXPR:
1632       return true;
1633
1634     default:
1635       break;
1636     }
1637
1638   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1639          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1640          && TYPE_MODE (type1) == TYPE_MODE (type2);
1641 }
1642
1643
1644 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1645    to produce a new constant.  Return NULL_TREE if we don't know how
1646    to evaluate CODE at compile-time.
1647
1648    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1649
1650 tree
1651 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1652 {
1653   unsigned HOST_WIDE_INT int1l, int2l;
1654   HOST_WIDE_INT int1h, int2h;
1655   unsigned HOST_WIDE_INT low;
1656   HOST_WIDE_INT hi;
1657   unsigned HOST_WIDE_INT garbagel;
1658   HOST_WIDE_INT garbageh;
1659   tree t;
1660   tree type = TREE_TYPE (arg1);
1661   int uns = TYPE_UNSIGNED (type);
1662   int is_sizetype
1663     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1664   int overflow = 0;
1665
1666   int1l = TREE_INT_CST_LOW (arg1);
1667   int1h = TREE_INT_CST_HIGH (arg1);
1668   int2l = TREE_INT_CST_LOW (arg2);
1669   int2h = TREE_INT_CST_HIGH (arg2);
1670
1671   switch (code)
1672     {
1673     case BIT_IOR_EXPR:
1674       low = int1l | int2l, hi = int1h | int2h;
1675       break;
1676
1677     case BIT_XOR_EXPR:
1678       low = int1l ^ int2l, hi = int1h ^ int2h;
1679       break;
1680
1681     case BIT_AND_EXPR:
1682       low = int1l & int2l, hi = int1h & int2h;
1683       break;
1684
1685     case RSHIFT_EXPR:
1686       int2l = -int2l;
1687     case LSHIFT_EXPR:
1688       /* It's unclear from the C standard whether shifts can overflow.
1689          The following code ignores overflow; perhaps a C standard
1690          interpretation ruling is needed.  */
1691       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1692                      &low, &hi, !uns);
1693       break;
1694
1695     case RROTATE_EXPR:
1696       int2l = - int2l;
1697     case LROTATE_EXPR:
1698       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1699                       &low, &hi);
1700       break;
1701
1702     case PLUS_EXPR:
1703       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1704       break;
1705
1706     case MINUS_EXPR:
1707       neg_double (int2l, int2h, &low, &hi);
1708       add_double (int1l, int1h, low, hi, &low, &hi);
1709       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1710       break;
1711
1712     case MULT_EXPR:
1713       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1714       break;
1715
1716     case TRUNC_DIV_EXPR:
1717     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1718     case EXACT_DIV_EXPR:
1719       /* This is a shortcut for a common special case.  */
1720       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1721           && !TREE_OVERFLOW (arg1)
1722           && !TREE_OVERFLOW (arg2)
1723           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1724         {
1725           if (code == CEIL_DIV_EXPR)
1726             int1l += int2l - 1;
1727
1728           low = int1l / int2l, hi = 0;
1729           break;
1730         }
1731
1732       /* ... fall through ...  */
1733
1734     case ROUND_DIV_EXPR:
1735       if (int2h == 0 && int2l == 0)
1736         return NULL_TREE;
1737       if (int2h == 0 && int2l == 1)
1738         {
1739           low = int1l, hi = int1h;
1740           break;
1741         }
1742       if (int1l == int2l && int1h == int2h
1743           && ! (int1l == 0 && int1h == 0))
1744         {
1745           low = 1, hi = 0;
1746           break;
1747         }
1748       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1749                                        &low, &hi, &garbagel, &garbageh);
1750       break;
1751
1752     case TRUNC_MOD_EXPR:
1753     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1754       /* This is a shortcut for a common special case.  */
1755       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1756           && !TREE_OVERFLOW (arg1)
1757           && !TREE_OVERFLOW (arg2)
1758           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1759         {
1760           if (code == CEIL_MOD_EXPR)
1761             int1l += int2l - 1;
1762           low = int1l % int2l, hi = 0;
1763           break;
1764         }
1765
1766       /* ... fall through ...  */
1767
1768     case ROUND_MOD_EXPR:
1769       if (int2h == 0 && int2l == 0)
1770         return NULL_TREE;
1771       overflow = div_and_round_double (code, uns,
1772                                        int1l, int1h, int2l, int2h,
1773                                        &garbagel, &garbageh, &low, &hi);
1774       break;
1775
1776     case MIN_EXPR:
1777     case MAX_EXPR:
1778       if (uns)
1779         low = (((unsigned HOST_WIDE_INT) int1h
1780                 < (unsigned HOST_WIDE_INT) int2h)
1781                || (((unsigned HOST_WIDE_INT) int1h
1782                     == (unsigned HOST_WIDE_INT) int2h)
1783                    && int1l < int2l));
1784       else
1785         low = (int1h < int2h
1786                || (int1h == int2h && int1l < int2l));
1787
1788       if (low == (code == MIN_EXPR))
1789         low = int1l, hi = int1h;
1790       else
1791         low = int2l, hi = int2h;
1792       break;
1793
1794     default:
1795       return NULL_TREE;
1796     }
1797
1798   if (notrunc)
1799     {
1800       t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1801
1802       /* Propagate overflow flags ourselves.  */
1803       if (((!uns || is_sizetype) && overflow)
1804           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1805         {
1806           t = copy_node (t);
1807           TREE_OVERFLOW (t) = 1;
1808         }
1809     }
1810   else
1811     t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1812                                ((!uns || is_sizetype) && overflow)
1813                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1814
1815   return t;
1816 }
1817
1818 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1819    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1820    are the same kind of constant and the same machine mode.  Return zero if
1821    combining the constants is not allowed in the current operating mode.
1822
1823    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1824
1825 static tree
1826 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1827 {
1828   /* Sanity check for the recursive cases.  */
1829   if (!arg1 || !arg2)
1830     return NULL_TREE;
1831
1832   STRIP_NOPS (arg1);
1833   STRIP_NOPS (arg2);
1834
1835   if (TREE_CODE (arg1) == INTEGER_CST)
1836     return int_const_binop (code, arg1, arg2, notrunc);
1837
1838   if (TREE_CODE (arg1) == REAL_CST)
1839     {
1840       enum machine_mode mode;
1841       REAL_VALUE_TYPE d1;
1842       REAL_VALUE_TYPE d2;
1843       REAL_VALUE_TYPE value;
1844       REAL_VALUE_TYPE result;
1845       bool inexact;
1846       tree t, type;
1847
1848       /* The following codes are handled by real_arithmetic.  */
1849       switch (code)
1850         {
1851         case PLUS_EXPR:
1852         case MINUS_EXPR:
1853         case MULT_EXPR:
1854         case RDIV_EXPR:
1855         case MIN_EXPR:
1856         case MAX_EXPR:
1857           break;
1858
1859         default:
1860           return NULL_TREE;
1861         }
1862
1863       d1 = TREE_REAL_CST (arg1);
1864       d2 = TREE_REAL_CST (arg2);
1865
1866       type = TREE_TYPE (arg1);
1867       mode = TYPE_MODE (type);
1868
1869       /* Don't perform operation if we honor signaling NaNs and
1870          either operand is a NaN.  */
1871       if (HONOR_SNANS (mode)
1872           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1873         return NULL_TREE;
1874
1875       /* Don't perform operation if it would raise a division
1876          by zero exception.  */
1877       if (code == RDIV_EXPR
1878           && REAL_VALUES_EQUAL (d2, dconst0)
1879           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1880         return NULL_TREE;
1881
1882       /* If either operand is a NaN, just return it.  Otherwise, set up
1883          for floating-point trap; we return an overflow.  */
1884       if (REAL_VALUE_ISNAN (d1))
1885         return arg1;
1886       else if (REAL_VALUE_ISNAN (d2))
1887         return arg2;
1888
1889       inexact = real_arithmetic (&value, code, &d1, &d2);
1890       real_convert (&result, mode, &value);
1891
1892       /* Don't constant fold this floating point operation if
1893          the result has overflowed and flag_trapping_math.  */
1894       if (flag_trapping_math
1895           && MODE_HAS_INFINITIES (mode)
1896           && REAL_VALUE_ISINF (result)
1897           && !REAL_VALUE_ISINF (d1)
1898           && !REAL_VALUE_ISINF (d2))
1899         return NULL_TREE;
1900
1901       /* Don't constant fold this floating point operation if the
1902          result may dependent upon the run-time rounding mode and
1903          flag_rounding_math is set, or if GCC's software emulation
1904          is unable to accurately represent the result.  */
1905       if ((flag_rounding_math
1906            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1907           && (inexact || !real_identical (&result, &value)))
1908         return NULL_TREE;
1909
1910       t = build_real (type, result);
1911
1912       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1913       return t;
1914     }
1915
1916   if (TREE_CODE (arg1) == FIXED_CST)
1917     {
1918       FIXED_VALUE_TYPE f1;
1919       FIXED_VALUE_TYPE f2;
1920       FIXED_VALUE_TYPE result;
1921       tree t, type;
1922       int sat_p;
1923       bool overflow_p;
1924
1925       /* The following codes are handled by fixed_arithmetic.  */
1926       switch (code)
1927         {
1928         case PLUS_EXPR:
1929         case MINUS_EXPR:
1930         case MULT_EXPR:
1931         case TRUNC_DIV_EXPR:
1932           f2 = TREE_FIXED_CST (arg2);
1933           break;
1934
1935         case LSHIFT_EXPR:
1936         case RSHIFT_EXPR:
1937           f2.data.high = TREE_INT_CST_HIGH (arg2);
1938           f2.data.low = TREE_INT_CST_LOW (arg2);
1939           f2.mode = SImode;
1940           break;
1941
1942         default:
1943           return NULL_TREE;
1944         }
1945
1946       f1 = TREE_FIXED_CST (arg1);
1947       type = TREE_TYPE (arg1);
1948       sat_p = TYPE_SATURATING (type);
1949       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1950       t = build_fixed (type, result);
1951       /* Propagate overflow flags.  */
1952       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1953         TREE_OVERFLOW (t) = 1;
1954       return t;
1955     }
1956
1957   if (TREE_CODE (arg1) == COMPLEX_CST)
1958     {
1959       tree type = TREE_TYPE (arg1);
1960       tree r1 = TREE_REALPART (arg1);
1961       tree i1 = TREE_IMAGPART (arg1);
1962       tree r2 = TREE_REALPART (arg2);
1963       tree i2 = TREE_IMAGPART (arg2);
1964       tree real, imag;
1965
1966       switch (code)
1967         {
1968         case PLUS_EXPR:
1969         case MINUS_EXPR:
1970           real = const_binop (code, r1, r2, notrunc);
1971           imag = const_binop (code, i1, i2, notrunc);
1972           break;
1973
1974         case MULT_EXPR:
1975 #ifdef HAVE_mpc
1976           if (COMPLEX_FLOAT_TYPE_P (type))
1977             return do_mpc_arg2 (arg1, arg2, type,
1978                                 /* do_nonfinite= */ folding_initializer,
1979                                 mpc_mul);
1980 #endif
1981
1982           real = const_binop (MINUS_EXPR,
1983                               const_binop (MULT_EXPR, r1, r2, notrunc),
1984                               const_binop (MULT_EXPR, i1, i2, notrunc),
1985                               notrunc);
1986           imag = const_binop (PLUS_EXPR,
1987                               const_binop (MULT_EXPR, r1, i2, notrunc),
1988                               const_binop (MULT_EXPR, i1, r2, notrunc),
1989                               notrunc);
1990           break;
1991
1992         case RDIV_EXPR:
1993 #ifdef HAVE_mpc
1994           if (COMPLEX_FLOAT_TYPE_P (type))
1995             return do_mpc_arg2 (arg1, arg2, type,
1996                                 /* do_nonfinite= */ folding_initializer,
1997                                 mpc_div);
1998           /* Fallthru ... */
1999 #endif
2000
2001         case TRUNC_DIV_EXPR:
2002         case CEIL_DIV_EXPR:
2003         case FLOOR_DIV_EXPR:
2004         case ROUND_DIV_EXPR:
2005           if (flag_complex_method == 0)
2006           {
2007             /* Keep this algorithm in sync with
2008                tree-complex.c:expand_complex_div_straight().
2009
2010                Expand complex division to scalars, straightforward algorithm.
2011                a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
2012                t = br*br + bi*bi
2013             */
2014             tree magsquared
2015               = const_binop (PLUS_EXPR,
2016                              const_binop (MULT_EXPR, r2, r2, notrunc),
2017                              const_binop (MULT_EXPR, i2, i2, notrunc),
2018                              notrunc);
2019             tree t1
2020               = const_binop (PLUS_EXPR,
2021                              const_binop (MULT_EXPR, r1, r2, notrunc),
2022                              const_binop (MULT_EXPR, i1, i2, notrunc),
2023                              notrunc);
2024             tree t2
2025               = const_binop (MINUS_EXPR,
2026                              const_binop (MULT_EXPR, i1, r2, notrunc),
2027                              const_binop (MULT_EXPR, r1, i2, notrunc),
2028                              notrunc);
2029
2030             real = const_binop (code, t1, magsquared, notrunc);
2031             imag = const_binop (code, t2, magsquared, notrunc);
2032           }
2033           else
2034           {
2035             /* Keep this algorithm in sync with
2036                tree-complex.c:expand_complex_div_wide().
2037
2038                Expand complex division to scalars, modified algorithm to minimize
2039                overflow with wide input ranges.  */
2040             tree inner_type = TREE_TYPE (type);
2041             tree absr2 = fold_build1 (ABS_EXPR, inner_type, r2);
2042             tree absi2 = fold_build1 (ABS_EXPR, inner_type, i2);
2043             tree compare = fold_build2 (LT_EXPR, boolean_type_node, absr2, absi2);
2044             if (integer_nonzerop (compare))
2045               {
2046                 /* In the TRUE branch, we compute
2047                    ratio = br/bi;
2048                    div = (br * ratio) + bi;
2049                    tr = (ar * ratio) + ai;
2050                    ti = (ai * ratio) - ar;
2051                    tr = tr / div;
2052                    ti = ti / div;  */
2053                 tree ratio = fold_build2 (code, inner_type, r2, i2);
2054                 tree div = fold_build2 (PLUS_EXPR, inner_type, i2,
2055                                         fold_build2 (MULT_EXPR, inner_type,
2056                                                      r2, ratio));
2057                 real = fold_build2 (MULT_EXPR, inner_type, r1, ratio);
2058                 real = fold_build2 (PLUS_EXPR, inner_type, real, i1);
2059                 real = fold_build2 (code, inner_type, real, div);
2060
2061                 imag = fold_build2 (MULT_EXPR, inner_type, i1, ratio);
2062                 imag = fold_build2 (MINUS_EXPR, inner_type, imag, r1);
2063                 imag = fold_build2 (code, inner_type, imag, div);
2064               }
2065             else
2066               {
2067                 /* In the FALSE branch, we compute
2068                    ratio = d/c;
2069                    divisor = (d * ratio) + c;
2070                    tr = (b * ratio) + a;
2071                    ti = b - (a * ratio);
2072                    tr = tr / div;
2073                    ti = ti / div;  */
2074                 tree ratio = fold_build2 (code, inner_type, i2, r2);
2075                 tree div = fold_build2 (PLUS_EXPR, inner_type, r2,
2076                                         fold_build2 (MULT_EXPR, inner_type,
2077                                                      i2, ratio));
2078
2079                 real = fold_build2 (MULT_EXPR, inner_type, i1, ratio);
2080                 real = fold_build2 (PLUS_EXPR, inner_type, real, r1);
2081                 real = fold_build2 (code, inner_type, real, div);
2082
2083                 imag = fold_build2 (MULT_EXPR, inner_type, r1, ratio);
2084                 imag = fold_build2 (MINUS_EXPR, inner_type, i1, imag);
2085                 imag = fold_build2 (code, inner_type, imag, div);
2086               }
2087           }
2088           break;
2089
2090         default:
2091           return NULL_TREE;
2092         }
2093
2094       if (real && imag)
2095         return build_complex (type, real, imag);
2096     }
2097
2098   if (TREE_CODE (arg1) == VECTOR_CST)
2099     {
2100       tree type = TREE_TYPE(arg1);
2101       int count = TYPE_VECTOR_SUBPARTS (type), i;
2102       tree elements1, elements2, list = NULL_TREE;
2103       
2104       if(TREE_CODE(arg2) != VECTOR_CST)
2105         return NULL_TREE;
2106         
2107       elements1 = TREE_VECTOR_CST_ELTS (arg1);
2108       elements2 = TREE_VECTOR_CST_ELTS (arg2);
2109
2110       for (i = 0; i < count; i++)
2111         {
2112           tree elem1, elem2, elem;
2113           
2114           /* The trailing elements can be empty and should be treated as 0 */
2115           if(!elements1)
2116             elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2117           else
2118             {
2119               elem1 = TREE_VALUE(elements1);
2120               elements1 = TREE_CHAIN (elements1);
2121             }  
2122             
2123           if(!elements2)
2124             elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2125           else
2126             {
2127               elem2 = TREE_VALUE(elements2);
2128               elements2 = TREE_CHAIN (elements2);
2129             }
2130               
2131           elem = const_binop (code, elem1, elem2, notrunc);
2132           
2133           /* It is possible that const_binop cannot handle the given
2134             code and return NULL_TREE */
2135           if(elem == NULL_TREE)
2136             return NULL_TREE;
2137           
2138           list = tree_cons (NULL_TREE, elem, list);
2139         }
2140       return build_vector(type, nreverse(list));  
2141     }
2142   return NULL_TREE;
2143 }
2144
2145 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
2146    indicates which particular sizetype to create.  */
2147
2148 tree
2149 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2150 {
2151   return build_int_cst (sizetype_tab[(int) kind], number);
2152 }
2153 \f
2154 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
2155    is a tree code.  The type of the result is taken from the operands.
2156    Both must be equivalent integer types, ala int_binop_types_match_p.
2157    If the operands are constant, so is the result.  */
2158
2159 tree
2160 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2161 {
2162   tree type = TREE_TYPE (arg0);
2163
2164   if (arg0 == error_mark_node || arg1 == error_mark_node)
2165     return error_mark_node;
2166
2167   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2168                                        TREE_TYPE (arg1)));
2169
2170   /* Handle the special case of two integer constants faster.  */
2171   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2172     {
2173       /* And some specific cases even faster than that.  */
2174       if (code == PLUS_EXPR)
2175         {
2176           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2177             return arg1;
2178           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2179             return arg0;
2180         }
2181       else if (code == MINUS_EXPR)
2182         {
2183           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2184             return arg0;
2185         }
2186       else if (code == MULT_EXPR)
2187         {
2188           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2189             return arg1;
2190         }
2191
2192       /* Handle general case of two integer constants.  */
2193       return int_const_binop (code, arg0, arg1, 0);
2194     }
2195
2196   return fold_build2_loc (loc, code, type, arg0, arg1);
2197 }
2198
2199 /* Given two values, either both of sizetype or both of bitsizetype,
2200    compute the difference between the two values.  Return the value
2201    in signed type corresponding to the type of the operands.  */
2202
2203 tree
2204 size_diffop_loc (location_t loc, tree arg0, tree arg1)
2205 {
2206   tree type = TREE_TYPE (arg0);
2207   tree ctype;
2208
2209   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2210                                        TREE_TYPE (arg1)));
2211
2212   /* If the type is already signed, just do the simple thing.  */
2213   if (!TYPE_UNSIGNED (type))
2214     return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2215
2216   if (type == sizetype)
2217     ctype = ssizetype;
2218   else if (type == bitsizetype)
2219     ctype = sbitsizetype;
2220   else
2221     ctype = signed_type_for (type);
2222
2223   /* If either operand is not a constant, do the conversions to the signed
2224      type and subtract.  The hardware will do the right thing with any
2225      overflow in the subtraction.  */
2226   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2227     return size_binop_loc (loc, MINUS_EXPR,
2228                            fold_convert_loc (loc, ctype, arg0),
2229                            fold_convert_loc (loc, ctype, arg1));
2230
2231   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2232      Otherwise, subtract the other way, convert to CTYPE (we know that can't
2233      overflow) and negate (which can't either).  Special-case a result
2234      of zero while we're here.  */
2235   if (tree_int_cst_equal (arg0, arg1))
2236     return build_int_cst (ctype, 0);
2237   else if (tree_int_cst_lt (arg1, arg0))
2238     return fold_convert_loc (loc, ctype,
2239                              size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2240   else
2241     return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2242                            fold_convert_loc (loc, ctype,
2243                                              size_binop_loc (loc,
2244                                                              MINUS_EXPR,
2245                                                              arg1, arg0)));
2246 }
2247 \f
2248 /* A subroutine of fold_convert_const handling conversions of an
2249    INTEGER_CST to another integer type.  */
2250
2251 static tree
2252 fold_convert_const_int_from_int (tree type, const_tree arg1)
2253 {
2254   tree t;
2255
2256   /* Given an integer constant, make new constant with new type,
2257      appropriately sign-extended or truncated.  */
2258   t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2259                              TREE_INT_CST_HIGH (arg1),
2260                              /* Don't set the overflow when
2261                                 converting from a pointer,  */
2262                              !POINTER_TYPE_P (TREE_TYPE (arg1))
2263                              /* or to a sizetype with same signedness
2264                                 and the precision is unchanged.
2265                                 ???  sizetype is always sign-extended,
2266                                 but its signedness depends on the
2267                                 frontend.  Thus we see spurious overflows
2268                                 here if we do not check this.  */
2269                              && !((TYPE_PRECISION (TREE_TYPE (arg1))
2270                                    == TYPE_PRECISION (type))
2271                                   && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2272                                       == TYPE_UNSIGNED (type))
2273                                   && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2274                                        && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2275                                       || (TREE_CODE (type) == INTEGER_TYPE
2276                                           && TYPE_IS_SIZETYPE (type)))),
2277                              (TREE_INT_CST_HIGH (arg1) < 0
2278                               && (TYPE_UNSIGNED (type)
2279                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2280                              | TREE_OVERFLOW (arg1));
2281
2282   return t;
2283 }
2284
2285 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2286    to an integer type.  */
2287
2288 static tree
2289 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2290 {
2291   int overflow = 0;
2292   tree t;
2293
2294   /* The following code implements the floating point to integer
2295      conversion rules required by the Java Language Specification,
2296      that IEEE NaNs are mapped to zero and values that overflow
2297      the target precision saturate, i.e. values greater than
2298      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2299      are mapped to INT_MIN.  These semantics are allowed by the
2300      C and C++ standards that simply state that the behavior of
2301      FP-to-integer conversion is unspecified upon overflow.  */
2302
2303   HOST_WIDE_INT high, low;
2304   REAL_VALUE_TYPE r;
2305   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2306
2307   switch (code)
2308     {
2309     case FIX_TRUNC_EXPR:
2310       real_trunc (&r, VOIDmode, &x);
2311       break;
2312
2313     default:
2314       gcc_unreachable ();
2315     }
2316
2317   /* If R is NaN, return zero and show we have an overflow.  */
2318   if (REAL_VALUE_ISNAN (r))
2319     {
2320       overflow = 1;
2321       high = 0;
2322       low = 0;
2323     }
2324
2325   /* See if R is less than the lower bound or greater than the
2326      upper bound.  */
2327
2328   if (! overflow)
2329     {
2330       tree lt = TYPE_MIN_VALUE (type);
2331       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2332       if (REAL_VALUES_LESS (r, l))
2333         {
2334           overflow = 1;
2335           high = TREE_INT_CST_HIGH (lt);
2336           low = TREE_INT_CST_LOW (lt);
2337         }
2338     }
2339
2340   if (! overflow)
2341     {
2342       tree ut = TYPE_MAX_VALUE (type);
2343       if (ut)
2344         {
2345           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2346           if (REAL_VALUES_LESS (u, r))
2347             {
2348               overflow = 1;
2349               high = TREE_INT_CST_HIGH (ut);
2350               low = TREE_INT_CST_LOW (ut);
2351             }
2352         }
2353     }
2354
2355   if (! overflow)
2356     REAL_VALUE_TO_INT (&low, &high, r);
2357
2358   t = force_fit_type_double (type, low, high, -1,
2359                              overflow | TREE_OVERFLOW (arg1));
2360   return t;
2361 }
2362
2363 /* A subroutine of fold_convert_const handling conversions of a
2364    FIXED_CST to an integer type.  */
2365
2366 static tree
2367 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2368 {
2369   tree t;
2370   double_int temp, temp_trunc;
2371   unsigned int mode;
2372
2373   /* Right shift FIXED_CST to temp by fbit.  */
2374   temp = TREE_FIXED_CST (arg1).data;
2375   mode = TREE_FIXED_CST (arg1).mode;
2376   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2377     {
2378       lshift_double (temp.low, temp.high,
2379                      - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2380                      &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2381
2382       /* Left shift temp to temp_trunc by fbit.  */
2383       lshift_double (temp.low, temp.high,
2384                      GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2385                      &temp_trunc.low, &temp_trunc.high,
2386                      SIGNED_FIXED_POINT_MODE_P (mode));
2387     }
2388   else
2389     {
2390       temp.low = 0;
2391       temp.high = 0;
2392       temp_trunc.low = 0;
2393       temp_trunc.high = 0;
2394     }
2395
2396   /* If FIXED_CST is negative, we need to round the value toward 0.
2397      By checking if the fractional bits are not zero to add 1 to temp.  */
2398   if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2399       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2400     {
2401       double_int one;
2402       one.low = 1;
2403       one.high = 0;
2404       temp = double_int_add (temp, one);
2405     }
2406
2407   /* Given a fixed-point constant, make new constant with new type,
2408      appropriately sign-extended or truncated.  */
2409   t = force_fit_type_double (type, temp.low, temp.high, -1,
2410                              (temp.high < 0
2411                               && (TYPE_UNSIGNED (type)
2412                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2413                              | TREE_OVERFLOW (arg1));
2414
2415   return t;
2416 }
2417
2418 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2419    to another floating point type.  */
2420
2421 static tree
2422 fold_convert_const_real_from_real (tree type, const_tree arg1)
2423 {
2424   REAL_VALUE_TYPE value;
2425   tree t;
2426
2427   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2428   t = build_real (type, value);
2429
2430   /* If converting an infinity or NAN to a representation that doesn't
2431      have one, set the overflow bit so that we can produce some kind of
2432      error message at the appropriate point if necessary.  It's not the
2433      most user-friendly message, but it's better than nothing.  */
2434   if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2435       && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2436     TREE_OVERFLOW (t) = 1;
2437   else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2438            && !MODE_HAS_NANS (TYPE_MODE (type)))
2439     TREE_OVERFLOW (t) = 1;
2440   /* Regular overflow, conversion produced an infinity in a mode that
2441      can't represent them.  */
2442   else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2443            && REAL_VALUE_ISINF (value)
2444            && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2445     TREE_OVERFLOW (t) = 1;
2446   else
2447     TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2448   return t;
2449 }
2450
2451 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2452    to a floating point type.  */
2453
2454 static tree
2455 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2456 {
2457   REAL_VALUE_TYPE value;
2458   tree t;
2459
2460   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2461   t = build_real (type, value);
2462
2463   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2464   return t;
2465 }
2466
2467 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2468    to another fixed-point type.  */
2469
2470 static tree
2471 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2472 {
2473   FIXED_VALUE_TYPE value;
2474   tree t;
2475   bool overflow_p;
2476
2477   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2478                               TYPE_SATURATING (type));
2479   t = build_fixed (type, value);
2480
2481   /* Propagate overflow flags.  */
2482   if (overflow_p | TREE_OVERFLOW (arg1))
2483     TREE_OVERFLOW (t) = 1;
2484   return t;
2485 }
2486
2487 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2488    to a fixed-point type.  */
2489
2490 static tree
2491 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2492 {
2493   FIXED_VALUE_TYPE value;
2494   tree t;
2495   bool overflow_p;
2496
2497   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2498                                        TREE_INT_CST (arg1),
2499                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
2500                                        TYPE_SATURATING (type));
2501   t = build_fixed (type, value);
2502
2503   /* Propagate overflow flags.  */
2504   if (overflow_p | TREE_OVERFLOW (arg1))
2505     TREE_OVERFLOW (t) = 1;
2506   return t;
2507 }
2508
2509 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2510    to a fixed-point type.  */
2511
2512 static tree
2513 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2514 {
2515   FIXED_VALUE_TYPE value;
2516   tree t;
2517   bool overflow_p;
2518
2519   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2520                                         &TREE_REAL_CST (arg1),
2521                                         TYPE_SATURATING (type));
2522   t = build_fixed (type, value);
2523
2524   /* Propagate overflow flags.  */
2525   if (overflow_p | TREE_OVERFLOW (arg1))
2526     TREE_OVERFLOW (t) = 1;
2527   return t;
2528 }
2529
2530 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2531    type TYPE.  If no simplification can be done return NULL_TREE.  */
2532
2533 static tree
2534 fold_convert_const (enum tree_code code, tree type, tree arg1)
2535 {
2536   if (TREE_TYPE (arg1) == type)
2537     return arg1;
2538
2539   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2540       || TREE_CODE (type) == OFFSET_TYPE)
2541     {
2542       if (TREE_CODE (arg1) == INTEGER_CST)
2543         return fold_convert_const_int_from_int (type, arg1);
2544       else if (TREE_CODE (arg1) == REAL_CST)
2545         return fold_convert_const_int_from_real (code, type, arg1);
2546       else if (TREE_CODE (arg1) == FIXED_CST)
2547         return fold_convert_const_int_from_fixed (type, arg1);
2548     }
2549   else if (TREE_CODE (type) == REAL_TYPE)
2550     {
2551       if (TREE_CODE (arg1) == INTEGER_CST)
2552         return build_real_from_int_cst (type, arg1);
2553       else if (TREE_CODE (arg1) == REAL_CST)
2554         return fold_convert_const_real_from_real (type, arg1);
2555       else if (TREE_CODE (arg1) == FIXED_CST)
2556         return fold_convert_const_real_from_fixed (type, arg1);
2557     }
2558   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2559     {
2560       if (TREE_CODE (arg1) == FIXED_CST)
2561         return fold_convert_const_fixed_from_fixed (type, arg1);
2562       else if (TREE_CODE (arg1) == INTEGER_CST)
2563         return fold_convert_const_fixed_from_int (type, arg1);
2564       else if (TREE_CODE (arg1) == REAL_CST)
2565         return fold_convert_const_fixed_from_real (type, arg1);
2566     }
2567   return NULL_TREE;
2568 }
2569
2570 /* Construct a vector of zero elements of vector type TYPE.  */
2571
2572 static tree
2573 build_zero_vector (tree type)
2574 {
2575   tree elem, list;
2576   int i, units;
2577
2578   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2579   units = TYPE_VECTOR_SUBPARTS (type);
2580   
2581   list = NULL_TREE;
2582   for (i = 0; i < units; i++)
2583     list = tree_cons (NULL_TREE, elem, list);
2584   return build_vector (type, list);
2585 }
2586
2587 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
2588
2589 bool
2590 fold_convertible_p (const_tree type, const_tree arg)
2591 {
2592   tree orig = TREE_TYPE (arg);
2593
2594   if (type == orig)
2595     return true;
2596
2597   if (TREE_CODE (arg) == ERROR_MARK
2598       || TREE_CODE (type) == ERROR_MARK
2599       || TREE_CODE (orig) == ERROR_MARK)
2600     return false;
2601
2602   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2603     return true;
2604
2605   switch (TREE_CODE (type))
2606     {
2607     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2608     case POINTER_TYPE: case REFERENCE_TYPE:
2609     case OFFSET_TYPE:
2610       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2611           || TREE_CODE (orig) == OFFSET_TYPE)
2612         return true;
2613       return (TREE_CODE (orig) == VECTOR_TYPE
2614               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2615
2616     case REAL_TYPE:
2617     case FIXED_POINT_TYPE:
2618     case COMPLEX_TYPE:
2619     case VECTOR_TYPE:
2620     case VOID_TYPE:
2621       return TREE_CODE (type) == TREE_CODE (orig);
2622
2623     default:
2624       return false;
2625     }
2626 }
2627
2628 /* Convert expression ARG to type TYPE.  Used by the middle-end for
2629    simple conversions in preference to calling the front-end's convert.  */
2630
2631 tree
2632 fold_convert_loc (location_t loc, tree type, tree arg)
2633 {
2634   tree orig = TREE_TYPE (arg);
2635   tree tem;
2636
2637   if (type == orig)
2638     return arg;
2639
2640   if (TREE_CODE (arg) == ERROR_MARK
2641       || TREE_CODE (type) == ERROR_MARK
2642       || TREE_CODE (orig) == ERROR_MARK)
2643     return error_mark_node;
2644
2645   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2646     return fold_build1_loc (loc, NOP_EXPR, type, arg);
2647
2648   switch (TREE_CODE (type))
2649     {
2650     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2651     case POINTER_TYPE: case REFERENCE_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   /* If both types don't have the same precision, then it is not safe
3183      to strip NOPs.  */
3184   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3185     return 0;
3186
3187   STRIP_NOPS (arg0);
3188   STRIP_NOPS (arg1);
3189
3190   /* In case both args are comparisons but with different comparison
3191      code, try to swap the comparison operands of one arg to produce
3192      a match and compare that variant.  */
3193   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3194       && COMPARISON_CLASS_P (arg0)
3195       && COMPARISON_CLASS_P (arg1))
3196     {
3197       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3198
3199       if (TREE_CODE (arg0) == swap_code)
3200         return operand_equal_p (TREE_OPERAND (arg0, 0),
3201                                 TREE_OPERAND (arg1, 1), flags)
3202                && operand_equal_p (TREE_OPERAND (arg0, 1),
3203                                    TREE_OPERAND (arg1, 0), flags);
3204     }
3205
3206   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3207       /* This is needed for conversions and for COMPONENT_REF.
3208          Might as well play it safe and always test this.  */
3209       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3210       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3211       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3212     return 0;
3213
3214   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3215      We don't care about side effects in that case because the SAVE_EXPR
3216      takes care of that for us. In all other cases, two expressions are
3217      equal if they have no side effects.  If we have two identical
3218      expressions with side effects that should be treated the same due
3219      to the only side effects being identical SAVE_EXPR's, that will
3220      be detected in the recursive calls below.  */
3221   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3222       && (TREE_CODE (arg0) == SAVE_EXPR
3223           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3224     return 1;
3225
3226   /* Next handle constant cases, those for which we can return 1 even
3227      if ONLY_CONST is set.  */
3228   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3229     switch (TREE_CODE (arg0))
3230       {
3231       case INTEGER_CST:
3232         return tree_int_cst_equal (arg0, arg1);
3233
3234       case FIXED_CST:
3235         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3236                                        TREE_FIXED_CST (arg1));
3237
3238       case REAL_CST:
3239         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3240                                    TREE_REAL_CST (arg1)))
3241           return 1;
3242
3243         
3244         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3245           {
3246             /* If we do not distinguish between signed and unsigned zero,
3247                consider them equal.  */
3248             if (real_zerop (arg0) && real_zerop (arg1))
3249               return 1;
3250           }
3251         return 0;
3252
3253       case VECTOR_CST:
3254         {
3255           tree v1, v2;
3256
3257           v1 = TREE_VECTOR_CST_ELTS (arg0);
3258           v2 = TREE_VECTOR_CST_ELTS (arg1);
3259           while (v1 && v2)
3260             {
3261               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3262                                     flags))
3263                 return 0;
3264               v1 = TREE_CHAIN (v1);
3265               v2 = TREE_CHAIN (v2);
3266             }
3267
3268           return v1 == v2;
3269         }
3270
3271       case COMPLEX_CST:
3272         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3273                                  flags)
3274                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3275                                     flags));
3276
3277       case STRING_CST:
3278         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3279                 && ! memcmp (TREE_STRING_POINTER (arg0),
3280                               TREE_STRING_POINTER (arg1),
3281                               TREE_STRING_LENGTH (arg0)));
3282
3283       case ADDR_EXPR:
3284         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3285                                 0);
3286       default:
3287         break;
3288       }
3289
3290   if (flags & OEP_ONLY_CONST)
3291     return 0;
3292
3293 /* Define macros to test an operand from arg0 and arg1 for equality and a
3294    variant that allows null and views null as being different from any
3295    non-null value.  In the latter case, if either is null, the both
3296    must be; otherwise, do the normal comparison.  */
3297 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
3298                                     TREE_OPERAND (arg1, N), flags)
3299
3300 #define OP_SAME_WITH_NULL(N)                            \
3301   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3302    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3303
3304   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3305     {
3306     case tcc_unary:
3307       /* Two conversions are equal only if signedness and modes match.  */
3308       switch (TREE_CODE (arg0))
3309         {
3310         CASE_CONVERT:
3311         case FIX_TRUNC_EXPR:
3312           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3313               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3314             return 0;
3315           break;
3316         default:
3317           break;
3318         }
3319
3320       return OP_SAME (0);
3321
3322
3323     case tcc_comparison:
3324     case tcc_binary:
3325       if (OP_SAME (0) && OP_SAME (1))
3326         return 1;
3327
3328       /* For commutative ops, allow the other order.  */
3329       return (commutative_tree_code (TREE_CODE (arg0))
3330               && operand_equal_p (TREE_OPERAND (arg0, 0),
3331                                   TREE_OPERAND (arg1, 1), flags)
3332               && operand_equal_p (TREE_OPERAND (arg0, 1),
3333                                   TREE_OPERAND (arg1, 0), flags));
3334
3335     case tcc_reference:
3336       /* If either of the pointer (or reference) expressions we are
3337          dereferencing contain a side effect, these cannot be equal.  */
3338       if (TREE_SIDE_EFFECTS (arg0)
3339           || TREE_SIDE_EFFECTS (arg1))
3340         return 0;
3341
3342       switch (TREE_CODE (arg0))
3343         {
3344         case INDIRECT_REF:
3345         case ALIGN_INDIRECT_REF:
3346         case MISALIGNED_INDIRECT_REF:
3347         case REALPART_EXPR:
3348         case IMAGPART_EXPR:
3349           return OP_SAME (0);
3350
3351         case ARRAY_REF:
3352         case ARRAY_RANGE_REF:
3353           /* Operands 2 and 3 may be null.
3354              Compare the array index by value if it is constant first as we
3355              may have different types but same value here.  */
3356           return (OP_SAME (0)
3357                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3358                                           TREE_OPERAND (arg1, 1))
3359                       || OP_SAME (1))
3360                   && OP_SAME_WITH_NULL (2)
3361                   && OP_SAME_WITH_NULL (3));
3362
3363         case COMPONENT_REF:
3364           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
3365              may be NULL when we're called to compare MEM_EXPRs.  */
3366           return OP_SAME_WITH_NULL (0)
3367                  && OP_SAME (1)
3368                  && OP_SAME_WITH_NULL (2);
3369
3370         case BIT_FIELD_REF:
3371           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3372
3373         default:
3374           return 0;
3375         }
3376
3377     case tcc_expression:
3378       switch (TREE_CODE (arg0))
3379         {
3380         case ADDR_EXPR:
3381         case TRUTH_NOT_EXPR:
3382           return OP_SAME (0);
3383
3384         case TRUTH_ANDIF_EXPR:
3385         case TRUTH_ORIF_EXPR:
3386           return OP_SAME (0) && OP_SAME (1);
3387
3388         case TRUTH_AND_EXPR:
3389         case TRUTH_OR_EXPR:
3390         case TRUTH_XOR_EXPR:
3391           if (OP_SAME (0) && OP_SAME (1))
3392             return 1;
3393
3394           /* Otherwise take into account this is a commutative operation.  */
3395           return (operand_equal_p (TREE_OPERAND (arg0, 0),
3396                                    TREE_OPERAND (arg1, 1), flags)
3397                   && operand_equal_p (TREE_OPERAND (arg0, 1),
3398                                       TREE_OPERAND (arg1, 0), flags));
3399
3400         case COND_EXPR:
3401           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3402           
3403         default:
3404           return 0;
3405         }
3406
3407     case tcc_vl_exp:
3408       switch (TREE_CODE (arg0))
3409         {
3410         case CALL_EXPR:
3411           /* If the CALL_EXPRs call different functions, then they
3412              clearly can not be equal.  */
3413           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3414                                  flags))
3415             return 0;
3416
3417           {
3418             unsigned int cef = call_expr_flags (arg0);
3419             if (flags & OEP_PURE_SAME)
3420               cef &= ECF_CONST | ECF_PURE;
3421             else
3422               cef &= ECF_CONST;
3423             if (!cef)
3424               return 0;
3425           }
3426
3427           /* Now see if all the arguments are the same.  */
3428           {
3429             const_call_expr_arg_iterator iter0, iter1;
3430             const_tree a0, a1;
3431             for (a0 = first_const_call_expr_arg (arg0, &iter0),
3432                    a1 = first_const_call_expr_arg (arg1, &iter1);
3433                  a0 && a1;
3434                  a0 = next_const_call_expr_arg (&iter0),
3435                    a1 = next_const_call_expr_arg (&iter1))
3436               if (! operand_equal_p (a0, a1, flags))
3437                 return 0;
3438
3439             /* If we get here and both argument lists are exhausted
3440                then the CALL_EXPRs are equal.  */
3441             return ! (a0 || a1);
3442           }
3443         default:
3444           return 0;
3445         }
3446
3447     case tcc_declaration:
3448       /* Consider __builtin_sqrt equal to sqrt.  */
3449       return (TREE_CODE (arg0) == FUNCTION_DECL
3450               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3451               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3452               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3453
3454     default:
3455       return 0;
3456     }
3457
3458 #undef OP_SAME
3459 #undef OP_SAME_WITH_NULL
3460 }
3461 \f
3462 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3463    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3464
3465    When in doubt, return 0.  */
3466
3467 static int
3468 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3469 {
3470   int unsignedp1, unsignedpo;
3471   tree primarg0, primarg1, primother;
3472   unsigned int correct_width;
3473
3474   if (operand_equal_p (arg0, arg1, 0))
3475     return 1;
3476
3477   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3478       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3479     return 0;
3480
3481   /* Discard any conversions that don't change the modes of ARG0 and ARG1
3482      and see if the inner values are the same.  This removes any
3483      signedness comparison, which doesn't matter here.  */
3484   primarg0 = arg0, primarg1 = arg1;
3485   STRIP_NOPS (primarg0);
3486   STRIP_NOPS (primarg1);
3487   if (operand_equal_p (primarg0, primarg1, 0))
3488     return 1;
3489
3490   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3491      actual comparison operand, ARG0.
3492
3493      First throw away any conversions to wider types
3494      already present in the operands.  */
3495
3496   primarg1 = get_narrower (arg1, &unsignedp1);
3497   primother = get_narrower (other, &unsignedpo);
3498
3499   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3500   if (unsignedp1 == unsignedpo
3501       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3502       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3503     {
3504       tree type = TREE_TYPE (arg0);
3505
3506       /* Make sure shorter operand is extended the right way
3507          to match the longer operand.  */
3508       primarg1 = fold_convert (signed_or_unsigned_type_for
3509                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3510
3511       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3512         return 1;
3513     }
3514
3515   return 0;
3516 }
3517 \f
3518 /* See if ARG is an expression that is either a comparison or is performing
3519    arithmetic on comparisons.  The comparisons must only be comparing
3520    two different values, which will be stored in *CVAL1 and *CVAL2; if
3521    they are nonzero it means that some operands have already been found.
3522    No variables may be used anywhere else in the expression except in the
3523    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
3524    the expression and save_expr needs to be called with CVAL1 and CVAL2.
3525
3526    If this is true, return 1.  Otherwise, return zero.  */
3527
3528 static int
3529 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3530 {
3531   enum tree_code code = TREE_CODE (arg);
3532   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3533
3534   /* We can handle some of the tcc_expression cases here.  */
3535   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3536     tclass = tcc_unary;
3537   else if (tclass == tcc_expression
3538            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3539                || code == COMPOUND_EXPR))
3540     tclass = tcc_binary;
3541
3542   else if (tclass == tcc_expression && code == SAVE_EXPR
3543            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3544     {
3545       /* If we've already found a CVAL1 or CVAL2, this expression is
3546          two complex to handle.  */
3547       if (*cval1 || *cval2)
3548         return 0;
3549
3550       tclass = tcc_unary;
3551       *save_p = 1;
3552     }
3553
3554   switch (tclass)
3555     {
3556     case tcc_unary:
3557       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3558
3559     case tcc_binary:
3560       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3561               && twoval_comparison_p (TREE_OPERAND (arg, 1),
3562                                       cval1, cval2, save_p));
3563
3564     case tcc_constant:
3565       return 1;
3566
3567     case tcc_expression:
3568       if (code == COND_EXPR)
3569         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3570                                      cval1, cval2, save_p)
3571                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3572                                         cval1, cval2, save_p)
3573                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3574                                         cval1, cval2, save_p));
3575       return 0;
3576
3577     case tcc_comparison:
3578       /* First see if we can handle the first operand, then the second.  For
3579          the second operand, we know *CVAL1 can't be zero.  It must be that
3580          one side of the comparison is each of the values; test for the
3581          case where this isn't true by failing if the two operands
3582          are the same.  */
3583
3584       if (operand_equal_p (TREE_OPERAND (arg, 0),
3585                            TREE_OPERAND (arg, 1), 0))
3586         return 0;
3587
3588       if (*cval1 == 0)
3589         *cval1 = TREE_OPERAND (arg, 0);
3590       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3591         ;
3592       else if (*cval2 == 0)
3593         *cval2 = TREE_OPERAND (arg, 0);
3594       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3595         ;
3596       else
3597         return 0;
3598
3599       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3600         ;
3601       else if (*cval2 == 0)
3602         *cval2 = TREE_OPERAND (arg, 1);
3603       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3604         ;
3605       else
3606         return 0;
3607
3608       return 1;
3609
3610     default:
3611       return 0;
3612     }
3613 }
3614 \f
3615 /* ARG is a tree that is known to contain just arithmetic operations and
3616    comparisons.  Evaluate the operations in the tree substituting NEW0 for
3617    any occurrence of OLD0 as an operand of a comparison and likewise for
3618    NEW1 and OLD1.  */
3619
3620 static tree
3621 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3622             tree old1, tree new1)
3623 {
3624   tree type = TREE_TYPE (arg);
3625   enum tree_code code = TREE_CODE (arg);
3626   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3627
3628   /* We can handle some of the tcc_expression cases here.  */
3629   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3630     tclass = tcc_unary;
3631   else if (tclass == tcc_expression
3632            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3633     tclass = tcc_binary;
3634
3635   switch (tclass)
3636     {
3637     case tcc_unary:
3638       return fold_build1_loc (loc, code, type,
3639                           eval_subst (loc, TREE_OPERAND (arg, 0),
3640                                       old0, new0, old1, new1));
3641
3642     case tcc_binary:
3643       return fold_build2_loc (loc, code, type,
3644                           eval_subst (loc, TREE_OPERAND (arg, 0),
3645                                       old0, new0, old1, new1),
3646                           eval_subst (loc, TREE_OPERAND (arg, 1),
3647                                       old0, new0, old1, new1));
3648
3649     case tcc_expression:
3650       switch (code)
3651         {
3652         case SAVE_EXPR:
3653           return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3654                              old1, new1);
3655
3656         case COMPOUND_EXPR:
3657           return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3658                              old1, new1);
3659
3660         case COND_EXPR:
3661           return fold_build3_loc (loc, code, type,
3662                               eval_subst (loc, TREE_OPERAND (arg, 0),
3663                                           old0, new0, old1, new1),
3664                               eval_subst (loc, TREE_OPERAND (arg, 1),
3665                                           old0, new0, old1, new1),
3666                               eval_subst (loc, TREE_OPERAND (arg, 2),
3667                                           old0, new0, old1, new1));
3668         default:
3669           break;
3670         }
3671       /* Fall through - ???  */
3672
3673     case tcc_comparison:
3674       {
3675         tree arg0 = TREE_OPERAND (arg, 0);
3676         tree arg1 = TREE_OPERAND (arg, 1);
3677
3678         /* We need to check both for exact equality and tree equality.  The
3679            former will be true if the operand has a side-effect.  In that
3680            case, we know the operand occurred exactly once.  */
3681
3682         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3683           arg0 = new0;
3684         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3685           arg0 = new1;
3686
3687         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3688           arg1 = new0;
3689         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3690           arg1 = new1;
3691
3692         return fold_build2_loc (loc, code, type, arg0, arg1);
3693       }
3694
3695     default:
3696       return arg;
3697     }
3698 }
3699 \f
3700 /* Return a tree for the case when the result of an expression is RESULT
3701    converted to TYPE and OMITTED was previously an operand of the expression
3702    but is now not needed (e.g., we folded OMITTED * 0).
3703
3704    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
3705    the conversion of RESULT to TYPE.  */
3706
3707 tree
3708 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3709 {
3710   tree t = fold_convert_loc (loc, type, result);
3711
3712   /* If the resulting operand is an empty statement, just return the omitted
3713      statement casted to void. */
3714   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3715     {
3716       t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3717       goto omit_one_operand_exit;
3718     }
3719
3720   if (TREE_SIDE_EFFECTS (omitted))
3721     {
3722       t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3723       goto omit_one_operand_exit;
3724     }
3725
3726   return non_lvalue_loc (loc, t);
3727
3728  omit_one_operand_exit:
3729   protected_set_expr_location (t, loc);
3730   return t;
3731 }
3732
3733 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3734
3735 static tree
3736 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
3737                                tree omitted)
3738 {
3739   tree t = fold_convert_loc (loc, type, result);
3740
3741   /* If the resulting operand is an empty statement, just return the omitted
3742      statement casted to void. */
3743   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3744     {
3745       t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3746       goto pedantic_omit_one_operand_exit;
3747     }
3748
3749   if (TREE_SIDE_EFFECTS (omitted))
3750     {
3751       t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3752       goto pedantic_omit_one_operand_exit;
3753     }
3754
3755   return pedantic_non_lvalue_loc (loc, t);
3756
3757  pedantic_omit_one_operand_exit:
3758   protected_set_expr_location (t, loc);
3759   return t;
3760 }
3761
3762 /* Return a tree for the case when the result of an expression is RESULT
3763    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3764    of the expression but are now not needed.
3765
3766    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3767    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3768    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3769    just do the conversion of RESULT to TYPE.  */
3770
3771 tree
3772 omit_two_operands_loc (location_t loc, tree type, tree result,
3773                    tree omitted1, tree omitted2)
3774 {
3775   tree t = fold_convert_loc (loc, type, result);
3776
3777   if (TREE_SIDE_EFFECTS (omitted2))
3778     {
3779       t = build2 (COMPOUND_EXPR, type, omitted2, t);
3780       SET_EXPR_LOCATION (t, loc);
3781     }
3782   if (TREE_SIDE_EFFECTS (omitted1))
3783     {
3784       t = build2 (COMPOUND_EXPR, type, omitted1, t);
3785       SET_EXPR_LOCATION (t, loc);
3786     }
3787
3788   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3789 }
3790
3791 \f
3792 /* Return a simplified tree node for the truth-negation of ARG.  This
3793    never alters ARG itself.  We assume that ARG is an operation that
3794    returns a truth value (0 or 1).
3795
3796    FIXME: one would think we would fold the result, but it causes
3797    problems with the dominator optimizer.  */
3798
3799 tree
3800 fold_truth_not_expr (location_t loc, tree arg)
3801 {
3802   tree t, type = TREE_TYPE (arg);
3803   enum tree_code code = TREE_CODE (arg);
3804   location_t loc1, loc2;
3805
3806   /* If this is a comparison, we can simply invert it, except for
3807      floating-point non-equality comparisons, in which case we just
3808      enclose a TRUTH_NOT_EXPR around what we have.  */
3809
3810   if (TREE_CODE_CLASS (code) == tcc_comparison)
3811     {
3812       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3813       if (FLOAT_TYPE_P (op_type)
3814           && flag_trapping_math
3815           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3816           && code != NE_EXPR && code != EQ_EXPR)
3817         return NULL_TREE;
3818
3819       code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3820       if (code == ERROR_MARK)
3821         return NULL_TREE;
3822
3823       t = build2 (code, type, TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3824       SET_EXPR_LOCATION (t, loc);
3825       return t;
3826     }
3827
3828   switch (code)
3829     {
3830     case INTEGER_CST:
3831       return constant_boolean_node (integer_zerop (arg), type);
3832
3833     case TRUTH_AND_EXPR:
3834       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3835       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3836       if (loc1 == UNKNOWN_LOCATION)
3837         loc1 = loc;
3838       if (loc2 == UNKNOWN_LOCATION)
3839         loc2 = loc;
3840       t = build2 (TRUTH_OR_EXPR, type,
3841                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3842                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3843       break;
3844
3845     case TRUTH_OR_EXPR:
3846       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3847       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3848       if (loc1 == UNKNOWN_LOCATION)
3849         loc1 = loc;
3850       if (loc2 == UNKNOWN_LOCATION)
3851         loc2 = loc;
3852       t = build2 (TRUTH_AND_EXPR, type,
3853                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3854                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3855       break;
3856
3857     case TRUTH_XOR_EXPR:
3858       /* Here we can invert either operand.  We invert the first operand
3859          unless the second operand is a TRUTH_NOT_EXPR in which case our
3860          result is the XOR of the first operand with the inside of the
3861          negation of the second operand.  */
3862
3863       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3864         t = build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3865                     TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3866       else
3867         t = build2 (TRUTH_XOR_EXPR, type,
3868                     invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3869                     TREE_OPERAND (arg, 1));
3870       break;
3871
3872     case TRUTH_ANDIF_EXPR:
3873       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3874       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3875       if (loc1 == UNKNOWN_LOCATION)
3876         loc1 = loc;
3877       if (loc2 == UNKNOWN_LOCATION)
3878         loc2 = loc;
3879       t = build2 (TRUTH_ORIF_EXPR, type,
3880                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3881                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3882       break;
3883
3884     case TRUTH_ORIF_EXPR:
3885       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3886       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3887       if (loc1 == UNKNOWN_LOCATION)
3888         loc1 = loc;
3889       if (loc2 == UNKNOWN_LOCATION)
3890         loc2 = loc;
3891       t = build2 (TRUTH_ANDIF_EXPR, type,
3892                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3893                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3894       break;
3895
3896     case TRUTH_NOT_EXPR:
3897       return TREE_OPERAND (arg, 0);
3898
3899     case COND_EXPR:
3900       {
3901         tree arg1 = TREE_OPERAND (arg, 1);
3902         tree arg2 = TREE_OPERAND (arg, 2);
3903
3904         loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3905         loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 2));
3906         if (loc1 == UNKNOWN_LOCATION)
3907           loc1 = loc;
3908         if (loc2 == UNKNOWN_LOCATION)
3909           loc2 = loc;
3910
3911         /* A COND_EXPR may have a throw as one operand, which
3912            then has void type.  Just leave void operands
3913            as they are.  */
3914         t = build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3915                     VOID_TYPE_P (TREE_TYPE (arg1))
3916                     ? arg1 : invert_truthvalue_loc (loc1, arg1),
3917                     VOID_TYPE_P (TREE_TYPE (arg2))
3918                     ? arg2 : invert_truthvalue_loc (loc2, arg2));
3919         break;
3920       }
3921
3922     case COMPOUND_EXPR:
3923       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3924       if (loc1 == UNKNOWN_LOCATION)
3925         loc1 = loc;
3926       t = build2 (COMPOUND_EXPR, type,
3927                   TREE_OPERAND (arg, 0),
3928                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3929       break;
3930
3931     case NON_LVALUE_EXPR:
3932       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3933       if (loc1 == UNKNOWN_LOCATION)
3934         loc1 = loc;
3935       return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3936
3937     CASE_CONVERT:
3938       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3939         {
3940           t = build1 (TRUTH_NOT_EXPR, type, arg);
3941           break;
3942         }
3943
3944       /* ... fall through ...  */
3945
3946     case FLOAT_EXPR:
3947       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3948       if (loc1 == UNKNOWN_LOCATION)
3949         loc1 = loc;
3950       t = build1 (TREE_CODE (arg), type,
3951                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3952       break;
3953
3954     case BIT_AND_EXPR:
3955       if (!integer_onep (TREE_OPERAND (arg, 1)))
3956         return NULL_TREE;
3957       t = build2 (EQ_EXPR, type, arg, build_int_cst (type, 0));
3958       break;
3959
3960     case SAVE_EXPR:
3961       t = build1 (TRUTH_NOT_EXPR, type, arg);
3962       break;
3963
3964     case CLEANUP_POINT_EXPR:
3965       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3966       if (loc1 == UNKNOWN_LOCATION)
3967         loc1 = loc;
3968       t = build1 (CLEANUP_POINT_EXPR, type,
3969                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3970       break;
3971
3972     default:
3973       t = NULL_TREE;
3974       break;
3975     }
3976
3977   if (t)
3978     SET_EXPR_LOCATION (t, loc);
3979
3980   return t;
3981 }
3982
3983 /* Return a simplified tree node for the truth-negation of ARG.  This
3984    never alters ARG itself.  We assume that ARG is an operation that
3985    returns a truth value (0 or 1).
3986
3987    FIXME: one would think we would fold the result, but it causes
3988    problems with the dominator optimizer.  */
3989
3990 tree
3991 invert_truthvalue_loc (location_t loc, tree arg)
3992 {
3993   tree tem;
3994
3995   if (TREE_CODE (arg) == ERROR_MARK)
3996     return arg;
3997
3998   tem = fold_truth_not_expr (loc, arg);
3999   if (!tem)
4000     {
4001       tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
4002       SET_EXPR_LOCATION (tem, loc);
4003     }
4004
4005   return tem;
4006 }
4007
4008 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
4009    operands are another bit-wise operation with a common input.  If so,
4010    distribute the bit operations to save an operation and possibly two if
4011    constants are involved.  For example, convert
4012         (A | B) & (A | C) into A | (B & C)
4013    Further simplification will occur if B and C are constants.
4014
4015    If this optimization cannot be done, 0 will be returned.  */
4016
4017 static tree
4018 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
4019                      tree arg0, tree arg1)
4020 {
4021   tree common;
4022   tree left, right;
4023
4024   if (TREE_CODE (arg0) != TREE_CODE (arg1)
4025       || TREE_CODE (arg0) == code
4026       || (TREE_CODE (arg0) != BIT_AND_EXPR
4027           && TREE_CODE (arg0) != BIT_IOR_EXPR))
4028     return 0;
4029
4030   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
4031     {
4032       common = TREE_OPERAND (arg0, 0);
4033       left = TREE_OPERAND (arg0, 1);
4034       right = TREE_OPERAND (arg1, 1);
4035     }
4036   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
4037     {
4038       common = TREE_OPERAND (arg0, 0);
4039       left = TREE_OPERAND (arg0, 1);
4040       right = TREE_OPERAND (arg1, 0);
4041     }
4042   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
4043     {
4044       common = TREE_OPERAND (arg0, 1);
4045       left = TREE_OPERAND (arg0, 0);
4046       right = TREE_OPERAND (arg1, 1);
4047     }
4048   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
4049     {
4050       common = TREE_OPERAND (arg0, 1);
4051       left = TREE_OPERAND (arg0, 0);
4052       right = TREE_OPERAND (arg1, 0);
4053     }
4054   else
4055     return 0;
4056
4057   common = fold_convert_loc (loc, type, common);
4058   left = fold_convert_loc (loc, type, left);
4059   right = fold_convert_loc (loc, type, right);
4060   return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
4061                       fold_build2_loc (loc, code, type, left, right));
4062 }
4063
4064 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
4065    with code CODE.  This optimization is unsafe.  */
4066 static tree
4067 distribute_real_division (location_t loc, enum tree_code code, tree type,
4068                           tree arg0, tree arg1)
4069 {
4070   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
4071   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
4072
4073   /* (A / C) +- (B / C) -> (A +- B) / C.  */
4074   if (mul0 == mul1
4075       && operand_equal_p (TREE_OPERAND (arg0, 1),
4076                        TREE_OPERAND (arg1, 1), 0))
4077     return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
4078                         fold_build2_loc (loc, code, type,
4079                                      TREE_OPERAND (arg0, 0),
4080                                      TREE_OPERAND (arg1, 0)),
4081                         TREE_OPERAND (arg0, 1));
4082
4083   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
4084   if (operand_equal_p (TREE_OPERAND (arg0, 0),
4085                        TREE_OPERAND (arg1, 0), 0)
4086       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
4087       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
4088     {
4089       REAL_VALUE_TYPE r0, r1;
4090       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
4091       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
4092       if (!mul0)
4093         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
4094       if (!mul1)
4095         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
4096       real_arithmetic (&r0, code, &r0, &r1);
4097       return fold_build2_loc (loc, MULT_EXPR, type,
4098                           TREE_OPERAND (arg0, 0),
4099                           build_real (type, r0));
4100     }
4101
4102   return NULL_TREE;
4103 }
4104 \f
4105 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4106    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
4107
4108 static tree
4109 make_bit_field_ref (location_t loc, tree inner, tree type,
4110                     HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
4111 {
4112   tree result, bftype;
4113
4114   if (bitpos == 0)
4115     {
4116       tree size = TYPE_SIZE (TREE_TYPE (inner));
4117       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4118            || POINTER_TYPE_P (TREE_TYPE (inner)))
4119           && host_integerp (size, 0) 
4120           && tree_low_cst (size, 0) == bitsize)
4121         return fold_convert_loc (loc, type, inner);
4122     }
4123
4124   bftype = type;
4125   if (TYPE_PRECISION (bftype) != bitsize
4126       || TYPE_UNSIGNED (bftype) == !unsignedp)
4127     bftype = build_nonstandard_integer_type (bitsize, 0);
4128
4129   result = build3 (BIT_FIELD_REF, bftype, inner,
4130                    size_int (bitsize), bitsize_int (bitpos));
4131   SET_EXPR_LOCATION (result, loc);
4132
4133   if (bftype != type)
4134     result = fold_convert_loc (loc, type, result);
4135
4136   return result;
4137 }
4138
4139 /* Optimize a bit-field compare.
4140
4141    There are two cases:  First is a compare against a constant and the
4142    second is a comparison of two items where the fields are at the same
4143    bit position relative to the start of a chunk (byte, halfword, word)
4144    large enough to contain it.  In these cases we can avoid the shift
4145    implicit in bitfield extractions.
4146
4147    For constants, we emit a compare of the shifted constant with the
4148    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4149    compared.  For two fields at the same position, we do the ANDs with the
4150    similar mask and compare the result of the ANDs.
4151
4152    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4153    COMPARE_TYPE is the type of the comparison, and LHS and RHS
4154    are the left and right operands of the comparison, respectively.
4155
4156    If the optimization described above can be done, we return the resulting
4157    tree.  Otherwise we return zero.  */
4158
4159 static tree
4160 optimize_bit_field_compare (location_t loc, enum tree_code code,
4161                             tree compare_type, tree lhs, tree rhs)
4162 {
4163   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
4164   tree type = TREE_TYPE (lhs);
4165   tree signed_type, unsigned_type;
4166   int const_p = TREE_CODE (rhs) == INTEGER_CST;
4167   enum machine_mode lmode, rmode, nmode;
4168   int lunsignedp, runsignedp;
4169   int lvolatilep = 0, rvolatilep = 0;
4170   tree linner, rinner = NULL_TREE;
4171   tree mask;
4172   tree offset;
4173
4174   /* Get all the information about the extractions being done.  If the bit size
4175      if the same as the size of the underlying object, we aren't doing an
4176      extraction at all and so can do nothing.  We also don't want to
4177      do anything if the inner expression is a PLACEHOLDER_EXPR since we
4178      then will no longer be able to replace it.  */
4179   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
4180                                 &lunsignedp, &lvolatilep, false);
4181   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
4182       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
4183     return 0;
4184
4185  if (!const_p)
4186    {
4187      /* If this is not a constant, we can only do something if bit positions,
4188         sizes, and signedness are the same.  */
4189      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4190                                    &runsignedp, &rvolatilep, false);
4191
4192      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
4193          || lunsignedp != runsignedp || offset != 0
4194          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
4195        return 0;
4196    }
4197
4198   /* See if we can find a mode to refer to this field.  We should be able to,
4199      but fail if we can't.  */
4200   nmode = get_best_mode (lbitsize, lbitpos,
4201                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4202                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4203                                 TYPE_ALIGN (TREE_TYPE (rinner))),
4204                          word_mode, lvolatilep || rvolatilep);
4205   if (nmode == VOIDmode)
4206     return 0;
4207
4208   /* Set signed and unsigned types of the precision of this mode for the
4209      shifts below.  */
4210   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
4211   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4212
4213   /* Compute the bit position and size for the new reference and our offset
4214      within it. If the new reference is the same size as the original, we
4215      won't optimize anything, so return zero.  */
4216   nbitsize = GET_MODE_BITSIZE (nmode);
4217   nbitpos = lbitpos & ~ (nbitsize - 1);
4218   lbitpos -= nbitpos;
4219   if (nbitsize == lbitsize)
4220     return 0;
4221
4222   if (BYTES_BIG_ENDIAN)
4223     lbitpos = nbitsize - lbitsize - lbitpos;
4224
4225   /* Make the mask to be used against the extracted field.  */
4226   mask = build_int_cst_type (unsigned_type, -1);
4227   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
4228   mask = const_binop (RSHIFT_EXPR, mask,
4229                       size_int (nbitsize - lbitsize - lbitpos), 0);
4230
4231   if (! const_p)
4232     /* If not comparing with constant, just rework the comparison
4233        and return.  */
4234     return fold_build2_loc (loc, code, compare_type,
4235                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4236                                      make_bit_field_ref (loc, linner,
4237                                                          unsigned_type,
4238                                                          nbitsize, nbitpos,
4239                                                          1),
4240                                      mask),
4241                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4242                                      make_bit_field_ref (loc, rinner,
4243                                                          unsigned_type,
4244                                                          nbitsize, nbitpos,
4245                                                          1),
4246                                      mask));
4247
4248   /* Otherwise, we are handling the constant case. See if the constant is too
4249      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
4250      this not only for its own sake, but to avoid having to test for this
4251      error case below.  If we didn't, we might generate wrong code.
4252
4253      For unsigned fields, the constant shifted right by the field length should
4254      be all zero.  For signed fields, the high-order bits should agree with
4255      the sign bit.  */
4256
4257   if (lunsignedp)
4258     {
4259       if (! integer_zerop (const_binop (RSHIFT_EXPR,
4260                                         fold_convert_loc (loc,
4261                                                           unsigned_type, rhs),
4262                                         size_int (lbitsize), 0)))
4263         {
4264           warning (0, "comparison is always %d due to width of bit-field",
4265                    code == NE_EXPR);
4266           return constant_boolean_node (code == NE_EXPR, compare_type);
4267         }
4268     }
4269   else
4270     {
4271       tree tem = const_binop (RSHIFT_EXPR,
4272                               fold_convert_loc (loc, signed_type, rhs),
4273                               size_int (lbitsize - 1), 0);
4274       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
4275         {
4276           warning (0, "comparison is always %d due to width of bit-field",
4277                    code == NE_EXPR);
4278           return constant_boolean_node (code == NE_EXPR, compare_type);
4279         }
4280     }
4281
4282   /* Single-bit compares should always be against zero.  */
4283   if (lbitsize == 1 && ! integer_zerop (rhs))
4284     {
4285       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4286       rhs = build_int_cst (type, 0);
4287     }
4288
4289   /* Make a new bitfield reference, shift the constant over the
4290      appropriate number of bits and mask it with the computed mask
4291      (in case this was a signed field).  If we changed it, make a new one.  */
4292   lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
4293   if (lvolatilep)
4294     {
4295       TREE_SIDE_EFFECTS (lhs) = 1;
4296       TREE_THIS_VOLATILE (lhs) = 1;
4297     }
4298
4299   rhs = const_binop (BIT_AND_EXPR,
4300                      const_binop (LSHIFT_EXPR,
4301                                   fold_convert_loc (loc, unsigned_type, rhs),
4302                                   size_int (lbitpos), 0),
4303                      mask, 0);
4304
4305   lhs = build2 (code, compare_type,
4306                 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4307                 rhs);
4308   SET_EXPR_LOCATION (lhs, loc);
4309   return lhs;
4310 }
4311 \f
4312 /* Subroutine for fold_truthop: decode a field reference.
4313
4314    If EXP is a comparison reference, we return the innermost reference.
4315
4316    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4317    set to the starting bit number.
4318
4319    If the innermost field can be completely contained in a mode-sized
4320    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
4321
4322    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4323    otherwise it is not changed.
4324
4325    *PUNSIGNEDP is set to the signedness of the field.
4326
4327    *PMASK is set to the mask used.  This is either contained in a
4328    BIT_AND_EXPR or derived from the width of the field.
4329
4330    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4331
4332    Return 0 if this is not a component reference or is one that we can't
4333    do anything with.  */
4334
4335 static tree
4336 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
4337                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4338                         int *punsignedp, int *pvolatilep,
4339                         tree *pmask, tree *pand_mask)
4340 {
4341   tree outer_type = 0;
4342   tree and_mask = 0;
4343   tree mask, inner, offset;
4344   tree unsigned_type;
4345   unsigned int precision;
4346
4347   /* All the optimizations using this function assume integer fields.
4348      There are problems with FP fields since the type_for_size call
4349      below can fail for, e.g., XFmode.  */
4350   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4351     return 0;
4352
4353   /* We are interested in the bare arrangement of bits, so strip everything
4354      that doesn't affect the machine mode.  However, record the type of the
4355      outermost expression if it may matter below.  */
4356   if (CONVERT_EXPR_P (exp)
4357       || TREE_CODE (exp) == NON_LVALUE_EXPR)
4358     outer_type = TREE_TYPE (exp);
4359   STRIP_NOPS (exp);
4360
4361   if (TREE_CODE (exp) == BIT_AND_EXPR)
4362     {
4363       and_mask = TREE_OPERAND (exp, 1);
4364       exp = TREE_OPERAND (exp, 0);
4365       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4366       if (TREE_CODE (and_mask) != INTEGER_CST)
4367         return 0;
4368     }
4369
4370   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4371                                punsignedp, pvolatilep, false);
4372   if ((inner == exp && and_mask == 0)
4373       || *pbitsize < 0 || offset != 0
4374       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4375     return 0;
4376
4377   /* If the number of bits in the reference is the same as the bitsize of
4378      the outer type, then the outer type gives the signedness. Otherwise
4379      (in case of a small bitfield) the signedness is unchanged.  */
4380   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4381     *punsignedp = TYPE_UNSIGNED (outer_type);
4382
4383   /* Compute the mask to access the bitfield.  */
4384   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4385   precision = TYPE_PRECISION (unsigned_type);
4386
4387   mask = build_int_cst_type (unsigned_type, -1);
4388
4389   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4390   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4391
4392   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
4393   if (and_mask != 0)
4394     mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4395                         fold_convert_loc (loc, unsigned_type, and_mask), mask);
4396
4397   *pmask = mask;
4398   *pand_mask = and_mask;
4399   return inner;
4400 }
4401
4402 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4403    bit positions.  */
4404
4405 static int
4406 all_ones_mask_p (const_tree mask, int size)
4407 {
4408   tree type = TREE_TYPE (mask);
4409   unsigned int precision = TYPE_PRECISION (type);
4410   tree tmask;
4411
4412   tmask = build_int_cst_type (signed_type_for (type), -1);
4413
4414   return
4415     tree_int_cst_equal (mask,
4416                         const_binop (RSHIFT_EXPR,
4417                                      const_binop (LSHIFT_EXPR, tmask,
4418                                                   size_int (precision - size),
4419                                                   0),
4420                                      size_int (precision - size), 0));
4421 }
4422
4423 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4424    represents the sign bit of EXP's type.  If EXP represents a sign
4425    or zero extension, also test VAL against the unextended type.
4426    The return value is the (sub)expression whose sign bit is VAL,
4427    or NULL_TREE otherwise.  */
4428
4429 static tree
4430 sign_bit_p (tree exp, const_tree val)
4431 {
4432   unsigned HOST_WIDE_INT mask_lo, lo;
4433   HOST_WIDE_INT mask_hi, hi;
4434   int width;
4435   tree t;
4436
4437   /* Tree EXP must have an integral type.  */
4438   t = TREE_TYPE (exp);
4439   if (! INTEGRAL_TYPE_P (t))
4440     return NULL_TREE;
4441
4442   /* Tree VAL must be an integer constant.  */
4443   if (TREE_CODE (val) != INTEGER_CST
4444       || TREE_OVERFLOW (val))
4445     return NULL_TREE;
4446
4447   width = TYPE_PRECISION (t);
4448   if (width > HOST_BITS_PER_WIDE_INT)
4449     {
4450       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4451       lo = 0;
4452
4453       mask_hi = ((unsigned HOST_WIDE_INT) -1
4454                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
4455       mask_lo = -1;
4456     }
4457   else
4458     {
4459       hi = 0;
4460       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4461
4462       mask_hi = 0;
4463       mask_lo = ((unsigned HOST_WIDE_INT) -1
4464                  >> (HOST_BITS_PER_WIDE_INT - width));
4465     }
4466
4467   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4468      treat VAL as if it were unsigned.  */
4469   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4470       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4471     return exp;
4472
4473   /* Handle extension from a narrower type.  */
4474   if (TREE_CODE (exp) == NOP_EXPR
4475       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4476     return sign_bit_p (TREE_OPERAND (exp, 0), val);
4477
4478   return NULL_TREE;
4479 }
4480
4481 /* Subroutine for fold_truthop: determine if an operand is simple enough
4482    to be evaluated unconditionally.  */
4483
4484 static int
4485 simple_operand_p (const_tree exp)
4486 {
4487   /* Strip any conversions that don't change the machine mode.  */
4488   STRIP_NOPS (exp);
4489
4490   return (CONSTANT_CLASS_P (exp)
4491           || TREE_CODE (exp) == SSA_NAME
4492           || (DECL_P (exp)
4493               && ! TREE_ADDRESSABLE (exp)
4494               && ! TREE_THIS_VOLATILE (exp)
4495               && ! DECL_NONLOCAL (exp)
4496               /* Don't regard global variables as simple.  They may be
4497                  allocated in ways unknown to the compiler (shared memory,
4498                  #pragma weak, etc).  */
4499               && ! TREE_PUBLIC (exp)
4500               && ! DECL_EXTERNAL (exp)
4501               /* Loading a static variable is unduly expensive, but global
4502                  registers aren't expensive.  */
4503               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4504 }
4505 \f
4506 /* The following functions are subroutines to fold_range_test and allow it to
4507    try to change a logical combination of comparisons into a range test.
4508
4509    For example, both
4510         X == 2 || X == 3 || X == 4 || X == 5
4511    and
4512         X >= 2 && X <= 5
4513    are converted to
4514         (unsigned) (X - 2) <= 3
4515
4516    We describe each set of comparisons as being either inside or outside
4517    a range, using a variable named like IN_P, and then describe the
4518    range with a lower and upper bound.  If one of the bounds is omitted,
4519    it represents either the highest or lowest value of the type.
4520
4521    In the comments below, we represent a range by two numbers in brackets
4522    preceded by a "+" to designate being inside that range, or a "-" to
4523    designate being outside that range, so the condition can be inverted by
4524    flipping the prefix.  An omitted bound is represented by a "-".  For
4525    example, "- [-, 10]" means being outside the range starting at the lowest
4526    possible value and ending at 10, in other words, being greater than 10.
4527    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4528    always false.
4529
4530    We set up things so that the missing bounds are handled in a consistent
4531    manner so neither a missing bound nor "true" and "false" need to be
4532    handled using a special case.  */
4533
4534 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4535    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4536    and UPPER1_P are nonzero if the respective argument is an upper bound
4537    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
4538    must be specified for a comparison.  ARG1 will be converted to ARG0's
4539    type if both are specified.  */
4540
4541 static tree
4542 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4543              tree arg1, int upper1_p)
4544 {
4545   tree tem;
4546   int result;
4547   int sgn0, sgn1;
4548
4549   /* If neither arg represents infinity, do the normal operation.
4550      Else, if not a comparison, return infinity.  Else handle the special
4551      comparison rules. Note that most of the cases below won't occur, but
4552      are handled for consistency.  */
4553
4554   if (arg0 != 0 && arg1 != 0)
4555     {
4556       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4557                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
4558       STRIP_NOPS (tem);
4559       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4560     }
4561
4562   if (TREE_CODE_CLASS (code) != tcc_comparison)
4563     return 0;
4564
4565   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4566      for neither.  In real maths, we cannot assume open ended ranges are
4567      the same. But, this is computer arithmetic, where numbers are finite.
4568      We can therefore make the transformation of any unbounded range with
4569      the value Z, Z being greater than any representable number. This permits
4570      us to treat unbounded ranges as equal.  */
4571   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4572   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4573   switch (code)
4574     {
4575     case EQ_EXPR:
4576       result = sgn0 == sgn1;
4577       break;
4578     case NE_EXPR:
4579       result = sgn0 != sgn1;
4580       break;
4581     case LT_EXPR:
4582       result = sgn0 < sgn1;
4583       break;
4584     case LE_EXPR:
4585       result = sgn0 <= sgn1;
4586       break;
4587     case GT_EXPR:
4588       result = sgn0 > sgn1;
4589       break;
4590     case GE_EXPR:
4591       result = sgn0 >= sgn1;
4592       break;
4593     default:
4594       gcc_unreachable ();
4595     }
4596
4597   return constant_boolean_node (result, type);
4598 }
4599 \f
4600 /* Given EXP, a logical expression, set the range it is testing into
4601    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4602    actually being tested.  *PLOW and *PHIGH will be made of the same
4603    type as the returned expression.  If EXP is not a comparison, we
4604    will most likely not be returning a useful value and range.  Set
4605    *STRICT_OVERFLOW_P to true if the return value is only valid
4606    because signed overflow is undefined; otherwise, do not change
4607    *STRICT_OVERFLOW_P.  */
4608
4609 tree
4610 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4611             bool *strict_overflow_p)
4612 {
4613   enum tree_code code;
4614   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4615   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4616   int in_p, n_in_p;
4617   tree low, high, n_low, n_high;
4618   location_t loc = EXPR_LOCATION (exp);
4619
4620   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4621      and see if we can refine the range.  Some of the cases below may not
4622      happen, but it doesn't seem worth worrying about this.  We "continue"
4623      the outer loop when we've changed something; otherwise we "break"
4624      the switch, which will "break" the while.  */
4625
4626   in_p = 0;
4627   low = high = build_int_cst (TREE_TYPE (exp), 0);
4628
4629   while (1)
4630     {
4631       code = TREE_CODE (exp);
4632       exp_type = TREE_TYPE (exp);
4633
4634       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4635         {
4636           if (TREE_OPERAND_LENGTH (exp) > 0)
4637             arg0 = TREE_OPERAND (exp, 0);
4638           if (TREE_CODE_CLASS (code) == tcc_comparison
4639               || TREE_CODE_CLASS (code) == tcc_unary
4640               || TREE_CODE_CLASS (code) == tcc_binary)
4641             arg0_type = TREE_TYPE (arg0);
4642           if (TREE_CODE_CLASS (code) == tcc_binary
4643               || TREE_CODE_CLASS (code) == tcc_comparison
4644               || (TREE_CODE_CLASS (code) == tcc_expression
4645                   && TREE_OPERAND_LENGTH (exp) > 1))
4646             arg1 = TREE_OPERAND (exp, 1);
4647         }
4648
4649       switch (code)
4650         {
4651         case TRUTH_NOT_EXPR:
4652           in_p = ! in_p, exp = arg0;
4653           continue;
4654
4655         case EQ_EXPR: case NE_EXPR:
4656         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4657           /* We can only do something if the range is testing for zero
4658              and if the second operand is an integer constant.  Note that
4659              saying something is "in" the range we make is done by
4660              complementing IN_P since it will set in the initial case of
4661              being not equal to zero; "out" is leaving it alone.  */
4662           if (low == 0 || high == 0
4663               || ! integer_zerop (low) || ! integer_zerop (high)
4664               || TREE_CODE (arg1) != INTEGER_CST)
4665             break;
4666
4667           switch (code)
4668             {
4669             case NE_EXPR:  /* - [c, c]  */
4670               low = high = arg1;
4671               break;
4672             case EQ_EXPR:  /* + [c, c]  */
4673               in_p = ! in_p, low = high = arg1;
4674               break;
4675             case GT_EXPR:  /* - [-, c] */
4676               low = 0, high = arg1;
4677               break;
4678             case GE_EXPR:  /* + [c, -] */
4679               in_p = ! in_p, low = arg1, high = 0;
4680               break;
4681             case LT_EXPR:  /* - [c, -] */
4682               low = arg1, high = 0;
4683               break;
4684             case LE_EXPR:  /* + [-, c] */
4685               in_p = ! in_p, low = 0, high = arg1;
4686               break;
4687             default:
4688               gcc_unreachable ();
4689             }
4690
4691           /* If this is an unsigned comparison, we also know that EXP is
4692              greater than or equal to zero.  We base the range tests we make
4693              on that fact, so we record it here so we can parse existing
4694              range tests.  We test arg0_type since often the return type
4695              of, e.g. EQ_EXPR, is boolean.  */
4696           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4697             {
4698               if (! merge_ranges (&n_in_p, &n_low, &n_high,
4699                                   in_p, low, high, 1,
4700                                   build_int_cst (arg0_type, 0),
4701                                   NULL_TREE))
4702                 break;
4703
4704               in_p = n_in_p, low = n_low, high = n_high;
4705
4706               /* If the high bound is missing, but we have a nonzero low
4707                  bound, reverse the range so it goes from zero to the low bound
4708                  minus 1.  */
4709               if (high == 0 && low && ! integer_zerop (low))
4710                 {
4711                   in_p = ! in_p;
4712                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4713                                       integer_one_node, 0);
4714                   low = build_int_cst (arg0_type, 0);
4715                 }
4716             }
4717
4718           exp = arg0;
4719           continue;
4720
4721         case NEGATE_EXPR:
4722           /* (-x) IN [a,b] -> x in [-b, -a]  */
4723           n_low = range_binop (MINUS_EXPR, exp_type,
4724                                build_int_cst (exp_type, 0),
4725                                0, high, 1);
4726           n_high = range_binop (MINUS_EXPR, exp_type,
4727                                 build_int_cst (exp_type, 0),
4728                                 0, low, 0);
4729           low = n_low, high = n_high;
4730           exp = arg0;
4731           continue;
4732
4733         case BIT_NOT_EXPR:
4734           /* ~ X -> -X - 1  */
4735           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4736                         build_int_cst (exp_type, 1));
4737           SET_EXPR_LOCATION (exp, loc);
4738           continue;
4739
4740         case PLUS_EXPR:  case MINUS_EXPR:
4741           if (TREE_CODE (arg1) != INTEGER_CST)
4742             break;
4743
4744           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4745              move a constant to the other side.  */
4746           if (!TYPE_UNSIGNED (arg0_type)
4747               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4748             break;
4749
4750           /* If EXP is signed, any overflow in the computation is undefined,
4751              so we don't worry about it so long as our computations on
4752              the bounds don't overflow.  For unsigned, overflow is defined
4753              and this is exactly the right thing.  */
4754           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4755                                arg0_type, low, 0, arg1, 0);
4756           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4757                                 arg0_type, high, 1, arg1, 0);
4758           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4759               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4760             break;
4761
4762           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4763             *strict_overflow_p = true;
4764
4765           /* Check for an unsigned range which has wrapped around the maximum
4766              value thus making n_high < n_low, and normalize it.  */
4767           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4768             {
4769               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4770                                  integer_one_node, 0);
4771               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4772                                   integer_one_node, 0);
4773
4774               /* If the range is of the form +/- [ x+1, x ], we won't
4775                  be able to normalize it.  But then, it represents the
4776                  whole range or the empty set, so make it
4777                  +/- [ -, - ].  */
4778               if (tree_int_cst_equal (n_low, low)
4779                   && tree_int_cst_equal (n_high, high))
4780                 low = high = 0;
4781               else
4782                 in_p = ! in_p;
4783             }
4784           else
4785             low = n_low, high = n_high;
4786
4787           exp = arg0;
4788           continue;
4789
4790         CASE_CONVERT: case NON_LVALUE_EXPR:
4791           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4792             break;
4793
4794           if (! INTEGRAL_TYPE_P (arg0_type)
4795               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4796               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4797             break;
4798
4799           n_low = low, n_high = high;
4800
4801           if (n_low != 0)
4802             n_low = fold_convert_loc (loc, arg0_type, n_low);
4803
4804           if (n_high != 0)
4805             n_high = fold_convert_loc (loc, arg0_type, n_high);
4806
4807
4808           /* If we're converting arg0 from an unsigned type, to exp,
4809              a signed type,  we will be doing the comparison as unsigned.
4810              The tests above have already verified that LOW and HIGH
4811              are both positive.
4812
4813              So we have to ensure that we will handle large unsigned
4814              values the same way that the current signed bounds treat
4815              negative values.  */
4816
4817           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4818             {
4819               tree high_positive;
4820               tree equiv_type;
4821               /* For fixed-point modes, we need to pass the saturating flag
4822                  as the 2nd parameter.  */
4823               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4824                 equiv_type = lang_hooks.types.type_for_mode
4825                              (TYPE_MODE (arg0_type),
4826                               TYPE_SATURATING (arg0_type));
4827               else
4828                 equiv_type = lang_hooks.types.type_for_mode
4829                              (TYPE_MODE (arg0_type), 1);
4830
4831               /* A range without an upper bound is, naturally, unbounded.
4832                  Since convert would have cropped a very large value, use
4833                  the max value for the destination type.  */
4834               high_positive
4835                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4836                 : TYPE_MAX_VALUE (arg0_type);
4837
4838               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4839                 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4840                                              fold_convert_loc (loc, arg0_type,
4841                                                                high_positive),
4842                                              build_int_cst (arg0_type, 1));
4843
4844               /* If the low bound is specified, "and" the range with the
4845                  range for which the original unsigned value will be
4846                  positive.  */
4847               if (low != 0)
4848                 {
4849                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4850                                       1, n_low, n_high, 1,
4851                                       fold_convert_loc (loc, arg0_type,
4852                                                         integer_zero_node),
4853                                       high_positive))
4854                     break;
4855
4856                   in_p = (n_in_p == in_p);
4857                 }
4858               else
4859                 {
4860                   /* Otherwise, "or" the range with the range of the input
4861                      that will be interpreted as negative.  */
4862                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4863                                       0, n_low, n_high, 1,
4864                                       fold_convert_loc (loc, arg0_type,
4865                                                         integer_zero_node),
4866                                       high_positive))
4867                     break;
4868
4869                   in_p = (in_p != n_in_p);
4870                 }
4871             }
4872
4873           exp = arg0;
4874           low = n_low, high = n_high;
4875           continue;
4876
4877         default:
4878           break;
4879         }
4880
4881       break;
4882     }
4883
4884   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4885   if (TREE_CODE (exp) == INTEGER_CST)
4886     {
4887       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4888                                                  exp, 0, low, 0))
4889                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4890                                                     exp, 1, high, 1)));
4891       low = high = 0;
4892       exp = 0;
4893     }
4894
4895   *pin_p = in_p, *plow = low, *phigh = high;
4896   return exp;
4897 }
4898 \f
4899 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4900    type, TYPE, return an expression to test if EXP is in (or out of, depending
4901    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4902
4903 tree
4904 build_range_check (location_t loc, tree type, tree exp, int in_p,
4905                    tree low, tree high)
4906 {
4907   tree etype = TREE_TYPE (exp), value;
4908
4909 #ifdef HAVE_canonicalize_funcptr_for_compare
4910   /* Disable this optimization for function pointer expressions
4911      on targets that require function pointer canonicalization.  */
4912   if (HAVE_canonicalize_funcptr_for_compare
4913       && TREE_CODE (etype) == POINTER_TYPE
4914       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4915     return NULL_TREE;
4916 #endif
4917
4918   if (! in_p)
4919     {
4920       value = build_range_check (loc, type, exp, 1, low, high);
4921       if (value != 0)
4922         return invert_truthvalue_loc (loc, value);
4923
4924       return 0;
4925     }
4926
4927   if (low == 0 && high == 0)
4928     return build_int_cst (type, 1);
4929
4930   if (low == 0)
4931     return fold_build2_loc (loc, LE_EXPR, type, exp,
4932                         fold_convert_loc (loc, etype, high));
4933
4934   if (high == 0)
4935     return fold_build2_loc (loc, GE_EXPR, type, exp,
4936                         fold_convert_loc (loc, etype, low));
4937
4938   if (operand_equal_p (low, high, 0))
4939     return fold_build2_loc (loc, EQ_EXPR, type, exp,
4940                         fold_convert_loc (loc, etype, low));
4941
4942   if (integer_zerop (low))
4943     {
4944       if (! TYPE_UNSIGNED (etype))
4945         {
4946           etype = unsigned_type_for (etype);
4947           high = fold_convert_loc (loc, etype, high);
4948           exp = fold_convert_loc (loc, etype, exp);
4949         }
4950       return build_range_check (loc, type, exp, 1, 0, high);
4951     }
4952
4953   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4954   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4955     {
4956       unsigned HOST_WIDE_INT lo;
4957       HOST_WIDE_INT hi;
4958       int prec;
4959
4960       prec = TYPE_PRECISION (etype);
4961       if (prec <= HOST_BITS_PER_WIDE_INT)
4962         {
4963           hi = 0;
4964           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4965         }
4966       else
4967         {
4968           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4969           lo = (unsigned HOST_WIDE_INT) -1;
4970         }
4971
4972       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4973         {
4974           if (TYPE_UNSIGNED (etype))
4975             {
4976               tree signed_etype = signed_type_for (etype);
4977               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4978                 etype
4979                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4980               else
4981                 etype = signed_etype;
4982               exp = fold_convert_loc (loc, etype, exp);
4983             }
4984           return fold_build2_loc (loc, GT_EXPR, type, exp,
4985                               build_int_cst (etype, 0));
4986         }
4987     }
4988
4989   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4990      This requires wrap-around arithmetics for the type of the expression.
4991      First make sure that arithmetics in this type is valid, then make sure
4992      that it wraps around.  */
4993   if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4994     etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4995                                             TYPE_UNSIGNED (etype));
4996
4997   if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4998     {
4999       tree utype, minv, maxv;
5000
5001       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5002          for the type in question, as we rely on this here.  */
5003       utype = unsigned_type_for (etype);
5004       maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
5005       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5006                           integer_one_node, 1);
5007       minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
5008
5009       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5010                                       minv, 1, maxv, 1)))
5011         etype = utype;
5012       else
5013         return 0;
5014     }
5015
5016   high = fold_convert_loc (loc, etype, high);
5017   low = fold_convert_loc (loc, etype, low);
5018   exp = fold_convert_loc (loc, etype, exp);
5019
5020   value = const_binop (MINUS_EXPR, high, low, 0);
5021
5022
5023   if (POINTER_TYPE_P (etype))
5024     {
5025       if (value != 0 && !TREE_OVERFLOW (value))
5026         {
5027           low = fold_convert_loc (loc, sizetype, low);
5028           low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low);
5029           return build_range_check (loc, type,
5030                                     fold_build2_loc (loc, POINTER_PLUS_EXPR,
5031                                                  etype, exp, low),
5032                                     1, build_int_cst (etype, 0), value);
5033         }
5034       return 0;
5035     }
5036
5037   if (value != 0 && !TREE_OVERFLOW (value))
5038     return build_range_check (loc, type,
5039                               fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5040                               1, build_int_cst (etype, 0), value);
5041
5042   return 0;
5043 }
5044 \f
5045 /* Return the predecessor of VAL in its type, handling the infinite case.  */
5046
5047 static tree
5048 range_predecessor (tree val)
5049 {
5050   tree type = TREE_TYPE (val);
5051
5052   if (INTEGRAL_TYPE_P (type)
5053       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5054     return 0;
5055   else
5056     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
5057 }
5058
5059 /* Return the successor of VAL in its type, handling the infinite case.  */
5060
5061 static tree
5062 range_successor (tree val)
5063 {
5064   tree type = TREE_TYPE (val);
5065
5066   if (INTEGRAL_TYPE_P (type)
5067       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5068     return 0;
5069   else
5070     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
5071 }
5072
5073 /* Given two ranges, see if we can merge them into one.  Return 1 if we
5074    can, 0 if we can't.  Set the output range into the specified parameters.  */
5075
5076 bool
5077 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5078               tree high0, int in1_p, tree low1, tree high1)
5079 {
5080   int no_overlap;
5081   int subset;
5082   int temp;
5083   tree tem;
5084   int in_p;
5085   tree low, high;
5086   int lowequal = ((low0 == 0 && low1 == 0)
5087                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5088                                                 low0, 0, low1, 0)));
5089   int highequal = ((high0 == 0 && high1 == 0)
5090                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5091                                                  high0, 1, high1, 1)));
5092
5093   /* Make range 0 be the range that starts first, or ends last if they
5094      start at the same value.  Swap them if it isn't.  */
5095   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5096                                  low0, 0, low1, 0))
5097       || (lowequal
5098           && integer_onep (range_binop (GT_EXPR, integer_type_node,
5099                                         high1, 1, high0, 1))))
5100     {
5101       temp = in0_p, in0_p = in1_p, in1_p = temp;
5102       tem = low0, low0 = low1, low1 = tem;
5103       tem = high0, high0 = high1, high1 = tem;
5104     }
5105
5106   /* Now flag two cases, whether the ranges are disjoint or whether the
5107      second range is totally subsumed in the first.  Note that the tests
5108      below are simplified by the ones above.  */
5109   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5110                                           high0, 1, low1, 0));
5111   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5112                                       high1, 1, high0, 1));
5113
5114   /* We now have four cases, depending on whether we are including or
5115      excluding the two ranges.  */
5116   if (in0_p && in1_p)
5117     {
5118       /* If they don't overlap, the result is false.  If the second range
5119          is a subset it is the result.  Otherwise, the range is from the start
5120          of the second to the end of the first.  */
5121       if (no_overlap)
5122         in_p = 0, low = high = 0;
5123       else if (subset)
5124         in_p = 1, low = low1, high = high1;
5125       else
5126         in_p = 1, low = low1, high = high0;
5127     }
5128
5129   else if (in0_p && ! in1_p)
5130     {
5131       /* If they don't overlap, the result is the first range.  If they are
5132          equal, the result is false.  If the second range is a subset of the
5133          first, and the ranges begin at the same place, we go from just after
5134          the end of the second range to the end of the first.  If the second
5135          range is not a subset of the first, or if it is a subset and both
5136          ranges end at the same place, the range starts at the start of the
5137          first range and ends just before the second range.
5138          Otherwise, we can't describe this as a single range.  */
5139       if (no_overlap)
5140         in_p = 1, low = low0, high = high0;
5141       else if (lowequal && highequal)
5142         in_p = 0, low = high = 0;
5143       else if (subset && lowequal)
5144         {
5145           low = range_successor (high1);
5146           high = high0;
5147           in_p = 1;
5148           if (low == 0)
5149             {
5150               /* We are in the weird situation where high0 > high1 but
5151                  high1 has no successor.  Punt.  */
5152               return 0;
5153             }
5154         }
5155       else if (! subset || highequal)
5156         {
5157           low = low0;
5158           high = range_predecessor (low1);
5159           in_p = 1;
5160           if (high == 0)
5161             {
5162               /* low0 < low1 but low1 has no predecessor.  Punt.  */
5163               return 0;
5164             }
5165         }
5166       else
5167         return 0;
5168     }
5169
5170   else if (! in0_p && in1_p)
5171     {
5172       /* If they don't overlap, the result is the second range.  If the second
5173          is a subset of the first, the result is false.  Otherwise,
5174          the range starts just after the first range and ends at the
5175          end of the second.  */
5176       if (no_overlap)
5177         in_p = 1, low = low1, high = high1;
5178       else if (subset || highequal)
5179         in_p = 0, low = high = 0;
5180       else
5181         {
5182           low = range_successor (high0);
5183           high = high1;
5184           in_p = 1;
5185           if (low == 0)
5186             {
5187               /* high1 > high0 but high0 has no successor.  Punt.  */
5188               return 0;
5189             }
5190         }
5191     }
5192
5193   else
5194     {
5195       /* The case where we are excluding both ranges.  Here the complex case
5196          is if they don't overlap.  In that case, the only time we have a
5197          range is if they are adjacent.  If the second is a subset of the
5198          first, the result is the first.  Otherwise, the range to exclude
5199          starts at the beginning of the first range and ends at the end of the
5200          second.  */
5201       if (no_overlap)
5202         {
5203           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5204                                          range_successor (high0),
5205                                          1, low1, 0)))
5206             in_p = 0, low = low0, high = high1;
5207           else
5208             {
5209               /* Canonicalize - [min, x] into - [-, x].  */
5210               if (low0 && TREE_CODE (low0) == INTEGER_CST)
5211                 switch (TREE_CODE (TREE_TYPE (low0)))
5212                   {
5213                   case ENUMERAL_TYPE:
5214                     if (TYPE_PRECISION (TREE_TYPE (low0))
5215                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
5216                       break;
5217                     /* FALLTHROUGH */
5218                   case INTEGER_TYPE:
5219                     if (tree_int_cst_equal (low0,
5220                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
5221                       low0 = 0;
5222                     break;
5223                   case POINTER_TYPE:
5224                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
5225                         && integer_zerop (low0))
5226                       low0 = 0;
5227                     break;
5228                   default:
5229                     break;
5230                   }
5231
5232               /* Canonicalize - [x, max] into - [x, -].  */
5233               if (high1 && TREE_CODE (high1) == INTEGER_CST)
5234                 switch (TREE_CODE (TREE_TYPE (high1)))
5235                   {
5236                   case ENUMERAL_TYPE:
5237                     if (TYPE_PRECISION (TREE_TYPE (high1))
5238                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5239                       break;
5240                     /* FALLTHROUGH */
5241                   case INTEGER_TYPE:
5242                     if (tree_int_cst_equal (high1,
5243                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
5244                       high1 = 0;
5245                     break;
5246                   case POINTER_TYPE:
5247                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
5248                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5249                                                        high1, 1,
5250                                                        integer_one_node, 1)))
5251                       high1 = 0;
5252                     break;
5253                   default:
5254                     break;
5255                   }
5256
5257               /* The ranges might be also adjacent between the maximum and
5258                  minimum values of the given type.  For
5259                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5260                  return + [x + 1, y - 1].  */
5261               if (low0 == 0 && high1 == 0)
5262                 {
5263                   low = range_successor (high0);
5264                   high = range_predecessor (low1);
5265                   if (low == 0 || high == 0)
5266                     return 0;
5267
5268                   in_p = 1;
5269                 }
5270               else
5271                 return 0;
5272             }
5273         }
5274       else if (subset)
5275         in_p = 0, low = low0, high = high0;
5276       else
5277         in_p = 0, low = low0, high = high1;
5278     }
5279
5280   *pin_p = in_p, *plow = low, *phigh = high;
5281   return 1;
5282 }
5283 \f
5284
5285 /* Subroutine of fold, looking inside expressions of the form
5286    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5287    of the COND_EXPR.  This function is being used also to optimize
5288    A op B ? C : A, by reversing the comparison first.
5289
5290    Return a folded expression whose code is not a COND_EXPR
5291    anymore, or NULL_TREE if no folding opportunity is found.  */
5292
5293 static tree
5294 fold_cond_expr_with_comparison (location_t loc, tree type,
5295                                 tree arg0, tree arg1, tree arg2)
5296 {
5297   enum tree_code comp_code = TREE_CODE (arg0);
5298   tree arg00 = TREE_OPERAND (arg0, 0);
5299   tree arg01 = TREE_OPERAND (arg0, 1);
5300   tree arg1_type = TREE_TYPE (arg1);
5301   tree tem;
5302
5303   STRIP_NOPS (arg1);
5304   STRIP_NOPS (arg2);
5305
5306   /* If we have A op 0 ? A : -A, consider applying the following
5307      transformations:
5308
5309      A == 0? A : -A    same as -A
5310      A != 0? A : -A    same as A
5311      A >= 0? A : -A    same as abs (A)
5312      A > 0?  A : -A    same as abs (A)
5313      A <= 0? A : -A    same as -abs (A)
5314      A < 0?  A : -A    same as -abs (A)
5315
5316      None of these transformations work for modes with signed
5317      zeros.  If A is +/-0, the first two transformations will
5318      change the sign of the result (from +0 to -0, or vice
5319      versa).  The last four will fix the sign of the result,
5320      even though the original expressions could be positive or
5321      negative, depending on the sign of A.
5322
5323      Note that all these transformations are correct if A is
5324      NaN, since the two alternatives (A and -A) are also NaNs.  */
5325   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5326       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5327           ? real_zerop (arg01)
5328           : integer_zerop (arg01))
5329       && ((TREE_CODE (arg2) == NEGATE_EXPR
5330            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5331              /* In the case that A is of the form X-Y, '-A' (arg2) may
5332                 have already been folded to Y-X, check for that. */
5333           || (TREE_CODE (arg1) == MINUS_EXPR
5334               && TREE_CODE (arg2) == MINUS_EXPR
5335               && operand_equal_p (TREE_OPERAND (arg1, 0),
5336                                   TREE_OPERAND (arg2, 1), 0)
5337               && operand_equal_p (TREE_OPERAND (arg1, 1),
5338                                   TREE_OPERAND (arg2, 0), 0))))
5339     switch (comp_code)
5340       {
5341       case EQ_EXPR:
5342       case UNEQ_EXPR:
5343         tem = fold_convert_loc (loc, arg1_type, arg1);
5344         return pedantic_non_lvalue_loc (loc,
5345                                     fold_convert_loc (loc, type,
5346                                                   negate_expr (tem)));
5347       case NE_EXPR:
5348       case LTGT_EXPR:
5349         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5350       case UNGE_EXPR:
5351       case UNGT_EXPR:
5352         if (flag_trapping_math)
5353           break;
5354         /* Fall through.  */
5355       case GE_EXPR:
5356       case GT_EXPR:
5357         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5358           arg1 = fold_convert_loc (loc, signed_type_for
5359                                (TREE_TYPE (arg1)), arg1);
5360         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5361         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5362       case UNLE_EXPR:
5363       case UNLT_EXPR:
5364         if (flag_trapping_math)
5365           break;
5366       case LE_EXPR:
5367       case LT_EXPR:
5368         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5369           arg1 = fold_convert_loc (loc, signed_type_for
5370                                (TREE_TYPE (arg1)), arg1);
5371         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5372         return negate_expr (fold_convert_loc (loc, type, tem));
5373       default:
5374         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5375         break;
5376       }
5377
5378   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
5379      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
5380      both transformations are correct when A is NaN: A != 0
5381      is then true, and A == 0 is false.  */
5382
5383   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5384       && integer_zerop (arg01) && integer_zerop (arg2))
5385     {
5386       if (comp_code == NE_EXPR)
5387         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5388       else if (comp_code == EQ_EXPR)
5389         return build_int_cst (type, 0);
5390     }
5391
5392   /* Try some transformations of A op B ? A : B.
5393
5394      A == B? A : B    same as B
5395      A != B? A : B    same as A
5396      A >= B? A : B    same as max (A, B)
5397      A > B?  A : B    same as max (B, A)
5398      A <= B? A : B    same as min (A, B)
5399      A < B?  A : B    same as min (B, A)
5400
5401      As above, these transformations don't work in the presence
5402      of signed zeros.  For example, if A and B are zeros of
5403      opposite sign, the first two transformations will change
5404      the sign of the result.  In the last four, the original
5405      expressions give different results for (A=+0, B=-0) and
5406      (A=-0, B=+0), but the transformed expressions do not.
5407
5408      The first two transformations are correct if either A or B
5409      is a NaN.  In the first transformation, the condition will
5410      be false, and B will indeed be chosen.  In the case of the
5411      second transformation, the condition A != B will be true,
5412      and A will be chosen.
5413
5414      The conversions to max() and min() are not correct if B is
5415      a number and A is not.  The conditions in the original
5416      expressions will be false, so all four give B.  The min()
5417      and max() versions would give a NaN instead.  */
5418   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5419       && operand_equal_for_comparison_p (arg01, arg2, arg00)
5420       /* Avoid these transformations if the COND_EXPR may be used
5421          as an lvalue in the C++ front-end.  PR c++/19199.  */
5422       && (in_gimple_form
5423           || (strcmp (lang_hooks.name, "GNU C++") != 0
5424               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5425           || ! maybe_lvalue_p (arg1)
5426           || ! maybe_lvalue_p (arg2)))
5427     {
5428       tree comp_op0 = arg00;
5429       tree comp_op1 = arg01;
5430       tree comp_type = TREE_TYPE (comp_op0);
5431
5432       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
5433       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5434         {
5435           comp_type = type;
5436           comp_op0 = arg1;
5437           comp_op1 = arg2;
5438         }
5439
5440       switch (comp_code)
5441         {
5442         case EQ_EXPR:
5443           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
5444         case NE_EXPR:
5445           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5446         case LE_EXPR:
5447         case LT_EXPR:
5448         case UNLE_EXPR:
5449         case UNLT_EXPR:
5450           /* In C++ a ?: expression can be an lvalue, so put the
5451              operand which will be used if they are equal first
5452              so that we can convert this back to the
5453              corresponding COND_EXPR.  */
5454           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5455             {
5456               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5457               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5458               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5459                     ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5460                     : fold_build2_loc (loc, MIN_EXPR, comp_type,
5461                                    comp_op1, comp_op0);
5462               return pedantic_non_lvalue_loc (loc,
5463                                           fold_convert_loc (loc, type, tem));
5464             }
5465           break;
5466         case GE_EXPR:
5467         case GT_EXPR:
5468         case UNGE_EXPR:
5469         case UNGT_EXPR:
5470           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5471             {
5472               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5473               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5474               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5475                     ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5476                     : fold_build2_loc (loc, MAX_EXPR, comp_type,
5477                                    comp_op1, comp_op0);
5478               return pedantic_non_lvalue_loc (loc,
5479                                           fold_convert_loc (loc, type, tem));
5480             }
5481           break;
5482         case UNEQ_EXPR:
5483           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5484             return pedantic_non_lvalue_loc (loc,
5485                                         fold_convert_loc (loc, type, arg2));
5486           break;
5487         case LTGT_EXPR:
5488           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5489             return pedantic_non_lvalue_loc (loc,
5490                                         fold_convert_loc (loc, type, arg1));
5491           break;
5492         default:
5493           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5494           break;
5495         }
5496     }
5497
5498   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5499      we might still be able to simplify this.  For example,
5500      if C1 is one less or one more than C2, this might have started
5501      out as a MIN or MAX and been transformed by this function.
5502      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
5503
5504   if (INTEGRAL_TYPE_P (type)
5505       && TREE_CODE (arg01) == INTEGER_CST
5506       && TREE_CODE (arg2) == INTEGER_CST)
5507     switch (comp_code)
5508       {
5509       case EQ_EXPR:
5510         if (TREE_CODE (arg1) == INTEGER_CST)
5511           break;
5512         /* We can replace A with C1 in this case.  */
5513         arg1 = fold_convert_loc (loc, type, arg01);
5514         return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5515
5516       case LT_EXPR:
5517         /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5518            MIN_EXPR, to preserve the signedness of the comparison.  */
5519         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5520                                OEP_ONLY_CONST)
5521             && operand_equal_p (arg01,
5522                                 const_binop (PLUS_EXPR, arg2,
5523                                              build_int_cst (type, 1), 0),
5524                                 OEP_ONLY_CONST))
5525           {
5526             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5527                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5528                                                      arg2));
5529             return pedantic_non_lvalue_loc (loc, 
5530                                             fold_convert_loc (loc, type, tem));
5531           }
5532         break;
5533
5534       case LE_EXPR:
5535         /* If C1 is C2 - 1, this is min(A, C2), with the same care
5536            as above.  */
5537         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5538                                OEP_ONLY_CONST)
5539             && operand_equal_p (arg01,
5540                                 const_binop (MINUS_EXPR, arg2,
5541                                              build_int_cst (type, 1), 0),
5542                                 OEP_ONLY_CONST))
5543           {
5544             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5545                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5546                                                      arg2));
5547             return pedantic_non_lvalue_loc (loc,
5548                                             fold_convert_loc (loc, type, tem));
5549           }
5550         break;
5551
5552       case GT_EXPR:
5553         /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5554            MAX_EXPR, to preserve the signedness of the comparison.  */
5555         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5556                                OEP_ONLY_CONST)
5557             && operand_equal_p (arg01,
5558                                 const_binop (MINUS_EXPR, arg2,
5559                                              build_int_cst (type, 1), 0),
5560                                 OEP_ONLY_CONST))
5561           {
5562             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5563                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5564                                                      arg2));
5565             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5566           }
5567         break;
5568
5569       case GE_EXPR:
5570         /* If C1 is C2 + 1, this is max(A, C2), with the same care as above.  */
5571         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5572                                OEP_ONLY_CONST)
5573             && operand_equal_p (arg01,
5574                                 const_binop (PLUS_EXPR, arg2,
5575                                              build_int_cst (type, 1), 0),
5576                                 OEP_ONLY_CONST))
5577           {
5578             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5579                                    fold_convert_loc (loc, TREE_TYPE (arg00),
5580                                                      arg2));
5581             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5582           }
5583         break;
5584       case NE_EXPR:
5585         break;
5586       default:
5587         gcc_unreachable ();
5588       }
5589
5590   return NULL_TREE;
5591 }
5592
5593
5594 \f
5595 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5596 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5597   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5598                 false) >= 2)
5599 #endif
5600
5601 /* EXP is some logical combination of boolean tests.  See if we can
5602    merge it into some range test.  Return the new tree if so.  */
5603
5604 static tree
5605 fold_range_test (location_t loc, enum tree_code code, tree type,
5606                  tree op0, tree op1)
5607 {
5608   int or_op = (code == TRUTH_ORIF_EXPR
5609                || code == TRUTH_OR_EXPR);
5610   int in0_p, in1_p, in_p;
5611   tree low0, low1, low, high0, high1, high;
5612   bool strict_overflow_p = false;
5613   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5614   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5615   tree tem;
5616   const char * const warnmsg = G_("assuming signed overflow does not occur "
5617                                   "when simplifying range test");
5618
5619   /* If this is an OR operation, invert both sides; we will invert
5620      again at the end.  */
5621   if (or_op)
5622     in0_p = ! in0_p, in1_p = ! in1_p;
5623
5624   /* If both expressions are the same, if we can merge the ranges, and we
5625      can build the range test, return it or it inverted.  If one of the
5626      ranges is always true or always false, consider it to be the same
5627      expression as the other.  */
5628   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5629       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5630                        in1_p, low1, high1)
5631       && 0 != (tem = (build_range_check (UNKNOWN_LOCATION, type,
5632                                          lhs != 0 ? lhs
5633                                          : rhs != 0 ? rhs : integer_zero_node,
5634                                          in_p, low, high))))
5635     {
5636       if (strict_overflow_p)
5637         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5638       return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5639     }
5640
5641   /* On machines where the branch cost is expensive, if this is a
5642      short-circuited branch and the underlying object on both sides
5643      is the same, make a non-short-circuit operation.  */
5644   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5645            && lhs != 0 && rhs != 0
5646            && (code == TRUTH_ANDIF_EXPR
5647                || code == TRUTH_ORIF_EXPR)
5648            && operand_equal_p (lhs, rhs, 0))
5649     {
5650       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
5651          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5652          which cases we can't do this.  */
5653       if (simple_operand_p (lhs))
5654         {
5655           tem = build2 (code == TRUTH_ANDIF_EXPR
5656                         ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5657                         type, op0, op1);
5658           SET_EXPR_LOCATION (tem, loc);
5659           return tem;
5660         }
5661
5662       else if (lang_hooks.decls.global_bindings_p () == 0
5663                && ! CONTAINS_PLACEHOLDER_P (lhs))
5664         {
5665           tree common = save_expr (lhs);
5666
5667           if (0 != (lhs = build_range_check (loc, type, common,
5668                                              or_op ? ! in0_p : in0_p,
5669                                              low0, high0))
5670               && (0 != (rhs = build_range_check (loc, type, common,
5671                                                  or_op ? ! in1_p : in1_p,
5672                                                  low1, high1))))
5673             {
5674               if (strict_overflow_p)
5675                 fold_overflow_warning (warnmsg,
5676                                        WARN_STRICT_OVERFLOW_COMPARISON);
5677               tem = build2 (code == TRUTH_ANDIF_EXPR
5678                             ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5679                             type, lhs, rhs);
5680               SET_EXPR_LOCATION (tem, loc);
5681               return tem;
5682             }
5683         }
5684     }
5685
5686   return 0;
5687 }
5688 \f
5689 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5690    bit value.  Arrange things so the extra bits will be set to zero if and
5691    only if C is signed-extended to its full width.  If MASK is nonzero,
5692    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
5693
5694 static tree
5695 unextend (tree c, int p, int unsignedp, tree mask)
5696 {
5697   tree type = TREE_TYPE (c);
5698   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5699   tree temp;
5700
5701   if (p == modesize || unsignedp)
5702     return c;
5703
5704   /* We work by getting just the sign bit into the low-order bit, then
5705      into the high-order bit, then sign-extend.  We then XOR that value
5706      with C.  */
5707   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5708   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5709
5710   /* We must use a signed type in order to get an arithmetic right shift.
5711      However, we must also avoid introducing accidental overflows, so that
5712      a subsequent call to integer_zerop will work.  Hence we must
5713      do the type conversion here.  At this point, the constant is either
5714      zero or one, and the conversion to a signed type can never overflow.
5715      We could get an overflow if this conversion is done anywhere else.  */
5716   if (TYPE_UNSIGNED (type))
5717     temp = fold_convert (signed_type_for (type), temp);
5718
5719   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5720   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5721   if (mask != 0)
5722     temp = const_binop (BIT_AND_EXPR, temp,
5723                         fold_convert (TREE_TYPE (c), mask),
5724                         0);
5725   /* If necessary, convert the type back to match the type of C.  */
5726   if (TYPE_UNSIGNED (type))
5727     temp = fold_convert (type, temp);
5728
5729   return fold_convert (type,
5730                        const_binop (BIT_XOR_EXPR, c, temp, 0));
5731 }
5732 \f
5733 /* Find ways of folding logical expressions of LHS and RHS:
5734    Try to merge two comparisons to the same innermost item.
5735    Look for range tests like "ch >= '0' && ch <= '9'".
5736    Look for combinations of simple terms on machines with expensive branches
5737    and evaluate the RHS unconditionally.
5738
5739    For example, if we have p->a == 2 && p->b == 4 and we can make an
5740    object large enough to span both A and B, we can do this with a comparison
5741    against the object ANDed with the a mask.
5742
5743    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5744    operations to do this with one comparison.
5745
5746    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5747    function and the one above.
5748
5749    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5750    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5751
5752    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5753    two operands.
5754
5755    We return the simplified tree or 0 if no optimization is possible.  */
5756
5757 static tree
5758 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5759               tree lhs, tree rhs)
5760 {
5761   /* If this is the "or" of two comparisons, we can do something if
5762      the comparisons are NE_EXPR.  If this is the "and", we can do something
5763      if the comparisons are EQ_EXPR.  I.e.,
5764         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5765
5766      WANTED_CODE is this operation code.  For single bit fields, we can
5767      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5768      comparison for one-bit fields.  */
5769
5770   enum tree_code wanted_code;
5771   enum tree_code lcode, rcode;
5772   tree ll_arg, lr_arg, rl_arg, rr_arg;
5773   tree ll_inner, lr_inner, rl_inner, rr_inner;
5774   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5775   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5776   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5777   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5778   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5779   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5780   enum machine_mode lnmode, rnmode;
5781   tree ll_mask, lr_mask, rl_mask, rr_mask;
5782   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5783   tree l_const, r_const;
5784   tree lntype, rntype, result;
5785   HOST_WIDE_INT first_bit, end_bit;
5786   int volatilep;
5787   tree orig_lhs = lhs, orig_rhs = rhs;
5788   enum tree_code orig_code = code;
5789
5790   /* Start by getting the comparison codes.  Fail if anything is volatile.
5791      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5792      it were surrounded with a NE_EXPR.  */
5793
5794   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5795     return 0;
5796
5797   lcode = TREE_CODE (lhs);
5798   rcode = TREE_CODE (rhs);
5799
5800   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5801     {
5802       lhs = build2 (NE_EXPR, truth_type, lhs,
5803                     build_int_cst (TREE_TYPE (lhs), 0));
5804       lcode = NE_EXPR;
5805     }
5806
5807   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5808     {
5809       rhs = build2 (NE_EXPR, truth_type, rhs,
5810                     build_int_cst (TREE_TYPE (rhs), 0));
5811       rcode = NE_EXPR;
5812     }
5813
5814   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5815       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5816     return 0;
5817
5818   ll_arg = TREE_OPERAND (lhs, 0);
5819   lr_arg = TREE_OPERAND (lhs, 1);
5820   rl_arg = TREE_OPERAND (rhs, 0);
5821   rr_arg = TREE_OPERAND (rhs, 1);
5822
5823   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5824   if (simple_operand_p (ll_arg)
5825       && simple_operand_p (lr_arg))
5826     {
5827       tree result;
5828       if (operand_equal_p (ll_arg, rl_arg, 0)
5829           && operand_equal_p (lr_arg, rr_arg, 0))
5830         {
5831           result = combine_comparisons (loc, code, lcode, rcode,
5832                                         truth_type, ll_arg, lr_arg);
5833           if (result)
5834             return result;
5835         }
5836       else if (operand_equal_p (ll_arg, rr_arg, 0)
5837                && operand_equal_p (lr_arg, rl_arg, 0))
5838         {
5839           result = combine_comparisons (loc, code, lcode,
5840                                         swap_tree_comparison (rcode),
5841                                         truth_type, ll_arg, lr_arg);
5842           if (result)
5843             return result;
5844         }
5845     }
5846
5847   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5848           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5849
5850   /* If the RHS can be evaluated unconditionally and its operands are
5851      simple, it wins to evaluate the RHS unconditionally on machines
5852      with expensive branches.  In this case, this isn't a comparison
5853      that can be merged.  Avoid doing this if the RHS is a floating-point
5854      comparison since those can trap.  */
5855
5856   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5857                    false) >= 2
5858       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5859       && simple_operand_p (rl_arg)
5860       && simple_operand_p (rr_arg))
5861     {
5862       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5863       if (code == TRUTH_OR_EXPR
5864           && lcode == NE_EXPR && integer_zerop (lr_arg)
5865           && rcode == NE_EXPR && integer_zerop (rr_arg)
5866           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5867           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5868         {
5869           result = build2 (NE_EXPR, truth_type,
5870                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5871                                    ll_arg, rl_arg),
5872                            build_int_cst (TREE_TYPE (ll_arg), 0));
5873           goto fold_truthop_exit;
5874         }
5875
5876       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5877       if (code == TRUTH_AND_EXPR
5878           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5879           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5880           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5881           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5882         {
5883           result = build2 (EQ_EXPR, truth_type,
5884                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5885                                    ll_arg, rl_arg),
5886                            build_int_cst (TREE_TYPE (ll_arg), 0));
5887           goto fold_truthop_exit;
5888         }
5889
5890       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5891         {
5892           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5893             {
5894               result = build2 (code, truth_type, lhs, rhs);
5895               goto fold_truthop_exit;
5896             }
5897           return NULL_TREE;
5898         }
5899     }
5900
5901   /* See if the comparisons can be merged.  Then get all the parameters for
5902      each side.  */
5903
5904   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5905       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5906     return 0;
5907
5908   volatilep = 0;
5909   ll_inner = decode_field_reference (loc, ll_arg,
5910                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5911                                      &ll_unsignedp, &volatilep, &ll_mask,
5912                                      &ll_and_mask);
5913   lr_inner = decode_field_reference (loc, lr_arg,
5914                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5915                                      &lr_unsignedp, &volatilep, &lr_mask,
5916                                      &lr_and_mask);
5917   rl_inner = decode_field_reference (loc, rl_arg,
5918                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5919                                      &rl_unsignedp, &volatilep, &rl_mask,
5920                                      &rl_and_mask);
5921   rr_inner = decode_field_reference (loc, rr_arg,
5922                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5923                                      &rr_unsignedp, &volatilep, &rr_mask,
5924                                      &rr_and_mask);
5925
5926   /* It must be true that the inner operation on the lhs of each
5927      comparison must be the same if we are to be able to do anything.
5928      Then see if we have constants.  If not, the same must be true for
5929      the rhs's.  */
5930   if (volatilep || ll_inner == 0 || rl_inner == 0
5931       || ! operand_equal_p (ll_inner, rl_inner, 0))
5932     return 0;
5933
5934   if (TREE_CODE (lr_arg) == INTEGER_CST
5935       && TREE_CODE (rr_arg) == INTEGER_CST)
5936     l_const = lr_arg, r_const = rr_arg;
5937   else if (lr_inner == 0 || rr_inner == 0
5938            || ! operand_equal_p (lr_inner, rr_inner, 0))
5939     return 0;
5940   else
5941     l_const = r_const = 0;
5942
5943   /* If either comparison code is not correct for our logical operation,
5944      fail.  However, we can convert a one-bit comparison against zero into
5945      the opposite comparison against that bit being set in the field.  */
5946
5947   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5948   if (lcode != wanted_code)
5949     {
5950       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5951         {
5952           /* Make the left operand unsigned, since we are only interested
5953              in the value of one bit.  Otherwise we are doing the wrong
5954              thing below.  */
5955           ll_unsignedp = 1;
5956           l_const = ll_mask;
5957         }
5958       else
5959         return 0;
5960     }
5961
5962   /* This is analogous to the code for l_const above.  */
5963   if (rcode != wanted_code)
5964     {
5965       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5966         {
5967           rl_unsignedp = 1;
5968           r_const = rl_mask;
5969         }
5970       else
5971         return 0;
5972     }
5973
5974   /* See if we can find a mode that contains both fields being compared on
5975      the left.  If we can't, fail.  Otherwise, update all constants and masks
5976      to be relative to a field of that size.  */
5977   first_bit = MIN (ll_bitpos, rl_bitpos);
5978   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5979   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5980                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5981                           volatilep);
5982   if (lnmode == VOIDmode)
5983     return 0;
5984
5985   lnbitsize = GET_MODE_BITSIZE (lnmode);
5986   lnbitpos = first_bit & ~ (lnbitsize - 1);
5987   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5988   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5989
5990   if (BYTES_BIG_ENDIAN)
5991     {
5992       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5993       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5994     }
5995
5996   ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5997                          size_int (xll_bitpos), 0);
5998   rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5999                          size_int (xrl_bitpos), 0);
6000
6001   if (l_const)
6002     {
6003       l_const = fold_convert_loc (loc, lntype, l_const);
6004       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
6005       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
6006       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
6007                                         fold_build1_loc (loc, BIT_NOT_EXPR,
6008                                                      lntype, ll_mask),
6009                                         0)))
6010         {
6011           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6012
6013           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6014         }
6015     }
6016   if (r_const)
6017     {
6018       r_const = fold_convert_loc (loc, lntype, r_const);
6019       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
6020       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
6021       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
6022                                         fold_build1_loc (loc, BIT_NOT_EXPR,
6023                                                      lntype, rl_mask),
6024                                         0)))
6025         {
6026           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6027
6028           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6029         }
6030     }
6031
6032   /* If the right sides are not constant, do the same for it.  Also,
6033      disallow this optimization if a size or signedness mismatch occurs
6034      between the left and right sides.  */
6035   if (l_const == 0)
6036     {
6037       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
6038           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
6039           /* Make sure the two fields on the right
6040              correspond to the left without being swapped.  */
6041           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
6042         return 0;
6043
6044       first_bit = MIN (lr_bitpos, rr_bitpos);
6045       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
6046       rnmode = get_best_mode (end_bit - first_bit, first_bit,
6047                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
6048                               volatilep);
6049       if (rnmode == VOIDmode)
6050         return 0;
6051
6052       rnbitsize = GET_MODE_BITSIZE (rnmode);
6053       rnbitpos = first_bit & ~ (rnbitsize - 1);
6054       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
6055       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
6056
6057       if (BYTES_BIG_ENDIAN)
6058         {
6059           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
6060           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
6061         }
6062
6063       lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6064                                                             rntype, lr_mask),
6065                              size_int (xlr_bitpos), 0);
6066       rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6067                                                             rntype, rr_mask),
6068                              size_int (xrr_bitpos), 0);
6069
6070       /* Make a mask that corresponds to both fields being compared.
6071          Do this for both items being compared.  If the operands are the
6072          same size and the bits being compared are in the same position
6073          then we can do this by masking both and comparing the masked
6074          results.  */
6075       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
6076       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
6077       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
6078         {
6079           lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
6080                                     ll_unsignedp || rl_unsignedp);
6081           if (! all_ones_mask_p (ll_mask, lnbitsize))
6082             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6083
6084           rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
6085                                     lr_unsignedp || rr_unsignedp);
6086           if (! all_ones_mask_p (lr_mask, rnbitsize))
6087             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6088
6089           result = build2 (wanted_code, truth_type, lhs, rhs);
6090           goto fold_truthop_exit;
6091         }
6092
6093       /* There is still another way we can do something:  If both pairs of
6094          fields being compared are adjacent, we may be able to make a wider
6095          field containing them both.
6096
6097          Note that we still must mask the lhs/rhs expressions.  Furthermore,
6098          the mask must be shifted to account for the shift done by
6099          make_bit_field_ref.  */
6100       if ((ll_bitsize + ll_bitpos == rl_bitpos
6101            && lr_bitsize + lr_bitpos == rr_bitpos)
6102           || (ll_bitpos == rl_bitpos + rl_bitsize
6103               && lr_bitpos == rr_bitpos + rr_bitsize))
6104         {
6105           tree type;
6106
6107           lhs = make_bit_field_ref (loc, ll_inner, lntype,
6108                                     ll_bitsize + rl_bitsize,
6109                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
6110           rhs = make_bit_field_ref (loc, lr_inner, rntype,
6111                                     lr_bitsize + rr_bitsize,
6112                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
6113
6114           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6115                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
6116           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6117                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
6118
6119           /* Convert to the smaller type before masking out unwanted bits.  */
6120           type = lntype;
6121           if (lntype != rntype)
6122             {
6123               if (lnbitsize > rnbitsize)
6124                 {
6125                   lhs = fold_convert_loc (loc, rntype, lhs);
6126                   ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6127                   type = rntype;
6128                 }
6129               else if (lnbitsize < rnbitsize)
6130                 {
6131                   rhs = fold_convert_loc (loc, lntype, rhs);
6132                   lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6133                   type = lntype;
6134                 }
6135             }
6136
6137           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6138             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6139
6140           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6141             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6142
6143           result = build2 (wanted_code, truth_type, lhs, rhs);
6144           goto fold_truthop_exit;
6145         }
6146
6147       return 0;
6148     }
6149
6150   /* Handle the case of comparisons with constants.  If there is something in
6151      common between the masks, those bits of the constants must be the same.
6152      If not, the condition is always false.  Test for this to avoid generating
6153      incorrect code below.  */
6154   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
6155   if (! integer_zerop (result)
6156       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
6157                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
6158     {
6159       if (wanted_code == NE_EXPR)
6160         {
6161           warning (0, "%<or%> of unmatched not-equal tests is always 1");
6162           return constant_boolean_node (true, truth_type);
6163         }
6164       else
6165         {
6166           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6167           return constant_boolean_node (false, truth_type);
6168         }
6169     }
6170
6171   /* Construct the expression we will return.  First get the component
6172      reference we will make.  Unless the mask is all ones the width of
6173      that field, perform the mask operation.  Then compare with the
6174      merged constant.  */
6175   result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
6176                                ll_unsignedp || rl_unsignedp);
6177
6178   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
6179   if (! all_ones_mask_p (ll_mask, lnbitsize))
6180     {
6181       result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
6182       SET_EXPR_LOCATION (result, loc);
6183     }
6184
6185   result = build2 (wanted_code, truth_type, result,
6186                    const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
6187
6188  fold_truthop_exit:
6189   SET_EXPR_LOCATION (result, loc);
6190   return result;
6191 }
6192 \f
6193 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
6194    constant.  */
6195
6196 static tree
6197 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
6198                             tree op0, tree op1)
6199 {
6200   tree arg0 = op0;
6201   enum tree_code op_code;
6202   tree comp_const;
6203   tree minmax_const;
6204   int consts_equal, consts_lt;
6205   tree inner;
6206
6207   STRIP_SIGN_NOPS (arg0);
6208
6209   op_code = TREE_CODE (arg0);
6210   minmax_const = TREE_OPERAND (arg0, 1);
6211   comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
6212   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
6213   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
6214   inner = TREE_OPERAND (arg0, 0);
6215
6216   /* If something does not permit us to optimize, return the original tree.  */
6217   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
6218       || TREE_CODE (comp_const) != INTEGER_CST
6219       || TREE_OVERFLOW (comp_const)
6220       || TREE_CODE (minmax_const) != INTEGER_CST
6221       || TREE_OVERFLOW (minmax_const))
6222     return NULL_TREE;
6223
6224   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
6225      and GT_EXPR, doing the rest with recursive calls using logical
6226      simplifications.  */
6227   switch (code)
6228     {
6229     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
6230       {
6231         tree tem
6232           = optimize_minmax_comparison (loc,
6233                                         invert_tree_comparison (code, false),
6234                                         type, op0, op1);
6235         if (tem)
6236           return invert_truthvalue_loc (loc, tem);
6237         return NULL_TREE;
6238       }
6239
6240     case GE_EXPR:
6241       return
6242         fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
6243                      optimize_minmax_comparison
6244                      (loc, EQ_EXPR, type, arg0, comp_const),
6245                      optimize_minmax_comparison
6246                      (loc, GT_EXPR, type, arg0, comp_const));
6247
6248     case EQ_EXPR:
6249       if (op_code == MAX_EXPR && consts_equal)
6250         /* MAX (X, 0) == 0  ->  X <= 0  */
6251         return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
6252
6253       else if (op_code == MAX_EXPR && consts_lt)
6254         /* MAX (X, 0) == 5  ->  X == 5   */
6255         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
6256
6257       else if (op_code == MAX_EXPR)
6258         /* MAX (X, 0) == -1  ->  false  */
6259         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6260
6261       else if (consts_equal)
6262         /* MIN (X, 0) == 0  ->  X >= 0  */
6263         return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
6264
6265       else if (consts_lt)
6266         /* MIN (X, 0) == 5  ->  false  */
6267         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6268
6269       else
6270         /* MIN (X, 0) == -1  ->  X == -1  */
6271         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
6272
6273     case GT_EXPR:
6274       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
6275         /* MAX (X, 0) > 0  ->  X > 0
6276            MAX (X, 0) > 5  ->  X > 5  */
6277         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
6278
6279       else if (op_code == MAX_EXPR)
6280         /* MAX (X, 0) > -1  ->  true  */
6281         return omit_one_operand_loc (loc, type, integer_one_node, inner);
6282
6283       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
6284         /* MIN (X, 0) > 0  ->  false
6285            MIN (X, 0) > 5  ->  false  */
6286         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6287
6288       else
6289         /* MIN (X, 0) > -1  ->  X > -1  */
6290         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
6291
6292     default:
6293       return NULL_TREE;
6294     }
6295 }
6296 \f
6297 /* T is an integer expression that is being multiplied, divided, or taken a
6298    modulus (CODE says which and what kind of divide or modulus) by a
6299    constant C.  See if we can eliminate that operation by folding it with
6300    other operations already in T.  WIDE_TYPE, if non-null, is a type that
6301    should be used for the computation if wider than our type.
6302
6303    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6304    (X * 2) + (Y * 4).  We must, however, be assured that either the original
6305    expression would not overflow or that overflow is undefined for the type
6306    in the language in question.
6307
6308    If we return a non-null expression, it is an equivalent form of the
6309    original computation, but need not be in the original type.
6310
6311    We set *STRICT_OVERFLOW_P to true if the return values depends on
6312    signed overflow being undefined.  Otherwise we do not change
6313    *STRICT_OVERFLOW_P.  */
6314
6315 static tree
6316 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6317                 bool *strict_overflow_p)
6318 {
6319   /* To avoid exponential search depth, refuse to allow recursion past
6320      three levels.  Beyond that (1) it's highly unlikely that we'll find
6321      something interesting and (2) we've probably processed it before
6322      when we built the inner expression.  */
6323
6324   static int depth;
6325   tree ret;
6326
6327   if (depth > 3)
6328     return NULL;
6329
6330   depth++;
6331   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6332   depth--;
6333
6334   return ret;
6335 }
6336
6337 static tree
6338 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6339                   bool *strict_overflow_p)
6340 {
6341   tree type = TREE_TYPE (t);
6342   enum tree_code tcode = TREE_CODE (t);
6343   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6344                                    > GET_MODE_SIZE (TYPE_MODE (type)))
6345                 ? wide_type : type);
6346   tree t1, t2;
6347   int same_p = tcode == code;
6348   tree op0 = NULL_TREE, op1 = NULL_TREE;
6349   bool sub_strict_overflow_p;
6350
6351   /* Don't deal with constants of zero here; they confuse the code below.  */
6352   if (integer_zerop (c))
6353     return NULL_TREE;
6354
6355   if (TREE_CODE_CLASS (tcode) == tcc_unary)
6356     op0 = TREE_OPERAND (t, 0);
6357
6358   if (TREE_CODE_CLASS (tcode) == tcc_binary)
6359     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6360
6361   /* Note that we need not handle conditional operations here since fold
6362      already handles those cases.  So just do arithmetic here.  */
6363   switch (tcode)
6364     {
6365     case INTEGER_CST:
6366       /* For a constant, we can always simplify if we are a multiply
6367          or (for divide and modulus) if it is a multiple of our constant.  */
6368       if (code == MULT_EXPR
6369           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6370         return const_binop (code, fold_convert (ctype, t),
6371                             fold_convert (ctype, c), 0);
6372       break;
6373
6374     CASE_CONVERT: case NON_LVALUE_EXPR:
6375       /* If op0 is an expression ...  */
6376       if ((COMPARISON_CLASS_P (op0)
6377            || UNARY_CLASS_P (op0)
6378            || BINARY_CLASS_P (op0)
6379            || VL_EXP_CLASS_P (op0)
6380            || EXPRESSION_CLASS_P (op0))
6381           /* ... and has wrapping overflow, and its type is smaller
6382              than ctype, then we cannot pass through as widening.  */
6383           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6384                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6385                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6386                && (TYPE_PRECISION (ctype)
6387                    > TYPE_PRECISION (TREE_TYPE (op0))))
6388               /* ... or this is a truncation (t is narrower than op0),
6389                  then we cannot pass through this narrowing.  */
6390               || (TYPE_PRECISION (type)
6391                   < TYPE_PRECISION (TREE_TYPE (op0)))
6392               /* ... or signedness changes for division or modulus,
6393                  then we cannot pass through this conversion.  */
6394               || (code != MULT_EXPR
6395                   && (TYPE_UNSIGNED (ctype)
6396                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
6397               /* ... or has undefined overflow while the converted to
6398                  type has not, we cannot do the operation in the inner type
6399                  as that would introduce undefined overflow.  */
6400               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6401                   && !TYPE_OVERFLOW_UNDEFINED (type))))
6402         break;
6403
6404       /* Pass the constant down and see if we can make a simplification.  If
6405          we can, replace this expression with the inner simplification for
6406          possible later conversion to our or some other type.  */
6407       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6408           && TREE_CODE (t2) == INTEGER_CST
6409           && !TREE_OVERFLOW (t2)
6410           && (0 != (t1 = extract_muldiv (op0, t2, code,
6411                                          code == MULT_EXPR
6412                                          ? ctype : NULL_TREE,
6413                                          strict_overflow_p))))
6414         return t1;
6415       break;
6416
6417     case ABS_EXPR:
6418       /* If widening the type changes it from signed to unsigned, then we
6419          must avoid building ABS_EXPR itself as unsigned.  */
6420       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6421         {
6422           tree cstype = (*signed_type_for) (ctype);
6423           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6424               != 0)
6425             {
6426               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6427               return fold_convert (ctype, t1);
6428             }
6429           break;
6430         }
6431       /* If the constant is negative, we cannot simplify this.  */
6432       if (tree_int_cst_sgn (c) == -1)
6433         break;
6434       /* FALLTHROUGH */
6435     case NEGATE_EXPR:
6436       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6437           != 0)
6438         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6439       break;
6440
6441     case MIN_EXPR:  case MAX_EXPR:
6442       /* If widening the type changes the signedness, then we can't perform
6443          this optimization as that changes the result.  */
6444       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6445         break;
6446
6447       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
6448       sub_strict_overflow_p = false;
6449       if ((t1 = extract_muldiv (op0, c, code, wide_type,
6450                                 &sub_strict_overflow_p)) != 0
6451           && (t2 = extract_muldiv (op1, c, code, wide_type,
6452                                    &sub_strict_overflow_p)) != 0)
6453         {
6454           if (tree_int_cst_sgn (c) < 0)
6455             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6456           if (sub_strict_overflow_p)
6457             *strict_overflow_p = true;
6458           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6459                               fold_convert (ctype, t2));
6460         }
6461       break;
6462
6463     case LSHIFT_EXPR:  case RSHIFT_EXPR:
6464       /* If the second operand is constant, this is a multiplication
6465          or floor division, by a power of two, so we can treat it that
6466          way unless the multiplier or divisor overflows.  Signed
6467          left-shift overflow is implementation-defined rather than
6468          undefined in C90, so do not convert signed left shift into
6469          multiplication.  */
6470       if (TREE_CODE (op1) == INTEGER_CST
6471           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6472           /* const_binop may not detect overflow correctly,
6473              so check for it explicitly here.  */
6474           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6475           && TREE_INT_CST_HIGH (op1) == 0
6476           && 0 != (t1 = fold_convert (ctype,
6477                                       const_binop (LSHIFT_EXPR,
6478                                                    size_one_node,
6479                                                    op1, 0)))
6480           && !TREE_OVERFLOW (t1))
6481         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6482                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
6483                                        ctype,
6484                                        fold_convert (ctype, op0),
6485                                        t1),
6486                                c, code, wide_type, strict_overflow_p);
6487       break;
6488
6489     case PLUS_EXPR:  case MINUS_EXPR:
6490       /* See if we can eliminate the operation on both sides.  If we can, we
6491          can return a new PLUS or MINUS.  If we can't, the only remaining
6492          cases where we can do anything are if the second operand is a
6493          constant.  */
6494       sub_strict_overflow_p = false;
6495       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6496       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6497       if (t1 != 0 && t2 != 0
6498           && (code == MULT_EXPR
6499               /* If not multiplication, we can only do this if both operands
6500                  are divisible by c.  */
6501               || (multiple_of_p (ctype, op0, c)
6502                   && multiple_of_p (ctype, op1, c))))
6503         {
6504           if (sub_strict_overflow_p)
6505             *strict_overflow_p = true;
6506           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6507                               fold_convert (ctype, t2));
6508         }
6509
6510       /* If this was a subtraction, negate OP1 and set it to be an addition.
6511          This simplifies the logic below.  */
6512       if (tcode == MINUS_EXPR)
6513         {
6514           tcode = PLUS_EXPR, op1 = negate_expr (op1);
6515           /* If OP1 was not easily negatable, the constant may be OP0.  */
6516           if (TREE_CODE (op0) == INTEGER_CST)
6517             {
6518               tree tem = op0;
6519               op0 = op1;
6520               op1 = tem;
6521               tem = t1;
6522               t1 = t2;
6523               t2 = tem;
6524             }
6525         }
6526
6527       if (TREE_CODE (op1) != INTEGER_CST)
6528         break;
6529
6530       /* If either OP1 or C are negative, this optimization is not safe for
6531          some of the division and remainder types while for others we need
6532          to change the code.  */
6533       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6534         {
6535           if (code == CEIL_DIV_EXPR)
6536             code = FLOOR_DIV_EXPR;
6537           else if (code == FLOOR_DIV_EXPR)
6538             code = CEIL_DIV_EXPR;
6539           else if (code != MULT_EXPR
6540                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6541             break;
6542         }
6543
6544       /* If it's a multiply or a division/modulus operation of a multiple
6545          of our constant, do the operation and verify it doesn't overflow.  */
6546       if (code == MULT_EXPR
6547           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6548         {
6549           op1 = const_binop (code, fold_convert (ctype, op1),
6550                              fold_convert (ctype, c), 0);
6551           /* We allow the constant to overflow with wrapping semantics.  */
6552           if (op1 == 0
6553               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6554             break;
6555         }
6556       else
6557         break;
6558
6559       /* If we have an unsigned type is not a sizetype, we cannot widen
6560          the operation since it will change the result if the original
6561          computation overflowed.  */
6562       if (TYPE_UNSIGNED (ctype)
6563           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6564           && ctype != type)
6565         break;
6566
6567       /* If we were able to eliminate our operation from the first side,
6568          apply our operation to the second side and reform the PLUS.  */
6569       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6570         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6571
6572       /* The last case is if we are a multiply.  In that case, we can
6573          apply the distributive law to commute the multiply and addition
6574          if the multiplication of the constants doesn't overflow.  */
6575       if (code == MULT_EXPR)
6576         return fold_build2 (tcode, ctype,
6577                             fold_build2 (code, ctype,
6578                                          fold_convert (ctype, op0),
6579                                          fold_convert (ctype, c)),
6580                             op1);
6581
6582       break;
6583
6584     case MULT_EXPR:
6585       /* We have a special case here if we are doing something like
6586          (C * 8) % 4 since we know that's zero.  */
6587       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6588            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6589           /* If the multiplication can overflow we cannot optimize this.
6590              ???  Until we can properly mark individual operations as
6591              not overflowing we need to treat sizetype special here as
6592              stor-layout relies on this opimization to make
6593              DECL_FIELD_BIT_OFFSET always a constant.  */
6594           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6595               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6596                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
6597           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6598           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6599         {
6600           *strict_overflow_p = true;
6601           return omit_one_operand (type, integer_zero_node, op0);
6602         }
6603
6604       /* ... fall through ...  */
6605
6606     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
6607     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
6608       /* If we can extract our operation from the LHS, do so and return a
6609          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
6610          do something only if the second operand is a constant.  */
6611       if (same_p
6612           && (t1 = extract_muldiv (op0, c, code, wide_type,
6613                                    strict_overflow_p)) != 0)
6614         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6615                             fold_convert (ctype, op1));
6616       else if (tcode == MULT_EXPR && code == MULT_EXPR
6617                && (t1 = extract_muldiv (op1, c, code, wide_type,
6618                                         strict_overflow_p)) != 0)
6619         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6620                             fold_convert (ctype, t1));
6621       else if (TREE_CODE (op1) != INTEGER_CST)
6622         return 0;
6623
6624       /* If these are the same operation types, we can associate them
6625          assuming no overflow.  */
6626       if (tcode == code
6627           && 0 != (t1 = int_const_binop (MULT_EXPR,
6628                                          fold_convert (ctype, op1),
6629                                          fold_convert (ctype, c), 1))
6630           && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
6631                                                TREE_INT_CST_HIGH (t1),
6632                                                (TYPE_UNSIGNED (ctype)
6633                                                 && tcode != MULT_EXPR) ? -1 : 1,
6634                                                TREE_OVERFLOW (t1)))
6635           && !TREE_OVERFLOW (t1))
6636         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6637
6638       /* If these operations "cancel" each other, we have the main
6639          optimizations of this pass, which occur when either constant is a
6640          multiple of the other, in which case we replace this with either an
6641          operation or CODE or TCODE.
6642
6643          If we have an unsigned type that is not a sizetype, we cannot do
6644          this since it will change the result if the original computation
6645          overflowed.  */
6646       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6647            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6648           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6649               || (tcode == MULT_EXPR
6650                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6651                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6652                   && code != MULT_EXPR)))
6653         {
6654           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6655             {
6656               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6657                 *strict_overflow_p = true;
6658               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6659                                   fold_convert (ctype,
6660                                                 const_binop (TRUNC_DIV_EXPR,
6661                                                              op1, c, 0)));
6662             }
6663           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6664             {
6665               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6666                 *strict_overflow_p = true;
6667               return fold_build2 (code, ctype, fold_convert (ctype, op0),
6668                                   fold_convert (ctype,
6669                                                 const_binop (TRUNC_DIV_EXPR,
6670                                                              c, op1, 0)));
6671             }
6672         }
6673       break;
6674
6675     default:
6676       break;
6677     }
6678
6679   return 0;
6680 }
6681 \f
6682 /* Return a node which has the indicated constant VALUE (either 0 or
6683    1), and is of the indicated TYPE.  */
6684
6685 tree
6686 constant_boolean_node (int value, tree type)
6687 {
6688   if (type == integer_type_node)
6689     return value ? integer_one_node : integer_zero_node;
6690   else if (type == boolean_type_node)
6691     return value ? boolean_true_node : boolean_false_node;
6692   else
6693     return build_int_cst (type, value);
6694 }
6695
6696
6697 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6698    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6699    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6700    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6701    COND is the first argument to CODE; otherwise (as in the example
6702    given here), it is the second argument.  TYPE is the type of the
6703    original expression.  Return NULL_TREE if no simplification is
6704    possible.  */
6705
6706 static tree
6707 fold_binary_op_with_conditional_arg (location_t loc,
6708                                      enum tree_code code,
6709                                      tree type, tree op0, tree op1,
6710                                      tree cond, tree arg, int cond_first_p)
6711 {
6712   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6713   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6714   tree test, true_value, false_value;
6715   tree lhs = NULL_TREE;
6716   tree rhs = NULL_TREE;
6717
6718   /* This transformation is only worthwhile if we don't have to wrap
6719      arg in a SAVE_EXPR, and the operation can be simplified on at least
6720      one of the branches once its pushed inside the COND_EXPR.  */
6721   if (!TREE_CONSTANT (arg))
6722     return NULL_TREE;
6723
6724   if (TREE_CODE (cond) == COND_EXPR)
6725     {
6726       test = TREE_OPERAND (cond, 0);
6727       true_value = TREE_OPERAND (cond, 1);
6728       false_value = TREE_OPERAND (cond, 2);
6729       /* If this operand throws an expression, then it does not make
6730          sense to try to perform a logical or arithmetic operation
6731          involving it.  */
6732       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6733         lhs = true_value;
6734       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6735         rhs = false_value;
6736     }
6737   else
6738     {
6739       tree testtype = TREE_TYPE (cond);
6740       test = cond;
6741       true_value = constant_boolean_node (true, testtype);
6742       false_value = constant_boolean_node (false, testtype);
6743     }
6744
6745   arg = fold_convert_loc (loc, arg_type, arg);
6746   if (lhs == 0)
6747     {
6748       true_value = fold_convert_loc (loc, cond_type, true_value);
6749       if (cond_first_p)
6750         lhs = fold_build2_loc (loc, code, type, true_value, arg);
6751       else
6752         lhs = fold_build2_loc (loc, code, type, arg, true_value);
6753     }
6754   if (rhs == 0)
6755     {
6756       false_value = fold_convert_loc (loc, cond_type, false_value);
6757       if (cond_first_p)
6758         rhs = fold_build2_loc (loc, code, type, false_value, arg);
6759       else
6760         rhs = fold_build2_loc (loc, code, type, arg, false_value);
6761     }
6762
6763   test = fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6764   return fold_convert_loc (loc, type, test);
6765 }
6766
6767 \f
6768 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6769
6770    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6771    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6772    ADDEND is the same as X.
6773
6774    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6775    and finite.  The problematic cases are when X is zero, and its mode
6776    has signed zeros.  In the case of rounding towards -infinity,
6777    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6778    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6779
6780 bool
6781 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6782 {
6783   if (!real_zerop (addend))
6784     return false;
6785
6786   /* Don't allow the fold with -fsignaling-nans.  */
6787   if (HONOR_SNANS (TYPE_MODE (type)))
6788     return false;
6789
6790   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6791   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6792     return true;
6793
6794   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6795   if (TREE_CODE (addend) == REAL_CST
6796       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6797     negate = !negate;
6798
6799   /* The mode has signed zeros, and we have to honor their sign.
6800      In this situation, there is only one case we can return true for.
6801      X - 0 is the same as X unless rounding towards -infinity is
6802      supported.  */
6803   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6804 }
6805
6806 /* Subroutine of fold() that checks comparisons of built-in math
6807    functions against real constants.
6808
6809    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6810    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6811    is the type of the result and ARG0 and ARG1 are the operands of the
6812    comparison.  ARG1 must be a TREE_REAL_CST.
6813
6814    The function returns the constant folded tree if a simplification
6815    can be made, and NULL_TREE otherwise.  */
6816
6817 static tree
6818 fold_mathfn_compare (location_t loc,
6819                      enum built_in_function fcode, enum tree_code code,
6820                      tree type, tree arg0, tree arg1)
6821 {
6822   REAL_VALUE_TYPE c;
6823
6824   if (BUILTIN_SQRT_P (fcode))
6825     {
6826       tree arg = CALL_EXPR_ARG (arg0, 0);
6827       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6828
6829       c = TREE_REAL_CST (arg1);
6830       if (REAL_VALUE_NEGATIVE (c))
6831         {
6832           /* sqrt(x) < y is always false, if y is negative.  */
6833           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6834             return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6835
6836           /* sqrt(x) > y is always true, if y is negative and we
6837              don't care about NaNs, i.e. negative values of x.  */
6838           if (code == NE_EXPR || !HONOR_NANS (mode))
6839             return omit_one_operand_loc (loc, type, integer_one_node, arg);
6840
6841           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6842           return fold_build2_loc (loc, GE_EXPR, type, arg,
6843                               build_real (TREE_TYPE (arg), dconst0));
6844         }
6845       else if (code == GT_EXPR || code == GE_EXPR)
6846         {
6847           REAL_VALUE_TYPE c2;
6848
6849           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6850           real_convert (&c2, mode, &c2);
6851
6852           if (REAL_VALUE_ISINF (c2))
6853             {
6854               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6855               if (HONOR_INFINITIES (mode))
6856                 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6857                                     build_real (TREE_TYPE (arg), c2));
6858
6859               /* sqrt(x) > y is always false, when y is very large
6860                  and we don't care about infinities.  */
6861               return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6862             }
6863
6864           /* sqrt(x) > c is the same as x > c*c.  */
6865           return fold_build2_loc (loc, code, type, arg,
6866                               build_real (TREE_TYPE (arg), c2));
6867         }
6868       else if (code == LT_EXPR || code == LE_EXPR)
6869         {
6870           REAL_VALUE_TYPE c2;
6871
6872           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6873           real_convert (&c2, mode, &c2);
6874
6875           if (REAL_VALUE_ISINF (c2))
6876             {
6877               /* sqrt(x) < y is always true, when y is a very large
6878                  value and we don't care about NaNs or Infinities.  */
6879               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6880                 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6881
6882               /* sqrt(x) < y is x != +Inf when y is very large and we
6883                  don't care about NaNs.  */
6884               if (! HONOR_NANS (mode))
6885                 return fold_build2_loc (loc, NE_EXPR, type, arg,
6886                                     build_real (TREE_TYPE (arg), c2));
6887
6888               /* sqrt(x) < y is x >= 0 when y is very large and we
6889                  don't care about Infinities.  */
6890               if (! HONOR_INFINITIES (mode))
6891                 return fold_build2_loc (loc, GE_EXPR, type, arg,
6892                                     build_real (TREE_TYPE (arg), dconst0));
6893
6894               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6895               if (lang_hooks.decls.global_bindings_p () != 0
6896                   || CONTAINS_PLACEHOLDER_P (arg))
6897                 return NULL_TREE;
6898
6899               arg = save_expr (arg);
6900               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6901                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6902                                                build_real (TREE_TYPE (arg),
6903                                                            dconst0)),
6904                                   fold_build2_loc (loc, NE_EXPR, type, arg,
6905                                                build_real (TREE_TYPE (arg),
6906                                                            c2)));
6907             }
6908
6909           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6910           if (! HONOR_NANS (mode))
6911             return fold_build2_loc (loc, code, type, arg,
6912                                 build_real (TREE_TYPE (arg), c2));
6913
6914           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6915           if (lang_hooks.decls.global_bindings_p () == 0
6916               && ! CONTAINS_PLACEHOLDER_P (arg))
6917             {
6918               arg = save_expr (arg);
6919               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6920                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6921                                                build_real (TREE_TYPE (arg),
6922                                                            dconst0)),
6923                                   fold_build2_loc (loc, code, type, arg,
6924                                                build_real (TREE_TYPE (arg),
6925                                                            c2)));
6926             }
6927         }
6928     }
6929
6930   return NULL_TREE;
6931 }
6932
6933 /* Subroutine of fold() that optimizes comparisons against Infinities,
6934    either +Inf or -Inf.
6935
6936    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6937    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6938    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6939
6940    The function returns the constant folded tree if a simplification
6941    can be made, and NULL_TREE otherwise.  */
6942
6943 static tree
6944 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6945                   tree arg0, tree arg1)
6946 {
6947   enum machine_mode mode;
6948   REAL_VALUE_TYPE max;
6949   tree temp;
6950   bool neg;
6951
6952   mode = TYPE_MODE (TREE_TYPE (arg0));
6953
6954   /* For negative infinity swap the sense of the comparison.  */
6955   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6956   if (neg)
6957     code = swap_tree_comparison (code);
6958
6959   switch (code)
6960     {
6961     case GT_EXPR:
6962       /* x > +Inf is always false, if with ignore sNANs.  */
6963       if (HONOR_SNANS (mode))
6964         return NULL_TREE;
6965       return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6966
6967     case LE_EXPR:
6968       /* x <= +Inf is always true, if we don't case about NaNs.  */
6969       if (! HONOR_NANS (mode))
6970         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6971
6972       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6973       if (lang_hooks.decls.global_bindings_p () == 0
6974           && ! CONTAINS_PLACEHOLDER_P (arg0))
6975         {
6976           arg0 = save_expr (arg0);
6977           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6978         }
6979       break;
6980
6981     case EQ_EXPR:
6982     case GE_EXPR:
6983       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6984       real_maxval (&max, neg, mode);
6985       return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6986                           arg0, build_real (TREE_TYPE (arg0), max));
6987
6988     case LT_EXPR:
6989       /* x < +Inf is always equal to x <= DBL_MAX.  */
6990       real_maxval (&max, neg, mode);
6991       return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6992                           arg0, build_real (TREE_TYPE (arg0), max));
6993
6994     case NE_EXPR:
6995       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6996       real_maxval (&max, neg, mode);
6997       if (! HONOR_NANS (mode))
6998         return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6999                             arg0, build_real (TREE_TYPE (arg0), max));
7000
7001       temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
7002                           arg0, build_real (TREE_TYPE (arg0), max));
7003       return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
7004
7005     default:
7006       break;
7007     }
7008
7009   return NULL_TREE;
7010 }
7011
7012 /* Subroutine of fold() that optimizes comparisons of a division by
7013    a nonzero integer constant against an integer constant, i.e.
7014    X/C1 op C2.
7015
7016    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7017    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
7018    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
7019
7020    The function returns the constant folded tree if a simplification
7021    can be made, and NULL_TREE otherwise.  */
7022
7023 static tree
7024 fold_div_compare (location_t loc,
7025                   enum tree_code code, tree type, tree arg0, tree arg1)
7026 {
7027   tree prod, tmp, hi, lo;
7028   tree arg00 = TREE_OPERAND (arg0, 0);
7029   tree arg01 = TREE_OPERAND (arg0, 1);
7030   unsigned HOST_WIDE_INT lpart;
7031   HOST_WIDE_INT hpart;
7032   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
7033   bool neg_overflow;
7034   int overflow;
7035
7036   /* We have to do this the hard way to detect unsigned overflow.
7037      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
7038   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
7039                                    TREE_INT_CST_HIGH (arg01),
7040                                    TREE_INT_CST_LOW (arg1),
7041                                    TREE_INT_CST_HIGH (arg1),
7042                                    &lpart, &hpart, unsigned_p);
7043   prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
7044                                 -1, overflow);
7045   neg_overflow = false;
7046
7047   if (unsigned_p)
7048     {
7049       tmp = int_const_binop (MINUS_EXPR, arg01,
7050                              build_int_cst (TREE_TYPE (arg01), 1), 0);
7051       lo = prod;
7052
7053       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
7054       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
7055                                        TREE_INT_CST_HIGH (prod),
7056                                        TREE_INT_CST_LOW (tmp),
7057                                        TREE_INT_CST_HIGH (tmp),
7058                                        &lpart, &hpart, unsigned_p);
7059       hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
7060                                   -1, overflow | TREE_OVERFLOW (prod));
7061     }
7062   else if (tree_int_cst_sgn (arg01) >= 0)
7063     {
7064       tmp = int_const_binop (MINUS_EXPR, arg01,
7065                              build_int_cst (TREE_TYPE (arg01), 1), 0);
7066       switch (tree_int_cst_sgn (arg1))
7067         {
7068         case -1:
7069           neg_overflow = true;
7070           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
7071           hi = prod;
7072           break;
7073
7074         case  0:
7075           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
7076           hi = tmp;
7077           break;
7078
7079         case  1:
7080           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
7081           lo = prod;
7082           break;
7083
7084         default:
7085           gcc_unreachable ();
7086         }
7087     }
7088   else
7089     {
7090       /* A negative divisor reverses the relational operators.  */
7091       code = swap_tree_comparison (code);
7092
7093       tmp = int_const_binop (PLUS_EXPR, arg01,
7094                              build_int_cst (TREE_TYPE (arg01), 1), 0);
7095       switch (tree_int_cst_sgn (arg1))
7096         {
7097         case -1:
7098           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
7099           lo = prod;
7100           break;
7101
7102         case  0:
7103           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
7104           lo = tmp;
7105           break;
7106
7107         case  1:
7108           neg_overflow = true;
7109           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
7110           hi = prod;
7111           break;
7112
7113         default:
7114           gcc_unreachable ();
7115         }
7116     }
7117
7118   switch (code)
7119     {
7120     case EQ_EXPR:
7121       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
7122         return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
7123       if (TREE_OVERFLOW (hi))
7124         return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
7125       if (TREE_OVERFLOW (lo))
7126         return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
7127       return build_range_check (loc, type, arg00, 1, lo, hi);
7128
7129     case NE_EXPR:
7130       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
7131         return omit_one_operand_loc (loc, type, integer_one_node, arg00);
7132       if (TREE_OVERFLOW (hi))
7133         return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
7134       if (TREE_OVERFLOW (lo))
7135         return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
7136       return build_range_check (loc, type, arg00, 0, lo, hi);
7137
7138     case LT_EXPR:
7139       if (TREE_OVERFLOW (lo))
7140         {
7141           tmp = neg_overflow ? integer_zero_node : integer_one_node;
7142           return omit_one_operand_loc (loc, type, tmp, arg00);
7143         }
7144       return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
7145
7146     case LE_EXPR:
7147       if (TREE_OVERFLOW (hi))
7148         {
7149           tmp = neg_overflow ? integer_zero_node : integer_one_node;
7150           return omit_one_operand_loc (loc, type, tmp, arg00);
7151         }
7152       return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
7153
7154     case GT_EXPR:
7155       if (TREE_OVERFLOW (hi))
7156         {
7157           tmp = neg_overflow ? integer_one_node : integer_zero_node;
7158           return omit_one_operand_loc (loc, type, tmp, arg00);
7159         }
7160       return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
7161
7162     case GE_EXPR:
7163       if (TREE_OVERFLOW (lo))
7164         {
7165           tmp = neg_overflow ? integer_one_node : integer_zero_node;
7166           return omit_one_operand_loc (loc, type, tmp, arg00);
7167         }
7168       return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
7169
7170     default:
7171       break;
7172     }
7173
7174   return NULL_TREE;
7175 }
7176
7177
7178 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7179    equality/inequality test, then return a simplified form of the test
7180    using a sign testing.  Otherwise return NULL.  TYPE is the desired
7181    result type.  */
7182
7183 static tree
7184 fold_single_bit_test_into_sign_test (location_t loc,
7185                                      enum tree_code code, tree arg0, tree arg1,
7186                                      tree result_type)
7187 {
7188   /* If this is testing a single bit, we can optimize the test.  */
7189   if ((code == NE_EXPR || code == EQ_EXPR)
7190       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
7191       && integer_pow2p (TREE_OPERAND (arg0, 1)))
7192     {
7193       /* If we have (A & C) != 0 where C is the sign bit of A, convert
7194          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
7195       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
7196
7197       if (arg00 != NULL_TREE
7198           /* This is only a win if casting to a signed type is cheap,
7199              i.e. when arg00's type is not a partial mode.  */
7200           && TYPE_PRECISION (TREE_TYPE (arg00))
7201              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
7202         {
7203           tree stype = signed_type_for (TREE_TYPE (arg00));
7204           return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
7205                               result_type,
7206                               fold_convert_loc (loc, stype, arg00),
7207                               build_int_cst (stype, 0));
7208         }
7209     }
7210
7211   return NULL_TREE;
7212 }
7213
7214 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7215    equality/inequality test, then return a simplified form of
7216    the test using shifts and logical operations.  Otherwise return
7217    NULL.  TYPE is the desired result type.  */
7218
7219 tree
7220 fold_single_bit_test (location_t loc, enum tree_code code,
7221                       tree arg0, tree arg1, tree result_type)
7222 {
7223   /* If this is testing a single bit, we can optimize the test.  */
7224   if ((code == NE_EXPR || code == EQ_EXPR)
7225       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
7226       && integer_pow2p (TREE_OPERAND (arg0, 1)))
7227     {
7228       tree inner = TREE_OPERAND (arg0, 0);
7229       tree type = TREE_TYPE (arg0);
7230       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
7231       enum machine_mode operand_mode = TYPE_MODE (type);
7232       int ops_unsigned;
7233       tree signed_type, unsigned_type, intermediate_type;
7234       tree tem, one;
7235
7236       /* First, see if we can fold the single bit test into a sign-bit
7237          test.  */
7238       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
7239                                                  result_type);
7240       if (tem)
7241         return tem;
7242
7243       /* Otherwise we have (A & C) != 0 where C is a single bit,
7244          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
7245          Similarly for (A & C) == 0.  */
7246
7247       /* If INNER is a right shift of a constant and it plus BITNUM does
7248          not overflow, adjust BITNUM and INNER.  */
7249       if (TREE_CODE (inner) == RSHIFT_EXPR
7250           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
7251           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
7252           && bitnum < TYPE_PRECISION (type)
7253           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
7254                                    bitnum - TYPE_PRECISION (type)))
7255         {
7256           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
7257           inner = TREE_OPERAND (inner, 0);
7258         }
7259
7260       /* If we are going to be able to omit the AND below, we must do our
7261          operations as unsigned.  If we must use the AND, we have a choice.
7262          Normally unsigned is faster, but for some machines signed is.  */
7263 #ifdef LOAD_EXTEND_OP
7264       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND 
7265                       && !flag_syntax_only) ? 0 : 1;
7266 #else
7267       ops_unsigned = 1;
7268 #endif
7269
7270       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
7271       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
7272       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
7273       inner = fold_convert_loc (loc, intermediate_type, inner);
7274
7275       if (bitnum != 0)
7276         inner = build2 (RSHIFT_EXPR, intermediate_type,
7277                         inner, size_int (bitnum));
7278
7279       one = build_int_cst (intermediate_type, 1);
7280
7281       if (code == EQ_EXPR)
7282         inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
7283
7284       /* Put the AND last so it can combine with more things.  */
7285       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
7286
7287       /* Make sure to return the proper type.  */
7288       inner = fold_convert_loc (loc, result_type, inner);
7289
7290       return inner;
7291     }
7292   return NULL_TREE;
7293 }
7294
7295 /* Check whether we are allowed to reorder operands arg0 and arg1,
7296    such that the evaluation of arg1 occurs before arg0.  */
7297
7298 static bool
7299 reorder_operands_p (const_tree arg0, const_tree arg1)
7300 {
7301   if (! flag_evaluation_order)
7302       return true;
7303   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
7304     return true;
7305   return ! TREE_SIDE_EFFECTS (arg0)
7306          && ! TREE_SIDE_EFFECTS (arg1);
7307 }
7308
7309 /* Test whether it is preferable two swap two operands, ARG0 and
7310    ARG1, for example because ARG0 is an integer constant and ARG1
7311    isn't.  If REORDER is true, only recommend swapping if we can
7312    evaluate the operands in reverse order.  */
7313
7314 bool
7315 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
7316 {
7317   STRIP_SIGN_NOPS (arg0);
7318   STRIP_SIGN_NOPS (arg1);
7319
7320   if (TREE_CODE (arg1) == INTEGER_CST)
7321     return 0;
7322   if (TREE_CODE (arg0) == INTEGER_CST)
7323     return 1;
7324
7325   if (TREE_CODE (arg1) == REAL_CST)
7326     return 0;
7327   if (TREE_CODE (arg0) == REAL_CST)
7328     return 1;
7329
7330   if (TREE_CODE (arg1) == FIXED_CST)
7331     return 0;
7332   if (TREE_CODE (arg0) == FIXED_CST)
7333     return 1;
7334
7335   if (TREE_CODE (arg1) == COMPLEX_CST)
7336     return 0;
7337   if (TREE_CODE (arg0) == COMPLEX_CST)
7338     return 1;
7339
7340   if (TREE_CONSTANT (arg1))
7341     return 0;
7342   if (TREE_CONSTANT (arg0))
7343     return 1;
7344
7345   if (optimize_function_for_size_p (cfun))
7346     return 0;
7347
7348   if (reorder && flag_evaluation_order
7349       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7350     return 0;
7351
7352   /* It is preferable to swap two SSA_NAME to ensure a canonical form
7353      for commutative and comparison operators.  Ensuring a canonical
7354      form allows the optimizers to find additional redundancies without
7355      having to explicitly check for both orderings.  */
7356   if (TREE_CODE (arg0) == SSA_NAME
7357       && TREE_CODE (arg1) == SSA_NAME
7358       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7359     return 1;
7360
7361   /* Put SSA_NAMEs last.  */
7362   if (TREE_CODE (arg1) == SSA_NAME)
7363     return 0;
7364   if (TREE_CODE (arg0) == SSA_NAME)
7365     return 1;
7366
7367   /* Put variables last.  */
7368   if (DECL_P (arg1))
7369     return 0;
7370   if (DECL_P (arg0))
7371     return 1;
7372
7373   return 0;
7374 }
7375
7376 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7377    ARG0 is extended to a wider type.  */
7378
7379 static tree
7380 fold_widened_comparison (location_t loc, enum tree_code code,
7381                          tree type, tree arg0, tree arg1)
7382 {
7383   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7384   tree arg1_unw;
7385   tree shorter_type, outer_type;
7386   tree min, max;
7387   bool above, below;
7388
7389   if (arg0_unw == arg0)
7390     return NULL_TREE;
7391   shorter_type = TREE_TYPE (arg0_unw);
7392
7393 #ifdef HAVE_canonicalize_funcptr_for_compare
7394   /* Disable this optimization if we're casting a function pointer
7395      type on targets that require function pointer canonicalization.  */
7396   if (HAVE_canonicalize_funcptr_for_compare
7397       && TREE_CODE (shorter_type) == POINTER_TYPE
7398       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7399     return NULL_TREE;
7400 #endif
7401
7402   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7403     return NULL_TREE;
7404
7405   arg1_unw = get_unwidened (arg1, NULL_TREE);
7406
7407   /* If possible, express the comparison in the shorter mode.  */
7408   if ((code == EQ_EXPR || code == NE_EXPR
7409        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7410       && (TREE_TYPE (arg1_unw) == shorter_type
7411           || ((TYPE_PRECISION (shorter_type)
7412                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
7413               && (TYPE_UNSIGNED (shorter_type)
7414                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
7415           || (TREE_CODE (arg1_unw) == INTEGER_CST
7416               && (TREE_CODE (shorter_type) == INTEGER_TYPE
7417                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7418               && int_fits_type_p (arg1_unw, shorter_type))))
7419     return fold_build2_loc (loc, code, type, arg0_unw,
7420                         fold_convert_loc (loc, shorter_type, arg1_unw));
7421
7422   if (TREE_CODE (arg1_unw) != INTEGER_CST
7423       || TREE_CODE (shorter_type) != INTEGER_TYPE
7424       || !int_fits_type_p (arg1_unw, shorter_type))
7425     return NULL_TREE;
7426
7427   /* If we are comparing with the integer that does not fit into the range
7428      of the shorter type, the result is known.  */
7429   outer_type = TREE_TYPE (arg1_unw);
7430   min = lower_bound_in_type (outer_type, shorter_type);
7431   max = upper_bound_in_type (outer_type, shorter_type);
7432
7433   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7434                                                    max, arg1_unw));
7435   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7436                                                    arg1_unw, min));
7437
7438   switch (code)
7439     {
7440     case EQ_EXPR:
7441       if (above || below)
7442         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7443       break;
7444
7445     case NE_EXPR:
7446       if (above || below)
7447         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7448       break;
7449
7450     case LT_EXPR:
7451     case LE_EXPR:
7452       if (above)
7453         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7454       else if (below)
7455         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7456
7457     case GT_EXPR:
7458     case GE_EXPR:
7459       if (above)
7460         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7461       else if (below)
7462         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7463
7464     default:
7465       break;
7466     }
7467
7468   return NULL_TREE;
7469 }
7470
7471 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7472    ARG0 just the signedness is changed.  */
7473
7474 static tree
7475 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
7476                               tree arg0, tree arg1)
7477 {
7478   tree arg0_inner;
7479   tree inner_type, outer_type;
7480
7481   if (!CONVERT_EXPR_P (arg0))
7482     return NULL_TREE;
7483
7484   outer_type = TREE_TYPE (arg0);
7485   arg0_inner = TREE_OPERAND (arg0, 0);
7486   inner_type = TREE_TYPE (arg0_inner);
7487
7488 #ifdef HAVE_canonicalize_funcptr_for_compare
7489   /* Disable this optimization if we're casting a function pointer
7490      type on targets that require function pointer canonicalization.  */
7491   if (HAVE_canonicalize_funcptr_for_compare
7492       && TREE_CODE (inner_type) == POINTER_TYPE
7493       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7494     return NULL_TREE;
7495 #endif
7496
7497   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7498     return NULL_TREE;
7499
7500   if (TREE_CODE (arg1) != INTEGER_CST
7501       && !(CONVERT_EXPR_P (arg1)
7502            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7503     return NULL_TREE;
7504
7505   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7506        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7507       && code != NE_EXPR
7508       && code != EQ_EXPR)
7509     return NULL_TREE;
7510
7511   if (TREE_CODE (arg1) == INTEGER_CST)
7512     arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7513                                   TREE_INT_CST_HIGH (arg1), 0,
7514                                   TREE_OVERFLOW (arg1));
7515   else
7516     arg1 = fold_convert_loc (loc, inner_type, arg1);
7517
7518   return fold_build2_loc (loc, code, type, arg0_inner, arg1);
7519 }
7520
7521 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7522    step of the array.  Reconstructs s and delta in the case of s *
7523    delta being an integer constant (and thus already folded).  ADDR is
7524    the address. MULT is the multiplicative expression.  If the
7525    function succeeds, the new address expression is returned.
7526    Otherwise NULL_TREE is returned.  LOC is the location of the
7527    resulting expression.  */
7528
7529 static tree
7530 try_move_mult_to_index (location_t loc, tree addr, tree op1)
7531 {
7532   tree s, delta, step;
7533   tree ref = TREE_OPERAND (addr, 0), pref;
7534   tree ret, pos;
7535   tree itype;
7536   bool mdim = false;
7537
7538   /*  Strip the nops that might be added when converting op1 to sizetype. */
7539   STRIP_NOPS (op1);
7540
7541   /* Canonicalize op1 into a possibly non-constant delta
7542      and an INTEGER_CST s.  */
7543   if (TREE_CODE (op1) == MULT_EXPR)
7544     {
7545       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7546
7547       STRIP_NOPS (arg0);
7548       STRIP_NOPS (arg1);
7549   
7550       if (TREE_CODE (arg0) == INTEGER_CST)
7551         {
7552           s = arg0;
7553           delta = arg1;
7554         }
7555       else if (TREE_CODE (arg1) == INTEGER_CST)
7556         {
7557           s = arg1;
7558           delta = arg0;
7559         }
7560       else
7561         return NULL_TREE;
7562     }
7563   else if (TREE_CODE (op1) == INTEGER_CST)
7564     {
7565       delta = op1;
7566       s = NULL_TREE;
7567     }
7568   else
7569     {
7570       /* Simulate we are delta * 1.  */
7571       delta = op1;
7572       s = integer_one_node;
7573     }
7574
7575   for (;; ref = TREE_OPERAND (ref, 0))
7576     {
7577       if (TREE_CODE (ref) == ARRAY_REF)
7578         {
7579           /* Remember if this was a multi-dimensional array.  */
7580           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7581             mdim = true;
7582
7583           itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7584           if (! itype)
7585             continue;
7586
7587           step = array_ref_element_size (ref);
7588           if (TREE_CODE (step) != INTEGER_CST)
7589             continue;
7590
7591           if (s)
7592             {
7593               if (! tree_int_cst_equal (step, s))
7594                 continue;
7595             }
7596           else
7597             {
7598               /* Try if delta is a multiple of step.  */
7599               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
7600               if (! tmp)
7601                 continue;
7602               delta = tmp;
7603             }
7604
7605           /* Only fold here if we can verify we do not overflow one
7606              dimension of a multi-dimensional array.  */
7607           if (mdim)
7608             {
7609               tree tmp;
7610
7611               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7612                   || !INTEGRAL_TYPE_P (itype)
7613                   || !TYPE_MAX_VALUE (itype)
7614                   || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
7615                 continue;
7616
7617               tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
7618                                  fold_convert_loc (loc, itype,
7619                                                    TREE_OPERAND (ref, 1)),
7620                                  fold_convert_loc (loc, itype, delta));
7621               if (!tmp
7622                   || TREE_CODE (tmp) != INTEGER_CST
7623                   || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
7624                 continue;
7625             }
7626
7627           break;
7628         }
7629       else
7630         mdim = false;
7631
7632       if (!handled_component_p (ref))
7633         return NULL_TREE;
7634     }
7635
7636   /* We found the suitable array reference.  So copy everything up to it,
7637      and replace the index.  */
7638
7639   pref = TREE_OPERAND (addr, 0);
7640   ret = copy_node (pref);
7641   SET_EXPR_LOCATION (ret, loc);
7642   pos = ret;
7643
7644   while (pref != ref)
7645     {
7646       pref = TREE_OPERAND (pref, 0);
7647       TREE_OPERAND (pos, 0) = copy_node (pref);
7648       pos = TREE_OPERAND (pos, 0);
7649     }
7650
7651   TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype,
7652                                        fold_convert_loc (loc, itype,
7653                                                          TREE_OPERAND (pos, 1)),
7654                                        fold_convert_loc (loc, itype, delta));
7655
7656   return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
7657 }
7658
7659
7660 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
7661    means A >= Y && A != MAX, but in this case we know that
7662    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
7663
7664 static tree
7665 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7666 {
7667   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7668
7669   if (TREE_CODE (bound) == LT_EXPR)
7670     a = TREE_OPERAND (bound, 0);
7671   else if (TREE_CODE (bound) == GT_EXPR)
7672     a = TREE_OPERAND (bound, 1);
7673   else
7674     return NULL_TREE;
7675
7676   typea = TREE_TYPE (a);
7677   if (!INTEGRAL_TYPE_P (typea)
7678       && !POINTER_TYPE_P (typea))
7679     return NULL_TREE;
7680
7681   if (TREE_CODE (ineq) == LT_EXPR)
7682     {
7683       a1 = TREE_OPERAND (ineq, 1);
7684       y = TREE_OPERAND (ineq, 0);
7685     }
7686   else if (TREE_CODE (ineq) == GT_EXPR)
7687     {
7688       a1 = TREE_OPERAND (ineq, 0);
7689       y = TREE_OPERAND (ineq, 1);
7690     }
7691   else
7692     return NULL_TREE;
7693
7694   if (TREE_TYPE (a1) != typea)
7695     return NULL_TREE;
7696
7697   if (POINTER_TYPE_P (typea))
7698     {
7699       /* Convert the pointer types into integer before taking the difference.  */
7700       tree ta = fold_convert_loc (loc, ssizetype, a);
7701       tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7702       diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7703     }
7704   else
7705     diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7706
7707   if (!diff || !integer_onep (diff))
7708    return NULL_TREE;
7709
7710   return fold_build2_loc (loc, GE_EXPR, type, a, y);
7711 }
7712
7713 /* Fold a sum or difference of at least one multiplication.
7714    Returns the folded tree or NULL if no simplification could be made.  */
7715
7716 static tree
7717 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7718                           tree arg0, tree arg1)
7719 {
7720   tree arg00, arg01, arg10, arg11;
7721   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7722
7723   /* (A * C) +- (B * C) -> (A+-B) * C.
7724      (A * C) +- A -> A * (C+-1).
7725      We are most concerned about the case where C is a constant,
7726      but other combinations show up during loop reduction.  Since
7727      it is not difficult, try all four possibilities.  */
7728
7729   if (TREE_CODE (arg0) == MULT_EXPR)
7730     {
7731       arg00 = TREE_OPERAND (arg0, 0);
7732       arg01 = TREE_OPERAND (arg0, 1);
7733     }
7734   else if (TREE_CODE (arg0) == INTEGER_CST)
7735     {
7736       arg00 = build_one_cst (type);
7737       arg01 = arg0;
7738     }
7739   else
7740     {
7741       /* We cannot generate constant 1 for fract.  */
7742       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7743         return NULL_TREE;
7744       arg00 = arg0;
7745       arg01 = build_one_cst (type);
7746     }
7747   if (TREE_CODE (arg1) == MULT_EXPR)
7748     {
7749       arg10 = TREE_OPERAND (arg1, 0);
7750       arg11 = TREE_OPERAND (arg1, 1);
7751     }
7752   else if (TREE_CODE (arg1) == INTEGER_CST)
7753     {
7754       arg10 = build_one_cst (type);
7755       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7756          the purpose of this canonicalization.  */
7757       if (TREE_INT_CST_HIGH (arg1) == -1
7758           && negate_expr_p (arg1)
7759           && code == PLUS_EXPR)
7760         {
7761           arg11 = negate_expr (arg1);
7762           code = MINUS_EXPR;
7763         }
7764       else
7765         arg11 = arg1;
7766     }
7767   else
7768     {
7769       /* We cannot generate constant 1 for fract.  */
7770       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7771         return NULL_TREE;
7772       arg10 = arg1;
7773       arg11 = build_one_cst (type);
7774     }
7775   same = NULL_TREE;
7776
7777   if (operand_equal_p (arg01, arg11, 0))
7778     same = arg01, alt0 = arg00, alt1 = arg10;
7779   else if (operand_equal_p (arg00, arg10, 0))
7780     same = arg00, alt0 = arg01, alt1 = arg11;
7781   else if (operand_equal_p (arg00, arg11, 0))
7782     same = arg00, alt0 = arg01, alt1 = arg10;
7783   else if (operand_equal_p (arg01, arg10, 0))
7784     same = arg01, alt0 = arg00, alt1 = arg11;
7785
7786   /* No identical multiplicands; see if we can find a common
7787      power-of-two factor in non-power-of-two multiplies.  This
7788      can help in multi-dimensional array access.  */
7789   else if (host_integerp (arg01, 0)
7790            && host_integerp (arg11, 0))
7791     {
7792       HOST_WIDE_INT int01, int11, tmp;
7793       bool swap = false;
7794       tree maybe_same;
7795       int01 = TREE_INT_CST_LOW (arg01);
7796       int11 = TREE_INT_CST_LOW (arg11);
7797
7798       /* Move min of absolute values to int11.  */
7799       if ((int01 >= 0 ? int01 : -int01)
7800           < (int11 >= 0 ? int11 : -int11))
7801         {
7802           tmp = int01, int01 = int11, int11 = tmp;
7803           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7804           maybe_same = arg01;
7805           swap = true;
7806         }
7807       else
7808         maybe_same = arg11;
7809
7810       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7811           /* The remainder should not be a constant, otherwise we
7812              end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7813              increased the number of multiplications necessary.  */
7814           && TREE_CODE (arg10) != INTEGER_CST)
7815         {
7816           alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7817                               build_int_cst (TREE_TYPE (arg00),
7818                                              int01 / int11));
7819           alt1 = arg10;
7820           same = maybe_same;
7821           if (swap)
7822             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7823         }
7824     }
7825
7826   if (same)
7827     return fold_build2_loc (loc, MULT_EXPR, type,
7828                         fold_build2_loc (loc, code, type,
7829                                      fold_convert_loc (loc, type, alt0),
7830                                      fold_convert_loc (loc, type, alt1)),
7831                         fold_convert_loc (loc, type, same));
7832
7833   return NULL_TREE;
7834 }
7835
7836 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7837    specified by EXPR into the buffer PTR of length LEN bytes.
7838    Return the number of bytes placed in the buffer, or zero
7839    upon failure.  */
7840
7841 static int
7842 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7843 {
7844   tree type = TREE_TYPE (expr);
7845   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7846   int byte, offset, word, words;
7847   unsigned char value;
7848
7849   if (total_bytes > len)
7850     return 0;
7851   words = total_bytes / UNITS_PER_WORD;
7852
7853   for (byte = 0; byte < total_bytes; byte++)
7854     {
7855       int bitpos = byte * BITS_PER_UNIT;
7856       if (bitpos < HOST_BITS_PER_WIDE_INT)
7857         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7858       else
7859         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7860                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7861
7862       if (total_bytes > UNITS_PER_WORD)
7863         {
7864           word = byte / UNITS_PER_WORD;
7865           if (WORDS_BIG_ENDIAN)
7866             word = (words - 1) - word;
7867           offset = word * UNITS_PER_WORD;
7868           if (BYTES_BIG_ENDIAN)
7869             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7870           else
7871             offset += byte % UNITS_PER_WORD;
7872         }
7873       else
7874         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7875       ptr[offset] = value;
7876     }
7877   return total_bytes;
7878 }
7879
7880
7881 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7882    specified by EXPR into the buffer PTR of length LEN bytes.
7883    Return the number of bytes placed in the buffer, or zero
7884    upon failure.  */
7885
7886 static int
7887 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7888 {
7889   tree type = TREE_TYPE (expr);
7890   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7891   int byte, offset, word, words, bitpos;
7892   unsigned char value;
7893
7894   /* There are always 32 bits in each long, no matter the size of
7895      the hosts long.  We handle floating point representations with
7896      up to 192 bits.  */
7897   long tmp[6];
7898
7899   if (total_bytes > len)
7900     return 0;
7901   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7902
7903   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7904
7905   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7906        bitpos += BITS_PER_UNIT)
7907     {
7908       byte = (bitpos / BITS_PER_UNIT) & 3;
7909       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7910
7911       if (UNITS_PER_WORD < 4)
7912         {
7913           word = byte / UNITS_PER_WORD;
7914           if (WORDS_BIG_ENDIAN)
7915             word = (words - 1) - word;
7916           offset = word * UNITS_PER_WORD;
7917           if (BYTES_BIG_ENDIAN)
7918             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7919           else
7920             offset += byte % UNITS_PER_WORD;
7921         }
7922       else
7923         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7924       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7925     }
7926   return total_bytes;
7927 }
7928
7929 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7930    specified by EXPR into the buffer PTR of length LEN bytes.
7931    Return the number of bytes placed in the buffer, or zero
7932    upon failure.  */
7933
7934 static int
7935 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7936 {
7937   int rsize, isize;
7938   tree part;
7939
7940   part = TREE_REALPART (expr);
7941   rsize = native_encode_expr (part, ptr, len);
7942   if (rsize == 0)
7943     return 0;
7944   part = TREE_IMAGPART (expr);
7945   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7946   if (isize != rsize)
7947     return 0;
7948   return rsize + isize;
7949 }
7950
7951
7952 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7953    specified by EXPR into the buffer PTR of length LEN bytes.
7954    Return the number of bytes placed in the buffer, or zero
7955    upon failure.  */
7956
7957 static int
7958 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7959 {
7960   int i, size, offset, count;
7961   tree itype, elem, elements;
7962
7963   offset = 0;
7964   elements = TREE_VECTOR_CST_ELTS (expr);
7965   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7966   itype = TREE_TYPE (TREE_TYPE (expr));
7967   size = GET_MODE_SIZE (TYPE_MODE (itype));
7968   for (i = 0; i < count; i++)
7969     {
7970       if (elements)
7971         {
7972           elem = TREE_VALUE (elements);
7973           elements = TREE_CHAIN (elements);
7974         }
7975       else
7976         elem = NULL_TREE;
7977
7978       if (elem)
7979         {
7980           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7981             return 0;
7982         }
7983       else
7984         {
7985           if (offset + size > len)
7986             return 0;
7987           memset (ptr+offset, 0, size);
7988         }
7989       offset += size;
7990     }
7991   return offset;
7992 }
7993
7994
7995 /* Subroutine of native_encode_expr.  Encode the STRING_CST
7996    specified by EXPR into the buffer PTR of length LEN bytes.
7997    Return the number of bytes placed in the buffer, or zero
7998    upon failure.  */
7999
8000 static int
8001 native_encode_string (const_tree expr, unsigned char *ptr, int len)
8002 {
8003   tree type = TREE_TYPE (expr);
8004   HOST_WIDE_INT total_bytes;
8005
8006   if (TREE_CODE (type) != ARRAY_TYPE
8007       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
8008       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
8009       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
8010     return 0;
8011   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
8012   if (total_bytes > len)
8013     return 0;
8014   if (TREE_STRING_LENGTH (expr) < total_bytes)
8015     {
8016       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
8017       memset (ptr + TREE_STRING_LENGTH (expr), 0,
8018               total_bytes - TREE_STRING_LENGTH (expr));
8019     }
8020   else
8021     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
8022   return total_bytes;
8023 }
8024
8025
8026 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
8027    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
8028    buffer PTR of length LEN bytes.  Return the number of bytes
8029    placed in the buffer, or zero upon failure.  */
8030
8031 int
8032 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
8033 {
8034   switch (TREE_CODE (expr))
8035     {
8036     case INTEGER_CST:
8037       return native_encode_int (expr, ptr, len);
8038
8039     case REAL_CST:
8040       return native_encode_real (expr, ptr, len);
8041
8042     case COMPLEX_CST:
8043       return native_encode_complex (expr, ptr, len);
8044
8045     case VECTOR_CST:
8046       return native_encode_vector (expr, ptr, len);
8047
8048     case STRING_CST:
8049       return native_encode_string (expr, ptr, len);
8050
8051     default:
8052       return 0;
8053     }
8054 }
8055
8056
8057 /* Subroutine of native_interpret_expr.  Interpret the contents of
8058    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8059    If the buffer cannot be interpreted, return NULL_TREE.  */
8060
8061 static tree
8062 native_interpret_int (tree type, const unsigned char *ptr, int len)
8063 {
8064   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
8065   int byte, offset, word, words;
8066   unsigned char value;
8067   unsigned int HOST_WIDE_INT lo = 0;
8068   HOST_WIDE_INT hi = 0;
8069
8070   if (total_bytes > len)
8071     return NULL_TREE;
8072   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
8073     return NULL_TREE;
8074   words = total_bytes / UNITS_PER_WORD;
8075
8076   for (byte = 0; byte < total_bytes; byte++)
8077     {
8078       int bitpos = byte * BITS_PER_UNIT;
8079       if (total_bytes > UNITS_PER_WORD)
8080         {
8081           word = byte / UNITS_PER_WORD;
8082           if (WORDS_BIG_ENDIAN)
8083             word = (words - 1) - word;
8084           offset = word * UNITS_PER_WORD;
8085           if (BYTES_BIG_ENDIAN)
8086             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8087           else
8088             offset += byte % UNITS_PER_WORD;
8089         }
8090       else
8091         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
8092       value = ptr[offset];
8093
8094       if (bitpos < HOST_BITS_PER_WIDE_INT)
8095         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
8096       else
8097         hi |= (unsigned HOST_WIDE_INT) value
8098               << (bitpos - HOST_BITS_PER_WIDE_INT);
8099     }
8100
8101   return build_int_cst_wide_type (type, lo, hi);
8102 }
8103
8104
8105 /* Subroutine of native_interpret_expr.  Interpret the contents of
8106    the buffer PTR of length LEN as a REAL_CST of type TYPE.
8107    If the buffer cannot be interpreted, return NULL_TREE.  */
8108
8109 static tree
8110 native_interpret_real (tree type, const unsigned char *ptr, int len)
8111 {
8112   enum machine_mode mode = TYPE_MODE (type);
8113   int total_bytes = GET_MODE_SIZE (mode);
8114   int byte, offset, word, words, bitpos;
8115   unsigned char value;
8116   /* There are always 32 bits in each long, no matter the size of
8117      the hosts long.  We handle floating point representations with
8118      up to 192 bits.  */
8119   REAL_VALUE_TYPE r;
8120   long tmp[6];
8121
8122   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
8123   if (total_bytes > len || total_bytes > 24)
8124     return NULL_TREE;
8125   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8126
8127   memset (tmp, 0, sizeof (tmp));
8128   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8129        bitpos += BITS_PER_UNIT)
8130     {
8131       byte = (bitpos / BITS_PER_UNIT) & 3;
8132       if (UNITS_PER_WORD < 4)
8133         {
8134           word = byte / UNITS_PER_WORD;
8135           if (WORDS_BIG_ENDIAN)
8136             word = (words - 1) - word;
8137           offset = word * UNITS_PER_WORD;
8138           if (BYTES_BIG_ENDIAN)
8139             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8140           else
8141             offset += byte % UNITS_PER_WORD;
8142         }
8143       else
8144         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
8145       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8146
8147       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8148     }
8149
8150   real_from_target (&r, tmp, mode);
8151   return build_real (type, r);
8152 }
8153
8154
8155 /* Subroutine of native_interpret_expr.  Interpret the contents of
8156    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8157    If the buffer cannot be interpreted, return NULL_TREE.  */
8158
8159 static tree
8160 native_interpret_complex (tree type, const unsigned char *ptr, int len)
8161 {
8162   tree etype, rpart, ipart;
8163   int size;
8164
8165   etype = TREE_TYPE (type);
8166   size = GET_MODE_SIZE (TYPE_MODE (etype));
8167   if (size * 2 > len)
8168     return NULL_TREE;
8169   rpart = native_interpret_expr (etype, ptr, size);
8170   if (!rpart)
8171     return NULL_TREE;
8172   ipart = native_interpret_expr (etype, ptr+size, size);
8173   if (!ipart)
8174     return NULL_TREE;
8175   return build_complex (type, rpart, ipart);
8176 }
8177
8178
8179 /* Subroutine of native_interpret_expr.  Interpret the contents of
8180    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8181    If the buffer cannot be interpreted, return NULL_TREE.  */
8182
8183 static tree
8184 native_interpret_vector (tree type, const unsigned char *ptr, int len)
8185 {
8186   tree etype, elem, elements;
8187   int i, size, count;
8188
8189   etype = TREE_TYPE (type);
8190   size = GET_MODE_SIZE (TYPE_MODE (etype));
8191   count = TYPE_VECTOR_SUBPARTS (type);
8192   if (size * count > len)
8193     return NULL_TREE;
8194
8195   elements = NULL_TREE;
8196   for (i = count - 1; i >= 0; i--)
8197     {
8198       elem = native_interpret_expr (etype, ptr+(i*size), size);
8199       if (!elem)
8200         return NULL_TREE;
8201       elements = tree_cons (NULL_TREE, elem, elements);
8202     }
8203   return build_vector (type, elements);
8204 }
8205
8206
8207 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
8208    the buffer PTR of length LEN as a constant of type TYPE.  For
8209    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8210    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
8211    return NULL_TREE.  */
8212
8213 tree
8214 native_interpret_expr (tree type, const unsigned char *ptr, int len)
8215 {
8216   switch (TREE_CODE (type))
8217     {
8218     case INTEGER_TYPE:
8219     case ENUMERAL_TYPE:
8220     case BOOLEAN_TYPE:
8221       return native_interpret_int (type, ptr, len);
8222
8223     case REAL_TYPE:
8224       return native_interpret_real (type, ptr, len);
8225
8226     case COMPLEX_TYPE:
8227       return native_interpret_complex (type, ptr, len);
8228
8229     case VECTOR_TYPE:
8230       return native_interpret_vector (type, ptr, len);
8231
8232     default:
8233       return NULL_TREE;
8234     }
8235 }
8236
8237
8238 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
8239    TYPE at compile-time.  If we're unable to perform the conversion
8240    return NULL_TREE.  */
8241
8242 static tree
8243 fold_view_convert_expr (tree type, tree expr)
8244 {
8245   /* We support up to 512-bit values (for V8DFmode).  */
8246   unsigned char buffer[64];
8247   int len;
8248
8249   /* Check that the host and target are sane.  */
8250   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
8251     return NULL_TREE;
8252
8253   len = native_encode_expr (expr, buffer, sizeof (buffer));
8254   if (len == 0)
8255     return NULL_TREE;
8256
8257   return native_interpret_expr (type, buffer, len);
8258 }
8259
8260 /* Build an expression for the address of T.  Folds away INDIRECT_REF
8261    to avoid confusing the gimplify process.  */
8262
8263 tree
8264 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
8265 {
8266   /* The size of the object is not relevant when talking about its address.  */
8267   if (TREE_CODE (t) == WITH_SIZE_EXPR)
8268     t = TREE_OPERAND (t, 0);
8269
8270   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
8271   if (TREE_CODE (t) == INDIRECT_REF
8272       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
8273     {
8274       t = TREE_OPERAND (t, 0);
8275
8276       if (TREE_TYPE (t) != ptrtype)
8277         {
8278           t = build1 (NOP_EXPR, ptrtype, t);
8279           SET_EXPR_LOCATION (t, loc);
8280         }
8281     }
8282   else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
8283     {
8284       t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
8285
8286       if (TREE_TYPE (t) != ptrtype)
8287         t = fold_convert_loc (loc, ptrtype, t);
8288     }
8289   else
8290     {
8291       t = build1 (ADDR_EXPR, ptrtype, t);
8292       SET_EXPR_LOCATION (t, loc);
8293     }
8294
8295   return t;
8296 }
8297
8298 /* Build an expression for the address of T.  */
8299
8300 tree
8301 build_fold_addr_expr_loc (location_t loc, tree t)
8302 {
8303   tree ptrtype = build_pointer_type (TREE_TYPE (t));
8304
8305   return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
8306 }
8307
8308 /* Fold a unary expression of code CODE and type TYPE with operand
8309    OP0.  Return the folded expression if folding is successful.
8310    Otherwise, return NULL_TREE.  */
8311
8312 tree
8313 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
8314 {
8315   tree tem;
8316   tree arg0;
8317   enum tree_code_class kind = TREE_CODE_CLASS (code);
8318
8319   gcc_assert (IS_EXPR_CODE_CLASS (kind)
8320               && TREE_CODE_LENGTH (code) == 1);
8321
8322   arg0 = op0;
8323   if (arg0)
8324     {
8325       if (CONVERT_EXPR_CODE_P (code)
8326           || code == FLOAT_EXPR || code == ABS_EXPR)
8327         {
8328           /* Don't use STRIP_NOPS, because signedness of argument type
8329              matters.  */
8330           STRIP_SIGN_NOPS (arg0);
8331         }
8332       else
8333         {
8334           /* Strip any conversions that don't change the mode.  This
8335              is safe for every expression, except for a comparison
8336              expression because its signedness is derived from its
8337              operands.
8338
8339              Note that this is done as an internal manipulation within
8340              the constant folder, in order to find the simplest
8341              representation of the arguments so that their form can be
8342              studied.  In any cases, the appropriate type conversions
8343              should be put back in the tree that will get out of the
8344              constant folder.  */
8345           STRIP_NOPS (arg0);
8346         }
8347     }
8348
8349   if (TREE_CODE_CLASS (code) == tcc_unary)
8350     {
8351       if (TREE_CODE (arg0) == COMPOUND_EXPR)
8352         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
8353                        fold_build1_loc (loc, code, type,
8354                                     fold_convert_loc (loc, TREE_TYPE (op0),
8355                                                       TREE_OPERAND (arg0, 1))));
8356       else if (TREE_CODE (arg0) == COND_EXPR)
8357         {
8358           tree arg01 = TREE_OPERAND (arg0, 1);
8359           tree arg02 = TREE_OPERAND (arg0, 2);
8360           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8361             arg01 = fold_build1_loc (loc, code, type,
8362                                  fold_convert_loc (loc,
8363                                                    TREE_TYPE (op0), arg01));
8364           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8365             arg02 = fold_build1_loc (loc, code, type,
8366                                  fold_convert_loc (loc,
8367                                                    TREE_TYPE (op0), arg02));
8368           tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
8369                              arg01, arg02);
8370
8371           /* If this was a conversion, and all we did was to move into
8372              inside the COND_EXPR, bring it back out.  But leave it if
8373              it is a conversion from integer to integer and the
8374              result precision is no wider than a word since such a
8375              conversion is cheap and may be optimized away by combine,
8376              while it couldn't if it were outside the COND_EXPR.  Then return
8377              so we don't get into an infinite recursion loop taking the
8378              conversion out and then back in.  */
8379
8380           if ((CONVERT_EXPR_CODE_P (code)
8381                || code == NON_LVALUE_EXPR)
8382               && TREE_CODE (tem) == COND_EXPR
8383               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8384               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8385               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8386               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8387               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8388                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8389               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8390                      && (INTEGRAL_TYPE_P
8391                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8392                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8393                   || flag_syntax_only))
8394             {
8395               tem = build1 (code, type,
8396                             build3 (COND_EXPR,
8397                                     TREE_TYPE (TREE_OPERAND
8398                                                (TREE_OPERAND (tem, 1), 0)),
8399                                     TREE_OPERAND (tem, 0),
8400                                     TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8401                                     TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8402               SET_EXPR_LOCATION (tem, loc);
8403             }
8404           return tem;
8405         }
8406       else if (COMPARISON_CLASS_P (arg0))
8407         {
8408           if (TREE_CODE (type) == BOOLEAN_TYPE)
8409             {
8410               arg0 = copy_node (arg0);
8411               TREE_TYPE (arg0) = type;
8412               return arg0;
8413             }
8414           else if (TREE_CODE (type) != INTEGER_TYPE)
8415             return fold_build3_loc (loc, COND_EXPR, type, arg0,
8416                                 fold_build1_loc (loc, code, type,
8417                                              integer_one_node),
8418                                 fold_build1_loc (loc, code, type,
8419                                              integer_zero_node));
8420         }
8421    }
8422
8423   switch (code)
8424     {
8425     case PAREN_EXPR:
8426       /* Re-association barriers around constants and other re-association
8427          barriers can be removed.  */
8428       if (CONSTANT_CLASS_P (op0)
8429           || TREE_CODE (op0) == PAREN_EXPR)
8430         return fold_convert_loc (loc, type, op0);
8431       return NULL_TREE;
8432
8433     CASE_CONVERT:
8434     case FLOAT_EXPR:
8435     case FIX_TRUNC_EXPR:
8436       if (TREE_TYPE (op0) == type)
8437         return op0;
8438       
8439       /* If we have (type) (a CMP b) and type is an integral type, return
8440          new expression involving the new type.  */
8441       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8442         return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8443                             TREE_OPERAND (op0, 1));
8444
8445       /* Handle cases of two conversions in a row.  */
8446       if (CONVERT_EXPR_P (op0))
8447         {
8448           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8449           tree inter_type = TREE_TYPE (op0);
8450           int inside_int = INTEGRAL_TYPE_P (inside_type);
8451           int inside_ptr = POINTER_TYPE_P (inside_type);
8452           int inside_float = FLOAT_TYPE_P (inside_type);
8453           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8454           unsigned int inside_prec = TYPE_PRECISION (inside_type);
8455           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8456           int inter_int = INTEGRAL_TYPE_P (inter_type);
8457           int inter_ptr = POINTER_TYPE_P (inter_type);
8458           int inter_float = FLOAT_TYPE_P (inter_type);
8459           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8460           unsigned int inter_prec = TYPE_PRECISION (inter_type);
8461           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8462           int final_int = INTEGRAL_TYPE_P (type);
8463           int final_ptr = POINTER_TYPE_P (type);
8464           int final_float = FLOAT_TYPE_P (type);
8465           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8466           unsigned int final_prec = TYPE_PRECISION (type);
8467           int final_unsignedp = TYPE_UNSIGNED (type);
8468
8469           /* In addition to the cases of two conversions in a row
8470              handled below, if we are converting something to its own
8471              type via an object of identical or wider precision, neither
8472              conversion is needed.  */
8473           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8474               && (((inter_int || inter_ptr) && final_int)
8475                   || (inter_float && final_float))
8476               && inter_prec >= final_prec)
8477             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8478
8479           /* Likewise, if the intermediate and initial types are either both
8480              float or both integer, we don't need the middle conversion if the
8481              former is wider than the latter and doesn't change the signedness
8482              (for integers).  Avoid this if the final type is a pointer since
8483              then we sometimes need the middle conversion.  Likewise if the
8484              final type has a precision not equal to the size of its mode.  */
8485           if (((inter_int && inside_int)
8486                || (inter_float && inside_float)
8487                || (inter_vec && inside_vec))
8488               && inter_prec >= inside_prec
8489               && (inter_float || inter_vec
8490                   || inter_unsignedp == inside_unsignedp)
8491               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8492                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
8493               && ! final_ptr
8494               && (! final_vec || inter_prec == inside_prec))
8495             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8496
8497           /* If we have a sign-extension of a zero-extended value, we can
8498              replace that by a single zero-extension.  */
8499           if (inside_int && inter_int && final_int
8500               && inside_prec < inter_prec && inter_prec < final_prec
8501               && inside_unsignedp && !inter_unsignedp)
8502             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8503
8504           /* Two conversions in a row are not needed unless:
8505              - some conversion is floating-point (overstrict for now), or
8506              - some conversion is a vector (overstrict for now), or
8507              - the intermediate type is narrower than both initial and
8508                final, or
8509              - the intermediate type and innermost type differ in signedness,
8510                and the outermost type is wider than the intermediate, or
8511              - the initial type is a pointer type and the precisions of the
8512                intermediate and final types differ, or
8513              - the final type is a pointer type and the precisions of the
8514                initial and intermediate types differ.  */
8515           if (! inside_float && ! inter_float && ! final_float
8516               && ! inside_vec && ! inter_vec && ! final_vec
8517               && (inter_prec >= inside_prec || inter_prec >= final_prec)
8518               && ! (inside_int && inter_int
8519                     && inter_unsignedp != inside_unsignedp
8520                     && inter_prec < final_prec)
8521               && ((inter_unsignedp && inter_prec > inside_prec)
8522                   == (final_unsignedp && final_prec > inter_prec))
8523               && ! (inside_ptr && inter_prec != final_prec)
8524               && ! (final_ptr && inside_prec != inter_prec)
8525               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8526                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
8527             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8528         }
8529
8530       /* Handle (T *)&A.B.C for A being of type T and B and C
8531          living at offset zero.  This occurs frequently in
8532          C++ upcasting and then accessing the base.  */
8533       if (TREE_CODE (op0) == ADDR_EXPR
8534           && POINTER_TYPE_P (type)
8535           && handled_component_p (TREE_OPERAND (op0, 0)))
8536         {
8537           HOST_WIDE_INT bitsize, bitpos;
8538           tree offset;
8539           enum machine_mode mode;
8540           int unsignedp, volatilep;
8541           tree base = TREE_OPERAND (op0, 0);
8542           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8543                                       &mode, &unsignedp, &volatilep, false);
8544           /* If the reference was to a (constant) zero offset, we can use
8545              the address of the base if it has the same base type
8546              as the result type.  */
8547           if (! offset && bitpos == 0
8548               && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8549                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8550             return fold_convert_loc (loc, type,
8551                                      build_fold_addr_expr_loc (loc, base));
8552         }
8553
8554       if (TREE_CODE (op0) == MODIFY_EXPR
8555           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
8556           /* Detect assigning a bitfield.  */
8557           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
8558                && DECL_BIT_FIELD
8559                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
8560         {
8561           /* Don't leave an assignment inside a conversion
8562              unless assigning a bitfield.  */
8563           tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
8564           /* First do the assignment, then return converted constant.  */
8565           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8566           TREE_NO_WARNING (tem) = 1;
8567           TREE_USED (tem) = 1;
8568           SET_EXPR_LOCATION (tem, loc);
8569           return tem;
8570         }
8571
8572       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8573          constants (if x has signed type, the sign bit cannot be set
8574          in c).  This folds extension into the BIT_AND_EXPR.
8575          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8576          very likely don't have maximal range for their precision and this
8577          transformation effectively doesn't preserve non-maximal ranges.  */
8578       if (TREE_CODE (type) == INTEGER_TYPE
8579           && TREE_CODE (op0) == BIT_AND_EXPR
8580           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
8581         {
8582           tree and_expr = op0;
8583           tree and0 = TREE_OPERAND (and_expr, 0);
8584           tree and1 = TREE_OPERAND (and_expr, 1);
8585           int change = 0;
8586
8587           if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
8588               || (TYPE_PRECISION (type)
8589                   <= TYPE_PRECISION (TREE_TYPE (and_expr))))
8590             change = 1;
8591           else if (TYPE_PRECISION (TREE_TYPE (and1))
8592                    <= HOST_BITS_PER_WIDE_INT
8593                    && host_integerp (and1, 1))
8594             {
8595               unsigned HOST_WIDE_INT cst;
8596
8597               cst = tree_low_cst (and1, 1);
8598               cst &= (HOST_WIDE_INT) -1
8599                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8600               change = (cst == 0);
8601 #ifdef LOAD_EXTEND_OP
8602               if (change
8603                   && !flag_syntax_only
8604                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8605                       == ZERO_EXTEND))
8606                 {
8607                   tree uns = unsigned_type_for (TREE_TYPE (and0));
8608                   and0 = fold_convert_loc (loc, uns, and0);
8609                   and1 = fold_convert_loc (loc, uns, and1);
8610                 }
8611 #endif
8612             }
8613           if (change)
8614             {
8615               tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8616                                            TREE_INT_CST_HIGH (and1), 0,
8617                                            TREE_OVERFLOW (and1));
8618               return fold_build2_loc (loc, BIT_AND_EXPR, type,
8619                                   fold_convert_loc (loc, type, and0), tem);
8620             }
8621         }
8622
8623       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8624          when one of the new casts will fold away. Conservatively we assume
8625          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8626       if (POINTER_TYPE_P (type)
8627           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8628           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8629               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8630               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8631         {
8632           tree arg00 = TREE_OPERAND (arg0, 0);
8633           tree arg01 = TREE_OPERAND (arg0, 1);
8634
8635           return fold_build2_loc (loc,
8636                               TREE_CODE (arg0), type,
8637                               fold_convert_loc (loc, type, arg00),
8638                               fold_convert_loc (loc, sizetype, arg01));
8639         }
8640
8641       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8642          of the same precision, and X is an integer type not narrower than
8643          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
8644       if (INTEGRAL_TYPE_P (type)
8645           && TREE_CODE (op0) == BIT_NOT_EXPR
8646           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8647           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8648           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8649         {
8650           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8651           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8652               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8653             return fold_build1_loc (loc, BIT_NOT_EXPR, type,
8654                                 fold_convert_loc (loc, type, tem));
8655         }
8656
8657       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8658          type of X and Y (integer types only).  */
8659       if (INTEGRAL_TYPE_P (type)
8660           && TREE_CODE (op0) == MULT_EXPR
8661           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8662           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8663         {
8664           /* Be careful not to introduce new overflows.  */
8665           tree mult_type;
8666           if (TYPE_OVERFLOW_WRAPS (type))
8667             mult_type = type;
8668           else
8669             mult_type = unsigned_type_for (type);
8670
8671           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8672             {
8673               tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8674                                  fold_convert_loc (loc, mult_type,
8675                                                    TREE_OPERAND (op0, 0)),
8676                                  fold_convert_loc (loc, mult_type,
8677                                                    TREE_OPERAND (op0, 1)));
8678               return fold_convert_loc (loc, type, tem);
8679             }
8680         }
8681
8682       tem = fold_convert_const (code, type, op0);
8683       return tem ? tem : NULL_TREE;
8684
8685     case FIXED_CONVERT_EXPR:
8686       tem = fold_convert_const (code, type, arg0);
8687       return tem ? tem : NULL_TREE;
8688
8689     case VIEW_CONVERT_EXPR:
8690       if (TREE_TYPE (op0) == type)
8691         return op0;
8692       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8693         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8694                             type, TREE_OPERAND (op0, 0));
8695
8696       /* For integral conversions with the same precision or pointer
8697          conversions use a NOP_EXPR instead.  */
8698       if ((INTEGRAL_TYPE_P (type)
8699            || POINTER_TYPE_P (type))
8700           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8701               || POINTER_TYPE_P (TREE_TYPE (op0)))
8702           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8703         return fold_convert_loc (loc, type, op0);
8704
8705       /* Strip inner integral conversions that do not change the precision.  */
8706       if (CONVERT_EXPR_P (op0)
8707           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8708               || POINTER_TYPE_P (TREE_TYPE (op0)))
8709           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8710               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8711           && (TYPE_PRECISION (TREE_TYPE (op0))
8712               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8713         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8714                             type, TREE_OPERAND (op0, 0));
8715
8716       return fold_view_convert_expr (type, op0);
8717
8718     case NEGATE_EXPR:
8719       tem = fold_negate_expr (loc, arg0);
8720       if (tem)
8721         return fold_convert_loc (loc, type, tem);
8722       return NULL_TREE;
8723
8724     case ABS_EXPR:
8725       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8726         return fold_abs_const (arg0, type);
8727       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8728         return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8729       /* Convert fabs((double)float) into (double)fabsf(float).  */
8730       else if (TREE_CODE (arg0) == NOP_EXPR
8731                && TREE_CODE (type) == REAL_TYPE)
8732         {
8733           tree targ0 = strip_float_extensions (arg0);
8734           if (targ0 != arg0)
8735             return fold_convert_loc (loc, type,
8736                                      fold_build1_loc (loc, ABS_EXPR,
8737                                                   TREE_TYPE (targ0),
8738                                                   targ0));
8739         }
8740       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8741       else if (TREE_CODE (arg0) == ABS_EXPR)
8742         return arg0;
8743       else if (tree_expr_nonnegative_p (arg0))
8744         return arg0;
8745
8746       /* Strip sign ops from argument.  */
8747       if (TREE_CODE (type) == REAL_TYPE)
8748         {
8749           tem = fold_strip_sign_ops (arg0);
8750           if (tem)
8751             return fold_build1_loc (loc, ABS_EXPR, type,
8752                                 fold_convert_loc (loc, type, tem));
8753         }
8754       return NULL_TREE;
8755
8756     case CONJ_EXPR:
8757       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8758         return fold_convert_loc (loc, type, arg0);
8759       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8760         {
8761           tree itype = TREE_TYPE (type);
8762           tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8763           tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8764           return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8765                               negate_expr (ipart));
8766         }
8767       if (TREE_CODE (arg0) == COMPLEX_CST)
8768         {
8769           tree itype = TREE_TYPE (type);
8770           tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8771           tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8772           return build_complex (type, rpart, negate_expr (ipart));
8773         }
8774       if (TREE_CODE (arg0) == CONJ_EXPR)
8775         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8776       return NULL_TREE;
8777
8778     case BIT_NOT_EXPR:
8779       if (TREE_CODE (arg0) == INTEGER_CST)
8780         return fold_not_const (arg0, type);
8781       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8782         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8783       /* Convert ~ (-A) to A - 1.  */
8784       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8785         return fold_build2_loc (loc, MINUS_EXPR, type,
8786                             fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8787                             build_int_cst (type, 1));
8788       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8789       else if (INTEGRAL_TYPE_P (type)
8790                && ((TREE_CODE (arg0) == MINUS_EXPR
8791                     && integer_onep (TREE_OPERAND (arg0, 1)))
8792                    || (TREE_CODE (arg0) == PLUS_EXPR
8793                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8794         return fold_build1_loc (loc, NEGATE_EXPR, type,
8795                             fold_convert_loc (loc, type,
8796                                               TREE_OPERAND (arg0, 0)));
8797       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8798       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8799                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8800                                      fold_convert_loc (loc, type,
8801                                                        TREE_OPERAND (arg0, 0)))))
8802         return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8803                             fold_convert_loc (loc, type,
8804                                               TREE_OPERAND (arg0, 1)));
8805       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8806                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8807                                      fold_convert_loc (loc, type,
8808                                                        TREE_OPERAND (arg0, 1)))))
8809         return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8810                             fold_convert_loc (loc, type,
8811                                               TREE_OPERAND (arg0, 0)), tem);
8812       /* Perform BIT_NOT_EXPR on each element individually.  */
8813       else if (TREE_CODE (arg0) == VECTOR_CST)
8814         {
8815           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8816           int count = TYPE_VECTOR_SUBPARTS (type), i;
8817
8818           for (i = 0; i < count; i++)
8819             {
8820               if (elements)
8821                 {
8822                   elem = TREE_VALUE (elements);
8823                   elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8824                   if (elem == NULL_TREE)
8825                     break;
8826                   elements = TREE_CHAIN (elements);
8827                 }
8828               else
8829                 elem = build_int_cst (TREE_TYPE (type), -1);
8830               list = tree_cons (NULL_TREE, elem, list);
8831             }
8832           if (i == count)
8833             return build_vector (type, nreverse (list));
8834         }
8835
8836       return NULL_TREE;
8837
8838     case TRUTH_NOT_EXPR:
8839       /* The argument to invert_truthvalue must have Boolean type.  */
8840       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8841           arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8842
8843       /* Note that the operand of this must be an int
8844          and its values must be 0 or 1.
8845          ("true" is a fixed value perhaps depending on the language,
8846          but we don't handle values other than 1 correctly yet.)  */
8847       tem = fold_truth_not_expr (loc, arg0);
8848       if (!tem)
8849         return NULL_TREE;
8850       return fold_convert_loc (loc, type, tem);
8851
8852     case REALPART_EXPR:
8853       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8854         return fold_convert_loc (loc, type, arg0);
8855       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8856         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8857                                  TREE_OPERAND (arg0, 1));
8858       if (TREE_CODE (arg0) == COMPLEX_CST)
8859         return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8860       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8861         {
8862           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8863           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8864                              fold_build1_loc (loc, REALPART_EXPR, itype,
8865                                           TREE_OPERAND (arg0, 0)),
8866                              fold_build1_loc (loc, REALPART_EXPR, itype,
8867                                           TREE_OPERAND (arg0, 1)));
8868           return fold_convert_loc (loc, type, tem);
8869         }
8870       if (TREE_CODE (arg0) == CONJ_EXPR)
8871         {
8872           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8873           tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8874                              TREE_OPERAND (arg0, 0));
8875           return fold_convert_loc (loc, type, tem);
8876         }
8877       if (TREE_CODE (arg0) == CALL_EXPR)
8878         {
8879           tree fn = get_callee_fndecl (arg0);
8880           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8881             switch (DECL_FUNCTION_CODE (fn))
8882               {
8883               CASE_FLT_FN (BUILT_IN_CEXPI):
8884                 fn = mathfn_built_in (type, BUILT_IN_COS);
8885                 if (fn)
8886                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8887                 break;
8888
8889               default:
8890                 break;
8891               }
8892         }
8893       return NULL_TREE;
8894
8895     case IMAGPART_EXPR:
8896       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8897         return fold_convert_loc (loc, type, integer_zero_node);
8898       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8899         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8900                                  TREE_OPERAND (arg0, 0));
8901       if (TREE_CODE (arg0) == COMPLEX_CST)
8902         return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8903       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8904         {
8905           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8906           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8907                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8908                                           TREE_OPERAND (arg0, 0)),
8909                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8910                                           TREE_OPERAND (arg0, 1)));
8911           return fold_convert_loc (loc, type, tem);
8912         }
8913       if (TREE_CODE (arg0) == CONJ_EXPR)
8914         {
8915           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8916           tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8917           return fold_convert_loc (loc, type, negate_expr (tem));
8918         }
8919       if (TREE_CODE (arg0) == CALL_EXPR)
8920         {
8921           tree fn = get_callee_fndecl (arg0);
8922           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8923             switch (DECL_FUNCTION_CODE (fn))
8924               {
8925               CASE_FLT_FN (BUILT_IN_CEXPI):
8926                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8927                 if (fn)
8928                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8929                 break;
8930
8931               default:
8932                 break;
8933               }
8934         }
8935       return NULL_TREE;
8936
8937     default:
8938       return NULL_TREE;
8939     } /* switch (code) */
8940 }
8941
8942
8943 /* If the operation was a conversion do _not_ mark a resulting constant
8944    with TREE_OVERFLOW if the original constant was not.  These conversions
8945    have implementation defined behavior and retaining the TREE_OVERFLOW
8946    flag here would confuse later passes such as VRP.  */
8947 tree
8948 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8949                                 tree type, tree op0)
8950 {
8951   tree res = fold_unary_loc (loc, code, type, op0);
8952   if (res
8953       && TREE_CODE (res) == INTEGER_CST
8954       && TREE_CODE (op0) == INTEGER_CST
8955       && CONVERT_EXPR_CODE_P (code))
8956     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8957
8958   return res;
8959 }
8960
8961 /* Fold a binary expression of code CODE and type TYPE with operands
8962    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8963    Return the folded expression if folding is successful.  Otherwise,
8964    return NULL_TREE.  */
8965
8966 static tree
8967 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8968 {
8969   enum tree_code compl_code;
8970
8971   if (code == MIN_EXPR)
8972     compl_code = MAX_EXPR;
8973   else if (code == MAX_EXPR)
8974     compl_code = MIN_EXPR;
8975   else
8976     gcc_unreachable ();
8977
8978   /* MIN (MAX (a, b), b) == b.  */
8979   if (TREE_CODE (op0) == compl_code
8980       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8981     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8982
8983   /* MIN (MAX (b, a), b) == b.  */
8984   if (TREE_CODE (op0) == compl_code
8985       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8986       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8987     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8988
8989   /* MIN (a, MAX (a, b)) == a.  */
8990   if (TREE_CODE (op1) == compl_code
8991       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8992       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8993     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8994
8995   /* MIN (a, MAX (b, a)) == a.  */
8996   if (TREE_CODE (op1) == compl_code
8997       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8998       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8999     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
9000
9001   return NULL_TREE;
9002 }
9003
9004 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9005    by changing CODE to reduce the magnitude of constants involved in
9006    ARG0 of the comparison.
9007    Returns a canonicalized comparison tree if a simplification was
9008    possible, otherwise returns NULL_TREE.
9009    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9010    valid if signed overflow is undefined.  */
9011
9012 static tree
9013 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9014                                  tree arg0, tree arg1,
9015                                  bool *strict_overflow_p)
9016 {
9017   enum tree_code code0 = TREE_CODE (arg0);
9018   tree t, cst0 = NULL_TREE;
9019   int sgn0;
9020   bool swap = false;
9021
9022   /* Match A +- CST code arg1 and CST code arg1.  We can change the
9023      first form only if overflow is undefined.  */
9024   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9025          /* In principle pointers also have undefined overflow behavior,
9026             but that causes problems elsewhere.  */
9027          && !POINTER_TYPE_P (TREE_TYPE (arg0))
9028          && (code0 == MINUS_EXPR
9029              || code0 == PLUS_EXPR)
9030          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9031         || code0 == INTEGER_CST))
9032     return NULL_TREE;
9033
9034   /* Identify the constant in arg0 and its sign.  */
9035   if (code0 == INTEGER_CST)
9036     cst0 = arg0;
9037   else
9038     cst0 = TREE_OPERAND (arg0, 1);
9039   sgn0 = tree_int_cst_sgn (cst0);
9040
9041   /* Overflowed constants and zero will cause problems.  */
9042   if (integer_zerop (cst0)
9043       || TREE_OVERFLOW (cst0))
9044     return NULL_TREE;
9045
9046   /* See if we can reduce the magnitude of the constant in
9047      arg0 by changing the comparison code.  */
9048   if (code0 == INTEGER_CST)
9049     {
9050       /* CST <= arg1  ->  CST-1 < arg1.  */
9051       if (code == LE_EXPR && sgn0 == 1)
9052         code = LT_EXPR;
9053       /* -CST < arg1  ->  -CST-1 <= arg1.  */
9054       else if (code == LT_EXPR && sgn0 == -1)
9055         code = LE_EXPR;
9056       /* CST > arg1  ->  CST-1 >= arg1.  */
9057       else if (code == GT_EXPR && sgn0 == 1)
9058         code = GE_EXPR;
9059       /* -CST >= arg1  ->  -CST-1 > arg1.  */
9060       else if (code == GE_EXPR && sgn0 == -1)
9061         code = GT_EXPR;
9062       else
9063         return NULL_TREE;
9064       /* arg1 code' CST' might be more canonical.  */
9065       swap = true;
9066     }
9067   else
9068     {
9069       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
9070       if (code == LT_EXPR
9071           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9072         code = LE_EXPR;
9073       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
9074       else if (code == GT_EXPR
9075                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9076         code = GE_EXPR;
9077       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
9078       else if (code == LE_EXPR
9079                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9080         code = LT_EXPR;
9081       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
9082       else if (code == GE_EXPR
9083                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9084         code = GT_EXPR;
9085       else
9086         return NULL_TREE;
9087       *strict_overflow_p = true;
9088     }
9089
9090   /* Now build the constant reduced in magnitude.  But not if that
9091      would produce one outside of its types range.  */
9092   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9093       && ((sgn0 == 1
9094            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9095            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9096           || (sgn0 == -1
9097               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9098               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9099     /* We cannot swap the comparison here as that would cause us to
9100        endlessly recurse.  */
9101     return NULL_TREE;
9102
9103   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9104                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
9105   if (code0 != INTEGER_CST)
9106     t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9107
9108   /* If swapping might yield to a more canonical form, do so.  */
9109   if (swap)
9110     return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
9111   else
9112     return fold_build2_loc (loc, code, type, t, arg1);
9113 }
9114
9115 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9116    overflow further.  Try to decrease the magnitude of constants involved
9117    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9118    and put sole constants at the second argument position.
9119    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
9120
9121 static tree
9122 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9123                                tree arg0, tree arg1)
9124 {
9125   tree t;
9126   bool strict_overflow_p;
9127   const char * const warnmsg = G_("assuming signed overflow does not occur "
9128                                   "when reducing constant in comparison");
9129
9130   /* Try canonicalization by simplifying arg0.  */
9131   strict_overflow_p = false;
9132   t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
9133                                        &strict_overflow_p);
9134   if (t)
9135     {
9136       if (strict_overflow_p)
9137         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9138       return t;
9139     }
9140
9141   /* Try canonicalization by simplifying arg1 using the swapped
9142      comparison.  */
9143   code = swap_tree_comparison (code);
9144   strict_overflow_p = false;
9145   t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
9146                                        &strict_overflow_p);
9147   if (t && strict_overflow_p)
9148     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9149   return t;
9150 }
9151
9152 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
9153    space.  This is used to avoid issuing overflow warnings for
9154    expressions like &p->x which can not wrap.  */
9155
9156 static bool
9157 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
9158 {
9159   unsigned HOST_WIDE_INT offset_low, total_low;
9160   HOST_WIDE_INT size, offset_high, total_high;
9161
9162   if (!POINTER_TYPE_P (TREE_TYPE (base)))
9163     return true;
9164
9165   if (bitpos < 0)
9166     return true;
9167
9168   if (offset == NULL_TREE)
9169     {
9170       offset_low = 0;
9171       offset_high = 0;
9172     }
9173   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
9174     return true;
9175   else
9176     {
9177       offset_low = TREE_INT_CST_LOW (offset);
9178       offset_high = TREE_INT_CST_HIGH (offset);
9179     }
9180
9181   if (add_double_with_sign (offset_low, offset_high,
9182                             bitpos / BITS_PER_UNIT, 0,
9183                             &total_low, &total_high,
9184                             true))
9185     return true;
9186
9187   if (total_high != 0)
9188     return true;
9189
9190   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
9191   if (size <= 0)
9192     return true;
9193
9194   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
9195      array.  */
9196   if (TREE_CODE (base) == ADDR_EXPR)
9197     {
9198       HOST_WIDE_INT base_size;
9199
9200       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
9201       if (base_size > 0 && size < base_size)
9202         size = base_size;
9203     }
9204
9205   return total_low > (unsigned HOST_WIDE_INT) size;
9206 }
9207
9208 /* Subroutine of fold_binary.  This routine performs all of the
9209    transformations that are common to the equality/inequality
9210    operators (EQ_EXPR and NE_EXPR) and the ordering operators
9211    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
9212    fold_binary should call fold_binary.  Fold a comparison with
9213    tree code CODE and type TYPE with operands OP0 and OP1.  Return
9214    the folded comparison or NULL_TREE.  */
9215
9216 static tree
9217 fold_comparison (location_t loc, enum tree_code code, tree type,
9218                  tree op0, tree op1)
9219 {
9220   tree arg0, arg1, tem;
9221
9222   arg0 = op0;
9223   arg1 = op1;
9224
9225   STRIP_SIGN_NOPS (arg0);
9226   STRIP_SIGN_NOPS (arg1);
9227
9228   tem = fold_relational_const (code, type, arg0, arg1);
9229   if (tem != NULL_TREE)
9230     return tem;
9231
9232   /* If one arg is a real or integer constant, put it last.  */
9233   if (tree_swap_operands_p (arg0, arg1, true))
9234     return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9235
9236   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
9237   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9238       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9239           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9240           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
9241       && (TREE_CODE (arg1) == INTEGER_CST
9242           && !TREE_OVERFLOW (arg1)))
9243     {
9244       tree const1 = TREE_OPERAND (arg0, 1);
9245       tree const2 = arg1;
9246       tree variable = TREE_OPERAND (arg0, 0);
9247       tree lhs;
9248       int lhs_add;
9249       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
9250
9251       lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
9252                          TREE_TYPE (arg1), const2, const1);
9253
9254       /* If the constant operation overflowed this can be
9255          simplified as a comparison against INT_MAX/INT_MIN.  */
9256       if (TREE_CODE (lhs) == INTEGER_CST
9257           && TREE_OVERFLOW (lhs))
9258         {
9259           int const1_sgn = tree_int_cst_sgn (const1);
9260           enum tree_code code2 = code;
9261
9262           /* Get the sign of the constant on the lhs if the
9263              operation were VARIABLE + CONST1.  */
9264           if (TREE_CODE (arg0) == MINUS_EXPR)
9265             const1_sgn = -const1_sgn;
9266
9267           /* The sign of the constant determines if we overflowed
9268              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
9269              Canonicalize to the INT_MIN overflow by swapping the comparison
9270              if necessary.  */
9271           if (const1_sgn == -1)
9272             code2 = swap_tree_comparison (code);
9273
9274           /* We now can look at the canonicalized case
9275                VARIABLE + 1  CODE2  INT_MIN
9276              and decide on the result.  */
9277           if (code2 == LT_EXPR
9278               || code2 == LE_EXPR
9279               || code2 == EQ_EXPR)
9280             return omit_one_operand_loc (loc, type, boolean_false_node, variable);
9281           else if (code2 == NE_EXPR
9282                    || code2 == GE_EXPR
9283                    || code2 == GT_EXPR)
9284             return omit_one_operand_loc (loc, type, boolean_true_node, variable);
9285         }
9286
9287       if (TREE_CODE (lhs) == TREE_CODE (arg1)
9288           && (TREE_CODE (lhs) != INTEGER_CST
9289               || !TREE_OVERFLOW (lhs)))
9290         {
9291           fold_overflow_warning (("assuming signed overflow does not occur "
9292                                   "when changing X +- C1 cmp C2 to "
9293                                   "X cmp C1 +- C2"),
9294                                  WARN_STRICT_OVERFLOW_COMPARISON);
9295           return fold_build2_loc (loc, code, type, variable, lhs);
9296         }
9297     }
9298
9299   /* For comparisons of pointers we can decompose it to a compile time
9300      comparison of the base objects and the offsets into the object.
9301      This requires at least one operand being an ADDR_EXPR or a
9302      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
9303   if (POINTER_TYPE_P (TREE_TYPE (arg0))
9304       && (TREE_CODE (arg0) == ADDR_EXPR
9305           || TREE_CODE (arg1) == ADDR_EXPR
9306           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9307           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9308     {
9309       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9310       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
9311       enum machine_mode mode;
9312       int volatilep, unsignedp;
9313       bool indirect_base0 = false, indirect_base1 = false;
9314
9315       /* Get base and offset for the access.  Strip ADDR_EXPR for
9316          get_inner_reference, but put it back by stripping INDIRECT_REF
9317          off the base object if possible.  indirect_baseN will be true
9318          if baseN is not an address but refers to the object itself.  */
9319       base0 = arg0;
9320       if (TREE_CODE (arg0) == ADDR_EXPR)
9321         {
9322           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
9323                                        &bitsize, &bitpos0, &offset0, &mode,
9324                                        &unsignedp, &volatilep, false);
9325           if (TREE_CODE (base0) == INDIRECT_REF)
9326             base0 = TREE_OPERAND (base0, 0);
9327           else
9328             indirect_base0 = true;
9329         }
9330       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9331         {
9332           base0 = TREE_OPERAND (arg0, 0);
9333           offset0 = TREE_OPERAND (arg0, 1);
9334         }
9335
9336       base1 = arg1;
9337       if (TREE_CODE (arg1) == ADDR_EXPR)
9338         {
9339           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
9340                                        &bitsize, &bitpos1, &offset1, &mode,
9341                                        &unsignedp, &volatilep, false);
9342           if (TREE_CODE (base1) == INDIRECT_REF)
9343             base1 = TREE_OPERAND (base1, 0);
9344           else
9345             indirect_base1 = true;
9346         }
9347       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9348         {
9349           base1 = TREE_OPERAND (arg1, 0);
9350           offset1 = TREE_OPERAND (arg1, 1);
9351         }
9352
9353       /* If we have equivalent bases we might be able to simplify.  */
9354       if (indirect_base0 == indirect_base1
9355           && operand_equal_p (base0, base1, 0))
9356         {
9357           /* We can fold this expression to a constant if the non-constant
9358              offset parts are equal.  */
9359           if ((offset0 == offset1
9360                || (offset0 && offset1
9361                    && operand_equal_p (offset0, offset1, 0)))
9362               && (code == EQ_EXPR
9363                   || code == NE_EXPR
9364                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
9365                 
9366             {
9367               if (code != EQ_EXPR
9368                   && code != NE_EXPR
9369                   && bitpos0 != bitpos1
9370                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9371                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9372                 fold_overflow_warning (("assuming pointer wraparound does not "
9373                                         "occur when comparing P +- C1 with "
9374                                         "P +- C2"),
9375                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
9376
9377               switch (code)
9378                 {
9379                 case EQ_EXPR:
9380                   return constant_boolean_node (bitpos0 == bitpos1, type);
9381                 case NE_EXPR:
9382                   return constant_boolean_node (bitpos0 != bitpos1, type);
9383                 case LT_EXPR:
9384                   return constant_boolean_node (bitpos0 < bitpos1, type);
9385                 case LE_EXPR:
9386                   return constant_boolean_node (bitpos0 <= bitpos1, type);
9387                 case GE_EXPR:
9388                   return constant_boolean_node (bitpos0 >= bitpos1, type);
9389                 case GT_EXPR:
9390                   return constant_boolean_node (bitpos0 > bitpos1, type);
9391                 default:;
9392                 }
9393             }
9394           /* We can simplify the comparison to a comparison of the variable
9395              offset parts if the constant offset parts are equal.
9396              Be careful to use signed size type here because otherwise we
9397              mess with array offsets in the wrong way.  This is possible
9398              because pointer arithmetic is restricted to retain within an
9399              object and overflow on pointer differences is undefined as of
9400              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
9401           else if (bitpos0 == bitpos1
9402                    && ((code == EQ_EXPR || code == NE_EXPR)
9403                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
9404             {
9405               tree signed_size_type_node;
9406               signed_size_type_node = signed_type_for (size_type_node);
9407
9408               /* By converting to signed size type we cover middle-end pointer
9409                  arithmetic which operates on unsigned pointer types of size
9410                  type size and ARRAY_REF offsets which are properly sign or
9411                  zero extended from their type in case it is narrower than
9412                  size type.  */
9413               if (offset0 == NULL_TREE)
9414                 offset0 = build_int_cst (signed_size_type_node, 0);
9415               else
9416                 offset0 = fold_convert_loc (loc, signed_size_type_node,
9417                                             offset0);
9418               if (offset1 == NULL_TREE)
9419                 offset1 = build_int_cst (signed_size_type_node, 0);
9420               else
9421                 offset1 = fold_convert_loc (loc, signed_size_type_node,
9422                                             offset1);
9423
9424               if (code != EQ_EXPR
9425                   && code != NE_EXPR
9426                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9427                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9428                 fold_overflow_warning (("assuming pointer wraparound does not "
9429                                         "occur when comparing P +- C1 with "
9430                                         "P +- C2"),
9431                                        WARN_STRICT_OVERFLOW_COMPARISON);
9432
9433               return fold_build2_loc (loc, code, type, offset0, offset1);
9434             }
9435         }
9436       /* For non-equal bases we can simplify if they are addresses
9437          of local binding decls or constants.  */
9438       else if (indirect_base0 && indirect_base1
9439                /* We know that !operand_equal_p (base0, base1, 0)
9440                   because the if condition was false.  But make
9441                   sure two decls are not the same.  */
9442                && base0 != base1
9443                && TREE_CODE (arg0) == ADDR_EXPR
9444                && TREE_CODE (arg1) == ADDR_EXPR
9445                && (((TREE_CODE (base0) == VAR_DECL
9446                      || TREE_CODE (base0) == PARM_DECL)
9447                     && (targetm.binds_local_p (base0)
9448                         || CONSTANT_CLASS_P (base1)))
9449                    || CONSTANT_CLASS_P (base0))
9450                && (((TREE_CODE (base1) == VAR_DECL
9451                      || TREE_CODE (base1) == PARM_DECL)
9452                     && (targetm.binds_local_p (base1)
9453                         || CONSTANT_CLASS_P (base0)))
9454                    || CONSTANT_CLASS_P (base1)))
9455         {
9456           if (code == EQ_EXPR)
9457             return omit_two_operands_loc (loc, type, boolean_false_node,
9458                                       arg0, arg1);
9459           else if (code == NE_EXPR)
9460             return omit_two_operands_loc (loc, type, boolean_true_node,
9461                                       arg0, arg1);
9462         }
9463       /* For equal offsets we can simplify to a comparison of the
9464          base addresses.  */
9465       else if (bitpos0 == bitpos1
9466                && (indirect_base0
9467                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9468                && (indirect_base1
9469                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9470                && ((offset0 == offset1)
9471                    || (offset0 && offset1
9472                        && operand_equal_p (offset0, offset1, 0))))
9473         {
9474           if (indirect_base0)
9475             base0 = build_fold_addr_expr_loc (loc, base0);
9476           if (indirect_base1)
9477             base1 = build_fold_addr_expr_loc (loc, base1);
9478           return fold_build2_loc (loc, code, type, base0, base1);
9479         }
9480     }
9481
9482   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9483      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
9484      the resulting offset is smaller in absolute value than the
9485      original one.  */
9486   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9487       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9488       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9489           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9490       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9491       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9492           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9493     {
9494       tree const1 = TREE_OPERAND (arg0, 1);
9495       tree const2 = TREE_OPERAND (arg1, 1);
9496       tree variable1 = TREE_OPERAND (arg0, 0);
9497       tree variable2 = TREE_OPERAND (arg1, 0);
9498       tree cst;
9499       const char * const warnmsg = G_("assuming signed overflow does not "
9500                                       "occur when combining constants around "
9501                                       "a comparison");
9502
9503       /* Put the constant on the side where it doesn't overflow and is
9504          of lower absolute value than before.  */
9505       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9506                              ? MINUS_EXPR : PLUS_EXPR,
9507                              const2, const1, 0);
9508       if (!TREE_OVERFLOW (cst)
9509           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9510         {
9511           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9512           return fold_build2_loc (loc, code, type,
9513                               variable1,
9514                               fold_build2_loc (loc,
9515                                            TREE_CODE (arg1), TREE_TYPE (arg1),
9516                                            variable2, cst));
9517         }
9518
9519       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9520                              ? MINUS_EXPR : PLUS_EXPR,
9521                              const1, const2, 0);
9522       if (!TREE_OVERFLOW (cst)
9523           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9524         {
9525           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9526           return fold_build2_loc (loc, code, type,
9527                               fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
9528                                            variable1, cst),
9529                               variable2);
9530         }
9531     }
9532
9533   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9534      signed arithmetic case.  That form is created by the compiler
9535      often enough for folding it to be of value.  One example is in
9536      computing loop trip counts after Operator Strength Reduction.  */
9537   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9538       && TREE_CODE (arg0) == MULT_EXPR
9539       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9540           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9541       && integer_zerop (arg1))
9542     {
9543       tree const1 = TREE_OPERAND (arg0, 1);
9544       tree const2 = arg1;                       /* zero */
9545       tree variable1 = TREE_OPERAND (arg0, 0);
9546       enum tree_code cmp_code = code;
9547
9548       gcc_assert (!integer_zerop (const1));
9549
9550       fold_overflow_warning (("assuming signed overflow does not occur when "
9551                               "eliminating multiplication in comparison "
9552                               "with zero"),
9553                              WARN_STRICT_OVERFLOW_COMPARISON);
9554
9555       /* If const1 is negative we swap the sense of the comparison.  */
9556       if (tree_int_cst_sgn (const1) < 0)
9557         cmp_code = swap_tree_comparison (cmp_code);
9558
9559       return fold_build2_loc (loc, cmp_code, type, variable1, const2);
9560     }
9561
9562   tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
9563   if (tem)
9564     return tem;
9565
9566   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9567     {
9568       tree targ0 = strip_float_extensions (arg0);
9569       tree targ1 = strip_float_extensions (arg1);
9570       tree newtype = TREE_TYPE (targ0);
9571
9572       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9573         newtype = TREE_TYPE (targ1);
9574
9575       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9576       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9577         return fold_build2_loc (loc, code, type,
9578                             fold_convert_loc (loc, newtype, targ0),
9579                             fold_convert_loc (loc, newtype, targ1));
9580
9581       /* (-a) CMP (-b) -> b CMP a  */
9582       if (TREE_CODE (arg0) == NEGATE_EXPR
9583           && TREE_CODE (arg1) == NEGATE_EXPR)
9584         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
9585                             TREE_OPERAND (arg0, 0));
9586
9587       if (TREE_CODE (arg1) == REAL_CST)
9588         {
9589           REAL_VALUE_TYPE cst;
9590           cst = TREE_REAL_CST (arg1);
9591
9592           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
9593           if (TREE_CODE (arg0) == NEGATE_EXPR)
9594             return fold_build2_loc (loc, swap_tree_comparison (code), type,
9595                                 TREE_OPERAND (arg0, 0),
9596                                 build_real (TREE_TYPE (arg1),
9597                                             REAL_VALUE_NEGATE (cst)));
9598
9599           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
9600           /* a CMP (-0) -> a CMP 0  */
9601           if (REAL_VALUE_MINUS_ZERO (cst))
9602             return fold_build2_loc (loc, code, type, arg0,
9603                                 build_real (TREE_TYPE (arg1), dconst0));
9604
9605           /* x != NaN is always true, other ops are always false.  */
9606           if (REAL_VALUE_ISNAN (cst)
9607               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9608             {
9609               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9610               return omit_one_operand_loc (loc, type, tem, arg0);
9611             }
9612
9613           /* Fold comparisons against infinity.  */
9614           if (REAL_VALUE_ISINF (cst)
9615               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9616             {
9617               tem = fold_inf_compare (loc, code, type, arg0, arg1);
9618               if (tem != NULL_TREE)
9619                 return tem;
9620             }
9621         }
9622
9623       /* If this is a comparison of a real constant with a PLUS_EXPR
9624          or a MINUS_EXPR of a real constant, we can convert it into a
9625          comparison with a revised real constant as long as no overflow
9626          occurs when unsafe_math_optimizations are enabled.  */
9627       if (flag_unsafe_math_optimizations
9628           && TREE_CODE (arg1) == REAL_CST
9629           && (TREE_CODE (arg0) == PLUS_EXPR
9630               || TREE_CODE (arg0) == MINUS_EXPR)
9631           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9632           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9633                                       ? MINUS_EXPR : PLUS_EXPR,
9634                                       arg1, TREE_OPERAND (arg0, 1), 0))
9635           && !TREE_OVERFLOW (tem))
9636         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9637
9638       /* Likewise, we can simplify a comparison of a real constant with
9639          a MINUS_EXPR whose first operand is also a real constant, i.e.
9640          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on 
9641          floating-point types only if -fassociative-math is set.  */
9642       if (flag_associative_math
9643           && TREE_CODE (arg1) == REAL_CST
9644           && TREE_CODE (arg0) == MINUS_EXPR
9645           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9646           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9647                                       arg1, 0))
9648           && !TREE_OVERFLOW (tem))
9649         return fold_build2_loc (loc, swap_tree_comparison (code), type,
9650                             TREE_OPERAND (arg0, 1), tem);
9651
9652       /* Fold comparisons against built-in math functions.  */
9653       if (TREE_CODE (arg1) == REAL_CST
9654           && flag_unsafe_math_optimizations
9655           && ! flag_errno_math)
9656         {
9657           enum built_in_function fcode = builtin_mathfn_code (arg0);
9658
9659           if (fcode != END_BUILTINS)
9660             {
9661               tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9662               if (tem != NULL_TREE)
9663                 return tem;
9664             }
9665         }
9666     }
9667
9668   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9669       && CONVERT_EXPR_P (arg0))
9670     {
9671       /* If we are widening one operand of an integer comparison,
9672          see if the other operand is similarly being widened.  Perhaps we
9673          can do the comparison in the narrower type.  */
9674       tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9675       if (tem)
9676         return tem;
9677
9678       /* Or if we are changing signedness.  */
9679       tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9680       if (tem)
9681         return tem;
9682     }
9683
9684   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9685      constant, we can simplify it.  */
9686   if (TREE_CODE (arg1) == INTEGER_CST
9687       && (TREE_CODE (arg0) == MIN_EXPR
9688           || TREE_CODE (arg0) == MAX_EXPR)
9689       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9690     {
9691       tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9692       if (tem)
9693         return tem;
9694     }
9695
9696   /* Simplify comparison of something with itself.  (For IEEE
9697      floating-point, we can only do some of these simplifications.)  */
9698   if (operand_equal_p (arg0, arg1, 0))
9699     {
9700       switch (code)
9701         {
9702         case EQ_EXPR:
9703           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9704               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9705             return constant_boolean_node (1, type);
9706           break;
9707
9708         case GE_EXPR:
9709         case LE_EXPR:
9710           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9711               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9712             return constant_boolean_node (1, type);
9713           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9714
9715         case NE_EXPR:
9716           /* For NE, we can only do this simplification if integer
9717              or we don't honor IEEE floating point NaNs.  */
9718           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9719               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9720             break;
9721           /* ... fall through ...  */
9722         case GT_EXPR:
9723         case LT_EXPR:
9724           return constant_boolean_node (0, type);
9725         default:
9726           gcc_unreachable ();
9727         }
9728     }
9729
9730   /* If we are comparing an expression that just has comparisons
9731      of two integer values, arithmetic expressions of those comparisons,
9732      and constants, we can simplify it.  There are only three cases
9733      to check: the two values can either be equal, the first can be
9734      greater, or the second can be greater.  Fold the expression for
9735      those three values.  Since each value must be 0 or 1, we have
9736      eight possibilities, each of which corresponds to the constant 0
9737      or 1 or one of the six possible comparisons.
9738
9739      This handles common cases like (a > b) == 0 but also handles
9740      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9741      occur in macroized code.  */
9742
9743   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9744     {
9745       tree cval1 = 0, cval2 = 0;
9746       int save_p = 0;
9747
9748       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9749           /* Don't handle degenerate cases here; they should already
9750              have been handled anyway.  */
9751           && cval1 != 0 && cval2 != 0
9752           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9753           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9754           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9755           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9756           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9757           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9758                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9759         {
9760           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9761           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9762
9763           /* We can't just pass T to eval_subst in case cval1 or cval2
9764              was the same as ARG1.  */
9765
9766           tree high_result
9767                 = fold_build2_loc (loc, code, type,
9768                                eval_subst (loc, arg0, cval1, maxval,
9769                                            cval2, minval),
9770                                arg1);
9771           tree equal_result
9772                 = fold_build2_loc (loc, code, type,
9773                                eval_subst (loc, arg0, cval1, maxval,
9774                                            cval2, maxval),
9775                                arg1);
9776           tree low_result
9777                 = fold_build2_loc (loc, code, type,
9778                                eval_subst (loc, arg0, cval1, minval,
9779                                            cval2, maxval),
9780                                arg1);
9781
9782           /* All three of these results should be 0 or 1.  Confirm they are.
9783              Then use those values to select the proper code to use.  */
9784
9785           if (TREE_CODE (high_result) == INTEGER_CST
9786               && TREE_CODE (equal_result) == INTEGER_CST
9787               && TREE_CODE (low_result) == INTEGER_CST)
9788             {
9789               /* Make a 3-bit mask with the high-order bit being the
9790                  value for `>', the next for '=', and the low for '<'.  */
9791               switch ((integer_onep (high_result) * 4)
9792                       + (integer_onep (equal_result) * 2)
9793                       + integer_onep (low_result))
9794                 {
9795                 case 0:
9796                   /* Always false.  */
9797                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9798                 case 1:
9799                   code = LT_EXPR;
9800                   break;
9801                 case 2:
9802                   code = EQ_EXPR;
9803                   break;
9804                 case 3:
9805                   code = LE_EXPR;
9806                   break;
9807                 case 4:
9808                   code = GT_EXPR;
9809                   break;
9810                 case 5:
9811                   code = NE_EXPR;
9812                   break;
9813                 case 6:
9814                   code = GE_EXPR;
9815                   break;
9816                 case 7:
9817                   /* Always true.  */
9818                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9819                 }
9820
9821               if (save_p)
9822                 {
9823                   tem = save_expr (build2 (code, type, cval1, cval2));
9824                   SET_EXPR_LOCATION (tem, loc);
9825                   return tem;
9826                 }
9827               return fold_build2_loc (loc, code, type, cval1, cval2);
9828             }
9829         }
9830     }
9831
9832   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9833      into a single range test.  */
9834   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9835        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9836       && TREE_CODE (arg1) == INTEGER_CST
9837       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9838       && !integer_zerop (TREE_OPERAND (arg0, 1))
9839       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9840       && !TREE_OVERFLOW (arg1))
9841     {
9842       tem = fold_div_compare (loc, code, type, arg0, arg1);
9843       if (tem != NULL_TREE)
9844         return tem;
9845     }
9846
9847   /* Fold ~X op ~Y as Y op X.  */
9848   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9849       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9850     {
9851       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9852       return fold_build2_loc (loc, code, type,
9853                           fold_convert_loc (loc, cmp_type,
9854                                             TREE_OPERAND (arg1, 0)),
9855                           TREE_OPERAND (arg0, 0));
9856     }
9857
9858   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9859   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9860       && TREE_CODE (arg1) == INTEGER_CST)
9861     {
9862       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9863       return fold_build2_loc (loc, swap_tree_comparison (code), type,
9864                           TREE_OPERAND (arg0, 0),
9865                           fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9866                                        fold_convert_loc (loc, cmp_type, arg1)));
9867     }
9868
9869   return NULL_TREE;
9870 }
9871
9872
9873 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9874    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9875    argument EXPR represents the expression "z" of type TYPE.  */
9876
9877 static tree
9878 fold_mult_zconjz (location_t loc, tree type, tree expr)
9879 {
9880   tree itype = TREE_TYPE (type);
9881   tree rpart, ipart, tem;
9882
9883   if (TREE_CODE (expr) == COMPLEX_EXPR)
9884     {
9885       rpart = TREE_OPERAND (expr, 0);
9886       ipart = TREE_OPERAND (expr, 1);
9887     }
9888   else if (TREE_CODE (expr) == COMPLEX_CST)
9889     {
9890       rpart = TREE_REALPART (expr);
9891       ipart = TREE_IMAGPART (expr);
9892     }
9893   else
9894     {
9895       expr = save_expr (expr);
9896       rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9897       ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9898     }
9899
9900   rpart = save_expr (rpart);
9901   ipart = save_expr (ipart);
9902   tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9903                      fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9904                      fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9905   return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9906                       fold_convert_loc (loc, itype, integer_zero_node));
9907 }
9908
9909
9910 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9911    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9912    guarantees that P and N have the same least significant log2(M) bits.
9913    N is not otherwise constrained.  In particular, N is not normalized to
9914    0 <= N < M as is common.  In general, the precise value of P is unknown.
9915    M is chosen as large as possible such that constant N can be determined.
9916
9917    Returns M and sets *RESIDUE to N.
9918
9919    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9920    account.  This is not always possible due to PR 35705.
9921  */
9922
9923 static unsigned HOST_WIDE_INT
9924 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9925                                  bool allow_func_align)
9926 {
9927   enum tree_code code;
9928
9929   *residue = 0;
9930
9931   code = TREE_CODE (expr);
9932   if (code == ADDR_EXPR)
9933     {
9934       expr = TREE_OPERAND (expr, 0);
9935       if (handled_component_p (expr))
9936         {
9937           HOST_WIDE_INT bitsize, bitpos;
9938           tree offset;
9939           enum machine_mode mode;
9940           int unsignedp, volatilep;
9941
9942           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9943                                       &mode, &unsignedp, &volatilep, false);
9944           *residue = bitpos / BITS_PER_UNIT;
9945           if (offset)
9946             {
9947               if (TREE_CODE (offset) == INTEGER_CST)
9948                 *residue += TREE_INT_CST_LOW (offset);
9949               else
9950                 /* We don't handle more complicated offset expressions.  */
9951                 return 1;
9952             }
9953         }
9954
9955       if (DECL_P (expr)
9956           && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9957         return DECL_ALIGN_UNIT (expr);
9958     }
9959   else if (code == POINTER_PLUS_EXPR)
9960     {
9961       tree op0, op1;
9962       unsigned HOST_WIDE_INT modulus;
9963       enum tree_code inner_code;
9964       
9965       op0 = TREE_OPERAND (expr, 0);
9966       STRIP_NOPS (op0);
9967       modulus = get_pointer_modulus_and_residue (op0, residue,
9968                                                  allow_func_align);
9969
9970       op1 = TREE_OPERAND (expr, 1);
9971       STRIP_NOPS (op1);
9972       inner_code = TREE_CODE (op1);
9973       if (inner_code == INTEGER_CST)
9974         {
9975           *residue += TREE_INT_CST_LOW (op1);
9976           return modulus;
9977         }
9978       else if (inner_code == MULT_EXPR)
9979         {
9980           op1 = TREE_OPERAND (op1, 1);
9981           if (TREE_CODE (op1) == INTEGER_CST)
9982             {
9983               unsigned HOST_WIDE_INT align;
9984               
9985               /* Compute the greatest power-of-2 divisor of op1.  */
9986               align = TREE_INT_CST_LOW (op1);
9987               align &= -align;
9988
9989               /* If align is non-zero and less than *modulus, replace
9990                  *modulus with align., If align is 0, then either op1 is 0
9991                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9992                  unsigned HOST_WIDE_INT.  In either case, no additional
9993                  constraint is imposed.  */
9994               if (align)
9995                 modulus = MIN (modulus, align);
9996
9997               return modulus;
9998             }
9999         }
10000     }
10001
10002     /* If we get here, we were unable to determine anything useful about the
10003        expression.  */
10004     return 1;
10005 }
10006
10007
10008 /* Fold a binary expression of code CODE and type TYPE with operands
10009    OP0 and OP1.  LOC is the location of the resulting expression.
10010    Return the folded expression if folding is successful.  Otherwise,
10011    return NULL_TREE.  */
10012
10013 tree
10014 fold_binary_loc (location_t loc,
10015              enum tree_code code, tree type, tree op0, tree op1)
10016 {
10017   enum tree_code_class kind = TREE_CODE_CLASS (code);
10018   tree arg0, arg1, tem;
10019   tree t1 = NULL_TREE;
10020   bool strict_overflow_p;
10021
10022   gcc_assert (IS_EXPR_CODE_CLASS (kind)
10023               && TREE_CODE_LENGTH (code) == 2
10024               && op0 != NULL_TREE
10025               && op1 != NULL_TREE);
10026
10027   arg0 = op0;
10028   arg1 = op1;
10029
10030   /* Strip any conversions that don't change the mode.  This is
10031      safe for every expression, except for a comparison expression
10032      because its signedness is derived from its operands.  So, in
10033      the latter case, only strip conversions that don't change the
10034      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
10035      preserved.
10036
10037      Note that this is done as an internal manipulation within the
10038      constant folder, in order to find the simplest representation
10039      of the arguments so that their form can be studied.  In any
10040      cases, the appropriate type conversions should be put back in
10041      the tree that will get out of the constant folder.  */
10042
10043   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10044     {
10045       STRIP_SIGN_NOPS (arg0);
10046       STRIP_SIGN_NOPS (arg1);
10047     }
10048   else
10049     {
10050       STRIP_NOPS (arg0);
10051       STRIP_NOPS (arg1);
10052     }
10053
10054   /* Note that TREE_CONSTANT isn't enough: static var addresses are
10055      constant but we can't do arithmetic on them.  */
10056   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10057       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
10058       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
10059       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
10060       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
10061       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
10062     {
10063       if (kind == tcc_binary)
10064         {
10065           /* Make sure type and arg0 have the same saturating flag.  */
10066           gcc_assert (TYPE_SATURATING (type)
10067                       == TYPE_SATURATING (TREE_TYPE (arg0)));
10068           tem = const_binop (code, arg0, arg1, 0);
10069         }
10070       else if (kind == tcc_comparison)
10071         tem = fold_relational_const (code, type, arg0, arg1);
10072       else
10073         tem = NULL_TREE;
10074
10075       if (tem != NULL_TREE)
10076         {
10077           if (TREE_TYPE (tem) != type)
10078             tem = fold_convert_loc (loc, type, tem);
10079           return tem;
10080         }
10081     }
10082
10083   /* If this is a commutative operation, and ARG0 is a constant, move it
10084      to ARG1 to reduce the number of tests below.  */
10085   if (commutative_tree_code (code)
10086       && tree_swap_operands_p (arg0, arg1, true))
10087     return fold_build2_loc (loc, code, type, op1, op0);
10088
10089   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10090
10091      First check for cases where an arithmetic operation is applied to a
10092      compound, conditional, or comparison operation.  Push the arithmetic
10093      operation inside the compound or conditional to see if any folding
10094      can then be done.  Convert comparison to conditional for this purpose.
10095      The also optimizes non-constant cases that used to be done in
10096      expand_expr.
10097
10098      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10099      one of the operands is a comparison and the other is a comparison, a
10100      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
10101      code below would make the expression more complex.  Change it to a
10102      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
10103      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
10104
10105   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
10106        || code == EQ_EXPR || code == NE_EXPR)
10107       && ((truth_value_p (TREE_CODE (arg0))
10108            && (truth_value_p (TREE_CODE (arg1))
10109                || (TREE_CODE (arg1) == BIT_AND_EXPR
10110                    && integer_onep (TREE_OPERAND (arg1, 1)))))
10111           || (truth_value_p (TREE_CODE (arg1))
10112               && (truth_value_p (TREE_CODE (arg0))
10113                   || (TREE_CODE (arg0) == BIT_AND_EXPR
10114                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
10115     {
10116       tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
10117                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
10118                          : TRUTH_XOR_EXPR,
10119                          boolean_type_node,
10120                          fold_convert_loc (loc, boolean_type_node, arg0),
10121                          fold_convert_loc (loc, boolean_type_node, arg1));
10122
10123       if (code == EQ_EXPR)
10124         tem = invert_truthvalue_loc (loc, tem);
10125
10126       return fold_convert_loc (loc, type, tem);
10127     }
10128
10129   if (TREE_CODE_CLASS (code) == tcc_binary
10130       || TREE_CODE_CLASS (code) == tcc_comparison)
10131     {
10132       if (TREE_CODE (arg0) == COMPOUND_EXPR)
10133         {
10134           tem = fold_build2_loc (loc, code, type,
10135                              fold_convert_loc (loc, TREE_TYPE (op0),
10136                                                TREE_OPERAND (arg0, 1)), op1);
10137           protected_set_expr_location (tem, loc);
10138           tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), tem);
10139           goto fold_binary_exit;
10140         }
10141       if (TREE_CODE (arg1) == COMPOUND_EXPR
10142           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10143         {
10144           tem = fold_build2_loc (loc, code, type, op0,
10145                              fold_convert_loc (loc, TREE_TYPE (op1),
10146                                                TREE_OPERAND (arg1, 1)));
10147           protected_set_expr_location (tem, loc);
10148           tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), tem);
10149           goto fold_binary_exit;
10150         }
10151
10152       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
10153         {
10154           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10155                                                      arg0, arg1, 
10156                                                      /*cond_first_p=*/1);
10157           if (tem != NULL_TREE)
10158             return tem;
10159         }
10160
10161       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
10162         {
10163           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10164                                                      arg1, arg0, 
10165                                                      /*cond_first_p=*/0);
10166           if (tem != NULL_TREE)
10167             return tem;
10168         }
10169     }
10170
10171   switch (code)
10172     {
10173     case POINTER_PLUS_EXPR:
10174       /* 0 +p index -> (type)index */
10175       if (integer_zerop (arg0))
10176         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10177
10178       /* PTR +p 0 -> PTR */
10179       if (integer_zerop (arg1))
10180         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10181
10182       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
10183       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10184            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
10185         return fold_convert_loc (loc, type,
10186                                  fold_build2_loc (loc, PLUS_EXPR, sizetype,
10187                                               fold_convert_loc (loc, sizetype,
10188                                                                 arg1),
10189                                               fold_convert_loc (loc, sizetype,
10190                                                                 arg0)));
10191
10192       /* index +p PTR -> PTR +p index */
10193       if (POINTER_TYPE_P (TREE_TYPE (arg1))
10194           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
10195         return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
10196                             fold_convert_loc (loc, type, arg1),
10197                             fold_convert_loc (loc, sizetype, arg0));
10198
10199       /* (PTR +p B) +p A -> PTR +p (B + A) */
10200       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10201         {
10202           tree inner;
10203           tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
10204           tree arg00 = TREE_OPERAND (arg0, 0);
10205           inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
10206                                arg01, fold_convert_loc (loc, sizetype, arg1));
10207           return fold_convert_loc (loc, type,
10208                                    fold_build2_loc (loc, POINTER_PLUS_EXPR,
10209                                                 TREE_TYPE (arg00),
10210                                                 arg00, inner));
10211         }
10212
10213       /* PTR_CST +p CST -> CST1 */
10214       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10215         return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
10216                             fold_convert_loc (loc, type, arg1));
10217
10218      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
10219         of the array.  Loop optimizer sometimes produce this type of
10220         expressions.  */
10221       if (TREE_CODE (arg0) == ADDR_EXPR)
10222         {
10223           tem = try_move_mult_to_index (loc, arg0,
10224                                         fold_convert_loc (loc, sizetype, arg1));
10225           if (tem)
10226             return fold_convert_loc (loc, type, tem);
10227         }
10228
10229       return NULL_TREE;
10230
10231     case PLUS_EXPR:
10232       /* A + (-B) -> A - B */
10233       if (TREE_CODE (arg1) == NEGATE_EXPR)
10234         return fold_build2_loc (loc, MINUS_EXPR, type,
10235                             fold_convert_loc (loc, type, arg0),
10236                             fold_convert_loc (loc, type,
10237                                               TREE_OPERAND (arg1, 0)));
10238       /* (-A) + B -> B - A */
10239       if (TREE_CODE (arg0) == NEGATE_EXPR
10240           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
10241         return fold_build2_loc (loc, MINUS_EXPR, type,
10242                             fold_convert_loc (loc, type, arg1),
10243                             fold_convert_loc (loc, type,
10244                                               TREE_OPERAND (arg0, 0)));
10245
10246       if (INTEGRAL_TYPE_P (type))
10247         {
10248           /* Convert ~A + 1 to -A.  */
10249           if (TREE_CODE (arg0) == BIT_NOT_EXPR
10250               && integer_onep (arg1))
10251             return fold_build1_loc (loc, NEGATE_EXPR, type,
10252                                 fold_convert_loc (loc, type,
10253                                                   TREE_OPERAND (arg0, 0)));
10254
10255           /* ~X + X is -1.  */
10256           if (TREE_CODE (arg0) == BIT_NOT_EXPR
10257               && !TYPE_OVERFLOW_TRAPS (type))
10258             {
10259               tree tem = TREE_OPERAND (arg0, 0);
10260
10261               STRIP_NOPS (tem);
10262               if (operand_equal_p (tem, arg1, 0))
10263                 {
10264                   t1 = build_int_cst_type (type, -1);
10265                   return omit_one_operand_loc (loc, type, t1, arg1);
10266                 }
10267             }
10268
10269           /* X + ~X is -1.  */
10270           if (TREE_CODE (arg1) == BIT_NOT_EXPR
10271               && !TYPE_OVERFLOW_TRAPS (type))
10272             {
10273               tree tem = TREE_OPERAND (arg1, 0);
10274
10275               STRIP_NOPS (tem);
10276               if (operand_equal_p (arg0, tem, 0))
10277                 {
10278                   t1 = build_int_cst_type (type, -1);
10279                   return omit_one_operand_loc (loc, type, t1, arg0);
10280                 }
10281             }
10282
10283           /* X + (X / CST) * -CST is X % CST.  */
10284           if (TREE_CODE (arg1) == MULT_EXPR
10285               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10286               && operand_equal_p (arg0,
10287                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
10288             {
10289               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
10290               tree cst1 = TREE_OPERAND (arg1, 1);
10291               tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
10292                                       cst1, cst0);
10293               if (sum && integer_zerop (sum))
10294                 return fold_convert_loc (loc, type,
10295                                          fold_build2_loc (loc, TRUNC_MOD_EXPR,
10296                                                       TREE_TYPE (arg0), arg0,
10297                                                       cst0));
10298             }
10299         }
10300
10301       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
10302          same or one.  Make sure type is not saturating.
10303          fold_plusminus_mult_expr will re-associate.  */
10304       if ((TREE_CODE (arg0) == MULT_EXPR
10305            || TREE_CODE (arg1) == MULT_EXPR)
10306           && !TYPE_SATURATING (type)
10307           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10308         {
10309           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10310           if (tem)
10311             return tem;
10312         }
10313
10314       if (! FLOAT_TYPE_P (type))
10315         {
10316           if (integer_zerop (arg1))
10317             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10318
10319           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
10320              with a constant, and the two constants have no bits in common,
10321              we should treat this as a BIT_IOR_EXPR since this may produce more
10322              simplifications.  */
10323           if (TREE_CODE (arg0) == BIT_AND_EXPR
10324               && TREE_CODE (arg1) == BIT_AND_EXPR
10325               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10326               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10327               && integer_zerop (const_binop (BIT_AND_EXPR,
10328                                              TREE_OPERAND (arg0, 1),
10329                                              TREE_OPERAND (arg1, 1), 0)))
10330             {
10331               code = BIT_IOR_EXPR;
10332               goto bit_ior;
10333             }
10334
10335           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10336              (plus (plus (mult) (mult)) (foo)) so that we can
10337              take advantage of the factoring cases below.  */
10338           if (((TREE_CODE (arg0) == PLUS_EXPR
10339                 || TREE_CODE (arg0) == MINUS_EXPR)
10340                && TREE_CODE (arg1) == MULT_EXPR)
10341               || ((TREE_CODE (arg1) == PLUS_EXPR
10342                    || TREE_CODE (arg1) == MINUS_EXPR)
10343                   && TREE_CODE (arg0) == MULT_EXPR))
10344             {
10345               tree parg0, parg1, parg, marg;
10346               enum tree_code pcode;
10347
10348               if (TREE_CODE (arg1) == MULT_EXPR)
10349                 parg = arg0, marg = arg1;
10350               else
10351                 parg = arg1, marg = arg0;
10352               pcode = TREE_CODE (parg);
10353               parg0 = TREE_OPERAND (parg, 0);
10354               parg1 = TREE_OPERAND (parg, 1);
10355               STRIP_NOPS (parg0);
10356               STRIP_NOPS (parg1);
10357
10358               if (TREE_CODE (parg0) == MULT_EXPR
10359                   && TREE_CODE (parg1) != MULT_EXPR)
10360                 return fold_build2_loc (loc, pcode, type,
10361                                     fold_build2_loc (loc, PLUS_EXPR, type,
10362                                                  fold_convert_loc (loc, type,
10363                                                                    parg0),
10364                                                  fold_convert_loc (loc, type,
10365                                                                    marg)),
10366                                     fold_convert_loc (loc, type, parg1));
10367               if (TREE_CODE (parg0) != MULT_EXPR
10368                   && TREE_CODE (parg1) == MULT_EXPR)
10369                 return
10370                   fold_build2_loc (loc, PLUS_EXPR, type,
10371                                fold_convert_loc (loc, type, parg0),
10372                                fold_build2_loc (loc, pcode, type,
10373                                             fold_convert_loc (loc, type, marg),
10374                                             fold_convert_loc (loc, type,
10375                                                               parg1)));
10376             }
10377         }
10378       else
10379         {
10380           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
10381           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10382             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10383
10384           /* Likewise if the operands are reversed.  */
10385           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10386             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10387
10388           /* Convert X + -C into X - C.  */
10389           if (TREE_CODE (arg1) == REAL_CST
10390               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10391             {
10392               tem = fold_negate_const (arg1, type);
10393               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10394                 return fold_build2_loc (loc, MINUS_EXPR, type,
10395                                     fold_convert_loc (loc, type, arg0),
10396                                     fold_convert_loc (loc, type, tem));
10397             }
10398
10399           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10400              to __complex__ ( x, y ).  This is not the same for SNaNs or
10401              if signed zeros are involved.  */
10402           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10403               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10404               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10405             {
10406               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10407               tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10408               tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10409               bool arg0rz = false, arg0iz = false;
10410               if ((arg0r && (arg0rz = real_zerop (arg0r)))
10411                   || (arg0i && (arg0iz = real_zerop (arg0i))))
10412                 {
10413                   tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10414                   tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10415                   if (arg0rz && arg1i && real_zerop (arg1i))
10416                     {
10417                       tree rp = arg1r ? arg1r
10418                                   : build1 (REALPART_EXPR, rtype, arg1);
10419                       tree ip = arg0i ? arg0i
10420                                   : build1 (IMAGPART_EXPR, rtype, arg0);
10421                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10422                     }
10423                   else if (arg0iz && arg1r && real_zerop (arg1r))
10424                     {
10425                       tree rp = arg0r ? arg0r
10426                                   : build1 (REALPART_EXPR, rtype, arg0);
10427                       tree ip = arg1i ? arg1i
10428                                   : build1 (IMAGPART_EXPR, rtype, arg1);
10429                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10430                     }
10431                 }
10432             }
10433
10434           if (flag_unsafe_math_optimizations
10435               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10436               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10437               && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10438             return tem;
10439
10440           /* Convert x+x into x*2.0.  */
10441           if (operand_equal_p (arg0, arg1, 0)
10442               && SCALAR_FLOAT_TYPE_P (type))
10443             return fold_build2_loc (loc, MULT_EXPR, type, arg0,
10444                                 build_real (type, dconst2));
10445
10446           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.  
10447              We associate floats only if the user has specified
10448              -fassociative-math.  */
10449           if (flag_associative_math
10450               && TREE_CODE (arg1) == PLUS_EXPR
10451               && TREE_CODE (arg0) != MULT_EXPR)
10452             {
10453               tree tree10 = TREE_OPERAND (arg1, 0);
10454               tree tree11 = TREE_OPERAND (arg1, 1);
10455               if (TREE_CODE (tree11) == MULT_EXPR
10456                   && TREE_CODE (tree10) == MULT_EXPR)
10457                 {
10458                   tree tree0;
10459                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
10460                   return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
10461                 }
10462             }
10463           /* Convert (b*c + d*e) + a into b*c + (d*e +a).  
10464              We associate floats only if the user has specified
10465              -fassociative-math.  */
10466           if (flag_associative_math
10467               && TREE_CODE (arg0) == PLUS_EXPR
10468               && TREE_CODE (arg1) != MULT_EXPR)
10469             {
10470               tree tree00 = TREE_OPERAND (arg0, 0);
10471               tree tree01 = TREE_OPERAND (arg0, 1);
10472               if (TREE_CODE (tree01) == MULT_EXPR
10473                   && TREE_CODE (tree00) == MULT_EXPR)
10474                 {
10475                   tree tree0;
10476                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
10477                   return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
10478                 }
10479             }
10480         }
10481
10482      bit_rotate:
10483       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10484          is a rotate of A by C1 bits.  */
10485       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10486          is a rotate of A by B bits.  */
10487       {
10488         enum tree_code code0, code1;
10489         tree rtype;
10490         code0 = TREE_CODE (arg0);
10491         code1 = TREE_CODE (arg1);
10492         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10493              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10494             && operand_equal_p (TREE_OPERAND (arg0, 0),
10495                                 TREE_OPERAND (arg1, 0), 0)
10496             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10497                 TYPE_UNSIGNED (rtype))
10498             /* Only create rotates in complete modes.  Other cases are not
10499                expanded properly.  */
10500             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10501           {
10502             tree tree01, tree11;
10503             enum tree_code code01, code11;
10504
10505             tree01 = TREE_OPERAND (arg0, 1);
10506             tree11 = TREE_OPERAND (arg1, 1);
10507             STRIP_NOPS (tree01);
10508             STRIP_NOPS (tree11);
10509             code01 = TREE_CODE (tree01);
10510             code11 = TREE_CODE (tree11);
10511             if (code01 == INTEGER_CST
10512                 && code11 == INTEGER_CST
10513                 && TREE_INT_CST_HIGH (tree01) == 0
10514                 && TREE_INT_CST_HIGH (tree11) == 0
10515                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10516                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10517               {
10518                 tem = build2 (LROTATE_EXPR,
10519                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
10520                               TREE_OPERAND (arg0, 0),
10521                               code0 == LSHIFT_EXPR
10522                               ? tree01 : tree11);
10523                 SET_EXPR_LOCATION (tem, loc);
10524                 return fold_convert_loc (loc, type, tem);
10525               }
10526             else if (code11 == MINUS_EXPR)
10527               {
10528                 tree tree110, tree111;
10529                 tree110 = TREE_OPERAND (tree11, 0);
10530                 tree111 = TREE_OPERAND (tree11, 1);
10531                 STRIP_NOPS (tree110);
10532                 STRIP_NOPS (tree111);
10533                 if (TREE_CODE (tree110) == INTEGER_CST
10534                     && 0 == compare_tree_int (tree110,
10535                                               TYPE_PRECISION
10536                                               (TREE_TYPE (TREE_OPERAND
10537                                                           (arg0, 0))))
10538                     && operand_equal_p (tree01, tree111, 0))
10539                   return
10540                     fold_convert_loc (loc, type,
10541                                       build2 ((code0 == LSHIFT_EXPR
10542                                                ? LROTATE_EXPR
10543                                                : RROTATE_EXPR),
10544                                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
10545                                               TREE_OPERAND (arg0, 0), tree01));
10546               }
10547             else if (code01 == MINUS_EXPR)
10548               {
10549                 tree tree010, tree011;
10550                 tree010 = TREE_OPERAND (tree01, 0);
10551                 tree011 = TREE_OPERAND (tree01, 1);
10552                 STRIP_NOPS (tree010);
10553                 STRIP_NOPS (tree011);
10554                 if (TREE_CODE (tree010) == INTEGER_CST
10555                     && 0 == compare_tree_int (tree010,
10556                                               TYPE_PRECISION
10557                                               (TREE_TYPE (TREE_OPERAND
10558                                                           (arg0, 0))))
10559                     && operand_equal_p (tree11, tree011, 0))
10560                     return fold_convert_loc
10561                       (loc, type,
10562                        build2 ((code0 != LSHIFT_EXPR
10563                                 ? LROTATE_EXPR
10564                                 : RROTATE_EXPR),
10565                                TREE_TYPE (TREE_OPERAND (arg0, 0)),
10566                                TREE_OPERAND (arg0, 0), tree11));
10567               }
10568           }
10569       }
10570
10571     associate:
10572       /* In most languages, can't associate operations on floats through
10573          parentheses.  Rather than remember where the parentheses were, we
10574          don't associate floats at all, unless the user has specified
10575          -fassociative-math.
10576          And, we need to make sure type is not saturating.  */
10577
10578       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10579           && !TYPE_SATURATING (type))
10580         {
10581           tree var0, con0, lit0, minus_lit0;
10582           tree var1, con1, lit1, minus_lit1;
10583           bool ok = true;
10584
10585           /* Split both trees into variables, constants, and literals.  Then
10586              associate each group together, the constants with literals,
10587              then the result with variables.  This increases the chances of
10588              literals being recombined later and of generating relocatable
10589              expressions for the sum of a constant and literal.  */
10590           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10591           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10592                              code == MINUS_EXPR);
10593
10594           /* With undefined overflow we can only associate constants
10595              with one variable.  */
10596           if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10597                || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10598               && var0 && var1)
10599             {
10600               tree tmp0 = var0;
10601               tree tmp1 = var1;
10602
10603               if (TREE_CODE (tmp0) == NEGATE_EXPR)
10604                 tmp0 = TREE_OPERAND (tmp0, 0);
10605               if (TREE_CODE (tmp1) == NEGATE_EXPR)
10606                 tmp1 = TREE_OPERAND (tmp1, 0);
10607               /* The only case we can still associate with two variables
10608                  is if they are the same, modulo negation.  */
10609               if (!operand_equal_p (tmp0, tmp1, 0))
10610                 ok = false;
10611             }
10612
10613           /* Only do something if we found more than two objects.  Otherwise,
10614              nothing has changed and we risk infinite recursion.  */
10615           if (ok
10616               && (2 < ((var0 != 0) + (var1 != 0)
10617                        + (con0 != 0) + (con1 != 0)
10618                        + (lit0 != 0) + (lit1 != 0)
10619                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10620             {
10621               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
10622               if (code == MINUS_EXPR)
10623                 code = PLUS_EXPR;
10624
10625               var0 = associate_trees (loc, var0, var1, code, type);
10626               con0 = associate_trees (loc, con0, con1, code, type);
10627               lit0 = associate_trees (loc, lit0, lit1, code, type);
10628               minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
10629
10630               /* Preserve the MINUS_EXPR if the negative part of the literal is
10631                  greater than the positive part.  Otherwise, the multiplicative
10632                  folding code (i.e extract_muldiv) may be fooled in case
10633                  unsigned constants are subtracted, like in the following
10634                  example: ((X*2 + 4) - 8U)/2.  */
10635               if (minus_lit0 && lit0)
10636                 {
10637                   if (TREE_CODE (lit0) == INTEGER_CST
10638                       && TREE_CODE (minus_lit0) == INTEGER_CST
10639                       && tree_int_cst_lt (lit0, minus_lit0))
10640                     {
10641                       minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10642                                                     MINUS_EXPR, type);
10643                       lit0 = 0;
10644                     }
10645                   else
10646                     {
10647                       lit0 = associate_trees (loc, lit0, minus_lit0,
10648                                               MINUS_EXPR, type);
10649                       minus_lit0 = 0;
10650                     }
10651                 }
10652               if (minus_lit0)
10653                 {
10654                   if (con0 == 0)
10655                     return
10656                       fold_convert_loc (loc, type,
10657                                         associate_trees (loc, var0, minus_lit0,
10658                                                          MINUS_EXPR, type));
10659                   else
10660                     {
10661                       con0 = associate_trees (loc, con0, minus_lit0,
10662                                               MINUS_EXPR, type);
10663                       return
10664                         fold_convert_loc (loc, type,
10665                                           associate_trees (loc, var0, con0,
10666                                                            PLUS_EXPR, type));
10667                     }
10668                 }
10669
10670               con0 = associate_trees (loc, con0, lit0, code, type);
10671               return
10672                 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10673                                                               code, type));
10674             }
10675         }
10676
10677       return NULL_TREE;
10678
10679     case MINUS_EXPR:
10680       /* Pointer simplifications for subtraction, simple reassociations. */
10681       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10682         {
10683           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10684           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10685               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10686             {
10687               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10688               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10689               tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10690               tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10691               return fold_build2_loc (loc, PLUS_EXPR, type,
10692                                   fold_build2_loc (loc, MINUS_EXPR, type,
10693                                                arg00, arg10),
10694                                   fold_build2_loc (loc, MINUS_EXPR, type,
10695                                                arg01, arg11));
10696             }
10697           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10698           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10699             {
10700               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10701               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10702               tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10703                                       fold_convert_loc (loc, type, arg1));
10704               if (tmp)
10705                 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10706             }
10707         }
10708       /* A - (-B) -> A + B */
10709       if (TREE_CODE (arg1) == NEGATE_EXPR)
10710         return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10711                             fold_convert_loc (loc, type,
10712                                               TREE_OPERAND (arg1, 0)));
10713       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10714       if (TREE_CODE (arg0) == NEGATE_EXPR
10715           && (FLOAT_TYPE_P (type)
10716               || INTEGRAL_TYPE_P (type))
10717           && negate_expr_p (arg1)
10718           && reorder_operands_p (arg0, arg1))
10719         return fold_build2_loc (loc, MINUS_EXPR, type,
10720                             fold_convert_loc (loc, type,
10721                                               negate_expr (arg1)),
10722                             fold_convert_loc (loc, type,
10723                                               TREE_OPERAND (arg0, 0)));
10724       /* Convert -A - 1 to ~A.  */
10725       if (INTEGRAL_TYPE_P (type)
10726           && TREE_CODE (arg0) == NEGATE_EXPR
10727           && integer_onep (arg1)
10728           && !TYPE_OVERFLOW_TRAPS (type))
10729         return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10730                             fold_convert_loc (loc, type,
10731                                               TREE_OPERAND (arg0, 0)));
10732
10733       /* Convert -1 - A to ~A.  */
10734       if (INTEGRAL_TYPE_P (type)
10735           && integer_all_onesp (arg0))
10736         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10737
10738
10739       /* X - (X / CST) * CST is X % CST.  */
10740       if (INTEGRAL_TYPE_P (type)
10741           && TREE_CODE (arg1) == MULT_EXPR
10742           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10743           && operand_equal_p (arg0,
10744                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10745           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10746                               TREE_OPERAND (arg1, 1), 0))
10747         return
10748           fold_convert_loc (loc, type,
10749                             fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10750                                          arg0, TREE_OPERAND (arg1, 1)));
10751
10752       if (! FLOAT_TYPE_P (type))
10753         {
10754           if (integer_zerop (arg0))
10755             return negate_expr (fold_convert_loc (loc, type, arg1));
10756           if (integer_zerop (arg1))
10757             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10758
10759           /* Fold A - (A & B) into ~B & A.  */
10760           if (!TREE_SIDE_EFFECTS (arg0)
10761               && TREE_CODE (arg1) == BIT_AND_EXPR)
10762             {
10763               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10764                 {
10765                   tree arg10 = fold_convert_loc (loc, type,
10766                                                  TREE_OPERAND (arg1, 0));
10767                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10768                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10769                                                    type, arg10),
10770                                       fold_convert_loc (loc, type, arg0));
10771                 }
10772               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10773                 {
10774                   tree arg11 = fold_convert_loc (loc,
10775                                                  type, TREE_OPERAND (arg1, 1));
10776                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10777                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10778                                                    type, arg11),
10779                                       fold_convert_loc (loc, type, arg0));
10780                 }
10781             }
10782
10783           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10784              any power of 2 minus 1.  */
10785           if (TREE_CODE (arg0) == BIT_AND_EXPR
10786               && TREE_CODE (arg1) == BIT_AND_EXPR
10787               && operand_equal_p (TREE_OPERAND (arg0, 0),
10788                                   TREE_OPERAND (arg1, 0), 0))
10789             {
10790               tree mask0 = TREE_OPERAND (arg0, 1);
10791               tree mask1 = TREE_OPERAND (arg1, 1);
10792               tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10793
10794               if (operand_equal_p (tem, mask1, 0))
10795                 {
10796                   tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10797                                      TREE_OPERAND (arg0, 0), mask1);
10798                   return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10799                 }
10800             }
10801         }
10802
10803       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10804       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10805         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10806
10807       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10808          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10809          (-ARG1 + ARG0) reduces to -ARG1.  */
10810       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10811         return negate_expr (fold_convert_loc (loc, type, arg1));
10812
10813       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10814          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10815          signed zeros are involved.  */
10816       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10817           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10818           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10819         {
10820           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10821           tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10822           tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10823           bool arg0rz = false, arg0iz = false;
10824           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10825               || (arg0i && (arg0iz = real_zerop (arg0i))))
10826             {
10827               tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10828               tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10829               if (arg0rz && arg1i && real_zerop (arg1i))
10830                 {
10831                   tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10832                                          arg1r ? arg1r
10833                                          : build1 (REALPART_EXPR, rtype, arg1));
10834                   tree ip = arg0i ? arg0i
10835                     : build1 (IMAGPART_EXPR, rtype, arg0);
10836                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10837                 }
10838               else if (arg0iz && arg1r && real_zerop (arg1r))
10839                 {
10840                   tree rp = arg0r ? arg0r
10841                     : build1 (REALPART_EXPR, rtype, arg0);
10842                   tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10843                                          arg1i ? arg1i
10844                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10845                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10846                 }
10847             }
10848         }
10849
10850       /* Fold &x - &x.  This can happen from &x.foo - &x.
10851          This is unsafe for certain floats even in non-IEEE formats.
10852          In IEEE, it is unsafe because it does wrong for NaNs.
10853          Also note that operand_equal_p is always false if an operand
10854          is volatile.  */
10855
10856       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10857           && operand_equal_p (arg0, arg1, 0))
10858         return fold_convert_loc (loc, type, integer_zero_node);
10859
10860       /* A - B -> A + (-B) if B is easily negatable.  */
10861       if (negate_expr_p (arg1)
10862           && ((FLOAT_TYPE_P (type)
10863                /* Avoid this transformation if B is a positive REAL_CST.  */
10864                && (TREE_CODE (arg1) != REAL_CST
10865                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10866               || INTEGRAL_TYPE_P (type)))
10867         return fold_build2_loc (loc, PLUS_EXPR, type,
10868                             fold_convert_loc (loc, type, arg0),
10869                             fold_convert_loc (loc, type,
10870                                               negate_expr (arg1)));
10871
10872       /* Try folding difference of addresses.  */
10873       {
10874         HOST_WIDE_INT diff;
10875
10876         if ((TREE_CODE (arg0) == ADDR_EXPR
10877              || TREE_CODE (arg1) == ADDR_EXPR)
10878             && ptr_difference_const (arg0, arg1, &diff))
10879           return build_int_cst_type (type, diff);
10880       }
10881
10882       /* Fold &a[i] - &a[j] to i-j.  */
10883       if (TREE_CODE (arg0) == ADDR_EXPR
10884           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10885           && TREE_CODE (arg1) == ADDR_EXPR
10886           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10887         {
10888           tree aref0 = TREE_OPERAND (arg0, 0);
10889           tree aref1 = TREE_OPERAND (arg1, 0);
10890           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10891                                TREE_OPERAND (aref1, 0), 0))
10892             {
10893               tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10894               tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10895               tree esz = array_ref_element_size (aref0);
10896               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10897               return fold_build2_loc (loc, MULT_EXPR, type, diff,
10898                                   fold_convert_loc (loc, type, esz));
10899                                   
10900             }
10901         }
10902
10903       if (FLOAT_TYPE_P (type)
10904           && flag_unsafe_math_optimizations
10905           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10906           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10907           && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10908         return tem;
10909
10910       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10911          same or one.  Make sure type is not saturating.
10912          fold_plusminus_mult_expr will re-associate.  */
10913       if ((TREE_CODE (arg0) == MULT_EXPR
10914            || TREE_CODE (arg1) == MULT_EXPR)
10915           && !TYPE_SATURATING (type)
10916           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10917         {
10918           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10919           if (tem)
10920             return tem;
10921         }
10922
10923       goto associate;
10924
10925     case MULT_EXPR:
10926       /* (-A) * (-B) -> A * B  */
10927       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10928         return fold_build2_loc (loc, MULT_EXPR, type,
10929                             fold_convert_loc (loc, type,
10930                                               TREE_OPERAND (arg0, 0)),
10931                             fold_convert_loc (loc, type,
10932                                               negate_expr (arg1)));
10933       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10934         return fold_build2_loc (loc, MULT_EXPR, type,
10935                             fold_convert_loc (loc, type,
10936                                               negate_expr (arg0)),
10937                             fold_convert_loc (loc, type,
10938                                               TREE_OPERAND (arg1, 0)));
10939
10940       if (! FLOAT_TYPE_P (type))
10941         {
10942           if (integer_zerop (arg1))
10943             return omit_one_operand_loc (loc, type, arg1, arg0);
10944           if (integer_onep (arg1))
10945             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10946           /* Transform x * -1 into -x.  Make sure to do the negation
10947              on the original operand with conversions not stripped
10948              because we can only strip non-sign-changing conversions.  */
10949           if (integer_all_onesp (arg1))
10950             return fold_convert_loc (loc, type, negate_expr (op0));
10951           /* Transform x * -C into -x * C if x is easily negatable.  */
10952           if (TREE_CODE (arg1) == INTEGER_CST
10953               && tree_int_cst_sgn (arg1) == -1
10954               && negate_expr_p (arg0)
10955               && (tem = negate_expr (arg1)) != arg1
10956               && !TREE_OVERFLOW (tem))
10957             return fold_build2_loc (loc, MULT_EXPR, type,
10958                                 fold_convert_loc (loc, type,
10959                                                   negate_expr (arg0)),
10960                                 tem);
10961
10962           /* (a * (1 << b)) is (a << b)  */
10963           if (TREE_CODE (arg1) == LSHIFT_EXPR
10964               && integer_onep (TREE_OPERAND (arg1, 0)))
10965             return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10966                                 TREE_OPERAND (arg1, 1));
10967           if (TREE_CODE (arg0) == LSHIFT_EXPR
10968               && integer_onep (TREE_OPERAND (arg0, 0)))
10969             return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10970                                 TREE_OPERAND (arg0, 1));
10971
10972           /* (A + A) * C -> A * 2 * C  */
10973           if (TREE_CODE (arg0) == PLUS_EXPR
10974               && TREE_CODE (arg1) == INTEGER_CST
10975               && operand_equal_p (TREE_OPERAND (arg0, 0),
10976                                   TREE_OPERAND (arg0, 1), 0))
10977             return fold_build2_loc (loc, MULT_EXPR, type,
10978                                 omit_one_operand_loc (loc, type,
10979                                                   TREE_OPERAND (arg0, 0),
10980                                                   TREE_OPERAND (arg0, 1)),
10981                                 fold_build2_loc (loc, MULT_EXPR, type,
10982                                              build_int_cst (type, 2) , arg1));
10983
10984           strict_overflow_p = false;
10985           if (TREE_CODE (arg1) == INTEGER_CST
10986               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10987                                              &strict_overflow_p)))
10988             {
10989               if (strict_overflow_p)
10990                 fold_overflow_warning (("assuming signed overflow does not "
10991                                         "occur when simplifying "
10992                                         "multiplication"),
10993                                        WARN_STRICT_OVERFLOW_MISC);
10994               return fold_convert_loc (loc, type, tem);
10995             }
10996
10997           /* Optimize z * conj(z) for integer complex numbers.  */
10998           if (TREE_CODE (arg0) == CONJ_EXPR
10999               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11000             return fold_mult_zconjz (loc, type, arg1);
11001           if (TREE_CODE (arg1) == CONJ_EXPR
11002               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11003             return fold_mult_zconjz (loc, type, arg0);
11004         }
11005       else
11006         {
11007           /* Maybe fold x * 0 to 0.  The expressions aren't the same
11008              when x is NaN, since x * 0 is also NaN.  Nor are they the
11009              same in modes with signed zeros, since multiplying a
11010              negative value by 0 gives -0, not +0.  */
11011           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11012               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11013               && real_zerop (arg1))
11014             return omit_one_operand_loc (loc, type, arg1, arg0);
11015           /* In IEEE floating point, x*1 is not equivalent to x for snans.
11016              Likewise for complex arithmetic with signed zeros.  */
11017           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11018               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11019                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11020               && real_onep (arg1))
11021             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11022
11023           /* Transform x * -1.0 into -x.  */
11024           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11025               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11026                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11027               && real_minus_onep (arg1))
11028             return fold_convert_loc (loc, type, negate_expr (arg0));
11029
11030           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
11031              the result for floating point types due to rounding so it is applied
11032              only if -fassociative-math was specify.  */
11033           if (flag_associative_math
11034               && TREE_CODE (arg0) == RDIV_EXPR
11035               && TREE_CODE (arg1) == REAL_CST
11036               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
11037             {
11038               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
11039                                       arg1, 0);
11040               if (tem)
11041                 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11042                                     TREE_OPERAND (arg0, 1));
11043             }
11044
11045           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
11046           if (operand_equal_p (arg0, arg1, 0))
11047             {
11048               tree tem = fold_strip_sign_ops (arg0);
11049               if (tem != NULL_TREE)
11050                 {
11051                   tem = fold_convert_loc (loc, type, tem);
11052                   return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
11053                 }
11054             }
11055
11056           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11057              This is not the same for NaNs or if signed zeros are
11058              involved.  */
11059           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11060               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11061               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11062               && TREE_CODE (arg1) == COMPLEX_CST
11063               && real_zerop (TREE_REALPART (arg1)))
11064             {
11065               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11066               if (real_onep (TREE_IMAGPART (arg1)))
11067                 return
11068                   fold_build2_loc (loc, COMPLEX_EXPR, type,
11069                                negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
11070                                                              rtype, arg0)),
11071                                fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
11072               else if (real_minus_onep (TREE_IMAGPART (arg1)))
11073                 return
11074                   fold_build2_loc (loc, COMPLEX_EXPR, type,
11075                                fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
11076                                negate_expr (fold_build1_loc (loc, REALPART_EXPR,
11077                                                              rtype, arg0)));
11078             }
11079
11080           /* Optimize z * conj(z) for floating point complex numbers.
11081              Guarded by flag_unsafe_math_optimizations as non-finite
11082              imaginary components don't produce scalar results.  */
11083           if (flag_unsafe_math_optimizations
11084               && TREE_CODE (arg0) == CONJ_EXPR
11085               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11086             return fold_mult_zconjz (loc, type, arg1);
11087           if (flag_unsafe_math_optimizations
11088               && TREE_CODE (arg1) == CONJ_EXPR
11089               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11090             return fold_mult_zconjz (loc, type, arg0);
11091
11092           if (flag_unsafe_math_optimizations)
11093             {
11094               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11095               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11096
11097               /* Optimizations of root(...)*root(...).  */
11098               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
11099                 {
11100                   tree rootfn, arg;
11101                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
11102                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
11103
11104                   /* Optimize sqrt(x)*sqrt(x) as x.  */
11105                   if (BUILTIN_SQRT_P (fcode0)
11106                       && operand_equal_p (arg00, arg10, 0)
11107                       && ! HONOR_SNANS (TYPE_MODE (type)))
11108                     return arg00;
11109
11110                   /* Optimize root(x)*root(y) as root(x*y).  */
11111                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11112                   arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
11113                   return build_call_expr_loc (loc, rootfn, 1, arg);
11114                 }
11115
11116               /* Optimize expN(x)*expN(y) as expN(x+y).  */
11117               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
11118                 {
11119                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11120                   tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
11121                                           CALL_EXPR_ARG (arg0, 0),
11122                                           CALL_EXPR_ARG (arg1, 0));
11123                   return build_call_expr_loc (loc, expfn, 1, arg);
11124                 }
11125
11126               /* Optimizations of pow(...)*pow(...).  */
11127               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
11128                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
11129                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
11130                 {
11131                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
11132                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
11133                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
11134                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
11135
11136                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
11137                   if (operand_equal_p (arg01, arg11, 0))
11138                     {
11139                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11140                       tree arg = fold_build2_loc (loc, MULT_EXPR, type,
11141                                               arg00, arg10);
11142                       return build_call_expr_loc (loc, powfn, 2, arg, arg01);
11143                     }
11144
11145                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
11146                   if (operand_equal_p (arg00, arg10, 0))
11147                     {
11148                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11149                       tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
11150                                               arg01, arg11);
11151                       return build_call_expr_loc (loc, powfn, 2, arg00, arg);
11152                     }
11153                 }
11154
11155               /* Optimize tan(x)*cos(x) as sin(x).  */
11156               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
11157                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
11158                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
11159                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
11160                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
11161                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
11162                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11163                                       CALL_EXPR_ARG (arg1, 0), 0))
11164                 {
11165                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
11166
11167                   if (sinfn != NULL_TREE)
11168                     return build_call_expr_loc (loc, sinfn, 1,
11169                                             CALL_EXPR_ARG (arg0, 0));
11170                 }
11171
11172               /* Optimize x*pow(x,c) as pow(x,c+1).  */
11173               if (fcode1 == BUILT_IN_POW
11174                   || fcode1 == BUILT_IN_POWF
11175                   || fcode1 == BUILT_IN_POWL)
11176                 {
11177                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
11178                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
11179                   if (TREE_CODE (arg11) == REAL_CST
11180                       && !TREE_OVERFLOW (arg11)
11181                       && operand_equal_p (arg0, arg10, 0))
11182                     {
11183                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11184                       REAL_VALUE_TYPE c;
11185                       tree arg;
11186
11187                       c = TREE_REAL_CST (arg11);
11188                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
11189                       arg = build_real (type, c);
11190                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
11191                     }
11192                 }
11193
11194               /* Optimize pow(x,c)*x as pow(x,c+1).  */
11195               if (fcode0 == BUILT_IN_POW
11196                   || fcode0 == BUILT_IN_POWF
11197                   || fcode0 == BUILT_IN_POWL)
11198                 {
11199                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
11200                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
11201                   if (TREE_CODE (arg01) == REAL_CST
11202                       && !TREE_OVERFLOW (arg01)
11203                       && operand_equal_p (arg1, arg00, 0))
11204                     {
11205                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11206                       REAL_VALUE_TYPE c;
11207                       tree arg;
11208
11209                       c = TREE_REAL_CST (arg01);
11210                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
11211                       arg = build_real (type, c);
11212                       return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11213                     }
11214                 }
11215
11216               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
11217               if (optimize_function_for_speed_p (cfun)
11218                   && operand_equal_p (arg0, arg1, 0))
11219                 {
11220                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
11221
11222                   if (powfn)
11223                     {
11224                       tree arg = build_real (type, dconst2);
11225                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
11226                     }
11227                 }
11228             }
11229         }
11230       goto associate;
11231
11232     case BIT_IOR_EXPR:
11233     bit_ior:
11234       if (integer_all_onesp (arg1))
11235         return omit_one_operand_loc (loc, type, arg1, arg0);
11236       if (integer_zerop (arg1))
11237         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11238       if (operand_equal_p (arg0, arg1, 0))
11239         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11240
11241       /* ~X | X is -1.  */
11242       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11243           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11244         {
11245           t1 = fold_convert_loc (loc, type, integer_zero_node);
11246           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11247           return omit_one_operand_loc (loc, type, t1, arg1);
11248         }
11249
11250       /* X | ~X is -1.  */
11251       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11252           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11253         {
11254           t1 = fold_convert_loc (loc, type, integer_zero_node);
11255           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11256           return omit_one_operand_loc (loc, type, t1, arg0);
11257         }
11258
11259       /* Canonicalize (X & C1) | C2.  */
11260       if (TREE_CODE (arg0) == BIT_AND_EXPR
11261           && TREE_CODE (arg1) == INTEGER_CST
11262           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11263         {
11264           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
11265           int width = TYPE_PRECISION (type), w;
11266           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
11267           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11268           hi2 = TREE_INT_CST_HIGH (arg1);
11269           lo2 = TREE_INT_CST_LOW (arg1);
11270
11271           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
11272           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
11273             return omit_one_operand_loc (loc, type, arg1,
11274                                      TREE_OPERAND (arg0, 0));
11275
11276           if (width > HOST_BITS_PER_WIDE_INT)
11277             {
11278               mhi = (unsigned HOST_WIDE_INT) -1 
11279                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
11280               mlo = -1;
11281             }
11282           else
11283             {
11284               mhi = 0;
11285               mlo = (unsigned HOST_WIDE_INT) -1
11286                     >> (HOST_BITS_PER_WIDE_INT - width);
11287             }
11288
11289           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
11290           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
11291             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11292                                 TREE_OPERAND (arg0, 0), arg1);
11293
11294           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11295              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11296              mode which allows further optimizations.  */
11297           hi1 &= mhi;
11298           lo1 &= mlo;
11299           hi2 &= mhi;
11300           lo2 &= mlo;
11301           hi3 = hi1 & ~hi2;
11302           lo3 = lo1 & ~lo2;
11303           for (w = BITS_PER_UNIT;
11304                w <= width && w <= HOST_BITS_PER_WIDE_INT;
11305                w <<= 1)
11306             {
11307               unsigned HOST_WIDE_INT mask
11308                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
11309               if (((lo1 | lo2) & mask) == mask
11310                   && (lo1 & ~mask) == 0 && hi1 == 0)
11311                 {
11312                   hi3 = 0;
11313                   lo3 = mask;
11314                   break;
11315                 }
11316             }
11317           if (hi3 != hi1 || lo3 != lo1)
11318             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11319                                 fold_build2_loc (loc, BIT_AND_EXPR, type,
11320                                              TREE_OPERAND (arg0, 0),
11321                                              build_int_cst_wide (type,
11322                                                                  lo3, hi3)),
11323                                 arg1);
11324         }
11325
11326       /* (X & Y) | Y is (X, Y).  */
11327       if (TREE_CODE (arg0) == BIT_AND_EXPR
11328           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11329         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11330       /* (X & Y) | X is (Y, X).  */
11331       if (TREE_CODE (arg0) == BIT_AND_EXPR
11332           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11333           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11334         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11335       /* X | (X & Y) is (Y, X).  */
11336       if (TREE_CODE (arg1) == BIT_AND_EXPR
11337           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11338           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11339         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11340       /* X | (Y & X) is (Y, X).  */
11341       if (TREE_CODE (arg1) == BIT_AND_EXPR
11342           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11343           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11344         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11345
11346       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11347       if (t1 != NULL_TREE)
11348         return t1;
11349
11350       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
11351
11352          This results in more efficient code for machines without a NAND
11353          instruction.  Combine will canonicalize to the first form
11354          which will allow use of NAND instructions provided by the
11355          backend if they exist.  */
11356       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11357           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11358         {
11359           return
11360             fold_build1_loc (loc, BIT_NOT_EXPR, type,
11361                          build2 (BIT_AND_EXPR, type,
11362                                  fold_convert_loc (loc, type,
11363                                                    TREE_OPERAND (arg0, 0)),
11364                                  fold_convert_loc (loc, type,
11365                                                    TREE_OPERAND (arg1, 0))));
11366         }
11367
11368       /* See if this can be simplified into a rotate first.  If that
11369          is unsuccessful continue in the association code.  */
11370       goto bit_rotate;
11371
11372     case BIT_XOR_EXPR:
11373       if (integer_zerop (arg1))
11374         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11375       if (integer_all_onesp (arg1))
11376         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
11377       if (operand_equal_p (arg0, arg1, 0))
11378         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11379
11380       /* ~X ^ X is -1.  */
11381       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11382           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11383         {
11384           t1 = fold_convert_loc (loc, type, integer_zero_node);
11385           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11386           return omit_one_operand_loc (loc, type, t1, arg1);
11387         }
11388
11389       /* X ^ ~X is -1.  */
11390       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11391           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11392         {
11393           t1 = fold_convert_loc (loc, type, integer_zero_node);
11394           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11395           return omit_one_operand_loc (loc, type, t1, arg0);
11396         }
11397
11398       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11399          with a constant, and the two constants have no bits in common,
11400          we should treat this as a BIT_IOR_EXPR since this may produce more
11401          simplifications.  */
11402       if (TREE_CODE (arg0) == BIT_AND_EXPR
11403           && TREE_CODE (arg1) == BIT_AND_EXPR
11404           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11405           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11406           && integer_zerop (const_binop (BIT_AND_EXPR,
11407                                          TREE_OPERAND (arg0, 1),
11408                                          TREE_OPERAND (arg1, 1), 0)))
11409         {
11410           code = BIT_IOR_EXPR;
11411           goto bit_ior;
11412         }
11413
11414       /* (X | Y) ^ X -> Y & ~ X*/
11415       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11416           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11417         {
11418           tree t2 = TREE_OPERAND (arg0, 1);
11419           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11420                             arg1);
11421           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11422                             fold_convert_loc (loc, type, t2),
11423                             fold_convert_loc (loc, type, t1));
11424           return t1;
11425         }
11426
11427       /* (Y | X) ^ X -> Y & ~ X*/
11428       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11429           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11430         {
11431           tree t2 = TREE_OPERAND (arg0, 0);
11432           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11433                             arg1);
11434           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11435                             fold_convert_loc (loc, type, t2),
11436                             fold_convert_loc (loc, type, t1));
11437           return t1;
11438         }
11439
11440       /* X ^ (X | Y) -> Y & ~ X*/
11441       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11442           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11443         {
11444           tree t2 = TREE_OPERAND (arg1, 1);
11445           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11446                             arg0);
11447           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11448                             fold_convert_loc (loc, type, t2),
11449                             fold_convert_loc (loc, type, t1));
11450           return t1;
11451         }
11452
11453       /* X ^ (Y | X) -> Y & ~ X*/
11454       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11455           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11456         {
11457           tree t2 = TREE_OPERAND (arg1, 0);
11458           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11459                             arg0);
11460           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11461                             fold_convert_loc (loc, type, t2),
11462                             fold_convert_loc (loc, type, t1));
11463           return t1;
11464         }
11465         
11466       /* Convert ~X ^ ~Y to X ^ Y.  */
11467       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11468           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11469         return fold_build2_loc (loc, code, type,
11470                             fold_convert_loc (loc, type,
11471                                               TREE_OPERAND (arg0, 0)),
11472                             fold_convert_loc (loc, type,
11473                                               TREE_OPERAND (arg1, 0)));
11474
11475       /* Convert ~X ^ C to X ^ ~C.  */
11476       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11477           && TREE_CODE (arg1) == INTEGER_CST)
11478         return fold_build2_loc (loc, code, type,
11479                             fold_convert_loc (loc, type,
11480                                               TREE_OPERAND (arg0, 0)),
11481                             fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
11482
11483       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
11484       if (TREE_CODE (arg0) == BIT_AND_EXPR
11485           && integer_onep (TREE_OPERAND (arg0, 1))
11486           && integer_onep (arg1))
11487         return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11488                             build_int_cst (TREE_TYPE (arg0), 0));
11489
11490       /* Fold (X & Y) ^ Y as ~X & Y.  */
11491       if (TREE_CODE (arg0) == BIT_AND_EXPR
11492           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11493         {
11494           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11495           return fold_build2_loc (loc, BIT_AND_EXPR, type, 
11496                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11497                               fold_convert_loc (loc, type, arg1));
11498         }
11499       /* Fold (X & Y) ^ X as ~Y & X.  */
11500       if (TREE_CODE (arg0) == BIT_AND_EXPR
11501           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11502           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11503         {
11504           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11505           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11506                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11507                               fold_convert_loc (loc, type, arg1));
11508         }
11509       /* Fold X ^ (X & Y) as X & ~Y.  */
11510       if (TREE_CODE (arg1) == BIT_AND_EXPR
11511           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11512         {
11513           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11514           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11515                               fold_convert_loc (loc, type, arg0),
11516                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11517         }
11518       /* Fold X ^ (Y & X) as ~Y & X.  */
11519       if (TREE_CODE (arg1) == BIT_AND_EXPR
11520           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11521           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11522         {
11523           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11524           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11525                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11526                               fold_convert_loc (loc, type, arg0));
11527         }
11528
11529       /* See if this can be simplified into a rotate first.  If that
11530          is unsuccessful continue in the association code.  */
11531       goto bit_rotate;
11532
11533     case BIT_AND_EXPR:
11534       if (integer_all_onesp (arg1))
11535         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11536       if (integer_zerop (arg1))
11537         return omit_one_operand_loc (loc, type, arg1, arg0);
11538       if (operand_equal_p (arg0, arg1, 0))
11539         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11540
11541       /* ~X & X is always zero.  */
11542       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11543           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11544         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11545
11546       /* X & ~X is always zero.  */
11547       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11548           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11549         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11550
11551       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
11552       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11553           && TREE_CODE (arg1) == INTEGER_CST
11554           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11555         {
11556           tree tmp1 = fold_convert_loc (loc, type, arg1);
11557           tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11558           tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11559           tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
11560           tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
11561           return
11562             fold_convert_loc (loc, type,
11563                               fold_build2_loc (loc, BIT_IOR_EXPR,
11564                                            type, tmp2, tmp3));
11565         }
11566
11567       /* (X | Y) & Y is (X, Y).  */
11568       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11569           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11570         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11571       /* (X | Y) & X is (Y, X).  */
11572       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11573           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11574           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11575         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11576       /* X & (X | Y) is (Y, X).  */
11577       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11578           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11579           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11580         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11581       /* X & (Y | X) is (Y, X).  */
11582       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11583           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11584           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11585         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11586
11587       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11588       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11589           && integer_onep (TREE_OPERAND (arg0, 1))
11590           && integer_onep (arg1))
11591         {
11592           tem = TREE_OPERAND (arg0, 0);
11593           return fold_build2_loc (loc, EQ_EXPR, type,
11594                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11595                                            build_int_cst (TREE_TYPE (tem), 1)),
11596                               build_int_cst (TREE_TYPE (tem), 0));
11597         }
11598       /* Fold ~X & 1 as (X & 1) == 0.  */
11599       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11600           && integer_onep (arg1))
11601         {
11602           tem = TREE_OPERAND (arg0, 0);
11603           return fold_build2_loc (loc, EQ_EXPR, type,
11604                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11605                                            build_int_cst (TREE_TYPE (tem), 1)),
11606                               build_int_cst (TREE_TYPE (tem), 0));
11607         }
11608
11609       /* Fold (X ^ Y) & Y as ~X & Y.  */
11610       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11611           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11612         {
11613           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11614           return fold_build2_loc (loc, BIT_AND_EXPR, type, 
11615                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11616                               fold_convert_loc (loc, type, arg1));
11617         }
11618       /* Fold (X ^ Y) & X as ~Y & X.  */
11619       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11620           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11621           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11622         {
11623           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11624           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11625                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11626                               fold_convert_loc (loc, type, arg1));
11627         }
11628       /* Fold X & (X ^ Y) as X & ~Y.  */
11629       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11630           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11631         {
11632           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11633           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11634                               fold_convert_loc (loc, type, arg0),
11635                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11636         }
11637       /* Fold X & (Y ^ X) as ~Y & X.  */
11638       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11639           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11640           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11641         {
11642           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11643           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11644                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11645                               fold_convert_loc (loc, type, arg0));
11646         }
11647
11648       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11649       if (t1 != NULL_TREE)
11650         return t1;
11651       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11652       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11653           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11654         {
11655           unsigned int prec
11656             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11657
11658           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11659               && (~TREE_INT_CST_LOW (arg1)
11660                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11661             return
11662               fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11663         }
11664
11665       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11666
11667          This results in more efficient code for machines without a NOR
11668          instruction.  Combine will canonicalize to the first form
11669          which will allow use of NOR instructions provided by the
11670          backend if they exist.  */
11671       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11672           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11673         {
11674           return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11675                               build2 (BIT_IOR_EXPR, type,
11676                                       fold_convert_loc (loc, type,
11677                                                         TREE_OPERAND (arg0, 0)),
11678                                       fold_convert_loc (loc, type,
11679                                                         TREE_OPERAND (arg1, 0))));
11680         }
11681
11682       /* If arg0 is derived from the address of an object or function, we may
11683          be able to fold this expression using the object or function's
11684          alignment.  */
11685       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11686         {
11687           unsigned HOST_WIDE_INT modulus, residue;
11688           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11689
11690           modulus = get_pointer_modulus_and_residue (arg0, &residue,
11691                                                      integer_onep (arg1));
11692
11693           /* This works because modulus is a power of 2.  If this weren't the
11694              case, we'd have to replace it by its greatest power-of-2
11695              divisor: modulus & -modulus.  */
11696           if (low < modulus)
11697             return build_int_cst (type, residue & low);
11698         }
11699
11700       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11701               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11702          if the new mask might be further optimized.  */
11703       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11704            || TREE_CODE (arg0) == RSHIFT_EXPR)
11705           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11706           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11707           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11708              < TYPE_PRECISION (TREE_TYPE (arg0))
11709           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11710           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11711         {
11712           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11713           unsigned HOST_WIDE_INT mask
11714             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11715           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11716           tree shift_type = TREE_TYPE (arg0);
11717
11718           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11719             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11720           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11721                    && TYPE_PRECISION (TREE_TYPE (arg0))
11722                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11723             {
11724               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11725               tree arg00 = TREE_OPERAND (arg0, 0);
11726               /* See if more bits can be proven as zero because of
11727                  zero extension.  */
11728               if (TREE_CODE (arg00) == NOP_EXPR
11729                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11730                 {
11731                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11732                   if (TYPE_PRECISION (inner_type)
11733                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11734                       && TYPE_PRECISION (inner_type) < prec)
11735                     {
11736                       prec = TYPE_PRECISION (inner_type);
11737                       /* See if we can shorten the right shift.  */
11738                       if (shiftc < prec)
11739                         shift_type = inner_type;
11740                     }
11741                 }
11742               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11743               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11744               zerobits <<= prec - shiftc;
11745               /* For arithmetic shift if sign bit could be set, zerobits
11746                  can contain actually sign bits, so no transformation is
11747                  possible, unless MASK masks them all away.  In that
11748                  case the shift needs to be converted into logical shift.  */
11749               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11750                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11751                 {
11752                   if ((mask & zerobits) == 0)
11753                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11754                   else
11755                     zerobits = 0;
11756                 }
11757             }
11758
11759           /* ((X << 16) & 0xff00) is (X, 0).  */
11760           if ((mask & zerobits) == mask)
11761             return omit_one_operand_loc (loc, type,
11762                                      build_int_cst (type, 0), arg0);
11763
11764           newmask = mask | zerobits;
11765           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11766             {
11767               unsigned int prec;
11768
11769               /* Only do the transformation if NEWMASK is some integer
11770                  mode's mask.  */
11771               for (prec = BITS_PER_UNIT;
11772                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11773                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11774                   break;
11775               if (prec < HOST_BITS_PER_WIDE_INT
11776                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11777                 {
11778                   tree newmaskt;
11779
11780                   if (shift_type != TREE_TYPE (arg0))
11781                     {
11782                       tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11783                                          fold_convert_loc (loc, shift_type,
11784                                                            TREE_OPERAND (arg0, 0)),
11785                                          TREE_OPERAND (arg0, 1));
11786                       tem = fold_convert_loc (loc, type, tem);
11787                     }
11788                   else
11789                     tem = op0;
11790                   newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11791                   if (!tree_int_cst_equal (newmaskt, arg1))
11792                     return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11793                 }
11794             }
11795         }
11796
11797       goto associate;
11798
11799     case RDIV_EXPR:
11800       /* Don't touch a floating-point divide by zero unless the mode
11801          of the constant can represent infinity.  */
11802       if (TREE_CODE (arg1) == REAL_CST
11803           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11804           && real_zerop (arg1))
11805         return NULL_TREE;
11806
11807       /* Optimize A / A to 1.0 if we don't care about
11808          NaNs or Infinities.  Skip the transformation
11809          for non-real operands.  */
11810       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11811           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11812           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11813           && operand_equal_p (arg0, arg1, 0))
11814         {
11815           tree r = build_real (TREE_TYPE (arg0), dconst1);
11816
11817           return omit_two_operands_loc (loc, type, r, arg0, arg1);
11818         }
11819
11820       /* The complex version of the above A / A optimization.  */
11821       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11822           && operand_equal_p (arg0, arg1, 0))
11823         {
11824           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11825           if (! HONOR_NANS (TYPE_MODE (elem_type))
11826               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11827             {
11828               tree r = build_real (elem_type, dconst1);
11829               /* omit_two_operands will call fold_convert for us.  */
11830               return omit_two_operands_loc (loc, type, r, arg0, arg1);
11831             }
11832         }
11833
11834       /* (-A) / (-B) -> A / B  */
11835       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11836         return fold_build2_loc (loc, RDIV_EXPR, type,
11837                             TREE_OPERAND (arg0, 0),
11838                             negate_expr (arg1));
11839       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11840         return fold_build2_loc (loc, RDIV_EXPR, type,
11841                             negate_expr (arg0),
11842                             TREE_OPERAND (arg1, 0));
11843
11844       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11845       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11846           && real_onep (arg1))
11847         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11848
11849       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11850       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11851           && real_minus_onep (arg1))
11852         return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11853                                                   negate_expr (arg0)));
11854
11855       /* If ARG1 is a constant, we can convert this to a multiply by the
11856          reciprocal.  This does not have the same rounding properties,
11857          so only do this if -freciprocal-math.  We can actually
11858          always safely do it if ARG1 is a power of two, but it's hard to
11859          tell if it is or not in a portable manner.  */
11860       if (TREE_CODE (arg1) == REAL_CST)
11861         {
11862           if (flag_reciprocal_math
11863               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11864                                           arg1, 0)))
11865             return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11866           /* Find the reciprocal if optimizing and the result is exact.  */
11867           if (optimize)
11868             {
11869               REAL_VALUE_TYPE r;
11870               r = TREE_REAL_CST (arg1);
11871               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11872                 {
11873                   tem = build_real (type, r);
11874                   return fold_build2_loc (loc, MULT_EXPR, type,
11875                                       fold_convert_loc (loc, type, arg0), tem);
11876                 }
11877             }
11878         }
11879       /* Convert A/B/C to A/(B*C).  */ 
11880       if (flag_reciprocal_math
11881           && TREE_CODE (arg0) == RDIV_EXPR)
11882         return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11883                             fold_build2_loc (loc, MULT_EXPR, type,
11884                                          TREE_OPERAND (arg0, 1), arg1));
11885
11886       /* Convert A/(B/C) to (A/B)*C.  */
11887       if (flag_reciprocal_math
11888           && TREE_CODE (arg1) == RDIV_EXPR)
11889         return fold_build2_loc (loc, MULT_EXPR, type,
11890                             fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11891                                          TREE_OPERAND (arg1, 0)),
11892                             TREE_OPERAND (arg1, 1));
11893
11894       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11895       if (flag_reciprocal_math
11896           && TREE_CODE (arg1) == MULT_EXPR
11897           && TREE_CODE (arg0) == REAL_CST
11898           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11899         {
11900           tree tem = const_binop (RDIV_EXPR, arg0,
11901                                   TREE_OPERAND (arg1, 1), 0);
11902           if (tem)
11903             return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11904                                 TREE_OPERAND (arg1, 0));
11905         }
11906
11907       if (flag_unsafe_math_optimizations)
11908         {
11909           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11910           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11911
11912           /* Optimize sin(x)/cos(x) as tan(x).  */
11913           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11914                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11915                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11916               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11917                                   CALL_EXPR_ARG (arg1, 0), 0))
11918             {
11919               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11920
11921               if (tanfn != NULL_TREE)
11922                 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11923             }
11924
11925           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11926           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11927                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11928                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11929               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11930                                   CALL_EXPR_ARG (arg1, 0), 0))
11931             {
11932               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11933
11934               if (tanfn != NULL_TREE)
11935                 {
11936                   tree tmp = build_call_expr_loc (loc, tanfn, 1,
11937                                               CALL_EXPR_ARG (arg0, 0));
11938                   return fold_build2_loc (loc, RDIV_EXPR, type,
11939                                       build_real (type, dconst1), tmp);
11940                 }
11941             }
11942
11943           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11944              NaNs or Infinities.  */
11945           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11946                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11947                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11948             {
11949               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11950               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11951
11952               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11953                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11954                   && operand_equal_p (arg00, arg01, 0))
11955                 {
11956                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11957
11958                   if (cosfn != NULL_TREE)
11959                     return build_call_expr_loc (loc, cosfn, 1, arg00);
11960                 }
11961             }
11962
11963           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11964              NaNs or Infinities.  */
11965           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11966                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11967                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11968             {
11969               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11970               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11971
11972               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11973                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11974                   && operand_equal_p (arg00, arg01, 0))
11975                 {
11976                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11977
11978                   if (cosfn != NULL_TREE)
11979                     {
11980                       tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11981                       return fold_build2_loc (loc, RDIV_EXPR, type,
11982                                           build_real (type, dconst1),
11983                                           tmp);
11984                     }
11985                 }
11986             }
11987
11988           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11989           if (fcode0 == BUILT_IN_POW
11990               || fcode0 == BUILT_IN_POWF
11991               || fcode0 == BUILT_IN_POWL)
11992             {
11993               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11994               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11995               if (TREE_CODE (arg01) == REAL_CST
11996                   && !TREE_OVERFLOW (arg01)
11997                   && operand_equal_p (arg1, arg00, 0))
11998                 {
11999                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
12000                   REAL_VALUE_TYPE c;
12001                   tree arg;
12002
12003                   c = TREE_REAL_CST (arg01);
12004                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
12005                   arg = build_real (type, c);
12006                   return build_call_expr_loc (loc, powfn, 2, arg1, arg);
12007                 }
12008             }
12009
12010           /* Optimize a/root(b/c) into a*root(c/b).  */
12011           if (BUILTIN_ROOT_P (fcode1))
12012             {
12013               tree rootarg = CALL_EXPR_ARG (arg1, 0);
12014
12015               if (TREE_CODE (rootarg) == RDIV_EXPR)
12016                 {
12017                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12018                   tree b = TREE_OPERAND (rootarg, 0);
12019                   tree c = TREE_OPERAND (rootarg, 1);
12020
12021                   tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
12022
12023                   tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
12024                   return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
12025                 }
12026             }
12027
12028           /* Optimize x/expN(y) into x*expN(-y).  */
12029           if (BUILTIN_EXPONENT_P (fcode1))
12030             {
12031               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12032               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
12033               arg1 = build_call_expr_loc (loc,
12034                                       expfn, 1,
12035                                       fold_convert_loc (loc, type, arg));
12036               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
12037             }
12038
12039           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
12040           if (fcode1 == BUILT_IN_POW
12041               || fcode1 == BUILT_IN_POWF
12042               || fcode1 == BUILT_IN_POWL)
12043             {
12044               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12045               tree arg10 = CALL_EXPR_ARG (arg1, 0);
12046               tree arg11 = CALL_EXPR_ARG (arg1, 1);
12047               tree neg11 = fold_convert_loc (loc, type,
12048                                              negate_expr (arg11));
12049               arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
12050               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
12051             }
12052         }
12053       return NULL_TREE;
12054
12055     case TRUNC_DIV_EXPR:
12056     case FLOOR_DIV_EXPR:
12057       /* Simplify A / (B << N) where A and B are positive and B is
12058          a power of 2, to A >> (N + log2(B)).  */
12059       strict_overflow_p = false;
12060       if (TREE_CODE (arg1) == LSHIFT_EXPR
12061           && (TYPE_UNSIGNED (type)
12062               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12063         {
12064           tree sval = TREE_OPERAND (arg1, 0);
12065           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
12066             {
12067               tree sh_cnt = TREE_OPERAND (arg1, 1);
12068               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
12069
12070               if (strict_overflow_p)
12071                 fold_overflow_warning (("assuming signed overflow does not "
12072                                         "occur when simplifying A / (B << N)"),
12073                                        WARN_STRICT_OVERFLOW_MISC);
12074
12075               sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
12076                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
12077               return fold_build2_loc (loc, RSHIFT_EXPR, type,
12078                                   fold_convert_loc (loc, type, arg0), sh_cnt);
12079             }
12080         }
12081
12082       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
12083          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
12084       if (INTEGRAL_TYPE_P (type)
12085           && TYPE_UNSIGNED (type)
12086           && code == FLOOR_DIV_EXPR)
12087         return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
12088
12089       /* Fall thru */
12090
12091     case ROUND_DIV_EXPR:
12092     case CEIL_DIV_EXPR:
12093     case EXACT_DIV_EXPR:
12094       if (integer_onep (arg1))
12095         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12096       if (integer_zerop (arg1))
12097         return NULL_TREE;
12098       /* X / -1 is -X.  */
12099       if (!TYPE_UNSIGNED (type)
12100           && TREE_CODE (arg1) == INTEGER_CST
12101           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
12102           && TREE_INT_CST_HIGH (arg1) == -1)
12103         return fold_convert_loc (loc, type, negate_expr (arg0));
12104
12105       /* Convert -A / -B to A / B when the type is signed and overflow is
12106          undefined.  */
12107       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12108           && TREE_CODE (arg0) == NEGATE_EXPR
12109           && negate_expr_p (arg1))
12110         {
12111           if (INTEGRAL_TYPE_P (type))
12112             fold_overflow_warning (("assuming signed overflow does not occur "
12113                                     "when distributing negation across "
12114                                     "division"),
12115                                    WARN_STRICT_OVERFLOW_MISC);
12116           return fold_build2_loc (loc, code, type,
12117                               fold_convert_loc (loc, type,
12118                                                 TREE_OPERAND (arg0, 0)),
12119                               fold_convert_loc (loc, type,
12120                                                 negate_expr (arg1)));
12121         }
12122       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12123           && TREE_CODE (arg1) == NEGATE_EXPR
12124           && negate_expr_p (arg0))
12125         {
12126           if (INTEGRAL_TYPE_P (type))
12127             fold_overflow_warning (("assuming signed overflow does not occur "
12128                                     "when distributing negation across "
12129                                     "division"),
12130                                    WARN_STRICT_OVERFLOW_MISC);
12131           return fold_build2_loc (loc, code, type,
12132                               fold_convert_loc (loc, type,
12133                                                 negate_expr (arg0)),
12134                               fold_convert_loc (loc, type,
12135                                                 TREE_OPERAND (arg1, 0)));
12136         }
12137
12138       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12139          operation, EXACT_DIV_EXPR.
12140
12141          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12142          At one time others generated faster code, it's not clear if they do
12143          after the last round to changes to the DIV code in expmed.c.  */
12144       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12145           && multiple_of_p (type, arg0, arg1))
12146         return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
12147
12148       strict_overflow_p = false;
12149       if (TREE_CODE (arg1) == INTEGER_CST
12150           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12151                                          &strict_overflow_p)))
12152         {
12153           if (strict_overflow_p)
12154             fold_overflow_warning (("assuming signed overflow does not occur "
12155                                     "when simplifying division"),
12156                                    WARN_STRICT_OVERFLOW_MISC);
12157           return fold_convert_loc (loc, type, tem);
12158         }
12159
12160       return NULL_TREE;
12161
12162     case CEIL_MOD_EXPR:
12163     case FLOOR_MOD_EXPR:
12164     case ROUND_MOD_EXPR:
12165     case TRUNC_MOD_EXPR:
12166       /* X % 1 is always zero, but be sure to preserve any side
12167          effects in X.  */
12168       if (integer_onep (arg1))
12169         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12170
12171       /* X % 0, return X % 0 unchanged so that we can get the
12172          proper warnings and errors.  */
12173       if (integer_zerop (arg1))
12174         return NULL_TREE;
12175
12176       /* 0 % X is always zero, but be sure to preserve any side
12177          effects in X.  Place this after checking for X == 0.  */
12178       if (integer_zerop (arg0))
12179         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12180
12181       /* X % -1 is zero.  */
12182       if (!TYPE_UNSIGNED (type)
12183           && TREE_CODE (arg1) == INTEGER_CST
12184           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
12185           && TREE_INT_CST_HIGH (arg1) == -1)
12186         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12187
12188       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
12189          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
12190       strict_overflow_p = false;
12191       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
12192           && (TYPE_UNSIGNED (type)
12193               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12194         {
12195           tree c = arg1;
12196           /* Also optimize A % (C << N)  where C is a power of 2,
12197              to A & ((C << N) - 1).  */
12198           if (TREE_CODE (arg1) == LSHIFT_EXPR)
12199             c = TREE_OPERAND (arg1, 0);
12200
12201           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
12202             {
12203               tree mask = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
12204                                        build_int_cst (TREE_TYPE (arg1), 1));
12205               if (strict_overflow_p)
12206                 fold_overflow_warning (("assuming signed overflow does not "
12207                                         "occur when simplifying "
12208                                         "X % (power of two)"),
12209                                        WARN_STRICT_OVERFLOW_MISC);
12210               return fold_build2_loc (loc, BIT_AND_EXPR, type,
12211                                   fold_convert_loc (loc, type, arg0),
12212                                   fold_convert_loc (loc, type, mask));
12213             }
12214         }
12215
12216       /* X % -C is the same as X % C.  */
12217       if (code == TRUNC_MOD_EXPR
12218           && !TYPE_UNSIGNED (type)
12219           && TREE_CODE (arg1) == INTEGER_CST
12220           && !TREE_OVERFLOW (arg1)
12221           && TREE_INT_CST_HIGH (arg1) < 0
12222           && !TYPE_OVERFLOW_TRAPS (type)
12223           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
12224           && !sign_bit_p (arg1, arg1))
12225         return fold_build2_loc (loc, code, type,
12226                             fold_convert_loc (loc, type, arg0),
12227                             fold_convert_loc (loc, type,
12228                                               negate_expr (arg1)));
12229
12230       /* X % -Y is the same as X % Y.  */
12231       if (code == TRUNC_MOD_EXPR
12232           && !TYPE_UNSIGNED (type)
12233           && TREE_CODE (arg1) == NEGATE_EXPR
12234           && !TYPE_OVERFLOW_TRAPS (type))
12235         return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
12236                             fold_convert_loc (loc, type,
12237                                               TREE_OPERAND (arg1, 0)));
12238
12239       if (TREE_CODE (arg1) == INTEGER_CST
12240           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12241                                          &strict_overflow_p)))
12242         {
12243           if (strict_overflow_p)
12244             fold_overflow_warning (("assuming signed overflow does not occur "
12245                                     "when simplifying modulus"),
12246                                    WARN_STRICT_OVERFLOW_MISC);
12247           return fold_convert_loc (loc, type, tem);
12248         }
12249
12250       return NULL_TREE;
12251
12252     case LROTATE_EXPR:
12253     case RROTATE_EXPR:
12254       if (integer_all_onesp (arg0))
12255         return omit_one_operand_loc (loc, type, arg0, arg1);
12256       goto shift;
12257
12258     case RSHIFT_EXPR:
12259       /* Optimize -1 >> x for arithmetic right shifts.  */
12260       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
12261           && tree_expr_nonnegative_p (arg1))
12262         return omit_one_operand_loc (loc, type, arg0, arg1);
12263       /* ... fall through ...  */
12264
12265     case LSHIFT_EXPR:
12266     shift:
12267       if (integer_zerop (arg1))
12268         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12269       if (integer_zerop (arg0))
12270         return omit_one_operand_loc (loc, type, arg0, arg1);
12271
12272       /* Since negative shift count is not well-defined,
12273          don't try to compute it in the compiler.  */
12274       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12275         return NULL_TREE;
12276
12277       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
12278       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
12279           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
12280           && host_integerp (TREE_OPERAND (arg0, 1), false)
12281           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
12282         {
12283           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
12284                                + TREE_INT_CST_LOW (arg1));
12285
12286           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
12287              being well defined.  */
12288           if (low >= TYPE_PRECISION (type))
12289             {
12290               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
12291                 low = low % TYPE_PRECISION (type);
12292               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
12293                 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
12294                                          TREE_OPERAND (arg0, 0));
12295               else
12296                 low = TYPE_PRECISION (type) - 1;
12297             }
12298
12299           return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12300                               build_int_cst (type, low));
12301         }
12302
12303       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
12304          into x & ((unsigned)-1 >> c) for unsigned types.  */
12305       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
12306            || (TYPE_UNSIGNED (type)
12307                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
12308           && host_integerp (arg1, false)
12309           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
12310           && host_integerp (TREE_OPERAND (arg0, 1), false)
12311           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
12312         {
12313           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
12314           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
12315           tree lshift;
12316           tree arg00;
12317
12318           if (low0 == low1)
12319             {
12320               arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12321
12322               lshift = build_int_cst (type, -1);
12323               lshift = int_const_binop (code, lshift, arg1, 0);
12324
12325               return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
12326             }
12327         }
12328
12329       /* Rewrite an LROTATE_EXPR by a constant into an
12330          RROTATE_EXPR by a new constant.  */
12331       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
12332         {
12333           tree tem = build_int_cst (TREE_TYPE (arg1),
12334                                     TYPE_PRECISION (type));
12335           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
12336           return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
12337         }
12338
12339       /* If we have a rotate of a bit operation with the rotate count and
12340          the second operand of the bit operation both constant,
12341          permute the two operations.  */
12342       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12343           && (TREE_CODE (arg0) == BIT_AND_EXPR
12344               || TREE_CODE (arg0) == BIT_IOR_EXPR
12345               || TREE_CODE (arg0) == BIT_XOR_EXPR)
12346           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12347         return fold_build2_loc (loc, TREE_CODE (arg0), type,
12348                             fold_build2_loc (loc, code, type,
12349                                          TREE_OPERAND (arg0, 0), arg1),
12350                             fold_build2_loc (loc, code, type,
12351                                          TREE_OPERAND (arg0, 1), arg1));
12352
12353       /* Two consecutive rotates adding up to the precision of the
12354          type can be ignored.  */
12355       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12356           && TREE_CODE (arg0) == RROTATE_EXPR
12357           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12358           && TREE_INT_CST_HIGH (arg1) == 0
12359           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
12360           && ((TREE_INT_CST_LOW (arg1)
12361                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
12362               == (unsigned int) TYPE_PRECISION (type)))
12363         return TREE_OPERAND (arg0, 0);
12364
12365       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
12366               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
12367          if the latter can be further optimized.  */
12368       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
12369           && TREE_CODE (arg0) == BIT_AND_EXPR
12370           && TREE_CODE (arg1) == INTEGER_CST
12371           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12372         {
12373           tree mask = fold_build2_loc (loc, code, type,
12374                                    fold_convert_loc (loc, type,
12375                                                      TREE_OPERAND (arg0, 1)),
12376                                    arg1);
12377           tree shift = fold_build2_loc (loc, code, type,
12378                                     fold_convert_loc (loc, type,
12379                                                       TREE_OPERAND (arg0, 0)),
12380                                     arg1);
12381           tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
12382           if (tem)
12383             return tem;
12384         }
12385
12386       return NULL_TREE;
12387
12388     case MIN_EXPR:
12389       if (operand_equal_p (arg0, arg1, 0))
12390         return omit_one_operand_loc (loc, type, arg0, arg1);
12391       if (INTEGRAL_TYPE_P (type)
12392           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
12393         return omit_one_operand_loc (loc, type, arg1, arg0);
12394       tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
12395       if (tem)
12396         return tem;
12397       goto associate;
12398
12399     case MAX_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           && TYPE_MAX_VALUE (type)
12404           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
12405         return omit_one_operand_loc (loc, type, arg1, arg0);
12406       tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
12407       if (tem)
12408         return tem;
12409       goto associate;
12410
12411     case TRUTH_ANDIF_EXPR:
12412       /* Note that the operands of this must be ints
12413          and their values must be 0 or 1.
12414          ("true" is a fixed value perhaps depending on the language.)  */
12415       /* If first arg is constant zero, return it.  */
12416       if (integer_zerop (arg0))
12417         return fold_convert_loc (loc, type, arg0);
12418     case TRUTH_AND_EXPR:
12419       /* If either arg is constant true, drop it.  */
12420       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12421         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12422       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12423           /* Preserve sequence points.  */
12424           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12425         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12426       /* If second arg is constant zero, result is zero, but first arg
12427          must be evaluated.  */
12428       if (integer_zerop (arg1))
12429         return omit_one_operand_loc (loc, type, arg1, arg0);
12430       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12431          case will be handled here.  */
12432       if (integer_zerop (arg0))
12433         return omit_one_operand_loc (loc, type, arg0, arg1);
12434
12435       /* !X && X is always false.  */
12436       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12437           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12438         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12439       /* X && !X is always false.  */
12440       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12441           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12442         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12443
12444       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
12445          means A >= Y && A != MAX, but in this case we know that
12446          A < X <= MAX.  */
12447
12448       if (!TREE_SIDE_EFFECTS (arg0)
12449           && !TREE_SIDE_EFFECTS (arg1))
12450         {
12451           tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12452           if (tem && !operand_equal_p (tem, arg0, 0))
12453             return fold_build2_loc (loc, code, type, tem, arg1);
12454
12455           tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12456           if (tem && !operand_equal_p (tem, arg1, 0))
12457             return fold_build2_loc (loc, code, type, arg0, tem);
12458         }
12459
12460     truth_andor:
12461       /* We only do these simplifications if we are optimizing.  */
12462       if (!optimize)
12463         return NULL_TREE;
12464
12465       /* Check for things like (A || B) && (A || C).  We can convert this
12466          to A || (B && C).  Note that either operator can be any of the four
12467          truth and/or operations and the transformation will still be
12468          valid.   Also note that we only care about order for the
12469          ANDIF and ORIF operators.  If B contains side effects, this
12470          might change the truth-value of A.  */
12471       if (TREE_CODE (arg0) == TREE_CODE (arg1)
12472           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12473               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12474               || TREE_CODE (arg0) == TRUTH_AND_EXPR
12475               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12476           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12477         {
12478           tree a00 = TREE_OPERAND (arg0, 0);
12479           tree a01 = TREE_OPERAND (arg0, 1);
12480           tree a10 = TREE_OPERAND (arg1, 0);
12481           tree a11 = TREE_OPERAND (arg1, 1);
12482           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12483                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12484                              && (code == TRUTH_AND_EXPR
12485                                  || code == TRUTH_OR_EXPR));
12486
12487           if (operand_equal_p (a00, a10, 0))
12488             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12489                                 fold_build2_loc (loc, code, type, a01, a11));
12490           else if (commutative && operand_equal_p (a00, a11, 0))
12491             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12492                                 fold_build2_loc (loc, code, type, a01, a10));
12493           else if (commutative && operand_equal_p (a01, a10, 0))
12494             return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
12495                                 fold_build2_loc (loc, code, type, a00, a11));
12496
12497           /* This case if tricky because we must either have commutative
12498              operators or else A10 must not have side-effects.  */
12499
12500           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12501                    && operand_equal_p (a01, a11, 0))
12502             return fold_build2_loc (loc, TREE_CODE (arg0), type,
12503                                 fold_build2_loc (loc, code, type, a00, a10),
12504                                 a01);
12505         }
12506
12507       /* See if we can build a range comparison.  */
12508       if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
12509         return tem;
12510
12511       /* Check for the possibility of merging component references.  If our
12512          lhs is another similar operation, try to merge its rhs with our
12513          rhs.  Then try to merge our lhs and rhs.  */
12514       if (TREE_CODE (arg0) == code
12515           && 0 != (tem = fold_truthop (loc, code, type,
12516                                        TREE_OPERAND (arg0, 1), arg1)))
12517         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12518
12519       if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
12520         return tem;
12521
12522       return NULL_TREE;
12523
12524     case TRUTH_ORIF_EXPR:
12525       /* Note that the operands of this must be ints
12526          and their values must be 0 or true.
12527          ("true" is a fixed value perhaps depending on the language.)  */
12528       /* If first arg is constant true, return it.  */
12529       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12530         return fold_convert_loc (loc, type, arg0);
12531     case TRUTH_OR_EXPR:
12532       /* If either arg is constant zero, drop it.  */
12533       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12534         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12535       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12536           /* Preserve sequence points.  */
12537           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12538         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12539       /* If second arg is constant true, result is true, but we must
12540          evaluate first arg.  */
12541       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12542         return omit_one_operand_loc (loc, type, arg1, arg0);
12543       /* Likewise for first arg, but note this only occurs here for
12544          TRUTH_OR_EXPR.  */
12545       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12546         return omit_one_operand_loc (loc, type, arg0, arg1);
12547
12548       /* !X || X is always true.  */
12549       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12550           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12551         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12552       /* X || !X is always true.  */
12553       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12554           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12555         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12556
12557       goto truth_andor;
12558
12559     case TRUTH_XOR_EXPR:
12560       /* If the second arg is constant zero, drop it.  */
12561       if (integer_zerop (arg1))
12562         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12563       /* If the second arg is constant true, this is a logical inversion.  */
12564       if (integer_onep (arg1))
12565         {
12566           /* Only call invert_truthvalue if operand is a truth value.  */
12567           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12568             tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12569           else
12570             tem = invert_truthvalue_loc (loc, arg0);
12571           return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12572         }
12573       /* Identical arguments cancel to zero.  */
12574       if (operand_equal_p (arg0, arg1, 0))
12575         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12576
12577       /* !X ^ X is always true.  */
12578       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12579           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12580         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12581
12582       /* X ^ !X is always true.  */
12583       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12584           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12585         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12586
12587       return NULL_TREE;
12588
12589     case EQ_EXPR:
12590     case NE_EXPR:
12591       tem = fold_comparison (loc, code, type, op0, op1);
12592       if (tem != NULL_TREE)
12593         return tem;
12594
12595       /* bool_var != 0 becomes bool_var. */
12596       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12597           && code == NE_EXPR)
12598         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12599
12600       /* bool_var == 1 becomes bool_var. */
12601       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12602           && code == EQ_EXPR)
12603         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12604
12605       /* bool_var != 1 becomes !bool_var. */
12606       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12607           && code == NE_EXPR)
12608         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12609                             fold_convert_loc (loc, type, arg0));
12610
12611       /* bool_var == 0 becomes !bool_var. */
12612       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12613           && code == EQ_EXPR)
12614         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12615                             fold_convert_loc (loc, type, arg0));
12616
12617       /* !exp != 0 becomes !exp */
12618       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12619           && code == NE_EXPR)
12620         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12621
12622       /* If this is an equality comparison of the address of two non-weak,
12623          unaliased symbols neither of which are extern (since we do not
12624          have access to attributes for externs), then we know the result.  */
12625       if (TREE_CODE (arg0) == ADDR_EXPR
12626           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12627           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12628           && ! lookup_attribute ("alias",
12629                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12630           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12631           && TREE_CODE (arg1) == ADDR_EXPR
12632           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12633           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12634           && ! lookup_attribute ("alias",
12635                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12636           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12637         {
12638           /* We know that we're looking at the address of two
12639              non-weak, unaliased, static _DECL nodes.
12640
12641              It is both wasteful and incorrect to call operand_equal_p
12642              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12643              all we need to do is test pointer equality for the arguments
12644              to the two ADDR_EXPR nodes.  It is incorrect to use
12645              operand_equal_p as that function is NOT equivalent to a
12646              C equality test.  It can in fact return false for two
12647              objects which would test as equal using the C equality
12648              operator.  */
12649           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12650           return constant_boolean_node (equal
12651                                         ? code == EQ_EXPR : code != EQ_EXPR,
12652                                         type);
12653         }
12654
12655       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12656          a MINUS_EXPR of a constant, we can convert it into a comparison with
12657          a revised constant as long as no overflow occurs.  */
12658       if (TREE_CODE (arg1) == INTEGER_CST
12659           && (TREE_CODE (arg0) == PLUS_EXPR
12660               || TREE_CODE (arg0) == MINUS_EXPR)
12661           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12662           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12663                                       ? MINUS_EXPR : PLUS_EXPR,
12664                                       fold_convert_loc (loc, TREE_TYPE (arg0),
12665                                                         arg1),
12666                                       TREE_OPERAND (arg0, 1), 0))
12667           && !TREE_OVERFLOW (tem))
12668         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12669
12670       /* Similarly for a NEGATE_EXPR.  */
12671       if (TREE_CODE (arg0) == NEGATE_EXPR
12672           && TREE_CODE (arg1) == INTEGER_CST
12673           && 0 != (tem = negate_expr (arg1))
12674           && TREE_CODE (tem) == INTEGER_CST
12675           && !TREE_OVERFLOW (tem))
12676         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12677
12678       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12679       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12680           && TREE_CODE (arg1) == INTEGER_CST
12681           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12682         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12683                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12684                                          fold_convert_loc (loc,
12685                                                            TREE_TYPE (arg0),
12686                                                            arg1),
12687                                          TREE_OPERAND (arg0, 1)));
12688
12689       /* Transform comparisons of the form X +- Y CMP X to Y CMP 0.  */
12690       if ((TREE_CODE (arg0) == PLUS_EXPR
12691            || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12692            || TREE_CODE (arg0) == MINUS_EXPR)
12693           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12694           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12695               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12696         {
12697           tree val = TREE_OPERAND (arg0, 1);
12698           return omit_two_operands_loc (loc, type,
12699                                     fold_build2_loc (loc, code, type,
12700                                                  val,
12701                                                  build_int_cst (TREE_TYPE (val),
12702                                                                 0)),
12703                                     TREE_OPERAND (arg0, 0), arg1);
12704         }
12705
12706       /* Transform comparisons of the form C - X CMP X if C % 2 == 1.  */
12707       if (TREE_CODE (arg0) == MINUS_EXPR
12708           && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12709           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
12710           && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12711         {
12712           return omit_two_operands_loc (loc, type,
12713                                     code == NE_EXPR
12714                                     ? boolean_true_node : boolean_false_node,
12715                                     TREE_OPERAND (arg0, 1), arg1);
12716         }
12717
12718       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12719          for !=.  Don't do this for ordered comparisons due to overflow.  */
12720       if (TREE_CODE (arg0) == MINUS_EXPR
12721           && integer_zerop (arg1))
12722         return fold_build2_loc (loc, code, type,
12723                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12724
12725       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12726       if (TREE_CODE (arg0) == ABS_EXPR
12727           && (integer_zerop (arg1) || real_zerop (arg1)))
12728         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12729
12730       /* If this is an EQ or NE comparison with zero and ARG0 is
12731          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12732          two operations, but the latter can be done in one less insn
12733          on machines that have only two-operand insns or on which a
12734          constant cannot be the first operand.  */
12735       if (TREE_CODE (arg0) == BIT_AND_EXPR
12736           && integer_zerop (arg1))
12737         {
12738           tree arg00 = TREE_OPERAND (arg0, 0);
12739           tree arg01 = TREE_OPERAND (arg0, 1);
12740           if (TREE_CODE (arg00) == LSHIFT_EXPR
12741               && integer_onep (TREE_OPERAND (arg00, 0)))
12742             {
12743               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12744                                       arg01, TREE_OPERAND (arg00, 1));
12745               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12746                                  build_int_cst (TREE_TYPE (arg0), 1));
12747               return fold_build2_loc (loc, code, type,
12748                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12749                                   arg1);
12750             }
12751           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12752                    && integer_onep (TREE_OPERAND (arg01, 0)))
12753             {
12754               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12755                                       arg00, TREE_OPERAND (arg01, 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         }
12763
12764       /* If this is an NE or EQ comparison of zero against the result of a
12765          signed MOD operation whose second operand is a power of 2, make
12766          the MOD operation unsigned since it is simpler and equivalent.  */
12767       if (integer_zerop (arg1)
12768           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12769           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12770               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12771               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12772               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12773           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12774         {
12775           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12776           tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12777                                      fold_convert_loc (loc, newtype,
12778                                                        TREE_OPERAND (arg0, 0)),
12779                                      fold_convert_loc (loc, newtype,
12780                                                        TREE_OPERAND (arg0, 1)));
12781
12782           return fold_build2_loc (loc, code, type, newmod,
12783                               fold_convert_loc (loc, newtype, arg1));
12784         }
12785
12786       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12787          C1 is a valid shift constant, and C2 is a power of two, i.e.
12788          a single bit.  */
12789       if (TREE_CODE (arg0) == BIT_AND_EXPR
12790           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12791           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12792              == INTEGER_CST
12793           && integer_pow2p (TREE_OPERAND (arg0, 1))
12794           && integer_zerop (arg1))
12795         {
12796           tree itype = TREE_TYPE (arg0);
12797           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12798           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12799
12800           /* Check for a valid shift count.  */
12801           if (TREE_INT_CST_HIGH (arg001) == 0
12802               && TREE_INT_CST_LOW (arg001) < prec)
12803             {
12804               tree arg01 = TREE_OPERAND (arg0, 1);
12805               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12806               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12807               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12808                  can be rewritten as (X & (C2 << C1)) != 0.  */
12809               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12810                 {
12811                   tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12812                   tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12813                   return fold_build2_loc (loc, code, type, tem, arg1);
12814                 }
12815               /* Otherwise, for signed (arithmetic) shifts,
12816                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12817                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12818               else if (!TYPE_UNSIGNED (itype))
12819                 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12820                                     arg000, build_int_cst (itype, 0));
12821               /* Otherwise, of unsigned (logical) shifts,
12822                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12823                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12824               else
12825                 return omit_one_operand_loc (loc, type,
12826                                          code == EQ_EXPR ? integer_one_node
12827                                                          : integer_zero_node,
12828                                          arg000);
12829             }
12830         }
12831
12832       /* If this is an NE comparison of zero with an AND of one, remove the
12833          comparison since the AND will give the correct value.  */
12834       if (code == NE_EXPR
12835           && integer_zerop (arg1)
12836           && TREE_CODE (arg0) == BIT_AND_EXPR
12837           && integer_onep (TREE_OPERAND (arg0, 1)))
12838         return fold_convert_loc (loc, type, arg0);
12839
12840       /* If we have (A & C) == C where C is a power of 2, convert this into
12841          (A & C) != 0.  Similarly for NE_EXPR.  */
12842       if (TREE_CODE (arg0) == BIT_AND_EXPR
12843           && integer_pow2p (TREE_OPERAND (arg0, 1))
12844           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12845         return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12846                             arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12847                                                     integer_zero_node));
12848
12849       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12850          bit, then fold the expression into A < 0 or A >= 0.  */
12851       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12852       if (tem)
12853         return tem;
12854
12855       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12856          Similarly for NE_EXPR.  */
12857       if (TREE_CODE (arg0) == BIT_AND_EXPR
12858           && TREE_CODE (arg1) == INTEGER_CST
12859           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12860         {
12861           tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12862                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12863                                    TREE_OPERAND (arg0, 1));
12864           tree dandnotc = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12865                                        arg1, notc);
12866           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12867           if (integer_nonzerop (dandnotc))
12868             return omit_one_operand_loc (loc, type, rslt, arg0);
12869         }
12870
12871       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12872          Similarly for NE_EXPR.  */
12873       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12874           && TREE_CODE (arg1) == INTEGER_CST
12875           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12876         {
12877           tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12878           tree candnotd = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12879                                        TREE_OPERAND (arg0, 1), notd);
12880           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12881           if (integer_nonzerop (candnotd))
12882             return omit_one_operand_loc (loc, type, rslt, arg0);
12883         }
12884
12885       /* If this is a comparison of a field, we may be able to simplify it.  */
12886       if ((TREE_CODE (arg0) == COMPONENT_REF
12887            || TREE_CODE (arg0) == BIT_FIELD_REF)
12888           /* Handle the constant case even without -O
12889              to make sure the warnings are given.  */
12890           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12891         {
12892           t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12893           if (t1)
12894             return t1;
12895         }
12896
12897       /* Optimize comparisons of strlen vs zero to a compare of the
12898          first character of the string vs zero.  To wit,
12899                 strlen(ptr) == 0   =>  *ptr == 0
12900                 strlen(ptr) != 0   =>  *ptr != 0
12901          Other cases should reduce to one of these two (or a constant)
12902          due to the return value of strlen being unsigned.  */
12903       if (TREE_CODE (arg0) == CALL_EXPR
12904           && integer_zerop (arg1))
12905         {
12906           tree fndecl = get_callee_fndecl (arg0);
12907
12908           if (fndecl
12909               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12910               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12911               && call_expr_nargs (arg0) == 1
12912               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12913             {
12914               tree iref = build_fold_indirect_ref_loc (loc,
12915                                                    CALL_EXPR_ARG (arg0, 0));
12916               return fold_build2_loc (loc, code, type, iref,
12917                                   build_int_cst (TREE_TYPE (iref), 0));
12918             }
12919         }
12920
12921       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12922          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12923       if (TREE_CODE (arg0) == RSHIFT_EXPR
12924           && integer_zerop (arg1)
12925           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12926         {
12927           tree arg00 = TREE_OPERAND (arg0, 0);
12928           tree arg01 = TREE_OPERAND (arg0, 1);
12929           tree itype = TREE_TYPE (arg00);
12930           if (TREE_INT_CST_HIGH (arg01) == 0
12931               && TREE_INT_CST_LOW (arg01)
12932                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12933             {
12934               if (TYPE_UNSIGNED (itype))
12935                 {
12936                   itype = signed_type_for (itype);
12937                   arg00 = fold_convert_loc (loc, itype, arg00);
12938                 }
12939               return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12940                                   type, arg00, build_int_cst (itype, 0));
12941             }
12942         }
12943
12944       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12945       if (integer_zerop (arg1)
12946           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12947         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12948                             TREE_OPERAND (arg0, 1));
12949
12950       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12951       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12952           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12953         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12954                             build_int_cst (TREE_TYPE (arg1), 0));
12955       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12956       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12957           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12958           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12959         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12960                             build_int_cst (TREE_TYPE (arg1), 0));
12961
12962       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12963       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12964           && TREE_CODE (arg1) == INTEGER_CST
12965           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12966         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12967                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12968                                          TREE_OPERAND (arg0, 1), arg1));
12969
12970       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12971          (X & C) == 0 when C is a single bit.  */
12972       if (TREE_CODE (arg0) == BIT_AND_EXPR
12973           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12974           && integer_zerop (arg1)
12975           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12976         {
12977           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12978                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12979                              TREE_OPERAND (arg0, 1));
12980           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12981                               type, tem, arg1);
12982         }
12983
12984       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12985          constant C is a power of two, i.e. a single bit.  */
12986       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12987           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12988           && integer_zerop (arg1)
12989           && integer_pow2p (TREE_OPERAND (arg0, 1))
12990           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12991                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12992         {
12993           tree arg00 = TREE_OPERAND (arg0, 0);
12994           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12995                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12996         }
12997
12998       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12999          when is C is a power of two, i.e. a single bit.  */
13000       if (TREE_CODE (arg0) == BIT_AND_EXPR
13001           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
13002           && integer_zerop (arg1)
13003           && integer_pow2p (TREE_OPERAND (arg0, 1))
13004           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13005                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
13006         {
13007           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
13008           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
13009                              arg000, TREE_OPERAND (arg0, 1));
13010           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
13011                               tem, build_int_cst (TREE_TYPE (tem), 0));
13012         }
13013
13014       if (integer_zerop (arg1)
13015           && tree_expr_nonzero_p (arg0))
13016         {
13017           tree res = constant_boolean_node (code==NE_EXPR, type);
13018           return omit_one_operand_loc (loc, type, res, arg0);
13019         }
13020
13021       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
13022       if (TREE_CODE (arg0) == NEGATE_EXPR
13023           && TREE_CODE (arg1) == NEGATE_EXPR)
13024         return fold_build2_loc (loc, code, type,
13025                             TREE_OPERAND (arg0, 0),
13026                             TREE_OPERAND (arg1, 0));
13027
13028       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
13029       if (TREE_CODE (arg0) == BIT_AND_EXPR
13030           && TREE_CODE (arg1) == BIT_AND_EXPR)
13031         {
13032           tree arg00 = TREE_OPERAND (arg0, 0);
13033           tree arg01 = TREE_OPERAND (arg0, 1);
13034           tree arg10 = TREE_OPERAND (arg1, 0);
13035           tree arg11 = TREE_OPERAND (arg1, 1);
13036           tree itype = TREE_TYPE (arg0);
13037
13038           if (operand_equal_p (arg01, arg11, 0))
13039             return fold_build2_loc (loc, code, type,
13040                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13041                                              fold_build2_loc (loc,
13042                                                           BIT_XOR_EXPR, itype,
13043                                                           arg00, arg10),
13044                                              arg01),
13045                                 build_int_cst (itype, 0));
13046
13047           if (operand_equal_p (arg01, arg10, 0))
13048             return fold_build2_loc (loc, code, type,
13049                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13050                                              fold_build2_loc (loc,
13051                                                           BIT_XOR_EXPR, itype,
13052                                                           arg00, arg11),
13053                                              arg01),
13054                                 build_int_cst (itype, 0));
13055
13056           if (operand_equal_p (arg00, arg11, 0))
13057             return fold_build2_loc (loc, code, type,
13058                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13059                                              fold_build2_loc (loc,
13060                                                           BIT_XOR_EXPR, itype,
13061                                                           arg01, arg10),
13062                                              arg00),
13063                                 build_int_cst (itype, 0));
13064
13065           if (operand_equal_p (arg00, arg10, 0))
13066             return fold_build2_loc (loc, code, type,
13067                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13068                                              fold_build2_loc (loc,
13069                                                           BIT_XOR_EXPR, itype,
13070                                                           arg01, arg11),
13071                                              arg00),
13072                                 build_int_cst (itype, 0));
13073         }
13074
13075       if (TREE_CODE (arg0) == BIT_XOR_EXPR
13076           && TREE_CODE (arg1) == BIT_XOR_EXPR)
13077         {
13078           tree arg00 = TREE_OPERAND (arg0, 0);
13079           tree arg01 = TREE_OPERAND (arg0, 1);
13080           tree arg10 = TREE_OPERAND (arg1, 0);
13081           tree arg11 = TREE_OPERAND (arg1, 1);
13082           tree itype = TREE_TYPE (arg0);
13083
13084           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
13085              operand_equal_p guarantees no side-effects so we don't need
13086              to use omit_one_operand on Z.  */
13087           if (operand_equal_p (arg01, arg11, 0))
13088             return fold_build2_loc (loc, code, type, arg00, arg10);
13089           if (operand_equal_p (arg01, arg10, 0))
13090             return fold_build2_loc (loc, code, type, arg00, arg11);
13091           if (operand_equal_p (arg00, arg11, 0))
13092             return fold_build2_loc (loc, code, type, arg01, arg10);
13093           if (operand_equal_p (arg00, arg10, 0))
13094             return fold_build2_loc (loc, code, type, arg01, arg11);
13095
13096           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
13097           if (TREE_CODE (arg01) == INTEGER_CST
13098               && TREE_CODE (arg11) == INTEGER_CST)
13099             return fold_build2_loc (loc, code, type,
13100                                 fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00,
13101                                              fold_build2_loc (loc,
13102                                                           BIT_XOR_EXPR, itype,
13103                                                           arg01, arg11)),
13104                                 arg10);
13105         }
13106
13107       /* Attempt to simplify equality/inequality comparisons of complex
13108          values.  Only lower the comparison if the result is known or
13109          can be simplified to a single scalar comparison.  */
13110       if ((TREE_CODE (arg0) == COMPLEX_EXPR
13111            || TREE_CODE (arg0) == COMPLEX_CST)
13112           && (TREE_CODE (arg1) == COMPLEX_EXPR
13113               || TREE_CODE (arg1) == COMPLEX_CST))
13114         {
13115           tree real0, imag0, real1, imag1;
13116           tree rcond, icond;
13117
13118           if (TREE_CODE (arg0) == COMPLEX_EXPR)
13119             {
13120               real0 = TREE_OPERAND (arg0, 0);
13121               imag0 = TREE_OPERAND (arg0, 1);
13122             }
13123           else
13124             {
13125               real0 = TREE_REALPART (arg0);
13126               imag0 = TREE_IMAGPART (arg0);
13127             }
13128
13129           if (TREE_CODE (arg1) == COMPLEX_EXPR)
13130             {
13131               real1 = TREE_OPERAND (arg1, 0);
13132               imag1 = TREE_OPERAND (arg1, 1);
13133             }
13134           else
13135             {
13136               real1 = TREE_REALPART (arg1);
13137               imag1 = TREE_IMAGPART (arg1);
13138             }
13139
13140           rcond = fold_binary_loc (loc, code, type, real0, real1);
13141           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
13142             {
13143               if (integer_zerop (rcond))
13144                 {
13145                   if (code == EQ_EXPR)
13146                     return omit_two_operands_loc (loc, type, boolean_false_node,
13147                                               imag0, imag1);
13148                   return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
13149                 }
13150               else
13151                 {
13152                   if (code == NE_EXPR)
13153                     return omit_two_operands_loc (loc, type, boolean_true_node,
13154                                               imag0, imag1);
13155                   return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
13156                 }
13157             }
13158
13159           icond = fold_binary_loc (loc, code, type, imag0, imag1);
13160           if (icond && TREE_CODE (icond) == INTEGER_CST)
13161             {
13162               if (integer_zerop (icond))
13163                 {
13164                   if (code == EQ_EXPR)
13165                     return omit_two_operands_loc (loc, type, boolean_false_node,
13166                                               real0, real1);
13167                   return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
13168                 }
13169               else
13170                 {
13171                   if (code == NE_EXPR)
13172                     return omit_two_operands_loc (loc, type, boolean_true_node,
13173                                               real0, real1);
13174                   return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
13175                 }
13176             }
13177         }
13178
13179       return NULL_TREE;
13180
13181     case LT_EXPR:
13182     case GT_EXPR:
13183     case LE_EXPR:
13184     case GE_EXPR:
13185       tem = fold_comparison (loc, code, type, op0, op1);
13186       if (tem != NULL_TREE)
13187         return tem;
13188
13189       /* Transform comparisons of the form X +- C CMP X.  */
13190       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
13191           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
13192           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
13193                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
13194               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
13195                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
13196         {
13197           tree arg01 = TREE_OPERAND (arg0, 1);
13198           enum tree_code code0 = TREE_CODE (arg0);
13199           int is_positive;
13200
13201           if (TREE_CODE (arg01) == REAL_CST)
13202             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
13203           else
13204             is_positive = tree_int_cst_sgn (arg01);
13205
13206           /* (X - c) > X becomes false.  */
13207           if (code == GT_EXPR
13208               && ((code0 == MINUS_EXPR && is_positive >= 0)
13209                   || (code0 == PLUS_EXPR && is_positive <= 0)))
13210             {
13211               if (TREE_CODE (arg01) == INTEGER_CST
13212                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13213                 fold_overflow_warning (("assuming signed overflow does not "
13214                                         "occur when assuming that (X - c) > X "
13215                                         "is always false"),
13216                                        WARN_STRICT_OVERFLOW_ALL);
13217               return constant_boolean_node (0, type);
13218             }
13219
13220           /* Likewise (X + c) < X becomes false.  */
13221           if (code == LT_EXPR
13222               && ((code0 == PLUS_EXPR && is_positive >= 0)
13223                   || (code0 == MINUS_EXPR && is_positive <= 0)))
13224             {
13225               if (TREE_CODE (arg01) == INTEGER_CST
13226                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13227                 fold_overflow_warning (("assuming signed overflow does not "
13228                                         "occur when assuming that "
13229                                         "(X + c) < X is always false"),
13230                                        WARN_STRICT_OVERFLOW_ALL);
13231               return constant_boolean_node (0, type);
13232             }
13233
13234           /* Convert (X - c) <= X to true.  */
13235           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13236               && code == LE_EXPR
13237               && ((code0 == MINUS_EXPR && is_positive >= 0)
13238                   || (code0 == PLUS_EXPR && is_positive <= 0)))
13239             {
13240               if (TREE_CODE (arg01) == INTEGER_CST
13241                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13242                 fold_overflow_warning (("assuming signed overflow does not "
13243                                         "occur when assuming that "
13244                                         "(X - c) <= X is always true"),
13245                                        WARN_STRICT_OVERFLOW_ALL);
13246               return constant_boolean_node (1, type);
13247             }
13248
13249           /* Convert (X + c) >= X to true.  */
13250           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13251               && code == GE_EXPR
13252               && ((code0 == PLUS_EXPR && is_positive >= 0)
13253                   || (code0 == MINUS_EXPR && is_positive <= 0)))
13254             {
13255               if (TREE_CODE (arg01) == INTEGER_CST
13256                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13257                 fold_overflow_warning (("assuming signed overflow does not "
13258                                         "occur when assuming that "
13259                                         "(X + c) >= X is always true"),
13260                                        WARN_STRICT_OVERFLOW_ALL);
13261               return constant_boolean_node (1, type);
13262             }
13263
13264           if (TREE_CODE (arg01) == INTEGER_CST)
13265             {
13266               /* Convert X + c > X and X - c < X to true for integers.  */
13267               if (code == GT_EXPR
13268                   && ((code0 == PLUS_EXPR && is_positive > 0)
13269                       || (code0 == MINUS_EXPR && is_positive < 0)))
13270                 {
13271                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13272                     fold_overflow_warning (("assuming signed overflow does "
13273                                             "not occur when assuming that "
13274                                             "(X + c) > X is always true"),
13275                                            WARN_STRICT_OVERFLOW_ALL);
13276                   return constant_boolean_node (1, type);
13277                 }
13278
13279               if (code == LT_EXPR
13280                   && ((code0 == MINUS_EXPR && is_positive > 0)
13281                       || (code0 == PLUS_EXPR && is_positive < 0)))
13282                 {
13283                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13284                     fold_overflow_warning (("assuming signed overflow does "
13285                                             "not occur when assuming that "
13286                                             "(X - c) < X is always true"),
13287                                            WARN_STRICT_OVERFLOW_ALL);
13288                   return constant_boolean_node (1, type);
13289                 }
13290
13291               /* Convert X + c <= X and X - c >= X to false for integers.  */
13292               if (code == LE_EXPR
13293                   && ((code0 == PLUS_EXPR && is_positive > 0)
13294                       || (code0 == MINUS_EXPR && is_positive < 0)))
13295                 {
13296                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13297                     fold_overflow_warning (("assuming signed overflow does "
13298                                             "not occur when assuming that "
13299                                             "(X + c) <= X is always false"),
13300                                            WARN_STRICT_OVERFLOW_ALL);
13301                   return constant_boolean_node (0, type);
13302                 }
13303
13304               if (code == GE_EXPR
13305                   && ((code0 == MINUS_EXPR && is_positive > 0)
13306                       || (code0 == PLUS_EXPR && is_positive < 0)))
13307                 {
13308                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13309                     fold_overflow_warning (("assuming signed overflow does "
13310                                             "not occur when assuming that "
13311                                             "(X - c) >= X is always false"),
13312                                            WARN_STRICT_OVERFLOW_ALL);
13313                   return constant_boolean_node (0, type);
13314                 }
13315             }
13316         }
13317
13318       /* Comparisons with the highest or lowest possible integer of
13319          the specified precision will have known values.  */
13320       {
13321         tree arg1_type = TREE_TYPE (arg1);
13322         unsigned int width = TYPE_PRECISION (arg1_type);
13323
13324         if (TREE_CODE (arg1) == INTEGER_CST
13325             && width <= 2 * HOST_BITS_PER_WIDE_INT
13326             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
13327           {
13328             HOST_WIDE_INT signed_max_hi;
13329             unsigned HOST_WIDE_INT signed_max_lo;
13330             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
13331
13332             if (width <= HOST_BITS_PER_WIDE_INT)
13333               {
13334                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13335                                 - 1;
13336                 signed_max_hi = 0;
13337                 max_hi = 0;
13338
13339                 if (TYPE_UNSIGNED (arg1_type))
13340                   {
13341                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13342                     min_lo = 0;
13343                     min_hi = 0;
13344                   }
13345                 else
13346                   {
13347                     max_lo = signed_max_lo;
13348                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
13349                     min_hi = -1;
13350                   }
13351               }
13352             else
13353               {
13354                 width -= HOST_BITS_PER_WIDE_INT;
13355                 signed_max_lo = -1;
13356                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13357                                 - 1;
13358                 max_lo = -1;
13359                 min_lo = 0;
13360
13361                 if (TYPE_UNSIGNED (arg1_type))
13362                   {
13363                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13364                     min_hi = 0;
13365                   }
13366                 else
13367                   {
13368                     max_hi = signed_max_hi;
13369                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
13370                   }
13371               }
13372
13373             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
13374                 && TREE_INT_CST_LOW (arg1) == max_lo)
13375               switch (code)
13376                 {
13377                 case GT_EXPR:
13378                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13379
13380                 case GE_EXPR:
13381                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13382
13383                 case LE_EXPR:
13384                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13385
13386                 case LT_EXPR:
13387                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13388
13389                 /* The GE_EXPR and LT_EXPR cases above are not normally
13390                    reached because of previous transformations.  */
13391
13392                 default:
13393                   break;
13394                 }
13395             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13396                      == max_hi
13397                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
13398               switch (code)
13399                 {
13400                 case GT_EXPR:
13401                   arg1 = const_binop (PLUS_EXPR, arg1,
13402                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
13403                   return fold_build2_loc (loc, EQ_EXPR, type,
13404                                       fold_convert_loc (loc,
13405                                                         TREE_TYPE (arg1), arg0),
13406                                       arg1);
13407                 case LE_EXPR:
13408                   arg1 = const_binop (PLUS_EXPR, arg1,
13409                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
13410                   return fold_build2_loc (loc, NE_EXPR, type,
13411                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13412                                                         arg0),
13413                                       arg1);
13414                 default:
13415                   break;
13416                 }
13417             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13418                      == min_hi
13419                      && TREE_INT_CST_LOW (arg1) == min_lo)
13420               switch (code)
13421                 {
13422                 case LT_EXPR:
13423                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13424
13425                 case LE_EXPR:
13426                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13427
13428                 case GE_EXPR:
13429                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13430
13431                 case GT_EXPR:
13432                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13433
13434                 default:
13435                   break;
13436                 }
13437             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13438                      == min_hi
13439                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
13440               switch (code)
13441                 {
13442                 case GE_EXPR:
13443                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13444                   return fold_build2_loc (loc, NE_EXPR, type,
13445                                       fold_convert_loc (loc,
13446                                                         TREE_TYPE (arg1), arg0),
13447                                       arg1);
13448                 case LT_EXPR:
13449                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13450                   return fold_build2_loc (loc, EQ_EXPR, type,
13451                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13452                                                         arg0),
13453                                       arg1);
13454                 default:
13455                   break;
13456                 }
13457
13458             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13459                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
13460                      && TYPE_UNSIGNED (arg1_type)
13461                      /* We will flip the signedness of the comparison operator
13462                         associated with the mode of arg1, so the sign bit is
13463                         specified by this mode.  Check that arg1 is the signed
13464                         max associated with this sign bit.  */
13465                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13466                      /* signed_type does not work on pointer types.  */
13467                      && INTEGRAL_TYPE_P (arg1_type))
13468               {
13469                 /* The following case also applies to X < signed_max+1
13470                    and X >= signed_max+1 because previous transformations.  */
13471                 if (code == LE_EXPR || code == GT_EXPR)
13472                   {
13473                     tree st;
13474                     st = signed_type_for (TREE_TYPE (arg1));
13475                     return fold_build2_loc (loc,
13476                                         code == LE_EXPR ? GE_EXPR : LT_EXPR,
13477                                         type, fold_convert_loc (loc, st, arg0),
13478                                         build_int_cst (st, 0));
13479                   }
13480               }
13481           }
13482       }
13483
13484       /* If we are comparing an ABS_EXPR with a constant, we can
13485          convert all the cases into explicit comparisons, but they may
13486          well not be faster than doing the ABS and one comparison.
13487          But ABS (X) <= C is a range comparison, which becomes a subtraction
13488          and a comparison, and is probably faster.  */
13489       if (code == LE_EXPR
13490           && TREE_CODE (arg1) == INTEGER_CST
13491           && TREE_CODE (arg0) == ABS_EXPR
13492           && ! TREE_SIDE_EFFECTS (arg0)
13493           && (0 != (tem = negate_expr (arg1)))
13494           && TREE_CODE (tem) == INTEGER_CST
13495           && !TREE_OVERFLOW (tem))
13496         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13497                             build2 (GE_EXPR, type,
13498                                     TREE_OPERAND (arg0, 0), tem),
13499                             build2 (LE_EXPR, type,
13500                                     TREE_OPERAND (arg0, 0), arg1));
13501
13502       /* Convert ABS_EXPR<x> >= 0 to true.  */
13503       strict_overflow_p = false;
13504       if (code == GE_EXPR
13505           && (integer_zerop (arg1)
13506               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13507                   && real_zerop (arg1)))
13508           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13509         {
13510           if (strict_overflow_p)
13511             fold_overflow_warning (("assuming signed overflow does not occur "
13512                                     "when simplifying comparison of "
13513                                     "absolute value and zero"),
13514                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13515           return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13516         }
13517
13518       /* Convert ABS_EXPR<x> < 0 to false.  */
13519       strict_overflow_p = false;
13520       if (code == LT_EXPR
13521           && (integer_zerop (arg1) || real_zerop (arg1))
13522           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13523         {
13524           if (strict_overflow_p)
13525             fold_overflow_warning (("assuming signed overflow does not occur "
13526                                     "when simplifying comparison of "
13527                                     "absolute value and zero"),
13528                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13529           return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13530         }
13531
13532       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13533          and similarly for >= into !=.  */
13534       if ((code == LT_EXPR || code == GE_EXPR)
13535           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13536           && TREE_CODE (arg1) == LSHIFT_EXPR
13537           && integer_onep (TREE_OPERAND (arg1, 0)))
13538         {
13539           tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13540                         build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13541                                 TREE_OPERAND (arg1, 1)),
13542                         build_int_cst (TREE_TYPE (arg0), 0));
13543           goto fold_binary_exit;
13544         }
13545
13546       if ((code == LT_EXPR || code == GE_EXPR)
13547           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13548           && CONVERT_EXPR_P (arg1)
13549           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13550           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13551         {
13552           tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13553                         fold_convert_loc (loc, TREE_TYPE (arg0),
13554                                           build2 (RSHIFT_EXPR,
13555                                                   TREE_TYPE (arg0), arg0,
13556                                                   TREE_OPERAND (TREE_OPERAND (arg1, 0),
13557                                                                 1))),
13558                         build_int_cst (TREE_TYPE (arg0), 0));
13559           goto fold_binary_exit;
13560         }
13561
13562       return NULL_TREE;
13563
13564     case UNORDERED_EXPR:
13565     case ORDERED_EXPR:
13566     case UNLT_EXPR:
13567     case UNLE_EXPR:
13568     case UNGT_EXPR:
13569     case UNGE_EXPR:
13570     case UNEQ_EXPR:
13571     case LTGT_EXPR:
13572       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13573         {
13574           t1 = fold_relational_const (code, type, arg0, arg1);
13575           if (t1 != NULL_TREE)
13576             return t1;
13577         }
13578
13579       /* If the first operand is NaN, the result is constant.  */
13580       if (TREE_CODE (arg0) == REAL_CST
13581           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13582           && (code != LTGT_EXPR || ! flag_trapping_math))
13583         {
13584           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13585                ? integer_zero_node
13586                : integer_one_node;
13587           return omit_one_operand_loc (loc, type, t1, arg1);
13588         }
13589
13590       /* If the second operand is NaN, the result is constant.  */
13591       if (TREE_CODE (arg1) == REAL_CST
13592           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
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, arg0);
13599         }
13600
13601       /* Simplify unordered comparison of something with itself.  */
13602       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13603           && operand_equal_p (arg0, arg1, 0))
13604         return constant_boolean_node (1, type);
13605
13606       if (code == LTGT_EXPR
13607           && !flag_trapping_math
13608           && operand_equal_p (arg0, arg1, 0))
13609         return constant_boolean_node (0, type);
13610
13611       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13612       {
13613         tree targ0 = strip_float_extensions (arg0);
13614         tree targ1 = strip_float_extensions (arg1);
13615         tree newtype = TREE_TYPE (targ0);
13616
13617         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13618           newtype = TREE_TYPE (targ1);
13619
13620         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13621           return fold_build2_loc (loc, code, type,
13622                               fold_convert_loc (loc, newtype, targ0),
13623                               fold_convert_loc (loc, newtype, targ1));
13624       }
13625
13626       return NULL_TREE;
13627
13628     case COMPOUND_EXPR:
13629       /* When pedantic, a compound expression can be neither an lvalue
13630          nor an integer constant expression.  */
13631       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13632         return NULL_TREE;
13633       /* Don't let (0, 0) be null pointer constant.  */
13634       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13635                                  : fold_convert_loc (loc, type, arg1);
13636       return pedantic_non_lvalue_loc (loc, tem);
13637
13638     case COMPLEX_EXPR:
13639       if ((TREE_CODE (arg0) == REAL_CST
13640            && TREE_CODE (arg1) == REAL_CST)
13641           || (TREE_CODE (arg0) == INTEGER_CST
13642               && TREE_CODE (arg1) == INTEGER_CST))
13643         return build_complex (type, arg0, arg1);
13644       return NULL_TREE;
13645
13646     case ASSERT_EXPR:
13647       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13648       gcc_unreachable ();
13649
13650     default:
13651       return NULL_TREE;
13652     } /* switch (code) */
13653  fold_binary_exit:
13654   protected_set_expr_location (tem, loc);
13655   return tem;
13656 }
13657
13658 /* Callback for walk_tree, looking for LABEL_EXPR.  Return *TP if it is
13659    a LABEL_EXPR; otherwise return NULL_TREE.  Do not check the subtrees
13660    of GOTO_EXPR.  */
13661
13662 static tree
13663 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13664 {
13665   switch (TREE_CODE (*tp))
13666     {
13667     case LABEL_EXPR:
13668       return *tp;
13669
13670     case GOTO_EXPR:
13671       *walk_subtrees = 0;
13672
13673       /* ... fall through ...  */
13674
13675     default:
13676       return NULL_TREE;
13677     }
13678 }
13679
13680 /* Return whether the sub-tree ST contains a label which is accessible from
13681    outside the sub-tree.  */
13682
13683 static bool
13684 contains_label_p (tree st)
13685 {
13686   return
13687    (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13688 }
13689
13690 /* Fold a ternary expression of code CODE and type TYPE with operands
13691    OP0, OP1, and OP2.  Return the folded expression if folding is
13692    successful.  Otherwise, return NULL_TREE.  */
13693
13694 tree
13695 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13696               tree op0, tree op1, tree op2)
13697 {
13698   tree tem;
13699   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13700   enum tree_code_class kind = TREE_CODE_CLASS (code);
13701
13702   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13703               && TREE_CODE_LENGTH (code) == 3);
13704
13705   /* Strip any conversions that don't change the mode.  This is safe
13706      for every expression, except for a comparison expression because
13707      its signedness is derived from its operands.  So, in the latter
13708      case, only strip conversions that don't change the signedness.
13709
13710      Note that this is done as an internal manipulation within the
13711      constant folder, in order to find the simplest representation of
13712      the arguments so that their form can be studied.  In any cases,
13713      the appropriate type conversions should be put back in the tree
13714      that will get out of the constant folder.  */
13715   if (op0)
13716     {
13717       arg0 = op0;
13718       STRIP_NOPS (arg0);
13719     }
13720
13721   if (op1)
13722     {
13723       arg1 = op1;
13724       STRIP_NOPS (arg1);
13725     }
13726
13727   switch (code)
13728     {
13729     case COMPONENT_REF:
13730       if (TREE_CODE (arg0) == CONSTRUCTOR
13731           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13732         {
13733           unsigned HOST_WIDE_INT idx;
13734           tree field, value;
13735           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13736             if (field == arg1)
13737               return value;
13738         }
13739       return NULL_TREE;
13740
13741     case COND_EXPR:
13742       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13743          so all simple results must be passed through pedantic_non_lvalue.  */
13744       if (TREE_CODE (arg0) == INTEGER_CST)
13745         {
13746           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13747           tem = integer_zerop (arg0) ? op2 : op1;
13748           /* Only optimize constant conditions when the selected branch
13749              has the same type as the COND_EXPR.  This avoids optimizing
13750              away "c ? x : throw", where the throw has a void type.
13751              Avoid throwing away that operand which contains label.  */
13752           if ((!TREE_SIDE_EFFECTS (unused_op)
13753                || !contains_label_p (unused_op))
13754               && (! VOID_TYPE_P (TREE_TYPE (tem))
13755                   || VOID_TYPE_P (type)))
13756             return pedantic_non_lvalue_loc (loc, tem);
13757           return NULL_TREE;
13758         }
13759       if (operand_equal_p (arg1, op2, 0))
13760         return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13761
13762       /* If we have A op B ? A : C, we may be able to convert this to a
13763          simpler expression, depending on the operation and the values
13764          of B and C.  Signed zeros prevent all of these transformations,
13765          for reasons given above each one.
13766
13767          Also try swapping the arguments and inverting the conditional.  */
13768       if (COMPARISON_CLASS_P (arg0)
13769           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13770                                              arg1, TREE_OPERAND (arg0, 1))
13771           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13772         {
13773           tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13774           if (tem)
13775             return tem;
13776         }
13777
13778       if (COMPARISON_CLASS_P (arg0)
13779           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13780                                              op2,
13781                                              TREE_OPERAND (arg0, 1))
13782           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13783         {
13784           tem = fold_truth_not_expr (loc, arg0);
13785           if (tem && COMPARISON_CLASS_P (tem))
13786             {
13787               tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13788               if (tem)
13789                 return tem;
13790             }
13791         }
13792
13793       /* If the second operand is simpler than the third, swap them
13794          since that produces better jump optimization results.  */
13795       if (truth_value_p (TREE_CODE (arg0))
13796           && tree_swap_operands_p (op1, op2, false))
13797         {
13798           /* See if this can be inverted.  If it can't, possibly because
13799              it was a floating-point inequality comparison, don't do
13800              anything.  */
13801           tem = fold_truth_not_expr (loc, arg0);
13802           if (tem)
13803             return fold_build3_loc (loc, code, type, tem, op2, op1);
13804         }
13805
13806       /* Convert A ? 1 : 0 to simply A.  */
13807       if (integer_onep (op1)
13808           && integer_zerop (op2)
13809           /* If we try to convert OP0 to our type, the
13810              call to fold will try to move the conversion inside
13811              a COND, which will recurse.  In that case, the COND_EXPR
13812              is probably the best choice, so leave it alone.  */
13813           && type == TREE_TYPE (arg0))
13814         return pedantic_non_lvalue_loc (loc, arg0);
13815
13816       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13817          over COND_EXPR in cases such as floating point comparisons.  */
13818       if (integer_zerop (op1)
13819           && integer_onep (op2)
13820           && truth_value_p (TREE_CODE (arg0)))
13821         return pedantic_non_lvalue_loc (loc,
13822                                     fold_convert_loc (loc, type,
13823                                               invert_truthvalue_loc (loc,
13824                                                                      arg0)));
13825
13826       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13827       if (TREE_CODE (arg0) == LT_EXPR
13828           && integer_zerop (TREE_OPERAND (arg0, 1))
13829           && integer_zerop (op2)
13830           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13831         {
13832           /* sign_bit_p only checks ARG1 bits within A's precision.
13833              If <sign bit of A> has wider type than A, bits outside
13834              of A's precision in <sign bit of A> need to be checked.
13835              If they are all 0, this optimization needs to be done
13836              in unsigned A's type, if they are all 1 in signed A's type,
13837              otherwise this can't be done.  */
13838           if (TYPE_PRECISION (TREE_TYPE (tem))
13839               < TYPE_PRECISION (TREE_TYPE (arg1))
13840               && TYPE_PRECISION (TREE_TYPE (tem))
13841                  < TYPE_PRECISION (type))
13842             {
13843               unsigned HOST_WIDE_INT mask_lo;
13844               HOST_WIDE_INT mask_hi;
13845               int inner_width, outer_width;
13846               tree tem_type;
13847
13848               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13849               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13850               if (outer_width > TYPE_PRECISION (type))
13851                 outer_width = TYPE_PRECISION (type);
13852
13853               if (outer_width > HOST_BITS_PER_WIDE_INT)
13854                 {
13855                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13856                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13857                   mask_lo = -1;
13858                 }
13859               else
13860                 {
13861                   mask_hi = 0;
13862                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13863                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13864                 }
13865               if (inner_width > HOST_BITS_PER_WIDE_INT)
13866                 {
13867                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13868                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13869                   mask_lo = 0;
13870                 }
13871               else
13872                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13873                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13874
13875               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13876                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13877                 {
13878                   tem_type = signed_type_for (TREE_TYPE (tem));
13879                   tem = fold_convert_loc (loc, tem_type, tem);
13880                 }
13881               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13882                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13883                 {
13884                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13885                   tem = fold_convert_loc (loc, tem_type, tem);
13886                 }
13887               else
13888                 tem = NULL;
13889             }
13890
13891           if (tem)
13892             return
13893               fold_convert_loc (loc, type,
13894                                 fold_build2_loc (loc, BIT_AND_EXPR,
13895                                              TREE_TYPE (tem), tem,
13896                                              fold_convert_loc (loc,
13897                                                                TREE_TYPE (tem),
13898                                                                arg1)));
13899         }
13900
13901       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13902          already handled above.  */
13903       if (TREE_CODE (arg0) == BIT_AND_EXPR
13904           && integer_onep (TREE_OPERAND (arg0, 1))
13905           && integer_zerop (op2)
13906           && integer_pow2p (arg1))
13907         {
13908           tree tem = TREE_OPERAND (arg0, 0);
13909           STRIP_NOPS (tem);
13910           if (TREE_CODE (tem) == RSHIFT_EXPR
13911               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13912               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13913                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13914             return fold_build2_loc (loc, BIT_AND_EXPR, type,
13915                                 TREE_OPERAND (tem, 0), arg1);
13916         }
13917
13918       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13919          is probably obsolete because the first operand should be a
13920          truth value (that's why we have the two cases above), but let's
13921          leave it in until we can confirm this for all front-ends.  */
13922       if (integer_zerop (op2)
13923           && TREE_CODE (arg0) == NE_EXPR
13924           && integer_zerop (TREE_OPERAND (arg0, 1))
13925           && integer_pow2p (arg1)
13926           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13927           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13928                               arg1, OEP_ONLY_CONST))
13929         return pedantic_non_lvalue_loc (loc,
13930                                     fold_convert_loc (loc, type,
13931                                                       TREE_OPERAND (arg0, 0)));
13932
13933       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13934       if (integer_zerop (op2)
13935           && truth_value_p (TREE_CODE (arg0))
13936           && truth_value_p (TREE_CODE (arg1)))
13937         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13938                             fold_convert_loc (loc, type, arg0),
13939                             arg1);
13940
13941       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13942       if (integer_onep (op2)
13943           && truth_value_p (TREE_CODE (arg0))
13944           && truth_value_p (TREE_CODE (arg1)))
13945         {
13946           /* Only perform transformation if ARG0 is easily inverted.  */
13947           tem = fold_truth_not_expr (loc, arg0);
13948           if (tem)
13949             return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13950                                 fold_convert_loc (loc, type, tem),
13951                                 arg1);
13952         }
13953
13954       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13955       if (integer_zerop (arg1)
13956           && truth_value_p (TREE_CODE (arg0))
13957           && truth_value_p (TREE_CODE (op2)))
13958         {
13959           /* Only perform transformation if ARG0 is easily inverted.  */
13960           tem = fold_truth_not_expr (loc, arg0);
13961           if (tem)
13962             return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13963                                 fold_convert_loc (loc, type, tem),
13964                                 op2);
13965         }
13966
13967       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13968       if (integer_onep (arg1)
13969           && truth_value_p (TREE_CODE (arg0))
13970           && truth_value_p (TREE_CODE (op2)))
13971         return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13972                             fold_convert_loc (loc, type, arg0),
13973                             op2);
13974
13975       return NULL_TREE;
13976
13977     case CALL_EXPR:
13978       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13979          of fold_ternary on them.  */
13980       gcc_unreachable ();
13981
13982     case BIT_FIELD_REF:
13983       if ((TREE_CODE (arg0) == VECTOR_CST
13984            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13985           && type == TREE_TYPE (TREE_TYPE (arg0)))
13986         {
13987           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13988           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13989
13990           if (width != 0
13991               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13992               && (idx % width) == 0
13993               && (idx = idx / width)
13994                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13995             {
13996               tree elements = NULL_TREE;
13997
13998               if (TREE_CODE (arg0) == VECTOR_CST)
13999                 elements = TREE_VECTOR_CST_ELTS (arg0);
14000               else
14001                 {
14002                   unsigned HOST_WIDE_INT idx;
14003                   tree value;
14004
14005                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
14006                     elements = tree_cons (NULL_TREE, value, elements);
14007                 }
14008               while (idx-- > 0 && elements)
14009                 elements = TREE_CHAIN (elements);
14010               if (elements)
14011                 return TREE_VALUE (elements);
14012               else
14013                 return fold_convert_loc (loc, type, integer_zero_node);
14014             }
14015         }
14016
14017       /* A bit-field-ref that referenced the full argument can be stripped.  */
14018       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
14019           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
14020           && integer_zerop (op2))
14021         return fold_convert_loc (loc, type, arg0);
14022
14023       return NULL_TREE;
14024
14025     default:
14026       return NULL_TREE;
14027     } /* switch (code) */
14028 }
14029
14030 /* Perform constant folding and related simplification of EXPR.
14031    The related simplifications include x*1 => x, x*0 => 0, etc.,
14032    and application of the associative law.
14033    NOP_EXPR conversions may be removed freely (as long as we
14034    are careful not to change the type of the overall expression).
14035    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
14036    but we can constant-fold them if they have constant operands.  */
14037
14038 #ifdef ENABLE_FOLD_CHECKING
14039 # define fold(x) fold_1 (x)
14040 static tree fold_1 (tree);
14041 static
14042 #endif
14043 tree
14044 fold (tree expr)
14045 {
14046   const tree t = expr;
14047   enum tree_code code = TREE_CODE (t);
14048   enum tree_code_class kind = TREE_CODE_CLASS (code);
14049   tree tem;
14050   location_t loc = EXPR_LOCATION (expr);
14051
14052   /* Return right away if a constant.  */
14053   if (kind == tcc_constant)
14054     return t;
14055
14056   /* CALL_EXPR-like objects with variable numbers of operands are
14057      treated specially.  */
14058   if (kind == tcc_vl_exp)
14059     {
14060       if (code == CALL_EXPR)
14061         {
14062           tem = fold_call_expr (loc, expr, false);
14063           return tem ? tem : expr;
14064         }
14065       return expr;
14066     }
14067
14068   if (IS_EXPR_CODE_CLASS (kind))
14069     {
14070       tree type = TREE_TYPE (t);
14071       tree op0, op1, op2;
14072
14073       switch (TREE_CODE_LENGTH (code))
14074         {
14075         case 1:
14076           op0 = TREE_OPERAND (t, 0);
14077           tem = fold_unary_loc (loc, code, type, op0);
14078           return tem ? tem : expr;
14079         case 2:
14080           op0 = TREE_OPERAND (t, 0);
14081           op1 = TREE_OPERAND (t, 1);
14082           tem = fold_binary_loc (loc, code, type, op0, op1);
14083           return tem ? tem : expr;
14084         case 3:
14085           op0 = TREE_OPERAND (t, 0);
14086           op1 = TREE_OPERAND (t, 1);
14087           op2 = TREE_OPERAND (t, 2);
14088           tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14089           return tem ? tem : expr;
14090         default:
14091           break;
14092         }
14093     }
14094
14095   switch (code)
14096     {
14097     case ARRAY_REF:
14098       {
14099         tree op0 = TREE_OPERAND (t, 0);
14100         tree op1 = TREE_OPERAND (t, 1);
14101
14102         if (TREE_CODE (op1) == INTEGER_CST
14103             && TREE_CODE (op0) == CONSTRUCTOR
14104             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
14105           {
14106             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
14107             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
14108             unsigned HOST_WIDE_INT begin = 0;
14109
14110             /* Find a matching index by means of a binary search.  */
14111             while (begin != end)
14112               {
14113                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
14114                 tree index = VEC_index (constructor_elt, elts, middle)->index;
14115
14116                 if (TREE_CODE (index) == INTEGER_CST
14117                     && tree_int_cst_lt (index, op1))
14118                   begin = middle + 1;
14119                 else if (TREE_CODE (index) == INTEGER_CST
14120                          && tree_int_cst_lt (op1, index))
14121                   end = middle;
14122                 else if (TREE_CODE (index) == RANGE_EXPR
14123                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
14124                   begin = middle + 1;
14125                 else if (TREE_CODE (index) == RANGE_EXPR
14126                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
14127                   end = middle;
14128                 else
14129                   return VEC_index (constructor_elt, elts, middle)->value;
14130               }
14131           }
14132
14133         return t;
14134       }
14135
14136     case CONST_DECL:
14137       return fold (DECL_INITIAL (t));
14138
14139     default:
14140       return t;
14141     } /* switch (code) */
14142 }
14143
14144 #ifdef ENABLE_FOLD_CHECKING
14145 #undef fold
14146
14147 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
14148 static void fold_check_failed (const_tree, const_tree);
14149 void print_fold_checksum (const_tree);
14150
14151 /* When --enable-checking=fold, compute a digest of expr before
14152    and after actual fold call to see if fold did not accidentally
14153    change original expr.  */
14154
14155 tree
14156 fold (tree expr)
14157 {
14158   tree ret;
14159   struct md5_ctx ctx;
14160   unsigned char checksum_before[16], checksum_after[16];
14161   htab_t ht;
14162
14163   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14164   md5_init_ctx (&ctx);
14165   fold_checksum_tree (expr, &ctx, ht);
14166   md5_finish_ctx (&ctx, checksum_before);
14167   htab_empty (ht);
14168
14169   ret = fold_1 (expr);
14170
14171   md5_init_ctx (&ctx);
14172   fold_checksum_tree (expr, &ctx, ht);
14173   md5_finish_ctx (&ctx, checksum_after);
14174   htab_delete (ht);
14175
14176   if (memcmp (checksum_before, checksum_after, 16))
14177     fold_check_failed (expr, ret);
14178
14179   return ret;
14180 }
14181
14182 void
14183 print_fold_checksum (const_tree expr)
14184 {
14185   struct md5_ctx ctx;
14186   unsigned char checksum[16], cnt;
14187   htab_t ht;
14188
14189   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14190   md5_init_ctx (&ctx);
14191   fold_checksum_tree (expr, &ctx, ht);
14192   md5_finish_ctx (&ctx, checksum);
14193   htab_delete (ht);
14194   for (cnt = 0; cnt < 16; ++cnt)
14195     fprintf (stderr, "%02x", checksum[cnt]);
14196   putc ('\n', stderr);
14197 }
14198
14199 static void
14200 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
14201 {
14202   internal_error ("fold check: original tree changed by fold");
14203 }
14204
14205 static void
14206 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
14207 {
14208   const void **slot;
14209   enum tree_code code;
14210   union tree_node buf;
14211   int i, len;
14212   
14213 recursive_label:
14214
14215   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
14216                <= sizeof (struct tree_function_decl))
14217               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
14218   if (expr == NULL)
14219     return;
14220   slot = (const void **) htab_find_slot (ht, expr, INSERT);
14221   if (*slot != NULL)
14222     return;
14223   *slot = expr;
14224   code = TREE_CODE (expr);
14225   if (TREE_CODE_CLASS (code) == tcc_declaration
14226       && DECL_ASSEMBLER_NAME_SET_P (expr))
14227     {
14228       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
14229       memcpy ((char *) &buf, expr, tree_size (expr));
14230       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
14231       expr = (tree) &buf;
14232     }
14233   else if (TREE_CODE_CLASS (code) == tcc_type
14234            && (TYPE_POINTER_TO (expr)
14235                || TYPE_REFERENCE_TO (expr)
14236                || TYPE_CACHED_VALUES_P (expr)
14237                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
14238                || TYPE_NEXT_VARIANT (expr)))
14239     {
14240       /* Allow these fields to be modified.  */
14241       tree tmp;
14242       memcpy ((char *) &buf, expr, tree_size (expr));
14243       expr = tmp = (tree) &buf;
14244       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
14245       TYPE_POINTER_TO (tmp) = NULL;
14246       TYPE_REFERENCE_TO (tmp) = NULL;
14247       TYPE_NEXT_VARIANT (tmp) = NULL;
14248       if (TYPE_CACHED_VALUES_P (tmp))
14249         {
14250           TYPE_CACHED_VALUES_P (tmp) = 0;
14251           TYPE_CACHED_VALUES (tmp) = NULL;
14252         }
14253     }
14254   md5_process_bytes (expr, tree_size (expr), ctx);
14255   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
14256   if (TREE_CODE_CLASS (code) != tcc_type
14257       && TREE_CODE_CLASS (code) != tcc_declaration
14258       && code != TREE_LIST
14259       && code != SSA_NAME)
14260     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
14261   switch (TREE_CODE_CLASS (code))
14262     {
14263     case tcc_constant:
14264       switch (code)
14265         {
14266         case STRING_CST:
14267           md5_process_bytes (TREE_STRING_POINTER (expr),
14268                              TREE_STRING_LENGTH (expr), ctx);
14269           break;
14270         case COMPLEX_CST:
14271           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
14272           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
14273           break;
14274         case VECTOR_CST:
14275           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
14276           break;
14277         default:
14278           break;
14279         }
14280       break;
14281     case tcc_exceptional:
14282       switch (code)
14283         {
14284         case TREE_LIST:
14285           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
14286           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
14287           expr = TREE_CHAIN (expr);
14288           goto recursive_label;
14289           break;
14290         case TREE_VEC:
14291           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
14292             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
14293           break;
14294         default:
14295           break;
14296         }
14297       break;
14298     case tcc_expression:
14299     case tcc_reference:
14300     case tcc_comparison:
14301     case tcc_unary:
14302     case tcc_binary:
14303     case tcc_statement:
14304     case tcc_vl_exp:
14305       len = TREE_OPERAND_LENGTH (expr);
14306       for (i = 0; i < len; ++i)
14307         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
14308       break;
14309     case tcc_declaration:
14310       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
14311       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
14312       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
14313         {
14314           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
14315           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
14316           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
14317           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
14318           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
14319         }
14320       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
14321         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
14322           
14323       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
14324         {
14325           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
14326           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
14327           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
14328         }
14329       break;
14330     case tcc_type:
14331       if (TREE_CODE (expr) == ENUMERAL_TYPE)
14332         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
14333       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
14334       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
14335       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
14336       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
14337       if (INTEGRAL_TYPE_P (expr)
14338           || SCALAR_FLOAT_TYPE_P (expr))
14339         {
14340           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
14341           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
14342         }
14343       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
14344       if (TREE_CODE (expr) == RECORD_TYPE
14345           || TREE_CODE (expr) == UNION_TYPE
14346           || TREE_CODE (expr) == QUAL_UNION_TYPE)
14347         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
14348       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
14349       break;
14350     default:
14351       break;
14352     }
14353 }
14354
14355 /* Helper function for outputting the checksum of a tree T.  When
14356    debugging with gdb, you can "define mynext" to be "next" followed
14357    by "call debug_fold_checksum (op0)", then just trace down till the
14358    outputs differ.  */
14359
14360 void
14361 debug_fold_checksum (const_tree t)
14362 {
14363   int i;
14364   unsigned char checksum[16];
14365   struct md5_ctx ctx;
14366   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14367   
14368   md5_init_ctx (&ctx);
14369   fold_checksum_tree (t, &ctx, ht);
14370   md5_finish_ctx (&ctx, checksum);
14371   htab_empty (ht);
14372
14373   for (i = 0; i < 16; i++)
14374     fprintf (stderr, "%d ", checksum[i]);
14375
14376   fprintf (stderr, "\n");
14377 }
14378
14379 #endif
14380
14381 /* Fold a unary tree expression with code CODE of type TYPE with an
14382    operand OP0.  LOC is the location of the resulting expression.
14383    Return a folded expression if successful.  Otherwise, return a tree
14384    expression with code CODE of type TYPE with an operand OP0.  */
14385
14386 tree
14387 fold_build1_stat_loc (location_t loc,
14388                       enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
14389 {
14390   tree tem;
14391 #ifdef ENABLE_FOLD_CHECKING
14392   unsigned char checksum_before[16], checksum_after[16];
14393   struct md5_ctx ctx;
14394   htab_t ht;
14395
14396   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14397   md5_init_ctx (&ctx);
14398   fold_checksum_tree (op0, &ctx, ht);
14399   md5_finish_ctx (&ctx, checksum_before);
14400   htab_empty (ht);
14401 #endif
14402   
14403   tem = fold_unary_loc (loc, code, type, op0);
14404   if (!tem)
14405     {
14406       tem = build1_stat (code, type, op0 PASS_MEM_STAT);
14407       SET_EXPR_LOCATION (tem, loc);
14408     }
14409   
14410 #ifdef ENABLE_FOLD_CHECKING
14411   md5_init_ctx (&ctx);
14412   fold_checksum_tree (op0, &ctx, ht);
14413   md5_finish_ctx (&ctx, checksum_after);
14414   htab_delete (ht);
14415
14416   if (memcmp (checksum_before, checksum_after, 16))
14417     fold_check_failed (op0, tem);
14418 #endif
14419   return tem;
14420 }
14421
14422 /* Fold a binary tree expression with code CODE of type TYPE with
14423    operands OP0 and OP1.  LOC is the location of the resulting
14424    expression.  Return a folded expression if successful.  Otherwise,
14425    return a tree expression with code CODE of type TYPE with operands
14426    OP0 and OP1.  */
14427
14428 tree
14429 fold_build2_stat_loc (location_t loc,
14430                       enum tree_code code, tree type, tree op0, tree op1
14431                       MEM_STAT_DECL)
14432 {
14433   tree tem;
14434 #ifdef ENABLE_FOLD_CHECKING
14435   unsigned char checksum_before_op0[16],
14436                 checksum_before_op1[16],
14437                 checksum_after_op0[16],
14438                 checksum_after_op1[16];
14439   struct md5_ctx ctx;
14440   htab_t ht;
14441
14442   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14443   md5_init_ctx (&ctx);
14444   fold_checksum_tree (op0, &ctx, ht);
14445   md5_finish_ctx (&ctx, checksum_before_op0);
14446   htab_empty (ht);
14447
14448   md5_init_ctx (&ctx);
14449   fold_checksum_tree (op1, &ctx, ht);
14450   md5_finish_ctx (&ctx, checksum_before_op1);
14451   htab_empty (ht);
14452 #endif
14453
14454   tem = fold_binary_loc (loc, code, type, op0, op1);
14455   if (!tem)
14456     {
14457       tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
14458       SET_EXPR_LOCATION (tem, loc);
14459     }
14460   
14461 #ifdef ENABLE_FOLD_CHECKING
14462   md5_init_ctx (&ctx);
14463   fold_checksum_tree (op0, &ctx, ht);
14464   md5_finish_ctx (&ctx, checksum_after_op0);
14465   htab_empty (ht);
14466
14467   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14468     fold_check_failed (op0, tem);
14469   
14470   md5_init_ctx (&ctx);
14471   fold_checksum_tree (op1, &ctx, ht);
14472   md5_finish_ctx (&ctx, checksum_after_op1);
14473   htab_delete (ht);
14474
14475   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14476     fold_check_failed (op1, tem);
14477 #endif
14478   return tem;
14479 }
14480
14481 /* Fold a ternary tree expression with code CODE of type TYPE with
14482    operands OP0, OP1, and OP2.  Return a folded expression if
14483    successful.  Otherwise, return a tree expression with code CODE of
14484    type TYPE with operands OP0, OP1, and OP2.  */
14485
14486 tree
14487 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14488                       tree op0, tree op1, tree op2 MEM_STAT_DECL)
14489 {
14490   tree tem;
14491 #ifdef ENABLE_FOLD_CHECKING
14492   unsigned char checksum_before_op0[16],
14493                 checksum_before_op1[16],
14494                 checksum_before_op2[16],
14495                 checksum_after_op0[16],
14496                 checksum_after_op1[16],
14497                 checksum_after_op2[16];
14498   struct md5_ctx ctx;
14499   htab_t ht;
14500
14501   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14502   md5_init_ctx (&ctx);
14503   fold_checksum_tree (op0, &ctx, ht);
14504   md5_finish_ctx (&ctx, checksum_before_op0);
14505   htab_empty (ht);
14506
14507   md5_init_ctx (&ctx);
14508   fold_checksum_tree (op1, &ctx, ht);
14509   md5_finish_ctx (&ctx, checksum_before_op1);
14510   htab_empty (ht);
14511
14512   md5_init_ctx (&ctx);
14513   fold_checksum_tree (op2, &ctx, ht);
14514   md5_finish_ctx (&ctx, checksum_before_op2);
14515   htab_empty (ht);
14516 #endif
14517
14518   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14519   tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14520   if (!tem)
14521     {
14522       tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
14523       SET_EXPR_LOCATION (tem, loc);
14524     }
14525       
14526 #ifdef ENABLE_FOLD_CHECKING
14527   md5_init_ctx (&ctx);
14528   fold_checksum_tree (op0, &ctx, ht);
14529   md5_finish_ctx (&ctx, checksum_after_op0);
14530   htab_empty (ht);
14531
14532   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14533     fold_check_failed (op0, tem);
14534   
14535   md5_init_ctx (&ctx);
14536   fold_checksum_tree (op1, &ctx, ht);
14537   md5_finish_ctx (&ctx, checksum_after_op1);
14538   htab_empty (ht);
14539
14540   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14541     fold_check_failed (op1, tem);
14542   
14543   md5_init_ctx (&ctx);
14544   fold_checksum_tree (op2, &ctx, ht);
14545   md5_finish_ctx (&ctx, checksum_after_op2);
14546   htab_delete (ht);
14547
14548   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14549     fold_check_failed (op2, tem);
14550 #endif
14551   return tem;
14552 }
14553
14554 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14555    arguments in ARGARRAY, and a null static chain.
14556    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14557    of type TYPE from the given operands as constructed by build_call_array.  */
14558
14559 tree
14560 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14561                            int nargs, tree *argarray)
14562 {
14563   tree tem;
14564 #ifdef ENABLE_FOLD_CHECKING
14565   unsigned char checksum_before_fn[16],
14566                 checksum_before_arglist[16],
14567                 checksum_after_fn[16],
14568                 checksum_after_arglist[16];
14569   struct md5_ctx ctx;
14570   htab_t ht;
14571   int i;
14572
14573   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14574   md5_init_ctx (&ctx);
14575   fold_checksum_tree (fn, &ctx, ht);
14576   md5_finish_ctx (&ctx, checksum_before_fn);
14577   htab_empty (ht);
14578
14579   md5_init_ctx (&ctx);
14580   for (i = 0; i < nargs; i++)
14581     fold_checksum_tree (argarray[i], &ctx, ht);
14582   md5_finish_ctx (&ctx, checksum_before_arglist);
14583   htab_empty (ht);
14584 #endif
14585
14586   tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14587       
14588 #ifdef ENABLE_FOLD_CHECKING
14589   md5_init_ctx (&ctx);
14590   fold_checksum_tree (fn, &ctx, ht);
14591   md5_finish_ctx (&ctx, checksum_after_fn);
14592   htab_empty (ht);
14593
14594   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14595     fold_check_failed (fn, tem);
14596   
14597   md5_init_ctx (&ctx);
14598   for (i = 0; i < nargs; i++)
14599     fold_checksum_tree (argarray[i], &ctx, ht);
14600   md5_finish_ctx (&ctx, checksum_after_arglist);
14601   htab_delete (ht);
14602
14603   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14604     fold_check_failed (NULL_TREE, tem);
14605 #endif
14606   return tem;
14607 }
14608
14609 /* Perform constant folding and related simplification of initializer
14610    expression EXPR.  These behave identically to "fold_buildN" but ignore
14611    potential run-time traps and exceptions that fold must preserve.  */
14612
14613 #define START_FOLD_INIT \
14614   int saved_signaling_nans = flag_signaling_nans;\
14615   int saved_trapping_math = flag_trapping_math;\
14616   int saved_rounding_math = flag_rounding_math;\
14617   int saved_trapv = flag_trapv;\
14618   int saved_folding_initializer = folding_initializer;\
14619   flag_signaling_nans = 0;\
14620   flag_trapping_math = 0;\
14621   flag_rounding_math = 0;\
14622   flag_trapv = 0;\
14623   folding_initializer = 1;
14624
14625 #define END_FOLD_INIT \
14626   flag_signaling_nans = saved_signaling_nans;\
14627   flag_trapping_math = saved_trapping_math;\
14628   flag_rounding_math = saved_rounding_math;\
14629   flag_trapv = saved_trapv;\
14630   folding_initializer = saved_folding_initializer;
14631
14632 tree
14633 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14634                              tree type, tree op)
14635 {
14636   tree result;
14637   START_FOLD_INIT;
14638
14639   result = fold_build1_loc (loc, code, type, op);
14640
14641   END_FOLD_INIT;
14642   return result;
14643 }
14644
14645 tree
14646 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14647                              tree type, tree op0, tree op1)
14648 {
14649   tree result;
14650   START_FOLD_INIT;
14651
14652   result = fold_build2_loc (loc, code, type, op0, op1);
14653
14654   END_FOLD_INIT;
14655   return result;
14656 }
14657
14658 tree
14659 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14660                              tree type, tree op0, tree op1, tree op2)
14661 {
14662   tree result;
14663   START_FOLD_INIT;
14664
14665   result = fold_build3_loc (loc, code, type, op0, op1, op2);
14666
14667   END_FOLD_INIT;
14668   return result;
14669 }
14670
14671 tree
14672 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14673                                        int nargs, tree *argarray)
14674 {
14675   tree result;
14676   START_FOLD_INIT;
14677
14678   result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14679
14680   END_FOLD_INIT;
14681   return result;
14682 }
14683
14684 #undef START_FOLD_INIT
14685 #undef END_FOLD_INIT
14686
14687 /* Determine if first argument is a multiple of second argument.  Return 0 if
14688    it is not, or we cannot easily determined it to be.
14689
14690    An example of the sort of thing we care about (at this point; this routine
14691    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14692    fold cases do now) is discovering that
14693
14694      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14695
14696    is a multiple of
14697
14698      SAVE_EXPR (J * 8)
14699
14700    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14701
14702    This code also handles discovering that
14703
14704      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14705
14706    is a multiple of 8 so we don't have to worry about dealing with a
14707    possible remainder.
14708
14709    Note that we *look* inside a SAVE_EXPR only to determine how it was
14710    calculated; it is not safe for fold to do much of anything else with the
14711    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14712    at run time.  For example, the latter example above *cannot* be implemented
14713    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14714    evaluation time of the original SAVE_EXPR is not necessarily the same at
14715    the time the new expression is evaluated.  The only optimization of this
14716    sort that would be valid is changing
14717
14718      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14719
14720    divided by 8 to
14721
14722      SAVE_EXPR (I) * SAVE_EXPR (J)
14723
14724    (where the same SAVE_EXPR (J) is used in the original and the
14725    transformed version).  */
14726
14727 int
14728 multiple_of_p (tree type, const_tree top, const_tree bottom)
14729 {
14730   if (operand_equal_p (top, bottom, 0))
14731     return 1;
14732
14733   if (TREE_CODE (type) != INTEGER_TYPE)
14734     return 0;
14735
14736   switch (TREE_CODE (top))
14737     {
14738     case BIT_AND_EXPR:
14739       /* Bitwise and provides a power of two multiple.  If the mask is
14740          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14741       if (!integer_pow2p (bottom))
14742         return 0;
14743       /* FALLTHRU */
14744
14745     case MULT_EXPR:
14746       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14747               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14748
14749     case PLUS_EXPR:
14750     case MINUS_EXPR:
14751       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14752               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14753
14754     case LSHIFT_EXPR:
14755       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14756         {
14757           tree op1, t1;
14758
14759           op1 = TREE_OPERAND (top, 1);
14760           /* const_binop may not detect overflow correctly,
14761              so check for it explicitly here.  */
14762           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14763               > TREE_INT_CST_LOW (op1)
14764               && TREE_INT_CST_HIGH (op1) == 0
14765               && 0 != (t1 = fold_convert (type,
14766                                           const_binop (LSHIFT_EXPR,
14767                                                        size_one_node,
14768                                                        op1, 0)))
14769               && !TREE_OVERFLOW (t1))
14770             return multiple_of_p (type, t1, bottom);
14771         }
14772       return 0;
14773
14774     case NOP_EXPR:
14775       /* Can't handle conversions from non-integral or wider integral type.  */
14776       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14777           || (TYPE_PRECISION (type)
14778               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14779         return 0;
14780
14781       /* .. fall through ...  */
14782
14783     case SAVE_EXPR:
14784       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14785
14786     case INTEGER_CST:
14787       if (TREE_CODE (bottom) != INTEGER_CST
14788           || integer_zerop (bottom)
14789           || (TYPE_UNSIGNED (type)
14790               && (tree_int_cst_sgn (top) < 0
14791                   || tree_int_cst_sgn (bottom) < 0)))
14792         return 0;
14793       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14794                                              top, bottom, 0));
14795
14796     default:
14797       return 0;
14798     }
14799 }
14800
14801 /* Return true if CODE or TYPE is known to be non-negative. */
14802
14803 static bool
14804 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14805 {
14806   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14807       && truth_value_p (code))
14808     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14809        have a signed:1 type (where the value is -1 and 0).  */
14810     return true;
14811   return false;
14812 }
14813
14814 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14815    value is based on the assumption that signed overflow is undefined,
14816    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14817    *STRICT_OVERFLOW_P.  */
14818
14819 bool
14820 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14821                                 bool *strict_overflow_p)
14822 {
14823   if (TYPE_UNSIGNED (type))
14824     return true;
14825
14826   switch (code)
14827     {
14828     case ABS_EXPR:
14829       /* We can't return 1 if flag_wrapv is set because
14830          ABS_EXPR<INT_MIN> = INT_MIN.  */
14831       if (!INTEGRAL_TYPE_P (type))
14832         return true;
14833       if (TYPE_OVERFLOW_UNDEFINED (type))
14834         {
14835           *strict_overflow_p = true;
14836           return true;
14837         }
14838       break;
14839
14840     case NON_LVALUE_EXPR:
14841     case FLOAT_EXPR:
14842     case FIX_TRUNC_EXPR:
14843       return tree_expr_nonnegative_warnv_p (op0,
14844                                             strict_overflow_p);
14845
14846     case NOP_EXPR:
14847       {
14848         tree inner_type = TREE_TYPE (op0);
14849         tree outer_type = type;
14850
14851         if (TREE_CODE (outer_type) == REAL_TYPE)
14852           {
14853             if (TREE_CODE (inner_type) == REAL_TYPE)
14854               return tree_expr_nonnegative_warnv_p (op0,
14855                                                     strict_overflow_p);
14856             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14857               {
14858                 if (TYPE_UNSIGNED (inner_type))
14859                   return true;
14860                 return tree_expr_nonnegative_warnv_p (op0,
14861                                                       strict_overflow_p);
14862               }
14863           }
14864         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14865           {
14866             if (TREE_CODE (inner_type) == REAL_TYPE)
14867               return tree_expr_nonnegative_warnv_p (op0,
14868                                                     strict_overflow_p);
14869             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14870               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14871                       && TYPE_UNSIGNED (inner_type);
14872           }
14873       }
14874       break;
14875
14876     default:
14877       return tree_simple_nonnegative_warnv_p (code, type);
14878     }
14879
14880   /* We don't know sign of `t', so be conservative and return false.  */
14881   return false;
14882 }
14883
14884 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14885    value is based on the assumption that signed overflow is undefined,
14886    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14887    *STRICT_OVERFLOW_P.  */
14888
14889 bool
14890 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14891                                       tree op1, bool *strict_overflow_p)
14892 {
14893   if (TYPE_UNSIGNED (type))
14894     return true;
14895
14896   switch (code)
14897     {
14898     case POINTER_PLUS_EXPR:
14899     case PLUS_EXPR:
14900       if (FLOAT_TYPE_P (type))
14901         return (tree_expr_nonnegative_warnv_p (op0,
14902                                                strict_overflow_p)
14903                 && tree_expr_nonnegative_warnv_p (op1,
14904                                                   strict_overflow_p));
14905
14906       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14907          both unsigned and at least 2 bits shorter than the result.  */
14908       if (TREE_CODE (type) == INTEGER_TYPE
14909           && TREE_CODE (op0) == NOP_EXPR
14910           && TREE_CODE (op1) == NOP_EXPR)
14911         {
14912           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14913           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14914           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14915               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14916             {
14917               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14918                                        TYPE_PRECISION (inner2)) + 1;
14919               return prec < TYPE_PRECISION (type);
14920             }
14921         }
14922       break;
14923
14924     case MULT_EXPR:
14925       if (FLOAT_TYPE_P (type))
14926         {
14927           /* x * x for floating point x is always non-negative.  */
14928           if (operand_equal_p (op0, op1, 0))
14929             return true;
14930           return (tree_expr_nonnegative_warnv_p (op0,
14931                                                  strict_overflow_p)
14932                   && tree_expr_nonnegative_warnv_p (op1,
14933                                                     strict_overflow_p));
14934         }
14935
14936       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14937          both unsigned and their total bits is shorter than the result.  */
14938       if (TREE_CODE (type) == INTEGER_TYPE
14939           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14940           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14941         {
14942           tree inner0 = (TREE_CODE (op0) == NOP_EXPR) 
14943             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14944             : TREE_TYPE (op0);
14945           tree inner1 = (TREE_CODE (op1) == NOP_EXPR) 
14946             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14947             : TREE_TYPE (op1);
14948
14949           bool unsigned0 = TYPE_UNSIGNED (inner0);
14950           bool unsigned1 = TYPE_UNSIGNED (inner1);
14951
14952           if (TREE_CODE (op0) == INTEGER_CST)
14953             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14954
14955           if (TREE_CODE (op1) == INTEGER_CST)
14956             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14957
14958           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14959               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14960             {
14961               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14962                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14963                 : TYPE_PRECISION (inner0);
14964
14965               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14966                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14967                 : TYPE_PRECISION (inner1);
14968
14969               return precision0 + precision1 < TYPE_PRECISION (type);
14970             }
14971         }
14972       return false;
14973
14974     case BIT_AND_EXPR:
14975     case MAX_EXPR:
14976       return (tree_expr_nonnegative_warnv_p (op0,
14977                                              strict_overflow_p)
14978               || tree_expr_nonnegative_warnv_p (op1,
14979                                                 strict_overflow_p));
14980
14981     case BIT_IOR_EXPR:
14982     case BIT_XOR_EXPR:
14983     case MIN_EXPR:
14984     case RDIV_EXPR:
14985     case TRUNC_DIV_EXPR:
14986     case CEIL_DIV_EXPR:
14987     case FLOOR_DIV_EXPR:
14988     case ROUND_DIV_EXPR:
14989       return (tree_expr_nonnegative_warnv_p (op0,
14990                                              strict_overflow_p)
14991               && tree_expr_nonnegative_warnv_p (op1,
14992                                                 strict_overflow_p));
14993
14994     case TRUNC_MOD_EXPR:
14995     case CEIL_MOD_EXPR:
14996     case FLOOR_MOD_EXPR:
14997     case ROUND_MOD_EXPR:
14998       return tree_expr_nonnegative_warnv_p (op0,
14999                                             strict_overflow_p);
15000     default:
15001       return tree_simple_nonnegative_warnv_p (code, type);
15002     }
15003
15004   /* We don't know sign of `t', so be conservative and return false.  */
15005   return false;
15006 }
15007
15008 /* Return true if T is known to be non-negative.  If the return
15009    value is based on the assumption that signed overflow is undefined,
15010    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15011    *STRICT_OVERFLOW_P.  */
15012
15013 bool
15014 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15015 {
15016   if (TYPE_UNSIGNED (TREE_TYPE (t)))
15017     return true;
15018
15019   switch (TREE_CODE (t))
15020     {
15021     case INTEGER_CST:
15022       return tree_int_cst_sgn (t) >= 0;
15023
15024     case REAL_CST:
15025       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
15026
15027     case FIXED_CST:
15028       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
15029
15030     case COND_EXPR:
15031       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15032                                              strict_overflow_p)
15033               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
15034                                                 strict_overflow_p));
15035     default:
15036       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15037                                                    TREE_TYPE (t));
15038     }
15039   /* We don't know sign of `t', so be conservative and return false.  */
15040   return false;
15041 }
15042
15043 /* Return true if T is known to be non-negative.  If the return
15044    value is based on the assumption that signed overflow is undefined,
15045    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15046    *STRICT_OVERFLOW_P.  */
15047
15048 bool
15049 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
15050                                tree arg0, tree arg1, bool *strict_overflow_p)
15051 {
15052   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
15053     switch (DECL_FUNCTION_CODE (fndecl))
15054       {
15055         CASE_FLT_FN (BUILT_IN_ACOS):
15056         CASE_FLT_FN (BUILT_IN_ACOSH):
15057         CASE_FLT_FN (BUILT_IN_CABS):
15058         CASE_FLT_FN (BUILT_IN_COSH):
15059         CASE_FLT_FN (BUILT_IN_ERFC):
15060         CASE_FLT_FN (BUILT_IN_EXP):
15061         CASE_FLT_FN (BUILT_IN_EXP10):
15062         CASE_FLT_FN (BUILT_IN_EXP2):
15063         CASE_FLT_FN (BUILT_IN_FABS):
15064         CASE_FLT_FN (BUILT_IN_FDIM):
15065         CASE_FLT_FN (BUILT_IN_HYPOT):
15066         CASE_FLT_FN (BUILT_IN_POW10):
15067         CASE_INT_FN (BUILT_IN_FFS):
15068         CASE_INT_FN (BUILT_IN_PARITY):
15069         CASE_INT_FN (BUILT_IN_POPCOUNT):
15070       case BUILT_IN_BSWAP32:
15071       case BUILT_IN_BSWAP64:
15072         /* Always true.  */
15073         return true;
15074
15075         CASE_FLT_FN (BUILT_IN_SQRT):
15076         /* sqrt(-0.0) is -0.0.  */
15077         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
15078           return true;
15079         return tree_expr_nonnegative_warnv_p (arg0,
15080                                               strict_overflow_p);
15081
15082         CASE_FLT_FN (BUILT_IN_ASINH):
15083         CASE_FLT_FN (BUILT_IN_ATAN):
15084         CASE_FLT_FN (BUILT_IN_ATANH):
15085         CASE_FLT_FN (BUILT_IN_CBRT):
15086         CASE_FLT_FN (BUILT_IN_CEIL):
15087         CASE_FLT_FN (BUILT_IN_ERF):
15088         CASE_FLT_FN (BUILT_IN_EXPM1):
15089         CASE_FLT_FN (BUILT_IN_FLOOR):
15090         CASE_FLT_FN (BUILT_IN_FMOD):
15091         CASE_FLT_FN (BUILT_IN_FREXP):
15092         CASE_FLT_FN (BUILT_IN_LCEIL):
15093         CASE_FLT_FN (BUILT_IN_LDEXP):
15094         CASE_FLT_FN (BUILT_IN_LFLOOR):
15095         CASE_FLT_FN (BUILT_IN_LLCEIL):
15096         CASE_FLT_FN (BUILT_IN_LLFLOOR):
15097         CASE_FLT_FN (BUILT_IN_LLRINT):
15098         CASE_FLT_FN (BUILT_IN_LLROUND):
15099         CASE_FLT_FN (BUILT_IN_LRINT):
15100         CASE_FLT_FN (BUILT_IN_LROUND):
15101         CASE_FLT_FN (BUILT_IN_MODF):
15102         CASE_FLT_FN (BUILT_IN_NEARBYINT):
15103         CASE_FLT_FN (BUILT_IN_RINT):
15104         CASE_FLT_FN (BUILT_IN_ROUND):
15105         CASE_FLT_FN (BUILT_IN_SCALB):
15106         CASE_FLT_FN (BUILT_IN_SCALBLN):
15107         CASE_FLT_FN (BUILT_IN_SCALBN):
15108         CASE_FLT_FN (BUILT_IN_SIGNBIT):
15109         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
15110         CASE_FLT_FN (BUILT_IN_SINH):
15111         CASE_FLT_FN (BUILT_IN_TANH):
15112         CASE_FLT_FN (BUILT_IN_TRUNC):
15113         /* True if the 1st argument is nonnegative.  */
15114         return tree_expr_nonnegative_warnv_p (arg0,
15115                                               strict_overflow_p);
15116
15117         CASE_FLT_FN (BUILT_IN_FMAX):
15118         /* True if the 1st OR 2nd arguments are nonnegative.  */
15119         return (tree_expr_nonnegative_warnv_p (arg0,
15120                                                strict_overflow_p)
15121                 || (tree_expr_nonnegative_warnv_p (arg1,
15122                                                    strict_overflow_p)));
15123
15124         CASE_FLT_FN (BUILT_IN_FMIN):
15125         /* True if the 1st AND 2nd arguments are nonnegative.  */
15126         return (tree_expr_nonnegative_warnv_p (arg0,
15127                                                strict_overflow_p)
15128                 && (tree_expr_nonnegative_warnv_p (arg1,
15129                                                    strict_overflow_p)));
15130
15131         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15132         /* True if the 2nd argument is nonnegative.  */
15133         return tree_expr_nonnegative_warnv_p (arg1,
15134                                               strict_overflow_p);
15135
15136         CASE_FLT_FN (BUILT_IN_POWI):
15137         /* True if the 1st argument is nonnegative or the second
15138            argument is an even integer.  */
15139         if (TREE_CODE (arg1) == INTEGER_CST
15140             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15141           return true;
15142         return tree_expr_nonnegative_warnv_p (arg0,
15143                                               strict_overflow_p);
15144
15145         CASE_FLT_FN (BUILT_IN_POW):
15146         /* True if the 1st argument is nonnegative or the second
15147            argument is an even integer valued real.  */
15148         if (TREE_CODE (arg1) == REAL_CST)
15149           {
15150             REAL_VALUE_TYPE c;
15151             HOST_WIDE_INT n;
15152
15153             c = TREE_REAL_CST (arg1);
15154             n = real_to_integer (&c);
15155             if ((n & 1) == 0)
15156               {
15157                 REAL_VALUE_TYPE cint;
15158                 real_from_integer (&cint, VOIDmode, n,
15159                                    n < 0 ? -1 : 0, 0);
15160                 if (real_identical (&c, &cint))
15161                   return true;
15162               }
15163           }
15164         return tree_expr_nonnegative_warnv_p (arg0,
15165                                               strict_overflow_p);
15166
15167       default:
15168         break;
15169       }
15170   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
15171                                           type);
15172 }
15173
15174 /* Return true if T is known to be non-negative.  If the return
15175    value is based on the assumption that signed overflow is undefined,
15176    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15177    *STRICT_OVERFLOW_P.  */
15178
15179 bool
15180 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15181 {
15182   enum tree_code code = TREE_CODE (t);
15183   if (TYPE_UNSIGNED (TREE_TYPE (t)))
15184     return true;
15185
15186   switch (code)
15187     {
15188     case TARGET_EXPR:
15189       {
15190         tree temp = TARGET_EXPR_SLOT (t);
15191         t = TARGET_EXPR_INITIAL (t);
15192
15193         /* If the initializer is non-void, then it's a normal expression
15194            that will be assigned to the slot.  */
15195         if (!VOID_TYPE_P (t))
15196           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
15197
15198         /* Otherwise, the initializer sets the slot in some way.  One common
15199            way is an assignment statement at the end of the initializer.  */
15200         while (1)
15201           {
15202             if (TREE_CODE (t) == BIND_EXPR)
15203               t = expr_last (BIND_EXPR_BODY (t));
15204             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15205                      || TREE_CODE (t) == TRY_CATCH_EXPR)
15206               t = expr_last (TREE_OPERAND (t, 0));
15207             else if (TREE_CODE (t) == STATEMENT_LIST)
15208               t = expr_last (t);
15209             else
15210               break;
15211           }
15212         if (TREE_CODE (t) == MODIFY_EXPR
15213             && TREE_OPERAND (t, 0) == temp)
15214           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15215                                                 strict_overflow_p);
15216
15217         return false;
15218       }
15219
15220     case CALL_EXPR:
15221       {
15222         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
15223         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
15224
15225         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15226                                               get_callee_fndecl (t),
15227                                               arg0,
15228                                               arg1,
15229                                               strict_overflow_p);
15230       }
15231     case COMPOUND_EXPR:
15232     case MODIFY_EXPR:
15233       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15234                                             strict_overflow_p);
15235     case BIND_EXPR:
15236       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
15237                                             strict_overflow_p);
15238     case SAVE_EXPR:
15239       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
15240                                             strict_overflow_p);
15241
15242     default:
15243       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15244                                                    TREE_TYPE (t));
15245     }
15246
15247   /* We don't know sign of `t', so be conservative and return false.  */
15248   return false;
15249 }
15250
15251 /* Return true if T is known to be non-negative.  If the return
15252    value is based on the assumption that signed overflow is undefined,
15253    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15254    *STRICT_OVERFLOW_P.  */
15255
15256 bool
15257 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15258 {
15259   enum tree_code code;
15260   if (t == error_mark_node)
15261     return false;
15262
15263   code = TREE_CODE (t);
15264   switch (TREE_CODE_CLASS (code))
15265     {
15266     case tcc_binary:
15267     case tcc_comparison:
15268       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15269                                               TREE_TYPE (t),
15270                                               TREE_OPERAND (t, 0),
15271                                               TREE_OPERAND (t, 1),
15272                                               strict_overflow_p);
15273
15274     case tcc_unary:
15275       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15276                                              TREE_TYPE (t),
15277                                              TREE_OPERAND (t, 0),
15278                                              strict_overflow_p);
15279
15280     case tcc_constant:
15281     case tcc_declaration:
15282     case tcc_reference:
15283       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15284
15285     default:
15286       break;
15287     }
15288
15289   switch (code)
15290     {
15291     case TRUTH_AND_EXPR:
15292     case TRUTH_OR_EXPR:
15293     case TRUTH_XOR_EXPR:
15294       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15295                                               TREE_TYPE (t),
15296                                               TREE_OPERAND (t, 0),
15297                                               TREE_OPERAND (t, 1),
15298                                               strict_overflow_p);
15299     case TRUTH_NOT_EXPR:
15300       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15301                                              TREE_TYPE (t),
15302                                              TREE_OPERAND (t, 0),
15303                                              strict_overflow_p);
15304
15305     case COND_EXPR:
15306     case CONSTRUCTOR:
15307     case OBJ_TYPE_REF:
15308     case ASSERT_EXPR:
15309     case ADDR_EXPR:
15310     case WITH_SIZE_EXPR:
15311     case SSA_NAME:
15312       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15313
15314     default:
15315       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
15316     }
15317 }
15318
15319 /* Return true if `t' is known to be non-negative.  Handle warnings
15320    about undefined signed overflow.  */
15321
15322 bool
15323 tree_expr_nonnegative_p (tree t)
15324 {
15325   bool ret, strict_overflow_p;
15326
15327   strict_overflow_p = false;
15328   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15329   if (strict_overflow_p)
15330     fold_overflow_warning (("assuming signed overflow does not occur when "
15331                             "determining that expression is always "
15332                             "non-negative"),
15333                            WARN_STRICT_OVERFLOW_MISC);
15334   return ret;
15335 }
15336
15337
15338 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15339    For floating point we further ensure that T is not denormal.
15340    Similar logic is present in nonzero_address in rtlanal.h.
15341
15342    If the return value is based on the assumption that signed overflow
15343    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15344    change *STRICT_OVERFLOW_P.  */
15345
15346 bool
15347 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15348                                  bool *strict_overflow_p)
15349 {
15350   switch (code)
15351     {
15352     case ABS_EXPR:
15353       return tree_expr_nonzero_warnv_p (op0,
15354                                         strict_overflow_p);
15355
15356     case NOP_EXPR:
15357       {
15358         tree inner_type = TREE_TYPE (op0);
15359         tree outer_type = type;
15360
15361         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15362                 && tree_expr_nonzero_warnv_p (op0,
15363                                               strict_overflow_p));
15364       }
15365       break;
15366
15367     case NON_LVALUE_EXPR:
15368       return tree_expr_nonzero_warnv_p (op0,
15369                                         strict_overflow_p);
15370
15371     default:
15372       break;
15373   }
15374
15375   return false;
15376 }
15377
15378 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15379    For floating point we further ensure that T is not denormal.
15380    Similar logic is present in nonzero_address in rtlanal.h.
15381
15382    If the return value is based on the assumption that signed overflow
15383    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15384    change *STRICT_OVERFLOW_P.  */
15385
15386 bool
15387 tree_binary_nonzero_warnv_p (enum tree_code code,
15388                              tree type,
15389                              tree op0,
15390                              tree op1, bool *strict_overflow_p)
15391 {
15392   bool sub_strict_overflow_p;
15393   switch (code)
15394     {
15395     case POINTER_PLUS_EXPR:
15396     case PLUS_EXPR:
15397       if (TYPE_OVERFLOW_UNDEFINED (type))
15398         {
15399           /* With the presence of negative values it is hard
15400              to say something.  */
15401           sub_strict_overflow_p = false;
15402           if (!tree_expr_nonnegative_warnv_p (op0,
15403                                               &sub_strict_overflow_p)
15404               || !tree_expr_nonnegative_warnv_p (op1,
15405                                                  &sub_strict_overflow_p))
15406             return false;
15407           /* One of operands must be positive and the other non-negative.  */
15408           /* We don't set *STRICT_OVERFLOW_P here: even if this value
15409              overflows, on a twos-complement machine the sum of two
15410              nonnegative numbers can never be zero.  */
15411           return (tree_expr_nonzero_warnv_p (op0,
15412                                              strict_overflow_p)
15413                   || tree_expr_nonzero_warnv_p (op1,
15414                                                 strict_overflow_p));
15415         }
15416       break;
15417
15418     case MULT_EXPR:
15419       if (TYPE_OVERFLOW_UNDEFINED (type))
15420         {
15421           if (tree_expr_nonzero_warnv_p (op0,
15422                                          strict_overflow_p)
15423               && tree_expr_nonzero_warnv_p (op1,
15424                                             strict_overflow_p))
15425             {
15426               *strict_overflow_p = true;
15427               return true;
15428             }
15429         }
15430       break;
15431
15432     case MIN_EXPR:
15433       sub_strict_overflow_p = false;
15434       if (tree_expr_nonzero_warnv_p (op0,
15435                                      &sub_strict_overflow_p)
15436           && tree_expr_nonzero_warnv_p (op1,
15437                                         &sub_strict_overflow_p))
15438         {
15439           if (sub_strict_overflow_p)
15440             *strict_overflow_p = true;
15441         }
15442       break;
15443
15444     case MAX_EXPR:
15445       sub_strict_overflow_p = false;
15446       if (tree_expr_nonzero_warnv_p (op0,
15447                                      &sub_strict_overflow_p))
15448         {
15449           if (sub_strict_overflow_p)
15450             *strict_overflow_p = true;
15451
15452           /* When both operands are nonzero, then MAX must be too.  */
15453           if (tree_expr_nonzero_warnv_p (op1,
15454                                          strict_overflow_p))
15455             return true;
15456
15457           /* MAX where operand 0 is positive is positive.  */
15458           return tree_expr_nonnegative_warnv_p (op0,
15459                                                strict_overflow_p);
15460         }
15461       /* MAX where operand 1 is positive is positive.  */
15462       else if (tree_expr_nonzero_warnv_p (op1,
15463                                           &sub_strict_overflow_p)
15464                && tree_expr_nonnegative_warnv_p (op1,
15465                                                  &sub_strict_overflow_p))
15466         {
15467           if (sub_strict_overflow_p)
15468             *strict_overflow_p = true;
15469           return true;
15470         }
15471       break;
15472
15473     case BIT_IOR_EXPR:
15474       return (tree_expr_nonzero_warnv_p (op1,
15475                                          strict_overflow_p)
15476               || tree_expr_nonzero_warnv_p (op0,
15477                                             strict_overflow_p));
15478
15479     default:
15480       break;
15481   }
15482
15483   return false;
15484 }
15485
15486 /* Return true when T is an address and is known to be nonzero.
15487    For floating point we further ensure that T is not denormal.
15488    Similar logic is present in nonzero_address in rtlanal.h.
15489
15490    If the return value is based on the assumption that signed overflow
15491    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15492    change *STRICT_OVERFLOW_P.  */
15493
15494 bool
15495 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15496 {
15497   bool sub_strict_overflow_p;
15498   switch (TREE_CODE (t))
15499     {
15500     case INTEGER_CST:
15501       return !integer_zerop (t);
15502
15503     case ADDR_EXPR:
15504       {
15505         tree base = get_base_address (TREE_OPERAND (t, 0));
15506
15507         if (!base)
15508           return false;
15509
15510         /* Weak declarations may link to NULL.  Other things may also be NULL
15511            so protect with -fdelete-null-pointer-checks; but not variables
15512            allocated on the stack.  */
15513         if (DECL_P (base)
15514             && (flag_delete_null_pointer_checks
15515                 || (TREE_CODE (base) == VAR_DECL && !TREE_STATIC (base))))
15516           return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15517
15518         /* Constants are never weak.  */
15519         if (CONSTANT_CLASS_P (base))
15520           return true;
15521
15522         return false;
15523       }
15524
15525     case COND_EXPR:
15526       sub_strict_overflow_p = false;
15527       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15528                                      &sub_strict_overflow_p)
15529           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15530                                         &sub_strict_overflow_p))
15531         {
15532           if (sub_strict_overflow_p)
15533             *strict_overflow_p = true;
15534           return true;
15535         }
15536       break;
15537
15538     default:
15539       break;
15540     }
15541   return false;
15542 }
15543
15544 /* Return true when T is an address and is known to be nonzero.
15545    For floating point we further ensure that T is not denormal.
15546    Similar logic is present in nonzero_address in rtlanal.h.
15547
15548    If the return value is based on the assumption that signed overflow
15549    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15550    change *STRICT_OVERFLOW_P.  */
15551
15552 bool
15553 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15554 {
15555   tree type = TREE_TYPE (t);
15556   enum tree_code code;
15557
15558   /* Doing something useful for floating point would need more work.  */
15559   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15560     return false;
15561
15562   code = TREE_CODE (t);
15563   switch (TREE_CODE_CLASS (code))
15564     {
15565     case tcc_unary:
15566       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15567                                               strict_overflow_p);
15568     case tcc_binary:
15569     case tcc_comparison:
15570       return tree_binary_nonzero_warnv_p (code, type,
15571                                                TREE_OPERAND (t, 0),
15572                                                TREE_OPERAND (t, 1),
15573                                                strict_overflow_p);
15574     case tcc_constant:
15575     case tcc_declaration:
15576     case tcc_reference:
15577       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15578
15579     default:
15580       break;
15581     }
15582
15583   switch (code)
15584     {
15585     case TRUTH_NOT_EXPR:
15586       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15587                                               strict_overflow_p);
15588
15589     case TRUTH_AND_EXPR:
15590     case TRUTH_OR_EXPR:
15591     case TRUTH_XOR_EXPR:
15592       return tree_binary_nonzero_warnv_p (code, type,
15593                                                TREE_OPERAND (t, 0),
15594                                                TREE_OPERAND (t, 1),
15595                                                strict_overflow_p);
15596
15597     case COND_EXPR:
15598     case CONSTRUCTOR:
15599     case OBJ_TYPE_REF:
15600     case ASSERT_EXPR:
15601     case ADDR_EXPR:
15602     case WITH_SIZE_EXPR:
15603     case SSA_NAME:
15604       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15605
15606     case COMPOUND_EXPR:
15607     case MODIFY_EXPR:
15608     case BIND_EXPR:
15609       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15610                                         strict_overflow_p);
15611
15612     case SAVE_EXPR:
15613       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15614                                         strict_overflow_p);
15615
15616     case CALL_EXPR:
15617       return alloca_call_p (t);
15618
15619     default:
15620       break;
15621     }
15622   return false;
15623 }
15624
15625 /* Return true when T is an address and is known to be nonzero.
15626    Handle warnings about undefined signed overflow.  */
15627
15628 bool
15629 tree_expr_nonzero_p (tree t)
15630 {
15631   bool ret, strict_overflow_p;
15632
15633   strict_overflow_p = false;
15634   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15635   if (strict_overflow_p)
15636     fold_overflow_warning (("assuming signed overflow does not occur when "
15637                             "determining that expression is always "
15638                             "non-zero"),
15639                            WARN_STRICT_OVERFLOW_MISC);
15640   return ret;
15641 }
15642
15643 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15644    attempt to fold the expression to a constant without modifying TYPE,
15645    OP0 or OP1.
15646
15647    If the expression could be simplified to a constant, then return
15648    the constant.  If the expression would not be simplified to a
15649    constant, then return NULL_TREE.  */
15650
15651 tree
15652 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15653 {
15654   tree tem = fold_binary (code, type, op0, op1);
15655   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15656 }
15657
15658 /* Given the components of a unary expression CODE, TYPE and OP0,
15659    attempt to fold the expression to a constant without modifying
15660    TYPE or OP0.
15661
15662    If the expression could be simplified to a constant, then return
15663    the constant.  If the expression would not be simplified to a
15664    constant, then return NULL_TREE.  */
15665
15666 tree
15667 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15668 {
15669   tree tem = fold_unary (code, type, op0);
15670   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15671 }
15672
15673 /* If EXP represents referencing an element in a constant string
15674    (either via pointer arithmetic or array indexing), return the
15675    tree representing the value accessed, otherwise return NULL.  */
15676
15677 tree
15678 fold_read_from_constant_string (tree exp)
15679 {
15680   if ((TREE_CODE (exp) == INDIRECT_REF
15681        || TREE_CODE (exp) == ARRAY_REF)
15682       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15683     {
15684       tree exp1 = TREE_OPERAND (exp, 0);
15685       tree index;
15686       tree string;
15687       location_t loc = EXPR_LOCATION (exp);
15688
15689       if (TREE_CODE (exp) == INDIRECT_REF)
15690         string = string_constant (exp1, &index);
15691       else
15692         {
15693           tree low_bound = array_ref_low_bound (exp);
15694           index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15695
15696           /* Optimize the special-case of a zero lower bound.
15697
15698              We convert the low_bound to sizetype to avoid some problems
15699              with constant folding.  (E.g. suppose the lower bound is 1,
15700              and its mode is QI.  Without the conversion,l (ARRAY
15701              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15702              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15703           if (! integer_zerop (low_bound))
15704             index = size_diffop_loc (loc, index,
15705                                  fold_convert_loc (loc, sizetype, low_bound));
15706
15707           string = exp1;
15708         }
15709
15710       if (string
15711           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15712           && TREE_CODE (string) == STRING_CST
15713           && TREE_CODE (index) == INTEGER_CST
15714           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15715           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15716               == MODE_INT)
15717           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15718         return build_int_cst_type (TREE_TYPE (exp),
15719                                    (TREE_STRING_POINTER (string)
15720                                     [TREE_INT_CST_LOW (index)]));
15721     }
15722   return NULL;
15723 }
15724
15725 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15726    an integer constant, real, or fixed-point constant.
15727
15728    TYPE is the type of the result.  */
15729
15730 static tree
15731 fold_negate_const (tree arg0, tree type)
15732 {
15733   tree t = NULL_TREE;
15734
15735   switch (TREE_CODE (arg0))
15736     {
15737     case INTEGER_CST:
15738       {
15739         unsigned HOST_WIDE_INT low;
15740         HOST_WIDE_INT high;
15741         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15742                                    TREE_INT_CST_HIGH (arg0),
15743                                    &low, &high);
15744         t = force_fit_type_double (type, low, high, 1,
15745                                    (overflow | TREE_OVERFLOW (arg0))
15746                                    && !TYPE_UNSIGNED (type));
15747         break;
15748       }
15749
15750     case REAL_CST:
15751       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15752       break;
15753
15754     case FIXED_CST:
15755       {
15756         FIXED_VALUE_TYPE f;
15757         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15758                                             &(TREE_FIXED_CST (arg0)), NULL,
15759                                             TYPE_SATURATING (type));
15760         t = build_fixed (type, f);
15761         /* Propagate overflow flags.  */
15762         if (overflow_p | TREE_OVERFLOW (arg0))
15763           TREE_OVERFLOW (t) = 1;
15764         break;
15765       }
15766
15767     default:
15768       gcc_unreachable ();
15769     }
15770
15771   return t;
15772 }
15773
15774 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15775    an integer constant or real constant.
15776
15777    TYPE is the type of the result.  */
15778
15779 tree
15780 fold_abs_const (tree arg0, tree type)
15781 {
15782   tree t = NULL_TREE;
15783
15784   switch (TREE_CODE (arg0))
15785     {
15786     case INTEGER_CST:
15787       /* If the value is unsigned, then the absolute value is
15788          the same as the ordinary value.  */
15789       if (TYPE_UNSIGNED (type))
15790         t = arg0;
15791       /* Similarly, if the value is non-negative.  */
15792       else if (INT_CST_LT (integer_minus_one_node, arg0))
15793         t = arg0;
15794       /* If the value is negative, then the absolute value is
15795          its negation.  */
15796       else
15797         {
15798           unsigned HOST_WIDE_INT low;
15799           HOST_WIDE_INT high;
15800           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15801                                      TREE_INT_CST_HIGH (arg0),
15802                                      &low, &high);
15803           t = force_fit_type_double (type, low, high, -1,
15804                                      overflow | TREE_OVERFLOW (arg0));
15805         }
15806       break;
15807
15808     case REAL_CST:
15809       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15810         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15811       else
15812         t =  arg0;
15813       break;
15814
15815     default:
15816       gcc_unreachable ();
15817     }
15818
15819   return t;
15820 }
15821
15822 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15823    constant.  TYPE is the type of the result.  */
15824
15825 static tree
15826 fold_not_const (tree arg0, tree type)
15827 {
15828   tree t = NULL_TREE;
15829
15830   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15831
15832   t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15833                              ~TREE_INT_CST_HIGH (arg0), 0,
15834                              TREE_OVERFLOW (arg0));
15835
15836   return t;
15837 }
15838
15839 /* Given CODE, a relational operator, the target type, TYPE and two
15840    constant operands OP0 and OP1, return the result of the
15841    relational operation.  If the result is not a compile time
15842    constant, then return NULL_TREE.  */
15843
15844 static tree
15845 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15846 {
15847   int result, invert;
15848
15849   /* From here on, the only cases we handle are when the result is
15850      known to be a constant.  */
15851
15852   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15853     {
15854       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15855       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15856
15857       /* Handle the cases where either operand is a NaN.  */
15858       if (real_isnan (c0) || real_isnan (c1))
15859         {
15860           switch (code)
15861             {
15862             case EQ_EXPR:
15863             case ORDERED_EXPR:
15864               result = 0;
15865               break;
15866
15867             case NE_EXPR:
15868             case UNORDERED_EXPR:
15869             case UNLT_EXPR:
15870             case UNLE_EXPR:
15871             case UNGT_EXPR:
15872             case UNGE_EXPR:
15873             case UNEQ_EXPR:
15874               result = 1;
15875               break;
15876
15877             case LT_EXPR:
15878             case LE_EXPR:
15879             case GT_EXPR:
15880             case GE_EXPR:
15881             case LTGT_EXPR:
15882               if (flag_trapping_math)
15883                 return NULL_TREE;
15884               result = 0;
15885               break;
15886
15887             default:
15888               gcc_unreachable ();
15889             }
15890
15891           return constant_boolean_node (result, type);
15892         }
15893
15894       return constant_boolean_node (real_compare (code, c0, c1), type);
15895     }
15896
15897   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15898     {
15899       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15900       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15901       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15902     }
15903
15904   /* Handle equality/inequality of complex constants.  */
15905   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15906     {
15907       tree rcond = fold_relational_const (code, type,
15908                                           TREE_REALPART (op0),
15909                                           TREE_REALPART (op1));
15910       tree icond = fold_relational_const (code, type,
15911                                           TREE_IMAGPART (op0),
15912                                           TREE_IMAGPART (op1));
15913       if (code == EQ_EXPR)
15914         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15915       else if (code == NE_EXPR)
15916         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15917       else
15918         return NULL_TREE;
15919     }
15920
15921   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15922
15923      To compute GT, swap the arguments and do LT.
15924      To compute GE, do LT and invert the result.
15925      To compute LE, swap the arguments, do LT and invert the result.
15926      To compute NE, do EQ and invert the result.
15927
15928      Therefore, the code below must handle only EQ and LT.  */
15929
15930   if (code == LE_EXPR || code == GT_EXPR)
15931     {
15932       tree tem = op0;
15933       op0 = op1;
15934       op1 = tem;
15935       code = swap_tree_comparison (code);
15936     }
15937
15938   /* Note that it is safe to invert for real values here because we
15939      have already handled the one case that it matters.  */
15940
15941   invert = 0;
15942   if (code == NE_EXPR || code == GE_EXPR)
15943     {
15944       invert = 1;
15945       code = invert_tree_comparison (code, false);
15946     }
15947
15948   /* Compute a result for LT or EQ if args permit;
15949      Otherwise return T.  */
15950   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15951     {
15952       if (code == EQ_EXPR)
15953         result = tree_int_cst_equal (op0, op1);
15954       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15955         result = INT_CST_LT_UNSIGNED (op0, op1);
15956       else
15957         result = INT_CST_LT (op0, op1);
15958     }
15959   else
15960     return NULL_TREE;
15961
15962   if (invert)
15963     result ^= 1;
15964   return constant_boolean_node (result, type);
15965 }
15966
15967 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15968    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15969    itself.  */
15970
15971 tree
15972 fold_build_cleanup_point_expr (tree type, tree expr)
15973 {
15974   /* If the expression does not have side effects then we don't have to wrap
15975      it with a cleanup point expression.  */
15976   if (!TREE_SIDE_EFFECTS (expr))
15977     return expr;
15978
15979   /* If the expression is a return, check to see if the expression inside the
15980      return has no side effects or the right hand side of the modify expression
15981      inside the return. If either don't have side effects set we don't need to
15982      wrap the expression in a cleanup point expression.  Note we don't check the
15983      left hand side of the modify because it should always be a return decl.  */
15984   if (TREE_CODE (expr) == RETURN_EXPR)
15985     {
15986       tree op = TREE_OPERAND (expr, 0);
15987       if (!op || !TREE_SIDE_EFFECTS (op))
15988         return expr;
15989       op = TREE_OPERAND (op, 1);
15990       if (!TREE_SIDE_EFFECTS (op))
15991         return expr;
15992     }
15993   
15994   return build1 (CLEANUP_POINT_EXPR, type, expr);
15995 }
15996
15997 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15998    of an indirection through OP0, or NULL_TREE if no simplification is
15999    possible.  */
16000
16001 tree
16002 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16003 {
16004   tree sub = op0;
16005   tree subtype;
16006
16007   STRIP_NOPS (sub);
16008   subtype = TREE_TYPE (sub);
16009   if (!POINTER_TYPE_P (subtype))
16010     return NULL_TREE;
16011
16012   if (TREE_CODE (sub) == ADDR_EXPR)
16013     {
16014       tree op = TREE_OPERAND (sub, 0);
16015       tree optype = TREE_TYPE (op);
16016       /* *&CONST_DECL -> to the value of the const decl.  */
16017       if (TREE_CODE (op) == CONST_DECL)
16018         return DECL_INITIAL (op);
16019       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
16020       if (type == optype)
16021         {
16022           tree fop = fold_read_from_constant_string (op);
16023           if (fop)
16024             return fop;
16025           else
16026             return op;
16027         }
16028       /* *(foo *)&fooarray => fooarray[0] */
16029       else if (TREE_CODE (optype) == ARRAY_TYPE
16030                && type == TREE_TYPE (optype))
16031         {
16032           tree type_domain = TYPE_DOMAIN (optype);
16033           tree min_val = size_zero_node;
16034           if (type_domain && TYPE_MIN_VALUE (type_domain))
16035             min_val = TYPE_MIN_VALUE (type_domain);
16036           op0 = build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
16037           SET_EXPR_LOCATION (op0, loc);
16038           return op0;
16039         }
16040       /* *(foo *)&complexfoo => __real__ complexfoo */
16041       else if (TREE_CODE (optype) == COMPLEX_TYPE
16042                && type == TREE_TYPE (optype))
16043         return fold_build1_loc (loc, REALPART_EXPR, type, op);
16044       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16045       else if (TREE_CODE (optype) == VECTOR_TYPE
16046                && type == TREE_TYPE (optype))
16047         {
16048           tree part_width = TYPE_SIZE (type);
16049           tree index = bitsize_int (0);
16050           return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
16051         }
16052     }
16053
16054   /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16055   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16056       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
16057     { 
16058       tree op00 = TREE_OPERAND (sub, 0);
16059       tree op01 = TREE_OPERAND (sub, 1);
16060       tree op00type;
16061       
16062       STRIP_NOPS (op00);
16063       op00type = TREE_TYPE (op00);
16064       if (TREE_CODE (op00) == ADDR_EXPR
16065           && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
16066           && type == TREE_TYPE (TREE_TYPE (op00type)))
16067         { 
16068           HOST_WIDE_INT offset = tree_low_cst (op01, 0);
16069           tree part_width = TYPE_SIZE (type);
16070           unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
16071           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
16072           tree index = bitsize_int (indexi);
16073
16074           if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
16075             return fold_build3_loc (loc,
16076                                 BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
16077                                 part_width, index);
16078         
16079         }
16080     }
16081
16082
16083   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16084   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16085       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
16086     {
16087       tree op00 = TREE_OPERAND (sub, 0);
16088       tree op01 = TREE_OPERAND (sub, 1);
16089       tree op00type;
16090
16091       STRIP_NOPS (op00);
16092       op00type = TREE_TYPE (op00);
16093       if (TREE_CODE (op00) == ADDR_EXPR
16094           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
16095           && type == TREE_TYPE (TREE_TYPE (op00type)))
16096         {
16097           tree size = TYPE_SIZE_UNIT (type);
16098           if (tree_int_cst_equal (size, op01))
16099             return fold_build1_loc (loc, IMAGPART_EXPR, type,
16100                                 TREE_OPERAND (op00, 0));
16101         }
16102     }
16103   
16104   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16105   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16106       && type == TREE_TYPE (TREE_TYPE (subtype)))
16107     {
16108       tree type_domain;
16109       tree min_val = size_zero_node;
16110       sub = build_fold_indirect_ref_loc (loc, sub);
16111       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16112       if (type_domain && TYPE_MIN_VALUE (type_domain))
16113         min_val = TYPE_MIN_VALUE (type_domain);
16114       op0 = build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
16115       SET_EXPR_LOCATION (op0, loc);
16116       return op0;
16117     }
16118
16119   return NULL_TREE;
16120 }
16121
16122 /* Builds an expression for an indirection through T, simplifying some
16123    cases.  */
16124
16125 tree
16126 build_fold_indirect_ref_loc (location_t loc, tree t)
16127 {
16128   tree type = TREE_TYPE (TREE_TYPE (t));
16129   tree sub = fold_indirect_ref_1 (loc, type, t);
16130
16131   if (sub)
16132     return sub;
16133
16134   t = build1 (INDIRECT_REF, type, t);
16135   SET_EXPR_LOCATION (t, loc);
16136   return t;
16137 }
16138
16139 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
16140
16141 tree
16142 fold_indirect_ref_loc (location_t loc, tree t)
16143 {
16144   tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16145
16146   if (sub)
16147     return sub;
16148   else
16149     return t;
16150 }
16151
16152 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16153    whose result is ignored.  The type of the returned tree need not be
16154    the same as the original expression.  */
16155
16156 tree
16157 fold_ignored_result (tree t)
16158 {
16159   if (!TREE_SIDE_EFFECTS (t))
16160     return integer_zero_node;
16161
16162   for (;;)
16163     switch (TREE_CODE_CLASS (TREE_CODE (t)))
16164       {
16165       case tcc_unary:
16166         t = TREE_OPERAND (t, 0);
16167         break;
16168
16169       case tcc_binary:
16170       case tcc_comparison:
16171         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16172           t = TREE_OPERAND (t, 0);
16173         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16174           t = TREE_OPERAND (t, 1);
16175         else
16176           return t;
16177         break;
16178
16179       case tcc_expression:
16180         switch (TREE_CODE (t))
16181           {
16182           case COMPOUND_EXPR:
16183             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16184               return t;
16185             t = TREE_OPERAND (t, 0);
16186             break;
16187
16188           case COND_EXPR:
16189             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16190                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16191               return t;
16192             t = TREE_OPERAND (t, 0);
16193             break;
16194
16195           default:
16196             return t;
16197           }
16198         break;
16199
16200       default:
16201         return t;
16202       }
16203 }
16204
16205 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
16206    This can only be applied to objects of a sizetype.  */
16207
16208 tree
16209 round_up_loc (location_t loc, tree value, int divisor)
16210 {
16211   tree div = NULL_TREE;
16212
16213   gcc_assert (divisor > 0);
16214   if (divisor == 1)
16215     return value;
16216
16217   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
16218      have to do anything.  Only do this when we are not given a const,
16219      because in that case, this check is more expensive than just
16220      doing it.  */
16221   if (TREE_CODE (value) != INTEGER_CST)
16222     {
16223       div = build_int_cst (TREE_TYPE (value), divisor);
16224
16225       if (multiple_of_p (TREE_TYPE (value), value, div))
16226         return value;
16227     }
16228
16229   /* If divisor is a power of two, simplify this to bit manipulation.  */
16230   if (divisor == (divisor & -divisor))
16231     {
16232       if (TREE_CODE (value) == INTEGER_CST)
16233         {
16234           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
16235           unsigned HOST_WIDE_INT high;
16236           bool overflow_p;
16237
16238           if ((low & (divisor - 1)) == 0)
16239             return value;
16240
16241           overflow_p = TREE_OVERFLOW (value);
16242           high = TREE_INT_CST_HIGH (value);
16243           low &= ~(divisor - 1);
16244           low += divisor;
16245           if (low == 0)
16246             {
16247               high++;
16248               if (high == 0)
16249                 overflow_p = true;
16250             }
16251
16252           return force_fit_type_double (TREE_TYPE (value), low, high,
16253                                         -1, overflow_p);
16254         }
16255       else
16256         {
16257           tree t;
16258
16259           t = build_int_cst (TREE_TYPE (value), divisor - 1);
16260           value = size_binop_loc (loc, PLUS_EXPR, value, t);
16261           t = build_int_cst (TREE_TYPE (value), -divisor);
16262           value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16263         }
16264     }
16265   else
16266     {
16267       if (!div)
16268         div = build_int_cst (TREE_TYPE (value), divisor);
16269       value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16270       value = size_binop_loc (loc, MULT_EXPR, value, div);
16271     }
16272
16273   return value;
16274 }
16275
16276 /* Likewise, but round down.  */
16277
16278 tree
16279 round_down_loc (location_t loc, tree value, int divisor)
16280 {
16281   tree div = NULL_TREE;
16282
16283   gcc_assert (divisor > 0);
16284   if (divisor == 1)
16285     return value;
16286
16287   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
16288      have to do anything.  Only do this when we are not given a const,
16289      because in that case, this check is more expensive than just
16290      doing it.  */
16291   if (TREE_CODE (value) != INTEGER_CST)
16292     {
16293       div = build_int_cst (TREE_TYPE (value), divisor);
16294
16295       if (multiple_of_p (TREE_TYPE (value), value, div))
16296         return value;
16297     }
16298
16299   /* If divisor is a power of two, simplify this to bit manipulation.  */
16300   if (divisor == (divisor & -divisor))
16301     {
16302       tree t;
16303
16304       t = build_int_cst (TREE_TYPE (value), -divisor);
16305       value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16306     }
16307   else
16308     {
16309       if (!div)
16310         div = build_int_cst (TREE_TYPE (value), divisor);
16311       value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16312       value = size_binop_loc (loc, MULT_EXPR, value, div);
16313     }
16314
16315   return value;
16316 }
16317
16318 /* Returns the pointer to the base of the object addressed by EXP and
16319    extracts the information about the offset of the access, storing it
16320    to PBITPOS and POFFSET.  */
16321
16322 static tree
16323 split_address_to_core_and_offset (tree exp,
16324                                   HOST_WIDE_INT *pbitpos, tree *poffset)
16325 {
16326   tree core;
16327   enum machine_mode mode;
16328   int unsignedp, volatilep;
16329   HOST_WIDE_INT bitsize;
16330   location_t loc = EXPR_LOCATION (exp);
16331
16332   if (TREE_CODE (exp) == ADDR_EXPR)
16333     {
16334       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16335                                   poffset, &mode, &unsignedp, &volatilep,
16336                                   false);
16337       core = build_fold_addr_expr_loc (loc, core);
16338     }
16339   else
16340     {
16341       core = exp;
16342       *pbitpos = 0;
16343       *poffset = NULL_TREE;
16344     }
16345
16346   return core;
16347 }
16348
16349 /* Returns true if addresses of E1 and E2 differ by a constant, false
16350    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
16351
16352 bool
16353 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
16354 {
16355   tree core1, core2;
16356   HOST_WIDE_INT bitpos1, bitpos2;
16357   tree toffset1, toffset2, tdiff, type;
16358
16359   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16360   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16361
16362   if (bitpos1 % BITS_PER_UNIT != 0
16363       || bitpos2 % BITS_PER_UNIT != 0
16364       || !operand_equal_p (core1, core2, 0))
16365     return false;
16366
16367   if (toffset1 && toffset2)
16368     {
16369       type = TREE_TYPE (toffset1);
16370       if (type != TREE_TYPE (toffset2))
16371         toffset2 = fold_convert (type, toffset2);
16372
16373       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16374       if (!cst_and_fits_in_hwi (tdiff))
16375         return false;
16376
16377       *diff = int_cst_value (tdiff);
16378     }
16379   else if (toffset1 || toffset2)
16380     {
16381       /* If only one of the offsets is non-constant, the difference cannot
16382          be a constant.  */
16383       return false;
16384     }
16385   else
16386     *diff = 0;
16387
16388   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
16389   return true;
16390 }
16391
16392 /* Simplify the floating point expression EXP when the sign of the
16393    result is not significant.  Return NULL_TREE if no simplification
16394    is possible.  */
16395
16396 tree
16397 fold_strip_sign_ops (tree exp)
16398 {
16399   tree arg0, arg1;
16400   location_t loc = EXPR_LOCATION (exp);
16401
16402   switch (TREE_CODE (exp))
16403     {
16404     case ABS_EXPR:
16405     case NEGATE_EXPR:
16406       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16407       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
16408
16409     case MULT_EXPR:
16410     case RDIV_EXPR:
16411       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
16412         return NULL_TREE;
16413       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16414       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16415       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
16416         return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
16417                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
16418                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
16419       break;
16420
16421     case COMPOUND_EXPR:
16422       arg0 = TREE_OPERAND (exp, 0);
16423       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16424       if (arg1)
16425         return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16426       break;
16427       
16428     case COND_EXPR:
16429       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16430       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16431       if (arg0 || arg1)
16432         return fold_build3_loc (loc,
16433                             COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16434                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
16435                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
16436       break;
16437       
16438     case CALL_EXPR:
16439       {
16440         const enum built_in_function fcode = builtin_mathfn_code (exp);
16441         switch (fcode)
16442         {
16443         CASE_FLT_FN (BUILT_IN_COPYSIGN):
16444           /* Strip copysign function call, return the 1st argument. */
16445           arg0 = CALL_EXPR_ARG (exp, 0);
16446           arg1 = CALL_EXPR_ARG (exp, 1);
16447           return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16448
16449         default:
16450           /* Strip sign ops from the argument of "odd" math functions.  */
16451           if (negate_mathfn_p (fcode))
16452             {
16453               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16454               if (arg0)
16455                 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
16456             }
16457           break;
16458         }
16459       }
16460       break;
16461
16462     default:
16463       break;
16464     }
16465   return NULL_TREE;
16466 }