OSDN Git Service

gcc/:
[pf3gnuchains/gcc-fork.git] / gcc / fold-const.c
1 /* Fold a constant sub-tree into a single node for C-compiler
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
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
69 /* Nonzero if we are folding constants inside an initializer; zero
70    otherwise.  */
71 int folding_initializer = 0;
72
73 /* The following constants represent a bit based encoding of GCC's
74    comparison operators.  This encoding simplifies transformations
75    on relational comparison operators, such as AND and OR.  */
76 enum comparison_code {
77   COMPCODE_FALSE = 0,
78   COMPCODE_LT = 1,
79   COMPCODE_EQ = 2,
80   COMPCODE_LE = 3,
81   COMPCODE_GT = 4,
82   COMPCODE_LTGT = 5,
83   COMPCODE_GE = 6,
84   COMPCODE_ORD = 7,
85   COMPCODE_UNORD = 8,
86   COMPCODE_UNLT = 9,
87   COMPCODE_UNEQ = 10,
88   COMPCODE_UNLE = 11,
89   COMPCODE_UNGT = 12,
90   COMPCODE_NE = 13,
91   COMPCODE_UNGE = 14,
92   COMPCODE_TRUE = 15
93 };
94
95 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
96 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
97 static bool negate_mathfn_p (enum built_in_function);
98 static bool negate_expr_p (tree);
99 static tree negate_expr (tree);
100 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
101 static tree associate_trees (tree, tree, enum tree_code, tree);
102 static tree const_binop (enum tree_code, tree, tree, int);
103 static enum comparison_code comparison_to_compcode (enum tree_code);
104 static enum tree_code compcode_to_comparison (enum comparison_code);
105 static tree combine_comparisons (enum tree_code, enum tree_code,
106                                  enum tree_code, tree, tree, tree);
107 static int truth_value_p (enum tree_code);
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 decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
114                                     enum machine_mode *, int *, int *,
115                                     tree *, tree *);
116 static tree sign_bit_p (tree, const_tree);
117 static int simple_operand_p (const_tree);
118 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
119 static tree range_predecessor (tree);
120 static tree range_successor (tree);
121 static tree make_range (tree, int *, tree *, tree *, bool *);
122 static tree build_range_check (tree, tree, int, tree, tree);
123 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
124                          tree);
125 static tree fold_range_test (enum tree_code, tree, tree, tree);
126 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
127 static tree unextend (tree, int, int, tree);
128 static tree fold_truthop (enum tree_code, tree, tree, tree);
129 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
130 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
131 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
132 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
133                                                  tree, tree,
134                                                  tree, tree, int);
135 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
136                                  tree, tree, tree);
137 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
138 static tree fold_div_compare (enum tree_code, tree, tree, tree);
139 static bool reorder_operands_p (const_tree, const_tree);
140 static tree fold_negate_const (tree, tree);
141 static tree fold_not_const (tree, tree);
142 static tree fold_relational_const (enum tree_code, tree, tree, tree);
143
144
145 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
146    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
147    and SUM1.  Then this yields nonzero if overflow occurred during the
148    addition.
149
150    Overflow occurs if A and B have the same sign, but A and SUM differ in
151    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
152    sign.  */
153 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
154 \f
155 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
156    We do that by representing the two-word integer in 4 words, with only
157    HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
158    number.  The value of the word is LOWPART + HIGHPART * BASE.  */
159
160 #define LOWPART(x) \
161   ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
162 #define HIGHPART(x) \
163   ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
164 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
165
166 /* Unpack a two-word integer into 4 words.
167    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
168    WORDS points to the array of HOST_WIDE_INTs.  */
169
170 static void
171 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
172 {
173   words[0] = LOWPART (low);
174   words[1] = HIGHPART (low);
175   words[2] = LOWPART (hi);
176   words[3] = HIGHPART (hi);
177 }
178
179 /* Pack an array of 4 words into a two-word integer.
180    WORDS points to the array of words.
181    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
182
183 static void
184 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
185         HOST_WIDE_INT *hi)
186 {
187   *low = words[0] + words[1] * BASE;
188   *hi = words[2] + words[3] * BASE;
189 }
190 \f
191 /* Force the double-word integer L1, H1 to be within the range of the
192    integer type TYPE.  Stores the properly truncated and sign-extended
193    double-word integer in *LV, *HV.  Returns true if the operation
194    overflows, that is, argument and result are different.  */
195
196 int
197 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
198                  unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
199 {
200   unsigned HOST_WIDE_INT low0 = l1;
201   HOST_WIDE_INT high0 = h1;
202   unsigned int prec;
203   int sign_extended_type;
204
205   if (POINTER_TYPE_P (type)
206       || TREE_CODE (type) == OFFSET_TYPE)
207     prec = POINTER_SIZE;
208   else
209     prec = TYPE_PRECISION (type);
210
211   /* Size types *are* sign extended.  */
212   sign_extended_type = (!TYPE_UNSIGNED (type)
213                         || (TREE_CODE (type) == INTEGER_TYPE
214                             && TYPE_IS_SIZETYPE (type)));
215
216   /* First clear all bits that are beyond the type's precision.  */
217   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
218     ;
219   else if (prec > HOST_BITS_PER_WIDE_INT)
220     h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
221   else
222     {
223       h1 = 0;
224       if (prec < HOST_BITS_PER_WIDE_INT)
225         l1 &= ~((HOST_WIDE_INT) (-1) << prec);
226     }
227
228   /* Then do sign extension if necessary.  */
229   if (!sign_extended_type)
230     /* No sign extension */;
231   else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
232     /* Correct width already.  */;
233   else if (prec > HOST_BITS_PER_WIDE_INT)
234     {
235       /* Sign extend top half? */
236       if (h1 & ((unsigned HOST_WIDE_INT)1
237                 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
238         h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
239     }
240   else if (prec == HOST_BITS_PER_WIDE_INT)
241     {
242       if ((HOST_WIDE_INT)l1 < 0)
243         h1 = -1;
244     }
245   else
246     {
247       /* Sign extend bottom half? */
248       if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
249         {
250           h1 = -1;
251           l1 |= (HOST_WIDE_INT)(-1) << prec;
252         }
253     }
254
255   *lv = l1;
256   *hv = h1;
257
258   /* If the value didn't fit, signal overflow.  */
259   return l1 != low0 || h1 != high0;
260 }
261
262 /* We force the double-int HIGH:LOW to the range of the type TYPE by
263    sign or zero extending it.
264    OVERFLOWABLE indicates if we are interested
265    in overflow of the value, when >0 we are only interested in signed
266    overflow, for <0 we are interested in any overflow.  OVERFLOWED
267    indicates whether overflow has already occurred.  CONST_OVERFLOWED
268    indicates whether constant overflow has already occurred.  We force
269    T's value to be within range of T's type (by setting to 0 or 1 all
270    the bits outside the type's range).  We set TREE_OVERFLOWED if,
271         OVERFLOWED is nonzero,
272         or OVERFLOWABLE is >0 and signed overflow occurs
273         or OVERFLOWABLE is <0 and any overflow occurs
274    We return a new tree node for the extended double-int.  The node
275    is shared if no overflow flags are set.  */
276
277 tree
278 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
279                        HOST_WIDE_INT high, int overflowable,
280                        bool overflowed)
281 {
282   int sign_extended_type;
283   bool overflow;
284
285   /* Size types *are* sign extended.  */
286   sign_extended_type = (!TYPE_UNSIGNED (type)
287                         || (TREE_CODE (type) == INTEGER_TYPE
288                             && TYPE_IS_SIZETYPE (type)));
289
290   overflow = fit_double_type (low, high, &low, &high, type);
291
292   /* If we need to set overflow flags, return a new unshared node.  */
293   if (overflowed || overflow)
294     {
295       if (overflowed
296           || overflowable < 0
297           || (overflowable > 0 && sign_extended_type))
298         {
299           tree t = make_node (INTEGER_CST);
300           TREE_INT_CST_LOW (t) = low;
301           TREE_INT_CST_HIGH (t) = high;
302           TREE_TYPE (t) = type;
303           TREE_OVERFLOW (t) = 1;
304           return t;
305         }
306     }
307
308   /* Else build a shared node.  */
309   return build_int_cst_wide (type, low, high);
310 }
311 \f
312 /* Add two doubleword integers with doubleword result.
313    Return nonzero if the operation overflows according to UNSIGNED_P.
314    Each argument is given as two `HOST_WIDE_INT' pieces.
315    One argument is L1 and H1; the other, L2 and H2.
316    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
317
318 int
319 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
320                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
321                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
322                       bool unsigned_p)
323 {
324   unsigned HOST_WIDE_INT l;
325   HOST_WIDE_INT h;
326
327   l = l1 + l2;
328   h = h1 + h2 + (l < l1);
329
330   *lv = l;
331   *hv = h;
332
333   if (unsigned_p)
334     return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
335   else
336     return OVERFLOW_SUM_SIGN (h1, h2, h);
337 }
338
339 /* Negate a doubleword integer with doubleword result.
340    Return nonzero if the operation overflows, assuming it's signed.
341    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
342    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
343
344 int
345 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
346             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
347 {
348   if (l1 == 0)
349     {
350       *lv = 0;
351       *hv = - h1;
352       return (*hv & h1) < 0;
353     }
354   else
355     {
356       *lv = -l1;
357       *hv = ~h1;
358       return 0;
359     }
360 }
361 \f
362 /* Multiply two doubleword integers with doubleword result.
363    Return nonzero if the operation overflows according to UNSIGNED_P.
364    Each argument is given as two `HOST_WIDE_INT' pieces.
365    One argument is L1 and H1; the other, L2 and H2.
366    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
367
368 int
369 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
370                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
371                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
372                       bool unsigned_p)
373 {
374   HOST_WIDE_INT arg1[4];
375   HOST_WIDE_INT arg2[4];
376   HOST_WIDE_INT prod[4 * 2];
377   unsigned HOST_WIDE_INT carry;
378   int i, j, k;
379   unsigned HOST_WIDE_INT toplow, neglow;
380   HOST_WIDE_INT tophigh, neghigh;
381
382   encode (arg1, l1, h1);
383   encode (arg2, l2, h2);
384
385   memset (prod, 0, sizeof prod);
386
387   for (i = 0; i < 4; i++)
388     {
389       carry = 0;
390       for (j = 0; j < 4; j++)
391         {
392           k = i + j;
393           /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
394           carry += arg1[i] * arg2[j];
395           /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
396           carry += prod[k];
397           prod[k] = LOWPART (carry);
398           carry = HIGHPART (carry);
399         }
400       prod[i + 4] = carry;
401     }
402
403   decode (prod, lv, hv);
404   decode (prod + 4, &toplow, &tophigh);
405
406   /* Unsigned overflow is immediate.  */
407   if (unsigned_p)
408     return (toplow | tophigh) != 0;
409
410   /* Check for signed overflow by calculating the signed representation of the
411      top half of the result; it should agree with the low half's sign bit.  */
412   if (h1 < 0)
413     {
414       neg_double (l2, h2, &neglow, &neghigh);
415       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
416     }
417   if (h2 < 0)
418     {
419       neg_double (l1, h1, &neglow, &neghigh);
420       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
421     }
422   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
423 }
424 \f
425 /* Shift the doubleword integer in L1, H1 left by COUNT places
426    keeping only PREC bits of result.
427    Shift right if COUNT is negative.
428    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
429    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
430
431 void
432 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
433                HOST_WIDE_INT count, unsigned int prec,
434                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
435 {
436   unsigned HOST_WIDE_INT signmask;
437
438   if (count < 0)
439     {
440       rshift_double (l1, h1, -count, prec, lv, hv, arith);
441       return;
442     }
443
444   if (SHIFT_COUNT_TRUNCATED)
445     count %= prec;
446
447   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
448     {
449       /* Shifting by the host word size is undefined according to the
450          ANSI standard, so we must handle this as a special case.  */
451       *hv = 0;
452       *lv = 0;
453     }
454   else if (count >= HOST_BITS_PER_WIDE_INT)
455     {
456       *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
457       *lv = 0;
458     }
459   else
460     {
461       *hv = (((unsigned HOST_WIDE_INT) h1 << count)
462              | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
463       *lv = l1 << count;
464     }
465
466   /* Sign extend all bits that are beyond the precision.  */
467
468   signmask = -((prec > HOST_BITS_PER_WIDE_INT
469                 ? ((unsigned HOST_WIDE_INT) *hv
470                    >> (prec - HOST_BITS_PER_WIDE_INT - 1))
471                 : (*lv >> (prec - 1))) & 1);
472
473   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
474     ;
475   else if (prec >= HOST_BITS_PER_WIDE_INT)
476     {
477       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
478       *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
479     }
480   else
481     {
482       *hv = signmask;
483       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
484       *lv |= signmask << prec;
485     }
486 }
487
488 /* Shift the doubleword integer in L1, H1 right by COUNT places
489    keeping only PREC bits of result.  COUNT must be positive.
490    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
491    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
492
493 void
494 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
495                HOST_WIDE_INT count, unsigned int prec,
496                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
497                int arith)
498 {
499   unsigned HOST_WIDE_INT signmask;
500
501   signmask = (arith
502               ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
503               : 0);
504
505   if (SHIFT_COUNT_TRUNCATED)
506     count %= prec;
507
508   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
509     {
510       /* Shifting by the host word size is undefined according to the
511          ANSI standard, so we must handle this as a special case.  */
512       *hv = 0;
513       *lv = 0;
514     }
515   else if (count >= HOST_BITS_PER_WIDE_INT)
516     {
517       *hv = 0;
518       *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
519     }
520   else
521     {
522       *hv = (unsigned HOST_WIDE_INT) h1 >> count;
523       *lv = ((l1 >> count)
524              | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
525     }
526
527   /* Zero / sign extend all bits that are beyond the precision.  */
528
529   if (count >= (HOST_WIDE_INT)prec)
530     {
531       *hv = signmask;
532       *lv = signmask;
533     }
534   else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
535     ;
536   else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
537     {
538       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
539       *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
540     }
541   else
542     {
543       *hv = signmask;
544       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
545       *lv |= signmask << (prec - count);
546     }
547 }
548 \f
549 /* Rotate the doubleword integer in L1, H1 left by COUNT places
550    keeping only PREC bits of result.
551    Rotate right if COUNT is negative.
552    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
553
554 void
555 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
556                 HOST_WIDE_INT count, unsigned int prec,
557                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
558 {
559   unsigned HOST_WIDE_INT s1l, s2l;
560   HOST_WIDE_INT s1h, s2h;
561
562   count %= prec;
563   if (count < 0)
564     count += prec;
565
566   lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
567   rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
568   *lv = s1l | s2l;
569   *hv = s1h | s2h;
570 }
571
572 /* Rotate the doubleword integer in L1, H1 left by COUNT places
573    keeping only PREC bits of result.  COUNT must be positive.
574    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
575
576 void
577 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
578                 HOST_WIDE_INT count, unsigned int prec,
579                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
580 {
581   unsigned HOST_WIDE_INT s1l, s2l;
582   HOST_WIDE_INT s1h, s2h;
583
584   count %= prec;
585   if (count < 0)
586     count += prec;
587
588   rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
589   lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
590   *lv = s1l | s2l;
591   *hv = s1h | s2h;
592 }
593 \f
594 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
595    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
596    CODE is a tree code for a kind of division, one of
597    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
598    or EXACT_DIV_EXPR
599    It controls how the quotient is rounded to an integer.
600    Return nonzero if the operation overflows.
601    UNS nonzero says do unsigned division.  */
602
603 int
604 div_and_round_double (enum tree_code code, int uns,
605                       unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
606                       HOST_WIDE_INT hnum_orig,
607                       unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
608                       HOST_WIDE_INT hden_orig,
609                       unsigned HOST_WIDE_INT *lquo,
610                       HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
611                       HOST_WIDE_INT *hrem)
612 {
613   int quo_neg = 0;
614   HOST_WIDE_INT num[4 + 1];     /* extra element for scaling.  */
615   HOST_WIDE_INT den[4], quo[4];
616   int i, j;
617   unsigned HOST_WIDE_INT work;
618   unsigned HOST_WIDE_INT carry = 0;
619   unsigned HOST_WIDE_INT lnum = lnum_orig;
620   HOST_WIDE_INT hnum = hnum_orig;
621   unsigned HOST_WIDE_INT lden = lden_orig;
622   HOST_WIDE_INT hden = hden_orig;
623   int overflow = 0;
624
625   if (hden == 0 && lden == 0)
626     overflow = 1, lden = 1;
627
628   /* Calculate quotient sign and convert operands to unsigned.  */
629   if (!uns)
630     {
631       if (hnum < 0)
632         {
633           quo_neg = ~ quo_neg;
634           /* (minimum integer) / (-1) is the only overflow case.  */
635           if (neg_double (lnum, hnum, &lnum, &hnum)
636               && ((HOST_WIDE_INT) lden & hden) == -1)
637             overflow = 1;
638         }
639       if (hden < 0)
640         {
641           quo_neg = ~ quo_neg;
642           neg_double (lden, hden, &lden, &hden);
643         }
644     }
645
646   if (hnum == 0 && hden == 0)
647     {                           /* single precision */
648       *hquo = *hrem = 0;
649       /* This unsigned division rounds toward zero.  */
650       *lquo = lnum / lden;
651       goto finish_up;
652     }
653
654   if (hnum == 0)
655     {                           /* trivial case: dividend < divisor */
656       /* hden != 0 already checked.  */
657       *hquo = *lquo = 0;
658       *hrem = hnum;
659       *lrem = lnum;
660       goto finish_up;
661     }
662
663   memset (quo, 0, sizeof quo);
664
665   memset (num, 0, sizeof num);  /* to zero 9th element */
666   memset (den, 0, sizeof den);
667
668   encode (num, lnum, hnum);
669   encode (den, lden, hden);
670
671   /* Special code for when the divisor < BASE.  */
672   if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
673     {
674       /* hnum != 0 already checked.  */
675       for (i = 4 - 1; i >= 0; i--)
676         {
677           work = num[i] + carry * BASE;
678           quo[i] = work / lden;
679           carry = work % lden;
680         }
681     }
682   else
683     {
684       /* Full double precision division,
685          with thanks to Don Knuth's "Seminumerical Algorithms".  */
686       int num_hi_sig, den_hi_sig;
687       unsigned HOST_WIDE_INT quo_est, scale;
688
689       /* Find the highest nonzero divisor digit.  */
690       for (i = 4 - 1;; i--)
691         if (den[i] != 0)
692           {
693             den_hi_sig = i;
694             break;
695           }
696
697       /* Insure that the first digit of the divisor is at least BASE/2.
698          This is required by the quotient digit estimation algorithm.  */
699
700       scale = BASE / (den[den_hi_sig] + 1);
701       if (scale > 1)
702         {               /* scale divisor and dividend */
703           carry = 0;
704           for (i = 0; i <= 4 - 1; i++)
705             {
706               work = (num[i] * scale) + carry;
707               num[i] = LOWPART (work);
708               carry = HIGHPART (work);
709             }
710
711           num[4] = carry;
712           carry = 0;
713           for (i = 0; i <= 4 - 1; i++)
714             {
715               work = (den[i] * scale) + carry;
716               den[i] = LOWPART (work);
717               carry = HIGHPART (work);
718               if (den[i] != 0) den_hi_sig = i;
719             }
720         }
721
722       num_hi_sig = 4;
723
724       /* Main loop */
725       for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
726         {
727           /* Guess the next quotient digit, quo_est, by dividing the first
728              two remaining dividend digits by the high order quotient digit.
729              quo_est is never low and is at most 2 high.  */
730           unsigned HOST_WIDE_INT tmp;
731
732           num_hi_sig = i + den_hi_sig + 1;
733           work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
734           if (num[num_hi_sig] != den[den_hi_sig])
735             quo_est = work / den[den_hi_sig];
736           else
737             quo_est = BASE - 1;
738
739           /* Refine quo_est so it's usually correct, and at most one high.  */
740           tmp = work - quo_est * den[den_hi_sig];
741           if (tmp < BASE
742               && (den[den_hi_sig - 1] * quo_est
743                   > (tmp * BASE + num[num_hi_sig - 2])))
744             quo_est--;
745
746           /* Try QUO_EST as the quotient digit, by multiplying the
747              divisor by QUO_EST and subtracting from the remaining dividend.
748              Keep in mind that QUO_EST is the I - 1st digit.  */
749
750           carry = 0;
751           for (j = 0; j <= den_hi_sig; j++)
752             {
753               work = quo_est * den[j] + carry;
754               carry = HIGHPART (work);
755               work = num[i + j] - LOWPART (work);
756               num[i + j] = LOWPART (work);
757               carry += HIGHPART (work) != 0;
758             }
759
760           /* If quo_est was high by one, then num[i] went negative and
761              we need to correct things.  */
762           if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
763             {
764               quo_est--;
765               carry = 0;                /* add divisor back in */
766               for (j = 0; j <= den_hi_sig; j++)
767                 {
768                   work = num[i + j] + den[j] + carry;
769                   carry = HIGHPART (work);
770                   num[i + j] = LOWPART (work);
771                 }
772
773               num [num_hi_sig] += carry;
774             }
775
776           /* Store the quotient digit.  */
777           quo[i] = quo_est;
778         }
779     }
780
781   decode (quo, lquo, hquo);
782
783  finish_up:
784   /* If result is negative, make it so.  */
785   if (quo_neg)
786     neg_double (*lquo, *hquo, lquo, hquo);
787
788   /* Compute trial remainder:  rem = num - (quo * den)  */
789   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
790   neg_double (*lrem, *hrem, lrem, hrem);
791   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
792
793   switch (code)
794     {
795     case TRUNC_DIV_EXPR:
796     case TRUNC_MOD_EXPR:        /* round toward zero */
797     case EXACT_DIV_EXPR:        /* for this one, it shouldn't matter */
798       return overflow;
799
800     case FLOOR_DIV_EXPR:
801     case FLOOR_MOD_EXPR:        /* round toward negative infinity */
802       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
803         {
804           /* quo = quo - 1;  */
805           add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
806                       lquo, hquo);
807         }
808       else
809         return overflow;
810       break;
811
812     case CEIL_DIV_EXPR:
813     case CEIL_MOD_EXPR:         /* round toward positive infinity */
814       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
815         {
816           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
817                       lquo, hquo);
818         }
819       else
820         return overflow;
821       break;
822
823     case ROUND_DIV_EXPR:
824     case ROUND_MOD_EXPR:        /* round to closest integer */
825       {
826         unsigned HOST_WIDE_INT labs_rem = *lrem;
827         HOST_WIDE_INT habs_rem = *hrem;
828         unsigned HOST_WIDE_INT labs_den = lden, ltwice;
829         HOST_WIDE_INT habs_den = hden, htwice;
830
831         /* Get absolute values.  */
832         if (*hrem < 0)
833           neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
834         if (hden < 0)
835           neg_double (lden, hden, &labs_den, &habs_den);
836
837         /* If (2 * abs (lrem) >= abs (lden)) */
838         mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
839                     labs_rem, habs_rem, &ltwice, &htwice);
840
841         if (((unsigned HOST_WIDE_INT) habs_den
842              < (unsigned HOST_WIDE_INT) htwice)
843             || (((unsigned HOST_WIDE_INT) habs_den
844                  == (unsigned HOST_WIDE_INT) htwice)
845                 && (labs_den < ltwice)))
846           {
847             if (*hquo < 0)
848               /* quo = quo - 1;  */
849               add_double (*lquo, *hquo,
850                           (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
851             else
852               /* quo = quo + 1; */
853               add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
854                           lquo, hquo);
855           }
856         else
857           return overflow;
858       }
859       break;
860
861     default:
862       gcc_unreachable ();
863     }
864
865   /* Compute true remainder:  rem = num - (quo * den)  */
866   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
867   neg_double (*lrem, *hrem, lrem, hrem);
868   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
869   return overflow;
870 }
871
872 /* If ARG2 divides ARG1 with zero remainder, carries out the division
873    of type CODE and returns the quotient.
874    Otherwise returns NULL_TREE.  */
875
876 static tree
877 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
878 {
879   unsigned HOST_WIDE_INT int1l, int2l;
880   HOST_WIDE_INT int1h, int2h;
881   unsigned HOST_WIDE_INT quol, reml;
882   HOST_WIDE_INT quoh, remh;
883   tree type = TREE_TYPE (arg1);
884   int uns = TYPE_UNSIGNED (type);
885
886   int1l = TREE_INT_CST_LOW (arg1);
887   int1h = TREE_INT_CST_HIGH (arg1);
888   /* &obj[0] + -128 really should be compiled as &obj[-8] rather than
889      &obj[some_exotic_number].  */
890   if (POINTER_TYPE_P (type))
891     {
892       uns = false;
893       type = signed_type_for (type);
894       fit_double_type (int1l, int1h, &int1l, &int1h,
895                        type);
896     }
897   else
898     fit_double_type (int1l, int1h, &int1l, &int1h, type);
899   int2l = TREE_INT_CST_LOW (arg2);
900   int2h = TREE_INT_CST_HIGH (arg2);
901
902   div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
903                         &quol, &quoh, &reml, &remh);
904   if (remh != 0 || reml != 0)
905     return NULL_TREE;
906
907   return build_int_cst_wide (type, quol, quoh);
908 }
909 \f
910 /* This is nonzero if we should defer warnings about undefined
911    overflow.  This facility exists because these warnings are a
912    special case.  The code to estimate loop iterations does not want
913    to issue any warnings, since it works with expressions which do not
914    occur in user code.  Various bits of cleanup code call fold(), but
915    only use the result if it has certain characteristics (e.g., is a
916    constant); that code only wants to issue a warning if the result is
917    used.  */
918
919 static int fold_deferring_overflow_warnings;
920
921 /* If a warning about undefined overflow is deferred, this is the
922    warning.  Note that this may cause us to turn two warnings into
923    one, but that is fine since it is sufficient to only give one
924    warning per expression.  */
925
926 static const char* fold_deferred_overflow_warning;
927
928 /* If a warning about undefined overflow is deferred, this is the
929    level at which the warning should be emitted.  */
930
931 static enum warn_strict_overflow_code fold_deferred_overflow_code;
932
933 /* Start deferring overflow warnings.  We could use a stack here to
934    permit nested calls, but at present it is not necessary.  */
935
936 void
937 fold_defer_overflow_warnings (void)
938 {
939   ++fold_deferring_overflow_warnings;
940 }
941
942 /* Stop deferring overflow warnings.  If there is a pending warning,
943    and ISSUE is true, then issue the warning if appropriate.  STMT is
944    the statement with which the warning should be associated (used for
945    location information); STMT may be NULL.  CODE is the level of the
946    warning--a warn_strict_overflow_code value.  This function will use
947    the smaller of CODE and the deferred code when deciding whether to
948    issue the warning.  CODE may be zero to mean to always use the
949    deferred code.  */
950
951 void
952 fold_undefer_overflow_warnings (bool issue, const_tree stmt, int code)
953 {
954   const char *warnmsg;
955   location_t locus;
956
957   gcc_assert (fold_deferring_overflow_warnings > 0);
958   --fold_deferring_overflow_warnings;
959   if (fold_deferring_overflow_warnings > 0)
960     {
961       if (fold_deferred_overflow_warning != NULL
962           && code != 0
963           && code < (int) fold_deferred_overflow_code)
964         fold_deferred_overflow_code = code;
965       return;
966     }
967
968   warnmsg = fold_deferred_overflow_warning;
969   fold_deferred_overflow_warning = NULL;
970
971   if (!issue || warnmsg == NULL)
972     return;
973
974   if (stmt != NULL_TREE && TREE_NO_WARNING (stmt))
975     return;
976
977   /* Use the smallest code level when deciding to issue the
978      warning.  */
979   if (code == 0 || code > (int) fold_deferred_overflow_code)
980     code = fold_deferred_overflow_code;
981
982   if (!issue_strict_overflow_warning (code))
983     return;
984
985   if (stmt == NULL_TREE || !expr_has_location (stmt))
986     locus = input_location;
987   else
988     locus = expr_location (stmt);
989   warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
990 }
991
992 /* Stop deferring overflow warnings, ignoring any deferred
993    warnings.  */
994
995 void
996 fold_undefer_and_ignore_overflow_warnings (void)
997 {
998   fold_undefer_overflow_warnings (false, NULL_TREE, 0);
999 }
1000
1001 /* Whether we are deferring overflow warnings.  */
1002
1003 bool
1004 fold_deferring_overflow_warnings_p (void)
1005 {
1006   return fold_deferring_overflow_warnings > 0;
1007 }
1008
1009 /* This is called when we fold something based on the fact that signed
1010    overflow is undefined.  */
1011
1012 static void
1013 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1014 {
1015   gcc_assert (!flag_wrapv && !flag_trapv);
1016   if (fold_deferring_overflow_warnings > 0)
1017     {
1018       if (fold_deferred_overflow_warning == NULL
1019           || wc < fold_deferred_overflow_code)
1020         {
1021           fold_deferred_overflow_warning = gmsgid;
1022           fold_deferred_overflow_code = wc;
1023         }
1024     }
1025   else if (issue_strict_overflow_warning (wc))
1026     warning (OPT_Wstrict_overflow, gmsgid);
1027 }
1028 \f
1029 /* Return true if the built-in mathematical function specified by CODE
1030    is odd, i.e. -f(x) == f(-x).  */
1031
1032 static bool
1033 negate_mathfn_p (enum built_in_function code)
1034 {
1035   switch (code)
1036     {
1037     CASE_FLT_FN (BUILT_IN_ASIN):
1038     CASE_FLT_FN (BUILT_IN_ASINH):
1039     CASE_FLT_FN (BUILT_IN_ATAN):
1040     CASE_FLT_FN (BUILT_IN_ATANH):
1041     CASE_FLT_FN (BUILT_IN_CASIN):
1042     CASE_FLT_FN (BUILT_IN_CASINH):
1043     CASE_FLT_FN (BUILT_IN_CATAN):
1044     CASE_FLT_FN (BUILT_IN_CATANH):
1045     CASE_FLT_FN (BUILT_IN_CBRT):
1046     CASE_FLT_FN (BUILT_IN_CPROJ):
1047     CASE_FLT_FN (BUILT_IN_CSIN):
1048     CASE_FLT_FN (BUILT_IN_CSINH):
1049     CASE_FLT_FN (BUILT_IN_CTAN):
1050     CASE_FLT_FN (BUILT_IN_CTANH):
1051     CASE_FLT_FN (BUILT_IN_ERF):
1052     CASE_FLT_FN (BUILT_IN_LLROUND):
1053     CASE_FLT_FN (BUILT_IN_LROUND):
1054     CASE_FLT_FN (BUILT_IN_ROUND):
1055     CASE_FLT_FN (BUILT_IN_SIN):
1056     CASE_FLT_FN (BUILT_IN_SINH):
1057     CASE_FLT_FN (BUILT_IN_TAN):
1058     CASE_FLT_FN (BUILT_IN_TANH):
1059     CASE_FLT_FN (BUILT_IN_TRUNC):
1060       return true;
1061
1062     CASE_FLT_FN (BUILT_IN_LLRINT):
1063     CASE_FLT_FN (BUILT_IN_LRINT):
1064     CASE_FLT_FN (BUILT_IN_NEARBYINT):
1065     CASE_FLT_FN (BUILT_IN_RINT):
1066       return !flag_rounding_math;
1067     
1068     default:
1069       break;
1070     }
1071   return false;
1072 }
1073
1074 /* Check whether we may negate an integer constant T without causing
1075    overflow.  */
1076
1077 bool
1078 may_negate_without_overflow_p (const_tree t)
1079 {
1080   unsigned HOST_WIDE_INT val;
1081   unsigned int prec;
1082   tree type;
1083
1084   gcc_assert (TREE_CODE (t) == INTEGER_CST);
1085
1086   type = TREE_TYPE (t);
1087   if (TYPE_UNSIGNED (type))
1088     return false;
1089
1090   prec = TYPE_PRECISION (type);
1091   if (prec > HOST_BITS_PER_WIDE_INT)
1092     {
1093       if (TREE_INT_CST_LOW (t) != 0)
1094         return true;
1095       prec -= HOST_BITS_PER_WIDE_INT;
1096       val = TREE_INT_CST_HIGH (t);
1097     }
1098   else
1099     val = TREE_INT_CST_LOW (t);
1100   if (prec < HOST_BITS_PER_WIDE_INT)
1101     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1102   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1103 }
1104
1105 /* Determine whether an expression T can be cheaply negated using
1106    the function negate_expr without introducing undefined overflow.  */
1107
1108 static bool
1109 negate_expr_p (tree t)
1110 {
1111   tree type;
1112
1113   if (t == 0)
1114     return false;
1115
1116   type = TREE_TYPE (t);
1117
1118   STRIP_SIGN_NOPS (t);
1119   switch (TREE_CODE (t))
1120     {
1121     case INTEGER_CST:
1122       if (TYPE_OVERFLOW_WRAPS (type))
1123         return true;
1124
1125       /* Check that -CST will not overflow type.  */
1126       return may_negate_without_overflow_p (t);
1127     case BIT_NOT_EXPR:
1128       return (INTEGRAL_TYPE_P (type)
1129               && TYPE_OVERFLOW_WRAPS (type));
1130
1131     case FIXED_CST:
1132     case REAL_CST:
1133     case NEGATE_EXPR:
1134       return true;
1135
1136     case COMPLEX_CST:
1137       return negate_expr_p (TREE_REALPART (t))
1138              && negate_expr_p (TREE_IMAGPART (t));
1139
1140     case COMPLEX_EXPR:
1141       return negate_expr_p (TREE_OPERAND (t, 0))
1142              && negate_expr_p (TREE_OPERAND (t, 1));
1143
1144     case CONJ_EXPR:
1145       return negate_expr_p (TREE_OPERAND (t, 0));
1146
1147     case PLUS_EXPR:
1148       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1149           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1150         return false;
1151       /* -(A + B) -> (-B) - A.  */
1152       if (negate_expr_p (TREE_OPERAND (t, 1))
1153           && reorder_operands_p (TREE_OPERAND (t, 0),
1154                                  TREE_OPERAND (t, 1)))
1155         return true;
1156       /* -(A + B) -> (-A) - B.  */
1157       return negate_expr_p (TREE_OPERAND (t, 0));
1158
1159     case MINUS_EXPR:
1160       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
1161       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1162              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1163              && reorder_operands_p (TREE_OPERAND (t, 0),
1164                                     TREE_OPERAND (t, 1));
1165
1166     case MULT_EXPR:
1167       if (TYPE_UNSIGNED (TREE_TYPE (t)))
1168         break;
1169
1170       /* Fall through.  */
1171
1172     case RDIV_EXPR:
1173       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1174         return negate_expr_p (TREE_OPERAND (t, 1))
1175                || negate_expr_p (TREE_OPERAND (t, 0));
1176       break;
1177
1178     case TRUNC_DIV_EXPR:
1179     case ROUND_DIV_EXPR:
1180     case FLOOR_DIV_EXPR:
1181     case CEIL_DIV_EXPR:
1182     case EXACT_DIV_EXPR:
1183       /* In general we can't negate A / B, because if A is INT_MIN and
1184          B is 1, we may turn this into INT_MIN / -1 which is undefined
1185          and actually traps on some architectures.  But if overflow is
1186          undefined, we can negate, because - (INT_MIN / 1) is an
1187          overflow.  */
1188       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1189           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1190         break;
1191       return negate_expr_p (TREE_OPERAND (t, 1))
1192              || negate_expr_p (TREE_OPERAND (t, 0));
1193
1194     case NOP_EXPR:
1195       /* Negate -((double)float) as (double)(-float).  */
1196       if (TREE_CODE (type) == REAL_TYPE)
1197         {
1198           tree tem = strip_float_extensions (t);
1199           if (tem != t)
1200             return negate_expr_p (tem);
1201         }
1202       break;
1203
1204     case CALL_EXPR:
1205       /* Negate -f(x) as f(-x).  */
1206       if (negate_mathfn_p (builtin_mathfn_code (t)))
1207         return negate_expr_p (CALL_EXPR_ARG (t, 0));
1208       break;
1209
1210     case RSHIFT_EXPR:
1211       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1212       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1213         {
1214           tree op1 = TREE_OPERAND (t, 1);
1215           if (TREE_INT_CST_HIGH (op1) == 0
1216               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1217                  == TREE_INT_CST_LOW (op1))
1218             return true;
1219         }
1220       break;
1221
1222     default:
1223       break;
1224     }
1225   return false;
1226 }
1227
1228 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1229    simplification is possible.
1230    If negate_expr_p would return true for T, NULL_TREE will never be
1231    returned.  */
1232
1233 static tree
1234 fold_negate_expr (tree t)
1235 {
1236   tree type = TREE_TYPE (t);
1237   tree tem;
1238
1239   switch (TREE_CODE (t))
1240     {
1241     /* Convert - (~A) to A + 1.  */
1242     case BIT_NOT_EXPR:
1243       if (INTEGRAL_TYPE_P (type))
1244         return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
1245                             build_int_cst (type, 1));
1246       break;
1247       
1248     case INTEGER_CST:
1249       tem = fold_negate_const (t, type);
1250       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1251           || !TYPE_OVERFLOW_TRAPS (type))
1252         return tem;
1253       break;
1254
1255     case REAL_CST:
1256       tem = fold_negate_const (t, type);
1257       /* Two's complement FP formats, such as c4x, may overflow.  */
1258       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1259         return tem;
1260       break;
1261
1262     case FIXED_CST:
1263       tem = fold_negate_const (t, type);
1264       return tem;
1265
1266     case COMPLEX_CST:
1267       {
1268         tree rpart = negate_expr (TREE_REALPART (t));
1269         tree ipart = negate_expr (TREE_IMAGPART (t));
1270
1271         if ((TREE_CODE (rpart) == REAL_CST
1272              && TREE_CODE (ipart) == REAL_CST)
1273             || (TREE_CODE (rpart) == INTEGER_CST
1274                 && TREE_CODE (ipart) == INTEGER_CST))
1275           return build_complex (type, rpart, ipart);
1276       }
1277       break;
1278
1279     case COMPLEX_EXPR:
1280       if (negate_expr_p (t))
1281         return fold_build2 (COMPLEX_EXPR, type,
1282                             fold_negate_expr (TREE_OPERAND (t, 0)),
1283                             fold_negate_expr (TREE_OPERAND (t, 1)));
1284       break;
1285       
1286     case CONJ_EXPR:
1287       if (negate_expr_p (t))
1288         return fold_build1 (CONJ_EXPR, type,
1289                             fold_negate_expr (TREE_OPERAND (t, 0)));
1290       break;
1291
1292     case NEGATE_EXPR:
1293       return TREE_OPERAND (t, 0);
1294
1295     case PLUS_EXPR:
1296       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1297           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1298         {
1299           /* -(A + B) -> (-B) - A.  */
1300           if (negate_expr_p (TREE_OPERAND (t, 1))
1301               && reorder_operands_p (TREE_OPERAND (t, 0),
1302                                      TREE_OPERAND (t, 1)))
1303             {
1304               tem = negate_expr (TREE_OPERAND (t, 1));
1305               return fold_build2 (MINUS_EXPR, type,
1306                                   tem, TREE_OPERAND (t, 0));
1307             }
1308
1309           /* -(A + B) -> (-A) - B.  */
1310           if (negate_expr_p (TREE_OPERAND (t, 0)))
1311             {
1312               tem = negate_expr (TREE_OPERAND (t, 0));
1313               return fold_build2 (MINUS_EXPR, type,
1314                                   tem, TREE_OPERAND (t, 1));
1315             }
1316         }
1317       break;
1318
1319     case MINUS_EXPR:
1320       /* - (A - B) -> B - A  */
1321       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1322           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1323           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1324         return fold_build2 (MINUS_EXPR, type,
1325                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1326       break;
1327
1328     case MULT_EXPR:
1329       if (TYPE_UNSIGNED (type))
1330         break;
1331
1332       /* Fall through.  */
1333
1334     case RDIV_EXPR:
1335       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1336         {
1337           tem = TREE_OPERAND (t, 1);
1338           if (negate_expr_p (tem))
1339             return fold_build2 (TREE_CODE (t), type,
1340                                 TREE_OPERAND (t, 0), negate_expr (tem));
1341           tem = TREE_OPERAND (t, 0);
1342           if (negate_expr_p (tem))
1343             return fold_build2 (TREE_CODE (t), type,
1344                                 negate_expr (tem), TREE_OPERAND (t, 1));
1345         }
1346       break;
1347
1348     case TRUNC_DIV_EXPR:
1349     case ROUND_DIV_EXPR:
1350     case FLOOR_DIV_EXPR:
1351     case CEIL_DIV_EXPR:
1352     case EXACT_DIV_EXPR:
1353       /* In general we can't negate A / B, because if A is INT_MIN and
1354          B is 1, we may turn this into INT_MIN / -1 which is undefined
1355          and actually traps on some architectures.  But if overflow is
1356          undefined, we can negate, because - (INT_MIN / 1) is an
1357          overflow.  */
1358       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1359         {
1360           const char * const warnmsg = G_("assuming signed overflow does not "
1361                                           "occur when negating a division");
1362           tem = TREE_OPERAND (t, 1);
1363           if (negate_expr_p (tem))
1364             {
1365               if (INTEGRAL_TYPE_P (type)
1366                   && (TREE_CODE (tem) != INTEGER_CST
1367                       || integer_onep (tem)))
1368                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1369               return fold_build2 (TREE_CODE (t), type,
1370                                   TREE_OPERAND (t, 0), negate_expr (tem));
1371             }
1372           tem = TREE_OPERAND (t, 0);
1373           if (negate_expr_p (tem))
1374             {
1375               if (INTEGRAL_TYPE_P (type)
1376                   && (TREE_CODE (tem) != INTEGER_CST
1377                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1378                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1379               return fold_build2 (TREE_CODE (t), type,
1380                                   negate_expr (tem), TREE_OPERAND (t, 1));
1381             }
1382         }
1383       break;
1384
1385     case NOP_EXPR:
1386       /* Convert -((double)float) into (double)(-float).  */
1387       if (TREE_CODE (type) == REAL_TYPE)
1388         {
1389           tem = strip_float_extensions (t);
1390           if (tem != t && negate_expr_p (tem))
1391             return fold_convert (type, negate_expr (tem));
1392         }
1393       break;
1394
1395     case CALL_EXPR:
1396       /* Negate -f(x) as f(-x).  */
1397       if (negate_mathfn_p (builtin_mathfn_code (t))
1398           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1399         {
1400           tree fndecl, arg;
1401
1402           fndecl = get_callee_fndecl (t);
1403           arg = negate_expr (CALL_EXPR_ARG (t, 0));
1404           return build_call_expr (fndecl, 1, arg);
1405         }
1406       break;
1407
1408     case RSHIFT_EXPR:
1409       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1410       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1411         {
1412           tree op1 = TREE_OPERAND (t, 1);
1413           if (TREE_INT_CST_HIGH (op1) == 0
1414               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1415                  == TREE_INT_CST_LOW (op1))
1416             {
1417               tree ntype = TYPE_UNSIGNED (type)
1418                            ? signed_type_for (type)
1419                            : unsigned_type_for (type);
1420               tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1421               temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1422               return fold_convert (type, temp);
1423             }
1424         }
1425       break;
1426
1427     default:
1428       break;
1429     }
1430
1431   return NULL_TREE;
1432 }
1433
1434 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1435    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
1436    return NULL_TREE. */
1437
1438 static tree
1439 negate_expr (tree t)
1440 {
1441   tree type, tem;
1442
1443   if (t == NULL_TREE)
1444     return NULL_TREE;
1445
1446   type = TREE_TYPE (t);
1447   STRIP_SIGN_NOPS (t);
1448
1449   tem = fold_negate_expr (t);
1450   if (!tem)
1451     tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1452   return fold_convert (type, tem);
1453 }
1454 \f
1455 /* Split a tree IN into a constant, literal and variable parts that could be
1456    combined with CODE to make IN.  "constant" means an expression with
1457    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1458    commutative arithmetic operation.  Store the constant part into *CONP,
1459    the literal in *LITP and return the variable part.  If a part isn't
1460    present, set it to null.  If the tree does not decompose in this way,
1461    return the entire tree as the variable part and the other parts as null.
1462
1463    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1464    case, we negate an operand that was subtracted.  Except if it is a
1465    literal for which we use *MINUS_LITP instead.
1466
1467    If NEGATE_P is true, we are negating all of IN, again except a literal
1468    for which we use *MINUS_LITP instead.
1469
1470    If IN is itself a literal or constant, return it as appropriate.
1471
1472    Note that we do not guarantee that any of the three values will be the
1473    same type as IN, but they will have the same signedness and mode.  */
1474
1475 static tree
1476 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1477             tree *minus_litp, int negate_p)
1478 {
1479   tree var = 0;
1480
1481   *conp = 0;
1482   *litp = 0;
1483   *minus_litp = 0;
1484
1485   /* Strip any conversions that don't change the machine mode or signedness.  */
1486   STRIP_SIGN_NOPS (in);
1487
1488   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1489       || TREE_CODE (in) == FIXED_CST)
1490     *litp = in;
1491   else if (TREE_CODE (in) == code
1492            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1493                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1494                /* We can associate addition and subtraction together (even
1495                   though the C standard doesn't say so) for integers because
1496                   the value is not affected.  For reals, the value might be
1497                   affected, so we can't.  */
1498                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1499                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1500     {
1501       tree op0 = TREE_OPERAND (in, 0);
1502       tree op1 = TREE_OPERAND (in, 1);
1503       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1504       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1505
1506       /* First see if either of the operands is a literal, then a constant.  */
1507       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1508           || TREE_CODE (op0) == FIXED_CST)
1509         *litp = op0, op0 = 0;
1510       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1511                || TREE_CODE (op1) == FIXED_CST)
1512         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1513
1514       if (op0 != 0 && TREE_CONSTANT (op0))
1515         *conp = op0, op0 = 0;
1516       else if (op1 != 0 && TREE_CONSTANT (op1))
1517         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1518
1519       /* If we haven't dealt with either operand, this is not a case we can
1520          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1521       if (op0 != 0 && op1 != 0)
1522         var = in;
1523       else if (op0 != 0)
1524         var = op0;
1525       else
1526         var = op1, neg_var_p = neg1_p;
1527
1528       /* Now do any needed negations.  */
1529       if (neg_litp_p)
1530         *minus_litp = *litp, *litp = 0;
1531       if (neg_conp_p)
1532         *conp = negate_expr (*conp);
1533       if (neg_var_p)
1534         var = negate_expr (var);
1535     }
1536   else if (TREE_CONSTANT (in))
1537     *conp = in;
1538   else
1539     var = in;
1540
1541   if (negate_p)
1542     {
1543       if (*litp)
1544         *minus_litp = *litp, *litp = 0;
1545       else if (*minus_litp)
1546         *litp = *minus_litp, *minus_litp = 0;
1547       *conp = negate_expr (*conp);
1548       var = negate_expr (var);
1549     }
1550
1551   return var;
1552 }
1553
1554 /* Re-associate trees split by the above function.  T1 and T2 are either
1555    expressions to associate or null.  Return the new expression, if any.  If
1556    we build an operation, do it in TYPE and with CODE.  */
1557
1558 static tree
1559 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1560 {
1561   if (t1 == 0)
1562     return t2;
1563   else if (t2 == 0)
1564     return t1;
1565
1566   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1567      try to fold this since we will have infinite recursion.  But do
1568      deal with any NEGATE_EXPRs.  */
1569   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1570       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1571     {
1572       if (code == PLUS_EXPR)
1573         {
1574           if (TREE_CODE (t1) == NEGATE_EXPR)
1575             return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1576                            fold_convert (type, TREE_OPERAND (t1, 0)));
1577           else if (TREE_CODE (t2) == NEGATE_EXPR)
1578             return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1579                            fold_convert (type, TREE_OPERAND (t2, 0)));
1580           else if (integer_zerop (t2))
1581             return fold_convert (type, t1);
1582         }
1583       else if (code == MINUS_EXPR)
1584         {
1585           if (integer_zerop (t2))
1586             return fold_convert (type, t1);
1587         }
1588
1589       return build2 (code, type, fold_convert (type, t1),
1590                      fold_convert (type, t2));
1591     }
1592
1593   return fold_build2 (code, type, fold_convert (type, t1),
1594                       fold_convert (type, t2));
1595 }
1596 \f
1597 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1598    for use in int_const_binop, size_binop and size_diffop.  */
1599
1600 static bool
1601 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1602 {
1603   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1604     return false;
1605   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1606     return false;
1607
1608   switch (code)
1609     {
1610     case LSHIFT_EXPR:
1611     case RSHIFT_EXPR:
1612     case LROTATE_EXPR:
1613     case RROTATE_EXPR:
1614       return true;
1615
1616     default:
1617       break;
1618     }
1619
1620   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1621          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1622          && TYPE_MODE (type1) == TYPE_MODE (type2);
1623 }
1624
1625
1626 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1627    to produce a new constant.  Return NULL_TREE if we don't know how
1628    to evaluate CODE at compile-time.
1629
1630    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1631
1632 tree
1633 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1634 {
1635   unsigned HOST_WIDE_INT int1l, int2l;
1636   HOST_WIDE_INT int1h, int2h;
1637   unsigned HOST_WIDE_INT low;
1638   HOST_WIDE_INT hi;
1639   unsigned HOST_WIDE_INT garbagel;
1640   HOST_WIDE_INT garbageh;
1641   tree t;
1642   tree type = TREE_TYPE (arg1);
1643   int uns = TYPE_UNSIGNED (type);
1644   int is_sizetype
1645     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1646   int overflow = 0;
1647
1648   int1l = TREE_INT_CST_LOW (arg1);
1649   int1h = TREE_INT_CST_HIGH (arg1);
1650   int2l = TREE_INT_CST_LOW (arg2);
1651   int2h = TREE_INT_CST_HIGH (arg2);
1652
1653   switch (code)
1654     {
1655     case BIT_IOR_EXPR:
1656       low = int1l | int2l, hi = int1h | int2h;
1657       break;
1658
1659     case BIT_XOR_EXPR:
1660       low = int1l ^ int2l, hi = int1h ^ int2h;
1661       break;
1662
1663     case BIT_AND_EXPR:
1664       low = int1l & int2l, hi = int1h & int2h;
1665       break;
1666
1667     case RSHIFT_EXPR:
1668       int2l = -int2l;
1669     case LSHIFT_EXPR:
1670       /* It's unclear from the C standard whether shifts can overflow.
1671          The following code ignores overflow; perhaps a C standard
1672          interpretation ruling is needed.  */
1673       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1674                      &low, &hi, !uns);
1675       break;
1676
1677     case RROTATE_EXPR:
1678       int2l = - int2l;
1679     case LROTATE_EXPR:
1680       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1681                       &low, &hi);
1682       break;
1683
1684     case PLUS_EXPR:
1685       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1686       break;
1687
1688     case MINUS_EXPR:
1689       neg_double (int2l, int2h, &low, &hi);
1690       add_double (int1l, int1h, low, hi, &low, &hi);
1691       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1692       break;
1693
1694     case MULT_EXPR:
1695       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1696       break;
1697
1698     case TRUNC_DIV_EXPR:
1699     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1700     case EXACT_DIV_EXPR:
1701       /* This is a shortcut for a common special case.  */
1702       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1703           && !TREE_OVERFLOW (arg1)
1704           && !TREE_OVERFLOW (arg2)
1705           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1706         {
1707           if (code == CEIL_DIV_EXPR)
1708             int1l += int2l - 1;
1709
1710           low = int1l / int2l, hi = 0;
1711           break;
1712         }
1713
1714       /* ... fall through ...  */
1715
1716     case ROUND_DIV_EXPR:
1717       if (int2h == 0 && int2l == 0)
1718         return NULL_TREE;
1719       if (int2h == 0 && int2l == 1)
1720         {
1721           low = int1l, hi = int1h;
1722           break;
1723         }
1724       if (int1l == int2l && int1h == int2h
1725           && ! (int1l == 0 && int1h == 0))
1726         {
1727           low = 1, hi = 0;
1728           break;
1729         }
1730       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1731                                        &low, &hi, &garbagel, &garbageh);
1732       break;
1733
1734     case TRUNC_MOD_EXPR:
1735     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1736       /* This is a shortcut for a common special case.  */
1737       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1738           && !TREE_OVERFLOW (arg1)
1739           && !TREE_OVERFLOW (arg2)
1740           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1741         {
1742           if (code == CEIL_MOD_EXPR)
1743             int1l += int2l - 1;
1744           low = int1l % int2l, hi = 0;
1745           break;
1746         }
1747
1748       /* ... fall through ...  */
1749
1750     case ROUND_MOD_EXPR:
1751       if (int2h == 0 && int2l == 0)
1752         return NULL_TREE;
1753       overflow = div_and_round_double (code, uns,
1754                                        int1l, int1h, int2l, int2h,
1755                                        &garbagel, &garbageh, &low, &hi);
1756       break;
1757
1758     case MIN_EXPR:
1759     case MAX_EXPR:
1760       if (uns)
1761         low = (((unsigned HOST_WIDE_INT) int1h
1762                 < (unsigned HOST_WIDE_INT) int2h)
1763                || (((unsigned HOST_WIDE_INT) int1h
1764                     == (unsigned HOST_WIDE_INT) int2h)
1765                    && int1l < int2l));
1766       else
1767         low = (int1h < int2h
1768                || (int1h == int2h && int1l < int2l));
1769
1770       if (low == (code == MIN_EXPR))
1771         low = int1l, hi = int1h;
1772       else
1773         low = int2l, hi = int2h;
1774       break;
1775
1776     default:
1777       return NULL_TREE;
1778     }
1779
1780   if (notrunc)
1781     {
1782       t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1783
1784       /* Propagate overflow flags ourselves.  */
1785       if (((!uns || is_sizetype) && overflow)
1786           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1787         {
1788           t = copy_node (t);
1789           TREE_OVERFLOW (t) = 1;
1790         }
1791     }
1792   else
1793     t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1794                                ((!uns || is_sizetype) && overflow)
1795                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1796
1797   return t;
1798 }
1799
1800 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1801    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1802    are the same kind of constant and the same machine mode.  Return zero if
1803    combining the constants is not allowed in the current operating mode.
1804
1805    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1806
1807 static tree
1808 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1809 {
1810   /* Sanity check for the recursive cases.  */
1811   if (!arg1 || !arg2)
1812     return NULL_TREE;
1813
1814   STRIP_NOPS (arg1);
1815   STRIP_NOPS (arg2);
1816
1817   if (TREE_CODE (arg1) == INTEGER_CST)
1818     return int_const_binop (code, arg1, arg2, notrunc);
1819
1820   if (TREE_CODE (arg1) == REAL_CST)
1821     {
1822       enum machine_mode mode;
1823       REAL_VALUE_TYPE d1;
1824       REAL_VALUE_TYPE d2;
1825       REAL_VALUE_TYPE value;
1826       REAL_VALUE_TYPE result;
1827       bool inexact;
1828       tree t, type;
1829
1830       /* The following codes are handled by real_arithmetic.  */
1831       switch (code)
1832         {
1833         case PLUS_EXPR:
1834         case MINUS_EXPR:
1835         case MULT_EXPR:
1836         case RDIV_EXPR:
1837         case MIN_EXPR:
1838         case MAX_EXPR:
1839           break;
1840
1841         default:
1842           return NULL_TREE;
1843         }
1844
1845       d1 = TREE_REAL_CST (arg1);
1846       d2 = TREE_REAL_CST (arg2);
1847
1848       type = TREE_TYPE (arg1);
1849       mode = TYPE_MODE (type);
1850
1851       /* Don't perform operation if we honor signaling NaNs and
1852          either operand is a NaN.  */
1853       if (HONOR_SNANS (mode)
1854           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1855         return NULL_TREE;
1856
1857       /* Don't perform operation if it would raise a division
1858          by zero exception.  */
1859       if (code == RDIV_EXPR
1860           && REAL_VALUES_EQUAL (d2, dconst0)
1861           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1862         return NULL_TREE;
1863
1864       /* If either operand is a NaN, just return it.  Otherwise, set up
1865          for floating-point trap; we return an overflow.  */
1866       if (REAL_VALUE_ISNAN (d1))
1867         return arg1;
1868       else if (REAL_VALUE_ISNAN (d2))
1869         return arg2;
1870
1871       inexact = real_arithmetic (&value, code, &d1, &d2);
1872       real_convert (&result, mode, &value);
1873
1874       /* Don't constant fold this floating point operation if
1875          the result has overflowed and flag_trapping_math.  */
1876       if (flag_trapping_math
1877           && MODE_HAS_INFINITIES (mode)
1878           && REAL_VALUE_ISINF (result)
1879           && !REAL_VALUE_ISINF (d1)
1880           && !REAL_VALUE_ISINF (d2))
1881         return NULL_TREE;
1882
1883       /* Don't constant fold this floating point operation if the
1884          result may dependent upon the run-time rounding mode and
1885          flag_rounding_math is set, or if GCC's software emulation
1886          is unable to accurately represent the result.  */
1887       if ((flag_rounding_math
1888            || (REAL_MODE_FORMAT_COMPOSITE_P (mode)
1889                && !flag_unsafe_math_optimizations))
1890           && (inexact || !real_identical (&result, &value)))
1891         return NULL_TREE;
1892
1893       t = build_real (type, result);
1894
1895       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1896       return t;
1897     }
1898
1899   if (TREE_CODE (arg1) == FIXED_CST)
1900     {
1901       FIXED_VALUE_TYPE f1;
1902       FIXED_VALUE_TYPE f2;
1903       FIXED_VALUE_TYPE result;
1904       tree t, type;
1905       int sat_p;
1906       bool overflow_p;
1907
1908       /* The following codes are handled by fixed_arithmetic.  */
1909       switch (code)
1910         {
1911         case PLUS_EXPR:
1912         case MINUS_EXPR:
1913         case MULT_EXPR:
1914         case TRUNC_DIV_EXPR:
1915           f2 = TREE_FIXED_CST (arg2);
1916           break;
1917
1918         case LSHIFT_EXPR:
1919         case RSHIFT_EXPR:
1920           f2.data.high = TREE_INT_CST_HIGH (arg2);
1921           f2.data.low = TREE_INT_CST_LOW (arg2);
1922           f2.mode = SImode;
1923           break;
1924
1925         default:
1926           return NULL_TREE;
1927         }
1928
1929       f1 = TREE_FIXED_CST (arg1);
1930       type = TREE_TYPE (arg1);
1931       sat_p = TYPE_SATURATING (type);
1932       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1933       t = build_fixed (type, result);
1934       /* Propagate overflow flags.  */
1935       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1936         {
1937           TREE_OVERFLOW (t) = 1;
1938           TREE_CONSTANT_OVERFLOW (t) = 1;
1939         }
1940       else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1941         TREE_CONSTANT_OVERFLOW (t) = 1;
1942       return t;
1943     }
1944
1945   if (TREE_CODE (arg1) == COMPLEX_CST)
1946     {
1947       tree type = TREE_TYPE (arg1);
1948       tree r1 = TREE_REALPART (arg1);
1949       tree i1 = TREE_IMAGPART (arg1);
1950       tree r2 = TREE_REALPART (arg2);
1951       tree i2 = TREE_IMAGPART (arg2);
1952       tree real, imag;
1953
1954       switch (code)
1955         {
1956         case PLUS_EXPR:
1957         case MINUS_EXPR:
1958           real = const_binop (code, r1, r2, notrunc);
1959           imag = const_binop (code, i1, i2, notrunc);
1960           break;
1961
1962         case MULT_EXPR:
1963           real = const_binop (MINUS_EXPR,
1964                               const_binop (MULT_EXPR, r1, r2, notrunc),
1965                               const_binop (MULT_EXPR, i1, i2, notrunc),
1966                               notrunc);
1967           imag = const_binop (PLUS_EXPR,
1968                               const_binop (MULT_EXPR, r1, i2, notrunc),
1969                               const_binop (MULT_EXPR, i1, r2, notrunc),
1970                               notrunc);
1971           break;
1972
1973         case RDIV_EXPR:
1974           {
1975             tree magsquared
1976               = const_binop (PLUS_EXPR,
1977                              const_binop (MULT_EXPR, r2, r2, notrunc),
1978                              const_binop (MULT_EXPR, i2, i2, notrunc),
1979                              notrunc);
1980             tree t1
1981               = const_binop (PLUS_EXPR,
1982                              const_binop (MULT_EXPR, r1, r2, notrunc),
1983                              const_binop (MULT_EXPR, i1, i2, notrunc),
1984                              notrunc);
1985             tree t2
1986               = const_binop (MINUS_EXPR,
1987                              const_binop (MULT_EXPR, i1, r2, notrunc),
1988                              const_binop (MULT_EXPR, r1, i2, notrunc),
1989                              notrunc);
1990
1991             if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1992               code = TRUNC_DIV_EXPR;
1993
1994             real = const_binop (code, t1, magsquared, notrunc);
1995             imag = const_binop (code, t2, magsquared, notrunc);
1996           }
1997           break;
1998
1999         default:
2000           return NULL_TREE;
2001         }
2002
2003       if (real && imag)
2004         return build_complex (type, real, imag);
2005     }
2006
2007   return NULL_TREE;
2008 }
2009
2010 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
2011    indicates which particular sizetype to create.  */
2012
2013 tree
2014 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2015 {
2016   return build_int_cst (sizetype_tab[(int) kind], number);
2017 }
2018 \f
2019 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
2020    is a tree code.  The type of the result is taken from the operands.
2021    Both must be equivalent integer types, ala int_binop_types_match_p.
2022    If the operands are constant, so is the result.  */
2023
2024 tree
2025 size_binop (enum tree_code code, tree arg0, tree arg1)
2026 {
2027   tree type = TREE_TYPE (arg0);
2028
2029   if (arg0 == error_mark_node || arg1 == error_mark_node)
2030     return error_mark_node;
2031
2032   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2033                                        TREE_TYPE (arg1)));
2034
2035   /* Handle the special case of two integer constants faster.  */
2036   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2037     {
2038       /* And some specific cases even faster than that.  */
2039       if (code == PLUS_EXPR)
2040         {
2041           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2042             return arg1;
2043           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2044             return arg0;
2045         }
2046       else if (code == MINUS_EXPR)
2047         {
2048           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2049             return arg0;
2050         }
2051       else if (code == MULT_EXPR)
2052         {
2053           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2054             return arg1;
2055         }
2056
2057       /* Handle general case of two integer constants.  */
2058       return int_const_binop (code, arg0, arg1, 0);
2059     }
2060
2061   return fold_build2 (code, type, arg0, arg1);
2062 }
2063
2064 /* Given two values, either both of sizetype or both of bitsizetype,
2065    compute the difference between the two values.  Return the value
2066    in signed type corresponding to the type of the operands.  */
2067
2068 tree
2069 size_diffop (tree arg0, tree arg1)
2070 {
2071   tree type = TREE_TYPE (arg0);
2072   tree ctype;
2073
2074   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2075                                        TREE_TYPE (arg1)));
2076
2077   /* If the type is already signed, just do the simple thing.  */
2078   if (!TYPE_UNSIGNED (type))
2079     return size_binop (MINUS_EXPR, arg0, arg1);
2080
2081   if (type == sizetype)
2082     ctype = ssizetype;
2083   else if (type == bitsizetype)
2084     ctype = sbitsizetype;
2085   else
2086     ctype = signed_type_for (type);
2087
2088   /* If either operand is not a constant, do the conversions to the signed
2089      type and subtract.  The hardware will do the right thing with any
2090      overflow in the subtraction.  */
2091   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2092     return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2093                        fold_convert (ctype, arg1));
2094
2095   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2096      Otherwise, subtract the other way, convert to CTYPE (we know that can't
2097      overflow) and negate (which can't either).  Special-case a result
2098      of zero while we're here.  */
2099   if (tree_int_cst_equal (arg0, arg1))
2100     return build_int_cst (ctype, 0);
2101   else if (tree_int_cst_lt (arg1, arg0))
2102     return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2103   else
2104     return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2105                        fold_convert (ctype, size_binop (MINUS_EXPR,
2106                                                         arg1, arg0)));
2107 }
2108 \f
2109 /* A subroutine of fold_convert_const handling conversions of an
2110    INTEGER_CST to another integer type.  */
2111
2112 static tree
2113 fold_convert_const_int_from_int (tree type, const_tree arg1)
2114 {
2115   tree t;
2116
2117   /* Given an integer constant, make new constant with new type,
2118      appropriately sign-extended or truncated.  */
2119   t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2120                              TREE_INT_CST_HIGH (arg1),
2121                              /* Don't set the overflow when
2122                                 converting from a pointer,  */
2123                              !POINTER_TYPE_P (TREE_TYPE (arg1))
2124                              /* or to a sizetype with same signedness
2125                                 and the precision is unchanged.
2126                                 ???  sizetype is always sign-extended,
2127                                 but its signedness depends on the
2128                                 frontend.  Thus we see spurious overflows
2129                                 here if we do not check this.  */
2130                              && !((TYPE_PRECISION (TREE_TYPE (arg1))
2131                                    == TYPE_PRECISION (type))
2132                                   && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2133                                       == TYPE_UNSIGNED (type))
2134                                   && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2135                                        && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2136                                       || (TREE_CODE (type) == INTEGER_TYPE
2137                                           && TYPE_IS_SIZETYPE (type)))),
2138                              (TREE_INT_CST_HIGH (arg1) < 0
2139                               && (TYPE_UNSIGNED (type)
2140                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2141                              | TREE_OVERFLOW (arg1));
2142
2143   return t;
2144 }
2145
2146 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2147    to an integer type.  */
2148
2149 static tree
2150 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2151 {
2152   int overflow = 0;
2153   tree t;
2154
2155   /* The following code implements the floating point to integer
2156      conversion rules required by the Java Language Specification,
2157      that IEEE NaNs are mapped to zero and values that overflow
2158      the target precision saturate, i.e. values greater than
2159      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2160      are mapped to INT_MIN.  These semantics are allowed by the
2161      C and C++ standards that simply state that the behavior of
2162      FP-to-integer conversion is unspecified upon overflow.  */
2163
2164   HOST_WIDE_INT high, low;
2165   REAL_VALUE_TYPE r;
2166   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2167
2168   switch (code)
2169     {
2170     case FIX_TRUNC_EXPR:
2171       real_trunc (&r, VOIDmode, &x);
2172       break;
2173
2174     default:
2175       gcc_unreachable ();
2176     }
2177
2178   /* If R is NaN, return zero and show we have an overflow.  */
2179   if (REAL_VALUE_ISNAN (r))
2180     {
2181       overflow = 1;
2182       high = 0;
2183       low = 0;
2184     }
2185
2186   /* See if R is less than the lower bound or greater than the
2187      upper bound.  */
2188
2189   if (! overflow)
2190     {
2191       tree lt = TYPE_MIN_VALUE (type);
2192       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2193       if (REAL_VALUES_LESS (r, l))
2194         {
2195           overflow = 1;
2196           high = TREE_INT_CST_HIGH (lt);
2197           low = TREE_INT_CST_LOW (lt);
2198         }
2199     }
2200
2201   if (! overflow)
2202     {
2203       tree ut = TYPE_MAX_VALUE (type);
2204       if (ut)
2205         {
2206           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2207           if (REAL_VALUES_LESS (u, r))
2208             {
2209               overflow = 1;
2210               high = TREE_INT_CST_HIGH (ut);
2211               low = TREE_INT_CST_LOW (ut);
2212             }
2213         }
2214     }
2215
2216   if (! overflow)
2217     REAL_VALUE_TO_INT (&low, &high, r);
2218
2219   t = force_fit_type_double (type, low, high, -1,
2220                              overflow | TREE_OVERFLOW (arg1));
2221   return t;
2222 }
2223
2224 /* A subroutine of fold_convert_const handling conversions of a
2225    FIXED_CST to an integer type.  */
2226
2227 static tree
2228 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2229 {
2230   tree t;
2231   double_int temp, temp_trunc;
2232   unsigned int mode;
2233
2234   /* Right shift FIXED_CST to temp by fbit.  */
2235   temp = TREE_FIXED_CST (arg1).data;
2236   mode = TREE_FIXED_CST (arg1).mode;
2237   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2238     {
2239       lshift_double (temp.low, temp.high,
2240                      - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2241                      &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2242
2243       /* Left shift temp to temp_trunc by fbit.  */
2244       lshift_double (temp.low, temp.high,
2245                      GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2246                      &temp_trunc.low, &temp_trunc.high,
2247                      SIGNED_FIXED_POINT_MODE_P (mode));
2248     }
2249   else
2250     {
2251       temp.low = 0;
2252       temp.high = 0;
2253       temp_trunc.low = 0;
2254       temp_trunc.high = 0;
2255     }
2256
2257   /* If FIXED_CST is negative, we need to round the value toward 0.
2258      By checking if the fractional bits are not zero to add 1 to temp.  */
2259   if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2260       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2261     {
2262       double_int one;
2263       one.low = 1;
2264       one.high = 0;
2265       temp = double_int_add (temp, one);
2266     }
2267
2268   /* Given a fixed-point constant, make new constant with new type,
2269      appropriately sign-extended or truncated.  */
2270   t = force_fit_type_double (type, temp.low, temp.high, -1,
2271                              (temp.high < 0
2272                               && (TYPE_UNSIGNED (type)
2273                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2274                              | TREE_OVERFLOW (arg1));
2275
2276   return t;
2277 }
2278
2279 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2280    to another floating point type.  */
2281
2282 static tree
2283 fold_convert_const_real_from_real (tree type, const_tree arg1)
2284 {
2285   REAL_VALUE_TYPE value;
2286   tree t;
2287
2288   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2289   t = build_real (type, value);
2290
2291   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2292   return t;
2293 }
2294
2295 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2296    to a floating point type.  */
2297
2298 static tree
2299 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2300 {
2301   REAL_VALUE_TYPE value;
2302   tree t;
2303
2304   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2305   t = build_real (type, value);
2306
2307   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2308   TREE_CONSTANT_OVERFLOW (t)
2309     = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2310   return t;
2311 }
2312
2313 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2314    to another fixed-point type.  */
2315
2316 static tree
2317 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2318 {
2319   FIXED_VALUE_TYPE value;
2320   tree t;
2321   bool overflow_p;
2322
2323   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2324                               TYPE_SATURATING (type));
2325   t = build_fixed (type, value);
2326
2327   /* Propagate overflow flags.  */
2328   if (overflow_p | TREE_OVERFLOW (arg1))
2329     {
2330       TREE_OVERFLOW (t) = 1;
2331       TREE_CONSTANT_OVERFLOW (t) = 1;
2332     }
2333   else if (TREE_CONSTANT_OVERFLOW (arg1))
2334     TREE_CONSTANT_OVERFLOW (t) = 1;
2335   return t;
2336 }
2337
2338 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2339    to a fixed-point type.  */
2340
2341 static tree
2342 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2343 {
2344   FIXED_VALUE_TYPE value;
2345   tree t;
2346   bool overflow_p;
2347
2348   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2349                                        TREE_INT_CST (arg1),
2350                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
2351                                        TYPE_SATURATING (type));
2352   t = build_fixed (type, value);
2353
2354   /* Propagate overflow flags.  */
2355   if (overflow_p | TREE_OVERFLOW (arg1))
2356     {
2357       TREE_OVERFLOW (t) = 1;
2358       TREE_CONSTANT_OVERFLOW (t) = 1;
2359     }
2360   else if (TREE_CONSTANT_OVERFLOW (arg1))
2361     TREE_CONSTANT_OVERFLOW (t) = 1;
2362   return t;
2363 }
2364
2365 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2366    to a fixed-point type.  */
2367
2368 static tree
2369 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2370 {
2371   FIXED_VALUE_TYPE value;
2372   tree t;
2373   bool overflow_p;
2374
2375   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2376                                         &TREE_REAL_CST (arg1),
2377                                         TYPE_SATURATING (type));
2378   t = build_fixed (type, value);
2379
2380   /* Propagate overflow flags.  */
2381   if (overflow_p | TREE_OVERFLOW (arg1))
2382     {
2383       TREE_OVERFLOW (t) = 1;
2384       TREE_CONSTANT_OVERFLOW (t) = 1;
2385     }
2386   else if (TREE_CONSTANT_OVERFLOW (arg1))
2387     TREE_CONSTANT_OVERFLOW (t) = 1;
2388   return t;
2389 }
2390
2391 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2392    type TYPE.  If no simplification can be done return NULL_TREE.  */
2393
2394 static tree
2395 fold_convert_const (enum tree_code code, tree type, tree arg1)
2396 {
2397   if (TREE_TYPE (arg1) == type)
2398     return arg1;
2399
2400   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2401     {
2402       if (TREE_CODE (arg1) == INTEGER_CST)
2403         return fold_convert_const_int_from_int (type, arg1);
2404       else if (TREE_CODE (arg1) == REAL_CST)
2405         return fold_convert_const_int_from_real (code, type, arg1);
2406       else if (TREE_CODE (arg1) == FIXED_CST)
2407         return fold_convert_const_int_from_fixed (type, arg1);
2408     }
2409   else if (TREE_CODE (type) == REAL_TYPE)
2410     {
2411       if (TREE_CODE (arg1) == INTEGER_CST)
2412         return build_real_from_int_cst (type, arg1);
2413       else if (TREE_CODE (arg1) == REAL_CST)
2414         return fold_convert_const_real_from_real (type, arg1);
2415       else if (TREE_CODE (arg1) == FIXED_CST)
2416         return fold_convert_const_real_from_fixed (type, arg1);
2417     }
2418   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2419     {
2420       if (TREE_CODE (arg1) == FIXED_CST)
2421         return fold_convert_const_fixed_from_fixed (type, arg1);
2422       else if (TREE_CODE (arg1) == INTEGER_CST)
2423         return fold_convert_const_fixed_from_int (type, arg1);
2424       else if (TREE_CODE (arg1) == REAL_CST)
2425         return fold_convert_const_fixed_from_real (type, arg1);
2426     }
2427   return NULL_TREE;
2428 }
2429
2430 /* Construct a vector of zero elements of vector type TYPE.  */
2431
2432 static tree
2433 build_zero_vector (tree type)
2434 {
2435   tree elem, list;
2436   int i, units;
2437
2438   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2439   units = TYPE_VECTOR_SUBPARTS (type);
2440   
2441   list = NULL_TREE;
2442   for (i = 0; i < units; i++)
2443     list = tree_cons (NULL_TREE, elem, list);
2444   return build_vector (type, list);
2445 }
2446
2447 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
2448
2449 bool
2450 fold_convertible_p (const_tree type, const_tree arg)
2451 {
2452   tree orig = TREE_TYPE (arg);
2453
2454   if (type == orig)
2455     return true;
2456
2457   if (TREE_CODE (arg) == ERROR_MARK
2458       || TREE_CODE (type) == ERROR_MARK
2459       || TREE_CODE (orig) == ERROR_MARK)
2460     return false;
2461
2462   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2463     return true;
2464
2465   switch (TREE_CODE (type))
2466     {
2467     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2468     case POINTER_TYPE: case REFERENCE_TYPE:
2469     case OFFSET_TYPE:
2470       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2471           || TREE_CODE (orig) == OFFSET_TYPE)
2472         return true;
2473       return (TREE_CODE (orig) == VECTOR_TYPE
2474               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2475
2476     case REAL_TYPE:
2477     case FIXED_POINT_TYPE:
2478     case COMPLEX_TYPE:
2479     case VECTOR_TYPE:
2480     case VOID_TYPE:
2481       return TREE_CODE (type) == TREE_CODE (orig);
2482
2483     default:
2484       return false;
2485     }
2486 }
2487
2488 /* Convert expression ARG to type TYPE.  Used by the middle-end for
2489    simple conversions in preference to calling the front-end's convert.  */
2490
2491 tree
2492 fold_convert (tree type, tree arg)
2493 {
2494   tree orig = TREE_TYPE (arg);
2495   tree tem;
2496
2497   if (type == orig)
2498     return arg;
2499
2500   if (TREE_CODE (arg) == ERROR_MARK
2501       || TREE_CODE (type) == ERROR_MARK
2502       || TREE_CODE (orig) == ERROR_MARK)
2503     return error_mark_node;
2504
2505   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2506     return fold_build1 (NOP_EXPR, type, arg);
2507
2508   switch (TREE_CODE (type))
2509     {
2510     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2511     case POINTER_TYPE: case REFERENCE_TYPE:
2512     case OFFSET_TYPE:
2513       if (TREE_CODE (arg) == INTEGER_CST)
2514         {
2515           tem = fold_convert_const (NOP_EXPR, type, arg);
2516           if (tem != NULL_TREE)
2517             return tem;
2518         }
2519       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2520           || TREE_CODE (orig) == OFFSET_TYPE)
2521         return fold_build1 (NOP_EXPR, type, arg);
2522       if (TREE_CODE (orig) == COMPLEX_TYPE)
2523         {
2524           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2525           return fold_convert (type, tem);
2526         }
2527       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2528                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2529       return fold_build1 (NOP_EXPR, type, arg);
2530
2531     case REAL_TYPE:
2532       if (TREE_CODE (arg) == INTEGER_CST)
2533         {
2534           tem = fold_convert_const (FLOAT_EXPR, type, arg);
2535           if (tem != NULL_TREE)
2536             return tem;
2537         }
2538       else if (TREE_CODE (arg) == REAL_CST)
2539         {
2540           tem = fold_convert_const (NOP_EXPR, type, arg);
2541           if (tem != NULL_TREE)
2542             return tem;
2543         }
2544       else if (TREE_CODE (arg) == FIXED_CST)
2545         {
2546           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2547           if (tem != NULL_TREE)
2548             return tem;
2549         }
2550
2551       switch (TREE_CODE (orig))
2552         {
2553         case INTEGER_TYPE:
2554         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2555         case POINTER_TYPE: case REFERENCE_TYPE:
2556           return fold_build1 (FLOAT_EXPR, type, arg);
2557
2558         case REAL_TYPE:
2559           return fold_build1 (NOP_EXPR, type, arg);
2560
2561         case FIXED_POINT_TYPE:
2562           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2563
2564         case COMPLEX_TYPE:
2565           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2566           return fold_convert (type, tem);
2567
2568         default:
2569           gcc_unreachable ();
2570         }
2571
2572     case FIXED_POINT_TYPE:
2573       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2574           || TREE_CODE (arg) == REAL_CST)
2575         {
2576           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2577           if (tem != NULL_TREE)
2578             return tem;
2579         }
2580
2581       switch (TREE_CODE (orig))
2582         {
2583         case FIXED_POINT_TYPE:
2584         case INTEGER_TYPE:
2585         case ENUMERAL_TYPE:
2586         case BOOLEAN_TYPE:
2587         case REAL_TYPE:
2588           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2589
2590         case COMPLEX_TYPE:
2591           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2592           return fold_convert (type, tem);
2593
2594         default:
2595           gcc_unreachable ();
2596         }
2597
2598     case COMPLEX_TYPE:
2599       switch (TREE_CODE (orig))
2600         {
2601         case INTEGER_TYPE:
2602         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2603         case POINTER_TYPE: case REFERENCE_TYPE:
2604         case REAL_TYPE:
2605         case FIXED_POINT_TYPE:
2606           return build2 (COMPLEX_EXPR, type,
2607                          fold_convert (TREE_TYPE (type), arg),
2608                          fold_convert (TREE_TYPE (type), integer_zero_node));
2609         case COMPLEX_TYPE:
2610           {
2611             tree rpart, ipart;
2612
2613             if (TREE_CODE (arg) == COMPLEX_EXPR)
2614               {
2615                 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2616                 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2617                 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2618               }
2619
2620             arg = save_expr (arg);
2621             rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2622             ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2623             rpart = fold_convert (TREE_TYPE (type), rpart);
2624             ipart = fold_convert (TREE_TYPE (type), ipart);
2625             return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2626           }
2627
2628         default:
2629           gcc_unreachable ();
2630         }
2631
2632     case VECTOR_TYPE:
2633       if (integer_zerop (arg))
2634         return build_zero_vector (type);
2635       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2636       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2637                   || TREE_CODE (orig) == VECTOR_TYPE);
2638       return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2639
2640     case VOID_TYPE:
2641       tem = fold_ignored_result (arg);
2642       if (TREE_CODE (tem) == GIMPLE_MODIFY_STMT)
2643         return tem;
2644       return fold_build1 (NOP_EXPR, type, tem);
2645
2646     default:
2647       gcc_unreachable ();
2648     }
2649 }
2650 \f
2651 /* Return false if expr can be assumed not to be an lvalue, true
2652    otherwise.  */
2653
2654 static bool
2655 maybe_lvalue_p (const_tree x)
2656 {
2657   /* We only need to wrap lvalue tree codes.  */
2658   switch (TREE_CODE (x))
2659   {
2660   case VAR_DECL:
2661   case PARM_DECL:
2662   case RESULT_DECL:
2663   case LABEL_DECL:
2664   case FUNCTION_DECL:
2665   case SSA_NAME:
2666
2667   case COMPONENT_REF:
2668   case INDIRECT_REF:
2669   case ALIGN_INDIRECT_REF:
2670   case MISALIGNED_INDIRECT_REF:
2671   case ARRAY_REF:
2672   case ARRAY_RANGE_REF:
2673   case BIT_FIELD_REF:
2674   case OBJ_TYPE_REF:
2675
2676   case REALPART_EXPR:
2677   case IMAGPART_EXPR:
2678   case PREINCREMENT_EXPR:
2679   case PREDECREMENT_EXPR:
2680   case SAVE_EXPR:
2681   case TRY_CATCH_EXPR:
2682   case WITH_CLEANUP_EXPR:
2683   case COMPOUND_EXPR:
2684   case MODIFY_EXPR:
2685   case GIMPLE_MODIFY_STMT:
2686   case TARGET_EXPR:
2687   case COND_EXPR:
2688   case BIND_EXPR:
2689   case MIN_EXPR:
2690   case MAX_EXPR:
2691     break;
2692
2693   default:
2694     /* Assume the worst for front-end tree codes.  */
2695     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2696       break;
2697     return false;
2698   }
2699
2700   return true;
2701 }
2702
2703 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2704
2705 tree
2706 non_lvalue (tree x)
2707 {
2708   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2709      us.  */
2710   if (in_gimple_form)
2711     return x;
2712
2713   if (! maybe_lvalue_p (x))
2714     return x;
2715   return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2716 }
2717
2718 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2719    Zero means allow extended lvalues.  */
2720
2721 int pedantic_lvalues;
2722
2723 /* When pedantic, return an expr equal to X but certainly not valid as a
2724    pedantic lvalue.  Otherwise, return X.  */
2725
2726 static tree
2727 pedantic_non_lvalue (tree x)
2728 {
2729   if (pedantic_lvalues)
2730     return non_lvalue (x);
2731   else
2732     return x;
2733 }
2734 \f
2735 /* Given a tree comparison code, return the code that is the logical inverse
2736    of the given code.  It is not safe to do this for floating-point
2737    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2738    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2739
2740 enum tree_code
2741 invert_tree_comparison (enum tree_code code, bool honor_nans)
2742 {
2743   if (honor_nans && flag_trapping_math)
2744     return ERROR_MARK;
2745
2746   switch (code)
2747     {
2748     case EQ_EXPR:
2749       return NE_EXPR;
2750     case NE_EXPR:
2751       return EQ_EXPR;
2752     case GT_EXPR:
2753       return honor_nans ? UNLE_EXPR : LE_EXPR;
2754     case GE_EXPR:
2755       return honor_nans ? UNLT_EXPR : LT_EXPR;
2756     case LT_EXPR:
2757       return honor_nans ? UNGE_EXPR : GE_EXPR;
2758     case LE_EXPR:
2759       return honor_nans ? UNGT_EXPR : GT_EXPR;
2760     case LTGT_EXPR:
2761       return UNEQ_EXPR;
2762     case UNEQ_EXPR:
2763       return LTGT_EXPR;
2764     case UNGT_EXPR:
2765       return LE_EXPR;
2766     case UNGE_EXPR:
2767       return LT_EXPR;
2768     case UNLT_EXPR:
2769       return GE_EXPR;
2770     case UNLE_EXPR:
2771       return GT_EXPR;
2772     case ORDERED_EXPR:
2773       return UNORDERED_EXPR;
2774     case UNORDERED_EXPR:
2775       return ORDERED_EXPR;
2776     default:
2777       gcc_unreachable ();
2778     }
2779 }
2780
2781 /* Similar, but return the comparison that results if the operands are
2782    swapped.  This is safe for floating-point.  */
2783
2784 enum tree_code
2785 swap_tree_comparison (enum tree_code code)
2786 {
2787   switch (code)
2788     {
2789     case EQ_EXPR:
2790     case NE_EXPR:
2791     case ORDERED_EXPR:
2792     case UNORDERED_EXPR:
2793     case LTGT_EXPR:
2794     case UNEQ_EXPR:
2795       return code;
2796     case GT_EXPR:
2797       return LT_EXPR;
2798     case GE_EXPR:
2799       return LE_EXPR;
2800     case LT_EXPR:
2801       return GT_EXPR;
2802     case LE_EXPR:
2803       return GE_EXPR;
2804     case UNGT_EXPR:
2805       return UNLT_EXPR;
2806     case UNGE_EXPR:
2807       return UNLE_EXPR;
2808     case UNLT_EXPR:
2809       return UNGT_EXPR;
2810     case UNLE_EXPR:
2811       return UNGE_EXPR;
2812     default:
2813       gcc_unreachable ();
2814     }
2815 }
2816
2817
2818 /* Convert a comparison tree code from an enum tree_code representation
2819    into a compcode bit-based encoding.  This function is the inverse of
2820    compcode_to_comparison.  */
2821
2822 static enum comparison_code
2823 comparison_to_compcode (enum tree_code code)
2824 {
2825   switch (code)
2826     {
2827     case LT_EXPR:
2828       return COMPCODE_LT;
2829     case EQ_EXPR:
2830       return COMPCODE_EQ;
2831     case LE_EXPR:
2832       return COMPCODE_LE;
2833     case GT_EXPR:
2834       return COMPCODE_GT;
2835     case NE_EXPR:
2836       return COMPCODE_NE;
2837     case GE_EXPR:
2838       return COMPCODE_GE;
2839     case ORDERED_EXPR:
2840       return COMPCODE_ORD;
2841     case UNORDERED_EXPR:
2842       return COMPCODE_UNORD;
2843     case UNLT_EXPR:
2844       return COMPCODE_UNLT;
2845     case UNEQ_EXPR:
2846       return COMPCODE_UNEQ;
2847     case UNLE_EXPR:
2848       return COMPCODE_UNLE;
2849     case UNGT_EXPR:
2850       return COMPCODE_UNGT;
2851     case LTGT_EXPR:
2852       return COMPCODE_LTGT;
2853     case UNGE_EXPR:
2854       return COMPCODE_UNGE;
2855     default:
2856       gcc_unreachable ();
2857     }
2858 }
2859
2860 /* Convert a compcode bit-based encoding of a comparison operator back
2861    to GCC's enum tree_code representation.  This function is the
2862    inverse of comparison_to_compcode.  */
2863
2864 static enum tree_code
2865 compcode_to_comparison (enum comparison_code code)
2866 {
2867   switch (code)
2868     {
2869     case COMPCODE_LT:
2870       return LT_EXPR;
2871     case COMPCODE_EQ:
2872       return EQ_EXPR;
2873     case COMPCODE_LE:
2874       return LE_EXPR;
2875     case COMPCODE_GT:
2876       return GT_EXPR;
2877     case COMPCODE_NE:
2878       return NE_EXPR;
2879     case COMPCODE_GE:
2880       return GE_EXPR;
2881     case COMPCODE_ORD:
2882       return ORDERED_EXPR;
2883     case COMPCODE_UNORD:
2884       return UNORDERED_EXPR;
2885     case COMPCODE_UNLT:
2886       return UNLT_EXPR;
2887     case COMPCODE_UNEQ:
2888       return UNEQ_EXPR;
2889     case COMPCODE_UNLE:
2890       return UNLE_EXPR;
2891     case COMPCODE_UNGT:
2892       return UNGT_EXPR;
2893     case COMPCODE_LTGT:
2894       return LTGT_EXPR;
2895     case COMPCODE_UNGE:
2896       return UNGE_EXPR;
2897     default:
2898       gcc_unreachable ();
2899     }
2900 }
2901
2902 /* Return a tree for the comparison which is the combination of
2903    doing the AND or OR (depending on CODE) of the two operations LCODE
2904    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2905    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2906    if this makes the transformation invalid.  */
2907
2908 tree
2909 combine_comparisons (enum tree_code code, enum tree_code lcode,
2910                      enum tree_code rcode, tree truth_type,
2911                      tree ll_arg, tree lr_arg)
2912 {
2913   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2914   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2915   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2916   enum comparison_code compcode;
2917
2918   switch (code)
2919     {
2920     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2921       compcode = lcompcode & rcompcode;
2922       break;
2923
2924     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2925       compcode = lcompcode | rcompcode;
2926       break;
2927
2928     default:
2929       return NULL_TREE;
2930     }
2931
2932   if (!honor_nans)
2933     {
2934       /* Eliminate unordered comparisons, as well as LTGT and ORD
2935          which are not used unless the mode has NaNs.  */
2936       compcode &= ~COMPCODE_UNORD;
2937       if (compcode == COMPCODE_LTGT)
2938         compcode = COMPCODE_NE;
2939       else if (compcode == COMPCODE_ORD)
2940         compcode = COMPCODE_TRUE;
2941     }
2942    else if (flag_trapping_math)
2943      {
2944         /* Check that the original operation and the optimized ones will trap
2945            under the same condition.  */
2946         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2947                      && (lcompcode != COMPCODE_EQ)
2948                      && (lcompcode != COMPCODE_ORD);
2949         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2950                      && (rcompcode != COMPCODE_EQ)
2951                      && (rcompcode != COMPCODE_ORD);
2952         bool trap = (compcode & COMPCODE_UNORD) == 0
2953                     && (compcode != COMPCODE_EQ)
2954                     && (compcode != COMPCODE_ORD);
2955
2956         /* In a short-circuited boolean expression the LHS might be
2957            such that the RHS, if evaluated, will never trap.  For
2958            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2959            if neither x nor y is NaN.  (This is a mixed blessing: for
2960            example, the expression above will never trap, hence
2961            optimizing it to x < y would be invalid).  */
2962         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2963             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2964           rtrap = false;
2965
2966         /* If the comparison was short-circuited, and only the RHS
2967            trapped, we may now generate a spurious trap.  */
2968         if (rtrap && !ltrap
2969             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2970           return NULL_TREE;
2971
2972         /* If we changed the conditions that cause a trap, we lose.  */
2973         if ((ltrap || rtrap) != trap)
2974           return NULL_TREE;
2975       }
2976
2977   if (compcode == COMPCODE_TRUE)
2978     return constant_boolean_node (true, truth_type);
2979   else if (compcode == COMPCODE_FALSE)
2980     return constant_boolean_node (false, truth_type);
2981   else
2982     return fold_build2 (compcode_to_comparison (compcode),
2983                         truth_type, ll_arg, lr_arg);
2984 }
2985
2986 /* Return nonzero if CODE is a tree code that represents a truth value.  */
2987
2988 static int
2989 truth_value_p (enum tree_code code)
2990 {
2991   return (TREE_CODE_CLASS (code) == tcc_comparison
2992           || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2993           || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2994           || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2995 }
2996 \f
2997 /* Return nonzero if two operands (typically of the same tree node)
2998    are necessarily equal.  If either argument has side-effects this
2999    function returns zero.  FLAGS modifies behavior as follows:
3000
3001    If OEP_ONLY_CONST is set, only return nonzero for constants.
3002    This function tests whether the operands are indistinguishable;
3003    it does not test whether they are equal using C's == operation.
3004    The distinction is important for IEEE floating point, because
3005    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3006    (2) two NaNs may be indistinguishable, but NaN!=NaN.
3007
3008    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3009    even though it may hold multiple values during a function.
3010    This is because a GCC tree node guarantees that nothing else is
3011    executed between the evaluation of its "operands" (which may often
3012    be evaluated in arbitrary order).  Hence if the operands themselves
3013    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3014    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
3015    unset means assuming isochronic (or instantaneous) tree equivalence.
3016    Unless comparing arbitrary expression trees, such as from different
3017    statements, this flag can usually be left unset.
3018
3019    If OEP_PURE_SAME is set, then pure functions with identical arguments
3020    are considered the same.  It is used when the caller has other ways
3021    to ensure that global memory is unchanged in between.  */
3022
3023 int
3024 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3025 {
3026   /* If either is ERROR_MARK, they aren't equal.  */
3027   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3028     return 0;
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.  */
3033   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3034     return 0;
3035
3036   /* If both types don't have the same precision, then it is not safe
3037      to strip NOPs.  */
3038   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3039     return 0;
3040
3041   STRIP_NOPS (arg0);
3042   STRIP_NOPS (arg1);
3043
3044   /* In case both args are comparisons but with different comparison
3045      code, try to swap the comparison operands of one arg to produce
3046      a match and compare that variant.  */
3047   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3048       && COMPARISON_CLASS_P (arg0)
3049       && COMPARISON_CLASS_P (arg1))
3050     {
3051       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3052
3053       if (TREE_CODE (arg0) == swap_code)
3054         return operand_equal_p (TREE_OPERAND (arg0, 0),
3055                                 TREE_OPERAND (arg1, 1), flags)
3056                && operand_equal_p (TREE_OPERAND (arg0, 1),
3057                                    TREE_OPERAND (arg1, 0), flags);
3058     }
3059
3060   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3061       /* This is needed for conversions and for COMPONENT_REF.
3062          Might as well play it safe and always test this.  */
3063       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3064       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3065       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3066     return 0;
3067
3068   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3069      We don't care about side effects in that case because the SAVE_EXPR
3070      takes care of that for us. In all other cases, two expressions are
3071      equal if they have no side effects.  If we have two identical
3072      expressions with side effects that should be treated the same due
3073      to the only side effects being identical SAVE_EXPR's, that will
3074      be detected in the recursive calls below.  */
3075   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3076       && (TREE_CODE (arg0) == SAVE_EXPR
3077           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3078     return 1;
3079
3080   /* Next handle constant cases, those for which we can return 1 even
3081      if ONLY_CONST is set.  */
3082   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3083     switch (TREE_CODE (arg0))
3084       {
3085       case INTEGER_CST:
3086         return tree_int_cst_equal (arg0, arg1);
3087
3088       case FIXED_CST:
3089         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3090                                        TREE_FIXED_CST (arg1));
3091
3092       case REAL_CST:
3093         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3094                                    TREE_REAL_CST (arg1)))
3095           return 1;
3096
3097         
3098         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3099           {
3100             /* If we do not distinguish between signed and unsigned zero,
3101                consider them equal.  */
3102             if (real_zerop (arg0) && real_zerop (arg1))
3103               return 1;
3104           }
3105         return 0;
3106
3107       case VECTOR_CST:
3108         {
3109           tree v1, v2;
3110
3111           v1 = TREE_VECTOR_CST_ELTS (arg0);
3112           v2 = TREE_VECTOR_CST_ELTS (arg1);
3113           while (v1 && v2)
3114             {
3115               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3116                                     flags))
3117                 return 0;
3118               v1 = TREE_CHAIN (v1);
3119               v2 = TREE_CHAIN (v2);
3120             }
3121
3122           return v1 == v2;
3123         }
3124
3125       case COMPLEX_CST:
3126         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3127                                  flags)
3128                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3129                                     flags));
3130
3131       case STRING_CST:
3132         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3133                 && ! memcmp (TREE_STRING_POINTER (arg0),
3134                               TREE_STRING_POINTER (arg1),
3135                               TREE_STRING_LENGTH (arg0)));
3136
3137       case ADDR_EXPR:
3138         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3139                                 0);
3140       default:
3141         break;
3142       }
3143
3144   if (flags & OEP_ONLY_CONST)
3145     return 0;
3146
3147 /* Define macros to test an operand from arg0 and arg1 for equality and a
3148    variant that allows null and views null as being different from any
3149    non-null value.  In the latter case, if either is null, the both
3150    must be; otherwise, do the normal comparison.  */
3151 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
3152                                     TREE_OPERAND (arg1, N), flags)
3153
3154 #define OP_SAME_WITH_NULL(N)                            \
3155   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3156    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3157
3158   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3159     {
3160     case tcc_unary:
3161       /* Two conversions are equal only if signedness and modes match.  */
3162       switch (TREE_CODE (arg0))
3163         {
3164         case NOP_EXPR:
3165         case CONVERT_EXPR:
3166         case FIX_TRUNC_EXPR:
3167           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3168               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3169             return 0;
3170           break;
3171         default:
3172           break;
3173         }
3174
3175       return OP_SAME (0);
3176
3177
3178     case tcc_comparison:
3179     case tcc_binary:
3180       if (OP_SAME (0) && OP_SAME (1))
3181         return 1;
3182
3183       /* For commutative ops, allow the other order.  */
3184       return (commutative_tree_code (TREE_CODE (arg0))
3185               && operand_equal_p (TREE_OPERAND (arg0, 0),
3186                                   TREE_OPERAND (arg1, 1), flags)
3187               && operand_equal_p (TREE_OPERAND (arg0, 1),
3188                                   TREE_OPERAND (arg1, 0), flags));
3189
3190     case tcc_reference:
3191       /* If either of the pointer (or reference) expressions we are
3192          dereferencing contain a side effect, these cannot be equal.  */
3193       if (TREE_SIDE_EFFECTS (arg0)
3194           || TREE_SIDE_EFFECTS (arg1))
3195         return 0;
3196
3197       switch (TREE_CODE (arg0))
3198         {
3199         case INDIRECT_REF:
3200         case ALIGN_INDIRECT_REF:
3201         case MISALIGNED_INDIRECT_REF:
3202         case REALPART_EXPR:
3203         case IMAGPART_EXPR:
3204           return OP_SAME (0);
3205
3206         case ARRAY_REF:
3207         case ARRAY_RANGE_REF:
3208           /* Operands 2 and 3 may be null.
3209              Compare the array index by value if it is constant first as we
3210              may have different types but same value here.  */
3211           return (OP_SAME (0)
3212                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3213                                           TREE_OPERAND (arg1, 1))
3214                       || OP_SAME (1))
3215                   && OP_SAME_WITH_NULL (2)
3216                   && OP_SAME_WITH_NULL (3));
3217
3218         case COMPONENT_REF:
3219           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
3220              may be NULL when we're called to compare MEM_EXPRs.  */
3221           return OP_SAME_WITH_NULL (0)
3222                  && OP_SAME (1)
3223                  && OP_SAME_WITH_NULL (2);
3224
3225         case BIT_FIELD_REF:
3226           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3227
3228         default:
3229           return 0;
3230         }
3231
3232     case tcc_expression:
3233       switch (TREE_CODE (arg0))
3234         {
3235         case ADDR_EXPR:
3236         case TRUTH_NOT_EXPR:
3237           return OP_SAME (0);
3238
3239         case TRUTH_ANDIF_EXPR:
3240         case TRUTH_ORIF_EXPR:
3241           return OP_SAME (0) && OP_SAME (1);
3242
3243         case TRUTH_AND_EXPR:
3244         case TRUTH_OR_EXPR:
3245         case TRUTH_XOR_EXPR:
3246           if (OP_SAME (0) && OP_SAME (1))
3247             return 1;
3248
3249           /* Otherwise take into account this is a commutative operation.  */
3250           return (operand_equal_p (TREE_OPERAND (arg0, 0),
3251                                    TREE_OPERAND (arg1, 1), flags)
3252                   && operand_equal_p (TREE_OPERAND (arg0, 1),
3253                                       TREE_OPERAND (arg1, 0), flags));
3254
3255         default:
3256           return 0;
3257         }
3258
3259     case tcc_vl_exp:
3260       switch (TREE_CODE (arg0))
3261         {
3262         case CALL_EXPR:
3263           /* If the CALL_EXPRs call different functions, then they
3264              clearly can not be equal.  */
3265           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3266                                  flags))
3267             return 0;
3268
3269           {
3270             unsigned int cef = call_expr_flags (arg0);
3271             if (flags & OEP_PURE_SAME)
3272               cef &= ECF_CONST | ECF_PURE;
3273             else
3274               cef &= ECF_CONST;
3275             if (!cef)
3276               return 0;
3277           }
3278
3279           /* Now see if all the arguments are the same.  */
3280           {
3281             const_call_expr_arg_iterator iter0, iter1;
3282             const_tree a0, a1;
3283             for (a0 = first_const_call_expr_arg (arg0, &iter0),
3284                    a1 = first_const_call_expr_arg (arg1, &iter1);
3285                  a0 && a1;
3286                  a0 = next_const_call_expr_arg (&iter0),
3287                    a1 = next_const_call_expr_arg (&iter1))
3288               if (! operand_equal_p (a0, a1, flags))
3289                 return 0;
3290
3291             /* If we get here and both argument lists are exhausted
3292                then the CALL_EXPRs are equal.  */
3293             return ! (a0 || a1);
3294           }
3295         default:
3296           return 0;
3297         }
3298
3299     case tcc_declaration:
3300       /* Consider __builtin_sqrt equal to sqrt.  */
3301       return (TREE_CODE (arg0) == FUNCTION_DECL
3302               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3303               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3304               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3305
3306     default:
3307       return 0;
3308     }
3309
3310 #undef OP_SAME
3311 #undef OP_SAME_WITH_NULL
3312 }
3313 \f
3314 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3315    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3316
3317    When in doubt, return 0.  */
3318
3319 static int
3320 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3321 {
3322   int unsignedp1, unsignedpo;
3323   tree primarg0, primarg1, primother;
3324   unsigned int correct_width;
3325
3326   if (operand_equal_p (arg0, arg1, 0))
3327     return 1;
3328
3329   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3330       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3331     return 0;
3332
3333   /* Discard any conversions that don't change the modes of ARG0 and ARG1
3334      and see if the inner values are the same.  This removes any
3335      signedness comparison, which doesn't matter here.  */
3336   primarg0 = arg0, primarg1 = arg1;
3337   STRIP_NOPS (primarg0);
3338   STRIP_NOPS (primarg1);
3339   if (operand_equal_p (primarg0, primarg1, 0))
3340     return 1;
3341
3342   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3343      actual comparison operand, ARG0.
3344
3345      First throw away any conversions to wider types
3346      already present in the operands.  */
3347
3348   primarg1 = get_narrower (arg1, &unsignedp1);
3349   primother = get_narrower (other, &unsignedpo);
3350
3351   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3352   if (unsignedp1 == unsignedpo
3353       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3354       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3355     {
3356       tree type = TREE_TYPE (arg0);
3357
3358       /* Make sure shorter operand is extended the right way
3359          to match the longer operand.  */
3360       primarg1 = fold_convert (signed_or_unsigned_type_for
3361                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3362
3363       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3364         return 1;
3365     }
3366
3367   return 0;
3368 }
3369 \f
3370 /* See if ARG is an expression that is either a comparison or is performing
3371    arithmetic on comparisons.  The comparisons must only be comparing
3372    two different values, which will be stored in *CVAL1 and *CVAL2; if
3373    they are nonzero it means that some operands have already been found.
3374    No variables may be used anywhere else in the expression except in the
3375    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
3376    the expression and save_expr needs to be called with CVAL1 and CVAL2.
3377
3378    If this is true, return 1.  Otherwise, return zero.  */
3379
3380 static int
3381 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3382 {
3383   enum tree_code code = TREE_CODE (arg);
3384   enum tree_code_class class = TREE_CODE_CLASS (code);
3385
3386   /* We can handle some of the tcc_expression cases here.  */
3387   if (class == tcc_expression && code == TRUTH_NOT_EXPR)
3388     class = tcc_unary;
3389   else if (class == tcc_expression
3390            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3391                || code == COMPOUND_EXPR))
3392     class = tcc_binary;
3393
3394   else if (class == tcc_expression && code == SAVE_EXPR
3395            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3396     {
3397       /* If we've already found a CVAL1 or CVAL2, this expression is
3398          two complex to handle.  */
3399       if (*cval1 || *cval2)
3400         return 0;
3401
3402       class = tcc_unary;
3403       *save_p = 1;
3404     }
3405
3406   switch (class)
3407     {
3408     case tcc_unary:
3409       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3410
3411     case tcc_binary:
3412       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3413               && twoval_comparison_p (TREE_OPERAND (arg, 1),
3414                                       cval1, cval2, save_p));
3415
3416     case tcc_constant:
3417       return 1;
3418
3419     case tcc_expression:
3420       if (code == COND_EXPR)
3421         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3422                                      cval1, cval2, save_p)
3423                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3424                                         cval1, cval2, save_p)
3425                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3426                                         cval1, cval2, save_p));
3427       return 0;
3428
3429     case tcc_comparison:
3430       /* First see if we can handle the first operand, then the second.  For
3431          the second operand, we know *CVAL1 can't be zero.  It must be that
3432          one side of the comparison is each of the values; test for the
3433          case where this isn't true by failing if the two operands
3434          are the same.  */
3435
3436       if (operand_equal_p (TREE_OPERAND (arg, 0),
3437                            TREE_OPERAND (arg, 1), 0))
3438         return 0;
3439
3440       if (*cval1 == 0)
3441         *cval1 = TREE_OPERAND (arg, 0);
3442       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3443         ;
3444       else if (*cval2 == 0)
3445         *cval2 = TREE_OPERAND (arg, 0);
3446       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3447         ;
3448       else
3449         return 0;
3450
3451       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3452         ;
3453       else if (*cval2 == 0)
3454         *cval2 = TREE_OPERAND (arg, 1);
3455       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3456         ;
3457       else
3458         return 0;
3459
3460       return 1;
3461
3462     default:
3463       return 0;
3464     }
3465 }
3466 \f
3467 /* ARG is a tree that is known to contain just arithmetic operations and
3468    comparisons.  Evaluate the operations in the tree substituting NEW0 for
3469    any occurrence of OLD0 as an operand of a comparison and likewise for
3470    NEW1 and OLD1.  */
3471
3472 static tree
3473 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3474 {
3475   tree type = TREE_TYPE (arg);
3476   enum tree_code code = TREE_CODE (arg);
3477   enum tree_code_class class = TREE_CODE_CLASS (code);
3478
3479   /* We can handle some of the tcc_expression cases here.  */
3480   if (class == tcc_expression && code == TRUTH_NOT_EXPR)
3481     class = tcc_unary;
3482   else if (class == tcc_expression
3483            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3484     class = tcc_binary;
3485
3486   switch (class)
3487     {
3488     case tcc_unary:
3489       return fold_build1 (code, type,
3490                           eval_subst (TREE_OPERAND (arg, 0),
3491                                       old0, new0, old1, new1));
3492
3493     case tcc_binary:
3494       return fold_build2 (code, type,
3495                           eval_subst (TREE_OPERAND (arg, 0),
3496                                       old0, new0, old1, new1),
3497                           eval_subst (TREE_OPERAND (arg, 1),
3498                                       old0, new0, old1, new1));
3499
3500     case tcc_expression:
3501       switch (code)
3502         {
3503         case SAVE_EXPR:
3504           return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3505
3506         case COMPOUND_EXPR:
3507           return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3508
3509         case COND_EXPR:
3510           return fold_build3 (code, type,
3511                               eval_subst (TREE_OPERAND (arg, 0),
3512                                           old0, new0, old1, new1),
3513                               eval_subst (TREE_OPERAND (arg, 1),
3514                                           old0, new0, old1, new1),
3515                               eval_subst (TREE_OPERAND (arg, 2),
3516                                           old0, new0, old1, new1));
3517         default:
3518           break;
3519         }
3520       /* Fall through - ???  */
3521
3522     case tcc_comparison:
3523       {
3524         tree arg0 = TREE_OPERAND (arg, 0);
3525         tree arg1 = TREE_OPERAND (arg, 1);
3526
3527         /* We need to check both for exact equality and tree equality.  The
3528            former will be true if the operand has a side-effect.  In that
3529            case, we know the operand occurred exactly once.  */
3530
3531         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3532           arg0 = new0;
3533         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3534           arg0 = new1;
3535
3536         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3537           arg1 = new0;
3538         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3539           arg1 = new1;
3540
3541         return fold_build2 (code, type, arg0, arg1);
3542       }
3543
3544     default:
3545       return arg;
3546     }
3547 }
3548 \f
3549 /* Return a tree for the case when the result of an expression is RESULT
3550    converted to TYPE and OMITTED was previously an operand of the expression
3551    but is now not needed (e.g., we folded OMITTED * 0).
3552
3553    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
3554    the conversion of RESULT to TYPE.  */
3555
3556 tree
3557 omit_one_operand (tree type, tree result, tree omitted)
3558 {
3559   tree t = fold_convert (type, result);
3560
3561   /* If the resulting operand is an empty statement, just return the omitted
3562      statement casted to void. */
3563   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3564     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3565
3566   if (TREE_SIDE_EFFECTS (omitted))
3567     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3568
3569   return non_lvalue (t);
3570 }
3571
3572 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3573
3574 static tree
3575 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3576 {
3577   tree t = fold_convert (type, result);
3578
3579   /* If the resulting operand is an empty statement, just return the omitted
3580      statement casted to void. */
3581   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3582     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3583
3584   if (TREE_SIDE_EFFECTS (omitted))
3585     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3586
3587   return pedantic_non_lvalue (t);
3588 }
3589
3590 /* Return a tree for the case when the result of an expression is RESULT
3591    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3592    of the expression but are now not needed.
3593
3594    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3595    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3596    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3597    just do the conversion of RESULT to TYPE.  */
3598
3599 tree
3600 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3601 {
3602   tree t = fold_convert (type, result);
3603
3604   if (TREE_SIDE_EFFECTS (omitted2))
3605     t = build2 (COMPOUND_EXPR, type, omitted2, t);
3606   if (TREE_SIDE_EFFECTS (omitted1))
3607     t = build2 (COMPOUND_EXPR, type, omitted1, t);
3608
3609   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3610 }
3611
3612 \f
3613 /* Return a simplified tree node for the truth-negation of ARG.  This
3614    never alters ARG itself.  We assume that ARG is an operation that
3615    returns a truth value (0 or 1).
3616
3617    FIXME: one would think we would fold the result, but it causes
3618    problems with the dominator optimizer.  */
3619
3620 tree
3621 fold_truth_not_expr (tree arg)
3622 {
3623   tree type = TREE_TYPE (arg);
3624   enum tree_code code = TREE_CODE (arg);
3625
3626   /* If this is a comparison, we can simply invert it, except for
3627      floating-point non-equality comparisons, in which case we just
3628      enclose a TRUTH_NOT_EXPR around what we have.  */
3629
3630   if (TREE_CODE_CLASS (code) == tcc_comparison)
3631     {
3632       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3633       if (FLOAT_TYPE_P (op_type)
3634           && flag_trapping_math
3635           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3636           && code != NE_EXPR && code != EQ_EXPR)
3637         return NULL_TREE;
3638       else
3639         {
3640           code = invert_tree_comparison (code,
3641                                          HONOR_NANS (TYPE_MODE (op_type)));
3642           if (code == ERROR_MARK)
3643             return NULL_TREE;
3644           else
3645             return build2 (code, type,
3646                            TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3647         }
3648     }
3649
3650   switch (code)
3651     {
3652     case INTEGER_CST:
3653       return constant_boolean_node (integer_zerop (arg), type);
3654
3655     case TRUTH_AND_EXPR:
3656       return build2 (TRUTH_OR_EXPR, type,
3657                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3658                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3659
3660     case TRUTH_OR_EXPR:
3661       return build2 (TRUTH_AND_EXPR, type,
3662                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3663                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3664
3665     case TRUTH_XOR_EXPR:
3666       /* Here we can invert either operand.  We invert the first operand
3667          unless the second operand is a TRUTH_NOT_EXPR in which case our
3668          result is the XOR of the first operand with the inside of the
3669          negation of the second operand.  */
3670
3671       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3672         return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3673                        TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3674       else
3675         return build2 (TRUTH_XOR_EXPR, type,
3676                        invert_truthvalue (TREE_OPERAND (arg, 0)),
3677                        TREE_OPERAND (arg, 1));
3678
3679     case TRUTH_ANDIF_EXPR:
3680       return build2 (TRUTH_ORIF_EXPR, type,
3681                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3682                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3683
3684     case TRUTH_ORIF_EXPR:
3685       return build2 (TRUTH_ANDIF_EXPR, type,
3686                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3687                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3688
3689     case TRUTH_NOT_EXPR:
3690       return TREE_OPERAND (arg, 0);
3691
3692     case COND_EXPR:
3693       {
3694         tree arg1 = TREE_OPERAND (arg, 1);
3695         tree arg2 = TREE_OPERAND (arg, 2);
3696         /* A COND_EXPR may have a throw as one operand, which
3697            then has void type.  Just leave void operands
3698            as they are.  */
3699         return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3700                        VOID_TYPE_P (TREE_TYPE (arg1))
3701                        ? arg1 : invert_truthvalue (arg1),
3702                        VOID_TYPE_P (TREE_TYPE (arg2))
3703                        ? arg2 : invert_truthvalue (arg2));
3704       }
3705
3706     case COMPOUND_EXPR:
3707       return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3708                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3709
3710     case NON_LVALUE_EXPR:
3711       return invert_truthvalue (TREE_OPERAND (arg, 0));
3712
3713     case NOP_EXPR:
3714       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3715         return build1 (TRUTH_NOT_EXPR, type, arg);
3716
3717     case CONVERT_EXPR:
3718     case FLOAT_EXPR:
3719       return build1 (TREE_CODE (arg), type,
3720                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3721
3722     case BIT_AND_EXPR:
3723       if (!integer_onep (TREE_OPERAND (arg, 1)))
3724         break;
3725       return build2 (EQ_EXPR, type, arg,
3726                      build_int_cst (type, 0));
3727
3728     case SAVE_EXPR:
3729       return build1 (TRUTH_NOT_EXPR, type, arg);
3730
3731     case CLEANUP_POINT_EXPR:
3732       return build1 (CLEANUP_POINT_EXPR, type,
3733                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3734
3735     default:
3736       break;
3737     }
3738
3739   return NULL_TREE;
3740 }
3741
3742 /* Return a simplified tree node for the truth-negation of ARG.  This
3743    never alters ARG itself.  We assume that ARG is an operation that
3744    returns a truth value (0 or 1).
3745
3746    FIXME: one would think we would fold the result, but it causes
3747    problems with the dominator optimizer.  */
3748
3749 tree
3750 invert_truthvalue (tree arg)
3751 {
3752   tree tem;
3753
3754   if (TREE_CODE (arg) == ERROR_MARK)
3755     return arg;
3756
3757   tem = fold_truth_not_expr (arg);
3758   if (!tem)
3759     tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3760
3761   return tem;
3762 }
3763
3764 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3765    operands are another bit-wise operation with a common input.  If so,
3766    distribute the bit operations to save an operation and possibly two if
3767    constants are involved.  For example, convert
3768         (A | B) & (A | C) into A | (B & C)
3769    Further simplification will occur if B and C are constants.
3770
3771    If this optimization cannot be done, 0 will be returned.  */
3772
3773 static tree
3774 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3775 {
3776   tree common;
3777   tree left, right;
3778
3779   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3780       || TREE_CODE (arg0) == code
3781       || (TREE_CODE (arg0) != BIT_AND_EXPR
3782           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3783     return 0;
3784
3785   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3786     {
3787       common = TREE_OPERAND (arg0, 0);
3788       left = TREE_OPERAND (arg0, 1);
3789       right = TREE_OPERAND (arg1, 1);
3790     }
3791   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3792     {
3793       common = TREE_OPERAND (arg0, 0);
3794       left = TREE_OPERAND (arg0, 1);
3795       right = TREE_OPERAND (arg1, 0);
3796     }
3797   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3798     {
3799       common = TREE_OPERAND (arg0, 1);
3800       left = TREE_OPERAND (arg0, 0);
3801       right = TREE_OPERAND (arg1, 1);
3802     }
3803   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3804     {
3805       common = TREE_OPERAND (arg0, 1);
3806       left = TREE_OPERAND (arg0, 0);
3807       right = TREE_OPERAND (arg1, 0);
3808     }
3809   else
3810     return 0;
3811
3812   return fold_build2 (TREE_CODE (arg0), type, common,
3813                       fold_build2 (code, type, left, right));
3814 }
3815
3816 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3817    with code CODE.  This optimization is unsafe.  */
3818 static tree
3819 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3820 {
3821   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3822   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3823
3824   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3825   if (mul0 == mul1
3826       && operand_equal_p (TREE_OPERAND (arg0, 1),
3827                        TREE_OPERAND (arg1, 1), 0))
3828     return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3829                         fold_build2 (code, type,
3830                                      TREE_OPERAND (arg0, 0),
3831                                      TREE_OPERAND (arg1, 0)),
3832                         TREE_OPERAND (arg0, 1));
3833
3834   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3835   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3836                        TREE_OPERAND (arg1, 0), 0)
3837       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3838       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3839     {
3840       REAL_VALUE_TYPE r0, r1;
3841       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3842       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3843       if (!mul0)
3844         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3845       if (!mul1)
3846         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3847       real_arithmetic (&r0, code, &r0, &r1);
3848       return fold_build2 (MULT_EXPR, type,
3849                           TREE_OPERAND (arg0, 0),
3850                           build_real (type, r0));
3851     }
3852
3853   return NULL_TREE;
3854 }
3855 \f
3856 /* Subroutine for fold_truthop: decode a field reference.
3857
3858    If EXP is a comparison reference, we return the innermost reference.
3859
3860    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3861    set to the starting bit number.
3862
3863    If the innermost field can be completely contained in a mode-sized
3864    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
3865
3866    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3867    otherwise it is not changed.
3868
3869    *PUNSIGNEDP is set to the signedness of the field.
3870
3871    *PMASK is set to the mask used.  This is either contained in a
3872    BIT_AND_EXPR or derived from the width of the field.
3873
3874    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3875
3876    Return 0 if this is not a component reference or is one that we can't
3877    do anything with.  */
3878
3879 static tree
3880 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3881                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3882                         int *punsignedp, int *pvolatilep,
3883                         tree *pmask, tree *pand_mask)
3884 {
3885   tree outer_type = 0;
3886   tree and_mask = 0;
3887   tree mask, inner, offset;
3888   tree unsigned_type;
3889   unsigned int precision;
3890
3891   /* All the optimizations using this function assume integer fields.
3892      There are problems with FP fields since the type_for_size call
3893      below can fail for, e.g., XFmode.  */
3894   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3895     return 0;
3896
3897   /* We are interested in the bare arrangement of bits, so strip everything
3898      that doesn't affect the machine mode.  However, record the type of the
3899      outermost expression if it may matter below.  */
3900   if (TREE_CODE (exp) == NOP_EXPR
3901       || TREE_CODE (exp) == CONVERT_EXPR
3902       || TREE_CODE (exp) == NON_LVALUE_EXPR)
3903     outer_type = TREE_TYPE (exp);
3904   STRIP_NOPS (exp);
3905
3906   if (TREE_CODE (exp) == BIT_AND_EXPR)
3907     {
3908       and_mask = TREE_OPERAND (exp, 1);
3909       exp = TREE_OPERAND (exp, 0);
3910       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3911       if (TREE_CODE (and_mask) != INTEGER_CST)
3912         return 0;
3913     }
3914
3915   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3916                                punsignedp, pvolatilep, false);
3917   if ((inner == exp && and_mask == 0)
3918       || *pbitsize < 0 || offset != 0
3919       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3920     return 0;
3921
3922   /* If the number of bits in the reference is the same as the bitsize of
3923      the outer type, then the outer type gives the signedness. Otherwise
3924      (in case of a small bitfield) the signedness is unchanged.  */
3925   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3926     *punsignedp = TYPE_UNSIGNED (outer_type);
3927
3928   /* Compute the mask to access the bitfield.  */
3929   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3930   precision = TYPE_PRECISION (unsigned_type);
3931
3932   mask = build_int_cst_type (unsigned_type, -1);
3933
3934   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3935   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3936
3937   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
3938   if (and_mask != 0)
3939     mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
3940                         fold_convert (unsigned_type, and_mask), mask);
3941
3942   *pmask = mask;
3943   *pand_mask = and_mask;
3944   return inner;
3945 }
3946
3947 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3948    represents the sign bit of EXP's type.  If EXP represents a sign
3949    or zero extension, also test VAL against the unextended type.
3950    The return value is the (sub)expression whose sign bit is VAL,
3951    or NULL_TREE otherwise.  */
3952
3953 static tree
3954 sign_bit_p (tree exp, const_tree val)
3955 {
3956   unsigned HOST_WIDE_INT mask_lo, lo;
3957   HOST_WIDE_INT mask_hi, hi;
3958   int width;
3959   tree t;
3960
3961   /* Tree EXP must have an integral type.  */
3962   t = TREE_TYPE (exp);
3963   if (! INTEGRAL_TYPE_P (t))
3964     return NULL_TREE;
3965
3966   /* Tree VAL must be an integer constant.  */
3967   if (TREE_CODE (val) != INTEGER_CST
3968       || TREE_OVERFLOW (val))
3969     return NULL_TREE;
3970
3971   width = TYPE_PRECISION (t);
3972   if (width > HOST_BITS_PER_WIDE_INT)
3973     {
3974       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3975       lo = 0;
3976
3977       mask_hi = ((unsigned HOST_WIDE_INT) -1
3978                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
3979       mask_lo = -1;
3980     }
3981   else
3982     {
3983       hi = 0;
3984       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3985
3986       mask_hi = 0;
3987       mask_lo = ((unsigned HOST_WIDE_INT) -1
3988                  >> (HOST_BITS_PER_WIDE_INT - width));
3989     }
3990
3991   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3992      treat VAL as if it were unsigned.  */
3993   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3994       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3995     return exp;
3996
3997   /* Handle extension from a narrower type.  */
3998   if (TREE_CODE (exp) == NOP_EXPR
3999       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4000     return sign_bit_p (TREE_OPERAND (exp, 0), val);
4001
4002   return NULL_TREE;
4003 }
4004
4005 /* Subroutine for fold_truthop: determine if an operand is simple enough
4006    to be evaluated unconditionally.  */
4007
4008 static int
4009 simple_operand_p (const_tree exp)
4010 {
4011   /* Strip any conversions that don't change the machine mode.  */
4012   STRIP_NOPS (exp);
4013
4014   return (CONSTANT_CLASS_P (exp)
4015           || TREE_CODE (exp) == SSA_NAME
4016           || (DECL_P (exp)
4017               && ! TREE_ADDRESSABLE (exp)
4018               && ! TREE_THIS_VOLATILE (exp)
4019               && ! DECL_NONLOCAL (exp)
4020               /* Don't regard global variables as simple.  They may be
4021                  allocated in ways unknown to the compiler (shared memory,
4022                  #pragma weak, etc).  */
4023               && ! TREE_PUBLIC (exp)
4024               && ! DECL_EXTERNAL (exp)
4025               /* Loading a static variable is unduly expensive, but global
4026                  registers aren't expensive.  */
4027               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4028 }
4029 \f
4030 /* The following functions are subroutines to fold_range_test and allow it to
4031    try to change a logical combination of comparisons into a range test.
4032
4033    For example, both
4034         X == 2 || X == 3 || X == 4 || X == 5
4035    and
4036         X >= 2 && X <= 5
4037    are converted to
4038         (unsigned) (X - 2) <= 3
4039
4040    We describe each set of comparisons as being either inside or outside
4041    a range, using a variable named like IN_P, and then describe the
4042    range with a lower and upper bound.  If one of the bounds is omitted,
4043    it represents either the highest or lowest value of the type.
4044
4045    In the comments below, we represent a range by two numbers in brackets
4046    preceded by a "+" to designate being inside that range, or a "-" to
4047    designate being outside that range, so the condition can be inverted by
4048    flipping the prefix.  An omitted bound is represented by a "-".  For
4049    example, "- [-, 10]" means being outside the range starting at the lowest
4050    possible value and ending at 10, in other words, being greater than 10.
4051    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4052    always false.
4053
4054    We set up things so that the missing bounds are handled in a consistent
4055    manner so neither a missing bound nor "true" and "false" need to be
4056    handled using a special case.  */
4057
4058 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4059    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4060    and UPPER1_P are nonzero if the respective argument is an upper bound
4061    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
4062    must be specified for a comparison.  ARG1 will be converted to ARG0's
4063    type if both are specified.  */
4064
4065 static tree
4066 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4067              tree arg1, int upper1_p)
4068 {
4069   tree tem;
4070   int result;
4071   int sgn0, sgn1;
4072
4073   /* If neither arg represents infinity, do the normal operation.
4074      Else, if not a comparison, return infinity.  Else handle the special
4075      comparison rules. Note that most of the cases below won't occur, but
4076      are handled for consistency.  */
4077
4078   if (arg0 != 0 && arg1 != 0)
4079     {
4080       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4081                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
4082       STRIP_NOPS (tem);
4083       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4084     }
4085
4086   if (TREE_CODE_CLASS (code) != tcc_comparison)
4087     return 0;
4088
4089   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4090      for neither.  In real maths, we cannot assume open ended ranges are
4091      the same. But, this is computer arithmetic, where numbers are finite.
4092      We can therefore make the transformation of any unbounded range with
4093      the value Z, Z being greater than any representable number. This permits
4094      us to treat unbounded ranges as equal.  */
4095   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4096   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4097   switch (code)
4098     {
4099     case EQ_EXPR:
4100       result = sgn0 == sgn1;
4101       break;
4102     case NE_EXPR:
4103       result = sgn0 != sgn1;
4104       break;
4105     case LT_EXPR:
4106       result = sgn0 < sgn1;
4107       break;
4108     case LE_EXPR:
4109       result = sgn0 <= sgn1;
4110       break;
4111     case GT_EXPR:
4112       result = sgn0 > sgn1;
4113       break;
4114     case GE_EXPR:
4115       result = sgn0 >= sgn1;
4116       break;
4117     default:
4118       gcc_unreachable ();
4119     }
4120
4121   return constant_boolean_node (result, type);
4122 }
4123 \f
4124 /* Given EXP, a logical expression, set the range it is testing into
4125    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4126    actually being tested.  *PLOW and *PHIGH will be made of the same
4127    type as the returned expression.  If EXP is not a comparison, we
4128    will most likely not be returning a useful value and range.  Set
4129    *STRICT_OVERFLOW_P to true if the return value is only valid
4130    because signed overflow is undefined; otherwise, do not change
4131    *STRICT_OVERFLOW_P.  */
4132
4133 static tree
4134 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4135             bool *strict_overflow_p)
4136 {
4137   enum tree_code code;
4138   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4139   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4140   int in_p, n_in_p;
4141   tree low, high, n_low, n_high;
4142
4143   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4144      and see if we can refine the range.  Some of the cases below may not
4145      happen, but it doesn't seem worth worrying about this.  We "continue"
4146      the outer loop when we've changed something; otherwise we "break"
4147      the switch, which will "break" the while.  */
4148
4149   in_p = 0;
4150   low = high = build_int_cst (TREE_TYPE (exp), 0);
4151
4152   while (1)
4153     {
4154       code = TREE_CODE (exp);
4155       exp_type = TREE_TYPE (exp);
4156
4157       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4158         {
4159           if (TREE_OPERAND_LENGTH (exp) > 0)
4160             arg0 = TREE_OPERAND (exp, 0);
4161           if (TREE_CODE_CLASS (code) == tcc_comparison
4162               || TREE_CODE_CLASS (code) == tcc_unary
4163               || TREE_CODE_CLASS (code) == tcc_binary)
4164             arg0_type = TREE_TYPE (arg0);
4165           if (TREE_CODE_CLASS (code) == tcc_binary
4166               || TREE_CODE_CLASS (code) == tcc_comparison
4167               || (TREE_CODE_CLASS (code) == tcc_expression
4168                   && TREE_OPERAND_LENGTH (exp) > 1))
4169             arg1 = TREE_OPERAND (exp, 1);
4170         }
4171
4172       switch (code)
4173         {
4174         case TRUTH_NOT_EXPR:
4175           in_p = ! in_p, exp = arg0;
4176           continue;
4177
4178         case EQ_EXPR: case NE_EXPR:
4179         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4180           /* We can only do something if the range is testing for zero
4181              and if the second operand is an integer constant.  Note that
4182              saying something is "in" the range we make is done by
4183              complementing IN_P since it will set in the initial case of
4184              being not equal to zero; "out" is leaving it alone.  */
4185           if (low == 0 || high == 0
4186               || ! integer_zerop (low) || ! integer_zerop (high)
4187               || TREE_CODE (arg1) != INTEGER_CST)
4188             break;
4189
4190           switch (code)
4191             {
4192             case NE_EXPR:  /* - [c, c]  */
4193               low = high = arg1;
4194               break;
4195             case EQ_EXPR:  /* + [c, c]  */
4196               in_p = ! in_p, low = high = arg1;
4197               break;
4198             case GT_EXPR:  /* - [-, c] */
4199               low = 0, high = arg1;
4200               break;
4201             case GE_EXPR:  /* + [c, -] */
4202               in_p = ! in_p, low = arg1, high = 0;
4203               break;
4204             case LT_EXPR:  /* - [c, -] */
4205               low = arg1, high = 0;
4206               break;
4207             case LE_EXPR:  /* + [-, c] */
4208               in_p = ! in_p, low = 0, high = arg1;
4209               break;
4210             default:
4211               gcc_unreachable ();
4212             }
4213
4214           /* If this is an unsigned comparison, we also know that EXP is
4215              greater than or equal to zero.  We base the range tests we make
4216              on that fact, so we record it here so we can parse existing
4217              range tests.  We test arg0_type since often the return type
4218              of, e.g. EQ_EXPR, is boolean.  */
4219           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4220             {
4221               if (! merge_ranges (&n_in_p, &n_low, &n_high,
4222                                   in_p, low, high, 1,
4223                                   build_int_cst (arg0_type, 0),
4224                                   NULL_TREE))
4225                 break;
4226
4227               in_p = n_in_p, low = n_low, high = n_high;
4228
4229               /* If the high bound is missing, but we have a nonzero low
4230                  bound, reverse the range so it goes from zero to the low bound
4231                  minus 1.  */
4232               if (high == 0 && low && ! integer_zerop (low))
4233                 {
4234                   in_p = ! in_p;
4235                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4236                                       integer_one_node, 0);
4237                   low = build_int_cst (arg0_type, 0);
4238                 }
4239             }
4240
4241           exp = arg0;
4242           continue;
4243
4244         case NEGATE_EXPR:
4245           /* (-x) IN [a,b] -> x in [-b, -a]  */
4246           n_low = range_binop (MINUS_EXPR, exp_type,
4247                                build_int_cst (exp_type, 0),
4248                                0, high, 1);
4249           n_high = range_binop (MINUS_EXPR, exp_type,
4250                                 build_int_cst (exp_type, 0),
4251                                 0, low, 0);
4252           low = n_low, high = n_high;
4253           exp = arg0;
4254           continue;
4255
4256         case BIT_NOT_EXPR:
4257           /* ~ X -> -X - 1  */
4258           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4259                         build_int_cst (exp_type, 1));
4260           continue;
4261
4262         case PLUS_EXPR:  case MINUS_EXPR:
4263           if (TREE_CODE (arg1) != INTEGER_CST)
4264             break;
4265
4266           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4267              move a constant to the other side.  */
4268           if (!TYPE_UNSIGNED (arg0_type)
4269               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4270             break;
4271
4272           /* If EXP is signed, any overflow in the computation is undefined,
4273              so we don't worry about it so long as our computations on
4274              the bounds don't overflow.  For unsigned, overflow is defined
4275              and this is exactly the right thing.  */
4276           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4277                                arg0_type, low, 0, arg1, 0);
4278           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4279                                 arg0_type, high, 1, arg1, 0);
4280           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4281               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4282             break;
4283
4284           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4285             *strict_overflow_p = true;
4286
4287           /* Check for an unsigned range which has wrapped around the maximum
4288              value thus making n_high < n_low, and normalize it.  */
4289           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4290             {
4291               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4292                                  integer_one_node, 0);
4293               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4294                                   integer_one_node, 0);
4295
4296               /* If the range is of the form +/- [ x+1, x ], we won't
4297                  be able to normalize it.  But then, it represents the
4298                  whole range or the empty set, so make it
4299                  +/- [ -, - ].  */
4300               if (tree_int_cst_equal (n_low, low)
4301                   && tree_int_cst_equal (n_high, high))
4302                 low = high = 0;
4303               else
4304                 in_p = ! in_p;
4305             }
4306           else
4307             low = n_low, high = n_high;
4308
4309           exp = arg0;
4310           continue;
4311
4312         case NOP_EXPR:  case NON_LVALUE_EXPR:  case CONVERT_EXPR:
4313           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4314             break;
4315
4316           if (! INTEGRAL_TYPE_P (arg0_type)
4317               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4318               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4319             break;
4320
4321           n_low = low, n_high = high;
4322
4323           if (n_low != 0)
4324             n_low = fold_convert (arg0_type, n_low);
4325
4326           if (n_high != 0)
4327             n_high = fold_convert (arg0_type, n_high);
4328
4329
4330           /* If we're converting arg0 from an unsigned type, to exp,
4331              a signed type,  we will be doing the comparison as unsigned.
4332              The tests above have already verified that LOW and HIGH
4333              are both positive.
4334
4335              So we have to ensure that we will handle large unsigned
4336              values the same way that the current signed bounds treat
4337              negative values.  */
4338
4339           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4340             {
4341               tree high_positive;
4342               tree equiv_type;
4343               /* For fixed-point modes, we need to pass the saturating flag
4344                  as the 2nd parameter.  */
4345               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4346                 equiv_type = lang_hooks.types.type_for_mode
4347                              (TYPE_MODE (arg0_type),
4348                               TYPE_SATURATING (arg0_type));
4349               else
4350                 equiv_type = lang_hooks.types.type_for_mode
4351                              (TYPE_MODE (arg0_type), 1);
4352
4353               /* A range without an upper bound is, naturally, unbounded.
4354                  Since convert would have cropped a very large value, use
4355                  the max value for the destination type.  */
4356               high_positive
4357                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4358                 : TYPE_MAX_VALUE (arg0_type);
4359
4360               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4361                 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4362                                              fold_convert (arg0_type,
4363                                                            high_positive),
4364                                              build_int_cst (arg0_type, 1));
4365
4366               /* If the low bound is specified, "and" the range with the
4367                  range for which the original unsigned value will be
4368                  positive.  */
4369               if (low != 0)
4370                 {
4371                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4372                                       1, n_low, n_high, 1,
4373                                       fold_convert (arg0_type,
4374                                                     integer_zero_node),
4375                                       high_positive))
4376                     break;
4377
4378                   in_p = (n_in_p == in_p);
4379                 }
4380               else
4381                 {
4382                   /* Otherwise, "or" the range with the range of the input
4383                      that will be interpreted as negative.  */
4384                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4385                                       0, n_low, n_high, 1,
4386                                       fold_convert (arg0_type,
4387                                                     integer_zero_node),
4388                                       high_positive))
4389                     break;
4390
4391                   in_p = (in_p != n_in_p);
4392                 }
4393             }
4394
4395           exp = arg0;
4396           low = n_low, high = n_high;
4397           continue;
4398
4399         default:
4400           break;
4401         }
4402
4403       break;
4404     }
4405
4406   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4407   if (TREE_CODE (exp) == INTEGER_CST)
4408     {
4409       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4410                                                  exp, 0, low, 0))
4411                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4412                                                     exp, 1, high, 1)));
4413       low = high = 0;
4414       exp = 0;
4415     }
4416
4417   *pin_p = in_p, *plow = low, *phigh = high;
4418   return exp;
4419 }
4420 \f
4421 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4422    type, TYPE, return an expression to test if EXP is in (or out of, depending
4423    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4424
4425 static tree
4426 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4427 {
4428   tree etype = TREE_TYPE (exp);
4429   tree value;
4430
4431 #ifdef HAVE_canonicalize_funcptr_for_compare
4432   /* Disable this optimization for function pointer expressions
4433      on targets that require function pointer canonicalization.  */
4434   if (HAVE_canonicalize_funcptr_for_compare
4435       && TREE_CODE (etype) == POINTER_TYPE
4436       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4437     return NULL_TREE;
4438 #endif
4439
4440   if (! in_p)
4441     {
4442       value = build_range_check (type, exp, 1, low, high);
4443       if (value != 0)
4444         return invert_truthvalue (value);
4445
4446       return 0;
4447     }
4448
4449   if (low == 0 && high == 0)
4450     return build_int_cst (type, 1);
4451
4452   if (low == 0)
4453     return fold_build2 (LE_EXPR, type, exp,
4454                         fold_convert (etype, high));
4455
4456   if (high == 0)
4457     return fold_build2 (GE_EXPR, type, exp,
4458                         fold_convert (etype, low));
4459
4460   if (operand_equal_p (low, high, 0))
4461     return fold_build2 (EQ_EXPR, type, exp,
4462                         fold_convert (etype, low));
4463
4464   if (integer_zerop (low))
4465     {
4466       if (! TYPE_UNSIGNED (etype))
4467         {
4468           etype = unsigned_type_for (etype);
4469           high = fold_convert (etype, high);
4470           exp = fold_convert (etype, exp);
4471         }
4472       return build_range_check (type, exp, 1, 0, high);
4473     }
4474
4475   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4476   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4477     {
4478       unsigned HOST_WIDE_INT lo;
4479       HOST_WIDE_INT hi;
4480       int prec;
4481
4482       prec = TYPE_PRECISION (etype);
4483       if (prec <= HOST_BITS_PER_WIDE_INT)
4484         {
4485           hi = 0;
4486           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4487         }
4488       else
4489         {
4490           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4491           lo = (unsigned HOST_WIDE_INT) -1;
4492         }
4493
4494       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4495         {
4496           if (TYPE_UNSIGNED (etype))
4497             {
4498               etype = signed_type_for (etype);
4499               exp = fold_convert (etype, exp);
4500             }
4501           return fold_build2 (GT_EXPR, type, exp,
4502                               build_int_cst (etype, 0));
4503         }
4504     }
4505
4506   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4507      This requires wrap-around arithmetics for the type of the expression.  */
4508   switch (TREE_CODE (etype))
4509     {
4510     case INTEGER_TYPE:
4511       /* There is no requirement that LOW be within the range of ETYPE
4512          if the latter is a subtype.  It must, however, be within the base
4513          type of ETYPE.  So be sure we do the subtraction in that type.  */
4514       if (TREE_TYPE (etype))
4515         etype = TREE_TYPE (etype);
4516       break;
4517
4518     case ENUMERAL_TYPE:
4519     case BOOLEAN_TYPE:
4520       etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4521                                               TYPE_UNSIGNED (etype));
4522       break;
4523
4524     default:
4525       break;
4526     }
4527
4528   /* If we don't have wrap-around arithmetics upfront, try to force it.  */
4529   if (TREE_CODE (etype) == INTEGER_TYPE
4530       && !TYPE_OVERFLOW_WRAPS (etype))
4531     {
4532       tree utype, minv, maxv;
4533
4534       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4535          for the type in question, as we rely on this here.  */
4536       utype = unsigned_type_for (etype);
4537       maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4538       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4539                           integer_one_node, 1);
4540       minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4541
4542       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4543                                       minv, 1, maxv, 1)))
4544         etype = utype;
4545       else
4546         return 0;
4547     }
4548
4549   high = fold_convert (etype, high);
4550   low = fold_convert (etype, low);
4551   exp = fold_convert (etype, exp);
4552
4553   value = const_binop (MINUS_EXPR, high, low, 0);
4554
4555
4556   if (POINTER_TYPE_P (etype))
4557     {
4558       if (value != 0 && !TREE_OVERFLOW (value))
4559         {
4560           low = fold_convert (sizetype, low);
4561           low = fold_build1 (NEGATE_EXPR, sizetype, low);
4562           return build_range_check (type,
4563                                     fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4564                                     1, build_int_cst (etype, 0), value);
4565         }
4566       return 0;
4567     }
4568
4569   if (value != 0 && !TREE_OVERFLOW (value))
4570     return build_range_check (type,
4571                               fold_build2 (MINUS_EXPR, etype, exp, low),
4572                               1, build_int_cst (etype, 0), value);
4573
4574   return 0;
4575 }
4576 \f
4577 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4578
4579 static tree
4580 range_predecessor (tree val)
4581 {
4582   tree type = TREE_TYPE (val);
4583
4584   if (INTEGRAL_TYPE_P (type)
4585       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4586     return 0;
4587   else
4588     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4589 }
4590
4591 /* Return the successor of VAL in its type, handling the infinite case.  */
4592
4593 static tree
4594 range_successor (tree val)
4595 {
4596   tree type = TREE_TYPE (val);
4597
4598   if (INTEGRAL_TYPE_P (type)
4599       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4600     return 0;
4601   else
4602     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4603 }
4604
4605 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4606    can, 0 if we can't.  Set the output range into the specified parameters.  */
4607
4608 static int
4609 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4610               tree high0, int in1_p, tree low1, tree high1)
4611 {
4612   int no_overlap;
4613   int subset;
4614   int temp;
4615   tree tem;
4616   int in_p;
4617   tree low, high;
4618   int lowequal = ((low0 == 0 && low1 == 0)
4619                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4620                                                 low0, 0, low1, 0)));
4621   int highequal = ((high0 == 0 && high1 == 0)
4622                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4623                                                  high0, 1, high1, 1)));
4624
4625   /* Make range 0 be the range that starts first, or ends last if they
4626      start at the same value.  Swap them if it isn't.  */
4627   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4628                                  low0, 0, low1, 0))
4629       || (lowequal
4630           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4631                                         high1, 1, high0, 1))))
4632     {
4633       temp = in0_p, in0_p = in1_p, in1_p = temp;
4634       tem = low0, low0 = low1, low1 = tem;
4635       tem = high0, high0 = high1, high1 = tem;
4636     }
4637
4638   /* Now flag two cases, whether the ranges are disjoint or whether the
4639      second range is totally subsumed in the first.  Note that the tests
4640      below are simplified by the ones above.  */
4641   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4642                                           high0, 1, low1, 0));
4643   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4644                                       high1, 1, high0, 1));
4645
4646   /* We now have four cases, depending on whether we are including or
4647      excluding the two ranges.  */
4648   if (in0_p && in1_p)
4649     {
4650       /* If they don't overlap, the result is false.  If the second range
4651          is a subset it is the result.  Otherwise, the range is from the start
4652          of the second to the end of the first.  */
4653       if (no_overlap)
4654         in_p = 0, low = high = 0;
4655       else if (subset)
4656         in_p = 1, low = low1, high = high1;
4657       else
4658         in_p = 1, low = low1, high = high0;
4659     }
4660
4661   else if (in0_p && ! in1_p)
4662     {
4663       /* If they don't overlap, the result is the first range.  If they are
4664          equal, the result is false.  If the second range is a subset of the
4665          first, and the ranges begin at the same place, we go from just after
4666          the end of the second range to the end of the first.  If the second
4667          range is not a subset of the first, or if it is a subset and both
4668          ranges end at the same place, the range starts at the start of the
4669          first range and ends just before the second range.
4670          Otherwise, we can't describe this as a single range.  */
4671       if (no_overlap)
4672         in_p = 1, low = low0, high = high0;
4673       else if (lowequal && highequal)
4674         in_p = 0, low = high = 0;
4675       else if (subset && lowequal)
4676         {
4677           low = range_successor (high1);
4678           high = high0;
4679           in_p = 1;
4680           if (low == 0)
4681             {
4682               /* We are in the weird situation where high0 > high1 but
4683                  high1 has no successor.  Punt.  */
4684               return 0;
4685             }
4686         }
4687       else if (! subset || highequal)
4688         {
4689           low = low0;
4690           high = range_predecessor (low1);
4691           in_p = 1;
4692           if (high == 0)
4693             {
4694               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4695               return 0;
4696             }
4697         }
4698       else
4699         return 0;
4700     }
4701
4702   else if (! in0_p && in1_p)
4703     {
4704       /* If they don't overlap, the result is the second range.  If the second
4705          is a subset of the first, the result is false.  Otherwise,
4706          the range starts just after the first range and ends at the
4707          end of the second.  */
4708       if (no_overlap)
4709         in_p = 1, low = low1, high = high1;
4710       else if (subset || highequal)
4711         in_p = 0, low = high = 0;
4712       else
4713         {
4714           low = range_successor (high0);
4715           high = high1;
4716           in_p = 1;
4717           if (low == 0)
4718             {
4719               /* high1 > high0 but high0 has no successor.  Punt.  */
4720               return 0;
4721             }
4722         }
4723     }
4724
4725   else
4726     {
4727       /* The case where we are excluding both ranges.  Here the complex case
4728          is if they don't overlap.  In that case, the only time we have a
4729          range is if they are adjacent.  If the second is a subset of the
4730          first, the result is the first.  Otherwise, the range to exclude
4731          starts at the beginning of the first range and ends at the end of the
4732          second.  */
4733       if (no_overlap)
4734         {
4735           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4736                                          range_successor (high0),
4737                                          1, low1, 0)))
4738             in_p = 0, low = low0, high = high1;
4739           else
4740             {
4741               /* Canonicalize - [min, x] into - [-, x].  */
4742               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4743                 switch (TREE_CODE (TREE_TYPE (low0)))
4744                   {
4745                   case ENUMERAL_TYPE:
4746                     if (TYPE_PRECISION (TREE_TYPE (low0))
4747                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4748                       break;
4749                     /* FALLTHROUGH */
4750                   case INTEGER_TYPE:
4751                     if (tree_int_cst_equal (low0,
4752                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4753                       low0 = 0;
4754                     break;
4755                   case POINTER_TYPE:
4756                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4757                         && integer_zerop (low0))
4758                       low0 = 0;
4759                     break;
4760                   default:
4761                     break;
4762                   }
4763
4764               /* Canonicalize - [x, max] into - [x, -].  */
4765               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4766                 switch (TREE_CODE (TREE_TYPE (high1)))
4767                   {
4768                   case ENUMERAL_TYPE:
4769                     if (TYPE_PRECISION (TREE_TYPE (high1))
4770                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4771                       break;
4772                     /* FALLTHROUGH */
4773                   case INTEGER_TYPE:
4774                     if (tree_int_cst_equal (high1,
4775                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4776                       high1 = 0;
4777                     break;
4778                   case POINTER_TYPE:
4779                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4780                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4781                                                        high1, 1,
4782                                                        integer_one_node, 1)))
4783                       high1 = 0;
4784                     break;
4785                   default:
4786                     break;
4787                   }
4788
4789               /* The ranges might be also adjacent between the maximum and
4790                  minimum values of the given type.  For
4791                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4792                  return + [x + 1, y - 1].  */
4793               if (low0 == 0 && high1 == 0)
4794                 {
4795                   low = range_successor (high0);
4796                   high = range_predecessor (low1);
4797                   if (low == 0 || high == 0)
4798                     return 0;
4799
4800                   in_p = 1;
4801                 }
4802               else
4803                 return 0;
4804             }
4805         }
4806       else if (subset)
4807         in_p = 0, low = low0, high = high0;
4808       else
4809         in_p = 0, low = low0, high = high1;
4810     }
4811
4812   *pin_p = in_p, *plow = low, *phigh = high;
4813   return 1;
4814 }
4815 \f
4816
4817 /* Subroutine of fold, looking inside expressions of the form
4818    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4819    of the COND_EXPR.  This function is being used also to optimize
4820    A op B ? C : A, by reversing the comparison first.
4821
4822    Return a folded expression whose code is not a COND_EXPR
4823    anymore, or NULL_TREE if no folding opportunity is found.  */
4824
4825 static tree
4826 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
4827 {
4828   enum tree_code comp_code = TREE_CODE (arg0);
4829   tree arg00 = TREE_OPERAND (arg0, 0);
4830   tree arg01 = TREE_OPERAND (arg0, 1);
4831   tree arg1_type = TREE_TYPE (arg1);
4832   tree tem;
4833
4834   STRIP_NOPS (arg1);
4835   STRIP_NOPS (arg2);
4836
4837   /* If we have A op 0 ? A : -A, consider applying the following
4838      transformations:
4839
4840      A == 0? A : -A    same as -A
4841      A != 0? A : -A    same as A
4842      A >= 0? A : -A    same as abs (A)
4843      A > 0?  A : -A    same as abs (A)
4844      A <= 0? A : -A    same as -abs (A)
4845      A < 0?  A : -A    same as -abs (A)
4846
4847      None of these transformations work for modes with signed
4848      zeros.  If A is +/-0, the first two transformations will
4849      change the sign of the result (from +0 to -0, or vice
4850      versa).  The last four will fix the sign of the result,
4851      even though the original expressions could be positive or
4852      negative, depending on the sign of A.
4853
4854      Note that all these transformations are correct if A is
4855      NaN, since the two alternatives (A and -A) are also NaNs.  */
4856   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4857       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4858           ? real_zerop (arg01)
4859           : integer_zerop (arg01))
4860       && ((TREE_CODE (arg2) == NEGATE_EXPR
4861            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4862              /* In the case that A is of the form X-Y, '-A' (arg2) may
4863                 have already been folded to Y-X, check for that. */
4864           || (TREE_CODE (arg1) == MINUS_EXPR
4865               && TREE_CODE (arg2) == MINUS_EXPR
4866               && operand_equal_p (TREE_OPERAND (arg1, 0),
4867                                   TREE_OPERAND (arg2, 1), 0)
4868               && operand_equal_p (TREE_OPERAND (arg1, 1),
4869                                   TREE_OPERAND (arg2, 0), 0))))
4870     switch (comp_code)
4871       {
4872       case EQ_EXPR:
4873       case UNEQ_EXPR:
4874         tem = fold_convert (arg1_type, arg1);
4875         return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
4876       case NE_EXPR:
4877       case LTGT_EXPR:
4878         return pedantic_non_lvalue (fold_convert (type, arg1));
4879       case UNGE_EXPR:
4880       case UNGT_EXPR:
4881         if (flag_trapping_math)
4882           break;
4883         /* Fall through.  */
4884       case GE_EXPR:
4885       case GT_EXPR:
4886         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4887           arg1 = fold_convert (signed_type_for
4888                                (TREE_TYPE (arg1)), arg1);
4889         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4890         return pedantic_non_lvalue (fold_convert (type, tem));
4891       case UNLE_EXPR:
4892       case UNLT_EXPR:
4893         if (flag_trapping_math)
4894           break;
4895       case LE_EXPR:
4896       case LT_EXPR:
4897         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4898           arg1 = fold_convert (signed_type_for
4899                                (TREE_TYPE (arg1)), arg1);
4900         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4901         return negate_expr (fold_convert (type, tem));
4902       default:
4903         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4904         break;
4905       }
4906
4907   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
4908      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
4909      both transformations are correct when A is NaN: A != 0
4910      is then true, and A == 0 is false.  */
4911
4912   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4913       && integer_zerop (arg01) && integer_zerop (arg2))
4914     {
4915       if (comp_code == NE_EXPR)
4916         return pedantic_non_lvalue (fold_convert (type, arg1));
4917       else if (comp_code == EQ_EXPR)
4918         return build_int_cst (type, 0);
4919     }
4920
4921   /* Try some transformations of A op B ? A : B.
4922
4923      A == B? A : B    same as B
4924      A != B? A : B    same as A
4925      A >= B? A : B    same as max (A, B)
4926      A > B?  A : B    same as max (B, A)
4927      A <= B? A : B    same as min (A, B)
4928      A < B?  A : B    same as min (B, A)
4929
4930      As above, these transformations don't work in the presence
4931      of signed zeros.  For example, if A and B are zeros of
4932      opposite sign, the first two transformations will change
4933      the sign of the result.  In the last four, the original
4934      expressions give different results for (A=+0, B=-0) and
4935      (A=-0, B=+0), but the transformed expressions do not.
4936
4937      The first two transformations are correct if either A or B
4938      is a NaN.  In the first transformation, the condition will
4939      be false, and B will indeed be chosen.  In the case of the
4940      second transformation, the condition A != B will be true,
4941      and A will be chosen.
4942
4943      The conversions to max() and min() are not correct if B is
4944      a number and A is not.  The conditions in the original
4945      expressions will be false, so all four give B.  The min()
4946      and max() versions would give a NaN instead.  */
4947   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4948       && operand_equal_for_comparison_p (arg01, arg2, arg00)
4949       /* Avoid these transformations if the COND_EXPR may be used
4950          as an lvalue in the C++ front-end.  PR c++/19199.  */
4951       && (in_gimple_form
4952           || (strcmp (lang_hooks.name, "GNU C++") != 0
4953               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4954           || ! maybe_lvalue_p (arg1)
4955           || ! maybe_lvalue_p (arg2)))
4956     {
4957       tree comp_op0 = arg00;
4958       tree comp_op1 = arg01;
4959       tree comp_type = TREE_TYPE (comp_op0);
4960
4961       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
4962       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4963         {
4964           comp_type = type;
4965           comp_op0 = arg1;
4966           comp_op1 = arg2;
4967         }
4968
4969       switch (comp_code)
4970         {
4971         case EQ_EXPR:
4972           return pedantic_non_lvalue (fold_convert (type, arg2));
4973         case NE_EXPR:
4974           return pedantic_non_lvalue (fold_convert (type, arg1));
4975         case LE_EXPR:
4976         case LT_EXPR:
4977         case UNLE_EXPR:
4978         case UNLT_EXPR:
4979           /* In C++ a ?: expression can be an lvalue, so put the
4980              operand which will be used if they are equal first
4981              so that we can convert this back to the
4982              corresponding COND_EXPR.  */
4983           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4984             {
4985               comp_op0 = fold_convert (comp_type, comp_op0);
4986               comp_op1 = fold_convert (comp_type, comp_op1);
4987               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4988                     ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
4989                     : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
4990               return pedantic_non_lvalue (fold_convert (type, tem));
4991             }
4992           break;
4993         case GE_EXPR:
4994         case GT_EXPR:
4995         case UNGE_EXPR:
4996         case UNGT_EXPR:
4997           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4998             {
4999               comp_op0 = fold_convert (comp_type, comp_op0);
5000               comp_op1 = fold_convert (comp_type, comp_op1);
5001               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5002                     ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5003                     : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5004               return pedantic_non_lvalue (fold_convert (type, tem));
5005             }
5006           break;
5007         case UNEQ_EXPR:
5008           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5009             return pedantic_non_lvalue (fold_convert (type, arg2));
5010           break;
5011         case LTGT_EXPR:
5012           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5013             return pedantic_non_lvalue (fold_convert (type, arg1));
5014           break;
5015         default:
5016           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5017           break;
5018         }
5019     }
5020
5021   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5022      we might still be able to simplify this.  For example,
5023      if C1 is one less or one more than C2, this might have started
5024      out as a MIN or MAX and been transformed by this function.
5025      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
5026
5027   if (INTEGRAL_TYPE_P (type)
5028       && TREE_CODE (arg01) == INTEGER_CST
5029       && TREE_CODE (arg2) == INTEGER_CST)
5030     switch (comp_code)
5031       {
5032       case EQ_EXPR:
5033         /* We can replace A with C1 in this case.  */
5034         arg1 = fold_convert (type, arg01);
5035         return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5036
5037       case LT_EXPR:
5038         /* If C1 is C2 + 1, this is min(A, C2).  */
5039         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5040                                OEP_ONLY_CONST)
5041             && operand_equal_p (arg01,
5042                                 const_binop (PLUS_EXPR, arg2,
5043                                              build_int_cst (type, 1), 0),
5044                                 OEP_ONLY_CONST))
5045           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5046                                                    type,
5047                                                    fold_convert (type, arg1),
5048                                                    arg2));
5049         break;
5050
5051       case LE_EXPR:
5052         /* If C1 is C2 - 1, this is min(A, C2).  */
5053         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5054                                OEP_ONLY_CONST)
5055             && operand_equal_p (arg01,
5056                                 const_binop (MINUS_EXPR, arg2,
5057                                              build_int_cst (type, 1), 0),
5058                                 OEP_ONLY_CONST))
5059           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5060                                                    type,
5061                                                    fold_convert (type, arg1),
5062                                                    arg2));
5063         break;
5064
5065       case GT_EXPR:
5066         /* If C1 is C2 - 1, this is max(A, C2).  */
5067         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5068                                OEP_ONLY_CONST)
5069             && operand_equal_p (arg01,
5070                                 const_binop (MINUS_EXPR, arg2,
5071                                              build_int_cst (type, 1), 0),
5072                                 OEP_ONLY_CONST))
5073           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5074                                                    type,
5075                                                    fold_convert (type, arg1),
5076                                                    arg2));
5077         break;
5078
5079       case GE_EXPR:
5080         /* If C1 is C2 + 1, this is max(A, C2).  */
5081         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5082                                OEP_ONLY_CONST)
5083             && operand_equal_p (arg01,
5084                                 const_binop (PLUS_EXPR, arg2,
5085                                              build_int_cst (type, 1), 0),
5086                                 OEP_ONLY_CONST))
5087           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5088                                                    type,
5089                                                    fold_convert (type, arg1),
5090                                                    arg2));
5091         break;
5092       case NE_EXPR:
5093         break;
5094       default:
5095         gcc_unreachable ();
5096       }
5097
5098   return NULL_TREE;
5099 }
5100
5101
5102 \f
5103 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5104 #define LOGICAL_OP_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
5105 #endif
5106
5107 /* EXP is some logical combination of boolean tests.  See if we can
5108    merge it into some range test.  Return the new tree if so.  */
5109
5110 static tree
5111 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5112 {
5113   int or_op = (code == TRUTH_ORIF_EXPR
5114                || code == TRUTH_OR_EXPR);
5115   int in0_p, in1_p, in_p;
5116   tree low0, low1, low, high0, high1, high;
5117   bool strict_overflow_p = false;
5118   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5119   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5120   tree tem;
5121   const char * const warnmsg = G_("assuming signed overflow does not occur "
5122                                   "when simplifying range test");
5123
5124   /* If this is an OR operation, invert both sides; we will invert
5125      again at the end.  */
5126   if (or_op)
5127     in0_p = ! in0_p, in1_p = ! in1_p;
5128
5129   /* If both expressions are the same, if we can merge the ranges, and we
5130      can build the range test, return it or it inverted.  If one of the
5131      ranges is always true or always false, consider it to be the same
5132      expression as the other.  */
5133   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5134       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5135                        in1_p, low1, high1)
5136       && 0 != (tem = (build_range_check (type,
5137                                          lhs != 0 ? lhs
5138                                          : rhs != 0 ? rhs : integer_zero_node,
5139                                          in_p, low, high))))
5140     {
5141       if (strict_overflow_p)
5142         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5143       return or_op ? invert_truthvalue (tem) : tem;
5144     }
5145
5146   /* On machines where the branch cost is expensive, if this is a
5147      short-circuited branch and the underlying object on both sides
5148      is the same, make a non-short-circuit operation.  */
5149   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5150            && lhs != 0 && rhs != 0
5151            && (code == TRUTH_ANDIF_EXPR
5152                || code == TRUTH_ORIF_EXPR)
5153            && operand_equal_p (lhs, rhs, 0))
5154     {
5155       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
5156          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5157          which cases we can't do this.  */
5158       if (simple_operand_p (lhs))
5159         return build2 (code == TRUTH_ANDIF_EXPR
5160                        ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5161                        type, op0, op1);
5162
5163       else if (lang_hooks.decls.global_bindings_p () == 0
5164                && ! CONTAINS_PLACEHOLDER_P (lhs))
5165         {
5166           tree common = save_expr (lhs);
5167
5168           if (0 != (lhs = build_range_check (type, common,
5169                                              or_op ? ! in0_p : in0_p,
5170                                              low0, high0))
5171               && (0 != (rhs = build_range_check (type, common,
5172                                                  or_op ? ! in1_p : in1_p,
5173                                                  low1, high1))))
5174             {
5175               if (strict_overflow_p)
5176                 fold_overflow_warning (warnmsg,
5177                                        WARN_STRICT_OVERFLOW_COMPARISON);
5178               return build2 (code == TRUTH_ANDIF_EXPR
5179                              ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5180                              type, lhs, rhs);
5181             }
5182         }
5183     }
5184
5185   return 0;
5186 }
5187 \f
5188 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5189    bit value.  Arrange things so the extra bits will be set to zero if and
5190    only if C is signed-extended to its full width.  If MASK is nonzero,
5191    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
5192
5193 static tree
5194 unextend (tree c, int p, int unsignedp, tree mask)
5195 {
5196   tree type = TREE_TYPE (c);
5197   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5198   tree temp;
5199
5200   if (p == modesize || unsignedp)
5201     return c;
5202
5203   /* We work by getting just the sign bit into the low-order bit, then
5204      into the high-order bit, then sign-extend.  We then XOR that value
5205      with C.  */
5206   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5207   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5208
5209   /* We must use a signed type in order to get an arithmetic right shift.
5210      However, we must also avoid introducing accidental overflows, so that
5211      a subsequent call to integer_zerop will work.  Hence we must
5212      do the type conversion here.  At this point, the constant is either
5213      zero or one, and the conversion to a signed type can never overflow.
5214      We could get an overflow if this conversion is done anywhere else.  */
5215   if (TYPE_UNSIGNED (type))
5216     temp = fold_convert (signed_type_for (type), temp);
5217
5218   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5219   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5220   if (mask != 0)
5221     temp = const_binop (BIT_AND_EXPR, temp,
5222                         fold_convert (TREE_TYPE (c), mask), 0);
5223   /* If necessary, convert the type back to match the type of C.  */
5224   if (TYPE_UNSIGNED (type))
5225     temp = fold_convert (type, temp);
5226
5227   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5228 }
5229 \f
5230 /* Find ways of folding logical expressions of LHS and RHS:
5231    Try to merge two comparisons to the same innermost item.
5232    Look for range tests like "ch >= '0' && ch <= '9'".
5233    Look for combinations of simple terms on machines with expensive branches
5234    and evaluate the RHS unconditionally.
5235
5236    For example, if we have p->a == 2 && p->b == 4 and we can make an
5237    object large enough to span both A and B, we can do this with a comparison
5238    against the object ANDed with the a mask.
5239
5240    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5241    operations to do this with one comparison.
5242
5243    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5244    function and the one above.
5245
5246    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5247    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5248
5249    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5250    two operands.
5251
5252    We return the simplified tree or 0 if no optimization is possible.  */
5253
5254 static tree
5255 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5256 {
5257   /* If this is the "or" of two comparisons, we can do something if
5258      the comparisons are NE_EXPR.  If this is the "and", we can do something
5259      if the comparisons are EQ_EXPR.  I.e.,
5260         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5261
5262      WANTED_CODE is this operation code.  For single bit fields, we can
5263      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5264      comparison for one-bit fields.  */
5265
5266   enum tree_code wanted_code;
5267   enum tree_code lcode, rcode;
5268   tree ll_arg, lr_arg, rl_arg, rr_arg;
5269   tree ll_inner, lr_inner, rl_inner, rr_inner;
5270   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5271   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5272   HOST_WIDE_INT xll_bitpos, xrl_bitpos;
5273   HOST_WIDE_INT lnbitsize, lnbitpos;
5274   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5275   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5276   enum machine_mode lnmode;
5277   tree ll_mask, lr_mask, rl_mask, rr_mask;
5278   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5279   tree l_const, r_const;
5280   tree lntype, result;
5281   int first_bit, end_bit;
5282   int volatilep;
5283   tree orig_lhs = lhs, orig_rhs = rhs;
5284   enum tree_code orig_code = code;
5285
5286   /* Start by getting the comparison codes.  Fail if anything is volatile.
5287      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5288      it were surrounded with a NE_EXPR.  */
5289
5290   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5291     return 0;
5292
5293   lcode = TREE_CODE (lhs);
5294   rcode = TREE_CODE (rhs);
5295
5296   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5297     {
5298       lhs = build2 (NE_EXPR, truth_type, lhs,
5299                     build_int_cst (TREE_TYPE (lhs), 0));
5300       lcode = NE_EXPR;
5301     }
5302
5303   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5304     {
5305       rhs = build2 (NE_EXPR, truth_type, rhs,
5306                     build_int_cst (TREE_TYPE (rhs), 0));
5307       rcode = NE_EXPR;
5308     }
5309
5310   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5311       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5312     return 0;
5313
5314   ll_arg = TREE_OPERAND (lhs, 0);
5315   lr_arg = TREE_OPERAND (lhs, 1);
5316   rl_arg = TREE_OPERAND (rhs, 0);
5317   rr_arg = TREE_OPERAND (rhs, 1);
5318
5319   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5320   if (simple_operand_p (ll_arg)
5321       && simple_operand_p (lr_arg))
5322     {
5323       tree result;
5324       if (operand_equal_p (ll_arg, rl_arg, 0)
5325           && operand_equal_p (lr_arg, rr_arg, 0))
5326         {
5327           result = combine_comparisons (code, lcode, rcode,
5328                                         truth_type, ll_arg, lr_arg);
5329           if (result)
5330             return result;
5331         }
5332       else if (operand_equal_p (ll_arg, rr_arg, 0)
5333                && operand_equal_p (lr_arg, rl_arg, 0))
5334         {
5335           result = combine_comparisons (code, lcode,
5336                                         swap_tree_comparison (rcode),
5337                                         truth_type, ll_arg, lr_arg);
5338           if (result)
5339             return result;
5340         }
5341     }
5342
5343   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5344           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5345
5346   /* If the RHS can be evaluated unconditionally and its operands are
5347      simple, it wins to evaluate the RHS unconditionally on machines
5348      with expensive branches.  In this case, this isn't a comparison
5349      that can be merged.  Avoid doing this if the RHS is a floating-point
5350      comparison since those can trap.  */
5351
5352   if (BRANCH_COST >= 2
5353       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5354       && simple_operand_p (rl_arg)
5355       && simple_operand_p (rr_arg))
5356     {
5357       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5358       if (code == TRUTH_OR_EXPR
5359           && lcode == NE_EXPR && integer_zerop (lr_arg)
5360           && rcode == NE_EXPR && integer_zerop (rr_arg)
5361           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5362           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5363         return build2 (NE_EXPR, truth_type,
5364                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5365                                ll_arg, rl_arg),
5366                        build_int_cst (TREE_TYPE (ll_arg), 0));
5367
5368       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5369       if (code == TRUTH_AND_EXPR
5370           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5371           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5372           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5373           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5374         return build2 (EQ_EXPR, truth_type,
5375                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5376                                ll_arg, rl_arg),
5377                        build_int_cst (TREE_TYPE (ll_arg), 0));
5378
5379       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5380         {
5381           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5382             return build2 (code, truth_type, lhs, rhs);
5383           return NULL_TREE;
5384         }
5385     }
5386
5387   /* See if the comparisons can be merged.  Then get all the parameters for
5388      each side.  */
5389
5390   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5391       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5392     return 0;
5393
5394   volatilep = 0;
5395   ll_inner = decode_field_reference (ll_arg,
5396                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5397                                      &ll_unsignedp, &volatilep, &ll_mask,
5398                                      &ll_and_mask);
5399   lr_inner = decode_field_reference (lr_arg,
5400                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5401                                      &lr_unsignedp, &volatilep, &lr_mask,
5402                                      &lr_and_mask);
5403   rl_inner = decode_field_reference (rl_arg,
5404                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5405                                      &rl_unsignedp, &volatilep, &rl_mask,
5406                                      &rl_and_mask);
5407   rr_inner = decode_field_reference (rr_arg,
5408                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5409                                      &rr_unsignedp, &volatilep, &rr_mask,
5410                                      &rr_and_mask);
5411
5412   /* It must be true that the inner operation on the lhs of each
5413      comparison must be the same if we are to be able to do anything.
5414      Then see if we have constants.  If not, the same must be true for
5415      the rhs's.  */
5416   if (volatilep || ll_inner == 0 || rl_inner == 0
5417       || ! operand_equal_p (ll_inner, rl_inner, 0))
5418     return 0;
5419
5420   if (TREE_CODE (lr_arg) == INTEGER_CST
5421       && TREE_CODE (rr_arg) == INTEGER_CST)
5422     l_const = lr_arg, r_const = rr_arg;
5423   else if (lr_inner == 0 || rr_inner == 0
5424            || ! operand_equal_p (lr_inner, rr_inner, 0))
5425     return 0;
5426   else
5427     l_const = r_const = 0;
5428
5429   /* If either comparison code is not correct for our logical operation,
5430      fail.  However, we can convert a one-bit comparison against zero into
5431      the opposite comparison against that bit being set in the field.  */
5432
5433   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5434   if (lcode != wanted_code)
5435     {
5436       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5437         {
5438           /* Make the left operand unsigned, since we are only interested
5439              in the value of one bit.  Otherwise we are doing the wrong
5440              thing below.  */
5441           ll_unsignedp = 1;
5442           l_const = ll_mask;
5443         }
5444       else
5445         return 0;
5446     }
5447
5448   /* This is analogous to the code for l_const above.  */
5449   if (rcode != wanted_code)
5450     {
5451       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5452         {
5453           rl_unsignedp = 1;
5454           r_const = rl_mask;
5455         }
5456       else
5457         return 0;
5458     }
5459
5460   /* See if we can find a mode that contains both fields being compared on
5461      the left.  If we can't, fail.  Otherwise, update all constants and masks
5462      to be relative to a field of that size.  */
5463   first_bit = MIN (ll_bitpos, rl_bitpos);
5464   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5465   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5466                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5467                           volatilep);
5468   if (lnmode == VOIDmode)
5469     return 0;
5470
5471   lnbitsize = GET_MODE_BITSIZE (lnmode);
5472   lnbitpos = first_bit & ~ (lnbitsize - 1);
5473   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5474   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5475
5476   if (BYTES_BIG_ENDIAN)
5477     {
5478       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5479       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5480     }
5481
5482   ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5483                          size_int (xll_bitpos), 0);
5484   rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5485                          size_int (xrl_bitpos), 0);
5486
5487   if (l_const)
5488     {
5489       l_const = fold_convert (lntype, l_const);
5490       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5491       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5492       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5493                                         fold_build1 (BIT_NOT_EXPR,
5494                                                      lntype, ll_mask),
5495                                         0)))
5496         {
5497           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5498
5499           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5500         }
5501     }
5502   if (r_const)
5503     {
5504       r_const = fold_convert (lntype, r_const);
5505       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5506       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5507       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5508                                         fold_build1 (BIT_NOT_EXPR,
5509                                                      lntype, rl_mask),
5510                                         0)))
5511         {
5512           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5513
5514           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5515         }
5516     }
5517
5518   /* Handle the case of comparisons with constants.  If there is something in
5519      common between the masks, those bits of the constants must be the same.
5520      If not, the condition is always false.  Test for this to avoid generating
5521      incorrect code below.  */
5522   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5523   if (! integer_zerop (result)
5524       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5525                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5526     {
5527       if (wanted_code == NE_EXPR)
5528         {
5529           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5530           return constant_boolean_node (true, truth_type);
5531         }
5532       else
5533         {
5534           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5535           return constant_boolean_node (false, truth_type);
5536         }
5537     }
5538
5539   return NULL_TREE;
5540 }
5541 \f
5542 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5543    constant.  */
5544
5545 static tree
5546 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5547 {
5548   tree arg0 = op0;
5549   enum tree_code op_code;
5550   tree comp_const;
5551   tree minmax_const;
5552   int consts_equal, consts_lt;
5553   tree inner;
5554
5555   STRIP_SIGN_NOPS (arg0);
5556
5557   op_code = TREE_CODE (arg0);
5558   minmax_const = TREE_OPERAND (arg0, 1);
5559   comp_const = fold_convert (TREE_TYPE (arg0), op1);
5560   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5561   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5562   inner = TREE_OPERAND (arg0, 0);
5563
5564   /* If something does not permit us to optimize, return the original tree.  */
5565   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5566       || TREE_CODE (comp_const) != INTEGER_CST
5567       || TREE_OVERFLOW (comp_const)
5568       || TREE_CODE (minmax_const) != INTEGER_CST
5569       || TREE_OVERFLOW (minmax_const))
5570     return NULL_TREE;
5571
5572   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5573      and GT_EXPR, doing the rest with recursive calls using logical
5574      simplifications.  */
5575   switch (code)
5576     {
5577     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5578       {
5579         tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5580                                           type, op0, op1);
5581         if (tem)
5582           return invert_truthvalue (tem);
5583         return NULL_TREE;
5584       }
5585
5586     case GE_EXPR:
5587       return
5588         fold_build2 (TRUTH_ORIF_EXPR, type,
5589                      optimize_minmax_comparison
5590                      (EQ_EXPR, type, arg0, comp_const),
5591                      optimize_minmax_comparison
5592                      (GT_EXPR, type, arg0, comp_const));
5593
5594     case EQ_EXPR:
5595       if (op_code == MAX_EXPR && consts_equal)
5596         /* MAX (X, 0) == 0  ->  X <= 0  */
5597         return fold_build2 (LE_EXPR, type, inner, comp_const);
5598
5599       else if (op_code == MAX_EXPR && consts_lt)
5600         /* MAX (X, 0) == 5  ->  X == 5   */
5601         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5602
5603       else if (op_code == MAX_EXPR)
5604         /* MAX (X, 0) == -1  ->  false  */
5605         return omit_one_operand (type, integer_zero_node, inner);
5606
5607       else if (consts_equal)
5608         /* MIN (X, 0) == 0  ->  X >= 0  */
5609         return fold_build2 (GE_EXPR, type, inner, comp_const);
5610
5611       else if (consts_lt)
5612         /* MIN (X, 0) == 5  ->  false  */
5613         return omit_one_operand (type, integer_zero_node, inner);
5614
5615       else
5616         /* MIN (X, 0) == -1  ->  X == -1  */
5617         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5618
5619     case GT_EXPR:
5620       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5621         /* MAX (X, 0) > 0  ->  X > 0
5622            MAX (X, 0) > 5  ->  X > 5  */
5623         return fold_build2 (GT_EXPR, type, inner, comp_const);
5624
5625       else if (op_code == MAX_EXPR)
5626         /* MAX (X, 0) > -1  ->  true  */
5627         return omit_one_operand (type, integer_one_node, inner);
5628
5629       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5630         /* MIN (X, 0) > 0  ->  false
5631            MIN (X, 0) > 5  ->  false  */
5632         return omit_one_operand (type, integer_zero_node, inner);
5633
5634       else
5635         /* MIN (X, 0) > -1  ->  X > -1  */
5636         return fold_build2 (GT_EXPR, type, inner, comp_const);
5637
5638     default:
5639       return NULL_TREE;
5640     }
5641 }
5642 \f
5643 /* T is an integer expression that is being multiplied, divided, or taken a
5644    modulus (CODE says which and what kind of divide or modulus) by a
5645    constant C.  See if we can eliminate that operation by folding it with
5646    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5647    should be used for the computation if wider than our type.
5648
5649    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5650    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5651    expression would not overflow or that overflow is undefined for the type
5652    in the language in question.
5653
5654    We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
5655    the machine has a multiply-accumulate insn or that this is part of an
5656    addressing calculation.
5657
5658    If we return a non-null expression, it is an equivalent form of the
5659    original computation, but need not be in the original type.
5660
5661    We set *STRICT_OVERFLOW_P to true if the return values depends on
5662    signed overflow being undefined.  Otherwise we do not change
5663    *STRICT_OVERFLOW_P.  */
5664
5665 static tree
5666 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5667                 bool *strict_overflow_p)
5668 {
5669   /* To avoid exponential search depth, refuse to allow recursion past
5670      three levels.  Beyond that (1) it's highly unlikely that we'll find
5671      something interesting and (2) we've probably processed it before
5672      when we built the inner expression.  */
5673
5674   static int depth;
5675   tree ret;
5676
5677   if (depth > 3)
5678     return NULL;
5679
5680   depth++;
5681   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5682   depth--;
5683
5684   return ret;
5685 }
5686
5687 static tree
5688 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5689                   bool *strict_overflow_p)
5690 {
5691   tree type = TREE_TYPE (t);
5692   enum tree_code tcode = TREE_CODE (t);
5693   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5694                                    > GET_MODE_SIZE (TYPE_MODE (type)))
5695                 ? wide_type : type);
5696   tree t1, t2;
5697   int same_p = tcode == code;
5698   tree op0 = NULL_TREE, op1 = NULL_TREE;
5699   bool sub_strict_overflow_p;
5700
5701   /* Don't deal with constants of zero here; they confuse the code below.  */
5702   if (integer_zerop (c))
5703     return NULL_TREE;
5704
5705   if (TREE_CODE_CLASS (tcode) == tcc_unary)
5706     op0 = TREE_OPERAND (t, 0);
5707
5708   if (TREE_CODE_CLASS (tcode) == tcc_binary)
5709     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5710
5711   /* Note that we need not handle conditional operations here since fold
5712      already handles those cases.  So just do arithmetic here.  */
5713   switch (tcode)
5714     {
5715     case INTEGER_CST:
5716       /* For a constant, we can always simplify if we are a multiply
5717          or (for divide and modulus) if it is a multiple of our constant.  */
5718       if (code == MULT_EXPR
5719           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
5720         return const_binop (code, fold_convert (ctype, t),
5721                             fold_convert (ctype, c), 0);
5722       break;
5723
5724     case CONVERT_EXPR:  case NON_LVALUE_EXPR:  case NOP_EXPR:
5725       /* If op0 is an expression ...  */
5726       if ((COMPARISON_CLASS_P (op0)
5727            || UNARY_CLASS_P (op0)
5728            || BINARY_CLASS_P (op0)
5729            || VL_EXP_CLASS_P (op0)
5730            || EXPRESSION_CLASS_P (op0))
5731           /* ... and is unsigned, and its type is smaller than ctype,
5732              then we cannot pass through as widening.  */
5733           && ((TYPE_UNSIGNED (TREE_TYPE (op0))
5734                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5735                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5736                && (GET_MODE_SIZE (TYPE_MODE (ctype))
5737                    > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
5738               /* ... or this is a truncation (t is narrower than op0),
5739                  then we cannot pass through this narrowing.  */
5740               || (GET_MODE_SIZE (TYPE_MODE (type))
5741                   < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
5742               /* ... or signedness changes for division or modulus,
5743                  then we cannot pass through this conversion.  */
5744               || (code != MULT_EXPR
5745                   && (TYPE_UNSIGNED (ctype)
5746                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
5747               /* ... or has undefined overflow while the converted to
5748                  type has not, we cannot do the operation in the inner type
5749                  as that would introduce undefined overflow.  */
5750               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5751                   && !TYPE_OVERFLOW_UNDEFINED (type))))
5752         break;
5753
5754       /* Pass the constant down and see if we can make a simplification.  If
5755          we can, replace this expression with the inner simplification for
5756          possible later conversion to our or some other type.  */
5757       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5758           && TREE_CODE (t2) == INTEGER_CST
5759           && !TREE_OVERFLOW (t2)
5760           && (0 != (t1 = extract_muldiv (op0, t2, code,
5761                                          code == MULT_EXPR
5762                                          ? ctype : NULL_TREE,
5763                                          strict_overflow_p))))
5764         return t1;
5765       break;
5766
5767     case ABS_EXPR:
5768       /* If widening the type changes it from signed to unsigned, then we
5769          must avoid building ABS_EXPR itself as unsigned.  */
5770       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5771         {
5772           tree cstype = (*signed_type_for) (ctype);
5773           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5774               != 0)
5775             {
5776               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5777               return fold_convert (ctype, t1);
5778             }
5779           break;
5780         }
5781       /* If the constant is negative, we cannot simplify this.  */
5782       if (tree_int_cst_sgn (c) == -1)
5783         break;
5784       /* FALLTHROUGH */
5785     case NEGATE_EXPR:
5786       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5787           != 0)
5788         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5789       break;
5790
5791     case MIN_EXPR:  case MAX_EXPR:
5792       /* If widening the type changes the signedness, then we can't perform
5793          this optimization as that changes the result.  */
5794       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5795         break;
5796
5797       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
5798       sub_strict_overflow_p = false;
5799       if ((t1 = extract_muldiv (op0, c, code, wide_type,
5800                                 &sub_strict_overflow_p)) != 0
5801           && (t2 = extract_muldiv (op1, c, code, wide_type,
5802                                    &sub_strict_overflow_p)) != 0)
5803         {
5804           if (tree_int_cst_sgn (c) < 0)
5805             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5806           if (sub_strict_overflow_p)
5807             *strict_overflow_p = true;
5808           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5809                               fold_convert (ctype, t2));
5810         }
5811       break;
5812
5813     case LSHIFT_EXPR:  case RSHIFT_EXPR:
5814       /* If the second operand is constant, this is a multiplication
5815          or floor division, by a power of two, so we can treat it that
5816          way unless the multiplier or divisor overflows.  Signed
5817          left-shift overflow is implementation-defined rather than
5818          undefined in C90, so do not convert signed left shift into
5819          multiplication.  */
5820       if (TREE_CODE (op1) == INTEGER_CST
5821           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5822           /* const_binop may not detect overflow correctly,
5823              so check for it explicitly here.  */
5824           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5825           && TREE_INT_CST_HIGH (op1) == 0
5826           && 0 != (t1 = fold_convert (ctype,
5827                                       const_binop (LSHIFT_EXPR,
5828                                                    size_one_node,
5829                                                    op1, 0)))
5830           && !TREE_OVERFLOW (t1))
5831         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5832                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
5833                                        ctype, fold_convert (ctype, op0), t1),
5834                                c, code, wide_type, strict_overflow_p);
5835       break;
5836
5837     case PLUS_EXPR:  case MINUS_EXPR:
5838       /* See if we can eliminate the operation on both sides.  If we can, we
5839          can return a new PLUS or MINUS.  If we can't, the only remaining
5840          cases where we can do anything are if the second operand is a
5841          constant.  */
5842       sub_strict_overflow_p = false;
5843       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5844       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5845       if (t1 != 0 && t2 != 0
5846           && (code == MULT_EXPR
5847               /* If not multiplication, we can only do this if both operands
5848                  are divisible by c.  */
5849               || (multiple_of_p (ctype, op0, c)
5850                   && multiple_of_p (ctype, op1, c))))
5851         {
5852           if (sub_strict_overflow_p)
5853             *strict_overflow_p = true;
5854           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5855                               fold_convert (ctype, t2));
5856         }
5857
5858       /* If this was a subtraction, negate OP1 and set it to be an addition.
5859          This simplifies the logic below.  */
5860       if (tcode == MINUS_EXPR)
5861         tcode = PLUS_EXPR, op1 = negate_expr (op1);
5862
5863       if (TREE_CODE (op1) != INTEGER_CST)
5864         break;
5865
5866       /* If either OP1 or C are negative, this optimization is not safe for
5867          some of the division and remainder types while for others we need
5868          to change the code.  */
5869       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5870         {
5871           if (code == CEIL_DIV_EXPR)
5872             code = FLOOR_DIV_EXPR;
5873           else if (code == FLOOR_DIV_EXPR)
5874             code = CEIL_DIV_EXPR;
5875           else if (code != MULT_EXPR
5876                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5877             break;
5878         }
5879
5880       /* If it's a multiply or a division/modulus operation of a multiple
5881          of our constant, do the operation and verify it doesn't overflow.  */
5882       if (code == MULT_EXPR
5883           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5884         {
5885           op1 = const_binop (code, fold_convert (ctype, op1),
5886                              fold_convert (ctype, c), 0);
5887           /* We allow the constant to overflow with wrapping semantics.  */
5888           if (op1 == 0
5889               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5890             break;
5891         }
5892       else
5893         break;
5894
5895       /* If we have an unsigned type is not a sizetype, we cannot widen
5896          the operation since it will change the result if the original
5897          computation overflowed.  */
5898       if (TYPE_UNSIGNED (ctype)
5899           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5900           && ctype != type)
5901         break;
5902
5903       /* If we were able to eliminate our operation from the first side,
5904          apply our operation to the second side and reform the PLUS.  */
5905       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5906         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5907
5908       /* The last case is if we are a multiply.  In that case, we can
5909          apply the distributive law to commute the multiply and addition
5910          if the multiplication of the constants doesn't overflow.  */
5911       if (code == MULT_EXPR)
5912         return fold_build2 (tcode, ctype,
5913                             fold_build2 (code, ctype,
5914                                          fold_convert (ctype, op0),
5915                                          fold_convert (ctype, c)),
5916                             op1);
5917
5918       break;
5919
5920     case MULT_EXPR:
5921       /* We have a special case here if we are doing something like
5922          (C * 8) % 4 since we know that's zero.  */
5923       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5924            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5925           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5926           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5927         return omit_one_operand (type, integer_zero_node, op0);
5928
5929       /* ... fall through ...  */
5930
5931     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
5932     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
5933       /* If we can extract our operation from the LHS, do so and return a
5934          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
5935          do something only if the second operand is a constant.  */
5936       if (same_p
5937           && (t1 = extract_muldiv (op0, c, code, wide_type,
5938                                    strict_overflow_p)) != 0)
5939         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5940                             fold_convert (ctype, op1));
5941       else if (tcode == MULT_EXPR && code == MULT_EXPR
5942                && (t1 = extract_muldiv (op1, c, code, wide_type,
5943                                         strict_overflow_p)) != 0)
5944         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5945                             fold_convert (ctype, t1));
5946       else if (TREE_CODE (op1) != INTEGER_CST)
5947         return 0;
5948
5949       /* If these are the same operation types, we can associate them
5950          assuming no overflow.  */
5951       if (tcode == code
5952           && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
5953                                      fold_convert (ctype, c), 0))
5954           && !TREE_OVERFLOW (t1))
5955         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5956
5957       /* If these operations "cancel" each other, we have the main
5958          optimizations of this pass, which occur when either constant is a
5959          multiple of the other, in which case we replace this with either an
5960          operation or CODE or TCODE.
5961
5962          If we have an unsigned type that is not a sizetype, we cannot do
5963          this since it will change the result if the original computation
5964          overflowed.  */
5965       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5966            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5967           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5968               || (tcode == MULT_EXPR
5969                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5970                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5971                   && code != MULT_EXPR)))
5972         {
5973           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5974             {
5975               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5976                 *strict_overflow_p = true;
5977               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5978                                   fold_convert (ctype,
5979                                                 const_binop (TRUNC_DIV_EXPR,
5980                                                              op1, c, 0)));
5981             }
5982           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
5983             {
5984               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5985                 *strict_overflow_p = true;
5986               return fold_build2 (code, ctype, fold_convert (ctype, op0),
5987                                   fold_convert (ctype,
5988                                                 const_binop (TRUNC_DIV_EXPR,
5989                                                              c, op1, 0)));
5990             }
5991         }
5992       break;
5993
5994     default:
5995       break;
5996     }
5997
5998   return 0;
5999 }
6000 \f
6001 /* Return a node which has the indicated constant VALUE (either 0 or
6002    1), and is of the indicated TYPE.  */
6003
6004 tree
6005 constant_boolean_node (int value, tree type)
6006 {
6007   if (type == integer_type_node)
6008     return value ? integer_one_node : integer_zero_node;
6009   else if (type == boolean_type_node)
6010     return value ? boolean_true_node : boolean_false_node;
6011   else
6012     return build_int_cst (type, value);
6013 }
6014
6015
6016 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6017    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6018    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6019    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6020    COND is the first argument to CODE; otherwise (as in the example
6021    given here), it is the second argument.  TYPE is the type of the
6022    original expression.  Return NULL_TREE if no simplification is
6023    possible.  */
6024
6025 static tree
6026 fold_binary_op_with_conditional_arg (enum tree_code code,
6027                                      tree type, tree op0, tree op1,
6028                                      tree cond, tree arg, int cond_first_p)
6029 {
6030   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6031   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6032   tree test, true_value, false_value;
6033   tree lhs = NULL_TREE;
6034   tree rhs = NULL_TREE;
6035
6036   /* This transformation is only worthwhile if we don't have to wrap
6037      arg in a SAVE_EXPR, and the operation can be simplified on at least
6038      one of the branches once its pushed inside the COND_EXPR.  */
6039   if (!TREE_CONSTANT (arg))
6040     return NULL_TREE;
6041
6042   if (TREE_CODE (cond) == COND_EXPR)
6043     {
6044       test = TREE_OPERAND (cond, 0);
6045       true_value = TREE_OPERAND (cond, 1);
6046       false_value = TREE_OPERAND (cond, 2);
6047       /* If this operand throws an expression, then it does not make
6048          sense to try to perform a logical or arithmetic operation
6049          involving it.  */
6050       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6051         lhs = true_value;
6052       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6053         rhs = false_value;
6054     }
6055   else
6056     {
6057       tree testtype = TREE_TYPE (cond);
6058       test = cond;
6059       true_value = constant_boolean_node (true, testtype);
6060       false_value = constant_boolean_node (false, testtype);
6061     }
6062
6063   arg = fold_convert (arg_type, arg);
6064   if (lhs == 0)
6065     {
6066       true_value = fold_convert (cond_type, true_value);
6067       if (cond_first_p)
6068         lhs = fold_build2 (code, type, true_value, arg);
6069       else
6070         lhs = fold_build2 (code, type, arg, true_value);
6071     }
6072   if (rhs == 0)
6073     {
6074       false_value = fold_convert (cond_type, false_value);
6075       if (cond_first_p)
6076         rhs = fold_build2 (code, type, false_value, arg);
6077       else
6078         rhs = fold_build2 (code, type, arg, false_value);
6079     }
6080
6081   test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6082   return fold_convert (type, test);
6083 }
6084
6085 \f
6086 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6087
6088    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6089    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6090    ADDEND is the same as X.
6091
6092    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6093    and finite.  The problematic cases are when X is zero, and its mode
6094    has signed zeros.  In the case of rounding towards -infinity,
6095    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6096    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6097
6098 bool
6099 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6100 {
6101   if (!real_zerop (addend))
6102     return false;
6103
6104   /* Don't allow the fold with -fsignaling-nans.  */
6105   if (HONOR_SNANS (TYPE_MODE (type)))
6106     return false;
6107
6108   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6109   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6110     return true;
6111
6112   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6113   if (TREE_CODE (addend) == REAL_CST
6114       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6115     negate = !negate;
6116
6117   /* The mode has signed zeros, and we have to honor their sign.
6118      In this situation, there is only one case we can return true for.
6119      X - 0 is the same as X unless rounding towards -infinity is
6120      supported.  */
6121   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6122 }
6123
6124 /* Subroutine of fold() that checks comparisons of built-in math
6125    functions against real constants.
6126
6127    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6128    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6129    is the type of the result and ARG0 and ARG1 are the operands of the
6130    comparison.  ARG1 must be a TREE_REAL_CST.
6131
6132    The function returns the constant folded tree if a simplification
6133    can be made, and NULL_TREE otherwise.  */
6134
6135 static tree
6136 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6137                      tree type, tree arg0, tree arg1)
6138 {
6139   REAL_VALUE_TYPE c;
6140
6141   if (BUILTIN_SQRT_P (fcode))
6142     {
6143       tree arg = CALL_EXPR_ARG (arg0, 0);
6144       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6145
6146       c = TREE_REAL_CST (arg1);
6147       if (REAL_VALUE_NEGATIVE (c))
6148         {
6149           /* sqrt(x) < y is always false, if y is negative.  */
6150           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6151             return omit_one_operand (type, integer_zero_node, arg);
6152
6153           /* sqrt(x) > y is always true, if y is negative and we
6154              don't care about NaNs, i.e. negative values of x.  */
6155           if (code == NE_EXPR || !HONOR_NANS (mode))
6156             return omit_one_operand (type, integer_one_node, arg);
6157
6158           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6159           return fold_build2 (GE_EXPR, type, arg,
6160                               build_real (TREE_TYPE (arg), dconst0));
6161         }
6162       else if (code == GT_EXPR || code == GE_EXPR)
6163         {
6164           REAL_VALUE_TYPE c2;
6165
6166           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6167           real_convert (&c2, mode, &c2);
6168
6169           if (REAL_VALUE_ISINF (c2))
6170             {
6171               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6172               if (HONOR_INFINITIES (mode))
6173                 return fold_build2 (EQ_EXPR, type, arg,
6174                                     build_real (TREE_TYPE (arg), c2));
6175
6176               /* sqrt(x) > y is always false, when y is very large
6177                  and we don't care about infinities.  */
6178               return omit_one_operand (type, integer_zero_node, arg);
6179             }
6180
6181           /* sqrt(x) > c is the same as x > c*c.  */
6182           return fold_build2 (code, type, arg,
6183                               build_real (TREE_TYPE (arg), c2));
6184         }
6185       else if (code == LT_EXPR || code == LE_EXPR)
6186         {
6187           REAL_VALUE_TYPE c2;
6188
6189           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6190           real_convert (&c2, mode, &c2);
6191
6192           if (REAL_VALUE_ISINF (c2))
6193             {
6194               /* sqrt(x) < y is always true, when y is a very large
6195                  value and we don't care about NaNs or Infinities.  */
6196               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6197                 return omit_one_operand (type, integer_one_node, arg);
6198
6199               /* sqrt(x) < y is x != +Inf when y is very large and we
6200                  don't care about NaNs.  */
6201               if (! HONOR_NANS (mode))
6202                 return fold_build2 (NE_EXPR, type, arg,
6203                                     build_real (TREE_TYPE (arg), c2));
6204
6205               /* sqrt(x) < y is x >= 0 when y is very large and we
6206                  don't care about Infinities.  */
6207               if (! HONOR_INFINITIES (mode))
6208                 return fold_build2 (GE_EXPR, type, arg,
6209                                     build_real (TREE_TYPE (arg), dconst0));
6210
6211               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6212               if (lang_hooks.decls.global_bindings_p () != 0
6213                   || CONTAINS_PLACEHOLDER_P (arg))
6214                 return NULL_TREE;
6215
6216               arg = save_expr (arg);
6217               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6218                                   fold_build2 (GE_EXPR, type, arg,
6219                                                build_real (TREE_TYPE (arg),
6220                                                            dconst0)),
6221                                   fold_build2 (NE_EXPR, type, arg,
6222                                                build_real (TREE_TYPE (arg),
6223                                                            c2)));
6224             }
6225
6226           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6227           if (! HONOR_NANS (mode))
6228             return fold_build2 (code, type, arg,
6229                                 build_real (TREE_TYPE (arg), c2));
6230
6231           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6232           if (lang_hooks.decls.global_bindings_p () == 0
6233               && ! CONTAINS_PLACEHOLDER_P (arg))
6234             {
6235               arg = save_expr (arg);
6236               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6237                                   fold_build2 (GE_EXPR, type, arg,
6238                                                build_real (TREE_TYPE (arg),
6239                                                            dconst0)),
6240                                   fold_build2 (code, type, arg,
6241                                                build_real (TREE_TYPE (arg),
6242                                                            c2)));
6243             }
6244         }
6245     }
6246
6247   return NULL_TREE;
6248 }
6249
6250 /* Subroutine of fold() that optimizes comparisons against Infinities,
6251    either +Inf or -Inf.
6252
6253    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6254    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6255    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6256
6257    The function returns the constant folded tree if a simplification
6258    can be made, and NULL_TREE otherwise.  */
6259
6260 static tree
6261 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6262 {
6263   enum machine_mode mode;
6264   REAL_VALUE_TYPE max;
6265   tree temp;
6266   bool neg;
6267
6268   mode = TYPE_MODE (TREE_TYPE (arg0));
6269
6270   /* For negative infinity swap the sense of the comparison.  */
6271   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6272   if (neg)
6273     code = swap_tree_comparison (code);
6274
6275   switch (code)
6276     {
6277     case GT_EXPR:
6278       /* x > +Inf is always false, if with ignore sNANs.  */
6279       if (HONOR_SNANS (mode))
6280         return NULL_TREE;
6281       return omit_one_operand (type, integer_zero_node, arg0);
6282
6283     case LE_EXPR:
6284       /* x <= +Inf is always true, if we don't case about NaNs.  */
6285       if (! HONOR_NANS (mode))
6286         return omit_one_operand (type, integer_one_node, arg0);
6287
6288       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6289       if (lang_hooks.decls.global_bindings_p () == 0
6290           && ! CONTAINS_PLACEHOLDER_P (arg0))
6291         {
6292           arg0 = save_expr (arg0);
6293           return fold_build2 (EQ_EXPR, type, arg0, arg0);
6294         }
6295       break;
6296
6297     case EQ_EXPR:
6298     case GE_EXPR:
6299       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6300       real_maxval (&max, neg, mode);
6301       return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6302                           arg0, build_real (TREE_TYPE (arg0), max));
6303
6304     case LT_EXPR:
6305       /* x < +Inf is always equal to x <= DBL_MAX.  */
6306       real_maxval (&max, neg, mode);
6307       return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6308                           arg0, build_real (TREE_TYPE (arg0), max));
6309
6310     case NE_EXPR:
6311       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6312       real_maxval (&max, neg, mode);
6313       if (! HONOR_NANS (mode))
6314         return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6315                             arg0, build_real (TREE_TYPE (arg0), max));
6316
6317       temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6318                           arg0, build_real (TREE_TYPE (arg0), max));
6319       return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6320
6321     default:
6322       break;
6323     }
6324
6325   return NULL_TREE;
6326 }
6327
6328 /* Subroutine of fold() that optimizes comparisons of a division by
6329    a nonzero integer constant against an integer constant, i.e.
6330    X/C1 op C2.
6331
6332    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6333    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6334    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6335
6336    The function returns the constant folded tree if a simplification
6337    can be made, and NULL_TREE otherwise.  */
6338
6339 static tree
6340 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6341 {
6342   tree prod, tmp, hi, lo;
6343   tree arg00 = TREE_OPERAND (arg0, 0);
6344   tree arg01 = TREE_OPERAND (arg0, 1);
6345   unsigned HOST_WIDE_INT lpart;
6346   HOST_WIDE_INT hpart;
6347   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6348   bool neg_overflow;
6349   int overflow;
6350
6351   /* We have to do this the hard way to detect unsigned overflow.
6352      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
6353   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6354                                    TREE_INT_CST_HIGH (arg01),
6355                                    TREE_INT_CST_LOW (arg1),
6356                                    TREE_INT_CST_HIGH (arg1),
6357                                    &lpart, &hpart, unsigned_p);
6358   prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6359                                 -1, overflow);
6360   neg_overflow = false;
6361
6362   if (unsigned_p)
6363     {
6364       tmp = int_const_binop (MINUS_EXPR, arg01,
6365                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6366       lo = prod;
6367
6368       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
6369       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6370                                        TREE_INT_CST_HIGH (prod),
6371                                        TREE_INT_CST_LOW (tmp),
6372                                        TREE_INT_CST_HIGH (tmp),
6373                                        &lpart, &hpart, unsigned_p);
6374       hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6375                                   -1, overflow | TREE_OVERFLOW (prod));
6376     }
6377   else if (tree_int_cst_sgn (arg01) >= 0)
6378     {
6379       tmp = int_const_binop (MINUS_EXPR, arg01,
6380                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6381       switch (tree_int_cst_sgn (arg1))
6382         {
6383         case -1:
6384           neg_overflow = true;
6385           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6386           hi = prod;
6387           break;
6388
6389         case  0:
6390           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6391           hi = tmp;
6392           break;
6393
6394         case  1:
6395           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6396           lo = prod;
6397           break;
6398
6399         default:
6400           gcc_unreachable ();
6401         }
6402     }
6403   else
6404     {
6405       /* A negative divisor reverses the relational operators.  */
6406       code = swap_tree_comparison (code);
6407
6408       tmp = int_const_binop (PLUS_EXPR, arg01,
6409                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6410       switch (tree_int_cst_sgn (arg1))
6411         {
6412         case -1:
6413           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6414           lo = prod;
6415           break;
6416
6417         case  0:
6418           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6419           lo = tmp;
6420           break;
6421
6422         case  1:
6423           neg_overflow = true;
6424           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6425           hi = prod;
6426           break;
6427
6428         default:
6429           gcc_unreachable ();
6430         }
6431     }
6432
6433   switch (code)
6434     {
6435     case EQ_EXPR:
6436       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6437         return omit_one_operand (type, integer_zero_node, arg00);
6438       if (TREE_OVERFLOW (hi))
6439         return fold_build2 (GE_EXPR, type, arg00, lo);
6440       if (TREE_OVERFLOW (lo))
6441         return fold_build2 (LE_EXPR, type, arg00, hi);
6442       return build_range_check (type, arg00, 1, lo, hi);
6443
6444     case NE_EXPR:
6445       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6446         return omit_one_operand (type, integer_one_node, arg00);
6447       if (TREE_OVERFLOW (hi))
6448         return fold_build2 (LT_EXPR, type, arg00, lo);
6449       if (TREE_OVERFLOW (lo))
6450         return fold_build2 (GT_EXPR, type, arg00, hi);
6451       return build_range_check (type, arg00, 0, lo, hi);
6452
6453     case LT_EXPR:
6454       if (TREE_OVERFLOW (lo))
6455         {
6456           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6457           return omit_one_operand (type, tmp, arg00);
6458         }
6459       return fold_build2 (LT_EXPR, type, arg00, lo);
6460
6461     case LE_EXPR:
6462       if (TREE_OVERFLOW (hi))
6463         {
6464           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6465           return omit_one_operand (type, tmp, arg00);
6466         }
6467       return fold_build2 (LE_EXPR, type, arg00, hi);
6468
6469     case GT_EXPR:
6470       if (TREE_OVERFLOW (hi))
6471         {
6472           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6473           return omit_one_operand (type, tmp, arg00);
6474         }
6475       return fold_build2 (GT_EXPR, type, arg00, hi);
6476
6477     case GE_EXPR:
6478       if (TREE_OVERFLOW (lo))
6479         {
6480           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6481           return omit_one_operand (type, tmp, arg00);
6482         }
6483       return fold_build2 (GE_EXPR, type, arg00, lo);
6484
6485     default:
6486       break;
6487     }
6488
6489   return NULL_TREE;
6490 }
6491
6492
6493 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6494    equality/inequality test, then return a simplified form of the test
6495    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6496    result type.  */
6497
6498 static tree
6499 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6500                                      tree result_type)
6501 {
6502   /* If this is testing a single bit, we can optimize the test.  */
6503   if ((code == NE_EXPR || code == EQ_EXPR)
6504       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6505       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6506     {
6507       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6508          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6509       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6510
6511       if (arg00 != NULL_TREE
6512           /* This is only a win if casting to a signed type is cheap,
6513              i.e. when arg00's type is not a partial mode.  */
6514           && TYPE_PRECISION (TREE_TYPE (arg00))
6515              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6516         {
6517           tree stype = signed_type_for (TREE_TYPE (arg00));
6518           return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6519                               result_type, fold_convert (stype, arg00),
6520                               build_int_cst (stype, 0));
6521         }
6522     }
6523
6524   return NULL_TREE;
6525 }
6526
6527 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6528    equality/inequality test, then return a simplified form of
6529    the test using shifts and logical operations.  Otherwise return
6530    NULL.  TYPE is the desired result type.  */
6531
6532 tree
6533 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6534                       tree result_type)
6535 {
6536   /* If this is testing a single bit, we can optimize the test.  */
6537   if ((code == NE_EXPR || code == EQ_EXPR)
6538       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6539       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6540     {
6541       tree inner = TREE_OPERAND (arg0, 0);
6542       tree type = TREE_TYPE (arg0);
6543       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6544       enum machine_mode operand_mode = TYPE_MODE (type);
6545       int ops_unsigned;
6546       tree signed_type, unsigned_type, intermediate_type;
6547       tree tem, one;
6548
6549       /* First, see if we can fold the single bit test into a sign-bit
6550          test.  */
6551       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6552                                                  result_type);
6553       if (tem)
6554         return tem;
6555
6556       /* Otherwise we have (A & C) != 0 where C is a single bit,
6557          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6558          Similarly for (A & C) == 0.  */
6559
6560       /* If INNER is a right shift of a constant and it plus BITNUM does
6561          not overflow, adjust BITNUM and INNER.  */
6562       if (TREE_CODE (inner) == RSHIFT_EXPR
6563           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6564           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6565           && bitnum < TYPE_PRECISION (type)
6566           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6567                                    bitnum - TYPE_PRECISION (type)))
6568         {
6569           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6570           inner = TREE_OPERAND (inner, 0);
6571         }
6572
6573       /* If we are going to be able to omit the AND below, we must do our
6574          operations as unsigned.  If we must use the AND, we have a choice.
6575          Normally unsigned is faster, but for some machines signed is.  */
6576 #ifdef LOAD_EXTEND_OP
6577       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND 
6578                       && !flag_syntax_only) ? 0 : 1;
6579 #else
6580       ops_unsigned = 1;
6581 #endif
6582
6583       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6584       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6585       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6586       inner = fold_convert (intermediate_type, inner);
6587
6588       if (bitnum != 0)
6589         inner = build2 (RSHIFT_EXPR, intermediate_type,
6590                         inner, size_int (bitnum));
6591
6592       one = build_int_cst (intermediate_type, 1);
6593
6594       if (code == EQ_EXPR)
6595         inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6596
6597       /* Put the AND last so it can combine with more things.  */
6598       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6599
6600       /* Make sure to return the proper type.  */
6601       inner = fold_convert (result_type, inner);
6602
6603       return inner;
6604     }
6605   return NULL_TREE;
6606 }
6607
6608 /* Check whether we are allowed to reorder operands arg0 and arg1,
6609    such that the evaluation of arg1 occurs before arg0.  */
6610
6611 static bool
6612 reorder_operands_p (const_tree arg0, const_tree arg1)
6613 {
6614   if (! flag_evaluation_order)
6615       return true;
6616   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6617     return true;
6618   return ! TREE_SIDE_EFFECTS (arg0)
6619          && ! TREE_SIDE_EFFECTS (arg1);
6620 }
6621
6622 /* Test whether it is preferable two swap two operands, ARG0 and
6623    ARG1, for example because ARG0 is an integer constant and ARG1
6624    isn't.  If REORDER is true, only recommend swapping if we can
6625    evaluate the operands in reverse order.  */
6626
6627 bool
6628 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6629 {
6630   STRIP_SIGN_NOPS (arg0);
6631   STRIP_SIGN_NOPS (arg1);
6632
6633   if (TREE_CODE (arg1) == INTEGER_CST)
6634     return 0;
6635   if (TREE_CODE (arg0) == INTEGER_CST)
6636     return 1;
6637
6638   if (TREE_CODE (arg1) == REAL_CST)
6639     return 0;
6640   if (TREE_CODE (arg0) == REAL_CST)
6641     return 1;
6642
6643   if (TREE_CODE (arg1) == FIXED_CST)
6644     return 0;
6645   if (TREE_CODE (arg0) == FIXED_CST)
6646     return 1;
6647
6648   if (TREE_CODE (arg1) == COMPLEX_CST)
6649     return 0;
6650   if (TREE_CODE (arg0) == COMPLEX_CST)
6651     return 1;
6652
6653   if (TREE_CONSTANT (arg1))
6654     return 0;
6655   if (TREE_CONSTANT (arg0))
6656     return 1;
6657
6658   if (optimize_size)
6659     return 0;
6660
6661   if (reorder && flag_evaluation_order
6662       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6663     return 0;
6664
6665   /* It is preferable to swap two SSA_NAME to ensure a canonical form
6666      for commutative and comparison operators.  Ensuring a canonical
6667      form allows the optimizers to find additional redundancies without
6668      having to explicitly check for both orderings.  */
6669   if (TREE_CODE (arg0) == SSA_NAME
6670       && TREE_CODE (arg1) == SSA_NAME
6671       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6672     return 1;
6673
6674   /* Put SSA_NAMEs last.  */
6675   if (TREE_CODE (arg1) == SSA_NAME)
6676     return 0;
6677   if (TREE_CODE (arg0) == SSA_NAME)
6678     return 1;
6679
6680   /* Put variables last.  */
6681   if (DECL_P (arg1))
6682     return 0;
6683   if (DECL_P (arg0))
6684     return 1;
6685
6686   return 0;
6687 }
6688
6689 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6690    ARG0 is extended to a wider type.  */
6691
6692 static tree
6693 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
6694 {
6695   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6696   tree arg1_unw;
6697   tree shorter_type, outer_type;
6698   tree min, max;
6699   bool above, below;
6700
6701   if (arg0_unw == arg0)
6702     return NULL_TREE;
6703   shorter_type = TREE_TYPE (arg0_unw);
6704
6705 #ifdef HAVE_canonicalize_funcptr_for_compare
6706   /* Disable this optimization if we're casting a function pointer
6707      type on targets that require function pointer canonicalization.  */
6708   if (HAVE_canonicalize_funcptr_for_compare
6709       && TREE_CODE (shorter_type) == POINTER_TYPE
6710       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6711     return NULL_TREE;
6712 #endif
6713
6714   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6715     return NULL_TREE;
6716
6717   arg1_unw = get_unwidened (arg1, NULL_TREE);
6718
6719   /* If possible, express the comparison in the shorter mode.  */
6720   if ((code == EQ_EXPR || code == NE_EXPR
6721        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6722       && (TREE_TYPE (arg1_unw) == shorter_type
6723           || (TYPE_PRECISION (shorter_type)
6724               > TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6725           || ((TYPE_PRECISION (shorter_type)
6726                == TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6727               && (TYPE_UNSIGNED (shorter_type)
6728                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6729           || (TREE_CODE (arg1_unw) == INTEGER_CST
6730               && (TREE_CODE (shorter_type) == INTEGER_TYPE
6731                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6732               && int_fits_type_p (arg1_unw, shorter_type))))
6733     return fold_build2 (code, type, arg0_unw,
6734                        fold_convert (shorter_type, arg1_unw));
6735
6736   if (TREE_CODE (arg1_unw) != INTEGER_CST
6737       || TREE_CODE (shorter_type) != INTEGER_TYPE
6738       || !int_fits_type_p (arg1_unw, shorter_type))
6739     return NULL_TREE;
6740
6741   /* If we are comparing with the integer that does not fit into the range
6742      of the shorter type, the result is known.  */
6743   outer_type = TREE_TYPE (arg1_unw);
6744   min = lower_bound_in_type (outer_type, shorter_type);
6745   max = upper_bound_in_type (outer_type, shorter_type);
6746
6747   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6748                                                    max, arg1_unw));
6749   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6750                                                    arg1_unw, min));
6751
6752   switch (code)
6753     {
6754     case EQ_EXPR:
6755       if (above || below)
6756         return omit_one_operand (type, integer_zero_node, arg0);
6757       break;
6758
6759     case NE_EXPR:
6760       if (above || below)
6761         return omit_one_operand (type, integer_one_node, arg0);
6762       break;
6763
6764     case LT_EXPR:
6765     case LE_EXPR:
6766       if (above)
6767         return omit_one_operand (type, integer_one_node, arg0);
6768       else if (below)
6769         return omit_one_operand (type, integer_zero_node, arg0);
6770
6771     case GT_EXPR:
6772     case GE_EXPR:
6773       if (above)
6774         return omit_one_operand (type, integer_zero_node, arg0);
6775       else if (below)
6776         return omit_one_operand (type, integer_one_node, arg0);
6777
6778     default:
6779       break;
6780     }
6781
6782   return NULL_TREE;
6783 }
6784
6785 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6786    ARG0 just the signedness is changed.  */
6787
6788 static tree
6789 fold_sign_changed_comparison (enum tree_code code, tree type,
6790                               tree arg0, tree arg1)
6791 {
6792   tree arg0_inner;
6793   tree inner_type, outer_type;
6794
6795   if (TREE_CODE (arg0) != NOP_EXPR
6796       && TREE_CODE (arg0) != CONVERT_EXPR)
6797     return NULL_TREE;
6798
6799   outer_type = TREE_TYPE (arg0);
6800   arg0_inner = TREE_OPERAND (arg0, 0);
6801   inner_type = TREE_TYPE (arg0_inner);
6802
6803 #ifdef HAVE_canonicalize_funcptr_for_compare
6804   /* Disable this optimization if we're casting a function pointer
6805      type on targets that require function pointer canonicalization.  */
6806   if (HAVE_canonicalize_funcptr_for_compare
6807       && TREE_CODE (inner_type) == POINTER_TYPE
6808       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6809     return NULL_TREE;
6810 #endif
6811
6812   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6813     return NULL_TREE;
6814
6815   /* If the conversion is from an integral subtype to its basetype
6816      leave it alone.  */
6817   if (TREE_TYPE (inner_type) == outer_type)
6818     return NULL_TREE;
6819
6820   if (TREE_CODE (arg1) != INTEGER_CST
6821       && !((TREE_CODE (arg1) == NOP_EXPR
6822             || TREE_CODE (arg1) == CONVERT_EXPR)
6823            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6824     return NULL_TREE;
6825
6826   if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6827       && code != NE_EXPR
6828       && code != EQ_EXPR)
6829     return NULL_TREE;
6830
6831   if (TREE_CODE (arg1) == INTEGER_CST)
6832     arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
6833                                   TREE_INT_CST_HIGH (arg1), 0,
6834                                   TREE_OVERFLOW (arg1));
6835   else
6836     arg1 = fold_convert (inner_type, arg1);
6837
6838   return fold_build2 (code, type, arg0_inner, arg1);
6839 }
6840
6841 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6842    step of the array.  Reconstructs s and delta in the case of s * delta
6843    being an integer constant (and thus already folded).
6844    ADDR is the address. MULT is the multiplicative expression.
6845    If the function succeeds, the new address expression is returned.  Otherwise
6846    NULL_TREE is returned.  */
6847
6848 static tree
6849 try_move_mult_to_index (tree addr, tree op1)
6850 {
6851   tree s, delta, step;
6852   tree ref = TREE_OPERAND (addr, 0), pref;
6853   tree ret, pos;
6854   tree itype;
6855   bool mdim = false;
6856
6857   /*  Strip the nops that might be added when converting op1 to sizetype. */
6858   STRIP_NOPS (op1);
6859
6860   /* Canonicalize op1 into a possibly non-constant delta
6861      and an INTEGER_CST s.  */
6862   if (TREE_CODE (op1) == MULT_EXPR)
6863     {
6864       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6865
6866       STRIP_NOPS (arg0);
6867       STRIP_NOPS (arg1);
6868   
6869       if (TREE_CODE (arg0) == INTEGER_CST)
6870         {
6871           s = arg0;
6872           delta = arg1;
6873         }
6874       else if (TREE_CODE (arg1) == INTEGER_CST)
6875         {
6876           s = arg1;
6877           delta = arg0;
6878         }
6879       else
6880         return NULL_TREE;
6881     }
6882   else if (TREE_CODE (op1) == INTEGER_CST)
6883     {
6884       delta = op1;
6885       s = NULL_TREE;
6886     }
6887   else
6888     {
6889       /* Simulate we are delta * 1.  */
6890       delta = op1;
6891       s = integer_one_node;
6892     }
6893
6894   for (;; ref = TREE_OPERAND (ref, 0))
6895     {
6896       if (TREE_CODE (ref) == ARRAY_REF)
6897         {
6898           /* Remember if this was a multi-dimensional array.  */
6899           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6900             mdim = true;
6901
6902           itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6903           if (! itype)
6904             continue;
6905
6906           step = array_ref_element_size (ref);
6907           if (TREE_CODE (step) != INTEGER_CST)
6908             continue;
6909
6910           if (s)
6911             {
6912               if (! tree_int_cst_equal (step, s))
6913                 continue;
6914             }
6915           else
6916             {
6917               /* Try if delta is a multiple of step.  */
6918               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, delta, step);
6919               if (! tmp)
6920                 continue;
6921               delta = tmp;
6922             }
6923
6924           /* Only fold here if we can verify we do not overflow one
6925              dimension of a multi-dimensional array.  */
6926           if (mdim)
6927             {
6928               tree tmp;
6929
6930               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6931                   || !INTEGRAL_TYPE_P (itype)
6932                   || !TYPE_MAX_VALUE (itype)
6933                   || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
6934                 continue;
6935
6936               tmp = fold_binary (PLUS_EXPR, itype,
6937                                  fold_convert (itype,
6938                                                TREE_OPERAND (ref, 1)),
6939                                  fold_convert (itype, delta));
6940               if (!tmp
6941                   || TREE_CODE (tmp) != INTEGER_CST
6942                   || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
6943                 continue;
6944             }
6945
6946           break;
6947         }
6948       else
6949         mdim = false;
6950
6951       if (!handled_component_p (ref))
6952         return NULL_TREE;
6953     }
6954
6955   /* We found the suitable array reference.  So copy everything up to it,
6956      and replace the index.  */
6957
6958   pref = TREE_OPERAND (addr, 0);
6959   ret = copy_node (pref);
6960   pos = ret;
6961
6962   while (pref != ref)
6963     {
6964       pref = TREE_OPERAND (pref, 0);
6965       TREE_OPERAND (pos, 0) = copy_node (pref);
6966       pos = TREE_OPERAND (pos, 0);
6967     }
6968
6969   TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
6970                                        fold_convert (itype,
6971                                                      TREE_OPERAND (pos, 1)),
6972                                        fold_convert (itype, delta));
6973
6974   return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
6975 }
6976
6977
6978 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
6979    means A >= Y && A != MAX, but in this case we know that
6980    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
6981
6982 static tree
6983 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
6984 {
6985   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6986
6987   if (TREE_CODE (bound) == LT_EXPR)
6988     a = TREE_OPERAND (bound, 0);
6989   else if (TREE_CODE (bound) == GT_EXPR)
6990     a = TREE_OPERAND (bound, 1);
6991   else
6992     return NULL_TREE;
6993
6994   typea = TREE_TYPE (a);
6995   if (!INTEGRAL_TYPE_P (typea)
6996       && !POINTER_TYPE_P (typea))
6997     return NULL_TREE;
6998
6999   if (TREE_CODE (ineq) == LT_EXPR)
7000     {
7001       a1 = TREE_OPERAND (ineq, 1);
7002       y = TREE_OPERAND (ineq, 0);
7003     }
7004   else if (TREE_CODE (ineq) == GT_EXPR)
7005     {
7006       a1 = TREE_OPERAND (ineq, 0);
7007       y = TREE_OPERAND (ineq, 1);
7008     }
7009   else
7010     return NULL_TREE;
7011
7012   if (TREE_TYPE (a1) != typea)
7013     return NULL_TREE;
7014
7015   if (POINTER_TYPE_P (typea))
7016     {
7017       /* Convert the pointer types into integer before taking the difference.  */
7018       tree ta = fold_convert (ssizetype, a);
7019       tree ta1 = fold_convert (ssizetype, a1);
7020       diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7021     }
7022   else
7023    diff = fold_binary (MINUS_EXPR, typea, a1, a);
7024
7025   if (!diff || !integer_onep (diff))
7026    return NULL_TREE;
7027
7028   return fold_build2 (GE_EXPR, type, a, y);
7029 }
7030
7031 /* Fold a sum or difference of at least one multiplication.
7032    Returns the folded tree or NULL if no simplification could be made.  */
7033
7034 static tree
7035 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7036 {
7037   tree arg00, arg01, arg10, arg11;
7038   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7039
7040   /* (A * C) +- (B * C) -> (A+-B) * C.
7041      (A * C) +- A -> A * (C+-1).
7042      We are most concerned about the case where C is a constant,
7043      but other combinations show up during loop reduction.  Since
7044      it is not difficult, try all four possibilities.  */
7045
7046   if (TREE_CODE (arg0) == MULT_EXPR)
7047     {
7048       arg00 = TREE_OPERAND (arg0, 0);
7049       arg01 = TREE_OPERAND (arg0, 1);
7050     }
7051   else if (TREE_CODE (arg0) == INTEGER_CST)
7052     {
7053       arg00 = build_one_cst (type);
7054       arg01 = arg0;
7055     }
7056   else
7057     {
7058       /* We cannot generate constant 1 for fract.  */
7059       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7060         return NULL_TREE;
7061       arg00 = arg0;
7062       arg01 = build_one_cst (type);
7063     }
7064   if (TREE_CODE (arg1) == MULT_EXPR)
7065     {
7066       arg10 = TREE_OPERAND (arg1, 0);
7067       arg11 = TREE_OPERAND (arg1, 1);
7068     }
7069   else if (TREE_CODE (arg1) == INTEGER_CST)
7070     {
7071       arg10 = build_one_cst (type);
7072       arg11 = arg1;
7073     }
7074   else
7075     {
7076       /* We cannot generate constant 1 for fract.  */
7077       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7078         return NULL_TREE;
7079       arg10 = arg1;
7080       arg11 = build_one_cst (type);
7081     }
7082   same = NULL_TREE;
7083
7084   if (operand_equal_p (arg01, arg11, 0))
7085     same = arg01, alt0 = arg00, alt1 = arg10;
7086   else if (operand_equal_p (arg00, arg10, 0))
7087     same = arg00, alt0 = arg01, alt1 = arg11;
7088   else if (operand_equal_p (arg00, arg11, 0))
7089     same = arg00, alt0 = arg01, alt1 = arg10;
7090   else if (operand_equal_p (arg01, arg10, 0))
7091     same = arg01, alt0 = arg00, alt1 = arg11;
7092
7093   /* No identical multiplicands; see if we can find a common
7094      power-of-two factor in non-power-of-two multiplies.  This
7095      can help in multi-dimensional array access.  */
7096   else if (host_integerp (arg01, 0)
7097            && host_integerp (arg11, 0))
7098     {
7099       HOST_WIDE_INT int01, int11, tmp;
7100       bool swap = false;
7101       tree maybe_same;
7102       int01 = TREE_INT_CST_LOW (arg01);
7103       int11 = TREE_INT_CST_LOW (arg11);
7104
7105       /* Move min of absolute values to int11.  */
7106       if ((int01 >= 0 ? int01 : -int01)
7107           < (int11 >= 0 ? int11 : -int11))
7108         {
7109           tmp = int01, int01 = int11, int11 = tmp;
7110           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7111           maybe_same = arg01;
7112           swap = true;
7113         }
7114       else
7115         maybe_same = arg11;
7116
7117       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
7118         {
7119           alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7120                               build_int_cst (TREE_TYPE (arg00),
7121                                              int01 / int11));
7122           alt1 = arg10;
7123           same = maybe_same;
7124           if (swap)
7125             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7126         }
7127     }
7128
7129   if (same)
7130     return fold_build2 (MULT_EXPR, type,
7131                         fold_build2 (code, type,
7132                                      fold_convert (type, alt0),
7133                                      fold_convert (type, alt1)),
7134                         fold_convert (type, same));
7135
7136   return NULL_TREE;
7137 }
7138
7139 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7140    specified by EXPR into the buffer PTR of length LEN bytes.
7141    Return the number of bytes placed in the buffer, or zero
7142    upon failure.  */
7143
7144 static int
7145 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7146 {
7147   tree type = TREE_TYPE (expr);
7148   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7149   int byte, offset, word, words;
7150   unsigned char value;
7151
7152   if (total_bytes > len)
7153     return 0;
7154   words = total_bytes / UNITS_PER_WORD;
7155
7156   for (byte = 0; byte < total_bytes; byte++)
7157     {
7158       int bitpos = byte * BITS_PER_UNIT;
7159       if (bitpos < HOST_BITS_PER_WIDE_INT)
7160         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7161       else
7162         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7163                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7164
7165       if (total_bytes > UNITS_PER_WORD)
7166         {
7167           word = byte / UNITS_PER_WORD;
7168           if (WORDS_BIG_ENDIAN)
7169             word = (words - 1) - word;
7170           offset = word * UNITS_PER_WORD;
7171           if (BYTES_BIG_ENDIAN)
7172             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7173           else
7174             offset += byte % UNITS_PER_WORD;
7175         }
7176       else
7177         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7178       ptr[offset] = value;
7179     }
7180   return total_bytes;
7181 }
7182
7183
7184 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7185    specified by EXPR into the buffer PTR of length LEN bytes.
7186    Return the number of bytes placed in the buffer, or zero
7187    upon failure.  */
7188
7189 static int
7190 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7191 {
7192   tree type = TREE_TYPE (expr);
7193   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7194   int byte, offset, word, words, bitpos;
7195   unsigned char value;
7196
7197   /* There are always 32 bits in each long, no matter the size of
7198      the hosts long.  We handle floating point representations with
7199      up to 192 bits.  */
7200   long tmp[6];
7201
7202   if (total_bytes > len)
7203     return 0;
7204   words = 32 / UNITS_PER_WORD;
7205
7206   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7207
7208   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7209        bitpos += BITS_PER_UNIT)
7210     {
7211       byte = (bitpos / BITS_PER_UNIT) & 3;
7212       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7213
7214       if (UNITS_PER_WORD < 4)
7215         {
7216           word = byte / UNITS_PER_WORD;
7217           if (WORDS_BIG_ENDIAN)
7218             word = (words - 1) - word;
7219           offset = word * UNITS_PER_WORD;
7220           if (BYTES_BIG_ENDIAN)
7221             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7222           else
7223             offset += byte % UNITS_PER_WORD;
7224         }
7225       else
7226         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7227       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7228     }
7229   return total_bytes;
7230 }
7231
7232 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7233    specified by EXPR into the buffer PTR of length LEN bytes.
7234    Return the number of bytes placed in the buffer, or zero
7235    upon failure.  */
7236
7237 static int
7238 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7239 {
7240   int rsize, isize;
7241   tree part;
7242
7243   part = TREE_REALPART (expr);
7244   rsize = native_encode_expr (part, ptr, len);
7245   if (rsize == 0)
7246     return 0;
7247   part = TREE_IMAGPART (expr);
7248   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7249   if (isize != rsize)
7250     return 0;
7251   return rsize + isize;
7252 }
7253
7254
7255 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7256    specified by EXPR into the buffer PTR of length LEN bytes.
7257    Return the number of bytes placed in the buffer, or zero
7258    upon failure.  */
7259
7260 static int
7261 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7262 {
7263   int i, size, offset, count;
7264   tree itype, elem, elements;
7265
7266   offset = 0;
7267   elements = TREE_VECTOR_CST_ELTS (expr);
7268   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7269   itype = TREE_TYPE (TREE_TYPE (expr));
7270   size = GET_MODE_SIZE (TYPE_MODE (itype));
7271   for (i = 0; i < count; i++)
7272     {
7273       if (elements)
7274         {
7275           elem = TREE_VALUE (elements);
7276           elements = TREE_CHAIN (elements);
7277         }
7278       else
7279         elem = NULL_TREE;
7280
7281       if (elem)
7282         {
7283           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7284             return 0;
7285         }
7286       else
7287         {
7288           if (offset + size > len)
7289             return 0;
7290           memset (ptr+offset, 0, size);
7291         }
7292       offset += size;
7293     }
7294   return offset;
7295 }
7296
7297
7298 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7299    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7300    buffer PTR of length LEN bytes.  Return the number of bytes
7301    placed in the buffer, or zero upon failure.  */
7302
7303 int
7304 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7305 {
7306   switch (TREE_CODE (expr))
7307     {
7308     case INTEGER_CST:
7309       return native_encode_int (expr, ptr, len);
7310
7311     case REAL_CST:
7312       return native_encode_real (expr, ptr, len);
7313
7314     case COMPLEX_CST:
7315       return native_encode_complex (expr, ptr, len);
7316
7317     case VECTOR_CST:
7318       return native_encode_vector (expr, ptr, len);
7319
7320     default:
7321       return 0;
7322     }
7323 }
7324
7325
7326 /* Subroutine of native_interpret_expr.  Interpret the contents of
7327    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7328    If the buffer cannot be interpreted, return NULL_TREE.  */
7329
7330 static tree
7331 native_interpret_int (tree type, const unsigned char *ptr, int len)
7332 {
7333   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7334   int byte, offset, word, words;
7335   unsigned char value;
7336   unsigned int HOST_WIDE_INT lo = 0;
7337   HOST_WIDE_INT hi = 0;
7338
7339   if (total_bytes > len)
7340     return NULL_TREE;
7341   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7342     return NULL_TREE;
7343   words = total_bytes / UNITS_PER_WORD;
7344
7345   for (byte = 0; byte < total_bytes; byte++)
7346     {
7347       int bitpos = byte * BITS_PER_UNIT;
7348       if (total_bytes > UNITS_PER_WORD)
7349         {
7350           word = byte / UNITS_PER_WORD;
7351           if (WORDS_BIG_ENDIAN)
7352             word = (words - 1) - word;
7353           offset = word * UNITS_PER_WORD;
7354           if (BYTES_BIG_ENDIAN)
7355             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7356           else
7357             offset += byte % UNITS_PER_WORD;
7358         }
7359       else
7360         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7361       value = ptr[offset];
7362
7363       if (bitpos < HOST_BITS_PER_WIDE_INT)
7364         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7365       else
7366         hi |= (unsigned HOST_WIDE_INT) value
7367               << (bitpos - HOST_BITS_PER_WIDE_INT);
7368     }
7369
7370   return build_int_cst_wide_type (type, lo, hi);
7371 }
7372
7373
7374 /* Subroutine of native_interpret_expr.  Interpret the contents of
7375    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7376    If the buffer cannot be interpreted, return NULL_TREE.  */
7377
7378 static tree
7379 native_interpret_real (tree type, const unsigned char *ptr, int len)
7380 {
7381   enum machine_mode mode = TYPE_MODE (type);
7382   int total_bytes = GET_MODE_SIZE (mode);
7383   int byte, offset, word, words, bitpos;
7384   unsigned char value;
7385   /* There are always 32 bits in each long, no matter the size of
7386      the hosts long.  We handle floating point representations with
7387      up to 192 bits.  */
7388   REAL_VALUE_TYPE r;
7389   long tmp[6];
7390
7391   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7392   if (total_bytes > len || total_bytes > 24)
7393     return NULL_TREE;
7394   words = 32 / UNITS_PER_WORD;
7395
7396   memset (tmp, 0, sizeof (tmp));
7397   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7398        bitpos += BITS_PER_UNIT)
7399     {
7400       byte = (bitpos / BITS_PER_UNIT) & 3;
7401       if (UNITS_PER_WORD < 4)
7402         {
7403           word = byte / UNITS_PER_WORD;
7404           if (WORDS_BIG_ENDIAN)
7405             word = (words - 1) - word;
7406           offset = word * UNITS_PER_WORD;
7407           if (BYTES_BIG_ENDIAN)
7408             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7409           else
7410             offset += byte % UNITS_PER_WORD;
7411         }
7412       else
7413         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7414       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7415
7416       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7417     }
7418
7419   real_from_target (&r, tmp, mode);
7420   return build_real (type, r);
7421 }
7422
7423
7424 /* Subroutine of native_interpret_expr.  Interpret the contents of
7425    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7426    If the buffer cannot be interpreted, return NULL_TREE.  */
7427
7428 static tree
7429 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7430 {
7431   tree etype, rpart, ipart;
7432   int size;
7433
7434   etype = TREE_TYPE (type);
7435   size = GET_MODE_SIZE (TYPE_MODE (etype));
7436   if (size * 2 > len)
7437     return NULL_TREE;
7438   rpart = native_interpret_expr (etype, ptr, size);
7439   if (!rpart)
7440     return NULL_TREE;
7441   ipart = native_interpret_expr (etype, ptr+size, size);
7442   if (!ipart)
7443     return NULL_TREE;
7444   return build_complex (type, rpart, ipart);
7445 }
7446
7447
7448 /* Subroutine of native_interpret_expr.  Interpret the contents of
7449    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7450    If the buffer cannot be interpreted, return NULL_TREE.  */
7451
7452 static tree
7453 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7454 {
7455   tree etype, elem, elements;
7456   int i, size, count;
7457
7458   etype = TREE_TYPE (type);
7459   size = GET_MODE_SIZE (TYPE_MODE (etype));
7460   count = TYPE_VECTOR_SUBPARTS (type);
7461   if (size * count > len)
7462     return NULL_TREE;
7463
7464   elements = NULL_TREE;
7465   for (i = count - 1; i >= 0; i--)
7466     {
7467       elem = native_interpret_expr (etype, ptr+(i*size), size);
7468       if (!elem)
7469         return NULL_TREE;
7470       elements = tree_cons (NULL_TREE, elem, elements);
7471     }
7472   return build_vector (type, elements);
7473 }
7474
7475
7476 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7477    the buffer PTR of length LEN as a constant of type TYPE.  For
7478    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7479    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7480    return NULL_TREE.  */
7481
7482 tree
7483 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7484 {
7485   switch (TREE_CODE (type))
7486     {
7487     case INTEGER_TYPE:
7488     case ENUMERAL_TYPE:
7489     case BOOLEAN_TYPE:
7490       return native_interpret_int (type, ptr, len);
7491
7492     case REAL_TYPE:
7493       return native_interpret_real (type, ptr, len);
7494
7495     case COMPLEX_TYPE:
7496       return native_interpret_complex (type, ptr, len);
7497
7498     case VECTOR_TYPE:
7499       return native_interpret_vector (type, ptr, len);
7500
7501     default:
7502       return NULL_TREE;
7503     }
7504 }
7505
7506
7507 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7508    TYPE at compile-time.  If we're unable to perform the conversion
7509    return NULL_TREE.  */
7510
7511 static tree
7512 fold_view_convert_expr (tree type, tree expr)
7513 {
7514   /* We support up to 512-bit values (for V8DFmode).  */
7515   unsigned char buffer[64];
7516   int len;
7517
7518   /* Check that the host and target are sane.  */
7519   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7520     return NULL_TREE;
7521
7522   len = native_encode_expr (expr, buffer, sizeof (buffer));
7523   if (len == 0)
7524     return NULL_TREE;
7525
7526   return native_interpret_expr (type, buffer, len);
7527 }
7528
7529 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7530    to avoid confusing the gimplify process.  When IN_FOLD is true
7531    avoid modifications of T.  */
7532
7533 static tree
7534 build_fold_addr_expr_with_type_1 (tree t, tree ptrtype, bool in_fold)
7535 {
7536   /* The size of the object is not relevant when talking about its address.  */
7537   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7538     t = TREE_OPERAND (t, 0);
7539
7540   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7541   if (TREE_CODE (t) == INDIRECT_REF
7542       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7543     {
7544       t = TREE_OPERAND (t, 0);
7545
7546       if (TREE_TYPE (t) != ptrtype)
7547         t = build1 (NOP_EXPR, ptrtype, t);
7548     }
7549   else if (!in_fold)
7550     {
7551       tree base = t;
7552
7553       while (handled_component_p (base))
7554         base = TREE_OPERAND (base, 0);
7555
7556       if (DECL_P (base))
7557         TREE_ADDRESSABLE (base) = 1;
7558
7559       t = build1 (ADDR_EXPR, ptrtype, t);
7560     }
7561   else
7562     t = build1 (ADDR_EXPR, ptrtype, t);
7563
7564   return t;
7565 }
7566
7567 /* Build an expression for the address of T with type PTRTYPE.  This
7568    function modifies the input parameter 'T' by sometimes setting the
7569    TREE_ADDRESSABLE flag.  */
7570
7571 tree
7572 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7573 {
7574   return build_fold_addr_expr_with_type_1 (t, ptrtype, false);
7575 }
7576
7577 /* Build an expression for the address of T.  This function modifies
7578    the input parameter 'T' by sometimes setting the TREE_ADDRESSABLE
7579    flag.  When called from fold functions, use fold_addr_expr instead.  */
7580
7581 tree
7582 build_fold_addr_expr (tree t)
7583 {
7584   return build_fold_addr_expr_with_type_1 (t, 
7585                                            build_pointer_type (TREE_TYPE (t)),
7586                                            false);
7587 }
7588
7589 /* Same as build_fold_addr_expr, builds an expression for the address
7590    of T, but avoids touching the input node 't'.  Fold functions
7591    should use this version.  */
7592
7593 static tree
7594 fold_addr_expr (tree t)
7595 {
7596   tree ptrtype = build_pointer_type (TREE_TYPE (t));
7597
7598   return build_fold_addr_expr_with_type_1 (t, ptrtype, true);
7599 }
7600
7601 /* Fold a unary expression of code CODE and type TYPE with operand
7602    OP0.  Return the folded expression if folding is successful.
7603    Otherwise, return NULL_TREE.  */
7604
7605 tree
7606 fold_unary (enum tree_code code, tree type, tree op0)
7607 {
7608   tree tem;
7609   tree arg0;
7610   enum tree_code_class kind = TREE_CODE_CLASS (code);
7611
7612   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7613               && TREE_CODE_LENGTH (code) == 1);
7614
7615   arg0 = op0;
7616   if (arg0)
7617     {
7618       if (code == NOP_EXPR || code == CONVERT_EXPR
7619           || code == FLOAT_EXPR || code == ABS_EXPR)
7620         {
7621           /* Don't use STRIP_NOPS, because signedness of argument type
7622              matters.  */
7623           STRIP_SIGN_NOPS (arg0);
7624         }
7625       else
7626         {
7627           /* Strip any conversions that don't change the mode.  This
7628              is safe for every expression, except for a comparison
7629              expression because its signedness is derived from its
7630              operands.
7631
7632              Note that this is done as an internal manipulation within
7633              the constant folder, in order to find the simplest
7634              representation of the arguments so that their form can be
7635              studied.  In any cases, the appropriate type conversions
7636              should be put back in the tree that will get out of the
7637              constant folder.  */
7638           STRIP_NOPS (arg0);
7639         }
7640     }
7641
7642   if (TREE_CODE_CLASS (code) == tcc_unary)
7643     {
7644       if (TREE_CODE (arg0) == COMPOUND_EXPR)
7645         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7646                        fold_build1 (code, type, TREE_OPERAND (arg0, 1)));
7647       else if (TREE_CODE (arg0) == COND_EXPR)
7648         {
7649           tree arg01 = TREE_OPERAND (arg0, 1);
7650           tree arg02 = TREE_OPERAND (arg0, 2);
7651           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7652             arg01 = fold_build1 (code, type, arg01);
7653           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7654             arg02 = fold_build1 (code, type, arg02);
7655           tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
7656                              arg01, arg02);
7657
7658           /* If this was a conversion, and all we did was to move into
7659              inside the COND_EXPR, bring it back out.  But leave it if
7660              it is a conversion from integer to integer and the
7661              result precision is no wider than a word since such a
7662              conversion is cheap and may be optimized away by combine,
7663              while it couldn't if it were outside the COND_EXPR.  Then return
7664              so we don't get into an infinite recursion loop taking the
7665              conversion out and then back in.  */
7666
7667           if ((code == NOP_EXPR || code == CONVERT_EXPR
7668                || code == NON_LVALUE_EXPR)
7669               && TREE_CODE (tem) == COND_EXPR
7670               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7671               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7672               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7673               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7674               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7675                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7676               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7677                      && (INTEGRAL_TYPE_P
7678                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7679                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7680                   || flag_syntax_only))
7681             tem = build1 (code, type,
7682                           build3 (COND_EXPR,
7683                                   TREE_TYPE (TREE_OPERAND
7684                                              (TREE_OPERAND (tem, 1), 0)),
7685                                   TREE_OPERAND (tem, 0),
7686                                   TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7687                                   TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
7688           return tem;
7689         }
7690       else if (COMPARISON_CLASS_P (arg0))
7691         {
7692           if (TREE_CODE (type) == BOOLEAN_TYPE)
7693             {
7694               arg0 = copy_node (arg0);
7695               TREE_TYPE (arg0) = type;
7696               return arg0;
7697             }
7698           else if (TREE_CODE (type) != INTEGER_TYPE)
7699             return fold_build3 (COND_EXPR, type, arg0,
7700                                 fold_build1 (code, type,
7701                                              integer_one_node),
7702                                 fold_build1 (code, type,
7703                                              integer_zero_node));
7704         }
7705    }
7706
7707   switch (code)
7708     {
7709     case PAREN_EXPR:
7710       /* Re-association barriers around constants and other re-association
7711          barriers can be removed.  */
7712       if (CONSTANT_CLASS_P (op0)
7713           || TREE_CODE (op0) == PAREN_EXPR)
7714         return fold_convert (type, op0);
7715       return NULL_TREE;
7716
7717     case NOP_EXPR:
7718     case FLOAT_EXPR:
7719     case CONVERT_EXPR:
7720     case FIX_TRUNC_EXPR:
7721       if (TREE_TYPE (op0) == type)
7722         return op0;
7723       
7724       /* If we have (type) (a CMP b) and type is an integral type, return
7725          new expression involving the new type.  */
7726       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7727         return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7728                             TREE_OPERAND (op0, 1));
7729
7730       /* Handle cases of two conversions in a row.  */
7731       if (TREE_CODE (op0) == NOP_EXPR
7732           || TREE_CODE (op0) == CONVERT_EXPR)
7733         {
7734           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7735           tree inter_type = TREE_TYPE (op0);
7736           int inside_int = INTEGRAL_TYPE_P (inside_type);
7737           int inside_ptr = POINTER_TYPE_P (inside_type);
7738           int inside_float = FLOAT_TYPE_P (inside_type);
7739           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7740           unsigned int inside_prec = TYPE_PRECISION (inside_type);
7741           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7742           int inter_int = INTEGRAL_TYPE_P (inter_type);
7743           int inter_ptr = POINTER_TYPE_P (inter_type);
7744           int inter_float = FLOAT_TYPE_P (inter_type);
7745           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7746           unsigned int inter_prec = TYPE_PRECISION (inter_type);
7747           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7748           int final_int = INTEGRAL_TYPE_P (type);
7749           int final_ptr = POINTER_TYPE_P (type);
7750           int final_float = FLOAT_TYPE_P (type);
7751           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7752           unsigned int final_prec = TYPE_PRECISION (type);
7753           int final_unsignedp = TYPE_UNSIGNED (type);
7754
7755           /* In addition to the cases of two conversions in a row
7756              handled below, if we are converting something to its own
7757              type via an object of identical or wider precision, neither
7758              conversion is needed.  */
7759           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7760               && (((inter_int || inter_ptr) && final_int)
7761                   || (inter_float && final_float))
7762               && inter_prec >= final_prec)
7763             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7764
7765           /* Likewise, if the intermediate and final types are either both
7766              float or both integer, we don't need the middle conversion if
7767              it is wider than the final type and doesn't change the signedness
7768              (for integers).  Avoid this if the final type is a pointer
7769              since then we sometimes need the inner conversion.  Likewise if
7770              the outer has a precision not equal to the size of its mode.  */
7771           if (((inter_int && inside_int)
7772                || (inter_float && inside_float)
7773                || (inter_vec && inside_vec))
7774               && inter_prec >= inside_prec
7775               && (inter_float || inter_vec
7776                   || inter_unsignedp == inside_unsignedp)
7777               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7778                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
7779               && ! final_ptr
7780               && (! final_vec || inter_prec == inside_prec))
7781             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7782
7783           /* If we have a sign-extension of a zero-extended value, we can
7784              replace that by a single zero-extension.  */
7785           if (inside_int && inter_int && final_int
7786               && inside_prec < inter_prec && inter_prec < final_prec
7787               && inside_unsignedp && !inter_unsignedp)
7788             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7789
7790           /* Two conversions in a row are not needed unless:
7791              - some conversion is floating-point (overstrict for now), or
7792              - some conversion is a vector (overstrict for now), or
7793              - the intermediate type is narrower than both initial and
7794                final, or
7795              - the intermediate type and innermost type differ in signedness,
7796                and the outermost type is wider than the intermediate, or
7797              - the initial type is a pointer type and the precisions of the
7798                intermediate and final types differ, or
7799              - the final type is a pointer type and the precisions of the
7800                initial and intermediate types differ.  */
7801           if (! inside_float && ! inter_float && ! final_float
7802               && ! inside_vec && ! inter_vec && ! final_vec
7803               && (inter_prec >= inside_prec || inter_prec >= final_prec)
7804               && ! (inside_int && inter_int
7805                     && inter_unsignedp != inside_unsignedp
7806                     && inter_prec < final_prec)
7807               && ((inter_unsignedp && inter_prec > inside_prec)
7808                   == (final_unsignedp && final_prec > inter_prec))
7809               && ! (inside_ptr && inter_prec != final_prec)
7810               && ! (final_ptr && inside_prec != inter_prec)
7811               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7812                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7813             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7814         }
7815
7816       /* Handle (T *)&A.B.C for A being of type T and B and C
7817          living at offset zero.  This occurs frequently in
7818          C++ upcasting and then accessing the base.  */
7819       if (TREE_CODE (op0) == ADDR_EXPR
7820           && POINTER_TYPE_P (type)
7821           && handled_component_p (TREE_OPERAND (op0, 0)))
7822         {
7823           HOST_WIDE_INT bitsize, bitpos;
7824           tree offset;
7825           enum machine_mode mode;
7826           int unsignedp, volatilep;
7827           tree base = TREE_OPERAND (op0, 0);
7828           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7829                                       &mode, &unsignedp, &volatilep, false);
7830           /* If the reference was to a (constant) zero offset, we can use
7831              the address of the base if it has the same base type
7832              as the result type.  */
7833           if (! offset && bitpos == 0
7834               && TYPE_MAIN_VARIANT (TREE_TYPE (type))
7835                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7836             return fold_convert (type, fold_addr_expr (base));
7837         }
7838
7839       if ((TREE_CODE (op0) == MODIFY_EXPR
7840            || TREE_CODE (op0) == GIMPLE_MODIFY_STMT)
7841           && TREE_CONSTANT (GENERIC_TREE_OPERAND (op0, 1))
7842           /* Detect assigning a bitfield.  */
7843           && !(TREE_CODE (GENERIC_TREE_OPERAND (op0, 0)) == COMPONENT_REF
7844                && DECL_BIT_FIELD
7845                (TREE_OPERAND (GENERIC_TREE_OPERAND (op0, 0), 1))))
7846         {
7847           /* Don't leave an assignment inside a conversion
7848              unless assigning a bitfield.  */
7849           tem = fold_build1 (code, type, GENERIC_TREE_OPERAND (op0, 1));
7850           /* First do the assignment, then return converted constant.  */
7851           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7852           TREE_NO_WARNING (tem) = 1;
7853           TREE_USED (tem) = 1;
7854           return tem;
7855         }
7856
7857       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7858          constants (if x has signed type, the sign bit cannot be set
7859          in c).  This folds extension into the BIT_AND_EXPR.  */
7860       if (INTEGRAL_TYPE_P (type)
7861           && TREE_CODE (type) != BOOLEAN_TYPE
7862           && TREE_CODE (op0) == BIT_AND_EXPR
7863           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7864         {
7865           tree and = op0;
7866           tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
7867           int change = 0;
7868
7869           if (TYPE_UNSIGNED (TREE_TYPE (and))
7870               || (TYPE_PRECISION (type)
7871                   <= TYPE_PRECISION (TREE_TYPE (and))))
7872             change = 1;
7873           else if (TYPE_PRECISION (TREE_TYPE (and1))
7874                    <= HOST_BITS_PER_WIDE_INT
7875                    && host_integerp (and1, 1))
7876             {
7877               unsigned HOST_WIDE_INT cst;
7878
7879               cst = tree_low_cst (and1, 1);
7880               cst &= (HOST_WIDE_INT) -1
7881                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7882               change = (cst == 0);
7883 #ifdef LOAD_EXTEND_OP
7884               if (change
7885                   && !flag_syntax_only
7886                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7887                       == ZERO_EXTEND))
7888                 {
7889                   tree uns = unsigned_type_for (TREE_TYPE (and0));
7890                   and0 = fold_convert (uns, and0);
7891                   and1 = fold_convert (uns, and1);
7892                 }
7893 #endif
7894             }
7895           if (change)
7896             {
7897               tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
7898                                            TREE_INT_CST_HIGH (and1), 0,
7899                                            TREE_OVERFLOW (and1));
7900               return fold_build2 (BIT_AND_EXPR, type,
7901                                   fold_convert (type, and0), tem);
7902             }
7903         }
7904
7905       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7906          when one of the new casts will fold away. Conservatively we assume
7907          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7908       if (POINTER_TYPE_P (type)
7909           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7910           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7911               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7912               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7913         {
7914           tree arg00 = TREE_OPERAND (arg0, 0);
7915           tree arg01 = TREE_OPERAND (arg0, 1);
7916
7917           return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
7918                               fold_convert (sizetype, arg01));
7919         }
7920
7921       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7922          of the same precision, and X is an integer type not narrower than
7923          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
7924       if (INTEGRAL_TYPE_P (type)
7925           && TREE_CODE (op0) == BIT_NOT_EXPR
7926           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7927           && (TREE_CODE (TREE_OPERAND (op0, 0)) == NOP_EXPR
7928               || TREE_CODE (TREE_OPERAND (op0, 0)) == CONVERT_EXPR)
7929           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7930         {
7931           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7932           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7933               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7934             return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
7935         }
7936
7937       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7938          type of X and Y (integer types only).  */
7939       if (INTEGRAL_TYPE_P (type)
7940           && TREE_CODE (op0) == MULT_EXPR
7941           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7942           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7943         {
7944           /* Be careful not to introduce new overflows.  */
7945           tree mult_type;
7946           if (TYPE_OVERFLOW_WRAPS (type))
7947             mult_type = type;
7948           else
7949             mult_type = unsigned_type_for (type);
7950           
7951           tem = fold_build2 (MULT_EXPR, mult_type,
7952                              fold_convert (mult_type, TREE_OPERAND (op0, 0)),
7953                              fold_convert (mult_type, TREE_OPERAND (op0, 1)));
7954           return fold_convert (type, tem);
7955         }
7956
7957       tem = fold_convert_const (code, type, op0);
7958       return tem ? tem : NULL_TREE;
7959
7960     case FIXED_CONVERT_EXPR:
7961       tem = fold_convert_const (code, type, arg0);
7962       return tem ? tem : NULL_TREE;
7963
7964     case VIEW_CONVERT_EXPR:
7965       if (TREE_TYPE (op0) == type)
7966         return op0;
7967       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
7968         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
7969
7970       /* For integral conversions with the same precision or pointer
7971          conversions use a NOP_EXPR instead.  */
7972       if ((INTEGRAL_TYPE_P (type)
7973            || POINTER_TYPE_P (type))
7974           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7975               || POINTER_TYPE_P (TREE_TYPE (op0)))
7976           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))
7977           /* Do not muck with VIEW_CONVERT_EXPRs that convert from
7978              a sub-type to its base type as generated by the Ada FE.  */
7979           && !(INTEGRAL_TYPE_P (TREE_TYPE (op0))
7980                && TREE_TYPE (TREE_TYPE (op0))))
7981         return fold_convert (type, op0);
7982
7983       /* Strip inner integral conversions that do not change the precision.  */
7984       if ((TREE_CODE (op0) == NOP_EXPR
7985            || TREE_CODE (op0) == CONVERT_EXPR)
7986           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7987               || POINTER_TYPE_P (TREE_TYPE (op0)))
7988           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
7989               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
7990           && (TYPE_PRECISION (TREE_TYPE (op0))
7991               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
7992         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
7993
7994       return fold_view_convert_expr (type, op0);
7995
7996     case NEGATE_EXPR:
7997       tem = fold_negate_expr (arg0);
7998       if (tem)
7999         return fold_convert (type, tem);
8000       return NULL_TREE;
8001
8002     case ABS_EXPR:
8003       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8004         return fold_abs_const (arg0, type);
8005       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8006         return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8007       /* Convert fabs((double)float) into (double)fabsf(float).  */
8008       else if (TREE_CODE (arg0) == NOP_EXPR
8009                && TREE_CODE (type) == REAL_TYPE)
8010         {
8011           tree targ0 = strip_float_extensions (arg0);
8012           if (targ0 != arg0)
8013             return fold_convert (type, fold_build1 (ABS_EXPR,
8014                                                     TREE_TYPE (targ0),
8015                                                     targ0));
8016         }
8017       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8018       else if (TREE_CODE (arg0) == ABS_EXPR)
8019         return arg0;
8020       else if (tree_expr_nonnegative_p (arg0))
8021         return arg0;
8022
8023       /* Strip sign ops from argument.  */
8024       if (TREE_CODE (type) == REAL_TYPE)
8025         {
8026           tem = fold_strip_sign_ops (arg0);
8027           if (tem)
8028             return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8029         }
8030       return NULL_TREE;
8031
8032     case CONJ_EXPR:
8033       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8034         return fold_convert (type, arg0);
8035       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8036         {
8037           tree itype = TREE_TYPE (type);
8038           tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8039           tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8040           return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8041         }
8042       if (TREE_CODE (arg0) == COMPLEX_CST)
8043         {
8044           tree itype = TREE_TYPE (type);
8045           tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8046           tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8047           return build_complex (type, rpart, negate_expr (ipart));
8048         }
8049       if (TREE_CODE (arg0) == CONJ_EXPR)
8050         return fold_convert (type, TREE_OPERAND (arg0, 0));
8051       return NULL_TREE;
8052
8053     case BIT_NOT_EXPR:
8054       if (TREE_CODE (arg0) == INTEGER_CST)
8055         return fold_not_const (arg0, type);
8056       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8057         return fold_convert (type, TREE_OPERAND (arg0, 0));
8058       /* Convert ~ (-A) to A - 1.  */
8059       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8060         return fold_build2 (MINUS_EXPR, type,
8061                             fold_convert (type, TREE_OPERAND (arg0, 0)),
8062                             build_int_cst (type, 1));
8063       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8064       else if (INTEGRAL_TYPE_P (type)
8065                && ((TREE_CODE (arg0) == MINUS_EXPR
8066                     && integer_onep (TREE_OPERAND (arg0, 1)))
8067                    || (TREE_CODE (arg0) == PLUS_EXPR
8068                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8069         return fold_build1 (NEGATE_EXPR, type,
8070                             fold_convert (type, TREE_OPERAND (arg0, 0)));
8071       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8072       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8073                && (tem = fold_unary (BIT_NOT_EXPR, type,
8074                                      fold_convert (type,
8075                                                    TREE_OPERAND (arg0, 0)))))
8076         return fold_build2 (BIT_XOR_EXPR, type, tem,
8077                             fold_convert (type, TREE_OPERAND (arg0, 1)));
8078       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8079                && (tem = fold_unary (BIT_NOT_EXPR, type,
8080                                      fold_convert (type,
8081                                                    TREE_OPERAND (arg0, 1)))))
8082         return fold_build2 (BIT_XOR_EXPR, type,
8083                             fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8084       /* Perform BIT_NOT_EXPR on each element individually.  */
8085       else if (TREE_CODE (arg0) == VECTOR_CST)
8086         {
8087           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8088           int count = TYPE_VECTOR_SUBPARTS (type), i;
8089
8090           for (i = 0; i < count; i++)
8091             {
8092               if (elements)
8093                 {
8094                   elem = TREE_VALUE (elements);
8095                   elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8096                   if (elem == NULL_TREE)
8097                     break;
8098                   elements = TREE_CHAIN (elements);
8099                 }
8100               else
8101                 elem = build_int_cst (TREE_TYPE (type), -1);
8102               list = tree_cons (NULL_TREE, elem, list);
8103             }
8104           if (i == count)
8105             return build_vector (type, nreverse (list));
8106         }
8107
8108       return NULL_TREE;
8109
8110     case TRUTH_NOT_EXPR:
8111       /* The argument to invert_truthvalue must have Boolean type.  */
8112       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8113           arg0 = fold_convert (boolean_type_node, arg0);
8114
8115       /* Note that the operand of this must be an int
8116          and its values must be 0 or 1.
8117          ("true" is a fixed value perhaps depending on the language,
8118          but we don't handle values other than 1 correctly yet.)  */
8119       tem = fold_truth_not_expr (arg0);
8120       if (!tem)
8121         return NULL_TREE;
8122       return fold_convert (type, tem);
8123
8124     case REALPART_EXPR:
8125       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8126         return fold_convert (type, arg0);
8127       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8128         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8129                                  TREE_OPERAND (arg0, 1));
8130       if (TREE_CODE (arg0) == COMPLEX_CST)
8131         return fold_convert (type, TREE_REALPART (arg0));
8132       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8133         {
8134           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8135           tem = fold_build2 (TREE_CODE (arg0), itype,
8136                              fold_build1 (REALPART_EXPR, itype,
8137                                           TREE_OPERAND (arg0, 0)),
8138                              fold_build1 (REALPART_EXPR, itype,
8139                                           TREE_OPERAND (arg0, 1)));
8140           return fold_convert (type, tem);
8141         }
8142       if (TREE_CODE (arg0) == CONJ_EXPR)
8143         {
8144           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8145           tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8146           return fold_convert (type, tem);
8147         }
8148       if (TREE_CODE (arg0) == CALL_EXPR)
8149         {
8150           tree fn = get_callee_fndecl (arg0);
8151           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8152             switch (DECL_FUNCTION_CODE (fn))
8153               {
8154               CASE_FLT_FN (BUILT_IN_CEXPI):
8155                 fn = mathfn_built_in (type, BUILT_IN_COS);
8156                 if (fn)
8157                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8158                 break;
8159
8160               default:
8161                 break;
8162               }
8163         }
8164       return NULL_TREE;
8165
8166     case IMAGPART_EXPR:
8167       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8168         return fold_convert (type, integer_zero_node);
8169       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8170         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8171                                  TREE_OPERAND (arg0, 0));
8172       if (TREE_CODE (arg0) == COMPLEX_CST)
8173         return fold_convert (type, TREE_IMAGPART (arg0));
8174       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8175         {
8176           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8177           tem = fold_build2 (TREE_CODE (arg0), itype,
8178                              fold_build1 (IMAGPART_EXPR, itype,
8179                                           TREE_OPERAND (arg0, 0)),
8180                              fold_build1 (IMAGPART_EXPR, itype,
8181                                           TREE_OPERAND (arg0, 1)));
8182           return fold_convert (type, tem);
8183         }
8184       if (TREE_CODE (arg0) == CONJ_EXPR)
8185         {
8186           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8187           tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8188           return fold_convert (type, negate_expr (tem));
8189         }
8190       if (TREE_CODE (arg0) == CALL_EXPR)
8191         {
8192           tree fn = get_callee_fndecl (arg0);
8193           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8194             switch (DECL_FUNCTION_CODE (fn))
8195               {
8196               CASE_FLT_FN (BUILT_IN_CEXPI):
8197                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8198                 if (fn)
8199                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8200                 break;
8201
8202               default:
8203                 break;
8204               }
8205         }
8206       return NULL_TREE;
8207
8208     default:
8209       return NULL_TREE;
8210     } /* switch (code) */
8211 }
8212
8213 /* Fold a binary expression of code CODE and type TYPE with operands
8214    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8215    Return the folded expression if folding is successful.  Otherwise,
8216    return NULL_TREE.  */
8217
8218 static tree
8219 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8220 {
8221   enum tree_code compl_code;
8222
8223   if (code == MIN_EXPR)
8224     compl_code = MAX_EXPR;
8225   else if (code == MAX_EXPR)
8226     compl_code = MIN_EXPR;
8227   else
8228     gcc_unreachable ();
8229
8230   /* MIN (MAX (a, b), b) == b.  */
8231   if (TREE_CODE (op0) == compl_code
8232       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8233     return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8234
8235   /* MIN (MAX (b, a), b) == b.  */
8236   if (TREE_CODE (op0) == compl_code
8237       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8238       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8239     return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8240
8241   /* MIN (a, MAX (a, b)) == a.  */
8242   if (TREE_CODE (op1) == compl_code
8243       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8244       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8245     return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8246
8247   /* MIN (a, MAX (b, a)) == a.  */
8248   if (TREE_CODE (op1) == compl_code
8249       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8250       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8251     return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8252
8253   return NULL_TREE;
8254 }
8255
8256 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8257    by changing CODE to reduce the magnitude of constants involved in
8258    ARG0 of the comparison.
8259    Returns a canonicalized comparison tree if a simplification was
8260    possible, otherwise returns NULL_TREE.
8261    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8262    valid if signed overflow is undefined.  */
8263
8264 static tree
8265 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8266                                  tree arg0, tree arg1,
8267                                  bool *strict_overflow_p)
8268 {
8269   enum tree_code code0 = TREE_CODE (arg0);
8270   tree t, cst0 = NULL_TREE;
8271   int sgn0;
8272   bool swap = false;
8273
8274   /* Match A +- CST code arg1 and CST code arg1.  */
8275   if (!(((code0 == MINUS_EXPR
8276           || code0 == PLUS_EXPR)
8277          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8278         || code0 == INTEGER_CST))
8279     return NULL_TREE;
8280
8281   /* Identify the constant in arg0 and its sign.  */
8282   if (code0 == INTEGER_CST)
8283     cst0 = arg0;
8284   else
8285     cst0 = TREE_OPERAND (arg0, 1);
8286   sgn0 = tree_int_cst_sgn (cst0);
8287
8288   /* Overflowed constants and zero will cause problems.  */
8289   if (integer_zerop (cst0)
8290       || TREE_OVERFLOW (cst0))
8291     return NULL_TREE;
8292
8293   /* See if we can reduce the magnitude of the constant in
8294      arg0 by changing the comparison code.  */
8295   if (code0 == INTEGER_CST)
8296     {
8297       /* CST <= arg1  ->  CST-1 < arg1.  */
8298       if (code == LE_EXPR && sgn0 == 1)
8299         code = LT_EXPR;
8300       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8301       else if (code == LT_EXPR && sgn0 == -1)
8302         code = LE_EXPR;
8303       /* CST > arg1  ->  CST-1 >= arg1.  */
8304       else if (code == GT_EXPR && sgn0 == 1)
8305         code = GE_EXPR;
8306       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8307       else if (code == GE_EXPR && sgn0 == -1)
8308         code = GT_EXPR;
8309       else
8310         return NULL_TREE;
8311       /* arg1 code' CST' might be more canonical.  */
8312       swap = true;
8313     }
8314   else
8315     {
8316       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8317       if (code == LT_EXPR
8318           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8319         code = LE_EXPR;
8320       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8321       else if (code == GT_EXPR
8322                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8323         code = GE_EXPR;
8324       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8325       else if (code == LE_EXPR
8326                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8327         code = LT_EXPR;
8328       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8329       else if (code == GE_EXPR
8330                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8331         code = GT_EXPR;
8332       else
8333         return NULL_TREE;
8334       *strict_overflow_p = true;
8335     }
8336
8337   /* Now build the constant reduced in magnitude.  */
8338   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8339                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8340   if (code0 != INTEGER_CST)
8341     t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8342
8343   /* If swapping might yield to a more canonical form, do so.  */
8344   if (swap)
8345     return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8346   else
8347     return fold_build2 (code, type, t, arg1);
8348 }
8349
8350 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8351    overflow further.  Try to decrease the magnitude of constants involved
8352    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8353    and put sole constants at the second argument position.
8354    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8355
8356 static tree
8357 maybe_canonicalize_comparison (enum tree_code code, tree type,
8358                                tree arg0, tree arg1)
8359 {
8360   tree t;
8361   bool strict_overflow_p;
8362   const char * const warnmsg = G_("assuming signed overflow does not occur "
8363                                   "when reducing constant in comparison");
8364
8365   /* In principle pointers also have undefined overflow behavior,
8366      but that causes problems elsewhere.  */
8367   if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8368       || POINTER_TYPE_P (TREE_TYPE (arg0)))
8369     return NULL_TREE;
8370
8371   /* Try canonicalization by simplifying arg0.  */
8372   strict_overflow_p = false;
8373   t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8374                                        &strict_overflow_p);
8375   if (t)
8376     {
8377       if (strict_overflow_p)
8378         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8379       return t;
8380     }
8381
8382   /* Try canonicalization by simplifying arg1 using the swapped
8383      comparison.  */
8384   code = swap_tree_comparison (code);
8385   strict_overflow_p = false;
8386   t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8387                                        &strict_overflow_p);
8388   if (t && strict_overflow_p)
8389     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8390   return t;
8391 }
8392
8393 /* Subroutine of fold_binary.  This routine performs all of the
8394    transformations that are common to the equality/inequality
8395    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8396    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8397    fold_binary should call fold_binary.  Fold a comparison with
8398    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8399    the folded comparison or NULL_TREE.  */
8400
8401 static tree
8402 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8403 {
8404   tree arg0, arg1, tem;
8405
8406   arg0 = op0;
8407   arg1 = op1;
8408
8409   STRIP_SIGN_NOPS (arg0);
8410   STRIP_SIGN_NOPS (arg1);
8411
8412   tem = fold_relational_const (code, type, arg0, arg1);
8413   if (tem != NULL_TREE)
8414     return tem;
8415
8416   /* If one arg is a real or integer constant, put it last.  */
8417   if (tree_swap_operands_p (arg0, arg1, true))
8418     return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8419
8420   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8421   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8422       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8423           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8424           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8425       && (TREE_CODE (arg1) == INTEGER_CST
8426           && !TREE_OVERFLOW (arg1)))
8427     {
8428       tree const1 = TREE_OPERAND (arg0, 1);
8429       tree const2 = arg1;
8430       tree variable = TREE_OPERAND (arg0, 0);
8431       tree lhs;
8432       int lhs_add;
8433       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8434
8435       lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8436                          TREE_TYPE (arg1), const2, const1);
8437
8438       /* If the constant operation overflowed this can be
8439          simplified as a comparison against INT_MAX/INT_MIN.  */
8440       if (TREE_CODE (lhs) == INTEGER_CST
8441           && TREE_OVERFLOW (lhs))
8442         {
8443           int const1_sgn = tree_int_cst_sgn (const1);
8444           enum tree_code code2 = code;
8445
8446           /* Get the sign of the constant on the lhs if the
8447              operation were VARIABLE + CONST1.  */
8448           if (TREE_CODE (arg0) == MINUS_EXPR)
8449             const1_sgn = -const1_sgn;
8450
8451           /* The sign of the constant determines if we overflowed
8452              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8453              Canonicalize to the INT_MIN overflow by swapping the comparison
8454              if necessary.  */
8455           if (const1_sgn == -1)
8456             code2 = swap_tree_comparison (code);
8457
8458           /* We now can look at the canonicalized case
8459                VARIABLE + 1  CODE2  INT_MIN
8460              and decide on the result.  */
8461           if (code2 == LT_EXPR
8462               || code2 == LE_EXPR
8463               || code2 == EQ_EXPR)
8464             return omit_one_operand (type, boolean_false_node, variable);
8465           else if (code2 == NE_EXPR
8466                    || code2 == GE_EXPR
8467                    || code2 == GT_EXPR)
8468             return omit_one_operand (type, boolean_true_node, variable);
8469         }
8470
8471       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8472           && (TREE_CODE (lhs) != INTEGER_CST
8473               || !TREE_OVERFLOW (lhs)))
8474         {
8475           fold_overflow_warning (("assuming signed overflow does not occur "
8476                                   "when changing X +- C1 cmp C2 to "
8477                                   "X cmp C1 +- C2"),
8478                                  WARN_STRICT_OVERFLOW_COMPARISON);
8479           return fold_build2 (code, type, variable, lhs);
8480         }
8481     }
8482
8483   /* For comparisons of pointers we can decompose it to a compile time
8484      comparison of the base objects and the offsets into the object.
8485      This requires at least one operand being an ADDR_EXPR or a
8486      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
8487   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8488       && (TREE_CODE (arg0) == ADDR_EXPR
8489           || TREE_CODE (arg1) == ADDR_EXPR
8490           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8491           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8492     {
8493       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8494       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8495       enum machine_mode mode;
8496       int volatilep, unsignedp;
8497       bool indirect_base0 = false, indirect_base1 = false;
8498
8499       /* Get base and offset for the access.  Strip ADDR_EXPR for
8500          get_inner_reference, but put it back by stripping INDIRECT_REF
8501          off the base object if possible.  indirect_baseN will be true
8502          if baseN is not an address but refers to the object itself.  */
8503       base0 = arg0;
8504       if (TREE_CODE (arg0) == ADDR_EXPR)
8505         {
8506           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8507                                        &bitsize, &bitpos0, &offset0, &mode,
8508                                        &unsignedp, &volatilep, false);
8509           if (TREE_CODE (base0) == INDIRECT_REF)
8510             base0 = TREE_OPERAND (base0, 0);
8511           else
8512             indirect_base0 = true;
8513         }
8514       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8515         {
8516           base0 = TREE_OPERAND (arg0, 0);
8517           offset0 = TREE_OPERAND (arg0, 1);
8518         }
8519
8520       base1 = arg1;
8521       if (TREE_CODE (arg1) == ADDR_EXPR)
8522         {
8523           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8524                                        &bitsize, &bitpos1, &offset1, &mode,
8525                                        &unsignedp, &volatilep, false);
8526           if (TREE_CODE (base1) == INDIRECT_REF)
8527             base1 = TREE_OPERAND (base1, 0);
8528           else
8529             indirect_base1 = true;
8530         }
8531       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8532         {
8533           base1 = TREE_OPERAND (arg1, 0);
8534           offset1 = TREE_OPERAND (arg1, 1);
8535         }
8536
8537       /* If we have equivalent bases we might be able to simplify.  */
8538       if (indirect_base0 == indirect_base1
8539           && operand_equal_p (base0, base1, 0))
8540         {
8541           /* We can fold this expression to a constant if the non-constant
8542              offset parts are equal.  */
8543           if (offset0 == offset1
8544               || (offset0 && offset1
8545                   && operand_equal_p (offset0, offset1, 0)))
8546             {
8547               switch (code)
8548                 {
8549                 case EQ_EXPR:
8550                   return build_int_cst (boolean_type_node, bitpos0 == bitpos1);
8551                 case NE_EXPR:
8552                   return build_int_cst (boolean_type_node, bitpos0 != bitpos1);
8553                 case LT_EXPR:
8554                   return build_int_cst (boolean_type_node, bitpos0 < bitpos1);
8555                 case LE_EXPR:
8556                   return build_int_cst (boolean_type_node, bitpos0 <= bitpos1);
8557                 case GE_EXPR:
8558                   return build_int_cst (boolean_type_node, bitpos0 >= bitpos1);
8559                 case GT_EXPR:
8560                   return build_int_cst (boolean_type_node, bitpos0 > bitpos1);
8561                 default:;
8562                 }
8563             }
8564           /* We can simplify the comparison to a comparison of the variable
8565              offset parts if the constant offset parts are equal.
8566              Be careful to use signed size type here because otherwise we
8567              mess with array offsets in the wrong way.  This is possible
8568              because pointer arithmetic is restricted to retain within an
8569              object and overflow on pointer differences is undefined as of
8570              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
8571           else if (bitpos0 == bitpos1
8572                    && ((code == EQ_EXPR || code == NE_EXPR)
8573                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
8574             {
8575               tree signed_size_type_node;
8576               signed_size_type_node = signed_type_for (size_type_node);
8577
8578               /* By converting to signed size type we cover middle-end pointer
8579                  arithmetic which operates on unsigned pointer types of size
8580                  type size and ARRAY_REF offsets which are properly sign or
8581                  zero extended from their type in case it is narrower than
8582                  size type.  */
8583               if (offset0 == NULL_TREE)
8584                 offset0 = build_int_cst (signed_size_type_node, 0);
8585               else
8586                 offset0 = fold_convert (signed_size_type_node, offset0);
8587               if (offset1 == NULL_TREE)
8588                 offset1 = build_int_cst (signed_size_type_node, 0);
8589               else
8590                 offset1 = fold_convert (signed_size_type_node, offset1);
8591
8592               if (code != EQ_EXPR && code != NE_EXPR)
8593                 fold_overflow_warning (("assuming pointer wraparound does not "
8594                                         "occur when comparing P +- C1 with "
8595                                         "P +- C2"),
8596                                        WARN_STRICT_OVERFLOW_COMPARISON);
8597
8598               return fold_build2 (code, type, offset0, offset1);
8599             }
8600         }
8601       /* For non-equal bases we can simplify if they are addresses
8602          of local binding decls or constants.  */
8603       else if (indirect_base0 && indirect_base1
8604                /* We know that !operand_equal_p (base0, base1, 0)
8605                   because the if condition was false.  But make
8606                   sure two decls are not the same.  */
8607                && base0 != base1
8608                && TREE_CODE (arg0) == ADDR_EXPR
8609                && TREE_CODE (arg1) == ADDR_EXPR
8610                && (((TREE_CODE (base0) == VAR_DECL
8611                      || TREE_CODE (base0) == PARM_DECL)
8612                     && (targetm.binds_local_p (base0)
8613                         || CONSTANT_CLASS_P (base1)))
8614                    || CONSTANT_CLASS_P (base0))
8615                && (((TREE_CODE (base1) == VAR_DECL
8616                      || TREE_CODE (base1) == PARM_DECL)
8617                     && (targetm.binds_local_p (base1)
8618                         || CONSTANT_CLASS_P (base0)))
8619                    || CONSTANT_CLASS_P (base1)))
8620         {
8621           if (code == EQ_EXPR)
8622             return omit_two_operands (type, boolean_false_node, arg0, arg1);
8623           else if (code == NE_EXPR)
8624             return omit_two_operands (type, boolean_true_node, arg0, arg1);
8625         }
8626       /* For equal offsets we can simplify to a comparison of the
8627          base addresses.  */
8628       else if (bitpos0 == bitpos1
8629                && (indirect_base0
8630                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8631                && (indirect_base1
8632                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8633                && ((offset0 == offset1)
8634                    || (offset0 && offset1
8635                        && operand_equal_p (offset0, offset1, 0))))
8636         {
8637           if (indirect_base0)
8638             base0 = fold_addr_expr (base0);
8639           if (indirect_base1)
8640             base1 = fold_addr_expr (base1);
8641           return fold_build2 (code, type, base0, base1);
8642         }
8643     }
8644
8645   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8646      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
8647      the resulting offset is smaller in absolute value than the
8648      original one.  */
8649   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8650       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8651       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8652           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8653       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8654       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8655           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8656     {
8657       tree const1 = TREE_OPERAND (arg0, 1);
8658       tree const2 = TREE_OPERAND (arg1, 1);
8659       tree variable1 = TREE_OPERAND (arg0, 0);
8660       tree variable2 = TREE_OPERAND (arg1, 0);
8661       tree cst;
8662       const char * const warnmsg = G_("assuming signed overflow does not "
8663                                       "occur when combining constants around "
8664                                       "a comparison");
8665
8666       /* Put the constant on the side where it doesn't overflow and is
8667          of lower absolute value than before.  */
8668       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8669                              ? MINUS_EXPR : PLUS_EXPR,
8670                              const2, const1, 0);
8671       if (!TREE_OVERFLOW (cst)
8672           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8673         {
8674           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8675           return fold_build2 (code, type,
8676                               variable1,
8677                               fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
8678                                            variable2, cst));
8679         }
8680
8681       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8682                              ? MINUS_EXPR : PLUS_EXPR,
8683                              const1, const2, 0);
8684       if (!TREE_OVERFLOW (cst)
8685           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8686         {
8687           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8688           return fold_build2 (code, type,
8689                               fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
8690                                            variable1, cst),
8691                               variable2);
8692         }
8693     }
8694
8695   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8696      signed arithmetic case.  That form is created by the compiler
8697      often enough for folding it to be of value.  One example is in
8698      computing loop trip counts after Operator Strength Reduction.  */
8699   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8700       && TREE_CODE (arg0) == MULT_EXPR
8701       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8702           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8703       && integer_zerop (arg1))
8704     {
8705       tree const1 = TREE_OPERAND (arg0, 1);
8706       tree const2 = arg1;                       /* zero */
8707       tree variable1 = TREE_OPERAND (arg0, 0);
8708       enum tree_code cmp_code = code;
8709
8710       gcc_assert (!integer_zerop (const1));
8711
8712       fold_overflow_warning (("assuming signed overflow does not occur when "
8713                               "eliminating multiplication in comparison "
8714                               "with zero"),
8715                              WARN_STRICT_OVERFLOW_COMPARISON);
8716
8717       /* If const1 is negative we swap the sense of the comparison.  */
8718       if (tree_int_cst_sgn (const1) < 0)
8719         cmp_code = swap_tree_comparison (cmp_code);
8720
8721       return fold_build2 (cmp_code, type, variable1, const2);
8722     }
8723
8724   tem = maybe_canonicalize_comparison (code, type, op0, op1);
8725   if (tem)
8726     return tem;
8727
8728   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8729     {
8730       tree targ0 = strip_float_extensions (arg0);
8731       tree targ1 = strip_float_extensions (arg1);
8732       tree newtype = TREE_TYPE (targ0);
8733
8734       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8735         newtype = TREE_TYPE (targ1);
8736
8737       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
8738       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8739         return fold_build2 (code, type, fold_convert (newtype, targ0),
8740                             fold_convert (newtype, targ1));
8741
8742       /* (-a) CMP (-b) -> b CMP a  */
8743       if (TREE_CODE (arg0) == NEGATE_EXPR
8744           && TREE_CODE (arg1) == NEGATE_EXPR)
8745         return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
8746                             TREE_OPERAND (arg0, 0));
8747
8748       if (TREE_CODE (arg1) == REAL_CST)
8749         {
8750           REAL_VALUE_TYPE cst;
8751           cst = TREE_REAL_CST (arg1);
8752
8753           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
8754           if (TREE_CODE (arg0) == NEGATE_EXPR)
8755             return fold_build2 (swap_tree_comparison (code), type,
8756                                 TREE_OPERAND (arg0, 0),
8757                                 build_real (TREE_TYPE (arg1),
8758                                             REAL_VALUE_NEGATE (cst)));
8759
8760           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
8761           /* a CMP (-0) -> a CMP 0  */
8762           if (REAL_VALUE_MINUS_ZERO (cst))
8763             return fold_build2 (code, type, arg0,
8764                                 build_real (TREE_TYPE (arg1), dconst0));
8765
8766           /* x != NaN is always true, other ops are always false.  */
8767           if (REAL_VALUE_ISNAN (cst)
8768               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8769             {
8770               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8771               return omit_one_operand (type, tem, arg0);
8772             }
8773
8774           /* Fold comparisons against infinity.  */
8775           if (REAL_VALUE_ISINF (cst))
8776             {
8777               tem = fold_inf_compare (code, type, arg0, arg1);
8778               if (tem != NULL_TREE)
8779                 return tem;
8780             }
8781         }
8782
8783       /* If this is a comparison of a real constant with a PLUS_EXPR
8784          or a MINUS_EXPR of a real constant, we can convert it into a
8785          comparison with a revised real constant as long as no overflow
8786          occurs when unsafe_math_optimizations are enabled.  */
8787       if (flag_unsafe_math_optimizations
8788           && TREE_CODE (arg1) == REAL_CST
8789           && (TREE_CODE (arg0) == PLUS_EXPR
8790               || TREE_CODE (arg0) == MINUS_EXPR)
8791           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
8792           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8793                                       ? MINUS_EXPR : PLUS_EXPR,
8794                                       arg1, TREE_OPERAND (arg0, 1), 0))
8795           && !TREE_OVERFLOW (tem))
8796         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
8797
8798       /* Likewise, we can simplify a comparison of a real constant with
8799          a MINUS_EXPR whose first operand is also a real constant, i.e.
8800          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on 
8801          floating-point types only if -fassociative-math is set.  */
8802       if (flag_associative_math
8803           && TREE_CODE (arg1) == REAL_CST
8804           && TREE_CODE (arg0) == MINUS_EXPR
8805           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
8806           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
8807                                       arg1, 0))
8808           && !TREE_OVERFLOW (tem))
8809         return fold_build2 (swap_tree_comparison (code), type,
8810                             TREE_OPERAND (arg0, 1), tem);
8811
8812       /* Fold comparisons against built-in math functions.  */
8813       if (TREE_CODE (arg1) == REAL_CST
8814           && flag_unsafe_math_optimizations
8815           && ! flag_errno_math)
8816         {
8817           enum built_in_function fcode = builtin_mathfn_code (arg0);
8818
8819           if (fcode != END_BUILTINS)
8820             {
8821               tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
8822               if (tem != NULL_TREE)
8823                 return tem;
8824             }
8825         }
8826     }
8827
8828   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
8829       && (TREE_CODE (arg0) == NOP_EXPR
8830           || TREE_CODE (arg0) == CONVERT_EXPR))
8831     {
8832       /* If we are widening one operand of an integer comparison,
8833          see if the other operand is similarly being widened.  Perhaps we
8834          can do the comparison in the narrower type.  */
8835       tem = fold_widened_comparison (code, type, arg0, arg1);
8836       if (tem)
8837         return tem;
8838
8839       /* Or if we are changing signedness.  */
8840       tem = fold_sign_changed_comparison (code, type, arg0, arg1);
8841       if (tem)
8842         return tem;
8843     }
8844
8845   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8846      constant, we can simplify it.  */
8847   if (TREE_CODE (arg1) == INTEGER_CST
8848       && (TREE_CODE (arg0) == MIN_EXPR
8849           || TREE_CODE (arg0) == MAX_EXPR)
8850       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8851     {
8852       tem = optimize_minmax_comparison (code, type, op0, op1);
8853       if (tem)
8854         return tem;
8855     }
8856
8857   /* Simplify comparison of something with itself.  (For IEEE
8858      floating-point, we can only do some of these simplifications.)  */
8859   if (operand_equal_p (arg0, arg1, 0))
8860     {
8861       switch (code)
8862         {
8863         case EQ_EXPR:
8864           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8865               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8866             return constant_boolean_node (1, type);
8867           break;
8868
8869         case GE_EXPR:
8870         case LE_EXPR:
8871           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8872               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8873             return constant_boolean_node (1, type);
8874           return fold_build2 (EQ_EXPR, type, arg0, arg1);
8875
8876         case NE_EXPR:
8877           /* For NE, we can only do this simplification if integer
8878              or we don't honor IEEE floating point NaNs.  */
8879           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
8880               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8881             break;
8882           /* ... fall through ...  */
8883         case GT_EXPR:
8884         case LT_EXPR:
8885           return constant_boolean_node (0, type);
8886         default:
8887           gcc_unreachable ();
8888         }
8889     }
8890
8891   /* If we are comparing an expression that just has comparisons
8892      of two integer values, arithmetic expressions of those comparisons,
8893      and constants, we can simplify it.  There are only three cases
8894      to check: the two values can either be equal, the first can be
8895      greater, or the second can be greater.  Fold the expression for
8896      those three values.  Since each value must be 0 or 1, we have
8897      eight possibilities, each of which corresponds to the constant 0
8898      or 1 or one of the six possible comparisons.
8899
8900      This handles common cases like (a > b) == 0 but also handles
8901      expressions like  ((x > y) - (y > x)) > 0, which supposedly
8902      occur in macroized code.  */
8903
8904   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8905     {
8906       tree cval1 = 0, cval2 = 0;
8907       int save_p = 0;
8908
8909       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8910           /* Don't handle degenerate cases here; they should already
8911              have been handled anyway.  */
8912           && cval1 != 0 && cval2 != 0
8913           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8914           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8915           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8916           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8917           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8918           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8919                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8920         {
8921           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8922           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8923
8924           /* We can't just pass T to eval_subst in case cval1 or cval2
8925              was the same as ARG1.  */
8926
8927           tree high_result
8928                 = fold_build2 (code, type,
8929                                eval_subst (arg0, cval1, maxval,
8930                                            cval2, minval),
8931                                arg1);
8932           tree equal_result
8933                 = fold_build2 (code, type,
8934                                eval_subst (arg0, cval1, maxval,
8935                                            cval2, maxval),
8936                                arg1);
8937           tree low_result
8938                 = fold_build2 (code, type,
8939                                eval_subst (arg0, cval1, minval,
8940                                            cval2, maxval),
8941                                arg1);
8942
8943           /* All three of these results should be 0 or 1.  Confirm they are.
8944              Then use those values to select the proper code to use.  */
8945
8946           if (TREE_CODE (high_result) == INTEGER_CST
8947               && TREE_CODE (equal_result) == INTEGER_CST
8948               && TREE_CODE (low_result) == INTEGER_CST)
8949             {
8950               /* Make a 3-bit mask with the high-order bit being the
8951                  value for `>', the next for '=', and the low for '<'.  */
8952               switch ((integer_onep (high_result) * 4)
8953                       + (integer_onep (equal_result) * 2)
8954                       + integer_onep (low_result))
8955                 {
8956                 case 0:
8957                   /* Always false.  */
8958                   return omit_one_operand (type, integer_zero_node, arg0);
8959                 case 1:
8960                   code = LT_EXPR;
8961                   break;
8962                 case 2:
8963                   code = EQ_EXPR;
8964                   break;
8965                 case 3:
8966                   code = LE_EXPR;
8967                   break;
8968                 case 4:
8969                   code = GT_EXPR;
8970                   break;
8971                 case 5:
8972                   code = NE_EXPR;
8973                   break;
8974                 case 6:
8975                   code = GE_EXPR;
8976                   break;
8977                 case 7:
8978                   /* Always true.  */
8979                   return omit_one_operand (type, integer_one_node, arg0);
8980                 }
8981
8982               if (save_p)
8983                 return save_expr (build2 (code, type, cval1, cval2));
8984               return fold_build2 (code, type, cval1, cval2);
8985             }
8986         }
8987     }
8988
8989   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8990      into a single range test.  */
8991   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8992        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8993       && TREE_CODE (arg1) == INTEGER_CST
8994       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8995       && !integer_zerop (TREE_OPERAND (arg0, 1))
8996       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8997       && !TREE_OVERFLOW (arg1))
8998     {
8999       tem = fold_div_compare (code, type, arg0, arg1);
9000       if (tem != NULL_TREE)
9001         return tem;
9002     }
9003
9004   /* Fold ~X op ~Y as Y op X.  */
9005   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9006       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9007     {
9008       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9009       return fold_build2 (code, type,
9010                           fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9011                           TREE_OPERAND (arg0, 0));
9012     }
9013
9014   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9015   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9016       && TREE_CODE (arg1) == INTEGER_CST)
9017     {
9018       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9019       return fold_build2 (swap_tree_comparison (code), type,
9020                           TREE_OPERAND (arg0, 0),
9021                           fold_build1 (BIT_NOT_EXPR, cmp_type,
9022                                        fold_convert (cmp_type, arg1)));
9023     }
9024
9025   return NULL_TREE;
9026 }
9027
9028
9029 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9030    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9031    argument EXPR represents the expression "z" of type TYPE.  */
9032
9033 static tree
9034 fold_mult_zconjz (tree type, tree expr)
9035 {
9036   tree itype = TREE_TYPE (type);
9037   tree rpart, ipart, tem;
9038
9039   if (TREE_CODE (expr) == COMPLEX_EXPR)
9040     {
9041       rpart = TREE_OPERAND (expr, 0);
9042       ipart = TREE_OPERAND (expr, 1);
9043     }
9044   else if (TREE_CODE (expr) == COMPLEX_CST)
9045     {
9046       rpart = TREE_REALPART (expr);
9047       ipart = TREE_IMAGPART (expr);
9048     }
9049   else
9050     {
9051       expr = save_expr (expr);
9052       rpart = fold_build1 (REALPART_EXPR, itype, expr);
9053       ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9054     }
9055
9056   rpart = save_expr (rpart);
9057   ipart = save_expr (ipart);
9058   tem = fold_build2 (PLUS_EXPR, itype,
9059                      fold_build2 (MULT_EXPR, itype, rpart, rpart),
9060                      fold_build2 (MULT_EXPR, itype, ipart, ipart));
9061   return fold_build2 (COMPLEX_EXPR, type, tem,
9062                       fold_convert (itype, integer_zero_node));
9063 }
9064
9065
9066 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9067    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9068    guarantees that P and N have the same least significant log2(M) bits.
9069    N is not otherwise constrained.  In particular, N is not normalized to
9070    0 <= N < M as is common.  In general, the precise value of P is unknown.
9071    M is chosen as large as possible such that constant N can be determined.
9072
9073    Returns M and sets *RESIDUE to N.  */
9074
9075 static unsigned HOST_WIDE_INT
9076 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue)
9077 {
9078   enum tree_code code;
9079
9080   *residue = 0;
9081
9082   code = TREE_CODE (expr);
9083   if (code == ADDR_EXPR)
9084     {
9085       expr = TREE_OPERAND (expr, 0);
9086       if (handled_component_p (expr))
9087         {
9088           HOST_WIDE_INT bitsize, bitpos;
9089           tree offset;
9090           enum machine_mode mode;
9091           int unsignedp, volatilep;
9092
9093           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9094                                       &mode, &unsignedp, &volatilep, false);
9095           *residue = bitpos / BITS_PER_UNIT;
9096           if (offset)
9097             {
9098               if (TREE_CODE (offset) == INTEGER_CST)
9099                 *residue += TREE_INT_CST_LOW (offset);
9100               else
9101                 /* We don't handle more complicated offset expressions.  */
9102                 return 1;
9103             }
9104         }
9105
9106       if (DECL_P (expr) && TREE_CODE (expr) != FUNCTION_DECL)
9107         return DECL_ALIGN_UNIT (expr);
9108     }
9109   else if (code == POINTER_PLUS_EXPR)
9110     {
9111       tree op0, op1;
9112       unsigned HOST_WIDE_INT modulus;
9113       enum tree_code inner_code;
9114       
9115       op0 = TREE_OPERAND (expr, 0);
9116       STRIP_NOPS (op0);
9117       modulus = get_pointer_modulus_and_residue (op0, residue);
9118
9119       op1 = TREE_OPERAND (expr, 1);
9120       STRIP_NOPS (op1);
9121       inner_code = TREE_CODE (op1);
9122       if (inner_code == INTEGER_CST)
9123         {
9124           *residue += TREE_INT_CST_LOW (op1);
9125           return modulus;
9126         }
9127       else if (inner_code == MULT_EXPR)
9128         {
9129           op1 = TREE_OPERAND (op1, 1);
9130           if (TREE_CODE (op1) == INTEGER_CST)
9131             {
9132               unsigned HOST_WIDE_INT align;
9133               
9134               /* Compute the greatest power-of-2 divisor of op1.  */
9135               align = TREE_INT_CST_LOW (op1);
9136               align &= -align;
9137
9138               /* If align is non-zero and less than *modulus, replace
9139                  *modulus with align., If align is 0, then either op1 is 0
9140                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9141                  unsigned HOST_WIDE_INT.  In either case, no additional
9142                  constraint is imposed.  */
9143               if (align)
9144                 modulus = MIN (modulus, align);
9145
9146               return modulus;
9147             }
9148         }
9149     }
9150
9151     /* If we get here, we were unable to determine anything useful about the
9152        expression.  */
9153     return 1;
9154 }
9155
9156
9157 /* Fold a binary expression of code CODE and type TYPE with operands
9158    OP0 and OP1.  Return the folded expression if folding is
9159    successful.  Otherwise, return NULL_TREE.  */
9160
9161 tree
9162 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9163 {
9164   enum tree_code_class kind = TREE_CODE_CLASS (code);
9165   tree arg0, arg1, tem;
9166   tree t1 = NULL_TREE;
9167   bool strict_overflow_p;
9168
9169   gcc_assert ((IS_EXPR_CODE_CLASS (kind)
9170                || IS_GIMPLE_STMT_CODE_CLASS (kind))
9171               && TREE_CODE_LENGTH (code) == 2
9172               && op0 != NULL_TREE
9173               && op1 != NULL_TREE);
9174
9175   arg0 = op0;
9176   arg1 = op1;
9177
9178   /* Strip any conversions that don't change the mode.  This is
9179      safe for every expression, except for a comparison expression
9180      because its signedness is derived from its operands.  So, in
9181      the latter case, only strip conversions that don't change the
9182      signedness.
9183
9184      Note that this is done as an internal manipulation within the
9185      constant folder, in order to find the simplest representation
9186      of the arguments so that their form can be studied.  In any
9187      cases, the appropriate type conversions should be put back in
9188      the tree that will get out of the constant folder.  */
9189
9190   if (kind == tcc_comparison)
9191     {
9192       STRIP_SIGN_NOPS (arg0);
9193       STRIP_SIGN_NOPS (arg1);
9194     }
9195   else
9196     {
9197       STRIP_NOPS (arg0);
9198       STRIP_NOPS (arg1);
9199     }
9200
9201   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9202      constant but we can't do arithmetic on them.  */
9203   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9204       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9205       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9206       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9207       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9208       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9209     {
9210       if (kind == tcc_binary)
9211         {
9212           /* Make sure type and arg0 have the same saturating flag.  */
9213           gcc_assert (TYPE_SATURATING (type)
9214                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9215           tem = const_binop (code, arg0, arg1, 0);
9216         }
9217       else if (kind == tcc_comparison)
9218         tem = fold_relational_const (code, type, arg0, arg1);
9219       else
9220         tem = NULL_TREE;
9221
9222       if (tem != NULL_TREE)
9223         {
9224           if (TREE_TYPE (tem) != type)
9225             tem = fold_convert (type, tem);
9226           return tem;
9227         }
9228     }
9229
9230   /* If this is a commutative operation, and ARG0 is a constant, move it
9231      to ARG1 to reduce the number of tests below.  */
9232   if (commutative_tree_code (code)
9233       && tree_swap_operands_p (arg0, arg1, true))
9234     return fold_build2 (code, type, op1, op0);
9235
9236   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9237
9238      First check for cases where an arithmetic operation is applied to a
9239      compound, conditional, or comparison operation.  Push the arithmetic
9240      operation inside the compound or conditional to see if any folding
9241      can then be done.  Convert comparison to conditional for this purpose.
9242      The also optimizes non-constant cases that used to be done in
9243      expand_expr.
9244
9245      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9246      one of the operands is a comparison and the other is a comparison, a
9247      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9248      code below would make the expression more complex.  Change it to a
9249      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9250      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9251
9252   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9253        || code == EQ_EXPR || code == NE_EXPR)
9254       && ((truth_value_p (TREE_CODE (arg0))
9255            && (truth_value_p (TREE_CODE (arg1))
9256                || (TREE_CODE (arg1) == BIT_AND_EXPR
9257                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9258           || (truth_value_p (TREE_CODE (arg1))
9259               && (truth_value_p (TREE_CODE (arg0))
9260                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9261                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9262     {
9263       tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9264                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9265                          : TRUTH_XOR_EXPR,
9266                          boolean_type_node,
9267                          fold_convert (boolean_type_node, arg0),
9268                          fold_convert (boolean_type_node, arg1));
9269
9270       if (code == EQ_EXPR)
9271         tem = invert_truthvalue (tem);
9272
9273       return fold_convert (type, tem);
9274     }
9275
9276   if (TREE_CODE_CLASS (code) == tcc_binary
9277       || TREE_CODE_CLASS (code) == tcc_comparison)
9278     {
9279       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9280         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9281                        fold_build2 (code, type,
9282                                     fold_convert (TREE_TYPE (op0),
9283                                                   TREE_OPERAND (arg0, 1)),
9284                                     op1));
9285       if (TREE_CODE (arg1) == COMPOUND_EXPR
9286           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9287         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9288                        fold_build2 (code, type, op0,
9289                                     fold_convert (TREE_TYPE (op1),
9290                                                   TREE_OPERAND (arg1, 1))));
9291
9292       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9293         {
9294           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9295                                                      arg0, arg1, 
9296                                                      /*cond_first_p=*/1);
9297           if (tem != NULL_TREE)
9298             return tem;
9299         }
9300
9301       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9302         {
9303           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9304                                                      arg1, arg0, 
9305                                                      /*cond_first_p=*/0);
9306           if (tem != NULL_TREE)
9307             return tem;
9308         }
9309     }
9310
9311   switch (code)
9312     {
9313     case POINTER_PLUS_EXPR:
9314       /* 0 +p index -> (type)index */
9315       if (integer_zerop (arg0))
9316         return non_lvalue (fold_convert (type, arg1));
9317
9318       /* PTR +p 0 -> PTR */
9319       if (integer_zerop (arg1))
9320         return non_lvalue (fold_convert (type, arg0));
9321
9322       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9323       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9324            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9325         return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9326                                                 fold_convert (sizetype, arg1),
9327                                                 fold_convert (sizetype, arg0)));
9328
9329       /* index +p PTR -> PTR +p index */
9330       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9331           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9332         return fold_build2 (POINTER_PLUS_EXPR, type,
9333                             fold_convert (type, arg1),
9334                             fold_convert (sizetype, arg0));
9335
9336       /* (PTR +p B) +p A -> PTR +p (B + A) */
9337       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9338         {
9339           tree inner;
9340           tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9341           tree arg00 = TREE_OPERAND (arg0, 0);
9342           inner = fold_build2 (PLUS_EXPR, sizetype,
9343                                arg01, fold_convert (sizetype, arg1));
9344           return fold_convert (type,
9345                                fold_build2 (POINTER_PLUS_EXPR,
9346                                             TREE_TYPE (arg00), arg00, inner));
9347         }
9348
9349       /* PTR_CST +p CST -> CST1 */
9350       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9351         return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9352
9353      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9354         of the array.  Loop optimizer sometimes produce this type of
9355         expressions.  */
9356       if (TREE_CODE (arg0) == ADDR_EXPR)
9357         {
9358           tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9359           if (tem)
9360             return fold_convert (type, tem);
9361         }
9362
9363       return NULL_TREE;
9364
9365     case PLUS_EXPR:
9366       /* PTR + INT -> (INT)(PTR p+ INT) */
9367       if (POINTER_TYPE_P (TREE_TYPE (arg0))
9368           && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
9369         return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9370                                                 TREE_TYPE (arg0),
9371                                                 arg0,
9372                                                 fold_convert (sizetype, arg1)));
9373       /* INT + PTR -> (INT)(PTR p+ INT) */
9374       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9375           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9376         return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9377                                                 TREE_TYPE (arg1),
9378                                                 arg1,
9379                                                 fold_convert (sizetype, arg0)));
9380       /* A + (-B) -> A - B */
9381       if (TREE_CODE (arg1) == NEGATE_EXPR)
9382         return fold_build2 (MINUS_EXPR, type,
9383                             fold_convert (type, arg0),
9384                             fold_convert (type, TREE_OPERAND (arg1, 0)));
9385       /* (-A) + B -> B - A */
9386       if (TREE_CODE (arg0) == NEGATE_EXPR
9387           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9388         return fold_build2 (MINUS_EXPR, type,
9389                             fold_convert (type, arg1),
9390                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9391
9392       if (INTEGRAL_TYPE_P (type))
9393         {
9394           /* Convert ~A + 1 to -A.  */
9395           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9396               && integer_onep (arg1))
9397             return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
9398
9399           /* ~X + X is -1.  */
9400           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9401               && !TYPE_OVERFLOW_TRAPS (type))
9402             {
9403               tree tem = TREE_OPERAND (arg0, 0);
9404
9405               STRIP_NOPS (tem);
9406               if (operand_equal_p (tem, arg1, 0))
9407                 {
9408                   t1 = build_int_cst_type (type, -1);
9409                   return omit_one_operand (type, t1, arg1);
9410                 }
9411             }
9412
9413           /* X + ~X is -1.  */
9414           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9415               && !TYPE_OVERFLOW_TRAPS (type))
9416             {
9417               tree tem = TREE_OPERAND (arg1, 0);
9418
9419               STRIP_NOPS (tem);
9420               if (operand_equal_p (arg0, tem, 0))
9421                 {
9422                   t1 = build_int_cst_type (type, -1);
9423                   return omit_one_operand (type, t1, arg0);
9424                 }
9425             }
9426
9427           /* X + (X / CST) * -CST is X % CST.  */
9428           if (TREE_CODE (arg1) == MULT_EXPR
9429               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9430               && operand_equal_p (arg0,
9431                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9432             {
9433               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9434               tree cst1 = TREE_OPERAND (arg1, 1);
9435               tree sum = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1, cst0);
9436               if (sum && integer_zerop (sum))
9437                 return fold_convert (type,
9438                                      fold_build2 (TRUNC_MOD_EXPR,
9439                                                   TREE_TYPE (arg0), arg0, cst0));
9440             }
9441         }
9442
9443       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9444          same or one.  Make sure type is not saturating.
9445          fold_plusminus_mult_expr will re-associate.  */
9446       if ((TREE_CODE (arg0) == MULT_EXPR
9447            || TREE_CODE (arg1) == MULT_EXPR)
9448           && !TYPE_SATURATING (type)
9449           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9450         {
9451           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9452           if (tem)
9453             return tem;
9454         }
9455
9456       if (! FLOAT_TYPE_P (type))
9457         {
9458           if (integer_zerop (arg1))
9459             return non_lvalue (fold_convert (type, arg0));
9460
9461           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9462              with a constant, and the two constants have no bits in common,
9463              we should treat this as a BIT_IOR_EXPR since this may produce more
9464              simplifications.  */
9465           if (TREE_CODE (arg0) == BIT_AND_EXPR
9466               && TREE_CODE (arg1) == BIT_AND_EXPR
9467               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9468               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9469               && integer_zerop (const_binop (BIT_AND_EXPR,
9470                                              TREE_OPERAND (arg0, 1),
9471                                              TREE_OPERAND (arg1, 1), 0)))
9472             {
9473               code = BIT_IOR_EXPR;
9474               goto bit_ior;
9475             }
9476
9477           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9478              (plus (plus (mult) (mult)) (foo)) so that we can
9479              take advantage of the factoring cases below.  */
9480           if (((TREE_CODE (arg0) == PLUS_EXPR
9481                 || TREE_CODE (arg0) == MINUS_EXPR)
9482                && TREE_CODE (arg1) == MULT_EXPR)
9483               || ((TREE_CODE (arg1) == PLUS_EXPR
9484                    || TREE_CODE (arg1) == MINUS_EXPR)
9485                   && TREE_CODE (arg0) == MULT_EXPR))
9486             {
9487               tree parg0, parg1, parg, marg;
9488               enum tree_code pcode;
9489
9490               if (TREE_CODE (arg1) == MULT_EXPR)
9491                 parg = arg0, marg = arg1;
9492               else
9493                 parg = arg1, marg = arg0;
9494               pcode = TREE_CODE (parg);
9495               parg0 = TREE_OPERAND (parg, 0);
9496               parg1 = TREE_OPERAND (parg, 1);
9497               STRIP_NOPS (parg0);
9498               STRIP_NOPS (parg1);
9499
9500               if (TREE_CODE (parg0) == MULT_EXPR
9501                   && TREE_CODE (parg1) != MULT_EXPR)
9502                 return fold_build2 (pcode, type,
9503                                     fold_build2 (PLUS_EXPR, type,
9504                                                  fold_convert (type, parg0),
9505                                                  fold_convert (type, marg)),
9506                                     fold_convert (type, parg1));
9507               if (TREE_CODE (parg0) != MULT_EXPR
9508                   && TREE_CODE (parg1) == MULT_EXPR)
9509                 return fold_build2 (PLUS_EXPR, type,
9510                                     fold_convert (type, parg0),
9511                                     fold_build2 (pcode, type,
9512                                                  fold_convert (type, marg),
9513                                                  fold_convert (type,
9514                                                                parg1)));
9515             }
9516         }
9517       else
9518         {
9519           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
9520           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9521             return non_lvalue (fold_convert (type, arg0));
9522
9523           /* Likewise if the operands are reversed.  */
9524           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9525             return non_lvalue (fold_convert (type, arg1));
9526
9527           /* Convert X + -C into X - C.  */
9528           if (TREE_CODE (arg1) == REAL_CST
9529               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9530             {
9531               tem = fold_negate_const (arg1, type);
9532               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9533                 return fold_build2 (MINUS_EXPR, type,
9534                                     fold_convert (type, arg0),
9535                                     fold_convert (type, tem));
9536             }
9537
9538           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9539              to __complex__ ( x, y ).  This is not the same for SNaNs or
9540              if signed zeros are involved.  */
9541           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9542               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9543               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9544             {
9545               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9546               tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
9547               tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
9548               bool arg0rz = false, arg0iz = false;
9549               if ((arg0r && (arg0rz = real_zerop (arg0r)))
9550                   || (arg0i && (arg0iz = real_zerop (arg0i))))
9551                 {
9552                   tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
9553                   tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
9554                   if (arg0rz && arg1i && real_zerop (arg1i))
9555                     {
9556                       tree rp = arg1r ? arg1r
9557                                   : build1 (REALPART_EXPR, rtype, arg1);
9558                       tree ip = arg0i ? arg0i
9559                                   : build1 (IMAGPART_EXPR, rtype, arg0);
9560                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9561                     }
9562                   else if (arg0iz && arg1r && real_zerop (arg1r))
9563                     {
9564                       tree rp = arg0r ? arg0r
9565                                   : build1 (REALPART_EXPR, rtype, arg0);
9566                       tree ip = arg1i ? arg1i
9567                                   : build1 (IMAGPART_EXPR, rtype, arg1);
9568                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9569                     }
9570                 }
9571             }
9572
9573           if (flag_unsafe_math_optimizations
9574               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9575               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9576               && (tem = distribute_real_division (code, type, arg0, arg1)))
9577             return tem;
9578
9579           /* Convert x+x into x*2.0.  */
9580           if (operand_equal_p (arg0, arg1, 0)
9581               && SCALAR_FLOAT_TYPE_P (type))
9582             return fold_build2 (MULT_EXPR, type, arg0,
9583                                 build_real (type, dconst2));
9584
9585           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.  
9586              We associate floats only if the user has specified
9587              -fassociative-math.  */
9588           if (flag_associative_math
9589               && TREE_CODE (arg1) == PLUS_EXPR
9590               && TREE_CODE (arg0) != MULT_EXPR)
9591             {
9592               tree tree10 = TREE_OPERAND (arg1, 0);
9593               tree tree11 = TREE_OPERAND (arg1, 1);
9594               if (TREE_CODE (tree11) == MULT_EXPR
9595                   && TREE_CODE (tree10) == MULT_EXPR)
9596                 {
9597                   tree tree0;
9598                   tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
9599                   return fold_build2 (PLUS_EXPR, type, tree0, tree11);
9600                 }
9601             }
9602           /* Convert (b*c + d*e) + a into b*c + (d*e +a).  
9603              We associate floats only if the user has specified
9604              -fassociative-math.  */
9605           if (flag_associative_math
9606               && TREE_CODE (arg0) == PLUS_EXPR
9607               && TREE_CODE (arg1) != MULT_EXPR)
9608             {
9609               tree tree00 = TREE_OPERAND (arg0, 0);
9610               tree tree01 = TREE_OPERAND (arg0, 1);
9611               if (TREE_CODE (tree01) == MULT_EXPR
9612                   && TREE_CODE (tree00) == MULT_EXPR)
9613                 {
9614                   tree tree0;
9615                   tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
9616                   return fold_build2 (PLUS_EXPR, type, tree00, tree0);
9617                 }
9618             }
9619         }
9620
9621      bit_rotate:
9622       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9623          is a rotate of A by C1 bits.  */
9624       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9625          is a rotate of A by B bits.  */
9626       {
9627         enum tree_code code0, code1;
9628         tree rtype;
9629         code0 = TREE_CODE (arg0);
9630         code1 = TREE_CODE (arg1);
9631         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9632              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9633             && operand_equal_p (TREE_OPERAND (arg0, 0),
9634                                 TREE_OPERAND (arg1, 0), 0)
9635             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9636                 TYPE_UNSIGNED (rtype))
9637             /* Only create rotates in complete modes.  Other cases are not
9638                expanded properly.  */
9639             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9640           {
9641             tree tree01, tree11;
9642             enum tree_code code01, code11;
9643
9644             tree01 = TREE_OPERAND (arg0, 1);
9645             tree11 = TREE_OPERAND (arg1, 1);
9646             STRIP_NOPS (tree01);
9647             STRIP_NOPS (tree11);
9648             code01 = TREE_CODE (tree01);
9649             code11 = TREE_CODE (tree11);
9650             if (code01 == INTEGER_CST
9651                 && code11 == INTEGER_CST
9652                 && TREE_INT_CST_HIGH (tree01) == 0
9653                 && TREE_INT_CST_HIGH (tree11) == 0
9654                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9655                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9656               return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
9657                              code0 == LSHIFT_EXPR ? tree01 : tree11);
9658             else if (code11 == MINUS_EXPR)
9659               {
9660                 tree tree110, tree111;
9661                 tree110 = TREE_OPERAND (tree11, 0);
9662                 tree111 = TREE_OPERAND (tree11, 1);
9663                 STRIP_NOPS (tree110);
9664                 STRIP_NOPS (tree111);
9665                 if (TREE_CODE (tree110) == INTEGER_CST
9666                     && 0 == compare_tree_int (tree110,
9667                                               TYPE_PRECISION
9668                                               (TREE_TYPE (TREE_OPERAND
9669                                                           (arg0, 0))))
9670                     && operand_equal_p (tree01, tree111, 0))
9671                   return build2 ((code0 == LSHIFT_EXPR
9672                                   ? LROTATE_EXPR
9673                                   : RROTATE_EXPR),
9674                                  type, TREE_OPERAND (arg0, 0), tree01);
9675               }
9676             else if (code01 == MINUS_EXPR)
9677               {
9678                 tree tree010, tree011;
9679                 tree010 = TREE_OPERAND (tree01, 0);
9680                 tree011 = TREE_OPERAND (tree01, 1);
9681                 STRIP_NOPS (tree010);
9682                 STRIP_NOPS (tree011);
9683                 if (TREE_CODE (tree010) == INTEGER_CST
9684                     && 0 == compare_tree_int (tree010,
9685                                               TYPE_PRECISION
9686                                               (TREE_TYPE (TREE_OPERAND
9687                                                           (arg0, 0))))
9688                     && operand_equal_p (tree11, tree011, 0))
9689                   return build2 ((code0 != LSHIFT_EXPR
9690                                   ? LROTATE_EXPR
9691                                   : RROTATE_EXPR),
9692                                  type, TREE_OPERAND (arg0, 0), tree11);
9693               }
9694           }
9695       }
9696
9697     associate:
9698       /* In most languages, can't associate operations on floats through
9699          parentheses.  Rather than remember where the parentheses were, we
9700          don't associate floats at all, unless the user has specified
9701          -fassociative-math.
9702          And, we need to make sure type is not saturating.  */
9703
9704       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9705           && !TYPE_SATURATING (type))
9706         {
9707           tree var0, con0, lit0, minus_lit0;
9708           tree var1, con1, lit1, minus_lit1;
9709           bool ok = true;
9710
9711           /* Split both trees into variables, constants, and literals.  Then
9712              associate each group together, the constants with literals,
9713              then the result with variables.  This increases the chances of
9714              literals being recombined later and of generating relocatable
9715              expressions for the sum of a constant and literal.  */
9716           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9717           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9718                              code == MINUS_EXPR);
9719
9720           /* With undefined overflow we can only associate constants
9721              with one variable.  */
9722           if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9723                || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9724               && var0 && var1)
9725             {
9726               tree tmp0 = var0;
9727               tree tmp1 = var1;
9728
9729               if (TREE_CODE (tmp0) == NEGATE_EXPR)
9730                 tmp0 = TREE_OPERAND (tmp0, 0);
9731               if (TREE_CODE (tmp1) == NEGATE_EXPR)
9732                 tmp1 = TREE_OPERAND (tmp1, 0);
9733               /* The only case we can still associate with two variables
9734                  is if they are the same, modulo negation.  */
9735               if (!operand_equal_p (tmp0, tmp1, 0))
9736                 ok = false;
9737             }
9738
9739           /* Only do something if we found more than two objects.  Otherwise,
9740              nothing has changed and we risk infinite recursion.  */
9741           if (ok
9742               && (2 < ((var0 != 0) + (var1 != 0)
9743                        + (con0 != 0) + (con1 != 0)
9744                        + (lit0 != 0) + (lit1 != 0)
9745                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
9746             {
9747               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
9748               if (code == MINUS_EXPR)
9749                 code = PLUS_EXPR;
9750
9751               var0 = associate_trees (var0, var1, code, type);
9752               con0 = associate_trees (con0, con1, code, type);
9753               lit0 = associate_trees (lit0, lit1, code, type);
9754               minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
9755
9756               /* Preserve the MINUS_EXPR if the negative part of the literal is
9757                  greater than the positive part.  Otherwise, the multiplicative
9758                  folding code (i.e extract_muldiv) may be fooled in case
9759                  unsigned constants are subtracted, like in the following
9760                  example: ((X*2 + 4) - 8U)/2.  */
9761               if (minus_lit0 && lit0)
9762                 {
9763                   if (TREE_CODE (lit0) == INTEGER_CST
9764                       && TREE_CODE (minus_lit0) == INTEGER_CST
9765                       && tree_int_cst_lt (lit0, minus_lit0))
9766                     {
9767                       minus_lit0 = associate_trees (minus_lit0, lit0,
9768                                                     MINUS_EXPR, type);
9769                       lit0 = 0;
9770                     }
9771                   else
9772                     {
9773                       lit0 = associate_trees (lit0, minus_lit0,
9774                                               MINUS_EXPR, type);
9775                       minus_lit0 = 0;
9776                     }
9777                 }
9778               if (minus_lit0)
9779                 {
9780                   if (con0 == 0)
9781                     return fold_convert (type,
9782                                          associate_trees (var0, minus_lit0,
9783                                                           MINUS_EXPR, type));
9784                   else
9785                     {
9786                       con0 = associate_trees (con0, minus_lit0,
9787                                               MINUS_EXPR, type);
9788                       return fold_convert (type,
9789                                            associate_trees (var0, con0,
9790                                                             PLUS_EXPR, type));
9791                     }
9792                 }
9793
9794               con0 = associate_trees (con0, lit0, code, type);
9795               return fold_convert (type, associate_trees (var0, con0,
9796                                                           code, type));
9797             }
9798         }
9799
9800       return NULL_TREE;
9801
9802     case MINUS_EXPR:
9803       /* Pointer simplifications for subtraction, simple reassociations. */
9804       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
9805         {
9806           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
9807           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
9808               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9809             {
9810               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9811               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
9812               tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
9813               tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
9814               return fold_build2 (PLUS_EXPR, type,
9815                                   fold_build2 (MINUS_EXPR, type, arg00, arg10),
9816                                   fold_build2 (MINUS_EXPR, type, arg01, arg11));
9817             }
9818           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
9819           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9820             {
9821               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9822               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
9823               tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
9824               if (tmp)
9825                 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
9826             }
9827         }
9828       /* A - (-B) -> A + B */
9829       if (TREE_CODE (arg1) == NEGATE_EXPR)
9830         return fold_build2 (PLUS_EXPR, type, op0,
9831                             fold_convert (type, TREE_OPERAND (arg1, 0)));
9832       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
9833       if (TREE_CODE (arg0) == NEGATE_EXPR
9834           && (FLOAT_TYPE_P (type)
9835               || INTEGRAL_TYPE_P (type))
9836           && negate_expr_p (arg1)
9837           && reorder_operands_p (arg0, arg1))
9838         return fold_build2 (MINUS_EXPR, type,
9839                             fold_convert (type, negate_expr (arg1)),
9840                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9841       /* Convert -A - 1 to ~A.  */
9842       if (INTEGRAL_TYPE_P (type)
9843           && TREE_CODE (arg0) == NEGATE_EXPR
9844           && integer_onep (arg1)
9845           && !TYPE_OVERFLOW_TRAPS (type))
9846         return fold_build1 (BIT_NOT_EXPR, type,
9847                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9848
9849       /* Convert -1 - A to ~A.  */
9850       if (INTEGRAL_TYPE_P (type)
9851           && integer_all_onesp (arg0))
9852         return fold_build1 (BIT_NOT_EXPR, type, op1);
9853
9854
9855       /* X - (X / CST) * CST is X % CST.  */
9856       if (INTEGRAL_TYPE_P (type)
9857           && TREE_CODE (arg1) == MULT_EXPR
9858           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9859           && operand_equal_p (arg0,
9860                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
9861           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
9862                               TREE_OPERAND (arg1, 1), 0))
9863         return fold_convert (type,
9864                              fold_build2 (TRUNC_MOD_EXPR, TREE_TYPE (arg0),
9865                                           arg0, TREE_OPERAND (arg1, 1)));
9866
9867       if (! FLOAT_TYPE_P (type))
9868         {
9869           if (integer_zerop (arg0))
9870             return negate_expr (fold_convert (type, arg1));
9871           if (integer_zerop (arg1))
9872             return non_lvalue (fold_convert (type, arg0));
9873
9874           /* Fold A - (A & B) into ~B & A.  */
9875           if (!TREE_SIDE_EFFECTS (arg0)
9876               && TREE_CODE (arg1) == BIT_AND_EXPR)
9877             {
9878               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
9879                 {
9880                   tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
9881                   return fold_build2 (BIT_AND_EXPR, type,
9882                                       fold_build1 (BIT_NOT_EXPR, type, arg10),
9883                                       fold_convert (type, arg0));
9884                 }
9885               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9886                 {
9887                   tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
9888                   return fold_build2 (BIT_AND_EXPR, type,
9889                                       fold_build1 (BIT_NOT_EXPR, type, arg11),
9890                                       fold_convert (type, arg0));
9891                 }
9892             }
9893
9894           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
9895              any power of 2 minus 1.  */
9896           if (TREE_CODE (arg0) == BIT_AND_EXPR
9897               && TREE_CODE (arg1) == BIT_AND_EXPR
9898               && operand_equal_p (TREE_OPERAND (arg0, 0),
9899                                   TREE_OPERAND (arg1, 0), 0))
9900             {
9901               tree mask0 = TREE_OPERAND (arg0, 1);
9902               tree mask1 = TREE_OPERAND (arg1, 1);
9903               tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
9904
9905               if (operand_equal_p (tem, mask1, 0))
9906                 {
9907                   tem = fold_build2 (BIT_XOR_EXPR, type,
9908                                      TREE_OPERAND (arg0, 0), mask1);
9909                   return fold_build2 (MINUS_EXPR, type, tem, mask1);
9910                 }
9911             }
9912         }
9913
9914       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
9915       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
9916         return non_lvalue (fold_convert (type, arg0));
9917
9918       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
9919          ARG0 is zero and X + ARG0 reduces to X, since that would mean
9920          (-ARG1 + ARG0) reduces to -ARG1.  */
9921       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9922         return negate_expr (fold_convert (type, arg1));
9923
9924       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9925          __complex__ ( x, -y ).  This is not the same for SNaNs or if
9926          signed zeros are involved.  */
9927       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9928           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9929           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9930         {
9931           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9932           tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
9933           tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
9934           bool arg0rz = false, arg0iz = false;
9935           if ((arg0r && (arg0rz = real_zerop (arg0r)))
9936               || (arg0i && (arg0iz = real_zerop (arg0i))))
9937             {
9938               tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
9939               tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
9940               if (arg0rz && arg1i && real_zerop (arg1i))
9941                 {
9942                   tree rp = fold_build1 (NEGATE_EXPR, rtype,
9943                                          arg1r ? arg1r
9944                                          : build1 (REALPART_EXPR, rtype, arg1));
9945                   tree ip = arg0i ? arg0i
9946                     : build1 (IMAGPART_EXPR, rtype, arg0);
9947                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9948                 }
9949               else if (arg0iz && arg1r && real_zerop (arg1r))
9950                 {
9951                   tree rp = arg0r ? arg0r
9952                     : build1 (REALPART_EXPR, rtype, arg0);
9953                   tree ip = fold_build1 (NEGATE_EXPR, rtype,
9954                                          arg1i ? arg1i
9955                                          : build1 (IMAGPART_EXPR, rtype, arg1));
9956                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9957                 }
9958             }
9959         }
9960
9961       /* Fold &x - &x.  This can happen from &x.foo - &x.
9962          This is unsafe for certain floats even in non-IEEE formats.
9963          In IEEE, it is unsafe because it does wrong for NaNs.
9964          Also note that operand_equal_p is always false if an operand
9965          is volatile.  */
9966
9967       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
9968           && operand_equal_p (arg0, arg1, 0))
9969         return fold_convert (type, integer_zero_node);
9970
9971       /* A - B -> A + (-B) if B is easily negatable.  */
9972       if (negate_expr_p (arg1)
9973           && ((FLOAT_TYPE_P (type)
9974                /* Avoid this transformation if B is a positive REAL_CST.  */
9975                && (TREE_CODE (arg1) != REAL_CST
9976                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
9977               || INTEGRAL_TYPE_P (type)))
9978         return fold_build2 (PLUS_EXPR, type,
9979                             fold_convert (type, arg0),
9980                             fold_convert (type, negate_expr (arg1)));
9981
9982       /* Try folding difference of addresses.  */
9983       {
9984         HOST_WIDE_INT diff;
9985
9986         if ((TREE_CODE (arg0) == ADDR_EXPR
9987              || TREE_CODE (arg1) == ADDR_EXPR)
9988             && ptr_difference_const (arg0, arg1, &diff))
9989           return build_int_cst_type (type, diff);
9990       }
9991
9992       /* Fold &a[i] - &a[j] to i-j.  */
9993       if (TREE_CODE (arg0) == ADDR_EXPR
9994           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9995           && TREE_CODE (arg1) == ADDR_EXPR
9996           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9997         {
9998           tree aref0 = TREE_OPERAND (arg0, 0);
9999           tree aref1 = TREE_OPERAND (arg1, 0);
10000           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10001                                TREE_OPERAND (aref1, 0), 0))
10002             {
10003               tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10004               tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10005               tree esz = array_ref_element_size (aref0);
10006               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10007               return fold_build2 (MULT_EXPR, type, diff,
10008                                   fold_convert (type, esz));
10009                                   
10010             }
10011         }
10012
10013       if (flag_unsafe_math_optimizations
10014           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10015           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10016           && (tem = distribute_real_division (code, type, arg0, arg1)))
10017         return tem;
10018
10019       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10020          same or one.  Make sure type is not saturating.
10021          fold_plusminus_mult_expr will re-associate.  */
10022       if ((TREE_CODE (arg0) == MULT_EXPR
10023            || TREE_CODE (arg1) == MULT_EXPR)
10024           && !TYPE_SATURATING (type)
10025           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10026         {
10027           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10028           if (tem)
10029             return tem;
10030         }
10031
10032       goto associate;
10033
10034     case MULT_EXPR:
10035       /* (-A) * (-B) -> A * B  */
10036       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10037         return fold_build2 (MULT_EXPR, type,
10038                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10039                             fold_convert (type, negate_expr (arg1)));
10040       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10041         return fold_build2 (MULT_EXPR, type,
10042                             fold_convert (type, negate_expr (arg0)),
10043                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10044
10045       if (! FLOAT_TYPE_P (type))
10046         {
10047           if (integer_zerop (arg1))
10048             return omit_one_operand (type, arg1, arg0);
10049           if (integer_onep (arg1))
10050             return non_lvalue (fold_convert (type, arg0));
10051           /* Transform x * -1 into -x.  Make sure to do the negation
10052              on the original operand with conversions not stripped
10053              because we can only strip non-sign-changing conversions.  */
10054           if (integer_all_onesp (arg1))
10055             return fold_convert (type, negate_expr (op0));
10056           /* Transform x * -C into -x * C if x is easily negatable.  */
10057           if (TREE_CODE (arg1) == INTEGER_CST
10058               && tree_int_cst_sgn (arg1) == -1
10059               && negate_expr_p (arg0)
10060               && (tem = negate_expr (arg1)) != arg1
10061               && !TREE_OVERFLOW (tem))
10062             return fold_build2 (MULT_EXPR, type,
10063                                 fold_convert (type, negate_expr (arg0)), tem);
10064
10065           /* (a * (1 << b)) is (a << b)  */
10066           if (TREE_CODE (arg1) == LSHIFT_EXPR
10067               && integer_onep (TREE_OPERAND (arg1, 0)))
10068             return fold_build2 (LSHIFT_EXPR, type, op0,
10069                                 TREE_OPERAND (arg1, 1));
10070           if (TREE_CODE (arg0) == LSHIFT_EXPR
10071               && integer_onep (TREE_OPERAND (arg0, 0)))
10072             return fold_build2 (LSHIFT_EXPR, type, op1,
10073                                 TREE_OPERAND (arg0, 1));
10074
10075           strict_overflow_p = false;
10076           if (TREE_CODE (arg1) == INTEGER_CST
10077               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10078                                              &strict_overflow_p)))
10079             {
10080               if (strict_overflow_p)
10081                 fold_overflow_warning (("assuming signed overflow does not "
10082                                         "occur when simplifying "
10083                                         "multiplication"),
10084                                        WARN_STRICT_OVERFLOW_MISC);
10085               return fold_convert (type, tem);
10086             }
10087
10088           /* Optimize z * conj(z) for integer complex numbers.  */
10089           if (TREE_CODE (arg0) == CONJ_EXPR
10090               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10091             return fold_mult_zconjz (type, arg1);
10092           if (TREE_CODE (arg1) == CONJ_EXPR
10093               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10094             return fold_mult_zconjz (type, arg0);
10095         }
10096       else
10097         {
10098           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10099              when x is NaN, since x * 0 is also NaN.  Nor are they the
10100              same in modes with signed zeros, since multiplying a
10101              negative value by 0 gives -0, not +0.  */
10102           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10103               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10104               && real_zerop (arg1))
10105             return omit_one_operand (type, arg1, arg0);
10106           /* In IEEE floating point, x*1 is not equivalent to x for snans.  */
10107           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10108               && real_onep (arg1))
10109             return non_lvalue (fold_convert (type, arg0));
10110
10111           /* Transform x * -1.0 into -x.  */
10112           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10113               && real_minus_onep (arg1))
10114             return fold_convert (type, negate_expr (arg0));
10115
10116           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10117              the result for floating point types due to rounding so it is applied
10118              only if -fassociative-math was specify.  */
10119           if (flag_associative_math
10120               && TREE_CODE (arg0) == RDIV_EXPR
10121               && TREE_CODE (arg1) == REAL_CST
10122               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10123             {
10124               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10125                                       arg1, 0);
10126               if (tem)
10127                 return fold_build2 (RDIV_EXPR, type, tem,
10128                                     TREE_OPERAND (arg0, 1));
10129             }
10130
10131           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10132           if (operand_equal_p (arg0, arg1, 0))
10133             {
10134               tree tem = fold_strip_sign_ops (arg0);
10135               if (tem != NULL_TREE)
10136                 {
10137                   tem = fold_convert (type, tem);
10138                   return fold_build2 (MULT_EXPR, type, tem, tem);
10139                 }
10140             }
10141
10142           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10143              This is not the same for NaNs or if signed zeros are
10144              involved.  */
10145           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10146               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10147               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10148               && TREE_CODE (arg1) == COMPLEX_CST
10149               && real_zerop (TREE_REALPART (arg1)))
10150             {
10151               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10152               if (real_onep (TREE_IMAGPART (arg1)))
10153                 return fold_build2 (COMPLEX_EXPR, type,
10154                                     negate_expr (fold_build1 (IMAGPART_EXPR,
10155                                                               rtype, arg0)),
10156                                     fold_build1 (REALPART_EXPR, rtype, arg0));
10157               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10158                 return fold_build2 (COMPLEX_EXPR, type,
10159                                     fold_build1 (IMAGPART_EXPR, rtype, arg0),
10160                                     negate_expr (fold_build1 (REALPART_EXPR,
10161                                                               rtype, arg0)));
10162             }
10163
10164           /* Optimize z * conj(z) for floating point complex numbers.
10165              Guarded by flag_unsafe_math_optimizations as non-finite
10166              imaginary components don't produce scalar results.  */
10167           if (flag_unsafe_math_optimizations
10168               && TREE_CODE (arg0) == CONJ_EXPR
10169               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10170             return fold_mult_zconjz (type, arg1);
10171           if (flag_unsafe_math_optimizations
10172               && TREE_CODE (arg1) == CONJ_EXPR
10173               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10174             return fold_mult_zconjz (type, arg0);
10175
10176           if (flag_unsafe_math_optimizations)
10177             {
10178               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10179               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10180
10181               /* Optimizations of root(...)*root(...).  */
10182               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10183                 {
10184                   tree rootfn, arg;
10185                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10186                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10187
10188                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10189                   if (BUILTIN_SQRT_P (fcode0)
10190                       && operand_equal_p (arg00, arg10, 0)
10191                       && ! HONOR_SNANS (TYPE_MODE (type)))
10192                     return arg00;
10193
10194                   /* Optimize root(x)*root(y) as root(x*y).  */
10195                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10196                   arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10197                   return build_call_expr (rootfn, 1, arg);
10198                 }
10199
10200               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10201               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10202                 {
10203                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10204                   tree arg = fold_build2 (PLUS_EXPR, type,
10205                                           CALL_EXPR_ARG (arg0, 0),
10206                                           CALL_EXPR_ARG (arg1, 0));
10207                   return build_call_expr (expfn, 1, arg);
10208                 }
10209
10210               /* Optimizations of pow(...)*pow(...).  */
10211               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10212                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10213                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10214                 {
10215                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10216                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10217                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10218                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10219
10220                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10221                   if (operand_equal_p (arg01, arg11, 0))
10222                     {
10223                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10224                       tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10225                       return build_call_expr (powfn, 2, arg, arg01);
10226                     }
10227
10228                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10229                   if (operand_equal_p (arg00, arg10, 0))
10230                     {
10231                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10232                       tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10233                       return build_call_expr (powfn, 2, arg00, arg);
10234                     }
10235                 }
10236
10237               /* Optimize tan(x)*cos(x) as sin(x).  */
10238               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10239                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10240                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10241                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10242                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10243                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10244                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10245                                       CALL_EXPR_ARG (arg1, 0), 0))
10246                 {
10247                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10248
10249                   if (sinfn != NULL_TREE)
10250                     return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10251                 }
10252
10253               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10254               if (fcode1 == BUILT_IN_POW
10255                   || fcode1 == BUILT_IN_POWF
10256                   || fcode1 == BUILT_IN_POWL)
10257                 {
10258                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10259                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10260                   if (TREE_CODE (arg11) == REAL_CST
10261                       && !TREE_OVERFLOW (arg11)
10262                       && operand_equal_p (arg0, arg10, 0))
10263                     {
10264                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10265                       REAL_VALUE_TYPE c;
10266                       tree arg;
10267
10268                       c = TREE_REAL_CST (arg11);
10269                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10270                       arg = build_real (type, c);
10271                       return build_call_expr (powfn, 2, arg0, arg);
10272                     }
10273                 }
10274
10275               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10276               if (fcode0 == BUILT_IN_POW
10277                   || fcode0 == BUILT_IN_POWF
10278                   || fcode0 == BUILT_IN_POWL)
10279                 {
10280                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10281                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10282                   if (TREE_CODE (arg01) == REAL_CST
10283                       && !TREE_OVERFLOW (arg01)
10284                       && operand_equal_p (arg1, arg00, 0))
10285                     {
10286                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10287                       REAL_VALUE_TYPE c;
10288                       tree arg;
10289
10290                       c = TREE_REAL_CST (arg01);
10291                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10292                       arg = build_real (type, c);
10293                       return build_call_expr (powfn, 2, arg1, arg);
10294                     }
10295                 }
10296
10297               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10298               if (! optimize_size
10299                   && operand_equal_p (arg0, arg1, 0))
10300                 {
10301                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10302
10303                   if (powfn)
10304                     {
10305                       tree arg = build_real (type, dconst2);
10306                       return build_call_expr (powfn, 2, arg0, arg);
10307                     }
10308                 }
10309             }
10310         }
10311       goto associate;
10312
10313     case BIT_IOR_EXPR:
10314     bit_ior:
10315       if (integer_all_onesp (arg1))
10316         return omit_one_operand (type, arg1, arg0);
10317       if (integer_zerop (arg1))
10318         return non_lvalue (fold_convert (type, arg0));
10319       if (operand_equal_p (arg0, arg1, 0))
10320         return non_lvalue (fold_convert (type, arg0));
10321
10322       /* ~X | X is -1.  */
10323       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10324           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10325         {
10326           t1 = fold_convert (type, integer_zero_node);
10327           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10328           return omit_one_operand (type, t1, arg1);
10329         }
10330
10331       /* X | ~X is -1.  */
10332       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10333           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10334         {
10335           t1 = fold_convert (type, integer_zero_node);
10336           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10337           return omit_one_operand (type, t1, arg0);
10338         }
10339
10340       /* Canonicalize (X & C1) | C2.  */
10341       if (TREE_CODE (arg0) == BIT_AND_EXPR
10342           && TREE_CODE (arg1) == INTEGER_CST
10343           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10344         {
10345           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10346           int width = TYPE_PRECISION (type), w;
10347           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10348           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10349           hi2 = TREE_INT_CST_HIGH (arg1);
10350           lo2 = TREE_INT_CST_LOW (arg1);
10351
10352           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10353           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10354             return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10355
10356           if (width > HOST_BITS_PER_WIDE_INT)
10357             {
10358               mhi = (unsigned HOST_WIDE_INT) -1 
10359                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10360               mlo = -1;
10361             }
10362           else
10363             {
10364               mhi = 0;
10365               mlo = (unsigned HOST_WIDE_INT) -1
10366                     >> (HOST_BITS_PER_WIDE_INT - width);
10367             }
10368
10369           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10370           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10371             return fold_build2 (BIT_IOR_EXPR, type,
10372                                 TREE_OPERAND (arg0, 0), arg1);
10373
10374           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10375              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10376              mode which allows further optimizations.  */
10377           hi1 &= mhi;
10378           lo1 &= mlo;
10379           hi2 &= mhi;
10380           lo2 &= mlo;
10381           hi3 = hi1 & ~hi2;
10382           lo3 = lo1 & ~lo2;
10383           for (w = BITS_PER_UNIT;
10384                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10385                w <<= 1)
10386             {
10387               unsigned HOST_WIDE_INT mask
10388                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10389               if (((lo1 | lo2) & mask) == mask
10390                   && (lo1 & ~mask) == 0 && hi1 == 0)
10391                 {
10392                   hi3 = 0;
10393                   lo3 = mask;
10394                   break;
10395                 }
10396             }
10397           if (hi3 != hi1 || lo3 != lo1)
10398             return fold_build2 (BIT_IOR_EXPR, type,
10399                                 fold_build2 (BIT_AND_EXPR, type,
10400                                              TREE_OPERAND (arg0, 0),
10401                                              build_int_cst_wide (type,
10402                                                                  lo3, hi3)),
10403                                 arg1);
10404         }
10405
10406       /* (X & Y) | Y is (X, Y).  */
10407       if (TREE_CODE (arg0) == BIT_AND_EXPR
10408           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10409         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10410       /* (X & Y) | X is (Y, X).  */
10411       if (TREE_CODE (arg0) == BIT_AND_EXPR
10412           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10413           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10414         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10415       /* X | (X & Y) is (Y, X).  */
10416       if (TREE_CODE (arg1) == BIT_AND_EXPR
10417           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10418           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10419         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10420       /* X | (Y & X) is (Y, X).  */
10421       if (TREE_CODE (arg1) == BIT_AND_EXPR
10422           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10423           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10424         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10425
10426       t1 = distribute_bit_expr (code, type, arg0, arg1);
10427       if (t1 != NULL_TREE)
10428         return t1;
10429
10430       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10431
10432          This results in more efficient code for machines without a NAND
10433          instruction.  Combine will canonicalize to the first form
10434          which will allow use of NAND instructions provided by the
10435          backend if they exist.  */
10436       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10437           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10438         {
10439           return fold_build1 (BIT_NOT_EXPR, type,
10440                               build2 (BIT_AND_EXPR, type,
10441                                       fold_convert (type,
10442                                                     TREE_OPERAND (arg0, 0)),
10443                                       fold_convert (type,
10444                                                     TREE_OPERAND (arg1, 0))));
10445         }
10446
10447       /* See if this can be simplified into a rotate first.  If that
10448          is unsuccessful continue in the association code.  */
10449       goto bit_rotate;
10450
10451     case BIT_XOR_EXPR:
10452       if (integer_zerop (arg1))
10453         return non_lvalue (fold_convert (type, arg0));
10454       if (integer_all_onesp (arg1))
10455         return fold_build1 (BIT_NOT_EXPR, type, op0);
10456       if (operand_equal_p (arg0, arg1, 0))
10457         return omit_one_operand (type, integer_zero_node, arg0);
10458
10459       /* ~X ^ X is -1.  */
10460       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10461           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10462         {
10463           t1 = fold_convert (type, integer_zero_node);
10464           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10465           return omit_one_operand (type, t1, arg1);
10466         }
10467
10468       /* X ^ ~X is -1.  */
10469       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10470           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10471         {
10472           t1 = fold_convert (type, integer_zero_node);
10473           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10474           return omit_one_operand (type, t1, arg0);
10475         }
10476
10477       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10478          with a constant, and the two constants have no bits in common,
10479          we should treat this as a BIT_IOR_EXPR since this may produce more
10480          simplifications.  */
10481       if (TREE_CODE (arg0) == BIT_AND_EXPR
10482           && TREE_CODE (arg1) == BIT_AND_EXPR
10483           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10484           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10485           && integer_zerop (const_binop (BIT_AND_EXPR,
10486                                          TREE_OPERAND (arg0, 1),
10487                                          TREE_OPERAND (arg1, 1), 0)))
10488         {
10489           code = BIT_IOR_EXPR;
10490           goto bit_ior;
10491         }
10492
10493       /* (X | Y) ^ X -> Y & ~ X*/
10494       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10495           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10496         {
10497           tree t2 = TREE_OPERAND (arg0, 1);
10498           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10499                             arg1);
10500           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10501                             fold_convert (type, t1));
10502           return t1;
10503         }
10504
10505       /* (Y | X) ^ X -> Y & ~ X*/
10506       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10507           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10508         {
10509           tree t2 = TREE_OPERAND (arg0, 0);
10510           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10511                             arg1);
10512           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10513                             fold_convert (type, t1));
10514           return t1;
10515         }
10516
10517       /* X ^ (X | Y) -> Y & ~ X*/
10518       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10519           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10520         {
10521           tree t2 = TREE_OPERAND (arg1, 1);
10522           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10523                             arg0);
10524           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10525                             fold_convert (type, t1));
10526           return t1;
10527         }
10528
10529       /* X ^ (Y | X) -> Y & ~ X*/
10530       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10531           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10532         {
10533           tree t2 = TREE_OPERAND (arg1, 0);
10534           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10535                             arg0);
10536           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10537                             fold_convert (type, t1));
10538           return t1;
10539         }
10540         
10541       /* Convert ~X ^ ~Y to X ^ Y.  */
10542       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10543           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10544         return fold_build2 (code, type,
10545                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10546                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10547
10548       /* Convert ~X ^ C to X ^ ~C.  */
10549       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10550           && TREE_CODE (arg1) == INTEGER_CST)
10551         return fold_build2 (code, type,
10552                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10553                             fold_build1 (BIT_NOT_EXPR, type, arg1));
10554
10555       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
10556       if (TREE_CODE (arg0) == BIT_AND_EXPR
10557           && integer_onep (TREE_OPERAND (arg0, 1))
10558           && integer_onep (arg1))
10559         return fold_build2 (EQ_EXPR, type, arg0,
10560                             build_int_cst (TREE_TYPE (arg0), 0));
10561
10562       /* Fold (X & Y) ^ Y as ~X & Y.  */
10563       if (TREE_CODE (arg0) == BIT_AND_EXPR
10564           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10565         {
10566           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10567           return fold_build2 (BIT_AND_EXPR, type, 
10568                               fold_build1 (BIT_NOT_EXPR, type, tem),
10569                               fold_convert (type, arg1));
10570         }
10571       /* Fold (X & Y) ^ X as ~Y & X.  */
10572       if (TREE_CODE (arg0) == BIT_AND_EXPR
10573           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10574           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10575         {
10576           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10577           return fold_build2 (BIT_AND_EXPR, type,
10578                               fold_build1 (BIT_NOT_EXPR, type, tem),
10579                               fold_convert (type, arg1));
10580         }
10581       /* Fold X ^ (X & Y) as X & ~Y.  */
10582       if (TREE_CODE (arg1) == BIT_AND_EXPR
10583           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10584         {
10585           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10586           return fold_build2 (BIT_AND_EXPR, type,
10587                               fold_convert (type, arg0),
10588                               fold_build1 (BIT_NOT_EXPR, type, tem));
10589         }
10590       /* Fold X ^ (Y & X) as ~Y & X.  */
10591       if (TREE_CODE (arg1) == BIT_AND_EXPR
10592           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10593           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10594         {
10595           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10596           return fold_build2 (BIT_AND_EXPR, type,
10597                               fold_build1 (BIT_NOT_EXPR, type, tem),
10598                               fold_convert (type, arg0));
10599         }
10600
10601       /* See if this can be simplified into a rotate first.  If that
10602          is unsuccessful continue in the association code.  */
10603       goto bit_rotate;
10604
10605     case BIT_AND_EXPR:
10606       if (integer_all_onesp (arg1))
10607         return non_lvalue (fold_convert (type, arg0));
10608       if (integer_zerop (arg1))
10609         return omit_one_operand (type, arg1, arg0);
10610       if (operand_equal_p (arg0, arg1, 0))
10611         return non_lvalue (fold_convert (type, arg0));
10612
10613       /* ~X & X is always zero.  */
10614       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10615           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10616         return omit_one_operand (type, integer_zero_node, arg1);
10617
10618       /* X & ~X is always zero.  */
10619       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10620           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10621         return omit_one_operand (type, integer_zero_node, arg0);
10622
10623       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
10624       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10625           && TREE_CODE (arg1) == INTEGER_CST
10626           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10627         {
10628           tree tmp1 = fold_convert (TREE_TYPE (arg0), arg1);
10629           tree tmp2 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
10630                                    TREE_OPERAND (arg0, 0), tmp1);
10631           tree tmp3 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
10632                                    TREE_OPERAND (arg0, 1), tmp1);
10633           return fold_convert (type,
10634                                fold_build2 (BIT_IOR_EXPR, TREE_TYPE (arg0),
10635                                             tmp2, tmp3));
10636         }
10637
10638       /* (X | Y) & Y is (X, Y).  */
10639       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10640           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10641         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10642       /* (X | Y) & X is (Y, X).  */
10643       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10644           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10645           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10646         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10647       /* X & (X | Y) is (Y, X).  */
10648       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10649           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10650           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10651         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10652       /* X & (Y | X) is (Y, X).  */
10653       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10654           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10655           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10656         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10657
10658       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
10659       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10660           && integer_onep (TREE_OPERAND (arg0, 1))
10661           && integer_onep (arg1))
10662         {
10663           tem = TREE_OPERAND (arg0, 0);
10664           return fold_build2 (EQ_EXPR, type,
10665                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10666                                            build_int_cst (TREE_TYPE (tem), 1)),
10667                               build_int_cst (TREE_TYPE (tem), 0));
10668         }
10669       /* Fold ~X & 1 as (X & 1) == 0.  */
10670       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10671           && integer_onep (arg1))
10672         {
10673           tem = TREE_OPERAND (arg0, 0);
10674           return fold_build2 (EQ_EXPR, type,
10675                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10676                                            build_int_cst (TREE_TYPE (tem), 1)),
10677                               build_int_cst (TREE_TYPE (tem), 0));
10678         }
10679
10680       /* Fold (X ^ Y) & Y as ~X & Y.  */
10681       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10682           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10683         {
10684           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10685           return fold_build2 (BIT_AND_EXPR, type, 
10686                               fold_build1 (BIT_NOT_EXPR, type, tem),
10687                               fold_convert (type, arg1));
10688         }
10689       /* Fold (X ^ Y) & X as ~Y & X.  */
10690       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10691           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10692           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10693         {
10694           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10695           return fold_build2 (BIT_AND_EXPR, type,
10696                               fold_build1 (BIT_NOT_EXPR, type, tem),
10697                               fold_convert (type, arg1));
10698         }
10699       /* Fold X & (X ^ Y) as X & ~Y.  */
10700       if (TREE_CODE (arg1) == BIT_XOR_EXPR
10701           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10702         {
10703           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10704           return fold_build2 (BIT_AND_EXPR, type,
10705                               fold_convert (type, arg0),
10706                               fold_build1 (BIT_NOT_EXPR, type, tem));
10707         }
10708       /* Fold X & (Y ^ X) as ~Y & X.  */
10709       if (TREE_CODE (arg1) == BIT_XOR_EXPR
10710           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10711           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10712         {
10713           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10714           return fold_build2 (BIT_AND_EXPR, type,
10715                               fold_build1 (BIT_NOT_EXPR, type, tem),
10716                               fold_convert (type, arg0));
10717         }
10718
10719       t1 = distribute_bit_expr (code, type, arg0, arg1);
10720       if (t1 != NULL_TREE)
10721         return t1;
10722       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
10723       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10724           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10725         {
10726           unsigned int prec
10727             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10728
10729           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
10730               && (~TREE_INT_CST_LOW (arg1)
10731                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
10732             return fold_convert (type, TREE_OPERAND (arg0, 0));
10733         }
10734
10735       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
10736
10737          This results in more efficient code for machines without a NOR
10738          instruction.  Combine will canonicalize to the first form
10739          which will allow use of NOR instructions provided by the
10740          backend if they exist.  */
10741       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10742           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10743         {
10744           return fold_build1 (BIT_NOT_EXPR, type,
10745                               build2 (BIT_IOR_EXPR, type,
10746                                       fold_convert (type,
10747                                                     TREE_OPERAND (arg0, 0)),
10748                                       fold_convert (type,
10749                                                     TREE_OPERAND (arg1, 0))));
10750         }
10751
10752       /* If arg0 is derived from the address of an object or function, we may
10753          be able to fold this expression using the object or function's
10754          alignment.  */
10755       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
10756         {
10757           unsigned HOST_WIDE_INT modulus, residue;
10758           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
10759
10760           modulus = get_pointer_modulus_and_residue (arg0, &residue);
10761
10762           /* This works because modulus is a power of 2.  If this weren't the
10763              case, we'd have to replace it by its greatest power-of-2
10764              divisor: modulus & -modulus.  */
10765           if (low < modulus)
10766             return build_int_cst (type, residue & low);
10767         }
10768
10769       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
10770               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
10771          if the new mask might be further optimized.  */
10772       if ((TREE_CODE (arg0) == LSHIFT_EXPR
10773            || TREE_CODE (arg0) == RSHIFT_EXPR)
10774           && host_integerp (TREE_OPERAND (arg0, 1), 1)
10775           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
10776           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
10777              < TYPE_PRECISION (TREE_TYPE (arg0))
10778           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
10779           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
10780         {
10781           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
10782           unsigned HOST_WIDE_INT mask
10783             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
10784           unsigned HOST_WIDE_INT newmask, zerobits = 0;
10785           tree shift_type = TREE_TYPE (arg0);
10786
10787           if (TREE_CODE (arg0) == LSHIFT_EXPR)
10788             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
10789           else if (TREE_CODE (arg0) == RSHIFT_EXPR
10790                    && TYPE_PRECISION (TREE_TYPE (arg0))
10791                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
10792             {
10793               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
10794               tree arg00 = TREE_OPERAND (arg0, 0);
10795               /* See if more bits can be proven as zero because of
10796                  zero extension.  */
10797               if (TREE_CODE (arg00) == NOP_EXPR
10798                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
10799                 {
10800                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
10801                   if (TYPE_PRECISION (inner_type)
10802                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
10803                       && TYPE_PRECISION (inner_type) < prec)
10804                     {
10805                       prec = TYPE_PRECISION (inner_type);
10806                       /* See if we can shorten the right shift.  */
10807                       if (shiftc < prec)
10808                         shift_type = inner_type;
10809                     }
10810                 }
10811               zerobits = ~(unsigned HOST_WIDE_INT) 0;
10812               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
10813               zerobits <<= prec - shiftc;
10814               /* For arithmetic shift if sign bit could be set, zerobits
10815                  can contain actually sign bits, so no transformation is
10816                  possible, unless MASK masks them all away.  In that
10817                  case the shift needs to be converted into logical shift.  */
10818               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
10819                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
10820                 {
10821                   if ((mask & zerobits) == 0)
10822                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
10823                   else
10824                     zerobits = 0;
10825                 }
10826             }
10827
10828           /* ((X << 16) & 0xff00) is (X, 0).  */
10829           if ((mask & zerobits) == mask)
10830             return omit_one_operand (type, build_int_cst (type, 0), arg0);
10831
10832           newmask = mask | zerobits;
10833           if (newmask != mask && (newmask & (newmask + 1)) == 0)
10834             {
10835               unsigned int prec;
10836
10837               /* Only do the transformation if NEWMASK is some integer
10838                  mode's mask.  */
10839               for (prec = BITS_PER_UNIT;
10840                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
10841                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
10842                   break;
10843               if (prec < HOST_BITS_PER_WIDE_INT
10844                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
10845                 {
10846                   if (shift_type != TREE_TYPE (arg0))
10847                     {
10848                       tem = fold_build2 (TREE_CODE (arg0), shift_type,
10849                                          fold_convert (shift_type,
10850                                                        TREE_OPERAND (arg0, 0)),
10851                                          TREE_OPERAND (arg0, 1));
10852                       tem = fold_convert (type, tem);
10853                     }
10854                   else
10855                     tem = op0;
10856                   return fold_build2 (BIT_AND_EXPR, type, tem,
10857                                       build_int_cst_type (TREE_TYPE (op1),
10858                                                           newmask));
10859                 }
10860             }
10861         }
10862
10863       goto associate;
10864
10865     case RDIV_EXPR:
10866       /* Don't touch a floating-point divide by zero unless the mode
10867          of the constant can represent infinity.  */
10868       if (TREE_CODE (arg1) == REAL_CST
10869           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10870           && real_zerop (arg1))
10871         return NULL_TREE;
10872
10873       /* Optimize A / A to 1.0 if we don't care about
10874          NaNs or Infinities.  Skip the transformation
10875          for non-real operands.  */
10876       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
10877           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10878           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
10879           && operand_equal_p (arg0, arg1, 0))
10880         {
10881           tree r = build_real (TREE_TYPE (arg0), dconst1);
10882
10883           return omit_two_operands (type, r, arg0, arg1);
10884         }
10885
10886       /* The complex version of the above A / A optimization.  */
10887       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10888           && operand_equal_p (arg0, arg1, 0))
10889         {
10890           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
10891           if (! HONOR_NANS (TYPE_MODE (elem_type))
10892               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
10893             {
10894               tree r = build_real (elem_type, dconst1);
10895               /* omit_two_operands will call fold_convert for us.  */
10896               return omit_two_operands (type, r, arg0, arg1);
10897             }
10898         }
10899
10900       /* (-A) / (-B) -> A / B  */
10901       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10902         return fold_build2 (RDIV_EXPR, type,
10903                             TREE_OPERAND (arg0, 0),
10904                             negate_expr (arg1));
10905       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10906         return fold_build2 (RDIV_EXPR, type,
10907                             negate_expr (arg0),
10908                             TREE_OPERAND (arg1, 0));
10909
10910       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
10911       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10912           && real_onep (arg1))
10913         return non_lvalue (fold_convert (type, arg0));
10914
10915       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
10916       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10917           && real_minus_onep (arg1))
10918         return non_lvalue (fold_convert (type, negate_expr (arg0)));
10919
10920       /* If ARG1 is a constant, we can convert this to a multiply by the
10921          reciprocal.  This does not have the same rounding properties,
10922          so only do this if -freciprocal-math.  We can actually
10923          always safely do it if ARG1 is a power of two, but it's hard to
10924          tell if it is or not in a portable manner.  */
10925       if (TREE_CODE (arg1) == REAL_CST)
10926         {
10927           if (flag_reciprocal_math
10928               && 0 != (tem = const_binop (code, build_real (type, dconst1),
10929                                           arg1, 0)))
10930             return fold_build2 (MULT_EXPR, type, arg0, tem);
10931           /* Find the reciprocal if optimizing and the result is exact.  */
10932           if (optimize)
10933             {
10934               REAL_VALUE_TYPE r;
10935               r = TREE_REAL_CST (arg1);
10936               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
10937                 {
10938                   tem = build_real (type, r);
10939                   return fold_build2 (MULT_EXPR, type,
10940                                       fold_convert (type, arg0), tem);
10941                 }
10942             }
10943         }
10944       /* Convert A/B/C to A/(B*C).  */ 
10945       if (flag_reciprocal_math
10946           && TREE_CODE (arg0) == RDIV_EXPR)
10947         return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
10948                             fold_build2 (MULT_EXPR, type,
10949                                          TREE_OPERAND (arg0, 1), arg1));
10950
10951       /* Convert A/(B/C) to (A/B)*C.  */
10952       if (flag_reciprocal_math
10953           && TREE_CODE (arg1) == RDIV_EXPR)
10954         return fold_build2 (MULT_EXPR, type,
10955                             fold_build2 (RDIV_EXPR, type, arg0,
10956                                          TREE_OPERAND (arg1, 0)),
10957                             TREE_OPERAND (arg1, 1));
10958
10959       /* Convert C1/(X*C2) into (C1/C2)/X.  */
10960       if (flag_reciprocal_math
10961           && TREE_CODE (arg1) == MULT_EXPR
10962           && TREE_CODE (arg0) == REAL_CST
10963           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
10964         {
10965           tree tem = const_binop (RDIV_EXPR, arg0,
10966                                   TREE_OPERAND (arg1, 1), 0);
10967           if (tem)
10968             return fold_build2 (RDIV_EXPR, type, tem,
10969                                 TREE_OPERAND (arg1, 0));
10970         }
10971
10972       if (flag_unsafe_math_optimizations)
10973         {
10974           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10975           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10976
10977           /* Optimize sin(x)/cos(x) as tan(x).  */
10978           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
10979                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
10980                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
10981               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10982                                   CALL_EXPR_ARG (arg1, 0), 0))
10983             {
10984               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
10985
10986               if (tanfn != NULL_TREE)
10987                 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
10988             }
10989
10990           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
10991           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
10992                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
10993                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
10994               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10995                                   CALL_EXPR_ARG (arg1, 0), 0))
10996             {
10997               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
10998
10999               if (tanfn != NULL_TREE)
11000                 {
11001                   tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11002                   return fold_build2 (RDIV_EXPR, type,
11003                                       build_real (type, dconst1), tmp);
11004                 }
11005             }
11006
11007           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11008              NaNs or Infinities.  */
11009           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11010                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11011                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11012             {
11013               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11014               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11015
11016               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11017                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11018                   && operand_equal_p (arg00, arg01, 0))
11019                 {
11020                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11021
11022                   if (cosfn != NULL_TREE)
11023                     return build_call_expr (cosfn, 1, arg00);
11024                 }
11025             }
11026
11027           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11028              NaNs or Infinities.  */
11029           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11030                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11031                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11032             {
11033               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11034               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11035
11036               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11037                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11038                   && operand_equal_p (arg00, arg01, 0))
11039                 {
11040                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11041
11042                   if (cosfn != NULL_TREE)
11043                     {
11044                       tree tmp = build_call_expr (cosfn, 1, arg00);
11045                       return fold_build2 (RDIV_EXPR, type,
11046                                           build_real (type, dconst1),
11047                                           tmp);
11048                     }
11049                 }
11050             }
11051
11052           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11053           if (fcode0 == BUILT_IN_POW
11054               || fcode0 == BUILT_IN_POWF
11055               || fcode0 == BUILT_IN_POWL)
11056             {
11057               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11058               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11059               if (TREE_CODE (arg01) == REAL_CST
11060                   && !TREE_OVERFLOW (arg01)
11061                   && operand_equal_p (arg1, arg00, 0))
11062                 {
11063                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11064                   REAL_VALUE_TYPE c;
11065                   tree arg;
11066
11067                   c = TREE_REAL_CST (arg01);
11068                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11069                   arg = build_real (type, c);
11070                   return build_call_expr (powfn, 2, arg1, arg);
11071                 }
11072             }
11073
11074           /* Optimize a/root(b/c) into a*root(c/b).  */
11075           if (BUILTIN_ROOT_P (fcode1))
11076             {
11077               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11078
11079               if (TREE_CODE (rootarg) == RDIV_EXPR)
11080                 {
11081                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11082                   tree b = TREE_OPERAND (rootarg, 0);
11083                   tree c = TREE_OPERAND (rootarg, 1);
11084
11085                   tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11086
11087                   tmp = build_call_expr (rootfn, 1, tmp);
11088                   return fold_build2 (MULT_EXPR, type, arg0, tmp);
11089                 }
11090             }
11091
11092           /* Optimize x/expN(y) into x*expN(-y).  */
11093           if (BUILTIN_EXPONENT_P (fcode1))
11094             {
11095               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11096               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11097               arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11098               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11099             }
11100
11101           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11102           if (fcode1 == BUILT_IN_POW
11103               || fcode1 == BUILT_IN_POWF
11104               || fcode1 == BUILT_IN_POWL)
11105             {
11106               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11107               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11108               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11109               tree neg11 = fold_convert (type, negate_expr (arg11));
11110               arg1 = build_call_expr (powfn, 2, arg10, neg11);
11111               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11112             }
11113         }
11114       return NULL_TREE;
11115
11116     case TRUNC_DIV_EXPR:
11117     case FLOOR_DIV_EXPR:
11118       /* Simplify A / (B << N) where A and B are positive and B is
11119          a power of 2, to A >> (N + log2(B)).  */
11120       strict_overflow_p = false;
11121       if (TREE_CODE (arg1) == LSHIFT_EXPR
11122           && (TYPE_UNSIGNED (type)
11123               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11124         {
11125           tree sval = TREE_OPERAND (arg1, 0);
11126           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11127             {
11128               tree sh_cnt = TREE_OPERAND (arg1, 1);
11129               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11130
11131               if (strict_overflow_p)
11132                 fold_overflow_warning (("assuming signed overflow does not "
11133                                         "occur when simplifying A / (B << N)"),
11134                                        WARN_STRICT_OVERFLOW_MISC);
11135
11136               sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11137                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
11138               return fold_build2 (RSHIFT_EXPR, type,
11139                                   fold_convert (type, arg0), sh_cnt);
11140             }
11141         }
11142
11143       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11144          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11145       if (INTEGRAL_TYPE_P (type)
11146           && TYPE_UNSIGNED (type)
11147           && code == FLOOR_DIV_EXPR)
11148         return fold_build2 (TRUNC_DIV_EXPR, type, op0, op1);
11149
11150       /* Fall thru */
11151
11152     case ROUND_DIV_EXPR:
11153     case CEIL_DIV_EXPR:
11154     case EXACT_DIV_EXPR:
11155       if (integer_onep (arg1))
11156         return non_lvalue (fold_convert (type, arg0));
11157       if (integer_zerop (arg1))
11158         return NULL_TREE;
11159       /* X / -1 is -X.  */
11160       if (!TYPE_UNSIGNED (type)
11161           && TREE_CODE (arg1) == INTEGER_CST
11162           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11163           && TREE_INT_CST_HIGH (arg1) == -1)
11164         return fold_convert (type, negate_expr (arg0));
11165
11166       /* Convert -A / -B to A / B when the type is signed and overflow is
11167          undefined.  */
11168       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11169           && TREE_CODE (arg0) == NEGATE_EXPR
11170           && negate_expr_p (arg1))
11171         {
11172           if (INTEGRAL_TYPE_P (type))
11173             fold_overflow_warning (("assuming signed overflow does not occur "
11174                                     "when distributing negation across "
11175                                     "division"),
11176                                    WARN_STRICT_OVERFLOW_MISC);
11177           return fold_build2 (code, type,
11178                               fold_convert (type, TREE_OPERAND (arg0, 0)),
11179                               negate_expr (arg1));
11180         }
11181       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11182           && TREE_CODE (arg1) == NEGATE_EXPR
11183           && negate_expr_p (arg0))
11184         {
11185           if (INTEGRAL_TYPE_P (type))
11186             fold_overflow_warning (("assuming signed overflow does not occur "
11187                                     "when distributing negation across "
11188                                     "division"),
11189                                    WARN_STRICT_OVERFLOW_MISC);
11190           return fold_build2 (code, type, negate_expr (arg0),
11191                               TREE_OPERAND (arg1, 0));
11192         }
11193
11194       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11195          operation, EXACT_DIV_EXPR.
11196
11197          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11198          At one time others generated faster code, it's not clear if they do
11199          after the last round to changes to the DIV code in expmed.c.  */
11200       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11201           && multiple_of_p (type, arg0, arg1))
11202         return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11203
11204       strict_overflow_p = false;
11205       if (TREE_CODE (arg1) == INTEGER_CST
11206           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11207                                          &strict_overflow_p)))
11208         {
11209           if (strict_overflow_p)
11210             fold_overflow_warning (("assuming signed overflow does not occur "
11211                                     "when simplifying division"),
11212                                    WARN_STRICT_OVERFLOW_MISC);
11213           return fold_convert (type, tem);
11214         }
11215
11216       return NULL_TREE;
11217
11218     case CEIL_MOD_EXPR:
11219     case FLOOR_MOD_EXPR:
11220     case ROUND_MOD_EXPR:
11221     case TRUNC_MOD_EXPR:
11222       /* X % 1 is always zero, but be sure to preserve any side
11223          effects in X.  */
11224       if (integer_onep (arg1))
11225         return omit_one_operand (type, integer_zero_node, arg0);
11226
11227       /* X % 0, return X % 0 unchanged so that we can get the
11228          proper warnings and errors.  */
11229       if (integer_zerop (arg1))
11230         return NULL_TREE;
11231
11232       /* 0 % X is always zero, but be sure to preserve any side
11233          effects in X.  Place this after checking for X == 0.  */
11234       if (integer_zerop (arg0))
11235         return omit_one_operand (type, integer_zero_node, arg1);
11236
11237       /* X % -1 is zero.  */
11238       if (!TYPE_UNSIGNED (type)
11239           && TREE_CODE (arg1) == INTEGER_CST
11240           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11241           && TREE_INT_CST_HIGH (arg1) == -1)
11242         return omit_one_operand (type, integer_zero_node, arg0);
11243
11244       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11245          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11246       strict_overflow_p = false;
11247       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11248           && (TYPE_UNSIGNED (type)
11249               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11250         {
11251           tree c = arg1;
11252           /* Also optimize A % (C << N)  where C is a power of 2,
11253              to A & ((C << N) - 1).  */
11254           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11255             c = TREE_OPERAND (arg1, 0);
11256
11257           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11258             {
11259               tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11260                                        build_int_cst (TREE_TYPE (arg1), 1));
11261               if (strict_overflow_p)
11262                 fold_overflow_warning (("assuming signed overflow does not "
11263                                         "occur when simplifying "
11264                                         "X % (power of two)"),
11265                                        WARN_STRICT_OVERFLOW_MISC);
11266               return fold_build2 (BIT_AND_EXPR, type,
11267                                   fold_convert (type, arg0),
11268                                   fold_convert (type, mask));
11269             }
11270         }
11271
11272       /* X % -C is the same as X % C.  */
11273       if (code == TRUNC_MOD_EXPR
11274           && !TYPE_UNSIGNED (type)
11275           && TREE_CODE (arg1) == INTEGER_CST
11276           && !TREE_OVERFLOW (arg1)
11277           && TREE_INT_CST_HIGH (arg1) < 0
11278           && !TYPE_OVERFLOW_TRAPS (type)
11279           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11280           && !sign_bit_p (arg1, arg1))
11281         return fold_build2 (code, type, fold_convert (type, arg0),
11282                             fold_convert (type, negate_expr (arg1)));
11283
11284       /* X % -Y is the same as X % Y.  */
11285       if (code == TRUNC_MOD_EXPR
11286           && !TYPE_UNSIGNED (type)
11287           && TREE_CODE (arg1) == NEGATE_EXPR
11288           && !TYPE_OVERFLOW_TRAPS (type))
11289         return fold_build2 (code, type, fold_convert (type, arg0),
11290                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11291
11292       if (TREE_CODE (arg1) == INTEGER_CST
11293           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11294                                          &strict_overflow_p)))
11295         {
11296           if (strict_overflow_p)
11297             fold_overflow_warning (("assuming signed overflow does not occur "
11298                                     "when simplifying modulos"),
11299                                    WARN_STRICT_OVERFLOW_MISC);
11300           return fold_convert (type, tem);
11301         }
11302
11303       return NULL_TREE;
11304
11305     case LROTATE_EXPR:
11306     case RROTATE_EXPR:
11307       if (integer_all_onesp (arg0))
11308         return omit_one_operand (type, arg0, arg1);
11309       goto shift;
11310
11311     case RSHIFT_EXPR:
11312       /* Optimize -1 >> x for arithmetic right shifts.  */
11313       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
11314         return omit_one_operand (type, arg0, arg1);
11315       /* ... fall through ...  */
11316
11317     case LSHIFT_EXPR:
11318     shift:
11319       if (integer_zerop (arg1))
11320         return non_lvalue (fold_convert (type, arg0));
11321       if (integer_zerop (arg0))
11322         return omit_one_operand (type, arg0, arg1);
11323
11324       /* Since negative shift count is not well-defined,
11325          don't try to compute it in the compiler.  */
11326       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11327         return NULL_TREE;
11328
11329       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11330       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11331           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11332           && host_integerp (TREE_OPERAND (arg0, 1), false)
11333           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11334         {
11335           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11336                                + TREE_INT_CST_LOW (arg1));
11337
11338           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11339              being well defined.  */
11340           if (low >= TYPE_PRECISION (type))
11341             {
11342               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11343                 low = low % TYPE_PRECISION (type);
11344               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11345                 return build_int_cst (type, 0);
11346               else
11347                 low = TYPE_PRECISION (type) - 1;
11348             }
11349
11350           return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11351                               build_int_cst (type, low));
11352         }
11353
11354       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11355          into x & ((unsigned)-1 >> c) for unsigned types.  */
11356       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11357            || (TYPE_UNSIGNED (type)
11358                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11359           && host_integerp (arg1, false)
11360           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11361           && host_integerp (TREE_OPERAND (arg0, 1), false)
11362           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11363         {
11364           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11365           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11366           tree lshift;
11367           tree arg00;
11368
11369           if (low0 == low1)
11370             {
11371               arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11372
11373               lshift = build_int_cst (type, -1);
11374               lshift = int_const_binop (code, lshift, arg1, 0);
11375
11376               return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11377             }
11378         }
11379
11380       /* Rewrite an LROTATE_EXPR by a constant into an
11381          RROTATE_EXPR by a new constant.  */
11382       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11383         {
11384           tree tem = build_int_cst (TREE_TYPE (arg1),
11385                                     TYPE_PRECISION (type));
11386           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11387           return fold_build2 (RROTATE_EXPR, type, op0, tem);
11388         }
11389
11390       /* If we have a rotate of a bit operation with the rotate count and
11391          the second operand of the bit operation both constant,
11392          permute the two operations.  */
11393       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11394           && (TREE_CODE (arg0) == BIT_AND_EXPR
11395               || TREE_CODE (arg0) == BIT_IOR_EXPR
11396               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11397           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11398         return fold_build2 (TREE_CODE (arg0), type,
11399                             fold_build2 (code, type,
11400                                          TREE_OPERAND (arg0, 0), arg1),
11401                             fold_build2 (code, type,
11402                                          TREE_OPERAND (arg0, 1), arg1));
11403
11404       /* Two consecutive rotates adding up to the precision of the
11405          type can be ignored.  */
11406       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11407           && TREE_CODE (arg0) == RROTATE_EXPR
11408           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11409           && TREE_INT_CST_HIGH (arg1) == 0
11410           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11411           && ((TREE_INT_CST_LOW (arg1)
11412                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11413               == (unsigned int) TYPE_PRECISION (type)))
11414         return TREE_OPERAND (arg0, 0);
11415
11416       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11417               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11418          if the latter can be further optimized.  */
11419       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11420           && TREE_CODE (arg0) == BIT_AND_EXPR
11421           && TREE_CODE (arg1) == INTEGER_CST
11422           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11423         {
11424           tree mask = fold_build2 (code, type,
11425                                    fold_convert (type, TREE_OPERAND (arg0, 1)),
11426                                    arg1);
11427           tree shift = fold_build2 (code, type,
11428                                     fold_convert (type, TREE_OPERAND (arg0, 0)),
11429                                     arg1);
11430           tem = fold_binary (BIT_AND_EXPR, type, shift, mask);
11431           if (tem)
11432             return tem;
11433         }
11434
11435       return NULL_TREE;
11436
11437     case MIN_EXPR:
11438       if (operand_equal_p (arg0, arg1, 0))
11439         return omit_one_operand (type, arg0, arg1);
11440       if (INTEGRAL_TYPE_P (type)
11441           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11442         return omit_one_operand (type, arg1, arg0);
11443       tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11444       if (tem)
11445         return tem;
11446       goto associate;
11447
11448     case MAX_EXPR:
11449       if (operand_equal_p (arg0, arg1, 0))
11450         return omit_one_operand (type, arg0, arg1);
11451       if (INTEGRAL_TYPE_P (type)
11452           && TYPE_MAX_VALUE (type)
11453           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11454         return omit_one_operand (type, arg1, arg0);
11455       tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11456       if (tem)
11457         return tem;
11458       goto associate;
11459
11460     case TRUTH_ANDIF_EXPR:
11461       /* Note that the operands of this must be ints
11462          and their values must be 0 or 1.
11463          ("true" is a fixed value perhaps depending on the language.)  */
11464       /* If first arg is constant zero, return it.  */
11465       if (integer_zerop (arg0))
11466         return fold_convert (type, arg0);
11467     case TRUTH_AND_EXPR:
11468       /* If either arg is constant true, drop it.  */
11469       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11470         return non_lvalue (fold_convert (type, arg1));
11471       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11472           /* Preserve sequence points.  */
11473           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11474         return non_lvalue (fold_convert (type, arg0));
11475       /* If second arg is constant zero, result is zero, but first arg
11476          must be evaluated.  */
11477       if (integer_zerop (arg1))
11478         return omit_one_operand (type, arg1, arg0);
11479       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11480          case will be handled here.  */
11481       if (integer_zerop (arg0))
11482         return omit_one_operand (type, arg0, arg1);
11483
11484       /* !X && X is always false.  */
11485       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11486           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11487         return omit_one_operand (type, integer_zero_node, arg1);
11488       /* X && !X is always false.  */
11489       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11490           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11491         return omit_one_operand (type, integer_zero_node, arg0);
11492
11493       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
11494          means A >= Y && A != MAX, but in this case we know that
11495          A < X <= MAX.  */
11496
11497       if (!TREE_SIDE_EFFECTS (arg0)
11498           && !TREE_SIDE_EFFECTS (arg1))
11499         {
11500           tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
11501           if (tem && !operand_equal_p (tem, arg0, 0))
11502             return fold_build2 (code, type, tem, arg1);
11503
11504           tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
11505           if (tem && !operand_equal_p (tem, arg1, 0))
11506             return fold_build2 (code, type, arg0, tem);
11507         }
11508
11509     truth_andor:
11510       /* We only do these simplifications if we are optimizing.  */
11511       if (!optimize)
11512         return NULL_TREE;
11513
11514       /* Check for things like (A || B) && (A || C).  We can convert this
11515          to A || (B && C).  Note that either operator can be any of the four
11516          truth and/or operations and the transformation will still be
11517          valid.   Also note that we only care about order for the
11518          ANDIF and ORIF operators.  If B contains side effects, this
11519          might change the truth-value of A.  */
11520       if (TREE_CODE (arg0) == TREE_CODE (arg1)
11521           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11522               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11523               || TREE_CODE (arg0) == TRUTH_AND_EXPR
11524               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11525           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11526         {
11527           tree a00 = TREE_OPERAND (arg0, 0);
11528           tree a01 = TREE_OPERAND (arg0, 1);
11529           tree a10 = TREE_OPERAND (arg1, 0);
11530           tree a11 = TREE_OPERAND (arg1, 1);
11531           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11532                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11533                              && (code == TRUTH_AND_EXPR
11534                                  || code == TRUTH_OR_EXPR));
11535
11536           if (operand_equal_p (a00, a10, 0))
11537             return fold_build2 (TREE_CODE (arg0), type, a00,
11538                                 fold_build2 (code, type, a01, a11));
11539           else if (commutative && operand_equal_p (a00, a11, 0))
11540             return fold_build2 (TREE_CODE (arg0), type, a00,
11541                                 fold_build2 (code, type, a01, a10));
11542           else if (commutative && operand_equal_p (a01, a10, 0))
11543             return fold_build2 (TREE_CODE (arg0), type, a01,
11544                                 fold_build2 (code, type, a00, a11));
11545
11546           /* This case if tricky because we must either have commutative
11547              operators or else A10 must not have side-effects.  */
11548
11549           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
11550                    && operand_equal_p (a01, a11, 0))
11551             return fold_build2 (TREE_CODE (arg0), type,
11552                                 fold_build2 (code, type, a00, a10),
11553                                 a01);
11554         }
11555
11556       /* See if we can build a range comparison.  */
11557       if (0 != (tem = fold_range_test (code, type, op0, op1)))
11558         return tem;
11559
11560       /* Check for the possibility of merging component references.  If our
11561          lhs is another similar operation, try to merge its rhs with our
11562          rhs.  Then try to merge our lhs and rhs.  */
11563       if (TREE_CODE (arg0) == code
11564           && 0 != (tem = fold_truthop (code, type,
11565                                        TREE_OPERAND (arg0, 1), arg1)))
11566         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11567
11568       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
11569         return tem;
11570
11571       return NULL_TREE;
11572
11573     case TRUTH_ORIF_EXPR:
11574       /* Note that the operands of this must be ints
11575          and their values must be 0 or true.
11576          ("true" is a fixed value perhaps depending on the language.)  */
11577       /* If first arg is constant true, return it.  */
11578       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11579         return fold_convert (type, arg0);
11580     case TRUTH_OR_EXPR:
11581       /* If either arg is constant zero, drop it.  */
11582       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
11583         return non_lvalue (fold_convert (type, arg1));
11584       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
11585           /* Preserve sequence points.  */
11586           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11587         return non_lvalue (fold_convert (type, arg0));
11588       /* If second arg is constant true, result is true, but we must
11589          evaluate first arg.  */
11590       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
11591         return omit_one_operand (type, arg1, arg0);
11592       /* Likewise for first arg, but note this only occurs here for
11593          TRUTH_OR_EXPR.  */
11594       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11595         return omit_one_operand (type, arg0, arg1);
11596
11597       /* !X || X is always true.  */
11598       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11599           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11600         return omit_one_operand (type, integer_one_node, arg1);
11601       /* X || !X is always true.  */
11602       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11603           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11604         return omit_one_operand (type, integer_one_node, arg0);
11605
11606       goto truth_andor;
11607
11608     case TRUTH_XOR_EXPR:
11609       /* If the second arg is constant zero, drop it.  */
11610       if (integer_zerop (arg1))
11611         return non_lvalue (fold_convert (type, arg0));
11612       /* If the second arg is constant true, this is a logical inversion.  */
11613       if (integer_onep (arg1))
11614         {
11615           /* Only call invert_truthvalue if operand is a truth value.  */
11616           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
11617             tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
11618           else
11619             tem = invert_truthvalue (arg0);
11620           return non_lvalue (fold_convert (type, tem));
11621         }
11622       /* Identical arguments cancel to zero.  */
11623       if (operand_equal_p (arg0, arg1, 0))
11624         return omit_one_operand (type, integer_zero_node, arg0);
11625
11626       /* !X ^ X is always true.  */
11627       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11628           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11629         return omit_one_operand (type, integer_one_node, arg1);
11630
11631       /* X ^ !X is always true.  */
11632       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11633           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11634         return omit_one_operand (type, integer_one_node, arg0);
11635
11636       return NULL_TREE;
11637
11638     case EQ_EXPR:
11639     case NE_EXPR:
11640       tem = fold_comparison (code, type, op0, op1);
11641       if (tem != NULL_TREE)
11642         return tem;
11643
11644       /* bool_var != 0 becomes bool_var. */
11645       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11646           && code == NE_EXPR)
11647         return non_lvalue (fold_convert (type, arg0));
11648
11649       /* bool_var == 1 becomes bool_var. */
11650       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11651           && code == EQ_EXPR)
11652         return non_lvalue (fold_convert (type, arg0));
11653
11654       /* bool_var != 1 becomes !bool_var. */
11655       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11656           && code == NE_EXPR)
11657         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
11658
11659       /* bool_var == 0 becomes !bool_var. */
11660       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11661           && code == EQ_EXPR)
11662         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
11663
11664       /* If this is an equality comparison of the address of two non-weak,
11665          unaliased symbols neither of which are extern (since we do not
11666          have access to attributes for externs), then we know the result.  */
11667       if (TREE_CODE (arg0) == ADDR_EXPR
11668           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
11669           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
11670           && ! lookup_attribute ("alias",
11671                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
11672           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
11673           && TREE_CODE (arg1) == ADDR_EXPR
11674           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
11675           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
11676           && ! lookup_attribute ("alias",
11677                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
11678           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
11679         {
11680           /* We know that we're looking at the address of two
11681              non-weak, unaliased, static _DECL nodes.
11682
11683              It is both wasteful and incorrect to call operand_equal_p
11684              to compare the two ADDR_EXPR nodes.  It is wasteful in that
11685              all we need to do is test pointer equality for the arguments
11686              to the two ADDR_EXPR nodes.  It is incorrect to use
11687              operand_equal_p as that function is NOT equivalent to a
11688              C equality test.  It can in fact return false for two
11689              objects which would test as equal using the C equality
11690              operator.  */
11691           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
11692           return constant_boolean_node (equal
11693                                         ? code == EQ_EXPR : code != EQ_EXPR,
11694                                         type);
11695         }
11696
11697       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
11698          a MINUS_EXPR of a constant, we can convert it into a comparison with
11699          a revised constant as long as no overflow occurs.  */
11700       if (TREE_CODE (arg1) == INTEGER_CST
11701           && (TREE_CODE (arg0) == PLUS_EXPR
11702               || TREE_CODE (arg0) == MINUS_EXPR)
11703           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11704           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
11705                                       ? MINUS_EXPR : PLUS_EXPR,
11706                                       fold_convert (TREE_TYPE (arg0), arg1),
11707                                       TREE_OPERAND (arg0, 1), 0))
11708           && !TREE_OVERFLOW (tem))
11709         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11710
11711       /* Similarly for a NEGATE_EXPR.  */
11712       if (TREE_CODE (arg0) == NEGATE_EXPR
11713           && TREE_CODE (arg1) == INTEGER_CST
11714           && 0 != (tem = negate_expr (arg1))
11715           && TREE_CODE (tem) == INTEGER_CST
11716           && !TREE_OVERFLOW (tem))
11717         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11718
11719       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
11720       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11721           && TREE_CODE (arg1) == INTEGER_CST
11722           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11723         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11724                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
11725                                          fold_convert (TREE_TYPE (arg0), arg1),
11726                                          TREE_OPERAND (arg0, 1)));
11727
11728       /* Transform comparisons of the form X +- C CMP X.  */
11729       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11730           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11731           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11732           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11733               || POINTER_TYPE_P (TREE_TYPE (arg0))))
11734         {
11735           tree cst = TREE_OPERAND (arg0, 1);
11736
11737           if (code == EQ_EXPR
11738               && !integer_zerop (cst))
11739             return omit_two_operands (type, boolean_false_node,
11740                                       TREE_OPERAND (arg0, 0), arg1);
11741           else
11742             return omit_two_operands (type, boolean_true_node,
11743                                       TREE_OPERAND (arg0, 0), arg1);
11744         }
11745
11746       /* If we have X - Y == 0, we can convert that to X == Y and similarly
11747          for !=.  Don't do this for ordered comparisons due to overflow.  */
11748       if (TREE_CODE (arg0) == MINUS_EXPR
11749           && integer_zerop (arg1))
11750         return fold_build2 (code, type,
11751                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
11752
11753       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
11754       if (TREE_CODE (arg0) == ABS_EXPR
11755           && (integer_zerop (arg1) || real_zerop (arg1)))
11756         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
11757
11758       /* If this is an EQ or NE comparison with zero and ARG0 is
11759          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
11760          two operations, but the latter can be done in one less insn
11761          on machines that have only two-operand insns or on which a
11762          constant cannot be the first operand.  */
11763       if (TREE_CODE (arg0) == BIT_AND_EXPR
11764           && integer_zerop (arg1))
11765         {
11766           tree arg00 = TREE_OPERAND (arg0, 0);
11767           tree arg01 = TREE_OPERAND (arg0, 1);
11768           if (TREE_CODE (arg00) == LSHIFT_EXPR
11769               && integer_onep (TREE_OPERAND (arg00, 0)))
11770             {
11771               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
11772                                       arg01, TREE_OPERAND (arg00, 1));
11773               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
11774                                  build_int_cst (TREE_TYPE (arg0), 1));
11775               return fold_build2 (code, type,
11776                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
11777             }
11778           else if (TREE_CODE (arg01) == LSHIFT_EXPR
11779                    && integer_onep (TREE_OPERAND (arg01, 0)))
11780             {
11781               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
11782                                       arg00, TREE_OPERAND (arg01, 1));
11783               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
11784                                  build_int_cst (TREE_TYPE (arg0), 1));
11785               return fold_build2 (code, type,
11786                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
11787             }
11788         }
11789
11790       /* If this is an NE or EQ comparison of zero against the result of a
11791          signed MOD operation whose second operand is a power of 2, make
11792          the MOD operation unsigned since it is simpler and equivalent.  */
11793       if (integer_zerop (arg1)
11794           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
11795           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
11796               || TREE_CODE (arg0) == CEIL_MOD_EXPR
11797               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
11798               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
11799           && integer_pow2p (TREE_OPERAND (arg0, 1)))
11800         {
11801           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
11802           tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
11803                                      fold_convert (newtype,
11804                                                    TREE_OPERAND (arg0, 0)),
11805                                      fold_convert (newtype,
11806                                                    TREE_OPERAND (arg0, 1)));
11807
11808           return fold_build2 (code, type, newmod,
11809                               fold_convert (newtype, arg1));
11810         }
11811
11812       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
11813          C1 is a valid shift constant, and C2 is a power of two, i.e.
11814          a single bit.  */
11815       if (TREE_CODE (arg0) == BIT_AND_EXPR
11816           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
11817           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
11818              == INTEGER_CST
11819           && integer_pow2p (TREE_OPERAND (arg0, 1))
11820           && integer_zerop (arg1))
11821         {
11822           tree itype = TREE_TYPE (arg0);
11823           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
11824           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
11825
11826           /* Check for a valid shift count.  */
11827           if (TREE_INT_CST_HIGH (arg001) == 0
11828               && TREE_INT_CST_LOW (arg001) < prec)
11829             {
11830               tree arg01 = TREE_OPERAND (arg0, 1);
11831               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
11832               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
11833               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
11834                  can be rewritten as (X & (C2 << C1)) != 0.  */
11835               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
11836                 {
11837                   tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
11838                   tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
11839                   return fold_build2 (code, type, tem, arg1);
11840                 }
11841               /* Otherwise, for signed (arithmetic) shifts,
11842                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
11843                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
11844               else if (!TYPE_UNSIGNED (itype))
11845                 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
11846                                     arg000, build_int_cst (itype, 0));
11847               /* Otherwise, of unsigned (logical) shifts,
11848                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
11849                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
11850               else
11851                 return omit_one_operand (type,
11852                                          code == EQ_EXPR ? integer_one_node
11853                                                          : integer_zero_node,
11854                                          arg000);
11855             }
11856         }
11857
11858       /* If this is an NE comparison of zero with an AND of one, remove the
11859          comparison since the AND will give the correct value.  */
11860       if (code == NE_EXPR
11861           && integer_zerop (arg1)
11862           && TREE_CODE (arg0) == BIT_AND_EXPR
11863           && integer_onep (TREE_OPERAND (arg0, 1)))
11864         return fold_convert (type, arg0);
11865
11866       /* If we have (A & C) == C where C is a power of 2, convert this into
11867          (A & C) != 0.  Similarly for NE_EXPR.  */
11868       if (TREE_CODE (arg0) == BIT_AND_EXPR
11869           && integer_pow2p (TREE_OPERAND (arg0, 1))
11870           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11871         return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11872                             arg0, fold_convert (TREE_TYPE (arg0),
11873                                                 integer_zero_node));
11874
11875       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
11876          bit, then fold the expression into A < 0 or A >= 0.  */
11877       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
11878       if (tem)
11879         return tem;
11880
11881       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
11882          Similarly for NE_EXPR.  */
11883       if (TREE_CODE (arg0) == BIT_AND_EXPR
11884           && TREE_CODE (arg1) == INTEGER_CST
11885           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11886         {
11887           tree notc = fold_build1 (BIT_NOT_EXPR,
11888                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
11889                                    TREE_OPERAND (arg0, 1));
11890           tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11891                                        arg1, notc);
11892           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
11893           if (integer_nonzerop (dandnotc))
11894             return omit_one_operand (type, rslt, arg0);
11895         }
11896
11897       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
11898          Similarly for NE_EXPR.  */
11899       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11900           && TREE_CODE (arg1) == INTEGER_CST
11901           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11902         {
11903           tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
11904           tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11905                                        TREE_OPERAND (arg0, 1), notd);
11906           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
11907           if (integer_nonzerop (candnotd))
11908             return omit_one_operand (type, rslt, arg0);
11909         }
11910
11911       /* Optimize comparisons of strlen vs zero to a compare of the
11912          first character of the string vs zero.  To wit,
11913                 strlen(ptr) == 0   =>  *ptr == 0
11914                 strlen(ptr) != 0   =>  *ptr != 0
11915          Other cases should reduce to one of these two (or a constant)
11916          due to the return value of strlen being unsigned.  */
11917       if (TREE_CODE (arg0) == CALL_EXPR
11918           && integer_zerop (arg1))
11919         {
11920           tree fndecl = get_callee_fndecl (arg0);
11921
11922           if (fndecl
11923               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
11924               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
11925               && call_expr_nargs (arg0) == 1
11926               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
11927             {
11928               tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
11929               return fold_build2 (code, type, iref,
11930                                   build_int_cst (TREE_TYPE (iref), 0));
11931             }
11932         }
11933
11934       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
11935          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
11936       if (TREE_CODE (arg0) == RSHIFT_EXPR
11937           && integer_zerop (arg1)
11938           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11939         {
11940           tree arg00 = TREE_OPERAND (arg0, 0);
11941           tree arg01 = TREE_OPERAND (arg0, 1);
11942           tree itype = TREE_TYPE (arg00);
11943           if (TREE_INT_CST_HIGH (arg01) == 0
11944               && TREE_INT_CST_LOW (arg01)
11945                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
11946             {
11947               if (TYPE_UNSIGNED (itype))
11948                 {
11949                   itype = signed_type_for (itype);
11950                   arg00 = fold_convert (itype, arg00);
11951                 }
11952               return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
11953                                   type, arg00, build_int_cst (itype, 0));
11954             }
11955         }
11956
11957       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
11958       if (integer_zerop (arg1)
11959           && TREE_CODE (arg0) == BIT_XOR_EXPR)
11960         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11961                             TREE_OPERAND (arg0, 1));
11962
11963       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
11964       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11965           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11966         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11967                             build_int_cst (TREE_TYPE (arg1), 0));
11968       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
11969       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11970           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11971           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11972         return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
11973                             build_int_cst (TREE_TYPE (arg1), 0));
11974
11975       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
11976       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11977           && TREE_CODE (arg1) == INTEGER_CST
11978           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11979         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11980                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
11981                                          TREE_OPERAND (arg0, 1), arg1));
11982
11983       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
11984          (X & C) == 0 when C is a single bit.  */
11985       if (TREE_CODE (arg0) == BIT_AND_EXPR
11986           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
11987           && integer_zerop (arg1)
11988           && integer_pow2p (TREE_OPERAND (arg0, 1)))
11989         {
11990           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11991                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
11992                              TREE_OPERAND (arg0, 1));
11993           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
11994                               type, tem, arg1);
11995         }
11996
11997       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
11998          constant C is a power of two, i.e. a single bit.  */
11999       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12000           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12001           && integer_zerop (arg1)
12002           && integer_pow2p (TREE_OPERAND (arg0, 1))
12003           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12004                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12005         {
12006           tree arg00 = TREE_OPERAND (arg0, 0);
12007           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12008                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12009         }
12010
12011       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12012          when is C is a power of two, i.e. a single bit.  */
12013       if (TREE_CODE (arg0) == BIT_AND_EXPR
12014           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12015           && integer_zerop (arg1)
12016           && integer_pow2p (TREE_OPERAND (arg0, 1))
12017           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12018                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12019         {
12020           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12021           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12022                              arg000, TREE_OPERAND (arg0, 1));
12023           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12024                               tem, build_int_cst (TREE_TYPE (tem), 0));
12025         }
12026
12027       if (integer_zerop (arg1)
12028           && tree_expr_nonzero_p (arg0))
12029         {
12030           tree res = constant_boolean_node (code==NE_EXPR, type);
12031           return omit_one_operand (type, res, arg0);
12032         }
12033
12034       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12035       if (TREE_CODE (arg0) == NEGATE_EXPR
12036           && TREE_CODE (arg1) == NEGATE_EXPR)
12037         return fold_build2 (code, type,
12038                             TREE_OPERAND (arg0, 0),
12039                             TREE_OPERAND (arg1, 0));
12040
12041       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12042       if (TREE_CODE (arg0) == BIT_AND_EXPR
12043           && TREE_CODE (arg1) == BIT_AND_EXPR)
12044         {
12045           tree arg00 = TREE_OPERAND (arg0, 0);
12046           tree arg01 = TREE_OPERAND (arg0, 1);
12047           tree arg10 = TREE_OPERAND (arg1, 0);
12048           tree arg11 = TREE_OPERAND (arg1, 1);
12049           tree itype = TREE_TYPE (arg0);
12050
12051           if (operand_equal_p (arg01, arg11, 0))
12052             return fold_build2 (code, type,
12053                                 fold_build2 (BIT_AND_EXPR, itype,
12054                                              fold_build2 (BIT_XOR_EXPR, itype,
12055                                                           arg00, arg10),
12056                                              arg01),
12057                                 build_int_cst (itype, 0));
12058
12059           if (operand_equal_p (arg01, arg10, 0))
12060             return fold_build2 (code, type,
12061                                 fold_build2 (BIT_AND_EXPR, itype,
12062                                              fold_build2 (BIT_XOR_EXPR, itype,
12063                                                           arg00, arg11),
12064                                              arg01),
12065                                 build_int_cst (itype, 0));
12066
12067           if (operand_equal_p (arg00, arg11, 0))
12068             return fold_build2 (code, type,
12069                                 fold_build2 (BIT_AND_EXPR, itype,
12070                                              fold_build2 (BIT_XOR_EXPR, itype,
12071                                                           arg01, arg10),
12072                                              arg00),
12073                                 build_int_cst (itype, 0));
12074
12075           if (operand_equal_p (arg00, arg10, 0))
12076             return fold_build2 (code, type,
12077                                 fold_build2 (BIT_AND_EXPR, itype,
12078                                              fold_build2 (BIT_XOR_EXPR, itype,
12079                                                           arg01, arg11),
12080                                              arg00),
12081                                 build_int_cst (itype, 0));
12082         }
12083
12084       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12085           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12086         {
12087           tree arg00 = TREE_OPERAND (arg0, 0);
12088           tree arg01 = TREE_OPERAND (arg0, 1);
12089           tree arg10 = TREE_OPERAND (arg1, 0);
12090           tree arg11 = TREE_OPERAND (arg1, 1);
12091           tree itype = TREE_TYPE (arg0);
12092
12093           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12094              operand_equal_p guarantees no side-effects so we don't need
12095              to use omit_one_operand on Z.  */
12096           if (operand_equal_p (arg01, arg11, 0))
12097             return fold_build2 (code, type, arg00, arg10);
12098           if (operand_equal_p (arg01, arg10, 0))
12099             return fold_build2 (code, type, arg00, arg11);
12100           if (operand_equal_p (arg00, arg11, 0))
12101             return fold_build2 (code, type, arg01, arg10);
12102           if (operand_equal_p (arg00, arg10, 0))
12103             return fold_build2 (code, type, arg01, arg11);
12104
12105           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12106           if (TREE_CODE (arg01) == INTEGER_CST
12107               && TREE_CODE (arg11) == INTEGER_CST)
12108             return fold_build2 (code, type,
12109                                 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12110                                              fold_build2 (BIT_XOR_EXPR, itype,
12111                                                           arg01, arg11)),
12112                                 arg10);
12113         }
12114
12115       /* Attempt to simplify equality/inequality comparisons of complex
12116          values.  Only lower the comparison if the result is known or
12117          can be simplified to a single scalar comparison.  */
12118       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12119            || TREE_CODE (arg0) == COMPLEX_CST)
12120           && (TREE_CODE (arg1) == COMPLEX_EXPR
12121               || TREE_CODE (arg1) == COMPLEX_CST))
12122         {
12123           tree real0, imag0, real1, imag1;
12124           tree rcond, icond;
12125
12126           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12127             {
12128               real0 = TREE_OPERAND (arg0, 0);
12129               imag0 = TREE_OPERAND (arg0, 1);
12130             }
12131           else
12132             {
12133               real0 = TREE_REALPART (arg0);
12134               imag0 = TREE_IMAGPART (arg0);
12135             }
12136
12137           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12138             {
12139               real1 = TREE_OPERAND (arg1, 0);
12140               imag1 = TREE_OPERAND (arg1, 1);
12141             }
12142           else
12143             {
12144               real1 = TREE_REALPART (arg1);
12145               imag1 = TREE_IMAGPART (arg1);
12146             }
12147
12148           rcond = fold_binary (code, type, real0, real1);
12149           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12150             {
12151               if (integer_zerop (rcond))
12152                 {
12153                   if (code == EQ_EXPR)
12154                     return omit_two_operands (type, boolean_false_node,
12155                                               imag0, imag1);
12156                   return fold_build2 (NE_EXPR, type, imag0, imag1);
12157                 }
12158               else
12159                 {
12160                   if (code == NE_EXPR)
12161                     return omit_two_operands (type, boolean_true_node,
12162                                               imag0, imag1);
12163                   return fold_build2 (EQ_EXPR, type, imag0, imag1);
12164                 }
12165             }
12166
12167           icond = fold_binary (code, type, imag0, imag1);
12168           if (icond && TREE_CODE (icond) == INTEGER_CST)
12169             {
12170               if (integer_zerop (icond))
12171                 {
12172                   if (code == EQ_EXPR)
12173                     return omit_two_operands (type, boolean_false_node,
12174                                               real0, real1);
12175                   return fold_build2 (NE_EXPR, type, real0, real1);
12176                 }
12177               else
12178                 {
12179                   if (code == NE_EXPR)
12180                     return omit_two_operands (type, boolean_true_node,
12181                                               real0, real1);
12182                   return fold_build2 (EQ_EXPR, type, real0, real1);
12183                 }
12184             }
12185         }
12186
12187       return NULL_TREE;
12188
12189     case LT_EXPR:
12190     case GT_EXPR:
12191     case LE_EXPR:
12192     case GE_EXPR:
12193       tem = fold_comparison (code, type, op0, op1);
12194       if (tem != NULL_TREE)
12195         return tem;
12196
12197       /* Transform comparisons of the form X +- C CMP X.  */
12198       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12199           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12200           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12201                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12202               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12203                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12204         {
12205           tree arg01 = TREE_OPERAND (arg0, 1);
12206           enum tree_code code0 = TREE_CODE (arg0);
12207           int is_positive;
12208
12209           if (TREE_CODE (arg01) == REAL_CST)
12210             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12211           else
12212             is_positive = tree_int_cst_sgn (arg01);
12213
12214           /* (X - c) > X becomes false.  */
12215           if (code == GT_EXPR
12216               && ((code0 == MINUS_EXPR && is_positive >= 0)
12217                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12218             {
12219               if (TREE_CODE (arg01) == INTEGER_CST
12220                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12221                 fold_overflow_warning (("assuming signed overflow does not "
12222                                         "occur when assuming that (X - c) > X "
12223                                         "is always false"),
12224                                        WARN_STRICT_OVERFLOW_ALL);
12225               return constant_boolean_node (0, type);
12226             }
12227
12228           /* Likewise (X + c) < X becomes false.  */
12229           if (code == LT_EXPR
12230               && ((code0 == PLUS_EXPR && is_positive >= 0)
12231                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12232             {
12233               if (TREE_CODE (arg01) == INTEGER_CST
12234                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12235                 fold_overflow_warning (("assuming signed overflow does not "
12236                                         "occur when assuming that "
12237                                         "(X + c) < X is always false"),
12238                                        WARN_STRICT_OVERFLOW_ALL);
12239               return constant_boolean_node (0, type);
12240             }
12241
12242           /* Convert (X - c) <= X to true.  */
12243           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12244               && code == LE_EXPR
12245               && ((code0 == MINUS_EXPR && is_positive >= 0)
12246                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12247             {
12248               if (TREE_CODE (arg01) == INTEGER_CST
12249                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12250                 fold_overflow_warning (("assuming signed overflow does not "
12251                                         "occur when assuming that "
12252                                         "(X - c) <= X is always true"),
12253                                        WARN_STRICT_OVERFLOW_ALL);
12254               return constant_boolean_node (1, type);
12255             }
12256
12257           /* Convert (X + c) >= X to true.  */
12258           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12259               && code == GE_EXPR
12260               && ((code0 == PLUS_EXPR && is_positive >= 0)
12261                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12262             {
12263               if (TREE_CODE (arg01) == INTEGER_CST
12264                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12265                 fold_overflow_warning (("assuming signed overflow does not "
12266                                         "occur when assuming that "
12267                                         "(X + c) >= X is always true"),
12268                                        WARN_STRICT_OVERFLOW_ALL);
12269               return constant_boolean_node (1, type);
12270             }
12271
12272           if (TREE_CODE (arg01) == INTEGER_CST)
12273             {
12274               /* Convert X + c > X and X - c < X to true for integers.  */
12275               if (code == GT_EXPR
12276                   && ((code0 == PLUS_EXPR && is_positive > 0)
12277                       || (code0 == MINUS_EXPR && is_positive < 0)))
12278                 {
12279                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12280                     fold_overflow_warning (("assuming signed overflow does "
12281                                             "not occur when assuming that "
12282                                             "(X + c) > X is always true"),
12283                                            WARN_STRICT_OVERFLOW_ALL);
12284                   return constant_boolean_node (1, type);
12285                 }
12286
12287               if (code == LT_EXPR
12288                   && ((code0 == MINUS_EXPR && is_positive > 0)
12289                       || (code0 == PLUS_EXPR && is_positive < 0)))
12290                 {
12291                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12292                     fold_overflow_warning (("assuming signed overflow does "
12293                                             "not occur when assuming that "
12294                                             "(X - c) < X is always true"),
12295                                            WARN_STRICT_OVERFLOW_ALL);
12296                   return constant_boolean_node (1, type);
12297                 }
12298
12299               /* Convert X + c <= X and X - c >= X to false for integers.  */
12300               if (code == LE_EXPR
12301                   && ((code0 == PLUS_EXPR && is_positive > 0)
12302                       || (code0 == MINUS_EXPR && is_positive < 0)))
12303                 {
12304                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12305                     fold_overflow_warning (("assuming signed overflow does "
12306                                             "not occur when assuming that "
12307                                             "(X + c) <= X is always false"),
12308                                            WARN_STRICT_OVERFLOW_ALL);
12309                   return constant_boolean_node (0, type);
12310                 }
12311
12312               if (code == GE_EXPR
12313                   && ((code0 == MINUS_EXPR && is_positive > 0)
12314                       || (code0 == PLUS_EXPR && is_positive < 0)))
12315                 {
12316                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12317                     fold_overflow_warning (("assuming signed overflow does "
12318                                             "not occur when assuming that "
12319                                             "(X - c) >= X is always false"),
12320                                            WARN_STRICT_OVERFLOW_ALL);
12321                   return constant_boolean_node (0, type);
12322                 }
12323             }
12324         }
12325
12326       /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
12327          This transformation affects the cases which are handled in later
12328          optimizations involving comparisons with non-negative constants.  */
12329       if (TREE_CODE (arg1) == INTEGER_CST
12330           && TREE_CODE (arg0) != INTEGER_CST
12331           && tree_int_cst_sgn (arg1) > 0)
12332         {
12333           if (code == GE_EXPR)
12334             {
12335               arg1 = const_binop (MINUS_EXPR, arg1,
12336                                   build_int_cst (TREE_TYPE (arg1), 1), 0);
12337               return fold_build2 (GT_EXPR, type, arg0,
12338                                   fold_convert (TREE_TYPE (arg0), arg1));
12339             }
12340           if (code == LT_EXPR)
12341             {
12342               arg1 = const_binop (MINUS_EXPR, arg1,
12343                                   build_int_cst (TREE_TYPE (arg1), 1), 0);
12344               return fold_build2 (LE_EXPR, type, arg0,
12345                                   fold_convert (TREE_TYPE (arg0), arg1));
12346             }
12347         }
12348
12349       /* Comparisons with the highest or lowest possible integer of
12350          the specified precision will have known values.  */
12351       {
12352         tree arg1_type = TREE_TYPE (arg1);
12353         unsigned int width = TYPE_PRECISION (arg1_type);
12354
12355         if (TREE_CODE (arg1) == INTEGER_CST
12356             && !TREE_OVERFLOW (arg1)
12357             && width <= 2 * HOST_BITS_PER_WIDE_INT
12358             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12359           {
12360             HOST_WIDE_INT signed_max_hi;
12361             unsigned HOST_WIDE_INT signed_max_lo;
12362             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12363
12364             if (width <= HOST_BITS_PER_WIDE_INT)
12365               {
12366                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12367                                 - 1;
12368                 signed_max_hi = 0;
12369                 max_hi = 0;
12370
12371                 if (TYPE_UNSIGNED (arg1_type))
12372                   {
12373                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12374                     min_lo = 0;
12375                     min_hi = 0;
12376                   }
12377                 else
12378                   {
12379                     max_lo = signed_max_lo;
12380                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12381                     min_hi = -1;
12382                   }
12383               }
12384             else
12385               {
12386                 width -= HOST_BITS_PER_WIDE_INT;
12387                 signed_max_lo = -1;
12388                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12389                                 - 1;
12390                 max_lo = -1;
12391                 min_lo = 0;
12392
12393                 if (TYPE_UNSIGNED (arg1_type))
12394                   {
12395                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12396                     min_hi = 0;
12397                   }
12398                 else
12399                   {
12400                     max_hi = signed_max_hi;
12401                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12402                   }
12403               }
12404
12405             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12406                 && TREE_INT_CST_LOW (arg1) == max_lo)
12407               switch (code)
12408                 {
12409                 case GT_EXPR:
12410                   return omit_one_operand (type, integer_zero_node, arg0);
12411
12412                 case GE_EXPR:
12413                   return fold_build2 (EQ_EXPR, type, op0, op1);
12414
12415                 case LE_EXPR:
12416                   return omit_one_operand (type, integer_one_node, arg0);
12417
12418                 case LT_EXPR:
12419                   return fold_build2 (NE_EXPR, type, op0, op1);
12420
12421                 /* The GE_EXPR and LT_EXPR cases above are not normally
12422                    reached because of previous transformations.  */
12423
12424                 default:
12425                   break;
12426                 }
12427             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12428                      == max_hi
12429                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12430               switch (code)
12431                 {
12432                 case GT_EXPR:
12433                   arg1 = const_binop (PLUS_EXPR, arg1,
12434                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12435                   return fold_build2 (EQ_EXPR, type,
12436                                       fold_convert (TREE_TYPE (arg1), arg0),
12437                                       arg1);
12438                 case LE_EXPR:
12439                   arg1 = const_binop (PLUS_EXPR, arg1,
12440                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12441                   return fold_build2 (NE_EXPR, type,
12442                                       fold_convert (TREE_TYPE (arg1), arg0),
12443                                       arg1);
12444                 default:
12445                   break;
12446                 }
12447             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12448                      == min_hi
12449                      && TREE_INT_CST_LOW (arg1) == min_lo)
12450               switch (code)
12451                 {
12452                 case LT_EXPR:
12453                   return omit_one_operand (type, integer_zero_node, arg0);
12454
12455                 case LE_EXPR:
12456                   return fold_build2 (EQ_EXPR, type, op0, op1);
12457
12458                 case GE_EXPR:
12459                   return omit_one_operand (type, integer_one_node, arg0);
12460
12461                 case GT_EXPR:
12462                   return fold_build2 (NE_EXPR, type, op0, op1);
12463
12464                 default:
12465                   break;
12466                 }
12467             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12468                      == min_hi
12469                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12470               switch (code)
12471                 {
12472                 case GE_EXPR:
12473                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12474                   return fold_build2 (NE_EXPR, type,
12475                                       fold_convert (TREE_TYPE (arg1), arg0),
12476                                       arg1);
12477                 case LT_EXPR:
12478                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12479                   return fold_build2 (EQ_EXPR, type,
12480                                       fold_convert (TREE_TYPE (arg1), arg0),
12481                                       arg1);
12482                 default:
12483                   break;
12484                 }
12485
12486             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12487                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
12488                      && TYPE_UNSIGNED (arg1_type)
12489                      /* We will flip the signedness of the comparison operator
12490                         associated with the mode of arg1, so the sign bit is
12491                         specified by this mode.  Check that arg1 is the signed
12492                         max associated with this sign bit.  */
12493                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12494                      /* signed_type does not work on pointer types.  */
12495                      && INTEGRAL_TYPE_P (arg1_type))
12496               {
12497                 /* The following case also applies to X < signed_max+1
12498                    and X >= signed_max+1 because previous transformations.  */
12499                 if (code == LE_EXPR || code == GT_EXPR)
12500                   {
12501                     tree st;
12502                     st = signed_type_for (TREE_TYPE (arg1));
12503                     return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
12504                                         type, fold_convert (st, arg0),
12505                                         build_int_cst (st, 0));
12506                   }
12507               }
12508           }
12509       }
12510
12511       /* If we are comparing an ABS_EXPR with a constant, we can
12512          convert all the cases into explicit comparisons, but they may
12513          well not be faster than doing the ABS and one comparison.
12514          But ABS (X) <= C is a range comparison, which becomes a subtraction
12515          and a comparison, and is probably faster.  */
12516       if (code == LE_EXPR
12517           && TREE_CODE (arg1) == INTEGER_CST
12518           && TREE_CODE (arg0) == ABS_EXPR
12519           && ! TREE_SIDE_EFFECTS (arg0)
12520           && (0 != (tem = negate_expr (arg1)))
12521           && TREE_CODE (tem) == INTEGER_CST
12522           && !TREE_OVERFLOW (tem))
12523         return fold_build2 (TRUTH_ANDIF_EXPR, type,
12524                             build2 (GE_EXPR, type,
12525                                     TREE_OPERAND (arg0, 0), tem),
12526                             build2 (LE_EXPR, type,
12527                                     TREE_OPERAND (arg0, 0), arg1));
12528
12529       /* Convert ABS_EXPR<x> >= 0 to true.  */
12530       strict_overflow_p = false;
12531       if (code == GE_EXPR
12532           && (integer_zerop (arg1)
12533               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
12534                   && real_zerop (arg1)))
12535           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12536         {
12537           if (strict_overflow_p)
12538             fold_overflow_warning (("assuming signed overflow does not occur "
12539                                     "when simplifying comparison of "
12540                                     "absolute value and zero"),
12541                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
12542           return omit_one_operand (type, integer_one_node, arg0);
12543         }
12544
12545       /* Convert ABS_EXPR<x> < 0 to false.  */
12546       strict_overflow_p = false;
12547       if (code == LT_EXPR
12548           && (integer_zerop (arg1) || real_zerop (arg1))
12549           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12550         {
12551           if (strict_overflow_p)
12552             fold_overflow_warning (("assuming signed overflow does not occur "
12553                                     "when simplifying comparison of "
12554                                     "absolute value and zero"),
12555                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
12556           return omit_one_operand (type, integer_zero_node, arg0);
12557         }
12558
12559       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12560          and similarly for >= into !=.  */
12561       if ((code == LT_EXPR || code == GE_EXPR)
12562           && TYPE_UNSIGNED (TREE_TYPE (arg0))
12563           && TREE_CODE (arg1) == LSHIFT_EXPR
12564           && integer_onep (TREE_OPERAND (arg1, 0)))
12565         return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12566                        build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12567                                TREE_OPERAND (arg1, 1)),
12568                        build_int_cst (TREE_TYPE (arg0), 0));
12569
12570       if ((code == LT_EXPR || code == GE_EXPR)
12571           && TYPE_UNSIGNED (TREE_TYPE (arg0))
12572           && (TREE_CODE (arg1) == NOP_EXPR
12573               || TREE_CODE (arg1) == CONVERT_EXPR)
12574           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12575           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12576         return
12577           build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12578                   fold_convert (TREE_TYPE (arg0),
12579                                 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12580                                         TREE_OPERAND (TREE_OPERAND (arg1, 0),
12581                                                       1))),
12582                   build_int_cst (TREE_TYPE (arg0), 0));
12583
12584       return NULL_TREE;
12585
12586     case UNORDERED_EXPR:
12587     case ORDERED_EXPR:
12588     case UNLT_EXPR:
12589     case UNLE_EXPR:
12590     case UNGT_EXPR:
12591     case UNGE_EXPR:
12592     case UNEQ_EXPR:
12593     case LTGT_EXPR:
12594       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
12595         {
12596           t1 = fold_relational_const (code, type, arg0, arg1);
12597           if (t1 != NULL_TREE)
12598             return t1;
12599         }
12600
12601       /* If the first operand is NaN, the result is constant.  */
12602       if (TREE_CODE (arg0) == REAL_CST
12603           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
12604           && (code != LTGT_EXPR || ! flag_trapping_math))
12605         {
12606           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12607                ? integer_zero_node
12608                : integer_one_node;
12609           return omit_one_operand (type, t1, arg1);
12610         }
12611
12612       /* If the second operand is NaN, the result is constant.  */
12613       if (TREE_CODE (arg1) == REAL_CST
12614           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
12615           && (code != LTGT_EXPR || ! flag_trapping_math))
12616         {
12617           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12618                ? integer_zero_node
12619                : integer_one_node;
12620           return omit_one_operand (type, t1, arg0);
12621         }
12622
12623       /* Simplify unordered comparison of something with itself.  */
12624       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
12625           && operand_equal_p (arg0, arg1, 0))
12626         return constant_boolean_node (1, type);
12627
12628       if (code == LTGT_EXPR
12629           && !flag_trapping_math
12630           && operand_equal_p (arg0, arg1, 0))
12631         return constant_boolean_node (0, type);
12632
12633       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
12634       {
12635         tree targ0 = strip_float_extensions (arg0);
12636         tree targ1 = strip_float_extensions (arg1);
12637         tree newtype = TREE_TYPE (targ0);
12638
12639         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
12640           newtype = TREE_TYPE (targ1);
12641
12642         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
12643           return fold_build2 (code, type, fold_convert (newtype, targ0),
12644                               fold_convert (newtype, targ1));
12645       }
12646
12647       return NULL_TREE;
12648
12649     case COMPOUND_EXPR:
12650       /* When pedantic, a compound expression can be neither an lvalue
12651          nor an integer constant expression.  */
12652       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12653         return NULL_TREE;
12654       /* Don't let (0, 0) be null pointer constant.  */
12655       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
12656                                  : fold_convert (type, arg1);
12657       return pedantic_non_lvalue (tem);
12658
12659     case COMPLEX_EXPR:
12660       if ((TREE_CODE (arg0) == REAL_CST
12661            && TREE_CODE (arg1) == REAL_CST)
12662           || (TREE_CODE (arg0) == INTEGER_CST
12663               && TREE_CODE (arg1) == INTEGER_CST))
12664         return build_complex (type, arg0, arg1);
12665       return NULL_TREE;
12666
12667     case ASSERT_EXPR:
12668       /* An ASSERT_EXPR should never be passed to fold_binary.  */
12669       gcc_unreachable ();
12670
12671     default:
12672       return NULL_TREE;
12673     } /* switch (code) */
12674 }
12675
12676 /* Callback for walk_tree, looking for LABEL_EXPR.
12677    Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
12678    Do not check the sub-tree of GOTO_EXPR.  */
12679
12680 static tree
12681 contains_label_1 (tree *tp,
12682                   int *walk_subtrees,
12683                   void *data ATTRIBUTE_UNUSED)
12684 {
12685   switch (TREE_CODE (*tp))
12686     {
12687     case LABEL_EXPR:
12688       return *tp;
12689     case GOTO_EXPR:
12690       *walk_subtrees = 0;
12691     /* no break */
12692     default:
12693       return NULL_TREE;
12694     }
12695 }
12696
12697 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
12698    accessible from outside the sub-tree. Returns NULL_TREE if no
12699    addressable label is found.  */
12700
12701 static bool
12702 contains_label_p (tree st)
12703 {
12704   return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
12705 }
12706
12707 /* Fold a ternary expression of code CODE and type TYPE with operands
12708    OP0, OP1, and OP2.  Return the folded expression if folding is
12709    successful.  Otherwise, return NULL_TREE.  */
12710
12711 tree
12712 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
12713 {
12714   tree tem;
12715   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
12716   enum tree_code_class kind = TREE_CODE_CLASS (code);
12717
12718   gcc_assert (IS_EXPR_CODE_CLASS (kind)
12719               && TREE_CODE_LENGTH (code) == 3);
12720
12721   /* Strip any conversions that don't change the mode.  This is safe
12722      for every expression, except for a comparison expression because
12723      its signedness is derived from its operands.  So, in the latter
12724      case, only strip conversions that don't change the signedness.
12725
12726      Note that this is done as an internal manipulation within the
12727      constant folder, in order to find the simplest representation of
12728      the arguments so that their form can be studied.  In any cases,
12729      the appropriate type conversions should be put back in the tree
12730      that will get out of the constant folder.  */
12731   if (op0)
12732     {
12733       arg0 = op0;
12734       STRIP_NOPS (arg0);
12735     }
12736
12737   if (op1)
12738     {
12739       arg1 = op1;
12740       STRIP_NOPS (arg1);
12741     }
12742
12743   switch (code)
12744     {
12745     case COMPONENT_REF:
12746       if (TREE_CODE (arg0) == CONSTRUCTOR
12747           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12748         {
12749           unsigned HOST_WIDE_INT idx;
12750           tree field, value;
12751           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12752             if (field == arg1)
12753               return value;
12754         }
12755       return NULL_TREE;
12756
12757     case COND_EXPR:
12758       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12759          so all simple results must be passed through pedantic_non_lvalue.  */
12760       if (TREE_CODE (arg0) == INTEGER_CST)
12761         {
12762           tree unused_op = integer_zerop (arg0) ? op1 : op2;
12763           tem = integer_zerop (arg0) ? op2 : op1;
12764           /* Only optimize constant conditions when the selected branch
12765              has the same type as the COND_EXPR.  This avoids optimizing
12766              away "c ? x : throw", where the throw has a void type.
12767              Avoid throwing away that operand which contains label.  */
12768           if ((!TREE_SIDE_EFFECTS (unused_op)
12769                || !contains_label_p (unused_op))
12770               && (! VOID_TYPE_P (TREE_TYPE (tem))
12771                   || VOID_TYPE_P (type)))
12772             return pedantic_non_lvalue (tem);
12773           return NULL_TREE;
12774         }
12775       if (operand_equal_p (arg1, op2, 0))
12776         return pedantic_omit_one_operand (type, arg1, arg0);
12777
12778       /* If we have A op B ? A : C, we may be able to convert this to a
12779          simpler expression, depending on the operation and the values
12780          of B and C.  Signed zeros prevent all of these transformations,
12781          for reasons given above each one.
12782
12783          Also try swapping the arguments and inverting the conditional.  */
12784       if (COMPARISON_CLASS_P (arg0)
12785           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12786                                              arg1, TREE_OPERAND (arg0, 1))
12787           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
12788         {
12789           tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
12790           if (tem)
12791             return tem;
12792         }
12793
12794       if (COMPARISON_CLASS_P (arg0)
12795           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12796                                              op2,
12797                                              TREE_OPERAND (arg0, 1))
12798           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
12799         {
12800           tem = fold_truth_not_expr (arg0);
12801           if (tem && COMPARISON_CLASS_P (tem))
12802             {
12803               tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
12804               if (tem)
12805                 return tem;
12806             }
12807         }
12808
12809       /* If the second operand is simpler than the third, swap them
12810          since that produces better jump optimization results.  */
12811       if (truth_value_p (TREE_CODE (arg0))
12812           && tree_swap_operands_p (op1, op2, false))
12813         {
12814           /* See if this can be inverted.  If it can't, possibly because
12815              it was a floating-point inequality comparison, don't do
12816              anything.  */
12817           tem = fold_truth_not_expr (arg0);
12818           if (tem)
12819             return fold_build3 (code, type, tem, op2, op1);
12820         }
12821
12822       /* Convert A ? 1 : 0 to simply A.  */
12823       if (integer_onep (op1)
12824           && integer_zerop (op2)
12825           /* If we try to convert OP0 to our type, the
12826              call to fold will try to move the conversion inside
12827              a COND, which will recurse.  In that case, the COND_EXPR
12828              is probably the best choice, so leave it alone.  */
12829           && type == TREE_TYPE (arg0))
12830         return pedantic_non_lvalue (arg0);
12831
12832       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
12833          over COND_EXPR in cases such as floating point comparisons.  */
12834       if (integer_zerop (op1)
12835           && integer_onep (op2)
12836           && truth_value_p (TREE_CODE (arg0)))
12837         return pedantic_non_lvalue (fold_convert (type,
12838                                                   invert_truthvalue (arg0)));
12839
12840       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
12841       if (TREE_CODE (arg0) == LT_EXPR
12842           && integer_zerop (TREE_OPERAND (arg0, 1))
12843           && integer_zerop (op2)
12844           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12845         {
12846           /* sign_bit_p only checks ARG1 bits within A's precision.
12847              If <sign bit of A> has wider type than A, bits outside
12848              of A's precision in <sign bit of A> need to be checked.
12849              If they are all 0, this optimization needs to be done
12850              in unsigned A's type, if they are all 1 in signed A's type,
12851              otherwise this can't be done.  */
12852           if (TYPE_PRECISION (TREE_TYPE (tem))
12853               < TYPE_PRECISION (TREE_TYPE (arg1))
12854               && TYPE_PRECISION (TREE_TYPE (tem))
12855                  < TYPE_PRECISION (type))
12856             {
12857               unsigned HOST_WIDE_INT mask_lo;
12858               HOST_WIDE_INT mask_hi;
12859               int inner_width, outer_width;
12860               tree tem_type;
12861
12862               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12863               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12864               if (outer_width > TYPE_PRECISION (type))
12865                 outer_width = TYPE_PRECISION (type);
12866
12867               if (outer_width > HOST_BITS_PER_WIDE_INT)
12868                 {
12869                   mask_hi = ((unsigned HOST_WIDE_INT) -1
12870                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
12871                   mask_lo = -1;
12872                 }
12873               else
12874                 {
12875                   mask_hi = 0;
12876                   mask_lo = ((unsigned HOST_WIDE_INT) -1
12877                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
12878                 }
12879               if (inner_width > HOST_BITS_PER_WIDE_INT)
12880                 {
12881                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
12882                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
12883                   mask_lo = 0;
12884                 }
12885               else
12886                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
12887                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
12888
12889               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
12890                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
12891                 {
12892                   tem_type = signed_type_for (TREE_TYPE (tem));
12893                   tem = fold_convert (tem_type, tem);
12894                 }
12895               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
12896                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
12897                 {
12898                   tem_type = unsigned_type_for (TREE_TYPE (tem));
12899                   tem = fold_convert (tem_type, tem);
12900                 }
12901               else
12902                 tem = NULL;
12903             }
12904
12905           if (tem)
12906             return fold_convert (type,
12907                                  fold_build2 (BIT_AND_EXPR,
12908                                               TREE_TYPE (tem), tem,
12909                                               fold_convert (TREE_TYPE (tem),
12910                                                             arg1)));
12911         }
12912
12913       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
12914          already handled above.  */
12915       if (TREE_CODE (arg0) == BIT_AND_EXPR
12916           && integer_onep (TREE_OPERAND (arg0, 1))
12917           && integer_zerop (op2)
12918           && integer_pow2p (arg1))
12919         {
12920           tree tem = TREE_OPERAND (arg0, 0);
12921           STRIP_NOPS (tem);
12922           if (TREE_CODE (tem) == RSHIFT_EXPR
12923               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
12924               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
12925                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
12926             return fold_build2 (BIT_AND_EXPR, type,
12927                                 TREE_OPERAND (tem, 0), arg1);
12928         }
12929
12930       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
12931          is probably obsolete because the first operand should be a
12932          truth value (that's why we have the two cases above), but let's
12933          leave it in until we can confirm this for all front-ends.  */
12934       if (integer_zerop (op2)
12935           && TREE_CODE (arg0) == NE_EXPR
12936           && integer_zerop (TREE_OPERAND (arg0, 1))
12937           && integer_pow2p (arg1)
12938           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12939           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12940                               arg1, OEP_ONLY_CONST))
12941         return pedantic_non_lvalue (fold_convert (type,
12942                                                   TREE_OPERAND (arg0, 0)));
12943
12944       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
12945       if (integer_zerop (op2)
12946           && truth_value_p (TREE_CODE (arg0))
12947           && truth_value_p (TREE_CODE (arg1)))
12948         return fold_build2 (TRUTH_ANDIF_EXPR, type,
12949                             fold_convert (type, arg0),
12950                             arg1);
12951
12952       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
12953       if (integer_onep (op2)
12954           && truth_value_p (TREE_CODE (arg0))
12955           && truth_value_p (TREE_CODE (arg1)))
12956         {
12957           /* Only perform transformation if ARG0 is easily inverted.  */
12958           tem = fold_truth_not_expr (arg0);
12959           if (tem)
12960             return fold_build2 (TRUTH_ORIF_EXPR, type,
12961                                 fold_convert (type, tem),
12962                                 arg1);
12963         }
12964
12965       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
12966       if (integer_zerop (arg1)
12967           && truth_value_p (TREE_CODE (arg0))
12968           && truth_value_p (TREE_CODE (op2)))
12969         {
12970           /* Only perform transformation if ARG0 is easily inverted.  */
12971           tem = fold_truth_not_expr (arg0);
12972           if (tem)
12973             return fold_build2 (TRUTH_ANDIF_EXPR, type,
12974                                 fold_convert (type, tem),
12975                                 op2);
12976         }
12977
12978       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
12979       if (integer_onep (arg1)
12980           && truth_value_p (TREE_CODE (arg0))
12981           && truth_value_p (TREE_CODE (op2)))
12982         return fold_build2 (TRUTH_ORIF_EXPR, type,
12983                             fold_convert (type, arg0),
12984                             op2);
12985
12986       return NULL_TREE;
12987
12988     case CALL_EXPR:
12989       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
12990          of fold_ternary on them.  */
12991       gcc_unreachable ();
12992
12993     case BIT_FIELD_REF:
12994       if ((TREE_CODE (arg0) == VECTOR_CST
12995            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
12996           && type == TREE_TYPE (TREE_TYPE (arg0)))
12997         {
12998           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
12999           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13000
13001           if (width != 0
13002               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13003               && (idx % width) == 0
13004               && (idx = idx / width)
13005                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13006             {
13007               tree elements = NULL_TREE;
13008
13009               if (TREE_CODE (arg0) == VECTOR_CST)
13010                 elements = TREE_VECTOR_CST_ELTS (arg0);
13011               else
13012                 {
13013                   unsigned HOST_WIDE_INT idx;
13014                   tree value;
13015
13016                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13017                     elements = tree_cons (NULL_TREE, value, elements);
13018                 }
13019               while (idx-- > 0 && elements)
13020                 elements = TREE_CHAIN (elements);
13021               if (elements)
13022                 return TREE_VALUE (elements);
13023               else
13024                 return fold_convert (type, integer_zero_node);
13025             }
13026         }
13027       return NULL_TREE;
13028
13029     default:
13030       return NULL_TREE;
13031     } /* switch (code) */
13032 }
13033
13034 /* Perform constant folding and related simplification of EXPR.
13035    The related simplifications include x*1 => x, x*0 => 0, etc.,
13036    and application of the associative law.
13037    NOP_EXPR conversions may be removed freely (as long as we
13038    are careful not to change the type of the overall expression).
13039    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13040    but we can constant-fold them if they have constant operands.  */
13041
13042 #ifdef ENABLE_FOLD_CHECKING
13043 # define fold(x) fold_1 (x)
13044 static tree fold_1 (tree);
13045 static
13046 #endif
13047 tree
13048 fold (tree expr)
13049 {
13050   const tree t = expr;
13051   enum tree_code code = TREE_CODE (t);
13052   enum tree_code_class kind = TREE_CODE_CLASS (code);
13053   tree tem;
13054
13055   /* Return right away if a constant.  */
13056   if (kind == tcc_constant)
13057     return t;
13058
13059   /* CALL_EXPR-like objects with variable numbers of operands are
13060      treated specially.  */
13061   if (kind == tcc_vl_exp)
13062     {
13063       if (code == CALL_EXPR)
13064         {
13065           tem = fold_call_expr (expr, false);
13066           return tem ? tem : expr;
13067         }
13068       return expr;
13069     }
13070
13071   if (IS_EXPR_CODE_CLASS (kind)
13072       || IS_GIMPLE_STMT_CODE_CLASS (kind))
13073     {
13074       tree type = TREE_TYPE (t);
13075       tree op0, op1, op2;
13076
13077       switch (TREE_CODE_LENGTH (code))
13078         {
13079         case 1:
13080           op0 = TREE_OPERAND (t, 0);
13081           tem = fold_unary (code, type, op0);
13082           return tem ? tem : expr;
13083         case 2:
13084           op0 = TREE_OPERAND (t, 0);
13085           op1 = TREE_OPERAND (t, 1);
13086           tem = fold_binary (code, type, op0, op1);
13087           return tem ? tem : expr;
13088         case 3:
13089           op0 = TREE_OPERAND (t, 0);
13090           op1 = TREE_OPERAND (t, 1);
13091           op2 = TREE_OPERAND (t, 2);
13092           tem = fold_ternary (code, type, op0, op1, op2);
13093           return tem ? tem : expr;
13094         default:
13095           break;
13096         }
13097     }
13098
13099   switch (code)
13100     {
13101     case ARRAY_REF:
13102       {
13103         tree op0 = TREE_OPERAND (t, 0);
13104         tree op1 = TREE_OPERAND (t, 1);
13105
13106         if (TREE_CODE (op1) == INTEGER_CST
13107             && TREE_CODE (op0) == CONSTRUCTOR
13108             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13109           {
13110             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13111             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13112             unsigned HOST_WIDE_INT begin = 0;
13113
13114             /* Find a matching index by means of a binary search.  */
13115             while (begin != end)
13116               {
13117                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13118                 tree index = VEC_index (constructor_elt, elts, middle)->index;
13119
13120                 if (TREE_CODE (index) == INTEGER_CST
13121                     && tree_int_cst_lt (index, op1))
13122                   begin = middle + 1;
13123                 else if (TREE_CODE (index) == INTEGER_CST
13124                          && tree_int_cst_lt (op1, index))
13125                   end = middle;
13126                 else if (TREE_CODE (index) == RANGE_EXPR
13127                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13128                   begin = middle + 1;
13129                 else if (TREE_CODE (index) == RANGE_EXPR
13130                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13131                   end = middle;
13132                 else
13133                   return VEC_index (constructor_elt, elts, middle)->value;
13134               }
13135           }
13136
13137         return t;
13138       }
13139
13140     case CONST_DECL:
13141       return fold (DECL_INITIAL (t));
13142
13143     default:
13144       return t;
13145     } /* switch (code) */
13146 }
13147
13148 #ifdef ENABLE_FOLD_CHECKING
13149 #undef fold
13150
13151 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13152 static void fold_check_failed (const_tree, const_tree);
13153 void print_fold_checksum (const_tree);
13154
13155 /* When --enable-checking=fold, compute a digest of expr before
13156    and after actual fold call to see if fold did not accidentally
13157    change original expr.  */
13158
13159 tree
13160 fold (tree expr)
13161 {
13162   tree ret;
13163   struct md5_ctx ctx;
13164   unsigned char checksum_before[16], checksum_after[16];
13165   htab_t ht;
13166
13167   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13168   md5_init_ctx (&ctx);
13169   fold_checksum_tree (expr, &ctx, ht);
13170   md5_finish_ctx (&ctx, checksum_before);
13171   htab_empty (ht);
13172
13173   ret = fold_1 (expr);
13174
13175   md5_init_ctx (&ctx);
13176   fold_checksum_tree (expr, &ctx, ht);
13177   md5_finish_ctx (&ctx, checksum_after);
13178   htab_delete (ht);
13179
13180   if (memcmp (checksum_before, checksum_after, 16))
13181     fold_check_failed (expr, ret);
13182
13183   return ret;
13184 }
13185
13186 void
13187 print_fold_checksum (const_tree expr)
13188 {
13189   struct md5_ctx ctx;
13190   unsigned char checksum[16], cnt;
13191   htab_t ht;
13192
13193   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13194   md5_init_ctx (&ctx);
13195   fold_checksum_tree (expr, &ctx, ht);
13196   md5_finish_ctx (&ctx, checksum);
13197   htab_delete (ht);
13198   for (cnt = 0; cnt < 16; ++cnt)
13199     fprintf (stderr, "%02x", checksum[cnt]);
13200   putc ('\n', stderr);
13201 }
13202
13203 static void
13204 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13205 {
13206   internal_error ("fold check: original tree changed by fold");
13207 }
13208
13209 static void
13210 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13211 {
13212   const void **slot;
13213   enum tree_code code;
13214   struct tree_function_decl buf;
13215   int i, len;
13216   
13217 recursive_label:
13218
13219   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13220                <= sizeof (struct tree_function_decl))
13221               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13222   if (expr == NULL)
13223     return;
13224   slot = (const void **) htab_find_slot (ht, expr, INSERT);
13225   if (*slot != NULL)
13226     return;
13227   *slot = expr;
13228   code = TREE_CODE (expr);
13229   if (TREE_CODE_CLASS (code) == tcc_declaration
13230       && DECL_ASSEMBLER_NAME_SET_P (expr))
13231     {
13232       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13233       memcpy ((char *) &buf, expr, tree_size (expr));
13234       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13235       expr = (tree) &buf;
13236     }
13237   else if (TREE_CODE_CLASS (code) == tcc_type
13238            && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)
13239                || TYPE_CACHED_VALUES_P (expr)
13240                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)))
13241     {
13242       /* Allow these fields to be modified.  */
13243       tree tmp;
13244       memcpy ((char *) &buf, expr, tree_size (expr));
13245       expr = tmp = (tree) &buf;
13246       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13247       TYPE_POINTER_TO (tmp) = NULL;
13248       TYPE_REFERENCE_TO (tmp) = NULL;
13249       if (TYPE_CACHED_VALUES_P (tmp))
13250         {
13251           TYPE_CACHED_VALUES_P (tmp) = 0;
13252           TYPE_CACHED_VALUES (tmp) = NULL;
13253         }
13254     }
13255   md5_process_bytes (expr, tree_size (expr), ctx);
13256   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13257   if (TREE_CODE_CLASS (code) != tcc_type
13258       && TREE_CODE_CLASS (code) != tcc_declaration
13259       && code != TREE_LIST
13260       && code != SSA_NAME)
13261     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13262   switch (TREE_CODE_CLASS (code))
13263     {
13264     case tcc_constant:
13265       switch (code)
13266         {
13267         case STRING_CST:
13268           md5_process_bytes (TREE_STRING_POINTER (expr),
13269                              TREE_STRING_LENGTH (expr), ctx);
13270           break;
13271         case COMPLEX_CST:
13272           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13273           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13274           break;
13275         case VECTOR_CST:
13276           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13277           break;
13278         default:
13279           break;
13280         }
13281       break;
13282     case tcc_exceptional:
13283       switch (code)
13284         {
13285         case TREE_LIST:
13286           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13287           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13288           expr = TREE_CHAIN (expr);
13289           goto recursive_label;
13290           break;
13291         case TREE_VEC:
13292           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13293             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13294           break;
13295         default:
13296           break;
13297         }
13298       break;
13299     case tcc_expression:
13300     case tcc_reference:
13301     case tcc_comparison:
13302     case tcc_unary:
13303     case tcc_binary:
13304     case tcc_statement:
13305     case tcc_vl_exp:
13306       len = TREE_OPERAND_LENGTH (expr);
13307       for (i = 0; i < len; ++i)
13308         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13309       break;
13310     case tcc_declaration:
13311       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13312       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13313       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13314         {
13315           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13316           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13317           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13318           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13319           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13320         }
13321       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13322         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13323           
13324       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13325         {
13326           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13327           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13328           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13329         }
13330       break;
13331     case tcc_type:
13332       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13333         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13334       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13335       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13336       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13337       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13338       if (INTEGRAL_TYPE_P (expr)
13339           || SCALAR_FLOAT_TYPE_P (expr))
13340         {
13341           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13342           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13343         }
13344       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13345       if (TREE_CODE (expr) == RECORD_TYPE
13346           || TREE_CODE (expr) == UNION_TYPE
13347           || TREE_CODE (expr) == QUAL_UNION_TYPE)
13348         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13349       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13350       break;
13351     default:
13352       break;
13353     }
13354 }
13355
13356 /* Helper function for outputting the checksum of a tree T.  When
13357    debugging with gdb, you can "define mynext" to be "next" followed
13358    by "call debug_fold_checksum (op0)", then just trace down till the
13359    outputs differ.  */
13360
13361 void
13362 debug_fold_checksum (const_tree t)
13363 {
13364   int i;
13365   unsigned char checksum[16];
13366   struct md5_ctx ctx;
13367   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13368   
13369   md5_init_ctx (&ctx);
13370   fold_checksum_tree (t, &ctx, ht);
13371   md5_finish_ctx (&ctx, checksum);
13372   htab_empty (ht);
13373
13374   for (i = 0; i < 16; i++)
13375     fprintf (stderr, "%d ", checksum[i]);
13376
13377   fprintf (stderr, "\n");
13378 }
13379
13380 #endif
13381
13382 /* Fold a unary tree expression with code CODE of type TYPE with an
13383    operand OP0.  Return a folded expression if successful.  Otherwise,
13384    return a tree expression with code CODE of type TYPE with an
13385    operand OP0.  */
13386
13387 tree
13388 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13389 {
13390   tree tem;
13391 #ifdef ENABLE_FOLD_CHECKING
13392   unsigned char checksum_before[16], checksum_after[16];
13393   struct md5_ctx ctx;
13394   htab_t ht;
13395
13396   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13397   md5_init_ctx (&ctx);
13398   fold_checksum_tree (op0, &ctx, ht);
13399   md5_finish_ctx (&ctx, checksum_before);
13400   htab_empty (ht);
13401 #endif
13402   
13403   tem = fold_unary (code, type, op0);
13404   if (!tem)
13405     tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13406   
13407 #ifdef ENABLE_FOLD_CHECKING
13408   md5_init_ctx (&ctx);
13409   fold_checksum_tree (op0, &ctx, ht);
13410   md5_finish_ctx (&ctx, checksum_after);
13411   htab_delete (ht);
13412
13413   if (memcmp (checksum_before, checksum_after, 16))
13414     fold_check_failed (op0, tem);
13415 #endif
13416   return tem;
13417 }
13418
13419 /* Fold a binary tree expression with code CODE of type TYPE with
13420    operands OP0 and OP1.  Return a folded expression if successful.
13421    Otherwise, return a tree expression with code CODE of type TYPE
13422    with operands OP0 and OP1.  */
13423
13424 tree
13425 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13426                   MEM_STAT_DECL)
13427 {
13428   tree tem;
13429 #ifdef ENABLE_FOLD_CHECKING
13430   unsigned char checksum_before_op0[16],
13431                 checksum_before_op1[16],
13432                 checksum_after_op0[16],
13433                 checksum_after_op1[16];
13434   struct md5_ctx ctx;
13435   htab_t ht;
13436
13437   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13438   md5_init_ctx (&ctx);
13439   fold_checksum_tree (op0, &ctx, ht);
13440   md5_finish_ctx (&ctx, checksum_before_op0);
13441   htab_empty (ht);
13442
13443   md5_init_ctx (&ctx);
13444   fold_checksum_tree (op1, &ctx, ht);
13445   md5_finish_ctx (&ctx, checksum_before_op1);
13446   htab_empty (ht);
13447 #endif
13448
13449   tem = fold_binary (code, type, op0, op1);
13450   if (!tem)
13451     tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13452   
13453 #ifdef ENABLE_FOLD_CHECKING
13454   md5_init_ctx (&ctx);
13455   fold_checksum_tree (op0, &ctx, ht);
13456   md5_finish_ctx (&ctx, checksum_after_op0);
13457   htab_empty (ht);
13458
13459   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13460     fold_check_failed (op0, tem);
13461   
13462   md5_init_ctx (&ctx);
13463   fold_checksum_tree (op1, &ctx, ht);
13464   md5_finish_ctx (&ctx, checksum_after_op1);
13465   htab_delete (ht);
13466
13467   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13468     fold_check_failed (op1, tem);
13469 #endif
13470   return tem;
13471 }
13472
13473 /* Fold a ternary tree expression with code CODE of type TYPE with
13474    operands OP0, OP1, and OP2.  Return a folded expression if
13475    successful.  Otherwise, return a tree expression with code CODE of
13476    type TYPE with operands OP0, OP1, and OP2.  */
13477
13478 tree
13479 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
13480              MEM_STAT_DECL)
13481 {
13482   tree tem;
13483 #ifdef ENABLE_FOLD_CHECKING
13484   unsigned char checksum_before_op0[16],
13485                 checksum_before_op1[16],
13486                 checksum_before_op2[16],
13487                 checksum_after_op0[16],
13488                 checksum_after_op1[16],
13489                 checksum_after_op2[16];
13490   struct md5_ctx ctx;
13491   htab_t ht;
13492
13493   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13494   md5_init_ctx (&ctx);
13495   fold_checksum_tree (op0, &ctx, ht);
13496   md5_finish_ctx (&ctx, checksum_before_op0);
13497   htab_empty (ht);
13498
13499   md5_init_ctx (&ctx);
13500   fold_checksum_tree (op1, &ctx, ht);
13501   md5_finish_ctx (&ctx, checksum_before_op1);
13502   htab_empty (ht);
13503
13504   md5_init_ctx (&ctx);
13505   fold_checksum_tree (op2, &ctx, ht);
13506   md5_finish_ctx (&ctx, checksum_before_op2);
13507   htab_empty (ht);
13508 #endif
13509
13510   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13511   tem = fold_ternary (code, type, op0, op1, op2);
13512   if (!tem)
13513     tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
13514       
13515 #ifdef ENABLE_FOLD_CHECKING
13516   md5_init_ctx (&ctx);
13517   fold_checksum_tree (op0, &ctx, ht);
13518   md5_finish_ctx (&ctx, checksum_after_op0);
13519   htab_empty (ht);
13520
13521   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13522     fold_check_failed (op0, tem);
13523   
13524   md5_init_ctx (&ctx);
13525   fold_checksum_tree (op1, &ctx, ht);
13526   md5_finish_ctx (&ctx, checksum_after_op1);
13527   htab_empty (ht);
13528
13529   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13530     fold_check_failed (op1, tem);
13531   
13532   md5_init_ctx (&ctx);
13533   fold_checksum_tree (op2, &ctx, ht);
13534   md5_finish_ctx (&ctx, checksum_after_op2);
13535   htab_delete (ht);
13536
13537   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13538     fold_check_failed (op2, tem);
13539 #endif
13540   return tem;
13541 }
13542
13543 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13544    arguments in ARGARRAY, and a null static chain.
13545    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
13546    of type TYPE from the given operands as constructed by build_call_array.  */
13547
13548 tree
13549 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
13550 {
13551   tree tem;
13552 #ifdef ENABLE_FOLD_CHECKING
13553   unsigned char checksum_before_fn[16],
13554                 checksum_before_arglist[16],
13555                 checksum_after_fn[16],
13556                 checksum_after_arglist[16];
13557   struct md5_ctx ctx;
13558   htab_t ht;
13559   int i;
13560
13561   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13562   md5_init_ctx (&ctx);
13563   fold_checksum_tree (fn, &ctx, ht);
13564   md5_finish_ctx (&ctx, checksum_before_fn);
13565   htab_empty (ht);
13566
13567   md5_init_ctx (&ctx);
13568   for (i = 0; i < nargs; i++)
13569     fold_checksum_tree (argarray[i], &ctx, ht);
13570   md5_finish_ctx (&ctx, checksum_before_arglist);
13571   htab_empty (ht);
13572 #endif
13573
13574   tem = fold_builtin_call_array (type, fn, nargs, argarray);
13575       
13576 #ifdef ENABLE_FOLD_CHECKING
13577   md5_init_ctx (&ctx);
13578   fold_checksum_tree (fn, &ctx, ht);
13579   md5_finish_ctx (&ctx, checksum_after_fn);
13580   htab_empty (ht);
13581
13582   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13583     fold_check_failed (fn, tem);
13584   
13585   md5_init_ctx (&ctx);
13586   for (i = 0; i < nargs; i++)
13587     fold_checksum_tree (argarray[i], &ctx, ht);
13588   md5_finish_ctx (&ctx, checksum_after_arglist);
13589   htab_delete (ht);
13590
13591   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13592     fold_check_failed (NULL_TREE, tem);
13593 #endif
13594   return tem;
13595 }
13596
13597 /* Perform constant folding and related simplification of initializer
13598    expression EXPR.  These behave identically to "fold_buildN" but ignore
13599    potential run-time traps and exceptions that fold must preserve.  */
13600
13601 #define START_FOLD_INIT \
13602   int saved_signaling_nans = flag_signaling_nans;\
13603   int saved_trapping_math = flag_trapping_math;\
13604   int saved_rounding_math = flag_rounding_math;\
13605   int saved_trapv = flag_trapv;\
13606   int saved_folding_initializer = folding_initializer;\
13607   flag_signaling_nans = 0;\
13608   flag_trapping_math = 0;\
13609   flag_rounding_math = 0;\
13610   flag_trapv = 0;\
13611   folding_initializer = 1;
13612
13613 #define END_FOLD_INIT \
13614   flag_signaling_nans = saved_signaling_nans;\
13615   flag_trapping_math = saved_trapping_math;\
13616   flag_rounding_math = saved_rounding_math;\
13617   flag_trapv = saved_trapv;\
13618   folding_initializer = saved_folding_initializer;
13619
13620 tree
13621 fold_build1_initializer (enum tree_code code, tree type, tree op)
13622 {
13623   tree result;
13624   START_FOLD_INIT;
13625
13626   result = fold_build1 (code, type, op);
13627
13628   END_FOLD_INIT;
13629   return result;
13630 }
13631
13632 tree
13633 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
13634 {
13635   tree result;
13636   START_FOLD_INIT;
13637
13638   result = fold_build2 (code, type, op0, op1);
13639
13640   END_FOLD_INIT;
13641   return result;
13642 }
13643
13644 tree
13645 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
13646                          tree op2)
13647 {
13648   tree result;
13649   START_FOLD_INIT;
13650
13651   result = fold_build3 (code, type, op0, op1, op2);
13652
13653   END_FOLD_INIT;
13654   return result;
13655 }
13656
13657 tree
13658 fold_build_call_array_initializer (tree type, tree fn,
13659                                    int nargs, tree *argarray)
13660 {
13661   tree result;
13662   START_FOLD_INIT;
13663
13664   result = fold_build_call_array (type, fn, nargs, argarray);
13665
13666   END_FOLD_INIT;
13667   return result;
13668 }
13669
13670 #undef START_FOLD_INIT
13671 #undef END_FOLD_INIT
13672
13673 /* Determine if first argument is a multiple of second argument.  Return 0 if
13674    it is not, or we cannot easily determined it to be.
13675
13676    An example of the sort of thing we care about (at this point; this routine
13677    could surely be made more general, and expanded to do what the *_DIV_EXPR's
13678    fold cases do now) is discovering that
13679
13680      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13681
13682    is a multiple of
13683
13684      SAVE_EXPR (J * 8)
13685
13686    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
13687
13688    This code also handles discovering that
13689
13690      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13691
13692    is a multiple of 8 so we don't have to worry about dealing with a
13693    possible remainder.
13694
13695    Note that we *look* inside a SAVE_EXPR only to determine how it was
13696    calculated; it is not safe for fold to do much of anything else with the
13697    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
13698    at run time.  For example, the latter example above *cannot* be implemented
13699    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
13700    evaluation time of the original SAVE_EXPR is not necessarily the same at
13701    the time the new expression is evaluated.  The only optimization of this
13702    sort that would be valid is changing
13703
13704      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
13705
13706    divided by 8 to
13707
13708      SAVE_EXPR (I) * SAVE_EXPR (J)
13709
13710    (where the same SAVE_EXPR (J) is used in the original and the
13711    transformed version).  */
13712
13713 int
13714 multiple_of_p (tree type, const_tree top, const_tree bottom)
13715 {
13716   if (operand_equal_p (top, bottom, 0))
13717     return 1;
13718
13719   if (TREE_CODE (type) != INTEGER_TYPE)
13720     return 0;
13721
13722   switch (TREE_CODE (top))
13723     {
13724     case BIT_AND_EXPR:
13725       /* Bitwise and provides a power of two multiple.  If the mask is
13726          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
13727       if (!integer_pow2p (bottom))
13728         return 0;
13729       /* FALLTHRU */
13730
13731     case MULT_EXPR:
13732       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13733               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13734
13735     case PLUS_EXPR:
13736     case MINUS_EXPR:
13737       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13738               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13739
13740     case LSHIFT_EXPR:
13741       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
13742         {
13743           tree op1, t1;
13744
13745           op1 = TREE_OPERAND (top, 1);
13746           /* const_binop may not detect overflow correctly,
13747              so check for it explicitly here.  */
13748           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
13749               > TREE_INT_CST_LOW (op1)
13750               && TREE_INT_CST_HIGH (op1) == 0
13751               && 0 != (t1 = fold_convert (type,
13752                                           const_binop (LSHIFT_EXPR,
13753                                                        size_one_node,
13754                                                        op1, 0)))
13755               && !TREE_OVERFLOW (t1))
13756             return multiple_of_p (type, t1, bottom);
13757         }
13758       return 0;
13759
13760     case NOP_EXPR:
13761       /* Can't handle conversions from non-integral or wider integral type.  */
13762       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
13763           || (TYPE_PRECISION (type)
13764               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
13765         return 0;
13766
13767       /* .. fall through ...  */
13768
13769     case SAVE_EXPR:
13770       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
13771
13772     case INTEGER_CST:
13773       if (TREE_CODE (bottom) != INTEGER_CST
13774           || integer_zerop (bottom)
13775           || (TYPE_UNSIGNED (type)
13776               && (tree_int_cst_sgn (top) < 0
13777                   || tree_int_cst_sgn (bottom) < 0)))
13778         return 0;
13779       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
13780                                              top, bottom, 0));
13781
13782     default:
13783       return 0;
13784     }
13785 }
13786
13787 /* Return true if CODE or TYPE is known to be non-negative. */
13788
13789 static bool
13790 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
13791 {
13792   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
13793       && truth_value_p (code))
13794     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
13795        have a signed:1 type (where the value is -1 and 0).  */
13796     return true;
13797   return false;
13798 }
13799
13800 /* Return true if (CODE OP0) is known to be non-negative.  If the return
13801    value is based on the assumption that signed overflow is undefined,
13802    set *STRICT_OVERFLOW_P to true; otherwise, don't change
13803    *STRICT_OVERFLOW_P.  */
13804
13805 bool
13806 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
13807                                 bool *strict_overflow_p)
13808 {
13809   if (TYPE_UNSIGNED (type))
13810     return true;
13811
13812   switch (code)
13813     {
13814     case ABS_EXPR:
13815       /* We can't return 1 if flag_wrapv is set because
13816          ABS_EXPR<INT_MIN> = INT_MIN.  */
13817       if (!INTEGRAL_TYPE_P (type))
13818         return true;
13819       if (TYPE_OVERFLOW_UNDEFINED (type))
13820         {
13821           *strict_overflow_p = true;
13822           return true;
13823         }
13824       break;
13825
13826     case NON_LVALUE_EXPR:
13827     case FLOAT_EXPR:
13828     case FIX_TRUNC_EXPR:
13829       return tree_expr_nonnegative_warnv_p (op0,
13830                                             strict_overflow_p);
13831
13832     case NOP_EXPR:
13833       {
13834         tree inner_type = TREE_TYPE (op0);
13835         tree outer_type = type;
13836
13837         if (TREE_CODE (outer_type) == REAL_TYPE)
13838           {
13839             if (TREE_CODE (inner_type) == REAL_TYPE)
13840               return tree_expr_nonnegative_warnv_p (op0,
13841                                                     strict_overflow_p);
13842             if (TREE_CODE (inner_type) == INTEGER_TYPE)
13843               {
13844                 if (TYPE_UNSIGNED (inner_type))
13845                   return true;
13846                 return tree_expr_nonnegative_warnv_p (op0,
13847                                                       strict_overflow_p);
13848               }
13849           }
13850         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
13851           {
13852             if (TREE_CODE (inner_type) == REAL_TYPE)
13853               return tree_expr_nonnegative_warnv_p (op0,
13854                                                     strict_overflow_p);
13855             if (TREE_CODE (inner_type) == INTEGER_TYPE)
13856               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
13857                       && TYPE_UNSIGNED (inner_type);
13858           }
13859       }
13860       break;
13861
13862     default:
13863       return tree_simple_nonnegative_warnv_p (code, type);
13864     }
13865
13866   /* We don't know sign of `t', so be conservative and return false.  */
13867   return false;
13868 }
13869
13870 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
13871    value is based on the assumption that signed overflow is undefined,
13872    set *STRICT_OVERFLOW_P to true; otherwise, don't change
13873    *STRICT_OVERFLOW_P.  */
13874
13875 bool
13876 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
13877                                       tree op1, bool *strict_overflow_p)
13878 {
13879   if (TYPE_UNSIGNED (type))
13880     return true;
13881
13882   switch (code)
13883     {
13884     case POINTER_PLUS_EXPR:
13885     case PLUS_EXPR:
13886       if (FLOAT_TYPE_P (type))
13887         return (tree_expr_nonnegative_warnv_p (op0,
13888                                                strict_overflow_p)
13889                 && tree_expr_nonnegative_warnv_p (op1,
13890                                                   strict_overflow_p));
13891
13892       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
13893          both unsigned and at least 2 bits shorter than the result.  */
13894       if (TREE_CODE (type) == INTEGER_TYPE
13895           && TREE_CODE (op0) == NOP_EXPR
13896           && TREE_CODE (op1) == NOP_EXPR)
13897         {
13898           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
13899           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
13900           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
13901               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
13902             {
13903               unsigned int prec = MAX (TYPE_PRECISION (inner1),
13904                                        TYPE_PRECISION (inner2)) + 1;
13905               return prec < TYPE_PRECISION (type);
13906             }
13907         }
13908       break;
13909
13910     case MULT_EXPR:
13911       if (FLOAT_TYPE_P (type))
13912         {
13913           /* x * x for floating point x is always non-negative.  */
13914           if (operand_equal_p (op0, op1, 0))
13915             return true;
13916           return (tree_expr_nonnegative_warnv_p (op0,
13917                                                  strict_overflow_p)
13918                   && tree_expr_nonnegative_warnv_p (op1,
13919                                                     strict_overflow_p));
13920         }
13921
13922       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
13923          both unsigned and their total bits is shorter than the result.  */
13924       if (TREE_CODE (type) == INTEGER_TYPE
13925           && TREE_CODE (op0) == NOP_EXPR
13926           && TREE_CODE (op1) == NOP_EXPR)
13927         {
13928           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
13929           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
13930           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
13931               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
13932             return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
13933                    < TYPE_PRECISION (type);
13934         }
13935       return false;
13936
13937     case BIT_AND_EXPR:
13938     case MAX_EXPR:
13939       return (tree_expr_nonnegative_warnv_p (op0,
13940                                              strict_overflow_p)
13941               || tree_expr_nonnegative_warnv_p (op1,
13942                                                 strict_overflow_p));
13943
13944     case BIT_IOR_EXPR:
13945     case BIT_XOR_EXPR:
13946     case MIN_EXPR:
13947     case RDIV_EXPR:
13948     case TRUNC_DIV_EXPR:
13949     case CEIL_DIV_EXPR:
13950     case FLOOR_DIV_EXPR:
13951     case ROUND_DIV_EXPR:
13952       return (tree_expr_nonnegative_warnv_p (op0,
13953                                              strict_overflow_p)
13954               && tree_expr_nonnegative_warnv_p (op1,
13955                                                 strict_overflow_p));
13956
13957     case TRUNC_MOD_EXPR:
13958     case CEIL_MOD_EXPR:
13959     case FLOOR_MOD_EXPR:
13960     case ROUND_MOD_EXPR:
13961       return tree_expr_nonnegative_warnv_p (op0,
13962                                             strict_overflow_p);
13963     default:
13964       return tree_simple_nonnegative_warnv_p (code, type);
13965     }
13966
13967   /* We don't know sign of `t', so be conservative and return false.  */
13968   return false;
13969 }
13970
13971 /* Return true if T is known to be non-negative.  If the return
13972    value is based on the assumption that signed overflow is undefined,
13973    set *STRICT_OVERFLOW_P to true; otherwise, don't change
13974    *STRICT_OVERFLOW_P.  */
13975
13976 bool
13977 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
13978 {
13979   if (TYPE_UNSIGNED (TREE_TYPE (t)))
13980     return true;
13981
13982   switch (TREE_CODE (t))
13983     {
13984     case SSA_NAME:
13985       /* Query VRP to see if it has recorded any information about
13986          the range of this object.  */
13987       return ssa_name_nonnegative_p (t);
13988
13989     case INTEGER_CST:
13990       return tree_int_cst_sgn (t) >= 0;
13991
13992     case REAL_CST:
13993       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
13994
13995     case FIXED_CST:
13996       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
13997
13998     case COND_EXPR:
13999       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14000                                              strict_overflow_p)
14001               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14002                                                 strict_overflow_p));
14003     default:
14004       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14005                                                    TREE_TYPE (t));
14006     }
14007   /* We don't know sign of `t', so be conservative and return false.  */
14008   return false;
14009 }
14010
14011 /* Return true if T is known to be non-negative.  If the return
14012    value is based on the assumption that signed overflow is undefined,
14013    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14014    *STRICT_OVERFLOW_P.  */
14015
14016 bool
14017 tree_call_nonnegative_warnv_p (enum tree_code code,  tree type, tree fndecl,
14018                                tree arg0, tree arg1, bool *strict_overflow_p)
14019 {
14020   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14021     switch (DECL_FUNCTION_CODE (fndecl))
14022       {
14023         CASE_FLT_FN (BUILT_IN_ACOS):
14024         CASE_FLT_FN (BUILT_IN_ACOSH):
14025         CASE_FLT_FN (BUILT_IN_CABS):
14026         CASE_FLT_FN (BUILT_IN_COSH):
14027         CASE_FLT_FN (BUILT_IN_ERFC):
14028         CASE_FLT_FN (BUILT_IN_EXP):
14029         CASE_FLT_FN (BUILT_IN_EXP10):
14030         CASE_FLT_FN (BUILT_IN_EXP2):
14031         CASE_FLT_FN (BUILT_IN_FABS):
14032         CASE_FLT_FN (BUILT_IN_FDIM):
14033         CASE_FLT_FN (BUILT_IN_HYPOT):
14034         CASE_FLT_FN (BUILT_IN_POW10):
14035         CASE_INT_FN (BUILT_IN_FFS):
14036         CASE_INT_FN (BUILT_IN_PARITY):
14037         CASE_INT_FN (BUILT_IN_POPCOUNT):
14038       case BUILT_IN_BSWAP32:
14039       case BUILT_IN_BSWAP64:
14040         /* Always true.  */
14041         return true;
14042
14043         CASE_FLT_FN (BUILT_IN_SQRT):
14044         /* sqrt(-0.0) is -0.0.  */
14045         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14046           return true;
14047         return tree_expr_nonnegative_warnv_p (arg0,
14048                                               strict_overflow_p);
14049
14050         CASE_FLT_FN (BUILT_IN_ASINH):
14051         CASE_FLT_FN (BUILT_IN_ATAN):
14052         CASE_FLT_FN (BUILT_IN_ATANH):
14053         CASE_FLT_FN (BUILT_IN_CBRT):
14054         CASE_FLT_FN (BUILT_IN_CEIL):
14055         CASE_FLT_FN (BUILT_IN_ERF):
14056         CASE_FLT_FN (BUILT_IN_EXPM1):
14057         CASE_FLT_FN (BUILT_IN_FLOOR):
14058         CASE_FLT_FN (BUILT_IN_FMOD):
14059         CASE_FLT_FN (BUILT_IN_FREXP):
14060         CASE_FLT_FN (BUILT_IN_LCEIL):
14061         CASE_FLT_FN (BUILT_IN_LDEXP):
14062         CASE_FLT_FN (BUILT_IN_LFLOOR):
14063         CASE_FLT_FN (BUILT_IN_LLCEIL):
14064         CASE_FLT_FN (BUILT_IN_LLFLOOR):
14065         CASE_FLT_FN (BUILT_IN_LLRINT):
14066         CASE_FLT_FN (BUILT_IN_LLROUND):
14067         CASE_FLT_FN (BUILT_IN_LRINT):
14068         CASE_FLT_FN (BUILT_IN_LROUND):
14069         CASE_FLT_FN (BUILT_IN_MODF):
14070         CASE_FLT_FN (BUILT_IN_NEARBYINT):
14071         CASE_FLT_FN (BUILT_IN_RINT):
14072         CASE_FLT_FN (BUILT_IN_ROUND):
14073         CASE_FLT_FN (BUILT_IN_SCALB):
14074         CASE_FLT_FN (BUILT_IN_SCALBLN):
14075         CASE_FLT_FN (BUILT_IN_SCALBN):
14076         CASE_FLT_FN (BUILT_IN_SIGNBIT):
14077         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14078         CASE_FLT_FN (BUILT_IN_SINH):
14079         CASE_FLT_FN (BUILT_IN_TANH):
14080         CASE_FLT_FN (BUILT_IN_TRUNC):
14081         /* True if the 1st argument is nonnegative.  */
14082         return tree_expr_nonnegative_warnv_p (arg0,
14083                                               strict_overflow_p);
14084
14085         CASE_FLT_FN (BUILT_IN_FMAX):
14086         /* True if the 1st OR 2nd arguments are nonnegative.  */
14087         return (tree_expr_nonnegative_warnv_p (arg0,
14088                                                strict_overflow_p)
14089                 || (tree_expr_nonnegative_warnv_p (arg1,
14090                                                    strict_overflow_p)));
14091
14092         CASE_FLT_FN (BUILT_IN_FMIN):
14093         /* True if the 1st AND 2nd arguments are nonnegative.  */
14094         return (tree_expr_nonnegative_warnv_p (arg0,
14095                                                strict_overflow_p)
14096                 && (tree_expr_nonnegative_warnv_p (arg1,
14097                                                    strict_overflow_p)));
14098
14099         CASE_FLT_FN (BUILT_IN_COPYSIGN):
14100         /* True if the 2nd argument is nonnegative.  */
14101         return tree_expr_nonnegative_warnv_p (arg1,
14102                                               strict_overflow_p);
14103
14104         CASE_FLT_FN (BUILT_IN_POWI):
14105         /* True if the 1st argument is nonnegative or the second
14106            argument is an even integer.  */
14107         if (TREE_CODE (arg1) == INTEGER_CST)
14108           {
14109             tree arg1 = arg1;
14110             if ((TREE_INT_CST_LOW (arg1) & 1) == 0)
14111               return true;
14112           }
14113         return tree_expr_nonnegative_warnv_p (arg0,
14114                                               strict_overflow_p);
14115
14116         CASE_FLT_FN (BUILT_IN_POW):
14117         /* True if the 1st argument is nonnegative or the second
14118            argument is an even integer valued real.  */
14119         if (TREE_CODE (arg1) == REAL_CST)
14120           {
14121             REAL_VALUE_TYPE c;
14122             HOST_WIDE_INT n;
14123
14124             c = TREE_REAL_CST (arg1);
14125             n = real_to_integer (&c);
14126             if ((n & 1) == 0)
14127               {
14128                 REAL_VALUE_TYPE cint;
14129                 real_from_integer (&cint, VOIDmode, n,
14130                                    n < 0 ? -1 : 0, 0);
14131                 if (real_identical (&c, &cint))
14132                   return true;
14133               }
14134           }
14135         return tree_expr_nonnegative_warnv_p (arg0,
14136                                               strict_overflow_p);
14137
14138       default:
14139         break;
14140       }
14141   return tree_simple_nonnegative_warnv_p (code,
14142                                           type);
14143 }
14144
14145 /* Return true if T is known to be non-negative.  If the return
14146    value is based on the assumption that signed overflow is undefined,
14147    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14148    *STRICT_OVERFLOW_P.  */
14149
14150 bool
14151 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14152 {
14153   enum tree_code code = TREE_CODE (t);
14154   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14155     return true;
14156
14157   switch (code)
14158     {
14159     case TARGET_EXPR:
14160       {
14161         tree temp = TARGET_EXPR_SLOT (t);
14162         t = TARGET_EXPR_INITIAL (t);
14163
14164         /* If the initializer is non-void, then it's a normal expression
14165            that will be assigned to the slot.  */
14166         if (!VOID_TYPE_P (t))
14167           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14168
14169         /* Otherwise, the initializer sets the slot in some way.  One common
14170            way is an assignment statement at the end of the initializer.  */
14171         while (1)
14172           {
14173             if (TREE_CODE (t) == BIND_EXPR)
14174               t = expr_last (BIND_EXPR_BODY (t));
14175             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14176                      || TREE_CODE (t) == TRY_CATCH_EXPR)
14177               t = expr_last (TREE_OPERAND (t, 0));
14178             else if (TREE_CODE (t) == STATEMENT_LIST)
14179               t = expr_last (t);
14180             else
14181               break;
14182           }
14183         if ((TREE_CODE (t) == MODIFY_EXPR
14184              || TREE_CODE (t) == GIMPLE_MODIFY_STMT)
14185             && GENERIC_TREE_OPERAND (t, 0) == temp)
14186           return tree_expr_nonnegative_warnv_p (GENERIC_TREE_OPERAND (t, 1),
14187                                                 strict_overflow_p);
14188
14189         return false;
14190       }
14191
14192     case CALL_EXPR:
14193       {
14194         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
14195         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
14196
14197         return tree_call_nonnegative_warnv_p (TREE_CODE (t),
14198                                               TREE_TYPE (t),
14199                                               get_callee_fndecl (t),
14200                                               arg0,
14201                                               arg1,
14202                                               strict_overflow_p);
14203       }
14204     case COMPOUND_EXPR:
14205     case MODIFY_EXPR:
14206     case GIMPLE_MODIFY_STMT:
14207       return tree_expr_nonnegative_warnv_p (GENERIC_TREE_OPERAND (t, 1),
14208                                             strict_overflow_p);
14209     case BIND_EXPR:
14210       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14211                                             strict_overflow_p);
14212     case SAVE_EXPR:
14213       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14214                                             strict_overflow_p);
14215
14216     default:
14217       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14218                                                    TREE_TYPE (t));
14219     }
14220
14221   /* We don't know sign of `t', so be conservative and return false.  */
14222   return false;
14223 }
14224
14225 /* Return true if T is known to be non-negative.  If the return
14226    value is based on the assumption that signed overflow is undefined,
14227    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14228    *STRICT_OVERFLOW_P.  */
14229
14230 bool
14231 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14232 {
14233   enum tree_code code;
14234   if (t == error_mark_node)
14235     return false;
14236
14237   code = TREE_CODE (t);
14238   switch (TREE_CODE_CLASS (code))
14239     {
14240     case tcc_binary:
14241     case tcc_comparison:
14242       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14243                                               TREE_TYPE (t),
14244                                               TREE_OPERAND (t, 0),
14245                                               TREE_OPERAND (t, 1),
14246                                               strict_overflow_p);
14247
14248     case tcc_unary:
14249       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14250                                              TREE_TYPE (t),
14251                                              TREE_OPERAND (t, 0),
14252                                              strict_overflow_p);
14253
14254     case tcc_constant:
14255     case tcc_declaration:
14256     case tcc_reference:
14257       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14258
14259     default:
14260       break;
14261     }
14262
14263   switch (code)
14264     {
14265     case TRUTH_AND_EXPR:
14266     case TRUTH_OR_EXPR:
14267     case TRUTH_XOR_EXPR:
14268       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14269                                               TREE_TYPE (t),
14270                                               TREE_OPERAND (t, 0),
14271                                               TREE_OPERAND (t, 1),
14272                                               strict_overflow_p);
14273     case TRUTH_NOT_EXPR:
14274       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14275                                              TREE_TYPE (t),
14276                                              TREE_OPERAND (t, 0),
14277                                              strict_overflow_p);
14278
14279     case COND_EXPR:
14280     case CONSTRUCTOR:
14281     case OBJ_TYPE_REF:
14282     case ASSERT_EXPR:
14283     case ADDR_EXPR:
14284     case WITH_SIZE_EXPR:
14285     case EXC_PTR_EXPR:
14286     case SSA_NAME:
14287     case FILTER_EXPR:
14288       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14289
14290     default:
14291       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14292     }
14293 }
14294
14295 /* Return true if `t' is known to be non-negative.  Handle warnings
14296    about undefined signed overflow.  */
14297
14298 bool
14299 tree_expr_nonnegative_p (tree t)
14300 {
14301   bool ret, strict_overflow_p;
14302
14303   strict_overflow_p = false;
14304   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14305   if (strict_overflow_p)
14306     fold_overflow_warning (("assuming signed overflow does not occur when "
14307                             "determining that expression is always "
14308                             "non-negative"),
14309                            WARN_STRICT_OVERFLOW_MISC);
14310   return ret;
14311 }
14312
14313
14314 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14315    For floating point we further ensure that T is not denormal.
14316    Similar logic is present in nonzero_address in rtlanal.h.
14317
14318    If the return value is based on the assumption that signed overflow
14319    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14320    change *STRICT_OVERFLOW_P.  */
14321
14322 bool
14323 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14324                                  bool *strict_overflow_p)
14325 {
14326   switch (code)
14327     {
14328     case ABS_EXPR:
14329       return tree_expr_nonzero_warnv_p (op0,
14330                                         strict_overflow_p);
14331
14332     case NOP_EXPR:
14333       {
14334         tree inner_type = TREE_TYPE (op0);
14335         tree outer_type = type;
14336
14337         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14338                 && tree_expr_nonzero_warnv_p (op0,
14339                                               strict_overflow_p));
14340       }
14341       break;
14342
14343     case NON_LVALUE_EXPR:
14344       return tree_expr_nonzero_warnv_p (op0,
14345                                         strict_overflow_p);
14346
14347     default:
14348       break;
14349   }
14350
14351   return false;
14352 }
14353
14354 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14355    For floating point we further ensure that T is not denormal.
14356    Similar logic is present in nonzero_address in rtlanal.h.
14357
14358    If the return value is based on the assumption that signed overflow
14359    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14360    change *STRICT_OVERFLOW_P.  */
14361
14362 bool
14363 tree_binary_nonzero_warnv_p (enum tree_code code,
14364                              tree type,
14365                              tree op0,
14366                              tree op1, bool *strict_overflow_p)
14367 {
14368   bool sub_strict_overflow_p;
14369   switch (code)
14370     {
14371     case POINTER_PLUS_EXPR:
14372     case PLUS_EXPR:
14373       if (TYPE_OVERFLOW_UNDEFINED (type))
14374         {
14375           /* With the presence of negative values it is hard
14376              to say something.  */
14377           sub_strict_overflow_p = false;
14378           if (!tree_expr_nonnegative_warnv_p (op0,
14379                                               &sub_strict_overflow_p)
14380               || !tree_expr_nonnegative_warnv_p (op1,
14381                                                  &sub_strict_overflow_p))
14382             return false;
14383           /* One of operands must be positive and the other non-negative.  */
14384           /* We don't set *STRICT_OVERFLOW_P here: even if this value
14385              overflows, on a twos-complement machine the sum of two
14386              nonnegative numbers can never be zero.  */
14387           return (tree_expr_nonzero_warnv_p (op0,
14388                                              strict_overflow_p)
14389                   || tree_expr_nonzero_warnv_p (op1,
14390                                                 strict_overflow_p));
14391         }
14392       break;
14393
14394     case MULT_EXPR:
14395       if (TYPE_OVERFLOW_UNDEFINED (type))
14396         {
14397           if (tree_expr_nonzero_warnv_p (op0,
14398                                          strict_overflow_p)
14399               && tree_expr_nonzero_warnv_p (op1,
14400                                             strict_overflow_p))
14401             {
14402               *strict_overflow_p = true;
14403               return true;
14404             }
14405         }
14406       break;
14407
14408     case MIN_EXPR:
14409       sub_strict_overflow_p = false;
14410       if (tree_expr_nonzero_warnv_p (op0,
14411                                      &sub_strict_overflow_p)
14412           && tree_expr_nonzero_warnv_p (op1,
14413                                         &sub_strict_overflow_p))
14414         {
14415           if (sub_strict_overflow_p)
14416             *strict_overflow_p = true;
14417         }
14418       break;
14419
14420     case MAX_EXPR:
14421       sub_strict_overflow_p = false;
14422       if (tree_expr_nonzero_warnv_p (op0,
14423                                      &sub_strict_overflow_p))
14424         {
14425           if (sub_strict_overflow_p)
14426             *strict_overflow_p = true;
14427
14428           /* When both operands are nonzero, then MAX must be too.  */
14429           if (tree_expr_nonzero_warnv_p (op1,
14430                                          strict_overflow_p))
14431             return true;
14432
14433           /* MAX where operand 0 is positive is positive.  */
14434           return tree_expr_nonnegative_warnv_p (op0,
14435                                                strict_overflow_p);
14436         }
14437       /* MAX where operand 1 is positive is positive.  */
14438       else if (tree_expr_nonzero_warnv_p (op1,
14439                                           &sub_strict_overflow_p)
14440                && tree_expr_nonnegative_warnv_p (op1,
14441                                                  &sub_strict_overflow_p))
14442         {
14443           if (sub_strict_overflow_p)
14444             *strict_overflow_p = true;
14445           return true;
14446         }
14447       break;
14448
14449     case BIT_IOR_EXPR:
14450       return (tree_expr_nonzero_warnv_p (op1,
14451                                          strict_overflow_p)
14452               || tree_expr_nonzero_warnv_p (op0,
14453                                             strict_overflow_p));
14454
14455     default:
14456       break;
14457   }
14458
14459   return false;
14460 }
14461
14462 /* Return true when T is an address and is known to be nonzero.
14463    For floating point we further ensure that T is not denormal.
14464    Similar logic is present in nonzero_address in rtlanal.h.
14465
14466    If the return value is based on the assumption that signed overflow
14467    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14468    change *STRICT_OVERFLOW_P.  */
14469
14470 bool
14471 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14472 {
14473   bool sub_strict_overflow_p;
14474   switch (TREE_CODE (t))
14475     {
14476     case SSA_NAME:
14477       /* Query VRP to see if it has recorded any information about
14478          the range of this object.  */
14479       return ssa_name_nonzero_p (t);
14480
14481     case INTEGER_CST:
14482       return !integer_zerop (t);
14483
14484     case ADDR_EXPR:
14485       {
14486         tree base = get_base_address (TREE_OPERAND (t, 0));
14487
14488         if (!base)
14489           return false;
14490
14491         /* Weak declarations may link to NULL.  */
14492         if (VAR_OR_FUNCTION_DECL_P (base))
14493           return !DECL_WEAK (base);
14494
14495         /* Constants are never weak.  */
14496         if (CONSTANT_CLASS_P (base))
14497           return true;
14498
14499         return false;
14500       }
14501
14502     case COND_EXPR:
14503       sub_strict_overflow_p = false;
14504       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14505                                      &sub_strict_overflow_p)
14506           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
14507                                         &sub_strict_overflow_p))
14508         {
14509           if (sub_strict_overflow_p)
14510             *strict_overflow_p = true;
14511           return true;
14512         }
14513       break;
14514
14515     default:
14516       break;
14517     }
14518   return false;
14519 }
14520
14521 /* Return true when T is an address and is known to be nonzero.
14522    For floating point we further ensure that T is not denormal.
14523    Similar logic is present in nonzero_address in rtlanal.h.
14524
14525    If the return value is based on the assumption that signed overflow
14526    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14527    change *STRICT_OVERFLOW_P.  */
14528
14529 bool
14530 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14531 {
14532   tree type = TREE_TYPE (t);
14533   enum tree_code code;
14534
14535   /* Doing something useful for floating point would need more work.  */
14536   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14537     return false;
14538
14539   code = TREE_CODE (t);
14540   switch (TREE_CODE_CLASS (code))
14541     {
14542     case tcc_unary:
14543       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
14544                                               strict_overflow_p);
14545     case tcc_binary:
14546     case tcc_comparison:
14547       return tree_binary_nonzero_warnv_p (code, type,
14548                                                TREE_OPERAND (t, 0),
14549                                                TREE_OPERAND (t, 1),
14550                                                strict_overflow_p);
14551     case tcc_constant:
14552     case tcc_declaration:
14553     case tcc_reference:
14554       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
14555
14556     default:
14557       break;
14558     }
14559
14560   switch (code)
14561     {
14562     case TRUTH_NOT_EXPR:
14563       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
14564                                               strict_overflow_p);
14565
14566     case TRUTH_AND_EXPR:
14567     case TRUTH_OR_EXPR:
14568     case TRUTH_XOR_EXPR:
14569       return tree_binary_nonzero_warnv_p (code, type,
14570                                                TREE_OPERAND (t, 0),
14571                                                TREE_OPERAND (t, 1),
14572                                                strict_overflow_p);
14573
14574     case COND_EXPR:
14575     case CONSTRUCTOR:
14576     case OBJ_TYPE_REF:
14577     case ASSERT_EXPR:
14578     case ADDR_EXPR:
14579     case WITH_SIZE_EXPR:
14580     case EXC_PTR_EXPR:
14581     case SSA_NAME:
14582     case FILTER_EXPR:
14583       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
14584
14585     case COMPOUND_EXPR:
14586     case MODIFY_EXPR:
14587     case GIMPLE_MODIFY_STMT:
14588     case BIND_EXPR:
14589       return tree_expr_nonzero_warnv_p (GENERIC_TREE_OPERAND (t, 1),
14590                                         strict_overflow_p);
14591
14592     case SAVE_EXPR:
14593       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14594                                         strict_overflow_p);
14595
14596     case CALL_EXPR:
14597       return alloca_call_p (t);
14598
14599     default:
14600       break;
14601     }
14602   return false;
14603 }
14604
14605 /* Return true when T is an address and is known to be nonzero.
14606    Handle warnings about undefined signed overflow.  */
14607
14608 bool
14609 tree_expr_nonzero_p (tree t)
14610 {
14611   bool ret, strict_overflow_p;
14612
14613   strict_overflow_p = false;
14614   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
14615   if (strict_overflow_p)
14616     fold_overflow_warning (("assuming signed overflow does not occur when "
14617                             "determining that expression is always "
14618                             "non-zero"),
14619                            WARN_STRICT_OVERFLOW_MISC);
14620   return ret;
14621 }
14622
14623 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
14624    attempt to fold the expression to a constant without modifying TYPE,
14625    OP0 or OP1.
14626
14627    If the expression could be simplified to a constant, then return
14628    the constant.  If the expression would not be simplified to a
14629    constant, then return NULL_TREE.  */
14630
14631 tree
14632 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
14633 {
14634   tree tem = fold_binary (code, type, op0, op1);
14635   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14636 }
14637
14638 /* Given the components of a unary expression CODE, TYPE and OP0,
14639    attempt to fold the expression to a constant without modifying
14640    TYPE or OP0.
14641
14642    If the expression could be simplified to a constant, then return
14643    the constant.  If the expression would not be simplified to a
14644    constant, then return NULL_TREE.  */
14645
14646 tree
14647 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
14648 {
14649   tree tem = fold_unary (code, type, op0);
14650   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14651 }
14652
14653 /* If EXP represents referencing an element in a constant string
14654    (either via pointer arithmetic or array indexing), return the
14655    tree representing the value accessed, otherwise return NULL.  */
14656
14657 tree
14658 fold_read_from_constant_string (tree exp)
14659 {
14660   if ((TREE_CODE (exp) == INDIRECT_REF
14661        || TREE_CODE (exp) == ARRAY_REF)
14662       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
14663     {
14664       tree exp1 = TREE_OPERAND (exp, 0);
14665       tree index;
14666       tree string;
14667
14668       if (TREE_CODE (exp) == INDIRECT_REF)
14669         string = string_constant (exp1, &index);
14670       else
14671         {
14672           tree low_bound = array_ref_low_bound (exp);
14673           index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
14674
14675           /* Optimize the special-case of a zero lower bound.
14676
14677              We convert the low_bound to sizetype to avoid some problems
14678              with constant folding.  (E.g. suppose the lower bound is 1,
14679              and its mode is QI.  Without the conversion,l (ARRAY
14680              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
14681              +INDEX), which becomes (ARRAY+255+INDEX).  Opps!)  */
14682           if (! integer_zerop (low_bound))
14683             index = size_diffop (index, fold_convert (sizetype, low_bound));
14684
14685           string = exp1;
14686         }
14687
14688       if (string
14689           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
14690           && TREE_CODE (string) == STRING_CST
14691           && TREE_CODE (index) == INTEGER_CST
14692           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
14693           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
14694               == MODE_INT)
14695           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
14696         return build_int_cst_type (TREE_TYPE (exp),
14697                                    (TREE_STRING_POINTER (string)
14698                                     [TREE_INT_CST_LOW (index)]));
14699     }
14700   return NULL;
14701 }
14702
14703 /* Return the tree for neg (ARG0) when ARG0 is known to be either
14704    an integer constant, real, or fixed-point constant.
14705
14706    TYPE is the type of the result.  */
14707
14708 static tree
14709 fold_negate_const (tree arg0, tree type)
14710 {
14711   tree t = NULL_TREE;
14712
14713   switch (TREE_CODE (arg0))
14714     {
14715     case INTEGER_CST:
14716       {
14717         unsigned HOST_WIDE_INT low;
14718         HOST_WIDE_INT high;
14719         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14720                                    TREE_INT_CST_HIGH (arg0),
14721                                    &low, &high);
14722         t = force_fit_type_double (type, low, high, 1,
14723                                    (overflow | TREE_OVERFLOW (arg0))
14724                                    && !TYPE_UNSIGNED (type));
14725         break;
14726       }
14727
14728     case REAL_CST:
14729       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
14730       break;
14731
14732     case FIXED_CST:
14733       {
14734         FIXED_VALUE_TYPE f;
14735         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
14736                                             &(TREE_FIXED_CST (arg0)), NULL,
14737                                             TYPE_SATURATING (type));
14738         t = build_fixed (type, f);
14739         /* Propagate overflow flags.  */
14740         if (overflow_p | TREE_OVERFLOW (arg0))
14741           {
14742             TREE_OVERFLOW (t) = 1;
14743             TREE_CONSTANT_OVERFLOW (t) = 1;
14744           }
14745         else if (TREE_CONSTANT_OVERFLOW (arg0))
14746           TREE_CONSTANT_OVERFLOW (t) = 1;
14747         break;
14748       }
14749
14750     default:
14751       gcc_unreachable ();
14752     }
14753
14754   return t;
14755 }
14756
14757 /* Return the tree for abs (ARG0) when ARG0 is known to be either
14758    an integer constant or real constant.
14759
14760    TYPE is the type of the result.  */
14761
14762 tree
14763 fold_abs_const (tree arg0, tree type)
14764 {
14765   tree t = NULL_TREE;
14766
14767   switch (TREE_CODE (arg0))
14768     {
14769     case INTEGER_CST:
14770       /* If the value is unsigned, then the absolute value is
14771          the same as the ordinary value.  */
14772       if (TYPE_UNSIGNED (type))
14773         t = arg0;
14774       /* Similarly, if the value is non-negative.  */
14775       else if (INT_CST_LT (integer_minus_one_node, arg0))
14776         t = arg0;
14777       /* If the value is negative, then the absolute value is
14778          its negation.  */
14779       else
14780         {
14781           unsigned HOST_WIDE_INT low;
14782           HOST_WIDE_INT high;
14783           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14784                                      TREE_INT_CST_HIGH (arg0),
14785                                      &low, &high);
14786           t = force_fit_type_double (type, low, high, -1,
14787                                      overflow | TREE_OVERFLOW (arg0));
14788         }
14789       break;
14790
14791     case REAL_CST:
14792       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
14793         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
14794       else
14795         t =  arg0;
14796       break;
14797
14798     default:
14799       gcc_unreachable ();
14800     }
14801
14802   return t;
14803 }
14804
14805 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
14806    constant.  TYPE is the type of the result.  */
14807
14808 static tree
14809 fold_not_const (tree arg0, tree type)
14810 {
14811   tree t = NULL_TREE;
14812
14813   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
14814
14815   t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
14816                              ~TREE_INT_CST_HIGH (arg0), 0,
14817                              TREE_OVERFLOW (arg0));
14818
14819   return t;
14820 }
14821
14822 /* Given CODE, a relational operator, the target type, TYPE and two
14823    constant operands OP0 and OP1, return the result of the
14824    relational operation.  If the result is not a compile time
14825    constant, then return NULL_TREE.  */
14826
14827 static tree
14828 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
14829 {
14830   int result, invert;
14831
14832   /* From here on, the only cases we handle are when the result is
14833      known to be a constant.  */
14834
14835   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
14836     {
14837       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
14838       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
14839
14840       /* Handle the cases where either operand is a NaN.  */
14841       if (real_isnan (c0) || real_isnan (c1))
14842         {
14843           switch (code)
14844             {
14845             case EQ_EXPR:
14846             case ORDERED_EXPR:
14847               result = 0;
14848               break;
14849
14850             case NE_EXPR:
14851             case UNORDERED_EXPR:
14852             case UNLT_EXPR:
14853             case UNLE_EXPR:
14854             case UNGT_EXPR:
14855             case UNGE_EXPR:
14856             case UNEQ_EXPR:
14857               result = 1;
14858               break;
14859
14860             case LT_EXPR:
14861             case LE_EXPR:
14862             case GT_EXPR:
14863             case GE_EXPR:
14864             case LTGT_EXPR:
14865               if (flag_trapping_math)
14866                 return NULL_TREE;
14867               result = 0;
14868               break;
14869
14870             default:
14871               gcc_unreachable ();
14872             }
14873
14874           return constant_boolean_node (result, type);
14875         }
14876
14877       return constant_boolean_node (real_compare (code, c0, c1), type);
14878     }
14879
14880   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
14881     {
14882       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
14883       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
14884       return constant_boolean_node (fixed_compare (code, c0, c1), type);
14885     }
14886
14887   /* Handle equality/inequality of complex constants.  */
14888   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
14889     {
14890       tree rcond = fold_relational_const (code, type,
14891                                           TREE_REALPART (op0),
14892                                           TREE_REALPART (op1));
14893       tree icond = fold_relational_const (code, type,
14894                                           TREE_IMAGPART (op0),
14895                                           TREE_IMAGPART (op1));
14896       if (code == EQ_EXPR)
14897         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
14898       else if (code == NE_EXPR)
14899         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
14900       else
14901         return NULL_TREE;
14902     }
14903
14904   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14905
14906      To compute GT, swap the arguments and do LT.
14907      To compute GE, do LT and invert the result.
14908      To compute LE, swap the arguments, do LT and invert the result.
14909      To compute NE, do EQ and invert the result.
14910
14911      Therefore, the code below must handle only EQ and LT.  */
14912
14913   if (code == LE_EXPR || code == GT_EXPR)
14914     {
14915       tree tem = op0;
14916       op0 = op1;
14917       op1 = tem;
14918       code = swap_tree_comparison (code);
14919     }
14920
14921   /* Note that it is safe to invert for real values here because we
14922      have already handled the one case that it matters.  */
14923
14924   invert = 0;
14925   if (code == NE_EXPR || code == GE_EXPR)
14926     {
14927       invert = 1;
14928       code = invert_tree_comparison (code, false);
14929     }
14930
14931   /* Compute a result for LT or EQ if args permit;
14932      Otherwise return T.  */
14933   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14934     {
14935       if (code == EQ_EXPR)
14936         result = tree_int_cst_equal (op0, op1);
14937       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
14938         result = INT_CST_LT_UNSIGNED (op0, op1);
14939       else
14940         result = INT_CST_LT (op0, op1);
14941     }
14942   else
14943     return NULL_TREE;
14944
14945   if (invert)
14946     result ^= 1;
14947   return constant_boolean_node (result, type);
14948 }
14949
14950 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14951    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
14952    itself.  */
14953
14954 tree
14955 fold_build_cleanup_point_expr (tree type, tree expr)
14956 {
14957   /* If the expression does not have side effects then we don't have to wrap
14958      it with a cleanup point expression.  */
14959   if (!TREE_SIDE_EFFECTS (expr))
14960     return expr;
14961
14962   /* If the expression is a return, check to see if the expression inside the
14963      return has no side effects or the right hand side of the modify expression
14964      inside the return. If either don't have side effects set we don't need to
14965      wrap the expression in a cleanup point expression.  Note we don't check the
14966      left hand side of the modify because it should always be a return decl.  */
14967   if (TREE_CODE (expr) == RETURN_EXPR)
14968     {
14969       tree op = TREE_OPERAND (expr, 0);
14970       if (!op || !TREE_SIDE_EFFECTS (op))
14971         return expr;
14972       op = TREE_OPERAND (op, 1);
14973       if (!TREE_SIDE_EFFECTS (op))
14974         return expr;
14975     }
14976   
14977   return build1 (CLEANUP_POINT_EXPR, type, expr);
14978 }
14979
14980 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14981    of an indirection through OP0, or NULL_TREE if no simplification is
14982    possible.  */
14983
14984 tree
14985 fold_indirect_ref_1 (tree type, tree op0)
14986 {
14987   tree sub = op0;
14988   tree subtype;
14989
14990   STRIP_NOPS (sub);
14991   subtype = TREE_TYPE (sub);
14992   if (!POINTER_TYPE_P (subtype))
14993     return NULL_TREE;
14994
14995   if (TREE_CODE (sub) == ADDR_EXPR)
14996     {
14997       tree op = TREE_OPERAND (sub, 0);
14998       tree optype = TREE_TYPE (op);
14999       /* *&CONST_DECL -> to the value of the const decl.  */
15000       if (TREE_CODE (op) == CONST_DECL)
15001         return DECL_INITIAL (op);
15002       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
15003       if (type == optype)
15004         {
15005           tree fop = fold_read_from_constant_string (op);
15006           if (fop)
15007             return fop;
15008           else
15009             return op;
15010         }
15011       /* *(foo *)&fooarray => fooarray[0] */
15012       else if (TREE_CODE (optype) == ARRAY_TYPE
15013                && type == TREE_TYPE (optype))
15014         {
15015           tree type_domain = TYPE_DOMAIN (optype);
15016           tree min_val = size_zero_node;
15017           if (type_domain && TYPE_MIN_VALUE (type_domain))
15018             min_val = TYPE_MIN_VALUE (type_domain);
15019           return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15020         }
15021       /* *(foo *)&complexfoo => __real__ complexfoo */
15022       else if (TREE_CODE (optype) == COMPLEX_TYPE
15023                && type == TREE_TYPE (optype))
15024         return fold_build1 (REALPART_EXPR, type, op);
15025       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15026       else if (TREE_CODE (optype) == VECTOR_TYPE
15027                && type == TREE_TYPE (optype))
15028         {
15029           tree part_width = TYPE_SIZE (type);
15030           tree index = bitsize_int (0);
15031           return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
15032         }
15033     }
15034
15035   /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15036   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15037       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15038     { 
15039       tree op00 = TREE_OPERAND (sub, 0);
15040       tree op01 = TREE_OPERAND (sub, 1);
15041       tree op00type;
15042       
15043       STRIP_NOPS (op00);
15044       op00type = TREE_TYPE (op00);
15045       if (TREE_CODE (op00) == ADDR_EXPR
15046           && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15047           && type == TREE_TYPE (TREE_TYPE (op00type)))
15048         { 
15049           HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15050           tree part_width = TYPE_SIZE (type);
15051           unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15052           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15053           tree index = bitsize_int (indexi);
15054
15055           if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15056             return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15057                                 part_width, index);
15058         
15059         }
15060     }
15061
15062
15063   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15064   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15065       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15066     {
15067       tree op00 = TREE_OPERAND (sub, 0);
15068       tree op01 = TREE_OPERAND (sub, 1);
15069       tree op00type;
15070
15071       STRIP_NOPS (op00);
15072       op00type = TREE_TYPE (op00);
15073       if (TREE_CODE (op00) == ADDR_EXPR
15074           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15075           && type == TREE_TYPE (TREE_TYPE (op00type)))
15076         {
15077           tree size = TYPE_SIZE_UNIT (type);
15078           if (tree_int_cst_equal (size, op01))
15079             return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
15080         }
15081     }
15082   
15083   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15084   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15085       && type == TREE_TYPE (TREE_TYPE (subtype)))
15086     {
15087       tree type_domain;
15088       tree min_val = size_zero_node;
15089       sub = build_fold_indirect_ref (sub);
15090       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15091       if (type_domain && TYPE_MIN_VALUE (type_domain))
15092         min_val = TYPE_MIN_VALUE (type_domain);
15093       return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15094     }
15095
15096   return NULL_TREE;
15097 }
15098
15099 /* Builds an expression for an indirection through T, simplifying some
15100    cases.  */
15101
15102 tree
15103 build_fold_indirect_ref (tree t)
15104 {
15105   tree type = TREE_TYPE (TREE_TYPE (t));
15106   tree sub = fold_indirect_ref_1 (type, t);
15107
15108   if (sub)
15109     return sub;
15110   else
15111     return build1 (INDIRECT_REF, type, t);
15112 }
15113
15114 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
15115
15116 tree
15117 fold_indirect_ref (tree t)
15118 {
15119   tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
15120
15121   if (sub)
15122     return sub;
15123   else
15124     return t;
15125 }
15126
15127 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15128    whose result is ignored.  The type of the returned tree need not be
15129    the same as the original expression.  */
15130
15131 tree
15132 fold_ignored_result (tree t)
15133 {
15134   if (!TREE_SIDE_EFFECTS (t))
15135     return integer_zero_node;
15136
15137   for (;;)
15138     switch (TREE_CODE_CLASS (TREE_CODE (t)))
15139       {
15140       case tcc_unary:
15141         t = TREE_OPERAND (t, 0);
15142         break;
15143
15144       case tcc_binary:
15145       case tcc_comparison:
15146         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15147           t = TREE_OPERAND (t, 0);
15148         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15149           t = TREE_OPERAND (t, 1);
15150         else
15151           return t;
15152         break;
15153
15154       case tcc_expression:
15155         switch (TREE_CODE (t))
15156           {
15157           case COMPOUND_EXPR:
15158             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15159               return t;
15160             t = TREE_OPERAND (t, 0);
15161             break;
15162
15163           case COND_EXPR:
15164             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15165                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15166               return t;
15167             t = TREE_OPERAND (t, 0);
15168             break;
15169
15170           default:
15171             return t;
15172           }
15173         break;
15174
15175       default:
15176         return t;
15177       }
15178 }
15179
15180 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15181    This can only be applied to objects of a sizetype.  */
15182
15183 tree
15184 round_up (tree value, int divisor)
15185 {
15186   tree div = NULL_TREE;
15187
15188   gcc_assert (divisor > 0);
15189   if (divisor == 1)
15190     return value;
15191
15192   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15193      have to do anything.  Only do this when we are not given a const,
15194      because in that case, this check is more expensive than just
15195      doing it.  */
15196   if (TREE_CODE (value) != INTEGER_CST)
15197     {
15198       div = build_int_cst (TREE_TYPE (value), divisor);
15199
15200       if (multiple_of_p (TREE_TYPE (value), value, div))
15201         return value;
15202     }
15203
15204   /* If divisor is a power of two, simplify this to bit manipulation.  */
15205   if (divisor == (divisor & -divisor))
15206     {
15207       if (TREE_CODE (value) == INTEGER_CST)
15208         {
15209           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15210           unsigned HOST_WIDE_INT high;
15211           bool overflow_p;
15212
15213           if ((low & (divisor - 1)) == 0)
15214             return value;
15215
15216           overflow_p = TREE_OVERFLOW (value);
15217           high = TREE_INT_CST_HIGH (value);
15218           low &= ~(divisor - 1);
15219           low += divisor;
15220           if (low == 0)
15221             {
15222               high++;
15223               if (high == 0)
15224                 overflow_p = true;
15225             }
15226
15227           return force_fit_type_double (TREE_TYPE (value), low, high,
15228                                         -1, overflow_p);
15229         }
15230       else
15231         {
15232           tree t;
15233
15234           t = build_int_cst (TREE_TYPE (value), divisor - 1);
15235           value = size_binop (PLUS_EXPR, value, t);
15236           t = build_int_cst (TREE_TYPE (value), -divisor);
15237           value = size_binop (BIT_AND_EXPR, value, t);
15238         }
15239     }
15240   else
15241     {
15242       if (!div)
15243         div = build_int_cst (TREE_TYPE (value), divisor);
15244       value = size_binop (CEIL_DIV_EXPR, value, div);
15245       value = size_binop (MULT_EXPR, value, div);
15246     }
15247
15248   return value;
15249 }
15250
15251 /* Likewise, but round down.  */
15252
15253 tree
15254 round_down (tree value, int divisor)
15255 {
15256   tree div = NULL_TREE;
15257
15258   gcc_assert (divisor > 0);
15259   if (divisor == 1)
15260     return value;
15261
15262   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15263      have to do anything.  Only do this when we are not given a const,
15264      because in that case, this check is more expensive than just
15265      doing it.  */
15266   if (TREE_CODE (value) != INTEGER_CST)
15267     {
15268       div = build_int_cst (TREE_TYPE (value), divisor);
15269
15270       if (multiple_of_p (TREE_TYPE (value), value, div))
15271         return value;
15272     }
15273
15274   /* If divisor is a power of two, simplify this to bit manipulation.  */
15275   if (divisor == (divisor & -divisor))
15276     {
15277       tree t;
15278
15279       t = build_int_cst (TREE_TYPE (value), -divisor);
15280       value = size_binop (BIT_AND_EXPR, value, t);
15281     }
15282   else
15283     {
15284       if (!div)
15285         div = build_int_cst (TREE_TYPE (value), divisor);
15286       value = size_binop (FLOOR_DIV_EXPR, value, div);
15287       value = size_binop (MULT_EXPR, value, div);
15288     }
15289
15290   return value;
15291 }
15292
15293 /* Returns the pointer to the base of the object addressed by EXP and
15294    extracts the information about the offset of the access, storing it
15295    to PBITPOS and POFFSET.  */
15296
15297 static tree
15298 split_address_to_core_and_offset (tree exp,
15299                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15300 {
15301   tree core;
15302   enum machine_mode mode;
15303   int unsignedp, volatilep;
15304   HOST_WIDE_INT bitsize;
15305
15306   if (TREE_CODE (exp) == ADDR_EXPR)
15307     {
15308       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15309                                   poffset, &mode, &unsignedp, &volatilep,
15310                                   false);
15311       core = fold_addr_expr (core);
15312     }
15313   else
15314     {
15315       core = exp;
15316       *pbitpos = 0;
15317       *poffset = NULL_TREE;
15318     }
15319
15320   return core;
15321 }
15322
15323 /* Returns true if addresses of E1 and E2 differ by a constant, false
15324    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
15325
15326 bool
15327 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15328 {
15329   tree core1, core2;
15330   HOST_WIDE_INT bitpos1, bitpos2;
15331   tree toffset1, toffset2, tdiff, type;
15332
15333   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15334   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15335
15336   if (bitpos1 % BITS_PER_UNIT != 0
15337       || bitpos2 % BITS_PER_UNIT != 0
15338       || !operand_equal_p (core1, core2, 0))
15339     return false;
15340
15341   if (toffset1 && toffset2)
15342     {
15343       type = TREE_TYPE (toffset1);
15344       if (type != TREE_TYPE (toffset2))
15345         toffset2 = fold_convert (type, toffset2);
15346
15347       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15348       if (!cst_and_fits_in_hwi (tdiff))
15349         return false;
15350
15351       *diff = int_cst_value (tdiff);
15352     }
15353   else if (toffset1 || toffset2)
15354     {
15355       /* If only one of the offsets is non-constant, the difference cannot
15356          be a constant.  */
15357       return false;
15358     }
15359   else
15360     *diff = 0;
15361
15362   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15363   return true;
15364 }
15365
15366 /* Simplify the floating point expression EXP when the sign of the
15367    result is not significant.  Return NULL_TREE if no simplification
15368    is possible.  */
15369
15370 tree
15371 fold_strip_sign_ops (tree exp)
15372 {
15373   tree arg0, arg1;
15374
15375   switch (TREE_CODE (exp))
15376     {
15377     case ABS_EXPR:
15378     case NEGATE_EXPR:
15379       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15380       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15381
15382     case MULT_EXPR:
15383     case RDIV_EXPR:
15384       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15385         return NULL_TREE;
15386       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15387       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15388       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15389         return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15390                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
15391                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
15392       break;
15393
15394     case COMPOUND_EXPR:
15395       arg0 = TREE_OPERAND (exp, 0);
15396       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15397       if (arg1)
15398         return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15399       break;
15400       
15401     case COND_EXPR:
15402       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15403       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15404       if (arg0 || arg1)
15405         return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15406                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
15407                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
15408       break;
15409       
15410     case CALL_EXPR:
15411       {
15412         const enum built_in_function fcode = builtin_mathfn_code (exp);
15413         switch (fcode)
15414         {
15415         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15416           /* Strip copysign function call, return the 1st argument. */
15417           arg0 = CALL_EXPR_ARG (exp, 0);
15418           arg1 = CALL_EXPR_ARG (exp, 1);
15419           return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15420
15421         default:
15422           /* Strip sign ops from the argument of "odd" math functions.  */
15423           if (negate_mathfn_p (fcode))
15424             {
15425               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15426               if (arg0)
15427                 return build_call_expr (get_callee_fndecl (exp), 1, arg0);
15428             }
15429           break;
15430         }
15431       }
15432       break;
15433
15434     default:
15435       break;
15436     }
15437   return NULL_TREE;
15438 }