gcc50/csu: Skip depends step to avoid possible race
[dragonfly.git] / contrib / gcc-4.4 / 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 (tree, tree, enum tree_code, tree);
103 static tree const_binop (enum tree_code, tree, tree, int);
104 static enum comparison_code comparison_to_compcode (enum tree_code);
105 static enum tree_code compcode_to_comparison (enum comparison_code);
106 static tree combine_comparisons (enum tree_code, enum tree_code,
107                                  enum tree_code, tree, tree, tree);
108 static int operand_equal_for_comparison_p (tree, tree, tree);
109 static int twoval_comparison_p (tree, tree *, tree *, int *);
110 static tree eval_subst (tree, tree, tree, tree, tree);
111 static tree pedantic_omit_one_operand (tree, tree, tree);
112 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
113 static tree make_bit_field_ref (tree, tree, HOST_WIDE_INT, HOST_WIDE_INT, int);
114 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
115 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
116                                     enum machine_mode *, int *, int *,
117                                     tree *, tree *);
118 static int all_ones_mask_p (const_tree, int);
119 static tree sign_bit_p (tree, const_tree);
120 static int simple_operand_p (const_tree);
121 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
122 static tree range_predecessor (tree);
123 static tree range_successor (tree);
124 static tree make_range (tree, int *, tree *, tree *, bool *);
125 static tree build_range_check (tree, tree, int, tree, tree);
126 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
127                          tree);
128 static tree fold_range_test (enum tree_code, tree, tree, tree);
129 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
130 static tree unextend (tree, int, int, tree);
131 static tree fold_truthop (enum tree_code, tree, tree, tree);
132 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
133 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
134 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
135 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
136                                                  tree, tree,
137                                                  tree, tree, int);
138 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
139                                  tree, tree, tree);
140 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
141 static tree fold_div_compare (enum tree_code, tree, tree, tree);
142 static bool reorder_operands_p (const_tree, const_tree);
143 static tree fold_negate_const (tree, tree);
144 static tree fold_not_const (tree, tree);
145 static tree fold_relational_const (enum tree_code, tree, tree, tree);
146
147
148 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
149    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
150    and SUM1.  Then this yields nonzero if overflow occurred during the
151    addition.
152
153    Overflow occurs if A and B have the same sign, but A and SUM differ in
154    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
155    sign.  */
156 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
157 \f
158 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
159    We do that by representing the two-word integer in 4 words, with only
160    HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
161    number.  The value of the word is LOWPART + HIGHPART * BASE.  */
162
163 #define LOWPART(x) \
164   ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
165 #define HIGHPART(x) \
166   ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
167 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
168
169 /* Unpack a two-word integer into 4 words.
170    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
171    WORDS points to the array of HOST_WIDE_INTs.  */
172
173 static void
174 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
175 {
176   words[0] = LOWPART (low);
177   words[1] = HIGHPART (low);
178   words[2] = LOWPART (hi);
179   words[3] = HIGHPART (hi);
180 }
181
182 /* Pack an array of 4 words into a two-word integer.
183    WORDS points to the array of words.
184    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
185
186 static void
187 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
188         HOST_WIDE_INT *hi)
189 {
190   *low = words[0] + words[1] * BASE;
191   *hi = words[2] + words[3] * BASE;
192 }
193 \f
194 /* Force the double-word integer L1, H1 to be within the range of the
195    integer type TYPE.  Stores the properly truncated and sign-extended
196    double-word integer in *LV, *HV.  Returns true if the operation
197    overflows, that is, argument and result are different.  */
198
199 int
200 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
201                  unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
202 {
203   unsigned HOST_WIDE_INT low0 = l1;
204   HOST_WIDE_INT high0 = h1;
205   unsigned int prec;
206   int sign_extended_type;
207
208   if (POINTER_TYPE_P (type)
209       || TREE_CODE (type) == OFFSET_TYPE)
210     prec = POINTER_SIZE;
211   else
212     prec = TYPE_PRECISION (type);
213
214   /* Size types *are* sign extended.  */
215   sign_extended_type = (!TYPE_UNSIGNED (type)
216                         || (TREE_CODE (type) == INTEGER_TYPE
217                             && TYPE_IS_SIZETYPE (type)));
218
219   /* First clear all bits that are beyond the type's precision.  */
220   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
221     ;
222   else if (prec > HOST_BITS_PER_WIDE_INT)
223     h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
224   else
225     {
226       h1 = 0;
227       if (prec < HOST_BITS_PER_WIDE_INT)
228         l1 &= ~((HOST_WIDE_INT) (-1) << prec);
229     }
230
231   /* Then do sign extension if necessary.  */
232   if (!sign_extended_type)
233     /* No sign extension */;
234   else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
235     /* Correct width already.  */;
236   else if (prec > HOST_BITS_PER_WIDE_INT)
237     {
238       /* Sign extend top half? */
239       if (h1 & ((unsigned HOST_WIDE_INT)1
240                 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
241         h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
242     }
243   else if (prec == HOST_BITS_PER_WIDE_INT)
244     {
245       if ((HOST_WIDE_INT)l1 < 0)
246         h1 = -1;
247     }
248   else
249     {
250       /* Sign extend bottom half? */
251       if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
252         {
253           h1 = -1;
254           l1 |= (HOST_WIDE_INT)(-1) << prec;
255         }
256     }
257
258   *lv = l1;
259   *hv = h1;
260
261   /* If the value didn't fit, signal overflow.  */
262   return l1 != low0 || h1 != high0;
263 }
264
265 /* We force the double-int HIGH:LOW to the range of the type TYPE by
266    sign or zero extending it.
267    OVERFLOWABLE indicates if we are interested
268    in overflow of the value, when >0 we are only interested in signed
269    overflow, for <0 we are interested in any overflow.  OVERFLOWED
270    indicates whether overflow has already occurred.  CONST_OVERFLOWED
271    indicates whether constant overflow has already occurred.  We force
272    T's value to be within range of T's type (by setting to 0 or 1 all
273    the bits outside the type's range).  We set TREE_OVERFLOWED if,
274         OVERFLOWED is nonzero,
275         or OVERFLOWABLE is >0 and signed overflow occurs
276         or OVERFLOWABLE is <0 and any overflow occurs
277    We return a new tree node for the extended double-int.  The node
278    is shared if no overflow flags are set.  */
279
280 tree
281 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
282                        HOST_WIDE_INT high, int overflowable,
283                        bool overflowed)
284 {
285   int sign_extended_type;
286   bool overflow;
287
288   /* Size types *are* sign extended.  */
289   sign_extended_type = (!TYPE_UNSIGNED (type)
290                         || (TREE_CODE (type) == INTEGER_TYPE
291                             && TYPE_IS_SIZETYPE (type)));
292
293   overflow = fit_double_type (low, high, &low, &high, type);
294
295   /* If we need to set overflow flags, return a new unshared node.  */
296   if (overflowed || overflow)
297     {
298       if (overflowed
299           || overflowable < 0
300           || (overflowable > 0 && sign_extended_type))
301         {
302           tree t = make_node (INTEGER_CST);
303           TREE_INT_CST_LOW (t) = low;
304           TREE_INT_CST_HIGH (t) = high;
305           TREE_TYPE (t) = type;
306           TREE_OVERFLOW (t) = 1;
307           return t;
308         }
309     }
310
311   /* Else build a shared node.  */
312   return build_int_cst_wide (type, low, high);
313 }
314 \f
315 /* Add two doubleword integers with doubleword result.
316    Return nonzero if the operation overflows according to UNSIGNED_P.
317    Each argument is given as two `HOST_WIDE_INT' pieces.
318    One argument is L1 and H1; the other, L2 and H2.
319    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
320
321 int
322 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
323                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
324                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
325                       bool unsigned_p)
326 {
327   unsigned HOST_WIDE_INT l;
328   HOST_WIDE_INT h;
329
330   l = l1 + l2;
331   h = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) h1
332                        + (unsigned HOST_WIDE_INT) h2
333                        + (l < l1));
334
335   *lv = l;
336   *hv = h;
337
338   if (unsigned_p)
339     return ((unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1
340             || (h == h1
341                 && l < l1));
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 static 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 = 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 (OPT_Wstrict_overflow, "%H%s", &locus, 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 (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 (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 (COMPLEX_EXPR, type,
1288                             fold_negate_expr (TREE_OPERAND (t, 0)),
1289                             fold_negate_expr (TREE_OPERAND (t, 1)));
1290       break;
1291       
1292     case CONJ_EXPR:
1293       if (negate_expr_p (t))
1294         return fold_build1 (CONJ_EXPR, type,
1295                             fold_negate_expr (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (ntype, TREE_OPERAND (t, 0));
1427               temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1428               return fold_convert (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
1449   if (t == NULL_TREE)
1450     return NULL_TREE;
1451
1452   type = TREE_TYPE (t);
1453   STRIP_SIGN_NOPS (t);
1454
1455   tem = fold_negate_expr (t);
1456   if (!tem)
1457     tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1458   return fold_convert (type, tem);
1459 }
1460 \f
1461 /* Split a tree IN into a constant, literal and variable parts that could be
1462    combined with CODE to make IN.  "constant" means an expression with
1463    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1464    commutative arithmetic operation.  Store the constant part into *CONP,
1465    the literal in *LITP and return the variable part.  If a part isn't
1466    present, set it to null.  If the tree does not decompose in this way,
1467    return the entire tree as the variable part and the other parts as null.
1468
1469    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1470    case, we negate an operand that was subtracted.  Except if it is a
1471    literal for which we use *MINUS_LITP instead.
1472
1473    If NEGATE_P is true, we are negating all of IN, again except a literal
1474    for which we use *MINUS_LITP instead.
1475
1476    If IN is itself a literal or constant, return it as appropriate.
1477
1478    Note that we do not guarantee that any of the three values will be the
1479    same type as IN, but they will have the same signedness and mode.  */
1480
1481 static tree
1482 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1483             tree *minus_litp, int negate_p)
1484 {
1485   tree var = 0;
1486
1487   *conp = 0;
1488   *litp = 0;
1489   *minus_litp = 0;
1490
1491   /* Strip any conversions that don't change the machine mode or signedness.  */
1492   STRIP_SIGN_NOPS (in);
1493
1494   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1495       || TREE_CODE (in) == FIXED_CST)
1496     *litp = in;
1497   else if (TREE_CODE (in) == code
1498            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1499                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1500                /* We can associate addition and subtraction together (even
1501                   though the C standard doesn't say so) for integers because
1502                   the value is not affected.  For reals, the value might be
1503                   affected, so we can't.  */
1504                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1505                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1506     {
1507       tree op0 = TREE_OPERAND (in, 0);
1508       tree op1 = TREE_OPERAND (in, 1);
1509       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1510       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1511
1512       /* First see if either of the operands is a literal, then a constant.  */
1513       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1514           || TREE_CODE (op0) == FIXED_CST)
1515         *litp = op0, op0 = 0;
1516       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1517                || TREE_CODE (op1) == FIXED_CST)
1518         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1519
1520       if (op0 != 0 && TREE_CONSTANT (op0))
1521         *conp = op0, op0 = 0;
1522       else if (op1 != 0 && TREE_CONSTANT (op1))
1523         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1524
1525       /* If we haven't dealt with either operand, this is not a case we can
1526          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1527       if (op0 != 0 && op1 != 0)
1528         var = in;
1529       else if (op0 != 0)
1530         var = op0;
1531       else
1532         var = op1, neg_var_p = neg1_p;
1533
1534       /* Now do any needed negations.  */
1535       if (neg_litp_p)
1536         *minus_litp = *litp, *litp = 0;
1537       if (neg_conp_p)
1538         *conp = negate_expr (*conp);
1539       if (neg_var_p)
1540         var = negate_expr (var);
1541     }
1542   else if (TREE_CONSTANT (in))
1543     *conp = in;
1544   else
1545     var = in;
1546
1547   if (negate_p)
1548     {
1549       if (*litp)
1550         *minus_litp = *litp, *litp = 0;
1551       else if (*minus_litp)
1552         *litp = *minus_litp, *minus_litp = 0;
1553       *conp = negate_expr (*conp);
1554       var = negate_expr (var);
1555     }
1556
1557   return var;
1558 }
1559
1560 /* Re-associate trees split by the above function.  T1 and T2 are either
1561    expressions to associate or null.  Return the new expression, if any.  If
1562    we build an operation, do it in TYPE and with CODE.  */
1563
1564 static tree
1565 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1566 {
1567   if (t1 == 0)
1568     return t2;
1569   else if (t2 == 0)
1570     return t1;
1571
1572   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1573      try to fold this since we will have infinite recursion.  But do
1574      deal with any NEGATE_EXPRs.  */
1575   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1576       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1577     {
1578       if (code == PLUS_EXPR)
1579         {
1580           if (TREE_CODE (t1) == NEGATE_EXPR)
1581             return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1582                            fold_convert (type, TREE_OPERAND (t1, 0)));
1583           else if (TREE_CODE (t2) == NEGATE_EXPR)
1584             return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1585                            fold_convert (type, TREE_OPERAND (t2, 0)));
1586           else if (integer_zerop (t2))
1587             return fold_convert (type, t1);
1588         }
1589       else if (code == MINUS_EXPR)
1590         {
1591           if (integer_zerop (t2))
1592             return fold_convert (type, t1);
1593         }
1594
1595       return build2 (code, type, fold_convert (type, t1),
1596                      fold_convert (type, t2));
1597     }
1598
1599   return fold_build2 (code, type, fold_convert (type, t1),
1600                       fold_convert (type, t2));
1601 }
1602 \f
1603 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1604    for use in int_const_binop, size_binop and size_diffop.  */
1605
1606 static bool
1607 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1608 {
1609   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1610     return false;
1611   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1612     return false;
1613
1614   switch (code)
1615     {
1616     case LSHIFT_EXPR:
1617     case RSHIFT_EXPR:
1618     case LROTATE_EXPR:
1619     case RROTATE_EXPR:
1620       return true;
1621
1622     default:
1623       break;
1624     }
1625
1626   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1627          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1628          && TYPE_MODE (type1) == TYPE_MODE (type2);
1629 }
1630
1631
1632 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1633    to produce a new constant.  Return NULL_TREE if we don't know how
1634    to evaluate CODE at compile-time.
1635
1636    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1637
1638 tree
1639 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1640 {
1641   unsigned HOST_WIDE_INT int1l, int2l;
1642   HOST_WIDE_INT int1h, int2h;
1643   unsigned HOST_WIDE_INT low;
1644   HOST_WIDE_INT hi;
1645   unsigned HOST_WIDE_INT garbagel;
1646   HOST_WIDE_INT garbageh;
1647   tree t;
1648   tree type = TREE_TYPE (arg1);
1649   int uns = TYPE_UNSIGNED (type);
1650   int is_sizetype
1651     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1652   int overflow = 0;
1653
1654   int1l = TREE_INT_CST_LOW (arg1);
1655   int1h = TREE_INT_CST_HIGH (arg1);
1656   int2l = TREE_INT_CST_LOW (arg2);
1657   int2h = TREE_INT_CST_HIGH (arg2);
1658
1659   switch (code)
1660     {
1661     case BIT_IOR_EXPR:
1662       low = int1l | int2l, hi = int1h | int2h;
1663       break;
1664
1665     case BIT_XOR_EXPR:
1666       low = int1l ^ int2l, hi = int1h ^ int2h;
1667       break;
1668
1669     case BIT_AND_EXPR:
1670       low = int1l & int2l, hi = int1h & int2h;
1671       break;
1672
1673     case RSHIFT_EXPR:
1674       int2l = -int2l;
1675     case LSHIFT_EXPR:
1676       /* It's unclear from the C standard whether shifts can overflow.
1677          The following code ignores overflow; perhaps a C standard
1678          interpretation ruling is needed.  */
1679       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1680                      &low, &hi, !uns);
1681       break;
1682
1683     case RROTATE_EXPR:
1684       int2l = - int2l;
1685     case LROTATE_EXPR:
1686       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1687                       &low, &hi);
1688       break;
1689
1690     case PLUS_EXPR:
1691       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1692       break;
1693
1694     case MINUS_EXPR:
1695       neg_double (int2l, int2h, &low, &hi);
1696       add_double (int1l, int1h, low, hi, &low, &hi);
1697       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1698       break;
1699
1700     case MULT_EXPR:
1701       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1702       break;
1703
1704     case TRUNC_DIV_EXPR:
1705     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1706     case EXACT_DIV_EXPR:
1707       /* This is a shortcut for a common special case.  */
1708       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1709           && !TREE_OVERFLOW (arg1)
1710           && !TREE_OVERFLOW (arg2)
1711           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1712         {
1713           if (code == CEIL_DIV_EXPR)
1714             int1l += int2l - 1;
1715
1716           low = int1l / int2l, hi = 0;
1717           break;
1718         }
1719
1720       /* ... fall through ...  */
1721
1722     case ROUND_DIV_EXPR:
1723       if (int2h == 0 && int2l == 0)
1724         return NULL_TREE;
1725       if (int2h == 0 && int2l == 1)
1726         {
1727           low = int1l, hi = int1h;
1728           break;
1729         }
1730       if (int1l == int2l && int1h == int2h
1731           && ! (int1l == 0 && int1h == 0))
1732         {
1733           low = 1, hi = 0;
1734           break;
1735         }
1736       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1737                                        &low, &hi, &garbagel, &garbageh);
1738       break;
1739
1740     case TRUNC_MOD_EXPR:
1741     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1742       /* This is a shortcut for a common special case.  */
1743       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1744           && !TREE_OVERFLOW (arg1)
1745           && !TREE_OVERFLOW (arg2)
1746           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1747         {
1748           if (code == CEIL_MOD_EXPR)
1749             int1l += int2l - 1;
1750           low = int1l % int2l, hi = 0;
1751           break;
1752         }
1753
1754       /* ... fall through ...  */
1755
1756     case ROUND_MOD_EXPR:
1757       if (int2h == 0 && int2l == 0)
1758         return NULL_TREE;
1759       overflow = div_and_round_double (code, uns,
1760                                        int1l, int1h, int2l, int2h,
1761                                        &garbagel, &garbageh, &low, &hi);
1762       break;
1763
1764     case MIN_EXPR:
1765     case MAX_EXPR:
1766       if (uns)
1767         low = (((unsigned HOST_WIDE_INT) int1h
1768                 < (unsigned HOST_WIDE_INT) int2h)
1769                || (((unsigned HOST_WIDE_INT) int1h
1770                     == (unsigned HOST_WIDE_INT) int2h)
1771                    && int1l < int2l));
1772       else
1773         low = (int1h < int2h
1774                || (int1h == int2h && int1l < int2l));
1775
1776       if (low == (code == MIN_EXPR))
1777         low = int1l, hi = int1h;
1778       else
1779         low = int2l, hi = int2h;
1780       break;
1781
1782     default:
1783       return NULL_TREE;
1784     }
1785
1786   if (notrunc)
1787     {
1788       t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1789
1790       /* Propagate overflow flags ourselves.  */
1791       if (((!uns || is_sizetype) && overflow)
1792           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1793         {
1794           t = copy_node (t);
1795           TREE_OVERFLOW (t) = 1;
1796         }
1797     }
1798   else
1799     t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1800                                ((!uns || is_sizetype) && overflow)
1801                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1802
1803   return t;
1804 }
1805
1806 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1807    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1808    are the same kind of constant and the same machine mode.  Return zero if
1809    combining the constants is not allowed in the current operating mode.
1810
1811    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1812
1813 static tree
1814 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1815 {
1816   /* Sanity check for the recursive cases.  */
1817   if (!arg1 || !arg2)
1818     return NULL_TREE;
1819
1820   STRIP_NOPS (arg1);
1821   STRIP_NOPS (arg2);
1822
1823   if (TREE_CODE (arg1) == INTEGER_CST)
1824     return int_const_binop (code, arg1, arg2, notrunc);
1825
1826   if (TREE_CODE (arg1) == REAL_CST)
1827     {
1828       enum machine_mode mode;
1829       REAL_VALUE_TYPE d1;
1830       REAL_VALUE_TYPE d2;
1831       REAL_VALUE_TYPE value;
1832       REAL_VALUE_TYPE result;
1833       bool inexact;
1834       tree t, type;
1835
1836       /* The following codes are handled by real_arithmetic.  */
1837       switch (code)
1838         {
1839         case PLUS_EXPR:
1840         case MINUS_EXPR:
1841         case MULT_EXPR:
1842         case RDIV_EXPR:
1843         case MIN_EXPR:
1844         case MAX_EXPR:
1845           break;
1846
1847         default:
1848           return NULL_TREE;
1849         }
1850
1851       d1 = TREE_REAL_CST (arg1);
1852       d2 = TREE_REAL_CST (arg2);
1853
1854       type = TREE_TYPE (arg1);
1855       mode = TYPE_MODE (type);
1856
1857       /* Don't perform operation if we honor signaling NaNs and
1858          either operand is a NaN.  */
1859       if (HONOR_SNANS (mode)
1860           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1861         return NULL_TREE;
1862
1863       /* Don't perform operation if it would raise a division
1864          by zero exception.  */
1865       if (code == RDIV_EXPR
1866           && REAL_VALUES_EQUAL (d2, dconst0)
1867           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1868         return NULL_TREE;
1869
1870       /* If either operand is a NaN, just return it.  Otherwise, set up
1871          for floating-point trap; we return an overflow.  */
1872       if (REAL_VALUE_ISNAN (d1))
1873         return arg1;
1874       else if (REAL_VALUE_ISNAN (d2))
1875         return arg2;
1876
1877       inexact = real_arithmetic (&value, code, &d1, &d2);
1878       real_convert (&result, mode, &value);
1879
1880       /* Don't constant fold this floating point operation if
1881          the result has overflowed and flag_trapping_math.  */
1882       if (flag_trapping_math
1883           && MODE_HAS_INFINITIES (mode)
1884           && REAL_VALUE_ISINF (result)
1885           && !REAL_VALUE_ISINF (d1)
1886           && !REAL_VALUE_ISINF (d2))
1887         return NULL_TREE;
1888
1889       /* Don't constant fold this floating point operation if the
1890          result may dependent upon the run-time rounding mode and
1891          flag_rounding_math is set, or if GCC's software emulation
1892          is unable to accurately represent the result.  */
1893       if ((flag_rounding_math
1894            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1895           && (inexact || !real_identical (&result, &value)))
1896         return NULL_TREE;
1897
1898       t = build_real (type, result);
1899
1900       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1901       return t;
1902     }
1903
1904   if (TREE_CODE (arg1) == FIXED_CST)
1905     {
1906       FIXED_VALUE_TYPE f1;
1907       FIXED_VALUE_TYPE f2;
1908       FIXED_VALUE_TYPE result;
1909       tree t, type;
1910       int sat_p;
1911       bool overflow_p;
1912
1913       /* The following codes are handled by fixed_arithmetic.  */
1914       switch (code)
1915         {
1916         case PLUS_EXPR:
1917         case MINUS_EXPR:
1918         case MULT_EXPR:
1919         case TRUNC_DIV_EXPR:
1920           f2 = TREE_FIXED_CST (arg2);
1921           break;
1922
1923         case LSHIFT_EXPR:
1924         case RSHIFT_EXPR:
1925           f2.data.high = TREE_INT_CST_HIGH (arg2);
1926           f2.data.low = TREE_INT_CST_LOW (arg2);
1927           f2.mode = SImode;
1928           break;
1929
1930         default:
1931           return NULL_TREE;
1932         }
1933
1934       f1 = TREE_FIXED_CST (arg1);
1935       type = TREE_TYPE (arg1);
1936       sat_p = TYPE_SATURATING (type);
1937       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1938       t = build_fixed (type, result);
1939       /* Propagate overflow flags.  */
1940       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1941         {
1942           TREE_OVERFLOW (t) = 1;
1943           TREE_CONSTANT_OVERFLOW (t) = 1;
1944         }
1945       else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1946         TREE_CONSTANT_OVERFLOW (t) = 1;
1947       return t;
1948     }
1949
1950   if (TREE_CODE (arg1) == COMPLEX_CST)
1951     {
1952       tree type = TREE_TYPE (arg1);
1953       tree r1 = TREE_REALPART (arg1);
1954       tree i1 = TREE_IMAGPART (arg1);
1955       tree r2 = TREE_REALPART (arg2);
1956       tree i2 = TREE_IMAGPART (arg2);
1957       tree real, imag;
1958
1959       switch (code)
1960         {
1961         case PLUS_EXPR:
1962         case MINUS_EXPR:
1963           real = const_binop (code, r1, r2, notrunc);
1964           imag = const_binop (code, i1, i2, notrunc);
1965           break;
1966
1967         case MULT_EXPR:
1968           real = const_binop (MINUS_EXPR,
1969                               const_binop (MULT_EXPR, r1, r2, notrunc),
1970                               const_binop (MULT_EXPR, i1, i2, notrunc),
1971                               notrunc);
1972           imag = const_binop (PLUS_EXPR,
1973                               const_binop (MULT_EXPR, r1, i2, notrunc),
1974                               const_binop (MULT_EXPR, i1, r2, notrunc),
1975                               notrunc);
1976           break;
1977
1978         case RDIV_EXPR:
1979           {
1980             tree magsquared
1981               = const_binop (PLUS_EXPR,
1982                              const_binop (MULT_EXPR, r2, r2, notrunc),
1983                              const_binop (MULT_EXPR, i2, i2, notrunc),
1984                              notrunc);
1985             tree t1
1986               = const_binop (PLUS_EXPR,
1987                              const_binop (MULT_EXPR, r1, r2, notrunc),
1988                              const_binop (MULT_EXPR, i1, i2, notrunc),
1989                              notrunc);
1990             tree t2
1991               = const_binop (MINUS_EXPR,
1992                              const_binop (MULT_EXPR, i1, r2, notrunc),
1993                              const_binop (MULT_EXPR, r1, i2, notrunc),
1994                              notrunc);
1995
1996             if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1997               code = TRUNC_DIV_EXPR;
1998
1999             real = const_binop (code, t1, magsquared, notrunc);
2000             imag = const_binop (code, t2, magsquared, notrunc);
2001           }
2002           break;
2003
2004         default:
2005           return NULL_TREE;
2006         }
2007
2008       if (real && imag)
2009         return build_complex (type, real, imag);
2010     }
2011
2012   return NULL_TREE;
2013 }
2014
2015 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
2016    indicates which particular sizetype to create.  */
2017
2018 tree
2019 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2020 {
2021   return build_int_cst (sizetype_tab[(int) kind], number);
2022 }
2023 \f
2024 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
2025    is a tree code.  The type of the result is taken from the operands.
2026    Both must be equivalent integer types, ala int_binop_types_match_p.
2027    If the operands are constant, so is the result.  */
2028
2029 tree
2030 size_binop (enum tree_code code, tree arg0, tree arg1)
2031 {
2032   tree type = TREE_TYPE (arg0);
2033
2034   if (arg0 == error_mark_node || arg1 == error_mark_node)
2035     return error_mark_node;
2036
2037   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2038                                        TREE_TYPE (arg1)));
2039
2040   /* Handle the special case of two integer constants faster.  */
2041   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2042     {
2043       /* And some specific cases even faster than that.  */
2044       if (code == PLUS_EXPR)
2045         {
2046           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2047             return arg1;
2048           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2049             return arg0;
2050         }
2051       else if (code == MINUS_EXPR)
2052         {
2053           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2054             return arg0;
2055         }
2056       else if (code == MULT_EXPR)
2057         {
2058           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2059             return arg1;
2060         }
2061
2062       /* Handle general case of two integer constants.  */
2063       return int_const_binop (code, arg0, arg1, 0);
2064     }
2065
2066   return fold_build2 (code, type, arg0, arg1);
2067 }
2068
2069 /* Given two values, either both of sizetype or both of bitsizetype,
2070    compute the difference between the two values.  Return the value
2071    in signed type corresponding to the type of the operands.  */
2072
2073 tree
2074 size_diffop (tree arg0, tree arg1)
2075 {
2076   tree type = TREE_TYPE (arg0);
2077   tree ctype;
2078
2079   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2080                                        TREE_TYPE (arg1)));
2081
2082   /* If the type is already signed, just do the simple thing.  */
2083   if (!TYPE_UNSIGNED (type))
2084     return size_binop (MINUS_EXPR, arg0, arg1);
2085
2086   if (type == sizetype)
2087     ctype = ssizetype;
2088   else if (type == bitsizetype)
2089     ctype = sbitsizetype;
2090   else
2091     ctype = signed_type_for (type);
2092
2093   /* If either operand is not a constant, do the conversions to the signed
2094      type and subtract.  The hardware will do the right thing with any
2095      overflow in the subtraction.  */
2096   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2097     return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2098                        fold_convert (ctype, arg1));
2099
2100   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2101      Otherwise, subtract the other way, convert to CTYPE (we know that can't
2102      overflow) and negate (which can't either).  Special-case a result
2103      of zero while we're here.  */
2104   if (tree_int_cst_equal (arg0, arg1))
2105     return build_int_cst (ctype, 0);
2106   else if (tree_int_cst_lt (arg1, arg0))
2107     return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2108   else
2109     return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2110                        fold_convert (ctype, size_binop (MINUS_EXPR,
2111                                                         arg1, arg0)));
2112 }
2113 \f
2114 /* A subroutine of fold_convert_const handling conversions of an
2115    INTEGER_CST to another integer type.  */
2116
2117 static tree
2118 fold_convert_const_int_from_int (tree type, const_tree arg1)
2119 {
2120   tree t;
2121
2122   /* Given an integer constant, make new constant with new type,
2123      appropriately sign-extended or truncated.  */
2124   t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2125                              TREE_INT_CST_HIGH (arg1),
2126                              /* Don't set the overflow when
2127                                 converting from a pointer,  */
2128                              !POINTER_TYPE_P (TREE_TYPE (arg1))
2129                              /* or to a sizetype with same signedness
2130                                 and the precision is unchanged.
2131                                 ???  sizetype is always sign-extended,
2132                                 but its signedness depends on the
2133                                 frontend.  Thus we see spurious overflows
2134                                 here if we do not check this.  */
2135                              && !((TYPE_PRECISION (TREE_TYPE (arg1))
2136                                    == TYPE_PRECISION (type))
2137                                   && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2138                                       == TYPE_UNSIGNED (type))
2139                                   && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2140                                        && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2141                                       || (TREE_CODE (type) == INTEGER_TYPE
2142                                           && TYPE_IS_SIZETYPE (type)))),
2143                              (TREE_INT_CST_HIGH (arg1) < 0
2144                               && (TYPE_UNSIGNED (type)
2145                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2146                              | TREE_OVERFLOW (arg1));
2147
2148   return t;
2149 }
2150
2151 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2152    to an integer type.  */
2153
2154 static tree
2155 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2156 {
2157   int overflow = 0;
2158   tree t;
2159
2160   /* The following code implements the floating point to integer
2161      conversion rules required by the Java Language Specification,
2162      that IEEE NaNs are mapped to zero and values that overflow
2163      the target precision saturate, i.e. values greater than
2164      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2165      are mapped to INT_MIN.  These semantics are allowed by the
2166      C and C++ standards that simply state that the behavior of
2167      FP-to-integer conversion is unspecified upon overflow.  */
2168
2169   HOST_WIDE_INT high, low;
2170   REAL_VALUE_TYPE r;
2171   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2172
2173   switch (code)
2174     {
2175     case FIX_TRUNC_EXPR:
2176       real_trunc (&r, VOIDmode, &x);
2177       break;
2178
2179     default:
2180       gcc_unreachable ();
2181     }
2182
2183   /* If R is NaN, return zero and show we have an overflow.  */
2184   if (REAL_VALUE_ISNAN (r))
2185     {
2186       overflow = 1;
2187       high = 0;
2188       low = 0;
2189     }
2190
2191   /* See if R is less than the lower bound or greater than the
2192      upper bound.  */
2193
2194   if (! overflow)
2195     {
2196       tree lt = TYPE_MIN_VALUE (type);
2197       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2198       if (REAL_VALUES_LESS (r, l))
2199         {
2200           overflow = 1;
2201           high = TREE_INT_CST_HIGH (lt);
2202           low = TREE_INT_CST_LOW (lt);
2203         }
2204     }
2205
2206   if (! overflow)
2207     {
2208       tree ut = TYPE_MAX_VALUE (type);
2209       if (ut)
2210         {
2211           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2212           if (REAL_VALUES_LESS (u, r))
2213             {
2214               overflow = 1;
2215               high = TREE_INT_CST_HIGH (ut);
2216               low = TREE_INT_CST_LOW (ut);
2217             }
2218         }
2219     }
2220
2221   if (! overflow)
2222     REAL_VALUE_TO_INT (&low, &high, r);
2223
2224   t = force_fit_type_double (type, low, high, -1,
2225                              overflow | TREE_OVERFLOW (arg1));
2226   return t;
2227 }
2228
2229 /* A subroutine of fold_convert_const handling conversions of a
2230    FIXED_CST to an integer type.  */
2231
2232 static tree
2233 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2234 {
2235   tree t;
2236   double_int temp, temp_trunc;
2237   unsigned int mode;
2238
2239   /* Right shift FIXED_CST to temp by fbit.  */
2240   temp = TREE_FIXED_CST (arg1).data;
2241   mode = TREE_FIXED_CST (arg1).mode;
2242   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2243     {
2244       lshift_double (temp.low, temp.high,
2245                      - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2246                      &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2247
2248       /* Left shift temp to temp_trunc by fbit.  */
2249       lshift_double (temp.low, temp.high,
2250                      GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2251                      &temp_trunc.low, &temp_trunc.high,
2252                      SIGNED_FIXED_POINT_MODE_P (mode));
2253     }
2254   else
2255     {
2256       temp.low = 0;
2257       temp.high = 0;
2258       temp_trunc.low = 0;
2259       temp_trunc.high = 0;
2260     }
2261
2262   /* If FIXED_CST is negative, we need to round the value toward 0.
2263      By checking if the fractional bits are not zero to add 1 to temp.  */
2264   if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2265       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2266     {
2267       double_int one;
2268       one.low = 1;
2269       one.high = 0;
2270       temp = double_int_add (temp, one);
2271     }
2272
2273   /* Given a fixed-point constant, make new constant with new type,
2274      appropriately sign-extended or truncated.  */
2275   t = force_fit_type_double (type, temp.low, temp.high, -1,
2276                              (temp.high < 0
2277                               && (TYPE_UNSIGNED (type)
2278                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2279                              | TREE_OVERFLOW (arg1));
2280
2281   return t;
2282 }
2283
2284 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2285    to another floating point type.  */
2286
2287 static tree
2288 fold_convert_const_real_from_real (tree type, const_tree arg1)
2289 {
2290   REAL_VALUE_TYPE value;
2291   tree t;
2292
2293   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2294   t = build_real (type, value);
2295
2296   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2297   return t;
2298 }
2299
2300 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2301    to a floating point type.  */
2302
2303 static tree
2304 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2305 {
2306   REAL_VALUE_TYPE value;
2307   tree t;
2308
2309   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2310   t = build_real (type, value);
2311
2312   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2313   TREE_CONSTANT_OVERFLOW (t)
2314     = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2315   return t;
2316 }
2317
2318 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2319    to another fixed-point type.  */
2320
2321 static tree
2322 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2323 {
2324   FIXED_VALUE_TYPE value;
2325   tree t;
2326   bool overflow_p;
2327
2328   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2329                               TYPE_SATURATING (type));
2330   t = build_fixed (type, value);
2331
2332   /* Propagate overflow flags.  */
2333   if (overflow_p | TREE_OVERFLOW (arg1))
2334     {
2335       TREE_OVERFLOW (t) = 1;
2336       TREE_CONSTANT_OVERFLOW (t) = 1;
2337     }
2338   else if (TREE_CONSTANT_OVERFLOW (arg1))
2339     TREE_CONSTANT_OVERFLOW (t) = 1;
2340   return t;
2341 }
2342
2343 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2344    to a fixed-point type.  */
2345
2346 static tree
2347 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2348 {
2349   FIXED_VALUE_TYPE value;
2350   tree t;
2351   bool overflow_p;
2352
2353   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2354                                        TREE_INT_CST (arg1),
2355                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
2356                                        TYPE_SATURATING (type));
2357   t = build_fixed (type, value);
2358
2359   /* Propagate overflow flags.  */
2360   if (overflow_p | TREE_OVERFLOW (arg1))
2361     {
2362       TREE_OVERFLOW (t) = 1;
2363       TREE_CONSTANT_OVERFLOW (t) = 1;
2364     }
2365   else if (TREE_CONSTANT_OVERFLOW (arg1))
2366     TREE_CONSTANT_OVERFLOW (t) = 1;
2367   return t;
2368 }
2369
2370 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2371    to a fixed-point type.  */
2372
2373 static tree
2374 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2375 {
2376   FIXED_VALUE_TYPE value;
2377   tree t;
2378   bool overflow_p;
2379
2380   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2381                                         &TREE_REAL_CST (arg1),
2382                                         TYPE_SATURATING (type));
2383   t = build_fixed (type, value);
2384
2385   /* Propagate overflow flags.  */
2386   if (overflow_p | TREE_OVERFLOW (arg1))
2387     {
2388       TREE_OVERFLOW (t) = 1;
2389       TREE_CONSTANT_OVERFLOW (t) = 1;
2390     }
2391   else if (TREE_CONSTANT_OVERFLOW (arg1))
2392     TREE_CONSTANT_OVERFLOW (t) = 1;
2393   return t;
2394 }
2395
2396 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2397    type TYPE.  If no simplification can be done return NULL_TREE.  */
2398
2399 static tree
2400 fold_convert_const (enum tree_code code, tree type, tree arg1)
2401 {
2402   if (TREE_TYPE (arg1) == type)
2403     return arg1;
2404
2405   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2406       || TREE_CODE (type) == OFFSET_TYPE)
2407     {
2408       if (TREE_CODE (arg1) == INTEGER_CST)
2409         return fold_convert_const_int_from_int (type, arg1);
2410       else if (TREE_CODE (arg1) == REAL_CST)
2411         return fold_convert_const_int_from_real (code, type, arg1);
2412       else if (TREE_CODE (arg1) == FIXED_CST)
2413         return fold_convert_const_int_from_fixed (type, arg1);
2414     }
2415   else if (TREE_CODE (type) == REAL_TYPE)
2416     {
2417       if (TREE_CODE (arg1) == INTEGER_CST)
2418         return build_real_from_int_cst (type, arg1);
2419       else if (TREE_CODE (arg1) == REAL_CST)
2420         return fold_convert_const_real_from_real (type, arg1);
2421       else if (TREE_CODE (arg1) == FIXED_CST)
2422         return fold_convert_const_real_from_fixed (type, arg1);
2423     }
2424   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2425     {
2426       if (TREE_CODE (arg1) == FIXED_CST)
2427         return fold_convert_const_fixed_from_fixed (type, arg1);
2428       else if (TREE_CODE (arg1) == INTEGER_CST)
2429         return fold_convert_const_fixed_from_int (type, arg1);
2430       else if (TREE_CODE (arg1) == REAL_CST)
2431         return fold_convert_const_fixed_from_real (type, arg1);
2432     }
2433   return NULL_TREE;
2434 }
2435
2436 /* Construct a vector of zero elements of vector type TYPE.  */
2437
2438 static tree
2439 build_zero_vector (tree type)
2440 {
2441   tree elem, list;
2442   int i, units;
2443
2444   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2445   units = TYPE_VECTOR_SUBPARTS (type);
2446   
2447   list = NULL_TREE;
2448   for (i = 0; i < units; i++)
2449     list = tree_cons (NULL_TREE, elem, list);
2450   return build_vector (type, list);
2451 }
2452
2453 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
2454
2455 bool
2456 fold_convertible_p (const_tree type, const_tree arg)
2457 {
2458   tree orig = TREE_TYPE (arg);
2459
2460   if (type == orig)
2461     return true;
2462
2463   if (TREE_CODE (arg) == ERROR_MARK
2464       || TREE_CODE (type) == ERROR_MARK
2465       || TREE_CODE (orig) == ERROR_MARK)
2466     return false;
2467
2468   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2469     return true;
2470
2471   switch (TREE_CODE (type))
2472     {
2473     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2474     case POINTER_TYPE: case REFERENCE_TYPE:
2475     case OFFSET_TYPE:
2476       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2477           || TREE_CODE (orig) == OFFSET_TYPE)
2478         return true;
2479       return (TREE_CODE (orig) == VECTOR_TYPE
2480               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2481
2482     case REAL_TYPE:
2483     case FIXED_POINT_TYPE:
2484     case COMPLEX_TYPE:
2485     case VECTOR_TYPE:
2486     case VOID_TYPE:
2487       return TREE_CODE (type) == TREE_CODE (orig);
2488
2489     default:
2490       return false;
2491     }
2492 }
2493
2494 /* Convert expression ARG to type TYPE.  Used by the middle-end for
2495    simple conversions in preference to calling the front-end's convert.  */
2496
2497 tree
2498 fold_convert (tree type, tree arg)
2499 {
2500   tree orig = TREE_TYPE (arg);
2501   tree tem;
2502
2503   if (type == orig)
2504     return arg;
2505
2506   if (TREE_CODE (arg) == ERROR_MARK
2507       || TREE_CODE (type) == ERROR_MARK
2508       || TREE_CODE (orig) == ERROR_MARK)
2509     return error_mark_node;
2510
2511   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2512     return fold_build1 (NOP_EXPR, type, arg);
2513
2514   switch (TREE_CODE (type))
2515     {
2516     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2517     case POINTER_TYPE: case REFERENCE_TYPE:
2518     case OFFSET_TYPE:
2519       if (TREE_CODE (arg) == INTEGER_CST)
2520         {
2521           tem = fold_convert_const (NOP_EXPR, type, arg);
2522           if (tem != NULL_TREE)
2523             return tem;
2524         }
2525       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2526           || TREE_CODE (orig) == OFFSET_TYPE)
2527         return fold_build1 (NOP_EXPR, type, arg);
2528       if (TREE_CODE (orig) == COMPLEX_TYPE)
2529         {
2530           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2531           return fold_convert (type, tem);
2532         }
2533       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2534                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2535       return fold_build1 (NOP_EXPR, type, arg);
2536
2537     case REAL_TYPE:
2538       if (TREE_CODE (arg) == INTEGER_CST)
2539         {
2540           tem = fold_convert_const (FLOAT_EXPR, type, arg);
2541           if (tem != NULL_TREE)
2542             return tem;
2543         }
2544       else if (TREE_CODE (arg) == REAL_CST)
2545         {
2546           tem = fold_convert_const (NOP_EXPR, type, arg);
2547           if (tem != NULL_TREE)
2548             return tem;
2549         }
2550       else if (TREE_CODE (arg) == FIXED_CST)
2551         {
2552           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2553           if (tem != NULL_TREE)
2554             return tem;
2555         }
2556
2557       switch (TREE_CODE (orig))
2558         {
2559         case INTEGER_TYPE:
2560         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2561         case POINTER_TYPE: case REFERENCE_TYPE:
2562           return fold_build1 (FLOAT_EXPR, type, arg);
2563
2564         case REAL_TYPE:
2565           return fold_build1 (NOP_EXPR, type, arg);
2566
2567         case FIXED_POINT_TYPE:
2568           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2569
2570         case COMPLEX_TYPE:
2571           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2572           return fold_convert (type, tem);
2573
2574         default:
2575           gcc_unreachable ();
2576         }
2577
2578     case FIXED_POINT_TYPE:
2579       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2580           || TREE_CODE (arg) == REAL_CST)
2581         {
2582           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2583           if (tem != NULL_TREE)
2584             return tem;
2585         }
2586
2587       switch (TREE_CODE (orig))
2588         {
2589         case FIXED_POINT_TYPE:
2590         case INTEGER_TYPE:
2591         case ENUMERAL_TYPE:
2592         case BOOLEAN_TYPE:
2593         case REAL_TYPE:
2594           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2595
2596         case COMPLEX_TYPE:
2597           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2598           return fold_convert (type, tem);
2599
2600         default:
2601           gcc_unreachable ();
2602         }
2603
2604     case COMPLEX_TYPE:
2605       switch (TREE_CODE (orig))
2606         {
2607         case INTEGER_TYPE:
2608         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2609         case POINTER_TYPE: case REFERENCE_TYPE:
2610         case REAL_TYPE:
2611         case FIXED_POINT_TYPE:
2612           return fold_build2 (COMPLEX_EXPR, type,
2613                               fold_convert (TREE_TYPE (type), arg),
2614                               fold_convert (TREE_TYPE (type),
2615                                             integer_zero_node));
2616         case COMPLEX_TYPE:
2617           {
2618             tree rpart, ipart;
2619
2620             if (TREE_CODE (arg) == COMPLEX_EXPR)
2621               {
2622                 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2623                 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2624                 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2625               }
2626
2627             arg = save_expr (arg);
2628             rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2629             ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2630             rpart = fold_convert (TREE_TYPE (type), rpart);
2631             ipart = fold_convert (TREE_TYPE (type), ipart);
2632             return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2633           }
2634
2635         default:
2636           gcc_unreachable ();
2637         }
2638
2639     case VECTOR_TYPE:
2640       if (integer_zerop (arg))
2641         return build_zero_vector (type);
2642       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2643       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2644                   || TREE_CODE (orig) == VECTOR_TYPE);
2645       return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2646
2647     case VOID_TYPE:
2648       tem = fold_ignored_result (arg);
2649       if (TREE_CODE (tem) == MODIFY_EXPR)
2650         return tem;
2651       return fold_build1 (NOP_EXPR, type, tem);
2652
2653     default:
2654       gcc_unreachable ();
2655     }
2656 }
2657 \f
2658 /* Return false if expr can be assumed not to be an lvalue, true
2659    otherwise.  */
2660
2661 static bool
2662 maybe_lvalue_p (const_tree x)
2663 {
2664   /* We only need to wrap lvalue tree codes.  */
2665   switch (TREE_CODE (x))
2666   {
2667   case VAR_DECL:
2668   case PARM_DECL:
2669   case RESULT_DECL:
2670   case LABEL_DECL:
2671   case FUNCTION_DECL:
2672   case SSA_NAME:
2673
2674   case COMPONENT_REF:
2675   case INDIRECT_REF:
2676   case ALIGN_INDIRECT_REF:
2677   case MISALIGNED_INDIRECT_REF:
2678   case ARRAY_REF:
2679   case ARRAY_RANGE_REF:
2680   case BIT_FIELD_REF:
2681   case OBJ_TYPE_REF:
2682
2683   case REALPART_EXPR:
2684   case IMAGPART_EXPR:
2685   case PREINCREMENT_EXPR:
2686   case PREDECREMENT_EXPR:
2687   case SAVE_EXPR:
2688   case TRY_CATCH_EXPR:
2689   case WITH_CLEANUP_EXPR:
2690   case COMPOUND_EXPR:
2691   case MODIFY_EXPR:
2692   case TARGET_EXPR:
2693   case COND_EXPR:
2694   case BIND_EXPR:
2695   case MIN_EXPR:
2696   case MAX_EXPR:
2697     break;
2698
2699   default:
2700     /* Assume the worst for front-end tree codes.  */
2701     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2702       break;
2703     return false;
2704   }
2705
2706   return true;
2707 }
2708
2709 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2710
2711 tree
2712 non_lvalue (tree x)
2713 {
2714   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2715      us.  */
2716   if (in_gimple_form)
2717     return x;
2718
2719   if (! maybe_lvalue_p (x))
2720     return x;
2721   return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2722 }
2723
2724 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2725    Zero means allow extended lvalues.  */
2726
2727 int pedantic_lvalues;
2728
2729 /* When pedantic, return an expr equal to X but certainly not valid as a
2730    pedantic lvalue.  Otherwise, return X.  */
2731
2732 static tree
2733 pedantic_non_lvalue (tree x)
2734 {
2735   if (pedantic_lvalues)
2736     return non_lvalue (x);
2737   else
2738     return x;
2739 }
2740 \f
2741 /* Given a tree comparison code, return the code that is the logical inverse
2742    of the given code.  It is not safe to do this for floating-point
2743    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2744    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2745
2746 enum tree_code
2747 invert_tree_comparison (enum tree_code code, bool honor_nans)
2748 {
2749   if (honor_nans && flag_trapping_math)
2750     return ERROR_MARK;
2751
2752   switch (code)
2753     {
2754     case EQ_EXPR:
2755       return NE_EXPR;
2756     case NE_EXPR:
2757       return EQ_EXPR;
2758     case GT_EXPR:
2759       return honor_nans ? UNLE_EXPR : LE_EXPR;
2760     case GE_EXPR:
2761       return honor_nans ? UNLT_EXPR : LT_EXPR;
2762     case LT_EXPR:
2763       return honor_nans ? UNGE_EXPR : GE_EXPR;
2764     case LE_EXPR:
2765       return honor_nans ? UNGT_EXPR : GT_EXPR;
2766     case LTGT_EXPR:
2767       return UNEQ_EXPR;
2768     case UNEQ_EXPR:
2769       return LTGT_EXPR;
2770     case UNGT_EXPR:
2771       return LE_EXPR;
2772     case UNGE_EXPR:
2773       return LT_EXPR;
2774     case UNLT_EXPR:
2775       return GE_EXPR;
2776     case UNLE_EXPR:
2777       return GT_EXPR;
2778     case ORDERED_EXPR:
2779       return UNORDERED_EXPR;
2780     case UNORDERED_EXPR:
2781       return ORDERED_EXPR;
2782     default:
2783       gcc_unreachable ();
2784     }
2785 }
2786
2787 /* Similar, but return the comparison that results if the operands are
2788    swapped.  This is safe for floating-point.  */
2789
2790 enum tree_code
2791 swap_tree_comparison (enum tree_code code)
2792 {
2793   switch (code)
2794     {
2795     case EQ_EXPR:
2796     case NE_EXPR:
2797     case ORDERED_EXPR:
2798     case UNORDERED_EXPR:
2799     case LTGT_EXPR:
2800     case UNEQ_EXPR:
2801       return code;
2802     case GT_EXPR:
2803       return LT_EXPR;
2804     case GE_EXPR:
2805       return LE_EXPR;
2806     case LT_EXPR:
2807       return GT_EXPR;
2808     case LE_EXPR:
2809       return GE_EXPR;
2810     case UNGT_EXPR:
2811       return UNLT_EXPR;
2812     case UNGE_EXPR:
2813       return UNLE_EXPR;
2814     case UNLT_EXPR:
2815       return UNGT_EXPR;
2816     case UNLE_EXPR:
2817       return UNGE_EXPR;
2818     default:
2819       gcc_unreachable ();
2820     }
2821 }
2822
2823
2824 /* Convert a comparison tree code from an enum tree_code representation
2825    into a compcode bit-based encoding.  This function is the inverse of
2826    compcode_to_comparison.  */
2827
2828 static enum comparison_code
2829 comparison_to_compcode (enum tree_code code)
2830 {
2831   switch (code)
2832     {
2833     case LT_EXPR:
2834       return COMPCODE_LT;
2835     case EQ_EXPR:
2836       return COMPCODE_EQ;
2837     case LE_EXPR:
2838       return COMPCODE_LE;
2839     case GT_EXPR:
2840       return COMPCODE_GT;
2841     case NE_EXPR:
2842       return COMPCODE_NE;
2843     case GE_EXPR:
2844       return COMPCODE_GE;
2845     case ORDERED_EXPR:
2846       return COMPCODE_ORD;
2847     case UNORDERED_EXPR:
2848       return COMPCODE_UNORD;
2849     case UNLT_EXPR:
2850       return COMPCODE_UNLT;
2851     case UNEQ_EXPR:
2852       return COMPCODE_UNEQ;
2853     case UNLE_EXPR:
2854       return COMPCODE_UNLE;
2855     case UNGT_EXPR:
2856       return COMPCODE_UNGT;
2857     case LTGT_EXPR:
2858       return COMPCODE_LTGT;
2859     case UNGE_EXPR:
2860       return COMPCODE_UNGE;
2861     default:
2862       gcc_unreachable ();
2863     }
2864 }
2865
2866 /* Convert a compcode bit-based encoding of a comparison operator back
2867    to GCC's enum tree_code representation.  This function is the
2868    inverse of comparison_to_compcode.  */
2869
2870 static enum tree_code
2871 compcode_to_comparison (enum comparison_code code)
2872 {
2873   switch (code)
2874     {
2875     case COMPCODE_LT:
2876       return LT_EXPR;
2877     case COMPCODE_EQ:
2878       return EQ_EXPR;
2879     case COMPCODE_LE:
2880       return LE_EXPR;
2881     case COMPCODE_GT:
2882       return GT_EXPR;
2883     case COMPCODE_NE:
2884       return NE_EXPR;
2885     case COMPCODE_GE:
2886       return GE_EXPR;
2887     case COMPCODE_ORD:
2888       return ORDERED_EXPR;
2889     case COMPCODE_UNORD:
2890       return UNORDERED_EXPR;
2891     case COMPCODE_UNLT:
2892       return UNLT_EXPR;
2893     case COMPCODE_UNEQ:
2894       return UNEQ_EXPR;
2895     case COMPCODE_UNLE:
2896       return UNLE_EXPR;
2897     case COMPCODE_UNGT:
2898       return UNGT_EXPR;
2899     case COMPCODE_LTGT:
2900       return LTGT_EXPR;
2901     case COMPCODE_UNGE:
2902       return UNGE_EXPR;
2903     default:
2904       gcc_unreachable ();
2905     }
2906 }
2907
2908 /* Return a tree for the comparison which is the combination of
2909    doing the AND or OR (depending on CODE) of the two operations LCODE
2910    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2911    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2912    if this makes the transformation invalid.  */
2913
2914 tree
2915 combine_comparisons (enum tree_code code, enum tree_code lcode,
2916                      enum tree_code rcode, tree truth_type,
2917                      tree ll_arg, tree lr_arg)
2918 {
2919   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2920   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2921   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2922   enum comparison_code compcode;
2923
2924   switch (code)
2925     {
2926     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2927       compcode = lcompcode & rcompcode;
2928       break;
2929
2930     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2931       compcode = lcompcode | rcompcode;
2932       break;
2933
2934     default:
2935       return NULL_TREE;
2936     }
2937
2938   if (!honor_nans)
2939     {
2940       /* Eliminate unordered comparisons, as well as LTGT and ORD
2941          which are not used unless the mode has NaNs.  */
2942       compcode &= ~COMPCODE_UNORD;
2943       if (compcode == COMPCODE_LTGT)
2944         compcode = COMPCODE_NE;
2945       else if (compcode == COMPCODE_ORD)
2946         compcode = COMPCODE_TRUE;
2947     }
2948    else if (flag_trapping_math)
2949      {
2950         /* Check that the original operation and the optimized ones will trap
2951            under the same condition.  */
2952         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2953                      && (lcompcode != COMPCODE_EQ)
2954                      && (lcompcode != COMPCODE_ORD);
2955         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2956                      && (rcompcode != COMPCODE_EQ)
2957                      && (rcompcode != COMPCODE_ORD);
2958         bool trap = (compcode & COMPCODE_UNORD) == 0
2959                     && (compcode != COMPCODE_EQ)
2960                     && (compcode != COMPCODE_ORD);
2961
2962         /* In a short-circuited boolean expression the LHS might be
2963            such that the RHS, if evaluated, will never trap.  For
2964            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2965            if neither x nor y is NaN.  (This is a mixed blessing: for
2966            example, the expression above will never trap, hence
2967            optimizing it to x < y would be invalid).  */
2968         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2969             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2970           rtrap = false;
2971
2972         /* If the comparison was short-circuited, and only the RHS
2973            trapped, we may now generate a spurious trap.  */
2974         if (rtrap && !ltrap
2975             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2976           return NULL_TREE;
2977
2978         /* If we changed the conditions that cause a trap, we lose.  */
2979         if ((ltrap || rtrap) != trap)
2980           return NULL_TREE;
2981       }
2982
2983   if (compcode == COMPCODE_TRUE)
2984     return constant_boolean_node (true, truth_type);
2985   else if (compcode == COMPCODE_FALSE)
2986     return constant_boolean_node (false, truth_type);
2987   else
2988     return fold_build2 (compcode_to_comparison (compcode),
2989                         truth_type, ll_arg, lr_arg);
2990 }
2991 \f
2992 /* Return nonzero if two operands (typically of the same tree node)
2993    are necessarily equal.  If either argument has side-effects this
2994    function returns zero.  FLAGS modifies behavior as follows:
2995
2996    If OEP_ONLY_CONST is set, only return nonzero for constants.
2997    This function tests whether the operands are indistinguishable;
2998    it does not test whether they are equal using C's == operation.
2999    The distinction is important for IEEE floating point, because
3000    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3001    (2) two NaNs may be indistinguishable, but NaN!=NaN.
3002
3003    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3004    even though it may hold multiple values during a function.
3005    This is because a GCC tree node guarantees that nothing else is
3006    executed between the evaluation of its "operands" (which may often
3007    be evaluated in arbitrary order).  Hence if the operands themselves
3008    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3009    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
3010    unset means assuming isochronic (or instantaneous) tree equivalence.
3011    Unless comparing arbitrary expression trees, such as from different
3012    statements, this flag can usually be left unset.
3013
3014    If OEP_PURE_SAME is set, then pure functions with identical arguments
3015    are considered the same.  It is used when the caller has other ways
3016    to ensure that global memory is unchanged in between.  */
3017
3018 int
3019 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3020 {
3021   /* If either is ERROR_MARK, they aren't equal.  */
3022   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3023     return 0;
3024
3025   /* Check equality of integer constants before bailing out due to
3026      precision differences.  */
3027   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3028     return tree_int_cst_equal (arg0, arg1);
3029
3030   /* If both types don't have the same signedness, then we can't consider
3031      them equal.  We must check this before the STRIP_NOPS calls
3032      because they may change the signedness of the arguments.  As pointers
3033      strictly don't have a signedness, require either two pointers or
3034      two non-pointers as well.  */
3035   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3036       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
3037     return 0;
3038
3039   /* If both types don't have the same precision, then it is not safe
3040      to strip NOPs.  */
3041   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3042     return 0;
3043
3044   STRIP_NOPS (arg0);
3045   STRIP_NOPS (arg1);
3046
3047   /* In case both args are comparisons but with different comparison
3048      code, try to swap the comparison operands of one arg to produce
3049      a match and compare that variant.  */
3050   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3051       && COMPARISON_CLASS_P (arg0)
3052       && COMPARISON_CLASS_P (arg1))
3053     {
3054       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3055
3056       if (TREE_CODE (arg0) == swap_code)
3057         return operand_equal_p (TREE_OPERAND (arg0, 0),
3058                                 TREE_OPERAND (arg1, 1), flags)
3059                && operand_equal_p (TREE_OPERAND (arg0, 1),
3060                                    TREE_OPERAND (arg1, 0), flags);
3061     }
3062
3063   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3064       /* This is needed for conversions and for COMPONENT_REF.
3065          Might as well play it safe and always test this.  */
3066       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3067       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3068       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3069     return 0;
3070
3071   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3072      We don't care about side effects in that case because the SAVE_EXPR
3073      takes care of that for us. In all other cases, two expressions are
3074      equal if they have no side effects.  If we have two identical
3075      expressions with side effects that should be treated the same due
3076      to the only side effects being identical SAVE_EXPR's, that will
3077      be detected in the recursive calls below.  */
3078   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3079       && (TREE_CODE (arg0) == SAVE_EXPR
3080           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3081     return 1;
3082
3083   /* Next handle constant cases, those for which we can return 1 even
3084      if ONLY_CONST is set.  */
3085   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3086     switch (TREE_CODE (arg0))
3087       {
3088       case INTEGER_CST:
3089         return tree_int_cst_equal (arg0, arg1);
3090
3091       case FIXED_CST:
3092         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3093                                        TREE_FIXED_CST (arg1));
3094
3095       case REAL_CST:
3096         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3097                                    TREE_REAL_CST (arg1)))
3098           return 1;
3099
3100         
3101         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3102           {
3103             /* If we do not distinguish between signed and unsigned zero,
3104                consider them equal.  */
3105             if (real_zerop (arg0) && real_zerop (arg1))
3106               return 1;
3107           }
3108         return 0;
3109
3110       case VECTOR_CST:
3111         {
3112           tree v1, v2;
3113
3114           v1 = TREE_VECTOR_CST_ELTS (arg0);
3115           v2 = TREE_VECTOR_CST_ELTS (arg1);
3116           while (v1 && v2)
3117             {
3118               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3119                                     flags))
3120                 return 0;
3121               v1 = TREE_CHAIN (v1);
3122               v2 = TREE_CHAIN (v2);
3123             }
3124
3125           return v1 == v2;
3126         }
3127
3128       case COMPLEX_CST:
3129         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3130                                  flags)
3131                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3132                                     flags));
3133
3134       case STRING_CST:
3135         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3136                 && ! memcmp (TREE_STRING_POINTER (arg0),
3137                               TREE_STRING_POINTER (arg1),
3138                               TREE_STRING_LENGTH (arg0)));
3139
3140       case ADDR_EXPR:
3141         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3142                                 0);
3143       default:
3144         break;
3145       }
3146
3147   if (flags & OEP_ONLY_CONST)
3148     return 0;
3149
3150 /* Define macros to test an operand from arg0 and arg1 for equality and a
3151    variant that allows null and views null as being different from any
3152    non-null value.  In the latter case, if either is null, the both
3153    must be; otherwise, do the normal comparison.  */
3154 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
3155                                     TREE_OPERAND (arg1, N), flags)
3156
3157 #define OP_SAME_WITH_NULL(N)                            \
3158   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3159    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3160
3161   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3162     {
3163     case tcc_unary:
3164       /* Two conversions are equal only if signedness and modes match.  */
3165       switch (TREE_CODE (arg0))
3166         {
3167         CASE_CONVERT:
3168         case FIX_TRUNC_EXPR:
3169           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3170               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3171             return 0;
3172           break;
3173         default:
3174           break;
3175         }
3176
3177       return OP_SAME (0);
3178
3179
3180     case tcc_comparison:
3181     case tcc_binary:
3182       if (OP_SAME (0) && OP_SAME (1))
3183         return 1;
3184
3185       /* For commutative ops, allow the other order.  */
3186       return (commutative_tree_code (TREE_CODE (arg0))
3187               && operand_equal_p (TREE_OPERAND (arg0, 0),
3188                                   TREE_OPERAND (arg1, 1), flags)
3189               && operand_equal_p (TREE_OPERAND (arg0, 1),
3190                                   TREE_OPERAND (arg1, 0), flags));
3191
3192     case tcc_reference:
3193       /* If either of the pointer (or reference) expressions we are
3194          dereferencing contain a side effect, these cannot be equal.  */
3195       if (TREE_SIDE_EFFECTS (arg0)
3196           || TREE_SIDE_EFFECTS (arg1))
3197         return 0;
3198
3199       switch (TREE_CODE (arg0))
3200         {
3201         case INDIRECT_REF:
3202         case ALIGN_INDIRECT_REF:
3203         case MISALIGNED_INDIRECT_REF:
3204         case REALPART_EXPR:
3205         case IMAGPART_EXPR:
3206           return OP_SAME (0);
3207
3208         case ARRAY_REF:
3209         case ARRAY_RANGE_REF:
3210           /* Operands 2 and 3 may be null.
3211              Compare the array index by value if it is constant first as we
3212              may have different types but same value here.  */
3213           return (OP_SAME (0)
3214                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3215                                           TREE_OPERAND (arg1, 1))
3216                       || OP_SAME (1))
3217                   && OP_SAME_WITH_NULL (2)
3218                   && OP_SAME_WITH_NULL (3));
3219
3220         case COMPONENT_REF:
3221           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
3222              may be NULL when we're called to compare MEM_EXPRs.  */
3223           return OP_SAME_WITH_NULL (0)
3224                  && OP_SAME (1)
3225                  && OP_SAME_WITH_NULL (2);
3226
3227         case BIT_FIELD_REF:
3228           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3229
3230         default:
3231           return 0;
3232         }
3233
3234     case tcc_expression:
3235       switch (TREE_CODE (arg0))
3236         {
3237         case ADDR_EXPR:
3238         case TRUTH_NOT_EXPR:
3239           return OP_SAME (0);
3240
3241         case TRUTH_ANDIF_EXPR:
3242         case TRUTH_ORIF_EXPR:
3243           return OP_SAME (0) && OP_SAME (1);
3244
3245         case TRUTH_AND_EXPR:
3246         case TRUTH_OR_EXPR:
3247         case TRUTH_XOR_EXPR:
3248           if (OP_SAME (0) && OP_SAME (1))
3249             return 1;
3250
3251           /* Otherwise take into account this is a commutative operation.  */
3252           return (operand_equal_p (TREE_OPERAND (arg0, 0),
3253                                    TREE_OPERAND (arg1, 1), flags)
3254                   && operand_equal_p (TREE_OPERAND (arg0, 1),
3255                                       TREE_OPERAND (arg1, 0), flags));
3256
3257         case COND_EXPR:
3258           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3259           
3260         default:
3261           return 0;
3262         }
3263
3264     case tcc_vl_exp:
3265       switch (TREE_CODE (arg0))
3266         {
3267         case CALL_EXPR:
3268           /* If the CALL_EXPRs call different functions, then they
3269              clearly can not be equal.  */
3270           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3271                                  flags))
3272             return 0;
3273
3274           {
3275             unsigned int cef = call_expr_flags (arg0);
3276             if (flags & OEP_PURE_SAME)
3277               cef &= ECF_CONST | ECF_PURE;
3278             else
3279               cef &= ECF_CONST;
3280             if (!cef)
3281               return 0;
3282           }
3283
3284           /* Now see if all the arguments are the same.  */
3285           {
3286             const_call_expr_arg_iterator iter0, iter1;
3287             const_tree a0, a1;
3288             for (a0 = first_const_call_expr_arg (arg0, &iter0),
3289                    a1 = first_const_call_expr_arg (arg1, &iter1);
3290                  a0 && a1;
3291                  a0 = next_const_call_expr_arg (&iter0),
3292                    a1 = next_const_call_expr_arg (&iter1))
3293               if (! operand_equal_p (a0, a1, flags))
3294                 return 0;
3295
3296             /* If we get here and both argument lists are exhausted
3297                then the CALL_EXPRs are equal.  */
3298             return ! (a0 || a1);
3299           }
3300         default:
3301           return 0;
3302         }
3303
3304     case tcc_declaration:
3305       /* Consider __builtin_sqrt equal to sqrt.  */
3306       return (TREE_CODE (arg0) == FUNCTION_DECL
3307               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3308               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3309               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3310
3311     default:
3312       return 0;
3313     }
3314
3315 #undef OP_SAME
3316 #undef OP_SAME_WITH_NULL
3317 }
3318 \f
3319 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3320    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3321
3322    When in doubt, return 0.  */
3323
3324 static int
3325 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3326 {
3327   int unsignedp1, unsignedpo;
3328   tree primarg0, primarg1, primother;
3329   unsigned int correct_width;
3330
3331   if (operand_equal_p (arg0, arg1, 0))
3332     return 1;
3333
3334   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3335       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3336     return 0;
3337
3338   /* Discard any conversions that don't change the modes of ARG0 and ARG1
3339      and see if the inner values are the same.  This removes any
3340      signedness comparison, which doesn't matter here.  */
3341   primarg0 = arg0, primarg1 = arg1;
3342   STRIP_NOPS (primarg0);
3343   STRIP_NOPS (primarg1);
3344   if (operand_equal_p (primarg0, primarg1, 0))
3345     return 1;
3346
3347   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3348      actual comparison operand, ARG0.
3349
3350      First throw away any conversions to wider types
3351      already present in the operands.  */
3352
3353   primarg1 = get_narrower (arg1, &unsignedp1);
3354   primother = get_narrower (other, &unsignedpo);
3355
3356   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3357   if (unsignedp1 == unsignedpo
3358       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3359       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3360     {
3361       tree type = TREE_TYPE (arg0);
3362
3363       /* Make sure shorter operand is extended the right way
3364          to match the longer operand.  */
3365       primarg1 = fold_convert (signed_or_unsigned_type_for
3366                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3367
3368       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3369         return 1;
3370     }
3371
3372   return 0;
3373 }
3374 \f
3375 /* See if ARG is an expression that is either a comparison or is performing
3376    arithmetic on comparisons.  The comparisons must only be comparing
3377    two different values, which will be stored in *CVAL1 and *CVAL2; if
3378    they are nonzero it means that some operands have already been found.
3379    No variables may be used anywhere else in the expression except in the
3380    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
3381    the expression and save_expr needs to be called with CVAL1 and CVAL2.
3382
3383    If this is true, return 1.  Otherwise, return zero.  */
3384
3385 static int
3386 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3387 {
3388   enum tree_code code = TREE_CODE (arg);
3389   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3390
3391   /* We can handle some of the tcc_expression cases here.  */
3392   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3393     tclass = tcc_unary;
3394   else if (tclass == tcc_expression
3395            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3396                || code == COMPOUND_EXPR))
3397     tclass = tcc_binary;
3398
3399   else if (tclass == tcc_expression && code == SAVE_EXPR
3400            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3401     {
3402       /* If we've already found a CVAL1 or CVAL2, this expression is
3403          two complex to handle.  */
3404       if (*cval1 || *cval2)
3405         return 0;
3406
3407       tclass = tcc_unary;
3408       *save_p = 1;
3409     }
3410
3411   switch (tclass)
3412     {
3413     case tcc_unary:
3414       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3415
3416     case tcc_binary:
3417       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3418               && twoval_comparison_p (TREE_OPERAND (arg, 1),
3419                                       cval1, cval2, save_p));
3420
3421     case tcc_constant:
3422       return 1;
3423
3424     case tcc_expression:
3425       if (code == COND_EXPR)
3426         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3427                                      cval1, cval2, save_p)
3428                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3429                                         cval1, cval2, save_p)
3430                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3431                                         cval1, cval2, save_p));
3432       return 0;
3433
3434     case tcc_comparison:
3435       /* First see if we can handle the first operand, then the second.  For
3436          the second operand, we know *CVAL1 can't be zero.  It must be that
3437          one side of the comparison is each of the values; test for the
3438          case where this isn't true by failing if the two operands
3439          are the same.  */
3440
3441       if (operand_equal_p (TREE_OPERAND (arg, 0),
3442                            TREE_OPERAND (arg, 1), 0))
3443         return 0;
3444
3445       if (*cval1 == 0)
3446         *cval1 = TREE_OPERAND (arg, 0);
3447       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3448         ;
3449       else if (*cval2 == 0)
3450         *cval2 = TREE_OPERAND (arg, 0);
3451       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3452         ;
3453       else
3454         return 0;
3455
3456       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3457         ;
3458       else if (*cval2 == 0)
3459         *cval2 = TREE_OPERAND (arg, 1);
3460       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3461         ;
3462       else
3463         return 0;
3464
3465       return 1;
3466
3467     default:
3468       return 0;
3469     }
3470 }
3471 \f
3472 /* ARG is a tree that is known to contain just arithmetic operations and
3473    comparisons.  Evaluate the operations in the tree substituting NEW0 for
3474    any occurrence of OLD0 as an operand of a comparison and likewise for
3475    NEW1 and OLD1.  */
3476
3477 static tree
3478 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3479 {
3480   tree type = TREE_TYPE (arg);
3481   enum tree_code code = TREE_CODE (arg);
3482   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3483
3484   /* We can handle some of the tcc_expression cases here.  */
3485   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3486     tclass = tcc_unary;
3487   else if (tclass == tcc_expression
3488            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3489     tclass = tcc_binary;
3490
3491   switch (tclass)
3492     {
3493     case tcc_unary:
3494       return fold_build1 (code, type,
3495                           eval_subst (TREE_OPERAND (arg, 0),
3496                                       old0, new0, old1, new1));
3497
3498     case tcc_binary:
3499       return fold_build2 (code, type,
3500                           eval_subst (TREE_OPERAND (arg, 0),
3501                                       old0, new0, old1, new1),
3502                           eval_subst (TREE_OPERAND (arg, 1),
3503                                       old0, new0, old1, new1));
3504
3505     case tcc_expression:
3506       switch (code)
3507         {
3508         case SAVE_EXPR:
3509           return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3510
3511         case COMPOUND_EXPR:
3512           return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3513
3514         case COND_EXPR:
3515           return fold_build3 (code, type,
3516                               eval_subst (TREE_OPERAND (arg, 0),
3517                                           old0, new0, old1, new1),
3518                               eval_subst (TREE_OPERAND (arg, 1),
3519                                           old0, new0, old1, new1),
3520                               eval_subst (TREE_OPERAND (arg, 2),
3521                                           old0, new0, old1, new1));
3522         default:
3523           break;
3524         }
3525       /* Fall through - ???  */
3526
3527     case tcc_comparison:
3528       {
3529         tree arg0 = TREE_OPERAND (arg, 0);
3530         tree arg1 = TREE_OPERAND (arg, 1);
3531
3532         /* We need to check both for exact equality and tree equality.  The
3533            former will be true if the operand has a side-effect.  In that
3534            case, we know the operand occurred exactly once.  */
3535
3536         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3537           arg0 = new0;
3538         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3539           arg0 = new1;
3540
3541         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3542           arg1 = new0;
3543         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3544           arg1 = new1;
3545
3546         return fold_build2 (code, type, arg0, arg1);
3547       }
3548
3549     default:
3550       return arg;
3551     }
3552 }
3553 \f
3554 /* Return a tree for the case when the result of an expression is RESULT
3555    converted to TYPE and OMITTED was previously an operand of the expression
3556    but is now not needed (e.g., we folded OMITTED * 0).
3557
3558    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
3559    the conversion of RESULT to TYPE.  */
3560
3561 tree
3562 omit_one_operand (tree type, tree result, tree omitted)
3563 {
3564   tree t = fold_convert (type, result);
3565
3566   /* If the resulting operand is an empty statement, just return the omitted
3567      statement casted to void. */
3568   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3569     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3570
3571   if (TREE_SIDE_EFFECTS (omitted))
3572     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3573
3574   return non_lvalue (t);
3575 }
3576
3577 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3578
3579 static tree
3580 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3581 {
3582   tree t = fold_convert (type, result);
3583
3584   /* If the resulting operand is an empty statement, just return the omitted
3585      statement casted to void. */
3586   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3587     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3588
3589   if (TREE_SIDE_EFFECTS (omitted))
3590     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3591
3592   return pedantic_non_lvalue (t);
3593 }
3594
3595 /* Return a tree for the case when the result of an expression is RESULT
3596    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3597    of the expression but are now not needed.
3598
3599    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3600    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3601    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3602    just do the conversion of RESULT to TYPE.  */
3603
3604 tree
3605 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3606 {
3607   tree t = fold_convert (type, result);
3608
3609   if (TREE_SIDE_EFFECTS (omitted2))
3610     t = build2 (COMPOUND_EXPR, type, omitted2, t);
3611   if (TREE_SIDE_EFFECTS (omitted1))
3612     t = build2 (COMPOUND_EXPR, type, omitted1, t);
3613
3614   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3615 }
3616
3617 \f
3618 /* Return a simplified tree node for the truth-negation of ARG.  This
3619    never alters ARG itself.  We assume that ARG is an operation that
3620    returns a truth value (0 or 1).
3621
3622    FIXME: one would think we would fold the result, but it causes
3623    problems with the dominator optimizer.  */
3624
3625 tree
3626 fold_truth_not_expr (tree arg)
3627 {
3628   tree type = TREE_TYPE (arg);
3629   enum tree_code code = TREE_CODE (arg);
3630
3631   /* If this is a comparison, we can simply invert it, except for
3632      floating-point non-equality comparisons, in which case we just
3633      enclose a TRUTH_NOT_EXPR around what we have.  */
3634
3635   if (TREE_CODE_CLASS (code) == tcc_comparison)
3636     {
3637       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3638       if (FLOAT_TYPE_P (op_type)
3639           && flag_trapping_math
3640           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3641           && code != NE_EXPR && code != EQ_EXPR)
3642         return NULL_TREE;
3643       else
3644         {
3645           code = invert_tree_comparison (code,
3646                                          HONOR_NANS (TYPE_MODE (op_type)));
3647           if (code == ERROR_MARK)
3648             return NULL_TREE;
3649           else
3650             return build2 (code, type,
3651                            TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3652         }
3653     }
3654
3655   switch (code)
3656     {
3657     case INTEGER_CST:
3658       return constant_boolean_node (integer_zerop (arg), type);
3659
3660     case TRUTH_AND_EXPR:
3661       return build2 (TRUTH_OR_EXPR, type,
3662                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3663                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3664
3665     case TRUTH_OR_EXPR:
3666       return build2 (TRUTH_AND_EXPR, type,
3667                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3668                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3669
3670     case TRUTH_XOR_EXPR:
3671       /* Here we can invert either operand.  We invert the first operand
3672          unless the second operand is a TRUTH_NOT_EXPR in which case our
3673          result is the XOR of the first operand with the inside of the
3674          negation of the second operand.  */
3675
3676       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3677         return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3678                        TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3679       else
3680         return build2 (TRUTH_XOR_EXPR, type,
3681                        invert_truthvalue (TREE_OPERAND (arg, 0)),
3682                        TREE_OPERAND (arg, 1));
3683
3684     case TRUTH_ANDIF_EXPR:
3685       return build2 (TRUTH_ORIF_EXPR, type,
3686                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3687                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3688
3689     case TRUTH_ORIF_EXPR:
3690       return build2 (TRUTH_ANDIF_EXPR, type,
3691                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3692                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3693
3694     case TRUTH_NOT_EXPR:
3695       return TREE_OPERAND (arg, 0);
3696
3697     case COND_EXPR:
3698       {
3699         tree arg1 = TREE_OPERAND (arg, 1);
3700         tree arg2 = TREE_OPERAND (arg, 2);
3701         /* A COND_EXPR may have a throw as one operand, which
3702            then has void type.  Just leave void operands
3703            as they are.  */
3704         return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3705                        VOID_TYPE_P (TREE_TYPE (arg1))
3706                        ? arg1 : invert_truthvalue (arg1),
3707                        VOID_TYPE_P (TREE_TYPE (arg2))
3708                        ? arg2 : invert_truthvalue (arg2));
3709       }
3710
3711     case COMPOUND_EXPR:
3712       return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3713                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3714
3715     case NON_LVALUE_EXPR:
3716       return invert_truthvalue (TREE_OPERAND (arg, 0));
3717
3718     case NOP_EXPR:
3719       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3720         return build1 (TRUTH_NOT_EXPR, type, arg);
3721
3722     case CONVERT_EXPR:
3723     case FLOAT_EXPR:
3724       return build1 (TREE_CODE (arg), type,
3725                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3726
3727     case BIT_AND_EXPR:
3728       if (!integer_onep (TREE_OPERAND (arg, 1)))
3729         break;
3730       return build2 (EQ_EXPR, type, arg,
3731                      build_int_cst (type, 0));
3732
3733     case SAVE_EXPR:
3734       return build1 (TRUTH_NOT_EXPR, type, arg);
3735
3736     case CLEANUP_POINT_EXPR:
3737       return build1 (CLEANUP_POINT_EXPR, type,
3738                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3739
3740     default:
3741       break;
3742     }
3743
3744   return NULL_TREE;
3745 }
3746
3747 /* Return a simplified tree node for the truth-negation of ARG.  This
3748    never alters ARG itself.  We assume that ARG is an operation that
3749    returns a truth value (0 or 1).
3750
3751    FIXME: one would think we would fold the result, but it causes
3752    problems with the dominator optimizer.  */
3753
3754 tree
3755 invert_truthvalue (tree arg)
3756 {
3757   tree tem;
3758
3759   if (TREE_CODE (arg) == ERROR_MARK)
3760     return arg;
3761
3762   tem = fold_truth_not_expr (arg);
3763   if (!tem)
3764     tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3765
3766   return tem;
3767 }
3768
3769 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3770    operands are another bit-wise operation with a common input.  If so,
3771    distribute the bit operations to save an operation and possibly two if
3772    constants are involved.  For example, convert
3773         (A | B) & (A | C) into A | (B & C)
3774    Further simplification will occur if B and C are constants.
3775
3776    If this optimization cannot be done, 0 will be returned.  */
3777
3778 static tree
3779 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3780 {
3781   tree common;
3782   tree left, right;
3783
3784   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3785       || TREE_CODE (arg0) == code
3786       || (TREE_CODE (arg0) != BIT_AND_EXPR
3787           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3788     return 0;
3789
3790   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3791     {
3792       common = TREE_OPERAND (arg0, 0);
3793       left = TREE_OPERAND (arg0, 1);
3794       right = TREE_OPERAND (arg1, 1);
3795     }
3796   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3797     {
3798       common = TREE_OPERAND (arg0, 0);
3799       left = TREE_OPERAND (arg0, 1);
3800       right = TREE_OPERAND (arg1, 0);
3801     }
3802   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3803     {
3804       common = TREE_OPERAND (arg0, 1);
3805       left = TREE_OPERAND (arg0, 0);
3806       right = TREE_OPERAND (arg1, 1);
3807     }
3808   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3809     {
3810       common = TREE_OPERAND (arg0, 1);
3811       left = TREE_OPERAND (arg0, 0);
3812       right = TREE_OPERAND (arg1, 0);
3813     }
3814   else
3815     return 0;
3816
3817   common = fold_convert (type, common);
3818   left = fold_convert (type, left);
3819   right = fold_convert (type, right);
3820   return fold_build2 (TREE_CODE (arg0), type, common,
3821                       fold_build2 (code, type, left, right));
3822 }
3823
3824 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3825    with code CODE.  This optimization is unsafe.  */
3826 static tree
3827 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3828 {
3829   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3830   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3831
3832   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3833   if (mul0 == mul1
3834       && operand_equal_p (TREE_OPERAND (arg0, 1),
3835                        TREE_OPERAND (arg1, 1), 0))
3836     return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3837                         fold_build2 (code, type,
3838                                      TREE_OPERAND (arg0, 0),
3839                                      TREE_OPERAND (arg1, 0)),
3840                         TREE_OPERAND (arg0, 1));
3841
3842   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3843   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3844                        TREE_OPERAND (arg1, 0), 0)
3845       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3846       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3847     {
3848       REAL_VALUE_TYPE r0, r1;
3849       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3850       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3851       if (!mul0)
3852         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3853       if (!mul1)
3854         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3855       real_arithmetic (&r0, code, &r0, &r1);
3856       return fold_build2 (MULT_EXPR, type,
3857                           TREE_OPERAND (arg0, 0),
3858                           build_real (type, r0));
3859     }
3860
3861   return NULL_TREE;
3862 }
3863 \f
3864 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3865    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3866
3867 static tree
3868 make_bit_field_ref (tree inner, tree type, HOST_WIDE_INT bitsize,
3869                     HOST_WIDE_INT bitpos, int unsignedp)
3870 {
3871   tree result, bftype;
3872
3873   if (bitpos == 0)
3874     {
3875       tree size = TYPE_SIZE (TREE_TYPE (inner));
3876       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3877            || POINTER_TYPE_P (TREE_TYPE (inner)))
3878           && host_integerp (size, 0) 
3879           && tree_low_cst (size, 0) == bitsize)
3880         return fold_convert (type, inner);
3881     }
3882
3883   bftype = type;
3884   if (TYPE_PRECISION (bftype) != bitsize
3885       || TYPE_UNSIGNED (bftype) == !unsignedp)
3886     bftype = build_nonstandard_integer_type (bitsize, 0);
3887
3888   result = build3 (BIT_FIELD_REF, bftype, inner,
3889                    size_int (bitsize), bitsize_int (bitpos));
3890
3891   if (bftype != type)
3892     result = fold_convert (type, result);
3893
3894   return result;
3895 }
3896
3897 /* Optimize a bit-field compare.
3898
3899    There are two cases:  First is a compare against a constant and the
3900    second is a comparison of two items where the fields are at the same
3901    bit position relative to the start of a chunk (byte, halfword, word)
3902    large enough to contain it.  In these cases we can avoid the shift
3903    implicit in bitfield extractions.
3904
3905    For constants, we emit a compare of the shifted constant with the
3906    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3907    compared.  For two fields at the same position, we do the ANDs with the
3908    similar mask and compare the result of the ANDs.
3909
3910    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3911    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3912    are the left and right operands of the comparison, respectively.
3913
3914    If the optimization described above can be done, we return the resulting
3915    tree.  Otherwise we return zero.  */
3916
3917 static tree
3918 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3919                             tree lhs, tree rhs)
3920 {
3921   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3922   tree type = TREE_TYPE (lhs);
3923   tree signed_type, unsigned_type;
3924   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3925   enum machine_mode lmode, rmode, nmode;
3926   int lunsignedp, runsignedp;
3927   int lvolatilep = 0, rvolatilep = 0;
3928   tree linner, rinner = NULL_TREE;
3929   tree mask;
3930   tree offset;
3931
3932   /* Get all the information about the extractions being done.  If the bit size
3933      if the same as the size of the underlying object, we aren't doing an
3934      extraction at all and so can do nothing.  We also don't want to
3935      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3936      then will no longer be able to replace it.  */
3937   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3938                                 &lunsignedp, &lvolatilep, false);
3939   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3940       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3941     return 0;
3942
3943  if (!const_p)
3944    {
3945      /* If this is not a constant, we can only do something if bit positions,
3946         sizes, and signedness are the same.  */
3947      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3948                                    &runsignedp, &rvolatilep, false);
3949
3950      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3951          || lunsignedp != runsignedp || offset != 0
3952          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3953        return 0;
3954    }
3955
3956   /* See if we can find a mode to refer to this field.  We should be able to,
3957      but fail if we can't.  */
3958   nmode = get_best_mode (lbitsize, lbitpos,
3959                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3960                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3961                                 TYPE_ALIGN (TREE_TYPE (rinner))),
3962                          word_mode, lvolatilep || rvolatilep);
3963   if (nmode == VOIDmode)
3964     return 0;
3965
3966   /* Set signed and unsigned types of the precision of this mode for the
3967      shifts below.  */
3968   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3969   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3970
3971   /* Compute the bit position and size for the new reference and our offset
3972      within it. If the new reference is the same size as the original, we
3973      won't optimize anything, so return zero.  */
3974   nbitsize = GET_MODE_BITSIZE (nmode);
3975   nbitpos = lbitpos & ~ (nbitsize - 1);
3976   lbitpos -= nbitpos;
3977   if (nbitsize == lbitsize)
3978     return 0;
3979
3980   if (BYTES_BIG_ENDIAN)
3981     lbitpos = nbitsize - lbitsize - lbitpos;
3982
3983   /* Make the mask to be used against the extracted field.  */
3984   mask = build_int_cst_type (unsigned_type, -1);
3985   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3986   mask = const_binop (RSHIFT_EXPR, mask,
3987                       size_int (nbitsize - lbitsize - lbitpos), 0);
3988
3989   if (! const_p)
3990     /* If not comparing with constant, just rework the comparison
3991        and return.  */
3992     return fold_build2 (code, compare_type,
3993                         fold_build2 (BIT_AND_EXPR, unsigned_type,
3994                                      make_bit_field_ref (linner,
3995                                                          unsigned_type,
3996                                                          nbitsize, nbitpos,
3997                                                          1),
3998                                      mask),
3999                         fold_build2 (BIT_AND_EXPR, unsigned_type,
4000                                      make_bit_field_ref (rinner,
4001                                                          unsigned_type,
4002                                                          nbitsize, nbitpos,
4003                                                          1),
4004                                      mask));
4005
4006   /* Otherwise, we are handling the constant case. See if the constant is too
4007      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
4008      this not only for its own sake, but to avoid having to test for this
4009      error case below.  If we didn't, we might generate wrong code.
4010
4011      For unsigned fields, the constant shifted right by the field length should
4012      be all zero.  For signed fields, the high-order bits should agree with
4013      the sign bit.  */
4014
4015   if (lunsignedp)
4016     {
4017       if (! integer_zerop (const_binop (RSHIFT_EXPR,
4018                                         fold_convert (unsigned_type, rhs),
4019                                         size_int (lbitsize), 0)))
4020         {
4021           warning (0, "comparison is always %d due to width of bit-field",
4022                    code == NE_EXPR);
4023           return constant_boolean_node (code == NE_EXPR, compare_type);
4024         }
4025     }
4026   else
4027     {
4028       tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
4029                               size_int (lbitsize - 1), 0);
4030       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
4031         {
4032           warning (0, "comparison is always %d due to width of bit-field",
4033                    code == NE_EXPR);
4034           return constant_boolean_node (code == NE_EXPR, compare_type);
4035         }
4036     }
4037
4038   /* Single-bit compares should always be against zero.  */
4039   if (lbitsize == 1 && ! integer_zerop (rhs))
4040     {
4041       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4042       rhs = build_int_cst (type, 0);
4043     }
4044
4045   /* Make a new bitfield reference, shift the constant over the
4046      appropriate number of bits and mask it with the computed mask
4047      (in case this was a signed field).  If we changed it, make a new one.  */
4048   lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
4049   if (lvolatilep)
4050     {
4051       TREE_SIDE_EFFECTS (lhs) = 1;
4052       TREE_THIS_VOLATILE (lhs) = 1;
4053     }
4054
4055   rhs = const_binop (BIT_AND_EXPR,
4056                      const_binop (LSHIFT_EXPR,
4057                                   fold_convert (unsigned_type, rhs),
4058                                   size_int (lbitpos), 0),
4059                      mask, 0);
4060
4061   return build2 (code, compare_type,
4062                  build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4063                  rhs);
4064 }
4065 \f
4066 /* Subroutine for fold_truthop: decode a field reference.
4067
4068    If EXP is a comparison reference, we return the innermost reference.
4069
4070    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4071    set to the starting bit number.
4072
4073    If the innermost field can be completely contained in a mode-sized
4074    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
4075
4076    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4077    otherwise it is not changed.
4078
4079    *PUNSIGNEDP is set to the signedness of the field.
4080
4081    *PMASK is set to the mask used.  This is either contained in a
4082    BIT_AND_EXPR or derived from the width of the field.
4083
4084    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4085
4086    Return 0 if this is not a component reference or is one that we can't
4087    do anything with.  */
4088
4089 static tree
4090 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
4091                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4092                         int *punsignedp, int *pvolatilep,
4093                         tree *pmask, tree *pand_mask)
4094 {
4095   tree outer_type = 0;
4096   tree and_mask = 0;
4097   tree mask, inner, offset;
4098   tree unsigned_type;
4099   unsigned int precision;
4100
4101   /* All the optimizations using this function assume integer fields.
4102      There are problems with FP fields since the type_for_size call
4103      below can fail for, e.g., XFmode.  */
4104   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4105     return 0;
4106
4107   /* We are interested in the bare arrangement of bits, so strip everything
4108      that doesn't affect the machine mode.  However, record the type of the
4109      outermost expression if it may matter below.  */
4110   if (CONVERT_EXPR_P (exp)
4111       || TREE_CODE (exp) == NON_LVALUE_EXPR)
4112     outer_type = TREE_TYPE (exp);
4113   STRIP_NOPS (exp);
4114
4115   if (TREE_CODE (exp) == BIT_AND_EXPR)
4116     {
4117       and_mask = TREE_OPERAND (exp, 1);
4118       exp = TREE_OPERAND (exp, 0);
4119       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4120       if (TREE_CODE (and_mask) != INTEGER_CST)
4121         return 0;
4122     }
4123
4124   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4125                                punsignedp, pvolatilep, false);
4126   if ((inner == exp && and_mask == 0)
4127       || *pbitsize < 0 || offset != 0
4128       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4129     return 0;
4130
4131   /* If the number of bits in the reference is the same as the bitsize of
4132      the outer type, then the outer type gives the signedness. Otherwise
4133      (in case of a small bitfield) the signedness is unchanged.  */
4134   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4135     *punsignedp = TYPE_UNSIGNED (outer_type);
4136
4137   /* Compute the mask to access the bitfield.  */
4138   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4139   precision = TYPE_PRECISION (unsigned_type);
4140
4141   mask = build_int_cst_type (unsigned_type, -1);
4142
4143   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4144   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4145
4146   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
4147   if (and_mask != 0)
4148     mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
4149                         fold_convert (unsigned_type, and_mask), mask);
4150
4151   *pmask = mask;
4152   *pand_mask = and_mask;
4153   return inner;
4154 }
4155
4156 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4157    bit positions.  */
4158
4159 static int
4160 all_ones_mask_p (const_tree mask, int size)
4161 {
4162   tree type = TREE_TYPE (mask);
4163   unsigned int precision = TYPE_PRECISION (type);
4164   tree tmask;
4165
4166   tmask = build_int_cst_type (signed_type_for (type), -1);
4167
4168   return
4169     tree_int_cst_equal (mask,
4170                         const_binop (RSHIFT_EXPR,
4171                                      const_binop (LSHIFT_EXPR, tmask,
4172                                                   size_int (precision - size),
4173                                                   0),
4174                                      size_int (precision - size), 0));
4175 }
4176
4177 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4178    represents the sign bit of EXP's type.  If EXP represents a sign
4179    or zero extension, also test VAL against the unextended type.
4180    The return value is the (sub)expression whose sign bit is VAL,
4181    or NULL_TREE otherwise.  */
4182
4183 static tree
4184 sign_bit_p (tree exp, const_tree val)
4185 {
4186   unsigned HOST_WIDE_INT mask_lo, lo;
4187   HOST_WIDE_INT mask_hi, hi;
4188   int width;
4189   tree t;
4190
4191   /* Tree EXP must have an integral type.  */
4192   t = TREE_TYPE (exp);
4193   if (! INTEGRAL_TYPE_P (t))
4194     return NULL_TREE;
4195
4196   /* Tree VAL must be an integer constant.  */
4197   if (TREE_CODE (val) != INTEGER_CST
4198       || TREE_OVERFLOW (val))
4199     return NULL_TREE;
4200
4201   width = TYPE_PRECISION (t);
4202   if (width > HOST_BITS_PER_WIDE_INT)
4203     {
4204       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4205       lo = 0;
4206
4207       mask_hi = ((unsigned HOST_WIDE_INT) -1
4208                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
4209       mask_lo = -1;
4210     }
4211   else
4212     {
4213       hi = 0;
4214       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4215
4216       mask_hi = 0;
4217       mask_lo = ((unsigned HOST_WIDE_INT) -1
4218                  >> (HOST_BITS_PER_WIDE_INT - width));
4219     }
4220
4221   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4222      treat VAL as if it were unsigned.  */
4223   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4224       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4225     return exp;
4226
4227   /* Handle extension from a narrower type.  */
4228   if (TREE_CODE (exp) == NOP_EXPR
4229       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4230     return sign_bit_p (TREE_OPERAND (exp, 0), val);
4231
4232   return NULL_TREE;
4233 }
4234
4235 /* Subroutine for fold_truthop: determine if an operand is simple enough
4236    to be evaluated unconditionally.  */
4237
4238 static int
4239 simple_operand_p (const_tree exp)
4240 {
4241   /* Strip any conversions that don't change the machine mode.  */
4242   STRIP_NOPS (exp);
4243
4244   return (CONSTANT_CLASS_P (exp)
4245           || TREE_CODE (exp) == SSA_NAME
4246           || (DECL_P (exp)
4247               && ! TREE_ADDRESSABLE (exp)
4248               && ! TREE_THIS_VOLATILE (exp)
4249               && ! DECL_NONLOCAL (exp)
4250               /* Don't regard global variables as simple.  They may be
4251                  allocated in ways unknown to the compiler (shared memory,
4252                  #pragma weak, etc).  */
4253               && ! TREE_PUBLIC (exp)
4254               && ! DECL_EXTERNAL (exp)
4255               /* Loading a static variable is unduly expensive, but global
4256                  registers aren't expensive.  */
4257               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4258 }
4259 \f
4260 /* The following functions are subroutines to fold_range_test and allow it to
4261    try to change a logical combination of comparisons into a range test.
4262
4263    For example, both
4264         X == 2 || X == 3 || X == 4 || X == 5
4265    and
4266         X >= 2 && X <= 5
4267    are converted to
4268         (unsigned) (X - 2) <= 3
4269
4270    We describe each set of comparisons as being either inside or outside
4271    a range, using a variable named like IN_P, and then describe the
4272    range with a lower and upper bound.  If one of the bounds is omitted,
4273    it represents either the highest or lowest value of the type.
4274
4275    In the comments below, we represent a range by two numbers in brackets
4276    preceded by a "+" to designate being inside that range, or a "-" to
4277    designate being outside that range, so the condition can be inverted by
4278    flipping the prefix.  An omitted bound is represented by a "-".  For
4279    example, "- [-, 10]" means being outside the range starting at the lowest
4280    possible value and ending at 10, in other words, being greater than 10.
4281    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4282    always false.
4283
4284    We set up things so that the missing bounds are handled in a consistent
4285    manner so neither a missing bound nor "true" and "false" need to be
4286    handled using a special case.  */
4287
4288 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4289    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4290    and UPPER1_P are nonzero if the respective argument is an upper bound
4291    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
4292    must be specified for a comparison.  ARG1 will be converted to ARG0's
4293    type if both are specified.  */
4294
4295 static tree
4296 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4297              tree arg1, int upper1_p)
4298 {
4299   tree tem;
4300   int result;
4301   int sgn0, sgn1;
4302
4303   /* If neither arg represents infinity, do the normal operation.
4304      Else, if not a comparison, return infinity.  Else handle the special
4305      comparison rules. Note that most of the cases below won't occur, but
4306      are handled for consistency.  */
4307
4308   if (arg0 != 0 && arg1 != 0)
4309     {
4310       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4311                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
4312       STRIP_NOPS (tem);
4313       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4314     }
4315
4316   if (TREE_CODE_CLASS (code) != tcc_comparison)
4317     return 0;
4318
4319   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4320      for neither.  In real maths, we cannot assume open ended ranges are
4321      the same. But, this is computer arithmetic, where numbers are finite.
4322      We can therefore make the transformation of any unbounded range with
4323      the value Z, Z being greater than any representable number. This permits
4324      us to treat unbounded ranges as equal.  */
4325   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4326   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4327   switch (code)
4328     {
4329     case EQ_EXPR:
4330       result = sgn0 == sgn1;
4331       break;
4332     case NE_EXPR:
4333       result = sgn0 != sgn1;
4334       break;
4335     case LT_EXPR:
4336       result = sgn0 < sgn1;
4337       break;
4338     case LE_EXPR:
4339       result = sgn0 <= sgn1;
4340       break;
4341     case GT_EXPR:
4342       result = sgn0 > sgn1;
4343       break;
4344     case GE_EXPR:
4345       result = sgn0 >= sgn1;
4346       break;
4347     default:
4348       gcc_unreachable ();
4349     }
4350
4351   return constant_boolean_node (result, type);
4352 }
4353 \f
4354 /* Given EXP, a logical expression, set the range it is testing into
4355    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4356    actually being tested.  *PLOW and *PHIGH will be made of the same
4357    type as the returned expression.  If EXP is not a comparison, we
4358    will most likely not be returning a useful value and range.  Set
4359    *STRICT_OVERFLOW_P to true if the return value is only valid
4360    because signed overflow is undefined; otherwise, do not change
4361    *STRICT_OVERFLOW_P.  */
4362
4363 static tree
4364 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4365             bool *strict_overflow_p)
4366 {
4367   enum tree_code code;
4368   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4369   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4370   int in_p, n_in_p;
4371   tree low, high, n_low, n_high;
4372
4373   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4374      and see if we can refine the range.  Some of the cases below may not
4375      happen, but it doesn't seem worth worrying about this.  We "continue"
4376      the outer loop when we've changed something; otherwise we "break"
4377      the switch, which will "break" the while.  */
4378
4379   in_p = 0;
4380   low = high = build_int_cst (TREE_TYPE (exp), 0);
4381
4382   while (1)
4383     {
4384       code = TREE_CODE (exp);
4385       exp_type = TREE_TYPE (exp);
4386
4387       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4388         {
4389           if (TREE_OPERAND_LENGTH (exp) > 0)
4390             arg0 = TREE_OPERAND (exp, 0);
4391           if (TREE_CODE_CLASS (code) == tcc_comparison
4392               || TREE_CODE_CLASS (code) == tcc_unary
4393               || TREE_CODE_CLASS (code) == tcc_binary)
4394             arg0_type = TREE_TYPE (arg0);
4395           if (TREE_CODE_CLASS (code) == tcc_binary
4396               || TREE_CODE_CLASS (code) == tcc_comparison
4397               || (TREE_CODE_CLASS (code) == tcc_expression
4398                   && TREE_OPERAND_LENGTH (exp) > 1))
4399             arg1 = TREE_OPERAND (exp, 1);
4400         }
4401
4402       switch (code)
4403         {
4404         case TRUTH_NOT_EXPR:
4405           in_p = ! in_p, exp = arg0;
4406           continue;
4407
4408         case EQ_EXPR: case NE_EXPR:
4409         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4410           /* We can only do something if the range is testing for zero
4411              and if the second operand is an integer constant.  Note that
4412              saying something is "in" the range we make is done by
4413              complementing IN_P since it will set in the initial case of
4414              being not equal to zero; "out" is leaving it alone.  */
4415           if (low == 0 || high == 0
4416               || ! integer_zerop (low) || ! integer_zerop (high)
4417               || TREE_CODE (arg1) != INTEGER_CST)
4418             break;
4419
4420           switch (code)
4421             {
4422             case NE_EXPR:  /* - [c, c]  */
4423               low = high = arg1;
4424               break;
4425             case EQ_EXPR:  /* + [c, c]  */
4426               in_p = ! in_p, low = high = arg1;
4427               break;
4428             case GT_EXPR:  /* - [-, c] */
4429               low = 0, high = arg1;
4430               break;
4431             case GE_EXPR:  /* + [c, -] */
4432               in_p = ! in_p, low = arg1, high = 0;
4433               break;
4434             case LT_EXPR:  /* - [c, -] */
4435               low = arg1, high = 0;
4436               break;
4437             case LE_EXPR:  /* + [-, c] */
4438               in_p = ! in_p, low = 0, high = arg1;
4439               break;
4440             default:
4441               gcc_unreachable ();
4442             }
4443
4444           /* If this is an unsigned comparison, we also know that EXP is
4445              greater than or equal to zero.  We base the range tests we make
4446              on that fact, so we record it here so we can parse existing
4447              range tests.  We test arg0_type since often the return type
4448              of, e.g. EQ_EXPR, is boolean.  */
4449           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4450             {
4451               if (! merge_ranges (&n_in_p, &n_low, &n_high,
4452                                   in_p, low, high, 1,
4453                                   build_int_cst (arg0_type, 0),
4454                                   NULL_TREE))
4455                 break;
4456
4457               in_p = n_in_p, low = n_low, high = n_high;
4458
4459               /* If the high bound is missing, but we have a nonzero low
4460                  bound, reverse the range so it goes from zero to the low bound
4461                  minus 1.  */
4462               if (high == 0 && low && ! integer_zerop (low))
4463                 {
4464                   in_p = ! in_p;
4465                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4466                                       integer_one_node, 0);
4467                   low = build_int_cst (arg0_type, 0);
4468                 }
4469             }
4470
4471           exp = arg0;
4472           continue;
4473
4474         case NEGATE_EXPR:
4475           /* (-x) IN [a,b] -> x in [-b, -a]  */
4476           n_low = range_binop (MINUS_EXPR, exp_type,
4477                                build_int_cst (exp_type, 0),
4478                                0, high, 1);
4479           n_high = range_binop (MINUS_EXPR, exp_type,
4480                                 build_int_cst (exp_type, 0),
4481                                 0, low, 0);
4482           if (n_high != 0 && TREE_OVERFLOW (n_high))
4483             break;
4484           goto normalize;
4485
4486         case BIT_NOT_EXPR:
4487           /* ~ X -> -X - 1  */
4488           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4489                         build_int_cst (exp_type, 1));
4490           continue;
4491
4492         case PLUS_EXPR:  case MINUS_EXPR:
4493           if (TREE_CODE (arg1) != INTEGER_CST)
4494             break;
4495
4496           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4497              move a constant to the other side.  */
4498           if (!TYPE_UNSIGNED (arg0_type)
4499               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4500             break;
4501
4502           /* If EXP is signed, any overflow in the computation is undefined,
4503              so we don't worry about it so long as our computations on
4504              the bounds don't overflow.  For unsigned, overflow is defined
4505              and this is exactly the right thing.  */
4506           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4507                                arg0_type, low, 0, arg1, 0);
4508           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4509                                 arg0_type, high, 1, arg1, 0);
4510           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4511               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4512             break;
4513
4514           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4515             *strict_overflow_p = true;
4516
4517         normalize:
4518           /* Check for an unsigned range which has wrapped around the maximum
4519              value thus making n_high < n_low, and normalize it.  */
4520           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4521             {
4522               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4523                                  integer_one_node, 0);
4524               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4525                                   integer_one_node, 0);
4526
4527               /* If the range is of the form +/- [ x+1, x ], we won't
4528                  be able to normalize it.  But then, it represents the
4529                  whole range or the empty set, so make it
4530                  +/- [ -, - ].  */
4531               if (tree_int_cst_equal (n_low, low)
4532                   && tree_int_cst_equal (n_high, high))
4533                 low = high = 0;
4534               else
4535                 in_p = ! in_p;
4536             }
4537           else
4538             low = n_low, high = n_high;
4539
4540           exp = arg0;
4541           continue;
4542
4543         CASE_CONVERT: case NON_LVALUE_EXPR:
4544           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4545             break;
4546
4547           if (! INTEGRAL_TYPE_P (arg0_type)
4548               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4549               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4550             break;
4551
4552           n_low = low, n_high = high;
4553
4554           if (n_low != 0)
4555             n_low = fold_convert (arg0_type, n_low);
4556
4557           if (n_high != 0)
4558             n_high = fold_convert (arg0_type, n_high);
4559
4560
4561           /* If we're converting arg0 from an unsigned type, to exp,
4562              a signed type,  we will be doing the comparison as unsigned.
4563              The tests above have already verified that LOW and HIGH
4564              are both positive.
4565
4566              So we have to ensure that we will handle large unsigned
4567              values the same way that the current signed bounds treat
4568              negative values.  */
4569
4570           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4571             {
4572               tree high_positive;
4573               tree equiv_type;
4574               /* For fixed-point modes, we need to pass the saturating flag
4575                  as the 2nd parameter.  */
4576               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4577                 equiv_type = lang_hooks.types.type_for_mode
4578                              (TYPE_MODE (arg0_type),
4579                               TYPE_SATURATING (arg0_type));
4580               else
4581                 equiv_type = lang_hooks.types.type_for_mode
4582                              (TYPE_MODE (arg0_type), 1);
4583
4584               /* A range without an upper bound is, naturally, unbounded.
4585                  Since convert would have cropped a very large value, use
4586                  the max value for the destination type.  */
4587               high_positive
4588                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4589                 : TYPE_MAX_VALUE (arg0_type);
4590
4591               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4592                 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4593                                              fold_convert (arg0_type,
4594                                                            high_positive),
4595                                              build_int_cst (arg0_type, 1));
4596
4597               /* If the low bound is specified, "and" the range with the
4598                  range for which the original unsigned value will be
4599                  positive.  */
4600               if (low != 0)
4601                 {
4602                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4603                                       1, n_low, n_high, 1,
4604                                       fold_convert (arg0_type,
4605                                                     integer_zero_node),
4606                                       high_positive))
4607                     break;
4608
4609                   in_p = (n_in_p == in_p);
4610                 }
4611               else
4612                 {
4613                   /* Otherwise, "or" the range with the range of the input
4614                      that will be interpreted as negative.  */
4615                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4616                                       0, n_low, n_high, 1,
4617                                       fold_convert (arg0_type,
4618                                                     integer_zero_node),
4619                                       high_positive))
4620                     break;
4621
4622                   in_p = (in_p != n_in_p);
4623                 }
4624             }
4625
4626           exp = arg0;
4627           low = n_low, high = n_high;
4628           continue;
4629
4630         default:
4631           break;
4632         }
4633
4634       break;
4635     }
4636
4637   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4638   if (TREE_CODE (exp) == INTEGER_CST)
4639     {
4640       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4641                                                  exp, 0, low, 0))
4642                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4643                                                     exp, 1, high, 1)));
4644       low = high = 0;
4645       exp = 0;
4646     }
4647
4648   *pin_p = in_p, *plow = low, *phigh = high;
4649   return exp;
4650 }
4651 \f
4652 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4653    type, TYPE, return an expression to test if EXP is in (or out of, depending
4654    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4655
4656 static tree
4657 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4658 {
4659   tree etype = TREE_TYPE (exp);
4660   tree value;
4661
4662 #ifdef HAVE_canonicalize_funcptr_for_compare
4663   /* Disable this optimization for function pointer expressions
4664      on targets that require function pointer canonicalization.  */
4665   if (HAVE_canonicalize_funcptr_for_compare
4666       && TREE_CODE (etype) == POINTER_TYPE
4667       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4668     return NULL_TREE;
4669 #endif
4670
4671   if (! in_p)
4672     {
4673       value = build_range_check (type, exp, 1, low, high);
4674       if (value != 0)
4675         return invert_truthvalue (value);
4676
4677       return 0;
4678     }
4679
4680   if (low == 0 && high == 0)
4681     return build_int_cst (type, 1);
4682
4683   if (low == 0)
4684     return fold_build2 (LE_EXPR, type, exp,
4685                         fold_convert (etype, high));
4686
4687   if (high == 0)
4688     return fold_build2 (GE_EXPR, type, exp,
4689                         fold_convert (etype, low));
4690
4691   if (operand_equal_p (low, high, 0))
4692     return fold_build2 (EQ_EXPR, type, exp,
4693                         fold_convert (etype, low));
4694
4695   if (integer_zerop (low))
4696     {
4697       if (! TYPE_UNSIGNED (etype))
4698         {
4699           etype = unsigned_type_for (etype);
4700           high = fold_convert (etype, high);
4701           exp = fold_convert (etype, exp);
4702         }
4703       return build_range_check (type, exp, 1, 0, high);
4704     }
4705
4706   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4707   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4708     {
4709       unsigned HOST_WIDE_INT lo;
4710       HOST_WIDE_INT hi;
4711       int prec;
4712
4713       prec = TYPE_PRECISION (etype);
4714       if (prec <= HOST_BITS_PER_WIDE_INT)
4715         {
4716           hi = 0;
4717           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4718         }
4719       else
4720         {
4721           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4722           lo = (unsigned HOST_WIDE_INT) -1;
4723         }
4724
4725       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4726         {
4727           if (TYPE_UNSIGNED (etype))
4728             {
4729               tree signed_etype = signed_type_for (etype);
4730               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4731                 etype
4732                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4733               else
4734                 etype = signed_etype;
4735               exp = fold_convert (etype, exp);
4736             }
4737           return fold_build2 (GT_EXPR, type, exp,
4738                               build_int_cst (etype, 0));
4739         }
4740     }
4741
4742   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4743      This requires wrap-around arithmetics for the type of the expression.  */
4744   switch (TREE_CODE (etype))
4745     {
4746     case INTEGER_TYPE:
4747       /* There is no requirement that LOW be within the range of ETYPE
4748          if the latter is a subtype.  It must, however, be within the base
4749          type of ETYPE.  So be sure we do the subtraction in that type.  */
4750       if (TREE_TYPE (etype))
4751         etype = TREE_TYPE (etype);
4752       break;
4753
4754     case ENUMERAL_TYPE:
4755     case BOOLEAN_TYPE:
4756       etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4757                                               TYPE_UNSIGNED (etype));
4758       break;
4759
4760     default:
4761       break;
4762     }
4763
4764   /* If we don't have wrap-around arithmetics upfront, try to force it.  */
4765   if (TREE_CODE (etype) == INTEGER_TYPE
4766       && !TYPE_OVERFLOW_WRAPS (etype))
4767     {
4768       tree utype, minv, maxv;
4769
4770       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4771          for the type in question, as we rely on this here.  */
4772       utype = unsigned_type_for (etype);
4773       maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4774       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4775                           integer_one_node, 1);
4776       minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4777
4778       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4779                                       minv, 1, maxv, 1)))
4780         etype = utype;
4781       else
4782         return 0;
4783     }
4784
4785   high = fold_convert (etype, high);
4786   low = fold_convert (etype, low);
4787   exp = fold_convert (etype, exp);
4788
4789   value = const_binop (MINUS_EXPR, high, low, 0);
4790
4791
4792   if (POINTER_TYPE_P (etype))
4793     {
4794       if (value != 0 && !TREE_OVERFLOW (value))
4795         {
4796           low = fold_convert (sizetype, low);
4797           low = fold_build1 (NEGATE_EXPR, sizetype, low);
4798           return build_range_check (type,
4799                                     fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4800                                     1, build_int_cst (etype, 0), value);
4801         }
4802       return 0;
4803     }
4804
4805   if (value != 0 && !TREE_OVERFLOW (value))
4806     return build_range_check (type,
4807                               fold_build2 (MINUS_EXPR, etype, exp, low),
4808                               1, build_int_cst (etype, 0), value);
4809
4810   return 0;
4811 }
4812 \f
4813 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4814
4815 static tree
4816 range_predecessor (tree val)
4817 {
4818   tree type = TREE_TYPE (val);
4819
4820   if (INTEGRAL_TYPE_P (type)
4821       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4822     return 0;
4823   else
4824     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4825 }
4826
4827 /* Return the successor of VAL in its type, handling the infinite case.  */
4828
4829 static tree
4830 range_successor (tree val)
4831 {
4832   tree type = TREE_TYPE (val);
4833
4834   if (INTEGRAL_TYPE_P (type)
4835       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4836     return 0;
4837   else
4838     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4839 }
4840
4841 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4842    can, 0 if we can't.  Set the output range into the specified parameters.  */
4843
4844 static int
4845 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4846               tree high0, int in1_p, tree low1, tree high1)
4847 {
4848   int no_overlap;
4849   int subset;
4850   int temp;
4851   tree tem;
4852   int in_p;
4853   tree low, high;
4854   int lowequal = ((low0 == 0 && low1 == 0)
4855                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4856                                                 low0, 0, low1, 0)));
4857   int highequal = ((high0 == 0 && high1 == 0)
4858                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4859                                                  high0, 1, high1, 1)));
4860
4861   /* Make range 0 be the range that starts first, or ends last if they
4862      start at the same value.  Swap them if it isn't.  */
4863   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4864                                  low0, 0, low1, 0))
4865       || (lowequal
4866           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4867                                         high1, 1, high0, 1))))
4868     {
4869       temp = in0_p, in0_p = in1_p, in1_p = temp;
4870       tem = low0, low0 = low1, low1 = tem;
4871       tem = high0, high0 = high1, high1 = tem;
4872     }
4873
4874   /* Now flag two cases, whether the ranges are disjoint or whether the
4875      second range is totally subsumed in the first.  Note that the tests
4876      below are simplified by the ones above.  */
4877   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4878                                           high0, 1, low1, 0));
4879   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4880                                       high1, 1, high0, 1));
4881
4882   /* We now have four cases, depending on whether we are including or
4883      excluding the two ranges.  */
4884   if (in0_p && in1_p)
4885     {
4886       /* If they don't overlap, the result is false.  If the second range
4887          is a subset it is the result.  Otherwise, the range is from the start
4888          of the second to the end of the first.  */
4889       if (no_overlap)
4890         in_p = 0, low = high = 0;
4891       else if (subset)
4892         in_p = 1, low = low1, high = high1;
4893       else
4894         in_p = 1, low = low1, high = high0;
4895     }
4896
4897   else if (in0_p && ! in1_p)
4898     {
4899       /* If they don't overlap, the result is the first range.  If they are
4900          equal, the result is false.  If the second range is a subset of the
4901          first, and the ranges begin at the same place, we go from just after
4902          the end of the second range to the end of the first.  If the second
4903          range is not a subset of the first, or if it is a subset and both
4904          ranges end at the same place, the range starts at the start of the
4905          first range and ends just before the second range.
4906          Otherwise, we can't describe this as a single range.  */
4907       if (no_overlap)
4908         in_p = 1, low = low0, high = high0;
4909       else if (lowequal && highequal)
4910         in_p = 0, low = high = 0;
4911       else if (subset && lowequal)
4912         {
4913           low = range_successor (high1);
4914           high = high0;
4915           in_p = 1;
4916           if (low == 0)
4917             {
4918               /* We are in the weird situation where high0 > high1 but
4919                  high1 has no successor.  Punt.  */
4920               return 0;
4921             }
4922         }
4923       else if (! subset || highequal)
4924         {
4925           low = low0;
4926           high = range_predecessor (low1);
4927           in_p = 1;
4928           if (high == 0)
4929             {
4930               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4931               return 0;
4932             }
4933         }
4934       else
4935         return 0;
4936     }
4937
4938   else if (! in0_p && in1_p)
4939     {
4940       /* If they don't overlap, the result is the second range.  If the second
4941          is a subset of the first, the result is false.  Otherwise,
4942          the range starts just after the first range and ends at the
4943          end of the second.  */
4944       if (no_overlap)
4945         in_p = 1, low = low1, high = high1;
4946       else if (subset || highequal)
4947         in_p = 0, low = high = 0;
4948       else
4949         {
4950           low = range_successor (high0);
4951           high = high1;
4952           in_p = 1;
4953           if (low == 0)
4954             {
4955               /* high1 > high0 but high0 has no successor.  Punt.  */
4956               return 0;
4957             }
4958         }
4959     }
4960
4961   else
4962     {
4963       /* The case where we are excluding both ranges.  Here the complex case
4964          is if they don't overlap.  In that case, the only time we have a
4965          range is if they are adjacent.  If the second is a subset of the
4966          first, the result is the first.  Otherwise, the range to exclude
4967          starts at the beginning of the first range and ends at the end of the
4968          second.  */
4969       if (no_overlap)
4970         {
4971           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4972                                          range_successor (high0),
4973                                          1, low1, 0)))
4974             in_p = 0, low = low0, high = high1;
4975           else
4976             {
4977               /* Canonicalize - [min, x] into - [-, x].  */
4978               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4979                 switch (TREE_CODE (TREE_TYPE (low0)))
4980                   {
4981                   case ENUMERAL_TYPE:
4982                     if (TYPE_PRECISION (TREE_TYPE (low0))
4983                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4984                       break;
4985                     /* FALLTHROUGH */
4986                   case INTEGER_TYPE:
4987                     if (tree_int_cst_equal (low0,
4988                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4989                       low0 = 0;
4990                     break;
4991                   case POINTER_TYPE:
4992                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4993                         && integer_zerop (low0))
4994                       low0 = 0;
4995                     break;
4996                   default:
4997                     break;
4998                   }
4999
5000               /* Canonicalize - [x, max] into - [x, -].  */
5001               if (high1 && TREE_CODE (high1) == INTEGER_CST)
5002                 switch (TREE_CODE (TREE_TYPE (high1)))
5003                   {
5004                   case ENUMERAL_TYPE:
5005                     if (TYPE_PRECISION (TREE_TYPE (high1))
5006                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5007                       break;
5008                     /* FALLTHROUGH */
5009                   case INTEGER_TYPE:
5010                     if (tree_int_cst_equal (high1,
5011                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
5012                       high1 = 0;
5013                     break;
5014                   case POINTER_TYPE:
5015                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
5016                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5017                                                        high1, 1,
5018                                                        integer_one_node, 1)))
5019                       high1 = 0;
5020                     break;
5021                   default:
5022                     break;
5023                   }
5024
5025               /* The ranges might be also adjacent between the maximum and
5026                  minimum values of the given type.  For
5027                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5028                  return + [x + 1, y - 1].  */
5029               if (low0 == 0 && high1 == 0)
5030                 {
5031                   low = range_successor (high0);
5032                   high = range_predecessor (low1);
5033                   if (low == 0 || high == 0)
5034                     return 0;
5035
5036                   in_p = 1;
5037                 }
5038               else
5039                 return 0;
5040             }
5041         }
5042       else if (subset)
5043         in_p = 0, low = low0, high = high0;
5044       else
5045         in_p = 0, low = low0, high = high1;
5046     }
5047
5048   *pin_p = in_p, *plow = low, *phigh = high;
5049   return 1;
5050 }
5051 \f
5052
5053 /* Subroutine of fold, looking inside expressions of the form
5054    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5055    of the COND_EXPR.  This function is being used also to optimize
5056    A op B ? C : A, by reversing the comparison first.
5057
5058    Return a folded expression whose code is not a COND_EXPR
5059    anymore, or NULL_TREE if no folding opportunity is found.  */
5060
5061 static tree
5062 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
5063 {
5064   enum tree_code comp_code = TREE_CODE (arg0);
5065   tree arg00 = TREE_OPERAND (arg0, 0);
5066   tree arg01 = TREE_OPERAND (arg0, 1);
5067   tree arg1_type = TREE_TYPE (arg1);
5068   tree tem;
5069
5070   STRIP_NOPS (arg1);
5071   STRIP_NOPS (arg2);
5072
5073   /* If we have A op 0 ? A : -A, consider applying the following
5074      transformations:
5075
5076      A == 0? A : -A    same as -A
5077      A != 0? A : -A    same as A
5078      A >= 0? A : -A    same as abs (A)
5079      A > 0?  A : -A    same as abs (A)
5080      A <= 0? A : -A    same as -abs (A)
5081      A < 0?  A : -A    same as -abs (A)
5082
5083      None of these transformations work for modes with signed
5084      zeros.  If A is +/-0, the first two transformations will
5085      change the sign of the result (from +0 to -0, or vice
5086      versa).  The last four will fix the sign of the result,
5087      even though the original expressions could be positive or
5088      negative, depending on the sign of A.
5089
5090      Note that all these transformations are correct if A is
5091      NaN, since the two alternatives (A and -A) are also NaNs.  */
5092   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5093       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5094           ? real_zerop (arg01)
5095           : integer_zerop (arg01))
5096       && ((TREE_CODE (arg2) == NEGATE_EXPR
5097            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5098              /* In the case that A is of the form X-Y, '-A' (arg2) may
5099                 have already been folded to Y-X, check for that. */
5100           || (TREE_CODE (arg1) == MINUS_EXPR
5101               && TREE_CODE (arg2) == MINUS_EXPR
5102               && operand_equal_p (TREE_OPERAND (arg1, 0),
5103                                   TREE_OPERAND (arg2, 1), 0)
5104               && operand_equal_p (TREE_OPERAND (arg1, 1),
5105                                   TREE_OPERAND (arg2, 0), 0))))
5106     switch (comp_code)
5107       {
5108       case EQ_EXPR:
5109       case UNEQ_EXPR:
5110         tem = fold_convert (arg1_type, arg1);
5111         return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
5112       case NE_EXPR:
5113       case LTGT_EXPR:
5114         return pedantic_non_lvalue (fold_convert (type, arg1));
5115       case UNGE_EXPR:
5116       case UNGT_EXPR:
5117         if (flag_trapping_math)
5118           break;
5119         /* Fall through.  */
5120       case GE_EXPR:
5121       case GT_EXPR:
5122         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5123           arg1 = fold_convert (signed_type_for
5124                                (TREE_TYPE (arg1)), arg1);
5125         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5126         return pedantic_non_lvalue (fold_convert (type, tem));
5127       case UNLE_EXPR:
5128       case UNLT_EXPR:
5129         if (flag_trapping_math)
5130           break;
5131       case LE_EXPR:
5132       case LT_EXPR:
5133         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5134           arg1 = fold_convert (signed_type_for
5135                                (TREE_TYPE (arg1)), arg1);
5136         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5137         return negate_expr (fold_convert (type, tem));
5138       default:
5139         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5140         break;
5141       }
5142
5143   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
5144      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
5145      both transformations are correct when A is NaN: A != 0
5146      is then true, and A == 0 is false.  */
5147
5148   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5149       && integer_zerop (arg01) && integer_zerop (arg2))
5150     {
5151       if (comp_code == NE_EXPR)
5152         return pedantic_non_lvalue (fold_convert (type, arg1));
5153       else if (comp_code == EQ_EXPR)
5154         return build_int_cst (type, 0);
5155     }
5156
5157   /* Try some transformations of A op B ? A : B.
5158
5159      A == B? A : B    same as B
5160      A != B? A : B    same as A
5161      A >= B? A : B    same as max (A, B)
5162      A > B?  A : B    same as max (B, A)
5163      A <= B? A : B    same as min (A, B)
5164      A < B?  A : B    same as min (B, A)
5165
5166      As above, these transformations don't work in the presence
5167      of signed zeros.  For example, if A and B are zeros of
5168      opposite sign, the first two transformations will change
5169      the sign of the result.  In the last four, the original
5170      expressions give different results for (A=+0, B=-0) and
5171      (A=-0, B=+0), but the transformed expressions do not.
5172
5173      The first two transformations are correct if either A or B
5174      is a NaN.  In the first transformation, the condition will
5175      be false, and B will indeed be chosen.  In the case of the
5176      second transformation, the condition A != B will be true,
5177      and A will be chosen.
5178
5179      The conversions to max() and min() are not correct if B is
5180      a number and A is not.  The conditions in the original
5181      expressions will be false, so all four give B.  The min()
5182      and max() versions would give a NaN instead.  */
5183   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5184       && operand_equal_for_comparison_p (arg01, arg2, arg00)
5185       /* Avoid these transformations if the COND_EXPR may be used
5186          as an lvalue in the C++ front-end.  PR c++/19199.  */
5187       && (in_gimple_form
5188           || (strcmp (lang_hooks.name, "GNU C++") != 0
5189               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5190           || ! maybe_lvalue_p (arg1)
5191           || ! maybe_lvalue_p (arg2)))
5192     {
5193       tree comp_op0 = arg00;
5194       tree comp_op1 = arg01;
5195       tree comp_type = TREE_TYPE (comp_op0);
5196
5197       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
5198       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5199         {
5200           comp_type = type;
5201           comp_op0 = arg1;
5202           comp_op1 = arg2;
5203         }
5204
5205       switch (comp_code)
5206         {
5207         case EQ_EXPR:
5208           return pedantic_non_lvalue (fold_convert (type, arg2));
5209         case NE_EXPR:
5210           return pedantic_non_lvalue (fold_convert (type, arg1));
5211         case LE_EXPR:
5212         case LT_EXPR:
5213         case UNLE_EXPR:
5214         case UNLT_EXPR:
5215           /* In C++ a ?: expression can be an lvalue, so put the
5216              operand which will be used if they are equal first
5217              so that we can convert this back to the
5218              corresponding COND_EXPR.  */
5219           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5220             {
5221               comp_op0 = fold_convert (comp_type, comp_op0);
5222               comp_op1 = fold_convert (comp_type, comp_op1);
5223               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5224                     ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
5225                     : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
5226               return pedantic_non_lvalue (fold_convert (type, tem));
5227             }
5228           break;
5229         case GE_EXPR:
5230         case GT_EXPR:
5231         case UNGE_EXPR:
5232         case UNGT_EXPR:
5233           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5234             {
5235               comp_op0 = fold_convert (comp_type, comp_op0);
5236               comp_op1 = fold_convert (comp_type, comp_op1);
5237               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5238                     ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5239                     : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5240               return pedantic_non_lvalue (fold_convert (type, tem));
5241             }
5242           break;
5243         case UNEQ_EXPR:
5244           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5245             return pedantic_non_lvalue (fold_convert (type, arg2));
5246           break;
5247         case LTGT_EXPR:
5248           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5249             return pedantic_non_lvalue (fold_convert (type, arg1));
5250           break;
5251         default:
5252           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5253           break;
5254         }
5255     }
5256
5257   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5258      we might still be able to simplify this.  For example,
5259      if C1 is one less or one more than C2, this might have started
5260      out as a MIN or MAX and been transformed by this function.
5261      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
5262
5263   if (INTEGRAL_TYPE_P (type)
5264       && TREE_CODE (arg01) == INTEGER_CST
5265       && TREE_CODE (arg2) == INTEGER_CST)
5266     switch (comp_code)
5267       {
5268       case EQ_EXPR:
5269         /* We can replace A with C1 in this case.  */
5270         arg1 = fold_convert (type, arg01);
5271         return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5272
5273       case LT_EXPR:
5274         /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5275            MIN_EXPR, to preserve the signedness of the comparison.  */
5276         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5277                                OEP_ONLY_CONST)
5278             && operand_equal_p (arg01,
5279                                 const_binop (PLUS_EXPR, arg2,
5280                                              build_int_cst (type, 1), 0),
5281                                 OEP_ONLY_CONST))
5282           {
5283             tem = fold_build2 (MIN_EXPR, TREE_TYPE (arg00), arg00,
5284                                fold_convert (TREE_TYPE (arg00), arg2));
5285             return pedantic_non_lvalue (fold_convert (type, tem));
5286           }
5287         break;
5288
5289       case LE_EXPR:
5290         /* If C1 is C2 - 1, this is min(A, C2), with the same care
5291            as above.  */
5292         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5293                                OEP_ONLY_CONST)
5294             && operand_equal_p (arg01,
5295                                 const_binop (MINUS_EXPR, arg2,
5296                                              build_int_cst (type, 1), 0),
5297                                 OEP_ONLY_CONST))
5298           {
5299             tem = fold_build2 (MIN_EXPR, TREE_TYPE (arg00), arg00,
5300                                fold_convert (TREE_TYPE (arg00), arg2));
5301             return pedantic_non_lvalue (fold_convert (type, tem));
5302           }
5303         break;
5304
5305       case GT_EXPR:
5306         /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5307            MAX_EXPR, to preserve the signedness of the comparison.  */
5308         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5309                                OEP_ONLY_CONST)
5310             && operand_equal_p (arg01,
5311                                 const_binop (MINUS_EXPR, arg2,
5312                                              build_int_cst (type, 1), 0),
5313                                 OEP_ONLY_CONST))
5314           {
5315             tem = fold_build2 (MAX_EXPR, TREE_TYPE (arg00), arg00,
5316                                fold_convert (TREE_TYPE (arg00), arg2));
5317             return pedantic_non_lvalue (fold_convert (type, tem));
5318           }
5319         break;
5320
5321       case GE_EXPR:
5322         /* If C1 is C2 + 1, this is max(A, C2), with the same care as above.  */
5323         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5324                                OEP_ONLY_CONST)
5325             && operand_equal_p (arg01,
5326                                 const_binop (PLUS_EXPR, arg2,
5327                                              build_int_cst (type, 1), 0),
5328                                 OEP_ONLY_CONST))
5329           {
5330             tem = fold_build2 (MAX_EXPR, TREE_TYPE (arg00), arg00,
5331                                fold_convert (TREE_TYPE (arg00), arg2));
5332             return pedantic_non_lvalue (fold_convert (type, tem));
5333           }
5334         break;
5335       case NE_EXPR:
5336         break;
5337       default:
5338         gcc_unreachable ();
5339       }
5340
5341   return NULL_TREE;
5342 }
5343
5344
5345 \f
5346 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5347 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5348   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5349                 false) >= 2)
5350 #endif
5351
5352 /* EXP is some logical combination of boolean tests.  See if we can
5353    merge it into some range test.  Return the new tree if so.  */
5354
5355 static tree
5356 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5357 {
5358   int or_op = (code == TRUTH_ORIF_EXPR
5359                || code == TRUTH_OR_EXPR);
5360   int in0_p, in1_p, in_p;
5361   tree low0, low1, low, high0, high1, high;
5362   bool strict_overflow_p = false;
5363   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5364   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5365   tree tem;
5366   const char * const warnmsg = G_("assuming signed overflow does not occur "
5367                                   "when simplifying range test");
5368
5369   /* If this is an OR operation, invert both sides; we will invert
5370      again at the end.  */
5371   if (or_op)
5372     in0_p = ! in0_p, in1_p = ! in1_p;
5373
5374   /* If both expressions are the same, if we can merge the ranges, and we
5375      can build the range test, return it or it inverted.  If one of the
5376      ranges is always true or always false, consider it to be the same
5377      expression as the other.  */
5378   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5379       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5380                        in1_p, low1, high1)
5381       && 0 != (tem = (build_range_check (type,
5382                                          lhs != 0 ? lhs
5383                                          : rhs != 0 ? rhs : integer_zero_node,
5384                                          in_p, low, high))))
5385     {
5386       if (strict_overflow_p)
5387         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5388       return or_op ? invert_truthvalue (tem) : tem;
5389     }
5390
5391   /* On machines where the branch cost is expensive, if this is a
5392      short-circuited branch and the underlying object on both sides
5393      is the same, make a non-short-circuit operation.  */
5394   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5395            && lhs != 0 && rhs != 0
5396            && (code == TRUTH_ANDIF_EXPR
5397                || code == TRUTH_ORIF_EXPR)
5398            && operand_equal_p (lhs, rhs, 0))
5399     {
5400       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
5401          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5402          which cases we can't do this.  */
5403       if (simple_operand_p (lhs))
5404         return build2 (code == TRUTH_ANDIF_EXPR
5405                        ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5406                        type, op0, op1);
5407
5408       else if (lang_hooks.decls.global_bindings_p () == 0
5409                && ! CONTAINS_PLACEHOLDER_P (lhs))
5410         {
5411           tree common = save_expr (lhs);
5412
5413           if (0 != (lhs = build_range_check (type, common,
5414                                              or_op ? ! in0_p : in0_p,
5415                                              low0, high0))
5416               && (0 != (rhs = build_range_check (type, common,
5417                                                  or_op ? ! in1_p : in1_p,
5418                                                  low1, high1))))
5419             {
5420               if (strict_overflow_p)
5421                 fold_overflow_warning (warnmsg,
5422                                        WARN_STRICT_OVERFLOW_COMPARISON);
5423               return build2 (code == TRUTH_ANDIF_EXPR
5424                              ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5425                              type, lhs, rhs);
5426             }
5427         }
5428     }
5429
5430   return 0;
5431 }
5432 \f
5433 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5434    bit value.  Arrange things so the extra bits will be set to zero if and
5435    only if C is signed-extended to its full width.  If MASK is nonzero,
5436    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
5437
5438 static tree
5439 unextend (tree c, int p, int unsignedp, tree mask)
5440 {
5441   tree type = TREE_TYPE (c);
5442   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5443   tree temp;
5444
5445   if (p == modesize || unsignedp)
5446     return c;
5447
5448   /* We work by getting just the sign bit into the low-order bit, then
5449      into the high-order bit, then sign-extend.  We then XOR that value
5450      with C.  */
5451   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5452   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5453
5454   /* We must use a signed type in order to get an arithmetic right shift.
5455      However, we must also avoid introducing accidental overflows, so that
5456      a subsequent call to integer_zerop will work.  Hence we must
5457      do the type conversion here.  At this point, the constant is either
5458      zero or one, and the conversion to a signed type can never overflow.
5459      We could get an overflow if this conversion is done anywhere else.  */
5460   if (TYPE_UNSIGNED (type))
5461     temp = fold_convert (signed_type_for (type), temp);
5462
5463   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5464   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5465   if (mask != 0)
5466     temp = const_binop (BIT_AND_EXPR, temp,
5467                         fold_convert (TREE_TYPE (c), mask), 0);
5468   /* If necessary, convert the type back to match the type of C.  */
5469   if (TYPE_UNSIGNED (type))
5470     temp = fold_convert (type, temp);
5471
5472   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5473 }
5474 \f
5475 /* Find ways of folding logical expressions of LHS and RHS:
5476    Try to merge two comparisons to the same innermost item.
5477    Look for range tests like "ch >= '0' && ch <= '9'".
5478    Look for combinations of simple terms on machines with expensive branches
5479    and evaluate the RHS unconditionally.
5480
5481    For example, if we have p->a == 2 && p->b == 4 and we can make an
5482    object large enough to span both A and B, we can do this with a comparison
5483    against the object ANDed with the a mask.
5484
5485    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5486    operations to do this with one comparison.
5487
5488    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5489    function and the one above.
5490
5491    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5492    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5493
5494    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5495    two operands.
5496
5497    We return the simplified tree or 0 if no optimization is possible.  */
5498
5499 static tree
5500 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5501 {
5502   /* If this is the "or" of two comparisons, we can do something if
5503      the comparisons are NE_EXPR.  If this is the "and", we can do something
5504      if the comparisons are EQ_EXPR.  I.e.,
5505         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5506
5507      WANTED_CODE is this operation code.  For single bit fields, we can
5508      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5509      comparison for one-bit fields.  */
5510
5511   enum tree_code wanted_code;
5512   enum tree_code lcode, rcode;
5513   tree ll_arg, lr_arg, rl_arg, rr_arg;
5514   tree ll_inner, lr_inner, rl_inner, rr_inner;
5515   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5516   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5517   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5518   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5519   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5520   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5521   enum machine_mode lnmode, rnmode;
5522   tree ll_mask, lr_mask, rl_mask, rr_mask;
5523   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5524   tree l_const, r_const;
5525   tree lntype, rntype, result;
5526   HOST_WIDE_INT first_bit, end_bit;
5527   int volatilep;
5528   tree orig_lhs = lhs, orig_rhs = rhs;
5529   enum tree_code orig_code = code;
5530
5531   /* Start by getting the comparison codes.  Fail if anything is volatile.
5532      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5533      it were surrounded with a NE_EXPR.  */
5534
5535   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5536     return 0;
5537
5538   lcode = TREE_CODE (lhs);
5539   rcode = TREE_CODE (rhs);
5540
5541   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5542     {
5543       lhs = build2 (NE_EXPR, truth_type, lhs,
5544                     build_int_cst (TREE_TYPE (lhs), 0));
5545       lcode = NE_EXPR;
5546     }
5547
5548   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5549     {
5550       rhs = build2 (NE_EXPR, truth_type, rhs,
5551                     build_int_cst (TREE_TYPE (rhs), 0));
5552       rcode = NE_EXPR;
5553     }
5554
5555   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5556       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5557     return 0;
5558
5559   ll_arg = TREE_OPERAND (lhs, 0);
5560   lr_arg = TREE_OPERAND (lhs, 1);
5561   rl_arg = TREE_OPERAND (rhs, 0);
5562   rr_arg = TREE_OPERAND (rhs, 1);
5563
5564   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5565   if (simple_operand_p (ll_arg)
5566       && simple_operand_p (lr_arg))
5567     {
5568       tree result;
5569       if (operand_equal_p (ll_arg, rl_arg, 0)
5570           && operand_equal_p (lr_arg, rr_arg, 0))
5571         {
5572           result = combine_comparisons (code, lcode, rcode,
5573                                         truth_type, ll_arg, lr_arg);
5574           if (result)
5575             return result;
5576         }
5577       else if (operand_equal_p (ll_arg, rr_arg, 0)
5578                && operand_equal_p (lr_arg, rl_arg, 0))
5579         {
5580           result = combine_comparisons (code, lcode,
5581                                         swap_tree_comparison (rcode),
5582                                         truth_type, ll_arg, lr_arg);
5583           if (result)
5584             return result;
5585         }
5586     }
5587
5588   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5589           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5590
5591   /* If the RHS can be evaluated unconditionally and its operands are
5592      simple, it wins to evaluate the RHS unconditionally on machines
5593      with expensive branches.  In this case, this isn't a comparison
5594      that can be merged.  Avoid doing this if the RHS is a floating-point
5595      comparison since those can trap.  */
5596
5597   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5598                    false) >= 2
5599       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5600       && simple_operand_p (rl_arg)
5601       && simple_operand_p (rr_arg))
5602     {
5603       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5604       if (code == TRUTH_OR_EXPR
5605           && lcode == NE_EXPR && integer_zerop (lr_arg)
5606           && rcode == NE_EXPR && integer_zerop (rr_arg)
5607           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5608           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5609         return build2 (NE_EXPR, truth_type,
5610                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5611                                ll_arg, rl_arg),
5612                        build_int_cst (TREE_TYPE (ll_arg), 0));
5613
5614       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5615       if (code == TRUTH_AND_EXPR
5616           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5617           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5618           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5619           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5620         return build2 (EQ_EXPR, truth_type,
5621                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5622                                ll_arg, rl_arg),
5623                        build_int_cst (TREE_TYPE (ll_arg), 0));
5624
5625       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5626         {
5627           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5628             return build2 (code, truth_type, lhs, rhs);
5629           return NULL_TREE;
5630         }
5631     }
5632
5633   /* See if the comparisons can be merged.  Then get all the parameters for
5634      each side.  */
5635
5636   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5637       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5638     return 0;
5639
5640   volatilep = 0;
5641   ll_inner = decode_field_reference (ll_arg,
5642                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5643                                      &ll_unsignedp, &volatilep, &ll_mask,
5644                                      &ll_and_mask);
5645   lr_inner = decode_field_reference (lr_arg,
5646                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5647                                      &lr_unsignedp, &volatilep, &lr_mask,
5648                                      &lr_and_mask);
5649   rl_inner = decode_field_reference (rl_arg,
5650                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5651                                      &rl_unsignedp, &volatilep, &rl_mask,
5652                                      &rl_and_mask);
5653   rr_inner = decode_field_reference (rr_arg,
5654                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5655                                      &rr_unsignedp, &volatilep, &rr_mask,
5656                                      &rr_and_mask);
5657
5658   /* It must be true that the inner operation on the lhs of each
5659      comparison must be the same if we are to be able to do anything.
5660      Then see if we have constants.  If not, the same must be true for
5661      the rhs's.  */
5662   if (volatilep || ll_inner == 0 || rl_inner == 0
5663       || ! operand_equal_p (ll_inner, rl_inner, 0))
5664     return 0;
5665
5666   if (TREE_CODE (lr_arg) == INTEGER_CST
5667       && TREE_CODE (rr_arg) == INTEGER_CST)
5668     l_const = lr_arg, r_const = rr_arg;
5669   else if (lr_inner == 0 || rr_inner == 0
5670            || ! operand_equal_p (lr_inner, rr_inner, 0))
5671     return 0;
5672   else
5673     l_const = r_const = 0;
5674
5675   /* If either comparison code is not correct for our logical operation,
5676      fail.  However, we can convert a one-bit comparison against zero into
5677      the opposite comparison against that bit being set in the field.  */
5678
5679   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5680   if (lcode != wanted_code)
5681     {
5682       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5683         {
5684           /* Make the left operand unsigned, since we are only interested
5685              in the value of one bit.  Otherwise we are doing the wrong
5686              thing below.  */
5687           ll_unsignedp = 1;
5688           l_const = ll_mask;
5689         }
5690       else
5691         return 0;
5692     }
5693
5694   /* This is analogous to the code for l_const above.  */
5695   if (rcode != wanted_code)
5696     {
5697       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5698         {
5699           rl_unsignedp = 1;
5700           r_const = rl_mask;
5701         }
5702       else
5703         return 0;
5704     }
5705
5706   /* See if we can find a mode that contains both fields being compared on
5707      the left.  If we can't, fail.  Otherwise, update all constants and masks
5708      to be relative to a field of that size.  */
5709   first_bit = MIN (ll_bitpos, rl_bitpos);
5710   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5711   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5712                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5713                           volatilep);
5714   if (lnmode == VOIDmode)
5715     return 0;
5716
5717   lnbitsize = GET_MODE_BITSIZE (lnmode);
5718   lnbitpos = first_bit & ~ (lnbitsize - 1);
5719   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5720   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5721
5722   if (BYTES_BIG_ENDIAN)
5723     {
5724       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5725       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5726     }
5727
5728   ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5729                          size_int (xll_bitpos), 0);
5730   rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5731                          size_int (xrl_bitpos), 0);
5732
5733   if (l_const)
5734     {
5735       l_const = fold_convert (lntype, l_const);
5736       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5737       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5738       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5739                                         fold_build1 (BIT_NOT_EXPR,
5740                                                      lntype, ll_mask),
5741                                         0)))
5742         {
5743           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5744
5745           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5746         }
5747     }
5748   if (r_const)
5749     {
5750       r_const = fold_convert (lntype, r_const);
5751       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5752       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5753       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5754                                         fold_build1 (BIT_NOT_EXPR,
5755                                                      lntype, rl_mask),
5756                                         0)))
5757         {
5758           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5759
5760           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5761         }
5762     }
5763
5764   /* If the right sides are not constant, do the same for it.  Also,
5765      disallow this optimization if a size or signedness mismatch occurs
5766      between the left and right sides.  */
5767   if (l_const == 0)
5768     {
5769       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5770           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5771           /* Make sure the two fields on the right
5772              correspond to the left without being swapped.  */
5773           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5774         return 0;
5775
5776       first_bit = MIN (lr_bitpos, rr_bitpos);
5777       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5778       rnmode = get_best_mode (end_bit - first_bit, first_bit,
5779                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5780                               volatilep);
5781       if (rnmode == VOIDmode)
5782         return 0;
5783
5784       rnbitsize = GET_MODE_BITSIZE (rnmode);
5785       rnbitpos = first_bit & ~ (rnbitsize - 1);
5786       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5787       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5788
5789       if (BYTES_BIG_ENDIAN)
5790         {
5791           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5792           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5793         }
5794
5795       lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
5796                              size_int (xlr_bitpos), 0);
5797       rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
5798                              size_int (xrr_bitpos), 0);
5799
5800       /* Make a mask that corresponds to both fields being compared.
5801          Do this for both items being compared.  If the operands are the
5802          same size and the bits being compared are in the same position
5803          then we can do this by masking both and comparing the masked
5804          results.  */
5805       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5806       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5807       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5808         {
5809           lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5810                                     ll_unsignedp || rl_unsignedp);
5811           if (! all_ones_mask_p (ll_mask, lnbitsize))
5812             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5813
5814           rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
5815                                     lr_unsignedp || rr_unsignedp);
5816           if (! all_ones_mask_p (lr_mask, rnbitsize))
5817             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5818
5819           return build2 (wanted_code, truth_type, lhs, rhs);
5820         }
5821
5822       /* There is still another way we can do something:  If both pairs of
5823          fields being compared are adjacent, we may be able to make a wider
5824          field containing them both.
5825
5826          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5827          the mask must be shifted to account for the shift done by
5828          make_bit_field_ref.  */
5829       if ((ll_bitsize + ll_bitpos == rl_bitpos
5830            && lr_bitsize + lr_bitpos == rr_bitpos)
5831           || (ll_bitpos == rl_bitpos + rl_bitsize
5832               && lr_bitpos == rr_bitpos + rr_bitsize))
5833         {
5834           tree type;
5835
5836           lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
5837                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5838           rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
5839                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5840
5841           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5842                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5843           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5844                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5845
5846           /* Convert to the smaller type before masking out unwanted bits.  */
5847           type = lntype;
5848           if (lntype != rntype)
5849             {
5850               if (lnbitsize > rnbitsize)
5851                 {
5852                   lhs = fold_convert (rntype, lhs);
5853                   ll_mask = fold_convert (rntype, ll_mask);
5854                   type = rntype;
5855                 }
5856               else if (lnbitsize < rnbitsize)
5857                 {
5858                   rhs = fold_convert (lntype, rhs);
5859                   lr_mask = fold_convert (lntype, lr_mask);
5860                   type = lntype;
5861                 }
5862             }
5863
5864           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5865             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5866
5867           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5868             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5869
5870           return build2 (wanted_code, truth_type, lhs, rhs);
5871         }
5872
5873       return 0;
5874     }
5875
5876   /* Handle the case of comparisons with constants.  If there is something in
5877      common between the masks, those bits of the constants must be the same.
5878      If not, the condition is always false.  Test for this to avoid generating
5879      incorrect code below.  */
5880   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5881   if (! integer_zerop (result)
5882       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5883                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5884     {
5885       if (wanted_code == NE_EXPR)
5886         {
5887           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5888           return constant_boolean_node (true, truth_type);
5889         }
5890       else
5891         {
5892           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5893           return constant_boolean_node (false, truth_type);
5894         }
5895     }
5896
5897   /* Construct the expression we will return.  First get the component
5898      reference we will make.  Unless the mask is all ones the width of
5899      that field, perform the mask operation.  Then compare with the
5900      merged constant.  */
5901   result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5902                                ll_unsignedp || rl_unsignedp);
5903
5904   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5905   if (! all_ones_mask_p (ll_mask, lnbitsize))
5906     result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5907
5908   return build2 (wanted_code, truth_type, result,
5909                  const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5910 }
5911 \f
5912 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5913    constant.  */
5914
5915 static tree
5916 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5917 {
5918   tree arg0 = op0;
5919   enum tree_code op_code;
5920   tree comp_const;
5921   tree minmax_const;
5922   int consts_equal, consts_lt;
5923   tree inner;
5924
5925   STRIP_SIGN_NOPS (arg0);
5926
5927   op_code = TREE_CODE (arg0);
5928   minmax_const = TREE_OPERAND (arg0, 1);
5929   comp_const = fold_convert (TREE_TYPE (arg0), op1);
5930   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5931   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5932   inner = TREE_OPERAND (arg0, 0);
5933
5934   /* If something does not permit us to optimize, return the original tree.  */
5935   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5936       || TREE_CODE (comp_const) != INTEGER_CST
5937       || TREE_OVERFLOW (comp_const)
5938       || TREE_CODE (minmax_const) != INTEGER_CST
5939       || TREE_OVERFLOW (minmax_const))
5940     return NULL_TREE;
5941
5942   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5943      and GT_EXPR, doing the rest with recursive calls using logical
5944      simplifications.  */
5945   switch (code)
5946     {
5947     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5948       {
5949         tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5950                                           type, op0, op1);
5951         if (tem)
5952           return invert_truthvalue (tem);
5953         return NULL_TREE;
5954       }
5955
5956     case GE_EXPR:
5957       return
5958         fold_build2 (TRUTH_ORIF_EXPR, type,
5959                      optimize_minmax_comparison
5960                      (EQ_EXPR, type, arg0, comp_const),
5961                      optimize_minmax_comparison
5962                      (GT_EXPR, type, arg0, comp_const));
5963
5964     case EQ_EXPR:
5965       if (op_code == MAX_EXPR && consts_equal)
5966         /* MAX (X, 0) == 0  ->  X <= 0  */
5967         return fold_build2 (LE_EXPR, type, inner, comp_const);
5968
5969       else if (op_code == MAX_EXPR && consts_lt)
5970         /* MAX (X, 0) == 5  ->  X == 5   */
5971         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5972
5973       else if (op_code == MAX_EXPR)
5974         /* MAX (X, 0) == -1  ->  false  */
5975         return omit_one_operand (type, integer_zero_node, inner);
5976
5977       else if (consts_equal)
5978         /* MIN (X, 0) == 0  ->  X >= 0  */
5979         return fold_build2 (GE_EXPR, type, inner, comp_const);
5980
5981       else if (consts_lt)
5982         /* MIN (X, 0) == 5  ->  false  */
5983         return omit_one_operand (type, integer_zero_node, inner);
5984
5985       else
5986         /* MIN (X, 0) == -1  ->  X == -1  */
5987         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5988
5989     case GT_EXPR:
5990       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5991         /* MAX (X, 0) > 0  ->  X > 0
5992            MAX (X, 0) > 5  ->  X > 5  */
5993         return fold_build2 (GT_EXPR, type, inner, comp_const);
5994
5995       else if (op_code == MAX_EXPR)
5996         /* MAX (X, 0) > -1  ->  true  */
5997         return omit_one_operand (type, integer_one_node, inner);
5998
5999       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
6000         /* MIN (X, 0) > 0  ->  false
6001            MIN (X, 0) > 5  ->  false  */
6002         return omit_one_operand (type, integer_zero_node, inner);
6003
6004       else
6005         /* MIN (X, 0) > -1  ->  X > -1  */
6006         return fold_build2 (GT_EXPR, type, inner, comp_const);
6007
6008     default:
6009       return NULL_TREE;
6010     }
6011 }
6012 \f
6013 /* T is an integer expression that is being multiplied, divided, or taken a
6014    modulus (CODE says which and what kind of divide or modulus) by a
6015    constant C.  See if we can eliminate that operation by folding it with
6016    other operations already in T.  WIDE_TYPE, if non-null, is a type that
6017    should be used for the computation if wider than our type.
6018
6019    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6020    (X * 2) + (Y * 4).  We must, however, be assured that either the original
6021    expression would not overflow or that overflow is undefined for the type
6022    in the language in question.
6023
6024    If we return a non-null expression, it is an equivalent form of the
6025    original computation, but need not be in the original type.
6026
6027    We set *STRICT_OVERFLOW_P to true if the return values depends on
6028    signed overflow being undefined.  Otherwise we do not change
6029    *STRICT_OVERFLOW_P.  */
6030
6031 static tree
6032 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6033                 bool *strict_overflow_p)
6034 {
6035   /* To avoid exponential search depth, refuse to allow recursion past
6036      three levels.  Beyond that (1) it's highly unlikely that we'll find
6037      something interesting and (2) we've probably processed it before
6038      when we built the inner expression.  */
6039
6040   static int depth;
6041   tree ret;
6042
6043   if (depth > 3)
6044     return NULL;
6045
6046   depth++;
6047   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6048   depth--;
6049
6050   return ret;
6051 }
6052
6053 static tree
6054 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6055                   bool *strict_overflow_p)
6056 {
6057   tree type = TREE_TYPE (t);
6058   enum tree_code tcode = TREE_CODE (t);
6059   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6060                                    > GET_MODE_SIZE (TYPE_MODE (type)))
6061                 ? wide_type : type);
6062   tree t1, t2;
6063   int same_p = tcode == code;
6064   tree op0 = NULL_TREE, op1 = NULL_TREE;
6065   bool sub_strict_overflow_p;
6066
6067   /* Don't deal with constants of zero here; they confuse the code below.  */
6068   if (integer_zerop (c))
6069     return NULL_TREE;
6070
6071   if (TREE_CODE_CLASS (tcode) == tcc_unary)
6072     op0 = TREE_OPERAND (t, 0);
6073
6074   if (TREE_CODE_CLASS (tcode) == tcc_binary)
6075     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6076
6077   /* Note that we need not handle conditional operations here since fold
6078      already handles those cases.  So just do arithmetic here.  */
6079   switch (tcode)
6080     {
6081     case INTEGER_CST:
6082       /* For a constant, we can always simplify if we are a multiply
6083          or (for divide and modulus) if it is a multiple of our constant.  */
6084       if (code == MULT_EXPR
6085           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6086         return const_binop (code, fold_convert (ctype, t),
6087                             fold_convert (ctype, c), 0);
6088       break;
6089
6090     CASE_CONVERT: case NON_LVALUE_EXPR:
6091       /* If op0 is an expression ...  */
6092       if ((COMPARISON_CLASS_P (op0)
6093            || UNARY_CLASS_P (op0)
6094            || BINARY_CLASS_P (op0)
6095            || VL_EXP_CLASS_P (op0)
6096            || EXPRESSION_CLASS_P (op0))
6097           /* ... and has wrapping overflow, and its type is smaller
6098              than ctype, then we cannot pass through as widening.  */
6099           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6100                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6101                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6102                && (TYPE_PRECISION (ctype)
6103                    > TYPE_PRECISION (TREE_TYPE (op0))))
6104               /* ... or this is a truncation (t is narrower than op0),
6105                  then we cannot pass through this narrowing.  */
6106               || (TYPE_PRECISION (type)
6107                   < TYPE_PRECISION (TREE_TYPE (op0)))
6108               /* ... or signedness changes for division or modulus,
6109                  then we cannot pass through this conversion.  */
6110               || (code != MULT_EXPR
6111                   && (TYPE_UNSIGNED (ctype)
6112                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
6113               /* ... or has undefined overflow while the converted to
6114                  type has not, we cannot do the operation in the inner type
6115                  as that would introduce undefined overflow.  */
6116               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6117                   && !TYPE_OVERFLOW_UNDEFINED (type))))
6118         break;
6119
6120       /* Pass the constant down and see if we can make a simplification.  If
6121          we can, replace this expression with the inner simplification for
6122          possible later conversion to our or some other type.  */
6123       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6124           && TREE_CODE (t2) == INTEGER_CST
6125           && !TREE_OVERFLOW (t2)
6126           && (0 != (t1 = extract_muldiv (op0, t2, code,
6127                                          code == MULT_EXPR
6128                                          ? ctype : NULL_TREE,
6129                                          strict_overflow_p))))
6130         return t1;
6131       break;
6132
6133     case ABS_EXPR:
6134       /* If widening the type changes it from signed to unsigned, then we
6135          must avoid building ABS_EXPR itself as unsigned.  */
6136       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6137         {
6138           tree cstype = (*signed_type_for) (ctype);
6139           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6140               != 0)
6141             {
6142               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6143               return fold_convert (ctype, t1);
6144             }
6145           break;
6146         }
6147       /* If the constant is negative, we cannot simplify this.  */
6148       if (tree_int_cst_sgn (c) == -1)
6149         break;
6150       /* FALLTHROUGH */
6151     case NEGATE_EXPR:
6152       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6153           != 0)
6154         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6155       break;
6156
6157     case MIN_EXPR:  case MAX_EXPR:
6158       /* If widening the type changes the signedness, then we can't perform
6159          this optimization as that changes the result.  */
6160       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6161         break;
6162
6163       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
6164       sub_strict_overflow_p = false;
6165       if ((t1 = extract_muldiv (op0, c, code, wide_type,
6166                                 &sub_strict_overflow_p)) != 0
6167           && (t2 = extract_muldiv (op1, c, code, wide_type,
6168                                    &sub_strict_overflow_p)) != 0)
6169         {
6170           if (tree_int_cst_sgn (c) < 0)
6171             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6172           if (sub_strict_overflow_p)
6173             *strict_overflow_p = true;
6174           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6175                               fold_convert (ctype, t2));
6176         }
6177       break;
6178
6179     case LSHIFT_EXPR:  case RSHIFT_EXPR:
6180       /* If the second operand is constant, this is a multiplication
6181          or floor division, by a power of two, so we can treat it that
6182          way unless the multiplier or divisor overflows.  Signed
6183          left-shift overflow is implementation-defined rather than
6184          undefined in C90, so do not convert signed left shift into
6185          multiplication.  */
6186       if (TREE_CODE (op1) == INTEGER_CST
6187           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6188           /* const_binop may not detect overflow correctly,
6189              so check for it explicitly here.  */
6190           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6191           && TREE_INT_CST_HIGH (op1) == 0
6192           && 0 != (t1 = fold_convert (ctype,
6193                                       const_binop (LSHIFT_EXPR,
6194                                                    size_one_node,
6195                                                    op1, 0)))
6196           && !TREE_OVERFLOW (t1))
6197         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6198                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
6199                                        ctype, fold_convert (ctype, op0), t1),
6200                                c, code, wide_type, strict_overflow_p);
6201       break;
6202
6203     case PLUS_EXPR:  case MINUS_EXPR:
6204       /* See if we can eliminate the operation on both sides.  If we can, we
6205          can return a new PLUS or MINUS.  If we can't, the only remaining
6206          cases where we can do anything are if the second operand is a
6207          constant.  */
6208       sub_strict_overflow_p = false;
6209       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6210       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6211       if (t1 != 0 && t2 != 0
6212           && (code == MULT_EXPR
6213               /* If not multiplication, we can only do this if both operands
6214                  are divisible by c.  */
6215               || (multiple_of_p (ctype, op0, c)
6216                   && multiple_of_p (ctype, op1, c))))
6217         {
6218           if (sub_strict_overflow_p)
6219             *strict_overflow_p = true;
6220           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6221                               fold_convert (ctype, t2));
6222         }
6223
6224       /* If this was a subtraction, negate OP1 and set it to be an addition.
6225          This simplifies the logic below.  */
6226       if (tcode == MINUS_EXPR)
6227         tcode = PLUS_EXPR, op1 = negate_expr (op1);
6228
6229       if (TREE_CODE (op1) != INTEGER_CST)
6230         break;
6231
6232       /* If either OP1 or C are negative, this optimization is not safe for
6233          some of the division and remainder types while for others we need
6234          to change the code.  */
6235       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6236         {
6237           if (code == CEIL_DIV_EXPR)
6238             code = FLOOR_DIV_EXPR;
6239           else if (code == FLOOR_DIV_EXPR)
6240             code = CEIL_DIV_EXPR;
6241           else if (code != MULT_EXPR
6242                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6243             break;
6244         }
6245
6246       /* If it's a multiply or a division/modulus operation of a multiple
6247          of our constant, do the operation and verify it doesn't overflow.  */
6248       if (code == MULT_EXPR
6249           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6250         {
6251           op1 = const_binop (code, fold_convert (ctype, op1),
6252                              fold_convert (ctype, c), 0);
6253           /* We allow the constant to overflow with wrapping semantics.  */
6254           if (op1 == 0
6255               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6256             break;
6257         }
6258       else
6259         break;
6260
6261       /* If we have an unsigned type is not a sizetype, we cannot widen
6262          the operation since it will change the result if the original
6263          computation overflowed.  */
6264       if (TYPE_UNSIGNED (ctype)
6265           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6266           && ctype != type)
6267         break;
6268
6269       /* If we were able to eliminate our operation from the first side,
6270          apply our operation to the second side and reform the PLUS.  */
6271       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6272         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6273
6274       /* The last case is if we are a multiply.  In that case, we can
6275          apply the distributive law to commute the multiply and addition
6276          if the multiplication of the constants doesn't overflow.  */
6277       if (code == MULT_EXPR)
6278         return fold_build2 (tcode, ctype,
6279                             fold_build2 (code, ctype,
6280                                          fold_convert (ctype, op0),
6281                                          fold_convert (ctype, c)),
6282                             op1);
6283
6284       break;
6285
6286     case MULT_EXPR:
6287       /* We have a special case here if we are doing something like
6288          (C * 8) % 4 since we know that's zero.  */
6289       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6290            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6291           /* If the multiplication can overflow we cannot optimize this.
6292              ???  Until we can properly mark individual operations as
6293              not overflowing we need to treat sizetype special here as
6294              stor-layout relies on this opimization to make
6295              DECL_FIELD_BIT_OFFSET always a constant.  */
6296           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6297               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6298                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
6299           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6300           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6301         {
6302           *strict_overflow_p = true;
6303           return omit_one_operand (type, integer_zero_node, op0);
6304         }
6305
6306       /* ... fall through ...  */
6307
6308     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
6309     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
6310       /* If we can extract our operation from the LHS, do so and return a
6311          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
6312          do something only if the second operand is a constant.  */
6313       if (same_p
6314           && (t1 = extract_muldiv (op0, c, code, wide_type,
6315                                    strict_overflow_p)) != 0)
6316         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6317                             fold_convert (ctype, op1));
6318       else if (tcode == MULT_EXPR && code == MULT_EXPR
6319                && (t1 = extract_muldiv (op1, c, code, wide_type,
6320                                         strict_overflow_p)) != 0)
6321         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6322                             fold_convert (ctype, t1));
6323       else if (TREE_CODE (op1) != INTEGER_CST)
6324         return 0;
6325
6326       /* If these are the same operation types, we can associate them
6327          assuming no overflow.  */
6328       if (tcode == code
6329           && 0 != (t1 = int_const_binop (MULT_EXPR, fold_convert (ctype, op1),
6330                                          fold_convert (ctype, c), 1))
6331           && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
6332                                                TREE_INT_CST_HIGH (t1),
6333                                                (TYPE_UNSIGNED (ctype)
6334                                                 && tcode != MULT_EXPR) ? -1 : 1,
6335                                                TREE_OVERFLOW (t1)))
6336           && !TREE_OVERFLOW (t1))
6337         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6338
6339       /* If these operations "cancel" each other, we have the main
6340          optimizations of this pass, which occur when either constant is a
6341          multiple of the other, in which case we replace this with either an
6342          operation or CODE or TCODE.
6343
6344          If we have an unsigned type that is not a sizetype, we cannot do
6345          this since it will change the result if the original computation
6346          overflowed.  */
6347       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6348            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6349           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6350               || (tcode == MULT_EXPR
6351                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6352                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6353                   && code != MULT_EXPR)))
6354         {
6355           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6356             {
6357               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6358                 *strict_overflow_p = true;
6359               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6360                                   fold_convert (ctype,
6361                                                 const_binop (TRUNC_DIV_EXPR,
6362                                                              op1, c, 0)));
6363             }
6364           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6365             {
6366               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6367                 *strict_overflow_p = true;
6368               return fold_build2 (code, ctype, fold_convert (ctype, op0),
6369                                   fold_convert (ctype,
6370                                                 const_binop (TRUNC_DIV_EXPR,
6371                                                              c, op1, 0)));
6372             }
6373         }
6374       break;
6375
6376     default:
6377       break;
6378     }
6379
6380   return 0;
6381 }
6382 \f
6383 /* Return a node which has the indicated constant VALUE (either 0 or
6384    1), and is of the indicated TYPE.  */
6385
6386 tree
6387 constant_boolean_node (int value, tree type)
6388 {
6389   if (type == integer_type_node)
6390     return value ? integer_one_node : integer_zero_node;
6391   else if (type == boolean_type_node)
6392     return value ? boolean_true_node : boolean_false_node;
6393   else
6394     return build_int_cst (type, value);
6395 }
6396
6397
6398 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6399    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6400    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6401    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6402    COND is the first argument to CODE; otherwise (as in the example
6403    given here), it is the second argument.  TYPE is the type of the
6404    original expression.  Return NULL_TREE if no simplification is
6405    possible.  */
6406
6407 static tree
6408 fold_binary_op_with_conditional_arg (enum tree_code code,
6409                                      tree type, tree op0, tree op1,
6410                                      tree cond, tree arg, int cond_first_p)
6411 {
6412   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6413   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6414   tree test, true_value, false_value;
6415   tree lhs = NULL_TREE;
6416   tree rhs = NULL_TREE;
6417
6418   /* This transformation is only worthwhile if we don't have to wrap
6419      arg in a SAVE_EXPR, and the operation can be simplified on at least
6420      one of the branches once its pushed inside the COND_EXPR.  */
6421   if (!TREE_CONSTANT (arg))
6422     return NULL_TREE;
6423
6424   if (TREE_CODE (cond) == COND_EXPR)
6425     {
6426       test = TREE_OPERAND (cond, 0);
6427       true_value = TREE_OPERAND (cond, 1);
6428       false_value = TREE_OPERAND (cond, 2);
6429       /* If this operand throws an expression, then it does not make
6430          sense to try to perform a logical or arithmetic operation
6431          involving it.  */
6432       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6433         lhs = true_value;
6434       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6435         rhs = false_value;
6436     }
6437   else
6438     {
6439       tree testtype = TREE_TYPE (cond);
6440       test = cond;
6441       true_value = constant_boolean_node (true, testtype);
6442       false_value = constant_boolean_node (false, testtype);
6443     }
6444
6445   arg = fold_convert (arg_type, arg);
6446   if (lhs == 0)
6447     {
6448       true_value = fold_convert (cond_type, true_value);
6449       if (cond_first_p)
6450         lhs = fold_build2 (code, type, true_value, arg);
6451       else
6452         lhs = fold_build2 (code, type, arg, true_value);
6453     }
6454   if (rhs == 0)
6455     {
6456       false_value = fold_convert (cond_type, false_value);
6457       if (cond_first_p)
6458         rhs = fold_build2 (code, type, false_value, arg);
6459       else
6460         rhs = fold_build2 (code, type, arg, false_value);
6461     }
6462
6463   test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6464   return fold_convert (type, test);
6465 }
6466
6467 \f
6468 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6469
6470    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6471    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6472    ADDEND is the same as X.
6473
6474    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6475    and finite.  The problematic cases are when X is zero, and its mode
6476    has signed zeros.  In the case of rounding towards -infinity,
6477    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6478    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6479
6480 bool
6481 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6482 {
6483   if (!real_zerop (addend))
6484     return false;
6485
6486   /* Don't allow the fold with -fsignaling-nans.  */
6487   if (HONOR_SNANS (TYPE_MODE (type)))
6488     return false;
6489
6490   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6491   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6492     return true;
6493
6494   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6495   if (TREE_CODE (addend) == REAL_CST
6496       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6497     negate = !negate;
6498
6499   /* The mode has signed zeros, and we have to honor their sign.
6500      In this situation, there is only one case we can return true for.
6501      X - 0 is the same as X unless rounding towards -infinity is
6502      supported.  */
6503   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6504 }
6505
6506 /* Subroutine of fold() that checks comparisons of built-in math
6507    functions against real constants.
6508
6509    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6510    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6511    is the type of the result and ARG0 and ARG1 are the operands of the
6512    comparison.  ARG1 must be a TREE_REAL_CST.
6513
6514    The function returns the constant folded tree if a simplification
6515    can be made, and NULL_TREE otherwise.  */
6516
6517 static tree
6518 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6519                      tree type, tree arg0, tree arg1)
6520 {
6521   REAL_VALUE_TYPE c;
6522
6523   if (BUILTIN_SQRT_P (fcode))
6524     {
6525       tree arg = CALL_EXPR_ARG (arg0, 0);
6526       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6527
6528       c = TREE_REAL_CST (arg1);
6529       if (REAL_VALUE_NEGATIVE (c))
6530         {
6531           /* sqrt(x) < y is always false, if y is negative.  */
6532           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6533             return omit_one_operand (type, integer_zero_node, arg);
6534
6535           /* sqrt(x) > y is always true, if y is negative and we
6536              don't care about NaNs, i.e. negative values of x.  */
6537           if (code == NE_EXPR || !HONOR_NANS (mode))
6538             return omit_one_operand (type, integer_one_node, arg);
6539
6540           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6541           return fold_build2 (GE_EXPR, type, arg,
6542                               build_real (TREE_TYPE (arg), dconst0));
6543         }
6544       else if (code == GT_EXPR || code == GE_EXPR)
6545         {
6546           REAL_VALUE_TYPE c2;
6547
6548           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6549           real_convert (&c2, mode, &c2);
6550
6551           if (REAL_VALUE_ISINF (c2))
6552             {
6553               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6554               if (HONOR_INFINITIES (mode))
6555                 return fold_build2 (EQ_EXPR, type, arg,
6556                                     build_real (TREE_TYPE (arg), c2));
6557
6558               /* sqrt(x) > y is always false, when y is very large
6559                  and we don't care about infinities.  */
6560               return omit_one_operand (type, integer_zero_node, arg);
6561             }
6562
6563           /* sqrt(x) > c is the same as x > c*c.  */
6564           return fold_build2 (code, type, arg,
6565                               build_real (TREE_TYPE (arg), c2));
6566         }
6567       else if (code == LT_EXPR || code == LE_EXPR)
6568         {
6569           REAL_VALUE_TYPE c2;
6570
6571           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6572           real_convert (&c2, mode, &c2);
6573
6574           if (REAL_VALUE_ISINF (c2))
6575             {
6576               /* sqrt(x) < y is always true, when y is a very large
6577                  value and we don't care about NaNs or Infinities.  */
6578               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6579                 return omit_one_operand (type, integer_one_node, arg);
6580
6581               /* sqrt(x) < y is x != +Inf when y is very large and we
6582                  don't care about NaNs.  */
6583               if (! HONOR_NANS (mode))
6584                 return fold_build2 (NE_EXPR, type, arg,
6585                                     build_real (TREE_TYPE (arg), c2));
6586
6587               /* sqrt(x) < y is x >= 0 when y is very large and we
6588                  don't care about Infinities.  */
6589               if (! HONOR_INFINITIES (mode))
6590                 return fold_build2 (GE_EXPR, type, arg,
6591                                     build_real (TREE_TYPE (arg), dconst0));
6592
6593               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6594               if (lang_hooks.decls.global_bindings_p () != 0
6595                   || CONTAINS_PLACEHOLDER_P (arg))
6596                 return NULL_TREE;
6597
6598               arg = save_expr (arg);
6599               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6600                                   fold_build2 (GE_EXPR, type, arg,
6601                                                build_real (TREE_TYPE (arg),
6602                                                            dconst0)),
6603                                   fold_build2 (NE_EXPR, type, arg,
6604                                                build_real (TREE_TYPE (arg),
6605                                                            c2)));
6606             }
6607
6608           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6609           if (! HONOR_NANS (mode))
6610             return fold_build2 (code, type, arg,
6611                                 build_real (TREE_TYPE (arg), c2));
6612
6613           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6614           if (lang_hooks.decls.global_bindings_p () == 0
6615               && ! CONTAINS_PLACEHOLDER_P (arg))
6616             {
6617               arg = save_expr (arg);
6618               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6619                                   fold_build2 (GE_EXPR, type, arg,
6620                                                build_real (TREE_TYPE (arg),
6621                                                            dconst0)),
6622                                   fold_build2 (code, type, arg,
6623                                                build_real (TREE_TYPE (arg),
6624                                                            c2)));
6625             }
6626         }
6627     }
6628
6629   return NULL_TREE;
6630 }
6631
6632 /* Subroutine of fold() that optimizes comparisons against Infinities,
6633    either +Inf or -Inf.
6634
6635    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6636    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6637    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6638
6639    The function returns the constant folded tree if a simplification
6640    can be made, and NULL_TREE otherwise.  */
6641
6642 static tree
6643 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6644 {
6645   enum machine_mode mode;
6646   REAL_VALUE_TYPE max;
6647   tree temp;
6648   bool neg;
6649
6650   mode = TYPE_MODE (TREE_TYPE (arg0));
6651
6652   /* For negative infinity swap the sense of the comparison.  */
6653   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6654   if (neg)
6655     code = swap_tree_comparison (code);
6656
6657   switch (code)
6658     {
6659     case GT_EXPR:
6660       /* x > +Inf is always false, if with ignore sNANs.  */
6661       if (HONOR_SNANS (mode))
6662         return NULL_TREE;
6663       return omit_one_operand (type, integer_zero_node, arg0);
6664
6665     case LE_EXPR:
6666       /* x <= +Inf is always true, if we don't case about NaNs.  */
6667       if (! HONOR_NANS (mode))
6668         return omit_one_operand (type, integer_one_node, arg0);
6669
6670       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6671       if (lang_hooks.decls.global_bindings_p () == 0
6672           && ! CONTAINS_PLACEHOLDER_P (arg0))
6673         {
6674           arg0 = save_expr (arg0);
6675           return fold_build2 (EQ_EXPR, type, arg0, arg0);
6676         }
6677       break;
6678
6679     case EQ_EXPR:
6680     case GE_EXPR:
6681       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6682       real_maxval (&max, neg, mode);
6683       return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6684                           arg0, build_real (TREE_TYPE (arg0), max));
6685
6686     case LT_EXPR:
6687       /* x < +Inf is always equal to x <= DBL_MAX.  */
6688       real_maxval (&max, neg, mode);
6689       return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6690                           arg0, build_real (TREE_TYPE (arg0), max));
6691
6692     case NE_EXPR:
6693       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6694       real_maxval (&max, neg, mode);
6695       if (! HONOR_NANS (mode))
6696         return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6697                             arg0, build_real (TREE_TYPE (arg0), max));
6698
6699       temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6700                           arg0, build_real (TREE_TYPE (arg0), max));
6701       return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6702
6703     default:
6704       break;
6705     }
6706
6707   return NULL_TREE;
6708 }
6709
6710 /* Subroutine of fold() that optimizes comparisons of a division by
6711    a nonzero integer constant against an integer constant, i.e.
6712    X/C1 op C2.
6713
6714    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6715    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6716    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6717
6718    The function returns the constant folded tree if a simplification
6719    can be made, and NULL_TREE otherwise.  */
6720
6721 static tree
6722 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6723 {
6724   tree prod, tmp, hi, lo;
6725   tree arg00 = TREE_OPERAND (arg0, 0);
6726   tree arg01 = TREE_OPERAND (arg0, 1);
6727   unsigned HOST_WIDE_INT lpart;
6728   HOST_WIDE_INT hpart;
6729   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6730   bool neg_overflow;
6731   int overflow;
6732
6733   /* We have to do this the hard way to detect unsigned overflow.
6734      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
6735   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6736                                    TREE_INT_CST_HIGH (arg01),
6737                                    TREE_INT_CST_LOW (arg1),
6738                                    TREE_INT_CST_HIGH (arg1),
6739                                    &lpart, &hpart, unsigned_p);
6740   prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6741                                 -1, overflow);
6742   neg_overflow = false;
6743
6744   if (unsigned_p)
6745     {
6746       tmp = int_const_binop (MINUS_EXPR, arg01,
6747                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6748       lo = prod;
6749
6750       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
6751       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6752                                        TREE_INT_CST_HIGH (prod),
6753                                        TREE_INT_CST_LOW (tmp),
6754                                        TREE_INT_CST_HIGH (tmp),
6755                                        &lpart, &hpart, unsigned_p);
6756       hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6757                                   -1, overflow | TREE_OVERFLOW (prod));
6758     }
6759   else if (tree_int_cst_sgn (arg01) >= 0)
6760     {
6761       tmp = int_const_binop (MINUS_EXPR, arg01,
6762                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6763       switch (tree_int_cst_sgn (arg1))
6764         {
6765         case -1:
6766           neg_overflow = true;
6767           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6768           hi = prod;
6769           break;
6770
6771         case  0:
6772           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6773           hi = tmp;
6774           break;
6775
6776         case  1:
6777           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6778           lo = prod;
6779           break;
6780
6781         default:
6782           gcc_unreachable ();
6783         }
6784     }
6785   else
6786     {
6787       /* A negative divisor reverses the relational operators.  */
6788       code = swap_tree_comparison (code);
6789
6790       tmp = int_const_binop (PLUS_EXPR, arg01,
6791                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6792       switch (tree_int_cst_sgn (arg1))
6793         {
6794         case -1:
6795           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6796           lo = prod;
6797           break;
6798
6799         case  0:
6800           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6801           lo = tmp;
6802           break;
6803
6804         case  1:
6805           neg_overflow = true;
6806           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6807           hi = prod;
6808           break;
6809
6810         default:
6811           gcc_unreachable ();
6812         }
6813     }
6814
6815   switch (code)
6816     {
6817     case EQ_EXPR:
6818       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6819         return omit_one_operand (type, integer_zero_node, arg00);
6820       if (TREE_OVERFLOW (hi))
6821         return fold_build2 (GE_EXPR, type, arg00, lo);
6822       if (TREE_OVERFLOW (lo))
6823         return fold_build2 (LE_EXPR, type, arg00, hi);
6824       return build_range_check (type, arg00, 1, lo, hi);
6825
6826     case NE_EXPR:
6827       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6828         return omit_one_operand (type, integer_one_node, arg00);
6829       if (TREE_OVERFLOW (hi))
6830         return fold_build2 (LT_EXPR, type, arg00, lo);
6831       if (TREE_OVERFLOW (lo))
6832         return fold_build2 (GT_EXPR, type, arg00, hi);
6833       return build_range_check (type, arg00, 0, lo, hi);
6834
6835     case LT_EXPR:
6836       if (TREE_OVERFLOW (lo))
6837         {
6838           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6839           return omit_one_operand (type, tmp, arg00);
6840         }
6841       return fold_build2 (LT_EXPR, type, arg00, lo);
6842
6843     case LE_EXPR:
6844       if (TREE_OVERFLOW (hi))
6845         {
6846           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6847           return omit_one_operand (type, tmp, arg00);
6848         }
6849       return fold_build2 (LE_EXPR, type, arg00, hi);
6850
6851     case GT_EXPR:
6852       if (TREE_OVERFLOW (hi))
6853         {
6854           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6855           return omit_one_operand (type, tmp, arg00);
6856         }
6857       return fold_build2 (GT_EXPR, type, arg00, hi);
6858
6859     case GE_EXPR:
6860       if (TREE_OVERFLOW (lo))
6861         {
6862           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6863           return omit_one_operand (type, tmp, arg00);
6864         }
6865       return fold_build2 (GE_EXPR, type, arg00, lo);
6866
6867     default:
6868       break;
6869     }
6870
6871   return NULL_TREE;
6872 }
6873
6874
6875 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6876    equality/inequality test, then return a simplified form of the test
6877    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6878    result type.  */
6879
6880 static tree
6881 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6882                                      tree result_type)
6883 {
6884   /* If this is testing a single bit, we can optimize the test.  */
6885   if ((code == NE_EXPR || code == EQ_EXPR)
6886       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6887       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6888     {
6889       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6890          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6891       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6892
6893       if (arg00 != NULL_TREE
6894           /* This is only a win if casting to a signed type is cheap,
6895              i.e. when arg00's type is not a partial mode.  */
6896           && TYPE_PRECISION (TREE_TYPE (arg00))
6897              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6898         {
6899           tree stype = signed_type_for (TREE_TYPE (arg00));
6900           return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6901                               result_type, fold_convert (stype, arg00),
6902                               build_int_cst (stype, 0));
6903         }
6904     }
6905
6906   return NULL_TREE;
6907 }
6908
6909 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6910    equality/inequality test, then return a simplified form of
6911    the test using shifts and logical operations.  Otherwise return
6912    NULL.  TYPE is the desired result type.  */
6913
6914 tree
6915 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6916                       tree result_type)
6917 {
6918   /* If this is testing a single bit, we can optimize the test.  */
6919   if ((code == NE_EXPR || code == EQ_EXPR)
6920       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6921       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6922     {
6923       tree inner = TREE_OPERAND (arg0, 0);
6924       tree type = TREE_TYPE (arg0);
6925       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6926       enum machine_mode operand_mode = TYPE_MODE (type);
6927       int ops_unsigned;
6928       tree signed_type, unsigned_type, intermediate_type;
6929       tree tem, one;
6930
6931       /* First, see if we can fold the single bit test into a sign-bit
6932          test.  */
6933       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6934                                                  result_type);
6935       if (tem)
6936         return tem;
6937
6938       /* Otherwise we have (A & C) != 0 where C is a single bit,
6939          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6940          Similarly for (A & C) == 0.  */
6941
6942       /* If INNER is a right shift of a constant and it plus BITNUM does
6943          not overflow, adjust BITNUM and INNER.  */
6944       if (TREE_CODE (inner) == RSHIFT_EXPR
6945           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6946           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6947           && bitnum < TYPE_PRECISION (type)
6948           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6949                                    bitnum - TYPE_PRECISION (type)))
6950         {
6951           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6952           inner = TREE_OPERAND (inner, 0);
6953         }
6954
6955       /* If we are going to be able to omit the AND below, we must do our
6956          operations as unsigned.  If we must use the AND, we have a choice.
6957          Normally unsigned is faster, but for some machines signed is.  */
6958 #ifdef LOAD_EXTEND_OP
6959       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND 
6960                       && !flag_syntax_only) ? 0 : 1;
6961 #else
6962       ops_unsigned = 1;
6963 #endif
6964
6965       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6966       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6967       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6968       inner = fold_convert (intermediate_type, inner);
6969
6970       if (bitnum != 0)
6971         inner = build2 (RSHIFT_EXPR, intermediate_type,
6972                         inner, size_int (bitnum));
6973
6974       one = build_int_cst (intermediate_type, 1);
6975
6976       if (code == EQ_EXPR)
6977         inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6978
6979       /* Put the AND last so it can combine with more things.  */
6980       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6981
6982       /* Make sure to return the proper type.  */
6983       inner = fold_convert (result_type, inner);
6984
6985       return inner;
6986     }
6987   return NULL_TREE;
6988 }
6989
6990 /* Check whether we are allowed to reorder operands arg0 and arg1,
6991    such that the evaluation of arg1 occurs before arg0.  */
6992
6993 static bool
6994 reorder_operands_p (const_tree arg0, const_tree arg1)
6995 {
6996   if (! flag_evaluation_order)
6997       return true;
6998   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6999     return true;
7000   return ! TREE_SIDE_EFFECTS (arg0)
7001          && ! TREE_SIDE_EFFECTS (arg1);
7002 }
7003
7004 /* Test whether it is preferable two swap two operands, ARG0 and
7005    ARG1, for example because ARG0 is an integer constant and ARG1
7006    isn't.  If REORDER is true, only recommend swapping if we can
7007    evaluate the operands in reverse order.  */
7008
7009 bool
7010 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
7011 {
7012   STRIP_SIGN_NOPS (arg0);
7013   STRIP_SIGN_NOPS (arg1);
7014
7015   if (TREE_CODE (arg1) == INTEGER_CST)
7016     return 0;
7017   if (TREE_CODE (arg0) == INTEGER_CST)
7018     return 1;
7019
7020   if (TREE_CODE (arg1) == REAL_CST)
7021     return 0;
7022   if (TREE_CODE (arg0) == REAL_CST)
7023     return 1;
7024
7025   if (TREE_CODE (arg1) == FIXED_CST)
7026     return 0;
7027   if (TREE_CODE (arg0) == FIXED_CST)
7028     return 1;
7029
7030   if (TREE_CODE (arg1) == COMPLEX_CST)
7031     return 0;
7032   if (TREE_CODE (arg0) == COMPLEX_CST)
7033     return 1;
7034
7035   if (TREE_CONSTANT (arg1))
7036     return 0;
7037   if (TREE_CONSTANT (arg0))
7038     return 1;
7039
7040   if (optimize_function_for_size_p (cfun))
7041     return 0;
7042
7043   if (reorder && flag_evaluation_order
7044       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7045     return 0;
7046
7047   /* It is preferable to swap two SSA_NAME to ensure a canonical form
7048      for commutative and comparison operators.  Ensuring a canonical
7049      form allows the optimizers to find additional redundancies without
7050      having to explicitly check for both orderings.  */
7051   if (TREE_CODE (arg0) == SSA_NAME
7052       && TREE_CODE (arg1) == SSA_NAME
7053       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7054     return 1;
7055
7056   /* Put SSA_NAMEs last.  */
7057   if (TREE_CODE (arg1) == SSA_NAME)
7058     return 0;
7059   if (TREE_CODE (arg0) == SSA_NAME)
7060     return 1;
7061
7062   /* Put variables last.  */
7063   if (DECL_P (arg1))
7064     return 0;
7065   if (DECL_P (arg0))
7066     return 1;
7067
7068   return 0;
7069 }
7070
7071 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7072    ARG0 is extended to a wider type.  */
7073
7074 static tree
7075 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
7076 {
7077   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7078   tree arg1_unw;
7079   tree shorter_type, outer_type;
7080   tree min, max;
7081   bool above, below;
7082
7083   if (arg0_unw == arg0)
7084     return NULL_TREE;
7085   shorter_type = TREE_TYPE (arg0_unw);
7086
7087 #ifdef HAVE_canonicalize_funcptr_for_compare
7088   /* Disable this optimization if we're casting a function pointer
7089      type on targets that require function pointer canonicalization.  */
7090   if (HAVE_canonicalize_funcptr_for_compare
7091       && TREE_CODE (shorter_type) == POINTER_TYPE
7092       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7093     return NULL_TREE;
7094 #endif
7095
7096   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7097     return NULL_TREE;
7098
7099   arg1_unw = get_unwidened (arg1, NULL_TREE);
7100
7101   /* If possible, express the comparison in the shorter mode.  */
7102   if ((code == EQ_EXPR || code == NE_EXPR
7103        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7104       && (TREE_TYPE (arg1_unw) == shorter_type
7105           || ((TYPE_PRECISION (shorter_type)
7106                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
7107               && (TYPE_UNSIGNED (shorter_type)
7108                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
7109           || (TREE_CODE (arg1_unw) == INTEGER_CST
7110               && (TREE_CODE (shorter_type) == INTEGER_TYPE
7111                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7112               && int_fits_type_p (arg1_unw, shorter_type))))
7113     return fold_build2 (code, type, arg0_unw,
7114                        fold_convert (shorter_type, arg1_unw));
7115
7116   if (TREE_CODE (arg1_unw) != INTEGER_CST
7117       || TREE_CODE (shorter_type) != INTEGER_TYPE
7118       || !int_fits_type_p (arg1_unw, shorter_type))
7119     return NULL_TREE;
7120
7121   /* If we are comparing with the integer that does not fit into the range
7122      of the shorter type, the result is known.  */
7123   outer_type = TREE_TYPE (arg1_unw);
7124   min = lower_bound_in_type (outer_type, shorter_type);
7125   max = upper_bound_in_type (outer_type, shorter_type);
7126
7127   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7128                                                    max, arg1_unw));
7129   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7130                                                    arg1_unw, min));
7131
7132   switch (code)
7133     {
7134     case EQ_EXPR:
7135       if (above || below)
7136         return omit_one_operand (type, integer_zero_node, arg0);
7137       break;
7138
7139     case NE_EXPR:
7140       if (above || below)
7141         return omit_one_operand (type, integer_one_node, arg0);
7142       break;
7143
7144     case LT_EXPR:
7145     case LE_EXPR:
7146       if (above)
7147         return omit_one_operand (type, integer_one_node, arg0);
7148       else if (below)
7149         return omit_one_operand (type, integer_zero_node, arg0);
7150
7151     case GT_EXPR:
7152     case GE_EXPR:
7153       if (above)
7154         return omit_one_operand (type, integer_zero_node, arg0);
7155       else if (below)
7156         return omit_one_operand (type, integer_one_node, arg0);
7157
7158     default:
7159       break;
7160     }
7161
7162   return NULL_TREE;
7163 }
7164
7165 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7166    ARG0 just the signedness is changed.  */
7167
7168 static tree
7169 fold_sign_changed_comparison (enum tree_code code, tree type,
7170                               tree arg0, tree arg1)
7171 {
7172   tree arg0_inner;
7173   tree inner_type, outer_type;
7174
7175   if (!CONVERT_EXPR_P (arg0))
7176     return NULL_TREE;
7177
7178   outer_type = TREE_TYPE (arg0);
7179   arg0_inner = TREE_OPERAND (arg0, 0);
7180   inner_type = TREE_TYPE (arg0_inner);
7181
7182 #ifdef HAVE_canonicalize_funcptr_for_compare
7183   /* Disable this optimization if we're casting a function pointer
7184      type on targets that require function pointer canonicalization.  */
7185   if (HAVE_canonicalize_funcptr_for_compare
7186       && TREE_CODE (inner_type) == POINTER_TYPE
7187       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7188     return NULL_TREE;
7189 #endif
7190
7191   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7192     return NULL_TREE;
7193
7194   /* If the conversion is from an integral subtype to its basetype
7195      leave it alone.  */
7196   if (TREE_TYPE (inner_type) == outer_type)
7197     return NULL_TREE;
7198
7199   if (TREE_CODE (arg1) != INTEGER_CST
7200       && !(CONVERT_EXPR_P (arg1)
7201            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7202     return NULL_TREE;
7203
7204   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7205        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7206       && code != NE_EXPR
7207       && code != EQ_EXPR)
7208     return NULL_TREE;
7209
7210   if (TREE_CODE (arg1) == INTEGER_CST)
7211     arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7212                                   TREE_INT_CST_HIGH (arg1), 0,
7213                                   TREE_OVERFLOW (arg1));
7214   else
7215     arg1 = fold_convert (inner_type, arg1);
7216
7217   return fold_build2 (code, type, arg0_inner, arg1);
7218 }
7219
7220 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7221    step of the array.  Reconstructs s and delta in the case of s * delta
7222    being an integer constant (and thus already folded).
7223    ADDR is the address. MULT is the multiplicative expression.
7224    If the function succeeds, the new address expression is returned.  Otherwise
7225    NULL_TREE is returned.  */
7226
7227 static tree
7228 try_move_mult_to_index (tree addr, tree op1)
7229 {
7230   tree s, delta, step;
7231   tree ref = TREE_OPERAND (addr, 0), pref;
7232   tree ret, pos;
7233   tree itype;
7234   bool mdim = false;
7235
7236   /*  Strip the nops that might be added when converting op1 to sizetype. */
7237   STRIP_NOPS (op1);
7238
7239   /* Canonicalize op1 into a possibly non-constant delta
7240      and an INTEGER_CST s.  */
7241   if (TREE_CODE (op1) == MULT_EXPR)
7242     {
7243       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7244
7245       STRIP_NOPS (arg0);
7246       STRIP_NOPS (arg1);
7247   
7248       if (TREE_CODE (arg0) == INTEGER_CST)
7249         {
7250           s = arg0;
7251           delta = arg1;
7252         }
7253       else if (TREE_CODE (arg1) == INTEGER_CST)
7254         {
7255           s = arg1;
7256           delta = arg0;
7257         }
7258       else
7259         return NULL_TREE;
7260     }
7261   else if (TREE_CODE (op1) == INTEGER_CST)
7262     {
7263       delta = op1;
7264       s = NULL_TREE;
7265     }
7266   else
7267     {
7268       /* Simulate we are delta * 1.  */
7269       delta = op1;
7270       s = integer_one_node;
7271     }
7272
7273   for (;; ref = TREE_OPERAND (ref, 0))
7274     {
7275       if (TREE_CODE (ref) == ARRAY_REF)
7276         {
7277           /* Remember if this was a multi-dimensional array.  */
7278           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7279             mdim = true;
7280
7281           itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7282           if (! itype)
7283             continue;
7284
7285           step = array_ref_element_size (ref);
7286           if (TREE_CODE (step) != INTEGER_CST)
7287             continue;
7288
7289           if (s)
7290             {
7291               if (! tree_int_cst_equal (step, s))
7292                 continue;
7293             }
7294           else
7295             {
7296               /* Try if delta is a multiple of step.  */
7297               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
7298               if (! tmp)
7299                 continue;
7300               delta = tmp;
7301             }
7302
7303           /* Only fold here if we can verify we do not overflow one
7304              dimension of a multi-dimensional array.  */
7305           if (mdim)
7306             {
7307               tree tmp;
7308
7309               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7310                   || !INTEGRAL_TYPE_P (itype)
7311                   || !TYPE_MAX_VALUE (itype)
7312                   || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
7313                 continue;
7314
7315               tmp = fold_binary (PLUS_EXPR, itype,
7316                                  fold_convert (itype,
7317                                                TREE_OPERAND (ref, 1)),
7318                                  fold_convert (itype, delta));
7319               if (!tmp
7320                   || TREE_CODE (tmp) != INTEGER_CST
7321                   || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
7322                 continue;
7323             }
7324
7325           break;
7326         }
7327       else
7328         mdim = false;
7329
7330       if (!handled_component_p (ref))
7331         return NULL_TREE;
7332     }
7333
7334   /* We found the suitable array reference.  So copy everything up to it,
7335      and replace the index.  */
7336
7337   pref = TREE_OPERAND (addr, 0);
7338   ret = copy_node (pref);
7339   pos = ret;
7340
7341   while (pref != ref)
7342     {
7343       pref = TREE_OPERAND (pref, 0);
7344       TREE_OPERAND (pos, 0) = copy_node (pref);
7345       pos = TREE_OPERAND (pos, 0);
7346     }
7347
7348   TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
7349                                        fold_convert (itype,
7350                                                      TREE_OPERAND (pos, 1)),
7351                                        fold_convert (itype, delta));
7352
7353   return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
7354 }
7355
7356
7357 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
7358    means A >= Y && A != MAX, but in this case we know that
7359    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
7360
7361 static tree
7362 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
7363 {
7364   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7365
7366   if (TREE_CODE (bound) == LT_EXPR)
7367     a = TREE_OPERAND (bound, 0);
7368   else if (TREE_CODE (bound) == GT_EXPR)
7369     a = TREE_OPERAND (bound, 1);
7370   else
7371     return NULL_TREE;
7372
7373   typea = TREE_TYPE (a);
7374   if (!INTEGRAL_TYPE_P (typea)
7375       && !POINTER_TYPE_P (typea))
7376     return NULL_TREE;
7377
7378   if (TREE_CODE (ineq) == LT_EXPR)
7379     {
7380       a1 = TREE_OPERAND (ineq, 1);
7381       y = TREE_OPERAND (ineq, 0);
7382     }
7383   else if (TREE_CODE (ineq) == GT_EXPR)
7384     {
7385       a1 = TREE_OPERAND (ineq, 0);
7386       y = TREE_OPERAND (ineq, 1);
7387     }
7388   else
7389     return NULL_TREE;
7390
7391   if (TREE_TYPE (a1) != typea)
7392     return NULL_TREE;
7393
7394   if (POINTER_TYPE_P (typea))
7395     {
7396       /* Convert the pointer types into integer before taking the difference.  */
7397       tree ta = fold_convert (ssizetype, a);
7398       tree ta1 = fold_convert (ssizetype, a1);
7399       diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7400     }
7401   else
7402    diff = fold_binary (MINUS_EXPR, typea, a1, a);
7403
7404   if (!diff || !integer_onep (diff))
7405    return NULL_TREE;
7406
7407   return fold_build2 (GE_EXPR, type, a, y);
7408 }
7409
7410 /* Fold a sum or difference of at least one multiplication.
7411    Returns the folded tree or NULL if no simplification could be made.  */
7412
7413 static tree
7414 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7415 {
7416   tree arg00, arg01, arg10, arg11;
7417   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7418
7419   /* (A * C) +- (B * C) -> (A+-B) * C.
7420      (A * C) +- A -> A * (C+-1).
7421      We are most concerned about the case where C is a constant,
7422      but other combinations show up during loop reduction.  Since
7423      it is not difficult, try all four possibilities.  */
7424
7425   if (TREE_CODE (arg0) == MULT_EXPR)
7426     {
7427       arg00 = TREE_OPERAND (arg0, 0);
7428       arg01 = TREE_OPERAND (arg0, 1);
7429     }
7430   else if (TREE_CODE (arg0) == INTEGER_CST)
7431     {
7432       arg00 = build_one_cst (type);
7433       arg01 = arg0;
7434     }
7435   else
7436     {
7437       /* We cannot generate constant 1 for fract.  */
7438       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7439         return NULL_TREE;
7440       arg00 = arg0;
7441       arg01 = build_one_cst (type);
7442     }
7443   if (TREE_CODE (arg1) == MULT_EXPR)
7444     {
7445       arg10 = TREE_OPERAND (arg1, 0);
7446       arg11 = TREE_OPERAND (arg1, 1);
7447     }
7448   else if (TREE_CODE (arg1) == INTEGER_CST)
7449     {
7450       arg10 = build_one_cst (type);
7451       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7452          the purpose of this canonicalization.  */
7453       if (TREE_INT_CST_HIGH (arg1) == -1
7454           && negate_expr_p (arg1)
7455           && code == PLUS_EXPR)
7456         {
7457           arg11 = negate_expr (arg1);
7458           code = MINUS_EXPR;
7459         }
7460       else
7461         arg11 = arg1;
7462     }
7463   else
7464     {
7465       /* We cannot generate constant 1 for fract.  */
7466       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7467         return NULL_TREE;
7468       arg10 = arg1;
7469       arg11 = build_one_cst (type);
7470     }
7471   same = NULL_TREE;
7472
7473   if (operand_equal_p (arg01, arg11, 0))
7474     same = arg01, alt0 = arg00, alt1 = arg10;
7475   else if (operand_equal_p (arg00, arg10, 0))
7476     same = arg00, alt0 = arg01, alt1 = arg11;
7477   else if (operand_equal_p (arg00, arg11, 0))
7478     same = arg00, alt0 = arg01, alt1 = arg10;
7479   else if (operand_equal_p (arg01, arg10, 0))
7480     same = arg01, alt0 = arg00, alt1 = arg11;
7481
7482   /* No identical multiplicands; see if we can find a common
7483      power-of-two factor in non-power-of-two multiplies.  This
7484      can help in multi-dimensional array access.  */
7485   else if (host_integerp (arg01, 0)
7486            && host_integerp (arg11, 0))
7487     {
7488       HOST_WIDE_INT int01, int11, tmp;
7489       bool swap = false;
7490       tree maybe_same;
7491       int01 = TREE_INT_CST_LOW (arg01);
7492       int11 = TREE_INT_CST_LOW (arg11);
7493
7494       /* Move min of absolute values to int11.  */
7495       if ((int01 >= 0 ? int01 : -int01)
7496           < (int11 >= 0 ? int11 : -int11))
7497         {
7498           tmp = int01, int01 = int11, int11 = tmp;
7499           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7500           maybe_same = arg01;
7501           swap = true;
7502         }
7503       else
7504         maybe_same = arg11;
7505
7506       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
7507         {
7508           alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7509                               build_int_cst (TREE_TYPE (arg00),
7510                                              int01 / int11));
7511           alt1 = arg10;
7512           same = maybe_same;
7513           if (swap)
7514             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7515         }
7516     }
7517
7518   if (same)
7519     return fold_build2 (MULT_EXPR, type,
7520                         fold_build2 (code, type,
7521                                      fold_convert (type, alt0),
7522                                      fold_convert (type, alt1)),
7523                         fold_convert (type, same));
7524
7525   return NULL_TREE;
7526 }
7527
7528 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7529    specified by EXPR into the buffer PTR of length LEN bytes.
7530    Return the number of bytes placed in the buffer, or zero
7531    upon failure.  */
7532
7533 static int
7534 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7535 {
7536   tree type = TREE_TYPE (expr);
7537   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7538   int byte, offset, word, words;
7539   unsigned char value;
7540
7541   if (total_bytes > len)
7542     return 0;
7543   words = total_bytes / UNITS_PER_WORD;
7544
7545   for (byte = 0; byte < total_bytes; byte++)
7546     {
7547       int bitpos = byte * BITS_PER_UNIT;
7548       if (bitpos < HOST_BITS_PER_WIDE_INT)
7549         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7550       else
7551         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7552                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7553
7554       if (total_bytes > UNITS_PER_WORD)
7555         {
7556           word = byte / UNITS_PER_WORD;
7557           if (WORDS_BIG_ENDIAN)
7558             word = (words - 1) - word;
7559           offset = word * UNITS_PER_WORD;
7560           if (BYTES_BIG_ENDIAN)
7561             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7562           else
7563             offset += byte % UNITS_PER_WORD;
7564         }
7565       else
7566         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7567       ptr[offset] = value;
7568     }
7569   return total_bytes;
7570 }
7571
7572
7573 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7574    specified by EXPR into the buffer PTR of length LEN bytes.
7575    Return the number of bytes placed in the buffer, or zero
7576    upon failure.  */
7577
7578 static int
7579 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7580 {
7581   tree type = TREE_TYPE (expr);
7582   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7583   int byte, offset, word, words, bitpos;
7584   unsigned char value;
7585
7586   /* There are always 32 bits in each long, no matter the size of
7587      the hosts long.  We handle floating point representations with
7588      up to 192 bits.  */
7589   long tmp[6];
7590
7591   if (total_bytes > len)
7592     return 0;
7593   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7594
7595   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7596
7597   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7598        bitpos += BITS_PER_UNIT)
7599     {
7600       byte = (bitpos / BITS_PER_UNIT) & 3;
7601       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7602
7603       if (UNITS_PER_WORD < 4)
7604         {
7605           word = byte / UNITS_PER_WORD;
7606           if (WORDS_BIG_ENDIAN)
7607             word = (words - 1) - word;
7608           offset = word * UNITS_PER_WORD;
7609           if (BYTES_BIG_ENDIAN)
7610             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7611           else
7612             offset += byte % UNITS_PER_WORD;
7613         }
7614       else
7615         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7616       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7617     }
7618   return total_bytes;
7619 }
7620
7621 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7622    specified by EXPR into the buffer PTR of length LEN bytes.
7623    Return the number of bytes placed in the buffer, or zero
7624    upon failure.  */
7625
7626 static int
7627 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7628 {
7629   int rsize, isize;
7630   tree part;
7631
7632   part = TREE_REALPART (expr);
7633   rsize = native_encode_expr (part, ptr, len);
7634   if (rsize == 0)
7635     return 0;
7636   part = TREE_IMAGPART (expr);
7637   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7638   if (isize != rsize)
7639     return 0;
7640   return rsize + isize;
7641 }
7642
7643
7644 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7645    specified by EXPR into the buffer PTR of length LEN bytes.
7646    Return the number of bytes placed in the buffer, or zero
7647    upon failure.  */
7648
7649 static int
7650 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7651 {
7652   int i, size, offset, count;
7653   tree itype, elem, elements;
7654
7655   offset = 0;
7656   elements = TREE_VECTOR_CST_ELTS (expr);
7657   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7658   itype = TREE_TYPE (TREE_TYPE (expr));
7659   size = GET_MODE_SIZE (TYPE_MODE (itype));
7660   for (i = 0; i < count; i++)
7661     {
7662       if (elements)
7663         {
7664           elem = TREE_VALUE (elements);
7665           elements = TREE_CHAIN (elements);
7666         }
7667       else
7668         elem = NULL_TREE;
7669
7670       if (elem)
7671         {
7672           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7673             return 0;
7674         }
7675       else
7676         {
7677           if (offset + size > len)
7678             return 0;
7679           memset (ptr+offset, 0, size);
7680         }
7681       offset += size;
7682     }
7683   return offset;
7684 }
7685
7686
7687 /* Subroutine of native_encode_expr.  Encode the STRING_CST
7688    specified by EXPR into the buffer PTR of length LEN bytes.
7689    Return the number of bytes placed in the buffer, or zero
7690    upon failure.  */
7691
7692 static int
7693 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7694 {
7695   tree type = TREE_TYPE (expr);
7696   HOST_WIDE_INT total_bytes;
7697
7698   if (TREE_CODE (type) != ARRAY_TYPE
7699       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7700       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7701       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7702     return 0;
7703   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7704   if (total_bytes > len)
7705     return 0;
7706   if (TREE_STRING_LENGTH (expr) < total_bytes)
7707     {
7708       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7709       memset (ptr + TREE_STRING_LENGTH (expr), 0,
7710               total_bytes - TREE_STRING_LENGTH (expr));
7711     }
7712   else
7713     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7714   return total_bytes;
7715 }
7716
7717
7718 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7719    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7720    buffer PTR of length LEN bytes.  Return the number of bytes
7721    placed in the buffer, or zero upon failure.  */
7722
7723 int
7724 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7725 {
7726   switch (TREE_CODE (expr))
7727     {
7728     case INTEGER_CST:
7729       return native_encode_int (expr, ptr, len);
7730
7731     case REAL_CST:
7732       return native_encode_real (expr, ptr, len);
7733
7734     case COMPLEX_CST:
7735       return native_encode_complex (expr, ptr, len);
7736
7737     case VECTOR_CST:
7738       return native_encode_vector (expr, ptr, len);
7739
7740     case STRING_CST:
7741       return native_encode_string (expr, ptr, len);
7742
7743     default:
7744       return 0;
7745     }
7746 }
7747
7748
7749 /* Subroutine of native_interpret_expr.  Interpret the contents of
7750    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7751    If the buffer cannot be interpreted, return NULL_TREE.  */
7752
7753 static tree
7754 native_interpret_int (tree type, const unsigned char *ptr, int len)
7755 {
7756   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7757   int byte, offset, word, words;
7758   unsigned char value;
7759   unsigned int HOST_WIDE_INT lo = 0;
7760   HOST_WIDE_INT hi = 0;
7761
7762   if (total_bytes > len)
7763     return NULL_TREE;
7764   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7765     return NULL_TREE;
7766   words = total_bytes / UNITS_PER_WORD;
7767
7768   for (byte = 0; byte < total_bytes; byte++)
7769     {
7770       int bitpos = byte * BITS_PER_UNIT;
7771       if (total_bytes > UNITS_PER_WORD)
7772         {
7773           word = byte / UNITS_PER_WORD;
7774           if (WORDS_BIG_ENDIAN)
7775             word = (words - 1) - word;
7776           offset = word * UNITS_PER_WORD;
7777           if (BYTES_BIG_ENDIAN)
7778             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7779           else
7780             offset += byte % UNITS_PER_WORD;
7781         }
7782       else
7783         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7784       value = ptr[offset];
7785
7786       if (bitpos < HOST_BITS_PER_WIDE_INT)
7787         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7788       else
7789         hi |= (unsigned HOST_WIDE_INT) value
7790               << (bitpos - HOST_BITS_PER_WIDE_INT);
7791     }
7792
7793   return build_int_cst_wide_type (type, lo, hi);
7794 }
7795
7796
7797 /* Subroutine of native_interpret_expr.  Interpret the contents of
7798    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7799    If the buffer cannot be interpreted, return NULL_TREE.  */
7800
7801 static tree
7802 native_interpret_real (tree type, const unsigned char *ptr, int len)
7803 {
7804   enum machine_mode mode = TYPE_MODE (type);
7805   int total_bytes = GET_MODE_SIZE (mode);
7806   int byte, offset, word, words, bitpos;
7807   unsigned char value;
7808   /* There are always 32 bits in each long, no matter the size of
7809      the hosts long.  We handle floating point representations with
7810      up to 192 bits.  */
7811   REAL_VALUE_TYPE r;
7812   long tmp[6];
7813
7814   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7815   if (total_bytes > len || total_bytes > 24)
7816     return NULL_TREE;
7817   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7818
7819   memset (tmp, 0, sizeof (tmp));
7820   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7821        bitpos += BITS_PER_UNIT)
7822     {
7823       byte = (bitpos / BITS_PER_UNIT) & 3;
7824       if (UNITS_PER_WORD < 4)
7825         {
7826           word = byte / UNITS_PER_WORD;
7827           if (WORDS_BIG_ENDIAN)
7828             word = (words - 1) - word;
7829           offset = word * UNITS_PER_WORD;
7830           if (BYTES_BIG_ENDIAN)
7831             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7832           else
7833             offset += byte % UNITS_PER_WORD;
7834         }
7835       else
7836         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7837       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7838
7839       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7840     }
7841
7842   real_from_target (&r, tmp, mode);
7843   return build_real (type, r);
7844 }
7845
7846
7847 /* Subroutine of native_interpret_expr.  Interpret the contents of
7848    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7849    If the buffer cannot be interpreted, return NULL_TREE.  */
7850
7851 static tree
7852 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7853 {
7854   tree etype, rpart, ipart;
7855   int size;
7856
7857   etype = TREE_TYPE (type);
7858   size = GET_MODE_SIZE (TYPE_MODE (etype));
7859   if (size * 2 > len)
7860     return NULL_TREE;
7861   rpart = native_interpret_expr (etype, ptr, size);
7862   if (!rpart)
7863     return NULL_TREE;
7864   ipart = native_interpret_expr (etype, ptr+size, size);
7865   if (!ipart)
7866     return NULL_TREE;
7867   return build_complex (type, rpart, ipart);
7868 }
7869
7870
7871 /* Subroutine of native_interpret_expr.  Interpret the contents of
7872    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7873    If the buffer cannot be interpreted, return NULL_TREE.  */
7874
7875 static tree
7876 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7877 {
7878   tree etype, elem, elements;
7879   int i, size, count;
7880
7881   etype = TREE_TYPE (type);
7882   size = GET_MODE_SIZE (TYPE_MODE (etype));
7883   count = TYPE_VECTOR_SUBPARTS (type);
7884   if (size * count > len)
7885     return NULL_TREE;
7886
7887   elements = NULL_TREE;
7888   for (i = count - 1; i >= 0; i--)
7889     {
7890       elem = native_interpret_expr (etype, ptr+(i*size), size);
7891       if (!elem)
7892         return NULL_TREE;
7893       elements = tree_cons (NULL_TREE, elem, elements);
7894     }
7895   return build_vector (type, elements);
7896 }
7897
7898
7899 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7900    the buffer PTR of length LEN as a constant of type TYPE.  For
7901    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7902    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7903    return NULL_TREE.  */
7904
7905 tree
7906 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7907 {
7908   switch (TREE_CODE (type))
7909     {
7910     case INTEGER_TYPE:
7911     case ENUMERAL_TYPE:
7912     case BOOLEAN_TYPE:
7913       return native_interpret_int (type, ptr, len);
7914
7915     case REAL_TYPE:
7916       return native_interpret_real (type, ptr, len);
7917
7918     case COMPLEX_TYPE:
7919       return native_interpret_complex (type, ptr, len);
7920
7921     case VECTOR_TYPE:
7922       return native_interpret_vector (type, ptr, len);
7923
7924     default:
7925       return NULL_TREE;
7926     }
7927 }
7928
7929
7930 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7931    TYPE at compile-time.  If we're unable to perform the conversion
7932    return NULL_TREE.  */
7933
7934 static tree
7935 fold_view_convert_expr (tree type, tree expr)
7936 {
7937   /* We support up to 512-bit values (for V8DFmode).  */
7938   unsigned char buffer[64];
7939   int len;
7940
7941   /* Check that the host and target are sane.  */
7942   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7943     return NULL_TREE;
7944
7945   len = native_encode_expr (expr, buffer, sizeof (buffer));
7946   if (len == 0)
7947     return NULL_TREE;
7948
7949   return native_interpret_expr (type, buffer, len);
7950 }
7951
7952 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7953    to avoid confusing the gimplify process.  When IN_FOLD is true
7954    avoid modifications of T.  */
7955
7956 static tree
7957 build_fold_addr_expr_with_type_1 (tree t, tree ptrtype, bool in_fold)
7958 {
7959   /* The size of the object is not relevant when talking about its address.  */
7960   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7961     t = TREE_OPERAND (t, 0);
7962
7963   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7964   if (TREE_CODE (t) == INDIRECT_REF
7965       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7966     {
7967       t = TREE_OPERAND (t, 0);
7968
7969       if (TREE_TYPE (t) != ptrtype)
7970         t = build1 (NOP_EXPR, ptrtype, t);
7971     }
7972   else if (!in_fold)
7973     {
7974       tree base = t;
7975
7976       while (handled_component_p (base))
7977         base = TREE_OPERAND (base, 0);
7978
7979       if (DECL_P (base))
7980         TREE_ADDRESSABLE (base) = 1;
7981
7982       t = build1 (ADDR_EXPR, ptrtype, t);
7983     }
7984   else
7985     t = build1 (ADDR_EXPR, ptrtype, t);
7986
7987   return t;
7988 }
7989
7990 /* Build an expression for the address of T with type PTRTYPE.  This
7991    function modifies the input parameter 'T' by sometimes setting the
7992    TREE_ADDRESSABLE flag.  */
7993
7994 tree
7995 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7996 {
7997   return build_fold_addr_expr_with_type_1 (t, ptrtype, false);
7998 }
7999
8000 /* Build an expression for the address of T.  This function modifies
8001    the input parameter 'T' by sometimes setting the TREE_ADDRESSABLE
8002    flag.  When called from fold functions, use fold_addr_expr instead.  */
8003
8004 tree
8005 build_fold_addr_expr (tree t)
8006 {
8007   return build_fold_addr_expr_with_type_1 (t, 
8008                                            build_pointer_type (TREE_TYPE (t)),
8009                                            false);
8010 }
8011
8012 /* Same as build_fold_addr_expr, builds an expression for the address
8013    of T, but avoids touching the input node 't'.  Fold functions
8014    should use this version.  */
8015
8016 static tree
8017 fold_addr_expr (tree t)
8018 {
8019   tree ptrtype = build_pointer_type (TREE_TYPE (t));
8020
8021   return build_fold_addr_expr_with_type_1 (t, ptrtype, true);
8022 }
8023
8024 /* Fold a unary expression of code CODE and type TYPE with operand
8025    OP0.  Return the folded expression if folding is successful.
8026    Otherwise, return NULL_TREE.  */
8027
8028 tree
8029 fold_unary (enum tree_code code, tree type, tree op0)
8030 {
8031   tree tem;
8032   tree arg0;
8033   enum tree_code_class kind = TREE_CODE_CLASS (code);
8034
8035   gcc_assert (IS_EXPR_CODE_CLASS (kind)
8036               && TREE_CODE_LENGTH (code) == 1);
8037
8038   arg0 = op0;
8039   if (arg0)
8040     {
8041       if (CONVERT_EXPR_CODE_P (code)
8042           || code == FLOAT_EXPR || code == ABS_EXPR)
8043         {
8044           /* Don't use STRIP_NOPS, because signedness of argument type
8045              matters.  */
8046           STRIP_SIGN_NOPS (arg0);
8047         }
8048       else
8049         {
8050           /* Strip any conversions that don't change the mode.  This
8051              is safe for every expression, except for a comparison
8052              expression because its signedness is derived from its
8053              operands.
8054
8055              Note that this is done as an internal manipulation within
8056              the constant folder, in order to find the simplest
8057              representation of the arguments so that their form can be
8058              studied.  In any cases, the appropriate type conversions
8059              should be put back in the tree that will get out of the
8060              constant folder.  */
8061           STRIP_NOPS (arg0);
8062         }
8063     }
8064
8065   if (TREE_CODE_CLASS (code) == tcc_unary)
8066     {
8067       if (TREE_CODE (arg0) == COMPOUND_EXPR)
8068         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
8069                        fold_build1 (code, type,
8070                                     fold_convert (TREE_TYPE (op0),
8071                                                   TREE_OPERAND (arg0, 1))));
8072       else if (TREE_CODE (arg0) == COND_EXPR)
8073         {
8074           tree arg01 = TREE_OPERAND (arg0, 1);
8075           tree arg02 = TREE_OPERAND (arg0, 2);
8076           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8077             arg01 = fold_build1 (code, type,
8078                                  fold_convert (TREE_TYPE (op0), arg01));
8079           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8080             arg02 = fold_build1 (code, type,
8081                                  fold_convert (TREE_TYPE (op0), arg02));
8082           tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
8083                              arg01, arg02);
8084
8085           /* If this was a conversion, and all we did was to move into
8086              inside the COND_EXPR, bring it back out.  But leave it if
8087              it is a conversion from integer to integer and the
8088              result precision is no wider than a word since such a
8089              conversion is cheap and may be optimized away by combine,
8090              while it couldn't if it were outside the COND_EXPR.  Then return
8091              so we don't get into an infinite recursion loop taking the
8092              conversion out and then back in.  */
8093
8094           if ((CONVERT_EXPR_CODE_P (code)
8095                || code == NON_LVALUE_EXPR)
8096               && TREE_CODE (tem) == COND_EXPR
8097               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8098               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8099               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8100               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8101               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8102                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8103               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8104                      && (INTEGRAL_TYPE_P
8105                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8106                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8107                   || flag_syntax_only))
8108             tem = build1 (code, type,
8109                           build3 (COND_EXPR,
8110                                   TREE_TYPE (TREE_OPERAND
8111                                              (TREE_OPERAND (tem, 1), 0)),
8112                                   TREE_OPERAND (tem, 0),
8113                                   TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8114                                   TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8115           return tem;
8116         }
8117       else if (COMPARISON_CLASS_P (arg0))
8118         {
8119           if (TREE_CODE (type) == BOOLEAN_TYPE)
8120             {
8121               arg0 = copy_node (arg0);
8122               TREE_TYPE (arg0) = type;
8123               return arg0;
8124             }
8125           else if (TREE_CODE (type) != INTEGER_TYPE)
8126             return fold_build3 (COND_EXPR, type, arg0,
8127                                 fold_build1 (code, type,
8128                                              integer_one_node),
8129                                 fold_build1 (code, type,
8130                                              integer_zero_node));
8131         }
8132    }
8133
8134   switch (code)
8135     {
8136     case PAREN_EXPR:
8137       /* Re-association barriers around constants and other re-association
8138          barriers can be removed.  */
8139       if (CONSTANT_CLASS_P (op0)
8140           || TREE_CODE (op0) == PAREN_EXPR)
8141         return fold_convert (type, op0);
8142       return NULL_TREE;
8143
8144     CASE_CONVERT:
8145     case FLOAT_EXPR:
8146     case FIX_TRUNC_EXPR:
8147       if (TREE_TYPE (op0) == type)
8148         return op0;
8149       
8150       /* If we have (type) (a CMP b) and type is an integral type, return
8151          new expression involving the new type.  */
8152       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8153         return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8154                             TREE_OPERAND (op0, 1));
8155
8156       /* Handle cases of two conversions in a row.  */
8157       if (CONVERT_EXPR_P (op0))
8158         {
8159           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8160           tree inter_type = TREE_TYPE (op0);
8161           int inside_int = INTEGRAL_TYPE_P (inside_type);
8162           int inside_ptr = POINTER_TYPE_P (inside_type);
8163           int inside_float = FLOAT_TYPE_P (inside_type);
8164           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8165           unsigned int inside_prec = TYPE_PRECISION (inside_type);
8166           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8167           int inter_int = INTEGRAL_TYPE_P (inter_type);
8168           int inter_ptr = POINTER_TYPE_P (inter_type);
8169           int inter_float = FLOAT_TYPE_P (inter_type);
8170           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8171           unsigned int inter_prec = TYPE_PRECISION (inter_type);
8172           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8173           int final_int = INTEGRAL_TYPE_P (type);
8174           int final_ptr = POINTER_TYPE_P (type);
8175           int final_float = FLOAT_TYPE_P (type);
8176           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8177           unsigned int final_prec = TYPE_PRECISION (type);
8178           int final_unsignedp = TYPE_UNSIGNED (type);
8179
8180           /* In addition to the cases of two conversions in a row
8181              handled below, if we are converting something to its own
8182              type via an object of identical or wider precision, neither
8183              conversion is needed.  */
8184           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8185               && (((inter_int || inter_ptr) && final_int)
8186                   || (inter_float && final_float))
8187               && inter_prec >= final_prec)
8188             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8189
8190           /* Likewise, if the intermediate and initial types are either both
8191              float or both integer, we don't need the middle conversion if the
8192              former is wider than the latter and doesn't change the signedness
8193              (for integers).  Avoid this if the final type is a pointer since
8194              then we sometimes need the middle conversion.  Likewise if the
8195              final type has a precision not equal to the size of its mode.  */
8196           if (((inter_int && inside_int)
8197                || (inter_float && inside_float)
8198                || (inter_vec && inside_vec))
8199               && inter_prec >= inside_prec
8200               && (inter_float || inter_vec
8201                   || inter_unsignedp == inside_unsignedp)
8202               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8203                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
8204               && ! final_ptr
8205               && (! final_vec || inter_prec == inside_prec))
8206             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8207
8208           /* If we have a sign-extension of a zero-extended value, we can
8209              replace that by a single zero-extension.  */
8210           if (inside_int && inter_int && final_int
8211               && inside_prec < inter_prec && inter_prec < final_prec
8212               && inside_unsignedp && !inter_unsignedp)
8213             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8214
8215           /* Two conversions in a row are not needed unless:
8216              - some conversion is floating-point (overstrict for now), or
8217              - some conversion is a vector (overstrict for now), or
8218              - the intermediate type is narrower than both initial and
8219                final, or
8220              - the intermediate type and innermost type differ in signedness,
8221                and the outermost type is wider than the intermediate, or
8222              - the initial type is a pointer type and the precisions of the
8223                intermediate and final types differ, or
8224              - the final type is a pointer type and the precisions of the
8225                initial and intermediate types differ.  */
8226           if (! inside_float && ! inter_float && ! final_float
8227               && ! inside_vec && ! inter_vec && ! final_vec
8228               && (inter_prec >= inside_prec || inter_prec >= final_prec)
8229               && ! (inside_int && inter_int
8230                     && inter_unsignedp != inside_unsignedp
8231                     && inter_prec < final_prec)
8232               && ((inter_unsignedp && inter_prec > inside_prec)
8233                   == (final_unsignedp && final_prec > inter_prec))
8234               && ! (inside_ptr && inter_prec != final_prec)
8235               && ! (final_ptr && inside_prec != inter_prec)
8236               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8237                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
8238             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8239         }
8240
8241       /* Handle (T *)&A.B.C for A being of type T and B and C
8242          living at offset zero.  This occurs frequently in
8243          C++ upcasting and then accessing the base.  */
8244       if (TREE_CODE (op0) == ADDR_EXPR
8245           && POINTER_TYPE_P (type)
8246           && handled_component_p (TREE_OPERAND (op0, 0)))
8247         {
8248           HOST_WIDE_INT bitsize, bitpos;
8249           tree offset;
8250           enum machine_mode mode;
8251           int unsignedp, volatilep;
8252           tree base = TREE_OPERAND (op0, 0);
8253           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8254                                       &mode, &unsignedp, &volatilep, false);
8255           /* If the reference was to a (constant) zero offset, we can use
8256              the address of the base if it has the same base type
8257              as the result type.  */
8258           if (! offset && bitpos == 0
8259               && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8260                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8261             return fold_convert (type, fold_addr_expr (base));
8262         }
8263
8264       if (TREE_CODE (op0) == MODIFY_EXPR
8265           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
8266           /* Detect assigning a bitfield.  */
8267           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
8268                && DECL_BIT_FIELD
8269                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
8270         {
8271           /* Don't leave an assignment inside a conversion
8272              unless assigning a bitfield.  */
8273           tem = fold_build1 (code, type, TREE_OPERAND (op0, 1));
8274           /* First do the assignment, then return converted constant.  */
8275           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8276           TREE_NO_WARNING (tem) = 1;
8277           TREE_USED (tem) = 1;
8278           return tem;
8279         }
8280
8281       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8282          constants (if x has signed type, the sign bit cannot be set
8283          in c).  This folds extension into the BIT_AND_EXPR.
8284          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8285          very likely don't have maximal range for their precision and this
8286          transformation effectively doesn't preserve non-maximal ranges.  */
8287       if (TREE_CODE (type) == INTEGER_TYPE
8288           && TREE_CODE (op0) == BIT_AND_EXPR
8289           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST
8290           /* Not if the conversion is to the sub-type.  */
8291           && TREE_TYPE (type) != TREE_TYPE (op0))
8292         {
8293           tree and = op0;
8294           tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
8295           int change = 0;
8296
8297           if (TYPE_UNSIGNED (TREE_TYPE (and))
8298               || (TYPE_PRECISION (type)
8299                   <= TYPE_PRECISION (TREE_TYPE (and))))
8300             change = 1;
8301           else if (TYPE_PRECISION (TREE_TYPE (and1))
8302                    <= HOST_BITS_PER_WIDE_INT
8303                    && host_integerp (and1, 1))
8304             {
8305               unsigned HOST_WIDE_INT cst;
8306
8307               cst = tree_low_cst (and1, 1);
8308               cst &= (HOST_WIDE_INT) -1
8309                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8310               change = (cst == 0);
8311 #ifdef LOAD_EXTEND_OP
8312               if (change
8313                   && !flag_syntax_only
8314                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8315                       == ZERO_EXTEND))
8316                 {
8317                   tree uns = unsigned_type_for (TREE_TYPE (and0));
8318                   and0 = fold_convert (uns, and0);
8319                   and1 = fold_convert (uns, and1);
8320                 }
8321 #endif
8322             }
8323           if (change)
8324             {
8325               tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8326                                            TREE_INT_CST_HIGH (and1), 0,
8327                                            TREE_OVERFLOW (and1));
8328               return fold_build2 (BIT_AND_EXPR, type,
8329                                   fold_convert (type, and0), tem);
8330             }
8331         }
8332
8333       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8334          when one of the new casts will fold away. Conservatively we assume
8335          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8336       if (POINTER_TYPE_P (type)
8337           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8338           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8339               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8340               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8341         {
8342           tree arg00 = TREE_OPERAND (arg0, 0);
8343           tree arg01 = TREE_OPERAND (arg0, 1);
8344
8345           return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
8346                               fold_convert (sizetype, arg01));
8347         }
8348
8349       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8350          of the same precision, and X is an integer type not narrower than
8351          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
8352       if (INTEGRAL_TYPE_P (type)
8353           && TREE_CODE (op0) == BIT_NOT_EXPR
8354           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8355           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8356           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8357         {
8358           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8359           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8360               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8361             return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
8362         }
8363
8364       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8365          type of X and Y (integer types only).  */
8366       if (INTEGRAL_TYPE_P (type)
8367           && TREE_CODE (op0) == MULT_EXPR
8368           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8369           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8370         {
8371           /* Be careful not to introduce new overflows.  */
8372           tree mult_type;
8373           if (TYPE_OVERFLOW_WRAPS (type))
8374             mult_type = type;
8375           else
8376             mult_type = unsigned_type_for (type);
8377
8378           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8379             {
8380               tem = fold_build2 (MULT_EXPR, mult_type,
8381                                  fold_convert (mult_type,
8382                                                TREE_OPERAND (op0, 0)),
8383                                  fold_convert (mult_type,
8384                                                TREE_OPERAND (op0, 1)));
8385               return fold_convert (type, tem);
8386             }
8387         }
8388
8389       tem = fold_convert_const (code, type, op0);
8390       return tem ? tem : NULL_TREE;
8391
8392     case FIXED_CONVERT_EXPR:
8393       tem = fold_convert_const (code, type, arg0);
8394       return tem ? tem : NULL_TREE;
8395
8396     case VIEW_CONVERT_EXPR:
8397       if (TREE_TYPE (op0) == type)
8398         return op0;
8399       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8400         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8401
8402       /* For integral conversions with the same precision or pointer
8403          conversions use a NOP_EXPR instead.  */
8404       if ((INTEGRAL_TYPE_P (type)
8405            || POINTER_TYPE_P (type))
8406           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8407               || POINTER_TYPE_P (TREE_TYPE (op0)))
8408           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))
8409           /* Do not muck with VIEW_CONVERT_EXPRs that convert from
8410              a sub-type to its base type as generated by the Ada FE.  */
8411           && !(INTEGRAL_TYPE_P (TREE_TYPE (op0))
8412                && TREE_TYPE (TREE_TYPE (op0))))
8413         return fold_convert (type, op0);
8414
8415       /* Strip inner integral conversions that do not change the precision.  */
8416       if (CONVERT_EXPR_P (op0)
8417           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8418               || POINTER_TYPE_P (TREE_TYPE (op0)))
8419           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8420               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8421           && (TYPE_PRECISION (TREE_TYPE (op0))
8422               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8423         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8424
8425       return fold_view_convert_expr (type, op0);
8426
8427     case NEGATE_EXPR:
8428       tem = fold_negate_expr (arg0);
8429       if (tem)
8430         return fold_convert (type, tem);
8431       return NULL_TREE;
8432
8433     case ABS_EXPR:
8434       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8435         return fold_abs_const (arg0, type);
8436       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8437         return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8438       /* Convert fabs((double)float) into (double)fabsf(float).  */
8439       else if (TREE_CODE (arg0) == NOP_EXPR
8440                && TREE_CODE (type) == REAL_TYPE)
8441         {
8442           tree targ0 = strip_float_extensions (arg0);
8443           if (targ0 != arg0)
8444             return fold_convert (type, fold_build1 (ABS_EXPR,
8445                                                     TREE_TYPE (targ0),
8446                                                     targ0));
8447         }
8448       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8449       else if (TREE_CODE (arg0) == ABS_EXPR)
8450         return arg0;
8451       else if (tree_expr_nonnegative_p (arg0))
8452         return arg0;
8453
8454       /* Strip sign ops from argument.  */
8455       if (TREE_CODE (type) == REAL_TYPE)
8456         {
8457           tem = fold_strip_sign_ops (arg0);
8458           if (tem)
8459             return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8460         }
8461       return NULL_TREE;
8462
8463     case CONJ_EXPR:
8464       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8465         return fold_convert (type, arg0);
8466       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8467         {
8468           tree itype = TREE_TYPE (type);
8469           tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8470           tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8471           return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8472         }
8473       if (TREE_CODE (arg0) == COMPLEX_CST)
8474         {
8475           tree itype = TREE_TYPE (type);
8476           tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8477           tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8478           return build_complex (type, rpart, negate_expr (ipart));
8479         }
8480       if (TREE_CODE (arg0) == CONJ_EXPR)
8481         return fold_convert (type, TREE_OPERAND (arg0, 0));
8482       return NULL_TREE;
8483
8484     case BIT_NOT_EXPR:
8485       if (TREE_CODE (arg0) == INTEGER_CST)
8486         return fold_not_const (arg0, type);
8487       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8488         return fold_convert (type, TREE_OPERAND (arg0, 0));
8489       /* Convert ~ (-A) to A - 1.  */
8490       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8491         return fold_build2 (MINUS_EXPR, type,
8492                             fold_convert (type, TREE_OPERAND (arg0, 0)),
8493                             build_int_cst (type, 1));
8494       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8495       else if (INTEGRAL_TYPE_P (type)
8496                && ((TREE_CODE (arg0) == MINUS_EXPR
8497                     && integer_onep (TREE_OPERAND (arg0, 1)))
8498                    || (TREE_CODE (arg0) == PLUS_EXPR
8499                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8500         return fold_build1 (NEGATE_EXPR, type,
8501                             fold_convert (type, TREE_OPERAND (arg0, 0)));
8502       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8503       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8504                && (tem = fold_unary (BIT_NOT_EXPR, type,
8505                                      fold_convert (type,
8506                                                    TREE_OPERAND (arg0, 0)))))
8507         return fold_build2 (BIT_XOR_EXPR, type, tem,
8508                             fold_convert (type, TREE_OPERAND (arg0, 1)));
8509       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8510                && (tem = fold_unary (BIT_NOT_EXPR, type,
8511                                      fold_convert (type,
8512                                                    TREE_OPERAND (arg0, 1)))))
8513         return fold_build2 (BIT_XOR_EXPR, type,
8514                             fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8515       /* Perform BIT_NOT_EXPR on each element individually.  */
8516       else if (TREE_CODE (arg0) == VECTOR_CST)
8517         {
8518           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8519           int count = TYPE_VECTOR_SUBPARTS (type), i;
8520
8521           for (i = 0; i < count; i++)
8522             {
8523               if (elements)
8524                 {
8525                   elem = TREE_VALUE (elements);
8526                   elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8527                   if (elem == NULL_TREE)
8528                     break;
8529                   elements = TREE_CHAIN (elements);
8530                 }
8531               else
8532                 elem = build_int_cst (TREE_TYPE (type), -1);
8533               list = tree_cons (NULL_TREE, elem, list);
8534             }
8535           if (i == count)
8536             return build_vector (type, nreverse (list));
8537         }
8538
8539       return NULL_TREE;
8540
8541     case TRUTH_NOT_EXPR:
8542       /* The argument to invert_truthvalue must have Boolean type.  */
8543       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8544           arg0 = fold_convert (boolean_type_node, arg0);
8545
8546       /* Note that the operand of this must be an int
8547          and its values must be 0 or 1.
8548          ("true" is a fixed value perhaps depending on the language,
8549          but we don't handle values other than 1 correctly yet.)  */
8550       tem = fold_truth_not_expr (arg0);
8551       if (!tem)
8552         return NULL_TREE;
8553       return fold_convert (type, tem);
8554
8555     case REALPART_EXPR:
8556       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8557         return fold_convert (type, arg0);
8558       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8559         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8560                                  TREE_OPERAND (arg0, 1));
8561       if (TREE_CODE (arg0) == COMPLEX_CST)
8562         return fold_convert (type, TREE_REALPART (arg0));
8563       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8564         {
8565           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8566           tem = fold_build2 (TREE_CODE (arg0), itype,
8567                              fold_build1 (REALPART_EXPR, itype,
8568                                           TREE_OPERAND (arg0, 0)),
8569                              fold_build1 (REALPART_EXPR, itype,
8570                                           TREE_OPERAND (arg0, 1)));
8571           return fold_convert (type, tem);
8572         }
8573       if (TREE_CODE (arg0) == CONJ_EXPR)
8574         {
8575           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8576           tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8577           return fold_convert (type, tem);
8578         }
8579       if (TREE_CODE (arg0) == CALL_EXPR)
8580         {
8581           tree fn = get_callee_fndecl (arg0);
8582           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8583             switch (DECL_FUNCTION_CODE (fn))
8584               {
8585               CASE_FLT_FN (BUILT_IN_CEXPI):
8586                 fn = mathfn_built_in (type, BUILT_IN_COS);
8587                 if (fn)
8588                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8589                 break;
8590
8591               default:
8592                 break;
8593               }
8594         }
8595       return NULL_TREE;
8596
8597     case IMAGPART_EXPR:
8598       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8599         return fold_convert (type, integer_zero_node);
8600       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8601         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8602                                  TREE_OPERAND (arg0, 0));
8603       if (TREE_CODE (arg0) == COMPLEX_CST)
8604         return fold_convert (type, TREE_IMAGPART (arg0));
8605       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8606         {
8607           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8608           tem = fold_build2 (TREE_CODE (arg0), itype,
8609                              fold_build1 (IMAGPART_EXPR, itype,
8610                                           TREE_OPERAND (arg0, 0)),
8611                              fold_build1 (IMAGPART_EXPR, itype,
8612                                           TREE_OPERAND (arg0, 1)));
8613           return fold_convert (type, tem);
8614         }
8615       if (TREE_CODE (arg0) == CONJ_EXPR)
8616         {
8617           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8618           tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8619           return fold_convert (type, negate_expr (tem));
8620         }
8621       if (TREE_CODE (arg0) == CALL_EXPR)
8622         {
8623           tree fn = get_callee_fndecl (arg0);
8624           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8625             switch (DECL_FUNCTION_CODE (fn))
8626               {
8627               CASE_FLT_FN (BUILT_IN_CEXPI):
8628                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8629                 if (fn)
8630                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8631                 break;
8632
8633               default:
8634                 break;
8635               }
8636         }
8637       return NULL_TREE;
8638
8639     default:
8640       return NULL_TREE;
8641     } /* switch (code) */
8642 }
8643
8644
8645 /* If the operation was a conversion do _not_ mark a resulting constant
8646    with TREE_OVERFLOW if the original constant was not.  These conversions
8647    have implementation defined behavior and retaining the TREE_OVERFLOW
8648    flag here would confuse later passes such as VRP.  */
8649 tree
8650 fold_unary_ignore_overflow (enum tree_code code, tree type, tree op0)
8651 {
8652   tree res = fold_unary (code, type, op0);
8653   if (res
8654       && TREE_CODE (res) == INTEGER_CST
8655       && TREE_CODE (op0) == INTEGER_CST
8656       && CONVERT_EXPR_CODE_P (code))
8657     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8658
8659   return res;
8660 }
8661
8662 /* Fold a binary expression of code CODE and type TYPE with operands
8663    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8664    Return the folded expression if folding is successful.  Otherwise,
8665    return NULL_TREE.  */
8666
8667 static tree
8668 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8669 {
8670   enum tree_code compl_code;
8671
8672   if (code == MIN_EXPR)
8673     compl_code = MAX_EXPR;
8674   else if (code == MAX_EXPR)
8675     compl_code = MIN_EXPR;
8676   else
8677     gcc_unreachable ();
8678
8679   /* MIN (MAX (a, b), b) == b.  */
8680   if (TREE_CODE (op0) == compl_code
8681       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8682     return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8683
8684   /* MIN (MAX (b, a), b) == b.  */
8685   if (TREE_CODE (op0) == compl_code
8686       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8687       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8688     return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8689
8690   /* MIN (a, MAX (a, b)) == a.  */
8691   if (TREE_CODE (op1) == compl_code
8692       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8693       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8694     return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8695
8696   /* MIN (a, MAX (b, a)) == a.  */
8697   if (TREE_CODE (op1) == compl_code
8698       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8699       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8700     return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8701
8702   return NULL_TREE;
8703 }
8704
8705 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8706    by changing CODE to reduce the magnitude of constants involved in
8707    ARG0 of the comparison.
8708    Returns a canonicalized comparison tree if a simplification was
8709    possible, otherwise returns NULL_TREE.
8710    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8711    valid if signed overflow is undefined.  */
8712
8713 static tree
8714 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8715                                  tree arg0, tree arg1,
8716                                  bool *strict_overflow_p)
8717 {
8718   enum tree_code code0 = TREE_CODE (arg0);
8719   tree t, cst0 = NULL_TREE;
8720   int sgn0;
8721   bool swap = false;
8722
8723   /* Match A +- CST code arg1 and CST code arg1.  We can change the
8724      first form only if overflow is undefined.  */
8725   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8726          /* In principle pointers also have undefined overflow behavior,
8727             but that causes problems elsewhere.  */
8728          && !POINTER_TYPE_P (TREE_TYPE (arg0))
8729          && (code0 == MINUS_EXPR
8730              || code0 == PLUS_EXPR)
8731          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8732         || code0 == INTEGER_CST))
8733     return NULL_TREE;
8734
8735   /* Identify the constant in arg0 and its sign.  */
8736   if (code0 == INTEGER_CST)
8737     cst0 = arg0;
8738   else
8739     cst0 = TREE_OPERAND (arg0, 1);
8740   sgn0 = tree_int_cst_sgn (cst0);
8741
8742   /* Overflowed constants and zero will cause problems.  */
8743   if (integer_zerop (cst0)
8744       || TREE_OVERFLOW (cst0))
8745     return NULL_TREE;
8746
8747   /* See if we can reduce the magnitude of the constant in
8748      arg0 by changing the comparison code.  */
8749   if (code0 == INTEGER_CST)
8750     {
8751       /* CST <= arg1  ->  CST-1 < arg1.  */
8752       if (code == LE_EXPR && sgn0 == 1)
8753         code = LT_EXPR;
8754       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8755       else if (code == LT_EXPR && sgn0 == -1)
8756         code = LE_EXPR;
8757       /* CST > arg1  ->  CST-1 >= arg1.  */
8758       else if (code == GT_EXPR && sgn0 == 1)
8759         code = GE_EXPR;
8760       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8761       else if (code == GE_EXPR && sgn0 == -1)
8762         code = GT_EXPR;
8763       else
8764         return NULL_TREE;
8765       /* arg1 code' CST' might be more canonical.  */
8766       swap = true;
8767     }
8768   else
8769     {
8770       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8771       if (code == LT_EXPR
8772           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8773         code = LE_EXPR;
8774       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8775       else if (code == GT_EXPR
8776                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8777         code = GE_EXPR;
8778       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8779       else if (code == LE_EXPR
8780                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8781         code = LT_EXPR;
8782       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8783       else if (code == GE_EXPR
8784                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8785         code = GT_EXPR;
8786       else
8787         return NULL_TREE;
8788       *strict_overflow_p = true;
8789     }
8790
8791   /* Now build the constant reduced in magnitude.  But not if that
8792      would produce one outside of its types range.  */
8793   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8794       && ((sgn0 == 1
8795            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8796            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8797           || (sgn0 == -1
8798               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8799               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8800     /* We cannot swap the comparison here as that would cause us to
8801        endlessly recurse.  */
8802     return NULL_TREE;
8803
8804   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8805                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8806   if (code0 != INTEGER_CST)
8807     t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8808
8809   /* If swapping might yield to a more canonical form, do so.  */
8810   if (swap)
8811     return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8812   else
8813     return fold_build2 (code, type, t, arg1);
8814 }
8815
8816 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8817    overflow further.  Try to decrease the magnitude of constants involved
8818    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8819    and put sole constants at the second argument position.
8820    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8821
8822 static tree
8823 maybe_canonicalize_comparison (enum tree_code code, tree type,
8824                                tree arg0, tree arg1)
8825 {
8826   tree t;
8827   bool strict_overflow_p;
8828   const char * const warnmsg = G_("assuming signed overflow does not occur "
8829                                   "when reducing constant in comparison");
8830
8831   /* Try canonicalization by simplifying arg0.  */
8832   strict_overflow_p = false;
8833   t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8834                                        &strict_overflow_p);
8835   if (t)
8836     {
8837       if (strict_overflow_p)
8838         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8839       return t;
8840     }
8841
8842   /* Try canonicalization by simplifying arg1 using the swapped
8843      comparison.  */
8844   code = swap_tree_comparison (code);
8845   strict_overflow_p = false;
8846   t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8847                                        &strict_overflow_p);
8848   if (t && strict_overflow_p)
8849     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8850   return t;
8851 }
8852
8853 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8854    space.  This is used to avoid issuing overflow warnings for
8855    expressions like &p->x which can not wrap.  */
8856
8857 static bool
8858 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8859 {
8860   unsigned HOST_WIDE_INT offset_low, total_low;
8861   HOST_WIDE_INT size, offset_high, total_high;
8862
8863   if (!POINTER_TYPE_P (TREE_TYPE (base)))
8864     return true;
8865
8866   if (bitpos < 0)
8867     return true;
8868
8869   if (offset == NULL_TREE)
8870     {
8871       offset_low = 0;
8872       offset_high = 0;
8873     }
8874   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8875     return true;
8876   else
8877     {
8878       offset_low = TREE_INT_CST_LOW (offset);
8879       offset_high = TREE_INT_CST_HIGH (offset);
8880     }
8881
8882   if (add_double_with_sign (offset_low, offset_high,
8883                             bitpos / BITS_PER_UNIT, 0,
8884                             &total_low, &total_high,
8885                             true))
8886     return true;
8887
8888   if (total_high != 0)
8889     return true;
8890
8891   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8892   if (size <= 0)
8893     return true;
8894
8895   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8896      array.  */
8897   if (TREE_CODE (base) == ADDR_EXPR)
8898     {
8899       HOST_WIDE_INT base_size;
8900
8901       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8902       if (base_size > 0 && size < base_size)
8903         size = base_size;
8904     }
8905
8906   return total_low > (unsigned HOST_WIDE_INT) size;
8907 }
8908
8909 /* Subroutine of fold_binary.  This routine performs all of the
8910    transformations that are common to the equality/inequality
8911    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8912    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8913    fold_binary should call fold_binary.  Fold a comparison with
8914    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8915    the folded comparison or NULL_TREE.  */
8916
8917 static tree
8918 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8919 {
8920   tree arg0, arg1, tem;
8921
8922   arg0 = op0;
8923   arg1 = op1;
8924
8925   STRIP_SIGN_NOPS (arg0);
8926   STRIP_SIGN_NOPS (arg1);
8927
8928   tem = fold_relational_const (code, type, arg0, arg1);
8929   if (tem != NULL_TREE)
8930     return tem;
8931
8932   /* If one arg is a real or integer constant, put it last.  */
8933   if (tree_swap_operands_p (arg0, arg1, true))
8934     return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8935
8936   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8937   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8938       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8939           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8940           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8941       && (TREE_CODE (arg1) == INTEGER_CST
8942           && !TREE_OVERFLOW (arg1)))
8943     {
8944       tree const1 = TREE_OPERAND (arg0, 1);
8945       tree const2 = arg1;
8946       tree variable = TREE_OPERAND (arg0, 0);
8947       tree lhs;
8948       int lhs_add;
8949       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8950
8951       lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8952                          TREE_TYPE (arg1), const2, const1);
8953
8954       /* If the constant operation overflowed this can be
8955          simplified as a comparison against INT_MAX/INT_MIN.  */
8956       if (TREE_CODE (lhs) == INTEGER_CST
8957           && TREE_OVERFLOW (lhs))
8958         {
8959           int const1_sgn = tree_int_cst_sgn (const1);
8960           enum tree_code code2 = code;
8961
8962           /* Get the sign of the constant on the lhs if the
8963              operation were VARIABLE + CONST1.  */
8964           if (TREE_CODE (arg0) == MINUS_EXPR)
8965             const1_sgn = -const1_sgn;
8966
8967           /* The sign of the constant determines if we overflowed
8968              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8969              Canonicalize to the INT_MIN overflow by swapping the comparison
8970              if necessary.  */
8971           if (const1_sgn == -1)
8972             code2 = swap_tree_comparison (code);
8973
8974           /* We now can look at the canonicalized case
8975                VARIABLE + 1  CODE2  INT_MIN
8976              and decide on the result.  */
8977           if (code2 == LT_EXPR
8978               || code2 == LE_EXPR
8979               || code2 == EQ_EXPR)
8980             return omit_one_operand (type, boolean_false_node, variable);
8981           else if (code2 == NE_EXPR
8982                    || code2 == GE_EXPR
8983                    || code2 == GT_EXPR)
8984             return omit_one_operand (type, boolean_true_node, variable);
8985         }
8986
8987       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8988           && (TREE_CODE (lhs) != INTEGER_CST
8989               || !TREE_OVERFLOW (lhs)))
8990         {
8991           fold_overflow_warning (("assuming signed overflow does not occur "
8992                                   "when changing X +- C1 cmp C2 to "
8993                                   "X cmp C1 +- C2"),
8994                                  WARN_STRICT_OVERFLOW_COMPARISON);
8995           return fold_build2 (code, type, variable, lhs);
8996         }
8997     }
8998
8999   /* For comparisons of pointers we can decompose it to a compile time
9000      comparison of the base objects and the offsets into the object.
9001      This requires at least one operand being an ADDR_EXPR or a
9002      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
9003   if (POINTER_TYPE_P (TREE_TYPE (arg0))
9004       && (TREE_CODE (arg0) == ADDR_EXPR
9005           || TREE_CODE (arg1) == ADDR_EXPR
9006           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9007           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9008     {
9009       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9010       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
9011       enum machine_mode mode;
9012       int volatilep, unsignedp;
9013       bool indirect_base0 = false, indirect_base1 = false;
9014
9015       /* Get base and offset for the access.  Strip ADDR_EXPR for
9016          get_inner_reference, but put it back by stripping INDIRECT_REF
9017          off the base object if possible.  indirect_baseN will be true
9018          if baseN is not an address but refers to the object itself.  */
9019       base0 = arg0;
9020       if (TREE_CODE (arg0) == ADDR_EXPR)
9021         {
9022           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
9023                                        &bitsize, &bitpos0, &offset0, &mode,
9024                                        &unsignedp, &volatilep, false);
9025           if (TREE_CODE (base0) == INDIRECT_REF)
9026             base0 = TREE_OPERAND (base0, 0);
9027           else
9028             indirect_base0 = true;
9029         }
9030       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9031         {
9032           base0 = TREE_OPERAND (arg0, 0);
9033           offset0 = TREE_OPERAND (arg0, 1);
9034         }
9035
9036       base1 = arg1;
9037       if (TREE_CODE (arg1) == ADDR_EXPR)
9038         {
9039           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
9040                                        &bitsize, &bitpos1, &offset1, &mode,
9041                                        &unsignedp, &volatilep, false);
9042           if (TREE_CODE (base1) == INDIRECT_REF)
9043             base1 = TREE_OPERAND (base1, 0);
9044           else
9045             indirect_base1 = true;
9046         }
9047       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9048         {
9049           base1 = TREE_OPERAND (arg1, 0);
9050           offset1 = TREE_OPERAND (arg1, 1);
9051         }
9052
9053       /* If we have equivalent bases we might be able to simplify.  */
9054       if (indirect_base0 == indirect_base1
9055           && operand_equal_p (base0, base1, 0))
9056         {
9057           /* We can fold this expression to a constant if the non-constant
9058              offset parts are equal.  */
9059           if ((offset0 == offset1
9060                || (offset0 && offset1
9061                    && operand_equal_p (offset0, offset1, 0)))
9062               && (code == EQ_EXPR
9063                   || code == NE_EXPR
9064                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
9065                 
9066             {
9067               if (code != EQ_EXPR
9068                   && code != NE_EXPR
9069                   && bitpos0 != bitpos1
9070                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9071                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9072                 fold_overflow_warning (("assuming pointer wraparound does not "
9073                                         "occur when comparing P +- C1 with "
9074                                         "P +- C2"),
9075                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
9076
9077               switch (code)
9078                 {
9079                 case EQ_EXPR:
9080                   return constant_boolean_node (bitpos0 == bitpos1, type);
9081                 case NE_EXPR:
9082                   return constant_boolean_node (bitpos0 != bitpos1, type);
9083                 case LT_EXPR:
9084                   return constant_boolean_node (bitpos0 < bitpos1, type);
9085                 case LE_EXPR:
9086                   return constant_boolean_node (bitpos0 <= bitpos1, type);
9087                 case GE_EXPR:
9088                   return constant_boolean_node (bitpos0 >= bitpos1, type);
9089                 case GT_EXPR:
9090                   return constant_boolean_node (bitpos0 > bitpos1, type);
9091                 default:;
9092                 }
9093             }
9094           /* We can simplify the comparison to a comparison of the variable
9095              offset parts if the constant offset parts are equal.
9096              Be careful to use signed size type here because otherwise we
9097              mess with array offsets in the wrong way.  This is possible
9098              because pointer arithmetic is restricted to retain within an
9099              object and overflow on pointer differences is undefined as of
9100              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
9101           else if (bitpos0 == bitpos1
9102                    && ((code == EQ_EXPR || code == NE_EXPR)
9103                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
9104             {
9105               tree signed_size_type_node;
9106               signed_size_type_node = signed_type_for (size_type_node);
9107
9108               /* By converting to signed size type we cover middle-end pointer
9109                  arithmetic which operates on unsigned pointer types of size
9110                  type size and ARRAY_REF offsets which are properly sign or
9111                  zero extended from their type in case it is narrower than
9112                  size type.  */
9113               if (offset0 == NULL_TREE)
9114                 offset0 = build_int_cst (signed_size_type_node, 0);
9115               else
9116                 offset0 = fold_convert (signed_size_type_node, offset0);
9117               if (offset1 == NULL_TREE)
9118                 offset1 = build_int_cst (signed_size_type_node, 0);
9119               else
9120                 offset1 = fold_convert (signed_size_type_node, offset1);
9121
9122               if (code != EQ_EXPR
9123                   && code != NE_EXPR
9124                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9125                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9126                 fold_overflow_warning (("assuming pointer wraparound does not "
9127                                         "occur when comparing P +- C1 with "
9128                                         "P +- C2"),
9129                                        WARN_STRICT_OVERFLOW_COMPARISON);
9130
9131               return fold_build2 (code, type, offset0, offset1);
9132             }
9133         }
9134       /* For non-equal bases we can simplify if they are addresses
9135          of local binding decls or constants.  */
9136       else if (indirect_base0 && indirect_base1
9137                /* We know that !operand_equal_p (base0, base1, 0)
9138                   because the if condition was false.  But make
9139                   sure two decls are not the same.  */
9140                && base0 != base1
9141                && TREE_CODE (arg0) == ADDR_EXPR
9142                && TREE_CODE (arg1) == ADDR_EXPR
9143                && (((TREE_CODE (base0) == VAR_DECL
9144                      || TREE_CODE (base0) == PARM_DECL)
9145                     && (targetm.binds_local_p (base0)
9146                         || CONSTANT_CLASS_P (base1)))
9147                    || CONSTANT_CLASS_P (base0))
9148                && (((TREE_CODE (base1) == VAR_DECL
9149                      || TREE_CODE (base1) == PARM_DECL)
9150                     && (targetm.binds_local_p (base1)
9151                         || CONSTANT_CLASS_P (base0)))
9152                    || CONSTANT_CLASS_P (base1)))
9153         {
9154           if (code == EQ_EXPR)
9155             return omit_two_operands (type, boolean_false_node, arg0, arg1);
9156           else if (code == NE_EXPR)
9157             return omit_two_operands (type, boolean_true_node, arg0, arg1);
9158         }
9159       /* For equal offsets we can simplify to a comparison of the
9160          base addresses.  */
9161       else if (bitpos0 == bitpos1
9162                && (indirect_base0
9163                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9164                && (indirect_base1
9165                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9166                && ((offset0 == offset1)
9167                    || (offset0 && offset1
9168                        && operand_equal_p (offset0, offset1, 0))))
9169         {
9170           if (indirect_base0)
9171             base0 = fold_addr_expr (base0);
9172           if (indirect_base1)
9173             base1 = fold_addr_expr (base1);
9174           return fold_build2 (code, type, base0, base1);
9175         }
9176     }
9177
9178   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9179      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
9180      the resulting offset is smaller in absolute value than the
9181      original one.  */
9182   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9183       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9184       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9185           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9186       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9187       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9188           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9189     {
9190       tree const1 = TREE_OPERAND (arg0, 1);
9191       tree const2 = TREE_OPERAND (arg1, 1);
9192       tree variable1 = TREE_OPERAND (arg0, 0);
9193       tree variable2 = TREE_OPERAND (arg1, 0);
9194       tree cst;
9195       const char * const warnmsg = G_("assuming signed overflow does not "
9196                                       "occur when combining constants around "
9197                                       "a comparison");
9198
9199       /* Put the constant on the side where it doesn't overflow and is
9200          of lower absolute value than before.  */
9201       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9202                              ? MINUS_EXPR : PLUS_EXPR,
9203                              const2, const1, 0);
9204       if (!TREE_OVERFLOW (cst)
9205           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9206         {
9207           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9208           return fold_build2 (code, type,
9209                               variable1,
9210                               fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
9211                                            variable2, cst));
9212         }
9213
9214       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9215                              ? MINUS_EXPR : PLUS_EXPR,
9216                              const1, const2, 0);
9217       if (!TREE_OVERFLOW (cst)
9218           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9219         {
9220           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9221           return fold_build2 (code, type,
9222                               fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
9223                                            variable1, cst),
9224                               variable2);
9225         }
9226     }
9227
9228   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9229      signed arithmetic case.  That form is created by the compiler
9230      often enough for folding it to be of value.  One example is in
9231      computing loop trip counts after Operator Strength Reduction.  */
9232   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9233       && TREE_CODE (arg0) == MULT_EXPR
9234       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9235           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9236       && integer_zerop (arg1))
9237     {
9238       tree const1 = TREE_OPERAND (arg0, 1);
9239       tree const2 = arg1;                       /* zero */
9240       tree variable1 = TREE_OPERAND (arg0, 0);
9241       enum tree_code cmp_code = code;
9242
9243       gcc_assert (!integer_zerop (const1));
9244
9245       fold_overflow_warning (("assuming signed overflow does not occur when "
9246                               "eliminating multiplication in comparison "
9247                               "with zero"),
9248                              WARN_STRICT_OVERFLOW_COMPARISON);
9249
9250       /* If const1 is negative we swap the sense of the comparison.  */
9251       if (tree_int_cst_sgn (const1) < 0)
9252         cmp_code = swap_tree_comparison (cmp_code);
9253
9254       return fold_build2 (cmp_code, type, variable1, const2);
9255     }
9256
9257   tem = maybe_canonicalize_comparison (code, type, op0, op1);
9258   if (tem)
9259     return tem;
9260
9261   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9262     {
9263       tree targ0 = strip_float_extensions (arg0);
9264       tree targ1 = strip_float_extensions (arg1);
9265       tree newtype = TREE_TYPE (targ0);
9266
9267       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9268         newtype = TREE_TYPE (targ1);
9269
9270       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9271       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9272         return fold_build2 (code, type, fold_convert (newtype, targ0),
9273                             fold_convert (newtype, targ1));
9274
9275       /* (-a) CMP (-b) -> b CMP a  */
9276       if (TREE_CODE (arg0) == NEGATE_EXPR
9277           && TREE_CODE (arg1) == NEGATE_EXPR)
9278         return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
9279                             TREE_OPERAND (arg0, 0));
9280
9281       if (TREE_CODE (arg1) == REAL_CST)
9282         {
9283           REAL_VALUE_TYPE cst;
9284           cst = TREE_REAL_CST (arg1);
9285
9286           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
9287           if (TREE_CODE (arg0) == NEGATE_EXPR)
9288             return fold_build2 (swap_tree_comparison (code), type,
9289                                 TREE_OPERAND (arg0, 0),
9290                                 build_real (TREE_TYPE (arg1),
9291                                             REAL_VALUE_NEGATE (cst)));
9292
9293           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
9294           /* a CMP (-0) -> a CMP 0  */
9295           if (REAL_VALUE_MINUS_ZERO (cst))
9296             return fold_build2 (code, type, arg0,
9297                                 build_real (TREE_TYPE (arg1), dconst0));
9298
9299           /* x != NaN is always true, other ops are always false.  */
9300           if (REAL_VALUE_ISNAN (cst)
9301               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9302             {
9303               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9304               return omit_one_operand (type, tem, arg0);
9305             }
9306
9307           /* Fold comparisons against infinity.  */
9308           if (REAL_VALUE_ISINF (cst)
9309               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9310             {
9311               tem = fold_inf_compare (code, type, arg0, arg1);
9312               if (tem != NULL_TREE)
9313                 return tem;
9314             }
9315         }
9316
9317       /* If this is a comparison of a real constant with a PLUS_EXPR
9318          or a MINUS_EXPR of a real constant, we can convert it into a
9319          comparison with a revised real constant as long as no overflow
9320          occurs when unsafe_math_optimizations are enabled.  */
9321       if (flag_unsafe_math_optimizations
9322           && TREE_CODE (arg1) == REAL_CST
9323           && (TREE_CODE (arg0) == PLUS_EXPR
9324               || TREE_CODE (arg0) == MINUS_EXPR)
9325           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9326           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9327                                       ? MINUS_EXPR : PLUS_EXPR,
9328                                       arg1, TREE_OPERAND (arg0, 1), 0))
9329           && !TREE_OVERFLOW (tem))
9330         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9331
9332       /* Likewise, we can simplify a comparison of a real constant with
9333          a MINUS_EXPR whose first operand is also a real constant, i.e.
9334          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on 
9335          floating-point types only if -fassociative-math is set.  */
9336       if (flag_associative_math
9337           && TREE_CODE (arg1) == REAL_CST
9338           && TREE_CODE (arg0) == MINUS_EXPR
9339           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9340           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9341                                       arg1, 0))
9342           && !TREE_OVERFLOW (tem))
9343         return fold_build2 (swap_tree_comparison (code), type,
9344                             TREE_OPERAND (arg0, 1), tem);
9345
9346       /* Fold comparisons against built-in math functions.  */
9347       if (TREE_CODE (arg1) == REAL_CST
9348           && flag_unsafe_math_optimizations
9349           && ! flag_errno_math)
9350         {
9351           enum built_in_function fcode = builtin_mathfn_code (arg0);
9352
9353           if (fcode != END_BUILTINS)
9354             {
9355               tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
9356               if (tem != NULL_TREE)
9357                 return tem;
9358             }
9359         }
9360     }
9361
9362   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9363       && CONVERT_EXPR_P (arg0))
9364     {
9365       /* If we are widening one operand of an integer comparison,
9366          see if the other operand is similarly being widened.  Perhaps we
9367          can do the comparison in the narrower type.  */
9368       tem = fold_widened_comparison (code, type, arg0, arg1);
9369       if (tem)
9370         return tem;
9371
9372       /* Or if we are changing signedness.  */
9373       tem = fold_sign_changed_comparison (code, type, arg0, arg1);
9374       if (tem)
9375         return tem;
9376     }
9377
9378   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9379      constant, we can simplify it.  */
9380   if (TREE_CODE (arg1) == INTEGER_CST
9381       && (TREE_CODE (arg0) == MIN_EXPR
9382           || TREE_CODE (arg0) == MAX_EXPR)
9383       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9384     {
9385       tem = optimize_minmax_comparison (code, type, op0, op1);
9386       if (tem)
9387         return tem;
9388     }
9389
9390   /* Simplify comparison of something with itself.  (For IEEE
9391      floating-point, we can only do some of these simplifications.)  */
9392   if (operand_equal_p (arg0, arg1, 0))
9393     {
9394       switch (code)
9395         {
9396         case EQ_EXPR:
9397           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9398               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9399             return constant_boolean_node (1, type);
9400           break;
9401
9402         case GE_EXPR:
9403         case LE_EXPR:
9404           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9405               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9406             return constant_boolean_node (1, type);
9407           return fold_build2 (EQ_EXPR, type, arg0, arg1);
9408
9409         case NE_EXPR:
9410           /* For NE, we can only do this simplification if integer
9411              or we don't honor IEEE floating point NaNs.  */
9412           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9413               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9414             break;
9415           /* ... fall through ...  */
9416         case GT_EXPR:
9417         case LT_EXPR:
9418           return constant_boolean_node (0, type);
9419         default:
9420           gcc_unreachable ();
9421         }
9422     }
9423
9424   /* If we are comparing an expression that just has comparisons
9425      of two integer values, arithmetic expressions of those comparisons,
9426      and constants, we can simplify it.  There are only three cases
9427      to check: the two values can either be equal, the first can be
9428      greater, or the second can be greater.  Fold the expression for
9429      those three values.  Since each value must be 0 or 1, we have
9430      eight possibilities, each of which corresponds to the constant 0
9431      or 1 or one of the six possible comparisons.
9432
9433      This handles common cases like (a > b) == 0 but also handles
9434      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9435      occur in macroized code.  */
9436
9437   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9438     {
9439       tree cval1 = 0, cval2 = 0;
9440       int save_p = 0;
9441
9442       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9443           /* Don't handle degenerate cases here; they should already
9444              have been handled anyway.  */
9445           && cval1 != 0 && cval2 != 0
9446           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9447           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9448           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9449           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9450           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9451           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9452                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9453         {
9454           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9455           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9456
9457           /* We can't just pass T to eval_subst in case cval1 or cval2
9458              was the same as ARG1.  */
9459
9460           tree high_result
9461                 = fold_build2 (code, type,
9462                                eval_subst (arg0, cval1, maxval,
9463                                            cval2, minval),
9464                                arg1);
9465           tree equal_result
9466                 = fold_build2 (code, type,
9467                                eval_subst (arg0, cval1, maxval,
9468                                            cval2, maxval),
9469                                arg1);
9470           tree low_result
9471                 = fold_build2 (code, type,
9472                                eval_subst (arg0, cval1, minval,
9473                                            cval2, maxval),
9474                                arg1);
9475
9476           /* All three of these results should be 0 or 1.  Confirm they are.
9477              Then use those values to select the proper code to use.  */
9478
9479           if (TREE_CODE (high_result) == INTEGER_CST
9480               && TREE_CODE (equal_result) == INTEGER_CST
9481               && TREE_CODE (low_result) == INTEGER_CST)
9482             {
9483               /* Make a 3-bit mask with the high-order bit being the
9484                  value for `>', the next for '=', and the low for '<'.  */
9485               switch ((integer_onep (high_result) * 4)
9486                       + (integer_onep (equal_result) * 2)
9487                       + integer_onep (low_result))
9488                 {
9489                 case 0:
9490                   /* Always false.  */
9491                   return omit_one_operand (type, integer_zero_node, arg0);
9492                 case 1:
9493                   code = LT_EXPR;
9494                   break;
9495                 case 2:
9496                   code = EQ_EXPR;
9497                   break;
9498                 case 3:
9499                   code = LE_EXPR;
9500                   break;
9501                 case 4:
9502                   code = GT_EXPR;
9503                   break;
9504                 case 5:
9505                   code = NE_EXPR;
9506                   break;
9507                 case 6:
9508                   code = GE_EXPR;
9509                   break;
9510                 case 7:
9511                   /* Always true.  */
9512                   return omit_one_operand (type, integer_one_node, arg0);
9513                 }
9514
9515               if (save_p)
9516                 return save_expr (build2 (code, type, cval1, cval2));
9517               return fold_build2 (code, type, cval1, cval2);
9518             }
9519         }
9520     }
9521
9522   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9523      into a single range test.  */
9524   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9525        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9526       && TREE_CODE (arg1) == INTEGER_CST
9527       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9528       && !integer_zerop (TREE_OPERAND (arg0, 1))
9529       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9530       && !TREE_OVERFLOW (arg1))
9531     {
9532       tem = fold_div_compare (code, type, arg0, arg1);
9533       if (tem != NULL_TREE)
9534         return tem;
9535     }
9536
9537   /* Fold ~X op ~Y as Y op X.  */
9538   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9539       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9540     {
9541       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9542       return fold_build2 (code, type,
9543                           fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9544                           TREE_OPERAND (arg0, 0));
9545     }
9546
9547   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9548   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9549       && TREE_CODE (arg1) == INTEGER_CST)
9550     {
9551       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9552       return fold_build2 (swap_tree_comparison (code), type,
9553                           TREE_OPERAND (arg0, 0),
9554                           fold_build1 (BIT_NOT_EXPR, cmp_type,
9555                                        fold_convert (cmp_type, arg1)));
9556     }
9557
9558   return NULL_TREE;
9559 }
9560
9561
9562 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9563    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9564    argument EXPR represents the expression "z" of type TYPE.  */
9565
9566 static tree
9567 fold_mult_zconjz (tree type, tree expr)
9568 {
9569   tree itype = TREE_TYPE (type);
9570   tree rpart, ipart, tem;
9571
9572   if (TREE_CODE (expr) == COMPLEX_EXPR)
9573     {
9574       rpart = TREE_OPERAND (expr, 0);
9575       ipart = TREE_OPERAND (expr, 1);
9576     }
9577   else if (TREE_CODE (expr) == COMPLEX_CST)
9578     {
9579       rpart = TREE_REALPART (expr);
9580       ipart = TREE_IMAGPART (expr);
9581     }
9582   else
9583     {
9584       expr = save_expr (expr);
9585       rpart = fold_build1 (REALPART_EXPR, itype, expr);
9586       ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9587     }
9588
9589   rpart = save_expr (rpart);
9590   ipart = save_expr (ipart);
9591   tem = fold_build2 (PLUS_EXPR, itype,
9592                      fold_build2 (MULT_EXPR, itype, rpart, rpart),
9593                      fold_build2 (MULT_EXPR, itype, ipart, ipart));
9594   return fold_build2 (COMPLEX_EXPR, type, tem,
9595                       fold_convert (itype, integer_zero_node));
9596 }
9597
9598
9599 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9600    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9601    guarantees that P and N have the same least significant log2(M) bits.
9602    N is not otherwise constrained.  In particular, N is not normalized to
9603    0 <= N < M as is common.  In general, the precise value of P is unknown.
9604    M is chosen as large as possible such that constant N can be determined.
9605
9606    Returns M and sets *RESIDUE to N.  */
9607
9608 static unsigned HOST_WIDE_INT
9609 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue)
9610 {
9611   enum tree_code code;
9612
9613   *residue = 0;
9614
9615   code = TREE_CODE (expr);
9616   if (code == ADDR_EXPR)
9617     {
9618       expr = TREE_OPERAND (expr, 0);
9619       if (handled_component_p (expr))
9620         {
9621           HOST_WIDE_INT bitsize, bitpos;
9622           tree offset;
9623           enum machine_mode mode;
9624           int unsignedp, volatilep;
9625
9626           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9627                                       &mode, &unsignedp, &volatilep, false);
9628           *residue = bitpos / BITS_PER_UNIT;
9629           if (offset)
9630             {
9631               if (TREE_CODE (offset) == INTEGER_CST)
9632                 *residue += TREE_INT_CST_LOW (offset);
9633               else
9634                 /* We don't handle more complicated offset expressions.  */
9635                 return 1;
9636             }
9637         }
9638
9639       if (DECL_P (expr) && TREE_CODE (expr) != FUNCTION_DECL)
9640         return DECL_ALIGN_UNIT (expr);
9641     }
9642   else if (code == POINTER_PLUS_EXPR)
9643     {
9644       tree op0, op1;
9645       unsigned HOST_WIDE_INT modulus;
9646       enum tree_code inner_code;
9647       
9648       op0 = TREE_OPERAND (expr, 0);
9649       STRIP_NOPS (op0);
9650       modulus = get_pointer_modulus_and_residue (op0, residue);
9651
9652       op1 = TREE_OPERAND (expr, 1);
9653       STRIP_NOPS (op1);
9654       inner_code = TREE_CODE (op1);
9655       if (inner_code == INTEGER_CST)
9656         {
9657           *residue += TREE_INT_CST_LOW (op1);
9658           return modulus;
9659         }
9660       else if (inner_code == MULT_EXPR)
9661         {
9662           op1 = TREE_OPERAND (op1, 1);
9663           if (TREE_CODE (op1) == INTEGER_CST)
9664             {
9665               unsigned HOST_WIDE_INT align;
9666               
9667               /* Compute the greatest power-of-2 divisor of op1.  */
9668               align = TREE_INT_CST_LOW (op1);
9669               align &= -align;
9670
9671               /* If align is non-zero and less than *modulus, replace
9672                  *modulus with align., If align is 0, then either op1 is 0
9673                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9674                  unsigned HOST_WIDE_INT.  In either case, no additional
9675                  constraint is imposed.  */
9676               if (align)
9677                 modulus = MIN (modulus, align);
9678
9679               return modulus;
9680             }
9681         }
9682     }
9683
9684     /* If we get here, we were unable to determine anything useful about the
9685        expression.  */
9686     return 1;
9687 }
9688
9689
9690 /* Fold a binary expression of code CODE and type TYPE with operands
9691    OP0 and OP1.  Return the folded expression if folding is
9692    successful.  Otherwise, return NULL_TREE.  */
9693
9694 tree
9695 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9696 {
9697   enum tree_code_class kind = TREE_CODE_CLASS (code);
9698   tree arg0, arg1, tem;
9699   tree t1 = NULL_TREE;
9700   bool strict_overflow_p;
9701
9702   gcc_assert (IS_EXPR_CODE_CLASS (kind)
9703               && TREE_CODE_LENGTH (code) == 2
9704               && op0 != NULL_TREE
9705               && op1 != NULL_TREE);
9706
9707   arg0 = op0;
9708   arg1 = op1;
9709
9710   /* Strip any conversions that don't change the mode.  This is
9711      safe for every expression, except for a comparison expression
9712      because its signedness is derived from its operands.  So, in
9713      the latter case, only strip conversions that don't change the
9714      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
9715      preserved.
9716
9717      Note that this is done as an internal manipulation within the
9718      constant folder, in order to find the simplest representation
9719      of the arguments so that their form can be studied.  In any
9720      cases, the appropriate type conversions should be put back in
9721      the tree that will get out of the constant folder.  */
9722
9723   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9724     {
9725       STRIP_SIGN_NOPS (arg0);
9726       STRIP_SIGN_NOPS (arg1);
9727     }
9728   else
9729     {
9730       STRIP_NOPS (arg0);
9731       STRIP_NOPS (arg1);
9732     }
9733
9734   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9735      constant but we can't do arithmetic on them.  */
9736   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9737       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9738       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9739       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9740       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9741       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9742     {
9743       if (kind == tcc_binary)
9744         {
9745           /* Make sure type and arg0 have the same saturating flag.  */
9746           gcc_assert (TYPE_SATURATING (type)
9747                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9748           tem = const_binop (code, arg0, arg1, 0);
9749         }
9750       else if (kind == tcc_comparison)
9751         tem = fold_relational_const (code, type, arg0, arg1);
9752       else
9753         tem = NULL_TREE;
9754
9755       if (tem != NULL_TREE)
9756         {
9757           if (TREE_TYPE (tem) != type)
9758             tem = fold_convert (type, tem);
9759           return tem;
9760         }
9761     }
9762
9763   /* If this is a commutative operation, and ARG0 is a constant, move it
9764      to ARG1 to reduce the number of tests below.  */
9765   if (commutative_tree_code (code)
9766       && tree_swap_operands_p (arg0, arg1, true))
9767     return fold_build2 (code, type, op1, op0);
9768
9769   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9770
9771      First check for cases where an arithmetic operation is applied to a
9772      compound, conditional, or comparison operation.  Push the arithmetic
9773      operation inside the compound or conditional to see if any folding
9774      can then be done.  Convert comparison to conditional for this purpose.
9775      The also optimizes non-constant cases that used to be done in
9776      expand_expr.
9777
9778      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9779      one of the operands is a comparison and the other is a comparison, a
9780      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9781      code below would make the expression more complex.  Change it to a
9782      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9783      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9784
9785   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9786        || code == EQ_EXPR || code == NE_EXPR)
9787       && ((truth_value_p (TREE_CODE (arg0))
9788            && (truth_value_p (TREE_CODE (arg1))
9789                || (TREE_CODE (arg1) == BIT_AND_EXPR
9790                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9791           || (truth_value_p (TREE_CODE (arg1))
9792               && (truth_value_p (TREE_CODE (arg0))
9793                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9794                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9795     {
9796       tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9797                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9798                          : TRUTH_XOR_EXPR,
9799                          boolean_type_node,
9800                          fold_convert (boolean_type_node, arg0),
9801                          fold_convert (boolean_type_node, arg1));
9802
9803       if (code == EQ_EXPR)
9804         tem = invert_truthvalue (tem);
9805
9806       return fold_convert (type, tem);
9807     }
9808
9809   if (TREE_CODE_CLASS (code) == tcc_binary
9810       || TREE_CODE_CLASS (code) == tcc_comparison)
9811     {
9812       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9813         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9814                        fold_build2 (code, type,
9815                                     fold_convert (TREE_TYPE (op0),
9816                                                   TREE_OPERAND (arg0, 1)),
9817                                     op1));
9818       if (TREE_CODE (arg1) == COMPOUND_EXPR
9819           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9820         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9821                        fold_build2 (code, type, op0,
9822                                     fold_convert (TREE_TYPE (op1),
9823                                                   TREE_OPERAND (arg1, 1))));
9824
9825       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9826         {
9827           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9828                                                      arg0, arg1, 
9829                                                      /*cond_first_p=*/1);
9830           if (tem != NULL_TREE)
9831             return tem;
9832         }
9833
9834       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9835         {
9836           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9837                                                      arg1, arg0, 
9838                                                      /*cond_first_p=*/0);
9839           if (tem != NULL_TREE)
9840             return tem;
9841         }
9842     }
9843
9844   switch (code)
9845     {
9846     case POINTER_PLUS_EXPR:
9847       /* 0 +p index -> (type)index */
9848       if (integer_zerop (arg0))
9849         return non_lvalue (fold_convert (type, arg1));
9850
9851       /* PTR +p 0 -> PTR */
9852       if (integer_zerop (arg1))
9853         return non_lvalue (fold_convert (type, arg0));
9854
9855       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9856       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9857            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9858         return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9859                                                 fold_convert (sizetype, arg1),
9860                                                 fold_convert (sizetype, arg0)));
9861
9862       /* index +p PTR -> PTR +p index */
9863       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9864           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9865         return fold_build2 (POINTER_PLUS_EXPR, type,
9866                             fold_convert (type, arg1),
9867                             fold_convert (sizetype, arg0));
9868
9869       /* (PTR +p B) +p A -> PTR +p (B + A) */
9870       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9871         {
9872           tree inner;
9873           tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9874           tree arg00 = TREE_OPERAND (arg0, 0);
9875           inner = fold_build2 (PLUS_EXPR, sizetype,
9876                                arg01, fold_convert (sizetype, arg1));
9877           return fold_convert (type,
9878                                fold_build2 (POINTER_PLUS_EXPR,
9879                                             TREE_TYPE (arg00), arg00, inner));
9880         }
9881
9882       /* PTR_CST +p CST -> CST1 */
9883       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9884         return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9885
9886      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9887         of the array.  Loop optimizer sometimes produce this type of
9888         expressions.  */
9889       if (TREE_CODE (arg0) == ADDR_EXPR)
9890         {
9891           tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9892           if (tem)
9893             return fold_convert (type, tem);
9894         }
9895
9896       return NULL_TREE;
9897
9898     case PLUS_EXPR:
9899       /* A + (-B) -> A - B */
9900       if (TREE_CODE (arg1) == NEGATE_EXPR)
9901         return fold_build2 (MINUS_EXPR, type,
9902                             fold_convert (type, arg0),
9903                             fold_convert (type, TREE_OPERAND (arg1, 0)));
9904       /* (-A) + B -> B - A */
9905       if (TREE_CODE (arg0) == NEGATE_EXPR
9906           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9907         return fold_build2 (MINUS_EXPR, type,
9908                             fold_convert (type, arg1),
9909                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9910
9911       if (INTEGRAL_TYPE_P (type))
9912         {
9913           /* Convert ~A + 1 to -A.  */
9914           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9915               && integer_onep (arg1))
9916             return fold_build1 (NEGATE_EXPR, type,
9917                                 fold_convert (type, TREE_OPERAND (arg0, 0)));
9918
9919           /* ~X + X is -1.  */
9920           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9921               && !TYPE_OVERFLOW_TRAPS (type))
9922             {
9923               tree tem = TREE_OPERAND (arg0, 0);
9924
9925               STRIP_NOPS (tem);
9926               if (operand_equal_p (tem, arg1, 0))
9927                 {
9928                   t1 = build_int_cst_type (type, -1);
9929                   return omit_one_operand (type, t1, arg1);
9930                 }
9931             }
9932
9933           /* X + ~X is -1.  */
9934           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9935               && !TYPE_OVERFLOW_TRAPS (type))
9936             {
9937               tree tem = TREE_OPERAND (arg1, 0);
9938
9939               STRIP_NOPS (tem);
9940               if (operand_equal_p (arg0, tem, 0))
9941                 {
9942                   t1 = build_int_cst_type (type, -1);
9943                   return omit_one_operand (type, t1, arg0);
9944                 }
9945             }
9946
9947           /* X + (X / CST) * -CST is X % CST.  */
9948           if (TREE_CODE (arg1) == MULT_EXPR
9949               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9950               && operand_equal_p (arg0,
9951                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9952             {
9953               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9954               tree cst1 = TREE_OPERAND (arg1, 1);
9955               tree sum = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1, cst0);
9956               if (sum && integer_zerop (sum))
9957                 return fold_convert (type,
9958                                      fold_build2 (TRUNC_MOD_EXPR,
9959                                                   TREE_TYPE (arg0), arg0, cst0));
9960             }
9961         }
9962
9963       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9964          same or one.  Make sure type is not saturating.
9965          fold_plusminus_mult_expr will re-associate.  */
9966       if ((TREE_CODE (arg0) == MULT_EXPR
9967            || TREE_CODE (arg1) == MULT_EXPR)
9968           && !TYPE_SATURATING (type)
9969           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9970         {
9971           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9972           if (tem)
9973             return tem;
9974         }
9975
9976       if (! FLOAT_TYPE_P (type))
9977         {
9978           if (integer_zerop (arg1))
9979             return non_lvalue (fold_convert (type, arg0));
9980
9981           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9982              with a constant, and the two constants have no bits in common,
9983              we should treat this as a BIT_IOR_EXPR since this may produce more
9984              simplifications.  */
9985           if (TREE_CODE (arg0) == BIT_AND_EXPR
9986               && TREE_CODE (arg1) == BIT_AND_EXPR
9987               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9988               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9989               && integer_zerop (const_binop (BIT_AND_EXPR,
9990                                              TREE_OPERAND (arg0, 1),
9991                                              TREE_OPERAND (arg1, 1), 0)))
9992             {
9993               code = BIT_IOR_EXPR;
9994               goto bit_ior;
9995             }
9996
9997           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9998              (plus (plus (mult) (mult)) (foo)) so that we can
9999              take advantage of the factoring cases below.  */
10000           if (((TREE_CODE (arg0) == PLUS_EXPR
10001                 || TREE_CODE (arg0) == MINUS_EXPR)
10002                && TREE_CODE (arg1) == MULT_EXPR)
10003               || ((TREE_CODE (arg1) == PLUS_EXPR
10004                    || TREE_CODE (arg1) == MINUS_EXPR)
10005                   && TREE_CODE (arg0) == MULT_EXPR))
10006             {
10007               tree parg0, parg1, parg, marg;
10008               enum tree_code pcode;
10009
10010               if (TREE_CODE (arg1) == MULT_EXPR)
10011                 parg = arg0, marg = arg1;
10012               else
10013                 parg = arg1, marg = arg0;
10014               pcode = TREE_CODE (parg);
10015               parg0 = TREE_OPERAND (parg, 0);
10016               parg1 = TREE_OPERAND (parg, 1);
10017               STRIP_NOPS (parg0);
10018               STRIP_NOPS (parg1);
10019
10020               if (TREE_CODE (parg0) == MULT_EXPR
10021                   && TREE_CODE (parg1) != MULT_EXPR)
10022                 return fold_build2 (pcode, type,
10023                                     fold_build2 (PLUS_EXPR, type,
10024                                                  fold_convert (type, parg0),
10025                                                  fold_convert (type, marg)),
10026                                     fold_convert (type, parg1));
10027               if (TREE_CODE (parg0) != MULT_EXPR
10028                   && TREE_CODE (parg1) == MULT_EXPR)
10029                 return fold_build2 (PLUS_EXPR, type,
10030                                     fold_convert (type, parg0),
10031                                     fold_build2 (pcode, type,
10032                                                  fold_convert (type, marg),
10033                                                  fold_convert (type,
10034                                                                parg1)));
10035             }
10036         }
10037       else
10038         {
10039           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
10040           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10041             return non_lvalue (fold_convert (type, arg0));
10042
10043           /* Likewise if the operands are reversed.  */
10044           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10045             return non_lvalue (fold_convert (type, arg1));
10046
10047           /* Convert X + -C into X - C.  */
10048           if (TREE_CODE (arg1) == REAL_CST
10049               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10050             {
10051               tem = fold_negate_const (arg1, type);
10052               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10053                 return fold_build2 (MINUS_EXPR, type,
10054                                     fold_convert (type, arg0),
10055                                     fold_convert (type, tem));
10056             }
10057
10058           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10059              to __complex__ ( x, y ).  This is not the same for SNaNs or
10060              if signed zeros are involved.  */
10061           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10062               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10063               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10064             {
10065               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10066               tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10067               tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10068               bool arg0rz = false, arg0iz = false;
10069               if ((arg0r && (arg0rz = real_zerop (arg0r)))
10070                   || (arg0i && (arg0iz = real_zerop (arg0i))))
10071                 {
10072                   tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10073                   tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10074                   if (arg0rz && arg1i && real_zerop (arg1i))
10075                     {
10076                       tree rp = arg1r ? arg1r
10077                                   : build1 (REALPART_EXPR, rtype, arg1);
10078                       tree ip = arg0i ? arg0i
10079                                   : build1 (IMAGPART_EXPR, rtype, arg0);
10080                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10081                     }
10082                   else if (arg0iz && arg1r && real_zerop (arg1r))
10083                     {
10084                       tree rp = arg0r ? arg0r
10085                                   : build1 (REALPART_EXPR, rtype, arg0);
10086                       tree ip = arg1i ? arg1i
10087                                   : build1 (IMAGPART_EXPR, rtype, arg1);
10088                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10089                     }
10090                 }
10091             }
10092
10093           if (flag_unsafe_math_optimizations
10094               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10095               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10096               && (tem = distribute_real_division (code, type, arg0, arg1)))
10097             return tem;
10098
10099           /* Convert x+x into x*2.0.  */
10100           if (operand_equal_p (arg0, arg1, 0)
10101               && SCALAR_FLOAT_TYPE_P (type))
10102             return fold_build2 (MULT_EXPR, type, arg0,
10103                                 build_real (type, dconst2));
10104
10105           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.  
10106              We associate floats only if the user has specified
10107              -fassociative-math.  */
10108           if (flag_associative_math
10109               && TREE_CODE (arg1) == PLUS_EXPR
10110               && TREE_CODE (arg0) != MULT_EXPR)
10111             {
10112               tree tree10 = TREE_OPERAND (arg1, 0);
10113               tree tree11 = TREE_OPERAND (arg1, 1);
10114               if (TREE_CODE (tree11) == MULT_EXPR
10115                   && TREE_CODE (tree10) == MULT_EXPR)
10116                 {
10117                   tree tree0;
10118                   tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
10119                   return fold_build2 (PLUS_EXPR, type, tree0, tree11);
10120                 }
10121             }
10122           /* Convert (b*c + d*e) + a into b*c + (d*e +a).  
10123              We associate floats only if the user has specified
10124              -fassociative-math.  */
10125           if (flag_associative_math
10126               && TREE_CODE (arg0) == PLUS_EXPR
10127               && TREE_CODE (arg1) != MULT_EXPR)
10128             {
10129               tree tree00 = TREE_OPERAND (arg0, 0);
10130               tree tree01 = TREE_OPERAND (arg0, 1);
10131               if (TREE_CODE (tree01) == MULT_EXPR
10132                   && TREE_CODE (tree00) == MULT_EXPR)
10133                 {
10134                   tree tree0;
10135                   tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
10136                   return fold_build2 (PLUS_EXPR, type, tree00, tree0);
10137                 }
10138             }
10139         }
10140
10141      bit_rotate:
10142       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10143          is a rotate of A by C1 bits.  */
10144       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10145          is a rotate of A by B bits.  */
10146       {
10147         enum tree_code code0, code1;
10148         tree rtype;
10149         code0 = TREE_CODE (arg0);
10150         code1 = TREE_CODE (arg1);
10151         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10152              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10153             && operand_equal_p (TREE_OPERAND (arg0, 0),
10154                                 TREE_OPERAND (arg1, 0), 0)
10155             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10156                 TYPE_UNSIGNED (rtype))
10157             /* Only create rotates in complete modes.  Other cases are not
10158                expanded properly.  */
10159             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10160           {
10161             tree tree01, tree11;
10162             enum tree_code code01, code11;
10163
10164             tree01 = TREE_OPERAND (arg0, 1);
10165             tree11 = TREE_OPERAND (arg1, 1);
10166             STRIP_NOPS (tree01);
10167             STRIP_NOPS (tree11);
10168             code01 = TREE_CODE (tree01);
10169             code11 = TREE_CODE (tree11);
10170             if (code01 == INTEGER_CST
10171                 && code11 == INTEGER_CST
10172                 && TREE_INT_CST_HIGH (tree01) == 0
10173                 && TREE_INT_CST_HIGH (tree11) == 0
10174                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10175                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10176               return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
10177                              code0 == LSHIFT_EXPR ? tree01 : tree11);
10178             else if (code11 == MINUS_EXPR)
10179               {
10180                 tree tree110, tree111;
10181                 tree110 = TREE_OPERAND (tree11, 0);
10182                 tree111 = TREE_OPERAND (tree11, 1);
10183                 STRIP_NOPS (tree110);
10184                 STRIP_NOPS (tree111);
10185                 if (TREE_CODE (tree110) == INTEGER_CST
10186                     && 0 == compare_tree_int (tree110,
10187                                               TYPE_PRECISION
10188                                               (TREE_TYPE (TREE_OPERAND
10189                                                           (arg0, 0))))
10190                     && operand_equal_p (tree01, tree111, 0))
10191                   return build2 ((code0 == LSHIFT_EXPR
10192                                   ? LROTATE_EXPR
10193                                   : RROTATE_EXPR),
10194                                  type, TREE_OPERAND (arg0, 0), tree01);
10195               }
10196             else if (code01 == MINUS_EXPR)
10197               {
10198                 tree tree010, tree011;
10199                 tree010 = TREE_OPERAND (tree01, 0);
10200                 tree011 = TREE_OPERAND (tree01, 1);
10201                 STRIP_NOPS (tree010);
10202                 STRIP_NOPS (tree011);
10203                 if (TREE_CODE (tree010) == INTEGER_CST
10204                     && 0 == compare_tree_int (tree010,
10205                                               TYPE_PRECISION
10206                                               (TREE_TYPE (TREE_OPERAND
10207                                                           (arg0, 0))))
10208                     && operand_equal_p (tree11, tree011, 0))
10209                   return build2 ((code0 != LSHIFT_EXPR
10210                                   ? LROTATE_EXPR
10211                                   : RROTATE_EXPR),
10212                                  type, TREE_OPERAND (arg0, 0), tree11);
10213               }
10214           }
10215       }
10216
10217     associate:
10218       /* In most languages, can't associate operations on floats through
10219          parentheses.  Rather than remember where the parentheses were, we
10220          don't associate floats at all, unless the user has specified
10221          -fassociative-math.
10222          And, we need to make sure type is not saturating.  */
10223
10224       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10225           && !TYPE_SATURATING (type))
10226         {
10227           tree var0, con0, lit0, minus_lit0;
10228           tree var1, con1, lit1, minus_lit1;
10229           bool ok = true;
10230
10231           /* Split both trees into variables, constants, and literals.  Then
10232              associate each group together, the constants with literals,
10233              then the result with variables.  This increases the chances of
10234              literals being recombined later and of generating relocatable
10235              expressions for the sum of a constant and literal.  */
10236           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10237           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10238                              code == MINUS_EXPR);
10239
10240           /* With undefined overflow we can only associate constants
10241              with one variable.  */
10242           if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10243                || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10244               && var0 && var1)
10245             {
10246               tree tmp0 = var0;
10247               tree tmp1 = var1;
10248
10249               if (TREE_CODE (tmp0) == NEGATE_EXPR)
10250                 tmp0 = TREE_OPERAND (tmp0, 0);
10251               if (TREE_CODE (tmp1) == NEGATE_EXPR)
10252                 tmp1 = TREE_OPERAND (tmp1, 0);
10253               /* The only case we can still associate with two variables
10254                  is if they are the same, modulo negation.  */
10255               if (!operand_equal_p (tmp0, tmp1, 0))
10256                 ok = false;
10257             }
10258
10259           /* Only do something if we found more than two objects.  Otherwise,
10260              nothing has changed and we risk infinite recursion.  */
10261           if (ok
10262               && (2 < ((var0 != 0) + (var1 != 0)
10263                        + (con0 != 0) + (con1 != 0)
10264                        + (lit0 != 0) + (lit1 != 0)
10265                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10266             {
10267               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
10268               if (code == MINUS_EXPR)
10269                 code = PLUS_EXPR;
10270
10271               var0 = associate_trees (var0, var1, code, type);
10272               con0 = associate_trees (con0, con1, code, type);
10273               lit0 = associate_trees (lit0, lit1, code, type);
10274               minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
10275
10276               /* Preserve the MINUS_EXPR if the negative part of the literal is
10277                  greater than the positive part.  Otherwise, the multiplicative
10278                  folding code (i.e extract_muldiv) may be fooled in case
10279                  unsigned constants are subtracted, like in the following
10280                  example: ((X*2 + 4) - 8U)/2.  */
10281               if (minus_lit0 && lit0)
10282                 {
10283                   if (TREE_CODE (lit0) == INTEGER_CST
10284                       && TREE_CODE (minus_lit0) == INTEGER_CST
10285                       && tree_int_cst_lt (lit0, minus_lit0))
10286                     {
10287                       minus_lit0 = associate_trees (minus_lit0, lit0,
10288                                                     MINUS_EXPR, type);
10289                       lit0 = 0;
10290                     }
10291                   else
10292                     {
10293                       lit0 = associate_trees (lit0, minus_lit0,
10294                                               MINUS_EXPR, type);
10295                       minus_lit0 = 0;
10296                     }
10297                 }
10298               if (minus_lit0)
10299                 {
10300                   if (con0 == 0)
10301                     return fold_convert (type,
10302                                          associate_trees (var0, minus_lit0,
10303                                                           MINUS_EXPR, type));
10304                   else
10305                     {
10306                       con0 = associate_trees (con0, minus_lit0,
10307                                               MINUS_EXPR, type);
10308                       return fold_convert (type,
10309                                            associate_trees (var0, con0,
10310                                                             PLUS_EXPR, type));
10311                     }
10312                 }
10313
10314               con0 = associate_trees (con0, lit0, code, type);
10315               return fold_convert (type, associate_trees (var0, con0,
10316                                                           code, type));
10317             }
10318         }
10319
10320       return NULL_TREE;
10321
10322     case MINUS_EXPR:
10323       /* Pointer simplifications for subtraction, simple reassociations. */
10324       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10325         {
10326           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10327           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10328               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10329             {
10330               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10331               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10332               tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10333               tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10334               return fold_build2 (PLUS_EXPR, type,
10335                                   fold_build2 (MINUS_EXPR, type, arg00, arg10),
10336                                   fold_build2 (MINUS_EXPR, type, arg01, arg11));
10337             }
10338           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10339           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10340             {
10341               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10342               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10343               tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
10344               if (tmp)
10345                 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
10346             }
10347         }
10348       /* A - (-B) -> A + B */
10349       if (TREE_CODE (arg1) == NEGATE_EXPR)
10350         return fold_build2 (PLUS_EXPR, type, op0,
10351                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10352       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10353       if (TREE_CODE (arg0) == NEGATE_EXPR
10354           && (FLOAT_TYPE_P (type)
10355               || INTEGRAL_TYPE_P (type))
10356           && negate_expr_p (arg1)
10357           && reorder_operands_p (arg0, arg1))
10358         return fold_build2 (MINUS_EXPR, type,
10359                             fold_convert (type, negate_expr (arg1)),
10360                             fold_convert (type, TREE_OPERAND (arg0, 0)));
10361       /* Convert -A - 1 to ~A.  */
10362       if (INTEGRAL_TYPE_P (type)
10363           && TREE_CODE (arg0) == NEGATE_EXPR
10364           && integer_onep (arg1)
10365           && !TYPE_OVERFLOW_TRAPS (type))
10366         return fold_build1 (BIT_NOT_EXPR, type,
10367                             fold_convert (type, TREE_OPERAND (arg0, 0)));
10368
10369       /* Convert -1 - A to ~A.  */
10370       if (INTEGRAL_TYPE_P (type)
10371           && integer_all_onesp (arg0))
10372         return fold_build1 (BIT_NOT_EXPR, type, op1);
10373
10374
10375       /* X - (X / CST) * CST is X % CST.  */
10376       if (INTEGRAL_TYPE_P (type)
10377           && TREE_CODE (arg1) == MULT_EXPR
10378           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10379           && operand_equal_p (arg0,
10380                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10381           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10382                               TREE_OPERAND (arg1, 1), 0))
10383         return fold_convert (type,
10384                              fold_build2 (TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10385                                           arg0, TREE_OPERAND (arg1, 1)));
10386
10387       if (! FLOAT_TYPE_P (type))
10388         {
10389           if (integer_zerop (arg0))
10390             return negate_expr (fold_convert (type, arg1));
10391           if (integer_zerop (arg1))
10392             return non_lvalue (fold_convert (type, arg0));
10393
10394           /* Fold A - (A & B) into ~B & A.  */
10395           if (!TREE_SIDE_EFFECTS (arg0)
10396               && TREE_CODE (arg1) == BIT_AND_EXPR)
10397             {
10398               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10399                 {
10400                   tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10401                   return fold_build2 (BIT_AND_EXPR, type,
10402                                       fold_build1 (BIT_NOT_EXPR, type, arg10),
10403                                       fold_convert (type, arg0));
10404                 }
10405               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10406                 {
10407                   tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10408                   return fold_build2 (BIT_AND_EXPR, type,
10409                                       fold_build1 (BIT_NOT_EXPR, type, arg11),
10410                                       fold_convert (type, arg0));
10411                 }
10412             }
10413
10414           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10415              any power of 2 minus 1.  */
10416           if (TREE_CODE (arg0) == BIT_AND_EXPR
10417               && TREE_CODE (arg1) == BIT_AND_EXPR
10418               && operand_equal_p (TREE_OPERAND (arg0, 0),
10419                                   TREE_OPERAND (arg1, 0), 0))
10420             {
10421               tree mask0 = TREE_OPERAND (arg0, 1);
10422               tree mask1 = TREE_OPERAND (arg1, 1);
10423               tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
10424
10425               if (operand_equal_p (tem, mask1, 0))
10426                 {
10427                   tem = fold_build2 (BIT_XOR_EXPR, type,
10428                                      TREE_OPERAND (arg0, 0), mask1);
10429                   return fold_build2 (MINUS_EXPR, type, tem, mask1);
10430                 }
10431             }
10432         }
10433
10434       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10435       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10436         return non_lvalue (fold_convert (type, arg0));
10437
10438       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10439          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10440          (-ARG1 + ARG0) reduces to -ARG1.  */
10441       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10442         return negate_expr (fold_convert (type, arg1));
10443
10444       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10445          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10446          signed zeros are involved.  */
10447       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10448           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10449           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10450         {
10451           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10452           tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10453           tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10454           bool arg0rz = false, arg0iz = false;
10455           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10456               || (arg0i && (arg0iz = real_zerop (arg0i))))
10457             {
10458               tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10459               tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10460               if (arg0rz && arg1i && real_zerop (arg1i))
10461                 {
10462                   tree rp = fold_build1 (NEGATE_EXPR, rtype,
10463                                          arg1r ? arg1r
10464                                          : build1 (REALPART_EXPR, rtype, arg1));
10465                   tree ip = arg0i ? arg0i
10466                     : build1 (IMAGPART_EXPR, rtype, arg0);
10467                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10468                 }
10469               else if (arg0iz && arg1r && real_zerop (arg1r))
10470                 {
10471                   tree rp = arg0r ? arg0r
10472                     : build1 (REALPART_EXPR, rtype, arg0);
10473                   tree ip = fold_build1 (NEGATE_EXPR, rtype,
10474                                          arg1i ? arg1i
10475                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10476                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10477                 }
10478             }
10479         }
10480
10481       /* Fold &x - &x.  This can happen from &x.foo - &x.
10482          This is unsafe for certain floats even in non-IEEE formats.
10483          In IEEE, it is unsafe because it does wrong for NaNs.
10484          Also note that operand_equal_p is always false if an operand
10485          is volatile.  */
10486
10487       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10488           && operand_equal_p (arg0, arg1, 0))
10489         return fold_convert (type, integer_zero_node);
10490
10491       /* A - B -> A + (-B) if B is easily negatable.  */
10492       if (negate_expr_p (arg1)
10493           && ((FLOAT_TYPE_P (type)
10494                /* Avoid this transformation if B is a positive REAL_CST.  */
10495                && (TREE_CODE (arg1) != REAL_CST
10496                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10497               || INTEGRAL_TYPE_P (type)))
10498         return fold_build2 (PLUS_EXPR, type,
10499                             fold_convert (type, arg0),
10500                             fold_convert (type, negate_expr (arg1)));
10501
10502       /* Try folding difference of addresses.  */
10503       {
10504         HOST_WIDE_INT diff;
10505
10506         if ((TREE_CODE (arg0) == ADDR_EXPR
10507              || TREE_CODE (arg1) == ADDR_EXPR)
10508             && ptr_difference_const (arg0, arg1, &diff))
10509           return build_int_cst_type (type, diff);
10510       }
10511
10512       /* Fold &a[i] - &a[j] to i-j.  */
10513       if (TREE_CODE (arg0) == ADDR_EXPR
10514           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10515           && TREE_CODE (arg1) == ADDR_EXPR
10516           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10517         {
10518           tree aref0 = TREE_OPERAND (arg0, 0);
10519           tree aref1 = TREE_OPERAND (arg1, 0);
10520           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10521                                TREE_OPERAND (aref1, 0), 0))
10522             {
10523               tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10524               tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10525               tree esz = array_ref_element_size (aref0);
10526               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10527               return fold_build2 (MULT_EXPR, type, diff,
10528                                   fold_convert (type, esz));
10529                                   
10530             }
10531         }
10532
10533       if (flag_unsafe_math_optimizations
10534           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10535           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10536           && (tem = distribute_real_division (code, type, arg0, arg1)))
10537         return tem;
10538
10539       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10540          same or one.  Make sure type is not saturating.
10541          fold_plusminus_mult_expr will re-associate.  */
10542       if ((TREE_CODE (arg0) == MULT_EXPR
10543            || TREE_CODE (arg1) == MULT_EXPR)
10544           && !TYPE_SATURATING (type)
10545           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10546         {
10547           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10548           if (tem)
10549             return tem;
10550         }
10551
10552       goto associate;
10553
10554     case MULT_EXPR:
10555       /* (-A) * (-B) -> A * B  */
10556       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10557         return fold_build2 (MULT_EXPR, type,
10558                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10559                             fold_convert (type, negate_expr (arg1)));
10560       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10561         return fold_build2 (MULT_EXPR, type,
10562                             fold_convert (type, negate_expr (arg0)),
10563                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10564
10565       if (! FLOAT_TYPE_P (type))
10566         {
10567           if (integer_zerop (arg1))
10568             return omit_one_operand (type, arg1, arg0);
10569           if (integer_onep (arg1))
10570             return non_lvalue (fold_convert (type, arg0));
10571           /* Transform x * -1 into -x.  Make sure to do the negation
10572              on the original operand with conversions not stripped
10573              because we can only strip non-sign-changing conversions.  */
10574           if (integer_all_onesp (arg1))
10575             return fold_convert (type, negate_expr (op0));
10576           /* Transform x * -C into -x * C if x is easily negatable.  */
10577           if (TREE_CODE (arg1) == INTEGER_CST
10578               && tree_int_cst_sgn (arg1) == -1
10579               && negate_expr_p (arg0)
10580               && (tem = negate_expr (arg1)) != arg1
10581               && !TREE_OVERFLOW (tem))
10582             return fold_build2 (MULT_EXPR, type,
10583                                 fold_convert (type, negate_expr (arg0)), tem);
10584
10585           /* (a * (1 << b)) is (a << b)  */
10586           if (TREE_CODE (arg1) == LSHIFT_EXPR
10587               && integer_onep (TREE_OPERAND (arg1, 0)))
10588             return fold_build2 (LSHIFT_EXPR, type, op0,
10589                                 TREE_OPERAND (arg1, 1));
10590           if (TREE_CODE (arg0) == LSHIFT_EXPR
10591               && integer_onep (TREE_OPERAND (arg0, 0)))
10592             return fold_build2 (LSHIFT_EXPR, type, op1,
10593                                 TREE_OPERAND (arg0, 1));
10594
10595           /* (A + A) * C -> A * 2 * C  */
10596           if (TREE_CODE (arg0) == PLUS_EXPR
10597               && TREE_CODE (arg1) == INTEGER_CST
10598               && operand_equal_p (TREE_OPERAND (arg0, 0),
10599                                   TREE_OPERAND (arg0, 1), 0))
10600             return fold_build2 (MULT_EXPR, type,
10601                                 omit_one_operand (type, TREE_OPERAND (arg0, 0),
10602                                                   TREE_OPERAND (arg0, 1)),
10603                                 fold_build2 (MULT_EXPR, type,
10604                                              build_int_cst (type, 2) , arg1));
10605
10606           strict_overflow_p = false;
10607           if (TREE_CODE (arg1) == INTEGER_CST
10608               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10609                                              &strict_overflow_p)))
10610             {
10611               if (strict_overflow_p)
10612                 fold_overflow_warning (("assuming signed overflow does not "
10613                                         "occur when simplifying "
10614                                         "multiplication"),
10615                                        WARN_STRICT_OVERFLOW_MISC);
10616               return fold_convert (type, tem);
10617             }
10618
10619           /* Optimize z * conj(z) for integer complex numbers.  */
10620           if (TREE_CODE (arg0) == CONJ_EXPR
10621               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10622             return fold_mult_zconjz (type, arg1);
10623           if (TREE_CODE (arg1) == CONJ_EXPR
10624               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10625             return fold_mult_zconjz (type, arg0);
10626         }
10627       else
10628         {
10629           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10630              when x is NaN, since x * 0 is also NaN.  Nor are they the
10631              same in modes with signed zeros, since multiplying a
10632              negative value by 0 gives -0, not +0.  */
10633           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10634               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10635               && real_zerop (arg1))
10636             return omit_one_operand (type, arg1, arg0);
10637           /* In IEEE floating point, x*1 is not equivalent to x for snans.  */
10638           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10639               && real_onep (arg1))
10640             return non_lvalue (fold_convert (type, arg0));
10641
10642           /* Transform x * -1.0 into -x.  */
10643           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10644               && real_minus_onep (arg1))
10645             return fold_convert (type, negate_expr (arg0));
10646
10647           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10648              the result for floating point types due to rounding so it is applied
10649              only if -fassociative-math was specify.  */
10650           if (flag_associative_math
10651               && TREE_CODE (arg0) == RDIV_EXPR
10652               && TREE_CODE (arg1) == REAL_CST
10653               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10654             {
10655               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10656                                       arg1, 0);
10657               if (tem)
10658                 return fold_build2 (RDIV_EXPR, type, tem,
10659                                     TREE_OPERAND (arg0, 1));
10660             }
10661
10662           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10663           if (operand_equal_p (arg0, arg1, 0))
10664             {
10665               tree tem = fold_strip_sign_ops (arg0);
10666               if (tem != NULL_TREE)
10667                 {
10668                   tem = fold_convert (type, tem);
10669                   return fold_build2 (MULT_EXPR, type, tem, tem);
10670                 }
10671             }
10672
10673           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10674              This is not the same for NaNs or if signed zeros are
10675              involved.  */
10676           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10677               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10678               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10679               && TREE_CODE (arg1) == COMPLEX_CST
10680               && real_zerop (TREE_REALPART (arg1)))
10681             {
10682               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10683               if (real_onep (TREE_IMAGPART (arg1)))
10684                 return fold_build2 (COMPLEX_EXPR, type,
10685                                     negate_expr (fold_build1 (IMAGPART_EXPR,
10686                                                               rtype, arg0)),
10687                                     fold_build1 (REALPART_EXPR, rtype, arg0));
10688               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10689                 return fold_build2 (COMPLEX_EXPR, type,
10690                                     fold_build1 (IMAGPART_EXPR, rtype, arg0),
10691                                     negate_expr (fold_build1 (REALPART_EXPR,
10692                                                               rtype, arg0)));
10693             }
10694
10695           /* Optimize z * conj(z) for floating point complex numbers.
10696              Guarded by flag_unsafe_math_optimizations as non-finite
10697              imaginary components don't produce scalar results.  */
10698           if (flag_unsafe_math_optimizations
10699               && TREE_CODE (arg0) == CONJ_EXPR
10700               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10701             return fold_mult_zconjz (type, arg1);
10702           if (flag_unsafe_math_optimizations
10703               && TREE_CODE (arg1) == CONJ_EXPR
10704               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10705             return fold_mult_zconjz (type, arg0);
10706
10707           if (flag_unsafe_math_optimizations)
10708             {
10709               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10710               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10711
10712               /* Optimizations of root(...)*root(...).  */
10713               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10714                 {
10715                   tree rootfn, arg;
10716                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10717                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10718
10719                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10720                   if (BUILTIN_SQRT_P (fcode0)
10721                       && operand_equal_p (arg00, arg10, 0)
10722                       && ! HONOR_SNANS (TYPE_MODE (type)))
10723                     return arg00;
10724
10725                   /* Optimize root(x)*root(y) as root(x*y).  */
10726                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10727                   arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10728                   return build_call_expr (rootfn, 1, arg);
10729                 }
10730
10731               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10732               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10733                 {
10734                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10735                   tree arg = fold_build2 (PLUS_EXPR, type,
10736                                           CALL_EXPR_ARG (arg0, 0),
10737                                           CALL_EXPR_ARG (arg1, 0));
10738                   return build_call_expr (expfn, 1, arg);
10739                 }
10740
10741               /* Optimizations of pow(...)*pow(...).  */
10742               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10743                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10744                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10745                 {
10746                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10747                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10748                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10749                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10750
10751                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10752                   if (operand_equal_p (arg01, arg11, 0))
10753                     {
10754                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10755                       tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10756                       return build_call_expr (powfn, 2, arg, arg01);
10757                     }
10758
10759                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10760                   if (operand_equal_p (arg00, arg10, 0))
10761                     {
10762                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10763                       tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10764                       return build_call_expr (powfn, 2, arg00, arg);
10765                     }
10766                 }
10767
10768               /* Optimize tan(x)*cos(x) as sin(x).  */
10769               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10770                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10771                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10772                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10773                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10774                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10775                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10776                                       CALL_EXPR_ARG (arg1, 0), 0))
10777                 {
10778                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10779
10780                   if (sinfn != NULL_TREE)
10781                     return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10782                 }
10783
10784               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10785               if (fcode1 == BUILT_IN_POW
10786                   || fcode1 == BUILT_IN_POWF
10787                   || fcode1 == BUILT_IN_POWL)
10788                 {
10789                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10790                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10791                   if (TREE_CODE (arg11) == REAL_CST
10792                       && !TREE_OVERFLOW (arg11)
10793                       && operand_equal_p (arg0, arg10, 0))
10794                     {
10795                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10796                       REAL_VALUE_TYPE c;
10797                       tree arg;
10798
10799                       c = TREE_REAL_CST (arg11);
10800                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10801                       arg = build_real (type, c);
10802                       return build_call_expr (powfn, 2, arg0, arg);
10803                     }
10804                 }
10805
10806               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10807               if (fcode0 == BUILT_IN_POW
10808                   || fcode0 == BUILT_IN_POWF
10809                   || fcode0 == BUILT_IN_POWL)
10810                 {
10811                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10812                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10813                   if (TREE_CODE (arg01) == REAL_CST
10814                       && !TREE_OVERFLOW (arg01)
10815                       && operand_equal_p (arg1, arg00, 0))
10816                     {
10817                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10818                       REAL_VALUE_TYPE c;
10819                       tree arg;
10820
10821                       c = TREE_REAL_CST (arg01);
10822                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10823                       arg = build_real (type, c);
10824                       return build_call_expr (powfn, 2, arg1, arg);
10825                     }
10826                 }
10827
10828               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10829               if (optimize_function_for_speed_p (cfun)
10830                   && operand_equal_p (arg0, arg1, 0))
10831                 {
10832                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10833
10834                   if (powfn)
10835                     {
10836                       tree arg = build_real (type, dconst2);
10837                       return build_call_expr (powfn, 2, arg0, arg);
10838                     }
10839                 }
10840             }
10841         }
10842       goto associate;
10843
10844     case BIT_IOR_EXPR:
10845     bit_ior:
10846       if (integer_all_onesp (arg1))
10847         return omit_one_operand (type, arg1, arg0);
10848       if (integer_zerop (arg1))
10849         return non_lvalue (fold_convert (type, arg0));
10850       if (operand_equal_p (arg0, arg1, 0))
10851         return non_lvalue (fold_convert (type, arg0));
10852
10853       /* ~X | X is -1.  */
10854       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10855           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10856         {
10857           t1 = fold_convert (type, integer_zero_node);
10858           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10859           return omit_one_operand (type, t1, arg1);
10860         }
10861
10862       /* X | ~X is -1.  */
10863       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10864           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10865         {
10866           t1 = fold_convert (type, integer_zero_node);
10867           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10868           return omit_one_operand (type, t1, arg0);
10869         }
10870
10871       /* Canonicalize (X & C1) | C2.  */
10872       if (TREE_CODE (arg0) == BIT_AND_EXPR
10873           && TREE_CODE (arg1) == INTEGER_CST
10874           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10875         {
10876           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10877           int width = TYPE_PRECISION (type), w;
10878           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10879           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10880           hi2 = TREE_INT_CST_HIGH (arg1);
10881           lo2 = TREE_INT_CST_LOW (arg1);
10882
10883           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10884           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10885             return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10886
10887           if (width > HOST_BITS_PER_WIDE_INT)
10888             {
10889               mhi = (unsigned HOST_WIDE_INT) -1 
10890                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10891               mlo = -1;
10892             }
10893           else
10894             {
10895               mhi = 0;
10896               mlo = (unsigned HOST_WIDE_INT) -1
10897                     >> (HOST_BITS_PER_WIDE_INT - width);
10898             }
10899
10900           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10901           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10902             return fold_build2 (BIT_IOR_EXPR, type,
10903                                 TREE_OPERAND (arg0, 0), arg1);
10904
10905           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10906              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10907              mode which allows further optimizations.  */
10908           hi1 &= mhi;
10909           lo1 &= mlo;
10910           hi2 &= mhi;
10911           lo2 &= mlo;
10912           hi3 = hi1 & ~hi2;
10913           lo3 = lo1 & ~lo2;
10914           for (w = BITS_PER_UNIT;
10915                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10916                w <<= 1)
10917             {
10918               unsigned HOST_WIDE_INT mask
10919                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10920               if (((lo1 | lo2) & mask) == mask
10921                   && (lo1 & ~mask) == 0 && hi1 == 0)
10922                 {
10923                   hi3 = 0;
10924                   lo3 = mask;
10925                   break;
10926                 }
10927             }
10928           if (hi3 != hi1 || lo3 != lo1)
10929             return fold_build2 (BIT_IOR_EXPR, type,
10930                                 fold_build2 (BIT_AND_EXPR, type,
10931                                              TREE_OPERAND (arg0, 0),
10932                                              build_int_cst_wide (type,
10933                                                                  lo3, hi3)),
10934                                 arg1);
10935         }
10936
10937       /* (X & Y) | Y is (X, Y).  */
10938       if (TREE_CODE (arg0) == BIT_AND_EXPR
10939           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10940         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10941       /* (X & Y) | X is (Y, X).  */
10942       if (TREE_CODE (arg0) == BIT_AND_EXPR
10943           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10944           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10945         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10946       /* X | (X & Y) is (Y, X).  */
10947       if (TREE_CODE (arg1) == BIT_AND_EXPR
10948           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10949           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10950         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10951       /* X | (Y & X) is (Y, X).  */
10952       if (TREE_CODE (arg1) == BIT_AND_EXPR
10953           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10954           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10955         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10956
10957       t1 = distribute_bit_expr (code, type, arg0, arg1);
10958       if (t1 != NULL_TREE)
10959         return t1;
10960
10961       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10962
10963          This results in more efficient code for machines without a NAND
10964          instruction.  Combine will canonicalize to the first form
10965          which will allow use of NAND instructions provided by the
10966          backend if they exist.  */
10967       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10968           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10969         {
10970           return fold_build1 (BIT_NOT_EXPR, type,
10971                               build2 (BIT_AND_EXPR, type,
10972                                       fold_convert (type,
10973                                                     TREE_OPERAND (arg0, 0)),
10974                                       fold_convert (type,
10975                                                     TREE_OPERAND (arg1, 0))));
10976         }
10977
10978       /* See if this can be simplified into a rotate first.  If that
10979          is unsuccessful continue in the association code.  */
10980       goto bit_rotate;
10981
10982     case BIT_XOR_EXPR:
10983       if (integer_zerop (arg1))
10984         return non_lvalue (fold_convert (type, arg0));
10985       if (integer_all_onesp (arg1))
10986         return fold_build1 (BIT_NOT_EXPR, type, op0);
10987       if (operand_equal_p (arg0, arg1, 0))
10988         return omit_one_operand (type, integer_zero_node, arg0);
10989
10990       /* ~X ^ X is -1.  */
10991       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10992           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10993         {
10994           t1 = fold_convert (type, integer_zero_node);
10995           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10996           return omit_one_operand (type, t1, arg1);
10997         }
10998
10999       /* X ^ ~X is -1.  */
11000       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11001           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11002         {
11003           t1 = fold_convert (type, integer_zero_node);
11004           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
11005           return omit_one_operand (type, t1, arg0);
11006         }
11007
11008       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11009          with a constant, and the two constants have no bits in common,
11010          we should treat this as a BIT_IOR_EXPR since this may produce more
11011          simplifications.  */
11012       if (TREE_CODE (arg0) == BIT_AND_EXPR
11013           && TREE_CODE (arg1) == BIT_AND_EXPR
11014           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11015           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11016           && integer_zerop (const_binop (BIT_AND_EXPR,
11017                                          TREE_OPERAND (arg0, 1),
11018                                          TREE_OPERAND (arg1, 1), 0)))
11019         {
11020           code = BIT_IOR_EXPR;
11021           goto bit_ior;
11022         }
11023
11024       /* (X | Y) ^ X -> Y & ~ X*/
11025       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11026           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11027         {
11028           tree t2 = TREE_OPERAND (arg0, 1);
11029           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
11030                             arg1);
11031           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11032                             fold_convert (type, t1));
11033           return t1;
11034         }
11035
11036       /* (Y | X) ^ X -> Y & ~ X*/
11037       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11038           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11039         {
11040           tree t2 = TREE_OPERAND (arg0, 0);
11041           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
11042                             arg1);
11043           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11044                             fold_convert (type, t1));
11045           return t1;
11046         }
11047
11048       /* X ^ (X | Y) -> Y & ~ X*/
11049       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11050           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11051         {
11052           tree t2 = TREE_OPERAND (arg1, 1);
11053           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11054                             arg0);
11055           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11056                             fold_convert (type, t1));
11057           return t1;
11058         }
11059
11060       /* X ^ (Y | X) -> Y & ~ X*/
11061       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11062           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11063         {
11064           tree t2 = TREE_OPERAND (arg1, 0);
11065           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11066                             arg0);
11067           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11068                             fold_convert (type, t1));
11069           return t1;
11070         }
11071         
11072       /* Convert ~X ^ ~Y to X ^ Y.  */
11073       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11074           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11075         return fold_build2 (code, type,
11076                             fold_convert (type, TREE_OPERAND (arg0, 0)),
11077                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11078
11079       /* Convert ~X ^ C to X ^ ~C.  */
11080       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11081           && TREE_CODE (arg1) == INTEGER_CST)
11082         return fold_build2 (code, type,
11083                             fold_convert (type, TREE_OPERAND (arg0, 0)),
11084                             fold_build1 (BIT_NOT_EXPR, type, arg1));
11085
11086       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
11087       if (TREE_CODE (arg0) == BIT_AND_EXPR
11088           && integer_onep (TREE_OPERAND (arg0, 1))
11089           && integer_onep (arg1))
11090         return fold_build2 (EQ_EXPR, type, arg0,
11091                             build_int_cst (TREE_TYPE (arg0), 0));
11092
11093       /* Fold (X & Y) ^ Y as ~X & Y.  */
11094       if (TREE_CODE (arg0) == BIT_AND_EXPR
11095           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11096         {
11097           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11098           return fold_build2 (BIT_AND_EXPR, type, 
11099                               fold_build1 (BIT_NOT_EXPR, type, tem),
11100                               fold_convert (type, arg1));
11101         }
11102       /* Fold (X & Y) ^ X as ~Y & X.  */
11103       if (TREE_CODE (arg0) == BIT_AND_EXPR
11104           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11105           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11106         {
11107           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11108           return fold_build2 (BIT_AND_EXPR, type,
11109                               fold_build1 (BIT_NOT_EXPR, type, tem),
11110                               fold_convert (type, arg1));
11111         }
11112       /* Fold X ^ (X & Y) as X & ~Y.  */
11113       if (TREE_CODE (arg1) == BIT_AND_EXPR
11114           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11115         {
11116           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11117           return fold_build2 (BIT_AND_EXPR, type,
11118                               fold_convert (type, arg0),
11119                               fold_build1 (BIT_NOT_EXPR, type, tem));
11120         }
11121       /* Fold X ^ (Y & X) as ~Y & X.  */
11122       if (TREE_CODE (arg1) == BIT_AND_EXPR
11123           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11124           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11125         {
11126           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11127           return fold_build2 (BIT_AND_EXPR, type,
11128                               fold_build1 (BIT_NOT_EXPR, type, tem),
11129                               fold_convert (type, arg0));
11130         }
11131
11132       /* See if this can be simplified into a rotate first.  If that
11133          is unsuccessful continue in the association code.  */
11134       goto bit_rotate;
11135
11136     case BIT_AND_EXPR:
11137       if (integer_all_onesp (arg1))
11138         return non_lvalue (fold_convert (type, arg0));
11139       if (integer_zerop (arg1))
11140         return omit_one_operand (type, arg1, arg0);
11141       if (operand_equal_p (arg0, arg1, 0))
11142         return non_lvalue (fold_convert (type, arg0));
11143
11144       /* ~X & X is always zero.  */
11145       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11146           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11147         return omit_one_operand (type, integer_zero_node, arg1);
11148
11149       /* X & ~X is always zero.  */
11150       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11151           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11152         return omit_one_operand (type, integer_zero_node, arg0);
11153
11154       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
11155       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11156           && TREE_CODE (arg1) == INTEGER_CST
11157           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11158         {
11159           tree tmp1 = fold_convert (type, arg1);
11160           tree tmp2 = fold_convert (type, TREE_OPERAND (arg0, 0));
11161           tree tmp3 = fold_convert (type, TREE_OPERAND (arg0, 1));
11162           tmp2 = fold_build2 (BIT_AND_EXPR, type, tmp2, tmp1);
11163           tmp3 = fold_build2 (BIT_AND_EXPR, type, tmp3, tmp1);
11164           return fold_convert (type,
11165                                fold_build2 (BIT_IOR_EXPR, type, tmp2, tmp3));
11166         }
11167
11168       /* (X | Y) & Y is (X, Y).  */
11169       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11170           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11171         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
11172       /* (X | Y) & X is (Y, X).  */
11173       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11174           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11175           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11176         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
11177       /* X & (X | Y) is (Y, X).  */
11178       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11179           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11180           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11181         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
11182       /* X & (Y | X) is (Y, X).  */
11183       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11184           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11185           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11186         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
11187
11188       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11189       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11190           && integer_onep (TREE_OPERAND (arg0, 1))
11191           && integer_onep (arg1))
11192         {
11193           tem = TREE_OPERAND (arg0, 0);
11194           return fold_build2 (EQ_EXPR, type,
11195                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11196                                            build_int_cst (TREE_TYPE (tem), 1)),
11197                               build_int_cst (TREE_TYPE (tem), 0));
11198         }
11199       /* Fold ~X & 1 as (X & 1) == 0.  */
11200       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11201           && integer_onep (arg1))
11202         {
11203           tem = TREE_OPERAND (arg0, 0);
11204           return fold_build2 (EQ_EXPR, type,
11205                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11206                                            build_int_cst (TREE_TYPE (tem), 1)),
11207                               build_int_cst (TREE_TYPE (tem), 0));
11208         }
11209
11210       /* Fold (X ^ Y) & Y as ~X & Y.  */
11211       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11212           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11213         {
11214           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11215           return fold_build2 (BIT_AND_EXPR, type, 
11216                               fold_build1 (BIT_NOT_EXPR, type, tem),
11217                               fold_convert (type, arg1));
11218         }
11219       /* Fold (X ^ Y) & X as ~Y & X.  */
11220       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11221           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11222           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11223         {
11224           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11225           return fold_build2 (BIT_AND_EXPR, type,
11226                               fold_build1 (BIT_NOT_EXPR, type, tem),
11227                               fold_convert (type, arg1));
11228         }
11229       /* Fold X & (X ^ Y) as X & ~Y.  */
11230       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11231           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11232         {
11233           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11234           return fold_build2 (BIT_AND_EXPR, type,
11235                               fold_convert (type, arg0),
11236                               fold_build1 (BIT_NOT_EXPR, type, tem));
11237         }
11238       /* Fold X & (Y ^ X) as ~Y & X.  */
11239       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11240           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11241           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11242         {
11243           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11244           return fold_build2 (BIT_AND_EXPR, type,
11245                               fold_build1 (BIT_NOT_EXPR, type, tem),
11246                               fold_convert (type, arg0));
11247         }
11248
11249       t1 = distribute_bit_expr (code, type, arg0, arg1);
11250       if (t1 != NULL_TREE)
11251         return t1;
11252       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11253       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11254           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11255         {
11256           unsigned int prec
11257             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11258
11259           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11260               && (~TREE_INT_CST_LOW (arg1)
11261                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11262             return fold_convert (type, TREE_OPERAND (arg0, 0));
11263         }
11264
11265       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11266
11267          This results in more efficient code for machines without a NOR
11268          instruction.  Combine will canonicalize to the first form
11269          which will allow use of NOR instructions provided by the
11270          backend if they exist.  */
11271       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11272           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11273         {
11274           return fold_build1 (BIT_NOT_EXPR, type,
11275                               build2 (BIT_IOR_EXPR, type,
11276                                       fold_convert (type,
11277                                                     TREE_OPERAND (arg0, 0)),
11278                                       fold_convert (type,
11279                                                     TREE_OPERAND (arg1, 0))));
11280         }
11281
11282       /* If arg0 is derived from the address of an object or function, we may
11283          be able to fold this expression using the object or function's
11284          alignment.  */
11285       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11286         {
11287           unsigned HOST_WIDE_INT modulus, residue;
11288           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11289
11290           modulus = get_pointer_modulus_and_residue (arg0, &residue);
11291
11292           /* This works because modulus is a power of 2.  If this weren't the
11293              case, we'd have to replace it by its greatest power-of-2
11294              divisor: modulus & -modulus.  */
11295           if (low < modulus)
11296             return build_int_cst (type, residue & low);
11297         }
11298
11299       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11300               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11301          if the new mask might be further optimized.  */
11302       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11303            || TREE_CODE (arg0) == RSHIFT_EXPR)
11304           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11305           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11306           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11307              < TYPE_PRECISION (TREE_TYPE (arg0))
11308           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11309           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11310         {
11311           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11312           unsigned HOST_WIDE_INT mask
11313             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11314           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11315           tree shift_type = TREE_TYPE (arg0);
11316
11317           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11318             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11319           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11320                    && TYPE_PRECISION (TREE_TYPE (arg0))
11321                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11322             {
11323               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11324               tree arg00 = TREE_OPERAND (arg0, 0);
11325               /* See if more bits can be proven as zero because of
11326                  zero extension.  */
11327               if (TREE_CODE (arg00) == NOP_EXPR
11328                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11329                 {
11330                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11331                   if (TYPE_PRECISION (inner_type)
11332                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11333                       && TYPE_PRECISION (inner_type) < prec)
11334                     {
11335                       prec = TYPE_PRECISION (inner_type);
11336                       /* See if we can shorten the right shift.  */
11337                       if (shiftc < prec)
11338                         shift_type = inner_type;
11339                     }
11340                 }
11341               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11342               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11343               zerobits <<= prec - shiftc;
11344               /* For arithmetic shift if sign bit could be set, zerobits
11345                  can contain actually sign bits, so no transformation is
11346                  possible, unless MASK masks them all away.  In that
11347                  case the shift needs to be converted into logical shift.  */
11348               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11349                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11350                 {
11351                   if ((mask & zerobits) == 0)
11352                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11353                   else
11354                     zerobits = 0;
11355                 }
11356             }
11357
11358           /* ((X << 16) & 0xff00) is (X, 0).  */
11359           if ((mask & zerobits) == mask)
11360             return omit_one_operand (type, build_int_cst (type, 0), arg0);
11361
11362           newmask = mask | zerobits;
11363           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11364             {
11365               unsigned int prec;
11366
11367               /* Only do the transformation if NEWMASK is some integer
11368                  mode's mask.  */
11369               for (prec = BITS_PER_UNIT;
11370                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11371                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11372                   break;
11373               if (prec < HOST_BITS_PER_WIDE_INT
11374                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11375                 {
11376                   tree newmaskt;
11377
11378                   if (shift_type != TREE_TYPE (arg0))
11379                     {
11380                       tem = fold_build2 (TREE_CODE (arg0), shift_type,
11381                                          fold_convert (shift_type,
11382                                                        TREE_OPERAND (arg0, 0)),
11383                                          TREE_OPERAND (arg0, 1));
11384                       tem = fold_convert (type, tem);
11385                     }
11386                   else
11387                     tem = op0;
11388                   newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11389                   if (!tree_int_cst_equal (newmaskt, arg1))
11390                     return fold_build2 (BIT_AND_EXPR, type, tem, newmaskt);
11391                 }
11392             }
11393         }
11394
11395       goto associate;
11396
11397     case RDIV_EXPR:
11398       /* Don't touch a floating-point divide by zero unless the mode
11399          of the constant can represent infinity.  */
11400       if (TREE_CODE (arg1) == REAL_CST
11401           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11402           && real_zerop (arg1))
11403         return NULL_TREE;
11404
11405       /* Optimize A / A to 1.0 if we don't care about
11406          NaNs or Infinities.  Skip the transformation
11407          for non-real operands.  */
11408       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11409           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11410           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11411           && operand_equal_p (arg0, arg1, 0))
11412         {
11413           tree r = build_real (TREE_TYPE (arg0), dconst1);
11414
11415           return omit_two_operands (type, r, arg0, arg1);
11416         }
11417
11418       /* The complex version of the above A / A optimization.  */
11419       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11420           && operand_equal_p (arg0, arg1, 0))
11421         {
11422           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11423           if (! HONOR_NANS (TYPE_MODE (elem_type))
11424               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11425             {
11426               tree r = build_real (elem_type, dconst1);
11427               /* omit_two_operands will call fold_convert for us.  */
11428               return omit_two_operands (type, r, arg0, arg1);
11429             }
11430         }
11431
11432       /* (-A) / (-B) -> A / B  */
11433       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11434         return fold_build2 (RDIV_EXPR, type,
11435                             TREE_OPERAND (arg0, 0),
11436                             negate_expr (arg1));
11437       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11438         return fold_build2 (RDIV_EXPR, type,
11439                             negate_expr (arg0),
11440                             TREE_OPERAND (arg1, 0));
11441
11442       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11443       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11444           && real_onep (arg1))
11445         return non_lvalue (fold_convert (type, arg0));
11446
11447       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11448       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11449           && real_minus_onep (arg1))
11450         return non_lvalue (fold_convert (type, negate_expr (arg0)));
11451
11452       /* If ARG1 is a constant, we can convert this to a multiply by the
11453          reciprocal.  This does not have the same rounding properties,
11454          so only do this if -freciprocal-math.  We can actually
11455          always safely do it if ARG1 is a power of two, but it's hard to
11456          tell if it is or not in a portable manner.  */
11457       if (TREE_CODE (arg1) == REAL_CST)
11458         {
11459           if (flag_reciprocal_math
11460               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11461                                           arg1, 0)))
11462             return fold_build2 (MULT_EXPR, type, arg0, tem);
11463           /* Find the reciprocal if optimizing and the result is exact.  */
11464           if (optimize)
11465             {
11466               REAL_VALUE_TYPE r;
11467               r = TREE_REAL_CST (arg1);
11468               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11469                 {
11470                   tem = build_real (type, r);
11471                   return fold_build2 (MULT_EXPR, type,
11472                                       fold_convert (type, arg0), tem);
11473                 }
11474             }
11475         }
11476       /* Convert A/B/C to A/(B*C).  */ 
11477       if (flag_reciprocal_math
11478           && TREE_CODE (arg0) == RDIV_EXPR)
11479         return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11480                             fold_build2 (MULT_EXPR, type,
11481                                          TREE_OPERAND (arg0, 1), arg1));
11482
11483       /* Convert A/(B/C) to (A/B)*C.  */
11484       if (flag_reciprocal_math
11485           && TREE_CODE (arg1) == RDIV_EXPR)
11486         return fold_build2 (MULT_EXPR, type,
11487                             fold_build2 (RDIV_EXPR, type, arg0,
11488                                          TREE_OPERAND (arg1, 0)),
11489                             TREE_OPERAND (arg1, 1));
11490
11491       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11492       if (flag_reciprocal_math
11493           && TREE_CODE (arg1) == MULT_EXPR
11494           && TREE_CODE (arg0) == REAL_CST
11495           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11496         {
11497           tree tem = const_binop (RDIV_EXPR, arg0,
11498                                   TREE_OPERAND (arg1, 1), 0);
11499           if (tem)
11500             return fold_build2 (RDIV_EXPR, type, tem,
11501                                 TREE_OPERAND (arg1, 0));
11502         }
11503
11504       if (flag_unsafe_math_optimizations)
11505         {
11506           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11507           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11508
11509           /* Optimize sin(x)/cos(x) as tan(x).  */
11510           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11511                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11512                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11513               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11514                                   CALL_EXPR_ARG (arg1, 0), 0))
11515             {
11516               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11517
11518               if (tanfn != NULL_TREE)
11519                 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11520             }
11521
11522           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11523           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11524                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11525                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11526               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11527                                   CALL_EXPR_ARG (arg1, 0), 0))
11528             {
11529               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11530
11531               if (tanfn != NULL_TREE)
11532                 {
11533                   tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11534                   return fold_build2 (RDIV_EXPR, type,
11535                                       build_real (type, dconst1), tmp);
11536                 }
11537             }
11538
11539           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11540              NaNs or Infinities.  */
11541           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11542                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11543                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11544             {
11545               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11546               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11547
11548               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11549                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11550                   && operand_equal_p (arg00, arg01, 0))
11551                 {
11552                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11553
11554                   if (cosfn != NULL_TREE)
11555                     return build_call_expr (cosfn, 1, arg00);
11556                 }
11557             }
11558
11559           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11560              NaNs or Infinities.  */
11561           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11562                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11563                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11564             {
11565               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11566               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11567
11568               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11569                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11570                   && operand_equal_p (arg00, arg01, 0))
11571                 {
11572                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11573
11574                   if (cosfn != NULL_TREE)
11575                     {
11576                       tree tmp = build_call_expr (cosfn, 1, arg00);
11577                       return fold_build2 (RDIV_EXPR, type,
11578                                           build_real (type, dconst1),
11579                                           tmp);
11580                     }
11581                 }
11582             }
11583
11584           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11585           if (fcode0 == BUILT_IN_POW
11586               || fcode0 == BUILT_IN_POWF
11587               || fcode0 == BUILT_IN_POWL)
11588             {
11589               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11590               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11591               if (TREE_CODE (arg01) == REAL_CST
11592                   && !TREE_OVERFLOW (arg01)
11593                   && operand_equal_p (arg1, arg00, 0))
11594                 {
11595                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11596                   REAL_VALUE_TYPE c;
11597                   tree arg;
11598
11599                   c = TREE_REAL_CST (arg01);
11600                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11601                   arg = build_real (type, c);
11602                   return build_call_expr (powfn, 2, arg1, arg);
11603                 }
11604             }
11605
11606           /* Optimize a/root(b/c) into a*root(c/b).  */
11607           if (BUILTIN_ROOT_P (fcode1))
11608             {
11609               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11610
11611               if (TREE_CODE (rootarg) == RDIV_EXPR)
11612                 {
11613                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11614                   tree b = TREE_OPERAND (rootarg, 0);
11615                   tree c = TREE_OPERAND (rootarg, 1);
11616
11617                   tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11618
11619                   tmp = build_call_expr (rootfn, 1, tmp);
11620                   return fold_build2 (MULT_EXPR, type, arg0, tmp);
11621                 }
11622             }
11623
11624           /* Optimize x/expN(y) into x*expN(-y).  */
11625           if (BUILTIN_EXPONENT_P (fcode1))
11626             {
11627               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11628               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11629               arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11630               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11631             }
11632
11633           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11634           if (fcode1 == BUILT_IN_POW
11635               || fcode1 == BUILT_IN_POWF
11636               || fcode1 == BUILT_IN_POWL)
11637             {
11638               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11639               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11640               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11641               tree neg11 = fold_convert (type, negate_expr (arg11));
11642               arg1 = build_call_expr (powfn, 2, arg10, neg11);
11643               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11644             }
11645         }
11646       return NULL_TREE;
11647
11648     case TRUNC_DIV_EXPR:
11649     case FLOOR_DIV_EXPR:
11650       /* Simplify A / (B << N) where A and B are positive and B is
11651          a power of 2, to A >> (N + log2(B)).  */
11652       strict_overflow_p = false;
11653       if (TREE_CODE (arg1) == LSHIFT_EXPR
11654           && (TYPE_UNSIGNED (type)
11655               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11656         {
11657           tree sval = TREE_OPERAND (arg1, 0);
11658           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11659             {
11660               tree sh_cnt = TREE_OPERAND (arg1, 1);
11661               unsigned long pow2;
11662
11663               if (TREE_INT_CST_LOW (sval))
11664                 pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11665               else
11666                 pow2 = exact_log2 (TREE_INT_CST_HIGH (sval))
11667                        + HOST_BITS_PER_WIDE_INT;
11668
11669               if (strict_overflow_p)
11670                 fold_overflow_warning (("assuming signed overflow does not "
11671                                         "occur when simplifying A / (B << N)"),
11672                                        WARN_STRICT_OVERFLOW_MISC);
11673
11674               sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11675                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
11676               return fold_build2 (RSHIFT_EXPR, type,
11677                                   fold_convert (type, arg0), sh_cnt);
11678             }
11679         }
11680
11681       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11682          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11683       if (INTEGRAL_TYPE_P (type)
11684           && TYPE_UNSIGNED (type)
11685           && code == FLOOR_DIV_EXPR)
11686         return fold_build2 (TRUNC_DIV_EXPR, type, op0, op1);
11687
11688       /* Fall thru */
11689
11690     case ROUND_DIV_EXPR:
11691     case CEIL_DIV_EXPR:
11692     case EXACT_DIV_EXPR:
11693       if (integer_onep (arg1))
11694         return non_lvalue (fold_convert (type, arg0));
11695       if (integer_zerop (arg1))
11696         return NULL_TREE;
11697       /* X / -1 is -X.  */
11698       if (!TYPE_UNSIGNED (type)
11699           && TREE_CODE (arg1) == INTEGER_CST
11700           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11701           && TREE_INT_CST_HIGH (arg1) == -1)
11702         return fold_convert (type, negate_expr (arg0));
11703
11704       /* Convert -A / -B to A / B when the type is signed and overflow is
11705          undefined.  */
11706       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11707           && TREE_CODE (arg0) == NEGATE_EXPR
11708           && negate_expr_p (arg1))
11709         {
11710           if (INTEGRAL_TYPE_P (type))
11711             fold_overflow_warning (("assuming signed overflow does not occur "
11712                                     "when distributing negation across "
11713                                     "division"),
11714                                    WARN_STRICT_OVERFLOW_MISC);
11715           return fold_build2 (code, type,
11716                               fold_convert (type, TREE_OPERAND (arg0, 0)),
11717                               fold_convert (type, negate_expr (arg1)));
11718         }
11719       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11720           && TREE_CODE (arg1) == NEGATE_EXPR
11721           && negate_expr_p (arg0))
11722         {
11723           if (INTEGRAL_TYPE_P (type))
11724             fold_overflow_warning (("assuming signed overflow does not occur "
11725                                     "when distributing negation across "
11726                                     "division"),
11727                                    WARN_STRICT_OVERFLOW_MISC);
11728           return fold_build2 (code, type,
11729                               fold_convert (type, negate_expr (arg0)),
11730                               fold_convert (type, TREE_OPERAND (arg1, 0)));
11731         }
11732
11733       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11734          operation, EXACT_DIV_EXPR.
11735
11736          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11737          At one time others generated faster code, it's not clear if they do
11738          after the last round to changes to the DIV code in expmed.c.  */
11739       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11740           && multiple_of_p (type, arg0, arg1))
11741         return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11742
11743       strict_overflow_p = false;
11744       if (TREE_CODE (arg1) == INTEGER_CST
11745           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11746                                          &strict_overflow_p)))
11747         {
11748           if (strict_overflow_p)
11749             fold_overflow_warning (("assuming signed overflow does not occur "
11750                                     "when simplifying division"),
11751                                    WARN_STRICT_OVERFLOW_MISC);
11752           return fold_convert (type, tem);
11753         }
11754
11755       return NULL_TREE;
11756
11757     case CEIL_MOD_EXPR:
11758     case FLOOR_MOD_EXPR:
11759     case ROUND_MOD_EXPR:
11760     case TRUNC_MOD_EXPR:
11761       /* X % 1 is always zero, but be sure to preserve any side
11762          effects in X.  */
11763       if (integer_onep (arg1))
11764         return omit_one_operand (type, integer_zero_node, arg0);
11765
11766       /* X % 0, return X % 0 unchanged so that we can get the
11767          proper warnings and errors.  */
11768       if (integer_zerop (arg1))
11769         return NULL_TREE;
11770
11771       /* 0 % X is always zero, but be sure to preserve any side
11772          effects in X.  Place this after checking for X == 0.  */
11773       if (integer_zerop (arg0))
11774         return omit_one_operand (type, integer_zero_node, arg1);
11775
11776       /* X % -1 is zero.  */
11777       if (!TYPE_UNSIGNED (type)
11778           && TREE_CODE (arg1) == INTEGER_CST
11779           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11780           && TREE_INT_CST_HIGH (arg1) == -1)
11781         return omit_one_operand (type, integer_zero_node, arg0);
11782
11783       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11784          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11785       strict_overflow_p = false;
11786       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11787           && (TYPE_UNSIGNED (type)
11788               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11789         {
11790           tree c = arg1;
11791           /* Also optimize A % (C << N)  where C is a power of 2,
11792              to A & ((C << N) - 1).  */
11793           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11794             c = TREE_OPERAND (arg1, 0);
11795
11796           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11797             {
11798               tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11799                                        build_int_cst (TREE_TYPE (arg1), 1));
11800               if (strict_overflow_p)
11801                 fold_overflow_warning (("assuming signed overflow does not "
11802                                         "occur when simplifying "
11803                                         "X % (power of two)"),
11804                                        WARN_STRICT_OVERFLOW_MISC);
11805               return fold_build2 (BIT_AND_EXPR, type,
11806                                   fold_convert (type, arg0),
11807                                   fold_convert (type, mask));
11808             }
11809         }
11810
11811       /* X % -C is the same as X % C.  */
11812       if (code == TRUNC_MOD_EXPR
11813           && !TYPE_UNSIGNED (type)
11814           && TREE_CODE (arg1) == INTEGER_CST
11815           && !TREE_OVERFLOW (arg1)
11816           && TREE_INT_CST_HIGH (arg1) < 0
11817           && !TYPE_OVERFLOW_TRAPS (type)
11818           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11819           && !sign_bit_p (arg1, arg1))
11820         return fold_build2 (code, type, fold_convert (type, arg0),
11821                             fold_convert (type, negate_expr (arg1)));
11822
11823       /* X % -Y is the same as X % Y.  */
11824       if (code == TRUNC_MOD_EXPR
11825           && !TYPE_UNSIGNED (type)
11826           && TREE_CODE (arg1) == NEGATE_EXPR
11827           && !TYPE_OVERFLOW_TRAPS (type))
11828         return fold_build2 (code, type, fold_convert (type, arg0),
11829                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11830
11831       if (TREE_CODE (arg1) == INTEGER_CST
11832           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11833                                          &strict_overflow_p)))
11834         {
11835           if (strict_overflow_p)
11836             fold_overflow_warning (("assuming signed overflow does not occur "
11837                                     "when simplifying modulus"),
11838                                    WARN_STRICT_OVERFLOW_MISC);
11839           return fold_convert (type, tem);
11840         }
11841
11842       return NULL_TREE;
11843
11844     case LROTATE_EXPR:
11845     case RROTATE_EXPR:
11846       if (integer_all_onesp (arg0))
11847         return omit_one_operand (type, arg0, arg1);
11848       goto shift;
11849
11850     case RSHIFT_EXPR:
11851       /* Optimize -1 >> x for arithmetic right shifts.  */
11852       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11853           && tree_expr_nonnegative_p (arg1))
11854         return omit_one_operand (type, arg0, arg1);
11855       /* ... fall through ...  */
11856
11857     case LSHIFT_EXPR:
11858     shift:
11859       if (integer_zerop (arg1))
11860         return non_lvalue (fold_convert (type, arg0));
11861       if (integer_zerop (arg0))
11862         return omit_one_operand (type, arg0, arg1);
11863
11864       /* Since negative shift count is not well-defined,
11865          don't try to compute it in the compiler.  */
11866       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11867         return NULL_TREE;
11868
11869       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11870       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11871           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11872           && host_integerp (TREE_OPERAND (arg0, 1), false)
11873           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11874         {
11875           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11876                                + TREE_INT_CST_LOW (arg1));
11877
11878           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11879              being well defined.  */
11880           if (low >= TYPE_PRECISION (type))
11881             {
11882               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11883                 low = low % TYPE_PRECISION (type);
11884               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11885                 return omit_one_operand (type, build_int_cst (type, 0),
11886                                          TREE_OPERAND (arg0, 0));
11887               else
11888                 low = TYPE_PRECISION (type) - 1;
11889             }
11890
11891           return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11892                               build_int_cst (type, low));
11893         }
11894
11895       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11896          into x & ((unsigned)-1 >> c) for unsigned types.  */
11897       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11898            || (TYPE_UNSIGNED (type)
11899                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11900           && host_integerp (arg1, false)
11901           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11902           && host_integerp (TREE_OPERAND (arg0, 1), false)
11903           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11904         {
11905           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11906           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11907           tree lshift;
11908           tree arg00;
11909
11910           if (low0 == low1)
11911             {
11912               arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11913
11914               lshift = build_int_cst (type, -1);
11915               lshift = int_const_binop (code, lshift, arg1, 0);
11916
11917               return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11918             }
11919         }
11920
11921       /* Rewrite an LROTATE_EXPR by a constant into an
11922          RROTATE_EXPR by a new constant.  */
11923       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11924         {
11925           tree tem = build_int_cst (TREE_TYPE (arg1),
11926                                     TYPE_PRECISION (type));
11927           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11928           return fold_build2 (RROTATE_EXPR, type, op0, tem);
11929         }
11930
11931       /* If we have a rotate of a bit operation with the rotate count and
11932          the second operand of the bit operation both constant,
11933          permute the two operations.  */
11934       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11935           && (TREE_CODE (arg0) == BIT_AND_EXPR
11936               || TREE_CODE (arg0) == BIT_IOR_EXPR
11937               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11938           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11939         return fold_build2 (TREE_CODE (arg0), type,
11940                             fold_build2 (code, type,
11941                                          TREE_OPERAND (arg0, 0), arg1),
11942                             fold_build2 (code, type,
11943                                          TREE_OPERAND (arg0, 1), arg1));
11944
11945       /* Two consecutive rotates adding up to the precision of the
11946          type can be ignored.  */
11947       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11948           && TREE_CODE (arg0) == RROTATE_EXPR
11949           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11950           && TREE_INT_CST_HIGH (arg1) == 0
11951           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11952           && ((TREE_INT_CST_LOW (arg1)
11953                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11954               == (unsigned int) TYPE_PRECISION (type)))
11955         return TREE_OPERAND (arg0, 0);
11956
11957       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11958               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11959          if the latter can be further optimized.  */
11960       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11961           && TREE_CODE (arg0) == BIT_AND_EXPR
11962           && TREE_CODE (arg1) == INTEGER_CST
11963           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11964         {
11965           tree mask = fold_build2 (code, type,
11966                                    fold_convert (type, TREE_OPERAND (arg0, 1)),
11967                                    arg1);
11968           tree shift = fold_build2 (code, type,
11969                                     fold_convert (type, TREE_OPERAND (arg0, 0)),
11970                                     arg1);
11971           tem = fold_binary (BIT_AND_EXPR, type, shift, mask);
11972           if (tem)
11973             return tem;
11974         }
11975
11976       return NULL_TREE;
11977
11978     case MIN_EXPR:
11979       if (operand_equal_p (arg0, arg1, 0))
11980         return omit_one_operand (type, arg0, arg1);
11981       if (INTEGRAL_TYPE_P (type)
11982           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11983         return omit_one_operand (type, arg1, arg0);
11984       tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11985       if (tem)
11986         return tem;
11987       goto associate;
11988
11989     case MAX_EXPR:
11990       if (operand_equal_p (arg0, arg1, 0))
11991         return omit_one_operand (type, arg0, arg1);
11992       if (INTEGRAL_TYPE_P (type)
11993           && TYPE_MAX_VALUE (type)
11994           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11995         return omit_one_operand (type, arg1, arg0);
11996       tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11997       if (tem)
11998         return tem;
11999       goto associate;
12000
12001     case TRUTH_ANDIF_EXPR:
12002       /* Note that the operands of this must be ints
12003          and their values must be 0 or 1.
12004          ("true" is a fixed value perhaps depending on the language.)  */
12005       /* If first arg is constant zero, return it.  */
12006       if (integer_zerop (arg0))
12007         return fold_convert (type, arg0);
12008     case TRUTH_AND_EXPR:
12009       /* If either arg is constant true, drop it.  */
12010       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12011         return non_lvalue (fold_convert (type, arg1));
12012       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12013           /* Preserve sequence points.  */
12014           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12015         return non_lvalue (fold_convert (type, arg0));
12016       /* If second arg is constant zero, result is zero, but first arg
12017          must be evaluated.  */
12018       if (integer_zerop (arg1))
12019         return omit_one_operand (type, arg1, arg0);
12020       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12021          case will be handled here.  */
12022       if (integer_zerop (arg0))
12023         return omit_one_operand (type, arg0, arg1);
12024
12025       /* !X && X is always false.  */
12026       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12027           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12028         return omit_one_operand (type, integer_zero_node, arg1);
12029       /* X && !X is always false.  */
12030       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12031           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12032         return omit_one_operand (type, integer_zero_node, arg0);
12033
12034       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
12035          means A >= Y && A != MAX, but in this case we know that
12036          A < X <= MAX.  */
12037
12038       if (!TREE_SIDE_EFFECTS (arg0)
12039           && !TREE_SIDE_EFFECTS (arg1))
12040         {
12041           tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
12042           if (tem && !operand_equal_p (tem, arg0, 0))
12043             return fold_build2 (code, type, tem, arg1);
12044
12045           tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
12046           if (tem && !operand_equal_p (tem, arg1, 0))
12047             return fold_build2 (code, type, arg0, tem);
12048         }
12049
12050     truth_andor:
12051       /* We only do these simplifications if we are optimizing.  */
12052       if (!optimize)
12053         return NULL_TREE;
12054
12055       /* Check for things like (A || B) && (A || C).  We can convert this
12056          to A || (B && C).  Note that either operator can be any of the four
12057          truth and/or operations and the transformation will still be
12058          valid.   Also note that we only care about order for the
12059          ANDIF and ORIF operators.  If B contains side effects, this
12060          might change the truth-value of A.  */
12061       if (TREE_CODE (arg0) == TREE_CODE (arg1)
12062           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12063               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12064               || TREE_CODE (arg0) == TRUTH_AND_EXPR
12065               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12066           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12067         {
12068           tree a00 = TREE_OPERAND (arg0, 0);
12069           tree a01 = TREE_OPERAND (arg0, 1);
12070           tree a10 = TREE_OPERAND (arg1, 0);
12071           tree a11 = TREE_OPERAND (arg1, 1);
12072           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12073                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12074                              && (code == TRUTH_AND_EXPR
12075                                  || code == TRUTH_OR_EXPR));
12076
12077           if (operand_equal_p (a00, a10, 0))
12078             return fold_build2 (TREE_CODE (arg0), type, a00,
12079                                 fold_build2 (code, type, a01, a11));
12080           else if (commutative && operand_equal_p (a00, a11, 0))
12081             return fold_build2 (TREE_CODE (arg0), type, a00,
12082                                 fold_build2 (code, type, a01, a10));
12083           else if (commutative && operand_equal_p (a01, a10, 0))
12084             return fold_build2 (TREE_CODE (arg0), type, a01,
12085                                 fold_build2 (code, type, a00, a11));
12086
12087           /* This case if tricky because we must either have commutative
12088              operators or else A10 must not have side-effects.  */
12089
12090           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12091                    && operand_equal_p (a01, a11, 0))
12092             return fold_build2 (TREE_CODE (arg0), type,
12093                                 fold_build2 (code, type, a00, a10),
12094                                 a01);
12095         }
12096
12097       /* See if we can build a range comparison.  */
12098       if (0 != (tem = fold_range_test (code, type, op0, op1)))
12099         return tem;
12100
12101       /* Check for the possibility of merging component references.  If our
12102          lhs is another similar operation, try to merge its rhs with our
12103          rhs.  Then try to merge our lhs and rhs.  */
12104       if (TREE_CODE (arg0) == code
12105           && 0 != (tem = fold_truthop (code, type,
12106                                        TREE_OPERAND (arg0, 1), arg1)))
12107         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12108
12109       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
12110         return tem;
12111
12112       return NULL_TREE;
12113
12114     case TRUTH_ORIF_EXPR:
12115       /* Note that the operands of this must be ints
12116          and their values must be 0 or true.
12117          ("true" is a fixed value perhaps depending on the language.)  */
12118       /* If first arg is constant true, return it.  */
12119       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12120         return fold_convert (type, arg0);
12121     case TRUTH_OR_EXPR:
12122       /* If either arg is constant zero, drop it.  */
12123       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12124         return non_lvalue (fold_convert (type, arg1));
12125       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12126           /* Preserve sequence points.  */
12127           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12128         return non_lvalue (fold_convert (type, arg0));
12129       /* If second arg is constant true, result is true, but we must
12130          evaluate first arg.  */
12131       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12132         return omit_one_operand (type, arg1, arg0);
12133       /* Likewise for first arg, but note this only occurs here for
12134          TRUTH_OR_EXPR.  */
12135       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12136         return omit_one_operand (type, arg0, arg1);
12137
12138       /* !X || X is always true.  */
12139       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12140           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12141         return omit_one_operand (type, integer_one_node, arg1);
12142       /* X || !X is always true.  */
12143       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12144           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12145         return omit_one_operand (type, integer_one_node, arg0);
12146
12147       goto truth_andor;
12148
12149     case TRUTH_XOR_EXPR:
12150       /* If the second arg is constant zero, drop it.  */
12151       if (integer_zerop (arg1))
12152         return non_lvalue (fold_convert (type, arg0));
12153       /* If the second arg is constant true, this is a logical inversion.  */
12154       if (integer_onep (arg1))
12155         {
12156           /* Only call invert_truthvalue if operand is a truth value.  */
12157           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12158             tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12159           else
12160             tem = invert_truthvalue (arg0);
12161           return non_lvalue (fold_convert (type, tem));
12162         }
12163       /* Identical arguments cancel to zero.  */
12164       if (operand_equal_p (arg0, arg1, 0))
12165         return omit_one_operand (type, integer_zero_node, arg0);
12166
12167       /* !X ^ X is always true.  */
12168       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12169           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12170         return omit_one_operand (type, integer_one_node, arg1);
12171
12172       /* X ^ !X is always true.  */
12173       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12174           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12175         return omit_one_operand (type, integer_one_node, arg0);
12176
12177       return NULL_TREE;
12178
12179     case EQ_EXPR:
12180     case NE_EXPR:
12181       tem = fold_comparison (code, type, op0, op1);
12182       if (tem != NULL_TREE)
12183         return tem;
12184
12185       /* bool_var != 0 becomes bool_var. */
12186       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12187           && code == NE_EXPR)
12188         return non_lvalue (fold_convert (type, arg0));
12189
12190       /* bool_var == 1 becomes bool_var. */
12191       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12192           && code == EQ_EXPR)
12193         return non_lvalue (fold_convert (type, arg0));
12194
12195       /* bool_var != 1 becomes !bool_var. */
12196       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12197           && code == NE_EXPR)
12198         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12199
12200       /* bool_var == 0 becomes !bool_var. */
12201       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12202           && code == EQ_EXPR)
12203         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12204
12205       /* If this is an equality comparison of the address of two non-weak,
12206          unaliased symbols neither of which are extern (since we do not
12207          have access to attributes for externs), then we know the result.  */
12208       if (TREE_CODE (arg0) == ADDR_EXPR
12209           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12210           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12211           && ! lookup_attribute ("alias",
12212                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12213           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12214           && TREE_CODE (arg1) == ADDR_EXPR
12215           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12216           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12217           && ! lookup_attribute ("alias",
12218                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12219           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12220         {
12221           /* We know that we're looking at the address of two
12222              non-weak, unaliased, static _DECL nodes.
12223
12224              It is both wasteful and incorrect to call operand_equal_p
12225              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12226              all we need to do is test pointer equality for the arguments
12227              to the two ADDR_EXPR nodes.  It is incorrect to use
12228              operand_equal_p as that function is NOT equivalent to a
12229              C equality test.  It can in fact return false for two
12230              objects which would test as equal using the C equality
12231              operator.  */
12232           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12233           return constant_boolean_node (equal
12234                                         ? code == EQ_EXPR : code != EQ_EXPR,
12235                                         type);
12236         }
12237
12238       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12239          a MINUS_EXPR of a constant, we can convert it into a comparison with
12240          a revised constant as long as no overflow occurs.  */
12241       if (TREE_CODE (arg1) == INTEGER_CST
12242           && (TREE_CODE (arg0) == PLUS_EXPR
12243               || TREE_CODE (arg0) == MINUS_EXPR)
12244           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12245           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12246                                       ? MINUS_EXPR : PLUS_EXPR,
12247                                       fold_convert (TREE_TYPE (arg0), arg1),
12248                                       TREE_OPERAND (arg0, 1), 0))
12249           && !TREE_OVERFLOW (tem))
12250         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12251
12252       /* Similarly for a NEGATE_EXPR.  */
12253       if (TREE_CODE (arg0) == NEGATE_EXPR
12254           && TREE_CODE (arg1) == INTEGER_CST
12255           && 0 != (tem = negate_expr (arg1))
12256           && TREE_CODE (tem) == INTEGER_CST
12257           && !TREE_OVERFLOW (tem))
12258         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12259
12260       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12261       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12262           && TREE_CODE (arg1) == INTEGER_CST
12263           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12264         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12265                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
12266                                          fold_convert (TREE_TYPE (arg0), arg1),
12267                                          TREE_OPERAND (arg0, 1)));
12268
12269       /* Transform comparisons of the form X +- C CMP X.  */
12270       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12271           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12272           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12273           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12274               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12275         {
12276           tree cst = TREE_OPERAND (arg0, 1);
12277
12278           if (code == EQ_EXPR
12279               && !integer_zerop (cst))
12280             return omit_two_operands (type, boolean_false_node,
12281                                       TREE_OPERAND (arg0, 0), arg1);
12282           else
12283             return omit_two_operands (type, boolean_true_node,
12284                                       TREE_OPERAND (arg0, 0), arg1);
12285         }
12286
12287       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12288          for !=.  Don't do this for ordered comparisons due to overflow.  */
12289       if (TREE_CODE (arg0) == MINUS_EXPR
12290           && integer_zerop (arg1))
12291         return fold_build2 (code, type,
12292                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12293
12294       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12295       if (TREE_CODE (arg0) == ABS_EXPR
12296           && (integer_zerop (arg1) || real_zerop (arg1)))
12297         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
12298
12299       /* If this is an EQ or NE comparison with zero and ARG0 is
12300          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12301          two operations, but the latter can be done in one less insn
12302          on machines that have only two-operand insns or on which a
12303          constant cannot be the first operand.  */
12304       if (TREE_CODE (arg0) == BIT_AND_EXPR
12305           && integer_zerop (arg1))
12306         {
12307           tree arg00 = TREE_OPERAND (arg0, 0);
12308           tree arg01 = TREE_OPERAND (arg0, 1);
12309           if (TREE_CODE (arg00) == LSHIFT_EXPR
12310               && integer_onep (TREE_OPERAND (arg00, 0)))
12311             {
12312               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
12313                                       arg01, TREE_OPERAND (arg00, 1));
12314               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12315                                  build_int_cst (TREE_TYPE (arg0), 1));
12316               return fold_build2 (code, type,
12317                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
12318             }
12319           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12320                    && integer_onep (TREE_OPERAND (arg01, 0)))
12321             {
12322               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
12323                                       arg00, TREE_OPERAND (arg01, 1));
12324               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12325                                  build_int_cst (TREE_TYPE (arg0), 1));
12326               return fold_build2 (code, type,
12327                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
12328             }
12329         }
12330
12331       /* If this is an NE or EQ comparison of zero against the result of a
12332          signed MOD operation whose second operand is a power of 2, make
12333          the MOD operation unsigned since it is simpler and equivalent.  */
12334       if (integer_zerop (arg1)
12335           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12336           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12337               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12338               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12339               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12340           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12341         {
12342           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12343           tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
12344                                      fold_convert (newtype,
12345                                                    TREE_OPERAND (arg0, 0)),
12346                                      fold_convert (newtype,
12347                                                    TREE_OPERAND (arg0, 1)));
12348
12349           return fold_build2 (code, type, newmod,
12350                               fold_convert (newtype, arg1));
12351         }
12352
12353       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12354          C1 is a valid shift constant, and C2 is a power of two, i.e.
12355          a single bit.  */
12356       if (TREE_CODE (arg0) == BIT_AND_EXPR
12357           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12358           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12359              == INTEGER_CST
12360           && integer_pow2p (TREE_OPERAND (arg0, 1))
12361           && integer_zerop (arg1))
12362         {
12363           tree itype = TREE_TYPE (arg0);
12364           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12365           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12366
12367           /* Check for a valid shift count.  */
12368           if (TREE_INT_CST_HIGH (arg001) == 0
12369               && TREE_INT_CST_LOW (arg001) < prec)
12370             {
12371               tree arg01 = TREE_OPERAND (arg0, 1);
12372               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12373               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12374               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12375                  can be rewritten as (X & (C2 << C1)) != 0.  */
12376               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12377                 {
12378                   tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
12379                   tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
12380                   return fold_build2 (code, type, tem, arg1);
12381                 }
12382               /* Otherwise, for signed (arithmetic) shifts,
12383                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12384                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12385               else if (!TYPE_UNSIGNED (itype))
12386                 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12387                                     arg000, build_int_cst (itype, 0));
12388               /* Otherwise, of unsigned (logical) shifts,
12389                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12390                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12391               else
12392                 return omit_one_operand (type,
12393                                          code == EQ_EXPR ? integer_one_node
12394                                                          : integer_zero_node,
12395                                          arg000);
12396             }
12397         }
12398
12399       /* If this is an NE comparison of zero with an AND of one, remove the
12400          comparison since the AND will give the correct value.  */
12401       if (code == NE_EXPR
12402           && integer_zerop (arg1)
12403           && TREE_CODE (arg0) == BIT_AND_EXPR
12404           && integer_onep (TREE_OPERAND (arg0, 1)))
12405         return fold_convert (type, arg0);
12406
12407       /* If we have (A & C) == C where C is a power of 2, convert this into
12408          (A & C) != 0.  Similarly for NE_EXPR.  */
12409       if (TREE_CODE (arg0) == BIT_AND_EXPR
12410           && integer_pow2p (TREE_OPERAND (arg0, 1))
12411           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12412         return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12413                             arg0, fold_convert (TREE_TYPE (arg0),
12414                                                 integer_zero_node));
12415
12416       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12417          bit, then fold the expression into A < 0 or A >= 0.  */
12418       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
12419       if (tem)
12420         return tem;
12421
12422       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12423          Similarly for NE_EXPR.  */
12424       if (TREE_CODE (arg0) == BIT_AND_EXPR
12425           && TREE_CODE (arg1) == INTEGER_CST
12426           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12427         {
12428           tree notc = fold_build1 (BIT_NOT_EXPR,
12429                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12430                                    TREE_OPERAND (arg0, 1));
12431           tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12432                                        arg1, notc);
12433           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12434           if (integer_nonzerop (dandnotc))
12435             return omit_one_operand (type, rslt, arg0);
12436         }
12437
12438       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12439          Similarly for NE_EXPR.  */
12440       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12441           && TREE_CODE (arg1) == INTEGER_CST
12442           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12443         {
12444           tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12445           tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12446                                        TREE_OPERAND (arg0, 1), notd);
12447           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12448           if (integer_nonzerop (candnotd))
12449             return omit_one_operand (type, rslt, arg0);
12450         }
12451
12452       /* If this is a comparison of a field, we may be able to simplify it.  */
12453       if ((TREE_CODE (arg0) == COMPONENT_REF
12454            || TREE_CODE (arg0) == BIT_FIELD_REF)
12455           /* Handle the constant case even without -O
12456              to make sure the warnings are given.  */
12457           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12458         {
12459           t1 = optimize_bit_field_compare (code, type, arg0, arg1);
12460           if (t1)
12461             return t1;
12462         }
12463
12464       /* Optimize comparisons of strlen vs zero to a compare of the
12465          first character of the string vs zero.  To wit,
12466                 strlen(ptr) == 0   =>  *ptr == 0
12467                 strlen(ptr) != 0   =>  *ptr != 0
12468          Other cases should reduce to one of these two (or a constant)
12469          due to the return value of strlen being unsigned.  */
12470       if (TREE_CODE (arg0) == CALL_EXPR
12471           && integer_zerop (arg1))
12472         {
12473           tree fndecl = get_callee_fndecl (arg0);
12474
12475           if (fndecl
12476               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12477               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12478               && call_expr_nargs (arg0) == 1
12479               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12480             {
12481               tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
12482               return fold_build2 (code, type, iref,
12483                                   build_int_cst (TREE_TYPE (iref), 0));
12484             }
12485         }
12486
12487       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12488          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12489       if (TREE_CODE (arg0) == RSHIFT_EXPR
12490           && integer_zerop (arg1)
12491           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12492         {
12493           tree arg00 = TREE_OPERAND (arg0, 0);
12494           tree arg01 = TREE_OPERAND (arg0, 1);
12495           tree itype = TREE_TYPE (arg00);
12496           if (TREE_INT_CST_HIGH (arg01) == 0
12497               && TREE_INT_CST_LOW (arg01)
12498                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12499             {
12500               if (TYPE_UNSIGNED (itype))
12501                 {
12502                   itype = signed_type_for (itype);
12503                   arg00 = fold_convert (itype, arg00);
12504                 }
12505               return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12506                                   type, arg00, build_int_cst (itype, 0));
12507             }
12508         }
12509
12510       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12511       if (integer_zerop (arg1)
12512           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12513         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12514                             TREE_OPERAND (arg0, 1));
12515
12516       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12517       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12518           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12519         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12520                             build_int_cst (TREE_TYPE (arg1), 0));
12521       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12522       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12523           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12524           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12525         return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
12526                             build_int_cst (TREE_TYPE (arg1), 0));
12527
12528       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12529       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12530           && TREE_CODE (arg1) == INTEGER_CST
12531           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12532         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12533                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
12534                                          TREE_OPERAND (arg0, 1), arg1));
12535
12536       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12537          (X & C) == 0 when C is a single bit.  */
12538       if (TREE_CODE (arg0) == BIT_AND_EXPR
12539           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12540           && integer_zerop (arg1)
12541           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12542         {
12543           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12544                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12545                              TREE_OPERAND (arg0, 1));
12546           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12547                               type, tem, arg1);
12548         }
12549
12550       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12551          constant C is a power of two, i.e. a single bit.  */
12552       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12553           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12554           && integer_zerop (arg1)
12555           && integer_pow2p (TREE_OPERAND (arg0, 1))
12556           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12557                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12558         {
12559           tree arg00 = TREE_OPERAND (arg0, 0);
12560           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12561                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12562         }
12563
12564       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12565          when is C is a power of two, i.e. a single bit.  */
12566       if (TREE_CODE (arg0) == BIT_AND_EXPR
12567           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12568           && integer_zerop (arg1)
12569           && integer_pow2p (TREE_OPERAND (arg0, 1))
12570           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12571                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12572         {
12573           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12574           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12575                              arg000, TREE_OPERAND (arg0, 1));
12576           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12577                               tem, build_int_cst (TREE_TYPE (tem), 0));
12578         }
12579
12580       if (integer_zerop (arg1)
12581           && tree_expr_nonzero_p (arg0))
12582         {
12583           tree res = constant_boolean_node (code==NE_EXPR, type);
12584           return omit_one_operand (type, res, arg0);
12585         }
12586
12587       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12588       if (TREE_CODE (arg0) == NEGATE_EXPR
12589           && TREE_CODE (arg1) == NEGATE_EXPR)
12590         return fold_build2 (code, type,
12591                             TREE_OPERAND (arg0, 0),
12592                             TREE_OPERAND (arg1, 0));
12593
12594       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12595       if (TREE_CODE (arg0) == BIT_AND_EXPR
12596           && TREE_CODE (arg1) == BIT_AND_EXPR)
12597         {
12598           tree arg00 = TREE_OPERAND (arg0, 0);
12599           tree arg01 = TREE_OPERAND (arg0, 1);
12600           tree arg10 = TREE_OPERAND (arg1, 0);
12601           tree arg11 = TREE_OPERAND (arg1, 1);
12602           tree itype = TREE_TYPE (arg0);
12603
12604           if (operand_equal_p (arg01, arg11, 0))
12605             return fold_build2 (code, type,
12606                                 fold_build2 (BIT_AND_EXPR, itype,
12607                                              fold_build2 (BIT_XOR_EXPR, itype,
12608                                                           arg00, arg10),
12609                                              arg01),
12610                                 build_int_cst (itype, 0));
12611
12612           if (operand_equal_p (arg01, arg10, 0))
12613             return fold_build2 (code, type,
12614                                 fold_build2 (BIT_AND_EXPR, itype,
12615                                              fold_build2 (BIT_XOR_EXPR, itype,
12616                                                           arg00, arg11),
12617                                              arg01),
12618                                 build_int_cst (itype, 0));
12619
12620           if (operand_equal_p (arg00, arg11, 0))
12621             return fold_build2 (code, type,
12622                                 fold_build2 (BIT_AND_EXPR, itype,
12623                                              fold_build2 (BIT_XOR_EXPR, itype,
12624                                                           arg01, arg10),
12625                                              arg00),
12626                                 build_int_cst (itype, 0));
12627
12628           if (operand_equal_p (arg00, arg10, 0))
12629             return fold_build2 (code, type,
12630                                 fold_build2 (BIT_AND_EXPR, itype,
12631                                              fold_build2 (BIT_XOR_EXPR, itype,
12632                                                           arg01, arg11),
12633                                              arg00),
12634                                 build_int_cst (itype, 0));
12635         }
12636
12637       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12638           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12639         {
12640           tree arg00 = TREE_OPERAND (arg0, 0);
12641           tree arg01 = TREE_OPERAND (arg0, 1);
12642           tree arg10 = TREE_OPERAND (arg1, 0);
12643           tree arg11 = TREE_OPERAND (arg1, 1);
12644           tree itype = TREE_TYPE (arg0);
12645
12646           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12647              operand_equal_p guarantees no side-effects so we don't need
12648              to use omit_one_operand on Z.  */
12649           if (operand_equal_p (arg01, arg11, 0))
12650             return fold_build2 (code, type, arg00, arg10);
12651           if (operand_equal_p (arg01, arg10, 0))
12652             return fold_build2 (code, type, arg00, arg11);
12653           if (operand_equal_p (arg00, arg11, 0))
12654             return fold_build2 (code, type, arg01, arg10);
12655           if (operand_equal_p (arg00, arg10, 0))
12656             return fold_build2 (code, type, arg01, arg11);
12657
12658           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12659           if (TREE_CODE (arg01) == INTEGER_CST
12660               && TREE_CODE (arg11) == INTEGER_CST)
12661             return fold_build2 (code, type,
12662                                 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12663                                              fold_build2 (BIT_XOR_EXPR, itype,
12664                                                           arg01, arg11)),
12665                                 arg10);
12666         }
12667
12668       /* Attempt to simplify equality/inequality comparisons of complex
12669          values.  Only lower the comparison if the result is known or
12670          can be simplified to a single scalar comparison.  */
12671       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12672            || TREE_CODE (arg0) == COMPLEX_CST)
12673           && (TREE_CODE (arg1) == COMPLEX_EXPR
12674               || TREE_CODE (arg1) == COMPLEX_CST))
12675         {
12676           tree real0, imag0, real1, imag1;
12677           tree rcond, icond;
12678
12679           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12680             {
12681               real0 = TREE_OPERAND (arg0, 0);
12682               imag0 = TREE_OPERAND (arg0, 1);
12683             }
12684           else
12685             {
12686               real0 = TREE_REALPART (arg0);
12687               imag0 = TREE_IMAGPART (arg0);
12688             }
12689
12690           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12691             {
12692               real1 = TREE_OPERAND (arg1, 0);
12693               imag1 = TREE_OPERAND (arg1, 1);
12694             }
12695           else
12696             {
12697               real1 = TREE_REALPART (arg1);
12698               imag1 = TREE_IMAGPART (arg1);
12699             }
12700
12701           rcond = fold_binary (code, type, real0, real1);
12702           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12703             {
12704               if (integer_zerop (rcond))
12705                 {
12706                   if (code == EQ_EXPR)
12707                     return omit_two_operands (type, boolean_false_node,
12708                                               imag0, imag1);
12709                   return fold_build2 (NE_EXPR, type, imag0, imag1);
12710                 }
12711               else
12712                 {
12713                   if (code == NE_EXPR)
12714                     return omit_two_operands (type, boolean_true_node,
12715                                               imag0, imag1);
12716                   return fold_build2 (EQ_EXPR, type, imag0, imag1);
12717                 }
12718             }
12719
12720           icond = fold_binary (code, type, imag0, imag1);
12721           if (icond && TREE_CODE (icond) == INTEGER_CST)
12722             {
12723               if (integer_zerop (icond))
12724                 {
12725                   if (code == EQ_EXPR)
12726                     return omit_two_operands (type, boolean_false_node,
12727                                               real0, real1);
12728                   return fold_build2 (NE_EXPR, type, real0, real1);
12729                 }
12730               else
12731                 {
12732                   if (code == NE_EXPR)
12733                     return omit_two_operands (type, boolean_true_node,
12734                                               real0, real1);
12735                   return fold_build2 (EQ_EXPR, type, real0, real1);
12736                 }
12737             }
12738         }
12739
12740       return NULL_TREE;
12741
12742     case LT_EXPR:
12743     case GT_EXPR:
12744     case LE_EXPR:
12745     case GE_EXPR:
12746       tem = fold_comparison (code, type, op0, op1);
12747       if (tem != NULL_TREE)
12748         return tem;
12749
12750       /* Transform comparisons of the form X +- C CMP X.  */
12751       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12752           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12753           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12754                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12755               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12756                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12757         {
12758           tree arg01 = TREE_OPERAND (arg0, 1);
12759           enum tree_code code0 = TREE_CODE (arg0);
12760           int is_positive;
12761
12762           if (TREE_CODE (arg01) == REAL_CST)
12763             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12764           else
12765             is_positive = tree_int_cst_sgn (arg01);
12766
12767           /* (X - c) > X becomes false.  */
12768           if (code == GT_EXPR
12769               && ((code0 == MINUS_EXPR && is_positive >= 0)
12770                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12771             {
12772               if (TREE_CODE (arg01) == INTEGER_CST
12773                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12774                 fold_overflow_warning (("assuming signed overflow does not "
12775                                         "occur when assuming that (X - c) > X "
12776                                         "is always false"),
12777                                        WARN_STRICT_OVERFLOW_ALL);
12778               return constant_boolean_node (0, type);
12779             }
12780
12781           /* Likewise (X + c) < X becomes false.  */
12782           if (code == LT_EXPR
12783               && ((code0 == PLUS_EXPR && is_positive >= 0)
12784                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12785             {
12786               if (TREE_CODE (arg01) == INTEGER_CST
12787                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12788                 fold_overflow_warning (("assuming signed overflow does not "
12789                                         "occur when assuming that "
12790                                         "(X + c) < X is always false"),
12791                                        WARN_STRICT_OVERFLOW_ALL);
12792               return constant_boolean_node (0, type);
12793             }
12794
12795           /* Convert (X - c) <= X to true.  */
12796           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12797               && code == LE_EXPR
12798               && ((code0 == MINUS_EXPR && is_positive >= 0)
12799                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12800             {
12801               if (TREE_CODE (arg01) == INTEGER_CST
12802                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12803                 fold_overflow_warning (("assuming signed overflow does not "
12804                                         "occur when assuming that "
12805                                         "(X - c) <= X is always true"),
12806                                        WARN_STRICT_OVERFLOW_ALL);
12807               return constant_boolean_node (1, type);
12808             }
12809
12810           /* Convert (X + c) >= X to true.  */
12811           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12812               && code == GE_EXPR
12813               && ((code0 == PLUS_EXPR && is_positive >= 0)
12814                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12815             {
12816               if (TREE_CODE (arg01) == INTEGER_CST
12817                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12818                 fold_overflow_warning (("assuming signed overflow does not "
12819                                         "occur when assuming that "
12820                                         "(X + c) >= X is always true"),
12821                                        WARN_STRICT_OVERFLOW_ALL);
12822               return constant_boolean_node (1, type);
12823             }
12824
12825           if (TREE_CODE (arg01) == INTEGER_CST)
12826             {
12827               /* Convert X + c > X and X - c < X to true for integers.  */
12828               if (code == GT_EXPR
12829                   && ((code0 == PLUS_EXPR && is_positive > 0)
12830                       || (code0 == MINUS_EXPR && is_positive < 0)))
12831                 {
12832                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12833                     fold_overflow_warning (("assuming signed overflow does "
12834                                             "not occur when assuming that "
12835                                             "(X + c) > X is always true"),
12836                                            WARN_STRICT_OVERFLOW_ALL);
12837                   return constant_boolean_node (1, type);
12838                 }
12839
12840               if (code == LT_EXPR
12841                   && ((code0 == MINUS_EXPR && is_positive > 0)
12842                       || (code0 == PLUS_EXPR && is_positive < 0)))
12843                 {
12844                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12845                     fold_overflow_warning (("assuming signed overflow does "
12846                                             "not occur when assuming that "
12847                                             "(X - c) < X is always true"),
12848                                            WARN_STRICT_OVERFLOW_ALL);
12849                   return constant_boolean_node (1, type);
12850                 }
12851
12852               /* Convert X + c <= X and X - c >= X to false for integers.  */
12853               if (code == LE_EXPR
12854                   && ((code0 == PLUS_EXPR && is_positive > 0)
12855                       || (code0 == MINUS_EXPR && is_positive < 0)))
12856                 {
12857                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12858                     fold_overflow_warning (("assuming signed overflow does "
12859                                             "not occur when assuming that "
12860                                             "(X + c) <= X is always false"),
12861                                            WARN_STRICT_OVERFLOW_ALL);
12862                   return constant_boolean_node (0, type);
12863                 }
12864
12865               if (code == GE_EXPR
12866                   && ((code0 == MINUS_EXPR && is_positive > 0)
12867                       || (code0 == PLUS_EXPR && is_positive < 0)))
12868                 {
12869                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12870                     fold_overflow_warning (("assuming signed overflow does "
12871                                             "not occur when assuming that "
12872                                             "(X - c) >= X is always false"),
12873                                            WARN_STRICT_OVERFLOW_ALL);
12874                   return constant_boolean_node (0, type);
12875                 }
12876             }
12877         }
12878
12879       /* Comparisons with the highest or lowest possible integer of
12880          the specified precision will have known values.  */
12881       {
12882         tree arg1_type = TREE_TYPE (arg1);
12883         unsigned int width = TYPE_PRECISION (arg1_type);
12884
12885         if (TREE_CODE (arg1) == INTEGER_CST
12886             && width <= 2 * HOST_BITS_PER_WIDE_INT
12887             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12888           {
12889             HOST_WIDE_INT signed_max_hi;
12890             unsigned HOST_WIDE_INT signed_max_lo;
12891             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12892
12893             if (width <= HOST_BITS_PER_WIDE_INT)
12894               {
12895                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12896                                 - 1;
12897                 signed_max_hi = 0;
12898                 max_hi = 0;
12899
12900                 if (TYPE_UNSIGNED (arg1_type))
12901                   {
12902                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12903                     min_lo = 0;
12904                     min_hi = 0;
12905                   }
12906                 else
12907                   {
12908                     max_lo = signed_max_lo;
12909                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12910                     min_hi = -1;
12911                   }
12912               }
12913             else
12914               {
12915                 width -= HOST_BITS_PER_WIDE_INT;
12916                 signed_max_lo = -1;
12917                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12918                                 - 1;
12919                 max_lo = -1;
12920                 min_lo = 0;
12921
12922                 if (TYPE_UNSIGNED (arg1_type))
12923                   {
12924                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12925                     min_hi = 0;
12926                   }
12927                 else
12928                   {
12929                     max_hi = signed_max_hi;
12930                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12931                   }
12932               }
12933
12934             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12935                 && TREE_INT_CST_LOW (arg1) == max_lo)
12936               switch (code)
12937                 {
12938                 case GT_EXPR:
12939                   return omit_one_operand (type, integer_zero_node, arg0);
12940
12941                 case GE_EXPR:
12942                   return fold_build2 (EQ_EXPR, type, op0, op1);
12943
12944                 case LE_EXPR:
12945                   return omit_one_operand (type, integer_one_node, arg0);
12946
12947                 case LT_EXPR:
12948                   return fold_build2 (NE_EXPR, type, op0, op1);
12949
12950                 /* The GE_EXPR and LT_EXPR cases above are not normally
12951                    reached because of previous transformations.  */
12952
12953                 default:
12954                   break;
12955                 }
12956             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12957                      == max_hi
12958                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12959               switch (code)
12960                 {
12961                 case GT_EXPR:
12962                   arg1 = const_binop (PLUS_EXPR, arg1,
12963                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12964                   return fold_build2 (EQ_EXPR, type,
12965                                       fold_convert (TREE_TYPE (arg1), arg0),
12966                                       arg1);
12967                 case LE_EXPR:
12968                   arg1 = const_binop (PLUS_EXPR, arg1,
12969                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12970                   return fold_build2 (NE_EXPR, type,
12971                                       fold_convert (TREE_TYPE (arg1), arg0),
12972                                       arg1);
12973                 default:
12974                   break;
12975                 }
12976             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12977                      == min_hi
12978                      && TREE_INT_CST_LOW (arg1) == min_lo)
12979               switch (code)
12980                 {
12981                 case LT_EXPR:
12982                   return omit_one_operand (type, integer_zero_node, arg0);
12983
12984                 case LE_EXPR:
12985                   return fold_build2 (EQ_EXPR, type, op0, op1);
12986
12987                 case GE_EXPR:
12988                   return omit_one_operand (type, integer_one_node, arg0);
12989
12990                 case GT_EXPR:
12991                   return fold_build2 (NE_EXPR, type, op0, op1);
12992
12993                 default:
12994                   break;
12995                 }
12996             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12997                      == min_hi
12998                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12999               switch (code)
13000                 {
13001                 case GE_EXPR:
13002                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13003                   return fold_build2 (NE_EXPR, type,
13004                                       fold_convert (TREE_TYPE (arg1), arg0),
13005                                       arg1);
13006                 case LT_EXPR:
13007                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13008                   return fold_build2 (EQ_EXPR, type,
13009                                       fold_convert (TREE_TYPE (arg1), arg0),
13010                                       arg1);
13011                 default:
13012                   break;
13013                 }
13014
13015             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13016                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
13017                      && TYPE_UNSIGNED (arg1_type)
13018                      /* We will flip the signedness of the comparison operator
13019                         associated with the mode of arg1, so the sign bit is
13020                         specified by this mode.  Check that arg1 is the signed
13021                         max associated with this sign bit.  */
13022                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13023                      /* signed_type does not work on pointer types.  */
13024                      && INTEGRAL_TYPE_P (arg1_type))
13025               {
13026                 /* The following case also applies to X < signed_max+1
13027                    and X >= signed_max+1 because previous transformations.  */
13028                 if (code == LE_EXPR || code == GT_EXPR)
13029                   {
13030                     tree st;
13031                     st = signed_type_for (TREE_TYPE (arg1));
13032                     return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
13033                                         type, fold_convert (st, arg0),
13034                                         build_int_cst (st, 0));
13035                   }
13036               }
13037           }
13038       }
13039
13040       /* If we are comparing an ABS_EXPR with a constant, we can
13041          convert all the cases into explicit comparisons, but they may
13042          well not be faster than doing the ABS and one comparison.
13043          But ABS (X) <= C is a range comparison, which becomes a subtraction
13044          and a comparison, and is probably faster.  */
13045       if (code == LE_EXPR
13046           && TREE_CODE (arg1) == INTEGER_CST
13047           && TREE_CODE (arg0) == ABS_EXPR
13048           && ! TREE_SIDE_EFFECTS (arg0)
13049           && (0 != (tem = negate_expr (arg1)))
13050           && TREE_CODE (tem) == INTEGER_CST
13051           && !TREE_OVERFLOW (tem))
13052         return fold_build2 (TRUTH_ANDIF_EXPR, type,
13053                             build2 (GE_EXPR, type,
13054                                     TREE_OPERAND (arg0, 0), tem),
13055                             build2 (LE_EXPR, type,
13056                                     TREE_OPERAND (arg0, 0), arg1));
13057
13058       /* Convert ABS_EXPR<x> >= 0 to true.  */
13059       strict_overflow_p = false;
13060       if (code == GE_EXPR
13061           && (integer_zerop (arg1)
13062               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13063                   && real_zerop (arg1)))
13064           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13065         {
13066           if (strict_overflow_p)
13067             fold_overflow_warning (("assuming signed overflow does not occur "
13068                                     "when simplifying comparison of "
13069                                     "absolute value and zero"),
13070                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13071           return omit_one_operand (type, integer_one_node, arg0);
13072         }
13073
13074       /* Convert ABS_EXPR<x> < 0 to false.  */
13075       strict_overflow_p = false;
13076       if (code == LT_EXPR
13077           && (integer_zerop (arg1) || real_zerop (arg1))
13078           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13079         {
13080           if (strict_overflow_p)
13081             fold_overflow_warning (("assuming signed overflow does not occur "
13082                                     "when simplifying comparison of "
13083                                     "absolute value and zero"),
13084                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13085           return omit_one_operand (type, integer_zero_node, arg0);
13086         }
13087
13088       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13089          and similarly for >= into !=.  */
13090       if ((code == LT_EXPR || code == GE_EXPR)
13091           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13092           && TREE_CODE (arg1) == LSHIFT_EXPR
13093           && integer_onep (TREE_OPERAND (arg1, 0)))
13094         return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13095                        build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13096                                TREE_OPERAND (arg1, 1)),
13097                        build_int_cst (TREE_TYPE (arg0), 0));
13098
13099       if ((code == LT_EXPR || code == GE_EXPR)
13100           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13101           && CONVERT_EXPR_P (arg1)
13102           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13103           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13104         return
13105           build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13106                   fold_convert (TREE_TYPE (arg0),
13107                                 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13108                                         TREE_OPERAND (TREE_OPERAND (arg1, 0),
13109                                                       1))),
13110                   build_int_cst (TREE_TYPE (arg0), 0));
13111
13112       return NULL_TREE;
13113
13114     case UNORDERED_EXPR:
13115     case ORDERED_EXPR:
13116     case UNLT_EXPR:
13117     case UNLE_EXPR:
13118     case UNGT_EXPR:
13119     case UNGE_EXPR:
13120     case UNEQ_EXPR:
13121     case LTGT_EXPR:
13122       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13123         {
13124           t1 = fold_relational_const (code, type, arg0, arg1);
13125           if (t1 != NULL_TREE)
13126             return t1;
13127         }
13128
13129       /* If the first operand is NaN, the result is constant.  */
13130       if (TREE_CODE (arg0) == REAL_CST
13131           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13132           && (code != LTGT_EXPR || ! flag_trapping_math))
13133         {
13134           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13135                ? integer_zero_node
13136                : integer_one_node;
13137           return omit_one_operand (type, t1, arg1);
13138         }
13139
13140       /* If the second operand is NaN, the result is constant.  */
13141       if (TREE_CODE (arg1) == REAL_CST
13142           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13143           && (code != LTGT_EXPR || ! flag_trapping_math))
13144         {
13145           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13146                ? integer_zero_node
13147                : integer_one_node;
13148           return omit_one_operand (type, t1, arg0);
13149         }
13150
13151       /* Simplify unordered comparison of something with itself.  */
13152       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13153           && operand_equal_p (arg0, arg1, 0))
13154         return constant_boolean_node (1, type);
13155
13156       if (code == LTGT_EXPR
13157           && !flag_trapping_math
13158           && operand_equal_p (arg0, arg1, 0))
13159         return constant_boolean_node (0, type);
13160
13161       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13162       {
13163         tree targ0 = strip_float_extensions (arg0);
13164         tree targ1 = strip_float_extensions (arg1);
13165         tree newtype = TREE_TYPE (targ0);
13166
13167         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13168           newtype = TREE_TYPE (targ1);
13169
13170         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13171           return fold_build2 (code, type, fold_convert (newtype, targ0),
13172                               fold_convert (newtype, targ1));
13173       }
13174
13175       return NULL_TREE;
13176
13177     case COMPOUND_EXPR:
13178       /* When pedantic, a compound expression can be neither an lvalue
13179          nor an integer constant expression.  */
13180       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13181         return NULL_TREE;
13182       /* Don't let (0, 0) be null pointer constant.  */
13183       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13184                                  : fold_convert (type, arg1);
13185       return pedantic_non_lvalue (tem);
13186
13187     case COMPLEX_EXPR:
13188       if ((TREE_CODE (arg0) == REAL_CST
13189            && TREE_CODE (arg1) == REAL_CST)
13190           || (TREE_CODE (arg0) == INTEGER_CST
13191               && TREE_CODE (arg1) == INTEGER_CST))
13192         return build_complex (type, arg0, arg1);
13193       return NULL_TREE;
13194
13195     case ASSERT_EXPR:
13196       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13197       gcc_unreachable ();
13198
13199     default:
13200       return NULL_TREE;
13201     } /* switch (code) */
13202 }
13203
13204 /* Callback for walk_tree, looking for LABEL_EXPR.
13205    Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
13206    Do not check the sub-tree of GOTO_EXPR.  */
13207
13208 static tree
13209 contains_label_1 (tree *tp,
13210                   int *walk_subtrees,
13211                   void *data ATTRIBUTE_UNUSED)
13212 {
13213   switch (TREE_CODE (*tp))
13214     {
13215     case LABEL_EXPR:
13216       return *tp;
13217     case GOTO_EXPR:
13218       *walk_subtrees = 0;
13219     /* no break */
13220     default:
13221       return NULL_TREE;
13222     }
13223 }
13224
13225 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
13226    accessible from outside the sub-tree. Returns NULL_TREE if no
13227    addressable label is found.  */
13228
13229 static bool
13230 contains_label_p (tree st)
13231 {
13232   return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
13233 }
13234
13235 /* Fold a ternary expression of code CODE and type TYPE with operands
13236    OP0, OP1, and OP2.  Return the folded expression if folding is
13237    successful.  Otherwise, return NULL_TREE.  */
13238
13239 tree
13240 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
13241 {
13242   tree tem;
13243   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13244   enum tree_code_class kind = TREE_CODE_CLASS (code);
13245
13246   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13247               && TREE_CODE_LENGTH (code) == 3);
13248
13249   /* Strip any conversions that don't change the mode.  This is safe
13250      for every expression, except for a comparison expression because
13251      its signedness is derived from its operands.  So, in the latter
13252      case, only strip conversions that don't change the signedness.
13253
13254      Note that this is done as an internal manipulation within the
13255      constant folder, in order to find the simplest representation of
13256      the arguments so that their form can be studied.  In any cases,
13257      the appropriate type conversions should be put back in the tree
13258      that will get out of the constant folder.  */
13259   if (op0)
13260     {
13261       arg0 = op0;
13262       STRIP_NOPS (arg0);
13263     }
13264
13265   if (op1)
13266     {
13267       arg1 = op1;
13268       STRIP_NOPS (arg1);
13269     }
13270
13271   switch (code)
13272     {
13273     case COMPONENT_REF:
13274       if (TREE_CODE (arg0) == CONSTRUCTOR
13275           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13276         {
13277           unsigned HOST_WIDE_INT idx;
13278           tree field, value;
13279           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13280             if (field == arg1)
13281               return value;
13282         }
13283       return NULL_TREE;
13284
13285     case COND_EXPR:
13286       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13287          so all simple results must be passed through pedantic_non_lvalue.  */
13288       if (TREE_CODE (arg0) == INTEGER_CST)
13289         {
13290           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13291           tem = integer_zerop (arg0) ? op2 : op1;
13292           /* Only optimize constant conditions when the selected branch
13293              has the same type as the COND_EXPR.  This avoids optimizing
13294              away "c ? x : throw", where the throw has a void type.
13295              Avoid throwing away that operand which contains label.  */
13296           if ((!TREE_SIDE_EFFECTS (unused_op)
13297                || !contains_label_p (unused_op))
13298               && (! VOID_TYPE_P (TREE_TYPE (tem))
13299                   || VOID_TYPE_P (type)))
13300             return pedantic_non_lvalue (tem);
13301           return NULL_TREE;
13302         }
13303       if (operand_equal_p (arg1, op2, 0))
13304         return pedantic_omit_one_operand (type, arg1, arg0);
13305
13306       /* If we have A op B ? A : C, we may be able to convert this to a
13307          simpler expression, depending on the operation and the values
13308          of B and C.  Signed zeros prevent all of these transformations,
13309          for reasons given above each one.
13310
13311          Also try swapping the arguments and inverting the conditional.  */
13312       if (COMPARISON_CLASS_P (arg0)
13313           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13314                                              arg1, TREE_OPERAND (arg0, 1))
13315           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13316         {
13317           tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
13318           if (tem)
13319             return tem;
13320         }
13321
13322       if (COMPARISON_CLASS_P (arg0)
13323           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13324                                              op2,
13325                                              TREE_OPERAND (arg0, 1))
13326           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13327         {
13328           tem = fold_truth_not_expr (arg0);
13329           if (tem && COMPARISON_CLASS_P (tem))
13330             {
13331               tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
13332               if (tem)
13333                 return tem;
13334             }
13335         }
13336
13337       /* If the second operand is simpler than the third, swap them
13338          since that produces better jump optimization results.  */
13339       if (truth_value_p (TREE_CODE (arg0))
13340           && tree_swap_operands_p (op1, op2, false))
13341         {
13342           /* See if this can be inverted.  If it can't, possibly because
13343              it was a floating-point inequality comparison, don't do
13344              anything.  */
13345           tem = fold_truth_not_expr (arg0);
13346           if (tem)
13347             return fold_build3 (code, type, tem, op2, op1);
13348         }
13349
13350       /* Convert A ? 1 : 0 to simply A.  */
13351       if (integer_onep (op1)
13352           && integer_zerop (op2)
13353           /* If we try to convert OP0 to our type, the
13354              call to fold will try to move the conversion inside
13355              a COND, which will recurse.  In that case, the COND_EXPR
13356              is probably the best choice, so leave it alone.  */
13357           && type == TREE_TYPE (arg0))
13358         return pedantic_non_lvalue (arg0);
13359
13360       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13361          over COND_EXPR in cases such as floating point comparisons.  */
13362       if (integer_zerop (op1)
13363           && integer_onep (op2)
13364           && truth_value_p (TREE_CODE (arg0)))
13365         return pedantic_non_lvalue (fold_convert (type,
13366                                                   invert_truthvalue (arg0)));
13367
13368       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13369       if (TREE_CODE (arg0) == LT_EXPR
13370           && integer_zerop (TREE_OPERAND (arg0, 1))
13371           && integer_zerop (op2)
13372           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13373         {
13374           /* sign_bit_p only checks ARG1 bits within A's precision.
13375              If <sign bit of A> has wider type than A, bits outside
13376              of A's precision in <sign bit of A> need to be checked.
13377              If they are all 0, this optimization needs to be done
13378              in unsigned A's type, if they are all 1 in signed A's type,
13379              otherwise this can't be done.  */
13380           if (TYPE_PRECISION (TREE_TYPE (tem))
13381               < TYPE_PRECISION (TREE_TYPE (arg1))
13382               && TYPE_PRECISION (TREE_TYPE (tem))
13383                  < TYPE_PRECISION (type))
13384             {
13385               unsigned HOST_WIDE_INT mask_lo;
13386               HOST_WIDE_INT mask_hi;
13387               int inner_width, outer_width;
13388               tree tem_type;
13389
13390               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13391               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13392               if (outer_width > TYPE_PRECISION (type))
13393                 outer_width = TYPE_PRECISION (type);
13394
13395               if (outer_width > HOST_BITS_PER_WIDE_INT)
13396                 {
13397                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13398                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13399                   mask_lo = -1;
13400                 }
13401               else
13402                 {
13403                   mask_hi = 0;
13404                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13405                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13406                 }
13407               if (inner_width > HOST_BITS_PER_WIDE_INT)
13408                 {
13409                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13410                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13411                   mask_lo = 0;
13412                 }
13413               else
13414                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13415                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13416
13417               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13418                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13419                 {
13420                   tem_type = signed_type_for (TREE_TYPE (tem));
13421                   tem = fold_convert (tem_type, tem);
13422                 }
13423               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13424                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13425                 {
13426                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13427                   tem = fold_convert (tem_type, tem);
13428                 }
13429               else
13430                 tem = NULL;
13431             }
13432
13433           if (tem)
13434             return fold_convert (type,
13435                                  fold_build2 (BIT_AND_EXPR,
13436                                               TREE_TYPE (tem), tem,
13437                                               fold_convert (TREE_TYPE (tem),
13438                                                             arg1)));
13439         }
13440
13441       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13442          already handled above.  */
13443       if (TREE_CODE (arg0) == BIT_AND_EXPR
13444           && integer_onep (TREE_OPERAND (arg0, 1))
13445           && integer_zerop (op2)
13446           && integer_pow2p (arg1))
13447         {
13448           tree tem = TREE_OPERAND (arg0, 0);
13449           STRIP_NOPS (tem);
13450           if (TREE_CODE (tem) == RSHIFT_EXPR
13451               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13452               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13453                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13454             return fold_build2 (BIT_AND_EXPR, type,
13455                                 TREE_OPERAND (tem, 0), arg1);
13456         }
13457
13458       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13459          is probably obsolete because the first operand should be a
13460          truth value (that's why we have the two cases above), but let's
13461          leave it in until we can confirm this for all front-ends.  */
13462       if (integer_zerop (op2)
13463           && TREE_CODE (arg0) == NE_EXPR
13464           && integer_zerop (TREE_OPERAND (arg0, 1))
13465           && integer_pow2p (arg1)
13466           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13467           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13468                               arg1, OEP_ONLY_CONST))
13469         return pedantic_non_lvalue (fold_convert (type,
13470                                                   TREE_OPERAND (arg0, 0)));
13471
13472       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13473       if (integer_zerop (op2)
13474           && truth_value_p (TREE_CODE (arg0))
13475           && truth_value_p (TREE_CODE (arg1)))
13476         return fold_build2 (TRUTH_ANDIF_EXPR, type,
13477                             fold_convert (type, arg0),
13478                             arg1);
13479
13480       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13481       if (integer_onep (op2)
13482           && truth_value_p (TREE_CODE (arg0))
13483           && truth_value_p (TREE_CODE (arg1)))
13484         {
13485           /* Only perform transformation if ARG0 is easily inverted.  */
13486           tem = fold_truth_not_expr (arg0);
13487           if (tem)
13488             return fold_build2 (TRUTH_ORIF_EXPR, type,
13489                                 fold_convert (type, tem),
13490                                 arg1);
13491         }
13492
13493       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13494       if (integer_zerop (arg1)
13495           && truth_value_p (TREE_CODE (arg0))
13496           && truth_value_p (TREE_CODE (op2)))
13497         {
13498           /* Only perform transformation if ARG0 is easily inverted.  */
13499           tem = fold_truth_not_expr (arg0);
13500           if (tem)
13501             return fold_build2 (TRUTH_ANDIF_EXPR, type,
13502                                 fold_convert (type, tem),
13503                                 op2);
13504         }
13505
13506       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13507       if (integer_onep (arg1)
13508           && truth_value_p (TREE_CODE (arg0))
13509           && truth_value_p (TREE_CODE (op2)))
13510         return fold_build2 (TRUTH_ORIF_EXPR, type,
13511                             fold_convert (type, arg0),
13512                             op2);
13513
13514       return NULL_TREE;
13515
13516     case CALL_EXPR:
13517       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13518          of fold_ternary on them.  */
13519       gcc_unreachable ();
13520
13521     case BIT_FIELD_REF:
13522       if ((TREE_CODE (arg0) == VECTOR_CST
13523            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13524           && type == TREE_TYPE (TREE_TYPE (arg0)))
13525         {
13526           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13527           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13528
13529           if (width != 0
13530               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13531               && (idx % width) == 0
13532               && (idx = idx / width)
13533                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13534             {
13535               tree elements = NULL_TREE;
13536
13537               if (TREE_CODE (arg0) == VECTOR_CST)
13538                 elements = TREE_VECTOR_CST_ELTS (arg0);
13539               else
13540                 {
13541                   unsigned HOST_WIDE_INT idx;
13542                   tree value;
13543
13544                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13545                     elements = tree_cons (NULL_TREE, value, elements);
13546                 }
13547               while (idx-- > 0 && elements)
13548                 elements = TREE_CHAIN (elements);
13549               if (elements)
13550                 return TREE_VALUE (elements);
13551               else
13552                 return fold_convert (type, integer_zero_node);
13553             }
13554         }
13555
13556       /* A bit-field-ref that referenced the full argument can be stripped.  */
13557       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13558           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13559           && integer_zerop (op2))
13560         return fold_convert (type, arg0);
13561
13562       return NULL_TREE;
13563
13564     default:
13565       return NULL_TREE;
13566     } /* switch (code) */
13567 }
13568
13569 /* Perform constant folding and related simplification of EXPR.
13570    The related simplifications include x*1 => x, x*0 => 0, etc.,
13571    and application of the associative law.
13572    NOP_EXPR conversions may be removed freely (as long as we
13573    are careful not to change the type of the overall expression).
13574    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13575    but we can constant-fold them if they have constant operands.  */
13576
13577 #ifdef ENABLE_FOLD_CHECKING
13578 # define fold(x) fold_1 (x)
13579 static tree fold_1 (tree);
13580 static
13581 #endif
13582 tree
13583 fold (tree expr)
13584 {
13585   const tree t = expr;
13586   enum tree_code code = TREE_CODE (t);
13587   enum tree_code_class kind = TREE_CODE_CLASS (code);
13588   tree tem;
13589
13590   /* Return right away if a constant.  */
13591   if (kind == tcc_constant)
13592     return t;
13593
13594   /* CALL_EXPR-like objects with variable numbers of operands are
13595      treated specially.  */
13596   if (kind == tcc_vl_exp)
13597     {
13598       if (code == CALL_EXPR)
13599         {
13600           tem = fold_call_expr (expr, false);
13601           return tem ? tem : expr;
13602         }
13603       return expr;
13604     }
13605
13606   if (IS_EXPR_CODE_CLASS (kind))
13607     {
13608       tree type = TREE_TYPE (t);
13609       tree op0, op1, op2;
13610
13611       switch (TREE_CODE_LENGTH (code))
13612         {
13613         case 1:
13614           op0 = TREE_OPERAND (t, 0);
13615           tem = fold_unary (code, type, op0);
13616           return tem ? tem : expr;
13617         case 2:
13618           op0 = TREE_OPERAND (t, 0);
13619           op1 = TREE_OPERAND (t, 1);
13620           tem = fold_binary (code, type, op0, op1);
13621           return tem ? tem : expr;
13622         case 3:
13623           op0 = TREE_OPERAND (t, 0);
13624           op1 = TREE_OPERAND (t, 1);
13625           op2 = TREE_OPERAND (t, 2);
13626           tem = fold_ternary (code, type, op0, op1, op2);
13627           return tem ? tem : expr;
13628         default:
13629           break;
13630         }
13631     }
13632
13633   switch (code)
13634     {
13635     case ARRAY_REF:
13636       {
13637         tree op0 = TREE_OPERAND (t, 0);
13638         tree op1 = TREE_OPERAND (t, 1);
13639
13640         if (TREE_CODE (op1) == INTEGER_CST
13641             && TREE_CODE (op0) == CONSTRUCTOR
13642             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13643           {
13644             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13645             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13646             unsigned HOST_WIDE_INT begin = 0;
13647
13648             /* Find a matching index by means of a binary search.  */
13649             while (begin != end)
13650               {
13651                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13652                 tree index = VEC_index (constructor_elt, elts, middle)->index;
13653
13654                 if (TREE_CODE (index) == INTEGER_CST
13655                     && tree_int_cst_lt (index, op1))
13656                   begin = middle + 1;
13657                 else if (TREE_CODE (index) == INTEGER_CST
13658                          && tree_int_cst_lt (op1, index))
13659                   end = middle;
13660                 else if (TREE_CODE (index) == RANGE_EXPR
13661                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13662                   begin = middle + 1;
13663                 else if (TREE_CODE (index) == RANGE_EXPR
13664                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13665                   end = middle;
13666                 else
13667                   return VEC_index (constructor_elt, elts, middle)->value;
13668               }
13669           }
13670
13671         return t;
13672       }
13673
13674     case CONST_DECL:
13675       return fold (DECL_INITIAL (t));
13676
13677     default:
13678       return t;
13679     } /* switch (code) */
13680 }
13681
13682 #ifdef ENABLE_FOLD_CHECKING
13683 #undef fold
13684
13685 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13686 static void fold_check_failed (const_tree, const_tree);
13687 void print_fold_checksum (const_tree);
13688
13689 /* When --enable-checking=fold, compute a digest of expr before
13690    and after actual fold call to see if fold did not accidentally
13691    change original expr.  */
13692
13693 tree
13694 fold (tree expr)
13695 {
13696   tree ret;
13697   struct md5_ctx ctx;
13698   unsigned char checksum_before[16], checksum_after[16];
13699   htab_t ht;
13700
13701   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13702   md5_init_ctx (&ctx);
13703   fold_checksum_tree (expr, &ctx, ht);
13704   md5_finish_ctx (&ctx, checksum_before);
13705   htab_empty (ht);
13706
13707   ret = fold_1 (expr);
13708
13709   md5_init_ctx (&ctx);
13710   fold_checksum_tree (expr, &ctx, ht);
13711   md5_finish_ctx (&ctx, checksum_after);
13712   htab_delete (ht);
13713
13714   if (memcmp (checksum_before, checksum_after, 16))
13715     fold_check_failed (expr, ret);
13716
13717   return ret;
13718 }
13719
13720 void
13721 print_fold_checksum (const_tree expr)
13722 {
13723   struct md5_ctx ctx;
13724   unsigned char checksum[16], cnt;
13725   htab_t ht;
13726
13727   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13728   md5_init_ctx (&ctx);
13729   fold_checksum_tree (expr, &ctx, ht);
13730   md5_finish_ctx (&ctx, checksum);
13731   htab_delete (ht);
13732   for (cnt = 0; cnt < 16; ++cnt)
13733     fprintf (stderr, "%02x", checksum[cnt]);
13734   putc ('\n', stderr);
13735 }
13736
13737 static void
13738 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13739 {
13740   internal_error ("fold check: original tree changed by fold");
13741 }
13742
13743 static void
13744 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13745 {
13746   const void **slot;
13747   enum tree_code code;
13748   union tree_node buf;
13749   int i, len;
13750   
13751 recursive_label:
13752
13753   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13754                <= sizeof (struct tree_function_decl))
13755               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13756   if (expr == NULL)
13757     return;
13758   slot = (const void **) htab_find_slot (ht, expr, INSERT);
13759   if (*slot != NULL)
13760     return;
13761   *slot = expr;
13762   code = TREE_CODE (expr);
13763   if (TREE_CODE_CLASS (code) == tcc_declaration
13764       && DECL_ASSEMBLER_NAME_SET_P (expr))
13765     {
13766       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13767       memcpy ((char *) &buf, expr, tree_size (expr));
13768       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13769       expr = (tree) &buf;
13770     }
13771   else if (TREE_CODE_CLASS (code) == tcc_type
13772            && (TYPE_POINTER_TO (expr)
13773                || TYPE_REFERENCE_TO (expr)
13774                || TYPE_CACHED_VALUES_P (expr)
13775                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13776                || TYPE_NEXT_VARIANT (expr)))
13777     {
13778       /* Allow these fields to be modified.  */
13779       tree tmp;
13780       memcpy ((char *) &buf, expr, tree_size (expr));
13781       expr = tmp = (tree) &buf;
13782       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13783       TYPE_POINTER_TO (tmp) = NULL;
13784       TYPE_REFERENCE_TO (tmp) = NULL;
13785       TYPE_NEXT_VARIANT (tmp) = NULL;
13786       if (TYPE_CACHED_VALUES_P (tmp))
13787         {
13788           TYPE_CACHED_VALUES_P (tmp) = 0;
13789           TYPE_CACHED_VALUES (tmp) = NULL;
13790         }
13791     }
13792   md5_process_bytes (expr, tree_size (expr), ctx);
13793   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13794   if (TREE_CODE_CLASS (code) != tcc_type
13795       && TREE_CODE_CLASS (code) != tcc_declaration
13796       && code != TREE_LIST
13797       && code != SSA_NAME)
13798     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13799   switch (TREE_CODE_CLASS (code))
13800     {
13801     case tcc_constant:
13802       switch (code)
13803         {
13804         case STRING_CST:
13805           md5_process_bytes (TREE_STRING_POINTER (expr),
13806                              TREE_STRING_LENGTH (expr), ctx);
13807           break;
13808         case COMPLEX_CST:
13809           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13810           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13811           break;
13812         case VECTOR_CST:
13813           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13814           break;
13815         default:
13816           break;
13817         }
13818       break;
13819     case tcc_exceptional:
13820       switch (code)
13821         {
13822         case TREE_LIST:
13823           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13824           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13825           expr = TREE_CHAIN (expr);
13826           goto recursive_label;
13827           break;
13828         case TREE_VEC:
13829           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13830             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13831           break;
13832         default:
13833           break;
13834         }
13835       break;
13836     case tcc_expression:
13837     case tcc_reference:
13838     case tcc_comparison:
13839     case tcc_unary:
13840     case tcc_binary:
13841     case tcc_statement:
13842     case tcc_vl_exp:
13843       len = TREE_OPERAND_LENGTH (expr);
13844       for (i = 0; i < len; ++i)
13845         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13846       break;
13847     case tcc_declaration:
13848       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13849       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13850       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13851         {
13852           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13853           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13854           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13855           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13856           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13857         }
13858       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13859         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13860           
13861       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13862         {
13863           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13864           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13865           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13866         }
13867       break;
13868     case tcc_type:
13869       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13870         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13871       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13872       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13873       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13874       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13875       if (INTEGRAL_TYPE_P (expr)
13876           || SCALAR_FLOAT_TYPE_P (expr))
13877         {
13878           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13879           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13880         }
13881       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13882       if (TREE_CODE (expr) == RECORD_TYPE
13883           || TREE_CODE (expr) == UNION_TYPE
13884           || TREE_CODE (expr) == QUAL_UNION_TYPE)
13885         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13886       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13887       break;
13888     default:
13889       break;
13890     }
13891 }
13892
13893 /* Helper function for outputting the checksum of a tree T.  When
13894    debugging with gdb, you can "define mynext" to be "next" followed
13895    by "call debug_fold_checksum (op0)", then just trace down till the
13896    outputs differ.  */
13897
13898 void
13899 debug_fold_checksum (const_tree t)
13900 {
13901   int i;
13902   unsigned char checksum[16];
13903   struct md5_ctx ctx;
13904   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13905   
13906   md5_init_ctx (&ctx);
13907   fold_checksum_tree (t, &ctx, ht);
13908   md5_finish_ctx (&ctx, checksum);
13909   htab_empty (ht);
13910
13911   for (i = 0; i < 16; i++)
13912     fprintf (stderr, "%d ", checksum[i]);
13913
13914   fprintf (stderr, "\n");
13915 }
13916
13917 #endif
13918
13919 /* Fold a unary tree expression with code CODE of type TYPE with an
13920    operand OP0.  Return a folded expression if successful.  Otherwise,
13921    return a tree expression with code CODE of type TYPE with an
13922    operand OP0.  */
13923
13924 tree
13925 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13926 {
13927   tree tem;
13928 #ifdef ENABLE_FOLD_CHECKING
13929   unsigned char checksum_before[16], checksum_after[16];
13930   struct md5_ctx ctx;
13931   htab_t ht;
13932
13933   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13934   md5_init_ctx (&ctx);
13935   fold_checksum_tree (op0, &ctx, ht);
13936   md5_finish_ctx (&ctx, checksum_before);
13937   htab_empty (ht);
13938 #endif
13939   
13940   tem = fold_unary (code, type, op0);
13941   if (!tem)
13942     tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13943   
13944 #ifdef ENABLE_FOLD_CHECKING
13945   md5_init_ctx (&ctx);
13946   fold_checksum_tree (op0, &ctx, ht);
13947   md5_finish_ctx (&ctx, checksum_after);
13948   htab_delete (ht);
13949
13950   if (memcmp (checksum_before, checksum_after, 16))
13951     fold_check_failed (op0, tem);
13952 #endif
13953   return tem;
13954 }
13955
13956 /* Fold a binary tree expression with code CODE of type TYPE with
13957    operands OP0 and OP1.  Return a folded expression if successful.
13958    Otherwise, return a tree expression with code CODE of type TYPE
13959    with operands OP0 and OP1.  */
13960
13961 tree
13962 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13963                   MEM_STAT_DECL)
13964 {
13965   tree tem;
13966 #ifdef ENABLE_FOLD_CHECKING
13967   unsigned char checksum_before_op0[16],
13968                 checksum_before_op1[16],
13969                 checksum_after_op0[16],
13970                 checksum_after_op1[16];
13971   struct md5_ctx ctx;
13972   htab_t ht;
13973
13974   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13975   md5_init_ctx (&ctx);
13976   fold_checksum_tree (op0, &ctx, ht);
13977   md5_finish_ctx (&ctx, checksum_before_op0);
13978   htab_empty (ht);
13979
13980   md5_init_ctx (&ctx);
13981   fold_checksum_tree (op1, &ctx, ht);
13982   md5_finish_ctx (&ctx, checksum_before_op1);
13983   htab_empty (ht);
13984 #endif
13985
13986   tem = fold_binary (code, type, op0, op1);
13987   if (!tem)
13988     tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13989   
13990 #ifdef ENABLE_FOLD_CHECKING
13991   md5_init_ctx (&ctx);
13992   fold_checksum_tree (op0, &ctx, ht);
13993   md5_finish_ctx (&ctx, checksum_after_op0);
13994   htab_empty (ht);
13995
13996   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13997     fold_check_failed (op0, tem);
13998   
13999   md5_init_ctx (&ctx);
14000   fold_checksum_tree (op1, &ctx, ht);
14001   md5_finish_ctx (&ctx, checksum_after_op1);
14002   htab_delete (ht);
14003
14004   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14005     fold_check_failed (op1, tem);
14006 #endif
14007   return tem;
14008 }
14009
14010 /* Fold a ternary tree expression with code CODE of type TYPE with
14011    operands OP0, OP1, and OP2.  Return a folded expression if
14012    successful.  Otherwise, return a tree expression with code CODE of
14013    type TYPE with operands OP0, OP1, and OP2.  */
14014
14015 tree
14016 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
14017              MEM_STAT_DECL)
14018 {
14019   tree tem;
14020 #ifdef ENABLE_FOLD_CHECKING
14021   unsigned char checksum_before_op0[16],
14022                 checksum_before_op1[16],
14023                 checksum_before_op2[16],
14024                 checksum_after_op0[16],
14025                 checksum_after_op1[16],
14026                 checksum_after_op2[16];
14027   struct md5_ctx ctx;
14028   htab_t ht;
14029
14030   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14031   md5_init_ctx (&ctx);
14032   fold_checksum_tree (op0, &ctx, ht);
14033   md5_finish_ctx (&ctx, checksum_before_op0);
14034   htab_empty (ht);
14035
14036   md5_init_ctx (&ctx);
14037   fold_checksum_tree (op1, &ctx, ht);
14038   md5_finish_ctx (&ctx, checksum_before_op1);
14039   htab_empty (ht);
14040
14041   md5_init_ctx (&ctx);
14042   fold_checksum_tree (op2, &ctx, ht);
14043   md5_finish_ctx (&ctx, checksum_before_op2);
14044   htab_empty (ht);
14045 #endif
14046
14047   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14048   tem = fold_ternary (code, type, op0, op1, op2);
14049   if (!tem)
14050     tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
14051       
14052 #ifdef ENABLE_FOLD_CHECKING
14053   md5_init_ctx (&ctx);
14054   fold_checksum_tree (op0, &ctx, ht);
14055   md5_finish_ctx (&ctx, checksum_after_op0);
14056   htab_empty (ht);
14057
14058   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14059     fold_check_failed (op0, tem);
14060   
14061   md5_init_ctx (&ctx);
14062   fold_checksum_tree (op1, &ctx, ht);
14063   md5_finish_ctx (&ctx, checksum_after_op1);
14064   htab_empty (ht);
14065
14066   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14067     fold_check_failed (op1, tem);
14068   
14069   md5_init_ctx (&ctx);
14070   fold_checksum_tree (op2, &ctx, ht);
14071   md5_finish_ctx (&ctx, checksum_after_op2);
14072   htab_delete (ht);
14073
14074   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14075     fold_check_failed (op2, tem);
14076 #endif
14077   return tem;
14078 }
14079
14080 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14081    arguments in ARGARRAY, and a null static chain.
14082    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14083    of type TYPE from the given operands as constructed by build_call_array.  */
14084
14085 tree
14086 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
14087 {
14088   tree tem;
14089 #ifdef ENABLE_FOLD_CHECKING
14090   unsigned char checksum_before_fn[16],
14091                 checksum_before_arglist[16],
14092                 checksum_after_fn[16],
14093                 checksum_after_arglist[16];
14094   struct md5_ctx ctx;
14095   htab_t ht;
14096   int i;
14097
14098   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14099   md5_init_ctx (&ctx);
14100   fold_checksum_tree (fn, &ctx, ht);
14101   md5_finish_ctx (&ctx, checksum_before_fn);
14102   htab_empty (ht);
14103
14104   md5_init_ctx (&ctx);
14105   for (i = 0; i < nargs; i++)
14106     fold_checksum_tree (argarray[i], &ctx, ht);
14107   md5_finish_ctx (&ctx, checksum_before_arglist);
14108   htab_empty (ht);
14109 #endif
14110
14111   tem = fold_builtin_call_array (type, fn, nargs, argarray);
14112       
14113 #ifdef ENABLE_FOLD_CHECKING
14114   md5_init_ctx (&ctx);
14115   fold_checksum_tree (fn, &ctx, ht);
14116   md5_finish_ctx (&ctx, checksum_after_fn);
14117   htab_empty (ht);
14118
14119   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14120     fold_check_failed (fn, tem);
14121   
14122   md5_init_ctx (&ctx);
14123   for (i = 0; i < nargs; i++)
14124     fold_checksum_tree (argarray[i], &ctx, ht);
14125   md5_finish_ctx (&ctx, checksum_after_arglist);
14126   htab_delete (ht);
14127
14128   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14129     fold_check_failed (NULL_TREE, tem);
14130 #endif
14131   return tem;
14132 }
14133
14134 /* Perform constant folding and related simplification of initializer
14135    expression EXPR.  These behave identically to "fold_buildN" but ignore
14136    potential run-time traps and exceptions that fold must preserve.  */
14137
14138 #define START_FOLD_INIT \
14139   int saved_signaling_nans = flag_signaling_nans;\
14140   int saved_trapping_math = flag_trapping_math;\
14141   int saved_rounding_math = flag_rounding_math;\
14142   int saved_trapv = flag_trapv;\
14143   int saved_folding_initializer = folding_initializer;\
14144   flag_signaling_nans = 0;\
14145   flag_trapping_math = 0;\
14146   flag_rounding_math = 0;\
14147   flag_trapv = 0;\
14148   folding_initializer = 1;
14149
14150 #define END_FOLD_INIT \
14151   flag_signaling_nans = saved_signaling_nans;\
14152   flag_trapping_math = saved_trapping_math;\
14153   flag_rounding_math = saved_rounding_math;\
14154   flag_trapv = saved_trapv;\
14155   folding_initializer = saved_folding_initializer;
14156
14157 tree
14158 fold_build1_initializer (enum tree_code code, tree type, tree op)
14159 {
14160   tree result;
14161   START_FOLD_INIT;
14162
14163   result = fold_build1 (code, type, op);
14164
14165   END_FOLD_INIT;
14166   return result;
14167 }
14168
14169 tree
14170 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
14171 {
14172   tree result;
14173   START_FOLD_INIT;
14174
14175   result = fold_build2 (code, type, op0, op1);
14176
14177   END_FOLD_INIT;
14178   return result;
14179 }
14180
14181 tree
14182 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
14183                          tree op2)
14184 {
14185   tree result;
14186   START_FOLD_INIT;
14187
14188   result = fold_build3 (code, type, op0, op1, op2);
14189
14190   END_FOLD_INIT;
14191   return result;
14192 }
14193
14194 tree
14195 fold_build_call_array_initializer (tree type, tree fn,
14196                                    int nargs, tree *argarray)
14197 {
14198   tree result;
14199   START_FOLD_INIT;
14200
14201   result = fold_build_call_array (type, fn, nargs, argarray);
14202
14203   END_FOLD_INIT;
14204   return result;
14205 }
14206
14207 #undef START_FOLD_INIT
14208 #undef END_FOLD_INIT
14209
14210 /* Determine if first argument is a multiple of second argument.  Return 0 if
14211    it is not, or we cannot easily determined it to be.
14212
14213    An example of the sort of thing we care about (at this point; this routine
14214    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14215    fold cases do now) is discovering that
14216
14217      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14218
14219    is a multiple of
14220
14221      SAVE_EXPR (J * 8)
14222
14223    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14224
14225    This code also handles discovering that
14226
14227      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14228
14229    is a multiple of 8 so we don't have to worry about dealing with a
14230    possible remainder.
14231
14232    Note that we *look* inside a SAVE_EXPR only to determine how it was
14233    calculated; it is not safe for fold to do much of anything else with the
14234    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14235    at run time.  For example, the latter example above *cannot* be implemented
14236    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14237    evaluation time of the original SAVE_EXPR is not necessarily the same at
14238    the time the new expression is evaluated.  The only optimization of this
14239    sort that would be valid is changing
14240
14241      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14242
14243    divided by 8 to
14244
14245      SAVE_EXPR (I) * SAVE_EXPR (J)
14246
14247    (where the same SAVE_EXPR (J) is used in the original and the
14248    transformed version).  */
14249
14250 int
14251 multiple_of_p (tree type, const_tree top, const_tree bottom)
14252 {
14253   if (operand_equal_p (top, bottom, 0))
14254     return 1;
14255
14256   if (TREE_CODE (type) != INTEGER_TYPE)
14257     return 0;
14258
14259   switch (TREE_CODE (top))
14260     {
14261     case BIT_AND_EXPR:
14262       /* Bitwise and provides a power of two multiple.  If the mask is
14263          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14264       if (!integer_pow2p (bottom))
14265         return 0;
14266       /* FALLTHRU */
14267
14268     case MULT_EXPR:
14269       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14270               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14271
14272     case PLUS_EXPR:
14273     case MINUS_EXPR:
14274       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14275               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14276
14277     case LSHIFT_EXPR:
14278       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14279         {
14280           tree op1, t1;
14281
14282           op1 = TREE_OPERAND (top, 1);
14283           /* const_binop may not detect overflow correctly,
14284              so check for it explicitly here.  */
14285           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14286               > TREE_INT_CST_LOW (op1)
14287               && TREE_INT_CST_HIGH (op1) == 0
14288               && 0 != (t1 = fold_convert (type,
14289                                           const_binop (LSHIFT_EXPR,
14290                                                        size_one_node,
14291                                                        op1, 0)))
14292               && !TREE_OVERFLOW (t1))
14293             return multiple_of_p (type, t1, bottom);
14294         }
14295       return 0;
14296
14297     case NOP_EXPR:
14298       /* Can't handle conversions from non-integral or wider integral type.  */
14299       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14300           || (TYPE_PRECISION (type)
14301               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14302         return 0;
14303
14304       /* .. fall through ...  */
14305
14306     case SAVE_EXPR:
14307       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14308
14309     case INTEGER_CST:
14310       if (TREE_CODE (bottom) != INTEGER_CST
14311           || integer_zerop (bottom)
14312           || (TYPE_UNSIGNED (type)
14313               && (tree_int_cst_sgn (top) < 0
14314                   || tree_int_cst_sgn (bottom) < 0)))
14315         return 0;
14316       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14317                                              top, bottom, 0));
14318
14319     default:
14320       return 0;
14321     }
14322 }
14323
14324 /* Return true if CODE or TYPE is known to be non-negative. */
14325
14326 static bool
14327 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14328 {
14329   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14330       && truth_value_p (code))
14331     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14332        have a signed:1 type (where the value is -1 and 0).  */
14333     return true;
14334   return false;
14335 }
14336
14337 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14338    value is based on the assumption that signed overflow is undefined,
14339    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14340    *STRICT_OVERFLOW_P.  */
14341
14342 bool
14343 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14344                                 bool *strict_overflow_p)
14345 {
14346   if (TYPE_UNSIGNED (type))
14347     return true;
14348
14349   switch (code)
14350     {
14351     case ABS_EXPR:
14352       /* We can't return 1 if flag_wrapv is set because
14353          ABS_EXPR<INT_MIN> = INT_MIN.  */
14354       if (!INTEGRAL_TYPE_P (type))
14355         return true;
14356       if (TYPE_OVERFLOW_UNDEFINED (type))
14357         {
14358           *strict_overflow_p = true;
14359           return true;
14360         }
14361       break;
14362
14363     case NON_LVALUE_EXPR:
14364     case FLOAT_EXPR:
14365     case FIX_TRUNC_EXPR:
14366       return tree_expr_nonnegative_warnv_p (op0,
14367                                             strict_overflow_p);
14368
14369     case NOP_EXPR:
14370       {
14371         tree inner_type = TREE_TYPE (op0);
14372         tree outer_type = type;
14373
14374         if (TREE_CODE (outer_type) == REAL_TYPE)
14375           {
14376             if (TREE_CODE (inner_type) == REAL_TYPE)
14377               return tree_expr_nonnegative_warnv_p (op0,
14378                                                     strict_overflow_p);
14379             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14380               {
14381                 if (TYPE_UNSIGNED (inner_type))
14382                   return true;
14383                 return tree_expr_nonnegative_warnv_p (op0,
14384                                                       strict_overflow_p);
14385               }
14386           }
14387         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14388           {
14389             if (TREE_CODE (inner_type) == REAL_TYPE)
14390               return tree_expr_nonnegative_warnv_p (op0,
14391                                                     strict_overflow_p);
14392             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14393               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14394                       && TYPE_UNSIGNED (inner_type);
14395           }
14396       }
14397       break;
14398
14399     default:
14400       return tree_simple_nonnegative_warnv_p (code, type);
14401     }
14402
14403   /* We don't know sign of `t', so be conservative and return false.  */
14404   return false;
14405 }
14406
14407 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14408    value is based on the assumption that signed overflow is undefined,
14409    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14410    *STRICT_OVERFLOW_P.  */
14411
14412 bool
14413 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14414                                       tree op1, bool *strict_overflow_p)
14415 {
14416   if (TYPE_UNSIGNED (type))
14417     return true;
14418
14419   switch (code)
14420     {
14421     case POINTER_PLUS_EXPR:
14422     case PLUS_EXPR:
14423       if (FLOAT_TYPE_P (type))
14424         return (tree_expr_nonnegative_warnv_p (op0,
14425                                                strict_overflow_p)
14426                 && tree_expr_nonnegative_warnv_p (op1,
14427                                                   strict_overflow_p));
14428
14429       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14430          both unsigned and at least 2 bits shorter than the result.  */
14431       if (TREE_CODE (type) == INTEGER_TYPE
14432           && TREE_CODE (op0) == NOP_EXPR
14433           && TREE_CODE (op1) == NOP_EXPR)
14434         {
14435           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14436           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14437           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14438               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14439             {
14440               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14441                                        TYPE_PRECISION (inner2)) + 1;
14442               return prec < TYPE_PRECISION (type);
14443             }
14444         }
14445       break;
14446
14447     case MULT_EXPR:
14448       if (FLOAT_TYPE_P (type))
14449         {
14450           /* x * x for floating point x is always non-negative.  */
14451           if (operand_equal_p (op0, op1, 0))
14452             return true;
14453           return (tree_expr_nonnegative_warnv_p (op0,
14454                                                  strict_overflow_p)
14455                   && tree_expr_nonnegative_warnv_p (op1,
14456                                                     strict_overflow_p));
14457         }
14458
14459       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14460          both unsigned and their total bits is shorter than the result.  */
14461       if (TREE_CODE (type) == INTEGER_TYPE
14462           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14463           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14464         {
14465           tree inner0 = (TREE_CODE (op0) == NOP_EXPR) 
14466             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14467             : TREE_TYPE (op0);
14468           tree inner1 = (TREE_CODE (op1) == NOP_EXPR) 
14469             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14470             : TREE_TYPE (op1);
14471
14472           bool unsigned0 = TYPE_UNSIGNED (inner0);
14473           bool unsigned1 = TYPE_UNSIGNED (inner1);
14474
14475           if (TREE_CODE (op0) == INTEGER_CST)
14476             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14477
14478           if (TREE_CODE (op1) == INTEGER_CST)
14479             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14480
14481           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14482               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14483             {
14484               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14485                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14486                 : TYPE_PRECISION (inner0);
14487
14488               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14489                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14490                 : TYPE_PRECISION (inner1);
14491
14492               return precision0 + precision1 < TYPE_PRECISION (type);
14493             }
14494         }
14495       return false;
14496
14497     case BIT_AND_EXPR:
14498     case MAX_EXPR:
14499       return (tree_expr_nonnegative_warnv_p (op0,
14500                                              strict_overflow_p)
14501               || tree_expr_nonnegative_warnv_p (op1,
14502                                                 strict_overflow_p));
14503
14504     case BIT_IOR_EXPR:
14505     case BIT_XOR_EXPR:
14506     case MIN_EXPR:
14507     case RDIV_EXPR:
14508     case TRUNC_DIV_EXPR:
14509     case CEIL_DIV_EXPR:
14510     case FLOOR_DIV_EXPR:
14511     case ROUND_DIV_EXPR:
14512       return (tree_expr_nonnegative_warnv_p (op0,
14513                                              strict_overflow_p)
14514               && tree_expr_nonnegative_warnv_p (op1,
14515                                                 strict_overflow_p));
14516
14517     case TRUNC_MOD_EXPR:
14518     case CEIL_MOD_EXPR:
14519     case FLOOR_MOD_EXPR:
14520     case ROUND_MOD_EXPR:
14521       return tree_expr_nonnegative_warnv_p (op0,
14522                                             strict_overflow_p);
14523     default:
14524       return tree_simple_nonnegative_warnv_p (code, type);
14525     }
14526
14527   /* We don't know sign of `t', so be conservative and return false.  */
14528   return false;
14529 }
14530
14531 /* Return true if T is known to be non-negative.  If the return
14532    value is based on the assumption that signed overflow is undefined,
14533    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14534    *STRICT_OVERFLOW_P.  */
14535
14536 bool
14537 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14538 {
14539   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14540     return true;
14541
14542   switch (TREE_CODE (t))
14543     {
14544     case INTEGER_CST:
14545       return tree_int_cst_sgn (t) >= 0;
14546
14547     case REAL_CST:
14548       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14549
14550     case FIXED_CST:
14551       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14552
14553     case COND_EXPR:
14554       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14555                                              strict_overflow_p)
14556               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14557                                                 strict_overflow_p));
14558     default:
14559       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14560                                                    TREE_TYPE (t));
14561     }
14562   /* We don't know sign of `t', so be conservative and return false.  */
14563   return false;
14564 }
14565
14566 /* Return true if T is known to be non-negative.  If the return
14567    value is based on the assumption that signed overflow is undefined,
14568    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14569    *STRICT_OVERFLOW_P.  */
14570
14571 bool
14572 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14573                                tree arg0, tree arg1, bool *strict_overflow_p)
14574 {
14575   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14576     switch (DECL_FUNCTION_CODE (fndecl))
14577       {
14578         CASE_FLT_FN (BUILT_IN_ACOS):
14579         CASE_FLT_FN (BUILT_IN_ACOSH):
14580         CASE_FLT_FN (BUILT_IN_CABS):
14581         CASE_FLT_FN (BUILT_IN_COSH):
14582         CASE_FLT_FN (BUILT_IN_ERFC):
14583         CASE_FLT_FN (BUILT_IN_EXP):
14584         CASE_FLT_FN (BUILT_IN_EXP10):
14585         CASE_FLT_FN (BUILT_IN_EXP2):
14586         CASE_FLT_FN (BUILT_IN_FABS):
14587         CASE_FLT_FN (BUILT_IN_FDIM):
14588         CASE_FLT_FN (BUILT_IN_HYPOT):
14589         CASE_FLT_FN (BUILT_IN_POW10):
14590         CASE_INT_FN (BUILT_IN_FFS):
14591         CASE_INT_FN (BUILT_IN_PARITY):
14592         CASE_INT_FN (BUILT_IN_POPCOUNT):
14593       case BUILT_IN_BSWAP32:
14594       case BUILT_IN_BSWAP64:
14595         /* Always true.  */
14596         return true;
14597
14598         CASE_FLT_FN (BUILT_IN_SQRT):
14599         /* sqrt(-0.0) is -0.0.  */
14600         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14601           return true;
14602         return tree_expr_nonnegative_warnv_p (arg0,
14603                                               strict_overflow_p);
14604
14605         CASE_FLT_FN (BUILT_IN_ASINH):
14606         CASE_FLT_FN (BUILT_IN_ATAN):
14607         CASE_FLT_FN (BUILT_IN_ATANH):
14608         CASE_FLT_FN (BUILT_IN_CBRT):
14609         CASE_FLT_FN (BUILT_IN_CEIL):
14610         CASE_FLT_FN (BUILT_IN_ERF):
14611         CASE_FLT_FN (BUILT_IN_EXPM1):
14612         CASE_FLT_FN (BUILT_IN_FLOOR):
14613         CASE_FLT_FN (BUILT_IN_FMOD):
14614         CASE_FLT_FN (BUILT_IN_FREXP):
14615         CASE_FLT_FN (BUILT_IN_LCEIL):
14616         CASE_FLT_FN (BUILT_IN_LDEXP):
14617         CASE_FLT_FN (BUILT_IN_LFLOOR):
14618         CASE_FLT_FN (BUILT_IN_LLCEIL):
14619         CASE_FLT_FN (BUILT_IN_LLFLOOR):
14620         CASE_FLT_FN (BUILT_IN_LLRINT):
14621         CASE_FLT_FN (BUILT_IN_LLROUND):
14622         CASE_FLT_FN (BUILT_IN_LRINT):
14623         CASE_FLT_FN (BUILT_IN_LROUND):
14624         CASE_FLT_FN (BUILT_IN_MODF):
14625         CASE_FLT_FN (BUILT_IN_NEARBYINT):
14626         CASE_FLT_FN (BUILT_IN_RINT):
14627         CASE_FLT_FN (BUILT_IN_ROUND):
14628         CASE_FLT_FN (BUILT_IN_SCALB):
14629         CASE_FLT_FN (BUILT_IN_SCALBLN):
14630         CASE_FLT_FN (BUILT_IN_SCALBN):
14631         CASE_FLT_FN (BUILT_IN_SIGNBIT):
14632         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14633         CASE_FLT_FN (BUILT_IN_SINH):
14634         CASE_FLT_FN (BUILT_IN_TANH):
14635         CASE_FLT_FN (BUILT_IN_TRUNC):
14636         /* True if the 1st argument is nonnegative.  */
14637         return tree_expr_nonnegative_warnv_p (arg0,
14638                                               strict_overflow_p);
14639
14640         CASE_FLT_FN (BUILT_IN_FMAX):
14641         /* True if the 1st OR 2nd arguments are nonnegative.  */
14642         return (tree_expr_nonnegative_warnv_p (arg0,
14643                                                strict_overflow_p)
14644                 || (tree_expr_nonnegative_warnv_p (arg1,
14645                                                    strict_overflow_p)));
14646
14647         CASE_FLT_FN (BUILT_IN_FMIN):
14648         /* True if the 1st AND 2nd arguments are nonnegative.  */
14649         return (tree_expr_nonnegative_warnv_p (arg0,
14650                                                strict_overflow_p)
14651                 && (tree_expr_nonnegative_warnv_p (arg1,
14652                                                    strict_overflow_p)));
14653
14654         CASE_FLT_FN (BUILT_IN_COPYSIGN):
14655         /* True if the 2nd argument is nonnegative.  */
14656         return tree_expr_nonnegative_warnv_p (arg1,
14657                                               strict_overflow_p);
14658
14659         CASE_FLT_FN (BUILT_IN_POWI):
14660         /* True if the 1st argument is nonnegative or the second
14661            argument is an even integer.  */
14662         if (TREE_CODE (arg1) == INTEGER_CST
14663             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14664           return true;
14665         return tree_expr_nonnegative_warnv_p (arg0,
14666                                               strict_overflow_p);
14667
14668         CASE_FLT_FN (BUILT_IN_POW):
14669         /* True if the 1st argument is nonnegative or the second
14670            argument is an even integer valued real.  */
14671         if (TREE_CODE (arg1) == REAL_CST)
14672           {
14673             REAL_VALUE_TYPE c;
14674             HOST_WIDE_INT n;
14675
14676             c = TREE_REAL_CST (arg1);
14677             n = real_to_integer (&c);
14678             if ((n & 1) == 0)
14679               {
14680                 REAL_VALUE_TYPE cint;
14681                 real_from_integer (&cint, VOIDmode, n,
14682                                    n < 0 ? -1 : 0, 0);
14683                 if (real_identical (&c, &cint))
14684                   return true;
14685               }
14686           }
14687         return tree_expr_nonnegative_warnv_p (arg0,
14688                                               strict_overflow_p);
14689
14690       default:
14691         break;
14692       }
14693   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14694                                           type);
14695 }
14696
14697 /* Return true if T is known to be non-negative.  If the return
14698    value is based on the assumption that signed overflow is undefined,
14699    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14700    *STRICT_OVERFLOW_P.  */
14701
14702 bool
14703 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14704 {
14705   enum tree_code code = TREE_CODE (t);
14706   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14707     return true;
14708
14709   switch (code)
14710     {
14711     case TARGET_EXPR:
14712       {
14713         tree temp = TARGET_EXPR_SLOT (t);
14714         t = TARGET_EXPR_INITIAL (t);
14715
14716         /* If the initializer is non-void, then it's a normal expression
14717            that will be assigned to the slot.  */
14718         if (!VOID_TYPE_P (t))
14719           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14720
14721         /* Otherwise, the initializer sets the slot in some way.  One common
14722            way is an assignment statement at the end of the initializer.  */
14723         while (1)
14724           {
14725             if (TREE_CODE (t) == BIND_EXPR)
14726               t = expr_last (BIND_EXPR_BODY (t));
14727             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14728                      || TREE_CODE (t) == TRY_CATCH_EXPR)
14729               t = expr_last (TREE_OPERAND (t, 0));
14730             else if (TREE_CODE (t) == STATEMENT_LIST)
14731               t = expr_last (t);
14732             else
14733               break;
14734           }
14735         if (TREE_CODE (t) == MODIFY_EXPR
14736             && TREE_OPERAND (t, 0) == temp)
14737           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14738                                                 strict_overflow_p);
14739
14740         return false;
14741       }
14742
14743     case CALL_EXPR:
14744       {
14745         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
14746         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
14747
14748         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14749                                               get_callee_fndecl (t),
14750                                               arg0,
14751                                               arg1,
14752                                               strict_overflow_p);
14753       }
14754     case COMPOUND_EXPR:
14755     case MODIFY_EXPR:
14756       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14757                                             strict_overflow_p);
14758     case BIND_EXPR:
14759       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14760                                             strict_overflow_p);
14761     case SAVE_EXPR:
14762       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14763                                             strict_overflow_p);
14764
14765     default:
14766       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14767                                                    TREE_TYPE (t));
14768     }
14769
14770   /* We don't know sign of `t', so be conservative and return false.  */
14771   return false;
14772 }
14773
14774 /* Return true if T is known to be non-negative.  If the return
14775    value is based on the assumption that signed overflow is undefined,
14776    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14777    *STRICT_OVERFLOW_P.  */
14778
14779 bool
14780 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14781 {
14782   enum tree_code code;
14783   if (t == error_mark_node)
14784     return false;
14785
14786   code = TREE_CODE (t);
14787   switch (TREE_CODE_CLASS (code))
14788     {
14789     case tcc_binary:
14790     case tcc_comparison:
14791       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14792                                               TREE_TYPE (t),
14793                                               TREE_OPERAND (t, 0),
14794                                               TREE_OPERAND (t, 1),
14795                                               strict_overflow_p);
14796
14797     case tcc_unary:
14798       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14799                                              TREE_TYPE (t),
14800                                              TREE_OPERAND (t, 0),
14801                                              strict_overflow_p);
14802
14803     case tcc_constant:
14804     case tcc_declaration:
14805     case tcc_reference:
14806       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14807
14808     default:
14809       break;
14810     }
14811
14812   switch (code)
14813     {
14814     case TRUTH_AND_EXPR:
14815     case TRUTH_OR_EXPR:
14816     case TRUTH_XOR_EXPR:
14817       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14818                                               TREE_TYPE (t),
14819                                               TREE_OPERAND (t, 0),
14820                                               TREE_OPERAND (t, 1),
14821                                               strict_overflow_p);
14822     case TRUTH_NOT_EXPR:
14823       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14824                                              TREE_TYPE (t),
14825                                              TREE_OPERAND (t, 0),
14826                                              strict_overflow_p);
14827
14828     case COND_EXPR:
14829     case CONSTRUCTOR:
14830     case OBJ_TYPE_REF:
14831     case ASSERT_EXPR:
14832     case ADDR_EXPR:
14833     case WITH_SIZE_EXPR:
14834     case EXC_PTR_EXPR:
14835     case SSA_NAME:
14836     case FILTER_EXPR:
14837       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14838
14839     default:
14840       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14841     }
14842 }
14843
14844 /* Return true if `t' is known to be non-negative.  Handle warnings
14845    about undefined signed overflow.  */
14846
14847 bool
14848 tree_expr_nonnegative_p (tree t)
14849 {
14850   bool ret, strict_overflow_p;
14851
14852   strict_overflow_p = false;
14853   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14854   if (strict_overflow_p)
14855     fold_overflow_warning (("assuming signed overflow does not occur when "
14856                             "determining that expression is always "
14857                             "non-negative"),
14858                            WARN_STRICT_OVERFLOW_MISC);
14859   return ret;
14860 }
14861
14862
14863 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14864    For floating point we further ensure that T is not denormal.
14865    Similar logic is present in nonzero_address in rtlanal.h.
14866
14867    If the return value is based on the assumption that signed overflow
14868    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14869    change *STRICT_OVERFLOW_P.  */
14870
14871 bool
14872 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14873                                  bool *strict_overflow_p)
14874 {
14875   switch (code)
14876     {
14877     case ABS_EXPR:
14878       return tree_expr_nonzero_warnv_p (op0,
14879                                         strict_overflow_p);
14880
14881     case NOP_EXPR:
14882       {
14883         tree inner_type = TREE_TYPE (op0);
14884         tree outer_type = type;
14885
14886         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14887                 && tree_expr_nonzero_warnv_p (op0,
14888                                               strict_overflow_p));
14889       }
14890       break;
14891
14892     case NON_LVALUE_EXPR:
14893       return tree_expr_nonzero_warnv_p (op0,
14894                                         strict_overflow_p);
14895
14896     default:
14897       break;
14898   }
14899
14900   return false;
14901 }
14902
14903 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14904    For floating point we further ensure that T is not denormal.
14905    Similar logic is present in nonzero_address in rtlanal.h.
14906
14907    If the return value is based on the assumption that signed overflow
14908    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14909    change *STRICT_OVERFLOW_P.  */
14910
14911 bool
14912 tree_binary_nonzero_warnv_p (enum tree_code code,
14913                              tree type,
14914                              tree op0,
14915                              tree op1, bool *strict_overflow_p)
14916 {
14917   bool sub_strict_overflow_p;
14918   switch (code)
14919     {
14920     case POINTER_PLUS_EXPR:
14921     case PLUS_EXPR:
14922       if (TYPE_OVERFLOW_UNDEFINED (type))
14923         {
14924           /* With the presence of negative values it is hard
14925              to say something.  */
14926           sub_strict_overflow_p = false;
14927           if (!tree_expr_nonnegative_warnv_p (op0,
14928                                               &sub_strict_overflow_p)
14929               || !tree_expr_nonnegative_warnv_p (op1,
14930                                                  &sub_strict_overflow_p))
14931             return false;
14932           /* One of operands must be positive and the other non-negative.  */
14933           /* We don't set *STRICT_OVERFLOW_P here: even if this value
14934              overflows, on a twos-complement machine the sum of two
14935              nonnegative numbers can never be zero.  */
14936           return (tree_expr_nonzero_warnv_p (op0,
14937                                              strict_overflow_p)
14938                   || tree_expr_nonzero_warnv_p (op1,
14939                                                 strict_overflow_p));
14940         }
14941       break;
14942
14943     case MULT_EXPR:
14944       if (TYPE_OVERFLOW_UNDEFINED (type))
14945         {
14946           if (tree_expr_nonzero_warnv_p (op0,
14947                                          strict_overflow_p)
14948               && tree_expr_nonzero_warnv_p (op1,
14949                                             strict_overflow_p))
14950             {
14951               *strict_overflow_p = true;
14952               return true;
14953             }
14954         }
14955       break;
14956
14957     case MIN_EXPR:
14958       sub_strict_overflow_p = false;
14959       if (tree_expr_nonzero_warnv_p (op0,
14960                                      &sub_strict_overflow_p)
14961           && tree_expr_nonzero_warnv_p (op1,
14962                                         &sub_strict_overflow_p))
14963         {
14964           if (sub_strict_overflow_p)
14965             *strict_overflow_p = true;
14966         }
14967       break;
14968
14969     case MAX_EXPR:
14970       sub_strict_overflow_p = false;
14971       if (tree_expr_nonzero_warnv_p (op0,
14972                                      &sub_strict_overflow_p))
14973         {
14974           if (sub_strict_overflow_p)
14975             *strict_overflow_p = true;
14976
14977           /* When both operands are nonzero, then MAX must be too.  */
14978           if (tree_expr_nonzero_warnv_p (op1,
14979                                          strict_overflow_p))
14980             return true;
14981
14982           /* MAX where operand 0 is positive is positive.  */
14983           return tree_expr_nonnegative_warnv_p (op0,
14984                                                strict_overflow_p);
14985         }
14986       /* MAX where operand 1 is positive is positive.  */
14987       else if (tree_expr_nonzero_warnv_p (op1,
14988                                           &sub_strict_overflow_p)
14989                && tree_expr_nonnegative_warnv_p (op1,
14990                                                  &sub_strict_overflow_p))
14991         {
14992           if (sub_strict_overflow_p)
14993             *strict_overflow_p = true;
14994           return true;
14995         }
14996       break;
14997
14998     case BIT_IOR_EXPR:
14999       return (tree_expr_nonzero_warnv_p (op1,
15000                                          strict_overflow_p)
15001               || tree_expr_nonzero_warnv_p (op0,
15002                                             strict_overflow_p));
15003
15004     default:
15005       break;
15006   }
15007
15008   return false;
15009 }
15010
15011 /* Return true when T is an address and is known to be nonzero.
15012    For floating point we further ensure that T is not denormal.
15013    Similar logic is present in nonzero_address in rtlanal.h.
15014
15015    If the return value is based on the assumption that signed overflow
15016    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15017    change *STRICT_OVERFLOW_P.  */
15018
15019 bool
15020 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15021 {
15022   bool sub_strict_overflow_p;
15023   switch (TREE_CODE (t))
15024     {
15025     case INTEGER_CST:
15026       return !integer_zerop (t);
15027
15028     case ADDR_EXPR:
15029       {
15030         tree base = get_base_address (TREE_OPERAND (t, 0));
15031
15032         if (!base)
15033           return false;
15034
15035         /* Weak declarations may link to NULL.  */
15036         if (VAR_OR_FUNCTION_DECL_P (base))
15037           return !DECL_WEAK (base);
15038
15039         /* Constants are never weak.  */
15040         if (CONSTANT_CLASS_P (base))
15041           return true;
15042
15043         return false;
15044       }
15045
15046     case COND_EXPR:
15047       sub_strict_overflow_p = false;
15048       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15049                                      &sub_strict_overflow_p)
15050           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15051                                         &sub_strict_overflow_p))
15052         {
15053           if (sub_strict_overflow_p)
15054             *strict_overflow_p = true;
15055           return true;
15056         }
15057       break;
15058
15059     default:
15060       break;
15061     }
15062   return false;
15063 }
15064
15065 /* Return true when T is an address and is known to be nonzero.
15066    For floating point we further ensure that T is not denormal.
15067    Similar logic is present in nonzero_address in rtlanal.h.
15068
15069    If the return value is based on the assumption that signed overflow
15070    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15071    change *STRICT_OVERFLOW_P.  */
15072
15073 bool
15074 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15075 {
15076   tree type = TREE_TYPE (t);
15077   enum tree_code code;
15078
15079   /* Doing something useful for floating point would need more work.  */
15080   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15081     return false;
15082
15083   code = TREE_CODE (t);
15084   switch (TREE_CODE_CLASS (code))
15085     {
15086     case tcc_unary:
15087       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15088                                               strict_overflow_p);
15089     case tcc_binary:
15090     case tcc_comparison:
15091       return tree_binary_nonzero_warnv_p (code, type,
15092                                                TREE_OPERAND (t, 0),
15093                                                TREE_OPERAND (t, 1),
15094                                                strict_overflow_p);
15095     case tcc_constant:
15096     case tcc_declaration:
15097     case tcc_reference:
15098       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15099
15100     default:
15101       break;
15102     }
15103
15104   switch (code)
15105     {
15106     case TRUTH_NOT_EXPR:
15107       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15108                                               strict_overflow_p);
15109
15110     case TRUTH_AND_EXPR:
15111     case TRUTH_OR_EXPR:
15112     case TRUTH_XOR_EXPR:
15113       return tree_binary_nonzero_warnv_p (code, type,
15114                                                TREE_OPERAND (t, 0),
15115                                                TREE_OPERAND (t, 1),
15116                                                strict_overflow_p);
15117
15118     case COND_EXPR:
15119     case CONSTRUCTOR:
15120     case OBJ_TYPE_REF:
15121     case ASSERT_EXPR:
15122     case ADDR_EXPR:
15123     case WITH_SIZE_EXPR:
15124     case EXC_PTR_EXPR:
15125     case SSA_NAME:
15126     case FILTER_EXPR:
15127       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15128
15129     case COMPOUND_EXPR:
15130     case MODIFY_EXPR:
15131     case BIND_EXPR:
15132       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15133                                         strict_overflow_p);
15134
15135     case SAVE_EXPR:
15136       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15137                                         strict_overflow_p);
15138
15139     case CALL_EXPR:
15140       return alloca_call_p (t);
15141
15142     default:
15143       break;
15144     }
15145   return false;
15146 }
15147
15148 /* Return true when T is an address and is known to be nonzero.
15149    Handle warnings about undefined signed overflow.  */
15150
15151 bool
15152 tree_expr_nonzero_p (tree t)
15153 {
15154   bool ret, strict_overflow_p;
15155
15156   strict_overflow_p = false;
15157   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15158   if (strict_overflow_p)
15159     fold_overflow_warning (("assuming signed overflow does not occur when "
15160                             "determining that expression is always "
15161                             "non-zero"),
15162                            WARN_STRICT_OVERFLOW_MISC);
15163   return ret;
15164 }
15165
15166 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15167    attempt to fold the expression to a constant without modifying TYPE,
15168    OP0 or OP1.
15169
15170    If the expression could be simplified to a constant, then return
15171    the constant.  If the expression would not be simplified to a
15172    constant, then return NULL_TREE.  */
15173
15174 tree
15175 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15176 {
15177   tree tem = fold_binary (code, type, op0, op1);
15178   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15179 }
15180
15181 /* Given the components of a unary expression CODE, TYPE and OP0,
15182    attempt to fold the expression to a constant without modifying
15183    TYPE or OP0.
15184
15185    If the expression could be simplified to a constant, then return
15186    the constant.  If the expression would not be simplified to a
15187    constant, then return NULL_TREE.  */
15188
15189 tree
15190 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15191 {
15192   tree tem = fold_unary (code, type, op0);
15193   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15194 }
15195
15196 /* If EXP represents referencing an element in a constant string
15197    (either via pointer arithmetic or array indexing), return the
15198    tree representing the value accessed, otherwise return NULL.  */
15199
15200 tree
15201 fold_read_from_constant_string (tree exp)
15202 {
15203   if ((TREE_CODE (exp) == INDIRECT_REF
15204        || TREE_CODE (exp) == ARRAY_REF)
15205       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15206     {
15207       tree exp1 = TREE_OPERAND (exp, 0);
15208       tree index;
15209       tree string;
15210
15211       if (TREE_CODE (exp) == INDIRECT_REF)
15212         string = string_constant (exp1, &index);
15213       else
15214         {
15215           tree low_bound = array_ref_low_bound (exp);
15216           index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
15217
15218           /* Optimize the special-case of a zero lower bound.
15219
15220              We convert the low_bound to sizetype to avoid some problems
15221              with constant folding.  (E.g. suppose the lower bound is 1,
15222              and its mode is QI.  Without the conversion,l (ARRAY
15223              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15224              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15225           if (! integer_zerop (low_bound))
15226             index = size_diffop (index, fold_convert (sizetype, low_bound));
15227
15228           string = exp1;
15229         }
15230
15231       if (string
15232           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15233           && TREE_CODE (string) == STRING_CST
15234           && TREE_CODE (index) == INTEGER_CST
15235           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15236           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15237               == MODE_INT)
15238           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15239         return build_int_cst_type (TREE_TYPE (exp),
15240                                    (TREE_STRING_POINTER (string)
15241                                     [TREE_INT_CST_LOW (index)]));
15242     }
15243   return NULL;
15244 }
15245
15246 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15247    an integer constant, real, or fixed-point constant.
15248
15249    TYPE is the type of the result.  */
15250
15251 static tree
15252 fold_negate_const (tree arg0, tree type)
15253 {
15254   tree t = NULL_TREE;
15255
15256   switch (TREE_CODE (arg0))
15257     {
15258     case INTEGER_CST:
15259       {
15260         unsigned HOST_WIDE_INT low;
15261         HOST_WIDE_INT high;
15262         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15263                                    TREE_INT_CST_HIGH (arg0),
15264                                    &low, &high);
15265         t = force_fit_type_double (type, low, high, 1,
15266                                    (overflow | TREE_OVERFLOW (arg0))
15267                                    && !TYPE_UNSIGNED (type));
15268         break;
15269       }
15270
15271     case REAL_CST:
15272       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15273       break;
15274
15275     case FIXED_CST:
15276       {
15277         FIXED_VALUE_TYPE f;
15278         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15279                                             &(TREE_FIXED_CST (arg0)), NULL,
15280                                             TYPE_SATURATING (type));
15281         t = build_fixed (type, f);
15282         /* Propagate overflow flags.  */
15283         if (overflow_p | TREE_OVERFLOW (arg0))
15284           {
15285             TREE_OVERFLOW (t) = 1;
15286             TREE_CONSTANT_OVERFLOW (t) = 1;
15287           }
15288         else if (TREE_CONSTANT_OVERFLOW (arg0))
15289           TREE_CONSTANT_OVERFLOW (t) = 1;
15290         break;
15291       }
15292
15293     default:
15294       gcc_unreachable ();
15295     }
15296
15297   return t;
15298 }
15299
15300 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15301    an integer constant or real constant.
15302
15303    TYPE is the type of the result.  */
15304
15305 tree
15306 fold_abs_const (tree arg0, tree type)
15307 {
15308   tree t = NULL_TREE;
15309
15310   switch (TREE_CODE (arg0))
15311     {
15312     case INTEGER_CST:
15313       /* If the value is unsigned, then the absolute value is
15314          the same as the ordinary value.  */
15315       if (TYPE_UNSIGNED (type))
15316         t = arg0;
15317       /* Similarly, if the value is non-negative.  */
15318       else if (INT_CST_LT (integer_minus_one_node, arg0))
15319         t = arg0;
15320       /* If the value is negative, then the absolute value is
15321          its negation.  */
15322       else
15323         {
15324           unsigned HOST_WIDE_INT low;
15325           HOST_WIDE_INT high;
15326           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15327                                      TREE_INT_CST_HIGH (arg0),
15328                                      &low, &high);
15329           t = force_fit_type_double (type, low, high, -1,
15330                                      overflow | TREE_OVERFLOW (arg0));
15331         }
15332       break;
15333
15334     case REAL_CST:
15335       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15336         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15337       else
15338         t =  arg0;
15339       break;
15340
15341     default:
15342       gcc_unreachable ();
15343     }
15344
15345   return t;
15346 }
15347
15348 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15349    constant.  TYPE is the type of the result.  */
15350
15351 static tree
15352 fold_not_const (tree arg0, tree type)
15353 {
15354   tree t = NULL_TREE;
15355
15356   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15357
15358   t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15359                              ~TREE_INT_CST_HIGH (arg0), 0,
15360                              TREE_OVERFLOW (arg0));
15361
15362   return t;
15363 }
15364
15365 /* Given CODE, a relational operator, the target type, TYPE and two
15366    constant operands OP0 and OP1, return the result of the
15367    relational operation.  If the result is not a compile time
15368    constant, then return NULL_TREE.  */
15369
15370 static tree
15371 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15372 {
15373   int result, invert;
15374
15375   /* From here on, the only cases we handle are when the result is
15376      known to be a constant.  */
15377
15378   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15379     {
15380       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15381       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15382
15383       /* Handle the cases where either operand is a NaN.  */
15384       if (real_isnan (c0) || real_isnan (c1))
15385         {
15386           switch (code)
15387             {
15388             case EQ_EXPR:
15389             case ORDERED_EXPR:
15390               result = 0;
15391               break;
15392
15393             case NE_EXPR:
15394             case UNORDERED_EXPR:
15395             case UNLT_EXPR:
15396             case UNLE_EXPR:
15397             case UNGT_EXPR:
15398             case UNGE_EXPR:
15399             case UNEQ_EXPR:
15400               result = 1;
15401               break;
15402
15403             case LT_EXPR:
15404             case LE_EXPR:
15405             case GT_EXPR:
15406             case GE_EXPR:
15407             case LTGT_EXPR:
15408               if (flag_trapping_math)
15409                 return NULL_TREE;
15410               result = 0;
15411               break;
15412
15413             default:
15414               gcc_unreachable ();
15415             }
15416
15417           return constant_boolean_node (result, type);
15418         }
15419
15420       return constant_boolean_node (real_compare (code, c0, c1), type);
15421     }
15422
15423   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15424     {
15425       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15426       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15427       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15428     }
15429
15430   /* Handle equality/inequality of complex constants.  */
15431   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15432     {
15433       tree rcond = fold_relational_const (code, type,
15434                                           TREE_REALPART (op0),
15435                                           TREE_REALPART (op1));
15436       tree icond = fold_relational_const (code, type,
15437                                           TREE_IMAGPART (op0),
15438                                           TREE_IMAGPART (op1));
15439       if (code == EQ_EXPR)
15440         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15441       else if (code == NE_EXPR)
15442         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15443       else
15444         return NULL_TREE;
15445     }
15446
15447   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15448
15449      To compute GT, swap the arguments and do LT.
15450      To compute GE, do LT and invert the result.
15451      To compute LE, swap the arguments, do LT and invert the result.
15452      To compute NE, do EQ and invert the result.
15453
15454      Therefore, the code below must handle only EQ and LT.  */
15455
15456   if (code == LE_EXPR || code == GT_EXPR)
15457     {
15458       tree tem = op0;
15459       op0 = op1;
15460       op1 = tem;
15461       code = swap_tree_comparison (code);
15462     }
15463
15464   /* Note that it is safe to invert for real values here because we
15465      have already handled the one case that it matters.  */
15466
15467   invert = 0;
15468   if (code == NE_EXPR || code == GE_EXPR)
15469     {
15470       invert = 1;
15471       code = invert_tree_comparison (code, false);
15472     }
15473
15474   /* Compute a result for LT or EQ if args permit;
15475      Otherwise return T.  */
15476   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15477     {
15478       if (code == EQ_EXPR)
15479         result = tree_int_cst_equal (op0, op1);
15480       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15481         result = INT_CST_LT_UNSIGNED (op0, op1);
15482       else
15483         result = INT_CST_LT (op0, op1);
15484     }
15485   else
15486     return NULL_TREE;
15487
15488   if (invert)
15489     result ^= 1;
15490   return constant_boolean_node (result, type);
15491 }
15492
15493 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15494    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15495    itself.  */
15496
15497 tree
15498 fold_build_cleanup_point_expr (tree type, tree expr)
15499 {
15500   /* If the expression does not have side effects then we don't have to wrap
15501      it with a cleanup point expression.  */
15502   if (!TREE_SIDE_EFFECTS (expr))
15503     return expr;
15504
15505   /* If the expression is a return, check to see if the expression inside the
15506      return has no side effects or the right hand side of the modify expression
15507      inside the return. If either don't have side effects set we don't need to
15508      wrap the expression in a cleanup point expression.  Note we don't check the
15509      left hand side of the modify because it should always be a return decl.  */
15510   if (TREE_CODE (expr) == RETURN_EXPR)
15511     {
15512       tree op = TREE_OPERAND (expr, 0);
15513       if (!op || !TREE_SIDE_EFFECTS (op))
15514         return expr;
15515       op = TREE_OPERAND (op, 1);
15516       if (!TREE_SIDE_EFFECTS (op))
15517         return expr;
15518     }
15519   
15520   return build1 (CLEANUP_POINT_EXPR, type, expr);
15521 }
15522
15523 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15524    of an indirection through OP0, or NULL_TREE if no simplification is
15525    possible.  */
15526
15527 tree
15528 fold_indirect_ref_1 (tree type, tree op0)
15529 {
15530   tree sub = op0;
15531   tree subtype;
15532
15533   STRIP_NOPS (sub);
15534   subtype = TREE_TYPE (sub);
15535   if (!POINTER_TYPE_P (subtype))
15536     return NULL_TREE;
15537
15538   if (TREE_CODE (sub) == ADDR_EXPR)
15539     {
15540       tree op = TREE_OPERAND (sub, 0);
15541       tree optype = TREE_TYPE (op);
15542       /* *&CONST_DECL -> to the value of the const decl.  */
15543       if (TREE_CODE (op) == CONST_DECL)
15544         return DECL_INITIAL (op);
15545       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
15546       if (type == optype)
15547         {
15548           tree fop = fold_read_from_constant_string (op);
15549           if (fop)
15550             return fop;
15551           else
15552             return op;
15553         }
15554       /* *(foo *)&fooarray => fooarray[0] */
15555       else if (TREE_CODE (optype) == ARRAY_TYPE
15556                && type == TREE_TYPE (optype))
15557         {
15558           tree type_domain = TYPE_DOMAIN (optype);
15559           tree min_val = size_zero_node;
15560           if (type_domain && TYPE_MIN_VALUE (type_domain))
15561             min_val = TYPE_MIN_VALUE (type_domain);
15562           return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15563         }
15564       /* *(foo *)&complexfoo => __real__ complexfoo */
15565       else if (TREE_CODE (optype) == COMPLEX_TYPE
15566                && type == TREE_TYPE (optype))
15567         return fold_build1 (REALPART_EXPR, type, op);
15568       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15569       else if (TREE_CODE (optype) == VECTOR_TYPE
15570                && type == TREE_TYPE (optype))
15571         {
15572           tree part_width = TYPE_SIZE (type);
15573           tree index = bitsize_int (0);
15574           return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
15575         }
15576     }
15577
15578   /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15579   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15580       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15581     { 
15582       tree op00 = TREE_OPERAND (sub, 0);
15583       tree op01 = TREE_OPERAND (sub, 1);
15584       tree op00type;
15585       
15586       STRIP_NOPS (op00);
15587       op00type = TREE_TYPE (op00);
15588       if (TREE_CODE (op00) == ADDR_EXPR
15589           && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15590           && type == TREE_TYPE (TREE_TYPE (op00type)))
15591         { 
15592           HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15593           tree part_width = TYPE_SIZE (type);
15594           unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15595           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15596           tree index = bitsize_int (indexi);
15597
15598           if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15599             return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15600                                 part_width, index);
15601         
15602         }
15603     }
15604
15605
15606   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15607   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15608       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15609     {
15610       tree op00 = TREE_OPERAND (sub, 0);
15611       tree op01 = TREE_OPERAND (sub, 1);
15612       tree op00type;
15613
15614       STRIP_NOPS (op00);
15615       op00type = TREE_TYPE (op00);
15616       if (TREE_CODE (op00) == ADDR_EXPR
15617           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15618           && type == TREE_TYPE (TREE_TYPE (op00type)))
15619         {
15620           tree size = TYPE_SIZE_UNIT (type);
15621           if (tree_int_cst_equal (size, op01))
15622             return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
15623         }
15624     }
15625   
15626   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15627   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15628       && type == TREE_TYPE (TREE_TYPE (subtype)))
15629     {
15630       tree type_domain;
15631       tree min_val = size_zero_node;
15632       sub = build_fold_indirect_ref (sub);
15633       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15634       if (type_domain && TYPE_MIN_VALUE (type_domain))
15635         min_val = TYPE_MIN_VALUE (type_domain);
15636       return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15637     }
15638
15639   return NULL_TREE;
15640 }
15641
15642 /* Builds an expression for an indirection through T, simplifying some
15643    cases.  */
15644
15645 tree
15646 build_fold_indirect_ref (tree t)
15647 {
15648   tree type = TREE_TYPE (TREE_TYPE (t));
15649   tree sub = fold_indirect_ref_1 (type, t);
15650
15651   if (sub)
15652     return sub;
15653   else
15654     return build1 (INDIRECT_REF, type, t);
15655 }
15656
15657 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
15658
15659 tree
15660 fold_indirect_ref (tree t)
15661 {
15662   tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
15663
15664   if (sub)
15665     return sub;
15666   else
15667     return t;
15668 }
15669
15670 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15671    whose result is ignored.  The type of the returned tree need not be
15672    the same as the original expression.  */
15673
15674 tree
15675 fold_ignored_result (tree t)
15676 {
15677   if (!TREE_SIDE_EFFECTS (t))
15678     return integer_zero_node;
15679
15680   for (;;)
15681     switch (TREE_CODE_CLASS (TREE_CODE (t)))
15682       {
15683       case tcc_unary:
15684         t = TREE_OPERAND (t, 0);
15685         break;
15686
15687       case tcc_binary:
15688       case tcc_comparison:
15689         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15690           t = TREE_OPERAND (t, 0);
15691         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15692           t = TREE_OPERAND (t, 1);
15693         else
15694           return t;
15695         break;
15696
15697       case tcc_expression:
15698         switch (TREE_CODE (t))
15699           {
15700           case COMPOUND_EXPR:
15701             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15702               return t;
15703             t = TREE_OPERAND (t, 0);
15704             break;
15705
15706           case COND_EXPR:
15707             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15708                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15709               return t;
15710             t = TREE_OPERAND (t, 0);
15711             break;
15712
15713           default:
15714             return t;
15715           }
15716         break;
15717
15718       default:
15719         return t;
15720       }
15721 }
15722
15723 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15724    This can only be applied to objects of a sizetype.  */
15725
15726 tree
15727 round_up (tree value, int divisor)
15728 {
15729   tree div = NULL_TREE;
15730
15731   gcc_assert (divisor > 0);
15732   if (divisor == 1)
15733     return value;
15734
15735   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15736      have to do anything.  Only do this when we are not given a const,
15737      because in that case, this check is more expensive than just
15738      doing it.  */
15739   if (TREE_CODE (value) != INTEGER_CST)
15740     {
15741       div = build_int_cst (TREE_TYPE (value), divisor);
15742
15743       if (multiple_of_p (TREE_TYPE (value), value, div))
15744         return value;
15745     }
15746
15747   /* If divisor is a power of two, simplify this to bit manipulation.  */
15748   if (divisor == (divisor & -divisor))
15749     {
15750       if (TREE_CODE (value) == INTEGER_CST)
15751         {
15752           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15753           unsigned HOST_WIDE_INT high;
15754           bool overflow_p;
15755
15756           if ((low & (divisor - 1)) == 0)
15757             return value;
15758
15759           overflow_p = TREE_OVERFLOW (value);
15760           high = TREE_INT_CST_HIGH (value);
15761           low &= ~(divisor - 1);
15762           low += divisor;
15763           if (low == 0)
15764             {
15765               high++;
15766               if (high == 0)
15767                 overflow_p = true;
15768             }
15769
15770           return force_fit_type_double (TREE_TYPE (value), low, high,
15771                                         -1, overflow_p);
15772         }
15773       else
15774         {
15775           tree t;
15776
15777           t = build_int_cst (TREE_TYPE (value), divisor - 1);
15778           value = size_binop (PLUS_EXPR, value, t);
15779           t = build_int_cst (TREE_TYPE (value), -divisor);
15780           value = size_binop (BIT_AND_EXPR, value, t);
15781         }
15782     }
15783   else
15784     {
15785       if (!div)
15786         div = build_int_cst (TREE_TYPE (value), divisor);
15787       value = size_binop (CEIL_DIV_EXPR, value, div);
15788       value = size_binop (MULT_EXPR, value, div);
15789     }
15790
15791   return value;
15792 }
15793
15794 /* Likewise, but round down.  */
15795
15796 tree
15797 round_down (tree value, int divisor)
15798 {
15799   tree div = NULL_TREE;
15800
15801   gcc_assert (divisor > 0);
15802   if (divisor == 1)
15803     return value;
15804
15805   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15806      have to do anything.  Only do this when we are not given a const,
15807      because in that case, this check is more expensive than just
15808      doing it.  */
15809   if (TREE_CODE (value) != INTEGER_CST)
15810     {
15811       div = build_int_cst (TREE_TYPE (value), divisor);
15812
15813       if (multiple_of_p (TREE_TYPE (value), value, div))
15814         return value;
15815     }
15816
15817   /* If divisor is a power of two, simplify this to bit manipulation.  */
15818   if (divisor == (divisor & -divisor))
15819     {
15820       tree t;
15821
15822       t = build_int_cst (TREE_TYPE (value), -divisor);
15823       value = size_binop (BIT_AND_EXPR, value, t);
15824     }
15825   else
15826     {
15827       if (!div)
15828         div = build_int_cst (TREE_TYPE (value), divisor);
15829       value = size_binop (FLOOR_DIV_EXPR, value, div);
15830       value = size_binop (MULT_EXPR, value, div);
15831     }
15832
15833   return value;
15834 }
15835
15836 /* Returns the pointer to the base of the object addressed by EXP and
15837    extracts the information about the offset of the access, storing it
15838    to PBITPOS and POFFSET.  */
15839
15840 static tree
15841 split_address_to_core_and_offset (tree exp,
15842                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15843 {
15844   tree core;
15845   enum machine_mode mode;
15846   int unsignedp, volatilep;
15847   HOST_WIDE_INT bitsize;
15848
15849   if (TREE_CODE (exp) == ADDR_EXPR)
15850     {
15851       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15852                                   poffset, &mode, &unsignedp, &volatilep,
15853                                   false);
15854       core = fold_addr_expr (core);
15855     }
15856   else
15857     {
15858       core = exp;
15859       *pbitpos = 0;
15860       *poffset = NULL_TREE;
15861     }
15862
15863   return core;
15864 }
15865
15866 /* Returns true if addresses of E1 and E2 differ by a constant, false
15867    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
15868
15869 bool
15870 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15871 {
15872   tree core1, core2;
15873   HOST_WIDE_INT bitpos1, bitpos2;
15874   tree toffset1, toffset2, tdiff, type;
15875
15876   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15877   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15878
15879   if (bitpos1 % BITS_PER_UNIT != 0
15880       || bitpos2 % BITS_PER_UNIT != 0
15881       || !operand_equal_p (core1, core2, 0))
15882     return false;
15883
15884   if (toffset1 && toffset2)
15885     {
15886       type = TREE_TYPE (toffset1);
15887       if (type != TREE_TYPE (toffset2))
15888         toffset2 = fold_convert (type, toffset2);
15889
15890       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15891       if (!cst_and_fits_in_hwi (tdiff))
15892         return false;
15893
15894       *diff = int_cst_value (tdiff);
15895     }
15896   else if (toffset1 || toffset2)
15897     {
15898       /* If only one of the offsets is non-constant, the difference cannot
15899          be a constant.  */
15900       return false;
15901     }
15902   else
15903     *diff = 0;
15904
15905   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15906   return true;
15907 }
15908
15909 /* Simplify the floating point expression EXP when the sign of the
15910    result is not significant.  Return NULL_TREE if no simplification
15911    is possible.  */
15912
15913 tree
15914 fold_strip_sign_ops (tree exp)
15915 {
15916   tree arg0, arg1;
15917
15918   switch (TREE_CODE (exp))
15919     {
15920     case ABS_EXPR:
15921     case NEGATE_EXPR:
15922       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15923       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15924
15925     case MULT_EXPR:
15926     case RDIV_EXPR:
15927       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15928         return NULL_TREE;
15929       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15930       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15931       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15932         return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15933                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
15934                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
15935       break;
15936
15937     case COMPOUND_EXPR:
15938       arg0 = TREE_OPERAND (exp, 0);
15939       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15940       if (arg1)
15941         return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15942       break;
15943       
15944     case COND_EXPR:
15945       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15946       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15947       if (arg0 || arg1)
15948         return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15949                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
15950                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
15951       break;
15952       
15953     case CALL_EXPR:
15954       {
15955         const enum built_in_function fcode = builtin_mathfn_code (exp);
15956         switch (fcode)
15957         {
15958         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15959           /* Strip copysign function call, return the 1st argument. */
15960           arg0 = CALL_EXPR_ARG (exp, 0);
15961           arg1 = CALL_EXPR_ARG (exp, 1);
15962           return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15963
15964         default:
15965           /* Strip sign ops from the argument of "odd" math functions.  */
15966           if (negate_mathfn_p (fcode))
15967             {
15968               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15969               if (arg0)
15970                 return build_call_expr (get_callee_fndecl (exp), 1, arg0);
15971             }
15972           break;
15973         }
15974       }
15975       break;
15976
15977     default:
15978       break;
15979     }
15980   return NULL_TREE;
15981 }